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" |
| 17 | #include "llvm/Bitcode/BitstreamWriter.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 18 | #include <cstdio> |
| 19 | |
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); |
| 58 | void VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
| 59 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
| 60 | void VisitBlockDecl(BlockDecl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 62 | uint64_t VisibleOffset); |
| 63 | void VisitObjCMethodDecl(ObjCMethodDecl *D); |
| 64 | void VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 65 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 66 | void VisitObjCIvarDecl(ObjCIvarDecl *D); |
| 67 | void VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 68 | void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D); |
| 69 | void VisitObjCClassDecl(ObjCClassDecl *D); |
| 70 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 71 | void VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 72 | void VisitObjCImplDecl(ObjCImplDecl *D); |
| 73 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 74 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 75 | void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); |
| 76 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 77 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | void PCHDeclWriter::VisitDecl(Decl *D) { |
| 82 | Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); |
| 83 | Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); |
| 84 | Writer.AddSourceLocation(D->getLocation(), Record); |
| 85 | Record.push_back(D->isInvalidDecl()); |
| 86 | Record.push_back(D->hasAttrs()); |
| 87 | Record.push_back(D->isImplicit()); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 88 | Record.push_back(D->isUsed()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 89 | Record.push_back(D->getAccess()); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 90 | Record.push_back(D->getPCHLevel()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 94 | VisitDecl(D); |
| 95 | Code = pch::DECL_TRANSLATION_UNIT; |
| 96 | } |
| 97 | |
| 98 | void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) { |
| 99 | VisitDecl(D); |
| 100 | Writer.AddDeclarationName(D->getDeclName(), Record); |
| 101 | } |
| 102 | |
| 103 | void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) { |
| 104 | VisitNamedDecl(D); |
| 105 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 106 | } |
| 107 | |
| 108 | void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { |
| 109 | VisitTypeDecl(D); |
| 110 | Writer.AddTypeRef(D->getUnderlyingType(), Record); |
| 111 | Code = pch::DECL_TYPEDEF; |
| 112 | } |
| 113 | |
| 114 | void PCHDeclWriter::VisitTagDecl(TagDecl *D) { |
| 115 | VisitTypeDecl(D); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 116 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 117 | Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding |
| 118 | Record.push_back(D->isDefinition()); |
| 119 | Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record); |
Argyrios Kyrtzidis | ad93a74 | 2009-07-14 03:18:02 +0000 | [diff] [blame] | 120 | Writer.AddSourceLocation(D->getRBraceLoc(), Record); |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 121 | Writer.AddSourceLocation(D->getTagKeywordLoc(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) { |
| 125 | VisitTagDecl(D); |
| 126 | Writer.AddTypeRef(D->getIntegerType(), 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 | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame^] | 155 | Writer.AddDeclaratorInfo(D->getDeclaratorInfo(), 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 |
| 165 | Record.push_back(D->isInline()); |
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); |
| 211 | Writer.AddSourceLocation(D->getAtEndLoc(), Record); |
| 212 | // Abstract class (no need to define a stable pch::DECL code). |
| 213 | } |
| 214 | |
| 215 | void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 216 | VisitObjCContainerDecl(D); |
| 217 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 218 | Writer.AddDeclRef(D->getSuperClass(), Record); |
| 219 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(), |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 221 | PEnd = D->protocol_end(); |
| 222 | P != PEnd; ++P) |
| 223 | Writer.AddDeclRef(*P, Record); |
| 224 | Record.push_back(D->ivar_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(), |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 226 | IEnd = D->ivar_end(); I != IEnd; ++I) |
| 227 | Writer.AddDeclRef(*I, Record); |
| 228 | Writer.AddDeclRef(D->getCategoryList(), Record); |
| 229 | Record.push_back(D->isForwardDecl()); |
| 230 | Record.push_back(D->isImplicitInterfaceDecl()); |
| 231 | Writer.AddSourceLocation(D->getClassLoc(), Record); |
| 232 | Writer.AddSourceLocation(D->getSuperClassLoc(), Record); |
| 233 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 234 | Code = pch::DECL_OBJC_INTERFACE; |
| 235 | } |
| 236 | |
| 237 | void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 238 | VisitFieldDecl(D); |
| 239 | // FIXME: stable encoding for @public/@private/@protected/@package |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | Record.push_back(D->getAccessControl()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 241 | Code = pch::DECL_OBJC_IVAR; |
| 242 | } |
| 243 | |
| 244 | void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
| 245 | VisitObjCContainerDecl(D); |
| 246 | Record.push_back(D->isForwardDecl()); |
| 247 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 248 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | for (ObjCProtocolDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 250 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 251 | Writer.AddDeclRef(*I, Record); |
| 252 | Code = pch::DECL_OBJC_PROTOCOL; |
| 253 | } |
| 254 | |
| 255 | void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 256 | VisitFieldDecl(D); |
| 257 | Code = pch::DECL_OBJC_AT_DEFS_FIELD; |
| 258 | } |
| 259 | |
| 260 | void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 261 | VisitDecl(D); |
| 262 | Record.push_back(D->size()); |
| 263 | for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) |
| 264 | Writer.AddDeclRef(*I, Record); |
| 265 | Code = pch::DECL_OBJC_CLASS; |
| 266 | } |
| 267 | |
| 268 | void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 269 | VisitDecl(D); |
| 270 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | for (ObjCProtocolDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 272 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 273 | Writer.AddDeclRef(*I, Record); |
| 274 | Code = pch::DECL_OBJC_FORWARD_PROTOCOL; |
| 275 | } |
| 276 | |
| 277 | void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 278 | VisitObjCContainerDecl(D); |
| 279 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 280 | Record.push_back(D->protocol_size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | for (ObjCProtocolDecl::protocol_iterator |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 282 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 283 | Writer.AddDeclRef(*I, Record); |
| 284 | Writer.AddDeclRef(D->getNextClassCategory(), Record); |
| 285 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 286 | Code = pch::DECL_OBJC_CATEGORY; |
| 287 | } |
| 288 | |
| 289 | void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) { |
| 290 | VisitNamedDecl(D); |
| 291 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 292 | Code = pch::DECL_OBJC_COMPATIBLE_ALIAS; |
| 293 | } |
| 294 | |
| 295 | void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 296 | VisitNamedDecl(D); |
| 297 | Writer.AddTypeRef(D->getType(), Record); |
| 298 | // FIXME: stable encoding |
| 299 | Record.push_back((unsigned)D->getPropertyAttributes()); |
| 300 | // FIXME: stable encoding |
| 301 | Record.push_back((unsigned)D->getPropertyImplementation()); |
| 302 | Writer.AddDeclarationName(D->getGetterName(), Record); |
| 303 | Writer.AddDeclarationName(D->getSetterName(), Record); |
| 304 | Writer.AddDeclRef(D->getGetterMethodDecl(), Record); |
| 305 | Writer.AddDeclRef(D->getSetterMethodDecl(), Record); |
| 306 | Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); |
| 307 | Code = pch::DECL_OBJC_PROPERTY; |
| 308 | } |
| 309 | |
| 310 | void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) { |
Argyrios Kyrtzidis | aecae62 | 2009-07-27 19:04:32 +0000 | [diff] [blame] | 311 | VisitObjCContainerDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 312 | Writer.AddDeclRef(D->getClassInterface(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 313 | // Abstract class (no need to define a stable pch::DECL code). |
| 314 | } |
| 315 | |
| 316 | void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 317 | VisitObjCImplDecl(D); |
| 318 | Writer.AddIdentifierRef(D->getIdentifier(), Record); |
| 319 | Code = pch::DECL_OBJC_CATEGORY_IMPL; |
| 320 | } |
| 321 | |
| 322 | void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 323 | VisitObjCImplDecl(D); |
| 324 | Writer.AddDeclRef(D->getSuperClass(), Record); |
| 325 | Code = pch::DECL_OBJC_IMPLEMENTATION; |
| 326 | } |
| 327 | |
| 328 | void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 329 | VisitDecl(D); |
| 330 | Writer.AddSourceLocation(D->getLocStart(), Record); |
| 331 | Writer.AddDeclRef(D->getPropertyDecl(), Record); |
| 332 | Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); |
| 333 | Code = pch::DECL_OBJC_PROPERTY_IMPL; |
| 334 | } |
| 335 | |
| 336 | void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) { |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 337 | VisitDeclaratorDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 338 | Record.push_back(D->isMutable()); |
| 339 | Record.push_back(D->getBitWidth()? 1 : 0); |
| 340 | if (D->getBitWidth()) |
| 341 | Writer.AddStmt(D->getBitWidth()); |
| 342 | Code = pch::DECL_FIELD; |
| 343 | } |
| 344 | |
| 345 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 346 | VisitDeclaratorDecl(D); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 347 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
| 348 | Record.push_back(D->isThreadSpecified()); |
| 349 | Record.push_back(D->hasCXXDirectInitializer()); |
| 350 | Record.push_back(D->isDeclaredInCondition()); |
| 351 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 352 | Record.push_back(D->getInit()? 1 : 0); |
| 353 | if (D->getInit()) |
| 354 | Writer.AddStmt(D->getInit()); |
| 355 | Code = pch::DECL_VAR; |
| 356 | } |
| 357 | |
| 358 | void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 359 | VisitVarDecl(D); |
| 360 | Code = pch::DECL_IMPLICIT_PARAM; |
| 361 | } |
| 362 | |
| 363 | void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { |
| 364 | VisitVarDecl(D); |
| 365 | Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 366 | Code = pch::DECL_PARM_VAR; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
| 368 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 369 | // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here |
| 370 | // we dynamically check for the properties that we optimize for, but don't |
| 371 | // know are true of all PARM_VAR_DECLs. |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 372 | if (!D->getDeclaratorInfo() && |
| 373 | !D->hasAttrs() && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 374 | !D->isImplicit() && |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 375 | !D->isUsed() && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 376 | D->getAccess() == AS_none && |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 377 | D->getPCHLevel() == 0 && |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 378 | D->getStorageClass() == 0 && |
| 379 | !D->hasCXXDirectInitializer() && // Can params have this ever? |
| 380 | D->getObjCDeclQualifier() == 0) |
| 381 | AbbrevToUse = Writer.getParmVarDeclAbbrev(); |
| 382 | |
| 383 | // Check things we know are true of *every* PARM_VAR_DECL, which is more than |
| 384 | // just us assuming it. |
| 385 | assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls"); |
| 386 | assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread"); |
| 387 | assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private"); |
| 388 | assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition"); |
| 389 | assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl"); |
| 390 | assert(D->getInit() == 0 && "PARM_VAR_DECL never has init"); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 394 | VisitParmVarDecl(D); |
| 395 | Writer.AddTypeRef(D->getOriginalType(), Record); |
| 396 | Code = pch::DECL_ORIGINAL_PARM_VAR; |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 397 | AbbrevToUse = 0; |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 401 | VisitDecl(D); |
| 402 | Writer.AddStmt(D->getAsmString()); |
| 403 | Code = pch::DECL_FILE_SCOPE_ASM; |
| 404 | } |
| 405 | |
| 406 | void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) { |
| 407 | VisitDecl(D); |
| 408 | Writer.AddStmt(D->getBody()); |
| 409 | Record.push_back(D->param_size()); |
| 410 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 411 | P != PEnd; ++P) |
| 412 | Writer.AddDeclRef(*P, Record); |
| 413 | Code = pch::DECL_BLOCK; |
| 414 | } |
| 415 | |
| 416 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 417 | /// |
| 418 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 419 | /// block for this declaration context is stored. May be 0 to indicate |
| 420 | /// that there are no declarations stored within this context. |
| 421 | /// |
| 422 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 423 | /// block for this declaration context is stored. May be 0 to indicate |
| 424 | /// that there are no declarations visible from this context. Note |
| 425 | /// that this value will not be emitted for non-primary declaration |
| 426 | /// contexts. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 428 | uint64_t VisibleOffset) { |
| 429 | Record.push_back(LexicalOffset); |
| 430 | Record.push_back(VisibleOffset); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | // PCHWriter Implementation |
| 436 | //===----------------------------------------------------------------------===// |
| 437 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 438 | void PCHWriter::WriteDeclsBlockAbbrevs() { |
| 439 | using namespace llvm; |
| 440 | // Abbreviation for DECL_PARM_VAR. |
| 441 | BitCodeAbbrev *Abv = new BitCodeAbbrev(); |
| 442 | Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR)); |
| 443 | |
| 444 | // Decl |
| 445 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext |
| 446 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext |
| 447 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location |
| 448 | Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) |
| 449 | Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs |
| 450 | Abv->Add(BitCodeAbbrevOp(0)); // isImplicit |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 451 | Abv->Add(BitCodeAbbrevOp(0)); // isUsed |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 452 | Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 453 | Abv->Add(BitCodeAbbrevOp(0)); // PCH level |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 455 | // NamedDecl |
| 456 | Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier |
| 457 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name |
| 458 | // ValueDecl |
| 459 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type |
Argyrios Kyrtzidis | d4a7e54 | 2009-08-19 01:28:35 +0000 | [diff] [blame] | 460 | // DeclaratorDecl |
| 461 | Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 462 | // VarDecl |
| 463 | Abv->Add(BitCodeAbbrevOp(0)); // StorageClass |
| 464 | Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified |
| 465 | Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer |
| 466 | Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition |
| 467 | Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 468 | Abv->Add(BitCodeAbbrevOp(0)); // HasInit |
| 469 | // ParmVarDecl |
| 470 | Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 472 | ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv); |
| 473 | } |
| 474 | |
Daniel Dunbar | e24d38f | 2009-09-17 03:06:51 +0000 | [diff] [blame] | 475 | /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by |
| 476 | /// consumers of the AST. |
| 477 | /// |
| 478 | /// Such decls will always be deserialized from the PCH file, so we would like |
| 479 | /// this to be as restrictive as possible. Currently the predicate is driven by |
| 480 | /// code generation requirements, if other clients have a different notion of |
| 481 | /// what is "required" then we may have to consider an alternate scheme where |
| 482 | /// clients can iterate over the top-level decls and get information on them, |
| 483 | /// without necessary deserializing them. We could explicitly require such |
| 484 | /// clients to use a separate API call to "realize" the decl. This should be |
| 485 | /// relatively painless since they would presumably only do it for top-level |
| 486 | /// decls. |
| 487 | // |
| 488 | // FIXME: This predicate is essentially IRgen's predicate to determine whether a |
| 489 | // declaration can be deferred. Merge them somehow. |
| 490 | static bool isRequiredDecl(const Decl *D, ASTContext &Context) { |
| 491 | // File scoped assembly must be seen. |
| 492 | if (isa<FileScopeAsmDecl>(D)) |
| 493 | return true; |
| 494 | |
| 495 | // Otherwise if this isn't a function or a file scoped variable it doesn't |
| 496 | // need to be seen. |
| 497 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 498 | if (!VD->isFileVarDecl()) |
| 499 | return false; |
| 500 | } else if (!isa<FunctionDecl>(D)) |
| 501 | return false; |
| 502 | |
| 503 | // Aliases and used decls must be seen. |
| 504 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 505 | return true; |
| 506 | |
| 507 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 508 | // Forward declarations don't need to be seen. |
| 509 | if (!FD->isThisDeclarationADefinition()) |
| 510 | return false; |
| 511 | |
| 512 | // Constructors and destructors must be seen. |
| 513 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 514 | return true; |
| 515 | |
| 516 | // Otherwise, this is required unless it is static. |
| 517 | // |
| 518 | // FIXME: Inlines. |
| 519 | return FD->getStorageClass() != FunctionDecl::Static; |
| 520 | } else { |
| 521 | const VarDecl *VD = cast<VarDecl>(D); |
| 522 | |
| 523 | // In C++, this doesn't need to be seen if it is marked "extern". |
| 524 | if (Context.getLangOptions().CPlusPlus && !VD->getInit() && |
| 525 | (VD->getStorageClass() == VarDecl::Extern || |
| 526 | VD->isExternC())) |
| 527 | return false; |
| 528 | |
| 529 | // In C, this doesn't need to be seen unless it is a definition. |
| 530 | if (!Context.getLangOptions().CPlusPlus && !VD->getInit()) |
| 531 | return false; |
| 532 | |
| 533 | // Otherwise, this is required unless it is static. |
| 534 | return VD->getStorageClass() != VarDecl::Static; |
| 535 | } |
| 536 | } |
| 537 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 538 | /// \brief Write a block containing all of the declarations. |
| 539 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
| 540 | // Enter the declarations block. |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 541 | Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 3); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 542 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 543 | // Output the abbreviations that we will use in this block. |
| 544 | WriteDeclsBlockAbbrevs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 546 | // Emit all of the declarations. |
| 547 | RecordData Record; |
| 548 | PCHDeclWriter W(*this, Context, Record); |
| 549 | while (!DeclsToEmit.empty()) { |
| 550 | // Pull the next declaration off the queue |
| 551 | Decl *D = DeclsToEmit.front(); |
| 552 | DeclsToEmit.pop(); |
| 553 | |
| 554 | // If this declaration is also a DeclContext, write blocks for the |
| 555 | // declarations that lexically stored inside its context and those |
| 556 | // declarations that are visible from its context. These blocks |
| 557 | // are written before the declaration itself so that we can put |
| 558 | // their offsets into the record for the declaration. |
| 559 | uint64_t LexicalOffset = 0; |
| 560 | uint64_t VisibleOffset = 0; |
| 561 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 562 | if (DC) { |
| 563 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 564 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 565 | } |
| 566 | |
| 567 | // Determine the ID for this declaration |
Zhongxing Xu | 44dfc98 | 2009-06-01 00:50:23 +0000 | [diff] [blame] | 568 | pch::DeclID &ID = DeclIDs[D]; |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 569 | if (ID == 0) |
| 570 | ID = DeclIDs.size(); |
| 571 | |
| 572 | unsigned Index = ID - 1; |
| 573 | |
| 574 | // Record the offset for this declaration |
| 575 | if (DeclOffsets.size() == Index) |
| 576 | DeclOffsets.push_back(Stream.GetCurrentBitNo()); |
| 577 | else if (DeclOffsets.size() < Index) { |
| 578 | DeclOffsets.resize(Index+1); |
| 579 | DeclOffsets[Index] = Stream.GetCurrentBitNo(); |
| 580 | } |
| 581 | |
| 582 | // Build and emit a record for this declaration |
| 583 | Record.clear(); |
| 584 | W.Code = (pch::DeclCode)0; |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 585 | W.AbbrevToUse = 0; |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 586 | W.Visit(D); |
| 587 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
| 588 | |
| 589 | if (!W.Code) { |
| 590 | fprintf(stderr, "Cannot serialize declaration of kind %s\n", |
| 591 | D->getDeclKindName()); |
| 592 | assert(false && "Unhandled declaration kind while generating PCH"); |
| 593 | exit(-1); |
| 594 | } |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 595 | Stream.EmitRecord(W.Code, Record, W.AbbrevToUse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 597 | // If the declaration had any attributes, write them now. |
| 598 | if (D->hasAttrs()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 599 | WriteAttributeRecord(D->getAttrs()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 600 | |
| 601 | // Flush any expressions that were written as part of this declaration. |
| 602 | FlushStmts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | |
Daniel Dunbar | e24d38f | 2009-09-17 03:06:51 +0000 | [diff] [blame] | 604 | // Note "external" declarations so that we can add them to a record in the |
| 605 | // PCH file later. |
| 606 | // |
| 607 | // FIXME: This should be renamed, the predicate is much more complicated. |
| 608 | if (isRequiredDecl(D, Context)) |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 609 | ExternalDefinitions.push_back(ID); |
| 610 | } |
| 611 | |
| 612 | // Exit the declarations block |
| 613 | Stream.ExitBlock(); |
| 614 | } |