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