Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 1 | //===--- PCHWriterDecl.cpp - Declaration Serialization --------------------===// |
| 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 serialization for Declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Frontend/PCHWriter.h" |
| 15 | #include "clang/AST/DeclVisitor.h" |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 16 | #include "clang/AST/DeclCXX.h" |
| 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | 3367198 | 2009-12-03 09:13:36 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Twine.h" |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/BitstreamWriter.h" |
Daniel Dunbar | 3367198 | 2009-12-03 09:13:36 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Declaration serialization |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | namespace { |
| 29 | class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> { |
| 30 | |
| 31 | PCHWriter &Writer; |
| 32 | ASTContext &Context; |
| 33 | PCHWriter::RecordData &Record; |
| 34 | |
| 35 | public: |
| 36 | pch::DeclCode Code; |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 37 | unsigned AbbrevToUse; |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 38 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | PCHDeclWriter(PCHWriter &Writer, ASTContext &Context, |
| 40 | PCHWriter::RecordData &Record) |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 41 | : Writer(Writer), Context(Context), Record(Record) { |
| 42 | } |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 43 | |
| 44 | void VisitDecl(Decl *D); |
| 45 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 46 | void VisitNamedDecl(NamedDecl *D); |
Douglas Gregor | 0cef483 | 2010-02-21 18:22:14 +0000 | [diff] [blame] | 47 | void VisitNamespaceDecl(NamespaceDecl *D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 48 | void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
| 49 | void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 50 | void VisitTypeDecl(TypeDecl *D); |
| 51 | void VisitTypedefDecl(TypedefDecl *D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 52 | void VisitUnresolvedUsingTypename(UnresolvedUsingTypenameDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 53 | void VisitTagDecl(TagDecl *D); |
| 54 | void VisitEnumDecl(EnumDecl *D); |
| 55 | void VisitRecordDecl(RecordDecl *D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 56 | void VisitCXXRecordDecl(CXXRecordDecl *D); |
| 57 | void VisitClassTemplateSpecializationDecl( |
| 58 | ClassTemplateSpecializationDecl *D); |
| 59 | void VisitClassTemplatePartialSpecializationDecl( |
| 60 | ClassTemplatePartialSpecializationDecl *D); |
| 61 | void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 62 | void VisitValueDecl(ValueDecl *D); |
| 63 | void VisitEnumConstantDecl(EnumConstantDecl *D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 64 | void VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D); |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 65 | void VisitDeclaratorDecl(DeclaratorDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 66 | void VisitFunctionDecl(FunctionDecl *D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 67 | void VisitCXXMethodDecl(CXXMethodDecl *D); |
| 68 | void VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 69 | void VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 70 | void VisitCXXConversionDecl(CXXConversionDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 71 | void VisitFieldDecl(FieldDecl *D); |
| 72 | void VisitVarDecl(VarDecl *D); |
| 73 | void VisitImplicitParamDecl(ImplicitParamDecl *D); |
| 74 | void VisitParmVarDecl(ParmVarDecl *D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 75 | void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 76 | void VisitTemplateDecl(TemplateDecl *D); |
| 77 | void VisitClassTemplateDecl(ClassTemplateDecl *D); |
| 78 | void visitFunctionTemplateDecl(FunctionTemplateDecl *D); |
| 79 | void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 80 | void VisitUsing(UsingDecl *D); |
| 81 | void VisitUsingShadow(UsingShadowDecl *D); |
| 82 | void VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 83 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 84 | void VisitFriendTemplateDecl(FriendTemplateDecl *D); |
| 85 | void VisitStaticAssertDecl(StaticAssertDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 86 | void VisitBlockDecl(BlockDecl *D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 87 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 89 | uint64_t VisibleOffset); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 90 | |
| 91 | |
| 92 | // FIXME: Put in the same order is DeclNodes.def? |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 93 | void VisitObjCMethodDecl(ObjCMethodDecl *D); |
| 94 | void VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 95 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 96 | void VisitObjCIvarDecl(ObjCIvarDecl *D); |
| 97 | void VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 98 | void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D); |
| 99 | void VisitObjCClassDecl(ObjCClassDecl *D); |
| 100 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 101 | void VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 102 | void VisitObjCImplDecl(ObjCImplDecl *D); |
| 103 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 104 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 105 | void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); |
| 106 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 107 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | void PCHDeclWriter::VisitDecl(Decl *D) { |
| 112 | Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); |
| 113 | Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); |
| 114 | Writer.AddSourceLocation(D->getLocation(), Record); |
| 115 | Record.push_back(D->isInvalidDecl()); |
| 116 | Record.push_back(D->hasAttrs()); |
| 117 | Record.push_back(D->isImplicit()); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 118 | Record.push_back(D->isUsed()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 119 | Record.push_back(D->getAccess()); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 120 | Record.push_back(D->getPCHLevel()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 124 | VisitDecl(D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 125 | Writer.AddDeclRef(D->getAnonymousNamespace(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 126 | Code = pch::DECL_TRANSLATION_UNIT; |
| 127 | } |
| 128 | |
| 129 | void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) { |
| 130 | VisitDecl(D); |
| 131 | Writer.AddDeclarationName(D->getDeclName(), Record); |
| 132 | } |
| 133 | |
| 134 | void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) { |
| 135 | VisitNamedDecl(D); |
| 136 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 137 | } |
| 138 | |
| 139 | void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { |
| 140 | VisitTypeDecl(D); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 141 | Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 142 | Code = pch::DECL_TYPEDEF; |
| 143 | } |
| 144 | |
| 145 | void PCHDeclWriter::VisitTagDecl(TagDecl *D) { |
| 146 | VisitTypeDecl(D); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 147 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 148 | Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding |
| 149 | Record.push_back(D->isDefinition()); |
Douglas Gregor | b37b648 | 2010-02-12 17:40:34 +0000 | [diff] [blame] | 150 | Record.push_back(D->isEmbeddedInDeclarator()); |
Argyrios Kyrtzidis | ad93a74 | 2009-07-14 03:18:02 +0000 | [diff] [blame] | 151 | Writer.AddSourceLocation(D->getRBraceLoc(), Record); |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 152 | Writer.AddSourceLocation(D->getTagKeywordLoc(), Record); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 153 | // FIXME: maybe write optional qualifier and its range. |
| 154 | Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) { |
| 158 | VisitTagDecl(D); |
| 159 | Writer.AddTypeRef(D->getIntegerType(), Record); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 160 | Writer.AddTypeRef(D->getPromotionType(), Record); |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 161 | Record.push_back(D->getNumPositiveBits()); |
| 162 | Record.push_back(D->getNumNegativeBits()); |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 163 | // FIXME: C++ InstantiatedFrom |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 164 | Code = pch::DECL_ENUM; |
| 165 | } |
| 166 | |
| 167 | void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) { |
| 168 | VisitTagDecl(D); |
| 169 | Record.push_back(D->hasFlexibleArrayMember()); |
| 170 | Record.push_back(D->isAnonymousStructOrUnion()); |
Fariborz Jahanian | 643b7df | 2009-07-08 16:37:44 +0000 | [diff] [blame] | 171 | Record.push_back(D->hasObjectMember()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 172 | Code = pch::DECL_RECORD; |
| 173 | } |
| 174 | |
| 175 | void PCHDeclWriter::VisitValueDecl(ValueDecl *D) { |
| 176 | VisitNamedDecl(D); |
| 177 | Writer.AddTypeRef(D->getType(), Record); |
| 178 | } |
| 179 | |
| 180 | void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 181 | VisitValueDecl(D); |
| 182 | Record.push_back(D->getInitExpr()? 1 : 0); |
| 183 | if (D->getInitExpr()) |
| 184 | Writer.AddStmt(D->getInitExpr()); |
| 185 | Writer.AddAPSInt(D->getInitVal(), Record); |
| 186 | Code = pch::DECL_ENUM_CONSTANT; |
| 187 | } |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 188 | |
| 189 | void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) { |
| 190 | VisitValueDecl(D); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 191 | Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 192 | // FIXME: write optional qualifier and its range. |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 193 | } |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 194 | |
| 195 | void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) { |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 196 | VisitDeclaratorDecl(D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 197 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 198 | Record.push_back(D->isThisDeclarationADefinition()); |
| 199 | if (D->isThisDeclarationADefinition()) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 200 | Writer.AddStmt(D->getBody()); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 201 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 202 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 203 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 204 | Record.push_back(D->getStorageClassAsWritten()); |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 205 | Record.push_back(D->isInlineSpecified()); |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 206 | Record.push_back(D->isVirtualAsWritten()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 207 | Record.push_back(D->isPure()); |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 208 | Record.push_back(D->hasInheritedPrototype()); |
| 209 | Record.push_back(D->hasWrittenPrototype()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 210 | Record.push_back(D->isDeleted()); |
Daniel Dunbar | 7f8b57a | 2009-09-22 05:38:14 +0000 | [diff] [blame] | 211 | Record.push_back(D->isTrivial()); |
| 212 | Record.push_back(D->isCopyAssignment()); |
| 213 | Record.push_back(D->hasImplicitReturnZero()); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 214 | // FIXME: C++ TemplateOrInstantiation??? |
Argyrios Kyrtzidis | 8cff90e | 2009-06-20 08:09:34 +0000 | [diff] [blame] | 215 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 216 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 217 | Record.push_back(D->param_size()); |
| 218 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 219 | P != PEnd; ++P) |
| 220 | Writer.AddDeclRef(*P, Record); |
| 221 | Code = pch::DECL_FUNCTION; |
| 222 | } |
| 223 | |
| 224 | void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 225 | VisitNamedDecl(D); |
| 226 | // FIXME: convert to LazyStmtPtr? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | // Unlike C/C++, method bodies will never be in header files. |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 228 | Record.push_back(D->getBody() != 0); |
| 229 | if (D->getBody() != 0) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 230 | Writer.AddStmt(D->getBody()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 231 | Writer.AddDeclRef(D->getSelfDecl(), Record); |
| 232 | Writer.AddDeclRef(D->getCmdDecl(), Record); |
| 233 | } |
| 234 | Record.push_back(D->isInstanceMethod()); |
| 235 | Record.push_back(D->isVariadic()); |
| 236 | Record.push_back(D->isSynthesized()); |
| 237 | // FIXME: stable encoding for @required/@optional |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | Record.push_back(D->getImplementationControl()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 239 | // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | Record.push_back(D->getObjCDeclQualifier()); |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 241 | Record.push_back(D->getNumSelectorArgs()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 242 | Writer.AddTypeRef(D->getResultType(), Record); |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 243 | Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 244 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 245 | Record.push_back(D->param_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 247 | PEnd = D->param_end(); P != PEnd; ++P) |
| 248 | Writer.AddDeclRef(*P, Record); |
| 249 | Code = pch::DECL_OBJC_METHOD; |
| 250 | } |
| 251 | |
| 252 | void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 253 | VisitNamedDecl(D); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 254 | Writer.AddSourceRange(D->getAtEndRange(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 255 | // Abstract class (no need to define a stable pch::DECL code). |
| 256 | } |
| 257 | |
| 258 | void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 259 | VisitObjCContainerDecl(D); |
| 260 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 261 | Writer.AddDeclRef(D->getSuperClass(), Record); |
| 262 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(), |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 264 | PEnd = D->protocol_end(); |
| 265 | P != PEnd; ++P) |
| 266 | Writer.AddDeclRef(*P, Record); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 267 | for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(), |
| 268 | PLEnd = D->protocol_loc_end(); |
| 269 | PL != PLEnd; ++PL) |
| 270 | Writer.AddSourceLocation(*PL, Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 271 | Record.push_back(D->ivar_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(), |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 273 | IEnd = D->ivar_end(); I != IEnd; ++I) |
| 274 | Writer.AddDeclRef(*I, Record); |
| 275 | Writer.AddDeclRef(D->getCategoryList(), Record); |
| 276 | Record.push_back(D->isForwardDecl()); |
| 277 | Record.push_back(D->isImplicitInterfaceDecl()); |
| 278 | Writer.AddSourceLocation(D->getClassLoc(), Record); |
| 279 | Writer.AddSourceLocation(D->getSuperClassLoc(), Record); |
| 280 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 281 | Code = pch::DECL_OBJC_INTERFACE; |
| 282 | } |
| 283 | |
| 284 | void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 285 | VisitFieldDecl(D); |
| 286 | // FIXME: stable encoding for @public/@private/@protected/@package |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | Record.push_back(D->getAccessControl()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 288 | Code = pch::DECL_OBJC_IVAR; |
| 289 | } |
| 290 | |
| 291 | void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
| 292 | VisitObjCContainerDecl(D); |
| 293 | Record.push_back(D->isForwardDecl()); |
| 294 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 295 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | for (ObjCProtocolDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 297 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 298 | Writer.AddDeclRef(*I, Record); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 299 | for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(), |
| 300 | PLEnd = D->protocol_loc_end(); |
| 301 | PL != PLEnd; ++PL) |
| 302 | Writer.AddSourceLocation(*PL, Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 303 | Code = pch::DECL_OBJC_PROTOCOL; |
| 304 | } |
| 305 | |
| 306 | void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 307 | VisitFieldDecl(D); |
| 308 | Code = pch::DECL_OBJC_AT_DEFS_FIELD; |
| 309 | } |
| 310 | |
| 311 | void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 312 | VisitDecl(D); |
| 313 | Record.push_back(D->size()); |
| 314 | for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 315 | Writer.AddDeclRef(I->getInterface(), Record); |
| 316 | for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) |
| 317 | Writer.AddSourceLocation(I->getLocation(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 318 | Code = pch::DECL_OBJC_CLASS; |
| 319 | } |
| 320 | |
| 321 | void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 322 | VisitDecl(D); |
| 323 | Record.push_back(D->protocol_size()); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 324 | for (ObjCForwardProtocolDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 325 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 326 | Writer.AddDeclRef(*I, Record); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 327 | for (ObjCForwardProtocolDecl::protocol_loc_iterator |
| 328 | PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end(); |
| 329 | PL != PLEnd; ++PL) |
| 330 | Writer.AddSourceLocation(*PL, Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 331 | Code = pch::DECL_OBJC_FORWARD_PROTOCOL; |
| 332 | } |
| 333 | |
| 334 | void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 335 | VisitObjCContainerDecl(D); |
| 336 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 337 | Record.push_back(D->protocol_size()); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 338 | for (ObjCCategoryDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 339 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 340 | Writer.AddDeclRef(*I, Record); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 341 | for (ObjCCategoryDecl::protocol_loc_iterator |
| 342 | PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end(); |
| 343 | PL != PLEnd; ++PL) |
| 344 | Writer.AddSourceLocation(*PL, Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 345 | Writer.AddDeclRef(D->getNextClassCategory(), Record); |
Douglas Gregor | 3db211b | 2010-01-16 16:38:58 +0000 | [diff] [blame] | 346 | Writer.AddSourceLocation(D->getAtLoc(), Record); |
| 347 | Writer.AddSourceLocation(D->getCategoryNameLoc(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 348 | Code = pch::DECL_OBJC_CATEGORY; |
| 349 | } |
| 350 | |
| 351 | void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) { |
| 352 | VisitNamedDecl(D); |
| 353 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 354 | Code = pch::DECL_OBJC_COMPATIBLE_ALIAS; |
| 355 | } |
| 356 | |
| 357 | void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 358 | VisitNamedDecl(D); |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 359 | Writer.AddSourceLocation(D->getAtLoc(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 360 | Writer.AddTypeRef(D->getType(), Record); |
| 361 | // FIXME: stable encoding |
| 362 | Record.push_back((unsigned)D->getPropertyAttributes()); |
| 363 | // FIXME: stable encoding |
| 364 | Record.push_back((unsigned)D->getPropertyImplementation()); |
| 365 | Writer.AddDeclarationName(D->getGetterName(), Record); |
| 366 | Writer.AddDeclarationName(D->getSetterName(), Record); |
| 367 | Writer.AddDeclRef(D->getGetterMethodDecl(), Record); |
| 368 | Writer.AddDeclRef(D->getSetterMethodDecl(), Record); |
| 369 | Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); |
| 370 | Code = pch::DECL_OBJC_PROPERTY; |
| 371 | } |
| 372 | |
| 373 | void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) { |
Argyrios Kyrtzidis | aecae62 | 2009-07-27 19:04:32 +0000 | [diff] [blame] | 374 | VisitObjCContainerDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 375 | Writer.AddDeclRef(D->getClassInterface(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 376 | // Abstract class (no need to define a stable pch::DECL code). |
| 377 | } |
| 378 | |
| 379 | void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 380 | VisitObjCImplDecl(D); |
| 381 | Writer.AddIdentifierRef(D->getIdentifier(), Record); |
| 382 | Code = pch::DECL_OBJC_CATEGORY_IMPL; |
| 383 | } |
| 384 | |
| 385 | void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 386 | VisitObjCImplDecl(D); |
| 387 | Writer.AddDeclRef(D->getSuperClass(), Record); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 388 | // FIXME add writing of IvarInitializers and NumIvarInitializers. |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 389 | Code = pch::DECL_OBJC_IMPLEMENTATION; |
| 390 | } |
| 391 | |
| 392 | void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 393 | VisitDecl(D); |
| 394 | Writer.AddSourceLocation(D->getLocStart(), Record); |
| 395 | Writer.AddDeclRef(D->getPropertyDecl(), Record); |
| 396 | Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 397 | // FIXME. write GetterCXXConstructor and SetterCXXAssignment. |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 398 | Code = pch::DECL_OBJC_PROPERTY_IMPL; |
| 399 | } |
| 400 | |
| 401 | void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) { |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 402 | VisitDeclaratorDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 403 | Record.push_back(D->isMutable()); |
| 404 | Record.push_back(D->getBitWidth()? 1 : 0); |
| 405 | if (D->getBitWidth()) |
| 406 | Writer.AddStmt(D->getBitWidth()); |
| 407 | Code = pch::DECL_FIELD; |
| 408 | } |
| 409 | |
| 410 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 411 | VisitDeclaratorDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 412 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 413 | Record.push_back(D->getStorageClassAsWritten()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 414 | Record.push_back(D->isThreadSpecified()); |
| 415 | Record.push_back(D->hasCXXDirectInitializer()); |
| 416 | Record.push_back(D->isDeclaredInCondition()); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 417 | Record.push_back(D->isExceptionVariable()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 418 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 419 | Record.push_back(D->getInit()? 1 : 0); |
| 420 | if (D->getInit()) |
| 421 | Writer.AddStmt(D->getInit()); |
| 422 | Code = pch::DECL_VAR; |
| 423 | } |
| 424 | |
| 425 | void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 426 | VisitVarDecl(D); |
| 427 | Code = pch::DECL_IMPLICIT_PARAM; |
| 428 | } |
| 429 | |
| 430 | void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { |
| 431 | VisitVarDecl(D); |
| 432 | Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 433 | Record.push_back(D->hasInheritedDefaultArg()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 434 | Code = pch::DECL_PARM_VAR; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 436 | // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here |
| 437 | // we dynamically check for the properties that we optimize for, but don't |
| 438 | // know are true of all PARM_VAR_DECLs. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 439 | if (!D->getTypeSourceInfo() && |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 440 | !D->hasAttrs() && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 441 | !D->isImplicit() && |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 442 | !D->isUsed() && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 443 | D->getAccess() == AS_none && |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 444 | D->getPCHLevel() == 0 && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 445 | D->getStorageClass() == 0 && |
| 446 | !D->hasCXXDirectInitializer() && // Can params have this ever? |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 447 | D->getObjCDeclQualifier() == 0 && |
| 448 | !D->hasInheritedDefaultArg()) |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 449 | AbbrevToUse = Writer.getParmVarDeclAbbrev(); |
| 450 | |
| 451 | // Check things we know are true of *every* PARM_VAR_DECL, which is more than |
| 452 | // just us assuming it. |
| 453 | assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls"); |
| 454 | assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread"); |
| 455 | assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private"); |
| 456 | assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition"); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 457 | assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var"); |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 458 | assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl"); |
| 459 | assert(D->getInit() == 0 && "PARM_VAR_DECL never has init"); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 462 | void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 463 | VisitDecl(D); |
| 464 | Writer.AddStmt(D->getAsmString()); |
| 465 | Code = pch::DECL_FILE_SCOPE_ASM; |
| 466 | } |
| 467 | |
| 468 | void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) { |
| 469 | VisitDecl(D); |
| 470 | Writer.AddStmt(D->getBody()); |
| 471 | Record.push_back(D->param_size()); |
| 472 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 473 | P != PEnd; ++P) |
| 474 | Writer.AddDeclRef(*P, Record); |
| 475 | Code = pch::DECL_BLOCK; |
| 476 | } |
| 477 | |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame^] | 478 | void PCHDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 479 | VisitDecl(D); |
| 480 | // FIXME: It might be nice to serialize the brace locations for this |
| 481 | // declaration, which don't seem to be readily available in the AST. |
| 482 | Record.push_back(D->getLanguage()); |
| 483 | Record.push_back(D->hasBraces()); |
| 484 | Code = pch::DECL_LINKAGE_SPEC; |
| 485 | } |
| 486 | |
| 487 | void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 488 | VisitNamedDecl(D); |
| 489 | Writer.AddSourceLocation(D->getLBracLoc(), Record); |
| 490 | Writer.AddSourceLocation(D->getRBracLoc(), Record); |
| 491 | Writer.AddDeclRef(D->getNextNamespace(), Record); |
| 492 | |
| 493 | // Only write one reference--original or anonymous |
| 494 | Record.push_back(D->isOriginalNamespace()); |
| 495 | if (D->isOriginalNamespace()) |
| 496 | Writer.AddDeclRef(D->getAnonymousNamespace(), Record); |
| 497 | else |
| 498 | Writer.AddDeclRef(D->getOriginalNamespace(), Record); |
| 499 | Code = pch::DECL_NAMESPACE; |
| 500 | } |
| 501 | |
| 502 | void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 503 | VisitNamedDecl(D); |
| 504 | Writer.AddSourceLocation(D->getAliasLoc(), Record); |
| 505 | Writer.AddSourceRange(D->getQualifierRange(), Record); |
| 506 | Writer.AddNestedNameSpecifier(D->getQualifier(), Record); |
| 507 | Writer.AddSourceLocation(D->getTargetNameLoc(), Record); |
| 508 | Writer.AddDeclRef(D->getNamespace(), Record); |
| 509 | Code = pch::DECL_NAMESPACE_ALIAS; |
| 510 | } |
| 511 | |
| 512 | void PCHDeclWriter::VisitUsing(UsingDecl *D) { |
| 513 | VisitNamedDecl(D); |
| 514 | Writer.AddSourceRange(D->getNestedNameRange(), Record); |
| 515 | Writer.AddSourceLocation(D->getUsingLocation(), Record); |
| 516 | Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record); |
| 517 | Record.push_back(D->getNumShadowDecls()); |
| 518 | for (UsingDecl::shadow_iterator P = D->shadow_begin(), |
| 519 | PEnd = D->shadow_end(); P != PEnd; ++P) |
| 520 | Writer.AddDeclRef(*P, Record); |
| 521 | Record.push_back(D->isTypeName()); |
| 522 | Code = pch::DECL_USING; |
| 523 | } |
| 524 | |
| 525 | void PCHDeclWriter::VisitUsingShadow(UsingShadowDecl *D) { |
| 526 | VisitNamedDecl(D); |
| 527 | Writer.AddDeclRef(D->getTargetDecl(), Record); |
| 528 | Writer.AddDeclRef(D->getUsingDecl(), Record); |
| 529 | Code = pch::DECL_USING_SHADOW; |
| 530 | } |
| 531 | |
| 532 | void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 533 | VisitNamedDecl(D); |
| 534 | Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record); |
| 535 | Writer.AddSourceRange(D->getQualifierRange(), Record); |
| 536 | Writer.AddNestedNameSpecifier(D->getQualifier(), Record); |
| 537 | Writer.AddSourceLocation(D->getIdentLocation(), Record); |
| 538 | Writer.AddDeclRef(D->getNominatedNamespace(), Record); |
| 539 | Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record); |
| 540 | Code = pch::DECL_USING_DIRECTIVE; |
| 541 | } |
| 542 | |
| 543 | void PCHDeclWriter::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) { |
| 544 | VisitValueDecl(D); |
| 545 | Writer.AddSourceRange(D->getTargetNestedNameRange(), Record); |
| 546 | Writer.AddSourceLocation(D->getUsingLoc(), Record); |
| 547 | Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record); |
| 548 | Code = pch::DECL_UNRESOLVED_USING_VALUE; |
| 549 | } |
| 550 | |
| 551 | void PCHDeclWriter::VisitUnresolvedUsingTypename( |
| 552 | UnresolvedUsingTypenameDecl *D) { |
| 553 | VisitTypeDecl(D); |
| 554 | Writer.AddSourceRange(D->getTargetNestedNameRange(), Record); |
| 555 | Writer.AddSourceLocation(D->getUsingLoc(), Record); |
| 556 | Writer.AddSourceLocation(D->getTypenameLoc(), Record); |
| 557 | Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record); |
| 558 | Code = pch::DECL_UNRESOLVED_USING_TYPENAME; |
| 559 | } |
| 560 | |
| 561 | void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 562 | // assert(false && "cannot write CXXRecordDecl"); |
| 563 | VisitRecordDecl(D); |
| 564 | Code = pch::DECL_CXX_RECORD; |
| 565 | } |
| 566 | |
| 567 | void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 568 | // assert(false && "cannot write CXXMethodDecl"); |
| 569 | VisitFunctionDecl(D); |
| 570 | Code = pch::DECL_CXX_METHOD; |
| 571 | } |
| 572 | |
| 573 | void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 574 | // assert(false && "cannot write CXXConstructorDecl"); |
| 575 | VisitCXXMethodDecl(D); |
| 576 | Code = pch::DECL_CXX_CONSTRUCTOR; |
| 577 | } |
| 578 | |
| 579 | void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 580 | // assert(false && "cannot write CXXDestructorDecl"); |
| 581 | VisitCXXMethodDecl(D); |
| 582 | Code = pch::DECL_CXX_DESTRUCTOR; |
| 583 | } |
| 584 | |
| 585 | void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 586 | // assert(false && "cannot write CXXConversionDecl"); |
| 587 | VisitCXXMethodDecl(D); |
| 588 | Code = pch::DECL_CXX_CONVERSION; |
| 589 | } |
| 590 | |
| 591 | void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) { |
| 592 | assert(false && "cannot write FriendTemplateDecl"); |
| 593 | } |
| 594 | |
| 595 | void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) { |
| 596 | assert(false && "cannot write TemplateDecl"); |
| 597 | } |
| 598 | |
| 599 | void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 600 | assert(false && "cannot write ClassTemplateDecl"); |
| 601 | } |
| 602 | |
| 603 | void PCHDeclWriter::VisitClassTemplateSpecializationDecl( |
| 604 | ClassTemplateSpecializationDecl *D) { |
| 605 | assert(false && "cannot write ClassTemplateSpecializationDecl"); |
| 606 | } |
| 607 | |
| 608 | void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl( |
| 609 | ClassTemplatePartialSpecializationDecl *D) { |
| 610 | assert(false && "cannot write ClassTemplatePartialSpecializationDecl"); |
| 611 | } |
| 612 | |
| 613 | void PCHDeclWriter::visitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 614 | assert(false && "cannot write FunctionTemplateDecl"); |
| 615 | } |
| 616 | |
| 617 | void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 618 | assert(false && "cannot write TemplateTypeParmDecl"); |
| 619 | } |
| 620 | |
| 621 | void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 622 | assert(false && "cannot write NonTypeTemplateParmDecl"); |
| 623 | } |
| 624 | |
| 625 | void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 626 | assert(false && "cannot write TemplateTemplateParmDecl"); |
| 627 | } |
| 628 | |
| 629 | void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 630 | assert(false && "cannot write StaticAssertDecl"); |
| 631 | } |
| 632 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 633 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 634 | /// |
| 635 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 636 | /// block for this declaration context is stored. May be 0 to indicate |
| 637 | /// that there are no declarations stored within this context. |
| 638 | /// |
| 639 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 640 | /// block for this declaration context is stored. May be 0 to indicate |
| 641 | /// that there are no declarations visible from this context. Note |
| 642 | /// that this value will not be emitted for non-primary declaration |
| 643 | /// contexts. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 645 | uint64_t VisibleOffset) { |
| 646 | Record.push_back(LexicalOffset); |
| 647 | Record.push_back(VisibleOffset); |
| 648 | } |
| 649 | |
| 650 | |
| 651 | //===----------------------------------------------------------------------===// |
| 652 | // PCHWriter Implementation |
| 653 | //===----------------------------------------------------------------------===// |
| 654 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 655 | void PCHWriter::WriteDeclsBlockAbbrevs() { |
| 656 | using namespace llvm; |
| 657 | // Abbreviation for DECL_PARM_VAR. |
| 658 | BitCodeAbbrev *Abv = new BitCodeAbbrev(); |
| 659 | Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR)); |
| 660 | |
| 661 | // Decl |
| 662 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext |
| 663 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext |
| 664 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location |
| 665 | Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) |
| 666 | Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs |
| 667 | Abv->Add(BitCodeAbbrevOp(0)); // isImplicit |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 668 | Abv->Add(BitCodeAbbrevOp(0)); // isUsed |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 669 | Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 670 | Abv->Add(BitCodeAbbrevOp(0)); // PCH level |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 672 | // NamedDecl |
| 673 | Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier |
| 674 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name |
| 675 | // ValueDecl |
| 676 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 677 | // DeclaratorDecl |
| 678 | Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 679 | // VarDecl |
| 680 | Abv->Add(BitCodeAbbrevOp(0)); // StorageClass |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 681 | Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 682 | Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified |
| 683 | Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer |
| 684 | Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 685 | Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 686 | Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 687 | Abv->Add(BitCodeAbbrevOp(0)); // HasInit |
| 688 | // ParmVarDecl |
| 689 | Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 690 | Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 692 | ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv); |
| 693 | } |
| 694 | |
Daniel Dunbar | e24d38f | 2009-09-17 03:06:51 +0000 | [diff] [blame] | 695 | /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by |
| 696 | /// consumers of the AST. |
| 697 | /// |
| 698 | /// Such decls will always be deserialized from the PCH file, so we would like |
| 699 | /// this to be as restrictive as possible. Currently the predicate is driven by |
| 700 | /// code generation requirements, if other clients have a different notion of |
| 701 | /// what is "required" then we may have to consider an alternate scheme where |
| 702 | /// clients can iterate over the top-level decls and get information on them, |
| 703 | /// without necessary deserializing them. We could explicitly require such |
| 704 | /// clients to use a separate API call to "realize" the decl. This should be |
| 705 | /// relatively painless since they would presumably only do it for top-level |
| 706 | /// decls. |
| 707 | // |
| 708 | // FIXME: This predicate is essentially IRgen's predicate to determine whether a |
| 709 | // declaration can be deferred. Merge them somehow. |
| 710 | static bool isRequiredDecl(const Decl *D, ASTContext &Context) { |
| 711 | // File scoped assembly must be seen. |
| 712 | if (isa<FileScopeAsmDecl>(D)) |
| 713 | return true; |
| 714 | |
| 715 | // Otherwise if this isn't a function or a file scoped variable it doesn't |
| 716 | // need to be seen. |
| 717 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 718 | if (!VD->isFileVarDecl()) |
| 719 | return false; |
| 720 | } else if (!isa<FunctionDecl>(D)) |
| 721 | return false; |
| 722 | |
| 723 | // Aliases and used decls must be seen. |
| 724 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 725 | return true; |
| 726 | |
| 727 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 728 | // Forward declarations don't need to be seen. |
| 729 | if (!FD->isThisDeclarationADefinition()) |
| 730 | return false; |
| 731 | |
| 732 | // Constructors and destructors must be seen. |
| 733 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 734 | return true; |
| 735 | |
| 736 | // Otherwise, this is required unless it is static. |
| 737 | // |
| 738 | // FIXME: Inlines. |
| 739 | return FD->getStorageClass() != FunctionDecl::Static; |
| 740 | } else { |
| 741 | const VarDecl *VD = cast<VarDecl>(D); |
| 742 | |
| 743 | // In C++, this doesn't need to be seen if it is marked "extern". |
| 744 | if (Context.getLangOptions().CPlusPlus && !VD->getInit() && |
| 745 | (VD->getStorageClass() == VarDecl::Extern || |
| 746 | VD->isExternC())) |
| 747 | return false; |
| 748 | |
| 749 | // In C, this doesn't need to be seen unless it is a definition. |
| 750 | if (!Context.getLangOptions().CPlusPlus && !VD->getInit()) |
| 751 | return false; |
| 752 | |
| 753 | // Otherwise, this is required unless it is static. |
| 754 | return VD->getStorageClass() != VarDecl::Static; |
| 755 | } |
| 756 | } |
| 757 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 758 | void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) { |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 759 | RecordData Record; |
| 760 | PCHDeclWriter W(*this, Context, Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 762 | // If this declaration is also a DeclContext, write blocks for the |
| 763 | // declarations that lexically stored inside its context and those |
| 764 | // declarations that are visible from its context. These blocks |
| 765 | // are written before the declaration itself so that we can put |
| 766 | // their offsets into the record for the declaration. |
| 767 | uint64_t LexicalOffset = 0; |
| 768 | uint64_t VisibleOffset = 0; |
| 769 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 770 | if (DC) { |
| 771 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 772 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 775 | // Determine the ID for this declaration |
| 776 | pch::DeclID &ID = DeclIDs[D]; |
| 777 | if (ID == 0) |
| 778 | ID = DeclIDs.size(); |
| 779 | |
| 780 | unsigned Index = ID - 1; |
| 781 | |
| 782 | // Record the offset for this declaration |
| 783 | if (DeclOffsets.size() == Index) |
| 784 | DeclOffsets.push_back(Stream.GetCurrentBitNo()); |
| 785 | else if (DeclOffsets.size() < Index) { |
| 786 | DeclOffsets.resize(Index+1); |
| 787 | DeclOffsets[Index] = Stream.GetCurrentBitNo(); |
| 788 | } |
| 789 | |
| 790 | // Build and emit a record for this declaration |
| 791 | Record.clear(); |
| 792 | W.Code = (pch::DeclCode)0; |
| 793 | W.AbbrevToUse = 0; |
| 794 | W.Visit(D); |
| 795 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
| 796 | |
Daniel Dunbar | 3367198 | 2009-12-03 09:13:36 +0000 | [diff] [blame] | 797 | if (!W.Code) |
Chris Lattner | 83e7a78 | 2010-04-07 22:58:06 +0000 | [diff] [blame] | 798 | llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") + |
Daniel Dunbar | 3367198 | 2009-12-03 09:13:36 +0000 | [diff] [blame] | 799 | D->getDeclKindName() + "'"); |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 800 | Stream.EmitRecord(W.Code, Record, W.AbbrevToUse); |
| 801 | |
| 802 | // If the declaration had any attributes, write them now. |
| 803 | if (D->hasAttrs()) |
| 804 | WriteAttributeRecord(D->getAttrs()); |
| 805 | |
| 806 | // Flush any expressions that were written as part of this declaration. |
| 807 | FlushStmts(); |
| 808 | |
| 809 | // Note "external" declarations so that we can add them to a record in the |
| 810 | // PCH file later. |
| 811 | // |
| 812 | // FIXME: This should be renamed, the predicate is much more complicated. |
| 813 | if (isRequiredDecl(D, Context)) |
| 814 | ExternalDefinitions.push_back(Index + 1); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 815 | } |