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