Type.cpp revision 200583
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 (isa<FixedWidthIntType>(CanonicalType)) 391 return true; 392 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 393 return VT->getElementType()->isIntegerType(); 394 return false; 395} 396 397bool Type::isIntegralType() const { 398 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 399 return BT->getKind() >= BuiltinType::Bool && 400 BT->getKind() <= BuiltinType::LongLong; 401 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 402 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 403 return true; // Complete enum types are integral. 404 // FIXME: In C++, enum types are never integral. 405 if (isa<FixedWidthIntType>(CanonicalType)) 406 return true; 407 return false; 408} 409 410bool Type::isEnumeralType() const { 411 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 412 return TT->getDecl()->isEnum(); 413 return false; 414} 415 416bool Type::isBooleanType() const { 417 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 418 return BT->getKind() == BuiltinType::Bool; 419 return false; 420} 421 422bool Type::isCharType() const { 423 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 424 return BT->getKind() == BuiltinType::Char_U || 425 BT->getKind() == BuiltinType::UChar || 426 BT->getKind() == BuiltinType::Char_S || 427 BT->getKind() == BuiltinType::SChar; 428 return false; 429} 430 431bool Type::isWideCharType() const { 432 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 433 return BT->getKind() == BuiltinType::WChar; 434 return false; 435} 436 437/// \brief Determine whether this type is any of the built-in character 438/// types. 439bool Type::isAnyCharacterType() const { 440 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 441 return (BT->getKind() >= BuiltinType::Char_U && 442 BT->getKind() <= BuiltinType::Char32) || 443 (BT->getKind() >= BuiltinType::Char_S && 444 BT->getKind() <= BuiltinType::WChar); 445 446 return false; 447} 448 449/// isSignedIntegerType - Return true if this is an integer type that is 450/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 451/// an enum decl which has a signed representation, or a vector of signed 452/// integer element type. 453bool Type::isSignedIntegerType() const { 454 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 455 return BT->getKind() >= BuiltinType::Char_S && 456 BT->getKind() <= BuiltinType::LongLong; 457 } 458 459 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 460 return ET->getDecl()->getIntegerType()->isSignedIntegerType(); 461 462 if (const FixedWidthIntType *FWIT = 463 dyn_cast<FixedWidthIntType>(CanonicalType)) 464 return FWIT->isSigned(); 465 466 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 467 return VT->getElementType()->isSignedIntegerType(); 468 return false; 469} 470 471/// isUnsignedIntegerType - Return true if this is an integer type that is 472/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 473/// decl which has an unsigned representation, or a vector of unsigned integer 474/// element type. 475bool Type::isUnsignedIntegerType() const { 476 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 477 return BT->getKind() >= BuiltinType::Bool && 478 BT->getKind() <= BuiltinType::UInt128; 479 } 480 481 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 482 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); 483 484 if (const FixedWidthIntType *FWIT = 485 dyn_cast<FixedWidthIntType>(CanonicalType)) 486 return !FWIT->isSigned(); 487 488 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 489 return VT->getElementType()->isUnsignedIntegerType(); 490 return false; 491} 492 493bool Type::isFloatingType() const { 494 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 495 return BT->getKind() >= BuiltinType::Float && 496 BT->getKind() <= BuiltinType::LongDouble; 497 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 498 return CT->getElementType()->isFloatingType(); 499 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 500 return VT->getElementType()->isFloatingType(); 501 return false; 502} 503 504bool Type::isRealFloatingType() const { 505 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 506 return BT->isFloatingPoint(); 507 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 508 return VT->getElementType()->isRealFloatingType(); 509 return false; 510} 511 512bool Type::isRealType() const { 513 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 514 return BT->getKind() >= BuiltinType::Bool && 515 BT->getKind() <= BuiltinType::LongDouble; 516 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 517 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); 518 if (isa<FixedWidthIntType>(CanonicalType)) 519 return true; 520 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 521 return VT->getElementType()->isRealType(); 522 return false; 523} 524 525bool Type::isArithmeticType() const { 526 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 527 return BT->getKind() >= BuiltinType::Bool && 528 BT->getKind() <= BuiltinType::LongDouble; 529 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 530 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). 531 // If a body isn't seen by the time we get here, return false. 532 return ET->getDecl()->isDefinition(); 533 if (isa<FixedWidthIntType>(CanonicalType)) 534 return true; 535 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); 536} 537 538bool Type::isScalarType() const { 539 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 540 return BT->getKind() != BuiltinType::Void; 541 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { 542 // Enums are scalar types, but only if they are defined. Incomplete enums 543 // are not treated as scalar types. 544 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 545 return true; 546 return false; 547 } 548 if (isa<FixedWidthIntType>(CanonicalType)) 549 return true; 550 return isa<PointerType>(CanonicalType) || 551 isa<BlockPointerType>(CanonicalType) || 552 isa<MemberPointerType>(CanonicalType) || 553 isa<ComplexType>(CanonicalType) || 554 isa<ObjCObjectPointerType>(CanonicalType); 555} 556 557/// \brief Determines whether the type is a C++ aggregate type or C 558/// aggregate or union type. 559/// 560/// An aggregate type is an array or a class type (struct, union, or 561/// class) that has no user-declared constructors, no private or 562/// protected non-static data members, no base classes, and no virtual 563/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type 564/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also 565/// includes union types. 566bool Type::isAggregateType() const { 567 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { 568 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) 569 return ClassDecl->isAggregate(); 570 571 return true; 572 } 573 574 return isa<ArrayType>(CanonicalType); 575} 576 577/// isConstantSizeType - Return true if this is not a variable sized type, 578/// according to the rules of C99 6.7.5p3. It is not legal to call this on 579/// incomplete types or dependent types. 580bool Type::isConstantSizeType() const { 581 assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); 582 assert(!isDependentType() && "This doesn't make sense for dependent types"); 583 // The VAT must have a size, as it is known to be complete. 584 return !isa<VariableArrayType>(CanonicalType); 585} 586 587/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) 588/// - a type that can describe objects, but which lacks information needed to 589/// determine its size. 590bool Type::isIncompleteType() const { 591 switch (CanonicalType->getTypeClass()) { 592 default: return false; 593 case Builtin: 594 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never 595 // be completed. 596 return isVoidType(); 597 case Record: 598 case Enum: 599 // A tagged type (struct/union/enum/class) is incomplete if the decl is a 600 // forward declaration, but not a full definition (C99 6.2.5p22). 601 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); 602 case ConstantArray: 603 // An array is incomplete if its element type is incomplete 604 // (C++ [dcl.array]p1). 605 // We don't handle variable arrays (they're not allowed in C++) or 606 // dependent-sized arrays (dependent types are never treated as incomplete). 607 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType(); 608 case IncompleteArray: 609 // An array of unknown size is an incomplete type (C99 6.2.5p22). 610 return true; 611 case ObjCInterface: 612 // ObjC interfaces are incomplete if they are @class, not @interface. 613 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl(); 614 } 615} 616 617/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) 618bool Type::isPODType() const { 619 // The compiler shouldn't query this for incomplete types, but the user might. 620 // We return false for that case. 621 if (isIncompleteType()) 622 return false; 623 624 switch (CanonicalType->getTypeClass()) { 625 // Everything not explicitly mentioned is not POD. 626 default: return false; 627 case VariableArray: 628 case ConstantArray: 629 // IncompleteArray is caught by isIncompleteType() above. 630 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType(); 631 632 case Builtin: 633 case Complex: 634 case Pointer: 635 case MemberPointer: 636 case Vector: 637 case ExtVector: 638 case ObjCObjectPointer: 639 return true; 640 641 case Enum: 642 return true; 643 644 case Record: 645 if (CXXRecordDecl *ClassDecl 646 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) 647 return ClassDecl->isPOD(); 648 649 // C struct/union is POD. 650 return true; 651 } 652} 653 654bool Type::isLiteralType() const { 655 if (isIncompleteType()) 656 return false; 657 658 // C++0x [basic.types]p10: 659 // A type is a literal type if it is: 660 switch (CanonicalType->getTypeClass()) { 661 // We're whitelisting 662 default: return false; 663 664 // -- a scalar type 665 case Builtin: 666 case Complex: 667 case Pointer: 668 case MemberPointer: 669 case Vector: 670 case ExtVector: 671 case ObjCObjectPointer: 672 case Enum: 673 return true; 674 675 // -- a class type with ... 676 case Record: 677 // FIXME: Do the tests 678 return false; 679 680 // -- an array of literal type 681 // Extension: variable arrays cannot be literal types, since they're 682 // runtime-sized. 683 case ConstantArray: 684 return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType(); 685 } 686} 687 688bool Type::isPromotableIntegerType() const { 689 if (const BuiltinType *BT = getAs<BuiltinType>()) 690 switch (BT->getKind()) { 691 case BuiltinType::Bool: 692 case BuiltinType::Char_S: 693 case BuiltinType::Char_U: 694 case BuiltinType::SChar: 695 case BuiltinType::UChar: 696 case BuiltinType::Short: 697 case BuiltinType::UShort: 698 return true; 699 default: 700 return false; 701 } 702 return false; 703} 704 705bool Type::isNullPtrType() const { 706 if (const BuiltinType *BT = getAs<BuiltinType>()) 707 return BT->getKind() == BuiltinType::NullPtr; 708 return false; 709} 710 711bool Type::isSpecifierType() const { 712 // Note that this intentionally does not use the canonical type. 713 switch (getTypeClass()) { 714 case Builtin: 715 case Record: 716 case Enum: 717 case Typedef: 718 case Complex: 719 case TypeOfExpr: 720 case TypeOf: 721 case TemplateTypeParm: 722 case SubstTemplateTypeParm: 723 case TemplateSpecialization: 724 case QualifiedName: 725 case Typename: 726 case ObjCInterface: 727 case ObjCObjectPointer: 728 return true; 729 default: 730 return false; 731 } 732} 733 734const char *Type::getTypeClassName() const { 735 switch (TC) { 736 default: assert(0 && "Type class not in TypeNodes.def!"); 737#define ABSTRACT_TYPE(Derived, Base) 738#define TYPE(Derived, Base) case Derived: return #Derived; 739#include "clang/AST/TypeNodes.def" 740 } 741} 742 743const char *BuiltinType::getName(const LangOptions &LO) const { 744 switch (getKind()) { 745 default: assert(0 && "Unknown builtin type!"); 746 case Void: return "void"; 747 case Bool: return LO.Bool ? "bool" : "_Bool"; 748 case Char_S: return "char"; 749 case Char_U: return "char"; 750 case SChar: return "signed char"; 751 case Short: return "short"; 752 case Int: return "int"; 753 case Long: return "long"; 754 case LongLong: return "long long"; 755 case Int128: return "__int128_t"; 756 case UChar: return "unsigned char"; 757 case UShort: return "unsigned short"; 758 case UInt: return "unsigned int"; 759 case ULong: return "unsigned long"; 760 case ULongLong: return "unsigned long long"; 761 case UInt128: return "__uint128_t"; 762 case Float: return "float"; 763 case Double: return "double"; 764 case LongDouble: return "long double"; 765 case WChar: return "wchar_t"; 766 case Char16: return "char16_t"; 767 case Char32: return "char32_t"; 768 case NullPtr: return "nullptr_t"; 769 case Overload: return "<overloaded function type>"; 770 case Dependent: return "<dependent type>"; 771 case UndeducedAuto: return "auto"; 772 case ObjCId: return "id"; 773 case ObjCClass: return "Class"; 774 case ObjCSel: return "SEL"; 775 } 776} 777 778void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 779 arg_type_iterator ArgTys, 780 unsigned NumArgs, bool isVariadic, 781 unsigned TypeQuals, bool hasExceptionSpec, 782 bool anyExceptionSpec, unsigned NumExceptions, 783 exception_iterator Exs, bool NoReturn) { 784 ID.AddPointer(Result.getAsOpaquePtr()); 785 for (unsigned i = 0; i != NumArgs; ++i) 786 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 787 ID.AddInteger(isVariadic); 788 ID.AddInteger(TypeQuals); 789 ID.AddInteger(hasExceptionSpec); 790 if (hasExceptionSpec) { 791 ID.AddInteger(anyExceptionSpec); 792 for (unsigned i = 0; i != NumExceptions; ++i) 793 ID.AddPointer(Exs[i].getAsOpaquePtr()); 794 } 795 ID.AddInteger(NoReturn); 796} 797 798void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { 799 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), 800 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), 801 getNumExceptions(), exception_begin(), getNoReturnAttr()); 802} 803 804void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID, 805 QualType OIT, ObjCProtocolDecl **protocols, 806 unsigned NumProtocols) { 807 ID.AddPointer(OIT.getAsOpaquePtr()); 808 for (unsigned i = 0; i != NumProtocols; i++) 809 ID.AddPointer(protocols[i]); 810} 811 812void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) { 813 if (getNumProtocols()) 814 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols()); 815 else 816 Profile(ID, getPointeeType(), 0, 0); 817} 818 819/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 820/// potentially looking through *all* consequtive typedefs. This returns the 821/// sum of the type qualifiers, so if you have: 822/// typedef const int A; 823/// typedef volatile A B; 824/// looking through the typedefs for B will give you "const volatile A". 825/// 826QualType TypedefType::LookThroughTypedefs() const { 827 // Usually, there is only a single level of typedefs, be fast in that case. 828 QualType FirstType = getDecl()->getUnderlyingType(); 829 if (!isa<TypedefType>(FirstType)) 830 return FirstType; 831 832 // Otherwise, do the fully general loop. 833 QualifierCollector Qs; 834 835 QualType CurType; 836 const TypedefType *TDT = this; 837 do { 838 CurType = TDT->getDecl()->getUnderlyingType(); 839 TDT = dyn_cast<TypedefType>(Qs.strip(CurType)); 840 } while (TDT); 841 842 return Qs.apply(CurType); 843} 844 845QualType TypedefType::desugar() const { 846 return getDecl()->getUnderlyingType(); 847} 848 849TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 850 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { 851} 852 853QualType TypeOfExprType::desugar() const { 854 return getUnderlyingExpr()->getType(); 855} 856 857void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, 858 ASTContext &Context, Expr *E) { 859 E->Profile(ID, Context, true); 860} 861 862DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) 863 : Type(Decltype, can, E->isTypeDependent()), E(E), 864 UnderlyingType(underlyingType) { 865} 866 867DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) 868 : DecltypeType(E, Context.DependentTy), Context(Context) { } 869 870void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, 871 ASTContext &Context, Expr *E) { 872 E->Profile(ID, Context, true); 873} 874 875TagType::TagType(TypeClass TC, TagDecl *D, QualType can) 876 : Type(TC, can, D->isDependentType()), decl(D, 0) {} 877 878bool RecordType::classof(const TagType *TT) { 879 return isa<RecordDecl>(TT->getDecl()); 880} 881 882bool EnumType::classof(const TagType *TT) { 883 return isa<EnumDecl>(TT->getDecl()); 884} 885 886static bool isDependent(const TemplateArgument &Arg) { 887 switch (Arg.getKind()) { 888 case TemplateArgument::Null: 889 assert(false && "Should not have a NULL template argument"); 890 return false; 891 892 case TemplateArgument::Type: 893 return Arg.getAsType()->isDependentType(); 894 895 case TemplateArgument::Template: 896 return Arg.getAsTemplate().isDependent(); 897 898 case TemplateArgument::Declaration: 899 case TemplateArgument::Integral: 900 // Never dependent 901 return false; 902 903 case TemplateArgument::Expression: 904 return (Arg.getAsExpr()->isTypeDependent() || 905 Arg.getAsExpr()->isValueDependent()); 906 907 case TemplateArgument::Pack: 908 assert(0 && "FIXME: Implement!"); 909 return false; 910 } 911 912 return false; 913} 914 915bool TemplateSpecializationType:: 916anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) { 917 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size()); 918} 919 920bool TemplateSpecializationType:: 921anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) { 922 for (unsigned i = 0; i != N; ++i) 923 if (isDependent(Args[i].getArgument())) 924 return true; 925 return false; 926} 927 928bool TemplateSpecializationType:: 929anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) { 930 for (unsigned i = 0; i != N; ++i) 931 if (isDependent(Args[i])) 932 return true; 933 return false; 934} 935 936TemplateSpecializationType:: 937TemplateSpecializationType(ASTContext &Context, TemplateName T, 938 const TemplateArgument *Args, 939 unsigned NumArgs, QualType Canon) 940 : Type(TemplateSpecialization, 941 Canon.isNull()? QualType(this, 0) : Canon, 942 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), 943 Context(Context), 944 Template(T), NumArgs(NumArgs) { 945 assert((!Canon.isNull() || 946 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && 947 "No canonical type for non-dependent class template specialization"); 948 949 TemplateArgument *TemplateArgs 950 = reinterpret_cast<TemplateArgument *>(this + 1); 951 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) 952 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); 953} 954 955void TemplateSpecializationType::Destroy(ASTContext& C) { 956 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 957 // FIXME: Not all expressions get cloned, so we can't yet perform 958 // this destruction. 959 // if (Expr *E = getArg(Arg).getAsExpr()) 960 // E->Destroy(C); 961 } 962} 963 964TemplateSpecializationType::iterator 965TemplateSpecializationType::end() const { 966 return begin() + getNumArgs(); 967} 968 969const TemplateArgument & 970TemplateSpecializationType::getArg(unsigned Idx) const { 971 assert(Idx < getNumArgs() && "Template argument out of range"); 972 return getArgs()[Idx]; 973} 974 975void 976TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 977 TemplateName T, 978 const TemplateArgument *Args, 979 unsigned NumArgs, 980 ASTContext &Context) { 981 T.Profile(ID); 982 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 983 Args[Idx].Profile(ID, Context); 984} 985 986QualType QualifierCollector::apply(QualType QT) const { 987 if (!hasNonFastQualifiers()) 988 return QT.withFastQualifiers(getFastQualifiers()); 989 990 assert(Context && "extended qualifiers but no context!"); 991 return Context->getQualifiedType(QT, *this); 992} 993 994QualType QualifierCollector::apply(const Type *T) const { 995 if (!hasNonFastQualifiers()) 996 return QualType(T, getFastQualifiers()); 997 998 assert(Context && "extended qualifiers but no context!"); 999 return Context->getQualifiedType(T, *this); 1000} 1001 1002void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID, 1003 const ObjCInterfaceDecl *Decl, 1004 ObjCProtocolDecl **protocols, 1005 unsigned NumProtocols) { 1006 ID.AddPointer(Decl); 1007 for (unsigned i = 0; i != NumProtocols; i++) 1008 ID.AddPointer(protocols[i]); 1009} 1010 1011void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { 1012 if (getNumProtocols()) 1013 Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); 1014 else 1015 Profile(ID, getDecl(), 0, 0); 1016} 1017