ASTContext.cpp revision 39f34e97d6a468f0a7dfa5664c61217cffc65b74
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/Expr.h" 18#include "clang/AST/RecordLayout.h" 19#include "clang/Basic/TargetInfo.h" 20#include "llvm/ADT/StringExtras.h" 21#include "llvm/Bitcode/Serialize.h" 22#include "llvm/Bitcode/Deserialize.h" 23 24using namespace clang; 25 26enum FloatingRank { 27 FloatRank, DoubleRank, LongDoubleRank 28}; 29 30ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, 31 TargetInfo &t, 32 IdentifierTable &idents, SelectorTable &sels, 33 unsigned size_reserve) : 34 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0), 35 SourceMgr(SM), LangOpts(LOpts), Target(t), 36 Idents(idents), Selectors(sels) 37{ 38 if (size_reserve > 0) Types.reserve(size_reserve); 39 InitBuiltinTypes(); 40 BuiltinInfo.InitializeBuiltins(idents, Target); 41 TUDecl = TranslationUnitDecl::Create(*this); 42} 43 44ASTContext::~ASTContext() { 45 // Deallocate all the types. 46 while (!Types.empty()) { 47 Types.back()->Destroy(*this); 48 Types.pop_back(); 49 } 50 51 TUDecl->Destroy(*this); 52} 53 54void ASTContext::PrintStats() const { 55 fprintf(stderr, "*** AST Context Stats:\n"); 56 fprintf(stderr, " %d types total.\n", (int)Types.size()); 57 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; 58 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0; 59 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; 60 61 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; 62 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0; 63 unsigned NumObjCQualifiedIds = 0; 64 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0; 65 66 for (unsigned i = 0, e = Types.size(); i != e; ++i) { 67 Type *T = Types[i]; 68 if (isa<BuiltinType>(T)) 69 ++NumBuiltin; 70 else if (isa<PointerType>(T)) 71 ++NumPointer; 72 else if (isa<BlockPointerType>(T)) 73 ++NumBlockPointer; 74 else if (isa<ReferenceType>(T)) 75 ++NumReference; 76 else if (isa<ComplexType>(T)) 77 ++NumComplex; 78 else if (isa<ArrayType>(T)) 79 ++NumArray; 80 else if (isa<VectorType>(T)) 81 ++NumVector; 82 else if (isa<FunctionTypeNoProto>(T)) 83 ++NumFunctionNP; 84 else if (isa<FunctionTypeProto>(T)) 85 ++NumFunctionP; 86 else if (isa<TypedefType>(T)) 87 ++NumTypeName; 88 else if (TagType *TT = dyn_cast<TagType>(T)) { 89 ++NumTagged; 90 switch (TT->getDecl()->getTagKind()) { 91 default: assert(0 && "Unknown tagged type!"); 92 case TagDecl::TK_struct: ++NumTagStruct; break; 93 case TagDecl::TK_union: ++NumTagUnion; break; 94 case TagDecl::TK_class: ++NumTagClass; break; 95 case TagDecl::TK_enum: ++NumTagEnum; break; 96 } 97 } else if (isa<ObjCInterfaceType>(T)) 98 ++NumObjCInterfaces; 99 else if (isa<ObjCQualifiedInterfaceType>(T)) 100 ++NumObjCQualifiedInterfaces; 101 else if (isa<ObjCQualifiedIdType>(T)) 102 ++NumObjCQualifiedIds; 103 else if (isa<TypeOfType>(T)) 104 ++NumTypeOfTypes; 105 else if (isa<TypeOfExpr>(T)) 106 ++NumTypeOfExprs; 107 else { 108 QualType(T, 0).dump(); 109 assert(0 && "Unknown type!"); 110 } 111 } 112 113 fprintf(stderr, " %d builtin types\n", NumBuiltin); 114 fprintf(stderr, " %d pointer types\n", NumPointer); 115 fprintf(stderr, " %d block pointer types\n", NumBlockPointer); 116 fprintf(stderr, " %d reference types\n", NumReference); 117 fprintf(stderr, " %d complex types\n", NumComplex); 118 fprintf(stderr, " %d array types\n", NumArray); 119 fprintf(stderr, " %d vector types\n", NumVector); 120 fprintf(stderr, " %d function types with proto\n", NumFunctionP); 121 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); 122 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); 123 fprintf(stderr, " %d tagged types\n", NumTagged); 124 fprintf(stderr, " %d struct types\n", NumTagStruct); 125 fprintf(stderr, " %d union types\n", NumTagUnion); 126 fprintf(stderr, " %d class types\n", NumTagClass); 127 fprintf(stderr, " %d enum types\n", NumTagEnum); 128 fprintf(stderr, " %d interface types\n", NumObjCInterfaces); 129 fprintf(stderr, " %d protocol qualified interface types\n", 130 NumObjCQualifiedInterfaces); 131 fprintf(stderr, " %d protocol qualified id types\n", 132 NumObjCQualifiedIds); 133 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes); 134 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs); 135 136 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ 137 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ 138 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ 139 NumFunctionP*sizeof(FunctionTypeProto)+ 140 NumFunctionNP*sizeof(FunctionTypeNoProto)+ 141 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+ 142 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr))); 143} 144 145 146void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { 147 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); 148} 149 150void ASTContext::InitBuiltinTypes() { 151 assert(VoidTy.isNull() && "Context reinitialized?"); 152 153 // C99 6.2.5p19. 154 InitBuiltinType(VoidTy, BuiltinType::Void); 155 156 // C99 6.2.5p2. 157 InitBuiltinType(BoolTy, BuiltinType::Bool); 158 // C99 6.2.5p3. 159 if (Target.isCharSigned()) 160 InitBuiltinType(CharTy, BuiltinType::Char_S); 161 else 162 InitBuiltinType(CharTy, BuiltinType::Char_U); 163 // C99 6.2.5p4. 164 InitBuiltinType(SignedCharTy, BuiltinType::SChar); 165 InitBuiltinType(ShortTy, BuiltinType::Short); 166 InitBuiltinType(IntTy, BuiltinType::Int); 167 InitBuiltinType(LongTy, BuiltinType::Long); 168 InitBuiltinType(LongLongTy, BuiltinType::LongLong); 169 170 // C99 6.2.5p6. 171 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); 172 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); 173 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); 174 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); 175 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); 176 177 // C99 6.2.5p10. 178 InitBuiltinType(FloatTy, BuiltinType::Float); 179 InitBuiltinType(DoubleTy, BuiltinType::Double); 180 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); 181 182 // C++ 3.9.1p5 183 InitBuiltinType(WCharTy, BuiltinType::WChar); 184 185 // Placeholder type for functions. 186 InitBuiltinType(OverloadTy, BuiltinType::Overload); 187 188 // C99 6.2.5p11. 189 FloatComplexTy = getComplexType(FloatTy); 190 DoubleComplexTy = getComplexType(DoubleTy); 191 LongDoubleComplexTy = getComplexType(LongDoubleTy); 192 193 BuiltinVaListType = QualType(); 194 ObjCIdType = QualType(); 195 IdStructType = 0; 196 ObjCClassType = QualType(); 197 ClassStructType = 0; 198 199 ObjCConstantStringType = QualType(); 200 201 // void * type 202 VoidPtrTy = getPointerType(VoidTy); 203} 204 205//===----------------------------------------------------------------------===// 206// Type Sizing and Analysis 207//===----------------------------------------------------------------------===// 208 209/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 210/// scalar floating point type. 211const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { 212 const BuiltinType *BT = T->getAsBuiltinType(); 213 assert(BT && "Not a floating point type!"); 214 switch (BT->getKind()) { 215 default: assert(0 && "Not a floating point type!"); 216 case BuiltinType::Float: return Target.getFloatFormat(); 217 case BuiltinType::Double: return Target.getDoubleFormat(); 218 case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); 219 } 220} 221 222 223/// getTypeSize - Return the size of the specified type, in bits. This method 224/// does not work on incomplete types. 225std::pair<uint64_t, unsigned> 226ASTContext::getTypeInfo(const Type *T) { 227 T = getCanonicalType(T); 228 uint64_t Width; 229 unsigned Align; 230 switch (T->getTypeClass()) { 231 case Type::TypeName: assert(0 && "Not a canonical type!"); 232 case Type::FunctionNoProto: 233 case Type::FunctionProto: 234 default: 235 assert(0 && "Incomplete types have no size!"); 236 case Type::VariableArray: 237 assert(0 && "VLAs not implemented yet!"); 238 case Type::ConstantArray: { 239 const ConstantArrayType *CAT = cast<ConstantArrayType>(T); 240 241 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); 242 Width = EltInfo.first*CAT->getSize().getZExtValue(); 243 Align = EltInfo.second; 244 break; 245 } 246 case Type::ExtVector: 247 case Type::Vector: { 248 std::pair<uint64_t, unsigned> EltInfo = 249 getTypeInfo(cast<VectorType>(T)->getElementType()); 250 Width = EltInfo.first*cast<VectorType>(T)->getNumElements(); 251 // FIXME: This isn't right for unusual vectors 252 Align = Width; 253 break; 254 } 255 256 case Type::Builtin: 257 switch (cast<BuiltinType>(T)->getKind()) { 258 default: assert(0 && "Unknown builtin type!"); 259 case BuiltinType::Void: 260 assert(0 && "Incomplete types have no size!"); 261 case BuiltinType::Bool: 262 Width = Target.getBoolWidth(); 263 Align = Target.getBoolAlign(); 264 break; 265 case BuiltinType::Char_S: 266 case BuiltinType::Char_U: 267 case BuiltinType::UChar: 268 case BuiltinType::SChar: 269 Width = Target.getCharWidth(); 270 Align = Target.getCharAlign(); 271 break; 272 case BuiltinType::WChar: 273 Width = Target.getWCharWidth(); 274 Align = Target.getWCharAlign(); 275 break; 276 case BuiltinType::UShort: 277 case BuiltinType::Short: 278 Width = Target.getShortWidth(); 279 Align = Target.getShortAlign(); 280 break; 281 case BuiltinType::UInt: 282 case BuiltinType::Int: 283 Width = Target.getIntWidth(); 284 Align = Target.getIntAlign(); 285 break; 286 case BuiltinType::ULong: 287 case BuiltinType::Long: 288 Width = Target.getLongWidth(); 289 Align = Target.getLongAlign(); 290 break; 291 case BuiltinType::ULongLong: 292 case BuiltinType::LongLong: 293 Width = Target.getLongLongWidth(); 294 Align = Target.getLongLongAlign(); 295 break; 296 case BuiltinType::Float: 297 Width = Target.getFloatWidth(); 298 Align = Target.getFloatAlign(); 299 break; 300 case BuiltinType::Double: 301 Width = Target.getDoubleWidth(); 302 Align = Target.getDoubleAlign(); 303 break; 304 case BuiltinType::LongDouble: 305 Width = Target.getLongDoubleWidth(); 306 Align = Target.getLongDoubleAlign(); 307 break; 308 } 309 break; 310 case Type::ASQual: 311 // FIXME: Pointers into different addr spaces could have different sizes and 312 // alignment requirements: getPointerInfo should take an AddrSpace. 313 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0)); 314 case Type::ObjCQualifiedId: 315 Width = Target.getPointerWidth(0); 316 Align = Target.getPointerAlign(0); 317 break; 318 case Type::BlockPointer: { 319 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); 320 Width = Target.getPointerWidth(AS); 321 Align = Target.getPointerAlign(AS); 322 break; 323 } 324 case Type::Pointer: { 325 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); 326 Width = Target.getPointerWidth(AS); 327 Align = Target.getPointerAlign(AS); 328 break; 329 } 330 case Type::Reference: 331 // "When applied to a reference or a reference type, the result is the size 332 // of the referenced type." C++98 5.3.3p2: expr.sizeof. 333 // FIXME: This is wrong for struct layout: a reference in a struct has 334 // pointer size. 335 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); 336 337 case Type::Complex: { 338 // Complex types have the same alignment as their elements, but twice the 339 // size. 340 std::pair<uint64_t, unsigned> EltInfo = 341 getTypeInfo(cast<ComplexType>(T)->getElementType()); 342 Width = EltInfo.first*2; 343 Align = EltInfo.second; 344 break; 345 } 346 case Type::ObjCInterface: { 347 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); 348 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); 349 Width = Layout.getSize(); 350 Align = Layout.getAlignment(); 351 break; 352 } 353 case Type::Tagged: { 354 const TagType *TT = cast<TagType>(T); 355 356 if (TT->getDecl()->isInvalidDecl()) { 357 Width = 1; 358 Align = 1; 359 break; 360 } 361 362 if (const EnumType *ET = dyn_cast<EnumType>(TT)) 363 return getTypeInfo(ET->getDecl()->getIntegerType()); 364 365 const RecordType *RT = cast<RecordType>(TT); 366 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); 367 Width = Layout.getSize(); 368 Align = Layout.getAlignment(); 369 break; 370 } 371 } 372 373 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); 374 return std::make_pair(Width, Align); 375} 376 377/// LayoutField - Field layout. 378void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, 379 bool IsUnion, unsigned StructPacking, 380 ASTContext &Context) { 381 unsigned FieldPacking = StructPacking; 382 uint64_t FieldOffset = IsUnion ? 0 : Size; 383 uint64_t FieldSize; 384 unsigned FieldAlign; 385 386 // FIXME: Should this override struct packing? Probably we want to 387 // take the minimum? 388 if (const PackedAttr *PA = FD->getAttr<PackedAttr>()) 389 FieldPacking = PA->getAlignment(); 390 391 if (const Expr *BitWidthExpr = FD->getBitWidth()) { 392 // TODO: Need to check this algorithm on other targets! 393 // (tested on Linux-X86) 394 FieldSize = 395 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue(); 396 397 std::pair<uint64_t, unsigned> FieldInfo = 398 Context.getTypeInfo(FD->getType()); 399 uint64_t TypeSize = FieldInfo.first; 400 401 // Determine the alignment of this bitfield. The packing 402 // attributes define a maximum and the alignment attribute defines 403 // a minimum. 404 // FIXME: What is the right behavior when the specified alignment 405 // is smaller than the specified packing? 406 FieldAlign = FieldInfo.second; 407 if (FieldPacking) 408 FieldAlign = std::min(FieldAlign, FieldPacking); 409 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) 410 FieldAlign = std::max(FieldAlign, AA->getAlignment()); 411 412 // Check if we need to add padding to give the field the correct 413 // alignment. 414 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) 415 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); 416 417 // Padding members don't affect overall alignment 418 if (!FD->getIdentifier()) 419 FieldAlign = 1; 420 } else { 421 if (FD->getType()->isIncompleteArrayType()) { 422 // This is a flexible array member; we can't directly 423 // query getTypeInfo about these, so we figure it out here. 424 // Flexible array members don't have any size, but they 425 // have to be aligned appropriately for their element type. 426 FieldSize = 0; 427 const ArrayType* ATy = Context.getAsArrayType(FD->getType()); 428 FieldAlign = Context.getTypeAlign(ATy->getElementType()); 429 } else { 430 std::pair<uint64_t, unsigned> FieldInfo = 431 Context.getTypeInfo(FD->getType()); 432 FieldSize = FieldInfo.first; 433 FieldAlign = FieldInfo.second; 434 } 435 436 // Determine the alignment of this bitfield. The packing 437 // attributes define a maximum and the alignment attribute defines 438 // a minimum. Additionally, the packing alignment must be at least 439 // a byte for non-bitfields. 440 // 441 // FIXME: What is the right behavior when the specified alignment 442 // is smaller than the specified packing? 443 if (FieldPacking) 444 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking)); 445 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) 446 FieldAlign = std::max(FieldAlign, AA->getAlignment()); 447 448 // Round up the current record size to the field's alignment boundary. 449 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); 450 } 451 452 // Place this field at the current location. 453 FieldOffsets[FieldNo] = FieldOffset; 454 455 // Reserve space for this field. 456 if (IsUnion) { 457 Size = std::max(Size, FieldSize); 458 } else { 459 Size = FieldOffset + FieldSize; 460 } 461 462 // Remember max struct/class alignment. 463 Alignment = std::max(Alignment, FieldAlign); 464} 465 466 467/// getASTObjcInterfaceLayout - Get or compute information about the layout of 468/// the specified Objective C, which indicates its size and ivar 469/// position information. 470const ASTRecordLayout & 471ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { 472 // Look up this layout, if already laid out, return what we have. 473 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D]; 474 if (Entry) return *Entry; 475 476 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can 477 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. 478 ASTRecordLayout *NewEntry = NULL; 479 unsigned FieldCount = D->ivar_size(); 480 if (ObjCInterfaceDecl *SD = D->getSuperClass()) { 481 FieldCount++; 482 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD); 483 unsigned Alignment = SL.getAlignment(); 484 uint64_t Size = SL.getSize(); 485 NewEntry = new ASTRecordLayout(Size, Alignment); 486 NewEntry->InitializeLayout(FieldCount); 487 // Super class is at the beginning of the layout. 488 NewEntry->SetFieldOffset(0, 0); 489 } else { 490 NewEntry = new ASTRecordLayout(); 491 NewEntry->InitializeLayout(FieldCount); 492 } 493 Entry = NewEntry; 494 495 unsigned StructPacking = 0; 496 if (const PackedAttr *PA = D->getAttr<PackedAttr>()) 497 StructPacking = PA->getAlignment(); 498 499 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) 500 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), 501 AA->getAlignment())); 502 503 // Layout each ivar sequentially. 504 unsigned i = 0; 505 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(), 506 IVE = D->ivar_end(); IVI != IVE; ++IVI) { 507 const ObjCIvarDecl* Ivar = (*IVI); 508 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this); 509 } 510 511 // Finally, round the size of the total struct up to the alignment of the 512 // struct itself. 513 NewEntry->FinalizeLayout(); 514 return *NewEntry; 515} 516 517/// getASTRecordLayout - Get or compute information about the layout of the 518/// specified record (struct/union/class), which indicates its size and field 519/// position information. 520const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { 521 D = D->getDefinition(*this); 522 assert(D && "Cannot get layout of forward declarations!"); 523 524 // Look up this layout, if already laid out, return what we have. 525 const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; 526 if (Entry) return *Entry; 527 528 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can 529 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. 530 ASTRecordLayout *NewEntry = new ASTRecordLayout(); 531 Entry = NewEntry; 532 533 NewEntry->InitializeLayout(D->getNumMembers()); 534 bool IsUnion = D->isUnion(); 535 536 unsigned StructPacking = 0; 537 if (const PackedAttr *PA = D->getAttr<PackedAttr>()) 538 StructPacking = PA->getAlignment(); 539 540 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) 541 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), 542 AA->getAlignment())); 543 544 // Layout each field, for now, just sequentially, respecting alignment. In 545 // the future, this will need to be tweakable by targets. 546 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { 547 const FieldDecl *FD = D->getMember(i); 548 NewEntry->LayoutField(FD, i, IsUnion, StructPacking, *this); 549 } 550 551 // Finally, round the size of the total struct up to the alignment of the 552 // struct itself. 553 NewEntry->FinalizeLayout(); 554 return *NewEntry; 555} 556 557//===----------------------------------------------------------------------===// 558// Type creation/memoization methods 559//===----------------------------------------------------------------------===// 560 561QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) { 562 QualType CanT = getCanonicalType(T); 563 if (CanT.getAddressSpace() == AddressSpace) 564 return T; 565 566 // Type's cannot have multiple ASQuals, therefore we know we only have to deal 567 // with CVR qualifiers from here on out. 568 assert(CanT.getAddressSpace() == 0 && 569 "Type is already address space qualified"); 570 571 // Check if we've already instantiated an address space qual'd type of this 572 // type. 573 llvm::FoldingSetNodeID ID; 574 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace); 575 void *InsertPos = 0; 576 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos)) 577 return QualType(ASQy, 0); 578 579 // If the base type isn't canonical, this won't be a canonical type either, 580 // so fill in the canonical type field. 581 QualType Canonical; 582 if (!T->isCanonical()) { 583 Canonical = getASQualType(CanT, AddressSpace); 584 585 // Get the new insert position for the node we care about. 586 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos); 587 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 588 } 589 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace); 590 ASQualTypes.InsertNode(New, InsertPos); 591 Types.push_back(New); 592 return QualType(New, T.getCVRQualifiers()); 593} 594 595 596/// getComplexType - Return the uniqued reference to the type for a complex 597/// number with the specified element type. 598QualType ASTContext::getComplexType(QualType T) { 599 // Unique pointers, to guarantee there is only one pointer of a particular 600 // structure. 601 llvm::FoldingSetNodeID ID; 602 ComplexType::Profile(ID, T); 603 604 void *InsertPos = 0; 605 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) 606 return QualType(CT, 0); 607 608 // If the pointee type isn't canonical, this won't be a canonical type either, 609 // so fill in the canonical type field. 610 QualType Canonical; 611 if (!T->isCanonical()) { 612 Canonical = getComplexType(getCanonicalType(T)); 613 614 // Get the new insert position for the node we care about. 615 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); 616 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 617 } 618 ComplexType *New = new ComplexType(T, Canonical); 619 Types.push_back(New); 620 ComplexTypes.InsertNode(New, InsertPos); 621 return QualType(New, 0); 622} 623 624 625/// getPointerType - Return the uniqued reference to the type for a pointer to 626/// the specified type. 627QualType ASTContext::getPointerType(QualType T) { 628 // Unique pointers, to guarantee there is only one pointer of a particular 629 // structure. 630 llvm::FoldingSetNodeID ID; 631 PointerType::Profile(ID, T); 632 633 void *InsertPos = 0; 634 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 635 return QualType(PT, 0); 636 637 // If the pointee type isn't canonical, this won't be a canonical type either, 638 // so fill in the canonical type field. 639 QualType Canonical; 640 if (!T->isCanonical()) { 641 Canonical = getPointerType(getCanonicalType(T)); 642 643 // Get the new insert position for the node we care about. 644 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); 645 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 646 } 647 PointerType *New = new PointerType(T, Canonical); 648 Types.push_back(New); 649 PointerTypes.InsertNode(New, InsertPos); 650 return QualType(New, 0); 651} 652 653/// getBlockPointerType - Return the uniqued reference to the type for 654/// a pointer to the specified block. 655QualType ASTContext::getBlockPointerType(QualType T) { 656 assert(T->isFunctionType() && "block of function types only"); 657 // Unique pointers, to guarantee there is only one block of a particular 658 // structure. 659 llvm::FoldingSetNodeID ID; 660 BlockPointerType::Profile(ID, T); 661 662 void *InsertPos = 0; 663 if (BlockPointerType *PT = 664 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 665 return QualType(PT, 0); 666 667 // If the block pointee type isn't canonical, this won't be a canonical 668 // type either so fill in the canonical type field. 669 QualType Canonical; 670 if (!T->isCanonical()) { 671 Canonical = getBlockPointerType(getCanonicalType(T)); 672 673 // Get the new insert position for the node we care about. 674 BlockPointerType *NewIP = 675 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 676 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 677 } 678 BlockPointerType *New = new BlockPointerType(T, Canonical); 679 Types.push_back(New); 680 BlockPointerTypes.InsertNode(New, InsertPos); 681 return QualType(New, 0); 682} 683 684/// getReferenceType - Return the uniqued reference to the type for a reference 685/// to the specified type. 686QualType ASTContext::getReferenceType(QualType T) { 687 // Unique pointers, to guarantee there is only one pointer of a particular 688 // structure. 689 llvm::FoldingSetNodeID ID; 690 ReferenceType::Profile(ID, T); 691 692 void *InsertPos = 0; 693 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 694 return QualType(RT, 0); 695 696 // If the referencee type isn't canonical, this won't be a canonical type 697 // either, so fill in the canonical type field. 698 QualType Canonical; 699 if (!T->isCanonical()) { 700 Canonical = getReferenceType(getCanonicalType(T)); 701 702 // Get the new insert position for the node we care about. 703 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 704 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 705 } 706 707 ReferenceType *New = new ReferenceType(T, Canonical); 708 Types.push_back(New); 709 ReferenceTypes.InsertNode(New, InsertPos); 710 return QualType(New, 0); 711} 712 713/// getConstantArrayType - Return the unique reference to the type for an 714/// array of the specified element type. 715QualType ASTContext::getConstantArrayType(QualType EltTy, 716 const llvm::APInt &ArySize, 717 ArrayType::ArraySizeModifier ASM, 718 unsigned EltTypeQuals) { 719 llvm::FoldingSetNodeID ID; 720 ConstantArrayType::Profile(ID, EltTy, ArySize); 721 722 void *InsertPos = 0; 723 if (ConstantArrayType *ATP = 724 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 725 return QualType(ATP, 0); 726 727 // If the element type isn't canonical, this won't be a canonical type either, 728 // so fill in the canonical type field. 729 QualType Canonical; 730 if (!EltTy->isCanonical()) { 731 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, 732 ASM, EltTypeQuals); 733 // Get the new insert position for the node we care about. 734 ConstantArrayType *NewIP = 735 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 736 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 737 } 738 739 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize, 740 ASM, EltTypeQuals); 741 ConstantArrayTypes.InsertNode(New, InsertPos); 742 Types.push_back(New); 743 return QualType(New, 0); 744} 745 746/// getVariableArrayType - Returns a non-unique reference to the type for a 747/// variable array of the specified element type. 748QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, 749 ArrayType::ArraySizeModifier ASM, 750 unsigned EltTypeQuals) { 751 // Since we don't unique expressions, it isn't possible to unique VLA's 752 // that have an expression provided for their size. 753 754 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, 755 ASM, EltTypeQuals); 756 757 VariableArrayTypes.push_back(New); 758 Types.push_back(New); 759 return QualType(New, 0); 760} 761 762QualType ASTContext::getIncompleteArrayType(QualType EltTy, 763 ArrayType::ArraySizeModifier ASM, 764 unsigned EltTypeQuals) { 765 llvm::FoldingSetNodeID ID; 766 IncompleteArrayType::Profile(ID, EltTy); 767 768 void *InsertPos = 0; 769 if (IncompleteArrayType *ATP = 770 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 771 return QualType(ATP, 0); 772 773 // If the element type isn't canonical, this won't be a canonical type 774 // either, so fill in the canonical type field. 775 QualType Canonical; 776 777 if (!EltTy->isCanonical()) { 778 Canonical = getIncompleteArrayType(getCanonicalType(EltTy), 779 ASM, EltTypeQuals); 780 781 // Get the new insert position for the node we care about. 782 IncompleteArrayType *NewIP = 783 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 784 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 785 } 786 787 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical, 788 ASM, EltTypeQuals); 789 790 IncompleteArrayTypes.InsertNode(New, InsertPos); 791 Types.push_back(New); 792 return QualType(New, 0); 793} 794 795/// getVectorType - Return the unique reference to a vector type of 796/// the specified element type and size. VectorType must be a built-in type. 797QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { 798 BuiltinType *baseType; 799 800 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 801 assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); 802 803 // Check if we've already instantiated a vector of this type. 804 llvm::FoldingSetNodeID ID; 805 VectorType::Profile(ID, vecType, NumElts, Type::Vector); 806 void *InsertPos = 0; 807 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 808 return QualType(VTP, 0); 809 810 // If the element type isn't canonical, this won't be a canonical type either, 811 // so fill in the canonical type field. 812 QualType Canonical; 813 if (!vecType->isCanonical()) { 814 Canonical = getVectorType(getCanonicalType(vecType), NumElts); 815 816 // Get the new insert position for the node we care about. 817 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 818 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 819 } 820 VectorType *New = new VectorType(vecType, NumElts, Canonical); 821 VectorTypes.InsertNode(New, InsertPos); 822 Types.push_back(New); 823 return QualType(New, 0); 824} 825 826/// getExtVectorType - Return the unique reference to an extended vector type of 827/// the specified element type and size. VectorType must be a built-in type. 828QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { 829 BuiltinType *baseType; 830 831 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 832 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); 833 834 // Check if we've already instantiated a vector of this type. 835 llvm::FoldingSetNodeID ID; 836 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); 837 void *InsertPos = 0; 838 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 839 return QualType(VTP, 0); 840 841 // If the element type isn't canonical, this won't be a canonical type either, 842 // so fill in the canonical type field. 843 QualType Canonical; 844 if (!vecType->isCanonical()) { 845 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); 846 847 // Get the new insert position for the node we care about. 848 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 849 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 850 } 851 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical); 852 VectorTypes.InsertNode(New, InsertPos); 853 Types.push_back(New); 854 return QualType(New, 0); 855} 856 857/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 858/// 859QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { 860 // Unique functions, to guarantee there is only one function of a particular 861 // structure. 862 llvm::FoldingSetNodeID ID; 863 FunctionTypeNoProto::Profile(ID, ResultTy); 864 865 void *InsertPos = 0; 866 if (FunctionTypeNoProto *FT = 867 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) 868 return QualType(FT, 0); 869 870 QualType Canonical; 871 if (!ResultTy->isCanonical()) { 872 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy)); 873 874 // Get the new insert position for the node we care about. 875 FunctionTypeNoProto *NewIP = 876 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); 877 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 878 } 879 880 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); 881 Types.push_back(New); 882 FunctionTypeNoProtos.InsertNode(New, InsertPos); 883 return QualType(New, 0); 884} 885 886/// getFunctionType - Return a normal function type with a typed argument 887/// list. isVariadic indicates whether the argument list includes '...'. 888QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, 889 unsigned NumArgs, bool isVariadic, 890 unsigned TypeQuals) { 891 // Unique functions, to guarantee there is only one function of a particular 892 // structure. 893 llvm::FoldingSetNodeID ID; 894 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, 895 TypeQuals); 896 897 void *InsertPos = 0; 898 if (FunctionTypeProto *FTP = 899 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) 900 return QualType(FTP, 0); 901 902 // Determine whether the type being created is already canonical or not. 903 bool isCanonical = ResultTy->isCanonical(); 904 for (unsigned i = 0; i != NumArgs && isCanonical; ++i) 905 if (!ArgArray[i]->isCanonical()) 906 isCanonical = false; 907 908 // If this type isn't canonical, get the canonical version of it. 909 QualType Canonical; 910 if (!isCanonical) { 911 llvm::SmallVector<QualType, 16> CanonicalArgs; 912 CanonicalArgs.reserve(NumArgs); 913 for (unsigned i = 0; i != NumArgs; ++i) 914 CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); 915 916 Canonical = getFunctionType(getCanonicalType(ResultTy), 917 &CanonicalArgs[0], NumArgs, 918 isVariadic, TypeQuals); 919 920 // Get the new insert position for the node we care about. 921 FunctionTypeProto *NewIP = 922 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); 923 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 924 } 925 926 // FunctionTypeProto objects are not allocated with new because they have a 927 // variable size array (for parameter types) at the end of them. 928 FunctionTypeProto *FTP = 929 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + 930 NumArgs*sizeof(QualType)); 931 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, 932 TypeQuals, Canonical); 933 Types.push_back(FTP); 934 FunctionTypeProtos.InsertNode(FTP, InsertPos); 935 return QualType(FTP, 0); 936} 937 938/// getTypeDeclType - Return the unique reference to the type for the 939/// specified type declaration. 940QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { 941 assert(Decl && "Passed null for Decl param"); 942 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 943 944 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) 945 return getTypedefType(Typedef); 946 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl)) 947 return getObjCInterfaceType(ObjCInterface); 948 949 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) { 950 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl 951 : new CXXRecordType(CXXRecord); 952 } 953 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { 954 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl 955 : new RecordType(Record); 956 } 957 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) 958 Decl->TypeForDecl = new EnumType(Enum); 959 else 960 assert(false && "TypeDecl without a type?"); 961 962 if (!PrevDecl) Types.push_back(Decl->TypeForDecl); 963 return QualType(Decl->TypeForDecl, 0); 964} 965 966/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext 967/// about which RecordDecl serves as the definition of a particular 968/// struct/union/class. This will eventually be used by enums as well. 969void ASTContext::setTagDefinition(TagDecl* D) { 970 assert (D->isDefinition()); 971 cast<TagType>(D->TypeForDecl)->decl = D; 972} 973 974/// getTypedefType - Return the unique reference to the type for the 975/// specified typename decl. 976QualType ASTContext::getTypedefType(TypedefDecl *Decl) { 977 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 978 979 QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); 980 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical); 981 Types.push_back(Decl->TypeForDecl); 982 return QualType(Decl->TypeForDecl, 0); 983} 984 985/// getObjCInterfaceType - Return the unique reference to the type for the 986/// specified ObjC interface decl. 987QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) { 988 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 989 990 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl); 991 Types.push_back(Decl->TypeForDecl); 992 return QualType(Decl->TypeForDecl, 0); 993} 994 995/// CmpProtocolNames - Comparison predicate for sorting protocols 996/// alphabetically. 997static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, 998 const ObjCProtocolDecl *RHS) { 999 return LHS->getDeclName() < RHS->getDeclName(); 1000} 1001 1002static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, 1003 unsigned &NumProtocols) { 1004 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; 1005 1006 // Sort protocols, keyed by name. 1007 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); 1008 1009 // Remove duplicates. 1010 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); 1011 NumProtocols = ProtocolsEnd-Protocols; 1012} 1013 1014 1015/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for 1016/// the given interface decl and the conforming protocol list. 1017QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 1018 ObjCProtocolDecl **Protocols, unsigned NumProtocols) { 1019 // Sort the protocol list alphabetically to canonicalize it. 1020 SortAndUniqueProtocols(Protocols, NumProtocols); 1021 1022 llvm::FoldingSetNodeID ID; 1023 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); 1024 1025 void *InsertPos = 0; 1026 if (ObjCQualifiedInterfaceType *QT = 1027 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) 1028 return QualType(QT, 0); 1029 1030 // No Match; 1031 ObjCQualifiedInterfaceType *QType = 1032 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols); 1033 Types.push_back(QType); 1034 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos); 1035 return QualType(QType, 0); 1036} 1037 1038/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl 1039/// and the conforming protocol list. 1040QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols, 1041 unsigned NumProtocols) { 1042 // Sort the protocol list alphabetically to canonicalize it. 1043 SortAndUniqueProtocols(Protocols, NumProtocols); 1044 1045 llvm::FoldingSetNodeID ID; 1046 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols); 1047 1048 void *InsertPos = 0; 1049 if (ObjCQualifiedIdType *QT = 1050 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos)) 1051 return QualType(QT, 0); 1052 1053 // No Match; 1054 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols); 1055 Types.push_back(QType); 1056 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos); 1057 return QualType(QType, 0); 1058} 1059 1060/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique 1061/// TypeOfExpr AST's (since expression's are never shared). For example, 1062/// multiple declarations that refer to "typeof(x)" all contain different 1063/// DeclRefExpr's. This doesn't effect the type checker, since it operates 1064/// on canonical type's (which are always unique). 1065QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { 1066 QualType Canonical = getCanonicalType(tofExpr->getType()); 1067 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical); 1068 Types.push_back(toe); 1069 return QualType(toe, 0); 1070} 1071 1072/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique 1073/// TypeOfType AST's. The only motivation to unique these nodes would be 1074/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be 1075/// an issue. This doesn't effect the type checker, since it operates 1076/// on canonical type's (which are always unique). 1077QualType ASTContext::getTypeOfType(QualType tofType) { 1078 QualType Canonical = getCanonicalType(tofType); 1079 TypeOfType *tot = new TypeOfType(tofType, Canonical); 1080 Types.push_back(tot); 1081 return QualType(tot, 0); 1082} 1083 1084/// getTagDeclType - Return the unique reference to the type for the 1085/// specified TagDecl (struct/union/class/enum) decl. 1086QualType ASTContext::getTagDeclType(TagDecl *Decl) { 1087 assert (Decl); 1088 return getTypeDeclType(Decl); 1089} 1090 1091/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result 1092/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and 1093/// needs to agree with the definition in <stddef.h>. 1094QualType ASTContext::getSizeType() const { 1095 return getFromTargetType(Target.getSizeType()); 1096} 1097 1098/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the 1099/// width of characters in wide strings, The value is target dependent and 1100/// needs to agree with the definition in <stddef.h>. 1101QualType ASTContext::getWCharType() const { 1102 if (LangOpts.CPlusPlus) 1103 return WCharTy; 1104 1105 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's 1106 // wide-character type? 1107 return getFromTargetType(Target.getWCharType()); 1108} 1109 1110/// getSignedWCharType - Return the type of "signed wchar_t". 1111/// Used when in C++, as a GCC extension. 1112QualType ASTContext::getSignedWCharType() const { 1113 // FIXME: derive from "Target" ? 1114 return WCharTy; 1115} 1116 1117/// getUnsignedWCharType - Return the type of "unsigned wchar_t". 1118/// Used when in C++, as a GCC extension. 1119QualType ASTContext::getUnsignedWCharType() const { 1120 // FIXME: derive from "Target" ? 1121 return UnsignedIntTy; 1122} 1123 1124/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 1125/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 1126QualType ASTContext::getPointerDiffType() const { 1127 return getFromTargetType(Target.getPtrDiffType(0)); 1128} 1129 1130//===----------------------------------------------------------------------===// 1131// Type Operators 1132//===----------------------------------------------------------------------===// 1133 1134/// getCanonicalType - Return the canonical (structural) type corresponding to 1135/// the specified potentially non-canonical type. The non-canonical version 1136/// of a type may have many "decorated" versions of types. Decorators can 1137/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 1138/// to be free of any of these, allowing two canonical types to be compared 1139/// for exact equality with a simple pointer comparison. 1140QualType ASTContext::getCanonicalType(QualType T) { 1141 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); 1142 1143 // If the result has type qualifiers, make sure to canonicalize them as well. 1144 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); 1145 if (TypeQuals == 0) return CanType; 1146 1147 // If the type qualifiers are on an array type, get the canonical type of the 1148 // array with the qualifiers applied to the element type. 1149 ArrayType *AT = dyn_cast<ArrayType>(CanType); 1150 if (!AT) 1151 return CanType.getQualifiedType(TypeQuals); 1152 1153 // Get the canonical version of the element with the extra qualifiers on it. 1154 // This can recursively sink qualifiers through multiple levels of arrays. 1155 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); 1156 NewEltTy = getCanonicalType(NewEltTy); 1157 1158 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 1159 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(), 1160 CAT->getIndexTypeQualifier()); 1161 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) 1162 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), 1163 IAT->getIndexTypeQualifier()); 1164 1165 // FIXME: What is the ownership of size expressions in VLAs? 1166 VariableArrayType *VAT = cast<VariableArrayType>(AT); 1167 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(), 1168 VAT->getSizeModifier(), 1169 VAT->getIndexTypeQualifier()); 1170} 1171 1172 1173const ArrayType *ASTContext::getAsArrayType(QualType T) { 1174 // Handle the non-qualified case efficiently. 1175 if (T.getCVRQualifiers() == 0) { 1176 // Handle the common positive case fast. 1177 if (const ArrayType *AT = dyn_cast<ArrayType>(T)) 1178 return AT; 1179 } 1180 1181 // Handle the common negative case fast, ignoring CVR qualifiers. 1182 QualType CType = T->getCanonicalTypeInternal(); 1183 1184 // Make sure to look through type qualifiers (like ASQuals) for the negative 1185 // test. 1186 if (!isa<ArrayType>(CType) && 1187 !isa<ArrayType>(CType.getUnqualifiedType())) 1188 return 0; 1189 1190 // Apply any CVR qualifiers from the array type to the element type. This 1191 // implements C99 6.7.3p8: "If the specification of an array type includes 1192 // any type qualifiers, the element type is so qualified, not the array type." 1193 1194 // If we get here, we either have type qualifiers on the type, or we have 1195 // sugar such as a typedef in the way. If we have type qualifiers on the type 1196 // we must propagate them down into the elemeng type. 1197 unsigned CVRQuals = T.getCVRQualifiers(); 1198 unsigned AddrSpace = 0; 1199 Type *Ty = T.getTypePtr(); 1200 1201 // Rip through ASQualType's and typedefs to get to a concrete type. 1202 while (1) { 1203 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) { 1204 AddrSpace = ASQT->getAddressSpace(); 1205 Ty = ASQT->getBaseType(); 1206 } else { 1207 T = Ty->getDesugaredType(); 1208 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) 1209 break; 1210 CVRQuals |= T.getCVRQualifiers(); 1211 Ty = T.getTypePtr(); 1212 } 1213 } 1214 1215 // If we have a simple case, just return now. 1216 const ArrayType *ATy = dyn_cast<ArrayType>(Ty); 1217 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) 1218 return ATy; 1219 1220 // Otherwise, we have an array and we have qualifiers on it. Push the 1221 // qualifiers into the array element type and return a new array type. 1222 // Get the canonical version of the element with the extra qualifiers on it. 1223 // This can recursively sink qualifiers through multiple levels of arrays. 1224 QualType NewEltTy = ATy->getElementType(); 1225 if (AddrSpace) 1226 NewEltTy = getASQualType(NewEltTy, AddrSpace); 1227 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); 1228 1229 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) 1230 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), 1231 CAT->getSizeModifier(), 1232 CAT->getIndexTypeQualifier())); 1233 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) 1234 return cast<ArrayType>(getIncompleteArrayType(NewEltTy, 1235 IAT->getSizeModifier(), 1236 IAT->getIndexTypeQualifier())); 1237 1238 // FIXME: What is the ownership of size expressions in VLAs? 1239 const VariableArrayType *VAT = cast<VariableArrayType>(ATy); 1240 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(), 1241 VAT->getSizeModifier(), 1242 VAT->getIndexTypeQualifier())); 1243} 1244 1245 1246/// getArrayDecayedType - Return the properly qualified result of decaying the 1247/// specified array type to a pointer. This operation is non-trivial when 1248/// handling typedefs etc. The canonical type of "T" must be an array type, 1249/// this returns a pointer to a properly qualified element of the array. 1250/// 1251/// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1252QualType ASTContext::getArrayDecayedType(QualType Ty) { 1253 // Get the element type with 'getAsArrayType' so that we don't lose any 1254 // typedefs in the element type of the array. This also handles propagation 1255 // of type qualifiers from the array type into the element type if present 1256 // (C99 6.7.3p8). 1257 const ArrayType *PrettyArrayType = getAsArrayType(Ty); 1258 assert(PrettyArrayType && "Not an array type!"); 1259 1260 QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); 1261 1262 // int x[restrict 4] -> int *restrict 1263 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); 1264} 1265 1266/// getFloatingRank - Return a relative rank for floating point types. 1267/// This routine will assert if passed a built-in type that isn't a float. 1268static FloatingRank getFloatingRank(QualType T) { 1269 if (const ComplexType *CT = T->getAsComplexType()) 1270 return getFloatingRank(CT->getElementType()); 1271 1272 switch (T->getAsBuiltinType()->getKind()) { 1273 default: assert(0 && "getFloatingRank(): not a floating type"); 1274 case BuiltinType::Float: return FloatRank; 1275 case BuiltinType::Double: return DoubleRank; 1276 case BuiltinType::LongDouble: return LongDoubleRank; 1277 } 1278} 1279 1280/// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1281/// point or a complex type (based on typeDomain/typeSize). 1282/// 'typeDomain' is a real floating point or complex type. 1283/// 'typeSize' is a real floating point or complex type. 1284QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, 1285 QualType Domain) const { 1286 FloatingRank EltRank = getFloatingRank(Size); 1287 if (Domain->isComplexType()) { 1288 switch (EltRank) { 1289 default: assert(0 && "getFloatingRank(): illegal value for rank"); 1290 case FloatRank: return FloatComplexTy; 1291 case DoubleRank: return DoubleComplexTy; 1292 case LongDoubleRank: return LongDoubleComplexTy; 1293 } 1294 } 1295 1296 assert(Domain->isRealFloatingType() && "Unknown domain!"); 1297 switch (EltRank) { 1298 default: assert(0 && "getFloatingRank(): illegal value for rank"); 1299 case FloatRank: return FloatTy; 1300 case DoubleRank: return DoubleTy; 1301 case LongDoubleRank: return LongDoubleTy; 1302 } 1303} 1304 1305/// getFloatingTypeOrder - Compare the rank of the two specified floating 1306/// point types, ignoring the domain of the type (i.e. 'double' == 1307/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1308/// LHS < RHS, return -1. 1309int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { 1310 FloatingRank LHSR = getFloatingRank(LHS); 1311 FloatingRank RHSR = getFloatingRank(RHS); 1312 1313 if (LHSR == RHSR) 1314 return 0; 1315 if (LHSR > RHSR) 1316 return 1; 1317 return -1; 1318} 1319 1320/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This 1321/// routine will assert if passed a built-in type that isn't an integer or enum, 1322/// or if it is not canonicalized. 1323static unsigned getIntegerRank(Type *T) { 1324 assert(T->isCanonical() && "T should be canonicalized"); 1325 if (isa<EnumType>(T)) 1326 return 4; 1327 1328 switch (cast<BuiltinType>(T)->getKind()) { 1329 default: assert(0 && "getIntegerRank(): not a built-in integer"); 1330 case BuiltinType::Bool: 1331 return 1; 1332 case BuiltinType::Char_S: 1333 case BuiltinType::Char_U: 1334 case BuiltinType::SChar: 1335 case BuiltinType::UChar: 1336 return 2; 1337 case BuiltinType::Short: 1338 case BuiltinType::UShort: 1339 return 3; 1340 case BuiltinType::Int: 1341 case BuiltinType::UInt: 1342 return 4; 1343 case BuiltinType::Long: 1344 case BuiltinType::ULong: 1345 return 5; 1346 case BuiltinType::LongLong: 1347 case BuiltinType::ULongLong: 1348 return 6; 1349 } 1350} 1351 1352/// getIntegerTypeOrder - Returns the highest ranked integer type: 1353/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1354/// LHS < RHS, return -1. 1355int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { 1356 Type *LHSC = getCanonicalType(LHS).getTypePtr(); 1357 Type *RHSC = getCanonicalType(RHS).getTypePtr(); 1358 if (LHSC == RHSC) return 0; 1359 1360 bool LHSUnsigned = LHSC->isUnsignedIntegerType(); 1361 bool RHSUnsigned = RHSC->isUnsignedIntegerType(); 1362 1363 unsigned LHSRank = getIntegerRank(LHSC); 1364 unsigned RHSRank = getIntegerRank(RHSC); 1365 1366 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. 1367 if (LHSRank == RHSRank) return 0; 1368 return LHSRank > RHSRank ? 1 : -1; 1369 } 1370 1371 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. 1372 if (LHSUnsigned) { 1373 // If the unsigned [LHS] type is larger, return it. 1374 if (LHSRank >= RHSRank) 1375 return 1; 1376 1377 // If the signed type can represent all values of the unsigned type, it 1378 // wins. Because we are dealing with 2's complement and types that are 1379 // powers of two larger than each other, this is always safe. 1380 return -1; 1381 } 1382 1383 // If the unsigned [RHS] type is larger, return it. 1384 if (RHSRank >= LHSRank) 1385 return -1; 1386 1387 // If the signed type can represent all values of the unsigned type, it 1388 // wins. Because we are dealing with 2's complement and types that are 1389 // powers of two larger than each other, this is always safe. 1390 return 1; 1391} 1392 1393// getCFConstantStringType - Return the type used for constant CFStrings. 1394QualType ASTContext::getCFConstantStringType() { 1395 if (!CFConstantStringTypeDecl) { 1396 CFConstantStringTypeDecl = 1397 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 1398 &Idents.get("NSConstantString")); 1399 QualType FieldTypes[4]; 1400 1401 // const int *isa; 1402 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); 1403 // int flags; 1404 FieldTypes[1] = IntTy; 1405 // const char *str; 1406 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); 1407 // long length; 1408 FieldTypes[3] = LongTy; 1409 // Create fields 1410 FieldDecl *FieldDecls[4]; 1411 1412 for (unsigned i = 0; i < 4; ++i) 1413 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0, 1414 FieldTypes[i]); 1415 1416 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4); 1417 } 1418 1419 return getTagDeclType(CFConstantStringTypeDecl); 1420} 1421 1422QualType ASTContext::getObjCFastEnumerationStateType() 1423{ 1424 if (!ObjCFastEnumerationStateTypeDecl) { 1425 QualType FieldTypes[] = { 1426 UnsignedLongTy, 1427 getPointerType(ObjCIdType), 1428 getPointerType(UnsignedLongTy), 1429 getConstantArrayType(UnsignedLongTy, 1430 llvm::APInt(32, 5), ArrayType::Normal, 0) 1431 }; 1432 1433 FieldDecl *FieldDecls[4]; 1434 for (size_t i = 0; i < 4; ++i) 1435 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0, 1436 FieldTypes[i]); 1437 1438 ObjCFastEnumerationStateTypeDecl = 1439 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 1440 &Idents.get("__objcFastEnumerationState")); 1441 1442 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4); 1443 } 1444 1445 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 1446} 1447 1448// This returns true if a type has been typedefed to BOOL: 1449// typedef <type> BOOL; 1450static bool isTypeTypedefedAsBOOL(QualType T) { 1451 if (const TypedefType *TT = dyn_cast<TypedefType>(T)) 1452 if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) 1453 return II->isStr("BOOL"); 1454 1455 return false; 1456} 1457 1458/// getObjCEncodingTypeSize returns size of type for objective-c encoding 1459/// purpose. 1460int ASTContext::getObjCEncodingTypeSize(QualType type) { 1461 uint64_t sz = getTypeSize(type); 1462 1463 // Make all integer and enum types at least as large as an int 1464 if (sz > 0 && type->isIntegralType()) 1465 sz = std::max(sz, getTypeSize(IntTy)); 1466 // Treat arrays as pointers, since that's how they're passed in. 1467 else if (type->isArrayType()) 1468 sz = getTypeSize(VoidPtrTy); 1469 return sz / getTypeSize(CharTy); 1470} 1471 1472/// getObjCEncodingForMethodDecl - Return the encoded type for this method 1473/// declaration. 1474void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, 1475 std::string& S) { 1476 // FIXME: This is not very efficient. 1477 // Encode type qualifer, 'in', 'inout', etc. for the return type. 1478 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); 1479 // Encode result type. 1480 getObjCEncodingForType(Decl->getResultType(), S); 1481 // Compute size of all parameters. 1482 // Start with computing size of a pointer in number of bytes. 1483 // FIXME: There might(should) be a better way of doing this computation! 1484 SourceLocation Loc; 1485 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); 1486 // The first two arguments (self and _cmd) are pointers; account for 1487 // their size. 1488 int ParmOffset = 2 * PtrSize; 1489 int NumOfParams = Decl->getNumParams(); 1490 for (int i = 0; i < NumOfParams; i++) { 1491 QualType PType = Decl->getParamDecl(i)->getType(); 1492 int sz = getObjCEncodingTypeSize (PType); 1493 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); 1494 ParmOffset += sz; 1495 } 1496 S += llvm::utostr(ParmOffset); 1497 S += "@0:"; 1498 S += llvm::utostr(PtrSize); 1499 1500 // Argument types. 1501 ParmOffset = 2 * PtrSize; 1502 for (int i = 0; i < NumOfParams; i++) { 1503 QualType PType = Decl->getParamDecl(i)->getType(); 1504 // Process argument qualifiers for user supplied arguments; such as, 1505 // 'in', 'inout', etc. 1506 getObjCEncodingForTypeQualifier( 1507 Decl->getParamDecl(i)->getObjCDeclQualifier(), S); 1508 getObjCEncodingForType(PType, S); 1509 S += llvm::utostr(ParmOffset); 1510 ParmOffset += getObjCEncodingTypeSize(PType); 1511 } 1512} 1513 1514/// getObjCEncodingForPropertyDecl - Return the encoded type for this 1515/// method declaration. If non-NULL, Container must be either an 1516/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be 1517/// NULL when getting encodings for protocol properties. 1518void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 1519 const Decl *Container, 1520 std::string& S) { 1521 // Collect information from the property implementation decl(s). 1522 bool Dynamic = false; 1523 ObjCPropertyImplDecl *SynthesizePID = 0; 1524 1525 // FIXME: Duplicated code due to poor abstraction. 1526 if (Container) { 1527 if (const ObjCCategoryImplDecl *CID = 1528 dyn_cast<ObjCCategoryImplDecl>(Container)) { 1529 for (ObjCCategoryImplDecl::propimpl_iterator 1530 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) { 1531 ObjCPropertyImplDecl *PID = *i; 1532 if (PID->getPropertyDecl() == PD) { 1533 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 1534 Dynamic = true; 1535 } else { 1536 SynthesizePID = PID; 1537 } 1538 } 1539 } 1540 } else { 1541 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); 1542 for (ObjCCategoryImplDecl::propimpl_iterator 1543 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) { 1544 ObjCPropertyImplDecl *PID = *i; 1545 if (PID->getPropertyDecl() == PD) { 1546 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 1547 Dynamic = true; 1548 } else { 1549 SynthesizePID = PID; 1550 } 1551 } 1552 } 1553 } 1554 } 1555 1556 // FIXME: This is not very efficient. 1557 S = "T"; 1558 1559 // Encode result type. 1560 // FIXME: GCC uses a generating_property_type_encoding mode during 1561 // this part. Investigate. 1562 getObjCEncodingForType(PD->getType(), S); 1563 1564 if (PD->isReadOnly()) { 1565 S += ",R"; 1566 } else { 1567 switch (PD->getSetterKind()) { 1568 case ObjCPropertyDecl::Assign: break; 1569 case ObjCPropertyDecl::Copy: S += ",C"; break; 1570 case ObjCPropertyDecl::Retain: S += ",&"; break; 1571 } 1572 } 1573 1574 // It really isn't clear at all what this means, since properties 1575 // are "dynamic by default". 1576 if (Dynamic) 1577 S += ",D"; 1578 1579 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 1580 S += ",G"; 1581 S += PD->getGetterName().getAsString(); 1582 } 1583 1584 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 1585 S += ",S"; 1586 S += PD->getSetterName().getAsString(); 1587 } 1588 1589 if (SynthesizePID) { 1590 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); 1591 S += ",V"; 1592 S += OID->getNameAsString(); 1593 } 1594 1595 // FIXME: OBJCGC: weak & strong 1596} 1597 1598void ASTContext::getObjCEncodingForType(QualType T, std::string& S, 1599 bool NameFields) const { 1600 // We follow the behavior of gcc, expanding structures which are 1601 // directly pointed to, and expanding embedded structures. Note that 1602 // these rules are sufficient to prevent recursive encoding of the 1603 // same type. 1604 getObjCEncodingForTypeImpl(T, S, true, true, NameFields); 1605} 1606 1607void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, 1608 bool ExpandPointedToStructures, 1609 bool ExpandStructures, 1610 bool NameFields) const { 1611 if (const BuiltinType *BT = T->getAsBuiltinType()) { 1612 char encoding; 1613 switch (BT->getKind()) { 1614 default: assert(0 && "Unhandled builtin type kind"); 1615 case BuiltinType::Void: encoding = 'v'; break; 1616 case BuiltinType::Bool: encoding = 'B'; break; 1617 case BuiltinType::Char_U: 1618 case BuiltinType::UChar: encoding = 'C'; break; 1619 case BuiltinType::UShort: encoding = 'S'; break; 1620 case BuiltinType::UInt: encoding = 'I'; break; 1621 case BuiltinType::ULong: encoding = 'L'; break; 1622 case BuiltinType::ULongLong: encoding = 'Q'; break; 1623 case BuiltinType::Char_S: 1624 case BuiltinType::SChar: encoding = 'c'; break; 1625 case BuiltinType::Short: encoding = 's'; break; 1626 case BuiltinType::Int: encoding = 'i'; break; 1627 case BuiltinType::Long: encoding = 'l'; break; 1628 case BuiltinType::LongLong: encoding = 'q'; break; 1629 case BuiltinType::Float: encoding = 'f'; break; 1630 case BuiltinType::Double: encoding = 'd'; break; 1631 case BuiltinType::LongDouble: encoding = 'd'; break; 1632 } 1633 1634 S += encoding; 1635 } 1636 else if (T->isObjCQualifiedIdType()) { 1637 // Treat id<P...> same as 'id' for encoding purposes. 1638 return getObjCEncodingForTypeImpl(getObjCIdType(), S, 1639 ExpandPointedToStructures, 1640 ExpandStructures, NameFields); 1641 } 1642 else if (const PointerType *PT = T->getAsPointerType()) { 1643 QualType PointeeTy = PT->getPointeeType(); 1644 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) { 1645 S += '@'; 1646 return; 1647 } else if (isObjCClassType(PointeeTy)) { 1648 S += '#'; 1649 return; 1650 } else if (isObjCSelType(PointeeTy)) { 1651 S += ':'; 1652 return; 1653 } 1654 1655 if (PointeeTy->isCharType()) { 1656 // char pointer types should be encoded as '*' unless it is a 1657 // type that has been typedef'd to 'BOOL'. 1658 if (!isTypeTypedefedAsBOOL(PointeeTy)) { 1659 S += '*'; 1660 return; 1661 } 1662 } 1663 1664 S += '^'; 1665 getObjCEncodingForTypeImpl(PT->getPointeeType(), S, 1666 false, ExpandPointedToStructures, 1667 NameFields); 1668 } else if (const ArrayType *AT = 1669 // Ignore type qualifiers etc. 1670 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { 1671 S += '['; 1672 1673 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 1674 S += llvm::utostr(CAT->getSize().getZExtValue()); 1675 else 1676 assert(0 && "Unhandled array type!"); 1677 1678 getObjCEncodingForTypeImpl(AT->getElementType(), S, 1679 false, ExpandStructures, NameFields); 1680 S += ']'; 1681 } else if (T->getAsFunctionType()) { 1682 S += '?'; 1683 } else if (const RecordType *RTy = T->getAsRecordType()) { 1684 RecordDecl *RDecl = RTy->getDecl(); 1685 S += RDecl->isUnion() ? '(' : '{'; 1686 // Anonymous structures print as '?' 1687 if (const IdentifierInfo *II = RDecl->getIdentifier()) { 1688 S += II->getName(); 1689 } else { 1690 S += '?'; 1691 } 1692 if (ExpandStructures) { 1693 S += '='; 1694 for (int i = 0; i < RDecl->getNumMembers(); i++) { 1695 FieldDecl *FD = RDecl->getMember(i); 1696 if (NameFields) { 1697 S += '"'; 1698 S += FD->getNameAsString(); 1699 S += '"'; 1700 } 1701 1702 // Special case bit-fields. 1703 if (const Expr *E = FD->getBitWidth()) { 1704 // FIXME: Fix constness. 1705 ASTContext *Ctx = const_cast<ASTContext*>(this); 1706 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue(); 1707 // FIXME: Obj-C is losing information about the type size 1708 // here. Investigate if this is a problem. 1709 S += 'b'; 1710 S += llvm::utostr(N); 1711 } else { 1712 getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields); 1713 } 1714 } 1715 } 1716 S += RDecl->isUnion() ? ')' : '}'; 1717 } else if (T->isEnumeralType()) { 1718 S += 'i'; 1719 } else if (T->isBlockPointerType()) { 1720 S += '^'; // This type string is the same as general pointers. 1721 } else 1722 assert(0 && "@encode for type not implemented!"); 1723} 1724 1725void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 1726 std::string& S) const { 1727 if (QT & Decl::OBJC_TQ_In) 1728 S += 'n'; 1729 if (QT & Decl::OBJC_TQ_Inout) 1730 S += 'N'; 1731 if (QT & Decl::OBJC_TQ_Out) 1732 S += 'o'; 1733 if (QT & Decl::OBJC_TQ_Bycopy) 1734 S += 'O'; 1735 if (QT & Decl::OBJC_TQ_Byref) 1736 S += 'R'; 1737 if (QT & Decl::OBJC_TQ_Oneway) 1738 S += 'V'; 1739} 1740 1741void ASTContext::setBuiltinVaListType(QualType T) 1742{ 1743 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); 1744 1745 BuiltinVaListType = T; 1746} 1747 1748void ASTContext::setObjCIdType(TypedefDecl *TD) 1749{ 1750 ObjCIdType = getTypedefType(TD); 1751 1752 // typedef struct objc_object *id; 1753 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); 1754 assert(ptr && "'id' incorrectly typed"); 1755 const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 1756 assert(rec && "'id' incorrectly typed"); 1757 IdStructType = rec; 1758} 1759 1760void ASTContext::setObjCSelType(TypedefDecl *TD) 1761{ 1762 ObjCSelType = getTypedefType(TD); 1763 1764 // typedef struct objc_selector *SEL; 1765 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); 1766 assert(ptr && "'SEL' incorrectly typed"); 1767 const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 1768 assert(rec && "'SEL' incorrectly typed"); 1769 SelStructType = rec; 1770} 1771 1772void ASTContext::setObjCProtoType(QualType QT) 1773{ 1774 ObjCProtoType = QT; 1775} 1776 1777void ASTContext::setObjCClassType(TypedefDecl *TD) 1778{ 1779 ObjCClassType = getTypedefType(TD); 1780 1781 // typedef struct objc_class *Class; 1782 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); 1783 assert(ptr && "'Class' incorrectly typed"); 1784 const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 1785 assert(rec && "'Class' incorrectly typed"); 1786 ClassStructType = rec; 1787} 1788 1789void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { 1790 assert(ObjCConstantStringType.isNull() && 1791 "'NSConstantString' type already set!"); 1792 1793 ObjCConstantStringType = getObjCInterfaceType(Decl); 1794} 1795 1796/// getFromTargetType - Given one of the integer types provided by 1797/// TargetInfo, produce the corresponding type. The unsigned @p Type 1798/// is actually a value of type @c TargetInfo::IntType. 1799QualType ASTContext::getFromTargetType(unsigned Type) const { 1800 switch (Type) { 1801 case TargetInfo::NoInt: return QualType(); 1802 case TargetInfo::SignedShort: return ShortTy; 1803 case TargetInfo::UnsignedShort: return UnsignedShortTy; 1804 case TargetInfo::SignedInt: return IntTy; 1805 case TargetInfo::UnsignedInt: return UnsignedIntTy; 1806 case TargetInfo::SignedLong: return LongTy; 1807 case TargetInfo::UnsignedLong: return UnsignedLongTy; 1808 case TargetInfo::SignedLongLong: return LongLongTy; 1809 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; 1810 } 1811 1812 assert(false && "Unhandled TargetInfo::IntType value"); 1813 return QualType(); 1814} 1815 1816//===----------------------------------------------------------------------===// 1817// Type Predicates. 1818//===----------------------------------------------------------------------===// 1819 1820/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 1821/// to an object type. This includes "id" and "Class" (two 'special' pointers 1822/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 1823/// ID type). 1824bool ASTContext::isObjCObjectPointerType(QualType Ty) const { 1825 if (Ty->isObjCQualifiedIdType()) 1826 return true; 1827 1828 // Blocks are objects. 1829 if (Ty->isBlockPointerType()) 1830 return true; 1831 1832 // All other object types are pointers. 1833 if (!Ty->isPointerType()) 1834 return false; 1835 1836 // Check to see if this is 'id' or 'Class', both of which are typedefs for 1837 // pointer types. This looks for the typedef specifically, not for the 1838 // underlying type. 1839 if (Ty == getObjCIdType() || Ty == getObjCClassType()) 1840 return true; 1841 1842 // If this a pointer to an interface (e.g. NSString*), it is ok. 1843 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType(); 1844} 1845 1846//===----------------------------------------------------------------------===// 1847// Type Compatibility Testing 1848//===----------------------------------------------------------------------===// 1849 1850/// typesAreBlockCompatible - This routine is called when comparing two 1851/// block types. Types must be strictly compatible here. For example, 1852/// C unfortunately doesn't produce an error for the following: 1853/// 1854/// int (*emptyArgFunc)(); 1855/// int (*intArgList)(int) = emptyArgFunc; 1856/// 1857/// For blocks, we will produce an error for the following (similar to C++): 1858/// 1859/// int (^emptyArgBlock)(); 1860/// int (^intArgBlock)(int) = emptyArgBlock; 1861/// 1862/// FIXME: When the dust settles on this integration, fold this into mergeTypes. 1863/// 1864bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) { 1865 return getCanonicalType(lhs) == getCanonicalType(rhs); 1866} 1867 1868/// areCompatVectorTypes - Return true if the two specified vector types are 1869/// compatible. 1870static bool areCompatVectorTypes(const VectorType *LHS, 1871 const VectorType *RHS) { 1872 assert(LHS->isCanonical() && RHS->isCanonical()); 1873 return LHS->getElementType() == RHS->getElementType() && 1874 LHS->getNumElements() == RHS->getNumElements(); 1875} 1876 1877/// canAssignObjCInterfaces - Return true if the two interface types are 1878/// compatible for assignment from RHS to LHS. This handles validation of any 1879/// protocol qualifiers on the LHS or RHS. 1880/// 1881bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 1882 const ObjCInterfaceType *RHS) { 1883 // Verify that the base decls are compatible: the RHS must be a subclass of 1884 // the LHS. 1885 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) 1886 return false; 1887 1888 // RHS must have a superset of the protocols in the LHS. If the LHS is not 1889 // protocol qualified at all, then we are good. 1890 if (!isa<ObjCQualifiedInterfaceType>(LHS)) 1891 return true; 1892 1893 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it 1894 // isn't a superset. 1895 if (!isa<ObjCQualifiedInterfaceType>(RHS)) 1896 return true; // FIXME: should return false! 1897 1898 // Finally, we must have two protocol-qualified interfaces. 1899 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS); 1900 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS); 1901 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(); 1902 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end(); 1903 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(); 1904 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end(); 1905 1906 // All protocols in LHS must have a presence in RHS. Since the protocol lists 1907 // are both sorted alphabetically and have no duplicates, we can scan RHS and 1908 // LHS in a single parallel scan until we run out of elements in LHS. 1909 assert(LHSPI != LHSPE && "Empty LHS protocol list?"); 1910 ObjCProtocolDecl *LHSProto = *LHSPI; 1911 1912 while (RHSPI != RHSPE) { 1913 ObjCProtocolDecl *RHSProto = *RHSPI++; 1914 // If the RHS has a protocol that the LHS doesn't, ignore it. 1915 if (RHSProto != LHSProto) 1916 continue; 1917 1918 // Otherwise, the RHS does have this element. 1919 ++LHSPI; 1920 if (LHSPI == LHSPE) 1921 return true; // All protocols in LHS exist in RHS. 1922 1923 LHSProto = *LHSPI; 1924 } 1925 1926 // If we got here, we didn't find one of the LHS's protocols in the RHS list. 1927 return false; 1928} 1929 1930/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, 1931/// both shall have the identically qualified version of a compatible type. 1932/// C99 6.2.7p1: Two types have compatible types if their types are the 1933/// same. See 6.7.[2,3,5] for additional rules. 1934bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { 1935 return !mergeTypes(LHS, RHS).isNull(); 1936} 1937 1938QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { 1939 const FunctionType *lbase = lhs->getAsFunctionType(); 1940 const FunctionType *rbase = rhs->getAsFunctionType(); 1941 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); 1942 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); 1943 bool allLTypes = true; 1944 bool allRTypes = true; 1945 1946 // Check return type 1947 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); 1948 if (retType.isNull()) return QualType(); 1949 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) 1950 allLTypes = false; 1951 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) 1952 allRTypes = false; 1953 1954 if (lproto && rproto) { // two C99 style function prototypes 1955 unsigned lproto_nargs = lproto->getNumArgs(); 1956 unsigned rproto_nargs = rproto->getNumArgs(); 1957 1958 // Compatible functions must have the same number of arguments 1959 if (lproto_nargs != rproto_nargs) 1960 return QualType(); 1961 1962 // Variadic and non-variadic functions aren't compatible 1963 if (lproto->isVariadic() != rproto->isVariadic()) 1964 return QualType(); 1965 1966 if (lproto->getTypeQuals() != rproto->getTypeQuals()) 1967 return QualType(); 1968 1969 // Check argument compatibility 1970 llvm::SmallVector<QualType, 10> types; 1971 for (unsigned i = 0; i < lproto_nargs; i++) { 1972 QualType largtype = lproto->getArgType(i).getUnqualifiedType(); 1973 QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); 1974 QualType argtype = mergeTypes(largtype, rargtype); 1975 if (argtype.isNull()) return QualType(); 1976 types.push_back(argtype); 1977 if (getCanonicalType(argtype) != getCanonicalType(largtype)) 1978 allLTypes = false; 1979 if (getCanonicalType(argtype) != getCanonicalType(rargtype)) 1980 allRTypes = false; 1981 } 1982 if (allLTypes) return lhs; 1983 if (allRTypes) return rhs; 1984 return getFunctionType(retType, types.begin(), types.size(), 1985 lproto->isVariadic(), lproto->getTypeQuals()); 1986 } 1987 1988 if (lproto) allRTypes = false; 1989 if (rproto) allLTypes = false; 1990 1991 const FunctionTypeProto *proto = lproto ? lproto : rproto; 1992 if (proto) { 1993 if (proto->isVariadic()) return QualType(); 1994 // Check that the types are compatible with the types that 1995 // would result from default argument promotions (C99 6.7.5.3p15). 1996 // The only types actually affected are promotable integer 1997 // types and floats, which would be passed as a different 1998 // type depending on whether the prototype is visible. 1999 unsigned proto_nargs = proto->getNumArgs(); 2000 for (unsigned i = 0; i < proto_nargs; ++i) { 2001 QualType argTy = proto->getArgType(i); 2002 if (argTy->isPromotableIntegerType() || 2003 getCanonicalType(argTy).getUnqualifiedType() == FloatTy) 2004 return QualType(); 2005 } 2006 2007 if (allLTypes) return lhs; 2008 if (allRTypes) return rhs; 2009 return getFunctionType(retType, proto->arg_type_begin(), 2010 proto->getNumArgs(), lproto->isVariadic(), 2011 lproto->getTypeQuals()); 2012 } 2013 2014 if (allLTypes) return lhs; 2015 if (allRTypes) return rhs; 2016 return getFunctionTypeNoProto(retType); 2017} 2018 2019QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { 2020 // C++ [expr]: If an expression initially has the type "reference to T", the 2021 // type is adjusted to "T" prior to any further analysis, the expression 2022 // designates the object or function denoted by the reference, and the 2023 // expression is an lvalue. 2024 // FIXME: C++ shouldn't be going through here! The rules are different 2025 // enough that they should be handled separately. 2026 if (const ReferenceType *RT = LHS->getAsReferenceType()) 2027 LHS = RT->getPointeeType(); 2028 if (const ReferenceType *RT = RHS->getAsReferenceType()) 2029 RHS = RT->getPointeeType(); 2030 2031 QualType LHSCan = getCanonicalType(LHS), 2032 RHSCan = getCanonicalType(RHS); 2033 2034 // If two types are identical, they are compatible. 2035 if (LHSCan == RHSCan) 2036 return LHS; 2037 2038 // If the qualifiers are different, the types aren't compatible 2039 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() || 2040 LHSCan.getAddressSpace() != RHSCan.getAddressSpace()) 2041 return QualType(); 2042 2043 Type::TypeClass LHSClass = LHSCan->getTypeClass(); 2044 Type::TypeClass RHSClass = RHSCan->getTypeClass(); 2045 2046 // We want to consider the two function types to be the same for these 2047 // comparisons, just force one to the other. 2048 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; 2049 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; 2050 2051 // Same as above for arrays 2052 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) 2053 LHSClass = Type::ConstantArray; 2054 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) 2055 RHSClass = Type::ConstantArray; 2056 2057 // Canonicalize ExtVector -> Vector. 2058 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; 2059 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; 2060 2061 // Consider qualified interfaces and interfaces the same. 2062 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface; 2063 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface; 2064 2065 // If the canonical type classes don't match. 2066 if (LHSClass != RHSClass) { 2067 // ID is compatible with all qualified id types. 2068 if (LHS->isObjCQualifiedIdType()) { 2069 if (const PointerType *PT = RHS->getAsPointerType()) { 2070 QualType pType = PT->getPointeeType(); 2071 if (isObjCIdType(pType)) 2072 return LHS; 2073 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). 2074 // Unfortunately, this API is part of Sema (which we don't have access 2075 // to. Need to refactor. The following check is insufficient, since we 2076 // need to make sure the class implements the protocol. 2077 if (pType->isObjCInterfaceType()) 2078 return LHS; 2079 } 2080 } 2081 if (RHS->isObjCQualifiedIdType()) { 2082 if (const PointerType *PT = LHS->getAsPointerType()) { 2083 QualType pType = PT->getPointeeType(); 2084 if (isObjCIdType(pType)) 2085 return RHS; 2086 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). 2087 // Unfortunately, this API is part of Sema (which we don't have access 2088 // to. Need to refactor. The following check is insufficient, since we 2089 // need to make sure the class implements the protocol. 2090 if (pType->isObjCInterfaceType()) 2091 return RHS; 2092 } 2093 } 2094 2095 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, 2096 // a signed integer type, or an unsigned integer type. 2097 if (const EnumType* ETy = LHS->getAsEnumType()) { 2098 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) 2099 return RHS; 2100 } 2101 if (const EnumType* ETy = RHS->getAsEnumType()) { 2102 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) 2103 return LHS; 2104 } 2105 2106 return QualType(); 2107 } 2108 2109 // The canonical type classes match. 2110 switch (LHSClass) { 2111 case Type::Pointer: 2112 { 2113 // Merge two pointer types, while trying to preserve typedef info 2114 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType(); 2115 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType(); 2116 QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 2117 if (ResultType.isNull()) return QualType(); 2118 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 2119 return LHS; 2120 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 2121 return RHS; 2122 return getPointerType(ResultType); 2123 } 2124 case Type::ConstantArray: 2125 { 2126 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); 2127 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); 2128 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) 2129 return QualType(); 2130 2131 QualType LHSElem = getAsArrayType(LHS)->getElementType(); 2132 QualType RHSElem = getAsArrayType(RHS)->getElementType(); 2133 QualType ResultType = mergeTypes(LHSElem, RHSElem); 2134 if (ResultType.isNull()) return QualType(); 2135 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 2136 return LHS; 2137 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 2138 return RHS; 2139 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), 2140 ArrayType::ArraySizeModifier(), 0); 2141 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), 2142 ArrayType::ArraySizeModifier(), 0); 2143 const VariableArrayType* LVAT = getAsVariableArrayType(LHS); 2144 const VariableArrayType* RVAT = getAsVariableArrayType(RHS); 2145 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 2146 return LHS; 2147 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 2148 return RHS; 2149 if (LVAT) { 2150 // FIXME: This isn't correct! But tricky to implement because 2151 // the array's size has to be the size of LHS, but the type 2152 // has to be different. 2153 return LHS; 2154 } 2155 if (RVAT) { 2156 // FIXME: This isn't correct! But tricky to implement because 2157 // the array's size has to be the size of RHS, but the type 2158 // has to be different. 2159 return RHS; 2160 } 2161 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; 2162 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; 2163 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0); 2164 } 2165 case Type::FunctionNoProto: 2166 return mergeFunctionTypes(LHS, RHS); 2167 case Type::Tagged: 2168 // FIXME: Why are these compatible? 2169 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS; 2170 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS; 2171 return QualType(); 2172 case Type::Builtin: 2173 // Only exactly equal builtin types are compatible, which is tested above. 2174 return QualType(); 2175 case Type::Vector: 2176 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) 2177 return LHS; 2178 return QualType(); 2179 case Type::ObjCInterface: 2180 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces 2181 // for checking assignment/comparison safety 2182 return QualType(); 2183 default: 2184 assert(0 && "unexpected type"); 2185 return QualType(); 2186 } 2187} 2188 2189//===----------------------------------------------------------------------===// 2190// Integer Predicates 2191//===----------------------------------------------------------------------===// 2192unsigned ASTContext::getIntWidth(QualType T) { 2193 if (T == BoolTy) 2194 return 1; 2195 // At the moment, only bool has padding bits 2196 return (unsigned)getTypeSize(T); 2197} 2198 2199QualType ASTContext::getCorrespondingUnsignedType(QualType T) { 2200 assert(T->isSignedIntegerType() && "Unexpected type"); 2201 if (const EnumType* ETy = T->getAsEnumType()) 2202 T = ETy->getDecl()->getIntegerType(); 2203 const BuiltinType* BTy = T->getAsBuiltinType(); 2204 assert (BTy && "Unexpected signed integer type"); 2205 switch (BTy->getKind()) { 2206 case BuiltinType::Char_S: 2207 case BuiltinType::SChar: 2208 return UnsignedCharTy; 2209 case BuiltinType::Short: 2210 return UnsignedShortTy; 2211 case BuiltinType::Int: 2212 return UnsignedIntTy; 2213 case BuiltinType::Long: 2214 return UnsignedLongTy; 2215 case BuiltinType::LongLong: 2216 return UnsignedLongLongTy; 2217 default: 2218 assert(0 && "Unexpected signed integer type"); 2219 return QualType(); 2220 } 2221} 2222 2223 2224//===----------------------------------------------------------------------===// 2225// Serialization Support 2226//===----------------------------------------------------------------------===// 2227 2228/// Emit - Serialize an ASTContext object to Bitcode. 2229void ASTContext::Emit(llvm::Serializer& S) const { 2230 S.Emit(LangOpts); 2231 S.EmitRef(SourceMgr); 2232 S.EmitRef(Target); 2233 S.EmitRef(Idents); 2234 S.EmitRef(Selectors); 2235 2236 // Emit the size of the type vector so that we can reserve that size 2237 // when we reconstitute the ASTContext object. 2238 S.EmitInt(Types.size()); 2239 2240 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); 2241 I!=E;++I) 2242 (*I)->Emit(S); 2243 2244 S.EmitOwnedPtr(TUDecl); 2245 2246 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); 2247} 2248 2249ASTContext* ASTContext::Create(llvm::Deserializer& D) { 2250 2251 // Read the language options. 2252 LangOptions LOpts; 2253 LOpts.Read(D); 2254 2255 SourceManager &SM = D.ReadRef<SourceManager>(); 2256 TargetInfo &t = D.ReadRef<TargetInfo>(); 2257 IdentifierTable &idents = D.ReadRef<IdentifierTable>(); 2258 SelectorTable &sels = D.ReadRef<SelectorTable>(); 2259 2260 unsigned size_reserve = D.ReadInt(); 2261 2262 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, 2263 size_reserve); 2264 2265 for (unsigned i = 0; i < size_reserve; ++i) 2266 Type::Create(*A,i,D); 2267 2268 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A)); 2269 2270 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); 2271 2272 return A; 2273} 2274