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