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" |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 22 | #include "clang/Lex/MacroInfo.h" |
| 23 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 24 | #include "clang/Basic/FileManager.h" |
| 25 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/APFloat.h" |
| 29 | #include "llvm/ADT/APInt.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 30 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 31 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 3c304bd | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 33 | #include <cstdio> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Type serialization |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | namespace { |
| 40 | class VISIBILITY_HIDDEN PCHTypeWriter { |
| 41 | PCHWriter &Writer; |
| 42 | PCHWriter::RecordData &Record; |
| 43 | |
| 44 | public: |
| 45 | /// \brief Type code that corresponds to the record generated. |
| 46 | pch::TypeCode Code; |
| 47 | |
| 48 | PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 49 | : Writer(Writer), Record(Record) { } |
| 50 | |
| 51 | void VisitArrayType(const ArrayType *T); |
| 52 | void VisitFunctionType(const FunctionType *T); |
| 53 | void VisitTagType(const TagType *T); |
| 54 | |
| 55 | #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); |
| 56 | #define ABSTRACT_TYPE(Class, Base) |
| 57 | #define DEPENDENT_TYPE(Class, Base) |
| 58 | #include "clang/AST/TypeNodes.def" |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) { |
| 63 | Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record); |
| 64 | Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values |
| 65 | Record.push_back(T->getAddressSpace()); |
| 66 | Code = pch::TYPE_EXT_QUAL; |
| 67 | } |
| 68 | |
| 69 | void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) { |
| 70 | assert(false && "Built-in types are never serialized"); |
| 71 | } |
| 72 | |
| 73 | void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) { |
| 74 | Record.push_back(T->getWidth()); |
| 75 | Record.push_back(T->isSigned()); |
| 76 | Code = pch::TYPE_FIXED_WIDTH_INT; |
| 77 | } |
| 78 | |
| 79 | void PCHTypeWriter::VisitComplexType(const ComplexType *T) { |
| 80 | Writer.AddTypeRef(T->getElementType(), Record); |
| 81 | Code = pch::TYPE_COMPLEX; |
| 82 | } |
| 83 | |
| 84 | void PCHTypeWriter::VisitPointerType(const PointerType *T) { |
| 85 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 86 | Code = pch::TYPE_POINTER; |
| 87 | } |
| 88 | |
| 89 | void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { |
| 90 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 91 | Code = pch::TYPE_BLOCK_POINTER; |
| 92 | } |
| 93 | |
| 94 | void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { |
| 95 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 96 | Code = pch::TYPE_LVALUE_REFERENCE; |
| 97 | } |
| 98 | |
| 99 | void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { |
| 100 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 101 | Code = pch::TYPE_RVALUE_REFERENCE; |
| 102 | } |
| 103 | |
| 104 | void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { |
| 105 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 106 | Writer.AddTypeRef(QualType(T->getClass(), 0), Record); |
| 107 | Code = pch::TYPE_MEMBER_POINTER; |
| 108 | } |
| 109 | |
| 110 | void PCHTypeWriter::VisitArrayType(const ArrayType *T) { |
| 111 | Writer.AddTypeRef(T->getElementType(), Record); |
| 112 | Record.push_back(T->getSizeModifier()); // FIXME: stable values |
| 113 | Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values |
| 114 | } |
| 115 | |
| 116 | void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { |
| 117 | VisitArrayType(T); |
| 118 | Writer.AddAPInt(T->getSize(), Record); |
| 119 | Code = pch::TYPE_CONSTANT_ARRAY; |
| 120 | } |
| 121 | |
| 122 | void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 123 | VisitArrayType(T); |
| 124 | Code = pch::TYPE_INCOMPLETE_ARRAY; |
| 125 | } |
| 126 | |
| 127 | void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { |
| 128 | VisitArrayType(T); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 129 | Writer.AddExpr(T->getSizeExpr()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 130 | Code = pch::TYPE_VARIABLE_ARRAY; |
| 131 | } |
| 132 | |
| 133 | void PCHTypeWriter::VisitVectorType(const VectorType *T) { |
| 134 | Writer.AddTypeRef(T->getElementType(), Record); |
| 135 | Record.push_back(T->getNumElements()); |
| 136 | Code = pch::TYPE_VECTOR; |
| 137 | } |
| 138 | |
| 139 | void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) { |
| 140 | VisitVectorType(T); |
| 141 | Code = pch::TYPE_EXT_VECTOR; |
| 142 | } |
| 143 | |
| 144 | void PCHTypeWriter::VisitFunctionType(const FunctionType *T) { |
| 145 | Writer.AddTypeRef(T->getResultType(), Record); |
| 146 | } |
| 147 | |
| 148 | void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 149 | VisitFunctionType(T); |
| 150 | Code = pch::TYPE_FUNCTION_NO_PROTO; |
| 151 | } |
| 152 | |
| 153 | void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| 154 | VisitFunctionType(T); |
| 155 | Record.push_back(T->getNumArgs()); |
| 156 | for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) |
| 157 | Writer.AddTypeRef(T->getArgType(I), Record); |
| 158 | Record.push_back(T->isVariadic()); |
| 159 | Record.push_back(T->getTypeQuals()); |
| 160 | Code = pch::TYPE_FUNCTION_PROTO; |
| 161 | } |
| 162 | |
| 163 | void PCHTypeWriter::VisitTypedefType(const TypedefType *T) { |
| 164 | Writer.AddDeclRef(T->getDecl(), Record); |
| 165 | Code = pch::TYPE_TYPEDEF; |
| 166 | } |
| 167 | |
| 168 | void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 169 | Writer.AddExpr(T->getUnderlyingExpr()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 170 | Code = pch::TYPE_TYPEOF_EXPR; |
| 171 | } |
| 172 | |
| 173 | void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) { |
| 174 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
| 175 | Code = pch::TYPE_TYPEOF; |
| 176 | } |
| 177 | |
| 178 | void PCHTypeWriter::VisitTagType(const TagType *T) { |
| 179 | Writer.AddDeclRef(T->getDecl(), Record); |
| 180 | assert(!T->isBeingDefined() && |
| 181 | "Cannot serialize in the middle of a type definition"); |
| 182 | } |
| 183 | |
| 184 | void PCHTypeWriter::VisitRecordType(const RecordType *T) { |
| 185 | VisitTagType(T); |
| 186 | Code = pch::TYPE_RECORD; |
| 187 | } |
| 188 | |
| 189 | void PCHTypeWriter::VisitEnumType(const EnumType *T) { |
| 190 | VisitTagType(T); |
| 191 | Code = pch::TYPE_ENUM; |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | PCHTypeWriter::VisitTemplateSpecializationType( |
| 196 | const TemplateSpecializationType *T) { |
| 197 | // FIXME: Serialize this type |
| 198 | assert(false && "Cannot serialize template specialization types"); |
| 199 | } |
| 200 | |
| 201 | void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) { |
| 202 | // FIXME: Serialize this type |
| 203 | assert(false && "Cannot serialize qualified name types"); |
| 204 | } |
| 205 | |
| 206 | void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
| 207 | Writer.AddDeclRef(T->getDecl(), Record); |
| 208 | Code = pch::TYPE_OBJC_INTERFACE; |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | PCHTypeWriter::VisitObjCQualifiedInterfaceType( |
| 213 | const ObjCQualifiedInterfaceType *T) { |
| 214 | VisitObjCInterfaceType(T); |
| 215 | Record.push_back(T->getNumProtocols()); |
| 216 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 217 | Writer.AddDeclRef(T->getProtocol(I), Record); |
| 218 | Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE; |
| 219 | } |
| 220 | |
| 221 | void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) { |
| 222 | Record.push_back(T->getNumProtocols()); |
| 223 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 224 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 225 | Code = pch::TYPE_OBJC_QUALIFIED_ID; |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) { |
| 230 | Record.push_back(T->getNumProtocols()); |
| 231 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 232 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 233 | Code = pch::TYPE_OBJC_QUALIFIED_CLASS; |
| 234 | } |
| 235 | |
| 236 | //===----------------------------------------------------------------------===// |
| 237 | // Declaration serialization |
| 238 | //===----------------------------------------------------------------------===// |
| 239 | namespace { |
| 240 | class VISIBILITY_HIDDEN PCHDeclWriter |
| 241 | : public DeclVisitor<PCHDeclWriter, void> { |
| 242 | |
| 243 | PCHWriter &Writer; |
| 244 | PCHWriter::RecordData &Record; |
| 245 | |
| 246 | public: |
| 247 | pch::DeclCode Code; |
| 248 | |
| 249 | PCHDeclWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 250 | : Writer(Writer), Record(Record) { } |
| 251 | |
| 252 | void VisitDecl(Decl *D); |
| 253 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 254 | void VisitNamedDecl(NamedDecl *D); |
| 255 | void VisitTypeDecl(TypeDecl *D); |
| 256 | void VisitTypedefDecl(TypedefDecl *D); |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 257 | void VisitTagDecl(TagDecl *D); |
| 258 | void VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 259 | void VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 260 | void VisitValueDecl(ValueDecl *D); |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 261 | void VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | 3a2f7e4 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 262 | void VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 263 | void VisitFieldDecl(FieldDecl *D); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 264 | void VisitVarDecl(VarDecl *D); |
Douglas Gregor | 3a2f7e4 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 265 | void VisitParmVarDecl(ParmVarDecl *D); |
| 266 | void VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
Douglas Gregor | 1028bc6 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 267 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
| 268 | void VisitBlockDecl(BlockDecl *D); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 269 | void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 270 | uint64_t VisibleOffset); |
| 271 | }; |
| 272 | } |
| 273 | |
| 274 | void PCHDeclWriter::VisitDecl(Decl *D) { |
| 275 | Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); |
| 276 | Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); |
| 277 | Writer.AddSourceLocation(D->getLocation(), Record); |
| 278 | Record.push_back(D->isInvalidDecl()); |
| 279 | // FIXME: hasAttrs |
| 280 | Record.push_back(D->isImplicit()); |
| 281 | Record.push_back(D->getAccess()); |
| 282 | } |
| 283 | |
| 284 | void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 285 | VisitDecl(D); |
| 286 | Code = pch::DECL_TRANSLATION_UNIT; |
| 287 | } |
| 288 | |
| 289 | void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) { |
| 290 | VisitDecl(D); |
| 291 | Writer.AddDeclarationName(D->getDeclName(), Record); |
| 292 | } |
| 293 | |
| 294 | void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) { |
| 295 | VisitNamedDecl(D); |
| 296 | Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); |
| 297 | } |
| 298 | |
| 299 | void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { |
| 300 | VisitTypeDecl(D); |
| 301 | Writer.AddTypeRef(D->getUnderlyingType(), Record); |
| 302 | Code = pch::DECL_TYPEDEF; |
| 303 | } |
| 304 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 305 | void PCHDeclWriter::VisitTagDecl(TagDecl *D) { |
| 306 | VisitTypeDecl(D); |
| 307 | Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding |
| 308 | Record.push_back(D->isDefinition()); |
| 309 | Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record); |
| 310 | } |
| 311 | |
| 312 | void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) { |
| 313 | VisitTagDecl(D); |
| 314 | Writer.AddTypeRef(D->getIntegerType(), Record); |
| 315 | Code = pch::DECL_ENUM; |
| 316 | } |
| 317 | |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 318 | void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) { |
| 319 | VisitTagDecl(D); |
| 320 | Record.push_back(D->hasFlexibleArrayMember()); |
| 321 | Record.push_back(D->isAnonymousStructOrUnion()); |
| 322 | Code = pch::DECL_RECORD; |
| 323 | } |
| 324 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 325 | void PCHDeclWriter::VisitValueDecl(ValueDecl *D) { |
| 326 | VisitNamedDecl(D); |
| 327 | Writer.AddTypeRef(D->getType(), Record); |
| 328 | } |
| 329 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 330 | void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 331 | VisitValueDecl(D); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 332 | Record.push_back(D->getInitExpr()? 1 : 0); |
| 333 | if (D->getInitExpr()) |
| 334 | Writer.AddExpr(D->getInitExpr()); |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 335 | Writer.AddAPSInt(D->getInitVal(), Record); |
| 336 | Code = pch::DECL_ENUM_CONSTANT; |
| 337 | } |
| 338 | |
Douglas Gregor | 3a2f7e4 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 339 | void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) { |
| 340 | VisitValueDecl(D); |
| 341 | // FIXME: function body |
| 342 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 343 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
| 344 | Record.push_back(D->isInline()); |
| 345 | Record.push_back(D->isVirtual()); |
| 346 | Record.push_back(D->isPure()); |
| 347 | Record.push_back(D->inheritedPrototype()); |
| 348 | Record.push_back(D->hasPrototype() && !D->inheritedPrototype()); |
| 349 | Record.push_back(D->isDeleted()); |
| 350 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
| 351 | Record.push_back(D->param_size()); |
| 352 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 353 | P != PEnd; ++P) |
| 354 | Writer.AddDeclRef(*P, Record); |
| 355 | Code = pch::DECL_FUNCTION; |
| 356 | } |
| 357 | |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 358 | void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) { |
| 359 | VisitValueDecl(D); |
| 360 | Record.push_back(D->isMutable()); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 361 | Record.push_back(D->getBitWidth()? 1 : 0); |
| 362 | if (D->getBitWidth()) |
| 363 | Writer.AddExpr(D->getBitWidth()); |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 364 | Code = pch::DECL_FIELD; |
| 365 | } |
| 366 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 367 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
| 368 | VisitValueDecl(D); |
Douglas Gregor | 3a2f7e4 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 369 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 370 | Record.push_back(D->isThreadSpecified()); |
| 371 | Record.push_back(D->hasCXXDirectInitializer()); |
| 372 | Record.push_back(D->isDeclaredInCondition()); |
| 373 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 374 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 375 | Record.push_back(D->getInit()? 1 : 0); |
| 376 | if (D->getInit()) |
| 377 | Writer.AddExpr(D->getInit()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 378 | Code = pch::DECL_VAR; |
| 379 | } |
| 380 | |
Douglas Gregor | 3a2f7e4 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 381 | void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { |
| 382 | VisitVarDecl(D); |
| 383 | Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding |
| 384 | // FIXME: emit default argument |
| 385 | // FIXME: why isn't the "default argument" just stored as the initializer |
| 386 | // in VarDecl? |
| 387 | Code = pch::DECL_PARM_VAR; |
| 388 | } |
| 389 | |
| 390 | void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 391 | VisitParmVarDecl(D); |
| 392 | Writer.AddTypeRef(D->getOriginalType(), Record); |
| 393 | Code = pch::DECL_ORIGINAL_PARM_VAR; |
| 394 | } |
| 395 | |
Douglas Gregor | 1028bc6 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 396 | void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 397 | VisitDecl(D); |
| 398 | // FIXME: Emit the string literal |
| 399 | Code = pch::DECL_FILE_SCOPE_ASM; |
| 400 | } |
| 401 | |
| 402 | void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) { |
| 403 | VisitDecl(D); |
| 404 | // FIXME: emit block body |
| 405 | Record.push_back(D->param_size()); |
| 406 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 407 | P != PEnd; ++P) |
| 408 | Writer.AddDeclRef(*P, Record); |
| 409 | Code = pch::DECL_BLOCK; |
| 410 | } |
| 411 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 412 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 413 | /// |
| 414 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 415 | /// block for this declaration context is stored. May be 0 to indicate |
| 416 | /// that there are no declarations stored within this context. |
| 417 | /// |
| 418 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 419 | /// block for this declaration context is stored. May be 0 to indicate |
| 420 | /// that there are no declarations visible from this context. Note |
| 421 | /// that this value will not be emitted for non-primary declaration |
| 422 | /// contexts. |
| 423 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 424 | uint64_t VisibleOffset) { |
| 425 | Record.push_back(LexicalOffset); |
| 426 | if (DC->getPrimaryContext() == DC) |
| 427 | Record.push_back(VisibleOffset); |
| 428 | } |
| 429 | |
| 430 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 431 | // Statement/expression serialization |
| 432 | //===----------------------------------------------------------------------===// |
| 433 | namespace { |
| 434 | class VISIBILITY_HIDDEN PCHStmtWriter |
| 435 | : public StmtVisitor<PCHStmtWriter, void> { |
| 436 | |
| 437 | PCHWriter &Writer; |
| 438 | PCHWriter::RecordData &Record; |
| 439 | |
| 440 | public: |
| 441 | pch::StmtCode Code; |
| 442 | |
| 443 | PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 444 | : Writer(Writer), Record(Record) { } |
| 445 | |
| 446 | void VisitExpr(Expr *E); |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 447 | void VisitPredefinedExpr(PredefinedExpr *E); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 448 | void VisitDeclRefExpr(DeclRefExpr *E); |
| 449 | void VisitIntegerLiteral(IntegerLiteral *E); |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 450 | void VisitFloatingLiteral(FloatingLiteral *E); |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame^] | 451 | void VisitStringLiteral(StringLiteral *E); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 452 | void VisitCharacterLiteral(CharacterLiteral *E); |
Douglas Gregor | c04db4f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 453 | void VisitParenExpr(ParenExpr *E); |
Douglas Gregor | 0b0b77f | 2009-04-15 15:58:59 +0000 | [diff] [blame] | 454 | void VisitUnaryOperator(UnaryOperator *E); |
| 455 | void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 456 | void VisitCastExpr(CastExpr *E); |
Douglas Gregor | db600c3 | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 457 | void VisitBinaryOperator(BinaryOperator *E); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 458 | void VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | db600c3 | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 459 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 460 | void VisitCStyleCastExpr(CStyleCastExpr *E); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 461 | }; |
| 462 | } |
| 463 | |
| 464 | void PCHStmtWriter::VisitExpr(Expr *E) { |
| 465 | Writer.AddTypeRef(E->getType(), Record); |
| 466 | Record.push_back(E->isTypeDependent()); |
| 467 | Record.push_back(E->isValueDependent()); |
| 468 | } |
| 469 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 470 | void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) { |
| 471 | VisitExpr(E); |
| 472 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 473 | Record.push_back(E->getIdentType()); // FIXME: stable encoding |
| 474 | Code = pch::EXPR_PREDEFINED; |
| 475 | } |
| 476 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 477 | void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) { |
| 478 | VisitExpr(E); |
| 479 | Writer.AddDeclRef(E->getDecl(), Record); |
| 480 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 481 | Code = pch::EXPR_DECL_REF; |
| 482 | } |
| 483 | |
| 484 | void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 485 | VisitExpr(E); |
| 486 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 487 | Writer.AddAPInt(E->getValue(), Record); |
| 488 | Code = pch::EXPR_INTEGER_LITERAL; |
| 489 | } |
| 490 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 491 | void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) { |
| 492 | VisitExpr(E); |
| 493 | Writer.AddAPFloat(E->getValue(), Record); |
| 494 | Record.push_back(E->isExact()); |
| 495 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 496 | Code = pch::EXPR_FLOATING_LITERAL; |
| 497 | } |
| 498 | |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame^] | 499 | void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) { |
| 500 | VisitExpr(E); |
| 501 | Record.push_back(E->getByteLength()); |
| 502 | Record.push_back(E->getNumConcatenated()); |
| 503 | Record.push_back(E->isWide()); |
| 504 | // FIXME: String data should be stored as a blob at the end of the |
| 505 | // StringLiteral. However, we can't do so now because we have no |
| 506 | // provision for coping with abbreviations when we're jumping around |
| 507 | // the PCH file during deserialization. |
| 508 | Record.insert(Record.end(), |
| 509 | E->getStrData(), E->getStrData() + E->getByteLength()); |
| 510 | for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I) |
| 511 | Writer.AddSourceLocation(E->getStrTokenLoc(I), Record); |
| 512 | Code = pch::EXPR_STRING_LITERAL; |
| 513 | } |
| 514 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 515 | void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 516 | VisitExpr(E); |
| 517 | Record.push_back(E->getValue()); |
| 518 | Writer.AddSourceLocation(E->getLoc(), Record); |
| 519 | Record.push_back(E->isWide()); |
| 520 | Code = pch::EXPR_CHARACTER_LITERAL; |
| 521 | } |
| 522 | |
Douglas Gregor | c04db4f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 523 | void PCHStmtWriter::VisitParenExpr(ParenExpr *E) { |
| 524 | VisitExpr(E); |
| 525 | Writer.AddSourceLocation(E->getLParen(), Record); |
| 526 | Writer.AddSourceLocation(E->getRParen(), Record); |
| 527 | Writer.WriteSubExpr(E->getSubExpr()); |
| 528 | Code = pch::EXPR_PAREN; |
| 529 | } |
| 530 | |
Douglas Gregor | 0b0b77f | 2009-04-15 15:58:59 +0000 | [diff] [blame] | 531 | void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) { |
| 532 | VisitExpr(E); |
| 533 | Writer.WriteSubExpr(E->getSubExpr()); |
| 534 | Record.push_back(E->getOpcode()); // FIXME: stable encoding |
| 535 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 536 | Code = pch::EXPR_UNARY_OPERATOR; |
| 537 | } |
| 538 | |
| 539 | void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { |
| 540 | VisitExpr(E); |
| 541 | Record.push_back(E->isSizeOf()); |
| 542 | if (E->isArgumentType()) |
| 543 | Writer.AddTypeRef(E->getArgumentType(), Record); |
| 544 | else { |
| 545 | Record.push_back(0); |
| 546 | Writer.WriteSubExpr(E->getArgumentExpr()); |
| 547 | } |
| 548 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 549 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 550 | Code = pch::EXPR_SIZEOF_ALIGN_OF; |
| 551 | } |
| 552 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 553 | void PCHStmtWriter::VisitCastExpr(CastExpr *E) { |
| 554 | VisitExpr(E); |
| 555 | Writer.WriteSubExpr(E->getSubExpr()); |
| 556 | } |
| 557 | |
Douglas Gregor | db600c3 | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 558 | void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) { |
| 559 | VisitExpr(E); |
| 560 | Writer.WriteSubExpr(E->getLHS()); |
| 561 | Writer.WriteSubExpr(E->getRHS()); |
| 562 | Record.push_back(E->getOpcode()); // FIXME: stable encoding |
| 563 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 564 | Code = pch::EXPR_BINARY_OPERATOR; |
| 565 | } |
| 566 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 567 | void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 568 | VisitCastExpr(E); |
| 569 | Record.push_back(E->isLvalueCast()); |
| 570 | Code = pch::EXPR_IMPLICIT_CAST; |
| 571 | } |
| 572 | |
Douglas Gregor | db600c3 | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 573 | void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 574 | VisitCastExpr(E); |
| 575 | Writer.AddTypeRef(E->getTypeAsWritten(), Record); |
| 576 | } |
| 577 | |
| 578 | void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 579 | VisitExplicitCastExpr(E); |
| 580 | Writer.AddSourceLocation(E->getLParenLoc(), Record); |
| 581 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 582 | Code = pch::EXPR_CSTYLE_CAST; |
| 583 | } |
| 584 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 585 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 586 | // PCHWriter Implementation |
| 587 | //===----------------------------------------------------------------------===// |
| 588 | |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 589 | /// \brief Write the target triple (e.g., i686-apple-darwin9). |
| 590 | void PCHWriter::WriteTargetTriple(const TargetInfo &Target) { |
| 591 | using namespace llvm; |
| 592 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 593 | Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE)); |
| 594 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
| 595 | unsigned TripleAbbrev = S.EmitAbbrev(Abbrev); |
| 596 | |
| 597 | RecordData Record; |
| 598 | Record.push_back(pch::TARGET_TRIPLE); |
| 599 | const char *Triple = Target.getTargetTriple(); |
| 600 | S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple)); |
| 601 | } |
| 602 | |
| 603 | /// \brief Write the LangOptions structure. |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 604 | void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { |
| 605 | RecordData Record; |
| 606 | Record.push_back(LangOpts.Trigraphs); |
| 607 | Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments. |
| 608 | Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers. |
| 609 | Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode. |
| 610 | Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc) |
| 611 | Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'. |
| 612 | Record.push_back(LangOpts.Digraphs); // C94, C99 and C++ |
| 613 | Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants. |
| 614 | Record.push_back(LangOpts.C99); // C99 Support |
| 615 | Record.push_back(LangOpts.Microsoft); // Microsoft extensions. |
| 616 | Record.push_back(LangOpts.CPlusPlus); // C++ Support |
| 617 | Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support |
| 618 | Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode. |
| 619 | Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords. |
| 620 | |
| 621 | Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled. |
| 622 | Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled. |
| 623 | Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled |
| 624 | |
| 625 | Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings |
| 626 | Record.push_back(LangOpts.Boolean); // Allow bool/true/false |
| 627 | Record.push_back(LangOpts.WritableStrings); // Allow writable strings |
| 628 | Record.push_back(LangOpts.LaxVectorConversions); |
| 629 | Record.push_back(LangOpts.Exceptions); // Support exception handling. |
| 630 | |
| 631 | Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime. |
| 632 | Record.push_back(LangOpts.Freestanding); // Freestanding implementation |
| 633 | Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin) |
| 634 | |
| 635 | Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected |
| 636 | // by locks. |
| 637 | Record.push_back(LangOpts.Blocks); // block extension to C |
| 638 | Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if |
| 639 | // they are unused. |
| 640 | Record.push_back(LangOpts.MathErrno); // Math functions must respect errno |
| 641 | // (modulo the platform support). |
| 642 | |
| 643 | Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when |
| 644 | // signed integer arithmetic overflows. |
| 645 | |
| 646 | Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and |
| 647 | // may be ripped out at any time. |
| 648 | |
| 649 | Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. |
| 650 | Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be |
| 651 | // defined. |
| 652 | Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as |
| 653 | // opposed to __DYNAMIC__). |
| 654 | Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero. |
| 655 | |
| 656 | Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be |
| 657 | // used (instead of C99 semantics). |
| 658 | Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined. |
| 659 | Record.push_back(LangOpts.getGCMode()); |
| 660 | Record.push_back(LangOpts.getVisibilityMode()); |
| 661 | Record.push_back(LangOpts.InstantiationDepth); |
| 662 | S.EmitRecord(pch::LANGUAGE_OPTIONS, Record); |
| 663 | } |
| 664 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 665 | //===----------------------------------------------------------------------===// |
| 666 | // Source Manager Serialization |
| 667 | //===----------------------------------------------------------------------===// |
| 668 | |
| 669 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 670 | /// file. |
| 671 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) { |
| 672 | using namespace llvm; |
| 673 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 674 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY)); |
| 675 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 676 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 677 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 678 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 679 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 680 | return S.EmitAbbrev(Abbrev); |
| 681 | } |
| 682 | |
| 683 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 684 | /// buffer. |
| 685 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) { |
| 686 | using namespace llvm; |
| 687 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 688 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY)); |
| 689 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 690 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 691 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 692 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 693 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
| 694 | return S.EmitAbbrev(Abbrev); |
| 695 | } |
| 696 | |
| 697 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 698 | /// buffer's blob. |
| 699 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) { |
| 700 | using namespace llvm; |
| 701 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 702 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB)); |
| 703 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
| 704 | return S.EmitAbbrev(Abbrev); |
| 705 | } |
| 706 | |
| 707 | /// \brief Create an abbreviation for the SLocEntry that refers to an |
| 708 | /// buffer. |
| 709 | static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) { |
| 710 | using namespace llvm; |
| 711 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 712 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY)); |
| 713 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 714 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 715 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 716 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
| 717 | return S.EmitAbbrev(Abbrev); |
| 718 | } |
| 719 | |
| 720 | /// \brief Writes the block containing the serialized form of the |
| 721 | /// source manager. |
| 722 | /// |
| 723 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 724 | /// blob), indexed based on the file name, so that we only create |
| 725 | /// entries for files that we actually need. In the common case (no |
| 726 | /// errors), we probably won't have to create file entries for any of |
| 727 | /// the files in the AST. |
| 728 | void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 729 | // Enter the source manager block. |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 730 | S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3); |
| 731 | |
| 732 | // Abbreviations for the various kinds of source-location entries. |
| 733 | int SLocFileAbbrv = -1; |
| 734 | int SLocBufferAbbrv = -1; |
| 735 | int SLocBufferBlobAbbrv = -1; |
| 736 | int SLocInstantiationAbbrv = -1; |
| 737 | |
| 738 | // Write out the source location entry table. We skip the first |
| 739 | // entry, which is always the same dummy entry. |
| 740 | RecordData Record; |
| 741 | for (SourceManager::sloc_entry_iterator |
| 742 | SLoc = SourceMgr.sloc_entry_begin() + 1, |
| 743 | SLocEnd = SourceMgr.sloc_entry_end(); |
| 744 | SLoc != SLocEnd; ++SLoc) { |
| 745 | // Figure out which record code to use. |
| 746 | unsigned Code; |
| 747 | if (SLoc->isFile()) { |
| 748 | if (SLoc->getFile().getContentCache()->Entry) |
| 749 | Code = pch::SM_SLOC_FILE_ENTRY; |
| 750 | else |
| 751 | Code = pch::SM_SLOC_BUFFER_ENTRY; |
| 752 | } else |
| 753 | Code = pch::SM_SLOC_INSTANTIATION_ENTRY; |
| 754 | Record.push_back(Code); |
| 755 | |
| 756 | Record.push_back(SLoc->getOffset()); |
| 757 | if (SLoc->isFile()) { |
| 758 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 759 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 760 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 761 | Record.push_back(File.hasLineDirectives()); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 762 | |
| 763 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
| 764 | if (Content->Entry) { |
| 765 | // The source location entry is a file. The blob associated |
| 766 | // with this entry is the file name. |
| 767 | if (SLocFileAbbrv == -1) |
| 768 | SLocFileAbbrv = CreateSLocFileAbbrev(S); |
| 769 | S.EmitRecordWithBlob(SLocFileAbbrv, Record, |
| 770 | Content->Entry->getName(), |
| 771 | strlen(Content->Entry->getName())); |
| 772 | } else { |
| 773 | // The source location entry is a buffer. The blob associated |
| 774 | // with this entry contains the contents of the buffer. |
| 775 | if (SLocBufferAbbrv == -1) { |
| 776 | SLocBufferAbbrv = CreateSLocBufferAbbrev(S); |
| 777 | SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S); |
| 778 | } |
| 779 | |
| 780 | // We add one to the size so that we capture the trailing NULL |
| 781 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 782 | // the reader side). |
| 783 | const llvm::MemoryBuffer *Buffer = Content->getBuffer(); |
| 784 | const char *Name = Buffer->getBufferIdentifier(); |
| 785 | S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1); |
| 786 | Record.clear(); |
| 787 | Record.push_back(pch::SM_SLOC_BUFFER_BLOB); |
| 788 | S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
| 789 | Buffer->getBufferStart(), |
| 790 | Buffer->getBufferSize() + 1); |
| 791 | } |
| 792 | } else { |
| 793 | // The source location entry is an instantiation. |
| 794 | const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); |
| 795 | Record.push_back(Inst.getSpellingLoc().getRawEncoding()); |
| 796 | Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); |
| 797 | Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); |
| 798 | |
| 799 | if (SLocInstantiationAbbrv == -1) |
| 800 | SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S); |
| 801 | S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); |
| 802 | } |
| 803 | |
| 804 | Record.clear(); |
| 805 | } |
| 806 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 807 | // Write the line table. |
| 808 | if (SourceMgr.hasLineTable()) { |
| 809 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 810 | |
| 811 | // Emit the file names |
| 812 | Record.push_back(LineTable.getNumFilenames()); |
| 813 | for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { |
| 814 | // Emit the file name |
| 815 | const char *Filename = LineTable.getFilename(I); |
| 816 | unsigned FilenameLen = Filename? strlen(Filename) : 0; |
| 817 | Record.push_back(FilenameLen); |
| 818 | if (FilenameLen) |
| 819 | Record.insert(Record.end(), Filename, Filename + FilenameLen); |
| 820 | } |
| 821 | |
| 822 | // Emit the line entries |
| 823 | for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); |
| 824 | L != LEnd; ++L) { |
| 825 | // Emit the file ID |
| 826 | Record.push_back(L->first); |
| 827 | |
| 828 | // Emit the line entries |
| 829 | Record.push_back(L->second.size()); |
| 830 | for (std::vector<LineEntry>::iterator LE = L->second.begin(), |
| 831 | LEEnd = L->second.end(); |
| 832 | LE != LEEnd; ++LE) { |
| 833 | Record.push_back(LE->FileOffset); |
| 834 | Record.push_back(LE->LineNo); |
| 835 | Record.push_back(LE->FilenameID); |
| 836 | Record.push_back((unsigned)LE->FileKind); |
| 837 | Record.push_back(LE->IncludeOffset); |
| 838 | } |
| 839 | S.EmitRecord(pch::SM_LINE_TABLE, Record); |
| 840 | } |
| 841 | } |
| 842 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 843 | S.ExitBlock(); |
| 844 | } |
| 845 | |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 846 | /// \brief Writes the block containing the serialized form of the |
| 847 | /// preprocessor. |
| 848 | /// |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 849 | void PCHWriter::WritePreprocessor(const Preprocessor &PP) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 850 | // Enter the preprocessor block. |
| 851 | S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3); |
| 852 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 853 | // If the PCH file contains __DATE__ or __TIME__ emit a warning about this. |
| 854 | // FIXME: use diagnostics subsystem for localization etc. |
| 855 | if (PP.SawDateOrTime()) |
| 856 | fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 858 | RecordData Record; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 859 | |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 860 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 861 | if (PP.getCounterValue() != 0) { |
| 862 | Record.push_back(PP.getCounterValue()); |
| 863 | S.EmitRecord(pch::PP_COUNTER_VALUE, Record); |
| 864 | Record.clear(); |
| 865 | } |
| 866 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 867 | // Loop over all the macro definitions that are live at the end of the file, |
| 868 | // emitting each to the PP section. |
| 869 | // FIXME: Eventually we want to emit an index so that we can lazily load |
| 870 | // macros. |
| 871 | for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); |
| 872 | I != E; ++I) { |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 873 | // FIXME: This emits macros in hash table order, we should do it in a stable |
| 874 | // order so that output is reproducible. |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 875 | MacroInfo *MI = I->second; |
| 876 | |
| 877 | // Don't emit builtin macros like __LINE__ to the PCH file unless they have |
| 878 | // been redefined by the header (in which case they are not isBuiltinMacro). |
| 879 | if (MI->isBuiltinMacro()) |
| 880 | continue; |
| 881 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 882 | AddIdentifierRef(I->first, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 883 | Record.push_back(MI->getDefinitionLoc().getRawEncoding()); |
| 884 | Record.push_back(MI->isUsed()); |
| 885 | |
| 886 | unsigned Code; |
| 887 | if (MI->isObjectLike()) { |
| 888 | Code = pch::PP_MACRO_OBJECT_LIKE; |
| 889 | } else { |
| 890 | Code = pch::PP_MACRO_FUNCTION_LIKE; |
| 891 | |
| 892 | Record.push_back(MI->isC99Varargs()); |
| 893 | Record.push_back(MI->isGNUVarargs()); |
| 894 | Record.push_back(MI->getNumArgs()); |
| 895 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 896 | I != E; ++I) |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 897 | AddIdentifierRef(*I, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 898 | } |
| 899 | S.EmitRecord(Code, Record); |
| 900 | Record.clear(); |
| 901 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 902 | // Emit the tokens array. |
| 903 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 904 | // Note that we know that the preprocessor does not have any annotation |
| 905 | // tokens in it because they are created by the parser, and thus can't be |
| 906 | // in a macro definition. |
| 907 | const Token &Tok = MI->getReplacementToken(TokNo); |
| 908 | |
| 909 | Record.push_back(Tok.getLocation().getRawEncoding()); |
| 910 | Record.push_back(Tok.getLength()); |
| 911 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 912 | // FIXME: When reading literal tokens, reconstruct the literal pointer if |
| 913 | // it is needed. |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 914 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 915 | |
| 916 | // FIXME: Should translate token kind to a stable encoding. |
| 917 | Record.push_back(Tok.getKind()); |
| 918 | // FIXME: Should translate token flags to a stable encoding. |
| 919 | Record.push_back(Tok.getFlags()); |
| 920 | |
| 921 | S.EmitRecord(pch::PP_TOKEN, Record); |
| 922 | Record.clear(); |
| 923 | } |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 924 | |
| 925 | } |
| 926 | |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 927 | S.ExitBlock(); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 931 | /// \brief Write the representation of a type to the PCH stream. |
| 932 | void PCHWriter::WriteType(const Type *T) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 933 | pch::TypeID &ID = TypeIDs[T]; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 934 | if (ID == 0) // we haven't seen this type before. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 935 | ID = NextTypeID++; |
| 936 | |
| 937 | // Record the offset for this type. |
| 938 | if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS) |
| 939 | TypeOffsets.push_back(S.GetCurrentBitNo()); |
| 940 | else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) { |
| 941 | TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS); |
| 942 | TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo(); |
| 943 | } |
| 944 | |
| 945 | RecordData Record; |
| 946 | |
| 947 | // Emit the type's representation. |
| 948 | PCHTypeWriter W(*this, Record); |
| 949 | switch (T->getTypeClass()) { |
| 950 | // For all of the concrete, non-dependent types, call the |
| 951 | // appropriate visitor function. |
| 952 | #define TYPE(Class, Base) \ |
| 953 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
| 954 | #define ABSTRACT_TYPE(Class, Base) |
| 955 | #define DEPENDENT_TYPE(Class, Base) |
| 956 | #include "clang/AST/TypeNodes.def" |
| 957 | |
| 958 | // For all of the dependent type nodes (which only occur in C++ |
| 959 | // templates), produce an error. |
| 960 | #define TYPE(Class, Base) |
| 961 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 962 | #include "clang/AST/TypeNodes.def" |
| 963 | assert(false && "Cannot serialize dependent type nodes"); |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | // Emit the serialized record. |
| 968 | S.EmitRecord(W.Code, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 969 | |
| 970 | // Flush any expressions that were written as part of this type. |
| 971 | FlushExprs(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /// \brief Write a block containing all of the types. |
| 975 | void PCHWriter::WriteTypesBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 976 | // Enter the types block. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 977 | S.EnterSubblock(pch::TYPES_BLOCK_ID, 2); |
| 978 | |
| 979 | // Emit all of the types in the ASTContext |
| 980 | for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(), |
| 981 | TEnd = Context.getTypes().end(); |
| 982 | T != TEnd; ++T) { |
| 983 | // Builtin types are never serialized. |
| 984 | if (isa<BuiltinType>(*T)) |
| 985 | continue; |
| 986 | |
| 987 | WriteType(*T); |
| 988 | } |
| 989 | |
| 990 | // Exit the types block |
| 991 | S.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /// \brief Write the block containing all of the declaration IDs |
| 995 | /// lexically declared within the given DeclContext. |
| 996 | /// |
| 997 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 998 | /// bistream, or 0 if no block was written. |
| 999 | uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
| 1000 | DeclContext *DC) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1001 | if (DC->decls_empty(Context)) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1002 | return 0; |
| 1003 | |
| 1004 | uint64_t Offset = S.GetCurrentBitNo(); |
| 1005 | RecordData Record; |
| 1006 | for (DeclContext::decl_iterator D = DC->decls_begin(Context), |
| 1007 | DEnd = DC->decls_end(Context); |
| 1008 | D != DEnd; ++D) |
| 1009 | AddDeclRef(*D, Record); |
| 1010 | |
| 1011 | S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record); |
| 1012 | return Offset; |
| 1013 | } |
| 1014 | |
| 1015 | /// \brief Write the block containing all of the declaration IDs |
| 1016 | /// visible from the given DeclContext. |
| 1017 | /// |
| 1018 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
| 1019 | /// bistream, or 0 if no block was written. |
| 1020 | uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 1021 | DeclContext *DC) { |
| 1022 | if (DC->getPrimaryContext() != DC) |
| 1023 | return 0; |
| 1024 | |
| 1025 | // Force the DeclContext to build a its name-lookup table. |
| 1026 | DC->lookup(Context, DeclarationName()); |
| 1027 | |
| 1028 | // Serialize the contents of the mapping used for lookup. Note that, |
| 1029 | // although we have two very different code paths, the serialized |
| 1030 | // representation is the same for both cases: a declaration name, |
| 1031 | // followed by a size, followed by references to the visible |
| 1032 | // declarations that have that name. |
| 1033 | uint64_t Offset = S.GetCurrentBitNo(); |
| 1034 | RecordData Record; |
| 1035 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1036 | if (!Map) |
| 1037 | return 0; |
| 1038 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1039 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 1040 | D != DEnd; ++D) { |
| 1041 | AddDeclarationName(D->first, Record); |
| 1042 | DeclContext::lookup_result Result = D->second.getLookupResult(Context); |
| 1043 | Record.push_back(Result.second - Result.first); |
| 1044 | for(; Result.first != Result.second; ++Result.first) |
| 1045 | AddDeclRef(*Result.first, Record); |
| 1046 | } |
| 1047 | |
| 1048 | if (Record.size() == 0) |
| 1049 | return 0; |
| 1050 | |
| 1051 | S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record); |
| 1052 | return Offset; |
| 1053 | } |
| 1054 | |
| 1055 | /// \brief Write a block containing all of the declarations. |
| 1056 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1057 | // Enter the declarations block. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1058 | S.EnterSubblock(pch::DECLS_BLOCK_ID, 2); |
| 1059 | |
| 1060 | // Emit all of the declarations. |
| 1061 | RecordData Record; |
| 1062 | PCHDeclWriter W(*this, Record); |
| 1063 | while (!DeclsToEmit.empty()) { |
| 1064 | // Pull the next declaration off the queue |
| 1065 | Decl *D = DeclsToEmit.front(); |
| 1066 | DeclsToEmit.pop(); |
| 1067 | |
| 1068 | // If this declaration is also a DeclContext, write blocks for the |
| 1069 | // declarations that lexically stored inside its context and those |
| 1070 | // declarations that are visible from its context. These blocks |
| 1071 | // are written before the declaration itself so that we can put |
| 1072 | // their offsets into the record for the declaration. |
| 1073 | uint64_t LexicalOffset = 0; |
| 1074 | uint64_t VisibleOffset = 0; |
| 1075 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 1076 | if (DC) { |
| 1077 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 1078 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 1079 | } |
| 1080 | |
| 1081 | // Determine the ID for this declaration |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1082 | pch::DeclID ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1083 | if (ID == 0) |
| 1084 | ID = DeclIDs.size(); |
| 1085 | |
| 1086 | unsigned Index = ID - 1; |
| 1087 | |
| 1088 | // Record the offset for this declaration |
| 1089 | if (DeclOffsets.size() == Index) |
| 1090 | DeclOffsets.push_back(S.GetCurrentBitNo()); |
| 1091 | else if (DeclOffsets.size() < Index) { |
| 1092 | DeclOffsets.resize(Index+1); |
| 1093 | DeclOffsets[Index] = S.GetCurrentBitNo(); |
| 1094 | } |
| 1095 | |
| 1096 | // Build and emit a record for this declaration |
| 1097 | Record.clear(); |
| 1098 | W.Code = (pch::DeclCode)0; |
| 1099 | W.Visit(D); |
| 1100 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1101 | assert(W.Code && "Unhandled declaration kind while generating PCH"); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1102 | S.EmitRecord(W.Code, Record); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1103 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1104 | // Flush any expressions that were written as part of this declaration. |
| 1105 | FlushExprs(); |
| 1106 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1107 | // Note external declarations so that we can add them to a record |
| 1108 | // in the PCH file later. |
| 1109 | if (isa<FileScopeAsmDecl>(D)) |
| 1110 | ExternalDefinitions.push_back(ID); |
| 1111 | else if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 1112 | if (// Non-static file-scope variables with initializers or that |
| 1113 | // are tentative definitions. |
| 1114 | (Var->isFileVarDecl() && |
| 1115 | (Var->getInit() || Var->getStorageClass() == VarDecl::None)) || |
| 1116 | // Out-of-line definitions of static data members (C++). |
| 1117 | (Var->getDeclContext()->isRecord() && |
| 1118 | !Var->getLexicalDeclContext()->isRecord() && |
| 1119 | Var->getStorageClass() == VarDecl::Static)) |
| 1120 | ExternalDefinitions.push_back(ID); |
| 1121 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) { |
| 1122 | if (Func->isThisDeclarationADefinition() && |
| 1123 | Func->getStorageClass() != FunctionDecl::Static && |
| 1124 | !Func->isInline()) |
| 1125 | ExternalDefinitions.push_back(ID); |
| 1126 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | // Exit the declarations block |
| 1130 | S.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1133 | /// \brief Write the identifier table into the PCH file. |
| 1134 | /// |
| 1135 | /// The identifier table consists of a blob containing string data |
| 1136 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 1137 | /// that maps identifier IDs to locations within the blob. |
| 1138 | void PCHWriter::WriteIdentifierTable() { |
| 1139 | using namespace llvm; |
| 1140 | |
| 1141 | // Create and write out the blob that contains the identifier |
| 1142 | // strings. |
| 1143 | RecordData IdentOffsets; |
| 1144 | IdentOffsets.resize(IdentifierIDs.size()); |
| 1145 | { |
| 1146 | // Create the identifier string data. |
| 1147 | std::vector<char> Data; |
| 1148 | Data.push_back(0); // Data must not be empty. |
| 1149 | for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator |
| 1150 | ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); |
| 1151 | ID != IDEnd; ++ID) { |
| 1152 | assert(ID->first && "NULL identifier in identifier table"); |
| 1153 | |
| 1154 | // Make sure we're starting on an odd byte. The PCH reader |
| 1155 | // expects the low bit to be set on all of the offsets. |
| 1156 | if ((Data.size() & 0x01) == 0) |
| 1157 | Data.push_back((char)0); |
| 1158 | |
| 1159 | IdentOffsets[ID->second - 1] = Data.size(); |
| 1160 | Data.insert(Data.end(), |
| 1161 | ID->first->getName(), |
| 1162 | ID->first->getName() + ID->first->getLength()); |
| 1163 | Data.push_back((char)0); |
| 1164 | } |
| 1165 | |
| 1166 | // Create a blob abbreviation |
| 1167 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1168 | Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE)); |
| 1169 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
| 1170 | unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev); |
| 1171 | |
| 1172 | // Write the identifier table |
| 1173 | RecordData Record; |
| 1174 | Record.push_back(pch::IDENTIFIER_TABLE); |
| 1175 | S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size()); |
| 1176 | } |
| 1177 | |
| 1178 | // Write the offsets table for identifier IDs. |
| 1179 | S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets); |
| 1180 | } |
| 1181 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1182 | PCHWriter::PCHWriter(llvm::BitstreamWriter &S) |
| 1183 | : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { } |
| 1184 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1185 | void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1186 | // Emit the file header. |
| 1187 | S.Emit((unsigned)'C', 8); |
| 1188 | S.Emit((unsigned)'P', 8); |
| 1189 | S.Emit((unsigned)'C', 8); |
| 1190 | S.Emit((unsigned)'H', 8); |
| 1191 | |
| 1192 | // The translation unit is the first declaration we'll emit. |
| 1193 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
| 1194 | DeclsToEmit.push(Context.getTranslationUnitDecl()); |
| 1195 | |
| 1196 | // Write the remaining PCH contents. |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1197 | S.EnterSubblock(pch::PCH_BLOCK_ID, 3); |
| 1198 | WriteTargetTriple(Context.Target); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1199 | WriteLanguageOptions(Context.getLangOptions()); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1200 | WriteSourceManagerBlock(Context.getSourceManager()); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1201 | WritePreprocessor(PP); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1202 | WriteTypesBlock(Context); |
| 1203 | WriteDeclsBlock(Context); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1204 | WriteIdentifierTable(); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1205 | S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets); |
| 1206 | S.EmitRecord(pch::DECL_OFFSET, DeclOffsets); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1207 | if (!ExternalDefinitions.empty()) |
| 1208 | S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1209 | S.ExitBlock(); |
| 1210 | } |
| 1211 | |
| 1212 | void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { |
| 1213 | Record.push_back(Loc.getRawEncoding()); |
| 1214 | } |
| 1215 | |
| 1216 | void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { |
| 1217 | Record.push_back(Value.getBitWidth()); |
| 1218 | unsigned N = Value.getNumWords(); |
| 1219 | const uint64_t* Words = Value.getRawData(); |
| 1220 | for (unsigned I = 0; I != N; ++I) |
| 1221 | Record.push_back(Words[I]); |
| 1222 | } |
| 1223 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1224 | void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) { |
| 1225 | Record.push_back(Value.isUnsigned()); |
| 1226 | AddAPInt(Value, Record); |
| 1227 | } |
| 1228 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1229 | void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) { |
| 1230 | AddAPInt(Value.bitcastToAPInt(), Record); |
| 1231 | } |
| 1232 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1233 | void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1234 | if (II == 0) { |
| 1235 | Record.push_back(0); |
| 1236 | return; |
| 1237 | } |
| 1238 | |
| 1239 | pch::IdentID &ID = IdentifierIDs[II]; |
| 1240 | if (ID == 0) |
| 1241 | ID = IdentifierIDs.size(); |
| 1242 | |
| 1243 | Record.push_back(ID); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | void PCHWriter::AddTypeRef(QualType T, RecordData &Record) { |
| 1247 | if (T.isNull()) { |
| 1248 | Record.push_back(pch::PREDEF_TYPE_NULL_ID); |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1253 | pch::TypeID ID = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1254 | switch (BT->getKind()) { |
| 1255 | case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break; |
| 1256 | case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break; |
| 1257 | case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break; |
| 1258 | case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break; |
| 1259 | case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break; |
| 1260 | case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break; |
| 1261 | case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break; |
| 1262 | case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break; |
| 1263 | case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break; |
| 1264 | case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break; |
| 1265 | case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break; |
| 1266 | case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break; |
| 1267 | case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break; |
| 1268 | case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break; |
| 1269 | case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break; |
| 1270 | case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break; |
| 1271 | case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break; |
| 1272 | case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break; |
| 1273 | case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break; |
| 1274 | case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break; |
| 1275 | } |
| 1276 | |
| 1277 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1278 | return; |
| 1279 | } |
| 1280 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1281 | pch::TypeID &ID = TypeIDs[T.getTypePtr()]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1282 | if (ID == 0) // we haven't seen this type before |
| 1283 | ID = NextTypeID++; |
| 1284 | |
| 1285 | // Encode the type qualifiers in the type reference. |
| 1286 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1287 | } |
| 1288 | |
| 1289 | void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) { |
| 1290 | if (D == 0) { |
| 1291 | Record.push_back(0); |
| 1292 | return; |
| 1293 | } |
| 1294 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1295 | pch::DeclID &ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1296 | if (ID == 0) { |
| 1297 | // We haven't seen this declaration before. Give it a new ID and |
| 1298 | // enqueue it in the list of declarations to emit. |
| 1299 | ID = DeclIDs.size(); |
| 1300 | DeclsToEmit.push(const_cast<Decl *>(D)); |
| 1301 | } |
| 1302 | |
| 1303 | Record.push_back(ID); |
| 1304 | } |
| 1305 | |
| 1306 | void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { |
| 1307 | Record.push_back(Name.getNameKind()); |
| 1308 | switch (Name.getNameKind()) { |
| 1309 | case DeclarationName::Identifier: |
| 1310 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 1311 | break; |
| 1312 | |
| 1313 | case DeclarationName::ObjCZeroArgSelector: |
| 1314 | case DeclarationName::ObjCOneArgSelector: |
| 1315 | case DeclarationName::ObjCMultiArgSelector: |
| 1316 | assert(false && "Serialization of Objective-C selectors unavailable"); |
| 1317 | break; |
| 1318 | |
| 1319 | case DeclarationName::CXXConstructorName: |
| 1320 | case DeclarationName::CXXDestructorName: |
| 1321 | case DeclarationName::CXXConversionFunctionName: |
| 1322 | AddTypeRef(Name.getCXXNameType(), Record); |
| 1323 | break; |
| 1324 | |
| 1325 | case DeclarationName::CXXOperatorName: |
| 1326 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 1327 | break; |
| 1328 | |
| 1329 | case DeclarationName::CXXUsingDirective: |
| 1330 | // No extra data to emit |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1334 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1335 | /// \brief Write the given subexpression to the bitstream. |
| 1336 | void PCHWriter::WriteSubExpr(Expr *E) { |
| 1337 | RecordData Record; |
| 1338 | PCHStmtWriter Writer(*this, Record); |
| 1339 | |
| 1340 | if (!E) { |
| 1341 | S.EmitRecord(pch::EXPR_NULL, Record); |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | Writer.Code = pch::EXPR_NULL; |
| 1346 | Writer.Visit(E); |
| 1347 | assert(Writer.Code != pch::EXPR_NULL && |
| 1348 | "Unhandled expression writing PCH file"); |
| 1349 | S.EmitRecord(Writer.Code, Record); |
| 1350 | } |
| 1351 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1352 | /// \brief Flush all of the expressions that have been added to the |
| 1353 | /// queue via AddExpr(). |
| 1354 | void PCHWriter::FlushExprs() { |
| 1355 | RecordData Record; |
| 1356 | PCHStmtWriter Writer(*this, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1357 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1358 | for (unsigned I = 0, N = ExprsToEmit.size(); I != N; ++I) { |
| 1359 | Expr *E = ExprsToEmit[I]; |
| 1360 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1361 | if (!E) { |
| 1362 | S.EmitRecord(pch::EXPR_NULL, Record); |
| 1363 | continue; |
| 1364 | } |
| 1365 | |
| 1366 | Writer.Code = pch::EXPR_NULL; |
| 1367 | Writer.Visit(E); |
| 1368 | assert(Writer.Code != pch::EXPR_NULL && |
| 1369 | "Unhandled expression writing PCH file"); |
| 1370 | S.EmitRecord(Writer.Code, Record); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1371 | |
| 1372 | assert(N == ExprsToEmit.size() && |
| 1373 | "Subexpression writen via AddExpr rather than WriteSubExpr!"); |
| 1374 | |
| 1375 | // Note that we are at the end of a full expression. Any |
| 1376 | // expression records that follow this one are part of a different |
| 1377 | // expression. |
| 1378 | Record.clear(); |
| 1379 | S.EmitRecord(pch::EXPR_STOP, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1380 | } |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1381 | |
| 1382 | ExprsToEmit.clear(); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1383 | } |