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" |
| 18 | using namespace clang; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // Declaration serialization |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | namespace { |
| 25 | class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> { |
| 26 | |
| 27 | PCHWriter &Writer; |
| 28 | ASTContext &Context; |
| 29 | PCHWriter::RecordData &Record; |
| 30 | |
| 31 | public: |
| 32 | pch::DeclCode Code; |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 33 | unsigned AbbrevToUse; |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 34 | |
| 35 | PCHDeclWriter(PCHWriter &Writer, ASTContext &Context, |
| 36 | PCHWriter::RecordData &Record) |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 37 | : Writer(Writer), Context(Context), Record(Record) { |
| 38 | } |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 39 | |
| 40 | void VisitDecl(Decl *D); |
| 41 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 42 | void VisitNamedDecl(NamedDecl *D); |
| 43 | void VisitTypeDecl(TypeDecl *D); |
| 44 | void VisitTypedefDecl(TypedefDecl *D); |
| 45 | void VisitTagDecl(TagDecl *D); |
| 46 | void VisitEnumDecl(EnumDecl *D); |
| 47 | void VisitRecordDecl(RecordDecl *D); |
| 48 | void VisitValueDecl(ValueDecl *D); |
| 49 | void VisitEnumConstantDecl(EnumConstantDecl *D); |
| 50 | void VisitFunctionDecl(FunctionDecl *D); |
| 51 | void VisitFieldDecl(FieldDecl *D); |
| 52 | void VisitVarDecl(VarDecl *D); |
| 53 | void VisitImplicitParamDecl(ImplicitParamDecl *D); |
| 54 | void VisitParmVarDecl(ParmVarDecl *D); |
| 55 | void VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
| 56 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
| 57 | void VisitBlockDecl(BlockDecl *D); |
| 58 | void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 59 | uint64_t VisibleOffset); |
| 60 | void VisitObjCMethodDecl(ObjCMethodDecl *D); |
| 61 | void VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 62 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 63 | void VisitObjCIvarDecl(ObjCIvarDecl *D); |
| 64 | void VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 65 | void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D); |
| 66 | void VisitObjCClassDecl(ObjCClassDecl *D); |
| 67 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 68 | void VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 69 | void VisitObjCImplDecl(ObjCImplDecl *D); |
| 70 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 71 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 72 | void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); |
| 73 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 74 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | void PCHDeclWriter::VisitDecl(Decl *D) { |
| 79 | Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); |
| 80 | Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); |
| 81 | Writer.AddSourceLocation(D->getLocation(), Record); |
| 82 | Record.push_back(D->isInvalidDecl()); |
| 83 | Record.push_back(D->hasAttrs()); |
| 84 | Record.push_back(D->isImplicit()); |
| 85 | Record.push_back(D->getAccess()); |
| 86 | } |
| 87 | |
| 88 | void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 89 | VisitDecl(D); |
| 90 | Code = pch::DECL_TRANSLATION_UNIT; |
| 91 | } |
| 92 | |
| 93 | void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) { |
| 94 | VisitDecl(D); |
| 95 | Writer.AddDeclarationName(D->getDeclName(), Record); |
| 96 | } |
| 97 | |
| 98 | void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) { |
| 99 | VisitNamedDecl(D); |
| 100 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 101 | } |
| 102 | |
| 103 | void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { |
| 104 | VisitTypeDecl(D); |
| 105 | Writer.AddTypeRef(D->getUnderlyingType(), Record); |
| 106 | Code = pch::DECL_TYPEDEF; |
| 107 | } |
| 108 | |
| 109 | void PCHDeclWriter::VisitTagDecl(TagDecl *D) { |
| 110 | VisitTypeDecl(D); |
| 111 | Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding |
| 112 | Record.push_back(D->isDefinition()); |
| 113 | Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record); |
| 114 | } |
| 115 | |
| 116 | void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) { |
| 117 | VisitTagDecl(D); |
| 118 | Writer.AddTypeRef(D->getIntegerType(), Record); |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 119 | // FIXME: C++ InstantiatedFrom |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 120 | Code = pch::DECL_ENUM; |
| 121 | } |
| 122 | |
| 123 | void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) { |
| 124 | VisitTagDecl(D); |
| 125 | Record.push_back(D->hasFlexibleArrayMember()); |
| 126 | Record.push_back(D->isAnonymousStructOrUnion()); |
| 127 | Code = pch::DECL_RECORD; |
| 128 | } |
| 129 | |
| 130 | void PCHDeclWriter::VisitValueDecl(ValueDecl *D) { |
| 131 | VisitNamedDecl(D); |
| 132 | Writer.AddTypeRef(D->getType(), Record); |
| 133 | } |
| 134 | |
| 135 | void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 136 | VisitValueDecl(D); |
| 137 | Record.push_back(D->getInitExpr()? 1 : 0); |
| 138 | if (D->getInitExpr()) |
| 139 | Writer.AddStmt(D->getInitExpr()); |
| 140 | Writer.AddAPSInt(D->getInitVal(), Record); |
| 141 | Code = pch::DECL_ENUM_CONSTANT; |
| 142 | } |
| 143 | |
| 144 | void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) { |
| 145 | VisitValueDecl(D); |
| 146 | Record.push_back(D->isThisDeclarationADefinition()); |
| 147 | if (D->isThisDeclarationADefinition()) |
| 148 | Writer.AddStmt(D->getBody(Context)); |
| 149 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 150 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
| 151 | Record.push_back(D->isInline()); |
| 152 | Record.push_back(D->isC99InlineDefinition()); |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 153 | Record.push_back(D->isVirtualAsWritten()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 154 | Record.push_back(D->isPure()); |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 155 | Record.push_back(D->hasInheritedPrototype()); |
| 156 | Record.push_back(D->hasWrittenPrototype()); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 157 | Record.push_back(D->isDeleted()); |
| 158 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 159 | // FIXME: C++ TemplateOrInstantiation |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 160 | Record.push_back(D->param_size()); |
| 161 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 162 | P != PEnd; ++P) |
| 163 | Writer.AddDeclRef(*P, Record); |
| 164 | Code = pch::DECL_FUNCTION; |
| 165 | } |
| 166 | |
| 167 | void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 168 | VisitNamedDecl(D); |
| 169 | // FIXME: convert to LazyStmtPtr? |
| 170 | // Unlike C/C++, method bodies will never be in header files. |
| 171 | Record.push_back(D->getBody() != 0); |
| 172 | if (D->getBody() != 0) { |
| 173 | Writer.AddStmt(D->getBody(Context)); |
| 174 | Writer.AddDeclRef(D->getSelfDecl(), Record); |
| 175 | Writer.AddDeclRef(D->getCmdDecl(), Record); |
| 176 | } |
| 177 | Record.push_back(D->isInstanceMethod()); |
| 178 | Record.push_back(D->isVariadic()); |
| 179 | Record.push_back(D->isSynthesized()); |
| 180 | // FIXME: stable encoding for @required/@optional |
| 181 | Record.push_back(D->getImplementationControl()); |
| 182 | // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway |
| 183 | Record.push_back(D->getObjCDeclQualifier()); |
| 184 | Writer.AddTypeRef(D->getResultType(), Record); |
| 185 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 186 | Record.push_back(D->param_size()); |
| 187 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 188 | PEnd = D->param_end(); P != PEnd; ++P) |
| 189 | Writer.AddDeclRef(*P, Record); |
| 190 | Code = pch::DECL_OBJC_METHOD; |
| 191 | } |
| 192 | |
| 193 | void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 194 | VisitNamedDecl(D); |
| 195 | Writer.AddSourceLocation(D->getAtEndLoc(), Record); |
| 196 | // Abstract class (no need to define a stable pch::DECL code). |
| 197 | } |
| 198 | |
| 199 | void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 200 | VisitObjCContainerDecl(D); |
| 201 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 202 | Writer.AddDeclRef(D->getSuperClass(), Record); |
| 203 | Record.push_back(D->protocol_size()); |
| 204 | for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(), |
| 205 | PEnd = D->protocol_end(); |
| 206 | P != PEnd; ++P) |
| 207 | Writer.AddDeclRef(*P, Record); |
| 208 | Record.push_back(D->ivar_size()); |
| 209 | for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(), |
| 210 | IEnd = D->ivar_end(); I != IEnd; ++I) |
| 211 | Writer.AddDeclRef(*I, Record); |
| 212 | Writer.AddDeclRef(D->getCategoryList(), Record); |
| 213 | Record.push_back(D->isForwardDecl()); |
| 214 | Record.push_back(D->isImplicitInterfaceDecl()); |
| 215 | Writer.AddSourceLocation(D->getClassLoc(), Record); |
| 216 | Writer.AddSourceLocation(D->getSuperClassLoc(), Record); |
| 217 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 218 | Code = pch::DECL_OBJC_INTERFACE; |
| 219 | } |
| 220 | |
| 221 | void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 222 | VisitFieldDecl(D); |
| 223 | // FIXME: stable encoding for @public/@private/@protected/@package |
| 224 | Record.push_back(D->getAccessControl()); |
| 225 | Code = pch::DECL_OBJC_IVAR; |
| 226 | } |
| 227 | |
| 228 | void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
| 229 | VisitObjCContainerDecl(D); |
| 230 | Record.push_back(D->isForwardDecl()); |
| 231 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 232 | Record.push_back(D->protocol_size()); |
| 233 | for (ObjCProtocolDecl::protocol_iterator |
| 234 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 235 | Writer.AddDeclRef(*I, Record); |
| 236 | Code = pch::DECL_OBJC_PROTOCOL; |
| 237 | } |
| 238 | |
| 239 | void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 240 | VisitFieldDecl(D); |
| 241 | Code = pch::DECL_OBJC_AT_DEFS_FIELD; |
| 242 | } |
| 243 | |
| 244 | void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 245 | VisitDecl(D); |
| 246 | Record.push_back(D->size()); |
| 247 | for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) |
| 248 | Writer.AddDeclRef(*I, Record); |
| 249 | Code = pch::DECL_OBJC_CLASS; |
| 250 | } |
| 251 | |
| 252 | void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 253 | VisitDecl(D); |
| 254 | Record.push_back(D->protocol_size()); |
| 255 | for (ObjCProtocolDecl::protocol_iterator |
| 256 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 257 | Writer.AddDeclRef(*I, Record); |
| 258 | Code = pch::DECL_OBJC_FORWARD_PROTOCOL; |
| 259 | } |
| 260 | |
| 261 | void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 262 | VisitObjCContainerDecl(D); |
| 263 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 264 | Record.push_back(D->protocol_size()); |
| 265 | for (ObjCProtocolDecl::protocol_iterator |
| 266 | I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) |
| 267 | Writer.AddDeclRef(*I, Record); |
| 268 | Writer.AddDeclRef(D->getNextClassCategory(), Record); |
| 269 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 270 | Code = pch::DECL_OBJC_CATEGORY; |
| 271 | } |
| 272 | |
| 273 | void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) { |
| 274 | VisitNamedDecl(D); |
| 275 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 276 | Code = pch::DECL_OBJC_COMPATIBLE_ALIAS; |
| 277 | } |
| 278 | |
| 279 | void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 280 | VisitNamedDecl(D); |
| 281 | Writer.AddTypeRef(D->getType(), Record); |
| 282 | // FIXME: stable encoding |
| 283 | Record.push_back((unsigned)D->getPropertyAttributes()); |
| 284 | // FIXME: stable encoding |
| 285 | Record.push_back((unsigned)D->getPropertyImplementation()); |
| 286 | Writer.AddDeclarationName(D->getGetterName(), Record); |
| 287 | Writer.AddDeclarationName(D->getSetterName(), Record); |
| 288 | Writer.AddDeclRef(D->getGetterMethodDecl(), Record); |
| 289 | Writer.AddDeclRef(D->getSetterMethodDecl(), Record); |
| 290 | Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); |
| 291 | Code = pch::DECL_OBJC_PROPERTY; |
| 292 | } |
| 293 | |
| 294 | void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 295 | VisitNamedDecl(D); |
| 296 | Writer.AddDeclRef(D->getClassInterface(), Record); |
| 297 | Writer.AddSourceLocation(D->getLocEnd(), Record); |
| 298 | // Abstract class (no need to define a stable pch::DECL code). |
| 299 | } |
| 300 | |
| 301 | void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 302 | VisitObjCImplDecl(D); |
| 303 | Writer.AddIdentifierRef(D->getIdentifier(), Record); |
| 304 | Code = pch::DECL_OBJC_CATEGORY_IMPL; |
| 305 | } |
| 306 | |
| 307 | void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 308 | VisitObjCImplDecl(D); |
| 309 | Writer.AddDeclRef(D->getSuperClass(), Record); |
| 310 | Code = pch::DECL_OBJC_IMPLEMENTATION; |
| 311 | } |
| 312 | |
| 313 | void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 314 | VisitDecl(D); |
| 315 | Writer.AddSourceLocation(D->getLocStart(), Record); |
| 316 | Writer.AddDeclRef(D->getPropertyDecl(), Record); |
| 317 | Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); |
| 318 | Code = pch::DECL_OBJC_PROPERTY_IMPL; |
| 319 | } |
| 320 | |
| 321 | void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) { |
| 322 | VisitValueDecl(D); |
| 323 | Record.push_back(D->isMutable()); |
| 324 | Record.push_back(D->getBitWidth()? 1 : 0); |
| 325 | if (D->getBitWidth()) |
| 326 | Writer.AddStmt(D->getBitWidth()); |
| 327 | Code = pch::DECL_FIELD; |
| 328 | } |
| 329 | |
| 330 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
| 331 | VisitValueDecl(D); |
| 332 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
| 333 | Record.push_back(D->isThreadSpecified()); |
| 334 | Record.push_back(D->hasCXXDirectInitializer()); |
| 335 | Record.push_back(D->isDeclaredInCondition()); |
| 336 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 337 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
| 338 | Record.push_back(D->getInit()? 1 : 0); |
| 339 | if (D->getInit()) |
| 340 | Writer.AddStmt(D->getInit()); |
| 341 | Code = pch::DECL_VAR; |
| 342 | } |
| 343 | |
| 344 | void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 345 | VisitVarDecl(D); |
| 346 | Code = pch::DECL_IMPLICIT_PARAM; |
| 347 | } |
| 348 | |
| 349 | void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { |
| 350 | VisitVarDecl(D); |
| 351 | Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding |
| 352 | // FIXME: emit default argument (C++) |
| 353 | // FIXME: why isn't the "default argument" just stored as the initializer |
| 354 | // in VarDecl? |
| 355 | Code = pch::DECL_PARM_VAR; |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 356 | |
| 357 | |
| 358 | // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here |
| 359 | // we dynamically check for the properties that we optimize for, but don't |
| 360 | // know are true of all PARM_VAR_DECLs. |
| 361 | if (!D->hasAttrs() && |
| 362 | !D->isImplicit() && |
| 363 | D->getAccess() == AS_none && |
| 364 | D->getStorageClass() == 0 && |
| 365 | !D->hasCXXDirectInitializer() && // Can params have this ever? |
| 366 | D->getObjCDeclQualifier() == 0) |
| 367 | AbbrevToUse = Writer.getParmVarDeclAbbrev(); |
| 368 | |
| 369 | // Check things we know are true of *every* PARM_VAR_DECL, which is more than |
| 370 | // just us assuming it. |
| 371 | assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls"); |
| 372 | assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread"); |
| 373 | assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private"); |
| 374 | assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition"); |
| 375 | assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl"); |
| 376 | assert(D->getInit() == 0 && "PARM_VAR_DECL never has init"); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 380 | VisitParmVarDecl(D); |
| 381 | Writer.AddTypeRef(D->getOriginalType(), Record); |
| 382 | Code = pch::DECL_ORIGINAL_PARM_VAR; |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 383 | AbbrevToUse = 0; |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 387 | VisitDecl(D); |
| 388 | Writer.AddStmt(D->getAsmString()); |
| 389 | Code = pch::DECL_FILE_SCOPE_ASM; |
| 390 | } |
| 391 | |
| 392 | void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) { |
| 393 | VisitDecl(D); |
| 394 | Writer.AddStmt(D->getBody()); |
| 395 | Record.push_back(D->param_size()); |
| 396 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 397 | P != PEnd; ++P) |
| 398 | Writer.AddDeclRef(*P, Record); |
| 399 | Code = pch::DECL_BLOCK; |
| 400 | } |
| 401 | |
| 402 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 403 | /// |
| 404 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 405 | /// block for this declaration context is stored. May be 0 to indicate |
| 406 | /// that there are no declarations stored within this context. |
| 407 | /// |
| 408 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 409 | /// block for this declaration context is stored. May be 0 to indicate |
| 410 | /// that there are no declarations visible from this context. Note |
| 411 | /// that this value will not be emitted for non-primary declaration |
| 412 | /// contexts. |
| 413 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 414 | uint64_t VisibleOffset) { |
| 415 | Record.push_back(LexicalOffset); |
| 416 | Record.push_back(VisibleOffset); |
| 417 | } |
| 418 | |
| 419 | |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | // PCHWriter Implementation |
| 422 | //===----------------------------------------------------------------------===// |
| 423 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 424 | void PCHWriter::WriteDeclsBlockAbbrevs() { |
| 425 | using namespace llvm; |
| 426 | // Abbreviation for DECL_PARM_VAR. |
| 427 | BitCodeAbbrev *Abv = new BitCodeAbbrev(); |
| 428 | Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR)); |
| 429 | |
| 430 | // Decl |
| 431 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext |
| 432 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext |
| 433 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location |
| 434 | Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) |
| 435 | Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs |
| 436 | Abv->Add(BitCodeAbbrevOp(0)); // isImplicit |
| 437 | Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier |
| 438 | |
| 439 | // NamedDecl |
| 440 | Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier |
| 441 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name |
| 442 | // ValueDecl |
| 443 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type |
| 444 | // VarDecl |
| 445 | Abv->Add(BitCodeAbbrevOp(0)); // StorageClass |
| 446 | Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified |
| 447 | Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer |
| 448 | Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition |
| 449 | Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl |
| 450 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeSpecStartLoc |
| 451 | Abv->Add(BitCodeAbbrevOp(0)); // HasInit |
| 452 | // ParmVarDecl |
| 453 | Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier |
| 454 | |
| 455 | ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv); |
| 456 | } |
| 457 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 458 | /// \brief Write a block containing all of the declarations. |
| 459 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
| 460 | // Enter the declarations block. |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 461 | Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 3); |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 462 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 463 | // Output the abbreviations that we will use in this block. |
| 464 | WriteDeclsBlockAbbrevs(); |
| 465 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 466 | // Emit all of the declarations. |
| 467 | RecordData Record; |
| 468 | PCHDeclWriter W(*this, Context, Record); |
| 469 | while (!DeclsToEmit.empty()) { |
| 470 | // Pull the next declaration off the queue |
| 471 | Decl *D = DeclsToEmit.front(); |
| 472 | DeclsToEmit.pop(); |
| 473 | |
| 474 | // If this declaration is also a DeclContext, write blocks for the |
| 475 | // declarations that lexically stored inside its context and those |
| 476 | // declarations that are visible from its context. These blocks |
| 477 | // are written before the declaration itself so that we can put |
| 478 | // their offsets into the record for the declaration. |
| 479 | uint64_t LexicalOffset = 0; |
| 480 | uint64_t VisibleOffset = 0; |
| 481 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 482 | if (DC) { |
| 483 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 484 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 485 | } |
| 486 | |
| 487 | // Determine the ID for this declaration |
| 488 | pch::DeclID ID = DeclIDs[D]; |
| 489 | if (ID == 0) |
| 490 | ID = DeclIDs.size(); |
| 491 | |
| 492 | unsigned Index = ID - 1; |
| 493 | |
| 494 | // Record the offset for this declaration |
| 495 | if (DeclOffsets.size() == Index) |
| 496 | DeclOffsets.push_back(Stream.GetCurrentBitNo()); |
| 497 | else if (DeclOffsets.size() < Index) { |
| 498 | DeclOffsets.resize(Index+1); |
| 499 | DeclOffsets[Index] = Stream.GetCurrentBitNo(); |
| 500 | } |
| 501 | |
| 502 | // Build and emit a record for this declaration |
| 503 | Record.clear(); |
| 504 | W.Code = (pch::DeclCode)0; |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 505 | W.AbbrevToUse = 0; |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 506 | W.Visit(D); |
| 507 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
| 508 | |
| 509 | if (!W.Code) { |
| 510 | fprintf(stderr, "Cannot serialize declaration of kind %s\n", |
| 511 | D->getDeclKindName()); |
| 512 | assert(false && "Unhandled declaration kind while generating PCH"); |
| 513 | exit(-1); |
| 514 | } |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 515 | Stream.EmitRecord(W.Code, Record, W.AbbrevToUse); |
| 516 | |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 517 | // If the declaration had any attributes, write them now. |
| 518 | if (D->hasAttrs()) |
| 519 | WriteAttributeRecord(D->getAttrs()); |
| 520 | |
| 521 | // Flush any expressions that were written as part of this declaration. |
| 522 | FlushStmts(); |
| 523 | |
| 524 | // Note external declarations so that we can add them to a record |
| 525 | // in the PCH file later. |
| 526 | if (isa<FileScopeAsmDecl>(D)) |
| 527 | ExternalDefinitions.push_back(ID); |
| 528 | } |
| 529 | |
| 530 | // Exit the declarations block |
| 531 | Stream.ExitBlock(); |
| 532 | } |