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