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