ASTReader.h revision 8f1231b70c2b1f6fe0cee097b447487b26810301
1//===--- ASTReader.h - AST File Reader --------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the ASTReader class, which reads AST files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_FRONTEND_AST_READER_H
15#define LLVM_CLANG_FRONTEND_AST_READER_H
16
17#include "clang/Serialization/ASTBitCodes.h"
18#include "clang/Serialization/ContinuousRangeMap.h"
19#include "clang/Sema/ExternalSemaSource.h"
20#include "clang/AST/DeclarationName.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/TemplateBase.h"
23#include "clang/Lex/ExternalPreprocessorSource.h"
24#include "clang/Lex/HeaderSearch.h"
25#include "clang/Lex/PreprocessingRecord.h"
26#include "clang/Basic/Diagnostic.h"
27#include "clang/Basic/IdentifierTable.h"
28#include "clang/Basic/SourceManager.h"
29#include "llvm/ADT/APFloat.h"
30#include "llvm/ADT/APInt.h"
31#include "llvm/ADT/APSInt.h"
32#include "llvm/ADT/OwningPtr.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/StringRef.h"
35#include "llvm/Bitcode/BitstreamReader.h"
36#include "llvm/Support/DataTypes.h"
37#include <deque>
38#include <map>
39#include <string>
40#include <utility>
41#include <vector>
42
43namespace llvm {
44  class MemoryBuffer;
45}
46
47namespace clang {
48
49class AddrLabelExpr;
50class ASTConsumer;
51class ASTContext;
52class ASTIdentifierIterator;
53class ASTUnit; // FIXME: Layering violation and egregious hack.
54class Attr;
55class Decl;
56class DeclContext;
57class NestedNameSpecifier;
58class CXXBaseSpecifier;
59class CXXConstructorDecl;
60class CXXCtorInitializer;
61class GotoStmt;
62class MacroDefinition;
63class NamedDecl;
64class OpaqueValueExpr;
65class Preprocessor;
66class Sema;
67class SwitchCase;
68class ASTDeserializationListener;
69class ASTWriter;
70class ASTReader;
71class ASTDeclReader;
72class ASTStmtReader;
73class ASTIdentifierLookupTrait;
74class TypeLocReader;
75struct HeaderFileInfo;
76class VersionTuple;
77
78struct PCHPredefinesBlock {
79  /// \brief The file ID for this predefines buffer in a PCH file.
80  FileID BufferID;
81
82  /// \brief This predefines buffer in a PCH file.
83  StringRef Data;
84};
85typedef SmallVector<PCHPredefinesBlock, 2> PCHPredefinesBlocks;
86
87/// \brief Abstract interface for callback invocations by the ASTReader.
88///
89/// While reading an AST file, the ASTReader will call the methods of the
90/// listener to pass on specific information. Some of the listener methods can
91/// return true to indicate to the ASTReader that the information (and
92/// consequently the AST file) is invalid.
93class ASTReaderListener {
94public:
95  virtual ~ASTReaderListener();
96
97  /// \brief Receives the language options.
98  ///
99  /// \returns true to indicate the options are invalid or false otherwise.
100  virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
101    return false;
102  }
103
104  /// \brief Receives the target triple.
105  ///
106  /// \returns true to indicate the target triple is invalid or false otherwise.
107  virtual bool ReadTargetTriple(StringRef Triple) {
108    return false;
109  }
110
111  /// \brief Receives the contents of the predefines buffer.
112  ///
113  /// \param Buffers Information about the predefines buffers.
114  ///
115  /// \param OriginalFileName The original file name for the AST file, which
116  /// will appear as an entry in the predefines buffer.
117  ///
118  /// \param SuggestedPredefines If necessary, additional definitions are added
119  /// here.
120  ///
121  /// \returns true to indicate the predefines are invalid or false otherwise.
122  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
123                                    StringRef OriginalFileName,
124                                    std::string &SuggestedPredefines,
125                                    FileManager &FileMgr) {
126    return false;
127  }
128
129  /// \brief Receives a HeaderFileInfo entry.
130  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {}
131
132  /// \brief Receives __COUNTER__ value.
133  virtual void ReadCounter(unsigned Value) {}
134};
135
136/// \brief ASTReaderListener implementation to validate the information of
137/// the PCH file against an initialized Preprocessor.
138class PCHValidator : public ASTReaderListener {
139  Preprocessor &PP;
140  ASTReader &Reader;
141
142  unsigned NumHeaderInfos;
143
144public:
145  PCHValidator(Preprocessor &PP, ASTReader &Reader)
146    : PP(PP), Reader(Reader), NumHeaderInfos(0) {}
147
148  virtual bool ReadLanguageOptions(const LangOptions &LangOpts);
149  virtual bool ReadTargetTriple(StringRef Triple);
150  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
151                                    StringRef OriginalFileName,
152                                    std::string &SuggestedPredefines,
153                                    FileManager &FileMgr);
154  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
155  virtual void ReadCounter(unsigned Value);
156
157private:
158  void Error(const char *Msg);
159};
160
161/// \brief Reads an AST files chain containing the contents of a translation
162/// unit.
163///
164/// The ASTReader class reads bitstreams (produced by the ASTWriter
165/// class) containing the serialized representation of a given
166/// abstract syntax tree and its supporting data structures. An
167/// instance of the ASTReader can be attached to an ASTContext object,
168/// which will provide access to the contents of the AST files.
169///
170/// The AST reader provides lazy de-serialization of declarations, as
171/// required when traversing the AST. Only those AST nodes that are
172/// actually required will be de-serialized.
173class ASTReader
174  : public ExternalPreprocessorSource,
175    public ExternalPreprocessingRecordSource,
176    public ExternalHeaderFileInfoSource,
177    public ExternalSemaSource,
178    public IdentifierInfoLookup,
179    public ExternalIdentifierLookup,
180    public ExternalSLocEntrySource
181{
182public:
183  enum ASTReadResult { Success, Failure, IgnorePCH };
184  /// \brief Types of AST files.
185  enum ASTFileType {
186    Module,   ///< File is a module proper.
187    PCH,      ///< File is a PCH file treated as such.
188    Preamble, ///< File is a PCH file treated as the preamble.
189    MainFile  ///< File is a PCH file treated as the actual main file.
190  };
191  friend class PCHValidator;
192  friend class ASTDeclReader;
193  friend class ASTStmtReader;
194  friend class ASTIdentifierIterator;
195  friend class ASTIdentifierLookupTrait;
196  friend class TypeLocReader;
197  friend class ASTWriter;
198  friend class ASTUnit; // ASTUnit needs to remap source locations.
199private:
200  /// \brief The receiver of some callbacks invoked by ASTReader.
201  llvm::OwningPtr<ASTReaderListener> Listener;
202
203  /// \brief The receiver of deserialization events.
204  ASTDeserializationListener *DeserializationListener;
205
206  SourceManager &SourceMgr;
207  FileManager &FileMgr;
208  Diagnostic &Diags;
209
210  /// \brief The semantic analysis object that will be processing the
211  /// AST files and the translation unit that uses it.
212  Sema *SemaObj;
213
214  /// \brief The preprocessor that will be loading the source file.
215  Preprocessor *PP;
216
217  /// \brief The AST context into which we'll read the AST files.
218  ASTContext *Context;
219
220  /// \brief The AST consumer.
221  ASTConsumer *Consumer;
222
223  /// \brief AST buffers for chained PCHs created and stored in memory.
224  /// First (not depending on another) PCH in chain is in front.
225  std::vector<llvm::MemoryBuffer *> ASTBuffers;
226
227public:
228  /// \brief Information that is needed for every module.
229  struct PerFileData {
230    PerFileData(ASTFileType Ty);
231    ~PerFileData();
232
233    // === General information ===
234
235    /// \brief The type of this AST file.
236    ASTFileType Type;
237
238    /// \brief The file name of the AST file.
239    std::string FileName;
240
241    /// \brief The memory buffer that stores the data associated with
242    /// this AST file.
243    llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
244
245    /// \brief The size of this file, in bits.
246    uint64_t SizeInBits;
247
248    /// \brief The global bit offset (or base) of this module
249    uint64_t GlobalBitOffset;
250
251    /// \brief The bitstream reader from which we'll read the AST file.
252    llvm::BitstreamReader StreamFile;
253
254    /// \brief The main bitstream cursor for the main block.
255    llvm::BitstreamCursor Stream;
256
257    /// \brief The source location where this module was first imported.
258    SourceLocation ImportLoc;
259
260    /// \brief The first source location in this module.
261    SourceLocation FirstLoc;
262
263    // === Source Locations ===
264
265    /// \brief Cursor used to read source location entries.
266    llvm::BitstreamCursor SLocEntryCursor;
267
268    /// \brief The number of source location entries in this AST file.
269    unsigned LocalNumSLocEntries;
270
271    /// \brief The base ID in the source manager's view of this module.
272    int SLocEntryBaseID;
273
274    /// \brief The base offset in the source manager's view of this module.
275    unsigned SLocEntryBaseOffset;
276
277    /// \brief Offsets for all of the source location entries in the
278    /// AST file.
279    const uint32_t *SLocEntryOffsets;
280
281    /// \brief The number of source location file entries in this AST file.
282    unsigned LocalNumSLocFileEntries;
283
284    /// \brief Offsets for all of the source location file entries in the
285    /// AST file.
286    const uint32_t *SLocFileOffsets;
287
288    /// \brief Remapping table for source locations in this module.
289    ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
290
291    // === Identifiers ===
292
293    /// \brief The number of identifiers in this AST file.
294    unsigned LocalNumIdentifiers;
295
296    /// \brief Offsets into the identifier table data.
297    ///
298    /// This array is indexed by the identifier ID (-1), and provides
299    /// the offset into IdentifierTableData where the string data is
300    /// stored.
301    const uint32_t *IdentifierOffsets;
302
303    /// \brief Actual data for the on-disk hash table of identifiers.
304    ///
305    /// This pointer points into a memory buffer, where the on-disk hash
306    /// table for identifiers actually lives.
307    const char *IdentifierTableData;
308
309    /// \brief A pointer to an on-disk hash table of opaque type
310    /// IdentifierHashTable.
311    void *IdentifierLookupTable;
312
313    // === Macros ===
314
315    /// \brief The cursor to the start of the preprocessor block, which stores
316    /// all of the macro definitions.
317    llvm::BitstreamCursor MacroCursor;
318
319    /// \brief The offset of the start of the set of defined macros.
320    uint64_t MacroStartOffset;
321
322    // === Detailed PreprocessingRecord ===
323
324    /// \brief The cursor to the start of the (optional) detailed preprocessing
325    /// record block.
326    llvm::BitstreamCursor PreprocessorDetailCursor;
327
328    /// \brief The offset of the start of the preprocessor detail cursor.
329    uint64_t PreprocessorDetailStartOffset;
330
331    /// \brief The number of macro definitions in this file.
332    unsigned LocalNumMacroDefinitions;
333
334    /// \brief Offsets of all of the macro definitions in the preprocessing
335    /// record in the AST file.
336    const uint32_t *MacroDefinitionOffsets;
337
338    // === Header search information ===
339
340    /// \brief The number of local HeaderFileInfo structures.
341    unsigned LocalNumHeaderFileInfos;
342
343    /// \brief Actual data for the on-disk hash table of header file
344    /// information.
345    ///
346    /// This pointer points into a memory buffer, where the on-disk hash
347    /// table for header file information actually lives.
348    const char *HeaderFileInfoTableData;
349
350    /// \brief The on-disk hash table that contains information about each of
351    /// the header files.
352    void *HeaderFileInfoTable;
353
354    // === Selectors ===
355
356    /// \brief The number of selectors new to this file.
357    ///
358    /// This is the number of entries in SelectorOffsets.
359    unsigned LocalNumSelectors;
360
361    /// \brief Offsets into the selector lookup table's data array
362    /// where each selector resides.
363    const uint32_t *SelectorOffsets;
364
365    /// \brief A pointer to the character data that comprises the selector table
366    ///
367    /// The SelectorOffsets table refers into this memory.
368    const unsigned char *SelectorLookupTableData;
369
370    /// \brief A pointer to an on-disk hash table of opaque type
371    /// ASTSelectorLookupTable.
372    ///
373    /// This hash table provides the IDs of all selectors, and the associated
374    /// instance and factory methods.
375    void *SelectorLookupTable;
376
377    /// \brief Method selectors used in a @selector expression. Used for
378    /// implementation of -Wselector.
379    SmallVector<uint64_t, 64> ReferencedSelectorsData;
380
381    // === Declarations ===
382
383    /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
384    /// has read all the abbreviations at the start of the block and is ready to
385    /// jump around with these in context.
386    llvm::BitstreamCursor DeclsCursor;
387
388    /// \brief The number of declarations in this AST file.
389    unsigned LocalNumDecls;
390
391    /// \brief Offset of each declaration within the bitstream, indexed
392    /// by the declaration ID (-1).
393    const uint32_t *DeclOffsets;
394
395    /// \brief A snapshot of the pending instantiations in the chain.
396    ///
397    /// This record tracks the instantiations that Sema has to perform at the
398    /// end of the TU. It consists of a pair of values for every pending
399    /// instantiation where the first value is the ID of the decl and the second
400    /// is the instantiation location.
401    SmallVector<uint64_t, 64> PendingInstantiations;
402
403    /// \brief The number of C++ base specifier sets in this AST file.
404    unsigned LocalNumCXXBaseSpecifiers;
405
406    /// \brief Offset of each C++ base specifier set within the bitstream,
407    /// indexed by the C++ base specifier set ID (-1).
408    const uint32_t *CXXBaseSpecifiersOffsets;
409
410    // === Types ===
411
412    /// \brief The number of types in this AST file.
413    unsigned LocalNumTypes;
414
415    /// \brief Offset of each type within the bitstream, indexed by the
416    /// type ID, or the representation of a Type*.
417    const uint32_t *TypeOffsets;
418
419    // === Miscellaneous ===
420
421    /// \brief Diagnostic IDs and their mappings that the user changed.
422    SmallVector<uint64_t, 8> PragmaDiagMappings;
423
424    /// \brief The AST stat cache installed for this file, if any.
425    ///
426    /// The dynamic type of this stat cache is always ASTStatCache
427    void *StatCache;
428
429    /// \brief The number of preallocated preprocessing entities in the
430    /// preprocessing record.
431    unsigned NumPreallocatedPreprocessingEntities;
432
433    /// \brief The next module in source order.
434    PerFileData *NextInSource;
435
436    /// \brief All the modules that loaded this one. Can contain NULL for
437    /// directly loaded modules.
438    SmallVector<PerFileData *, 1> Loaders;
439  };
440private:
441
442  /// \brief All loaded modules, indexed by name.
443  llvm::StringMap<PerFileData*> Modules;
444
445  /// \brief The first module in source order.
446  PerFileData *FirstInSource;
447
448  /// \brief The chain of AST files. The first entry is the one named by the
449  /// user, the last one is the one that doesn't depend on anything further.
450  /// That is, the entry I was created with -include-pch I+1.
451  SmallVector<PerFileData*, 2> Chain;
452
453  /// \brief A map of global bit offsets to the module that stores entities
454  /// at those bit offsets.
455  ContinuousRangeMap<uint64_t, PerFileData*, 4> GlobalBitOffsetsMap;
456
457  /// \brief SLocEntries that we're going to preload.
458  SmallVector<int, 64> PreloadSLocEntries;
459
460  /// \brief A map of negated SLocEntryIDs to the modules containing them.
461  ContinuousRangeMap<unsigned, PerFileData*, 64> GlobalSLocEntryMap;
462
463  /// \brief Types that have already been loaded from the chain.
464  ///
465  /// When the pointer at index I is non-NULL, the type with
466  /// ID = (I + 1) << FastQual::Width has already been loaded
467  std::vector<QualType> TypesLoaded;
468
469  typedef ContinuousRangeMap<serialization::TypeID,
470      std::pair<PerFileData *, int32_t>, 4>
471    GlobalTypeMapType;
472
473  /// \brief Mapping from global type IDs to the module in which the
474  /// type resides along with the offset that should be added to the
475  /// global type ID to produce a local ID.
476  GlobalTypeMapType GlobalTypeMap;
477
478  /// \brief Map that provides the ID numbers of each type within the
479  /// output stream, plus those deserialized from a chained PCH.
480  ///
481  /// The ID numbers of types are consecutive (in order of discovery)
482  /// and start at 1. 0 is reserved for NULL. When types are actually
483  /// stored in the stream, the ID number is shifted by 2 bits to
484  /// allow for the const/volatile qualifiers.
485  ///
486  /// Keys in the map never have const/volatile qualifiers.
487  serialization::TypeIdxMap TypeIdxs;
488
489  /// \brief Declarations that have already been loaded from the chain.
490  ///
491  /// When the pointer at index I is non-NULL, the declaration with ID
492  /// = I + 1 has already been loaded.
493  std::vector<Decl *> DeclsLoaded;
494
495  typedef ContinuousRangeMap<serialization::DeclID,
496                             std::pair<PerFileData *, int32_t>, 4>
497    GlobalDeclMapType;
498
499  /// \brief Mapping from global declaration IDs to the module in which the
500  /// declaration resides along with the offset that should be added to the
501  /// global declaration ID to produce a local ID.
502  GlobalDeclMapType GlobalDeclMap;
503
504  typedef std::pair<PerFileData *, uint64_t> FileOffset;
505  typedef SmallVector<FileOffset, 2> FileOffsetsTy;
506  typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
507      DeclUpdateOffsetsMap;
508
509  /// \brief Declarations that have modifications residing in a later file
510  /// in the chain.
511  DeclUpdateOffsetsMap DeclUpdateOffsets;
512
513  typedef llvm::DenseMap<serialization::DeclID,
514                         std::pair<PerFileData *, uint64_t> >
515      DeclReplacementMap;
516  /// \brief Declarations that have been replaced in a later file in the chain.
517  DeclReplacementMap ReplacedDecls;
518
519  /// \brief Information about the contents of a DeclContext.
520  struct DeclContextInfo {
521    PerFileData *F;
522    void *NameLookupTableData; // a ASTDeclContextNameLookupTable.
523    const serialization::KindDeclIDPair *LexicalDecls;
524    unsigned NumLexicalDecls;
525  };
526  // In a full chain, there could be multiple updates to every decl context,
527  // so this is a vector. However, typically a chain is only two elements long,
528  // with only one file containing updates, so there will be only one update
529  // per decl context.
530  typedef SmallVector<DeclContextInfo, 1> DeclContextInfos;
531  typedef llvm::DenseMap<const DeclContext *, DeclContextInfos>
532      DeclContextOffsetsMap;
533  // Updates for visible decls can occur for other contexts than just the
534  // TU, and when we read those update records, the actual context will not
535  // be available yet (unless it's the TU), so have this pending map using the
536  // ID as a key. It will be realized when the context is actually loaded.
537  typedef SmallVector<void *, 1> DeclContextVisibleUpdates;
538  typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
539      DeclContextVisibleUpdatesPending;
540
541  /// \brief Offsets of the lexical and visible declarations for each
542  /// DeclContext.
543  DeclContextOffsetsMap DeclContextOffsets;
544
545  /// \brief Updates to the visible declarations of declaration contexts that
546  /// haven't been loaded yet.
547  DeclContextVisibleUpdatesPending PendingVisibleUpdates;
548
549  typedef SmallVector<CXXRecordDecl *, 4> ForwardRefs;
550  typedef llvm::DenseMap<const CXXRecordDecl *, ForwardRefs>
551      PendingForwardRefsMap;
552  /// \brief Forward references that have a definition but the definition decl
553  /// is still initializing. When the definition gets read it will update
554  /// the DefinitionData pointer of all pending references.
555  PendingForwardRefsMap PendingForwardRefs;
556
557  typedef llvm::DenseMap<serialization::DeclID, serialization::DeclID>
558      FirstLatestDeclIDMap;
559  /// \brief Map of first declarations from a chained PCH that point to the
560  /// most recent declarations in another AST file.
561  FirstLatestDeclIDMap FirstLatestDeclIDs;
562
563  /// \brief Read the records that describe the contents of declcontexts.
564  bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
565                              const std::pair<uint64_t, uint64_t> &Offsets,
566                              DeclContextInfo &Info);
567
568  /// \brief A vector containing identifiers that have already been
569  /// loaded.
570  ///
571  /// If the pointer at index I is non-NULL, then it refers to the
572  /// IdentifierInfo for the identifier with ID=I+1 that has already
573  /// been loaded.
574  std::vector<IdentifierInfo *> IdentifiersLoaded;
575
576  typedef ContinuousRangeMap<serialization::IdentID,
577                             std::pair<PerFileData *, int32_t>, 4>
578    GlobalIdentifierMapType;
579
580  /// \brief Mapping from global identifer IDs to the module in which the
581  /// identifier resides along with the offset that should be added to the
582  /// global identifier ID to produce a local ID.
583  GlobalIdentifierMapType GlobalIdentifierMap;
584
585  /// \brief A vector containing selectors that have already been loaded.
586  ///
587  /// This vector is indexed by the Selector ID (-1). NULL selector
588  /// entries indicate that the particular selector ID has not yet
589  /// been loaded.
590  SmallVector<Selector, 16> SelectorsLoaded;
591
592  typedef ContinuousRangeMap<serialization::SelectorID,
593                             std::pair<PerFileData *, int32_t>, 4>
594    GlobalSelectorMapType;
595
596  /// \brief Mapping from global selector IDs to the module in which the
597  /// selector resides along with the offset that should be added to the
598  /// global selector ID to produce a local ID.
599  GlobalSelectorMapType GlobalSelectorMap;
600
601  /// \brief The macro definitions we have already loaded.
602  SmallVector<MacroDefinition *, 16> MacroDefinitionsLoaded;
603
604  typedef ContinuousRangeMap<serialization::MacroID,
605                             std::pair<PerFileData *, int32_t>, 4>
606    GlobalMacroDefinitionMapType;
607
608  /// \brief Mapping from global macro definition IDs to the module in which the
609  /// selector resides along with the offset that should be added to the
610  /// global selector ID to produce a local ID.
611  GlobalMacroDefinitionMapType GlobalMacroDefinitionMap;
612
613  /// \brief Mapping from identifiers that represent macros whose definitions
614  /// have not yet been deserialized to the global offset where the macro
615  /// record resides.
616  llvm::DenseMap<IdentifierInfo *, uint64_t> UnreadMacroRecordOffsets;
617
618  typedef ContinuousRangeMap<unsigned, std::pair<PerFileData *, int>, 4>
619    GlobalPreprocessedEntityMapType;
620
621  /// \brief Mapping from global preprocessing entity IDs to the module in
622  /// which the preprocessed entity resides along with the offset that should be
623  /// added to the global preprocessing entitiy ID to produce a local ID.
624  GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
625
626  typedef ContinuousRangeMap<serialization::CXXBaseSpecifiersID,
627                             std::pair<PerFileData *, int32_t>, 4>
628    GlobalCXXBaseSpecifiersMapType;
629
630  /// \brief Mapping from global CXX base specifier IDs to the module in which the
631  /// CXX base specifier resides along with the offset that should be added to the
632  /// global CXX base specifer ID to produce a local ID.
633  GlobalCXXBaseSpecifiersMapType GlobalCXXBaseSpecifiersMap;
634
635  /// \name CodeGen-relevant special data
636  /// \brief Fields containing data that is relevant to CodeGen.
637  //@{
638
639  /// \brief The IDs of all declarations that fulfill the criteria of
640  /// "interesting" decls.
641  ///
642  /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
643  /// chain. The referenced declarations are deserialized and passed to the
644  /// consumer eagerly.
645  SmallVector<uint64_t, 16> ExternalDefinitions;
646
647  /// \brief The IDs of all tentative definitions stored in the the chain.
648  ///
649  /// Sema keeps track of all tentative definitions in a TU because it has to
650  /// complete them and pass them on to CodeGen. Thus, tentative definitions in
651  /// the PCH chain must be eagerly deserialized.
652  SmallVector<uint64_t, 16> TentativeDefinitions;
653
654  /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
655  /// used.
656  ///
657  /// CodeGen has to emit VTables for these records, so they have to be eagerly
658  /// deserialized.
659  SmallVector<uint64_t, 64> VTableUses;
660
661  //@}
662
663  /// \name Diagnostic-relevant special data
664  /// \brief Fields containing data that is used for generating diagnostics
665  //@{
666
667  /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
668  /// generating warnings.
669  SmallVector<uint64_t, 16> UnusedFileScopedDecls;
670
671  /// \brief A list of all the delegating constructors we've seen, to diagnose
672  /// cycles.
673  SmallVector<uint64_t, 4> DelegatingCtorDecls;
674
675  /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
676  /// generating warnings.
677  SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
678
679  /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
680  ///
681  /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
682  SmallVector<uint64_t, 4> ExtVectorDecls;
683
684  //@}
685
686  /// \name Sema-relevant special data
687  /// \brief Fields containing data that is used for semantic analysis
688  //@{
689
690  /// \brief The IDs of all locally scoped external decls in the chain.
691  ///
692  /// Sema tracks these to validate that the types are consistent across all
693  /// local external declarations.
694  SmallVector<uint64_t, 16> LocallyScopedExternalDecls;
695
696  /// \brief The IDs of all dynamic class declarations in the chain.
697  ///
698  /// Sema tracks these because it checks for the key functions being defined
699  /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
700  SmallVector<uint64_t, 16> DynamicClasses;
701
702  /// \brief The IDs of the declarations Sema stores directly.
703  ///
704  /// Sema tracks a few important decls, such as namespace std, directly.
705  SmallVector<uint64_t, 4> SemaDeclRefs;
706
707  /// \brief The IDs of the types ASTContext stores directly.
708  ///
709  /// The AST context tracks a few important types, such as va_list, directly.
710  SmallVector<uint64_t, 16> SpecialTypes;
711
712  /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
713  ///
714  /// The AST context tracks a few important decls, currently cudaConfigureCall,
715  /// directly.
716  SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
717
718  /// \brief The floating point pragma option settings.
719  SmallVector<uint64_t, 1> FPPragmaOptions;
720
721  /// \brief The OpenCL extension settings.
722  SmallVector<uint64_t, 1> OpenCLExtensions;
723
724  /// \brief A list of the namespaces we've seen.
725  SmallVector<uint64_t, 4> KnownNamespaces;
726
727  //@}
728
729  /// \brief The original file name that was used to build the primary AST file,
730  /// which may have been modified for relocatable-pch support.
731  std::string OriginalFileName;
732
733  /// \brief The actual original file name that was used to build the primary
734  /// AST file.
735  std::string ActualOriginalFileName;
736
737  /// \brief The file ID for the original file that was used to build the
738  /// primary AST file.
739  FileID OriginalFileID;
740
741  /// \brief The directory that the PCH was originally created in. Used to
742  /// allow resolving headers even after headers+PCH was moved to a new path.
743  std::string OriginalDir;
744
745  /// \brief The directory that the PCH we are reading is stored in.
746  std::string CurrentDir;
747
748  /// \brief Whether this precompiled header is a relocatable PCH file.
749  bool RelocatablePCH;
750
751  /// \brief The system include root to be used when loading the
752  /// precompiled header.
753  const char *isysroot;
754
755  /// \brief Whether to disable the normal validation performed on precompiled
756  /// headers when they are loaded.
757  bool DisableValidation;
758
759  /// \brief Whether to disable the use of stat caches in AST files.
760  bool DisableStatCache;
761
762  /// \brief Mapping from switch-case IDs in the chain to switch-case statements
763  ///
764  /// Statements usually don't have IDs, but switch cases need them, so that the
765  /// switch statement can refer to them.
766  std::map<unsigned, SwitchCase *> SwitchCaseStmts;
767
768  /// \brief Mapping from opaque value IDs to OpaqueValueExprs.
769  std::map<unsigned, OpaqueValueExpr*> OpaqueValueExprs;
770
771  /// \brief The number of stat() calls that hit/missed the stat
772  /// cache.
773  unsigned NumStatHits, NumStatMisses;
774
775  /// \brief The number of source location entries de-serialized from
776  /// the PCH file.
777  unsigned NumSLocEntriesRead;
778
779  /// \brief The number of source location entries in the chain.
780  unsigned TotalNumSLocEntries;
781
782  /// \brief The number of statements (and expressions) de-serialized
783  /// from the chain.
784  unsigned NumStatementsRead;
785
786  /// \brief The total number of statements (and expressions) stored
787  /// in the chain.
788  unsigned TotalNumStatements;
789
790  /// \brief The number of macros de-serialized from the chain.
791  unsigned NumMacrosRead;
792
793  /// \brief The total number of macros stored in the chain.
794  unsigned TotalNumMacros;
795
796  /// \brief The number of selectors that have been read.
797  unsigned NumSelectorsRead;
798
799  /// \brief The number of method pool entries that have been read.
800  unsigned NumMethodPoolEntriesRead;
801
802  /// \brief The number of times we have looked up a selector in the method
803  /// pool and not found anything interesting.
804  unsigned NumMethodPoolMisses;
805
806  /// \brief The total number of method pool entries in the selector table.
807  unsigned TotalNumMethodPoolEntries;
808
809  /// Number of lexical decl contexts read/total.
810  unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
811
812  /// Number of visible decl contexts read/total.
813  unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
814
815  /// Total size of modules, in bits, currently loaded
816  uint64_t TotalModulesSizeInBits;
817
818  /// \brief Number of Decl/types that are currently deserializing.
819  unsigned NumCurrentElementsDeserializing;
820
821  /// Number of CXX base specifiers currently loaded
822  unsigned NumCXXBaseSpecifiersLoaded;
823
824  /// \brief An IdentifierInfo that has been loaded but whose top-level
825  /// declarations of the same name have not (yet) been loaded.
826  struct PendingIdentifierInfo {
827    IdentifierInfo *II;
828    SmallVector<uint32_t, 4> DeclIDs;
829  };
830
831  /// \brief The set of identifiers that were read while the AST reader was
832  /// (recursively) loading declarations.
833  ///
834  /// The declarations on the identifier chain for these identifiers will be
835  /// loaded once the recursive loading has completed.
836  std::deque<PendingIdentifierInfo> PendingIdentifierInfos;
837
838  /// \brief Contains declarations and definitions that will be
839  /// "interesting" to the ASTConsumer, when we get that AST consumer.
840  ///
841  /// "Interesting" declarations are those that have data that may
842  /// need to be emitted, such as inline function definitions or
843  /// Objective-C protocols.
844  std::deque<Decl *> InterestingDecls;
845
846  /// \brief We delay loading of the previous declaration chain to avoid
847  /// deeply nested calls when there are many redeclarations.
848  std::deque<std::pair<Decl *, serialization::DeclID> > PendingPreviousDecls;
849
850  /// \brief Ready to load the previous declaration of the given Decl.
851  void loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID);
852
853  /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
854  SmallVector<Stmt *, 16> StmtStack;
855
856  /// \brief What kind of records we are reading.
857  enum ReadingKind {
858    Read_Decl, Read_Type, Read_Stmt
859  };
860
861  /// \brief What kind of records we are reading.
862  ReadingKind ReadingKind;
863
864  /// \brief RAII object to change the reading kind.
865  class ReadingKindTracker {
866    ASTReader &Reader;
867    enum ReadingKind PrevKind;
868
869    ReadingKindTracker(const ReadingKindTracker&); // do not implement
870    ReadingKindTracker &operator=(const ReadingKindTracker&);// do not implement
871
872  public:
873    ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
874      : Reader(reader), PrevKind(Reader.ReadingKind) {
875      Reader.ReadingKind = newKind;
876    }
877
878    ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
879  };
880
881  /// \brief All predefines buffers in the chain, to be treated as if
882  /// concatenated.
883  PCHPredefinesBlocks PCHPredefinesBuffers;
884
885  /// \brief Suggested contents of the predefines buffer, after this
886  /// PCH file has been processed.
887  ///
888  /// In most cases, this string will be empty, because the predefines
889  /// buffer computed to build the PCH file will be identical to the
890  /// predefines buffer computed from the command line. However, when
891  /// there are differences that the PCH reader can work around, this
892  /// predefines buffer may contain additional definitions.
893  std::string SuggestedPredefines;
894
895  /// \brief Reads a statement from the specified cursor.
896  Stmt *ReadStmtFromStream(PerFileData &F);
897
898  /// \brief Get a FileEntry out of stored-in-PCH filename, making sure we take
899  /// into account all the necessary relocations.
900  const FileEntry *getFileEntry(StringRef filename);
901
902  void MaybeAddSystemRootToFilename(std::string &Filename);
903
904  ASTReadResult ReadASTCore(StringRef FileName, ASTFileType Type);
905  ASTReadResult ReadASTBlock(PerFileData &F);
906  bool CheckPredefinesBuffers();
907  bool ParseLineTable(PerFileData &F, SmallVectorImpl<uint64_t> &Record);
908  ASTReadResult ReadSourceManagerBlock(PerFileData &F);
909  ASTReadResult ReadSLocEntryRecord(int ID);
910  llvm::BitstreamCursor &SLocCursorForID(int ID);
911  SourceLocation getImportLocation(PerFileData *F);
912  bool ParseLanguageOptions(const SmallVectorImpl<uint64_t> &Record);
913
914  struct RecordLocation {
915    RecordLocation(PerFileData *M, uint64_t O)
916      : F(M), Offset(O) {}
917    PerFileData *F;
918    uint64_t Offset;
919  };
920
921  QualType readTypeRecord(unsigned Index);
922  RecordLocation TypeCursorForIndex(unsigned Index);
923  void LoadedDecl(unsigned Index, Decl *D);
924  Decl *ReadDeclRecord(unsigned Index, serialization::DeclID ID);
925  RecordLocation DeclCursorForIndex(unsigned Index, serialization::DeclID ID);
926  RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
927
928  void PassInterestingDeclsToConsumer();
929
930  /// \brief Produce an error diagnostic and return true.
931  ///
932  /// This routine should only be used for fatal errors that have to
933  /// do with non-routine failures (e.g., corrupted AST file).
934  void Error(StringRef Msg);
935  void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
936             StringRef Arg2 = StringRef());
937
938  ASTReader(const ASTReader&); // do not implement
939  ASTReader &operator=(const ASTReader &); // do not implement
940public:
941  typedef SmallVector<uint64_t, 64> RecordData;
942
943  /// \brief Load the AST file and validate its contents against the given
944  /// Preprocessor.
945  ///
946  /// \param PP the preprocessor associated with the context in which this
947  /// precompiled header will be loaded.
948  ///
949  /// \param Context the AST context that this precompiled header will be
950  /// loaded into.
951  ///
952  /// \param isysroot If non-NULL, the system include path specified by the
953  /// user. This is only used with relocatable PCH files. If non-NULL,
954  /// a relocatable PCH file will use the default path "/".
955  ///
956  /// \param DisableValidation If true, the AST reader will suppress most
957  /// of its regular consistency checking, allowing the use of precompiled
958  /// headers that cannot be determined to be compatible.
959  ///
960  /// \param DisableStatCache If true, the AST reader will ignore the
961  /// stat cache in the AST files. This performance pessimization can
962  /// help when an AST file is being used in cases where the
963  /// underlying files in the file system may have changed, but
964  /// parsing should still continue.
965  ASTReader(Preprocessor &PP, ASTContext *Context, const char *isysroot = 0,
966            bool DisableValidation = false, bool DisableStatCache = false);
967
968  /// \brief Load the AST file without using any pre-initialized Preprocessor.
969  ///
970  /// The necessary information to initialize a Preprocessor later can be
971  /// obtained by setting a ASTReaderListener.
972  ///
973  /// \param SourceMgr the source manager into which the AST file will be loaded
974  ///
975  /// \param FileMgr the file manager into which the AST file will be loaded.
976  ///
977  /// \param Diags the diagnostics system to use for reporting errors and
978  /// warnings relevant to loading the AST file.
979  ///
980  /// \param isysroot If non-NULL, the system include path specified by the
981  /// user. This is only used with relocatable PCH files. If non-NULL,
982  /// a relocatable PCH file will use the default path "/".
983  ///
984  /// \param DisableValidation If true, the AST reader will suppress most
985  /// of its regular consistency checking, allowing the use of precompiled
986  /// headers that cannot be determined to be compatible.
987  ///
988  /// \param DisableStatCache If true, the AST reader will ignore the
989  /// stat cache in the AST files. This performance pessimization can
990  /// help when an AST file is being used in cases where the
991  /// underlying files in the file system may have changed, but
992  /// parsing should still continue.
993  ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
994            Diagnostic &Diags, const char *isysroot = 0,
995            bool DisableValidation = false, bool DisableStatCache = false);
996  ~ASTReader();
997
998  /// \brief Load the precompiled header designated by the given file
999  /// name.
1000  ASTReadResult ReadAST(const std::string &FileName, ASTFileType Type);
1001
1002  /// \brief Checks that no file that is stored in PCH is out-of-sync with
1003  /// the actual file in the file system.
1004  ASTReadResult validateFileEntries();
1005
1006  /// \brief Set the AST callbacks listener.
1007  void setListener(ASTReaderListener *listener) {
1008    Listener.reset(listener);
1009  }
1010
1011  /// \brief Set the AST deserialization listener.
1012  void setDeserializationListener(ASTDeserializationListener *Listener);
1013
1014  /// \brief Set the Preprocessor to use.
1015  void setPreprocessor(Preprocessor &pp);
1016
1017  /// \brief Sets and initializes the given Context.
1018  void InitializeContext(ASTContext &Context);
1019
1020  /// \brief Set AST buffers for chained PCHs created and stored in memory.
1021  /// First (not depending on another) PCH in chain is first in array.
1022  void setASTMemoryBuffers(llvm::MemoryBuffer **bufs, unsigned numBufs) {
1023    ASTBuffers.clear();
1024    ASTBuffers.insert(ASTBuffers.begin(), bufs, bufs + numBufs);
1025  }
1026
1027  /// \brief Retrieve the name of the named (primary) AST file
1028  const std::string &getFileName() const { return Chain[0]->FileName; }
1029
1030  /// \brief Retrieve the name of the original source file name
1031  const std::string &getOriginalSourceFile() { return OriginalFileName; }
1032
1033  /// \brief Retrieve the name of the original source file name directly from
1034  /// the AST file, without actually loading the AST file.
1035  static std::string getOriginalSourceFile(const std::string &ASTFileName,
1036                                           FileManager &FileMgr,
1037                                           Diagnostic &Diags);
1038
1039  /// \brief Returns the suggested contents of the predefines buffer,
1040  /// which contains a (typically-empty) subset of the predefines
1041  /// build prior to including the precompiled header.
1042  const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1043
1044  /// \brief Read preprocessed entities into the preprocessing record.
1045  virtual void ReadPreprocessedEntities();
1046
1047  /// \brief Read the preprocessed entity at the given offset.
1048  virtual PreprocessedEntity *ReadPreprocessedEntityAtOffset(uint64_t Offset);
1049
1050  /// \brief Read the header file information for the given file entry.
1051  virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE);
1052
1053  void ReadPragmaDiagnosticMappings(Diagnostic &Diag);
1054
1055  /// \brief Returns the number of source locations found in the chain.
1056  unsigned getTotalNumSLocs() const {
1057    return TotalNumSLocEntries;
1058  }
1059
1060  /// \brief Returns the number of identifiers found in the chain.
1061  unsigned getTotalNumIdentifiers() const {
1062    return static_cast<unsigned>(IdentifiersLoaded.size());
1063  }
1064
1065  /// \brief Returns the number of types found in the chain.
1066  unsigned getTotalNumTypes() const {
1067    return static_cast<unsigned>(TypesLoaded.size());
1068  }
1069
1070  /// \brief Returns the number of declarations found in the chain.
1071  unsigned getTotalNumDecls() const {
1072    return static_cast<unsigned>(DeclsLoaded.size());
1073  }
1074
1075  /// \brief Returns the number of selectors found in the chain.
1076  unsigned getTotalNumSelectors() const {
1077    return static_cast<unsigned>(SelectorsLoaded.size());
1078  }
1079
1080  /// \brief Returns the number of preprocessed entities known to the AST
1081  /// reader.
1082  unsigned getTotalNumPreprocessedEntities() const {
1083    unsigned Result = 0;
1084    for (unsigned I = 0, N = Chain.size(); I != N; ++I)
1085      Result += Chain[I]->NumPreallocatedPreprocessingEntities;
1086
1087    return Result;
1088  }
1089
1090  /// \brief Returns the number of macro definitions found in the chain.
1091  unsigned getTotalNumMacroDefinitions() const {
1092    return static_cast<unsigned>(MacroDefinitionsLoaded.size());
1093  }
1094
1095  /// \brief Returns the number of C++ base specifiers found in the chain.
1096  unsigned getTotalNumCXXBaseSpecifiers() const {
1097    return NumCXXBaseSpecifiersLoaded;
1098  }
1099
1100  /// \brief Reads a TemplateArgumentLocInfo appropriate for the
1101  /// given TemplateArgument kind.
1102  TemplateArgumentLocInfo
1103  GetTemplateArgumentLocInfo(PerFileData &F, TemplateArgument::ArgKind Kind,
1104                             const RecordData &Record, unsigned &Idx);
1105
1106  /// \brief Reads a TemplateArgumentLoc.
1107  TemplateArgumentLoc
1108  ReadTemplateArgumentLoc(PerFileData &F,
1109                          const RecordData &Record, unsigned &Idx);
1110
1111  /// \brief Reads a declarator info from the given record.
1112  TypeSourceInfo *GetTypeSourceInfo(PerFileData &F,
1113                                    const RecordData &Record, unsigned &Idx);
1114
1115  /// \brief Resolve and return the translation unit declaration.
1116  TranslationUnitDecl *GetTranslationUnitDecl();
1117
1118  /// \brief Resolve a type ID into a type, potentially building a new
1119  /// type.
1120  QualType GetType(serialization::TypeID ID);
1121
1122  /// \brief Resolve a local type ID within a given AST file into a type.
1123  QualType getLocalType(PerFileData &F, unsigned LocalID);
1124
1125  /// \brief Map a local type ID within a given AST file into a global type ID.
1126  serialization::TypeID getGlobalTypeID(PerFileData &F, unsigned LocalID) const;
1127
1128  /// \brief Read a type from the current position in the given record, which
1129  /// was read from the given AST file.
1130  QualType readType(PerFileData &F, const RecordData &Record, unsigned &Idx) {
1131    if (Idx >= Record.size())
1132      return QualType();
1133
1134    return getLocalType(F, Record[Idx++]);
1135  }
1136
1137  /// \brief Returns the type ID associated with the given type.
1138  /// If the type didn't come from the AST file the ID that is returned is
1139  /// marked as "doesn't exist in AST".
1140  serialization::TypeID GetTypeID(QualType T) const;
1141
1142  /// \brief Returns the type index associated with the given type.
1143  /// If the type didn't come from the AST file the index that is returned is
1144  /// marked as "doesn't exist in AST".
1145  serialization::TypeIdx GetTypeIdx(QualType T) const;
1146
1147  /// \brief Map from a local declaration ID within a given module to a
1148  /// global declaration ID.
1149  serialization::DeclID getGlobalDeclID(PerFileData &F, unsigned LocalID) const;
1150
1151  /// \brief Resolve a declaration ID into a declaration, potentially
1152  /// building a new declaration.
1153  Decl *GetDecl(serialization::DeclID ID);
1154  virtual Decl *GetExternalDecl(uint32_t ID);
1155
1156  /// \brief Reads a declaration with the given local ID in the give module.
1157  Decl *GetLocalDecl(PerFileData &F, uint32_t LocalID) {
1158    return GetDecl(getGlobalDeclID(F, LocalID));
1159  }
1160
1161  /// \brief Reads a declaration with the given local ID in the give module.
1162  ///
1163  /// \returns The requested declaration, casted to the given return type.
1164  template<typename T>
1165  T *GetLocalDeclAs(PerFileData &F, uint32_t LocalID) {
1166    return cast_or_null<T>(GetLocalDecl(F, LocalID));
1167  }
1168
1169  /// \brief Reads a declaration ID from the given position in a record in the
1170  /// given module.
1171  ///
1172  /// \returns The declaration ID read from the record, adjusted to a global ID.
1173  serialization::DeclID ReadDeclID(PerFileData &F, const RecordData &Record,
1174                                   unsigned &Idx);
1175
1176  /// \brief Reads a declaration from the given position in a record in the
1177  /// given module.
1178  Decl *ReadDecl(PerFileData &F, const RecordData &R, unsigned &I) {
1179    return GetDecl(ReadDeclID(F, R, I));
1180  }
1181
1182  /// \brief Reads a declaration from the given position in a record in the
1183  /// given module.
1184  ///
1185  /// \returns The declaration read from this location, casted to the given
1186  /// result type.
1187  template<typename T>
1188  T *ReadDeclAs(PerFileData &F, const RecordData &R, unsigned &I) {
1189    return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1190  }
1191
1192  /// \brief Resolve a CXXBaseSpecifiers ID into an offset into the chain
1193  /// of loaded AST files.
1194  uint64_t GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID);
1195
1196  virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
1197
1198  /// \brief Resolve the offset of a statement into a statement.
1199  ///
1200  /// This operation will read a new statement from the external
1201  /// source each time it is called, and is meant to be used via a
1202  /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1203  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
1204
1205  /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1206  /// specified cursor.  Read the abbreviations that are at the top of the block
1207  /// and then leave the cursor pointing into the block.
1208  bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
1209
1210  /// \brief Finds all the visible declarations with a given name.
1211  /// The current implementation of this method just loads the entire
1212  /// lookup table as unmaterialized references.
1213  virtual DeclContext::lookup_result
1214  FindExternalVisibleDeclsByName(const DeclContext *DC,
1215                                 DeclarationName Name);
1216
1217  virtual void MaterializeVisibleDecls(const DeclContext *DC);
1218
1219  /// \brief Read all of the declarations lexically stored in a
1220  /// declaration context.
1221  ///
1222  /// \param DC The declaration context whose declarations will be
1223  /// read.
1224  ///
1225  /// \param Decls Vector that will contain the declarations loaded
1226  /// from the external source. The caller is responsible for merging
1227  /// these declarations with any declarations already stored in the
1228  /// declaration context.
1229  ///
1230  /// \returns true if there was an error while reading the
1231  /// declarations for this declaration context.
1232  virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
1233                                        bool (*isKindWeWant)(Decl::Kind),
1234                                        SmallVectorImpl<Decl*> &Decls);
1235
1236  /// \brief Notify ASTReader that we started deserialization of
1237  /// a decl or type so until FinishedDeserializing is called there may be
1238  /// decls that are initializing. Must be paired with FinishedDeserializing.
1239  virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
1240
1241  /// \brief Notify ASTReader that we finished the deserialization of
1242  /// a decl or type. Must be paired with StartedDeserializing.
1243  virtual void FinishedDeserializing();
1244
1245  /// \brief Function that will be invoked when we begin parsing a new
1246  /// translation unit involving this external AST source.
1247  ///
1248  /// This function will provide all of the external definitions to
1249  /// the ASTConsumer.
1250  virtual void StartTranslationUnit(ASTConsumer *Consumer);
1251
1252  /// \brief Print some statistics about AST usage.
1253  virtual void PrintStats();
1254
1255  /// \brief Dump information about the AST reader to standard error.
1256  void dump();
1257
1258  /// Return the amount of memory used by memory buffers, breaking down
1259  /// by heap-backed versus mmap'ed memory.
1260  virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
1261
1262  /// \brief Initialize the semantic source with the Sema instance
1263  /// being used to perform semantic analysis on the abstract syntax
1264  /// tree.
1265  virtual void InitializeSema(Sema &S);
1266
1267  /// \brief Inform the semantic consumer that Sema is no longer available.
1268  virtual void ForgetSema() { SemaObj = 0; }
1269
1270  /// \brief Retrieve the IdentifierInfo for the named identifier.
1271  ///
1272  /// This routine builds a new IdentifierInfo for the given identifier. If any
1273  /// declarations with this name are visible from translation unit scope, their
1274  /// declarations will be deserialized and introduced into the declaration
1275  /// chain of the identifier.
1276  virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
1277  IdentifierInfo *get(StringRef Name) {
1278    return get(Name.begin(), Name.end());
1279  }
1280
1281  /// \brief Retrieve an iterator into the set of all identifiers
1282  /// in all loaded AST files.
1283  virtual IdentifierIterator *getIdentifiers() const;
1284
1285  /// \brief Load the contents of the global method pool for a given
1286  /// selector.
1287  ///
1288  /// \returns a pair of Objective-C methods lists containing the
1289  /// instance and factory methods, respectively, with this selector.
1290  virtual std::pair<ObjCMethodList, ObjCMethodList>
1291    ReadMethodPool(Selector Sel);
1292
1293  /// \brief Load the set of namespaces that are known to the external source,
1294  /// which will be used during typo correction.
1295  virtual void ReadKnownNamespaces(
1296                           SmallVectorImpl<NamespaceDecl *> &Namespaces);
1297
1298  /// \brief Load a selector from disk, registering its ID if it exists.
1299  void LoadSelector(Selector Sel);
1300
1301  void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1302  void SetGloballyVisibleDecls(IdentifierInfo *II,
1303                               const SmallVectorImpl<uint32_t> &DeclIDs,
1304                               bool Nonrecursive = false);
1305
1306  /// \brief Report a diagnostic.
1307  DiagnosticBuilder Diag(unsigned DiagID);
1308
1309  /// \brief Report a diagnostic.
1310  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1311
1312  IdentifierInfo *DecodeIdentifierInfo(unsigned Idx);
1313
1314  IdentifierInfo *GetIdentifierInfo(const RecordData &Record, unsigned &Idx) {
1315    return DecodeIdentifierInfo(Record[Idx++]);
1316  }
1317
1318  virtual IdentifierInfo *GetIdentifier(unsigned ID) {
1319    return DecodeIdentifierInfo(ID);
1320  }
1321
1322  /// \brief Read the source location entry with index ID.
1323  virtual bool ReadSLocEntry(int ID);
1324
1325  Selector DecodeSelector(unsigned Idx);
1326
1327  virtual Selector GetExternalSelector(uint32_t ID);
1328  uint32_t GetNumExternalSelectors();
1329
1330  Selector GetSelector(const RecordData &Record, unsigned &Idx) {
1331    return DecodeSelector(Record[Idx++]);
1332  }
1333
1334  /// \brief Read a declaration name.
1335  DeclarationName ReadDeclarationName(PerFileData &F,
1336                                      const RecordData &Record, unsigned &Idx);
1337  void ReadDeclarationNameLoc(PerFileData &F,
1338                              DeclarationNameLoc &DNLoc, DeclarationName Name,
1339                              const RecordData &Record, unsigned &Idx);
1340  void ReadDeclarationNameInfo(PerFileData &F, DeclarationNameInfo &NameInfo,
1341                               const RecordData &Record, unsigned &Idx);
1342
1343  void ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
1344                         const RecordData &Record, unsigned &Idx);
1345
1346  NestedNameSpecifier *ReadNestedNameSpecifier(PerFileData &F,
1347                                               const RecordData &Record,
1348                                               unsigned &Idx);
1349
1350  NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(PerFileData &F,
1351                                                    const RecordData &Record,
1352                                                    unsigned &Idx);
1353
1354  /// \brief Read a template name.
1355  TemplateName ReadTemplateName(PerFileData &F, const RecordData &Record,
1356                                unsigned &Idx);
1357
1358  /// \brief Read a template argument.
1359  TemplateArgument ReadTemplateArgument(PerFileData &F,
1360                                        const RecordData &Record,unsigned &Idx);
1361
1362  /// \brief Read a template parameter list.
1363  TemplateParameterList *ReadTemplateParameterList(PerFileData &F,
1364                                                   const RecordData &Record,
1365                                                   unsigned &Idx);
1366
1367  /// \brief Read a template argument array.
1368  void
1369  ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
1370                           PerFileData &F, const RecordData &Record,
1371                           unsigned &Idx);
1372
1373  /// \brief Read a UnresolvedSet structure.
1374  void ReadUnresolvedSet(PerFileData &F, UnresolvedSetImpl &Set,
1375                         const RecordData &Record, unsigned &Idx);
1376
1377  /// \brief Read a C++ base specifier.
1378  CXXBaseSpecifier ReadCXXBaseSpecifier(PerFileData &F,
1379                                        const RecordData &Record,unsigned &Idx);
1380
1381  /// \brief Read a CXXCtorInitializer array.
1382  std::pair<CXXCtorInitializer **, unsigned>
1383  ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
1384                          unsigned &Idx);
1385
1386  /// \brief Read a source location from raw form.
1387  SourceLocation ReadSourceLocation(PerFileData &Module, unsigned Raw) {
1388    unsigned Flag = Raw & (1U << 31);
1389    unsigned Offset = Raw & ~(1U << 31);
1390    assert(Module.SLocRemap.find(Offset) != Module.SLocRemap.end() &&
1391           "Cannot find offset to remap.");
1392    int Remap = Module.SLocRemap.find(Offset)->second;
1393    Offset += Remap;
1394    assert((Offset & (1U << 31)) == 0 &&
1395           "Bad offset in reading source location");
1396    return SourceLocation::getFromRawEncoding(Offset | Flag);
1397  }
1398
1399  /// \brief Read a source location.
1400  SourceLocation ReadSourceLocation(PerFileData &Module,
1401                                    const RecordData &Record, unsigned& Idx) {
1402    return ReadSourceLocation(Module, Record[Idx++]);
1403  }
1404
1405  /// \brief Read a source range.
1406  SourceRange ReadSourceRange(PerFileData &F,
1407                              const RecordData &Record, unsigned& Idx);
1408
1409  /// \brief Read an integral value
1410  llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
1411
1412  /// \brief Read a signed integral value
1413  llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
1414
1415  /// \brief Read a floating-point value
1416  llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx);
1417
1418  // \brief Read a string
1419  std::string ReadString(const RecordData &Record, unsigned &Idx);
1420
1421  /// \brief Read a version tuple.
1422  VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
1423
1424  CXXTemporary *ReadCXXTemporary(PerFileData &F, const RecordData &Record,
1425                                 unsigned &Idx);
1426
1427  /// \brief Reads attributes from the current stream position.
1428  void ReadAttributes(PerFileData &F, AttrVec &Attrs,
1429                      const RecordData &Record, unsigned &Idx);
1430
1431  /// \brief Reads a statement.
1432  Stmt *ReadStmt(PerFileData &F);
1433
1434  /// \brief Reads an expression.
1435  Expr *ReadExpr(PerFileData &F);
1436
1437  /// \brief Reads a sub-statement operand during statement reading.
1438  Stmt *ReadSubStmt() {
1439    assert(ReadingKind == Read_Stmt &&
1440           "Should be called only during statement reading!");
1441    // Subexpressions are stored from last to first, so the next Stmt we need
1442    // is at the back of the stack.
1443    assert(!StmtStack.empty() && "Read too many sub statements!");
1444    return StmtStack.pop_back_val();
1445  }
1446
1447  /// \brief Reads a sub-expression operand during statement reading.
1448  Expr *ReadSubExpr();
1449
1450  /// \brief Reads the macro record located at the given offset.
1451  PreprocessedEntity *ReadMacroRecord(PerFileData &F, uint64_t Offset);
1452
1453  /// \brief Reads the preprocessed entity located at the current stream
1454  /// position.
1455  PreprocessedEntity *LoadPreprocessedEntity(PerFileData &F);
1456
1457  /// \brief Note that the identifier is a macro whose record will be loaded
1458  /// from the given AST file at the given (file-local) offset.
1459  void SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1460                            uint64_t Offset);
1461
1462  /// \brief Read the set of macros defined by this external macro source.
1463  virtual void ReadDefinedMacros();
1464
1465  /// \brief Read the macro definition for this identifier.
1466  virtual void LoadMacroDefinition(IdentifierInfo *II);
1467
1468  /// \brief Read the macro definition corresponding to this iterator
1469  /// into the unread macro record offsets table.
1470  void LoadMacroDefinition(
1471                     llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos);
1472
1473  /// \brief Retrieve the macro definition with the given ID.
1474  MacroDefinition *getMacroDefinition(serialization::MacroID ID);
1475
1476  /// \brief Retrieve the AST context that this AST reader supplements.
1477  ASTContext *getContext() { return Context; }
1478
1479  // \brief Contains declarations that were loaded before we have
1480  // access to a Sema object.
1481  SmallVector<NamedDecl *, 16> PreloadedDecls;
1482
1483  /// \brief Retrieve the semantic analysis object used to analyze the
1484  /// translation unit in which the precompiled header is being
1485  /// imported.
1486  Sema *getSema() { return SemaObj; }
1487
1488  /// \brief Retrieve the identifier table associated with the
1489  /// preprocessor.
1490  IdentifierTable &getIdentifierTable();
1491
1492  /// \brief Record that the given ID maps to the given switch-case
1493  /// statement.
1494  void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
1495
1496  /// \brief Retrieve the switch-case statement with the given ID.
1497  SwitchCase *getSwitchCaseWithID(unsigned ID);
1498
1499  void ClearSwitchCaseIDs();
1500};
1501
1502/// \brief Helper class that saves the current stream position and
1503/// then restores it when destroyed.
1504struct SavedStreamPosition {
1505  explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
1506  : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
1507
1508  ~SavedStreamPosition() {
1509    Cursor.JumpToBit(Offset);
1510  }
1511
1512private:
1513  llvm::BitstreamCursor &Cursor;
1514  uint64_t Offset;
1515};
1516
1517inline void PCHValidator::Error(const char *Msg) {
1518  Reader.Error(Msg);
1519}
1520
1521} // end namespace clang
1522
1523#endif
1524