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" |
| 16 | #include "clang/AST/Expr.h" |
Daniel Dunbar | 3367198 | 2009-12-03 09:13:36 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Twine.h" |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/BitstreamWriter.h" |
Daniel Dunbar | 3367198 | 2009-12-03 09:13:36 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // Declaration serialization |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | namespace { |
| 27 | class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> { |
| 28 | |
| 29 | PCHWriter &Writer; |
| 30 | ASTContext &Context; |
| 31 | PCHWriter::RecordData &Record; |
| 32 | |
| 33 | public: |
| 34 | pch::DeclCode Code; |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 35 | unsigned AbbrevToUse; |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 36 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | PCHDeclWriter(PCHWriter &Writer, ASTContext &Context, |
| 38 | PCHWriter::RecordData &Record) |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 39 | : Writer(Writer), Context(Context), Record(Record) { |
| 40 | } |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 41 | |
| 42 | void VisitDecl(Decl *D); |
| 43 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 44 | void VisitNamedDecl(NamedDecl *D); |
| 45 | void VisitTypeDecl(TypeDecl *D); |
| 46 | void VisitTypedefDecl(TypedefDecl *D); |
| 47 | void VisitTagDecl(TagDecl *D); |
| 48 | void VisitEnumDecl(EnumDecl *D); |
| 49 | void VisitRecordDecl(RecordDecl *D); |
| 50 | void VisitValueDecl(ValueDecl *D); |
| 51 | void VisitEnumConstantDecl(EnumConstantDecl *D); |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 52 | void VisitDeclaratorDecl(DeclaratorDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 53 | void VisitFunctionDecl(FunctionDecl *D); |
| 54 | void VisitFieldDecl(FieldDecl *D); |
| 55 | void VisitVarDecl(VarDecl *D); |
| 56 | void VisitImplicitParamDecl(ImplicitParamDecl *D); |
| 57 | void VisitParmVarDecl(ParmVarDecl *D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 58 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
| 59 | void VisitBlockDecl(BlockDecl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 61 | uint64_t VisibleOffset); |
| 62 | void VisitObjCMethodDecl(ObjCMethodDecl *D); |
| 63 | void VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 64 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 65 | void VisitObjCIvarDecl(ObjCIvarDecl *D); |
| 66 | void VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 67 | void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D); |
| 68 | void VisitObjCClassDecl(ObjCClassDecl *D); |
| 69 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 70 | void VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 71 | void VisitObjCImplDecl(ObjCImplDecl *D); |
| 72 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 73 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 74 | void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); |
| 75 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 76 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | void PCHDeclWriter::VisitDecl(Decl *D) { |
| 81 | Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); |
| 82 | Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); |
| 83 | Writer.AddSourceLocation(D->getLocation(), Record); |
| 84 | Record.push_back(D->isInvalidDecl()); |
| 85 | Record.push_back(D->hasAttrs()); |
| 86 | Record.push_back(D->isImplicit()); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 87 | Record.push_back(D->isUsed()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 88 | Record.push_back(D->getAccess()); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 89 | Record.push_back(D->getPCHLevel()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 93 | VisitDecl(D); |
| 94 | Code = pch::DECL_TRANSLATION_UNIT; |
| 95 | } |
| 96 | |
| 97 | void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) { |
| 98 | VisitDecl(D); |
| 99 | Writer.AddDeclarationName(D->getDeclName(), Record); |
| 100 | } |
| 101 | |
| 102 | void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) { |
| 103 | VisitNamedDecl(D); |
| 104 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 105 | } |
| 106 | |
| 107 | void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { |
| 108 | VisitTypeDecl(D); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 109 | Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 110 | Code = pch::DECL_TYPEDEF; |
| 111 | } |
| 112 | |
| 113 | void PCHDeclWriter::VisitTagDecl(TagDecl *D) { |
| 114 | VisitTypeDecl(D); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 115 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 116 | Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding |
| 117 | Record.push_back(D->isDefinition()); |
| 118 | Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record); |
Argyrios Kyrtzidis | ad93a74 | 2009-07-14 03:18:02 +0000 | [diff] [blame] | 119 | Writer.AddSourceLocation(D->getRBraceLoc(), Record); |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 120 | Writer.AddSourceLocation(D->getTagKeywordLoc(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) { |
| 124 | VisitTagDecl(D); |
| 125 | Writer.AddTypeRef(D->getIntegerType(), Record); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 126 | Writer.AddTypeRef(D->getPromotionType(), Record); |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 127 | // FIXME: C++ InstantiatedFrom |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 128 | Code = pch::DECL_ENUM; |
| 129 | } |
| 130 | |
| 131 | void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) { |
| 132 | VisitTagDecl(D); |
| 133 | Record.push_back(D->hasFlexibleArrayMember()); |
| 134 | Record.push_back(D->isAnonymousStructOrUnion()); |
Fariborz Jahanian | 643b7df | 2009-07-08 16:37:44 +0000 | [diff] [blame] | 135 | Record.push_back(D->hasObjectMember()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 136 | Code = pch::DECL_RECORD; |
| 137 | } |
| 138 | |
| 139 | void PCHDeclWriter::VisitValueDecl(ValueDecl *D) { |
| 140 | VisitNamedDecl(D); |
| 141 | Writer.AddTypeRef(D->getType(), Record); |
| 142 | } |
| 143 | |
| 144 | void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 145 | VisitValueDecl(D); |
| 146 | Record.push_back(D->getInitExpr()? 1 : 0); |
| 147 | if (D->getInitExpr()) |
| 148 | Writer.AddStmt(D->getInitExpr()); |
| 149 | Writer.AddAPSInt(D->getInitVal(), Record); |
| 150 | Code = pch::DECL_ENUM_CONSTANT; |
| 151 | } |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 152 | |
| 153 | void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) { |
| 154 | VisitValueDecl(D); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 155 | Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 157 | |
| 158 | void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) { |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 159 | VisitDeclaratorDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 160 | Record.push_back(D->isThisDeclarationADefinition()); |
| 161 | if (D->isThisDeclarationADefinition()) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 162 | Writer.AddStmt(D->getBody()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 163 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 164 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 165 | Record.push_back(D->isInlineSpecified()); |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 166 | Record.push_back(D->isVirtualAsWritten()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 167 | Record.push_back(D->isPure()); |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 168 | Record.push_back(D->hasInheritedPrototype()); |
| 169 | Record.push_back(D->hasWrittenPrototype()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 170 | Record.push_back(D->isDeleted()); |
Daniel Dunbar | 7f8b57a | 2009-09-22 05:38:14 +0000 | [diff] [blame] | 171 | Record.push_back(D->isTrivial()); |
| 172 | Record.push_back(D->isCopyAssignment()); |
| 173 | Record.push_back(D->hasImplicitReturnZero()); |
Argyrios Kyrtzidis | 8cff90e | 2009-06-20 08:09:34 +0000 | [diff] [blame] | 174 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 175 | // FIXME: C++ TemplateOrInstantiation |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 176 | Record.push_back(D->param_size()); |
| 177 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 178 | P != PEnd; ++P) |
| 179 | Writer.AddDeclRef(*P, Record); |
| 180 | Code = pch::DECL_FUNCTION; |
| 181 | } |
| 182 | |
| 183 | void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 184 | VisitNamedDecl(D); |
| 185 | // FIXME: convert to LazyStmtPtr? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | // Unlike C/C++, method bodies will never be in header files. |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 187 | Record.push_back(D->getBody() != 0); |
| 188 | if (D->getBody() != 0) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 189 | Writer.AddStmt(D->getBody()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 190 | Writer.AddDeclRef(D->getSelfDecl(), Record); |
| 191 | Writer.AddDeclRef(D->getCmdDecl(), Record); |
| 192 | } |
| 193 | Record.push_back(D->isInstanceMethod()); |
| 194 | Record.push_back(D->isVariadic()); |
| 195 | Record.push_back(D->isSynthesized()); |
| 196 | // FIXME: stable encoding for @required/@optional |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | Record.push_back(D->getImplementationControl()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 198 | // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | Record.push_back(D->getObjCDeclQualifier()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 200 | Writer.AddTypeRef(D->getResultType(), Record); |
| 201 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 202 | Record.push_back(D->param_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 204 | PEnd = D->param_end(); P != PEnd; ++P) |
| 205 | Writer.AddDeclRef(*P, Record); |
| 206 | Code = pch::DECL_OBJC_METHOD; |
| 207 | } |
| 208 | |
| 209 | void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 210 | VisitNamedDecl(D); |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 211 | SourceRange R = D->getAtEndRange(); |
| 212 | Writer.AddSourceLocation(R.getBegin(), Record); |
| 213 | Writer.AddSourceLocation(R.getEnd(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 214 | // Abstract class (no need to define a stable pch::DECL code). |
| 215 | } |
| 216 | |
| 217 | void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 218 | VisitObjCContainerDecl(D); |
| 219 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 220 | Writer.AddDeclRef(D->getSuperClass(), Record); |
| 221 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(), |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 223 | PEnd = D->protocol_end(); |
| 224 | P != PEnd; ++P) |
| 225 | Writer.AddDeclRef(*P, Record); |
| 226 | Record.push_back(D->ivar_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(), |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 228 | IEnd = D->ivar_end(); I != IEnd; ++I) |
| 229 | Writer.AddDeclRef(*I, Record); |
| 230 | Writer.AddDeclRef(D->getCategoryList(), Record); |
| 231 | Record.push_back(D->isForwardDecl()); |
| 232 | Record.push_back(D->isImplicitInterfaceDecl()); |
| 233 | Writer.AddSourceLocation(D->getClassLoc(), Record); |
| 234 | Writer.AddSourceLocation(D->getSuperClassLoc(), Record); |
| 235 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 236 | Code = pch::DECL_OBJC_INTERFACE; |
| 237 | } |
| 238 | |
| 239 | void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 240 | VisitFieldDecl(D); |
| 241 | // FIXME: stable encoding for @public/@private/@protected/@package |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | Record.push_back(D->getAccessControl()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 243 | Code = pch::DECL_OBJC_IVAR; |
| 244 | } |
| 245 | |
| 246 | void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
| 247 | VisitObjCContainerDecl(D); |
| 248 | Record.push_back(D->isForwardDecl()); |
| 249 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 250 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | for (ObjCProtocolDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 252 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 253 | Writer.AddDeclRef(*I, Record); |
| 254 | Code = pch::DECL_OBJC_PROTOCOL; |
| 255 | } |
| 256 | |
| 257 | void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 258 | VisitFieldDecl(D); |
| 259 | Code = pch::DECL_OBJC_AT_DEFS_FIELD; |
| 260 | } |
| 261 | |
| 262 | void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 263 | VisitDecl(D); |
| 264 | Record.push_back(D->size()); |
| 265 | for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 266 | Writer.AddDeclRef(I->getInterface(), Record); |
| 267 | for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) |
| 268 | Writer.AddSourceLocation(I->getLocation(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 269 | Code = pch::DECL_OBJC_CLASS; |
| 270 | } |
| 271 | |
| 272 | void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 273 | VisitDecl(D); |
| 274 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | for (ObjCProtocolDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 276 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 277 | Writer.AddDeclRef(*I, Record); |
| 278 | Code = pch::DECL_OBJC_FORWARD_PROTOCOL; |
| 279 | } |
| 280 | |
| 281 | void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 282 | VisitObjCContainerDecl(D); |
| 283 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 284 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | for (ObjCProtocolDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 286 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 287 | Writer.AddDeclRef(*I, Record); |
| 288 | Writer.AddDeclRef(D->getNextClassCategory(), Record); |
| 289 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 290 | Code = pch::DECL_OBJC_CATEGORY; |
| 291 | } |
| 292 | |
| 293 | void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) { |
| 294 | VisitNamedDecl(D); |
| 295 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 296 | Code = pch::DECL_OBJC_COMPATIBLE_ALIAS; |
| 297 | } |
| 298 | |
| 299 | void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 300 | VisitNamedDecl(D); |
| 301 | Writer.AddTypeRef(D->getType(), Record); |
| 302 | // FIXME: stable encoding |
| 303 | Record.push_back((unsigned)D->getPropertyAttributes()); |
| 304 | // FIXME: stable encoding |
| 305 | Record.push_back((unsigned)D->getPropertyImplementation()); |
| 306 | Writer.AddDeclarationName(D->getGetterName(), Record); |
| 307 | Writer.AddDeclarationName(D->getSetterName(), Record); |
| 308 | Writer.AddDeclRef(D->getGetterMethodDecl(), Record); |
| 309 | Writer.AddDeclRef(D->getSetterMethodDecl(), Record); |
| 310 | Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); |
| 311 | Code = pch::DECL_OBJC_PROPERTY; |
| 312 | } |
| 313 | |
| 314 | void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) { |
Argyrios Kyrtzidis | aecae62 | 2009-07-27 19:04:32 +0000 | [diff] [blame] | 315 | VisitObjCContainerDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 316 | Writer.AddDeclRef(D->getClassInterface(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 317 | // Abstract class (no need to define a stable pch::DECL code). |
| 318 | } |
| 319 | |
| 320 | void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 321 | VisitObjCImplDecl(D); |
| 322 | Writer.AddIdentifierRef(D->getIdentifier(), Record); |
| 323 | Code = pch::DECL_OBJC_CATEGORY_IMPL; |
| 324 | } |
| 325 | |
| 326 | void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 327 | VisitObjCImplDecl(D); |
| 328 | Writer.AddDeclRef(D->getSuperClass(), Record); |
| 329 | Code = pch::DECL_OBJC_IMPLEMENTATION; |
| 330 | } |
| 331 | |
| 332 | void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 333 | VisitDecl(D); |
| 334 | Writer.AddSourceLocation(D->getLocStart(), Record); |
| 335 | Writer.AddDeclRef(D->getPropertyDecl(), Record); |
| 336 | Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); |
| 337 | Code = pch::DECL_OBJC_PROPERTY_IMPL; |
| 338 | } |
| 339 | |
| 340 | void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) { |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 341 | VisitDeclaratorDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 342 | Record.push_back(D->isMutable()); |
| 343 | Record.push_back(D->getBitWidth()? 1 : 0); |
| 344 | if (D->getBitWidth()) |
| 345 | Writer.AddStmt(D->getBitWidth()); |
| 346 | Code = pch::DECL_FIELD; |
| 347 | } |
| 348 | |
| 349 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 350 | VisitDeclaratorDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 351 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
| 352 | Record.push_back(D->isThreadSpecified()); |
| 353 | Record.push_back(D->hasCXXDirectInitializer()); |
| 354 | Record.push_back(D->isDeclaredInCondition()); |
| 355 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 356 | Record.push_back(D->getInit()? 1 : 0); |
| 357 | if (D->getInit()) |
| 358 | Writer.AddStmt(D->getInit()); |
| 359 | Code = pch::DECL_VAR; |
| 360 | } |
| 361 | |
| 362 | void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 363 | VisitVarDecl(D); |
| 364 | Code = pch::DECL_IMPLICIT_PARAM; |
| 365 | } |
| 366 | |
| 367 | void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { |
| 368 | VisitVarDecl(D); |
| 369 | Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 370 | Code = pch::DECL_PARM_VAR; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
| 372 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 373 | // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here |
| 374 | // we dynamically check for the properties that we optimize for, but don't |
| 375 | // know are true of all PARM_VAR_DECLs. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 376 | if (!D->getTypeSourceInfo() && |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 377 | !D->hasAttrs() && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 378 | !D->isImplicit() && |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 379 | !D->isUsed() && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 380 | D->getAccess() == AS_none && |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 381 | D->getPCHLevel() == 0 && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 382 | D->getStorageClass() == 0 && |
| 383 | !D->hasCXXDirectInitializer() && // Can params have this ever? |
| 384 | D->getObjCDeclQualifier() == 0) |
| 385 | AbbrevToUse = Writer.getParmVarDeclAbbrev(); |
| 386 | |
| 387 | // Check things we know are true of *every* PARM_VAR_DECL, which is more than |
| 388 | // just us assuming it. |
| 389 | assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls"); |
| 390 | assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread"); |
| 391 | assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private"); |
| 392 | assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition"); |
| 393 | assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl"); |
| 394 | assert(D->getInit() == 0 && "PARM_VAR_DECL never has init"); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 397 | void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 398 | VisitDecl(D); |
| 399 | Writer.AddStmt(D->getAsmString()); |
| 400 | Code = pch::DECL_FILE_SCOPE_ASM; |
| 401 | } |
| 402 | |
| 403 | void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) { |
| 404 | VisitDecl(D); |
| 405 | Writer.AddStmt(D->getBody()); |
| 406 | Record.push_back(D->param_size()); |
| 407 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 408 | P != PEnd; ++P) |
| 409 | Writer.AddDeclRef(*P, Record); |
| 410 | Code = pch::DECL_BLOCK; |
| 411 | } |
| 412 | |
| 413 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 414 | /// |
| 415 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 416 | /// block for this declaration context is stored. May be 0 to indicate |
| 417 | /// that there are no declarations stored within this context. |
| 418 | /// |
| 419 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 420 | /// block for this declaration context is stored. May be 0 to indicate |
| 421 | /// that there are no declarations visible from this context. Note |
| 422 | /// that this value will not be emitted for non-primary declaration |
| 423 | /// contexts. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 425 | uint64_t VisibleOffset) { |
| 426 | Record.push_back(LexicalOffset); |
| 427 | Record.push_back(VisibleOffset); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | //===----------------------------------------------------------------------===// |
| 432 | // PCHWriter Implementation |
| 433 | //===----------------------------------------------------------------------===// |
| 434 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 435 | void PCHWriter::WriteDeclsBlockAbbrevs() { |
| 436 | using namespace llvm; |
| 437 | // Abbreviation for DECL_PARM_VAR. |
| 438 | BitCodeAbbrev *Abv = new BitCodeAbbrev(); |
| 439 | Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR)); |
| 440 | |
| 441 | // Decl |
| 442 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext |
| 443 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext |
| 444 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location |
| 445 | Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) |
| 446 | Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs |
| 447 | Abv->Add(BitCodeAbbrevOp(0)); // isImplicit |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 448 | Abv->Add(BitCodeAbbrevOp(0)); // isUsed |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 449 | Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 450 | Abv->Add(BitCodeAbbrevOp(0)); // PCH level |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 452 | // NamedDecl |
| 453 | Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier |
| 454 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name |
| 455 | // ValueDecl |
| 456 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 457 | // DeclaratorDecl |
| 458 | Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 459 | // VarDecl |
| 460 | Abv->Add(BitCodeAbbrevOp(0)); // StorageClass |
| 461 | Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified |
| 462 | Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer |
| 463 | Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition |
| 464 | Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 465 | Abv->Add(BitCodeAbbrevOp(0)); // HasInit |
| 466 | // ParmVarDecl |
| 467 | Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 469 | ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv); |
| 470 | } |
| 471 | |
Daniel Dunbar | e24d38f | 2009-09-17 03:06:51 +0000 | [diff] [blame] | 472 | /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by |
| 473 | /// consumers of the AST. |
| 474 | /// |
| 475 | /// Such decls will always be deserialized from the PCH file, so we would like |
| 476 | /// this to be as restrictive as possible. Currently the predicate is driven by |
| 477 | /// code generation requirements, if other clients have a different notion of |
| 478 | /// what is "required" then we may have to consider an alternate scheme where |
| 479 | /// clients can iterate over the top-level decls and get information on them, |
| 480 | /// without necessary deserializing them. We could explicitly require such |
| 481 | /// clients to use a separate API call to "realize" the decl. This should be |
| 482 | /// relatively painless since they would presumably only do it for top-level |
| 483 | /// decls. |
| 484 | // |
| 485 | // FIXME: This predicate is essentially IRgen's predicate to determine whether a |
| 486 | // declaration can be deferred. Merge them somehow. |
| 487 | static bool isRequiredDecl(const Decl *D, ASTContext &Context) { |
| 488 | // File scoped assembly must be seen. |
| 489 | if (isa<FileScopeAsmDecl>(D)) |
| 490 | return true; |
| 491 | |
| 492 | // Otherwise if this isn't a function or a file scoped variable it doesn't |
| 493 | // need to be seen. |
| 494 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 495 | if (!VD->isFileVarDecl()) |
| 496 | return false; |
| 497 | } else if (!isa<FunctionDecl>(D)) |
| 498 | return false; |
| 499 | |
| 500 | // Aliases and used decls must be seen. |
| 501 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 502 | return true; |
| 503 | |
| 504 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 505 | // Forward declarations don't need to be seen. |
| 506 | if (!FD->isThisDeclarationADefinition()) |
| 507 | return false; |
| 508 | |
| 509 | // Constructors and destructors must be seen. |
| 510 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 511 | return true; |
| 512 | |
| 513 | // Otherwise, this is required unless it is static. |
| 514 | // |
| 515 | // FIXME: Inlines. |
| 516 | return FD->getStorageClass() != FunctionDecl::Static; |
| 517 | } else { |
| 518 | const VarDecl *VD = cast<VarDecl>(D); |
| 519 | |
| 520 | // In C++, this doesn't need to be seen if it is marked "extern". |
| 521 | if (Context.getLangOptions().CPlusPlus && !VD->getInit() && |
| 522 | (VD->getStorageClass() == VarDecl::Extern || |
| 523 | VD->isExternC())) |
| 524 | return false; |
| 525 | |
| 526 | // In C, this doesn't need to be seen unless it is a definition. |
| 527 | if (!Context.getLangOptions().CPlusPlus && !VD->getInit()) |
| 528 | return false; |
| 529 | |
| 530 | // Otherwise, this is required unless it is static. |
| 531 | return VD->getStorageClass() != VarDecl::Static; |
| 532 | } |
| 533 | } |
| 534 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 535 | void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) { |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 536 | RecordData Record; |
| 537 | PCHDeclWriter W(*this, Context, Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 538 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 539 | // If this declaration is also a DeclContext, write blocks for the |
| 540 | // declarations that lexically stored inside its context and those |
| 541 | // declarations that are visible from its context. These blocks |
| 542 | // are written before the declaration itself so that we can put |
| 543 | // their offsets into the record for the declaration. |
| 544 | uint64_t LexicalOffset = 0; |
| 545 | uint64_t VisibleOffset = 0; |
| 546 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 547 | if (DC) { |
| 548 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 549 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 552 | // Determine the ID for this declaration |
| 553 | pch::DeclID &ID = DeclIDs[D]; |
| 554 | if (ID == 0) |
| 555 | ID = DeclIDs.size(); |
| 556 | |
| 557 | unsigned Index = ID - 1; |
| 558 | |
| 559 | // Record the offset for this declaration |
| 560 | if (DeclOffsets.size() == Index) |
| 561 | DeclOffsets.push_back(Stream.GetCurrentBitNo()); |
| 562 | else if (DeclOffsets.size() < Index) { |
| 563 | DeclOffsets.resize(Index+1); |
| 564 | DeclOffsets[Index] = Stream.GetCurrentBitNo(); |
| 565 | } |
| 566 | |
| 567 | // Build and emit a record for this declaration |
| 568 | Record.clear(); |
| 569 | W.Code = (pch::DeclCode)0; |
| 570 | W.AbbrevToUse = 0; |
| 571 | W.Visit(D); |
| 572 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
| 573 | |
Daniel Dunbar | 3367198 | 2009-12-03 09:13:36 +0000 | [diff] [blame] | 574 | if (!W.Code) |
| 575 | llvm::llvm_report_error(llvm::StringRef("unexpected declaration kind '") + |
| 576 | D->getDeclKindName() + "'"); |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 577 | Stream.EmitRecord(W.Code, Record, W.AbbrevToUse); |
| 578 | |
| 579 | // If the declaration had any attributes, write them now. |
| 580 | if (D->hasAttrs()) |
| 581 | WriteAttributeRecord(D->getAttrs()); |
| 582 | |
| 583 | // Flush any expressions that were written as part of this declaration. |
| 584 | FlushStmts(); |
| 585 | |
| 586 | // Note "external" declarations so that we can add them to a record in the |
| 587 | // PCH file later. |
| 588 | // |
| 589 | // FIXME: This should be renamed, the predicate is much more complicated. |
| 590 | if (isRequiredDecl(D, Context)) |
| 591 | ExternalDefinitions.push_back(Index + 1); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 592 | } |