blob: 198fd434c8ce64ef95caf4ee407b797c4b8b96f7 [file] [log] [blame]
Douglas Gregoref84c4b2009-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 Lattner92ba5ff2009-04-27 05:14:47 +000013
Douglas Gregoref84c4b2009-04-09 22:27:44 +000014#include "clang/Frontend/PCHReader.h"
Douglas Gregor55abb232009-04-10 20:39:37 +000015#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000016#include "clang/Frontend/Utils.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000017#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000018#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000032#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000033#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000034#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000036#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +000037#include "llvm/System/Path.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000038#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000039#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000041#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042using namespace clang;
43
44//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis366985d2009-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 Carruthe03aa552010-04-17 20:17:31 +000065 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-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 Jahanian45878032010-02-09 19:31:38 +000077 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +000078 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
79 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000080 PARSE_LANGOPT_BENIGN(PascalStrings);
81 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000082 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000083 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000084 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000085 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000086 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis366985d2009-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 Stump11289f42009-09-09 15:08:12 +000090 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000091 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +000092 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-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 Lattner51924e512010-06-26 21:25:03 +000096 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +000097 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-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 Thompsoned4e2952009-11-05 20:14:16 +0000114 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000115 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000116 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000117 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
118 return true;
119 }
120 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000121 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
122 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000123 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000124 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stumpd9546382009-12-12 01:27:46 +0000125 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000126 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000127#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000128#undef PARSE_LANGOPT_BENIGN
129
130 return false;
131}
132
Daniel Dunbar20a682d2009-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 Kyrtzidis366985d2009-06-19 00:03:23 +0000140}
141
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000142bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000143 FileID PCHBufferID,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000144 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000145 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-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 Dunbar000c4ff2009-11-11 05:29:04 +0000150 llvm::SmallString<256> PCHInclude;
151 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000152 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-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 Kremenek1ff615c2010-03-18 00:56:54 +0000157 if (Left == PP.getPredefines()) {
158 Error("Missing PCH include entry!");
159 return true;
160 }
Daniel Dunbar000c4ff2009-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 Kyrtzidis366985d2009-06-19 00:03:23 +0000165 return false;
166
167 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000168
Daniel Dunbar8665c7e2009-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 Dunbar045f917e2009-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 Kyrtzidis366985d2009-06-19 00:03:23 +0000177
Daniel Dunbar499baed2009-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 Kyrtzidis366985d2009-06-19 00:03:23 +0000180 std::sort(CmdLineLines.begin(), CmdLineLines.end());
181 std::sort(PCHLines.begin(), PCHLines.end());
182
Daniel Dunbar499baed2009-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 Kyrtzidis366985d2009-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 Dunbar499baed2009-11-11 05:26:28 +0000193 llvm::StringRef Missing = MissingPredefines[I];
194 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000195 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
196 return true;
197 }
Mike Stump11289f42009-09-09 15:08:12 +0000198
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000199 // This is a macro definition. Determine the name of the macro we're
200 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000201 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000202 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-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 Dunbar499baed2009-11-11 05:26:28 +0000206 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000207
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000208 // Determine whether this macro was given a different definition on the
209 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000210 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000211 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000212 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000213 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
214 MacroDefStart);
215 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000216 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000217 // Different macro; we're done.
218 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000219 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
222 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000223 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000224 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000225 (*ConflictPos)[MacroDefLen] != '(')
226 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000227
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000228 // We found a conflicting macro definition.
229 break;
230 }
Mike Stump11289f42009-09-09 15:08:12 +0000231
Argyrios Kyrtzidis366985d2009-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 Dunbar499baed2009-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 Kyrtzidis366985d2009-06-19 00:03:23 +0000242
243 ConflictingDefines = true;
244 continue;
245 }
Mike Stump11289f42009-09-09 15:08:12 +0000246
Daniel Dunbar8665c7e2009-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 Kyrtzidis366985d2009-06-19 00:03:23 +0000249 if (ConflictingDefines)
250 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000251
Argyrios Kyrtzidis366985d2009-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 Dunbar499baed2009-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 Kyrtzidis366985d2009-06-19 00:03:23 +0000261 .getFileLocWithOffset(Offset);
262 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
263 }
Mike Stump11289f42009-09-09 15:08:12 +0000264
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000265 if (ConflictingDefines)
266 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000267
Argyrios Kyrtzidis366985d2009-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 Dunbar499baed2009-11-11 05:26:28 +0000272 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000273 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
274 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000275 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000276 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000277 llvm::StringRef &Extra = ExtraPredefines[I];
278 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-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 Stump11289f42009-09-09 15:08:12 +0000286 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-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 Dunbar499baed2009-11-11 05:26:28 +0000290 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-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 Dunbar499baed2009-11-11 05:26:28 +0000295 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000296 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-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 Gregor5712ebc2010-03-16 16:35:32 +0000310void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
311 unsigned ID) {
312 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
313 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000314}
315
316void PCHValidator::ReadCounter(unsigned Value) {
317 PP.setCounterValue(Value);
318}
319
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000320//===----------------------------------------------------------------------===//
Douglas Gregora868bbd2009-04-21 22:25:48 +0000321// PCH reader implementation
322//===----------------------------------------------------------------------===//
323
Mike Stump11289f42009-09-09 15:08:12 +0000324PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context,
325 const char *isysroot)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000326 : Listener(new PCHValidator(PP, *this)), SourceMgr(PP.getSourceManager()),
327 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000328 SemaObj(0), PP(&PP), Context(Context), StatCache(0), Consumer(0),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000329 IdentifierTableData(0), IdentifierLookupTable(0),
330 IdentifierOffsets(0),
331 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
332 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
Douglas Gregoraae92242010-03-19 21:51:54 +0000333 TotalNumSelectors(0), MacroDefinitionOffsets(0),
Douglas Gregor9507d462010-03-19 22:13:20 +0000334 NumPreallocatedPreprocessingEntities(0),
Douglas Gregoraae92242010-03-19 21:51:54 +0000335 isysroot(isysroot), NumStatHits(0), NumStatMisses(0),
Mike Stump11289f42009-09-09 15:08:12 +0000336 NumSLocEntriesRead(0), NumStatementsRead(0),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000337 NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000338 NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0),
Mike Stump11289f42009-09-09 15:08:12 +0000339 CurrentlyLoadingTypeOrDecl(0) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000340 RelocatablePCH = false;
341}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000342
343PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr,
Mike Stump11289f42009-09-09 15:08:12 +0000344 Diagnostic &Diags, const char *isysroot)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000345 : SourceMgr(SourceMgr), FileMgr(FileMgr), Diags(Diags),
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000346 SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0),
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000347 IdentifierTableData(0), IdentifierLookupTable(0),
348 IdentifierOffsets(0),
349 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
350 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
Douglas Gregoraae92242010-03-19 21:51:54 +0000351 TotalNumSelectors(0), MacroDefinitionOffsets(0),
Douglas Gregor9507d462010-03-19 22:13:20 +0000352 NumPreallocatedPreprocessingEntities(0),
Douglas Gregoraae92242010-03-19 21:51:54 +0000353 isysroot(isysroot), NumStatHits(0), NumStatMisses(0),
Mike Stump11289f42009-09-09 15:08:12 +0000354 NumSLocEntriesRead(0), NumStatementsRead(0),
Douglas Gregor258ae542009-04-27 06:38:32 +0000355 NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
Douglas Gregor1342e842009-07-06 18:54:52 +0000356 NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0),
Mike Stump11289f42009-09-09 15:08:12 +0000357 CurrentlyLoadingTypeOrDecl(0) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000358 RelocatablePCH = false;
359}
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000360
361PCHReader::~PCHReader() {}
362
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000363
Douglas Gregora868bbd2009-04-21 22:25:48 +0000364namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +0000365class PCHMethodPoolLookupTrait {
Douglas Gregorc78d3462009-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 Stump11289f42009-09-09 15:08:12 +0000375
Douglas Gregorc78d3462009-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 Stump11289f42009-09-09 15:08:12 +0000380
Douglas Gregorc78d3462009-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 Dunbarf8502d52009-10-17 23:52:28 +0000388 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000389 return R;
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Douglas Gregorc78d3462009-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 Stump11289f42009-09-09 15:08:12 +0000395
Douglas Gregorc78d3462009-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 Stump11289f42009-09-09 15:08:12 +0000403
Douglas Gregor95c13f52009-04-25 17:48:32 +0000404 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000405 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000406 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000407 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000408 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-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 Gregor038c3382009-05-22 22:45:36 +0000420 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
Douglas Gregorc78d3462009-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 Stump11289f42009-09-09 15:08:12 +0000433 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-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 Kremenekda4abf12010-02-11 00:53:01 +0000442 ObjCMethodList *Mem =
443 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
444 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-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 Stump11289f42009-09-09 15:08:12 +0000451 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-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 Kremenekda4abf12010-02-11 00:53:01 +0000460 ObjCMethodList *Mem =
461 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
462 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000463 Prev = Prev->Next;
464 }
465
466 return Result;
467 }
468};
Mike Stump11289f42009-09-09 15:08:12 +0000469
470} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000471
472/// \brief The on-disk hash table used for the global method pool.
Mike Stump11289f42009-09-09 15:08:12 +0000473typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
Douglas Gregorc78d3462009-04-24 21:10:55 +0000474 PCHMethodPoolLookupTable;
475
476namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +0000477class PCHIdentifierLookupTrait {
Douglas Gregora868bbd2009-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 Stump11289f42009-09-09 15:08:12 +0000492 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
Douglas Gregora868bbd2009-04-21 22:25:48 +0000493 : Reader(Reader), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000494
Douglas Gregora868bbd2009-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 Stump11289f42009-09-09 15:08:12 +0000500
Douglas Gregora868bbd2009-04-21 22:25:48 +0000501 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000502 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Douglas Gregora868bbd2009-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 Stump11289f42009-09-09 15:08:12 +0000508
Douglas Gregora868bbd2009-04-21 22:25:48 +0000509 static std::pair<unsigned, unsigned>
510 ReadKeyDataLength(const unsigned char*& d) {
511 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000512 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000513 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000514 return std::make_pair(KeyLen, DataLen);
515 }
Mike Stump11289f42009-09-09 15:08:12 +0000516
Douglas Gregora868bbd2009-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 Stump11289f42009-09-09 15:08:12 +0000522
523 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000524 const unsigned char* d,
525 unsigned DataLen) {
526 using namespace clang::io;
Douglas Gregor1d583f22009-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 Gregorb9256522009-04-28 21:32:13 +0000544 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-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 Stump11289f42009-09-09 15:08:12 +0000555
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000556 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000557 DataLen -= 6;
Douglas Gregora868bbd2009-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 Gregor6b7bf5a2009-04-25 20:26:24 +0000563 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
564 k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000565 Reader.SetIdentifierInfo(ID, II);
566
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000567 // Set or check the various bits in the IdentifierInfo structure.
568 // FIXME: Load token IDs lazily, too?
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000569 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000570 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-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 Gregorc3366a52009-04-21 23:56:24 +0000578 // If this identifier is a macro, deserialize the macro
579 // definition.
580 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000581 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregorc3366a52009-04-21 23:56:24 +0000582 Reader.ReadMacroRecord(Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000583 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000584 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000585
586 // Read all of the declarations visible at global scope with this
587 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000588 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-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 Gregora868bbd2009-04-21 22:25:48 +0000594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Douglas Gregora868bbd2009-04-21 22:25:48 +0000596 return II;
597 }
598};
Mike Stump11289f42009-09-09 15:08:12 +0000599
600} // end anonymous namespace
Douglas Gregora868bbd2009-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 Stump11289f42009-09-09 15:08:12 +0000604typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
Douglas Gregora868bbd2009-04-21 22:25:48 +0000605 PCHIdentifierLookupTable;
606
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000607void PCHReader::Error(const char *Msg) {
608 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000609}
610
Douglas Gregor92863e42009-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 Dunbar20a682d2009-11-11 00:52:11 +0000628bool PCHReader::CheckPredefinesBuffer(llvm::StringRef PCHPredef,
Douglas Gregor92863e42009-04-10 23:10:45 +0000629 FileID PCHBufferID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000630 if (Listener)
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000631 return Listener->ReadPredefinesBuffer(PCHPredef, PCHBufferID,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000632 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000633 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000634 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000635}
636
Douglas Gregorc5046832009-04-27 18:38:38 +0000637//===----------------------------------------------------------------------===//
638// Source Manager Deserialization
639//===----------------------------------------------------------------------===//
640
Douglas Gregor4c7626e2009-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 Gregor0086a5a2009-07-07 00:12:59 +0000643bool PCHReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000644 unsigned Idx = 0;
645 LineTableInfo &LineTable = SourceMgr.getLineTable();
646
647 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000648 std::map<int, int> FileIDs;
649 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-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 Gregor0086a5a2009-07-07 00:12:59 +0000654 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000655 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000656 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000657 }
658
659 // Parse the line entries
660 std::vector<LineEntry> Entries;
661 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000662 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000663
664 // Extract the line entries
665 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000666 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000667 Entries.clear();
668 Entries.reserve(NumEntries);
669 for (unsigned I = 0; I != NumEntries; ++I) {
670 unsigned FileOffset = Record[Idx++];
671 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000672 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000673 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000674 = (SrcMgr::CharacteristicKind)Record[Idx++];
675 unsigned IncludeOffset = Record[Idx++];
676 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
677 FileKind, IncludeOffset));
678 }
679 LineTable.AddEntry(FID, Entries);
680 }
681
682 return false;
683}
684
Douglas Gregorc5046832009-04-27 18:38:38 +0000685namespace {
686
Benjamin Kramer16634c22009-11-28 10:07:24 +0000687class PCHStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +0000688public:
689 const bool hasStat;
690 const ino_t ino;
691 const dev_t dev;
692 const mode_t mode;
693 const time_t mtime;
694 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000695
Douglas Gregorc5046832009-04-27 18:38:38 +0000696 PCHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000697 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
698
Douglas Gregorc5046832009-04-27 18:38:38 +0000699 PCHStatData()
700 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
701};
702
Benjamin Kramer16634c22009-11-28 10:07:24 +0000703class PCHStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000704 public:
705 typedef const char *external_key_type;
706 typedef const char *internal_key_type;
707
708 typedef PCHStatData data_type;
709
710 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000711 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000712 }
713
714 static internal_key_type GetInternalKey(const char *path) { return path; }
715
716 static bool EqualKey(internal_key_type a, internal_key_type b) {
717 return strcmp(a, b) == 0;
718 }
719
720 static std::pair<unsigned, unsigned>
721 ReadKeyDataLength(const unsigned char*& d) {
722 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
723 unsigned DataLen = (unsigned) *d++;
724 return std::make_pair(KeyLen + 1, DataLen);
725 }
726
727 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
728 return (const char *)d;
729 }
730
731 static data_type ReadData(const internal_key_type, const unsigned char *d,
732 unsigned /*DataLen*/) {
733 using namespace clang::io;
734
735 if (*d++ == 1)
736 return data_type();
737
738 ino_t ino = (ino_t) ReadUnalignedLE32(d);
739 dev_t dev = (dev_t) ReadUnalignedLE32(d);
740 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000741 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +0000742 off_t size = (off_t) ReadUnalignedLE64(d);
743 return data_type(ino, dev, mode, mtime, size);
744 }
745};
746
747/// \brief stat() cache for precompiled headers.
748///
749/// This cache is very similar to the stat cache used by pretokenized
750/// headers.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000751class PCHStatCache : public StatSysCallCache {
Douglas Gregorc5046832009-04-27 18:38:38 +0000752 typedef OnDiskChainedHashTable<PCHStatLookupTrait> CacheTy;
753 CacheTy *Cache;
754
755 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000756public:
Douglas Gregorc5046832009-04-27 18:38:38 +0000757 PCHStatCache(const unsigned char *Buckets,
758 const unsigned char *Base,
759 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +0000760 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +0000761 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
762 Cache = CacheTy::Create(Buckets, Base);
763 }
764
765 ~PCHStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Douglas Gregorc5046832009-04-27 18:38:38 +0000767 int stat(const char *path, struct stat *buf) {
768 // Do the lookup for the file's data in the PCH file.
769 CacheTy::iterator I = Cache->find(path);
770
771 // If we don't get a hit in the PCH file just forward to 'stat'.
772 if (I == Cache->end()) {
773 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000774 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Douglas Gregorc5046832009-04-27 18:38:38 +0000777 ++NumStatHits;
778 PCHStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000779
Douglas Gregorc5046832009-04-27 18:38:38 +0000780 if (!Data.hasStat)
781 return 1;
782
783 buf->st_ino = Data.ino;
784 buf->st_dev = Data.dev;
785 buf->st_mtime = Data.mtime;
786 buf->st_mode = Data.mode;
787 buf->st_size = Data.size;
788 return 0;
789 }
790};
791} // end anonymous namespace
792
793
Douglas Gregora7f71a92009-04-10 03:52:48 +0000794/// \brief Read the source manager block
Douglas Gregor92863e42009-04-10 23:10:45 +0000795PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000796 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +0000797
798 // Set the source-location entry cursor to the current position in
799 // the stream. This cursor will be used to read the contents of the
800 // source manager block initially, and then lazily read
801 // source-location entries as needed.
802 SLocEntryCursor = Stream;
803
804 // The stream itself is going to skip over the source manager block.
805 if (Stream.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000806 Error("malformed block record in PCH file");
Douglas Gregor258ae542009-04-27 06:38:32 +0000807 return Failure;
808 }
809
810 // Enter the source manager block.
811 if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000812 Error("malformed source manager block record in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +0000813 return Failure;
814 }
Douglas Gregora7f71a92009-04-10 03:52:48 +0000815
Douglas Gregora7f71a92009-04-10 03:52:48 +0000816 RecordData Record;
817 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000818 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +0000819 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000820 if (SLocEntryCursor.ReadBlockEnd()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000821 Error("error at end of Source Manager block in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +0000822 return Failure;
823 }
Douglas Gregor92863e42009-04-10 23:10:45 +0000824 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +0000825 }
Mike Stump11289f42009-09-09 15:08:12 +0000826
Douglas Gregora7f71a92009-04-10 03:52:48 +0000827 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
828 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +0000829 SLocEntryCursor.ReadSubBlockID();
830 if (SLocEntryCursor.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000831 Error("malformed block record in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +0000832 return Failure;
833 }
Douglas Gregora7f71a92009-04-10 03:52:48 +0000834 continue;
835 }
Mike Stump11289f42009-09-09 15:08:12 +0000836
Douglas Gregora7f71a92009-04-10 03:52:48 +0000837 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000838 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +0000839 continue;
840 }
Mike Stump11289f42009-09-09 15:08:12 +0000841
Douglas Gregora7f71a92009-04-10 03:52:48 +0000842 // Read a record.
843 const char *BlobStart;
844 unsigned BlobLen;
845 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +0000846 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000847 default: // Default behavior: ignore.
848 break;
849
Chris Lattner184e65d2009-04-14 23:22:57 +0000850 case pch::SM_LINE_TABLE:
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000851 if (ParseLineTable(Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000852 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +0000853 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +0000854
Douglas Gregor258ae542009-04-27 06:38:32 +0000855 case pch::SM_SLOC_FILE_ENTRY:
856 case pch::SM_SLOC_BUFFER_ENTRY:
857 case pch::SM_SLOC_INSTANTIATION_ENTRY:
858 // Once we hit one of the source location entries, we're done.
859 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +0000860 }
861 }
862}
863
Douglas Gregor258ae542009-04-27 06:38:32 +0000864/// \brief Read in the source location entry with the given ID.
865PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) {
866 if (ID == 0)
867 return Success;
868
869 if (ID > TotalNumSLocEntries) {
870 Error("source location entry ID out-of-range for PCH file");
871 return Failure;
872 }
873
874 ++NumSLocEntriesRead;
875 SLocEntryCursor.JumpToBit(SLocOffsets[ID - 1]);
876 unsigned Code = SLocEntryCursor.ReadCode();
877 if (Code == llvm::bitc::END_BLOCK ||
878 Code == llvm::bitc::ENTER_SUBBLOCK ||
879 Code == llvm::bitc::DEFINE_ABBREV) {
880 Error("incorrectly-formatted source location entry in PCH file");
881 return Failure;
882 }
883
Douglas Gregor258ae542009-04-27 06:38:32 +0000884 RecordData Record;
885 const char *BlobStart;
886 unsigned BlobLen;
887 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
888 default:
889 Error("incorrectly-formatted source location entry in PCH file");
890 return Failure;
891
892 case pch::SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000893 std::string Filename(BlobStart, BlobStart + BlobLen);
894 MaybeAddSystemRootToFilename(Filename);
895 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +0000896 if (File == 0) {
897 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000898 ErrorStr += Filename;
Chris Lattnerd20dc872009-06-15 04:35:16 +0000899 ErrorStr += "' referenced by PCH file";
900 Error(ErrorStr.c_str());
901 return Failure;
902 }
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000904 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +0000905 Error("source location entry is incorrect");
906 return Failure;
907 }
908
Douglas Gregor08288f22010-04-09 15:54:22 +0000909 if ((off_t)Record[4] != File->getSize()
910#if !defined(LLVM_ON_WIN32)
911 // In our regression testing, the Windows file system seems to
912 // have inconsistent modification times that sometimes
913 // erroneously trigger this error-handling path.
914 || (time_t)Record[5] != File->getModificationTime()
915#endif
916 ) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000917 Diag(diag::err_fe_pch_file_modified)
918 << Filename;
919 return Failure;
920 }
921
Douglas Gregor258ae542009-04-27 06:38:32 +0000922 FileID FID = SourceMgr.createFileID(File,
923 SourceLocation::getFromRawEncoding(Record[1]),
924 (SrcMgr::CharacteristicKind)Record[2],
925 ID, Record[0]);
926 if (Record[3])
927 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
928 .setHasLineDirectives();
929
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000930 // Reconstruct header-search information for this file.
931 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000932 HFI.isImport = Record[6];
933 HFI.DirInfo = Record[7];
934 HFI.NumIncludes = Record[8];
935 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000936 if (Listener)
937 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +0000938 break;
939 }
940
941 case pch::SM_SLOC_BUFFER_ENTRY: {
942 const char *Name = BlobStart;
943 unsigned Offset = Record[0];
944 unsigned Code = SLocEntryCursor.ReadCode();
945 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000946 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +0000947 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000948
949 if (RecCode != pch::SM_SLOC_BUFFER_BLOB) {
950 Error("PCH record has invalid code");
951 return Failure;
952 }
953
Douglas Gregor258ae542009-04-27 06:38:32 +0000954 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +0000955 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
956 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +0000957 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +0000958
Douglas Gregore6648fb2009-04-28 20:33:11 +0000959 if (strcmp(Name, "<built-in>") == 0) {
960 PCHPredefinesBufferID = BufferID;
961 PCHPredefines = BlobStart;
962 PCHPredefinesLen = BlobLen - 1;
963 }
Douglas Gregor258ae542009-04-27 06:38:32 +0000964
965 break;
966 }
967
968 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump11289f42009-09-09 15:08:12 +0000969 SourceLocation SpellingLoc
Douglas Gregor258ae542009-04-27 06:38:32 +0000970 = SourceLocation::getFromRawEncoding(Record[1]);
971 SourceMgr.createInstantiationLoc(SpellingLoc,
972 SourceLocation::getFromRawEncoding(Record[2]),
973 SourceLocation::getFromRawEncoding(Record[3]),
974 Record[4],
975 ID,
976 Record[0]);
977 break;
Mike Stump11289f42009-09-09 15:08:12 +0000978 }
Douglas Gregor258ae542009-04-27 06:38:32 +0000979 }
980
981 return Success;
982}
983
Chris Lattnere78a6be2009-04-27 01:05:14 +0000984/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
985/// specified cursor. Read the abbreviations that are at the top of the block
986/// and then leave the cursor pointing into the block.
987bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
988 unsigned BlockID) {
989 if (Cursor.EnterSubBlock(BlockID)) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000990 Error("malformed block record in PCH file");
Chris Lattnere78a6be2009-04-27 01:05:14 +0000991 return Failure;
992 }
Mike Stump11289f42009-09-09 15:08:12 +0000993
Chris Lattnere78a6be2009-04-27 01:05:14 +0000994 while (true) {
995 unsigned Code = Cursor.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +0000996
Chris Lattnere78a6be2009-04-27 01:05:14 +0000997 // We expect all abbrevs to be at the start of the block.
998 if (Code != llvm::bitc::DEFINE_ABBREV)
999 return false;
1000 Cursor.ReadAbbrevRecord();
1001 }
1002}
1003
Douglas Gregorc3366a52009-04-21 23:56:24 +00001004void PCHReader::ReadMacroRecord(uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001005 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump11289f42009-09-09 15:08:12 +00001006
Douglas Gregorc3366a52009-04-21 23:56:24 +00001007 // Keep track of where we are in the stream, then jump back there
1008 // after reading this macro.
1009 SavedStreamPosition SavedPosition(Stream);
1010
1011 Stream.JumpToBit(Offset);
1012 RecordData Record;
1013 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1014 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001015
Douglas Gregorc3366a52009-04-21 23:56:24 +00001016 while (true) {
1017 unsigned Code = Stream.ReadCode();
1018 switch (Code) {
1019 case llvm::bitc::END_BLOCK:
1020 return;
1021
1022 case llvm::bitc::ENTER_SUBBLOCK:
1023 // No known subblocks, always skip them.
1024 Stream.ReadSubBlockID();
1025 if (Stream.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001026 Error("malformed block record in PCH file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001027 return;
1028 }
1029 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001030
Douglas Gregorc3366a52009-04-21 23:56:24 +00001031 case llvm::bitc::DEFINE_ABBREV:
1032 Stream.ReadAbbrevRecord();
1033 continue;
1034 default: break;
1035 }
1036
1037 // Read a record.
1038 Record.clear();
1039 pch::PreprocessorRecordTypes RecType =
1040 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1041 switch (RecType) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001042 case pch::PP_MACRO_OBJECT_LIKE:
1043 case pch::PP_MACRO_FUNCTION_LIKE: {
1044 // If we already have a macro, that means that we've hit the end
1045 // of the definition of the macro we were looking for. We're
1046 // done.
1047 if (Macro)
1048 return;
1049
1050 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1051 if (II == 0) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001052 Error("macro must have a name in PCH file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001053 return;
1054 }
1055 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1056 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001057
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001058 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001059 MI->setIsUsed(isUsed);
Mike Stump11289f42009-09-09 15:08:12 +00001060
Douglas Gregoraae92242010-03-19 21:51:54 +00001061 unsigned NextIndex = 3;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001062 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1063 // Decode function-like macro info.
1064 bool isC99VarArgs = Record[3];
1065 bool isGNUVarArgs = Record[4];
1066 MacroArgs.clear();
1067 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001068 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001069 for (unsigned i = 0; i != NumArgs; ++i)
1070 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1071
1072 // Install function-like macro info.
1073 MI->setIsFunctionLike();
1074 if (isC99VarArgs) MI->setIsC99Varargs();
1075 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001076 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001077 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001078 }
1079
1080 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001081 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001082
1083 // Remember that we saw this macro last so that we add the tokens that
1084 // form its body to it.
1085 Macro = MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001086
1087 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1088 // We have a macro definition. Load it now.
1089 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1090 getMacroDefinition(Record[NextIndex]));
1091 }
1092
Douglas Gregorc3366a52009-04-21 23:56:24 +00001093 ++NumMacrosRead;
1094 break;
1095 }
Mike Stump11289f42009-09-09 15:08:12 +00001096
Douglas Gregorc3366a52009-04-21 23:56:24 +00001097 case pch::PP_TOKEN: {
1098 // If we see a TOKEN before a PP_MACRO_*, then the file is
1099 // erroneous, just pretend we didn't see this.
1100 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001101
Douglas Gregorc3366a52009-04-21 23:56:24 +00001102 Token Tok;
1103 Tok.startToken();
1104 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1105 Tok.setLength(Record[1]);
1106 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1107 Tok.setIdentifierInfo(II);
1108 Tok.setKind((tok::TokenKind)Record[3]);
1109 Tok.setFlag((Token::TokenFlags)Record[4]);
1110 Macro->AddTokenToBody(Tok);
1111 break;
1112 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001113
1114 case pch::PP_MACRO_INSTANTIATION: {
1115 // If we already have a macro, that means that we've hit the end
1116 // of the definition of the macro we were looking for. We're
1117 // done.
1118 if (Macro)
1119 return;
1120
1121 if (!PP->getPreprocessingRecord()) {
1122 Error("missing preprocessing record in PCH file");
1123 return;
1124 }
1125
1126 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1127 if (PPRec.getPreprocessedEntity(Record[0]))
1128 return;
1129
1130 MacroInstantiation *MI
1131 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1132 SourceRange(
1133 SourceLocation::getFromRawEncoding(Record[1]),
1134 SourceLocation::getFromRawEncoding(Record[2])),
1135 getMacroDefinition(Record[4]));
1136 PPRec.SetPreallocatedEntity(Record[0], MI);
1137 return;
1138 }
1139
1140 case pch::PP_MACRO_DEFINITION: {
1141 // If we already have a macro, that means that we've hit the end
1142 // of the definition of the macro we were looking for. We're
1143 // done.
1144 if (Macro)
1145 return;
1146
1147 if (!PP->getPreprocessingRecord()) {
1148 Error("missing preprocessing record in PCH file");
1149 return;
1150 }
1151
1152 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1153 if (PPRec.getPreprocessedEntity(Record[0]))
1154 return;
1155
1156 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1157 Error("out-of-bounds macro definition record");
1158 return;
1159 }
1160
1161 MacroDefinition *MD
1162 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1163 SourceLocation::getFromRawEncoding(Record[5]),
1164 SourceRange(
1165 SourceLocation::getFromRawEncoding(Record[2]),
1166 SourceLocation::getFromRawEncoding(Record[3])));
1167 PPRec.SetPreallocatedEntity(Record[0], MD);
1168 MacroDefinitionsLoaded[Record[1]] = MD;
1169 return;
1170 }
Steve Naroff3fa455a2009-04-24 20:03:17 +00001171 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001172 }
1173}
1174
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001175void PCHReader::ReadDefinedMacros() {
1176 // If there was no preprocessor block, do nothing.
1177 if (!MacroCursor.getBitStreamReader())
1178 return;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001179
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001180 llvm::BitstreamCursor Cursor = MacroCursor;
1181 if (Cursor.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) {
1182 Error("malformed preprocessor block record in PCH file");
1183 return;
1184 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001185
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001186 RecordData Record;
1187 while (true) {
1188 unsigned Code = Cursor.ReadCode();
1189 if (Code == llvm::bitc::END_BLOCK) {
1190 if (Cursor.ReadBlockEnd())
1191 Error("error at end of preprocessor block in PCH file");
1192 return;
1193 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001194
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001195 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1196 // No known subblocks, always skip them.
1197 Cursor.ReadSubBlockID();
1198 if (Cursor.SkipBlock()) {
1199 Error("malformed block record in PCH file");
1200 return;
1201 }
1202 continue;
1203 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001204
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001205 if (Code == llvm::bitc::DEFINE_ABBREV) {
1206 Cursor.ReadAbbrevRecord();
1207 continue;
1208 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001209
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001210 // Read a record.
1211 const char *BlobStart;
1212 unsigned BlobLen;
1213 Record.clear();
1214 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1215 default: // Default behavior: ignore.
1216 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001217
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001218 case pch::PP_MACRO_OBJECT_LIKE:
1219 case pch::PP_MACRO_FUNCTION_LIKE:
Douglas Gregoraae92242010-03-19 21:51:54 +00001220 DecodeIdentifierInfo(Record[0]);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001221 break;
1222
1223 case pch::PP_TOKEN:
1224 // Ignore tokens.
1225 break;
Douglas Gregoraae92242010-03-19 21:51:54 +00001226
1227 case pch::PP_MACRO_INSTANTIATION:
1228 case pch::PP_MACRO_DEFINITION:
1229 // Read the macro record.
1230 ReadMacroRecord(Cursor.GetCurrentBitNo());
1231 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001232 }
1233 }
1234}
1235
Douglas Gregoraae92242010-03-19 21:51:54 +00001236MacroDefinition *PCHReader::getMacroDefinition(pch::IdentID ID) {
1237 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1238 return 0;
1239
1240 if (!MacroDefinitionsLoaded[ID])
1241 ReadMacroRecord(MacroDefinitionOffsets[ID]);
1242
1243 return MacroDefinitionsLoaded[ID];
1244}
1245
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001246/// \brief If we are loading a relocatable PCH file, and the filename is
1247/// not an absolute path, add the system root to the beginning of the file
1248/// name.
1249void PCHReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1250 // If this is not a relocatable PCH file, there's nothing to do.
1251 if (!RelocatablePCH)
1252 return;
Mike Stump11289f42009-09-09 15:08:12 +00001253
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001254 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001255 return;
1256
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001257 if (isysroot == 0) {
1258 // If no system root was given, default to '/'
1259 Filename.insert(Filename.begin(), '/');
1260 return;
1261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001263 unsigned Length = strlen(isysroot);
1264 if (isysroot[Length - 1] != '/')
1265 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001266
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001267 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1268}
1269
Mike Stump11289f42009-09-09 15:08:12 +00001270PCHReader::PCHReadResult
Douglas Gregoreda6a892009-04-26 00:07:37 +00001271PCHReader::ReadPCHBlock() {
Douglas Gregor55abb232009-04-10 20:39:37 +00001272 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001273 Error("malformed block record in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001274 return Failure;
1275 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001276
1277 // Read all of the records and blocks for the PCH file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001278 RecordData Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001279 while (!Stream.AtEndOfStream()) {
1280 unsigned Code = Stream.ReadCode();
1281 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001282 if (Stream.ReadBlockEnd()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001283 Error("error at end of module block in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001284 return Failure;
1285 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001286
Douglas Gregor55abb232009-04-10 20:39:37 +00001287 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001288 }
1289
1290 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1291 switch (Stream.ReadSubBlockID()) {
Douglas Gregor12bfa382009-10-17 00:13:19 +00001292 case pch::DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001293 // We lazily load the decls block, but we want to set up the
1294 // DeclsCursor cursor to point into it. Clone our current bitcode
1295 // cursor to it, enter the block and read the abbrevs in that block.
1296 // With the main cursor, we just skip over it.
1297 DeclsCursor = Stream;
1298 if (Stream.SkipBlock() || // Skip with the main cursor.
1299 // Read the abbrevs.
Douglas Gregor12bfa382009-10-17 00:13:19 +00001300 ReadBlockAbbrevs(DeclsCursor, pch::DECLTYPES_BLOCK_ID)) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001301 Error("malformed block record in PCH file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001302 return Failure;
1303 }
1304 break;
Mike Stump11289f42009-09-09 15:08:12 +00001305
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001306 case pch::PREPROCESSOR_BLOCK_ID:
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001307 MacroCursor = Stream;
1308 if (PP)
1309 PP->setExternalSource(this);
1310
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001311 if (Stream.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001312 Error("malformed block record in PCH file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001313 return Failure;
1314 }
1315 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001316
Douglas Gregora7f71a92009-04-10 03:52:48 +00001317 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00001318 switch (ReadSourceManagerBlock()) {
1319 case Success:
1320 break;
1321
1322 case Failure:
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001323 Error("malformed source manager block in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001324 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001325
1326 case IgnorePCH:
1327 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001328 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001329 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001330 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001331 continue;
1332 }
1333
1334 if (Code == llvm::bitc::DEFINE_ABBREV) {
1335 Stream.ReadAbbrevRecord();
1336 continue;
1337 }
1338
1339 // Read and process a record.
1340 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001341 const char *BlobStart = 0;
1342 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001343 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregorbfbde532009-04-10 21:16:55 +00001344 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001345 default: // Default behavior: ignore.
1346 break;
1347
1348 case pch::TYPE_OFFSET:
Douglas Gregor745ed142009-04-25 18:35:21 +00001349 if (!TypesLoaded.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001350 Error("duplicate TYPE_OFFSET record in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001351 return Failure;
1352 }
Chris Lattnereeb05692009-04-27 18:24:17 +00001353 TypeOffsets = (const uint32_t *)BlobStart;
Douglas Gregor745ed142009-04-25 18:35:21 +00001354 TypesLoaded.resize(Record[0]);
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001355 break;
1356
1357 case pch::DECL_OFFSET:
Douglas Gregor745ed142009-04-25 18:35:21 +00001358 if (!DeclsLoaded.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001359 Error("duplicate DECL_OFFSET record in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001360 return Failure;
1361 }
Chris Lattnereeb05692009-04-27 18:24:17 +00001362 DeclOffsets = (const uint32_t *)BlobStart;
Douglas Gregor745ed142009-04-25 18:35:21 +00001363 DeclsLoaded.resize(Record[0]);
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001364 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001365
1366 case pch::LANGUAGE_OPTIONS:
1367 if (ParseLanguageOptions(Record))
1368 return IgnorePCH;
1369 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001370
Douglas Gregor7b71e632009-04-27 22:23:34 +00001371 case pch::METADATA: {
1372 if (Record[0] != pch::VERSION_MAJOR) {
1373 Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
1374 : diag::warn_pch_version_too_new);
1375 return IgnorePCH;
1376 }
1377
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001378 RelocatablePCH = Record[4];
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001379 if (Listener) {
1380 std::string TargetTriple(BlobStart, BlobLen);
1381 if (Listener->ReadTargetTriple(TargetTriple))
1382 return IgnorePCH;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001383 }
1384 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001385 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001386
1387 case pch::IDENTIFIER_TABLE:
Douglas Gregora868bbd2009-04-21 22:25:48 +00001388 IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001389 if (Record[0]) {
Mike Stump11289f42009-09-09 15:08:12 +00001390 IdentifierLookupTable
Douglas Gregor0e149972009-04-25 19:10:14 +00001391 = PCHIdentifierLookupTable::Create(
Douglas Gregora868bbd2009-04-21 22:25:48 +00001392 (const unsigned char *)IdentifierTableData + Record[0],
Mike Stump11289f42009-09-09 15:08:12 +00001393 (const unsigned char *)IdentifierTableData,
Douglas Gregora868bbd2009-04-21 22:25:48 +00001394 PCHIdentifierLookupTrait(*this));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001395 if (PP)
1396 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001397 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001398 break;
1399
1400 case pch::IDENTIFIER_OFFSET:
Douglas Gregor0e149972009-04-25 19:10:14 +00001401 if (!IdentifiersLoaded.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001402 Error("duplicate IDENTIFIER_OFFSET record in PCH file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001403 return Failure;
1404 }
Douglas Gregor0e149972009-04-25 19:10:14 +00001405 IdentifierOffsets = (const uint32_t *)BlobStart;
1406 IdentifiersLoaded.resize(Record[0]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001407 if (PP)
1408 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001409 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001410
1411 case pch::EXTERNAL_DEFINITIONS:
1412 if (!ExternalDefinitions.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001413 Error("duplicate EXTERNAL_DEFINITIONS record in PCH file");
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001414 return Failure;
1415 }
1416 ExternalDefinitions.swap(Record);
1417 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001418
Douglas Gregor652d82a2009-04-18 05:55:16 +00001419 case pch::SPECIAL_TYPES:
1420 SpecialTypes.swap(Record);
1421 break;
1422
Douglas Gregor08f01292009-04-17 22:13:46 +00001423 case pch::STATISTICS:
1424 TotalNumStatements = Record[0];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001425 TotalNumMacros = Record[1];
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001426 TotalLexicalDeclContexts = Record[2];
1427 TotalVisibleDeclContexts = Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001428 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001429
Douglas Gregord4df8652009-04-22 22:02:47 +00001430 case pch::TENTATIVE_DEFINITIONS:
1431 if (!TentativeDefinitions.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001432 Error("duplicate TENTATIVE_DEFINITIONS record in PCH file");
Douglas Gregord4df8652009-04-22 22:02:47 +00001433 return Failure;
1434 }
1435 TentativeDefinitions.swap(Record);
1436 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001437
Tanya Lattner90073802010-02-12 00:07:30 +00001438 case pch::UNUSED_STATIC_FUNCS:
1439 if (!UnusedStaticFuncs.empty()) {
1440 Error("duplicate UNUSED_STATIC_FUNCS record in PCH file");
1441 return Failure;
1442 }
1443 UnusedStaticFuncs.swap(Record);
1444 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001445
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001446 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1447 if (!LocallyScopedExternalDecls.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001448 Error("duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001449 return Failure;
1450 }
1451 LocallyScopedExternalDecls.swap(Record);
1452 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001453
Douglas Gregor95c13f52009-04-25 17:48:32 +00001454 case pch::SELECTOR_OFFSETS:
1455 SelectorOffsets = (const uint32_t *)BlobStart;
1456 TotalNumSelectors = Record[0];
1457 SelectorsLoaded.resize(TotalNumSelectors);
1458 break;
1459
Douglas Gregorc78d3462009-04-24 21:10:55 +00001460 case pch::METHOD_POOL:
Douglas Gregor95c13f52009-04-25 17:48:32 +00001461 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1462 if (Record[0])
Mike Stump11289f42009-09-09 15:08:12 +00001463 MethodPoolLookupTable
Douglas Gregor95c13f52009-04-25 17:48:32 +00001464 = PCHMethodPoolLookupTable::Create(
1465 MethodPoolLookupTableData + Record[0],
Mike Stump11289f42009-09-09 15:08:12 +00001466 MethodPoolLookupTableData,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001467 PCHMethodPoolLookupTrait(*this));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001468 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00001469 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001470
1471 case pch::PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001472 if (!Record.empty() && Listener)
1473 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001474 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001475
1476 case pch::SOURCE_LOCATION_OFFSETS:
Chris Lattner12d61d32009-04-27 19:01:47 +00001477 SLocOffsets = (const uint32_t *)BlobStart;
Douglas Gregor258ae542009-04-27 06:38:32 +00001478 TotalNumSLocEntries = Record[0];
Douglas Gregord54f3a12009-10-05 21:07:28 +00001479 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001480 break;
1481
1482 case pch::SOURCE_LOCATION_PRELOADS:
1483 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
1484 PCHReadResult Result = ReadSLocEntryRecord(Record[I]);
1485 if (Result != Success)
1486 return Result;
1487 }
1488 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00001489
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001490 case pch::STAT_CACHE: {
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001491 PCHStatCache *MyStatCache =
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001492 new PCHStatCache((const unsigned char *)BlobStart + Record[0],
1493 (const unsigned char *)BlobStart,
1494 NumStatHits, NumStatMisses);
1495 FileMgr.addStatCache(MyStatCache);
1496 StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00001497 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001498 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001499
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001500 case pch::EXT_VECTOR_DECLS:
1501 if (!ExtVectorDecls.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001502 Error("duplicate EXT_VECTOR_DECLS record in PCH file");
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001503 return Failure;
1504 }
1505 ExtVectorDecls.swap(Record);
1506 break;
1507
Douglas Gregor45fe0362009-05-12 01:31:05 +00001508 case pch::ORIGINAL_FILE_NAME:
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00001509 ActualOriginalFileName.assign(BlobStart, BlobLen);
1510 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001511 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001512 break;
Mike Stump11289f42009-09-09 15:08:12 +00001513
Ted Kremenek17437132010-01-22 20:59:36 +00001514 case pch::VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00001515 const std::string &CurBranch = getClangFullRepositoryVersion();
Ted Kremenek2377a0e2010-01-22 20:55:35 +00001516 llvm::StringRef PCHBranch(BlobStart, BlobLen);
Ted Kremenek8bd09292010-02-12 23:31:14 +00001517 if (llvm::StringRef(CurBranch) != PCHBranch) {
Douglas Gregord54f3a12009-10-05 21:07:28 +00001518 Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch;
1519 return IgnorePCH;
1520 }
1521 break;
1522 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001523
1524 case pch::MACRO_DEFINITION_OFFSETS:
1525 MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1526 if (PP) {
1527 if (!PP->getPreprocessingRecord())
1528 PP->createPreprocessingRecord();
1529 PP->getPreprocessingRecord()->SetExternalSource(*this, Record[0]);
1530 } else {
1531 NumPreallocatedPreprocessingEntities = Record[0];
1532 }
1533
1534 MacroDefinitionsLoaded.resize(Record[1]);
1535 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001536 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001537 }
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001538 Error("premature end of bitstream in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001539 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001540}
1541
Douglas Gregor92863e42009-04-10 23:10:45 +00001542PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001543 // Set the PCH file name.
1544 this->FileName = FileName;
1545
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001546 // Open the PCH file.
Daniel Dunbar2d925eb2009-09-22 05:38:01 +00001547 //
1548 // FIXME: This shouldn't be here, we should just take a raw_ostream.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001549 std::string ErrStr;
Daniel Dunbar69914f42009-11-10 00:46:19 +00001550 Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
Douglas Gregor92863e42009-04-10 23:10:45 +00001551 if (!Buffer) {
1552 Error(ErrStr.c_str());
1553 return IgnorePCH;
1554 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001555
1556 // Initialize the stream
Mike Stump11289f42009-09-09 15:08:12 +00001557 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Chris Lattner9356ace2009-04-26 20:59:20 +00001558 (const unsigned char *)Buffer->getBufferEnd());
1559 Stream.init(StreamFile);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001560
1561 // Sniff for the signature.
1562 if (Stream.Read(8) != 'C' ||
1563 Stream.Read(8) != 'P' ||
1564 Stream.Read(8) != 'C' ||
Douglas Gregor92863e42009-04-10 23:10:45 +00001565 Stream.Read(8) != 'H') {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001566 Diag(diag::err_not_a_pch_file) << FileName;
1567 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001568 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001569
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001570 while (!Stream.AtEndOfStream()) {
1571 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001572
Douglas Gregor92863e42009-04-10 23:10:45 +00001573 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001574 Error("invalid record at top-level of PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001575 return Failure;
1576 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001577
1578 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00001579
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001580 // We only know the PCH subblock ID.
1581 switch (BlockID) {
1582 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00001583 if (Stream.ReadBlockInfoBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001584 Error("malformed BlockInfoBlock in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001585 return Failure;
1586 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001587 break;
1588 case pch::PCH_BLOCK_ID:
Douglas Gregoreda6a892009-04-26 00:07:37 +00001589 switch (ReadPCHBlock()) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001590 case Success:
1591 break;
1592
1593 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00001594 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00001595
1596 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00001597 // FIXME: We could consider reading through to the end of this
1598 // PCH block, skipping subblocks, to see if there are other
1599 // PCH blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00001600
1601 // Clear out any preallocated source location entries, so that
1602 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001603 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00001604
1605 // Remove the stat cache.
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001606 if (StatCache)
1607 FileMgr.removeStatCache((PCHStatCache*)StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00001608
Douglas Gregor92863e42009-04-10 23:10:45 +00001609 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001610 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001611 break;
1612 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00001613 if (Stream.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001614 Error("malformed block record in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001615 return Failure;
1616 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001617 break;
1618 }
Mike Stump11289f42009-09-09 15:08:12 +00001619 }
1620
Douglas Gregore6648fb2009-04-28 20:33:11 +00001621 // Check the predefines buffer.
Daniel Dunbar20a682d2009-11-11 00:52:11 +00001622 if (CheckPredefinesBuffer(llvm::StringRef(PCHPredefines, PCHPredefinesLen),
Douglas Gregore6648fb2009-04-28 20:33:11 +00001623 PCHPredefinesBufferID))
1624 return IgnorePCH;
Mike Stump11289f42009-09-09 15:08:12 +00001625
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001626 if (PP) {
Zhongxing Xu3f51f412009-07-18 09:26:51 +00001627 // Initialization of keywords and pragmas occurs before the
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001628 // PCH file is read, so there may be some identifiers that were
1629 // loaded into the IdentifierTable before we intercepted the
1630 // creation of identifiers. Iterate through the list of known
1631 // identifiers and determine whether we have to establish
1632 // preprocessor definitions or top-level identifier declaration
1633 // chains for those identifiers.
1634 //
1635 // We copy the IdentifierInfo pointers to a small vector first,
1636 // since de-serializing declarations or macro definitions can add
1637 // new entries into the identifier table, invalidating the
1638 // iterators.
1639 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1640 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1641 IdEnd = PP->getIdentifierTable().end();
1642 Id != IdEnd; ++Id)
1643 Identifiers.push_back(Id->second);
Mike Stump11289f42009-09-09 15:08:12 +00001644 PCHIdentifierLookupTable *IdTable
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001645 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1646 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1647 IdentifierInfo *II = Identifiers[I];
1648 // Look in the on-disk hash table for an entry for
1649 PCHIdentifierLookupTrait Info(*this, II);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001650 std::pair<const char*, unsigned> Key(II->getNameStart(), II->getLength());
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001651 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1652 if (Pos == IdTable->end())
1653 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001654
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001655 // Dereferencing the iterator has the effect of populating the
1656 // IdentifierInfo node with the various declarations it needs.
1657 (void)*Pos;
1658 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001659 }
1660
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001661 if (Context)
1662 InitializeContext(*Context);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001663
Douglas Gregora868bbd2009-04-21 22:25:48 +00001664 return Success;
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001665}
1666
Douglas Gregoraae92242010-03-19 21:51:54 +00001667void PCHReader::setPreprocessor(Preprocessor &pp) {
1668 PP = &pp;
1669
1670 if (NumPreallocatedPreprocessingEntities) {
1671 if (!PP->getPreprocessingRecord())
1672 PP->createPreprocessingRecord();
1673 PP->getPreprocessingRecord()->SetExternalSource(*this,
1674 NumPreallocatedPreprocessingEntities);
1675 NumPreallocatedPreprocessingEntities = 0;
1676 }
1677}
1678
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001679void PCHReader::InitializeContext(ASTContext &Ctx) {
1680 Context = &Ctx;
1681 assert(Context && "Passed null context!");
1682
1683 assert(PP && "Forgot to set Preprocessor ?");
1684 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1685 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001686 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001687
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001688 // Load the translation unit declaration
1689 ReadDeclRecord(DeclOffsets[0], 0);
1690
1691 // Load the special types.
1692 Context->setBuiltinVaListType(
1693 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1694 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
1695 Context->setObjCIdType(GetType(Id));
1696 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
1697 Context->setObjCSelType(GetType(Sel));
1698 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
1699 Context->setObjCProtoType(GetType(Proto));
1700 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
1701 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00001702
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001703 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
1704 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00001705 if (unsigned FastEnum
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001706 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
1707 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregor27821ce2009-07-07 16:35:42 +00001708 if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) {
1709 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001710 if (FileType.isNull()) {
1711 Error("FILE type is NULL");
1712 return;
1713 }
John McCall9dd450b2009-09-21 23:43:11 +00001714 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00001715 Context->setFILEDecl(Typedef->getDecl());
1716 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001717 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001718 if (!Tag) {
1719 Error("Invalid FILE type in PCH file");
1720 return;
1721 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00001722 Context->setFILEDecl(Tag->getDecl());
1723 }
1724 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00001725 if (unsigned Jmp_buf = SpecialTypes[pch::SPECIAL_TYPE_jmp_buf]) {
1726 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001727 if (Jmp_bufType.isNull()) {
1728 Error("jmp_bug type is NULL");
1729 return;
1730 }
John McCall9dd450b2009-09-21 23:43:11 +00001731 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00001732 Context->setjmp_bufDecl(Typedef->getDecl());
1733 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001734 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001735 if (!Tag) {
1736 Error("Invalid jmp_bug type in PCH file");
1737 return;
1738 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00001739 Context->setjmp_bufDecl(Tag->getDecl());
1740 }
1741 }
1742 if (unsigned Sigjmp_buf = SpecialTypes[pch::SPECIAL_TYPE_sigjmp_buf]) {
1743 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001744 if (Sigjmp_bufType.isNull()) {
1745 Error("sigjmp_buf type is NULL");
1746 return;
1747 }
John McCall9dd450b2009-09-21 23:43:11 +00001748 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00001749 Context->setsigjmp_bufDecl(Typedef->getDecl());
1750 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001751 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Mike Stumpa4de80b2009-07-28 02:25:19 +00001752 assert(Tag && "Invalid sigjmp_buf type in PCH file");
1753 Context->setsigjmp_bufDecl(Tag->getDecl());
1754 }
1755 }
Mike Stump11289f42009-09-09 15:08:12 +00001756 if (unsigned ObjCIdRedef
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001757 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID_REDEFINITION])
1758 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00001759 if (unsigned ObjCClassRedef
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001760 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
1761 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Mike Stumpd0153282009-10-20 02:12:22 +00001762 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR])
1763 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00001764 if (unsigned String
1765 = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
1766 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00001767 if (unsigned ObjCSelRedef
1768 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
1769 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
1770 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_NS_CONSTANT_STRING])
1771 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001772}
1773
Douglas Gregor45fe0362009-05-12 01:31:05 +00001774/// \brief Retrieve the name of the original source file name
1775/// directly from the PCH file, without actually loading the PCH
1776/// file.
Daniel Dunbar3b951482009-12-03 09:13:06 +00001777std::string PCHReader::getOriginalSourceFile(const std::string &PCHFileName,
1778 Diagnostic &Diags) {
Douglas Gregor45fe0362009-05-12 01:31:05 +00001779 // Open the PCH file.
1780 std::string ErrStr;
1781 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
1782 Buffer.reset(llvm::MemoryBuffer::getFile(PCHFileName.c_str(), &ErrStr));
1783 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00001784 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00001785 return std::string();
1786 }
1787
1788 // Initialize the stream
1789 llvm::BitstreamReader StreamFile;
1790 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00001791 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00001792 (const unsigned char *)Buffer->getBufferEnd());
1793 Stream.init(StreamFile);
1794
1795 // Sniff for the signature.
1796 if (Stream.Read(8) != 'C' ||
1797 Stream.Read(8) != 'P' ||
1798 Stream.Read(8) != 'C' ||
1799 Stream.Read(8) != 'H') {
Daniel Dunbar3b951482009-12-03 09:13:06 +00001800 Diags.Report(diag::err_fe_not_a_pch_file) << PCHFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00001801 return std::string();
1802 }
1803
1804 RecordData Record;
1805 while (!Stream.AtEndOfStream()) {
1806 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001807
Douglas Gregor45fe0362009-05-12 01:31:05 +00001808 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1809 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00001810
Douglas Gregor45fe0362009-05-12 01:31:05 +00001811 // We only know the PCH subblock ID.
1812 switch (BlockID) {
1813 case pch::PCH_BLOCK_ID:
1814 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00001815 Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00001816 return std::string();
1817 }
1818 break;
Mike Stump11289f42009-09-09 15:08:12 +00001819
Douglas Gregor45fe0362009-05-12 01:31:05 +00001820 default:
1821 if (Stream.SkipBlock()) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00001822 Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00001823 return std::string();
1824 }
1825 break;
1826 }
1827 continue;
1828 }
1829
1830 if (Code == llvm::bitc::END_BLOCK) {
1831 if (Stream.ReadBlockEnd()) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00001832 Diags.Report(diag::err_fe_pch_error_at_end_block) << PCHFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00001833 return std::string();
1834 }
1835 continue;
1836 }
1837
1838 if (Code == llvm::bitc::DEFINE_ABBREV) {
1839 Stream.ReadAbbrevRecord();
1840 continue;
1841 }
1842
1843 Record.clear();
1844 const char *BlobStart = 0;
1845 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001846 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Douglas Gregor45fe0362009-05-12 01:31:05 +00001847 == pch::ORIGINAL_FILE_NAME)
1848 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00001849 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00001850
1851 return std::string();
1852}
1853
Douglas Gregor55abb232009-04-10 20:39:37 +00001854/// \brief Parse the record that corresponds to a LangOptions data
1855/// structure.
1856///
1857/// This routine compares the language options used to generate the
1858/// PCH file against the language options set for the current
1859/// compilation. For each option, we classify differences between the
1860/// two compiler states as either "benign" or "important". Benign
1861/// differences don't matter, and we accept them without complaint
1862/// (and without modifying the language options). Differences between
1863/// the states for important options cause the PCH file to be
1864/// unusable, so we emit a warning and return true to indicate that
1865/// there was an error.
1866///
1867/// \returns true if the PCH file is unacceptable, false otherwise.
1868bool PCHReader::ParseLanguageOptions(
1869 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001870 if (Listener) {
1871 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00001872
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001873 #define PARSE_LANGOPT(Option) \
1874 LangOpts.Option = Record[Idx]; \
1875 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00001876
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001877 unsigned Idx = 0;
1878 PARSE_LANGOPT(Trigraphs);
1879 PARSE_LANGOPT(BCPLComment);
1880 PARSE_LANGOPT(DollarIdents);
1881 PARSE_LANGOPT(AsmPreprocessor);
1882 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00001883 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001884 PARSE_LANGOPT(ImplicitInt);
1885 PARSE_LANGOPT(Digraphs);
1886 PARSE_LANGOPT(HexFloats);
1887 PARSE_LANGOPT(C99);
1888 PARSE_LANGOPT(Microsoft);
1889 PARSE_LANGOPT(CPlusPlus);
1890 PARSE_LANGOPT(CPlusPlus0x);
1891 PARSE_LANGOPT(CXXOperatorNames);
1892 PARSE_LANGOPT(ObjC1);
1893 PARSE_LANGOPT(ObjC2);
1894 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00001895 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001896 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001897 PARSE_LANGOPT(PascalStrings);
1898 PARSE_LANGOPT(WritableStrings);
1899 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001900 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001901 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00001902 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001903 PARSE_LANGOPT(NeXTRuntime);
1904 PARSE_LANGOPT(Freestanding);
1905 PARSE_LANGOPT(NoBuiltin);
1906 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001907 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001908 PARSE_LANGOPT(Blocks);
1909 PARSE_LANGOPT(EmitAllDecls);
1910 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00001911 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
1912 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001913 PARSE_LANGOPT(HeinousExtensions);
1914 PARSE_LANGOPT(Optimize);
1915 PARSE_LANGOPT(OptimizeSize);
1916 PARSE_LANGOPT(Static);
1917 PARSE_LANGOPT(PICLevel);
1918 PARSE_LANGOPT(GNUInline);
1919 PARSE_LANGOPT(NoInline);
1920 PARSE_LANGOPT(AccessControl);
1921 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00001922 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00001923 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
1924 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00001925 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00001926 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001927 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001928 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00001929 PARSE_LANGOPT(CatchUndefined);
1930 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001931 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00001932
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001933 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00001934 }
Douglas Gregor55abb232009-04-10 20:39:37 +00001935
1936 return false;
1937}
1938
Douglas Gregoraae92242010-03-19 21:51:54 +00001939void PCHReader::ReadPreprocessedEntities() {
1940 ReadDefinedMacros();
1941}
1942
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001943/// \brief Read and return the type at the given offset.
1944///
1945/// This routine actually reads the record corresponding to the type
1946/// at the given offset in the bitstream. It is a helper routine for
1947/// GetType, which deals with reading type IDs.
1948QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001949 // Keep track of where we are in the stream, then jump back there
1950 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00001951 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001952
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00001953 ReadingKindTracker ReadingKind(Read_Type, *this);
1954
Douglas Gregor1342e842009-07-06 18:54:52 +00001955 // Note that we are loading a type record.
1956 LoadingTypeOrDecl Loading(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001957
Douglas Gregor12bfa382009-10-17 00:13:19 +00001958 DeclsCursor.JumpToBit(Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001959 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001960 unsigned Code = DeclsCursor.ReadCode();
1961 switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
Douglas Gregor455b8f42009-04-15 22:00:08 +00001962 case pch::TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001963 if (Record.size() != 2) {
1964 Error("Incorrect encoding of extended qualifier type");
1965 return QualType();
1966 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00001967 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00001968 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
1969 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00001970 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001971
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001972 case pch::TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001973 if (Record.size() != 1) {
1974 Error("Incorrect encoding of complex type");
1975 return QualType();
1976 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001977 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001978 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001979 }
1980
1981 case pch::TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001982 if (Record.size() != 1) {
1983 Error("Incorrect encoding of pointer type");
1984 return QualType();
1985 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001986 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001987 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001988 }
1989
1990 case pch::TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001991 if (Record.size() != 1) {
1992 Error("Incorrect encoding of block pointer type");
1993 return QualType();
1994 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001995 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001996 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001997 }
1998
1999 case pch::TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002000 if (Record.size() != 1) {
2001 Error("Incorrect encoding of lvalue reference type");
2002 return QualType();
2003 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002004 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002005 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002006 }
2007
2008 case pch::TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002009 if (Record.size() != 1) {
2010 Error("Incorrect encoding of rvalue reference type");
2011 return QualType();
2012 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002013 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002014 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002015 }
2016
2017 case pch::TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002018 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002019 Error("Incorrect encoding of member pointer type");
2020 return QualType();
2021 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002022 QualType PointeeType = GetType(Record[0]);
2023 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002024 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002025 }
2026
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002027 case pch::TYPE_CONSTANT_ARRAY: {
2028 QualType ElementType = GetType(Record[0]);
2029 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2030 unsigned IndexTypeQuals = Record[2];
2031 unsigned Idx = 3;
2032 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002033 return Context->getConstantArrayType(ElementType, Size,
2034 ASM, IndexTypeQuals);
2035 }
2036
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002037 case pch::TYPE_INCOMPLETE_ARRAY: {
2038 QualType ElementType = GetType(Record[0]);
2039 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2040 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002041 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002042 }
2043
2044 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002045 QualType ElementType = GetType(Record[0]);
2046 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2047 unsigned IndexTypeQuals = Record[2];
Douglas Gregor04318252009-07-06 15:59:29 +00002048 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2049 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002050 return Context->getVariableArrayType(ElementType, ReadExpr(),
Douglas Gregor04318252009-07-06 15:59:29 +00002051 ASM, IndexTypeQuals,
2052 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002053 }
2054
2055 case pch::TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002056 if (Record.size() != 3) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002057 Error("incorrect encoding of vector type in PCH file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002058 return QualType();
2059 }
2060
2061 QualType ElementType = GetType(Record[0]);
2062 unsigned NumElements = Record[1];
Chris Lattner37141f42010-06-23 06:00:24 +00002063 unsigned AltiVecSpec = Record[2];
2064 return Context->getVectorType(ElementType, NumElements,
2065 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002066 }
2067
2068 case pch::TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002069 if (Record.size() != 3) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002070 Error("incorrect encoding of extended vector type in PCH file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002071 return QualType();
2072 }
2073
2074 QualType ElementType = GetType(Record[0]);
2075 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002076 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002077 }
2078
2079 case pch::TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002080 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002081 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002082 return QualType();
2083 }
2084 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002085 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002086 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002087 }
2088
2089 case pch::TYPE_FUNCTION_PROTO: {
2090 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002091 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002092 unsigned RegParm = Record[2];
2093 CallingConv CallConv = (CallingConv)Record[3];
2094 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002095 unsigned NumParams = Record[Idx++];
2096 llvm::SmallVector<QualType, 16> ParamTypes;
2097 for (unsigned I = 0; I != NumParams; ++I)
2098 ParamTypes.push_back(GetType(Record[Idx++]));
2099 bool isVariadic = Record[Idx++];
2100 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002101 bool hasExceptionSpec = Record[Idx++];
2102 bool hasAnyExceptionSpec = Record[Idx++];
2103 unsigned NumExceptions = Record[Idx++];
2104 llvm::SmallVector<QualType, 2> Exceptions;
2105 for (unsigned I = 0; I != NumExceptions; ++I)
2106 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002107 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002108 isVariadic, Quals, hasExceptionSpec,
2109 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002110 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002111 FunctionType::ExtInfo(NoReturn, RegParm,
2112 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002113 }
2114
John McCallb96ec562009-12-04 22:46:56 +00002115 case pch::TYPE_UNRESOLVED_USING:
2116 return Context->getTypeDeclType(
2117 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2118
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002119 case pch::TYPE_TYPEDEF: {
2120 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002121 Error("incorrect encoding of typedef type");
2122 return QualType();
2123 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002124 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2125 QualType Canonical = GetType(Record[1]);
2126 return Context->getTypedefType(Decl, Canonical);
2127 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002128
2129 case pch::TYPE_TYPEOF_EXPR:
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002130 return Context->getTypeOfExprType(ReadExpr());
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002131
2132 case pch::TYPE_TYPEOF: {
2133 if (Record.size() != 1) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002134 Error("incorrect encoding of typeof(type) in PCH file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002135 return QualType();
2136 }
2137 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002138 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002139 }
Mike Stump11289f42009-09-09 15:08:12 +00002140
Anders Carlsson81df7b82009-06-24 19:06:50 +00002141 case pch::TYPE_DECLTYPE:
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002142 return Context->getDecltypeType(ReadExpr());
Anders Carlsson81df7b82009-06-24 19:06:50 +00002143
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002144 case pch::TYPE_RECORD:
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002145 if (Record.size() != 1) {
2146 Error("incorrect encoding of record type");
2147 return QualType();
2148 }
Chris Lattner8575daa2009-04-27 21:45:14 +00002149 return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002150
Douglas Gregor1daeb692009-04-13 18:14:40 +00002151 case pch::TYPE_ENUM:
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002152 if (Record.size() != 1) {
2153 Error("incorrect encoding of enum type");
2154 return QualType();
2155 }
Chris Lattner8575daa2009-04-27 21:45:14 +00002156 return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
Douglas Gregor1daeb692009-04-13 18:14:40 +00002157
John McCallfcc33b02009-09-05 00:15:47 +00002158 case pch::TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002159 unsigned Idx = 0;
2160 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2161 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2162 QualType NamedType = GetType(Record[Idx++]);
2163 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002164 }
2165
Steve Naroffc277ad12009-07-18 15:33:26 +00002166 case pch::TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002167 unsigned Idx = 0;
2168 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002169 return Context->getObjCInterfaceType(ItfD);
2170 }
2171
2172 case pch::TYPE_OBJC_OBJECT: {
2173 unsigned Idx = 0;
2174 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002175 unsigned NumProtos = Record[Idx++];
2176 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2177 for (unsigned I = 0; I != NumProtos; ++I)
2178 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCall8b07ec22010-05-15 11:32:37 +00002179 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002180 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002181
Steve Narofffb4330f2009-06-17 22:40:22 +00002182 case pch::TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002183 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002184 QualType Pointee = GetType(Record[Idx++]);
2185 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002186 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002187
John McCallcebee162009-10-18 09:09:24 +00002188 case pch::TYPE_SUBST_TEMPLATE_TYPE_PARM: {
2189 unsigned Idx = 0;
2190 QualType Parm = GetType(Record[Idx++]);
2191 QualType Replacement = GetType(Record[Idx++]);
2192 return
2193 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2194 Replacement);
2195 }
John McCalle78aac42010-03-10 03:28:59 +00002196
2197 case pch::TYPE_INJECTED_CLASS_NAME: {
2198 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2199 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002200 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
2201 // for PCH reading, too much interdependencies.
2202 return
2203 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002204 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002205
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002206 case pch::TYPE_TEMPLATE_TYPE_PARM: {
2207 unsigned Idx = 0;
2208 unsigned Depth = Record[Idx++];
2209 unsigned Index = Record[Idx++];
2210 bool Pack = Record[Idx++];
2211 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2212 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2213 }
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002214
2215 case pch::TYPE_DEPENDENT_NAME: {
2216 unsigned Idx = 0;
2217 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2218 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2219 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002220 QualType Canon = GetType(Record[Idx++]);
2221 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002222 }
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002223
2224 case pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
2225 unsigned Idx = 0;
2226 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2227 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2228 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2229 unsigned NumArgs = Record[Idx++];
2230 llvm::SmallVector<TemplateArgument, 8> Args;
2231 Args.reserve(NumArgs);
2232 while (NumArgs--)
2233 Args.push_back(ReadTemplateArgument(Record, Idx));
2234 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2235 Args.size(), Args.data());
2236 }
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002237
2238 case pch::TYPE_DEPENDENT_SIZED_ARRAY: {
2239 unsigned Idx = 0;
2240
2241 // ArrayType
2242 QualType ElementType = GetType(Record[Idx++]);
2243 ArrayType::ArraySizeModifier ASM
2244 = (ArrayType::ArraySizeModifier)Record[Idx++];
2245 unsigned IndexTypeQuals = Record[Idx++];
2246
2247 // DependentSizedArrayType
2248 Expr *NumElts = ReadExpr();
2249 SourceRange Brackets = ReadSourceRange(Record, Idx);
2250
2251 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2252 IndexTypeQuals, Brackets);
2253 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002254
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002255 case pch::TYPE_TEMPLATE_SPECIALIZATION: {
2256 unsigned Idx = 0;
2257 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002258 llvm::SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002259 ReadTemplateArgumentList(Args, Record, Idx);
2260 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002261 if (Canon.isNull())
2262 return Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2263 Args.size());
2264 else
2265 return Context->getTemplateSpecializationType(Name, Args.data(),
2266 Args.size(), Canon);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002267 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002268 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002269 // Suppress a GCC warning
2270 return QualType();
2271}
2272
John McCall8f115c62009-10-16 21:56:05 +00002273namespace {
2274
2275class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
2276 PCHReader &Reader;
2277 const PCHReader::RecordData &Record;
2278 unsigned &Idx;
2279
2280public:
2281 TypeLocReader(PCHReader &Reader, const PCHReader::RecordData &Record,
2282 unsigned &Idx)
2283 : Reader(Reader), Record(Record), Idx(Idx) { }
2284
John McCall17001972009-10-18 01:05:36 +00002285 // We want compile-time assurance that we've enumerated all of
2286 // these, so unfortunately we have to declare them first, then
2287 // define them out-of-line.
2288#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00002289#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00002290 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00002291#include "clang/AST/TypeLocNodes.def"
2292
John McCall17001972009-10-18 01:05:36 +00002293 void VisitFunctionTypeLoc(FunctionTypeLoc);
2294 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00002295};
2296
2297}
2298
John McCall17001972009-10-18 01:05:36 +00002299void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00002300 // nothing to do
2301}
John McCall17001972009-10-18 01:05:36 +00002302void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +00002303 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2304 if (TL.needsExtraLocalData()) {
2305 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2306 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2307 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2308 TL.setModeAttr(Record[Idx++]);
2309 }
John McCall8f115c62009-10-16 21:56:05 +00002310}
John McCall17001972009-10-18 01:05:36 +00002311void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2312 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002313}
John McCall17001972009-10-18 01:05:36 +00002314void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2315 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002316}
John McCall17001972009-10-18 01:05:36 +00002317void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2318 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002319}
John McCall17001972009-10-18 01:05:36 +00002320void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2321 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002322}
John McCall17001972009-10-18 01:05:36 +00002323void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2324 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002325}
John McCall17001972009-10-18 01:05:36 +00002326void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2327 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002328}
John McCall17001972009-10-18 01:05:36 +00002329void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2330 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2331 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002332 if (Record[Idx++])
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002333 TL.setSizeExpr(Reader.ReadExpr());
Douglas Gregor12bfa382009-10-17 00:13:19 +00002334 else
John McCall17001972009-10-18 01:05:36 +00002335 TL.setSizeExpr(0);
2336}
2337void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2338 VisitArrayTypeLoc(TL);
2339}
2340void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2341 VisitArrayTypeLoc(TL);
2342}
2343void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2344 VisitArrayTypeLoc(TL);
2345}
2346void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2347 DependentSizedArrayTypeLoc TL) {
2348 VisitArrayTypeLoc(TL);
2349}
2350void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2351 DependentSizedExtVectorTypeLoc TL) {
2352 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2353}
2354void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2355 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2356}
2357void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2358 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2359}
2360void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2361 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2362 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2363 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00002364 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00002365 }
2366}
2367void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2368 VisitFunctionTypeLoc(TL);
2369}
2370void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2371 VisitFunctionTypeLoc(TL);
2372}
John McCallb96ec562009-12-04 22:46:56 +00002373void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2374 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2375}
John McCall17001972009-10-18 01:05:36 +00002376void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2377 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2378}
2379void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002380 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2381 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2382 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall17001972009-10-18 01:05:36 +00002383}
2384void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002385 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2386 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2387 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2388 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002389}
2390void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2391 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2392}
2393void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2394 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2395}
2396void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2397 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2398}
John McCall17001972009-10-18 01:05:36 +00002399void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2400 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2401}
John McCallcebee162009-10-18 09:09:24 +00002402void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2403 SubstTemplateTypeParmTypeLoc TL) {
2404 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2405}
John McCall17001972009-10-18 01:05:36 +00002406void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2407 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +00002408 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2409 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2410 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2411 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2412 TL.setArgLocInfo(i,
2413 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
2414 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002415}
Abramo Bagnara6150c882010-05-11 21:36:43 +00002416void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00002417 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2418 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002419}
John McCalle78aac42010-03-10 03:28:59 +00002420void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2421 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2422}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002423void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00002424 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2425 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002426 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2427}
John McCallc392f372010-06-11 00:33:02 +00002428void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2429 DependentTemplateSpecializationTypeLoc TL) {
2430 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2431 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2432 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2433 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2434 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2435 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2436 TL.setArgLocInfo(I,
2437 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
2438 Record, Idx));
2439}
John McCall17001972009-10-18 01:05:36 +00002440void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2441 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002442}
2443void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2444 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00002445 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2446 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2447 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2448 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002449}
John McCallfc93cf92009-10-22 22:37:11 +00002450void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2451 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallfc93cf92009-10-22 22:37:11 +00002452}
John McCall8f115c62009-10-16 21:56:05 +00002453
John McCallbcd03502009-12-07 02:54:59 +00002454TypeSourceInfo *PCHReader::GetTypeSourceInfo(const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00002455 unsigned &Idx) {
2456 QualType InfoTy = GetType(Record[Idx++]);
2457 if (InfoTy.isNull())
2458 return 0;
2459
John McCallbcd03502009-12-07 02:54:59 +00002460 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
John McCall8f115c62009-10-16 21:56:05 +00002461 TypeLocReader TLR(*this, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00002462 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00002463 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00002464 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00002465}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002466
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002467QualType PCHReader::GetType(pch::TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00002468 unsigned FastQuals = ID & Qualifiers::FastMask;
2469 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002470
2471 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2472 QualType T;
2473 switch ((pch::PredefinedTypeIDs)Index) {
2474 case pch::PREDEF_TYPE_NULL_ID: return QualType();
Chris Lattner8575daa2009-04-27 21:45:14 +00002475 case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2476 case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002477
2478 case pch::PREDEF_TYPE_CHAR_U_ID:
2479 case pch::PREDEF_TYPE_CHAR_S_ID:
2480 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00002481 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002482 break;
2483
Chris Lattner8575daa2009-04-27 21:45:14 +00002484 case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2485 case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2486 case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2487 case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2488 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002489 case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
Chris Lattner8575daa2009-04-27 21:45:14 +00002490 case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2491 case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2492 case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2493 case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2494 case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2495 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002496 case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
Chris Lattner8575daa2009-04-27 21:45:14 +00002497 case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2498 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2499 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2500 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2501 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002502 case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002503 case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2504 case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002505 case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2506 case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002507 case pch::PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002508 }
2509
2510 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00002511 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002512 }
2513
2514 Index -= pch::NUM_PREDEF_TYPE_IDS;
Steve Naroffc277ad12009-07-18 15:33:26 +00002515 //assert(Index < TypesLoaded.size() && "Type index out-of-range");
John McCall8ccfcb52009-09-24 19:53:00 +00002516 if (TypesLoaded[Index].isNull())
2517 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]);
Mike Stump11289f42009-09-09 15:08:12 +00002518
John McCall8ccfcb52009-09-24 19:53:00 +00002519 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002520}
2521
John McCall0ad16662009-10-29 08:12:44 +00002522TemplateArgumentLocInfo
2523PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2524 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002525 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00002526 switch (Kind) {
2527 case TemplateArgument::Expression:
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002528 return ReadExpr();
John McCall0ad16662009-10-29 08:12:44 +00002529 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002530 return GetTypeSourceInfo(Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002531 case TemplateArgument::Template: {
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002532 SourceRange QualifierRange = ReadSourceRange(Record, Index);
2533 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
2534 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002535 }
John McCall0ad16662009-10-29 08:12:44 +00002536 case TemplateArgument::Null:
2537 case TemplateArgument::Integral:
2538 case TemplateArgument::Declaration:
2539 case TemplateArgument::Pack:
2540 return TemplateArgumentLocInfo();
2541 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00002542 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00002543 return TemplateArgumentLocInfo();
2544}
2545
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002546TemplateArgumentLoc
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002547PCHReader::ReadTemplateArgumentLoc(const RecordData &Record, unsigned &Index) {
2548 TemplateArgument Arg = ReadTemplateArgument(Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002549
2550 if (Arg.getKind() == TemplateArgument::Expression) {
2551 if (Record[Index++]) // bool InfoHasSameExpr.
2552 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
2553 }
2554 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002555 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002556}
2557
John McCall75b960e2010-06-01 09:23:16 +00002558Decl *PCHReader::GetExternalDecl(uint32_t ID) {
2559 return GetDecl(ID);
2560}
2561
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002562Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002563 if (ID == 0)
2564 return 0;
2565
Douglas Gregor745ed142009-04-25 18:35:21 +00002566 if (ID > DeclsLoaded.size()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002567 Error("declaration ID out-of-range for PCH file");
Douglas Gregor745ed142009-04-25 18:35:21 +00002568 return 0;
2569 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002570
Douglas Gregor745ed142009-04-25 18:35:21 +00002571 unsigned Index = ID - 1;
2572 if (!DeclsLoaded[Index])
2573 ReadDeclRecord(DeclOffsets[Index], Index);
2574
2575 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002576}
2577
Chris Lattner9c28af02009-04-27 05:46:25 +00002578/// \brief Resolve the offset of a statement into a statement.
2579///
2580/// This operation will read a new statement from the external
2581/// source each time it is called, and is meant to be used via a
2582/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
John McCall75b960e2010-06-01 09:23:16 +00002583Stmt *PCHReader::GetExternalDeclStmt(uint64_t Offset) {
Chris Lattner1de76db2009-04-27 05:58:23 +00002584 // Since we know tha this statement is part of a decl, make sure to use the
2585 // decl cursor to read it.
2586 DeclsCursor.JumpToBit(Offset);
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002587 return ReadStmtFromStream(DeclsCursor);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00002588}
2589
John McCall75b960e2010-06-01 09:23:16 +00002590bool PCHReader::FindExternalLexicalDecls(const DeclContext *DC,
2591 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00002592 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002593 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002594
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002595 uint64_t Offset = DeclContextOffsets[DC].first;
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002596 if (Offset == 0) {
2597 Error("DeclContext has no lexical decls in storage");
2598 return true;
2599 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002600
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002601 // Keep track of where we are in the stream, then jump back there
2602 // after reading this context.
Chris Lattner72405d62009-04-27 07:35:40 +00002603 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002604
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002605 // Load the record containing all of the declarations lexically in
2606 // this context.
Chris Lattner72405d62009-04-27 07:35:40 +00002607 DeclsCursor.JumpToBit(Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002608 RecordData Record;
Chris Lattner72405d62009-04-27 07:35:40 +00002609 unsigned Code = DeclsCursor.ReadCode();
2610 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002611 if (RecCode != pch::DECL_CONTEXT_LEXICAL) {
2612 Error("Expected lexical block");
2613 return true;
2614 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002615
2616 // Load all of the declaration IDs
John McCall75b960e2010-06-01 09:23:16 +00002617 for (RecordData::iterator I = Record.begin(), E = Record.end(); I != E; ++I)
2618 Decls.push_back(GetDecl(*I));
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002619 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002620 return false;
2621}
2622
John McCall75b960e2010-06-01 09:23:16 +00002623DeclContext::lookup_result
2624PCHReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
2625 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00002626 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002627 "DeclContext has no visible decls in storage");
2628 uint64_t Offset = DeclContextOffsets[DC].second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002629 if (Offset == 0) {
2630 Error("DeclContext has no visible decls in storage");
John McCall75b960e2010-06-01 09:23:16 +00002631 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2632 DeclContext::lookup_iterator());
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002633 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002634
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002635 // Keep track of where we are in the stream, then jump back there
2636 // after reading this context.
Chris Lattner72405d62009-04-27 07:35:40 +00002637 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002638
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002639 // Load the record containing all of the declarations visible in
2640 // this context.
Chris Lattner72405d62009-04-27 07:35:40 +00002641 DeclsCursor.JumpToBit(Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002642 RecordData Record;
Chris Lattner72405d62009-04-27 07:35:40 +00002643 unsigned Code = DeclsCursor.ReadCode();
2644 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002645 if (RecCode != pch::DECL_CONTEXT_VISIBLE) {
2646 Error("Expected visible block");
John McCall75b960e2010-06-01 09:23:16 +00002647 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2648 DeclContext::lookup_iterator());
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002649 }
2650
John McCall75b960e2010-06-01 09:23:16 +00002651 llvm::SmallVector<VisibleDeclaration, 64> Decls;
2652 if (Record.empty()) {
2653 SetExternalVisibleDecls(DC, Decls);
2654 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2655 DeclContext::lookup_iterator());
2656 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002657
2658 unsigned Idx = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002659 while (Idx < Record.size()) {
2660 Decls.push_back(VisibleDeclaration());
2661 Decls.back().Name = ReadDeclarationName(Record, Idx);
2662
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002663 unsigned Size = Record[Idx++];
Chris Lattner72405d62009-04-27 07:35:40 +00002664 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002665 LoadedDecls.reserve(Size);
2666 for (unsigned I = 0; I < Size; ++I)
2667 LoadedDecls.push_back(Record[Idx++]);
2668 }
2669
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002670 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00002671
2672 SetExternalVisibleDecls(DC, Decls);
2673 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002674}
2675
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002676void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00002677 this->Consumer = Consumer;
2678
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002679 if (!Consumer)
2680 return;
2681
2682 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002683 // Force deserialization of this decl, which will cause it to be passed to
2684 // the consumer (or queued).
2685 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002686 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00002687
2688 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2689 DeclGroupRef DG(InterestingDecls[I]);
2690 Consumer->HandleTopLevelDecl(DG);
2691 }
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002692}
2693
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002694void PCHReader::PrintStats() {
2695 std::fprintf(stderr, "*** PCH Statistics:\n");
2696
Mike Stump11289f42009-09-09 15:08:12 +00002697 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00002698 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00002699 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00002700 unsigned NumDeclsLoaded
2701 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2702 (Decl *)0);
2703 unsigned NumIdentifiersLoaded
2704 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2705 IdentifiersLoaded.end(),
2706 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00002707 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00002708 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2709 SelectorsLoaded.end(),
2710 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00002711
Douglas Gregorc5046832009-04-27 18:38:38 +00002712 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
2713 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00002714 if (TotalNumSLocEntries)
2715 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
2716 NumSLocEntriesRead, TotalNumSLocEntries,
2717 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00002718 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00002719 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00002720 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2721 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2722 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00002723 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00002724 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2725 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00002726 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00002727 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00002728 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2729 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002730 if (TotalNumSelectors)
2731 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2732 NumSelectorsLoaded, TotalNumSelectors,
2733 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2734 if (TotalNumStatements)
2735 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2736 NumStatementsRead, TotalNumStatements,
2737 ((float)NumStatementsRead/TotalNumStatements * 100));
2738 if (TotalNumMacros)
2739 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2740 NumMacrosRead, TotalNumMacros,
2741 ((float)NumMacrosRead/TotalNumMacros * 100));
2742 if (TotalLexicalDeclContexts)
2743 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2744 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2745 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2746 * 100));
2747 if (TotalVisibleDeclContexts)
2748 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2749 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2750 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2751 * 100));
2752 if (TotalSelectorsInMethodPool) {
2753 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2754 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2755 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2756 * 100));
2757 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2758 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002759 std::fprintf(stderr, "\n");
2760}
2761
Douglas Gregora868bbd2009-04-21 22:25:48 +00002762void PCHReader::InitializeSema(Sema &S) {
2763 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002764 S.ExternalSource = this;
2765
Douglas Gregor7cd60f72009-04-22 21:15:06 +00002766 // Makes sure any declarations that were deserialized "too early"
2767 // still get added to the identifier's declaration chains.
2768 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2769 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2770 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002771 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00002772 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00002773
2774 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00002775 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00002776 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2777 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00002778 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00002779 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002780
Tanya Lattner90073802010-02-12 00:07:30 +00002781 // If there were any unused static functions, deserialize them and add to
2782 // Sema's list of unused static functions.
2783 for (unsigned I = 0, N = UnusedStaticFuncs.size(); I != N; ++I) {
2784 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(UnusedStaticFuncs[I]));
2785 SemaObj->UnusedStaticFuncs.push_back(FD);
2786 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002787
2788 // If there were any locally-scoped external declarations,
2789 // deserialize them and add them to Sema's table of locally-scoped
2790 // external declarations.
2791 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2792 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2793 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2794 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002795
2796 // If there were any ext_vector type declarations, deserialize them
2797 // and add them to Sema's vector of such declarations.
2798 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
2799 SemaObj->ExtVectorDecls.push_back(
2800 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002801}
2802
2803IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2804 // Try to find this name within our on-disk hash table
Mike Stump11289f42009-09-09 15:08:12 +00002805 PCHIdentifierLookupTable *IdTable
Douglas Gregora868bbd2009-04-21 22:25:48 +00002806 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2807 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2808 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2809 if (Pos == IdTable->end())
2810 return 0;
2811
2812 // Dereferencing the iterator has the effect of building the
2813 // IdentifierInfo node and populating it with the various
2814 // declarations it needs.
2815 return *Pos;
2816}
2817
Mike Stump11289f42009-09-09 15:08:12 +00002818std::pair<ObjCMethodList, ObjCMethodList>
Douglas Gregorc78d3462009-04-24 21:10:55 +00002819PCHReader::ReadMethodPool(Selector Sel) {
2820 if (!MethodPoolLookupTable)
2821 return std::pair<ObjCMethodList, ObjCMethodList>();
2822
2823 // Try to find this selector within our on-disk hash table.
2824 PCHMethodPoolLookupTable *PoolTable
2825 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2826 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002827 if (Pos == PoolTable->end()) {
2828 ++NumMethodPoolMisses;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002829 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002830 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002831
Douglas Gregor95c13f52009-04-25 17:48:32 +00002832 ++NumMethodPoolSelectorsRead;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002833 return *Pos;
2834}
2835
Douglas Gregor0e149972009-04-25 19:10:14 +00002836void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00002837 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002838 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00002839 IdentifiersLoaded[ID - 1] = II;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002840}
2841
Douglas Gregor1342e842009-07-06 18:54:52 +00002842/// \brief Set the globally-visible declarations associated with the given
2843/// identifier.
2844///
2845/// If the PCH reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00002846/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00002847/// them.
2848///
2849/// \param II an IdentifierInfo that refers to one or more globally-visible
2850/// declarations.
2851///
2852/// \param DeclIDs the set of declaration IDs with the name @p II that are
2853/// visible at global scope.
2854///
2855/// \param Nonrecursive should be true to indicate that the caller knows that
2856/// this call is non-recursive, and therefore the globally-visible declarations
2857/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00002858void
2859PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00002860 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
2861 bool Nonrecursive) {
2862 if (CurrentlyLoadingTypeOrDecl && !Nonrecursive) {
2863 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
2864 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
2865 PII.II = II;
2866 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
2867 PII.DeclIDs.push_back(DeclIDs[I]);
2868 return;
2869 }
Mike Stump11289f42009-09-09 15:08:12 +00002870
Douglas Gregor1342e842009-07-06 18:54:52 +00002871 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
2872 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
2873 if (SemaObj) {
2874 // Introduce this declaration into the translation-unit scope
2875 // and add it to the declaration chain for this identifier, so
2876 // that (unqualified) name lookup will find it.
2877 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
2878 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
2879 } else {
2880 // Queue this declaration so that it will be added to the
2881 // translation unit scope and identifier's declaration chain
2882 // once a Sema object is known.
2883 PreloadedDecls.push_back(D);
2884 }
2885 }
2886}
2887
Chris Lattnerc523d8e2009-04-11 21:15:38 +00002888IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002889 if (ID == 0)
2890 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002891
Douglas Gregor0e149972009-04-25 19:10:14 +00002892 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002893 Error("no identifier table in PCH file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002894 return 0;
2895 }
Mike Stump11289f42009-09-09 15:08:12 +00002896
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002897 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor0e149972009-04-25 19:10:14 +00002898 if (!IdentifiersLoaded[ID - 1]) {
2899 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor95272492009-04-25 21:21:38 +00002900 const char *Str = IdentifierTableData + Offset;
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002901
Douglas Gregorab4df582009-04-28 20:01:51 +00002902 // All of the strings in the PCH file are preceded by a 16-bit
2903 // length. Extract that 16-bit length to avoid having to execute
2904 // strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00002905 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
2906 // unsigned integers. This is important to avoid integer overflow when
2907 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00002908 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00002909 unsigned StrLen = (((unsigned) StrLenPtr[0])
2910 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Mike Stump11289f42009-09-09 15:08:12 +00002911 IdentifiersLoaded[ID - 1]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00002912 = &PP->getIdentifierTable().get(Str, StrLen);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002913 }
Mike Stump11289f42009-09-09 15:08:12 +00002914
Douglas Gregor0e149972009-04-25 19:10:14 +00002915 return IdentifiersLoaded[ID - 1];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002916}
2917
Douglas Gregor258ae542009-04-27 06:38:32 +00002918void PCHReader::ReadSLocEntry(unsigned ID) {
2919 ReadSLocEntryRecord(ID);
2920}
2921
Steve Naroff2ddea052009-04-23 10:39:46 +00002922Selector PCHReader::DecodeSelector(unsigned ID) {
2923 if (ID == 0)
2924 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00002925
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002926 if (!MethodPoolLookupTableData)
Steve Naroff2ddea052009-04-23 10:39:46 +00002927 return Selector();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002928
2929 if (ID > TotalNumSelectors) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002930 Error("selector ID out of range in PCH file");
Steve Naroff2ddea052009-04-23 10:39:46 +00002931 return Selector();
2932 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00002933
2934 unsigned Index = ID - 1;
2935 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2936 // Load this selector from the selector table.
2937 // FIXME: endianness portability issues with SelectorOffsets table
2938 PCHMethodPoolLookupTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002939 SelectorsLoaded[Index]
Douglas Gregor95c13f52009-04-25 17:48:32 +00002940 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2941 }
2942
2943 return SelectorsLoaded[Index];
Steve Naroff2ddea052009-04-23 10:39:46 +00002944}
2945
John McCall75b960e2010-06-01 09:23:16 +00002946Selector PCHReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00002947 return DecodeSelector(ID);
2948}
2949
John McCall75b960e2010-06-01 09:23:16 +00002950uint32_t PCHReader::GetNumExternalSelectors() {
Douglas Gregord720daf2010-04-06 17:30:22 +00002951 return TotalNumSelectors + 1;
2952}
2953
Mike Stump11289f42009-09-09 15:08:12 +00002954DeclarationName
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002955PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2956 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2957 switch (Kind) {
2958 case DeclarationName::Identifier:
2959 return DeclarationName(GetIdentifierInfo(Record, Idx));
2960
2961 case DeclarationName::ObjCZeroArgSelector:
2962 case DeclarationName::ObjCOneArgSelector:
2963 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00002964 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002965
2966 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00002967 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00002968 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002969
2970 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00002971 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00002972 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002973
2974 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00002975 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00002976 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002977
2978 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00002979 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002980 (OverloadedOperatorKind)Record[Idx++]);
2981
Alexis Hunt3d221f22009-11-29 07:34:05 +00002982 case DeclarationName::CXXLiteralOperatorName:
2983 return Context->DeclarationNames.getCXXLiteralOperatorName(
2984 GetIdentifierInfo(Record, Idx));
2985
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002986 case DeclarationName::CXXUsingDirective:
2987 return DeclarationName::getUsingDirectiveName();
2988 }
2989
2990 // Required to silence GCC warning
2991 return DeclarationName();
2992}
Douglas Gregor55abb232009-04-10 20:39:37 +00002993
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002994TemplateName
2995PCHReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
2996 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
2997 switch (Kind) {
2998 case TemplateName::Template:
2999 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3000
3001 case TemplateName::OverloadedTemplate: {
3002 unsigned size = Record[Idx++];
3003 UnresolvedSet<8> Decls;
3004 while (size--)
3005 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3006
3007 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3008 }
3009
3010 case TemplateName::QualifiedTemplate: {
3011 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3012 bool hasTemplKeyword = Record[Idx++];
3013 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3014 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3015 }
3016
3017 case TemplateName::DependentTemplate: {
3018 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3019 if (Record[Idx++]) // isIdentifier
3020 return Context->getDependentTemplateName(NNS,
3021 GetIdentifierInfo(Record, Idx));
3022 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003023 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003024 }
3025 }
3026
3027 assert(0 && "Unhandled template name kind!");
3028 return TemplateName();
3029}
3030
3031TemplateArgument
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003032PCHReader::ReadTemplateArgument(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003033 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3034 case TemplateArgument::Null:
3035 return TemplateArgument();
3036 case TemplateArgument::Type:
3037 return TemplateArgument(GetType(Record[Idx++]));
3038 case TemplateArgument::Declaration:
3039 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00003040 case TemplateArgument::Integral: {
3041 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3042 QualType T = GetType(Record[Idx++]);
3043 return TemplateArgument(Value, T);
3044 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003045 case TemplateArgument::Template:
3046 return TemplateArgument(ReadTemplateName(Record, Idx));
3047 case TemplateArgument::Expression:
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003048 return TemplateArgument(ReadExpr());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003049 case TemplateArgument::Pack: {
3050 unsigned NumArgs = Record[Idx++];
3051 llvm::SmallVector<TemplateArgument, 8> Args;
3052 Args.reserve(NumArgs);
3053 while (NumArgs--)
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003054 Args.push_back(ReadTemplateArgument(Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003055 TemplateArgument TemplArg;
3056 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3057 return TemplArg;
3058 }
3059 }
3060
3061 assert(0 && "Unhandled template argument kind!");
3062 return TemplateArgument();
3063}
3064
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003065TemplateParameterList *
3066PCHReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
3067 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3068 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3069 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3070
3071 unsigned NumParams = Record[Idx++];
3072 llvm::SmallVector<NamedDecl *, 16> Params;
3073 Params.reserve(NumParams);
3074 while (NumParams--)
3075 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3076
3077 TemplateParameterList* TemplateParams =
3078 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3079 Params.data(), Params.size(), RAngleLoc);
3080 return TemplateParams;
3081}
3082
3083void
3084PCHReader::
3085ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
3086 const RecordData &Record, unsigned &Idx) {
3087 unsigned NumTemplateArgs = Record[Idx++];
3088 TemplArgs.reserve(NumTemplateArgs);
3089 while (NumTemplateArgs--)
3090 TemplArgs.push_back(ReadTemplateArgument(Record, Idx));
3091}
3092
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003093/// \brief Read a UnresolvedSet structure.
3094void PCHReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
3095 const RecordData &Record, unsigned &Idx) {
3096 unsigned NumDecls = Record[Idx++];
3097 while (NumDecls--) {
3098 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3099 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3100 Set.addDecl(D, AS);
3101 }
3102}
3103
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003104CXXBaseSpecifier
3105PCHReader::ReadCXXBaseSpecifier(const RecordData &Record, unsigned &Idx) {
3106 bool isVirtual = static_cast<bool>(Record[Idx++]);
3107 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3108 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
3109 QualType T = GetType(Record[Idx++]);
3110 SourceRange Range = ReadSourceRange(Record, Idx);
3111 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, T);
3112}
3113
Chris Lattnerca025db2010-05-07 21:43:38 +00003114NestedNameSpecifier *
3115PCHReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
3116 unsigned N = Record[Idx++];
3117 NestedNameSpecifier *NNS = 0, *Prev = 0;
3118 for (unsigned I = 0; I != N; ++I) {
3119 NestedNameSpecifier::SpecifierKind Kind
3120 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3121 switch (Kind) {
3122 case NestedNameSpecifier::Identifier: {
3123 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3124 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3125 break;
3126 }
3127
3128 case NestedNameSpecifier::Namespace: {
3129 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3130 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3131 break;
3132 }
3133
3134 case NestedNameSpecifier::TypeSpec:
3135 case NestedNameSpecifier::TypeSpecWithTemplate: {
3136 Type *T = GetType(Record[Idx++]).getTypePtr();
3137 bool Template = Record[Idx++];
3138 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3139 break;
3140 }
3141
3142 case NestedNameSpecifier::Global: {
3143 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3144 // No associated value, and there can't be a prefix.
3145 break;
3146 }
3147 Prev = NNS;
3148 }
3149 }
3150 return NNS;
3151}
3152
3153SourceRange
3154PCHReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00003155 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3156 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3157 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00003158}
3159
Douglas Gregor1daeb692009-04-13 18:14:40 +00003160/// \brief Read an integral value
3161llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
3162 unsigned BitWidth = Record[Idx++];
3163 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3164 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3165 Idx += NumWords;
3166 return Result;
3167}
3168
3169/// \brief Read a signed integral value
3170llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
3171 bool isUnsigned = Record[Idx++];
3172 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3173}
3174
Douglas Gregore0a3a512009-04-14 21:55:33 +00003175/// \brief Read a floating-point value
3176llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003177 return llvm::APFloat(ReadAPInt(Record, Idx));
3178}
3179
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003180// \brief Read a string
3181std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
3182 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00003183 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003184 Idx += Len;
3185 return Result;
3186}
3187
Chris Lattnercba86142010-05-10 00:25:06 +00003188CXXTemporary *PCHReader::ReadCXXTemporary(const RecordData &Record,
3189 unsigned &Idx) {
3190 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3191 return CXXTemporary::Create(*Context, Decl);
3192}
3193
Douglas Gregor55abb232009-04-10 20:39:37 +00003194DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00003195 return Diag(SourceLocation(), DiagID);
3196}
3197
3198DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003199 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00003200}
Douglas Gregora9af1d12009-04-17 00:04:06 +00003201
Douglas Gregora868bbd2009-04-21 22:25:48 +00003202/// \brief Retrieve the identifier table associated with the
3203/// preprocessor.
3204IdentifierTable &PCHReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003205 assert(PP && "Forgot to set Preprocessor ?");
3206 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00003207}
3208
Douglas Gregora9af1d12009-04-17 00:04:06 +00003209/// \brief Record that the given ID maps to the given switch-case
3210/// statement.
3211void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3212 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3213 SwitchCaseStmts[ID] = SC;
3214}
3215
3216/// \brief Retrieve the switch-case statement with the given ID.
3217SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3218 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3219 return SwitchCaseStmts[ID];
3220}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003221
3222/// \brief Record that the given label statement has been
3223/// deserialized and has the given ID.
3224void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00003225 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003226 "Deserialized label twice");
3227 LabelStmts[ID] = S;
3228
3229 // If we've already seen any goto statements that point to this
3230 // label, resolve them now.
3231 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3232 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3233 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3234 Goto->second->setLabel(S);
3235 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00003236
3237 // If we've already seen any address-label statements that point to
3238 // this label, resolve them now.
3239 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00003240 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00003241 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00003242 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00003243 AddrLabel != AddrLabels.second; ++AddrLabel)
3244 AddrLabel->second->setLabel(S);
3245 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003246}
3247
3248/// \brief Set the label of the given statement to the label
3249/// identified by ID.
3250///
3251/// Depending on the order in which the label and other statements
3252/// referencing that label occur, this operation may complete
3253/// immediately (updating the statement) or it may queue the
3254/// statement to be back-patched later.
3255void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3256 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3257 if (Label != LabelStmts.end()) {
3258 // We've already seen this label, so set the label of the goto and
3259 // we're done.
3260 S->setLabel(Label->second);
3261 } else {
3262 // We haven't seen this label yet, so add this goto to the set of
3263 // unresolved goto statements.
3264 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3265 }
3266}
Douglas Gregor779d8652009-04-17 18:58:21 +00003267
3268/// \brief Set the label of the given expression to the label
3269/// identified by ID.
3270///
3271/// Depending on the order in which the label and other statements
3272/// referencing that label occur, this operation may complete
3273/// immediately (updating the statement) or it may queue the
3274/// statement to be back-patched later.
3275void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3276 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3277 if (Label != LabelStmts.end()) {
3278 // We've already seen this label, so set the label of the
3279 // label-address expression and we're done.
3280 S->setLabel(Label->second);
3281 } else {
3282 // We haven't seen this label yet, so add this label-address
3283 // expression to the set of unresolved label-address expressions.
3284 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3285 }
3286}
Douglas Gregor1342e842009-07-06 18:54:52 +00003287
3288
Mike Stump11289f42009-09-09 15:08:12 +00003289PCHReader::LoadingTypeOrDecl::LoadingTypeOrDecl(PCHReader &Reader)
Douglas Gregor1342e842009-07-06 18:54:52 +00003290 : Reader(Reader), Parent(Reader.CurrentlyLoadingTypeOrDecl) {
3291 Reader.CurrentlyLoadingTypeOrDecl = this;
3292}
3293
3294PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() {
3295 if (!Parent) {
3296 // If any identifiers with corresponding top-level declarations have
3297 // been loaded, load those declarations now.
3298 while (!Reader.PendingIdentifierInfos.empty()) {
3299 Reader.SetGloballyVisibleDecls(Reader.PendingIdentifierInfos.front().II,
3300 Reader.PendingIdentifierInfos.front().DeclIDs,
3301 true);
3302 Reader.PendingIdentifierInfos.pop_front();
3303 }
3304 }
3305
Mike Stump11289f42009-09-09 15:08:12 +00003306 Reader.CurrentlyLoadingTypeOrDecl = Parent;
Douglas Gregor1342e842009-07-06 18:54:52 +00003307}