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