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