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