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