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 | 1f0d013 | 2009-04-15 17:43:59 +0000 | [diff] [blame^] | 456 | void VisitCallExpr(CallExpr *E); |
| 457 | void VisitMemberExpr(MemberExpr *E); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 458 | void VisitCastExpr(CastExpr *E); |
Douglas Gregor | db600c3 | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 459 | void VisitBinaryOperator(BinaryOperator *E); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 460 | void VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | db600c3 | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 461 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 462 | void VisitCStyleCastExpr(CStyleCastExpr *E); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 463 | }; |
| 464 | } |
| 465 | |
| 466 | void PCHStmtWriter::VisitExpr(Expr *E) { |
| 467 | Writer.AddTypeRef(E->getType(), Record); |
| 468 | Record.push_back(E->isTypeDependent()); |
| 469 | Record.push_back(E->isValueDependent()); |
| 470 | } |
| 471 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 472 | void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) { |
| 473 | VisitExpr(E); |
| 474 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 475 | Record.push_back(E->getIdentType()); // FIXME: stable encoding |
| 476 | Code = pch::EXPR_PREDEFINED; |
| 477 | } |
| 478 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 479 | void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) { |
| 480 | VisitExpr(E); |
| 481 | Writer.AddDeclRef(E->getDecl(), Record); |
| 482 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 483 | Code = pch::EXPR_DECL_REF; |
| 484 | } |
| 485 | |
| 486 | void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 487 | VisitExpr(E); |
| 488 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 489 | Writer.AddAPInt(E->getValue(), Record); |
| 490 | Code = pch::EXPR_INTEGER_LITERAL; |
| 491 | } |
| 492 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 493 | void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) { |
| 494 | VisitExpr(E); |
| 495 | Writer.AddAPFloat(E->getValue(), Record); |
| 496 | Record.push_back(E->isExact()); |
| 497 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 498 | Code = pch::EXPR_FLOATING_LITERAL; |
| 499 | } |
| 500 | |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 501 | void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) { |
| 502 | VisitExpr(E); |
| 503 | Record.push_back(E->getByteLength()); |
| 504 | Record.push_back(E->getNumConcatenated()); |
| 505 | Record.push_back(E->isWide()); |
| 506 | // FIXME: String data should be stored as a blob at the end of the |
| 507 | // StringLiteral. However, we can't do so now because we have no |
| 508 | // provision for coping with abbreviations when we're jumping around |
| 509 | // the PCH file during deserialization. |
| 510 | Record.insert(Record.end(), |
| 511 | E->getStrData(), E->getStrData() + E->getByteLength()); |
| 512 | for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I) |
| 513 | Writer.AddSourceLocation(E->getStrTokenLoc(I), Record); |
| 514 | Code = pch::EXPR_STRING_LITERAL; |
| 515 | } |
| 516 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 517 | void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 518 | VisitExpr(E); |
| 519 | Record.push_back(E->getValue()); |
| 520 | Writer.AddSourceLocation(E->getLoc(), Record); |
| 521 | Record.push_back(E->isWide()); |
| 522 | Code = pch::EXPR_CHARACTER_LITERAL; |
| 523 | } |
| 524 | |
Douglas Gregor | c04db4f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 525 | void PCHStmtWriter::VisitParenExpr(ParenExpr *E) { |
| 526 | VisitExpr(E); |
| 527 | Writer.AddSourceLocation(E->getLParen(), Record); |
| 528 | Writer.AddSourceLocation(E->getRParen(), Record); |
| 529 | Writer.WriteSubExpr(E->getSubExpr()); |
| 530 | Code = pch::EXPR_PAREN; |
| 531 | } |
| 532 | |
Douglas Gregor | 0b0b77f | 2009-04-15 15:58:59 +0000 | [diff] [blame] | 533 | void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) { |
| 534 | VisitExpr(E); |
| 535 | Writer.WriteSubExpr(E->getSubExpr()); |
| 536 | Record.push_back(E->getOpcode()); // FIXME: stable encoding |
| 537 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 538 | Code = pch::EXPR_UNARY_OPERATOR; |
| 539 | } |
| 540 | |
| 541 | void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { |
| 542 | VisitExpr(E); |
| 543 | Record.push_back(E->isSizeOf()); |
| 544 | if (E->isArgumentType()) |
| 545 | Writer.AddTypeRef(E->getArgumentType(), Record); |
| 546 | else { |
| 547 | Record.push_back(0); |
| 548 | Writer.WriteSubExpr(E->getArgumentExpr()); |
| 549 | } |
| 550 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 551 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 552 | Code = pch::EXPR_SIZEOF_ALIGN_OF; |
| 553 | } |
| 554 | |
Douglas Gregor | 1f0d013 | 2009-04-15 17:43:59 +0000 | [diff] [blame^] | 555 | void PCHStmtWriter::VisitCallExpr(CallExpr *E) { |
| 556 | VisitExpr(E); |
| 557 | Record.push_back(E->getNumArgs()); |
| 558 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 559 | Writer.WriteSubExpr(E->getCallee()); |
| 560 | for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end(); |
| 561 | Arg != ArgEnd; ++Arg) |
| 562 | Writer.WriteSubExpr(*Arg); |
| 563 | Code = pch::EXPR_CALL; |
| 564 | } |
| 565 | |
| 566 | void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) { |
| 567 | VisitExpr(E); |
| 568 | Writer.WriteSubExpr(E->getBase()); |
| 569 | Writer.AddDeclRef(E->getMemberDecl(), Record); |
| 570 | Writer.AddSourceLocation(E->getMemberLoc(), Record); |
| 571 | Record.push_back(E->isArrow()); |
| 572 | Code = pch::EXPR_MEMBER; |
| 573 | } |
| 574 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 575 | void PCHStmtWriter::VisitCastExpr(CastExpr *E) { |
| 576 | VisitExpr(E); |
| 577 | Writer.WriteSubExpr(E->getSubExpr()); |
| 578 | } |
| 579 | |
Douglas Gregor | db600c3 | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 580 | void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) { |
| 581 | VisitExpr(E); |
| 582 | Writer.WriteSubExpr(E->getLHS()); |
| 583 | Writer.WriteSubExpr(E->getRHS()); |
| 584 | Record.push_back(E->getOpcode()); // FIXME: stable encoding |
| 585 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 586 | Code = pch::EXPR_BINARY_OPERATOR; |
| 587 | } |
| 588 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 589 | void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 590 | VisitCastExpr(E); |
| 591 | Record.push_back(E->isLvalueCast()); |
| 592 | Code = pch::EXPR_IMPLICIT_CAST; |
| 593 | } |
| 594 | |
Douglas Gregor | db600c3 | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 595 | void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 596 | VisitCastExpr(E); |
| 597 | Writer.AddTypeRef(E->getTypeAsWritten(), Record); |
| 598 | } |
| 599 | |
| 600 | void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 601 | VisitExplicitCastExpr(E); |
| 602 | Writer.AddSourceLocation(E->getLParenLoc(), Record); |
| 603 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 604 | Code = pch::EXPR_CSTYLE_CAST; |
| 605 | } |
| 606 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 607 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 608 | // PCHWriter Implementation |
| 609 | //===----------------------------------------------------------------------===// |
| 610 | |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 611 | /// \brief Write the target triple (e.g., i686-apple-darwin9). |
| 612 | void PCHWriter::WriteTargetTriple(const TargetInfo &Target) { |
| 613 | using namespace llvm; |
| 614 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 615 | Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE)); |
| 616 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
| 617 | unsigned TripleAbbrev = S.EmitAbbrev(Abbrev); |
| 618 | |
| 619 | RecordData Record; |
| 620 | Record.push_back(pch::TARGET_TRIPLE); |
| 621 | const char *Triple = Target.getTargetTriple(); |
| 622 | S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple)); |
| 623 | } |
| 624 | |
| 625 | /// \brief Write the LangOptions structure. |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 626 | void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { |
| 627 | RecordData Record; |
| 628 | Record.push_back(LangOpts.Trigraphs); |
| 629 | Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments. |
| 630 | Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers. |
| 631 | Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode. |
| 632 | Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc) |
| 633 | Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'. |
| 634 | Record.push_back(LangOpts.Digraphs); // C94, C99 and C++ |
| 635 | Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants. |
| 636 | Record.push_back(LangOpts.C99); // C99 Support |
| 637 | Record.push_back(LangOpts.Microsoft); // Microsoft extensions. |
| 638 | Record.push_back(LangOpts.CPlusPlus); // C++ Support |
| 639 | Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support |
| 640 | Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode. |
| 641 | Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords. |
| 642 | |
| 643 | Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled. |
| 644 | Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled. |
| 645 | Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled |
| 646 | |
| 647 | Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings |
| 648 | Record.push_back(LangOpts.Boolean); // Allow bool/true/false |
| 649 | Record.push_back(LangOpts.WritableStrings); // Allow writable strings |
| 650 | Record.push_back(LangOpts.LaxVectorConversions); |
| 651 | Record.push_back(LangOpts.Exceptions); // Support exception handling. |
| 652 | |
| 653 | Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime. |
| 654 | Record.push_back(LangOpts.Freestanding); // Freestanding implementation |
| 655 | Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin) |
| 656 | |
| 657 | Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected |
| 658 | // by locks. |
| 659 | Record.push_back(LangOpts.Blocks); // block extension to C |
| 660 | Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if |
| 661 | // they are unused. |
| 662 | Record.push_back(LangOpts.MathErrno); // Math functions must respect errno |
| 663 | // (modulo the platform support). |
| 664 | |
| 665 | Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when |
| 666 | // signed integer arithmetic overflows. |
| 667 | |
| 668 | Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and |
| 669 | // may be ripped out at any time. |
| 670 | |
| 671 | Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. |
| 672 | Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be |
| 673 | // defined. |
| 674 | Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as |
| 675 | // opposed to __DYNAMIC__). |
| 676 | Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero. |
| 677 | |
| 678 | Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be |
| 679 | // used (instead of C99 semantics). |
| 680 | Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined. |
| 681 | Record.push_back(LangOpts.getGCMode()); |
| 682 | Record.push_back(LangOpts.getVisibilityMode()); |
| 683 | Record.push_back(LangOpts.InstantiationDepth); |
| 684 | S.EmitRecord(pch::LANGUAGE_OPTIONS, Record); |
| 685 | } |
| 686 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 687 | //===----------------------------------------------------------------------===// |
| 688 | // Source Manager Serialization |
| 689 | //===----------------------------------------------------------------------===// |
| 690 | |
| 691 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 692 | /// file. |
| 693 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) { |
| 694 | using namespace llvm; |
| 695 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 696 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY)); |
| 697 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 698 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 699 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 700 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 701 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 702 | return S.EmitAbbrev(Abbrev); |
| 703 | } |
| 704 | |
| 705 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 706 | /// buffer. |
| 707 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) { |
| 708 | using namespace llvm; |
| 709 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 710 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY)); |
| 711 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 712 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 713 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 714 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 715 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
| 716 | return S.EmitAbbrev(Abbrev); |
| 717 | } |
| 718 | |
| 719 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 720 | /// buffer's blob. |
| 721 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) { |
| 722 | using namespace llvm; |
| 723 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 724 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB)); |
| 725 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
| 726 | return S.EmitAbbrev(Abbrev); |
| 727 | } |
| 728 | |
| 729 | /// \brief Create an abbreviation for the SLocEntry that refers to an |
| 730 | /// buffer. |
| 731 | static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) { |
| 732 | using namespace llvm; |
| 733 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 734 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY)); |
| 735 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 736 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 737 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 738 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
| 739 | return S.EmitAbbrev(Abbrev); |
| 740 | } |
| 741 | |
| 742 | /// \brief Writes the block containing the serialized form of the |
| 743 | /// source manager. |
| 744 | /// |
| 745 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 746 | /// blob), indexed based on the file name, so that we only create |
| 747 | /// entries for files that we actually need. In the common case (no |
| 748 | /// errors), we probably won't have to create file entries for any of |
| 749 | /// the files in the AST. |
| 750 | void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 751 | // Enter the source manager block. |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 752 | S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3); |
| 753 | |
| 754 | // Abbreviations for the various kinds of source-location entries. |
| 755 | int SLocFileAbbrv = -1; |
| 756 | int SLocBufferAbbrv = -1; |
| 757 | int SLocBufferBlobAbbrv = -1; |
| 758 | int SLocInstantiationAbbrv = -1; |
| 759 | |
| 760 | // Write out the source location entry table. We skip the first |
| 761 | // entry, which is always the same dummy entry. |
| 762 | RecordData Record; |
| 763 | for (SourceManager::sloc_entry_iterator |
| 764 | SLoc = SourceMgr.sloc_entry_begin() + 1, |
| 765 | SLocEnd = SourceMgr.sloc_entry_end(); |
| 766 | SLoc != SLocEnd; ++SLoc) { |
| 767 | // Figure out which record code to use. |
| 768 | unsigned Code; |
| 769 | if (SLoc->isFile()) { |
| 770 | if (SLoc->getFile().getContentCache()->Entry) |
| 771 | Code = pch::SM_SLOC_FILE_ENTRY; |
| 772 | else |
| 773 | Code = pch::SM_SLOC_BUFFER_ENTRY; |
| 774 | } else |
| 775 | Code = pch::SM_SLOC_INSTANTIATION_ENTRY; |
| 776 | Record.push_back(Code); |
| 777 | |
| 778 | Record.push_back(SLoc->getOffset()); |
| 779 | if (SLoc->isFile()) { |
| 780 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 781 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 782 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 783 | Record.push_back(File.hasLineDirectives()); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 784 | |
| 785 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
| 786 | if (Content->Entry) { |
| 787 | // The source location entry is a file. The blob associated |
| 788 | // with this entry is the file name. |
| 789 | if (SLocFileAbbrv == -1) |
| 790 | SLocFileAbbrv = CreateSLocFileAbbrev(S); |
| 791 | S.EmitRecordWithBlob(SLocFileAbbrv, Record, |
| 792 | Content->Entry->getName(), |
| 793 | strlen(Content->Entry->getName())); |
| 794 | } else { |
| 795 | // The source location entry is a buffer. The blob associated |
| 796 | // with this entry contains the contents of the buffer. |
| 797 | if (SLocBufferAbbrv == -1) { |
| 798 | SLocBufferAbbrv = CreateSLocBufferAbbrev(S); |
| 799 | SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S); |
| 800 | } |
| 801 | |
| 802 | // We add one to the size so that we capture the trailing NULL |
| 803 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 804 | // the reader side). |
| 805 | const llvm::MemoryBuffer *Buffer = Content->getBuffer(); |
| 806 | const char *Name = Buffer->getBufferIdentifier(); |
| 807 | S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1); |
| 808 | Record.clear(); |
| 809 | Record.push_back(pch::SM_SLOC_BUFFER_BLOB); |
| 810 | S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
| 811 | Buffer->getBufferStart(), |
| 812 | Buffer->getBufferSize() + 1); |
| 813 | } |
| 814 | } else { |
| 815 | // The source location entry is an instantiation. |
| 816 | const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); |
| 817 | Record.push_back(Inst.getSpellingLoc().getRawEncoding()); |
| 818 | Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); |
| 819 | Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); |
| 820 | |
| 821 | if (SLocInstantiationAbbrv == -1) |
| 822 | SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S); |
| 823 | S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); |
| 824 | } |
| 825 | |
| 826 | Record.clear(); |
| 827 | } |
| 828 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 829 | // Write the line table. |
| 830 | if (SourceMgr.hasLineTable()) { |
| 831 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 832 | |
| 833 | // Emit the file names |
| 834 | Record.push_back(LineTable.getNumFilenames()); |
| 835 | for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { |
| 836 | // Emit the file name |
| 837 | const char *Filename = LineTable.getFilename(I); |
| 838 | unsigned FilenameLen = Filename? strlen(Filename) : 0; |
| 839 | Record.push_back(FilenameLen); |
| 840 | if (FilenameLen) |
| 841 | Record.insert(Record.end(), Filename, Filename + FilenameLen); |
| 842 | } |
| 843 | |
| 844 | // Emit the line entries |
| 845 | for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); |
| 846 | L != LEnd; ++L) { |
| 847 | // Emit the file ID |
| 848 | Record.push_back(L->first); |
| 849 | |
| 850 | // Emit the line entries |
| 851 | Record.push_back(L->second.size()); |
| 852 | for (std::vector<LineEntry>::iterator LE = L->second.begin(), |
| 853 | LEEnd = L->second.end(); |
| 854 | LE != LEEnd; ++LE) { |
| 855 | Record.push_back(LE->FileOffset); |
| 856 | Record.push_back(LE->LineNo); |
| 857 | Record.push_back(LE->FilenameID); |
| 858 | Record.push_back((unsigned)LE->FileKind); |
| 859 | Record.push_back(LE->IncludeOffset); |
| 860 | } |
| 861 | S.EmitRecord(pch::SM_LINE_TABLE, Record); |
| 862 | } |
| 863 | } |
| 864 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 865 | S.ExitBlock(); |
| 866 | } |
| 867 | |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 868 | /// \brief Writes the block containing the serialized form of the |
| 869 | /// preprocessor. |
| 870 | /// |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 871 | void PCHWriter::WritePreprocessor(const Preprocessor &PP) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 872 | // Enter the preprocessor block. |
| 873 | S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3); |
| 874 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 875 | // If the PCH file contains __DATE__ or __TIME__ emit a warning about this. |
| 876 | // FIXME: use diagnostics subsystem for localization etc. |
| 877 | if (PP.SawDateOrTime()) |
| 878 | fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 879 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 880 | RecordData Record; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 881 | |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 882 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 883 | if (PP.getCounterValue() != 0) { |
| 884 | Record.push_back(PP.getCounterValue()); |
| 885 | S.EmitRecord(pch::PP_COUNTER_VALUE, Record); |
| 886 | Record.clear(); |
| 887 | } |
| 888 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 889 | // Loop over all the macro definitions that are live at the end of the file, |
| 890 | // emitting each to the PP section. |
| 891 | // FIXME: Eventually we want to emit an index so that we can lazily load |
| 892 | // macros. |
| 893 | for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); |
| 894 | I != E; ++I) { |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 895 | // FIXME: This emits macros in hash table order, we should do it in a stable |
| 896 | // order so that output is reproducible. |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 897 | MacroInfo *MI = I->second; |
| 898 | |
| 899 | // Don't emit builtin macros like __LINE__ to the PCH file unless they have |
| 900 | // been redefined by the header (in which case they are not isBuiltinMacro). |
| 901 | if (MI->isBuiltinMacro()) |
| 902 | continue; |
| 903 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 904 | AddIdentifierRef(I->first, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 905 | Record.push_back(MI->getDefinitionLoc().getRawEncoding()); |
| 906 | Record.push_back(MI->isUsed()); |
| 907 | |
| 908 | unsigned Code; |
| 909 | if (MI->isObjectLike()) { |
| 910 | Code = pch::PP_MACRO_OBJECT_LIKE; |
| 911 | } else { |
| 912 | Code = pch::PP_MACRO_FUNCTION_LIKE; |
| 913 | |
| 914 | Record.push_back(MI->isC99Varargs()); |
| 915 | Record.push_back(MI->isGNUVarargs()); |
| 916 | Record.push_back(MI->getNumArgs()); |
| 917 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 918 | I != E; ++I) |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 919 | AddIdentifierRef(*I, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 920 | } |
| 921 | S.EmitRecord(Code, Record); |
| 922 | Record.clear(); |
| 923 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 924 | // Emit the tokens array. |
| 925 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 926 | // Note that we know that the preprocessor does not have any annotation |
| 927 | // tokens in it because they are created by the parser, and thus can't be |
| 928 | // in a macro definition. |
| 929 | const Token &Tok = MI->getReplacementToken(TokNo); |
| 930 | |
| 931 | Record.push_back(Tok.getLocation().getRawEncoding()); |
| 932 | Record.push_back(Tok.getLength()); |
| 933 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 934 | // FIXME: When reading literal tokens, reconstruct the literal pointer if |
| 935 | // it is needed. |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 936 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 937 | |
| 938 | // FIXME: Should translate token kind to a stable encoding. |
| 939 | Record.push_back(Tok.getKind()); |
| 940 | // FIXME: Should translate token flags to a stable encoding. |
| 941 | Record.push_back(Tok.getFlags()); |
| 942 | |
| 943 | S.EmitRecord(pch::PP_TOKEN, Record); |
| 944 | Record.clear(); |
| 945 | } |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 946 | |
| 947 | } |
| 948 | |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 949 | S.ExitBlock(); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 953 | /// \brief Write the representation of a type to the PCH stream. |
| 954 | void PCHWriter::WriteType(const Type *T) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 955 | pch::TypeID &ID = TypeIDs[T]; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 956 | if (ID == 0) // we haven't seen this type before. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 957 | ID = NextTypeID++; |
| 958 | |
| 959 | // Record the offset for this type. |
| 960 | if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS) |
| 961 | TypeOffsets.push_back(S.GetCurrentBitNo()); |
| 962 | else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) { |
| 963 | TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS); |
| 964 | TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo(); |
| 965 | } |
| 966 | |
| 967 | RecordData Record; |
| 968 | |
| 969 | // Emit the type's representation. |
| 970 | PCHTypeWriter W(*this, Record); |
| 971 | switch (T->getTypeClass()) { |
| 972 | // For all of the concrete, non-dependent types, call the |
| 973 | // appropriate visitor function. |
| 974 | #define TYPE(Class, Base) \ |
| 975 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
| 976 | #define ABSTRACT_TYPE(Class, Base) |
| 977 | #define DEPENDENT_TYPE(Class, Base) |
| 978 | #include "clang/AST/TypeNodes.def" |
| 979 | |
| 980 | // For all of the dependent type nodes (which only occur in C++ |
| 981 | // templates), produce an error. |
| 982 | #define TYPE(Class, Base) |
| 983 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 984 | #include "clang/AST/TypeNodes.def" |
| 985 | assert(false && "Cannot serialize dependent type nodes"); |
| 986 | break; |
| 987 | } |
| 988 | |
| 989 | // Emit the serialized record. |
| 990 | S.EmitRecord(W.Code, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 991 | |
| 992 | // Flush any expressions that were written as part of this type. |
| 993 | FlushExprs(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /// \brief Write a block containing all of the types. |
| 997 | void PCHWriter::WriteTypesBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 998 | // Enter the types block. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 999 | S.EnterSubblock(pch::TYPES_BLOCK_ID, 2); |
| 1000 | |
| 1001 | // Emit all of the types in the ASTContext |
| 1002 | for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(), |
| 1003 | TEnd = Context.getTypes().end(); |
| 1004 | T != TEnd; ++T) { |
| 1005 | // Builtin types are never serialized. |
| 1006 | if (isa<BuiltinType>(*T)) |
| 1007 | continue; |
| 1008 | |
| 1009 | WriteType(*T); |
| 1010 | } |
| 1011 | |
| 1012 | // Exit the types block |
| 1013 | S.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /// \brief Write the block containing all of the declaration IDs |
| 1017 | /// lexically declared within the given DeclContext. |
| 1018 | /// |
| 1019 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 1020 | /// bistream, or 0 if no block was written. |
| 1021 | uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
| 1022 | DeclContext *DC) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1023 | if (DC->decls_empty(Context)) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1024 | return 0; |
| 1025 | |
| 1026 | uint64_t Offset = S.GetCurrentBitNo(); |
| 1027 | RecordData Record; |
| 1028 | for (DeclContext::decl_iterator D = DC->decls_begin(Context), |
| 1029 | DEnd = DC->decls_end(Context); |
| 1030 | D != DEnd; ++D) |
| 1031 | AddDeclRef(*D, Record); |
| 1032 | |
| 1033 | S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record); |
| 1034 | return Offset; |
| 1035 | } |
| 1036 | |
| 1037 | /// \brief Write the block containing all of the declaration IDs |
| 1038 | /// visible from the given DeclContext. |
| 1039 | /// |
| 1040 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
| 1041 | /// bistream, or 0 if no block was written. |
| 1042 | uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 1043 | DeclContext *DC) { |
| 1044 | if (DC->getPrimaryContext() != DC) |
| 1045 | return 0; |
| 1046 | |
| 1047 | // Force the DeclContext to build a its name-lookup table. |
| 1048 | DC->lookup(Context, DeclarationName()); |
| 1049 | |
| 1050 | // Serialize the contents of the mapping used for lookup. Note that, |
| 1051 | // although we have two very different code paths, the serialized |
| 1052 | // representation is the same for both cases: a declaration name, |
| 1053 | // followed by a size, followed by references to the visible |
| 1054 | // declarations that have that name. |
| 1055 | uint64_t Offset = S.GetCurrentBitNo(); |
| 1056 | RecordData Record; |
| 1057 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1058 | if (!Map) |
| 1059 | return 0; |
| 1060 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1061 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 1062 | D != DEnd; ++D) { |
| 1063 | AddDeclarationName(D->first, Record); |
| 1064 | DeclContext::lookup_result Result = D->second.getLookupResult(Context); |
| 1065 | Record.push_back(Result.second - Result.first); |
| 1066 | for(; Result.first != Result.second; ++Result.first) |
| 1067 | AddDeclRef(*Result.first, Record); |
| 1068 | } |
| 1069 | |
| 1070 | if (Record.size() == 0) |
| 1071 | return 0; |
| 1072 | |
| 1073 | S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record); |
| 1074 | return Offset; |
| 1075 | } |
| 1076 | |
| 1077 | /// \brief Write a block containing all of the declarations. |
| 1078 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1079 | // Enter the declarations block. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1080 | S.EnterSubblock(pch::DECLS_BLOCK_ID, 2); |
| 1081 | |
| 1082 | // Emit all of the declarations. |
| 1083 | RecordData Record; |
| 1084 | PCHDeclWriter W(*this, Record); |
| 1085 | while (!DeclsToEmit.empty()) { |
| 1086 | // Pull the next declaration off the queue |
| 1087 | Decl *D = DeclsToEmit.front(); |
| 1088 | DeclsToEmit.pop(); |
| 1089 | |
| 1090 | // If this declaration is also a DeclContext, write blocks for the |
| 1091 | // declarations that lexically stored inside its context and those |
| 1092 | // declarations that are visible from its context. These blocks |
| 1093 | // are written before the declaration itself so that we can put |
| 1094 | // their offsets into the record for the declaration. |
| 1095 | uint64_t LexicalOffset = 0; |
| 1096 | uint64_t VisibleOffset = 0; |
| 1097 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 1098 | if (DC) { |
| 1099 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 1100 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 1101 | } |
| 1102 | |
| 1103 | // Determine the ID for this declaration |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1104 | pch::DeclID ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1105 | if (ID == 0) |
| 1106 | ID = DeclIDs.size(); |
| 1107 | |
| 1108 | unsigned Index = ID - 1; |
| 1109 | |
| 1110 | // Record the offset for this declaration |
| 1111 | if (DeclOffsets.size() == Index) |
| 1112 | DeclOffsets.push_back(S.GetCurrentBitNo()); |
| 1113 | else if (DeclOffsets.size() < Index) { |
| 1114 | DeclOffsets.resize(Index+1); |
| 1115 | DeclOffsets[Index] = S.GetCurrentBitNo(); |
| 1116 | } |
| 1117 | |
| 1118 | // Build and emit a record for this declaration |
| 1119 | Record.clear(); |
| 1120 | W.Code = (pch::DeclCode)0; |
| 1121 | W.Visit(D); |
| 1122 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1123 | assert(W.Code && "Unhandled declaration kind while generating PCH"); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1124 | S.EmitRecord(W.Code, Record); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1126 | // Flush any expressions that were written as part of this declaration. |
| 1127 | FlushExprs(); |
| 1128 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1129 | // Note external declarations so that we can add them to a record |
| 1130 | // in the PCH file later. |
| 1131 | if (isa<FileScopeAsmDecl>(D)) |
| 1132 | ExternalDefinitions.push_back(ID); |
| 1133 | else if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 1134 | if (// Non-static file-scope variables with initializers or that |
| 1135 | // are tentative definitions. |
| 1136 | (Var->isFileVarDecl() && |
| 1137 | (Var->getInit() || Var->getStorageClass() == VarDecl::None)) || |
| 1138 | // Out-of-line definitions of static data members (C++). |
| 1139 | (Var->getDeclContext()->isRecord() && |
| 1140 | !Var->getLexicalDeclContext()->isRecord() && |
| 1141 | Var->getStorageClass() == VarDecl::Static)) |
| 1142 | ExternalDefinitions.push_back(ID); |
| 1143 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) { |
| 1144 | if (Func->isThisDeclarationADefinition() && |
| 1145 | Func->getStorageClass() != FunctionDecl::Static && |
| 1146 | !Func->isInline()) |
| 1147 | ExternalDefinitions.push_back(ID); |
| 1148 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | // Exit the declarations block |
| 1152 | S.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1155 | /// \brief Write the identifier table into the PCH file. |
| 1156 | /// |
| 1157 | /// The identifier table consists of a blob containing string data |
| 1158 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 1159 | /// that maps identifier IDs to locations within the blob. |
| 1160 | void PCHWriter::WriteIdentifierTable() { |
| 1161 | using namespace llvm; |
| 1162 | |
| 1163 | // Create and write out the blob that contains the identifier |
| 1164 | // strings. |
| 1165 | RecordData IdentOffsets; |
| 1166 | IdentOffsets.resize(IdentifierIDs.size()); |
| 1167 | { |
| 1168 | // Create the identifier string data. |
| 1169 | std::vector<char> Data; |
| 1170 | Data.push_back(0); // Data must not be empty. |
| 1171 | for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator |
| 1172 | ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); |
| 1173 | ID != IDEnd; ++ID) { |
| 1174 | assert(ID->first && "NULL identifier in identifier table"); |
| 1175 | |
| 1176 | // Make sure we're starting on an odd byte. The PCH reader |
| 1177 | // expects the low bit to be set on all of the offsets. |
| 1178 | if ((Data.size() & 0x01) == 0) |
| 1179 | Data.push_back((char)0); |
| 1180 | |
| 1181 | IdentOffsets[ID->second - 1] = Data.size(); |
| 1182 | Data.insert(Data.end(), |
| 1183 | ID->first->getName(), |
| 1184 | ID->first->getName() + ID->first->getLength()); |
| 1185 | Data.push_back((char)0); |
| 1186 | } |
| 1187 | |
| 1188 | // Create a blob abbreviation |
| 1189 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1190 | Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE)); |
| 1191 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
| 1192 | unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev); |
| 1193 | |
| 1194 | // Write the identifier table |
| 1195 | RecordData Record; |
| 1196 | Record.push_back(pch::IDENTIFIER_TABLE); |
| 1197 | S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size()); |
| 1198 | } |
| 1199 | |
| 1200 | // Write the offsets table for identifier IDs. |
| 1201 | S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets); |
| 1202 | } |
| 1203 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1204 | PCHWriter::PCHWriter(llvm::BitstreamWriter &S) |
| 1205 | : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { } |
| 1206 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1207 | void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1208 | // Emit the file header. |
| 1209 | S.Emit((unsigned)'C', 8); |
| 1210 | S.Emit((unsigned)'P', 8); |
| 1211 | S.Emit((unsigned)'C', 8); |
| 1212 | S.Emit((unsigned)'H', 8); |
| 1213 | |
| 1214 | // The translation unit is the first declaration we'll emit. |
| 1215 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
| 1216 | DeclsToEmit.push(Context.getTranslationUnitDecl()); |
| 1217 | |
| 1218 | // Write the remaining PCH contents. |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1219 | S.EnterSubblock(pch::PCH_BLOCK_ID, 3); |
| 1220 | WriteTargetTriple(Context.Target); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1221 | WriteLanguageOptions(Context.getLangOptions()); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1222 | WriteSourceManagerBlock(Context.getSourceManager()); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1223 | WritePreprocessor(PP); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1224 | WriteTypesBlock(Context); |
| 1225 | WriteDeclsBlock(Context); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1226 | WriteIdentifierTable(); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1227 | S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets); |
| 1228 | S.EmitRecord(pch::DECL_OFFSET, DeclOffsets); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1229 | if (!ExternalDefinitions.empty()) |
| 1230 | S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1231 | S.ExitBlock(); |
| 1232 | } |
| 1233 | |
| 1234 | void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { |
| 1235 | Record.push_back(Loc.getRawEncoding()); |
| 1236 | } |
| 1237 | |
| 1238 | void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { |
| 1239 | Record.push_back(Value.getBitWidth()); |
| 1240 | unsigned N = Value.getNumWords(); |
| 1241 | const uint64_t* Words = Value.getRawData(); |
| 1242 | for (unsigned I = 0; I != N; ++I) |
| 1243 | Record.push_back(Words[I]); |
| 1244 | } |
| 1245 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1246 | void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) { |
| 1247 | Record.push_back(Value.isUnsigned()); |
| 1248 | AddAPInt(Value, Record); |
| 1249 | } |
| 1250 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1251 | void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) { |
| 1252 | AddAPInt(Value.bitcastToAPInt(), Record); |
| 1253 | } |
| 1254 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1255 | void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1256 | if (II == 0) { |
| 1257 | Record.push_back(0); |
| 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | pch::IdentID &ID = IdentifierIDs[II]; |
| 1262 | if (ID == 0) |
| 1263 | ID = IdentifierIDs.size(); |
| 1264 | |
| 1265 | Record.push_back(ID); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | void PCHWriter::AddTypeRef(QualType T, RecordData &Record) { |
| 1269 | if (T.isNull()) { |
| 1270 | Record.push_back(pch::PREDEF_TYPE_NULL_ID); |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1275 | pch::TypeID ID = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1276 | switch (BT->getKind()) { |
| 1277 | case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break; |
| 1278 | case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break; |
| 1279 | case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break; |
| 1280 | case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break; |
| 1281 | case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break; |
| 1282 | case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break; |
| 1283 | case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break; |
| 1284 | case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break; |
| 1285 | case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break; |
| 1286 | case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break; |
| 1287 | case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break; |
| 1288 | case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break; |
| 1289 | case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break; |
| 1290 | case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break; |
| 1291 | case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break; |
| 1292 | case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break; |
| 1293 | case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break; |
| 1294 | case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break; |
| 1295 | case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break; |
| 1296 | case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break; |
| 1297 | } |
| 1298 | |
| 1299 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1300 | return; |
| 1301 | } |
| 1302 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1303 | pch::TypeID &ID = TypeIDs[T.getTypePtr()]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1304 | if (ID == 0) // we haven't seen this type before |
| 1305 | ID = NextTypeID++; |
| 1306 | |
| 1307 | // Encode the type qualifiers in the type reference. |
| 1308 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1309 | } |
| 1310 | |
| 1311 | void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) { |
| 1312 | if (D == 0) { |
| 1313 | Record.push_back(0); |
| 1314 | return; |
| 1315 | } |
| 1316 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1317 | pch::DeclID &ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1318 | if (ID == 0) { |
| 1319 | // We haven't seen this declaration before. Give it a new ID and |
| 1320 | // enqueue it in the list of declarations to emit. |
| 1321 | ID = DeclIDs.size(); |
| 1322 | DeclsToEmit.push(const_cast<Decl *>(D)); |
| 1323 | } |
| 1324 | |
| 1325 | Record.push_back(ID); |
| 1326 | } |
| 1327 | |
| 1328 | void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { |
| 1329 | Record.push_back(Name.getNameKind()); |
| 1330 | switch (Name.getNameKind()) { |
| 1331 | case DeclarationName::Identifier: |
| 1332 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 1333 | break; |
| 1334 | |
| 1335 | case DeclarationName::ObjCZeroArgSelector: |
| 1336 | case DeclarationName::ObjCOneArgSelector: |
| 1337 | case DeclarationName::ObjCMultiArgSelector: |
| 1338 | assert(false && "Serialization of Objective-C selectors unavailable"); |
| 1339 | break; |
| 1340 | |
| 1341 | case DeclarationName::CXXConstructorName: |
| 1342 | case DeclarationName::CXXDestructorName: |
| 1343 | case DeclarationName::CXXConversionFunctionName: |
| 1344 | AddTypeRef(Name.getCXXNameType(), Record); |
| 1345 | break; |
| 1346 | |
| 1347 | case DeclarationName::CXXOperatorName: |
| 1348 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 1349 | break; |
| 1350 | |
| 1351 | case DeclarationName::CXXUsingDirective: |
| 1352 | // No extra data to emit |
| 1353 | break; |
| 1354 | } |
| 1355 | } |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1356 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1357 | /// \brief Write the given subexpression to the bitstream. |
| 1358 | void PCHWriter::WriteSubExpr(Expr *E) { |
| 1359 | RecordData Record; |
| 1360 | PCHStmtWriter Writer(*this, Record); |
| 1361 | |
| 1362 | if (!E) { |
| 1363 | S.EmitRecord(pch::EXPR_NULL, Record); |
| 1364 | return; |
| 1365 | } |
| 1366 | |
| 1367 | Writer.Code = pch::EXPR_NULL; |
| 1368 | Writer.Visit(E); |
| 1369 | assert(Writer.Code != pch::EXPR_NULL && |
| 1370 | "Unhandled expression writing PCH file"); |
| 1371 | S.EmitRecord(Writer.Code, Record); |
| 1372 | } |
| 1373 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1374 | /// \brief Flush all of the expressions that have been added to the |
| 1375 | /// queue via AddExpr(). |
| 1376 | void PCHWriter::FlushExprs() { |
| 1377 | RecordData Record; |
| 1378 | PCHStmtWriter Writer(*this, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1379 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1380 | for (unsigned I = 0, N = ExprsToEmit.size(); I != N; ++I) { |
| 1381 | Expr *E = ExprsToEmit[I]; |
| 1382 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1383 | if (!E) { |
| 1384 | S.EmitRecord(pch::EXPR_NULL, Record); |
| 1385 | continue; |
| 1386 | } |
| 1387 | |
| 1388 | Writer.Code = pch::EXPR_NULL; |
| 1389 | Writer.Visit(E); |
| 1390 | assert(Writer.Code != pch::EXPR_NULL && |
| 1391 | "Unhandled expression writing PCH file"); |
| 1392 | S.EmitRecord(Writer.Code, Record); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1393 | |
| 1394 | assert(N == ExprsToEmit.size() && |
| 1395 | "Subexpression writen via AddExpr rather than WriteSubExpr!"); |
| 1396 | |
| 1397 | // Note that we are at the end of a full expression. Any |
| 1398 | // expression records that follow this one are part of a different |
| 1399 | // expression. |
| 1400 | Record.clear(); |
| 1401 | S.EmitRecord(pch::EXPR_STOP, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1402 | } |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1403 | |
| 1404 | ExprsToEmit.clear(); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1405 | } |