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