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