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