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) { |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame^] | 197 | // FIXME: Serialize this type (C++ only) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 198 | assert(false && "Cannot serialize template specialization types"); |
| 199 | } |
| 200 | |
| 201 | void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) { |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame^] | 202 | // FIXME: Serialize this type (C++ only) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 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 |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame^] | 384 | // FIXME: emit default argument (C++) |
Douglas Gregor | 3a2f7e4 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 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); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame^] | 398 | Writer.AddExpr(D->getAsmString()); |
Douglas Gregor | 1028bc6 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 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 |
Douglas Gregor | f60e991 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 739 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 740 | return S.EmitAbbrev(Abbrev); |
| 741 | } |
| 742 | |
| 743 | /// \brief Writes the block containing the serialized form of the |
| 744 | /// source manager. |
| 745 | /// |
| 746 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 747 | /// blob), indexed based on the file name, so that we only create |
| 748 | /// entries for files that we actually need. In the common case (no |
| 749 | /// errors), we probably won't have to create file entries for any of |
| 750 | /// the files in the AST. |
| 751 | void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 752 | // Enter the source manager block. |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 753 | S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3); |
| 754 | |
| 755 | // Abbreviations for the various kinds of source-location entries. |
| 756 | int SLocFileAbbrv = -1; |
| 757 | int SLocBufferAbbrv = -1; |
| 758 | int SLocBufferBlobAbbrv = -1; |
| 759 | int SLocInstantiationAbbrv = -1; |
| 760 | |
| 761 | // Write out the source location entry table. We skip the first |
| 762 | // entry, which is always the same dummy entry. |
| 763 | RecordData Record; |
| 764 | for (SourceManager::sloc_entry_iterator |
| 765 | SLoc = SourceMgr.sloc_entry_begin() + 1, |
| 766 | SLocEnd = SourceMgr.sloc_entry_end(); |
| 767 | SLoc != SLocEnd; ++SLoc) { |
| 768 | // Figure out which record code to use. |
| 769 | unsigned Code; |
| 770 | if (SLoc->isFile()) { |
| 771 | if (SLoc->getFile().getContentCache()->Entry) |
| 772 | Code = pch::SM_SLOC_FILE_ENTRY; |
| 773 | else |
| 774 | Code = pch::SM_SLOC_BUFFER_ENTRY; |
| 775 | } else |
| 776 | Code = pch::SM_SLOC_INSTANTIATION_ENTRY; |
| 777 | Record.push_back(Code); |
| 778 | |
| 779 | Record.push_back(SLoc->getOffset()); |
| 780 | if (SLoc->isFile()) { |
| 781 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 782 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 783 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 784 | Record.push_back(File.hasLineDirectives()); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 785 | |
| 786 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
| 787 | if (Content->Entry) { |
| 788 | // The source location entry is a file. The blob associated |
| 789 | // with this entry is the file name. |
| 790 | if (SLocFileAbbrv == -1) |
| 791 | SLocFileAbbrv = CreateSLocFileAbbrev(S); |
| 792 | S.EmitRecordWithBlob(SLocFileAbbrv, Record, |
| 793 | Content->Entry->getName(), |
| 794 | strlen(Content->Entry->getName())); |
| 795 | } else { |
| 796 | // The source location entry is a buffer. The blob associated |
| 797 | // with this entry contains the contents of the buffer. |
| 798 | if (SLocBufferAbbrv == -1) { |
| 799 | SLocBufferAbbrv = CreateSLocBufferAbbrev(S); |
| 800 | SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S); |
| 801 | } |
| 802 | |
| 803 | // We add one to the size so that we capture the trailing NULL |
| 804 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 805 | // the reader side). |
| 806 | const llvm::MemoryBuffer *Buffer = Content->getBuffer(); |
| 807 | const char *Name = Buffer->getBufferIdentifier(); |
| 808 | S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1); |
| 809 | Record.clear(); |
| 810 | Record.push_back(pch::SM_SLOC_BUFFER_BLOB); |
| 811 | S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
| 812 | Buffer->getBufferStart(), |
| 813 | Buffer->getBufferSize() + 1); |
| 814 | } |
| 815 | } else { |
| 816 | // The source location entry is an instantiation. |
| 817 | const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); |
| 818 | Record.push_back(Inst.getSpellingLoc().getRawEncoding()); |
| 819 | Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); |
| 820 | Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); |
| 821 | |
Douglas Gregor | f60e991 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 822 | // Compute the token length for this macro expansion. |
| 823 | unsigned NextOffset = SourceMgr.getNextOffset(); |
| 824 | SourceManager::sloc_entry_iterator NextSLoc = SLoc; |
| 825 | if (++NextSLoc != SLocEnd) |
| 826 | NextOffset = NextSLoc->getOffset(); |
| 827 | Record.push_back(NextOffset - SLoc->getOffset() - 1); |
| 828 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 829 | if (SLocInstantiationAbbrv == -1) |
| 830 | SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S); |
| 831 | S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); |
| 832 | } |
| 833 | |
| 834 | Record.clear(); |
| 835 | } |
| 836 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 837 | // Write the line table. |
| 838 | if (SourceMgr.hasLineTable()) { |
| 839 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 840 | |
| 841 | // Emit the file names |
| 842 | Record.push_back(LineTable.getNumFilenames()); |
| 843 | for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { |
| 844 | // Emit the file name |
| 845 | const char *Filename = LineTable.getFilename(I); |
| 846 | unsigned FilenameLen = Filename? strlen(Filename) : 0; |
| 847 | Record.push_back(FilenameLen); |
| 848 | if (FilenameLen) |
| 849 | Record.insert(Record.end(), Filename, Filename + FilenameLen); |
| 850 | } |
| 851 | |
| 852 | // Emit the line entries |
| 853 | for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); |
| 854 | L != LEnd; ++L) { |
| 855 | // Emit the file ID |
| 856 | Record.push_back(L->first); |
| 857 | |
| 858 | // Emit the line entries |
| 859 | Record.push_back(L->second.size()); |
| 860 | for (std::vector<LineEntry>::iterator LE = L->second.begin(), |
| 861 | LEEnd = L->second.end(); |
| 862 | LE != LEEnd; ++LE) { |
| 863 | Record.push_back(LE->FileOffset); |
| 864 | Record.push_back(LE->LineNo); |
| 865 | Record.push_back(LE->FilenameID); |
| 866 | Record.push_back((unsigned)LE->FileKind); |
| 867 | Record.push_back(LE->IncludeOffset); |
| 868 | } |
| 869 | S.EmitRecord(pch::SM_LINE_TABLE, Record); |
| 870 | } |
| 871 | } |
| 872 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 873 | S.ExitBlock(); |
| 874 | } |
| 875 | |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 876 | /// \brief Writes the block containing the serialized form of the |
| 877 | /// preprocessor. |
| 878 | /// |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 879 | void PCHWriter::WritePreprocessor(const Preprocessor &PP) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 880 | // Enter the preprocessor block. |
| 881 | S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3); |
| 882 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 883 | // If the PCH file contains __DATE__ or __TIME__ emit a warning about this. |
| 884 | // FIXME: use diagnostics subsystem for localization etc. |
| 885 | if (PP.SawDateOrTime()) |
| 886 | fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 888 | RecordData Record; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 889 | |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 890 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 891 | if (PP.getCounterValue() != 0) { |
| 892 | Record.push_back(PP.getCounterValue()); |
| 893 | S.EmitRecord(pch::PP_COUNTER_VALUE, Record); |
| 894 | Record.clear(); |
| 895 | } |
| 896 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 897 | // Loop over all the macro definitions that are live at the end of the file, |
| 898 | // emitting each to the PP section. |
| 899 | // FIXME: Eventually we want to emit an index so that we can lazily load |
| 900 | // macros. |
| 901 | for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); |
| 902 | I != E; ++I) { |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 903 | // FIXME: This emits macros in hash table order, we should do it in a stable |
| 904 | // order so that output is reproducible. |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 905 | MacroInfo *MI = I->second; |
| 906 | |
| 907 | // Don't emit builtin macros like __LINE__ to the PCH file unless they have |
| 908 | // been redefined by the header (in which case they are not isBuiltinMacro). |
| 909 | if (MI->isBuiltinMacro()) |
| 910 | continue; |
| 911 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 912 | AddIdentifierRef(I->first, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 913 | Record.push_back(MI->getDefinitionLoc().getRawEncoding()); |
| 914 | Record.push_back(MI->isUsed()); |
| 915 | |
| 916 | unsigned Code; |
| 917 | if (MI->isObjectLike()) { |
| 918 | Code = pch::PP_MACRO_OBJECT_LIKE; |
| 919 | } else { |
| 920 | Code = pch::PP_MACRO_FUNCTION_LIKE; |
| 921 | |
| 922 | Record.push_back(MI->isC99Varargs()); |
| 923 | Record.push_back(MI->isGNUVarargs()); |
| 924 | Record.push_back(MI->getNumArgs()); |
| 925 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 926 | I != E; ++I) |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 927 | AddIdentifierRef(*I, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 928 | } |
| 929 | S.EmitRecord(Code, Record); |
| 930 | Record.clear(); |
| 931 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 932 | // Emit the tokens array. |
| 933 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 934 | // Note that we know that the preprocessor does not have any annotation |
| 935 | // tokens in it because they are created by the parser, and thus can't be |
| 936 | // in a macro definition. |
| 937 | const Token &Tok = MI->getReplacementToken(TokNo); |
| 938 | |
| 939 | Record.push_back(Tok.getLocation().getRawEncoding()); |
| 940 | Record.push_back(Tok.getLength()); |
| 941 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 942 | // FIXME: When reading literal tokens, reconstruct the literal pointer if |
| 943 | // it is needed. |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 944 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 945 | |
| 946 | // FIXME: Should translate token kind to a stable encoding. |
| 947 | Record.push_back(Tok.getKind()); |
| 948 | // FIXME: Should translate token flags to a stable encoding. |
| 949 | Record.push_back(Tok.getFlags()); |
| 950 | |
| 951 | S.EmitRecord(pch::PP_TOKEN, Record); |
| 952 | Record.clear(); |
| 953 | } |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 954 | |
| 955 | } |
| 956 | |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 957 | S.ExitBlock(); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 961 | /// \brief Write the representation of a type to the PCH stream. |
| 962 | void PCHWriter::WriteType(const Type *T) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 963 | pch::TypeID &ID = TypeIDs[T]; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 964 | if (ID == 0) // we haven't seen this type before. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 965 | ID = NextTypeID++; |
| 966 | |
| 967 | // Record the offset for this type. |
| 968 | if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS) |
| 969 | TypeOffsets.push_back(S.GetCurrentBitNo()); |
| 970 | else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) { |
| 971 | TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS); |
| 972 | TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo(); |
| 973 | } |
| 974 | |
| 975 | RecordData Record; |
| 976 | |
| 977 | // Emit the type's representation. |
| 978 | PCHTypeWriter W(*this, Record); |
| 979 | switch (T->getTypeClass()) { |
| 980 | // For all of the concrete, non-dependent types, call the |
| 981 | // appropriate visitor function. |
| 982 | #define TYPE(Class, Base) \ |
| 983 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
| 984 | #define ABSTRACT_TYPE(Class, Base) |
| 985 | #define DEPENDENT_TYPE(Class, Base) |
| 986 | #include "clang/AST/TypeNodes.def" |
| 987 | |
| 988 | // For all of the dependent type nodes (which only occur in C++ |
| 989 | // templates), produce an error. |
| 990 | #define TYPE(Class, Base) |
| 991 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 992 | #include "clang/AST/TypeNodes.def" |
| 993 | assert(false && "Cannot serialize dependent type nodes"); |
| 994 | break; |
| 995 | } |
| 996 | |
| 997 | // Emit the serialized record. |
| 998 | S.EmitRecord(W.Code, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 999 | |
| 1000 | // Flush any expressions that were written as part of this type. |
| 1001 | FlushExprs(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /// \brief Write a block containing all of the types. |
| 1005 | void PCHWriter::WriteTypesBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1006 | // Enter the types block. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1007 | S.EnterSubblock(pch::TYPES_BLOCK_ID, 2); |
| 1008 | |
| 1009 | // Emit all of the types in the ASTContext |
| 1010 | for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(), |
| 1011 | TEnd = Context.getTypes().end(); |
| 1012 | T != TEnd; ++T) { |
| 1013 | // Builtin types are never serialized. |
| 1014 | if (isa<BuiltinType>(*T)) |
| 1015 | continue; |
| 1016 | |
| 1017 | WriteType(*T); |
| 1018 | } |
| 1019 | |
| 1020 | // Exit the types block |
| 1021 | S.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /// \brief Write the block containing all of the declaration IDs |
| 1025 | /// lexically declared within the given DeclContext. |
| 1026 | /// |
| 1027 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 1028 | /// bistream, or 0 if no block was written. |
| 1029 | uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
| 1030 | DeclContext *DC) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1031 | if (DC->decls_empty(Context)) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1032 | return 0; |
| 1033 | |
| 1034 | uint64_t Offset = S.GetCurrentBitNo(); |
| 1035 | RecordData Record; |
| 1036 | for (DeclContext::decl_iterator D = DC->decls_begin(Context), |
| 1037 | DEnd = DC->decls_end(Context); |
| 1038 | D != DEnd; ++D) |
| 1039 | AddDeclRef(*D, Record); |
| 1040 | |
| 1041 | S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record); |
| 1042 | return Offset; |
| 1043 | } |
| 1044 | |
| 1045 | /// \brief Write the block containing all of the declaration IDs |
| 1046 | /// visible from the given DeclContext. |
| 1047 | /// |
| 1048 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
| 1049 | /// bistream, or 0 if no block was written. |
| 1050 | uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 1051 | DeclContext *DC) { |
| 1052 | if (DC->getPrimaryContext() != DC) |
| 1053 | return 0; |
| 1054 | |
| 1055 | // Force the DeclContext to build a its name-lookup table. |
| 1056 | DC->lookup(Context, DeclarationName()); |
| 1057 | |
| 1058 | // Serialize the contents of the mapping used for lookup. Note that, |
| 1059 | // although we have two very different code paths, the serialized |
| 1060 | // representation is the same for both cases: a declaration name, |
| 1061 | // followed by a size, followed by references to the visible |
| 1062 | // declarations that have that name. |
| 1063 | uint64_t Offset = S.GetCurrentBitNo(); |
| 1064 | RecordData Record; |
| 1065 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1066 | if (!Map) |
| 1067 | return 0; |
| 1068 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1069 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 1070 | D != DEnd; ++D) { |
| 1071 | AddDeclarationName(D->first, Record); |
| 1072 | DeclContext::lookup_result Result = D->second.getLookupResult(Context); |
| 1073 | Record.push_back(Result.second - Result.first); |
| 1074 | for(; Result.first != Result.second; ++Result.first) |
| 1075 | AddDeclRef(*Result.first, Record); |
| 1076 | } |
| 1077 | |
| 1078 | if (Record.size() == 0) |
| 1079 | return 0; |
| 1080 | |
| 1081 | S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record); |
| 1082 | return Offset; |
| 1083 | } |
| 1084 | |
| 1085 | /// \brief Write a block containing all of the declarations. |
| 1086 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1087 | // Enter the declarations block. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1088 | S.EnterSubblock(pch::DECLS_BLOCK_ID, 2); |
| 1089 | |
| 1090 | // Emit all of the declarations. |
| 1091 | RecordData Record; |
| 1092 | PCHDeclWriter W(*this, Record); |
| 1093 | while (!DeclsToEmit.empty()) { |
| 1094 | // Pull the next declaration off the queue |
| 1095 | Decl *D = DeclsToEmit.front(); |
| 1096 | DeclsToEmit.pop(); |
| 1097 | |
| 1098 | // If this declaration is also a DeclContext, write blocks for the |
| 1099 | // declarations that lexically stored inside its context and those |
| 1100 | // declarations that are visible from its context. These blocks |
| 1101 | // are written before the declaration itself so that we can put |
| 1102 | // their offsets into the record for the declaration. |
| 1103 | uint64_t LexicalOffset = 0; |
| 1104 | uint64_t VisibleOffset = 0; |
| 1105 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 1106 | if (DC) { |
| 1107 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 1108 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 1109 | } |
| 1110 | |
| 1111 | // Determine the ID for this declaration |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1112 | pch::DeclID ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1113 | if (ID == 0) |
| 1114 | ID = DeclIDs.size(); |
| 1115 | |
| 1116 | unsigned Index = ID - 1; |
| 1117 | |
| 1118 | // Record the offset for this declaration |
| 1119 | if (DeclOffsets.size() == Index) |
| 1120 | DeclOffsets.push_back(S.GetCurrentBitNo()); |
| 1121 | else if (DeclOffsets.size() < Index) { |
| 1122 | DeclOffsets.resize(Index+1); |
| 1123 | DeclOffsets[Index] = S.GetCurrentBitNo(); |
| 1124 | } |
| 1125 | |
| 1126 | // Build and emit a record for this declaration |
| 1127 | Record.clear(); |
| 1128 | W.Code = (pch::DeclCode)0; |
| 1129 | W.Visit(D); |
| 1130 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1131 | assert(W.Code && "Unhandled declaration kind while generating PCH"); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1132 | S.EmitRecord(W.Code, Record); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1133 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1134 | // Flush any expressions that were written as part of this declaration. |
| 1135 | FlushExprs(); |
| 1136 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1137 | // Note external declarations so that we can add them to a record |
| 1138 | // in the PCH file later. |
| 1139 | if (isa<FileScopeAsmDecl>(D)) |
| 1140 | ExternalDefinitions.push_back(ID); |
| 1141 | else if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 1142 | if (// Non-static file-scope variables with initializers or that |
| 1143 | // are tentative definitions. |
| 1144 | (Var->isFileVarDecl() && |
| 1145 | (Var->getInit() || Var->getStorageClass() == VarDecl::None)) || |
| 1146 | // Out-of-line definitions of static data members (C++). |
| 1147 | (Var->getDeclContext()->isRecord() && |
| 1148 | !Var->getLexicalDeclContext()->isRecord() && |
| 1149 | Var->getStorageClass() == VarDecl::Static)) |
| 1150 | ExternalDefinitions.push_back(ID); |
| 1151 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) { |
| 1152 | if (Func->isThisDeclarationADefinition() && |
| 1153 | Func->getStorageClass() != FunctionDecl::Static && |
| 1154 | !Func->isInline()) |
| 1155 | ExternalDefinitions.push_back(ID); |
| 1156 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | // Exit the declarations block |
| 1160 | S.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1163 | /// \brief Write the identifier table into the PCH file. |
| 1164 | /// |
| 1165 | /// The identifier table consists of a blob containing string data |
| 1166 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 1167 | /// that maps identifier IDs to locations within the blob. |
| 1168 | void PCHWriter::WriteIdentifierTable() { |
| 1169 | using namespace llvm; |
| 1170 | |
| 1171 | // Create and write out the blob that contains the identifier |
| 1172 | // strings. |
| 1173 | RecordData IdentOffsets; |
| 1174 | IdentOffsets.resize(IdentifierIDs.size()); |
| 1175 | { |
| 1176 | // Create the identifier string data. |
| 1177 | std::vector<char> Data; |
| 1178 | Data.push_back(0); // Data must not be empty. |
| 1179 | for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator |
| 1180 | ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); |
| 1181 | ID != IDEnd; ++ID) { |
| 1182 | assert(ID->first && "NULL identifier in identifier table"); |
| 1183 | |
| 1184 | // Make sure we're starting on an odd byte. The PCH reader |
| 1185 | // expects the low bit to be set on all of the offsets. |
| 1186 | if ((Data.size() & 0x01) == 0) |
| 1187 | Data.push_back((char)0); |
| 1188 | |
| 1189 | IdentOffsets[ID->second - 1] = Data.size(); |
| 1190 | Data.insert(Data.end(), |
| 1191 | ID->first->getName(), |
| 1192 | ID->first->getName() + ID->first->getLength()); |
| 1193 | Data.push_back((char)0); |
| 1194 | } |
| 1195 | |
| 1196 | // Create a blob abbreviation |
| 1197 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1198 | Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE)); |
| 1199 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
| 1200 | unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev); |
| 1201 | |
| 1202 | // Write the identifier table |
| 1203 | RecordData Record; |
| 1204 | Record.push_back(pch::IDENTIFIER_TABLE); |
| 1205 | S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size()); |
| 1206 | } |
| 1207 | |
| 1208 | // Write the offsets table for identifier IDs. |
| 1209 | S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets); |
| 1210 | } |
| 1211 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1212 | PCHWriter::PCHWriter(llvm::BitstreamWriter &S) |
| 1213 | : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { } |
| 1214 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1215 | void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1216 | // Emit the file header. |
| 1217 | S.Emit((unsigned)'C', 8); |
| 1218 | S.Emit((unsigned)'P', 8); |
| 1219 | S.Emit((unsigned)'C', 8); |
| 1220 | S.Emit((unsigned)'H', 8); |
| 1221 | |
| 1222 | // The translation unit is the first declaration we'll emit. |
| 1223 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
| 1224 | DeclsToEmit.push(Context.getTranslationUnitDecl()); |
| 1225 | |
| 1226 | // Write the remaining PCH contents. |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1227 | S.EnterSubblock(pch::PCH_BLOCK_ID, 3); |
| 1228 | WriteTargetTriple(Context.Target); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1229 | WriteLanguageOptions(Context.getLangOptions()); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1230 | WriteSourceManagerBlock(Context.getSourceManager()); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1231 | WritePreprocessor(PP); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1232 | WriteTypesBlock(Context); |
| 1233 | WriteDeclsBlock(Context); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1234 | WriteIdentifierTable(); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1235 | S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets); |
| 1236 | S.EmitRecord(pch::DECL_OFFSET, DeclOffsets); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1237 | if (!ExternalDefinitions.empty()) |
| 1238 | S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1239 | S.ExitBlock(); |
| 1240 | } |
| 1241 | |
| 1242 | void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { |
| 1243 | Record.push_back(Loc.getRawEncoding()); |
| 1244 | } |
| 1245 | |
| 1246 | void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { |
| 1247 | Record.push_back(Value.getBitWidth()); |
| 1248 | unsigned N = Value.getNumWords(); |
| 1249 | const uint64_t* Words = Value.getRawData(); |
| 1250 | for (unsigned I = 0; I != N; ++I) |
| 1251 | Record.push_back(Words[I]); |
| 1252 | } |
| 1253 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1254 | void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) { |
| 1255 | Record.push_back(Value.isUnsigned()); |
| 1256 | AddAPInt(Value, Record); |
| 1257 | } |
| 1258 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1259 | void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) { |
| 1260 | AddAPInt(Value.bitcastToAPInt(), Record); |
| 1261 | } |
| 1262 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1263 | void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1264 | if (II == 0) { |
| 1265 | Record.push_back(0); |
| 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | pch::IdentID &ID = IdentifierIDs[II]; |
| 1270 | if (ID == 0) |
| 1271 | ID = IdentifierIDs.size(); |
| 1272 | |
| 1273 | Record.push_back(ID); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | void PCHWriter::AddTypeRef(QualType T, RecordData &Record) { |
| 1277 | if (T.isNull()) { |
| 1278 | Record.push_back(pch::PREDEF_TYPE_NULL_ID); |
| 1279 | return; |
| 1280 | } |
| 1281 | |
| 1282 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1283 | pch::TypeID ID = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1284 | switch (BT->getKind()) { |
| 1285 | case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break; |
| 1286 | case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break; |
| 1287 | case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break; |
| 1288 | case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break; |
| 1289 | case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break; |
| 1290 | case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break; |
| 1291 | case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break; |
| 1292 | case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break; |
| 1293 | case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break; |
| 1294 | case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break; |
| 1295 | case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break; |
| 1296 | case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break; |
| 1297 | case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break; |
| 1298 | case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break; |
| 1299 | case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break; |
| 1300 | case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break; |
| 1301 | case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break; |
| 1302 | case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break; |
| 1303 | case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break; |
| 1304 | case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break; |
| 1305 | } |
| 1306 | |
| 1307 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1308 | return; |
| 1309 | } |
| 1310 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1311 | pch::TypeID &ID = TypeIDs[T.getTypePtr()]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1312 | if (ID == 0) // we haven't seen this type before |
| 1313 | ID = NextTypeID++; |
| 1314 | |
| 1315 | // Encode the type qualifiers in the type reference. |
| 1316 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1317 | } |
| 1318 | |
| 1319 | void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) { |
| 1320 | if (D == 0) { |
| 1321 | Record.push_back(0); |
| 1322 | return; |
| 1323 | } |
| 1324 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1325 | pch::DeclID &ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1326 | if (ID == 0) { |
| 1327 | // We haven't seen this declaration before. Give it a new ID and |
| 1328 | // enqueue it in the list of declarations to emit. |
| 1329 | ID = DeclIDs.size(); |
| 1330 | DeclsToEmit.push(const_cast<Decl *>(D)); |
| 1331 | } |
| 1332 | |
| 1333 | Record.push_back(ID); |
| 1334 | } |
| 1335 | |
| 1336 | void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { |
| 1337 | Record.push_back(Name.getNameKind()); |
| 1338 | switch (Name.getNameKind()) { |
| 1339 | case DeclarationName::Identifier: |
| 1340 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 1341 | break; |
| 1342 | |
| 1343 | case DeclarationName::ObjCZeroArgSelector: |
| 1344 | case DeclarationName::ObjCOneArgSelector: |
| 1345 | case DeclarationName::ObjCMultiArgSelector: |
| 1346 | assert(false && "Serialization of Objective-C selectors unavailable"); |
| 1347 | break; |
| 1348 | |
| 1349 | case DeclarationName::CXXConstructorName: |
| 1350 | case DeclarationName::CXXDestructorName: |
| 1351 | case DeclarationName::CXXConversionFunctionName: |
| 1352 | AddTypeRef(Name.getCXXNameType(), Record); |
| 1353 | break; |
| 1354 | |
| 1355 | case DeclarationName::CXXOperatorName: |
| 1356 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 1357 | break; |
| 1358 | |
| 1359 | case DeclarationName::CXXUsingDirective: |
| 1360 | // No extra data to emit |
| 1361 | break; |
| 1362 | } |
| 1363 | } |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1364 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1365 | /// \brief Write the given subexpression to the bitstream. |
| 1366 | void PCHWriter::WriteSubExpr(Expr *E) { |
| 1367 | RecordData Record; |
| 1368 | PCHStmtWriter Writer(*this, Record); |
| 1369 | |
| 1370 | if (!E) { |
| 1371 | S.EmitRecord(pch::EXPR_NULL, Record); |
| 1372 | return; |
| 1373 | } |
| 1374 | |
| 1375 | Writer.Code = pch::EXPR_NULL; |
| 1376 | Writer.Visit(E); |
| 1377 | assert(Writer.Code != pch::EXPR_NULL && |
| 1378 | "Unhandled expression writing PCH file"); |
| 1379 | S.EmitRecord(Writer.Code, Record); |
| 1380 | } |
| 1381 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1382 | /// \brief Flush all of the expressions that have been added to the |
| 1383 | /// queue via AddExpr(). |
| 1384 | void PCHWriter::FlushExprs() { |
| 1385 | RecordData Record; |
| 1386 | PCHStmtWriter Writer(*this, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1387 | |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1388 | for (unsigned I = 0, N = ExprsToEmit.size(); I != N; ++I) { |
| 1389 | Expr *E = ExprsToEmit[I]; |
| 1390 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1391 | if (!E) { |
| 1392 | S.EmitRecord(pch::EXPR_NULL, Record); |
| 1393 | continue; |
| 1394 | } |
| 1395 | |
| 1396 | Writer.Code = pch::EXPR_NULL; |
| 1397 | Writer.Visit(E); |
| 1398 | assert(Writer.Code != pch::EXPR_NULL && |
| 1399 | "Unhandled expression writing PCH file"); |
| 1400 | S.EmitRecord(Writer.Code, Record); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1401 | |
| 1402 | assert(N == ExprsToEmit.size() && |
| 1403 | "Subexpression writen via AddExpr rather than WriteSubExpr!"); |
| 1404 | |
| 1405 | // Note that we are at the end of a full expression. Any |
| 1406 | // expression records that follow this one are part of a different |
| 1407 | // expression. |
| 1408 | Record.clear(); |
| 1409 | S.EmitRecord(pch::EXPR_STOP, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1410 | } |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1411 | |
| 1412 | ExprsToEmit.clear(); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1413 | } |