Type.cpp revision 198092
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 ConstantArrayWithExprType::Destroy(ASTContext& C) { 41 // FIXME: destruction of SizeExpr commented out due to resource contention. 42 // SizeExpr->Destroy(C); 43 // See FIXME in SemaDecl.cpp:1536: if we were able to either steal 44 // or clone the SizeExpr there, then here we could freely delete it. 45 // Since we do not know how to steal or clone, we keep a pointer to 46 // a shared resource, but we cannot free it. 47 // (There probably is a trivial solution ... for people knowing clang!). 48 this->~ConstantArrayWithExprType(); 49 C.Deallocate(this); 50} 51 52void VariableArrayType::Destroy(ASTContext& C) { 53 if (SizeExpr) 54 SizeExpr->Destroy(C); 55 this->~VariableArrayType(); 56 C.Deallocate(this); 57} 58 59void DependentSizedArrayType::Destroy(ASTContext& C) { 60 // FIXME: Resource contention like in ConstantArrayWithExprType ? 61 // May crash, depending on platform or a particular build. 62 // SizeExpr->Destroy(C); 63 this->~DependentSizedArrayType(); 64 C.Deallocate(this); 65} 66 67void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID, 68 ASTContext &Context, 69 QualType ET, 70 ArraySizeModifier SizeMod, 71 unsigned TypeQuals, 72 Expr *E) { 73 ID.AddPointer(ET.getAsOpaquePtr()); 74 ID.AddInteger(SizeMod); 75 ID.AddInteger(TypeQuals); 76 E->Profile(ID, Context, true); 77} 78 79void 80DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID, 81 ASTContext &Context, 82 QualType ElementType, Expr *SizeExpr) { 83 ID.AddPointer(ElementType.getAsOpaquePtr()); 84 SizeExpr->Profile(ID, Context, true); 85} 86 87void DependentSizedExtVectorType::Destroy(ASTContext& C) { 88 // FIXME: Deallocate size expression, once we're cloning properly. 89// if (SizeExpr) 90// SizeExpr->Destroy(C); 91 this->~DependentSizedExtVectorType(); 92 C.Deallocate(this); 93} 94 95/// getArrayElementTypeNoTypeQual - If this is an array type, return the 96/// element type of the array, potentially with type qualifiers missing. 97/// This method should never be used when type qualifiers are meaningful. 98const Type *Type::getArrayElementTypeNoTypeQual() const { 99 // If this is directly an array type, return it. 100 if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) 101 return ATy->getElementType().getTypePtr(); 102 103 // If the canonical form of this type isn't the right kind, reject it. 104 if (!isa<ArrayType>(CanonicalType)) 105 return 0; 106 107 // If this is a typedef for an array type, strip the typedef off without 108 // losing all typedef information. 109 return cast<ArrayType>(getUnqualifiedDesugaredType()) 110 ->getElementType().getTypePtr(); 111} 112 113/// getDesugaredType - Return the specified type with any "sugar" removed from 114/// the type. This takes off typedefs, typeof's etc. If the outer level of 115/// the type is already concrete, it returns it unmodified. This is similar 116/// to getting the canonical type, but it doesn't remove *all* typedefs. For 117/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 118/// concrete. 119QualType QualType::getDesugaredType(QualType T) { 120 QualifierCollector Qs; 121 122 QualType Cur = T; 123 while (true) { 124 const Type *CurTy = Qs.strip(Cur); 125 switch (CurTy->getTypeClass()) { 126#define ABSTRACT_TYPE(Class, Parent) 127#define TYPE(Class, Parent) \ 128 case Type::Class: { \ 129 const Class##Type *Ty = cast<Class##Type>(CurTy); \ 130 if (!Ty->isSugared()) \ 131 return Qs.apply(Cur); \ 132 Cur = Ty->desugar(); \ 133 break; \ 134 } 135#include "clang/AST/TypeNodes.def" 136 } 137 } 138} 139 140/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic 141/// sugar off the given type. This should produce an object of the 142/// same dynamic type as the canonical type. 143const Type *Type::getUnqualifiedDesugaredType() const { 144 const Type *Cur = this; 145 146 while (true) { 147 switch (Cur->getTypeClass()) { 148#define ABSTRACT_TYPE(Class, Parent) 149#define TYPE(Class, Parent) \ 150 case Class: { \ 151 const Class##Type *Ty = cast<Class##Type>(Cur); \ 152 if (!Ty->isSugared()) return Cur; \ 153 Cur = Ty->desugar().getTypePtr(); \ 154 break; \ 155 } 156#include "clang/AST/TypeNodes.def" 157 } 158 } 159} 160 161/// isVoidType - Helper method to determine if this is the 'void' type. 162bool Type::isVoidType() const { 163 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 164 return BT->getKind() == BuiltinType::Void; 165 return false; 166} 167 168bool Type::isObjectType() const { 169 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) || 170 isa<IncompleteArrayType>(CanonicalType) || isVoidType()) 171 return false; 172 return true; 173} 174 175bool Type::isDerivedType() const { 176 switch (CanonicalType->getTypeClass()) { 177 case Pointer: 178 case VariableArray: 179 case ConstantArray: 180 case ConstantArrayWithExpr: 181 case ConstantArrayWithoutExpr: 182 case IncompleteArray: 183 case FunctionProto: 184 case FunctionNoProto: 185 case LValueReference: 186 case RValueReference: 187 case Record: 188 return true; 189 default: 190 return false; 191 } 192} 193 194bool Type::isClassType() const { 195 if (const RecordType *RT = getAs<RecordType>()) 196 return RT->getDecl()->isClass(); 197 return false; 198} 199bool Type::isStructureType() const { 200 if (const RecordType *RT = getAs<RecordType>()) 201 return RT->getDecl()->isStruct(); 202 return false; 203} 204bool Type::isVoidPointerType() const { 205 if (const PointerType *PT = getAs<PointerType>()) 206 return PT->getPointeeType()->isVoidType(); 207 return false; 208} 209 210bool Type::isUnionType() const { 211 if (const RecordType *RT = getAs<RecordType>()) 212 return RT->getDecl()->isUnion(); 213 return false; 214} 215 216bool Type::isComplexType() const { 217 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 218 return CT->getElementType()->isFloatingType(); 219 return false; 220} 221 222bool Type::isComplexIntegerType() const { 223 // Check for GCC complex integer extension. 224 return getAsComplexIntegerType(); 225} 226 227const ComplexType *Type::getAsComplexIntegerType() const { 228 if (const ComplexType *Complex = getAs<ComplexType>()) 229 if (Complex->getElementType()->isIntegerType()) 230 return Complex; 231 return 0; 232} 233 234QualType Type::getPointeeType() const { 235 if (const PointerType *PT = getAs<PointerType>()) 236 return PT->getPointeeType(); 237 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 238 return OPT->getPointeeType(); 239 if (const BlockPointerType *BPT = getAs<BlockPointerType>()) 240 return BPT->getPointeeType(); 241 return QualType(); 242} 243 244/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length 245/// array types and types that contain variable array types in their 246/// declarator 247bool Type::isVariablyModifiedType() const { 248 // A VLA is a variably modified type. 249 if (isVariableArrayType()) 250 return true; 251 252 // An array can contain a variably modified type 253 if (const Type *T = getArrayElementTypeNoTypeQual()) 254 return T->isVariablyModifiedType(); 255 256 // A pointer can point to a variably modified type. 257 // Also, C++ references and member pointers can point to a variably modified 258 // type, where VLAs appear as an extension to C++, and should be treated 259 // correctly. 260 if (const PointerType *PT = getAs<PointerType>()) 261 return PT->getPointeeType()->isVariablyModifiedType(); 262 if (const ReferenceType *RT = getAs<ReferenceType>()) 263 return RT->getPointeeType()->isVariablyModifiedType(); 264 if (const MemberPointerType *PT = getAs<MemberPointerType>()) 265 return PT->getPointeeType()->isVariablyModifiedType(); 266 267 // A function can return a variably modified type 268 // This one isn't completely obvious, but it follows from the 269 // definition in C99 6.7.5p3. Because of this rule, it's 270 // illegal to declare a function returning a variably modified type. 271 if (const FunctionType *FT = getAs<FunctionType>()) 272 return FT->getResultType()->isVariablyModifiedType(); 273 274 return false; 275} 276 277const RecordType *Type::getAsStructureType() const { 278 // If this is directly a structure type, return it. 279 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 280 if (RT->getDecl()->isStruct()) 281 return RT; 282 } 283 284 // If the canonical form of this type isn't the right kind, reject it. 285 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 286 if (!RT->getDecl()->isStruct()) 287 return 0; 288 289 // If this is a typedef for a structure type, strip the typedef off without 290 // losing all typedef information. 291 return cast<RecordType>(getUnqualifiedDesugaredType()); 292 } 293 return 0; 294} 295 296const RecordType *Type::getAsUnionType() const { 297 // If this is directly a union type, return it. 298 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 299 if (RT->getDecl()->isUnion()) 300 return RT; 301 } 302 303 // If the canonical form of this type isn't the right kind, reject it. 304 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 305 if (!RT->getDecl()->isUnion()) 306 return 0; 307 308 // If this is a typedef for a union type, strip the typedef off without 309 // losing all typedef information. 310 return cast<RecordType>(getUnqualifiedDesugaredType()); 311 } 312 313 return 0; 314} 315 316const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const { 317 // There is no sugar for ObjCInterfaceType's, just return the canonical 318 // type pointer if it is the right class. There is no typedef information to 319 // return and these cannot be Address-space qualified. 320 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>()) 321 if (OIT->getNumProtocols()) 322 return OIT; 323 return 0; 324} 325 326bool Type::isObjCQualifiedInterfaceType() const { 327 return getAsObjCQualifiedInterfaceType() != 0; 328} 329 330const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { 331 // There is no sugar for ObjCQualifiedIdType's, just return the canonical 332 // type pointer if it is the right class. 333 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 334 if (OPT->isObjCQualifiedIdType()) 335 return OPT; 336 } 337 return 0; 338} 339 340const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const { 341 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 342 if (OPT->getInterfaceType()) 343 return OPT; 344 } 345 return 0; 346} 347 348const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const { 349 if (const PointerType *PT = getAs<PointerType>()) 350 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) 351 return dyn_cast<CXXRecordDecl>(RT->getDecl()); 352 return 0; 353} 354 355bool Type::isIntegerType() const { 356 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 357 return BT->getKind() >= BuiltinType::Bool && 358 BT->getKind() <= BuiltinType::Int128; 359 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 360 // Incomplete enum types are not treated as integer types. 361 // FIXME: In C++, enum types are never integer types. 362 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 363 return true; 364 if (isa<FixedWidthIntType>(CanonicalType)) 365 return true; 366 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 367 return VT->getElementType()->isIntegerType(); 368 return false; 369} 370 371bool Type::isIntegralType() const { 372 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 373 return BT->getKind() >= BuiltinType::Bool && 374 BT->getKind() <= BuiltinType::LongLong; 375 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 376 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 377 return true; // Complete enum types are integral. 378 // FIXME: In C++, enum types are never integral. 379 if (isa<FixedWidthIntType>(CanonicalType)) 380 return true; 381 return false; 382} 383 384bool Type::isEnumeralType() const { 385 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 386 return TT->getDecl()->isEnum(); 387 return false; 388} 389 390bool Type::isBooleanType() const { 391 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 392 return BT->getKind() == BuiltinType::Bool; 393 return false; 394} 395 396bool Type::isCharType() const { 397 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 398 return BT->getKind() == BuiltinType::Char_U || 399 BT->getKind() == BuiltinType::UChar || 400 BT->getKind() == BuiltinType::Char_S || 401 BT->getKind() == BuiltinType::SChar; 402 return false; 403} 404 405bool Type::isWideCharType() const { 406 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 407 return BT->getKind() == BuiltinType::WChar; 408 return false; 409} 410 411/// isSignedIntegerType - Return true if this is an integer type that is 412/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 413/// an enum decl which has a signed representation, or a vector of signed 414/// integer element type. 415bool Type::isSignedIntegerType() const { 416 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 417 return BT->getKind() >= BuiltinType::Char_S && 418 BT->getKind() <= BuiltinType::LongLong; 419 } 420 421 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 422 return ET->getDecl()->getIntegerType()->isSignedIntegerType(); 423 424 if (const FixedWidthIntType *FWIT = 425 dyn_cast<FixedWidthIntType>(CanonicalType)) 426 return FWIT->isSigned(); 427 428 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 429 return VT->getElementType()->isSignedIntegerType(); 430 return false; 431} 432 433/// isUnsignedIntegerType - Return true if this is an integer type that is 434/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 435/// decl which has an unsigned representation, or a vector of unsigned integer 436/// element type. 437bool Type::isUnsignedIntegerType() const { 438 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 439 return BT->getKind() >= BuiltinType::Bool && 440 BT->getKind() <= BuiltinType::ULongLong; 441 } 442 443 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 444 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); 445 446 if (const FixedWidthIntType *FWIT = 447 dyn_cast<FixedWidthIntType>(CanonicalType)) 448 return !FWIT->isSigned(); 449 450 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 451 return VT->getElementType()->isUnsignedIntegerType(); 452 return false; 453} 454 455bool Type::isFloatingType() const { 456 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 457 return BT->getKind() >= BuiltinType::Float && 458 BT->getKind() <= BuiltinType::LongDouble; 459 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 460 return CT->getElementType()->isFloatingType(); 461 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 462 return VT->getElementType()->isFloatingType(); 463 return false; 464} 465 466bool Type::isRealFloatingType() const { 467 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 468 return BT->getKind() >= BuiltinType::Float && 469 BT->getKind() <= BuiltinType::LongDouble; 470 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 471 return VT->getElementType()->isRealFloatingType(); 472 return false; 473} 474 475bool Type::isRealType() const { 476 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 477 return BT->getKind() >= BuiltinType::Bool && 478 BT->getKind() <= BuiltinType::LongDouble; 479 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 480 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); 481 if (isa<FixedWidthIntType>(CanonicalType)) 482 return true; 483 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 484 return VT->getElementType()->isRealType(); 485 return false; 486} 487 488bool Type::isArithmeticType() const { 489 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 490 return BT->getKind() >= BuiltinType::Bool && 491 BT->getKind() <= BuiltinType::LongDouble; 492 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 493 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). 494 // If a body isn't seen by the time we get here, return false. 495 return ET->getDecl()->isDefinition(); 496 if (isa<FixedWidthIntType>(CanonicalType)) 497 return true; 498 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); 499} 500 501bool Type::isScalarType() const { 502 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 503 return BT->getKind() != BuiltinType::Void; 504 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { 505 // Enums are scalar types, but only if they are defined. Incomplete enums 506 // are not treated as scalar types. 507 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 508 return true; 509 return false; 510 } 511 if (isa<FixedWidthIntType>(CanonicalType)) 512 return true; 513 return isa<PointerType>(CanonicalType) || 514 isa<BlockPointerType>(CanonicalType) || 515 isa<MemberPointerType>(CanonicalType) || 516 isa<ComplexType>(CanonicalType) || 517 isa<ObjCObjectPointerType>(CanonicalType); 518} 519 520/// \brief Determines whether the type is a C++ aggregate type or C 521/// aggregate or union type. 522/// 523/// An aggregate type is an array or a class type (struct, union, or 524/// class) that has no user-declared constructors, no private or 525/// protected non-static data members, no base classes, and no virtual 526/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type 527/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also 528/// includes union types. 529bool Type::isAggregateType() const { 530 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { 531 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) 532 return ClassDecl->isAggregate(); 533 534 return true; 535 } 536 537 return isa<ArrayType>(CanonicalType); 538} 539 540/// isConstantSizeType - Return true if this is not a variable sized type, 541/// according to the rules of C99 6.7.5p3. It is not legal to call this on 542/// incomplete types or dependent types. 543bool Type::isConstantSizeType() const { 544 assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); 545 assert(!isDependentType() && "This doesn't make sense for dependent types"); 546 // The VAT must have a size, as it is known to be complete. 547 return !isa<VariableArrayType>(CanonicalType); 548} 549 550/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) 551/// - a type that can describe objects, but which lacks information needed to 552/// determine its size. 553bool Type::isIncompleteType() const { 554 switch (CanonicalType->getTypeClass()) { 555 default: return false; 556 case Builtin: 557 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never 558 // be completed. 559 return isVoidType(); 560 case Record: 561 case Enum: 562 // A tagged type (struct/union/enum/class) is incomplete if the decl is a 563 // forward declaration, but not a full definition (C99 6.2.5p22). 564 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); 565 case IncompleteArray: 566 // An array of unknown size is an incomplete type (C99 6.2.5p22). 567 return true; 568 case ObjCInterface: 569 // ObjC interfaces are incomplete if they are @class, not @interface. 570 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl(); 571 } 572} 573 574/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) 575bool Type::isPODType() const { 576 // The compiler shouldn't query this for incomplete types, but the user might. 577 // We return false for that case. 578 if (isIncompleteType()) 579 return false; 580 581 switch (CanonicalType->getTypeClass()) { 582 // Everything not explicitly mentioned is not POD. 583 default: return false; 584 case VariableArray: 585 case ConstantArray: 586 // IncompleteArray is caught by isIncompleteType() above. 587 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType(); 588 589 case Builtin: 590 case Complex: 591 case Pointer: 592 case MemberPointer: 593 case Vector: 594 case ExtVector: 595 case ObjCObjectPointer: 596 return true; 597 598 case Enum: 599 return true; 600 601 case Record: 602 if (CXXRecordDecl *ClassDecl 603 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) 604 return ClassDecl->isPOD(); 605 606 // C struct/union is POD. 607 return true; 608 } 609} 610 611bool Type::isPromotableIntegerType() const { 612 if (const BuiltinType *BT = getAs<BuiltinType>()) 613 switch (BT->getKind()) { 614 case BuiltinType::Bool: 615 case BuiltinType::Char_S: 616 case BuiltinType::Char_U: 617 case BuiltinType::SChar: 618 case BuiltinType::UChar: 619 case BuiltinType::Short: 620 case BuiltinType::UShort: 621 return true; 622 default: 623 return false; 624 } 625 return false; 626} 627 628bool Type::isNullPtrType() const { 629 if (const BuiltinType *BT = getAs<BuiltinType>()) 630 return BT->getKind() == BuiltinType::NullPtr; 631 return false; 632} 633 634bool Type::isSpecifierType() const { 635 // Note that this intentionally does not use the canonical type. 636 switch (getTypeClass()) { 637 case Builtin: 638 case Record: 639 case Enum: 640 case Typedef: 641 case Complex: 642 case TypeOfExpr: 643 case TypeOf: 644 case TemplateTypeParm: 645 case TemplateSpecialization: 646 case QualifiedName: 647 case Typename: 648 case ObjCInterface: 649 case ObjCObjectPointer: 650 return true; 651 default: 652 return false; 653 } 654} 655 656const char *Type::getTypeClassName() const { 657 switch (TC) { 658 default: assert(0 && "Type class not in TypeNodes.def!"); 659#define ABSTRACT_TYPE(Derived, Base) 660#define TYPE(Derived, Base) case Derived: return #Derived; 661#include "clang/AST/TypeNodes.def" 662 } 663} 664 665const char *BuiltinType::getName(const LangOptions &LO) const { 666 switch (getKind()) { 667 default: assert(0 && "Unknown builtin type!"); 668 case Void: return "void"; 669 case Bool: return LO.Bool ? "bool" : "_Bool"; 670 case Char_S: return "char"; 671 case Char_U: return "char"; 672 case SChar: return "signed char"; 673 case Short: return "short"; 674 case Int: return "int"; 675 case Long: return "long"; 676 case LongLong: return "long long"; 677 case Int128: return "__int128_t"; 678 case UChar: return "unsigned char"; 679 case UShort: return "unsigned short"; 680 case UInt: return "unsigned int"; 681 case ULong: return "unsigned long"; 682 case ULongLong: return "unsigned long long"; 683 case UInt128: return "__uint128_t"; 684 case Float: return "float"; 685 case Double: return "double"; 686 case LongDouble: return "long double"; 687 case WChar: return "wchar_t"; 688 case Char16: return "char16_t"; 689 case Char32: return "char32_t"; 690 case NullPtr: return "nullptr_t"; 691 case Overload: return "<overloaded function type>"; 692 case Dependent: return "<dependent type>"; 693 case UndeducedAuto: return "auto"; 694 case ObjCId: return "id"; 695 case ObjCClass: return "Class"; 696 } 697} 698 699void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 700 arg_type_iterator ArgTys, 701 unsigned NumArgs, bool isVariadic, 702 unsigned TypeQuals, bool hasExceptionSpec, 703 bool anyExceptionSpec, unsigned NumExceptions, 704 exception_iterator Exs, bool NoReturn) { 705 ID.AddPointer(Result.getAsOpaquePtr()); 706 for (unsigned i = 0; i != NumArgs; ++i) 707 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 708 ID.AddInteger(isVariadic); 709 ID.AddInteger(TypeQuals); 710 ID.AddInteger(hasExceptionSpec); 711 if (hasExceptionSpec) { 712 ID.AddInteger(anyExceptionSpec); 713 for (unsigned i = 0; i != NumExceptions; ++i) 714 ID.AddPointer(Exs[i].getAsOpaquePtr()); 715 } 716 ID.AddInteger(NoReturn); 717} 718 719void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { 720 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), 721 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), 722 getNumExceptions(), exception_begin(), getNoReturnAttr()); 723} 724 725void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID, 726 QualType OIT, ObjCProtocolDecl **protocols, 727 unsigned NumProtocols) { 728 ID.AddPointer(OIT.getAsOpaquePtr()); 729 for (unsigned i = 0; i != NumProtocols; i++) 730 ID.AddPointer(protocols[i]); 731} 732 733void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) { 734 if (getNumProtocols()) 735 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols()); 736 else 737 Profile(ID, getPointeeType(), 0, 0); 738} 739 740void ObjCProtocolListType::Profile(llvm::FoldingSetNodeID &ID, 741 QualType OIT, ObjCProtocolDecl **protocols, 742 unsigned NumProtocols) { 743 ID.AddPointer(OIT.getAsOpaquePtr()); 744 for (unsigned i = 0; i != NumProtocols; i++) 745 ID.AddPointer(protocols[i]); 746} 747 748void ObjCProtocolListType::Profile(llvm::FoldingSetNodeID &ID) { 749 Profile(ID, getBaseType(), &Protocols[0], getNumProtocols()); 750} 751 752/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 753/// potentially looking through *all* consequtive typedefs. This returns the 754/// sum of the type qualifiers, so if you have: 755/// typedef const int A; 756/// typedef volatile A B; 757/// looking through the typedefs for B will give you "const volatile A". 758/// 759QualType TypedefType::LookThroughTypedefs() const { 760 // Usually, there is only a single level of typedefs, be fast in that case. 761 QualType FirstType = getDecl()->getUnderlyingType(); 762 if (!isa<TypedefType>(FirstType)) 763 return FirstType; 764 765 // Otherwise, do the fully general loop. 766 QualifierCollector Qs; 767 768 QualType CurType; 769 const TypedefType *TDT = this; 770 do { 771 CurType = TDT->getDecl()->getUnderlyingType(); 772 TDT = dyn_cast<TypedefType>(Qs.strip(CurType)); 773 } while (TDT); 774 775 return Qs.apply(CurType); 776} 777 778QualType TypedefType::desugar() const { 779 return getDecl()->getUnderlyingType(); 780} 781 782TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 783 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { 784} 785 786QualType TypeOfExprType::desugar() const { 787 return getUnderlyingExpr()->getType(); 788} 789 790void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, 791 ASTContext &Context, Expr *E) { 792 E->Profile(ID, Context, true); 793} 794 795DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) 796 : Type(Decltype, can, E->isTypeDependent()), E(E), 797 UnderlyingType(underlyingType) { 798} 799 800DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) 801 : DecltypeType(E, Context.DependentTy), Context(Context) { } 802 803void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, 804 ASTContext &Context, Expr *E) { 805 E->Profile(ID, Context, true); 806} 807 808TagType::TagType(TypeClass TC, TagDecl *D, QualType can) 809 : Type(TC, can, D->isDependentType()), decl(D, 0) {} 810 811bool RecordType::classof(const TagType *TT) { 812 return isa<RecordDecl>(TT->getDecl()); 813} 814 815bool EnumType::classof(const TagType *TT) { 816 return isa<EnumDecl>(TT->getDecl()); 817} 818 819bool 820TemplateSpecializationType:: 821anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) { 822 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) { 823 switch (Args[Idx].getKind()) { 824 case TemplateArgument::Null: 825 assert(false && "Should not have a NULL template argument"); 826 break; 827 828 case TemplateArgument::Type: 829 if (Args[Idx].getAsType()->isDependentType()) 830 return true; 831 break; 832 833 case TemplateArgument::Declaration: 834 case TemplateArgument::Integral: 835 // Never dependent 836 break; 837 838 case TemplateArgument::Expression: 839 if (Args[Idx].getAsExpr()->isTypeDependent() || 840 Args[Idx].getAsExpr()->isValueDependent()) 841 return true; 842 break; 843 844 case TemplateArgument::Pack: 845 assert(0 && "FIXME: Implement!"); 846 break; 847 } 848 } 849 850 return false; 851} 852 853TemplateSpecializationType:: 854TemplateSpecializationType(ASTContext &Context, TemplateName T, 855 const TemplateArgument *Args, 856 unsigned NumArgs, QualType Canon) 857 : Type(TemplateSpecialization, 858 Canon.isNull()? QualType(this, 0) : Canon, 859 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), 860 Context(Context), 861 Template(T), NumArgs(NumArgs) { 862 assert((!Canon.isNull() || 863 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && 864 "No canonical type for non-dependent class template specialization"); 865 866 TemplateArgument *TemplateArgs 867 = reinterpret_cast<TemplateArgument *>(this + 1); 868 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) 869 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); 870} 871 872void TemplateSpecializationType::Destroy(ASTContext& C) { 873 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 874 // FIXME: Not all expressions get cloned, so we can't yet perform 875 // this destruction. 876 // if (Expr *E = getArg(Arg).getAsExpr()) 877 // E->Destroy(C); 878 } 879} 880 881TemplateSpecializationType::iterator 882TemplateSpecializationType::end() const { 883 return begin() + getNumArgs(); 884} 885 886const TemplateArgument & 887TemplateSpecializationType::getArg(unsigned Idx) const { 888 assert(Idx < getNumArgs() && "Template argument out of range"); 889 return getArgs()[Idx]; 890} 891 892void 893TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 894 TemplateName T, 895 const TemplateArgument *Args, 896 unsigned NumArgs, 897 ASTContext &Context) { 898 T.Profile(ID); 899 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 900 Args[Idx].Profile(ID, Context); 901} 902 903QualType QualifierCollector::apply(QualType QT) const { 904 if (!hasNonFastQualifiers()) 905 return QT.withFastQualifiers(getFastQualifiers()); 906 907 assert(Context && "extended qualifiers but no context!"); 908 return Context->getQualifiedType(QT, *this); 909} 910 911QualType QualifierCollector::apply(const Type *T) const { 912 if (!hasNonFastQualifiers()) 913 return QualType(T, getFastQualifiers()); 914 915 assert(Context && "extended qualifiers but no context!"); 916 return Context->getQualifiedType(T, *this); 917} 918 919 920//===----------------------------------------------------------------------===// 921// Type Printing 922//===----------------------------------------------------------------------===// 923 924void QualType::dump(const char *msg) const { 925 std::string R = "identifier"; 926 LangOptions LO; 927 getAsStringInternal(R, PrintingPolicy(LO)); 928 if (msg) 929 fprintf(stderr, "%s: %s\n", msg, R.c_str()); 930 else 931 fprintf(stderr, "%s\n", R.c_str()); 932} 933void QualType::dump() const { 934 dump(""); 935} 936 937void Type::dump() const { 938 std::string S = "identifier"; 939 LangOptions LO; 940 getAsStringInternal(S, PrintingPolicy(LO)); 941 fprintf(stderr, "%s\n", S.c_str()); 942} 943 944 945 946static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { 947 if (TypeQuals & Qualifiers::Const) { 948 if (!S.empty()) S += ' '; 949 S += "const"; 950 } 951 if (TypeQuals & Qualifiers::Volatile) { 952 if (!S.empty()) S += ' '; 953 S += "volatile"; 954 } 955 if (TypeQuals & Qualifiers::Restrict) { 956 if (!S.empty()) S += ' '; 957 S += "restrict"; 958 } 959} 960 961std::string Qualifiers::getAsString() const { 962 LangOptions LO; 963 return getAsString(PrintingPolicy(LO)); 964} 965 966// Appends qualifiers to the given string, separated by spaces. Will 967// prefix a space if the string is non-empty. Will not append a final 968// space. 969void Qualifiers::getAsStringInternal(std::string &S, 970 const PrintingPolicy&) const { 971 AppendTypeQualList(S, getCVRQualifiers()); 972 if (unsigned AddressSpace = getAddressSpace()) { 973 if (!S.empty()) S += ' '; 974 S += "__attribute__((address_space("; 975 S += llvm::utostr_32(AddressSpace); 976 S += ")))"; 977 } 978 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) { 979 if (!S.empty()) S += ' '; 980 S += "__attribute__((objc_gc("; 981 if (GCAttrType == Qualifiers::Weak) 982 S += "weak"; 983 else 984 S += "strong"; 985 S += ")))"; 986 } 987} 988 989std::string QualType::getAsString() const { 990 std::string S; 991 LangOptions LO; 992 getAsStringInternal(S, PrintingPolicy(LO)); 993 return S; 994} 995 996void 997QualType::getAsStringInternal(std::string &S, 998 const PrintingPolicy &Policy) const { 999 if (isNull()) { 1000 S += "NULL TYPE"; 1001 return; 1002 } 1003 1004 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType()) 1005 return; 1006 1007 // Print qualifiers as appropriate. 1008 Qualifiers Quals = getQualifiers(); 1009 if (!Quals.empty()) { 1010 std::string TQS; 1011 Quals.getAsStringInternal(TQS, Policy); 1012 1013 if (!S.empty()) { 1014 TQS += ' '; 1015 TQS += S; 1016 } 1017 std::swap(S, TQS); 1018 } 1019 1020 getTypePtr()->getAsStringInternal(S, Policy); 1021} 1022 1023void BuiltinType::getAsStringInternal(std::string &S, 1024 const PrintingPolicy &Policy) const { 1025 if (S.empty()) { 1026 S = getName(Policy.LangOpts); 1027 } else { 1028 // Prefix the basic type, e.g. 'int X'. 1029 S = ' ' + S; 1030 S = getName(Policy.LangOpts) + S; 1031 } 1032} 1033 1034void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1035 // FIXME: Once we get bitwidth attribute, write as 1036 // "int __attribute__((bitwidth(x)))". 1037 std::string prefix = "__clang_fixedwidth"; 1038 prefix += llvm::utostr_32(Width); 1039 prefix += (char)(Signed ? 'S' : 'U'); 1040 if (S.empty()) { 1041 S = prefix; 1042 } else { 1043 // Prefix the basic type, e.g. 'int X'. 1044 S = prefix + S; 1045 } 1046} 1047 1048 1049void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1050 ElementType->getAsStringInternal(S, Policy); 1051 S = "_Complex " + S; 1052} 1053 1054void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1055 S = '*' + S; 1056 1057 // Handle things like 'int (*A)[4];' correctly. 1058 // FIXME: this should include vectors, but vectors use attributes I guess. 1059 if (isa<ArrayType>(getPointeeType())) 1060 S = '(' + S + ')'; 1061 1062 getPointeeType().getAsStringInternal(S, Policy); 1063} 1064 1065void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1066 S = '^' + S; 1067 PointeeType.getAsStringInternal(S, Policy); 1068} 1069 1070void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1071 S = '&' + S; 1072 1073 // Handle things like 'int (&A)[4];' correctly. 1074 // FIXME: this should include vectors, but vectors use attributes I guess. 1075 if (isa<ArrayType>(getPointeeType())) 1076 S = '(' + S + ')'; 1077 1078 getPointeeType().getAsStringInternal(S, Policy); 1079} 1080 1081void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1082 S = "&&" + S; 1083 1084 // Handle things like 'int (&&A)[4];' correctly. 1085 // FIXME: this should include vectors, but vectors use attributes I guess. 1086 if (isa<ArrayType>(getPointeeType())) 1087 S = '(' + S + ')'; 1088 1089 getPointeeType().getAsStringInternal(S, Policy); 1090} 1091 1092void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1093 std::string C; 1094 Class->getAsStringInternal(C, Policy); 1095 C += "::*"; 1096 S = C + S; 1097 1098 // Handle things like 'int (Cls::*A)[4];' correctly. 1099 // FIXME: this should include vectors, but vectors use attributes I guess. 1100 if (isa<ArrayType>(getPointeeType())) 1101 S = '(' + S + ')'; 1102 1103 getPointeeType().getAsStringInternal(S, Policy); 1104} 1105 1106void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1107 S += '['; 1108 S += llvm::utostr(getSize().getZExtValue()); 1109 S += ']'; 1110 1111 getElementType().getAsStringInternal(S, Policy); 1112} 1113 1114void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1115 if (Policy.ConstantArraySizeAsWritten) { 1116 std::string SStr; 1117 llvm::raw_string_ostream s(SStr); 1118 getSizeExpr()->printPretty(s, 0, Policy); 1119 S += '['; 1120 S += s.str(); 1121 S += ']'; 1122 getElementType().getAsStringInternal(S, Policy); 1123 } 1124 else 1125 ConstantArrayType::getAsStringInternal(S, Policy); 1126} 1127 1128void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1129 if (Policy.ConstantArraySizeAsWritten) { 1130 S += "[]"; 1131 getElementType().getAsStringInternal(S, Policy); 1132 } 1133 else 1134 ConstantArrayType::getAsStringInternal(S, Policy); 1135} 1136 1137void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1138 S += "[]"; 1139 1140 getElementType().getAsStringInternal(S, Policy); 1141} 1142 1143void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1144 S += '['; 1145 1146 if (getIndexTypeQualifiers().hasQualifiers()) { 1147 AppendTypeQualList(S, getIndexTypeCVRQualifiers()); 1148 S += ' '; 1149 } 1150 1151 if (getSizeModifier() == Static) 1152 S += "static"; 1153 else if (getSizeModifier() == Star) 1154 S += '*'; 1155 1156 if (getSizeExpr()) { 1157 std::string SStr; 1158 llvm::raw_string_ostream s(SStr); 1159 getSizeExpr()->printPretty(s, 0, Policy); 1160 S += s.str(); 1161 } 1162 S += ']'; 1163 1164 getElementType().getAsStringInternal(S, Policy); 1165} 1166 1167void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1168 S += '['; 1169 1170 if (getIndexTypeQualifiers().hasQualifiers()) { 1171 AppendTypeQualList(S, getIndexTypeCVRQualifiers()); 1172 S += ' '; 1173 } 1174 1175 if (getSizeModifier() == Static) 1176 S += "static"; 1177 else if (getSizeModifier() == Star) 1178 S += '*'; 1179 1180 if (getSizeExpr()) { 1181 std::string SStr; 1182 llvm::raw_string_ostream s(SStr); 1183 getSizeExpr()->printPretty(s, 0, Policy); 1184 S += s.str(); 1185 } 1186 S += ']'; 1187 1188 getElementType().getAsStringInternal(S, Policy); 1189} 1190 1191void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1192 getElementType().getAsStringInternal(S, Policy); 1193 1194 S += " __attribute__((ext_vector_type("; 1195 if (getSizeExpr()) { 1196 std::string SStr; 1197 llvm::raw_string_ostream s(SStr); 1198 getSizeExpr()->printPretty(s, 0, Policy); 1199 S += s.str(); 1200 } 1201 S += ")))"; 1202} 1203 1204void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1205 // FIXME: We prefer to print the size directly here, but have no way 1206 // to get the size of the type. 1207 S += " __attribute__((__vector_size__("; 1208 S += llvm::utostr_32(NumElements); // convert back to bytes. 1209 S += " * sizeof(" + ElementType.getAsString() + "))))"; 1210 ElementType.getAsStringInternal(S, Policy); 1211} 1212 1213void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1214 S += " __attribute__((ext_vector_type("; 1215 S += llvm::utostr_32(NumElements); 1216 S += ")))"; 1217 ElementType.getAsStringInternal(S, Policy); 1218} 1219 1220void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1221 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. 1222 InnerString = ' ' + InnerString; 1223 std::string Str; 1224 llvm::raw_string_ostream s(Str); 1225 getUnderlyingExpr()->printPretty(s, 0, Policy); 1226 InnerString = "typeof " + s.str() + InnerString; 1227} 1228 1229void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1230 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. 1231 InnerString = ' ' + InnerString; 1232 std::string Tmp; 1233 getUnderlyingType().getAsStringInternal(Tmp, Policy); 1234 InnerString = "typeof(" + Tmp + ")" + InnerString; 1235} 1236 1237void DecltypeType::getAsStringInternal(std::string &InnerString, 1238 const PrintingPolicy &Policy) const { 1239 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'. 1240 InnerString = ' ' + InnerString; 1241 std::string Str; 1242 llvm::raw_string_ostream s(Str); 1243 getUnderlyingExpr()->printPretty(s, 0, Policy); 1244 InnerString = "decltype(" + s.str() + ")" + InnerString; 1245} 1246 1247void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1248 // If needed for precedence reasons, wrap the inner part in grouping parens. 1249 if (!S.empty()) 1250 S = "(" + S + ")"; 1251 1252 S += "()"; 1253 if (getNoReturnAttr()) 1254 S += " __attribute__((noreturn))"; 1255 getResultType().getAsStringInternal(S, Policy); 1256} 1257 1258void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1259 // If needed for precedence reasons, wrap the inner part in grouping parens. 1260 if (!S.empty()) 1261 S = "(" + S + ")"; 1262 1263 S += "("; 1264 std::string Tmp; 1265 PrintingPolicy ParamPolicy(Policy); 1266 ParamPolicy.SuppressSpecifiers = false; 1267 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { 1268 if (i) S += ", "; 1269 getArgType(i).getAsStringInternal(Tmp, ParamPolicy); 1270 S += Tmp; 1271 Tmp.clear(); 1272 } 1273 1274 if (isVariadic()) { 1275 if (getNumArgs()) 1276 S += ", "; 1277 S += "..."; 1278 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) { 1279 // Do not emit int() if we have a proto, emit 'int(void)'. 1280 S += "void"; 1281 } 1282 1283 S += ")"; 1284 if (getNoReturnAttr()) 1285 S += " __attribute__((noreturn))"; 1286 getResultType().getAsStringInternal(S, Policy); 1287} 1288 1289 1290void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1291 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1292 InnerString = ' ' + InnerString; 1293 InnerString = getDecl()->getIdentifier()->getName() + InnerString; 1294} 1295 1296void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1297 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'. 1298 InnerString = ' ' + InnerString; 1299 1300 if (!Name) 1301 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' + 1302 llvm::utostr_32(Index) + InnerString; 1303 else 1304 InnerString = Name->getName() + InnerString; 1305} 1306 1307std::string 1308TemplateSpecializationType::PrintTemplateArgumentList( 1309 const TemplateArgument *Args, 1310 unsigned NumArgs, 1311 const PrintingPolicy &Policy) { 1312 std::string SpecString; 1313 SpecString += '<'; 1314 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1315 if (Arg) 1316 SpecString += ", "; 1317 1318 // Print the argument into a string. 1319 std::string ArgString; 1320 switch (Args[Arg].getKind()) { 1321 case TemplateArgument::Null: 1322 assert(false && "Null template argument"); 1323 break; 1324 1325 case TemplateArgument::Type: 1326 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy); 1327 break; 1328 1329 case TemplateArgument::Declaration: 1330 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString(); 1331 break; 1332 1333 case TemplateArgument::Integral: 1334 ArgString = Args[Arg].getAsIntegral()->toString(10, true); 1335 break; 1336 1337 case TemplateArgument::Expression: { 1338 llvm::raw_string_ostream s(ArgString); 1339 Args[Arg].getAsExpr()->printPretty(s, 0, Policy); 1340 break; 1341 } 1342 case TemplateArgument::Pack: 1343 assert(0 && "FIXME: Implement!"); 1344 break; 1345 } 1346 1347 // If this is the first argument and its string representation 1348 // begins with the global scope specifier ('::foo'), add a space 1349 // to avoid printing the diagraph '<:'. 1350 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1351 SpecString += ' '; 1352 1353 SpecString += ArgString; 1354 } 1355 1356 // If the last character of our string is '>', add another space to 1357 // keep the two '>''s separate tokens. We don't *have* to do this in 1358 // C++0x, but it's still good hygiene. 1359 if (SpecString[SpecString.size() - 1] == '>') 1360 SpecString += ' '; 1361 1362 SpecString += '>'; 1363 1364 return SpecString; 1365} 1366 1367void 1368TemplateSpecializationType:: 1369getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1370 std::string SpecString; 1371 1372 { 1373 llvm::raw_string_ostream OS(SpecString); 1374 Template.print(OS, Policy); 1375 } 1376 1377 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy); 1378 if (InnerString.empty()) 1379 InnerString.swap(SpecString); 1380 else 1381 InnerString = SpecString + ' ' + InnerString; 1382} 1383 1384void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1385 std::string MyString; 1386 1387 { 1388 llvm::raw_string_ostream OS(MyString); 1389 NNS->print(OS, Policy); 1390 } 1391 1392 std::string TypeStr; 1393 PrintingPolicy InnerPolicy(Policy); 1394 InnerPolicy.SuppressTagKind = true; 1395 InnerPolicy.SuppressScope = true; 1396 NamedType.getAsStringInternal(TypeStr, InnerPolicy); 1397 1398 MyString += TypeStr; 1399 if (InnerString.empty()) 1400 InnerString.swap(MyString); 1401 else 1402 InnerString = MyString + ' ' + InnerString; 1403} 1404 1405void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1406 std::string MyString; 1407 1408 { 1409 llvm::raw_string_ostream OS(MyString); 1410 OS << "typename "; 1411 NNS->print(OS, Policy); 1412 1413 if (const IdentifierInfo *Ident = getIdentifier()) 1414 OS << Ident->getName(); 1415 else if (const TemplateSpecializationType *Spec = getTemplateId()) { 1416 Spec->getTemplateName().print(OS, Policy, true); 1417 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1418 Spec->getArgs(), 1419 Spec->getNumArgs(), 1420 Policy); 1421 } 1422 } 1423 1424 if (InnerString.empty()) 1425 InnerString.swap(MyString); 1426 else 1427 InnerString = MyString + ' ' + InnerString; 1428} 1429 1430void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID, 1431 const ObjCInterfaceDecl *Decl, 1432 ObjCProtocolDecl **protocols, 1433 unsigned NumProtocols) { 1434 ID.AddPointer(Decl); 1435 for (unsigned i = 0; i != NumProtocols; i++) 1436 ID.AddPointer(protocols[i]); 1437} 1438 1439void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { 1440 if (getNumProtocols()) 1441 Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); 1442 else 1443 Profile(ID, getDecl(), 0, 0); 1444} 1445 1446void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, 1447 const PrintingPolicy &Policy) const { 1448 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1449 InnerString = ' ' + InnerString; 1450 1451 std::string ObjCQIString = getDecl()->getNameAsString(); 1452 if (getNumProtocols()) { 1453 ObjCQIString += '<'; 1454 bool isFirst = true; 1455 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1456 if (isFirst) 1457 isFirst = false; 1458 else 1459 ObjCQIString += ','; 1460 ObjCQIString += (*I)->getNameAsString(); 1461 } 1462 ObjCQIString += '>'; 1463 } 1464 InnerString = ObjCQIString + InnerString; 1465} 1466 1467void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString, 1468 const PrintingPolicy &Policy) const { 1469 std::string ObjCQIString; 1470 1471 if (isObjCIdType() || isObjCQualifiedIdType()) 1472 ObjCQIString = "id"; 1473 else if (isObjCClassType() || isObjCQualifiedClassType()) 1474 ObjCQIString = "Class"; 1475 else 1476 ObjCQIString = getInterfaceDecl()->getNameAsString(); 1477 1478 if (!qual_empty()) { 1479 ObjCQIString += '<'; 1480 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1481 ObjCQIString += (*I)->getNameAsString(); 1482 if (I+1 != E) 1483 ObjCQIString += ','; 1484 } 1485 ObjCQIString += '>'; 1486 } 1487 1488 PointeeType.getQualifiers().getAsStringInternal(ObjCQIString, Policy); 1489 1490 if (!isObjCIdType() && !isObjCQualifiedIdType()) 1491 ObjCQIString += " *"; // Don't forget the implicit pointer. 1492 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1493 InnerString = ' ' + InnerString; 1494 1495 InnerString = ObjCQIString + InnerString; 1496} 1497 1498void ObjCProtocolListType::getAsStringInternal(std::string &InnerString, 1499 const PrintingPolicy &Policy) const { 1500 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1501 InnerString = ' ' + InnerString; 1502 1503 std::string ObjCQIString = getBaseType().getAsString(Policy); 1504 ObjCQIString += '<'; 1505 bool isFirst = true; 1506 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1507 if (isFirst) 1508 isFirst = false; 1509 else 1510 ObjCQIString += ','; 1511 ObjCQIString += (*I)->getNameAsString(); 1512 } 1513 ObjCQIString += '>'; 1514 InnerString = ObjCQIString + InnerString; 1515} 1516 1517void ElaboratedType::getAsStringInternal(std::string &InnerString, 1518 const PrintingPolicy &Policy) const { 1519 std::string TypeStr; 1520 PrintingPolicy InnerPolicy(Policy); 1521 InnerPolicy.SuppressTagKind = true; 1522 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy); 1523 1524 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString; 1525} 1526 1527void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1528 if (Policy.SuppressTag) 1529 return; 1530 1531 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1532 InnerString = ' ' + InnerString; 1533 1534 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName(); 1535 const char *ID; 1536 if (const IdentifierInfo *II = getDecl()->getIdentifier()) 1537 ID = II->getName(); 1538 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) { 1539 Kind = 0; 1540 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 1541 ID = Typedef->getIdentifier()->getName(); 1542 } else 1543 ID = "<anonymous>"; 1544 1545 // If this is a class template specialization, print the template 1546 // arguments. 1547 if (ClassTemplateSpecializationDecl *Spec 1548 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) { 1549 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1550 std::string TemplateArgsStr 1551 = TemplateSpecializationType::PrintTemplateArgumentList( 1552 TemplateArgs.getFlatArgumentList(), 1553 TemplateArgs.flat_size(), 1554 Policy); 1555 InnerString = TemplateArgsStr + InnerString; 1556 } 1557 1558 if (!Policy.SuppressScope) { 1559 // Compute the full nested-name-specifier for this type. In C, 1560 // this will always be empty. 1561 std::string ContextStr; 1562 for (DeclContext *DC = getDecl()->getDeclContext(); 1563 !DC->isTranslationUnit(); DC = DC->getParent()) { 1564 std::string MyPart; 1565 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 1566 if (NS->getIdentifier()) 1567 MyPart = NS->getNameAsString(); 1568 } else if (ClassTemplateSpecializationDecl *Spec 1569 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 1570 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1571 std::string TemplateArgsStr 1572 = TemplateSpecializationType::PrintTemplateArgumentList( 1573 TemplateArgs.getFlatArgumentList(), 1574 TemplateArgs.flat_size(), 1575 Policy); 1576 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr; 1577 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 1578 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl()) 1579 MyPart = Typedef->getIdentifier()->getName(); 1580 else if (Tag->getIdentifier()) 1581 MyPart = Tag->getIdentifier()->getName(); 1582 } 1583 1584 if (!MyPart.empty()) 1585 ContextStr = MyPart + "::" + ContextStr; 1586 } 1587 1588 if (Kind) 1589 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString; 1590 else 1591 InnerString = ContextStr + ID + InnerString; 1592 } else 1593 InnerString = ID + InnerString; 1594} 1595