Douglas Gregor | 2cf2634 | 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 | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/PCHReader.h" |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 16 | #include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 21 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 23 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 24 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 27 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 2cf2634 | 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 | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | /// \brief Helper class that saves the current stream position and |
| 38 | /// then restores it when destroyed. |
| 39 | struct VISIBILITY_HIDDEN SavedStreamPosition { |
Chris Lattner | b9fa917 | 2009-04-26 20:59:20 +0000 | [diff] [blame] | 40 | explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor) |
| 41 | : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { } |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 42 | |
| 43 | ~SavedStreamPosition() { |
Chris Lattner | b9fa917 | 2009-04-26 20:59:20 +0000 | [diff] [blame] | 44 | Cursor.JumpToBit(Offset); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | private: |
Chris Lattner | b9fa917 | 2009-04-26 20:59:20 +0000 | [diff] [blame] | 48 | llvm::BitstreamCursor &Cursor; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 49 | uint64_t Offset; |
| 50 | }; |
| 51 | } |
| 52 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 54 | // PCH reader implementation |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 57 | PCHReader::PCHReader(Preprocessor &PP, ASTContext &Context) |
| 58 | : SemaObj(0), PP(PP), Context(Context), Consumer(0), |
| 59 | IdentifierTableData(0), IdentifierLookupTable(0), |
| 60 | IdentifierOffsets(0), |
| 61 | MethodPoolLookupTable(0), MethodPoolLookupTableData(0), |
| 62 | TotalSelectorsInMethodPool(0), SelectorOffsets(0), |
| 63 | TotalNumSelectors(0), NumStatementsRead(0), NumMacrosRead(0), |
| 64 | NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0), |
| 65 | NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0) { } |
| 66 | |
| 67 | PCHReader::~PCHReader() {} |
| 68 | |
| 69 | Expr *PCHReader::ReadExpr() { |
| 70 | return dyn_cast_or_null<Expr>(ReadStmt()); |
| 71 | } |
| 72 | |
| 73 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 74 | namespace { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 75 | class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait { |
| 76 | PCHReader &Reader; |
| 77 | |
| 78 | public: |
| 79 | typedef std::pair<ObjCMethodList, ObjCMethodList> data_type; |
| 80 | |
| 81 | typedef Selector external_key_type; |
| 82 | typedef external_key_type internal_key_type; |
| 83 | |
| 84 | explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { } |
| 85 | |
| 86 | static bool EqualKey(const internal_key_type& a, |
| 87 | const internal_key_type& b) { |
| 88 | return a == b; |
| 89 | } |
| 90 | |
| 91 | static unsigned ComputeHash(Selector Sel) { |
| 92 | unsigned N = Sel.getNumArgs(); |
| 93 | if (N == 0) |
| 94 | ++N; |
| 95 | unsigned R = 5381; |
| 96 | for (unsigned I = 0; I != N; ++I) |
| 97 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) |
| 98 | R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R); |
| 99 | return R; |
| 100 | } |
| 101 | |
| 102 | // This hopefully will just get inlined and removed by the optimizer. |
| 103 | static const internal_key_type& |
| 104 | GetInternalKey(const external_key_type& x) { return x; } |
| 105 | |
| 106 | static std::pair<unsigned, unsigned> |
| 107 | ReadKeyDataLength(const unsigned char*& d) { |
| 108 | using namespace clang::io; |
| 109 | unsigned KeyLen = ReadUnalignedLE16(d); |
| 110 | unsigned DataLen = ReadUnalignedLE16(d); |
| 111 | return std::make_pair(KeyLen, DataLen); |
| 112 | } |
| 113 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 114 | internal_key_type ReadKey(const unsigned char* d, unsigned) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 115 | using namespace clang::io; |
| 116 | SelectorTable &SelTable = Reader.getContext().Selectors; |
| 117 | unsigned N = ReadUnalignedLE16(d); |
| 118 | IdentifierInfo *FirstII |
| 119 | = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); |
| 120 | if (N == 0) |
| 121 | return SelTable.getNullarySelector(FirstII); |
| 122 | else if (N == 1) |
| 123 | return SelTable.getUnarySelector(FirstII); |
| 124 | |
| 125 | llvm::SmallVector<IdentifierInfo *, 16> Args; |
| 126 | Args.push_back(FirstII); |
| 127 | for (unsigned I = 1; I != N; ++I) |
| 128 | Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d))); |
| 129 | |
| 130 | return SelTable.getSelector(N, &Args[0]); |
| 131 | } |
| 132 | |
| 133 | data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) { |
| 134 | using namespace clang::io; |
| 135 | unsigned NumInstanceMethods = ReadUnalignedLE16(d); |
| 136 | unsigned NumFactoryMethods = ReadUnalignedLE16(d); |
| 137 | |
| 138 | data_type Result; |
| 139 | |
| 140 | // Load instance methods |
| 141 | ObjCMethodList *Prev = 0; |
| 142 | for (unsigned I = 0; I != NumInstanceMethods; ++I) { |
| 143 | ObjCMethodDecl *Method |
| 144 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
| 145 | if (!Result.first.Method) { |
| 146 | // This is the first method, which is the easy case. |
| 147 | Result.first.Method = Method; |
| 148 | Prev = &Result.first; |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | Prev->Next = new ObjCMethodList(Method, 0); |
| 153 | Prev = Prev->Next; |
| 154 | } |
| 155 | |
| 156 | // Load factory methods |
| 157 | Prev = 0; |
| 158 | for (unsigned I = 0; I != NumFactoryMethods; ++I) { |
| 159 | ObjCMethodDecl *Method |
| 160 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
| 161 | if (!Result.second.Method) { |
| 162 | // This is the first method, which is the easy case. |
| 163 | Result.second.Method = Method; |
| 164 | Prev = &Result.second; |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | Prev->Next = new ObjCMethodList(Method, 0); |
| 169 | Prev = Prev->Next; |
| 170 | } |
| 171 | |
| 172 | return Result; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | } // end anonymous namespace |
| 177 | |
| 178 | /// \brief The on-disk hash table used for the global method pool. |
| 179 | typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait> |
| 180 | PCHMethodPoolLookupTable; |
| 181 | |
| 182 | namespace { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 183 | class VISIBILITY_HIDDEN PCHIdentifierLookupTrait { |
| 184 | PCHReader &Reader; |
| 185 | |
| 186 | // If we know the IdentifierInfo in advance, it is here and we will |
| 187 | // not build a new one. Used when deserializing information about an |
| 188 | // identifier that was constructed before the PCH file was read. |
| 189 | IdentifierInfo *KnownII; |
| 190 | |
| 191 | public: |
| 192 | typedef IdentifierInfo * data_type; |
| 193 | |
| 194 | typedef const std::pair<const char*, unsigned> external_key_type; |
| 195 | |
| 196 | typedef external_key_type internal_key_type; |
| 197 | |
| 198 | explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0) |
| 199 | : Reader(Reader), KnownII(II) { } |
| 200 | |
| 201 | static bool EqualKey(const internal_key_type& a, |
| 202 | const internal_key_type& b) { |
| 203 | return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 |
| 204 | : false; |
| 205 | } |
| 206 | |
| 207 | static unsigned ComputeHash(const internal_key_type& a) { |
| 208 | return BernsteinHash(a.first, a.second); |
| 209 | } |
| 210 | |
| 211 | // This hopefully will just get inlined and removed by the optimizer. |
| 212 | static const internal_key_type& |
| 213 | GetInternalKey(const external_key_type& x) { return x; } |
| 214 | |
| 215 | static std::pair<unsigned, unsigned> |
| 216 | ReadKeyDataLength(const unsigned char*& d) { |
| 217 | using namespace clang::io; |
Douglas Gregor | 5f8e330 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 218 | unsigned DataLen = ReadUnalignedLE16(d); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 219 | unsigned KeyLen = ReadUnalignedLE16(d); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 220 | return std::make_pair(KeyLen, DataLen); |
| 221 | } |
| 222 | |
| 223 | static std::pair<const char*, unsigned> |
| 224 | ReadKey(const unsigned char* d, unsigned n) { |
| 225 | assert(n >= 2 && d[n-1] == '\0'); |
| 226 | return std::make_pair((const char*) d, n-1); |
| 227 | } |
| 228 | |
| 229 | IdentifierInfo *ReadData(const internal_key_type& k, |
| 230 | const unsigned char* d, |
| 231 | unsigned DataLen) { |
| 232 | using namespace clang::io; |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 233 | uint32_t Bits = ReadUnalignedLE32(d); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 234 | bool CPlusPlusOperatorKeyword = Bits & 0x01; |
| 235 | Bits >>= 1; |
| 236 | bool Poisoned = Bits & 0x01; |
| 237 | Bits >>= 1; |
| 238 | bool ExtensionToken = Bits & 0x01; |
| 239 | Bits >>= 1; |
| 240 | bool hasMacroDefinition = Bits & 0x01; |
| 241 | Bits >>= 1; |
| 242 | unsigned ObjCOrBuiltinID = Bits & 0x3FF; |
| 243 | Bits >>= 10; |
| 244 | unsigned TokenID = Bits & 0xFF; |
| 245 | Bits >>= 8; |
| 246 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 247 | pch::IdentID ID = ReadUnalignedLE32(d); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 248 | assert(Bits == 0 && "Extra bits in the identifier?"); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 249 | DataLen -= 8; |
| 250 | |
| 251 | // Build the IdentifierInfo itself and link the identifier ID with |
| 252 | // the new IdentifierInfo. |
| 253 | IdentifierInfo *II = KnownII; |
| 254 | if (!II) |
Douglas Gregor | 5f8e330 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 255 | II = &Reader.getIdentifierTable().CreateIdentifierInfo( |
| 256 | k.first, k.first + k.second); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 257 | Reader.SetIdentifierInfo(ID, II); |
| 258 | |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 259 | // Set or check the various bits in the IdentifierInfo structure. |
| 260 | // FIXME: Load token IDs lazily, too? |
| 261 | assert((unsigned)II->getTokenID() == TokenID && |
| 262 | "Incorrect token ID loaded"); |
| 263 | (void)TokenID; |
| 264 | II->setObjCOrBuiltinID(ObjCOrBuiltinID); |
| 265 | assert(II->isExtensionToken() == ExtensionToken && |
| 266 | "Incorrect extension token flag"); |
| 267 | (void)ExtensionToken; |
| 268 | II->setIsPoisoned(Poisoned); |
| 269 | assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && |
| 270 | "Incorrect C++ operator keyword flag"); |
| 271 | (void)CPlusPlusOperatorKeyword; |
| 272 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 273 | // If this identifier is a macro, deserialize the macro |
| 274 | // definition. |
| 275 | if (hasMacroDefinition) { |
| 276 | uint32_t Offset = ReadUnalignedLE64(d); |
| 277 | Reader.ReadMacroRecord(Offset); |
| 278 | DataLen -= 8; |
| 279 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 280 | |
| 281 | // Read all of the declarations visible at global scope with this |
| 282 | // name. |
| 283 | Sema *SemaObj = Reader.getSema(); |
| 284 | while (DataLen > 0) { |
| 285 | NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 286 | if (SemaObj) { |
| 287 | // Introduce this declaration into the translation-unit scope |
| 288 | // and add it to the declaration chain for this identifier, so |
| 289 | // that (unqualified) name lookup will find it. |
| 290 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D)); |
| 291 | SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); |
| 292 | } else { |
| 293 | // Queue this declaration so that it will be added to the |
| 294 | // translation unit scope and identifier's declaration chain |
| 295 | // once a Sema object is known. |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 296 | Reader.PreloadedDecls.push_back(D); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | DataLen -= 4; |
| 300 | } |
| 301 | return II; |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | } // end anonymous namespace |
| 306 | |
| 307 | /// \brief The on-disk hash table used to contain information about |
| 308 | /// all of the identifiers in the program. |
| 309 | typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait> |
| 310 | PCHIdentifierLookupTable; |
| 311 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 312 | // FIXME: use the diagnostics machinery |
| 313 | static bool Error(const char *Str) { |
| 314 | std::fprintf(stderr, "%s\n", Str); |
| 315 | return true; |
| 316 | } |
| 317 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 318 | /// \brief Check the contents of the predefines buffer against the |
| 319 | /// contents of the predefines buffer used to build the PCH file. |
| 320 | /// |
| 321 | /// The contents of the two predefines buffers should be the same. If |
| 322 | /// not, then some command-line option changed the preprocessor state |
| 323 | /// and we must reject the PCH file. |
| 324 | /// |
| 325 | /// \param PCHPredef The start of the predefines buffer in the PCH |
| 326 | /// file. |
| 327 | /// |
| 328 | /// \param PCHPredefLen The length of the predefines buffer in the PCH |
| 329 | /// file. |
| 330 | /// |
| 331 | /// \param PCHBufferID The FileID for the PCH predefines buffer. |
| 332 | /// |
| 333 | /// \returns true if there was a mismatch (in which case the PCH file |
| 334 | /// should be ignored), or false otherwise. |
| 335 | bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef, |
| 336 | unsigned PCHPredefLen, |
| 337 | FileID PCHBufferID) { |
| 338 | const char *Predef = PP.getPredefines().c_str(); |
| 339 | unsigned PredefLen = PP.getPredefines().size(); |
| 340 | |
| 341 | // If the two predefines buffers compare equal, we're done!. |
| 342 | if (PredefLen == PCHPredefLen && |
| 343 | strncmp(Predef, PCHPredef, PCHPredefLen) == 0) |
| 344 | return false; |
| 345 | |
| 346 | // The predefines buffers are different. Produce a reasonable |
| 347 | // diagnostic showing where they are different. |
| 348 | |
| 349 | // The source locations (potentially in the two different predefines |
| 350 | // buffers) |
| 351 | SourceLocation Loc1, Loc2; |
| 352 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 353 | |
| 354 | // Create a source buffer for our predefines string, so |
| 355 | // that we can build a diagnostic that points into that |
| 356 | // source buffer. |
| 357 | FileID BufferID; |
| 358 | if (Predef && Predef[0]) { |
| 359 | llvm::MemoryBuffer *Buffer |
| 360 | = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen, |
| 361 | "<built-in>"); |
| 362 | BufferID = SourceMgr.createFileIDForMemBuffer(Buffer); |
| 363 | } |
| 364 | |
| 365 | unsigned MinLen = std::min(PredefLen, PCHPredefLen); |
| 366 | std::pair<const char *, const char *> Locations |
| 367 | = std::mismatch(Predef, Predef + MinLen, PCHPredef); |
| 368 | |
| 369 | if (Locations.first != Predef + MinLen) { |
| 370 | // We found the location in the two buffers where there is a |
| 371 | // difference. Form source locations to point there (in both |
| 372 | // buffers). |
| 373 | unsigned Offset = Locations.first - Predef; |
| 374 | Loc1 = SourceMgr.getLocForStartOfFile(BufferID) |
| 375 | .getFileLocWithOffset(Offset); |
| 376 | Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 377 | .getFileLocWithOffset(Offset); |
| 378 | } else if (PredefLen > PCHPredefLen) { |
| 379 | Loc1 = SourceMgr.getLocForStartOfFile(BufferID) |
| 380 | .getFileLocWithOffset(MinLen); |
| 381 | } else { |
| 382 | Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 383 | .getFileLocWithOffset(MinLen); |
| 384 | } |
| 385 | |
| 386 | Diag(Loc1, diag::warn_pch_preprocessor); |
| 387 | if (Loc2.isValid()) |
| 388 | Diag(Loc2, diag::note_predef_in_pch); |
| 389 | Diag(diag::note_ignoring_pch) << FileName; |
| 390 | return true; |
| 391 | } |
| 392 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 393 | /// \brief Read the line table in the source manager block. |
| 394 | /// \returns true if ther was an error. |
| 395 | static bool ParseLineTable(SourceManager &SourceMgr, |
| 396 | llvm::SmallVectorImpl<uint64_t> &Record) { |
| 397 | unsigned Idx = 0; |
| 398 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 399 | |
| 400 | // Parse the file names |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 401 | std::map<int, int> FileIDs; |
| 402 | for (int I = 0, N = Record[Idx++]; I != N; ++I) { |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 403 | // Extract the file name |
| 404 | unsigned FilenameLen = Record[Idx++]; |
| 405 | std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); |
| 406 | Idx += FilenameLen; |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 407 | FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(), |
| 408 | Filename.size()); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Parse the line entries |
| 412 | std::vector<LineEntry> Entries; |
| 413 | while (Idx < Record.size()) { |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 414 | int FID = FileIDs[Record[Idx++]]; |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 415 | |
| 416 | // Extract the line entries |
| 417 | unsigned NumEntries = Record[Idx++]; |
| 418 | Entries.clear(); |
| 419 | Entries.reserve(NumEntries); |
| 420 | for (unsigned I = 0; I != NumEntries; ++I) { |
| 421 | unsigned FileOffset = Record[Idx++]; |
| 422 | unsigned LineNo = Record[Idx++]; |
| 423 | int FilenameID = Record[Idx++]; |
| 424 | SrcMgr::CharacteristicKind FileKind |
| 425 | = (SrcMgr::CharacteristicKind)Record[Idx++]; |
| 426 | unsigned IncludeOffset = Record[Idx++]; |
| 427 | Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, |
| 428 | FileKind, IncludeOffset)); |
| 429 | } |
| 430 | LineTable.AddEntry(FID, Entries); |
| 431 | } |
| 432 | |
| 433 | return false; |
| 434 | } |
| 435 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 436 | /// \brief Read the source manager block |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 437 | PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 438 | using namespace SrcMgr; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 439 | if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) { |
| 440 | Error("Malformed source manager block record"); |
| 441 | return Failure; |
| 442 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 443 | |
| 444 | SourceManager &SourceMgr = Context.getSourceManager(); |
| 445 | RecordData Record; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 446 | unsigned NumHeaderInfos = 0; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 447 | while (true) { |
| 448 | unsigned Code = Stream.ReadCode(); |
| 449 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 450 | if (Stream.ReadBlockEnd()) { |
| 451 | Error("Error at end of Source Manager block"); |
| 452 | return Failure; |
| 453 | } |
| 454 | |
| 455 | return Success; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 459 | // No known subblocks, always skip them. |
| 460 | Stream.ReadSubBlockID(); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 461 | if (Stream.SkipBlock()) { |
| 462 | Error("Malformed block record"); |
| 463 | return Failure; |
| 464 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 465 | continue; |
| 466 | } |
| 467 | |
| 468 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 469 | Stream.ReadAbbrevRecord(); |
| 470 | continue; |
| 471 | } |
| 472 | |
| 473 | // Read a record. |
| 474 | const char *BlobStart; |
| 475 | unsigned BlobLen; |
| 476 | Record.clear(); |
| 477 | switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 478 | default: // Default behavior: ignore. |
| 479 | break; |
| 480 | |
| 481 | case pch::SM_SLOC_FILE_ENTRY: { |
| 482 | // FIXME: We would really like to delay the creation of this |
| 483 | // FileEntry until it is actually required, e.g., when producing |
| 484 | // a diagnostic with a source location in this file. |
| 485 | const FileEntry *File |
| 486 | = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen); |
| 487 | // FIXME: Error recovery if file cannot be found. |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 488 | FileID ID = SourceMgr.createFileID(File, |
| 489 | SourceLocation::getFromRawEncoding(Record[1]), |
| 490 | (CharacteristicKind)Record[2]); |
| 491 | if (Record[3]) |
| 492 | const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile()) |
| 493 | .setHasLineDirectives(); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 494 | break; |
| 495 | } |
| 496 | |
| 497 | case pch::SM_SLOC_BUFFER_ENTRY: { |
| 498 | const char *Name = BlobStart; |
| 499 | unsigned Code = Stream.ReadCode(); |
| 500 | Record.clear(); |
| 501 | unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen); |
| 502 | assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file"); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 503 | (void)RecCode; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 504 | llvm::MemoryBuffer *Buffer |
| 505 | = llvm::MemoryBuffer::getMemBuffer(BlobStart, |
| 506 | BlobStart + BlobLen - 1, |
| 507 | Name); |
| 508 | FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer); |
| 509 | |
| 510 | if (strcmp(Name, "<built-in>") == 0 |
| 511 | && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID)) |
| 512 | return IgnorePCH; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | |
| 516 | case pch::SM_SLOC_INSTANTIATION_ENTRY: { |
| 517 | SourceLocation SpellingLoc |
| 518 | = SourceLocation::getFromRawEncoding(Record[1]); |
| 519 | SourceMgr.createInstantiationLoc( |
| 520 | SpellingLoc, |
| 521 | SourceLocation::getFromRawEncoding(Record[2]), |
| 522 | SourceLocation::getFromRawEncoding(Record[3]), |
Douglas Gregor | f60e991 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 523 | Record[4]); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 524 | break; |
| 525 | } |
| 526 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 527 | case pch::SM_LINE_TABLE: |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 528 | if (ParseLineTable(SourceMgr, Record)) |
| 529 | return Failure; |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 530 | break; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 531 | |
| 532 | case pch::SM_HEADER_FILE_INFO: { |
| 533 | HeaderFileInfo HFI; |
| 534 | HFI.isImport = Record[0]; |
| 535 | HFI.DirInfo = Record[1]; |
| 536 | HFI.NumIncludes = Record[2]; |
| 537 | HFI.ControllingMacroID = Record[3]; |
| 538 | PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 539 | break; |
| 540 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 545 | /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the |
| 546 | /// specified cursor. Read the abbreviations that are at the top of the block |
| 547 | /// and then leave the cursor pointing into the block. |
| 548 | bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, |
| 549 | unsigned BlockID) { |
| 550 | if (Cursor.EnterSubBlock(BlockID)) { |
| 551 | Error("Malformed block record"); |
| 552 | return Failure; |
| 553 | } |
| 554 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 555 | while (true) { |
| 556 | unsigned Code = Cursor.ReadCode(); |
| 557 | |
| 558 | // We expect all abbrevs to be at the start of the block. |
| 559 | if (Code != llvm::bitc::DEFINE_ABBREV) |
| 560 | return false; |
| 561 | Cursor.ReadAbbrevRecord(); |
| 562 | } |
| 563 | } |
| 564 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 565 | void PCHReader::ReadMacroRecord(uint64_t Offset) { |
| 566 | // Keep track of where we are in the stream, then jump back there |
| 567 | // after reading this macro. |
| 568 | SavedStreamPosition SavedPosition(Stream); |
| 569 | |
| 570 | Stream.JumpToBit(Offset); |
| 571 | RecordData Record; |
| 572 | llvm::SmallVector<IdentifierInfo*, 16> MacroArgs; |
| 573 | MacroInfo *Macro = 0; |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 575 | while (true) { |
| 576 | unsigned Code = Stream.ReadCode(); |
| 577 | switch (Code) { |
| 578 | case llvm::bitc::END_BLOCK: |
| 579 | return; |
| 580 | |
| 581 | case llvm::bitc::ENTER_SUBBLOCK: |
| 582 | // No known subblocks, always skip them. |
| 583 | Stream.ReadSubBlockID(); |
| 584 | if (Stream.SkipBlock()) { |
| 585 | Error("Malformed block record"); |
| 586 | return; |
| 587 | } |
| 588 | continue; |
| 589 | |
| 590 | case llvm::bitc::DEFINE_ABBREV: |
| 591 | Stream.ReadAbbrevRecord(); |
| 592 | continue; |
| 593 | default: break; |
| 594 | } |
| 595 | |
| 596 | // Read a record. |
| 597 | Record.clear(); |
| 598 | pch::PreprocessorRecordTypes RecType = |
| 599 | (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record); |
| 600 | switch (RecType) { |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 601 | case pch::PP_MACRO_OBJECT_LIKE: |
| 602 | case pch::PP_MACRO_FUNCTION_LIKE: { |
| 603 | // If we already have a macro, that means that we've hit the end |
| 604 | // of the definition of the macro we were looking for. We're |
| 605 | // done. |
| 606 | if (Macro) |
| 607 | return; |
| 608 | |
| 609 | IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); |
| 610 | if (II == 0) { |
| 611 | Error("Macro must have a name"); |
| 612 | return; |
| 613 | } |
| 614 | SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]); |
| 615 | bool isUsed = Record[2]; |
| 616 | |
| 617 | MacroInfo *MI = PP.AllocateMacroInfo(Loc); |
| 618 | MI->setIsUsed(isUsed); |
| 619 | |
| 620 | if (RecType == pch::PP_MACRO_FUNCTION_LIKE) { |
| 621 | // Decode function-like macro info. |
| 622 | bool isC99VarArgs = Record[3]; |
| 623 | bool isGNUVarArgs = Record[4]; |
| 624 | MacroArgs.clear(); |
| 625 | unsigned NumArgs = Record[5]; |
| 626 | for (unsigned i = 0; i != NumArgs; ++i) |
| 627 | MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); |
| 628 | |
| 629 | // Install function-like macro info. |
| 630 | MI->setIsFunctionLike(); |
| 631 | if (isC99VarArgs) MI->setIsC99Varargs(); |
| 632 | if (isGNUVarArgs) MI->setIsGNUVarargs(); |
| 633 | MI->setArgumentList(&MacroArgs[0], MacroArgs.size(), |
| 634 | PP.getPreprocessorAllocator()); |
| 635 | } |
| 636 | |
| 637 | // Finally, install the macro. |
| 638 | PP.setMacroInfo(II, MI); |
| 639 | |
| 640 | // Remember that we saw this macro last so that we add the tokens that |
| 641 | // form its body to it. |
| 642 | Macro = MI; |
| 643 | ++NumMacrosRead; |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | case pch::PP_TOKEN: { |
| 648 | // If we see a TOKEN before a PP_MACRO_*, then the file is |
| 649 | // erroneous, just pretend we didn't see this. |
| 650 | if (Macro == 0) break; |
| 651 | |
| 652 | Token Tok; |
| 653 | Tok.startToken(); |
| 654 | Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0])); |
| 655 | Tok.setLength(Record[1]); |
| 656 | if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) |
| 657 | Tok.setIdentifierInfo(II); |
| 658 | Tok.setKind((tok::TokenKind)Record[3]); |
| 659 | Tok.setFlag((Token::TokenFlags)Record[4]); |
| 660 | Macro->AddTokenToBody(Tok); |
| 661 | break; |
| 662 | } |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 663 | } |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 667 | PCHReader::PCHReadResult |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 668 | PCHReader::ReadPCHBlock() { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 669 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
| 670 | Error("Malformed block record"); |
| 671 | return Failure; |
| 672 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 673 | |
| 674 | // Read all of the records and blocks for the PCH file. |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 675 | RecordData Record; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 676 | while (!Stream.AtEndOfStream()) { |
| 677 | unsigned Code = Stream.ReadCode(); |
| 678 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 679 | if (Stream.ReadBlockEnd()) { |
| 680 | Error("Error at end of module block"); |
| 681 | return Failure; |
| 682 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 684 | return Success; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 688 | switch (Stream.ReadSubBlockID()) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 689 | case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded) |
| 690 | default: // Skip unknown content. |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 691 | if (Stream.SkipBlock()) { |
| 692 | Error("Malformed block record"); |
| 693 | return Failure; |
| 694 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 695 | break; |
| 696 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 697 | case pch::DECLS_BLOCK_ID: |
| 698 | // We lazily load the decls block, but we want to set up the |
| 699 | // DeclsCursor cursor to point into it. Clone our current bitcode |
| 700 | // cursor to it, enter the block and read the abbrevs in that block. |
| 701 | // With the main cursor, we just skip over it. |
| 702 | DeclsCursor = Stream; |
| 703 | if (Stream.SkipBlock() || // Skip with the main cursor. |
| 704 | // Read the abbrevs. |
| 705 | ReadBlockAbbrevs(DeclsCursor, pch::DECLS_BLOCK_ID)) { |
| 706 | Error("Malformed block record"); |
| 707 | return Failure; |
| 708 | } |
| 709 | break; |
| 710 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 711 | case pch::PREPROCESSOR_BLOCK_ID: |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 712 | if (Stream.SkipBlock()) { |
| 713 | Error("Malformed block record"); |
| 714 | return Failure; |
| 715 | } |
| 716 | break; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 717 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 718 | case pch::SOURCE_MANAGER_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 719 | switch (ReadSourceManagerBlock()) { |
| 720 | case Success: |
| 721 | break; |
| 722 | |
| 723 | case Failure: |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 724 | Error("Malformed source manager block"); |
| 725 | return Failure; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 726 | |
| 727 | case IgnorePCH: |
| 728 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 729 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 730 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 731 | } |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 732 | continue; |
| 733 | } |
| 734 | |
| 735 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 736 | Stream.ReadAbbrevRecord(); |
| 737 | continue; |
| 738 | } |
| 739 | |
| 740 | // Read and process a record. |
| 741 | Record.clear(); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 742 | const char *BlobStart = 0; |
| 743 | unsigned BlobLen = 0; |
| 744 | switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, |
| 745 | &BlobStart, &BlobLen)) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 746 | default: // Default behavior: ignore. |
| 747 | break; |
| 748 | |
| 749 | case pch::TYPE_OFFSET: |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 750 | if (!TypesLoaded.empty()) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 751 | Error("Duplicate TYPE_OFFSET record in PCH file"); |
| 752 | return Failure; |
| 753 | } |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 754 | TypeOffsets = (const uint64_t *)BlobStart; |
| 755 | TypesLoaded.resize(Record[0]); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 756 | break; |
| 757 | |
| 758 | case pch::DECL_OFFSET: |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 759 | if (!DeclsLoaded.empty()) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 760 | Error("Duplicate DECL_OFFSET record in PCH file"); |
| 761 | return Failure; |
| 762 | } |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 763 | DeclOffsets = (const uint64_t *)BlobStart; |
| 764 | DeclsLoaded.resize(Record[0]); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 765 | break; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 766 | |
| 767 | case pch::LANGUAGE_OPTIONS: |
| 768 | if (ParseLanguageOptions(Record)) |
| 769 | return IgnorePCH; |
| 770 | break; |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 771 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 772 | case pch::TARGET_TRIPLE: { |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 773 | std::string TargetTriple(BlobStart, BlobLen); |
| 774 | if (TargetTriple != Context.Target.getTargetTriple()) { |
| 775 | Diag(diag::warn_pch_target_triple) |
| 776 | << TargetTriple << Context.Target.getTargetTriple(); |
| 777 | Diag(diag::note_ignoring_pch) << FileName; |
| 778 | return IgnorePCH; |
| 779 | } |
| 780 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 781 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 782 | |
| 783 | case pch::IDENTIFIER_TABLE: |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 784 | IdentifierTableData = BlobStart; |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 785 | if (Record[0]) { |
| 786 | IdentifierLookupTable |
| 787 | = PCHIdentifierLookupTable::Create( |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 788 | (const unsigned char *)IdentifierTableData + Record[0], |
| 789 | (const unsigned char *)IdentifierTableData, |
| 790 | PCHIdentifierLookupTrait(*this)); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 791 | PP.getIdentifierTable().setExternalIdentifierLookup(this); |
| 792 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 793 | break; |
| 794 | |
| 795 | case pch::IDENTIFIER_OFFSET: |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 796 | if (!IdentifiersLoaded.empty()) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 797 | Error("Duplicate IDENTIFIER_OFFSET record in PCH file"); |
| 798 | return Failure; |
| 799 | } |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 800 | IdentifierOffsets = (const uint32_t *)BlobStart; |
| 801 | IdentifiersLoaded.resize(Record[0]); |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 802 | PP.getHeaderSearchInfo().SetExternalLookup(this); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 803 | break; |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 804 | |
| 805 | case pch::EXTERNAL_DEFINITIONS: |
| 806 | if (!ExternalDefinitions.empty()) { |
| 807 | Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file"); |
| 808 | return Failure; |
| 809 | } |
| 810 | ExternalDefinitions.swap(Record); |
| 811 | break; |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 813 | case pch::SPECIAL_TYPES: |
| 814 | SpecialTypes.swap(Record); |
| 815 | break; |
| 816 | |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 817 | case pch::STATISTICS: |
| 818 | TotalNumStatements = Record[0]; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 819 | TotalNumMacros = Record[1]; |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 820 | TotalLexicalDeclContexts = Record[2]; |
| 821 | TotalVisibleDeclContexts = Record[3]; |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 822 | break; |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 823 | case pch::TENTATIVE_DEFINITIONS: |
| 824 | if (!TentativeDefinitions.empty()) { |
| 825 | Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file"); |
| 826 | return Failure; |
| 827 | } |
| 828 | TentativeDefinitions.swap(Record); |
| 829 | break; |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 830 | |
| 831 | case pch::LOCALLY_SCOPED_EXTERNAL_DECLS: |
| 832 | if (!LocallyScopedExternalDecls.empty()) { |
| 833 | Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file"); |
| 834 | return Failure; |
| 835 | } |
| 836 | LocallyScopedExternalDecls.swap(Record); |
| 837 | break; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 839 | case pch::SELECTOR_OFFSETS: |
| 840 | SelectorOffsets = (const uint32_t *)BlobStart; |
| 841 | TotalNumSelectors = Record[0]; |
| 842 | SelectorsLoaded.resize(TotalNumSelectors); |
| 843 | break; |
| 844 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 845 | case pch::METHOD_POOL: |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 846 | MethodPoolLookupTableData = (const unsigned char *)BlobStart; |
| 847 | if (Record[0]) |
| 848 | MethodPoolLookupTable |
| 849 | = PCHMethodPoolLookupTable::Create( |
| 850 | MethodPoolLookupTableData + Record[0], |
| 851 | MethodPoolLookupTableData, |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 852 | PCHMethodPoolLookupTrait(*this)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 853 | TotalSelectorsInMethodPool = Record[1]; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 854 | break; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 855 | |
| 856 | case pch::PP_COUNTER_VALUE: |
| 857 | if (!Record.empty()) |
| 858 | PP.setCounterValue(Record[0]); |
| 859 | break; |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 860 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 861 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 862 | Error("Premature end of bitstream"); |
| 863 | return Failure; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 866 | PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 867 | // Set the PCH file name. |
| 868 | this->FileName = FileName; |
| 869 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 870 | // Open the PCH file. |
| 871 | std::string ErrStr; |
| 872 | Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr)); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 873 | if (!Buffer) { |
| 874 | Error(ErrStr.c_str()); |
| 875 | return IgnorePCH; |
| 876 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 877 | |
| 878 | // Initialize the stream |
Chris Lattner | b9fa917 | 2009-04-26 20:59:20 +0000 | [diff] [blame] | 879 | StreamFile.init((const unsigned char *)Buffer->getBufferStart(), |
| 880 | (const unsigned char *)Buffer->getBufferEnd()); |
| 881 | Stream.init(StreamFile); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 882 | |
| 883 | // Sniff for the signature. |
| 884 | if (Stream.Read(8) != 'C' || |
| 885 | Stream.Read(8) != 'P' || |
| 886 | Stream.Read(8) != 'C' || |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 887 | Stream.Read(8) != 'H') { |
| 888 | Error("Not a PCH file"); |
| 889 | return IgnorePCH; |
| 890 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 892 | while (!Stream.AtEndOfStream()) { |
| 893 | unsigned Code = Stream.ReadCode(); |
| 894 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 895 | if (Code != llvm::bitc::ENTER_SUBBLOCK) { |
| 896 | Error("Invalid record at top-level"); |
| 897 | return Failure; |
| 898 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 899 | |
| 900 | unsigned BlockID = Stream.ReadSubBlockID(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 902 | // We only know the PCH subblock ID. |
| 903 | switch (BlockID) { |
| 904 | case llvm::bitc::BLOCKINFO_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 905 | if (Stream.ReadBlockInfoBlock()) { |
| 906 | Error("Malformed BlockInfoBlock"); |
| 907 | return Failure; |
| 908 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 909 | break; |
| 910 | case pch::PCH_BLOCK_ID: |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 911 | switch (ReadPCHBlock()) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 912 | case Success: |
| 913 | break; |
| 914 | |
| 915 | case Failure: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 916 | return Failure; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 917 | |
| 918 | case IgnorePCH: |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 919 | // FIXME: We could consider reading through to the end of this |
| 920 | // PCH block, skipping subblocks, to see if there are other |
| 921 | // PCH blocks elsewhere. |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 922 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 923 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 924 | break; |
| 925 | default: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 926 | if (Stream.SkipBlock()) { |
| 927 | Error("Malformed block record"); |
| 928 | return Failure; |
| 929 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 930 | break; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | // Load the translation unit declaration |
| 935 | ReadDeclRecord(DeclOffsets[0], 0); |
| 936 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 937 | // Initialization of builtins and library builtins occurs before the |
| 938 | // PCH file is read, so there may be some identifiers that were |
| 939 | // loaded into the IdentifierTable before we intercepted the |
| 940 | // creation of identifiers. Iterate through the list of known |
| 941 | // identifiers and determine whether we have to establish |
| 942 | // preprocessor definitions or top-level identifier declaration |
| 943 | // chains for those identifiers. |
| 944 | // |
| 945 | // We copy the IdentifierInfo pointers to a small vector first, |
| 946 | // since de-serializing declarations or macro definitions can add |
| 947 | // new entries into the identifier table, invalidating the |
| 948 | // iterators. |
| 949 | llvm::SmallVector<IdentifierInfo *, 128> Identifiers; |
| 950 | for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), |
| 951 | IdEnd = PP.getIdentifierTable().end(); |
| 952 | Id != IdEnd; ++Id) |
| 953 | Identifiers.push_back(Id->second); |
| 954 | PCHIdentifierLookupTable *IdTable |
| 955 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
| 956 | for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { |
| 957 | IdentifierInfo *II = Identifiers[I]; |
| 958 | // Look in the on-disk hash table for an entry for |
| 959 | PCHIdentifierLookupTrait Info(*this, II); |
| 960 | std::pair<const char*, unsigned> Key(II->getName(), II->getLength()); |
| 961 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); |
| 962 | if (Pos == IdTable->end()) |
| 963 | continue; |
| 964 | |
| 965 | // Dereferencing the iterator has the effect of populating the |
| 966 | // IdentifierInfo node with the various declarations it needs. |
| 967 | (void)*Pos; |
| 968 | } |
| 969 | |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 970 | // Load the special types. |
| 971 | Context.setBuiltinVaListType( |
| 972 | GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST])); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 973 | if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID]) |
| 974 | Context.setObjCIdType(GetType(Id)); |
| 975 | if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR]) |
| 976 | Context.setObjCSelType(GetType(Sel)); |
| 977 | if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL]) |
| 978 | Context.setObjCProtoType(GetType(Proto)); |
| 979 | if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS]) |
| 980 | Context.setObjCClassType(GetType(Class)); |
| 981 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING]) |
| 982 | Context.setCFConstantStringType(GetType(String)); |
| 983 | if (unsigned FastEnum |
| 984 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE]) |
| 985 | Context.setObjCFastEnumerationStateType(GetType(FastEnum)); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 986 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 987 | return Success; |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 990 | /// \brief Parse the record that corresponds to a LangOptions data |
| 991 | /// structure. |
| 992 | /// |
| 993 | /// This routine compares the language options used to generate the |
| 994 | /// PCH file against the language options set for the current |
| 995 | /// compilation. For each option, we classify differences between the |
| 996 | /// two compiler states as either "benign" or "important". Benign |
| 997 | /// differences don't matter, and we accept them without complaint |
| 998 | /// (and without modifying the language options). Differences between |
| 999 | /// the states for important options cause the PCH file to be |
| 1000 | /// unusable, so we emit a warning and return true to indicate that |
| 1001 | /// there was an error. |
| 1002 | /// |
| 1003 | /// \returns true if the PCH file is unacceptable, false otherwise. |
| 1004 | bool PCHReader::ParseLanguageOptions( |
| 1005 | const llvm::SmallVectorImpl<uint64_t> &Record) { |
| 1006 | const LangOptions &LangOpts = Context.getLangOptions(); |
| 1007 | #define PARSE_LANGOPT_BENIGN(Option) ++Idx |
| 1008 | #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \ |
| 1009 | if (Record[Idx] != LangOpts.Option) { \ |
| 1010 | Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \ |
| 1011 | Diag(diag::note_ignoring_pch) << FileName; \ |
| 1012 | return true; \ |
| 1013 | } \ |
| 1014 | ++Idx |
| 1015 | |
| 1016 | unsigned Idx = 0; |
| 1017 | PARSE_LANGOPT_BENIGN(Trigraphs); |
| 1018 | PARSE_LANGOPT_BENIGN(BCPLComment); |
| 1019 | PARSE_LANGOPT_BENIGN(DollarIdents); |
| 1020 | PARSE_LANGOPT_BENIGN(AsmPreprocessor); |
| 1021 | PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions); |
| 1022 | PARSE_LANGOPT_BENIGN(ImplicitInt); |
| 1023 | PARSE_LANGOPT_BENIGN(Digraphs); |
| 1024 | PARSE_LANGOPT_BENIGN(HexFloats); |
| 1025 | PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99); |
| 1026 | PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions); |
| 1027 | PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus); |
| 1028 | PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x); |
| 1029 | PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions); |
| 1030 | PARSE_LANGOPT_BENIGN(CXXOperatorName); |
| 1031 | PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c); |
| 1032 | PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2); |
| 1033 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi); |
| 1034 | PARSE_LANGOPT_BENIGN(PascalStrings); |
| 1035 | PARSE_LANGOPT_BENIGN(Boolean); |
| 1036 | PARSE_LANGOPT_BENIGN(WritableStrings); |
| 1037 | PARSE_LANGOPT_IMPORTANT(LaxVectorConversions, |
| 1038 | diag::warn_pch_lax_vector_conversions); |
| 1039 | PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions); |
| 1040 | PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime); |
| 1041 | PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding); |
| 1042 | PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins); |
| 1043 | PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics, |
| 1044 | diag::warn_pch_thread_safe_statics); |
| 1045 | PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); |
| 1046 | PARSE_LANGOPT_BENIGN(EmitAllDecls); |
| 1047 | PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); |
| 1048 | PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking); |
| 1049 | PARSE_LANGOPT_IMPORTANT(HeinousExtensions, |
| 1050 | diag::warn_pch_heinous_extensions); |
| 1051 | // FIXME: Most of the options below are benign if the macro wasn't |
| 1052 | // used. Unfortunately, this means that a PCH compiled without |
| 1053 | // optimization can't be used with optimization turned on, even |
| 1054 | // though the only thing that changes is whether __OPTIMIZE__ was |
| 1055 | // defined... but if __OPTIMIZE__ never showed up in the header, it |
| 1056 | // doesn't matter. We could consider making this some special kind |
| 1057 | // of check. |
| 1058 | PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize); |
| 1059 | PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size); |
| 1060 | PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static); |
| 1061 | PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level); |
| 1062 | PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline); |
| 1063 | PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); |
| 1064 | if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) { |
| 1065 | Diag(diag::warn_pch_gc_mode) |
| 1066 | << (unsigned)Record[Idx] << LangOpts.getGCMode(); |
| 1067 | Diag(diag::note_ignoring_pch) << FileName; |
| 1068 | return true; |
| 1069 | } |
| 1070 | ++Idx; |
| 1071 | PARSE_LANGOPT_BENIGN(getVisibilityMode()); |
| 1072 | PARSE_LANGOPT_BENIGN(InstantiationDepth); |
| 1073 | #undef PARSE_LANGOPT_IRRELEVANT |
| 1074 | #undef PARSE_LANGOPT_BENIGN |
| 1075 | |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1079 | /// \brief Read and return the type at the given offset. |
| 1080 | /// |
| 1081 | /// This routine actually reads the record corresponding to the type |
| 1082 | /// at the given offset in the bitstream. It is a helper routine for |
| 1083 | /// GetType, which deals with reading type IDs. |
| 1084 | QualType PCHReader::ReadTypeRecord(uint64_t Offset) { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1085 | // Keep track of where we are in the stream, then jump back there |
| 1086 | // after reading this type. |
| 1087 | SavedStreamPosition SavedPosition(Stream); |
| 1088 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1089 | Stream.JumpToBit(Offset); |
| 1090 | RecordData Record; |
| 1091 | unsigned Code = Stream.ReadCode(); |
| 1092 | switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) { |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1093 | case pch::TYPE_EXT_QUAL: { |
| 1094 | assert(Record.size() == 3 && |
| 1095 | "Incorrect encoding of extended qualifier type"); |
| 1096 | QualType Base = GetType(Record[0]); |
| 1097 | QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1]; |
| 1098 | unsigned AddressSpace = Record[2]; |
| 1099 | |
| 1100 | QualType T = Base; |
| 1101 | if (GCAttr != QualType::GCNone) |
| 1102 | T = Context.getObjCGCQualType(T, GCAttr); |
| 1103 | if (AddressSpace) |
| 1104 | T = Context.getAddrSpaceQualType(T, AddressSpace); |
| 1105 | return T; |
| 1106 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1108 | case pch::TYPE_FIXED_WIDTH_INT: { |
| 1109 | assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type"); |
| 1110 | return Context.getFixedWidthIntType(Record[0], Record[1]); |
| 1111 | } |
| 1112 | |
| 1113 | case pch::TYPE_COMPLEX: { |
| 1114 | assert(Record.size() == 1 && "Incorrect encoding of complex type"); |
| 1115 | QualType ElemType = GetType(Record[0]); |
| 1116 | return Context.getComplexType(ElemType); |
| 1117 | } |
| 1118 | |
| 1119 | case pch::TYPE_POINTER: { |
| 1120 | assert(Record.size() == 1 && "Incorrect encoding of pointer type"); |
| 1121 | QualType PointeeType = GetType(Record[0]); |
| 1122 | return Context.getPointerType(PointeeType); |
| 1123 | } |
| 1124 | |
| 1125 | case pch::TYPE_BLOCK_POINTER: { |
| 1126 | assert(Record.size() == 1 && "Incorrect encoding of block pointer type"); |
| 1127 | QualType PointeeType = GetType(Record[0]); |
| 1128 | return Context.getBlockPointerType(PointeeType); |
| 1129 | } |
| 1130 | |
| 1131 | case pch::TYPE_LVALUE_REFERENCE: { |
| 1132 | assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type"); |
| 1133 | QualType PointeeType = GetType(Record[0]); |
| 1134 | return Context.getLValueReferenceType(PointeeType); |
| 1135 | } |
| 1136 | |
| 1137 | case pch::TYPE_RVALUE_REFERENCE: { |
| 1138 | assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type"); |
| 1139 | QualType PointeeType = GetType(Record[0]); |
| 1140 | return Context.getRValueReferenceType(PointeeType); |
| 1141 | } |
| 1142 | |
| 1143 | case pch::TYPE_MEMBER_POINTER: { |
| 1144 | assert(Record.size() == 1 && "Incorrect encoding of member pointer type"); |
| 1145 | QualType PointeeType = GetType(Record[0]); |
| 1146 | QualType ClassType = GetType(Record[1]); |
| 1147 | return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); |
| 1148 | } |
| 1149 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1150 | case pch::TYPE_CONSTANT_ARRAY: { |
| 1151 | QualType ElementType = GetType(Record[0]); |
| 1152 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1153 | unsigned IndexTypeQuals = Record[2]; |
| 1154 | unsigned Idx = 3; |
| 1155 | llvm::APInt Size = ReadAPInt(Record, Idx); |
| 1156 | return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals); |
| 1157 | } |
| 1158 | |
| 1159 | case pch::TYPE_INCOMPLETE_ARRAY: { |
| 1160 | QualType ElementType = GetType(Record[0]); |
| 1161 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1162 | unsigned IndexTypeQuals = Record[2]; |
| 1163 | return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); |
| 1164 | } |
| 1165 | |
| 1166 | case pch::TYPE_VARIABLE_ARRAY: { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1167 | QualType ElementType = GetType(Record[0]); |
| 1168 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1169 | unsigned IndexTypeQuals = Record[2]; |
| 1170 | return Context.getVariableArrayType(ElementType, ReadExpr(), |
| 1171 | ASM, IndexTypeQuals); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | case pch::TYPE_VECTOR: { |
| 1175 | if (Record.size() != 2) { |
| 1176 | Error("Incorrect encoding of vector type in PCH file"); |
| 1177 | return QualType(); |
| 1178 | } |
| 1179 | |
| 1180 | QualType ElementType = GetType(Record[0]); |
| 1181 | unsigned NumElements = Record[1]; |
| 1182 | return Context.getVectorType(ElementType, NumElements); |
| 1183 | } |
| 1184 | |
| 1185 | case pch::TYPE_EXT_VECTOR: { |
| 1186 | if (Record.size() != 2) { |
| 1187 | Error("Incorrect encoding of extended vector type in PCH file"); |
| 1188 | return QualType(); |
| 1189 | } |
| 1190 | |
| 1191 | QualType ElementType = GetType(Record[0]); |
| 1192 | unsigned NumElements = Record[1]; |
| 1193 | return Context.getExtVectorType(ElementType, NumElements); |
| 1194 | } |
| 1195 | |
| 1196 | case pch::TYPE_FUNCTION_NO_PROTO: { |
| 1197 | if (Record.size() != 1) { |
| 1198 | Error("Incorrect encoding of no-proto function type"); |
| 1199 | return QualType(); |
| 1200 | } |
| 1201 | QualType ResultType = GetType(Record[0]); |
| 1202 | return Context.getFunctionNoProtoType(ResultType); |
| 1203 | } |
| 1204 | |
| 1205 | case pch::TYPE_FUNCTION_PROTO: { |
| 1206 | QualType ResultType = GetType(Record[0]); |
| 1207 | unsigned Idx = 1; |
| 1208 | unsigned NumParams = Record[Idx++]; |
| 1209 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 1210 | for (unsigned I = 0; I != NumParams; ++I) |
| 1211 | ParamTypes.push_back(GetType(Record[Idx++])); |
| 1212 | bool isVariadic = Record[Idx++]; |
| 1213 | unsigned Quals = Record[Idx++]; |
| 1214 | return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams, |
| 1215 | isVariadic, Quals); |
| 1216 | } |
| 1217 | |
| 1218 | case pch::TYPE_TYPEDEF: |
| 1219 | assert(Record.size() == 1 && "Incorrect encoding of typedef type"); |
| 1220 | return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0]))); |
| 1221 | |
| 1222 | case pch::TYPE_TYPEOF_EXPR: |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1223 | return Context.getTypeOfExprType(ReadExpr()); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1224 | |
| 1225 | case pch::TYPE_TYPEOF: { |
| 1226 | if (Record.size() != 1) { |
| 1227 | Error("Incorrect encoding of typeof(type) in PCH file"); |
| 1228 | return QualType(); |
| 1229 | } |
| 1230 | QualType UnderlyingType = GetType(Record[0]); |
| 1231 | return Context.getTypeOfType(UnderlyingType); |
| 1232 | } |
| 1233 | |
| 1234 | case pch::TYPE_RECORD: |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1235 | assert(Record.size() == 1 && "Incorrect encoding of record type"); |
| 1236 | return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1238 | case pch::TYPE_ENUM: |
| 1239 | assert(Record.size() == 1 && "Incorrect encoding of enum type"); |
| 1240 | return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0]))); |
| 1241 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1242 | case pch::TYPE_OBJC_INTERFACE: |
Chris Lattner | 4dcf151a | 2009-04-22 05:57:30 +0000 | [diff] [blame] | 1243 | assert(Record.size() == 1 && "Incorrect encoding of objc interface type"); |
| 1244 | return Context.getObjCInterfaceType( |
| 1245 | cast<ObjCInterfaceDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1246 | |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 1247 | case pch::TYPE_OBJC_QUALIFIED_INTERFACE: { |
| 1248 | unsigned Idx = 0; |
| 1249 | ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); |
| 1250 | unsigned NumProtos = Record[Idx++]; |
| 1251 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 1252 | for (unsigned I = 0; I != NumProtos; ++I) |
| 1253 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
| 1254 | return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos); |
| 1255 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 1257 | case pch::TYPE_OBJC_QUALIFIED_ID: { |
| 1258 | unsigned Idx = 0; |
| 1259 | unsigned NumProtos = Record[Idx++]; |
| 1260 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 1261 | for (unsigned I = 0; I != NumProtos; ++I) |
| 1262 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
| 1263 | return Context.getObjCQualifiedIdType(&Protos[0], NumProtos); |
| 1264 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1265 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1266 | // Suppress a GCC warning |
| 1267 | return QualType(); |
| 1268 | } |
| 1269 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1270 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1271 | QualType PCHReader::GetType(pch::TypeID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1272 | unsigned Quals = ID & 0x07; |
| 1273 | unsigned Index = ID >> 3; |
| 1274 | |
| 1275 | if (Index < pch::NUM_PREDEF_TYPE_IDS) { |
| 1276 | QualType T; |
| 1277 | switch ((pch::PredefinedTypeIDs)Index) { |
| 1278 | case pch::PREDEF_TYPE_NULL_ID: return QualType(); |
| 1279 | case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; |
| 1280 | case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; |
| 1281 | |
| 1282 | case pch::PREDEF_TYPE_CHAR_U_ID: |
| 1283 | case pch::PREDEF_TYPE_CHAR_S_ID: |
| 1284 | // FIXME: Check that the signedness of CharTy is correct! |
| 1285 | T = Context.CharTy; |
| 1286 | break; |
| 1287 | |
| 1288 | case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; |
| 1289 | case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; |
| 1290 | case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; |
| 1291 | case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; |
| 1292 | case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; |
| 1293 | case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; |
| 1294 | case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; |
| 1295 | case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; |
| 1296 | case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break; |
| 1297 | case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; |
| 1298 | case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; |
| 1299 | case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; |
| 1300 | case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; |
| 1301 | case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; |
| 1302 | case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; |
| 1303 | case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; |
| 1304 | } |
| 1305 | |
| 1306 | assert(!T.isNull() && "Unknown predefined type"); |
| 1307 | return T.getQualifiedType(Quals); |
| 1308 | } |
| 1309 | |
| 1310 | Index -= pch::NUM_PREDEF_TYPE_IDS; |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 1311 | assert(Index < TypesLoaded.size() && "Type index out-of-range"); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1312 | if (!TypesLoaded[Index]) |
| 1313 | TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1315 | return QualType(TypesLoaded[Index], Quals); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1318 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1319 | if (ID == 0) |
| 1320 | return 0; |
| 1321 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1322 | if (ID > DeclsLoaded.size()) { |
| 1323 | Error("Declaration ID out-of-range for PCH file"); |
| 1324 | return 0; |
| 1325 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1326 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1327 | unsigned Index = ID - 1; |
| 1328 | if (!DeclsLoaded[Index]) |
| 1329 | ReadDeclRecord(DeclOffsets[Index], Index); |
| 1330 | |
| 1331 | return DeclsLoaded[Index]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 1334 | Stmt *PCHReader::GetStmt(uint64_t Offset) { |
| 1335 | // Keep track of where we are in the stream, then jump back there |
| 1336 | // after reading this declaration. |
| 1337 | SavedStreamPosition SavedPosition(Stream); |
| 1338 | |
| 1339 | Stream.JumpToBit(Offset); |
| 1340 | return ReadStmt(); |
| 1341 | } |
| 1342 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1343 | bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1344 | llvm::SmallVectorImpl<pch::DeclID> &Decls) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1345 | assert(DC->hasExternalLexicalStorage() && |
| 1346 | "DeclContext has no lexical decls in storage"); |
| 1347 | uint64_t Offset = DeclContextOffsets[DC].first; |
| 1348 | assert(Offset && "DeclContext has no lexical decls in storage"); |
| 1349 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1350 | // Keep track of where we are in the stream, then jump back there |
| 1351 | // after reading this context. |
| 1352 | SavedStreamPosition SavedPosition(Stream); |
| 1353 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1354 | // Load the record containing all of the declarations lexically in |
| 1355 | // this context. |
| 1356 | Stream.JumpToBit(Offset); |
| 1357 | RecordData Record; |
| 1358 | unsigned Code = Stream.ReadCode(); |
| 1359 | unsigned RecCode = Stream.ReadRecord(Code, Record); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 1360 | (void)RecCode; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1361 | assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block"); |
| 1362 | |
| 1363 | // Load all of the declaration IDs |
| 1364 | Decls.clear(); |
| 1365 | Decls.insert(Decls.end(), Record.begin(), Record.end()); |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1366 | ++NumLexicalDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1367 | return false; |
| 1368 | } |
| 1369 | |
| 1370 | bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, |
| 1371 | llvm::SmallVectorImpl<VisibleDeclaration> & Decls) { |
| 1372 | assert(DC->hasExternalVisibleStorage() && |
| 1373 | "DeclContext has no visible decls in storage"); |
| 1374 | uint64_t Offset = DeclContextOffsets[DC].second; |
| 1375 | assert(Offset && "DeclContext has no visible decls in storage"); |
| 1376 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1377 | // Keep track of where we are in the stream, then jump back there |
| 1378 | // after reading this context. |
| 1379 | SavedStreamPosition SavedPosition(Stream); |
| 1380 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1381 | // Load the record containing all of the declarations visible in |
| 1382 | // this context. |
| 1383 | Stream.JumpToBit(Offset); |
| 1384 | RecordData Record; |
| 1385 | unsigned Code = Stream.ReadCode(); |
| 1386 | unsigned RecCode = Stream.ReadRecord(Code, Record); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 1387 | (void)RecCode; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1388 | assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block"); |
| 1389 | if (Record.size() == 0) |
| 1390 | return false; |
| 1391 | |
| 1392 | Decls.clear(); |
| 1393 | |
| 1394 | unsigned Idx = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1395 | while (Idx < Record.size()) { |
| 1396 | Decls.push_back(VisibleDeclaration()); |
| 1397 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 1398 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1399 | unsigned Size = Record[Idx++]; |
| 1400 | llvm::SmallVector<unsigned, 4> & LoadedDecls |
| 1401 | = Decls.back().Declarations; |
| 1402 | LoadedDecls.reserve(Size); |
| 1403 | for (unsigned I = 0; I < Size; ++I) |
| 1404 | LoadedDecls.push_back(Record[Idx++]); |
| 1405 | } |
| 1406 | |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1407 | ++NumVisibleDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1408 | return false; |
| 1409 | } |
| 1410 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1411 | void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { |
Douglas Gregor | 0af2ca4 | 2009-04-22 19:09:20 +0000 | [diff] [blame] | 1412 | this->Consumer = Consumer; |
| 1413 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1414 | if (!Consumer) |
| 1415 | return; |
| 1416 | |
| 1417 | for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { |
| 1418 | Decl *D = GetDecl(ExternalDefinitions[I]); |
| 1419 | DeclGroupRef DG(D); |
| 1420 | Consumer->HandleTopLevelDecl(DG); |
| 1421 | } |
Douglas Gregor | c62a2fe | 2009-04-25 00:41:30 +0000 | [diff] [blame] | 1422 | |
| 1423 | for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) { |
| 1424 | DeclGroupRef DG(InterestingDecls[I]); |
| 1425 | Consumer->HandleTopLevelDecl(DG); |
| 1426 | } |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1429 | void PCHReader::PrintStats() { |
| 1430 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 1431 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1432 | unsigned NumTypesLoaded |
| 1433 | = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), |
| 1434 | (Type *)0); |
| 1435 | unsigned NumDeclsLoaded |
| 1436 | = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), |
| 1437 | (Decl *)0); |
| 1438 | unsigned NumIdentifiersLoaded |
| 1439 | = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), |
| 1440 | IdentifiersLoaded.end(), |
| 1441 | (IdentifierInfo *)0); |
| 1442 | unsigned NumSelectorsLoaded |
| 1443 | = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), |
| 1444 | SelectorsLoaded.end(), |
| 1445 | Selector()); |
Douglas Gregor | 2d41cc1 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 1446 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1447 | if (!TypesLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1448 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1449 | NumTypesLoaded, (unsigned)TypesLoaded.size(), |
| 1450 | ((float)NumTypesLoaded/TypesLoaded.size() * 100)); |
| 1451 | if (!DeclsLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1452 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1453 | NumDeclsLoaded, (unsigned)DeclsLoaded.size(), |
| 1454 | ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1455 | if (!IdentifiersLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1456 | std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1457 | NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), |
| 1458 | ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1459 | if (TotalNumSelectors) |
| 1460 | std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", |
| 1461 | NumSelectorsLoaded, TotalNumSelectors, |
| 1462 | ((float)NumSelectorsLoaded/TotalNumSelectors * 100)); |
| 1463 | if (TotalNumStatements) |
| 1464 | std::fprintf(stderr, " %u/%u statements read (%f%%)\n", |
| 1465 | NumStatementsRead, TotalNumStatements, |
| 1466 | ((float)NumStatementsRead/TotalNumStatements * 100)); |
| 1467 | if (TotalNumMacros) |
| 1468 | std::fprintf(stderr, " %u/%u macros read (%f%%)\n", |
| 1469 | NumMacrosRead, TotalNumMacros, |
| 1470 | ((float)NumMacrosRead/TotalNumMacros * 100)); |
| 1471 | if (TotalLexicalDeclContexts) |
| 1472 | std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", |
| 1473 | NumLexicalDeclContextsRead, TotalLexicalDeclContexts, |
| 1474 | ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts |
| 1475 | * 100)); |
| 1476 | if (TotalVisibleDeclContexts) |
| 1477 | std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", |
| 1478 | NumVisibleDeclContextsRead, TotalVisibleDeclContexts, |
| 1479 | ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts |
| 1480 | * 100)); |
| 1481 | if (TotalSelectorsInMethodPool) { |
| 1482 | std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", |
| 1483 | NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool, |
| 1484 | ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool |
| 1485 | * 100)); |
| 1486 | std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); |
| 1487 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1488 | std::fprintf(stderr, "\n"); |
| 1489 | } |
| 1490 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1491 | void PCHReader::InitializeSema(Sema &S) { |
| 1492 | SemaObj = &S; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1493 | S.ExternalSource = this; |
| 1494 | |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 1495 | // Makes sure any declarations that were deserialized "too early" |
| 1496 | // still get added to the identifier's declaration chains. |
| 1497 | for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { |
| 1498 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I])); |
| 1499 | SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1500 | } |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 1501 | PreloadedDecls.clear(); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1502 | |
| 1503 | // If there were any tentative definitions, deserialize them and add |
| 1504 | // them to Sema's table of tentative definitions. |
| 1505 | for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { |
| 1506 | VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); |
| 1507 | SemaObj->TentativeDefinitions[Var->getDeclName()] = Var; |
| 1508 | } |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1509 | |
| 1510 | // If there were any locally-scoped external declarations, |
| 1511 | // deserialize them and add them to Sema's table of locally-scoped |
| 1512 | // external declarations. |
| 1513 | for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { |
| 1514 | NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); |
| 1515 | SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; |
| 1516 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) { |
| 1520 | // Try to find this name within our on-disk hash table |
| 1521 | PCHIdentifierLookupTable *IdTable |
| 1522 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
| 1523 | std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); |
| 1524 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key); |
| 1525 | if (Pos == IdTable->end()) |
| 1526 | return 0; |
| 1527 | |
| 1528 | // Dereferencing the iterator has the effect of building the |
| 1529 | // IdentifierInfo node and populating it with the various |
| 1530 | // declarations it needs. |
| 1531 | return *Pos; |
| 1532 | } |
| 1533 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1534 | std::pair<ObjCMethodList, ObjCMethodList> |
| 1535 | PCHReader::ReadMethodPool(Selector Sel) { |
| 1536 | if (!MethodPoolLookupTable) |
| 1537 | return std::pair<ObjCMethodList, ObjCMethodList>(); |
| 1538 | |
| 1539 | // Try to find this selector within our on-disk hash table. |
| 1540 | PCHMethodPoolLookupTable *PoolTable |
| 1541 | = (PCHMethodPoolLookupTable*)MethodPoolLookupTable; |
| 1542 | PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1543 | if (Pos == PoolTable->end()) { |
| 1544 | ++NumMethodPoolMisses; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1545 | return std::pair<ObjCMethodList, ObjCMethodList>();; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1546 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1548 | ++NumMethodPoolSelectorsRead; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1549 | return *Pos; |
| 1550 | } |
| 1551 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1552 | void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1553 | assert(ID && "Non-zero identifier ID required"); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1554 | assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range"); |
| 1555 | IdentifiersLoaded[ID - 1] = II; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1558 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1559 | if (ID == 0) |
| 1560 | return 0; |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1561 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1562 | if (!IdentifierTableData || IdentifiersLoaded.empty()) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1563 | Error("No identifier table in PCH file"); |
| 1564 | return 0; |
| 1565 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1566 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1567 | if (!IdentifiersLoaded[ID - 1]) { |
| 1568 | uint32_t Offset = IdentifierOffsets[ID - 1]; |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1569 | const char *Str = IdentifierTableData + Offset; |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1570 | |
| 1571 | // If there is an identifier lookup table, but the offset of this |
| 1572 | // string is after the identifier table itself, then we know that |
| 1573 | // this string is not in the on-disk hash table. Therefore, |
| 1574 | // disable lookup into the hash table when looking for this |
| 1575 | // identifier. |
| 1576 | PCHIdentifierLookupTable *IdTable |
| 1577 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1578 | if (!IdTable || |
| 1579 | Offset >= uint32_t(IdTable->getBuckets() - IdTable->getBase())) { |
| 1580 | // Turn off lookup into the on-disk hash table. We know that |
| 1581 | // this identifier is not there. |
| 1582 | if (IdTable) |
| 1583 | PP.getIdentifierTable().setExternalIdentifierLookup(0); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1584 | |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1585 | // All of the strings in the PCH file are preceded by a 16-bit |
| 1586 | // length. Extract that 16-bit length to avoid having to execute |
| 1587 | // strlen(). |
| 1588 | const char *StrLenPtr = Str - 2; |
| 1589 | unsigned StrLen = (((unsigned) StrLenPtr[0]) |
| 1590 | | (((unsigned) StrLenPtr[1]) << 8)) - 1; |
| 1591 | IdentifiersLoaded[ID - 1] = &Context.Idents.get(Str, Str + StrLen); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1592 | |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1593 | // Turn on lookup into the on-disk hash table, if we have an |
| 1594 | // on-disk hash table. |
| 1595 | if (IdTable) |
| 1596 | PP.getIdentifierTable().setExternalIdentifierLookup(this); |
| 1597 | } else { |
| 1598 | // The identifier is a key in our on-disk hash table. Since we |
| 1599 | // know where the hash table entry starts, just read in this |
| 1600 | // (key, value) pair. |
| 1601 | PCHIdentifierLookupTrait Trait(const_cast<PCHReader &>(*this)); |
| 1602 | const unsigned char *Pos = (const unsigned char *)Str - 4; |
| 1603 | std::pair<unsigned, unsigned> KeyDataLengths |
| 1604 | = Trait.ReadKeyDataLength(Pos); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1605 | |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1606 | PCHIdentifierLookupTrait::internal_key_type InternalKey |
| 1607 | = Trait.ReadKey(Pos, KeyDataLengths.first); |
| 1608 | Pos = (const unsigned char *)Str + KeyDataLengths.first; |
| 1609 | IdentifiersLoaded[ID - 1] = Trait.ReadData(InternalKey, Pos, |
| 1610 | KeyDataLengths.second); |
| 1611 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1612 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1614 | return IdentifiersLoaded[ID - 1]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1617 | Selector PCHReader::DecodeSelector(unsigned ID) { |
| 1618 | if (ID == 0) |
| 1619 | return Selector(); |
| 1620 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1621 | if (!MethodPoolLookupTableData) { |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1622 | Error("No selector table in PCH file"); |
| 1623 | return Selector(); |
| 1624 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (ID > TotalNumSelectors) { |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1627 | Error("Selector ID out of range"); |
| 1628 | return Selector(); |
| 1629 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1630 | |
| 1631 | unsigned Index = ID - 1; |
| 1632 | if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) { |
| 1633 | // Load this selector from the selector table. |
| 1634 | // FIXME: endianness portability issues with SelectorOffsets table |
| 1635 | PCHMethodPoolLookupTrait Trait(*this); |
| 1636 | SelectorsLoaded[Index] |
| 1637 | = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0); |
| 1638 | } |
| 1639 | |
| 1640 | return SelectorsLoaded[Index]; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1643 | DeclarationName |
| 1644 | PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { |
| 1645 | DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; |
| 1646 | switch (Kind) { |
| 1647 | case DeclarationName::Identifier: |
| 1648 | return DeclarationName(GetIdentifierInfo(Record, Idx)); |
| 1649 | |
| 1650 | case DeclarationName::ObjCZeroArgSelector: |
| 1651 | case DeclarationName::ObjCOneArgSelector: |
| 1652 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | a7503a7 | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 1653 | return DeclarationName(GetSelector(Record, Idx)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1654 | |
| 1655 | case DeclarationName::CXXConstructorName: |
| 1656 | return Context.DeclarationNames.getCXXConstructorName( |
| 1657 | GetType(Record[Idx++])); |
| 1658 | |
| 1659 | case DeclarationName::CXXDestructorName: |
| 1660 | return Context.DeclarationNames.getCXXDestructorName( |
| 1661 | GetType(Record[Idx++])); |
| 1662 | |
| 1663 | case DeclarationName::CXXConversionFunctionName: |
| 1664 | return Context.DeclarationNames.getCXXConversionFunctionName( |
| 1665 | GetType(Record[Idx++])); |
| 1666 | |
| 1667 | case DeclarationName::CXXOperatorName: |
| 1668 | return Context.DeclarationNames.getCXXOperatorName( |
| 1669 | (OverloadedOperatorKind)Record[Idx++]); |
| 1670 | |
| 1671 | case DeclarationName::CXXUsingDirective: |
| 1672 | return DeclarationName::getUsingDirectiveName(); |
| 1673 | } |
| 1674 | |
| 1675 | // Required to silence GCC warning |
| 1676 | return DeclarationName(); |
| 1677 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1678 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1679 | /// \brief Read an integral value |
| 1680 | llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { |
| 1681 | unsigned BitWidth = Record[Idx++]; |
| 1682 | unsigned NumWords = llvm::APInt::getNumWords(BitWidth); |
| 1683 | llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); |
| 1684 | Idx += NumWords; |
| 1685 | return Result; |
| 1686 | } |
| 1687 | |
| 1688 | /// \brief Read a signed integral value |
| 1689 | llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { |
| 1690 | bool isUnsigned = Record[Idx++]; |
| 1691 | return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); |
| 1692 | } |
| 1693 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1694 | /// \brief Read a floating-point value |
| 1695 | llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1696 | return llvm::APFloat(ReadAPInt(Record, Idx)); |
| 1697 | } |
| 1698 | |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1699 | // \brief Read a string |
| 1700 | std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) { |
| 1701 | unsigned Len = Record[Idx++]; |
| 1702 | std::string Result(&Record[Idx], &Record[Idx] + Len); |
| 1703 | Idx += Len; |
| 1704 | return Result; |
| 1705 | } |
| 1706 | |
| 1707 | /// \brief Reads attributes from the current stream position. |
| 1708 | Attr *PCHReader::ReadAttributes() { |
| 1709 | unsigned Code = Stream.ReadCode(); |
| 1710 | assert(Code == llvm::bitc::UNABBREV_RECORD && |
| 1711 | "Expected unabbreviated record"); (void)Code; |
| 1712 | |
| 1713 | RecordData Record; |
| 1714 | unsigned Idx = 0; |
| 1715 | unsigned RecCode = Stream.ReadRecord(Code, Record); |
| 1716 | assert(RecCode == pch::DECL_ATTR && "Expected attribute record"); |
| 1717 | (void)RecCode; |
| 1718 | |
| 1719 | #define SIMPLE_ATTR(Name) \ |
| 1720 | case Attr::Name: \ |
| 1721 | New = ::new (Context) Name##Attr(); \ |
| 1722 | break |
| 1723 | |
| 1724 | #define STRING_ATTR(Name) \ |
| 1725 | case Attr::Name: \ |
| 1726 | New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \ |
| 1727 | break |
| 1728 | |
| 1729 | #define UNSIGNED_ATTR(Name) \ |
| 1730 | case Attr::Name: \ |
| 1731 | New = ::new (Context) Name##Attr(Record[Idx++]); \ |
| 1732 | break |
| 1733 | |
| 1734 | Attr *Attrs = 0; |
| 1735 | while (Idx < Record.size()) { |
| 1736 | Attr *New = 0; |
| 1737 | Attr::Kind Kind = (Attr::Kind)Record[Idx++]; |
| 1738 | bool IsInherited = Record[Idx++]; |
| 1739 | |
| 1740 | switch (Kind) { |
| 1741 | STRING_ATTR(Alias); |
| 1742 | UNSIGNED_ATTR(Aligned); |
| 1743 | SIMPLE_ATTR(AlwaysInline); |
| 1744 | SIMPLE_ATTR(AnalyzerNoReturn); |
| 1745 | STRING_ATTR(Annotate); |
| 1746 | STRING_ATTR(AsmLabel); |
| 1747 | |
| 1748 | case Attr::Blocks: |
| 1749 | New = ::new (Context) BlocksAttr( |
| 1750 | (BlocksAttr::BlocksAttrTypes)Record[Idx++]); |
| 1751 | break; |
| 1752 | |
| 1753 | case Attr::Cleanup: |
| 1754 | New = ::new (Context) CleanupAttr( |
| 1755 | cast<FunctionDecl>(GetDecl(Record[Idx++]))); |
| 1756 | break; |
| 1757 | |
| 1758 | SIMPLE_ATTR(Const); |
| 1759 | UNSIGNED_ATTR(Constructor); |
| 1760 | SIMPLE_ATTR(DLLExport); |
| 1761 | SIMPLE_ATTR(DLLImport); |
| 1762 | SIMPLE_ATTR(Deprecated); |
| 1763 | UNSIGNED_ATTR(Destructor); |
| 1764 | SIMPLE_ATTR(FastCall); |
| 1765 | |
| 1766 | case Attr::Format: { |
| 1767 | std::string Type = ReadString(Record, Idx); |
| 1768 | unsigned FormatIdx = Record[Idx++]; |
| 1769 | unsigned FirstArg = Record[Idx++]; |
| 1770 | New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg); |
| 1771 | break; |
| 1772 | } |
| 1773 | |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1774 | SIMPLE_ATTR(GNUInline); |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1775 | |
| 1776 | case Attr::IBOutletKind: |
| 1777 | New = ::new (Context) IBOutletAttr(); |
| 1778 | break; |
| 1779 | |
| 1780 | SIMPLE_ATTR(NoReturn); |
| 1781 | SIMPLE_ATTR(NoThrow); |
| 1782 | SIMPLE_ATTR(Nodebug); |
| 1783 | SIMPLE_ATTR(Noinline); |
| 1784 | |
| 1785 | case Attr::NonNull: { |
| 1786 | unsigned Size = Record[Idx++]; |
| 1787 | llvm::SmallVector<unsigned, 16> ArgNums; |
| 1788 | ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size); |
| 1789 | Idx += Size; |
| 1790 | New = ::new (Context) NonNullAttr(&ArgNums[0], Size); |
| 1791 | break; |
| 1792 | } |
| 1793 | |
| 1794 | SIMPLE_ATTR(ObjCException); |
| 1795 | SIMPLE_ATTR(ObjCNSObject); |
Ted Kremenek | de9a81b | 2009-04-25 00:17:17 +0000 | [diff] [blame] | 1796 | SIMPLE_ATTR(ObjCOwnershipRetain); |
Ted Kremenek | 0fc169e | 2009-04-24 23:09:54 +0000 | [diff] [blame] | 1797 | SIMPLE_ATTR(ObjCOwnershipReturns); |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1798 | SIMPLE_ATTR(Overloadable); |
| 1799 | UNSIGNED_ATTR(Packed); |
| 1800 | SIMPLE_ATTR(Pure); |
| 1801 | UNSIGNED_ATTR(Regparm); |
| 1802 | STRING_ATTR(Section); |
| 1803 | SIMPLE_ATTR(StdCall); |
| 1804 | SIMPLE_ATTR(TransparentUnion); |
| 1805 | SIMPLE_ATTR(Unavailable); |
| 1806 | SIMPLE_ATTR(Unused); |
| 1807 | SIMPLE_ATTR(Used); |
| 1808 | |
| 1809 | case Attr::Visibility: |
| 1810 | New = ::new (Context) VisibilityAttr( |
| 1811 | (VisibilityAttr::VisibilityTypes)Record[Idx++]); |
| 1812 | break; |
| 1813 | |
| 1814 | SIMPLE_ATTR(WarnUnusedResult); |
| 1815 | SIMPLE_ATTR(Weak); |
| 1816 | SIMPLE_ATTR(WeakImport); |
| 1817 | } |
| 1818 | |
| 1819 | assert(New && "Unable to decode attribute?"); |
| 1820 | New->setInherited(IsInherited); |
| 1821 | New->setNext(Attrs); |
| 1822 | Attrs = New; |
| 1823 | } |
| 1824 | #undef UNSIGNED_ATTR |
| 1825 | #undef STRING_ATTR |
| 1826 | #undef SIMPLE_ATTR |
| 1827 | |
| 1828 | // The list of attributes was built backwards. Reverse the list |
| 1829 | // before returning it. |
| 1830 | Attr *PrevAttr = 0, *NextAttr = 0; |
| 1831 | while (Attrs) { |
| 1832 | NextAttr = Attrs->getNext(); |
| 1833 | Attrs->setNext(PrevAttr); |
| 1834 | PrevAttr = Attrs; |
| 1835 | Attrs = NextAttr; |
| 1836 | } |
| 1837 | |
| 1838 | return PrevAttr; |
| 1839 | } |
| 1840 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1841 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1842 | return Diag(SourceLocation(), DiagID); |
| 1843 | } |
| 1844 | |
| 1845 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
| 1846 | return PP.getDiagnostics().Report(FullSourceLoc(Loc, |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1847 | Context.getSourceManager()), |
| 1848 | DiagID); |
| 1849 | } |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 1850 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1851 | /// \brief Retrieve the identifier table associated with the |
| 1852 | /// preprocessor. |
| 1853 | IdentifierTable &PCHReader::getIdentifierTable() { |
| 1854 | return PP.getIdentifierTable(); |
| 1855 | } |
| 1856 | |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 1857 | /// \brief Record that the given ID maps to the given switch-case |
| 1858 | /// statement. |
| 1859 | void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { |
| 1860 | assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); |
| 1861 | SwitchCaseStmts[ID] = SC; |
| 1862 | } |
| 1863 | |
| 1864 | /// \brief Retrieve the switch-case statement with the given ID. |
| 1865 | SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) { |
| 1866 | assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); |
| 1867 | return SwitchCaseStmts[ID]; |
| 1868 | } |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 1869 | |
| 1870 | /// \brief Record that the given label statement has been |
| 1871 | /// deserialized and has the given ID. |
| 1872 | void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) { |
| 1873 | assert(LabelStmts.find(ID) == LabelStmts.end() && |
| 1874 | "Deserialized label twice"); |
| 1875 | LabelStmts[ID] = S; |
| 1876 | |
| 1877 | // If we've already seen any goto statements that point to this |
| 1878 | // label, resolve them now. |
| 1879 | typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter; |
| 1880 | std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID); |
| 1881 | for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto) |
| 1882 | Goto->second->setLabel(S); |
| 1883 | UnresolvedGotoStmts.erase(Gotos.first, Gotos.second); |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 1884 | |
| 1885 | // If we've already seen any address-label statements that point to |
| 1886 | // this label, resolve them now. |
| 1887 | typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter; |
| 1888 | std::pair<AddrLabelIter, AddrLabelIter> AddrLabels |
| 1889 | = UnresolvedAddrLabelExprs.equal_range(ID); |
| 1890 | for (AddrLabelIter AddrLabel = AddrLabels.first; |
| 1891 | AddrLabel != AddrLabels.second; ++AddrLabel) |
| 1892 | AddrLabel->second->setLabel(S); |
| 1893 | UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second); |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | /// \brief Set the label of the given statement to the label |
| 1897 | /// identified by ID. |
| 1898 | /// |
| 1899 | /// Depending on the order in which the label and other statements |
| 1900 | /// referencing that label occur, this operation may complete |
| 1901 | /// immediately (updating the statement) or it may queue the |
| 1902 | /// statement to be back-patched later. |
| 1903 | void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) { |
| 1904 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 1905 | if (Label != LabelStmts.end()) { |
| 1906 | // We've already seen this label, so set the label of the goto and |
| 1907 | // we're done. |
| 1908 | S->setLabel(Label->second); |
| 1909 | } else { |
| 1910 | // We haven't seen this label yet, so add this goto to the set of |
| 1911 | // unresolved goto statements. |
| 1912 | UnresolvedGotoStmts.insert(std::make_pair(ID, S)); |
| 1913 | } |
| 1914 | } |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 1915 | |
| 1916 | /// \brief Set the label of the given expression to the label |
| 1917 | /// identified by ID. |
| 1918 | /// |
| 1919 | /// Depending on the order in which the label and other statements |
| 1920 | /// referencing that label occur, this operation may complete |
| 1921 | /// immediately (updating the statement) or it may queue the |
| 1922 | /// statement to be back-patched later. |
| 1923 | void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) { |
| 1924 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 1925 | if (Label != LabelStmts.end()) { |
| 1926 | // We've already seen this label, so set the label of the |
| 1927 | // label-address expression and we're done. |
| 1928 | S->setLabel(Label->second); |
| 1929 | } else { |
| 1930 | // We haven't seen this label yet, so add this label-address |
| 1931 | // expression to the set of unresolved label-address expressions. |
| 1932 | UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S)); |
| 1933 | } |
| 1934 | } |