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