Chris Lattner | 698f925 | 2009-04-27 05:27:42 +0000 | [diff] [blame^] | 1 | //===--- PCHReaderDecl.cpp - Decl Deserialization ---------------*- 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 implements the PCHReader::ReadDeclRecord method, which is the |
| 11 | // entrypoint for loading a decl. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Frontend/PCHReader.h" |
| 16 | #include "clang/AST/ASTConsumer.h" |
| 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/DeclVisitor.h" |
| 19 | #include "clang/AST/DeclGroup.h" |
| 20 | #include "clang/AST/Expr.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | // FIXME: Temporarily cloned from PCHReader.cpp. |
| 24 | namespace { |
| 25 | /// \brief Helper class that saves the current stream position and |
| 26 | /// then restores it when destroyed. |
| 27 | struct SavedStreamPosition { |
| 28 | explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor) |
| 29 | : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { } |
| 30 | |
| 31 | ~SavedStreamPosition() { |
| 32 | Cursor.JumpToBit(Offset); |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | llvm::BitstreamCursor &Cursor; |
| 37 | uint64_t Offset; |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | // Declaration deserialization |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
| 46 | namespace { |
| 47 | class PCHDeclReader : public DeclVisitor<PCHDeclReader, void> { |
| 48 | PCHReader &Reader; |
| 49 | const PCHReader::RecordData &Record; |
| 50 | unsigned &Idx; |
| 51 | |
| 52 | public: |
| 53 | PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record, |
| 54 | unsigned &Idx) |
| 55 | : Reader(Reader), Record(Record), Idx(Idx) { } |
| 56 | |
| 57 | void VisitDecl(Decl *D); |
| 58 | void VisitTranslationUnitDecl(TranslationUnitDecl *TU); |
| 59 | void VisitNamedDecl(NamedDecl *ND); |
| 60 | void VisitTypeDecl(TypeDecl *TD); |
| 61 | void VisitTypedefDecl(TypedefDecl *TD); |
| 62 | void VisitTagDecl(TagDecl *TD); |
| 63 | void VisitEnumDecl(EnumDecl *ED); |
| 64 | void VisitRecordDecl(RecordDecl *RD); |
| 65 | void VisitValueDecl(ValueDecl *VD); |
| 66 | void VisitEnumConstantDecl(EnumConstantDecl *ECD); |
| 67 | void VisitFunctionDecl(FunctionDecl *FD); |
| 68 | void VisitFieldDecl(FieldDecl *FD); |
| 69 | void VisitVarDecl(VarDecl *VD); |
| 70 | void VisitImplicitParamDecl(ImplicitParamDecl *PD); |
| 71 | void VisitParmVarDecl(ParmVarDecl *PD); |
| 72 | void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD); |
| 73 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD); |
| 74 | void VisitBlockDecl(BlockDecl *BD); |
| 75 | std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC); |
| 76 | void VisitObjCMethodDecl(ObjCMethodDecl *D); |
| 77 | void VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 78 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 79 | void VisitObjCIvarDecl(ObjCIvarDecl *D); |
| 80 | void VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 81 | void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D); |
| 82 | void VisitObjCClassDecl(ObjCClassDecl *D); |
| 83 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 84 | void VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 85 | void VisitObjCImplDecl(ObjCImplDecl *D); |
| 86 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 87 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 88 | void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); |
| 89 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 90 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | void PCHDeclReader::VisitDecl(Decl *D) { |
| 95 | D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]))); |
| 96 | D->setLexicalDeclContext( |
| 97 | cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]))); |
| 98 | D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 99 | D->setInvalidDecl(Record[Idx++]); |
| 100 | if (Record[Idx++]) |
| 101 | D->addAttr(Reader.ReadAttributes()); |
| 102 | D->setImplicit(Record[Idx++]); |
| 103 | D->setAccess((AccessSpecifier)Record[Idx++]); |
| 104 | } |
| 105 | |
| 106 | void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { |
| 107 | VisitDecl(TU); |
| 108 | } |
| 109 | |
| 110 | void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) { |
| 111 | VisitDecl(ND); |
| 112 | ND->setDeclName(Reader.ReadDeclarationName(Record, Idx)); |
| 113 | } |
| 114 | |
| 115 | void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) { |
| 116 | VisitNamedDecl(TD); |
| 117 | TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr()); |
| 118 | } |
| 119 | |
| 120 | void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) { |
| 121 | // Note that we cannot use VisitTypeDecl here, because we need to |
| 122 | // set the underlying type of the typedef *before* we try to read |
| 123 | // the type associated with the TypedefDecl. |
| 124 | VisitNamedDecl(TD); |
| 125 | TD->setUnderlyingType(Reader.GetType(Record[Idx + 1])); |
| 126 | TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr()); |
| 127 | Idx += 2; |
| 128 | } |
| 129 | |
| 130 | void PCHDeclReader::VisitTagDecl(TagDecl *TD) { |
| 131 | VisitTypeDecl(TD); |
| 132 | TD->setTagKind((TagDecl::TagKind)Record[Idx++]); |
| 133 | TD->setDefinition(Record[Idx++]); |
| 134 | TD->setTypedefForAnonDecl( |
| 135 | cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++]))); |
| 136 | } |
| 137 | |
| 138 | void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) { |
| 139 | VisitTagDecl(ED); |
| 140 | ED->setIntegerType(Reader.GetType(Record[Idx++])); |
| 141 | } |
| 142 | |
| 143 | void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) { |
| 144 | VisitTagDecl(RD); |
| 145 | RD->setHasFlexibleArrayMember(Record[Idx++]); |
| 146 | RD->setAnonymousStructOrUnion(Record[Idx++]); |
| 147 | } |
| 148 | |
| 149 | void PCHDeclReader::VisitValueDecl(ValueDecl *VD) { |
| 150 | VisitNamedDecl(VD); |
| 151 | VD->setType(Reader.GetType(Record[Idx++])); |
| 152 | } |
| 153 | |
| 154 | void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) { |
| 155 | VisitValueDecl(ECD); |
| 156 | if (Record[Idx++]) |
| 157 | ECD->setInitExpr(Reader.ReadExpr()); |
| 158 | ECD->setInitVal(Reader.ReadAPSInt(Record, Idx)); |
| 159 | } |
| 160 | |
| 161 | void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) { |
| 162 | VisitValueDecl(FD); |
| 163 | if (Record[Idx++]) |
| 164 | FD->setLazyBody(Reader.getStream().GetCurrentBitNo()); |
| 165 | FD->setPreviousDeclaration( |
| 166 | cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]))); |
| 167 | FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]); |
| 168 | FD->setInline(Record[Idx++]); |
| 169 | FD->setC99InlineDefinition(Record[Idx++]); |
| 170 | FD->setVirtual(Record[Idx++]); |
| 171 | FD->setPure(Record[Idx++]); |
| 172 | FD->setInheritedPrototype(Record[Idx++]); |
| 173 | FD->setHasPrototype(Record[Idx++]); |
| 174 | FD->setDeleted(Record[Idx++]); |
| 175 | FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 176 | unsigned NumParams = Record[Idx++]; |
| 177 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 178 | Params.reserve(NumParams); |
| 179 | for (unsigned I = 0; I != NumParams; ++I) |
| 180 | Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 181 | FD->setParams(Reader.getContext(), &Params[0], NumParams); |
| 182 | } |
| 183 | |
| 184 | void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) { |
| 185 | VisitNamedDecl(MD); |
| 186 | if (Record[Idx++]) { |
| 187 | // In practice, this won't be executed (since method definitions |
| 188 | // don't occur in header files). |
| 189 | MD->setBody(Reader.ReadStmt()); |
| 190 | MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++]))); |
| 191 | MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++]))); |
| 192 | } |
| 193 | MD->setInstanceMethod(Record[Idx++]); |
| 194 | MD->setVariadic(Record[Idx++]); |
| 195 | MD->setSynthesized(Record[Idx++]); |
| 196 | MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]); |
| 197 | MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]); |
| 198 | MD->setResultType(Reader.GetType(Record[Idx++])); |
| 199 | MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 200 | unsigned NumParams = Record[Idx++]; |
| 201 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 202 | Params.reserve(NumParams); |
| 203 | for (unsigned I = 0; I != NumParams; ++I) |
| 204 | Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 205 | MD->setMethodParams(Reader.getContext(), &Params[0], NumParams); |
| 206 | } |
| 207 | |
| 208 | void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) { |
| 209 | VisitNamedDecl(CD); |
| 210 | CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 211 | } |
| 212 | |
| 213 | void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) { |
| 214 | VisitObjCContainerDecl(ID); |
| 215 | ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr()); |
| 216 | ID->setSuperClass(cast_or_null<ObjCInterfaceDecl> |
| 217 | (Reader.GetDecl(Record[Idx++]))); |
| 218 | unsigned NumProtocols = Record[Idx++]; |
| 219 | llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols; |
| 220 | Protocols.reserve(NumProtocols); |
| 221 | for (unsigned I = 0; I != NumProtocols; ++I) |
| 222 | Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++]))); |
| 223 | ID->setProtocolList(&Protocols[0], NumProtocols, Reader.getContext()); |
| 224 | unsigned NumIvars = Record[Idx++]; |
| 225 | llvm::SmallVector<ObjCIvarDecl *, 16> IVars; |
| 226 | IVars.reserve(NumIvars); |
| 227 | for (unsigned I = 0; I != NumIvars; ++I) |
| 228 | IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 229 | ID->setIVarList(&IVars[0], NumIvars, Reader.getContext()); |
| 230 | ID->setCategoryList( |
| 231 | cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++]))); |
| 232 | ID->setForwardDecl(Record[Idx++]); |
| 233 | ID->setImplicitInterfaceDecl(Record[Idx++]); |
| 234 | ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 235 | ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 236 | ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 237 | } |
| 238 | |
| 239 | void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) { |
| 240 | VisitFieldDecl(IVD); |
| 241 | IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]); |
| 242 | } |
| 243 | |
| 244 | void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) { |
| 245 | VisitObjCContainerDecl(PD); |
| 246 | PD->setForwardDecl(Record[Idx++]); |
| 247 | PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 248 | unsigned NumProtoRefs = Record[Idx++]; |
| 249 | llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs; |
| 250 | ProtoRefs.reserve(NumProtoRefs); |
| 251 | for (unsigned I = 0; I != NumProtoRefs; ++I) |
| 252 | ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++]))); |
| 253 | PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext()); |
| 254 | } |
| 255 | |
| 256 | void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) { |
| 257 | VisitFieldDecl(FD); |
| 258 | } |
| 259 | |
| 260 | void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) { |
| 261 | VisitDecl(CD); |
| 262 | unsigned NumClassRefs = Record[Idx++]; |
| 263 | llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs; |
| 264 | ClassRefs.reserve(NumClassRefs); |
| 265 | for (unsigned I = 0; I != NumClassRefs; ++I) |
| 266 | ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]))); |
| 267 | CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs); |
| 268 | } |
| 269 | |
| 270 | void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) { |
| 271 | VisitDecl(FPD); |
| 272 | unsigned NumProtoRefs = Record[Idx++]; |
| 273 | llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs; |
| 274 | ProtoRefs.reserve(NumProtoRefs); |
| 275 | for (unsigned I = 0; I != NumProtoRefs; ++I) |
| 276 | ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++]))); |
| 277 | FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext()); |
| 278 | } |
| 279 | |
| 280 | void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) { |
| 281 | VisitObjCContainerDecl(CD); |
| 282 | CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]))); |
| 283 | unsigned NumProtoRefs = Record[Idx++]; |
| 284 | llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs; |
| 285 | ProtoRefs.reserve(NumProtoRefs); |
| 286 | for (unsigned I = 0; I != NumProtoRefs; ++I) |
| 287 | ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++]))); |
| 288 | CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext()); |
| 289 | CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++]))); |
| 290 | CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 291 | } |
| 292 | |
| 293 | void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) { |
| 294 | VisitNamedDecl(CAD); |
| 295 | CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]))); |
| 296 | } |
| 297 | |
| 298 | void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 299 | VisitNamedDecl(D); |
| 300 | D->setType(Reader.GetType(Record[Idx++])); |
| 301 | // FIXME: stable encoding |
| 302 | D->setPropertyAttributes( |
| 303 | (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]); |
| 304 | // FIXME: stable encoding |
| 305 | D->setPropertyImplementation( |
| 306 | (ObjCPropertyDecl::PropertyControl)Record[Idx++]); |
| 307 | D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector()); |
| 308 | D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector()); |
| 309 | D->setGetterMethodDecl( |
| 310 | cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++]))); |
| 311 | D->setSetterMethodDecl( |
| 312 | cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++]))); |
| 313 | D->setPropertyIvarDecl( |
| 314 | cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 315 | } |
| 316 | |
| 317 | void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 318 | VisitNamedDecl(D); |
| 319 | D->setClassInterface( |
| 320 | cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]))); |
| 321 | D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 322 | } |
| 323 | |
| 324 | void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 325 | VisitObjCImplDecl(D); |
| 326 | D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx)); |
| 327 | } |
| 328 | |
| 329 | void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 330 | VisitObjCImplDecl(D); |
| 331 | D->setSuperClass( |
| 332 | cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]))); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 337 | VisitDecl(D); |
| 338 | D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 339 | D->setPropertyDecl( |
| 340 | cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++]))); |
| 341 | D->setPropertyIvarDecl( |
| 342 | cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 343 | } |
| 344 | |
| 345 | void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) { |
| 346 | VisitValueDecl(FD); |
| 347 | FD->setMutable(Record[Idx++]); |
| 348 | if (Record[Idx++]) |
| 349 | FD->setBitWidth(Reader.ReadExpr()); |
| 350 | } |
| 351 | |
| 352 | void PCHDeclReader::VisitVarDecl(VarDecl *VD) { |
| 353 | VisitValueDecl(VD); |
| 354 | VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]); |
| 355 | VD->setThreadSpecified(Record[Idx++]); |
| 356 | VD->setCXXDirectInitializer(Record[Idx++]); |
| 357 | VD->setDeclaredInCondition(Record[Idx++]); |
| 358 | VD->setPreviousDeclaration( |
| 359 | cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 360 | VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 361 | if (Record[Idx++]) |
| 362 | VD->setInit(Reader.ReadExpr()); |
| 363 | } |
| 364 | |
| 365 | void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) { |
| 366 | VisitVarDecl(PD); |
| 367 | } |
| 368 | |
| 369 | void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) { |
| 370 | VisitVarDecl(PD); |
| 371 | PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]); |
| 372 | // FIXME: default argument (C++ only) |
| 373 | } |
| 374 | |
| 375 | void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) { |
| 376 | VisitParmVarDecl(PD); |
| 377 | PD->setOriginalType(Reader.GetType(Record[Idx++])); |
| 378 | } |
| 379 | |
| 380 | void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) { |
| 381 | VisitDecl(AD); |
| 382 | AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr())); |
| 383 | } |
| 384 | |
| 385 | void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) { |
| 386 | VisitDecl(BD); |
| 387 | BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt())); |
| 388 | unsigned NumParams = Record[Idx++]; |
| 389 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 390 | Params.reserve(NumParams); |
| 391 | for (unsigned I = 0; I != NumParams; ++I) |
| 392 | Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 393 | BD->setParams(Reader.getContext(), &Params[0], NumParams); |
| 394 | } |
| 395 | |
| 396 | std::pair<uint64_t, uint64_t> |
| 397 | PCHDeclReader::VisitDeclContext(DeclContext *DC) { |
| 398 | uint64_t LexicalOffset = Record[Idx++]; |
| 399 | uint64_t VisibleOffset = Record[Idx++]; |
| 400 | return std::make_pair(LexicalOffset, VisibleOffset); |
| 401 | } |
| 402 | |
| 403 | //===----------------------------------------------------------------------===// |
| 404 | // PCHReader Implementation |
| 405 | //===----------------------------------------------------------------------===// |
| 406 | |
| 407 | |
| 408 | /// \brief Note that we have loaded the declaration with the given |
| 409 | /// Index. |
| 410 | /// |
| 411 | /// This routine notes that this declaration has already been loaded, |
| 412 | /// so that future GetDecl calls will return this declaration rather |
| 413 | /// than trying to load a new declaration. |
| 414 | inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) { |
| 415 | assert(!DeclsLoaded[Index] && "Decl loaded twice?"); |
| 416 | DeclsLoaded[Index] = D; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | /// \brief Determine whether the consumer will be interested in seeing |
| 421 | /// this declaration (via HandleTopLevelDecl). |
| 422 | /// |
| 423 | /// This routine should return true for anything that might affect |
| 424 | /// code generation, e.g., inline function definitions, Objective-C |
| 425 | /// declarations with metadata, etc. |
| 426 | static bool isConsumerInterestedIn(Decl *D) { |
| 427 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 428 | return Var->isFileVarDecl() && Var->getInit(); |
| 429 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) |
| 430 | return Func->isThisDeclarationADefinition(); |
| 431 | return isa<ObjCProtocolDecl>(D); |
| 432 | } |
| 433 | |
| 434 | /// \brief Read the declaration at the given offset from the PCH file. |
| 435 | Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { |
| 436 | // Keep track of where we are in the stream, then jump back there |
| 437 | // after reading this declaration. |
| 438 | SavedStreamPosition SavedPosition(Stream); |
| 439 | |
| 440 | Decl *D = 0; |
| 441 | Stream.JumpToBit(Offset); |
| 442 | RecordData Record; |
| 443 | unsigned Code = Stream.ReadCode(); |
| 444 | unsigned Idx = 0; |
| 445 | PCHDeclReader Reader(*this, Record, Idx); |
| 446 | |
| 447 | switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) { |
| 448 | case pch::DECL_ATTR: |
| 449 | case pch::DECL_CONTEXT_LEXICAL: |
| 450 | case pch::DECL_CONTEXT_VISIBLE: |
| 451 | assert(false && "Record cannot be de-serialized with ReadDeclRecord"); |
| 452 | break; |
| 453 | |
| 454 | case pch::DECL_TRANSLATION_UNIT: |
| 455 | assert(Index == 0 && "Translation unit must be at index 0"); |
| 456 | D = Context.getTranslationUnitDecl(); |
| 457 | break; |
| 458 | |
| 459 | case pch::DECL_TYPEDEF: { |
| 460 | D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType()); |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | case pch::DECL_ENUM: { |
| 465 | D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0); |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | case pch::DECL_RECORD: { |
| 470 | D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(), |
| 471 | 0, 0); |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | case pch::DECL_ENUM_CONSTANT: { |
| 476 | D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(), |
| 477 | 0, llvm::APSInt()); |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | case pch::DECL_FUNCTION: { |
| 482 | D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(), |
| 483 | QualType()); |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | case pch::DECL_OBJC_METHOD: { |
| 488 | D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(), |
| 489 | Selector(), QualType(), 0); |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | case pch::DECL_OBJC_INTERFACE: { |
| 494 | D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0); |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | case pch::DECL_OBJC_IVAR: { |
| 499 | D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(), |
| 500 | ObjCIvarDecl::None); |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | case pch::DECL_OBJC_PROTOCOL: { |
| 505 | D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0); |
| 506 | break; |
| 507 | } |
| 508 | |
| 509 | case pch::DECL_OBJC_AT_DEFS_FIELD: { |
| 510 | D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0, |
| 511 | QualType(), 0); |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | case pch::DECL_OBJC_CLASS: { |
| 516 | D = ObjCClassDecl::Create(Context, 0, SourceLocation()); |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | case pch::DECL_OBJC_FORWARD_PROTOCOL: { |
| 521 | D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation()); |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | case pch::DECL_OBJC_CATEGORY: { |
| 526 | D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0); |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | case pch::DECL_OBJC_CATEGORY_IMPL: { |
| 531 | D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0); |
| 532 | break; |
| 533 | } |
| 534 | |
| 535 | case pch::DECL_OBJC_IMPLEMENTATION: { |
| 536 | D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0); |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | case pch::DECL_OBJC_COMPATIBLE_ALIAS: { |
| 541 | D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0); |
| 542 | break; |
| 543 | } |
| 544 | |
| 545 | case pch::DECL_OBJC_PROPERTY: { |
| 546 | D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType()); |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | case pch::DECL_OBJC_PROPERTY_IMPL: { |
| 551 | D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(), |
| 552 | SourceLocation(), 0, |
| 553 | ObjCPropertyImplDecl::Dynamic, 0); |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | case pch::DECL_FIELD: { |
| 558 | D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0, |
| 559 | false); |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | case pch::DECL_VAR: |
| 564 | D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(), |
| 565 | VarDecl::None, SourceLocation()); |
| 566 | break; |
| 567 | |
| 568 | case pch::DECL_IMPLICIT_PARAM: |
| 569 | D = ImplicitParamDecl::Create(Context, 0, SourceLocation(), 0, QualType()); |
| 570 | break; |
| 571 | |
| 572 | case pch::DECL_PARM_VAR: { |
| 573 | D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(), |
| 574 | VarDecl::None, 0); |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | case pch::DECL_ORIGINAL_PARM_VAR: { |
| 579 | D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0, |
| 580 | QualType(), QualType(), VarDecl::None, |
| 581 | 0); |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | case pch::DECL_FILE_SCOPE_ASM: { |
| 586 | D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0); |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | case pch::DECL_BLOCK: { |
| 591 | D = BlockDecl::Create(Context, 0, SourceLocation()); |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | assert(D && "Unknown declaration reading PCH file"); |
| 597 | if (D) { |
| 598 | LoadedDecl(Index, D); |
| 599 | Reader.Visit(D); |
| 600 | } |
| 601 | |
| 602 | // If this declaration is also a declaration context, get the |
| 603 | // offsets for its tables of lexical and visible declarations. |
| 604 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) { |
| 605 | std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC); |
| 606 | if (Offsets.first || Offsets.second) { |
| 607 | DC->setHasExternalLexicalStorage(Offsets.first != 0); |
| 608 | DC->setHasExternalVisibleStorage(Offsets.second != 0); |
| 609 | DeclContextOffsets[DC] = Offsets; |
| 610 | } |
| 611 | } |
| 612 | assert(Idx == Record.size()); |
| 613 | |
| 614 | // If we have deserialized a declaration that has a definition the |
| 615 | // AST consumer might need to know about, notify the consumer |
| 616 | // about that definition now or queue it for later. |
| 617 | if (isConsumerInterestedIn(D)) { |
| 618 | if (Consumer) { |
| 619 | DeclGroupRef DG(D); |
| 620 | Consumer->HandleTopLevelDecl(DG); |
| 621 | } else { |
| 622 | InterestingDecls.push_back(D); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | return D; |
| 627 | } |
| 628 | |