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