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