Douglas Gregor | 2cf2634 | 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" |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 20 | #include "clang/Lex/MacroInfo.h" |
| 21 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 22 | #include "clang/Basic/FileManager.h" |
| 23 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 25 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 26 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 3c304bd | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 28 | #include <cstdio> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // Type serialization |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | namespace { |
| 35 | class VISIBILITY_HIDDEN PCHTypeWriter { |
| 36 | PCHWriter &Writer; |
| 37 | PCHWriter::RecordData &Record; |
| 38 | |
| 39 | public: |
| 40 | /// \brief Type code that corresponds to the record generated. |
| 41 | pch::TypeCode Code; |
| 42 | |
| 43 | PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 44 | : Writer(Writer), Record(Record) { } |
| 45 | |
| 46 | void VisitArrayType(const ArrayType *T); |
| 47 | void VisitFunctionType(const FunctionType *T); |
| 48 | void VisitTagType(const TagType *T); |
| 49 | |
| 50 | #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); |
| 51 | #define ABSTRACT_TYPE(Class, Base) |
| 52 | #define DEPENDENT_TYPE(Class, Base) |
| 53 | #include "clang/AST/TypeNodes.def" |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) { |
| 58 | Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record); |
| 59 | Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values |
| 60 | Record.push_back(T->getAddressSpace()); |
| 61 | Code = pch::TYPE_EXT_QUAL; |
| 62 | } |
| 63 | |
| 64 | void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) { |
| 65 | assert(false && "Built-in types are never serialized"); |
| 66 | } |
| 67 | |
| 68 | void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) { |
| 69 | Record.push_back(T->getWidth()); |
| 70 | Record.push_back(T->isSigned()); |
| 71 | Code = pch::TYPE_FIXED_WIDTH_INT; |
| 72 | } |
| 73 | |
| 74 | void PCHTypeWriter::VisitComplexType(const ComplexType *T) { |
| 75 | Writer.AddTypeRef(T->getElementType(), Record); |
| 76 | Code = pch::TYPE_COMPLEX; |
| 77 | } |
| 78 | |
| 79 | void PCHTypeWriter::VisitPointerType(const PointerType *T) { |
| 80 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 81 | Code = pch::TYPE_POINTER; |
| 82 | } |
| 83 | |
| 84 | void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { |
| 85 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 86 | Code = pch::TYPE_BLOCK_POINTER; |
| 87 | } |
| 88 | |
| 89 | void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { |
| 90 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 91 | Code = pch::TYPE_LVALUE_REFERENCE; |
| 92 | } |
| 93 | |
| 94 | void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { |
| 95 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 96 | Code = pch::TYPE_RVALUE_REFERENCE; |
| 97 | } |
| 98 | |
| 99 | void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { |
| 100 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 101 | Writer.AddTypeRef(QualType(T->getClass(), 0), Record); |
| 102 | Code = pch::TYPE_MEMBER_POINTER; |
| 103 | } |
| 104 | |
| 105 | void PCHTypeWriter::VisitArrayType(const ArrayType *T) { |
| 106 | Writer.AddTypeRef(T->getElementType(), Record); |
| 107 | Record.push_back(T->getSizeModifier()); // FIXME: stable values |
| 108 | Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values |
| 109 | } |
| 110 | |
| 111 | void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { |
| 112 | VisitArrayType(T); |
| 113 | Writer.AddAPInt(T->getSize(), Record); |
| 114 | Code = pch::TYPE_CONSTANT_ARRAY; |
| 115 | } |
| 116 | |
| 117 | void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 118 | VisitArrayType(T); |
| 119 | Code = pch::TYPE_INCOMPLETE_ARRAY; |
| 120 | } |
| 121 | |
| 122 | void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { |
| 123 | VisitArrayType(T); |
| 124 | // FIXME: Serialize array size expression. |
| 125 | assert(false && "Cannot serialize variable-length arrays"); |
| 126 | Code = pch::TYPE_VARIABLE_ARRAY; |
| 127 | } |
| 128 | |
| 129 | void PCHTypeWriter::VisitVectorType(const VectorType *T) { |
| 130 | Writer.AddTypeRef(T->getElementType(), Record); |
| 131 | Record.push_back(T->getNumElements()); |
| 132 | Code = pch::TYPE_VECTOR; |
| 133 | } |
| 134 | |
| 135 | void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) { |
| 136 | VisitVectorType(T); |
| 137 | Code = pch::TYPE_EXT_VECTOR; |
| 138 | } |
| 139 | |
| 140 | void PCHTypeWriter::VisitFunctionType(const FunctionType *T) { |
| 141 | Writer.AddTypeRef(T->getResultType(), Record); |
| 142 | } |
| 143 | |
| 144 | void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 145 | VisitFunctionType(T); |
| 146 | Code = pch::TYPE_FUNCTION_NO_PROTO; |
| 147 | } |
| 148 | |
| 149 | void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| 150 | VisitFunctionType(T); |
| 151 | Record.push_back(T->getNumArgs()); |
| 152 | for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) |
| 153 | Writer.AddTypeRef(T->getArgType(I), Record); |
| 154 | Record.push_back(T->isVariadic()); |
| 155 | Record.push_back(T->getTypeQuals()); |
| 156 | Code = pch::TYPE_FUNCTION_PROTO; |
| 157 | } |
| 158 | |
| 159 | void PCHTypeWriter::VisitTypedefType(const TypedefType *T) { |
| 160 | Writer.AddDeclRef(T->getDecl(), Record); |
| 161 | Code = pch::TYPE_TYPEDEF; |
| 162 | } |
| 163 | |
| 164 | void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { |
| 165 | // FIXME: serialize the typeof expression |
| 166 | assert(false && "Cannot serialize typeof(expr)"); |
| 167 | Code = pch::TYPE_TYPEOF_EXPR; |
| 168 | } |
| 169 | |
| 170 | void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) { |
| 171 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
| 172 | Code = pch::TYPE_TYPEOF; |
| 173 | } |
| 174 | |
| 175 | void PCHTypeWriter::VisitTagType(const TagType *T) { |
| 176 | Writer.AddDeclRef(T->getDecl(), Record); |
| 177 | assert(!T->isBeingDefined() && |
| 178 | "Cannot serialize in the middle of a type definition"); |
| 179 | } |
| 180 | |
| 181 | void PCHTypeWriter::VisitRecordType(const RecordType *T) { |
| 182 | VisitTagType(T); |
| 183 | Code = pch::TYPE_RECORD; |
| 184 | } |
| 185 | |
| 186 | void PCHTypeWriter::VisitEnumType(const EnumType *T) { |
| 187 | VisitTagType(T); |
| 188 | Code = pch::TYPE_ENUM; |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | PCHTypeWriter::VisitTemplateSpecializationType( |
| 193 | const TemplateSpecializationType *T) { |
| 194 | // FIXME: Serialize this type |
| 195 | assert(false && "Cannot serialize template specialization types"); |
| 196 | } |
| 197 | |
| 198 | void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) { |
| 199 | // FIXME: Serialize this type |
| 200 | assert(false && "Cannot serialize qualified name types"); |
| 201 | } |
| 202 | |
| 203 | void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
| 204 | Writer.AddDeclRef(T->getDecl(), Record); |
| 205 | Code = pch::TYPE_OBJC_INTERFACE; |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | PCHTypeWriter::VisitObjCQualifiedInterfaceType( |
| 210 | const ObjCQualifiedInterfaceType *T) { |
| 211 | VisitObjCInterfaceType(T); |
| 212 | Record.push_back(T->getNumProtocols()); |
| 213 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 214 | Writer.AddDeclRef(T->getProtocol(I), Record); |
| 215 | Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE; |
| 216 | } |
| 217 | |
| 218 | void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) { |
| 219 | Record.push_back(T->getNumProtocols()); |
| 220 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 221 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 222 | Code = pch::TYPE_OBJC_QUALIFIED_ID; |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) { |
| 227 | Record.push_back(T->getNumProtocols()); |
| 228 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 229 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 230 | Code = pch::TYPE_OBJC_QUALIFIED_CLASS; |
| 231 | } |
| 232 | |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | // Declaration serialization |
| 235 | //===----------------------------------------------------------------------===// |
| 236 | namespace { |
| 237 | class VISIBILITY_HIDDEN PCHDeclWriter |
| 238 | : public DeclVisitor<PCHDeclWriter, void> { |
| 239 | |
| 240 | PCHWriter &Writer; |
| 241 | PCHWriter::RecordData &Record; |
| 242 | |
| 243 | public: |
| 244 | pch::DeclCode Code; |
| 245 | |
| 246 | PCHDeclWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 247 | : Writer(Writer), Record(Record) { } |
| 248 | |
| 249 | void VisitDecl(Decl *D); |
| 250 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 251 | void VisitNamedDecl(NamedDecl *D); |
| 252 | void VisitTypeDecl(TypeDecl *D); |
| 253 | void VisitTypedefDecl(TypedefDecl *D); |
| 254 | void VisitValueDecl(ValueDecl *D); |
| 255 | void VisitVarDecl(VarDecl *D); |
| 256 | |
| 257 | void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 258 | uint64_t VisibleOffset); |
| 259 | }; |
| 260 | } |
| 261 | |
| 262 | void PCHDeclWriter::VisitDecl(Decl *D) { |
| 263 | Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); |
| 264 | Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); |
| 265 | Writer.AddSourceLocation(D->getLocation(), Record); |
| 266 | Record.push_back(D->isInvalidDecl()); |
| 267 | // FIXME: hasAttrs |
| 268 | Record.push_back(D->isImplicit()); |
| 269 | Record.push_back(D->getAccess()); |
| 270 | } |
| 271 | |
| 272 | void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 273 | VisitDecl(D); |
| 274 | Code = pch::DECL_TRANSLATION_UNIT; |
| 275 | } |
| 276 | |
| 277 | void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) { |
| 278 | VisitDecl(D); |
| 279 | Writer.AddDeclarationName(D->getDeclName(), Record); |
| 280 | } |
| 281 | |
| 282 | void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) { |
| 283 | VisitNamedDecl(D); |
| 284 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 285 | } |
| 286 | |
| 287 | void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { |
| 288 | VisitTypeDecl(D); |
| 289 | Writer.AddTypeRef(D->getUnderlyingType(), Record); |
| 290 | Code = pch::DECL_TYPEDEF; |
| 291 | } |
| 292 | |
| 293 | void PCHDeclWriter::VisitValueDecl(ValueDecl *D) { |
| 294 | VisitNamedDecl(D); |
| 295 | Writer.AddTypeRef(D->getType(), Record); |
| 296 | } |
| 297 | |
| 298 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
| 299 | VisitValueDecl(D); |
| 300 | Record.push_back(D->getStorageClass()); |
| 301 | Record.push_back(D->isThreadSpecified()); |
| 302 | Record.push_back(D->hasCXXDirectInitializer()); |
| 303 | Record.push_back(D->isDeclaredInCondition()); |
| 304 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 305 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
| 306 | // FIXME: emit initializer |
| 307 | Code = pch::DECL_VAR; |
| 308 | } |
| 309 | |
| 310 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 311 | /// |
| 312 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 313 | /// block for this declaration context is stored. May be 0 to indicate |
| 314 | /// that there are no declarations stored within this context. |
| 315 | /// |
| 316 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 317 | /// block for this declaration context is stored. May be 0 to indicate |
| 318 | /// that there are no declarations visible from this context. Note |
| 319 | /// that this value will not be emitted for non-primary declaration |
| 320 | /// contexts. |
| 321 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 322 | uint64_t VisibleOffset) { |
| 323 | Record.push_back(LexicalOffset); |
| 324 | if (DC->getPrimaryContext() == DC) |
| 325 | Record.push_back(VisibleOffset); |
| 326 | } |
| 327 | |
| 328 | //===----------------------------------------------------------------------===// |
| 329 | // PCHWriter Implementation |
| 330 | //===----------------------------------------------------------------------===// |
| 331 | |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 332 | /// \brief Write the target triple (e.g., i686-apple-darwin9). |
| 333 | void PCHWriter::WriteTargetTriple(const TargetInfo &Target) { |
| 334 | using namespace llvm; |
| 335 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 336 | Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE)); |
| 337 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
| 338 | unsigned TripleAbbrev = S.EmitAbbrev(Abbrev); |
| 339 | |
| 340 | RecordData Record; |
| 341 | Record.push_back(pch::TARGET_TRIPLE); |
| 342 | const char *Triple = Target.getTargetTriple(); |
| 343 | S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple)); |
| 344 | } |
| 345 | |
| 346 | /// \brief Write the LangOptions structure. |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 347 | void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { |
| 348 | RecordData Record; |
| 349 | Record.push_back(LangOpts.Trigraphs); |
| 350 | Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments. |
| 351 | Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers. |
| 352 | Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode. |
| 353 | Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc) |
| 354 | Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'. |
| 355 | Record.push_back(LangOpts.Digraphs); // C94, C99 and C++ |
| 356 | Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants. |
| 357 | Record.push_back(LangOpts.C99); // C99 Support |
| 358 | Record.push_back(LangOpts.Microsoft); // Microsoft extensions. |
| 359 | Record.push_back(LangOpts.CPlusPlus); // C++ Support |
| 360 | Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support |
| 361 | Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode. |
| 362 | Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords. |
| 363 | |
| 364 | Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled. |
| 365 | Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled. |
| 366 | Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled |
| 367 | |
| 368 | Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings |
| 369 | Record.push_back(LangOpts.Boolean); // Allow bool/true/false |
| 370 | Record.push_back(LangOpts.WritableStrings); // Allow writable strings |
| 371 | Record.push_back(LangOpts.LaxVectorConversions); |
| 372 | Record.push_back(LangOpts.Exceptions); // Support exception handling. |
| 373 | |
| 374 | Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime. |
| 375 | Record.push_back(LangOpts.Freestanding); // Freestanding implementation |
| 376 | Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin) |
| 377 | |
| 378 | Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected |
| 379 | // by locks. |
| 380 | Record.push_back(LangOpts.Blocks); // block extension to C |
| 381 | Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if |
| 382 | // they are unused. |
| 383 | Record.push_back(LangOpts.MathErrno); // Math functions must respect errno |
| 384 | // (modulo the platform support). |
| 385 | |
| 386 | Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when |
| 387 | // signed integer arithmetic overflows. |
| 388 | |
| 389 | Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and |
| 390 | // may be ripped out at any time. |
| 391 | |
| 392 | Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. |
| 393 | Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be |
| 394 | // defined. |
| 395 | Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as |
| 396 | // opposed to __DYNAMIC__). |
| 397 | Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero. |
| 398 | |
| 399 | Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be |
| 400 | // used (instead of C99 semantics). |
| 401 | Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined. |
| 402 | Record.push_back(LangOpts.getGCMode()); |
| 403 | Record.push_back(LangOpts.getVisibilityMode()); |
| 404 | Record.push_back(LangOpts.InstantiationDepth); |
| 405 | S.EmitRecord(pch::LANGUAGE_OPTIONS, Record); |
| 406 | } |
| 407 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 408 | //===----------------------------------------------------------------------===// |
| 409 | // Source Manager Serialization |
| 410 | //===----------------------------------------------------------------------===// |
| 411 | |
| 412 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 413 | /// file. |
| 414 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) { |
| 415 | using namespace llvm; |
| 416 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 417 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY)); |
| 418 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 419 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 420 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 421 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 422 | // FIXME: Need an actual encoding for the line directives; maybe |
| 423 | // this should be an array? |
| 424 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 425 | return S.EmitAbbrev(Abbrev); |
| 426 | } |
| 427 | |
| 428 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 429 | /// buffer. |
| 430 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) { |
| 431 | using namespace llvm; |
| 432 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 433 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY)); |
| 434 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 435 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 436 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 437 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 438 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
| 439 | return S.EmitAbbrev(Abbrev); |
| 440 | } |
| 441 | |
| 442 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 443 | /// buffer's blob. |
| 444 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) { |
| 445 | using namespace llvm; |
| 446 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 447 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB)); |
| 448 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
| 449 | return S.EmitAbbrev(Abbrev); |
| 450 | } |
| 451 | |
| 452 | /// \brief Create an abbreviation for the SLocEntry that refers to an |
| 453 | /// buffer. |
| 454 | static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) { |
| 455 | using namespace llvm; |
| 456 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 457 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY)); |
| 458 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 459 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 460 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 461 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
| 462 | return S.EmitAbbrev(Abbrev); |
| 463 | } |
| 464 | |
| 465 | /// \brief Writes the block containing the serialized form of the |
| 466 | /// source manager. |
| 467 | /// |
| 468 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 469 | /// blob), indexed based on the file name, so that we only create |
| 470 | /// entries for files that we actually need. In the common case (no |
| 471 | /// errors), we probably won't have to create file entries for any of |
| 472 | /// the files in the AST. |
| 473 | void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 474 | // Enter the source manager block. |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 475 | S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3); |
| 476 | |
| 477 | // Abbreviations for the various kinds of source-location entries. |
| 478 | int SLocFileAbbrv = -1; |
| 479 | int SLocBufferAbbrv = -1; |
| 480 | int SLocBufferBlobAbbrv = -1; |
| 481 | int SLocInstantiationAbbrv = -1; |
| 482 | |
| 483 | // Write out the source location entry table. We skip the first |
| 484 | // entry, which is always the same dummy entry. |
| 485 | RecordData Record; |
| 486 | for (SourceManager::sloc_entry_iterator |
| 487 | SLoc = SourceMgr.sloc_entry_begin() + 1, |
| 488 | SLocEnd = SourceMgr.sloc_entry_end(); |
| 489 | SLoc != SLocEnd; ++SLoc) { |
| 490 | // Figure out which record code to use. |
| 491 | unsigned Code; |
| 492 | if (SLoc->isFile()) { |
| 493 | if (SLoc->getFile().getContentCache()->Entry) |
| 494 | Code = pch::SM_SLOC_FILE_ENTRY; |
| 495 | else |
| 496 | Code = pch::SM_SLOC_BUFFER_ENTRY; |
| 497 | } else |
| 498 | Code = pch::SM_SLOC_INSTANTIATION_ENTRY; |
| 499 | Record.push_back(Code); |
| 500 | |
| 501 | Record.push_back(SLoc->getOffset()); |
| 502 | if (SLoc->isFile()) { |
| 503 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 504 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 505 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
| 506 | Record.push_back(File.hasLineDirectives()); // FIXME: encode the |
| 507 | // line directives? |
| 508 | |
| 509 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
| 510 | if (Content->Entry) { |
| 511 | // The source location entry is a file. The blob associated |
| 512 | // with this entry is the file name. |
| 513 | if (SLocFileAbbrv == -1) |
| 514 | SLocFileAbbrv = CreateSLocFileAbbrev(S); |
| 515 | S.EmitRecordWithBlob(SLocFileAbbrv, Record, |
| 516 | Content->Entry->getName(), |
| 517 | strlen(Content->Entry->getName())); |
| 518 | } else { |
| 519 | // The source location entry is a buffer. The blob associated |
| 520 | // with this entry contains the contents of the buffer. |
| 521 | if (SLocBufferAbbrv == -1) { |
| 522 | SLocBufferAbbrv = CreateSLocBufferAbbrev(S); |
| 523 | SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S); |
| 524 | } |
| 525 | |
| 526 | // We add one to the size so that we capture the trailing NULL |
| 527 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 528 | // the reader side). |
| 529 | const llvm::MemoryBuffer *Buffer = Content->getBuffer(); |
| 530 | const char *Name = Buffer->getBufferIdentifier(); |
| 531 | S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1); |
| 532 | Record.clear(); |
| 533 | Record.push_back(pch::SM_SLOC_BUFFER_BLOB); |
| 534 | S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
| 535 | Buffer->getBufferStart(), |
| 536 | Buffer->getBufferSize() + 1); |
| 537 | } |
| 538 | } else { |
| 539 | // The source location entry is an instantiation. |
| 540 | const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); |
| 541 | Record.push_back(Inst.getSpellingLoc().getRawEncoding()); |
| 542 | Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); |
| 543 | Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); |
| 544 | |
| 545 | if (SLocInstantiationAbbrv == -1) |
| 546 | SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S); |
| 547 | S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); |
| 548 | } |
| 549 | |
| 550 | Record.clear(); |
| 551 | } |
| 552 | |
| 553 | S.ExitBlock(); |
| 554 | } |
| 555 | |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 556 | /// \brief Writes the block containing the serialized form of the |
| 557 | /// preprocessor. |
| 558 | /// |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 559 | void PCHWriter::WritePreprocessor(const Preprocessor &PP) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 560 | // Enter the preprocessor block. |
| 561 | S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3); |
| 562 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 563 | // If the PCH file contains __DATE__ or __TIME__ emit a warning about this. |
| 564 | // FIXME: use diagnostics subsystem for localization etc. |
| 565 | if (PP.SawDateOrTime()) |
| 566 | fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 568 | RecordData Record; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 569 | |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 570 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 571 | if (PP.getCounterValue() != 0) { |
| 572 | Record.push_back(PP.getCounterValue()); |
| 573 | S.EmitRecord(pch::PP_COUNTER_VALUE, Record); |
| 574 | Record.clear(); |
| 575 | } |
| 576 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 577 | // Loop over all the macro definitions that are live at the end of the file, |
| 578 | // emitting each to the PP section. |
| 579 | // FIXME: Eventually we want to emit an index so that we can lazily load |
| 580 | // macros. |
| 581 | for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); |
| 582 | I != E; ++I) { |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 583 | // FIXME: This emits macros in hash table order, we should do it in a stable |
| 584 | // order so that output is reproducible. |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 585 | MacroInfo *MI = I->second; |
| 586 | |
| 587 | // Don't emit builtin macros like __LINE__ to the PCH file unless they have |
| 588 | // been redefined by the header (in which case they are not isBuiltinMacro). |
| 589 | if (MI->isBuiltinMacro()) |
| 590 | continue; |
| 591 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 592 | AddIdentifierRef(I->first, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 593 | Record.push_back(MI->getDefinitionLoc().getRawEncoding()); |
| 594 | Record.push_back(MI->isUsed()); |
| 595 | |
| 596 | unsigned Code; |
| 597 | if (MI->isObjectLike()) { |
| 598 | Code = pch::PP_MACRO_OBJECT_LIKE; |
| 599 | } else { |
| 600 | Code = pch::PP_MACRO_FUNCTION_LIKE; |
| 601 | |
| 602 | Record.push_back(MI->isC99Varargs()); |
| 603 | Record.push_back(MI->isGNUVarargs()); |
| 604 | Record.push_back(MI->getNumArgs()); |
| 605 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 606 | I != E; ++I) |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 607 | AddIdentifierRef(*I, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 608 | } |
| 609 | S.EmitRecord(Code, Record); |
| 610 | Record.clear(); |
| 611 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 612 | // Emit the tokens array. |
| 613 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 614 | // Note that we know that the preprocessor does not have any annotation |
| 615 | // tokens in it because they are created by the parser, and thus can't be |
| 616 | // in a macro definition. |
| 617 | const Token &Tok = MI->getReplacementToken(TokNo); |
| 618 | |
| 619 | Record.push_back(Tok.getLocation().getRawEncoding()); |
| 620 | Record.push_back(Tok.getLength()); |
| 621 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 622 | // FIXME: When reading literal tokens, reconstruct the literal pointer if |
| 623 | // it is needed. |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 624 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 625 | |
| 626 | // FIXME: Should translate token kind to a stable encoding. |
| 627 | Record.push_back(Tok.getKind()); |
| 628 | // FIXME: Should translate token flags to a stable encoding. |
| 629 | Record.push_back(Tok.getFlags()); |
| 630 | |
| 631 | S.EmitRecord(pch::PP_TOKEN, Record); |
| 632 | Record.clear(); |
| 633 | } |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 634 | |
| 635 | } |
| 636 | |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 637 | S.ExitBlock(); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 641 | /// \brief Write the representation of a type to the PCH stream. |
| 642 | void PCHWriter::WriteType(const Type *T) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 643 | pch::TypeID &ID = TypeIDs[T]; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 644 | if (ID == 0) // we haven't seen this type before. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 645 | ID = NextTypeID++; |
| 646 | |
| 647 | // Record the offset for this type. |
| 648 | if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS) |
| 649 | TypeOffsets.push_back(S.GetCurrentBitNo()); |
| 650 | else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) { |
| 651 | TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS); |
| 652 | TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo(); |
| 653 | } |
| 654 | |
| 655 | RecordData Record; |
| 656 | |
| 657 | // Emit the type's representation. |
| 658 | PCHTypeWriter W(*this, Record); |
| 659 | switch (T->getTypeClass()) { |
| 660 | // For all of the concrete, non-dependent types, call the |
| 661 | // appropriate visitor function. |
| 662 | #define TYPE(Class, Base) \ |
| 663 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
| 664 | #define ABSTRACT_TYPE(Class, Base) |
| 665 | #define DEPENDENT_TYPE(Class, Base) |
| 666 | #include "clang/AST/TypeNodes.def" |
| 667 | |
| 668 | // For all of the dependent type nodes (which only occur in C++ |
| 669 | // templates), produce an error. |
| 670 | #define TYPE(Class, Base) |
| 671 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 672 | #include "clang/AST/TypeNodes.def" |
| 673 | assert(false && "Cannot serialize dependent type nodes"); |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | // Emit the serialized record. |
| 678 | S.EmitRecord(W.Code, Record); |
| 679 | } |
| 680 | |
| 681 | /// \brief Write a block containing all of the types. |
| 682 | void PCHWriter::WriteTypesBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 683 | // Enter the types block. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 684 | S.EnterSubblock(pch::TYPES_BLOCK_ID, 2); |
| 685 | |
| 686 | // Emit all of the types in the ASTContext |
| 687 | for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(), |
| 688 | TEnd = Context.getTypes().end(); |
| 689 | T != TEnd; ++T) { |
| 690 | // Builtin types are never serialized. |
| 691 | if (isa<BuiltinType>(*T)) |
| 692 | continue; |
| 693 | |
| 694 | WriteType(*T); |
| 695 | } |
| 696 | |
| 697 | // Exit the types block |
| 698 | S.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /// \brief Write the block containing all of the declaration IDs |
| 702 | /// lexically declared within the given DeclContext. |
| 703 | /// |
| 704 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 705 | /// bistream, or 0 if no block was written. |
| 706 | uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
| 707 | DeclContext *DC) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 708 | if (DC->decls_empty(Context)) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 709 | return 0; |
| 710 | |
| 711 | uint64_t Offset = S.GetCurrentBitNo(); |
| 712 | RecordData Record; |
| 713 | for (DeclContext::decl_iterator D = DC->decls_begin(Context), |
| 714 | DEnd = DC->decls_end(Context); |
| 715 | D != DEnd; ++D) |
| 716 | AddDeclRef(*D, Record); |
| 717 | |
| 718 | S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record); |
| 719 | return Offset; |
| 720 | } |
| 721 | |
| 722 | /// \brief Write the block containing all of the declaration IDs |
| 723 | /// visible from the given DeclContext. |
| 724 | /// |
| 725 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
| 726 | /// bistream, or 0 if no block was written. |
| 727 | uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 728 | DeclContext *DC) { |
| 729 | if (DC->getPrimaryContext() != DC) |
| 730 | return 0; |
| 731 | |
| 732 | // Force the DeclContext to build a its name-lookup table. |
| 733 | DC->lookup(Context, DeclarationName()); |
| 734 | |
| 735 | // Serialize the contents of the mapping used for lookup. Note that, |
| 736 | // although we have two very different code paths, the serialized |
| 737 | // representation is the same for both cases: a declaration name, |
| 738 | // followed by a size, followed by references to the visible |
| 739 | // declarations that have that name. |
| 740 | uint64_t Offset = S.GetCurrentBitNo(); |
| 741 | RecordData Record; |
| 742 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
| 743 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 744 | D != DEnd; ++D) { |
| 745 | AddDeclarationName(D->first, Record); |
| 746 | DeclContext::lookup_result Result = D->second.getLookupResult(Context); |
| 747 | Record.push_back(Result.second - Result.first); |
| 748 | for(; Result.first != Result.second; ++Result.first) |
| 749 | AddDeclRef(*Result.first, Record); |
| 750 | } |
| 751 | |
| 752 | if (Record.size() == 0) |
| 753 | return 0; |
| 754 | |
| 755 | S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record); |
| 756 | return Offset; |
| 757 | } |
| 758 | |
| 759 | /// \brief Write a block containing all of the declarations. |
| 760 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 761 | // Enter the declarations block. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 762 | S.EnterSubblock(pch::DECLS_BLOCK_ID, 2); |
| 763 | |
| 764 | // Emit all of the declarations. |
| 765 | RecordData Record; |
| 766 | PCHDeclWriter W(*this, Record); |
| 767 | while (!DeclsToEmit.empty()) { |
| 768 | // Pull the next declaration off the queue |
| 769 | Decl *D = DeclsToEmit.front(); |
| 770 | DeclsToEmit.pop(); |
| 771 | |
| 772 | // If this declaration is also a DeclContext, write blocks for the |
| 773 | // declarations that lexically stored inside its context and those |
| 774 | // declarations that are visible from its context. These blocks |
| 775 | // are written before the declaration itself so that we can put |
| 776 | // their offsets into the record for the declaration. |
| 777 | uint64_t LexicalOffset = 0; |
| 778 | uint64_t VisibleOffset = 0; |
| 779 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 780 | if (DC) { |
| 781 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 782 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 783 | } |
| 784 | |
| 785 | // Determine the ID for this declaration |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 786 | pch::DeclID ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 787 | if (ID == 0) |
| 788 | ID = DeclIDs.size(); |
| 789 | |
| 790 | unsigned Index = ID - 1; |
| 791 | |
| 792 | // Record the offset for this declaration |
| 793 | if (DeclOffsets.size() == Index) |
| 794 | DeclOffsets.push_back(S.GetCurrentBitNo()); |
| 795 | else if (DeclOffsets.size() < Index) { |
| 796 | DeclOffsets.resize(Index+1); |
| 797 | DeclOffsets[Index] = S.GetCurrentBitNo(); |
| 798 | } |
| 799 | |
| 800 | // Build and emit a record for this declaration |
| 801 | Record.clear(); |
| 802 | W.Code = (pch::DeclCode)0; |
| 803 | W.Visit(D); |
| 804 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
| 805 | assert(W.Code && "Visitor did not set record code"); |
| 806 | S.EmitRecord(W.Code, Record); |
| 807 | } |
| 808 | |
| 809 | // Exit the declarations block |
| 810 | S.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 813 | /// \brief Write the identifier table into the PCH file. |
| 814 | /// |
| 815 | /// The identifier table consists of a blob containing string data |
| 816 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 817 | /// that maps identifier IDs to locations within the blob. |
| 818 | void PCHWriter::WriteIdentifierTable() { |
| 819 | using namespace llvm; |
| 820 | |
| 821 | // Create and write out the blob that contains the identifier |
| 822 | // strings. |
| 823 | RecordData IdentOffsets; |
| 824 | IdentOffsets.resize(IdentifierIDs.size()); |
| 825 | { |
| 826 | // Create the identifier string data. |
| 827 | std::vector<char> Data; |
| 828 | Data.push_back(0); // Data must not be empty. |
| 829 | for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator |
| 830 | ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); |
| 831 | ID != IDEnd; ++ID) { |
| 832 | assert(ID->first && "NULL identifier in identifier table"); |
| 833 | |
| 834 | // Make sure we're starting on an odd byte. The PCH reader |
| 835 | // expects the low bit to be set on all of the offsets. |
| 836 | if ((Data.size() & 0x01) == 0) |
| 837 | Data.push_back((char)0); |
| 838 | |
| 839 | IdentOffsets[ID->second - 1] = Data.size(); |
| 840 | Data.insert(Data.end(), |
| 841 | ID->first->getName(), |
| 842 | ID->first->getName() + ID->first->getLength()); |
| 843 | Data.push_back((char)0); |
| 844 | } |
| 845 | |
| 846 | // Create a blob abbreviation |
| 847 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 848 | Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE)); |
| 849 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
| 850 | unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev); |
| 851 | |
| 852 | // Write the identifier table |
| 853 | RecordData Record; |
| 854 | Record.push_back(pch::IDENTIFIER_TABLE); |
| 855 | S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size()); |
| 856 | } |
| 857 | |
| 858 | // Write the offsets table for identifier IDs. |
| 859 | S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets); |
| 860 | } |
| 861 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 862 | PCHWriter::PCHWriter(llvm::BitstreamWriter &S) |
| 863 | : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { } |
| 864 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 865 | void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 866 | // Emit the file header. |
| 867 | S.Emit((unsigned)'C', 8); |
| 868 | S.Emit((unsigned)'P', 8); |
| 869 | S.Emit((unsigned)'C', 8); |
| 870 | S.Emit((unsigned)'H', 8); |
| 871 | |
| 872 | // The translation unit is the first declaration we'll emit. |
| 873 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
| 874 | DeclsToEmit.push(Context.getTranslationUnitDecl()); |
| 875 | |
| 876 | // Write the remaining PCH contents. |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 877 | S.EnterSubblock(pch::PCH_BLOCK_ID, 3); |
| 878 | WriteTargetTriple(Context.Target); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 879 | WriteLanguageOptions(Context.getLangOptions()); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 880 | WriteSourceManagerBlock(Context.getSourceManager()); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 881 | WritePreprocessor(PP); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 882 | WriteTypesBlock(Context); |
| 883 | WriteDeclsBlock(Context); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 884 | S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets); |
| 885 | S.EmitRecord(pch::DECL_OFFSET, DeclOffsets); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 886 | WriteIdentifierTable(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 887 | S.ExitBlock(); |
| 888 | } |
| 889 | |
| 890 | void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { |
| 891 | Record.push_back(Loc.getRawEncoding()); |
| 892 | } |
| 893 | |
| 894 | void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { |
| 895 | Record.push_back(Value.getBitWidth()); |
| 896 | unsigned N = Value.getNumWords(); |
| 897 | const uint64_t* Words = Value.getRawData(); |
| 898 | for (unsigned I = 0; I != N; ++I) |
| 899 | Record.push_back(Words[I]); |
| 900 | } |
| 901 | |
| 902 | void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 903 | if (II == 0) { |
| 904 | Record.push_back(0); |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | pch::IdentID &ID = IdentifierIDs[II]; |
| 909 | if (ID == 0) |
| 910 | ID = IdentifierIDs.size(); |
| 911 | |
| 912 | Record.push_back(ID); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void PCHWriter::AddTypeRef(QualType T, RecordData &Record) { |
| 916 | if (T.isNull()) { |
| 917 | Record.push_back(pch::PREDEF_TYPE_NULL_ID); |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 922 | pch::TypeID ID = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 923 | switch (BT->getKind()) { |
| 924 | case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break; |
| 925 | case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break; |
| 926 | case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break; |
| 927 | case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break; |
| 928 | case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break; |
| 929 | case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break; |
| 930 | case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break; |
| 931 | case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break; |
| 932 | case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break; |
| 933 | case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break; |
| 934 | case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break; |
| 935 | case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break; |
| 936 | case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break; |
| 937 | case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break; |
| 938 | case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break; |
| 939 | case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break; |
| 940 | case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break; |
| 941 | case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break; |
| 942 | case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break; |
| 943 | case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break; |
| 944 | } |
| 945 | |
| 946 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 947 | return; |
| 948 | } |
| 949 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 950 | pch::TypeID &ID = TypeIDs[T.getTypePtr()]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 951 | if (ID == 0) // we haven't seen this type before |
| 952 | ID = NextTypeID++; |
| 953 | |
| 954 | // Encode the type qualifiers in the type reference. |
| 955 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 956 | } |
| 957 | |
| 958 | void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) { |
| 959 | if (D == 0) { |
| 960 | Record.push_back(0); |
| 961 | return; |
| 962 | } |
| 963 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 964 | pch::DeclID &ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 965 | if (ID == 0) { |
| 966 | // We haven't seen this declaration before. Give it a new ID and |
| 967 | // enqueue it in the list of declarations to emit. |
| 968 | ID = DeclIDs.size(); |
| 969 | DeclsToEmit.push(const_cast<Decl *>(D)); |
| 970 | } |
| 971 | |
| 972 | Record.push_back(ID); |
| 973 | } |
| 974 | |
| 975 | void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { |
| 976 | Record.push_back(Name.getNameKind()); |
| 977 | switch (Name.getNameKind()) { |
| 978 | case DeclarationName::Identifier: |
| 979 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 980 | break; |
| 981 | |
| 982 | case DeclarationName::ObjCZeroArgSelector: |
| 983 | case DeclarationName::ObjCOneArgSelector: |
| 984 | case DeclarationName::ObjCMultiArgSelector: |
| 985 | assert(false && "Serialization of Objective-C selectors unavailable"); |
| 986 | break; |
| 987 | |
| 988 | case DeclarationName::CXXConstructorName: |
| 989 | case DeclarationName::CXXDestructorName: |
| 990 | case DeclarationName::CXXConversionFunctionName: |
| 991 | AddTypeRef(Name.getCXXNameType(), Record); |
| 992 | break; |
| 993 | |
| 994 | case DeclarationName::CXXOperatorName: |
| 995 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 996 | break; |
| 997 | |
| 998 | case DeclarationName::CXXUsingDirective: |
| 999 | // No extra data to emit |
| 1000 | break; |
| 1001 | } |
| 1002 | } |