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