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