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