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/Lex/PPMutationListener.h"
22 #include "clang/Serialization/ASTBitCodes.h"
23 #include "clang/Serialization/ASTDeserializationListener.h"
24 #include "clang/Sema/SemaConsumer.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/DenseSet.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/SetVector.h"
31 #include "llvm/Bitcode/BitstreamWriter.h"
39 class BitstreamWriter;
45 class NestedNameSpecifier;
46 class CXXBaseSpecifier;
47 class CXXCtorInitializer;
51 class IdentifierResolver;
52 class MacroDefinition;
53 class OpaqueValueExpr;
58 class PreprocessedEntity;
59 class PreprocessingRecord;
67 namespace SrcMgr { class SLocEntry; }
69 /// \brief Writes an AST file containing the contents of a translation unit.
71 /// The ASTWriter class produces a bitstream containing the serialized
72 /// representation of a given abstract syntax tree and its supporting
73 /// data structures. This bitstream can be de-serialized via an
74 /// instance of the ASTReader class.
75 class ASTWriter : public ASTDeserializationListener,
76 public PPMutationListener,
77 public ASTMutationListener {
79 typedef SmallVector<uint64_t, 64> RecordData;
80 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
82 friend class ASTDeclWriter;
83 friend class ASTStmtWriter;
85 /// \brief Map that provides the ID numbers of each type within the
86 /// output stream, plus those deserialized from a chained PCH.
88 /// The ID numbers of types are consecutive (in order of discovery)
89 /// and start at 1. 0 is reserved for NULL. When types are actually
90 /// stored in the stream, the ID number is shifted by 2 bits to
91 /// allow for the const/volatile qualifiers.
93 /// Keys in the map never have const/volatile qualifiers.
94 typedef llvm::DenseMap<QualType, serialization::TypeIdx,
95 serialization::UnsafeQualTypeDenseMapInfo>
98 /// \brief The bitstream writer used to emit this precompiled header.
99 llvm::BitstreamWriter &Stream;
101 /// \brief The ASTContext we're writing.
104 /// \brief The preprocessor we're writing.
107 /// \brief The reader of existing AST files, if we're chaining.
110 /// \brief The module we're currently writing, if any.
111 Module *WritingModule;
113 /// \brief Indicates when the AST writing is actively performing
114 /// serialization, rather than just queueing updates.
117 /// \brief Indicates that we are done serializing the collection of decls
118 /// and types to emit.
119 bool DoneWritingDeclsAndTypes;
121 /// \brief Indicates that the AST contained compiler errors.
122 bool ASTHasCompilerErrors;
124 /// \brief Mapping from input file entries to the index into the
125 /// offset table where information about that input file is stored.
126 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
128 /// \brief Stores a declaration or a type to be written to the AST file.
131 DeclOrType(Decl *D) : Stored(D), IsType(false) { }
132 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
134 bool isType() const { return IsType; }
135 bool isDecl() const { return !IsType; }
137 QualType getType() const {
138 assert(isType() && "Not a type!");
139 return QualType::getFromOpaquePtr(Stored);
142 Decl *getDecl() const {
143 assert(isDecl() && "Not a decl!");
144 return static_cast<Decl *>(Stored);
152 /// \brief The declarations and types to emit.
153 std::queue<DeclOrType> DeclTypesToEmit;
155 /// \brief The first ID number we can use for our own declarations.
156 serialization::DeclID FirstDeclID;
158 /// \brief The decl ID that will be assigned to the next new decl.
159 serialization::DeclID NextDeclID;
161 /// \brief Map that provides the ID numbers of each declaration within
162 /// the output stream, as well as those deserialized from a chained PCH.
164 /// The ID numbers of declarations are consecutive (in order of
165 /// discovery) and start at 2. 1 is reserved for the translation
166 /// unit, while 0 is reserved for NULL.
167 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
169 /// \brief Offset of each declaration in the bitstream, indexed by
170 /// the declaration's ID.
171 std::vector<serialization::DeclOffset> DeclOffsets;
173 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
174 typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
176 struct DeclIDInFileInfo {
177 LocDeclIDsTy DeclIDs;
178 /// \brief Set when the DeclIDs vectors from all files are joined, this
179 /// indicates the index that this particular vector has in the global one.
180 unsigned FirstDeclIndex;
182 typedef llvm::DenseMap<FileID, DeclIDInFileInfo *> FileDeclIDsTy;
184 /// \brief Map from file SLocEntries to info about the file-level declarations
185 /// that it contains.
186 FileDeclIDsTy FileDeclIDs;
188 void associateDeclWithFile(const Decl *D, serialization::DeclID);
190 /// \brief The first ID number we can use for our own types.
191 serialization::TypeID FirstTypeID;
193 /// \brief The type ID that will be assigned to the next new type.
194 serialization::TypeID NextTypeID;
196 /// \brief Map that provides the ID numbers of each type within the
197 /// output stream, plus those deserialized from a chained PCH.
199 /// The ID numbers of types are consecutive (in order of discovery)
200 /// and start at 1. 0 is reserved for NULL. When types are actually
201 /// stored in the stream, the ID number is shifted by 2 bits to
202 /// allow for the const/volatile qualifiers.
204 /// Keys in the map never have const/volatile qualifiers.
207 /// \brief Offset of each type in the bitstream, indexed by
209 std::vector<uint32_t> TypeOffsets;
211 /// \brief The first ID number we can use for our own identifiers.
212 serialization::IdentID FirstIdentID;
214 /// \brief The identifier ID that will be assigned to the next new identifier.
215 serialization::IdentID NextIdentID;
217 /// \brief Map that provides the ID numbers of each identifier in
218 /// the output stream.
220 /// The ID numbers for identifiers are consecutive (in order of
221 /// discovery), starting at 1. An ID of zero refers to a NULL
223 llvm::DenseMap<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
225 /// \brief The first ID number we can use for our own macros.
226 serialization::MacroID FirstMacroID;
228 /// \brief The identifier ID that will be assigned to the next new identifier.
229 serialization::MacroID NextMacroID;
231 /// \brief Map that provides the ID numbers of each macro.
232 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
234 /// @name FlushStmt Caches
237 /// \brief Set of parent Stmts for the currently serializing sub stmt.
238 llvm::DenseSet<Stmt *> ParentStmts;
240 /// \brief Offsets of sub stmts already serialized. The offset points
241 /// just after the stmt record.
242 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
246 /// \brief Offsets of each of the identifier IDs into the identifier
248 std::vector<uint32_t> IdentifierOffsets;
250 /// \brief The first ID number we can use for our own submodules.
251 serialization::SubmoduleID FirstSubmoduleID;
253 /// \brief The submodule ID that will be assigned to the next new submodule.
254 serialization::SubmoduleID NextSubmoduleID;
256 /// \brief The first ID number we can use for our own selectors.
257 serialization::SelectorID FirstSelectorID;
259 /// \brief The selector ID that will be assigned to the next new selector.
260 serialization::SelectorID NextSelectorID;
262 /// \brief Map that provides the ID numbers of each Selector.
263 llvm::DenseMap<Selector, serialization::SelectorID> SelectorIDs;
265 /// \brief Offset of each selector within the method pool/selector
266 /// table, indexed by the Selector ID (-1).
267 std::vector<uint32_t> SelectorOffsets;
269 typedef llvm::MapVector<MacroInfo *, MacroUpdate> MacroUpdatesMap;
271 /// \brief Updates to macro definitions that were loaded from an AST file.
272 MacroUpdatesMap MacroUpdates;
274 /// \brief Mapping from macro definitions (as they occur in the preprocessing
275 /// record) to the macro IDs.
276 llvm::DenseMap<const MacroDefinition *, serialization::PreprocessedEntityID>
279 typedef SmallVector<uint64_t, 2> UpdateRecord;
280 typedef llvm::DenseMap<const Decl *, UpdateRecord> DeclUpdateMap;
281 /// \brief Mapping from declarations that came from a chained PCH to the
282 /// record containing modifications to them.
283 DeclUpdateMap DeclUpdates;
285 typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
286 /// \brief Map of first declarations from a chained PCH that point to the
287 /// most recent declarations in another PCH.
288 FirstLatestDeclMap FirstLatestDecls;
290 /// \brief Declarations encountered that might be external
293 /// We keep track of external definitions (as well as tentative
294 /// definitions) as we are emitting declarations to the AST
295 /// file. The AST file contains a separate record for these external
296 /// definitions, which are provided to the AST consumer by the AST
297 /// reader. This is behavior is required to properly cope with,
298 /// e.g., tentative variable definitions that occur within
299 /// headers. The declarations themselves are stored as declaration
300 /// IDs, since they will be written out to an EXTERNAL_DEFINITIONS
302 SmallVector<uint64_t, 16> ExternalDefinitions;
304 /// \brief DeclContexts that have received extensions since their serialized
307 /// For namespaces, when we're chaining and encountering a namespace, we check
308 /// if its primary namespace comes from the chain. If it does, we add the
309 /// primary to this set, so that we can write out lexical content updates for
311 llvm::SmallPtrSet<const DeclContext *, 16> UpdatedDeclContexts;
313 /// \brief Keeps track of visible decls that were added in DeclContexts
314 /// coming from another AST file.
315 SmallVector<const Decl *, 16> UpdatingVisibleDecls;
317 typedef llvm::SmallPtrSet<const Decl *, 16> DeclsToRewriteTy;
318 /// \brief Decls that will be replaced in the current dependent AST file.
319 DeclsToRewriteTy DeclsToRewrite;
321 /// \brief The set of Objective-C class that have categories we
322 /// should serialize.
323 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
325 struct ReplacedDeclInfo {
326 serialization::DeclID ID;
330 ReplacedDeclInfo() : ID(0), Offset(0), Loc(0) {}
331 ReplacedDeclInfo(serialization::DeclID ID, uint64_t Offset,
333 : ID(ID), Offset(Offset), Loc(Loc.getRawEncoding()) {}
336 /// \brief Decls that have been replaced in the current dependent AST file.
338 /// When a decl changes fundamentally after being deserialized (this shouldn't
339 /// happen, but the ObjC AST nodes are designed this way), it will be
340 /// serialized again. In this case, it is registered here, so that the reader
341 /// knows to read the updated version.
342 SmallVector<ReplacedDeclInfo, 16> ReplacedDecls;
344 /// \brief The set of declarations that may have redeclaration chains that
345 /// need to be serialized.
346 llvm::SetVector<Decl *, llvm::SmallVector<Decl *, 4>,
347 llvm::SmallPtrSet<Decl *, 4> > Redeclarations;
349 /// \brief Statements that we've encountered while serializing a
350 /// declaration or type.
351 SmallVector<Stmt *, 16> StmtsToEmit;
353 /// \brief Statements collection to use for ASTWriter::AddStmt().
354 /// It will point to StmtsToEmit unless it is overriden.
355 SmallVector<Stmt *, 16> *CollectedStmts;
357 /// \brief Mapping from SwitchCase statements to IDs.
358 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
360 /// \brief The number of statements written to the AST file.
361 unsigned NumStatements;
363 /// \brief The number of macros written to the AST file.
366 /// \brief The number of lexical declcontexts written to the AST
368 unsigned NumLexicalDeclContexts;
370 /// \brief The number of visible declcontexts written to the AST
372 unsigned NumVisibleDeclContexts;
374 /// \brief The offset of each CXXBaseSpecifier set within the AST.
375 SmallVector<uint32_t, 4> CXXBaseSpecifiersOffsets;
377 /// \brief The first ID number we can use for our own base specifiers.
378 serialization::CXXBaseSpecifiersID FirstCXXBaseSpecifiersID;
380 /// \brief The base specifiers ID that will be assigned to the next new
381 /// set of C++ base specifiers.
382 serialization::CXXBaseSpecifiersID NextCXXBaseSpecifiersID;
384 /// \brief A set of C++ base specifiers that is queued to be written into the
386 struct QueuedCXXBaseSpecifiers {
387 QueuedCXXBaseSpecifiers() : ID(), Bases(), BasesEnd() { }
389 QueuedCXXBaseSpecifiers(serialization::CXXBaseSpecifiersID ID,
390 CXXBaseSpecifier const *Bases,
391 CXXBaseSpecifier const *BasesEnd)
392 : ID(ID), Bases(Bases), BasesEnd(BasesEnd) { }
394 serialization::CXXBaseSpecifiersID ID;
395 CXXBaseSpecifier const * Bases;
396 CXXBaseSpecifier const * BasesEnd;
399 /// \brief Queue of C++ base specifiers to be written to the AST file,
400 /// in the order they should be written.
401 SmallVector<QueuedCXXBaseSpecifiers, 2> CXXBaseSpecifiersToWrite;
403 /// \brief A mapping from each known submodule to its ID number, which will
404 /// be a positive integer.
405 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
407 /// \brief Retrieve or create a submodule ID for this module.
408 unsigned getSubmoduleID(Module *Mod);
410 /// \brief Write the given subexpression to the bitstream.
411 void WriteSubStmt(Stmt *S,
412 llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries,
413 llvm::DenseSet<Stmt *> &ParentStmts);
415 void WriteBlockInfoBlock();
416 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
417 StringRef isysroot, const std::string &OutputFile);
418 void WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot);
419 void WriteSourceManagerBlock(SourceManager &SourceMgr,
420 const Preprocessor &PP,
422 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
423 void WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot);
424 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
425 void WriteSubmodules(Module *WritingModule);
427 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag);
428 void WriteCXXBaseSpecifiersOffsets();
429 void WriteType(QualType T);
430 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
431 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
432 void WriteTypeDeclOffsets();
433 void WriteFileDeclIDsMap();
434 void WriteComments();
435 void WriteSelectors(Sema &SemaRef);
436 void WriteReferencedSelectorsPool(Sema &SemaRef);
437 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
439 void WriteAttributes(ArrayRef<const Attr*> Attrs, RecordDataImpl &Record);
440 void WriteMacroUpdates();
441 void ResolveDeclUpdatesBlocks();
442 void WriteDeclUpdatesBlocks();
443 void WriteDeclReplacementsBlock();
444 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
445 void WriteFPPragmaOptions(const FPOptions &Opts);
446 void WriteOpenCLExtensions(Sema &SemaRef);
447 void WriteObjCCategories();
448 void WriteRedeclarations();
449 void WriteMergedDecls();
451 unsigned DeclParmVarAbbrev;
452 unsigned DeclContextLexicalAbbrev;
453 unsigned DeclContextVisibleLookupAbbrev;
454 unsigned UpdateVisibleAbbrev;
455 unsigned DeclRefExprAbbrev;
456 unsigned CharacterLiteralAbbrev;
457 unsigned DeclRecordAbbrev;
458 unsigned IntegerLiteralAbbrev;
459 unsigned DeclTypedefAbbrev;
460 unsigned DeclVarAbbrev;
461 unsigned DeclFieldAbbrev;
462 unsigned DeclEnumAbbrev;
463 unsigned DeclObjCIvarAbbrev;
465 void WriteDeclsBlockAbbrevs();
466 void WriteDecl(ASTContext &Context, Decl *D);
468 void WriteASTCore(Sema &SemaRef,
469 StringRef isysroot, const std::string &OutputFile,
470 Module *WritingModule);
473 /// \brief Create a new precompiled header writer that outputs to
474 /// the given bitstream.
475 ASTWriter(llvm::BitstreamWriter &Stream);
478 /// \brief Write a precompiled header for the given semantic analysis.
480 /// \param SemaRef a reference to the semantic analysis object that processed
481 /// the AST to be written into the precompiled header.
483 /// \param WritingModule The module that we are writing. If null, we are
484 /// writing a precompiled header.
486 /// \param isysroot if non-empty, write a relocatable file whose headers
487 /// are relative to the given system root.
488 void WriteAST(Sema &SemaRef,
489 const std::string &OutputFile,
490 Module *WritingModule, StringRef isysroot,
491 bool hasErrors = false);
493 /// \brief Emit a source location.
494 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
496 /// \brief Emit a source range.
497 void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
499 /// \brief Emit an integral value.
500 void AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record);
502 /// \brief Emit a signed integral value.
503 void AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record);
505 /// \brief Emit a floating-point value.
506 void AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record);
508 /// \brief Emit a reference to an identifier.
509 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
511 /// \brief Emit a reference to a macro.
512 void addMacroRef(MacroInfo *MI, RecordDataImpl &Record);
514 /// \brief Emit a Selector (which is a smart pointer reference).
515 void AddSelectorRef(Selector, RecordDataImpl &Record);
517 /// \brief Emit a CXXTemporary.
518 void AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record);
520 /// \brief Emit a set of C++ base specifiers to the record.
521 void AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
522 CXXBaseSpecifier const *BasesEnd,
523 RecordDataImpl &Record);
525 /// \brief Get the unique number used to refer to the given selector.
526 serialization::SelectorID getSelectorRef(Selector Sel);
528 /// \brief Get the unique number used to refer to the given identifier.
529 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
531 /// \brief Get the unique number used to refer to the given macro.
532 serialization::MacroID getMacroRef(MacroInfo *MI);
534 /// \brief Emit a reference to a type.
535 void AddTypeRef(QualType T, RecordDataImpl &Record);
537 /// \brief Force a type to be emitted and get its ID.
538 serialization::TypeID GetOrCreateTypeID(QualType T);
540 /// \brief Determine the type ID of an already-emitted type.
541 serialization::TypeID getTypeID(QualType T) const;
543 /// \brief Force a type to be emitted and get its index.
544 serialization::TypeIdx GetOrCreateTypeIdx( QualType T);
546 /// \brief Determine the type index of an already-emitted type.
547 serialization::TypeIdx getTypeIdx(QualType T) const;
549 /// \brief Emits a reference to a declarator info.
550 void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record);
552 /// \brief Emits a type with source-location information.
553 void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record);
555 /// \brief Emits a template argument location info.
556 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
557 const TemplateArgumentLocInfo &Arg,
558 RecordDataImpl &Record);
560 /// \brief Emits a template argument location.
561 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
562 RecordDataImpl &Record);
564 /// \brief Emit a reference to a declaration.
565 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
568 /// \brief Force a declaration to be emitted and get its ID.
569 serialization::DeclID GetDeclRef(const Decl *D);
571 /// \brief Determine the declaration ID of an already-emitted
573 serialization::DeclID getDeclID(const Decl *D);
575 /// \brief Emit a declaration name.
576 void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record);
577 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
578 DeclarationName Name, RecordDataImpl &Record);
579 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
580 RecordDataImpl &Record);
582 void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record);
584 /// \brief Emit a nested name specifier.
585 void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record);
587 /// \brief Emit a nested name specifier with source-location information.
588 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
589 RecordDataImpl &Record);
591 /// \brief Emit a template name.
592 void AddTemplateName(TemplateName Name, RecordDataImpl &Record);
594 /// \brief Emit a template argument.
595 void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record);
597 /// \brief Emit a template parameter list.
598 void AddTemplateParameterList(const TemplateParameterList *TemplateParams,
599 RecordDataImpl &Record);
601 /// \brief Emit a template argument list.
602 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
603 RecordDataImpl &Record);
605 /// \brief Emit a UnresolvedSet structure.
606 void AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record);
608 /// \brief Emit a C++ base specifier.
609 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
610 RecordDataImpl &Record);
612 /// \brief Emit a CXXCtorInitializer array.
613 void AddCXXCtorInitializers(
614 const CXXCtorInitializer * const *CtorInitializers,
615 unsigned NumCtorInitializers,
616 RecordDataImpl &Record);
618 void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record);
620 /// \brief Add a string to the given record.
621 void AddString(StringRef Str, RecordDataImpl &Record);
623 /// \brief Add a version tuple to the given record
624 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
626 /// \brief Mark a declaration context as needing an update.
627 void AddUpdatedDeclContext(const DeclContext *DC) {
628 UpdatedDeclContexts.insert(DC);
631 void RewriteDecl(const Decl *D) {
632 DeclsToRewrite.insert(D);
635 bool isRewritten(const Decl *D) const {
636 return DeclsToRewrite.count(D);
639 /// \brief Infer the submodule ID that contains an entity at the given
641 serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
643 /// \brief Note that the identifier II occurs at the given offset
644 /// within the identifier table.
645 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
647 /// \brief Note that the selector Sel occurs at the given offset
648 /// within the method pool/selector table.
649 void SetSelectorOffset(Selector Sel, uint32_t Offset);
651 /// \brief Add the given statement or expression to the queue of
652 /// statements to emit.
654 /// This routine should be used when emitting types and declarations
655 /// that have expressions as part of their formulation. Once the
656 /// type or declaration has been written, call FlushStmts() to write
657 /// the corresponding statements just after the type or
659 void AddStmt(Stmt *S) {
660 CollectedStmts->push_back(S);
663 /// \brief Flush all of the statements and expressions that have
664 /// been added to the queue via AddStmt().
667 /// \brief Flush all of the C++ base specifier sets that have been added
668 /// via \c AddCXXBaseSpecifiersRef().
669 void FlushCXXBaseSpecifiers();
671 /// \brief Record an ID for the given switch-case statement.
672 unsigned RecordSwitchCaseID(SwitchCase *S);
674 /// \brief Retrieve the ID for the given switch-case statement.
675 unsigned getSwitchCaseID(SwitchCase *S);
677 void ClearSwitchCaseIDs();
679 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
680 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
681 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
682 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
683 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
684 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
685 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
686 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
687 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
688 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
690 bool hasChain() const { return Chain; }
692 // ASTDeserializationListener implementation
693 void ReaderInitialized(ASTReader *Reader);
694 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II);
695 void MacroRead(serialization::MacroID ID, MacroInfo *MI);
696 void TypeRead(serialization::TypeIdx Idx, QualType T);
697 void SelectorRead(serialization::SelectorID ID, Selector Sel);
698 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
699 MacroDefinition *MD);
700 void ModuleRead(serialization::SubmoduleID ID, Module *Mod);
702 // PPMutationListener implementation.
703 virtual void UndefinedMacro(MacroInfo *MI);
705 // ASTMutationListener implementation.
706 virtual void CompletedTagDefinition(const TagDecl *D);
707 virtual void AddedVisibleDecl(const DeclContext *DC, const Decl *D);
708 virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D);
709 virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
710 const ClassTemplateSpecializationDecl *D);
711 virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
712 const FunctionDecl *D);
713 virtual void CompletedImplicitDefinition(const FunctionDecl *D);
714 virtual void StaticDataMemberInstantiated(const VarDecl *D);
715 virtual void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
716 const ObjCInterfaceDecl *IFD);
717 virtual void AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
718 const ObjCPropertyDecl *OrigProp,
719 const ObjCCategoryDecl *ClassExt);
722 /// \brief AST and semantic-analysis consumer that generates a
723 /// precompiled header from the parsed source code.
724 class PCHGenerator : public SemaConsumer {
725 const Preprocessor &PP;
726 std::string OutputFile;
727 clang::Module *Module;
728 std::string isysroot;
731 llvm::SmallVector<char, 128> Buffer;
732 llvm::BitstreamWriter Stream;
736 ASTWriter &getWriter() { return Writer; }
737 const ASTWriter &getWriter() const { return Writer; }
740 PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
741 clang::Module *Module,
742 StringRef isysroot, raw_ostream *Out);
744 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
745 virtual void HandleTranslationUnit(ASTContext &Ctx);
746 virtual PPMutationListener *GetPPMutationListener();
747 virtual ASTMutationListener *GetASTMutationListener();
748 virtual ASTDeserializationListener *GetASTDeserializationListener();
751 } // end namespace clang