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