blob: 96fc7d69f1f3e3219b9c58cf52c8415185150831 [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"
Douglas Gregora868bbd2009-04-21 22:25:48 +000016#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000017#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000021#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000022#include "clang/Lex/MacroInfo.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000023#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000024#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000025#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000027#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/FileManager.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000030#include "clang/Basic/Version.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamReader.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000035#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000036#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000037#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000041// PCH reader validator implementation
42//===----------------------------------------------------------------------===//
43
44PCHReaderListener::~PCHReaderListener() {}
45
46bool
47PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
48 const LangOptions &PPLangOpts = PP.getLangOptions();
49#define PARSE_LANGOPT_BENIGN(Option)
50#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
51 if (PPLangOpts.Option != LangOpts.Option) { \
52 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
53 return true; \
54 }
55
56 PARSE_LANGOPT_BENIGN(Trigraphs);
57 PARSE_LANGOPT_BENIGN(BCPLComment);
58 PARSE_LANGOPT_BENIGN(DollarIdents);
59 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
60 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
61 PARSE_LANGOPT_BENIGN(ImplicitInt);
62 PARSE_LANGOPT_BENIGN(Digraphs);
63 PARSE_LANGOPT_BENIGN(HexFloats);
64 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
65 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
66 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
67 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
68 PARSE_LANGOPT_BENIGN(CXXOperatorName);
69 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
70 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
71 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
72 PARSE_LANGOPT_BENIGN(PascalStrings);
73 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000074 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000075 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000076 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000077 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
78 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
79 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
80 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +000081 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000082 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +000083 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000084 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
85 PARSE_LANGOPT_BENIGN(EmitAllDecls);
86 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
87 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
Mike Stump11289f42009-09-09 15:08:12 +000088 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000089 diag::warn_pch_heinous_extensions);
90 // FIXME: Most of the options below are benign if the macro wasn't
91 // used. Unfortunately, this means that a PCH compiled without
92 // optimization can't be used with optimization turned on, even
93 // though the only thing that changes is whether __OPTIMIZE__ was
94 // defined... but if __OPTIMIZE__ never showed up in the header, it
95 // doesn't matter. We could consider making this some special kind
96 // of check.
97 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
98 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
99 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
100 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
101 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
102 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
103 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
104 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
105 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000106 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000107 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
108 return true;
109 }
110 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000111 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
112 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000113 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000114 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000115 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000116#undef PARSE_LANGOPT_IRRELEVANT
117#undef PARSE_LANGOPT_BENIGN
118
119 return false;
120}
121
122bool PCHValidator::ReadTargetTriple(const std::string &Triple) {
Daniel Dunbar40165182009-08-24 09:10:05 +0000123 if (Triple != PP.getTargetInfo().getTriple().getTriple()) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000124 Reader.Diag(diag::warn_pch_target_triple)
Daniel Dunbar40165182009-08-24 09:10:05 +0000125 << Triple << PP.getTargetInfo().getTriple().getTriple();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000126 return true;
127 }
128 return false;
129}
130
131/// \brief Split the given string into a vector of lines, eliminating
132/// any empty lines in the process.
133///
134/// \param Str the string to split.
135/// \param Len the length of Str.
136/// \param KeepEmptyLines true if empty lines should be included
137/// \returns a vector of lines, with the line endings removed
138static std::vector<std::string> splitLines(const char *Str, unsigned Len,
139 bool KeepEmptyLines = false) {
140 std::vector<std::string> Lines;
141 for (unsigned LineStart = 0; LineStart < Len; ++LineStart) {
142 unsigned LineEnd = LineStart;
143 while (LineEnd < Len && Str[LineEnd] != '\n')
144 ++LineEnd;
145 if (LineStart != LineEnd || KeepEmptyLines)
146 Lines.push_back(std::string(&Str[LineStart], &Str[LineEnd]));
147 LineStart = LineEnd;
148 }
149 return Lines;
150}
151
152/// \brief Determine whether the string Haystack starts with the
153/// substring Needle.
154static bool startsWith(const std::string &Haystack, const char *Needle) {
155 for (unsigned I = 0, N = Haystack.size(); Needle[I] != 0; ++I) {
156 if (I == N)
157 return false;
158 if (Haystack[I] != Needle[I])
159 return false;
160 }
161
162 return true;
163}
164
165/// \brief Determine whether the string Haystack starts with the
166/// substring Needle.
167static inline bool startsWith(const std::string &Haystack,
168 const std::string &Needle) {
169 return startsWith(Haystack, Needle.c_str());
170}
171
Mike Stump11289f42009-09-09 15:08:12 +0000172bool PCHValidator::ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000173 unsigned PCHPredefLen,
174 FileID PCHBufferID,
175 std::string &SuggestedPredefines) {
176 const char *Predef = PP.getPredefines().c_str();
177 unsigned PredefLen = PP.getPredefines().size();
178
179 // If the two predefines buffers compare equal, we're done!
Mike Stump11289f42009-09-09 15:08:12 +0000180 if (PredefLen == PCHPredefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000181 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
182 return false;
183
184 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000185
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000186 // The predefines buffers are different. Determine what the
187 // differences are, and whether they require us to reject the PCH
188 // file.
189 std::vector<std::string> CmdLineLines = splitLines(Predef, PredefLen);
190 std::vector<std::string> PCHLines = splitLines(PCHPredef, PCHPredefLen);
191
Mike Stump11289f42009-09-09 15:08:12 +0000192 // Sort both sets of predefined buffer lines, since
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000193 std::sort(CmdLineLines.begin(), CmdLineLines.end());
194 std::sort(PCHLines.begin(), PCHLines.end());
195
196 // Determine which predefines that where used to build the PCH file
197 // are missing from the command line.
198 std::vector<std::string> MissingPredefines;
199 std::set_difference(PCHLines.begin(), PCHLines.end(),
200 CmdLineLines.begin(), CmdLineLines.end(),
201 std::back_inserter(MissingPredefines));
202
203 bool MissingDefines = false;
204 bool ConflictingDefines = false;
205 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
206 const std::string &Missing = MissingPredefines[I];
207 if (!startsWith(Missing, "#define ") != 0) {
208 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
209 return true;
210 }
Mike Stump11289f42009-09-09 15:08:12 +0000211
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000212 // This is a macro definition. Determine the name of the macro
213 // we're defining.
214 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000215 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000216 = Missing.find_first_of("( \n\r", StartOfMacroName);
217 assert(EndOfMacroName != std::string::npos &&
218 "Couldn't find the end of the macro name");
219 std::string MacroName = Missing.substr(StartOfMacroName,
220 EndOfMacroName - StartOfMacroName);
221
222 // Determine whether this macro was given a different definition
223 // on the command line.
224 std::string MacroDefStart = "#define " + MacroName;
225 std::string::size_type MacroDefLen = MacroDefStart.size();
226 std::vector<std::string>::iterator ConflictPos
227 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
228 MacroDefStart);
229 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
230 if (!startsWith(*ConflictPos, MacroDefStart)) {
231 // Different macro; we're done.
232 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000233 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000234 }
Mike Stump11289f42009-09-09 15:08:12 +0000235
236 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000237 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000238 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000239 (*ConflictPos)[MacroDefLen] != '(')
240 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000241
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000242 // We found a conflicting macro definition.
243 break;
244 }
Mike Stump11289f42009-09-09 15:08:12 +0000245
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000246 if (ConflictPos != CmdLineLines.end()) {
247 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
248 << MacroName;
249
250 // Show the definition of this macro within the PCH file.
251 const char *MissingDef = strstr(PCHPredef, Missing.c_str());
252 unsigned Offset = MissingDef - PCHPredef;
253 SourceLocation PCHMissingLoc
254 = SourceMgr.getLocForStartOfFile(PCHBufferID)
255 .getFileLocWithOffset(Offset);
256 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as)
257 << MacroName;
258
259 ConflictingDefines = true;
260 continue;
261 }
Mike Stump11289f42009-09-09 15:08:12 +0000262
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000263 // If the macro doesn't conflict, then we'll just pick up the
264 // macro definition from the PCH file. Warn the user that they
265 // made a mistake.
266 if (ConflictingDefines)
267 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000268
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000269 if (!MissingDefines) {
270 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
271 MissingDefines = true;
272 }
273
274 // Show the definition of this macro within the PCH file.
275 const char *MissingDef = strstr(PCHPredef, Missing.c_str());
276 unsigned Offset = MissingDef - PCHPredef;
277 SourceLocation PCHMissingLoc
278 = SourceMgr.getLocForStartOfFile(PCHBufferID)
279 .getFileLocWithOffset(Offset);
280 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
281 }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000283 if (ConflictingDefines)
284 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000285
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000286 // Determine what predefines were introduced based on command-line
287 // parameters that were not present when building the PCH
288 // file. Extra #defines are okay, so long as the identifiers being
289 // defined were not used within the precompiled header.
290 std::vector<std::string> ExtraPredefines;
291 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
292 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000293 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000294 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
295 const std::string &Extra = ExtraPredefines[I];
296 if (!startsWith(Extra, "#define ") != 0) {
297 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
298 return true;
299 }
300
301 // This is an extra macro definition. Determine the name of the
302 // macro we're defining.
303 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000304 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000305 = Extra.find_first_of("( \n\r", StartOfMacroName);
306 assert(EndOfMacroName != std::string::npos &&
307 "Couldn't find the end of the macro name");
308 std::string MacroName = Extra.substr(StartOfMacroName,
309 EndOfMacroName - StartOfMacroName);
310
311 // Check whether this name was used somewhere in the PCH file. If
312 // so, defining it as a macro could change behavior, so we reject
313 // the PCH file.
314 if (IdentifierInfo *II = Reader.get(MacroName.c_str(),
315 MacroName.c_str() + MacroName.size())) {
316 Reader.Diag(diag::warn_macro_name_used_in_pch)
317 << II;
318 return true;
319 }
320
321 // Add this definition to the suggested predefines buffer.
322 SuggestedPredefines += Extra;
323 SuggestedPredefines += '\n';
324 }
325
326 // If we get here, it's because the predefines buffer had compatible
327 // contents. Accept the PCH file.
328 return false;
329}
330
331void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
332 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
333}
334
335void PCHValidator::ReadCounter(unsigned Value) {
336 PP.setCounterValue(Value);
337}
338
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000339//===----------------------------------------------------------------------===//
Douglas Gregora868bbd2009-04-21 22:25:48 +0000340// PCH reader implementation
341//===----------------------------------------------------------------------===//
342
Mike Stump11289f42009-09-09 15:08:12 +0000343PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context,
344 const char *isysroot)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000345 : Listener(new PCHValidator(PP, *this)), SourceMgr(PP.getSourceManager()),
346 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000347 SemaObj(0), PP(&PP), Context(Context), StatCache(0), Consumer(0),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000348 IdentifierTableData(0), IdentifierLookupTable(0),
349 IdentifierOffsets(0),
350 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
351 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000352 TotalNumSelectors(0), Comments(0), NumComments(0), isysroot(isysroot),
Mike Stump11289f42009-09-09 15:08:12 +0000353 NumStatHits(0), NumStatMisses(0),
354 NumSLocEntriesRead(0), NumStatementsRead(0),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000355 NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
Douglas Gregor0086a5a2009-07-07 00:12:59 +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}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000360
361PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr,
Mike Stump11289f42009-09-09 15:08:12 +0000362 Diagnostic &Diags, const char *isysroot)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000363 : SourceMgr(SourceMgr), FileMgr(FileMgr), Diags(Diags),
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000364 SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0),
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000365 IdentifierTableData(0), IdentifierLookupTable(0),
366 IdentifierOffsets(0),
367 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
368 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000369 TotalNumSelectors(0), Comments(0), NumComments(0), isysroot(isysroot),
Mike Stump11289f42009-09-09 15:08:12 +0000370 NumStatHits(0), NumStatMisses(0),
371 NumSLocEntriesRead(0), NumStatementsRead(0),
Douglas Gregor258ae542009-04-27 06:38:32 +0000372 NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
Douglas Gregor1342e842009-07-06 18:54:52 +0000373 NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0),
Mike Stump11289f42009-09-09 15:08:12 +0000374 CurrentlyLoadingTypeOrDecl(0) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000375 RelocatablePCH = false;
376}
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000377
378PCHReader::~PCHReader() {}
379
Chris Lattner1de76db2009-04-27 05:58:23 +0000380Expr *PCHReader::ReadDeclExpr() {
381 return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor));
382}
383
384Expr *PCHReader::ReadTypeExpr() {
Douglas Gregor12bfa382009-10-17 00:13:19 +0000385 return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor));
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000386}
387
388
Douglas Gregora868bbd2009-04-21 22:25:48 +0000389namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000390class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
391 PCHReader &Reader;
392
393public:
394 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
395
396 typedef Selector external_key_type;
397 typedef external_key_type internal_key_type;
398
399 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Douglas Gregorc78d3462009-04-24 21:10:55 +0000401 static bool EqualKey(const internal_key_type& a,
402 const internal_key_type& b) {
403 return a == b;
404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Douglas Gregorc78d3462009-04-24 21:10:55 +0000406 static unsigned ComputeHash(Selector Sel) {
407 unsigned N = Sel.getNumArgs();
408 if (N == 0)
409 ++N;
410 unsigned R = 5381;
411 for (unsigned I = 0; I != N; ++I)
412 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
413 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
414 return R;
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Douglas Gregorc78d3462009-04-24 21:10:55 +0000417 // This hopefully will just get inlined and removed by the optimizer.
418 static const internal_key_type&
419 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Douglas Gregorc78d3462009-04-24 21:10:55 +0000421 static std::pair<unsigned, unsigned>
422 ReadKeyDataLength(const unsigned char*& d) {
423 using namespace clang::io;
424 unsigned KeyLen = ReadUnalignedLE16(d);
425 unsigned DataLen = ReadUnalignedLE16(d);
426 return std::make_pair(KeyLen, DataLen);
427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Douglas Gregor95c13f52009-04-25 17:48:32 +0000429 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000430 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000431 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000432 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000433 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000434 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
435 if (N == 0)
436 return SelTable.getNullarySelector(FirstII);
437 else if (N == 1)
438 return SelTable.getUnarySelector(FirstII);
439
440 llvm::SmallVector<IdentifierInfo *, 16> Args;
441 Args.push_back(FirstII);
442 for (unsigned I = 1; I != N; ++I)
443 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
444
Douglas Gregor038c3382009-05-22 22:45:36 +0000445 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Douglas Gregorc78d3462009-04-24 21:10:55 +0000448 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
449 using namespace clang::io;
450 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
451 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
452
453 data_type Result;
454
455 // Load instance methods
456 ObjCMethodList *Prev = 0;
457 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000458 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000459 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
460 if (!Result.first.Method) {
461 // This is the first method, which is the easy case.
462 Result.first.Method = Method;
463 Prev = &Result.first;
464 continue;
465 }
466
467 Prev->Next = new ObjCMethodList(Method, 0);
468 Prev = Prev->Next;
469 }
470
471 // Load factory methods
472 Prev = 0;
473 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000474 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000475 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
476 if (!Result.second.Method) {
477 // This is the first method, which is the easy case.
478 Result.second.Method = Method;
479 Prev = &Result.second;
480 continue;
481 }
482
483 Prev->Next = new ObjCMethodList(Method, 0);
484 Prev = Prev->Next;
485 }
486
487 return Result;
488 }
489};
Mike Stump11289f42009-09-09 15:08:12 +0000490
491} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000492
493/// \brief The on-disk hash table used for the global method pool.
Mike Stump11289f42009-09-09 15:08:12 +0000494typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
Douglas Gregorc78d3462009-04-24 21:10:55 +0000495 PCHMethodPoolLookupTable;
496
497namespace {
Douglas Gregora868bbd2009-04-21 22:25:48 +0000498class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
499 PCHReader &Reader;
500
501 // If we know the IdentifierInfo in advance, it is here and we will
502 // not build a new one. Used when deserializing information about an
503 // identifier that was constructed before the PCH file was read.
504 IdentifierInfo *KnownII;
505
506public:
507 typedef IdentifierInfo * data_type;
508
509 typedef const std::pair<const char*, unsigned> external_key_type;
510
511 typedef external_key_type internal_key_type;
512
Mike Stump11289f42009-09-09 15:08:12 +0000513 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
Douglas Gregora868bbd2009-04-21 22:25:48 +0000514 : Reader(Reader), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Douglas Gregora868bbd2009-04-21 22:25:48 +0000516 static bool EqualKey(const internal_key_type& a,
517 const internal_key_type& b) {
518 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
519 : false;
520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Douglas Gregora868bbd2009-04-21 22:25:48 +0000522 static unsigned ComputeHash(const internal_key_type& a) {
523 return BernsteinHash(a.first, a.second);
524 }
Mike Stump11289f42009-09-09 15:08:12 +0000525
Douglas Gregora868bbd2009-04-21 22:25:48 +0000526 // This hopefully will just get inlined and removed by the optimizer.
527 static const internal_key_type&
528 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000529
Douglas Gregora868bbd2009-04-21 22:25:48 +0000530 static std::pair<unsigned, unsigned>
531 ReadKeyDataLength(const unsigned char*& d) {
532 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000533 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000534 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000535 return std::make_pair(KeyLen, DataLen);
536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Douglas Gregora868bbd2009-04-21 22:25:48 +0000538 static std::pair<const char*, unsigned>
539 ReadKey(const unsigned char* d, unsigned n) {
540 assert(n >= 2 && d[n-1] == '\0');
541 return std::make_pair((const char*) d, n-1);
542 }
Mike Stump11289f42009-09-09 15:08:12 +0000543
544 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000545 const unsigned char* d,
546 unsigned DataLen) {
547 using namespace clang::io;
Douglas Gregor1d583f22009-04-28 21:18:29 +0000548 pch::IdentID ID = ReadUnalignedLE32(d);
549 bool IsInteresting = ID & 0x01;
550
551 // Wipe out the "is interesting" bit.
552 ID = ID >> 1;
553
554 if (!IsInteresting) {
555 // For unintersting identifiers, just build the IdentifierInfo
556 // and associate it with the persistent ID.
557 IdentifierInfo *II = KnownII;
558 if (!II)
559 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
560 k.first, k.first + k.second);
561 Reader.SetIdentifierInfo(ID, II);
562 return II;
563 }
564
Douglas Gregorb9256522009-04-28 21:32:13 +0000565 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000566 bool CPlusPlusOperatorKeyword = Bits & 0x01;
567 Bits >>= 1;
568 bool Poisoned = Bits & 0x01;
569 Bits >>= 1;
570 bool ExtensionToken = Bits & 0x01;
571 Bits >>= 1;
572 bool hasMacroDefinition = Bits & 0x01;
573 Bits >>= 1;
574 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
575 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000576
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000577 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000578 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000579
580 // Build the IdentifierInfo itself and link the identifier ID with
581 // the new IdentifierInfo.
582 IdentifierInfo *II = KnownII;
583 if (!II)
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000584 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
585 k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000586 Reader.SetIdentifierInfo(ID, II);
587
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000588 // Set or check the various bits in the IdentifierInfo structure.
589 // FIXME: Load token IDs lazily, too?
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000590 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000591 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000592 "Incorrect extension token flag");
593 (void)ExtensionToken;
594 II->setIsPoisoned(Poisoned);
595 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
596 "Incorrect C++ operator keyword flag");
597 (void)CPlusPlusOperatorKeyword;
598
Douglas Gregorc3366a52009-04-21 23:56:24 +0000599 // If this identifier is a macro, deserialize the macro
600 // definition.
601 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000602 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregorc3366a52009-04-21 23:56:24 +0000603 Reader.ReadMacroRecord(Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000604 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000605 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000606
607 // Read all of the declarations visible at global scope with this
608 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000609 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000610 if (DataLen > 0) {
611 llvm::SmallVector<uint32_t, 4> DeclIDs;
612 for (; DataLen > 0; DataLen -= 4)
613 DeclIDs.push_back(ReadUnalignedLE32(d));
614 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Douglas Gregora868bbd2009-04-21 22:25:48 +0000617 return II;
618 }
619};
Mike Stump11289f42009-09-09 15:08:12 +0000620
621} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000622
623/// \brief The on-disk hash table used to contain information about
624/// all of the identifiers in the program.
Mike Stump11289f42009-09-09 15:08:12 +0000625typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
Douglas Gregora868bbd2009-04-21 22:25:48 +0000626 PCHIdentifierLookupTable;
627
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000628bool PCHReader::Error(const char *Msg) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000629 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Fatal, Msg);
630 Diag(DiagID);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000631 return true;
632}
633
Douglas Gregor92863e42009-04-10 23:10:45 +0000634/// \brief Check the contents of the predefines buffer against the
635/// contents of the predefines buffer used to build the PCH file.
636///
637/// The contents of the two predefines buffers should be the same. If
638/// not, then some command-line option changed the preprocessor state
639/// and we must reject the PCH file.
640///
641/// \param PCHPredef The start of the predefines buffer in the PCH
642/// file.
643///
644/// \param PCHPredefLen The length of the predefines buffer in the PCH
645/// file.
646///
647/// \param PCHBufferID The FileID for the PCH predefines buffer.
648///
649/// \returns true if there was a mismatch (in which case the PCH file
650/// should be ignored), or false otherwise.
Mike Stump11289f42009-09-09 15:08:12 +0000651bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
Douglas Gregor92863e42009-04-10 23:10:45 +0000652 unsigned PCHPredefLen,
653 FileID PCHBufferID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000654 if (Listener)
655 return Listener->ReadPredefinesBuffer(PCHPredef, PCHPredefLen, PCHBufferID,
656 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000657 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000658}
659
Douglas Gregorc5046832009-04-27 18:38:38 +0000660//===----------------------------------------------------------------------===//
661// Source Manager Deserialization
662//===----------------------------------------------------------------------===//
663
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000664/// \brief Read the line table in the source manager block.
665/// \returns true if ther was an error.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000666bool PCHReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000667 unsigned Idx = 0;
668 LineTableInfo &LineTable = SourceMgr.getLineTable();
669
670 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000671 std::map<int, int> FileIDs;
672 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000673 // Extract the file name
674 unsigned FilenameLen = Record[Idx++];
675 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
676 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000677 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000678 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000679 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000680 }
681
682 // Parse the line entries
683 std::vector<LineEntry> Entries;
684 while (Idx < Record.size()) {
Douglas Gregora8854652009-04-13 17:12:42 +0000685 int FID = FileIDs[Record[Idx++]];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000686
687 // Extract the line entries
688 unsigned NumEntries = Record[Idx++];
689 Entries.clear();
690 Entries.reserve(NumEntries);
691 for (unsigned I = 0; I != NumEntries; ++I) {
692 unsigned FileOffset = Record[Idx++];
693 unsigned LineNo = Record[Idx++];
694 int FilenameID = Record[Idx++];
Mike Stump11289f42009-09-09 15:08:12 +0000695 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000696 = (SrcMgr::CharacteristicKind)Record[Idx++];
697 unsigned IncludeOffset = Record[Idx++];
698 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
699 FileKind, IncludeOffset));
700 }
701 LineTable.AddEntry(FID, Entries);
702 }
703
704 return false;
705}
706
Douglas Gregorc5046832009-04-27 18:38:38 +0000707namespace {
708
709class VISIBILITY_HIDDEN PCHStatData {
710public:
711 const bool hasStat;
712 const ino_t ino;
713 const dev_t dev;
714 const mode_t mode;
715 const time_t mtime;
716 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000717
Douglas Gregorc5046832009-04-27 18:38:38 +0000718 PCHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000719 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
720
Douglas Gregorc5046832009-04-27 18:38:38 +0000721 PCHStatData()
722 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
723};
724
725class VISIBILITY_HIDDEN PCHStatLookupTrait {
726 public:
727 typedef const char *external_key_type;
728 typedef const char *internal_key_type;
729
730 typedef PCHStatData data_type;
731
732 static unsigned ComputeHash(const char *path) {
733 return BernsteinHash(path);
734 }
735
736 static internal_key_type GetInternalKey(const char *path) { return path; }
737
738 static bool EqualKey(internal_key_type a, internal_key_type b) {
739 return strcmp(a, b) == 0;
740 }
741
742 static std::pair<unsigned, unsigned>
743 ReadKeyDataLength(const unsigned char*& d) {
744 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
745 unsigned DataLen = (unsigned) *d++;
746 return std::make_pair(KeyLen + 1, DataLen);
747 }
748
749 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
750 return (const char *)d;
751 }
752
753 static data_type ReadData(const internal_key_type, const unsigned char *d,
754 unsigned /*DataLen*/) {
755 using namespace clang::io;
756
757 if (*d++ == 1)
758 return data_type();
759
760 ino_t ino = (ino_t) ReadUnalignedLE32(d);
761 dev_t dev = (dev_t) ReadUnalignedLE32(d);
762 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000763 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +0000764 off_t size = (off_t) ReadUnalignedLE64(d);
765 return data_type(ino, dev, mode, mtime, size);
766 }
767};
768
769/// \brief stat() cache for precompiled headers.
770///
771/// This cache is very similar to the stat cache used by pretokenized
772/// headers.
773class VISIBILITY_HIDDEN PCHStatCache : public StatSysCallCache {
774 typedef OnDiskChainedHashTable<PCHStatLookupTrait> CacheTy;
775 CacheTy *Cache;
776
777 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000778public:
Douglas Gregorc5046832009-04-27 18:38:38 +0000779 PCHStatCache(const unsigned char *Buckets,
780 const unsigned char *Base,
781 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +0000782 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +0000783 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
784 Cache = CacheTy::Create(Buckets, Base);
785 }
786
787 ~PCHStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +0000788
Douglas Gregorc5046832009-04-27 18:38:38 +0000789 int stat(const char *path, struct stat *buf) {
790 // Do the lookup for the file's data in the PCH file.
791 CacheTy::iterator I = Cache->find(path);
792
793 // If we don't get a hit in the PCH file just forward to 'stat'.
794 if (I == Cache->end()) {
795 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000796 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +0000797 }
Mike Stump11289f42009-09-09 15:08:12 +0000798
Douglas Gregorc5046832009-04-27 18:38:38 +0000799 ++NumStatHits;
800 PCHStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000801
Douglas Gregorc5046832009-04-27 18:38:38 +0000802 if (!Data.hasStat)
803 return 1;
804
805 buf->st_ino = Data.ino;
806 buf->st_dev = Data.dev;
807 buf->st_mtime = Data.mtime;
808 buf->st_mode = Data.mode;
809 buf->st_size = Data.size;
810 return 0;
811 }
812};
813} // end anonymous namespace
814
815
Douglas Gregora7f71a92009-04-10 03:52:48 +0000816/// \brief Read the source manager block
Douglas Gregor92863e42009-04-10 23:10:45 +0000817PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000818 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +0000819
820 // Set the source-location entry cursor to the current position in
821 // the stream. This cursor will be used to read the contents of the
822 // source manager block initially, and then lazily read
823 // source-location entries as needed.
824 SLocEntryCursor = Stream;
825
826 // The stream itself is going to skip over the source manager block.
827 if (Stream.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000828 Error("malformed block record in PCH file");
Douglas Gregor258ae542009-04-27 06:38:32 +0000829 return Failure;
830 }
831
832 // Enter the source manager block.
833 if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000834 Error("malformed source manager block record in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +0000835 return Failure;
836 }
Douglas Gregora7f71a92009-04-10 03:52:48 +0000837
Douglas Gregora7f71a92009-04-10 03:52:48 +0000838 RecordData Record;
839 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000840 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +0000841 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000842 if (SLocEntryCursor.ReadBlockEnd()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000843 Error("error at end of Source Manager block in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +0000844 return Failure;
845 }
Douglas Gregor92863e42009-04-10 23:10:45 +0000846 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +0000847 }
Mike Stump11289f42009-09-09 15:08:12 +0000848
Douglas Gregora7f71a92009-04-10 03:52:48 +0000849 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
850 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +0000851 SLocEntryCursor.ReadSubBlockID();
852 if (SLocEntryCursor.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000853 Error("malformed block record in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +0000854 return Failure;
855 }
Douglas Gregora7f71a92009-04-10 03:52:48 +0000856 continue;
857 }
Mike Stump11289f42009-09-09 15:08:12 +0000858
Douglas Gregora7f71a92009-04-10 03:52:48 +0000859 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000860 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +0000861 continue;
862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Douglas Gregora7f71a92009-04-10 03:52:48 +0000864 // Read a record.
865 const char *BlobStart;
866 unsigned BlobLen;
867 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +0000868 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000869 default: // Default behavior: ignore.
870 break;
871
Chris Lattner184e65d2009-04-14 23:22:57 +0000872 case pch::SM_LINE_TABLE:
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000873 if (ParseLineTable(Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000874 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +0000875 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +0000876
877 case pch::SM_HEADER_FILE_INFO: {
878 HeaderFileInfo HFI;
879 HFI.isImport = Record[0];
880 HFI.DirInfo = Record[1];
881 HFI.NumIncludes = Record[2];
882 HFI.ControllingMacroID = Record[3];
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000883 if (Listener)
884 Listener->ReadHeaderFileInfo(HFI);
Douglas Gregoreda6a892009-04-26 00:07:37 +0000885 break;
886 }
Douglas Gregor258ae542009-04-27 06:38:32 +0000887
888 case pch::SM_SLOC_FILE_ENTRY:
889 case pch::SM_SLOC_BUFFER_ENTRY:
890 case pch::SM_SLOC_INSTANTIATION_ENTRY:
891 // Once we hit one of the source location entries, we're done.
892 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +0000893 }
894 }
895}
896
Douglas Gregor258ae542009-04-27 06:38:32 +0000897/// \brief Read in the source location entry with the given ID.
898PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) {
899 if (ID == 0)
900 return Success;
901
902 if (ID > TotalNumSLocEntries) {
903 Error("source location entry ID out-of-range for PCH file");
904 return Failure;
905 }
906
907 ++NumSLocEntriesRead;
908 SLocEntryCursor.JumpToBit(SLocOffsets[ID - 1]);
909 unsigned Code = SLocEntryCursor.ReadCode();
910 if (Code == llvm::bitc::END_BLOCK ||
911 Code == llvm::bitc::ENTER_SUBBLOCK ||
912 Code == llvm::bitc::DEFINE_ABBREV) {
913 Error("incorrectly-formatted source location entry in PCH file");
914 return Failure;
915 }
916
Douglas Gregor258ae542009-04-27 06:38:32 +0000917 RecordData Record;
918 const char *BlobStart;
919 unsigned BlobLen;
920 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
921 default:
922 Error("incorrectly-formatted source location entry in PCH file");
923 return Failure;
924
925 case pch::SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000926 std::string Filename(BlobStart, BlobStart + BlobLen);
927 MaybeAddSystemRootToFilename(Filename);
928 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +0000929 if (File == 0) {
930 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000931 ErrorStr += Filename;
Chris Lattnerd20dc872009-06-15 04:35:16 +0000932 ErrorStr += "' referenced by PCH file";
933 Error(ErrorStr.c_str());
934 return Failure;
935 }
Mike Stump11289f42009-09-09 15:08:12 +0000936
Douglas Gregor258ae542009-04-27 06:38:32 +0000937 FileID FID = SourceMgr.createFileID(File,
938 SourceLocation::getFromRawEncoding(Record[1]),
939 (SrcMgr::CharacteristicKind)Record[2],
940 ID, Record[0]);
941 if (Record[3])
942 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
943 .setHasLineDirectives();
944
945 break;
946 }
947
948 case pch::SM_SLOC_BUFFER_ENTRY: {
949 const char *Name = BlobStart;
950 unsigned Offset = Record[0];
951 unsigned Code = SLocEntryCursor.ReadCode();
952 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000953 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +0000954 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
955 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
956 (void)RecCode;
957 llvm::MemoryBuffer *Buffer
Mike Stump11289f42009-09-09 15:08:12 +0000958 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
Douglas Gregor258ae542009-04-27 06:38:32 +0000959 BlobStart + BlobLen - 1,
960 Name);
961 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +0000962
Douglas Gregore6648fb2009-04-28 20:33:11 +0000963 if (strcmp(Name, "<built-in>") == 0) {
964 PCHPredefinesBufferID = BufferID;
965 PCHPredefines = BlobStart;
966 PCHPredefinesLen = BlobLen - 1;
967 }
Douglas Gregor258ae542009-04-27 06:38:32 +0000968
969 break;
970 }
971
972 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump11289f42009-09-09 15:08:12 +0000973 SourceLocation SpellingLoc
Douglas Gregor258ae542009-04-27 06:38:32 +0000974 = SourceLocation::getFromRawEncoding(Record[1]);
975 SourceMgr.createInstantiationLoc(SpellingLoc,
976 SourceLocation::getFromRawEncoding(Record[2]),
977 SourceLocation::getFromRawEncoding(Record[3]),
978 Record[4],
979 ID,
980 Record[0]);
981 break;
Mike Stump11289f42009-09-09 15:08:12 +0000982 }
Douglas Gregor258ae542009-04-27 06:38:32 +0000983 }
984
985 return Success;
986}
987
Chris Lattnere78a6be2009-04-27 01:05:14 +0000988/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
989/// specified cursor. Read the abbreviations that are at the top of the block
990/// and then leave the cursor pointing into the block.
991bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
992 unsigned BlockID) {
993 if (Cursor.EnterSubBlock(BlockID)) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +0000994 Error("malformed block record in PCH file");
Chris Lattnere78a6be2009-04-27 01:05:14 +0000995 return Failure;
996 }
Mike Stump11289f42009-09-09 15:08:12 +0000997
Chris Lattnere78a6be2009-04-27 01:05:14 +0000998 while (true) {
999 unsigned Code = Cursor.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001000
Chris Lattnere78a6be2009-04-27 01:05:14 +00001001 // We expect all abbrevs to be at the start of the block.
1002 if (Code != llvm::bitc::DEFINE_ABBREV)
1003 return false;
1004 Cursor.ReadAbbrevRecord();
1005 }
1006}
1007
Douglas Gregorc3366a52009-04-21 23:56:24 +00001008void PCHReader::ReadMacroRecord(uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001009 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump11289f42009-09-09 15:08:12 +00001010
Douglas Gregorc3366a52009-04-21 23:56:24 +00001011 // Keep track of where we are in the stream, then jump back there
1012 // after reading this macro.
1013 SavedStreamPosition SavedPosition(Stream);
1014
1015 Stream.JumpToBit(Offset);
1016 RecordData Record;
1017 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1018 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001019
Douglas Gregorc3366a52009-04-21 23:56:24 +00001020 while (true) {
1021 unsigned Code = Stream.ReadCode();
1022 switch (Code) {
1023 case llvm::bitc::END_BLOCK:
1024 return;
1025
1026 case llvm::bitc::ENTER_SUBBLOCK:
1027 // No known subblocks, always skip them.
1028 Stream.ReadSubBlockID();
1029 if (Stream.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001030 Error("malformed block record in PCH file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001031 return;
1032 }
1033 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001034
Douglas Gregorc3366a52009-04-21 23:56:24 +00001035 case llvm::bitc::DEFINE_ABBREV:
1036 Stream.ReadAbbrevRecord();
1037 continue;
1038 default: break;
1039 }
1040
1041 // Read a record.
1042 Record.clear();
1043 pch::PreprocessorRecordTypes RecType =
1044 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1045 switch (RecType) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001046 case pch::PP_MACRO_OBJECT_LIKE:
1047 case pch::PP_MACRO_FUNCTION_LIKE: {
1048 // If we already have a macro, that means that we've hit the end
1049 // of the definition of the macro we were looking for. We're
1050 // done.
1051 if (Macro)
1052 return;
1053
1054 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1055 if (II == 0) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001056 Error("macro must have a name in PCH file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001057 return;
1058 }
1059 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1060 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001061
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001062 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001063 MI->setIsUsed(isUsed);
Mike Stump11289f42009-09-09 15:08:12 +00001064
Douglas Gregorc3366a52009-04-21 23:56:24 +00001065 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1066 // Decode function-like macro info.
1067 bool isC99VarArgs = Record[3];
1068 bool isGNUVarArgs = Record[4];
1069 MacroArgs.clear();
1070 unsigned NumArgs = Record[5];
1071 for (unsigned i = 0; i != NumArgs; ++i)
1072 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1073
1074 // Install function-like macro info.
1075 MI->setIsFunctionLike();
1076 if (isC99VarArgs) MI->setIsC99Varargs();
1077 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001078 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001079 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001080 }
1081
1082 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001083 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001084
1085 // Remember that we saw this macro last so that we add the tokens that
1086 // form its body to it.
1087 Macro = MI;
1088 ++NumMacrosRead;
1089 break;
1090 }
Mike Stump11289f42009-09-09 15:08:12 +00001091
Douglas Gregorc3366a52009-04-21 23:56:24 +00001092 case pch::PP_TOKEN: {
1093 // If we see a TOKEN before a PP_MACRO_*, then the file is
1094 // erroneous, just pretend we didn't see this.
1095 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001096
Douglas Gregorc3366a52009-04-21 23:56:24 +00001097 Token Tok;
1098 Tok.startToken();
1099 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1100 Tok.setLength(Record[1]);
1101 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1102 Tok.setIdentifierInfo(II);
1103 Tok.setKind((tok::TokenKind)Record[3]);
1104 Tok.setFlag((Token::TokenFlags)Record[4]);
1105 Macro->AddTokenToBody(Tok);
1106 break;
1107 }
Steve Naroff3fa455a2009-04-24 20:03:17 +00001108 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001109 }
1110}
1111
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001112/// \brief If we are loading a relocatable PCH file, and the filename is
1113/// not an absolute path, add the system root to the beginning of the file
1114/// name.
1115void PCHReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1116 // If this is not a relocatable PCH file, there's nothing to do.
1117 if (!RelocatablePCH)
1118 return;
Mike Stump11289f42009-09-09 15:08:12 +00001119
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001120 if (Filename.empty() || Filename[0] == '/' || Filename[0] == '<')
1121 return;
1122
1123 std::string FIXME = Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001124
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001125 if (isysroot == 0) {
1126 // If no system root was given, default to '/'
1127 Filename.insert(Filename.begin(), '/');
1128 return;
1129 }
Mike Stump11289f42009-09-09 15:08:12 +00001130
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001131 unsigned Length = strlen(isysroot);
1132 if (isysroot[Length - 1] != '/')
1133 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001134
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001135 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1136}
1137
Mike Stump11289f42009-09-09 15:08:12 +00001138PCHReader::PCHReadResult
Douglas Gregoreda6a892009-04-26 00:07:37 +00001139PCHReader::ReadPCHBlock() {
Douglas Gregor55abb232009-04-10 20:39:37 +00001140 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001141 Error("malformed block record in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001142 return Failure;
1143 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001144
1145 // Read all of the records and blocks for the PCH file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001146 RecordData Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001147 while (!Stream.AtEndOfStream()) {
1148 unsigned Code = Stream.ReadCode();
1149 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001150 if (Stream.ReadBlockEnd()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001151 Error("error at end of module block in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001152 return Failure;
1153 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001154
Douglas Gregor55abb232009-04-10 20:39:37 +00001155 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001156 }
1157
1158 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1159 switch (Stream.ReadSubBlockID()) {
Douglas Gregor12bfa382009-10-17 00:13:19 +00001160 case pch::DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001161 // We lazily load the decls block, but we want to set up the
1162 // DeclsCursor cursor to point into it. Clone our current bitcode
1163 // cursor to it, enter the block and read the abbrevs in that block.
1164 // With the main cursor, we just skip over it.
1165 DeclsCursor = Stream;
1166 if (Stream.SkipBlock() || // Skip with the main cursor.
1167 // Read the abbrevs.
Douglas Gregor12bfa382009-10-17 00:13:19 +00001168 ReadBlockAbbrevs(DeclsCursor, pch::DECLTYPES_BLOCK_ID)) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001169 Error("malformed block record in PCH file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001170 return Failure;
1171 }
1172 break;
Mike Stump11289f42009-09-09 15:08:12 +00001173
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001174 case pch::PREPROCESSOR_BLOCK_ID:
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001175 if (Stream.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001176 Error("malformed block record in PCH file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001177 return Failure;
1178 }
1179 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001180
Douglas Gregora7f71a92009-04-10 03:52:48 +00001181 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00001182 switch (ReadSourceManagerBlock()) {
1183 case Success:
1184 break;
1185
1186 case Failure:
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001187 Error("malformed source manager block in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001188 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001189
1190 case IgnorePCH:
1191 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001192 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001193 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001194 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001195 continue;
1196 }
1197
1198 if (Code == llvm::bitc::DEFINE_ABBREV) {
1199 Stream.ReadAbbrevRecord();
1200 continue;
1201 }
1202
1203 // Read and process a record.
1204 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001205 const char *BlobStart = 0;
1206 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001207 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregorbfbde532009-04-10 21:16:55 +00001208 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001209 default: // Default behavior: ignore.
1210 break;
1211
1212 case pch::TYPE_OFFSET:
Douglas Gregor745ed142009-04-25 18:35:21 +00001213 if (!TypesLoaded.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001214 Error("duplicate TYPE_OFFSET record in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001215 return Failure;
1216 }
Chris Lattnereeb05692009-04-27 18:24:17 +00001217 TypeOffsets = (const uint32_t *)BlobStart;
Douglas Gregor745ed142009-04-25 18:35:21 +00001218 TypesLoaded.resize(Record[0]);
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001219 break;
1220
1221 case pch::DECL_OFFSET:
Douglas Gregor745ed142009-04-25 18:35:21 +00001222 if (!DeclsLoaded.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001223 Error("duplicate DECL_OFFSET record in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001224 return Failure;
1225 }
Chris Lattnereeb05692009-04-27 18:24:17 +00001226 DeclOffsets = (const uint32_t *)BlobStart;
Douglas Gregor745ed142009-04-25 18:35:21 +00001227 DeclsLoaded.resize(Record[0]);
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001228 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001229
1230 case pch::LANGUAGE_OPTIONS:
1231 if (ParseLanguageOptions(Record))
1232 return IgnorePCH;
1233 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001234
Douglas Gregor7b71e632009-04-27 22:23:34 +00001235 case pch::METADATA: {
1236 if (Record[0] != pch::VERSION_MAJOR) {
1237 Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
1238 : diag::warn_pch_version_too_new);
1239 return IgnorePCH;
1240 }
1241
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001242 RelocatablePCH = Record[4];
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001243 if (Listener) {
1244 std::string TargetTriple(BlobStart, BlobLen);
1245 if (Listener->ReadTargetTriple(TargetTriple))
1246 return IgnorePCH;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001247 }
1248 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001249 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001250
1251 case pch::IDENTIFIER_TABLE:
Douglas Gregora868bbd2009-04-21 22:25:48 +00001252 IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001253 if (Record[0]) {
Mike Stump11289f42009-09-09 15:08:12 +00001254 IdentifierLookupTable
Douglas Gregor0e149972009-04-25 19:10:14 +00001255 = PCHIdentifierLookupTable::Create(
Douglas Gregora868bbd2009-04-21 22:25:48 +00001256 (const unsigned char *)IdentifierTableData + Record[0],
Mike Stump11289f42009-09-09 15:08:12 +00001257 (const unsigned char *)IdentifierTableData,
Douglas Gregora868bbd2009-04-21 22:25:48 +00001258 PCHIdentifierLookupTrait(*this));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001259 if (PP)
1260 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001261 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001262 break;
1263
1264 case pch::IDENTIFIER_OFFSET:
Douglas Gregor0e149972009-04-25 19:10:14 +00001265 if (!IdentifiersLoaded.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001266 Error("duplicate IDENTIFIER_OFFSET record in PCH file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001267 return Failure;
1268 }
Douglas Gregor0e149972009-04-25 19:10:14 +00001269 IdentifierOffsets = (const uint32_t *)BlobStart;
1270 IdentifiersLoaded.resize(Record[0]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001271 if (PP)
1272 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001273 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001274
1275 case pch::EXTERNAL_DEFINITIONS:
1276 if (!ExternalDefinitions.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001277 Error("duplicate EXTERNAL_DEFINITIONS record in PCH file");
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001278 return Failure;
1279 }
1280 ExternalDefinitions.swap(Record);
1281 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001282
Douglas Gregor652d82a2009-04-18 05:55:16 +00001283 case pch::SPECIAL_TYPES:
1284 SpecialTypes.swap(Record);
1285 break;
1286
Douglas Gregor08f01292009-04-17 22:13:46 +00001287 case pch::STATISTICS:
1288 TotalNumStatements = Record[0];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001289 TotalNumMacros = Record[1];
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001290 TotalLexicalDeclContexts = Record[2];
1291 TotalVisibleDeclContexts = Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001292 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001293
Douglas Gregord4df8652009-04-22 22:02:47 +00001294 case pch::TENTATIVE_DEFINITIONS:
1295 if (!TentativeDefinitions.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001296 Error("duplicate TENTATIVE_DEFINITIONS record in PCH file");
Douglas Gregord4df8652009-04-22 22:02:47 +00001297 return Failure;
1298 }
1299 TentativeDefinitions.swap(Record);
1300 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001301
1302 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1303 if (!LocallyScopedExternalDecls.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001304 Error("duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001305 return Failure;
1306 }
1307 LocallyScopedExternalDecls.swap(Record);
1308 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001309
Douglas Gregor95c13f52009-04-25 17:48:32 +00001310 case pch::SELECTOR_OFFSETS:
1311 SelectorOffsets = (const uint32_t *)BlobStart;
1312 TotalNumSelectors = Record[0];
1313 SelectorsLoaded.resize(TotalNumSelectors);
1314 break;
1315
Douglas Gregorc78d3462009-04-24 21:10:55 +00001316 case pch::METHOD_POOL:
Douglas Gregor95c13f52009-04-25 17:48:32 +00001317 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1318 if (Record[0])
Mike Stump11289f42009-09-09 15:08:12 +00001319 MethodPoolLookupTable
Douglas Gregor95c13f52009-04-25 17:48:32 +00001320 = PCHMethodPoolLookupTable::Create(
1321 MethodPoolLookupTableData + Record[0],
Mike Stump11289f42009-09-09 15:08:12 +00001322 MethodPoolLookupTableData,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001323 PCHMethodPoolLookupTrait(*this));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001324 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00001325 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001326
1327 case pch::PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001328 if (!Record.empty() && Listener)
1329 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001330 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001331
1332 case pch::SOURCE_LOCATION_OFFSETS:
Chris Lattner12d61d32009-04-27 19:01:47 +00001333 SLocOffsets = (const uint32_t *)BlobStart;
Douglas Gregor258ae542009-04-27 06:38:32 +00001334 TotalNumSLocEntries = Record[0];
Douglas Gregord54f3a12009-10-05 21:07:28 +00001335 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001336 break;
1337
1338 case pch::SOURCE_LOCATION_PRELOADS:
1339 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
1340 PCHReadResult Result = ReadSLocEntryRecord(Record[I]);
1341 if (Result != Success)
1342 return Result;
1343 }
1344 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00001345
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001346 case pch::STAT_CACHE: {
1347 PCHStatCache *MyStatCache =
1348 new PCHStatCache((const unsigned char *)BlobStart + Record[0],
1349 (const unsigned char *)BlobStart,
1350 NumStatHits, NumStatMisses);
1351 FileMgr.addStatCache(MyStatCache);
1352 StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00001353 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001354 }
1355
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001356 case pch::EXT_VECTOR_DECLS:
1357 if (!ExtVectorDecls.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001358 Error("duplicate EXT_VECTOR_DECLS record in PCH file");
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001359 return Failure;
1360 }
1361 ExtVectorDecls.swap(Record);
1362 break;
1363
Douglas Gregor45fe0362009-05-12 01:31:05 +00001364 case pch::ORIGINAL_FILE_NAME:
1365 OriginalFileName.assign(BlobStart, BlobLen);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001366 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001367 break;
Mike Stump11289f42009-09-09 15:08:12 +00001368
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001369 case pch::COMMENT_RANGES:
1370 Comments = (SourceRange *)BlobStart;
1371 NumComments = BlobLen / sizeof(SourceRange);
1372 break;
Douglas Gregord54f3a12009-10-05 21:07:28 +00001373
1374 case pch::SVN_BRANCH_REVISION: {
1375 unsigned CurRevision = getClangSubversionRevision();
1376 if (Record[0] && CurRevision && Record[0] != CurRevision) {
1377 Diag(Record[0] < CurRevision? diag::warn_pch_version_too_old
1378 : diag::warn_pch_version_too_new);
1379 return IgnorePCH;
1380 }
1381
1382 const char *CurBranch = getClangSubversionPath();
1383 if (strncmp(CurBranch, BlobStart, BlobLen)) {
1384 std::string PCHBranch(BlobStart, BlobLen);
1385 Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch;
1386 return IgnorePCH;
1387 }
1388 break;
1389 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001390 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001391 }
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001392 Error("premature end of bitstream in PCH file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001393 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001394}
1395
Douglas Gregor92863e42009-04-10 23:10:45 +00001396PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001397 // Set the PCH file name.
1398 this->FileName = FileName;
1399
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001400 // Open the PCH file.
Daniel Dunbar2d925eb2009-09-22 05:38:01 +00001401 //
1402 // FIXME: This shouldn't be here, we should just take a raw_ostream.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001403 std::string ErrStr;
Daniel Dunbar2d925eb2009-09-22 05:38:01 +00001404 if (FileName == "-")
1405 Buffer.reset(llvm::MemoryBuffer::getSTDIN());
1406 else
1407 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregor92863e42009-04-10 23:10:45 +00001408 if (!Buffer) {
1409 Error(ErrStr.c_str());
1410 return IgnorePCH;
1411 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001412
1413 // Initialize the stream
Mike Stump11289f42009-09-09 15:08:12 +00001414 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Chris Lattner9356ace2009-04-26 20:59:20 +00001415 (const unsigned char *)Buffer->getBufferEnd());
1416 Stream.init(StreamFile);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001417
1418 // Sniff for the signature.
1419 if (Stream.Read(8) != 'C' ||
1420 Stream.Read(8) != 'P' ||
1421 Stream.Read(8) != 'C' ||
Douglas Gregor92863e42009-04-10 23:10:45 +00001422 Stream.Read(8) != 'H') {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001423 Diag(diag::err_not_a_pch_file) << FileName;
1424 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001425 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001426
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001427 while (!Stream.AtEndOfStream()) {
1428 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001429
Douglas Gregor92863e42009-04-10 23:10:45 +00001430 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001431 Error("invalid record at top-level of PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001432 return Failure;
1433 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001434
1435 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00001436
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001437 // We only know the PCH subblock ID.
1438 switch (BlockID) {
1439 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00001440 if (Stream.ReadBlockInfoBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001441 Error("malformed BlockInfoBlock in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001442 return Failure;
1443 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001444 break;
1445 case pch::PCH_BLOCK_ID:
Douglas Gregoreda6a892009-04-26 00:07:37 +00001446 switch (ReadPCHBlock()) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001447 case Success:
1448 break;
1449
1450 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00001451 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00001452
1453 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00001454 // FIXME: We could consider reading through to the end of this
1455 // PCH block, skipping subblocks, to see if there are other
1456 // PCH blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00001457
1458 // Clear out any preallocated source location entries, so that
1459 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001460 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00001461
1462 // Remove the stat cache.
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001463 if (StatCache)
1464 FileMgr.removeStatCache((PCHStatCache*)StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00001465
Douglas Gregor92863e42009-04-10 23:10:45 +00001466 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001467 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001468 break;
1469 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00001470 if (Stream.SkipBlock()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001471 Error("malformed block record in PCH file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001472 return Failure;
1473 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001474 break;
1475 }
Mike Stump11289f42009-09-09 15:08:12 +00001476 }
1477
Douglas Gregore6648fb2009-04-28 20:33:11 +00001478 // Check the predefines buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001479 if (CheckPredefinesBuffer(PCHPredefines, PCHPredefinesLen,
Douglas Gregore6648fb2009-04-28 20:33:11 +00001480 PCHPredefinesBufferID))
1481 return IgnorePCH;
Mike Stump11289f42009-09-09 15:08:12 +00001482
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001483 if (PP) {
Zhongxing Xu3f51f412009-07-18 09:26:51 +00001484 // Initialization of keywords and pragmas occurs before the
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001485 // PCH file is read, so there may be some identifiers that were
1486 // loaded into the IdentifierTable before we intercepted the
1487 // creation of identifiers. Iterate through the list of known
1488 // identifiers and determine whether we have to establish
1489 // preprocessor definitions or top-level identifier declaration
1490 // chains for those identifiers.
1491 //
1492 // We copy the IdentifierInfo pointers to a small vector first,
1493 // since de-serializing declarations or macro definitions can add
1494 // new entries into the identifier table, invalidating the
1495 // iterators.
1496 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1497 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1498 IdEnd = PP->getIdentifierTable().end();
1499 Id != IdEnd; ++Id)
1500 Identifiers.push_back(Id->second);
Mike Stump11289f42009-09-09 15:08:12 +00001501 PCHIdentifierLookupTable *IdTable
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001502 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1503 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1504 IdentifierInfo *II = Identifiers[I];
1505 // Look in the on-disk hash table for an entry for
1506 PCHIdentifierLookupTrait Info(*this, II);
1507 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1508 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1509 if (Pos == IdTable->end())
1510 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001511
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001512 // Dereferencing the iterator has the effect of populating the
1513 // IdentifierInfo node with the various declarations it needs.
1514 (void)*Pos;
1515 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001516 }
1517
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001518 if (Context)
1519 InitializeContext(*Context);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001520
Douglas Gregora868bbd2009-04-21 22:25:48 +00001521 return Success;
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001522}
1523
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001524void PCHReader::InitializeContext(ASTContext &Ctx) {
1525 Context = &Ctx;
1526 assert(Context && "Passed null context!");
1527
1528 assert(PP && "Forgot to set Preprocessor ?");
1529 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1530 PP->getHeaderSearchInfo().SetExternalLookup(this);
Mike Stump11289f42009-09-09 15:08:12 +00001531
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001532 // Load the translation unit declaration
1533 ReadDeclRecord(DeclOffsets[0], 0);
1534
1535 // Load the special types.
1536 Context->setBuiltinVaListType(
1537 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1538 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
1539 Context->setObjCIdType(GetType(Id));
1540 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
1541 Context->setObjCSelType(GetType(Sel));
1542 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
1543 Context->setObjCProtoType(GetType(Proto));
1544 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
1545 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00001546
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001547 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
1548 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00001549 if (unsigned FastEnum
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001550 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
1551 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregor27821ce2009-07-07 16:35:42 +00001552 if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) {
1553 QualType FileType = GetType(File);
1554 assert(!FileType.isNull() && "FILE type is NULL");
John McCall9dd450b2009-09-21 23:43:11 +00001555 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00001556 Context->setFILEDecl(Typedef->getDecl());
1557 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001558 const TagType *Tag = FileType->getAs<TagType>();
Douglas Gregor27821ce2009-07-07 16:35:42 +00001559 assert(Tag && "Invalid FILE type in PCH file");
1560 Context->setFILEDecl(Tag->getDecl());
1561 }
1562 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00001563 if (unsigned Jmp_buf = SpecialTypes[pch::SPECIAL_TYPE_jmp_buf]) {
1564 QualType Jmp_bufType = GetType(Jmp_buf);
1565 assert(!Jmp_bufType.isNull() && "jmp_bug type is NULL");
John McCall9dd450b2009-09-21 23:43:11 +00001566 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00001567 Context->setjmp_bufDecl(Typedef->getDecl());
1568 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001569 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Mike Stumpa4de80b2009-07-28 02:25:19 +00001570 assert(Tag && "Invalid jmp_bug type in PCH file");
1571 Context->setjmp_bufDecl(Tag->getDecl());
1572 }
1573 }
1574 if (unsigned Sigjmp_buf = SpecialTypes[pch::SPECIAL_TYPE_sigjmp_buf]) {
1575 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
1576 assert(!Sigjmp_bufType.isNull() && "sigjmp_buf type is NULL");
John McCall9dd450b2009-09-21 23:43:11 +00001577 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00001578 Context->setsigjmp_bufDecl(Typedef->getDecl());
1579 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001580 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Mike Stumpa4de80b2009-07-28 02:25:19 +00001581 assert(Tag && "Invalid sigjmp_buf type in PCH file");
1582 Context->setsigjmp_bufDecl(Tag->getDecl());
1583 }
1584 }
Mike Stump11289f42009-09-09 15:08:12 +00001585 if (unsigned ObjCIdRedef
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001586 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID_REDEFINITION])
1587 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00001588 if (unsigned ObjCClassRedef
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001589 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
1590 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001591}
1592
Douglas Gregor45fe0362009-05-12 01:31:05 +00001593/// \brief Retrieve the name of the original source file name
1594/// directly from the PCH file, without actually loading the PCH
1595/// file.
1596std::string PCHReader::getOriginalSourceFile(const std::string &PCHFileName) {
1597 // Open the PCH file.
1598 std::string ErrStr;
1599 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
1600 Buffer.reset(llvm::MemoryBuffer::getFile(PCHFileName.c_str(), &ErrStr));
1601 if (!Buffer) {
1602 fprintf(stderr, "error: %s\n", ErrStr.c_str());
1603 return std::string();
1604 }
1605
1606 // Initialize the stream
1607 llvm::BitstreamReader StreamFile;
1608 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00001609 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00001610 (const unsigned char *)Buffer->getBufferEnd());
1611 Stream.init(StreamFile);
1612
1613 // Sniff for the signature.
1614 if (Stream.Read(8) != 'C' ||
1615 Stream.Read(8) != 'P' ||
1616 Stream.Read(8) != 'C' ||
1617 Stream.Read(8) != 'H') {
Mike Stump11289f42009-09-09 15:08:12 +00001618 fprintf(stderr,
Douglas Gregor45fe0362009-05-12 01:31:05 +00001619 "error: '%s' does not appear to be a precompiled header file\n",
1620 PCHFileName.c_str());
1621 return std::string();
1622 }
1623
1624 RecordData Record;
1625 while (!Stream.AtEndOfStream()) {
1626 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001627
Douglas Gregor45fe0362009-05-12 01:31:05 +00001628 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1629 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00001630
Douglas Gregor45fe0362009-05-12 01:31:05 +00001631 // We only know the PCH subblock ID.
1632 switch (BlockID) {
1633 case pch::PCH_BLOCK_ID:
1634 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1635 fprintf(stderr, "error: malformed block record in PCH file\n");
1636 return std::string();
1637 }
1638 break;
Mike Stump11289f42009-09-09 15:08:12 +00001639
Douglas Gregor45fe0362009-05-12 01:31:05 +00001640 default:
1641 if (Stream.SkipBlock()) {
1642 fprintf(stderr, "error: malformed block record in PCH file\n");
1643 return std::string();
1644 }
1645 break;
1646 }
1647 continue;
1648 }
1649
1650 if (Code == llvm::bitc::END_BLOCK) {
1651 if (Stream.ReadBlockEnd()) {
1652 fprintf(stderr, "error: error at end of module block in PCH file\n");
1653 return std::string();
1654 }
1655 continue;
1656 }
1657
1658 if (Code == llvm::bitc::DEFINE_ABBREV) {
1659 Stream.ReadAbbrevRecord();
1660 continue;
1661 }
1662
1663 Record.clear();
1664 const char *BlobStart = 0;
1665 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001666 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Douglas Gregor45fe0362009-05-12 01:31:05 +00001667 == pch::ORIGINAL_FILE_NAME)
1668 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00001669 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00001670
1671 return std::string();
1672}
1673
Douglas Gregor55abb232009-04-10 20:39:37 +00001674/// \brief Parse the record that corresponds to a LangOptions data
1675/// structure.
1676///
1677/// This routine compares the language options used to generate the
1678/// PCH file against the language options set for the current
1679/// compilation. For each option, we classify differences between the
1680/// two compiler states as either "benign" or "important". Benign
1681/// differences don't matter, and we accept them without complaint
1682/// (and without modifying the language options). Differences between
1683/// the states for important options cause the PCH file to be
1684/// unusable, so we emit a warning and return true to indicate that
1685/// there was an error.
1686///
1687/// \returns true if the PCH file is unacceptable, false otherwise.
1688bool PCHReader::ParseLanguageOptions(
1689 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001690 if (Listener) {
1691 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00001692
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001693 #define PARSE_LANGOPT(Option) \
1694 LangOpts.Option = Record[Idx]; \
1695 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00001696
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001697 unsigned Idx = 0;
1698 PARSE_LANGOPT(Trigraphs);
1699 PARSE_LANGOPT(BCPLComment);
1700 PARSE_LANGOPT(DollarIdents);
1701 PARSE_LANGOPT(AsmPreprocessor);
1702 PARSE_LANGOPT(GNUMode);
1703 PARSE_LANGOPT(ImplicitInt);
1704 PARSE_LANGOPT(Digraphs);
1705 PARSE_LANGOPT(HexFloats);
1706 PARSE_LANGOPT(C99);
1707 PARSE_LANGOPT(Microsoft);
1708 PARSE_LANGOPT(CPlusPlus);
1709 PARSE_LANGOPT(CPlusPlus0x);
1710 PARSE_LANGOPT(CXXOperatorNames);
1711 PARSE_LANGOPT(ObjC1);
1712 PARSE_LANGOPT(ObjC2);
1713 PARSE_LANGOPT(ObjCNonFragileABI);
1714 PARSE_LANGOPT(PascalStrings);
1715 PARSE_LANGOPT(WritableStrings);
1716 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001717 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001718 PARSE_LANGOPT(Exceptions);
1719 PARSE_LANGOPT(NeXTRuntime);
1720 PARSE_LANGOPT(Freestanding);
1721 PARSE_LANGOPT(NoBuiltin);
1722 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001723 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001724 PARSE_LANGOPT(Blocks);
1725 PARSE_LANGOPT(EmitAllDecls);
1726 PARSE_LANGOPT(MathErrno);
1727 PARSE_LANGOPT(OverflowChecking);
1728 PARSE_LANGOPT(HeinousExtensions);
1729 PARSE_LANGOPT(Optimize);
1730 PARSE_LANGOPT(OptimizeSize);
1731 PARSE_LANGOPT(Static);
1732 PARSE_LANGOPT(PICLevel);
1733 PARSE_LANGOPT(GNUInline);
1734 PARSE_LANGOPT(NoInline);
1735 PARSE_LANGOPT(AccessControl);
1736 PARSE_LANGOPT(CharIsSigned);
1737 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx]);
1738 ++Idx;
1739 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx]);
1740 ++Idx;
Daniel Dunbar143021e2009-09-21 04:16:19 +00001741 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
1742 Record[Idx]);
1743 ++Idx;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001744 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001745 PARSE_LANGOPT(OpenCL);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001746 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00001747
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001748 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00001749 }
Douglas Gregor55abb232009-04-10 20:39:37 +00001750
1751 return false;
1752}
1753
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001754void PCHReader::ReadComments(std::vector<SourceRange> &Comments) {
1755 Comments.resize(NumComments);
1756 std::copy(this->Comments, this->Comments + NumComments,
1757 Comments.begin());
1758}
1759
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001760/// \brief Read and return the type at the given offset.
1761///
1762/// This routine actually reads the record corresponding to the type
1763/// at the given offset in the bitstream. It is a helper routine for
1764/// GetType, which deals with reading type IDs.
1765QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001766 // Keep track of where we are in the stream, then jump back there
1767 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00001768 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001769
Douglas Gregor1342e842009-07-06 18:54:52 +00001770 // Note that we are loading a type record.
1771 LoadingTypeOrDecl Loading(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001772
Douglas Gregor12bfa382009-10-17 00:13:19 +00001773 DeclsCursor.JumpToBit(Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001774 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001775 unsigned Code = DeclsCursor.ReadCode();
1776 switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
Douglas Gregor455b8f42009-04-15 22:00:08 +00001777 case pch::TYPE_EXT_QUAL: {
John McCall8ccfcb52009-09-24 19:53:00 +00001778 assert(Record.size() == 2 &&
Douglas Gregor455b8f42009-04-15 22:00:08 +00001779 "Incorrect encoding of extended qualifier type");
1780 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00001781 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
1782 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00001783 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001784
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001785 case pch::TYPE_FIXED_WIDTH_INT: {
1786 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
Chris Lattner8575daa2009-04-27 21:45:14 +00001787 return Context->getFixedWidthIntType(Record[0], Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001788 }
1789
1790 case pch::TYPE_COMPLEX: {
1791 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1792 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001793 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001794 }
1795
1796 case pch::TYPE_POINTER: {
1797 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1798 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001799 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001800 }
1801
1802 case pch::TYPE_BLOCK_POINTER: {
1803 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1804 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001805 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001806 }
1807
1808 case pch::TYPE_LVALUE_REFERENCE: {
1809 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1810 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001811 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001812 }
1813
1814 case pch::TYPE_RVALUE_REFERENCE: {
1815 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1816 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001817 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001818 }
1819
1820 case pch::TYPE_MEMBER_POINTER: {
1821 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1822 QualType PointeeType = GetType(Record[0]);
1823 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001824 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001825 }
1826
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001827 case pch::TYPE_CONSTANT_ARRAY: {
1828 QualType ElementType = GetType(Record[0]);
1829 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1830 unsigned IndexTypeQuals = Record[2];
1831 unsigned Idx = 3;
1832 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00001833 return Context->getConstantArrayType(ElementType, Size,
1834 ASM, IndexTypeQuals);
1835 }
1836
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001837 case pch::TYPE_INCOMPLETE_ARRAY: {
1838 QualType ElementType = GetType(Record[0]);
1839 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1840 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00001841 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001842 }
1843
1844 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001845 QualType ElementType = GetType(Record[0]);
1846 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1847 unsigned IndexTypeQuals = Record[2];
Douglas Gregor04318252009-07-06 15:59:29 +00001848 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
1849 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001850 return Context->getVariableArrayType(ElementType, ReadTypeExpr(),
Douglas Gregor04318252009-07-06 15:59:29 +00001851 ASM, IndexTypeQuals,
1852 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001853 }
1854
1855 case pch::TYPE_VECTOR: {
1856 if (Record.size() != 2) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001857 Error("incorrect encoding of vector type in PCH file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001858 return QualType();
1859 }
1860
1861 QualType ElementType = GetType(Record[0]);
1862 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00001863 return Context->getVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001864 }
1865
1866 case pch::TYPE_EXT_VECTOR: {
1867 if (Record.size() != 2) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001868 Error("incorrect encoding of extended vector type in PCH file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001869 return QualType();
1870 }
1871
1872 QualType ElementType = GetType(Record[0]);
1873 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00001874 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001875 }
1876
1877 case pch::TYPE_FUNCTION_NO_PROTO: {
1878 if (Record.size() != 1) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001879 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001880 return QualType();
1881 }
1882 QualType ResultType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001883 return Context->getFunctionNoProtoType(ResultType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001884 }
1885
1886 case pch::TYPE_FUNCTION_PROTO: {
1887 QualType ResultType = GetType(Record[0]);
1888 unsigned Idx = 1;
1889 unsigned NumParams = Record[Idx++];
1890 llvm::SmallVector<QualType, 16> ParamTypes;
1891 for (unsigned I = 0; I != NumParams; ++I)
1892 ParamTypes.push_back(GetType(Record[Idx++]));
1893 bool isVariadic = Record[Idx++];
1894 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001895 bool hasExceptionSpec = Record[Idx++];
1896 bool hasAnyExceptionSpec = Record[Idx++];
1897 unsigned NumExceptions = Record[Idx++];
1898 llvm::SmallVector<QualType, 2> Exceptions;
1899 for (unsigned I = 0; I != NumExceptions; ++I)
1900 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00001901 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001902 isVariadic, Quals, hasExceptionSpec,
1903 hasAnyExceptionSpec, NumExceptions,
1904 Exceptions.data());
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001905 }
1906
1907 case pch::TYPE_TYPEDEF:
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001908 assert(Record.size() == 1 && "incorrect encoding of typedef type");
Chris Lattner8575daa2009-04-27 21:45:14 +00001909 return Context->getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001910
1911 case pch::TYPE_TYPEOF_EXPR:
Chris Lattner8575daa2009-04-27 21:45:14 +00001912 return Context->getTypeOfExprType(ReadTypeExpr());
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001913
1914 case pch::TYPE_TYPEOF: {
1915 if (Record.size() != 1) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001916 Error("incorrect encoding of typeof(type) in PCH file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001917 return QualType();
1918 }
1919 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00001920 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001921 }
Mike Stump11289f42009-09-09 15:08:12 +00001922
Anders Carlsson81df7b82009-06-24 19:06:50 +00001923 case pch::TYPE_DECLTYPE:
1924 return Context->getDecltypeType(ReadTypeExpr());
1925
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001926 case pch::TYPE_RECORD:
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001927 assert(Record.size() == 1 && "incorrect encoding of record type");
Chris Lattner8575daa2009-04-27 21:45:14 +00001928 return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001929
Douglas Gregor1daeb692009-04-13 18:14:40 +00001930 case pch::TYPE_ENUM:
Douglas Gregor6f00bf82009-04-28 21:53:25 +00001931 assert(Record.size() == 1 && "incorrect encoding of enum type");
Chris Lattner8575daa2009-04-27 21:45:14 +00001932 return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
Douglas Gregor1daeb692009-04-13 18:14:40 +00001933
John McCallfcc33b02009-09-05 00:15:47 +00001934 case pch::TYPE_ELABORATED: {
1935 assert(Record.size() == 2 && "incorrect encoding of elaborated type");
1936 unsigned Tag = Record[1];
1937 return Context->getElaboratedType(GetType(Record[0]),
1938 (ElaboratedType::TagKind) Tag);
1939 }
1940
Steve Naroffc277ad12009-07-18 15:33:26 +00001941 case pch::TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00001942 unsigned Idx = 0;
1943 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
1944 unsigned NumProtos = Record[Idx++];
1945 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
1946 for (unsigned I = 0; I != NumProtos; ++I)
1947 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Steve Naroffc277ad12009-07-18 15:33:26 +00001948 return Context->getObjCInterfaceType(ItfD, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00001949 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00001950
Steve Narofffb4330f2009-06-17 22:40:22 +00001951 case pch::TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00001952 unsigned Idx = 0;
Steve Naroff7cae42b2009-07-10 23:34:53 +00001953 QualType OIT = GetType(Record[Idx++]);
Chris Lattner6e054af2009-04-22 06:40:03 +00001954 unsigned NumProtos = Record[Idx++];
1955 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
1956 for (unsigned I = 0; I != NumProtos; ++I)
1957 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Steve Naroff7cae42b2009-07-10 23:34:53 +00001958 return Context->getObjCObjectPointerType(OIT, Protos.data(), NumProtos);
Chris Lattner6e054af2009-04-22 06:40:03 +00001959 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00001960
1961 case pch::TYPE_OBJC_PROTOCOL_LIST: {
1962 unsigned Idx = 0;
1963 QualType OIT = GetType(Record[Idx++]);
1964 unsigned NumProtos = Record[Idx++];
1965 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
1966 for (unsigned I = 0; I != NumProtos; ++I)
1967 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
1968 return Context->getObjCProtocolListType(OIT, Protos.data(), NumProtos);
1969 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001970 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001971 // Suppress a GCC warning
1972 return QualType();
1973}
1974
John McCall8f115c62009-10-16 21:56:05 +00001975namespace {
1976
1977class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
1978 PCHReader &Reader;
1979 const PCHReader::RecordData &Record;
1980 unsigned &Idx;
1981
1982public:
1983 TypeLocReader(PCHReader &Reader, const PCHReader::RecordData &Record,
1984 unsigned &Idx)
1985 : Reader(Reader), Record(Record), Idx(Idx) { }
1986
1987#define ABSTRACT_TYPELOC(CLASS)
1988#define TYPELOC(CLASS, PARENT) \
1989 void Visit##CLASS(CLASS TyLoc);
1990#include "clang/AST/TypeLocNodes.def"
1991
1992 void VisitTypeLoc(TypeLoc TyLoc) {
1993 assert(0 && "A type loc wrapper was not handled!");
1994 }
1995};
1996
1997}
1998
1999void TypeLocReader::VisitQualifiedLoc(QualifiedLoc TyLoc) {
2000 // nothing to do
2001}
2002void TypeLocReader::VisitDefaultTypeSpecLoc(DefaultTypeSpecLoc TyLoc) {
2003 TyLoc.setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2004}
2005void TypeLocReader::VisitTypedefLoc(TypedefLoc TyLoc) {
2006 TyLoc.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2007}
2008void TypeLocReader::VisitObjCInterfaceLoc(ObjCInterfaceLoc TyLoc) {
2009 TyLoc.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2010}
2011void TypeLocReader::VisitObjCProtocolListLoc(ObjCProtocolListLoc TyLoc) {
2012 TyLoc.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2013 TyLoc.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2014 for (unsigned i = 0, e = TyLoc.getNumProtocols(); i != e; ++i)
2015 TyLoc.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
2016}
2017void TypeLocReader::VisitPointerLoc(PointerLoc TyLoc) {
2018 TyLoc.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2019}
2020void TypeLocReader::VisitBlockPointerLoc(BlockPointerLoc TyLoc) {
2021 TyLoc.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2022}
2023void TypeLocReader::VisitMemberPointerLoc(MemberPointerLoc TyLoc) {
2024 TyLoc.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2025}
2026void TypeLocReader::VisitReferenceLoc(ReferenceLoc TyLoc) {
2027 TyLoc.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2028}
2029void TypeLocReader::VisitFunctionLoc(FunctionLoc TyLoc) {
2030 TyLoc.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2031 TyLoc.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2032 for (unsigned i = 0, e = TyLoc.getNumArgs(); i != e; ++i)
2033 TyLoc.setArg(i, cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
2034}
2035void TypeLocReader::VisitArrayLoc(ArrayLoc TyLoc) {
2036 TyLoc.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2037 TyLoc.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2038 if (Record[Idx++])
2039 TyLoc.setSizeExpr(Reader.ReadDeclExpr());
Douglas Gregor12bfa382009-10-17 00:13:19 +00002040 else
2041 TyLoc.setSizeExpr(0);
John McCall8f115c62009-10-16 21:56:05 +00002042}
2043
2044DeclaratorInfo *PCHReader::GetDeclaratorInfo(const RecordData &Record,
2045 unsigned &Idx) {
2046 QualType InfoTy = GetType(Record[Idx++]);
2047 if (InfoTy.isNull())
2048 return 0;
2049
2050 DeclaratorInfo *DInfo = getContext()->CreateDeclaratorInfo(InfoTy);
2051 TypeLocReader TLR(*this, Record, Idx);
2052 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2053 TLR.Visit(TL);
2054 return DInfo;
2055}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002056
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002057QualType PCHReader::GetType(pch::TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00002058 unsigned FastQuals = ID & Qualifiers::FastMask;
2059 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002060
2061 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2062 QualType T;
2063 switch ((pch::PredefinedTypeIDs)Index) {
2064 case pch::PREDEF_TYPE_NULL_ID: return QualType();
Chris Lattner8575daa2009-04-27 21:45:14 +00002065 case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2066 case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002067
2068 case pch::PREDEF_TYPE_CHAR_U_ID:
2069 case pch::PREDEF_TYPE_CHAR_S_ID:
2070 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00002071 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002072 break;
2073
Chris Lattner8575daa2009-04-27 21:45:14 +00002074 case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2075 case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2076 case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2077 case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2078 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002079 case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
Chris Lattner8575daa2009-04-27 21:45:14 +00002080 case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2081 case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2082 case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2083 case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2084 case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2085 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002086 case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
Chris Lattner8575daa2009-04-27 21:45:14 +00002087 case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2088 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2089 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2090 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2091 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002092 case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002093 case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2094 case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002095 case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2096 case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002097 }
2098
2099 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00002100 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002101 }
2102
2103 Index -= pch::NUM_PREDEF_TYPE_IDS;
Steve Naroffc277ad12009-07-18 15:33:26 +00002104 //assert(Index < TypesLoaded.size() && "Type index out-of-range");
John McCall8ccfcb52009-09-24 19:53:00 +00002105 if (TypesLoaded[Index].isNull())
2106 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]);
Mike Stump11289f42009-09-09 15:08:12 +00002107
John McCall8ccfcb52009-09-24 19:53:00 +00002108 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002109}
2110
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002111Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002112 if (ID == 0)
2113 return 0;
2114
Douglas Gregor745ed142009-04-25 18:35:21 +00002115 if (ID > DeclsLoaded.size()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002116 Error("declaration ID out-of-range for PCH file");
Douglas Gregor745ed142009-04-25 18:35:21 +00002117 return 0;
2118 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002119
Douglas Gregor745ed142009-04-25 18:35:21 +00002120 unsigned Index = ID - 1;
2121 if (!DeclsLoaded[Index])
2122 ReadDeclRecord(DeclOffsets[Index], Index);
2123
2124 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002125}
2126
Chris Lattner9c28af02009-04-27 05:46:25 +00002127/// \brief Resolve the offset of a statement into a statement.
2128///
2129/// This operation will read a new statement from the external
2130/// source each time it is called, and is meant to be used via a
2131/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
2132Stmt *PCHReader::GetDeclStmt(uint64_t Offset) {
Chris Lattner1de76db2009-04-27 05:58:23 +00002133 // Since we know tha this statement is part of a decl, make sure to use the
2134 // decl cursor to read it.
2135 DeclsCursor.JumpToBit(Offset);
2136 return ReadStmt(DeclsCursor);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00002137}
2138
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002139bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002140 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00002141 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002142 "DeclContext has no lexical decls in storage");
2143 uint64_t Offset = DeclContextOffsets[DC].first;
2144 assert(Offset && "DeclContext has no lexical decls in storage");
2145
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002146 // Keep track of where we are in the stream, then jump back there
2147 // after reading this context.
Chris Lattner72405d62009-04-27 07:35:40 +00002148 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002149
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150 // Load the record containing all of the declarations lexically in
2151 // this context.
Chris Lattner72405d62009-04-27 07:35:40 +00002152 DeclsCursor.JumpToBit(Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002153 RecordData Record;
Chris Lattner72405d62009-04-27 07:35:40 +00002154 unsigned Code = DeclsCursor.ReadCode();
2155 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Douglas Gregore95304a2009-04-15 18:43:11 +00002156 (void)RecCode;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002157 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2158
2159 // Load all of the declaration IDs
2160 Decls.clear();
2161 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002162 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002163 return false;
2164}
2165
2166bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
Chris Lattner72405d62009-04-27 07:35:40 +00002167 llvm::SmallVectorImpl<VisibleDeclaration> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00002168 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002169 "DeclContext has no visible decls in storage");
2170 uint64_t Offset = DeclContextOffsets[DC].second;
2171 assert(Offset && "DeclContext has no visible decls in storage");
2172
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002173 // Keep track of where we are in the stream, then jump back there
2174 // after reading this context.
Chris Lattner72405d62009-04-27 07:35:40 +00002175 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002176
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002177 // Load the record containing all of the declarations visible in
2178 // this context.
Chris Lattner72405d62009-04-27 07:35:40 +00002179 DeclsCursor.JumpToBit(Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002180 RecordData Record;
Chris Lattner72405d62009-04-27 07:35:40 +00002181 unsigned Code = DeclsCursor.ReadCode();
2182 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Douglas Gregore95304a2009-04-15 18:43:11 +00002183 (void)RecCode;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002184 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2185 if (Record.size() == 0)
Mike Stump11289f42009-09-09 15:08:12 +00002186 return false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002187
2188 Decls.clear();
2189
2190 unsigned Idx = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002191 while (Idx < Record.size()) {
2192 Decls.push_back(VisibleDeclaration());
2193 Decls.back().Name = ReadDeclarationName(Record, Idx);
2194
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002195 unsigned Size = Record[Idx++];
Chris Lattner72405d62009-04-27 07:35:40 +00002196 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002197 LoadedDecls.reserve(Size);
2198 for (unsigned I = 0; I < Size; ++I)
2199 LoadedDecls.push_back(Record[Idx++]);
2200 }
2201
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002202 ++NumVisibleDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002203 return false;
2204}
2205
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002206void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00002207 this->Consumer = Consumer;
2208
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002209 if (!Consumer)
2210 return;
2211
2212 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002213 // Force deserialization of this decl, which will cause it to be passed to
2214 // the consumer (or queued).
2215 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002216 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00002217
2218 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2219 DeclGroupRef DG(InterestingDecls[I]);
2220 Consumer->HandleTopLevelDecl(DG);
2221 }
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002222}
2223
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002224void PCHReader::PrintStats() {
2225 std::fprintf(stderr, "*** PCH Statistics:\n");
2226
Mike Stump11289f42009-09-09 15:08:12 +00002227 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00002228 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00002229 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00002230 unsigned NumDeclsLoaded
2231 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2232 (Decl *)0);
2233 unsigned NumIdentifiersLoaded
2234 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2235 IdentifiersLoaded.end(),
2236 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00002237 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00002238 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2239 SelectorsLoaded.end(),
2240 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00002241
Douglas Gregorc5046832009-04-27 18:38:38 +00002242 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
2243 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00002244 if (TotalNumSLocEntries)
2245 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
2246 NumSLocEntriesRead, TotalNumSLocEntries,
2247 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00002248 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00002249 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00002250 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2251 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2252 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00002253 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00002254 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2255 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00002256 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00002257 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00002258 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2259 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002260 if (TotalNumSelectors)
2261 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2262 NumSelectorsLoaded, TotalNumSelectors,
2263 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2264 if (TotalNumStatements)
2265 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2266 NumStatementsRead, TotalNumStatements,
2267 ((float)NumStatementsRead/TotalNumStatements * 100));
2268 if (TotalNumMacros)
2269 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2270 NumMacrosRead, TotalNumMacros,
2271 ((float)NumMacrosRead/TotalNumMacros * 100));
2272 if (TotalLexicalDeclContexts)
2273 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2274 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2275 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2276 * 100));
2277 if (TotalVisibleDeclContexts)
2278 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2279 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2280 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2281 * 100));
2282 if (TotalSelectorsInMethodPool) {
2283 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2284 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2285 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2286 * 100));
2287 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2288 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002289 std::fprintf(stderr, "\n");
2290}
2291
Douglas Gregora868bbd2009-04-21 22:25:48 +00002292void PCHReader::InitializeSema(Sema &S) {
2293 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002294 S.ExternalSource = this;
2295
Douglas Gregor7cd60f72009-04-22 21:15:06 +00002296 // Makes sure any declarations that were deserialized "too early"
2297 // still get added to the identifier's declaration chains.
2298 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2299 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2300 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002301 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00002302 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00002303
2304 // If there were any tentative definitions, deserialize them and add
2305 // them to Sema's table of tentative definitions.
2306 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2307 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2308 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
Chris Lattner0c797362009-09-08 18:19:27 +00002309 SemaObj->TentativeDefinitionList.push_back(Var->getDeclName());
Douglas Gregord4df8652009-04-22 22:02:47 +00002310 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002311
2312 // If there were any locally-scoped external declarations,
2313 // deserialize them and add them to Sema's table of locally-scoped
2314 // external declarations.
2315 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2316 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2317 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2318 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002319
2320 // If there were any ext_vector type declarations, deserialize them
2321 // and add them to Sema's vector of such declarations.
2322 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
2323 SemaObj->ExtVectorDecls.push_back(
2324 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002325}
2326
2327IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2328 // Try to find this name within our on-disk hash table
Mike Stump11289f42009-09-09 15:08:12 +00002329 PCHIdentifierLookupTable *IdTable
Douglas Gregora868bbd2009-04-21 22:25:48 +00002330 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2331 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2332 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2333 if (Pos == IdTable->end())
2334 return 0;
2335
2336 // Dereferencing the iterator has the effect of building the
2337 // IdentifierInfo node and populating it with the various
2338 // declarations it needs.
2339 return *Pos;
2340}
2341
Mike Stump11289f42009-09-09 15:08:12 +00002342std::pair<ObjCMethodList, ObjCMethodList>
Douglas Gregorc78d3462009-04-24 21:10:55 +00002343PCHReader::ReadMethodPool(Selector Sel) {
2344 if (!MethodPoolLookupTable)
2345 return std::pair<ObjCMethodList, ObjCMethodList>();
2346
2347 // Try to find this selector within our on-disk hash table.
2348 PCHMethodPoolLookupTable *PoolTable
2349 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2350 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002351 if (Pos == PoolTable->end()) {
2352 ++NumMethodPoolMisses;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002353 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002354 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002355
Douglas Gregor95c13f52009-04-25 17:48:32 +00002356 ++NumMethodPoolSelectorsRead;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002357 return *Pos;
2358}
2359
Douglas Gregor0e149972009-04-25 19:10:14 +00002360void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00002361 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002362 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00002363 IdentifiersLoaded[ID - 1] = II;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002364}
2365
Douglas Gregor1342e842009-07-06 18:54:52 +00002366/// \brief Set the globally-visible declarations associated with the given
2367/// identifier.
2368///
2369/// If the PCH reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00002370/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00002371/// them.
2372///
2373/// \param II an IdentifierInfo that refers to one or more globally-visible
2374/// declarations.
2375///
2376/// \param DeclIDs the set of declaration IDs with the name @p II that are
2377/// visible at global scope.
2378///
2379/// \param Nonrecursive should be true to indicate that the caller knows that
2380/// this call is non-recursive, and therefore the globally-visible declarations
2381/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00002382void
2383PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00002384 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
2385 bool Nonrecursive) {
2386 if (CurrentlyLoadingTypeOrDecl && !Nonrecursive) {
2387 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
2388 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
2389 PII.II = II;
2390 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
2391 PII.DeclIDs.push_back(DeclIDs[I]);
2392 return;
2393 }
Mike Stump11289f42009-09-09 15:08:12 +00002394
Douglas Gregor1342e842009-07-06 18:54:52 +00002395 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
2396 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
2397 if (SemaObj) {
2398 // Introduce this declaration into the translation-unit scope
2399 // and add it to the declaration chain for this identifier, so
2400 // that (unqualified) name lookup will find it.
2401 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
2402 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
2403 } else {
2404 // Queue this declaration so that it will be added to the
2405 // translation unit scope and identifier's declaration chain
2406 // once a Sema object is known.
2407 PreloadedDecls.push_back(D);
2408 }
2409 }
2410}
2411
Chris Lattnerc523d8e2009-04-11 21:15:38 +00002412IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002413 if (ID == 0)
2414 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002415
Douglas Gregor0e149972009-04-25 19:10:14 +00002416 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002417 Error("no identifier table in PCH file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002418 return 0;
2419 }
Mike Stump11289f42009-09-09 15:08:12 +00002420
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002421 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor0e149972009-04-25 19:10:14 +00002422 if (!IdentifiersLoaded[ID - 1]) {
2423 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor95272492009-04-25 21:21:38 +00002424 const char *Str = IdentifierTableData + Offset;
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002425
Douglas Gregorab4df582009-04-28 20:01:51 +00002426 // All of the strings in the PCH file are preceded by a 16-bit
2427 // length. Extract that 16-bit length to avoid having to execute
2428 // strlen().
2429 const char *StrLenPtr = Str - 2;
2430 unsigned StrLen = (((unsigned) StrLenPtr[0])
2431 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Mike Stump11289f42009-09-09 15:08:12 +00002432 IdentifiersLoaded[ID - 1]
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002433 = &PP->getIdentifierTable().get(Str, Str + StrLen);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002434 }
Mike Stump11289f42009-09-09 15:08:12 +00002435
Douglas Gregor0e149972009-04-25 19:10:14 +00002436 return IdentifiersLoaded[ID - 1];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002437}
2438
Douglas Gregor258ae542009-04-27 06:38:32 +00002439void PCHReader::ReadSLocEntry(unsigned ID) {
2440 ReadSLocEntryRecord(ID);
2441}
2442
Steve Naroff2ddea052009-04-23 10:39:46 +00002443Selector PCHReader::DecodeSelector(unsigned ID) {
2444 if (ID == 0)
2445 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00002446
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002447 if (!MethodPoolLookupTableData)
Steve Naroff2ddea052009-04-23 10:39:46 +00002448 return Selector();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002449
2450 if (ID > TotalNumSelectors) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002451 Error("selector ID out of range in PCH file");
Steve Naroff2ddea052009-04-23 10:39:46 +00002452 return Selector();
2453 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00002454
2455 unsigned Index = ID - 1;
2456 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2457 // Load this selector from the selector table.
2458 // FIXME: endianness portability issues with SelectorOffsets table
2459 PCHMethodPoolLookupTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002460 SelectorsLoaded[Index]
Douglas Gregor95c13f52009-04-25 17:48:32 +00002461 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2462 }
2463
2464 return SelectorsLoaded[Index];
Steve Naroff2ddea052009-04-23 10:39:46 +00002465}
2466
Mike Stump11289f42009-09-09 15:08:12 +00002467DeclarationName
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002468PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2469 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2470 switch (Kind) {
2471 case DeclarationName::Identifier:
2472 return DeclarationName(GetIdentifierInfo(Record, Idx));
2473
2474 case DeclarationName::ObjCZeroArgSelector:
2475 case DeclarationName::ObjCOneArgSelector:
2476 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00002477 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002478
2479 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00002480 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00002481 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002482
2483 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00002484 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00002485 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002486
2487 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00002488 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00002489 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002490
2491 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00002492 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002493 (OverloadedOperatorKind)Record[Idx++]);
2494
2495 case DeclarationName::CXXUsingDirective:
2496 return DeclarationName::getUsingDirectiveName();
2497 }
2498
2499 // Required to silence GCC warning
2500 return DeclarationName();
2501}
Douglas Gregor55abb232009-04-10 20:39:37 +00002502
Douglas Gregor1daeb692009-04-13 18:14:40 +00002503/// \brief Read an integral value
2504llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2505 unsigned BitWidth = Record[Idx++];
2506 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2507 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2508 Idx += NumWords;
2509 return Result;
2510}
2511
2512/// \brief Read a signed integral value
2513llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2514 bool isUnsigned = Record[Idx++];
2515 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2516}
2517
Douglas Gregore0a3a512009-04-14 21:55:33 +00002518/// \brief Read a floating-point value
2519llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002520 return llvm::APFloat(ReadAPInt(Record, Idx));
2521}
2522
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002523// \brief Read a string
2524std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2525 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00002526 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002527 Idx += Len;
2528 return Result;
2529}
2530
Douglas Gregor55abb232009-04-10 20:39:37 +00002531DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002532 return Diag(SourceLocation(), DiagID);
2533}
2534
2535DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002536 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00002537}
Douglas Gregora9af1d12009-04-17 00:04:06 +00002538
Douglas Gregora868bbd2009-04-21 22:25:48 +00002539/// \brief Retrieve the identifier table associated with the
2540/// preprocessor.
2541IdentifierTable &PCHReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002542 assert(PP && "Forgot to set Preprocessor ?");
2543 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002544}
2545
Douglas Gregora9af1d12009-04-17 00:04:06 +00002546/// \brief Record that the given ID maps to the given switch-case
2547/// statement.
2548void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2549 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2550 SwitchCaseStmts[ID] = SC;
2551}
2552
2553/// \brief Retrieve the switch-case statement with the given ID.
2554SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2555 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2556 return SwitchCaseStmts[ID];
2557}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00002558
2559/// \brief Record that the given label statement has been
2560/// deserialized and has the given ID.
2561void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00002562 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00002563 "Deserialized label twice");
2564 LabelStmts[ID] = S;
2565
2566 // If we've already seen any goto statements that point to this
2567 // label, resolve them now.
2568 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2569 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2570 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2571 Goto->second->setLabel(S);
2572 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00002573
2574 // If we've already seen any address-label statements that point to
2575 // this label, resolve them now.
2576 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00002577 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00002578 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00002579 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00002580 AddrLabel != AddrLabels.second; ++AddrLabel)
2581 AddrLabel->second->setLabel(S);
2582 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00002583}
2584
2585/// \brief Set the label of the given statement to the label
2586/// identified by ID.
2587///
2588/// Depending on the order in which the label and other statements
2589/// referencing that label occur, this operation may complete
2590/// immediately (updating the statement) or it may queue the
2591/// statement to be back-patched later.
2592void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2593 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2594 if (Label != LabelStmts.end()) {
2595 // We've already seen this label, so set the label of the goto and
2596 // we're done.
2597 S->setLabel(Label->second);
2598 } else {
2599 // We haven't seen this label yet, so add this goto to the set of
2600 // unresolved goto statements.
2601 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2602 }
2603}
Douglas Gregor779d8652009-04-17 18:58:21 +00002604
2605/// \brief Set the label of the given expression to the label
2606/// identified by ID.
2607///
2608/// Depending on the order in which the label and other statements
2609/// referencing that label occur, this operation may complete
2610/// immediately (updating the statement) or it may queue the
2611/// statement to be back-patched later.
2612void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2613 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2614 if (Label != LabelStmts.end()) {
2615 // We've already seen this label, so set the label of the
2616 // label-address expression and we're done.
2617 S->setLabel(Label->second);
2618 } else {
2619 // We haven't seen this label yet, so add this label-address
2620 // expression to the set of unresolved label-address expressions.
2621 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2622 }
2623}
Douglas Gregor1342e842009-07-06 18:54:52 +00002624
2625
Mike Stump11289f42009-09-09 15:08:12 +00002626PCHReader::LoadingTypeOrDecl::LoadingTypeOrDecl(PCHReader &Reader)
Douglas Gregor1342e842009-07-06 18:54:52 +00002627 : Reader(Reader), Parent(Reader.CurrentlyLoadingTypeOrDecl) {
2628 Reader.CurrentlyLoadingTypeOrDecl = this;
2629}
2630
2631PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() {
2632 if (!Parent) {
2633 // If any identifiers with corresponding top-level declarations have
2634 // been loaded, load those declarations now.
2635 while (!Reader.PendingIdentifierInfos.empty()) {
2636 Reader.SetGloballyVisibleDecls(Reader.PendingIdentifierInfos.front().II,
2637 Reader.PendingIdentifierInfos.front().DeclIDs,
2638 true);
2639 Reader.PendingIdentifierInfos.pop_front();
2640 }
2641 }
2642
Mike Stump11289f42009-09-09 15:08:12 +00002643 Reader.CurrentlyLoadingTypeOrDecl = Parent;
Douglas Gregor1342e842009-07-06 18:54:52 +00002644}