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