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