ASTRecordWriter.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ASTRecordWriter.h - Helper classes for writing 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 the ASTRecordWriter class, a helper class useful
  15. // when serializing AST.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SERIALIZATION_ASTRECORDWRITER_H
  19. #define LLVM_CLANG_SERIALIZATION_ASTRECORDWRITER_H
  20. #include "clang/AST/AbstractBasicWriter.h"
  21. #include "clang/AST/OpenMPClause.h"
  22. #include "clang/Serialization/ASTWriter.h"
  23. #include "clang/Serialization/SourceLocationEncoding.h"
  24. namespace clang {
  25. class TypeLoc;
  26. /// An object for streaming information to a record.
  27. class ASTRecordWriter
  28. : public serialization::DataStreamBasicWriter<ASTRecordWriter> {
  29. using LocSeq = SourceLocationSequence;
  30. ASTWriter *Writer;
  31. ASTWriter::RecordDataImpl *Record;
  32. /// Statements that we've encountered while serializing a
  33. /// declaration or type.
  34. SmallVector<Stmt *, 16> StmtsToEmit;
  35. /// Indices of record elements that describe offsets within the
  36. /// bitcode. These will be converted to offsets relative to the current
  37. /// record when emitted.
  38. SmallVector<unsigned, 8> OffsetIndices;
  39. /// Flush all of the statements and expressions that have
  40. /// been added to the queue via AddStmt().
  41. void FlushStmts();
  42. void FlushSubStmts();
  43. void PrepareToEmit(uint64_t MyOffset) {
  44. // Convert offsets into relative form.
  45. for (unsigned I : OffsetIndices) {
  46. auto &StoredOffset = (*Record)[I];
  47. assert(StoredOffset < MyOffset && "invalid offset");
  48. if (StoredOffset)
  49. StoredOffset = MyOffset - StoredOffset;
  50. }
  51. OffsetIndices.clear();
  52. }
  53. public:
  54. /// Construct a ASTRecordWriter that uses the default encoding scheme.
  55. ASTRecordWriter(ASTWriter &W, ASTWriter::RecordDataImpl &Record)
  56. : DataStreamBasicWriter(W.getASTContext()), Writer(&W), Record(&Record) {}
  57. /// Construct a ASTRecordWriter that uses the same encoding scheme as another
  58. /// ASTRecordWriter.
  59. ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
  60. : DataStreamBasicWriter(Parent.getASTContext()), Writer(Parent.Writer),
  61. Record(&Record) {}
  62. /// Copying an ASTRecordWriter is almost certainly a bug.
  63. ASTRecordWriter(const ASTRecordWriter &) = delete;
  64. ASTRecordWriter &operator=(const ASTRecordWriter &) = delete;
  65. /// Extract the underlying record storage.
  66. ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
  67. /// Minimal vector-like interface.
  68. /// @{
  69. void push_back(uint64_t N) { Record->push_back(N); }
  70. template<typename InputIterator>
  71. void append(InputIterator begin, InputIterator end) {
  72. Record->append(begin, end);
  73. }
  74. bool empty() const { return Record->empty(); }
  75. size_t size() const { return Record->size(); }
  76. uint64_t &operator[](size_t N) { return (*Record)[N]; }
  77. /// @}
  78. /// Emit the record to the stream, followed by its substatements, and
  79. /// return its offset.
  80. // FIXME: Allow record producers to suggest Abbrevs.
  81. uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
  82. uint64_t Offset = Writer->Stream.GetCurrentBitNo();
  83. PrepareToEmit(Offset);
  84. Writer->Stream.EmitRecord(Code, *Record, Abbrev);
  85. FlushStmts();
  86. return Offset;
  87. }
  88. /// Emit the record to the stream, preceded by its substatements.
  89. uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
  90. FlushSubStmts();
  91. PrepareToEmit(Writer->Stream.GetCurrentBitNo());
  92. Writer->Stream.EmitRecord(Code, *Record, Abbrev);
  93. return Writer->Stream.GetCurrentBitNo();
  94. }
  95. /// Add a bit offset into the record. This will be converted into an
  96. /// offset relative to the current record when emitted.
  97. void AddOffset(uint64_t BitOffset) {
  98. OffsetIndices.push_back(Record->size());
  99. Record->push_back(BitOffset);
  100. }
  101. /// Add the given statement or expression to the queue of
  102. /// statements to emit.
  103. ///
  104. /// This routine should be used when emitting types and declarations
  105. /// that have expressions as part of their formulation. Once the
  106. /// type or declaration has been written, Emit() will write
  107. /// the corresponding statements just after the record.
  108. void AddStmt(Stmt *S) {
  109. StmtsToEmit.push_back(S);
  110. }
  111. void writeStmtRef(const Stmt *S) {
  112. AddStmt(const_cast<Stmt*>(S));
  113. }
  114. /// Write an BTFTypeTagAttr object.
  115. void writeBTFTypeTagAttr(const BTFTypeTagAttr *A) { AddAttr(A); }
  116. /// Add a definition for the given function to the queue of statements
  117. /// to emit.
  118. void AddFunctionDefinition(const FunctionDecl *FD);
  119. /// Emit a source location.
  120. void AddSourceLocation(SourceLocation Loc, LocSeq *Seq = nullptr) {
  121. return Writer->AddSourceLocation(Loc, *Record, Seq);
  122. }
  123. void writeSourceLocation(SourceLocation Loc) {
  124. AddSourceLocation(Loc);
  125. }
  126. /// Emit a source range.
  127. void AddSourceRange(SourceRange Range, LocSeq *Seq = nullptr) {
  128. return Writer->AddSourceRange(Range, *Record, Seq);
  129. }
  130. void writeBool(bool Value) {
  131. Record->push_back(Value);
  132. }
  133. void writeUInt32(uint32_t Value) {
  134. Record->push_back(Value);
  135. }
  136. void writeUInt64(uint64_t Value) {
  137. Record->push_back(Value);
  138. }
  139. /// Emit an integral value.
  140. void AddAPInt(const llvm::APInt &Value) {
  141. writeAPInt(Value);
  142. }
  143. /// Emit a signed integral value.
  144. void AddAPSInt(const llvm::APSInt &Value) {
  145. writeAPSInt(Value);
  146. }
  147. /// Emit a floating-point value.
  148. void AddAPFloat(const llvm::APFloat &Value);
  149. /// Emit an APvalue.
  150. void AddAPValue(const APValue &Value) { writeAPValue(Value); }
  151. /// Emit a reference to an identifier.
  152. void AddIdentifierRef(const IdentifierInfo *II) {
  153. return Writer->AddIdentifierRef(II, *Record);
  154. }
  155. void writeIdentifier(const IdentifierInfo *II) {
  156. AddIdentifierRef(II);
  157. }
  158. /// Emit a Selector (which is a smart pointer reference).
  159. void AddSelectorRef(Selector S);
  160. void writeSelector(Selector sel) {
  161. AddSelectorRef(sel);
  162. }
  163. /// Emit a CXXTemporary.
  164. void AddCXXTemporary(const CXXTemporary *Temp);
  165. /// Emit a C++ base specifier.
  166. void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base);
  167. /// Emit a set of C++ base specifiers.
  168. void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases);
  169. /// Emit a reference to a type.
  170. void AddTypeRef(QualType T) {
  171. return Writer->AddTypeRef(T, *Record);
  172. }
  173. void writeQualType(QualType T) {
  174. AddTypeRef(T);
  175. }
  176. /// Emits a reference to a declarator info.
  177. void AddTypeSourceInfo(TypeSourceInfo *TInfo);
  178. /// Emits source location information for a type. Does not emit the type.
  179. void AddTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);
  180. /// Emits a template argument location info.
  181. void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
  182. const TemplateArgumentLocInfo &Arg);
  183. /// Emits a template argument location.
  184. void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg);
  185. /// Emits an AST template argument list info.
  186. void AddASTTemplateArgumentListInfo(
  187. const ASTTemplateArgumentListInfo *ASTTemplArgList);
  188. /// Emit a reference to a declaration.
  189. void AddDeclRef(const Decl *D) {
  190. return Writer->AddDeclRef(D, *Record);
  191. }
  192. void writeDeclRef(const Decl *D) {
  193. AddDeclRef(D);
  194. }
  195. /// Emit a declaration name.
  196. void AddDeclarationName(DeclarationName Name) {
  197. writeDeclarationName(Name);
  198. }
  199. void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
  200. DeclarationName Name);
  201. void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
  202. void AddQualifierInfo(const QualifierInfo &Info);
  203. /// Emit a nested name specifier.
  204. void AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
  205. writeNestedNameSpecifier(NNS);
  206. }
  207. /// Emit a nested name specifier with source-location information.
  208. void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
  209. /// Emit a template name.
  210. void AddTemplateName(TemplateName Name) {
  211. writeTemplateName(Name);
  212. }
  213. /// Emit a template argument.
  214. void AddTemplateArgument(const TemplateArgument &Arg) {
  215. writeTemplateArgument(Arg);
  216. }
  217. /// Emit a template parameter list.
  218. void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
  219. /// Emit a template argument list.
  220. void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
  221. /// Emit a UnresolvedSet structure.
  222. void AddUnresolvedSet(const ASTUnresolvedSet &Set);
  223. /// Emit a CXXCtorInitializer array.
  224. void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer *> CtorInits);
  225. void AddCXXDefinitionData(const CXXRecordDecl *D);
  226. /// Emit information about the initializer of a VarDecl.
  227. void AddVarDeclInit(const VarDecl *VD);
  228. /// Write an OMPTraitInfo object.
  229. void writeOMPTraitInfo(const OMPTraitInfo *TI);
  230. void writeOMPClause(OMPClause *C);
  231. /// Writes data related to the OpenMP directives.
  232. void writeOMPChildren(OMPChildren *Data);
  233. /// Emit a string.
  234. void AddString(StringRef Str) {
  235. return Writer->AddString(Str, *Record);
  236. }
  237. /// Emit a path.
  238. void AddPath(StringRef Path) {
  239. return Writer->AddPath(Path, *Record);
  240. }
  241. /// Emit a version tuple.
  242. void AddVersionTuple(const VersionTuple &Version) {
  243. return Writer->AddVersionTuple(Version, *Record);
  244. }
  245. // Emit an attribute.
  246. void AddAttr(const Attr *A);
  247. /// Emit a list of attributes.
  248. void AddAttributes(ArrayRef<const Attr*> Attrs);
  249. };
  250. } // end namespace clang
  251. #endif
  252. #ifdef __GNUC__
  253. #pragma GCC diagnostic pop
  254. #endif