Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1 | //===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===// |
| 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 defines the PCHWriter class, which writes a precompiled header. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Frontend/PCHWriter.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclContextInternals.h" |
| 18 | #include "clang/AST/DeclVisitor.h" |
| 19 | #include "clang/AST/Type.h" |
| 20 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 21 | #include "llvm/Support/Compiler.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Type serialization |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | namespace { |
| 29 | class VISIBILITY_HIDDEN PCHTypeWriter { |
| 30 | PCHWriter &Writer; |
| 31 | PCHWriter::RecordData &Record; |
| 32 | |
| 33 | public: |
| 34 | /// \brief Type code that corresponds to the record generated. |
| 35 | pch::TypeCode Code; |
| 36 | |
| 37 | PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 38 | : Writer(Writer), Record(Record) { } |
| 39 | |
| 40 | void VisitArrayType(const ArrayType *T); |
| 41 | void VisitFunctionType(const FunctionType *T); |
| 42 | void VisitTagType(const TagType *T); |
| 43 | |
| 44 | #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); |
| 45 | #define ABSTRACT_TYPE(Class, Base) |
| 46 | #define DEPENDENT_TYPE(Class, Base) |
| 47 | #include "clang/AST/TypeNodes.def" |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) { |
| 52 | Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record); |
| 53 | Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values |
| 54 | Record.push_back(T->getAddressSpace()); |
| 55 | Code = pch::TYPE_EXT_QUAL; |
| 56 | } |
| 57 | |
| 58 | void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) { |
| 59 | assert(false && "Built-in types are never serialized"); |
| 60 | } |
| 61 | |
| 62 | void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) { |
| 63 | Record.push_back(T->getWidth()); |
| 64 | Record.push_back(T->isSigned()); |
| 65 | Code = pch::TYPE_FIXED_WIDTH_INT; |
| 66 | } |
| 67 | |
| 68 | void PCHTypeWriter::VisitComplexType(const ComplexType *T) { |
| 69 | Writer.AddTypeRef(T->getElementType(), Record); |
| 70 | Code = pch::TYPE_COMPLEX; |
| 71 | } |
| 72 | |
| 73 | void PCHTypeWriter::VisitPointerType(const PointerType *T) { |
| 74 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 75 | Code = pch::TYPE_POINTER; |
| 76 | } |
| 77 | |
| 78 | void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { |
| 79 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 80 | Code = pch::TYPE_BLOCK_POINTER; |
| 81 | } |
| 82 | |
| 83 | void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { |
| 84 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 85 | Code = pch::TYPE_LVALUE_REFERENCE; |
| 86 | } |
| 87 | |
| 88 | void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { |
| 89 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 90 | Code = pch::TYPE_RVALUE_REFERENCE; |
| 91 | } |
| 92 | |
| 93 | void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { |
| 94 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 95 | Writer.AddTypeRef(QualType(T->getClass(), 0), Record); |
| 96 | Code = pch::TYPE_MEMBER_POINTER; |
| 97 | } |
| 98 | |
| 99 | void PCHTypeWriter::VisitArrayType(const ArrayType *T) { |
| 100 | Writer.AddTypeRef(T->getElementType(), Record); |
| 101 | Record.push_back(T->getSizeModifier()); // FIXME: stable values |
| 102 | Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values |
| 103 | } |
| 104 | |
| 105 | void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { |
| 106 | VisitArrayType(T); |
| 107 | Writer.AddAPInt(T->getSize(), Record); |
| 108 | Code = pch::TYPE_CONSTANT_ARRAY; |
| 109 | } |
| 110 | |
| 111 | void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 112 | VisitArrayType(T); |
| 113 | Code = pch::TYPE_INCOMPLETE_ARRAY; |
| 114 | } |
| 115 | |
| 116 | void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { |
| 117 | VisitArrayType(T); |
| 118 | // FIXME: Serialize array size expression. |
| 119 | assert(false && "Cannot serialize variable-length arrays"); |
| 120 | Code = pch::TYPE_VARIABLE_ARRAY; |
| 121 | } |
| 122 | |
| 123 | void PCHTypeWriter::VisitVectorType(const VectorType *T) { |
| 124 | Writer.AddTypeRef(T->getElementType(), Record); |
| 125 | Record.push_back(T->getNumElements()); |
| 126 | Code = pch::TYPE_VECTOR; |
| 127 | } |
| 128 | |
| 129 | void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) { |
| 130 | VisitVectorType(T); |
| 131 | Code = pch::TYPE_EXT_VECTOR; |
| 132 | } |
| 133 | |
| 134 | void PCHTypeWriter::VisitFunctionType(const FunctionType *T) { |
| 135 | Writer.AddTypeRef(T->getResultType(), Record); |
| 136 | } |
| 137 | |
| 138 | void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 139 | VisitFunctionType(T); |
| 140 | Code = pch::TYPE_FUNCTION_NO_PROTO; |
| 141 | } |
| 142 | |
| 143 | void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| 144 | VisitFunctionType(T); |
| 145 | Record.push_back(T->getNumArgs()); |
| 146 | for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) |
| 147 | Writer.AddTypeRef(T->getArgType(I), Record); |
| 148 | Record.push_back(T->isVariadic()); |
| 149 | Record.push_back(T->getTypeQuals()); |
| 150 | Code = pch::TYPE_FUNCTION_PROTO; |
| 151 | } |
| 152 | |
| 153 | void PCHTypeWriter::VisitTypedefType(const TypedefType *T) { |
| 154 | Writer.AddDeclRef(T->getDecl(), Record); |
| 155 | Code = pch::TYPE_TYPEDEF; |
| 156 | } |
| 157 | |
| 158 | void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { |
| 159 | // FIXME: serialize the typeof expression |
| 160 | assert(false && "Cannot serialize typeof(expr)"); |
| 161 | Code = pch::TYPE_TYPEOF_EXPR; |
| 162 | } |
| 163 | |
| 164 | void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) { |
| 165 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
| 166 | Code = pch::TYPE_TYPEOF; |
| 167 | } |
| 168 | |
| 169 | void PCHTypeWriter::VisitTagType(const TagType *T) { |
| 170 | Writer.AddDeclRef(T->getDecl(), Record); |
| 171 | assert(!T->isBeingDefined() && |
| 172 | "Cannot serialize in the middle of a type definition"); |
| 173 | } |
| 174 | |
| 175 | void PCHTypeWriter::VisitRecordType(const RecordType *T) { |
| 176 | VisitTagType(T); |
| 177 | Code = pch::TYPE_RECORD; |
| 178 | } |
| 179 | |
| 180 | void PCHTypeWriter::VisitEnumType(const EnumType *T) { |
| 181 | VisitTagType(T); |
| 182 | Code = pch::TYPE_ENUM; |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | PCHTypeWriter::VisitTemplateSpecializationType( |
| 187 | const TemplateSpecializationType *T) { |
| 188 | // FIXME: Serialize this type |
| 189 | assert(false && "Cannot serialize template specialization types"); |
| 190 | } |
| 191 | |
| 192 | void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) { |
| 193 | // FIXME: Serialize this type |
| 194 | assert(false && "Cannot serialize qualified name types"); |
| 195 | } |
| 196 | |
| 197 | void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
| 198 | Writer.AddDeclRef(T->getDecl(), Record); |
| 199 | Code = pch::TYPE_OBJC_INTERFACE; |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | PCHTypeWriter::VisitObjCQualifiedInterfaceType( |
| 204 | const ObjCQualifiedInterfaceType *T) { |
| 205 | VisitObjCInterfaceType(T); |
| 206 | Record.push_back(T->getNumProtocols()); |
| 207 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 208 | Writer.AddDeclRef(T->getProtocol(I), Record); |
| 209 | Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE; |
| 210 | } |
| 211 | |
| 212 | void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) { |
| 213 | Record.push_back(T->getNumProtocols()); |
| 214 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 215 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 216 | Code = pch::TYPE_OBJC_QUALIFIED_ID; |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) { |
| 221 | Record.push_back(T->getNumProtocols()); |
| 222 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 223 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 224 | Code = pch::TYPE_OBJC_QUALIFIED_CLASS; |
| 225 | } |
| 226 | |
| 227 | //===----------------------------------------------------------------------===// |
| 228 | // Declaration serialization |
| 229 | //===----------------------------------------------------------------------===// |
| 230 | namespace { |
| 231 | class VISIBILITY_HIDDEN PCHDeclWriter |
| 232 | : public DeclVisitor<PCHDeclWriter, void> { |
| 233 | |
| 234 | PCHWriter &Writer; |
| 235 | PCHWriter::RecordData &Record; |
| 236 | |
| 237 | public: |
| 238 | pch::DeclCode Code; |
| 239 | |
| 240 | PCHDeclWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 241 | : Writer(Writer), Record(Record) { } |
| 242 | |
| 243 | void VisitDecl(Decl *D); |
| 244 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 245 | void VisitNamedDecl(NamedDecl *D); |
| 246 | void VisitTypeDecl(TypeDecl *D); |
| 247 | void VisitTypedefDecl(TypedefDecl *D); |
| 248 | void VisitValueDecl(ValueDecl *D); |
| 249 | void VisitVarDecl(VarDecl *D); |
| 250 | |
| 251 | void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 252 | uint64_t VisibleOffset); |
| 253 | }; |
| 254 | } |
| 255 | |
| 256 | void PCHDeclWriter::VisitDecl(Decl *D) { |
| 257 | Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); |
| 258 | Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); |
| 259 | Writer.AddSourceLocation(D->getLocation(), Record); |
| 260 | Record.push_back(D->isInvalidDecl()); |
| 261 | // FIXME: hasAttrs |
| 262 | Record.push_back(D->isImplicit()); |
| 263 | Record.push_back(D->getAccess()); |
| 264 | } |
| 265 | |
| 266 | void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 267 | VisitDecl(D); |
| 268 | Code = pch::DECL_TRANSLATION_UNIT; |
| 269 | } |
| 270 | |
| 271 | void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) { |
| 272 | VisitDecl(D); |
| 273 | Writer.AddDeclarationName(D->getDeclName(), Record); |
| 274 | } |
| 275 | |
| 276 | void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) { |
| 277 | VisitNamedDecl(D); |
| 278 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 279 | } |
| 280 | |
| 281 | void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { |
| 282 | VisitTypeDecl(D); |
| 283 | Writer.AddTypeRef(D->getUnderlyingType(), Record); |
| 284 | Code = pch::DECL_TYPEDEF; |
| 285 | } |
| 286 | |
| 287 | void PCHDeclWriter::VisitValueDecl(ValueDecl *D) { |
| 288 | VisitNamedDecl(D); |
| 289 | Writer.AddTypeRef(D->getType(), Record); |
| 290 | } |
| 291 | |
| 292 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
| 293 | VisitValueDecl(D); |
| 294 | Record.push_back(D->getStorageClass()); |
| 295 | Record.push_back(D->isThreadSpecified()); |
| 296 | Record.push_back(D->hasCXXDirectInitializer()); |
| 297 | Record.push_back(D->isDeclaredInCondition()); |
| 298 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 299 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
| 300 | // FIXME: emit initializer |
| 301 | Code = pch::DECL_VAR; |
| 302 | } |
| 303 | |
| 304 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 305 | /// |
| 306 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 307 | /// block for this declaration context is stored. May be 0 to indicate |
| 308 | /// that there are no declarations stored within this context. |
| 309 | /// |
| 310 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 311 | /// block for this declaration context is stored. May be 0 to indicate |
| 312 | /// that there are no declarations visible from this context. Note |
| 313 | /// that this value will not be emitted for non-primary declaration |
| 314 | /// contexts. |
| 315 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 316 | uint64_t VisibleOffset) { |
| 317 | Record.push_back(LexicalOffset); |
| 318 | if (DC->getPrimaryContext() == DC) |
| 319 | Record.push_back(VisibleOffset); |
| 320 | } |
| 321 | |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | // PCHWriter Implementation |
| 324 | //===----------------------------------------------------------------------===// |
| 325 | |
| 326 | /// \brief Write the representation of a type to the PCH stream. |
| 327 | void PCHWriter::WriteType(const Type *T) { |
| 328 | pch::ID &ID = TypeIDs[T]; |
| 329 | if (ID == 0) // we haven't seen this type before |
| 330 | ID = NextTypeID++; |
| 331 | |
| 332 | // Record the offset for this type. |
| 333 | if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS) |
| 334 | TypeOffsets.push_back(S.GetCurrentBitNo()); |
| 335 | else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) { |
| 336 | TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS); |
| 337 | TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo(); |
| 338 | } |
| 339 | |
| 340 | RecordData Record; |
| 341 | |
| 342 | // Emit the type's representation. |
| 343 | PCHTypeWriter W(*this, Record); |
| 344 | switch (T->getTypeClass()) { |
| 345 | // For all of the concrete, non-dependent types, call the |
| 346 | // appropriate visitor function. |
| 347 | #define TYPE(Class, Base) \ |
| 348 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
| 349 | #define ABSTRACT_TYPE(Class, Base) |
| 350 | #define DEPENDENT_TYPE(Class, Base) |
| 351 | #include "clang/AST/TypeNodes.def" |
| 352 | |
| 353 | // For all of the dependent type nodes (which only occur in C++ |
| 354 | // templates), produce an error. |
| 355 | #define TYPE(Class, Base) |
| 356 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 357 | #include "clang/AST/TypeNodes.def" |
| 358 | assert(false && "Cannot serialize dependent type nodes"); |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | // Emit the serialized record. |
| 363 | S.EmitRecord(W.Code, Record); |
| 364 | } |
| 365 | |
| 366 | /// \brief Write a block containing all of the types. |
| 367 | void PCHWriter::WriteTypesBlock(ASTContext &Context) { |
| 368 | // Enter the types block |
| 369 | S.EnterSubblock(pch::TYPES_BLOCK_ID, 2); |
| 370 | |
| 371 | // Emit all of the types in the ASTContext |
| 372 | for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(), |
| 373 | TEnd = Context.getTypes().end(); |
| 374 | T != TEnd; ++T) { |
| 375 | // Builtin types are never serialized. |
| 376 | if (isa<BuiltinType>(*T)) |
| 377 | continue; |
| 378 | |
| 379 | WriteType(*T); |
| 380 | } |
| 381 | |
| 382 | // Exit the types block |
| 383 | S.ExitBlock(); |
| 384 | |
| 385 | // Write the type offsets block |
| 386 | S.EnterSubblock(pch::TYPE_OFFSETS_BLOCK_ID, 2); |
| 387 | S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets); |
| 388 | S.ExitBlock(); |
| 389 | } |
| 390 | |
| 391 | /// \brief Write the block containing all of the declaration IDs |
| 392 | /// lexically declared within the given DeclContext. |
| 393 | /// |
| 394 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 395 | /// bistream, or 0 if no block was written. |
| 396 | uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
| 397 | DeclContext *DC) { |
| 398 | if (DC->decls_begin(Context) == DC->decls_end(Context)) |
| 399 | return 0; |
| 400 | |
| 401 | uint64_t Offset = S.GetCurrentBitNo(); |
| 402 | RecordData Record; |
| 403 | for (DeclContext::decl_iterator D = DC->decls_begin(Context), |
| 404 | DEnd = DC->decls_end(Context); |
| 405 | D != DEnd; ++D) |
| 406 | AddDeclRef(*D, Record); |
| 407 | |
| 408 | S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record); |
| 409 | return Offset; |
| 410 | } |
| 411 | |
| 412 | /// \brief Write the block containing all of the declaration IDs |
| 413 | /// visible from the given DeclContext. |
| 414 | /// |
| 415 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
| 416 | /// bistream, or 0 if no block was written. |
| 417 | uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 418 | DeclContext *DC) { |
| 419 | if (DC->getPrimaryContext() != DC) |
| 420 | return 0; |
| 421 | |
| 422 | // Force the DeclContext to build a its name-lookup table. |
| 423 | DC->lookup(Context, DeclarationName()); |
| 424 | |
| 425 | // Serialize the contents of the mapping used for lookup. Note that, |
| 426 | // although we have two very different code paths, the serialized |
| 427 | // representation is the same for both cases: a declaration name, |
| 428 | // followed by a size, followed by references to the visible |
| 429 | // declarations that have that name. |
| 430 | uint64_t Offset = S.GetCurrentBitNo(); |
| 431 | RecordData Record; |
| 432 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
| 433 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 434 | D != DEnd; ++D) { |
| 435 | AddDeclarationName(D->first, Record); |
| 436 | DeclContext::lookup_result Result = D->second.getLookupResult(Context); |
| 437 | Record.push_back(Result.second - Result.first); |
| 438 | for(; Result.first != Result.second; ++Result.first) |
| 439 | AddDeclRef(*Result.first, Record); |
| 440 | } |
| 441 | |
| 442 | if (Record.size() == 0) |
| 443 | return 0; |
| 444 | |
| 445 | S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record); |
| 446 | return Offset; |
| 447 | } |
| 448 | |
| 449 | /// \brief Write a block containing all of the declarations. |
| 450 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
| 451 | // Enter the declarations block |
| 452 | S.EnterSubblock(pch::DECLS_BLOCK_ID, 2); |
| 453 | |
| 454 | // Emit all of the declarations. |
| 455 | RecordData Record; |
| 456 | PCHDeclWriter W(*this, Record); |
| 457 | while (!DeclsToEmit.empty()) { |
| 458 | // Pull the next declaration off the queue |
| 459 | Decl *D = DeclsToEmit.front(); |
| 460 | DeclsToEmit.pop(); |
| 461 | |
| 462 | // If this declaration is also a DeclContext, write blocks for the |
| 463 | // declarations that lexically stored inside its context and those |
| 464 | // declarations that are visible from its context. These blocks |
| 465 | // are written before the declaration itself so that we can put |
| 466 | // their offsets into the record for the declaration. |
| 467 | uint64_t LexicalOffset = 0; |
| 468 | uint64_t VisibleOffset = 0; |
| 469 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 470 | if (DC) { |
| 471 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 472 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 473 | } |
| 474 | |
| 475 | // Determine the ID for this declaration |
| 476 | pch::ID ID = DeclIDs[D]; |
| 477 | if (ID == 0) |
| 478 | ID = DeclIDs.size(); |
| 479 | |
| 480 | unsigned Index = ID - 1; |
| 481 | |
| 482 | // Record the offset for this declaration |
| 483 | if (DeclOffsets.size() == Index) |
| 484 | DeclOffsets.push_back(S.GetCurrentBitNo()); |
| 485 | else if (DeclOffsets.size() < Index) { |
| 486 | DeclOffsets.resize(Index+1); |
| 487 | DeclOffsets[Index] = S.GetCurrentBitNo(); |
| 488 | } |
| 489 | |
| 490 | // Build and emit a record for this declaration |
| 491 | Record.clear(); |
| 492 | W.Code = (pch::DeclCode)0; |
| 493 | W.Visit(D); |
| 494 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
| 495 | assert(W.Code && "Visitor did not set record code"); |
| 496 | S.EmitRecord(W.Code, Record); |
| 497 | } |
| 498 | |
| 499 | // Exit the declarations block |
| 500 | S.ExitBlock(); |
| 501 | |
| 502 | // Write the declaration offsets block |
| 503 | S.EnterSubblock(pch::DECL_OFFSETS_BLOCK_ID, 2); |
| 504 | S.EmitRecord(pch::DECL_OFFSET, DeclOffsets); |
| 505 | S.ExitBlock(); |
| 506 | } |
| 507 | |
| 508 | PCHWriter::PCHWriter(llvm::BitstreamWriter &S) |
| 509 | : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { } |
| 510 | |
| 511 | void PCHWriter::WritePCH(ASTContext &Context) { |
| 512 | // Emit the file header. |
| 513 | S.Emit((unsigned)'C', 8); |
| 514 | S.Emit((unsigned)'P', 8); |
| 515 | S.Emit((unsigned)'C', 8); |
| 516 | S.Emit((unsigned)'H', 8); |
| 517 | |
| 518 | // The translation unit is the first declaration we'll emit. |
| 519 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
| 520 | DeclsToEmit.push(Context.getTranslationUnitDecl()); |
| 521 | |
| 522 | // Write the remaining PCH contents. |
| 523 | S.EnterSubblock(pch::PCH_BLOCK_ID, 2); |
| 524 | WriteTypesBlock(Context); |
| 525 | WriteDeclsBlock(Context); |
| 526 | S.ExitBlock(); |
| 527 | } |
| 528 | |
| 529 | void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { |
| 530 | Record.push_back(Loc.getRawEncoding()); |
| 531 | } |
| 532 | |
| 533 | void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { |
| 534 | Record.push_back(Value.getBitWidth()); |
| 535 | unsigned N = Value.getNumWords(); |
| 536 | const uint64_t* Words = Value.getRawData(); |
| 537 | for (unsigned I = 0; I != N; ++I) |
| 538 | Record.push_back(Words[I]); |
| 539 | } |
| 540 | |
| 541 | void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { |
| 542 | // FIXME: Emit an identifier ID, not the actual string! |
| 543 | const char *Name = II->getName(); |
| 544 | unsigned Len = strlen(Name); |
| 545 | Record.push_back(Len); |
| 546 | Record.insert(Record.end(), Name, Name + Len); |
| 547 | } |
| 548 | |
| 549 | void PCHWriter::AddTypeRef(QualType T, RecordData &Record) { |
| 550 | if (T.isNull()) { |
| 551 | Record.push_back(pch::PREDEF_TYPE_NULL_ID); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) { |
| 556 | pch::ID ID; |
| 557 | switch (BT->getKind()) { |
| 558 | case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break; |
| 559 | case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break; |
| 560 | case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break; |
| 561 | case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break; |
| 562 | case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break; |
| 563 | case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break; |
| 564 | case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break; |
| 565 | case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break; |
| 566 | case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break; |
| 567 | case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break; |
| 568 | case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break; |
| 569 | case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break; |
| 570 | case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break; |
| 571 | case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break; |
| 572 | case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break; |
| 573 | case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break; |
| 574 | case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break; |
| 575 | case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break; |
| 576 | case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break; |
| 577 | case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break; |
| 578 | } |
| 579 | |
| 580 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | pch::ID &ID = TypeIDs[T.getTypePtr()]; |
| 585 | if (ID == 0) // we haven't seen this type before |
| 586 | ID = NextTypeID++; |
| 587 | |
| 588 | // Encode the type qualifiers in the type reference. |
| 589 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 590 | } |
| 591 | |
| 592 | void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) { |
| 593 | if (D == 0) { |
| 594 | Record.push_back(0); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | pch::ID &ID = DeclIDs[D]; |
| 599 | if (ID == 0) { |
| 600 | // We haven't seen this declaration before. Give it a new ID and |
| 601 | // enqueue it in the list of declarations to emit. |
| 602 | ID = DeclIDs.size(); |
| 603 | DeclsToEmit.push(const_cast<Decl *>(D)); |
| 604 | } |
| 605 | |
| 606 | Record.push_back(ID); |
| 607 | } |
| 608 | |
| 609 | void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { |
| 610 | Record.push_back(Name.getNameKind()); |
| 611 | switch (Name.getNameKind()) { |
| 612 | case DeclarationName::Identifier: |
| 613 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 614 | break; |
| 615 | |
| 616 | case DeclarationName::ObjCZeroArgSelector: |
| 617 | case DeclarationName::ObjCOneArgSelector: |
| 618 | case DeclarationName::ObjCMultiArgSelector: |
| 619 | assert(false && "Serialization of Objective-C selectors unavailable"); |
| 620 | break; |
| 621 | |
| 622 | case DeclarationName::CXXConstructorName: |
| 623 | case DeclarationName::CXXDestructorName: |
| 624 | case DeclarationName::CXXConversionFunctionName: |
| 625 | AddTypeRef(Name.getCXXNameType(), Record); |
| 626 | break; |
| 627 | |
| 628 | case DeclarationName::CXXOperatorName: |
| 629 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 630 | break; |
| 631 | |
| 632 | case DeclarationName::CXXUsingDirective: |
| 633 | // No extra data to emit |
| 634 | break; |
| 635 | } |
| 636 | } |