1 //===--- ASTWriter.cpp - AST File Writer ----------------------------------===// 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 ASTWriter class, which writes AST files. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Serialization/ASTWriter.h" 15 #include "ASTCommon.h" 16 #include "clang/Sema/Sema.h" 17 #include "clang/Sema/IdentifierResolver.h" 18 #include "clang/AST/ASTContext.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclContextInternals.h" 21 #include "clang/AST/DeclTemplate.h" 22 #include "clang/AST/DeclFriend.h" 23 #include "clang/AST/Expr.h" 24 #include "clang/AST/ExprCXX.h" 25 #include "clang/AST/Type.h" 26 #include "clang/AST/TypeLocVisitor.h" 27 #include "clang/Serialization/ASTReader.h" 28 #include "clang/Lex/MacroInfo.h" 29 #include "clang/Lex/PreprocessingRecord.h" 30 #include "clang/Lex/Preprocessor.h" 31 #include "clang/Lex/HeaderSearch.h" 32 #include "clang/Basic/FileManager.h" 33 #include "clang/Basic/FileSystemStatCache.h" 34 #include "clang/Basic/OnDiskHashTable.h" 35 #include "clang/Basic/SourceManager.h" 36 #include "clang/Basic/SourceManagerInternals.h" 37 #include "clang/Basic/TargetInfo.h" 38 #include "clang/Basic/Version.h" 39 #include "clang/Basic/VersionTuple.h" 40 #include "llvm/ADT/APFloat.h" 41 #include "llvm/ADT/APInt.h" 42 #include "llvm/ADT/StringExtras.h" 43 #include "llvm/Bitcode/BitstreamWriter.h" 44 #include "llvm/Support/FileSystem.h" 45 #include "llvm/Support/MemoryBuffer.h" 46 #include "llvm/Support/Path.h" 47 #include <algorithm> 48 #include <cstdio> 49 #include <string.h> 50 #include <utility> 51 using namespace clang; 52 using namespace clang::serialization; 53 54 template <typename T, typename Allocator> data(const std::vector<T,Allocator> & v)55 static StringRef data(const std::vector<T, Allocator> &v) { 56 if (v.empty()) return StringRef(); 57 return StringRef(reinterpret_cast<const char*>(&v[0]), 58 sizeof(T) * v.size()); 59 } 60 61 template <typename T> data(const SmallVectorImpl<T> & v)62 static StringRef data(const SmallVectorImpl<T> &v) { 63 return StringRef(reinterpret_cast<const char*>(v.data()), 64 sizeof(T) * v.size()); 65 } 66 67 //===----------------------------------------------------------------------===// 68 // Type serialization 69 //===----------------------------------------------------------------------===// 70 71 namespace { 72 class ASTTypeWriter { 73 ASTWriter &Writer; 74 ASTWriter::RecordDataImpl &Record; 75 76 public: 77 /// \brief Type code that corresponds to the record generated. 78 TypeCode Code; 79 ASTTypeWriter(ASTWriter & Writer,ASTWriter::RecordDataImpl & Record)80 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 81 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { } 82 83 void VisitArrayType(const ArrayType *T); 84 void VisitFunctionType(const FunctionType *T); 85 void VisitTagType(const TagType *T); 86 87 #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); 88 #define ABSTRACT_TYPE(Class, Base) 89 #include "clang/AST/TypeNodes.def" 90 }; 91 } 92 VisitBuiltinType(const BuiltinType * T)93 void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) { 94 llvm_unreachable("Built-in types are never serialized"); 95 } 96 VisitComplexType(const ComplexType * T)97 void ASTTypeWriter::VisitComplexType(const ComplexType *T) { 98 Writer.AddTypeRef(T->getElementType(), Record); 99 Code = TYPE_COMPLEX; 100 } 101 VisitPointerType(const PointerType * T)102 void ASTTypeWriter::VisitPointerType(const PointerType *T) { 103 Writer.AddTypeRef(T->getPointeeType(), Record); 104 Code = TYPE_POINTER; 105 } 106 VisitBlockPointerType(const BlockPointerType * T)107 void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { 108 Writer.AddTypeRef(T->getPointeeType(), Record); 109 Code = TYPE_BLOCK_POINTER; 110 } 111 VisitLValueReferenceType(const LValueReferenceType * T)112 void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { 113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); 114 Record.push_back(T->isSpelledAsLValue()); 115 Code = TYPE_LVALUE_REFERENCE; 116 } 117 VisitRValueReferenceType(const RValueReferenceType * T)118 void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { 119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); 120 Code = TYPE_RVALUE_REFERENCE; 121 } 122 VisitMemberPointerType(const MemberPointerType * T)123 void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { 124 Writer.AddTypeRef(T->getPointeeType(), Record); 125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record); 126 Code = TYPE_MEMBER_POINTER; 127 } 128 VisitArrayType(const ArrayType * T)129 void ASTTypeWriter::VisitArrayType(const ArrayType *T) { 130 Writer.AddTypeRef(T->getElementType(), Record); 131 Record.push_back(T->getSizeModifier()); // FIXME: stable values 132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values 133 } 134 VisitConstantArrayType(const ConstantArrayType * T)135 void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { 136 VisitArrayType(T); 137 Writer.AddAPInt(T->getSize(), Record); 138 Code = TYPE_CONSTANT_ARRAY; 139 } 140 VisitIncompleteArrayType(const IncompleteArrayType * T)141 void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 142 VisitArrayType(T); 143 Code = TYPE_INCOMPLETE_ARRAY; 144 } 145 VisitVariableArrayType(const VariableArrayType * T)146 void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { 147 VisitArrayType(T); 148 Writer.AddSourceLocation(T->getLBracketLoc(), Record); 149 Writer.AddSourceLocation(T->getRBracketLoc(), Record); 150 Writer.AddStmt(T->getSizeExpr()); 151 Code = TYPE_VARIABLE_ARRAY; 152 } 153 VisitVectorType(const VectorType * T)154 void ASTTypeWriter::VisitVectorType(const VectorType *T) { 155 Writer.AddTypeRef(T->getElementType(), Record); 156 Record.push_back(T->getNumElements()); 157 Record.push_back(T->getVectorKind()); 158 Code = TYPE_VECTOR; 159 } 160 VisitExtVectorType(const ExtVectorType * T)161 void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) { 162 VisitVectorType(T); 163 Code = TYPE_EXT_VECTOR; 164 } 165 VisitFunctionType(const FunctionType * T)166 void ASTTypeWriter::VisitFunctionType(const FunctionType *T) { 167 Writer.AddTypeRef(T->getResultType(), Record); 168 FunctionType::ExtInfo C = T->getExtInfo(); 169 Record.push_back(C.getNoReturn()); 170 Record.push_back(C.getHasRegParm()); 171 Record.push_back(C.getRegParm()); 172 // FIXME: need to stabilize encoding of calling convention... 173 Record.push_back(C.getCC()); 174 Record.push_back(C.getProducesResult()); 175 } 176 VisitFunctionNoProtoType(const FunctionNoProtoType * T)177 void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 178 VisitFunctionType(T); 179 Code = TYPE_FUNCTION_NO_PROTO; 180 } 181 VisitFunctionProtoType(const FunctionProtoType * T)182 void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { 183 VisitFunctionType(T); 184 Record.push_back(T->getNumArgs()); 185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) 186 Writer.AddTypeRef(T->getArgType(I), Record); 187 Record.push_back(T->isVariadic()); 188 Record.push_back(T->hasTrailingReturn()); 189 Record.push_back(T->getTypeQuals()); 190 Record.push_back(static_cast<unsigned>(T->getRefQualifier())); 191 Record.push_back(T->getExceptionSpecType()); 192 if (T->getExceptionSpecType() == EST_Dynamic) { 193 Record.push_back(T->getNumExceptions()); 194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) 195 Writer.AddTypeRef(T->getExceptionType(I), Record); 196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) { 197 Writer.AddStmt(T->getNoexceptExpr()); 198 } 199 Code = TYPE_FUNCTION_PROTO; 200 } 201 VisitUnresolvedUsingType(const UnresolvedUsingType * T)202 void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { 203 Writer.AddDeclRef(T->getDecl(), Record); 204 Code = TYPE_UNRESOLVED_USING; 205 } 206 VisitTypedefType(const TypedefType * T)207 void ASTTypeWriter::VisitTypedefType(const TypedefType *T) { 208 Writer.AddDeclRef(T->getDecl(), Record); 209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); 210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); 211 Code = TYPE_TYPEDEF; 212 } 213 VisitTypeOfExprType(const TypeOfExprType * T)214 void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { 215 Writer.AddStmt(T->getUnderlyingExpr()); 216 Code = TYPE_TYPEOF_EXPR; 217 } 218 VisitTypeOfType(const TypeOfType * T)219 void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) { 220 Writer.AddTypeRef(T->getUnderlyingType(), Record); 221 Code = TYPE_TYPEOF; 222 } 223 VisitDecltypeType(const DecltypeType * T)224 void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) { 225 Writer.AddTypeRef(T->getUnderlyingType(), Record); 226 Writer.AddStmt(T->getUnderlyingExpr()); 227 Code = TYPE_DECLTYPE; 228 } 229 VisitUnaryTransformType(const UnaryTransformType * T)230 void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) { 231 Writer.AddTypeRef(T->getBaseType(), Record); 232 Writer.AddTypeRef(T->getUnderlyingType(), Record); 233 Record.push_back(T->getUTTKind()); 234 Code = TYPE_UNARY_TRANSFORM; 235 } 236 VisitAutoType(const AutoType * T)237 void ASTTypeWriter::VisitAutoType(const AutoType *T) { 238 Writer.AddTypeRef(T->getDeducedType(), Record); 239 Code = TYPE_AUTO; 240 } 241 VisitTagType(const TagType * T)242 void ASTTypeWriter::VisitTagType(const TagType *T) { 243 Record.push_back(T->isDependentType()); 244 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); 245 assert(!T->isBeingDefined() && 246 "Cannot serialize in the middle of a type definition"); 247 } 248 VisitRecordType(const RecordType * T)249 void ASTTypeWriter::VisitRecordType(const RecordType *T) { 250 VisitTagType(T); 251 Code = TYPE_RECORD; 252 } 253 VisitEnumType(const EnumType * T)254 void ASTTypeWriter::VisitEnumType(const EnumType *T) { 255 VisitTagType(T); 256 Code = TYPE_ENUM; 257 } 258 VisitAttributedType(const AttributedType * T)259 void ASTTypeWriter::VisitAttributedType(const AttributedType *T) { 260 Writer.AddTypeRef(T->getModifiedType(), Record); 261 Writer.AddTypeRef(T->getEquivalentType(), Record); 262 Record.push_back(T->getAttrKind()); 263 Code = TYPE_ATTRIBUTED; 264 } 265 266 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType * T)267 ASTTypeWriter::VisitSubstTemplateTypeParmType( 268 const SubstTemplateTypeParmType *T) { 269 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); 270 Writer.AddTypeRef(T->getReplacementType(), Record); 271 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM; 272 } 273 274 void VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType * T)275 ASTTypeWriter::VisitSubstTemplateTypeParmPackType( 276 const SubstTemplateTypeParmPackType *T) { 277 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); 278 Writer.AddTemplateArgument(T->getArgumentPack(), Record); 279 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK; 280 } 281 282 void VisitTemplateSpecializationType(const TemplateSpecializationType * T)283 ASTTypeWriter::VisitTemplateSpecializationType( 284 const TemplateSpecializationType *T) { 285 Record.push_back(T->isDependentType()); 286 Writer.AddTemplateName(T->getTemplateName(), Record); 287 Record.push_back(T->getNumArgs()); 288 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end(); 289 ArgI != ArgE; ++ArgI) 290 Writer.AddTemplateArgument(*ArgI, Record); 291 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() : 292 T->isCanonicalUnqualified() ? QualType() 293 : T->getCanonicalTypeInternal(), 294 Record); 295 Code = TYPE_TEMPLATE_SPECIALIZATION; 296 } 297 298 void VisitDependentSizedArrayType(const DependentSizedArrayType * T)299 ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { 300 VisitArrayType(T); 301 Writer.AddStmt(T->getSizeExpr()); 302 Writer.AddSourceRange(T->getBracketsRange(), Record); 303 Code = TYPE_DEPENDENT_SIZED_ARRAY; 304 } 305 306 void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType * T)307 ASTTypeWriter::VisitDependentSizedExtVectorType( 308 const DependentSizedExtVectorType *T) { 309 // FIXME: Serialize this type (C++ only) 310 llvm_unreachable("Cannot serialize dependent sized extended vector types"); 311 } 312 313 void VisitTemplateTypeParmType(const TemplateTypeParmType * T)314 ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { 315 Record.push_back(T->getDepth()); 316 Record.push_back(T->getIndex()); 317 Record.push_back(T->isParameterPack()); 318 Writer.AddDeclRef(T->getDecl(), Record); 319 Code = TYPE_TEMPLATE_TYPE_PARM; 320 } 321 322 void VisitDependentNameType(const DependentNameType * T)323 ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) { 324 Record.push_back(T->getKeyword()); 325 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 326 Writer.AddIdentifierRef(T->getIdentifier(), Record); 327 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() 328 : T->getCanonicalTypeInternal(), 329 Record); 330 Code = TYPE_DEPENDENT_NAME; 331 } 332 333 void VisitDependentTemplateSpecializationType(const DependentTemplateSpecializationType * T)334 ASTTypeWriter::VisitDependentTemplateSpecializationType( 335 const DependentTemplateSpecializationType *T) { 336 Record.push_back(T->getKeyword()); 337 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 338 Writer.AddIdentifierRef(T->getIdentifier(), Record); 339 Record.push_back(T->getNumArgs()); 340 for (DependentTemplateSpecializationType::iterator 341 I = T->begin(), E = T->end(); I != E; ++I) 342 Writer.AddTemplateArgument(*I, Record); 343 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; 344 } 345 VisitPackExpansionType(const PackExpansionType * T)346 void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) { 347 Writer.AddTypeRef(T->getPattern(), Record); 348 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions()) 349 Record.push_back(*NumExpansions + 1); 350 else 351 Record.push_back(0); 352 Code = TYPE_PACK_EXPANSION; 353 } 354 VisitParenType(const ParenType * T)355 void ASTTypeWriter::VisitParenType(const ParenType *T) { 356 Writer.AddTypeRef(T->getInnerType(), Record); 357 Code = TYPE_PAREN; 358 } 359 VisitElaboratedType(const ElaboratedType * T)360 void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { 361 Record.push_back(T->getKeyword()); 362 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 363 Writer.AddTypeRef(T->getNamedType(), Record); 364 Code = TYPE_ELABORATED; 365 } 366 VisitInjectedClassNameType(const InjectedClassNameType * T)367 void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) { 368 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); 369 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record); 370 Code = TYPE_INJECTED_CLASS_NAME; 371 } 372 VisitObjCInterfaceType(const ObjCInterfaceType * T)373 void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 374 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); 375 Code = TYPE_OBJC_INTERFACE; 376 } 377 VisitObjCObjectType(const ObjCObjectType * T)378 void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) { 379 Writer.AddTypeRef(T->getBaseType(), Record); 380 Record.push_back(T->getNumProtocols()); 381 for (ObjCObjectType::qual_iterator I = T->qual_begin(), 382 E = T->qual_end(); I != E; ++I) 383 Writer.AddDeclRef(*I, Record); 384 Code = TYPE_OBJC_OBJECT; 385 } 386 387 void VisitObjCObjectPointerType(const ObjCObjectPointerType * T)388 ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 389 Writer.AddTypeRef(T->getPointeeType(), Record); 390 Code = TYPE_OBJC_OBJECT_POINTER; 391 } 392 393 void VisitAtomicType(const AtomicType * T)394 ASTTypeWriter::VisitAtomicType(const AtomicType *T) { 395 Writer.AddTypeRef(T->getValueType(), Record); 396 Code = TYPE_ATOMIC; 397 } 398 399 namespace { 400 401 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> { 402 ASTWriter &Writer; 403 ASTWriter::RecordDataImpl &Record; 404 405 public: TypeLocWriter(ASTWriter & Writer,ASTWriter::RecordDataImpl & Record)406 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 407 : Writer(Writer), Record(Record) { } 408 409 #define ABSTRACT_TYPELOC(CLASS, PARENT) 410 #define TYPELOC(CLASS, PARENT) \ 411 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 412 #include "clang/AST/TypeLocNodes.def" 413 414 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc); 415 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc); 416 }; 417 418 } 419 VisitQualifiedTypeLoc(QualifiedTypeLoc TL)420 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 421 // nothing to do 422 } VisitBuiltinTypeLoc(BuiltinTypeLoc TL)423 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 424 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record); 425 if (TL.needsExtraLocalData()) { 426 Record.push_back(TL.getWrittenTypeSpec()); 427 Record.push_back(TL.getWrittenSignSpec()); 428 Record.push_back(TL.getWrittenWidthSpec()); 429 Record.push_back(TL.hasModeAttr()); 430 } 431 } VisitComplexTypeLoc(ComplexTypeLoc TL)432 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { 433 Writer.AddSourceLocation(TL.getNameLoc(), Record); 434 } VisitPointerTypeLoc(PointerTypeLoc TL)435 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { 436 Writer.AddSourceLocation(TL.getStarLoc(), Record); 437 } VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL)438 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 439 Writer.AddSourceLocation(TL.getCaretLoc(), Record); 440 } VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL)441 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 442 Writer.AddSourceLocation(TL.getAmpLoc(), Record); 443 } VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL)444 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 445 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record); 446 } VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL)447 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 448 Writer.AddSourceLocation(TL.getStarLoc(), Record); 449 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record); 450 } VisitArrayTypeLoc(ArrayTypeLoc TL)451 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { 452 Writer.AddSourceLocation(TL.getLBracketLoc(), Record); 453 Writer.AddSourceLocation(TL.getRBracketLoc(), Record); 454 Record.push_back(TL.getSizeExpr() ? 1 : 0); 455 if (TL.getSizeExpr()) 456 Writer.AddStmt(TL.getSizeExpr()); 457 } VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL)458 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 459 VisitArrayTypeLoc(TL); 460 } VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL)461 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 462 VisitArrayTypeLoc(TL); 463 } VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL)464 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 465 VisitArrayTypeLoc(TL); 466 } VisitDependentSizedArrayTypeLoc(DependentSizedArrayTypeLoc TL)467 void TypeLocWriter::VisitDependentSizedArrayTypeLoc( 468 DependentSizedArrayTypeLoc TL) { 469 VisitArrayTypeLoc(TL); 470 } VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL)471 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( 472 DependentSizedExtVectorTypeLoc TL) { 473 Writer.AddSourceLocation(TL.getNameLoc(), Record); 474 } VisitVectorTypeLoc(VectorTypeLoc TL)475 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { 476 Writer.AddSourceLocation(TL.getNameLoc(), Record); 477 } VisitExtVectorTypeLoc(ExtVectorTypeLoc TL)478 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 479 Writer.AddSourceLocation(TL.getNameLoc(), Record); 480 } VisitFunctionTypeLoc(FunctionTypeLoc TL)481 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 482 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record); 483 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record); 484 Record.push_back(TL.getTrailingReturn()); 485 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 486 Writer.AddDeclRef(TL.getArg(i), Record); 487 } VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL)488 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 489 VisitFunctionTypeLoc(TL); 490 } VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL)491 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 492 VisitFunctionTypeLoc(TL); 493 } VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL)494 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 495 Writer.AddSourceLocation(TL.getNameLoc(), Record); 496 } VisitTypedefTypeLoc(TypedefTypeLoc TL)497 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 498 Writer.AddSourceLocation(TL.getNameLoc(), Record); 499 } VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL)500 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 501 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 502 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 503 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 504 } VisitTypeOfTypeLoc(TypeOfTypeLoc TL)505 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 506 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 507 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 508 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 509 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); 510 } VisitDecltypeTypeLoc(DecltypeTypeLoc TL)511 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 512 Writer.AddSourceLocation(TL.getNameLoc(), Record); 513 } VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL)514 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 515 Writer.AddSourceLocation(TL.getKWLoc(), Record); 516 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 517 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 518 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); 519 } VisitAutoTypeLoc(AutoTypeLoc TL)520 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) { 521 Writer.AddSourceLocation(TL.getNameLoc(), Record); 522 } VisitRecordTypeLoc(RecordTypeLoc TL)523 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { 524 Writer.AddSourceLocation(TL.getNameLoc(), Record); 525 } VisitEnumTypeLoc(EnumTypeLoc TL)526 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { 527 Writer.AddSourceLocation(TL.getNameLoc(), Record); 528 } VisitAttributedTypeLoc(AttributedTypeLoc TL)529 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 530 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record); 531 if (TL.hasAttrOperand()) { 532 SourceRange range = TL.getAttrOperandParensRange(); 533 Writer.AddSourceLocation(range.getBegin(), Record); 534 Writer.AddSourceLocation(range.getEnd(), Record); 535 } 536 if (TL.hasAttrExprOperand()) { 537 Expr *operand = TL.getAttrExprOperand(); 538 Record.push_back(operand ? 1 : 0); 539 if (operand) Writer.AddStmt(operand); 540 } else if (TL.hasAttrEnumOperand()) { 541 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record); 542 } 543 } VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL)544 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 545 Writer.AddSourceLocation(TL.getNameLoc(), Record); 546 } VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL)547 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( 548 SubstTemplateTypeParmTypeLoc TL) { 549 Writer.AddSourceLocation(TL.getNameLoc(), Record); 550 } VisitSubstTemplateTypeParmPackTypeLoc(SubstTemplateTypeParmPackTypeLoc TL)551 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc( 552 SubstTemplateTypeParmPackTypeLoc TL) { 553 Writer.AddSourceLocation(TL.getNameLoc(), Record); 554 } VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL)555 void TypeLocWriter::VisitTemplateSpecializationTypeLoc( 556 TemplateSpecializationTypeLoc TL) { 557 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record); 558 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); 559 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 560 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 561 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 562 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), 563 TL.getArgLoc(i).getLocInfo(), Record); 564 } VisitParenTypeLoc(ParenTypeLoc TL)565 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) { 566 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 567 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 568 } VisitElaboratedTypeLoc(ElaboratedTypeLoc TL)569 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 570 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); 571 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 572 } VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL)573 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 574 Writer.AddSourceLocation(TL.getNameLoc(), Record); 575 } VisitDependentNameTypeLoc(DependentNameTypeLoc TL)576 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 577 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); 578 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 579 Writer.AddSourceLocation(TL.getNameLoc(), Record); 580 } VisitDependentTemplateSpecializationTypeLoc(DependentTemplateSpecializationTypeLoc TL)581 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( 582 DependentTemplateSpecializationTypeLoc TL) { 583 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); 584 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 585 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record); 586 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); 587 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 588 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 589 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 590 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), 591 TL.getArgLoc(I).getLocInfo(), Record); 592 } VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL)593 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 594 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record); 595 } VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL)596 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 597 Writer.AddSourceLocation(TL.getNameLoc(), Record); 598 } VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL)599 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 600 Record.push_back(TL.hasBaseTypeAsWritten()); 601 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 602 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 603 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 604 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record); 605 } VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL)606 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 607 Writer.AddSourceLocation(TL.getStarLoc(), Record); 608 } VisitAtomicTypeLoc(AtomicTypeLoc TL)609 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 610 Writer.AddSourceLocation(TL.getKWLoc(), Record); 611 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 612 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 613 } 614 615 //===----------------------------------------------------------------------===// 616 // ASTWriter Implementation 617 //===----------------------------------------------------------------------===// 618 EmitBlockID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)619 static void EmitBlockID(unsigned ID, const char *Name, 620 llvm::BitstreamWriter &Stream, 621 ASTWriter::RecordDataImpl &Record) { 622 Record.clear(); 623 Record.push_back(ID); 624 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); 625 626 // Emit the block name if present. 627 if (Name == 0 || Name[0] == 0) return; 628 Record.clear(); 629 while (*Name) 630 Record.push_back(*Name++); 631 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); 632 } 633 EmitRecordID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)634 static void EmitRecordID(unsigned ID, const char *Name, 635 llvm::BitstreamWriter &Stream, 636 ASTWriter::RecordDataImpl &Record) { 637 Record.clear(); 638 Record.push_back(ID); 639 while (*Name) 640 Record.push_back(*Name++); 641 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); 642 } 643 AddStmtsExprs(llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)644 static void AddStmtsExprs(llvm::BitstreamWriter &Stream, 645 ASTWriter::RecordDataImpl &Record) { 646 #define RECORD(X) EmitRecordID(X, #X, Stream, Record) 647 RECORD(STMT_STOP); 648 RECORD(STMT_NULL_PTR); 649 RECORD(STMT_NULL); 650 RECORD(STMT_COMPOUND); 651 RECORD(STMT_CASE); 652 RECORD(STMT_DEFAULT); 653 RECORD(STMT_LABEL); 654 RECORD(STMT_ATTRIBUTED); 655 RECORD(STMT_IF); 656 RECORD(STMT_SWITCH); 657 RECORD(STMT_WHILE); 658 RECORD(STMT_DO); 659 RECORD(STMT_FOR); 660 RECORD(STMT_GOTO); 661 RECORD(STMT_INDIRECT_GOTO); 662 RECORD(STMT_CONTINUE); 663 RECORD(STMT_BREAK); 664 RECORD(STMT_RETURN); 665 RECORD(STMT_DECL); 666 RECORD(STMT_ASM); 667 RECORD(EXPR_PREDEFINED); 668 RECORD(EXPR_DECL_REF); 669 RECORD(EXPR_INTEGER_LITERAL); 670 RECORD(EXPR_FLOATING_LITERAL); 671 RECORD(EXPR_IMAGINARY_LITERAL); 672 RECORD(EXPR_STRING_LITERAL); 673 RECORD(EXPR_CHARACTER_LITERAL); 674 RECORD(EXPR_PAREN); 675 RECORD(EXPR_UNARY_OPERATOR); 676 RECORD(EXPR_SIZEOF_ALIGN_OF); 677 RECORD(EXPR_ARRAY_SUBSCRIPT); 678 RECORD(EXPR_CALL); 679 RECORD(EXPR_MEMBER); 680 RECORD(EXPR_BINARY_OPERATOR); 681 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); 682 RECORD(EXPR_CONDITIONAL_OPERATOR); 683 RECORD(EXPR_IMPLICIT_CAST); 684 RECORD(EXPR_CSTYLE_CAST); 685 RECORD(EXPR_COMPOUND_LITERAL); 686 RECORD(EXPR_EXT_VECTOR_ELEMENT); 687 RECORD(EXPR_INIT_LIST); 688 RECORD(EXPR_DESIGNATED_INIT); 689 RECORD(EXPR_IMPLICIT_VALUE_INIT); 690 RECORD(EXPR_VA_ARG); 691 RECORD(EXPR_ADDR_LABEL); 692 RECORD(EXPR_STMT); 693 RECORD(EXPR_CHOOSE); 694 RECORD(EXPR_GNU_NULL); 695 RECORD(EXPR_SHUFFLE_VECTOR); 696 RECORD(EXPR_BLOCK); 697 RECORD(EXPR_GENERIC_SELECTION); 698 RECORD(EXPR_OBJC_STRING_LITERAL); 699 RECORD(EXPR_OBJC_BOXED_EXPRESSION); 700 RECORD(EXPR_OBJC_ARRAY_LITERAL); 701 RECORD(EXPR_OBJC_DICTIONARY_LITERAL); 702 RECORD(EXPR_OBJC_ENCODE); 703 RECORD(EXPR_OBJC_SELECTOR_EXPR); 704 RECORD(EXPR_OBJC_PROTOCOL_EXPR); 705 RECORD(EXPR_OBJC_IVAR_REF_EXPR); 706 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); 707 RECORD(EXPR_OBJC_KVC_REF_EXPR); 708 RECORD(EXPR_OBJC_MESSAGE_EXPR); 709 RECORD(STMT_OBJC_FOR_COLLECTION); 710 RECORD(STMT_OBJC_CATCH); 711 RECORD(STMT_OBJC_FINALLY); 712 RECORD(STMT_OBJC_AT_TRY); 713 RECORD(STMT_OBJC_AT_SYNCHRONIZED); 714 RECORD(STMT_OBJC_AT_THROW); 715 RECORD(EXPR_OBJC_BOOL_LITERAL); 716 RECORD(EXPR_CXX_OPERATOR_CALL); 717 RECORD(EXPR_CXX_CONSTRUCT); 718 RECORD(EXPR_CXX_STATIC_CAST); 719 RECORD(EXPR_CXX_DYNAMIC_CAST); 720 RECORD(EXPR_CXX_REINTERPRET_CAST); 721 RECORD(EXPR_CXX_CONST_CAST); 722 RECORD(EXPR_CXX_FUNCTIONAL_CAST); 723 RECORD(EXPR_USER_DEFINED_LITERAL); 724 RECORD(EXPR_CXX_BOOL_LITERAL); 725 RECORD(EXPR_CXX_NULL_PTR_LITERAL); 726 RECORD(EXPR_CXX_TYPEID_EXPR); 727 RECORD(EXPR_CXX_TYPEID_TYPE); 728 RECORD(EXPR_CXX_UUIDOF_EXPR); 729 RECORD(EXPR_CXX_UUIDOF_TYPE); 730 RECORD(EXPR_CXX_THIS); 731 RECORD(EXPR_CXX_THROW); 732 RECORD(EXPR_CXX_DEFAULT_ARG); 733 RECORD(EXPR_CXX_BIND_TEMPORARY); 734 RECORD(EXPR_CXX_SCALAR_VALUE_INIT); 735 RECORD(EXPR_CXX_NEW); 736 RECORD(EXPR_CXX_DELETE); 737 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR); 738 RECORD(EXPR_EXPR_WITH_CLEANUPS); 739 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER); 740 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF); 741 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT); 742 RECORD(EXPR_CXX_UNRESOLVED_MEMBER); 743 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP); 744 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT); 745 RECORD(EXPR_CXX_NOEXCEPT); 746 RECORD(EXPR_OPAQUE_VALUE); 747 RECORD(EXPR_BINARY_TYPE_TRAIT); 748 RECORD(EXPR_PACK_EXPANSION); 749 RECORD(EXPR_SIZEOF_PACK); 750 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK); 751 RECORD(EXPR_CUDA_KERNEL_CALL); 752 #undef RECORD 753 } 754 WriteBlockInfoBlock()755 void ASTWriter::WriteBlockInfoBlock() { 756 RecordData Record; 757 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); 758 759 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record) 760 #define RECORD(X) EmitRecordID(X, #X, Stream, Record) 761 762 // AST Top-Level Block. 763 BLOCK(AST_BLOCK); 764 RECORD(ORIGINAL_FILE_NAME); 765 RECORD(ORIGINAL_FILE_ID); 766 RECORD(TYPE_OFFSET); 767 RECORD(DECL_OFFSET); 768 RECORD(LANGUAGE_OPTIONS); 769 RECORD(METADATA); 770 RECORD(IDENTIFIER_OFFSET); 771 RECORD(IDENTIFIER_TABLE); 772 RECORD(EXTERNAL_DEFINITIONS); 773 RECORD(SPECIAL_TYPES); 774 RECORD(STATISTICS); 775 RECORD(TENTATIVE_DEFINITIONS); 776 RECORD(UNUSED_FILESCOPED_DECLS); 777 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS); 778 RECORD(SELECTOR_OFFSETS); 779 RECORD(METHOD_POOL); 780 RECORD(PP_COUNTER_VALUE); 781 RECORD(SOURCE_LOCATION_OFFSETS); 782 RECORD(SOURCE_LOCATION_PRELOADS); 783 RECORD(STAT_CACHE); 784 RECORD(EXT_VECTOR_DECLS); 785 RECORD(VERSION_CONTROL_BRANCH_REVISION); 786 RECORD(PPD_ENTITIES_OFFSETS); 787 RECORD(IMPORTS); 788 RECORD(REFERENCED_SELECTOR_POOL); 789 RECORD(TU_UPDATE_LEXICAL); 790 RECORD(LOCAL_REDECLARATIONS_MAP); 791 RECORD(SEMA_DECL_REFS); 792 RECORD(WEAK_UNDECLARED_IDENTIFIERS); 793 RECORD(PENDING_IMPLICIT_INSTANTIATIONS); 794 RECORD(DECL_REPLACEMENTS); 795 RECORD(UPDATE_VISIBLE); 796 RECORD(DECL_UPDATE_OFFSETS); 797 RECORD(DECL_UPDATES); 798 RECORD(CXX_BASE_SPECIFIER_OFFSETS); 799 RECORD(DIAG_PRAGMA_MAPPINGS); 800 RECORD(CUDA_SPECIAL_DECL_REFS); 801 RECORD(HEADER_SEARCH_TABLE); 802 RECORD(ORIGINAL_PCH_DIR); 803 RECORD(FP_PRAGMA_OPTIONS); 804 RECORD(OPENCL_EXTENSIONS); 805 RECORD(DELEGATING_CTORS); 806 RECORD(FILE_SOURCE_LOCATION_OFFSETS); 807 RECORD(KNOWN_NAMESPACES); 808 RECORD(MODULE_OFFSET_MAP); 809 RECORD(SOURCE_MANAGER_LINE_TABLE); 810 RECORD(OBJC_CATEGORIES_MAP); 811 RECORD(FILE_SORTED_DECLS); 812 RECORD(IMPORTED_MODULES); 813 RECORD(MERGED_DECLARATIONS); 814 RECORD(LOCAL_REDECLARATIONS); 815 RECORD(OBJC_CATEGORIES); 816 817 // SourceManager Block. 818 BLOCK(SOURCE_MANAGER_BLOCK); 819 RECORD(SM_SLOC_FILE_ENTRY); 820 RECORD(SM_SLOC_BUFFER_ENTRY); 821 RECORD(SM_SLOC_BUFFER_BLOB); 822 RECORD(SM_SLOC_EXPANSION_ENTRY); 823 824 // Preprocessor Block. 825 BLOCK(PREPROCESSOR_BLOCK); 826 RECORD(PP_MACRO_OBJECT_LIKE); 827 RECORD(PP_MACRO_FUNCTION_LIKE); 828 RECORD(PP_TOKEN); 829 830 // Decls and Types block. 831 BLOCK(DECLTYPES_BLOCK); 832 RECORD(TYPE_EXT_QUAL); 833 RECORD(TYPE_COMPLEX); 834 RECORD(TYPE_POINTER); 835 RECORD(TYPE_BLOCK_POINTER); 836 RECORD(TYPE_LVALUE_REFERENCE); 837 RECORD(TYPE_RVALUE_REFERENCE); 838 RECORD(TYPE_MEMBER_POINTER); 839 RECORD(TYPE_CONSTANT_ARRAY); 840 RECORD(TYPE_INCOMPLETE_ARRAY); 841 RECORD(TYPE_VARIABLE_ARRAY); 842 RECORD(TYPE_VECTOR); 843 RECORD(TYPE_EXT_VECTOR); 844 RECORD(TYPE_FUNCTION_PROTO); 845 RECORD(TYPE_FUNCTION_NO_PROTO); 846 RECORD(TYPE_TYPEDEF); 847 RECORD(TYPE_TYPEOF_EXPR); 848 RECORD(TYPE_TYPEOF); 849 RECORD(TYPE_RECORD); 850 RECORD(TYPE_ENUM); 851 RECORD(TYPE_OBJC_INTERFACE); 852 RECORD(TYPE_OBJC_OBJECT); 853 RECORD(TYPE_OBJC_OBJECT_POINTER); 854 RECORD(TYPE_DECLTYPE); 855 RECORD(TYPE_ELABORATED); 856 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM); 857 RECORD(TYPE_UNRESOLVED_USING); 858 RECORD(TYPE_INJECTED_CLASS_NAME); 859 RECORD(TYPE_OBJC_OBJECT); 860 RECORD(TYPE_TEMPLATE_TYPE_PARM); 861 RECORD(TYPE_TEMPLATE_SPECIALIZATION); 862 RECORD(TYPE_DEPENDENT_NAME); 863 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION); 864 RECORD(TYPE_DEPENDENT_SIZED_ARRAY); 865 RECORD(TYPE_PAREN); 866 RECORD(TYPE_PACK_EXPANSION); 867 RECORD(TYPE_ATTRIBUTED); 868 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK); 869 RECORD(TYPE_ATOMIC); 870 RECORD(DECL_TYPEDEF); 871 RECORD(DECL_ENUM); 872 RECORD(DECL_RECORD); 873 RECORD(DECL_ENUM_CONSTANT); 874 RECORD(DECL_FUNCTION); 875 RECORD(DECL_OBJC_METHOD); 876 RECORD(DECL_OBJC_INTERFACE); 877 RECORD(DECL_OBJC_PROTOCOL); 878 RECORD(DECL_OBJC_IVAR); 879 RECORD(DECL_OBJC_AT_DEFS_FIELD); 880 RECORD(DECL_OBJC_CATEGORY); 881 RECORD(DECL_OBJC_CATEGORY_IMPL); 882 RECORD(DECL_OBJC_IMPLEMENTATION); 883 RECORD(DECL_OBJC_COMPATIBLE_ALIAS); 884 RECORD(DECL_OBJC_PROPERTY); 885 RECORD(DECL_OBJC_PROPERTY_IMPL); 886 RECORD(DECL_FIELD); 887 RECORD(DECL_VAR); 888 RECORD(DECL_IMPLICIT_PARAM); 889 RECORD(DECL_PARM_VAR); 890 RECORD(DECL_FILE_SCOPE_ASM); 891 RECORD(DECL_BLOCK); 892 RECORD(DECL_CONTEXT_LEXICAL); 893 RECORD(DECL_CONTEXT_VISIBLE); 894 RECORD(DECL_NAMESPACE); 895 RECORD(DECL_NAMESPACE_ALIAS); 896 RECORD(DECL_USING); 897 RECORD(DECL_USING_SHADOW); 898 RECORD(DECL_USING_DIRECTIVE); 899 RECORD(DECL_UNRESOLVED_USING_VALUE); 900 RECORD(DECL_UNRESOLVED_USING_TYPENAME); 901 RECORD(DECL_LINKAGE_SPEC); 902 RECORD(DECL_CXX_RECORD); 903 RECORD(DECL_CXX_METHOD); 904 RECORD(DECL_CXX_CONSTRUCTOR); 905 RECORD(DECL_CXX_DESTRUCTOR); 906 RECORD(DECL_CXX_CONVERSION); 907 RECORD(DECL_ACCESS_SPEC); 908 RECORD(DECL_FRIEND); 909 RECORD(DECL_FRIEND_TEMPLATE); 910 RECORD(DECL_CLASS_TEMPLATE); 911 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION); 912 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION); 913 RECORD(DECL_FUNCTION_TEMPLATE); 914 RECORD(DECL_TEMPLATE_TYPE_PARM); 915 RECORD(DECL_NON_TYPE_TEMPLATE_PARM); 916 RECORD(DECL_TEMPLATE_TEMPLATE_PARM); 917 RECORD(DECL_STATIC_ASSERT); 918 RECORD(DECL_CXX_BASE_SPECIFIERS); 919 RECORD(DECL_INDIRECTFIELD); 920 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK); 921 922 // Statements and Exprs can occur in the Decls and Types block. 923 AddStmtsExprs(Stream, Record); 924 925 BLOCK(PREPROCESSOR_DETAIL_BLOCK); 926 RECORD(PPD_MACRO_EXPANSION); 927 RECORD(PPD_MACRO_DEFINITION); 928 RECORD(PPD_INCLUSION_DIRECTIVE); 929 930 #undef RECORD 931 #undef BLOCK 932 Stream.ExitBlock(); 933 } 934 935 /// \brief Adjusts the given filename to only write out the portion of the 936 /// filename that is not part of the system root directory. 937 /// 938 /// \param Filename the file name to adjust. 939 /// 940 /// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and 941 /// the returned filename will be adjusted by this system root. 942 /// 943 /// \returns either the original filename (if it needs no adjustment) or the 944 /// adjusted filename (which points into the @p Filename parameter). 945 static const char * adjustFilenameForRelocatablePCH(const char * Filename,StringRef isysroot)946 adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) { 947 assert(Filename && "No file name to adjust?"); 948 949 if (isysroot.empty()) 950 return Filename; 951 952 // Verify that the filename and the system root have the same prefix. 953 unsigned Pos = 0; 954 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos) 955 if (Filename[Pos] != isysroot[Pos]) 956 return Filename; // Prefixes don't match. 957 958 // We hit the end of the filename before we hit the end of the system root. 959 if (!Filename[Pos]) 960 return Filename; 961 962 // If the file name has a '/' at the current position, skip over the '/'. 963 // We distinguish sysroot-based includes from absolute includes by the 964 // absence of '/' at the beginning of sysroot-based includes. 965 if (Filename[Pos] == '/') 966 ++Pos; 967 968 return Filename + Pos; 969 } 970 971 /// \brief Write the AST metadata (e.g., i686-apple-darwin9). WriteMetadata(ASTContext & Context,StringRef isysroot,const std::string & OutputFile)972 void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot, 973 const std::string &OutputFile) { 974 using namespace llvm; 975 976 // Metadata 977 const TargetInfo &Target = Context.getTargetInfo(); 978 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev(); 979 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA)); 980 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major 981 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor 982 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major 983 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor 984 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable 985 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors 986 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple 987 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev); 988 989 RecordData Record; 990 Record.push_back(METADATA); 991 Record.push_back(VERSION_MAJOR); 992 Record.push_back(VERSION_MINOR); 993 Record.push_back(CLANG_VERSION_MAJOR); 994 Record.push_back(CLANG_VERSION_MINOR); 995 Record.push_back(!isysroot.empty()); 996 Record.push_back(ASTHasCompilerErrors); 997 const std::string &Triple = Target.getTriple().getTriple(); 998 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple); 999 1000 if (Chain) { 1001 serialization::ModuleManager &Mgr = Chain->getModuleManager(); 1002 llvm::SmallVector<char, 128> ModulePaths; 1003 Record.clear(); 1004 1005 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end(); 1006 M != MEnd; ++M) { 1007 // Skip modules that weren't directly imported. 1008 if (!(*M)->isDirectlyImported()) 1009 continue; 1010 1011 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding 1012 // FIXME: Write import location, once it matters. 1013 // FIXME: This writes the absolute path for AST files we depend on. 1014 const std::string &FileName = (*M)->FileName; 1015 Record.push_back(FileName.size()); 1016 Record.append(FileName.begin(), FileName.end()); 1017 } 1018 Stream.EmitRecord(IMPORTS, Record); 1019 } 1020 1021 // Original file name and file ID 1022 SourceManager &SM = Context.getSourceManager(); 1023 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { 1024 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev(); 1025 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME)); 1026 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1027 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev); 1028 1029 SmallString<128> MainFilePath(MainFile->getName()); 1030 1031 llvm::sys::fs::make_absolute(MainFilePath); 1032 1033 const char *MainFileNameStr = MainFilePath.c_str(); 1034 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr, 1035 isysroot); 1036 RecordData Record; 1037 Record.push_back(ORIGINAL_FILE_NAME); 1038 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr); 1039 1040 Record.clear(); 1041 Record.push_back(SM.getMainFileID().getOpaqueValue()); 1042 Stream.EmitRecord(ORIGINAL_FILE_ID, Record); 1043 } 1044 1045 // Original PCH directory 1046 if (!OutputFile.empty() && OutputFile != "-") { 1047 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1048 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR)); 1049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1050 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); 1051 1052 SmallString<128> OutputPath(OutputFile); 1053 1054 llvm::sys::fs::make_absolute(OutputPath); 1055 StringRef origDir = llvm::sys::path::parent_path(OutputPath); 1056 1057 RecordData Record; 1058 Record.push_back(ORIGINAL_PCH_DIR); 1059 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir); 1060 } 1061 1062 // Repository branch/version information. 1063 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev(); 1064 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION)); 1065 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag 1066 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev); 1067 Record.clear(); 1068 Record.push_back(VERSION_CONTROL_BRANCH_REVISION); 1069 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record, 1070 getClangFullRepositoryVersion()); 1071 } 1072 1073 /// \brief Write the LangOptions structure. WriteLanguageOptions(const LangOptions & LangOpts)1074 void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) { 1075 RecordData Record; 1076 #define LANGOPT(Name, Bits, Default, Description) \ 1077 Record.push_back(LangOpts.Name); 1078 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 1079 Record.push_back(static_cast<unsigned>(LangOpts.get##Name())); 1080 #include "clang/Basic/LangOptions.def" 1081 1082 Record.push_back(LangOpts.CurrentModule.size()); 1083 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end()); 1084 Stream.EmitRecord(LANGUAGE_OPTIONS, Record); 1085 } 1086 1087 //===----------------------------------------------------------------------===// 1088 // stat cache Serialization 1089 //===----------------------------------------------------------------------===// 1090 1091 namespace { 1092 // Trait used for the on-disk hash table of stat cache results. 1093 class ASTStatCacheTrait { 1094 public: 1095 typedef const char * key_type; 1096 typedef key_type key_type_ref; 1097 1098 typedef struct stat data_type; 1099 typedef const data_type &data_type_ref; 1100 ComputeHash(const char * path)1101 static unsigned ComputeHash(const char *path) { 1102 return llvm::HashString(path); 1103 } 1104 1105 std::pair<unsigned,unsigned> EmitKeyDataLength(raw_ostream & Out,const char * path,data_type_ref Data)1106 EmitKeyDataLength(raw_ostream& Out, const char *path, 1107 data_type_ref Data) { 1108 unsigned StrLen = strlen(path); 1109 clang::io::Emit16(Out, StrLen); 1110 unsigned DataLen = 4 + 4 + 2 + 8 + 8; 1111 clang::io::Emit8(Out, DataLen); 1112 return std::make_pair(StrLen + 1, DataLen); 1113 } 1114 EmitKey(raw_ostream & Out,const char * path,unsigned KeyLen)1115 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) { 1116 Out.write(path, KeyLen); 1117 } 1118 EmitData(raw_ostream & Out,key_type_ref,data_type_ref Data,unsigned DataLen)1119 void EmitData(raw_ostream &Out, key_type_ref, 1120 data_type_ref Data, unsigned DataLen) { 1121 using namespace clang::io; 1122 uint64_t Start = Out.tell(); (void)Start; 1123 1124 Emit32(Out, (uint32_t) Data.st_ino); 1125 Emit32(Out, (uint32_t) Data.st_dev); 1126 Emit16(Out, (uint16_t) Data.st_mode); 1127 Emit64(Out, (uint64_t) Data.st_mtime); 1128 Emit64(Out, (uint64_t) Data.st_size); 1129 1130 assert(Out.tell() - Start == DataLen && "Wrong data length"); 1131 } 1132 }; 1133 } // end anonymous namespace 1134 1135 /// \brief Write the stat() system call cache to the AST file. WriteStatCache(MemorizeStatCalls & StatCalls)1136 void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) { 1137 // Build the on-disk hash table containing information about every 1138 // stat() call. 1139 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator; 1140 unsigned NumStatEntries = 0; 1141 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(), 1142 StatEnd = StatCalls.end(); 1143 Stat != StatEnd; ++Stat, ++NumStatEntries) { 1144 StringRef Filename = Stat->first(); 1145 Generator.insert(Filename.data(), Stat->second); 1146 } 1147 1148 // Create the on-disk hash table in a buffer. 1149 SmallString<4096> StatCacheData; 1150 uint32_t BucketOffset; 1151 { 1152 llvm::raw_svector_ostream Out(StatCacheData); 1153 // Make sure that no bucket is at offset 0 1154 clang::io::Emit32(Out, 0); 1155 BucketOffset = Generator.Emit(Out); 1156 } 1157 1158 // Create a blob abbreviation 1159 using namespace llvm; 1160 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1161 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE)); 1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1165 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev); 1166 1167 // Write the stat cache 1168 RecordData Record; 1169 Record.push_back(STAT_CACHE); 1170 Record.push_back(BucketOffset); 1171 Record.push_back(NumStatEntries); 1172 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str()); 1173 } 1174 1175 //===----------------------------------------------------------------------===// 1176 // Source Manager Serialization 1177 //===----------------------------------------------------------------------===// 1178 1179 /// \brief Create an abbreviation for the SLocEntry that refers to a 1180 /// file. CreateSLocFileAbbrev(llvm::BitstreamWriter & Stream)1181 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { 1182 using namespace llvm; 1183 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1184 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY)); 1185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1189 // FileEntry fields. 1190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size 1191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time 1192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden 1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs 1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex 1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls 1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1197 return Stream.EmitAbbrev(Abbrev); 1198 } 1199 1200 /// \brief Create an abbreviation for the SLocEntry that refers to a 1201 /// buffer. CreateSLocBufferAbbrev(llvm::BitstreamWriter & Stream)1202 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { 1203 using namespace llvm; 1204 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1205 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY)); 1206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob 1211 return Stream.EmitAbbrev(Abbrev); 1212 } 1213 1214 /// \brief Create an abbreviation for the SLocEntry that refers to a 1215 /// buffer's blob. CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter & Stream)1216 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { 1217 using namespace llvm; 1218 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1219 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB)); 1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob 1221 return Stream.EmitAbbrev(Abbrev); 1222 } 1223 1224 /// \brief Create an abbreviation for the SLocEntry that refers to a macro 1225 /// expansion. CreateSLocExpansionAbbrev(llvm::BitstreamWriter & Stream)1226 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) { 1227 using namespace llvm; 1228 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1229 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY)); 1230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location 1232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location 1233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location 1234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length 1235 return Stream.EmitAbbrev(Abbrev); 1236 } 1237 1238 namespace { 1239 // Trait used for the on-disk hash table of header search information. 1240 class HeaderFileInfoTrait { 1241 ASTWriter &Writer; 1242 const HeaderSearch &HS; 1243 1244 // Keep track of the framework names we've used during serialization. 1245 SmallVector<char, 128> FrameworkStringData; 1246 llvm::StringMap<unsigned> FrameworkNameOffset; 1247 1248 public: HeaderFileInfoTrait(ASTWriter & Writer,const HeaderSearch & HS)1249 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS) 1250 : Writer(Writer), HS(HS) { } 1251 1252 typedef const char *key_type; 1253 typedef key_type key_type_ref; 1254 1255 typedef HeaderFileInfo data_type; 1256 typedef const data_type &data_type_ref; 1257 ComputeHash(const char * path)1258 static unsigned ComputeHash(const char *path) { 1259 // The hash is based only on the filename portion of the key, so that the 1260 // reader can match based on filenames when symlinking or excess path 1261 // elements ("foo/../", "../") change the form of the name. However, 1262 // complete path is still the key. 1263 return llvm::HashString(llvm::sys::path::filename(path)); 1264 } 1265 1266 std::pair<unsigned,unsigned> EmitKeyDataLength(raw_ostream & Out,const char * path,data_type_ref Data)1267 EmitKeyDataLength(raw_ostream& Out, const char *path, 1268 data_type_ref Data) { 1269 unsigned StrLen = strlen(path); 1270 clang::io::Emit16(Out, StrLen); 1271 unsigned DataLen = 1 + 2 + 4 + 4; 1272 clang::io::Emit8(Out, DataLen); 1273 return std::make_pair(StrLen + 1, DataLen); 1274 } 1275 EmitKey(raw_ostream & Out,const char * path,unsigned KeyLen)1276 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) { 1277 Out.write(path, KeyLen); 1278 } 1279 EmitData(raw_ostream & Out,key_type_ref,data_type_ref Data,unsigned DataLen)1280 void EmitData(raw_ostream &Out, key_type_ref, 1281 data_type_ref Data, unsigned DataLen) { 1282 using namespace clang::io; 1283 uint64_t Start = Out.tell(); (void)Start; 1284 1285 unsigned char Flags = (Data.isImport << 5) 1286 | (Data.isPragmaOnce << 4) 1287 | (Data.DirInfo << 2) 1288 | (Data.Resolved << 1) 1289 | Data.IndexHeaderMapHeader; 1290 Emit8(Out, (uint8_t)Flags); 1291 Emit16(Out, (uint16_t) Data.NumIncludes); 1292 1293 if (!Data.ControllingMacro) 1294 Emit32(Out, (uint32_t)Data.ControllingMacroID); 1295 else 1296 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro)); 1297 1298 unsigned Offset = 0; 1299 if (!Data.Framework.empty()) { 1300 // If this header refers into a framework, save the framework name. 1301 llvm::StringMap<unsigned>::iterator Pos 1302 = FrameworkNameOffset.find(Data.Framework); 1303 if (Pos == FrameworkNameOffset.end()) { 1304 Offset = FrameworkStringData.size() + 1; 1305 FrameworkStringData.append(Data.Framework.begin(), 1306 Data.Framework.end()); 1307 FrameworkStringData.push_back(0); 1308 1309 FrameworkNameOffset[Data.Framework] = Offset; 1310 } else 1311 Offset = Pos->second; 1312 } 1313 Emit32(Out, Offset); 1314 1315 assert(Out.tell() - Start == DataLen && "Wrong data length"); 1316 } 1317 strings_begin() const1318 const char *strings_begin() const { return FrameworkStringData.begin(); } strings_end() const1319 const char *strings_end() const { return FrameworkStringData.end(); } 1320 }; 1321 } // end anonymous namespace 1322 1323 /// \brief Write the header search block for the list of files that 1324 /// 1325 /// \param HS The header search structure to save. 1326 /// 1327 /// \param Chain Whether we're creating a chained AST file. WriteHeaderSearch(const HeaderSearch & HS,StringRef isysroot)1328 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) { 1329 SmallVector<const FileEntry *, 16> FilesByUID; 1330 HS.getFileMgr().GetUniqueIDMapping(FilesByUID); 1331 1332 if (FilesByUID.size() > HS.header_file_size()) 1333 FilesByUID.resize(HS.header_file_size()); 1334 1335 HeaderFileInfoTrait GeneratorTrait(*this, HS); 1336 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator; 1337 SmallVector<const char *, 4> SavedStrings; 1338 unsigned NumHeaderSearchEntries = 0; 1339 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) { 1340 const FileEntry *File = FilesByUID[UID]; 1341 if (!File) 1342 continue; 1343 1344 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo 1345 // from the external source if it was not provided already. 1346 const HeaderFileInfo &HFI = HS.getFileInfo(File); 1347 if (HFI.External && Chain) 1348 continue; 1349 1350 // Turn the file name into an absolute path, if it isn't already. 1351 const char *Filename = File->getName(); 1352 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1353 1354 // If we performed any translation on the file name at all, we need to 1355 // save this string, since the generator will refer to it later. 1356 if (Filename != File->getName()) { 1357 Filename = strdup(Filename); 1358 SavedStrings.push_back(Filename); 1359 } 1360 1361 Generator.insert(Filename, HFI, GeneratorTrait); 1362 ++NumHeaderSearchEntries; 1363 } 1364 1365 // Create the on-disk hash table in a buffer. 1366 SmallString<4096> TableData; 1367 uint32_t BucketOffset; 1368 { 1369 llvm::raw_svector_ostream Out(TableData); 1370 // Make sure that no bucket is at offset 0 1371 clang::io::Emit32(Out, 0); 1372 BucketOffset = Generator.Emit(Out, GeneratorTrait); 1373 } 1374 1375 // Create a blob abbreviation 1376 using namespace llvm; 1377 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1378 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE)); 1379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1383 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev); 1384 1385 // Write the header search table 1386 RecordData Record; 1387 Record.push_back(HEADER_SEARCH_TABLE); 1388 Record.push_back(BucketOffset); 1389 Record.push_back(NumHeaderSearchEntries); 1390 Record.push_back(TableData.size()); 1391 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end()); 1392 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str()); 1393 1394 // Free all of the strings we had to duplicate. 1395 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I) 1396 free((void*)SavedStrings[I]); 1397 } 1398 1399 /// \brief Writes the block containing the serialized form of the 1400 /// source manager. 1401 /// 1402 /// TODO: We should probably use an on-disk hash table (stored in a 1403 /// blob), indexed based on the file name, so that we only create 1404 /// entries for files that we actually need. In the common case (no 1405 /// errors), we probably won't have to create file entries for any of 1406 /// the files in the AST. WriteSourceManagerBlock(SourceManager & SourceMgr,const Preprocessor & PP,StringRef isysroot)1407 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, 1408 const Preprocessor &PP, 1409 StringRef isysroot) { 1410 RecordData Record; 1411 1412 // Enter the source manager block. 1413 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3); 1414 1415 // Abbreviations for the various kinds of source-location entries. 1416 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); 1417 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); 1418 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); 1419 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream); 1420 1421 // Write out the source location entry table. We skip the first 1422 // entry, which is always the same dummy entry. 1423 std::vector<uint32_t> SLocEntryOffsets; 1424 // Write out the offsets of only source location file entries. 1425 // We will go through them in ASTReader::validateFileEntries(). 1426 std::vector<uint32_t> SLocFileEntryOffsets; 1427 RecordData PreloadSLocs; 1428 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1); 1429 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); 1430 I != N; ++I) { 1431 // Get this source location entry. 1432 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I); 1433 1434 // Record the offset of this source-location entry. 1435 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); 1436 1437 // Figure out which record code to use. 1438 unsigned Code; 1439 if (SLoc->isFile()) { 1440 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache(); 1441 if (Cache->OrigEntry) { 1442 Code = SM_SLOC_FILE_ENTRY; 1443 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo()); 1444 } else 1445 Code = SM_SLOC_BUFFER_ENTRY; 1446 } else 1447 Code = SM_SLOC_EXPANSION_ENTRY; 1448 Record.clear(); 1449 Record.push_back(Code); 1450 1451 // Starting offset of this entry within this module, so skip the dummy. 1452 Record.push_back(SLoc->getOffset() - 2); 1453 if (SLoc->isFile()) { 1454 const SrcMgr::FileInfo &File = SLoc->getFile(); 1455 Record.push_back(File.getIncludeLoc().getRawEncoding()); 1456 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding 1457 Record.push_back(File.hasLineDirectives()); 1458 1459 const SrcMgr::ContentCache *Content = File.getContentCache(); 1460 if (Content->OrigEntry) { 1461 assert(Content->OrigEntry == Content->ContentsEntry && 1462 "Writing to AST an overridden file is not supported"); 1463 1464 // The source location entry is a file. The blob associated 1465 // with this entry is the file name. 1466 1467 // Emit size/modification time for this file. 1468 Record.push_back(Content->OrigEntry->getSize()); 1469 Record.push_back(Content->OrigEntry->getModificationTime()); 1470 Record.push_back(Content->BufferOverridden); 1471 Record.push_back(File.NumCreatedFIDs); 1472 1473 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc); 1474 if (FDI != FileDeclIDs.end()) { 1475 Record.push_back(FDI->second->FirstDeclIndex); 1476 Record.push_back(FDI->second->DeclIDs.size()); 1477 } else { 1478 Record.push_back(0); 1479 Record.push_back(0); 1480 } 1481 1482 // Turn the file name into an absolute path, if it isn't already. 1483 const char *Filename = Content->OrigEntry->getName(); 1484 SmallString<128> FilePath(Filename); 1485 1486 // Ask the file manager to fixup the relative path for us. This will 1487 // honor the working directory. 1488 SourceMgr.getFileManager().FixupRelativePath(FilePath); 1489 1490 // FIXME: This call to make_absolute shouldn't be necessary, the 1491 // call to FixupRelativePath should always return an absolute path. 1492 llvm::sys::fs::make_absolute(FilePath); 1493 Filename = FilePath.c_str(); 1494 1495 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1496 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename); 1497 1498 if (Content->BufferOverridden) { 1499 Record.clear(); 1500 Record.push_back(SM_SLOC_BUFFER_BLOB); 1501 const llvm::MemoryBuffer *Buffer 1502 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); 1503 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, 1504 StringRef(Buffer->getBufferStart(), 1505 Buffer->getBufferSize() + 1)); 1506 } 1507 } else { 1508 // The source location entry is a buffer. The blob associated 1509 // with this entry contains the contents of the buffer. 1510 1511 // We add one to the size so that we capture the trailing NULL 1512 // that is required by llvm::MemoryBuffer::getMemBuffer (on 1513 // the reader side). 1514 const llvm::MemoryBuffer *Buffer 1515 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); 1516 const char *Name = Buffer->getBufferIdentifier(); 1517 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, 1518 StringRef(Name, strlen(Name) + 1)); 1519 Record.clear(); 1520 Record.push_back(SM_SLOC_BUFFER_BLOB); 1521 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, 1522 StringRef(Buffer->getBufferStart(), 1523 Buffer->getBufferSize() + 1)); 1524 1525 if (strcmp(Name, "<built-in>") == 0) { 1526 PreloadSLocs.push_back(SLocEntryOffsets.size()); 1527 } 1528 } 1529 } else { 1530 // The source location entry is a macro expansion. 1531 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion(); 1532 Record.push_back(Expansion.getSpellingLoc().getRawEncoding()); 1533 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding()); 1534 Record.push_back(Expansion.isMacroArgExpansion() ? 0 1535 : Expansion.getExpansionLocEnd().getRawEncoding()); 1536 1537 // Compute the token length for this macro expansion. 1538 unsigned NextOffset = SourceMgr.getNextLocalOffset(); 1539 if (I + 1 != N) 1540 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset(); 1541 Record.push_back(NextOffset - SLoc->getOffset() - 1); 1542 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record); 1543 } 1544 } 1545 1546 Stream.ExitBlock(); 1547 1548 if (SLocEntryOffsets.empty()) 1549 return; 1550 1551 // Write the source-location offsets table into the AST block. This 1552 // table is used for lazily loading source-location information. 1553 using namespace llvm; 1554 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1555 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS)); 1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs 1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size 1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets 1559 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); 1560 1561 Record.clear(); 1562 Record.push_back(SOURCE_LOCATION_OFFSETS); 1563 Record.push_back(SLocEntryOffsets.size()); 1564 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy 1565 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets)); 1566 1567 Abbrev = new BitCodeAbbrev(); 1568 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS)); 1569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs 1570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets 1571 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); 1572 1573 Record.clear(); 1574 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS); 1575 Record.push_back(SLocFileEntryOffsets.size()); 1576 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record, 1577 data(SLocFileEntryOffsets)); 1578 1579 // Write the source location entry preloads array, telling the AST 1580 // reader which source locations entries it should load eagerly. 1581 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs); 1582 1583 // Write the line table. It depends on remapping working, so it must come 1584 // after the source location offsets. 1585 if (SourceMgr.hasLineTable()) { 1586 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1587 1588 Record.clear(); 1589 // Emit the file names 1590 Record.push_back(LineTable.getNumFilenames()); 1591 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { 1592 // Emit the file name 1593 const char *Filename = LineTable.getFilename(I); 1594 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1595 unsigned FilenameLen = Filename? strlen(Filename) : 0; 1596 Record.push_back(FilenameLen); 1597 if (FilenameLen) 1598 Record.insert(Record.end(), Filename, Filename + FilenameLen); 1599 } 1600 1601 // Emit the line entries 1602 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); 1603 L != LEnd; ++L) { 1604 // Only emit entries for local files. 1605 if (L->first < 0) 1606 continue; 1607 1608 // Emit the file ID 1609 Record.push_back(L->first); 1610 1611 // Emit the line entries 1612 Record.push_back(L->second.size()); 1613 for (std::vector<LineEntry>::iterator LE = L->second.begin(), 1614 LEEnd = L->second.end(); 1615 LE != LEEnd; ++LE) { 1616 Record.push_back(LE->FileOffset); 1617 Record.push_back(LE->LineNo); 1618 Record.push_back(LE->FilenameID); 1619 Record.push_back((unsigned)LE->FileKind); 1620 Record.push_back(LE->IncludeOffset); 1621 } 1622 } 1623 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record); 1624 } 1625 } 1626 1627 //===----------------------------------------------------------------------===// 1628 // Preprocessor Serialization 1629 //===----------------------------------------------------------------------===// 1630 compareMacroDefinitions(const void * XPtr,const void * YPtr)1631 static int compareMacroDefinitions(const void *XPtr, const void *YPtr) { 1632 const std::pair<const IdentifierInfo *, MacroInfo *> &X = 1633 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr; 1634 const std::pair<const IdentifierInfo *, MacroInfo *> &Y = 1635 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr; 1636 return X.first->getName().compare(Y.first->getName()); 1637 } 1638 1639 /// \brief Writes the block containing the serialized form of the 1640 /// preprocessor. 1641 /// WritePreprocessor(const Preprocessor & PP,bool IsModule)1642 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) { 1643 PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); 1644 if (PPRec) 1645 WritePreprocessorDetail(*PPRec); 1646 1647 RecordData Record; 1648 1649 // If the preprocessor __COUNTER__ value has been bumped, remember it. 1650 if (PP.getCounterValue() != 0) { 1651 Record.push_back(PP.getCounterValue()); 1652 Stream.EmitRecord(PP_COUNTER_VALUE, Record); 1653 Record.clear(); 1654 } 1655 1656 // Enter the preprocessor block. 1657 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); 1658 1659 // If the AST file contains __DATE__ or __TIME__ emit a warning about this. 1660 // FIXME: use diagnostics subsystem for localization etc. 1661 if (PP.SawDateOrTime()) 1662 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); 1663 1664 1665 // Loop over all the macro definitions that are live at the end of the file, 1666 // emitting each to the PP section. 1667 1668 // Construct the list of macro definitions that need to be serialized. 1669 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2> 1670 MacrosToEmit; 1671 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen; 1672 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0), 1673 E = PP.macro_end(Chain == 0); 1674 I != E; ++I) { 1675 const IdentifierInfo *Name = I->first; 1676 if (!IsModule || I->second->isPublic()) { 1677 MacroDefinitionsSeen.insert(Name); 1678 MacrosToEmit.push_back(std::make_pair(I->first, I->second)); 1679 } 1680 } 1681 1682 // Sort the set of macro definitions that need to be serialized by the 1683 // name of the macro, to provide a stable ordering. 1684 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(), 1685 &compareMacroDefinitions); 1686 1687 // Resolve any identifiers that defined macros at the time they were 1688 // deserialized, adding them to the list of macros to emit (if appropriate). 1689 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) { 1690 IdentifierInfo *Name 1691 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]); 1692 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name)) 1693 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name))); 1694 } 1695 1696 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) { 1697 const IdentifierInfo *Name = MacrosToEmit[I].first; 1698 MacroInfo *MI = MacrosToEmit[I].second; 1699 if (!MI) 1700 continue; 1701 1702 // Don't emit builtin macros like __LINE__ to the AST file unless they have 1703 // been redefined by the header (in which case they are not isBuiltinMacro). 1704 // Also skip macros from a AST file if we're chaining. 1705 1706 // FIXME: There is a (probably minor) optimization we could do here, if 1707 // the macro comes from the original PCH but the identifier comes from a 1708 // chained PCH, by storing the offset into the original PCH rather than 1709 // writing the macro definition a second time. 1710 if (MI->isBuiltinMacro() || 1711 (Chain && 1712 Name->isFromAST() && !Name->hasChangedSinceDeserialization() && 1713 MI->isFromAST() && !MI->hasChangedAfterLoad())) 1714 continue; 1715 1716 AddIdentifierRef(Name, Record); 1717 MacroOffsets[Name] = Stream.GetCurrentBitNo(); 1718 Record.push_back(MI->getDefinitionLoc().getRawEncoding()); 1719 Record.push_back(MI->isUsed()); 1720 Record.push_back(MI->isPublic()); 1721 AddSourceLocation(MI->getVisibilityLocation(), Record); 1722 unsigned Code; 1723 if (MI->isObjectLike()) { 1724 Code = PP_MACRO_OBJECT_LIKE; 1725 } else { 1726 Code = PP_MACRO_FUNCTION_LIKE; 1727 1728 Record.push_back(MI->isC99Varargs()); 1729 Record.push_back(MI->isGNUVarargs()); 1730 Record.push_back(MI->getNumArgs()); 1731 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); 1732 I != E; ++I) 1733 AddIdentifierRef(*I, Record); 1734 } 1735 1736 // If we have a detailed preprocessing record, record the macro definition 1737 // ID that corresponds to this macro. 1738 if (PPRec) 1739 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]); 1740 1741 Stream.EmitRecord(Code, Record); 1742 Record.clear(); 1743 1744 // Emit the tokens array. 1745 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { 1746 // Note that we know that the preprocessor does not have any annotation 1747 // tokens in it because they are created by the parser, and thus can't be 1748 // in a macro definition. 1749 const Token &Tok = MI->getReplacementToken(TokNo); 1750 1751 Record.push_back(Tok.getLocation().getRawEncoding()); 1752 Record.push_back(Tok.getLength()); 1753 1754 // FIXME: When reading literal tokens, reconstruct the literal pointer if 1755 // it is needed. 1756 AddIdentifierRef(Tok.getIdentifierInfo(), Record); 1757 // FIXME: Should translate token kind to a stable encoding. 1758 Record.push_back(Tok.getKind()); 1759 // FIXME: Should translate token flags to a stable encoding. 1760 Record.push_back(Tok.getFlags()); 1761 1762 Stream.EmitRecord(PP_TOKEN, Record); 1763 Record.clear(); 1764 } 1765 ++NumMacros; 1766 } 1767 Stream.ExitBlock(); 1768 } 1769 WritePreprocessorDetail(PreprocessingRecord & PPRec)1770 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) { 1771 if (PPRec.local_begin() == PPRec.local_end()) 1772 return; 1773 1774 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets; 1775 1776 // Enter the preprocessor block. 1777 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3); 1778 1779 // If the preprocessor has a preprocessing record, emit it. 1780 unsigned NumPreprocessingRecords = 0; 1781 using namespace llvm; 1782 1783 // Set up the abbreviation for 1784 unsigned InclusionAbbrev = 0; 1785 { 1786 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1787 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE)); 1788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length 1789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes 1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind 1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1792 InclusionAbbrev = Stream.EmitAbbrev(Abbrev); 1793 } 1794 1795 unsigned FirstPreprocessorEntityID 1796 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0) 1797 + NUM_PREDEF_PP_ENTITY_IDS; 1798 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID; 1799 RecordData Record; 1800 for (PreprocessingRecord::iterator E = PPRec.local_begin(), 1801 EEnd = PPRec.local_end(); 1802 E != EEnd; 1803 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) { 1804 Record.clear(); 1805 1806 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(), 1807 Stream.GetCurrentBitNo())); 1808 1809 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { 1810 // Record this macro definition's ID. 1811 MacroDefinitions[MD] = NextPreprocessorEntityID; 1812 1813 AddIdentifierRef(MD->getName(), Record); 1814 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record); 1815 continue; 1816 } 1817 1818 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) { 1819 Record.push_back(ME->isBuiltinMacro()); 1820 if (ME->isBuiltinMacro()) 1821 AddIdentifierRef(ME->getName(), Record); 1822 else 1823 Record.push_back(MacroDefinitions[ME->getDefinition()]); 1824 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record); 1825 continue; 1826 } 1827 1828 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { 1829 Record.push_back(PPD_INCLUSION_DIRECTIVE); 1830 Record.push_back(ID->getFileName().size()); 1831 Record.push_back(ID->wasInQuotes()); 1832 Record.push_back(static_cast<unsigned>(ID->getKind())); 1833 SmallString<64> Buffer; 1834 Buffer += ID->getFileName(); 1835 // Check that the FileEntry is not null because it was not resolved and 1836 // we create a PCH even with compiler errors. 1837 if (ID->getFile()) 1838 Buffer += ID->getFile()->getName(); 1839 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); 1840 continue; 1841 } 1842 1843 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter"); 1844 } 1845 Stream.ExitBlock(); 1846 1847 // Write the offsets table for the preprocessing record. 1848 if (NumPreprocessingRecords > 0) { 1849 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords); 1850 1851 // Write the offsets table for identifier IDs. 1852 using namespace llvm; 1853 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1854 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS)); 1855 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity 1856 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1857 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1858 1859 Record.clear(); 1860 Record.push_back(PPD_ENTITIES_OFFSETS); 1861 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS); 1862 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record, 1863 data(PreprocessedEntityOffsets)); 1864 } 1865 } 1866 getSubmoduleID(Module * Mod)1867 unsigned ASTWriter::getSubmoduleID(Module *Mod) { 1868 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod); 1869 if (Known != SubmoduleIDs.end()) 1870 return Known->second; 1871 1872 return SubmoduleIDs[Mod] = NextSubmoduleID++; 1873 } 1874 1875 /// \brief Compute the number of modules within the given tree (including the 1876 /// given module). getNumberOfModules(Module * Mod)1877 static unsigned getNumberOfModules(Module *Mod) { 1878 unsigned ChildModules = 0; 1879 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 1880 SubEnd = Mod->submodule_end(); 1881 Sub != SubEnd; ++Sub) 1882 ChildModules += getNumberOfModules(*Sub); 1883 1884 return ChildModules + 1; 1885 } 1886 WriteSubmodules(Module * WritingModule)1887 void ASTWriter::WriteSubmodules(Module *WritingModule) { 1888 // Determine the dependencies of our module and each of it's submodules. 1889 // FIXME: This feels like it belongs somewhere else, but there are no 1890 // other consumers of this information. 1891 SourceManager &SrcMgr = PP->getSourceManager(); 1892 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap(); 1893 for (ASTContext::import_iterator I = Context->local_import_begin(), 1894 IEnd = Context->local_import_end(); 1895 I != IEnd; ++I) { 1896 if (Module *ImportedFrom 1897 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(), 1898 SrcMgr))) { 1899 ImportedFrom->Imports.push_back(I->getImportedModule()); 1900 } 1901 } 1902 1903 // Enter the submodule description block. 1904 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE); 1905 1906 // Write the abbreviations needed for the submodules block. 1907 using namespace llvm; 1908 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1909 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION)); 1910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID 1911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent 1912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework 1913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit 1914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem 1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules... 1916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit... 1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild... 1918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 1919 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev); 1920 1921 Abbrev = new BitCodeAbbrev(); 1922 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER)); 1923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 1924 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev); 1925 1926 Abbrev = new BitCodeAbbrev(); 1927 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER)); 1928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 1929 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev); 1930 1931 Abbrev = new BitCodeAbbrev(); 1932 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR)); 1933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 1934 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev); 1935 1936 Abbrev = new BitCodeAbbrev(); 1937 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES)); 1938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature 1939 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev); 1940 1941 // Write the submodule metadata block. 1942 RecordData Record; 1943 Record.push_back(getNumberOfModules(WritingModule)); 1944 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS); 1945 Stream.EmitRecord(SUBMODULE_METADATA, Record); 1946 1947 // Write all of the submodules. 1948 std::queue<Module *> Q; 1949 Q.push(WritingModule); 1950 while (!Q.empty()) { 1951 Module *Mod = Q.front(); 1952 Q.pop(); 1953 unsigned ID = getSubmoduleID(Mod); 1954 1955 // Emit the definition of the block. 1956 Record.clear(); 1957 Record.push_back(SUBMODULE_DEFINITION); 1958 Record.push_back(ID); 1959 if (Mod->Parent) { 1960 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?"); 1961 Record.push_back(SubmoduleIDs[Mod->Parent]); 1962 } else { 1963 Record.push_back(0); 1964 } 1965 Record.push_back(Mod->IsFramework); 1966 Record.push_back(Mod->IsExplicit); 1967 Record.push_back(Mod->IsSystem); 1968 Record.push_back(Mod->InferSubmodules); 1969 Record.push_back(Mod->InferExplicitSubmodules); 1970 Record.push_back(Mod->InferExportWildcard); 1971 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name); 1972 1973 // Emit the requirements. 1974 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) { 1975 Record.clear(); 1976 Record.push_back(SUBMODULE_REQUIRES); 1977 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, 1978 Mod->Requires[I].data(), 1979 Mod->Requires[I].size()); 1980 } 1981 1982 // Emit the umbrella header, if there is one. 1983 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) { 1984 Record.clear(); 1985 Record.push_back(SUBMODULE_UMBRELLA_HEADER); 1986 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record, 1987 UmbrellaHeader->getName()); 1988 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) { 1989 Record.clear(); 1990 Record.push_back(SUBMODULE_UMBRELLA_DIR); 1991 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record, 1992 UmbrellaDir->getName()); 1993 } 1994 1995 // Emit the headers. 1996 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) { 1997 Record.clear(); 1998 Record.push_back(SUBMODULE_HEADER); 1999 Stream.EmitRecordWithBlob(HeaderAbbrev, Record, 2000 Mod->Headers[I]->getName()); 2001 } 2002 2003 // Emit the imports. 2004 if (!Mod->Imports.empty()) { 2005 Record.clear(); 2006 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) { 2007 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]); 2008 assert(ImportedID && "Unknown submodule!"); 2009 Record.push_back(ImportedID); 2010 } 2011 Stream.EmitRecord(SUBMODULE_IMPORTS, Record); 2012 } 2013 2014 // Emit the exports. 2015 if (!Mod->Exports.empty()) { 2016 Record.clear(); 2017 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) { 2018 if (Module *Exported = Mod->Exports[I].getPointer()) { 2019 unsigned ExportedID = SubmoduleIDs[Exported]; 2020 assert(ExportedID > 0 && "Unknown submodule ID?"); 2021 Record.push_back(ExportedID); 2022 } else { 2023 Record.push_back(0); 2024 } 2025 2026 Record.push_back(Mod->Exports[I].getInt()); 2027 } 2028 Stream.EmitRecord(SUBMODULE_EXPORTS, Record); 2029 } 2030 2031 // Queue up the submodules of this module. 2032 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 2033 SubEnd = Mod->submodule_end(); 2034 Sub != SubEnd; ++Sub) 2035 Q.push(*Sub); 2036 } 2037 2038 Stream.ExitBlock(); 2039 2040 assert((NextSubmoduleID - FirstSubmoduleID 2041 == getNumberOfModules(WritingModule)) && "Wrong # of submodules"); 2042 } 2043 2044 serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc)2045 ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) { 2046 if (Loc.isInvalid() || !WritingModule) 2047 return 0; // No submodule 2048 2049 // Find the module that owns this location. 2050 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap(); 2051 Module *OwningMod 2052 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager())); 2053 if (!OwningMod) 2054 return 0; 2055 2056 // Check whether this submodule is part of our own module. 2057 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule)) 2058 return 0; 2059 2060 return getSubmoduleID(OwningMod); 2061 } 2062 WritePragmaDiagnosticMappings(const DiagnosticsEngine & Diag)2063 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) { 2064 RecordData Record; 2065 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator 2066 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end(); 2067 I != E; ++I) { 2068 const DiagnosticsEngine::DiagStatePoint &point = *I; 2069 if (point.Loc.isInvalid()) 2070 continue; 2071 2072 Record.push_back(point.Loc.getRawEncoding()); 2073 for (DiagnosticsEngine::DiagState::const_iterator 2074 I = point.State->begin(), E = point.State->end(); I != E; ++I) { 2075 if (I->second.isPragma()) { 2076 Record.push_back(I->first); 2077 Record.push_back(I->second.getMapping()); 2078 } 2079 } 2080 Record.push_back(-1); // mark the end of the diag/map pairs for this 2081 // location. 2082 } 2083 2084 if (!Record.empty()) 2085 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record); 2086 } 2087 WriteCXXBaseSpecifiersOffsets()2088 void ASTWriter::WriteCXXBaseSpecifiersOffsets() { 2089 if (CXXBaseSpecifiersOffsets.empty()) 2090 return; 2091 2092 RecordData Record; 2093 2094 // Create a blob abbreviation for the C++ base specifiers offsets. 2095 using namespace llvm; 2096 2097 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2098 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS)); 2099 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 2100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2101 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2102 2103 // Write the base specifier offsets table. 2104 Record.clear(); 2105 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS); 2106 Record.push_back(CXXBaseSpecifiersOffsets.size()); 2107 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record, 2108 data(CXXBaseSpecifiersOffsets)); 2109 } 2110 2111 //===----------------------------------------------------------------------===// 2112 // Type Serialization 2113 //===----------------------------------------------------------------------===// 2114 2115 /// \brief Write the representation of a type to the AST stream. WriteType(QualType T)2116 void ASTWriter::WriteType(QualType T) { 2117 TypeIdx &Idx = TypeIdxs[T]; 2118 if (Idx.getIndex() == 0) // we haven't seen this type before. 2119 Idx = TypeIdx(NextTypeID++); 2120 2121 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST"); 2122 2123 // Record the offset for this type. 2124 unsigned Index = Idx.getIndex() - FirstTypeID; 2125 if (TypeOffsets.size() == Index) 2126 TypeOffsets.push_back(Stream.GetCurrentBitNo()); 2127 else if (TypeOffsets.size() < Index) { 2128 TypeOffsets.resize(Index + 1); 2129 TypeOffsets[Index] = Stream.GetCurrentBitNo(); 2130 } 2131 2132 RecordData Record; 2133 2134 // Emit the type's representation. 2135 ASTTypeWriter W(*this, Record); 2136 2137 if (T.hasLocalNonFastQualifiers()) { 2138 Qualifiers Qs = T.getLocalQualifiers(); 2139 AddTypeRef(T.getLocalUnqualifiedType(), Record); 2140 Record.push_back(Qs.getAsOpaqueValue()); 2141 W.Code = TYPE_EXT_QUAL; 2142 } else { 2143 switch (T->getTypeClass()) { 2144 // For all of the concrete, non-dependent types, call the 2145 // appropriate visitor function. 2146 #define TYPE(Class, Base) \ 2147 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; 2148 #define ABSTRACT_TYPE(Class, Base) 2149 #include "clang/AST/TypeNodes.def" 2150 } 2151 } 2152 2153 // Emit the serialized record. 2154 Stream.EmitRecord(W.Code, Record); 2155 2156 // Flush any expressions that were written as part of this type. 2157 FlushStmts(); 2158 } 2159 2160 //===----------------------------------------------------------------------===// 2161 // Declaration Serialization 2162 //===----------------------------------------------------------------------===// 2163 2164 /// \brief Write the block containing all of the declaration IDs 2165 /// lexically declared within the given DeclContext. 2166 /// 2167 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the 2168 /// bistream, or 0 if no block was written. WriteDeclContextLexicalBlock(ASTContext & Context,DeclContext * DC)2169 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, 2170 DeclContext *DC) { 2171 if (DC->decls_empty()) 2172 return 0; 2173 2174 uint64_t Offset = Stream.GetCurrentBitNo(); 2175 RecordData Record; 2176 Record.push_back(DECL_CONTEXT_LEXICAL); 2177 SmallVector<KindDeclIDPair, 64> Decls; 2178 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 2179 D != DEnd; ++D) 2180 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D))); 2181 2182 ++NumLexicalDeclContexts; 2183 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls)); 2184 return Offset; 2185 } 2186 WriteTypeDeclOffsets()2187 void ASTWriter::WriteTypeDeclOffsets() { 2188 using namespace llvm; 2189 RecordData Record; 2190 2191 // Write the type offsets array 2192 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2193 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET)); 2194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types 2195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index 2196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block 2197 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2198 Record.clear(); 2199 Record.push_back(TYPE_OFFSET); 2200 Record.push_back(TypeOffsets.size()); 2201 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS); 2202 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets)); 2203 2204 // Write the declaration offsets array 2205 Abbrev = new BitCodeAbbrev(); 2206 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET)); 2207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations 2208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID 2209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block 2210 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2211 Record.clear(); 2212 Record.push_back(DECL_OFFSET); 2213 Record.push_back(DeclOffsets.size()); 2214 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS); 2215 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets)); 2216 } 2217 WriteFileDeclIDsMap()2218 void ASTWriter::WriteFileDeclIDsMap() { 2219 using namespace llvm; 2220 RecordData Record; 2221 2222 // Join the vectors of DeclIDs from all files. 2223 SmallVector<DeclID, 256> FileSortedIDs; 2224 for (FileDeclIDsTy::iterator 2225 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) { 2226 DeclIDInFileInfo &Info = *FI->second; 2227 Info.FirstDeclIndex = FileSortedIDs.size(); 2228 for (LocDeclIDsTy::iterator 2229 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI) 2230 FileSortedIDs.push_back(DI->second); 2231 } 2232 2233 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2234 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS)); 2235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2236 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); 2237 Record.push_back(FILE_SORTED_DECLS); 2238 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs)); 2239 } 2240 2241 //===----------------------------------------------------------------------===// 2242 // Global Method Pool and Selector Serialization 2243 //===----------------------------------------------------------------------===// 2244 2245 namespace { 2246 // Trait used for the on-disk hash table used in the method pool. 2247 class ASTMethodPoolTrait { 2248 ASTWriter &Writer; 2249 2250 public: 2251 typedef Selector key_type; 2252 typedef key_type key_type_ref; 2253 2254 struct data_type { 2255 SelectorID ID; 2256 ObjCMethodList Instance, Factory; 2257 }; 2258 typedef const data_type& data_type_ref; 2259 ASTMethodPoolTrait(ASTWriter & Writer)2260 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { } 2261 ComputeHash(Selector Sel)2262 static unsigned ComputeHash(Selector Sel) { 2263 return serialization::ComputeHash(Sel); 2264 } 2265 2266 std::pair<unsigned,unsigned> EmitKeyDataLength(raw_ostream & Out,Selector Sel,data_type_ref Methods)2267 EmitKeyDataLength(raw_ostream& Out, Selector Sel, 2268 data_type_ref Methods) { 2269 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); 2270 clang::io::Emit16(Out, KeyLen); 2271 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts 2272 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2273 Method = Method->Next) 2274 if (Method->Method) 2275 DataLen += 4; 2276 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2277 Method = Method->Next) 2278 if (Method->Method) 2279 DataLen += 4; 2280 clang::io::Emit16(Out, DataLen); 2281 return std::make_pair(KeyLen, DataLen); 2282 } 2283 EmitKey(raw_ostream & Out,Selector Sel,unsigned)2284 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) { 2285 uint64_t Start = Out.tell(); 2286 assert((Start >> 32) == 0 && "Selector key offset too large"); 2287 Writer.SetSelectorOffset(Sel, Start); 2288 unsigned N = Sel.getNumArgs(); 2289 clang::io::Emit16(Out, N); 2290 if (N == 0) 2291 N = 1; 2292 for (unsigned I = 0; I != N; ++I) 2293 clang::io::Emit32(Out, 2294 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); 2295 } 2296 EmitData(raw_ostream & Out,key_type_ref,data_type_ref Methods,unsigned DataLen)2297 void EmitData(raw_ostream& Out, key_type_ref, 2298 data_type_ref Methods, unsigned DataLen) { 2299 uint64_t Start = Out.tell(); (void)Start; 2300 clang::io::Emit32(Out, Methods.ID); 2301 unsigned NumInstanceMethods = 0; 2302 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2303 Method = Method->Next) 2304 if (Method->Method) 2305 ++NumInstanceMethods; 2306 2307 unsigned NumFactoryMethods = 0; 2308 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2309 Method = Method->Next) 2310 if (Method->Method) 2311 ++NumFactoryMethods; 2312 2313 clang::io::Emit16(Out, NumInstanceMethods); 2314 clang::io::Emit16(Out, NumFactoryMethods); 2315 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2316 Method = Method->Next) 2317 if (Method->Method) 2318 clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); 2319 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2320 Method = Method->Next) 2321 if (Method->Method) 2322 clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); 2323 2324 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 2325 } 2326 }; 2327 } // end anonymous namespace 2328 2329 /// \brief Write ObjC data: selectors and the method pool. 2330 /// 2331 /// The method pool contains both instance and factory methods, stored 2332 /// in an on-disk hash table indexed by the selector. The hash table also 2333 /// contains an empty entry for every other selector known to Sema. WriteSelectors(Sema & SemaRef)2334 void ASTWriter::WriteSelectors(Sema &SemaRef) { 2335 using namespace llvm; 2336 2337 // Do we have to do anything at all? 2338 if (SemaRef.MethodPool.empty() && SelectorIDs.empty()) 2339 return; 2340 unsigned NumTableEntries = 0; 2341 // Create and write out the blob that contains selectors and the method pool. 2342 { 2343 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator; 2344 ASTMethodPoolTrait Trait(*this); 2345 2346 // Create the on-disk hash table representation. We walk through every 2347 // selector we've seen and look it up in the method pool. 2348 SelectorOffsets.resize(NextSelectorID - FirstSelectorID); 2349 for (llvm::DenseMap<Selector, SelectorID>::iterator 2350 I = SelectorIDs.begin(), E = SelectorIDs.end(); 2351 I != E; ++I) { 2352 Selector S = I->first; 2353 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S); 2354 ASTMethodPoolTrait::data_type Data = { 2355 I->second, 2356 ObjCMethodList(), 2357 ObjCMethodList() 2358 }; 2359 if (F != SemaRef.MethodPool.end()) { 2360 Data.Instance = F->second.first; 2361 Data.Factory = F->second.second; 2362 } 2363 // Only write this selector if it's not in an existing AST or something 2364 // changed. 2365 if (Chain && I->second < FirstSelectorID) { 2366 // Selector already exists. Did it change? 2367 bool changed = false; 2368 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method; 2369 M = M->Next) { 2370 if (!M->Method->isFromASTFile()) 2371 changed = true; 2372 } 2373 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method; 2374 M = M->Next) { 2375 if (!M->Method->isFromASTFile()) 2376 changed = true; 2377 } 2378 if (!changed) 2379 continue; 2380 } else if (Data.Instance.Method || Data.Factory.Method) { 2381 // A new method pool entry. 2382 ++NumTableEntries; 2383 } 2384 Generator.insert(S, Data, Trait); 2385 } 2386 2387 // Create the on-disk hash table in a buffer. 2388 SmallString<4096> MethodPool; 2389 uint32_t BucketOffset; 2390 { 2391 ASTMethodPoolTrait Trait(*this); 2392 llvm::raw_svector_ostream Out(MethodPool); 2393 // Make sure that no bucket is at offset 0 2394 clang::io::Emit32(Out, 0); 2395 BucketOffset = Generator.Emit(Out, Trait); 2396 } 2397 2398 // Create a blob abbreviation 2399 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2400 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL)); 2401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2404 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); 2405 2406 // Write the method pool 2407 RecordData Record; 2408 Record.push_back(METHOD_POOL); 2409 Record.push_back(BucketOffset); 2410 Record.push_back(NumTableEntries); 2411 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str()); 2412 2413 // Create a blob abbreviation for the selector table offsets. 2414 Abbrev = new BitCodeAbbrev(); 2415 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS)); 2416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 2417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID 2418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2419 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2420 2421 // Write the selector offsets table. 2422 Record.clear(); 2423 Record.push_back(SELECTOR_OFFSETS); 2424 Record.push_back(SelectorOffsets.size()); 2425 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS); 2426 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, 2427 data(SelectorOffsets)); 2428 } 2429 } 2430 2431 /// \brief Write the selectors referenced in @selector expression into AST file. WriteReferencedSelectorsPool(Sema & SemaRef)2432 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) { 2433 using namespace llvm; 2434 if (SemaRef.ReferencedSelectors.empty()) 2435 return; 2436 2437 RecordData Record; 2438 2439 // Note: this writes out all references even for a dependent AST. But it is 2440 // very tricky to fix, and given that @selector shouldn't really appear in 2441 // headers, probably not worth it. It's not a correctness issue. 2442 for (DenseMap<Selector, SourceLocation>::iterator S = 2443 SemaRef.ReferencedSelectors.begin(), 2444 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) { 2445 Selector Sel = (*S).first; 2446 SourceLocation Loc = (*S).second; 2447 AddSelectorRef(Sel, Record); 2448 AddSourceLocation(Loc, Record); 2449 } 2450 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record); 2451 } 2452 2453 //===----------------------------------------------------------------------===// 2454 // Identifier Table Serialization 2455 //===----------------------------------------------------------------------===// 2456 2457 namespace { 2458 class ASTIdentifierTableTrait { 2459 ASTWriter &Writer; 2460 Preprocessor &PP; 2461 IdentifierResolver &IdResolver; 2462 bool IsModule; 2463 2464 /// \brief Determines whether this is an "interesting" identifier 2465 /// that needs a full IdentifierInfo structure written into the hash 2466 /// table. isInterestingIdentifier(IdentifierInfo * II,MacroInfo * & Macro)2467 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) { 2468 if (II->isPoisoned() || 2469 II->isExtensionToken() || 2470 II->getObjCOrBuiltinID() || 2471 II->hasRevertedTokenIDToIdentifier() || 2472 II->getFETokenInfo<void>()) 2473 return true; 2474 2475 return hasMacroDefinition(II, Macro); 2476 } 2477 hasMacroDefinition(IdentifierInfo * II,MacroInfo * & Macro)2478 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) { 2479 if (!II->hasMacroDefinition()) 2480 return false; 2481 2482 if (Macro || (Macro = PP.getMacroInfo(II))) 2483 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic()); 2484 2485 return false; 2486 } 2487 2488 public: 2489 typedef IdentifierInfo* key_type; 2490 typedef key_type key_type_ref; 2491 2492 typedef IdentID data_type; 2493 typedef data_type data_type_ref; 2494 ASTIdentifierTableTrait(ASTWriter & Writer,Preprocessor & PP,IdentifierResolver & IdResolver,bool IsModule)2495 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP, 2496 IdentifierResolver &IdResolver, bool IsModule) 2497 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { } 2498 ComputeHash(const IdentifierInfo * II)2499 static unsigned ComputeHash(const IdentifierInfo* II) { 2500 return llvm::HashString(II->getName()); 2501 } 2502 2503 std::pair<unsigned,unsigned> EmitKeyDataLength(raw_ostream & Out,IdentifierInfo * II,IdentID ID)2504 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) { 2505 unsigned KeyLen = II->getLength() + 1; 2506 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 2507 MacroInfo *Macro = 0; 2508 if (isInterestingIdentifier(II, Macro)) { 2509 DataLen += 2; // 2 bytes for builtin ID, flags 2510 if (hasMacroDefinition(II, Macro)) 2511 DataLen += 8; 2512 2513 for (IdentifierResolver::iterator D = IdResolver.begin(II), 2514 DEnd = IdResolver.end(); 2515 D != DEnd; ++D) 2516 DataLen += sizeof(DeclID); 2517 } 2518 clang::io::Emit16(Out, DataLen); 2519 // We emit the key length after the data length so that every 2520 // string is preceded by a 16-bit length. This matches the PTH 2521 // format for storing identifiers. 2522 clang::io::Emit16(Out, KeyLen); 2523 return std::make_pair(KeyLen, DataLen); 2524 } 2525 EmitKey(raw_ostream & Out,const IdentifierInfo * II,unsigned KeyLen)2526 void EmitKey(raw_ostream& Out, const IdentifierInfo* II, 2527 unsigned KeyLen) { 2528 // Record the location of the key data. This is used when generating 2529 // the mapping from persistent IDs to strings. 2530 Writer.SetIdentifierOffset(II, Out.tell()); 2531 Out.write(II->getNameStart(), KeyLen); 2532 } 2533 EmitData(raw_ostream & Out,IdentifierInfo * II,IdentID ID,unsigned)2534 void EmitData(raw_ostream& Out, IdentifierInfo* II, 2535 IdentID ID, unsigned) { 2536 MacroInfo *Macro = 0; 2537 if (!isInterestingIdentifier(II, Macro)) { 2538 clang::io::Emit32(Out, ID << 1); 2539 return; 2540 } 2541 2542 clang::io::Emit32(Out, (ID << 1) | 0x01); 2543 uint32_t Bits = 0; 2544 bool HasMacroDefinition = hasMacroDefinition(II, Macro); 2545 Bits = (uint32_t)II->getObjCOrBuiltinID(); 2546 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader."); 2547 Bits = (Bits << 1) | unsigned(HasMacroDefinition); 2548 Bits = (Bits << 1) | unsigned(II->isExtensionToken()); 2549 Bits = (Bits << 1) | unsigned(II->isPoisoned()); 2550 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); 2551 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); 2552 clang::io::Emit16(Out, Bits); 2553 2554 if (HasMacroDefinition) { 2555 clang::io::Emit32(Out, Writer.getMacroOffset(II)); 2556 clang::io::Emit32(Out, 2557 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc())); 2558 } 2559 2560 // Emit the declaration IDs in reverse order, because the 2561 // IdentifierResolver provides the declarations as they would be 2562 // visible (e.g., the function "stat" would come before the struct 2563 // "stat"), but the ASTReader adds declarations to the end of the list 2564 // (so we need to see the struct "status" before the function "status"). 2565 // Only emit declarations that aren't from a chained PCH, though. 2566 SmallVector<Decl *, 16> Decls(IdResolver.begin(II), 2567 IdResolver.end()); 2568 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(), 2569 DEnd = Decls.rend(); 2570 D != DEnd; ++D) 2571 clang::io::Emit32(Out, Writer.getDeclID(*D)); 2572 } 2573 }; 2574 } // end anonymous namespace 2575 2576 /// \brief Write the identifier table into the AST file. 2577 /// 2578 /// The identifier table consists of a blob containing string data 2579 /// (the actual identifiers themselves) and a separate "offsets" index 2580 /// that maps identifier IDs to locations within the blob. WriteIdentifierTable(Preprocessor & PP,IdentifierResolver & IdResolver,bool IsModule)2581 void ASTWriter::WriteIdentifierTable(Preprocessor &PP, 2582 IdentifierResolver &IdResolver, 2583 bool IsModule) { 2584 using namespace llvm; 2585 2586 // Create and write out the blob that contains the identifier 2587 // strings. 2588 { 2589 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator; 2590 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule); 2591 2592 // Look for any identifiers that were named while processing the 2593 // headers, but are otherwise not needed. We add these to the hash 2594 // table to enable checking of the predefines buffer in the case 2595 // where the user adds new macro definitions when building the AST 2596 // file. 2597 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), 2598 IDEnd = PP.getIdentifierTable().end(); 2599 ID != IDEnd; ++ID) 2600 getIdentifierRef(ID->second); 2601 2602 // Create the on-disk hash table representation. We only store offsets 2603 // for identifiers that appear here for the first time. 2604 IdentifierOffsets.resize(NextIdentID - FirstIdentID); 2605 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator 2606 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); 2607 ID != IDEnd; ++ID) { 2608 assert(ID->first && "NULL identifier in identifier table"); 2609 if (!Chain || !ID->first->isFromAST() || 2610 ID->first->hasChangedSinceDeserialization()) 2611 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second, 2612 Trait); 2613 } 2614 2615 // Create the on-disk hash table in a buffer. 2616 SmallString<4096> IdentifierTable; 2617 uint32_t BucketOffset; 2618 { 2619 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule); 2620 llvm::raw_svector_ostream Out(IdentifierTable); 2621 // Make sure that no bucket is at offset 0 2622 clang::io::Emit32(Out, 0); 2623 BucketOffset = Generator.Emit(Out, Trait); 2624 } 2625 2626 // Create a blob abbreviation 2627 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2628 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE)); 2629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2631 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); 2632 2633 // Write the identifier table 2634 RecordData Record; 2635 Record.push_back(IDENTIFIER_TABLE); 2636 Record.push_back(BucketOffset); 2637 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str()); 2638 } 2639 2640 // Write the offsets table for identifier IDs. 2641 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2642 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET)); 2643 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers 2644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID 2645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2646 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2647 2648 RecordData Record; 2649 Record.push_back(IDENTIFIER_OFFSET); 2650 Record.push_back(IdentifierOffsets.size()); 2651 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS); 2652 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, 2653 data(IdentifierOffsets)); 2654 } 2655 2656 //===----------------------------------------------------------------------===// 2657 // DeclContext's Name Lookup Table Serialization 2658 //===----------------------------------------------------------------------===// 2659 2660 namespace { 2661 // Trait used for the on-disk hash table used in the method pool. 2662 class ASTDeclContextNameLookupTrait { 2663 ASTWriter &Writer; 2664 2665 public: 2666 typedef DeclarationName key_type; 2667 typedef key_type key_type_ref; 2668 2669 typedef DeclContext::lookup_result data_type; 2670 typedef const data_type& data_type_ref; 2671 ASTDeclContextNameLookupTrait(ASTWriter & Writer)2672 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { } 2673 ComputeHash(DeclarationName Name)2674 unsigned ComputeHash(DeclarationName Name) { 2675 llvm::FoldingSetNodeID ID; 2676 ID.AddInteger(Name.getNameKind()); 2677 2678 switch (Name.getNameKind()) { 2679 case DeclarationName::Identifier: 2680 ID.AddString(Name.getAsIdentifierInfo()->getName()); 2681 break; 2682 case DeclarationName::ObjCZeroArgSelector: 2683 case DeclarationName::ObjCOneArgSelector: 2684 case DeclarationName::ObjCMultiArgSelector: 2685 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector())); 2686 break; 2687 case DeclarationName::CXXConstructorName: 2688 case DeclarationName::CXXDestructorName: 2689 case DeclarationName::CXXConversionFunctionName: 2690 break; 2691 case DeclarationName::CXXOperatorName: 2692 ID.AddInteger(Name.getCXXOverloadedOperator()); 2693 break; 2694 case DeclarationName::CXXLiteralOperatorName: 2695 ID.AddString(Name.getCXXLiteralIdentifier()->getName()); 2696 case DeclarationName::CXXUsingDirective: 2697 break; 2698 } 2699 2700 return ID.ComputeHash(); 2701 } 2702 2703 std::pair<unsigned,unsigned> EmitKeyDataLength(raw_ostream & Out,DeclarationName Name,data_type_ref Lookup)2704 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name, 2705 data_type_ref Lookup) { 2706 unsigned KeyLen = 1; 2707 switch (Name.getNameKind()) { 2708 case DeclarationName::Identifier: 2709 case DeclarationName::ObjCZeroArgSelector: 2710 case DeclarationName::ObjCOneArgSelector: 2711 case DeclarationName::ObjCMultiArgSelector: 2712 case DeclarationName::CXXLiteralOperatorName: 2713 KeyLen += 4; 2714 break; 2715 case DeclarationName::CXXOperatorName: 2716 KeyLen += 1; 2717 break; 2718 case DeclarationName::CXXConstructorName: 2719 case DeclarationName::CXXDestructorName: 2720 case DeclarationName::CXXConversionFunctionName: 2721 case DeclarationName::CXXUsingDirective: 2722 break; 2723 } 2724 clang::io::Emit16(Out, KeyLen); 2725 2726 // 2 bytes for num of decls and 4 for each DeclID. 2727 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first); 2728 clang::io::Emit16(Out, DataLen); 2729 2730 return std::make_pair(KeyLen, DataLen); 2731 } 2732 EmitKey(raw_ostream & Out,DeclarationName Name,unsigned)2733 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) { 2734 using namespace clang::io; 2735 2736 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?"); 2737 Emit8(Out, Name.getNameKind()); 2738 switch (Name.getNameKind()) { 2739 case DeclarationName::Identifier: 2740 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo())); 2741 break; 2742 case DeclarationName::ObjCZeroArgSelector: 2743 case DeclarationName::ObjCOneArgSelector: 2744 case DeclarationName::ObjCMultiArgSelector: 2745 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector())); 2746 break; 2747 case DeclarationName::CXXOperatorName: 2748 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?"); 2749 Emit8(Out, Name.getCXXOverloadedOperator()); 2750 break; 2751 case DeclarationName::CXXLiteralOperatorName: 2752 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier())); 2753 break; 2754 case DeclarationName::CXXConstructorName: 2755 case DeclarationName::CXXDestructorName: 2756 case DeclarationName::CXXConversionFunctionName: 2757 case DeclarationName::CXXUsingDirective: 2758 break; 2759 } 2760 } 2761 EmitData(raw_ostream & Out,key_type_ref,data_type Lookup,unsigned DataLen)2762 void EmitData(raw_ostream& Out, key_type_ref, 2763 data_type Lookup, unsigned DataLen) { 2764 uint64_t Start = Out.tell(); (void)Start; 2765 clang::io::Emit16(Out, Lookup.second - Lookup.first); 2766 for (; Lookup.first != Lookup.second; ++Lookup.first) 2767 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first)); 2768 2769 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 2770 } 2771 }; 2772 } // end anonymous namespace 2773 2774 /// \brief Write the block containing all of the declaration IDs 2775 /// visible from the given DeclContext. 2776 /// 2777 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the 2778 /// bitstream, or 0 if no block was written. WriteDeclContextVisibleBlock(ASTContext & Context,DeclContext * DC)2779 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context, 2780 DeclContext *DC) { 2781 if (DC->getPrimaryContext() != DC) 2782 return 0; 2783 2784 // Since there is no name lookup into functions or methods, don't bother to 2785 // build a visible-declarations table for these entities. 2786 if (DC->isFunctionOrMethod()) 2787 return 0; 2788 2789 // If not in C++, we perform name lookup for the translation unit via the 2790 // IdentifierInfo chains, don't bother to build a visible-declarations table. 2791 // FIXME: In C++ we need the visible declarations in order to "see" the 2792 // friend declarations, is there a way to do this without writing the table ? 2793 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus) 2794 return 0; 2795 2796 // Serialize the contents of the mapping used for lookup. Note that, 2797 // although we have two very different code paths, the serialized 2798 // representation is the same for both cases: a declaration name, 2799 // followed by a size, followed by references to the visible 2800 // declarations that have that name. 2801 uint64_t Offset = Stream.GetCurrentBitNo(); 2802 StoredDeclsMap *Map = DC->buildLookup(); 2803 if (!Map || Map->empty()) 2804 return 0; 2805 2806 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; 2807 ASTDeclContextNameLookupTrait Trait(*this); 2808 2809 // Create the on-disk hash table representation. 2810 DeclarationName ConversionName; 2811 llvm::SmallVector<NamedDecl *, 4> ConversionDecls; 2812 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 2813 D != DEnd; ++D) { 2814 DeclarationName Name = D->first; 2815 DeclContext::lookup_result Result = D->second.getLookupResult(); 2816 if (Result.first != Result.second) { 2817 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { 2818 // Hash all conversion function names to the same name. The actual 2819 // type information in conversion function name is not used in the 2820 // key (since such type information is not stable across different 2821 // modules), so the intended effect is to coalesce all of the conversion 2822 // functions under a single key. 2823 if (!ConversionName) 2824 ConversionName = Name; 2825 ConversionDecls.append(Result.first, Result.second); 2826 continue; 2827 } 2828 2829 Generator.insert(Name, Result, Trait); 2830 } 2831 } 2832 2833 // Add the conversion functions 2834 if (!ConversionDecls.empty()) { 2835 Generator.insert(ConversionName, 2836 DeclContext::lookup_result(ConversionDecls.begin(), 2837 ConversionDecls.end()), 2838 Trait); 2839 } 2840 2841 // Create the on-disk hash table in a buffer. 2842 SmallString<4096> LookupTable; 2843 uint32_t BucketOffset; 2844 { 2845 llvm::raw_svector_ostream Out(LookupTable); 2846 // Make sure that no bucket is at offset 0 2847 clang::io::Emit32(Out, 0); 2848 BucketOffset = Generator.Emit(Out, Trait); 2849 } 2850 2851 // Write the lookup table 2852 RecordData Record; 2853 Record.push_back(DECL_CONTEXT_VISIBLE); 2854 Record.push_back(BucketOffset); 2855 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record, 2856 LookupTable.str()); 2857 2858 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record); 2859 ++NumVisibleDeclContexts; 2860 return Offset; 2861 } 2862 2863 /// \brief Write an UPDATE_VISIBLE block for the given context. 2864 /// 2865 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing 2866 /// DeclContext in a dependent AST file. As such, they only exist for the TU 2867 /// (in C++), for namespaces, and for classes with forward-declared unscoped 2868 /// enumeration members (in C++11). WriteDeclContextVisibleUpdate(const DeclContext * DC)2869 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { 2870 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); 2871 if (!Map || Map->empty()) 2872 return; 2873 2874 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; 2875 ASTDeclContextNameLookupTrait Trait(*this); 2876 2877 // Create the hash table. 2878 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 2879 D != DEnd; ++D) { 2880 DeclarationName Name = D->first; 2881 DeclContext::lookup_result Result = D->second.getLookupResult(); 2882 // For any name that appears in this table, the results are complete, i.e. 2883 // they overwrite results from previous PCHs. Merging is always a mess. 2884 if (Result.first != Result.second) 2885 Generator.insert(Name, Result, Trait); 2886 } 2887 2888 // Create the on-disk hash table in a buffer. 2889 SmallString<4096> LookupTable; 2890 uint32_t BucketOffset; 2891 { 2892 llvm::raw_svector_ostream Out(LookupTable); 2893 // Make sure that no bucket is at offset 0 2894 clang::io::Emit32(Out, 0); 2895 BucketOffset = Generator.Emit(Out, Trait); 2896 } 2897 2898 // Write the lookup table 2899 RecordData Record; 2900 Record.push_back(UPDATE_VISIBLE); 2901 Record.push_back(getDeclID(cast<Decl>(DC))); 2902 Record.push_back(BucketOffset); 2903 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str()); 2904 } 2905 2906 /// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions. WriteFPPragmaOptions(const FPOptions & Opts)2907 void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) { 2908 RecordData Record; 2909 Record.push_back(Opts.fp_contract); 2910 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record); 2911 } 2912 2913 /// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions. WriteOpenCLExtensions(Sema & SemaRef)2914 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) { 2915 if (!SemaRef.Context.getLangOpts().OpenCL) 2916 return; 2917 2918 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions(); 2919 RecordData Record; 2920 #define OPENCLEXT(nm) Record.push_back(Opts.nm); 2921 #include "clang/Basic/OpenCLExtensions.def" 2922 Stream.EmitRecord(OPENCL_EXTENSIONS, Record); 2923 } 2924 WriteRedeclarations()2925 void ASTWriter::WriteRedeclarations() { 2926 RecordData LocalRedeclChains; 2927 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap; 2928 2929 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) { 2930 Decl *First = Redeclarations[I]; 2931 assert(First->getPreviousDecl() == 0 && "Not the first declaration?"); 2932 2933 Decl *MostRecent = First->getMostRecentDecl(); 2934 2935 // If we only have a single declaration, there is no point in storing 2936 // a redeclaration chain. 2937 if (First == MostRecent) 2938 continue; 2939 2940 unsigned Offset = LocalRedeclChains.size(); 2941 unsigned Size = 0; 2942 LocalRedeclChains.push_back(0); // Placeholder for the size. 2943 2944 // Collect the set of local redeclarations of this declaration. 2945 for (Decl *Prev = MostRecent; Prev != First; 2946 Prev = Prev->getPreviousDecl()) { 2947 if (!Prev->isFromASTFile()) { 2948 AddDeclRef(Prev, LocalRedeclChains); 2949 ++Size; 2950 } 2951 } 2952 LocalRedeclChains[Offset] = Size; 2953 2954 // Reverse the set of local redeclarations, so that we store them in 2955 // order (since we found them in reverse order). 2956 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end()); 2957 2958 // Add the mapping from the first ID to the set of local declarations. 2959 LocalRedeclarationsInfo Info = { getDeclID(First), Offset }; 2960 LocalRedeclsMap.push_back(Info); 2961 2962 assert(N == Redeclarations.size() && 2963 "Deserialized a declaration we shouldn't have"); 2964 } 2965 2966 if (LocalRedeclChains.empty()) 2967 return; 2968 2969 // Sort the local redeclarations map by the first declaration ID, 2970 // since the reader will be performing binary searches on this information. 2971 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end()); 2972 2973 // Emit the local redeclarations map. 2974 using namespace llvm; 2975 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2976 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP)); 2977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries 2978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2979 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev); 2980 2981 RecordData Record; 2982 Record.push_back(LOCAL_REDECLARATIONS_MAP); 2983 Record.push_back(LocalRedeclsMap.size()); 2984 Stream.EmitRecordWithBlob(AbbrevID, Record, 2985 reinterpret_cast<char*>(LocalRedeclsMap.data()), 2986 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo)); 2987 2988 // Emit the redeclaration chains. 2989 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains); 2990 } 2991 WriteObjCCategories()2992 void ASTWriter::WriteObjCCategories() { 2993 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap; 2994 RecordData Categories; 2995 2996 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) { 2997 unsigned Size = 0; 2998 unsigned StartIndex = Categories.size(); 2999 3000 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I]; 3001 3002 // Allocate space for the size. 3003 Categories.push_back(0); 3004 3005 // Add the categories. 3006 for (ObjCCategoryDecl *Cat = Class->getCategoryList(); 3007 Cat; Cat = Cat->getNextClassCategory(), ++Size) { 3008 assert(getDeclID(Cat) != 0 && "Bogus category"); 3009 AddDeclRef(Cat, Categories); 3010 } 3011 3012 // Update the size. 3013 Categories[StartIndex] = Size; 3014 3015 // Record this interface -> category map. 3016 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex }; 3017 CategoriesMap.push_back(CatInfo); 3018 } 3019 3020 // Sort the categories map by the definition ID, since the reader will be 3021 // performing binary searches on this information. 3022 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end()); 3023 3024 // Emit the categories map. 3025 using namespace llvm; 3026 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 3027 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP)); 3028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries 3029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 3030 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev); 3031 3032 RecordData Record; 3033 Record.push_back(OBJC_CATEGORIES_MAP); 3034 Record.push_back(CategoriesMap.size()); 3035 Stream.EmitRecordWithBlob(AbbrevID, Record, 3036 reinterpret_cast<char*>(CategoriesMap.data()), 3037 CategoriesMap.size() * sizeof(ObjCCategoriesInfo)); 3038 3039 // Emit the category lists. 3040 Stream.EmitRecord(OBJC_CATEGORIES, Categories); 3041 } 3042 WriteMergedDecls()3043 void ASTWriter::WriteMergedDecls() { 3044 if (!Chain || Chain->MergedDecls.empty()) 3045 return; 3046 3047 RecordData Record; 3048 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(), 3049 IEnd = Chain->MergedDecls.end(); 3050 I != IEnd; ++I) { 3051 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID() 3052 : getDeclID(I->first); 3053 assert(CanonID && "Merged declaration not known?"); 3054 3055 Record.push_back(CanonID); 3056 Record.push_back(I->second.size()); 3057 Record.append(I->second.begin(), I->second.end()); 3058 } 3059 Stream.EmitRecord(MERGED_DECLARATIONS, Record); 3060 } 3061 3062 //===----------------------------------------------------------------------===// 3063 // General Serialization Routines 3064 //===----------------------------------------------------------------------===// 3065 3066 /// \brief Write a record containing the given attributes. WriteAttributes(const AttrVec & Attrs,RecordDataImpl & Record)3067 void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) { 3068 Record.push_back(Attrs.size()); 3069 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){ 3070 const Attr * A = *i; 3071 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs 3072 AddSourceRange(A->getRange(), Record); 3073 3074 #include "clang/Serialization/AttrPCHWrite.inc" 3075 3076 } 3077 } 3078 AddString(StringRef Str,RecordDataImpl & Record)3079 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) { 3080 Record.push_back(Str.size()); 3081 Record.insert(Record.end(), Str.begin(), Str.end()); 3082 } 3083 AddVersionTuple(const VersionTuple & Version,RecordDataImpl & Record)3084 void ASTWriter::AddVersionTuple(const VersionTuple &Version, 3085 RecordDataImpl &Record) { 3086 Record.push_back(Version.getMajor()); 3087 if (llvm::Optional<unsigned> Minor = Version.getMinor()) 3088 Record.push_back(*Minor + 1); 3089 else 3090 Record.push_back(0); 3091 if (llvm::Optional<unsigned> Subminor = Version.getSubminor()) 3092 Record.push_back(*Subminor + 1); 3093 else 3094 Record.push_back(0); 3095 } 3096 3097 /// \brief Note that the identifier II occurs at the given offset 3098 /// within the identifier table. SetIdentifierOffset(const IdentifierInfo * II,uint32_t Offset)3099 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { 3100 IdentID ID = IdentifierIDs[II]; 3101 // Only store offsets new to this AST file. Other identifier names are looked 3102 // up earlier in the chain and thus don't need an offset. 3103 if (ID >= FirstIdentID) 3104 IdentifierOffsets[ID - FirstIdentID] = Offset; 3105 } 3106 3107 /// \brief Note that the selector Sel occurs at the given offset 3108 /// within the method pool/selector table. SetSelectorOffset(Selector Sel,uint32_t Offset)3109 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { 3110 unsigned ID = SelectorIDs[Sel]; 3111 assert(ID && "Unknown selector"); 3112 // Don't record offsets for selectors that are also available in a different 3113 // file. 3114 if (ID < FirstSelectorID) 3115 return; 3116 SelectorOffsets[ID - FirstSelectorID] = Offset; 3117 } 3118 ASTWriter(llvm::BitstreamWriter & Stream)3119 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream) 3120 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0), 3121 WritingAST(false), ASTHasCompilerErrors(false), 3122 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID), 3123 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID), 3124 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID), 3125 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS), 3126 NextSubmoduleID(FirstSubmoduleID), 3127 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID), 3128 CollectedStmts(&StmtsToEmit), 3129 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0), 3130 NumVisibleDeclContexts(0), 3131 NextCXXBaseSpecifiersID(1), 3132 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0), 3133 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0), 3134 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0), 3135 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0), 3136 DeclTypedefAbbrev(0), 3137 DeclVarAbbrev(0), DeclFieldAbbrev(0), 3138 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0) 3139 { 3140 } 3141 ~ASTWriter()3142 ASTWriter::~ASTWriter() { 3143 for (FileDeclIDsTy::iterator 3144 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I) 3145 delete I->second; 3146 } 3147 WriteAST(Sema & SemaRef,MemorizeStatCalls * StatCalls,const std::string & OutputFile,Module * WritingModule,StringRef isysroot,bool hasErrors)3148 void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls, 3149 const std::string &OutputFile, 3150 Module *WritingModule, StringRef isysroot, 3151 bool hasErrors) { 3152 WritingAST = true; 3153 3154 ASTHasCompilerErrors = hasErrors; 3155 3156 // Emit the file header. 3157 Stream.Emit((unsigned)'C', 8); 3158 Stream.Emit((unsigned)'P', 8); 3159 Stream.Emit((unsigned)'C', 8); 3160 Stream.Emit((unsigned)'H', 8); 3161 3162 WriteBlockInfoBlock(); 3163 3164 Context = &SemaRef.Context; 3165 PP = &SemaRef.PP; 3166 this->WritingModule = WritingModule; 3167 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule); 3168 Context = 0; 3169 PP = 0; 3170 this->WritingModule = 0; 3171 3172 WritingAST = false; 3173 } 3174 3175 template<typename Vector> AddLazyVectorDecls(ASTWriter & Writer,Vector & Vec,ASTWriter::RecordData & Record)3176 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec, 3177 ASTWriter::RecordData &Record) { 3178 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end(); 3179 I != E; ++I) { 3180 Writer.AddDeclRef(*I, Record); 3181 } 3182 } 3183 WriteASTCore(Sema & SemaRef,MemorizeStatCalls * StatCalls,StringRef isysroot,const std::string & OutputFile,Module * WritingModule)3184 void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, 3185 StringRef isysroot, 3186 const std::string &OutputFile, 3187 Module *WritingModule) { 3188 using namespace llvm; 3189 3190 // Make sure that the AST reader knows to finalize itself. 3191 if (Chain) 3192 Chain->finalizeForWriting(); 3193 3194 ASTContext &Context = SemaRef.Context; 3195 Preprocessor &PP = SemaRef.PP; 3196 3197 // Set up predefined declaration IDs. 3198 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID; 3199 if (Context.ObjCIdDecl) 3200 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID; 3201 if (Context.ObjCSelDecl) 3202 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID; 3203 if (Context.ObjCClassDecl) 3204 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID; 3205 if (Context.ObjCProtocolClassDecl) 3206 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID; 3207 if (Context.Int128Decl) 3208 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID; 3209 if (Context.UInt128Decl) 3210 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID; 3211 if (Context.ObjCInstanceTypeDecl) 3212 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID; 3213 3214 if (!Chain) { 3215 // Make sure that we emit IdentifierInfos (and any attached 3216 // declarations) for builtins. We don't need to do this when we're 3217 // emitting chained PCH files, because all of the builtins will be 3218 // in the original PCH file. 3219 // FIXME: Modules won't like this at all. 3220 IdentifierTable &Table = PP.getIdentifierTable(); 3221 SmallVector<const char *, 32> BuiltinNames; 3222 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames, 3223 Context.getLangOpts().NoBuiltin); 3224 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I) 3225 getIdentifierRef(&Table.get(BuiltinNames[I])); 3226 } 3227 3228 // If there are any out-of-date identifiers, bring them up to date. 3229 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) { 3230 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), 3231 IDEnd = PP.getIdentifierTable().end(); 3232 ID != IDEnd; ++ID) 3233 if (ID->second->isOutOfDate()) 3234 ExtSource->updateOutOfDateIdentifier(*ID->second); 3235 } 3236 3237 // Build a record containing all of the tentative definitions in this file, in 3238 // TentativeDefinitions order. Generally, this record will be empty for 3239 // headers. 3240 RecordData TentativeDefinitions; 3241 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions); 3242 3243 // Build a record containing all of the file scoped decls in this file. 3244 RecordData UnusedFileScopedDecls; 3245 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls, 3246 UnusedFileScopedDecls); 3247 3248 // Build a record containing all of the delegating constructors we still need 3249 // to resolve. 3250 RecordData DelegatingCtorDecls; 3251 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls); 3252 3253 // Write the set of weak, undeclared identifiers. We always write the 3254 // entire table, since later PCH files in a PCH chain are only interested in 3255 // the results at the end of the chain. 3256 RecordData WeakUndeclaredIdentifiers; 3257 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { 3258 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator 3259 I = SemaRef.WeakUndeclaredIdentifiers.begin(), 3260 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { 3261 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); 3262 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); 3263 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); 3264 WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); 3265 } 3266 } 3267 3268 // Build a record containing all of the locally-scoped external 3269 // declarations in this header file. Generally, this record will be 3270 // empty. 3271 RecordData LocallyScopedExternalDecls; 3272 // FIXME: This is filling in the AST file in densemap order which is 3273 // nondeterminstic! 3274 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator 3275 TD = SemaRef.LocallyScopedExternalDecls.begin(), 3276 TDEnd = SemaRef.LocallyScopedExternalDecls.end(); 3277 TD != TDEnd; ++TD) { 3278 if (!TD->second->isFromASTFile()) 3279 AddDeclRef(TD->second, LocallyScopedExternalDecls); 3280 } 3281 3282 // Build a record containing all of the ext_vector declarations. 3283 RecordData ExtVectorDecls; 3284 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls); 3285 3286 // Build a record containing all of the VTable uses information. 3287 RecordData VTableUses; 3288 if (!SemaRef.VTableUses.empty()) { 3289 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { 3290 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); 3291 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); 3292 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); 3293 } 3294 } 3295 3296 // Build a record containing all of dynamic classes declarations. 3297 RecordData DynamicClasses; 3298 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses); 3299 3300 // Build a record containing all of pending implicit instantiations. 3301 RecordData PendingInstantiations; 3302 for (std::deque<Sema::PendingImplicitInstantiation>::iterator 3303 I = SemaRef.PendingInstantiations.begin(), 3304 N = SemaRef.PendingInstantiations.end(); I != N; ++I) { 3305 AddDeclRef(I->first, PendingInstantiations); 3306 AddSourceLocation(I->second, PendingInstantiations); 3307 } 3308 assert(SemaRef.PendingLocalImplicitInstantiations.empty() && 3309 "There are local ones at end of translation unit!"); 3310 3311 // Build a record containing some declaration references. 3312 RecordData SemaDeclRefs; 3313 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { 3314 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); 3315 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); 3316 } 3317 3318 RecordData CUDASpecialDeclRefs; 3319 if (Context.getcudaConfigureCallDecl()) { 3320 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs); 3321 } 3322 3323 // Build a record containing all of the known namespaces. 3324 RecordData KnownNamespaces; 3325 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator 3326 I = SemaRef.KnownNamespaces.begin(), 3327 IEnd = SemaRef.KnownNamespaces.end(); 3328 I != IEnd; ++I) { 3329 if (!I->second) 3330 AddDeclRef(I->first, KnownNamespaces); 3331 } 3332 3333 // Write the remaining AST contents. 3334 RecordData Record; 3335 Stream.EnterSubblock(AST_BLOCK_ID, 5); 3336 WriteMetadata(Context, isysroot, OutputFile); 3337 WriteLanguageOptions(Context.getLangOpts()); 3338 if (StatCalls && isysroot.empty()) 3339 WriteStatCache(*StatCalls); 3340 3341 // Create a lexical update block containing all of the declarations in the 3342 // translation unit that do not come from other AST files. 3343 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); 3344 SmallVector<KindDeclIDPair, 64> NewGlobalDecls; 3345 for (DeclContext::decl_iterator I = TU->noload_decls_begin(), 3346 E = TU->noload_decls_end(); 3347 I != E; ++I) { 3348 if (!(*I)->isFromASTFile()) 3349 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I))); 3350 } 3351 3352 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev(); 3353 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL)); 3354 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 3355 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); 3356 Record.clear(); 3357 Record.push_back(TU_UPDATE_LEXICAL); 3358 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, 3359 data(NewGlobalDecls)); 3360 3361 // And a visible updates block for the translation unit. 3362 Abv = new llvm::BitCodeAbbrev(); 3363 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); 3364 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); 3365 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32)); 3366 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 3367 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv); 3368 WriteDeclContextVisibleUpdate(TU); 3369 3370 // If the translation unit has an anonymous namespace, and we don't already 3371 // have an update block for it, write it as an update block. 3372 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) { 3373 ASTWriter::UpdateRecord &Record = DeclUpdates[TU]; 3374 if (Record.empty()) { 3375 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE); 3376 Record.push_back(reinterpret_cast<uint64_t>(NS)); 3377 } 3378 } 3379 3380 // Resolve any declaration pointers within the declaration updates block. 3381 ResolveDeclUpdatesBlocks(); 3382 3383 // Form the record of special types. 3384 RecordData SpecialTypes; 3385 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes); 3386 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes); 3387 AddTypeRef(Context.getFILEType(), SpecialTypes); 3388 AddTypeRef(Context.getjmp_bufType(), SpecialTypes); 3389 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes); 3390 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes); 3391 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes); 3392 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes); 3393 AddTypeRef(Context.getucontext_tType(), SpecialTypes); 3394 3395 // Keep writing types and declarations until all types and 3396 // declarations have been written. 3397 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE); 3398 WriteDeclsBlockAbbrevs(); 3399 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(), 3400 E = DeclsToRewrite.end(); 3401 I != E; ++I) 3402 DeclTypesToEmit.push(const_cast<Decl*>(*I)); 3403 while (!DeclTypesToEmit.empty()) { 3404 DeclOrType DOT = DeclTypesToEmit.front(); 3405 DeclTypesToEmit.pop(); 3406 if (DOT.isType()) 3407 WriteType(DOT.getType()); 3408 else 3409 WriteDecl(Context, DOT.getDecl()); 3410 } 3411 Stream.ExitBlock(); 3412 3413 WriteFileDeclIDsMap(); 3414 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); 3415 3416 if (Chain) { 3417 // Write the mapping information describing our module dependencies and how 3418 // each of those modules were mapped into our own offset/ID space, so that 3419 // the reader can build the appropriate mapping to its own offset/ID space. 3420 // The map consists solely of a blob with the following format: 3421 // *(module-name-len:i16 module-name:len*i8 3422 // source-location-offset:i32 3423 // identifier-id:i32 3424 // preprocessed-entity-id:i32 3425 // macro-definition-id:i32 3426 // submodule-id:i32 3427 // selector-id:i32 3428 // declaration-id:i32 3429 // c++-base-specifiers-id:i32 3430 // type-id:i32) 3431 // 3432 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 3433 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP)); 3434 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 3435 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev); 3436 SmallString<2048> Buffer; 3437 { 3438 llvm::raw_svector_ostream Out(Buffer); 3439 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(), 3440 MEnd = Chain->ModuleMgr.end(); 3441 M != MEnd; ++M) { 3442 StringRef FileName = (*M)->FileName; 3443 io::Emit16(Out, FileName.size()); 3444 Out.write(FileName.data(), FileName.size()); 3445 io::Emit32(Out, (*M)->SLocEntryBaseOffset); 3446 io::Emit32(Out, (*M)->BaseIdentifierID); 3447 io::Emit32(Out, (*M)->BasePreprocessedEntityID); 3448 io::Emit32(Out, (*M)->BaseSubmoduleID); 3449 io::Emit32(Out, (*M)->BaseSelectorID); 3450 io::Emit32(Out, (*M)->BaseDeclID); 3451 io::Emit32(Out, (*M)->BaseTypeIndex); 3452 } 3453 } 3454 Record.clear(); 3455 Record.push_back(MODULE_OFFSET_MAP); 3456 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record, 3457 Buffer.data(), Buffer.size()); 3458 } 3459 WritePreprocessor(PP, WritingModule != 0); 3460 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot); 3461 WriteSelectors(SemaRef); 3462 WriteReferencedSelectorsPool(SemaRef); 3463 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0); 3464 WriteFPPragmaOptions(SemaRef.getFPOptions()); 3465 WriteOpenCLExtensions(SemaRef); 3466 3467 WriteTypeDeclOffsets(); 3468 WritePragmaDiagnosticMappings(Context.getDiagnostics()); 3469 3470 WriteCXXBaseSpecifiersOffsets(); 3471 3472 // If we're emitting a module, write out the submodule information. 3473 if (WritingModule) 3474 WriteSubmodules(WritingModule); 3475 3476 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes); 3477 3478 // Write the record containing external, unnamed definitions. 3479 if (!ExternalDefinitions.empty()) 3480 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); 3481 3482 // Write the record containing tentative definitions. 3483 if (!TentativeDefinitions.empty()) 3484 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); 3485 3486 // Write the record containing unused file scoped decls. 3487 if (!UnusedFileScopedDecls.empty()) 3488 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); 3489 3490 // Write the record containing weak undeclared identifiers. 3491 if (!WeakUndeclaredIdentifiers.empty()) 3492 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, 3493 WeakUndeclaredIdentifiers); 3494 3495 // Write the record containing locally-scoped external definitions. 3496 if (!LocallyScopedExternalDecls.empty()) 3497 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, 3498 LocallyScopedExternalDecls); 3499 3500 // Write the record containing ext_vector type names. 3501 if (!ExtVectorDecls.empty()) 3502 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); 3503 3504 // Write the record containing VTable uses information. 3505 if (!VTableUses.empty()) 3506 Stream.EmitRecord(VTABLE_USES, VTableUses); 3507 3508 // Write the record containing dynamic classes declarations. 3509 if (!DynamicClasses.empty()) 3510 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); 3511 3512 // Write the record containing pending implicit instantiations. 3513 if (!PendingInstantiations.empty()) 3514 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); 3515 3516 // Write the record containing declaration references of Sema. 3517 if (!SemaDeclRefs.empty()) 3518 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); 3519 3520 // Write the record containing CUDA-specific declaration references. 3521 if (!CUDASpecialDeclRefs.empty()) 3522 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs); 3523 3524 // Write the delegating constructors. 3525 if (!DelegatingCtorDecls.empty()) 3526 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls); 3527 3528 // Write the known namespaces. 3529 if (!KnownNamespaces.empty()) 3530 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces); 3531 3532 // Write the visible updates to DeclContexts. 3533 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator 3534 I = UpdatedDeclContexts.begin(), 3535 E = UpdatedDeclContexts.end(); 3536 I != E; ++I) 3537 WriteDeclContextVisibleUpdate(*I); 3538 3539 if (!WritingModule) { 3540 // Write the submodules that were imported, if any. 3541 RecordData ImportedModules; 3542 for (ASTContext::import_iterator I = Context.local_import_begin(), 3543 IEnd = Context.local_import_end(); 3544 I != IEnd; ++I) { 3545 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end()); 3546 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]); 3547 } 3548 if (!ImportedModules.empty()) { 3549 // Sort module IDs. 3550 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end()); 3551 3552 // Unique module IDs. 3553 ImportedModules.erase(std::unique(ImportedModules.begin(), 3554 ImportedModules.end()), 3555 ImportedModules.end()); 3556 3557 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules); 3558 } 3559 } 3560 3561 WriteDeclUpdatesBlocks(); 3562 WriteDeclReplacementsBlock(); 3563 WriteMergedDecls(); 3564 WriteRedeclarations(); 3565 WriteObjCCategories(); 3566 3567 // Some simple statistics 3568 Record.clear(); 3569 Record.push_back(NumStatements); 3570 Record.push_back(NumMacros); 3571 Record.push_back(NumLexicalDeclContexts); 3572 Record.push_back(NumVisibleDeclContexts); 3573 Stream.EmitRecord(STATISTICS, Record); 3574 Stream.ExitBlock(); 3575 } 3576 3577 /// \brief Go through the declaration update blocks and resolve declaration 3578 /// pointers into declaration IDs. ResolveDeclUpdatesBlocks()3579 void ASTWriter::ResolveDeclUpdatesBlocks() { 3580 for (DeclUpdateMap::iterator 3581 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) { 3582 const Decl *D = I->first; 3583 UpdateRecord &URec = I->second; 3584 3585 if (isRewritten(D)) 3586 continue; // The decl will be written completely 3587 3588 unsigned Idx = 0, N = URec.size(); 3589 while (Idx < N) { 3590 switch ((DeclUpdateKind)URec[Idx++]) { 3591 case UPD_CXX_ADDED_IMPLICIT_MEMBER: 3592 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: 3593 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: 3594 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx])); 3595 ++Idx; 3596 break; 3597 3598 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: 3599 ++Idx; 3600 break; 3601 } 3602 } 3603 } 3604 } 3605 WriteDeclUpdatesBlocks()3606 void ASTWriter::WriteDeclUpdatesBlocks() { 3607 if (DeclUpdates.empty()) 3608 return; 3609 3610 RecordData OffsetsRecord; 3611 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE); 3612 for (DeclUpdateMap::iterator 3613 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) { 3614 const Decl *D = I->first; 3615 UpdateRecord &URec = I->second; 3616 3617 if (isRewritten(D)) 3618 continue; // The decl will be written completely,no need to store updates. 3619 3620 uint64_t Offset = Stream.GetCurrentBitNo(); 3621 Stream.EmitRecord(DECL_UPDATES, URec); 3622 3623 OffsetsRecord.push_back(GetDeclRef(D)); 3624 OffsetsRecord.push_back(Offset); 3625 } 3626 Stream.ExitBlock(); 3627 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord); 3628 } 3629 WriteDeclReplacementsBlock()3630 void ASTWriter::WriteDeclReplacementsBlock() { 3631 if (ReplacedDecls.empty()) 3632 return; 3633 3634 RecordData Record; 3635 for (SmallVector<ReplacedDeclInfo, 16>::iterator 3636 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) { 3637 Record.push_back(I->ID); 3638 Record.push_back(I->Offset); 3639 Record.push_back(I->Loc); 3640 } 3641 Stream.EmitRecord(DECL_REPLACEMENTS, Record); 3642 } 3643 AddSourceLocation(SourceLocation Loc,RecordDataImpl & Record)3644 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { 3645 Record.push_back(Loc.getRawEncoding()); 3646 } 3647 AddSourceRange(SourceRange Range,RecordDataImpl & Record)3648 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { 3649 AddSourceLocation(Range.getBegin(), Record); 3650 AddSourceLocation(Range.getEnd(), Record); 3651 } 3652 AddAPInt(const llvm::APInt & Value,RecordDataImpl & Record)3653 void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) { 3654 Record.push_back(Value.getBitWidth()); 3655 const uint64_t *Words = Value.getRawData(); 3656 Record.append(Words, Words + Value.getNumWords()); 3657 } 3658 AddAPSInt(const llvm::APSInt & Value,RecordDataImpl & Record)3659 void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) { 3660 Record.push_back(Value.isUnsigned()); 3661 AddAPInt(Value, Record); 3662 } 3663 AddAPFloat(const llvm::APFloat & Value,RecordDataImpl & Record)3664 void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) { 3665 AddAPInt(Value.bitcastToAPInt(), Record); 3666 } 3667 AddIdentifierRef(const IdentifierInfo * II,RecordDataImpl & Record)3668 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) { 3669 Record.push_back(getIdentifierRef(II)); 3670 } 3671 getIdentifierRef(const IdentifierInfo * II)3672 IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { 3673 if (II == 0) 3674 return 0; 3675 3676 IdentID &ID = IdentifierIDs[II]; 3677 if (ID == 0) 3678 ID = NextIdentID++; 3679 return ID; 3680 } 3681 AddSelectorRef(const Selector SelRef,RecordDataImpl & Record)3682 void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) { 3683 Record.push_back(getSelectorRef(SelRef)); 3684 } 3685 getSelectorRef(Selector Sel)3686 SelectorID ASTWriter::getSelectorRef(Selector Sel) { 3687 if (Sel.getAsOpaquePtr() == 0) { 3688 return 0; 3689 } 3690 3691 SelectorID &SID = SelectorIDs[Sel]; 3692 if (SID == 0 && Chain) { 3693 // This might trigger a ReadSelector callback, which will set the ID for 3694 // this selector. 3695 Chain->LoadSelector(Sel); 3696 } 3697 if (SID == 0) { 3698 SID = NextSelectorID++; 3699 } 3700 return SID; 3701 } 3702 AddCXXTemporary(const CXXTemporary * Temp,RecordDataImpl & Record)3703 void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) { 3704 AddDeclRef(Temp->getDestructor(), Record); 3705 } 3706 AddCXXBaseSpecifiersRef(CXXBaseSpecifier const * Bases,CXXBaseSpecifier const * BasesEnd,RecordDataImpl & Record)3707 void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases, 3708 CXXBaseSpecifier const *BasesEnd, 3709 RecordDataImpl &Record) { 3710 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded"); 3711 CXXBaseSpecifiersToWrite.push_back( 3712 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID, 3713 Bases, BasesEnd)); 3714 Record.push_back(NextCXXBaseSpecifiersID++); 3715 } 3716 AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,const TemplateArgumentLocInfo & Arg,RecordDataImpl & Record)3717 void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, 3718 const TemplateArgumentLocInfo &Arg, 3719 RecordDataImpl &Record) { 3720 switch (Kind) { 3721 case TemplateArgument::Expression: 3722 AddStmt(Arg.getAsExpr()); 3723 break; 3724 case TemplateArgument::Type: 3725 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); 3726 break; 3727 case TemplateArgument::Template: 3728 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); 3729 AddSourceLocation(Arg.getTemplateNameLoc(), Record); 3730 break; 3731 case TemplateArgument::TemplateExpansion: 3732 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); 3733 AddSourceLocation(Arg.getTemplateNameLoc(), Record); 3734 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record); 3735 break; 3736 case TemplateArgument::Null: 3737 case TemplateArgument::Integral: 3738 case TemplateArgument::Declaration: 3739 case TemplateArgument::Pack: 3740 break; 3741 } 3742 } 3743 AddTemplateArgumentLoc(const TemplateArgumentLoc & Arg,RecordDataImpl & Record)3744 void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, 3745 RecordDataImpl &Record) { 3746 AddTemplateArgument(Arg.getArgument(), Record); 3747 3748 if (Arg.getArgument().getKind() == TemplateArgument::Expression) { 3749 bool InfoHasSameExpr 3750 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); 3751 Record.push_back(InfoHasSameExpr); 3752 if (InfoHasSameExpr) 3753 return; // Avoid storing the same expr twice. 3754 } 3755 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), 3756 Record); 3757 } 3758 AddTypeSourceInfo(TypeSourceInfo * TInfo,RecordDataImpl & Record)3759 void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, 3760 RecordDataImpl &Record) { 3761 if (TInfo == 0) { 3762 AddTypeRef(QualType(), Record); 3763 return; 3764 } 3765 3766 AddTypeLoc(TInfo->getTypeLoc(), Record); 3767 } 3768 AddTypeLoc(TypeLoc TL,RecordDataImpl & Record)3769 void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) { 3770 AddTypeRef(TL.getType(), Record); 3771 3772 TypeLocWriter TLW(*this, Record); 3773 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 3774 TLW.Visit(TL); 3775 } 3776 AddTypeRef(QualType T,RecordDataImpl & Record)3777 void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) { 3778 Record.push_back(GetOrCreateTypeID(T)); 3779 } 3780 GetOrCreateTypeID(QualType T)3781 TypeID ASTWriter::GetOrCreateTypeID( QualType T) { 3782 return MakeTypeID(*Context, T, 3783 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this)); 3784 } 3785 getTypeID(QualType T) const3786 TypeID ASTWriter::getTypeID(QualType T) const { 3787 return MakeTypeID(*Context, T, 3788 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this)); 3789 } 3790 GetOrCreateTypeIdx(QualType T)3791 TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) { 3792 if (T.isNull()) 3793 return TypeIdx(); 3794 assert(!T.getLocalFastQualifiers()); 3795 3796 TypeIdx &Idx = TypeIdxs[T]; 3797 if (Idx.getIndex() == 0) { 3798 // We haven't seen this type before. Assign it a new ID and put it 3799 // into the queue of types to emit. 3800 Idx = TypeIdx(NextTypeID++); 3801 DeclTypesToEmit.push(T); 3802 } 3803 return Idx; 3804 } 3805 getTypeIdx(QualType T) const3806 TypeIdx ASTWriter::getTypeIdx(QualType T) const { 3807 if (T.isNull()) 3808 return TypeIdx(); 3809 assert(!T.getLocalFastQualifiers()); 3810 3811 TypeIdxMap::const_iterator I = TypeIdxs.find(T); 3812 assert(I != TypeIdxs.end() && "Type not emitted!"); 3813 return I->second; 3814 } 3815 AddDeclRef(const Decl * D,RecordDataImpl & Record)3816 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) { 3817 Record.push_back(GetDeclRef(D)); 3818 } 3819 GetDeclRef(const Decl * D)3820 DeclID ASTWriter::GetDeclRef(const Decl *D) { 3821 assert(WritingAST && "Cannot request a declaration ID before AST writing"); 3822 3823 if (D == 0) { 3824 return 0; 3825 } 3826 3827 // If D comes from an AST file, its declaration ID is already known and 3828 // fixed. 3829 if (D->isFromASTFile()) 3830 return D->getGlobalID(); 3831 3832 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer"); 3833 DeclID &ID = DeclIDs[D]; 3834 if (ID == 0) { 3835 // We haven't seen this declaration before. Give it a new ID and 3836 // enqueue it in the list of declarations to emit. 3837 ID = NextDeclID++; 3838 DeclTypesToEmit.push(const_cast<Decl *>(D)); 3839 } 3840 3841 return ID; 3842 } 3843 getDeclID(const Decl * D)3844 DeclID ASTWriter::getDeclID(const Decl *D) { 3845 if (D == 0) 3846 return 0; 3847 3848 // If D comes from an AST file, its declaration ID is already known and 3849 // fixed. 3850 if (D->isFromASTFile()) 3851 return D->getGlobalID(); 3852 3853 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); 3854 return DeclIDs[D]; 3855 } 3856 compLocDecl(std::pair<unsigned,serialization::DeclID> L,std::pair<unsigned,serialization::DeclID> R)3857 static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L, 3858 std::pair<unsigned, serialization::DeclID> R) { 3859 return L.first < R.first; 3860 } 3861 associateDeclWithFile(const Decl * D,DeclID ID)3862 void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) { 3863 assert(ID); 3864 assert(D); 3865 3866 SourceLocation Loc = D->getLocation(); 3867 if (Loc.isInvalid()) 3868 return; 3869 3870 // We only keep track of the file-level declarations of each file. 3871 if (!D->getLexicalDeclContext()->isFileContext()) 3872 return; 3873 // FIXME: ParmVarDecls that are part of a function type of a parameter of 3874 // a function/objc method, should not have TU as lexical context. 3875 if (isa<ParmVarDecl>(D)) 3876 return; 3877 3878 SourceManager &SM = Context->getSourceManager(); 3879 SourceLocation FileLoc = SM.getFileLoc(Loc); 3880 assert(SM.isLocalSourceLocation(FileLoc)); 3881 FileID FID; 3882 unsigned Offset; 3883 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc); 3884 if (FID.isInvalid()) 3885 return; 3886 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID); 3887 assert(Entry->isFile()); 3888 3889 DeclIDInFileInfo *&Info = FileDeclIDs[Entry]; 3890 if (!Info) 3891 Info = new DeclIDInFileInfo(); 3892 3893 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID); 3894 LocDeclIDsTy &Decls = Info->DeclIDs; 3895 3896 if (Decls.empty() || Decls.back().first <= Offset) { 3897 Decls.push_back(LocDecl); 3898 return; 3899 } 3900 3901 LocDeclIDsTy::iterator 3902 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl); 3903 3904 Decls.insert(I, LocDecl); 3905 } 3906 AddDeclarationName(DeclarationName Name,RecordDataImpl & Record)3907 void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) { 3908 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. 3909 Record.push_back(Name.getNameKind()); 3910 switch (Name.getNameKind()) { 3911 case DeclarationName::Identifier: 3912 AddIdentifierRef(Name.getAsIdentifierInfo(), Record); 3913 break; 3914 3915 case DeclarationName::ObjCZeroArgSelector: 3916 case DeclarationName::ObjCOneArgSelector: 3917 case DeclarationName::ObjCMultiArgSelector: 3918 AddSelectorRef(Name.getObjCSelector(), Record); 3919 break; 3920 3921 case DeclarationName::CXXConstructorName: 3922 case DeclarationName::CXXDestructorName: 3923 case DeclarationName::CXXConversionFunctionName: 3924 AddTypeRef(Name.getCXXNameType(), Record); 3925 break; 3926 3927 case DeclarationName::CXXOperatorName: 3928 Record.push_back(Name.getCXXOverloadedOperator()); 3929 break; 3930 3931 case DeclarationName::CXXLiteralOperatorName: 3932 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record); 3933 break; 3934 3935 case DeclarationName::CXXUsingDirective: 3936 // No extra data to emit 3937 break; 3938 } 3939 } 3940 AddDeclarationNameLoc(const DeclarationNameLoc & DNLoc,DeclarationName Name,RecordDataImpl & Record)3941 void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, 3942 DeclarationName Name, RecordDataImpl &Record) { 3943 switch (Name.getNameKind()) { 3944 case DeclarationName::CXXConstructorName: 3945 case DeclarationName::CXXDestructorName: 3946 case DeclarationName::CXXConversionFunctionName: 3947 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record); 3948 break; 3949 3950 case DeclarationName::CXXOperatorName: 3951 AddSourceLocation( 3952 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc), 3953 Record); 3954 AddSourceLocation( 3955 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc), 3956 Record); 3957 break; 3958 3959 case DeclarationName::CXXLiteralOperatorName: 3960 AddSourceLocation( 3961 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc), 3962 Record); 3963 break; 3964 3965 case DeclarationName::Identifier: 3966 case DeclarationName::ObjCZeroArgSelector: 3967 case DeclarationName::ObjCOneArgSelector: 3968 case DeclarationName::ObjCMultiArgSelector: 3969 case DeclarationName::CXXUsingDirective: 3970 break; 3971 } 3972 } 3973 AddDeclarationNameInfo(const DeclarationNameInfo & NameInfo,RecordDataImpl & Record)3974 void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, 3975 RecordDataImpl &Record) { 3976 AddDeclarationName(NameInfo.getName(), Record); 3977 AddSourceLocation(NameInfo.getLoc(), Record); 3978 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record); 3979 } 3980 AddQualifierInfo(const QualifierInfo & Info,RecordDataImpl & Record)3981 void ASTWriter::AddQualifierInfo(const QualifierInfo &Info, 3982 RecordDataImpl &Record) { 3983 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record); 3984 Record.push_back(Info.NumTemplParamLists); 3985 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i) 3986 AddTemplateParameterList(Info.TemplParamLists[i], Record); 3987 } 3988 AddNestedNameSpecifier(NestedNameSpecifier * NNS,RecordDataImpl & Record)3989 void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, 3990 RecordDataImpl &Record) { 3991 // Nested name specifiers usually aren't too long. I think that 8 would 3992 // typically accommodate the vast majority. 3993 SmallVector<NestedNameSpecifier *, 8> NestedNames; 3994 3995 // Push each of the NNS's onto a stack for serialization in reverse order. 3996 while (NNS) { 3997 NestedNames.push_back(NNS); 3998 NNS = NNS->getPrefix(); 3999 } 4000 4001 Record.push_back(NestedNames.size()); 4002 while(!NestedNames.empty()) { 4003 NNS = NestedNames.pop_back_val(); 4004 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); 4005 Record.push_back(Kind); 4006 switch (Kind) { 4007 case NestedNameSpecifier::Identifier: 4008 AddIdentifierRef(NNS->getAsIdentifier(), Record); 4009 break; 4010 4011 case NestedNameSpecifier::Namespace: 4012 AddDeclRef(NNS->getAsNamespace(), Record); 4013 break; 4014 4015 case NestedNameSpecifier::NamespaceAlias: 4016 AddDeclRef(NNS->getAsNamespaceAlias(), Record); 4017 break; 4018 4019 case NestedNameSpecifier::TypeSpec: 4020 case NestedNameSpecifier::TypeSpecWithTemplate: 4021 AddTypeRef(QualType(NNS->getAsType(), 0), Record); 4022 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); 4023 break; 4024 4025 case NestedNameSpecifier::Global: 4026 // Don't need to write an associated value. 4027 break; 4028 } 4029 } 4030 } 4031 AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,RecordDataImpl & Record)4032 void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, 4033 RecordDataImpl &Record) { 4034 // Nested name specifiers usually aren't too long. I think that 8 would 4035 // typically accommodate the vast majority. 4036 SmallVector<NestedNameSpecifierLoc , 8> NestedNames; 4037 4038 // Push each of the nested-name-specifiers's onto a stack for 4039 // serialization in reverse order. 4040 while (NNS) { 4041 NestedNames.push_back(NNS); 4042 NNS = NNS.getPrefix(); 4043 } 4044 4045 Record.push_back(NestedNames.size()); 4046 while(!NestedNames.empty()) { 4047 NNS = NestedNames.pop_back_val(); 4048 NestedNameSpecifier::SpecifierKind Kind 4049 = NNS.getNestedNameSpecifier()->getKind(); 4050 Record.push_back(Kind); 4051 switch (Kind) { 4052 case NestedNameSpecifier::Identifier: 4053 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record); 4054 AddSourceRange(NNS.getLocalSourceRange(), Record); 4055 break; 4056 4057 case NestedNameSpecifier::Namespace: 4058 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record); 4059 AddSourceRange(NNS.getLocalSourceRange(), Record); 4060 break; 4061 4062 case NestedNameSpecifier::NamespaceAlias: 4063 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record); 4064 AddSourceRange(NNS.getLocalSourceRange(), Record); 4065 break; 4066 4067 case NestedNameSpecifier::TypeSpec: 4068 case NestedNameSpecifier::TypeSpecWithTemplate: 4069 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); 4070 AddTypeLoc(NNS.getTypeLoc(), Record); 4071 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); 4072 break; 4073 4074 case NestedNameSpecifier::Global: 4075 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); 4076 break; 4077 } 4078 } 4079 } 4080 AddTemplateName(TemplateName Name,RecordDataImpl & Record)4081 void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) { 4082 TemplateName::NameKind Kind = Name.getKind(); 4083 Record.push_back(Kind); 4084 switch (Kind) { 4085 case TemplateName::Template: 4086 AddDeclRef(Name.getAsTemplateDecl(), Record); 4087 break; 4088 4089 case TemplateName::OverloadedTemplate: { 4090 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); 4091 Record.push_back(OvT->size()); 4092 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end(); 4093 I != E; ++I) 4094 AddDeclRef(*I, Record); 4095 break; 4096 } 4097 4098 case TemplateName::QualifiedTemplate: { 4099 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); 4100 AddNestedNameSpecifier(QualT->getQualifier(), Record); 4101 Record.push_back(QualT->hasTemplateKeyword()); 4102 AddDeclRef(QualT->getTemplateDecl(), Record); 4103 break; 4104 } 4105 4106 case TemplateName::DependentTemplate: { 4107 DependentTemplateName *DepT = Name.getAsDependentTemplateName(); 4108 AddNestedNameSpecifier(DepT->getQualifier(), Record); 4109 Record.push_back(DepT->isIdentifier()); 4110 if (DepT->isIdentifier()) 4111 AddIdentifierRef(DepT->getIdentifier(), Record); 4112 else 4113 Record.push_back(DepT->getOperator()); 4114 break; 4115 } 4116 4117 case TemplateName::SubstTemplateTemplateParm: { 4118 SubstTemplateTemplateParmStorage *subst 4119 = Name.getAsSubstTemplateTemplateParm(); 4120 AddDeclRef(subst->getParameter(), Record); 4121 AddTemplateName(subst->getReplacement(), Record); 4122 break; 4123 } 4124 4125 case TemplateName::SubstTemplateTemplateParmPack: { 4126 SubstTemplateTemplateParmPackStorage *SubstPack 4127 = Name.getAsSubstTemplateTemplateParmPack(); 4128 AddDeclRef(SubstPack->getParameterPack(), Record); 4129 AddTemplateArgument(SubstPack->getArgumentPack(), Record); 4130 break; 4131 } 4132 } 4133 } 4134 AddTemplateArgument(const TemplateArgument & Arg,RecordDataImpl & Record)4135 void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg, 4136 RecordDataImpl &Record) { 4137 Record.push_back(Arg.getKind()); 4138 switch (Arg.getKind()) { 4139 case TemplateArgument::Null: 4140 break; 4141 case TemplateArgument::Type: 4142 AddTypeRef(Arg.getAsType(), Record); 4143 break; 4144 case TemplateArgument::Declaration: 4145 AddDeclRef(Arg.getAsDecl(), Record); 4146 break; 4147 case TemplateArgument::Integral: 4148 AddAPSInt(*Arg.getAsIntegral(), Record); 4149 AddTypeRef(Arg.getIntegralType(), Record); 4150 break; 4151 case TemplateArgument::Template: 4152 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); 4153 break; 4154 case TemplateArgument::TemplateExpansion: 4155 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); 4156 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions()) 4157 Record.push_back(*NumExpansions + 1); 4158 else 4159 Record.push_back(0); 4160 break; 4161 case TemplateArgument::Expression: 4162 AddStmt(Arg.getAsExpr()); 4163 break; 4164 case TemplateArgument::Pack: 4165 Record.push_back(Arg.pack_size()); 4166 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end(); 4167 I != E; ++I) 4168 AddTemplateArgument(*I, Record); 4169 break; 4170 } 4171 } 4172 4173 void AddTemplateParameterList(const TemplateParameterList * TemplateParams,RecordDataImpl & Record)4174 ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, 4175 RecordDataImpl &Record) { 4176 assert(TemplateParams && "No TemplateParams!"); 4177 AddSourceLocation(TemplateParams->getTemplateLoc(), Record); 4178 AddSourceLocation(TemplateParams->getLAngleLoc(), Record); 4179 AddSourceLocation(TemplateParams->getRAngleLoc(), Record); 4180 Record.push_back(TemplateParams->size()); 4181 for (TemplateParameterList::const_iterator 4182 P = TemplateParams->begin(), PEnd = TemplateParams->end(); 4183 P != PEnd; ++P) 4184 AddDeclRef(*P, Record); 4185 } 4186 4187 /// \brief Emit a template argument list. 4188 void AddTemplateArgumentList(const TemplateArgumentList * TemplateArgs,RecordDataImpl & Record)4189 ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, 4190 RecordDataImpl &Record) { 4191 assert(TemplateArgs && "No TemplateArgs!"); 4192 Record.push_back(TemplateArgs->size()); 4193 for (int i=0, e = TemplateArgs->size(); i != e; ++i) 4194 AddTemplateArgument(TemplateArgs->get(i), Record); 4195 } 4196 4197 4198 void AddUnresolvedSet(const UnresolvedSetImpl & Set,RecordDataImpl & Record)4199 ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) { 4200 Record.push_back(Set.size()); 4201 for (UnresolvedSetImpl::const_iterator 4202 I = Set.begin(), E = Set.end(); I != E; ++I) { 4203 AddDeclRef(I.getDecl(), Record); 4204 Record.push_back(I.getAccess()); 4205 } 4206 } 4207 AddCXXBaseSpecifier(const CXXBaseSpecifier & Base,RecordDataImpl & Record)4208 void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, 4209 RecordDataImpl &Record) { 4210 Record.push_back(Base.isVirtual()); 4211 Record.push_back(Base.isBaseOfClass()); 4212 Record.push_back(Base.getAccessSpecifierAsWritten()); 4213 Record.push_back(Base.getInheritConstructors()); 4214 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record); 4215 AddSourceRange(Base.getSourceRange(), Record); 4216 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc() 4217 : SourceLocation(), 4218 Record); 4219 } 4220 FlushCXXBaseSpecifiers()4221 void ASTWriter::FlushCXXBaseSpecifiers() { 4222 RecordData Record; 4223 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) { 4224 Record.clear(); 4225 4226 // Record the offset of this base-specifier set. 4227 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1; 4228 if (Index == CXXBaseSpecifiersOffsets.size()) 4229 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo()); 4230 else { 4231 if (Index > CXXBaseSpecifiersOffsets.size()) 4232 CXXBaseSpecifiersOffsets.resize(Index + 1); 4233 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo(); 4234 } 4235 4236 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases, 4237 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd; 4238 Record.push_back(BEnd - B); 4239 for (; B != BEnd; ++B) 4240 AddCXXBaseSpecifier(*B, Record); 4241 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record); 4242 4243 // Flush any expressions that were written as part of the base specifiers. 4244 FlushStmts(); 4245 } 4246 4247 CXXBaseSpecifiersToWrite.clear(); 4248 } 4249 AddCXXCtorInitializers(const CXXCtorInitializer * const * CtorInitializers,unsigned NumCtorInitializers,RecordDataImpl & Record)4250 void ASTWriter::AddCXXCtorInitializers( 4251 const CXXCtorInitializer * const *CtorInitializers, 4252 unsigned NumCtorInitializers, 4253 RecordDataImpl &Record) { 4254 Record.push_back(NumCtorInitializers); 4255 for (unsigned i=0; i != NumCtorInitializers; ++i) { 4256 const CXXCtorInitializer *Init = CtorInitializers[i]; 4257 4258 if (Init->isBaseInitializer()) { 4259 Record.push_back(CTOR_INITIALIZER_BASE); 4260 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record); 4261 Record.push_back(Init->isBaseVirtual()); 4262 } else if (Init->isDelegatingInitializer()) { 4263 Record.push_back(CTOR_INITIALIZER_DELEGATING); 4264 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record); 4265 } else if (Init->isMemberInitializer()){ 4266 Record.push_back(CTOR_INITIALIZER_MEMBER); 4267 AddDeclRef(Init->getMember(), Record); 4268 } else { 4269 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER); 4270 AddDeclRef(Init->getIndirectMember(), Record); 4271 } 4272 4273 AddSourceLocation(Init->getMemberLocation(), Record); 4274 AddStmt(Init->getInit()); 4275 AddSourceLocation(Init->getLParenLoc(), Record); 4276 AddSourceLocation(Init->getRParenLoc(), Record); 4277 Record.push_back(Init->isWritten()); 4278 if (Init->isWritten()) { 4279 Record.push_back(Init->getSourceOrder()); 4280 } else { 4281 Record.push_back(Init->getNumArrayIndices()); 4282 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) 4283 AddDeclRef(Init->getArrayIndex(i), Record); 4284 } 4285 } 4286 } 4287 AddCXXDefinitionData(const CXXRecordDecl * D,RecordDataImpl & Record)4288 void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) { 4289 assert(D->DefinitionData); 4290 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData; 4291 Record.push_back(Data.IsLambda); 4292 Record.push_back(Data.UserDeclaredConstructor); 4293 Record.push_back(Data.UserDeclaredCopyConstructor); 4294 Record.push_back(Data.UserDeclaredMoveConstructor); 4295 Record.push_back(Data.UserDeclaredCopyAssignment); 4296 Record.push_back(Data.UserDeclaredMoveAssignment); 4297 Record.push_back(Data.UserDeclaredDestructor); 4298 Record.push_back(Data.Aggregate); 4299 Record.push_back(Data.PlainOldData); 4300 Record.push_back(Data.Empty); 4301 Record.push_back(Data.Polymorphic); 4302 Record.push_back(Data.Abstract); 4303 Record.push_back(Data.IsStandardLayout); 4304 Record.push_back(Data.HasNoNonEmptyBases); 4305 Record.push_back(Data.HasPrivateFields); 4306 Record.push_back(Data.HasProtectedFields); 4307 Record.push_back(Data.HasPublicFields); 4308 Record.push_back(Data.HasMutableFields); 4309 Record.push_back(Data.HasOnlyCMembers); 4310 Record.push_back(Data.HasTrivialDefaultConstructor); 4311 Record.push_back(Data.HasConstexprNonCopyMoveConstructor); 4312 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr); 4313 Record.push_back(Data.DefaultedCopyConstructorIsConstexpr); 4314 Record.push_back(Data.DefaultedMoveConstructorIsConstexpr); 4315 Record.push_back(Data.HasConstexprDefaultConstructor); 4316 Record.push_back(Data.HasConstexprCopyConstructor); 4317 Record.push_back(Data.HasConstexprMoveConstructor); 4318 Record.push_back(Data.HasTrivialCopyConstructor); 4319 Record.push_back(Data.HasTrivialMoveConstructor); 4320 Record.push_back(Data.HasTrivialCopyAssignment); 4321 Record.push_back(Data.HasTrivialMoveAssignment); 4322 Record.push_back(Data.HasTrivialDestructor); 4323 Record.push_back(Data.HasIrrelevantDestructor); 4324 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases); 4325 Record.push_back(Data.ComputedVisibleConversions); 4326 Record.push_back(Data.UserProvidedDefaultConstructor); 4327 Record.push_back(Data.DeclaredDefaultConstructor); 4328 Record.push_back(Data.DeclaredCopyConstructor); 4329 Record.push_back(Data.DeclaredMoveConstructor); 4330 Record.push_back(Data.DeclaredCopyAssignment); 4331 Record.push_back(Data.DeclaredMoveAssignment); 4332 Record.push_back(Data.DeclaredDestructor); 4333 Record.push_back(Data.FailedImplicitMoveConstructor); 4334 Record.push_back(Data.FailedImplicitMoveAssignment); 4335 // IsLambda bit is already saved. 4336 4337 Record.push_back(Data.NumBases); 4338 if (Data.NumBases > 0) 4339 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases, 4340 Record); 4341 4342 // FIXME: Make VBases lazily computed when needed to avoid storing them. 4343 Record.push_back(Data.NumVBases); 4344 if (Data.NumVBases > 0) 4345 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases, 4346 Record); 4347 4348 AddUnresolvedSet(Data.Conversions, Record); 4349 AddUnresolvedSet(Data.VisibleConversions, Record); 4350 // Data.Definition is the owning decl, no need to write it. 4351 AddDeclRef(Data.FirstFriend, Record); 4352 4353 // Add lambda-specific data. 4354 if (Data.IsLambda) { 4355 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData(); 4356 Record.push_back(Lambda.Dependent); 4357 Record.push_back(Lambda.NumCaptures); 4358 Record.push_back(Lambda.NumExplicitCaptures); 4359 Record.push_back(Lambda.ManglingNumber); 4360 AddDeclRef(Lambda.ContextDecl, Record); 4361 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) { 4362 LambdaExpr::Capture &Capture = Lambda.Captures[I]; 4363 AddSourceLocation(Capture.getLocation(), Record); 4364 Record.push_back(Capture.isImplicit()); 4365 Record.push_back(Capture.getCaptureKind()); // FIXME: stable! 4366 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0; 4367 AddDeclRef(Var, Record); 4368 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc() 4369 : SourceLocation(), 4370 Record); 4371 } 4372 } 4373 } 4374 ReaderInitialized(ASTReader * Reader)4375 void ASTWriter::ReaderInitialized(ASTReader *Reader) { 4376 assert(Reader && "Cannot remove chain"); 4377 assert((!Chain || Chain == Reader) && "Cannot replace chain"); 4378 assert(FirstDeclID == NextDeclID && 4379 FirstTypeID == NextTypeID && 4380 FirstIdentID == NextIdentID && 4381 FirstSubmoduleID == NextSubmoduleID && 4382 FirstSelectorID == NextSelectorID && 4383 "Setting chain after writing has started."); 4384 4385 Chain = Reader; 4386 4387 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls(); 4388 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes(); 4389 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers(); 4390 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules(); 4391 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors(); 4392 NextDeclID = FirstDeclID; 4393 NextTypeID = FirstTypeID; 4394 NextIdentID = FirstIdentID; 4395 NextSelectorID = FirstSelectorID; 4396 NextSubmoduleID = FirstSubmoduleID; 4397 } 4398 IdentifierRead(IdentID ID,IdentifierInfo * II)4399 void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { 4400 IdentifierIDs[II] = ID; 4401 if (II->hasMacroDefinition()) 4402 DeserializedMacroNames.push_back(II); 4403 } 4404 TypeRead(TypeIdx Idx,QualType T)4405 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) { 4406 // Always take the highest-numbered type index. This copes with an interesting 4407 // case for chained AST writing where we schedule writing the type and then, 4408 // later, deserialize the type from another AST. In this case, we want to 4409 // keep the higher-numbered entry so that we can properly write it out to 4410 // the AST file. 4411 TypeIdx &StoredIdx = TypeIdxs[T]; 4412 if (Idx.getIndex() >= StoredIdx.getIndex()) 4413 StoredIdx = Idx; 4414 } 4415 SelectorRead(SelectorID ID,Selector S)4416 void ASTWriter::SelectorRead(SelectorID ID, Selector S) { 4417 SelectorIDs[S] = ID; 4418 } 4419 MacroDefinitionRead(serialization::PreprocessedEntityID ID,MacroDefinition * MD)4420 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID, 4421 MacroDefinition *MD) { 4422 assert(MacroDefinitions.find(MD) == MacroDefinitions.end()); 4423 MacroDefinitions[MD] = ID; 4424 } 4425 MacroVisible(IdentifierInfo * II)4426 void ASTWriter::MacroVisible(IdentifierInfo *II) { 4427 DeserializedMacroNames.push_back(II); 4428 } 4429 ModuleRead(serialization::SubmoduleID ID,Module * Mod)4430 void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) { 4431 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end()); 4432 SubmoduleIDs[Mod] = ID; 4433 } 4434 CompletedTagDefinition(const TagDecl * D)4435 void ASTWriter::CompletedTagDefinition(const TagDecl *D) { 4436 assert(D->isCompleteDefinition()); 4437 assert(!WritingAST && "Already writing the AST!"); 4438 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 4439 // We are interested when a PCH decl is modified. 4440 if (RD->isFromASTFile()) { 4441 // A forward reference was mutated into a definition. Rewrite it. 4442 // FIXME: This happens during template instantiation, should we 4443 // have created a new definition decl instead ? 4444 RewriteDecl(RD); 4445 } 4446 } 4447 } AddedVisibleDecl(const DeclContext * DC,const Decl * D)4448 void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) { 4449 assert(!WritingAST && "Already writing the AST!"); 4450 4451 // TU and namespaces are handled elsewhere. 4452 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC)) 4453 return; 4454 4455 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile())) 4456 return; // Not a source decl added to a DeclContext from PCH. 4457 4458 AddUpdatedDeclContext(DC); 4459 } 4460 AddedCXXImplicitMember(const CXXRecordDecl * RD,const Decl * D)4461 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) { 4462 assert(!WritingAST && "Already writing the AST!"); 4463 assert(D->isImplicit()); 4464 if (!(!D->isFromASTFile() && RD->isFromASTFile())) 4465 return; // Not a source member added to a class from PCH. 4466 if (!isa<CXXMethodDecl>(D)) 4467 return; // We are interested in lazily declared implicit methods. 4468 4469 // A decl coming from PCH was modified. 4470 assert(RD->isCompleteDefinition()); 4471 UpdateRecord &Record = DeclUpdates[RD]; 4472 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER); 4473 Record.push_back(reinterpret_cast<uint64_t>(D)); 4474 } 4475 AddedCXXTemplateSpecialization(const ClassTemplateDecl * TD,const ClassTemplateSpecializationDecl * D)4476 void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, 4477 const ClassTemplateSpecializationDecl *D) { 4478 // The specializations set is kept in the canonical template. 4479 assert(!WritingAST && "Already writing the AST!"); 4480 TD = TD->getCanonicalDecl(); 4481 if (!(!D->isFromASTFile() && TD->isFromASTFile())) 4482 return; // Not a source specialization added to a template from PCH. 4483 4484 UpdateRecord &Record = DeclUpdates[TD]; 4485 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION); 4486 Record.push_back(reinterpret_cast<uint64_t>(D)); 4487 } 4488 AddedCXXTemplateSpecialization(const FunctionTemplateDecl * TD,const FunctionDecl * D)4489 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, 4490 const FunctionDecl *D) { 4491 // The specializations set is kept in the canonical template. 4492 assert(!WritingAST && "Already writing the AST!"); 4493 TD = TD->getCanonicalDecl(); 4494 if (!(!D->isFromASTFile() && TD->isFromASTFile())) 4495 return; // Not a source specialization added to a template from PCH. 4496 4497 UpdateRecord &Record = DeclUpdates[TD]; 4498 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION); 4499 Record.push_back(reinterpret_cast<uint64_t>(D)); 4500 } 4501 CompletedImplicitDefinition(const FunctionDecl * D)4502 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) { 4503 assert(!WritingAST && "Already writing the AST!"); 4504 if (!D->isFromASTFile()) 4505 return; // Declaration not imported from PCH. 4506 4507 // Implicit decl from a PCH was defined. 4508 // FIXME: Should implicit definition be a separate FunctionDecl? 4509 RewriteDecl(D); 4510 } 4511 StaticDataMemberInstantiated(const VarDecl * D)4512 void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) { 4513 assert(!WritingAST && "Already writing the AST!"); 4514 if (!D->isFromASTFile()) 4515 return; 4516 4517 // Since the actual instantiation is delayed, this really means that we need 4518 // to update the instantiation location. 4519 UpdateRecord &Record = DeclUpdates[D]; 4520 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER); 4521 AddSourceLocation( 4522 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record); 4523 } 4524 AddedObjCCategoryToInterface(const ObjCCategoryDecl * CatD,const ObjCInterfaceDecl * IFD)4525 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, 4526 const ObjCInterfaceDecl *IFD) { 4527 assert(!WritingAST && "Already writing the AST!"); 4528 if (!IFD->isFromASTFile()) 4529 return; // Declaration not imported from PCH. 4530 4531 assert(IFD->getDefinition() && "Category on a class without a definition?"); 4532 ObjCClassesWithCategories.insert( 4533 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition())); 4534 } 4535 4536 AddedObjCPropertyInClassExtension(const ObjCPropertyDecl * Prop,const ObjCPropertyDecl * OrigProp,const ObjCCategoryDecl * ClassExt)4537 void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop, 4538 const ObjCPropertyDecl *OrigProp, 4539 const ObjCCategoryDecl *ClassExt) { 4540 const ObjCInterfaceDecl *D = ClassExt->getClassInterface(); 4541 if (!D) 4542 return; 4543 4544 assert(!WritingAST && "Already writing the AST!"); 4545 if (!D->isFromASTFile()) 4546 return; // Declaration not imported from PCH. 4547 4548 RewriteDecl(D); 4549 } 4550