blob: b5ddc86369dc21a573a0b917fb087e9b05148d8f [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 Gregor6a5a23f2010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000032#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000033#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000034#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000035#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000036#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000037#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000038#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000039#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000041#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000042using namespace clang;
43
44//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000045// PCH reader validator implementation
46//===----------------------------------------------------------------------===//
47
48PCHReaderListener::~PCHReaderListener() {}
49
50bool
51PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
52 const LangOptions &PPLangOpts = PP.getLangOptions();
53#define PARSE_LANGOPT_BENIGN(Option)
54#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
55 if (PPLangOpts.Option != LangOpts.Option) { \
56 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
57 return true; \
58 }
59
60 PARSE_LANGOPT_BENIGN(Trigraphs);
61 PARSE_LANGOPT_BENIGN(BCPLComment);
62 PARSE_LANGOPT_BENIGN(DollarIdents);
63 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
64 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000065 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000066 PARSE_LANGOPT_BENIGN(ImplicitInt);
67 PARSE_LANGOPT_BENIGN(Digraphs);
68 PARSE_LANGOPT_BENIGN(HexFloats);
69 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
70 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
71 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
72 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
73 PARSE_LANGOPT_BENIGN(CXXOperatorName);
74 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
75 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
76 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000077 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000078 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
79 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000080 PARSE_LANGOPT_BENIGN(PascalStrings);
81 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000082 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000084 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000085 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000086 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000087 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
88 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
89 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000090 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000091 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000092 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000093 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
94 PARSE_LANGOPT_BENIGN(EmitAllDecls);
95 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +000096 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +000097 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000098 diag::warn_pch_heinous_extensions);
99 // FIXME: Most of the options below are benign if the macro wasn't
100 // used. Unfortunately, this means that a PCH compiled without
101 // optimization can't be used with optimization turned on, even
102 // though the only thing that changes is whether __OPTIMIZE__ was
103 // defined... but if __OPTIMIZE__ never showed up in the header, it
104 // doesn't matter. We could consider making this some special kind
105 // of check.
106 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
107 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
108 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
109 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
110 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
111 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
112 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
113 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000114 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000115 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000116 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000117 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
118 return true;
119 }
120 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000121 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
122 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000123 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000124 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000125 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000126 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000127#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000128#undef PARSE_LANGOPT_BENIGN
129
130 return false;
131}
132
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000133bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
134 if (Triple == PP.getTargetInfo().getTriple().str())
135 return false;
136
137 Reader.Diag(diag::warn_pch_target_triple)
138 << Triple << PP.getTargetInfo().getTriple().str();
139 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000140}
141
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000142bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000143 FileID PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000144 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000145 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000146 // We are in the context of an implicit include, so the predefines buffer will
147 // have a #include entry for the PCH file itself (as normalized by the
148 // preprocessor initialization). Find it and skip over it in the checking
149 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000150 llvm::SmallString<256> PCHInclude;
151 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000152 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000153 PCHInclude += "\"\n";
154 std::pair<llvm::StringRef,llvm::StringRef> Split =
155 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
156 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000157 if (Left == PP.getPredefines()) {
158 Error("Missing PCH include entry!");
159 return true;
160 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000161
162 // If the predefines is equal to the joined left and right halves, we're done!
163 if (Left.size() + Right.size() == PCHPredef.size() &&
164 PCHPredef.startswith(Left) && PCHPredef.endswith(Right))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000165 return false;
166
167 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000169 // The predefines buffers are different. Determine what the differences are,
170 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000171 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
172 PCHPredef.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
173
174 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
175 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
176 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000177
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000178 // Sort both sets of predefined buffer lines, since we allow some extra
179 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000180 std::sort(CmdLineLines.begin(), CmdLineLines.end());
181 std::sort(PCHLines.begin(), PCHLines.end());
182
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000183 // Determine which predefines that were used to build the PCH file are missing
184 // from the command line.
185 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000186 std::set_difference(PCHLines.begin(), PCHLines.end(),
187 CmdLineLines.begin(), CmdLineLines.end(),
188 std::back_inserter(MissingPredefines));
189
190 bool MissingDefines = false;
191 bool ConflictingDefines = false;
192 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000193 llvm::StringRef Missing = MissingPredefines[I];
194 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000195 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
196 return true;
197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000199 // This is a macro definition. Determine the name of the macro we're
200 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000201 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000202 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000203 = Missing.find_first_of("( \n\r", StartOfMacroName);
204 assert(EndOfMacroName != std::string::npos &&
205 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000206 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000207
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000208 // Determine whether this macro was given a different definition on the
209 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000210 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000211 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000212 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000213 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
214 MacroDefStart);
215 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000216 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000217 // Different macro; we're done.
218 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000219 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
222 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000223 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000224 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000225 (*ConflictPos)[MacroDefLen] != '(')
226 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000228 // We found a conflicting macro definition.
229 break;
230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000232 if (ConflictPos != CmdLineLines.end()) {
233 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
234 << MacroName;
235
236 // Show the definition of this macro within the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000237 llvm::StringRef::size_type Offset = PCHPredef.find(Missing);
238 assert(Offset != llvm::StringRef::npos && "Unable to find macro!");
239 SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID)
240 .getFileLocWithOffset(Offset);
241 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000242
243 ConflictingDefines = true;
244 continue;
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000247 // If the macro doesn't conflict, then we'll just pick up the macro
248 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000249 if (ConflictingDefines)
250 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000252 if (!MissingDefines) {
253 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
254 MissingDefines = true;
255 }
256
257 // Show the definition of this macro within the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000258 llvm::StringRef::size_type Offset = PCHPredef.find(Missing);
259 assert(Offset != llvm::StringRef::npos && "Unable to find macro!");
260 SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000261 .getFileLocWithOffset(Offset);
262 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000265 if (ConflictingDefines)
266 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000268 // Determine what predefines were introduced based on command-line
269 // parameters that were not present when building the PCH
270 // file. Extra #defines are okay, so long as the identifiers being
271 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000272 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000273 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
274 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000275 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000276 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000277 llvm::StringRef &Extra = ExtraPredefines[I];
278 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000279 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
280 return true;
281 }
282
283 // This is an extra macro definition. Determine the name of the
284 // macro we're defining.
285 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000286 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000287 = Extra.find_first_of("( \n\r", StartOfMacroName);
288 assert(EndOfMacroName != std::string::npos &&
289 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000290 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000291
292 // Check whether this name was used somewhere in the PCH file. If
293 // so, defining it as a macro could change behavior, so we reject
294 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000295 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000296 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000297 return true;
298 }
299
300 // Add this definition to the suggested predefines buffer.
301 SuggestedPredefines += Extra;
302 SuggestedPredefines += '\n';
303 }
304
305 // If we get here, it's because the predefines buffer had compatible
306 // contents. Accept the PCH file.
307 return false;
308}
309
Douglas Gregor12fab312010-03-16 16:35:32 +0000310void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
311 unsigned ID) {
312 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
313 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314}
315
316void PCHValidator::ReadCounter(unsigned Value) {
317 PP.setCounterValue(Value);
318}
319
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000320//===----------------------------------------------------------------------===//
Douglas Gregor668c1a42009-04-21 22:25:48 +0000321// PCH reader implementation
322//===----------------------------------------------------------------------===//
323
Mike Stump1eb44332009-09-09 15:08:12 +0000324PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context,
325 const char *isysroot)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000326 : Listener(new PCHValidator(PP, *this)), SourceMgr(PP.getSourceManager()),
327 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
Douglas Gregor52e71082009-10-16 18:18:30 +0000328 SemaObj(0), PP(&PP), Context(Context), StatCache(0), Consumer(0),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000329 IdentifierTableData(0), IdentifierLookupTable(0),
330 IdentifierOffsets(0),
331 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
332 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000333 TotalNumSelectors(0), MacroDefinitionOffsets(0),
Douglas Gregorc6fbbed2010-03-19 22:13:20 +0000334 NumPreallocatedPreprocessingEntities(0),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000335 isysroot(isysroot), NumStatHits(0), NumStatMisses(0),
Mike Stump1eb44332009-09-09 15:08:12 +0000336 NumSLocEntriesRead(0), NumStatementsRead(0),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
Douglas Gregore650c8c2009-07-07 00:12:59 +0000338 NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0),
Mike Stump1eb44332009-09-09 15:08:12 +0000339 CurrentlyLoadingTypeOrDecl(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000340 RelocatablePCH = false;
341}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000342
343PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr,
Mike Stump1eb44332009-09-09 15:08:12 +0000344 Diagnostic &Diags, const char *isysroot)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 : SourceMgr(SourceMgr), FileMgr(FileMgr), Diags(Diags),
Douglas Gregor52e71082009-10-16 18:18:30 +0000346 SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0),
Chris Lattner4c6f9522009-04-27 05:14:47 +0000347 IdentifierTableData(0), IdentifierLookupTable(0),
348 IdentifierOffsets(0),
349 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
350 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000351 TotalNumSelectors(0), MacroDefinitionOffsets(0),
Douglas Gregorc6fbbed2010-03-19 22:13:20 +0000352 NumPreallocatedPreprocessingEntities(0),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000353 isysroot(isysroot), NumStatHits(0), NumStatMisses(0),
Mike Stump1eb44332009-09-09 15:08:12 +0000354 NumSLocEntriesRead(0), NumStatementsRead(0),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000355 NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
Douglas Gregord89275b2009-07-06 18:54:52 +0000356 NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0),
Mike Stump1eb44332009-09-09 15:08:12 +0000357 CurrentlyLoadingTypeOrDecl(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000358 RelocatablePCH = false;
359}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000360
361PCHReader::~PCHReader() {}
362
Chris Lattner4c6f9522009-04-27 05:14:47 +0000363
Douglas Gregor668c1a42009-04-21 22:25:48 +0000364namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +0000365class PCHMethodPoolLookupTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000366 PCHReader &Reader;
367
368public:
369 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
370
371 typedef Selector external_key_type;
372 typedef external_key_type internal_key_type;
373
374 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000376 static bool EqualKey(const internal_key_type& a,
377 const internal_key_type& b) {
378 return a == b;
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000381 static unsigned ComputeHash(Selector Sel) {
382 unsigned N = Sel.getNumArgs();
383 if (N == 0)
384 ++N;
385 unsigned R = 5381;
386 for (unsigned I = 0; I != N; ++I)
387 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +0000388 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000389 return R;
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000392 // This hopefully will just get inlined and removed by the optimizer.
393 static const internal_key_type&
394 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000396 static std::pair<unsigned, unsigned>
397 ReadKeyDataLength(const unsigned char*& d) {
398 using namespace clang::io;
399 unsigned KeyLen = ReadUnalignedLE16(d);
400 unsigned DataLen = ReadUnalignedLE16(d);
401 return std::make_pair(KeyLen, DataLen);
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Douglas Gregor83941df2009-04-25 17:48:32 +0000404 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000405 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000406 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000407 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000408 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000409 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
410 if (N == 0)
411 return SelTable.getNullarySelector(FirstII);
412 else if (N == 1)
413 return SelTable.getUnarySelector(FirstII);
414
415 llvm::SmallVector<IdentifierInfo *, 16> Args;
416 Args.push_back(FirstII);
417 for (unsigned I = 1; I != N; ++I)
418 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
419
Douglas Gregor75fdb232009-05-22 22:45:36 +0000420 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000423 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
424 using namespace clang::io;
425 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
426 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
427
428 data_type Result;
429
430 // Load instance methods
431 ObjCMethodList *Prev = 0;
432 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000433 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000434 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
435 if (!Result.first.Method) {
436 // This is the first method, which is the easy case.
437 Result.first.Method = Method;
438 Prev = &Result.first;
439 continue;
440 }
441
Ted Kremenek298ed872010-02-11 00:53:01 +0000442 ObjCMethodList *Mem =
443 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
444 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000445 Prev = Prev->Next;
446 }
447
448 // Load factory methods
449 Prev = 0;
450 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000451 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000452 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
453 if (!Result.second.Method) {
454 // This is the first method, which is the easy case.
455 Result.second.Method = Method;
456 Prev = &Result.second;
457 continue;
458 }
459
Ted Kremenek298ed872010-02-11 00:53:01 +0000460 ObjCMethodList *Mem =
461 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
462 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000463 Prev = Prev->Next;
464 }
465
466 return Result;
467 }
468};
Mike Stump1eb44332009-09-09 15:08:12 +0000469
470} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000471
472/// \brief The on-disk hash table used for the global method pool.
Mike Stump1eb44332009-09-09 15:08:12 +0000473typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000474 PCHMethodPoolLookupTable;
475
476namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +0000477class PCHIdentifierLookupTrait {
Douglas Gregor668c1a42009-04-21 22:25:48 +0000478 PCHReader &Reader;
479
480 // If we know the IdentifierInfo in advance, it is here and we will
481 // not build a new one. Used when deserializing information about an
482 // identifier that was constructed before the PCH file was read.
483 IdentifierInfo *KnownII;
484
485public:
486 typedef IdentifierInfo * data_type;
487
488 typedef const std::pair<const char*, unsigned> external_key_type;
489
490 typedef external_key_type internal_key_type;
491
Mike Stump1eb44332009-09-09 15:08:12 +0000492 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
Douglas Gregor668c1a42009-04-21 22:25:48 +0000493 : Reader(Reader), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Douglas Gregor668c1a42009-04-21 22:25:48 +0000495 static bool EqualKey(const internal_key_type& a,
496 const internal_key_type& b) {
497 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
498 : false;
499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregor668c1a42009-04-21 22:25:48 +0000501 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000502 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Douglas Gregor668c1a42009-04-21 22:25:48 +0000505 // This hopefully will just get inlined and removed by the optimizer.
506 static const internal_key_type&
507 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Douglas Gregor668c1a42009-04-21 22:25:48 +0000509 static std::pair<unsigned, unsigned>
510 ReadKeyDataLength(const unsigned char*& d) {
511 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000512 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000513 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000514 return std::make_pair(KeyLen, DataLen);
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Douglas Gregor668c1a42009-04-21 22:25:48 +0000517 static std::pair<const char*, unsigned>
518 ReadKey(const unsigned char* d, unsigned n) {
519 assert(n >= 2 && d[n-1] == '\0');
520 return std::make_pair((const char*) d, n-1);
521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
523 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000524 const unsigned char* d,
525 unsigned DataLen) {
526 using namespace clang::io;
Douglas Gregora92193e2009-04-28 21:18:29 +0000527 pch::IdentID ID = ReadUnalignedLE32(d);
528 bool IsInteresting = ID & 0x01;
529
530 // Wipe out the "is interesting" bit.
531 ID = ID >> 1;
532
533 if (!IsInteresting) {
534 // For unintersting identifiers, just build the IdentifierInfo
535 // and associate it with the persistent ID.
536 IdentifierInfo *II = KnownII;
537 if (!II)
538 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
539 k.first, k.first + k.second);
540 Reader.SetIdentifierInfo(ID, II);
541 return II;
542 }
543
Douglas Gregor5998da52009-04-28 21:32:13 +0000544 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000545 bool CPlusPlusOperatorKeyword = Bits & 0x01;
546 Bits >>= 1;
547 bool Poisoned = Bits & 0x01;
548 Bits >>= 1;
549 bool ExtensionToken = Bits & 0x01;
550 Bits >>= 1;
551 bool hasMacroDefinition = Bits & 0x01;
552 Bits >>= 1;
553 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
554 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Douglas Gregor2deaea32009-04-22 18:49:13 +0000556 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000557 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000558
559 // Build the IdentifierInfo itself and link the identifier ID with
560 // the new IdentifierInfo.
561 IdentifierInfo *II = KnownII;
562 if (!II)
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000563 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
564 k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000565 Reader.SetIdentifierInfo(ID, II);
566
Douglas Gregor2deaea32009-04-22 18:49:13 +0000567 // Set or check the various bits in the IdentifierInfo structure.
568 // FIXME: Load token IDs lazily, too?
Douglas Gregor2deaea32009-04-22 18:49:13 +0000569 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000570 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000571 "Incorrect extension token flag");
572 (void)ExtensionToken;
573 II->setIsPoisoned(Poisoned);
574 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
575 "Incorrect C++ operator keyword flag");
576 (void)CPlusPlusOperatorKeyword;
577
Douglas Gregor37e26842009-04-21 23:56:24 +0000578 // If this identifier is a macro, deserialize the macro
579 // definition.
580 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000581 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor37e26842009-04-21 23:56:24 +0000582 Reader.ReadMacroRecord(Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000583 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000584 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000585
586 // Read all of the declarations visible at global scope with this
587 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000588 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000589 if (DataLen > 0) {
590 llvm::SmallVector<uint32_t, 4> DeclIDs;
591 for (; DataLen > 0; DataLen -= 4)
592 DeclIDs.push_back(ReadUnalignedLE32(d));
593 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Douglas Gregor668c1a42009-04-21 22:25:48 +0000596 return II;
597 }
598};
Mike Stump1eb44332009-09-09 15:08:12 +0000599
600} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000601
602/// \brief The on-disk hash table used to contain information about
603/// all of the identifiers in the program.
Mike Stump1eb44332009-09-09 15:08:12 +0000604typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
Douglas Gregor668c1a42009-04-21 22:25:48 +0000605 PCHIdentifierLookupTable;
606
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000607void PCHReader::Error(const char *Msg) {
608 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000609}
610
Douglas Gregore1d918e2009-04-10 23:10:45 +0000611/// \brief Check the contents of the predefines buffer against the
612/// contents of the predefines buffer used to build the PCH file.
613///
614/// The contents of the two predefines buffers should be the same. If
615/// not, then some command-line option changed the preprocessor state
616/// and we must reject the PCH file.
617///
618/// \param PCHPredef The start of the predefines buffer in the PCH
619/// file.
620///
621/// \param PCHPredefLen The length of the predefines buffer in the PCH
622/// file.
623///
624/// \param PCHBufferID The FileID for the PCH predefines buffer.
625///
626/// \returns true if there was a mismatch (in which case the PCH file
627/// should be ignored), or false otherwise.
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000628bool PCHReader::CheckPredefinesBuffer(llvm::StringRef PCHPredef,
Douglas Gregore1d918e2009-04-10 23:10:45 +0000629 FileID PCHBufferID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000630 if (Listener)
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000631 return Listener->ReadPredefinesBuffer(PCHPredef, PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000632 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000633 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000634 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000635}
636
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000637//===----------------------------------------------------------------------===//
638// Source Manager Deserialization
639//===----------------------------------------------------------------------===//
640
Douglas Gregorbd945002009-04-13 16:31:14 +0000641/// \brief Read the line table in the source manager block.
642/// \returns true if ther was an error.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000643bool PCHReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000644 unsigned Idx = 0;
645 LineTableInfo &LineTable = SourceMgr.getLineTable();
646
647 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000648 std::map<int, int> FileIDs;
649 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000650 // Extract the file name
651 unsigned FilenameLen = Record[Idx++];
652 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
653 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000654 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000655 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000656 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000657 }
658
659 // Parse the line entries
660 std::vector<LineEntry> Entries;
661 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000662 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000663
664 // Extract the line entries
665 unsigned NumEntries = Record[Idx++];
666 Entries.clear();
667 Entries.reserve(NumEntries);
668 for (unsigned I = 0; I != NumEntries; ++I) {
669 unsigned FileOffset = Record[Idx++];
670 unsigned LineNo = Record[Idx++];
671 int FilenameID = Record[Idx++];
Mike Stump1eb44332009-09-09 15:08:12 +0000672 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000673 = (SrcMgr::CharacteristicKind)Record[Idx++];
674 unsigned IncludeOffset = Record[Idx++];
675 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
676 FileKind, IncludeOffset));
677 }
678 LineTable.AddEntry(FID, Entries);
679 }
680
681 return false;
682}
683
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000684namespace {
685
Benjamin Kramerbd218282009-11-28 10:07:24 +0000686class PCHStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000687public:
688 const bool hasStat;
689 const ino_t ino;
690 const dev_t dev;
691 const mode_t mode;
692 const time_t mtime;
693 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000695 PCHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +0000696 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
697
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000698 PCHStatData()
699 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
700};
701
Benjamin Kramerbd218282009-11-28 10:07:24 +0000702class PCHStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000703 public:
704 typedef const char *external_key_type;
705 typedef const char *internal_key_type;
706
707 typedef PCHStatData data_type;
708
709 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000710 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000711 }
712
713 static internal_key_type GetInternalKey(const char *path) { return path; }
714
715 static bool EqualKey(internal_key_type a, internal_key_type b) {
716 return strcmp(a, b) == 0;
717 }
718
719 static std::pair<unsigned, unsigned>
720 ReadKeyDataLength(const unsigned char*& d) {
721 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
722 unsigned DataLen = (unsigned) *d++;
723 return std::make_pair(KeyLen + 1, DataLen);
724 }
725
726 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
727 return (const char *)d;
728 }
729
730 static data_type ReadData(const internal_key_type, const unsigned char *d,
731 unsigned /*DataLen*/) {
732 using namespace clang::io;
733
734 if (*d++ == 1)
735 return data_type();
736
737 ino_t ino = (ino_t) ReadUnalignedLE32(d);
738 dev_t dev = (dev_t) ReadUnalignedLE32(d);
739 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000740 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000741 off_t size = (off_t) ReadUnalignedLE64(d);
742 return data_type(ino, dev, mode, mtime, size);
743 }
744};
745
746/// \brief stat() cache for precompiled headers.
747///
748/// This cache is very similar to the stat cache used by pretokenized
749/// headers.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000750class PCHStatCache : public StatSysCallCache {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000751 typedef OnDiskChainedHashTable<PCHStatLookupTrait> CacheTy;
752 CacheTy *Cache;
753
754 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000755public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000756 PCHStatCache(const unsigned char *Buckets,
757 const unsigned char *Base,
758 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +0000759 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000760 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
761 Cache = CacheTy::Create(Buckets, Base);
762 }
763
764 ~PCHStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000766 int stat(const char *path, struct stat *buf) {
767 // Do the lookup for the file's data in the PCH file.
768 CacheTy::iterator I = Cache->find(path);
769
770 // If we don't get a hit in the PCH file just forward to 'stat'.
771 if (I == Cache->end()) {
772 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +0000773 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000776 ++NumStatHits;
777 PCHStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000779 if (!Data.hasStat)
780 return 1;
781
782 buf->st_ino = Data.ino;
783 buf->st_dev = Data.dev;
784 buf->st_mtime = Data.mtime;
785 buf->st_mode = Data.mode;
786 buf->st_size = Data.size;
787 return 0;
788 }
789};
790} // end anonymous namespace
791
792
Douglas Gregor14f79002009-04-10 03:52:48 +0000793/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000794PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000795 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000796
797 // Set the source-location entry cursor to the current position in
798 // the stream. This cursor will be used to read the contents of the
799 // source manager block initially, and then lazily read
800 // source-location entries as needed.
801 SLocEntryCursor = Stream;
802
803 // The stream itself is going to skip over the source manager block.
804 if (Stream.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000805 Error("malformed block record in PCH file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000806 return Failure;
807 }
808
809 // Enter the source manager block.
810 if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000811 Error("malformed source manager block record in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000812 return Failure;
813 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000814
Douglas Gregor14f79002009-04-10 03:52:48 +0000815 RecordData Record;
816 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000817 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000818 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000819 if (SLocEntryCursor.ReadBlockEnd()) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000820 Error("error at end of Source Manager block in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000821 return Failure;
822 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000823 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Douglas Gregor14f79002009-04-10 03:52:48 +0000826 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
827 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000828 SLocEntryCursor.ReadSubBlockID();
829 if (SLocEntryCursor.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000830 Error("malformed block record in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000831 return Failure;
832 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000833 continue;
834 }
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Douglas Gregor14f79002009-04-10 03:52:48 +0000836 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000837 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000838 continue;
839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Douglas Gregor14f79002009-04-10 03:52:48 +0000841 // Read a record.
842 const char *BlobStart;
843 unsigned BlobLen;
844 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000845 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000846 default: // Default behavior: ignore.
847 break;
848
Chris Lattner2c78b872009-04-14 23:22:57 +0000849 case pch::SM_LINE_TABLE:
Douglas Gregore650c8c2009-07-07 00:12:59 +0000850 if (ParseLineTable(Record))
Douglas Gregorbd945002009-04-13 16:31:14 +0000851 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000852 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000853
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000854 case pch::SM_SLOC_FILE_ENTRY:
855 case pch::SM_SLOC_BUFFER_ENTRY:
856 case pch::SM_SLOC_INSTANTIATION_ENTRY:
857 // Once we hit one of the source location entries, we're done.
858 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000859 }
860 }
861}
862
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000863/// \brief Read in the source location entry with the given ID.
864PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) {
865 if (ID == 0)
866 return Success;
867
868 if (ID > TotalNumSLocEntries) {
869 Error("source location entry ID out-of-range for PCH file");
870 return Failure;
871 }
872
873 ++NumSLocEntriesRead;
874 SLocEntryCursor.JumpToBit(SLocOffsets[ID - 1]);
875 unsigned Code = SLocEntryCursor.ReadCode();
876 if (Code == llvm::bitc::END_BLOCK ||
877 Code == llvm::bitc::ENTER_SUBBLOCK ||
878 Code == llvm::bitc::DEFINE_ABBREV) {
879 Error("incorrectly-formatted source location entry in PCH file");
880 return Failure;
881 }
882
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000883 RecordData Record;
884 const char *BlobStart;
885 unsigned BlobLen;
886 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
887 default:
888 Error("incorrectly-formatted source location entry in PCH file");
889 return Failure;
890
891 case pch::SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000892 std::string Filename(BlobStart, BlobStart + BlobLen);
893 MaybeAddSystemRootToFilename(Filename);
894 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +0000895 if (File == 0) {
896 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +0000897 ErrorStr += Filename;
Chris Lattnerd3555ae2009-06-15 04:35:16 +0000898 ErrorStr += "' referenced by PCH file";
899 Error(ErrorStr.c_str());
900 return Failure;
901 }
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregor2d52be52010-03-21 22:49:54 +0000903 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +0000904 Error("source location entry is incorrect");
905 return Failure;
906 }
907
Douglas Gregor9f692a02010-04-09 15:54:22 +0000908 if ((off_t)Record[4] != File->getSize()
909#if !defined(LLVM_ON_WIN32)
910 // In our regression testing, the Windows file system seems to
911 // have inconsistent modification times that sometimes
912 // erroneously trigger this error-handling path.
913 || (time_t)Record[5] != File->getModificationTime()
914#endif
915 ) {
Douglas Gregor2d52be52010-03-21 22:49:54 +0000916 Diag(diag::err_fe_pch_file_modified)
917 << Filename;
918 return Failure;
919 }
920
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000921 FileID FID = SourceMgr.createFileID(File,
922 SourceLocation::getFromRawEncoding(Record[1]),
923 (SrcMgr::CharacteristicKind)Record[2],
924 ID, Record[0]);
925 if (Record[3])
926 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
927 .setHasLineDirectives();
928
Douglas Gregor12fab312010-03-16 16:35:32 +0000929 // Reconstruct header-search information for this file.
930 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +0000931 HFI.isImport = Record[6];
932 HFI.DirInfo = Record[7];
933 HFI.NumIncludes = Record[8];
934 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +0000935 if (Listener)
936 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000937 break;
938 }
939
940 case pch::SM_SLOC_BUFFER_ENTRY: {
941 const char *Name = BlobStart;
942 unsigned Offset = Record[0];
943 unsigned Code = SLocEntryCursor.ReadCode();
944 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000945 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000946 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000947
948 if (RecCode != pch::SM_SLOC_BUFFER_BLOB) {
949 Error("PCH record has invalid code");
950 return Failure;
951 }
952
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000953 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +0000954 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
955 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000956 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor92b059e2009-04-28 20:33:11 +0000958 if (strcmp(Name, "<built-in>") == 0) {
959 PCHPredefinesBufferID = BufferID;
960 PCHPredefines = BlobStart;
961 PCHPredefinesLen = BlobLen - 1;
962 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000963
964 break;
965 }
966
967 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump1eb44332009-09-09 15:08:12 +0000968 SourceLocation SpellingLoc
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000969 = SourceLocation::getFromRawEncoding(Record[1]);
970 SourceMgr.createInstantiationLoc(SpellingLoc,
971 SourceLocation::getFromRawEncoding(Record[2]),
972 SourceLocation::getFromRawEncoding(Record[3]),
973 Record[4],
974 ID,
975 Record[0]);
976 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000977 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000978 }
979
980 return Success;
981}
982
Chris Lattner6367f6d2009-04-27 01:05:14 +0000983/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
984/// specified cursor. Read the abbreviations that are at the top of the block
985/// and then leave the cursor pointing into the block.
986bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
987 unsigned BlockID) {
988 if (Cursor.EnterSubBlock(BlockID)) {
Douglas Gregora02b1472009-04-28 21:53:25 +0000989 Error("malformed block record in PCH file");
Chris Lattner6367f6d2009-04-27 01:05:14 +0000990 return Failure;
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Chris Lattner6367f6d2009-04-27 01:05:14 +0000993 while (true) {
994 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattner6367f6d2009-04-27 01:05:14 +0000996 // We expect all abbrevs to be at the start of the block.
997 if (Code != llvm::bitc::DEFINE_ABBREV)
998 return false;
999 Cursor.ReadAbbrevRecord();
1000 }
1001}
1002
Douglas Gregor37e26842009-04-21 23:56:24 +00001003void PCHReader::ReadMacroRecord(uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001004 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Douglas Gregor37e26842009-04-21 23:56:24 +00001006 // Keep track of where we are in the stream, then jump back there
1007 // after reading this macro.
1008 SavedStreamPosition SavedPosition(Stream);
1009
1010 Stream.JumpToBit(Offset);
1011 RecordData Record;
1012 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1013 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregor37e26842009-04-21 23:56:24 +00001015 while (true) {
1016 unsigned Code = Stream.ReadCode();
1017 switch (Code) {
1018 case llvm::bitc::END_BLOCK:
1019 return;
1020
1021 case llvm::bitc::ENTER_SUBBLOCK:
1022 // No known subblocks, always skip them.
1023 Stream.ReadSubBlockID();
1024 if (Stream.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001025 Error("malformed block record in PCH file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001026 return;
1027 }
1028 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Douglas Gregor37e26842009-04-21 23:56:24 +00001030 case llvm::bitc::DEFINE_ABBREV:
1031 Stream.ReadAbbrevRecord();
1032 continue;
1033 default: break;
1034 }
1035
1036 // Read a record.
1037 Record.clear();
1038 pch::PreprocessorRecordTypes RecType =
1039 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1040 switch (RecType) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001041 case pch::PP_MACRO_OBJECT_LIKE:
1042 case pch::PP_MACRO_FUNCTION_LIKE: {
1043 // If we already have a macro, that means that we've hit the end
1044 // of the definition of the macro we were looking for. We're
1045 // done.
1046 if (Macro)
1047 return;
1048
1049 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1050 if (II == 0) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001051 Error("macro must have a name in PCH file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001052 return;
1053 }
1054 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1055 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001057 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001058 MI->setIsUsed(isUsed);
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001060 unsigned NextIndex = 3;
Douglas Gregor37e26842009-04-21 23:56:24 +00001061 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1062 // Decode function-like macro info.
1063 bool isC99VarArgs = Record[3];
1064 bool isGNUVarArgs = Record[4];
1065 MacroArgs.clear();
1066 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001067 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001068 for (unsigned i = 0; i != NumArgs; ++i)
1069 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1070
1071 // Install function-like macro info.
1072 MI->setIsFunctionLike();
1073 if (isC99VarArgs) MI->setIsC99Varargs();
1074 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001075 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001076 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001077 }
1078
1079 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001080 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001081
1082 // Remember that we saw this macro last so that we add the tokens that
1083 // form its body to it.
1084 Macro = MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001085
1086 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1087 // We have a macro definition. Load it now.
1088 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1089 getMacroDefinition(Record[NextIndex]));
1090 }
1091
Douglas Gregor37e26842009-04-21 23:56:24 +00001092 ++NumMacrosRead;
1093 break;
1094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Douglas Gregor37e26842009-04-21 23:56:24 +00001096 case pch::PP_TOKEN: {
1097 // If we see a TOKEN before a PP_MACRO_*, then the file is
1098 // erroneous, just pretend we didn't see this.
1099 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregor37e26842009-04-21 23:56:24 +00001101 Token Tok;
1102 Tok.startToken();
1103 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1104 Tok.setLength(Record[1]);
1105 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1106 Tok.setIdentifierInfo(II);
1107 Tok.setKind((tok::TokenKind)Record[3]);
1108 Tok.setFlag((Token::TokenFlags)Record[4]);
1109 Macro->AddTokenToBody(Tok);
1110 break;
1111 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001112
1113 case pch::PP_MACRO_INSTANTIATION: {
1114 // If we already have a macro, that means that we've hit the end
1115 // of the definition of the macro we were looking for. We're
1116 // done.
1117 if (Macro)
1118 return;
1119
1120 if (!PP->getPreprocessingRecord()) {
1121 Error("missing preprocessing record in PCH file");
1122 return;
1123 }
1124
1125 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1126 if (PPRec.getPreprocessedEntity(Record[0]))
1127 return;
1128
1129 MacroInstantiation *MI
1130 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1131 SourceRange(
1132 SourceLocation::getFromRawEncoding(Record[1]),
1133 SourceLocation::getFromRawEncoding(Record[2])),
1134 getMacroDefinition(Record[4]));
1135 PPRec.SetPreallocatedEntity(Record[0], MI);
1136 return;
1137 }
1138
1139 case pch::PP_MACRO_DEFINITION: {
1140 // If we already have a macro, that means that we've hit the end
1141 // of the definition of the macro we were looking for. We're
1142 // done.
1143 if (Macro)
1144 return;
1145
1146 if (!PP->getPreprocessingRecord()) {
1147 Error("missing preprocessing record in PCH file");
1148 return;
1149 }
1150
1151 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1152 if (PPRec.getPreprocessedEntity(Record[0]))
1153 return;
1154
1155 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1156 Error("out-of-bounds macro definition record");
1157 return;
1158 }
1159
1160 MacroDefinition *MD
1161 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1162 SourceLocation::getFromRawEncoding(Record[5]),
1163 SourceRange(
1164 SourceLocation::getFromRawEncoding(Record[2]),
1165 SourceLocation::getFromRawEncoding(Record[3])));
1166 PPRec.SetPreallocatedEntity(Record[0], MD);
1167 MacroDefinitionsLoaded[Record[1]] = MD;
1168 return;
1169 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001170 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001171 }
1172}
1173
Douglas Gregor88a35862010-01-04 19:18:44 +00001174void PCHReader::ReadDefinedMacros() {
1175 // If there was no preprocessor block, do nothing.
1176 if (!MacroCursor.getBitStreamReader())
1177 return;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001178
Douglas Gregor88a35862010-01-04 19:18:44 +00001179 llvm::BitstreamCursor Cursor = MacroCursor;
1180 if (Cursor.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) {
1181 Error("malformed preprocessor block record in PCH file");
1182 return;
1183 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001184
Douglas Gregor88a35862010-01-04 19:18:44 +00001185 RecordData Record;
1186 while (true) {
1187 unsigned Code = Cursor.ReadCode();
1188 if (Code == llvm::bitc::END_BLOCK) {
1189 if (Cursor.ReadBlockEnd())
1190 Error("error at end of preprocessor block in PCH file");
1191 return;
1192 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001193
Douglas Gregor88a35862010-01-04 19:18:44 +00001194 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1195 // No known subblocks, always skip them.
1196 Cursor.ReadSubBlockID();
1197 if (Cursor.SkipBlock()) {
1198 Error("malformed block record in PCH file");
1199 return;
1200 }
1201 continue;
1202 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001203
Douglas Gregor88a35862010-01-04 19:18:44 +00001204 if (Code == llvm::bitc::DEFINE_ABBREV) {
1205 Cursor.ReadAbbrevRecord();
1206 continue;
1207 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001208
Douglas Gregor88a35862010-01-04 19:18:44 +00001209 // Read a record.
1210 const char *BlobStart;
1211 unsigned BlobLen;
1212 Record.clear();
1213 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1214 default: // Default behavior: ignore.
1215 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001216
Douglas Gregor88a35862010-01-04 19:18:44 +00001217 case pch::PP_MACRO_OBJECT_LIKE:
1218 case pch::PP_MACRO_FUNCTION_LIKE:
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001219 DecodeIdentifierInfo(Record[0]);
Douglas Gregor88a35862010-01-04 19:18:44 +00001220 break;
1221
1222 case pch::PP_TOKEN:
1223 // Ignore tokens.
1224 break;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001225
1226 case pch::PP_MACRO_INSTANTIATION:
1227 case pch::PP_MACRO_DEFINITION:
1228 // Read the macro record.
1229 ReadMacroRecord(Cursor.GetCurrentBitNo());
1230 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001231 }
1232 }
1233}
1234
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001235MacroDefinition *PCHReader::getMacroDefinition(pch::IdentID ID) {
1236 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1237 return 0;
1238
1239 if (!MacroDefinitionsLoaded[ID])
1240 ReadMacroRecord(MacroDefinitionOffsets[ID]);
1241
1242 return MacroDefinitionsLoaded[ID];
1243}
1244
Douglas Gregore650c8c2009-07-07 00:12:59 +00001245/// \brief If we are loading a relocatable PCH file, and the filename is
1246/// not an absolute path, add the system root to the beginning of the file
1247/// name.
1248void PCHReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1249 // If this is not a relocatable PCH file, there's nothing to do.
1250 if (!RelocatablePCH)
1251 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Daniel Dunbard5b21972009-11-18 19:50:41 +00001253 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001254 return;
1255
Douglas Gregore650c8c2009-07-07 00:12:59 +00001256 if (isysroot == 0) {
1257 // If no system root was given, default to '/'
1258 Filename.insert(Filename.begin(), '/');
1259 return;
1260 }
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Douglas Gregore650c8c2009-07-07 00:12:59 +00001262 unsigned Length = strlen(isysroot);
1263 if (isysroot[Length - 1] != '/')
1264 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Douglas Gregore650c8c2009-07-07 00:12:59 +00001266 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1267}
1268
Mike Stump1eb44332009-09-09 15:08:12 +00001269PCHReader::PCHReadResult
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001270PCHReader::ReadPCHBlock() {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001271 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001272 Error("malformed block record in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001273 return Failure;
1274 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001275
1276 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001277 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001278 while (!Stream.AtEndOfStream()) {
1279 unsigned Code = Stream.ReadCode();
1280 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001281 if (Stream.ReadBlockEnd()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001282 Error("error at end of module block in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001283 return Failure;
1284 }
Chris Lattner7356a312009-04-11 21:15:38 +00001285
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001286 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001287 }
1288
1289 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1290 switch (Stream.ReadSubBlockID()) {
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001291 case pch::DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001292 // We lazily load the decls block, but we want to set up the
1293 // DeclsCursor cursor to point into it. Clone our current bitcode
1294 // cursor to it, enter the block and read the abbrevs in that block.
1295 // With the main cursor, we just skip over it.
1296 DeclsCursor = Stream;
1297 if (Stream.SkipBlock() || // Skip with the main cursor.
1298 // Read the abbrevs.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001299 ReadBlockAbbrevs(DeclsCursor, pch::DECLTYPES_BLOCK_ID)) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001300 Error("malformed block record in PCH file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001301 return Failure;
1302 }
1303 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Chris Lattner7356a312009-04-11 21:15:38 +00001305 case pch::PREPROCESSOR_BLOCK_ID:
Douglas Gregor88a35862010-01-04 19:18:44 +00001306 MacroCursor = Stream;
1307 if (PP)
1308 PP->setExternalSource(this);
1309
Chris Lattner7356a312009-04-11 21:15:38 +00001310 if (Stream.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001311 Error("malformed block record in PCH file");
Chris Lattner7356a312009-04-11 21:15:38 +00001312 return Failure;
1313 }
1314 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001315
Douglas Gregor14f79002009-04-10 03:52:48 +00001316 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001317 switch (ReadSourceManagerBlock()) {
1318 case Success:
1319 break;
1320
1321 case Failure:
Douglas Gregora02b1472009-04-28 21:53:25 +00001322 Error("malformed source manager block in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001323 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001324
1325 case IgnorePCH:
1326 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001327 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001328 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001329 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001330 continue;
1331 }
1332
1333 if (Code == llvm::bitc::DEFINE_ABBREV) {
1334 Stream.ReadAbbrevRecord();
1335 continue;
1336 }
1337
1338 // Read and process a record.
1339 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001340 const char *BlobStart = 0;
1341 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001342 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregor2bec0412009-04-10 21:16:55 +00001343 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001344 default: // Default behavior: ignore.
1345 break;
1346
1347 case pch::TYPE_OFFSET:
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001348 if (!TypesLoaded.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001349 Error("duplicate TYPE_OFFSET record in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001350 return Failure;
1351 }
Chris Lattnerc732f5a2009-04-27 18:24:17 +00001352 TypeOffsets = (const uint32_t *)BlobStart;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001353 TypesLoaded.resize(Record[0]);
Douglas Gregor8038d512009-04-10 17:25:41 +00001354 break;
1355
1356 case pch::DECL_OFFSET:
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001357 if (!DeclsLoaded.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001358 Error("duplicate DECL_OFFSET record in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001359 return Failure;
1360 }
Chris Lattnerc732f5a2009-04-27 18:24:17 +00001361 DeclOffsets = (const uint32_t *)BlobStart;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001362 DeclsLoaded.resize(Record[0]);
Douglas Gregor8038d512009-04-10 17:25:41 +00001363 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001364
1365 case pch::LANGUAGE_OPTIONS:
1366 if (ParseLanguageOptions(Record))
1367 return IgnorePCH;
1368 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001369
Douglas Gregorab41e632009-04-27 22:23:34 +00001370 case pch::METADATA: {
1371 if (Record[0] != pch::VERSION_MAJOR) {
1372 Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
1373 : diag::warn_pch_version_too_new);
1374 return IgnorePCH;
1375 }
1376
Douglas Gregore650c8c2009-07-07 00:12:59 +00001377 RelocatablePCH = Record[4];
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001378 if (Listener) {
1379 std::string TargetTriple(BlobStart, BlobLen);
1380 if (Listener->ReadTargetTriple(TargetTriple))
1381 return IgnorePCH;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001382 }
1383 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001384 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001385
1386 case pch::IDENTIFIER_TABLE:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001387 IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001388 if (Record[0]) {
Mike Stump1eb44332009-09-09 15:08:12 +00001389 IdentifierLookupTable
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001390 = PCHIdentifierLookupTable::Create(
Douglas Gregor668c1a42009-04-21 22:25:48 +00001391 (const unsigned char *)IdentifierTableData + Record[0],
Mike Stump1eb44332009-09-09 15:08:12 +00001392 (const unsigned char *)IdentifierTableData,
Douglas Gregor668c1a42009-04-21 22:25:48 +00001393 PCHIdentifierLookupTrait(*this));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001394 if (PP)
1395 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001396 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001397 break;
1398
1399 case pch::IDENTIFIER_OFFSET:
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001400 if (!IdentifiersLoaded.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001401 Error("duplicate IDENTIFIER_OFFSET record in PCH file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001402 return Failure;
1403 }
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001404 IdentifierOffsets = (const uint32_t *)BlobStart;
1405 IdentifiersLoaded.resize(Record[0]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001406 if (PP)
1407 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001408 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001409
1410 case pch::EXTERNAL_DEFINITIONS:
1411 if (!ExternalDefinitions.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001412 Error("duplicate EXTERNAL_DEFINITIONS record in PCH file");
Douglas Gregorfdd01722009-04-14 00:24:19 +00001413 return Failure;
1414 }
1415 ExternalDefinitions.swap(Record);
1416 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001417
Douglas Gregorad1de002009-04-18 05:55:16 +00001418 case pch::SPECIAL_TYPES:
1419 SpecialTypes.swap(Record);
1420 break;
1421
Douglas Gregor3e1af842009-04-17 22:13:46 +00001422 case pch::STATISTICS:
1423 TotalNumStatements = Record[0];
Douglas Gregor37e26842009-04-21 23:56:24 +00001424 TotalNumMacros = Record[1];
Douglas Gregor25123082009-04-22 22:34:57 +00001425 TotalLexicalDeclContexts = Record[2];
1426 TotalVisibleDeclContexts = Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001427 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001428
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001429 case pch::TENTATIVE_DEFINITIONS:
1430 if (!TentativeDefinitions.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001431 Error("duplicate TENTATIVE_DEFINITIONS record in PCH file");
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001432 return Failure;
1433 }
1434 TentativeDefinitions.swap(Record);
1435 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001436
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001437 case pch::UNUSED_STATIC_FUNCS:
1438 if (!UnusedStaticFuncs.empty()) {
1439 Error("duplicate UNUSED_STATIC_FUNCS record in PCH file");
1440 return Failure;
1441 }
1442 UnusedStaticFuncs.swap(Record);
1443 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001444
Douglas Gregor14c22f22009-04-22 22:18:58 +00001445 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1446 if (!LocallyScopedExternalDecls.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001447 Error("duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
Douglas Gregor14c22f22009-04-22 22:18:58 +00001448 return Failure;
1449 }
1450 LocallyScopedExternalDecls.swap(Record);
1451 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001452
Douglas Gregor83941df2009-04-25 17:48:32 +00001453 case pch::SELECTOR_OFFSETS:
1454 SelectorOffsets = (const uint32_t *)BlobStart;
1455 TotalNumSelectors = Record[0];
1456 SelectorsLoaded.resize(TotalNumSelectors);
1457 break;
1458
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001459 case pch::METHOD_POOL:
Douglas Gregor83941df2009-04-25 17:48:32 +00001460 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1461 if (Record[0])
Mike Stump1eb44332009-09-09 15:08:12 +00001462 MethodPoolLookupTable
Douglas Gregor83941df2009-04-25 17:48:32 +00001463 = PCHMethodPoolLookupTable::Create(
1464 MethodPoolLookupTableData + Record[0],
Mike Stump1eb44332009-09-09 15:08:12 +00001465 MethodPoolLookupTableData,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001466 PCHMethodPoolLookupTrait(*this));
Douglas Gregor83941df2009-04-25 17:48:32 +00001467 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001468 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001469
1470 case pch::PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001471 if (!Record.empty() && Listener)
1472 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001473 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001474
1475 case pch::SOURCE_LOCATION_OFFSETS:
Chris Lattner090d9b52009-04-27 19:01:47 +00001476 SLocOffsets = (const uint32_t *)BlobStart;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001477 TotalNumSLocEntries = Record[0];
Douglas Gregor445e23e2009-10-05 21:07:28 +00001478 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001479 break;
1480
1481 case pch::SOURCE_LOCATION_PRELOADS:
1482 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
1483 PCHReadResult Result = ReadSLocEntryRecord(Record[I]);
1484 if (Result != Success)
1485 return Result;
1486 }
1487 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001488
Douglas Gregor52e71082009-10-16 18:18:30 +00001489 case pch::STAT_CACHE: {
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001490 PCHStatCache *MyStatCache =
Douglas Gregor52e71082009-10-16 18:18:30 +00001491 new PCHStatCache((const unsigned char *)BlobStart + Record[0],
1492 (const unsigned char *)BlobStart,
1493 NumStatHits, NumStatMisses);
1494 FileMgr.addStatCache(MyStatCache);
1495 StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001496 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001497 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001498
Douglas Gregorb81c1702009-04-27 20:06:05 +00001499 case pch::EXT_VECTOR_DECLS:
1500 if (!ExtVectorDecls.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001501 Error("duplicate EXT_VECTOR_DECLS record in PCH file");
Douglas Gregorb81c1702009-04-27 20:06:05 +00001502 return Failure;
1503 }
1504 ExtVectorDecls.swap(Record);
1505 break;
1506
Douglas Gregorb64c1932009-05-12 01:31:05 +00001507 case pch::ORIGINAL_FILE_NAME:
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001508 ActualOriginalFileName.assign(BlobStart, BlobLen);
1509 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001510 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001511 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Ted Kremenek5b4ec632010-01-22 20:59:36 +00001513 case pch::VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001514 const std::string &CurBranch = getClangFullRepositoryVersion();
Ted Kremenek517e6762010-01-22 20:55:35 +00001515 llvm::StringRef PCHBranch(BlobStart, BlobLen);
Ted Kremenek974be4d2010-02-12 23:31:14 +00001516 if (llvm::StringRef(CurBranch) != PCHBranch) {
Douglas Gregor445e23e2009-10-05 21:07:28 +00001517 Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch;
1518 return IgnorePCH;
1519 }
1520 break;
1521 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001522
1523 case pch::MACRO_DEFINITION_OFFSETS:
1524 MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1525 if (PP) {
1526 if (!PP->getPreprocessingRecord())
1527 PP->createPreprocessingRecord();
1528 PP->getPreprocessingRecord()->SetExternalSource(*this, Record[0]);
1529 } else {
1530 NumPreallocatedPreprocessingEntities = Record[0];
1531 }
1532
1533 MacroDefinitionsLoaded.resize(Record[1]);
1534 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001535 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001536 }
Douglas Gregora02b1472009-04-28 21:53:25 +00001537 Error("premature end of bitstream in PCH file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001538 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001539}
1540
Douglas Gregore1d918e2009-04-10 23:10:45 +00001541PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001542 // Set the PCH file name.
1543 this->FileName = FileName;
1544
Douglas Gregor2cf26342009-04-09 22:27:44 +00001545 // Open the PCH file.
Daniel Dunbarf3c740e2009-09-22 05:38:01 +00001546 //
1547 // FIXME: This shouldn't be here, we should just take a raw_ostream.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001548 std::string ErrStr;
Daniel Dunbar731ad8f2009-11-10 00:46:19 +00001549 Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001550 if (!Buffer) {
1551 Error(ErrStr.c_str());
1552 return IgnorePCH;
1553 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001554
1555 // Initialize the stream
Mike Stump1eb44332009-09-09 15:08:12 +00001556 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Chris Lattnerb9fa9172009-04-26 20:59:20 +00001557 (const unsigned char *)Buffer->getBufferEnd());
1558 Stream.init(StreamFile);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001559
1560 // Sniff for the signature.
1561 if (Stream.Read(8) != 'C' ||
1562 Stream.Read(8) != 'P' ||
1563 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001564 Stream.Read(8) != 'H') {
Douglas Gregora02b1472009-04-28 21:53:25 +00001565 Diag(diag::err_not_a_pch_file) << FileName;
1566 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001567 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001568
Douglas Gregor2cf26342009-04-09 22:27:44 +00001569 while (!Stream.AtEndOfStream()) {
1570 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Douglas Gregore1d918e2009-04-10 23:10:45 +00001572 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001573 Error("invalid record at top-level of PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001574 return Failure;
1575 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001576
1577 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001578
Douglas Gregor2cf26342009-04-09 22:27:44 +00001579 // We only know the PCH subblock ID.
1580 switch (BlockID) {
1581 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001582 if (Stream.ReadBlockInfoBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001583 Error("malformed BlockInfoBlock in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001584 return Failure;
1585 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001586 break;
1587 case pch::PCH_BLOCK_ID:
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001588 switch (ReadPCHBlock()) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001589 case Success:
1590 break;
1591
1592 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001593 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001594
1595 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001596 // FIXME: We could consider reading through to the end of this
1597 // PCH block, skipping subblocks, to see if there are other
1598 // PCH blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001599
1600 // Clear out any preallocated source location entries, so that
1601 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001602 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001603
1604 // Remove the stat cache.
Douglas Gregor52e71082009-10-16 18:18:30 +00001605 if (StatCache)
1606 FileMgr.removeStatCache((PCHStatCache*)StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001607
Douglas Gregore1d918e2009-04-10 23:10:45 +00001608 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001609 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001610 break;
1611 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001612 if (Stream.SkipBlock()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00001613 Error("malformed block record in PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001614 return Failure;
1615 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001616 break;
1617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618 }
1619
Douglas Gregor92b059e2009-04-28 20:33:11 +00001620 // Check the predefines buffer.
Daniel Dunbardc3c0d22009-11-11 00:52:11 +00001621 if (CheckPredefinesBuffer(llvm::StringRef(PCHPredefines, PCHPredefinesLen),
Douglas Gregor92b059e2009-04-28 20:33:11 +00001622 PCHPredefinesBufferID))
1623 return IgnorePCH;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001625 if (PP) {
Zhongxing Xu08996212009-07-18 09:26:51 +00001626 // Initialization of keywords and pragmas occurs before the
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001627 // PCH file is read, so there may be some identifiers that were
1628 // loaded into the IdentifierTable before we intercepted the
1629 // creation of identifiers. Iterate through the list of known
1630 // identifiers and determine whether we have to establish
1631 // preprocessor definitions or top-level identifier declaration
1632 // chains for those identifiers.
1633 //
1634 // We copy the IdentifierInfo pointers to a small vector first,
1635 // since de-serializing declarations or macro definitions can add
1636 // new entries into the identifier table, invalidating the
1637 // iterators.
1638 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1639 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1640 IdEnd = PP->getIdentifierTable().end();
1641 Id != IdEnd; ++Id)
1642 Identifiers.push_back(Id->second);
Mike Stump1eb44332009-09-09 15:08:12 +00001643 PCHIdentifierLookupTable *IdTable
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001644 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1645 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1646 IdentifierInfo *II = Identifiers[I];
1647 // Look in the on-disk hash table for an entry for
1648 PCHIdentifierLookupTrait Info(*this, II);
Daniel Dunbare013d682009-10-18 20:26:12 +00001649 std::pair<const char*, unsigned> Key(II->getNameStart(), II->getLength());
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001650 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1651 if (Pos == IdTable->end())
1652 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001654 // Dereferencing the iterator has the effect of populating the
1655 // IdentifierInfo node with the various declarations it needs.
1656 (void)*Pos;
1657 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001658 }
1659
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001660 if (Context)
1661 InitializeContext(*Context);
Douglas Gregor0b748912009-04-14 21:18:50 +00001662
Douglas Gregor668c1a42009-04-21 22:25:48 +00001663 return Success;
Douglas Gregor0b748912009-04-14 21:18:50 +00001664}
1665
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001666void PCHReader::setPreprocessor(Preprocessor &pp) {
1667 PP = &pp;
1668
1669 if (NumPreallocatedPreprocessingEntities) {
1670 if (!PP->getPreprocessingRecord())
1671 PP->createPreprocessingRecord();
1672 PP->getPreprocessingRecord()->SetExternalSource(*this,
1673 NumPreallocatedPreprocessingEntities);
1674 NumPreallocatedPreprocessingEntities = 0;
1675 }
1676}
1677
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001678void PCHReader::InitializeContext(ASTContext &Ctx) {
1679 Context = &Ctx;
1680 assert(Context && "Passed null context!");
1681
1682 assert(PP && "Forgot to set Preprocessor ?");
1683 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1684 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001685 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001686
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001687 // Load the translation unit declaration
1688 ReadDeclRecord(DeclOffsets[0], 0);
1689
1690 // Load the special types.
1691 Context->setBuiltinVaListType(
1692 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1693 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
1694 Context->setObjCIdType(GetType(Id));
1695 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
1696 Context->setObjCSelType(GetType(Sel));
1697 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
1698 Context->setObjCProtoType(GetType(Proto));
1699 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
1700 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00001701
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001702 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
1703 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00001704 if (unsigned FastEnum
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001705 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
1706 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001707 if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) {
1708 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001709 if (FileType.isNull()) {
1710 Error("FILE type is NULL");
1711 return;
1712 }
John McCall183700f2009-09-21 23:43:11 +00001713 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001714 Context->setFILEDecl(Typedef->getDecl());
1715 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001716 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001717 if (!Tag) {
1718 Error("Invalid FILE type in PCH file");
1719 return;
1720 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001721 Context->setFILEDecl(Tag->getDecl());
1722 }
1723 }
Mike Stump782fa302009-07-28 02:25:19 +00001724 if (unsigned Jmp_buf = SpecialTypes[pch::SPECIAL_TYPE_jmp_buf]) {
1725 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001726 if (Jmp_bufType.isNull()) {
1727 Error("jmp_bug type is NULL");
1728 return;
1729 }
John McCall183700f2009-09-21 23:43:11 +00001730 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00001731 Context->setjmp_bufDecl(Typedef->getDecl());
1732 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001733 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001734 if (!Tag) {
1735 Error("Invalid jmp_bug type in PCH file");
1736 return;
1737 }
Mike Stump782fa302009-07-28 02:25:19 +00001738 Context->setjmp_bufDecl(Tag->getDecl());
1739 }
1740 }
1741 if (unsigned Sigjmp_buf = SpecialTypes[pch::SPECIAL_TYPE_sigjmp_buf]) {
1742 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001743 if (Sigjmp_bufType.isNull()) {
1744 Error("sigjmp_buf type is NULL");
1745 return;
1746 }
John McCall183700f2009-09-21 23:43:11 +00001747 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00001748 Context->setsigjmp_bufDecl(Typedef->getDecl());
1749 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001750 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Mike Stump782fa302009-07-28 02:25:19 +00001751 assert(Tag && "Invalid sigjmp_buf type in PCH file");
1752 Context->setsigjmp_bufDecl(Tag->getDecl());
1753 }
1754 }
Mike Stump1eb44332009-09-09 15:08:12 +00001755 if (unsigned ObjCIdRedef
Douglas Gregord1571ac2009-08-21 00:27:50 +00001756 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID_REDEFINITION])
1757 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00001758 if (unsigned ObjCClassRedef
Douglas Gregord1571ac2009-08-21 00:27:50 +00001759 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
1760 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Mike Stumpadaaad32009-10-20 02:12:22 +00001761 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR])
1762 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00001763 if (unsigned String
1764 = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
1765 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00001766 if (unsigned ObjCSelRedef
1767 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
1768 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
1769 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_NS_CONSTANT_STRING])
1770 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001771}
1772
Douglas Gregorb64c1932009-05-12 01:31:05 +00001773/// \brief Retrieve the name of the original source file name
1774/// directly from the PCH file, without actually loading the PCH
1775/// file.
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001776std::string PCHReader::getOriginalSourceFile(const std::string &PCHFileName,
1777 Diagnostic &Diags) {
Douglas Gregorb64c1932009-05-12 01:31:05 +00001778 // Open the PCH file.
1779 std::string ErrStr;
1780 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
1781 Buffer.reset(llvm::MemoryBuffer::getFile(PCHFileName.c_str(), &ErrStr));
1782 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001783 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001784 return std::string();
1785 }
1786
1787 // Initialize the stream
1788 llvm::BitstreamReader StreamFile;
1789 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00001790 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00001791 (const unsigned char *)Buffer->getBufferEnd());
1792 Stream.init(StreamFile);
1793
1794 // Sniff for the signature.
1795 if (Stream.Read(8) != 'C' ||
1796 Stream.Read(8) != 'P' ||
1797 Stream.Read(8) != 'C' ||
1798 Stream.Read(8) != 'H') {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001799 Diags.Report(diag::err_fe_not_a_pch_file) << PCHFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001800 return std::string();
1801 }
1802
1803 RecordData Record;
1804 while (!Stream.AtEndOfStream()) {
1805 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Douglas Gregorb64c1932009-05-12 01:31:05 +00001807 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1808 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Douglas Gregorb64c1932009-05-12 01:31:05 +00001810 // We only know the PCH subblock ID.
1811 switch (BlockID) {
1812 case pch::PCH_BLOCK_ID:
1813 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001814 Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001815 return std::string();
1816 }
1817 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Douglas Gregorb64c1932009-05-12 01:31:05 +00001819 default:
1820 if (Stream.SkipBlock()) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001821 Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001822 return std::string();
1823 }
1824 break;
1825 }
1826 continue;
1827 }
1828
1829 if (Code == llvm::bitc::END_BLOCK) {
1830 if (Stream.ReadBlockEnd()) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001831 Diags.Report(diag::err_fe_pch_error_at_end_block) << PCHFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00001832 return std::string();
1833 }
1834 continue;
1835 }
1836
1837 if (Code == llvm::bitc::DEFINE_ABBREV) {
1838 Stream.ReadAbbrevRecord();
1839 continue;
1840 }
1841
1842 Record.clear();
1843 const char *BlobStart = 0;
1844 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001845 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Douglas Gregorb64c1932009-05-12 01:31:05 +00001846 == pch::ORIGINAL_FILE_NAME)
1847 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00001848 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00001849
1850 return std::string();
1851}
1852
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001853/// \brief Parse the record that corresponds to a LangOptions data
1854/// structure.
1855///
1856/// This routine compares the language options used to generate the
1857/// PCH file against the language options set for the current
1858/// compilation. For each option, we classify differences between the
1859/// two compiler states as either "benign" or "important". Benign
1860/// differences don't matter, and we accept them without complaint
1861/// (and without modifying the language options). Differences between
1862/// the states for important options cause the PCH file to be
1863/// unusable, so we emit a warning and return true to indicate that
1864/// there was an error.
1865///
1866/// \returns true if the PCH file is unacceptable, false otherwise.
1867bool PCHReader::ParseLanguageOptions(
1868 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001869 if (Listener) {
1870 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001872 #define PARSE_LANGOPT(Option) \
1873 LangOpts.Option = Record[Idx]; \
1874 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001876 unsigned Idx = 0;
1877 PARSE_LANGOPT(Trigraphs);
1878 PARSE_LANGOPT(BCPLComment);
1879 PARSE_LANGOPT(DollarIdents);
1880 PARSE_LANGOPT(AsmPreprocessor);
1881 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001882 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001883 PARSE_LANGOPT(ImplicitInt);
1884 PARSE_LANGOPT(Digraphs);
1885 PARSE_LANGOPT(HexFloats);
1886 PARSE_LANGOPT(C99);
1887 PARSE_LANGOPT(Microsoft);
1888 PARSE_LANGOPT(CPlusPlus);
1889 PARSE_LANGOPT(CPlusPlus0x);
1890 PARSE_LANGOPT(CXXOperatorNames);
1891 PARSE_LANGOPT(ObjC1);
1892 PARSE_LANGOPT(ObjC2);
1893 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001894 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001895 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001896 PARSE_LANGOPT(PascalStrings);
1897 PARSE_LANGOPT(WritableStrings);
1898 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001899 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001900 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00001901 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001902 PARSE_LANGOPT(NeXTRuntime);
1903 PARSE_LANGOPT(Freestanding);
1904 PARSE_LANGOPT(NoBuiltin);
1905 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001906 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001907 PARSE_LANGOPT(Blocks);
1908 PARSE_LANGOPT(EmitAllDecls);
1909 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00001910 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
1911 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001912 PARSE_LANGOPT(HeinousExtensions);
1913 PARSE_LANGOPT(Optimize);
1914 PARSE_LANGOPT(OptimizeSize);
1915 PARSE_LANGOPT(Static);
1916 PARSE_LANGOPT(PICLevel);
1917 PARSE_LANGOPT(GNUInline);
1918 PARSE_LANGOPT(NoInline);
1919 PARSE_LANGOPT(AccessControl);
1920 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00001921 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00001922 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
1923 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001924 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00001925 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001926 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001927 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00001928 PARSE_LANGOPT(CatchUndefined);
1929 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001930 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001931
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001932 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001933 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001934
1935 return false;
1936}
1937
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001938void PCHReader::ReadPreprocessedEntities() {
1939 ReadDefinedMacros();
1940}
1941
Douglas Gregor2cf26342009-04-09 22:27:44 +00001942/// \brief Read and return the type at the given offset.
1943///
1944/// This routine actually reads the record corresponding to the type
1945/// at the given offset in the bitstream. It is a helper routine for
1946/// GetType, which deals with reading type IDs.
1947QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001948 // Keep track of where we are in the stream, then jump back there
1949 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001950 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00001951
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00001952 ReadingKindTracker ReadingKind(Read_Type, *this);
1953
Douglas Gregord89275b2009-07-06 18:54:52 +00001954 // Note that we are loading a type record.
1955 LoadingTypeOrDecl Loading(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001957 DeclsCursor.JumpToBit(Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001958 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001959 unsigned Code = DeclsCursor.ReadCode();
1960 switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001961 case pch::TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001962 if (Record.size() != 2) {
1963 Error("Incorrect encoding of extended qualifier type");
1964 return QualType();
1965 }
Douglas Gregor6d473962009-04-15 22:00:08 +00001966 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00001967 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
1968 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00001969 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001970
Douglas Gregor2cf26342009-04-09 22:27:44 +00001971 case pch::TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001972 if (Record.size() != 1) {
1973 Error("Incorrect encoding of complex type");
1974 return QualType();
1975 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001976 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001977 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001978 }
1979
1980 case pch::TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001981 if (Record.size() != 1) {
1982 Error("Incorrect encoding of pointer type");
1983 return QualType();
1984 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001985 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001986 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001987 }
1988
1989 case pch::TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001990 if (Record.size() != 1) {
1991 Error("Incorrect encoding of block pointer type");
1992 return QualType();
1993 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001994 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001995 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001996 }
1997
1998 case pch::TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001999 if (Record.size() != 1) {
2000 Error("Incorrect encoding of lvalue reference type");
2001 return QualType();
2002 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002003 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002004 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002005 }
2006
2007 case pch::TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002008 if (Record.size() != 1) {
2009 Error("Incorrect encoding of rvalue reference type");
2010 return QualType();
2011 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002012 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002013 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002014 }
2015
2016 case pch::TYPE_MEMBER_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002017 if (Record.size() != 1) {
2018 Error("Incorrect encoding of member pointer type");
2019 return QualType();
2020 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002021 QualType PointeeType = GetType(Record[0]);
2022 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002023 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002024 }
2025
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002026 case pch::TYPE_CONSTANT_ARRAY: {
2027 QualType ElementType = GetType(Record[0]);
2028 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2029 unsigned IndexTypeQuals = Record[2];
2030 unsigned Idx = 3;
2031 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002032 return Context->getConstantArrayType(ElementType, Size,
2033 ASM, IndexTypeQuals);
2034 }
2035
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002036 case pch::TYPE_INCOMPLETE_ARRAY: {
2037 QualType ElementType = GetType(Record[0]);
2038 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2039 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002040 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002041 }
2042
2043 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002044 QualType ElementType = GetType(Record[0]);
2045 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2046 unsigned IndexTypeQuals = Record[2];
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002047 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2048 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002049 return Context->getVariableArrayType(ElementType, ReadExpr(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002050 ASM, IndexTypeQuals,
2051 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002052 }
2053
2054 case pch::TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002055 if (Record.size() != 3) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002056 Error("incorrect encoding of vector type in PCH file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002057 return QualType();
2058 }
2059
2060 QualType ElementType = GetType(Record[0]);
2061 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002062 unsigned AltiVecSpec = Record[2];
2063 return Context->getVectorType(ElementType, NumElements,
2064 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002065 }
2066
2067 case pch::TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002068 if (Record.size() != 3) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002069 Error("incorrect encoding of extended vector type in PCH file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002070 return QualType();
2071 }
2072
2073 QualType ElementType = GetType(Record[0]);
2074 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002075 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002076 }
2077
2078 case pch::TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002079 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002080 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002081 return QualType();
2082 }
2083 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002084 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002085 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002086 }
2087
2088 case pch::TYPE_FUNCTION_PROTO: {
2089 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002090 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002091 unsigned RegParm = Record[2];
2092 CallingConv CallConv = (CallingConv)Record[3];
2093 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002094 unsigned NumParams = Record[Idx++];
2095 llvm::SmallVector<QualType, 16> ParamTypes;
2096 for (unsigned I = 0; I != NumParams; ++I)
2097 ParamTypes.push_back(GetType(Record[Idx++]));
2098 bool isVariadic = Record[Idx++];
2099 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002100 bool hasExceptionSpec = Record[Idx++];
2101 bool hasAnyExceptionSpec = Record[Idx++];
2102 unsigned NumExceptions = Record[Idx++];
2103 llvm::SmallVector<QualType, 2> Exceptions;
2104 for (unsigned I = 0; I != NumExceptions; ++I)
2105 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002106 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002107 isVariadic, Quals, hasExceptionSpec,
2108 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002109 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002110 FunctionType::ExtInfo(NoReturn, RegParm,
2111 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002112 }
2113
John McCalled976492009-12-04 22:46:56 +00002114 case pch::TYPE_UNRESOLVED_USING:
2115 return Context->getTypeDeclType(
2116 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2117
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002118 case pch::TYPE_TYPEDEF:
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002119 if (Record.size() != 1) {
2120 Error("incorrect encoding of typedef type");
2121 return QualType();
2122 }
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002123 return Context->getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002124
2125 case pch::TYPE_TYPEOF_EXPR:
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002126 return Context->getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002127
2128 case pch::TYPE_TYPEOF: {
2129 if (Record.size() != 1) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002130 Error("incorrect encoding of typeof(type) in PCH file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002131 return QualType();
2132 }
2133 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002134 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002135 }
Mike Stump1eb44332009-09-09 15:08:12 +00002136
Anders Carlsson395b4752009-06-24 19:06:50 +00002137 case pch::TYPE_DECLTYPE:
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002138 return Context->getDecltypeType(ReadExpr());
Anders Carlsson395b4752009-06-24 19:06:50 +00002139
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002140 case pch::TYPE_RECORD:
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002141 if (Record.size() != 1) {
2142 Error("incorrect encoding of record type");
2143 return QualType();
2144 }
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002145 return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002146
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002147 case pch::TYPE_ENUM:
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002148 if (Record.size() != 1) {
2149 Error("incorrect encoding of enum type");
2150 return QualType();
2151 }
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002152 return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002153
John McCall7da24312009-09-05 00:15:47 +00002154 case pch::TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002155 unsigned Idx = 0;
2156 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2157 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2158 QualType NamedType = GetType(Record[Idx++]);
2159 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002160 }
2161
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002162 case pch::TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002163 unsigned Idx = 0;
2164 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002165 return Context->getObjCInterfaceType(ItfD);
2166 }
2167
2168 case pch::TYPE_OBJC_OBJECT: {
2169 unsigned Idx = 0;
2170 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002171 unsigned NumProtos = Record[Idx++];
2172 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2173 for (unsigned I = 0; I != NumProtos; ++I)
2174 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCallc12c5bb2010-05-15 11:32:37 +00002175 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002176 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002177
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002178 case pch::TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002179 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002180 QualType Pointee = GetType(Record[Idx++]);
2181 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002182 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002183
John McCall49a832b2009-10-18 09:09:24 +00002184 case pch::TYPE_SUBST_TEMPLATE_TYPE_PARM: {
2185 unsigned Idx = 0;
2186 QualType Parm = GetType(Record[Idx++]);
2187 QualType Replacement = GetType(Record[Idx++]);
2188 return
2189 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2190 Replacement);
2191 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002192
2193 case pch::TYPE_INJECTED_CLASS_NAME: {
2194 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2195 QualType TST = GetType(Record[1]); // probably derivable
2196 return Context->getInjectedClassNameType(D, TST);
2197 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002198
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002199 case pch::TYPE_TEMPLATE_TYPE_PARM: {
2200 unsigned Idx = 0;
2201 unsigned Depth = Record[Idx++];
2202 unsigned Index = Record[Idx++];
2203 bool Pack = Record[Idx++];
2204 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2205 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2206 }
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002207
2208 case pch::TYPE_DEPENDENT_NAME: {
2209 unsigned Idx = 0;
2210 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2211 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2212 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2213 return Context->getDependentNameType(Keyword, NNS, Name, QualType());
2214 }
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002215
2216 case pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
2217 unsigned Idx = 0;
2218 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2219 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2220 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2221 unsigned NumArgs = Record[Idx++];
2222 llvm::SmallVector<TemplateArgument, 8> Args;
2223 Args.reserve(NumArgs);
2224 while (NumArgs--)
2225 Args.push_back(ReadTemplateArgument(Record, Idx));
2226 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2227 Args.size(), Args.data());
2228 }
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002229
2230 case pch::TYPE_DEPENDENT_SIZED_ARRAY: {
2231 unsigned Idx = 0;
2232
2233 // ArrayType
2234 QualType ElementType = GetType(Record[Idx++]);
2235 ArrayType::ArraySizeModifier ASM
2236 = (ArrayType::ArraySizeModifier)Record[Idx++];
2237 unsigned IndexTypeQuals = Record[Idx++];
2238
2239 // DependentSizedArrayType
2240 Expr *NumElts = ReadExpr();
2241 SourceRange Brackets = ReadSourceRange(Record, Idx);
2242
2243 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2244 IndexTypeQuals, Brackets);
2245 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002246
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002247 case pch::TYPE_TEMPLATE_SPECIALIZATION: {
2248 unsigned Idx = 0;
2249 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002250 llvm::SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002251 ReadTemplateArgumentList(Args, Record, Idx);
2252 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002253 return Context->getTemplateSpecializationType(Name, Args.data(),Args.size(),
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002254 Canon);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002255 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002256 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002257 // Suppress a GCC warning
2258 return QualType();
2259}
2260
John McCalla1ee0c52009-10-16 21:56:05 +00002261namespace {
2262
2263class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
2264 PCHReader &Reader;
2265 const PCHReader::RecordData &Record;
2266 unsigned &Idx;
2267
2268public:
2269 TypeLocReader(PCHReader &Reader, const PCHReader::RecordData &Record,
2270 unsigned &Idx)
2271 : Reader(Reader), Record(Record), Idx(Idx) { }
2272
John McCall51bd8032009-10-18 01:05:36 +00002273 // We want compile-time assurance that we've enumerated all of
2274 // these, so unfortunately we have to declare them first, then
2275 // define them out-of-line.
2276#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002277#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002278 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002279#include "clang/AST/TypeLocNodes.def"
2280
John McCall51bd8032009-10-18 01:05:36 +00002281 void VisitFunctionTypeLoc(FunctionTypeLoc);
2282 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002283};
2284
2285}
2286
John McCall51bd8032009-10-18 01:05:36 +00002287void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002288 // nothing to do
2289}
John McCall51bd8032009-10-18 01:05:36 +00002290void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00002291 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2292 if (TL.needsExtraLocalData()) {
2293 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2294 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2295 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2296 TL.setModeAttr(Record[Idx++]);
2297 }
John McCalla1ee0c52009-10-16 21:56:05 +00002298}
John McCall51bd8032009-10-18 01:05:36 +00002299void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2300 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002301}
John McCall51bd8032009-10-18 01:05:36 +00002302void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2303 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002304}
John McCall51bd8032009-10-18 01:05:36 +00002305void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2306 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002307}
John McCall51bd8032009-10-18 01:05:36 +00002308void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2309 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002310}
John McCall51bd8032009-10-18 01:05:36 +00002311void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2312 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002313}
John McCall51bd8032009-10-18 01:05:36 +00002314void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2315 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002316}
John McCall51bd8032009-10-18 01:05:36 +00002317void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2318 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2319 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002320 if (Record[Idx++])
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002321 TL.setSizeExpr(Reader.ReadExpr());
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002322 else
John McCall51bd8032009-10-18 01:05:36 +00002323 TL.setSizeExpr(0);
2324}
2325void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2326 VisitArrayTypeLoc(TL);
2327}
2328void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2329 VisitArrayTypeLoc(TL);
2330}
2331void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2332 VisitArrayTypeLoc(TL);
2333}
2334void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2335 DependentSizedArrayTypeLoc TL) {
2336 VisitArrayTypeLoc(TL);
2337}
2338void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2339 DependentSizedExtVectorTypeLoc TL) {
2340 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2341}
2342void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2343 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2344}
2345void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2346 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2347}
2348void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2349 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2350 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2351 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002352 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002353 }
2354}
2355void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2356 VisitFunctionTypeLoc(TL);
2357}
2358void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2359 VisitFunctionTypeLoc(TL);
2360}
John McCalled976492009-12-04 22:46:56 +00002361void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2362 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2363}
John McCall51bd8032009-10-18 01:05:36 +00002364void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2365 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2366}
2367void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002368 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2369 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2370 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall51bd8032009-10-18 01:05:36 +00002371}
2372void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002373 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2374 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2375 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2376 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002377}
2378void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2379 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2380}
2381void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2382 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2383}
2384void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2385 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2386}
John McCall51bd8032009-10-18 01:05:36 +00002387void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2388 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2389}
John McCall49a832b2009-10-18 09:09:24 +00002390void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2391 SubstTemplateTypeParmTypeLoc TL) {
2392 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2393}
John McCall51bd8032009-10-18 01:05:36 +00002394void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2395 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00002396 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2397 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2398 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2399 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2400 TL.setArgLocInfo(i,
2401 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
2402 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002403}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002404void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002405 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2406 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002407}
John McCall3cb0ebd2010-03-10 03:28:59 +00002408void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2409 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2410}
Douglas Gregor4714c122010-03-31 17:34:00 +00002411void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002412 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2413 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002414 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2415}
John McCall33500952010-06-11 00:33:02 +00002416void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2417 DependentTemplateSpecializationTypeLoc TL) {
2418 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2419 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2420 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2421 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2422 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2423 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2424 TL.setArgLocInfo(I,
2425 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
2426 Record, Idx));
2427}
John McCall51bd8032009-10-18 01:05:36 +00002428void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2429 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002430}
2431void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2432 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00002433 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2434 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2435 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2436 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002437}
John McCall54e14c42009-10-22 22:37:11 +00002438void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2439 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall54e14c42009-10-22 22:37:11 +00002440}
John McCalla1ee0c52009-10-16 21:56:05 +00002441
John McCalla93c9342009-12-07 02:54:59 +00002442TypeSourceInfo *PCHReader::GetTypeSourceInfo(const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00002443 unsigned &Idx) {
2444 QualType InfoTy = GetType(Record[Idx++]);
2445 if (InfoTy.isNull())
2446 return 0;
2447
John McCalla93c9342009-12-07 02:54:59 +00002448 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
John McCalla1ee0c52009-10-16 21:56:05 +00002449 TypeLocReader TLR(*this, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00002450 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002451 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00002452 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00002453}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002454
Douglas Gregor8038d512009-04-10 17:25:41 +00002455QualType PCHReader::GetType(pch::TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00002456 unsigned FastQuals = ID & Qualifiers::FastMask;
2457 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002458
2459 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2460 QualType T;
2461 switch ((pch::PredefinedTypeIDs)Index) {
2462 case pch::PREDEF_TYPE_NULL_ID: return QualType();
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002463 case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2464 case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002465
2466 case pch::PREDEF_TYPE_CHAR_U_ID:
2467 case pch::PREDEF_TYPE_CHAR_S_ID:
2468 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002469 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002470 break;
2471
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002472 case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2473 case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2474 case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2475 case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2476 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002477 case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002478 case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2479 case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2480 case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2481 case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2482 case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2483 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002484 case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002485 case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2486 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2487 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2488 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2489 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002490 case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002491 case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2492 case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002493 case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2494 case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002495 case pch::PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002496 }
2497
2498 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00002499 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002500 }
2501
2502 Index -= pch::NUM_PREDEF_TYPE_IDS;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002503 //assert(Index < TypesLoaded.size() && "Type index out-of-range");
John McCall0953e762009-09-24 19:53:00 +00002504 if (TypesLoaded[Index].isNull())
2505 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]);
Mike Stump1eb44332009-09-09 15:08:12 +00002506
John McCall0953e762009-09-24 19:53:00 +00002507 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002508}
2509
John McCall833ca992009-10-29 08:12:44 +00002510TemplateArgumentLocInfo
2511PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2512 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002513 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00002514 switch (Kind) {
2515 case TemplateArgument::Expression:
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002516 return ReadExpr();
John McCall833ca992009-10-29 08:12:44 +00002517 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002518 return GetTypeSourceInfo(Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00002519 case TemplateArgument::Template: {
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002520 SourceRange QualifierRange = ReadSourceRange(Record, Index);
2521 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
2522 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00002523 }
John McCall833ca992009-10-29 08:12:44 +00002524 case TemplateArgument::Null:
2525 case TemplateArgument::Integral:
2526 case TemplateArgument::Declaration:
2527 case TemplateArgument::Pack:
2528 return TemplateArgumentLocInfo();
2529 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002530 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00002531 return TemplateArgumentLocInfo();
2532}
2533
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002534TemplateArgumentLoc
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002535PCHReader::ReadTemplateArgumentLoc(const RecordData &Record, unsigned &Index) {
2536 TemplateArgument Arg = ReadTemplateArgument(Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002537
2538 if (Arg.getKind() == TemplateArgument::Expression) {
2539 if (Record[Index++]) // bool InfoHasSameExpr.
2540 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
2541 }
2542 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002543 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002544}
2545
John McCall76bd1f32010-06-01 09:23:16 +00002546Decl *PCHReader::GetExternalDecl(uint32_t ID) {
2547 return GetDecl(ID);
2548}
2549
Douglas Gregor8038d512009-04-10 17:25:41 +00002550Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002551 if (ID == 0)
2552 return 0;
2553
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002554 if (ID > DeclsLoaded.size()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002555 Error("declaration ID out-of-range for PCH file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002556 return 0;
2557 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002558
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002559 unsigned Index = ID - 1;
2560 if (!DeclsLoaded[Index])
2561 ReadDeclRecord(DeclOffsets[Index], Index);
2562
2563 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002564}
2565
Chris Lattner887e2b32009-04-27 05:46:25 +00002566/// \brief Resolve the offset of a statement into a statement.
2567///
2568/// This operation will read a new statement from the external
2569/// source each time it is called, and is meant to be used via a
2570/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
John McCall76bd1f32010-06-01 09:23:16 +00002571Stmt *PCHReader::GetExternalDeclStmt(uint64_t Offset) {
Chris Lattnerda930612009-04-27 05:58:23 +00002572 // Since we know tha this statement is part of a decl, make sure to use the
2573 // decl cursor to read it.
2574 DeclsCursor.JumpToBit(Offset);
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002575 return ReadStmtFromStream(DeclsCursor);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002576}
2577
John McCall76bd1f32010-06-01 09:23:16 +00002578bool PCHReader::FindExternalLexicalDecls(const DeclContext *DC,
2579 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00002580 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002581 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002582
Douglas Gregor2cf26342009-04-09 22:27:44 +00002583 uint64_t Offset = DeclContextOffsets[DC].first;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002584 if (Offset == 0) {
2585 Error("DeclContext has no lexical decls in storage");
2586 return true;
2587 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002588
Douglas Gregor0b748912009-04-14 21:18:50 +00002589 // Keep track of where we are in the stream, then jump back there
2590 // after reading this context.
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002591 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002592
Douglas Gregor2cf26342009-04-09 22:27:44 +00002593 // Load the record containing all of the declarations lexically in
2594 // this context.
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002595 DeclsCursor.JumpToBit(Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002596 RecordData Record;
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002597 unsigned Code = DeclsCursor.ReadCode();
2598 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002599 if (RecCode != pch::DECL_CONTEXT_LEXICAL) {
2600 Error("Expected lexical block");
2601 return true;
2602 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002603
2604 // Load all of the declaration IDs
John McCall76bd1f32010-06-01 09:23:16 +00002605 for (RecordData::iterator I = Record.begin(), E = Record.end(); I != E; ++I)
2606 Decls.push_back(GetDecl(*I));
Douglas Gregor25123082009-04-22 22:34:57 +00002607 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002608 return false;
2609}
2610
John McCall76bd1f32010-06-01 09:23:16 +00002611DeclContext::lookup_result
2612PCHReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
2613 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00002614 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002615 "DeclContext has no visible decls in storage");
2616 uint64_t Offset = DeclContextOffsets[DC].second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002617 if (Offset == 0) {
2618 Error("DeclContext has no visible decls in storage");
John McCall76bd1f32010-06-01 09:23:16 +00002619 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2620 DeclContext::lookup_iterator());
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002621 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002622
Douglas Gregor0b748912009-04-14 21:18:50 +00002623 // Keep track of where we are in the stream, then jump back there
2624 // after reading this context.
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002625 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002626
Douglas Gregor2cf26342009-04-09 22:27:44 +00002627 // Load the record containing all of the declarations visible in
2628 // this context.
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002629 DeclsCursor.JumpToBit(Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002630 RecordData Record;
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002631 unsigned Code = DeclsCursor.ReadCode();
2632 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002633 if (RecCode != pch::DECL_CONTEXT_VISIBLE) {
2634 Error("Expected visible block");
John McCall76bd1f32010-06-01 09:23:16 +00002635 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2636 DeclContext::lookup_iterator());
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002637 }
2638
John McCall76bd1f32010-06-01 09:23:16 +00002639 llvm::SmallVector<VisibleDeclaration, 64> Decls;
2640 if (Record.empty()) {
2641 SetExternalVisibleDecls(DC, Decls);
2642 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2643 DeclContext::lookup_iterator());
2644 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002645
2646 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002647 while (Idx < Record.size()) {
2648 Decls.push_back(VisibleDeclaration());
2649 Decls.back().Name = ReadDeclarationName(Record, Idx);
2650
Douglas Gregor2cf26342009-04-09 22:27:44 +00002651 unsigned Size = Record[Idx++];
Chris Lattnerc47be9e2009-04-27 07:35:40 +00002652 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002653 LoadedDecls.reserve(Size);
2654 for (unsigned I = 0; I < Size; ++I)
2655 LoadedDecls.push_back(Record[Idx++]);
2656 }
2657
Douglas Gregor25123082009-04-22 22:34:57 +00002658 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00002659
2660 SetExternalVisibleDecls(DC, Decls);
2661 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002662}
2663
Douglas Gregorfdd01722009-04-14 00:24:19 +00002664void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002665 this->Consumer = Consumer;
2666
Douglas Gregorfdd01722009-04-14 00:24:19 +00002667 if (!Consumer)
2668 return;
2669
2670 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Daniel Dunbar04a0b502009-09-17 03:06:44 +00002671 // Force deserialization of this decl, which will cause it to be passed to
2672 // the consumer (or queued).
2673 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00002674 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00002675
2676 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2677 DeclGroupRef DG(InterestingDecls[I]);
2678 Consumer->HandleTopLevelDecl(DG);
2679 }
Douglas Gregorfdd01722009-04-14 00:24:19 +00002680}
2681
Douglas Gregor2cf26342009-04-09 22:27:44 +00002682void PCHReader::PrintStats() {
2683 std::fprintf(stderr, "*** PCH Statistics:\n");
2684
Mike Stump1eb44332009-09-09 15:08:12 +00002685 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002686 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00002687 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002688 unsigned NumDeclsLoaded
2689 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2690 (Decl *)0);
2691 unsigned NumIdentifiersLoaded
2692 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2693 IdentifiersLoaded.end(),
2694 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00002695 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002696 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2697 SelectorsLoaded.end(),
2698 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002699
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002700 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
2701 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002702 if (TotalNumSLocEntries)
2703 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
2704 NumSLocEntriesRead, TotalNumSLocEntries,
2705 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002706 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002707 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002708 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2709 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2710 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002711 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002712 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2713 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002714 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002715 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002716 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2717 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00002718 if (TotalNumSelectors)
2719 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2720 NumSelectorsLoaded, TotalNumSelectors,
2721 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2722 if (TotalNumStatements)
2723 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2724 NumStatementsRead, TotalNumStatements,
2725 ((float)NumStatementsRead/TotalNumStatements * 100));
2726 if (TotalNumMacros)
2727 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2728 NumMacrosRead, TotalNumMacros,
2729 ((float)NumMacrosRead/TotalNumMacros * 100));
2730 if (TotalLexicalDeclContexts)
2731 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2732 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2733 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2734 * 100));
2735 if (TotalVisibleDeclContexts)
2736 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2737 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2738 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2739 * 100));
2740 if (TotalSelectorsInMethodPool) {
2741 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2742 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2743 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2744 * 100));
2745 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2746 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002747 std::fprintf(stderr, "\n");
2748}
2749
Douglas Gregor668c1a42009-04-21 22:25:48 +00002750void PCHReader::InitializeSema(Sema &S) {
2751 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002752 S.ExternalSource = this;
2753
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002754 // Makes sure any declarations that were deserialized "too early"
2755 // still get added to the identifier's declaration chains.
2756 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2757 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2758 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002759 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002760 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002761
2762 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00002763 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002764 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2765 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00002766 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002767 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002768
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002769 // If there were any unused static functions, deserialize them and add to
2770 // Sema's list of unused static functions.
2771 for (unsigned I = 0, N = UnusedStaticFuncs.size(); I != N; ++I) {
2772 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(UnusedStaticFuncs[I]));
2773 SemaObj->UnusedStaticFuncs.push_back(FD);
2774 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00002775
2776 // If there were any locally-scoped external declarations,
2777 // deserialize them and add them to Sema's table of locally-scoped
2778 // external declarations.
2779 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2780 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2781 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2782 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00002783
2784 // If there were any ext_vector type declarations, deserialize them
2785 // and add them to Sema's vector of such declarations.
2786 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
2787 SemaObj->ExtVectorDecls.push_back(
2788 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002789}
2790
2791IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2792 // Try to find this name within our on-disk hash table
Mike Stump1eb44332009-09-09 15:08:12 +00002793 PCHIdentifierLookupTable *IdTable
Douglas Gregor668c1a42009-04-21 22:25:48 +00002794 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2795 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2796 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2797 if (Pos == IdTable->end())
2798 return 0;
2799
2800 // Dereferencing the iterator has the effect of building the
2801 // IdentifierInfo node and populating it with the various
2802 // declarations it needs.
2803 return *Pos;
2804}
2805
Mike Stump1eb44332009-09-09 15:08:12 +00002806std::pair<ObjCMethodList, ObjCMethodList>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002807PCHReader::ReadMethodPool(Selector Sel) {
2808 if (!MethodPoolLookupTable)
2809 return std::pair<ObjCMethodList, ObjCMethodList>();
2810
2811 // Try to find this selector within our on-disk hash table.
2812 PCHMethodPoolLookupTable *PoolTable
2813 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2814 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor83941df2009-04-25 17:48:32 +00002815 if (Pos == PoolTable->end()) {
2816 ++NumMethodPoolMisses;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002817 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor83941df2009-04-25 17:48:32 +00002818 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002819
Douglas Gregor83941df2009-04-25 17:48:32 +00002820 ++NumMethodPoolSelectorsRead;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002821 return *Pos;
2822}
2823
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002824void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00002825 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00002826 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002827 IdentifiersLoaded[ID - 1] = II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002828}
2829
Douglas Gregord89275b2009-07-06 18:54:52 +00002830/// \brief Set the globally-visible declarations associated with the given
2831/// identifier.
2832///
2833/// If the PCH reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00002834/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00002835/// them.
2836///
2837/// \param II an IdentifierInfo that refers to one or more globally-visible
2838/// declarations.
2839///
2840/// \param DeclIDs the set of declaration IDs with the name @p II that are
2841/// visible at global scope.
2842///
2843/// \param Nonrecursive should be true to indicate that the caller knows that
2844/// this call is non-recursive, and therefore the globally-visible declarations
2845/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00002846void
2847PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00002848 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
2849 bool Nonrecursive) {
2850 if (CurrentlyLoadingTypeOrDecl && !Nonrecursive) {
2851 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
2852 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
2853 PII.II = II;
2854 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
2855 PII.DeclIDs.push_back(DeclIDs[I]);
2856 return;
2857 }
Mike Stump1eb44332009-09-09 15:08:12 +00002858
Douglas Gregord89275b2009-07-06 18:54:52 +00002859 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
2860 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
2861 if (SemaObj) {
2862 // Introduce this declaration into the translation-unit scope
2863 // and add it to the declaration chain for this identifier, so
2864 // that (unqualified) name lookup will find it.
2865 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
2866 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
2867 } else {
2868 // Queue this declaration so that it will be added to the
2869 // translation unit scope and identifier's declaration chain
2870 // once a Sema object is known.
2871 PreloadedDecls.push_back(D);
2872 }
2873 }
2874}
2875
Chris Lattner7356a312009-04-11 21:15:38 +00002876IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002877 if (ID == 0)
2878 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002879
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002880 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002881 Error("no identifier table in PCH file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002882 return 0;
2883 }
Mike Stump1eb44332009-09-09 15:08:12 +00002884
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002885 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002886 if (!IdentifiersLoaded[ID - 1]) {
2887 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor17e1c5e2009-04-25 21:21:38 +00002888 const char *Str = IdentifierTableData + Offset;
Douglas Gregord6595a42009-04-25 21:04:17 +00002889
Douglas Gregor02fc7512009-04-28 20:01:51 +00002890 // All of the strings in the PCH file are preceded by a 16-bit
2891 // length. Extract that 16-bit length to avoid having to execute
2892 // strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00002893 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
2894 // unsigned integers. This is important to avoid integer overflow when
2895 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00002896 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00002897 unsigned StrLen = (((unsigned) StrLenPtr[0])
2898 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002899 IdentifiersLoaded[ID - 1]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00002900 = &PP->getIdentifierTable().get(Str, StrLen);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002901 }
Mike Stump1eb44332009-09-09 15:08:12 +00002902
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002903 return IdentifiersLoaded[ID - 1];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002904}
2905
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002906void PCHReader::ReadSLocEntry(unsigned ID) {
2907 ReadSLocEntryRecord(ID);
2908}
2909
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002910Selector PCHReader::DecodeSelector(unsigned ID) {
2911 if (ID == 0)
2912 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Douglas Gregora02b1472009-04-28 21:53:25 +00002914 if (!MethodPoolLookupTableData)
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002915 return Selector();
Douglas Gregor83941df2009-04-25 17:48:32 +00002916
2917 if (ID > TotalNumSelectors) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002918 Error("selector ID out of range in PCH file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002919 return Selector();
2920 }
Douglas Gregor83941df2009-04-25 17:48:32 +00002921
2922 unsigned Index = ID - 1;
2923 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2924 // Load this selector from the selector table.
2925 // FIXME: endianness portability issues with SelectorOffsets table
2926 PCHMethodPoolLookupTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002927 SelectorsLoaded[Index]
Douglas Gregor83941df2009-04-25 17:48:32 +00002928 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2929 }
2930
2931 return SelectorsLoaded[Index];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002932}
2933
John McCall76bd1f32010-06-01 09:23:16 +00002934Selector PCHReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00002935 return DecodeSelector(ID);
2936}
2937
John McCall76bd1f32010-06-01 09:23:16 +00002938uint32_t PCHReader::GetNumExternalSelectors() {
Douglas Gregor719770d2010-04-06 17:30:22 +00002939 return TotalNumSelectors + 1;
2940}
2941
Mike Stump1eb44332009-09-09 15:08:12 +00002942DeclarationName
Douglas Gregor2cf26342009-04-09 22:27:44 +00002943PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2944 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2945 switch (Kind) {
2946 case DeclarationName::Identifier:
2947 return DeclarationName(GetIdentifierInfo(Record, Idx));
2948
2949 case DeclarationName::ObjCZeroArgSelector:
2950 case DeclarationName::ObjCOneArgSelector:
2951 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00002952 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002953
2954 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002955 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00002956 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002957
2958 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002959 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00002960 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002961
2962 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002963 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00002964 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002965
2966 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002967 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00002968 (OverloadedOperatorKind)Record[Idx++]);
2969
Sean Hunt3e518bd2009-11-29 07:34:05 +00002970 case DeclarationName::CXXLiteralOperatorName:
2971 return Context->DeclarationNames.getCXXLiteralOperatorName(
2972 GetIdentifierInfo(Record, Idx));
2973
Douglas Gregor2cf26342009-04-09 22:27:44 +00002974 case DeclarationName::CXXUsingDirective:
2975 return DeclarationName::getUsingDirectiveName();
2976 }
2977
2978 // Required to silence GCC warning
2979 return DeclarationName();
2980}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002981
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002982TemplateName
2983PCHReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
2984 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
2985 switch (Kind) {
2986 case TemplateName::Template:
2987 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
2988
2989 case TemplateName::OverloadedTemplate: {
2990 unsigned size = Record[Idx++];
2991 UnresolvedSet<8> Decls;
2992 while (size--)
2993 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
2994
2995 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
2996 }
2997
2998 case TemplateName::QualifiedTemplate: {
2999 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3000 bool hasTemplKeyword = Record[Idx++];
3001 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3002 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3003 }
3004
3005 case TemplateName::DependentTemplate: {
3006 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3007 if (Record[Idx++]) // isIdentifier
3008 return Context->getDependentTemplateName(NNS,
3009 GetIdentifierInfo(Record, Idx));
3010 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003011 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003012 }
3013 }
3014
3015 assert(0 && "Unhandled template name kind!");
3016 return TemplateName();
3017}
3018
3019TemplateArgument
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003020PCHReader::ReadTemplateArgument(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003021 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3022 case TemplateArgument::Null:
3023 return TemplateArgument();
3024 case TemplateArgument::Type:
3025 return TemplateArgument(GetType(Record[Idx++]));
3026 case TemplateArgument::Declaration:
3027 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003028 case TemplateArgument::Integral: {
3029 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3030 QualType T = GetType(Record[Idx++]);
3031 return TemplateArgument(Value, T);
3032 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003033 case TemplateArgument::Template:
3034 return TemplateArgument(ReadTemplateName(Record, Idx));
3035 case TemplateArgument::Expression:
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003036 return TemplateArgument(ReadExpr());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003037 case TemplateArgument::Pack: {
3038 unsigned NumArgs = Record[Idx++];
3039 llvm::SmallVector<TemplateArgument, 8> Args;
3040 Args.reserve(NumArgs);
3041 while (NumArgs--)
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003042 Args.push_back(ReadTemplateArgument(Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003043 TemplateArgument TemplArg;
3044 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3045 return TemplArg;
3046 }
3047 }
3048
3049 assert(0 && "Unhandled template argument kind!");
3050 return TemplateArgument();
3051}
3052
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003053TemplateParameterList *
3054PCHReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
3055 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3056 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3057 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3058
3059 unsigned NumParams = Record[Idx++];
3060 llvm::SmallVector<NamedDecl *, 16> Params;
3061 Params.reserve(NumParams);
3062 while (NumParams--)
3063 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3064
3065 TemplateParameterList* TemplateParams =
3066 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3067 Params.data(), Params.size(), RAngleLoc);
3068 return TemplateParams;
3069}
3070
3071void
3072PCHReader::
3073ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
3074 const RecordData &Record, unsigned &Idx) {
3075 unsigned NumTemplateArgs = Record[Idx++];
3076 TemplArgs.reserve(NumTemplateArgs);
3077 while (NumTemplateArgs--)
3078 TemplArgs.push_back(ReadTemplateArgument(Record, Idx));
3079}
3080
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003081NestedNameSpecifier *
3082PCHReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
3083 unsigned N = Record[Idx++];
3084 NestedNameSpecifier *NNS = 0, *Prev = 0;
3085 for (unsigned I = 0; I != N; ++I) {
3086 NestedNameSpecifier::SpecifierKind Kind
3087 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3088 switch (Kind) {
3089 case NestedNameSpecifier::Identifier: {
3090 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3091 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3092 break;
3093 }
3094
3095 case NestedNameSpecifier::Namespace: {
3096 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3097 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3098 break;
3099 }
3100
3101 case NestedNameSpecifier::TypeSpec:
3102 case NestedNameSpecifier::TypeSpecWithTemplate: {
3103 Type *T = GetType(Record[Idx++]).getTypePtr();
3104 bool Template = Record[Idx++];
3105 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3106 break;
3107 }
3108
3109 case NestedNameSpecifier::Global: {
3110 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3111 // No associated value, and there can't be a prefix.
3112 break;
3113 }
3114 Prev = NNS;
3115 }
3116 }
3117 return NNS;
3118}
3119
3120SourceRange
3121PCHReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar8ee59392010-06-02 15:47:10 +00003122 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3123 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3124 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003125}
3126
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003127/// \brief Read an integral value
3128llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
3129 unsigned BitWidth = Record[Idx++];
3130 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3131 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3132 Idx += NumWords;
3133 return Result;
3134}
3135
3136/// \brief Read a signed integral value
3137llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
3138 bool isUnsigned = Record[Idx++];
3139 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3140}
3141
Douglas Gregor17fc2232009-04-14 21:55:33 +00003142/// \brief Read a floating-point value
3143llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003144 return llvm::APFloat(ReadAPInt(Record, Idx));
3145}
3146
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003147// \brief Read a string
3148std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
3149 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00003150 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003151 Idx += Len;
3152 return Result;
3153}
3154
Chris Lattnerd2598362010-05-10 00:25:06 +00003155CXXTemporary *PCHReader::ReadCXXTemporary(const RecordData &Record,
3156 unsigned &Idx) {
3157 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3158 return CXXTemporary::Create(*Context, Decl);
3159}
3160
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003161DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003162 return Diag(SourceLocation(), DiagID);
3163}
3164
3165DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003166 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003167}
Douglas Gregor025452f2009-04-17 00:04:06 +00003168
Douglas Gregor668c1a42009-04-21 22:25:48 +00003169/// \brief Retrieve the identifier table associated with the
3170/// preprocessor.
3171IdentifierTable &PCHReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003172 assert(PP && "Forgot to set Preprocessor ?");
3173 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003174}
3175
Douglas Gregor025452f2009-04-17 00:04:06 +00003176/// \brief Record that the given ID maps to the given switch-case
3177/// statement.
3178void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3179 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3180 SwitchCaseStmts[ID] = SC;
3181}
3182
3183/// \brief Retrieve the switch-case statement with the given ID.
3184SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3185 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3186 return SwitchCaseStmts[ID];
3187}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003188
3189/// \brief Record that the given label statement has been
3190/// deserialized and has the given ID.
3191void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00003192 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003193 "Deserialized label twice");
3194 LabelStmts[ID] = S;
3195
3196 // If we've already seen any goto statements that point to this
3197 // label, resolve them now.
3198 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3199 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3200 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3201 Goto->second->setLabel(S);
3202 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003203
3204 // If we've already seen any address-label statements that point to
3205 // this label, resolve them now.
3206 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00003207 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003208 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00003209 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003210 AddrLabel != AddrLabels.second; ++AddrLabel)
3211 AddrLabel->second->setLabel(S);
3212 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003213}
3214
3215/// \brief Set the label of the given statement to the label
3216/// identified by ID.
3217///
3218/// Depending on the order in which the label and other statements
3219/// referencing that label occur, this operation may complete
3220/// immediately (updating the statement) or it may queue the
3221/// statement to be back-patched later.
3222void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3223 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3224 if (Label != LabelStmts.end()) {
3225 // We've already seen this label, so set the label of the goto and
3226 // we're done.
3227 S->setLabel(Label->second);
3228 } else {
3229 // We haven't seen this label yet, so add this goto to the set of
3230 // unresolved goto statements.
3231 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3232 }
3233}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003234
3235/// \brief Set the label of the given expression to the label
3236/// identified by ID.
3237///
3238/// Depending on the order in which the label and other statements
3239/// referencing that label occur, this operation may complete
3240/// immediately (updating the statement) or it may queue the
3241/// statement to be back-patched later.
3242void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3243 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3244 if (Label != LabelStmts.end()) {
3245 // We've already seen this label, so set the label of the
3246 // label-address expression and we're done.
3247 S->setLabel(Label->second);
3248 } else {
3249 // We haven't seen this label yet, so add this label-address
3250 // expression to the set of unresolved label-address expressions.
3251 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3252 }
3253}
Douglas Gregord89275b2009-07-06 18:54:52 +00003254
3255
Mike Stump1eb44332009-09-09 15:08:12 +00003256PCHReader::LoadingTypeOrDecl::LoadingTypeOrDecl(PCHReader &Reader)
Douglas Gregord89275b2009-07-06 18:54:52 +00003257 : Reader(Reader), Parent(Reader.CurrentlyLoadingTypeOrDecl) {
3258 Reader.CurrentlyLoadingTypeOrDecl = this;
3259}
3260
3261PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() {
3262 if (!Parent) {
3263 // If any identifiers with corresponding top-level declarations have
3264 // been loaded, load those declarations now.
3265 while (!Reader.PendingIdentifierInfos.empty()) {
3266 Reader.SetGloballyVisibleDecls(Reader.PendingIdentifierInfos.front().II,
3267 Reader.PendingIdentifierInfos.front().DeclIDs,
3268 true);
3269 Reader.PendingIdentifierInfos.pop_front();
3270 }
3271 }
3272
Mike Stump1eb44332009-09-09 15:08:12 +00003273 Reader.CurrentlyLoadingTypeOrDecl = Parent;
Douglas Gregord89275b2009-07-06 18:54:52 +00003274}