1 //===--- ASTWriter.h - AST File Writer --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTWriter class, which writes an AST file
11 // containing a serialized representation of a translation unit.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_FRONTEND_AST_WRITER_H
15 #define LLVM_CLANG_FRONTEND_AST_WRITER_H
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/TemplateBase.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/Serialization/ASTBitCodes.h"
22 #include "clang/Serialization/ASTDeserializationListener.h"
23 #include "clang/Sema/SemaConsumer.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/DenseSet.h"
28 #include "llvm/ADT/SetVector.h"
29 #include "llvm/Bitcode/BitstreamWriter.h"
37 class BitstreamWriter;
43 class NestedNameSpecifier;
44 class CXXBaseSpecifier;
45 class CXXCtorInitializer;
48 class IdentifierResolver;
49 class MacroDefinition;
50 class MemorizeStatCalls;
51 class OpaqueValueExpr;
55 class PreprocessedEntity;
56 class PreprocessingRecord;
64 namespace SrcMgr { class SLocEntry; }
66 /// \brief Writes an AST file containing the contents of a translation unit.
68 /// The ASTWriter class produces a bitstream containing the serialized
69 /// representation of a given abstract syntax tree and its supporting
70 /// data structures. This bitstream can be de-serialized via an
71 /// instance of the ASTReader class.
72 class ASTWriter : public ASTDeserializationListener,
73 public ASTMutationListener {
75 typedef SmallVector<uint64_t, 64> RecordData;
76 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
78 friend class ASTDeclWriter;
79 friend class ASTStmtWriter;
81 /// \brief Map that provides the ID numbers of each type within the
82 /// output stream, plus those deserialized from a chained PCH.
84 /// The ID numbers of types are consecutive (in order of discovery)
85 /// and start at 1. 0 is reserved for NULL. When types are actually
86 /// stored in the stream, the ID number is shifted by 2 bits to
87 /// allow for the const/volatile qualifiers.
89 /// Keys in the map never have const/volatile qualifiers.
90 typedef llvm::DenseMap<QualType, serialization::TypeIdx,
91 serialization::UnsafeQualTypeDenseMapInfo>
94 /// \brief The bitstream writer used to emit this precompiled header.
95 llvm::BitstreamWriter &Stream;
97 /// \brief The ASTContext we're writing.
100 /// \brief The preprocessor we're writing.
103 /// \brief The reader of existing AST files, if we're chaining.
106 /// \brief The module we're currently writing, if any.
107 Module *WritingModule;
109 /// \brief Indicates when the AST writing is actively performing
110 /// serialization, rather than just queueing updates.
113 /// \brief Indicates that we are done serializing the collection of decls
114 /// and types to emit.
115 bool DoneWritingDeclsAndTypes;
117 /// \brief Indicates that the AST contained compiler errors.
118 bool ASTHasCompilerErrors;
120 /// \brief Stores a declaration or a type to be written to the AST file.
123 DeclOrType(Decl *D) : Stored(D), IsType(false) { }
124 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
126 bool isType() const { return IsType; }
127 bool isDecl() const { return !IsType; }
129 QualType getType() const {
130 assert(isType() && "Not a type!");
131 return QualType::getFromOpaquePtr(Stored);
134 Decl *getDecl() const {
135 assert(isDecl() && "Not a decl!");
136 return static_cast<Decl *>(Stored);
144 /// \brief The declarations and types to emit.
145 std::queue<DeclOrType> DeclTypesToEmit;
147 /// \brief The first ID number we can use for our own declarations.
148 serialization::DeclID FirstDeclID;
150 /// \brief The decl ID that will be assigned to the next new decl.
151 serialization::DeclID NextDeclID;
153 /// \brief Map that provides the ID numbers of each declaration within
154 /// the output stream, as well as those deserialized from a chained PCH.
156 /// The ID numbers of declarations are consecutive (in order of
157 /// discovery) and start at 2. 1 is reserved for the translation
158 /// unit, while 0 is reserved for NULL.
159 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
161 /// \brief Offset of each declaration in the bitstream, indexed by
162 /// the declaration's ID.
163 std::vector<serialization::DeclOffset> DeclOffsets;
165 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
166 typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
168 struct DeclIDInFileInfo {
169 LocDeclIDsTy DeclIDs;
170 /// \brief Set when the DeclIDs vectors from all files are joined, this
171 /// indicates the index that this particular vector has in the global one.
172 unsigned FirstDeclIndex;
174 typedef llvm::DenseMap<const SrcMgr::SLocEntry *,
175 DeclIDInFileInfo *> FileDeclIDsTy;
177 /// \brief Map from file SLocEntries to info about the file-level declarations
178 /// that it contains.
179 FileDeclIDsTy FileDeclIDs;
181 void associateDeclWithFile(const Decl *D, serialization::DeclID);
183 /// \brief The first ID number we can use for our own types.
184 serialization::TypeID FirstTypeID;
186 /// \brief The type ID that will be assigned to the next new type.
187 serialization::TypeID NextTypeID;
189 /// \brief Map that provides the ID numbers of each type within the
190 /// output stream, plus those deserialized from a chained PCH.
192 /// The ID numbers of types are consecutive (in order of discovery)
193 /// and start at 1. 0 is reserved for NULL. When types are actually
194 /// stored in the stream, the ID number is shifted by 2 bits to
195 /// allow for the const/volatile qualifiers.
197 /// Keys in the map never have const/volatile qualifiers.
200 /// \brief Offset of each type in the bitstream, indexed by
202 std::vector<uint32_t> TypeOffsets;
204 /// \brief The first ID number we can use for our own identifiers.
205 serialization::IdentID FirstIdentID;
207 /// \brief The identifier ID that will be assigned to the next new identifier.
208 serialization::IdentID NextIdentID;
210 /// \brief Map that provides the ID numbers of each identifier in
211 /// the output stream.
213 /// The ID numbers for identifiers are consecutive (in order of
214 /// discovery), starting at 1. An ID of zero refers to a NULL
216 llvm::DenseMap<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
218 /// @name FlushStmt Caches
221 /// \brief Set of parent Stmts for the currently serializing sub stmt.
222 llvm::DenseSet<Stmt *> ParentStmts;
224 /// \brief Offsets of sub stmts already serialized. The offset points
225 /// just after the stmt record.
226 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
230 /// \brief Offsets of each of the identifier IDs into the identifier
232 std::vector<uint32_t> IdentifierOffsets;
234 /// \brief The first ID number we can use for our own submodules.
235 serialization::SubmoduleID FirstSubmoduleID;
237 /// \brief The submodule ID that will be assigned to the next new submodule.
238 serialization::SubmoduleID NextSubmoduleID;
240 /// \brief The first ID number we can use for our own selectors.
241 serialization::SelectorID FirstSelectorID;
243 /// \brief The selector ID that will be assigned to the next new selector.
244 serialization::SelectorID NextSelectorID;
246 /// \brief Map that provides the ID numbers of each Selector.
247 llvm::DenseMap<Selector, serialization::SelectorID> SelectorIDs;
249 /// \brief Offset of each selector within the method pool/selector
250 /// table, indexed by the Selector ID (-1).
251 std::vector<uint32_t> SelectorOffsets;
253 /// \brief Offsets of each of the macro identifiers into the
256 /// For each identifier that is associated with a macro, this map
257 /// provides the offset into the bitstream where that macro is
259 llvm::DenseMap<const IdentifierInfo *, uint64_t> MacroOffsets;
261 /// \brief The set of identifiers that had macro definitions at some point.
262 std::vector<const IdentifierInfo *> DeserializedMacroNames;
264 /// \brief Mapping from macro definitions (as they occur in the preprocessing
265 /// record) to the macro IDs.
266 llvm::DenseMap<const MacroDefinition *, serialization::PreprocessedEntityID>
269 typedef SmallVector<uint64_t, 2> UpdateRecord;
270 typedef llvm::DenseMap<const Decl *, UpdateRecord> DeclUpdateMap;
271 /// \brief Mapping from declarations that came from a chained PCH to the
272 /// record containing modifications to them.
273 DeclUpdateMap DeclUpdates;
275 typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
276 /// \brief Map of first declarations from a chained PCH that point to the
277 /// most recent declarations in another PCH.
278 FirstLatestDeclMap FirstLatestDecls;
280 /// \brief Declarations encountered that might be external
283 /// We keep track of external definitions (as well as tentative
284 /// definitions) as we are emitting declarations to the AST
285 /// file. The AST file contains a separate record for these external
286 /// definitions, which are provided to the AST consumer by the AST
287 /// reader. This is behavior is required to properly cope with,
288 /// e.g., tentative variable definitions that occur within
289 /// headers. The declarations themselves are stored as declaration
290 /// IDs, since they will be written out to an EXTERNAL_DEFINITIONS
292 SmallVector<uint64_t, 16> ExternalDefinitions;
294 /// \brief DeclContexts that have received extensions since their serialized
297 /// For namespaces, when we're chaining and encountering a namespace, we check
298 /// if its primary namespace comes from the chain. If it does, we add the
299 /// primary to this set, so that we can write out lexical content updates for
301 llvm::SmallPtrSet<const DeclContext *, 16> UpdatedDeclContexts;
303 /// \brief Keeps track of visible decls that were added in DeclContexts
304 /// coming from another AST file.
305 SmallVector<const Decl *, 16> UpdatingVisibleDecls;
307 typedef llvm::SmallPtrSet<const Decl *, 16> DeclsToRewriteTy;
308 /// \brief Decls that will be replaced in the current dependent AST file.
309 DeclsToRewriteTy DeclsToRewrite;
311 /// \brief The set of Objective-C class that have categories we
312 /// should serialize.
313 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
315 struct ReplacedDeclInfo {
316 serialization::DeclID ID;
320 ReplacedDeclInfo() : ID(0), Offset(0), Loc(0) {}
321 ReplacedDeclInfo(serialization::DeclID ID, uint64_t Offset,
323 : ID(ID), Offset(Offset), Loc(Loc.getRawEncoding()) {}
326 /// \brief Decls that have been replaced in the current dependent AST file.
328 /// When a decl changes fundamentally after being deserialized (this shouldn't
329 /// happen, but the ObjC AST nodes are designed this way), it will be
330 /// serialized again. In this case, it is registered here, so that the reader
331 /// knows to read the updated version.
332 SmallVector<ReplacedDeclInfo, 16> ReplacedDecls;
334 /// \brief The set of declarations that may have redeclaration chains that
335 /// need to be serialized.
336 llvm::SetVector<Decl *, llvm::SmallVector<Decl *, 4>,
337 llvm::SmallPtrSet<Decl *, 4> > Redeclarations;
339 /// \brief Statements that we've encountered while serializing a
340 /// declaration or type.
341 SmallVector<Stmt *, 16> StmtsToEmit;
343 /// \brief Statements collection to use for ASTWriter::AddStmt().
344 /// It will point to StmtsToEmit unless it is overriden.
345 SmallVector<Stmt *, 16> *CollectedStmts;
347 /// \brief Mapping from SwitchCase statements to IDs.
348 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
350 /// \brief The number of statements written to the AST file.
351 unsigned NumStatements;
353 /// \brief The number of macros written to the AST file.
356 /// \brief The number of lexical declcontexts written to the AST
358 unsigned NumLexicalDeclContexts;
360 /// \brief The number of visible declcontexts written to the AST
362 unsigned NumVisibleDeclContexts;
364 /// \brief The offset of each CXXBaseSpecifier set within the AST.
365 SmallVector<uint32_t, 4> CXXBaseSpecifiersOffsets;
367 /// \brief The first ID number we can use for our own base specifiers.
368 serialization::CXXBaseSpecifiersID FirstCXXBaseSpecifiersID;
370 /// \brief The base specifiers ID that will be assigned to the next new
371 /// set of C++ base specifiers.
372 serialization::CXXBaseSpecifiersID NextCXXBaseSpecifiersID;
374 /// \brief A set of C++ base specifiers that is queued to be written into the
376 struct QueuedCXXBaseSpecifiers {
377 QueuedCXXBaseSpecifiers() : ID(), Bases(), BasesEnd() { }
379 QueuedCXXBaseSpecifiers(serialization::CXXBaseSpecifiersID ID,
380 CXXBaseSpecifier const *Bases,
381 CXXBaseSpecifier const *BasesEnd)
382 : ID(ID), Bases(Bases), BasesEnd(BasesEnd) { }
384 serialization::CXXBaseSpecifiersID ID;
385 CXXBaseSpecifier const * Bases;
386 CXXBaseSpecifier const * BasesEnd;
389 /// \brief Queue of C++ base specifiers to be written to the AST file,
390 /// in the order they should be written.
391 SmallVector<QueuedCXXBaseSpecifiers, 2> CXXBaseSpecifiersToWrite;
393 /// \brief A mapping from each known submodule to its ID number, which will
394 /// be a positive integer.
395 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
397 /// \brief Retrieve or create a submodule ID for this module.
398 unsigned getSubmoduleID(Module *Mod);
400 /// \brief Write the given subexpression to the bitstream.
401 void WriteSubStmt(Stmt *S,
402 llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries,
403 llvm::DenseSet<Stmt *> &ParentStmts);
405 void WriteBlockInfoBlock();
406 void WriteMetadata(ASTContext &Context, StringRef isysroot,
407 const std::string &OutputFile);
408 void WriteLanguageOptions(const LangOptions &LangOpts);
409 void WriteStatCache(MemorizeStatCalls &StatCalls);
410 void WriteSourceManagerBlock(SourceManager &SourceMgr,
411 const Preprocessor &PP,
413 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
414 void WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot);
415 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
416 void WriteSubmodules(Module *WritingModule);
418 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag);
419 void WriteCXXBaseSpecifiersOffsets();
420 void WriteType(QualType T);
421 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
422 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
423 void WriteTypeDeclOffsets();
424 void WriteFileDeclIDsMap();
425 void WriteComments();
426 void WriteSelectors(Sema &SemaRef);
427 void WriteReferencedSelectorsPool(Sema &SemaRef);
428 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
430 void WriteAttributes(ArrayRef<const Attr*> Attrs, RecordDataImpl &Record);
431 void ResolveDeclUpdatesBlocks();
432 void WriteDeclUpdatesBlocks();
433 void WriteDeclReplacementsBlock();
434 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
435 void WriteFPPragmaOptions(const FPOptions &Opts);
436 void WriteOpenCLExtensions(Sema &SemaRef);
437 void WriteObjCCategories();
438 void WriteRedeclarations();
439 void WriteMergedDecls();
441 unsigned DeclParmVarAbbrev;
442 unsigned DeclContextLexicalAbbrev;
443 unsigned DeclContextVisibleLookupAbbrev;
444 unsigned UpdateVisibleAbbrev;
445 unsigned DeclRefExprAbbrev;
446 unsigned CharacterLiteralAbbrev;
447 unsigned DeclRecordAbbrev;
448 unsigned IntegerLiteralAbbrev;
449 unsigned DeclTypedefAbbrev;
450 unsigned DeclVarAbbrev;
451 unsigned DeclFieldAbbrev;
452 unsigned DeclEnumAbbrev;
453 unsigned DeclObjCIvarAbbrev;
455 void WriteDeclsBlockAbbrevs();
456 void WriteDecl(ASTContext &Context, Decl *D);
458 void WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
459 StringRef isysroot, const std::string &OutputFile,
460 Module *WritingModule);
463 /// \brief Create a new precompiled header writer that outputs to
464 /// the given bitstream.
465 ASTWriter(llvm::BitstreamWriter &Stream);
468 /// \brief Write a precompiled header for the given semantic analysis.
470 /// \param SemaRef a reference to the semantic analysis object that processed
471 /// the AST to be written into the precompiled header.
473 /// \param StatCalls the object that cached all of the stat() calls made while
474 /// searching for source files and headers.
476 /// \param WritingModule The module that we are writing. If null, we are
477 /// writing a precompiled header.
479 /// \param isysroot if non-empty, write a relocatable file whose headers
480 /// are relative to the given system root.
481 void WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
482 const std::string &OutputFile,
483 Module *WritingModule, StringRef isysroot,
484 bool hasErrors = false);
486 /// \brief Emit a source location.
487 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
489 /// \brief Emit a source range.
490 void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
492 /// \brief Emit an integral value.
493 void AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record);
495 /// \brief Emit a signed integral value.
496 void AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record);
498 /// \brief Emit a floating-point value.
499 void AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record);
501 /// \brief Emit a reference to an identifier.
502 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
504 /// \brief Emit a Selector (which is a smart pointer reference).
505 void AddSelectorRef(Selector, RecordDataImpl &Record);
507 /// \brief Emit a CXXTemporary.
508 void AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record);
510 /// \brief Emit a set of C++ base specifiers to the record.
511 void AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
512 CXXBaseSpecifier const *BasesEnd,
513 RecordDataImpl &Record);
515 /// \brief Get the unique number used to refer to the given selector.
516 serialization::SelectorID getSelectorRef(Selector Sel);
518 /// \brief Get the unique number used to refer to the given identifier.
519 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
521 /// \brief Retrieve the offset of the macro definition for the given
524 /// The identifier must refer to a macro.
525 uint64_t getMacroOffset(const IdentifierInfo *II) {
526 assert(MacroOffsets.find(II) != MacroOffsets.end() &&
527 "Identifier does not name a macro");
528 return MacroOffsets[II];
531 /// \brief Emit a reference to a type.
532 void AddTypeRef(QualType T, RecordDataImpl &Record);
534 /// \brief Force a type to be emitted and get its ID.
535 serialization::TypeID GetOrCreateTypeID(QualType T);
537 /// \brief Determine the type ID of an already-emitted type.
538 serialization::TypeID getTypeID(QualType T) const;
540 /// \brief Force a type to be emitted and get its index.
541 serialization::TypeIdx GetOrCreateTypeIdx( QualType T);
543 /// \brief Determine the type index of an already-emitted type.
544 serialization::TypeIdx getTypeIdx(QualType T) const;
546 /// \brief Emits a reference to a declarator info.
547 void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record);
549 /// \brief Emits a type with source-location information.
550 void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record);
552 /// \brief Emits a template argument location info.
553 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
554 const TemplateArgumentLocInfo &Arg,
555 RecordDataImpl &Record);
557 /// \brief Emits a template argument location.
558 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
559 RecordDataImpl &Record);
561 /// \brief Emit a reference to a declaration.
562 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
565 /// \brief Force a declaration to be emitted and get its ID.
566 serialization::DeclID GetDeclRef(const Decl *D);
568 /// \brief Determine the declaration ID of an already-emitted
570 serialization::DeclID getDeclID(const Decl *D);
572 /// \brief Emit a declaration name.
573 void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record);
574 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
575 DeclarationName Name, RecordDataImpl &Record);
576 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
577 RecordDataImpl &Record);
579 void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record);
581 /// \brief Emit a nested name specifier.
582 void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record);
584 /// \brief Emit a nested name specifier with source-location information.
585 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
586 RecordDataImpl &Record);
588 /// \brief Emit a template name.
589 void AddTemplateName(TemplateName Name, RecordDataImpl &Record);
591 /// \brief Emit a template argument.
592 void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record);
594 /// \brief Emit a template parameter list.
595 void AddTemplateParameterList(const TemplateParameterList *TemplateParams,
596 RecordDataImpl &Record);
598 /// \brief Emit a template argument list.
599 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
600 RecordDataImpl &Record);
602 /// \brief Emit a UnresolvedSet structure.
603 void AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record);
605 /// \brief Emit a C++ base specifier.
606 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
607 RecordDataImpl &Record);
609 /// \brief Emit a CXXCtorInitializer array.
610 void AddCXXCtorInitializers(
611 const CXXCtorInitializer * const *CtorInitializers,
612 unsigned NumCtorInitializers,
613 RecordDataImpl &Record);
615 void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record);
617 /// \brief Add a string to the given record.
618 void AddString(StringRef Str, RecordDataImpl &Record);
620 /// \brief Add a version tuple to the given record
621 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
623 /// \brief Mark a declaration context as needing an update.
624 void AddUpdatedDeclContext(const DeclContext *DC) {
625 UpdatedDeclContexts.insert(DC);
628 void RewriteDecl(const Decl *D) {
629 DeclsToRewrite.insert(D);
632 bool isRewritten(const Decl *D) const {
633 return DeclsToRewrite.count(D);
636 /// \brief Infer the submodule ID that contains an entity at the given
638 serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
640 /// \brief Note that the identifier II occurs at the given offset
641 /// within the identifier table.
642 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
644 /// \brief Note that the selector Sel occurs at the given offset
645 /// within the method pool/selector table.
646 void SetSelectorOffset(Selector Sel, uint32_t Offset);
648 /// \brief Add the given statement or expression to the queue of
649 /// statements to emit.
651 /// This routine should be used when emitting types and declarations
652 /// that have expressions as part of their formulation. Once the
653 /// type or declaration has been written, call FlushStmts() to write
654 /// the corresponding statements just after the type or
656 void AddStmt(Stmt *S) {
657 CollectedStmts->push_back(S);
660 /// \brief Flush all of the statements and expressions that have
661 /// been added to the queue via AddStmt().
664 /// \brief Flush all of the C++ base specifier sets that have been added
665 /// via \c AddCXXBaseSpecifiersRef().
666 void FlushCXXBaseSpecifiers();
668 /// \brief Record an ID for the given switch-case statement.
669 unsigned RecordSwitchCaseID(SwitchCase *S);
671 /// \brief Retrieve the ID for the given switch-case statement.
672 unsigned getSwitchCaseID(SwitchCase *S);
674 void ClearSwitchCaseIDs();
676 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
677 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
678 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
679 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
680 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
681 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
682 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
683 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
684 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
685 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
687 bool hasChain() const { return Chain; }
689 // ASTDeserializationListener implementation
690 void ReaderInitialized(ASTReader *Reader);
691 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II);
692 void TypeRead(serialization::TypeIdx Idx, QualType T);
693 void SelectorRead(serialization::SelectorID ID, Selector Sel);
694 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
695 MacroDefinition *MD);
696 void MacroVisible(IdentifierInfo *II);
697 void ModuleRead(serialization::SubmoduleID ID, Module *Mod);
699 // ASTMutationListener implementation.
700 virtual void CompletedTagDefinition(const TagDecl *D);
701 virtual void AddedVisibleDecl(const DeclContext *DC, const Decl *D);
702 virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D);
703 virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
704 const ClassTemplateSpecializationDecl *D);
705 virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
706 const FunctionDecl *D);
707 virtual void CompletedImplicitDefinition(const FunctionDecl *D);
708 virtual void StaticDataMemberInstantiated(const VarDecl *D);
709 virtual void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
710 const ObjCInterfaceDecl *IFD);
711 virtual void AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
712 const ObjCPropertyDecl *OrigProp,
713 const ObjCCategoryDecl *ClassExt);
716 /// \brief AST and semantic-analysis consumer that generates a
717 /// precompiled header from the parsed source code.
718 class PCHGenerator : public SemaConsumer {
719 const Preprocessor &PP;
720 std::string OutputFile;
721 clang::Module *Module;
722 std::string isysroot;
725 MemorizeStatCalls *StatCalls; // owned by the FileManager
726 llvm::SmallVector<char, 128> Buffer;
727 llvm::BitstreamWriter Stream;
731 ASTWriter &getWriter() { return Writer; }
732 const ASTWriter &getWriter() const { return Writer; }
735 PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
736 clang::Module *Module,
737 StringRef isysroot, raw_ostream *Out);
739 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
740 virtual void HandleTranslationUnit(ASTContext &Ctx);
741 virtual ASTMutationListener *GetASTMutationListener();
742 virtual ASTDeserializationListener *GetASTDeserializationListener();
745 } // end namespace clang