Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | 0954794 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/PCHReader.h" |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 16 | #include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 21 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | cda68f2 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 23 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 24 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 27 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 29 | #include "llvm/Bitcode/BitstreamReader.h" |
| 30 | #include "llvm/Support/Compiler.h" |
| 31 | #include "llvm/Support/MemoryBuffer.h" |
| 32 | #include <algorithm> |
| 33 | #include <cstdio> |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 37 | // PCH reader implementation |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Chris Lattner | 0954794 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 40 | PCHReader::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 | |
| 50 | PCHReader::~PCHReader() {} |
| 51 | |
Chris Lattner | 3ef2196 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 52 | Expr *PCHReader::ReadDeclExpr() { |
| 53 | return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor)); |
| 54 | } |
| 55 | |
| 56 | Expr *PCHReader::ReadTypeExpr() { |
Chris Lattner | 3282c39 | 2009-04-27 05:41:06 +0000 | [diff] [blame] | 57 | return dyn_cast_or_null<Expr>(ReadStmt(Stream)); |
Chris Lattner | 0954794 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 61 | namespace { |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 62 | class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait { |
| 63 | PCHReader &Reader; |
| 64 | |
| 65 | public: |
| 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 Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 101 | internal_key_type ReadKey(const unsigned char* d, unsigned) { |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 102 | 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. |
| 166 | typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait> |
| 167 | PCHMethodPoolLookupTable; |
| 168 | |
| 169 | namespace { |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 170 | class 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 | |
| 178 | public: |
| 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 Gregor | 4bb2488 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 205 | unsigned DataLen = ReadUnalignedLE16(d); |
Douglas Gregor | 85c4a87 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 206 | unsigned KeyLen = ReadUnalignedLE16(d); |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 207 | 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 Gregor | 2554cf2 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 220 | uint32_t Bits = ReadUnalignedLE32(d); |
Douglas Gregor | da38c6c | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 221 | 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 Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 234 | pch::IdentID ID = ReadUnalignedLE32(d); |
Douglas Gregor | da38c6c | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 235 | assert(Bits == 0 && "Extra bits in the identifier?"); |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 236 | 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 Gregor | 4bb2488 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 242 | II = &Reader.getIdentifierTable().CreateIdentifierInfo( |
| 243 | k.first, k.first + k.second); |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 244 | Reader.SetIdentifierInfo(ID, II); |
| 245 | |
Douglas Gregor | da38c6c | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 246 | // 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 Gregor | e0ad2dd | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 260 | // 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 Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 267 | |
| 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 Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 273 | 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 Gregor | 2554cf2 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 283 | Reader.PreloadedDecls.push_back(D); |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 284 | } |
| 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. |
| 296 | typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait> |
| 297 | PCHIdentifierLookupTable; |
| 298 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 299 | // FIXME: use the diagnostics machinery |
| 300 | static bool Error(const char *Str) { |
| 301 | std::fprintf(stderr, "%s\n", Str); |
| 302 | return true; |
| 303 | } |
| 304 | |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 305 | /// \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. |
| 322 | bool 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 Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 380 | /// \brief Read the line table in the source manager block. |
| 381 | /// \returns true if ther was an error. |
| 382 | static 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 Gregor | 183ad60 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 388 | std::map<int, int> FileIDs; |
| 389 | for (int I = 0, N = Record[Idx++]; I != N; ++I) { |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 390 | // Extract the file name |
| 391 | unsigned FilenameLen = Record[Idx++]; |
| 392 | std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); |
| 393 | Idx += FilenameLen; |
Douglas Gregor | 183ad60 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 394 | FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(), |
| 395 | Filename.size()); |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // Parse the line entries |
| 399 | std::vector<LineEntry> Entries; |
| 400 | while (Idx < Record.size()) { |
Douglas Gregor | 183ad60 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 401 | int FID = FileIDs[Record[Idx++]]; |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 402 | |
| 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 Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 423 | /// \brief Read the source manager block |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 424 | PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() { |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 425 | using namespace SrcMgr; |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 426 | if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) { |
| 427 | Error("Malformed source manager block record"); |
| 428 | return Failure; |
| 429 | } |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 430 | |
| 431 | SourceManager &SourceMgr = Context.getSourceManager(); |
| 432 | RecordData Record; |
Douglas Gregor | f6e1fb2 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 433 | unsigned NumHeaderInfos = 0; |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 434 | while (true) { |
| 435 | unsigned Code = Stream.ReadCode(); |
| 436 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 437 | if (Stream.ReadBlockEnd()) { |
| 438 | Error("Error at end of Source Manager block"); |
| 439 | return Failure; |
| 440 | } |
| 441 | |
| 442 | return Success; |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 446 | // No known subblocks, always skip them. |
| 447 | Stream.ReadSubBlockID(); |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 448 | if (Stream.SkipBlock()) { |
| 449 | Error("Malformed block record"); |
| 450 | return Failure; |
| 451 | } |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 452 | 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 Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 475 | 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 Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 481 | 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 Gregor | 3c8ff3e | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 490 | (void)RecCode; |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 491 | 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 Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 500 | 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 Gregor | 364e580 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 510 | Record[4]); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 511 | break; |
| 512 | } |
| 513 | |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 514 | case pch::SM_LINE_TABLE: |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 515 | if (ParseLineTable(SourceMgr, Record)) |
| 516 | return Failure; |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 517 | break; |
Douglas Gregor | f6e1fb2 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 518 | |
| 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 Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Chris Lattner | 4fc71eb | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 532 | /// 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. |
| 535 | bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, |
| 536 | unsigned BlockID) { |
| 537 | if (Cursor.EnterSubBlock(BlockID)) { |
| 538 | Error("Malformed block record"); |
| 539 | return Failure; |
| 540 | } |
| 541 | |
Chris Lattner | 4fc71eb | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 542 | 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 Gregor | e0ad2dd | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 552 | void 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 Naroff | cda68f2 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 561 | |
Douglas Gregor | e0ad2dd | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 562 | 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 Gregor | e0ad2dd | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 588 | 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 Naroff | cda68f2 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 650 | } |
Douglas Gregor | e0ad2dd | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 654 | PCHReader::PCHReadResult |
Douglas Gregor | f6e1fb2 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 655 | PCHReader::ReadPCHBlock() { |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 656 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
| 657 | Error("Malformed block record"); |
| 658 | return Failure; |
| 659 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 660 | |
| 661 | // Read all of the records and blocks for the PCH file. |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 662 | RecordData Record; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 663 | while (!Stream.AtEndOfStream()) { |
| 664 | unsigned Code = Stream.ReadCode(); |
| 665 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 666 | if (Stream.ReadBlockEnd()) { |
| 667 | Error("Error at end of module block"); |
| 668 | return Failure; |
| 669 | } |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 671 | return Success; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 675 | switch (Stream.ReadSubBlockID()) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 676 | case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded) |
| 677 | default: // Skip unknown content. |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 678 | if (Stream.SkipBlock()) { |
| 679 | Error("Malformed block record"); |
| 680 | return Failure; |
| 681 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 682 | break; |
| 683 | |
Chris Lattner | 4fc71eb | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 684 | 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 Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 698 | case pch::PREPROCESSOR_BLOCK_ID: |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 699 | if (Stream.SkipBlock()) { |
| 700 | Error("Malformed block record"); |
| 701 | return Failure; |
| 702 | } |
| 703 | break; |
Steve Naroff | 9e84d78 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 704 | |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 705 | case pch::SOURCE_MANAGER_BLOCK_ID: |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 706 | switch (ReadSourceManagerBlock()) { |
| 707 | case Success: |
| 708 | break; |
| 709 | |
| 710 | case Failure: |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 711 | Error("Malformed source manager block"); |
| 712 | return Failure; |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 713 | |
| 714 | case IgnorePCH: |
| 715 | return IgnorePCH; |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 716 | } |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 717 | break; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 718 | } |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 719 | 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 Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 729 | const char *BlobStart = 0; |
| 730 | unsigned BlobLen = 0; |
| 731 | switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, |
| 732 | &BlobStart, &BlobLen)) { |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 733 | default: // Default behavior: ignore. |
| 734 | break; |
| 735 | |
| 736 | case pch::TYPE_OFFSET: |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 737 | if (!TypesLoaded.empty()) { |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 738 | Error("Duplicate TYPE_OFFSET record in PCH file"); |
| 739 | return Failure; |
| 740 | } |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 741 | TypeOffsets = (const uint64_t *)BlobStart; |
| 742 | TypesLoaded.resize(Record[0]); |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 743 | break; |
| 744 | |
| 745 | case pch::DECL_OFFSET: |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 746 | if (!DeclsLoaded.empty()) { |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 747 | Error("Duplicate DECL_OFFSET record in PCH file"); |
| 748 | return Failure; |
| 749 | } |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 750 | DeclOffsets = (const uint64_t *)BlobStart; |
| 751 | DeclsLoaded.resize(Record[0]); |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 752 | break; |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 753 | |
| 754 | case pch::LANGUAGE_OPTIONS: |
| 755 | if (ParseLanguageOptions(Record)) |
| 756 | return IgnorePCH; |
| 757 | break; |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 758 | |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 759 | case pch::TARGET_TRIPLE: { |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 760 | 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 Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 768 | } |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 769 | |
| 770 | case pch::IDENTIFIER_TABLE: |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 771 | IdentifierTableData = BlobStart; |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 772 | if (Record[0]) { |
| 773 | IdentifierLookupTable |
| 774 | = PCHIdentifierLookupTable::Create( |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 775 | (const unsigned char *)IdentifierTableData + Record[0], |
| 776 | (const unsigned char *)IdentifierTableData, |
| 777 | PCHIdentifierLookupTrait(*this)); |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 778 | PP.getIdentifierTable().setExternalIdentifierLookup(this); |
| 779 | } |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 780 | break; |
| 781 | |
| 782 | case pch::IDENTIFIER_OFFSET: |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 783 | if (!IdentifiersLoaded.empty()) { |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 784 | Error("Duplicate IDENTIFIER_OFFSET record in PCH file"); |
| 785 | return Failure; |
| 786 | } |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 787 | IdentifierOffsets = (const uint32_t *)BlobStart; |
| 788 | IdentifiersLoaded.resize(Record[0]); |
Douglas Gregor | eccb51d | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 789 | PP.getHeaderSearchInfo().SetExternalLookup(this); |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 790 | break; |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 791 | |
| 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 Gregor | 456e095 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 799 | |
Douglas Gregor | e01ad44 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 800 | case pch::SPECIAL_TYPES: |
| 801 | SpecialTypes.swap(Record); |
| 802 | break; |
| 803 | |
Douglas Gregor | 456e095 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 804 | case pch::STATISTICS: |
| 805 | TotalNumStatements = Record[0]; |
Douglas Gregor | e0ad2dd | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 806 | TotalNumMacros = Record[1]; |
Douglas Gregor | af136d9 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 807 | TotalLexicalDeclContexts = Record[2]; |
| 808 | TotalVisibleDeclContexts = Record[3]; |
Douglas Gregor | 456e095 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 809 | break; |
Douglas Gregor | 77b2cd5 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 810 | 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 Gregor | 062d948 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 817 | |
| 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 Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 825 | |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 826 | case pch::SELECTOR_OFFSETS: |
| 827 | SelectorOffsets = (const uint32_t *)BlobStart; |
| 828 | TotalNumSelectors = Record[0]; |
| 829 | SelectorsLoaded.resize(TotalNumSelectors); |
| 830 | break; |
| 831 | |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 832 | case pch::METHOD_POOL: |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 833 | MethodPoolLookupTableData = (const unsigned char *)BlobStart; |
| 834 | if (Record[0]) |
| 835 | MethodPoolLookupTable |
| 836 | = PCHMethodPoolLookupTable::Create( |
| 837 | MethodPoolLookupTableData + Record[0], |
| 838 | MethodPoolLookupTableData, |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 839 | PCHMethodPoolLookupTrait(*this)); |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 840 | TotalSelectorsInMethodPool = Record[1]; |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 841 | break; |
Douglas Gregor | f6e1fb2 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 842 | |
| 843 | case pch::PP_COUNTER_VALUE: |
| 844 | if (!Record.empty()) |
| 845 | PP.setCounterValue(Record[0]); |
| 846 | break; |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 847 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 848 | } |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 849 | Error("Premature end of bitstream"); |
| 850 | return Failure; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 853 | PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 854 | // Set the PCH file name. |
| 855 | this->FileName = FileName; |
| 856 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 857 | // Open the PCH file. |
| 858 | std::string ErrStr; |
| 859 | Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr)); |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 860 | if (!Buffer) { |
| 861 | Error(ErrStr.c_str()); |
| 862 | return IgnorePCH; |
| 863 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 864 | |
| 865 | // Initialize the stream |
Chris Lattner | 587788a | 2009-04-26 20:59:20 +0000 | [diff] [blame] | 866 | StreamFile.init((const unsigned char *)Buffer->getBufferStart(), |
| 867 | (const unsigned char *)Buffer->getBufferEnd()); |
| 868 | Stream.init(StreamFile); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 869 | |
| 870 | // Sniff for the signature. |
| 871 | if (Stream.Read(8) != 'C' || |
| 872 | Stream.Read(8) != 'P' || |
| 873 | Stream.Read(8) != 'C' || |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 874 | Stream.Read(8) != 'H') { |
| 875 | Error("Not a PCH file"); |
| 876 | return IgnorePCH; |
| 877 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 879 | while (!Stream.AtEndOfStream()) { |
| 880 | unsigned Code = Stream.ReadCode(); |
| 881 | |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 882 | if (Code != llvm::bitc::ENTER_SUBBLOCK) { |
| 883 | Error("Invalid record at top-level"); |
| 884 | return Failure; |
| 885 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 886 | |
| 887 | unsigned BlockID = Stream.ReadSubBlockID(); |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 889 | // We only know the PCH subblock ID. |
| 890 | switch (BlockID) { |
| 891 | case llvm::bitc::BLOCKINFO_BLOCK_ID: |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 892 | if (Stream.ReadBlockInfoBlock()) { |
| 893 | Error("Malformed BlockInfoBlock"); |
| 894 | return Failure; |
| 895 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 896 | break; |
| 897 | case pch::PCH_BLOCK_ID: |
Douglas Gregor | f6e1fb2 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 898 | switch (ReadPCHBlock()) { |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 899 | case Success: |
| 900 | break; |
| 901 | |
| 902 | case Failure: |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 903 | return Failure; |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 904 | |
| 905 | case IgnorePCH: |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 906 | // 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 Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 909 | return IgnorePCH; |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 910 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 911 | break; |
| 912 | default: |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 913 | if (Stream.SkipBlock()) { |
| 914 | Error("Malformed block record"); |
| 915 | return Failure; |
| 916 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | // Load the translation unit declaration |
| 922 | ReadDeclRecord(DeclOffsets[0], 0); |
| 923 | |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 924 | // 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 Gregor | e01ad44 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 957 | // Load the special types. |
| 958 | Context.setBuiltinVaListType( |
| 959 | GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST])); |
Douglas Gregor | bb21d4b | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 960 | 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 Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 973 | |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 974 | return Success; |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 977 | /// \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. |
| 991 | bool 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 Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1066 | /// \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. |
| 1071 | QualType PCHReader::ReadTypeRecord(uint64_t Offset) { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1072 | // Keep track of where we are in the stream, then jump back there |
| 1073 | // after reading this type. |
| 1074 | SavedStreamPosition SavedPosition(Stream); |
| 1075 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1076 | Stream.JumpToBit(Offset); |
| 1077 | RecordData Record; |
| 1078 | unsigned Code = Stream.ReadCode(); |
| 1079 | switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) { |
Douglas Gregor | bdd4ba5 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1080 | 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 Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1095 | 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 Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1137 | 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 Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1154 | QualType ElementType = GetType(Record[0]); |
| 1155 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1156 | unsigned IndexTypeQuals = Record[2]; |
Chris Lattner | 3ef2196 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 1157 | return Context.getVariableArrayType(ElementType, ReadTypeExpr(), |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1158 | ASM, IndexTypeQuals); |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1159 | } |
| 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 Lattner | 3ef2196 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 1210 | return Context.getTypeOfExprType(ReadTypeExpr()); |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1211 | |
| 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 Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1222 | assert(Record.size() == 1 && "Incorrect encoding of record type"); |
| 1223 | return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0]))); |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1225 | 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 Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1229 | case pch::TYPE_OBJC_INTERFACE: |
Chris Lattner | 80f83c6 | 2009-04-22 05:57:30 +0000 | [diff] [blame] | 1230 | assert(Record.size() == 1 && "Incorrect encoding of objc interface type"); |
| 1231 | return Context.getObjCInterfaceType( |
| 1232 | cast<ObjCInterfaceDecl>(GetDecl(Record[0]))); |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | bab2c0f | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 1234 | 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 Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | 9b9f235 | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 1244 | 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 Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1252 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1253 | // Suppress a GCC warning |
| 1254 | return QualType(); |
| 1255 | } |
| 1256 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1257 | |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1258 | QualType PCHReader::GetType(pch::TypeID ID) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1259 | 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 Gregor | e43f097 | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 1298 | assert(Index < TypesLoaded.size() && "Type index out-of-range"); |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1299 | if (!TypesLoaded[Index]) |
| 1300 | TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1301 | |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1302 | return QualType(TypesLoaded[Index], Quals); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1305 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1306 | if (ID == 0) |
| 1307 | return 0; |
| 1308 | |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1309 | if (ID > DeclsLoaded.size()) { |
| 1310 | Error("Declaration ID out-of-range for PCH file"); |
| 1311 | return 0; |
| 1312 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1314 | unsigned Index = ID - 1; |
| 1315 | if (!DeclsLoaded[Index]) |
| 1316 | ReadDeclRecord(DeclOffsets[Index], Index); |
| 1317 | |
| 1318 | return DeclsLoaded[Index]; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Chris Lattner | 77055f6 | 2009-04-27 05:46:25 +0000 | [diff] [blame] | 1321 | /// \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). |
| 1326 | Stmt *PCHReader::GetDeclStmt(uint64_t Offset) { |
Chris Lattner | 3ef2196 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 1327 | // 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 Gregor | 3b9a7c8 | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1333 | bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1334 | llvm::SmallVectorImpl<pch::DeclID> &Decls) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1335 | 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 Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1340 | // Keep track of where we are in the stream, then jump back there |
| 1341 | // after reading this context. |
| 1342 | SavedStreamPosition SavedPosition(Stream); |
| 1343 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1344 | // 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 Gregor | 3c8ff3e | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 1350 | (void)RecCode; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1351 | 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 Gregor | af136d9 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1356 | ++NumLexicalDeclContextsRead; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1357 | return false; |
| 1358 | } |
| 1359 | |
| 1360 | bool 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 Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1367 | // Keep track of where we are in the stream, then jump back there |
| 1368 | // after reading this context. |
| 1369 | SavedStreamPosition SavedPosition(Stream); |
| 1370 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1371 | // 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 Gregor | 3c8ff3e | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 1377 | (void)RecCode; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1378 | 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 Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1385 | while (Idx < Record.size()) { |
| 1386 | Decls.push_back(VisibleDeclaration()); |
| 1387 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 1388 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1389 | 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 Gregor | af136d9 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1397 | ++NumVisibleDeclContextsRead; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1398 | return false; |
| 1399 | } |
| 1400 | |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1401 | void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { |
Douglas Gregor | 405b643 | 2009-04-22 19:09:20 +0000 | [diff] [blame] | 1402 | this->Consumer = Consumer; |
| 1403 | |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1404 | 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 Gregor | f93cfee | 2009-04-25 00:41:30 +0000 | [diff] [blame] | 1412 | |
| 1413 | for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) { |
| 1414 | DeclGroupRef DG(InterestingDecls[I]); |
| 1415 | Consumer->HandleTopLevelDecl(DG); |
| 1416 | } |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1419 | void PCHReader::PrintStats() { |
| 1420 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 1421 | |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1422 | 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 Gregor | 9cf4742 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 1436 | |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1437 | if (!TypesLoaded.empty()) |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1438 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1439 | NumTypesLoaded, (unsigned)TypesLoaded.size(), |
| 1440 | ((float)NumTypesLoaded/TypesLoaded.size() * 100)); |
| 1441 | if (!DeclsLoaded.empty()) |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1442 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
Douglas Gregor | 24a224c | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1443 | NumDeclsLoaded, (unsigned)DeclsLoaded.size(), |
| 1444 | ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1445 | if (!IdentifiersLoaded.empty()) |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1446 | std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1447 | NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), |
| 1448 | ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1449 | 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 Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1478 | std::fprintf(stderr, "\n"); |
| 1479 | } |
| 1480 | |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1481 | void PCHReader::InitializeSema(Sema &S) { |
| 1482 | SemaObj = &S; |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1483 | S.ExternalSource = this; |
| 1484 | |
Douglas Gregor | 2554cf2 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 1485 | // 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 Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1490 | } |
Douglas Gregor | 2554cf2 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 1491 | PreloadedDecls.clear(); |
Douglas Gregor | 77b2cd5 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1492 | |
| 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 Gregor | 062d948 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1499 | |
| 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 Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | IdentifierInfo* 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 Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1524 | std::pair<ObjCMethodList, ObjCMethodList> |
| 1525 | PCHReader::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 Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1533 | if (Pos == PoolTable->end()) { |
| 1534 | ++NumMethodPoolMisses; |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1535 | return std::pair<ObjCMethodList, ObjCMethodList>();; |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1536 | } |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1537 | |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1538 | ++NumMethodPoolSelectorsRead; |
Douglas Gregor | c3221aa | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1539 | return *Pos; |
| 1540 | } |
| 1541 | |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1542 | void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1543 | assert(ID && "Non-zero identifier ID required"); |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1544 | assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range"); |
| 1545 | IdentifiersLoaded[ID - 1] = II; |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1548 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1549 | if (ID == 0) |
| 1550 | return 0; |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1551 | |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1552 | if (!IdentifierTableData || IdentifiersLoaded.empty()) { |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1553 | Error("No identifier table in PCH file"); |
| 1554 | return 0; |
| 1555 | } |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1556 | |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1557 | if (!IdentifiersLoaded[ID - 1]) { |
| 1558 | uint32_t Offset = IdentifierOffsets[ID - 1]; |
Douglas Gregor | 4d7a6e4 | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1559 | const char *Str = IdentifierTableData + Offset; |
Douglas Gregor | 85c4a87 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1560 | |
| 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 Gregor | 4d7a6e4 | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1568 | 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 Gregor | 85c4a87 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1574 | |
Douglas Gregor | 4d7a6e4 | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1575 | // 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 Gregor | 85c4a87 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1582 | |
Douglas Gregor | 4d7a6e4 | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1583 | // 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 Gregor | 85c4a87 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1595 | |
Douglas Gregor | 4d7a6e4 | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1596 | 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 Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1602 | } |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1603 | |
Douglas Gregor | de44c9f | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1604 | return IdentifiersLoaded[ID - 1]; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Steve Naroff | 9e84d78 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1607 | Selector PCHReader::DecodeSelector(unsigned ID) { |
| 1608 | if (ID == 0) |
| 1609 | return Selector(); |
| 1610 | |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1611 | if (!MethodPoolLookupTableData) { |
Steve Naroff | 9e84d78 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1612 | Error("No selector table in PCH file"); |
| 1613 | return Selector(); |
| 1614 | } |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (ID > TotalNumSelectors) { |
Steve Naroff | 9e84d78 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1617 | Error("Selector ID out of range"); |
| 1618 | return Selector(); |
| 1619 | } |
Douglas Gregor | 2d71183 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1620 | |
| 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 Naroff | 9e84d78 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1633 | DeclarationName |
| 1634 | PCHReader::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 Naroff | 104956f | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 1643 | return DeclarationName(GetSelector(Record, Idx)); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1644 | |
| 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 Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1668 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1669 | /// \brief Read an integral value |
| 1670 | llvm::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 |
| 1679 | llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { |
| 1680 | bool isUnsigned = Record[Idx++]; |
| 1681 | return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); |
| 1682 | } |
| 1683 | |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1684 | /// \brief Read a floating-point value |
| 1685 | llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1686 | return llvm::APFloat(ReadAPInt(Record, Idx)); |
| 1687 | } |
| 1688 | |
Douglas Gregor | 1c50788 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1689 | // \brief Read a string |
| 1690 | std::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 Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1697 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1698 | return Diag(SourceLocation(), DiagID); |
| 1699 | } |
| 1700 | |
| 1701 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
| 1702 | return PP.getDiagnostics().Report(FullSourceLoc(Loc, |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1703 | Context.getSourceManager()), |
| 1704 | DiagID); |
| 1705 | } |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 1706 | |
Douglas Gregor | c713da9 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1707 | /// \brief Retrieve the identifier table associated with the |
| 1708 | /// preprocessor. |
| 1709 | IdentifierTable &PCHReader::getIdentifierTable() { |
| 1710 | return PP.getIdentifierTable(); |
| 1711 | } |
| 1712 | |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 1713 | /// \brief Record that the given ID maps to the given switch-case |
| 1714 | /// statement. |
| 1715 | void 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. |
| 1721 | SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) { |
| 1722 | assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); |
| 1723 | return SwitchCaseStmts[ID]; |
| 1724 | } |
Douglas Gregor | 6e411bf | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 1725 | |
| 1726 | /// \brief Record that the given label statement has been |
| 1727 | /// deserialized and has the given ID. |
| 1728 | void 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 Gregor | 95a8fe3 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 1740 | |
| 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 Gregor | 6e411bf | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 1750 | } |
| 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. |
| 1759 | void 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 Gregor | 95a8fe3 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 1771 | |
| 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. |
| 1779 | void 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 | } |