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