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" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 23 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Type serialization |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | namespace { |
| 32 | class VISIBILITY_HIDDEN PCHTypeWriter { |
| 33 | PCHWriter &Writer; |
| 34 | PCHWriter::RecordData &Record; |
| 35 | |
| 36 | public: |
| 37 | /// \brief Type code that corresponds to the record generated. |
| 38 | pch::TypeCode Code; |
| 39 | |
| 40 | PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 41 | : Writer(Writer), Record(Record) { } |
| 42 | |
| 43 | void VisitArrayType(const ArrayType *T); |
| 44 | void VisitFunctionType(const FunctionType *T); |
| 45 | void VisitTagType(const TagType *T); |
| 46 | |
| 47 | #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); |
| 48 | #define ABSTRACT_TYPE(Class, Base) |
| 49 | #define DEPENDENT_TYPE(Class, Base) |
| 50 | #include "clang/AST/TypeNodes.def" |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) { |
| 55 | Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record); |
| 56 | Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values |
| 57 | Record.push_back(T->getAddressSpace()); |
| 58 | Code = pch::TYPE_EXT_QUAL; |
| 59 | } |
| 60 | |
| 61 | void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) { |
| 62 | assert(false && "Built-in types are never serialized"); |
| 63 | } |
| 64 | |
| 65 | void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) { |
| 66 | Record.push_back(T->getWidth()); |
| 67 | Record.push_back(T->isSigned()); |
| 68 | Code = pch::TYPE_FIXED_WIDTH_INT; |
| 69 | } |
| 70 | |
| 71 | void PCHTypeWriter::VisitComplexType(const ComplexType *T) { |
| 72 | Writer.AddTypeRef(T->getElementType(), Record); |
| 73 | Code = pch::TYPE_COMPLEX; |
| 74 | } |
| 75 | |
| 76 | void PCHTypeWriter::VisitPointerType(const PointerType *T) { |
| 77 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 78 | Code = pch::TYPE_POINTER; |
| 79 | } |
| 80 | |
| 81 | void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { |
| 82 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 83 | Code = pch::TYPE_BLOCK_POINTER; |
| 84 | } |
| 85 | |
| 86 | void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { |
| 87 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 88 | Code = pch::TYPE_LVALUE_REFERENCE; |
| 89 | } |
| 90 | |
| 91 | void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { |
| 92 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 93 | Code = pch::TYPE_RVALUE_REFERENCE; |
| 94 | } |
| 95 | |
| 96 | void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { |
| 97 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 98 | Writer.AddTypeRef(QualType(T->getClass(), 0), Record); |
| 99 | Code = pch::TYPE_MEMBER_POINTER; |
| 100 | } |
| 101 | |
| 102 | void PCHTypeWriter::VisitArrayType(const ArrayType *T) { |
| 103 | Writer.AddTypeRef(T->getElementType(), Record); |
| 104 | Record.push_back(T->getSizeModifier()); // FIXME: stable values |
| 105 | Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values |
| 106 | } |
| 107 | |
| 108 | void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { |
| 109 | VisitArrayType(T); |
| 110 | Writer.AddAPInt(T->getSize(), Record); |
| 111 | Code = pch::TYPE_CONSTANT_ARRAY; |
| 112 | } |
| 113 | |
| 114 | void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 115 | VisitArrayType(T); |
| 116 | Code = pch::TYPE_INCOMPLETE_ARRAY; |
| 117 | } |
| 118 | |
| 119 | void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { |
| 120 | VisitArrayType(T); |
| 121 | // FIXME: Serialize array size expression. |
| 122 | assert(false && "Cannot serialize variable-length arrays"); |
| 123 | Code = pch::TYPE_VARIABLE_ARRAY; |
| 124 | } |
| 125 | |
| 126 | void PCHTypeWriter::VisitVectorType(const VectorType *T) { |
| 127 | Writer.AddTypeRef(T->getElementType(), Record); |
| 128 | Record.push_back(T->getNumElements()); |
| 129 | Code = pch::TYPE_VECTOR; |
| 130 | } |
| 131 | |
| 132 | void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) { |
| 133 | VisitVectorType(T); |
| 134 | Code = pch::TYPE_EXT_VECTOR; |
| 135 | } |
| 136 | |
| 137 | void PCHTypeWriter::VisitFunctionType(const FunctionType *T) { |
| 138 | Writer.AddTypeRef(T->getResultType(), Record); |
| 139 | } |
| 140 | |
| 141 | void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 142 | VisitFunctionType(T); |
| 143 | Code = pch::TYPE_FUNCTION_NO_PROTO; |
| 144 | } |
| 145 | |
| 146 | void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| 147 | VisitFunctionType(T); |
| 148 | Record.push_back(T->getNumArgs()); |
| 149 | for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) |
| 150 | Writer.AddTypeRef(T->getArgType(I), Record); |
| 151 | Record.push_back(T->isVariadic()); |
| 152 | Record.push_back(T->getTypeQuals()); |
| 153 | Code = pch::TYPE_FUNCTION_PROTO; |
| 154 | } |
| 155 | |
| 156 | void PCHTypeWriter::VisitTypedefType(const TypedefType *T) { |
| 157 | Writer.AddDeclRef(T->getDecl(), Record); |
| 158 | Code = pch::TYPE_TYPEDEF; |
| 159 | } |
| 160 | |
| 161 | void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { |
| 162 | // FIXME: serialize the typeof expression |
| 163 | assert(false && "Cannot serialize typeof(expr)"); |
| 164 | Code = pch::TYPE_TYPEOF_EXPR; |
| 165 | } |
| 166 | |
| 167 | void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) { |
| 168 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
| 169 | Code = pch::TYPE_TYPEOF; |
| 170 | } |
| 171 | |
| 172 | void PCHTypeWriter::VisitTagType(const TagType *T) { |
| 173 | Writer.AddDeclRef(T->getDecl(), Record); |
| 174 | assert(!T->isBeingDefined() && |
| 175 | "Cannot serialize in the middle of a type definition"); |
| 176 | } |
| 177 | |
| 178 | void PCHTypeWriter::VisitRecordType(const RecordType *T) { |
| 179 | VisitTagType(T); |
| 180 | Code = pch::TYPE_RECORD; |
| 181 | } |
| 182 | |
| 183 | void PCHTypeWriter::VisitEnumType(const EnumType *T) { |
| 184 | VisitTagType(T); |
| 185 | Code = pch::TYPE_ENUM; |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | PCHTypeWriter::VisitTemplateSpecializationType( |
| 190 | const TemplateSpecializationType *T) { |
| 191 | // FIXME: Serialize this type |
| 192 | assert(false && "Cannot serialize template specialization types"); |
| 193 | } |
| 194 | |
| 195 | void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) { |
| 196 | // FIXME: Serialize this type |
| 197 | assert(false && "Cannot serialize qualified name types"); |
| 198 | } |
| 199 | |
| 200 | void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
| 201 | Writer.AddDeclRef(T->getDecl(), Record); |
| 202 | Code = pch::TYPE_OBJC_INTERFACE; |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | PCHTypeWriter::VisitObjCQualifiedInterfaceType( |
| 207 | const ObjCQualifiedInterfaceType *T) { |
| 208 | VisitObjCInterfaceType(T); |
| 209 | Record.push_back(T->getNumProtocols()); |
| 210 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 211 | Writer.AddDeclRef(T->getProtocol(I), Record); |
| 212 | Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE; |
| 213 | } |
| 214 | |
| 215 | void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) { |
| 216 | Record.push_back(T->getNumProtocols()); |
| 217 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 218 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 219 | Code = pch::TYPE_OBJC_QUALIFIED_ID; |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) { |
| 224 | Record.push_back(T->getNumProtocols()); |
| 225 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 226 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 227 | Code = pch::TYPE_OBJC_QUALIFIED_CLASS; |
| 228 | } |
| 229 | |
| 230 | //===----------------------------------------------------------------------===// |
| 231 | // Declaration serialization |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | namespace { |
| 234 | class VISIBILITY_HIDDEN PCHDeclWriter |
| 235 | : public DeclVisitor<PCHDeclWriter, void> { |
| 236 | |
| 237 | PCHWriter &Writer; |
| 238 | PCHWriter::RecordData &Record; |
| 239 | |
| 240 | public: |
| 241 | pch::DeclCode Code; |
| 242 | |
| 243 | PCHDeclWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 244 | : Writer(Writer), Record(Record) { } |
| 245 | |
| 246 | void VisitDecl(Decl *D); |
| 247 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 248 | void VisitNamedDecl(NamedDecl *D); |
| 249 | void VisitTypeDecl(TypeDecl *D); |
| 250 | void VisitTypedefDecl(TypedefDecl *D); |
| 251 | void VisitValueDecl(ValueDecl *D); |
| 252 | void VisitVarDecl(VarDecl *D); |
| 253 | |
| 254 | void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 255 | uint64_t VisibleOffset); |
| 256 | }; |
| 257 | } |
| 258 | |
| 259 | void PCHDeclWriter::VisitDecl(Decl *D) { |
| 260 | Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); |
| 261 | Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); |
| 262 | Writer.AddSourceLocation(D->getLocation(), Record); |
| 263 | Record.push_back(D->isInvalidDecl()); |
| 264 | // FIXME: hasAttrs |
| 265 | Record.push_back(D->isImplicit()); |
| 266 | Record.push_back(D->getAccess()); |
| 267 | } |
| 268 | |
| 269 | void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 270 | VisitDecl(D); |
| 271 | Code = pch::DECL_TRANSLATION_UNIT; |
| 272 | } |
| 273 | |
| 274 | void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) { |
| 275 | VisitDecl(D); |
| 276 | Writer.AddDeclarationName(D->getDeclName(), Record); |
| 277 | } |
| 278 | |
| 279 | void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) { |
| 280 | VisitNamedDecl(D); |
| 281 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 282 | } |
| 283 | |
| 284 | void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { |
| 285 | VisitTypeDecl(D); |
| 286 | Writer.AddTypeRef(D->getUnderlyingType(), Record); |
| 287 | Code = pch::DECL_TYPEDEF; |
| 288 | } |
| 289 | |
| 290 | void PCHDeclWriter::VisitValueDecl(ValueDecl *D) { |
| 291 | VisitNamedDecl(D); |
| 292 | Writer.AddTypeRef(D->getType(), Record); |
| 293 | } |
| 294 | |
| 295 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
| 296 | VisitValueDecl(D); |
| 297 | Record.push_back(D->getStorageClass()); |
| 298 | Record.push_back(D->isThreadSpecified()); |
| 299 | Record.push_back(D->hasCXXDirectInitializer()); |
| 300 | Record.push_back(D->isDeclaredInCondition()); |
| 301 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 302 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
| 303 | // FIXME: emit initializer |
| 304 | Code = pch::DECL_VAR; |
| 305 | } |
| 306 | |
| 307 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 308 | /// |
| 309 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 310 | /// block for this declaration context is stored. May be 0 to indicate |
| 311 | /// that there are no declarations stored within this context. |
| 312 | /// |
| 313 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 314 | /// block for this declaration context is stored. May be 0 to indicate |
| 315 | /// that there are no declarations visible from this context. Note |
| 316 | /// that this value will not be emitted for non-primary declaration |
| 317 | /// contexts. |
| 318 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 319 | uint64_t VisibleOffset) { |
| 320 | Record.push_back(LexicalOffset); |
| 321 | if (DC->getPrimaryContext() == DC) |
| 322 | Record.push_back(VisibleOffset); |
| 323 | } |
| 324 | |
| 325 | //===----------------------------------------------------------------------===// |
| 326 | // PCHWriter Implementation |
| 327 | //===----------------------------------------------------------------------===// |
| 328 | |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 329 | //===----------------------------------------------------------------------===// |
| 330 | // Source Manager Serialization |
| 331 | //===----------------------------------------------------------------------===// |
| 332 | |
| 333 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 334 | /// file. |
| 335 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) { |
| 336 | using namespace llvm; |
| 337 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 338 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY)); |
| 339 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 340 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 341 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 342 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 343 | // FIXME: Need an actual encoding for the line directives; maybe |
| 344 | // this should be an array? |
| 345 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 346 | return S.EmitAbbrev(Abbrev); |
| 347 | } |
| 348 | |
| 349 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 350 | /// buffer. |
| 351 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) { |
| 352 | using namespace llvm; |
| 353 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 354 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY)); |
| 355 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 356 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 357 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 358 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 359 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
| 360 | return S.EmitAbbrev(Abbrev); |
| 361 | } |
| 362 | |
| 363 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 364 | /// buffer's blob. |
| 365 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) { |
| 366 | using namespace llvm; |
| 367 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 368 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB)); |
| 369 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
| 370 | return S.EmitAbbrev(Abbrev); |
| 371 | } |
| 372 | |
| 373 | /// \brief Create an abbreviation for the SLocEntry that refers to an |
| 374 | /// buffer. |
| 375 | static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) { |
| 376 | using namespace llvm; |
| 377 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 378 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY)); |
| 379 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 380 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 381 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 382 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
| 383 | return S.EmitAbbrev(Abbrev); |
| 384 | } |
| 385 | |
| 386 | /// \brief Writes the block containing the serialized form of the |
| 387 | /// source manager. |
| 388 | /// |
| 389 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 390 | /// blob), indexed based on the file name, so that we only create |
| 391 | /// entries for files that we actually need. In the common case (no |
| 392 | /// errors), we probably won't have to create file entries for any of |
| 393 | /// the files in the AST. |
| 394 | void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) { |
| 395 | // Enter the types block |
| 396 | S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3); |
| 397 | |
| 398 | // Abbreviations for the various kinds of source-location entries. |
| 399 | int SLocFileAbbrv = -1; |
| 400 | int SLocBufferAbbrv = -1; |
| 401 | int SLocBufferBlobAbbrv = -1; |
| 402 | int SLocInstantiationAbbrv = -1; |
| 403 | |
| 404 | // Write out the source location entry table. We skip the first |
| 405 | // entry, which is always the same dummy entry. |
| 406 | RecordData Record; |
| 407 | for (SourceManager::sloc_entry_iterator |
| 408 | SLoc = SourceMgr.sloc_entry_begin() + 1, |
| 409 | SLocEnd = SourceMgr.sloc_entry_end(); |
| 410 | SLoc != SLocEnd; ++SLoc) { |
| 411 | // Figure out which record code to use. |
| 412 | unsigned Code; |
| 413 | if (SLoc->isFile()) { |
| 414 | if (SLoc->getFile().getContentCache()->Entry) |
| 415 | Code = pch::SM_SLOC_FILE_ENTRY; |
| 416 | else |
| 417 | Code = pch::SM_SLOC_BUFFER_ENTRY; |
| 418 | } else |
| 419 | Code = pch::SM_SLOC_INSTANTIATION_ENTRY; |
| 420 | Record.push_back(Code); |
| 421 | |
| 422 | Record.push_back(SLoc->getOffset()); |
| 423 | if (SLoc->isFile()) { |
| 424 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 425 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 426 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
| 427 | Record.push_back(File.hasLineDirectives()); // FIXME: encode the |
| 428 | // line directives? |
| 429 | |
| 430 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
| 431 | if (Content->Entry) { |
| 432 | // The source location entry is a file. The blob associated |
| 433 | // with this entry is the file name. |
| 434 | if (SLocFileAbbrv == -1) |
| 435 | SLocFileAbbrv = CreateSLocFileAbbrev(S); |
| 436 | S.EmitRecordWithBlob(SLocFileAbbrv, Record, |
| 437 | Content->Entry->getName(), |
| 438 | strlen(Content->Entry->getName())); |
| 439 | } else { |
| 440 | // The source location entry is a buffer. The blob associated |
| 441 | // with this entry contains the contents of the buffer. |
| 442 | if (SLocBufferAbbrv == -1) { |
| 443 | SLocBufferAbbrv = CreateSLocBufferAbbrev(S); |
| 444 | SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S); |
| 445 | } |
| 446 | |
| 447 | // We add one to the size so that we capture the trailing NULL |
| 448 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 449 | // the reader side). |
| 450 | const llvm::MemoryBuffer *Buffer = Content->getBuffer(); |
| 451 | const char *Name = Buffer->getBufferIdentifier(); |
| 452 | S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1); |
| 453 | Record.clear(); |
| 454 | Record.push_back(pch::SM_SLOC_BUFFER_BLOB); |
| 455 | S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
| 456 | Buffer->getBufferStart(), |
| 457 | Buffer->getBufferSize() + 1); |
| 458 | } |
| 459 | } else { |
| 460 | // The source location entry is an instantiation. |
| 461 | const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); |
| 462 | Record.push_back(Inst.getSpellingLoc().getRawEncoding()); |
| 463 | Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); |
| 464 | Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); |
| 465 | |
| 466 | if (SLocInstantiationAbbrv == -1) |
| 467 | SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S); |
| 468 | S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); |
| 469 | } |
| 470 | |
| 471 | Record.clear(); |
| 472 | } |
| 473 | |
| 474 | S.ExitBlock(); |
| 475 | } |
| 476 | |
Chris Lattner | ffc05ed | 2009-04-10 17:15:23 +0000 | [diff] [blame^] | 477 | /// \brief Writes the block containing the serialized form of the |
| 478 | /// preprocessor. |
| 479 | /// |
| 480 | void PCHWriter::WritePreprocessor(Preprocessor &PP) { |
| 481 | } |
| 482 | |
| 483 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 484 | /// \brief Write the representation of a type to the PCH stream. |
| 485 | void PCHWriter::WriteType(const Type *T) { |
| 486 | pch::ID &ID = TypeIDs[T]; |
| 487 | if (ID == 0) // we haven't seen this type before |
| 488 | ID = NextTypeID++; |
| 489 | |
| 490 | // Record the offset for this type. |
| 491 | if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS) |
| 492 | TypeOffsets.push_back(S.GetCurrentBitNo()); |
| 493 | else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) { |
| 494 | TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS); |
| 495 | TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo(); |
| 496 | } |
| 497 | |
| 498 | RecordData Record; |
| 499 | |
| 500 | // Emit the type's representation. |
| 501 | PCHTypeWriter W(*this, Record); |
| 502 | switch (T->getTypeClass()) { |
| 503 | // For all of the concrete, non-dependent types, call the |
| 504 | // appropriate visitor function. |
| 505 | #define TYPE(Class, Base) \ |
| 506 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
| 507 | #define ABSTRACT_TYPE(Class, Base) |
| 508 | #define DEPENDENT_TYPE(Class, Base) |
| 509 | #include "clang/AST/TypeNodes.def" |
| 510 | |
| 511 | // For all of the dependent type nodes (which only occur in C++ |
| 512 | // templates), produce an error. |
| 513 | #define TYPE(Class, Base) |
| 514 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 515 | #include "clang/AST/TypeNodes.def" |
| 516 | assert(false && "Cannot serialize dependent type nodes"); |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | // Emit the serialized record. |
| 521 | S.EmitRecord(W.Code, Record); |
| 522 | } |
| 523 | |
| 524 | /// \brief Write a block containing all of the types. |
| 525 | void PCHWriter::WriteTypesBlock(ASTContext &Context) { |
| 526 | // Enter the types block |
| 527 | S.EnterSubblock(pch::TYPES_BLOCK_ID, 2); |
| 528 | |
| 529 | // Emit all of the types in the ASTContext |
| 530 | for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(), |
| 531 | TEnd = Context.getTypes().end(); |
| 532 | T != TEnd; ++T) { |
| 533 | // Builtin types are never serialized. |
| 534 | if (isa<BuiltinType>(*T)) |
| 535 | continue; |
| 536 | |
| 537 | WriteType(*T); |
| 538 | } |
| 539 | |
| 540 | // Exit the types block |
| 541 | S.ExitBlock(); |
| 542 | |
| 543 | // Write the type offsets block |
| 544 | S.EnterSubblock(pch::TYPE_OFFSETS_BLOCK_ID, 2); |
| 545 | S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets); |
| 546 | S.ExitBlock(); |
| 547 | } |
| 548 | |
| 549 | /// \brief Write the block containing all of the declaration IDs |
| 550 | /// lexically declared within the given DeclContext. |
| 551 | /// |
| 552 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 553 | /// bistream, or 0 if no block was written. |
| 554 | uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
| 555 | DeclContext *DC) { |
| 556 | if (DC->decls_begin(Context) == DC->decls_end(Context)) |
| 557 | return 0; |
| 558 | |
| 559 | uint64_t Offset = S.GetCurrentBitNo(); |
| 560 | RecordData Record; |
| 561 | for (DeclContext::decl_iterator D = DC->decls_begin(Context), |
| 562 | DEnd = DC->decls_end(Context); |
| 563 | D != DEnd; ++D) |
| 564 | AddDeclRef(*D, Record); |
| 565 | |
| 566 | S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record); |
| 567 | return Offset; |
| 568 | } |
| 569 | |
| 570 | /// \brief Write the block containing all of the declaration IDs |
| 571 | /// visible from the given DeclContext. |
| 572 | /// |
| 573 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
| 574 | /// bistream, or 0 if no block was written. |
| 575 | uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 576 | DeclContext *DC) { |
| 577 | if (DC->getPrimaryContext() != DC) |
| 578 | return 0; |
| 579 | |
| 580 | // Force the DeclContext to build a its name-lookup table. |
| 581 | DC->lookup(Context, DeclarationName()); |
| 582 | |
| 583 | // Serialize the contents of the mapping used for lookup. Note that, |
| 584 | // although we have two very different code paths, the serialized |
| 585 | // representation is the same for both cases: a declaration name, |
| 586 | // followed by a size, followed by references to the visible |
| 587 | // declarations that have that name. |
| 588 | uint64_t Offset = S.GetCurrentBitNo(); |
| 589 | RecordData Record; |
| 590 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
| 591 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 592 | D != DEnd; ++D) { |
| 593 | AddDeclarationName(D->first, Record); |
| 594 | DeclContext::lookup_result Result = D->second.getLookupResult(Context); |
| 595 | Record.push_back(Result.second - Result.first); |
| 596 | for(; Result.first != Result.second; ++Result.first) |
| 597 | AddDeclRef(*Result.first, Record); |
| 598 | } |
| 599 | |
| 600 | if (Record.size() == 0) |
| 601 | return 0; |
| 602 | |
| 603 | S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record); |
| 604 | return Offset; |
| 605 | } |
| 606 | |
| 607 | /// \brief Write a block containing all of the declarations. |
| 608 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
| 609 | // Enter the declarations block |
| 610 | S.EnterSubblock(pch::DECLS_BLOCK_ID, 2); |
| 611 | |
| 612 | // Emit all of the declarations. |
| 613 | RecordData Record; |
| 614 | PCHDeclWriter W(*this, Record); |
| 615 | while (!DeclsToEmit.empty()) { |
| 616 | // Pull the next declaration off the queue |
| 617 | Decl *D = DeclsToEmit.front(); |
| 618 | DeclsToEmit.pop(); |
| 619 | |
| 620 | // If this declaration is also a DeclContext, write blocks for the |
| 621 | // declarations that lexically stored inside its context and those |
| 622 | // declarations that are visible from its context. These blocks |
| 623 | // are written before the declaration itself so that we can put |
| 624 | // their offsets into the record for the declaration. |
| 625 | uint64_t LexicalOffset = 0; |
| 626 | uint64_t VisibleOffset = 0; |
| 627 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 628 | if (DC) { |
| 629 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 630 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 631 | } |
| 632 | |
| 633 | // Determine the ID for this declaration |
| 634 | pch::ID ID = DeclIDs[D]; |
| 635 | if (ID == 0) |
| 636 | ID = DeclIDs.size(); |
| 637 | |
| 638 | unsigned Index = ID - 1; |
| 639 | |
| 640 | // Record the offset for this declaration |
| 641 | if (DeclOffsets.size() == Index) |
| 642 | DeclOffsets.push_back(S.GetCurrentBitNo()); |
| 643 | else if (DeclOffsets.size() < Index) { |
| 644 | DeclOffsets.resize(Index+1); |
| 645 | DeclOffsets[Index] = S.GetCurrentBitNo(); |
| 646 | } |
| 647 | |
| 648 | // Build and emit a record for this declaration |
| 649 | Record.clear(); |
| 650 | W.Code = (pch::DeclCode)0; |
| 651 | W.Visit(D); |
| 652 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
| 653 | assert(W.Code && "Visitor did not set record code"); |
| 654 | S.EmitRecord(W.Code, Record); |
| 655 | } |
| 656 | |
| 657 | // Exit the declarations block |
| 658 | S.ExitBlock(); |
| 659 | |
| 660 | // Write the declaration offsets block |
| 661 | S.EnterSubblock(pch::DECL_OFFSETS_BLOCK_ID, 2); |
| 662 | S.EmitRecord(pch::DECL_OFFSET, DeclOffsets); |
| 663 | S.ExitBlock(); |
| 664 | } |
| 665 | |
| 666 | PCHWriter::PCHWriter(llvm::BitstreamWriter &S) |
| 667 | : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { } |
| 668 | |
Chris Lattner | ffc05ed | 2009-04-10 17:15:23 +0000 | [diff] [blame^] | 669 | void PCHWriter::WritePCH(ASTContext &Context, Preprocessor &PP) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 670 | // Emit the file header. |
| 671 | S.Emit((unsigned)'C', 8); |
| 672 | S.Emit((unsigned)'P', 8); |
| 673 | S.Emit((unsigned)'C', 8); |
| 674 | S.Emit((unsigned)'H', 8); |
| 675 | |
| 676 | // The translation unit is the first declaration we'll emit. |
| 677 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
| 678 | DeclsToEmit.push(Context.getTranslationUnitDecl()); |
| 679 | |
| 680 | // Write the remaining PCH contents. |
| 681 | S.EnterSubblock(pch::PCH_BLOCK_ID, 2); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 682 | WriteSourceManagerBlock(Context.getSourceManager()); |
Chris Lattner | ffc05ed | 2009-04-10 17:15:23 +0000 | [diff] [blame^] | 683 | WritePreprocessor(PP); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 684 | WriteTypesBlock(Context); |
| 685 | WriteDeclsBlock(Context); |
| 686 | S.ExitBlock(); |
| 687 | } |
| 688 | |
| 689 | void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { |
| 690 | Record.push_back(Loc.getRawEncoding()); |
| 691 | } |
| 692 | |
| 693 | void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { |
| 694 | Record.push_back(Value.getBitWidth()); |
| 695 | unsigned N = Value.getNumWords(); |
| 696 | const uint64_t* Words = Value.getRawData(); |
| 697 | for (unsigned I = 0; I != N; ++I) |
| 698 | Record.push_back(Words[I]); |
| 699 | } |
| 700 | |
| 701 | void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { |
| 702 | // FIXME: Emit an identifier ID, not the actual string! |
| 703 | const char *Name = II->getName(); |
| 704 | unsigned Len = strlen(Name); |
| 705 | Record.push_back(Len); |
| 706 | Record.insert(Record.end(), Name, Name + Len); |
| 707 | } |
| 708 | |
| 709 | void PCHWriter::AddTypeRef(QualType T, RecordData &Record) { |
| 710 | if (T.isNull()) { |
| 711 | Record.push_back(pch::PREDEF_TYPE_NULL_ID); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) { |
| 716 | pch::ID ID; |
| 717 | switch (BT->getKind()) { |
| 718 | case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break; |
| 719 | case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break; |
| 720 | case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break; |
| 721 | case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break; |
| 722 | case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break; |
| 723 | case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break; |
| 724 | case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break; |
| 725 | case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break; |
| 726 | case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break; |
| 727 | case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break; |
| 728 | case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break; |
| 729 | case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break; |
| 730 | case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break; |
| 731 | case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break; |
| 732 | case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break; |
| 733 | case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break; |
| 734 | case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break; |
| 735 | case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break; |
| 736 | case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break; |
| 737 | case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break; |
| 738 | } |
| 739 | |
| 740 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | pch::ID &ID = TypeIDs[T.getTypePtr()]; |
| 745 | if (ID == 0) // we haven't seen this type before |
| 746 | ID = NextTypeID++; |
| 747 | |
| 748 | // Encode the type qualifiers in the type reference. |
| 749 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 750 | } |
| 751 | |
| 752 | void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) { |
| 753 | if (D == 0) { |
| 754 | Record.push_back(0); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | pch::ID &ID = DeclIDs[D]; |
| 759 | if (ID == 0) { |
| 760 | // We haven't seen this declaration before. Give it a new ID and |
| 761 | // enqueue it in the list of declarations to emit. |
| 762 | ID = DeclIDs.size(); |
| 763 | DeclsToEmit.push(const_cast<Decl *>(D)); |
| 764 | } |
| 765 | |
| 766 | Record.push_back(ID); |
| 767 | } |
| 768 | |
| 769 | void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { |
| 770 | Record.push_back(Name.getNameKind()); |
| 771 | switch (Name.getNameKind()) { |
| 772 | case DeclarationName::Identifier: |
| 773 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 774 | break; |
| 775 | |
| 776 | case DeclarationName::ObjCZeroArgSelector: |
| 777 | case DeclarationName::ObjCOneArgSelector: |
| 778 | case DeclarationName::ObjCMultiArgSelector: |
| 779 | assert(false && "Serialization of Objective-C selectors unavailable"); |
| 780 | break; |
| 781 | |
| 782 | case DeclarationName::CXXConstructorName: |
| 783 | case DeclarationName::CXXDestructorName: |
| 784 | case DeclarationName::CXXConversionFunctionName: |
| 785 | AddTypeRef(Name.getCXXNameType(), Record); |
| 786 | break; |
| 787 | |
| 788 | case DeclarationName::CXXOperatorName: |
| 789 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 790 | break; |
| 791 | |
| 792 | case DeclarationName::CXXUsingDirective: |
| 793 | // No extra data to emit |
| 794 | break; |
| 795 | } |
| 796 | } |