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/Expr.h" 23 #include "clang/AST/ExprCXX.h" 24 #include "clang/AST/Type.h" 25 #include "clang/AST/TypeLocVisitor.h" 26 #include "clang/Serialization/ASTReader.h" 27 #include "clang/Lex/MacroInfo.h" 28 #include "clang/Lex/PreprocessingRecord.h" 29 #include "clang/Lex/Preprocessor.h" 30 #include "clang/Lex/HeaderSearch.h" 31 #include "clang/Basic/FileManager.h" 32 #include "clang/Basic/OnDiskHashTable.h" 33 #include "clang/Basic/SourceManager.h" 34 #include "clang/Basic/SourceManagerInternals.h" 35 #include "clang/Basic/TargetInfo.h" 36 #include "clang/Basic/Version.h" 37 #include "llvm/ADT/APFloat.h" 38 #include "llvm/ADT/APInt.h" 39 #include "llvm/ADT/StringExtras.h" 40 #include "llvm/Bitcode/BitstreamWriter.h" 41 #include "llvm/Support/MemoryBuffer.h" 42 #include "llvm/System/Path.h" 43 #include <cstdio> 44 using namespace clang; 45 using namespace clang::serialization; 46 47 template <typename T, typename Allocator> 48 T *data(std::vector<T, Allocator> &v) { 49 return v.empty() ? 0 : &v.front(); 50 } 51 template <typename T, typename Allocator> 52 const T *data(const std::vector<T, Allocator> &v) { 53 return v.empty() ? 0 : &v.front(); 54 } 55 56 //===----------------------------------------------------------------------===// 57 // Type serialization 58 //===----------------------------------------------------------------------===// 59 60 namespace { 61 class ASTTypeWriter { 62 ASTWriter &Writer; 63 ASTWriter::RecordData &Record; 64 65 public: 66 /// \brief Type code that corresponds to the record generated. 67 TypeCode Code; 68 69 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordData &Record) 70 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { } 71 72 void VisitArrayType(const ArrayType *T); 73 void VisitFunctionType(const FunctionType *T); 74 void VisitTagType(const TagType *T); 75 76 #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); 77 #define ABSTRACT_TYPE(Class, Base) 78 #include "clang/AST/TypeNodes.def" 79 }; 80 } 81 82 void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) { 83 assert(false && "Built-in types are never serialized"); 84 } 85 86 void ASTTypeWriter::VisitComplexType(const ComplexType *T) { 87 Writer.AddTypeRef(T->getElementType(), Record); 88 Code = TYPE_COMPLEX; 89 } 90 91 void ASTTypeWriter::VisitPointerType(const PointerType *T) { 92 Writer.AddTypeRef(T->getPointeeType(), Record); 93 Code = TYPE_POINTER; 94 } 95 96 void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { 97 Writer.AddTypeRef(T->getPointeeType(), Record); 98 Code = TYPE_BLOCK_POINTER; 99 } 100 101 void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { 102 Writer.AddTypeRef(T->getPointeeType(), Record); 103 Code = TYPE_LVALUE_REFERENCE; 104 } 105 106 void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { 107 Writer.AddTypeRef(T->getPointeeType(), Record); 108 Code = TYPE_RVALUE_REFERENCE; 109 } 110 111 void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { 112 Writer.AddTypeRef(T->getPointeeType(), Record); 113 Writer.AddTypeRef(QualType(T->getClass(), 0), Record); 114 Code = TYPE_MEMBER_POINTER; 115 } 116 117 void ASTTypeWriter::VisitArrayType(const ArrayType *T) { 118 Writer.AddTypeRef(T->getElementType(), Record); 119 Record.push_back(T->getSizeModifier()); // FIXME: stable values 120 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values 121 } 122 123 void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { 124 VisitArrayType(T); 125 Writer.AddAPInt(T->getSize(), Record); 126 Code = TYPE_CONSTANT_ARRAY; 127 } 128 129 void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 130 VisitArrayType(T); 131 Code = TYPE_INCOMPLETE_ARRAY; 132 } 133 134 void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { 135 VisitArrayType(T); 136 Writer.AddSourceLocation(T->getLBracketLoc(), Record); 137 Writer.AddSourceLocation(T->getRBracketLoc(), Record); 138 Writer.AddStmt(T->getSizeExpr()); 139 Code = TYPE_VARIABLE_ARRAY; 140 } 141 142 void ASTTypeWriter::VisitVectorType(const VectorType *T) { 143 Writer.AddTypeRef(T->getElementType(), Record); 144 Record.push_back(T->getNumElements()); 145 Record.push_back(T->getAltiVecSpecific()); 146 Code = TYPE_VECTOR; 147 } 148 149 void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) { 150 VisitVectorType(T); 151 Code = TYPE_EXT_VECTOR; 152 } 153 154 void ASTTypeWriter::VisitFunctionType(const FunctionType *T) { 155 Writer.AddTypeRef(T->getResultType(), Record); 156 FunctionType::ExtInfo C = T->getExtInfo(); 157 Record.push_back(C.getNoReturn()); 158 Record.push_back(C.getRegParm()); 159 // FIXME: need to stabilize encoding of calling convention... 160 Record.push_back(C.getCC()); 161 } 162 163 void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 164 VisitFunctionType(T); 165 Code = TYPE_FUNCTION_NO_PROTO; 166 } 167 168 void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { 169 VisitFunctionType(T); 170 Record.push_back(T->getNumArgs()); 171 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) 172 Writer.AddTypeRef(T->getArgType(I), Record); 173 Record.push_back(T->isVariadic()); 174 Record.push_back(T->getTypeQuals()); 175 Record.push_back(T->hasExceptionSpec()); 176 Record.push_back(T->hasAnyExceptionSpec()); 177 Record.push_back(T->getNumExceptions()); 178 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) 179 Writer.AddTypeRef(T->getExceptionType(I), Record); 180 Code = TYPE_FUNCTION_PROTO; 181 } 182 183 void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { 184 Writer.AddDeclRef(T->getDecl(), Record); 185 Code = TYPE_UNRESOLVED_USING; 186 } 187 188 void ASTTypeWriter::VisitTypedefType(const TypedefType *T) { 189 Writer.AddDeclRef(T->getDecl(), Record); 190 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); 191 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); 192 Code = TYPE_TYPEDEF; 193 } 194 195 void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { 196 Writer.AddStmt(T->getUnderlyingExpr()); 197 Code = TYPE_TYPEOF_EXPR; 198 } 199 200 void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) { 201 Writer.AddTypeRef(T->getUnderlyingType(), Record); 202 Code = TYPE_TYPEOF; 203 } 204 205 void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) { 206 Writer.AddStmt(T->getUnderlyingExpr()); 207 Code = TYPE_DECLTYPE; 208 } 209 210 void ASTTypeWriter::VisitTagType(const TagType *T) { 211 Record.push_back(T->isDependentType()); 212 Writer.AddDeclRef(T->getDecl(), Record); 213 assert(!T->isBeingDefined() && 214 "Cannot serialize in the middle of a type definition"); 215 } 216 217 void ASTTypeWriter::VisitRecordType(const RecordType *T) { 218 VisitTagType(T); 219 Code = TYPE_RECORD; 220 } 221 222 void ASTTypeWriter::VisitEnumType(const EnumType *T) { 223 VisitTagType(T); 224 Code = TYPE_ENUM; 225 } 226 227 void 228 ASTTypeWriter::VisitSubstTemplateTypeParmType( 229 const SubstTemplateTypeParmType *T) { 230 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); 231 Writer.AddTypeRef(T->getReplacementType(), Record); 232 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM; 233 } 234 235 void 236 ASTTypeWriter::VisitTemplateSpecializationType( 237 const TemplateSpecializationType *T) { 238 Record.push_back(T->isDependentType()); 239 Writer.AddTemplateName(T->getTemplateName(), Record); 240 Record.push_back(T->getNumArgs()); 241 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end(); 242 ArgI != ArgE; ++ArgI) 243 Writer.AddTemplateArgument(*ArgI, Record); 244 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() 245 : T->getCanonicalTypeInternal(), 246 Record); 247 Code = TYPE_TEMPLATE_SPECIALIZATION; 248 } 249 250 void 251 ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { 252 VisitArrayType(T); 253 Writer.AddStmt(T->getSizeExpr()); 254 Writer.AddSourceRange(T->getBracketsRange(), Record); 255 Code = TYPE_DEPENDENT_SIZED_ARRAY; 256 } 257 258 void 259 ASTTypeWriter::VisitDependentSizedExtVectorType( 260 const DependentSizedExtVectorType *T) { 261 // FIXME: Serialize this type (C++ only) 262 assert(false && "Cannot serialize dependent sized extended vector types"); 263 } 264 265 void 266 ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { 267 Record.push_back(T->getDepth()); 268 Record.push_back(T->getIndex()); 269 Record.push_back(T->isParameterPack()); 270 Writer.AddIdentifierRef(T->getName(), Record); 271 Code = TYPE_TEMPLATE_TYPE_PARM; 272 } 273 274 void 275 ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) { 276 Record.push_back(T->getKeyword()); 277 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 278 Writer.AddIdentifierRef(T->getIdentifier(), Record); 279 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() 280 : T->getCanonicalTypeInternal(), 281 Record); 282 Code = TYPE_DEPENDENT_NAME; 283 } 284 285 void 286 ASTTypeWriter::VisitDependentTemplateSpecializationType( 287 const DependentTemplateSpecializationType *T) { 288 Record.push_back(T->getKeyword()); 289 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 290 Writer.AddIdentifierRef(T->getIdentifier(), Record); 291 Record.push_back(T->getNumArgs()); 292 for (DependentTemplateSpecializationType::iterator 293 I = T->begin(), E = T->end(); I != E; ++I) 294 Writer.AddTemplateArgument(*I, Record); 295 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; 296 } 297 298 void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { 299 Record.push_back(T->getKeyword()); 300 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 301 Writer.AddTypeRef(T->getNamedType(), Record); 302 Code = TYPE_ELABORATED; 303 } 304 305 void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) { 306 Writer.AddDeclRef(T->getDecl(), Record); 307 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record); 308 Code = TYPE_INJECTED_CLASS_NAME; 309 } 310 311 void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 312 Writer.AddDeclRef(T->getDecl(), Record); 313 Code = TYPE_OBJC_INTERFACE; 314 } 315 316 void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) { 317 Writer.AddTypeRef(T->getBaseType(), Record); 318 Record.push_back(T->getNumProtocols()); 319 for (ObjCObjectType::qual_iterator I = T->qual_begin(), 320 E = T->qual_end(); I != E; ++I) 321 Writer.AddDeclRef(*I, Record); 322 Code = TYPE_OBJC_OBJECT; 323 } 324 325 void 326 ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 327 Writer.AddTypeRef(T->getPointeeType(), Record); 328 Code = TYPE_OBJC_OBJECT_POINTER; 329 } 330 331 namespace { 332 333 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> { 334 ASTWriter &Writer; 335 ASTWriter::RecordData &Record; 336 337 public: 338 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordData &Record) 339 : Writer(Writer), Record(Record) { } 340 341 #define ABSTRACT_TYPELOC(CLASS, PARENT) 342 #define TYPELOC(CLASS, PARENT) \ 343 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 344 #include "clang/AST/TypeLocNodes.def" 345 346 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc); 347 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc); 348 }; 349 350 } 351 352 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 353 // nothing to do 354 } 355 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 356 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record); 357 if (TL.needsExtraLocalData()) { 358 Record.push_back(TL.getWrittenTypeSpec()); 359 Record.push_back(TL.getWrittenSignSpec()); 360 Record.push_back(TL.getWrittenWidthSpec()); 361 Record.push_back(TL.hasModeAttr()); 362 } 363 } 364 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { 365 Writer.AddSourceLocation(TL.getNameLoc(), Record); 366 } 367 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { 368 Writer.AddSourceLocation(TL.getStarLoc(), Record); 369 } 370 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 371 Writer.AddSourceLocation(TL.getCaretLoc(), Record); 372 } 373 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 374 Writer.AddSourceLocation(TL.getAmpLoc(), Record); 375 } 376 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 377 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record); 378 } 379 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 380 Writer.AddSourceLocation(TL.getStarLoc(), Record); 381 } 382 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { 383 Writer.AddSourceLocation(TL.getLBracketLoc(), Record); 384 Writer.AddSourceLocation(TL.getRBracketLoc(), Record); 385 Record.push_back(TL.getSizeExpr() ? 1 : 0); 386 if (TL.getSizeExpr()) 387 Writer.AddStmt(TL.getSizeExpr()); 388 } 389 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 390 VisitArrayTypeLoc(TL); 391 } 392 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 393 VisitArrayTypeLoc(TL); 394 } 395 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 396 VisitArrayTypeLoc(TL); 397 } 398 void TypeLocWriter::VisitDependentSizedArrayTypeLoc( 399 DependentSizedArrayTypeLoc TL) { 400 VisitArrayTypeLoc(TL); 401 } 402 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( 403 DependentSizedExtVectorTypeLoc TL) { 404 Writer.AddSourceLocation(TL.getNameLoc(), Record); 405 } 406 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { 407 Writer.AddSourceLocation(TL.getNameLoc(), Record); 408 } 409 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 410 Writer.AddSourceLocation(TL.getNameLoc(), Record); 411 } 412 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 413 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 414 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 415 Record.push_back(TL.getTrailingReturn()); 416 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 417 Writer.AddDeclRef(TL.getArg(i), Record); 418 } 419 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 420 VisitFunctionTypeLoc(TL); 421 } 422 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 423 VisitFunctionTypeLoc(TL); 424 } 425 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 426 Writer.AddSourceLocation(TL.getNameLoc(), Record); 427 } 428 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 429 Writer.AddSourceLocation(TL.getNameLoc(), Record); 430 } 431 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 432 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 433 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 434 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 435 } 436 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 437 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 438 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 439 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 440 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); 441 } 442 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 443 Writer.AddSourceLocation(TL.getNameLoc(), Record); 444 } 445 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { 446 Writer.AddSourceLocation(TL.getNameLoc(), Record); 447 } 448 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { 449 Writer.AddSourceLocation(TL.getNameLoc(), Record); 450 } 451 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 452 Writer.AddSourceLocation(TL.getNameLoc(), Record); 453 } 454 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( 455 SubstTemplateTypeParmTypeLoc TL) { 456 Writer.AddSourceLocation(TL.getNameLoc(), Record); 457 } 458 void TypeLocWriter::VisitTemplateSpecializationTypeLoc( 459 TemplateSpecializationTypeLoc TL) { 460 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); 461 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 462 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 463 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 464 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), 465 TL.getArgLoc(i).getLocInfo(), Record); 466 } 467 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 468 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 469 Writer.AddSourceRange(TL.getQualifierRange(), Record); 470 } 471 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 472 Writer.AddSourceLocation(TL.getNameLoc(), Record); 473 } 474 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 475 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 476 Writer.AddSourceRange(TL.getQualifierRange(), Record); 477 Writer.AddSourceLocation(TL.getNameLoc(), Record); 478 } 479 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( 480 DependentTemplateSpecializationTypeLoc TL) { 481 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 482 Writer.AddSourceRange(TL.getQualifierRange(), Record); 483 Writer.AddSourceLocation(TL.getNameLoc(), Record); 484 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 485 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 486 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 487 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), 488 TL.getArgLoc(I).getLocInfo(), Record); 489 } 490 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 491 Writer.AddSourceLocation(TL.getNameLoc(), Record); 492 } 493 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 494 Record.push_back(TL.hasBaseTypeAsWritten()); 495 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 496 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 497 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 498 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record); 499 } 500 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 501 Writer.AddSourceLocation(TL.getStarLoc(), Record); 502 } 503 504 //===----------------------------------------------------------------------===// 505 // ASTWriter Implementation 506 //===----------------------------------------------------------------------===// 507 508 static void EmitBlockID(unsigned ID, const char *Name, 509 llvm::BitstreamWriter &Stream, 510 ASTWriter::RecordData &Record) { 511 Record.clear(); 512 Record.push_back(ID); 513 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); 514 515 // Emit the block name if present. 516 if (Name == 0 || Name[0] == 0) return; 517 Record.clear(); 518 while (*Name) 519 Record.push_back(*Name++); 520 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); 521 } 522 523 static void EmitRecordID(unsigned ID, const char *Name, 524 llvm::BitstreamWriter &Stream, 525 ASTWriter::RecordData &Record) { 526 Record.clear(); 527 Record.push_back(ID); 528 while (*Name) 529 Record.push_back(*Name++); 530 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); 531 } 532 533 static void AddStmtsExprs(llvm::BitstreamWriter &Stream, 534 ASTWriter::RecordData &Record) { 535 #define RECORD(X) EmitRecordID(X, #X, Stream, Record) 536 RECORD(STMT_STOP); 537 RECORD(STMT_NULL_PTR); 538 RECORD(STMT_NULL); 539 RECORD(STMT_COMPOUND); 540 RECORD(STMT_CASE); 541 RECORD(STMT_DEFAULT); 542 RECORD(STMT_LABEL); 543 RECORD(STMT_IF); 544 RECORD(STMT_SWITCH); 545 RECORD(STMT_WHILE); 546 RECORD(STMT_DO); 547 RECORD(STMT_FOR); 548 RECORD(STMT_GOTO); 549 RECORD(STMT_INDIRECT_GOTO); 550 RECORD(STMT_CONTINUE); 551 RECORD(STMT_BREAK); 552 RECORD(STMT_RETURN); 553 RECORD(STMT_DECL); 554 RECORD(STMT_ASM); 555 RECORD(EXPR_PREDEFINED); 556 RECORD(EXPR_DECL_REF); 557 RECORD(EXPR_INTEGER_LITERAL); 558 RECORD(EXPR_FLOATING_LITERAL); 559 RECORD(EXPR_IMAGINARY_LITERAL); 560 RECORD(EXPR_STRING_LITERAL); 561 RECORD(EXPR_CHARACTER_LITERAL); 562 RECORD(EXPR_PAREN); 563 RECORD(EXPR_UNARY_OPERATOR); 564 RECORD(EXPR_SIZEOF_ALIGN_OF); 565 RECORD(EXPR_ARRAY_SUBSCRIPT); 566 RECORD(EXPR_CALL); 567 RECORD(EXPR_MEMBER); 568 RECORD(EXPR_BINARY_OPERATOR); 569 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); 570 RECORD(EXPR_CONDITIONAL_OPERATOR); 571 RECORD(EXPR_IMPLICIT_CAST); 572 RECORD(EXPR_CSTYLE_CAST); 573 RECORD(EXPR_COMPOUND_LITERAL); 574 RECORD(EXPR_EXT_VECTOR_ELEMENT); 575 RECORD(EXPR_INIT_LIST); 576 RECORD(EXPR_DESIGNATED_INIT); 577 RECORD(EXPR_IMPLICIT_VALUE_INIT); 578 RECORD(EXPR_VA_ARG); 579 RECORD(EXPR_ADDR_LABEL); 580 RECORD(EXPR_STMT); 581 RECORD(EXPR_TYPES_COMPATIBLE); 582 RECORD(EXPR_CHOOSE); 583 RECORD(EXPR_GNU_NULL); 584 RECORD(EXPR_SHUFFLE_VECTOR); 585 RECORD(EXPR_BLOCK); 586 RECORD(EXPR_BLOCK_DECL_REF); 587 RECORD(EXPR_OBJC_STRING_LITERAL); 588 RECORD(EXPR_OBJC_ENCODE); 589 RECORD(EXPR_OBJC_SELECTOR_EXPR); 590 RECORD(EXPR_OBJC_PROTOCOL_EXPR); 591 RECORD(EXPR_OBJC_IVAR_REF_EXPR); 592 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); 593 RECORD(EXPR_OBJC_KVC_REF_EXPR); 594 RECORD(EXPR_OBJC_MESSAGE_EXPR); 595 RECORD(STMT_OBJC_FOR_COLLECTION); 596 RECORD(STMT_OBJC_CATCH); 597 RECORD(STMT_OBJC_FINALLY); 598 RECORD(STMT_OBJC_AT_TRY); 599 RECORD(STMT_OBJC_AT_SYNCHRONIZED); 600 RECORD(STMT_OBJC_AT_THROW); 601 RECORD(EXPR_CXX_OPERATOR_CALL); 602 RECORD(EXPR_CXX_CONSTRUCT); 603 RECORD(EXPR_CXX_STATIC_CAST); 604 RECORD(EXPR_CXX_DYNAMIC_CAST); 605 RECORD(EXPR_CXX_REINTERPRET_CAST); 606 RECORD(EXPR_CXX_CONST_CAST); 607 RECORD(EXPR_CXX_FUNCTIONAL_CAST); 608 RECORD(EXPR_CXX_BOOL_LITERAL); 609 RECORD(EXPR_CXX_NULL_PTR_LITERAL); 610 #undef RECORD 611 } 612 613 void ASTWriter::WriteBlockInfoBlock() { 614 RecordData Record; 615 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); 616 617 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record) 618 #define RECORD(X) EmitRecordID(X, #X, Stream, Record) 619 620 // AST Top-Level Block. 621 BLOCK(AST_BLOCK); 622 RECORD(ORIGINAL_FILE_NAME); 623 RECORD(TYPE_OFFSET); 624 RECORD(DECL_OFFSET); 625 RECORD(LANGUAGE_OPTIONS); 626 RECORD(METADATA); 627 RECORD(IDENTIFIER_OFFSET); 628 RECORD(IDENTIFIER_TABLE); 629 RECORD(EXTERNAL_DEFINITIONS); 630 RECORD(SPECIAL_TYPES); 631 RECORD(STATISTICS); 632 RECORD(TENTATIVE_DEFINITIONS); 633 RECORD(UNUSED_FILESCOPED_DECLS); 634 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS); 635 RECORD(SELECTOR_OFFSETS); 636 RECORD(METHOD_POOL); 637 RECORD(PP_COUNTER_VALUE); 638 RECORD(SOURCE_LOCATION_OFFSETS); 639 RECORD(SOURCE_LOCATION_PRELOADS); 640 RECORD(STAT_CACHE); 641 RECORD(EXT_VECTOR_DECLS); 642 RECORD(VERSION_CONTROL_BRANCH_REVISION); 643 RECORD(MACRO_DEFINITION_OFFSETS); 644 RECORD(CHAINED_METADATA); 645 RECORD(REFERENCED_SELECTOR_POOL); 646 647 // SourceManager Block. 648 BLOCK(SOURCE_MANAGER_BLOCK); 649 RECORD(SM_SLOC_FILE_ENTRY); 650 RECORD(SM_SLOC_BUFFER_ENTRY); 651 RECORD(SM_SLOC_BUFFER_BLOB); 652 RECORD(SM_SLOC_INSTANTIATION_ENTRY); 653 RECORD(SM_LINE_TABLE); 654 655 // Preprocessor Block. 656 BLOCK(PREPROCESSOR_BLOCK); 657 RECORD(PP_MACRO_OBJECT_LIKE); 658 RECORD(PP_MACRO_FUNCTION_LIKE); 659 RECORD(PP_TOKEN); 660 RECORD(PP_MACRO_INSTANTIATION); 661 RECORD(PP_MACRO_DEFINITION); 662 663 // Decls and Types block. 664 BLOCK(DECLTYPES_BLOCK); 665 RECORD(TYPE_EXT_QUAL); 666 RECORD(TYPE_COMPLEX); 667 RECORD(TYPE_POINTER); 668 RECORD(TYPE_BLOCK_POINTER); 669 RECORD(TYPE_LVALUE_REFERENCE); 670 RECORD(TYPE_RVALUE_REFERENCE); 671 RECORD(TYPE_MEMBER_POINTER); 672 RECORD(TYPE_CONSTANT_ARRAY); 673 RECORD(TYPE_INCOMPLETE_ARRAY); 674 RECORD(TYPE_VARIABLE_ARRAY); 675 RECORD(TYPE_VECTOR); 676 RECORD(TYPE_EXT_VECTOR); 677 RECORD(TYPE_FUNCTION_PROTO); 678 RECORD(TYPE_FUNCTION_NO_PROTO); 679 RECORD(TYPE_TYPEDEF); 680 RECORD(TYPE_TYPEOF_EXPR); 681 RECORD(TYPE_TYPEOF); 682 RECORD(TYPE_RECORD); 683 RECORD(TYPE_ENUM); 684 RECORD(TYPE_OBJC_INTERFACE); 685 RECORD(TYPE_OBJC_OBJECT); 686 RECORD(TYPE_OBJC_OBJECT_POINTER); 687 RECORD(DECL_TRANSLATION_UNIT); 688 RECORD(DECL_TYPEDEF); 689 RECORD(DECL_ENUM); 690 RECORD(DECL_RECORD); 691 RECORD(DECL_ENUM_CONSTANT); 692 RECORD(DECL_FUNCTION); 693 RECORD(DECL_OBJC_METHOD); 694 RECORD(DECL_OBJC_INTERFACE); 695 RECORD(DECL_OBJC_PROTOCOL); 696 RECORD(DECL_OBJC_IVAR); 697 RECORD(DECL_OBJC_AT_DEFS_FIELD); 698 RECORD(DECL_OBJC_CLASS); 699 RECORD(DECL_OBJC_FORWARD_PROTOCOL); 700 RECORD(DECL_OBJC_CATEGORY); 701 RECORD(DECL_OBJC_CATEGORY_IMPL); 702 RECORD(DECL_OBJC_IMPLEMENTATION); 703 RECORD(DECL_OBJC_COMPATIBLE_ALIAS); 704 RECORD(DECL_OBJC_PROPERTY); 705 RECORD(DECL_OBJC_PROPERTY_IMPL); 706 RECORD(DECL_FIELD); 707 RECORD(DECL_VAR); 708 RECORD(DECL_IMPLICIT_PARAM); 709 RECORD(DECL_PARM_VAR); 710 RECORD(DECL_FILE_SCOPE_ASM); 711 RECORD(DECL_BLOCK); 712 RECORD(DECL_CONTEXT_LEXICAL); 713 RECORD(DECL_CONTEXT_VISIBLE); 714 // Statements and Exprs can occur in the Decls and Types block. 715 AddStmtsExprs(Stream, Record); 716 #undef RECORD 717 #undef BLOCK 718 Stream.ExitBlock(); 719 } 720 721 /// \brief Adjusts the given filename to only write out the portion of the 722 /// filename that is not part of the system root directory. 723 /// 724 /// \param Filename the file name to adjust. 725 /// 726 /// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and 727 /// the returned filename will be adjusted by this system root. 728 /// 729 /// \returns either the original filename (if it needs no adjustment) or the 730 /// adjusted filename (which points into the @p Filename parameter). 731 static const char * 732 adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) { 733 assert(Filename && "No file name to adjust?"); 734 735 if (!isysroot) 736 return Filename; 737 738 // Verify that the filename and the system root have the same prefix. 739 unsigned Pos = 0; 740 for (; Filename[Pos] && isysroot[Pos]; ++Pos) 741 if (Filename[Pos] != isysroot[Pos]) 742 return Filename; // Prefixes don't match. 743 744 // We hit the end of the filename before we hit the end of the system root. 745 if (!Filename[Pos]) 746 return Filename; 747 748 // If the file name has a '/' at the current position, skip over the '/'. 749 // We distinguish sysroot-based includes from absolute includes by the 750 // absence of '/' at the beginning of sysroot-based includes. 751 if (Filename[Pos] == '/') 752 ++Pos; 753 754 return Filename + Pos; 755 } 756 757 /// \brief Write the AST metadata (e.g., i686-apple-darwin9). 758 void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) { 759 using namespace llvm; 760 761 // Metadata 762 const TargetInfo &Target = Context.Target; 763 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev(); 764 MetaAbbrev->Add(BitCodeAbbrevOp( 765 Chain ? CHAINED_METADATA : METADATA)); 766 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major 767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor 768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major 769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor 770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable 771 // Target triple or chained PCH name 772 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 773 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev); 774 775 RecordData Record; 776 Record.push_back(Chain ? CHAINED_METADATA : METADATA); 777 Record.push_back(VERSION_MAJOR); 778 Record.push_back(VERSION_MINOR); 779 Record.push_back(CLANG_VERSION_MAJOR); 780 Record.push_back(CLANG_VERSION_MINOR); 781 Record.push_back(isysroot != 0); 782 // FIXME: This writes the absolute path for chained headers. 783 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple(); 784 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr); 785 786 // Original file name 787 SourceManager &SM = Context.getSourceManager(); 788 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { 789 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev(); 790 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME)); 791 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 792 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev); 793 794 llvm::sys::Path MainFilePath(MainFile->getName()); 795 796 MainFilePath.makeAbsolute(); 797 798 const char *MainFileNameStr = MainFilePath.c_str(); 799 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr, 800 isysroot); 801 RecordData Record; 802 Record.push_back(ORIGINAL_FILE_NAME); 803 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr); 804 } 805 806 // Repository branch/version information. 807 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev(); 808 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION)); 809 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag 810 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev); 811 Record.clear(); 812 Record.push_back(VERSION_CONTROL_BRANCH_REVISION); 813 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record, 814 getClangFullRepositoryVersion()); 815 } 816 817 /// \brief Write the LangOptions structure. 818 void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) { 819 RecordData Record; 820 Record.push_back(LangOpts.Trigraphs); 821 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments. 822 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers. 823 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode. 824 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc) 825 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords 826 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'. 827 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++ 828 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants. 829 Record.push_back(LangOpts.C99); // C99 Support 830 Record.push_back(LangOpts.Microsoft); // Microsoft extensions. 831 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is 832 // already saved elsewhere. 833 Record.push_back(LangOpts.CPlusPlus); // C++ Support 834 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support 835 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords. 836 837 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled. 838 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled. 839 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C 840 // modern abi enabled. 841 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced 842 // modern abi enabled. 843 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled.. 844 845 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings 846 Record.push_back(LangOpts.WritableStrings); // Allow writable strings 847 Record.push_back(LangOpts.LaxVectorConversions); 848 Record.push_back(LangOpts.AltiVec); 849 Record.push_back(LangOpts.Exceptions); // Support exception handling. 850 Record.push_back(LangOpts.SjLjExceptions); 851 852 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime. 853 Record.push_back(LangOpts.Freestanding); // Freestanding implementation 854 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin) 855 856 // Whether static initializers are protected by locks. 857 Record.push_back(LangOpts.ThreadsafeStatics); 858 Record.push_back(LangOpts.POSIXThreads); 859 Record.push_back(LangOpts.Blocks); // block extension to C 860 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if 861 // they are unused. 862 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno 863 // (modulo the platform support). 864 865 Record.push_back(LangOpts.getSignedOverflowBehavior()); 866 Record.push_back(LangOpts.HeinousExtensions); 867 868 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. 869 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be 870 // defined. 871 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as 872 // opposed to __DYNAMIC__). 873 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero. 874 875 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be 876 // used (instead of C99 semantics). 877 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined. 878 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should 879 // be enabled. 880 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or 881 // unsigned type 882 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short 883 Record.push_back(LangOpts.getGCMode()); 884 Record.push_back(LangOpts.getVisibilityMode()); 885 Record.push_back(LangOpts.getStackProtectorMode()); 886 Record.push_back(LangOpts.InstantiationDepth); 887 Record.push_back(LangOpts.OpenCL); 888 Record.push_back(LangOpts.CatchUndefined); 889 Record.push_back(LangOpts.ElideConstructors); 890 Record.push_back(LangOpts.SpellChecking); 891 Stream.EmitRecord(LANGUAGE_OPTIONS, Record); 892 } 893 894 //===----------------------------------------------------------------------===// 895 // stat cache Serialization 896 //===----------------------------------------------------------------------===// 897 898 namespace { 899 // Trait used for the on-disk hash table of stat cache results. 900 class ASTStatCacheTrait { 901 public: 902 typedef const char * key_type; 903 typedef key_type key_type_ref; 904 905 typedef std::pair<int, struct stat> data_type; 906 typedef const data_type& data_type_ref; 907 908 static unsigned ComputeHash(const char *path) { 909 return llvm::HashString(path); 910 } 911 912 std::pair<unsigned,unsigned> 913 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path, 914 data_type_ref Data) { 915 unsigned StrLen = strlen(path); 916 clang::io::Emit16(Out, StrLen); 917 unsigned DataLen = 1; // result value 918 if (Data.first == 0) 919 DataLen += 4 + 4 + 2 + 8 + 8; 920 clang::io::Emit8(Out, DataLen); 921 return std::make_pair(StrLen + 1, DataLen); 922 } 923 924 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) { 925 Out.write(path, KeyLen); 926 } 927 928 void EmitData(llvm::raw_ostream& Out, key_type_ref, 929 data_type_ref Data, unsigned DataLen) { 930 using namespace clang::io; 931 uint64_t Start = Out.tell(); (void)Start; 932 933 // Result of stat() 934 Emit8(Out, Data.first? 1 : 0); 935 936 if (Data.first == 0) { 937 Emit32(Out, (uint32_t) Data.second.st_ino); 938 Emit32(Out, (uint32_t) Data.second.st_dev); 939 Emit16(Out, (uint16_t) Data.second.st_mode); 940 Emit64(Out, (uint64_t) Data.second.st_mtime); 941 Emit64(Out, (uint64_t) Data.second.st_size); 942 } 943 944 assert(Out.tell() - Start == DataLen && "Wrong data length"); 945 } 946 }; 947 } // end anonymous namespace 948 949 /// \brief Write the stat() system call cache to the AST file. 950 void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) { 951 // Build the on-disk hash table containing information about every 952 // stat() call. 953 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator; 954 unsigned NumStatEntries = 0; 955 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(), 956 StatEnd = StatCalls.end(); 957 Stat != StatEnd; ++Stat, ++NumStatEntries) { 958 const char *Filename = Stat->first(); 959 Generator.insert(Filename, Stat->second); 960 } 961 962 // Create the on-disk hash table in a buffer. 963 llvm::SmallString<4096> StatCacheData; 964 uint32_t BucketOffset; 965 { 966 llvm::raw_svector_ostream Out(StatCacheData); 967 // Make sure that no bucket is at offset 0 968 clang::io::Emit32(Out, 0); 969 BucketOffset = Generator.Emit(Out); 970 } 971 972 // Create a blob abbreviation 973 using namespace llvm; 974 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 975 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE)); 976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 979 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev); 980 981 // Write the stat cache 982 RecordData Record; 983 Record.push_back(STAT_CACHE); 984 Record.push_back(BucketOffset); 985 Record.push_back(NumStatEntries); 986 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str()); 987 } 988 989 //===----------------------------------------------------------------------===// 990 // Source Manager Serialization 991 //===----------------------------------------------------------------------===// 992 993 /// \brief Create an abbreviation for the SLocEntry that refers to a 994 /// file. 995 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { 996 using namespace llvm; 997 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 998 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY)); 999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1003 // FileEntry fields. 1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size 1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time 1006 // HeaderFileInfo fields. 1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport 1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo 1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes 1010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro 1011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1012 return Stream.EmitAbbrev(Abbrev); 1013 } 1014 1015 /// \brief Create an abbreviation for the SLocEntry that refers to a 1016 /// buffer. 1017 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { 1018 using namespace llvm; 1019 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1020 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY)); 1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob 1026 return Stream.EmitAbbrev(Abbrev); 1027 } 1028 1029 /// \brief Create an abbreviation for the SLocEntry that refers to a 1030 /// buffer's blob. 1031 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { 1032 using namespace llvm; 1033 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1034 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB)); 1035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob 1036 return Stream.EmitAbbrev(Abbrev); 1037 } 1038 1039 /// \brief Create an abbreviation for the SLocEntry that refers to an 1040 /// buffer. 1041 static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) { 1042 using namespace llvm; 1043 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1044 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY)); 1045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location 1047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location 1048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location 1049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length 1050 return Stream.EmitAbbrev(Abbrev); 1051 } 1052 1053 /// \brief Writes the block containing the serialized form of the 1054 /// source manager. 1055 /// 1056 /// TODO: We should probably use an on-disk hash table (stored in a 1057 /// blob), indexed based on the file name, so that we only create 1058 /// entries for files that we actually need. In the common case (no 1059 /// errors), we probably won't have to create file entries for any of 1060 /// the files in the AST. 1061 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, 1062 const Preprocessor &PP, 1063 const char *isysroot) { 1064 RecordData Record; 1065 1066 // Enter the source manager block. 1067 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3); 1068 1069 // Abbreviations for the various kinds of source-location entries. 1070 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); 1071 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); 1072 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); 1073 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream); 1074 1075 // Write the line table. 1076 if (SourceMgr.hasLineTable()) { 1077 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1078 1079 // Emit the file names 1080 Record.push_back(LineTable.getNumFilenames()); 1081 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { 1082 // Emit the file name 1083 const char *Filename = LineTable.getFilename(I); 1084 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1085 unsigned FilenameLen = Filename? strlen(Filename) : 0; 1086 Record.push_back(FilenameLen); 1087 if (FilenameLen) 1088 Record.insert(Record.end(), Filename, Filename + FilenameLen); 1089 } 1090 1091 // Emit the line entries 1092 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); 1093 L != LEnd; ++L) { 1094 // Emit the file ID 1095 Record.push_back(L->first); 1096 1097 // Emit the line entries 1098 Record.push_back(L->second.size()); 1099 for (std::vector<LineEntry>::iterator LE = L->second.begin(), 1100 LEEnd = L->second.end(); 1101 LE != LEEnd; ++LE) { 1102 Record.push_back(LE->FileOffset); 1103 Record.push_back(LE->LineNo); 1104 Record.push_back(LE->FilenameID); 1105 Record.push_back((unsigned)LE->FileKind); 1106 Record.push_back(LE->IncludeOffset); 1107 } 1108 } 1109 Stream.EmitRecord(SM_LINE_TABLE, Record); 1110 } 1111 1112 // Write out the source location entry table. We skip the first 1113 // entry, which is always the same dummy entry. 1114 std::vector<uint32_t> SLocEntryOffsets; 1115 RecordData PreloadSLocs; 1116 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0; 1117 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID); 1118 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size(); 1119 I != N; ++I) { 1120 // Get this source location entry. 1121 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I); 1122 1123 // Record the offset of this source-location entry. 1124 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); 1125 1126 // Figure out which record code to use. 1127 unsigned Code; 1128 if (SLoc->isFile()) { 1129 if (SLoc->getFile().getContentCache()->Entry) 1130 Code = SM_SLOC_FILE_ENTRY; 1131 else 1132 Code = SM_SLOC_BUFFER_ENTRY; 1133 } else 1134 Code = SM_SLOC_INSTANTIATION_ENTRY; 1135 Record.clear(); 1136 Record.push_back(Code); 1137 1138 Record.push_back(SLoc->getOffset()); 1139 if (SLoc->isFile()) { 1140 const SrcMgr::FileInfo &File = SLoc->getFile(); 1141 Record.push_back(File.getIncludeLoc().getRawEncoding()); 1142 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding 1143 Record.push_back(File.hasLineDirectives()); 1144 1145 const SrcMgr::ContentCache *Content = File.getContentCache(); 1146 if (Content->Entry) { 1147 // The source location entry is a file. The blob associated 1148 // with this entry is the file name. 1149 1150 // Emit size/modification time for this file. 1151 Record.push_back(Content->Entry->getSize()); 1152 Record.push_back(Content->Entry->getModificationTime()); 1153 1154 // Emit header-search information associated with this file. 1155 HeaderFileInfo HFI; 1156 HeaderSearch &HS = PP.getHeaderSearchInfo(); 1157 if (Content->Entry->getUID() < HS.header_file_size()) 1158 HFI = HS.header_file_begin()[Content->Entry->getUID()]; 1159 Record.push_back(HFI.isImport); 1160 Record.push_back(HFI.DirInfo); 1161 Record.push_back(HFI.NumIncludes); 1162 AddIdentifierRef(HFI.ControllingMacro, Record); 1163 1164 // Turn the file name into an absolute path, if it isn't already. 1165 const char *Filename = Content->Entry->getName(); 1166 llvm::sys::Path FilePath(Filename, strlen(Filename)); 1167 FilePath.makeAbsolute(); 1168 Filename = FilePath.c_str(); 1169 1170 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1171 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename); 1172 1173 // FIXME: For now, preload all file source locations, so that 1174 // we get the appropriate File entries in the reader. This is 1175 // a temporary measure. 1176 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size()); 1177 } else { 1178 // The source location entry is a buffer. The blob associated 1179 // with this entry contains the contents of the buffer. 1180 1181 // We add one to the size so that we capture the trailing NULL 1182 // that is required by llvm::MemoryBuffer::getMemBuffer (on 1183 // the reader side). 1184 const llvm::MemoryBuffer *Buffer 1185 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); 1186 const char *Name = Buffer->getBufferIdentifier(); 1187 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, 1188 llvm::StringRef(Name, strlen(Name) + 1)); 1189 Record.clear(); 1190 Record.push_back(SM_SLOC_BUFFER_BLOB); 1191 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, 1192 llvm::StringRef(Buffer->getBufferStart(), 1193 Buffer->getBufferSize() + 1)); 1194 1195 if (strcmp(Name, "<built-in>") == 0) 1196 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size()); 1197 } 1198 } else { 1199 // The source location entry is an instantiation. 1200 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); 1201 Record.push_back(Inst.getSpellingLoc().getRawEncoding()); 1202 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); 1203 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); 1204 1205 // Compute the token length for this macro expansion. 1206 unsigned NextOffset = SourceMgr.getNextOffset(); 1207 if (I + 1 != N) 1208 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset(); 1209 Record.push_back(NextOffset - SLoc->getOffset() - 1); 1210 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); 1211 } 1212 } 1213 1214 Stream.ExitBlock(); 1215 1216 if (SLocEntryOffsets.empty()) 1217 return; 1218 1219 // Write the source-location offsets table into the AST block. This 1220 // table is used for lazily loading source-location information. 1221 using namespace llvm; 1222 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1223 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS)); 1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs 1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset 1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets 1227 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); 1228 1229 Record.clear(); 1230 Record.push_back(SOURCE_LOCATION_OFFSETS); 1231 Record.push_back(SLocEntryOffsets.size()); 1232 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0; 1233 Record.push_back(SourceMgr.getNextOffset() - BaseOffset); 1234 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, 1235 (const char *)data(SLocEntryOffsets), 1236 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0])); 1237 1238 // Write the source location entry preloads array, telling the AST 1239 // reader which source locations entries it should load eagerly. 1240 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs); 1241 } 1242 1243 //===----------------------------------------------------------------------===// 1244 // Preprocessor Serialization 1245 //===----------------------------------------------------------------------===// 1246 1247 /// \brief Writes the block containing the serialized form of the 1248 /// preprocessor. 1249 /// 1250 void ASTWriter::WritePreprocessor(const Preprocessor &PP) { 1251 RecordData Record; 1252 1253 // If the preprocessor __COUNTER__ value has been bumped, remember it. 1254 if (PP.getCounterValue() != 0) { 1255 Record.push_back(PP.getCounterValue()); 1256 Stream.EmitRecord(PP_COUNTER_VALUE, Record); 1257 Record.clear(); 1258 } 1259 1260 // Enter the preprocessor block. 1261 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); 1262 1263 // If the AST file contains __DATE__ or __TIME__ emit a warning about this. 1264 // FIXME: use diagnostics subsystem for localization etc. 1265 if (PP.SawDateOrTime()) 1266 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); 1267 1268 1269 // Loop over all the macro definitions that are live at the end of the file, 1270 // emitting each to the PP section. 1271 PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); 1272 unsigned InclusionAbbrev = 0; 1273 if (PPRec) { 1274 using namespace llvm; 1275 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1276 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE)); 1277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index 1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location 1279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location 1280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length 1281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes 1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind 1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1284 InclusionAbbrev = Stream.EmitAbbrev(Abbrev); 1285 } 1286 1287 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); 1288 I != E; ++I) { 1289 // FIXME: This emits macros in hash table order, we should do it in a stable 1290 // order so that output is reproducible. 1291 MacroInfo *MI = I->second; 1292 1293 // Don't emit builtin macros like __LINE__ to the AST file unless they have 1294 // been redefined by the header (in which case they are not isBuiltinMacro). 1295 // Also skip macros from a AST file if we're chaining. 1296 1297 // FIXME: There is a (probably minor) optimization we could do here, if 1298 // the macro comes from the original PCH but the identifier comes from a 1299 // chained PCH, by storing the offset into the original PCH rather than 1300 // writing the macro definition a second time. 1301 if (MI->isBuiltinMacro() || 1302 (Chain && I->first->isFromAST() && MI->isFromAST())) 1303 continue; 1304 1305 AddIdentifierRef(I->first, Record); 1306 MacroOffsets[I->first] = Stream.GetCurrentBitNo(); 1307 Record.push_back(MI->getDefinitionLoc().getRawEncoding()); 1308 Record.push_back(MI->isUsed()); 1309 1310 unsigned Code; 1311 if (MI->isObjectLike()) { 1312 Code = PP_MACRO_OBJECT_LIKE; 1313 } else { 1314 Code = PP_MACRO_FUNCTION_LIKE; 1315 1316 Record.push_back(MI->isC99Varargs()); 1317 Record.push_back(MI->isGNUVarargs()); 1318 Record.push_back(MI->getNumArgs()); 1319 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); 1320 I != E; ++I) 1321 AddIdentifierRef(*I, Record); 1322 } 1323 1324 // If we have a detailed preprocessing record, record the macro definition 1325 // ID that corresponds to this macro. 1326 if (PPRec) 1327 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI))); 1328 1329 Stream.EmitRecord(Code, Record); 1330 Record.clear(); 1331 1332 // Emit the tokens array. 1333 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { 1334 // Note that we know that the preprocessor does not have any annotation 1335 // tokens in it because they are created by the parser, and thus can't be 1336 // in a macro definition. 1337 const Token &Tok = MI->getReplacementToken(TokNo); 1338 1339 Record.push_back(Tok.getLocation().getRawEncoding()); 1340 Record.push_back(Tok.getLength()); 1341 1342 // FIXME: When reading literal tokens, reconstruct the literal pointer if 1343 // it is needed. 1344 AddIdentifierRef(Tok.getIdentifierInfo(), Record); 1345 1346 // FIXME: Should translate token kind to a stable encoding. 1347 Record.push_back(Tok.getKind()); 1348 // FIXME: Should translate token flags to a stable encoding. 1349 Record.push_back(Tok.getFlags()); 1350 1351 Stream.EmitRecord(PP_TOKEN, Record); 1352 Record.clear(); 1353 } 1354 ++NumMacros; 1355 } 1356 1357 // If the preprocessor has a preprocessing record, emit it. 1358 unsigned NumPreprocessingRecords = 0; 1359 if (PPRec) { 1360 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0; 1361 for (PreprocessingRecord::iterator E = PPRec->begin(Chain), 1362 EEnd = PPRec->end(Chain); 1363 E != EEnd; ++E) { 1364 Record.clear(); 1365 1366 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) { 1367 Record.push_back(IndexBase + NumPreprocessingRecords++); 1368 AddSourceLocation(MI->getSourceRange().getBegin(), Record); 1369 AddSourceLocation(MI->getSourceRange().getEnd(), Record); 1370 AddIdentifierRef(MI->getName(), Record); 1371 Record.push_back(getMacroDefinitionID(MI->getDefinition())); 1372 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record); 1373 continue; 1374 } 1375 1376 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { 1377 // Record this macro definition's location. 1378 MacroID ID = getMacroDefinitionID(MD); 1379 1380 // Don't write the macro definition if it is from another AST file. 1381 if (ID < FirstMacroID) 1382 continue; 1383 1384 unsigned Position = ID - FirstMacroID; 1385 if (Position != MacroDefinitionOffsets.size()) { 1386 if (Position > MacroDefinitionOffsets.size()) 1387 MacroDefinitionOffsets.resize(Position + 1); 1388 1389 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo(); 1390 } else 1391 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo()); 1392 1393 Record.push_back(IndexBase + NumPreprocessingRecords++); 1394 Record.push_back(ID); 1395 AddSourceLocation(MD->getSourceRange().getBegin(), Record); 1396 AddSourceLocation(MD->getSourceRange().getEnd(), Record); 1397 AddIdentifierRef(MD->getName(), Record); 1398 AddSourceLocation(MD->getLocation(), Record); 1399 Stream.EmitRecord(PP_MACRO_DEFINITION, Record); 1400 continue; 1401 } 1402 1403 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { 1404 Record.push_back(PP_INCLUSION_DIRECTIVE); 1405 Record.push_back(IndexBase + NumPreprocessingRecords++); 1406 AddSourceLocation(ID->getSourceRange().getBegin(), Record); 1407 AddSourceLocation(ID->getSourceRange().getEnd(), Record); 1408 Record.push_back(ID->getFileName().size()); 1409 Record.push_back(ID->wasInQuotes()); 1410 Record.push_back(static_cast<unsigned>(ID->getKind())); 1411 llvm::SmallString<64> Buffer; 1412 Buffer += ID->getFileName(); 1413 Buffer += ID->getFile()->getName(); 1414 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); 1415 continue; 1416 } 1417 } 1418 } 1419 1420 Stream.ExitBlock(); 1421 1422 // Write the offsets table for the preprocessing record. 1423 if (NumPreprocessingRecords > 0) { 1424 // Write the offsets table for identifier IDs. 1425 using namespace llvm; 1426 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1427 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS)); 1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records 1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs 1430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1431 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1432 1433 Record.clear(); 1434 Record.push_back(MACRO_DEFINITION_OFFSETS); 1435 Record.push_back(NumPreprocessingRecords); 1436 Record.push_back(MacroDefinitionOffsets.size()); 1437 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record, 1438 (const char *)data(MacroDefinitionOffsets), 1439 MacroDefinitionOffsets.size() * sizeof(uint32_t)); 1440 } 1441 } 1442 1443 //===----------------------------------------------------------------------===// 1444 // Type Serialization 1445 //===----------------------------------------------------------------------===// 1446 1447 /// \brief Write the representation of a type to the AST stream. 1448 void ASTWriter::WriteType(QualType T) { 1449 TypeIdx &Idx = TypeIdxs[T]; 1450 if (Idx.getIndex() == 0) // we haven't seen this type before. 1451 Idx = TypeIdx(NextTypeID++); 1452 1453 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST"); 1454 1455 // Record the offset for this type. 1456 unsigned Index = Idx.getIndex() - FirstTypeID; 1457 if (TypeOffsets.size() == Index) 1458 TypeOffsets.push_back(Stream.GetCurrentBitNo()); 1459 else if (TypeOffsets.size() < Index) { 1460 TypeOffsets.resize(Index + 1); 1461 TypeOffsets[Index] = Stream.GetCurrentBitNo(); 1462 } 1463 1464 RecordData Record; 1465 1466 // Emit the type's representation. 1467 ASTTypeWriter W(*this, Record); 1468 1469 if (T.hasLocalNonFastQualifiers()) { 1470 Qualifiers Qs = T.getLocalQualifiers(); 1471 AddTypeRef(T.getLocalUnqualifiedType(), Record); 1472 Record.push_back(Qs.getAsOpaqueValue()); 1473 W.Code = TYPE_EXT_QUAL; 1474 } else { 1475 switch (T->getTypeClass()) { 1476 // For all of the concrete, non-dependent types, call the 1477 // appropriate visitor function. 1478 #define TYPE(Class, Base) \ 1479 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; 1480 #define ABSTRACT_TYPE(Class, Base) 1481 #include "clang/AST/TypeNodes.def" 1482 } 1483 } 1484 1485 // Emit the serialized record. 1486 Stream.EmitRecord(W.Code, Record); 1487 1488 // Flush any expressions that were written as part of this type. 1489 FlushStmts(); 1490 } 1491 1492 //===----------------------------------------------------------------------===// 1493 // Declaration Serialization 1494 //===----------------------------------------------------------------------===// 1495 1496 /// \brief Write the block containing all of the declaration IDs 1497 /// lexically declared within the given DeclContext. 1498 /// 1499 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the 1500 /// bistream, or 0 if no block was written. 1501 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, 1502 DeclContext *DC) { 1503 if (DC->decls_empty()) 1504 return 0; 1505 1506 uint64_t Offset = Stream.GetCurrentBitNo(); 1507 RecordData Record; 1508 Record.push_back(DECL_CONTEXT_LEXICAL); 1509 llvm::SmallVector<KindDeclIDPair, 64> Decls; 1510 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 1511 D != DEnd; ++D) 1512 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D))); 1513 1514 ++NumLexicalDeclContexts; 1515 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, 1516 reinterpret_cast<char*>(Decls.data()), 1517 Decls.size() * sizeof(KindDeclIDPair)); 1518 return Offset; 1519 } 1520 1521 void ASTWriter::WriteTypeDeclOffsets() { 1522 using namespace llvm; 1523 RecordData Record; 1524 1525 // Write the type offsets array 1526 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1527 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET)); 1528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types 1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block 1530 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1531 Record.clear(); 1532 Record.push_back(TYPE_OFFSET); 1533 Record.push_back(TypeOffsets.size()); 1534 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, 1535 (const char *)data(TypeOffsets), 1536 TypeOffsets.size() * sizeof(TypeOffsets[0])); 1537 1538 // Write the declaration offsets array 1539 Abbrev = new BitCodeAbbrev(); 1540 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET)); 1541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations 1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block 1543 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1544 Record.clear(); 1545 Record.push_back(DECL_OFFSET); 1546 Record.push_back(DeclOffsets.size()); 1547 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, 1548 (const char *)data(DeclOffsets), 1549 DeclOffsets.size() * sizeof(DeclOffsets[0])); 1550 } 1551 1552 //===----------------------------------------------------------------------===// 1553 // Global Method Pool and Selector Serialization 1554 //===----------------------------------------------------------------------===// 1555 1556 namespace { 1557 // Trait used for the on-disk hash table used in the method pool. 1558 class ASTMethodPoolTrait { 1559 ASTWriter &Writer; 1560 1561 public: 1562 typedef Selector key_type; 1563 typedef key_type key_type_ref; 1564 1565 struct data_type { 1566 SelectorID ID; 1567 ObjCMethodList Instance, Factory; 1568 }; 1569 typedef const data_type& data_type_ref; 1570 1571 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { } 1572 1573 static unsigned ComputeHash(Selector Sel) { 1574 return serialization::ComputeHash(Sel); 1575 } 1576 1577 std::pair<unsigned,unsigned> 1578 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel, 1579 data_type_ref Methods) { 1580 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); 1581 clang::io::Emit16(Out, KeyLen); 1582 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts 1583 for (const ObjCMethodList *Method = &Methods.Instance; Method; 1584 Method = Method->Next) 1585 if (Method->Method) 1586 DataLen += 4; 1587 for (const ObjCMethodList *Method = &Methods.Factory; Method; 1588 Method = Method->Next) 1589 if (Method->Method) 1590 DataLen += 4; 1591 clang::io::Emit16(Out, DataLen); 1592 return std::make_pair(KeyLen, DataLen); 1593 } 1594 1595 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) { 1596 uint64_t Start = Out.tell(); 1597 assert((Start >> 32) == 0 && "Selector key offset too large"); 1598 Writer.SetSelectorOffset(Sel, Start); 1599 unsigned N = Sel.getNumArgs(); 1600 clang::io::Emit16(Out, N); 1601 if (N == 0) 1602 N = 1; 1603 for (unsigned I = 0; I != N; ++I) 1604 clang::io::Emit32(Out, 1605 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); 1606 } 1607 1608 void EmitData(llvm::raw_ostream& Out, key_type_ref, 1609 data_type_ref Methods, unsigned DataLen) { 1610 uint64_t Start = Out.tell(); (void)Start; 1611 clang::io::Emit32(Out, Methods.ID); 1612 unsigned NumInstanceMethods = 0; 1613 for (const ObjCMethodList *Method = &Methods.Instance; Method; 1614 Method = Method->Next) 1615 if (Method->Method) 1616 ++NumInstanceMethods; 1617 1618 unsigned NumFactoryMethods = 0; 1619 for (const ObjCMethodList *Method = &Methods.Factory; Method; 1620 Method = Method->Next) 1621 if (Method->Method) 1622 ++NumFactoryMethods; 1623 1624 clang::io::Emit16(Out, NumInstanceMethods); 1625 clang::io::Emit16(Out, NumFactoryMethods); 1626 for (const ObjCMethodList *Method = &Methods.Instance; Method; 1627 Method = Method->Next) 1628 if (Method->Method) 1629 clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); 1630 for (const ObjCMethodList *Method = &Methods.Factory; Method; 1631 Method = Method->Next) 1632 if (Method->Method) 1633 clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); 1634 1635 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 1636 } 1637 }; 1638 } // end anonymous namespace 1639 1640 /// \brief Write ObjC data: selectors and the method pool. 1641 /// 1642 /// The method pool contains both instance and factory methods, stored 1643 /// in an on-disk hash table indexed by the selector. The hash table also 1644 /// contains an empty entry for every other selector known to Sema. 1645 void ASTWriter::WriteSelectors(Sema &SemaRef) { 1646 using namespace llvm; 1647 1648 // Do we have to do anything at all? 1649 if (SemaRef.MethodPool.empty() && SelectorIDs.empty()) 1650 return; 1651 unsigned NumTableEntries = 0; 1652 // Create and write out the blob that contains selectors and the method pool. 1653 { 1654 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator; 1655 ASTMethodPoolTrait Trait(*this); 1656 1657 // Create the on-disk hash table representation. We walk through every 1658 // selector we've seen and look it up in the method pool. 1659 SelectorOffsets.resize(NextSelectorID - FirstSelectorID); 1660 for (llvm::DenseMap<Selector, SelectorID>::iterator 1661 I = SelectorIDs.begin(), E = SelectorIDs.end(); 1662 I != E; ++I) { 1663 Selector S = I->first; 1664 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S); 1665 ASTMethodPoolTrait::data_type Data = { 1666 I->second, 1667 ObjCMethodList(), 1668 ObjCMethodList() 1669 }; 1670 if (F != SemaRef.MethodPool.end()) { 1671 Data.Instance = F->second.first; 1672 Data.Factory = F->second.second; 1673 } 1674 // Only write this selector if it's not in an existing AST or something 1675 // changed. 1676 if (Chain && I->second < FirstSelectorID) { 1677 // Selector already exists. Did it change? 1678 bool changed = false; 1679 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method; 1680 M = M->Next) { 1681 if (M->Method->getPCHLevel() == 0) 1682 changed = true; 1683 } 1684 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method; 1685 M = M->Next) { 1686 if (M->Method->getPCHLevel() == 0) 1687 changed = true; 1688 } 1689 if (!changed) 1690 continue; 1691 } else if (Data.Instance.Method || Data.Factory.Method) { 1692 // A new method pool entry. 1693 ++NumTableEntries; 1694 } 1695 Generator.insert(S, Data, Trait); 1696 } 1697 1698 // Create the on-disk hash table in a buffer. 1699 llvm::SmallString<4096> MethodPool; 1700 uint32_t BucketOffset; 1701 { 1702 ASTMethodPoolTrait Trait(*this); 1703 llvm::raw_svector_ostream Out(MethodPool); 1704 // Make sure that no bucket is at offset 0 1705 clang::io::Emit32(Out, 0); 1706 BucketOffset = Generator.Emit(Out, Trait); 1707 } 1708 1709 // Create a blob abbreviation 1710 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1711 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL)); 1712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1715 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); 1716 1717 // Write the method pool 1718 RecordData Record; 1719 Record.push_back(METHOD_POOL); 1720 Record.push_back(BucketOffset); 1721 Record.push_back(NumTableEntries); 1722 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str()); 1723 1724 // Create a blob abbreviation for the selector table offsets. 1725 Abbrev = new BitCodeAbbrev(); 1726 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS)); 1727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index 1728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1729 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1730 1731 // Write the selector offsets table. 1732 Record.clear(); 1733 Record.push_back(SELECTOR_OFFSETS); 1734 Record.push_back(SelectorOffsets.size()); 1735 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, 1736 (const char *)data(SelectorOffsets), 1737 SelectorOffsets.size() * 4); 1738 } 1739 } 1740 1741 /// \brief Write the selectors referenced in @selector expression into AST file. 1742 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) { 1743 using namespace llvm; 1744 if (SemaRef.ReferencedSelectors.empty()) 1745 return; 1746 1747 RecordData Record; 1748 1749 // Note: this writes out all references even for a dependent AST. But it is 1750 // very tricky to fix, and given that @selector shouldn't really appear in 1751 // headers, probably not worth it. It's not a correctness issue. 1752 for (DenseMap<Selector, SourceLocation>::iterator S = 1753 SemaRef.ReferencedSelectors.begin(), 1754 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) { 1755 Selector Sel = (*S).first; 1756 SourceLocation Loc = (*S).second; 1757 AddSelectorRef(Sel, Record); 1758 AddSourceLocation(Loc, Record); 1759 } 1760 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record); 1761 } 1762 1763 //===----------------------------------------------------------------------===// 1764 // Identifier Table Serialization 1765 //===----------------------------------------------------------------------===// 1766 1767 namespace { 1768 class ASTIdentifierTableTrait { 1769 ASTWriter &Writer; 1770 Preprocessor &PP; 1771 1772 /// \brief Determines whether this is an "interesting" identifier 1773 /// that needs a full IdentifierInfo structure written into the hash 1774 /// table. 1775 static bool isInterestingIdentifier(const IdentifierInfo *II) { 1776 return II->isPoisoned() || 1777 II->isExtensionToken() || 1778 II->hasMacroDefinition() || 1779 II->getObjCOrBuiltinID() || 1780 II->getFETokenInfo<void>(); 1781 } 1782 1783 public: 1784 typedef const IdentifierInfo* key_type; 1785 typedef key_type key_type_ref; 1786 1787 typedef IdentID data_type; 1788 typedef data_type data_type_ref; 1789 1790 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP) 1791 : Writer(Writer), PP(PP) { } 1792 1793 static unsigned ComputeHash(const IdentifierInfo* II) { 1794 return llvm::HashString(II->getName()); 1795 } 1796 1797 std::pair<unsigned,unsigned> 1798 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II, 1799 IdentID ID) { 1800 unsigned KeyLen = II->getLength() + 1; 1801 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 1802 if (isInterestingIdentifier(II)) { 1803 DataLen += 2; // 2 bytes for builtin ID, flags 1804 if (II->hasMacroDefinition() && 1805 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro()) 1806 DataLen += 4; 1807 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II), 1808 DEnd = IdentifierResolver::end(); 1809 D != DEnd; ++D) 1810 DataLen += sizeof(DeclID); 1811 } 1812 clang::io::Emit16(Out, DataLen); 1813 // We emit the key length after the data length so that every 1814 // string is preceded by a 16-bit length. This matches the PTH 1815 // format for storing identifiers. 1816 clang::io::Emit16(Out, KeyLen); 1817 return std::make_pair(KeyLen, DataLen); 1818 } 1819 1820 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II, 1821 unsigned KeyLen) { 1822 // Record the location of the key data. This is used when generating 1823 // the mapping from persistent IDs to strings. 1824 Writer.SetIdentifierOffset(II, Out.tell()); 1825 Out.write(II->getNameStart(), KeyLen); 1826 } 1827 1828 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II, 1829 IdentID ID, unsigned) { 1830 if (!isInterestingIdentifier(II)) { 1831 clang::io::Emit32(Out, ID << 1); 1832 return; 1833 } 1834 1835 clang::io::Emit32(Out, (ID << 1) | 0x01); 1836 uint32_t Bits = 0; 1837 bool hasMacroDefinition = 1838 II->hasMacroDefinition() && 1839 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro(); 1840 Bits = (uint32_t)II->getObjCOrBuiltinID(); 1841 Bits = (Bits << 1) | unsigned(hasMacroDefinition); 1842 Bits = (Bits << 1) | unsigned(II->isExtensionToken()); 1843 Bits = (Bits << 1) | unsigned(II->isPoisoned()); 1844 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); 1845 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); 1846 clang::io::Emit16(Out, Bits); 1847 1848 if (hasMacroDefinition) 1849 clang::io::Emit32(Out, Writer.getMacroOffset(II)); 1850 1851 // Emit the declaration IDs in reverse order, because the 1852 // IdentifierResolver provides the declarations as they would be 1853 // visible (e.g., the function "stat" would come before the struct 1854 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain() 1855 // adds declarations to the end of the list (so we need to see the 1856 // struct "status" before the function "status"). 1857 // Only emit declarations that aren't from a chained PCH, though. 1858 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II), 1859 IdentifierResolver::end()); 1860 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(), 1861 DEnd = Decls.rend(); 1862 D != DEnd; ++D) 1863 clang::io::Emit32(Out, Writer.getDeclID(*D)); 1864 } 1865 }; 1866 } // end anonymous namespace 1867 1868 /// \brief Write the identifier table into the AST file. 1869 /// 1870 /// The identifier table consists of a blob containing string data 1871 /// (the actual identifiers themselves) and a separate "offsets" index 1872 /// that maps identifier IDs to locations within the blob. 1873 void ASTWriter::WriteIdentifierTable(Preprocessor &PP) { 1874 using namespace llvm; 1875 1876 // Create and write out the blob that contains the identifier 1877 // strings. 1878 { 1879 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator; 1880 ASTIdentifierTableTrait Trait(*this, PP); 1881 1882 // Look for any identifiers that were named while processing the 1883 // headers, but are otherwise not needed. We add these to the hash 1884 // table to enable checking of the predefines buffer in the case 1885 // where the user adds new macro definitions when building the AST 1886 // file. 1887 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), 1888 IDEnd = PP.getIdentifierTable().end(); 1889 ID != IDEnd; ++ID) 1890 getIdentifierRef(ID->second); 1891 1892 // Create the on-disk hash table representation. We only store offsets 1893 // for identifiers that appear here for the first time. 1894 IdentifierOffsets.resize(NextIdentID - FirstIdentID); 1895 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator 1896 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); 1897 ID != IDEnd; ++ID) { 1898 assert(ID->first && "NULL identifier in identifier table"); 1899 if (!Chain || !ID->first->isFromAST()) 1900 Generator.insert(ID->first, ID->second, Trait); 1901 } 1902 1903 // Create the on-disk hash table in a buffer. 1904 llvm::SmallString<4096> IdentifierTable; 1905 uint32_t BucketOffset; 1906 { 1907 ASTIdentifierTableTrait Trait(*this, PP); 1908 llvm::raw_svector_ostream Out(IdentifierTable); 1909 // Make sure that no bucket is at offset 0 1910 clang::io::Emit32(Out, 0); 1911 BucketOffset = Generator.Emit(Out, Trait); 1912 } 1913 1914 // Create a blob abbreviation 1915 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1916 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE)); 1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1919 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); 1920 1921 // Write the identifier table 1922 RecordData Record; 1923 Record.push_back(IDENTIFIER_TABLE); 1924 Record.push_back(BucketOffset); 1925 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str()); 1926 } 1927 1928 // Write the offsets table for identifier IDs. 1929 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1930 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET)); 1931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers 1932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1933 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1934 1935 RecordData Record; 1936 Record.push_back(IDENTIFIER_OFFSET); 1937 Record.push_back(IdentifierOffsets.size()); 1938 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, 1939 (const char *)data(IdentifierOffsets), 1940 IdentifierOffsets.size() * sizeof(uint32_t)); 1941 } 1942 1943 //===----------------------------------------------------------------------===// 1944 // DeclContext's Name Lookup Table Serialization 1945 //===----------------------------------------------------------------------===// 1946 1947 namespace { 1948 // Trait used for the on-disk hash table used in the method pool. 1949 class ASTDeclContextNameLookupTrait { 1950 ASTWriter &Writer; 1951 1952 public: 1953 typedef DeclarationName key_type; 1954 typedef key_type key_type_ref; 1955 1956 typedef DeclContext::lookup_result data_type; 1957 typedef const data_type& data_type_ref; 1958 1959 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { } 1960 1961 unsigned ComputeHash(DeclarationName Name) { 1962 llvm::FoldingSetNodeID ID; 1963 ID.AddInteger(Name.getNameKind()); 1964 1965 switch (Name.getNameKind()) { 1966 case DeclarationName::Identifier: 1967 ID.AddString(Name.getAsIdentifierInfo()->getName()); 1968 break; 1969 case DeclarationName::ObjCZeroArgSelector: 1970 case DeclarationName::ObjCOneArgSelector: 1971 case DeclarationName::ObjCMultiArgSelector: 1972 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector())); 1973 break; 1974 case DeclarationName::CXXConstructorName: 1975 case DeclarationName::CXXDestructorName: 1976 case DeclarationName::CXXConversionFunctionName: 1977 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType())); 1978 break; 1979 case DeclarationName::CXXOperatorName: 1980 ID.AddInteger(Name.getCXXOverloadedOperator()); 1981 break; 1982 case DeclarationName::CXXLiteralOperatorName: 1983 ID.AddString(Name.getCXXLiteralIdentifier()->getName()); 1984 case DeclarationName::CXXUsingDirective: 1985 break; 1986 } 1987 1988 return ID.ComputeHash(); 1989 } 1990 1991 std::pair<unsigned,unsigned> 1992 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name, 1993 data_type_ref Lookup) { 1994 unsigned KeyLen = 1; 1995 switch (Name.getNameKind()) { 1996 case DeclarationName::Identifier: 1997 case DeclarationName::ObjCZeroArgSelector: 1998 case DeclarationName::ObjCOneArgSelector: 1999 case DeclarationName::ObjCMultiArgSelector: 2000 case DeclarationName::CXXConstructorName: 2001 case DeclarationName::CXXDestructorName: 2002 case DeclarationName::CXXConversionFunctionName: 2003 case DeclarationName::CXXLiteralOperatorName: 2004 KeyLen += 4; 2005 break; 2006 case DeclarationName::CXXOperatorName: 2007 KeyLen += 1; 2008 break; 2009 case DeclarationName::CXXUsingDirective: 2010 break; 2011 } 2012 clang::io::Emit16(Out, KeyLen); 2013 2014 // 2 bytes for num of decls and 4 for each DeclID. 2015 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first); 2016 clang::io::Emit16(Out, DataLen); 2017 2018 return std::make_pair(KeyLen, DataLen); 2019 } 2020 2021 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) { 2022 using namespace clang::io; 2023 2024 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?"); 2025 Emit8(Out, Name.getNameKind()); 2026 switch (Name.getNameKind()) { 2027 case DeclarationName::Identifier: 2028 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo())); 2029 break; 2030 case DeclarationName::ObjCZeroArgSelector: 2031 case DeclarationName::ObjCOneArgSelector: 2032 case DeclarationName::ObjCMultiArgSelector: 2033 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector())); 2034 break; 2035 case DeclarationName::CXXConstructorName: 2036 case DeclarationName::CXXDestructorName: 2037 case DeclarationName::CXXConversionFunctionName: 2038 Emit32(Out, Writer.getTypeID(Name.getCXXNameType())); 2039 break; 2040 case DeclarationName::CXXOperatorName: 2041 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?"); 2042 Emit8(Out, Name.getCXXOverloadedOperator()); 2043 break; 2044 case DeclarationName::CXXLiteralOperatorName: 2045 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier())); 2046 break; 2047 case DeclarationName::CXXUsingDirective: 2048 break; 2049 } 2050 } 2051 2052 void EmitData(llvm::raw_ostream& Out, key_type_ref, 2053 data_type Lookup, unsigned DataLen) { 2054 uint64_t Start = Out.tell(); (void)Start; 2055 clang::io::Emit16(Out, Lookup.second - Lookup.first); 2056 for (; Lookup.first != Lookup.second; ++Lookup.first) 2057 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first)); 2058 2059 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 2060 } 2061 }; 2062 } // end anonymous namespace 2063 2064 /// \brief Write the block containing all of the declaration IDs 2065 /// visible from the given DeclContext. 2066 /// 2067 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the 2068 /// bitstream, or 0 if no block was written. 2069 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context, 2070 DeclContext *DC) { 2071 if (DC->getPrimaryContext() != DC) 2072 return 0; 2073 2074 // Since there is no name lookup into functions or methods, don't bother to 2075 // build a visible-declarations table for these entities. 2076 if (DC->isFunctionOrMethod()) 2077 return 0; 2078 2079 // If not in C++, we perform name lookup for the translation unit via the 2080 // IdentifierInfo chains, don't bother to build a visible-declarations table. 2081 // FIXME: In C++ we need the visible declarations in order to "see" the 2082 // friend declarations, is there a way to do this without writing the table ? 2083 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus) 2084 return 0; 2085 2086 // Force the DeclContext to build a its name-lookup table. 2087 if (DC->hasExternalVisibleStorage()) 2088 DC->MaterializeVisibleDeclsFromExternalStorage(); 2089 else 2090 DC->lookup(DeclarationName()); 2091 2092 // Serialize the contents of the mapping used for lookup. Note that, 2093 // although we have two very different code paths, the serialized 2094 // representation is the same for both cases: a declaration name, 2095 // followed by a size, followed by references to the visible 2096 // declarations that have that name. 2097 uint64_t Offset = Stream.GetCurrentBitNo(); 2098 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); 2099 if (!Map || Map->empty()) 2100 return 0; 2101 2102 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; 2103 ASTDeclContextNameLookupTrait Trait(*this); 2104 2105 // Create the on-disk hash table representation. 2106 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 2107 D != DEnd; ++D) { 2108 DeclarationName Name = D->first; 2109 DeclContext::lookup_result Result = D->second.getLookupResult(); 2110 Generator.insert(Name, Result, Trait); 2111 } 2112 2113 // Create the on-disk hash table in a buffer. 2114 llvm::SmallString<4096> LookupTable; 2115 uint32_t BucketOffset; 2116 { 2117 llvm::raw_svector_ostream Out(LookupTable); 2118 // Make sure that no bucket is at offset 0 2119 clang::io::Emit32(Out, 0); 2120 BucketOffset = Generator.Emit(Out, Trait); 2121 } 2122 2123 // Write the lookup table 2124 RecordData Record; 2125 Record.push_back(DECL_CONTEXT_VISIBLE); 2126 Record.push_back(BucketOffset); 2127 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record, 2128 LookupTable.str()); 2129 2130 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record); 2131 ++NumVisibleDeclContexts; 2132 return Offset; 2133 } 2134 2135 /// \brief Write an UPDATE_VISIBLE block for the given context. 2136 /// 2137 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing 2138 /// DeclContext in a dependent AST file. As such, they only exist for the TU 2139 /// (in C++) and for namespaces. 2140 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { 2141 assert((DC->isTranslationUnit() || DC->isNamespace()) && 2142 "Only TU and namespaces should have visible decl updates."); 2143 2144 // Make the context build its lookup table, but don't make it load external 2145 // decls. 2146 DC->lookup(DeclarationName()); 2147 2148 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); 2149 if (!Map || Map->empty()) 2150 return; 2151 2152 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; 2153 ASTDeclContextNameLookupTrait Trait(*this); 2154 2155 // Create the hash table. 2156 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 2157 D != DEnd; ++D) { 2158 DeclarationName Name = D->first; 2159 DeclContext::lookup_result Result = D->second.getLookupResult(); 2160 // For any name that appears in this table, the results are complete, i.e. 2161 // they overwrite results from previous PCHs. Merging is always a mess. 2162 Generator.insert(Name, Result, Trait); 2163 } 2164 2165 // Create the on-disk hash table in a buffer. 2166 llvm::SmallString<4096> LookupTable; 2167 uint32_t BucketOffset; 2168 { 2169 llvm::raw_svector_ostream Out(LookupTable); 2170 // Make sure that no bucket is at offset 0 2171 clang::io::Emit32(Out, 0); 2172 BucketOffset = Generator.Emit(Out, Trait); 2173 } 2174 2175 // Write the lookup table 2176 RecordData Record; 2177 Record.push_back(UPDATE_VISIBLE); 2178 Record.push_back(getDeclID(cast<Decl>(DC))); 2179 Record.push_back(BucketOffset); 2180 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str()); 2181 } 2182 2183 /// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates 2184 /// that have new specializations in the current AST file. 2185 void ASTWriter::WriteAdditionalTemplateSpecializations() { 2186 RecordData Record; 2187 for (AdditionalTemplateSpecializationsMap::iterator 2188 I = AdditionalTemplateSpecializations.begin(), 2189 E = AdditionalTemplateSpecializations.end(); 2190 I != E; ++I) { 2191 Record.clear(); 2192 Record.push_back(I->first); 2193 Record.insert(Record.end(), I->second.begin(), I->second.end()); 2194 Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record); 2195 } 2196 } 2197 2198 //===----------------------------------------------------------------------===// 2199 // General Serialization Routines 2200 //===----------------------------------------------------------------------===// 2201 2202 /// \brief Write a record containing the given attributes. 2203 void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordData &Record) { 2204 Record.push_back(Attrs.size()); 2205 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){ 2206 const Attr * A = *i; 2207 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs 2208 AddSourceLocation(A->getLocation(), Record); 2209 Record.push_back(A->isInherited()); 2210 2211 #include "clang/Serialization/AttrPCHWrite.inc" 2212 2213 } 2214 } 2215 2216 void ASTWriter::AddString(llvm::StringRef Str, RecordData &Record) { 2217 Record.push_back(Str.size()); 2218 Record.insert(Record.end(), Str.begin(), Str.end()); 2219 } 2220 2221 /// \brief Note that the identifier II occurs at the given offset 2222 /// within the identifier table. 2223 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { 2224 IdentID ID = IdentifierIDs[II]; 2225 // Only store offsets new to this AST file. Other identifier names are looked 2226 // up earlier in the chain and thus don't need an offset. 2227 if (ID >= FirstIdentID) 2228 IdentifierOffsets[ID - FirstIdentID] = Offset; 2229 } 2230 2231 /// \brief Note that the selector Sel occurs at the given offset 2232 /// within the method pool/selector table. 2233 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { 2234 unsigned ID = SelectorIDs[Sel]; 2235 assert(ID && "Unknown selector"); 2236 // Don't record offsets for selectors that are also available in a different 2237 // file. 2238 if (ID < FirstSelectorID) 2239 return; 2240 SelectorOffsets[ID - FirstSelectorID] = Offset; 2241 } 2242 2243 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream) 2244 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID), 2245 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID), 2246 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1), 2247 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID), 2248 CollectedStmts(&StmtsToEmit), 2249 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0), 2250 NumVisibleDeclContexts(0) { 2251 } 2252 2253 void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls, 2254 const char *isysroot) { 2255 // Emit the file header. 2256 Stream.Emit((unsigned)'C', 8); 2257 Stream.Emit((unsigned)'P', 8); 2258 Stream.Emit((unsigned)'C', 8); 2259 Stream.Emit((unsigned)'H', 8); 2260 2261 WriteBlockInfoBlock(); 2262 2263 if (Chain) 2264 WriteASTChain(SemaRef, StatCalls, isysroot); 2265 else 2266 WriteASTCore(SemaRef, StatCalls, isysroot); 2267 } 2268 2269 void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, 2270 const char *isysroot) { 2271 using namespace llvm; 2272 2273 ASTContext &Context = SemaRef.Context; 2274 Preprocessor &PP = SemaRef.PP; 2275 2276 // The translation unit is the first declaration we'll emit. 2277 DeclIDs[Context.getTranslationUnitDecl()] = 1; 2278 ++NextDeclID; 2279 DeclTypesToEmit.push(Context.getTranslationUnitDecl()); 2280 2281 // Make sure that we emit IdentifierInfos (and any attached 2282 // declarations) for builtins. 2283 { 2284 IdentifierTable &Table = PP.getIdentifierTable(); 2285 llvm::SmallVector<const char *, 32> BuiltinNames; 2286 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames, 2287 Context.getLangOptions().NoBuiltin); 2288 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I) 2289 getIdentifierRef(&Table.get(BuiltinNames[I])); 2290 } 2291 2292 // Build a record containing all of the tentative definitions in this file, in 2293 // TentativeDefinitions order. Generally, this record will be empty for 2294 // headers. 2295 RecordData TentativeDefinitions; 2296 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) { 2297 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions); 2298 } 2299 2300 // Build a record containing all of the file scoped decls in this file. 2301 RecordData UnusedFileScopedDecls; 2302 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) 2303 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls); 2304 2305 RecordData WeakUndeclaredIdentifiers; 2306 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { 2307 WeakUndeclaredIdentifiers.push_back( 2308 SemaRef.WeakUndeclaredIdentifiers.size()); 2309 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator 2310 I = SemaRef.WeakUndeclaredIdentifiers.begin(), 2311 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { 2312 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); 2313 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); 2314 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); 2315 WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); 2316 } 2317 } 2318 2319 // Build a record containing all of the locally-scoped external 2320 // declarations in this header file. Generally, this record will be 2321 // empty. 2322 RecordData LocallyScopedExternalDecls; 2323 // FIXME: This is filling in the AST file in densemap order which is 2324 // nondeterminstic! 2325 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator 2326 TD = SemaRef.LocallyScopedExternalDecls.begin(), 2327 TDEnd = SemaRef.LocallyScopedExternalDecls.end(); 2328 TD != TDEnd; ++TD) 2329 AddDeclRef(TD->second, LocallyScopedExternalDecls); 2330 2331 // Build a record containing all of the ext_vector declarations. 2332 RecordData ExtVectorDecls; 2333 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) 2334 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls); 2335 2336 // Build a record containing all of the VTable uses information. 2337 RecordData VTableUses; 2338 if (!SemaRef.VTableUses.empty()) { 2339 VTableUses.push_back(SemaRef.VTableUses.size()); 2340 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { 2341 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); 2342 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); 2343 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); 2344 } 2345 } 2346 2347 // Build a record containing all of dynamic classes declarations. 2348 RecordData DynamicClasses; 2349 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I) 2350 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses); 2351 2352 // Build a record containing all of pending implicit instantiations. 2353 RecordData PendingInstantiations; 2354 for (std::deque<Sema::PendingImplicitInstantiation>::iterator 2355 I = SemaRef.PendingInstantiations.begin(), 2356 N = SemaRef.PendingInstantiations.end(); I != N; ++I) { 2357 AddDeclRef(I->first, PendingInstantiations); 2358 AddSourceLocation(I->second, PendingInstantiations); 2359 } 2360 assert(SemaRef.PendingLocalImplicitInstantiations.empty() && 2361 "There are local ones at end of translation unit!"); 2362 2363 // Build a record containing some declaration references. 2364 RecordData SemaDeclRefs; 2365 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { 2366 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); 2367 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); 2368 } 2369 2370 // Write the remaining AST contents. 2371 RecordData Record; 2372 Stream.EnterSubblock(AST_BLOCK_ID, 5); 2373 WriteMetadata(Context, isysroot); 2374 WriteLanguageOptions(Context.getLangOptions()); 2375 if (StatCalls && !isysroot) 2376 WriteStatCache(*StatCalls); 2377 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); 2378 // Write the record of special types. 2379 Record.clear(); 2380 2381 AddTypeRef(Context.getBuiltinVaListType(), Record); 2382 AddTypeRef(Context.getObjCIdType(), Record); 2383 AddTypeRef(Context.getObjCSelType(), Record); 2384 AddTypeRef(Context.getObjCProtoType(), Record); 2385 AddTypeRef(Context.getObjCClassType(), Record); 2386 AddTypeRef(Context.getRawCFConstantStringType(), Record); 2387 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record); 2388 AddTypeRef(Context.getFILEType(), Record); 2389 AddTypeRef(Context.getjmp_bufType(), Record); 2390 AddTypeRef(Context.getsigjmp_bufType(), Record); 2391 AddTypeRef(Context.ObjCIdRedefinitionType, Record); 2392 AddTypeRef(Context.ObjCClassRedefinitionType, Record); 2393 AddTypeRef(Context.getRawBlockdescriptorType(), Record); 2394 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record); 2395 AddTypeRef(Context.ObjCSelRedefinitionType, Record); 2396 AddTypeRef(Context.getRawNSConstantStringType(), Record); 2397 Record.push_back(Context.isInt128Installed()); 2398 Stream.EmitRecord(SPECIAL_TYPES, Record); 2399 2400 // Keep writing types and declarations until all types and 2401 // declarations have been written. 2402 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3); 2403 WriteDeclsBlockAbbrevs(); 2404 while (!DeclTypesToEmit.empty()) { 2405 DeclOrType DOT = DeclTypesToEmit.front(); 2406 DeclTypesToEmit.pop(); 2407 if (DOT.isType()) 2408 WriteType(DOT.getType()); 2409 else 2410 WriteDecl(Context, DOT.getDecl()); 2411 } 2412 Stream.ExitBlock(); 2413 2414 WritePreprocessor(PP); 2415 WriteSelectors(SemaRef); 2416 WriteReferencedSelectorsPool(SemaRef); 2417 WriteIdentifierTable(PP); 2418 2419 WriteTypeDeclOffsets(); 2420 2421 // Write the record containing external, unnamed definitions. 2422 if (!ExternalDefinitions.empty()) 2423 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); 2424 2425 // Write the record containing tentative definitions. 2426 if (!TentativeDefinitions.empty()) 2427 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); 2428 2429 // Write the record containing unused file scoped decls. 2430 if (!UnusedFileScopedDecls.empty()) 2431 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); 2432 2433 // Write the record containing weak undeclared identifiers. 2434 if (!WeakUndeclaredIdentifiers.empty()) 2435 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, 2436 WeakUndeclaredIdentifiers); 2437 2438 // Write the record containing locally-scoped external definitions. 2439 if (!LocallyScopedExternalDecls.empty()) 2440 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, 2441 LocallyScopedExternalDecls); 2442 2443 // Write the record containing ext_vector type names. 2444 if (!ExtVectorDecls.empty()) 2445 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); 2446 2447 // Write the record containing VTable uses information. 2448 if (!VTableUses.empty()) 2449 Stream.EmitRecord(VTABLE_USES, VTableUses); 2450 2451 // Write the record containing dynamic classes declarations. 2452 if (!DynamicClasses.empty()) 2453 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); 2454 2455 // Write the record containing pending implicit instantiations. 2456 if (!PendingInstantiations.empty()) 2457 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); 2458 2459 // Write the record containing declaration references of Sema. 2460 if (!SemaDeclRefs.empty()) 2461 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); 2462 2463 // Some simple statistics 2464 Record.clear(); 2465 Record.push_back(NumStatements); 2466 Record.push_back(NumMacros); 2467 Record.push_back(NumLexicalDeclContexts); 2468 Record.push_back(NumVisibleDeclContexts); 2469 Stream.EmitRecord(STATISTICS, Record); 2470 Stream.ExitBlock(); 2471 } 2472 2473 void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls, 2474 const char *isysroot) { 2475 using namespace llvm; 2476 2477 FirstDeclID += Chain->getTotalNumDecls(); 2478 FirstTypeID += Chain->getTotalNumTypes(); 2479 FirstIdentID += Chain->getTotalNumIdentifiers(); 2480 FirstSelectorID += Chain->getTotalNumSelectors(); 2481 FirstMacroID += Chain->getTotalNumMacroDefinitions(); 2482 NextDeclID = FirstDeclID; 2483 NextTypeID = FirstTypeID; 2484 NextIdentID = FirstIdentID; 2485 NextSelectorID = FirstSelectorID; 2486 NextMacroID = FirstMacroID; 2487 2488 ASTContext &Context = SemaRef.Context; 2489 Preprocessor &PP = SemaRef.PP; 2490 2491 RecordData Record; 2492 Stream.EnterSubblock(AST_BLOCK_ID, 5); 2493 WriteMetadata(Context, isysroot); 2494 if (StatCalls && !isysroot) 2495 WriteStatCache(*StatCalls); 2496 // FIXME: Source manager block should only write new stuff, which could be 2497 // done by tracking the largest ID in the chain 2498 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); 2499 2500 // The special types are in the chained PCH. 2501 2502 // We don't start with the translation unit, but with its decls that 2503 // don't come from the chained PCH. 2504 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); 2505 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls; 2506 for (DeclContext::decl_iterator I = TU->noload_decls_begin(), 2507 E = TU->noload_decls_end(); 2508 I != E; ++I) { 2509 if ((*I)->getPCHLevel() == 0) 2510 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I))); 2511 else if ((*I)->isChangedSinceDeserialization()) 2512 (void)GetDeclRef(*I); // Make sure it's written, but don't record it. 2513 } 2514 // We also need to write a lexical updates block for the TU. 2515 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev(); 2516 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL)); 2517 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 2518 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); 2519 Record.clear(); 2520 Record.push_back(TU_UPDATE_LEXICAL); 2521 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, 2522 reinterpret_cast<const char*>(NewGlobalDecls.data()), 2523 NewGlobalDecls.size() * sizeof(KindDeclIDPair)); 2524 // And in C++, a visible updates block for the TU. 2525 if (Context.getLangOptions().CPlusPlus) { 2526 Abv = new llvm::BitCodeAbbrev(); 2527 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); 2528 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); 2529 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32)); 2530 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 2531 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv); 2532 WriteDeclContextVisibleUpdate(TU); 2533 } 2534 2535 // Build a record containing all of the new tentative definitions in this 2536 // file, in TentativeDefinitions order. 2537 RecordData TentativeDefinitions; 2538 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) { 2539 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0) 2540 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions); 2541 } 2542 2543 // Build a record containing all of the file scoped decls in this file. 2544 RecordData UnusedFileScopedDecls; 2545 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) { 2546 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0) 2547 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls); 2548 } 2549 2550 // We write the entire table, overwriting the tables from the chain. 2551 RecordData WeakUndeclaredIdentifiers; 2552 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { 2553 WeakUndeclaredIdentifiers.push_back( 2554 SemaRef.WeakUndeclaredIdentifiers.size()); 2555 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator 2556 I = SemaRef.WeakUndeclaredIdentifiers.begin(), 2557 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { 2558 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); 2559 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); 2560 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); 2561 WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); 2562 } 2563 } 2564 2565 // Build a record containing all of the locally-scoped external 2566 // declarations in this header file. Generally, this record will be 2567 // empty. 2568 RecordData LocallyScopedExternalDecls; 2569 // FIXME: This is filling in the AST file in densemap order which is 2570 // nondeterminstic! 2571 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator 2572 TD = SemaRef.LocallyScopedExternalDecls.begin(), 2573 TDEnd = SemaRef.LocallyScopedExternalDecls.end(); 2574 TD != TDEnd; ++TD) { 2575 if (TD->second->getPCHLevel() == 0) 2576 AddDeclRef(TD->second, LocallyScopedExternalDecls); 2577 } 2578 2579 // Build a record containing all of the ext_vector declarations. 2580 RecordData ExtVectorDecls; 2581 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) { 2582 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0) 2583 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls); 2584 } 2585 2586 // Build a record containing all of the VTable uses information. 2587 // We write everything here, because it's too hard to determine whether 2588 // a use is new to this part. 2589 RecordData VTableUses; 2590 if (!SemaRef.VTableUses.empty()) { 2591 VTableUses.push_back(SemaRef.VTableUses.size()); 2592 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { 2593 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); 2594 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); 2595 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); 2596 } 2597 } 2598 2599 // Build a record containing all of dynamic classes declarations. 2600 RecordData DynamicClasses; 2601 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I) 2602 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0) 2603 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses); 2604 2605 // Build a record containing all of pending implicit instantiations. 2606 RecordData PendingInstantiations; 2607 for (std::deque<Sema::PendingImplicitInstantiation>::iterator 2608 I = SemaRef.PendingInstantiations.begin(), 2609 N = SemaRef.PendingInstantiations.end(); I != N; ++I) { 2610 if (I->first->getPCHLevel() == 0) { 2611 AddDeclRef(I->first, PendingInstantiations); 2612 AddSourceLocation(I->second, PendingInstantiations); 2613 } 2614 } 2615 assert(SemaRef.PendingLocalImplicitInstantiations.empty() && 2616 "There are local ones at end of translation unit!"); 2617 2618 // Build a record containing some declaration references. 2619 // It's not worth the effort to avoid duplication here. 2620 RecordData SemaDeclRefs; 2621 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { 2622 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); 2623 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); 2624 } 2625 2626 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3); 2627 WriteDeclsBlockAbbrevs(); 2628 while (!DeclTypesToEmit.empty()) { 2629 DeclOrType DOT = DeclTypesToEmit.front(); 2630 DeclTypesToEmit.pop(); 2631 if (DOT.isType()) 2632 WriteType(DOT.getType()); 2633 else 2634 WriteDecl(Context, DOT.getDecl()); 2635 } 2636 Stream.ExitBlock(); 2637 2638 WritePreprocessor(PP); 2639 WriteSelectors(SemaRef); 2640 WriteReferencedSelectorsPool(SemaRef); 2641 WriteIdentifierTable(PP); 2642 WriteTypeDeclOffsets(); 2643 2644 /// Build a record containing first declarations from a chained PCH and the 2645 /// most recent declarations in this AST that they point to. 2646 RecordData FirstLatestDeclIDs; 2647 for (FirstLatestDeclMap::iterator 2648 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) { 2649 assert(I->first->getPCHLevel() > I->second->getPCHLevel() && 2650 "Expected first & second to be in different PCHs"); 2651 AddDeclRef(I->first, FirstLatestDeclIDs); 2652 AddDeclRef(I->second, FirstLatestDeclIDs); 2653 } 2654 if (!FirstLatestDeclIDs.empty()) 2655 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs); 2656 2657 // Write the record containing external, unnamed definitions. 2658 if (!ExternalDefinitions.empty()) 2659 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); 2660 2661 // Write the record containing tentative definitions. 2662 if (!TentativeDefinitions.empty()) 2663 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); 2664 2665 // Write the record containing unused file scoped decls. 2666 if (!UnusedFileScopedDecls.empty()) 2667 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); 2668 2669 // Write the record containing weak undeclared identifiers. 2670 if (!WeakUndeclaredIdentifiers.empty()) 2671 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, 2672 WeakUndeclaredIdentifiers); 2673 2674 // Write the record containing locally-scoped external definitions. 2675 if (!LocallyScopedExternalDecls.empty()) 2676 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, 2677 LocallyScopedExternalDecls); 2678 2679 // Write the record containing ext_vector type names. 2680 if (!ExtVectorDecls.empty()) 2681 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); 2682 2683 // Write the record containing VTable uses information. 2684 if (!VTableUses.empty()) 2685 Stream.EmitRecord(VTABLE_USES, VTableUses); 2686 2687 // Write the record containing dynamic classes declarations. 2688 if (!DynamicClasses.empty()) 2689 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); 2690 2691 // Write the record containing pending implicit instantiations. 2692 if (!PendingInstantiations.empty()) 2693 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); 2694 2695 // Write the record containing declaration references of Sema. 2696 if (!SemaDeclRefs.empty()) 2697 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); 2698 2699 // Write the updates to C++ namespaces. 2700 for (llvm::SmallPtrSet<const NamespaceDecl *, 16>::iterator 2701 I = UpdatedNamespaces.begin(), 2702 E = UpdatedNamespaces.end(); 2703 I != E; ++I) 2704 WriteDeclContextVisibleUpdate(*I); 2705 2706 // Write the updates to C++ template specialization lists. 2707 if (!AdditionalTemplateSpecializations.empty()) 2708 WriteAdditionalTemplateSpecializations(); 2709 2710 Record.clear(); 2711 Record.push_back(NumStatements); 2712 Record.push_back(NumMacros); 2713 Record.push_back(NumLexicalDeclContexts); 2714 Record.push_back(NumVisibleDeclContexts); 2715 WriteDeclUpdateBlock(); 2716 Stream.EmitRecord(STATISTICS, Record); 2717 Stream.ExitBlock(); 2718 } 2719 2720 void ASTWriter::WriteDeclUpdateBlock() { 2721 if (ReplacedDecls.empty()) 2722 return; 2723 2724 RecordData Record; 2725 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator 2726 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) { 2727 Record.push_back(I->first); 2728 Record.push_back(I->second); 2729 } 2730 Stream.EmitRecord(DECL_REPLACEMENTS, Record); 2731 } 2732 2733 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { 2734 Record.push_back(Loc.getRawEncoding()); 2735 } 2736 2737 void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) { 2738 AddSourceLocation(Range.getBegin(), Record); 2739 AddSourceLocation(Range.getEnd(), Record); 2740 } 2741 2742 void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { 2743 Record.push_back(Value.getBitWidth()); 2744 const uint64_t *Words = Value.getRawData(); 2745 Record.append(Words, Words + Value.getNumWords()); 2746 } 2747 2748 void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) { 2749 Record.push_back(Value.isUnsigned()); 2750 AddAPInt(Value, Record); 2751 } 2752 2753 void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) { 2754 AddAPInt(Value.bitcastToAPInt(), Record); 2755 } 2756 2757 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { 2758 Record.push_back(getIdentifierRef(II)); 2759 } 2760 2761 IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { 2762 if (II == 0) 2763 return 0; 2764 2765 IdentID &ID = IdentifierIDs[II]; 2766 if (ID == 0) 2767 ID = NextIdentID++; 2768 return ID; 2769 } 2770 2771 MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) { 2772 if (MD == 0) 2773 return 0; 2774 2775 MacroID &ID = MacroDefinitions[MD]; 2776 if (ID == 0) 2777 ID = NextMacroID++; 2778 return ID; 2779 } 2780 2781 void ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) { 2782 Record.push_back(getSelectorRef(SelRef)); 2783 } 2784 2785 SelectorID ASTWriter::getSelectorRef(Selector Sel) { 2786 if (Sel.getAsOpaquePtr() == 0) { 2787 return 0; 2788 } 2789 2790 SelectorID &SID = SelectorIDs[Sel]; 2791 if (SID == 0 && Chain) { 2792 // This might trigger a ReadSelector callback, which will set the ID for 2793 // this selector. 2794 Chain->LoadSelector(Sel); 2795 } 2796 if (SID == 0) { 2797 SID = NextSelectorID++; 2798 } 2799 return SID; 2800 } 2801 2802 void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) { 2803 AddDeclRef(Temp->getDestructor(), Record); 2804 } 2805 2806 void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, 2807 const TemplateArgumentLocInfo &Arg, 2808 RecordData &Record) { 2809 switch (Kind) { 2810 case TemplateArgument::Expression: 2811 AddStmt(Arg.getAsExpr()); 2812 break; 2813 case TemplateArgument::Type: 2814 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); 2815 break; 2816 case TemplateArgument::Template: 2817 AddSourceRange(Arg.getTemplateQualifierRange(), Record); 2818 AddSourceLocation(Arg.getTemplateNameLoc(), Record); 2819 break; 2820 case TemplateArgument::Null: 2821 case TemplateArgument::Integral: 2822 case TemplateArgument::Declaration: 2823 case TemplateArgument::Pack: 2824 break; 2825 } 2826 } 2827 2828 void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, 2829 RecordData &Record) { 2830 AddTemplateArgument(Arg.getArgument(), Record); 2831 2832 if (Arg.getArgument().getKind() == TemplateArgument::Expression) { 2833 bool InfoHasSameExpr 2834 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); 2835 Record.push_back(InfoHasSameExpr); 2836 if (InfoHasSameExpr) 2837 return; // Avoid storing the same expr twice. 2838 } 2839 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), 2840 Record); 2841 } 2842 2843 void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) { 2844 if (TInfo == 0) { 2845 AddTypeRef(QualType(), Record); 2846 return; 2847 } 2848 2849 AddTypeRef(TInfo->getType(), Record); 2850 TypeLocWriter TLW(*this, Record); 2851 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 2852 TLW.Visit(TL); 2853 } 2854 2855 void ASTWriter::AddTypeRef(QualType T, RecordData &Record) { 2856 Record.push_back(GetOrCreateTypeID(T)); 2857 } 2858 2859 TypeID ASTWriter::GetOrCreateTypeID(QualType T) { 2860 return MakeTypeID(T, 2861 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this)); 2862 } 2863 2864 TypeID ASTWriter::getTypeID(QualType T) const { 2865 return MakeTypeID(T, 2866 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this)); 2867 } 2868 2869 TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) { 2870 if (T.isNull()) 2871 return TypeIdx(); 2872 assert(!T.getLocalFastQualifiers()); 2873 2874 TypeIdx &Idx = TypeIdxs[T]; 2875 if (Idx.getIndex() == 0) { 2876 // We haven't seen this type before. Assign it a new ID and put it 2877 // into the queue of types to emit. 2878 Idx = TypeIdx(NextTypeID++); 2879 DeclTypesToEmit.push(T); 2880 } 2881 return Idx; 2882 } 2883 2884 TypeIdx ASTWriter::getTypeIdx(QualType T) const { 2885 if (T.isNull()) 2886 return TypeIdx(); 2887 assert(!T.getLocalFastQualifiers()); 2888 2889 TypeIdxMap::const_iterator I = TypeIdxs.find(T); 2890 assert(I != TypeIdxs.end() && "Type not emitted!"); 2891 return I->second; 2892 } 2893 2894 void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) { 2895 Record.push_back(GetDeclRef(D)); 2896 } 2897 2898 DeclID ASTWriter::GetDeclRef(const Decl *D) { 2899 if (D == 0) { 2900 return 0; 2901 } 2902 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer"); 2903 DeclID &ID = DeclIDs[D]; 2904 if (ID == 0) { 2905 // We haven't seen this declaration before. Give it a new ID and 2906 // enqueue it in the list of declarations to emit. 2907 ID = NextDeclID++; 2908 DeclTypesToEmit.push(const_cast<Decl *>(D)); 2909 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) { 2910 // We don't add it to the replacement collection here, because we don't 2911 // have the offset yet. 2912 DeclTypesToEmit.push(const_cast<Decl *>(D)); 2913 // Reset the flag, so that we don't add this decl multiple times. 2914 const_cast<Decl *>(D)->setChangedSinceDeserialization(false); 2915 } 2916 2917 return ID; 2918 } 2919 2920 DeclID ASTWriter::getDeclID(const Decl *D) { 2921 if (D == 0) 2922 return 0; 2923 2924 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); 2925 return DeclIDs[D]; 2926 } 2927 2928 void ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { 2929 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. 2930 Record.push_back(Name.getNameKind()); 2931 switch (Name.getNameKind()) { 2932 case DeclarationName::Identifier: 2933 AddIdentifierRef(Name.getAsIdentifierInfo(), Record); 2934 break; 2935 2936 case DeclarationName::ObjCZeroArgSelector: 2937 case DeclarationName::ObjCOneArgSelector: 2938 case DeclarationName::ObjCMultiArgSelector: 2939 AddSelectorRef(Name.getObjCSelector(), Record); 2940 break; 2941 2942 case DeclarationName::CXXConstructorName: 2943 case DeclarationName::CXXDestructorName: 2944 case DeclarationName::CXXConversionFunctionName: 2945 AddTypeRef(Name.getCXXNameType(), Record); 2946 break; 2947 2948 case DeclarationName::CXXOperatorName: 2949 Record.push_back(Name.getCXXOverloadedOperator()); 2950 break; 2951 2952 case DeclarationName::CXXLiteralOperatorName: 2953 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record); 2954 break; 2955 2956 case DeclarationName::CXXUsingDirective: 2957 // No extra data to emit 2958 break; 2959 } 2960 } 2961 2962 void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, 2963 DeclarationName Name, RecordData &Record) { 2964 switch (Name.getNameKind()) { 2965 case DeclarationName::CXXConstructorName: 2966 case DeclarationName::CXXDestructorName: 2967 case DeclarationName::CXXConversionFunctionName: 2968 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record); 2969 break; 2970 2971 case DeclarationName::CXXOperatorName: 2972 AddSourceLocation( 2973 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc), 2974 Record); 2975 AddSourceLocation( 2976 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc), 2977 Record); 2978 break; 2979 2980 case DeclarationName::CXXLiteralOperatorName: 2981 AddSourceLocation( 2982 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc), 2983 Record); 2984 break; 2985 2986 case DeclarationName::Identifier: 2987 case DeclarationName::ObjCZeroArgSelector: 2988 case DeclarationName::ObjCOneArgSelector: 2989 case DeclarationName::ObjCMultiArgSelector: 2990 case DeclarationName::CXXUsingDirective: 2991 break; 2992 } 2993 } 2994 2995 void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, 2996 RecordData &Record) { 2997 AddDeclarationName(NameInfo.getName(), Record); 2998 AddSourceLocation(NameInfo.getLoc(), Record); 2999 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record); 3000 } 3001 3002 void ASTWriter::AddQualifierInfo(const QualifierInfo &Info, 3003 RecordData &Record) { 3004 AddNestedNameSpecifier(Info.NNS, Record); 3005 AddSourceRange(Info.NNSRange, Record); 3006 Record.push_back(Info.NumTemplParamLists); 3007 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i) 3008 AddTemplateParameterList(Info.TemplParamLists[i], Record); 3009 } 3010 3011 void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, 3012 RecordData &Record) { 3013 // Nested name specifiers usually aren't too long. I think that 8 would 3014 // typically accomodate the vast majority. 3015 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames; 3016 3017 // Push each of the NNS's onto a stack for serialization in reverse order. 3018 while (NNS) { 3019 NestedNames.push_back(NNS); 3020 NNS = NNS->getPrefix(); 3021 } 3022 3023 Record.push_back(NestedNames.size()); 3024 while(!NestedNames.empty()) { 3025 NNS = NestedNames.pop_back_val(); 3026 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); 3027 Record.push_back(Kind); 3028 switch (Kind) { 3029 case NestedNameSpecifier::Identifier: 3030 AddIdentifierRef(NNS->getAsIdentifier(), Record); 3031 break; 3032 3033 case NestedNameSpecifier::Namespace: 3034 AddDeclRef(NNS->getAsNamespace(), Record); 3035 break; 3036 3037 case NestedNameSpecifier::TypeSpec: 3038 case NestedNameSpecifier::TypeSpecWithTemplate: 3039 AddTypeRef(QualType(NNS->getAsType(), 0), Record); 3040 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); 3041 break; 3042 3043 case NestedNameSpecifier::Global: 3044 // Don't need to write an associated value. 3045 break; 3046 } 3047 } 3048 } 3049 3050 void ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) { 3051 TemplateName::NameKind Kind = Name.getKind(); 3052 Record.push_back(Kind); 3053 switch (Kind) { 3054 case TemplateName::Template: 3055 AddDeclRef(Name.getAsTemplateDecl(), Record); 3056 break; 3057 3058 case TemplateName::OverloadedTemplate: { 3059 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); 3060 Record.push_back(OvT->size()); 3061 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end(); 3062 I != E; ++I) 3063 AddDeclRef(*I, Record); 3064 break; 3065 } 3066 3067 case TemplateName::QualifiedTemplate: { 3068 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); 3069 AddNestedNameSpecifier(QualT->getQualifier(), Record); 3070 Record.push_back(QualT->hasTemplateKeyword()); 3071 AddDeclRef(QualT->getTemplateDecl(), Record); 3072 break; 3073 } 3074 3075 case TemplateName::DependentTemplate: { 3076 DependentTemplateName *DepT = Name.getAsDependentTemplateName(); 3077 AddNestedNameSpecifier(DepT->getQualifier(), Record); 3078 Record.push_back(DepT->isIdentifier()); 3079 if (DepT->isIdentifier()) 3080 AddIdentifierRef(DepT->getIdentifier(), Record); 3081 else 3082 Record.push_back(DepT->getOperator()); 3083 break; 3084 } 3085 } 3086 } 3087 3088 void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg, 3089 RecordData &Record) { 3090 Record.push_back(Arg.getKind()); 3091 switch (Arg.getKind()) { 3092 case TemplateArgument::Null: 3093 break; 3094 case TemplateArgument::Type: 3095 AddTypeRef(Arg.getAsType(), Record); 3096 break; 3097 case TemplateArgument::Declaration: 3098 AddDeclRef(Arg.getAsDecl(), Record); 3099 break; 3100 case TemplateArgument::Integral: 3101 AddAPSInt(*Arg.getAsIntegral(), Record); 3102 AddTypeRef(Arg.getIntegralType(), Record); 3103 break; 3104 case TemplateArgument::Template: 3105 AddTemplateName(Arg.getAsTemplate(), Record); 3106 break; 3107 case TemplateArgument::Expression: 3108 AddStmt(Arg.getAsExpr()); 3109 break; 3110 case TemplateArgument::Pack: 3111 Record.push_back(Arg.pack_size()); 3112 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end(); 3113 I != E; ++I) 3114 AddTemplateArgument(*I, Record); 3115 break; 3116 } 3117 } 3118 3119 void 3120 ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, 3121 RecordData &Record) { 3122 assert(TemplateParams && "No TemplateParams!"); 3123 AddSourceLocation(TemplateParams->getTemplateLoc(), Record); 3124 AddSourceLocation(TemplateParams->getLAngleLoc(), Record); 3125 AddSourceLocation(TemplateParams->getRAngleLoc(), Record); 3126 Record.push_back(TemplateParams->size()); 3127 for (TemplateParameterList::const_iterator 3128 P = TemplateParams->begin(), PEnd = TemplateParams->end(); 3129 P != PEnd; ++P) 3130 AddDeclRef(*P, Record); 3131 } 3132 3133 /// \brief Emit a template argument list. 3134 void 3135 ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, 3136 RecordData &Record) { 3137 assert(TemplateArgs && "No TemplateArgs!"); 3138 Record.push_back(TemplateArgs->flat_size()); 3139 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i) 3140 AddTemplateArgument(TemplateArgs->get(i), Record); 3141 } 3142 3143 3144 void 3145 ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) { 3146 Record.push_back(Set.size()); 3147 for (UnresolvedSetImpl::const_iterator 3148 I = Set.begin(), E = Set.end(); I != E; ++I) { 3149 AddDeclRef(I.getDecl(), Record); 3150 Record.push_back(I.getAccess()); 3151 } 3152 } 3153 3154 void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, 3155 RecordData &Record) { 3156 Record.push_back(Base.isVirtual()); 3157 Record.push_back(Base.isBaseOfClass()); 3158 Record.push_back(Base.getAccessSpecifierAsWritten()); 3159 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record); 3160 AddSourceRange(Base.getSourceRange(), Record); 3161 } 3162 3163 void ASTWriter::AddCXXBaseOrMemberInitializers( 3164 const CXXBaseOrMemberInitializer * const *BaseOrMembers, 3165 unsigned NumBaseOrMembers, RecordData &Record) { 3166 Record.push_back(NumBaseOrMembers); 3167 for (unsigned i=0; i != NumBaseOrMembers; ++i) { 3168 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i]; 3169 3170 Record.push_back(Init->isBaseInitializer()); 3171 if (Init->isBaseInitializer()) { 3172 AddTypeSourceInfo(Init->getBaseClassInfo(), Record); 3173 Record.push_back(Init->isBaseVirtual()); 3174 } else { 3175 AddDeclRef(Init->getMember(), Record); 3176 } 3177 AddSourceLocation(Init->getMemberLocation(), Record); 3178 AddStmt(Init->getInit()); 3179 AddDeclRef(Init->getAnonUnionMember(), Record); 3180 AddSourceLocation(Init->getLParenLoc(), Record); 3181 AddSourceLocation(Init->getRParenLoc(), Record); 3182 Record.push_back(Init->isWritten()); 3183 if (Init->isWritten()) { 3184 Record.push_back(Init->getSourceOrder()); 3185 } else { 3186 Record.push_back(Init->getNumArrayIndices()); 3187 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) 3188 AddDeclRef(Init->getArrayIndex(i), Record); 3189 } 3190 } 3191 } 3192 3193 void ASTWriter::SetReader(ASTReader *Reader) { 3194 assert(Reader && "Cannot remove chain"); 3195 assert(FirstDeclID == NextDeclID && 3196 FirstTypeID == NextTypeID && 3197 FirstIdentID == NextIdentID && 3198 FirstSelectorID == NextSelectorID && 3199 FirstMacroID == NextMacroID && 3200 "Setting chain after writing has started."); 3201 Chain = Reader; 3202 } 3203 3204 void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { 3205 IdentifierIDs[II] = ID; 3206 } 3207 3208 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) { 3209 // Always take the highest-numbered type index. This copes with an interesting 3210 // case for chained AST writing where we schedule writing the type and then, 3211 // later, deserialize the type from another AST. In this case, we want to 3212 // keep the higher-numbered entry so that we can properly write it out to 3213 // the AST file. 3214 TypeIdx &StoredIdx = TypeIdxs[T]; 3215 if (Idx.getIndex() >= StoredIdx.getIndex()) 3216 StoredIdx = Idx; 3217 } 3218 3219 void ASTWriter::DeclRead(DeclID ID, const Decl *D) { 3220 DeclIDs[D] = ID; 3221 } 3222 3223 void ASTWriter::SelectorRead(SelectorID ID, Selector S) { 3224 SelectorIDs[S] = ID; 3225 } 3226 3227 void ASTWriter::MacroDefinitionRead(serialization::MacroID ID, 3228 MacroDefinition *MD) { 3229 MacroDefinitions[MD] = ID; 3230 } 3231