blob: f1c0247b814a398d51dff341a7fc857b68786eb2 [file] [log] [blame]
Douglas Gregor2cf26342009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers 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 PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Douglas Gregor2cf26342009-04-09 22:27:44 +000014#include "clang/Frontend/PCHReader.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000015#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000016#include "clang/Frontend/Utils.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000017#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregorfdd01722009-04-14 00:24:19 +000018#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000031#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000032#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000034#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000035#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000036#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000037#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000038#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000039#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000040#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000041using namespace clang;
42
43//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000044// PCH reader validator implementation
45//===----------------------------------------------------------------------===//
46
47PCHReaderListener::~PCHReaderListener() {}
48
49bool
50PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
51 const LangOptions &PPLangOpts = PP.getLangOptions();
52#define PARSE_LANGOPT_BENIGN(Option)
53#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
54 if (PPLangOpts.Option != LangOpts.Option) { \
55 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
56 return true; \
57 }
58
59 PARSE_LANGOPT_BENIGN(Trigraphs);
60 PARSE_LANGOPT_BENIGN(BCPLComment);
61 PARSE_LANGOPT_BENIGN(DollarIdents);
62 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
63 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
64 PARSE_LANGOPT_BENIGN(ImplicitInt);
65 PARSE_LANGOPT_BENIGN(Digraphs);
66 PARSE_LANGOPT_BENIGN(HexFloats);
67 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
68 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
69 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
70 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
71 PARSE_LANGOPT_BENIGN(CXXOperatorName);
72 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
73 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
74 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000075 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000076 PARSE_LANGOPT_BENIGN(PascalStrings);
77 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000078 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000079 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000080 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000081 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000082 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
84 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
85 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000086 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000087 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000088 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000089 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
90 PARSE_LANGOPT_BENIGN(EmitAllDecls);
91 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
92 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
Mike Stump1eb44332009-09-09 15:08:12 +000093 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 diag::warn_pch_heinous_extensions);
95 // FIXME: Most of the options below are benign if the macro wasn't
96 // used. Unfortunately, this means that a PCH compiled without
97 // optimization can't be used with optimization turned on, even
98 // though the only thing that changes is whether __OPTIMIZE__ was
99 // defined... but if __OPTIMIZE__ never showed up in the header, it
100 // doesn't matter. We could consider making this some special kind
101 // of check.
102 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
103 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
104 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
105 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
106 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
107 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
108 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
109 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000110 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000111 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000113 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
114 return true;
115 }
116 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000117 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
118 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000119 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000120 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000121 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000122 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000123#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000124#undef PARSE_LANGOPT_BENIGN
125
126 return false;
127}
128
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000129bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
130 if (Triple == PP.getTargetInfo().getTriple().str())
131 return false;
132
133 Reader.Diag(diag::warn_pch_target_triple)
134 << Triple << PP.getTargetInfo().getTriple().str();
135 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000136}
137
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000138bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000139 FileID PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000140 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000141 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000142 // We are in the context of an implicit include, so the predefines buffer will
143 // have a #include entry for the PCH file itself (as normalized by the
144 // preprocessor initialization). Find it and skip over it in the checking
145 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000146 llvm::SmallString<256> PCHInclude;
147 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000148 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000149 PCHInclude += "\"\n";
150 std::pair<llvm::StringRef,llvm::StringRef> Split =
151 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
152 llvm::StringRef Left = Split.first, Right = Split.second;
153 assert(Left != PP.getPredefines() && "Missing PCH include entry!");
154
155 // If the predefines is equal to the joined left and right halves, we're done!
156 if (Left.size() + Right.size() == PCHPredef.size() &&
157 PCHPredef.startswith(Left) && PCHPredef.endswith(Right))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000158 return false;
159
160 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000162 // The predefines buffers are different. Determine what the differences are,
163 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000164 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
165 PCHPredef.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
166
167 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
168 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
169 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000170
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000171 // Sort both sets of predefined buffer lines, since we allow some extra
172 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000173 std::sort(CmdLineLines.begin(), CmdLineLines.end());
174 std::sort(PCHLines.begin(), PCHLines.end());
175
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000176 // Determine which predefines that were used to build the PCH file are missing
177 // from the command line.
178 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000179 std::set_difference(PCHLines.begin(), PCHLines.end(),
180 CmdLineLines.begin(), CmdLineLines.end(),
181 std::back_inserter(MissingPredefines));
182
183 bool MissingDefines = false;
184 bool ConflictingDefines = false;
185 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000186 llvm::StringRef Missing = MissingPredefines[I];
187 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000188 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
189 return true;
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000192 // This is a macro definition. Determine the name of the macro we're
193 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000194 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000195 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000196 = Missing.find_first_of("( \n\r", StartOfMacroName);
197 assert(EndOfMacroName != std::string::npos &&
198 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000199 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000200
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000201 // Determine whether this macro was given a different definition on the
202 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000203 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000204 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000205 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000206 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
207 MacroDefStart);
208 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000209 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000210 // Different macro; we're done.
211 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000212 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
215 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000216 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000217 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000218 (*ConflictPos)[MacroDefLen] != '(')
219 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000221 // We found a conflicting macro definition.
222 break;
223 }
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000225 if (ConflictPos != CmdLineLines.end()) {
226 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
227 << MacroName;
228
229 // Show the definition of this macro within the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000230 llvm::StringRef::size_type Offset = PCHPredef.find(Missing);
231 assert(Offset != llvm::StringRef::npos && "Unable to find macro!");
232 SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID)
233 .getFileLocWithOffset(Offset);
234 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000235
236 ConflictingDefines = true;
237 continue;
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000240 // If the macro doesn't conflict, then we'll just pick up the macro
241 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000242 if (ConflictingDefines)
243 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000245 if (!MissingDefines) {
246 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
247 MissingDefines = true;
248 }
249
250 // Show the definition of this macro within the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000251 llvm::StringRef::size_type Offset = PCHPredef.find(Missing);
252 assert(Offset != llvm::StringRef::npos && "Unable to find macro!");
253 SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000254 .getFileLocWithOffset(Offset);
255 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000258 if (ConflictingDefines)
259 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000261 // Determine what predefines were introduced based on command-line
262 // parameters that were not present when building the PCH
263 // file. Extra #defines are okay, so long as the identifiers being
264 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000265 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000266 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
267 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000268 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000269 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000270 llvm::StringRef &Extra = ExtraPredefines[I];
271 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000272 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
273 return true;
274 }
275
276 // This is an extra macro definition. Determine the name of the
277 // macro we're defining.
278 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000279 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000280 = Extra.find_first_of("( \n\r", StartOfMacroName);
281 assert(EndOfMacroName != std::string::npos &&
282 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000283 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000284
285 // Check whether this name was used somewhere in the PCH file. If
286 // so, defining it as a macro could change behavior, so we reject
287 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000288 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000289 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000290 return true;
291 }
292
293 // Add this definition to the suggested predefines buffer.
294 SuggestedPredefines += Extra;
295 SuggestedPredefines += '\n';
296 }
297
298 // If we get here, it's because the predefines buffer had compatible
299 // contents. Accept the PCH file.
300 return false;
301}
302
Douglas Gregor12fab312010-03-16 16:35:32 +0000303void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
304 unsigned ID) {
305 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
306 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000307}
308
309void PCHValidator::ReadCounter(unsigned Value) {
310 PP.setCounterValue(Value);
311}
312
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000313//===----------------------------------------------------------------------===//
Douglas Gregor668c1a42009-04-21 22:25:48 +0000314// PCH reader implementation
315//===----------------------------------------------------------------------===//
316
Mike Stump1eb44332009-09-09 15:08:12 +0000317PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context,
318 const char *isysroot)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 : Listener(new PCHValidator(PP, *this)), SourceMgr(PP.getSourceManager()),
320 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
Douglas Gregor52e71082009-10-16 18:18:30 +0000321 SemaObj(0), PP(&PP), Context(Context), StatCache(0), Consumer(0),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000322 IdentifierTableData(0), IdentifierLookupTable(0),
323 IdentifierOffsets(0),
324 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
325 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
Douglas Gregore650c8c2009-07-07 00:12:59 +0000326 TotalNumSelectors(0), Comments(0), NumComments(0), isysroot(isysroot),
Mike Stump1eb44332009-09-09 15:08:12 +0000327 NumStatHits(0), NumStatMisses(0),
328 NumSLocEntriesRead(0), NumStatementsRead(0),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000329 NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
Douglas Gregore650c8c2009-07-07 00:12:59 +0000330 NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0),
Mike Stump1eb44332009-09-09 15:08:12 +0000331 CurrentlyLoadingTypeOrDecl(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000332 RelocatablePCH = false;
333}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000334
335PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr,
Mike Stump1eb44332009-09-09 15:08:12 +0000336 Diagnostic &Diags, const char *isysroot)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 : SourceMgr(SourceMgr), FileMgr(FileMgr), Diags(Diags),
Douglas Gregor52e71082009-10-16 18:18:30 +0000338 SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0),
Chris Lattner4c6f9522009-04-27 05:14:47 +0000339 IdentifierTableData(0), IdentifierLookupTable(0),
340 IdentifierOffsets(0),
341 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
342 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
Douglas Gregore650c8c2009-07-07 00:12:59 +0000343 TotalNumSelectors(0), Comments(0), NumComments(0), isysroot(isysroot),
Mike Stump1eb44332009-09-09 15:08:12 +0000344 NumStatHits(0), NumStatMisses(0),
345 NumSLocEntriesRead(0), NumStatementsRead(0),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000346 NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
Douglas Gregord89275b2009-07-06 18:54:52 +0000347 NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0),
Mike Stump1eb44332009-09-09 15:08:12 +0000348 CurrentlyLoadingTypeOrDecl(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000349 RelocatablePCH = false;
350}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000351
352PCHReader::~PCHReader() {}
353
Chris Lattnerda930612009-04-27 05:58:23 +0000354Expr *PCHReader::ReadDeclExpr() {
355 return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor));
356}
357
358Expr *PCHReader::ReadTypeExpr() {
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000359 return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor));
Chris Lattner4c6f9522009-04-27 05:14:47 +0000360}
361
362
Douglas Gregor668c1a42009-04-21 22:25:48 +0000363namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +0000364class PCHMethodPoolLookupTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000365 PCHReader &Reader;
366
367public:
368 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
369
370 typedef Selector external_key_type;
371 typedef external_key_type internal_key_type;
372
373 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000375 static bool EqualKey(const internal_key_type& a,
376 const internal_key_type& b) {
377 return a == b;
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000380 static unsigned ComputeHash(Selector Sel) {
381 unsigned N = Sel.getNumArgs();
382 if (N == 0)
383 ++N;
384 unsigned R = 5381;
385 for (unsigned I = 0; I != N; ++I)
386 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +0000387 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000388 return R;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000391 // This hopefully will just get inlined and removed by the optimizer.
392 static const internal_key_type&
393 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000395 static std::pair<unsigned, unsigned>
396 ReadKeyDataLength(const unsigned char*& d) {
397 using namespace clang::io;
398 unsigned KeyLen = ReadUnalignedLE16(d);
399 unsigned DataLen = ReadUnalignedLE16(d);
400 return std::make_pair(KeyLen, DataLen);
401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Douglas Gregor83941df2009-04-25 17:48:32 +0000403 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000404 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000405 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000406 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000407 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000408 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
409 if (N == 0)
410 return SelTable.getNullarySelector(FirstII);
411 else if (N == 1)
412 return SelTable.getUnarySelector(FirstII);
413
414 llvm::SmallVector<IdentifierInfo *, 16> Args;
415 Args.push_back(FirstII);
416 for (unsigned I = 1; I != N; ++I)
417 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
418
Douglas Gregor75fdb232009-05-22 22:45:36 +0000419 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000422 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
423 using namespace clang::io;
424 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
425 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
426
427 data_type Result;
428
429 // Load instance methods
430 ObjCMethodList *Prev = 0;
431 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000432 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000433 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
434 if (!Result.first.Method) {
435 // This is the first method, which is the easy case.
436 Result.first.Method = Method;
437 Prev = &Result.first;
438 continue;
439 }
440
Ted Kremenek298ed872010-02-11 00:53:01 +0000441 ObjCMethodList *Mem =
442 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
443 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000444 Prev = Prev->Next;
445 }
446
447 // Load factory methods
448 Prev = 0;
449 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000450 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000451 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
452 if (!Result.second.Method) {
453 // This is the first method, which is the easy case.
454 Result.second.Method = Method;
455 Prev = &Result.second;
456 continue;
457 }
458
Ted Kremenek298ed872010-02-11 00:53:01 +0000459 ObjCMethodList *Mem =
460 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
461 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000462 Prev = Prev->Next;
463 }
464
465 return Result;
466 }
467};
Mike Stump1eb44332009-09-09 15:08:12 +0000468
469} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000470
471/// \brief The on-disk hash table used for the global method pool.
Mike Stump1eb44332009-09-09 15:08:12 +0000472typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000473 PCHMethodPoolLookupTable;
474
475namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +0000476class PCHIdentifierLookupTrait {
Douglas Gregor668c1a42009-04-21 22:25:48 +0000477 PCHReader &Reader;
478
479 // If we know the IdentifierInfo in advance, it is here and we will
480 // not build a new one. Used when deserializing information about an
481 // identifier that was constructed before the PCH file was read.
482 IdentifierInfo *KnownII;
483
484public:
485 typedef IdentifierInfo * data_type;
486
487 typedef const std::pair<const char*, unsigned> external_key_type;
488
489 typedef external_key_type internal_key_type;
490
Mike Stump1eb44332009-09-09 15:08:12 +0000491 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
Douglas Gregor668c1a42009-04-21 22:25:48 +0000492 : Reader(Reader), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregor668c1a42009-04-21 22:25:48 +0000494 static bool EqualKey(const internal_key_type& a,
495 const internal_key_type& b) {
496 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
497 : false;
498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Douglas Gregor668c1a42009-04-21 22:25:48 +0000500 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000501 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregor668c1a42009-04-21 22:25:48 +0000504 // This hopefully will just get inlined and removed by the optimizer.
505 static const internal_key_type&
506 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor668c1a42009-04-21 22:25:48 +0000508 static std::pair<unsigned, unsigned>
509 ReadKeyDataLength(const unsigned char*& d) {
510 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000511 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000512 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000513 return std::make_pair(KeyLen, DataLen);
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregor668c1a42009-04-21 22:25:48 +0000516 static std::pair<const char*, unsigned>
517 ReadKey(const unsigned char* d, unsigned n) {
518 assert(n >= 2 && d[n-1] == '\0');
519 return std::make_pair((const char*) d, n-1);
520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
522 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000523 const unsigned char* d,
524 unsigned DataLen) {
525 using namespace clang::io;
Douglas Gregora92193e2009-04-28 21:18:29 +0000526 pch::IdentID ID = ReadUnalignedLE32(d);
527 bool IsInteresting = ID & 0x01;
528
529 // Wipe out the "is interesting" bit.
530 ID = ID >> 1;
531
532 if (!IsInteresting) {
533 // For unintersting identifiers, just build the IdentifierInfo
534 // and associate it with the persistent ID.
535 IdentifierInfo *II = KnownII;
536 if (!II)
537 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
538 k.first, k.first + k.second);
539 Reader.SetIdentifierInfo(ID, II);
540 return II;
541 }
542
Douglas Gregor5998da52009-04-28 21:32:13 +0000543 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000544 bool CPlusPlusOperatorKeyword = Bits & 0x01;
545 Bits >>= 1;
546 bool Poisoned = Bits & 0x01;
547 Bits >>= 1;
548 bool ExtensionToken = Bits & 0x01;
549 Bits >>= 1;
550 bool hasMacroDefinition = Bits & 0x01;
551 Bits >>= 1;
552 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
553 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Douglas Gregor2deaea32009-04-22 18:49:13 +0000555 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000556 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000557
558 // Build the IdentifierInfo itself and link the identifier ID with
559 // the new IdentifierInfo.
560 IdentifierInfo *II = KnownII;
561 if (!II)
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000562 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
563 k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000564 Reader.SetIdentifierInfo(ID, II);
565
Douglas Gregor2deaea32009-04-22 18:49:13 +0000566 // Set or check the various bits in the IdentifierInfo structure.
567 // FIXME: Load token IDs lazily, too?
Douglas Gregor2deaea32009-04-22 18:49:13 +0000568 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000569 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000570 "Incorrect extension token flag");
571 (void)ExtensionToken;
572 II->setIsPoisoned(Poisoned);
573 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
574 "Incorrect C++ operator keyword flag");
575 (void)CPlusPlusOperatorKeyword;
576
Douglas Gregor37e26842009-04-21 23:56:24 +0000577 // If this identifier is a macro, deserialize the macro
578 // definition.
579 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000580 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor37e26842009-04-21 23:56:24 +0000581 Reader.ReadMacroRecord(Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000582 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000583 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000584
585 // Read all of the declarations visible at global scope with this
586 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000587 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000588 if (DataLen > 0) {
589 llvm::SmallVector<uint32_t, 4> DeclIDs;
590 for (; DataLen > 0; DataLen -= 4)
591 DeclIDs.push_back(ReadUnalignedLE32(d));
592 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Douglas Gregor668c1a42009-04-21 22:25:48 +0000595 return II;
596 }
597};
Mike Stump1eb44332009-09-09 15:08:12 +0000598
599} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000600
601/// \brief The on-disk hash table used to contain information about
602/// all of the identifiers in the program.
Mike Stump1eb44332009-09-09 15:08:12 +0000603typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
Douglas Gregor668c1a42009-04-21 22:25:48 +0000604 PCHIdentifierLookupTable;
605
Douglas Gregora02b1472009-04-28 21:53:25 +0000606bool PCHReader::Error(const char *Msg) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000607 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Fatal, Msg);
608 Diag(DiagID);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000609 return true;
610}
611
Douglas Gregore1d918e2009-04-10 23:10:45 +0000612/// \brief Check the contents of the predefines buffer against the
613/// contents of the predefines buffer used to build the PCH file.
614///
615/// The contents of the two predefines buffers should be the same. If
616/// not, then some command-line option changed the preprocessor state
617/// and we must reject the PCH file.
618///
619/// \param PCHPredef The start of the predefines buffer in the PCH
620/// file.
621///
622/// \param PCHPredefLen The length of the predefines buffer in the PCH
623/// file.
624///
625/// \param PCHBufferID The FileID for the PCH predefines buffer.
626///
627/// \returns true if there was a mismatch (in which case the PCH file
628/// should be ignored), or false otherwise.
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000629bool PCHReader::CheckPredefinesBuffer(llvm::StringRef PCHPredef,
Douglas Gregore1d918e2009-04-10 23:10:45 +0000630 FileID PCHBufferID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000631 if (Listener)
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000632 return Listener->ReadPredefinesBuffer(PCHPredef, PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000633 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000634 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000635 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000636}
637
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000638//===----------------------------------------------------------------------===//
639// Source Manager Deserialization
640//===----------------------------------------------------------------------===//
641
Douglas Gregorbd945002009-04-13 16:31:14 +0000642/// \brief Read the line table in the source manager block.
643/// \returns true if ther was an error.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000644bool PCHReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000645 unsigned Idx = 0;
646 LineTableInfo &LineTable = SourceMgr.getLineTable();
647
648 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000649 std::map<int, int> FileIDs;
650 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000651 // Extract the file name
652 unsigned FilenameLen = Record[Idx++];
653 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
654 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000655 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000656 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000657 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000658 }
659
660 // Parse the line entries
661 std::vector<LineEntry> Entries;
662 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000663 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000664
665 // Extract the line entries
666 unsigned NumEntries = Record[Idx++];
667 Entries.clear();
668 Entries.reserve(NumEntries);
669 for (unsigned I = 0; I != NumEntries; ++I) {
670 unsigned FileOffset = Record[Idx++];
671 unsigned LineNo = Record[Idx++];
672 int FilenameID = Record[Idx++];
Mike Stump1eb44332009-09-09 15:08:12 +0000673 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000674 = (SrcMgr::CharacteristicKind)Record[Idx++];
675 unsigned IncludeOffset = Record[Idx++];
676 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
677 FileKind, IncludeOffset));
678 }
679 LineTable.AddEntry(FID, Entries);
680 }
681
682 return false;
683}
684
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000685namespace {
686
Benjamin Kramerbd218282009-11-28 10:07:24 +0000687class PCHStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000688public:
689 const bool hasStat;
690 const ino_t ino;
691 const dev_t dev;
692 const mode_t mode;
693 const time_t mtime;
694 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000696 PCHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +0000697 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
698
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000699 PCHStatData()
700 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
701};
702
Benjamin Kramerbd218282009-11-28 10:07:24 +0000703class PCHStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000704 public:
705 typedef const char *external_key_type;
706 typedef const char *internal_key_type;
707
708 typedef PCHStatData data_type;
709
710 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000711 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000712 }
713
714 static internal_key_type GetInternalKey(const char *path) { return path; }
715
716 static bool EqualKey(internal_key_type a, internal_key_type b) {
717 return strcmp(a, b) == 0;
718 }
719
720 static std::pair<unsigned, unsigned>
721 ReadKeyDataLength(const unsigned char*& d) {
722 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
723 unsigned DataLen = (unsigned) *d++;
724 return std::make_pair(KeyLen + 1, DataLen);
725 }
726
727 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
728 return (const char *)d;
729 }
730
731 static data_type ReadData(const internal_key_type, const unsigned char *d,
732 unsigned /*DataLen*/) {
733 using namespace clang::io;
734
735 if (*d++ == 1)
736 return data_type();
737
738 ino_t ino = (ino_t) ReadUnalignedLE32(d);
739 dev_t dev = (dev_t) ReadUnalignedLE32(d);
740 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000741 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000742 off_t size = (off_t) ReadUnalignedLE64(d);
743 return data_type(ino, dev, mode, mtime, size);
744 }
745};
746
747/// \brief stat() cache for precompiled headers.
748///
749/// This cache is very similar to the stat cache used by pretokenized
750/// headers.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000751class PCHStatCache : public StatSysCallCache {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000752 typedef OnDiskChainedHashTable<PCHStatLookupTrait> CacheTy;
753 CacheTy *Cache;
754
755 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000756public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000757 PCHStatCache(const unsigned char *Buckets,
758 const unsigned char *Base,
759 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +0000760 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000761 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
762 Cache = CacheTy::Create(Buckets, Base);
763 }
764
765 ~PCHStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000767 int stat(const char *path, struct stat *buf) {
768 // Do the lookup for the file's data in the PCH file.
769 CacheTy::iterator I = Cache->find(path);
770
771 // If we don't get a hit in the PCH file just forward to 'stat'.
772 if (I == Cache->end()) {
773 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +0000774 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000777 ++NumStatHits;
778 PCHStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000780 if (!Data.hasStat)
781 return 1;
782
783 buf->st_ino = Data.ino;
784 buf->st_dev = Data.dev;
785 buf->st_mtime = Data.mtime;
786 buf->st_mode = Data.mode;
787 buf->st_size = Data.size;
788 return 0;
789 }
790};
791} // end anonymous namespace
792
793
Douglas Gregor14f79002009-04-10 03:52:48 +0000794/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000795PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000796 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000797
798 // Set the source-location entry cursor to the current position in
799 // the stream. This cursor will be used to read the contents of the
800 // source manager block initially, and then lazily read
801 // source-location entries as needed.
802 SLocEntryCursor = Stream;
803
804 // The stream itself is going to skip over the source manager block.
805 if (Stream.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000806 Error("malformed block record in PCH file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000807 return Failure;
808 }
809
810 // Enter the source manager block.
811 if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000812 Error("malformed source manager block record in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000813 return Failure;
814 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000815
Douglas Gregor14f79002009-04-10 03:52:48 +0000816 RecordData Record;
817 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000818 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000819 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000820 if (SLocEntryCursor.ReadBlockEnd()) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000821 Error("error at end of Source Manager block in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000822 return Failure;
823 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000824 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Douglas Gregor14f79002009-04-10 03:52:48 +0000827 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
828 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000829 SLocEntryCursor.ReadSubBlockID();
830 if (SLocEntryCursor.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000831 Error("malformed block record in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000832 return Failure;
833 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000834 continue;
835 }
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Douglas Gregor14f79002009-04-10 03:52:48 +0000837 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000838 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000839 continue;
840 }
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Douglas Gregor14f79002009-04-10 03:52:48 +0000842 // Read a record.
843 const char *BlobStart;
844 unsigned BlobLen;
845 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000846 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000847 default: // Default behavior: ignore.
848 break;
849
Chris Lattner2c78b872009-04-14 23:22:57 +0000850 case pch::SM_LINE_TABLE:
Douglas Gregore650c8c2009-07-07 00:12:59 +0000851 if (ParseLineTable(Record))
Douglas Gregorbd945002009-04-13 16:31:14 +0000852 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000853 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000854
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000855 case pch::SM_SLOC_FILE_ENTRY:
856 case pch::SM_SLOC_BUFFER_ENTRY:
857 case pch::SM_SLOC_INSTANTIATION_ENTRY:
858 // Once we hit one of the source location entries, we're done.
859 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000860 }
861 }
862}
863
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000864/// \brief Read in the source location entry with the given ID.
865PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) {
866 if (ID == 0)
867 return Success;
868
869 if (ID > TotalNumSLocEntries) {
870 Error("source location entry ID out-of-range for PCH file");
871 return Failure;
872 }
873
874 ++NumSLocEntriesRead;
875 SLocEntryCursor.JumpToBit(SLocOffsets[ID - 1]);
876 unsigned Code = SLocEntryCursor.ReadCode();
877 if (Code == llvm::bitc::END_BLOCK ||
878 Code == llvm::bitc::ENTER_SUBBLOCK ||
879 Code == llvm::bitc::DEFINE_ABBREV) {
880 Error("incorrectly-formatted source location entry in PCH file");
881 return Failure;
882 }
883
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000884 RecordData Record;
885 const char *BlobStart;
886 unsigned BlobLen;
887 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
888 default:
889 Error("incorrectly-formatted source location entry in PCH file");
890 return Failure;
891
892 case pch::SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000893 std::string Filename(BlobStart, BlobStart + BlobLen);
894 MaybeAddSystemRootToFilename(Filename);
895 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +0000896 if (File == 0) {
897 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +0000898 ErrorStr += Filename;
Chris Lattnerd3555ae2009-06-15 04:35:16 +0000899 ErrorStr += "' referenced by PCH file";
900 Error(ErrorStr.c_str());
901 return Failure;
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000904 FileID FID = SourceMgr.createFileID(File,
905 SourceLocation::getFromRawEncoding(Record[1]),
906 (SrcMgr::CharacteristicKind)Record[2],
907 ID, Record[0]);
908 if (Record[3])
909 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
910 .setHasLineDirectives();
911
Douglas Gregor12fab312010-03-16 16:35:32 +0000912 // Reconstruct header-search information for this file.
913 HeaderFileInfo HFI;
914 HFI.isImport = Record[4];
915 HFI.DirInfo = Record[5];
916 HFI.NumIncludes = Record[6];
917 HFI.ControllingMacroID = Record[7];
918 if (Listener)
919 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000920 break;
921 }
922
923 case pch::SM_SLOC_BUFFER_ENTRY: {
924 const char *Name = BlobStart;
925 unsigned Offset = Record[0];
926 unsigned Code = SLocEntryCursor.ReadCode();
927 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000928 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000929 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
930 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
931 (void)RecCode;
932 llvm::MemoryBuffer *Buffer
Mike Stump1eb44332009-09-09 15:08:12 +0000933 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000934 BlobStart + BlobLen - 1,
935 Name);
936 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Douglas Gregor92b059e2009-04-28 20:33:11 +0000938 if (strcmp(Name, "<built-in>") == 0) {
939 PCHPredefinesBufferID = BufferID;
940 PCHPredefines = BlobStart;
941 PCHPredefinesLen = BlobLen - 1;
942 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000943
944 break;
945 }
946
947 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump1eb44332009-09-09 15:08:12 +0000948 SourceLocation SpellingLoc
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000949 = SourceLocation::getFromRawEncoding(Record[1]);
950 SourceMgr.createInstantiationLoc(SpellingLoc,
951 SourceLocation::getFromRawEncoding(Record[2]),
952 SourceLocation::getFromRawEncoding(Record[3]),
953 Record[4],
954 ID,
955 Record[0]);
956 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000957 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000958 }
959
960 return Success;
961}
962
Chris Lattner6367f6d2009-04-27 01:05:14 +0000963/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
964/// specified cursor. Read the abbreviations that are at the top of the block
965/// and then leave the cursor pointing into the block.
966bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
967 unsigned BlockID) {
968 if (Cursor.EnterSubBlock(BlockID)) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000969 Error("malformed block record in PCH file");
Chris Lattner6367f6d2009-04-27 01:05:14 +0000970 return Failure;
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattner6367f6d2009-04-27 01:05:14 +0000973 while (true) {
974 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattner6367f6d2009-04-27 01:05:14 +0000976 // We expect all abbrevs to be at the start of the block.
977 if (Code != llvm::bitc::DEFINE_ABBREV)
978 return false;
979 Cursor.ReadAbbrevRecord();
980 }
981}
982
Douglas Gregor37e26842009-04-21 23:56:24 +0000983void PCHReader::ReadMacroRecord(uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000984 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Douglas Gregor37e26842009-04-21 23:56:24 +0000986 // Keep track of where we are in the stream, then jump back there
987 // after reading this macro.
988 SavedStreamPosition SavedPosition(Stream);
989
990 Stream.JumpToBit(Offset);
991 RecordData Record;
992 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
993 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Douglas Gregor37e26842009-04-21 23:56:24 +0000995 while (true) {
996 unsigned Code = Stream.ReadCode();
997 switch (Code) {
998 case llvm::bitc::END_BLOCK:
999 return;
1000
1001 case llvm::bitc::ENTER_SUBBLOCK:
1002 // No known subblocks, always skip them.
1003 Stream.ReadSubBlockID();
1004 if (Stream.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001005 Error("malformed block record in PCH file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001006 return;
1007 }
1008 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregor37e26842009-04-21 23:56:24 +00001010 case llvm::bitc::DEFINE_ABBREV:
1011 Stream.ReadAbbrevRecord();
1012 continue;
1013 default: break;
1014 }
1015
1016 // Read a record.
1017 Record.clear();
1018 pch::PreprocessorRecordTypes RecType =
1019 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1020 switch (RecType) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001021 case pch::PP_MACRO_OBJECT_LIKE:
1022 case pch::PP_MACRO_FUNCTION_LIKE: {
1023 // If we already have a macro, that means that we've hit the end
1024 // of the definition of the macro we were looking for. We're
1025 // done.
1026 if (Macro)
1027 return;
1028
1029 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1030 if (II == 0) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001031 Error("macro must have a name in PCH file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001032 return;
1033 }
1034 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1035 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001037 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001038 MI->setIsUsed(isUsed);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Douglas Gregor37e26842009-04-21 23:56:24 +00001040 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1041 // Decode function-like macro info.
1042 bool isC99VarArgs = Record[3];
1043 bool isGNUVarArgs = Record[4];
1044 MacroArgs.clear();
1045 unsigned NumArgs = Record[5];
1046 for (unsigned i = 0; i != NumArgs; ++i)
1047 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1048
1049 // Install function-like macro info.
1050 MI->setIsFunctionLike();
1051 if (isC99VarArgs) MI->setIsC99Varargs();
1052 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001053 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001054 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001055 }
1056
1057 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001058 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001059
1060 // Remember that we saw this macro last so that we add the tokens that
1061 // form its body to it.
1062 Macro = MI;
1063 ++NumMacrosRead;
1064 break;
1065 }
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Douglas Gregor37e26842009-04-21 23:56:24 +00001067 case pch::PP_TOKEN: {
1068 // If we see a TOKEN before a PP_MACRO_*, then the file is
1069 // erroneous, just pretend we didn't see this.
1070 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregor37e26842009-04-21 23:56:24 +00001072 Token Tok;
1073 Tok.startToken();
1074 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1075 Tok.setLength(Record[1]);
1076 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1077 Tok.setIdentifierInfo(II);
1078 Tok.setKind((tok::TokenKind)Record[3]);
1079 Tok.setFlag((Token::TokenFlags)Record[4]);
1080 Macro->AddTokenToBody(Tok);
1081 break;
1082 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001083 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001084 }
1085}
1086
Douglas Gregor88a35862010-01-04 19:18:44 +00001087void PCHReader::ReadDefinedMacros() {
1088 // If there was no preprocessor block, do nothing.
1089 if (!MacroCursor.getBitStreamReader())
1090 return;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001091
Douglas Gregor88a35862010-01-04 19:18:44 +00001092 llvm::BitstreamCursor Cursor = MacroCursor;
1093 if (Cursor.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) {
1094 Error("malformed preprocessor block record in PCH file");
1095 return;
1096 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001097
Douglas Gregor88a35862010-01-04 19:18:44 +00001098 RecordData Record;
1099 while (true) {
1100 unsigned Code = Cursor.ReadCode();
1101 if (Code == llvm::bitc::END_BLOCK) {
1102 if (Cursor.ReadBlockEnd())
1103 Error("error at end of preprocessor block in PCH file");
1104 return;
1105 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001106
Douglas Gregor88a35862010-01-04 19:18:44 +00001107 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1108 // No known subblocks, always skip them.
1109 Cursor.ReadSubBlockID();
1110 if (Cursor.SkipBlock()) {
1111 Error("malformed block record in PCH file");
1112 return;
1113 }
1114 continue;
1115 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001116
Douglas Gregor88a35862010-01-04 19:18:44 +00001117 if (Code == llvm::bitc::DEFINE_ABBREV) {
1118 Cursor.ReadAbbrevRecord();
1119 continue;
1120 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001121
Douglas Gregor88a35862010-01-04 19:18:44 +00001122 // Read a record.
1123 const char *BlobStart;
1124 unsigned BlobLen;
1125 Record.clear();
1126 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1127 default: // Default behavior: ignore.
1128 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001129
Douglas Gregor88a35862010-01-04 19:18:44 +00001130 case pch::PP_MACRO_OBJECT_LIKE:
1131 case pch::PP_MACRO_FUNCTION_LIKE:
1132 DecodeIdentifierInfo(Record[0]);
1133 break;
1134
1135 case pch::PP_TOKEN:
1136 // Ignore tokens.
1137 break;
1138 }
1139 }
1140}
1141
Douglas Gregore650c8c2009-07-07 00:12:59 +00001142/// \brief If we are loading a relocatable PCH file, and the filename is
1143/// not an absolute path, add the system root to the beginning of the file
1144/// name.
1145void PCHReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1146 // If this is not a relocatable PCH file, there's nothing to do.
1147 if (!RelocatablePCH)
1148 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Daniel Dunbard5b21972009-11-18 19:50:41 +00001150 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001151 return;
1152
Douglas Gregore650c8c2009-07-07 00:12:59 +00001153 if (isysroot == 0) {
1154 // If no system root was given, default to '/'
1155 Filename.insert(Filename.begin(), '/');
1156 return;
1157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregore650c8c2009-07-07 00:12:59 +00001159 unsigned Length = strlen(isysroot);
1160 if (isysroot[Length - 1] != '/')
1161 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregore650c8c2009-07-07 00:12:59 +00001163 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1164}
1165
Mike Stump1eb44332009-09-09 15:08:12 +00001166PCHReader::PCHReadResult
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001167PCHReader::ReadPCHBlock() {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001168 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001169 Error("malformed block record in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001170 return Failure;
1171 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001172
1173 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001174 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001175 while (!Stream.AtEndOfStream()) {
1176 unsigned Code = Stream.ReadCode();
1177 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001178 if (Stream.ReadBlockEnd()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001179 Error("error at end of module block in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001180 return Failure;
1181 }
Chris Lattner7356a312009-04-11 21:15:38 +00001182
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001183 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001184 }
1185
1186 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1187 switch (Stream.ReadSubBlockID()) {
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001188 case pch::DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001189 // We lazily load the decls block, but we want to set up the
1190 // DeclsCursor cursor to point into it. Clone our current bitcode
1191 // cursor to it, enter the block and read the abbrevs in that block.
1192 // With the main cursor, we just skip over it.
1193 DeclsCursor = Stream;
1194 if (Stream.SkipBlock() || // Skip with the main cursor.
1195 // Read the abbrevs.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001196 ReadBlockAbbrevs(DeclsCursor, pch::DECLTYPES_BLOCK_ID)) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001197 Error("malformed block record in PCH file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001198 return Failure;
1199 }
1200 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Chris Lattner7356a312009-04-11 21:15:38 +00001202 case pch::PREPROCESSOR_BLOCK_ID:
Douglas Gregor88a35862010-01-04 19:18:44 +00001203 MacroCursor = Stream;
1204 if (PP)
1205 PP->setExternalSource(this);
1206
Chris Lattner7356a312009-04-11 21:15:38 +00001207 if (Stream.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001208 Error("malformed block record in PCH file");
Chris Lattner7356a312009-04-11 21:15:38 +00001209 return Failure;
1210 }
1211 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001212
Douglas Gregor14f79002009-04-10 03:52:48 +00001213 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001214 switch (ReadSourceManagerBlock()) {
1215 case Success:
1216 break;
1217
1218 case Failure:
Douglas Gregora02b1472009-04-28 21:53:25 +00001219 Error("malformed source manager block in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001220 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001221
1222 case IgnorePCH:
1223 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001224 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001225 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001226 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001227 continue;
1228 }
1229
1230 if (Code == llvm::bitc::DEFINE_ABBREV) {
1231 Stream.ReadAbbrevRecord();
1232 continue;
1233 }
1234
1235 // Read and process a record.
1236 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001237 const char *BlobStart = 0;
1238 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001239 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregor2bec0412009-04-10 21:16:55 +00001240 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001241 default: // Default behavior: ignore.
1242 break;
1243
1244 case pch::TYPE_OFFSET:
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001245 if (!TypesLoaded.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001246 Error("duplicate TYPE_OFFSET record in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001247 return Failure;
1248 }
Chris Lattnerc732f5a2009-04-27 18:24:17 +00001249 TypeOffsets = (const uint32_t *)BlobStart;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001250 TypesLoaded.resize(Record[0]);
Douglas Gregor8038d512009-04-10 17:25:41 +00001251 break;
1252
1253 case pch::DECL_OFFSET:
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001254 if (!DeclsLoaded.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001255 Error("duplicate DECL_OFFSET record in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001256 return Failure;
1257 }
Chris Lattnerc732f5a2009-04-27 18:24:17 +00001258 DeclOffsets = (const uint32_t *)BlobStart;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001259 DeclsLoaded.resize(Record[0]);
Douglas Gregor8038d512009-04-10 17:25:41 +00001260 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001261
1262 case pch::LANGUAGE_OPTIONS:
1263 if (ParseLanguageOptions(Record))
1264 return IgnorePCH;
1265 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001266
Douglas Gregorab41e632009-04-27 22:23:34 +00001267 case pch::METADATA: {
1268 if (Record[0] != pch::VERSION_MAJOR) {
1269 Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
1270 : diag::warn_pch_version_too_new);
1271 return IgnorePCH;
1272 }
1273
Douglas Gregore650c8c2009-07-07 00:12:59 +00001274 RelocatablePCH = Record[4];
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001275 if (Listener) {
1276 std::string TargetTriple(BlobStart, BlobLen);
1277 if (Listener->ReadTargetTriple(TargetTriple))
1278 return IgnorePCH;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001279 }
1280 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001281 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001282
1283 case pch::IDENTIFIER_TABLE:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001284 IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001285 if (Record[0]) {
Mike Stump1eb44332009-09-09 15:08:12 +00001286 IdentifierLookupTable
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001287 = PCHIdentifierLookupTable::Create(
Douglas Gregor668c1a42009-04-21 22:25:48 +00001288 (const unsigned char *)IdentifierTableData + Record[0],
Mike Stump1eb44332009-09-09 15:08:12 +00001289 (const unsigned char *)IdentifierTableData,
Douglas Gregor668c1a42009-04-21 22:25:48 +00001290 PCHIdentifierLookupTrait(*this));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001291 if (PP)
1292 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001293 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001294 break;
1295
1296 case pch::IDENTIFIER_OFFSET:
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001297 if (!IdentifiersLoaded.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001298 Error("duplicate IDENTIFIER_OFFSET record in PCH file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001299 return Failure;
1300 }
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001301 IdentifierOffsets = (const uint32_t *)BlobStart;
1302 IdentifiersLoaded.resize(Record[0]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001303 if (PP)
1304 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001305 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001306
1307 case pch::EXTERNAL_DEFINITIONS:
1308 if (!ExternalDefinitions.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001309 Error("duplicate EXTERNAL_DEFINITIONS record in PCH file");
Douglas Gregorfdd01722009-04-14 00:24:19 +00001310 return Failure;
1311 }
1312 ExternalDefinitions.swap(Record);
1313 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001314
Douglas Gregorad1de002009-04-18 05:55:16 +00001315 case pch::SPECIAL_TYPES:
1316 SpecialTypes.swap(Record);
1317 break;
1318
Douglas Gregor3e1af842009-04-17 22:13:46 +00001319 case pch::STATISTICS:
1320 TotalNumStatements = Record[0];
Douglas Gregor37e26842009-04-21 23:56:24 +00001321 TotalNumMacros = Record[1];
Douglas Gregor25123082009-04-22 22:34:57 +00001322 TotalLexicalDeclContexts = Record[2];
1323 TotalVisibleDeclContexts = Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001324 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001325
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001326 case pch::TENTATIVE_DEFINITIONS:
1327 if (!TentativeDefinitions.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001328 Error("duplicate TENTATIVE_DEFINITIONS record in PCH file");
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001329 return Failure;
1330 }
1331 TentativeDefinitions.swap(Record);
1332 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001333
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001334 case pch::UNUSED_STATIC_FUNCS:
1335 if (!UnusedStaticFuncs.empty()) {
1336 Error("duplicate UNUSED_STATIC_FUNCS record in PCH file");
1337 return Failure;
1338 }
1339 UnusedStaticFuncs.swap(Record);
1340 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001341
Douglas Gregor14c22f22009-04-22 22:18:58 +00001342 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1343 if (!LocallyScopedExternalDecls.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001344 Error("duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
Douglas Gregor14c22f22009-04-22 22:18:58 +00001345 return Failure;
1346 }
1347 LocallyScopedExternalDecls.swap(Record);
1348 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001349
Douglas Gregor83941df2009-04-25 17:48:32 +00001350 case pch::SELECTOR_OFFSETS:
1351 SelectorOffsets = (const uint32_t *)BlobStart;
1352 TotalNumSelectors = Record[0];
1353 SelectorsLoaded.resize(TotalNumSelectors);
1354 break;
1355
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001356 case pch::METHOD_POOL:
Douglas Gregor83941df2009-04-25 17:48:32 +00001357 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1358 if (Record[0])
Mike Stump1eb44332009-09-09 15:08:12 +00001359 MethodPoolLookupTable
Douglas Gregor83941df2009-04-25 17:48:32 +00001360 = PCHMethodPoolLookupTable::Create(
1361 MethodPoolLookupTableData + Record[0],
Mike Stump1eb44332009-09-09 15:08:12 +00001362 MethodPoolLookupTableData,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001363 PCHMethodPoolLookupTrait(*this));
Douglas Gregor83941df2009-04-25 17:48:32 +00001364 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001365 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001366
1367 case pch::PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001368 if (!Record.empty() && Listener)
1369 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001370 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001371
1372 case pch::SOURCE_LOCATION_OFFSETS:
Chris Lattner090d9b52009-04-27 19:01:47 +00001373 SLocOffsets = (const uint32_t *)BlobStart;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001374 TotalNumSLocEntries = Record[0];
Douglas Gregor445e23e2009-10-05 21:07:28 +00001375 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001376 break;
1377
1378 case pch::SOURCE_LOCATION_PRELOADS:
1379 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
1380 PCHReadResult Result = ReadSLocEntryRecord(Record[I]);
1381 if (Result != Success)
1382 return Result;
1383 }
1384 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001385
Douglas Gregor52e71082009-10-16 18:18:30 +00001386 case pch::STAT_CACHE: {
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001387 PCHStatCache *MyStatCache =
Douglas Gregor52e71082009-10-16 18:18:30 +00001388 new PCHStatCache((const unsigned char *)BlobStart + Record[0],
1389 (const unsigned char *)BlobStart,
1390 NumStatHits, NumStatMisses);
1391 FileMgr.addStatCache(MyStatCache);
1392 StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001393 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001394 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001395
Douglas Gregorb81c1702009-04-27 20:06:05 +00001396 case pch::EXT_VECTOR_DECLS:
1397 if (!ExtVectorDecls.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001398 Error("duplicate EXT_VECTOR_DECLS record in PCH file");
Douglas Gregorb81c1702009-04-27 20:06:05 +00001399 return Failure;
1400 }
1401 ExtVectorDecls.swap(Record);
1402 break;
1403
Douglas Gregorb64c1932009-05-12 01:31:05 +00001404 case pch::ORIGINAL_FILE_NAME:
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001405 ActualOriginalFileName.assign(BlobStart, BlobLen);
1406 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001407 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001408 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Douglas Gregor2e222532009-07-02 17:08:52 +00001410 case pch::COMMENT_RANGES:
1411 Comments = (SourceRange *)BlobStart;
1412 NumComments = BlobLen / sizeof(SourceRange);
1413 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001414
Ted Kremenek5b4ec632010-01-22 20:59:36 +00001415 case pch::VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001416 const std::string &CurBranch = getClangFullRepositoryVersion();
Ted Kremenek517e6762010-01-22 20:55:35 +00001417 llvm::StringRef PCHBranch(BlobStart, BlobLen);
Ted Kremenek974be4d2010-02-12 23:31:14 +00001418 if (llvm::StringRef(CurBranch) != PCHBranch) {
Douglas Gregor445e23e2009-10-05 21:07:28 +00001419 Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch;
1420 return IgnorePCH;
1421 }
1422 break;
1423 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001424 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001425 }
Douglas Gregora02b1472009-04-28 21:53:25 +00001426 Error("premature end of bitstream in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001427 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001428}
1429
Douglas Gregore1d918e2009-04-10 23:10:45 +00001430PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001431 // Set the PCH file name.
1432 this->FileName = FileName;
1433
Douglas Gregor2cf26342009-04-09 22:27:44 +00001434 // Open the PCH file.
Daniel Dunbarf3c740e2009-09-22 05:38:01 +00001435 //
1436 // FIXME: This shouldn't be here, we should just take a raw_ostream.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001437 std::string ErrStr;
Daniel Dunbar731ad8f2009-11-10 00:46:19 +00001438 Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001439 if (!Buffer) {
1440 Error(ErrStr.c_str());
1441 return IgnorePCH;
1442 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001443
1444 // Initialize the stream
Mike Stump1eb44332009-09-09 15:08:12 +00001445 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Chris Lattnerb9fa9172009-04-26 20:59:20 +00001446 (const unsigned char *)Buffer->getBufferEnd());
1447 Stream.init(StreamFile);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001448
1449 // Sniff for the signature.
1450 if (Stream.Read(8) != 'C' ||
1451 Stream.Read(8) != 'P' ||
1452 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001453 Stream.Read(8) != 'H') {
Douglas Gregora02b1472009-04-28 21:53:25 +00001454 Diag(diag::err_not_a_pch_file) << FileName;
1455 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001456 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001457
Douglas Gregor2cf26342009-04-09 22:27:44 +00001458 while (!Stream.AtEndOfStream()) {
1459 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregore1d918e2009-04-10 23:10:45 +00001461 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001462 Error("invalid record at top-level of PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001463 return Failure;
1464 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001465
1466 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001467
Douglas Gregor2cf26342009-04-09 22:27:44 +00001468 // We only know the PCH subblock ID.
1469 switch (BlockID) {
1470 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001471 if (Stream.ReadBlockInfoBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001472 Error("malformed BlockInfoBlock in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001473 return Failure;
1474 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001475 break;
1476 case pch::PCH_BLOCK_ID:
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001477 switch (ReadPCHBlock()) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001478 case Success:
1479 break;
1480
1481 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001482 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001483
1484 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001485 // FIXME: We could consider reading through to the end of this
1486 // PCH block, skipping subblocks, to see if there are other
1487 // PCH blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001488
1489 // Clear out any preallocated source location entries, so that
1490 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001491 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001492
1493 // Remove the stat cache.
Douglas Gregor52e71082009-10-16 18:18:30 +00001494 if (StatCache)
1495 FileMgr.removeStatCache((PCHStatCache*)StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001496
Douglas Gregore1d918e2009-04-10 23:10:45 +00001497 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001498 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001499 break;
1500 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001501 if (Stream.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001502 Error("malformed block record in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001503 return Failure;
1504 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001505 break;
1506 }
Mike Stump1eb44332009-09-09 15:08:12 +00001507 }
1508
Douglas Gregor92b059e2009-04-28 20:33:11 +00001509 // Check the predefines buffer.
Daniel Dunbardc3c0d22009-11-11 00:52:11 +00001510 if (CheckPredefinesBuffer(llvm::StringRef(PCHPredefines, PCHPredefinesLen),
Douglas Gregor92b059e2009-04-28 20:33:11 +00001511 PCHPredefinesBufferID))
1512 return IgnorePCH;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001514 if (PP) {
Zhongxing Xu08996212009-07-18 09:26:51 +00001515 // Initialization of keywords and pragmas occurs before the
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001516 // PCH file is read, so there may be some identifiers that were
1517 // loaded into the IdentifierTable before we intercepted the
1518 // creation of identifiers. Iterate through the list of known
1519 // identifiers and determine whether we have to establish
1520 // preprocessor definitions or top-level identifier declaration
1521 // chains for those identifiers.
1522 //
1523 // We copy the IdentifierInfo pointers to a small vector first,
1524 // since de-serializing declarations or macro definitions can add
1525 // new entries into the identifier table, invalidating the
1526 // iterators.
1527 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1528 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1529 IdEnd = PP->getIdentifierTable().end();
1530 Id != IdEnd; ++Id)
1531 Identifiers.push_back(Id->second);
Mike Stump1eb44332009-09-09 15:08:12 +00001532 PCHIdentifierLookupTable *IdTable
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001533 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1534 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1535 IdentifierInfo *II = Identifiers[I];
1536 // Look in the on-disk hash table for an entry for
1537 PCHIdentifierLookupTrait Info(*this, II);
Daniel Dunbare013d682009-10-18 20:26:12 +00001538 std::pair<const char*, unsigned> Key(II->getNameStart(), II->getLength());
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001539 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1540 if (Pos == IdTable->end())
1541 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001543 // Dereferencing the iterator has the effect of populating the
1544 // IdentifierInfo node with the various declarations it needs.
1545 (void)*Pos;
1546 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001547 }
1548
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001549 if (Context)
1550 InitializeContext(*Context);
Douglas Gregor0b748912009-04-14 21:18:50 +00001551
Douglas Gregor668c1a42009-04-21 22:25:48 +00001552 return Success;
Douglas Gregor0b748912009-04-14 21:18:50 +00001553}
1554
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001555void PCHReader::InitializeContext(ASTContext &Ctx) {
1556 Context = &Ctx;
1557 assert(Context && "Passed null context!");
1558
1559 assert(PP && "Forgot to set Preprocessor ?");
1560 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1561 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001562 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001563
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001564 // Load the translation unit declaration
1565 ReadDeclRecord(DeclOffsets[0], 0);
1566
1567 // Load the special types.
1568 Context->setBuiltinVaListType(
1569 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1570 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
1571 Context->setObjCIdType(GetType(Id));
1572 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
1573 Context->setObjCSelType(GetType(Sel));
1574 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
1575 Context->setObjCProtoType(GetType(Proto));
1576 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
1577 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00001578
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001579 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
1580 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00001581 if (unsigned FastEnum
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001582 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
1583 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001584 if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) {
1585 QualType FileType = GetType(File);
1586 assert(!FileType.isNull() && "FILE type is NULL");
John McCall183700f2009-09-21 23:43:11 +00001587 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001588 Context->setFILEDecl(Typedef->getDecl());
1589 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001590 const TagType *Tag = FileType->getAs<TagType>();
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001591 assert(Tag && "Invalid FILE type in PCH file");
1592 Context->setFILEDecl(Tag->getDecl());
1593 }
1594 }
Mike Stump782fa302009-07-28 02:25:19 +00001595 if (unsigned Jmp_buf = SpecialTypes[pch::SPECIAL_TYPE_jmp_buf]) {
1596 QualType Jmp_bufType = GetType(Jmp_buf);
1597 assert(!Jmp_bufType.isNull() && "jmp_bug type is NULL");
John McCall183700f2009-09-21 23:43:11 +00001598 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00001599 Context->setjmp_bufDecl(Typedef->getDecl());
1600 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001601 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Mike Stump782fa302009-07-28 02:25:19 +00001602 assert(Tag && "Invalid jmp_bug type in PCH file");
1603 Context->setjmp_bufDecl(Tag->getDecl());
1604 }
1605 }
1606 if (unsigned Sigjmp_buf = SpecialTypes[pch::SPECIAL_TYPE_sigjmp_buf]) {
1607 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
1608 assert(!Sigjmp_bufType.isNull() && "sigjmp_buf type is NULL");
John McCall183700f2009-09-21 23:43:11 +00001609 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00001610 Context->setsigjmp_bufDecl(Typedef->getDecl());
1611 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001612 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Mike Stump782fa302009-07-28 02:25:19 +00001613 assert(Tag && "Invalid sigjmp_buf type in PCH file");
1614 Context->setsigjmp_bufDecl(Tag->getDecl());
1615 }
1616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617 if (unsigned ObjCIdRedef
Douglas Gregord1571ac2009-08-21 00:27:50 +00001618 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID_REDEFINITION])
1619 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00001620 if (unsigned ObjCClassRedef
Douglas Gregord1571ac2009-08-21 00:27:50 +00001621 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
1622 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001623#if 0
1624 // FIXME. Accommodate for this in several PCH/Index tests
1625 if (unsigned ObjCSelRedef
1626 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00001627 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001628#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00001629 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR])
1630 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00001631 if (unsigned String
1632 = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
1633 Context->setBlockDescriptorExtendedType(GetType(String));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001634}
1635
Douglas Gregorb64c1932009-05-12 01:31:05 +00001636/// \brief Retrieve the name of the original source file name
1637/// directly from the PCH file, without actually loading the PCH
1638/// file.
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001639std::string PCHReader::getOriginalSourceFile(const std::string &PCHFileName,
1640 Diagnostic &Diags) {
Douglas Gregorb64c1932009-05-12 01:31:05 +00001641 // Open the PCH file.
1642 std::string ErrStr;
1643 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
1644 Buffer.reset(llvm::MemoryBuffer::getFile(PCHFileName.c_str(), &ErrStr));
1645 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001646 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001647 return std::string();
1648 }
1649
1650 // Initialize the stream
1651 llvm::BitstreamReader StreamFile;
1652 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00001653 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00001654 (const unsigned char *)Buffer->getBufferEnd());
1655 Stream.init(StreamFile);
1656
1657 // Sniff for the signature.
1658 if (Stream.Read(8) != 'C' ||
1659 Stream.Read(8) != 'P' ||
1660 Stream.Read(8) != 'C' ||
1661 Stream.Read(8) != 'H') {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001662 Diags.Report(diag::err_fe_not_a_pch_file) << PCHFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001663 return std::string();
1664 }
1665
1666 RecordData Record;
1667 while (!Stream.AtEndOfStream()) {
1668 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Douglas Gregorb64c1932009-05-12 01:31:05 +00001670 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1671 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Douglas Gregorb64c1932009-05-12 01:31:05 +00001673 // We only know the PCH subblock ID.
1674 switch (BlockID) {
1675 case pch::PCH_BLOCK_ID:
1676 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001677 Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001678 return std::string();
1679 }
1680 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Douglas Gregorb64c1932009-05-12 01:31:05 +00001682 default:
1683 if (Stream.SkipBlock()) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001684 Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001685 return std::string();
1686 }
1687 break;
1688 }
1689 continue;
1690 }
1691
1692 if (Code == llvm::bitc::END_BLOCK) {
1693 if (Stream.ReadBlockEnd()) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001694 Diags.Report(diag::err_fe_pch_error_at_end_block) << PCHFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001695 return std::string();
1696 }
1697 continue;
1698 }
1699
1700 if (Code == llvm::bitc::DEFINE_ABBREV) {
1701 Stream.ReadAbbrevRecord();
1702 continue;
1703 }
1704
1705 Record.clear();
1706 const char *BlobStart = 0;
1707 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001708 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Douglas Gregorb64c1932009-05-12 01:31:05 +00001709 == pch::ORIGINAL_FILE_NAME)
1710 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00001711 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00001712
1713 return std::string();
1714}
1715
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001716/// \brief Parse the record that corresponds to a LangOptions data
1717/// structure.
1718///
1719/// This routine compares the language options used to generate the
1720/// PCH file against the language options set for the current
1721/// compilation. For each option, we classify differences between the
1722/// two compiler states as either "benign" or "important". Benign
1723/// differences don't matter, and we accept them without complaint
1724/// (and without modifying the language options). Differences between
1725/// the states for important options cause the PCH file to be
1726/// unusable, so we emit a warning and return true to indicate that
1727/// there was an error.
1728///
1729/// \returns true if the PCH file is unacceptable, false otherwise.
1730bool PCHReader::ParseLanguageOptions(
1731 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001732 if (Listener) {
1733 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001735 #define PARSE_LANGOPT(Option) \
1736 LangOpts.Option = Record[Idx]; \
1737 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001739 unsigned Idx = 0;
1740 PARSE_LANGOPT(Trigraphs);
1741 PARSE_LANGOPT(BCPLComment);
1742 PARSE_LANGOPT(DollarIdents);
1743 PARSE_LANGOPT(AsmPreprocessor);
1744 PARSE_LANGOPT(GNUMode);
1745 PARSE_LANGOPT(ImplicitInt);
1746 PARSE_LANGOPT(Digraphs);
1747 PARSE_LANGOPT(HexFloats);
1748 PARSE_LANGOPT(C99);
1749 PARSE_LANGOPT(Microsoft);
1750 PARSE_LANGOPT(CPlusPlus);
1751 PARSE_LANGOPT(CPlusPlus0x);
1752 PARSE_LANGOPT(CXXOperatorNames);
1753 PARSE_LANGOPT(ObjC1);
1754 PARSE_LANGOPT(ObjC2);
1755 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001756 PARSE_LANGOPT(ObjCNonFragileABI2);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001757 PARSE_LANGOPT(PascalStrings);
1758 PARSE_LANGOPT(WritableStrings);
1759 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001760 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001761 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00001762 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001763 PARSE_LANGOPT(NeXTRuntime);
1764 PARSE_LANGOPT(Freestanding);
1765 PARSE_LANGOPT(NoBuiltin);
1766 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001767 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001768 PARSE_LANGOPT(Blocks);
1769 PARSE_LANGOPT(EmitAllDecls);
1770 PARSE_LANGOPT(MathErrno);
1771 PARSE_LANGOPT(OverflowChecking);
1772 PARSE_LANGOPT(HeinousExtensions);
1773 PARSE_LANGOPT(Optimize);
1774 PARSE_LANGOPT(OptimizeSize);
1775 PARSE_LANGOPT(Static);
1776 PARSE_LANGOPT(PICLevel);
1777 PARSE_LANGOPT(GNUInline);
1778 PARSE_LANGOPT(NoInline);
1779 PARSE_LANGOPT(AccessControl);
1780 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00001781 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001782 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx]);
1783 ++Idx;
1784 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx]);
1785 ++Idx;
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001786 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
1787 Record[Idx]);
1788 ++Idx;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001789 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001790 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00001791 PARSE_LANGOPT(CatchUndefined);
1792 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001793 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001794
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001795 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001796 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001797
1798 return false;
1799}
1800
Douglas Gregor2e222532009-07-02 17:08:52 +00001801void PCHReader::ReadComments(std::vector<SourceRange> &Comments) {
1802 Comments.resize(NumComments);
1803 std::copy(this->Comments, this->Comments + NumComments,
1804 Comments.begin());
1805}
1806
Douglas Gregor2cf26342009-04-09 22:27:44 +00001807/// \brief Read and return the type at the given offset.
1808///
1809/// This routine actually reads the record corresponding to the type
1810/// at the given offset in the bitstream. It is a helper routine for
1811/// GetType, which deals with reading type IDs.
1812QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001813 // Keep track of where we are in the stream, then jump back there
1814 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001815 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00001816
Douglas Gregord89275b2009-07-06 18:54:52 +00001817 // Note that we are loading a type record.
1818 LoadingTypeOrDecl Loading(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001820 DeclsCursor.JumpToBit(Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001821 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001822 unsigned Code = DeclsCursor.ReadCode();
1823 switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001824 case pch::TYPE_EXT_QUAL: {
John McCall0953e762009-09-24 19:53:00 +00001825 assert(Record.size() == 2 &&
Douglas Gregor6d473962009-04-15 22:00:08 +00001826 "Incorrect encoding of extended qualifier type");
1827 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00001828 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
1829 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00001830 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001831
Douglas Gregor2cf26342009-04-09 22:27:44 +00001832 case pch::TYPE_COMPLEX: {
1833 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1834 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001835 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001836 }
1837
1838 case pch::TYPE_POINTER: {
1839 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1840 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001841 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001842 }
1843
1844 case pch::TYPE_BLOCK_POINTER: {
1845 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1846 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001847 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001848 }
1849
1850 case pch::TYPE_LVALUE_REFERENCE: {
1851 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1852 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001853 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001854 }
1855
1856 case pch::TYPE_RVALUE_REFERENCE: {
1857 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1858 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001859 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001860 }
1861
1862 case pch::TYPE_MEMBER_POINTER: {
1863 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1864 QualType PointeeType = GetType(Record[0]);
1865 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001866 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001867 }
1868
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001869 case pch::TYPE_CONSTANT_ARRAY: {
1870 QualType ElementType = GetType(Record[0]);
1871 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1872 unsigned IndexTypeQuals = Record[2];
1873 unsigned Idx = 3;
1874 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001875 return Context->getConstantArrayType(ElementType, Size,
1876 ASM, IndexTypeQuals);
1877 }
1878
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001879 case pch::TYPE_INCOMPLETE_ARRAY: {
1880 QualType ElementType = GetType(Record[0]);
1881 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1882 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001883 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001884 }
1885
1886 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00001887 QualType ElementType = GetType(Record[0]);
1888 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1889 unsigned IndexTypeQuals = Record[2];
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001890 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
1891 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001892 return Context->getVariableArrayType(ElementType, ReadTypeExpr(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001893 ASM, IndexTypeQuals,
1894 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001895 }
1896
1897 case pch::TYPE_VECTOR: {
John Thompson82287d12010-02-05 00:12:22 +00001898 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001899 Error("incorrect encoding of vector type in PCH file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001900 return QualType();
1901 }
1902
1903 QualType ElementType = GetType(Record[0]);
1904 unsigned NumElements = Record[1];
John Thompson82287d12010-02-05 00:12:22 +00001905 bool AltiVec = Record[2];
1906 bool Pixel = Record[3];
1907 return Context->getVectorType(ElementType, NumElements, AltiVec, Pixel);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001908 }
1909
1910 case pch::TYPE_EXT_VECTOR: {
John Thompson82287d12010-02-05 00:12:22 +00001911 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001912 Error("incorrect encoding of extended vector type in PCH file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001913 return QualType();
1914 }
1915
1916 QualType ElementType = GetType(Record[0]);
1917 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001918 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001919 }
1920
1921 case pch::TYPE_FUNCTION_NO_PROTO: {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001922 if (Record.size() != 3) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001923 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001924 return QualType();
1925 }
1926 QualType ResultType = GetType(Record[0]);
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001927 return Context->getFunctionNoProtoType(ResultType, Record[1],
1928 (CallingConv)Record[2]);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001929 }
1930
1931 case pch::TYPE_FUNCTION_PROTO: {
1932 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00001933 bool NoReturn = Record[1];
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001934 CallingConv CallConv = (CallingConv)Record[2];
1935 unsigned Idx = 3;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001936 unsigned NumParams = Record[Idx++];
1937 llvm::SmallVector<QualType, 16> ParamTypes;
1938 for (unsigned I = 0; I != NumParams; ++I)
1939 ParamTypes.push_back(GetType(Record[Idx++]));
1940 bool isVariadic = Record[Idx++];
1941 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00001942 bool hasExceptionSpec = Record[Idx++];
1943 bool hasAnyExceptionSpec = Record[Idx++];
1944 unsigned NumExceptions = Record[Idx++];
1945 llvm::SmallVector<QualType, 2> Exceptions;
1946 for (unsigned I = 0; I != NumExceptions; ++I)
1947 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00001948 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00001949 isVariadic, Quals, hasExceptionSpec,
1950 hasAnyExceptionSpec, NumExceptions,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001951 Exceptions.data(), NoReturn, CallConv);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001952 }
1953
John McCalled976492009-12-04 22:46:56 +00001954 case pch::TYPE_UNRESOLVED_USING:
1955 return Context->getTypeDeclType(
1956 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
1957
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001958 case pch::TYPE_TYPEDEF:
Douglas Gregora02b1472009-04-28 21:53:25 +00001959 assert(Record.size() == 1 && "incorrect encoding of typedef type");
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001960 return Context->getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001961
1962 case pch::TYPE_TYPEOF_EXPR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001963 return Context->getTypeOfExprType(ReadTypeExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001964
1965 case pch::TYPE_TYPEOF: {
1966 if (Record.size() != 1) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001967 Error("incorrect encoding of typeof(type) in PCH file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001968 return QualType();
1969 }
1970 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001971 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001972 }
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Anders Carlsson395b4752009-06-24 19:06:50 +00001974 case pch::TYPE_DECLTYPE:
1975 return Context->getDecltypeType(ReadTypeExpr());
1976
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001977 case pch::TYPE_RECORD:
Douglas Gregora02b1472009-04-28 21:53:25 +00001978 assert(Record.size() == 1 && "incorrect encoding of record type");
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001979 return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001980
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001981 case pch::TYPE_ENUM:
Douglas Gregora02b1472009-04-28 21:53:25 +00001982 assert(Record.size() == 1 && "incorrect encoding of enum type");
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001983 return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001984
John McCall7da24312009-09-05 00:15:47 +00001985 case pch::TYPE_ELABORATED: {
1986 assert(Record.size() == 2 && "incorrect encoding of elaborated type");
1987 unsigned Tag = Record[1];
1988 return Context->getElaboratedType(GetType(Record[0]),
1989 (ElaboratedType::TagKind) Tag);
1990 }
1991
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001992 case pch::TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00001993 unsigned Idx = 0;
1994 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
1995 unsigned NumProtos = Record[Idx++];
1996 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
1997 for (unsigned I = 0; I != NumProtos; ++I)
1998 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001999 return Context->getObjCInterfaceType(ItfD, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002000 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002001
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002002 case pch::TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002003 unsigned Idx = 0;
Steve Naroff14108da2009-07-10 23:34:53 +00002004 QualType OIT = GetType(Record[Idx++]);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002005 unsigned NumProtos = Record[Idx++];
2006 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2007 for (unsigned I = 0; I != NumProtos; ++I)
2008 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Steve Naroff14108da2009-07-10 23:34:53 +00002009 return Context->getObjCObjectPointerType(OIT, Protos.data(), NumProtos);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002010 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002011
John McCall49a832b2009-10-18 09:09:24 +00002012 case pch::TYPE_SUBST_TEMPLATE_TYPE_PARM: {
2013 unsigned Idx = 0;
2014 QualType Parm = GetType(Record[Idx++]);
2015 QualType Replacement = GetType(Record[Idx++]);
2016 return
2017 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2018 Replacement);
2019 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002020
2021 case pch::TYPE_INJECTED_CLASS_NAME: {
2022 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2023 QualType TST = GetType(Record[1]); // probably derivable
2024 return Context->getInjectedClassNameType(D, TST);
2025 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002026 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002027 // Suppress a GCC warning
2028 return QualType();
2029}
2030
John McCalla1ee0c52009-10-16 21:56:05 +00002031namespace {
2032
2033class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
2034 PCHReader &Reader;
2035 const PCHReader::RecordData &Record;
2036 unsigned &Idx;
2037
2038public:
2039 TypeLocReader(PCHReader &Reader, const PCHReader::RecordData &Record,
2040 unsigned &Idx)
2041 : Reader(Reader), Record(Record), Idx(Idx) { }
2042
John McCall51bd8032009-10-18 01:05:36 +00002043 // We want compile-time assurance that we've enumerated all of
2044 // these, so unfortunately we have to declare them first, then
2045 // define them out-of-line.
2046#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002047#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002048 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002049#include "clang/AST/TypeLocNodes.def"
2050
John McCall51bd8032009-10-18 01:05:36 +00002051 void VisitFunctionTypeLoc(FunctionTypeLoc);
2052 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002053};
2054
2055}
2056
John McCall51bd8032009-10-18 01:05:36 +00002057void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002058 // nothing to do
2059}
John McCall51bd8032009-10-18 01:05:36 +00002060void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00002061 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2062 if (TL.needsExtraLocalData()) {
2063 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2064 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2065 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2066 TL.setModeAttr(Record[Idx++]);
2067 }
John McCalla1ee0c52009-10-16 21:56:05 +00002068}
John McCall51bd8032009-10-18 01:05:36 +00002069void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2070 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002071}
John McCall51bd8032009-10-18 01:05:36 +00002072void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2073 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002074}
John McCall51bd8032009-10-18 01:05:36 +00002075void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2076 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002077}
John McCall51bd8032009-10-18 01:05:36 +00002078void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2079 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002080}
John McCall51bd8032009-10-18 01:05:36 +00002081void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2082 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002083}
John McCall51bd8032009-10-18 01:05:36 +00002084void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2085 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002086}
John McCall51bd8032009-10-18 01:05:36 +00002087void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2088 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2089 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002090 if (Record[Idx++])
John McCall51bd8032009-10-18 01:05:36 +00002091 TL.setSizeExpr(Reader.ReadDeclExpr());
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002092 else
John McCall51bd8032009-10-18 01:05:36 +00002093 TL.setSizeExpr(0);
2094}
2095void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2096 VisitArrayTypeLoc(TL);
2097}
2098void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2099 VisitArrayTypeLoc(TL);
2100}
2101void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2102 VisitArrayTypeLoc(TL);
2103}
2104void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2105 DependentSizedArrayTypeLoc TL) {
2106 VisitArrayTypeLoc(TL);
2107}
2108void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2109 DependentSizedExtVectorTypeLoc TL) {
2110 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2111}
2112void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2113 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2114}
2115void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2116 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2117}
2118void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2119 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2120 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2121 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002122 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002123 }
2124}
2125void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2126 VisitFunctionTypeLoc(TL);
2127}
2128void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2129 VisitFunctionTypeLoc(TL);
2130}
John McCalled976492009-12-04 22:46:56 +00002131void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2132 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2133}
John McCall51bd8032009-10-18 01:05:36 +00002134void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2135 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2136}
2137void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002138 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2139 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2140 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall51bd8032009-10-18 01:05:36 +00002141}
2142void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002143 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2144 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2145 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2146 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002147}
2148void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2149 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2150}
2151void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2152 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2153}
2154void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2155 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2156}
2157void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2158 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2159}
2160void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2161 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2162}
John McCall49a832b2009-10-18 09:09:24 +00002163void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2164 SubstTemplateTypeParmTypeLoc TL) {
2165 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2166}
John McCall51bd8032009-10-18 01:05:36 +00002167void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2168 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00002169 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2170 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2171 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2172 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2173 TL.setArgLocInfo(i,
2174 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
2175 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002176}
2177void TypeLocReader::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
2178 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2179}
John McCall3cb0ebd2010-03-10 03:28:59 +00002180void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2181 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2182}
John McCall51bd8032009-10-18 01:05:36 +00002183void TypeLocReader::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
2184 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2185}
2186void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2187 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall51bd8032009-10-18 01:05:36 +00002188 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2189 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2190 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2191 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002192}
John McCall54e14c42009-10-22 22:37:11 +00002193void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2194 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2195 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2196 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2197 TL.setHasBaseTypeAsWritten(Record[Idx++]);
2198 TL.setHasProtocolsAsWritten(Record[Idx++]);
2199 if (TL.hasProtocolsAsWritten())
2200 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2201 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
2202}
John McCalla1ee0c52009-10-16 21:56:05 +00002203
John McCalla93c9342009-12-07 02:54:59 +00002204TypeSourceInfo *PCHReader::GetTypeSourceInfo(const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00002205 unsigned &Idx) {
2206 QualType InfoTy = GetType(Record[Idx++]);
2207 if (InfoTy.isNull())
2208 return 0;
2209
John McCalla93c9342009-12-07 02:54:59 +00002210 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
John McCalla1ee0c52009-10-16 21:56:05 +00002211 TypeLocReader TLR(*this, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00002212 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002213 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00002214 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00002215}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002216
Douglas Gregor8038d512009-04-10 17:25:41 +00002217QualType PCHReader::GetType(pch::TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00002218 unsigned FastQuals = ID & Qualifiers::FastMask;
2219 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002220
2221 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2222 QualType T;
2223 switch ((pch::PredefinedTypeIDs)Index) {
2224 case pch::PREDEF_TYPE_NULL_ID: return QualType();
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002225 case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2226 case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002227
2228 case pch::PREDEF_TYPE_CHAR_U_ID:
2229 case pch::PREDEF_TYPE_CHAR_S_ID:
2230 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002231 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002232 break;
2233
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002234 case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2235 case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2236 case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2237 case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2238 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002239 case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002240 case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2241 case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2242 case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2243 case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2244 case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2245 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002246 case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002247 case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2248 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2249 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2250 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2251 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002252 case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002253 case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2254 case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002255 case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2256 case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002257 case pch::PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002258 }
2259
2260 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00002261 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002262 }
2263
2264 Index -= pch::NUM_PREDEF_TYPE_IDS;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002265 //assert(Index < TypesLoaded.size() && "Type index out-of-range");
John McCall0953e762009-09-24 19:53:00 +00002266 if (TypesLoaded[Index].isNull())
2267 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]);
Mike Stump1eb44332009-09-09 15:08:12 +00002268
John McCall0953e762009-09-24 19:53:00 +00002269 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002270}
2271
John McCall833ca992009-10-29 08:12:44 +00002272TemplateArgumentLocInfo
2273PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2274 const RecordData &Record,
2275 unsigned &Index) {
2276 switch (Kind) {
2277 case TemplateArgument::Expression:
2278 return ReadDeclExpr();
2279 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002280 return GetTypeSourceInfo(Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00002281 case TemplateArgument::Template: {
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002282 SourceLocation
Douglas Gregor788cd062009-11-11 01:00:40 +00002283 QualStart = SourceLocation::getFromRawEncoding(Record[Index++]),
2284 QualEnd = SourceLocation::getFromRawEncoding(Record[Index++]),
2285 TemplateNameLoc = SourceLocation::getFromRawEncoding(Record[Index++]);
2286 return TemplateArgumentLocInfo(SourceRange(QualStart, QualEnd),
2287 TemplateNameLoc);
2288 }
John McCall833ca992009-10-29 08:12:44 +00002289 case TemplateArgument::Null:
2290 case TemplateArgument::Integral:
2291 case TemplateArgument::Declaration:
2292 case TemplateArgument::Pack:
2293 return TemplateArgumentLocInfo();
2294 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002295 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00002296 return TemplateArgumentLocInfo();
2297}
2298
Douglas Gregor8038d512009-04-10 17:25:41 +00002299Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002300 if (ID == 0)
2301 return 0;
2302
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002303 if (ID > DeclsLoaded.size()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002304 Error("declaration ID out-of-range for PCH file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002305 return 0;
2306 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002307
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002308 unsigned Index = ID - 1;
2309 if (!DeclsLoaded[Index])
2310 ReadDeclRecord(DeclOffsets[Index], Index);
2311
2312 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002313}
2314
Chris Lattner887e2b32009-04-27 05:46:25 +00002315/// \brief Resolve the offset of a statement into a statement.
2316///
2317/// This operation will read a new statement from the external
2318/// source each time it is called, and is meant to be used via a
2319/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
2320Stmt *PCHReader::GetDeclStmt(uint64_t Offset) {
Chris Lattnerda930612009-04-27 05:58:23 +00002321 // Since we know tha this statement is part of a decl, make sure to use the
2322 // decl cursor to read it.
2323 DeclsCursor.JumpToBit(Offset);
2324 return ReadStmt(DeclsCursor);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002325}
2326
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00002328 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00002329 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002330 "DeclContext has no lexical decls in storage");
2331 uint64_t Offset = DeclContextOffsets[DC].first;
2332 assert(Offset && "DeclContext has no lexical decls in storage");
2333
Douglas Gregor0b748912009-04-14 21:18:50 +00002334 // Keep track of where we are in the stream, then jump back there
2335 // after reading this context.
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002336 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002337
Douglas Gregor2cf26342009-04-09 22:27:44 +00002338 // Load the record containing all of the declarations lexically in
2339 // this context.
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002340 DeclsCursor.JumpToBit(Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002341 RecordData Record;
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002342 unsigned Code = DeclsCursor.ReadCode();
2343 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002344 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002345 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2346
2347 // Load all of the declaration IDs
2348 Decls.clear();
2349 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregor25123082009-04-22 22:34:57 +00002350 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002351 return false;
2352}
2353
2354bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002355 llvm::SmallVectorImpl<VisibleDeclaration> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00002356 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002357 "DeclContext has no visible decls in storage");
2358 uint64_t Offset = DeclContextOffsets[DC].second;
2359 assert(Offset && "DeclContext has no visible decls in storage");
2360
Douglas Gregor0b748912009-04-14 21:18:50 +00002361 // Keep track of where we are in the stream, then jump back there
2362 // after reading this context.
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002363 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002364
Douglas Gregor2cf26342009-04-09 22:27:44 +00002365 // Load the record containing all of the declarations visible in
2366 // this context.
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002367 DeclsCursor.JumpToBit(Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002368 RecordData Record;
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002369 unsigned Code = DeclsCursor.ReadCode();
2370 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002371 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002372 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2373 if (Record.size() == 0)
Mike Stump1eb44332009-09-09 15:08:12 +00002374 return false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002375
2376 Decls.clear();
2377
2378 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002379 while (Idx < Record.size()) {
2380 Decls.push_back(VisibleDeclaration());
2381 Decls.back().Name = ReadDeclarationName(Record, Idx);
2382
Douglas Gregor2cf26342009-04-09 22:27:44 +00002383 unsigned Size = Record[Idx++];
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002384 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002385 LoadedDecls.reserve(Size);
2386 for (unsigned I = 0; I < Size; ++I)
2387 LoadedDecls.push_back(Record[Idx++]);
2388 }
2389
Douglas Gregor25123082009-04-22 22:34:57 +00002390 ++NumVisibleDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002391 return false;
2392}
2393
Douglas Gregorfdd01722009-04-14 00:24:19 +00002394void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002395 this->Consumer = Consumer;
2396
Douglas Gregorfdd01722009-04-14 00:24:19 +00002397 if (!Consumer)
2398 return;
2399
2400 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Daniel Dunbar04a0b502009-09-17 03:06:44 +00002401 // Force deserialization of this decl, which will cause it to be passed to
2402 // the consumer (or queued).
2403 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00002404 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00002405
2406 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2407 DeclGroupRef DG(InterestingDecls[I]);
2408 Consumer->HandleTopLevelDecl(DG);
2409 }
Douglas Gregorfdd01722009-04-14 00:24:19 +00002410}
2411
Douglas Gregor2cf26342009-04-09 22:27:44 +00002412void PCHReader::PrintStats() {
2413 std::fprintf(stderr, "*** PCH Statistics:\n");
2414
Mike Stump1eb44332009-09-09 15:08:12 +00002415 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002416 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00002417 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002418 unsigned NumDeclsLoaded
2419 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2420 (Decl *)0);
2421 unsigned NumIdentifiersLoaded
2422 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2423 IdentifiersLoaded.end(),
2424 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00002425 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002426 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2427 SelectorsLoaded.end(),
2428 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002429
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002430 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
2431 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002432 if (TotalNumSLocEntries)
2433 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
2434 NumSLocEntriesRead, TotalNumSLocEntries,
2435 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002436 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002437 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002438 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2439 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2440 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002441 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002442 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2443 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002444 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002445 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002446 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2447 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00002448 if (TotalNumSelectors)
2449 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2450 NumSelectorsLoaded, TotalNumSelectors,
2451 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2452 if (TotalNumStatements)
2453 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2454 NumStatementsRead, TotalNumStatements,
2455 ((float)NumStatementsRead/TotalNumStatements * 100));
2456 if (TotalNumMacros)
2457 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2458 NumMacrosRead, TotalNumMacros,
2459 ((float)NumMacrosRead/TotalNumMacros * 100));
2460 if (TotalLexicalDeclContexts)
2461 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2462 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2463 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2464 * 100));
2465 if (TotalVisibleDeclContexts)
2466 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2467 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2468 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2469 * 100));
2470 if (TotalSelectorsInMethodPool) {
2471 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2472 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2473 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2474 * 100));
2475 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2476 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002477 std::fprintf(stderr, "\n");
2478}
2479
Douglas Gregor668c1a42009-04-21 22:25:48 +00002480void PCHReader::InitializeSema(Sema &S) {
2481 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002482 S.ExternalSource = this;
2483
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002484 // Makes sure any declarations that were deserialized "too early"
2485 // still get added to the identifier's declaration chains.
2486 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2487 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2488 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002489 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002490 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002491
2492 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00002493 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002494 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2495 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00002496 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002497 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002498
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002499 // If there were any unused static functions, deserialize them and add to
2500 // Sema's list of unused static functions.
2501 for (unsigned I = 0, N = UnusedStaticFuncs.size(); I != N; ++I) {
2502 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(UnusedStaticFuncs[I]));
2503 SemaObj->UnusedStaticFuncs.push_back(FD);
2504 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00002505
2506 // If there were any locally-scoped external declarations,
2507 // deserialize them and add them to Sema's table of locally-scoped
2508 // external declarations.
2509 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2510 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2511 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2512 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00002513
2514 // If there were any ext_vector type declarations, deserialize them
2515 // and add them to Sema's vector of such declarations.
2516 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
2517 SemaObj->ExtVectorDecls.push_back(
2518 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002519}
2520
2521IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2522 // Try to find this name within our on-disk hash table
Mike Stump1eb44332009-09-09 15:08:12 +00002523 PCHIdentifierLookupTable *IdTable
Douglas Gregor668c1a42009-04-21 22:25:48 +00002524 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2525 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2526 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2527 if (Pos == IdTable->end())
2528 return 0;
2529
2530 // Dereferencing the iterator has the effect of building the
2531 // IdentifierInfo node and populating it with the various
2532 // declarations it needs.
2533 return *Pos;
2534}
2535
Mike Stump1eb44332009-09-09 15:08:12 +00002536std::pair<ObjCMethodList, ObjCMethodList>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002537PCHReader::ReadMethodPool(Selector Sel) {
2538 if (!MethodPoolLookupTable)
2539 return std::pair<ObjCMethodList, ObjCMethodList>();
2540
2541 // Try to find this selector within our on-disk hash table.
2542 PCHMethodPoolLookupTable *PoolTable
2543 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2544 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor83941df2009-04-25 17:48:32 +00002545 if (Pos == PoolTable->end()) {
2546 ++NumMethodPoolMisses;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002547 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor83941df2009-04-25 17:48:32 +00002548 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002549
Douglas Gregor83941df2009-04-25 17:48:32 +00002550 ++NumMethodPoolSelectorsRead;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002551 return *Pos;
2552}
2553
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002554void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00002555 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00002556 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002557 IdentifiersLoaded[ID - 1] = II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002558}
2559
Douglas Gregord89275b2009-07-06 18:54:52 +00002560/// \brief Set the globally-visible declarations associated with the given
2561/// identifier.
2562///
2563/// If the PCH reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00002564/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00002565/// them.
2566///
2567/// \param II an IdentifierInfo that refers to one or more globally-visible
2568/// declarations.
2569///
2570/// \param DeclIDs the set of declaration IDs with the name @p II that are
2571/// visible at global scope.
2572///
2573/// \param Nonrecursive should be true to indicate that the caller knows that
2574/// this call is non-recursive, and therefore the globally-visible declarations
2575/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00002576void
2577PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00002578 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
2579 bool Nonrecursive) {
2580 if (CurrentlyLoadingTypeOrDecl && !Nonrecursive) {
2581 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
2582 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
2583 PII.II = II;
2584 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
2585 PII.DeclIDs.push_back(DeclIDs[I]);
2586 return;
2587 }
Mike Stump1eb44332009-09-09 15:08:12 +00002588
Douglas Gregord89275b2009-07-06 18:54:52 +00002589 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
2590 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
2591 if (SemaObj) {
2592 // Introduce this declaration into the translation-unit scope
2593 // and add it to the declaration chain for this identifier, so
2594 // that (unqualified) name lookup will find it.
2595 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
2596 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
2597 } else {
2598 // Queue this declaration so that it will be added to the
2599 // translation unit scope and identifier's declaration chain
2600 // once a Sema object is known.
2601 PreloadedDecls.push_back(D);
2602 }
2603 }
2604}
2605
Chris Lattner7356a312009-04-11 21:15:38 +00002606IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002607 if (ID == 0)
2608 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002610 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002611 Error("no identifier table in PCH file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002612 return 0;
2613 }
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002615 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002616 if (!IdentifiersLoaded[ID - 1]) {
2617 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor17e1c5e2009-04-25 21:21:38 +00002618 const char *Str = IdentifierTableData + Offset;
Douglas Gregord6595a42009-04-25 21:04:17 +00002619
Douglas Gregor02fc7512009-04-28 20:01:51 +00002620 // All of the strings in the PCH file are preceded by a 16-bit
2621 // length. Extract that 16-bit length to avoid having to execute
2622 // strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00002623 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
2624 // unsigned integers. This is important to avoid integer overflow when
2625 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00002626 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00002627 unsigned StrLen = (((unsigned) StrLenPtr[0])
2628 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002629 IdentifiersLoaded[ID - 1]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00002630 = &PP->getIdentifierTable().get(Str, StrLen);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002631 }
Mike Stump1eb44332009-09-09 15:08:12 +00002632
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002633 return IdentifiersLoaded[ID - 1];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002634}
2635
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002636void PCHReader::ReadSLocEntry(unsigned ID) {
2637 ReadSLocEntryRecord(ID);
2638}
2639
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002640Selector PCHReader::DecodeSelector(unsigned ID) {
2641 if (ID == 0)
2642 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00002643
Douglas Gregora02b1472009-04-28 21:53:25 +00002644 if (!MethodPoolLookupTableData)
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002645 return Selector();
Douglas Gregor83941df2009-04-25 17:48:32 +00002646
2647 if (ID > TotalNumSelectors) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002648 Error("selector ID out of range in PCH file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002649 return Selector();
2650 }
Douglas Gregor83941df2009-04-25 17:48:32 +00002651
2652 unsigned Index = ID - 1;
2653 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2654 // Load this selector from the selector table.
2655 // FIXME: endianness portability issues with SelectorOffsets table
2656 PCHMethodPoolLookupTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002657 SelectorsLoaded[Index]
Douglas Gregor83941df2009-04-25 17:48:32 +00002658 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2659 }
2660
2661 return SelectorsLoaded[Index];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002662}
2663
Mike Stump1eb44332009-09-09 15:08:12 +00002664DeclarationName
Douglas Gregor2cf26342009-04-09 22:27:44 +00002665PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2666 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2667 switch (Kind) {
2668 case DeclarationName::Identifier:
2669 return DeclarationName(GetIdentifierInfo(Record, Idx));
2670
2671 case DeclarationName::ObjCZeroArgSelector:
2672 case DeclarationName::ObjCOneArgSelector:
2673 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00002674 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002675
2676 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002677 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00002678 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002679
2680 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002681 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00002682 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002683
2684 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002685 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00002686 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002687
2688 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002689 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00002690 (OverloadedOperatorKind)Record[Idx++]);
2691
Sean Hunt3e518bd2009-11-29 07:34:05 +00002692 case DeclarationName::CXXLiteralOperatorName:
2693 return Context->DeclarationNames.getCXXLiteralOperatorName(
2694 GetIdentifierInfo(Record, Idx));
2695
Douglas Gregor2cf26342009-04-09 22:27:44 +00002696 case DeclarationName::CXXUsingDirective:
2697 return DeclarationName::getUsingDirectiveName();
2698 }
2699
2700 // Required to silence GCC warning
2701 return DeclarationName();
2702}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002703
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002704/// \brief Read an integral value
2705llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2706 unsigned BitWidth = Record[Idx++];
2707 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2708 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2709 Idx += NumWords;
2710 return Result;
2711}
2712
2713/// \brief Read a signed integral value
2714llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2715 bool isUnsigned = Record[Idx++];
2716 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2717}
2718
Douglas Gregor17fc2232009-04-14 21:55:33 +00002719/// \brief Read a floating-point value
2720llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002721 return llvm::APFloat(ReadAPInt(Record, Idx));
2722}
2723
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002724// \brief Read a string
2725std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2726 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00002727 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002728 Idx += Len;
2729 return Result;
2730}
2731
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002732DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002733 return Diag(SourceLocation(), DiagID);
2734}
2735
2736DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002737 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002738}
Douglas Gregor025452f2009-04-17 00:04:06 +00002739
Douglas Gregor668c1a42009-04-21 22:25:48 +00002740/// \brief Retrieve the identifier table associated with the
2741/// preprocessor.
2742IdentifierTable &PCHReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002743 assert(PP && "Forgot to set Preprocessor ?");
2744 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002745}
2746
Douglas Gregor025452f2009-04-17 00:04:06 +00002747/// \brief Record that the given ID maps to the given switch-case
2748/// statement.
2749void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2750 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2751 SwitchCaseStmts[ID] = SC;
2752}
2753
2754/// \brief Retrieve the switch-case statement with the given ID.
2755SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2756 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2757 return SwitchCaseStmts[ID];
2758}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002759
2760/// \brief Record that the given label statement has been
2761/// deserialized and has the given ID.
2762void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00002763 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002764 "Deserialized label twice");
2765 LabelStmts[ID] = S;
2766
2767 // If we've already seen any goto statements that point to this
2768 // label, resolve them now.
2769 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2770 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2771 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2772 Goto->second->setLabel(S);
2773 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002774
2775 // If we've already seen any address-label statements that point to
2776 // this label, resolve them now.
2777 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00002778 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002779 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00002780 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002781 AddrLabel != AddrLabels.second; ++AddrLabel)
2782 AddrLabel->second->setLabel(S);
2783 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002784}
2785
2786/// \brief Set the label of the given statement to the label
2787/// identified by ID.
2788///
2789/// Depending on the order in which the label and other statements
2790/// referencing that label occur, this operation may complete
2791/// immediately (updating the statement) or it may queue the
2792/// statement to be back-patched later.
2793void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2794 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2795 if (Label != LabelStmts.end()) {
2796 // We've already seen this label, so set the label of the goto and
2797 // we're done.
2798 S->setLabel(Label->second);
2799 } else {
2800 // We haven't seen this label yet, so add this goto to the set of
2801 // unresolved goto statements.
2802 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2803 }
2804}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002805
2806/// \brief Set the label of the given expression to the label
2807/// identified by ID.
2808///
2809/// Depending on the order in which the label and other statements
2810/// referencing that label occur, this operation may complete
2811/// immediately (updating the statement) or it may queue the
2812/// statement to be back-patched later.
2813void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2814 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2815 if (Label != LabelStmts.end()) {
2816 // We've already seen this label, so set the label of the
2817 // label-address expression and we're done.
2818 S->setLabel(Label->second);
2819 } else {
2820 // We haven't seen this label yet, so add this label-address
2821 // expression to the set of unresolved label-address expressions.
2822 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2823 }
2824}
Douglas Gregord89275b2009-07-06 18:54:52 +00002825
2826
Mike Stump1eb44332009-09-09 15:08:12 +00002827PCHReader::LoadingTypeOrDecl::LoadingTypeOrDecl(PCHReader &Reader)
Douglas Gregord89275b2009-07-06 18:54:52 +00002828 : Reader(Reader), Parent(Reader.CurrentlyLoadingTypeOrDecl) {
2829 Reader.CurrentlyLoadingTypeOrDecl = this;
2830}
2831
2832PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() {
2833 if (!Parent) {
2834 // If any identifiers with corresponding top-level declarations have
2835 // been loaded, load those declarations now.
2836 while (!Reader.PendingIdentifierInfos.empty()) {
2837 Reader.SetGloballyVisibleDecls(Reader.PendingIdentifierInfos.front().II,
2838 Reader.PendingIdentifierInfos.front().DeclIDs,
2839 true);
2840 Reader.PendingIdentifierInfos.pop_front();
2841 }
2842 }
2843
Mike Stump1eb44332009-09-09 15:08:12 +00002844 Reader.CurrentlyLoadingTypeOrDecl = Parent;
Douglas Gregord89275b2009-07-06 18:54:52 +00002845}