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