Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1 | //===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the PCHReader class, which reads a precompiled header. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "clang/Frontend/PCHReader.h" |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclGroup.h" |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 22 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
| 24 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 26 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 28 | #include "llvm/Bitcode/BitstreamReader.h" |
| 29 | #include "llvm/Support/Compiler.h" |
| 30 | #include "llvm/Support/MemoryBuffer.h" |
| 31 | #include <algorithm> |
| 32 | #include <cstdio> |
| 33 | |
| 34 | using namespace clang; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Declaration deserialization |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | namespace { |
| 40 | class VISIBILITY_HIDDEN PCHDeclReader { |
| 41 | PCHReader &Reader; |
| 42 | const PCHReader::RecordData &Record; |
| 43 | unsigned &Idx; |
| 44 | |
| 45 | public: |
| 46 | PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record, |
| 47 | unsigned &Idx) |
| 48 | : Reader(Reader), Record(Record), Idx(Idx) { } |
| 49 | |
| 50 | void VisitDecl(Decl *D); |
| 51 | void VisitTranslationUnitDecl(TranslationUnitDecl *TU); |
| 52 | void VisitNamedDecl(NamedDecl *ND); |
| 53 | void VisitTypeDecl(TypeDecl *TD); |
| 54 | void VisitTypedefDecl(TypedefDecl *TD); |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 55 | void VisitTagDecl(TagDecl *TD); |
| 56 | void VisitEnumDecl(EnumDecl *ED); |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 57 | void VisitRecordDecl(RecordDecl *RD); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 58 | void VisitValueDecl(ValueDecl *VD); |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 59 | void VisitEnumConstantDecl(EnumConstantDecl *ECD); |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 60 | void VisitFunctionDecl(FunctionDecl *FD); |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 61 | void VisitFieldDecl(FieldDecl *FD); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 62 | void VisitVarDecl(VarDecl *VD); |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 63 | void VisitParmVarDecl(ParmVarDecl *PD); |
| 64 | void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD); |
Douglas Gregor | 2a49179 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 65 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD); |
| 66 | void VisitBlockDecl(BlockDecl *BD); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 67 | std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC); |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | void PCHDeclReader::VisitDecl(Decl *D) { |
| 72 | D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]))); |
| 73 | D->setLexicalDeclContext( |
| 74 | cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]))); |
| 75 | D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 76 | D->setInvalidDecl(Record[Idx++]); |
| 77 | // FIXME: hasAttrs |
| 78 | D->setImplicit(Record[Idx++]); |
| 79 | D->setAccess((AccessSpecifier)Record[Idx++]); |
| 80 | } |
| 81 | |
| 82 | void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { |
| 83 | VisitDecl(TU); |
| 84 | } |
| 85 | |
| 86 | void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) { |
| 87 | VisitDecl(ND); |
| 88 | ND->setDeclName(Reader.ReadDeclarationName(Record, Idx)); |
| 89 | } |
| 90 | |
| 91 | void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) { |
| 92 | VisitNamedDecl(TD); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 93 | TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr()); |
| 94 | } |
| 95 | |
| 96 | void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) { |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 97 | // Note that we cannot use VisitTypeDecl here, because we need to |
| 98 | // set the underlying type of the typedef *before* we try to read |
| 99 | // the type associated with the TypedefDecl. |
| 100 | VisitNamedDecl(TD); |
| 101 | TD->setUnderlyingType(Reader.GetType(Record[Idx + 1])); |
| 102 | TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr()); |
| 103 | Idx += 2; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 106 | void PCHDeclReader::VisitTagDecl(TagDecl *TD) { |
| 107 | VisitTypeDecl(TD); |
| 108 | TD->setTagKind((TagDecl::TagKind)Record[Idx++]); |
| 109 | TD->setDefinition(Record[Idx++]); |
| 110 | TD->setTypedefForAnonDecl( |
| 111 | cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++]))); |
| 112 | } |
| 113 | |
| 114 | void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) { |
| 115 | VisitTagDecl(ED); |
| 116 | ED->setIntegerType(Reader.GetType(Record[Idx++])); |
| 117 | } |
| 118 | |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 119 | void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) { |
| 120 | VisitTagDecl(RD); |
| 121 | RD->setHasFlexibleArrayMember(Record[Idx++]); |
| 122 | RD->setAnonymousStructOrUnion(Record[Idx++]); |
| 123 | } |
| 124 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 125 | void PCHDeclReader::VisitValueDecl(ValueDecl *VD) { |
| 126 | VisitNamedDecl(VD); |
| 127 | VD->setType(Reader.GetType(Record[Idx++])); |
| 128 | } |
| 129 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 130 | void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) { |
| 131 | VisitValueDecl(ECD); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 132 | if (Record[Idx++]) |
| 133 | ECD->setInitExpr(Reader.ReadExpr()); |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 134 | ECD->setInitVal(Reader.ReadAPSInt(Record, Idx)); |
| 135 | } |
| 136 | |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 137 | void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) { |
| 138 | VisitValueDecl(FD); |
| 139 | // FIXME: function body |
| 140 | FD->setPreviousDeclaration( |
| 141 | cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]))); |
| 142 | FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]); |
| 143 | FD->setInline(Record[Idx++]); |
| 144 | FD->setVirtual(Record[Idx++]); |
| 145 | FD->setPure(Record[Idx++]); |
| 146 | FD->setInheritedPrototype(Record[Idx++]); |
| 147 | FD->setHasPrototype(Record[Idx++]); |
| 148 | FD->setDeleted(Record[Idx++]); |
| 149 | FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 150 | unsigned NumParams = Record[Idx++]; |
| 151 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 152 | Params.reserve(NumParams); |
| 153 | for (unsigned I = 0; I != NumParams; ++I) |
| 154 | Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 155 | FD->setParams(Reader.getContext(), &Params[0], NumParams); |
| 156 | } |
| 157 | |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 158 | void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) { |
| 159 | VisitValueDecl(FD); |
| 160 | FD->setMutable(Record[Idx++]); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 161 | if (Record[Idx++]) |
| 162 | FD->setBitWidth(Reader.ReadExpr()); |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 165 | void PCHDeclReader::VisitVarDecl(VarDecl *VD) { |
| 166 | VisitValueDecl(VD); |
| 167 | VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]); |
| 168 | VD->setThreadSpecified(Record[Idx++]); |
| 169 | VD->setCXXDirectInitializer(Record[Idx++]); |
| 170 | VD->setDeclaredInCondition(Record[Idx++]); |
| 171 | VD->setPreviousDeclaration( |
| 172 | cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 173 | VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 174 | if (Record[Idx++]) |
| 175 | VD->setInit(Reader.ReadExpr()); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 178 | void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) { |
| 179 | VisitVarDecl(PD); |
| 180 | PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]); |
| 181 | // FIXME: default argument |
| 182 | } |
| 183 | |
| 184 | void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) { |
| 185 | VisitParmVarDecl(PD); |
| 186 | PD->setOriginalType(Reader.GetType(Record[Idx++])); |
| 187 | } |
| 188 | |
Douglas Gregor | 2a49179 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 189 | void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) { |
| 190 | VisitDecl(AD); |
| 191 | // FIXME: read asm string |
| 192 | } |
| 193 | |
| 194 | void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) { |
| 195 | VisitDecl(BD); |
| 196 | unsigned NumParams = Record[Idx++]; |
| 197 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 198 | Params.reserve(NumParams); |
| 199 | for (unsigned I = 0; I != NumParams; ++I) |
| 200 | Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 201 | BD->setParams(Reader.getContext(), &Params[0], NumParams); |
| 202 | } |
| 203 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 204 | std::pair<uint64_t, uint64_t> |
| 205 | PCHDeclReader::VisitDeclContext(DeclContext *DC) { |
| 206 | uint64_t LexicalOffset = Record[Idx++]; |
| 207 | uint64_t VisibleOffset = 0; |
| 208 | if (DC->getPrimaryContext() == DC) |
| 209 | VisibleOffset = Record[Idx++]; |
| 210 | return std::make_pair(LexicalOffset, VisibleOffset); |
| 211 | } |
| 212 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 213 | //===----------------------------------------------------------------------===// |
| 214 | // Statement/expression deserialization |
| 215 | //===----------------------------------------------------------------------===// |
| 216 | namespace { |
| 217 | class VISIBILITY_HIDDEN PCHStmtReader |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 218 | : public StmtVisitor<PCHStmtReader, unsigned> { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 219 | PCHReader &Reader; |
| 220 | const PCHReader::RecordData &Record; |
| 221 | unsigned &Idx; |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 222 | llvm::SmallVectorImpl<Expr *> &ExprStack; |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 223 | |
| 224 | public: |
| 225 | PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record, |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 226 | unsigned &Idx, llvm::SmallVectorImpl<Expr *> &ExprStack) |
| 227 | : Reader(Reader), Record(Record), Idx(Idx), ExprStack(ExprStack) { } |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 228 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 229 | // Each of the Visit* functions reads in part of the expression |
| 230 | // from the given record and the current expression stack, then |
| 231 | // return the total number of operands that it read from the |
| 232 | // expression stack. |
| 233 | |
| 234 | unsigned VisitExpr(Expr *E); |
| 235 | unsigned VisitPredefinedExpr(PredefinedExpr *E); |
| 236 | unsigned VisitDeclRefExpr(DeclRefExpr *E); |
| 237 | unsigned VisitIntegerLiteral(IntegerLiteral *E); |
| 238 | unsigned VisitFloatingLiteral(FloatingLiteral *E); |
| 239 | unsigned VisitCharacterLiteral(CharacterLiteral *E); |
Douglas Gregor | 4ea0b1f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 240 | unsigned VisitParenExpr(ParenExpr *E); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 241 | unsigned VisitCastExpr(CastExpr *E); |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame^] | 242 | unsigned VisitBinaryOperator(BinaryOperator *E); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 243 | unsigned VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame^] | 244 | unsigned VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 245 | unsigned VisitCStyleCastExpr(CStyleCastExpr *E); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 246 | }; |
| 247 | } |
| 248 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 249 | unsigned PCHStmtReader::VisitExpr(Expr *E) { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 250 | E->setType(Reader.GetType(Record[Idx++])); |
| 251 | E->setTypeDependent(Record[Idx++]); |
| 252 | E->setValueDependent(Record[Idx++]); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 253 | return 0; |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 256 | unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) { |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 257 | VisitExpr(E); |
| 258 | E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 259 | E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 260 | return 0; |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 263 | unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 264 | VisitExpr(E); |
| 265 | E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++]))); |
| 266 | E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 267 | return 0; |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 270 | unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 271 | VisitExpr(E); |
| 272 | E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 273 | E->setValue(Reader.ReadAPInt(Record, Idx)); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 274 | return 0; |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 277 | unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) { |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 278 | VisitExpr(E); |
| 279 | E->setValue(Reader.ReadAPFloat(Record, Idx)); |
| 280 | E->setExact(Record[Idx++]); |
| 281 | E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 282 | return 0; |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 285 | unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 286 | VisitExpr(E); |
| 287 | E->setValue(Record[Idx++]); |
| 288 | E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 289 | E->setWide(Record[Idx++]); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Douglas Gregor | 4ea0b1f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 293 | unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) { |
| 294 | VisitExpr(E); |
| 295 | E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 296 | E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 297 | E->setSubExpr(ExprStack.back()); |
| 298 | return 1; |
| 299 | } |
| 300 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 301 | unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) { |
| 302 | VisitExpr(E); |
| 303 | E->setSubExpr(ExprStack.back()); |
| 304 | return 1; |
| 305 | } |
| 306 | |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame^] | 307 | unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) { |
| 308 | VisitExpr(E); |
| 309 | E->setLHS(ExprStack.end()[-2]); |
| 310 | E->setRHS(ExprStack.end()[-1]); |
| 311 | E->setOpcode((BinaryOperator::Opcode)Record[Idx++]); |
| 312 | E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 313 | return 2; |
| 314 | } |
| 315 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 316 | unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 317 | VisitCastExpr(E); |
| 318 | E->setLvalueCast(Record[Idx++]); |
| 319 | return 1; |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame^] | 322 | unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 323 | VisitCastExpr(E); |
| 324 | E->setTypeAsWritten(Reader.GetType(Record[Idx++])); |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 329 | VisitExplicitCastExpr(E); |
| 330 | E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 331 | E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 332 | return 1; |
| 333 | } |
| 334 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 335 | // FIXME: use the diagnostics machinery |
| 336 | static bool Error(const char *Str) { |
| 337 | std::fprintf(stderr, "%s\n", Str); |
| 338 | return true; |
| 339 | } |
| 340 | |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 341 | /// \brief Check the contents of the predefines buffer against the |
| 342 | /// contents of the predefines buffer used to build the PCH file. |
| 343 | /// |
| 344 | /// The contents of the two predefines buffers should be the same. If |
| 345 | /// not, then some command-line option changed the preprocessor state |
| 346 | /// and we must reject the PCH file. |
| 347 | /// |
| 348 | /// \param PCHPredef The start of the predefines buffer in the PCH |
| 349 | /// file. |
| 350 | /// |
| 351 | /// \param PCHPredefLen The length of the predefines buffer in the PCH |
| 352 | /// file. |
| 353 | /// |
| 354 | /// \param PCHBufferID The FileID for the PCH predefines buffer. |
| 355 | /// |
| 356 | /// \returns true if there was a mismatch (in which case the PCH file |
| 357 | /// should be ignored), or false otherwise. |
| 358 | bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef, |
| 359 | unsigned PCHPredefLen, |
| 360 | FileID PCHBufferID) { |
| 361 | const char *Predef = PP.getPredefines().c_str(); |
| 362 | unsigned PredefLen = PP.getPredefines().size(); |
| 363 | |
| 364 | // If the two predefines buffers compare equal, we're done!. |
| 365 | if (PredefLen == PCHPredefLen && |
| 366 | strncmp(Predef, PCHPredef, PCHPredefLen) == 0) |
| 367 | return false; |
| 368 | |
| 369 | // The predefines buffers are different. Produce a reasonable |
| 370 | // diagnostic showing where they are different. |
| 371 | |
| 372 | // The source locations (potentially in the two different predefines |
| 373 | // buffers) |
| 374 | SourceLocation Loc1, Loc2; |
| 375 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 376 | |
| 377 | // Create a source buffer for our predefines string, so |
| 378 | // that we can build a diagnostic that points into that |
| 379 | // source buffer. |
| 380 | FileID BufferID; |
| 381 | if (Predef && Predef[0]) { |
| 382 | llvm::MemoryBuffer *Buffer |
| 383 | = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen, |
| 384 | "<built-in>"); |
| 385 | BufferID = SourceMgr.createFileIDForMemBuffer(Buffer); |
| 386 | } |
| 387 | |
| 388 | unsigned MinLen = std::min(PredefLen, PCHPredefLen); |
| 389 | std::pair<const char *, const char *> Locations |
| 390 | = std::mismatch(Predef, Predef + MinLen, PCHPredef); |
| 391 | |
| 392 | if (Locations.first != Predef + MinLen) { |
| 393 | // We found the location in the two buffers where there is a |
| 394 | // difference. Form source locations to point there (in both |
| 395 | // buffers). |
| 396 | unsigned Offset = Locations.first - Predef; |
| 397 | Loc1 = SourceMgr.getLocForStartOfFile(BufferID) |
| 398 | .getFileLocWithOffset(Offset); |
| 399 | Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 400 | .getFileLocWithOffset(Offset); |
| 401 | } else if (PredefLen > PCHPredefLen) { |
| 402 | Loc1 = SourceMgr.getLocForStartOfFile(BufferID) |
| 403 | .getFileLocWithOffset(MinLen); |
| 404 | } else { |
| 405 | Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 406 | .getFileLocWithOffset(MinLen); |
| 407 | } |
| 408 | |
| 409 | Diag(Loc1, diag::warn_pch_preprocessor); |
| 410 | if (Loc2.isValid()) |
| 411 | Diag(Loc2, diag::note_predef_in_pch); |
| 412 | Diag(diag::note_ignoring_pch) << FileName; |
| 413 | return true; |
| 414 | } |
| 415 | |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 416 | /// \brief Read the line table in the source manager block. |
| 417 | /// \returns true if ther was an error. |
| 418 | static bool ParseLineTable(SourceManager &SourceMgr, |
| 419 | llvm::SmallVectorImpl<uint64_t> &Record) { |
| 420 | unsigned Idx = 0; |
| 421 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 422 | |
| 423 | // Parse the file names |
Douglas Gregor | 183ad60 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 424 | std::map<int, int> FileIDs; |
| 425 | for (int I = 0, N = Record[Idx++]; I != N; ++I) { |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 426 | // Extract the file name |
| 427 | unsigned FilenameLen = Record[Idx++]; |
| 428 | std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); |
| 429 | Idx += FilenameLen; |
Douglas Gregor | 183ad60 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 430 | FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(), |
| 431 | Filename.size()); |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Parse the line entries |
| 435 | std::vector<LineEntry> Entries; |
| 436 | while (Idx < Record.size()) { |
Douglas Gregor | 183ad60 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 437 | int FID = FileIDs[Record[Idx++]]; |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 438 | |
| 439 | // Extract the line entries |
| 440 | unsigned NumEntries = Record[Idx++]; |
| 441 | Entries.clear(); |
| 442 | Entries.reserve(NumEntries); |
| 443 | for (unsigned I = 0; I != NumEntries; ++I) { |
| 444 | unsigned FileOffset = Record[Idx++]; |
| 445 | unsigned LineNo = Record[Idx++]; |
| 446 | int FilenameID = Record[Idx++]; |
| 447 | SrcMgr::CharacteristicKind FileKind |
| 448 | = (SrcMgr::CharacteristicKind)Record[Idx++]; |
| 449 | unsigned IncludeOffset = Record[Idx++]; |
| 450 | Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, |
| 451 | FileKind, IncludeOffset)); |
| 452 | } |
| 453 | LineTable.AddEntry(FID, Entries); |
| 454 | } |
| 455 | |
| 456 | return false; |
| 457 | } |
| 458 | |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 459 | /// \brief Read the source manager block |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 460 | PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() { |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 461 | using namespace SrcMgr; |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 462 | if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) { |
| 463 | Error("Malformed source manager block record"); |
| 464 | return Failure; |
| 465 | } |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 466 | |
| 467 | SourceManager &SourceMgr = Context.getSourceManager(); |
| 468 | RecordData Record; |
| 469 | while (true) { |
| 470 | unsigned Code = Stream.ReadCode(); |
| 471 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 472 | if (Stream.ReadBlockEnd()) { |
| 473 | Error("Error at end of Source Manager block"); |
| 474 | return Failure; |
| 475 | } |
| 476 | |
| 477 | return Success; |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 481 | // No known subblocks, always skip them. |
| 482 | Stream.ReadSubBlockID(); |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 483 | if (Stream.SkipBlock()) { |
| 484 | Error("Malformed block record"); |
| 485 | return Failure; |
| 486 | } |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 487 | continue; |
| 488 | } |
| 489 | |
| 490 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 491 | Stream.ReadAbbrevRecord(); |
| 492 | continue; |
| 493 | } |
| 494 | |
| 495 | // Read a record. |
| 496 | const char *BlobStart; |
| 497 | unsigned BlobLen; |
| 498 | Record.clear(); |
| 499 | switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 500 | default: // Default behavior: ignore. |
| 501 | break; |
| 502 | |
| 503 | case pch::SM_SLOC_FILE_ENTRY: { |
| 504 | // FIXME: We would really like to delay the creation of this |
| 505 | // FileEntry until it is actually required, e.g., when producing |
| 506 | // a diagnostic with a source location in this file. |
| 507 | const FileEntry *File |
| 508 | = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen); |
| 509 | // FIXME: Error recovery if file cannot be found. |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 510 | FileID ID = SourceMgr.createFileID(File, |
| 511 | SourceLocation::getFromRawEncoding(Record[1]), |
| 512 | (CharacteristicKind)Record[2]); |
| 513 | if (Record[3]) |
| 514 | const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile()) |
| 515 | .setHasLineDirectives(); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 516 | break; |
| 517 | } |
| 518 | |
| 519 | case pch::SM_SLOC_BUFFER_ENTRY: { |
| 520 | const char *Name = BlobStart; |
| 521 | unsigned Code = Stream.ReadCode(); |
| 522 | Record.clear(); |
| 523 | unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen); |
| 524 | assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file"); |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 525 | llvm::MemoryBuffer *Buffer |
| 526 | = llvm::MemoryBuffer::getMemBuffer(BlobStart, |
| 527 | BlobStart + BlobLen - 1, |
| 528 | Name); |
| 529 | FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer); |
| 530 | |
| 531 | if (strcmp(Name, "<built-in>") == 0 |
| 532 | && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID)) |
| 533 | return IgnorePCH; |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 534 | break; |
| 535 | } |
| 536 | |
| 537 | case pch::SM_SLOC_INSTANTIATION_ENTRY: { |
| 538 | SourceLocation SpellingLoc |
| 539 | = SourceLocation::getFromRawEncoding(Record[1]); |
| 540 | SourceMgr.createInstantiationLoc( |
| 541 | SpellingLoc, |
| 542 | SourceLocation::getFromRawEncoding(Record[2]), |
| 543 | SourceLocation::getFromRawEncoding(Record[3]), |
| 544 | Lexer::MeasureTokenLength(SpellingLoc, |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 545 | SourceMgr, |
| 546 | PP.getLangOptions())); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 550 | case pch::SM_LINE_TABLE: |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 551 | if (ParseLineTable(SourceMgr, Record)) |
| 552 | return Failure; |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 553 | break; |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 558 | bool PCHReader::ReadPreprocessorBlock() { |
| 559 | if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) |
| 560 | return Error("Malformed preprocessor block record"); |
| 561 | |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 562 | RecordData Record; |
| 563 | llvm::SmallVector<IdentifierInfo*, 16> MacroArgs; |
| 564 | MacroInfo *LastMacro = 0; |
| 565 | |
| 566 | while (true) { |
| 567 | unsigned Code = Stream.ReadCode(); |
| 568 | switch (Code) { |
| 569 | case llvm::bitc::END_BLOCK: |
| 570 | if (Stream.ReadBlockEnd()) |
| 571 | return Error("Error at end of preprocessor block"); |
| 572 | return false; |
| 573 | |
| 574 | case llvm::bitc::ENTER_SUBBLOCK: |
| 575 | // No known subblocks, always skip them. |
| 576 | Stream.ReadSubBlockID(); |
| 577 | if (Stream.SkipBlock()) |
| 578 | return Error("Malformed block record"); |
| 579 | continue; |
| 580 | |
| 581 | case llvm::bitc::DEFINE_ABBREV: |
| 582 | Stream.ReadAbbrevRecord(); |
| 583 | continue; |
| 584 | default: break; |
| 585 | } |
| 586 | |
| 587 | // Read a record. |
| 588 | Record.clear(); |
| 589 | pch::PreprocessorRecordTypes RecType = |
| 590 | (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record); |
| 591 | switch (RecType) { |
| 592 | default: // Default behavior: ignore unknown records. |
| 593 | break; |
Chris Lattner | 4b21c20 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 594 | case pch::PP_COUNTER_VALUE: |
| 595 | if (!Record.empty()) |
| 596 | PP.setCounterValue(Record[0]); |
| 597 | break; |
| 598 | |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 599 | case pch::PP_MACRO_OBJECT_LIKE: |
| 600 | case pch::PP_MACRO_FUNCTION_LIKE: { |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 601 | IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); |
| 602 | if (II == 0) |
| 603 | return Error("Macro must have a name"); |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 604 | SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]); |
| 605 | bool isUsed = Record[2]; |
| 606 | |
| 607 | MacroInfo *MI = PP.AllocateMacroInfo(Loc); |
| 608 | MI->setIsUsed(isUsed); |
| 609 | |
| 610 | if (RecType == pch::PP_MACRO_FUNCTION_LIKE) { |
| 611 | // Decode function-like macro info. |
| 612 | bool isC99VarArgs = Record[3]; |
| 613 | bool isGNUVarArgs = Record[4]; |
| 614 | MacroArgs.clear(); |
| 615 | unsigned NumArgs = Record[5]; |
| 616 | for (unsigned i = 0; i != NumArgs; ++i) |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 617 | MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 618 | |
| 619 | // Install function-like macro info. |
| 620 | MI->setIsFunctionLike(); |
| 621 | if (isC99VarArgs) MI->setIsC99Varargs(); |
| 622 | if (isGNUVarArgs) MI->setIsGNUVarargs(); |
| 623 | MI->setArgumentList(&MacroArgs[0], MacroArgs.size(), |
| 624 | PP.getPreprocessorAllocator()); |
| 625 | } |
| 626 | |
| 627 | // Finally, install the macro. |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 628 | PP.setMacroInfo(II, MI); |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 629 | |
| 630 | // Remember that we saw this macro last so that we add the tokens that |
| 631 | // form its body to it. |
| 632 | LastMacro = MI; |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | case pch::PP_TOKEN: { |
| 637 | // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just |
| 638 | // pretend we didn't see this. |
| 639 | if (LastMacro == 0) break; |
| 640 | |
| 641 | Token Tok; |
| 642 | Tok.startToken(); |
| 643 | Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0])); |
| 644 | Tok.setLength(Record[1]); |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 645 | if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) |
| 646 | Tok.setIdentifierInfo(II); |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 647 | Tok.setKind((tok::TokenKind)Record[3]); |
| 648 | Tok.setFlag((Token::TokenFlags)Record[4]); |
| 649 | LastMacro->AddTokenToBody(Tok); |
| 650 | break; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 656 | PCHReader::PCHReadResult PCHReader::ReadPCHBlock() { |
| 657 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
| 658 | Error("Malformed block record"); |
| 659 | return Failure; |
| 660 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 662 | uint64_t PreprocessorBlockBit = 0; |
| 663 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 664 | // Read all of the records and blocks for the PCH file. |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 665 | RecordData Record; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 666 | while (!Stream.AtEndOfStream()) { |
| 667 | unsigned Code = Stream.ReadCode(); |
| 668 | if (Code == llvm::bitc::END_BLOCK) { |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 669 | // If we saw the preprocessor block, read it now. |
| 670 | if (PreprocessorBlockBit) { |
| 671 | uint64_t SavedPos = Stream.GetCurrentBitNo(); |
| 672 | Stream.JumpToBit(PreprocessorBlockBit); |
| 673 | if (ReadPreprocessorBlock()) { |
| 674 | Error("Malformed preprocessor block"); |
| 675 | return Failure; |
| 676 | } |
| 677 | Stream.JumpToBit(SavedPos); |
| 678 | } |
| 679 | |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 680 | if (Stream.ReadBlockEnd()) { |
| 681 | Error("Error at end of module block"); |
| 682 | return Failure; |
| 683 | } |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 684 | |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 685 | return Success; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 689 | switch (Stream.ReadSubBlockID()) { |
| 690 | case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded) |
| 691 | case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded) |
| 692 | default: // Skip unknown content. |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 693 | if (Stream.SkipBlock()) { |
| 694 | Error("Malformed block record"); |
| 695 | return Failure; |
| 696 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 697 | break; |
| 698 | |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 699 | case pch::PREPROCESSOR_BLOCK_ID: |
| 700 | // Skip the preprocessor block for now, but remember where it is. We |
| 701 | // want to read it in after the identifier table. |
| 702 | if (PreprocessorBlockBit) { |
| 703 | Error("Multiple preprocessor blocks found."); |
| 704 | return Failure; |
| 705 | } |
| 706 | PreprocessorBlockBit = Stream.GetCurrentBitNo(); |
| 707 | if (Stream.SkipBlock()) { |
| 708 | Error("Malformed block record"); |
| 709 | return Failure; |
| 710 | } |
| 711 | break; |
| 712 | |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 713 | case pch::SOURCE_MANAGER_BLOCK_ID: |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 714 | switch (ReadSourceManagerBlock()) { |
| 715 | case Success: |
| 716 | break; |
| 717 | |
| 718 | case Failure: |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 719 | Error("Malformed source manager block"); |
| 720 | return Failure; |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 721 | |
| 722 | case IgnorePCH: |
| 723 | return IgnorePCH; |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 724 | } |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 725 | break; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 726 | } |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 727 | continue; |
| 728 | } |
| 729 | |
| 730 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 731 | Stream.ReadAbbrevRecord(); |
| 732 | continue; |
| 733 | } |
| 734 | |
| 735 | // Read and process a record. |
| 736 | Record.clear(); |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 737 | const char *BlobStart = 0; |
| 738 | unsigned BlobLen = 0; |
| 739 | switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, |
| 740 | &BlobStart, &BlobLen)) { |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 741 | default: // Default behavior: ignore. |
| 742 | break; |
| 743 | |
| 744 | case pch::TYPE_OFFSET: |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 745 | if (!TypeOffsets.empty()) { |
| 746 | Error("Duplicate TYPE_OFFSET record in PCH file"); |
| 747 | return Failure; |
| 748 | } |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 749 | TypeOffsets.swap(Record); |
| 750 | TypeAlreadyLoaded.resize(TypeOffsets.size(), false); |
| 751 | break; |
| 752 | |
| 753 | case pch::DECL_OFFSET: |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 754 | if (!DeclOffsets.empty()) { |
| 755 | Error("Duplicate DECL_OFFSET record in PCH file"); |
| 756 | return Failure; |
| 757 | } |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 758 | DeclOffsets.swap(Record); |
| 759 | DeclAlreadyLoaded.resize(DeclOffsets.size(), false); |
| 760 | break; |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 761 | |
| 762 | case pch::LANGUAGE_OPTIONS: |
| 763 | if (ParseLanguageOptions(Record)) |
| 764 | return IgnorePCH; |
| 765 | break; |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 767 | case pch::TARGET_TRIPLE: { |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 768 | std::string TargetTriple(BlobStart, BlobLen); |
| 769 | if (TargetTriple != Context.Target.getTargetTriple()) { |
| 770 | Diag(diag::warn_pch_target_triple) |
| 771 | << TargetTriple << Context.Target.getTargetTriple(); |
| 772 | Diag(diag::note_ignoring_pch) << FileName; |
| 773 | return IgnorePCH; |
| 774 | } |
| 775 | break; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 776 | } |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 777 | |
| 778 | case pch::IDENTIFIER_TABLE: |
| 779 | IdentifierTable = BlobStart; |
| 780 | break; |
| 781 | |
| 782 | case pch::IDENTIFIER_OFFSET: |
| 783 | if (!IdentifierData.empty()) { |
| 784 | Error("Duplicate IDENTIFIER_OFFSET record in PCH file"); |
| 785 | return Failure; |
| 786 | } |
| 787 | IdentifierData.swap(Record); |
| 788 | #ifndef NDEBUG |
| 789 | for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) { |
| 790 | if ((IdentifierData[I] & 0x01) == 0) { |
| 791 | Error("Malformed identifier table in the precompiled header"); |
| 792 | return Failure; |
| 793 | } |
| 794 | } |
| 795 | #endif |
| 796 | break; |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 797 | |
| 798 | case pch::EXTERNAL_DEFINITIONS: |
| 799 | if (!ExternalDefinitions.empty()) { |
| 800 | Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file"); |
| 801 | return Failure; |
| 802 | } |
| 803 | ExternalDefinitions.swap(Record); |
| 804 | break; |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 805 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 808 | Error("Premature end of bitstream"); |
| 809 | return Failure; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 812 | PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 813 | // Set the PCH file name. |
| 814 | this->FileName = FileName; |
| 815 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 816 | // Open the PCH file. |
| 817 | std::string ErrStr; |
| 818 | Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr)); |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 819 | if (!Buffer) { |
| 820 | Error(ErrStr.c_str()); |
| 821 | return IgnorePCH; |
| 822 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 823 | |
| 824 | // Initialize the stream |
| 825 | Stream.init((const unsigned char *)Buffer->getBufferStart(), |
| 826 | (const unsigned char *)Buffer->getBufferEnd()); |
| 827 | |
| 828 | // Sniff for the signature. |
| 829 | if (Stream.Read(8) != 'C' || |
| 830 | Stream.Read(8) != 'P' || |
| 831 | Stream.Read(8) != 'C' || |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 832 | Stream.Read(8) != 'H') { |
| 833 | Error("Not a PCH file"); |
| 834 | return IgnorePCH; |
| 835 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 836 | |
| 837 | // We expect a number of well-defined blocks, though we don't necessarily |
| 838 | // need to understand them all. |
| 839 | while (!Stream.AtEndOfStream()) { |
| 840 | unsigned Code = Stream.ReadCode(); |
| 841 | |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 842 | if (Code != llvm::bitc::ENTER_SUBBLOCK) { |
| 843 | Error("Invalid record at top-level"); |
| 844 | return Failure; |
| 845 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 846 | |
| 847 | unsigned BlockID = Stream.ReadSubBlockID(); |
| 848 | |
| 849 | // We only know the PCH subblock ID. |
| 850 | switch (BlockID) { |
| 851 | case llvm::bitc::BLOCKINFO_BLOCK_ID: |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 852 | if (Stream.ReadBlockInfoBlock()) { |
| 853 | Error("Malformed BlockInfoBlock"); |
| 854 | return Failure; |
| 855 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 856 | break; |
| 857 | case pch::PCH_BLOCK_ID: |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 858 | switch (ReadPCHBlock()) { |
| 859 | case Success: |
| 860 | break; |
| 861 | |
| 862 | case Failure: |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 863 | return Failure; |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 864 | |
| 865 | case IgnorePCH: |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 866 | // FIXME: We could consider reading through to the end of this |
| 867 | // PCH block, skipping subblocks, to see if there are other |
| 868 | // PCH blocks elsewhere. |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 869 | return IgnorePCH; |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 870 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 871 | break; |
| 872 | default: |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 873 | if (Stream.SkipBlock()) { |
| 874 | Error("Malformed block record"); |
| 875 | return Failure; |
| 876 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | // Load the translation unit declaration |
| 882 | ReadDeclRecord(DeclOffsets[0], 0); |
| 883 | |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 884 | return Success; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 887 | namespace { |
| 888 | /// \brief Helper class that saves the current stream position and |
| 889 | /// then restores it when destroyed. |
| 890 | struct VISIBILITY_HIDDEN SavedStreamPosition { |
| 891 | explicit SavedStreamPosition(llvm::BitstreamReader &Stream) |
| 892 | : Stream(Stream), Offset(Stream.GetCurrentBitNo()), |
| 893 | EndOfStream(Stream.AtEndOfStream()){ } |
| 894 | |
| 895 | ~SavedStreamPosition() { |
| 896 | if (!EndOfStream) |
| 897 | Stream.JumpToBit(Offset); |
| 898 | } |
| 899 | |
| 900 | private: |
| 901 | llvm::BitstreamReader &Stream; |
| 902 | uint64_t Offset; |
| 903 | bool EndOfStream; |
| 904 | }; |
| 905 | } |
| 906 | |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 907 | /// \brief Parse the record that corresponds to a LangOptions data |
| 908 | /// structure. |
| 909 | /// |
| 910 | /// This routine compares the language options used to generate the |
| 911 | /// PCH file against the language options set for the current |
| 912 | /// compilation. For each option, we classify differences between the |
| 913 | /// two compiler states as either "benign" or "important". Benign |
| 914 | /// differences don't matter, and we accept them without complaint |
| 915 | /// (and without modifying the language options). Differences between |
| 916 | /// the states for important options cause the PCH file to be |
| 917 | /// unusable, so we emit a warning and return true to indicate that |
| 918 | /// there was an error. |
| 919 | /// |
| 920 | /// \returns true if the PCH file is unacceptable, false otherwise. |
| 921 | bool PCHReader::ParseLanguageOptions( |
| 922 | const llvm::SmallVectorImpl<uint64_t> &Record) { |
| 923 | const LangOptions &LangOpts = Context.getLangOptions(); |
| 924 | #define PARSE_LANGOPT_BENIGN(Option) ++Idx |
| 925 | #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \ |
| 926 | if (Record[Idx] != LangOpts.Option) { \ |
| 927 | Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \ |
| 928 | Diag(diag::note_ignoring_pch) << FileName; \ |
| 929 | return true; \ |
| 930 | } \ |
| 931 | ++Idx |
| 932 | |
| 933 | unsigned Idx = 0; |
| 934 | PARSE_LANGOPT_BENIGN(Trigraphs); |
| 935 | PARSE_LANGOPT_BENIGN(BCPLComment); |
| 936 | PARSE_LANGOPT_BENIGN(DollarIdents); |
| 937 | PARSE_LANGOPT_BENIGN(AsmPreprocessor); |
| 938 | PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions); |
| 939 | PARSE_LANGOPT_BENIGN(ImplicitInt); |
| 940 | PARSE_LANGOPT_BENIGN(Digraphs); |
| 941 | PARSE_LANGOPT_BENIGN(HexFloats); |
| 942 | PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99); |
| 943 | PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions); |
| 944 | PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus); |
| 945 | PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x); |
| 946 | PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions); |
| 947 | PARSE_LANGOPT_BENIGN(CXXOperatorName); |
| 948 | PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c); |
| 949 | PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2); |
| 950 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi); |
| 951 | PARSE_LANGOPT_BENIGN(PascalStrings); |
| 952 | PARSE_LANGOPT_BENIGN(Boolean); |
| 953 | PARSE_LANGOPT_BENIGN(WritableStrings); |
| 954 | PARSE_LANGOPT_IMPORTANT(LaxVectorConversions, |
| 955 | diag::warn_pch_lax_vector_conversions); |
| 956 | PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions); |
| 957 | PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime); |
| 958 | PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding); |
| 959 | PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins); |
| 960 | PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics, |
| 961 | diag::warn_pch_thread_safe_statics); |
| 962 | PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); |
| 963 | PARSE_LANGOPT_BENIGN(EmitAllDecls); |
| 964 | PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); |
| 965 | PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking); |
| 966 | PARSE_LANGOPT_IMPORTANT(HeinousExtensions, |
| 967 | diag::warn_pch_heinous_extensions); |
| 968 | // FIXME: Most of the options below are benign if the macro wasn't |
| 969 | // used. Unfortunately, this means that a PCH compiled without |
| 970 | // optimization can't be used with optimization turned on, even |
| 971 | // though the only thing that changes is whether __OPTIMIZE__ was |
| 972 | // defined... but if __OPTIMIZE__ never showed up in the header, it |
| 973 | // doesn't matter. We could consider making this some special kind |
| 974 | // of check. |
| 975 | PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize); |
| 976 | PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size); |
| 977 | PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static); |
| 978 | PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level); |
| 979 | PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline); |
| 980 | PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); |
| 981 | if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) { |
| 982 | Diag(diag::warn_pch_gc_mode) |
| 983 | << (unsigned)Record[Idx] << LangOpts.getGCMode(); |
| 984 | Diag(diag::note_ignoring_pch) << FileName; |
| 985 | return true; |
| 986 | } |
| 987 | ++Idx; |
| 988 | PARSE_LANGOPT_BENIGN(getVisibilityMode()); |
| 989 | PARSE_LANGOPT_BENIGN(InstantiationDepth); |
| 990 | #undef PARSE_LANGOPT_IRRELEVANT |
| 991 | #undef PARSE_LANGOPT_BENIGN |
| 992 | |
| 993 | return false; |
| 994 | } |
| 995 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 996 | /// \brief Read and return the type at the given offset. |
| 997 | /// |
| 998 | /// This routine actually reads the record corresponding to the type |
| 999 | /// at the given offset in the bitstream. It is a helper routine for |
| 1000 | /// GetType, which deals with reading type IDs. |
| 1001 | QualType PCHReader::ReadTypeRecord(uint64_t Offset) { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1002 | // Keep track of where we are in the stream, then jump back there |
| 1003 | // after reading this type. |
| 1004 | SavedStreamPosition SavedPosition(Stream); |
| 1005 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1006 | Stream.JumpToBit(Offset); |
| 1007 | RecordData Record; |
| 1008 | unsigned Code = Stream.ReadCode(); |
| 1009 | switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) { |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1010 | case pch::TYPE_EXT_QUAL: |
| 1011 | // FIXME: Deserialize ExtQualType |
| 1012 | assert(false && "Cannot deserialize qualified types yet"); |
| 1013 | return QualType(); |
| 1014 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1015 | case pch::TYPE_FIXED_WIDTH_INT: { |
| 1016 | assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type"); |
| 1017 | return Context.getFixedWidthIntType(Record[0], Record[1]); |
| 1018 | } |
| 1019 | |
| 1020 | case pch::TYPE_COMPLEX: { |
| 1021 | assert(Record.size() == 1 && "Incorrect encoding of complex type"); |
| 1022 | QualType ElemType = GetType(Record[0]); |
| 1023 | return Context.getComplexType(ElemType); |
| 1024 | } |
| 1025 | |
| 1026 | case pch::TYPE_POINTER: { |
| 1027 | assert(Record.size() == 1 && "Incorrect encoding of pointer type"); |
| 1028 | QualType PointeeType = GetType(Record[0]); |
| 1029 | return Context.getPointerType(PointeeType); |
| 1030 | } |
| 1031 | |
| 1032 | case pch::TYPE_BLOCK_POINTER: { |
| 1033 | assert(Record.size() == 1 && "Incorrect encoding of block pointer type"); |
| 1034 | QualType PointeeType = GetType(Record[0]); |
| 1035 | return Context.getBlockPointerType(PointeeType); |
| 1036 | } |
| 1037 | |
| 1038 | case pch::TYPE_LVALUE_REFERENCE: { |
| 1039 | assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type"); |
| 1040 | QualType PointeeType = GetType(Record[0]); |
| 1041 | return Context.getLValueReferenceType(PointeeType); |
| 1042 | } |
| 1043 | |
| 1044 | case pch::TYPE_RVALUE_REFERENCE: { |
| 1045 | assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type"); |
| 1046 | QualType PointeeType = GetType(Record[0]); |
| 1047 | return Context.getRValueReferenceType(PointeeType); |
| 1048 | } |
| 1049 | |
| 1050 | case pch::TYPE_MEMBER_POINTER: { |
| 1051 | assert(Record.size() == 1 && "Incorrect encoding of member pointer type"); |
| 1052 | QualType PointeeType = GetType(Record[0]); |
| 1053 | QualType ClassType = GetType(Record[1]); |
| 1054 | return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); |
| 1055 | } |
| 1056 | |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1057 | case pch::TYPE_CONSTANT_ARRAY: { |
| 1058 | QualType ElementType = GetType(Record[0]); |
| 1059 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1060 | unsigned IndexTypeQuals = Record[2]; |
| 1061 | unsigned Idx = 3; |
| 1062 | llvm::APInt Size = ReadAPInt(Record, Idx); |
| 1063 | return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals); |
| 1064 | } |
| 1065 | |
| 1066 | case pch::TYPE_INCOMPLETE_ARRAY: { |
| 1067 | QualType ElementType = GetType(Record[0]); |
| 1068 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1069 | unsigned IndexTypeQuals = Record[2]; |
| 1070 | return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); |
| 1071 | } |
| 1072 | |
| 1073 | case pch::TYPE_VARIABLE_ARRAY: { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1074 | QualType ElementType = GetType(Record[0]); |
| 1075 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1076 | unsigned IndexTypeQuals = Record[2]; |
| 1077 | return Context.getVariableArrayType(ElementType, ReadExpr(), |
| 1078 | ASM, IndexTypeQuals); |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | case pch::TYPE_VECTOR: { |
| 1082 | if (Record.size() != 2) { |
| 1083 | Error("Incorrect encoding of vector type in PCH file"); |
| 1084 | return QualType(); |
| 1085 | } |
| 1086 | |
| 1087 | QualType ElementType = GetType(Record[0]); |
| 1088 | unsigned NumElements = Record[1]; |
| 1089 | return Context.getVectorType(ElementType, NumElements); |
| 1090 | } |
| 1091 | |
| 1092 | case pch::TYPE_EXT_VECTOR: { |
| 1093 | if (Record.size() != 2) { |
| 1094 | Error("Incorrect encoding of extended vector type in PCH file"); |
| 1095 | return QualType(); |
| 1096 | } |
| 1097 | |
| 1098 | QualType ElementType = GetType(Record[0]); |
| 1099 | unsigned NumElements = Record[1]; |
| 1100 | return Context.getExtVectorType(ElementType, NumElements); |
| 1101 | } |
| 1102 | |
| 1103 | case pch::TYPE_FUNCTION_NO_PROTO: { |
| 1104 | if (Record.size() != 1) { |
| 1105 | Error("Incorrect encoding of no-proto function type"); |
| 1106 | return QualType(); |
| 1107 | } |
| 1108 | QualType ResultType = GetType(Record[0]); |
| 1109 | return Context.getFunctionNoProtoType(ResultType); |
| 1110 | } |
| 1111 | |
| 1112 | case pch::TYPE_FUNCTION_PROTO: { |
| 1113 | QualType ResultType = GetType(Record[0]); |
| 1114 | unsigned Idx = 1; |
| 1115 | unsigned NumParams = Record[Idx++]; |
| 1116 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 1117 | for (unsigned I = 0; I != NumParams; ++I) |
| 1118 | ParamTypes.push_back(GetType(Record[Idx++])); |
| 1119 | bool isVariadic = Record[Idx++]; |
| 1120 | unsigned Quals = Record[Idx++]; |
| 1121 | return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams, |
| 1122 | isVariadic, Quals); |
| 1123 | } |
| 1124 | |
| 1125 | case pch::TYPE_TYPEDEF: |
| 1126 | assert(Record.size() == 1 && "Incorrect encoding of typedef type"); |
| 1127 | return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0]))); |
| 1128 | |
| 1129 | case pch::TYPE_TYPEOF_EXPR: |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1130 | return Context.getTypeOfExprType(ReadExpr()); |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1131 | |
| 1132 | case pch::TYPE_TYPEOF: { |
| 1133 | if (Record.size() != 1) { |
| 1134 | Error("Incorrect encoding of typeof(type) in PCH file"); |
| 1135 | return QualType(); |
| 1136 | } |
| 1137 | QualType UnderlyingType = GetType(Record[0]); |
| 1138 | return Context.getTypeOfType(UnderlyingType); |
| 1139 | } |
| 1140 | |
| 1141 | case pch::TYPE_RECORD: |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1142 | assert(Record.size() == 1 && "Incorrect encoding of record type"); |
| 1143 | return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0]))); |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1144 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1145 | case pch::TYPE_ENUM: |
| 1146 | assert(Record.size() == 1 && "Incorrect encoding of enum type"); |
| 1147 | return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0]))); |
| 1148 | |
Douglas Gregor | 88fd09d | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1149 | case pch::TYPE_OBJC_INTERFACE: |
| 1150 | // FIXME: Deserialize ObjCInterfaceType |
| 1151 | assert(false && "Cannot de-serialize ObjC interface types yet"); |
| 1152 | return QualType(); |
| 1153 | |
| 1154 | case pch::TYPE_OBJC_QUALIFIED_INTERFACE: |
| 1155 | // FIXME: Deserialize ObjCQualifiedInterfaceType |
| 1156 | assert(false && "Cannot de-serialize ObjC qualified interface types yet"); |
| 1157 | return QualType(); |
| 1158 | |
| 1159 | case pch::TYPE_OBJC_QUALIFIED_ID: |
| 1160 | // FIXME: Deserialize ObjCQualifiedIdType |
| 1161 | assert(false && "Cannot de-serialize ObjC qualified id types yet"); |
| 1162 | return QualType(); |
| 1163 | |
| 1164 | case pch::TYPE_OBJC_QUALIFIED_CLASS: |
| 1165 | // FIXME: Deserialize ObjCQualifiedClassType |
| 1166 | assert(false && "Cannot de-serialize ObjC qualified class types yet"); |
| 1167 | return QualType(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | // Suppress a GCC warning |
| 1171 | return QualType(); |
| 1172 | } |
| 1173 | |
| 1174 | /// \brief Note that we have loaded the declaration with the given |
| 1175 | /// Index. |
| 1176 | /// |
| 1177 | /// This routine notes that this declaration has already been loaded, |
| 1178 | /// so that future GetDecl calls will return this declaration rather |
| 1179 | /// than trying to load a new declaration. |
| 1180 | inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) { |
| 1181 | assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?"); |
| 1182 | DeclAlreadyLoaded[Index] = true; |
| 1183 | DeclOffsets[Index] = reinterpret_cast<uint64_t>(D); |
| 1184 | } |
| 1185 | |
| 1186 | /// \brief Read the declaration at the given offset from the PCH file. |
| 1187 | Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1188 | // Keep track of where we are in the stream, then jump back there |
| 1189 | // after reading this declaration. |
| 1190 | SavedStreamPosition SavedPosition(Stream); |
| 1191 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1192 | Decl *D = 0; |
| 1193 | Stream.JumpToBit(Offset); |
| 1194 | RecordData Record; |
| 1195 | unsigned Code = Stream.ReadCode(); |
| 1196 | unsigned Idx = 0; |
| 1197 | PCHDeclReader Reader(*this, Record, Idx); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1198 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1199 | switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) { |
| 1200 | case pch::DECL_TRANSLATION_UNIT: |
| 1201 | assert(Index == 0 && "Translation unit must be at index 0"); |
| 1202 | Reader.VisitTranslationUnitDecl(Context.getTranslationUnitDecl()); |
| 1203 | D = Context.getTranslationUnitDecl(); |
| 1204 | LoadedDecl(Index, D); |
| 1205 | break; |
| 1206 | |
| 1207 | case pch::DECL_TYPEDEF: { |
| 1208 | TypedefDecl *Typedef = TypedefDecl::Create(Context, 0, SourceLocation(), |
| 1209 | 0, QualType()); |
| 1210 | LoadedDecl(Index, Typedef); |
| 1211 | Reader.VisitTypedefDecl(Typedef); |
| 1212 | D = Typedef; |
| 1213 | break; |
| 1214 | } |
| 1215 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1216 | case pch::DECL_ENUM: { |
| 1217 | EnumDecl *Enum = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0); |
| 1218 | LoadedDecl(Index, Enum); |
| 1219 | Reader.VisitEnumDecl(Enum); |
| 1220 | D = Enum; |
| 1221 | break; |
| 1222 | } |
| 1223 | |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1224 | case pch::DECL_RECORD: { |
| 1225 | RecordDecl *Record = RecordDecl::Create(Context, TagDecl::TK_struct, |
| 1226 | 0, SourceLocation(), 0, 0); |
| 1227 | LoadedDecl(Index, Record); |
| 1228 | Reader.VisitRecordDecl(Record); |
| 1229 | D = Record; |
| 1230 | break; |
| 1231 | } |
| 1232 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1233 | case pch::DECL_ENUM_CONSTANT: { |
| 1234 | EnumConstantDecl *ECD = EnumConstantDecl::Create(Context, 0, |
| 1235 | SourceLocation(), 0, |
| 1236 | QualType(), 0, |
| 1237 | llvm::APSInt()); |
| 1238 | LoadedDecl(Index, ECD); |
| 1239 | Reader.VisitEnumConstantDecl(ECD); |
| 1240 | D = ECD; |
| 1241 | break; |
| 1242 | } |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 1243 | |
| 1244 | case pch::DECL_FUNCTION: { |
| 1245 | FunctionDecl *Function = FunctionDecl::Create(Context, 0, SourceLocation(), |
| 1246 | DeclarationName(), |
| 1247 | QualType()); |
| 1248 | LoadedDecl(Index, Function); |
| 1249 | Reader.VisitFunctionDecl(Function); |
| 1250 | D = Function; |
| 1251 | break; |
| 1252 | } |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1253 | |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1254 | case pch::DECL_FIELD: { |
| 1255 | FieldDecl *Field = FieldDecl::Create(Context, 0, SourceLocation(), 0, |
| 1256 | QualType(), 0, false); |
| 1257 | LoadedDecl(Index, Field); |
| 1258 | Reader.VisitFieldDecl(Field); |
| 1259 | D = Field; |
| 1260 | break; |
| 1261 | } |
| 1262 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1263 | case pch::DECL_VAR: { |
| 1264 | VarDecl *Var = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(), |
| 1265 | VarDecl::None, SourceLocation()); |
| 1266 | LoadedDecl(Index, Var); |
| 1267 | Reader.VisitVarDecl(Var); |
| 1268 | D = Var; |
| 1269 | break; |
| 1270 | } |
| 1271 | |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 1272 | case pch::DECL_PARM_VAR: { |
| 1273 | ParmVarDecl *Parm = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, |
| 1274 | QualType(), VarDecl::None, 0); |
| 1275 | LoadedDecl(Index, Parm); |
| 1276 | Reader.VisitParmVarDecl(Parm); |
| 1277 | D = Parm; |
| 1278 | break; |
| 1279 | } |
| 1280 | |
| 1281 | case pch::DECL_ORIGINAL_PARM_VAR: { |
| 1282 | OriginalParmVarDecl *Parm |
| 1283 | = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0, |
| 1284 | QualType(), QualType(), VarDecl::None, |
| 1285 | 0); |
| 1286 | LoadedDecl(Index, Parm); |
| 1287 | Reader.VisitOriginalParmVarDecl(Parm); |
| 1288 | D = Parm; |
| 1289 | break; |
| 1290 | } |
| 1291 | |
Douglas Gregor | 2a49179 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 1292 | case pch::DECL_FILE_SCOPE_ASM: { |
| 1293 | FileScopeAsmDecl *Asm = FileScopeAsmDecl::Create(Context, 0, |
| 1294 | SourceLocation(), 0); |
| 1295 | LoadedDecl(Index, Asm); |
| 1296 | Reader.VisitFileScopeAsmDecl(Asm); |
| 1297 | D = Asm; |
| 1298 | break; |
| 1299 | } |
| 1300 | |
| 1301 | case pch::DECL_BLOCK: { |
| 1302 | BlockDecl *Block = BlockDecl::Create(Context, 0, SourceLocation()); |
| 1303 | LoadedDecl(Index, Block); |
| 1304 | Reader.VisitBlockDecl(Block); |
| 1305 | D = Block; |
| 1306 | break; |
| 1307 | } |
| 1308 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1309 | default: |
| 1310 | assert(false && "Cannot de-serialize this kind of declaration"); |
| 1311 | break; |
| 1312 | } |
| 1313 | |
| 1314 | // If this declaration is also a declaration context, get the |
| 1315 | // offsets for its tables of lexical and visible declarations. |
| 1316 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) { |
| 1317 | std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC); |
| 1318 | if (Offsets.first || Offsets.second) { |
| 1319 | DC->setHasExternalLexicalStorage(Offsets.first != 0); |
| 1320 | DC->setHasExternalVisibleStorage(Offsets.second != 0); |
| 1321 | DeclContextOffsets[DC] = Offsets; |
| 1322 | } |
| 1323 | } |
| 1324 | assert(Idx == Record.size()); |
| 1325 | |
| 1326 | return D; |
| 1327 | } |
| 1328 | |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1329 | QualType PCHReader::GetType(pch::TypeID ID) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1330 | unsigned Quals = ID & 0x07; |
| 1331 | unsigned Index = ID >> 3; |
| 1332 | |
| 1333 | if (Index < pch::NUM_PREDEF_TYPE_IDS) { |
| 1334 | QualType T; |
| 1335 | switch ((pch::PredefinedTypeIDs)Index) { |
| 1336 | case pch::PREDEF_TYPE_NULL_ID: return QualType(); |
| 1337 | case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; |
| 1338 | case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; |
| 1339 | |
| 1340 | case pch::PREDEF_TYPE_CHAR_U_ID: |
| 1341 | case pch::PREDEF_TYPE_CHAR_S_ID: |
| 1342 | // FIXME: Check that the signedness of CharTy is correct! |
| 1343 | T = Context.CharTy; |
| 1344 | break; |
| 1345 | |
| 1346 | case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; |
| 1347 | case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; |
| 1348 | case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; |
| 1349 | case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; |
| 1350 | case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; |
| 1351 | case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; |
| 1352 | case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; |
| 1353 | case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; |
| 1354 | case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break; |
| 1355 | case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; |
| 1356 | case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; |
| 1357 | case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; |
| 1358 | case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; |
| 1359 | case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; |
| 1360 | case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; |
| 1361 | case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; |
| 1362 | } |
| 1363 | |
| 1364 | assert(!T.isNull() && "Unknown predefined type"); |
| 1365 | return T.getQualifiedType(Quals); |
| 1366 | } |
| 1367 | |
| 1368 | Index -= pch::NUM_PREDEF_TYPE_IDS; |
| 1369 | if (!TypeAlreadyLoaded[Index]) { |
| 1370 | // Load the type from the PCH file. |
| 1371 | TypeOffsets[Index] = reinterpret_cast<uint64_t>( |
| 1372 | ReadTypeRecord(TypeOffsets[Index]).getTypePtr()); |
| 1373 | TypeAlreadyLoaded[Index] = true; |
| 1374 | } |
| 1375 | |
| 1376 | return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals); |
| 1377 | } |
| 1378 | |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1379 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1380 | if (ID == 0) |
| 1381 | return 0; |
| 1382 | |
| 1383 | unsigned Index = ID - 1; |
| 1384 | if (DeclAlreadyLoaded[Index]) |
| 1385 | return reinterpret_cast<Decl *>(DeclOffsets[Index]); |
| 1386 | |
| 1387 | // Load the declaration from the PCH file. |
| 1388 | return ReadDeclRecord(DeclOffsets[Index], Index); |
| 1389 | } |
| 1390 | |
| 1391 | bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1392 | llvm::SmallVectorImpl<pch::DeclID> &Decls) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1393 | assert(DC->hasExternalLexicalStorage() && |
| 1394 | "DeclContext has no lexical decls in storage"); |
| 1395 | uint64_t Offset = DeclContextOffsets[DC].first; |
| 1396 | assert(Offset && "DeclContext has no lexical decls in storage"); |
| 1397 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1398 | // Keep track of where we are in the stream, then jump back there |
| 1399 | // after reading this context. |
| 1400 | SavedStreamPosition SavedPosition(Stream); |
| 1401 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1402 | // Load the record containing all of the declarations lexically in |
| 1403 | // this context. |
| 1404 | Stream.JumpToBit(Offset); |
| 1405 | RecordData Record; |
| 1406 | unsigned Code = Stream.ReadCode(); |
| 1407 | unsigned RecCode = Stream.ReadRecord(Code, Record); |
| 1408 | assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block"); |
| 1409 | |
| 1410 | // Load all of the declaration IDs |
| 1411 | Decls.clear(); |
| 1412 | Decls.insert(Decls.end(), Record.begin(), Record.end()); |
| 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, |
| 1417 | llvm::SmallVectorImpl<VisibleDeclaration> & Decls) { |
| 1418 | assert(DC->hasExternalVisibleStorage() && |
| 1419 | "DeclContext has no visible decls in storage"); |
| 1420 | uint64_t Offset = DeclContextOffsets[DC].second; |
| 1421 | assert(Offset && "DeclContext has no visible decls in storage"); |
| 1422 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1423 | // Keep track of where we are in the stream, then jump back there |
| 1424 | // after reading this context. |
| 1425 | SavedStreamPosition SavedPosition(Stream); |
| 1426 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1427 | // Load the record containing all of the declarations visible in |
| 1428 | // this context. |
| 1429 | Stream.JumpToBit(Offset); |
| 1430 | RecordData Record; |
| 1431 | unsigned Code = Stream.ReadCode(); |
| 1432 | unsigned RecCode = Stream.ReadRecord(Code, Record); |
| 1433 | assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block"); |
| 1434 | if (Record.size() == 0) |
| 1435 | return false; |
| 1436 | |
| 1437 | Decls.clear(); |
| 1438 | |
| 1439 | unsigned Idx = 0; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1440 | while (Idx < Record.size()) { |
| 1441 | Decls.push_back(VisibleDeclaration()); |
| 1442 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 1443 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1444 | unsigned Size = Record[Idx++]; |
| 1445 | llvm::SmallVector<unsigned, 4> & LoadedDecls |
| 1446 | = Decls.back().Declarations; |
| 1447 | LoadedDecls.reserve(Size); |
| 1448 | for (unsigned I = 0; I < Size; ++I) |
| 1449 | LoadedDecls.push_back(Record[Idx++]); |
| 1450 | } |
| 1451 | |
| 1452 | return false; |
| 1453 | } |
| 1454 | |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1455 | void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { |
| 1456 | if (!Consumer) |
| 1457 | return; |
| 1458 | |
| 1459 | for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { |
| 1460 | Decl *D = GetDecl(ExternalDefinitions[I]); |
| 1461 | DeclGroupRef DG(D); |
| 1462 | Consumer->HandleTopLevelDecl(DG); |
| 1463 | } |
| 1464 | } |
| 1465 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1466 | void PCHReader::PrintStats() { |
| 1467 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 1468 | |
| 1469 | unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(), |
| 1470 | TypeAlreadyLoaded.end(), |
| 1471 | true); |
| 1472 | unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(), |
| 1473 | DeclAlreadyLoaded.end(), |
| 1474 | true); |
Douglas Gregor | 9cf4742 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 1475 | unsigned NumIdentifiersLoaded = 0; |
| 1476 | for (unsigned I = 0; I < IdentifierData.size(); ++I) { |
| 1477 | if ((IdentifierData[I] & 0x01) == 0) |
| 1478 | ++NumIdentifiersLoaded; |
| 1479 | } |
| 1480 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1481 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
| 1482 | NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(), |
Douglas Gregor | 9cf4742 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 1483 | ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100)); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1484 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
| 1485 | NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(), |
Douglas Gregor | 9cf4742 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 1486 | ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100)); |
| 1487 | std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", |
| 1488 | NumIdentifiersLoaded, (unsigned)IdentifierData.size(), |
| 1489 | ((float)NumIdentifiersLoaded/IdentifierData.size() * 100)); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1490 | std::fprintf(stderr, "\n"); |
| 1491 | } |
| 1492 | |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1493 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1494 | if (ID == 0) |
| 1495 | return 0; |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1496 | |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1497 | if (!IdentifierTable || IdentifierData.empty()) { |
| 1498 | Error("No identifier table in PCH file"); |
| 1499 | return 0; |
| 1500 | } |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1502 | if (IdentifierData[ID - 1] & 0x01) { |
| 1503 | uint64_t Offset = IdentifierData[ID - 1]; |
| 1504 | IdentifierData[ID - 1] = reinterpret_cast<uint64_t>( |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1505 | &Context.Idents.get(IdentifierTable + Offset)); |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1506 | } |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1507 | |
| 1508 | return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | DeclarationName |
| 1512 | PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { |
| 1513 | DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; |
| 1514 | switch (Kind) { |
| 1515 | case DeclarationName::Identifier: |
| 1516 | return DeclarationName(GetIdentifierInfo(Record, Idx)); |
| 1517 | |
| 1518 | case DeclarationName::ObjCZeroArgSelector: |
| 1519 | case DeclarationName::ObjCOneArgSelector: |
| 1520 | case DeclarationName::ObjCMultiArgSelector: |
| 1521 | assert(false && "Unable to de-serialize Objective-C selectors"); |
| 1522 | break; |
| 1523 | |
| 1524 | case DeclarationName::CXXConstructorName: |
| 1525 | return Context.DeclarationNames.getCXXConstructorName( |
| 1526 | GetType(Record[Idx++])); |
| 1527 | |
| 1528 | case DeclarationName::CXXDestructorName: |
| 1529 | return Context.DeclarationNames.getCXXDestructorName( |
| 1530 | GetType(Record[Idx++])); |
| 1531 | |
| 1532 | case DeclarationName::CXXConversionFunctionName: |
| 1533 | return Context.DeclarationNames.getCXXConversionFunctionName( |
| 1534 | GetType(Record[Idx++])); |
| 1535 | |
| 1536 | case DeclarationName::CXXOperatorName: |
| 1537 | return Context.DeclarationNames.getCXXOperatorName( |
| 1538 | (OverloadedOperatorKind)Record[Idx++]); |
| 1539 | |
| 1540 | case DeclarationName::CXXUsingDirective: |
| 1541 | return DeclarationName::getUsingDirectiveName(); |
| 1542 | } |
| 1543 | |
| 1544 | // Required to silence GCC warning |
| 1545 | return DeclarationName(); |
| 1546 | } |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1548 | /// \brief Read an integral value |
| 1549 | llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { |
| 1550 | unsigned BitWidth = Record[Idx++]; |
| 1551 | unsigned NumWords = llvm::APInt::getNumWords(BitWidth); |
| 1552 | llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); |
| 1553 | Idx += NumWords; |
| 1554 | return Result; |
| 1555 | } |
| 1556 | |
| 1557 | /// \brief Read a signed integral value |
| 1558 | llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { |
| 1559 | bool isUnsigned = Record[Idx++]; |
| 1560 | return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); |
| 1561 | } |
| 1562 | |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1563 | /// \brief Read a floating-point value |
| 1564 | llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { |
| 1565 | // FIXME: is this really correct? |
| 1566 | return llvm::APFloat(ReadAPInt(Record, Idx)); |
| 1567 | } |
| 1568 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1569 | Expr *PCHReader::ReadExpr() { |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1570 | // Within the bitstream, expressions are stored in Reverse Polish |
| 1571 | // Notation, with each of the subexpressions preceding the |
| 1572 | // expression they are stored in. To evaluate expressions, we |
| 1573 | // continue reading expressions and placing them on the stack, with |
| 1574 | // expressions having operands removing those operands from the |
| 1575 | // stack. Evaluation terminates when we see a EXPR_STOP record, and |
| 1576 | // the single remaining expression on the stack is our result. |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1577 | RecordData Record; |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1578 | unsigned Idx; |
| 1579 | llvm::SmallVector<Expr *, 16> ExprStack; |
| 1580 | PCHStmtReader Reader(*this, Record, Idx, ExprStack); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1581 | Stmt::EmptyShell Empty; |
| 1582 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1583 | while (true) { |
| 1584 | unsigned Code = Stream.ReadCode(); |
| 1585 | if (Code == llvm::bitc::END_BLOCK) { |
| 1586 | if (Stream.ReadBlockEnd()) { |
| 1587 | Error("Error at end of Source Manager block"); |
| 1588 | return 0; |
| 1589 | } |
| 1590 | break; |
| 1591 | } |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1592 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1593 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1594 | // No known subblocks, always skip them. |
| 1595 | Stream.ReadSubBlockID(); |
| 1596 | if (Stream.SkipBlock()) { |
| 1597 | Error("Malformed block record"); |
| 1598 | return 0; |
| 1599 | } |
| 1600 | continue; |
| 1601 | } |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1603 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1604 | Stream.ReadAbbrevRecord(); |
| 1605 | continue; |
| 1606 | } |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1608 | Expr *E = 0; |
| 1609 | Idx = 0; |
| 1610 | Record.clear(); |
| 1611 | bool Finished = false; |
| 1612 | switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) { |
| 1613 | case pch::EXPR_STOP: |
| 1614 | Finished = true; |
| 1615 | break; |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1616 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1617 | case pch::EXPR_NULL: |
| 1618 | E = 0; |
| 1619 | break; |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1620 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1621 | case pch::EXPR_PREDEFINED: |
| 1622 | // FIXME: untested (until we can serialize function bodies). |
| 1623 | E = new (Context) PredefinedExpr(Empty); |
| 1624 | break; |
| 1625 | |
| 1626 | case pch::EXPR_DECL_REF: |
| 1627 | E = new (Context) DeclRefExpr(Empty); |
| 1628 | break; |
| 1629 | |
| 1630 | case pch::EXPR_INTEGER_LITERAL: |
| 1631 | E = new (Context) IntegerLiteral(Empty); |
| 1632 | break; |
| 1633 | |
| 1634 | case pch::EXPR_FLOATING_LITERAL: |
| 1635 | E = new (Context) FloatingLiteral(Empty); |
| 1636 | break; |
| 1637 | |
| 1638 | case pch::EXPR_CHARACTER_LITERAL: |
| 1639 | E = new (Context) CharacterLiteral(Empty); |
| 1640 | break; |
| 1641 | |
Douglas Gregor | 4ea0b1f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 1642 | case pch::EXPR_PAREN: |
| 1643 | E = new (Context) ParenExpr(Empty); |
| 1644 | break; |
| 1645 | |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame^] | 1646 | case pch::EXPR_BINARY_OPERATOR: |
| 1647 | E = new (Context) BinaryOperator(Empty); |
| 1648 | break; |
| 1649 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1650 | case pch::EXPR_IMPLICIT_CAST: |
| 1651 | E = new (Context) ImplicitCastExpr(Empty); |
| 1652 | break; |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame^] | 1653 | |
| 1654 | case pch::EXPR_CSTYLE_CAST: |
| 1655 | E = new (Context) CStyleCastExpr(Empty); |
| 1656 | break; |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | // We hit an EXPR_STOP, so we're done with this expression. |
| 1660 | if (Finished) |
| 1661 | break; |
| 1662 | |
| 1663 | if (E) { |
| 1664 | unsigned NumSubExprs = Reader.Visit(E); |
| 1665 | while (NumSubExprs > 0) { |
| 1666 | ExprStack.pop_back(); |
| 1667 | --NumSubExprs; |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | assert(Idx == Record.size() && "Invalid deserialization of expression"); |
| 1672 | ExprStack.push_back(E); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1673 | } |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1674 | assert(ExprStack.size() == 1 && "Extra expressions on stack!"); |
| 1675 | return ExprStack.back(); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1678 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1679 | return Diag(SourceLocation(), DiagID); |
| 1680 | } |
| 1681 | |
| 1682 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
| 1683 | return PP.getDiagnostics().Report(FullSourceLoc(Loc, |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1684 | Context.getSourceManager()), |
| 1685 | DiagID); |
| 1686 | } |