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