Douglas Gregor | c34897d | 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 | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 22 | #include "clang/Lex/MacroInfo.h" |
| 23 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 24 | #include "clang/Basic/FileManager.h" |
| 25 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/APFloat.h" |
| 29 | #include "llvm/ADT/APInt.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 30 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 31 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 64b65f8 | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 33 | #include <cstdio> |
Douglas Gregor | c34897d | 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 | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 129 | Writer.AddStmt(T->getSizeExpr()); |
Douglas Gregor | c34897d | 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 | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 169 | Writer.AddStmt(T->getUnderlyingExpr()); |
Douglas Gregor | c34897d | 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 | 3c8ff3e | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 197 | // FIXME: Serialize this type (C++ only) |
Douglas Gregor | c34897d | 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 | 3c8ff3e | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 202 | // FIXME: Serialize this type (C++ only) |
Douglas Gregor | c34897d | 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 | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 257 | void VisitTagDecl(TagDecl *D); |
| 258 | void VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 259 | void VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 260 | void VisitValueDecl(ValueDecl *D); |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 261 | void VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 262 | void VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 263 | void VisitFieldDecl(FieldDecl *D); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 264 | void VisitVarDecl(VarDecl *D); |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 265 | void VisitParmVarDecl(ParmVarDecl *D); |
| 266 | void VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
Douglas Gregor | 2a49179 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 267 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
| 268 | void VisitBlockDecl(BlockDecl *D); |
Douglas Gregor | c34897d | 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 | 1c50788 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 279 | Record.push_back(D->hasAttrs()); |
Douglas Gregor | c34897d | 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 | 47f1b2c | 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 | 982365e | 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 | c34897d | 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 | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 330 | void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 331 | VisitValueDecl(D); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 332 | Record.push_back(D->getInitExpr()? 1 : 0); |
| 333 | if (D->getInitExpr()) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 334 | Writer.AddStmt(D->getInitExpr()); |
Douglas Gregor | 47f1b2c | 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 | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 339 | void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) { |
| 340 | VisitValueDecl(D); |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 341 | Record.push_back(D->isThisDeclarationADefinition()); |
| 342 | if (D->isThisDeclarationADefinition()) |
| 343 | Writer.AddStmt(D->getBody()); |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 344 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 345 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
| 346 | Record.push_back(D->isInline()); |
| 347 | Record.push_back(D->isVirtual()); |
| 348 | Record.push_back(D->isPure()); |
| 349 | Record.push_back(D->inheritedPrototype()); |
| 350 | Record.push_back(D->hasPrototype() && !D->inheritedPrototype()); |
| 351 | Record.push_back(D->isDeleted()); |
| 352 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
| 353 | Record.push_back(D->param_size()); |
| 354 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 355 | P != PEnd; ++P) |
| 356 | Writer.AddDeclRef(*P, Record); |
| 357 | Code = pch::DECL_FUNCTION; |
| 358 | } |
| 359 | |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 360 | void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) { |
| 361 | VisitValueDecl(D); |
| 362 | Record.push_back(D->isMutable()); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 363 | Record.push_back(D->getBitWidth()? 1 : 0); |
| 364 | if (D->getBitWidth()) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 365 | Writer.AddStmt(D->getBitWidth()); |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 366 | Code = pch::DECL_FIELD; |
| 367 | } |
| 368 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 369 | void PCHDeclWriter::VisitVarDecl(VarDecl *D) { |
| 370 | VisitValueDecl(D); |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 371 | Record.push_back(D->getStorageClass()); // FIXME: stable encoding |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 372 | Record.push_back(D->isThreadSpecified()); |
| 373 | Record.push_back(D->hasCXXDirectInitializer()); |
| 374 | Record.push_back(D->isDeclaredInCondition()); |
| 375 | Writer.AddDeclRef(D->getPreviousDeclaration(), Record); |
| 376 | Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 377 | Record.push_back(D->getInit()? 1 : 0); |
| 378 | if (D->getInit()) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 379 | Writer.AddStmt(D->getInit()); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 380 | Code = pch::DECL_VAR; |
| 381 | } |
| 382 | |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 383 | void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { |
| 384 | VisitVarDecl(D); |
| 385 | Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding |
Douglas Gregor | 3c8ff3e | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 386 | // FIXME: emit default argument (C++) |
Douglas Gregor | 23ce3a5 | 2009-04-13 22:18:37 +0000 | [diff] [blame] | 387 | // FIXME: why isn't the "default argument" just stored as the initializer |
| 388 | // in VarDecl? |
| 389 | Code = pch::DECL_PARM_VAR; |
| 390 | } |
| 391 | |
| 392 | void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 393 | VisitParmVarDecl(D); |
| 394 | Writer.AddTypeRef(D->getOriginalType(), Record); |
| 395 | Code = pch::DECL_ORIGINAL_PARM_VAR; |
| 396 | } |
| 397 | |
Douglas Gregor | 2a49179 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 398 | void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 399 | VisitDecl(D); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 400 | Writer.AddStmt(D->getAsmString()); |
Douglas Gregor | 2a49179 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 401 | Code = pch::DECL_FILE_SCOPE_ASM; |
| 402 | } |
| 403 | |
| 404 | void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) { |
| 405 | VisitDecl(D); |
Douglas Gregor | e246b74 | 2009-04-17 19:21:43 +0000 | [diff] [blame^] | 406 | Writer.AddStmt(D->getBody()); |
Douglas Gregor | 2a49179 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 407 | Record.push_back(D->param_size()); |
| 408 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 409 | P != PEnd; ++P) |
| 410 | Writer.AddDeclRef(*P, Record); |
| 411 | Code = pch::DECL_BLOCK; |
| 412 | } |
| 413 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 414 | /// \brief Emit the DeclContext part of a declaration context decl. |
| 415 | /// |
| 416 | /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL |
| 417 | /// block for this declaration context is stored. May be 0 to indicate |
| 418 | /// that there are no declarations stored within this context. |
| 419 | /// |
| 420 | /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE |
| 421 | /// block for this declaration context is stored. May be 0 to indicate |
| 422 | /// that there are no declarations visible from this context. Note |
| 423 | /// that this value will not be emitted for non-primary declaration |
| 424 | /// contexts. |
| 425 | void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, |
| 426 | uint64_t VisibleOffset) { |
| 427 | Record.push_back(LexicalOffset); |
| 428 | if (DC->getPrimaryContext() == DC) |
| 429 | Record.push_back(VisibleOffset); |
| 430 | } |
| 431 | |
| 432 | //===----------------------------------------------------------------------===// |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 433 | // Statement/expression serialization |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | namespace { |
| 436 | class VISIBILITY_HIDDEN PCHStmtWriter |
| 437 | : public StmtVisitor<PCHStmtWriter, void> { |
| 438 | |
| 439 | PCHWriter &Writer; |
| 440 | PCHWriter::RecordData &Record; |
| 441 | |
| 442 | public: |
| 443 | pch::StmtCode Code; |
| 444 | |
| 445 | PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
| 446 | : Writer(Writer), Record(Record) { } |
| 447 | |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 448 | void VisitStmt(Stmt *S); |
| 449 | void VisitNullStmt(NullStmt *S); |
| 450 | void VisitCompoundStmt(CompoundStmt *S); |
| 451 | void VisitSwitchCase(SwitchCase *S); |
| 452 | void VisitCaseStmt(CaseStmt *S); |
| 453 | void VisitDefaultStmt(DefaultStmt *S); |
Douglas Gregor | 6e411bf | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 454 | void VisitLabelStmt(LabelStmt *S); |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 455 | void VisitIfStmt(IfStmt *S); |
| 456 | void VisitSwitchStmt(SwitchStmt *S); |
Douglas Gregor | a6b503f | 2009-04-17 00:16:09 +0000 | [diff] [blame] | 457 | void VisitWhileStmt(WhileStmt *S); |
Douglas Gregor | fb5f25b | 2009-04-17 00:29:51 +0000 | [diff] [blame] | 458 | void VisitDoStmt(DoStmt *S); |
| 459 | void VisitForStmt(ForStmt *S); |
Douglas Gregor | 6e411bf | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 460 | void VisitGotoStmt(GotoStmt *S); |
Douglas Gregor | 95a8fe3 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 461 | void VisitIndirectGotoStmt(IndirectGotoStmt *S); |
Douglas Gregor | a6b503f | 2009-04-17 00:16:09 +0000 | [diff] [blame] | 462 | void VisitContinueStmt(ContinueStmt *S); |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 463 | void VisitBreakStmt(BreakStmt *S); |
Douglas Gregor | 22d2dcd | 2009-04-17 16:34:57 +0000 | [diff] [blame] | 464 | void VisitReturnStmt(ReturnStmt *S); |
Douglas Gregor | 78ff29f | 2009-04-17 16:55:36 +0000 | [diff] [blame] | 465 | void VisitDeclStmt(DeclStmt *S); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 466 | void VisitExpr(Expr *E); |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 467 | void VisitPredefinedExpr(PredefinedExpr *E); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 468 | void VisitDeclRefExpr(DeclRefExpr *E); |
| 469 | void VisitIntegerLiteral(IntegerLiteral *E); |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 470 | void VisitFloatingLiteral(FloatingLiteral *E); |
Douglas Gregor | 21ddd8c | 2009-04-15 22:19:53 +0000 | [diff] [blame] | 471 | void VisitImaginaryLiteral(ImaginaryLiteral *E); |
Douglas Gregor | 596e093 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 472 | void VisitStringLiteral(StringLiteral *E); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 473 | void VisitCharacterLiteral(CharacterLiteral *E); |
Douglas Gregor | 4ea0b1f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 474 | void VisitParenExpr(ParenExpr *E); |
Douglas Gregor | 12d7405 | 2009-04-15 15:58:59 +0000 | [diff] [blame] | 475 | void VisitUnaryOperator(UnaryOperator *E); |
| 476 | void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); |
Douglas Gregor | 21ddd8c | 2009-04-15 22:19:53 +0000 | [diff] [blame] | 477 | void VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Douglas Gregor | 7e2b1cd | 2009-04-15 17:43:59 +0000 | [diff] [blame] | 478 | void VisitCallExpr(CallExpr *E); |
| 479 | void VisitMemberExpr(MemberExpr *E); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 480 | void VisitCastExpr(CastExpr *E); |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 481 | void VisitBinaryOperator(BinaryOperator *E); |
Douglas Gregor | c599bbf | 2009-04-15 22:40:36 +0000 | [diff] [blame] | 482 | void VisitCompoundAssignOperator(CompoundAssignOperator *E); |
| 483 | void VisitConditionalOperator(ConditionalOperator *E); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 484 | void VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 485 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 486 | void VisitCStyleCastExpr(CStyleCastExpr *E); |
Douglas Gregor | b70b48f | 2009-04-16 02:33:48 +0000 | [diff] [blame] | 487 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Douglas Gregor | ec0b829 | 2009-04-15 23:02:49 +0000 | [diff] [blame] | 488 | void VisitExtVectorElementExpr(ExtVectorElementExpr *E); |
Douglas Gregor | 6710a3c | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 489 | void VisitInitListExpr(InitListExpr *E); |
| 490 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
| 491 | void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); |
Douglas Gregor | ec0b829 | 2009-04-15 23:02:49 +0000 | [diff] [blame] | 492 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 95a8fe3 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 493 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Douglas Gregor | eca12f6 | 2009-04-17 19:05:30 +0000 | [diff] [blame] | 494 | void VisitStmtExpr(StmtExpr *E); |
Douglas Gregor | 209d462 | 2009-04-15 23:33:31 +0000 | [diff] [blame] | 495 | void VisitTypesCompatibleExpr(TypesCompatibleExpr *E); |
| 496 | void VisitChooseExpr(ChooseExpr *E); |
| 497 | void VisitGNUNullExpr(GNUNullExpr *E); |
Douglas Gregor | 725e94b | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 498 | void VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
Douglas Gregor | e246b74 | 2009-04-17 19:21:43 +0000 | [diff] [blame^] | 499 | void VisitBlockExpr(BlockExpr *E); |
Douglas Gregor | 725e94b | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 500 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 501 | }; |
| 502 | } |
| 503 | |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 504 | void PCHStmtWriter::VisitStmt(Stmt *S) { |
| 505 | } |
| 506 | |
| 507 | void PCHStmtWriter::VisitNullStmt(NullStmt *S) { |
| 508 | VisitStmt(S); |
| 509 | Writer.AddSourceLocation(S->getSemiLoc(), Record); |
| 510 | Code = pch::STMT_NULL; |
| 511 | } |
| 512 | |
| 513 | void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) { |
| 514 | VisitStmt(S); |
| 515 | Record.push_back(S->size()); |
| 516 | for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end(); |
| 517 | CS != CSEnd; ++CS) |
| 518 | Writer.WriteSubStmt(*CS); |
| 519 | Writer.AddSourceLocation(S->getLBracLoc(), Record); |
| 520 | Writer.AddSourceLocation(S->getRBracLoc(), Record); |
| 521 | Code = pch::STMT_COMPOUND; |
| 522 | } |
| 523 | |
| 524 | void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) { |
| 525 | VisitStmt(S); |
| 526 | Record.push_back(Writer.RecordSwitchCaseID(S)); |
| 527 | } |
| 528 | |
| 529 | void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) { |
| 530 | VisitSwitchCase(S); |
| 531 | Writer.WriteSubStmt(S->getLHS()); |
| 532 | Writer.WriteSubStmt(S->getRHS()); |
| 533 | Writer.WriteSubStmt(S->getSubStmt()); |
| 534 | Writer.AddSourceLocation(S->getCaseLoc(), Record); |
| 535 | Code = pch::STMT_CASE; |
| 536 | } |
| 537 | |
| 538 | void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) { |
| 539 | VisitSwitchCase(S); |
| 540 | Writer.WriteSubStmt(S->getSubStmt()); |
| 541 | Writer.AddSourceLocation(S->getDefaultLoc(), Record); |
| 542 | Code = pch::STMT_DEFAULT; |
| 543 | } |
| 544 | |
Douglas Gregor | 6e411bf | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 545 | void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) { |
| 546 | VisitStmt(S); |
| 547 | Writer.AddIdentifierRef(S->getID(), Record); |
| 548 | Writer.WriteSubStmt(S->getSubStmt()); |
| 549 | Writer.AddSourceLocation(S->getIdentLoc(), Record); |
| 550 | Record.push_back(Writer.GetLabelID(S)); |
| 551 | Code = pch::STMT_LABEL; |
| 552 | } |
| 553 | |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 554 | void PCHStmtWriter::VisitIfStmt(IfStmt *S) { |
| 555 | VisitStmt(S); |
| 556 | Writer.WriteSubStmt(S->getCond()); |
| 557 | Writer.WriteSubStmt(S->getThen()); |
| 558 | Writer.WriteSubStmt(S->getElse()); |
| 559 | Writer.AddSourceLocation(S->getIfLoc(), Record); |
| 560 | Code = pch::STMT_IF; |
| 561 | } |
| 562 | |
| 563 | void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) { |
| 564 | VisitStmt(S); |
| 565 | Writer.WriteSubStmt(S->getCond()); |
| 566 | Writer.WriteSubStmt(S->getBody()); |
| 567 | Writer.AddSourceLocation(S->getSwitchLoc(), Record); |
| 568 | for (SwitchCase *SC = S->getSwitchCaseList(); SC; |
| 569 | SC = SC->getNextSwitchCase()) |
| 570 | Record.push_back(Writer.getSwitchCaseID(SC)); |
| 571 | Code = pch::STMT_SWITCH; |
| 572 | } |
| 573 | |
Douglas Gregor | a6b503f | 2009-04-17 00:16:09 +0000 | [diff] [blame] | 574 | void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) { |
| 575 | VisitStmt(S); |
| 576 | Writer.WriteSubStmt(S->getCond()); |
| 577 | Writer.WriteSubStmt(S->getBody()); |
| 578 | Writer.AddSourceLocation(S->getWhileLoc(), Record); |
| 579 | Code = pch::STMT_WHILE; |
| 580 | } |
| 581 | |
Douglas Gregor | fb5f25b | 2009-04-17 00:29:51 +0000 | [diff] [blame] | 582 | void PCHStmtWriter::VisitDoStmt(DoStmt *S) { |
| 583 | VisitStmt(S); |
| 584 | Writer.WriteSubStmt(S->getCond()); |
| 585 | Writer.WriteSubStmt(S->getBody()); |
| 586 | Writer.AddSourceLocation(S->getDoLoc(), Record); |
| 587 | Code = pch::STMT_DO; |
| 588 | } |
| 589 | |
| 590 | void PCHStmtWriter::VisitForStmt(ForStmt *S) { |
| 591 | VisitStmt(S); |
| 592 | Writer.WriteSubStmt(S->getInit()); |
| 593 | Writer.WriteSubStmt(S->getCond()); |
| 594 | Writer.WriteSubStmt(S->getInc()); |
| 595 | Writer.WriteSubStmt(S->getBody()); |
| 596 | Writer.AddSourceLocation(S->getForLoc(), Record); |
| 597 | Code = pch::STMT_FOR; |
| 598 | } |
| 599 | |
Douglas Gregor | 6e411bf | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 600 | void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) { |
| 601 | VisitStmt(S); |
| 602 | Record.push_back(Writer.GetLabelID(S->getLabel())); |
| 603 | Writer.AddSourceLocation(S->getGotoLoc(), Record); |
| 604 | Writer.AddSourceLocation(S->getLabelLoc(), Record); |
| 605 | Code = pch::STMT_GOTO; |
| 606 | } |
| 607 | |
Douglas Gregor | 95a8fe3 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 608 | void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
| 609 | VisitStmt(S); |
| 610 | Writer.WriteSubStmt(S->getTarget()); |
| 611 | Code = pch::STMT_INDIRECT_GOTO; |
| 612 | } |
| 613 | |
Douglas Gregor | a6b503f | 2009-04-17 00:16:09 +0000 | [diff] [blame] | 614 | void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) { |
| 615 | VisitStmt(S); |
| 616 | Writer.AddSourceLocation(S->getContinueLoc(), Record); |
| 617 | Code = pch::STMT_CONTINUE; |
| 618 | } |
| 619 | |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 620 | void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) { |
| 621 | VisitStmt(S); |
| 622 | Writer.AddSourceLocation(S->getBreakLoc(), Record); |
| 623 | Code = pch::STMT_BREAK; |
| 624 | } |
| 625 | |
Douglas Gregor | 22d2dcd | 2009-04-17 16:34:57 +0000 | [diff] [blame] | 626 | void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) { |
| 627 | VisitStmt(S); |
| 628 | Writer.WriteSubStmt(S->getRetValue()); |
| 629 | Writer.AddSourceLocation(S->getReturnLoc(), Record); |
| 630 | Code = pch::STMT_RETURN; |
| 631 | } |
| 632 | |
Douglas Gregor | 78ff29f | 2009-04-17 16:55:36 +0000 | [diff] [blame] | 633 | void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) { |
| 634 | VisitStmt(S); |
| 635 | Writer.AddSourceLocation(S->getStartLoc(), Record); |
| 636 | Writer.AddSourceLocation(S->getEndLoc(), Record); |
| 637 | DeclGroupRef DG = S->getDeclGroup(); |
| 638 | for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D) |
| 639 | Writer.AddDeclRef(*D, Record); |
| 640 | Code = pch::STMT_DECL; |
| 641 | } |
| 642 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 643 | void PCHStmtWriter::VisitExpr(Expr *E) { |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 644 | VisitStmt(E); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 645 | Writer.AddTypeRef(E->getType(), Record); |
| 646 | Record.push_back(E->isTypeDependent()); |
| 647 | Record.push_back(E->isValueDependent()); |
| 648 | } |
| 649 | |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 650 | void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) { |
| 651 | VisitExpr(E); |
| 652 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 653 | Record.push_back(E->getIdentType()); // FIXME: stable encoding |
| 654 | Code = pch::EXPR_PREDEFINED; |
| 655 | } |
| 656 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 657 | void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) { |
| 658 | VisitExpr(E); |
| 659 | Writer.AddDeclRef(E->getDecl(), Record); |
| 660 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 661 | Code = pch::EXPR_DECL_REF; |
| 662 | } |
| 663 | |
| 664 | void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 665 | VisitExpr(E); |
| 666 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 667 | Writer.AddAPInt(E->getValue(), Record); |
| 668 | Code = pch::EXPR_INTEGER_LITERAL; |
| 669 | } |
| 670 | |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 671 | void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) { |
| 672 | VisitExpr(E); |
| 673 | Writer.AddAPFloat(E->getValue(), Record); |
| 674 | Record.push_back(E->isExact()); |
| 675 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 676 | Code = pch::EXPR_FLOATING_LITERAL; |
| 677 | } |
| 678 | |
Douglas Gregor | 21ddd8c | 2009-04-15 22:19:53 +0000 | [diff] [blame] | 679 | void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) { |
| 680 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 681 | Writer.WriteSubStmt(E->getSubExpr()); |
Douglas Gregor | 21ddd8c | 2009-04-15 22:19:53 +0000 | [diff] [blame] | 682 | Code = pch::EXPR_IMAGINARY_LITERAL; |
| 683 | } |
| 684 | |
Douglas Gregor | 596e093 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 685 | void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) { |
| 686 | VisitExpr(E); |
| 687 | Record.push_back(E->getByteLength()); |
| 688 | Record.push_back(E->getNumConcatenated()); |
| 689 | Record.push_back(E->isWide()); |
| 690 | // FIXME: String data should be stored as a blob at the end of the |
| 691 | // StringLiteral. However, we can't do so now because we have no |
| 692 | // provision for coping with abbreviations when we're jumping around |
| 693 | // the PCH file during deserialization. |
| 694 | Record.insert(Record.end(), |
| 695 | E->getStrData(), E->getStrData() + E->getByteLength()); |
| 696 | for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I) |
| 697 | Writer.AddSourceLocation(E->getStrTokenLoc(I), Record); |
| 698 | Code = pch::EXPR_STRING_LITERAL; |
| 699 | } |
| 700 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 701 | void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 702 | VisitExpr(E); |
| 703 | Record.push_back(E->getValue()); |
| 704 | Writer.AddSourceLocation(E->getLoc(), Record); |
| 705 | Record.push_back(E->isWide()); |
| 706 | Code = pch::EXPR_CHARACTER_LITERAL; |
| 707 | } |
| 708 | |
Douglas Gregor | 4ea0b1f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 709 | void PCHStmtWriter::VisitParenExpr(ParenExpr *E) { |
| 710 | VisitExpr(E); |
| 711 | Writer.AddSourceLocation(E->getLParen(), Record); |
| 712 | Writer.AddSourceLocation(E->getRParen(), Record); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 713 | Writer.WriteSubStmt(E->getSubExpr()); |
Douglas Gregor | 4ea0b1f | 2009-04-14 23:59:37 +0000 | [diff] [blame] | 714 | Code = pch::EXPR_PAREN; |
| 715 | } |
| 716 | |
Douglas Gregor | 12d7405 | 2009-04-15 15:58:59 +0000 | [diff] [blame] | 717 | void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) { |
| 718 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 719 | Writer.WriteSubStmt(E->getSubExpr()); |
Douglas Gregor | 12d7405 | 2009-04-15 15:58:59 +0000 | [diff] [blame] | 720 | Record.push_back(E->getOpcode()); // FIXME: stable encoding |
| 721 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 722 | Code = pch::EXPR_UNARY_OPERATOR; |
| 723 | } |
| 724 | |
| 725 | void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { |
| 726 | VisitExpr(E); |
| 727 | Record.push_back(E->isSizeOf()); |
| 728 | if (E->isArgumentType()) |
| 729 | Writer.AddTypeRef(E->getArgumentType(), Record); |
| 730 | else { |
| 731 | Record.push_back(0); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 732 | Writer.WriteSubStmt(E->getArgumentExpr()); |
Douglas Gregor | 12d7405 | 2009-04-15 15:58:59 +0000 | [diff] [blame] | 733 | } |
| 734 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 735 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 736 | Code = pch::EXPR_SIZEOF_ALIGN_OF; |
| 737 | } |
| 738 | |
Douglas Gregor | 21ddd8c | 2009-04-15 22:19:53 +0000 | [diff] [blame] | 739 | void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 740 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 741 | Writer.WriteSubStmt(E->getLHS()); |
| 742 | Writer.WriteSubStmt(E->getRHS()); |
Douglas Gregor | 21ddd8c | 2009-04-15 22:19:53 +0000 | [diff] [blame] | 743 | Writer.AddSourceLocation(E->getRBracketLoc(), Record); |
| 744 | Code = pch::EXPR_ARRAY_SUBSCRIPT; |
| 745 | } |
| 746 | |
Douglas Gregor | 7e2b1cd | 2009-04-15 17:43:59 +0000 | [diff] [blame] | 747 | void PCHStmtWriter::VisitCallExpr(CallExpr *E) { |
| 748 | VisitExpr(E); |
| 749 | Record.push_back(E->getNumArgs()); |
| 750 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 751 | Writer.WriteSubStmt(E->getCallee()); |
Douglas Gregor | 7e2b1cd | 2009-04-15 17:43:59 +0000 | [diff] [blame] | 752 | for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end(); |
| 753 | Arg != ArgEnd; ++Arg) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 754 | Writer.WriteSubStmt(*Arg); |
Douglas Gregor | 7e2b1cd | 2009-04-15 17:43:59 +0000 | [diff] [blame] | 755 | Code = pch::EXPR_CALL; |
| 756 | } |
| 757 | |
| 758 | void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) { |
| 759 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 760 | Writer.WriteSubStmt(E->getBase()); |
Douglas Gregor | 7e2b1cd | 2009-04-15 17:43:59 +0000 | [diff] [blame] | 761 | Writer.AddDeclRef(E->getMemberDecl(), Record); |
| 762 | Writer.AddSourceLocation(E->getMemberLoc(), Record); |
| 763 | Record.push_back(E->isArrow()); |
| 764 | Code = pch::EXPR_MEMBER; |
| 765 | } |
| 766 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 767 | void PCHStmtWriter::VisitCastExpr(CastExpr *E) { |
| 768 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 769 | Writer.WriteSubStmt(E->getSubExpr()); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 772 | void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) { |
| 773 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 774 | Writer.WriteSubStmt(E->getLHS()); |
| 775 | Writer.WriteSubStmt(E->getRHS()); |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 776 | Record.push_back(E->getOpcode()); // FIXME: stable encoding |
| 777 | Writer.AddSourceLocation(E->getOperatorLoc(), Record); |
| 778 | Code = pch::EXPR_BINARY_OPERATOR; |
| 779 | } |
| 780 | |
Douglas Gregor | c599bbf | 2009-04-15 22:40:36 +0000 | [diff] [blame] | 781 | void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 782 | VisitBinaryOperator(E); |
| 783 | Writer.AddTypeRef(E->getComputationLHSType(), Record); |
| 784 | Writer.AddTypeRef(E->getComputationResultType(), Record); |
| 785 | Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR; |
| 786 | } |
| 787 | |
| 788 | void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) { |
| 789 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 790 | Writer.WriteSubStmt(E->getCond()); |
| 791 | Writer.WriteSubStmt(E->getLHS()); |
| 792 | Writer.WriteSubStmt(E->getRHS()); |
Douglas Gregor | c599bbf | 2009-04-15 22:40:36 +0000 | [diff] [blame] | 793 | Code = pch::EXPR_CONDITIONAL_OPERATOR; |
| 794 | } |
| 795 | |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 796 | void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 797 | VisitCastExpr(E); |
| 798 | Record.push_back(E->isLvalueCast()); |
| 799 | Code = pch::EXPR_IMPLICIT_CAST; |
| 800 | } |
| 801 | |
Douglas Gregor | c75d0cb | 2009-04-15 00:25:59 +0000 | [diff] [blame] | 802 | void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 803 | VisitCastExpr(E); |
| 804 | Writer.AddTypeRef(E->getTypeAsWritten(), Record); |
| 805 | } |
| 806 | |
| 807 | void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 808 | VisitExplicitCastExpr(E); |
| 809 | Writer.AddSourceLocation(E->getLParenLoc(), Record); |
| 810 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 811 | Code = pch::EXPR_CSTYLE_CAST; |
| 812 | } |
| 813 | |
Douglas Gregor | b70b48f | 2009-04-16 02:33:48 +0000 | [diff] [blame] | 814 | void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 815 | VisitExpr(E); |
| 816 | Writer.AddSourceLocation(E->getLParenLoc(), Record); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 817 | Writer.WriteSubStmt(E->getInitializer()); |
Douglas Gregor | b70b48f | 2009-04-16 02:33:48 +0000 | [diff] [blame] | 818 | Record.push_back(E->isFileScope()); |
| 819 | Code = pch::EXPR_COMPOUND_LITERAL; |
| 820 | } |
| 821 | |
Douglas Gregor | ec0b829 | 2009-04-15 23:02:49 +0000 | [diff] [blame] | 822 | void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) { |
| 823 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 824 | Writer.WriteSubStmt(E->getBase()); |
Douglas Gregor | ec0b829 | 2009-04-15 23:02:49 +0000 | [diff] [blame] | 825 | Writer.AddIdentifierRef(&E->getAccessor(), Record); |
| 826 | Writer.AddSourceLocation(E->getAccessorLoc(), Record); |
| 827 | Code = pch::EXPR_EXT_VECTOR_ELEMENT; |
| 828 | } |
| 829 | |
Douglas Gregor | 6710a3c | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 830 | void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) { |
| 831 | VisitExpr(E); |
| 832 | Record.push_back(E->getNumInits()); |
| 833 | for (unsigned I = 0, N = E->getNumInits(); I != N; ++I) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 834 | Writer.WriteSubStmt(E->getInit(I)); |
| 835 | Writer.WriteSubStmt(E->getSyntacticForm()); |
Douglas Gregor | 6710a3c | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 836 | Writer.AddSourceLocation(E->getLBraceLoc(), Record); |
| 837 | Writer.AddSourceLocation(E->getRBraceLoc(), Record); |
| 838 | Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record); |
| 839 | Record.push_back(E->hadArrayRangeDesignator()); |
| 840 | Code = pch::EXPR_INIT_LIST; |
| 841 | } |
| 842 | |
| 843 | void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 844 | VisitExpr(E); |
| 845 | Record.push_back(E->getNumSubExprs()); |
| 846 | for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 847 | Writer.WriteSubStmt(E->getSubExpr(I)); |
Douglas Gregor | 6710a3c | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 848 | Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record); |
| 849 | Record.push_back(E->usesGNUSyntax()); |
| 850 | for (DesignatedInitExpr::designators_iterator D = E->designators_begin(), |
| 851 | DEnd = E->designators_end(); |
| 852 | D != DEnd; ++D) { |
| 853 | if (D->isFieldDesignator()) { |
| 854 | if (FieldDecl *Field = D->getField()) { |
| 855 | Record.push_back(pch::DESIG_FIELD_DECL); |
| 856 | Writer.AddDeclRef(Field, Record); |
| 857 | } else { |
| 858 | Record.push_back(pch::DESIG_FIELD_NAME); |
| 859 | Writer.AddIdentifierRef(D->getFieldName(), Record); |
| 860 | } |
| 861 | Writer.AddSourceLocation(D->getDotLoc(), Record); |
| 862 | Writer.AddSourceLocation(D->getFieldLoc(), Record); |
| 863 | } else if (D->isArrayDesignator()) { |
| 864 | Record.push_back(pch::DESIG_ARRAY); |
| 865 | Record.push_back(D->getFirstExprIndex()); |
| 866 | Writer.AddSourceLocation(D->getLBracketLoc(), Record); |
| 867 | Writer.AddSourceLocation(D->getRBracketLoc(), Record); |
| 868 | } else { |
| 869 | assert(D->isArrayRangeDesignator() && "Unknown designator"); |
| 870 | Record.push_back(pch::DESIG_ARRAY_RANGE); |
| 871 | Record.push_back(D->getFirstExprIndex()); |
| 872 | Writer.AddSourceLocation(D->getLBracketLoc(), Record); |
| 873 | Writer.AddSourceLocation(D->getEllipsisLoc(), Record); |
| 874 | Writer.AddSourceLocation(D->getRBracketLoc(), Record); |
| 875 | } |
| 876 | } |
| 877 | Code = pch::EXPR_DESIGNATED_INIT; |
| 878 | } |
| 879 | |
| 880 | void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
| 881 | VisitExpr(E); |
| 882 | Code = pch::EXPR_IMPLICIT_VALUE_INIT; |
| 883 | } |
| 884 | |
Douglas Gregor | ec0b829 | 2009-04-15 23:02:49 +0000 | [diff] [blame] | 885 | void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) { |
| 886 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 887 | Writer.WriteSubStmt(E->getSubExpr()); |
Douglas Gregor | ec0b829 | 2009-04-15 23:02:49 +0000 | [diff] [blame] | 888 | Writer.AddSourceLocation(E->getBuiltinLoc(), Record); |
| 889 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 890 | Code = pch::EXPR_VA_ARG; |
| 891 | } |
| 892 | |
Douglas Gregor | 95a8fe3 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 893 | void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 894 | VisitExpr(E); |
| 895 | Writer.AddSourceLocation(E->getAmpAmpLoc(), Record); |
| 896 | Writer.AddSourceLocation(E->getLabelLoc(), Record); |
| 897 | Record.push_back(Writer.GetLabelID(E->getLabel())); |
| 898 | Code = pch::EXPR_ADDR_LABEL; |
| 899 | } |
| 900 | |
Douglas Gregor | eca12f6 | 2009-04-17 19:05:30 +0000 | [diff] [blame] | 901 | void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) { |
| 902 | VisitExpr(E); |
| 903 | Writer.WriteSubStmt(E->getSubStmt()); |
| 904 | Writer.AddSourceLocation(E->getLParenLoc(), Record); |
| 905 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 906 | Code = pch::EXPR_STMT; |
| 907 | } |
| 908 | |
Douglas Gregor | 209d462 | 2009-04-15 23:33:31 +0000 | [diff] [blame] | 909 | void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) { |
| 910 | VisitExpr(E); |
| 911 | Writer.AddTypeRef(E->getArgType1(), Record); |
| 912 | Writer.AddTypeRef(E->getArgType2(), Record); |
| 913 | Writer.AddSourceLocation(E->getBuiltinLoc(), Record); |
| 914 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 915 | Code = pch::EXPR_TYPES_COMPATIBLE; |
| 916 | } |
| 917 | |
| 918 | void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) { |
| 919 | VisitExpr(E); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 920 | Writer.WriteSubStmt(E->getCond()); |
| 921 | Writer.WriteSubStmt(E->getLHS()); |
| 922 | Writer.WriteSubStmt(E->getRHS()); |
Douglas Gregor | 209d462 | 2009-04-15 23:33:31 +0000 | [diff] [blame] | 923 | Writer.AddSourceLocation(E->getBuiltinLoc(), Record); |
| 924 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 925 | Code = pch::EXPR_CHOOSE; |
| 926 | } |
| 927 | |
| 928 | void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) { |
| 929 | VisitExpr(E); |
| 930 | Writer.AddSourceLocation(E->getTokenLocation(), Record); |
| 931 | Code = pch::EXPR_GNU_NULL; |
| 932 | } |
| 933 | |
Douglas Gregor | 725e94b | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 934 | void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
| 935 | VisitExpr(E); |
| 936 | Record.push_back(E->getNumSubExprs()); |
| 937 | for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 938 | Writer.WriteSubStmt(E->getExpr(I)); |
Douglas Gregor | 725e94b | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 939 | Writer.AddSourceLocation(E->getBuiltinLoc(), Record); |
| 940 | Writer.AddSourceLocation(E->getRParenLoc(), Record); |
| 941 | Code = pch::EXPR_SHUFFLE_VECTOR; |
| 942 | } |
| 943 | |
Douglas Gregor | e246b74 | 2009-04-17 19:21:43 +0000 | [diff] [blame^] | 944 | void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) { |
| 945 | VisitExpr(E); |
| 946 | Writer.AddDeclRef(E->getBlockDecl(), Record); |
| 947 | Record.push_back(E->hasBlockDeclRefExprs()); |
| 948 | Code = pch::EXPR_BLOCK; |
| 949 | } |
| 950 | |
Douglas Gregor | 725e94b | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 951 | void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 952 | VisitExpr(E); |
| 953 | Writer.AddDeclRef(E->getDecl(), Record); |
| 954 | Writer.AddSourceLocation(E->getLocation(), Record); |
| 955 | Record.push_back(E->isByRef()); |
| 956 | Code = pch::EXPR_BLOCK_DECL_REF; |
| 957 | } |
| 958 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 959 | //===----------------------------------------------------------------------===// |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 960 | // PCHWriter Implementation |
| 961 | //===----------------------------------------------------------------------===// |
| 962 | |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 963 | /// \brief Write the target triple (e.g., i686-apple-darwin9). |
| 964 | void PCHWriter::WriteTargetTriple(const TargetInfo &Target) { |
| 965 | using namespace llvm; |
| 966 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 967 | Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE)); |
| 968 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 969 | unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 970 | |
| 971 | RecordData Record; |
| 972 | Record.push_back(pch::TARGET_TRIPLE); |
| 973 | const char *Triple = Target.getTargetTriple(); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 974 | Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple)); |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /// \brief Write the LangOptions structure. |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 978 | void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { |
| 979 | RecordData Record; |
| 980 | Record.push_back(LangOpts.Trigraphs); |
| 981 | Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments. |
| 982 | Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers. |
| 983 | Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode. |
| 984 | Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc) |
| 985 | Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'. |
| 986 | Record.push_back(LangOpts.Digraphs); // C94, C99 and C++ |
| 987 | Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants. |
| 988 | Record.push_back(LangOpts.C99); // C99 Support |
| 989 | Record.push_back(LangOpts.Microsoft); // Microsoft extensions. |
| 990 | Record.push_back(LangOpts.CPlusPlus); // C++ Support |
| 991 | Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support |
| 992 | Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode. |
| 993 | Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords. |
| 994 | |
| 995 | Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled. |
| 996 | Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled. |
| 997 | Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled |
| 998 | |
| 999 | Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings |
| 1000 | Record.push_back(LangOpts.Boolean); // Allow bool/true/false |
| 1001 | Record.push_back(LangOpts.WritableStrings); // Allow writable strings |
| 1002 | Record.push_back(LangOpts.LaxVectorConversions); |
| 1003 | Record.push_back(LangOpts.Exceptions); // Support exception handling. |
| 1004 | |
| 1005 | Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime. |
| 1006 | Record.push_back(LangOpts.Freestanding); // Freestanding implementation |
| 1007 | Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin) |
| 1008 | |
| 1009 | Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected |
| 1010 | // by locks. |
| 1011 | Record.push_back(LangOpts.Blocks); // block extension to C |
| 1012 | Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if |
| 1013 | // they are unused. |
| 1014 | Record.push_back(LangOpts.MathErrno); // Math functions must respect errno |
| 1015 | // (modulo the platform support). |
| 1016 | |
| 1017 | Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when |
| 1018 | // signed integer arithmetic overflows. |
| 1019 | |
| 1020 | Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and |
| 1021 | // may be ripped out at any time. |
| 1022 | |
| 1023 | Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. |
| 1024 | Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be |
| 1025 | // defined. |
| 1026 | Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as |
| 1027 | // opposed to __DYNAMIC__). |
| 1028 | Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero. |
| 1029 | |
| 1030 | Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be |
| 1031 | // used (instead of C99 semantics). |
| 1032 | Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined. |
| 1033 | Record.push_back(LangOpts.getGCMode()); |
| 1034 | Record.push_back(LangOpts.getVisibilityMode()); |
| 1035 | Record.push_back(LangOpts.InstantiationDepth); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1036 | Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record); |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1039 | //===----------------------------------------------------------------------===// |
| 1040 | // Source Manager Serialization |
| 1041 | //===----------------------------------------------------------------------===// |
| 1042 | |
| 1043 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1044 | /// file. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1045 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1046 | using namespace llvm; |
| 1047 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1048 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY)); |
| 1049 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1050 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 1051 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 1052 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1053 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1054 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1058 | /// buffer. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1059 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1060 | using namespace llvm; |
| 1061 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1062 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY)); |
| 1063 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1064 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 1065 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 1066 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 1067 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1068 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1072 | /// buffer's blob. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1073 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1074 | using namespace llvm; |
| 1075 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1076 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB)); |
| 1077 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1078 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /// \brief Create an abbreviation for the SLocEntry that refers to an |
| 1082 | /// buffer. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1083 | static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1084 | using namespace llvm; |
| 1085 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1086 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY)); |
| 1087 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1088 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 1089 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 1090 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
Douglas Gregor | 364e580 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 1091 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1092 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /// \brief Writes the block containing the serialized form of the |
| 1096 | /// source manager. |
| 1097 | /// |
| 1098 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 1099 | /// blob), indexed based on the file name, so that we only create |
| 1100 | /// entries for files that we actually need. In the common case (no |
| 1101 | /// errors), we probably won't have to create file entries for any of |
| 1102 | /// the files in the AST. |
| 1103 | void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) { |
Chris Lattner | 84b04f1 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1104 | // Enter the source manager block. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1105 | Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Abbreviations for the various kinds of source-location entries. |
| 1108 | int SLocFileAbbrv = -1; |
| 1109 | int SLocBufferAbbrv = -1; |
| 1110 | int SLocBufferBlobAbbrv = -1; |
| 1111 | int SLocInstantiationAbbrv = -1; |
| 1112 | |
| 1113 | // Write out the source location entry table. We skip the first |
| 1114 | // entry, which is always the same dummy entry. |
| 1115 | RecordData Record; |
| 1116 | for (SourceManager::sloc_entry_iterator |
| 1117 | SLoc = SourceMgr.sloc_entry_begin() + 1, |
| 1118 | SLocEnd = SourceMgr.sloc_entry_end(); |
| 1119 | SLoc != SLocEnd; ++SLoc) { |
| 1120 | // Figure out which record code to use. |
| 1121 | unsigned Code; |
| 1122 | if (SLoc->isFile()) { |
| 1123 | if (SLoc->getFile().getContentCache()->Entry) |
| 1124 | Code = pch::SM_SLOC_FILE_ENTRY; |
| 1125 | else |
| 1126 | Code = pch::SM_SLOC_BUFFER_ENTRY; |
| 1127 | } else |
| 1128 | Code = pch::SM_SLOC_INSTANTIATION_ENTRY; |
| 1129 | Record.push_back(Code); |
| 1130 | |
| 1131 | Record.push_back(SLoc->getOffset()); |
| 1132 | if (SLoc->isFile()) { |
| 1133 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 1134 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 1135 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1136 | Record.push_back(File.hasLineDirectives()); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1137 | |
| 1138 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
| 1139 | if (Content->Entry) { |
| 1140 | // The source location entry is a file. The blob associated |
| 1141 | // with this entry is the file name. |
| 1142 | if (SLocFileAbbrv == -1) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1143 | SLocFileAbbrv = CreateSLocFileAbbrev(Stream); |
| 1144 | Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1145 | Content->Entry->getName(), |
| 1146 | strlen(Content->Entry->getName())); |
| 1147 | } else { |
| 1148 | // The source location entry is a buffer. The blob associated |
| 1149 | // with this entry contains the contents of the buffer. |
| 1150 | if (SLocBufferAbbrv == -1) { |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1151 | SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); |
| 1152 | SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | // We add one to the size so that we capture the trailing NULL |
| 1156 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 1157 | // the reader side). |
| 1158 | const llvm::MemoryBuffer *Buffer = Content->getBuffer(); |
| 1159 | const char *Name = Buffer->getBufferIdentifier(); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1160 | Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1161 | Record.clear(); |
| 1162 | Record.push_back(pch::SM_SLOC_BUFFER_BLOB); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1163 | Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1164 | Buffer->getBufferStart(), |
| 1165 | Buffer->getBufferSize() + 1); |
| 1166 | } |
| 1167 | } else { |
| 1168 | // The source location entry is an instantiation. |
| 1169 | const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); |
| 1170 | Record.push_back(Inst.getSpellingLoc().getRawEncoding()); |
| 1171 | Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); |
| 1172 | Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); |
| 1173 | |
Douglas Gregor | 364e580 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 1174 | // Compute the token length for this macro expansion. |
| 1175 | unsigned NextOffset = SourceMgr.getNextOffset(); |
| 1176 | SourceManager::sloc_entry_iterator NextSLoc = SLoc; |
| 1177 | if (++NextSLoc != SLocEnd) |
| 1178 | NextOffset = NextSLoc->getOffset(); |
| 1179 | Record.push_back(NextOffset - SLoc->getOffset() - 1); |
| 1180 | |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1181 | if (SLocInstantiationAbbrv == -1) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1182 | SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream); |
| 1183 | Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | Record.clear(); |
| 1187 | } |
| 1188 | |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1189 | // Write the line table. |
| 1190 | if (SourceMgr.hasLineTable()) { |
| 1191 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 1192 | |
| 1193 | // Emit the file names |
| 1194 | Record.push_back(LineTable.getNumFilenames()); |
| 1195 | for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { |
| 1196 | // Emit the file name |
| 1197 | const char *Filename = LineTable.getFilename(I); |
| 1198 | unsigned FilenameLen = Filename? strlen(Filename) : 0; |
| 1199 | Record.push_back(FilenameLen); |
| 1200 | if (FilenameLen) |
| 1201 | Record.insert(Record.end(), Filename, Filename + FilenameLen); |
| 1202 | } |
| 1203 | |
| 1204 | // Emit the line entries |
| 1205 | for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); |
| 1206 | L != LEnd; ++L) { |
| 1207 | // Emit the file ID |
| 1208 | Record.push_back(L->first); |
| 1209 | |
| 1210 | // Emit the line entries |
| 1211 | Record.push_back(L->second.size()); |
| 1212 | for (std::vector<LineEntry>::iterator LE = L->second.begin(), |
| 1213 | LEEnd = L->second.end(); |
| 1214 | LE != LEEnd; ++LE) { |
| 1215 | Record.push_back(LE->FileOffset); |
| 1216 | Record.push_back(LE->LineNo); |
| 1217 | Record.push_back(LE->FilenameID); |
| 1218 | Record.push_back((unsigned)LE->FileKind); |
| 1219 | Record.push_back(LE->IncludeOffset); |
| 1220 | } |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1221 | Stream.EmitRecord(pch::SM_LINE_TABLE, Record); |
Douglas Gregor | 635f97f | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1225 | Stream.ExitBlock(); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Chris Lattner | ffc05ed | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1228 | /// \brief Writes the block containing the serialized form of the |
| 1229 | /// preprocessor. |
| 1230 | /// |
Chris Lattner | 850eabd | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1231 | void PCHWriter::WritePreprocessor(const Preprocessor &PP) { |
Chris Lattner | 84b04f1 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1232 | // Enter the preprocessor block. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1233 | Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3); |
Chris Lattner | 84b04f1 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1234 | |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1235 | // If the PCH file contains __DATE__ or __TIME__ emit a warning about this. |
| 1236 | // FIXME: use diagnostics subsystem for localization etc. |
| 1237 | if (PP.SawDateOrTime()) |
| 1238 | fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); |
Chris Lattner | 84b04f1 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1240 | RecordData Record; |
Chris Lattner | 84b04f1 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | 4b21c20 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 1242 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 1243 | if (PP.getCounterValue() != 0) { |
| 1244 | Record.push_back(PP.getCounterValue()); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1245 | Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record); |
Chris Lattner | 4b21c20 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 1246 | Record.clear(); |
| 1247 | } |
| 1248 | |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1249 | // Loop over all the macro definitions that are live at the end of the file, |
| 1250 | // emitting each to the PP section. |
| 1251 | // FIXME: Eventually we want to emit an index so that we can lazily load |
| 1252 | // macros. |
| 1253 | for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); |
| 1254 | I != E; ++I) { |
Chris Lattner | db1c81b | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 1255 | // FIXME: This emits macros in hash table order, we should do it in a stable |
| 1256 | // order so that output is reproducible. |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1257 | MacroInfo *MI = I->second; |
| 1258 | |
| 1259 | // Don't emit builtin macros like __LINE__ to the PCH file unless they have |
| 1260 | // been redefined by the header (in which case they are not isBuiltinMacro). |
| 1261 | if (MI->isBuiltinMacro()) |
| 1262 | continue; |
| 1263 | |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1264 | AddIdentifierRef(I->first, Record); |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1265 | Record.push_back(MI->getDefinitionLoc().getRawEncoding()); |
| 1266 | Record.push_back(MI->isUsed()); |
| 1267 | |
| 1268 | unsigned Code; |
| 1269 | if (MI->isObjectLike()) { |
| 1270 | Code = pch::PP_MACRO_OBJECT_LIKE; |
| 1271 | } else { |
| 1272 | Code = pch::PP_MACRO_FUNCTION_LIKE; |
| 1273 | |
| 1274 | Record.push_back(MI->isC99Varargs()); |
| 1275 | Record.push_back(MI->isGNUVarargs()); |
| 1276 | Record.push_back(MI->getNumArgs()); |
| 1277 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 1278 | I != E; ++I) |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1279 | AddIdentifierRef(*I, Record); |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1280 | } |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1281 | Stream.EmitRecord(Code, Record); |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1282 | Record.clear(); |
| 1283 | |
Chris Lattner | 850eabd | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1284 | // Emit the tokens array. |
| 1285 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 1286 | // Note that we know that the preprocessor does not have any annotation |
| 1287 | // tokens in it because they are created by the parser, and thus can't be |
| 1288 | // in a macro definition. |
| 1289 | const Token &Tok = MI->getReplacementToken(TokNo); |
| 1290 | |
| 1291 | Record.push_back(Tok.getLocation().getRawEncoding()); |
| 1292 | Record.push_back(Tok.getLength()); |
| 1293 | |
Chris Lattner | 850eabd | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1294 | // FIXME: When reading literal tokens, reconstruct the literal pointer if |
| 1295 | // it is needed. |
Chris Lattner | 2924186 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1296 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
Chris Lattner | 850eabd | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1297 | |
| 1298 | // FIXME: Should translate token kind to a stable encoding. |
| 1299 | Record.push_back(Tok.getKind()); |
| 1300 | // FIXME: Should translate token flags to a stable encoding. |
| 1301 | Record.push_back(Tok.getFlags()); |
| 1302 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1303 | Stream.EmitRecord(pch::PP_TOKEN, Record); |
Chris Lattner | 850eabd | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1304 | Record.clear(); |
| 1305 | } |
Chris Lattner | 1b09495 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1306 | |
| 1307 | } |
| 1308 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1309 | Stream.ExitBlock(); |
Chris Lattner | ffc05ed | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1313 | /// \brief Write the representation of a type to the PCH stream. |
| 1314 | void PCHWriter::WriteType(const Type *T) { |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1315 | pch::TypeID &ID = TypeIDs[T]; |
Chris Lattner | 84b04f1 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1316 | if (ID == 0) // we haven't seen this type before. |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1317 | ID = NextTypeID++; |
| 1318 | |
| 1319 | // Record the offset for this type. |
| 1320 | if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1321 | TypeOffsets.push_back(Stream.GetCurrentBitNo()); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1322 | else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) { |
| 1323 | TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1324 | TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | RecordData Record; |
| 1328 | |
| 1329 | // Emit the type's representation. |
| 1330 | PCHTypeWriter W(*this, Record); |
| 1331 | switch (T->getTypeClass()) { |
| 1332 | // For all of the concrete, non-dependent types, call the |
| 1333 | // appropriate visitor function. |
| 1334 | #define TYPE(Class, Base) \ |
| 1335 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
| 1336 | #define ABSTRACT_TYPE(Class, Base) |
| 1337 | #define DEPENDENT_TYPE(Class, Base) |
| 1338 | #include "clang/AST/TypeNodes.def" |
| 1339 | |
| 1340 | // For all of the dependent type nodes (which only occur in C++ |
| 1341 | // templates), produce an error. |
| 1342 | #define TYPE(Class, Base) |
| 1343 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1344 | #include "clang/AST/TypeNodes.def" |
| 1345 | assert(false && "Cannot serialize dependent type nodes"); |
| 1346 | break; |
| 1347 | } |
| 1348 | |
| 1349 | // Emit the serialized record. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1350 | Stream.EmitRecord(W.Code, Record); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1351 | |
| 1352 | // Flush any expressions that were written as part of this type. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1353 | FlushStmts(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | /// \brief Write a block containing all of the types. |
| 1357 | void PCHWriter::WriteTypesBlock(ASTContext &Context) { |
Chris Lattner | 84b04f1 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1358 | // Enter the types block. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1359 | Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1360 | |
| 1361 | // Emit all of the types in the ASTContext |
| 1362 | for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(), |
| 1363 | TEnd = Context.getTypes().end(); |
| 1364 | T != TEnd; ++T) { |
| 1365 | // Builtin types are never serialized. |
| 1366 | if (isa<BuiltinType>(*T)) |
| 1367 | continue; |
| 1368 | |
| 1369 | WriteType(*T); |
| 1370 | } |
| 1371 | |
| 1372 | // Exit the types block |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1373 | Stream.ExitBlock(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | /// \brief Write the block containing all of the declaration IDs |
| 1377 | /// lexically declared within the given DeclContext. |
| 1378 | /// |
| 1379 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 1380 | /// bistream, or 0 if no block was written. |
| 1381 | uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
| 1382 | DeclContext *DC) { |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1383 | if (DC->decls_empty(Context)) |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1384 | return 0; |
| 1385 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1386 | uint64_t Offset = Stream.GetCurrentBitNo(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1387 | RecordData Record; |
| 1388 | for (DeclContext::decl_iterator D = DC->decls_begin(Context), |
| 1389 | DEnd = DC->decls_end(Context); |
| 1390 | D != DEnd; ++D) |
| 1391 | AddDeclRef(*D, Record); |
| 1392 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1393 | Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1394 | return Offset; |
| 1395 | } |
| 1396 | |
| 1397 | /// \brief Write the block containing all of the declaration IDs |
| 1398 | /// visible from the given DeclContext. |
| 1399 | /// |
| 1400 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
| 1401 | /// bistream, or 0 if no block was written. |
| 1402 | uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 1403 | DeclContext *DC) { |
| 1404 | if (DC->getPrimaryContext() != DC) |
| 1405 | return 0; |
| 1406 | |
| 1407 | // Force the DeclContext to build a its name-lookup table. |
| 1408 | DC->lookup(Context, DeclarationName()); |
| 1409 | |
| 1410 | // Serialize the contents of the mapping used for lookup. Note that, |
| 1411 | // although we have two very different code paths, the serialized |
| 1412 | // representation is the same for both cases: a declaration name, |
| 1413 | // followed by a size, followed by references to the visible |
| 1414 | // declarations that have that name. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1415 | uint64_t Offset = Stream.GetCurrentBitNo(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1416 | RecordData Record; |
| 1417 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
Douglas Gregor | 982365e | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1418 | if (!Map) |
| 1419 | return 0; |
| 1420 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1421 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 1422 | D != DEnd; ++D) { |
| 1423 | AddDeclarationName(D->first, Record); |
| 1424 | DeclContext::lookup_result Result = D->second.getLookupResult(Context); |
| 1425 | Record.push_back(Result.second - Result.first); |
| 1426 | for(; Result.first != Result.second; ++Result.first) |
| 1427 | AddDeclRef(*Result.first, Record); |
| 1428 | } |
| 1429 | |
| 1430 | if (Record.size() == 0) |
| 1431 | return 0; |
| 1432 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1433 | Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1434 | return Offset; |
| 1435 | } |
| 1436 | |
| 1437 | /// \brief Write a block containing all of the declarations. |
| 1438 | void PCHWriter::WriteDeclsBlock(ASTContext &Context) { |
Chris Lattner | 84b04f1 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1439 | // Enter the declarations block. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1440 | Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1441 | |
| 1442 | // Emit all of the declarations. |
| 1443 | RecordData Record; |
| 1444 | PCHDeclWriter W(*this, Record); |
| 1445 | while (!DeclsToEmit.empty()) { |
| 1446 | // Pull the next declaration off the queue |
| 1447 | Decl *D = DeclsToEmit.front(); |
| 1448 | DeclsToEmit.pop(); |
| 1449 | |
| 1450 | // If this declaration is also a DeclContext, write blocks for the |
| 1451 | // declarations that lexically stored inside its context and those |
| 1452 | // declarations that are visible from its context. These blocks |
| 1453 | // are written before the declaration itself so that we can put |
| 1454 | // their offsets into the record for the declaration. |
| 1455 | uint64_t LexicalOffset = 0; |
| 1456 | uint64_t VisibleOffset = 0; |
| 1457 | DeclContext *DC = dyn_cast<DeclContext>(D); |
| 1458 | if (DC) { |
| 1459 | LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); |
| 1460 | VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); |
| 1461 | } |
| 1462 | |
| 1463 | // Determine the ID for this declaration |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1464 | pch::DeclID ID = DeclIDs[D]; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1465 | if (ID == 0) |
| 1466 | ID = DeclIDs.size(); |
| 1467 | |
| 1468 | unsigned Index = ID - 1; |
| 1469 | |
| 1470 | // Record the offset for this declaration |
| 1471 | if (DeclOffsets.size() == Index) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1472 | DeclOffsets.push_back(Stream.GetCurrentBitNo()); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1473 | else if (DeclOffsets.size() < Index) { |
| 1474 | DeclOffsets.resize(Index+1); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1475 | DeclOffsets[Index] = Stream.GetCurrentBitNo(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | // Build and emit a record for this declaration |
| 1479 | Record.clear(); |
| 1480 | W.Code = (pch::DeclCode)0; |
| 1481 | W.Visit(D); |
| 1482 | if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1483 | assert(W.Code && "Unhandled declaration kind while generating PCH"); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1484 | Stream.EmitRecord(W.Code, Record); |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | 1c50788 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1486 | // If the declaration had any attributes, write them now. |
| 1487 | if (D->hasAttrs()) |
| 1488 | WriteAttributeRecord(D->getAttrs()); |
| 1489 | |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1490 | // Flush any expressions that were written as part of this declaration. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1491 | FlushStmts(); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1493 | // Note external declarations so that we can add them to a record |
| 1494 | // in the PCH file later. |
| 1495 | if (isa<FileScopeAsmDecl>(D)) |
| 1496 | ExternalDefinitions.push_back(ID); |
| 1497 | else if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 1498 | if (// Non-static file-scope variables with initializers or that |
| 1499 | // are tentative definitions. |
| 1500 | (Var->isFileVarDecl() && |
| 1501 | (Var->getInit() || Var->getStorageClass() == VarDecl::None)) || |
| 1502 | // Out-of-line definitions of static data members (C++). |
| 1503 | (Var->getDeclContext()->isRecord() && |
| 1504 | !Var->getLexicalDeclContext()->isRecord() && |
| 1505 | Var->getStorageClass() == VarDecl::Static)) |
| 1506 | ExternalDefinitions.push_back(ID); |
| 1507 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) { |
| 1508 | if (Func->isThisDeclarationADefinition() && |
| 1509 | Func->getStorageClass() != FunctionDecl::Static && |
| 1510 | !Func->isInline()) |
| 1511 | ExternalDefinitions.push_back(ID); |
| 1512 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | // Exit the declarations block |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1516 | Stream.ExitBlock(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1519 | /// \brief Write the identifier table into the PCH file. |
| 1520 | /// |
| 1521 | /// The identifier table consists of a blob containing string data |
| 1522 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 1523 | /// that maps identifier IDs to locations within the blob. |
| 1524 | void PCHWriter::WriteIdentifierTable() { |
| 1525 | using namespace llvm; |
| 1526 | |
| 1527 | // Create and write out the blob that contains the identifier |
| 1528 | // strings. |
| 1529 | RecordData IdentOffsets; |
| 1530 | IdentOffsets.resize(IdentifierIDs.size()); |
| 1531 | { |
| 1532 | // Create the identifier string data. |
| 1533 | std::vector<char> Data; |
| 1534 | Data.push_back(0); // Data must not be empty. |
| 1535 | for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator |
| 1536 | ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); |
| 1537 | ID != IDEnd; ++ID) { |
| 1538 | assert(ID->first && "NULL identifier in identifier table"); |
| 1539 | |
| 1540 | // Make sure we're starting on an odd byte. The PCH reader |
| 1541 | // expects the low bit to be set on all of the offsets. |
| 1542 | if ((Data.size() & 0x01) == 0) |
| 1543 | Data.push_back((char)0); |
| 1544 | |
| 1545 | IdentOffsets[ID->second - 1] = Data.size(); |
| 1546 | Data.insert(Data.end(), |
| 1547 | ID->first->getName(), |
| 1548 | ID->first->getName() + ID->first->getLength()); |
| 1549 | Data.push_back((char)0); |
| 1550 | } |
| 1551 | |
| 1552 | // Create a blob abbreviation |
| 1553 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1554 | Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE)); |
| 1555 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1556 | unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1557 | |
| 1558 | // Write the identifier table |
| 1559 | RecordData Record; |
| 1560 | Record.push_back(pch::IDENTIFIER_TABLE); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1561 | Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size()); |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | // Write the offsets table for identifier IDs. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1565 | Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets); |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Douglas Gregor | 1c50788 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1568 | /// \brief Write a record containing the given attributes. |
| 1569 | void PCHWriter::WriteAttributeRecord(const Attr *Attr) { |
| 1570 | RecordData Record; |
| 1571 | for (; Attr; Attr = Attr->getNext()) { |
| 1572 | Record.push_back(Attr->getKind()); // FIXME: stable encoding |
| 1573 | Record.push_back(Attr->isInherited()); |
| 1574 | switch (Attr->getKind()) { |
| 1575 | case Attr::Alias: |
| 1576 | AddString(cast<AliasAttr>(Attr)->getAliasee(), Record); |
| 1577 | break; |
| 1578 | |
| 1579 | case Attr::Aligned: |
| 1580 | Record.push_back(cast<AlignedAttr>(Attr)->getAlignment()); |
| 1581 | break; |
| 1582 | |
| 1583 | case Attr::AlwaysInline: |
| 1584 | break; |
| 1585 | |
| 1586 | case Attr::AnalyzerNoReturn: |
| 1587 | break; |
| 1588 | |
| 1589 | case Attr::Annotate: |
| 1590 | AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record); |
| 1591 | break; |
| 1592 | |
| 1593 | case Attr::AsmLabel: |
| 1594 | AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record); |
| 1595 | break; |
| 1596 | |
| 1597 | case Attr::Blocks: |
| 1598 | Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable |
| 1599 | break; |
| 1600 | |
| 1601 | case Attr::Cleanup: |
| 1602 | AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record); |
| 1603 | break; |
| 1604 | |
| 1605 | case Attr::Const: |
| 1606 | break; |
| 1607 | |
| 1608 | case Attr::Constructor: |
| 1609 | Record.push_back(cast<ConstructorAttr>(Attr)->getPriority()); |
| 1610 | break; |
| 1611 | |
| 1612 | case Attr::DLLExport: |
| 1613 | case Attr::DLLImport: |
| 1614 | case Attr::Deprecated: |
| 1615 | break; |
| 1616 | |
| 1617 | case Attr::Destructor: |
| 1618 | Record.push_back(cast<DestructorAttr>(Attr)->getPriority()); |
| 1619 | break; |
| 1620 | |
| 1621 | case Attr::FastCall: |
| 1622 | break; |
| 1623 | |
| 1624 | case Attr::Format: { |
| 1625 | const FormatAttr *Format = cast<FormatAttr>(Attr); |
| 1626 | AddString(Format->getType(), Record); |
| 1627 | Record.push_back(Format->getFormatIdx()); |
| 1628 | Record.push_back(Format->getFirstArg()); |
| 1629 | break; |
| 1630 | } |
| 1631 | |
| 1632 | case Attr::GNUCInline: |
| 1633 | case Attr::IBOutletKind: |
| 1634 | case Attr::NoReturn: |
| 1635 | case Attr::NoThrow: |
| 1636 | case Attr::Nodebug: |
| 1637 | case Attr::Noinline: |
| 1638 | break; |
| 1639 | |
| 1640 | case Attr::NonNull: { |
| 1641 | const NonNullAttr *NonNull = cast<NonNullAttr>(Attr); |
| 1642 | Record.push_back(NonNull->size()); |
| 1643 | Record.insert(Record.end(), NonNull->begin(), NonNull->end()); |
| 1644 | break; |
| 1645 | } |
| 1646 | |
| 1647 | case Attr::ObjCException: |
| 1648 | case Attr::ObjCNSObject: |
| 1649 | case Attr::Overloadable: |
| 1650 | break; |
| 1651 | |
| 1652 | case Attr::Packed: |
| 1653 | Record.push_back(cast<PackedAttr>(Attr)->getAlignment()); |
| 1654 | break; |
| 1655 | |
| 1656 | case Attr::Pure: |
| 1657 | break; |
| 1658 | |
| 1659 | case Attr::Regparm: |
| 1660 | Record.push_back(cast<RegparmAttr>(Attr)->getNumParams()); |
| 1661 | break; |
| 1662 | |
| 1663 | case Attr::Section: |
| 1664 | AddString(cast<SectionAttr>(Attr)->getName(), Record); |
| 1665 | break; |
| 1666 | |
| 1667 | case Attr::StdCall: |
| 1668 | case Attr::TransparentUnion: |
| 1669 | case Attr::Unavailable: |
| 1670 | case Attr::Unused: |
| 1671 | case Attr::Used: |
| 1672 | break; |
| 1673 | |
| 1674 | case Attr::Visibility: |
| 1675 | // FIXME: stable encoding |
| 1676 | Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility()); |
| 1677 | break; |
| 1678 | |
| 1679 | case Attr::WarnUnusedResult: |
| 1680 | case Attr::Weak: |
| 1681 | case Attr::WeakImport: |
| 1682 | break; |
| 1683 | } |
| 1684 | } |
| 1685 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1686 | Stream.EmitRecord(pch::DECL_ATTR, Record); |
Douglas Gregor | 1c50788 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | void PCHWriter::AddString(const std::string &Str, RecordData &Record) { |
| 1690 | Record.push_back(Str.size()); |
| 1691 | Record.insert(Record.end(), Str.begin(), Str.end()); |
| 1692 | } |
| 1693 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1694 | PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream) |
| 1695 | : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1696 | |
Chris Lattner | 850eabd | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1697 | void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1698 | // Emit the file header. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1699 | Stream.Emit((unsigned)'C', 8); |
| 1700 | Stream.Emit((unsigned)'P', 8); |
| 1701 | Stream.Emit((unsigned)'C', 8); |
| 1702 | Stream.Emit((unsigned)'H', 8); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1703 | |
| 1704 | // The translation unit is the first declaration we'll emit. |
| 1705 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
| 1706 | DeclsToEmit.push(Context.getTranslationUnitDecl()); |
| 1707 | |
| 1708 | // Write the remaining PCH contents. |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1709 | Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3); |
Douglas Gregor | b5887f3 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1710 | WriteTargetTriple(Context.Target); |
Douglas Gregor | 179cfb1 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1711 | WriteLanguageOptions(Context.getLangOptions()); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1712 | WriteSourceManagerBlock(Context.getSourceManager()); |
Chris Lattner | ffc05ed | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1713 | WritePreprocessor(PP); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1714 | WriteTypesBlock(Context); |
| 1715 | WriteDeclsBlock(Context); |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1716 | WriteIdentifierTable(); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1717 | Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets); |
| 1718 | Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets); |
Douglas Gregor | 631f6c6 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1719 | if (!ExternalDefinitions.empty()) |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1720 | Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions); |
| 1721 | Stream.ExitBlock(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { |
| 1725 | Record.push_back(Loc.getRawEncoding()); |
| 1726 | } |
| 1727 | |
| 1728 | void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { |
| 1729 | Record.push_back(Value.getBitWidth()); |
| 1730 | unsigned N = Value.getNumWords(); |
| 1731 | const uint64_t* Words = Value.getRawData(); |
| 1732 | for (unsigned I = 0; I != N; ++I) |
| 1733 | Record.push_back(Words[I]); |
| 1734 | } |
| 1735 | |
Douglas Gregor | 47f1b2c | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1736 | void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) { |
| 1737 | Record.push_back(Value.isUnsigned()); |
| 1738 | AddAPInt(Value, Record); |
| 1739 | } |
| 1740 | |
Douglas Gregor | e2f3720 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1741 | void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) { |
| 1742 | AddAPInt(Value.bitcastToAPInt(), Record); |
| 1743 | } |
| 1744 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1745 | void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { |
Douglas Gregor | 7a224cf | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1746 | if (II == 0) { |
| 1747 | Record.push_back(0); |
| 1748 | return; |
| 1749 | } |
| 1750 | |
| 1751 | pch::IdentID &ID = IdentifierIDs[II]; |
| 1752 | if (ID == 0) |
| 1753 | ID = IdentifierIDs.size(); |
| 1754 | |
| 1755 | Record.push_back(ID); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | void PCHWriter::AddTypeRef(QualType T, RecordData &Record) { |
| 1759 | if (T.isNull()) { |
| 1760 | Record.push_back(pch::PREDEF_TYPE_NULL_ID); |
| 1761 | return; |
| 1762 | } |
| 1763 | |
| 1764 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) { |
Douglas Gregor | b3a04c8 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1765 | pch::TypeID ID = 0; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1766 | switch (BT->getKind()) { |
| 1767 | case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break; |
| 1768 | case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break; |
| 1769 | case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break; |
| 1770 | case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break; |
| 1771 | case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break; |
| 1772 | case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break; |
| 1773 | case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break; |
| 1774 | case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break; |
| 1775 | case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break; |
| 1776 | case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break; |
| 1777 | case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break; |
| 1778 | case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break; |
| 1779 | case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break; |
| 1780 | case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break; |
| 1781 | case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break; |
| 1782 | case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break; |
| 1783 | case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break; |
| 1784 | case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break; |
| 1785 | case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break; |
| 1786 | case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break; |
| 1787 | } |
| 1788 | |
| 1789 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1790 | return; |
| 1791 | } |
| 1792 | |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1793 | pch::TypeID &ID = TypeIDs[T.getTypePtr()]; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1794 | if (ID == 0) // we haven't seen this type before |
| 1795 | ID = NextTypeID++; |
| 1796 | |
| 1797 | // Encode the type qualifiers in the type reference. |
| 1798 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1799 | } |
| 1800 | |
| 1801 | void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) { |
| 1802 | if (D == 0) { |
| 1803 | Record.push_back(0); |
| 1804 | return; |
| 1805 | } |
| 1806 | |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1807 | pch::DeclID &ID = DeclIDs[D]; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1808 | if (ID == 0) { |
| 1809 | // We haven't seen this declaration before. Give it a new ID and |
| 1810 | // enqueue it in the list of declarations to emit. |
| 1811 | ID = DeclIDs.size(); |
| 1812 | DeclsToEmit.push(const_cast<Decl *>(D)); |
| 1813 | } |
| 1814 | |
| 1815 | Record.push_back(ID); |
| 1816 | } |
| 1817 | |
| 1818 | void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { |
| 1819 | Record.push_back(Name.getNameKind()); |
| 1820 | switch (Name.getNameKind()) { |
| 1821 | case DeclarationName::Identifier: |
| 1822 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 1823 | break; |
| 1824 | |
| 1825 | case DeclarationName::ObjCZeroArgSelector: |
| 1826 | case DeclarationName::ObjCOneArgSelector: |
| 1827 | case DeclarationName::ObjCMultiArgSelector: |
| 1828 | assert(false && "Serialization of Objective-C selectors unavailable"); |
| 1829 | break; |
| 1830 | |
| 1831 | case DeclarationName::CXXConstructorName: |
| 1832 | case DeclarationName::CXXDestructorName: |
| 1833 | case DeclarationName::CXXConversionFunctionName: |
| 1834 | AddTypeRef(Name.getCXXNameType(), Record); |
| 1835 | break; |
| 1836 | |
| 1837 | case DeclarationName::CXXOperatorName: |
| 1838 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 1839 | break; |
| 1840 | |
| 1841 | case DeclarationName::CXXUsingDirective: |
| 1842 | // No extra data to emit |
| 1843 | break; |
| 1844 | } |
| 1845 | } |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1847 | /// \brief Write the given substatement or subexpression to the |
| 1848 | /// bitstream. |
| 1849 | void PCHWriter::WriteSubStmt(Stmt *S) { |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1850 | RecordData Record; |
| 1851 | PCHStmtWriter Writer(*this, Record); |
| 1852 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1853 | if (!S) { |
| 1854 | Stream.EmitRecord(pch::STMT_NULL_PTR, Record); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1855 | return; |
| 1856 | } |
| 1857 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1858 | Writer.Code = pch::STMT_NULL_PTR; |
| 1859 | Writer.Visit(S); |
| 1860 | assert(Writer.Code != pch::STMT_NULL_PTR && |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1861 | "Unhandled expression writing PCH file"); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1862 | Stream.EmitRecord(Writer.Code, Record); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1865 | /// \brief Flush all of the statements that have been added to the |
| 1866 | /// queue via AddStmt(). |
| 1867 | void PCHWriter::FlushStmts() { |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1868 | RecordData Record; |
| 1869 | PCHStmtWriter Writer(*this, Record); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1871 | for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) { |
| 1872 | Stmt *S = StmtsToEmit[I]; |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1873 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1874 | if (!S) { |
| 1875 | Stream.EmitRecord(pch::STMT_NULL_PTR, Record); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1876 | continue; |
| 1877 | } |
| 1878 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1879 | Writer.Code = pch::STMT_NULL_PTR; |
| 1880 | Writer.Visit(S); |
| 1881 | assert(Writer.Code != pch::STMT_NULL_PTR && |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1882 | "Unhandled expression writing PCH file"); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1883 | Stream.EmitRecord(Writer.Code, Record); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1884 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1885 | assert(N == StmtsToEmit.size() && |
| 1886 | "Substatement writen via AddStmt rather than WriteSubStmt!"); |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1887 | |
| 1888 | // Note that we are at the end of a full expression. Any |
| 1889 | // expression records that follow this one are part of a different |
| 1890 | // expression. |
| 1891 | Record.clear(); |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1892 | Stream.EmitRecord(pch::STMT_STOP, Record); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1893 | } |
Douglas Gregor | a151ba4 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 1894 | |
Douglas Gregor | c72f6c8 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1895 | StmtsToEmit.clear(); |
Douglas Gregor | 22d2dcd | 2009-04-17 16:34:57 +0000 | [diff] [blame] | 1896 | SwitchCaseIDs.clear(); |
Douglas Gregor | c10f86f | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1897 | } |
Douglas Gregor | 9c4782a | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 1898 | |
| 1899 | unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) { |
| 1900 | assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() && |
| 1901 | "SwitchCase recorded twice"); |
| 1902 | unsigned NextID = SwitchCaseIDs.size(); |
| 1903 | SwitchCaseIDs[S] = NextID; |
| 1904 | return NextID; |
| 1905 | } |
| 1906 | |
| 1907 | unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) { |
| 1908 | assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() && |
| 1909 | "SwitchCase hasn't been seen yet"); |
| 1910 | return SwitchCaseIDs[S]; |
| 1911 | } |
Douglas Gregor | 6e411bf | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 1912 | |
| 1913 | /// \brief Retrieve the ID for the given label statement, which may |
| 1914 | /// or may not have been emitted yet. |
| 1915 | unsigned PCHWriter::GetLabelID(LabelStmt *S) { |
| 1916 | std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S); |
| 1917 | if (Pos != LabelIDs.end()) |
| 1918 | return Pos->second; |
| 1919 | |
| 1920 | unsigned NextID = LabelIDs.size(); |
| 1921 | LabelIDs[S] = NextID; |
| 1922 | return NextID; |
| 1923 | } |