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