ASTContext.cpp revision af3e72285238369c2ea4ebd40a1c9a87bd3eabb7
1//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// 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 implements the ASTContext interface. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/AST/ASTContext.h" 15#include "clang/AST/DeclCXX.h" 16#include "clang/AST/DeclObjC.h" 17#include "clang/AST/DeclTemplate.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/RecordLayout.h" 20#include "clang/Basic/SourceManager.h" 21#include "clang/Basic/TargetInfo.h" 22#include "llvm/ADT/StringExtras.h" 23#include "llvm/Bitcode/Serialize.h" 24#include "llvm/Bitcode/Deserialize.h" 25#include "llvm/Support/MathExtras.h" 26#include "llvm/Support/MemoryBuffer.h" 27using namespace clang; 28 29enum FloatingRank { 30 FloatRank, DoubleRank, LongDoubleRank 31}; 32 33ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, 34 TargetInfo &t, 35 IdentifierTable &idents, SelectorTable &sels, 36 bool FreeMem, unsigned size_reserve) : 37 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), 38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), 39 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels) { 40 if (size_reserve > 0) Types.reserve(size_reserve); 41 InitBuiltinTypes(); 42 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin); 43 TUDecl = TranslationUnitDecl::Create(*this); 44} 45 46ASTContext::~ASTContext() { 47 // Deallocate all the types. 48 while (!Types.empty()) { 49 Types.back()->Destroy(*this); 50 Types.pop_back(); 51 } 52 53 { 54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator 55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); 56 while (I != E) { 57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); 58 delete R; 59 } 60 } 61 62 { 63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator 64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end(); 65 while (I != E) { 66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); 67 delete R; 68 } 69 } 70 71 { 72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator 73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end(); 74 while (I != E) { 75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second); 76 R->Destroy(*this); 77 } 78 } 79 80 // Destroy nested-name-specifiers. 81 for (llvm::FoldingSet<NestedNameSpecifier>::iterator 82 NNS = NestedNameSpecifiers.begin(), 83 NNSEnd = NestedNameSpecifiers.end(); 84 NNS != NNSEnd; 85 /* Increment in loop */) 86 (*NNS++).Destroy(*this); 87 88 if (GlobalNestedNameSpecifier) 89 GlobalNestedNameSpecifier->Destroy(*this); 90 91 TUDecl->Destroy(*this); 92} 93 94void ASTContext::PrintStats() const { 95 fprintf(stderr, "*** AST Context Stats:\n"); 96 fprintf(stderr, " %d types total.\n", (int)Types.size()); 97 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; 98 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0; 99 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0; 100 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0; 101 102 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; 103 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0; 104 unsigned NumObjCQualifiedIds = 0; 105 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0; 106 107 for (unsigned i = 0, e = Types.size(); i != e; ++i) { 108 Type *T = Types[i]; 109 if (isa<BuiltinType>(T)) 110 ++NumBuiltin; 111 else if (isa<PointerType>(T)) 112 ++NumPointer; 113 else if (isa<BlockPointerType>(T)) 114 ++NumBlockPointer; 115 else if (isa<LValueReferenceType>(T)) 116 ++NumLValueReference; 117 else if (isa<RValueReferenceType>(T)) 118 ++NumRValueReference; 119 else if (isa<MemberPointerType>(T)) 120 ++NumMemberPointer; 121 else if (isa<ComplexType>(T)) 122 ++NumComplex; 123 else if (isa<ArrayType>(T)) 124 ++NumArray; 125 else if (isa<VectorType>(T)) 126 ++NumVector; 127 else if (isa<FunctionNoProtoType>(T)) 128 ++NumFunctionNP; 129 else if (isa<FunctionProtoType>(T)) 130 ++NumFunctionP; 131 else if (isa<TypedefType>(T)) 132 ++NumTypeName; 133 else if (TagType *TT = dyn_cast<TagType>(T)) { 134 ++NumTagged; 135 switch (TT->getDecl()->getTagKind()) { 136 default: assert(0 && "Unknown tagged type!"); 137 case TagDecl::TK_struct: ++NumTagStruct; break; 138 case TagDecl::TK_union: ++NumTagUnion; break; 139 case TagDecl::TK_class: ++NumTagClass; break; 140 case TagDecl::TK_enum: ++NumTagEnum; break; 141 } 142 } else if (isa<ObjCInterfaceType>(T)) 143 ++NumObjCInterfaces; 144 else if (isa<ObjCQualifiedInterfaceType>(T)) 145 ++NumObjCQualifiedInterfaces; 146 else if (isa<ObjCQualifiedIdType>(T)) 147 ++NumObjCQualifiedIds; 148 else if (isa<TypeOfType>(T)) 149 ++NumTypeOfTypes; 150 else if (isa<TypeOfExprType>(T)) 151 ++NumTypeOfExprTypes; 152 else { 153 QualType(T, 0).dump(); 154 assert(0 && "Unknown type!"); 155 } 156 } 157 158 fprintf(stderr, " %d builtin types\n", NumBuiltin); 159 fprintf(stderr, " %d pointer types\n", NumPointer); 160 fprintf(stderr, " %d block pointer types\n", NumBlockPointer); 161 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference); 162 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference); 163 fprintf(stderr, " %d member pointer types\n", NumMemberPointer); 164 fprintf(stderr, " %d complex types\n", NumComplex); 165 fprintf(stderr, " %d array types\n", NumArray); 166 fprintf(stderr, " %d vector types\n", NumVector); 167 fprintf(stderr, " %d function types with proto\n", NumFunctionP); 168 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); 169 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); 170 fprintf(stderr, " %d tagged types\n", NumTagged); 171 fprintf(stderr, " %d struct types\n", NumTagStruct); 172 fprintf(stderr, " %d union types\n", NumTagUnion); 173 fprintf(stderr, " %d class types\n", NumTagClass); 174 fprintf(stderr, " %d enum types\n", NumTagEnum); 175 fprintf(stderr, " %d interface types\n", NumObjCInterfaces); 176 fprintf(stderr, " %d protocol qualified interface types\n", 177 NumObjCQualifiedInterfaces); 178 fprintf(stderr, " %d protocol qualified id types\n", 179 NumObjCQualifiedIds); 180 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes); 181 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes); 182 183 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ 184 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ 185 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ 186 NumLValueReference*sizeof(LValueReferenceType)+ 187 NumRValueReference*sizeof(RValueReferenceType)+ 188 NumMemberPointer*sizeof(MemberPointerType)+ 189 NumFunctionP*sizeof(FunctionProtoType)+ 190 NumFunctionNP*sizeof(FunctionNoProtoType)+ 191 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+ 192 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType))); 193} 194 195 196void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { 197 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr()); 198} 199 200void ASTContext::InitBuiltinTypes() { 201 assert(VoidTy.isNull() && "Context reinitialized?"); 202 203 // C99 6.2.5p19. 204 InitBuiltinType(VoidTy, BuiltinType::Void); 205 206 // C99 6.2.5p2. 207 InitBuiltinType(BoolTy, BuiltinType::Bool); 208 // C99 6.2.5p3. 209 if (Target.isCharSigned()) 210 InitBuiltinType(CharTy, BuiltinType::Char_S); 211 else 212 InitBuiltinType(CharTy, BuiltinType::Char_U); 213 // C99 6.2.5p4. 214 InitBuiltinType(SignedCharTy, BuiltinType::SChar); 215 InitBuiltinType(ShortTy, BuiltinType::Short); 216 InitBuiltinType(IntTy, BuiltinType::Int); 217 InitBuiltinType(LongTy, BuiltinType::Long); 218 InitBuiltinType(LongLongTy, BuiltinType::LongLong); 219 220 // C99 6.2.5p6. 221 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); 222 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); 223 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); 224 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); 225 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); 226 227 // C99 6.2.5p10. 228 InitBuiltinType(FloatTy, BuiltinType::Float); 229 InitBuiltinType(DoubleTy, BuiltinType::Double); 230 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); 231 232 if (LangOpts.CPlusPlus) // C++ 3.9.1p5 233 InitBuiltinType(WCharTy, BuiltinType::WChar); 234 else // C99 235 WCharTy = getFromTargetType(Target.getWCharType()); 236 237 // Placeholder type for functions. 238 InitBuiltinType(OverloadTy, BuiltinType::Overload); 239 240 // Placeholder type for type-dependent expressions whose type is 241 // completely unknown. No code should ever check a type against 242 // DependentTy and users should never see it; however, it is here to 243 // help diagnose failures to properly check for type-dependent 244 // expressions. 245 InitBuiltinType(DependentTy, BuiltinType::Dependent); 246 247 // C99 6.2.5p11. 248 FloatComplexTy = getComplexType(FloatTy); 249 DoubleComplexTy = getComplexType(DoubleTy); 250 LongDoubleComplexTy = getComplexType(LongDoubleTy); 251 252 BuiltinVaListType = QualType(); 253 ObjCIdType = QualType(); 254 IdStructType = 0; 255 ObjCClassType = QualType(); 256 ClassStructType = 0; 257 258 ObjCConstantStringType = QualType(); 259 260 // void * type 261 VoidPtrTy = getPointerType(VoidTy); 262} 263 264//===----------------------------------------------------------------------===// 265// Type Sizing and Analysis 266//===----------------------------------------------------------------------===// 267 268/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 269/// scalar floating point type. 270const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { 271 const BuiltinType *BT = T->getAsBuiltinType(); 272 assert(BT && "Not a floating point type!"); 273 switch (BT->getKind()) { 274 default: assert(0 && "Not a floating point type!"); 275 case BuiltinType::Float: return Target.getFloatFormat(); 276 case BuiltinType::Double: return Target.getDoubleFormat(); 277 case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); 278 } 279} 280 281/// getDeclAlign - Return a conservative estimate of the alignment of the 282/// specified decl. Note that bitfields do not have a valid alignment, so 283/// this method will assert on them. 284unsigned ASTContext::getDeclAlignInBytes(const Decl *D) { 285 unsigned Align = Target.getCharWidth(); 286 287 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) 288 Align = std::max(Align, AA->getAlignment()); 289 290 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { 291 QualType T = VD->getType(); 292 // Incomplete or function types default to 1. 293 if (!T->isIncompleteType() && !T->isFunctionType()) { 294 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) 295 T = cast<ArrayType>(T)->getElementType(); 296 297 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); 298 } 299 } 300 301 return Align / Target.getCharWidth(); 302} 303 304/// getTypeSize - Return the size of the specified type, in bits. This method 305/// does not work on incomplete types. 306std::pair<uint64_t, unsigned> 307ASTContext::getTypeInfo(const Type *T) { 308 T = getCanonicalType(T); 309 uint64_t Width=0; 310 unsigned Align=8; 311 switch (T->getTypeClass()) { 312#define TYPE(Class, Base) 313#define ABSTRACT_TYPE(Class, Base) 314#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 315#define DEPENDENT_TYPE(Class, Base) case Type::Class: 316#include "clang/AST/TypeNodes.def" 317 assert(false && "Should not see non-canonical or dependent types"); 318 break; 319 320 case Type::FunctionNoProto: 321 case Type::FunctionProto: 322 case Type::IncompleteArray: 323 assert(0 && "Incomplete types have no size!"); 324 case Type::VariableArray: 325 assert(0 && "VLAs not implemented yet!"); 326 case Type::ConstantArray: { 327 const ConstantArrayType *CAT = cast<ConstantArrayType>(T); 328 329 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); 330 Width = EltInfo.first*CAT->getSize().getZExtValue(); 331 Align = EltInfo.second; 332 break; 333 } 334 case Type::ExtVector: 335 case Type::Vector: { 336 std::pair<uint64_t, unsigned> EltInfo = 337 getTypeInfo(cast<VectorType>(T)->getElementType()); 338 Width = EltInfo.first*cast<VectorType>(T)->getNumElements(); 339 Align = Width; 340 // If the alignment is not a power of 2, round up to the next power of 2. 341 // This happens for non-power-of-2 length vectors. 342 // FIXME: this should probably be a target property. 343 Align = 1 << llvm::Log2_32_Ceil(Align); 344 break; 345 } 346 347 case Type::Builtin: 348 switch (cast<BuiltinType>(T)->getKind()) { 349 default: assert(0 && "Unknown builtin type!"); 350 case BuiltinType::Void: 351 assert(0 && "Incomplete types have no size!"); 352 case BuiltinType::Bool: 353 Width = Target.getBoolWidth(); 354 Align = Target.getBoolAlign(); 355 break; 356 case BuiltinType::Char_S: 357 case BuiltinType::Char_U: 358 case BuiltinType::UChar: 359 case BuiltinType::SChar: 360 Width = Target.getCharWidth(); 361 Align = Target.getCharAlign(); 362 break; 363 case BuiltinType::WChar: 364 Width = Target.getWCharWidth(); 365 Align = Target.getWCharAlign(); 366 break; 367 case BuiltinType::UShort: 368 case BuiltinType::Short: 369 Width = Target.getShortWidth(); 370 Align = Target.getShortAlign(); 371 break; 372 case BuiltinType::UInt: 373 case BuiltinType::Int: 374 Width = Target.getIntWidth(); 375 Align = Target.getIntAlign(); 376 break; 377 case BuiltinType::ULong: 378 case BuiltinType::Long: 379 Width = Target.getLongWidth(); 380 Align = Target.getLongAlign(); 381 break; 382 case BuiltinType::ULongLong: 383 case BuiltinType::LongLong: 384 Width = Target.getLongLongWidth(); 385 Align = Target.getLongLongAlign(); 386 break; 387 case BuiltinType::Float: 388 Width = Target.getFloatWidth(); 389 Align = Target.getFloatAlign(); 390 break; 391 case BuiltinType::Double: 392 Width = Target.getDoubleWidth(); 393 Align = Target.getDoubleAlign(); 394 break; 395 case BuiltinType::LongDouble: 396 Width = Target.getLongDoubleWidth(); 397 Align = Target.getLongDoubleAlign(); 398 break; 399 } 400 break; 401 case Type::FixedWidthInt: 402 // FIXME: This isn't precisely correct; the width/alignment should depend 403 // on the available types for the target 404 Width = cast<FixedWidthIntType>(T)->getWidth(); 405 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); 406 Align = Width; 407 break; 408 case Type::ExtQual: 409 // FIXME: Pointers into different addr spaces could have different sizes and 410 // alignment requirements: getPointerInfo should take an AddrSpace. 411 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0)); 412 case Type::ObjCQualifiedId: 413 case Type::ObjCQualifiedClass: 414 case Type::ObjCQualifiedInterface: 415 Width = Target.getPointerWidth(0); 416 Align = Target.getPointerAlign(0); 417 break; 418 case Type::BlockPointer: { 419 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); 420 Width = Target.getPointerWidth(AS); 421 Align = Target.getPointerAlign(AS); 422 break; 423 } 424 case Type::Pointer: { 425 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); 426 Width = Target.getPointerWidth(AS); 427 Align = Target.getPointerAlign(AS); 428 break; 429 } 430 case Type::LValueReference: 431 case Type::RValueReference: 432 // "When applied to a reference or a reference type, the result is the size 433 // of the referenced type." C++98 5.3.3p2: expr.sizeof. 434 // FIXME: This is wrong for struct layout: a reference in a struct has 435 // pointer size. 436 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); 437 case Type::MemberPointer: { 438 // FIXME: This is not only platform- but also ABI-dependent. We follow 439 // the GCC ABI, where pointers to data are one pointer large, pointers to 440 // functions two pointers. But if we want to support ABI compatibility with 441 // other compilers too, we need to delegate this completely to TargetInfo 442 // or some ABI abstraction layer. 443 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); 444 unsigned AS = Pointee.getAddressSpace(); 445 Width = Target.getPointerWidth(AS); 446 if (Pointee->isFunctionType()) 447 Width *= 2; 448 Align = Target.getPointerAlign(AS); 449 // GCC aligns at single pointer width. 450 } 451 case Type::Complex: { 452 // Complex types have the same alignment as their elements, but twice the 453 // size. 454 std::pair<uint64_t, unsigned> EltInfo = 455 getTypeInfo(cast<ComplexType>(T)->getElementType()); 456 Width = EltInfo.first*2; 457 Align = EltInfo.second; 458 break; 459 } 460 case Type::ObjCInterface: { 461 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); 462 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); 463 Width = Layout.getSize(); 464 Align = Layout.getAlignment(); 465 break; 466 } 467 case Type::Record: 468 case Type::Enum: { 469 const TagType *TT = cast<TagType>(T); 470 471 if (TT->getDecl()->isInvalidDecl()) { 472 Width = 1; 473 Align = 1; 474 break; 475 } 476 477 if (const EnumType *ET = dyn_cast<EnumType>(TT)) 478 return getTypeInfo(ET->getDecl()->getIntegerType()); 479 480 const RecordType *RT = cast<RecordType>(TT); 481 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); 482 Width = Layout.getSize(); 483 Align = Layout.getAlignment(); 484 break; 485 } 486 487 case Type::TemplateSpecialization: 488 assert(false && "Dependent types have no size"); 489 break; 490 } 491 492 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); 493 return std::make_pair(Width, Align); 494} 495 496/// getPreferredTypeAlign - Return the "preferred" alignment of the specified 497/// type for the current target in bits. This can be different than the ABI 498/// alignment in cases where it is beneficial for performance to overalign 499/// a data type. 500unsigned ASTContext::getPreferredTypeAlign(const Type *T) { 501 unsigned ABIAlign = getTypeAlign(T); 502 503 // Doubles should be naturally aligned if possible. 504 if (T->isSpecificBuiltinType(BuiltinType::Double)) 505 return std::max(ABIAlign, 64U); 506 507 return ABIAlign; 508} 509 510 511/// LayoutField - Field layout. 512void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, 513 bool IsUnion, unsigned StructPacking, 514 ASTContext &Context) { 515 unsigned FieldPacking = StructPacking; 516 uint64_t FieldOffset = IsUnion ? 0 : Size; 517 uint64_t FieldSize; 518 unsigned FieldAlign; 519 520 // FIXME: Should this override struct packing? Probably we want to 521 // take the minimum? 522 if (const PackedAttr *PA = FD->getAttr<PackedAttr>()) 523 FieldPacking = PA->getAlignment(); 524 525 if (const Expr *BitWidthExpr = FD->getBitWidth()) { 526 // TODO: Need to check this algorithm on other targets! 527 // (tested on Linux-X86) 528 FieldSize = 529 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue(); 530 531 std::pair<uint64_t, unsigned> FieldInfo = 532 Context.getTypeInfo(FD->getType()); 533 uint64_t TypeSize = FieldInfo.first; 534 535 // Determine the alignment of this bitfield. The packing 536 // attributes define a maximum and the alignment attribute defines 537 // a minimum. 538 // FIXME: What is the right behavior when the specified alignment 539 // is smaller than the specified packing? 540 FieldAlign = FieldInfo.second; 541 if (FieldPacking) 542 FieldAlign = std::min(FieldAlign, FieldPacking); 543 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) 544 FieldAlign = std::max(FieldAlign, AA->getAlignment()); 545 546 // Check if we need to add padding to give the field the correct 547 // alignment. 548 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) 549 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); 550 551 // Padding members don't affect overall alignment 552 if (!FD->getIdentifier()) 553 FieldAlign = 1; 554 } else { 555 if (FD->getType()->isIncompleteArrayType()) { 556 // This is a flexible array member; we can't directly 557 // query getTypeInfo about these, so we figure it out here. 558 // Flexible array members don't have any size, but they 559 // have to be aligned appropriately for their element type. 560 FieldSize = 0; 561 const ArrayType* ATy = Context.getAsArrayType(FD->getType()); 562 FieldAlign = Context.getTypeAlign(ATy->getElementType()); 563 } else { 564 std::pair<uint64_t, unsigned> FieldInfo = 565 Context.getTypeInfo(FD->getType()); 566 FieldSize = FieldInfo.first; 567 FieldAlign = FieldInfo.second; 568 } 569 570 // Determine the alignment of this bitfield. The packing 571 // attributes define a maximum and the alignment attribute defines 572 // a minimum. Additionally, the packing alignment must be at least 573 // a byte for non-bitfields. 574 // 575 // FIXME: What is the right behavior when the specified alignment 576 // is smaller than the specified packing? 577 if (FieldPacking) 578 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking)); 579 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) 580 FieldAlign = std::max(FieldAlign, AA->getAlignment()); 581 582 // Round up the current record size to the field's alignment boundary. 583 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); 584 } 585 586 // Place this field at the current location. 587 FieldOffsets[FieldNo] = FieldOffset; 588 589 // Reserve space for this field. 590 if (IsUnion) { 591 Size = std::max(Size, FieldSize); 592 } else { 593 Size = FieldOffset + FieldSize; 594 } 595 596 // Remember max struct/class alignment. 597 Alignment = std::max(Alignment, FieldAlign); 598} 599 600void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, 601 std::vector<FieldDecl*> &Fields) const { 602 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); 603 if (SuperClass) 604 CollectObjCIvars(SuperClass, Fields); 605 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 606 E = OI->ivar_end(); I != E; ++I) { 607 ObjCIvarDecl *IVDecl = (*I); 608 if (!IVDecl->isInvalidDecl()) 609 Fields.push_back(cast<FieldDecl>(IVDecl)); 610 } 611 // look into properties. 612 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), 613 E = OI->prop_end(); I != E; ++I) { 614 ObjCPropertyDecl *PDecl = (*I); 615 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) 616 Fields.push_back(cast<FieldDecl>(IV)); 617 } 618} 619 620/// addRecordToClass - produces record info. for the class for its 621/// ivars and all those inherited. 622/// 623const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) 624{ 625 const RecordDecl *&RD = ASTRecordForInterface[D]; 626 if (RD) 627 return RD; 628 std::vector<FieldDecl*> RecFields; 629 CollectObjCIvars(D, RecFields); 630 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, 631 D->getLocation(), 632 D->getIdentifier()); 633 /// FIXME! Can do collection of ivars and adding to the record while 634 /// doing it. 635 for (unsigned int i = 0; i != RecFields.size(); i++) { 636 FieldDecl *Field = FieldDecl::Create(*this, NewRD, 637 RecFields[i]->getLocation(), 638 RecFields[i]->getIdentifier(), 639 RecFields[i]->getType(), 640 RecFields[i]->getBitWidth(), false); 641 NewRD->addDecl(Field); 642 } 643 NewRD->completeDefinition(*this); 644 RD = NewRD; 645 return RD; 646} 647 648/// setFieldDecl - maps a field for the given Ivar reference node. 649// 650void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI, 651 const ObjCIvarDecl *Ivar, 652 const ObjCIvarRefExpr *MRef) { 653 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))-> 654 lookupFieldDeclForIvar(*this, Ivar); 655 ASTFieldForIvarRef[MRef] = FD; 656} 657 658/// getASTObjcInterfaceLayout - Get or compute information about the layout of 659/// the specified Objective C, which indicates its size and ivar 660/// position information. 661const ASTRecordLayout & 662ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { 663 // Look up this layout, if already laid out, return what we have. 664 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D]; 665 if (Entry) return *Entry; 666 667 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can 668 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. 669 ASTRecordLayout *NewEntry = NULL; 670 unsigned FieldCount = D->ivar_size(); 671 if (ObjCInterfaceDecl *SD = D->getSuperClass()) { 672 FieldCount++; 673 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD); 674 unsigned Alignment = SL.getAlignment(); 675 uint64_t Size = SL.getSize(); 676 NewEntry = new ASTRecordLayout(Size, Alignment); 677 NewEntry->InitializeLayout(FieldCount); 678 // Super class is at the beginning of the layout. 679 NewEntry->SetFieldOffset(0, 0); 680 } else { 681 NewEntry = new ASTRecordLayout(); 682 NewEntry->InitializeLayout(FieldCount); 683 } 684 Entry = NewEntry; 685 686 unsigned StructPacking = 0; 687 if (const PackedAttr *PA = D->getAttr<PackedAttr>()) 688 StructPacking = PA->getAlignment(); 689 690 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) 691 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), 692 AA->getAlignment())); 693 694 // Layout each ivar sequentially. 695 unsigned i = 0; 696 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(), 697 IVE = D->ivar_end(); IVI != IVE; ++IVI) { 698 const ObjCIvarDecl* Ivar = (*IVI); 699 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this); 700 } 701 702 // Finally, round the size of the total struct up to the alignment of the 703 // struct itself. 704 NewEntry->FinalizeLayout(); 705 return *NewEntry; 706} 707 708/// getASTRecordLayout - Get or compute information about the layout of the 709/// specified record (struct/union/class), which indicates its size and field 710/// position information. 711const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { 712 D = D->getDefinition(*this); 713 assert(D && "Cannot get layout of forward declarations!"); 714 715 // Look up this layout, if already laid out, return what we have. 716 const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; 717 if (Entry) return *Entry; 718 719 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can 720 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. 721 ASTRecordLayout *NewEntry = new ASTRecordLayout(); 722 Entry = NewEntry; 723 724 // FIXME: Avoid linear walk through the fields, if possible. 725 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end())); 726 bool IsUnion = D->isUnion(); 727 728 unsigned StructPacking = 0; 729 if (const PackedAttr *PA = D->getAttr<PackedAttr>()) 730 StructPacking = PA->getAlignment(); 731 732 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) 733 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), 734 AA->getAlignment())); 735 736 // Layout each field, for now, just sequentially, respecting alignment. In 737 // the future, this will need to be tweakable by targets. 738 unsigned FieldIdx = 0; 739 for (RecordDecl::field_iterator Field = D->field_begin(), 740 FieldEnd = D->field_end(); 741 Field != FieldEnd; (void)++Field, ++FieldIdx) 742 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this); 743 744 // Finally, round the size of the total struct up to the alignment of the 745 // struct itself. 746 NewEntry->FinalizeLayout(); 747 return *NewEntry; 748} 749 750//===----------------------------------------------------------------------===// 751// Type creation/memoization methods 752//===----------------------------------------------------------------------===// 753 754QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { 755 QualType CanT = getCanonicalType(T); 756 if (CanT.getAddressSpace() == AddressSpace) 757 return T; 758 759 // If we are composing extended qualifiers together, merge together into one 760 // ExtQualType node. 761 unsigned CVRQuals = T.getCVRQualifiers(); 762 QualType::GCAttrTypes GCAttr = QualType::GCNone; 763 Type *TypeNode = T.getTypePtr(); 764 765 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { 766 // If this type already has an address space specified, it cannot get 767 // another one. 768 assert(EQT->getAddressSpace() == 0 && 769 "Type cannot be in multiple addr spaces!"); 770 GCAttr = EQT->getObjCGCAttr(); 771 TypeNode = EQT->getBaseType(); 772 } 773 774 // Check if we've already instantiated this type. 775 llvm::FoldingSetNodeID ID; 776 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); 777 void *InsertPos = 0; 778 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) 779 return QualType(EXTQy, CVRQuals); 780 781 // If the base type isn't canonical, this won't be a canonical type either, 782 // so fill in the canonical type field. 783 QualType Canonical; 784 if (!TypeNode->isCanonical()) { 785 Canonical = getAddrSpaceQualType(CanT, AddressSpace); 786 787 // Update InsertPos, the previous call could have invalidated it. 788 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); 789 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 790 } 791 ExtQualType *New = 792 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); 793 ExtQualTypes.InsertNode(New, InsertPos); 794 Types.push_back(New); 795 return QualType(New, CVRQuals); 796} 797 798QualType ASTContext::getObjCGCQualType(QualType T, 799 QualType::GCAttrTypes GCAttr) { 800 QualType CanT = getCanonicalType(T); 801 if (CanT.getObjCGCAttr() == GCAttr) 802 return T; 803 804 // If we are composing extended qualifiers together, merge together into one 805 // ExtQualType node. 806 unsigned CVRQuals = T.getCVRQualifiers(); 807 Type *TypeNode = T.getTypePtr(); 808 unsigned AddressSpace = 0; 809 810 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { 811 // If this type already has an address space specified, it cannot get 812 // another one. 813 assert(EQT->getObjCGCAttr() == QualType::GCNone && 814 "Type cannot be in multiple addr spaces!"); 815 AddressSpace = EQT->getAddressSpace(); 816 TypeNode = EQT->getBaseType(); 817 } 818 819 // Check if we've already instantiated an gc qual'd type of this type. 820 llvm::FoldingSetNodeID ID; 821 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); 822 void *InsertPos = 0; 823 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) 824 return QualType(EXTQy, CVRQuals); 825 826 // If the base type isn't canonical, this won't be a canonical type either, 827 // so fill in the canonical type field. 828 // FIXME: Isn't this also not canonical if the base type is a array 829 // or pointer type? I can't find any documentation for objc_gc, though... 830 QualType Canonical; 831 if (!T->isCanonical()) { 832 Canonical = getObjCGCQualType(CanT, GCAttr); 833 834 // Update InsertPos, the previous call could have invalidated it. 835 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); 836 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 837 } 838 ExtQualType *New = 839 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); 840 ExtQualTypes.InsertNode(New, InsertPos); 841 Types.push_back(New); 842 return QualType(New, CVRQuals); 843} 844 845/// getComplexType - Return the uniqued reference to the type for a complex 846/// number with the specified element type. 847QualType ASTContext::getComplexType(QualType T) { 848 // Unique pointers, to guarantee there is only one pointer of a particular 849 // structure. 850 llvm::FoldingSetNodeID ID; 851 ComplexType::Profile(ID, T); 852 853 void *InsertPos = 0; 854 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) 855 return QualType(CT, 0); 856 857 // If the pointee type isn't canonical, this won't be a canonical type either, 858 // so fill in the canonical type field. 859 QualType Canonical; 860 if (!T->isCanonical()) { 861 Canonical = getComplexType(getCanonicalType(T)); 862 863 // Get the new insert position for the node we care about. 864 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); 865 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 866 } 867 ComplexType *New = new (*this,8) ComplexType(T, Canonical); 868 Types.push_back(New); 869 ComplexTypes.InsertNode(New, InsertPos); 870 return QualType(New, 0); 871} 872 873QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { 874 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? 875 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; 876 FixedWidthIntType *&Entry = Map[Width]; 877 if (!Entry) 878 Entry = new FixedWidthIntType(Width, Signed); 879 return QualType(Entry, 0); 880} 881 882/// getPointerType - Return the uniqued reference to the type for a pointer to 883/// the specified type. 884QualType ASTContext::getPointerType(QualType T) { 885 // Unique pointers, to guarantee there is only one pointer of a particular 886 // structure. 887 llvm::FoldingSetNodeID ID; 888 PointerType::Profile(ID, T); 889 890 void *InsertPos = 0; 891 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 892 return QualType(PT, 0); 893 894 // If the pointee type isn't canonical, this won't be a canonical type either, 895 // so fill in the canonical type field. 896 QualType Canonical; 897 if (!T->isCanonical()) { 898 Canonical = getPointerType(getCanonicalType(T)); 899 900 // Get the new insert position for the node we care about. 901 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); 902 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 903 } 904 PointerType *New = new (*this,8) PointerType(T, Canonical); 905 Types.push_back(New); 906 PointerTypes.InsertNode(New, InsertPos); 907 return QualType(New, 0); 908} 909 910/// getBlockPointerType - Return the uniqued reference to the type for 911/// a pointer to the specified block. 912QualType ASTContext::getBlockPointerType(QualType T) { 913 assert(T->isFunctionType() && "block of function types only"); 914 // Unique pointers, to guarantee there is only one block of a particular 915 // structure. 916 llvm::FoldingSetNodeID ID; 917 BlockPointerType::Profile(ID, T); 918 919 void *InsertPos = 0; 920 if (BlockPointerType *PT = 921 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 922 return QualType(PT, 0); 923 924 // If the block pointee type isn't canonical, this won't be a canonical 925 // type either so fill in the canonical type field. 926 QualType Canonical; 927 if (!T->isCanonical()) { 928 Canonical = getBlockPointerType(getCanonicalType(T)); 929 930 // Get the new insert position for the node we care about. 931 BlockPointerType *NewIP = 932 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 933 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 934 } 935 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical); 936 Types.push_back(New); 937 BlockPointerTypes.InsertNode(New, InsertPos); 938 return QualType(New, 0); 939} 940 941/// getLValueReferenceType - Return the uniqued reference to the type for an 942/// lvalue reference to the specified type. 943QualType ASTContext::getLValueReferenceType(QualType T) { 944 // Unique pointers, to guarantee there is only one pointer of a particular 945 // structure. 946 llvm::FoldingSetNodeID ID; 947 ReferenceType::Profile(ID, T); 948 949 void *InsertPos = 0; 950 if (LValueReferenceType *RT = 951 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 952 return QualType(RT, 0); 953 954 // If the referencee type isn't canonical, this won't be a canonical type 955 // either, so fill in the canonical type field. 956 QualType Canonical; 957 if (!T->isCanonical()) { 958 Canonical = getLValueReferenceType(getCanonicalType(T)); 959 960 // Get the new insert position for the node we care about. 961 LValueReferenceType *NewIP = 962 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 963 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 964 } 965 966 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical); 967 Types.push_back(New); 968 LValueReferenceTypes.InsertNode(New, InsertPos); 969 return QualType(New, 0); 970} 971 972/// getRValueReferenceType - Return the uniqued reference to the type for an 973/// rvalue reference to the specified type. 974QualType ASTContext::getRValueReferenceType(QualType T) { 975 // Unique pointers, to guarantee there is only one pointer of a particular 976 // structure. 977 llvm::FoldingSetNodeID ID; 978 ReferenceType::Profile(ID, T); 979 980 void *InsertPos = 0; 981 if (RValueReferenceType *RT = 982 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 983 return QualType(RT, 0); 984 985 // If the referencee type isn't canonical, this won't be a canonical type 986 // either, so fill in the canonical type field. 987 QualType Canonical; 988 if (!T->isCanonical()) { 989 Canonical = getRValueReferenceType(getCanonicalType(T)); 990 991 // Get the new insert position for the node we care about. 992 RValueReferenceType *NewIP = 993 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 994 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 995 } 996 997 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical); 998 Types.push_back(New); 999 RValueReferenceTypes.InsertNode(New, InsertPos); 1000 return QualType(New, 0); 1001} 1002 1003/// getMemberPointerType - Return the uniqued reference to the type for a 1004/// member pointer to the specified type, in the specified class. 1005QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) 1006{ 1007 // Unique pointers, to guarantee there is only one pointer of a particular 1008 // structure. 1009 llvm::FoldingSetNodeID ID; 1010 MemberPointerType::Profile(ID, T, Cls); 1011 1012 void *InsertPos = 0; 1013 if (MemberPointerType *PT = 1014 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1015 return QualType(PT, 0); 1016 1017 // If the pointee or class type isn't canonical, this won't be a canonical 1018 // type either, so fill in the canonical type field. 1019 QualType Canonical; 1020 if (!T->isCanonical()) { 1021 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); 1022 1023 // Get the new insert position for the node we care about. 1024 MemberPointerType *NewIP = 1025 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1026 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1027 } 1028 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical); 1029 Types.push_back(New); 1030 MemberPointerTypes.InsertNode(New, InsertPos); 1031 return QualType(New, 0); 1032} 1033 1034/// getConstantArrayType - Return the unique reference to the type for an 1035/// array of the specified element type. 1036QualType ASTContext::getConstantArrayType(QualType EltTy, 1037 const llvm::APInt &ArySize, 1038 ArrayType::ArraySizeModifier ASM, 1039 unsigned EltTypeQuals) { 1040 llvm::FoldingSetNodeID ID; 1041 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); 1042 1043 void *InsertPos = 0; 1044 if (ConstantArrayType *ATP = 1045 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 1046 return QualType(ATP, 0); 1047 1048 // If the element type isn't canonical, this won't be a canonical type either, 1049 // so fill in the canonical type field. 1050 QualType Canonical; 1051 if (!EltTy->isCanonical()) { 1052 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, 1053 ASM, EltTypeQuals); 1054 // Get the new insert position for the node we care about. 1055 ConstantArrayType *NewIP = 1056 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1057 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1058 } 1059 1060 ConstantArrayType *New = 1061 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); 1062 ConstantArrayTypes.InsertNode(New, InsertPos); 1063 Types.push_back(New); 1064 return QualType(New, 0); 1065} 1066 1067/// getVariableArrayType - Returns a non-unique reference to the type for a 1068/// variable array of the specified element type. 1069QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, 1070 ArrayType::ArraySizeModifier ASM, 1071 unsigned EltTypeQuals) { 1072 // Since we don't unique expressions, it isn't possible to unique VLA's 1073 // that have an expression provided for their size. 1074 1075 VariableArrayType *New = 1076 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals); 1077 1078 VariableArrayTypes.push_back(New); 1079 Types.push_back(New); 1080 return QualType(New, 0); 1081} 1082 1083/// getDependentSizedArrayType - Returns a non-unique reference to 1084/// the type for a dependently-sized array of the specified element 1085/// type. FIXME: We will need these to be uniqued, or at least 1086/// comparable, at some point. 1087QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 1088 ArrayType::ArraySizeModifier ASM, 1089 unsigned EltTypeQuals) { 1090 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && 1091 "Size must be type- or value-dependent!"); 1092 1093 // Since we don't unique expressions, it isn't possible to unique 1094 // dependently-sized array types. 1095 1096 DependentSizedArrayType *New = 1097 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts, 1098 ASM, EltTypeQuals); 1099 1100 DependentSizedArrayTypes.push_back(New); 1101 Types.push_back(New); 1102 return QualType(New, 0); 1103} 1104 1105QualType ASTContext::getIncompleteArrayType(QualType EltTy, 1106 ArrayType::ArraySizeModifier ASM, 1107 unsigned EltTypeQuals) { 1108 llvm::FoldingSetNodeID ID; 1109 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); 1110 1111 void *InsertPos = 0; 1112 if (IncompleteArrayType *ATP = 1113 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 1114 return QualType(ATP, 0); 1115 1116 // If the element type isn't canonical, this won't be a canonical type 1117 // either, so fill in the canonical type field. 1118 QualType Canonical; 1119 1120 if (!EltTy->isCanonical()) { 1121 Canonical = getIncompleteArrayType(getCanonicalType(EltTy), 1122 ASM, EltTypeQuals); 1123 1124 // Get the new insert position for the node we care about. 1125 IncompleteArrayType *NewIP = 1126 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1127 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1128 } 1129 1130 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical, 1131 ASM, EltTypeQuals); 1132 1133 IncompleteArrayTypes.InsertNode(New, InsertPos); 1134 Types.push_back(New); 1135 return QualType(New, 0); 1136} 1137 1138/// getVectorType - Return the unique reference to a vector type of 1139/// the specified element type and size. VectorType must be a built-in type. 1140QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { 1141 BuiltinType *baseType; 1142 1143 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 1144 assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); 1145 1146 // Check if we've already instantiated a vector of this type. 1147 llvm::FoldingSetNodeID ID; 1148 VectorType::Profile(ID, vecType, NumElts, Type::Vector); 1149 void *InsertPos = 0; 1150 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 1151 return QualType(VTP, 0); 1152 1153 // If the element type isn't canonical, this won't be a canonical type either, 1154 // so fill in the canonical type field. 1155 QualType Canonical; 1156 if (!vecType->isCanonical()) { 1157 Canonical = getVectorType(getCanonicalType(vecType), NumElts); 1158 1159 // Get the new insert position for the node we care about. 1160 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1161 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1162 } 1163 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical); 1164 VectorTypes.InsertNode(New, InsertPos); 1165 Types.push_back(New); 1166 return QualType(New, 0); 1167} 1168 1169/// getExtVectorType - Return the unique reference to an extended vector type of 1170/// the specified element type and size. VectorType must be a built-in type. 1171QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { 1172 BuiltinType *baseType; 1173 1174 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 1175 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); 1176 1177 // Check if we've already instantiated a vector of this type. 1178 llvm::FoldingSetNodeID ID; 1179 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); 1180 void *InsertPos = 0; 1181 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 1182 return QualType(VTP, 0); 1183 1184 // If the element type isn't canonical, this won't be a canonical type either, 1185 // so fill in the canonical type field. 1186 QualType Canonical; 1187 if (!vecType->isCanonical()) { 1188 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); 1189 1190 // Get the new insert position for the node we care about. 1191 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1192 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1193 } 1194 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical); 1195 VectorTypes.InsertNode(New, InsertPos); 1196 Types.push_back(New); 1197 return QualType(New, 0); 1198} 1199 1200/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 1201/// 1202QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) { 1203 // Unique functions, to guarantee there is only one function of a particular 1204 // structure. 1205 llvm::FoldingSetNodeID ID; 1206 FunctionNoProtoType::Profile(ID, ResultTy); 1207 1208 void *InsertPos = 0; 1209 if (FunctionNoProtoType *FT = 1210 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) 1211 return QualType(FT, 0); 1212 1213 QualType Canonical; 1214 if (!ResultTy->isCanonical()) { 1215 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy)); 1216 1217 // Get the new insert position for the node we care about. 1218 FunctionNoProtoType *NewIP = 1219 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); 1220 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1221 } 1222 1223 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical); 1224 Types.push_back(New); 1225 FunctionNoProtoTypes.InsertNode(New, InsertPos); 1226 return QualType(New, 0); 1227} 1228 1229/// getFunctionType - Return a normal function type with a typed argument 1230/// list. isVariadic indicates whether the argument list includes '...'. 1231QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, 1232 unsigned NumArgs, bool isVariadic, 1233 unsigned TypeQuals) { 1234 // Unique functions, to guarantee there is only one function of a particular 1235 // structure. 1236 llvm::FoldingSetNodeID ID; 1237 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, 1238 TypeQuals); 1239 1240 void *InsertPos = 0; 1241 if (FunctionProtoType *FTP = 1242 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) 1243 return QualType(FTP, 0); 1244 1245 // Determine whether the type being created is already canonical or not. 1246 bool isCanonical = ResultTy->isCanonical(); 1247 for (unsigned i = 0; i != NumArgs && isCanonical; ++i) 1248 if (!ArgArray[i]->isCanonical()) 1249 isCanonical = false; 1250 1251 // If this type isn't canonical, get the canonical version of it. 1252 QualType Canonical; 1253 if (!isCanonical) { 1254 llvm::SmallVector<QualType, 16> CanonicalArgs; 1255 CanonicalArgs.reserve(NumArgs); 1256 for (unsigned i = 0; i != NumArgs; ++i) 1257 CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); 1258 1259 Canonical = getFunctionType(getCanonicalType(ResultTy), 1260 &CanonicalArgs[0], NumArgs, 1261 isVariadic, TypeQuals); 1262 1263 // Get the new insert position for the node we care about. 1264 FunctionProtoType *NewIP = 1265 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); 1266 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1267 } 1268 1269 // FunctionProtoType objects are allocated with extra bytes after them 1270 // for a variable size array (for parameter types) at the end of them. 1271 FunctionProtoType *FTP = 1272 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + 1273 NumArgs*sizeof(QualType), 8); 1274 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, 1275 TypeQuals, Canonical); 1276 Types.push_back(FTP); 1277 FunctionProtoTypes.InsertNode(FTP, InsertPos); 1278 return QualType(FTP, 0); 1279} 1280 1281/// getTypeDeclType - Return the unique reference to the type for the 1282/// specified type declaration. 1283QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { 1284 assert(Decl && "Passed null for Decl param"); 1285 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 1286 1287 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) 1288 return getTypedefType(Typedef); 1289 else if (isa<TemplateTypeParmDecl>(Decl)) { 1290 assert(false && "Template type parameter types are always available."); 1291 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl)) 1292 return getObjCInterfaceType(ObjCInterface); 1293 1294 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { 1295 if (PrevDecl) 1296 Decl->TypeForDecl = PrevDecl->TypeForDecl; 1297 else 1298 Decl->TypeForDecl = new (*this,8) RecordType(Record); 1299 } 1300 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { 1301 if (PrevDecl) 1302 Decl->TypeForDecl = PrevDecl->TypeForDecl; 1303 else 1304 Decl->TypeForDecl = new (*this,8) EnumType(Enum); 1305 } 1306 else 1307 assert(false && "TypeDecl without a type?"); 1308 1309 if (!PrevDecl) Types.push_back(Decl->TypeForDecl); 1310 return QualType(Decl->TypeForDecl, 0); 1311} 1312 1313/// getTypedefType - Return the unique reference to the type for the 1314/// specified typename decl. 1315QualType ASTContext::getTypedefType(TypedefDecl *Decl) { 1316 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 1317 1318 QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); 1319 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical); 1320 Types.push_back(Decl->TypeForDecl); 1321 return QualType(Decl->TypeForDecl, 0); 1322} 1323 1324/// getObjCInterfaceType - Return the unique reference to the type for the 1325/// specified ObjC interface decl. 1326QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) { 1327 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 1328 1329 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl); 1330 Types.push_back(Decl->TypeForDecl); 1331 return QualType(Decl->TypeForDecl, 0); 1332} 1333 1334/// buildObjCInterfaceType - Returns a new type for the interface 1335/// declaration, regardless. It also removes any previously built 1336/// record declaration so caller can rebuild it. 1337QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) { 1338 const RecordDecl *&RD = ASTRecordForInterface[Decl]; 1339 if (RD) 1340 RD = 0; 1341 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl); 1342 Types.push_back(Decl->TypeForDecl); 1343 return QualType(Decl->TypeForDecl, 0); 1344} 1345 1346/// \brief Retrieve the template type parameter type for a template 1347/// parameter with the given depth, index, and (optionally) name. 1348QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, 1349 IdentifierInfo *Name) { 1350 llvm::FoldingSetNodeID ID; 1351 TemplateTypeParmType::Profile(ID, Depth, Index, Name); 1352 void *InsertPos = 0; 1353 TemplateTypeParmType *TypeParm 1354 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); 1355 1356 if (TypeParm) 1357 return QualType(TypeParm, 0); 1358 1359 if (Name) 1360 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name, 1361 getTemplateTypeParmType(Depth, Index)); 1362 else 1363 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index); 1364 1365 Types.push_back(TypeParm); 1366 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); 1367 1368 return QualType(TypeParm, 0); 1369} 1370 1371QualType 1372ASTContext::getTemplateSpecializationType(TemplateName Template, 1373 const TemplateArgument *Args, 1374 unsigned NumArgs, 1375 QualType Canon) { 1376 // FIXME: If Template is dependent, canonicalize it! 1377 1378 if (!Canon.isNull()) 1379 Canon = getCanonicalType(Canon); 1380 1381 llvm::FoldingSetNodeID ID; 1382 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs); 1383 1384 void *InsertPos = 0; 1385 TemplateSpecializationType *Spec 1386 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); 1387 1388 if (Spec) 1389 return QualType(Spec, 0); 1390 1391 void *Mem = Allocate((sizeof(TemplateSpecializationType) + 1392 sizeof(TemplateArgument) * NumArgs), 1393 8); 1394 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon); 1395 Types.push_back(Spec); 1396 TemplateSpecializationTypes.InsertNode(Spec, InsertPos); 1397 1398 return QualType(Spec, 0); 1399} 1400 1401QualType 1402ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, 1403 QualType NamedType) { 1404 llvm::FoldingSetNodeID ID; 1405 QualifiedNameType::Profile(ID, NNS, NamedType); 1406 1407 void *InsertPos = 0; 1408 QualifiedNameType *T 1409 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); 1410 if (T) 1411 return QualType(T, 0); 1412 1413 T = new (*this) QualifiedNameType(NNS, NamedType, 1414 getCanonicalType(NamedType)); 1415 Types.push_back(T); 1416 QualifiedNameTypes.InsertNode(T, InsertPos); 1417 return QualType(T, 0); 1418} 1419 1420QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, 1421 const IdentifierInfo *Name, 1422 QualType Canon) { 1423 assert(NNS->isDependent() && "nested-name-specifier must be dependent"); 1424 1425 if (Canon.isNull()) { 1426 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 1427 if (CanonNNS != NNS) 1428 Canon = getTypenameType(CanonNNS, Name); 1429 } 1430 1431 llvm::FoldingSetNodeID ID; 1432 TypenameType::Profile(ID, NNS, Name); 1433 1434 void *InsertPos = 0; 1435 TypenameType *T 1436 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); 1437 if (T) 1438 return QualType(T, 0); 1439 1440 T = new (*this) TypenameType(NNS, Name, Canon); 1441 Types.push_back(T); 1442 TypenameTypes.InsertNode(T, InsertPos); 1443 return QualType(T, 0); 1444} 1445 1446/// CmpProtocolNames - Comparison predicate for sorting protocols 1447/// alphabetically. 1448static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, 1449 const ObjCProtocolDecl *RHS) { 1450 return LHS->getDeclName() < RHS->getDeclName(); 1451} 1452 1453static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, 1454 unsigned &NumProtocols) { 1455 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; 1456 1457 // Sort protocols, keyed by name. 1458 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); 1459 1460 // Remove duplicates. 1461 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); 1462 NumProtocols = ProtocolsEnd-Protocols; 1463} 1464 1465 1466/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for 1467/// the given interface decl and the conforming protocol list. 1468QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 1469 ObjCProtocolDecl **Protocols, unsigned NumProtocols) { 1470 // Sort the protocol list alphabetically to canonicalize it. 1471 SortAndUniqueProtocols(Protocols, NumProtocols); 1472 1473 llvm::FoldingSetNodeID ID; 1474 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); 1475 1476 void *InsertPos = 0; 1477 if (ObjCQualifiedInterfaceType *QT = 1478 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) 1479 return QualType(QT, 0); 1480 1481 // No Match; 1482 ObjCQualifiedInterfaceType *QType = 1483 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols); 1484 1485 Types.push_back(QType); 1486 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos); 1487 return QualType(QType, 0); 1488} 1489 1490/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl 1491/// and the conforming protocol list. 1492QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols, 1493 unsigned NumProtocols) { 1494 // Sort the protocol list alphabetically to canonicalize it. 1495 SortAndUniqueProtocols(Protocols, NumProtocols); 1496 1497 llvm::FoldingSetNodeID ID; 1498 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols); 1499 1500 void *InsertPos = 0; 1501 if (ObjCQualifiedIdType *QT = 1502 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos)) 1503 return QualType(QT, 0); 1504 1505 // No Match; 1506 ObjCQualifiedIdType *QType = 1507 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols); 1508 Types.push_back(QType); 1509 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos); 1510 return QualType(QType, 0); 1511} 1512 1513/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique 1514/// TypeOfExprType AST's (since expression's are never shared). For example, 1515/// multiple declarations that refer to "typeof(x)" all contain different 1516/// DeclRefExpr's. This doesn't effect the type checker, since it operates 1517/// on canonical type's (which are always unique). 1518QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { 1519 QualType Canonical = getCanonicalType(tofExpr->getType()); 1520 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical); 1521 Types.push_back(toe); 1522 return QualType(toe, 0); 1523} 1524 1525/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique 1526/// TypeOfType AST's. The only motivation to unique these nodes would be 1527/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be 1528/// an issue. This doesn't effect the type checker, since it operates 1529/// on canonical type's (which are always unique). 1530QualType ASTContext::getTypeOfType(QualType tofType) { 1531 QualType Canonical = getCanonicalType(tofType); 1532 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical); 1533 Types.push_back(tot); 1534 return QualType(tot, 0); 1535} 1536 1537/// getTagDeclType - Return the unique reference to the type for the 1538/// specified TagDecl (struct/union/class/enum) decl. 1539QualType ASTContext::getTagDeclType(TagDecl *Decl) { 1540 assert (Decl); 1541 return getTypeDeclType(Decl); 1542} 1543 1544/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result 1545/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and 1546/// needs to agree with the definition in <stddef.h>. 1547QualType ASTContext::getSizeType() const { 1548 return getFromTargetType(Target.getSizeType()); 1549} 1550 1551/// getSignedWCharType - Return the type of "signed wchar_t". 1552/// Used when in C++, as a GCC extension. 1553QualType ASTContext::getSignedWCharType() const { 1554 // FIXME: derive from "Target" ? 1555 return WCharTy; 1556} 1557 1558/// getUnsignedWCharType - Return the type of "unsigned wchar_t". 1559/// Used when in C++, as a GCC extension. 1560QualType ASTContext::getUnsignedWCharType() const { 1561 // FIXME: derive from "Target" ? 1562 return UnsignedIntTy; 1563} 1564 1565/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 1566/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 1567QualType ASTContext::getPointerDiffType() const { 1568 return getFromTargetType(Target.getPtrDiffType(0)); 1569} 1570 1571//===----------------------------------------------------------------------===// 1572// Type Operators 1573//===----------------------------------------------------------------------===// 1574 1575/// getCanonicalType - Return the canonical (structural) type corresponding to 1576/// the specified potentially non-canonical type. The non-canonical version 1577/// of a type may have many "decorated" versions of types. Decorators can 1578/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 1579/// to be free of any of these, allowing two canonical types to be compared 1580/// for exact equality with a simple pointer comparison. 1581QualType ASTContext::getCanonicalType(QualType T) { 1582 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); 1583 1584 // If the result has type qualifiers, make sure to canonicalize them as well. 1585 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); 1586 if (TypeQuals == 0) return CanType; 1587 1588 // If the type qualifiers are on an array type, get the canonical type of the 1589 // array with the qualifiers applied to the element type. 1590 ArrayType *AT = dyn_cast<ArrayType>(CanType); 1591 if (!AT) 1592 return CanType.getQualifiedType(TypeQuals); 1593 1594 // Get the canonical version of the element with the extra qualifiers on it. 1595 // This can recursively sink qualifiers through multiple levels of arrays. 1596 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); 1597 NewEltTy = getCanonicalType(NewEltTy); 1598 1599 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 1600 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(), 1601 CAT->getIndexTypeQualifier()); 1602 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) 1603 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), 1604 IAT->getIndexTypeQualifier()); 1605 1606 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) 1607 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(), 1608 DSAT->getSizeModifier(), 1609 DSAT->getIndexTypeQualifier()); 1610 1611 VariableArrayType *VAT = cast<VariableArrayType>(AT); 1612 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(), 1613 VAT->getSizeModifier(), 1614 VAT->getIndexTypeQualifier()); 1615} 1616 1617NestedNameSpecifier * 1618ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { 1619 if (!NNS) 1620 return 0; 1621 1622 switch (NNS->getKind()) { 1623 case NestedNameSpecifier::Identifier: 1624 // Canonicalize the prefix but keep the identifier the same. 1625 return NestedNameSpecifier::Create(*this, 1626 getCanonicalNestedNameSpecifier(NNS->getPrefix()), 1627 NNS->getAsIdentifier()); 1628 1629 case NestedNameSpecifier::Namespace: 1630 // A namespace is canonical; build a nested-name-specifier with 1631 // this namespace and no prefix. 1632 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); 1633 1634 case NestedNameSpecifier::TypeSpec: 1635 case NestedNameSpecifier::TypeSpecWithTemplate: { 1636 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); 1637 NestedNameSpecifier *Prefix = 0; 1638 1639 // FIXME: This isn't the right check! 1640 if (T->isDependentType()) 1641 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix()); 1642 1643 return NestedNameSpecifier::Create(*this, Prefix, 1644 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, 1645 T.getTypePtr()); 1646 } 1647 1648 case NestedNameSpecifier::Global: 1649 // The global specifier is canonical and unique. 1650 return NNS; 1651 } 1652 1653 // Required to silence a GCC warning 1654 return 0; 1655} 1656 1657 1658const ArrayType *ASTContext::getAsArrayType(QualType T) { 1659 // Handle the non-qualified case efficiently. 1660 if (T.getCVRQualifiers() == 0) { 1661 // Handle the common positive case fast. 1662 if (const ArrayType *AT = dyn_cast<ArrayType>(T)) 1663 return AT; 1664 } 1665 1666 // Handle the common negative case fast, ignoring CVR qualifiers. 1667 QualType CType = T->getCanonicalTypeInternal(); 1668 1669 // Make sure to look through type qualifiers (like ExtQuals) for the negative 1670 // test. 1671 if (!isa<ArrayType>(CType) && 1672 !isa<ArrayType>(CType.getUnqualifiedType())) 1673 return 0; 1674 1675 // Apply any CVR qualifiers from the array type to the element type. This 1676 // implements C99 6.7.3p8: "If the specification of an array type includes 1677 // any type qualifiers, the element type is so qualified, not the array type." 1678 1679 // If we get here, we either have type qualifiers on the type, or we have 1680 // sugar such as a typedef in the way. If we have type qualifiers on the type 1681 // we must propagate them down into the elemeng type. 1682 unsigned CVRQuals = T.getCVRQualifiers(); 1683 unsigned AddrSpace = 0; 1684 Type *Ty = T.getTypePtr(); 1685 1686 // Rip through ExtQualType's and typedefs to get to a concrete type. 1687 while (1) { 1688 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) { 1689 AddrSpace = EXTQT->getAddressSpace(); 1690 Ty = EXTQT->getBaseType(); 1691 } else { 1692 T = Ty->getDesugaredType(); 1693 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) 1694 break; 1695 CVRQuals |= T.getCVRQualifiers(); 1696 Ty = T.getTypePtr(); 1697 } 1698 } 1699 1700 // If we have a simple case, just return now. 1701 const ArrayType *ATy = dyn_cast<ArrayType>(Ty); 1702 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) 1703 return ATy; 1704 1705 // Otherwise, we have an array and we have qualifiers on it. Push the 1706 // qualifiers into the array element type and return a new array type. 1707 // Get the canonical version of the element with the extra qualifiers on it. 1708 // This can recursively sink qualifiers through multiple levels of arrays. 1709 QualType NewEltTy = ATy->getElementType(); 1710 if (AddrSpace) 1711 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace); 1712 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); 1713 1714 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) 1715 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), 1716 CAT->getSizeModifier(), 1717 CAT->getIndexTypeQualifier())); 1718 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) 1719 return cast<ArrayType>(getIncompleteArrayType(NewEltTy, 1720 IAT->getSizeModifier(), 1721 IAT->getIndexTypeQualifier())); 1722 1723 if (const DependentSizedArrayType *DSAT 1724 = dyn_cast<DependentSizedArrayType>(ATy)) 1725 return cast<ArrayType>( 1726 getDependentSizedArrayType(NewEltTy, 1727 DSAT->getSizeExpr(), 1728 DSAT->getSizeModifier(), 1729 DSAT->getIndexTypeQualifier())); 1730 1731 const VariableArrayType *VAT = cast<VariableArrayType>(ATy); 1732 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(), 1733 VAT->getSizeModifier(), 1734 VAT->getIndexTypeQualifier())); 1735} 1736 1737 1738/// getArrayDecayedType - Return the properly qualified result of decaying the 1739/// specified array type to a pointer. This operation is non-trivial when 1740/// handling typedefs etc. The canonical type of "T" must be an array type, 1741/// this returns a pointer to a properly qualified element of the array. 1742/// 1743/// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1744QualType ASTContext::getArrayDecayedType(QualType Ty) { 1745 // Get the element type with 'getAsArrayType' so that we don't lose any 1746 // typedefs in the element type of the array. This also handles propagation 1747 // of type qualifiers from the array type into the element type if present 1748 // (C99 6.7.3p8). 1749 const ArrayType *PrettyArrayType = getAsArrayType(Ty); 1750 assert(PrettyArrayType && "Not an array type!"); 1751 1752 QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); 1753 1754 // int x[restrict 4] -> int *restrict 1755 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); 1756} 1757 1758QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) { 1759 QualType ElemTy = VAT->getElementType(); 1760 1761 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy)) 1762 return getBaseElementType(VAT); 1763 1764 return ElemTy; 1765} 1766 1767/// getFloatingRank - Return a relative rank for floating point types. 1768/// This routine will assert if passed a built-in type that isn't a float. 1769static FloatingRank getFloatingRank(QualType T) { 1770 if (const ComplexType *CT = T->getAsComplexType()) 1771 return getFloatingRank(CT->getElementType()); 1772 1773 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type"); 1774 switch (T->getAsBuiltinType()->getKind()) { 1775 default: assert(0 && "getFloatingRank(): not a floating type"); 1776 case BuiltinType::Float: return FloatRank; 1777 case BuiltinType::Double: return DoubleRank; 1778 case BuiltinType::LongDouble: return LongDoubleRank; 1779 } 1780} 1781 1782/// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1783/// point or a complex type (based on typeDomain/typeSize). 1784/// 'typeDomain' is a real floating point or complex type. 1785/// 'typeSize' is a real floating point or complex type. 1786QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, 1787 QualType Domain) const { 1788 FloatingRank EltRank = getFloatingRank(Size); 1789 if (Domain->isComplexType()) { 1790 switch (EltRank) { 1791 default: assert(0 && "getFloatingRank(): illegal value for rank"); 1792 case FloatRank: return FloatComplexTy; 1793 case DoubleRank: return DoubleComplexTy; 1794 case LongDoubleRank: return LongDoubleComplexTy; 1795 } 1796 } 1797 1798 assert(Domain->isRealFloatingType() && "Unknown domain!"); 1799 switch (EltRank) { 1800 default: assert(0 && "getFloatingRank(): illegal value for rank"); 1801 case FloatRank: return FloatTy; 1802 case DoubleRank: return DoubleTy; 1803 case LongDoubleRank: return LongDoubleTy; 1804 } 1805} 1806 1807/// getFloatingTypeOrder - Compare the rank of the two specified floating 1808/// point types, ignoring the domain of the type (i.e. 'double' == 1809/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1810/// LHS < RHS, return -1. 1811int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { 1812 FloatingRank LHSR = getFloatingRank(LHS); 1813 FloatingRank RHSR = getFloatingRank(RHS); 1814 1815 if (LHSR == RHSR) 1816 return 0; 1817 if (LHSR > RHSR) 1818 return 1; 1819 return -1; 1820} 1821 1822/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This 1823/// routine will assert if passed a built-in type that isn't an integer or enum, 1824/// or if it is not canonicalized. 1825unsigned ASTContext::getIntegerRank(Type *T) { 1826 assert(T->isCanonical() && "T should be canonicalized"); 1827 if (EnumType* ET = dyn_cast<EnumType>(T)) 1828 T = ET->getDecl()->getIntegerType().getTypePtr(); 1829 1830 // There are two things which impact the integer rank: the width, and 1831 // the ordering of builtins. The builtin ordering is encoded in the 1832 // bottom three bits; the width is encoded in the bits above that. 1833 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) { 1834 return FWIT->getWidth() << 3; 1835 } 1836 1837 switch (cast<BuiltinType>(T)->getKind()) { 1838 default: assert(0 && "getIntegerRank(): not a built-in integer"); 1839 case BuiltinType::Bool: 1840 return 1 + (getIntWidth(BoolTy) << 3); 1841 case BuiltinType::Char_S: 1842 case BuiltinType::Char_U: 1843 case BuiltinType::SChar: 1844 case BuiltinType::UChar: 1845 return 2 + (getIntWidth(CharTy) << 3); 1846 case BuiltinType::Short: 1847 case BuiltinType::UShort: 1848 return 3 + (getIntWidth(ShortTy) << 3); 1849 case BuiltinType::Int: 1850 case BuiltinType::UInt: 1851 return 4 + (getIntWidth(IntTy) << 3); 1852 case BuiltinType::Long: 1853 case BuiltinType::ULong: 1854 return 5 + (getIntWidth(LongTy) << 3); 1855 case BuiltinType::LongLong: 1856 case BuiltinType::ULongLong: 1857 return 6 + (getIntWidth(LongLongTy) << 3); 1858 } 1859} 1860 1861/// getIntegerTypeOrder - Returns the highest ranked integer type: 1862/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1863/// LHS < RHS, return -1. 1864int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { 1865 Type *LHSC = getCanonicalType(LHS).getTypePtr(); 1866 Type *RHSC = getCanonicalType(RHS).getTypePtr(); 1867 if (LHSC == RHSC) return 0; 1868 1869 bool LHSUnsigned = LHSC->isUnsignedIntegerType(); 1870 bool RHSUnsigned = RHSC->isUnsignedIntegerType(); 1871 1872 unsigned LHSRank = getIntegerRank(LHSC); 1873 unsigned RHSRank = getIntegerRank(RHSC); 1874 1875 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. 1876 if (LHSRank == RHSRank) return 0; 1877 return LHSRank > RHSRank ? 1 : -1; 1878 } 1879 1880 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. 1881 if (LHSUnsigned) { 1882 // If the unsigned [LHS] type is larger, return it. 1883 if (LHSRank >= RHSRank) 1884 return 1; 1885 1886 // If the signed type can represent all values of the unsigned type, it 1887 // wins. Because we are dealing with 2's complement and types that are 1888 // powers of two larger than each other, this is always safe. 1889 return -1; 1890 } 1891 1892 // If the unsigned [RHS] type is larger, return it. 1893 if (RHSRank >= LHSRank) 1894 return -1; 1895 1896 // If the signed type can represent all values of the unsigned type, it 1897 // wins. Because we are dealing with 2's complement and types that are 1898 // powers of two larger than each other, this is always safe. 1899 return 1; 1900} 1901 1902// getCFConstantStringType - Return the type used for constant CFStrings. 1903QualType ASTContext::getCFConstantStringType() { 1904 if (!CFConstantStringTypeDecl) { 1905 CFConstantStringTypeDecl = 1906 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 1907 &Idents.get("NSConstantString")); 1908 QualType FieldTypes[4]; 1909 1910 // const int *isa; 1911 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); 1912 // int flags; 1913 FieldTypes[1] = IntTy; 1914 // const char *str; 1915 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); 1916 // long length; 1917 FieldTypes[3] = LongTy; 1918 1919 // Create fields 1920 for (unsigned i = 0; i < 4; ++i) { 1921 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, 1922 SourceLocation(), 0, 1923 FieldTypes[i], /*BitWidth=*/0, 1924 /*Mutable=*/false); 1925 CFConstantStringTypeDecl->addDecl(Field); 1926 } 1927 1928 CFConstantStringTypeDecl->completeDefinition(*this); 1929 } 1930 1931 return getTagDeclType(CFConstantStringTypeDecl); 1932} 1933 1934QualType ASTContext::getObjCFastEnumerationStateType() 1935{ 1936 if (!ObjCFastEnumerationStateTypeDecl) { 1937 ObjCFastEnumerationStateTypeDecl = 1938 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 1939 &Idents.get("__objcFastEnumerationState")); 1940 1941 QualType FieldTypes[] = { 1942 UnsignedLongTy, 1943 getPointerType(ObjCIdType), 1944 getPointerType(UnsignedLongTy), 1945 getConstantArrayType(UnsignedLongTy, 1946 llvm::APInt(32, 5), ArrayType::Normal, 0) 1947 }; 1948 1949 for (size_t i = 0; i < 4; ++i) { 1950 FieldDecl *Field = FieldDecl::Create(*this, 1951 ObjCFastEnumerationStateTypeDecl, 1952 SourceLocation(), 0, 1953 FieldTypes[i], /*BitWidth=*/0, 1954 /*Mutable=*/false); 1955 ObjCFastEnumerationStateTypeDecl->addDecl(Field); 1956 } 1957 1958 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); 1959 } 1960 1961 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 1962} 1963 1964// This returns true if a type has been typedefed to BOOL: 1965// typedef <type> BOOL; 1966static bool isTypeTypedefedAsBOOL(QualType T) { 1967 if (const TypedefType *TT = dyn_cast<TypedefType>(T)) 1968 if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) 1969 return II->isStr("BOOL"); 1970 1971 return false; 1972} 1973 1974/// getObjCEncodingTypeSize returns size of type for objective-c encoding 1975/// purpose. 1976int ASTContext::getObjCEncodingTypeSize(QualType type) { 1977 uint64_t sz = getTypeSize(type); 1978 1979 // Make all integer and enum types at least as large as an int 1980 if (sz > 0 && type->isIntegralType()) 1981 sz = std::max(sz, getTypeSize(IntTy)); 1982 // Treat arrays as pointers, since that's how they're passed in. 1983 else if (type->isArrayType()) 1984 sz = getTypeSize(VoidPtrTy); 1985 return sz / getTypeSize(CharTy); 1986} 1987 1988/// getObjCEncodingForMethodDecl - Return the encoded type for this method 1989/// declaration. 1990void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, 1991 std::string& S) { 1992 // FIXME: This is not very efficient. 1993 // Encode type qualifer, 'in', 'inout', etc. for the return type. 1994 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); 1995 // Encode result type. 1996 getObjCEncodingForType(Decl->getResultType(), S); 1997 // Compute size of all parameters. 1998 // Start with computing size of a pointer in number of bytes. 1999 // FIXME: There might(should) be a better way of doing this computation! 2000 SourceLocation Loc; 2001 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); 2002 // The first two arguments (self and _cmd) are pointers; account for 2003 // their size. 2004 int ParmOffset = 2 * PtrSize; 2005 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), 2006 E = Decl->param_end(); PI != E; ++PI) { 2007 QualType PType = (*PI)->getType(); 2008 int sz = getObjCEncodingTypeSize(PType); 2009 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); 2010 ParmOffset += sz; 2011 } 2012 S += llvm::utostr(ParmOffset); 2013 S += "@0:"; 2014 S += llvm::utostr(PtrSize); 2015 2016 // Argument types. 2017 ParmOffset = 2 * PtrSize; 2018 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), 2019 E = Decl->param_end(); PI != E; ++PI) { 2020 ParmVarDecl *PVDecl = *PI; 2021 QualType PType = PVDecl->getOriginalType(); 2022 if (const ArrayType *AT = 2023 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) 2024 // Use array's original type only if it has known number of 2025 // elements. 2026 if (!dyn_cast<ConstantArrayType>(AT)) 2027 PType = PVDecl->getType(); 2028 // Process argument qualifiers for user supplied arguments; such as, 2029 // 'in', 'inout', etc. 2030 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); 2031 getObjCEncodingForType(PType, S); 2032 S += llvm::utostr(ParmOffset); 2033 ParmOffset += getObjCEncodingTypeSize(PType); 2034 } 2035} 2036 2037/// getObjCEncodingForPropertyDecl - Return the encoded type for this 2038/// property declaration. If non-NULL, Container must be either an 2039/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be 2040/// NULL when getting encodings for protocol properties. 2041/// Property attributes are stored as a comma-delimited C string. The simple 2042/// attributes readonly and bycopy are encoded as single characters. The 2043/// parametrized attributes, getter=name, setter=name, and ivar=name, are 2044/// encoded as single characters, followed by an identifier. Property types 2045/// are also encoded as a parametrized attribute. The characters used to encode 2046/// these attributes are defined by the following enumeration: 2047/// @code 2048/// enum PropertyAttributes { 2049/// kPropertyReadOnly = 'R', // property is read-only. 2050/// kPropertyBycopy = 'C', // property is a copy of the value last assigned 2051/// kPropertyByref = '&', // property is a reference to the value last assigned 2052/// kPropertyDynamic = 'D', // property is dynamic 2053/// kPropertyGetter = 'G', // followed by getter selector name 2054/// kPropertySetter = 'S', // followed by setter selector name 2055/// kPropertyInstanceVariable = 'V' // followed by instance variable name 2056/// kPropertyType = 't' // followed by old-style type encoding. 2057/// kPropertyWeak = 'W' // 'weak' property 2058/// kPropertyStrong = 'P' // property GC'able 2059/// kPropertyNonAtomic = 'N' // property non-atomic 2060/// }; 2061/// @endcode 2062void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 2063 const Decl *Container, 2064 std::string& S) { 2065 // Collect information from the property implementation decl(s). 2066 bool Dynamic = false; 2067 ObjCPropertyImplDecl *SynthesizePID = 0; 2068 2069 // FIXME: Duplicated code due to poor abstraction. 2070 if (Container) { 2071 if (const ObjCCategoryImplDecl *CID = 2072 dyn_cast<ObjCCategoryImplDecl>(Container)) { 2073 for (ObjCCategoryImplDecl::propimpl_iterator 2074 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) { 2075 ObjCPropertyImplDecl *PID = *i; 2076 if (PID->getPropertyDecl() == PD) { 2077 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 2078 Dynamic = true; 2079 } else { 2080 SynthesizePID = PID; 2081 } 2082 } 2083 } 2084 } else { 2085 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); 2086 for (ObjCCategoryImplDecl::propimpl_iterator 2087 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) { 2088 ObjCPropertyImplDecl *PID = *i; 2089 if (PID->getPropertyDecl() == PD) { 2090 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 2091 Dynamic = true; 2092 } else { 2093 SynthesizePID = PID; 2094 } 2095 } 2096 } 2097 } 2098 } 2099 2100 // FIXME: This is not very efficient. 2101 S = "T"; 2102 2103 // Encode result type. 2104 // GCC has some special rules regarding encoding of properties which 2105 // closely resembles encoding of ivars. 2106 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL, 2107 true /* outermost type */, 2108 true /* encoding for property */); 2109 2110 if (PD->isReadOnly()) { 2111 S += ",R"; 2112 } else { 2113 switch (PD->getSetterKind()) { 2114 case ObjCPropertyDecl::Assign: break; 2115 case ObjCPropertyDecl::Copy: S += ",C"; break; 2116 case ObjCPropertyDecl::Retain: S += ",&"; break; 2117 } 2118 } 2119 2120 // It really isn't clear at all what this means, since properties 2121 // are "dynamic by default". 2122 if (Dynamic) 2123 S += ",D"; 2124 2125 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) 2126 S += ",N"; 2127 2128 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 2129 S += ",G"; 2130 S += PD->getGetterName().getAsString(); 2131 } 2132 2133 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 2134 S += ",S"; 2135 S += PD->getSetterName().getAsString(); 2136 } 2137 2138 if (SynthesizePID) { 2139 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); 2140 S += ",V"; 2141 S += OID->getNameAsString(); 2142 } 2143 2144 // FIXME: OBJCGC: weak & strong 2145} 2146 2147/// getLegacyIntegralTypeEncoding - 2148/// Another legacy compatibility encoding: 32-bit longs are encoded as 2149/// 'l' or 'L' , but not always. For typedefs, we need to use 2150/// 'i' or 'I' instead if encoding a struct field, or a pointer! 2151/// 2152void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { 2153 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) { 2154 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) { 2155 if (BT->getKind() == BuiltinType::ULong && 2156 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) 2157 PointeeTy = UnsignedIntTy; 2158 else 2159 if (BT->getKind() == BuiltinType::Long && 2160 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) 2161 PointeeTy = IntTy; 2162 } 2163 } 2164} 2165 2166void ASTContext::getObjCEncodingForType(QualType T, std::string& S, 2167 FieldDecl *Field) const { 2168 // We follow the behavior of gcc, expanding structures which are 2169 // directly pointed to, and expanding embedded structures. Note that 2170 // these rules are sufficient to prevent recursive encoding of the 2171 // same type. 2172 getObjCEncodingForTypeImpl(T, S, true, true, Field, 2173 true /* outermost type */); 2174} 2175 2176static void EncodeBitField(const ASTContext *Context, std::string& S, 2177 FieldDecl *FD) { 2178 const Expr *E = FD->getBitWidth(); 2179 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); 2180 ASTContext *Ctx = const_cast<ASTContext*>(Context); 2181 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue(); 2182 S += 'b'; 2183 S += llvm::utostr(N); 2184} 2185 2186void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, 2187 bool ExpandPointedToStructures, 2188 bool ExpandStructures, 2189 FieldDecl *FD, 2190 bool OutermostType, 2191 bool EncodingProperty) const { 2192 if (const BuiltinType *BT = T->getAsBuiltinType()) { 2193 if (FD && FD->isBitField()) { 2194 EncodeBitField(this, S, FD); 2195 } 2196 else { 2197 char encoding; 2198 switch (BT->getKind()) { 2199 default: assert(0 && "Unhandled builtin type kind"); 2200 case BuiltinType::Void: encoding = 'v'; break; 2201 case BuiltinType::Bool: encoding = 'B'; break; 2202 case BuiltinType::Char_U: 2203 case BuiltinType::UChar: encoding = 'C'; break; 2204 case BuiltinType::UShort: encoding = 'S'; break; 2205 case BuiltinType::UInt: encoding = 'I'; break; 2206 case BuiltinType::ULong: 2207 encoding = 2208 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; 2209 break; 2210 case BuiltinType::ULongLong: encoding = 'Q'; break; 2211 case BuiltinType::Char_S: 2212 case BuiltinType::SChar: encoding = 'c'; break; 2213 case BuiltinType::Short: encoding = 's'; break; 2214 case BuiltinType::Int: encoding = 'i'; break; 2215 case BuiltinType::Long: 2216 encoding = 2217 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; 2218 break; 2219 case BuiltinType::LongLong: encoding = 'q'; break; 2220 case BuiltinType::Float: encoding = 'f'; break; 2221 case BuiltinType::Double: encoding = 'd'; break; 2222 case BuiltinType::LongDouble: encoding = 'd'; break; 2223 } 2224 2225 S += encoding; 2226 } 2227 } 2228 else if (T->isObjCQualifiedIdType()) { 2229 getObjCEncodingForTypeImpl(getObjCIdType(), S, 2230 ExpandPointedToStructures, 2231 ExpandStructures, FD); 2232 if (FD || EncodingProperty) { 2233 // Note that we do extended encoding of protocol qualifer list 2234 // Only when doing ivar or property encoding. 2235 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType(); 2236 S += '"'; 2237 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) { 2238 ObjCProtocolDecl *Proto = QIDT->getProtocols(i); 2239 S += '<'; 2240 S += Proto->getNameAsString(); 2241 S += '>'; 2242 } 2243 S += '"'; 2244 } 2245 return; 2246 } 2247 else if (const PointerType *PT = T->getAsPointerType()) { 2248 QualType PointeeTy = PT->getPointeeType(); 2249 bool isReadOnly = false; 2250 // For historical/compatibility reasons, the read-only qualifier of the 2251 // pointee gets emitted _before_ the '^'. The read-only qualifier of 2252 // the pointer itself gets ignored, _unless_ we are looking at a typedef! 2253 // Also, do not emit the 'r' for anything but the outermost type! 2254 if (dyn_cast<TypedefType>(T.getTypePtr())) { 2255 if (OutermostType && T.isConstQualified()) { 2256 isReadOnly = true; 2257 S += 'r'; 2258 } 2259 } 2260 else if (OutermostType) { 2261 QualType P = PointeeTy; 2262 while (P->getAsPointerType()) 2263 P = P->getAsPointerType()->getPointeeType(); 2264 if (P.isConstQualified()) { 2265 isReadOnly = true; 2266 S += 'r'; 2267 } 2268 } 2269 if (isReadOnly) { 2270 // Another legacy compatibility encoding. Some ObjC qualifier and type 2271 // combinations need to be rearranged. 2272 // Rewrite "in const" from "nr" to "rn" 2273 const char * s = S.c_str(); 2274 int len = S.length(); 2275 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { 2276 std::string replace = "rn"; 2277 S.replace(S.end()-2, S.end(), replace); 2278 } 2279 } 2280 if (isObjCIdStructType(PointeeTy)) { 2281 S += '@'; 2282 return; 2283 } 2284 else if (PointeeTy->isObjCInterfaceType()) { 2285 if (!EncodingProperty && 2286 isa<TypedefType>(PointeeTy.getTypePtr())) { 2287 // Another historical/compatibility reason. 2288 // We encode the underlying type which comes out as 2289 // {...}; 2290 S += '^'; 2291 getObjCEncodingForTypeImpl(PointeeTy, S, 2292 false, ExpandPointedToStructures, 2293 NULL); 2294 return; 2295 } 2296 S += '@'; 2297 if (FD || EncodingProperty) { 2298 const ObjCInterfaceType *OIT = 2299 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType(); 2300 ObjCInterfaceDecl *OI = OIT->getDecl(); 2301 S += '"'; 2302 S += OI->getNameAsCString(); 2303 for (unsigned i =0; i < OIT->getNumProtocols(); i++) { 2304 ObjCProtocolDecl *Proto = OIT->getProtocol(i); 2305 S += '<'; 2306 S += Proto->getNameAsString(); 2307 S += '>'; 2308 } 2309 S += '"'; 2310 } 2311 return; 2312 } else if (isObjCClassStructType(PointeeTy)) { 2313 S += '#'; 2314 return; 2315 } else if (isObjCSelType(PointeeTy)) { 2316 S += ':'; 2317 return; 2318 } 2319 2320 if (PointeeTy->isCharType()) { 2321 // char pointer types should be encoded as '*' unless it is a 2322 // type that has been typedef'd to 'BOOL'. 2323 if (!isTypeTypedefedAsBOOL(PointeeTy)) { 2324 S += '*'; 2325 return; 2326 } 2327 } 2328 2329 S += '^'; 2330 getLegacyIntegralTypeEncoding(PointeeTy); 2331 2332 getObjCEncodingForTypeImpl(PointeeTy, S, 2333 false, ExpandPointedToStructures, 2334 NULL); 2335 } else if (const ArrayType *AT = 2336 // Ignore type qualifiers etc. 2337 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { 2338 if (isa<IncompleteArrayType>(AT)) { 2339 // Incomplete arrays are encoded as a pointer to the array element. 2340 S += '^'; 2341 2342 getObjCEncodingForTypeImpl(AT->getElementType(), S, 2343 false, ExpandStructures, FD); 2344 } else { 2345 S += '['; 2346 2347 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 2348 S += llvm::utostr(CAT->getSize().getZExtValue()); 2349 else { 2350 //Variable length arrays are encoded as a regular array with 0 elements. 2351 assert(isa<VariableArrayType>(AT) && "Unknown array type!"); 2352 S += '0'; 2353 } 2354 2355 getObjCEncodingForTypeImpl(AT->getElementType(), S, 2356 false, ExpandStructures, FD); 2357 S += ']'; 2358 } 2359 } else if (T->getAsFunctionType()) { 2360 S += '?'; 2361 } else if (const RecordType *RTy = T->getAsRecordType()) { 2362 RecordDecl *RDecl = RTy->getDecl(); 2363 S += RDecl->isUnion() ? '(' : '{'; 2364 // Anonymous structures print as '?' 2365 if (const IdentifierInfo *II = RDecl->getIdentifier()) { 2366 S += II->getName(); 2367 } else { 2368 S += '?'; 2369 } 2370 if (ExpandStructures) { 2371 S += '='; 2372 for (RecordDecl::field_iterator Field = RDecl->field_begin(), 2373 FieldEnd = RDecl->field_end(); 2374 Field != FieldEnd; ++Field) { 2375 if (FD) { 2376 S += '"'; 2377 S += Field->getNameAsString(); 2378 S += '"'; 2379 } 2380 2381 // Special case bit-fields. 2382 if (Field->isBitField()) { 2383 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, 2384 (*Field)); 2385 } else { 2386 QualType qt = Field->getType(); 2387 getLegacyIntegralTypeEncoding(qt); 2388 getObjCEncodingForTypeImpl(qt, S, false, true, 2389 FD); 2390 } 2391 } 2392 } 2393 S += RDecl->isUnion() ? ')' : '}'; 2394 } else if (T->isEnumeralType()) { 2395 if (FD && FD->isBitField()) 2396 EncodeBitField(this, S, FD); 2397 else 2398 S += 'i'; 2399 } else if (T->isBlockPointerType()) { 2400 S += "@?"; // Unlike a pointer-to-function, which is "^?". 2401 } else if (T->isObjCInterfaceType()) { 2402 // @encode(class_name) 2403 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl(); 2404 S += '{'; 2405 const IdentifierInfo *II = OI->getIdentifier(); 2406 S += II->getName(); 2407 S += '='; 2408 std::vector<FieldDecl*> RecFields; 2409 CollectObjCIvars(OI, RecFields); 2410 for (unsigned int i = 0; i != RecFields.size(); i++) { 2411 if (RecFields[i]->isBitField()) 2412 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, 2413 RecFields[i]); 2414 else 2415 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, 2416 FD); 2417 } 2418 S += '}'; 2419 } 2420 else 2421 assert(0 && "@encode for type not implemented!"); 2422} 2423 2424void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 2425 std::string& S) const { 2426 if (QT & Decl::OBJC_TQ_In) 2427 S += 'n'; 2428 if (QT & Decl::OBJC_TQ_Inout) 2429 S += 'N'; 2430 if (QT & Decl::OBJC_TQ_Out) 2431 S += 'o'; 2432 if (QT & Decl::OBJC_TQ_Bycopy) 2433 S += 'O'; 2434 if (QT & Decl::OBJC_TQ_Byref) 2435 S += 'R'; 2436 if (QT & Decl::OBJC_TQ_Oneway) 2437 S += 'V'; 2438} 2439 2440void ASTContext::setBuiltinVaListType(QualType T) 2441{ 2442 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); 2443 2444 BuiltinVaListType = T; 2445} 2446 2447void ASTContext::setObjCIdType(TypedefDecl *TD) 2448{ 2449 ObjCIdType = getTypedefType(TD); 2450 2451 // typedef struct objc_object *id; 2452 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); 2453 // User error - caller will issue diagnostics. 2454 if (!ptr) 2455 return; 2456 const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 2457 // User error - caller will issue diagnostics. 2458 if (!rec) 2459 return; 2460 IdStructType = rec; 2461} 2462 2463void ASTContext::setObjCSelType(TypedefDecl *TD) 2464{ 2465 ObjCSelType = getTypedefType(TD); 2466 2467 // typedef struct objc_selector *SEL; 2468 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); 2469 if (!ptr) 2470 return; 2471 const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 2472 if (!rec) 2473 return; 2474 SelStructType = rec; 2475} 2476 2477void ASTContext::setObjCProtoType(QualType QT) 2478{ 2479 ObjCProtoType = QT; 2480} 2481 2482void ASTContext::setObjCClassType(TypedefDecl *TD) 2483{ 2484 ObjCClassType = getTypedefType(TD); 2485 2486 // typedef struct objc_class *Class; 2487 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); 2488 assert(ptr && "'Class' incorrectly typed"); 2489 const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 2490 assert(rec && "'Class' incorrectly typed"); 2491 ClassStructType = rec; 2492} 2493 2494void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { 2495 assert(ObjCConstantStringType.isNull() && 2496 "'NSConstantString' type already set!"); 2497 2498 ObjCConstantStringType = getObjCInterfaceType(Decl); 2499} 2500 2501/// \brief Retrieve the template name that represents a qualified 2502/// template name such as \c std::vector. 2503TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, 2504 bool TemplateKeyword, 2505 TemplateDecl *Template) { 2506 llvm::FoldingSetNodeID ID; 2507 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); 2508 2509 void *InsertPos = 0; 2510 QualifiedTemplateName *QTN = 2511 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 2512 if (!QTN) { 2513 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); 2514 QualifiedTemplateNames.InsertNode(QTN, InsertPos); 2515 } 2516 2517 return TemplateName(QTN); 2518} 2519 2520/// \brief Retrieve the template name that represents a dependent 2521/// template name such as \c MetaFun::template apply. 2522TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, 2523 const IdentifierInfo *Name) { 2524 assert(NNS->isDependent() && "Nested name specifier must be dependent"); 2525 2526 llvm::FoldingSetNodeID ID; 2527 DependentTemplateName::Profile(ID, NNS, Name); 2528 2529 void *InsertPos = 0; 2530 DependentTemplateName *QTN = 2531 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 2532 2533 if (QTN) 2534 return TemplateName(QTN); 2535 2536 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 2537 if (CanonNNS == NNS) { 2538 QTN = new (*this,4) DependentTemplateName(NNS, Name); 2539 } else { 2540 TemplateName Canon = getDependentTemplateName(CanonNNS, Name); 2541 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); 2542 } 2543 2544 DependentTemplateNames.InsertNode(QTN, InsertPos); 2545 return TemplateName(QTN); 2546} 2547 2548/// getFromTargetType - Given one of the integer types provided by 2549/// TargetInfo, produce the corresponding type. The unsigned @p Type 2550/// is actually a value of type @c TargetInfo::IntType. 2551QualType ASTContext::getFromTargetType(unsigned Type) const { 2552 switch (Type) { 2553 case TargetInfo::NoInt: return QualType(); 2554 case TargetInfo::SignedShort: return ShortTy; 2555 case TargetInfo::UnsignedShort: return UnsignedShortTy; 2556 case TargetInfo::SignedInt: return IntTy; 2557 case TargetInfo::UnsignedInt: return UnsignedIntTy; 2558 case TargetInfo::SignedLong: return LongTy; 2559 case TargetInfo::UnsignedLong: return UnsignedLongTy; 2560 case TargetInfo::SignedLongLong: return LongLongTy; 2561 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; 2562 } 2563 2564 assert(false && "Unhandled TargetInfo::IntType value"); 2565 return QualType(); 2566} 2567 2568//===----------------------------------------------------------------------===// 2569// Type Predicates. 2570//===----------------------------------------------------------------------===// 2571 2572/// isObjCNSObjectType - Return true if this is an NSObject object using 2573/// NSObject attribute on a c-style pointer type. 2574/// FIXME - Make it work directly on types. 2575/// 2576bool ASTContext::isObjCNSObjectType(QualType Ty) const { 2577 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { 2578 if (TypedefDecl *TD = TDT->getDecl()) 2579 if (TD->getAttr<ObjCNSObjectAttr>()) 2580 return true; 2581 } 2582 return false; 2583} 2584 2585/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 2586/// to an object type. This includes "id" and "Class" (two 'special' pointers 2587/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 2588/// ID type). 2589bool ASTContext::isObjCObjectPointerType(QualType Ty) const { 2590 if (Ty->isObjCQualifiedIdType()) 2591 return true; 2592 2593 // Blocks are objects. 2594 if (Ty->isBlockPointerType()) 2595 return true; 2596 2597 // All other object types are pointers. 2598 if (!Ty->isPointerType()) 2599 return false; 2600 2601 // Check to see if this is 'id' or 'Class', both of which are typedefs for 2602 // pointer types. This looks for the typedef specifically, not for the 2603 // underlying type. 2604 if (Ty.getUnqualifiedType() == getObjCIdType() || 2605 Ty.getUnqualifiedType() == getObjCClassType()) 2606 return true; 2607 2608 // If this a pointer to an interface (e.g. NSString*), it is ok. 2609 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType()) 2610 return true; 2611 2612 // If is has NSObject attribute, OK as well. 2613 return isObjCNSObjectType(Ty); 2614} 2615 2616/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 2617/// garbage collection attribute. 2618/// 2619QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const { 2620 QualType::GCAttrTypes GCAttrs = QualType::GCNone; 2621 if (getLangOptions().ObjC1 && 2622 getLangOptions().getGCMode() != LangOptions::NonGC) { 2623 GCAttrs = Ty.getObjCGCAttr(); 2624 // Default behavious under objective-c's gc is for objective-c pointers 2625 // (or pointers to them) be treated as though they were declared 2626 // as __strong. 2627 if (GCAttrs == QualType::GCNone) { 2628 if (isObjCObjectPointerType(Ty)) 2629 GCAttrs = QualType::Strong; 2630 else if (Ty->isPointerType()) 2631 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType()); 2632 } 2633 } 2634 return GCAttrs; 2635} 2636 2637//===----------------------------------------------------------------------===// 2638// Type Compatibility Testing 2639//===----------------------------------------------------------------------===// 2640 2641/// typesAreBlockCompatible - This routine is called when comparing two 2642/// block types. Types must be strictly compatible here. For example, 2643/// C unfortunately doesn't produce an error for the following: 2644/// 2645/// int (*emptyArgFunc)(); 2646/// int (*intArgList)(int) = emptyArgFunc; 2647/// 2648/// For blocks, we will produce an error for the following (similar to C++): 2649/// 2650/// int (^emptyArgBlock)(); 2651/// int (^intArgBlock)(int) = emptyArgBlock; 2652/// 2653/// FIXME: When the dust settles on this integration, fold this into mergeTypes. 2654/// 2655bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) { 2656 const FunctionType *lbase = lhs->getAsFunctionType(); 2657 const FunctionType *rbase = rhs->getAsFunctionType(); 2658 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); 2659 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); 2660 if (lproto && rproto) 2661 return !mergeTypes(lhs, rhs).isNull(); 2662 return false; 2663} 2664 2665/// areCompatVectorTypes - Return true if the two specified vector types are 2666/// compatible. 2667static bool areCompatVectorTypes(const VectorType *LHS, 2668 const VectorType *RHS) { 2669 assert(LHS->isCanonical() && RHS->isCanonical()); 2670 return LHS->getElementType() == RHS->getElementType() && 2671 LHS->getNumElements() == RHS->getNumElements(); 2672} 2673 2674/// canAssignObjCInterfaces - Return true if the two interface types are 2675/// compatible for assignment from RHS to LHS. This handles validation of any 2676/// protocol qualifiers on the LHS or RHS. 2677/// 2678bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 2679 const ObjCInterfaceType *RHS) { 2680 // Verify that the base decls are compatible: the RHS must be a subclass of 2681 // the LHS. 2682 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) 2683 return false; 2684 2685 // RHS must have a superset of the protocols in the LHS. If the LHS is not 2686 // protocol qualified at all, then we are good. 2687 if (!isa<ObjCQualifiedInterfaceType>(LHS)) 2688 return true; 2689 2690 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it 2691 // isn't a superset. 2692 if (!isa<ObjCQualifiedInterfaceType>(RHS)) 2693 return true; // FIXME: should return false! 2694 2695 // Finally, we must have two protocol-qualified interfaces. 2696 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS); 2697 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS); 2698 2699 // All LHS protocols must have a presence on the RHS. 2700 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?"); 2701 2702 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(), 2703 LHSPE = LHSP->qual_end(); 2704 LHSPI != LHSPE; LHSPI++) { 2705 bool RHSImplementsProtocol = false; 2706 2707 // If the RHS doesn't implement the protocol on the left, the types 2708 // are incompatible. 2709 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(), 2710 RHSPE = RHSP->qual_end(); 2711 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) { 2712 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) 2713 RHSImplementsProtocol = true; 2714 } 2715 // FIXME: For better diagnostics, consider passing back the protocol name. 2716 if (!RHSImplementsProtocol) 2717 return false; 2718 } 2719 // The RHS implements all protocols listed on the LHS. 2720 return true; 2721} 2722 2723bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { 2724 // get the "pointed to" types 2725 const PointerType *LHSPT = LHS->getAsPointerType(); 2726 const PointerType *RHSPT = RHS->getAsPointerType(); 2727 2728 if (!LHSPT || !RHSPT) 2729 return false; 2730 2731 QualType lhptee = LHSPT->getPointeeType(); 2732 QualType rhptee = RHSPT->getPointeeType(); 2733 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); 2734 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); 2735 // ID acts sort of like void* for ObjC interfaces 2736 if (LHSIface && isObjCIdStructType(rhptee)) 2737 return true; 2738 if (RHSIface && isObjCIdStructType(lhptee)) 2739 return true; 2740 if (!LHSIface || !RHSIface) 2741 return false; 2742 return canAssignObjCInterfaces(LHSIface, RHSIface) || 2743 canAssignObjCInterfaces(RHSIface, LHSIface); 2744} 2745 2746/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, 2747/// both shall have the identically qualified version of a compatible type. 2748/// C99 6.2.7p1: Two types have compatible types if their types are the 2749/// same. See 6.7.[2,3,5] for additional rules. 2750bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { 2751 return !mergeTypes(LHS, RHS).isNull(); 2752} 2753 2754QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { 2755 const FunctionType *lbase = lhs->getAsFunctionType(); 2756 const FunctionType *rbase = rhs->getAsFunctionType(); 2757 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); 2758 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); 2759 bool allLTypes = true; 2760 bool allRTypes = true; 2761 2762 // Check return type 2763 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); 2764 if (retType.isNull()) return QualType(); 2765 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) 2766 allLTypes = false; 2767 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) 2768 allRTypes = false; 2769 2770 if (lproto && rproto) { // two C99 style function prototypes 2771 unsigned lproto_nargs = lproto->getNumArgs(); 2772 unsigned rproto_nargs = rproto->getNumArgs(); 2773 2774 // Compatible functions must have the same number of arguments 2775 if (lproto_nargs != rproto_nargs) 2776 return QualType(); 2777 2778 // Variadic and non-variadic functions aren't compatible 2779 if (lproto->isVariadic() != rproto->isVariadic()) 2780 return QualType(); 2781 2782 if (lproto->getTypeQuals() != rproto->getTypeQuals()) 2783 return QualType(); 2784 2785 // Check argument compatibility 2786 llvm::SmallVector<QualType, 10> types; 2787 for (unsigned i = 0; i < lproto_nargs; i++) { 2788 QualType largtype = lproto->getArgType(i).getUnqualifiedType(); 2789 QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); 2790 QualType argtype = mergeTypes(largtype, rargtype); 2791 if (argtype.isNull()) return QualType(); 2792 types.push_back(argtype); 2793 if (getCanonicalType(argtype) != getCanonicalType(largtype)) 2794 allLTypes = false; 2795 if (getCanonicalType(argtype) != getCanonicalType(rargtype)) 2796 allRTypes = false; 2797 } 2798 if (allLTypes) return lhs; 2799 if (allRTypes) return rhs; 2800 return getFunctionType(retType, types.begin(), types.size(), 2801 lproto->isVariadic(), lproto->getTypeQuals()); 2802 } 2803 2804 if (lproto) allRTypes = false; 2805 if (rproto) allLTypes = false; 2806 2807 const FunctionProtoType *proto = lproto ? lproto : rproto; 2808 if (proto) { 2809 if (proto->isVariadic()) return QualType(); 2810 // Check that the types are compatible with the types that 2811 // would result from default argument promotions (C99 6.7.5.3p15). 2812 // The only types actually affected are promotable integer 2813 // types and floats, which would be passed as a different 2814 // type depending on whether the prototype is visible. 2815 unsigned proto_nargs = proto->getNumArgs(); 2816 for (unsigned i = 0; i < proto_nargs; ++i) { 2817 QualType argTy = proto->getArgType(i); 2818 if (argTy->isPromotableIntegerType() || 2819 getCanonicalType(argTy).getUnqualifiedType() == FloatTy) 2820 return QualType(); 2821 } 2822 2823 if (allLTypes) return lhs; 2824 if (allRTypes) return rhs; 2825 return getFunctionType(retType, proto->arg_type_begin(), 2826 proto->getNumArgs(), lproto->isVariadic(), 2827 lproto->getTypeQuals()); 2828 } 2829 2830 if (allLTypes) return lhs; 2831 if (allRTypes) return rhs; 2832 return getFunctionNoProtoType(retType); 2833} 2834 2835QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { 2836 // C++ [expr]: If an expression initially has the type "reference to T", the 2837 // type is adjusted to "T" prior to any further analysis, the expression 2838 // designates the object or function denoted by the reference, and the 2839 // expression is an lvalue unless the reference is an rvalue reference and 2840 // the expression is a function call (possibly inside parentheses). 2841 // FIXME: C++ shouldn't be going through here! The rules are different 2842 // enough that they should be handled separately. 2843 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* 2844 // shouldn't be going through here! 2845 if (const ReferenceType *RT = LHS->getAsReferenceType()) 2846 LHS = RT->getPointeeType(); 2847 if (const ReferenceType *RT = RHS->getAsReferenceType()) 2848 RHS = RT->getPointeeType(); 2849 2850 QualType LHSCan = getCanonicalType(LHS), 2851 RHSCan = getCanonicalType(RHS); 2852 2853 // If two types are identical, they are compatible. 2854 if (LHSCan == RHSCan) 2855 return LHS; 2856 2857 // If the qualifiers are different, the types aren't compatible 2858 // Note that we handle extended qualifiers later, in the 2859 // case for ExtQualType. 2860 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers()) 2861 return QualType(); 2862 2863 Type::TypeClass LHSClass = LHSCan->getTypeClass(); 2864 Type::TypeClass RHSClass = RHSCan->getTypeClass(); 2865 2866 // We want to consider the two function types to be the same for these 2867 // comparisons, just force one to the other. 2868 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; 2869 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; 2870 2871 // Same as above for arrays 2872 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) 2873 LHSClass = Type::ConstantArray; 2874 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) 2875 RHSClass = Type::ConstantArray; 2876 2877 // Canonicalize ExtVector -> Vector. 2878 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; 2879 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; 2880 2881 // Consider qualified interfaces and interfaces the same. 2882 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface; 2883 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface; 2884 2885 // If the canonical type classes don't match. 2886 if (LHSClass != RHSClass) { 2887 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); 2888 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); 2889 2890 // ID acts sort of like void* for ObjC interfaces 2891 if (LHSIface && isObjCIdStructType(RHS)) 2892 return LHS; 2893 if (RHSIface && isObjCIdStructType(LHS)) 2894 return RHS; 2895 2896 // ID is compatible with all qualified id types. 2897 if (LHS->isObjCQualifiedIdType()) { 2898 if (const PointerType *PT = RHS->getAsPointerType()) { 2899 QualType pType = PT->getPointeeType(); 2900 if (isObjCIdStructType(pType)) 2901 return LHS; 2902 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). 2903 // Unfortunately, this API is part of Sema (which we don't have access 2904 // to. Need to refactor. The following check is insufficient, since we 2905 // need to make sure the class implements the protocol. 2906 if (pType->isObjCInterfaceType()) 2907 return LHS; 2908 } 2909 } 2910 if (RHS->isObjCQualifiedIdType()) { 2911 if (const PointerType *PT = LHS->getAsPointerType()) { 2912 QualType pType = PT->getPointeeType(); 2913 if (isObjCIdStructType(pType)) 2914 return RHS; 2915 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). 2916 // Unfortunately, this API is part of Sema (which we don't have access 2917 // to. Need to refactor. The following check is insufficient, since we 2918 // need to make sure the class implements the protocol. 2919 if (pType->isObjCInterfaceType()) 2920 return RHS; 2921 } 2922 } 2923 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, 2924 // a signed integer type, or an unsigned integer type. 2925 if (const EnumType* ETy = LHS->getAsEnumType()) { 2926 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) 2927 return RHS; 2928 } 2929 if (const EnumType* ETy = RHS->getAsEnumType()) { 2930 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) 2931 return LHS; 2932 } 2933 2934 return QualType(); 2935 } 2936 2937 // The canonical type classes match. 2938 switch (LHSClass) { 2939#define TYPE(Class, Base) 2940#define ABSTRACT_TYPE(Class, Base) 2941#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2942#define DEPENDENT_TYPE(Class, Base) case Type::Class: 2943#include "clang/AST/TypeNodes.def" 2944 assert(false && "Non-canonical and dependent types shouldn't get here"); 2945 return QualType(); 2946 2947 case Type::LValueReference: 2948 case Type::RValueReference: 2949 case Type::MemberPointer: 2950 assert(false && "C++ should never be in mergeTypes"); 2951 return QualType(); 2952 2953 case Type::IncompleteArray: 2954 case Type::VariableArray: 2955 case Type::FunctionProto: 2956 case Type::ExtVector: 2957 case Type::ObjCQualifiedInterface: 2958 assert(false && "Types are eliminated above"); 2959 return QualType(); 2960 2961 case Type::Pointer: 2962 { 2963 // Merge two pointer types, while trying to preserve typedef info 2964 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType(); 2965 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType(); 2966 QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 2967 if (ResultType.isNull()) return QualType(); 2968 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 2969 return LHS; 2970 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 2971 return RHS; 2972 return getPointerType(ResultType); 2973 } 2974 case Type::BlockPointer: 2975 { 2976 // Merge two block pointer types, while trying to preserve typedef info 2977 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType(); 2978 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType(); 2979 QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 2980 if (ResultType.isNull()) return QualType(); 2981 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 2982 return LHS; 2983 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 2984 return RHS; 2985 return getBlockPointerType(ResultType); 2986 } 2987 case Type::ConstantArray: 2988 { 2989 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); 2990 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); 2991 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) 2992 return QualType(); 2993 2994 QualType LHSElem = getAsArrayType(LHS)->getElementType(); 2995 QualType RHSElem = getAsArrayType(RHS)->getElementType(); 2996 QualType ResultType = mergeTypes(LHSElem, RHSElem); 2997 if (ResultType.isNull()) return QualType(); 2998 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 2999 return LHS; 3000 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 3001 return RHS; 3002 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), 3003 ArrayType::ArraySizeModifier(), 0); 3004 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), 3005 ArrayType::ArraySizeModifier(), 0); 3006 const VariableArrayType* LVAT = getAsVariableArrayType(LHS); 3007 const VariableArrayType* RVAT = getAsVariableArrayType(RHS); 3008 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 3009 return LHS; 3010 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 3011 return RHS; 3012 if (LVAT) { 3013 // FIXME: This isn't correct! But tricky to implement because 3014 // the array's size has to be the size of LHS, but the type 3015 // has to be different. 3016 return LHS; 3017 } 3018 if (RVAT) { 3019 // FIXME: This isn't correct! But tricky to implement because 3020 // the array's size has to be the size of RHS, but the type 3021 // has to be different. 3022 return RHS; 3023 } 3024 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; 3025 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; 3026 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0); 3027 } 3028 case Type::FunctionNoProto: 3029 return mergeFunctionTypes(LHS, RHS); 3030 case Type::Record: 3031 case Type::Enum: 3032 // FIXME: Why are these compatible? 3033 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS; 3034 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS; 3035 return QualType(); 3036 case Type::Builtin: 3037 // Only exactly equal builtin types are compatible, which is tested above. 3038 return QualType(); 3039 case Type::Complex: 3040 // Distinct complex types are incompatible. 3041 return QualType(); 3042 case Type::Vector: 3043 // FIXME: The merged type should be an ExtVector! 3044 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) 3045 return LHS; 3046 return QualType(); 3047 case Type::ObjCInterface: { 3048 // Check if the interfaces are assignment compatible. 3049 // FIXME: This should be type compatibility, e.g. whether 3050 // "LHS x; RHS x;" at global scope is legal. 3051 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); 3052 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); 3053 if (LHSIface && RHSIface && 3054 canAssignObjCInterfaces(LHSIface, RHSIface)) 3055 return LHS; 3056 3057 return QualType(); 3058 } 3059 case Type::ObjCQualifiedId: 3060 // Distinct qualified id's are not compatible. 3061 return QualType(); 3062 case Type::FixedWidthInt: 3063 // Distinct fixed-width integers are not compatible. 3064 return QualType(); 3065 case Type::ObjCQualifiedClass: 3066 // Distinct qualified classes are not compatible. 3067 return QualType(); 3068 case Type::ExtQual: 3069 // FIXME: ExtQual types can be compatible even if they're not 3070 // identical! 3071 return QualType(); 3072 // First attempt at an implementation, but I'm not really sure it's 3073 // right... 3074#if 0 3075 ExtQualType* LQual = cast<ExtQualType>(LHSCan); 3076 ExtQualType* RQual = cast<ExtQualType>(RHSCan); 3077 if (LQual->getAddressSpace() != RQual->getAddressSpace() || 3078 LQual->getObjCGCAttr() != RQual->getObjCGCAttr()) 3079 return QualType(); 3080 QualType LHSBase, RHSBase, ResultType, ResCanUnqual; 3081 LHSBase = QualType(LQual->getBaseType(), 0); 3082 RHSBase = QualType(RQual->getBaseType(), 0); 3083 ResultType = mergeTypes(LHSBase, RHSBase); 3084 if (ResultType.isNull()) return QualType(); 3085 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType(); 3086 if (LHSCan.getUnqualifiedType() == ResCanUnqual) 3087 return LHS; 3088 if (RHSCan.getUnqualifiedType() == ResCanUnqual) 3089 return RHS; 3090 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace()); 3091 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr()); 3092 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers()); 3093 return ResultType; 3094#endif 3095 3096 case Type::TemplateSpecialization: 3097 assert(false && "Dependent types have no size"); 3098 break; 3099 } 3100 3101 return QualType(); 3102} 3103 3104//===----------------------------------------------------------------------===// 3105// Integer Predicates 3106//===----------------------------------------------------------------------===// 3107 3108unsigned ASTContext::getIntWidth(QualType T) { 3109 if (T == BoolTy) 3110 return 1; 3111 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) { 3112 return FWIT->getWidth(); 3113 } 3114 // For builtin types, just use the standard type sizing method 3115 return (unsigned)getTypeSize(T); 3116} 3117 3118QualType ASTContext::getCorrespondingUnsignedType(QualType T) { 3119 assert(T->isSignedIntegerType() && "Unexpected type"); 3120 if (const EnumType* ETy = T->getAsEnumType()) 3121 T = ETy->getDecl()->getIntegerType(); 3122 const BuiltinType* BTy = T->getAsBuiltinType(); 3123 assert (BTy && "Unexpected signed integer type"); 3124 switch (BTy->getKind()) { 3125 case BuiltinType::Char_S: 3126 case BuiltinType::SChar: 3127 return UnsignedCharTy; 3128 case BuiltinType::Short: 3129 return UnsignedShortTy; 3130 case BuiltinType::Int: 3131 return UnsignedIntTy; 3132 case BuiltinType::Long: 3133 return UnsignedLongTy; 3134 case BuiltinType::LongLong: 3135 return UnsignedLongLongTy; 3136 default: 3137 assert(0 && "Unexpected signed integer type"); 3138 return QualType(); 3139 } 3140} 3141 3142 3143//===----------------------------------------------------------------------===// 3144// Serialization Support 3145//===----------------------------------------------------------------------===// 3146 3147enum { 3148 BasicMetadataBlock = 1, 3149 ASTContextBlock = 2, 3150 DeclsBlock = 3 3151}; 3152 3153void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{ 3154 // Create bitstream. 3155 llvm::BitstreamWriter Stream(Buffer); 3156 3157 // Emit the preamble. 3158 Stream.Emit((unsigned)'B', 8); 3159 Stream.Emit((unsigned)'C', 8); 3160 Stream.Emit(0xC, 4); 3161 Stream.Emit(0xF, 4); 3162 Stream.Emit(0xE, 4); 3163 Stream.Emit(0x0, 4); 3164 3165 // Create serializer. 3166 llvm::Serializer S(Stream); 3167 3168 // ===---------------------------------------------------===/ 3169 // Serialize the "Translation Unit" metadata. 3170 // ===---------------------------------------------------===/ 3171 3172 // Emit ASTContext. 3173 S.EnterBlock(ASTContextBlock); 3174 S.EmitOwnedPtr(this); 3175 S.ExitBlock(); // exit "ASTContextBlock" 3176 3177 S.EnterBlock(BasicMetadataBlock); 3178 3179 // Block for SourceManager and Target. Allows easy skipping 3180 // around to the block for the Selectors during deserialization. 3181 S.EnterBlock(); 3182 3183 // Emit the SourceManager. 3184 S.Emit(getSourceManager()); 3185 3186 // Emit the Target. 3187 S.EmitPtr(&Target); 3188 S.EmitCStr(Target.getTargetTriple()); 3189 3190 S.ExitBlock(); // exit "SourceManager and Target Block" 3191 3192 // Emit the Selectors. 3193 S.Emit(Selectors); 3194 3195 // Emit the Identifier Table. 3196 S.Emit(Idents); 3197 3198 S.ExitBlock(); // exit "BasicMetadataBlock" 3199} 3200 3201 3202/// Emit - Serialize an ASTContext object to Bitcode. 3203void ASTContext::Emit(llvm::Serializer& S) const { 3204 S.Emit(LangOpts); 3205 S.EmitRef(SourceMgr); 3206 S.EmitRef(Target); 3207 S.EmitRef(Idents); 3208 S.EmitRef(Selectors); 3209 3210 // Emit the size of the type vector so that we can reserve that size 3211 // when we reconstitute the ASTContext object. 3212 S.EmitInt(Types.size()); 3213 3214 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); 3215 I!=E;++I) 3216 (*I)->Emit(S); 3217 3218 S.EmitOwnedPtr(TUDecl); 3219 3220 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); 3221} 3222 3223 3224ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer, 3225 FileManager &FMgr) { 3226 // Check if the file is of the proper length. 3227 if (Buffer.getBufferSize() & 0x3) { 3228 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes." 3229 return 0; 3230 } 3231 3232 // Create the bitstream reader. 3233 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart(); 3234 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize()); 3235 3236 if (Stream.Read(8) != 'B' || 3237 Stream.Read(8) != 'C' || 3238 Stream.Read(4) != 0xC || 3239 Stream.Read(4) != 0xF || 3240 Stream.Read(4) != 0xE || 3241 Stream.Read(4) != 0x0) { 3242 // FIXME: Provide diagnostic. 3243 return NULL; 3244 } 3245 3246 // Create the deserializer. 3247 llvm::Deserializer Dezr(Stream); 3248 3249 // ===---------------------------------------------------===/ 3250 // Deserialize the "Translation Unit" metadata. 3251 // ===---------------------------------------------------===/ 3252 3253 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock 3254 // (which will appear earlier) and record its location. 3255 3256 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock); 3257 assert (FoundBlock); 3258 3259 llvm::Deserializer::Location ASTContextBlockLoc = 3260 Dezr.getCurrentBlockLocation(); 3261 3262 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock); 3263 assert (FoundBlock); 3264 3265 // Read the SourceManager. 3266 SourceManager::CreateAndRegister(Dezr, FMgr); 3267 3268 { // Read the TargetInfo. 3269 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID(); 3270 char* triple = Dezr.ReadCStr(NULL,0,true); 3271 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple))); 3272 delete [] triple; 3273 } 3274 3275 // For Selectors, we must read the identifier table first because the 3276 // SelectorTable depends on the identifiers being already deserialized. 3277 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation(); 3278 Dezr.SkipBlock(); 3279 3280 // Read the identifier table. 3281 IdentifierTable::CreateAndRegister(Dezr); 3282 3283 // Now jump back and read the selectors. 3284 Dezr.JumpTo(SelectorBlkLoc); 3285 SelectorTable::CreateAndRegister(Dezr); 3286 3287 // Now jump back to ASTContextBlock and read the ASTContext. 3288 Dezr.JumpTo(ASTContextBlockLoc); 3289 return Dezr.ReadOwnedPtr<ASTContext>(); 3290} 3291 3292ASTContext* ASTContext::Create(llvm::Deserializer& D) { 3293 3294 // Read the language options. 3295 LangOptions LOpts; 3296 LOpts.Read(D); 3297 3298 SourceManager &SM = D.ReadRef<SourceManager>(); 3299 TargetInfo &t = D.ReadRef<TargetInfo>(); 3300 IdentifierTable &idents = D.ReadRef<IdentifierTable>(); 3301 SelectorTable &sels = D.ReadRef<SelectorTable>(); 3302 3303 unsigned size_reserve = D.ReadInt(); 3304 3305 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, 3306 size_reserve); 3307 3308 for (unsigned i = 0; i < size_reserve; ++i) 3309 Type::Create(*A,i,D); 3310 3311 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A)); 3312 3313 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); 3314 3315 return A; 3316} 3317