Share the common code of ComputeHash(Selector Sel) instead of keeping 2 copies in...
[clang.git] / lib / Serialization / ASTReader.cpp
blob238ce3f5ed883bc9b9889324a2d59f2828ac9575
1 //===--- ASTReader.cpp - 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.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Serialization/ASTReader.h"
15 #include "clang/Serialization/ASTDeserializationListener.h"
16 #include "ASTCommon.h"
17 #include "clang/Frontend/FrontendDiagnostic.h"
18 #include "clang/Frontend/Utils.h"
19 #include "clang/Sema/Sema.h"
20 #include "clang/AST/ASTConsumer.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/Type.h"
24 #include "clang/AST/TypeLocVisitor.h"
25 #include "clang/Lex/MacroInfo.h"
26 #include "clang/Lex/PreprocessingRecord.h"
27 #include "clang/Lex/Preprocessor.h"
28 #include "clang/Lex/HeaderSearch.h"
29 #include "clang/Basic/OnDiskHashTable.h"
30 #include "clang/Basic/SourceManager.h"
31 #include "clang/Basic/SourceManagerInternals.h"
32 #include "clang/Basic/FileManager.h"
33 #include "clang/Basic/TargetInfo.h"
34 #include "clang/Basic/Version.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/Bitcode/BitstreamReader.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/System/Path.h"
40 #include <algorithm>
41 #include <iterator>
42 #include <cstdio>
43 #include <sys/stat.h>
44 using namespace clang;
45 using namespace clang::serialization;
47 //===----------------------------------------------------------------------===//
48 // PCH validator implementation
49 //===----------------------------------------------------------------------===//
51 ASTReaderListener::~ASTReaderListener() {}
53 bool
54 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
55 const LangOptions &PPLangOpts = PP.getLangOptions();
56 #define PARSE_LANGOPT_BENIGN(Option)
57 #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
58 if (PPLangOpts.Option != LangOpts.Option) { \
59 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
60 return true; \
63 PARSE_LANGOPT_BENIGN(Trigraphs);
64 PARSE_LANGOPT_BENIGN(BCPLComment);
65 PARSE_LANGOPT_BENIGN(DollarIdents);
66 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
67 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
68 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
69 PARSE_LANGOPT_BENIGN(ImplicitInt);
70 PARSE_LANGOPT_BENIGN(Digraphs);
71 PARSE_LANGOPT_BENIGN(HexFloats);
72 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
73 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
74 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
75 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
76 PARSE_LANGOPT_BENIGN(CXXOperatorName);
77 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
78 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
79 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
80 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
81 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
82 diag::warn_pch_no_constant_cfstrings);
83 PARSE_LANGOPT_BENIGN(PascalStrings);
84 PARSE_LANGOPT_BENIGN(WritableStrings);
85 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
86 diag::warn_pch_lax_vector_conversions);
87 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
88 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
89 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
90 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
91 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
92 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
93 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
94 diag::warn_pch_thread_safe_statics);
95 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
96 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
97 PARSE_LANGOPT_BENIGN(EmitAllDecls);
98 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
99 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
100 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
101 diag::warn_pch_heinous_extensions);
102 // FIXME: Most of the options below are benign if the macro wasn't
103 // used. Unfortunately, this means that a PCH compiled without
104 // optimization can't be used with optimization turned on, even
105 // though the only thing that changes is whether __OPTIMIZE__ was
106 // defined... but if __OPTIMIZE__ never showed up in the header, it
107 // doesn't matter. We could consider making this some special kind
108 // of check.
109 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
110 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
111 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
112 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
113 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
114 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
115 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
116 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
117 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
118 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
119 Reader.Diag(diag::warn_pch_gc_mode)
120 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
121 return true;
123 PARSE_LANGOPT_BENIGN(getVisibilityMode());
124 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
125 diag::warn_pch_stack_protector);
126 PARSE_LANGOPT_BENIGN(InstantiationDepth);
127 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
128 PARSE_LANGOPT_BENIGN(CatchUndefined);
129 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
130 PARSE_LANGOPT_BENIGN(SpellChecking);
131 #undef PARSE_LANGOPT_IMPORTANT
132 #undef PARSE_LANGOPT_BENIGN
134 return false;
137 bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
138 if (Triple == PP.getTargetInfo().getTriple().str())
139 return false;
141 Reader.Diag(diag::warn_pch_target_triple)
142 << Triple << PP.getTargetInfo().getTriple().str();
143 return true;
146 struct EmptyStringRef {
147 bool operator ()(llvm::StringRef r) const { return r.empty(); }
149 struct EmptyBlock {
150 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
153 static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
154 PCHPredefinesBlocks R) {
155 // First, sum up the lengths.
156 unsigned LL = 0, RL = 0;
157 for (unsigned I = 0, N = L.size(); I != N; ++I) {
158 LL += L[I].size();
160 for (unsigned I = 0, N = R.size(); I != N; ++I) {
161 RL += R[I].Data.size();
163 if (LL != RL)
164 return false;
165 if (LL == 0 && RL == 0)
166 return true;
168 // Kick out empty parts, they confuse the algorithm below.
169 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
170 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
172 // Do it the hard way. At this point, both vectors must be non-empty.
173 llvm::StringRef LR = L[0], RR = R[0].Data;
174 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
175 (void) RN;
176 for (;;) {
177 // Compare the current pieces.
178 if (LR.size() == RR.size()) {
179 // If they're the same length, it's pretty easy.
180 if (LR != RR)
181 return false;
182 // Both pieces are done, advance.
183 ++LI;
184 ++RI;
185 // If either string is done, they're both done, since they're the same
186 // length.
187 if (LI == LN) {
188 assert(RI == RN && "Strings not the same length after all?");
189 return true;
191 LR = L[LI];
192 RR = R[RI].Data;
193 } else if (LR.size() < RR.size()) {
194 // Right piece is longer.
195 if (!RR.startswith(LR))
196 return false;
197 ++LI;
198 assert(LI != LN && "Strings not the same length after all?");
199 RR = RR.substr(LR.size());
200 LR = L[LI];
201 } else {
202 // Left piece is longer.
203 if (!LR.startswith(RR))
204 return false;
205 ++RI;
206 assert(RI != RN && "Strings not the same length after all?");
207 LR = LR.substr(RR.size());
208 RR = R[RI].Data;
213 static std::pair<FileID, llvm::StringRef::size_type>
214 FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
215 std::pair<FileID, llvm::StringRef::size_type> Res;
216 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
217 Res.second = Buffers[I].Data.find(MacroDef);
218 if (Res.second != llvm::StringRef::npos) {
219 Res.first = Buffers[I].BufferID;
220 break;
223 return Res;
226 bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
227 llvm::StringRef OriginalFileName,
228 std::string &SuggestedPredefines) {
229 // We are in the context of an implicit include, so the predefines buffer will
230 // have a #include entry for the PCH file itself (as normalized by the
231 // preprocessor initialization). Find it and skip over it in the checking
232 // below.
233 llvm::SmallString<256> PCHInclude;
234 PCHInclude += "#include \"";
235 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
236 PCHInclude += "\"\n";
237 std::pair<llvm::StringRef,llvm::StringRef> Split =
238 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
239 llvm::StringRef Left = Split.first, Right = Split.second;
240 if (Left == PP.getPredefines()) {
241 Error("Missing PCH include entry!");
242 return true;
245 // If the concatenation of all the PCH buffers is equal to the adjusted
246 // command line, we're done.
247 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
248 CommandLine.push_back(Left);
249 CommandLine.push_back(Right);
250 if (EqualConcatenations(CommandLine, Buffers))
251 return false;
253 SourceManager &SourceMgr = PP.getSourceManager();
255 // The predefines buffers are different. Determine what the differences are,
256 // and whether they require us to reject the PCH file.
257 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
258 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
259 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
261 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
262 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
263 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
265 // Sort both sets of predefined buffer lines, since we allow some extra
266 // definitions and they may appear at any point in the output.
267 std::sort(CmdLineLines.begin(), CmdLineLines.end());
268 std::sort(PCHLines.begin(), PCHLines.end());
270 // Determine which predefines that were used to build the PCH file are missing
271 // from the command line.
272 std::vector<llvm::StringRef> MissingPredefines;
273 std::set_difference(PCHLines.begin(), PCHLines.end(),
274 CmdLineLines.begin(), CmdLineLines.end(),
275 std::back_inserter(MissingPredefines));
277 bool MissingDefines = false;
278 bool ConflictingDefines = false;
279 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
280 llvm::StringRef Missing = MissingPredefines[I];
281 if (!Missing.startswith("#define ")) {
282 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
283 return true;
286 // This is a macro definition. Determine the name of the macro we're
287 // defining.
288 std::string::size_type StartOfMacroName = strlen("#define ");
289 std::string::size_type EndOfMacroName
290 = Missing.find_first_of("( \n\r", StartOfMacroName);
291 assert(EndOfMacroName != std::string::npos &&
292 "Couldn't find the end of the macro name");
293 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
295 // Determine whether this macro was given a different definition on the
296 // command line.
297 std::string MacroDefStart = "#define " + MacroName.str();
298 std::string::size_type MacroDefLen = MacroDefStart.size();
299 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
300 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
301 MacroDefStart);
302 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
303 if (!ConflictPos->startswith(MacroDefStart)) {
304 // Different macro; we're done.
305 ConflictPos = CmdLineLines.end();
306 break;
309 assert(ConflictPos->size() > MacroDefLen &&
310 "Invalid #define in predefines buffer?");
311 if ((*ConflictPos)[MacroDefLen] != ' ' &&
312 (*ConflictPos)[MacroDefLen] != '(')
313 continue; // Longer macro name; keep trying.
315 // We found a conflicting macro definition.
316 break;
319 if (ConflictPos != CmdLineLines.end()) {
320 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
321 << MacroName;
323 // Show the definition of this macro within the PCH file.
324 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
325 FindMacro(Buffers, Missing);
326 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
327 SourceLocation PCHMissingLoc =
328 SourceMgr.getLocForStartOfFile(MacroLoc.first)
329 .getFileLocWithOffset(MacroLoc.second);
330 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
332 ConflictingDefines = true;
333 continue;
336 // If the macro doesn't conflict, then we'll just pick up the macro
337 // definition from the PCH file. Warn the user that they made a mistake.
338 if (ConflictingDefines)
339 continue; // Don't complain if there are already conflicting defs
341 if (!MissingDefines) {
342 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
343 MissingDefines = true;
346 // Show the definition of this macro within the PCH file.
347 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
348 FindMacro(Buffers, Missing);
349 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
350 SourceLocation PCHMissingLoc =
351 SourceMgr.getLocForStartOfFile(MacroLoc.first)
352 .getFileLocWithOffset(MacroLoc.second);
353 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
356 if (ConflictingDefines)
357 return true;
359 // Determine what predefines were introduced based on command-line
360 // parameters that were not present when building the PCH
361 // file. Extra #defines are okay, so long as the identifiers being
362 // defined were not used within the precompiled header.
363 std::vector<llvm::StringRef> ExtraPredefines;
364 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
365 PCHLines.begin(), PCHLines.end(),
366 std::back_inserter(ExtraPredefines));
367 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
368 llvm::StringRef &Extra = ExtraPredefines[I];
369 if (!Extra.startswith("#define ")) {
370 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
371 return true;
374 // This is an extra macro definition. Determine the name of the
375 // macro we're defining.
376 std::string::size_type StartOfMacroName = strlen("#define ");
377 std::string::size_type EndOfMacroName
378 = Extra.find_first_of("( \n\r", StartOfMacroName);
379 assert(EndOfMacroName != std::string::npos &&
380 "Couldn't find the end of the macro name");
381 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
383 // Check whether this name was used somewhere in the PCH file. If
384 // so, defining it as a macro could change behavior, so we reject
385 // the PCH file.
386 if (IdentifierInfo *II = Reader.get(MacroName)) {
387 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
388 return true;
391 // Add this definition to the suggested predefines buffer.
392 SuggestedPredefines += Extra;
393 SuggestedPredefines += '\n';
396 // If we get here, it's because the predefines buffer had compatible
397 // contents. Accept the PCH file.
398 return false;
401 void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
402 unsigned ID) {
403 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
404 ++NumHeaderInfos;
407 void PCHValidator::ReadCounter(unsigned Value) {
408 PP.setCounterValue(Value);
411 //===----------------------------------------------------------------------===//
412 // AST reader implementation
413 //===----------------------------------------------------------------------===//
415 ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
416 const char *isysroot, bool DisableValidation)
417 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
418 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
419 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
420 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
421 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
422 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
423 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
424 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
425 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
426 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
427 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
428 RelocatablePCH = false;
431 ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
432 Diagnostic &Diags, const char *isysroot,
433 bool DisableValidation)
434 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
435 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
436 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
437 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
438 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
439 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
440 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
441 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
442 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
443 NumCurrentElementsDeserializing(0) {
444 RelocatablePCH = false;
447 ASTReader::~ASTReader() {
448 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
449 delete Chain[e - i - 1];
452 void
453 ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
454 DeserializationListener = Listener;
455 if (DeserializationListener)
456 DeserializationListener->SetReader(this);
460 namespace {
461 class ASTSelectorLookupTrait {
462 ASTReader &Reader;
464 public:
465 struct data_type {
466 SelectorID ID;
467 ObjCMethodList Instance, Factory;
470 typedef Selector external_key_type;
471 typedef external_key_type internal_key_type;
473 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
475 static bool EqualKey(const internal_key_type& a,
476 const internal_key_type& b) {
477 return a == b;
480 static unsigned ComputeHash(Selector Sel) {
481 return serialization::ComputeHash(Sel);
484 // This hopefully will just get inlined and removed by the optimizer.
485 static const internal_key_type&
486 GetInternalKey(const external_key_type& x) { return x; }
488 static std::pair<unsigned, unsigned>
489 ReadKeyDataLength(const unsigned char*& d) {
490 using namespace clang::io;
491 unsigned KeyLen = ReadUnalignedLE16(d);
492 unsigned DataLen = ReadUnalignedLE16(d);
493 return std::make_pair(KeyLen, DataLen);
496 internal_key_type ReadKey(const unsigned char* d, unsigned) {
497 using namespace clang::io;
498 SelectorTable &SelTable = Reader.getContext()->Selectors;
499 unsigned N = ReadUnalignedLE16(d);
500 IdentifierInfo *FirstII
501 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
502 if (N == 0)
503 return SelTable.getNullarySelector(FirstII);
504 else if (N == 1)
505 return SelTable.getUnarySelector(FirstII);
507 llvm::SmallVector<IdentifierInfo *, 16> Args;
508 Args.push_back(FirstII);
509 for (unsigned I = 1; I != N; ++I)
510 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
512 return SelTable.getSelector(N, Args.data());
515 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
516 using namespace clang::io;
518 data_type Result;
520 Result.ID = ReadUnalignedLE32(d);
521 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
522 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
524 // Load instance methods
525 ObjCMethodList *Prev = 0;
526 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
527 ObjCMethodDecl *Method
528 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
529 if (!Result.Instance.Method) {
530 // This is the first method, which is the easy case.
531 Result.Instance.Method = Method;
532 Prev = &Result.Instance;
533 continue;
536 ObjCMethodList *Mem =
537 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
538 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
539 Prev = Prev->Next;
542 // Load factory methods
543 Prev = 0;
544 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
545 ObjCMethodDecl *Method
546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
547 if (!Result.Factory.Method) {
548 // This is the first method, which is the easy case.
549 Result.Factory.Method = Method;
550 Prev = &Result.Factory;
551 continue;
554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
557 Prev = Prev->Next;
560 return Result;
564 } // end anonymous namespace
566 /// \brief The on-disk hash table used for the global method pool.
567 typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
568 ASTSelectorLookupTable;
570 namespace {
571 class ASTIdentifierLookupTrait {
572 ASTReader &Reader;
573 llvm::BitstreamCursor &Stream;
575 // If we know the IdentifierInfo in advance, it is here and we will
576 // not build a new one. Used when deserializing information about an
577 // identifier that was constructed before the AST file was read.
578 IdentifierInfo *KnownII;
580 public:
581 typedef IdentifierInfo * data_type;
583 typedef const std::pair<const char*, unsigned> external_key_type;
585 typedef external_key_type internal_key_type;
587 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
588 IdentifierInfo *II = 0)
589 : Reader(Reader), Stream(Stream), KnownII(II) { }
591 static bool EqualKey(const internal_key_type& a,
592 const internal_key_type& b) {
593 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
594 : false;
597 static unsigned ComputeHash(const internal_key_type& a) {
598 return llvm::HashString(llvm::StringRef(a.first, a.second));
601 // This hopefully will just get inlined and removed by the optimizer.
602 static const internal_key_type&
603 GetInternalKey(const external_key_type& x) { return x; }
605 static std::pair<unsigned, unsigned>
606 ReadKeyDataLength(const unsigned char*& d) {
607 using namespace clang::io;
608 unsigned DataLen = ReadUnalignedLE16(d);
609 unsigned KeyLen = ReadUnalignedLE16(d);
610 return std::make_pair(KeyLen, DataLen);
613 static std::pair<const char*, unsigned>
614 ReadKey(const unsigned char* d, unsigned n) {
615 assert(n >= 2 && d[n-1] == '\0');
616 return std::make_pair((const char*) d, n-1);
619 IdentifierInfo *ReadData(const internal_key_type& k,
620 const unsigned char* d,
621 unsigned DataLen) {
622 using namespace clang::io;
623 IdentID ID = ReadUnalignedLE32(d);
624 bool IsInteresting = ID & 0x01;
626 // Wipe out the "is interesting" bit.
627 ID = ID >> 1;
629 if (!IsInteresting) {
630 // For uninteresting identifiers, just build the IdentifierInfo
631 // and associate it with the persistent ID.
632 IdentifierInfo *II = KnownII;
633 if (!II)
634 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
635 Reader.SetIdentifierInfo(ID, II);
636 II->setIsFromAST();
637 return II;
640 unsigned Bits = ReadUnalignedLE16(d);
641 bool CPlusPlusOperatorKeyword = Bits & 0x01;
642 Bits >>= 1;
643 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
644 Bits >>= 1;
645 bool Poisoned = Bits & 0x01;
646 Bits >>= 1;
647 bool ExtensionToken = Bits & 0x01;
648 Bits >>= 1;
649 bool hasMacroDefinition = Bits & 0x01;
650 Bits >>= 1;
651 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
652 Bits >>= 10;
654 assert(Bits == 0 && "Extra bits in the identifier?");
655 DataLen -= 6;
657 // Build the IdentifierInfo itself and link the identifier ID with
658 // the new IdentifierInfo.
659 IdentifierInfo *II = KnownII;
660 if (!II)
661 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
662 Reader.SetIdentifierInfo(ID, II);
664 // Set or check the various bits in the IdentifierInfo structure.
665 // Token IDs are read-only.
666 if (HasRevertedTokenIDToIdentifier)
667 II->RevertTokenIDToIdentifier();
668 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
669 assert(II->isExtensionToken() == ExtensionToken &&
670 "Incorrect extension token flag");
671 (void)ExtensionToken;
672 II->setIsPoisoned(Poisoned);
673 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
674 "Incorrect C++ operator keyword flag");
675 (void)CPlusPlusOperatorKeyword;
677 // If this identifier is a macro, deserialize the macro
678 // definition.
679 if (hasMacroDefinition) {
680 uint32_t Offset = ReadUnalignedLE32(d);
681 Reader.ReadMacroRecord(Stream, Offset);
682 DataLen -= 4;
685 // Read all of the declarations visible at global scope with this
686 // name.
687 if (Reader.getContext() == 0) return II;
688 if (DataLen > 0) {
689 llvm::SmallVector<uint32_t, 4> DeclIDs;
690 for (; DataLen > 0; DataLen -= 4)
691 DeclIDs.push_back(ReadUnalignedLE32(d));
692 Reader.SetGloballyVisibleDecls(II, DeclIDs);
695 II->setIsFromAST();
696 return II;
700 } // end anonymous namespace
702 /// \brief The on-disk hash table used to contain information about
703 /// all of the identifiers in the program.
704 typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
705 ASTIdentifierLookupTable;
707 void ASTReader::Error(const char *Msg) {
708 Diag(diag::err_fe_pch_malformed) << Msg;
711 /// \brief Tell the AST listener about the predefines buffers in the chain.
712 bool ASTReader::CheckPredefinesBuffers() {
713 if (Listener)
714 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
715 ActualOriginalFileName,
716 SuggestedPredefines);
717 return false;
720 //===----------------------------------------------------------------------===//
721 // Source Manager Deserialization
722 //===----------------------------------------------------------------------===//
724 /// \brief Read the line table in the source manager block.
725 /// \returns true if ther was an error.
726 bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
727 unsigned Idx = 0;
728 LineTableInfo &LineTable = SourceMgr.getLineTable();
730 // Parse the file names
731 std::map<int, int> FileIDs;
732 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
733 // Extract the file name
734 unsigned FilenameLen = Record[Idx++];
735 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
736 Idx += FilenameLen;
737 MaybeAddSystemRootToFilename(Filename);
738 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
739 Filename.size());
742 // Parse the line entries
743 std::vector<LineEntry> Entries;
744 while (Idx < Record.size()) {
745 int FID = Record[Idx++];
747 // Extract the line entries
748 unsigned NumEntries = Record[Idx++];
749 assert(NumEntries && "Numentries is 00000");
750 Entries.clear();
751 Entries.reserve(NumEntries);
752 for (unsigned I = 0; I != NumEntries; ++I) {
753 unsigned FileOffset = Record[Idx++];
754 unsigned LineNo = Record[Idx++];
755 int FilenameID = FileIDs[Record[Idx++]];
756 SrcMgr::CharacteristicKind FileKind
757 = (SrcMgr::CharacteristicKind)Record[Idx++];
758 unsigned IncludeOffset = Record[Idx++];
759 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
760 FileKind, IncludeOffset));
762 LineTable.AddEntry(FID, Entries);
765 return false;
768 namespace {
770 class ASTStatData {
771 public:
772 const bool hasStat;
773 const ino_t ino;
774 const dev_t dev;
775 const mode_t mode;
776 const time_t mtime;
777 const off_t size;
779 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
780 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
782 ASTStatData()
783 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
786 class ASTStatLookupTrait {
787 public:
788 typedef const char *external_key_type;
789 typedef const char *internal_key_type;
791 typedef ASTStatData data_type;
793 static unsigned ComputeHash(const char *path) {
794 return llvm::HashString(path);
797 static internal_key_type GetInternalKey(const char *path) { return path; }
799 static bool EqualKey(internal_key_type a, internal_key_type b) {
800 return strcmp(a, b) == 0;
803 static std::pair<unsigned, unsigned>
804 ReadKeyDataLength(const unsigned char*& d) {
805 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
806 unsigned DataLen = (unsigned) *d++;
807 return std::make_pair(KeyLen + 1, DataLen);
810 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
811 return (const char *)d;
814 static data_type ReadData(const internal_key_type, const unsigned char *d,
815 unsigned /*DataLen*/) {
816 using namespace clang::io;
818 if (*d++ == 1)
819 return data_type();
821 ino_t ino = (ino_t) ReadUnalignedLE32(d);
822 dev_t dev = (dev_t) ReadUnalignedLE32(d);
823 mode_t mode = (mode_t) ReadUnalignedLE16(d);
824 time_t mtime = (time_t) ReadUnalignedLE64(d);
825 off_t size = (off_t) ReadUnalignedLE64(d);
826 return data_type(ino, dev, mode, mtime, size);
830 /// \brief stat() cache for precompiled headers.
832 /// This cache is very similar to the stat cache used by pretokenized
833 /// headers.
834 class ASTStatCache : public StatSysCallCache {
835 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
836 CacheTy *Cache;
838 unsigned &NumStatHits, &NumStatMisses;
839 public:
840 ASTStatCache(const unsigned char *Buckets,
841 const unsigned char *Base,
842 unsigned &NumStatHits,
843 unsigned &NumStatMisses)
844 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
845 Cache = CacheTy::Create(Buckets, Base);
848 ~ASTStatCache() { delete Cache; }
850 int stat(const char *path, struct stat *buf) {
851 // Do the lookup for the file's data in the AST file.
852 CacheTy::iterator I = Cache->find(path);
854 // If we don't get a hit in the AST file just forward to 'stat'.
855 if (I == Cache->end()) {
856 ++NumStatMisses;
857 return StatSysCallCache::stat(path, buf);
860 ++NumStatHits;
861 ASTStatData Data = *I;
863 if (!Data.hasStat)
864 return 1;
866 buf->st_ino = Data.ino;
867 buf->st_dev = Data.dev;
868 buf->st_mtime = Data.mtime;
869 buf->st_mode = Data.mode;
870 buf->st_size = Data.size;
871 return 0;
874 } // end anonymous namespace
877 /// \brief Read a source manager block
878 ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
879 using namespace SrcMgr;
881 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
883 // Set the source-location entry cursor to the current position in
884 // the stream. This cursor will be used to read the contents of the
885 // source manager block initially, and then lazily read
886 // source-location entries as needed.
887 SLocEntryCursor = F.Stream;
889 // The stream itself is going to skip over the source manager block.
890 if (F.Stream.SkipBlock()) {
891 Error("malformed block record in AST file");
892 return Failure;
895 // Enter the source manager block.
896 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
897 Error("malformed source manager block record in AST file");
898 return Failure;
901 RecordData Record;
902 while (true) {
903 unsigned Code = SLocEntryCursor.ReadCode();
904 if (Code == llvm::bitc::END_BLOCK) {
905 if (SLocEntryCursor.ReadBlockEnd()) {
906 Error("error at end of Source Manager block in AST file");
907 return Failure;
909 return Success;
912 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
913 // No known subblocks, always skip them.
914 SLocEntryCursor.ReadSubBlockID();
915 if (SLocEntryCursor.SkipBlock()) {
916 Error("malformed block record in AST file");
917 return Failure;
919 continue;
922 if (Code == llvm::bitc::DEFINE_ABBREV) {
923 SLocEntryCursor.ReadAbbrevRecord();
924 continue;
927 // Read a record.
928 const char *BlobStart;
929 unsigned BlobLen;
930 Record.clear();
931 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
932 default: // Default behavior: ignore.
933 break;
935 case SM_LINE_TABLE:
936 if (ParseLineTable(Record))
937 return Failure;
938 break;
940 case SM_SLOC_FILE_ENTRY:
941 case SM_SLOC_BUFFER_ENTRY:
942 case SM_SLOC_INSTANTIATION_ENTRY:
943 // Once we hit one of the source location entries, we're done.
944 return Success;
949 /// \brief Get a cursor that's correctly positioned for reading the source
950 /// location entry with the given ID.
951 llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
952 assert(ID != 0 && ID <= TotalNumSLocEntries &&
953 "SLocCursorForID should only be called for real IDs.");
955 ID -= 1;
956 PerFileData *F = 0;
957 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
958 F = Chain[N - I - 1];
959 if (ID < F->LocalNumSLocEntries)
960 break;
961 ID -= F->LocalNumSLocEntries;
963 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
965 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
966 return F->SLocEntryCursor;
969 /// \brief Read in the source location entry with the given ID.
970 ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
971 if (ID == 0)
972 return Success;
974 if (ID > TotalNumSLocEntries) {
975 Error("source location entry ID out-of-range for AST file");
976 return Failure;
979 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
981 ++NumSLocEntriesRead;
982 unsigned Code = SLocEntryCursor.ReadCode();
983 if (Code == llvm::bitc::END_BLOCK ||
984 Code == llvm::bitc::ENTER_SUBBLOCK ||
985 Code == llvm::bitc::DEFINE_ABBREV) {
986 Error("incorrectly-formatted source location entry in AST file");
987 return Failure;
990 RecordData Record;
991 const char *BlobStart;
992 unsigned BlobLen;
993 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
994 default:
995 Error("incorrectly-formatted source location entry in AST file");
996 return Failure;
998 case SM_SLOC_FILE_ENTRY: {
999 std::string Filename(BlobStart, BlobStart + BlobLen);
1000 MaybeAddSystemRootToFilename(Filename);
1001 const FileEntry *File = FileMgr.getFile(Filename);
1002 if (File == 0) {
1003 std::string ErrorStr = "could not find file '";
1004 ErrorStr += Filename;
1005 ErrorStr += "' referenced by AST file";
1006 Error(ErrorStr.c_str());
1007 return Failure;
1010 if (Record.size() < 10) {
1011 Error("source location entry is incorrect");
1012 return Failure;
1015 if (!DisableValidation &&
1016 ((off_t)Record[4] != File->getSize()
1017 #if !defined(LLVM_ON_WIN32)
1018 // In our regression testing, the Windows file system seems to
1019 // have inconsistent modification times that sometimes
1020 // erroneously trigger this error-handling path.
1021 || (time_t)Record[5] != File->getModificationTime()
1022 #endif
1023 )) {
1024 Diag(diag::err_fe_pch_file_modified)
1025 << Filename;
1026 return Failure;
1029 FileID FID = SourceMgr.createFileID(File,
1030 SourceLocation::getFromRawEncoding(Record[1]),
1031 (SrcMgr::CharacteristicKind)Record[2],
1032 ID, Record[0]);
1033 if (Record[3])
1034 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1035 .setHasLineDirectives();
1037 // Reconstruct header-search information for this file.
1038 HeaderFileInfo HFI;
1039 HFI.isImport = Record[6];
1040 HFI.DirInfo = Record[7];
1041 HFI.NumIncludes = Record[8];
1042 HFI.ControllingMacroID = Record[9];
1043 if (Listener)
1044 Listener->ReadHeaderFileInfo(HFI, File->getUID());
1045 break;
1048 case SM_SLOC_BUFFER_ENTRY: {
1049 const char *Name = BlobStart;
1050 unsigned Offset = Record[0];
1051 unsigned Code = SLocEntryCursor.ReadCode();
1052 Record.clear();
1053 unsigned RecCode
1054 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1056 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1057 Error("AST record has invalid code");
1058 return Failure;
1061 llvm::MemoryBuffer *Buffer
1062 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1063 Name);
1064 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
1066 if (strcmp(Name, "<built-in>") == 0) {
1067 PCHPredefinesBlock Block = {
1068 BufferID,
1069 llvm::StringRef(BlobStart, BlobLen - 1)
1071 PCHPredefinesBuffers.push_back(Block);
1074 break;
1077 case SM_SLOC_INSTANTIATION_ENTRY: {
1078 SourceLocation SpellingLoc
1079 = SourceLocation::getFromRawEncoding(Record[1]);
1080 SourceMgr.createInstantiationLoc(SpellingLoc,
1081 SourceLocation::getFromRawEncoding(Record[2]),
1082 SourceLocation::getFromRawEncoding(Record[3]),
1083 Record[4],
1085 Record[0]);
1086 break;
1090 return Success;
1093 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1094 /// specified cursor. Read the abbreviations that are at the top of the block
1095 /// and then leave the cursor pointing into the block.
1096 bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
1097 unsigned BlockID) {
1098 if (Cursor.EnterSubBlock(BlockID)) {
1099 Error("malformed block record in AST file");
1100 return Failure;
1103 while (true) {
1104 unsigned Code = Cursor.ReadCode();
1106 // We expect all abbrevs to be at the start of the block.
1107 if (Code != llvm::bitc::DEFINE_ABBREV)
1108 return false;
1109 Cursor.ReadAbbrevRecord();
1113 void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
1114 assert(PP && "Forgot to set Preprocessor ?");
1116 // Keep track of where we are in the stream, then jump back there
1117 // after reading this macro.
1118 SavedStreamPosition SavedPosition(Stream);
1120 Stream.JumpToBit(Offset);
1121 RecordData Record;
1122 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1123 MacroInfo *Macro = 0;
1125 while (true) {
1126 unsigned Code = Stream.ReadCode();
1127 switch (Code) {
1128 case llvm::bitc::END_BLOCK:
1129 return;
1131 case llvm::bitc::ENTER_SUBBLOCK:
1132 // No known subblocks, always skip them.
1133 Stream.ReadSubBlockID();
1134 if (Stream.SkipBlock()) {
1135 Error("malformed block record in AST file");
1136 return;
1138 continue;
1140 case llvm::bitc::DEFINE_ABBREV:
1141 Stream.ReadAbbrevRecord();
1142 continue;
1143 default: break;
1146 // Read a record.
1147 Record.clear();
1148 PreprocessorRecordTypes RecType =
1149 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1150 switch (RecType) {
1151 case PP_MACRO_OBJECT_LIKE:
1152 case PP_MACRO_FUNCTION_LIKE: {
1153 // If we already have a macro, that means that we've hit the end
1154 // of the definition of the macro we were looking for. We're
1155 // done.
1156 if (Macro)
1157 return;
1159 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1160 if (II == 0) {
1161 Error("macro must have a name in AST file");
1162 return;
1164 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1165 bool isUsed = Record[2];
1167 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
1168 MI->setIsUsed(isUsed);
1169 MI->setIsFromAST();
1171 unsigned NextIndex = 3;
1172 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1173 // Decode function-like macro info.
1174 bool isC99VarArgs = Record[3];
1175 bool isGNUVarArgs = Record[4];
1176 MacroArgs.clear();
1177 unsigned NumArgs = Record[5];
1178 NextIndex = 6 + NumArgs;
1179 for (unsigned i = 0; i != NumArgs; ++i)
1180 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1182 // Install function-like macro info.
1183 MI->setIsFunctionLike();
1184 if (isC99VarArgs) MI->setIsC99Varargs();
1185 if (isGNUVarArgs) MI->setIsGNUVarargs();
1186 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1187 PP->getPreprocessorAllocator());
1190 // Finally, install the macro.
1191 PP->setMacroInfo(II, MI);
1193 // Remember that we saw this macro last so that we add the tokens that
1194 // form its body to it.
1195 Macro = MI;
1197 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1198 // We have a macro definition. Load it now.
1199 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1200 getMacroDefinition(Record[NextIndex]));
1203 ++NumMacrosRead;
1204 break;
1207 case PP_TOKEN: {
1208 // If we see a TOKEN before a PP_MACRO_*, then the file is
1209 // erroneous, just pretend we didn't see this.
1210 if (Macro == 0) break;
1212 Token Tok;
1213 Tok.startToken();
1214 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1215 Tok.setLength(Record[1]);
1216 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1217 Tok.setIdentifierInfo(II);
1218 Tok.setKind((tok::TokenKind)Record[3]);
1219 Tok.setFlag((Token::TokenFlags)Record[4]);
1220 Macro->AddTokenToBody(Tok);
1221 break;
1224 case PP_MACRO_INSTANTIATION: {
1225 // If we already have a macro, that means that we've hit the end
1226 // of the definition of the macro we were looking for. We're
1227 // done.
1228 if (Macro)
1229 return;
1231 if (!PP->getPreprocessingRecord()) {
1232 Error("missing preprocessing record in AST file");
1233 return;
1236 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1237 if (PPRec.getPreprocessedEntity(Record[0]))
1238 return;
1240 MacroInstantiation *MI
1241 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1242 SourceRange(
1243 SourceLocation::getFromRawEncoding(Record[1]),
1244 SourceLocation::getFromRawEncoding(Record[2])),
1245 getMacroDefinition(Record[4]));
1246 PPRec.SetPreallocatedEntity(Record[0], MI);
1247 return;
1250 case PP_MACRO_DEFINITION: {
1251 // If we already have a macro, that means that we've hit the end
1252 // of the definition of the macro we were looking for. We're
1253 // done.
1254 if (Macro)
1255 return;
1257 if (!PP->getPreprocessingRecord()) {
1258 Error("missing preprocessing record in AST file");
1259 return;
1262 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1263 if (PPRec.getPreprocessedEntity(Record[0]))
1264 return;
1266 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1267 Error("out-of-bounds macro definition record");
1268 return;
1271 MacroDefinition *MD
1272 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1273 SourceLocation::getFromRawEncoding(Record[5]),
1274 SourceRange(
1275 SourceLocation::getFromRawEncoding(Record[2]),
1276 SourceLocation::getFromRawEncoding(Record[3])));
1277 PPRec.SetPreallocatedEntity(Record[0], MD);
1278 MacroDefinitionsLoaded[Record[1]] = MD;
1279 return;
1285 void ASTReader::ReadDefinedMacros() {
1286 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1287 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
1289 // If there was no preprocessor block, skip this file.
1290 if (!MacroCursor.getBitStreamReader())
1291 continue;
1293 llvm::BitstreamCursor Cursor = MacroCursor;
1294 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
1295 Error("malformed preprocessor block record in AST file");
1296 return;
1299 RecordData Record;
1300 while (true) {
1301 unsigned Code = Cursor.ReadCode();
1302 if (Code == llvm::bitc::END_BLOCK) {
1303 if (Cursor.ReadBlockEnd()) {
1304 Error("error at end of preprocessor block in AST file");
1305 return;
1307 break;
1310 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1311 // No known subblocks, always skip them.
1312 Cursor.ReadSubBlockID();
1313 if (Cursor.SkipBlock()) {
1314 Error("malformed block record in AST file");
1315 return;
1317 continue;
1320 if (Code == llvm::bitc::DEFINE_ABBREV) {
1321 Cursor.ReadAbbrevRecord();
1322 continue;
1325 // Read a record.
1326 const char *BlobStart;
1327 unsigned BlobLen;
1328 Record.clear();
1329 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1330 default: // Default behavior: ignore.
1331 break;
1333 case PP_MACRO_OBJECT_LIKE:
1334 case PP_MACRO_FUNCTION_LIKE:
1335 DecodeIdentifierInfo(Record[0]);
1336 break;
1338 case PP_TOKEN:
1339 // Ignore tokens.
1340 break;
1342 case PP_MACRO_INSTANTIATION:
1343 case PP_MACRO_DEFINITION:
1344 // Read the macro record.
1345 ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1346 break;
1352 MacroDefinition *ASTReader::getMacroDefinition(IdentID ID) {
1353 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1354 return 0;
1356 if (!MacroDefinitionsLoaded[ID]) {
1357 unsigned Index = ID;
1358 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1359 PerFileData &F = *Chain[N - I - 1];
1360 if (Index < F.LocalNumMacroDefinitions) {
1361 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1362 break;
1364 Index -= F.LocalNumMacroDefinitions;
1366 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1369 return MacroDefinitionsLoaded[ID];
1372 /// \brief If we are loading a relocatable PCH file, and the filename is
1373 /// not an absolute path, add the system root to the beginning of the file
1374 /// name.
1375 void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1376 // If this is not a relocatable PCH file, there's nothing to do.
1377 if (!RelocatablePCH)
1378 return;
1380 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
1381 return;
1383 if (isysroot == 0) {
1384 // If no system root was given, default to '/'
1385 Filename.insert(Filename.begin(), '/');
1386 return;
1389 unsigned Length = strlen(isysroot);
1390 if (isysroot[Length - 1] != '/')
1391 Filename.insert(Filename.begin(), '/');
1393 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1396 ASTReader::ASTReadResult
1397 ASTReader::ReadASTBlock(PerFileData &F) {
1398 llvm::BitstreamCursor &Stream = F.Stream;
1400 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1401 Error("malformed block record in AST file");
1402 return Failure;
1405 // Read all of the records and blocks for the ASt file.
1406 RecordData Record;
1407 bool First = true;
1408 while (!Stream.AtEndOfStream()) {
1409 unsigned Code = Stream.ReadCode();
1410 if (Code == llvm::bitc::END_BLOCK) {
1411 if (Stream.ReadBlockEnd()) {
1412 Error("error at end of module block in AST file");
1413 return Failure;
1416 return Success;
1419 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1420 switch (Stream.ReadSubBlockID()) {
1421 case DECLTYPES_BLOCK_ID:
1422 // We lazily load the decls block, but we want to set up the
1423 // DeclsCursor cursor to point into it. Clone our current bitcode
1424 // cursor to it, enter the block and read the abbrevs in that block.
1425 // With the main cursor, we just skip over it.
1426 F.DeclsCursor = Stream;
1427 if (Stream.SkipBlock() || // Skip with the main cursor.
1428 // Read the abbrevs.
1429 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1430 Error("malformed block record in AST file");
1431 return Failure;
1433 break;
1435 case PREPROCESSOR_BLOCK_ID:
1436 F.MacroCursor = Stream;
1437 if (PP)
1438 PP->setExternalSource(this);
1440 if (Stream.SkipBlock()) {
1441 Error("malformed block record in AST file");
1442 return Failure;
1444 break;
1446 case SOURCE_MANAGER_BLOCK_ID:
1447 switch (ReadSourceManagerBlock(F)) {
1448 case Success:
1449 break;
1451 case Failure:
1452 Error("malformed source manager block in AST file");
1453 return Failure;
1455 case IgnorePCH:
1456 return IgnorePCH;
1458 break;
1460 First = false;
1461 continue;
1464 if (Code == llvm::bitc::DEFINE_ABBREV) {
1465 Stream.ReadAbbrevRecord();
1466 continue;
1469 // Read and process a record.
1470 Record.clear();
1471 const char *BlobStart = 0;
1472 unsigned BlobLen = 0;
1473 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
1474 &BlobStart, &BlobLen)) {
1475 default: // Default behavior: ignore.
1476 break;
1478 case METADATA: {
1479 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1480 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1481 : diag::warn_pch_version_too_new);
1482 return IgnorePCH;
1485 RelocatablePCH = Record[4];
1486 if (Listener) {
1487 std::string TargetTriple(BlobStart, BlobLen);
1488 if (Listener->ReadTargetTriple(TargetTriple))
1489 return IgnorePCH;
1491 break;
1494 case CHAINED_METADATA: {
1495 if (!First) {
1496 Error("CHAINED_METADATA is not first record in block");
1497 return Failure;
1499 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1500 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1501 : diag::warn_pch_version_too_new);
1502 return IgnorePCH;
1505 // Load the chained file.
1506 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
1507 case Failure: return Failure;
1508 // If we have to ignore the dependency, we'll have to ignore this too.
1509 case IgnorePCH: return IgnorePCH;
1510 case Success: break;
1512 break;
1515 case TYPE_OFFSET:
1516 if (F.LocalNumTypes != 0) {
1517 Error("duplicate TYPE_OFFSET record in AST file");
1518 return Failure;
1520 F.TypeOffsets = (const uint32_t *)BlobStart;
1521 F.LocalNumTypes = Record[0];
1522 break;
1524 case DECL_OFFSET:
1525 if (F.LocalNumDecls != 0) {
1526 Error("duplicate DECL_OFFSET record in AST file");
1527 return Failure;
1529 F.DeclOffsets = (const uint32_t *)BlobStart;
1530 F.LocalNumDecls = Record[0];
1531 break;
1533 case TU_UPDATE_LEXICAL: {
1534 DeclContextInfo Info = {
1535 /* No visible information */ 0, 0,
1536 reinterpret_cast<const DeclID *>(BlobStart),
1537 BlobLen / sizeof(DeclID)
1539 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1540 break;
1543 case REDECLS_UPDATE_LATEST: {
1544 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1545 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
1546 DeclID First = Record[i], Latest = Record[i+1];
1547 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1548 Latest > FirstLatestDeclIDs[First]) &&
1549 "The new latest is supposed to come after the previous latest");
1550 FirstLatestDeclIDs[First] = Latest;
1552 break;
1555 case LANGUAGE_OPTIONS:
1556 if (ParseLanguageOptions(Record) && !DisableValidation)
1557 return IgnorePCH;
1558 break;
1560 case IDENTIFIER_TABLE:
1561 F.IdentifierTableData = BlobStart;
1562 if (Record[0]) {
1563 F.IdentifierLookupTable
1564 = ASTIdentifierLookupTable::Create(
1565 (const unsigned char *)F.IdentifierTableData + Record[0],
1566 (const unsigned char *)F.IdentifierTableData,
1567 ASTIdentifierLookupTrait(*this, F.Stream));
1568 if (PP)
1569 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1571 break;
1573 case IDENTIFIER_OFFSET:
1574 if (F.LocalNumIdentifiers != 0) {
1575 Error("duplicate IDENTIFIER_OFFSET record in AST file");
1576 return Failure;
1578 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1579 F.LocalNumIdentifiers = Record[0];
1580 break;
1582 case EXTERNAL_DEFINITIONS:
1583 // Optimization for the first block.
1584 if (ExternalDefinitions.empty())
1585 ExternalDefinitions.swap(Record);
1586 else
1587 ExternalDefinitions.insert(ExternalDefinitions.end(),
1588 Record.begin(), Record.end());
1589 break;
1591 case SPECIAL_TYPES:
1592 // Optimization for the first block
1593 if (SpecialTypes.empty())
1594 SpecialTypes.swap(Record);
1595 else
1596 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
1597 break;
1599 case STATISTICS:
1600 TotalNumStatements += Record[0];
1601 TotalNumMacros += Record[1];
1602 TotalLexicalDeclContexts += Record[2];
1603 TotalVisibleDeclContexts += Record[3];
1604 break;
1606 case TENTATIVE_DEFINITIONS:
1607 // Optimization for the first block.
1608 if (TentativeDefinitions.empty())
1609 TentativeDefinitions.swap(Record);
1610 else
1611 TentativeDefinitions.insert(TentativeDefinitions.end(),
1612 Record.begin(), Record.end());
1613 break;
1615 case UNUSED_FILESCOPED_DECLS:
1616 // Optimization for the first block.
1617 if (UnusedFileScopedDecls.empty())
1618 UnusedFileScopedDecls.swap(Record);
1619 else
1620 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1621 Record.begin(), Record.end());
1622 break;
1624 case WEAK_UNDECLARED_IDENTIFIERS:
1625 // Later blocks overwrite earlier ones.
1626 WeakUndeclaredIdentifiers.swap(Record);
1627 break;
1629 case LOCALLY_SCOPED_EXTERNAL_DECLS:
1630 // Optimization for the first block.
1631 if (LocallyScopedExternalDecls.empty())
1632 LocallyScopedExternalDecls.swap(Record);
1633 else
1634 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1635 Record.begin(), Record.end());
1636 break;
1638 case SELECTOR_OFFSETS:
1639 F.SelectorOffsets = (const uint32_t *)BlobStart;
1640 F.LocalNumSelectors = Record[0];
1641 break;
1643 case METHOD_POOL:
1644 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
1645 if (Record[0])
1646 F.SelectorLookupTable
1647 = ASTSelectorLookupTable::Create(
1648 F.SelectorLookupTableData + Record[0],
1649 F.SelectorLookupTableData,
1650 ASTSelectorLookupTrait(*this));
1651 TotalNumMethodPoolEntries += Record[1];
1652 break;
1654 case REFERENCED_SELECTOR_POOL: {
1655 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1656 Record.begin(), Record.end());
1657 break;
1660 case PP_COUNTER_VALUE:
1661 if (!Record.empty() && Listener)
1662 Listener->ReadCounter(Record[0]);
1663 break;
1665 case SOURCE_LOCATION_OFFSETS:
1666 F.SLocOffsets = (const uint32_t *)BlobStart;
1667 F.LocalNumSLocEntries = Record[0];
1668 // We cannot delay this until the entire chain is loaded, because then
1669 // source location preloads would also have to be delayed.
1670 // FIXME: Is there a reason not to do that?
1671 TotalNumSLocEntries += F.LocalNumSLocEntries;
1672 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
1673 break;
1675 case SOURCE_LOCATION_PRELOADS:
1676 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
1677 ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
1678 if (Result != Success)
1679 return Result;
1681 break;
1683 case STAT_CACHE: {
1684 ASTStatCache *MyStatCache =
1685 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
1686 (const unsigned char *)BlobStart,
1687 NumStatHits, NumStatMisses);
1688 FileMgr.addStatCache(MyStatCache);
1689 F.StatCache = MyStatCache;
1690 break;
1693 case EXT_VECTOR_DECLS:
1694 // Optimization for the first block.
1695 if (ExtVectorDecls.empty())
1696 ExtVectorDecls.swap(Record);
1697 else
1698 ExtVectorDecls.insert(ExtVectorDecls.end(),
1699 Record.begin(), Record.end());
1700 break;
1702 case VTABLE_USES:
1703 // Later tables overwrite earlier ones.
1704 VTableUses.swap(Record);
1705 break;
1707 case DYNAMIC_CLASSES:
1708 // Optimization for the first block.
1709 if (DynamicClasses.empty())
1710 DynamicClasses.swap(Record);
1711 else
1712 DynamicClasses.insert(DynamicClasses.end(),
1713 Record.begin(), Record.end());
1714 break;
1716 case PENDING_IMPLICIT_INSTANTIATIONS:
1717 // Optimization for the first block.
1718 if (PendingImplicitInstantiations.empty())
1719 PendingImplicitInstantiations.swap(Record);
1720 else
1721 PendingImplicitInstantiations.insert(
1722 PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1723 break;
1725 case SEMA_DECL_REFS:
1726 // Later tables overwrite earlier ones.
1727 SemaDeclRefs.swap(Record);
1728 break;
1730 case ORIGINAL_FILE_NAME:
1731 // The primary AST will be the last to get here, so it will be the one
1732 // that's used.
1733 ActualOriginalFileName.assign(BlobStart, BlobLen);
1734 OriginalFileName = ActualOriginalFileName;
1735 MaybeAddSystemRootToFilename(OriginalFileName);
1736 break;
1738 case VERSION_CONTROL_BRANCH_REVISION: {
1739 const std::string &CurBranch = getClangFullRepositoryVersion();
1740 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1741 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1742 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1743 return IgnorePCH;
1745 break;
1748 case MACRO_DEFINITION_OFFSETS:
1749 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1750 F.NumPreallocatedPreprocessingEntities = Record[0];
1751 F.LocalNumMacroDefinitions = Record[1];
1752 break;
1754 case DECL_REPLACEMENTS: {
1755 if (Record.size() % 2 != 0) {
1756 Error("invalid DECL_REPLACEMENTS block in AST file");
1757 return Failure;
1759 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
1760 ReplacedDecls[static_cast<DeclID>(Record[I])] =
1761 std::make_pair(&F, Record[I+1]);
1762 break;
1765 First = false;
1767 Error("premature end of bitstream in AST file");
1768 return Failure;
1771 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1772 switch(ReadASTCore(FileName)) {
1773 case Failure: return Failure;
1774 case IgnorePCH: return IgnorePCH;
1775 case Success: break;
1778 // Here comes stuff that we only do once the entire chain is loaded.
1780 // Allocate space for loaded identifiers, decls and types.
1781 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
1782 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
1783 TotalNumSelectors = 0;
1784 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1785 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
1786 TotalNumTypes += Chain[I]->LocalNumTypes;
1787 TotalNumDecls += Chain[I]->LocalNumDecls;
1788 TotalNumPreallocatedPreprocessingEntities +=
1789 Chain[I]->NumPreallocatedPreprocessingEntities;
1790 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
1791 TotalNumSelectors += Chain[I]->LocalNumSelectors;
1793 IdentifiersLoaded.resize(TotalNumIdentifiers);
1794 TypesLoaded.resize(TotalNumTypes);
1795 DeclsLoaded.resize(TotalNumDecls);
1796 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
1797 if (PP) {
1798 if (TotalNumIdentifiers > 0)
1799 PP->getHeaderSearchInfo().SetExternalLookup(this);
1800 if (TotalNumPreallocatedPreprocessingEntities > 0) {
1801 if (!PP->getPreprocessingRecord())
1802 PP->createPreprocessingRecord();
1803 PP->getPreprocessingRecord()->SetExternalSource(*this,
1804 TotalNumPreallocatedPreprocessingEntities);
1807 SelectorsLoaded.resize(TotalNumSelectors);
1809 // Check the predefines buffers.
1810 if (!DisableValidation && CheckPredefinesBuffers())
1811 return IgnorePCH;
1813 if (PP) {
1814 // Initialization of keywords and pragmas occurs before the
1815 // AST file is read, so there may be some identifiers that were
1816 // loaded into the IdentifierTable before we intercepted the
1817 // creation of identifiers. Iterate through the list of known
1818 // identifiers and determine whether we have to establish
1819 // preprocessor definitions or top-level identifier declaration
1820 // chains for those identifiers.
1822 // We copy the IdentifierInfo pointers to a small vector first,
1823 // since de-serializing declarations or macro definitions can add
1824 // new entries into the identifier table, invalidating the
1825 // iterators.
1826 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1827 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1828 IdEnd = PP->getIdentifierTable().end();
1829 Id != IdEnd; ++Id)
1830 Identifiers.push_back(Id->second);
1831 // We need to search the tables in all files.
1832 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
1833 ASTIdentifierLookupTable *IdTable
1834 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
1835 // Not all AST files necessarily have identifier tables, only the useful
1836 // ones.
1837 if (!IdTable)
1838 continue;
1839 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1840 IdentifierInfo *II = Identifiers[I];
1841 // Look in the on-disk hash tables for an entry for this identifier
1842 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
1843 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
1844 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1845 if (Pos == IdTable->end())
1846 continue;
1848 // Dereferencing the iterator has the effect of populating the
1849 // IdentifierInfo node with the various declarations it needs.
1850 (void)*Pos;
1855 if (Context)
1856 InitializeContext(*Context);
1858 return Success;
1861 ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
1862 Chain.push_back(new PerFileData());
1863 PerFileData &F = *Chain.back();
1865 // Set the AST file name.
1866 F.FileName = FileName;
1868 // Open the AST file.
1870 // FIXME: This shouldn't be here, we should just take a raw_ostream.
1871 std::string ErrStr;
1872 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
1873 if (!F.Buffer) {
1874 Error(ErrStr.c_str());
1875 return IgnorePCH;
1878 // Initialize the stream
1879 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
1880 (const unsigned char *)F.Buffer->getBufferEnd());
1881 llvm::BitstreamCursor &Stream = F.Stream;
1882 Stream.init(F.StreamFile);
1883 F.SizeInBits = F.Buffer->getBufferSize() * 8;
1885 // Sniff for the signature.
1886 if (Stream.Read(8) != 'C' ||
1887 Stream.Read(8) != 'P' ||
1888 Stream.Read(8) != 'C' ||
1889 Stream.Read(8) != 'H') {
1890 Diag(diag::err_not_a_pch_file) << FileName;
1891 return Failure;
1894 while (!Stream.AtEndOfStream()) {
1895 unsigned Code = Stream.ReadCode();
1897 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1898 Error("invalid record at top-level of AST file");
1899 return Failure;
1902 unsigned BlockID = Stream.ReadSubBlockID();
1904 // We only know the AST subblock ID.
1905 switch (BlockID) {
1906 case llvm::bitc::BLOCKINFO_BLOCK_ID:
1907 if (Stream.ReadBlockInfoBlock()) {
1908 Error("malformed BlockInfoBlock in AST file");
1909 return Failure;
1911 break;
1912 case AST_BLOCK_ID:
1913 switch (ReadASTBlock(F)) {
1914 case Success:
1915 break;
1917 case Failure:
1918 return Failure;
1920 case IgnorePCH:
1921 // FIXME: We could consider reading through to the end of this
1922 // AST block, skipping subblocks, to see if there are other
1923 // AST blocks elsewhere.
1925 // Clear out any preallocated source location entries, so that
1926 // the source manager does not try to resolve them later.
1927 SourceMgr.ClearPreallocatedSLocEntries();
1929 // Remove the stat cache.
1930 if (F.StatCache)
1931 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
1933 return IgnorePCH;
1935 break;
1936 default:
1937 if (Stream.SkipBlock()) {
1938 Error("malformed block record in AST file");
1939 return Failure;
1941 break;
1945 return Success;
1948 void ASTReader::setPreprocessor(Preprocessor &pp) {
1949 PP = &pp;
1951 unsigned TotalNum = 0;
1952 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
1953 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
1954 if (TotalNum) {
1955 if (!PP->getPreprocessingRecord())
1956 PP->createPreprocessingRecord();
1957 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
1961 void ASTReader::InitializeContext(ASTContext &Ctx) {
1962 Context = &Ctx;
1963 assert(Context && "Passed null context!");
1965 assert(PP && "Forgot to set Preprocessor ?");
1966 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1967 PP->getHeaderSearchInfo().SetExternalLookup(this);
1968 PP->setExternalSource(this);
1970 // Load the translation unit declaration
1971 GetTranslationUnitDecl();
1973 // Load the special types.
1974 Context->setBuiltinVaListType(
1975 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
1976 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
1977 Context->setObjCIdType(GetType(Id));
1978 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
1979 Context->setObjCSelType(GetType(Sel));
1980 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
1981 Context->setObjCProtoType(GetType(Proto));
1982 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
1983 Context->setObjCClassType(GetType(Class));
1985 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
1986 Context->setCFConstantStringType(GetType(String));
1987 if (unsigned FastEnum
1988 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
1989 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
1990 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
1991 QualType FileType = GetType(File);
1992 if (FileType.isNull()) {
1993 Error("FILE type is NULL");
1994 return;
1996 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
1997 Context->setFILEDecl(Typedef->getDecl());
1998 else {
1999 const TagType *Tag = FileType->getAs<TagType>();
2000 if (!Tag) {
2001 Error("Invalid FILE type in AST file");
2002 return;
2004 Context->setFILEDecl(Tag->getDecl());
2007 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2008 QualType Jmp_bufType = GetType(Jmp_buf);
2009 if (Jmp_bufType.isNull()) {
2010 Error("jmp_bug type is NULL");
2011 return;
2013 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2014 Context->setjmp_bufDecl(Typedef->getDecl());
2015 else {
2016 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2017 if (!Tag) {
2018 Error("Invalid jmp_buf type in AST file");
2019 return;
2021 Context->setjmp_bufDecl(Tag->getDecl());
2024 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2025 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2026 if (Sigjmp_bufType.isNull()) {
2027 Error("sigjmp_buf type is NULL");
2028 return;
2030 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2031 Context->setsigjmp_bufDecl(Typedef->getDecl());
2032 else {
2033 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2034 assert(Tag && "Invalid sigjmp_buf type in AST file");
2035 Context->setsigjmp_bufDecl(Tag->getDecl());
2038 if (unsigned ObjCIdRedef
2039 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
2040 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2041 if (unsigned ObjCClassRedef
2042 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
2043 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2044 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
2045 Context->setBlockDescriptorType(GetType(String));
2046 if (unsigned String
2047 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
2048 Context->setBlockDescriptorExtendedType(GetType(String));
2049 if (unsigned ObjCSelRedef
2050 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
2051 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2052 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
2053 Context->setNSConstantStringType(GetType(String));
2055 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
2056 Context->setInt128Installed();
2059 /// \brief Retrieve the name of the original source file name
2060 /// directly from the AST file, without actually loading the AST
2061 /// file.
2062 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
2063 Diagnostic &Diags) {
2064 // Open the AST file.
2065 std::string ErrStr;
2066 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
2067 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
2068 if (!Buffer) {
2069 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
2070 return std::string();
2073 // Initialize the stream
2074 llvm::BitstreamReader StreamFile;
2075 llvm::BitstreamCursor Stream;
2076 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2077 (const unsigned char *)Buffer->getBufferEnd());
2078 Stream.init(StreamFile);
2080 // Sniff for the signature.
2081 if (Stream.Read(8) != 'C' ||
2082 Stream.Read(8) != 'P' ||
2083 Stream.Read(8) != 'C' ||
2084 Stream.Read(8) != 'H') {
2085 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
2086 return std::string();
2089 RecordData Record;
2090 while (!Stream.AtEndOfStream()) {
2091 unsigned Code = Stream.ReadCode();
2093 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2094 unsigned BlockID = Stream.ReadSubBlockID();
2096 // We only know the AST subblock ID.
2097 switch (BlockID) {
2098 case AST_BLOCK_ID:
2099 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2100 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2101 return std::string();
2103 break;
2105 default:
2106 if (Stream.SkipBlock()) {
2107 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2108 return std::string();
2110 break;
2112 continue;
2115 if (Code == llvm::bitc::END_BLOCK) {
2116 if (Stream.ReadBlockEnd()) {
2117 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
2118 return std::string();
2120 continue;
2123 if (Code == llvm::bitc::DEFINE_ABBREV) {
2124 Stream.ReadAbbrevRecord();
2125 continue;
2128 Record.clear();
2129 const char *BlobStart = 0;
2130 unsigned BlobLen = 0;
2131 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
2132 == ORIGINAL_FILE_NAME)
2133 return std::string(BlobStart, BlobLen);
2136 return std::string();
2139 /// \brief Parse the record that corresponds to a LangOptions data
2140 /// structure.
2142 /// This routine parses the language options from the AST file and then gives
2143 /// them to the AST listener if one is set.
2145 /// \returns true if the listener deems the file unacceptable, false otherwise.
2146 bool ASTReader::ParseLanguageOptions(
2147 const llvm::SmallVectorImpl<uint64_t> &Record) {
2148 if (Listener) {
2149 LangOptions LangOpts;
2151 #define PARSE_LANGOPT(Option) \
2152 LangOpts.Option = Record[Idx]; \
2153 ++Idx
2155 unsigned Idx = 0;
2156 PARSE_LANGOPT(Trigraphs);
2157 PARSE_LANGOPT(BCPLComment);
2158 PARSE_LANGOPT(DollarIdents);
2159 PARSE_LANGOPT(AsmPreprocessor);
2160 PARSE_LANGOPT(GNUMode);
2161 PARSE_LANGOPT(GNUKeywords);
2162 PARSE_LANGOPT(ImplicitInt);
2163 PARSE_LANGOPT(Digraphs);
2164 PARSE_LANGOPT(HexFloats);
2165 PARSE_LANGOPT(C99);
2166 PARSE_LANGOPT(Microsoft);
2167 PARSE_LANGOPT(CPlusPlus);
2168 PARSE_LANGOPT(CPlusPlus0x);
2169 PARSE_LANGOPT(CXXOperatorNames);
2170 PARSE_LANGOPT(ObjC1);
2171 PARSE_LANGOPT(ObjC2);
2172 PARSE_LANGOPT(ObjCNonFragileABI);
2173 PARSE_LANGOPT(ObjCNonFragileABI2);
2174 PARSE_LANGOPT(NoConstantCFStrings);
2175 PARSE_LANGOPT(PascalStrings);
2176 PARSE_LANGOPT(WritableStrings);
2177 PARSE_LANGOPT(LaxVectorConversions);
2178 PARSE_LANGOPT(AltiVec);
2179 PARSE_LANGOPT(Exceptions);
2180 PARSE_LANGOPT(SjLjExceptions);
2181 PARSE_LANGOPT(NeXTRuntime);
2182 PARSE_LANGOPT(Freestanding);
2183 PARSE_LANGOPT(NoBuiltin);
2184 PARSE_LANGOPT(ThreadsafeStatics);
2185 PARSE_LANGOPT(POSIXThreads);
2186 PARSE_LANGOPT(Blocks);
2187 PARSE_LANGOPT(EmitAllDecls);
2188 PARSE_LANGOPT(MathErrno);
2189 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2190 Record[Idx++]);
2191 PARSE_LANGOPT(HeinousExtensions);
2192 PARSE_LANGOPT(Optimize);
2193 PARSE_LANGOPT(OptimizeSize);
2194 PARSE_LANGOPT(Static);
2195 PARSE_LANGOPT(PICLevel);
2196 PARSE_LANGOPT(GNUInline);
2197 PARSE_LANGOPT(NoInline);
2198 PARSE_LANGOPT(AccessControl);
2199 PARSE_LANGOPT(CharIsSigned);
2200 PARSE_LANGOPT(ShortWChar);
2201 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2202 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
2203 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
2204 Record[Idx++]);
2205 PARSE_LANGOPT(InstantiationDepth);
2206 PARSE_LANGOPT(OpenCL);
2207 PARSE_LANGOPT(CatchUndefined);
2208 // FIXME: Missing ElideConstructors?!
2209 #undef PARSE_LANGOPT
2211 return Listener->ReadLanguageOptions(LangOpts);
2214 return false;
2217 void ASTReader::ReadPreprocessedEntities() {
2218 ReadDefinedMacros();
2221 /// \brief Get the correct cursor and offset for loading a type.
2222 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
2223 PerFileData *F = 0;
2224 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2225 F = Chain[N - I - 1];
2226 if (Index < F->LocalNumTypes)
2227 break;
2228 Index -= F->LocalNumTypes;
2230 assert(F && F->LocalNumTypes > Index && "Broken chain");
2231 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
2234 /// \brief Read and return the type with the given index..
2236 /// The index is the type ID, shifted and minus the number of predefs. This
2237 /// routine actually reads the record corresponding to the type at the given
2238 /// location. It is a helper routine for GetType, which deals with reading type
2239 /// IDs.
2240 QualType ASTReader::ReadTypeRecord(unsigned Index) {
2241 RecordLocation Loc = TypeCursorForIndex(Index);
2242 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
2244 // Keep track of where we are in the stream, then jump back there
2245 // after reading this type.
2246 SavedStreamPosition SavedPosition(DeclsCursor);
2248 ReadingKindTracker ReadingKind(Read_Type, *this);
2250 // Note that we are loading a type record.
2251 Deserializing AType(this);
2253 DeclsCursor.JumpToBit(Loc.second);
2254 RecordData Record;
2255 unsigned Code = DeclsCursor.ReadCode();
2256 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2257 case TYPE_EXT_QUAL: {
2258 if (Record.size() != 2) {
2259 Error("Incorrect encoding of extended qualifier type");
2260 return QualType();
2262 QualType Base = GetType(Record[0]);
2263 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2264 return Context->getQualifiedType(Base, Quals);
2267 case TYPE_COMPLEX: {
2268 if (Record.size() != 1) {
2269 Error("Incorrect encoding of complex type");
2270 return QualType();
2272 QualType ElemType = GetType(Record[0]);
2273 return Context->getComplexType(ElemType);
2276 case TYPE_POINTER: {
2277 if (Record.size() != 1) {
2278 Error("Incorrect encoding of pointer type");
2279 return QualType();
2281 QualType PointeeType = GetType(Record[0]);
2282 return Context->getPointerType(PointeeType);
2285 case TYPE_BLOCK_POINTER: {
2286 if (Record.size() != 1) {
2287 Error("Incorrect encoding of block pointer type");
2288 return QualType();
2290 QualType PointeeType = GetType(Record[0]);
2291 return Context->getBlockPointerType(PointeeType);
2294 case TYPE_LVALUE_REFERENCE: {
2295 if (Record.size() != 1) {
2296 Error("Incorrect encoding of lvalue reference type");
2297 return QualType();
2299 QualType PointeeType = GetType(Record[0]);
2300 return Context->getLValueReferenceType(PointeeType);
2303 case TYPE_RVALUE_REFERENCE: {
2304 if (Record.size() != 1) {
2305 Error("Incorrect encoding of rvalue reference type");
2306 return QualType();
2308 QualType PointeeType = GetType(Record[0]);
2309 return Context->getRValueReferenceType(PointeeType);
2312 case TYPE_MEMBER_POINTER: {
2313 if (Record.size() != 2) {
2314 Error("Incorrect encoding of member pointer type");
2315 return QualType();
2317 QualType PointeeType = GetType(Record[0]);
2318 QualType ClassType = GetType(Record[1]);
2319 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
2322 case TYPE_CONSTANT_ARRAY: {
2323 QualType ElementType = GetType(Record[0]);
2324 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2325 unsigned IndexTypeQuals = Record[2];
2326 unsigned Idx = 3;
2327 llvm::APInt Size = ReadAPInt(Record, Idx);
2328 return Context->getConstantArrayType(ElementType, Size,
2329 ASM, IndexTypeQuals);
2332 case TYPE_INCOMPLETE_ARRAY: {
2333 QualType ElementType = GetType(Record[0]);
2334 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2335 unsigned IndexTypeQuals = Record[2];
2336 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2339 case TYPE_VARIABLE_ARRAY: {
2340 QualType ElementType = GetType(Record[0]);
2341 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2342 unsigned IndexTypeQuals = Record[2];
2343 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2344 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
2345 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
2346 ASM, IndexTypeQuals,
2347 SourceRange(LBLoc, RBLoc));
2350 case TYPE_VECTOR: {
2351 if (Record.size() != 3) {
2352 Error("incorrect encoding of vector type in AST file");
2353 return QualType();
2356 QualType ElementType = GetType(Record[0]);
2357 unsigned NumElements = Record[1];
2358 unsigned AltiVecSpec = Record[2];
2359 return Context->getVectorType(ElementType, NumElements,
2360 (VectorType::AltiVecSpecific)AltiVecSpec);
2363 case TYPE_EXT_VECTOR: {
2364 if (Record.size() != 3) {
2365 Error("incorrect encoding of extended vector type in AST file");
2366 return QualType();
2369 QualType ElementType = GetType(Record[0]);
2370 unsigned NumElements = Record[1];
2371 return Context->getExtVectorType(ElementType, NumElements);
2374 case TYPE_FUNCTION_NO_PROTO: {
2375 if (Record.size() != 4) {
2376 Error("incorrect encoding of no-proto function type");
2377 return QualType();
2379 QualType ResultType = GetType(Record[0]);
2380 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
2381 return Context->getFunctionNoProtoType(ResultType, Info);
2384 case TYPE_FUNCTION_PROTO: {
2385 QualType ResultType = GetType(Record[0]);
2386 bool NoReturn = Record[1];
2387 unsigned RegParm = Record[2];
2388 CallingConv CallConv = (CallingConv)Record[3];
2389 unsigned Idx = 4;
2390 unsigned NumParams = Record[Idx++];
2391 llvm::SmallVector<QualType, 16> ParamTypes;
2392 for (unsigned I = 0; I != NumParams; ++I)
2393 ParamTypes.push_back(GetType(Record[Idx++]));
2394 bool isVariadic = Record[Idx++];
2395 unsigned Quals = Record[Idx++];
2396 bool hasExceptionSpec = Record[Idx++];
2397 bool hasAnyExceptionSpec = Record[Idx++];
2398 unsigned NumExceptions = Record[Idx++];
2399 llvm::SmallVector<QualType, 2> Exceptions;
2400 for (unsigned I = 0; I != NumExceptions; ++I)
2401 Exceptions.push_back(GetType(Record[Idx++]));
2402 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
2403 isVariadic, Quals, hasExceptionSpec,
2404 hasAnyExceptionSpec, NumExceptions,
2405 Exceptions.data(),
2406 FunctionType::ExtInfo(NoReturn, RegParm,
2407 CallConv));
2410 case TYPE_UNRESOLVED_USING:
2411 return Context->getTypeDeclType(
2412 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2414 case TYPE_TYPEDEF: {
2415 if (Record.size() != 2) {
2416 Error("incorrect encoding of typedef type");
2417 return QualType();
2419 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2420 QualType Canonical = GetType(Record[1]);
2421 return Context->getTypedefType(Decl, Canonical);
2424 case TYPE_TYPEOF_EXPR:
2425 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
2427 case TYPE_TYPEOF: {
2428 if (Record.size() != 1) {
2429 Error("incorrect encoding of typeof(type) in AST file");
2430 return QualType();
2432 QualType UnderlyingType = GetType(Record[0]);
2433 return Context->getTypeOfType(UnderlyingType);
2436 case TYPE_DECLTYPE:
2437 return Context->getDecltypeType(ReadExpr(DeclsCursor));
2439 case TYPE_RECORD: {
2440 if (Record.size() != 2) {
2441 Error("incorrect encoding of record type");
2442 return QualType();
2444 bool IsDependent = Record[0];
2445 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2446 T->Dependent = IsDependent;
2447 return T;
2450 case TYPE_ENUM: {
2451 if (Record.size() != 2) {
2452 Error("incorrect encoding of enum type");
2453 return QualType();
2455 bool IsDependent = Record[0];
2456 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2457 T->Dependent = IsDependent;
2458 return T;
2461 case TYPE_ELABORATED: {
2462 unsigned Idx = 0;
2463 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2464 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2465 QualType NamedType = GetType(Record[Idx++]);
2466 return Context->getElaboratedType(Keyword, NNS, NamedType);
2469 case TYPE_OBJC_INTERFACE: {
2470 unsigned Idx = 0;
2471 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2472 return Context->getObjCInterfaceType(ItfD);
2475 case TYPE_OBJC_OBJECT: {
2476 unsigned Idx = 0;
2477 QualType Base = GetType(Record[Idx++]);
2478 unsigned NumProtos = Record[Idx++];
2479 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2480 for (unsigned I = 0; I != NumProtos; ++I)
2481 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2482 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
2485 case TYPE_OBJC_OBJECT_POINTER: {
2486 unsigned Idx = 0;
2487 QualType Pointee = GetType(Record[Idx++]);
2488 return Context->getObjCObjectPointerType(Pointee);
2491 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
2492 unsigned Idx = 0;
2493 QualType Parm = GetType(Record[Idx++]);
2494 QualType Replacement = GetType(Record[Idx++]);
2495 return
2496 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2497 Replacement);
2500 case TYPE_INJECTED_CLASS_NAME: {
2501 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2502 QualType TST = GetType(Record[1]); // probably derivable
2503 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
2504 // for AST reading, too much interdependencies.
2505 return
2506 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
2509 case TYPE_TEMPLATE_TYPE_PARM: {
2510 unsigned Idx = 0;
2511 unsigned Depth = Record[Idx++];
2512 unsigned Index = Record[Idx++];
2513 bool Pack = Record[Idx++];
2514 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2515 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2518 case TYPE_DEPENDENT_NAME: {
2519 unsigned Idx = 0;
2520 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2521 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2522 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2523 QualType Canon = GetType(Record[Idx++]);
2524 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
2527 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
2528 unsigned Idx = 0;
2529 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2530 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2531 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2532 unsigned NumArgs = Record[Idx++];
2533 llvm::SmallVector<TemplateArgument, 8> Args;
2534 Args.reserve(NumArgs);
2535 while (NumArgs--)
2536 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
2537 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2538 Args.size(), Args.data());
2541 case TYPE_DEPENDENT_SIZED_ARRAY: {
2542 unsigned Idx = 0;
2544 // ArrayType
2545 QualType ElementType = GetType(Record[Idx++]);
2546 ArrayType::ArraySizeModifier ASM
2547 = (ArrayType::ArraySizeModifier)Record[Idx++];
2548 unsigned IndexTypeQuals = Record[Idx++];
2550 // DependentSizedArrayType
2551 Expr *NumElts = ReadExpr(DeclsCursor);
2552 SourceRange Brackets = ReadSourceRange(Record, Idx);
2554 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2555 IndexTypeQuals, Brackets);
2558 case TYPE_TEMPLATE_SPECIALIZATION: {
2559 unsigned Idx = 0;
2560 bool IsDependent = Record[Idx++];
2561 TemplateName Name = ReadTemplateName(Record, Idx);
2562 llvm::SmallVector<TemplateArgument, 8> Args;
2563 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
2564 QualType Canon = GetType(Record[Idx++]);
2565 QualType T;
2566 if (Canon.isNull())
2567 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2568 Args.size());
2569 else
2570 T = Context->getTemplateSpecializationType(Name, Args.data(),
2571 Args.size(), Canon);
2572 T->Dependent = IsDependent;
2573 return T;
2576 // Suppress a GCC warning
2577 return QualType();
2580 namespace {
2582 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
2583 ASTReader &Reader;
2584 llvm::BitstreamCursor &DeclsCursor;
2585 const ASTReader::RecordData &Record;
2586 unsigned &Idx;
2588 public:
2589 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2590 const ASTReader::RecordData &Record, unsigned &Idx)
2591 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
2593 // We want compile-time assurance that we've enumerated all of
2594 // these, so unfortunately we have to declare them first, then
2595 // define them out-of-line.
2596 #define ABSTRACT_TYPELOC(CLASS, PARENT)
2597 #define TYPELOC(CLASS, PARENT) \
2598 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
2599 #include "clang/AST/TypeLocNodes.def"
2601 void VisitFunctionTypeLoc(FunctionTypeLoc);
2602 void VisitArrayTypeLoc(ArrayTypeLoc);
2607 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
2608 // nothing to do
2610 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2611 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2612 if (TL.needsExtraLocalData()) {
2613 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2614 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2615 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2616 TL.setModeAttr(Record[Idx++]);
2619 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2620 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2622 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2623 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2625 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2626 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2628 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2629 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2631 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2632 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2634 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2635 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2637 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2638 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2639 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2640 if (Record[Idx++])
2641 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
2642 else
2643 TL.setSizeExpr(0);
2645 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2646 VisitArrayTypeLoc(TL);
2648 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2649 VisitArrayTypeLoc(TL);
2651 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2652 VisitArrayTypeLoc(TL);
2654 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2655 DependentSizedArrayTypeLoc TL) {
2656 VisitArrayTypeLoc(TL);
2658 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2659 DependentSizedExtVectorTypeLoc TL) {
2660 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2662 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2663 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2665 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2666 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2668 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2669 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2670 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2671 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
2672 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
2675 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2676 VisitFunctionTypeLoc(TL);
2678 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2679 VisitFunctionTypeLoc(TL);
2681 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2682 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2684 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2685 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2687 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2688 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2689 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2690 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2692 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2693 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2694 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2695 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2696 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
2698 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2699 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2701 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2702 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2704 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2705 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2707 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2708 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2710 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2711 SubstTemplateTypeParmTypeLoc TL) {
2712 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2714 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2715 TemplateSpecializationTypeLoc TL) {
2716 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2717 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2718 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2719 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2720 TL.setArgLocInfo(i,
2721 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
2722 DeclsCursor, Record, Idx));
2724 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2725 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2726 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2728 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2729 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2731 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2732 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2733 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2734 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2736 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2737 DependentTemplateSpecializationTypeLoc TL) {
2738 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2739 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2740 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2741 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2742 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2743 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2744 TL.setArgLocInfo(I,
2745 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
2746 DeclsCursor, Record, Idx));
2748 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2749 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2751 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2752 TL.setHasBaseTypeAsWritten(Record[Idx++]);
2753 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2754 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2755 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2756 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
2758 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2759 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2762 TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
2763 const RecordData &Record,
2764 unsigned &Idx) {
2765 QualType InfoTy = GetType(Record[Idx++]);
2766 if (InfoTy.isNull())
2767 return 0;
2769 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
2770 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
2771 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2772 TLR.Visit(TL);
2773 return TInfo;
2776 QualType ASTReader::GetType(TypeID ID) {
2777 unsigned FastQuals = ID & Qualifiers::FastMask;
2778 unsigned Index = ID >> Qualifiers::FastWidth;
2780 if (Index < NUM_PREDEF_TYPE_IDS) {
2781 QualType T;
2782 switch ((PredefinedTypeIDs)Index) {
2783 case PREDEF_TYPE_NULL_ID: return QualType();
2784 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2785 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
2787 case PREDEF_TYPE_CHAR_U_ID:
2788 case PREDEF_TYPE_CHAR_S_ID:
2789 // FIXME: Check that the signedness of CharTy is correct!
2790 T = Context->CharTy;
2791 break;
2793 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2794 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2795 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2796 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2797 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
2798 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
2799 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2800 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2801 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2802 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2803 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2804 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
2805 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
2806 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2807 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2808 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2809 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2810 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
2811 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
2812 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2813 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
2814 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2815 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
2816 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
2819 assert(!T.isNull() && "Unknown predefined type");
2820 return T.withFastQualifiers(FastQuals);
2823 Index -= NUM_PREDEF_TYPE_IDS;
2824 assert(Index < TypesLoaded.size() && "Type index out-of-range");
2825 if (TypesLoaded[Index].isNull()) {
2826 TypesLoaded[Index] = ReadTypeRecord(Index);
2827 TypesLoaded[Index]->setFromAST();
2828 if (DeserializationListener)
2829 DeserializationListener->TypeRead(ID >> Qualifiers::FastWidth,
2830 TypesLoaded[Index]);
2833 return TypesLoaded[Index].withFastQualifiers(FastQuals);
2836 TemplateArgumentLocInfo
2837 ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2838 llvm::BitstreamCursor &DeclsCursor,
2839 const RecordData &Record,
2840 unsigned &Index) {
2841 switch (Kind) {
2842 case TemplateArgument::Expression:
2843 return ReadExpr(DeclsCursor);
2844 case TemplateArgument::Type:
2845 return GetTypeSourceInfo(DeclsCursor, Record, Index);
2846 case TemplateArgument::Template: {
2847 SourceRange QualifierRange = ReadSourceRange(Record, Index);
2848 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
2849 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
2851 case TemplateArgument::Null:
2852 case TemplateArgument::Integral:
2853 case TemplateArgument::Declaration:
2854 case TemplateArgument::Pack:
2855 return TemplateArgumentLocInfo();
2857 llvm_unreachable("unexpected template argument loc");
2858 return TemplateArgumentLocInfo();
2861 TemplateArgumentLoc
2862 ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
2863 const RecordData &Record, unsigned &Index) {
2864 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
2866 if (Arg.getKind() == TemplateArgument::Expression) {
2867 if (Record[Index++]) // bool InfoHasSameExpr.
2868 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
2870 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
2871 DeclsCursor,
2872 Record, Index));
2875 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
2876 return GetDecl(ID);
2879 TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
2880 if (!DeclsLoaded[0]) {
2881 ReadDeclRecord(0, 0);
2882 if (DeserializationListener)
2883 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
2886 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
2889 Decl *ASTReader::GetDecl(DeclID ID) {
2890 if (ID == 0)
2891 return 0;
2893 if (ID > DeclsLoaded.size()) {
2894 Error("declaration ID out-of-range for AST file");
2895 return 0;
2898 unsigned Index = ID - 1;
2899 if (!DeclsLoaded[Index]) {
2900 ReadDeclRecord(Index, ID);
2901 if (DeserializationListener)
2902 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
2905 return DeclsLoaded[Index];
2908 /// \brief Resolve the offset of a statement into a statement.
2910 /// This operation will read a new statement from the external
2911 /// source each time it is called, and is meant to be used via a
2912 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
2913 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
2914 // Offset here is a global offset across the entire chain.
2915 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2916 PerFileData &F = *Chain[N - I - 1];
2917 if (Offset < F.SizeInBits) {
2918 // Since we know that this statement is part of a decl, make sure to use
2919 // the decl cursor to read it.
2920 F.DeclsCursor.JumpToBit(Offset);
2921 return ReadStmtFromStream(F.DeclsCursor);
2923 Offset -= F.SizeInBits;
2925 llvm_unreachable("Broken chain");
2928 bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
2929 llvm::SmallVectorImpl<Decl*> &Decls) {
2930 assert(DC->hasExternalLexicalStorage() &&
2931 "DeclContext has no lexical decls in storage");
2933 // There might be lexical decls in multiple parts of the chain, for the TU
2934 // at least.
2935 DeclContextInfos &Infos = DeclContextOffsets[DC];
2936 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2937 I != E; ++I) {
2938 // IDs can be 0 if this context doesn't contain declarations.
2939 if (!I->LexicalDecls)
2940 continue;
2942 // Load all of the declaration IDs
2943 for (const DeclID *ID = I->LexicalDecls,
2944 *IDE = ID + I->NumLexicalDecls;
2945 ID != IDE; ++ID)
2946 Decls.push_back(GetDecl(*ID));
2949 ++NumLexicalDeclContextsRead;
2950 return false;
2953 DeclContext::lookup_result
2954 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
2955 DeclarationName Name) {
2956 assert(DC->hasExternalVisibleStorage() &&
2957 "DeclContext has no visible decls in storage");
2959 llvm::SmallVector<VisibleDeclaration, 64> Decls;
2960 // There might be lexical decls in multiple parts of the chain, for the TU
2961 // and namespaces.
2962 DeclContextInfos &Infos = DeclContextOffsets[DC];
2963 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2964 I != E; ++I) {
2965 uint64_t Offset = I->OffsetToVisibleDecls;
2966 if (Offset == 0)
2967 continue;
2969 llvm::BitstreamCursor &DeclsCursor = *I->Stream;
2971 // Keep track of where we are in the stream, then jump back there
2972 // after reading this context.
2973 SavedStreamPosition SavedPosition(DeclsCursor);
2975 // Load the record containing all of the declarations visible in
2976 // this context.
2977 DeclsCursor.JumpToBit(Offset);
2978 RecordData Record;
2979 unsigned Code = DeclsCursor.ReadCode();
2980 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
2981 if (RecCode != DECL_CONTEXT_VISIBLE) {
2982 Error("Expected visible block");
2983 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2984 DeclContext::lookup_iterator());
2987 if (Record.empty())
2988 continue;
2990 unsigned Idx = 0;
2991 while (Idx < Record.size()) {
2992 Decls.push_back(VisibleDeclaration());
2993 Decls.back().Name = ReadDeclarationName(Record, Idx);
2995 unsigned Size = Record[Idx++];
2996 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
2997 LoadedDecls.reserve(Size);
2998 for (unsigned J = 0; J < Size; ++J)
2999 LoadedDecls.push_back(Record[Idx++]);
3003 ++NumVisibleDeclContextsRead;
3005 SetExternalVisibleDecls(DC, Decls);
3006 return const_cast<DeclContext*>(DC)->lookup(Name);
3009 void ASTReader::PassInterestingDeclsToConsumer() {
3010 assert(Consumer);
3011 while (!InterestingDecls.empty()) {
3012 DeclGroupRef DG(InterestingDecls.front());
3013 InterestingDecls.pop_front();
3014 Consumer->HandleInterestingDecl(DG);
3018 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
3019 this->Consumer = Consumer;
3021 if (!Consumer)
3022 return;
3024 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
3025 // Force deserialization of this decl, which will cause it to be queued for
3026 // passing to the consumer.
3027 GetDecl(ExternalDefinitions[I]);
3030 PassInterestingDeclsToConsumer();
3033 void ASTReader::PrintStats() {
3034 std::fprintf(stderr, "*** AST File Statistics:\n");
3036 unsigned NumTypesLoaded
3037 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
3038 QualType());
3039 unsigned NumDeclsLoaded
3040 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3041 (Decl *)0);
3042 unsigned NumIdentifiersLoaded
3043 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3044 IdentifiersLoaded.end(),
3045 (IdentifierInfo *)0);
3046 unsigned NumSelectorsLoaded
3047 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3048 SelectorsLoaded.end(),
3049 Selector());
3051 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3052 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
3053 if (TotalNumSLocEntries)
3054 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3055 NumSLocEntriesRead, TotalNumSLocEntries,
3056 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
3057 if (!TypesLoaded.empty())
3058 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
3059 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3060 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3061 if (!DeclsLoaded.empty())
3062 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
3063 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3064 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
3065 if (!IdentifiersLoaded.empty())
3066 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
3067 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3068 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
3069 if (!SelectorsLoaded.empty())
3070 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
3071 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3072 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
3073 if (TotalNumStatements)
3074 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3075 NumStatementsRead, TotalNumStatements,
3076 ((float)NumStatementsRead/TotalNumStatements * 100));
3077 if (TotalNumMacros)
3078 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3079 NumMacrosRead, TotalNumMacros,
3080 ((float)NumMacrosRead/TotalNumMacros * 100));
3081 if (TotalLexicalDeclContexts)
3082 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3083 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3084 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3085 * 100));
3086 if (TotalVisibleDeclContexts)
3087 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3088 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3089 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3090 * 100));
3091 if (TotalNumMethodPoolEntries) {
3092 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
3093 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3094 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
3095 * 100));
3096 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
3098 std::fprintf(stderr, "\n");
3101 void ASTReader::InitializeSema(Sema &S) {
3102 SemaObj = &S;
3103 S.ExternalSource = this;
3105 // Makes sure any declarations that were deserialized "too early"
3106 // still get added to the identifier's declaration chains.
3107 if (SemaObj->TUScope) {
3108 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3109 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
3110 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3113 PreloadedDecls.clear();
3115 // If there were any tentative definitions, deserialize them and add
3116 // them to Sema's list of tentative definitions.
3117 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3118 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
3119 SemaObj->TentativeDefinitions.push_back(Var);
3122 // If there were any unused file scoped decls, deserialize them and add to
3123 // Sema's list of unused file scoped decls.
3124 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3125 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3126 SemaObj->UnusedFileScopedDecls.push_back(D);
3129 // If there were any weak undeclared identifiers, deserialize them and add to
3130 // Sema's list of weak undeclared identifiers.
3131 if (!WeakUndeclaredIdentifiers.empty()) {
3132 unsigned Idx = 0;
3133 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3134 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3135 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3136 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3137 bool Used = WeakUndeclaredIdentifiers[Idx++];
3138 Sema::WeakInfo WI(AliasId, Loc);
3139 WI.setUsed(Used);
3140 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3144 // If there were any locally-scoped external declarations,
3145 // deserialize them and add them to Sema's table of locally-scoped
3146 // external declarations.
3147 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3148 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3149 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3152 // If there were any ext_vector type declarations, deserialize them
3153 // and add them to Sema's vector of such declarations.
3154 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3155 SemaObj->ExtVectorDecls.push_back(
3156 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
3158 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3159 // Can we cut them down before writing them ?
3161 // If there were any VTable uses, deserialize the information and add it
3162 // to Sema's vector and map of VTable uses.
3163 if (!VTableUses.empty()) {
3164 unsigned Idx = 0;
3165 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3166 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3167 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3168 bool DefinitionRequired = VTableUses[Idx++];
3169 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3170 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3174 // If there were any dynamic classes declarations, deserialize them
3175 // and add them to Sema's vector of such declarations.
3176 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3177 SemaObj->DynamicClasses.push_back(
3178 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
3180 // If there were any pending implicit instantiations, deserialize them
3181 // and add them to Sema's queue of such instantiations.
3182 assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3183 "Expected pairs of entries");
3184 for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3185 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3186 SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3187 SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3190 // Load the offsets of the declarations that Sema references.
3191 // They will be lazily deserialized when needed.
3192 if (!SemaDeclRefs.empty()) {
3193 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3194 SemaObj->StdNamespace = SemaDeclRefs[0];
3195 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3198 // If there are @selector references added them to its pool. This is for
3199 // implementation of -Wselector.
3200 if (!ReferencedSelectorsData.empty()) {
3201 unsigned int DataSize = ReferencedSelectorsData.size()-1;
3202 unsigned I = 0;
3203 while (I < DataSize) {
3204 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
3205 SourceLocation SelLoc =
3206 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
3207 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3212 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
3213 // Try to find this name within our on-disk hash tables. We start with the
3214 // most recent one, since that one contains the most up-to-date info.
3215 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3216 ASTIdentifierLookupTable *IdTable
3217 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
3218 if (!IdTable)
3219 continue;
3220 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
3221 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
3222 if (Pos == IdTable->end())
3223 continue;
3225 // Dereferencing the iterator has the effect of building the
3226 // IdentifierInfo node and populating it with the various
3227 // declarations it needs.
3228 return *Pos;
3230 return 0;
3233 std::pair<ObjCMethodList, ObjCMethodList>
3234 ASTReader::ReadMethodPool(Selector Sel) {
3235 // Find this selector in a hash table. We want to find the most recent entry.
3236 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3237 PerFileData &F = *Chain[I];
3238 if (!F.SelectorLookupTable)
3239 continue;
3241 ASTSelectorLookupTable *PoolTable
3242 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3243 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
3244 if (Pos != PoolTable->end()) {
3245 ++NumSelectorsRead;
3246 // FIXME: Not quite happy with the statistics here. We probably should
3247 // disable this tracking when called via LoadSelector.
3248 // Also, should entries without methods count as misses?
3249 ++NumMethodPoolEntriesRead;
3250 ASTSelectorLookupTrait::data_type Data = *Pos;
3251 if (DeserializationListener)
3252 DeserializationListener->SelectorRead(Data.ID, Sel);
3253 return std::make_pair(Data.Instance, Data.Factory);
3257 ++NumMethodPoolMisses;
3258 return std::pair<ObjCMethodList, ObjCMethodList>();
3261 void ASTReader::LoadSelector(Selector Sel) {
3262 // It would be complicated to avoid reading the methods anyway. So don't.
3263 ReadMethodPool(Sel);
3266 void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
3267 assert(ID && "Non-zero identifier ID required");
3268 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
3269 IdentifiersLoaded[ID - 1] = II;
3270 if (DeserializationListener)
3271 DeserializationListener->IdentifierRead(ID, II);
3274 /// \brief Set the globally-visible declarations associated with the given
3275 /// identifier.
3277 /// If the AST reader is currently in a state where the given declaration IDs
3278 /// cannot safely be resolved, they are queued until it is safe to resolve
3279 /// them.
3281 /// \param II an IdentifierInfo that refers to one or more globally-visible
3282 /// declarations.
3284 /// \param DeclIDs the set of declaration IDs with the name @p II that are
3285 /// visible at global scope.
3287 /// \param Nonrecursive should be true to indicate that the caller knows that
3288 /// this call is non-recursive, and therefore the globally-visible declarations
3289 /// will not be placed onto the pending queue.
3290 void
3291 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
3292 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3293 bool Nonrecursive) {
3294 if (NumCurrentElementsDeserializing && !Nonrecursive) {
3295 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3296 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3297 PII.II = II;
3298 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3299 PII.DeclIDs.push_back(DeclIDs[I]);
3300 return;
3303 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3304 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3305 if (SemaObj) {
3306 if (SemaObj->TUScope) {
3307 // Introduce this declaration into the translation-unit scope
3308 // and add it to the declaration chain for this identifier, so
3309 // that (unqualified) name lookup will find it.
3310 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
3311 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3313 } else {
3314 // Queue this declaration so that it will be added to the
3315 // translation unit scope and identifier's declaration chain
3316 // once a Sema object is known.
3317 PreloadedDecls.push_back(D);
3322 IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
3323 if (ID == 0)
3324 return 0;
3326 if (IdentifiersLoaded.empty()) {
3327 Error("no identifier table in AST file");
3328 return 0;
3331 assert(PP && "Forgot to set Preprocessor ?");
3332 ID -= 1;
3333 if (!IdentifiersLoaded[ID]) {
3334 unsigned Index = ID;
3335 const char *Str = 0;
3336 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3337 PerFileData *F = Chain[N - I - 1];
3338 if (Index < F->LocalNumIdentifiers) {
3339 uint32_t Offset = F->IdentifierOffsets[Index];
3340 Str = F->IdentifierTableData + Offset;
3341 break;
3343 Index -= F->LocalNumIdentifiers;
3345 assert(Str && "Broken Chain");
3347 // All of the strings in the AST file are preceded by a 16-bit length.
3348 // Extract that 16-bit length to avoid having to execute strlen().
3349 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3350 // unsigned integers. This is important to avoid integer overflow when
3351 // we cast them to 'unsigned'.
3352 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
3353 unsigned StrLen = (((unsigned) StrLenPtr[0])
3354 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
3355 IdentifiersLoaded[ID]
3356 = &PP->getIdentifierTable().get(Str, StrLen);
3357 if (DeserializationListener)
3358 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
3361 return IdentifiersLoaded[ID];
3364 void ASTReader::ReadSLocEntry(unsigned ID) {
3365 ReadSLocEntryRecord(ID);
3368 Selector ASTReader::DecodeSelector(unsigned ID) {
3369 if (ID == 0)
3370 return Selector();
3372 if (ID > SelectorsLoaded.size()) {
3373 Error("selector ID out of range in AST file");
3374 return Selector();
3377 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
3378 // Load this selector from the selector table.
3379 unsigned Idx = ID - 1;
3380 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3381 PerFileData &F = *Chain[N - I - 1];
3382 if (Idx < F.LocalNumSelectors) {
3383 ASTSelectorLookupTrait Trait(*this);
3384 SelectorsLoaded[ID - 1] =
3385 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3386 if (DeserializationListener)
3387 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3388 break;
3390 Idx -= F.LocalNumSelectors;
3394 return SelectorsLoaded[ID - 1];
3397 Selector ASTReader::GetExternalSelector(uint32_t ID) {
3398 return DecodeSelector(ID);
3401 uint32_t ASTReader::GetNumExternalSelectors() {
3402 // ID 0 (the null selector) is considered an external selector.
3403 return getTotalNumSelectors() + 1;
3406 DeclarationName
3407 ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
3408 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3409 switch (Kind) {
3410 case DeclarationName::Identifier:
3411 return DeclarationName(GetIdentifierInfo(Record, Idx));
3413 case DeclarationName::ObjCZeroArgSelector:
3414 case DeclarationName::ObjCOneArgSelector:
3415 case DeclarationName::ObjCMultiArgSelector:
3416 return DeclarationName(GetSelector(Record, Idx));
3418 case DeclarationName::CXXConstructorName:
3419 return Context->DeclarationNames.getCXXConstructorName(
3420 Context->getCanonicalType(GetType(Record[Idx++])));
3422 case DeclarationName::CXXDestructorName:
3423 return Context->DeclarationNames.getCXXDestructorName(
3424 Context->getCanonicalType(GetType(Record[Idx++])));
3426 case DeclarationName::CXXConversionFunctionName:
3427 return Context->DeclarationNames.getCXXConversionFunctionName(
3428 Context->getCanonicalType(GetType(Record[Idx++])));
3430 case DeclarationName::CXXOperatorName:
3431 return Context->DeclarationNames.getCXXOperatorName(
3432 (OverloadedOperatorKind)Record[Idx++]);
3434 case DeclarationName::CXXLiteralOperatorName:
3435 return Context->DeclarationNames.getCXXLiteralOperatorName(
3436 GetIdentifierInfo(Record, Idx));
3438 case DeclarationName::CXXUsingDirective:
3439 return DeclarationName::getUsingDirectiveName();
3442 // Required to silence GCC warning
3443 return DeclarationName();
3446 TemplateName
3447 ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
3448 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3449 switch (Kind) {
3450 case TemplateName::Template:
3451 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3453 case TemplateName::OverloadedTemplate: {
3454 unsigned size = Record[Idx++];
3455 UnresolvedSet<8> Decls;
3456 while (size--)
3457 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3459 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3462 case TemplateName::QualifiedTemplate: {
3463 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3464 bool hasTemplKeyword = Record[Idx++];
3465 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3466 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3469 case TemplateName::DependentTemplate: {
3470 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3471 if (Record[Idx++]) // isIdentifier
3472 return Context->getDependentTemplateName(NNS,
3473 GetIdentifierInfo(Record, Idx));
3474 return Context->getDependentTemplateName(NNS,
3475 (OverloadedOperatorKind)Record[Idx++]);
3479 assert(0 && "Unhandled template name kind!");
3480 return TemplateName();
3483 TemplateArgument
3484 ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
3485 const RecordData &Record, unsigned &Idx) {
3486 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3487 case TemplateArgument::Null:
3488 return TemplateArgument();
3489 case TemplateArgument::Type:
3490 return TemplateArgument(GetType(Record[Idx++]));
3491 case TemplateArgument::Declaration:
3492 return TemplateArgument(GetDecl(Record[Idx++]));
3493 case TemplateArgument::Integral: {
3494 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3495 QualType T = GetType(Record[Idx++]);
3496 return TemplateArgument(Value, T);
3498 case TemplateArgument::Template:
3499 return TemplateArgument(ReadTemplateName(Record, Idx));
3500 case TemplateArgument::Expression:
3501 return TemplateArgument(ReadExpr(DeclsCursor));
3502 case TemplateArgument::Pack: {
3503 unsigned NumArgs = Record[Idx++];
3504 llvm::SmallVector<TemplateArgument, 8> Args;
3505 Args.reserve(NumArgs);
3506 while (NumArgs--)
3507 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
3508 TemplateArgument TemplArg;
3509 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3510 return TemplArg;
3514 assert(0 && "Unhandled template argument kind!");
3515 return TemplateArgument();
3518 TemplateParameterList *
3519 ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
3520 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3521 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3522 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3524 unsigned NumParams = Record[Idx++];
3525 llvm::SmallVector<NamedDecl *, 16> Params;
3526 Params.reserve(NumParams);
3527 while (NumParams--)
3528 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3530 TemplateParameterList* TemplateParams =
3531 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3532 Params.data(), Params.size(), RAngleLoc);
3533 return TemplateParams;
3536 void
3537 ASTReader::
3538 ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
3539 llvm::BitstreamCursor &DeclsCursor,
3540 const RecordData &Record, unsigned &Idx) {
3541 unsigned NumTemplateArgs = Record[Idx++];
3542 TemplArgs.reserve(NumTemplateArgs);
3543 while (NumTemplateArgs--)
3544 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
3547 /// \brief Read a UnresolvedSet structure.
3548 void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
3549 const RecordData &Record, unsigned &Idx) {
3550 unsigned NumDecls = Record[Idx++];
3551 while (NumDecls--) {
3552 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3553 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3554 Set.addDecl(D, AS);
3558 CXXBaseSpecifier
3559 ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
3560 const RecordData &Record, unsigned &Idx) {
3561 bool isVirtual = static_cast<bool>(Record[Idx++]);
3562 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3563 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
3564 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
3565 SourceRange Range = ReadSourceRange(Record, Idx);
3566 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
3569 std::pair<CXXBaseOrMemberInitializer **, unsigned>
3570 ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
3571 const RecordData &Record,
3572 unsigned &Idx) {
3573 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3574 unsigned NumInitializers = Record[Idx++];
3575 if (NumInitializers) {
3576 ASTContext &C = *getContext();
3578 BaseOrMemberInitializers
3579 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3580 for (unsigned i=0; i != NumInitializers; ++i) {
3581 TypeSourceInfo *BaseClassInfo = 0;
3582 bool IsBaseVirtual = false;
3583 FieldDecl *Member = 0;
3585 bool IsBaseInitializer = Record[Idx++];
3586 if (IsBaseInitializer) {
3587 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3588 IsBaseVirtual = Record[Idx++];
3589 } else {
3590 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3592 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3593 Expr *Init = ReadExpr(Cursor);
3594 FieldDecl *AnonUnionMember
3595 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3596 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3597 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3598 bool IsWritten = Record[Idx++];
3599 unsigned SourceOrderOrNumArrayIndices;
3600 llvm::SmallVector<VarDecl *, 8> Indices;
3601 if (IsWritten) {
3602 SourceOrderOrNumArrayIndices = Record[Idx++];
3603 } else {
3604 SourceOrderOrNumArrayIndices = Record[Idx++];
3605 Indices.reserve(SourceOrderOrNumArrayIndices);
3606 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3607 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3610 CXXBaseOrMemberInitializer *BOMInit;
3611 if (IsBaseInitializer) {
3612 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3613 IsBaseVirtual, LParenLoc,
3614 Init, RParenLoc);
3615 } else if (IsWritten) {
3616 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3617 LParenLoc, Init, RParenLoc);
3618 } else {
3619 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3620 LParenLoc, Init, RParenLoc,
3621 Indices.data(),
3622 Indices.size());
3625 BOMInit->setAnonUnionMember(AnonUnionMember);
3626 BaseOrMemberInitializers[i] = BOMInit;
3630 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3633 NestedNameSpecifier *
3634 ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
3635 unsigned N = Record[Idx++];
3636 NestedNameSpecifier *NNS = 0, *Prev = 0;
3637 for (unsigned I = 0; I != N; ++I) {
3638 NestedNameSpecifier::SpecifierKind Kind
3639 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3640 switch (Kind) {
3641 case NestedNameSpecifier::Identifier: {
3642 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3643 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3644 break;
3647 case NestedNameSpecifier::Namespace: {
3648 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3649 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3650 break;
3653 case NestedNameSpecifier::TypeSpec:
3654 case NestedNameSpecifier::TypeSpecWithTemplate: {
3655 Type *T = GetType(Record[Idx++]).getTypePtr();
3656 bool Template = Record[Idx++];
3657 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3658 break;
3661 case NestedNameSpecifier::Global: {
3662 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3663 // No associated value, and there can't be a prefix.
3664 break;
3667 Prev = NNS;
3669 return NNS;
3672 SourceRange
3673 ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
3674 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3675 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3676 return SourceRange(beg, end);
3679 /// \brief Read an integral value
3680 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
3681 unsigned BitWidth = Record[Idx++];
3682 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3683 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3684 Idx += NumWords;
3685 return Result;
3688 /// \brief Read a signed integral value
3689 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
3690 bool isUnsigned = Record[Idx++];
3691 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3694 /// \brief Read a floating-point value
3695 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
3696 return llvm::APFloat(ReadAPInt(Record, Idx));
3699 // \brief Read a string
3700 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
3701 unsigned Len = Record[Idx++];
3702 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
3703 Idx += Len;
3704 return Result;
3707 CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
3708 unsigned &Idx) {
3709 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3710 return CXXTemporary::Create(*Context, Decl);
3713 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
3714 return Diag(SourceLocation(), DiagID);
3717 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
3718 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
3721 /// \brief Retrieve the identifier table associated with the
3722 /// preprocessor.
3723 IdentifierTable &ASTReader::getIdentifierTable() {
3724 assert(PP && "Forgot to set Preprocessor ?");
3725 return PP->getIdentifierTable();
3728 /// \brief Record that the given ID maps to the given switch-case
3729 /// statement.
3730 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3731 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3732 SwitchCaseStmts[ID] = SC;
3735 /// \brief Retrieve the switch-case statement with the given ID.
3736 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
3737 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3738 return SwitchCaseStmts[ID];
3741 /// \brief Record that the given label statement has been
3742 /// deserialized and has the given ID.
3743 void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3744 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3745 "Deserialized label twice");
3746 LabelStmts[ID] = S;
3748 // If we've already seen any goto statements that point to this
3749 // label, resolve them now.
3750 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3751 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3752 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3753 Goto->second->setLabel(S);
3754 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
3756 // If we've already seen any address-label statements that point to
3757 // this label, resolve them now.
3758 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3759 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3760 = UnresolvedAddrLabelExprs.equal_range(ID);
3761 for (AddrLabelIter AddrLabel = AddrLabels.first;
3762 AddrLabel != AddrLabels.second; ++AddrLabel)
3763 AddrLabel->second->setLabel(S);
3764 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
3767 /// \brief Set the label of the given statement to the label
3768 /// identified by ID.
3770 /// Depending on the order in which the label and other statements
3771 /// referencing that label occur, this operation may complete
3772 /// immediately (updating the statement) or it may queue the
3773 /// statement to be back-patched later.
3774 void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3775 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3776 if (Label != LabelStmts.end()) {
3777 // We've already seen this label, so set the label of the goto and
3778 // we're done.
3779 S->setLabel(Label->second);
3780 } else {
3781 // We haven't seen this label yet, so add this goto to the set of
3782 // unresolved goto statements.
3783 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3787 /// \brief Set the label of the given expression to the label
3788 /// identified by ID.
3790 /// Depending on the order in which the label and other statements
3791 /// referencing that label occur, this operation may complete
3792 /// immediately (updating the statement) or it may queue the
3793 /// statement to be back-patched later.
3794 void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3795 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3796 if (Label != LabelStmts.end()) {
3797 // We've already seen this label, so set the label of the
3798 // label-address expression and we're done.
3799 S->setLabel(Label->second);
3800 } else {
3801 // We haven't seen this label yet, so add this label-address
3802 // expression to the set of unresolved label-address expressions.
3803 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3807 void ASTReader::FinishedDeserializing() {
3808 assert(NumCurrentElementsDeserializing &&
3809 "FinishedDeserializing not paired with StartedDeserializing");
3810 if (NumCurrentElementsDeserializing == 1) {
3811 // If any identifiers with corresponding top-level declarations have
3812 // been loaded, load those declarations now.
3813 while (!PendingIdentifierInfos.empty()) {
3814 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
3815 PendingIdentifierInfos.front().DeclIDs, true);
3816 PendingIdentifierInfos.pop_front();
3819 // We are not in recursive loading, so it's safe to pass the "interesting"
3820 // decls to the consumer.
3821 if (Consumer)
3822 PassInterestingDeclsToConsumer();
3824 --NumCurrentElementsDeserializing;
3827 ASTReader::PerFileData::PerFileData()
3828 : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
3829 LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
3830 IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
3831 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
3832 NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
3833 SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
3836 ASTReader::PerFileData::~PerFileData() {
3837 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
3838 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);