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