1 //===--- PCHReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PCHReader::ReadDeclRecord method, which is the
11 // entrypoint for loading a decl.
13 //===----------------------------------------------------------------------===//
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;
24 //===----------------------------------------------------------------------===//
25 // Declaration deserialization
26 //===----------------------------------------------------------------------===//
29 class PCHDeclReader : public DeclVisitor<PCHDeclReader, void> {
31 const PCHReader::RecordData &Record;
35 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
37 : Reader(Reader), Record(Record), Idx(Idx) { }
39 void VisitDecl(Decl *D);
40 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
41 void VisitNamedDecl(NamedDecl *ND);
42 void VisitTypeDecl(TypeDecl *TD);
43 void VisitTypedefDecl(TypedefDecl *TD);
44 void VisitTagDecl(TagDecl *TD);
45 void VisitEnumDecl(EnumDecl *ED);
46 void VisitRecordDecl(RecordDecl *RD);
47 void VisitValueDecl(ValueDecl *VD);
48 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
49 void VisitDeclaratorDecl(DeclaratorDecl *DD);
50 void VisitFunctionDecl(FunctionDecl *FD);
51 void VisitFieldDecl(FieldDecl *FD);
52 void VisitVarDecl(VarDecl *VD);
53 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
54 void VisitParmVarDecl(ParmVarDecl *PD);
55 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
56 void VisitBlockDecl(BlockDecl *BD);
57 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
58 void VisitObjCMethodDecl(ObjCMethodDecl *D);
59 void VisitObjCContainerDecl(ObjCContainerDecl *D);
60 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
61 void VisitObjCIvarDecl(ObjCIvarDecl *D);
62 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
63 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
64 void VisitObjCClassDecl(ObjCClassDecl *D);
65 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
66 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
67 void VisitObjCImplDecl(ObjCImplDecl *D);
68 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
69 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
70 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
71 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
72 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
76 void PCHDeclReader::VisitDecl(Decl *D) {
77 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
78 D->setLexicalDeclContext(
79 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
80 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
81 D->setInvalidDecl(Record[Idx++]);
83 D->addAttr(Reader.ReadAttributes());
84 D->setImplicit(Record[Idx++]);
85 D->setUsed(Record[Idx++]);
86 D->setAccess((AccessSpecifier)Record[Idx++]);
87 D->setPCHLevel(Record[Idx++] + 1);
90 void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
94 void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
96 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
99 void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
101 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
104 void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
105 // Note that we cannot use VisitTypeDecl here, because we need to
106 // set the underlying type of the typedef *before* we try to read
107 // the type associated with the TypedefDecl.
109 uint64_t TypeData = Record[Idx++];
110 TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx));
111 TD->setTypeForDecl(Reader.GetType(TypeData).getTypePtr());
114 void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
116 TD->setPreviousDeclaration(
117 cast_or_null<TagDecl>(Reader.GetDecl(Record[Idx++])));
118 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
119 TD->setDefinition(Record[Idx++]);
120 TD->setEmbeddedInDeclarator(Record[Idx++]);
121 TD->setTypedefForAnonDecl(
122 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
123 TD->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
124 TD->setTagKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
127 void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
129 ED->setIntegerType(Reader.GetType(Record[Idx++]));
130 ED->setPromotionType(Reader.GetType(Record[Idx++]));
131 // FIXME: C++ InstantiatedFrom
134 void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
136 RD->setHasFlexibleArrayMember(Record[Idx++]);
137 RD->setAnonymousStructOrUnion(Record[Idx++]);
138 RD->setHasObjectMember(Record[Idx++]);
141 void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
143 VD->setType(Reader.GetType(Record[Idx++]));
146 void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
149 ECD->setInitExpr(Reader.ReadDeclExpr());
150 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
153 void PCHDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
155 TypeSourceInfo *TInfo = Reader.GetTypeSourceInfo(Record, Idx);
157 DD->setTypeSourceInfo(TInfo);
160 void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
161 VisitDeclaratorDecl(FD);
163 FD->setLazyBody(Reader.getDeclsCursor().GetCurrentBitNo());
164 FD->setPreviousDeclaration(
165 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
166 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
167 FD->setInlineSpecified(Record[Idx++]);
168 FD->setVirtualAsWritten(Record[Idx++]);
169 FD->setPure(Record[Idx++]);
170 FD->setHasInheritedPrototype(Record[Idx++]);
171 FD->setHasWrittenPrototype(Record[Idx++]);
172 FD->setDeleted(Record[Idx++]);
173 FD->setTrivial(Record[Idx++]);
174 FD->setCopyAssignment(Record[Idx++]);
175 FD->setHasImplicitReturnZero(Record[Idx++]);
176 FD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
177 // FIXME: C++ TemplateOrInstantiation
178 unsigned NumParams = Record[Idx++];
179 llvm::SmallVector<ParmVarDecl *, 16> Params;
180 Params.reserve(NumParams);
181 for (unsigned I = 0; I != NumParams; ++I)
182 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
183 FD->setParams(Params.data(), NumParams);
186 void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
189 // In practice, this won't be executed (since method definitions
190 // don't occur in header files).
191 MD->setBody(Reader.ReadDeclStmt());
192 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
193 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
195 MD->setInstanceMethod(Record[Idx++]);
196 MD->setVariadic(Record[Idx++]);
197 MD->setSynthesized(Record[Idx++]);
198 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
199 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
200 MD->setResultType(Reader.GetType(Record[Idx++]));
201 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
202 unsigned NumParams = Record[Idx++];
203 llvm::SmallVector<ParmVarDecl *, 16> Params;
204 Params.reserve(NumParams);
205 for (unsigned I = 0; I != NumParams; ++I)
206 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
207 MD->setMethodParams(*Reader.getContext(), Params.data(), NumParams);
210 void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
212 SourceLocation A = SourceLocation::getFromRawEncoding(Record[Idx++]);
213 SourceLocation B = SourceLocation::getFromRawEncoding(Record[Idx++]);
214 CD->setAtEndRange(SourceRange(A, B));
217 void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
218 VisitObjCContainerDecl(ID);
219 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
220 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
221 (Reader.GetDecl(Record[Idx++])));
222 unsigned NumProtocols = Record[Idx++];
223 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
224 Protocols.reserve(NumProtocols);
225 for (unsigned I = 0; I != NumProtocols; ++I)
226 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
227 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
228 ProtoLocs.reserve(NumProtocols);
229 for (unsigned I = 0; I != NumProtocols; ++I)
230 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
231 ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
232 *Reader.getContext());
233 unsigned NumIvars = Record[Idx++];
234 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
235 IVars.reserve(NumIvars);
236 for (unsigned I = 0; I != NumIvars; ++I)
237 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
238 ID->setIVarList(IVars.data(), NumIvars, *Reader.getContext());
240 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
241 ID->setForwardDecl(Record[Idx++]);
242 ID->setImplicitInterfaceDecl(Record[Idx++]);
243 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
244 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
245 ID->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
248 void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
250 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
253 void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
254 VisitObjCContainerDecl(PD);
255 PD->setForwardDecl(Record[Idx++]);
256 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
257 unsigned NumProtoRefs = Record[Idx++];
258 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
259 ProtoRefs.reserve(NumProtoRefs);
260 for (unsigned I = 0; I != NumProtoRefs; ++I)
261 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
262 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
263 ProtoLocs.reserve(NumProtoRefs);
264 for (unsigned I = 0; I != NumProtoRefs; ++I)
265 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
266 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
267 *Reader.getContext());
270 void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
274 void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *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 llvm::SmallVector<SourceLocation, 16> SLocs;
282 SLocs.reserve(NumClassRefs);
283 for (unsigned I = 0; I != NumClassRefs; ++I)
284 SLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
285 CD->setClassList(*Reader.getContext(), ClassRefs.data(), SLocs.data(),
289 void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
291 unsigned NumProtoRefs = Record[Idx++];
292 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
293 ProtoRefs.reserve(NumProtoRefs);
294 for (unsigned I = 0; I != NumProtoRefs; ++I)
295 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
296 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
297 ProtoLocs.reserve(NumProtoRefs);
298 for (unsigned I = 0; I != NumProtoRefs; ++I)
299 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
300 FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
301 *Reader.getContext());
304 void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
305 VisitObjCContainerDecl(CD);
306 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
307 unsigned NumProtoRefs = Record[Idx++];
308 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
309 ProtoRefs.reserve(NumProtoRefs);
310 for (unsigned I = 0; I != NumProtoRefs; ++I)
311 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
312 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
313 ProtoLocs.reserve(NumProtoRefs);
314 for (unsigned I = 0; I != NumProtoRefs; ++I)
315 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
316 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
317 *Reader.getContext());
318 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
319 CD->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
320 CD->setCategoryNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
323 void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
325 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
328 void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
330 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
331 D->setType(Reader.GetType(Record[Idx++]));
332 // FIXME: stable encoding
333 D->setPropertyAttributes(
334 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
335 // FIXME: stable encoding
336 D->setPropertyImplementation(
337 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
338 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
339 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
340 D->setGetterMethodDecl(
341 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
342 D->setSetterMethodDecl(
343 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
344 D->setPropertyIvarDecl(
345 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
348 void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
349 VisitObjCContainerDecl(D);
350 D->setClassInterface(
351 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
354 void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
355 VisitObjCImplDecl(D);
356 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
359 void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
360 VisitObjCImplDecl(D);
362 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
366 void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
368 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
370 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
371 D->setPropertyIvarDecl(
372 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
375 void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
376 VisitDeclaratorDecl(FD);
377 FD->setMutable(Record[Idx++]);
379 FD->setBitWidth(Reader.ReadDeclExpr());
382 void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
383 VisitDeclaratorDecl(VD);
384 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
385 VD->setThreadSpecified(Record[Idx++]);
386 VD->setCXXDirectInitializer(Record[Idx++]);
387 VD->setDeclaredInCondition(Record[Idx++]);
388 VD->setPreviousDeclaration(
389 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
391 VD->setInit(Reader.ReadDeclExpr());
394 void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
398 void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
400 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
403 void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
405 AD->setAsmString(cast<StringLiteral>(Reader.ReadDeclExpr()));
408 void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
410 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadDeclStmt()));
411 unsigned NumParams = Record[Idx++];
412 llvm::SmallVector<ParmVarDecl *, 16> Params;
413 Params.reserve(NumParams);
414 for (unsigned I = 0; I != NumParams; ++I)
415 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
416 BD->setParams(Params.data(), NumParams);
419 std::pair<uint64_t, uint64_t>
420 PCHDeclReader::VisitDeclContext(DeclContext *DC) {
421 uint64_t LexicalOffset = Record[Idx++];
422 uint64_t VisibleOffset = Record[Idx++];
423 return std::make_pair(LexicalOffset, VisibleOffset);
426 //===----------------------------------------------------------------------===//
428 //===----------------------------------------------------------------------===//
430 /// \brief Reads attributes from the current stream position.
431 Attr *PCHReader::ReadAttributes() {
432 unsigned Code = DeclsCursor.ReadCode();
433 assert(Code == llvm::bitc::UNABBREV_RECORD &&
434 "Expected unabbreviated record"); (void)Code;
438 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
439 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
442 #define SIMPLE_ATTR(Name) \
444 New = ::new (*Context) Name##Attr(); \
447 #define STRING_ATTR(Name) \
449 New = ::new (*Context) Name##Attr(*Context, ReadString(Record, Idx)); \
452 #define UNSIGNED_ATTR(Name) \
454 New = ::new (*Context) Name##Attr(Record[Idx++]); \
458 while (Idx < Record.size()) {
460 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
461 bool IsInherited = Record[Idx++];
465 assert(0 && "Unknown attribute!");
468 UNSIGNED_ATTR(Aligned);
469 SIMPLE_ATTR(AlwaysInline);
470 SIMPLE_ATTR(AnalyzerNoReturn);
471 STRING_ATTR(Annotate);
472 STRING_ATTR(AsmLabel);
473 SIMPLE_ATTR(BaseCheck);
476 New = ::new (*Context) BlocksAttr(
477 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
483 New = ::new (*Context) CleanupAttr(
484 cast<FunctionDecl>(GetDecl(Record[Idx++])));
488 UNSIGNED_ATTR(Constructor);
489 SIMPLE_ATTR(DLLExport);
490 SIMPLE_ATTR(DLLImport);
491 SIMPLE_ATTR(Deprecated);
492 UNSIGNED_ATTR(Destructor);
493 SIMPLE_ATTR(FastCall);
497 std::string Type = ReadString(Record, Idx);
498 unsigned FormatIdx = Record[Idx++];
499 unsigned FirstArg = Record[Idx++];
500 New = ::new (*Context) FormatAttr(*Context, Type, FormatIdx, FirstArg);
504 case Attr::FormatArg: {
505 unsigned FormatIdx = Record[Idx++];
506 New = ::new (*Context) FormatArgAttr(FormatIdx);
510 case Attr::Sentinel: {
511 int sentinel = Record[Idx++];
512 int nullPos = Record[Idx++];
513 New = ::new (*Context) SentinelAttr(sentinel, nullPos);
517 SIMPLE_ATTR(GNUInline);
520 case Attr::IBOutletKind:
521 New = ::new (*Context) IBOutletAttr();
525 SIMPLE_ATTR(NoDebug);
526 SIMPLE_ATTR(NoInline);
527 SIMPLE_ATTR(NoReturn);
528 SIMPLE_ATTR(NoThrow);
530 case Attr::NonNull: {
531 unsigned Size = Record[Idx++];
532 llvm::SmallVector<unsigned, 16> ArgNums;
533 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
535 New = ::new (*Context) NonNullAttr(*Context, ArgNums.data(), Size);
539 case Attr::ReqdWorkGroupSize: {
540 unsigned X = Record[Idx++];
541 unsigned Y = Record[Idx++];
542 unsigned Z = Record[Idx++];
543 New = ::new (*Context) ReqdWorkGroupSizeAttr(X, Y, Z);
547 SIMPLE_ATTR(ObjCException);
548 SIMPLE_ATTR(ObjCNSObject);
549 SIMPLE_ATTR(CFReturnsRetained);
550 SIMPLE_ATTR(NSReturnsRetained);
551 SIMPLE_ATTR(Overloadable);
552 SIMPLE_ATTR(Override);
554 UNSIGNED_ATTR(PragmaPack);
556 UNSIGNED_ATTR(Regparm);
557 STRING_ATTR(Section);
558 SIMPLE_ATTR(StdCall);
559 SIMPLE_ATTR(TransparentUnion);
560 SIMPLE_ATTR(Unavailable);
564 case Attr::Visibility:
565 New = ::new (*Context) VisibilityAttr(
566 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
569 SIMPLE_ATTR(WarnUnusedResult);
571 SIMPLE_ATTR(WeakImport);
574 assert(New && "Unable to decode attribute?");
575 New->setInherited(IsInherited);
583 // The list of attributes was built backwards. Reverse the list
584 // before returning it.
585 Attr *PrevAttr = 0, *NextAttr = 0;
587 NextAttr = Attrs->getNext();
588 Attrs->setNext(PrevAttr);
596 //===----------------------------------------------------------------------===//
597 // PCHReader Implementation
598 //===----------------------------------------------------------------------===//
600 /// \brief Note that we have loaded the declaration with the given
603 /// This routine notes that this declaration has already been loaded,
604 /// so that future GetDecl calls will return this declaration rather
605 /// than trying to load a new declaration.
606 inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
607 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
608 DeclsLoaded[Index] = D;
612 /// \brief Determine whether the consumer will be interested in seeing
613 /// this declaration (via HandleTopLevelDecl).
615 /// This routine should return true for anything that might affect
616 /// code generation, e.g., inline function definitions, Objective-C
617 /// declarations with metadata, etc.
618 static bool isConsumerInterestedIn(Decl *D) {
619 if (isa<FileScopeAsmDecl>(D))
621 if (VarDecl *Var = dyn_cast<VarDecl>(D))
622 return Var->isFileVarDecl() && Var->getInit();
623 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
624 return Func->isThisDeclarationADefinition();
625 return isa<ObjCProtocolDecl>(D);
628 /// \brief Read the declaration at the given offset from the PCH file.
629 Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
630 // Keep track of where we are in the stream, then jump back there
631 // after reading this declaration.
632 SavedStreamPosition SavedPosition(DeclsCursor);
634 // Note that we are loading a declaration record.
635 LoadingTypeOrDecl Loading(*this);
637 DeclsCursor.JumpToBit(Offset);
639 unsigned Code = DeclsCursor.ReadCode();
641 PCHDeclReader Reader(*this, Record, Idx);
644 switch ((pch::DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
646 case pch::DECL_CONTEXT_LEXICAL:
647 case pch::DECL_CONTEXT_VISIBLE:
648 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
650 case pch::DECL_TRANSLATION_UNIT:
651 assert(Index == 0 && "Translation unit must be at index 0");
652 D = Context->getTranslationUnitDecl();
654 case pch::DECL_TYPEDEF:
655 D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, 0);
658 D = EnumDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(), 0);
660 case pch::DECL_RECORD:
661 D = RecordDecl::Create(*Context, TagDecl::TK_struct, 0, SourceLocation(),
662 0, SourceLocation(), 0);
664 case pch::DECL_ENUM_CONSTANT:
665 D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
668 case pch::DECL_FUNCTION:
669 D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
672 case pch::DECL_OBJC_METHOD:
673 D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
674 Selector(), QualType(), 0);
676 case pch::DECL_OBJC_INTERFACE:
677 D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
679 case pch::DECL_OBJC_IVAR:
680 D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
683 case pch::DECL_OBJC_PROTOCOL:
684 D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
686 case pch::DECL_OBJC_AT_DEFS_FIELD:
687 D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
690 case pch::DECL_OBJC_CLASS:
691 D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
693 case pch::DECL_OBJC_FORWARD_PROTOCOL:
694 D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
696 case pch::DECL_OBJC_CATEGORY:
697 D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(),
698 SourceLocation(), SourceLocation(), 0);
700 case pch::DECL_OBJC_CATEGORY_IMPL:
701 D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
703 case pch::DECL_OBJC_IMPLEMENTATION:
704 D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
706 case pch::DECL_OBJC_COMPATIBLE_ALIAS:
707 D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
709 case pch::DECL_OBJC_PROPERTY:
710 D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(),
713 case pch::DECL_OBJC_PROPERTY_IMPL:
714 D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
716 ObjCPropertyImplDecl::Dynamic, 0);
718 case pch::DECL_FIELD:
719 D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0, 0,
723 D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
727 case pch::DECL_IMPLICIT_PARAM:
728 D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
731 case pch::DECL_PARM_VAR:
732 D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
735 case pch::DECL_FILE_SCOPE_ASM:
736 D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
738 case pch::DECL_BLOCK:
739 D = BlockDecl::Create(*Context, 0, SourceLocation());
743 assert(D && "Unknown declaration reading PCH file");
744 LoadedDecl(Index, D);
747 // If this declaration is also a declaration context, get the
748 // offsets for its tables of lexical and visible declarations.
749 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
750 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
751 if (Offsets.first || Offsets.second) {
752 DC->setHasExternalLexicalStorage(Offsets.first != 0);
753 DC->setHasExternalVisibleStorage(Offsets.second != 0);
754 DeclContextOffsets[DC] = Offsets;
757 assert(Idx == Record.size());
759 // If we have deserialized a declaration that has a definition the
760 // AST consumer might need to know about, notify the consumer
761 // about that definition now or queue it for later.
762 if (isConsumerInterestedIn(D)) {
765 Consumer->HandleTopLevelDecl(DG);
767 InterestingDecls.push_back(D);