Type.cpp revision 201361
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 "llvm/ADT/StringExtras.h" 22#include "llvm/Support/raw_ostream.h" 23using namespace clang; 24 25bool QualType::isConstant(QualType T, ASTContext &Ctx) { 26 if (T.isConstQualified()) 27 return true; 28 29 if (const ArrayType *AT = Ctx.getAsArrayType(T)) 30 return AT->getElementType().isConstant(Ctx); 31 32 return false; 33} 34 35void Type::Destroy(ASTContext& C) { 36 this->~Type(); 37 C.Deallocate(this); 38} 39 40void VariableArrayType::Destroy(ASTContext& C) { 41 if (SizeExpr) 42 SizeExpr->Destroy(C); 43 this->~VariableArrayType(); 44 C.Deallocate(this); 45} 46 47void DependentSizedArrayType::Destroy(ASTContext& C) { 48 // FIXME: Resource contention like in ConstantArrayWithExprType ? 49 // May crash, depending on platform or a particular build. 50 // SizeExpr->Destroy(C); 51 this->~DependentSizedArrayType(); 52 C.Deallocate(this); 53} 54 55void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID, 56 ASTContext &Context, 57 QualType ET, 58 ArraySizeModifier SizeMod, 59 unsigned TypeQuals, 60 Expr *E) { 61 ID.AddPointer(ET.getAsOpaquePtr()); 62 ID.AddInteger(SizeMod); 63 ID.AddInteger(TypeQuals); 64 E->Profile(ID, Context, true); 65} 66 67void 68DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID, 69 ASTContext &Context, 70 QualType ElementType, Expr *SizeExpr) { 71 ID.AddPointer(ElementType.getAsOpaquePtr()); 72 SizeExpr->Profile(ID, Context, true); 73} 74 75void DependentSizedExtVectorType::Destroy(ASTContext& C) { 76 // FIXME: Deallocate size expression, once we're cloning properly. 77// if (SizeExpr) 78// SizeExpr->Destroy(C); 79 this->~DependentSizedExtVectorType(); 80 C.Deallocate(this); 81} 82 83/// getArrayElementTypeNoTypeQual - If this is an array type, return the 84/// element type of the array, potentially with type qualifiers missing. 85/// This method should never be used when type qualifiers are meaningful. 86const Type *Type::getArrayElementTypeNoTypeQual() const { 87 // If this is directly an array type, return it. 88 if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) 89 return ATy->getElementType().getTypePtr(); 90 91 // If the canonical form of this type isn't the right kind, reject it. 92 if (!isa<ArrayType>(CanonicalType)) 93 return 0; 94 95 // If this is a typedef for an array type, strip the typedef off without 96 // losing all typedef information. 97 return cast<ArrayType>(getUnqualifiedDesugaredType()) 98 ->getElementType().getTypePtr(); 99} 100 101/// \brief Retrieve the unqualified variant of the given type, removing as 102/// little sugar as possible. 103/// 104/// This routine looks through various kinds of sugar to find the 105/// least-desuraged type that is unqualified. For example, given: 106/// 107/// \code 108/// typedef int Integer; 109/// typedef const Integer CInteger; 110/// typedef CInteger DifferenceType; 111/// \endcode 112/// 113/// Executing \c getUnqualifiedTypeSlow() on the type \c DifferenceType will 114/// desugar until we hit the type \c Integer, which has no qualifiers on it. 115QualType QualType::getUnqualifiedTypeSlow() const { 116 QualType Cur = *this; 117 while (true) { 118 if (!Cur.hasQualifiers()) 119 return Cur; 120 121 const Type *CurTy = Cur.getTypePtr(); 122 switch (CurTy->getTypeClass()) { 123#define ABSTRACT_TYPE(Class, Parent) 124#define TYPE(Class, Parent) \ 125 case Type::Class: { \ 126 const Class##Type *Ty = cast<Class##Type>(CurTy); \ 127 if (!Ty->isSugared()) \ 128 return Cur.getLocalUnqualifiedType(); \ 129 Cur = Ty->desugar(); \ 130 break; \ 131 } 132#include "clang/AST/TypeNodes.def" 133 } 134 } 135 136 return Cur.getUnqualifiedType(); 137} 138 139/// getDesugaredType - Return the specified type with any "sugar" removed from 140/// the type. This takes off typedefs, typeof's etc. If the outer level of 141/// the type is already concrete, it returns it unmodified. This is similar 142/// to getting the canonical type, but it doesn't remove *all* typedefs. For 143/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 144/// concrete. 145QualType QualType::getDesugaredType(QualType T) { 146 QualifierCollector Qs; 147 148 QualType Cur = T; 149 while (true) { 150 const Type *CurTy = Qs.strip(Cur); 151 switch (CurTy->getTypeClass()) { 152#define ABSTRACT_TYPE(Class, Parent) 153#define TYPE(Class, Parent) \ 154 case Type::Class: { \ 155 const Class##Type *Ty = cast<Class##Type>(CurTy); \ 156 if (!Ty->isSugared()) \ 157 return Qs.apply(Cur); \ 158 Cur = Ty->desugar(); \ 159 break; \ 160 } 161#include "clang/AST/TypeNodes.def" 162 } 163 } 164} 165 166/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic 167/// sugar off the given type. This should produce an object of the 168/// same dynamic type as the canonical type. 169const Type *Type::getUnqualifiedDesugaredType() const { 170 const Type *Cur = this; 171 172 while (true) { 173 switch (Cur->getTypeClass()) { 174#define ABSTRACT_TYPE(Class, Parent) 175#define TYPE(Class, Parent) \ 176 case Class: { \ 177 const Class##Type *Ty = cast<Class##Type>(Cur); \ 178 if (!Ty->isSugared()) return Cur; \ 179 Cur = Ty->desugar().getTypePtr(); \ 180 break; \ 181 } 182#include "clang/AST/TypeNodes.def" 183 } 184 } 185} 186 187/// isVoidType - Helper method to determine if this is the 'void' type. 188bool Type::isVoidType() const { 189 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 190 return BT->getKind() == BuiltinType::Void; 191 return false; 192} 193 194bool Type::isObjectType() const { 195 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) || 196 isa<IncompleteArrayType>(CanonicalType) || isVoidType()) 197 return false; 198 return true; 199} 200 201bool Type::isDerivedType() const { 202 switch (CanonicalType->getTypeClass()) { 203 case Pointer: 204 case VariableArray: 205 case ConstantArray: 206 case IncompleteArray: 207 case FunctionProto: 208 case FunctionNoProto: 209 case LValueReference: 210 case RValueReference: 211 case Record: 212 return true; 213 default: 214 return false; 215 } 216} 217 218bool Type::isClassType() const { 219 if (const RecordType *RT = getAs<RecordType>()) 220 return RT->getDecl()->isClass(); 221 return false; 222} 223bool Type::isStructureType() const { 224 if (const RecordType *RT = getAs<RecordType>()) 225 return RT->getDecl()->isStruct(); 226 return false; 227} 228bool Type::isVoidPointerType() const { 229 if (const PointerType *PT = getAs<PointerType>()) 230 return PT->getPointeeType()->isVoidType(); 231 return false; 232} 233 234bool Type::isUnionType() const { 235 if (const RecordType *RT = getAs<RecordType>()) 236 return RT->getDecl()->isUnion(); 237 return false; 238} 239 240bool Type::isComplexType() const { 241 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 242 return CT->getElementType()->isFloatingType(); 243 return false; 244} 245 246bool Type::isComplexIntegerType() const { 247 // Check for GCC complex integer extension. 248 return getAsComplexIntegerType(); 249} 250 251const ComplexType *Type::getAsComplexIntegerType() const { 252 if (const ComplexType *Complex = getAs<ComplexType>()) 253 if (Complex->getElementType()->isIntegerType()) 254 return Complex; 255 return 0; 256} 257 258QualType Type::getPointeeType() const { 259 if (const PointerType *PT = getAs<PointerType>()) 260 return PT->getPointeeType(); 261 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 262 return OPT->getPointeeType(); 263 if (const BlockPointerType *BPT = getAs<BlockPointerType>()) 264 return BPT->getPointeeType(); 265 if (const ReferenceType *RT = getAs<ReferenceType>()) 266 return RT->getPointeeType(); 267 return QualType(); 268} 269 270/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length 271/// array types and types that contain variable array types in their 272/// declarator 273bool Type::isVariablyModifiedType() const { 274 // A VLA is a variably modified type. 275 if (isVariableArrayType()) 276 return true; 277 278 // An array can contain a variably modified type 279 if (const Type *T = getArrayElementTypeNoTypeQual()) 280 return T->isVariablyModifiedType(); 281 282 // A pointer can point to a variably modified type. 283 // Also, C++ references and member pointers can point to a variably modified 284 // type, where VLAs appear as an extension to C++, and should be treated 285 // correctly. 286 if (const PointerType *PT = getAs<PointerType>()) 287 return PT->getPointeeType()->isVariablyModifiedType(); 288 if (const ReferenceType *RT = getAs<ReferenceType>()) 289 return RT->getPointeeType()->isVariablyModifiedType(); 290 if (const MemberPointerType *PT = getAs<MemberPointerType>()) 291 return PT->getPointeeType()->isVariablyModifiedType(); 292 293 // A function can return a variably modified type 294 // This one isn't completely obvious, but it follows from the 295 // definition in C99 6.7.5p3. Because of this rule, it's 296 // illegal to declare a function returning a variably modified type. 297 if (const FunctionType *FT = getAs<FunctionType>()) 298 return FT->getResultType()->isVariablyModifiedType(); 299 300 return false; 301} 302 303const RecordType *Type::getAsStructureType() const { 304 // If this is directly a structure type, return it. 305 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 306 if (RT->getDecl()->isStruct()) 307 return RT; 308 } 309 310 // If the canonical form of this type isn't the right kind, reject it. 311 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 312 if (!RT->getDecl()->isStruct()) 313 return 0; 314 315 // If this is a typedef for a structure type, strip the typedef off without 316 // losing all typedef information. 317 return cast<RecordType>(getUnqualifiedDesugaredType()); 318 } 319 return 0; 320} 321 322const RecordType *Type::getAsUnionType() const { 323 // If this is directly a union type, return it. 324 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 325 if (RT->getDecl()->isUnion()) 326 return RT; 327 } 328 329 // If the canonical form of this type isn't the right kind, reject it. 330 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 331 if (!RT->getDecl()->isUnion()) 332 return 0; 333 334 // If this is a typedef for a union type, strip the typedef off without 335 // losing all typedef information. 336 return cast<RecordType>(getUnqualifiedDesugaredType()); 337 } 338 339 return 0; 340} 341 342const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const { 343 // There is no sugar for ObjCInterfaceType's, just return the canonical 344 // type pointer if it is the right class. There is no typedef information to 345 // return and these cannot be Address-space qualified. 346 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>()) 347 if (OIT->getNumProtocols()) 348 return OIT; 349 return 0; 350} 351 352bool Type::isObjCQualifiedInterfaceType() const { 353 return getAsObjCQualifiedInterfaceType() != 0; 354} 355 356const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { 357 // There is no sugar for ObjCQualifiedIdType's, just return the canonical 358 // type pointer if it is the right class. 359 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 360 if (OPT->isObjCQualifiedIdType()) 361 return OPT; 362 } 363 return 0; 364} 365 366const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const { 367 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 368 if (OPT->getInterfaceType()) 369 return OPT; 370 } 371 return 0; 372} 373 374const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const { 375 if (const PointerType *PT = getAs<PointerType>()) 376 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) 377 return dyn_cast<CXXRecordDecl>(RT->getDecl()); 378 return 0; 379} 380 381bool Type::isIntegerType() const { 382 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 383 return BT->getKind() >= BuiltinType::Bool && 384 BT->getKind() <= BuiltinType::Int128; 385 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 386 // Incomplete enum types are not treated as integer types. 387 // FIXME: In C++, enum types are never integer types. 388 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 389 return true; 390 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 391 return VT->getElementType()->isIntegerType(); 392 return false; 393} 394 395bool Type::isIntegralType() const { 396 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 397 return BT->getKind() >= BuiltinType::Bool && 398 BT->getKind() <= BuiltinType::Int128; 399 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 400 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 401 return true; // Complete enum types are integral. 402 // FIXME: In C++, enum types are never integral. 403 return false; 404} 405 406bool Type::isEnumeralType() const { 407 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 408 return TT->getDecl()->isEnum(); 409 return false; 410} 411 412bool Type::isBooleanType() const { 413 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 414 return BT->getKind() == BuiltinType::Bool; 415 return false; 416} 417 418bool Type::isCharType() const { 419 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 420 return BT->getKind() == BuiltinType::Char_U || 421 BT->getKind() == BuiltinType::UChar || 422 BT->getKind() == BuiltinType::Char_S || 423 BT->getKind() == BuiltinType::SChar; 424 return false; 425} 426 427bool Type::isWideCharType() const { 428 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 429 return BT->getKind() == BuiltinType::WChar; 430 return false; 431} 432 433/// \brief Determine whether this type is any of the built-in character 434/// types. 435bool Type::isAnyCharacterType() const { 436 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 437 return (BT->getKind() >= BuiltinType::Char_U && 438 BT->getKind() <= BuiltinType::Char32) || 439 (BT->getKind() >= BuiltinType::Char_S && 440 BT->getKind() <= BuiltinType::WChar); 441 442 return false; 443} 444 445/// isSignedIntegerType - Return true if this is an integer type that is 446/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 447/// an enum decl which has a signed representation, or a vector of signed 448/// integer element type. 449bool Type::isSignedIntegerType() const { 450 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 451 return BT->getKind() >= BuiltinType::Char_S && 452 BT->getKind() <= BuiltinType::Int128; 453 } 454 455 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 456 return ET->getDecl()->getIntegerType()->isSignedIntegerType(); 457 458 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 459 return VT->getElementType()->isSignedIntegerType(); 460 return false; 461} 462 463/// isUnsignedIntegerType - Return true if this is an integer type that is 464/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 465/// decl which has an unsigned representation, or a vector of unsigned integer 466/// element type. 467bool Type::isUnsignedIntegerType() const { 468 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 469 return BT->getKind() >= BuiltinType::Bool && 470 BT->getKind() <= BuiltinType::UInt128; 471 } 472 473 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 474 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); 475 476 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 477 return VT->getElementType()->isUnsignedIntegerType(); 478 return false; 479} 480 481bool Type::isFloatingType() const { 482 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 483 return BT->getKind() >= BuiltinType::Float && 484 BT->getKind() <= BuiltinType::LongDouble; 485 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 486 return CT->getElementType()->isFloatingType(); 487 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 488 return VT->getElementType()->isFloatingType(); 489 return false; 490} 491 492bool Type::isRealFloatingType() const { 493 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 494 return BT->isFloatingPoint(); 495 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 496 return VT->getElementType()->isRealFloatingType(); 497 return false; 498} 499 500bool Type::isRealType() const { 501 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 502 return BT->getKind() >= BuiltinType::Bool && 503 BT->getKind() <= BuiltinType::LongDouble; 504 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 505 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); 506 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 507 return VT->getElementType()->isRealType(); 508 return false; 509} 510 511bool Type::isArithmeticType() const { 512 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 513 return BT->getKind() >= BuiltinType::Bool && 514 BT->getKind() <= BuiltinType::LongDouble; 515 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 516 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). 517 // If a body isn't seen by the time we get here, return false. 518 return ET->getDecl()->isDefinition(); 519 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); 520} 521 522bool Type::isScalarType() const { 523 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 524 return BT->getKind() != BuiltinType::Void; 525 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { 526 // Enums are scalar types, but only if they are defined. Incomplete enums 527 // are not treated as scalar types. 528 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 529 return true; 530 return false; 531 } 532 return isa<PointerType>(CanonicalType) || 533 isa<BlockPointerType>(CanonicalType) || 534 isa<MemberPointerType>(CanonicalType) || 535 isa<ComplexType>(CanonicalType) || 536 isa<ObjCObjectPointerType>(CanonicalType); 537} 538 539/// \brief Determines whether the type is a C++ aggregate type or C 540/// aggregate or union type. 541/// 542/// An aggregate type is an array or a class type (struct, union, or 543/// class) that has no user-declared constructors, no private or 544/// protected non-static data members, no base classes, and no virtual 545/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type 546/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also 547/// includes union types. 548bool Type::isAggregateType() const { 549 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { 550 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) 551 return ClassDecl->isAggregate(); 552 553 return true; 554 } 555 556 return isa<ArrayType>(CanonicalType); 557} 558 559/// isConstantSizeType - Return true if this is not a variable sized type, 560/// according to the rules of C99 6.7.5p3. It is not legal to call this on 561/// incomplete types or dependent types. 562bool Type::isConstantSizeType() const { 563 assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); 564 assert(!isDependentType() && "This doesn't make sense for dependent types"); 565 // The VAT must have a size, as it is known to be complete. 566 return !isa<VariableArrayType>(CanonicalType); 567} 568 569/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) 570/// - a type that can describe objects, but which lacks information needed to 571/// determine its size. 572bool Type::isIncompleteType() const { 573 switch (CanonicalType->getTypeClass()) { 574 default: return false; 575 case Builtin: 576 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never 577 // be completed. 578 return isVoidType(); 579 case Record: 580 case Enum: 581 // A tagged type (struct/union/enum/class) is incomplete if the decl is a 582 // forward declaration, but not a full definition (C99 6.2.5p22). 583 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); 584 case ConstantArray: 585 // An array is incomplete if its element type is incomplete 586 // (C++ [dcl.array]p1). 587 // We don't handle variable arrays (they're not allowed in C++) or 588 // dependent-sized arrays (dependent types are never treated as incomplete). 589 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType(); 590 case IncompleteArray: 591 // An array of unknown size is an incomplete type (C99 6.2.5p22). 592 return true; 593 case ObjCInterface: 594 // ObjC interfaces are incomplete if they are @class, not @interface. 595 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl(); 596 } 597} 598 599/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) 600bool Type::isPODType() const { 601 // The compiler shouldn't query this for incomplete types, but the user might. 602 // We return false for that case. 603 if (isIncompleteType()) 604 return false; 605 606 switch (CanonicalType->getTypeClass()) { 607 // Everything not explicitly mentioned is not POD. 608 default: return false; 609 case VariableArray: 610 case ConstantArray: 611 // IncompleteArray is caught by isIncompleteType() above. 612 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType(); 613 614 case Builtin: 615 case Complex: 616 case Pointer: 617 case MemberPointer: 618 case Vector: 619 case ExtVector: 620 case ObjCObjectPointer: 621 return true; 622 623 case Enum: 624 return true; 625 626 case Record: 627 if (CXXRecordDecl *ClassDecl 628 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) 629 return ClassDecl->isPOD(); 630 631 // C struct/union is POD. 632 return true; 633 } 634} 635 636bool Type::isLiteralType() const { 637 if (isIncompleteType()) 638 return false; 639 640 // C++0x [basic.types]p10: 641 // A type is a literal type if it is: 642 switch (CanonicalType->getTypeClass()) { 643 // We're whitelisting 644 default: return false; 645 646 // -- a scalar type 647 case Builtin: 648 case Complex: 649 case Pointer: 650 case MemberPointer: 651 case Vector: 652 case ExtVector: 653 case ObjCObjectPointer: 654 case Enum: 655 return true; 656 657 // -- a class type with ... 658 case Record: 659 // FIXME: Do the tests 660 return false; 661 662 // -- an array of literal type 663 // Extension: variable arrays cannot be literal types, since they're 664 // runtime-sized. 665 case ConstantArray: 666 return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType(); 667 } 668} 669 670bool Type::isPromotableIntegerType() const { 671 if (const BuiltinType *BT = getAs<BuiltinType>()) 672 switch (BT->getKind()) { 673 case BuiltinType::Bool: 674 case BuiltinType::Char_S: 675 case BuiltinType::Char_U: 676 case BuiltinType::SChar: 677 case BuiltinType::UChar: 678 case BuiltinType::Short: 679 case BuiltinType::UShort: 680 return true; 681 default: 682 return false; 683 } 684 return false; 685} 686 687bool Type::isNullPtrType() const { 688 if (const BuiltinType *BT = getAs<BuiltinType>()) 689 return BT->getKind() == BuiltinType::NullPtr; 690 return false; 691} 692 693bool Type::isSpecifierType() const { 694 // Note that this intentionally does not use the canonical type. 695 switch (getTypeClass()) { 696 case Builtin: 697 case Record: 698 case Enum: 699 case Typedef: 700 case Complex: 701 case TypeOfExpr: 702 case TypeOf: 703 case TemplateTypeParm: 704 case SubstTemplateTypeParm: 705 case TemplateSpecialization: 706 case QualifiedName: 707 case Typename: 708 case ObjCInterface: 709 case ObjCObjectPointer: 710 case Elaborated: 711 return true; 712 default: 713 return false; 714 } 715} 716 717const char *Type::getTypeClassName() const { 718 switch (TC) { 719 default: assert(0 && "Type class not in TypeNodes.def!"); 720#define ABSTRACT_TYPE(Derived, Base) 721#define TYPE(Derived, Base) case Derived: return #Derived; 722#include "clang/AST/TypeNodes.def" 723 } 724} 725 726const char *BuiltinType::getName(const LangOptions &LO) const { 727 switch (getKind()) { 728 default: assert(0 && "Unknown builtin type!"); 729 case Void: return "void"; 730 case Bool: return LO.Bool ? "bool" : "_Bool"; 731 case Char_S: return "char"; 732 case Char_U: return "char"; 733 case SChar: return "signed char"; 734 case Short: return "short"; 735 case Int: return "int"; 736 case Long: return "long"; 737 case LongLong: return "long long"; 738 case Int128: return "__int128_t"; 739 case UChar: return "unsigned char"; 740 case UShort: return "unsigned short"; 741 case UInt: return "unsigned int"; 742 case ULong: return "unsigned long"; 743 case ULongLong: return "unsigned long long"; 744 case UInt128: return "__uint128_t"; 745 case Float: return "float"; 746 case Double: return "double"; 747 case LongDouble: return "long double"; 748 case WChar: return "wchar_t"; 749 case Char16: return "char16_t"; 750 case Char32: return "char32_t"; 751 case NullPtr: return "nullptr_t"; 752 case Overload: return "<overloaded function type>"; 753 case Dependent: return "<dependent type>"; 754 case UndeducedAuto: return "auto"; 755 case ObjCId: return "id"; 756 case ObjCClass: return "Class"; 757 case ObjCSel: return "SEL"; 758 } 759} 760 761void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 762 arg_type_iterator ArgTys, 763 unsigned NumArgs, bool isVariadic, 764 unsigned TypeQuals, bool hasExceptionSpec, 765 bool anyExceptionSpec, unsigned NumExceptions, 766 exception_iterator Exs, bool NoReturn) { 767 ID.AddPointer(Result.getAsOpaquePtr()); 768 for (unsigned i = 0; i != NumArgs; ++i) 769 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 770 ID.AddInteger(isVariadic); 771 ID.AddInteger(TypeQuals); 772 ID.AddInteger(hasExceptionSpec); 773 if (hasExceptionSpec) { 774 ID.AddInteger(anyExceptionSpec); 775 for (unsigned i = 0; i != NumExceptions; ++i) 776 ID.AddPointer(Exs[i].getAsOpaquePtr()); 777 } 778 ID.AddInteger(NoReturn); 779} 780 781void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { 782 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), 783 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), 784 getNumExceptions(), exception_begin(), getNoReturnAttr()); 785} 786 787void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID, 788 QualType OIT, ObjCProtocolDecl **protocols, 789 unsigned NumProtocols) { 790 ID.AddPointer(OIT.getAsOpaquePtr()); 791 for (unsigned i = 0; i != NumProtocols; i++) 792 ID.AddPointer(protocols[i]); 793} 794 795void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) { 796 if (getNumProtocols()) 797 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols()); 798 else 799 Profile(ID, getPointeeType(), 0, 0); 800} 801 802/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 803/// potentially looking through *all* consequtive typedefs. This returns the 804/// sum of the type qualifiers, so if you have: 805/// typedef const int A; 806/// typedef volatile A B; 807/// looking through the typedefs for B will give you "const volatile A". 808/// 809QualType TypedefType::LookThroughTypedefs() const { 810 // Usually, there is only a single level of typedefs, be fast in that case. 811 QualType FirstType = getDecl()->getUnderlyingType(); 812 if (!isa<TypedefType>(FirstType)) 813 return FirstType; 814 815 // Otherwise, do the fully general loop. 816 QualifierCollector Qs; 817 818 QualType CurType; 819 const TypedefType *TDT = this; 820 do { 821 CurType = TDT->getDecl()->getUnderlyingType(); 822 TDT = dyn_cast<TypedefType>(Qs.strip(CurType)); 823 } while (TDT); 824 825 return Qs.apply(CurType); 826} 827 828QualType TypedefType::desugar() const { 829 return getDecl()->getUnderlyingType(); 830} 831 832TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 833 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { 834} 835 836QualType TypeOfExprType::desugar() const { 837 return getUnderlyingExpr()->getType(); 838} 839 840void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, 841 ASTContext &Context, Expr *E) { 842 E->Profile(ID, Context, true); 843} 844 845DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) 846 : Type(Decltype, can, E->isTypeDependent()), E(E), 847 UnderlyingType(underlyingType) { 848} 849 850DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) 851 : DecltypeType(E, Context.DependentTy), Context(Context) { } 852 853void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, 854 ASTContext &Context, Expr *E) { 855 E->Profile(ID, Context, true); 856} 857 858TagType::TagType(TypeClass TC, TagDecl *D, QualType can) 859 : Type(TC, can, D->isDependentType()), decl(D, 0) {} 860 861bool RecordType::classof(const TagType *TT) { 862 return isa<RecordDecl>(TT->getDecl()); 863} 864 865bool EnumType::classof(const TagType *TT) { 866 return isa<EnumDecl>(TT->getDecl()); 867} 868 869static bool isDependent(const TemplateArgument &Arg) { 870 switch (Arg.getKind()) { 871 case TemplateArgument::Null: 872 assert(false && "Should not have a NULL template argument"); 873 return false; 874 875 case TemplateArgument::Type: 876 return Arg.getAsType()->isDependentType(); 877 878 case TemplateArgument::Template: 879 return Arg.getAsTemplate().isDependent(); 880 881 case TemplateArgument::Declaration: 882 case TemplateArgument::Integral: 883 // Never dependent 884 return false; 885 886 case TemplateArgument::Expression: 887 return (Arg.getAsExpr()->isTypeDependent() || 888 Arg.getAsExpr()->isValueDependent()); 889 890 case TemplateArgument::Pack: 891 assert(0 && "FIXME: Implement!"); 892 return false; 893 } 894 895 return false; 896} 897 898bool TemplateSpecializationType:: 899anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) { 900 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size()); 901} 902 903bool TemplateSpecializationType:: 904anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) { 905 for (unsigned i = 0; i != N; ++i) 906 if (isDependent(Args[i].getArgument())) 907 return true; 908 return false; 909} 910 911bool TemplateSpecializationType:: 912anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) { 913 for (unsigned i = 0; i != N; ++i) 914 if (isDependent(Args[i])) 915 return true; 916 return false; 917} 918 919TemplateSpecializationType:: 920TemplateSpecializationType(ASTContext &Context, TemplateName T, 921 const TemplateArgument *Args, 922 unsigned NumArgs, QualType Canon) 923 : Type(TemplateSpecialization, 924 Canon.isNull()? QualType(this, 0) : Canon, 925 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), 926 Context(Context), 927 Template(T), NumArgs(NumArgs) { 928 assert((!Canon.isNull() || 929 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && 930 "No canonical type for non-dependent class template specialization"); 931 932 TemplateArgument *TemplateArgs 933 = reinterpret_cast<TemplateArgument *>(this + 1); 934 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) 935 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); 936} 937 938void TemplateSpecializationType::Destroy(ASTContext& C) { 939 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 940 // FIXME: Not all expressions get cloned, so we can't yet perform 941 // this destruction. 942 // if (Expr *E = getArg(Arg).getAsExpr()) 943 // E->Destroy(C); 944 } 945} 946 947TemplateSpecializationType::iterator 948TemplateSpecializationType::end() const { 949 return begin() + getNumArgs(); 950} 951 952const TemplateArgument & 953TemplateSpecializationType::getArg(unsigned Idx) const { 954 assert(Idx < getNumArgs() && "Template argument out of range"); 955 return getArgs()[Idx]; 956} 957 958void 959TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 960 TemplateName T, 961 const TemplateArgument *Args, 962 unsigned NumArgs, 963 ASTContext &Context) { 964 T.Profile(ID); 965 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 966 Args[Idx].Profile(ID, Context); 967} 968 969QualType QualifierCollector::apply(QualType QT) const { 970 if (!hasNonFastQualifiers()) 971 return QT.withFastQualifiers(getFastQualifiers()); 972 973 assert(Context && "extended qualifiers but no context!"); 974 return Context->getQualifiedType(QT, *this); 975} 976 977QualType QualifierCollector::apply(const Type *T) const { 978 if (!hasNonFastQualifiers()) 979 return QualType(T, getFastQualifiers()); 980 981 assert(Context && "extended qualifiers but no context!"); 982 return Context->getQualifiedType(T, *this); 983} 984 985void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID, 986 const ObjCInterfaceDecl *Decl, 987 ObjCProtocolDecl **protocols, 988 unsigned NumProtocols) { 989 ID.AddPointer(Decl); 990 for (unsigned i = 0; i != NumProtocols; i++) 991 ID.AddPointer(protocols[i]); 992} 993 994void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { 995 if (getNumProtocols()) 996 Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); 997 else 998 Profile(ID, getDecl(), 0, 0); 999} 1000