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