Type.cpp revision 208600
1//===--- Type.cpp - Type representation and manipulation ------------------===// 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 type-related functionality. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/AST/ASTContext.h" 15#include "clang/AST/Type.h" 16#include "clang/AST/DeclCXX.h" 17#include "clang/AST/DeclObjC.h" 18#include "clang/AST/DeclTemplate.h" 19#include "clang/AST/Expr.h" 20#include "clang/AST/PrettyPrinter.h" 21#include "clang/Basic/Specifiers.h" 22#include "llvm/ADT/StringExtras.h" 23#include "llvm/Support/raw_ostream.h" 24using namespace clang; 25 26bool QualType::isConstant(QualType T, ASTContext &Ctx) { 27 if (T.isConstQualified()) 28 return true; 29 30 if (const ArrayType *AT = Ctx.getAsArrayType(T)) 31 return AT->getElementType().isConstant(Ctx); 32 33 return false; 34} 35 36void Type::Destroy(ASTContext& C) { 37 this->~Type(); 38 C.Deallocate(this); 39} 40 41void VariableArrayType::Destroy(ASTContext& C) { 42 if (SizeExpr) 43 SizeExpr->Destroy(C); 44 this->~VariableArrayType(); 45 C.Deallocate(this); 46} 47 48void DependentSizedArrayType::Destroy(ASTContext& C) { 49 // FIXME: Resource contention like in ConstantArrayWithExprType ? 50 // May crash, depending on platform or a particular build. 51 // SizeExpr->Destroy(C); 52 this->~DependentSizedArrayType(); 53 C.Deallocate(this); 54} 55 56void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID, 57 ASTContext &Context, 58 QualType ET, 59 ArraySizeModifier SizeMod, 60 unsigned TypeQuals, 61 Expr *E) { 62 ID.AddPointer(ET.getAsOpaquePtr()); 63 ID.AddInteger(SizeMod); 64 ID.AddInteger(TypeQuals); 65 E->Profile(ID, Context, true); 66} 67 68void 69DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID, 70 ASTContext &Context, 71 QualType ElementType, Expr *SizeExpr) { 72 ID.AddPointer(ElementType.getAsOpaquePtr()); 73 SizeExpr->Profile(ID, Context, true); 74} 75 76void DependentSizedExtVectorType::Destroy(ASTContext& C) { 77 // FIXME: Deallocate size expression, once we're cloning properly. 78// if (SizeExpr) 79// SizeExpr->Destroy(C); 80 this->~DependentSizedExtVectorType(); 81 C.Deallocate(this); 82} 83 84/// getArrayElementTypeNoTypeQual - If this is an array type, return the 85/// element type of the array, potentially with type qualifiers missing. 86/// This method should never be used when type qualifiers are meaningful. 87const Type *Type::getArrayElementTypeNoTypeQual() const { 88 // If this is directly an array type, return it. 89 if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) 90 return ATy->getElementType().getTypePtr(); 91 92 // If the canonical form of this type isn't the right kind, reject it. 93 if (!isa<ArrayType>(CanonicalType)) 94 return 0; 95 96 // If this is a typedef for an array type, strip the typedef off without 97 // losing all typedef information. 98 return cast<ArrayType>(getUnqualifiedDesugaredType()) 99 ->getElementType().getTypePtr(); 100} 101 102/// \brief Retrieve the unqualified variant of the given type, removing as 103/// little sugar as possible. 104/// 105/// This routine looks through various kinds of sugar to find the 106/// least-desuraged type that is unqualified. For example, given: 107/// 108/// \code 109/// typedef int Integer; 110/// typedef const Integer CInteger; 111/// typedef CInteger DifferenceType; 112/// \endcode 113/// 114/// Executing \c getUnqualifiedTypeSlow() on the type \c DifferenceType will 115/// desugar until we hit the type \c Integer, which has no qualifiers on it. 116QualType QualType::getUnqualifiedTypeSlow() const { 117 QualType Cur = *this; 118 while (true) { 119 if (!Cur.hasQualifiers()) 120 return Cur; 121 122 const Type *CurTy = Cur.getTypePtr(); 123 switch (CurTy->getTypeClass()) { 124#define ABSTRACT_TYPE(Class, Parent) 125#define TYPE(Class, Parent) \ 126 case Type::Class: { \ 127 const Class##Type *Ty = cast<Class##Type>(CurTy); \ 128 if (!Ty->isSugared()) \ 129 return Cur.getLocalUnqualifiedType(); \ 130 Cur = Ty->desugar(); \ 131 break; \ 132 } 133#include "clang/AST/TypeNodes.def" 134 } 135 } 136 137 return Cur.getUnqualifiedType(); 138} 139 140/// getDesugaredType - Return the specified type with any "sugar" removed from 141/// the type. This takes off typedefs, typeof's etc. If the outer level of 142/// the type is already concrete, it returns it unmodified. This is similar 143/// to getting the canonical type, but it doesn't remove *all* typedefs. For 144/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 145/// concrete. 146QualType QualType::getDesugaredType(QualType T) { 147 QualifierCollector Qs; 148 149 QualType Cur = T; 150 while (true) { 151 const Type *CurTy = Qs.strip(Cur); 152 switch (CurTy->getTypeClass()) { 153#define ABSTRACT_TYPE(Class, Parent) 154#define TYPE(Class, Parent) \ 155 case Type::Class: { \ 156 const Class##Type *Ty = cast<Class##Type>(CurTy); \ 157 if (!Ty->isSugared()) \ 158 return Qs.apply(Cur); \ 159 Cur = Ty->desugar(); \ 160 break; \ 161 } 162#include "clang/AST/TypeNodes.def" 163 } 164 } 165} 166 167/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic 168/// sugar off the given type. This should produce an object of the 169/// same dynamic type as the canonical type. 170const Type *Type::getUnqualifiedDesugaredType() const { 171 const Type *Cur = this; 172 173 while (true) { 174 switch (Cur->getTypeClass()) { 175#define ABSTRACT_TYPE(Class, Parent) 176#define TYPE(Class, Parent) \ 177 case Class: { \ 178 const Class##Type *Ty = cast<Class##Type>(Cur); \ 179 if (!Ty->isSugared()) return Cur; \ 180 Cur = Ty->desugar().getTypePtr(); \ 181 break; \ 182 } 183#include "clang/AST/TypeNodes.def" 184 } 185 } 186} 187 188/// isVoidType - Helper method to determine if this is the 'void' type. 189bool Type::isVoidType() const { 190 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 191 return BT->getKind() == BuiltinType::Void; 192 return false; 193} 194 195bool Type::isObjectType() const { 196 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) || 197 isa<IncompleteArrayType>(CanonicalType) || isVoidType()) 198 return false; 199 return true; 200} 201 202bool Type::isDerivedType() const { 203 switch (CanonicalType->getTypeClass()) { 204 case Pointer: 205 case VariableArray: 206 case ConstantArray: 207 case IncompleteArray: 208 case FunctionProto: 209 case FunctionNoProto: 210 case LValueReference: 211 case RValueReference: 212 case Record: 213 return true; 214 default: 215 return false; 216 } 217} 218 219bool Type::isClassType() const { 220 if (const RecordType *RT = getAs<RecordType>()) 221 return RT->getDecl()->isClass(); 222 return false; 223} 224bool Type::isStructureType() const { 225 if (const RecordType *RT = getAs<RecordType>()) 226 return RT->getDecl()->isStruct(); 227 return false; 228} 229bool Type::isStructureOrClassType() const { 230 if (const RecordType *RT = getAs<RecordType>()) 231 return RT->getDecl()->isStruct() || RT->getDecl()->isClass(); 232 return false; 233} 234bool Type::isVoidPointerType() const { 235 if (const PointerType *PT = getAs<PointerType>()) 236 return PT->getPointeeType()->isVoidType(); 237 return false; 238} 239 240bool Type::isUnionType() const { 241 if (const RecordType *RT = getAs<RecordType>()) 242 return RT->getDecl()->isUnion(); 243 return false; 244} 245 246bool Type::isComplexType() const { 247 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 248 return CT->getElementType()->isFloatingType(); 249 return false; 250} 251 252bool Type::isComplexIntegerType() const { 253 // Check for GCC complex integer extension. 254 return getAsComplexIntegerType(); 255} 256 257const ComplexType *Type::getAsComplexIntegerType() const { 258 if (const ComplexType *Complex = getAs<ComplexType>()) 259 if (Complex->getElementType()->isIntegerType()) 260 return Complex; 261 return 0; 262} 263 264QualType Type::getPointeeType() const { 265 if (const PointerType *PT = getAs<PointerType>()) 266 return PT->getPointeeType(); 267 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 268 return OPT->getPointeeType(); 269 if (const BlockPointerType *BPT = getAs<BlockPointerType>()) 270 return BPT->getPointeeType(); 271 if (const ReferenceType *RT = getAs<ReferenceType>()) 272 return RT->getPointeeType(); 273 return QualType(); 274} 275 276/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length 277/// array types and types that contain variable array types in their 278/// declarator 279bool Type::isVariablyModifiedType() const { 280 // FIXME: We should really keep a "variably modified" bit in Type, rather 281 // than walking the type hierarchy to recompute it. 282 283 // A VLA is a variably modified type. 284 if (isVariableArrayType()) 285 return true; 286 287 // An array can contain a variably modified type 288 if (const Type *T = getArrayElementTypeNoTypeQual()) 289 return T->isVariablyModifiedType(); 290 291 // A pointer can point to a variably modified type. 292 // Also, C++ references and member pointers can point to a variably modified 293 // type, where VLAs appear as an extension to C++, and should be treated 294 // correctly. 295 if (const PointerType *PT = getAs<PointerType>()) 296 return PT->getPointeeType()->isVariablyModifiedType(); 297 if (const ReferenceType *RT = getAs<ReferenceType>()) 298 return RT->getPointeeType()->isVariablyModifiedType(); 299 if (const MemberPointerType *PT = getAs<MemberPointerType>()) 300 return PT->getPointeeType()->isVariablyModifiedType(); 301 302 // A function can return a variably modified type 303 // This one isn't completely obvious, but it follows from the 304 // definition in C99 6.7.5p3. Because of this rule, it's 305 // illegal to declare a function returning a variably modified type. 306 if (const FunctionType *FT = getAs<FunctionType>()) 307 return FT->getResultType()->isVariablyModifiedType(); 308 309 return false; 310} 311 312const RecordType *Type::getAsStructureType() const { 313 // If this is directly a structure type, return it. 314 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 315 if (RT->getDecl()->isStruct()) 316 return RT; 317 } 318 319 // If the canonical form of this type isn't the right kind, reject it. 320 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 321 if (!RT->getDecl()->isStruct()) 322 return 0; 323 324 // If this is a typedef for a structure type, strip the typedef off without 325 // losing all typedef information. 326 return cast<RecordType>(getUnqualifiedDesugaredType()); 327 } 328 return 0; 329} 330 331const RecordType *Type::getAsUnionType() const { 332 // If this is directly a union type, return it. 333 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 334 if (RT->getDecl()->isUnion()) 335 return RT; 336 } 337 338 // If the canonical form of this type isn't the right kind, reject it. 339 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 340 if (!RT->getDecl()->isUnion()) 341 return 0; 342 343 // If this is a typedef for a union type, strip the typedef off without 344 // losing all typedef information. 345 return cast<RecordType>(getUnqualifiedDesugaredType()); 346 } 347 348 return 0; 349} 350 351void ObjCInterfaceType::Destroy(ASTContext& C) { 352 this->~ObjCInterfaceType(); 353 C.Deallocate(this); 354} 355 356ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base, 357 ObjCProtocolDecl * const *Protocols, 358 unsigned NumProtocols) 359 : Type(ObjCObject, Canonical, false), 360 NumProtocols(NumProtocols), 361 BaseType(Base) { 362 assert(this->NumProtocols == NumProtocols && 363 "bitfield overflow in protocol count"); 364 if (NumProtocols) 365 memcpy(getProtocolStorage(), Protocols, 366 NumProtocols * sizeof(ObjCProtocolDecl*)); 367} 368 369void ObjCObjectTypeImpl::Destroy(ASTContext& C) { 370 this->~ObjCObjectTypeImpl(); 371 C.Deallocate(this); 372} 373 374const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const { 375 // There is no sugar for ObjCObjectType's, just return the canonical 376 // type pointer if it is the right class. There is no typedef information to 377 // return and these cannot be Address-space qualified. 378 if (const ObjCObjectType *T = getAs<ObjCObjectType>()) 379 if (T->getNumProtocols() && T->getInterface()) 380 return T; 381 return 0; 382} 383 384bool Type::isObjCQualifiedInterfaceType() const { 385 return getAsObjCQualifiedInterfaceType() != 0; 386} 387 388void ObjCObjectPointerType::Destroy(ASTContext& C) { 389 this->~ObjCObjectPointerType(); 390 C.Deallocate(this); 391} 392 393const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { 394 // There is no sugar for ObjCQualifiedIdType's, just return the canonical 395 // type pointer if it is the right class. 396 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 397 if (OPT->isObjCQualifiedIdType()) 398 return OPT; 399 } 400 return 0; 401} 402 403const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const { 404 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 405 if (OPT->getInterfaceType()) 406 return OPT; 407 } 408 return 0; 409} 410 411const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const { 412 if (const PointerType *PT = getAs<PointerType>()) 413 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) 414 return dyn_cast<CXXRecordDecl>(RT->getDecl()); 415 return 0; 416} 417 418CXXRecordDecl *Type::getAsCXXRecordDecl() const { 419 if (const RecordType *RT = getAs<RecordType>()) 420 return dyn_cast<CXXRecordDecl>(RT->getDecl()); 421 else if (const InjectedClassNameType *Injected 422 = getAs<InjectedClassNameType>()) 423 return Injected->getDecl(); 424 425 return 0; 426} 427 428bool Type::isIntegerType() const { 429 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 430 return BT->getKind() >= BuiltinType::Bool && 431 BT->getKind() <= BuiltinType::Int128; 432 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 433 // Incomplete enum types are not treated as integer types. 434 // FIXME: In C++, enum types are never integer types. 435 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 436 return true; 437 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 438 return VT->getElementType()->isIntegerType(); 439 return false; 440} 441 442bool Type::isIntegralType() const { 443 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 444 return BT->getKind() >= BuiltinType::Bool && 445 BT->getKind() <= BuiltinType::Int128; 446 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 447 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 448 return true; // Complete enum types are integral. 449 // FIXME: In C++, enum types are never integral. 450 return false; 451} 452 453bool Type::isEnumeralType() const { 454 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 455 return TT->getDecl()->isEnum(); 456 return false; 457} 458 459bool Type::isBooleanType() const { 460 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 461 return BT->getKind() == BuiltinType::Bool; 462 return false; 463} 464 465bool Type::isCharType() const { 466 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 467 return BT->getKind() == BuiltinType::Char_U || 468 BT->getKind() == BuiltinType::UChar || 469 BT->getKind() == BuiltinType::Char_S || 470 BT->getKind() == BuiltinType::SChar; 471 return false; 472} 473 474bool Type::isWideCharType() const { 475 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 476 return BT->getKind() == BuiltinType::WChar; 477 return false; 478} 479 480/// \brief Determine whether this type is any of the built-in character 481/// types. 482bool Type::isAnyCharacterType() const { 483 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 484 return (BT->getKind() >= BuiltinType::Char_U && 485 BT->getKind() <= BuiltinType::Char32) || 486 (BT->getKind() >= BuiltinType::Char_S && 487 BT->getKind() <= BuiltinType::WChar); 488 489 return false; 490} 491 492/// isSignedIntegerType - Return true if this is an integer type that is 493/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 494/// an enum decl which has a signed representation, or a vector of signed 495/// integer element type. 496bool Type::isSignedIntegerType() const { 497 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 498 return BT->getKind() >= BuiltinType::Char_S && 499 BT->getKind() <= BuiltinType::Int128; 500 } 501 502 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 503 return ET->getDecl()->getIntegerType()->isSignedIntegerType(); 504 505 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 506 return VT->getElementType()->isSignedIntegerType(); 507 return false; 508} 509 510/// isUnsignedIntegerType - Return true if this is an integer type that is 511/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 512/// decl which has an unsigned representation, or a vector of unsigned integer 513/// element type. 514bool Type::isUnsignedIntegerType() const { 515 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 516 return BT->getKind() >= BuiltinType::Bool && 517 BT->getKind() <= BuiltinType::UInt128; 518 } 519 520 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 521 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); 522 523 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 524 return VT->getElementType()->isUnsignedIntegerType(); 525 return false; 526} 527 528bool Type::isFloatingType() const { 529 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 530 return BT->getKind() >= BuiltinType::Float && 531 BT->getKind() <= BuiltinType::LongDouble; 532 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 533 return CT->getElementType()->isFloatingType(); 534 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 535 return VT->getElementType()->isFloatingType(); 536 return false; 537} 538 539bool Type::isRealFloatingType() const { 540 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 541 return BT->isFloatingPoint(); 542 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 543 return VT->getElementType()->isRealFloatingType(); 544 return false; 545} 546 547bool Type::isRealType() const { 548 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 549 return BT->getKind() >= BuiltinType::Bool && 550 BT->getKind() <= BuiltinType::LongDouble; 551 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 552 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); 553 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 554 return VT->getElementType()->isRealType(); 555 return false; 556} 557 558bool Type::isArithmeticType() const { 559 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 560 return BT->getKind() >= BuiltinType::Bool && 561 BT->getKind() <= BuiltinType::LongDouble; 562 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 563 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). 564 // If a body isn't seen by the time we get here, return false. 565 return ET->getDecl()->isDefinition(); 566 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); 567} 568 569bool Type::isScalarType() const { 570 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 571 return BT->getKind() != BuiltinType::Void; 572 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { 573 // Enums are scalar types, but only if they are defined. Incomplete enums 574 // are not treated as scalar types. 575 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 576 return true; 577 return false; 578 } 579 return isa<PointerType>(CanonicalType) || 580 isa<BlockPointerType>(CanonicalType) || 581 isa<MemberPointerType>(CanonicalType) || 582 isa<ComplexType>(CanonicalType) || 583 isa<ObjCObjectPointerType>(CanonicalType); 584} 585 586/// \brief Determines whether the type is a C++ aggregate type or C 587/// aggregate or union type. 588/// 589/// An aggregate type is an array or a class type (struct, union, or 590/// class) that has no user-declared constructors, no private or 591/// protected non-static data members, no base classes, and no virtual 592/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type 593/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also 594/// includes union types. 595bool Type::isAggregateType() const { 596 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { 597 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) 598 return ClassDecl->isAggregate(); 599 600 return true; 601 } 602 603 return isa<ArrayType>(CanonicalType); 604} 605 606/// isConstantSizeType - Return true if this is not a variable sized type, 607/// according to the rules of C99 6.7.5p3. It is not legal to call this on 608/// incomplete types or dependent types. 609bool Type::isConstantSizeType() const { 610 assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); 611 assert(!isDependentType() && "This doesn't make sense for dependent types"); 612 // The VAT must have a size, as it is known to be complete. 613 return !isa<VariableArrayType>(CanonicalType); 614} 615 616/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) 617/// - a type that can describe objects, but which lacks information needed to 618/// determine its size. 619bool Type::isIncompleteType() const { 620 switch (CanonicalType->getTypeClass()) { 621 default: return false; 622 case Builtin: 623 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never 624 // be completed. 625 return isVoidType(); 626 case Record: 627 case Enum: 628 // A tagged type (struct/union/enum/class) is incomplete if the decl is a 629 // forward declaration, but not a full definition (C99 6.2.5p22). 630 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); 631 case ConstantArray: 632 // An array is incomplete if its element type is incomplete 633 // (C++ [dcl.array]p1). 634 // We don't handle variable arrays (they're not allowed in C++) or 635 // dependent-sized arrays (dependent types are never treated as incomplete). 636 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType(); 637 case IncompleteArray: 638 // An array of unknown size is an incomplete type (C99 6.2.5p22). 639 return true; 640 case ObjCObject: 641 return cast<ObjCObjectType>(this)->getBaseType()->isIncompleteType(); 642 case ObjCInterface: 643 // ObjC interfaces are incomplete if they are @class, not @interface. 644 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl(); 645 } 646} 647 648/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) 649bool Type::isPODType() const { 650 // The compiler shouldn't query this for incomplete types, but the user might. 651 // We return false for that case. 652 if (isIncompleteType()) 653 return false; 654 655 switch (CanonicalType->getTypeClass()) { 656 // Everything not explicitly mentioned is not POD. 657 default: return false; 658 case VariableArray: 659 case ConstantArray: 660 // IncompleteArray is caught by isIncompleteType() above. 661 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType(); 662 663 case Builtin: 664 case Complex: 665 case Pointer: 666 case MemberPointer: 667 case Vector: 668 case ExtVector: 669 case ObjCObjectPointer: 670 case BlockPointer: 671 return true; 672 673 case Enum: 674 return true; 675 676 case Record: 677 if (CXXRecordDecl *ClassDecl 678 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) 679 return ClassDecl->isPOD(); 680 681 // C struct/union is POD. 682 return true; 683 } 684} 685 686bool Type::isLiteralType() const { 687 if (isIncompleteType()) 688 return false; 689 690 // C++0x [basic.types]p10: 691 // A type is a literal type if it is: 692 switch (CanonicalType->getTypeClass()) { 693 // We're whitelisting 694 default: return false; 695 696 // -- a scalar type 697 case Builtin: 698 case Complex: 699 case Pointer: 700 case MemberPointer: 701 case Vector: 702 case ExtVector: 703 case ObjCObjectPointer: 704 case Enum: 705 return true; 706 707 // -- a class type with ... 708 case Record: 709 // FIXME: Do the tests 710 return false; 711 712 // -- an array of literal type 713 // Extension: variable arrays cannot be literal types, since they're 714 // runtime-sized. 715 case ConstantArray: 716 return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType(); 717 } 718} 719 720bool Type::isPromotableIntegerType() const { 721 if (const BuiltinType *BT = getAs<BuiltinType>()) 722 switch (BT->getKind()) { 723 case BuiltinType::Bool: 724 case BuiltinType::Char_S: 725 case BuiltinType::Char_U: 726 case BuiltinType::SChar: 727 case BuiltinType::UChar: 728 case BuiltinType::Short: 729 case BuiltinType::UShort: 730 return true; 731 default: 732 return false; 733 } 734 735 // Enumerated types are promotable to their compatible integer types 736 // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2). 737 if (const EnumType *ET = getAs<EnumType>()){ 738 if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()) 739 return false; 740 741 const BuiltinType *BT 742 = ET->getDecl()->getPromotionType()->getAs<BuiltinType>(); 743 return BT->getKind() == BuiltinType::Int 744 || BT->getKind() == BuiltinType::UInt; 745 } 746 747 return false; 748} 749 750bool Type::isNullPtrType() const { 751 if (const BuiltinType *BT = getAs<BuiltinType>()) 752 return BT->getKind() == BuiltinType::NullPtr; 753 return false; 754} 755 756bool Type::isSpecifierType() const { 757 // Note that this intentionally does not use the canonical type. 758 switch (getTypeClass()) { 759 case Builtin: 760 case Record: 761 case Enum: 762 case Typedef: 763 case Complex: 764 case TypeOfExpr: 765 case TypeOf: 766 case TemplateTypeParm: 767 case SubstTemplateTypeParm: 768 case TemplateSpecialization: 769 case Elaborated: 770 case DependentName: 771 case ObjCInterface: 772 case ObjCObject: 773 case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers 774 return true; 775 default: 776 return false; 777 } 778} 779 780TypeWithKeyword::~TypeWithKeyword() { 781} 782 783ElaboratedTypeKeyword 784TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) { 785 switch (TypeSpec) { 786 default: return ETK_None; 787 case TST_typename: return ETK_Typename; 788 case TST_class: return ETK_Class; 789 case TST_struct: return ETK_Struct; 790 case TST_union: return ETK_Union; 791 case TST_enum: return ETK_Enum; 792 } 793} 794 795TagTypeKind 796TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) { 797 switch(TypeSpec) { 798 case TST_class: return TTK_Class; 799 case TST_struct: return TTK_Struct; 800 case TST_union: return TTK_Union; 801 case TST_enum: return TTK_Enum; 802 default: llvm_unreachable("Type specifier is not a tag type kind."); 803 } 804} 805 806ElaboratedTypeKeyword 807TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) { 808 switch (Kind) { 809 case TTK_Class: return ETK_Class; 810 case TTK_Struct: return ETK_Struct; 811 case TTK_Union: return ETK_Union; 812 case TTK_Enum: return ETK_Enum; 813 } 814 llvm_unreachable("Unknown tag type kind."); 815} 816 817TagTypeKind 818TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) { 819 switch (Keyword) { 820 case ETK_Class: return TTK_Class; 821 case ETK_Struct: return TTK_Struct; 822 case ETK_Union: return TTK_Union; 823 case ETK_Enum: return TTK_Enum; 824 case ETK_None: // Fall through. 825 case ETK_Typename: 826 llvm_unreachable("Elaborated type keyword is not a tag type kind."); 827 } 828 llvm_unreachable("Unknown elaborated type keyword."); 829} 830 831bool 832TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) { 833 switch (Keyword) { 834 case ETK_None: 835 case ETK_Typename: 836 return false; 837 case ETK_Class: 838 case ETK_Struct: 839 case ETK_Union: 840 case ETK_Enum: 841 return true; 842 } 843 llvm_unreachable("Unknown elaborated type keyword."); 844} 845 846const char* 847TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) { 848 switch (Keyword) { 849 default: llvm_unreachable("Unknown elaborated type keyword."); 850 case ETK_None: return ""; 851 case ETK_Typename: return "typename"; 852 case ETK_Class: return "class"; 853 case ETK_Struct: return "struct"; 854 case ETK_Union: return "union"; 855 case ETK_Enum: return "enum"; 856 } 857} 858 859bool Type::isElaboratedTypeSpecifier() const { 860 ElaboratedTypeKeyword Keyword; 861 if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this)) 862 Keyword = Elab->getKeyword(); 863 else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this)) 864 Keyword = DepName->getKeyword(); 865 else 866 return false; 867 868 return TypeWithKeyword::KeywordIsTagTypeKind(Keyword); 869} 870 871const char *Type::getTypeClassName() const { 872 switch (TC) { 873 default: assert(0 && "Type class not in TypeNodes.def!"); 874#define ABSTRACT_TYPE(Derived, Base) 875#define TYPE(Derived, Base) case Derived: return #Derived; 876#include "clang/AST/TypeNodes.def" 877 } 878} 879 880const char *BuiltinType::getName(const LangOptions &LO) const { 881 switch (getKind()) { 882 default: assert(0 && "Unknown builtin type!"); 883 case Void: return "void"; 884 case Bool: return LO.Bool ? "bool" : "_Bool"; 885 case Char_S: return "char"; 886 case Char_U: return "char"; 887 case SChar: return "signed char"; 888 case Short: return "short"; 889 case Int: return "int"; 890 case Long: return "long"; 891 case LongLong: return "long long"; 892 case Int128: return "__int128_t"; 893 case UChar: return "unsigned char"; 894 case UShort: return "unsigned short"; 895 case UInt: return "unsigned int"; 896 case ULong: return "unsigned long"; 897 case ULongLong: return "unsigned long long"; 898 case UInt128: return "__uint128_t"; 899 case Float: return "float"; 900 case Double: return "double"; 901 case LongDouble: return "long double"; 902 case WChar: return "wchar_t"; 903 case Char16: return "char16_t"; 904 case Char32: return "char32_t"; 905 case NullPtr: return "nullptr_t"; 906 case Overload: return "<overloaded function type>"; 907 case Dependent: return "<dependent type>"; 908 case UndeducedAuto: return "auto"; 909 case ObjCId: return "id"; 910 case ObjCClass: return "Class"; 911 case ObjCSel: return "SEL"; 912 } 913} 914 915void FunctionType::ANCHOR() {} // Key function for FunctionType. 916 917llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) { 918 switch (CC) { 919 case CC_Default: llvm_unreachable("no name for default cc"); 920 default: return ""; 921 922 case CC_C: return "cdecl"; 923 case CC_X86StdCall: return "stdcall"; 924 case CC_X86FastCall: return "fastcall"; 925 case CC_X86ThisCall: return "thiscall"; 926 } 927} 928 929void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 930 arg_type_iterator ArgTys, 931 unsigned NumArgs, bool isVariadic, 932 unsigned TypeQuals, bool hasExceptionSpec, 933 bool anyExceptionSpec, unsigned NumExceptions, 934 exception_iterator Exs, 935 const FunctionType::ExtInfo &Info) { 936 ID.AddPointer(Result.getAsOpaquePtr()); 937 for (unsigned i = 0; i != NumArgs; ++i) 938 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 939 ID.AddInteger(isVariadic); 940 ID.AddInteger(TypeQuals); 941 ID.AddInteger(hasExceptionSpec); 942 if (hasExceptionSpec) { 943 ID.AddInteger(anyExceptionSpec); 944 for (unsigned i = 0; i != NumExceptions; ++i) 945 ID.AddPointer(Exs[i].getAsOpaquePtr()); 946 } 947 ID.AddInteger(Info.getNoReturn()); 948 ID.AddInteger(Info.getRegParm()); 949 ID.AddInteger(Info.getCC()); 950} 951 952void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { 953 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), 954 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), 955 getNumExceptions(), exception_begin(), 956 getExtInfo()); 957} 958 959/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 960/// potentially looking through *all* consequtive typedefs. This returns the 961/// sum of the type qualifiers, so if you have: 962/// typedef const int A; 963/// typedef volatile A B; 964/// looking through the typedefs for B will give you "const volatile A". 965/// 966QualType TypedefType::LookThroughTypedefs() const { 967 // Usually, there is only a single level of typedefs, be fast in that case. 968 QualType FirstType = getDecl()->getUnderlyingType(); 969 if (!isa<TypedefType>(FirstType)) 970 return FirstType; 971 972 // Otherwise, do the fully general loop. 973 QualifierCollector Qs; 974 975 QualType CurType; 976 const TypedefType *TDT = this; 977 do { 978 CurType = TDT->getDecl()->getUnderlyingType(); 979 TDT = dyn_cast<TypedefType>(Qs.strip(CurType)); 980 } while (TDT); 981 982 return Qs.apply(CurType); 983} 984 985QualType TypedefType::desugar() const { 986 return getDecl()->getUnderlyingType(); 987} 988 989TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 990 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { 991} 992 993QualType TypeOfExprType::desugar() const { 994 return getUnderlyingExpr()->getType(); 995} 996 997void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, 998 ASTContext &Context, Expr *E) { 999 E->Profile(ID, Context, true); 1000} 1001 1002DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) 1003 : Type(Decltype, can, E->isTypeDependent()), E(E), 1004 UnderlyingType(underlyingType) { 1005} 1006 1007DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) 1008 : DecltypeType(E, Context.DependentTy), Context(Context) { } 1009 1010void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, 1011 ASTContext &Context, Expr *E) { 1012 E->Profile(ID, Context, true); 1013} 1014 1015TagType::TagType(TypeClass TC, const TagDecl *D, QualType can) 1016 : Type(TC, can, D->isDependentType()), 1017 decl(const_cast<TagDecl*>(D), 0) {} 1018 1019bool RecordType::classof(const TagType *TT) { 1020 return isa<RecordDecl>(TT->getDecl()); 1021} 1022 1023bool EnumType::classof(const TagType *TT) { 1024 return isa<EnumDecl>(TT->getDecl()); 1025} 1026 1027static bool isDependent(const TemplateArgument &Arg) { 1028 switch (Arg.getKind()) { 1029 case TemplateArgument::Null: 1030 assert(false && "Should not have a NULL template argument"); 1031 return false; 1032 1033 case TemplateArgument::Type: 1034 return Arg.getAsType()->isDependentType(); 1035 1036 case TemplateArgument::Template: 1037 return Arg.getAsTemplate().isDependent(); 1038 1039 case TemplateArgument::Declaration: 1040 if (DeclContext *DC = dyn_cast<DeclContext>(Arg.getAsDecl())) 1041 return DC->isDependentContext(); 1042 return Arg.getAsDecl()->getDeclContext()->isDependentContext(); 1043 1044 case TemplateArgument::Integral: 1045 // Never dependent 1046 return false; 1047 1048 case TemplateArgument::Expression: 1049 return (Arg.getAsExpr()->isTypeDependent() || 1050 Arg.getAsExpr()->isValueDependent()); 1051 1052 case TemplateArgument::Pack: 1053 for (TemplateArgument::pack_iterator P = Arg.pack_begin(), 1054 PEnd = Arg.pack_end(); 1055 P != PEnd; ++P) { 1056 if (isDependent(*P)) 1057 return true; 1058 } 1059 1060 return false; 1061 } 1062 1063 return false; 1064} 1065 1066bool TemplateSpecializationType:: 1067anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) { 1068 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size()); 1069} 1070 1071bool TemplateSpecializationType:: 1072anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) { 1073 for (unsigned i = 0; i != N; ++i) 1074 if (isDependent(Args[i].getArgument())) 1075 return true; 1076 return false; 1077} 1078 1079bool TemplateSpecializationType:: 1080anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) { 1081 for (unsigned i = 0; i != N; ++i) 1082 if (isDependent(Args[i])) 1083 return true; 1084 return false; 1085} 1086 1087TemplateSpecializationType:: 1088TemplateSpecializationType(ASTContext &Context, TemplateName T, 1089 bool IsCurrentInstantiation, 1090 const TemplateArgument *Args, 1091 unsigned NumArgs, QualType Canon) 1092 : Type(TemplateSpecialization, 1093 Canon.isNull()? QualType(this, 0) : Canon, 1094 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), 1095 ContextAndCurrentInstantiation(&Context, IsCurrentInstantiation), 1096 Template(T), NumArgs(NumArgs) { 1097 assert((!Canon.isNull() || 1098 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && 1099 "No canonical type for non-dependent class template specialization"); 1100 1101 TemplateArgument *TemplateArgs 1102 = reinterpret_cast<TemplateArgument *>(this + 1); 1103 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) 1104 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); 1105} 1106 1107void TemplateSpecializationType::Destroy(ASTContext& C) { 1108 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1109 // FIXME: Not all expressions get cloned, so we can't yet perform 1110 // this destruction. 1111 // if (Expr *E = getArg(Arg).getAsExpr()) 1112 // E->Destroy(C); 1113 } 1114} 1115 1116TemplateSpecializationType::iterator 1117TemplateSpecializationType::end() const { 1118 return begin() + getNumArgs(); 1119} 1120 1121const TemplateArgument & 1122TemplateSpecializationType::getArg(unsigned Idx) const { 1123 assert(Idx < getNumArgs() && "Template argument out of range"); 1124 return getArgs()[Idx]; 1125} 1126 1127void 1128TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 1129 TemplateName T, 1130 bool IsCurrentInstantiation, 1131 const TemplateArgument *Args, 1132 unsigned NumArgs, 1133 ASTContext &Context) { 1134 ID.AddBoolean(IsCurrentInstantiation); 1135 T.Profile(ID); 1136 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 1137 Args[Idx].Profile(ID, Context); 1138} 1139 1140QualType QualifierCollector::apply(QualType QT) const { 1141 if (!hasNonFastQualifiers()) 1142 return QT.withFastQualifiers(getFastQualifiers()); 1143 1144 assert(Context && "extended qualifiers but no context!"); 1145 return Context->getQualifiedType(QT, *this); 1146} 1147 1148QualType QualifierCollector::apply(const Type *T) const { 1149 if (!hasNonFastQualifiers()) 1150 return QualType(T, getFastQualifiers()); 1151 1152 assert(Context && "extended qualifiers but no context!"); 1153 return Context->getQualifiedType(T, *this); 1154} 1155 1156void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID, 1157 QualType BaseType, 1158 ObjCProtocolDecl * const *Protocols, 1159 unsigned NumProtocols) { 1160 ID.AddPointer(BaseType.getAsOpaquePtr()); 1161 for (unsigned i = 0; i != NumProtocols; i++) 1162 ID.AddPointer(Protocols[i]); 1163} 1164 1165void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) { 1166 Profile(ID, getBaseType(), qual_begin(), getNumProtocols()); 1167} 1168 1169/// \brief Determine the linkage of this type. 1170Linkage Type::getLinkage() const { 1171 if (this != CanonicalType.getTypePtr()) 1172 return CanonicalType->getLinkage(); 1173 1174 if (!LinkageKnown) { 1175 CachedLinkage = getLinkageImpl(); 1176 LinkageKnown = true; 1177 } 1178 1179 return static_cast<clang::Linkage>(CachedLinkage); 1180} 1181 1182Linkage Type::getLinkageImpl() const { 1183 // C++ [basic.link]p8: 1184 // Names not covered by these rules have no linkage. 1185 return NoLinkage; 1186} 1187 1188void Type::ClearLinkageCache() { 1189 if (this != CanonicalType.getTypePtr()) 1190 CanonicalType->ClearLinkageCache(); 1191 else 1192 LinkageKnown = false; 1193} 1194 1195Linkage BuiltinType::getLinkageImpl() const { 1196 // C++ [basic.link]p8: 1197 // A type is said to have linkage if and only if: 1198 // - it is a fundamental type (3.9.1); or 1199 return ExternalLinkage; 1200} 1201 1202Linkage TagType::getLinkageImpl() const { 1203 // C++ [basic.link]p8: 1204 // - it is a class or enumeration type that is named (or has a name for 1205 // linkage purposes (7.1.3)) and the name has linkage; or 1206 // - it is a specialization of a class template (14); or 1207 return getDecl()->getLinkage(); 1208} 1209 1210// C++ [basic.link]p8: 1211// - it is a compound type (3.9.2) other than a class or enumeration, 1212// compounded exclusively from types that have linkage; or 1213Linkage ComplexType::getLinkageImpl() const { 1214 return ElementType->getLinkage(); 1215} 1216 1217Linkage PointerType::getLinkageImpl() const { 1218 return PointeeType->getLinkage(); 1219} 1220 1221Linkage BlockPointerType::getLinkageImpl() const { 1222 return PointeeType->getLinkage(); 1223} 1224 1225Linkage ReferenceType::getLinkageImpl() const { 1226 return PointeeType->getLinkage(); 1227} 1228 1229Linkage MemberPointerType::getLinkageImpl() const { 1230 return minLinkage(Class->getLinkage(), PointeeType->getLinkage()); 1231} 1232 1233Linkage ArrayType::getLinkageImpl() const { 1234 return ElementType->getLinkage(); 1235} 1236 1237Linkage VectorType::getLinkageImpl() const { 1238 return ElementType->getLinkage(); 1239} 1240 1241Linkage FunctionNoProtoType::getLinkageImpl() const { 1242 return getResultType()->getLinkage(); 1243} 1244 1245Linkage FunctionProtoType::getLinkageImpl() const { 1246 Linkage L = getResultType()->getLinkage(); 1247 for (arg_type_iterator A = arg_type_begin(), AEnd = arg_type_end(); 1248 A != AEnd; ++A) 1249 L = minLinkage(L, (*A)->getLinkage()); 1250 1251 return L; 1252} 1253 1254Linkage ObjCObjectType::getLinkageImpl() const { 1255 return ExternalLinkage; 1256} 1257 1258Linkage ObjCObjectPointerType::getLinkageImpl() const { 1259 return ExternalLinkage; 1260} 1261