blob: 86848758ad6218a99ce50e79f81461a032bb6113 [file] [log] [blame]
Douglas Gregorc34897d2009-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 Lattner09547942009-04-27 05:14:47 +000013
Douglas Gregorc34897d2009-04-09 22:27:44 +000014#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000015#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000016#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor631f6c62009-04-14 00:24:19 +000017#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000020#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000021#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000022#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000023#include "clang/Lex/HeaderSearch.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000024#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
Douglas Gregorc713da92009-04-21 22:25:48 +000037// PCH reader implementation
38//===----------------------------------------------------------------------===//
39
Chris Lattner09547942009-04-27 05:14:47 +000040PCHReader::PCHReader(Preprocessor &PP, ASTContext &Context)
41 : SemaObj(0), PP(PP), Context(Context), Consumer(0),
42 IdentifierTableData(0), IdentifierLookupTable(0),
43 IdentifierOffsets(0),
44 MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
45 TotalSelectorsInMethodPool(0), SelectorOffsets(0),
46 TotalNumSelectors(0), NumStatementsRead(0), NumMacrosRead(0),
47 NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0),
48 NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0) { }
49
50PCHReader::~PCHReader() {}
51
Chris Lattner3ef21962009-04-27 05:58:23 +000052Expr *PCHReader::ReadDeclExpr() {
53 return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor));
54}
55
56Expr *PCHReader::ReadTypeExpr() {
Chris Lattner3282c392009-04-27 05:41:06 +000057 return dyn_cast_or_null<Expr>(ReadStmt(Stream));
Chris Lattner09547942009-04-27 05:14:47 +000058}
59
60
Douglas Gregorc713da92009-04-21 22:25:48 +000061namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +000062class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
63 PCHReader &Reader;
64
65public:
66 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
67
68 typedef Selector external_key_type;
69 typedef external_key_type internal_key_type;
70
71 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
72
73 static bool EqualKey(const internal_key_type& a,
74 const internal_key_type& b) {
75 return a == b;
76 }
77
78 static unsigned ComputeHash(Selector Sel) {
79 unsigned N = Sel.getNumArgs();
80 if (N == 0)
81 ++N;
82 unsigned R = 5381;
83 for (unsigned I = 0; I != N; ++I)
84 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
85 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
86 return R;
87 }
88
89 // This hopefully will just get inlined and removed by the optimizer.
90 static const internal_key_type&
91 GetInternalKey(const external_key_type& x) { return x; }
92
93 static std::pair<unsigned, unsigned>
94 ReadKeyDataLength(const unsigned char*& d) {
95 using namespace clang::io;
96 unsigned KeyLen = ReadUnalignedLE16(d);
97 unsigned DataLen = ReadUnalignedLE16(d);
98 return std::make_pair(KeyLen, DataLen);
99 }
100
Douglas Gregor2d711832009-04-25 17:48:32 +0000101 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000102 using namespace clang::io;
103 SelectorTable &SelTable = Reader.getContext().Selectors;
104 unsigned N = ReadUnalignedLE16(d);
105 IdentifierInfo *FirstII
106 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
107 if (N == 0)
108 return SelTable.getNullarySelector(FirstII);
109 else if (N == 1)
110 return SelTable.getUnarySelector(FirstII);
111
112 llvm::SmallVector<IdentifierInfo *, 16> Args;
113 Args.push_back(FirstII);
114 for (unsigned I = 1; I != N; ++I)
115 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
116
117 return SelTable.getSelector(N, &Args[0]);
118 }
119
120 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
121 using namespace clang::io;
122 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
123 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
124
125 data_type Result;
126
127 // Load instance methods
128 ObjCMethodList *Prev = 0;
129 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
130 ObjCMethodDecl *Method
131 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
132 if (!Result.first.Method) {
133 // This is the first method, which is the easy case.
134 Result.first.Method = Method;
135 Prev = &Result.first;
136 continue;
137 }
138
139 Prev->Next = new ObjCMethodList(Method, 0);
140 Prev = Prev->Next;
141 }
142
143 // Load factory methods
144 Prev = 0;
145 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
146 ObjCMethodDecl *Method
147 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
148 if (!Result.second.Method) {
149 // This is the first method, which is the easy case.
150 Result.second.Method = Method;
151 Prev = &Result.second;
152 continue;
153 }
154
155 Prev->Next = new ObjCMethodList(Method, 0);
156 Prev = Prev->Next;
157 }
158
159 return Result;
160 }
161};
162
163} // end anonymous namespace
164
165/// \brief The on-disk hash table used for the global method pool.
166typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
167 PCHMethodPoolLookupTable;
168
169namespace {
Douglas Gregorc713da92009-04-21 22:25:48 +0000170class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
171 PCHReader &Reader;
172
173 // If we know the IdentifierInfo in advance, it is here and we will
174 // not build a new one. Used when deserializing information about an
175 // identifier that was constructed before the PCH file was read.
176 IdentifierInfo *KnownII;
177
178public:
179 typedef IdentifierInfo * data_type;
180
181 typedef const std::pair<const char*, unsigned> external_key_type;
182
183 typedef external_key_type internal_key_type;
184
185 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
186 : Reader(Reader), KnownII(II) { }
187
188 static bool EqualKey(const internal_key_type& a,
189 const internal_key_type& b) {
190 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
191 : false;
192 }
193
194 static unsigned ComputeHash(const internal_key_type& a) {
195 return BernsteinHash(a.first, a.second);
196 }
197
198 // This hopefully will just get inlined and removed by the optimizer.
199 static const internal_key_type&
200 GetInternalKey(const external_key_type& x) { return x; }
201
202 static std::pair<unsigned, unsigned>
203 ReadKeyDataLength(const unsigned char*& d) {
204 using namespace clang::io;
Douglas Gregor4bb24882009-04-25 20:26:24 +0000205 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor85c4a872009-04-25 21:04:17 +0000206 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregorc713da92009-04-21 22:25:48 +0000207 return std::make_pair(KeyLen, DataLen);
208 }
209
210 static std::pair<const char*, unsigned>
211 ReadKey(const unsigned char* d, unsigned n) {
212 assert(n >= 2 && d[n-1] == '\0');
213 return std::make_pair((const char*) d, n-1);
214 }
215
216 IdentifierInfo *ReadData(const internal_key_type& k,
217 const unsigned char* d,
218 unsigned DataLen) {
219 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +0000220 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +0000221 bool CPlusPlusOperatorKeyword = Bits & 0x01;
222 Bits >>= 1;
223 bool Poisoned = Bits & 0x01;
224 Bits >>= 1;
225 bool ExtensionToken = Bits & 0x01;
226 Bits >>= 1;
227 bool hasMacroDefinition = Bits & 0x01;
228 Bits >>= 1;
229 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
230 Bits >>= 10;
231 unsigned TokenID = Bits & 0xFF;
232 Bits >>= 8;
233
Douglas Gregorc713da92009-04-21 22:25:48 +0000234 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +0000235 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +0000236 DataLen -= 8;
237
238 // Build the IdentifierInfo itself and link the identifier ID with
239 // the new IdentifierInfo.
240 IdentifierInfo *II = KnownII;
241 if (!II)
Douglas Gregor4bb24882009-04-25 20:26:24 +0000242 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
243 k.first, k.first + k.second);
Douglas Gregorc713da92009-04-21 22:25:48 +0000244 Reader.SetIdentifierInfo(ID, II);
245
Douglas Gregorda38c6c2009-04-22 18:49:13 +0000246 // Set or check the various bits in the IdentifierInfo structure.
247 // FIXME: Load token IDs lazily, too?
248 assert((unsigned)II->getTokenID() == TokenID &&
249 "Incorrect token ID loaded");
250 (void)TokenID;
251 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
252 assert(II->isExtensionToken() == ExtensionToken &&
253 "Incorrect extension token flag");
254 (void)ExtensionToken;
255 II->setIsPoisoned(Poisoned);
256 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
257 "Incorrect C++ operator keyword flag");
258 (void)CPlusPlusOperatorKeyword;
259
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000260 // If this identifier is a macro, deserialize the macro
261 // definition.
262 if (hasMacroDefinition) {
263 uint32_t Offset = ReadUnalignedLE64(d);
264 Reader.ReadMacroRecord(Offset);
265 DataLen -= 8;
266 }
Douglas Gregorc713da92009-04-21 22:25:48 +0000267
268 // Read all of the declarations visible at global scope with this
269 // name.
270 Sema *SemaObj = Reader.getSema();
271 while (DataLen > 0) {
272 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +0000273 if (SemaObj) {
274 // Introduce this declaration into the translation-unit scope
275 // and add it to the declaration chain for this identifier, so
276 // that (unqualified) name lookup will find it.
277 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
278 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
279 } else {
280 // Queue this declaration so that it will be added to the
281 // translation unit scope and identifier's declaration chain
282 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +0000283 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +0000284 }
285
286 DataLen -= 4;
287 }
288 return II;
289 }
290};
291
292} // end anonymous namespace
293
294/// \brief The on-disk hash table used to contain information about
295/// all of the identifiers in the program.
296typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
297 PCHIdentifierLookupTable;
298
Douglas Gregorc34897d2009-04-09 22:27:44 +0000299// FIXME: use the diagnostics machinery
300static bool Error(const char *Str) {
301 std::fprintf(stderr, "%s\n", Str);
302 return true;
303}
304
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000305/// \brief Check the contents of the predefines buffer against the
306/// contents of the predefines buffer used to build the PCH file.
307///
308/// The contents of the two predefines buffers should be the same. If
309/// not, then some command-line option changed the preprocessor state
310/// and we must reject the PCH file.
311///
312/// \param PCHPredef The start of the predefines buffer in the PCH
313/// file.
314///
315/// \param PCHPredefLen The length of the predefines buffer in the PCH
316/// file.
317///
318/// \param PCHBufferID The FileID for the PCH predefines buffer.
319///
320/// \returns true if there was a mismatch (in which case the PCH file
321/// should be ignored), or false otherwise.
322bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
323 unsigned PCHPredefLen,
324 FileID PCHBufferID) {
325 const char *Predef = PP.getPredefines().c_str();
326 unsigned PredefLen = PP.getPredefines().size();
327
328 // If the two predefines buffers compare equal, we're done!.
329 if (PredefLen == PCHPredefLen &&
330 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
331 return false;
332
333 // The predefines buffers are different. Produce a reasonable
334 // diagnostic showing where they are different.
335
336 // The source locations (potentially in the two different predefines
337 // buffers)
338 SourceLocation Loc1, Loc2;
339 SourceManager &SourceMgr = PP.getSourceManager();
340
341 // Create a source buffer for our predefines string, so
342 // that we can build a diagnostic that points into that
343 // source buffer.
344 FileID BufferID;
345 if (Predef && Predef[0]) {
346 llvm::MemoryBuffer *Buffer
347 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
348 "<built-in>");
349 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
350 }
351
352 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
353 std::pair<const char *, const char *> Locations
354 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
355
356 if (Locations.first != Predef + MinLen) {
357 // We found the location in the two buffers where there is a
358 // difference. Form source locations to point there (in both
359 // buffers).
360 unsigned Offset = Locations.first - Predef;
361 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
362 .getFileLocWithOffset(Offset);
363 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
364 .getFileLocWithOffset(Offset);
365 } else if (PredefLen > PCHPredefLen) {
366 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
367 .getFileLocWithOffset(MinLen);
368 } else {
369 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
370 .getFileLocWithOffset(MinLen);
371 }
372
373 Diag(Loc1, diag::warn_pch_preprocessor);
374 if (Loc2.isValid())
375 Diag(Loc2, diag::note_predef_in_pch);
376 Diag(diag::note_ignoring_pch) << FileName;
377 return true;
378}
379
Douglas Gregor635f97f2009-04-13 16:31:14 +0000380/// \brief Read the line table in the source manager block.
381/// \returns true if ther was an error.
382static bool ParseLineTable(SourceManager &SourceMgr,
383 llvm::SmallVectorImpl<uint64_t> &Record) {
384 unsigned Idx = 0;
385 LineTableInfo &LineTable = SourceMgr.getLineTable();
386
387 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000388 std::map<int, int> FileIDs;
389 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000390 // Extract the file name
391 unsigned FilenameLen = Record[Idx++];
392 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
393 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000394 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
395 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000396 }
397
398 // Parse the line entries
399 std::vector<LineEntry> Entries;
400 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000401 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000402
403 // Extract the line entries
404 unsigned NumEntries = Record[Idx++];
405 Entries.clear();
406 Entries.reserve(NumEntries);
407 for (unsigned I = 0; I != NumEntries; ++I) {
408 unsigned FileOffset = Record[Idx++];
409 unsigned LineNo = Record[Idx++];
410 int FilenameID = Record[Idx++];
411 SrcMgr::CharacteristicKind FileKind
412 = (SrcMgr::CharacteristicKind)Record[Idx++];
413 unsigned IncludeOffset = Record[Idx++];
414 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
415 FileKind, IncludeOffset));
416 }
417 LineTable.AddEntry(FID, Entries);
418 }
419
420 return false;
421}
422
Douglas Gregorab1cef72009-04-10 03:52:48 +0000423/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000424PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000425 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000426 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
427 Error("Malformed source manager block record");
428 return Failure;
429 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000430
431 SourceManager &SourceMgr = Context.getSourceManager();
432 RecordData Record;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000433 unsigned NumHeaderInfos = 0;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000434 while (true) {
435 unsigned Code = Stream.ReadCode();
436 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000437 if (Stream.ReadBlockEnd()) {
438 Error("Error at end of Source Manager block");
439 return Failure;
440 }
441
442 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000443 }
444
445 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
446 // No known subblocks, always skip them.
447 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000448 if (Stream.SkipBlock()) {
449 Error("Malformed block record");
450 return Failure;
451 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000452 continue;
453 }
454
455 if (Code == llvm::bitc::DEFINE_ABBREV) {
456 Stream.ReadAbbrevRecord();
457 continue;
458 }
459
460 // Read a record.
461 const char *BlobStart;
462 unsigned BlobLen;
463 Record.clear();
464 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
465 default: // Default behavior: ignore.
466 break;
467
468 case pch::SM_SLOC_FILE_ENTRY: {
469 // FIXME: We would really like to delay the creation of this
470 // FileEntry until it is actually required, e.g., when producing
471 // a diagnostic with a source location in this file.
472 const FileEntry *File
473 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
474 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000475 FileID ID = SourceMgr.createFileID(File,
476 SourceLocation::getFromRawEncoding(Record[1]),
477 (CharacteristicKind)Record[2]);
478 if (Record[3])
479 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
480 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000481 break;
482 }
483
484 case pch::SM_SLOC_BUFFER_ENTRY: {
485 const char *Name = BlobStart;
486 unsigned Code = Stream.ReadCode();
487 Record.clear();
488 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
489 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000490 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000491 llvm::MemoryBuffer *Buffer
492 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
493 BlobStart + BlobLen - 1,
494 Name);
495 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
496
497 if (strcmp(Name, "<built-in>") == 0
498 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
499 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000500 break;
501 }
502
503 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
504 SourceLocation SpellingLoc
505 = SourceLocation::getFromRawEncoding(Record[1]);
506 SourceMgr.createInstantiationLoc(
507 SpellingLoc,
508 SourceLocation::getFromRawEncoding(Record[2]),
509 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +0000510 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000511 break;
512 }
513
Chris Lattnere1be6022009-04-14 23:22:57 +0000514 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +0000515 if (ParseLineTable(SourceMgr, Record))
516 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +0000517 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000518
519 case pch::SM_HEADER_FILE_INFO: {
520 HeaderFileInfo HFI;
521 HFI.isImport = Record[0];
522 HFI.DirInfo = Record[1];
523 HFI.NumIncludes = Record[2];
524 HFI.ControllingMacroID = Record[3];
525 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
526 break;
527 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000528 }
529 }
530}
531
Chris Lattner4fc71eb2009-04-27 01:05:14 +0000532/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
533/// specified cursor. Read the abbreviations that are at the top of the block
534/// and then leave the cursor pointing into the block.
535bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
536 unsigned BlockID) {
537 if (Cursor.EnterSubBlock(BlockID)) {
538 Error("Malformed block record");
539 return Failure;
540 }
541
Chris Lattner4fc71eb2009-04-27 01:05:14 +0000542 while (true) {
543 unsigned Code = Cursor.ReadCode();
544
545 // We expect all abbrevs to be at the start of the block.
546 if (Code != llvm::bitc::DEFINE_ABBREV)
547 return false;
548 Cursor.ReadAbbrevRecord();
549 }
550}
551
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000552void PCHReader::ReadMacroRecord(uint64_t Offset) {
553 // Keep track of where we are in the stream, then jump back there
554 // after reading this macro.
555 SavedStreamPosition SavedPosition(Stream);
556
557 Stream.JumpToBit(Offset);
558 RecordData Record;
559 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
560 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +0000561
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000562 while (true) {
563 unsigned Code = Stream.ReadCode();
564 switch (Code) {
565 case llvm::bitc::END_BLOCK:
566 return;
567
568 case llvm::bitc::ENTER_SUBBLOCK:
569 // No known subblocks, always skip them.
570 Stream.ReadSubBlockID();
571 if (Stream.SkipBlock()) {
572 Error("Malformed block record");
573 return;
574 }
575 continue;
576
577 case llvm::bitc::DEFINE_ABBREV:
578 Stream.ReadAbbrevRecord();
579 continue;
580 default: break;
581 }
582
583 // Read a record.
584 Record.clear();
585 pch::PreprocessorRecordTypes RecType =
586 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
587 switch (RecType) {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000588 case pch::PP_MACRO_OBJECT_LIKE:
589 case pch::PP_MACRO_FUNCTION_LIKE: {
590 // If we already have a macro, that means that we've hit the end
591 // of the definition of the macro we were looking for. We're
592 // done.
593 if (Macro)
594 return;
595
596 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
597 if (II == 0) {
598 Error("Macro must have a name");
599 return;
600 }
601 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
602 bool isUsed = Record[2];
603
604 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
605 MI->setIsUsed(isUsed);
606
607 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
608 // Decode function-like macro info.
609 bool isC99VarArgs = Record[3];
610 bool isGNUVarArgs = Record[4];
611 MacroArgs.clear();
612 unsigned NumArgs = Record[5];
613 for (unsigned i = 0; i != NumArgs; ++i)
614 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
615
616 // Install function-like macro info.
617 MI->setIsFunctionLike();
618 if (isC99VarArgs) MI->setIsC99Varargs();
619 if (isGNUVarArgs) MI->setIsGNUVarargs();
620 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
621 PP.getPreprocessorAllocator());
622 }
623
624 // Finally, install the macro.
625 PP.setMacroInfo(II, MI);
626
627 // Remember that we saw this macro last so that we add the tokens that
628 // form its body to it.
629 Macro = MI;
630 ++NumMacrosRead;
631 break;
632 }
633
634 case pch::PP_TOKEN: {
635 // If we see a TOKEN before a PP_MACRO_*, then the file is
636 // erroneous, just pretend we didn't see this.
637 if (Macro == 0) break;
638
639 Token Tok;
640 Tok.startToken();
641 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
642 Tok.setLength(Record[1]);
643 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
644 Tok.setIdentifierInfo(II);
645 Tok.setKind((tok::TokenKind)Record[3]);
646 Tok.setFlag((Token::TokenFlags)Record[4]);
647 Macro->AddTokenToBody(Tok);
648 break;
649 }
Steve Naroffcda68f22009-04-24 20:03:17 +0000650 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000651 }
652}
653
Douglas Gregorc713da92009-04-21 22:25:48 +0000654PCHReader::PCHReadResult
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000655PCHReader::ReadPCHBlock() {
Douglas Gregor179cfb12009-04-10 20:39:37 +0000656 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
657 Error("Malformed block record");
658 return Failure;
659 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000660
661 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +0000662 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000663 while (!Stream.AtEndOfStream()) {
664 unsigned Code = Stream.ReadCode();
665 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +0000666 if (Stream.ReadBlockEnd()) {
667 Error("Error at end of module block");
668 return Failure;
669 }
Chris Lattner29241862009-04-11 21:15:38 +0000670
Douglas Gregor179cfb12009-04-10 20:39:37 +0000671 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000672 }
673
674 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
675 switch (Stream.ReadSubBlockID()) {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000676 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
677 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000678 if (Stream.SkipBlock()) {
679 Error("Malformed block record");
680 return Failure;
681 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000682 break;
683
Chris Lattner4fc71eb2009-04-27 01:05:14 +0000684 case pch::DECLS_BLOCK_ID:
685 // We lazily load the decls block, but we want to set up the
686 // DeclsCursor cursor to point into it. Clone our current bitcode
687 // cursor to it, enter the block and read the abbrevs in that block.
688 // With the main cursor, we just skip over it.
689 DeclsCursor = Stream;
690 if (Stream.SkipBlock() || // Skip with the main cursor.
691 // Read the abbrevs.
692 ReadBlockAbbrevs(DeclsCursor, pch::DECLS_BLOCK_ID)) {
693 Error("Malformed block record");
694 return Failure;
695 }
696 break;
697
Chris Lattner29241862009-04-11 21:15:38 +0000698 case pch::PREPROCESSOR_BLOCK_ID:
Chris Lattner29241862009-04-11 21:15:38 +0000699 if (Stream.SkipBlock()) {
700 Error("Malformed block record");
701 return Failure;
702 }
703 break;
Steve Naroff9e84d782009-04-23 10:39:46 +0000704
Douglas Gregorab1cef72009-04-10 03:52:48 +0000705 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000706 switch (ReadSourceManagerBlock()) {
707 case Success:
708 break;
709
710 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000711 Error("Malformed source manager block");
712 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000713
714 case IgnorePCH:
715 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000716 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000717 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000718 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000719 continue;
720 }
721
722 if (Code == llvm::bitc::DEFINE_ABBREV) {
723 Stream.ReadAbbrevRecord();
724 continue;
725 }
726
727 // Read and process a record.
728 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +0000729 const char *BlobStart = 0;
730 unsigned BlobLen = 0;
731 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
732 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000733 default: // Default behavior: ignore.
734 break;
735
736 case pch::TYPE_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +0000737 if (!TypesLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +0000738 Error("Duplicate TYPE_OFFSET record in PCH file");
739 return Failure;
740 }
Douglas Gregor24a224c2009-04-25 18:35:21 +0000741 TypeOffsets = (const uint64_t *)BlobStart;
742 TypesLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +0000743 break;
744
745 case pch::DECL_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +0000746 if (!DeclsLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +0000747 Error("Duplicate DECL_OFFSET record in PCH file");
748 return Failure;
749 }
Douglas Gregor24a224c2009-04-25 18:35:21 +0000750 DeclOffsets = (const uint64_t *)BlobStart;
751 DeclsLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +0000752 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000753
754 case pch::LANGUAGE_OPTIONS:
755 if (ParseLanguageOptions(Record))
756 return IgnorePCH;
757 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +0000758
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000759 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +0000760 std::string TargetTriple(BlobStart, BlobLen);
761 if (TargetTriple != Context.Target.getTargetTriple()) {
762 Diag(diag::warn_pch_target_triple)
763 << TargetTriple << Context.Target.getTargetTriple();
764 Diag(diag::note_ignoring_pch) << FileName;
765 return IgnorePCH;
766 }
767 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000768 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000769
770 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +0000771 IdentifierTableData = BlobStart;
Douglas Gregorde44c9f2009-04-25 19:10:14 +0000772 if (Record[0]) {
773 IdentifierLookupTable
774 = PCHIdentifierLookupTable::Create(
Douglas Gregorc713da92009-04-21 22:25:48 +0000775 (const unsigned char *)IdentifierTableData + Record[0],
776 (const unsigned char *)IdentifierTableData,
777 PCHIdentifierLookupTrait(*this));
Douglas Gregorde44c9f2009-04-25 19:10:14 +0000778 PP.getIdentifierTable().setExternalIdentifierLookup(this);
779 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000780 break;
781
782 case pch::IDENTIFIER_OFFSET:
Douglas Gregorde44c9f2009-04-25 19:10:14 +0000783 if (!IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000784 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
785 return Failure;
786 }
Douglas Gregorde44c9f2009-04-25 19:10:14 +0000787 IdentifierOffsets = (const uint32_t *)BlobStart;
788 IdentifiersLoaded.resize(Record[0]);
Douglas Gregoreccb51d2009-04-25 23:30:02 +0000789 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000790 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +0000791
792 case pch::EXTERNAL_DEFINITIONS:
793 if (!ExternalDefinitions.empty()) {
794 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
795 return Failure;
796 }
797 ExternalDefinitions.swap(Record);
798 break;
Douglas Gregor456e0952009-04-17 22:13:46 +0000799
Douglas Gregore01ad442009-04-18 05:55:16 +0000800 case pch::SPECIAL_TYPES:
801 SpecialTypes.swap(Record);
802 break;
803
Douglas Gregor456e0952009-04-17 22:13:46 +0000804 case pch::STATISTICS:
805 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000806 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +0000807 TotalLexicalDeclContexts = Record[2];
808 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +0000809 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +0000810 case pch::TENTATIVE_DEFINITIONS:
811 if (!TentativeDefinitions.empty()) {
812 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
813 return Failure;
814 }
815 TentativeDefinitions.swap(Record);
816 break;
Douglas Gregor062d9482009-04-22 22:18:58 +0000817
818 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
819 if (!LocallyScopedExternalDecls.empty()) {
820 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
821 return Failure;
822 }
823 LocallyScopedExternalDecls.swap(Record);
824 break;
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000825
Douglas Gregor2d711832009-04-25 17:48:32 +0000826 case pch::SELECTOR_OFFSETS:
827 SelectorOffsets = (const uint32_t *)BlobStart;
828 TotalNumSelectors = Record[0];
829 SelectorsLoaded.resize(TotalNumSelectors);
830 break;
831
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000832 case pch::METHOD_POOL:
Douglas Gregor2d711832009-04-25 17:48:32 +0000833 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
834 if (Record[0])
835 MethodPoolLookupTable
836 = PCHMethodPoolLookupTable::Create(
837 MethodPoolLookupTableData + Record[0],
838 MethodPoolLookupTableData,
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000839 PCHMethodPoolLookupTrait(*this));
Douglas Gregor2d711832009-04-25 17:48:32 +0000840 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000841 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000842
843 case pch::PP_COUNTER_VALUE:
844 if (!Record.empty())
845 PP.setCounterValue(Record[0]);
846 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000847 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000848 }
Douglas Gregor179cfb12009-04-10 20:39:37 +0000849 Error("Premature end of bitstream");
850 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000851}
852
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000853PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +0000854 // Set the PCH file name.
855 this->FileName = FileName;
856
Douglas Gregorc34897d2009-04-09 22:27:44 +0000857 // Open the PCH file.
858 std::string ErrStr;
859 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000860 if (!Buffer) {
861 Error(ErrStr.c_str());
862 return IgnorePCH;
863 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000864
865 // Initialize the stream
Chris Lattner587788a2009-04-26 20:59:20 +0000866 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
867 (const unsigned char *)Buffer->getBufferEnd());
868 Stream.init(StreamFile);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000869
870 // Sniff for the signature.
871 if (Stream.Read(8) != 'C' ||
872 Stream.Read(8) != 'P' ||
873 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000874 Stream.Read(8) != 'H') {
875 Error("Not a PCH file");
876 return IgnorePCH;
877 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000878
Douglas Gregorc34897d2009-04-09 22:27:44 +0000879 while (!Stream.AtEndOfStream()) {
880 unsigned Code = Stream.ReadCode();
881
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000882 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
883 Error("Invalid record at top-level");
884 return Failure;
885 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000886
887 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +0000888
Douglas Gregorc34897d2009-04-09 22:27:44 +0000889 // We only know the PCH subblock ID.
890 switch (BlockID) {
891 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000892 if (Stream.ReadBlockInfoBlock()) {
893 Error("Malformed BlockInfoBlock");
894 return Failure;
895 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000896 break;
897 case pch::PCH_BLOCK_ID:
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000898 switch (ReadPCHBlock()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +0000899 case Success:
900 break;
901
902 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000903 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000904
905 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +0000906 // FIXME: We could consider reading through to the end of this
907 // PCH block, skipping subblocks, to see if there are other
908 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000909 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000910 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000911 break;
912 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000913 if (Stream.SkipBlock()) {
914 Error("Malformed block record");
915 return Failure;
916 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000917 break;
918 }
919 }
920
921 // Load the translation unit declaration
922 ReadDeclRecord(DeclOffsets[0], 0);
923
Douglas Gregorc713da92009-04-21 22:25:48 +0000924 // Initialization of builtins and library builtins occurs before the
925 // PCH file is read, so there may be some identifiers that were
926 // loaded into the IdentifierTable before we intercepted the
927 // creation of identifiers. Iterate through the list of known
928 // identifiers and determine whether we have to establish
929 // preprocessor definitions or top-level identifier declaration
930 // chains for those identifiers.
931 //
932 // We copy the IdentifierInfo pointers to a small vector first,
933 // since de-serializing declarations or macro definitions can add
934 // new entries into the identifier table, invalidating the
935 // iterators.
936 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
937 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
938 IdEnd = PP.getIdentifierTable().end();
939 Id != IdEnd; ++Id)
940 Identifiers.push_back(Id->second);
941 PCHIdentifierLookupTable *IdTable
942 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
943 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
944 IdentifierInfo *II = Identifiers[I];
945 // Look in the on-disk hash table for an entry for
946 PCHIdentifierLookupTrait Info(*this, II);
947 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
948 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
949 if (Pos == IdTable->end())
950 continue;
951
952 // Dereferencing the iterator has the effect of populating the
953 // IdentifierInfo node with the various declarations it needs.
954 (void)*Pos;
955 }
956
Douglas Gregore01ad442009-04-18 05:55:16 +0000957 // Load the special types.
958 Context.setBuiltinVaListType(
959 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +0000960 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
961 Context.setObjCIdType(GetType(Id));
962 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
963 Context.setObjCSelType(GetType(Sel));
964 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
965 Context.setObjCProtoType(GetType(Proto));
966 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
967 Context.setObjCClassType(GetType(Class));
968 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
969 Context.setCFConstantStringType(GetType(String));
970 if (unsigned FastEnum
971 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
972 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000973
Douglas Gregorc713da92009-04-21 22:25:48 +0000974 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000975}
976
Douglas Gregor179cfb12009-04-10 20:39:37 +0000977/// \brief Parse the record that corresponds to a LangOptions data
978/// structure.
979///
980/// This routine compares the language options used to generate the
981/// PCH file against the language options set for the current
982/// compilation. For each option, we classify differences between the
983/// two compiler states as either "benign" or "important". Benign
984/// differences don't matter, and we accept them without complaint
985/// (and without modifying the language options). Differences between
986/// the states for important options cause the PCH file to be
987/// unusable, so we emit a warning and return true to indicate that
988/// there was an error.
989///
990/// \returns true if the PCH file is unacceptable, false otherwise.
991bool PCHReader::ParseLanguageOptions(
992 const llvm::SmallVectorImpl<uint64_t> &Record) {
993 const LangOptions &LangOpts = Context.getLangOptions();
994#define PARSE_LANGOPT_BENIGN(Option) ++Idx
995#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
996 if (Record[Idx] != LangOpts.Option) { \
997 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
998 Diag(diag::note_ignoring_pch) << FileName; \
999 return true; \
1000 } \
1001 ++Idx
1002
1003 unsigned Idx = 0;
1004 PARSE_LANGOPT_BENIGN(Trigraphs);
1005 PARSE_LANGOPT_BENIGN(BCPLComment);
1006 PARSE_LANGOPT_BENIGN(DollarIdents);
1007 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1008 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1009 PARSE_LANGOPT_BENIGN(ImplicitInt);
1010 PARSE_LANGOPT_BENIGN(Digraphs);
1011 PARSE_LANGOPT_BENIGN(HexFloats);
1012 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1013 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1014 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1015 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1016 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1017 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1018 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1019 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1020 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1021 PARSE_LANGOPT_BENIGN(PascalStrings);
1022 PARSE_LANGOPT_BENIGN(Boolean);
1023 PARSE_LANGOPT_BENIGN(WritableStrings);
1024 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1025 diag::warn_pch_lax_vector_conversions);
1026 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1027 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1028 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1029 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1030 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1031 diag::warn_pch_thread_safe_statics);
1032 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1033 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1034 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1035 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1036 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1037 diag::warn_pch_heinous_extensions);
1038 // FIXME: Most of the options below are benign if the macro wasn't
1039 // used. Unfortunately, this means that a PCH compiled without
1040 // optimization can't be used with optimization turned on, even
1041 // though the only thing that changes is whether __OPTIMIZE__ was
1042 // defined... but if __OPTIMIZE__ never showed up in the header, it
1043 // doesn't matter. We could consider making this some special kind
1044 // of check.
1045 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1046 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1047 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1048 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1049 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1050 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1051 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1052 Diag(diag::warn_pch_gc_mode)
1053 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1054 Diag(diag::note_ignoring_pch) << FileName;
1055 return true;
1056 }
1057 ++Idx;
1058 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1059 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1060#undef PARSE_LANGOPT_IRRELEVANT
1061#undef PARSE_LANGOPT_BENIGN
1062
1063 return false;
1064}
1065
Douglas Gregorc34897d2009-04-09 22:27:44 +00001066/// \brief Read and return the type at the given offset.
1067///
1068/// This routine actually reads the record corresponding to the type
1069/// at the given offset in the bitstream. It is a helper routine for
1070/// GetType, which deals with reading type IDs.
1071QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001072 // Keep track of where we are in the stream, then jump back there
1073 // after reading this type.
1074 SavedStreamPosition SavedPosition(Stream);
1075
Douglas Gregorc34897d2009-04-09 22:27:44 +00001076 Stream.JumpToBit(Offset);
1077 RecordData Record;
1078 unsigned Code = Stream.ReadCode();
1079 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001080 case pch::TYPE_EXT_QUAL: {
1081 assert(Record.size() == 3 &&
1082 "Incorrect encoding of extended qualifier type");
1083 QualType Base = GetType(Record[0]);
1084 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1085 unsigned AddressSpace = Record[2];
1086
1087 QualType T = Base;
1088 if (GCAttr != QualType::GCNone)
1089 T = Context.getObjCGCQualType(T, GCAttr);
1090 if (AddressSpace)
1091 T = Context.getAddrSpaceQualType(T, AddressSpace);
1092 return T;
1093 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001094
Douglas Gregorc34897d2009-04-09 22:27:44 +00001095 case pch::TYPE_FIXED_WIDTH_INT: {
1096 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1097 return Context.getFixedWidthIntType(Record[0], Record[1]);
1098 }
1099
1100 case pch::TYPE_COMPLEX: {
1101 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1102 QualType ElemType = GetType(Record[0]);
1103 return Context.getComplexType(ElemType);
1104 }
1105
1106 case pch::TYPE_POINTER: {
1107 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1108 QualType PointeeType = GetType(Record[0]);
1109 return Context.getPointerType(PointeeType);
1110 }
1111
1112 case pch::TYPE_BLOCK_POINTER: {
1113 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1114 QualType PointeeType = GetType(Record[0]);
1115 return Context.getBlockPointerType(PointeeType);
1116 }
1117
1118 case pch::TYPE_LVALUE_REFERENCE: {
1119 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1120 QualType PointeeType = GetType(Record[0]);
1121 return Context.getLValueReferenceType(PointeeType);
1122 }
1123
1124 case pch::TYPE_RVALUE_REFERENCE: {
1125 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1126 QualType PointeeType = GetType(Record[0]);
1127 return Context.getRValueReferenceType(PointeeType);
1128 }
1129
1130 case pch::TYPE_MEMBER_POINTER: {
1131 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1132 QualType PointeeType = GetType(Record[0]);
1133 QualType ClassType = GetType(Record[1]);
1134 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1135 }
1136
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001137 case pch::TYPE_CONSTANT_ARRAY: {
1138 QualType ElementType = GetType(Record[0]);
1139 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1140 unsigned IndexTypeQuals = Record[2];
1141 unsigned Idx = 3;
1142 llvm::APInt Size = ReadAPInt(Record, Idx);
1143 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1144 }
1145
1146 case pch::TYPE_INCOMPLETE_ARRAY: {
1147 QualType ElementType = GetType(Record[0]);
1148 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1149 unsigned IndexTypeQuals = Record[2];
1150 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1151 }
1152
1153 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001154 QualType ElementType = GetType(Record[0]);
1155 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1156 unsigned IndexTypeQuals = Record[2];
Chris Lattner3ef21962009-04-27 05:58:23 +00001157 return Context.getVariableArrayType(ElementType, ReadTypeExpr(),
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001158 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001159 }
1160
1161 case pch::TYPE_VECTOR: {
1162 if (Record.size() != 2) {
1163 Error("Incorrect encoding of vector type in PCH file");
1164 return QualType();
1165 }
1166
1167 QualType ElementType = GetType(Record[0]);
1168 unsigned NumElements = Record[1];
1169 return Context.getVectorType(ElementType, NumElements);
1170 }
1171
1172 case pch::TYPE_EXT_VECTOR: {
1173 if (Record.size() != 2) {
1174 Error("Incorrect encoding of extended vector type in PCH file");
1175 return QualType();
1176 }
1177
1178 QualType ElementType = GetType(Record[0]);
1179 unsigned NumElements = Record[1];
1180 return Context.getExtVectorType(ElementType, NumElements);
1181 }
1182
1183 case pch::TYPE_FUNCTION_NO_PROTO: {
1184 if (Record.size() != 1) {
1185 Error("Incorrect encoding of no-proto function type");
1186 return QualType();
1187 }
1188 QualType ResultType = GetType(Record[0]);
1189 return Context.getFunctionNoProtoType(ResultType);
1190 }
1191
1192 case pch::TYPE_FUNCTION_PROTO: {
1193 QualType ResultType = GetType(Record[0]);
1194 unsigned Idx = 1;
1195 unsigned NumParams = Record[Idx++];
1196 llvm::SmallVector<QualType, 16> ParamTypes;
1197 for (unsigned I = 0; I != NumParams; ++I)
1198 ParamTypes.push_back(GetType(Record[Idx++]));
1199 bool isVariadic = Record[Idx++];
1200 unsigned Quals = Record[Idx++];
1201 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1202 isVariadic, Quals);
1203 }
1204
1205 case pch::TYPE_TYPEDEF:
1206 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1207 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1208
1209 case pch::TYPE_TYPEOF_EXPR:
Chris Lattner3ef21962009-04-27 05:58:23 +00001210 return Context.getTypeOfExprType(ReadTypeExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001211
1212 case pch::TYPE_TYPEOF: {
1213 if (Record.size() != 1) {
1214 Error("Incorrect encoding of typeof(type) in PCH file");
1215 return QualType();
1216 }
1217 QualType UnderlyingType = GetType(Record[0]);
1218 return Context.getTypeOfType(UnderlyingType);
1219 }
1220
1221 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001222 assert(Record.size() == 1 && "Incorrect encoding of record type");
1223 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001224
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001225 case pch::TYPE_ENUM:
1226 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1227 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1228
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001229 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00001230 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
1231 return Context.getObjCInterfaceType(
1232 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001233
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00001234 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
1235 unsigned Idx = 0;
1236 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
1237 unsigned NumProtos = Record[Idx++];
1238 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
1239 for (unsigned I = 0; I != NumProtos; ++I)
1240 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
1241 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
1242 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001243
Chris Lattner9b9f2352009-04-22 06:40:03 +00001244 case pch::TYPE_OBJC_QUALIFIED_ID: {
1245 unsigned Idx = 0;
1246 unsigned NumProtos = Record[Idx++];
1247 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
1248 for (unsigned I = 0; I != NumProtos; ++I)
1249 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
1250 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
1251 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001252 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001253 // Suppress a GCC warning
1254 return QualType();
1255}
1256
Douglas Gregorc34897d2009-04-09 22:27:44 +00001257
Douglas Gregorac8f2802009-04-10 17:25:41 +00001258QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001259 unsigned Quals = ID & 0x07;
1260 unsigned Index = ID >> 3;
1261
1262 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1263 QualType T;
1264 switch ((pch::PredefinedTypeIDs)Index) {
1265 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1266 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1267 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1268
1269 case pch::PREDEF_TYPE_CHAR_U_ID:
1270 case pch::PREDEF_TYPE_CHAR_S_ID:
1271 // FIXME: Check that the signedness of CharTy is correct!
1272 T = Context.CharTy;
1273 break;
1274
1275 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1276 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1277 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1278 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1279 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1280 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1281 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1282 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1283 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1284 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1285 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1286 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1287 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1288 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1289 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1290 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1291 }
1292
1293 assert(!T.isNull() && "Unknown predefined type");
1294 return T.getQualifiedType(Quals);
1295 }
1296
1297 Index -= pch::NUM_PREDEF_TYPE_IDS;
Douglas Gregore43f0972009-04-26 03:49:13 +00001298 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Douglas Gregor24a224c2009-04-25 18:35:21 +00001299 if (!TypesLoaded[Index])
1300 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001301
Douglas Gregor24a224c2009-04-25 18:35:21 +00001302 return QualType(TypesLoaded[Index], Quals);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001303}
1304
Douglas Gregorac8f2802009-04-10 17:25:41 +00001305Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001306 if (ID == 0)
1307 return 0;
1308
Douglas Gregor24a224c2009-04-25 18:35:21 +00001309 if (ID > DeclsLoaded.size()) {
1310 Error("Declaration ID out-of-range for PCH file");
1311 return 0;
1312 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001313
Douglas Gregor24a224c2009-04-25 18:35:21 +00001314 unsigned Index = ID - 1;
1315 if (!DeclsLoaded[Index])
1316 ReadDeclRecord(DeclOffsets[Index], Index);
1317
1318 return DeclsLoaded[Index];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001319}
1320
Chris Lattner77055f62009-04-27 05:46:25 +00001321/// \brief Resolve the offset of a statement into a statement.
1322///
1323/// This operation will read a new statement from the external
1324/// source each time it is called, and is meant to be used via a
1325/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1326Stmt *PCHReader::GetDeclStmt(uint64_t Offset) {
Chris Lattner3ef21962009-04-27 05:58:23 +00001327 // Since we know tha this statement is part of a decl, make sure to use the
1328 // decl cursor to read it.
1329 DeclsCursor.JumpToBit(Offset);
1330 return ReadStmt(DeclsCursor);
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00001331}
1332
Douglas Gregorc34897d2009-04-09 22:27:44 +00001333bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001334 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001335 assert(DC->hasExternalLexicalStorage() &&
1336 "DeclContext has no lexical decls in storage");
1337 uint64_t Offset = DeclContextOffsets[DC].first;
1338 assert(Offset && "DeclContext has no lexical decls in storage");
1339
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001340 // Keep track of where we are in the stream, then jump back there
1341 // after reading this context.
1342 SavedStreamPosition SavedPosition(Stream);
1343
Douglas Gregorc34897d2009-04-09 22:27:44 +00001344 // Load the record containing all of the declarations lexically in
1345 // this context.
1346 Stream.JumpToBit(Offset);
1347 RecordData Record;
1348 unsigned Code = Stream.ReadCode();
1349 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001350 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001351 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1352
1353 // Load all of the declaration IDs
1354 Decls.clear();
1355 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00001356 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001357 return false;
1358}
1359
1360bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1361 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1362 assert(DC->hasExternalVisibleStorage() &&
1363 "DeclContext has no visible decls in storage");
1364 uint64_t Offset = DeclContextOffsets[DC].second;
1365 assert(Offset && "DeclContext has no visible decls in storage");
1366
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001367 // Keep track of where we are in the stream, then jump back there
1368 // after reading this context.
1369 SavedStreamPosition SavedPosition(Stream);
1370
Douglas Gregorc34897d2009-04-09 22:27:44 +00001371 // Load the record containing all of the declarations visible in
1372 // this context.
1373 Stream.JumpToBit(Offset);
1374 RecordData Record;
1375 unsigned Code = Stream.ReadCode();
1376 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001377 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001378 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1379 if (Record.size() == 0)
1380 return false;
1381
1382 Decls.clear();
1383
1384 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001385 while (Idx < Record.size()) {
1386 Decls.push_back(VisibleDeclaration());
1387 Decls.back().Name = ReadDeclarationName(Record, Idx);
1388
Douglas Gregorc34897d2009-04-09 22:27:44 +00001389 unsigned Size = Record[Idx++];
1390 llvm::SmallVector<unsigned, 4> & LoadedDecls
1391 = Decls.back().Declarations;
1392 LoadedDecls.reserve(Size);
1393 for (unsigned I = 0; I < Size; ++I)
1394 LoadedDecls.push_back(Record[Idx++]);
1395 }
1396
Douglas Gregoraf136d92009-04-22 22:34:57 +00001397 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001398 return false;
1399}
1400
Douglas Gregor631f6c62009-04-14 00:24:19 +00001401void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00001402 this->Consumer = Consumer;
1403
Douglas Gregor631f6c62009-04-14 00:24:19 +00001404 if (!Consumer)
1405 return;
1406
1407 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1408 Decl *D = GetDecl(ExternalDefinitions[I]);
1409 DeclGroupRef DG(D);
1410 Consumer->HandleTopLevelDecl(DG);
1411 }
Douglas Gregorf93cfee2009-04-25 00:41:30 +00001412
1413 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
1414 DeclGroupRef DG(InterestingDecls[I]);
1415 Consumer->HandleTopLevelDecl(DG);
1416 }
Douglas Gregor631f6c62009-04-14 00:24:19 +00001417}
1418
Douglas Gregorc34897d2009-04-09 22:27:44 +00001419void PCHReader::PrintStats() {
1420 std::fprintf(stderr, "*** PCH Statistics:\n");
1421
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001422 unsigned NumTypesLoaded
1423 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
1424 (Type *)0);
1425 unsigned NumDeclsLoaded
1426 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
1427 (Decl *)0);
1428 unsigned NumIdentifiersLoaded
1429 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
1430 IdentifiersLoaded.end(),
1431 (IdentifierInfo *)0);
1432 unsigned NumSelectorsLoaded
1433 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
1434 SelectorsLoaded.end(),
1435 Selector());
Douglas Gregor9cf47422009-04-13 20:50:16 +00001436
Douglas Gregor24a224c2009-04-25 18:35:21 +00001437 if (!TypesLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00001438 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00001439 NumTypesLoaded, (unsigned)TypesLoaded.size(),
1440 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
1441 if (!DeclsLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00001442 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00001443 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
1444 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001445 if (!IdentifiersLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00001446 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001447 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
1448 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor2d711832009-04-25 17:48:32 +00001449 if (TotalNumSelectors)
1450 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
1451 NumSelectorsLoaded, TotalNumSelectors,
1452 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
1453 if (TotalNumStatements)
1454 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
1455 NumStatementsRead, TotalNumStatements,
1456 ((float)NumStatementsRead/TotalNumStatements * 100));
1457 if (TotalNumMacros)
1458 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
1459 NumMacrosRead, TotalNumMacros,
1460 ((float)NumMacrosRead/TotalNumMacros * 100));
1461 if (TotalLexicalDeclContexts)
1462 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
1463 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
1464 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
1465 * 100));
1466 if (TotalVisibleDeclContexts)
1467 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
1468 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
1469 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
1470 * 100));
1471 if (TotalSelectorsInMethodPool) {
1472 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
1473 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
1474 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
1475 * 100));
1476 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
1477 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001478 std::fprintf(stderr, "\n");
1479}
1480
Douglas Gregorc713da92009-04-21 22:25:48 +00001481void PCHReader::InitializeSema(Sema &S) {
1482 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001483 S.ExternalSource = this;
1484
Douglas Gregor2554cf22009-04-22 21:15:06 +00001485 // Makes sure any declarations that were deserialized "too early"
1486 // still get added to the identifier's declaration chains.
1487 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
1488 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
1489 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00001490 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00001491 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001492
1493 // If there were any tentative definitions, deserialize them and add
1494 // them to Sema's table of tentative definitions.
1495 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
1496 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
1497 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
1498 }
Douglas Gregor062d9482009-04-22 22:18:58 +00001499
1500 // If there were any locally-scoped external declarations,
1501 // deserialize them and add them to Sema's table of locally-scoped
1502 // external declarations.
1503 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
1504 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
1505 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
1506 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001507}
1508
1509IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
1510 // Try to find this name within our on-disk hash table
1511 PCHIdentifierLookupTable *IdTable
1512 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1513 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
1514 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
1515 if (Pos == IdTable->end())
1516 return 0;
1517
1518 // Dereferencing the iterator has the effect of building the
1519 // IdentifierInfo node and populating it with the various
1520 // declarations it needs.
1521 return *Pos;
1522}
1523
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001524std::pair<ObjCMethodList, ObjCMethodList>
1525PCHReader::ReadMethodPool(Selector Sel) {
1526 if (!MethodPoolLookupTable)
1527 return std::pair<ObjCMethodList, ObjCMethodList>();
1528
1529 // Try to find this selector within our on-disk hash table.
1530 PCHMethodPoolLookupTable *PoolTable
1531 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
1532 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor2d711832009-04-25 17:48:32 +00001533 if (Pos == PoolTable->end()) {
1534 ++NumMethodPoolMisses;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001535 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor2d711832009-04-25 17:48:32 +00001536 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001537
Douglas Gregor2d711832009-04-25 17:48:32 +00001538 ++NumMethodPoolSelectorsRead;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001539 return *Pos;
1540}
1541
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001542void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregorc713da92009-04-21 22:25:48 +00001543 assert(ID && "Non-zero identifier ID required");
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001544 assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range");
1545 IdentifiersLoaded[ID - 1] = II;
Douglas Gregorc713da92009-04-21 22:25:48 +00001546}
1547
Chris Lattner29241862009-04-11 21:15:38 +00001548IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001549 if (ID == 0)
1550 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001551
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001552 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001553 Error("No identifier table in PCH file");
1554 return 0;
1555 }
Chris Lattner29241862009-04-11 21:15:38 +00001556
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001557 if (!IdentifiersLoaded[ID - 1]) {
1558 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00001559 const char *Str = IdentifierTableData + Offset;
Douglas Gregor85c4a872009-04-25 21:04:17 +00001560
1561 // If there is an identifier lookup table, but the offset of this
1562 // string is after the identifier table itself, then we know that
1563 // this string is not in the on-disk hash table. Therefore,
1564 // disable lookup into the hash table when looking for this
1565 // identifier.
1566 PCHIdentifierLookupTable *IdTable
1567 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00001568 if (!IdTable ||
1569 Offset >= uint32_t(IdTable->getBuckets() - IdTable->getBase())) {
1570 // Turn off lookup into the on-disk hash table. We know that
1571 // this identifier is not there.
1572 if (IdTable)
1573 PP.getIdentifierTable().setExternalIdentifierLookup(0);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001574
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00001575 // All of the strings in the PCH file are preceded by a 16-bit
1576 // length. Extract that 16-bit length to avoid having to execute
1577 // strlen().
1578 const char *StrLenPtr = Str - 2;
1579 unsigned StrLen = (((unsigned) StrLenPtr[0])
1580 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
1581 IdentifiersLoaded[ID - 1] = &Context.Idents.get(Str, Str + StrLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001582
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00001583 // Turn on lookup into the on-disk hash table, if we have an
1584 // on-disk hash table.
1585 if (IdTable)
1586 PP.getIdentifierTable().setExternalIdentifierLookup(this);
1587 } else {
1588 // The identifier is a key in our on-disk hash table. Since we
1589 // know where the hash table entry starts, just read in this
1590 // (key, value) pair.
1591 PCHIdentifierLookupTrait Trait(const_cast<PCHReader &>(*this));
1592 const unsigned char *Pos = (const unsigned char *)Str - 4;
1593 std::pair<unsigned, unsigned> KeyDataLengths
1594 = Trait.ReadKeyDataLength(Pos);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001595
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00001596 PCHIdentifierLookupTrait::internal_key_type InternalKey
1597 = Trait.ReadKey(Pos, KeyDataLengths.first);
1598 Pos = (const unsigned char *)Str + KeyDataLengths.first;
1599 IdentifiersLoaded[ID - 1] = Trait.ReadData(InternalKey, Pos,
1600 KeyDataLengths.second);
1601 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001602 }
Chris Lattner29241862009-04-11 21:15:38 +00001603
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001604 return IdentifiersLoaded[ID - 1];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001605}
1606
Steve Naroff9e84d782009-04-23 10:39:46 +00001607Selector PCHReader::DecodeSelector(unsigned ID) {
1608 if (ID == 0)
1609 return Selector();
1610
Douglas Gregor2d711832009-04-25 17:48:32 +00001611 if (!MethodPoolLookupTableData) {
Steve Naroff9e84d782009-04-23 10:39:46 +00001612 Error("No selector table in PCH file");
1613 return Selector();
1614 }
Douglas Gregor2d711832009-04-25 17:48:32 +00001615
1616 if (ID > TotalNumSelectors) {
Steve Naroff9e84d782009-04-23 10:39:46 +00001617 Error("Selector ID out of range");
1618 return Selector();
1619 }
Douglas Gregor2d711832009-04-25 17:48:32 +00001620
1621 unsigned Index = ID - 1;
1622 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
1623 // Load this selector from the selector table.
1624 // FIXME: endianness portability issues with SelectorOffsets table
1625 PCHMethodPoolLookupTrait Trait(*this);
1626 SelectorsLoaded[Index]
1627 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
1628 }
1629
1630 return SelectorsLoaded[Index];
Steve Naroff9e84d782009-04-23 10:39:46 +00001631}
1632
Douglas Gregorc34897d2009-04-09 22:27:44 +00001633DeclarationName
1634PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1635 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1636 switch (Kind) {
1637 case DeclarationName::Identifier:
1638 return DeclarationName(GetIdentifierInfo(Record, Idx));
1639
1640 case DeclarationName::ObjCZeroArgSelector:
1641 case DeclarationName::ObjCOneArgSelector:
1642 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00001643 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001644
1645 case DeclarationName::CXXConstructorName:
1646 return Context.DeclarationNames.getCXXConstructorName(
1647 GetType(Record[Idx++]));
1648
1649 case DeclarationName::CXXDestructorName:
1650 return Context.DeclarationNames.getCXXDestructorName(
1651 GetType(Record[Idx++]));
1652
1653 case DeclarationName::CXXConversionFunctionName:
1654 return Context.DeclarationNames.getCXXConversionFunctionName(
1655 GetType(Record[Idx++]));
1656
1657 case DeclarationName::CXXOperatorName:
1658 return Context.DeclarationNames.getCXXOperatorName(
1659 (OverloadedOperatorKind)Record[Idx++]);
1660
1661 case DeclarationName::CXXUsingDirective:
1662 return DeclarationName::getUsingDirectiveName();
1663 }
1664
1665 // Required to silence GCC warning
1666 return DeclarationName();
1667}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001668
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001669/// \brief Read an integral value
1670llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1671 unsigned BitWidth = Record[Idx++];
1672 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1673 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1674 Idx += NumWords;
1675 return Result;
1676}
1677
1678/// \brief Read a signed integral value
1679llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1680 bool isUnsigned = Record[Idx++];
1681 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1682}
1683
Douglas Gregore2f37202009-04-14 21:55:33 +00001684/// \brief Read a floating-point value
1685llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00001686 return llvm::APFloat(ReadAPInt(Record, Idx));
1687}
1688
Douglas Gregor1c507882009-04-15 21:30:51 +00001689// \brief Read a string
1690std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1691 unsigned Len = Record[Idx++];
1692 std::string Result(&Record[Idx], &Record[Idx] + Len);
1693 Idx += Len;
1694 return Result;
1695}
1696
Douglas Gregor179cfb12009-04-10 20:39:37 +00001697DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001698 return Diag(SourceLocation(), DiagID);
1699}
1700
1701DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
1702 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00001703 Context.getSourceManager()),
1704 DiagID);
1705}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001706
Douglas Gregorc713da92009-04-21 22:25:48 +00001707/// \brief Retrieve the identifier table associated with the
1708/// preprocessor.
1709IdentifierTable &PCHReader::getIdentifierTable() {
1710 return PP.getIdentifierTable();
1711}
1712
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001713/// \brief Record that the given ID maps to the given switch-case
1714/// statement.
1715void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
1716 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
1717 SwitchCaseStmts[ID] = SC;
1718}
1719
1720/// \brief Retrieve the switch-case statement with the given ID.
1721SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
1722 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
1723 return SwitchCaseStmts[ID];
1724}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00001725
1726/// \brief Record that the given label statement has been
1727/// deserialized and has the given ID.
1728void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
1729 assert(LabelStmts.find(ID) == LabelStmts.end() &&
1730 "Deserialized label twice");
1731 LabelStmts[ID] = S;
1732
1733 // If we've already seen any goto statements that point to this
1734 // label, resolve them now.
1735 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
1736 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
1737 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
1738 Goto->second->setLabel(S);
1739 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001740
1741 // If we've already seen any address-label statements that point to
1742 // this label, resolve them now.
1743 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
1744 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
1745 = UnresolvedAddrLabelExprs.equal_range(ID);
1746 for (AddrLabelIter AddrLabel = AddrLabels.first;
1747 AddrLabel != AddrLabels.second; ++AddrLabel)
1748 AddrLabel->second->setLabel(S);
1749 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00001750}
1751
1752/// \brief Set the label of the given statement to the label
1753/// identified by ID.
1754///
1755/// Depending on the order in which the label and other statements
1756/// referencing that label occur, this operation may complete
1757/// immediately (updating the statement) or it may queue the
1758/// statement to be back-patched later.
1759void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
1760 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
1761 if (Label != LabelStmts.end()) {
1762 // We've already seen this label, so set the label of the goto and
1763 // we're done.
1764 S->setLabel(Label->second);
1765 } else {
1766 // We haven't seen this label yet, so add this goto to the set of
1767 // unresolved goto statements.
1768 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
1769 }
1770}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001771
1772/// \brief Set the label of the given expression to the label
1773/// identified by ID.
1774///
1775/// Depending on the order in which the label and other statements
1776/// referencing that label occur, this operation may complete
1777/// immediately (updating the statement) or it may queue the
1778/// statement to be back-patched later.
1779void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
1780 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
1781 if (Label != LabelStmts.end()) {
1782 // We've already seen this label, so set the label of the
1783 // label-address expression and we're done.
1784 S->setLabel(Label->second);
1785 } else {
1786 // We haven't seen this label yet, so add this label-address
1787 // expression to the set of unresolved label-address expressions.
1788 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
1789 }
1790}