ASTRecordReader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ASTRecordReader.h - Helper classes for reading AST -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines classes that are useful in the implementation of
  15. // the ASTReader.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SERIALIZATION_ASTRECORDREADER_H
  19. #define LLVM_CLANG_SERIALIZATION_ASTRECORDREADER_H
  20. #include "clang/AST/ASTContext.h"
  21. #include "clang/AST/AbstractBasicReader.h"
  22. #include "clang/Lex/Token.h"
  23. #include "clang/Serialization/ASTReader.h"
  24. #include "clang/Serialization/SourceLocationEncoding.h"
  25. #include "llvm/ADT/APFloat.h"
  26. #include "llvm/ADT/APInt.h"
  27. #include "llvm/ADT/APSInt.h"
  28. namespace clang {
  29. class OMPTraitInfo;
  30. class OMPChildren;
  31. /// An object for streaming information from a record.
  32. class ASTRecordReader
  33. : public serialization::DataStreamBasicReader<ASTRecordReader> {
  34. using ModuleFile = serialization::ModuleFile;
  35. using LocSeq = SourceLocationSequence;
  36. ASTReader *Reader;
  37. ModuleFile *F;
  38. unsigned Idx = 0;
  39. ASTReader::RecordData Record;
  40. using RecordData = ASTReader::RecordData;
  41. using RecordDataImpl = ASTReader::RecordDataImpl;
  42. public:
  43. /// Construct an ASTRecordReader that uses the default encoding scheme.
  44. ASTRecordReader(ASTReader &Reader, ModuleFile &F)
  45. : DataStreamBasicReader(Reader.getContext()), Reader(&Reader), F(&F) {}
  46. /// Reads a record with id AbbrevID from Cursor, resetting the
  47. /// internal state.
  48. Expected<unsigned> readRecord(llvm::BitstreamCursor &Cursor,
  49. unsigned AbbrevID);
  50. /// Is this a module file for a module (rather than a PCH or similar).
  51. bool isModule() const { return F->isModule(); }
  52. /// Retrieve the AST context that this AST reader supplements.
  53. ASTContext &getContext() { return Reader->getContext(); }
  54. /// The current position in this record.
  55. unsigned getIdx() const { return Idx; }
  56. /// The length of this record.
  57. size_t size() const { return Record.size(); }
  58. /// An arbitrary index in this record.
  59. const uint64_t &operator[](size_t N) { return Record[N]; }
  60. /// Returns the last value in this record.
  61. uint64_t back() { return Record.back(); }
  62. /// Returns the current value in this record, and advances to the
  63. /// next value.
  64. uint64_t readInt() { return Record[Idx++]; }
  65. ArrayRef<uint64_t> readIntArray(unsigned Len) {
  66. auto Array = llvm::ArrayRef(Record).slice(Idx, Len);
  67. Idx += Len;
  68. return Array;
  69. }
  70. /// Returns the current value in this record, without advancing.
  71. uint64_t peekInt() { return Record[Idx]; }
  72. /// Skips the specified number of values.
  73. void skipInts(unsigned N) { Idx += N; }
  74. /// Retrieve the global submodule ID its local ID number.
  75. serialization::SubmoduleID
  76. getGlobalSubmoduleID(unsigned LocalID) {
  77. return Reader->getGlobalSubmoduleID(*F, LocalID);
  78. }
  79. /// Retrieve the submodule that corresponds to a global submodule ID.
  80. Module *getSubmodule(serialization::SubmoduleID GlobalID) {
  81. return Reader->getSubmodule(GlobalID);
  82. }
  83. /// Read the record that describes the lexical contents of a DC.
  84. bool readLexicalDeclContextStorage(uint64_t Offset, DeclContext *DC) {
  85. return Reader->ReadLexicalDeclContextStorage(*F, F->DeclsCursor, Offset,
  86. DC);
  87. }
  88. /// Read the record that describes the visible contents of a DC.
  89. bool readVisibleDeclContextStorage(uint64_t Offset,
  90. serialization::DeclID ID) {
  91. return Reader->ReadVisibleDeclContextStorage(*F, F->DeclsCursor, Offset,
  92. ID);
  93. }
  94. ExplicitSpecifier readExplicitSpec() {
  95. uint64_t Kind = readInt();
  96. bool HasExpr = Kind & 0x1;
  97. Kind = Kind >> 1;
  98. return ExplicitSpecifier(HasExpr ? readExpr() : nullptr,
  99. static_cast<ExplicitSpecKind>(Kind));
  100. }
  101. /// Read information about an exception specification (inherited).
  102. //FunctionProtoType::ExceptionSpecInfo
  103. //readExceptionSpecInfo(SmallVectorImpl<QualType> &ExceptionStorage);
  104. /// Get the global offset corresponding to a local offset.
  105. uint64_t getGlobalBitOffset(uint64_t LocalOffset) {
  106. return Reader->getGlobalBitOffset(*F, LocalOffset);
  107. }
  108. /// Reads a statement.
  109. Stmt *readStmt() { return Reader->ReadStmt(*F); }
  110. Stmt *readStmtRef() { return readStmt(); /* FIXME: readSubStmt? */ }
  111. /// Reads an expression.
  112. Expr *readExpr() { return Reader->ReadExpr(*F); }
  113. /// Reads a sub-statement operand during statement reading.
  114. Stmt *readSubStmt() { return Reader->ReadSubStmt(); }
  115. /// Reads a sub-expression operand during statement reading.
  116. Expr *readSubExpr() { return Reader->ReadSubExpr(); }
  117. /// Reads a declaration with the given local ID in the given module.
  118. ///
  119. /// \returns The requested declaration, casted to the given return type.
  120. template<typename T>
  121. T *GetLocalDeclAs(uint32_t LocalID) {
  122. return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
  123. }
  124. /// Reads a TemplateArgumentLocInfo appropriate for the
  125. /// given TemplateArgument kind, advancing Idx.
  126. TemplateArgumentLocInfo
  127. readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind);
  128. /// Reads a TemplateArgumentLoc, advancing Idx.
  129. TemplateArgumentLoc readTemplateArgumentLoc();
  130. const ASTTemplateArgumentListInfo*
  131. readASTTemplateArgumentListInfo();
  132. /// Reads a declarator info from the given record, advancing Idx.
  133. TypeSourceInfo *readTypeSourceInfo();
  134. /// Reads the location information for a type.
  135. void readTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);
  136. /// Map a local type ID within a given AST file to a global type ID.
  137. serialization::TypeID getGlobalTypeID(unsigned LocalID) const {
  138. return Reader->getGlobalTypeID(*F, LocalID);
  139. }
  140. Qualifiers readQualifiers() {
  141. return Qualifiers::fromOpaqueValue(readInt());
  142. }
  143. /// Read a type from the current position in the record.
  144. QualType readType() {
  145. return Reader->readType(*F, Record, Idx);
  146. }
  147. QualType readQualType() {
  148. return readType();
  149. }
  150. /// Reads a declaration ID from the given position in this record.
  151. ///
  152. /// \returns The declaration ID read from the record, adjusted to a global ID.
  153. serialization::DeclID readDeclID() {
  154. return Reader->ReadDeclID(*F, Record, Idx);
  155. }
  156. /// Reads a declaration from the given position in a record in the
  157. /// given module, advancing Idx.
  158. Decl *readDecl() {
  159. return Reader->ReadDecl(*F, Record, Idx);
  160. }
  161. Decl *readDeclRef() {
  162. return readDecl();
  163. }
  164. /// Reads a declaration from the given position in the record,
  165. /// advancing Idx.
  166. ///
  167. /// \returns The declaration read from this location, casted to the given
  168. /// result type.
  169. template<typename T>
  170. T *readDeclAs() {
  171. return Reader->ReadDeclAs<T>(*F, Record, Idx);
  172. }
  173. IdentifierInfo *readIdentifier() {
  174. return Reader->readIdentifier(*F, Record, Idx);
  175. }
  176. /// Read a selector from the Record, advancing Idx.
  177. Selector readSelector() {
  178. return Reader->ReadSelector(*F, Record, Idx);
  179. }
  180. /// Read a declaration name, advancing Idx.
  181. // DeclarationName readDeclarationName(); (inherited)
  182. DeclarationNameLoc readDeclarationNameLoc(DeclarationName Name);
  183. DeclarationNameInfo readDeclarationNameInfo();
  184. void readQualifierInfo(QualifierInfo &Info);
  185. /// Return a nested name specifier, advancing Idx.
  186. // NestedNameSpecifier *readNestedNameSpecifier(); (inherited)
  187. NestedNameSpecifierLoc readNestedNameSpecifierLoc();
  188. /// Read a template name, advancing Idx.
  189. // TemplateName readTemplateName(); (inherited)
  190. /// Read a template argument, advancing Idx. (inherited)
  191. // TemplateArgument readTemplateArgument();
  192. using DataStreamBasicReader::readTemplateArgument;
  193. TemplateArgument readTemplateArgument(bool Canonicalize) {
  194. TemplateArgument Arg = readTemplateArgument();
  195. if (Canonicalize) {
  196. Arg = getContext().getCanonicalTemplateArgument(Arg);
  197. }
  198. return Arg;
  199. }
  200. /// Read a template parameter list, advancing Idx.
  201. TemplateParameterList *readTemplateParameterList();
  202. /// Read a template argument array, advancing Idx.
  203. void readTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
  204. bool Canonicalize = false);
  205. /// Read a UnresolvedSet structure, advancing Idx.
  206. void readUnresolvedSet(LazyASTUnresolvedSet &Set);
  207. /// Read a C++ base specifier, advancing Idx.
  208. CXXBaseSpecifier readCXXBaseSpecifier();
  209. /// Read a CXXCtorInitializer array, advancing Idx.
  210. CXXCtorInitializer **readCXXCtorInitializers();
  211. CXXTemporary *readCXXTemporary() {
  212. return Reader->ReadCXXTemporary(*F, Record, Idx);
  213. }
  214. /// Read an OMPTraitInfo object, advancing Idx.
  215. OMPTraitInfo *readOMPTraitInfo();
  216. /// Read an OpenMP clause, advancing Idx.
  217. OMPClause *readOMPClause();
  218. /// Read an OpenMP children, advancing Idx.
  219. void readOMPChildren(OMPChildren *Data);
  220. /// Read a source location, advancing Idx.
  221. SourceLocation readSourceLocation(LocSeq *Seq = nullptr) {
  222. return Reader->ReadSourceLocation(*F, Record, Idx, Seq);
  223. }
  224. /// Read a source range, advancing Idx.
  225. SourceRange readSourceRange(LocSeq *Seq = nullptr) {
  226. return Reader->ReadSourceRange(*F, Record, Idx, Seq);
  227. }
  228. /// Read an arbitrary constant value, advancing Idx.
  229. // APValue readAPValue(); (inherited)
  230. /// Read an integral value, advancing Idx.
  231. // llvm::APInt readAPInt(); (inherited)
  232. /// Read a signed integral value, advancing Idx.
  233. // llvm::APSInt readAPSInt(); (inherited)
  234. /// Read a floating-point value, advancing Idx.
  235. llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem);
  236. /// Read a boolean value, advancing Idx.
  237. bool readBool() { return readInt() != 0; }
  238. /// Read a 32-bit unsigned value; required to satisfy BasicReader.
  239. uint32_t readUInt32() {
  240. return uint32_t(readInt());
  241. }
  242. /// Read a 64-bit unsigned value; required to satisfy BasicReader.
  243. uint64_t readUInt64() {
  244. return readInt();
  245. }
  246. /// Read a string, advancing Idx.
  247. std::string readString() {
  248. return Reader->ReadString(Record, Idx);
  249. }
  250. /// Read a path, advancing Idx.
  251. std::string readPath() {
  252. return Reader->ReadPath(*F, Record, Idx);
  253. }
  254. /// Read a version tuple, advancing Idx.
  255. VersionTuple readVersionTuple() {
  256. return ASTReader::ReadVersionTuple(Record, Idx);
  257. }
  258. /// Reads one attribute from the current stream position, advancing Idx.
  259. Attr *readAttr();
  260. /// Reads attributes from the current stream position, advancing Idx.
  261. void readAttributes(AttrVec &Attrs);
  262. /// Read an BTFTypeTagAttr object.
  263. BTFTypeTagAttr *readBTFTypeTagAttr() {
  264. return cast<BTFTypeTagAttr>(readAttr());
  265. }
  266. /// Reads a token out of a record, advancing Idx.
  267. Token readToken() {
  268. return Reader->ReadToken(*F, Record, Idx);
  269. }
  270. void recordSwitchCaseID(SwitchCase *SC, unsigned ID) {
  271. Reader->RecordSwitchCaseID(SC, ID);
  272. }
  273. /// Retrieve the switch-case statement with the given ID.
  274. SwitchCase *getSwitchCaseWithID(unsigned ID) {
  275. return Reader->getSwitchCaseWithID(ID);
  276. }
  277. };
  278. /// Helper class that saves the current stream position and
  279. /// then restores it when destroyed.
  280. struct SavedStreamPosition {
  281. explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
  282. : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) {}
  283. ~SavedStreamPosition() {
  284. if (llvm::Error Err = Cursor.JumpToBit(Offset))
  285. llvm::report_fatal_error(
  286. llvm::Twine("Cursor should always be able to go back, failed: ") +
  287. toString(std::move(Err)));
  288. }
  289. private:
  290. llvm::BitstreamCursor &Cursor;
  291. uint64_t Offset;
  292. };
  293. inline void PCHValidator::Error(const char *Msg) {
  294. Reader.Error(Msg);
  295. }
  296. } // namespace clang
  297. #endif
  298. #ifdef __GNUC__
  299. #pragma GCC diagnostic pop
  300. #endif