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