Type.cpp revision 04a67a6aa3dfdc92d57f7f8d93ba397348c868a4
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 342ObjCInterfaceType::ObjCInterfaceType(ASTContext &Ctx, QualType Canonical, 343 ObjCInterfaceDecl *D, 344 ObjCProtocolDecl **Protos, unsigned NumP) : 345 Type(ObjCInterface, Canonical, /*Dependent=*/false), 346 Decl(D), Protocols(0), NumProtocols(NumP) 347{ 348 if (NumProtocols) { 349 Protocols = new (Ctx) ObjCProtocolDecl*[NumProtocols]; 350 memcpy(Protocols, Protos, NumProtocols * sizeof(*Protocols)); 351 } 352} 353 354void ObjCInterfaceType::Destroy(ASTContext& C) { 355 if (Protocols) 356 C.Deallocate(Protocols); 357 this->~ObjCInterfaceType(); 358 C.Deallocate(this); 359} 360 361const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const { 362 // There is no sugar for ObjCInterfaceType's, just return the canonical 363 // type pointer if it is the right class. There is no typedef information to 364 // return and these cannot be Address-space qualified. 365 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>()) 366 if (OIT->getNumProtocols()) 367 return OIT; 368 return 0; 369} 370 371bool Type::isObjCQualifiedInterfaceType() const { 372 return getAsObjCQualifiedInterfaceType() != 0; 373} 374 375ObjCObjectPointerType::ObjCObjectPointerType(ASTContext &Ctx, 376 QualType Canonical, QualType T, 377 ObjCProtocolDecl **Protos, 378 unsigned NumP) : 379 Type(ObjCObjectPointer, Canonical, /*Dependent=*/false), 380 PointeeType(T), Protocols(NULL), NumProtocols(NumP) 381{ 382 if (NumProtocols) { 383 Protocols = new (Ctx) ObjCProtocolDecl*[NumProtocols]; 384 memcpy(Protocols, Protos, NumProtocols * sizeof(*Protocols)); 385 } 386} 387 388void ObjCObjectPointerType::Destroy(ASTContext& C) { 389 if (Protocols) 390 C.Deallocate(Protocols); 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 return true; 661 662 case Enum: 663 return true; 664 665 case Record: 666 if (CXXRecordDecl *ClassDecl 667 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) 668 return ClassDecl->isPOD(); 669 670 // C struct/union is POD. 671 return true; 672 } 673} 674 675bool Type::isLiteralType() const { 676 if (isIncompleteType()) 677 return false; 678 679 // C++0x [basic.types]p10: 680 // A type is a literal type if it is: 681 switch (CanonicalType->getTypeClass()) { 682 // We're whitelisting 683 default: return false; 684 685 // -- a scalar type 686 case Builtin: 687 case Complex: 688 case Pointer: 689 case MemberPointer: 690 case Vector: 691 case ExtVector: 692 case ObjCObjectPointer: 693 case Enum: 694 return true; 695 696 // -- a class type with ... 697 case Record: 698 // FIXME: Do the tests 699 return false; 700 701 // -- an array of literal type 702 // Extension: variable arrays cannot be literal types, since they're 703 // runtime-sized. 704 case ConstantArray: 705 return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType(); 706 } 707} 708 709bool Type::isPromotableIntegerType() const { 710 if (const BuiltinType *BT = getAs<BuiltinType>()) 711 switch (BT->getKind()) { 712 case BuiltinType::Bool: 713 case BuiltinType::Char_S: 714 case BuiltinType::Char_U: 715 case BuiltinType::SChar: 716 case BuiltinType::UChar: 717 case BuiltinType::Short: 718 case BuiltinType::UShort: 719 return true; 720 default: 721 return false; 722 } 723 724 // Enumerated types are promotable to their compatible integer types 725 // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2). 726 if (const EnumType *ET = getAs<EnumType>()){ 727 if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()) 728 return false; 729 730 const BuiltinType *BT 731 = ET->getDecl()->getPromotionType()->getAs<BuiltinType>(); 732 return BT->getKind() == BuiltinType::Int 733 || BT->getKind() == BuiltinType::UInt; 734 } 735 736 return false; 737} 738 739bool Type::isNullPtrType() const { 740 if (const BuiltinType *BT = getAs<BuiltinType>()) 741 return BT->getKind() == BuiltinType::NullPtr; 742 return false; 743} 744 745bool Type::isSpecifierType() const { 746 // Note that this intentionally does not use the canonical type. 747 switch (getTypeClass()) { 748 case Builtin: 749 case Record: 750 case Enum: 751 case Typedef: 752 case Complex: 753 case TypeOfExpr: 754 case TypeOf: 755 case TemplateTypeParm: 756 case SubstTemplateTypeParm: 757 case TemplateSpecialization: 758 case QualifiedName: 759 case Typename: 760 case ObjCInterface: 761 case ObjCObjectPointer: 762 case Elaborated: 763 return true; 764 default: 765 return false; 766 } 767} 768 769const char *Type::getTypeClassName() const { 770 switch (TC) { 771 default: assert(0 && "Type class not in TypeNodes.def!"); 772#define ABSTRACT_TYPE(Derived, Base) 773#define TYPE(Derived, Base) case Derived: return #Derived; 774#include "clang/AST/TypeNodes.def" 775 } 776} 777 778const char *BuiltinType::getName(const LangOptions &LO) const { 779 switch (getKind()) { 780 default: assert(0 && "Unknown builtin type!"); 781 case Void: return "void"; 782 case Bool: return LO.Bool ? "bool" : "_Bool"; 783 case Char_S: return "char"; 784 case Char_U: return "char"; 785 case SChar: return "signed char"; 786 case Short: return "short"; 787 case Int: return "int"; 788 case Long: return "long"; 789 case LongLong: return "long long"; 790 case Int128: return "__int128_t"; 791 case UChar: return "unsigned char"; 792 case UShort: return "unsigned short"; 793 case UInt: return "unsigned int"; 794 case ULong: return "unsigned long"; 795 case ULongLong: return "unsigned long long"; 796 case UInt128: return "__uint128_t"; 797 case Float: return "float"; 798 case Double: return "double"; 799 case LongDouble: return "long double"; 800 case WChar: return "wchar_t"; 801 case Char16: return "char16_t"; 802 case Char32: return "char32_t"; 803 case NullPtr: return "nullptr_t"; 804 case Overload: return "<overloaded function type>"; 805 case Dependent: return "<dependent type>"; 806 case UndeducedAuto: return "auto"; 807 case ObjCId: return "id"; 808 case ObjCClass: return "Class"; 809 case ObjCSel: return "SEL"; 810 } 811} 812 813llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) { 814 switch (CC) { 815 case CC_Default: llvm_unreachable("no name for default cc"); 816 default: return ""; 817 818 case CC_C: return "cdecl"; 819 case CC_X86StdCall: return "stdcall"; 820 case CC_X86FastCall: return "fastcall"; 821 } 822} 823 824void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 825 arg_type_iterator ArgTys, 826 unsigned NumArgs, bool isVariadic, 827 unsigned TypeQuals, bool hasExceptionSpec, 828 bool anyExceptionSpec, unsigned NumExceptions, 829 exception_iterator Exs, bool NoReturn, 830 CallingConv CallConv) { 831 ID.AddPointer(Result.getAsOpaquePtr()); 832 for (unsigned i = 0; i != NumArgs; ++i) 833 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 834 ID.AddInteger(isVariadic); 835 ID.AddInteger(TypeQuals); 836 ID.AddInteger(hasExceptionSpec); 837 if (hasExceptionSpec) { 838 ID.AddInteger(anyExceptionSpec); 839 for (unsigned i = 0; i != NumExceptions; ++i) 840 ID.AddPointer(Exs[i].getAsOpaquePtr()); 841 } 842 ID.AddInteger(NoReturn); 843 ID.AddInteger(CallConv); 844} 845 846void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { 847 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), 848 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), 849 getNumExceptions(), exception_begin(), getNoReturnAttr(), 850 getCallConv()); 851} 852 853void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID, 854 QualType OIT, ObjCProtocolDecl **protocols, 855 unsigned NumProtocols) { 856 ID.AddPointer(OIT.getAsOpaquePtr()); 857 for (unsigned i = 0; i != NumProtocols; i++) 858 ID.AddPointer(protocols[i]); 859} 860 861void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) { 862 if (getNumProtocols()) 863 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols()); 864 else 865 Profile(ID, getPointeeType(), 0, 0); 866} 867 868/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 869/// potentially looking through *all* consequtive typedefs. This returns the 870/// sum of the type qualifiers, so if you have: 871/// typedef const int A; 872/// typedef volatile A B; 873/// looking through the typedefs for B will give you "const volatile A". 874/// 875QualType TypedefType::LookThroughTypedefs() const { 876 // Usually, there is only a single level of typedefs, be fast in that case. 877 QualType FirstType = getDecl()->getUnderlyingType(); 878 if (!isa<TypedefType>(FirstType)) 879 return FirstType; 880 881 // Otherwise, do the fully general loop. 882 QualifierCollector Qs; 883 884 QualType CurType; 885 const TypedefType *TDT = this; 886 do { 887 CurType = TDT->getDecl()->getUnderlyingType(); 888 TDT = dyn_cast<TypedefType>(Qs.strip(CurType)); 889 } while (TDT); 890 891 return Qs.apply(CurType); 892} 893 894QualType TypedefType::desugar() const { 895 return getDecl()->getUnderlyingType(); 896} 897 898TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 899 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { 900} 901 902QualType TypeOfExprType::desugar() const { 903 return getUnderlyingExpr()->getType(); 904} 905 906void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, 907 ASTContext &Context, Expr *E) { 908 E->Profile(ID, Context, true); 909} 910 911DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) 912 : Type(Decltype, can, E->isTypeDependent()), E(E), 913 UnderlyingType(underlyingType) { 914} 915 916DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) 917 : DecltypeType(E, Context.DependentTy), Context(Context) { } 918 919void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, 920 ASTContext &Context, Expr *E) { 921 E->Profile(ID, Context, true); 922} 923 924TagType::TagType(TypeClass TC, TagDecl *D, QualType can) 925 : Type(TC, can, D->isDependentType()), decl(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 **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 if (getNumProtocols()) 1062 Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); 1063 else 1064 Profile(ID, getDecl(), 0, 0); 1065} 1066 1067Linkage Type::getLinkage() const { 1068 // C++ [basic.link]p8: 1069 // Names not covered by these rules have no linkage. 1070 if (this != CanonicalType.getTypePtr()) 1071 return CanonicalType->getLinkage(); 1072 1073 return NoLinkage; 1074} 1075 1076Linkage BuiltinType::getLinkage() const { 1077 // C++ [basic.link]p8: 1078 // A type is said to have linkage if and only if: 1079 // - it is a fundamental type (3.9.1); or 1080 return ExternalLinkage; 1081} 1082 1083Linkage TagType::getLinkage() const { 1084 // C++ [basic.link]p8: 1085 // - it is a class or enumeration type that is named (or has a name for 1086 // linkage purposes (7.1.3)) and the name has linkage; or 1087 // - it is a specialization of a class template (14); or 1088 return getDecl()->getLinkage(); 1089} 1090 1091// C++ [basic.link]p8: 1092// - it is a compound type (3.9.2) other than a class or enumeration, 1093// compounded exclusively from types that have linkage; or 1094Linkage ComplexType::getLinkage() const { 1095 return ElementType->getLinkage(); 1096} 1097 1098Linkage PointerType::getLinkage() const { 1099 return PointeeType->getLinkage(); 1100} 1101 1102Linkage BlockPointerType::getLinkage() const { 1103 return PointeeType->getLinkage(); 1104} 1105 1106Linkage ReferenceType::getLinkage() const { 1107 return PointeeType->getLinkage(); 1108} 1109 1110Linkage MemberPointerType::getLinkage() const { 1111 return minLinkage(Class->getLinkage(), PointeeType->getLinkage()); 1112} 1113 1114Linkage ArrayType::getLinkage() const { 1115 return ElementType->getLinkage(); 1116} 1117 1118Linkage VectorType::getLinkage() const { 1119 return ElementType->getLinkage(); 1120} 1121 1122Linkage FunctionNoProtoType::getLinkage() const { 1123 return getResultType()->getLinkage(); 1124} 1125 1126Linkage FunctionProtoType::getLinkage() const { 1127 Linkage L = getResultType()->getLinkage(); 1128 for (arg_type_iterator A = arg_type_begin(), AEnd = arg_type_end(); 1129 A != AEnd; ++A) 1130 L = minLinkage(L, (*A)->getLinkage()); 1131 1132 return L; 1133} 1134 1135Linkage ObjCInterfaceType::getLinkage() const { 1136 return ExternalLinkage; 1137} 1138 1139Linkage ObjCObjectPointerType::getLinkage() const { 1140 return ExternalLinkage; 1141} 1142