Type.cpp revision 46a617a792bfab0d9b1e057371ea3b9540802226
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 TemplateSpecialization: 632 case QualifiedName: 633 case Typename: 634 case ObjCInterface: 635 case ObjCObjectPointer: 636 return true; 637 default: 638 return false; 639 } 640} 641 642const char *Type::getTypeClassName() const { 643 switch (TC) { 644 default: assert(0 && "Type class not in TypeNodes.def!"); 645#define ABSTRACT_TYPE(Derived, Base) 646#define TYPE(Derived, Base) case Derived: return #Derived; 647#include "clang/AST/TypeNodes.def" 648 } 649} 650 651const char *BuiltinType::getName(const LangOptions &LO) const { 652 switch (getKind()) { 653 default: assert(0 && "Unknown builtin type!"); 654 case Void: return "void"; 655 case Bool: return LO.Bool ? "bool" : "_Bool"; 656 case Char_S: return "char"; 657 case Char_U: return "char"; 658 case SChar: return "signed char"; 659 case Short: return "short"; 660 case Int: return "int"; 661 case Long: return "long"; 662 case LongLong: return "long long"; 663 case Int128: return "__int128_t"; 664 case UChar: return "unsigned char"; 665 case UShort: return "unsigned short"; 666 case UInt: return "unsigned int"; 667 case ULong: return "unsigned long"; 668 case ULongLong: return "unsigned long long"; 669 case UInt128: return "__uint128_t"; 670 case Float: return "float"; 671 case Double: return "double"; 672 case LongDouble: return "long double"; 673 case WChar: return "wchar_t"; 674 case Char16: return "char16_t"; 675 case Char32: return "char32_t"; 676 case NullPtr: return "nullptr_t"; 677 case Overload: return "<overloaded function type>"; 678 case Dependent: return "<dependent type>"; 679 case UndeducedAuto: return "auto"; 680 case ObjCId: return "id"; 681 case ObjCClass: return "Class"; 682 } 683} 684 685void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 686 arg_type_iterator ArgTys, 687 unsigned NumArgs, bool isVariadic, 688 unsigned TypeQuals, bool hasExceptionSpec, 689 bool anyExceptionSpec, unsigned NumExceptions, 690 exception_iterator Exs, bool NoReturn) { 691 ID.AddPointer(Result.getAsOpaquePtr()); 692 for (unsigned i = 0; i != NumArgs; ++i) 693 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 694 ID.AddInteger(isVariadic); 695 ID.AddInteger(TypeQuals); 696 ID.AddInteger(hasExceptionSpec); 697 if (hasExceptionSpec) { 698 ID.AddInteger(anyExceptionSpec); 699 for (unsigned i = 0; i != NumExceptions; ++i) 700 ID.AddPointer(Exs[i].getAsOpaquePtr()); 701 } 702 ID.AddInteger(NoReturn); 703} 704 705void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { 706 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), 707 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), 708 getNumExceptions(), exception_begin(), getNoReturnAttr()); 709} 710 711void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID, 712 QualType OIT, ObjCProtocolDecl **protocols, 713 unsigned NumProtocols) { 714 ID.AddPointer(OIT.getAsOpaquePtr()); 715 for (unsigned i = 0; i != NumProtocols; i++) 716 ID.AddPointer(protocols[i]); 717} 718 719void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) { 720 if (getNumProtocols()) 721 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols()); 722 else 723 Profile(ID, getPointeeType(), 0, 0); 724} 725 726void ObjCProtocolListType::Profile(llvm::FoldingSetNodeID &ID, 727 QualType OIT, ObjCProtocolDecl **protocols, 728 unsigned NumProtocols) { 729 ID.AddPointer(OIT.getAsOpaquePtr()); 730 for (unsigned i = 0; i != NumProtocols; i++) 731 ID.AddPointer(protocols[i]); 732} 733 734void ObjCProtocolListType::Profile(llvm::FoldingSetNodeID &ID) { 735 Profile(ID, getBaseType(), &Protocols[0], getNumProtocols()); 736} 737 738/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 739/// potentially looking through *all* consequtive typedefs. This returns the 740/// sum of the type qualifiers, so if you have: 741/// typedef const int A; 742/// typedef volatile A B; 743/// looking through the typedefs for B will give you "const volatile A". 744/// 745QualType TypedefType::LookThroughTypedefs() const { 746 // Usually, there is only a single level of typedefs, be fast in that case. 747 QualType FirstType = getDecl()->getUnderlyingType(); 748 if (!isa<TypedefType>(FirstType)) 749 return FirstType; 750 751 // Otherwise, do the fully general loop. 752 QualifierCollector Qs; 753 754 QualType CurType; 755 const TypedefType *TDT = this; 756 do { 757 CurType = TDT->getDecl()->getUnderlyingType(); 758 TDT = dyn_cast<TypedefType>(Qs.strip(CurType)); 759 } while (TDT); 760 761 return Qs.apply(CurType); 762} 763 764QualType TypedefType::desugar() const { 765 return getDecl()->getUnderlyingType(); 766} 767 768TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 769 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { 770} 771 772QualType TypeOfExprType::desugar() const { 773 return getUnderlyingExpr()->getType(); 774} 775 776void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, 777 ASTContext &Context, Expr *E) { 778 E->Profile(ID, Context, true); 779} 780 781DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) 782 : Type(Decltype, can, E->isTypeDependent()), E(E), 783 UnderlyingType(underlyingType) { 784} 785 786DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) 787 : DecltypeType(E, Context.DependentTy), Context(Context) { } 788 789void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, 790 ASTContext &Context, Expr *E) { 791 E->Profile(ID, Context, true); 792} 793 794TagType::TagType(TypeClass TC, TagDecl *D, QualType can) 795 : Type(TC, can, D->isDependentType()), decl(D, 0) {} 796 797bool RecordType::classof(const TagType *TT) { 798 return isa<RecordDecl>(TT->getDecl()); 799} 800 801bool EnumType::classof(const TagType *TT) { 802 return isa<EnumDecl>(TT->getDecl()); 803} 804 805bool 806TemplateSpecializationType:: 807anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) { 808 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) { 809 switch (Args[Idx].getKind()) { 810 case TemplateArgument::Null: 811 assert(false && "Should not have a NULL template argument"); 812 break; 813 814 case TemplateArgument::Type: 815 if (Args[Idx].getAsType()->isDependentType()) 816 return true; 817 break; 818 819 case TemplateArgument::Declaration: 820 case TemplateArgument::Integral: 821 // Never dependent 822 break; 823 824 case TemplateArgument::Expression: 825 if (Args[Idx].getAsExpr()->isTypeDependent() || 826 Args[Idx].getAsExpr()->isValueDependent()) 827 return true; 828 break; 829 830 case TemplateArgument::Pack: 831 assert(0 && "FIXME: Implement!"); 832 break; 833 } 834 } 835 836 return false; 837} 838 839TemplateSpecializationType:: 840TemplateSpecializationType(ASTContext &Context, TemplateName T, 841 const TemplateArgument *Args, 842 unsigned NumArgs, QualType Canon) 843 : Type(TemplateSpecialization, 844 Canon.isNull()? QualType(this, 0) : Canon, 845 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), 846 Context(Context), 847 Template(T), NumArgs(NumArgs) { 848 assert((!Canon.isNull() || 849 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && 850 "No canonical type for non-dependent class template specialization"); 851 852 TemplateArgument *TemplateArgs 853 = reinterpret_cast<TemplateArgument *>(this + 1); 854 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) 855 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); 856} 857 858void TemplateSpecializationType::Destroy(ASTContext& C) { 859 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 860 // FIXME: Not all expressions get cloned, so we can't yet perform 861 // this destruction. 862 // if (Expr *E = getArg(Arg).getAsExpr()) 863 // E->Destroy(C); 864 } 865} 866 867TemplateSpecializationType::iterator 868TemplateSpecializationType::end() const { 869 return begin() + getNumArgs(); 870} 871 872const TemplateArgument & 873TemplateSpecializationType::getArg(unsigned Idx) const { 874 assert(Idx < getNumArgs() && "Template argument out of range"); 875 return getArgs()[Idx]; 876} 877 878void 879TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 880 TemplateName T, 881 const TemplateArgument *Args, 882 unsigned NumArgs, 883 ASTContext &Context) { 884 T.Profile(ID); 885 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 886 Args[Idx].Profile(ID, Context); 887} 888 889QualType QualifierCollector::apply(QualType QT) const { 890 if (!hasNonFastQualifiers()) 891 return QT.withFastQualifiers(getFastQualifiers()); 892 893 assert(Context && "extended qualifiers but no context!"); 894 return Context->getQualifiedType(QT, *this); 895} 896 897QualType QualifierCollector::apply(const Type *T) const { 898 if (!hasNonFastQualifiers()) 899 return QualType(T, getFastQualifiers()); 900 901 assert(Context && "extended qualifiers but no context!"); 902 return Context->getQualifiedType(T, *this); 903} 904 905 906//===----------------------------------------------------------------------===// 907// Type Printing 908//===----------------------------------------------------------------------===// 909 910void QualType::dump(const char *msg) const { 911 std::string R = "identifier"; 912 LangOptions LO; 913 getAsStringInternal(R, PrintingPolicy(LO)); 914 if (msg) 915 fprintf(stderr, "%s: %s\n", msg, R.c_str()); 916 else 917 fprintf(stderr, "%s\n", R.c_str()); 918} 919void QualType::dump() const { 920 dump(""); 921} 922 923void Type::dump() const { 924 std::string S = "identifier"; 925 LangOptions LO; 926 getAsStringInternal(S, PrintingPolicy(LO)); 927 fprintf(stderr, "%s\n", S.c_str()); 928} 929 930 931 932static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { 933 if (TypeQuals & Qualifiers::Const) { 934 if (!S.empty()) S += ' '; 935 S += "const"; 936 } 937 if (TypeQuals & Qualifiers::Volatile) { 938 if (!S.empty()) S += ' '; 939 S += "volatile"; 940 } 941 if (TypeQuals & Qualifiers::Restrict) { 942 if (!S.empty()) S += ' '; 943 S += "restrict"; 944 } 945} 946 947std::string Qualifiers::getAsString() const { 948 LangOptions LO; 949 return getAsString(PrintingPolicy(LO)); 950} 951 952// Appends qualifiers to the given string, separated by spaces. Will 953// prefix a space if the string is non-empty. Will not append a final 954// space. 955void Qualifiers::getAsStringInternal(std::string &S, 956 const PrintingPolicy&) const { 957 AppendTypeQualList(S, getCVRQualifiers()); 958 if (unsigned AddressSpace = getAddressSpace()) { 959 if (!S.empty()) S += ' '; 960 S += "__attribute__((address_space("; 961 S += llvm::utostr_32(AddressSpace); 962 S += ")))"; 963 } 964 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) { 965 if (!S.empty()) S += ' '; 966 S += "__attribute__((objc_gc("; 967 if (GCAttrType == Qualifiers::Weak) 968 S += "weak"; 969 else 970 S += "strong"; 971 S += ")))"; 972 } 973} 974 975std::string QualType::getAsString() const { 976 std::string S; 977 LangOptions LO; 978 getAsStringInternal(S, PrintingPolicy(LO)); 979 return S; 980} 981 982void 983QualType::getAsStringInternal(std::string &S, 984 const PrintingPolicy &Policy) const { 985 if (isNull()) { 986 S += "NULL TYPE"; 987 return; 988 } 989 990 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType()) 991 return; 992 993 // Print qualifiers as appropriate. 994 Qualifiers Quals = getQualifiers(); 995 if (!Quals.empty()) { 996 std::string TQS; 997 Quals.getAsStringInternal(TQS, Policy); 998 999 if (!S.empty()) { 1000 TQS += ' '; 1001 TQS += S; 1002 } 1003 std::swap(S, TQS); 1004 } 1005 1006 getTypePtr()->getAsStringInternal(S, Policy); 1007} 1008 1009void BuiltinType::getAsStringInternal(std::string &S, 1010 const PrintingPolicy &Policy) const { 1011 if (S.empty()) { 1012 S = getName(Policy.LangOpts); 1013 } else { 1014 // Prefix the basic type, e.g. 'int X'. 1015 S = ' ' + S; 1016 S = getName(Policy.LangOpts) + S; 1017 } 1018} 1019 1020void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1021 // FIXME: Once we get bitwidth attribute, write as 1022 // "int __attribute__((bitwidth(x)))". 1023 std::string prefix = "__clang_fixedwidth"; 1024 prefix += llvm::utostr_32(Width); 1025 prefix += (char)(Signed ? 'S' : 'U'); 1026 if (S.empty()) { 1027 S = prefix; 1028 } else { 1029 // Prefix the basic type, e.g. 'int X'. 1030 S = prefix + S; 1031 } 1032} 1033 1034 1035void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1036 ElementType->getAsStringInternal(S, Policy); 1037 S = "_Complex " + S; 1038} 1039 1040void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1041 S = '*' + S; 1042 1043 // Handle things like 'int (*A)[4];' correctly. 1044 // FIXME: this should include vectors, but vectors use attributes I guess. 1045 if (isa<ArrayType>(getPointeeType())) 1046 S = '(' + S + ')'; 1047 1048 getPointeeType().getAsStringInternal(S, Policy); 1049} 1050 1051void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1052 S = '^' + S; 1053 PointeeType.getAsStringInternal(S, Policy); 1054} 1055 1056void LValueReferenceType::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>(getPointeeType())) 1062 S = '(' + S + ')'; 1063 1064 getPointeeType().getAsStringInternal(S, Policy); 1065} 1066 1067void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1068 S = "&&" + S; 1069 1070 // Handle things like 'int (&&A)[4];' correctly. 1071 // FIXME: this should include vectors, but vectors use attributes I guess. 1072 if (isa<ArrayType>(getPointeeType())) 1073 S = '(' + S + ')'; 1074 1075 getPointeeType().getAsStringInternal(S, Policy); 1076} 1077 1078void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1079 std::string C; 1080 Class->getAsStringInternal(C, Policy); 1081 C += "::*"; 1082 S = C + S; 1083 1084 // Handle things like 'int (Cls::*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 ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1093 S += '['; 1094 S += llvm::utostr(getSize().getZExtValue()); 1095 S += ']'; 1096 1097 getElementType().getAsStringInternal(S, Policy); 1098} 1099 1100void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1101 S += "[]"; 1102 1103 getElementType().getAsStringInternal(S, Policy); 1104} 1105 1106void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1107 S += '['; 1108 1109 if (getIndexTypeQualifiers().hasQualifiers()) { 1110 AppendTypeQualList(S, getIndexTypeCVRQualifiers()); 1111 S += ' '; 1112 } 1113 1114 if (getSizeModifier() == Static) 1115 S += "static"; 1116 else if (getSizeModifier() == Star) 1117 S += '*'; 1118 1119 if (getSizeExpr()) { 1120 std::string SStr; 1121 llvm::raw_string_ostream s(SStr); 1122 getSizeExpr()->printPretty(s, 0, Policy); 1123 S += s.str(); 1124 } 1125 S += ']'; 1126 1127 getElementType().getAsStringInternal(S, Policy); 1128} 1129 1130void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1131 S += '['; 1132 1133 if (getIndexTypeQualifiers().hasQualifiers()) { 1134 AppendTypeQualList(S, getIndexTypeCVRQualifiers()); 1135 S += ' '; 1136 } 1137 1138 if (getSizeModifier() == Static) 1139 S += "static"; 1140 else if (getSizeModifier() == Star) 1141 S += '*'; 1142 1143 if (getSizeExpr()) { 1144 std::string SStr; 1145 llvm::raw_string_ostream s(SStr); 1146 getSizeExpr()->printPretty(s, 0, Policy); 1147 S += s.str(); 1148 } 1149 S += ']'; 1150 1151 getElementType().getAsStringInternal(S, Policy); 1152} 1153 1154void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1155 getElementType().getAsStringInternal(S, Policy); 1156 1157 S += " __attribute__((ext_vector_type("; 1158 if (getSizeExpr()) { 1159 std::string SStr; 1160 llvm::raw_string_ostream s(SStr); 1161 getSizeExpr()->printPretty(s, 0, Policy); 1162 S += s.str(); 1163 } 1164 S += ")))"; 1165} 1166 1167void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1168 // FIXME: We prefer to print the size directly here, but have no way 1169 // to get the size of the type. 1170 S += " __attribute__((__vector_size__("; 1171 S += llvm::utostr_32(NumElements); // convert back to bytes. 1172 S += " * sizeof(" + ElementType.getAsString() + "))))"; 1173 ElementType.getAsStringInternal(S, Policy); 1174} 1175 1176void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1177 S += " __attribute__((ext_vector_type("; 1178 S += llvm::utostr_32(NumElements); 1179 S += ")))"; 1180 ElementType.getAsStringInternal(S, Policy); 1181} 1182 1183void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1184 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. 1185 InnerString = ' ' + InnerString; 1186 std::string Str; 1187 llvm::raw_string_ostream s(Str); 1188 getUnderlyingExpr()->printPretty(s, 0, Policy); 1189 InnerString = "typeof " + s.str() + InnerString; 1190} 1191 1192void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1193 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. 1194 InnerString = ' ' + InnerString; 1195 std::string Tmp; 1196 getUnderlyingType().getAsStringInternal(Tmp, Policy); 1197 InnerString = "typeof(" + Tmp + ")" + InnerString; 1198} 1199 1200void DecltypeType::getAsStringInternal(std::string &InnerString, 1201 const PrintingPolicy &Policy) const { 1202 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'. 1203 InnerString = ' ' + InnerString; 1204 std::string Str; 1205 llvm::raw_string_ostream s(Str); 1206 getUnderlyingExpr()->printPretty(s, 0, Policy); 1207 InnerString = "decltype(" + s.str() + ")" + InnerString; 1208} 1209 1210void FunctionNoProtoType::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 if (getNoReturnAttr()) 1217 S += " __attribute__((noreturn))"; 1218 getResultType().getAsStringInternal(S, Policy); 1219} 1220 1221void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1222 // If needed for precedence reasons, wrap the inner part in grouping parens. 1223 if (!S.empty()) 1224 S = "(" + S + ")"; 1225 1226 S += "("; 1227 std::string Tmp; 1228 PrintingPolicy ParamPolicy(Policy); 1229 ParamPolicy.SuppressSpecifiers = false; 1230 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { 1231 if (i) S += ", "; 1232 getArgType(i).getAsStringInternal(Tmp, ParamPolicy); 1233 S += Tmp; 1234 Tmp.clear(); 1235 } 1236 1237 if (isVariadic()) { 1238 if (getNumArgs()) 1239 S += ", "; 1240 S += "..."; 1241 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) { 1242 // Do not emit int() if we have a proto, emit 'int(void)'. 1243 S += "void"; 1244 } 1245 1246 S += ")"; 1247 if (getNoReturnAttr()) 1248 S += " __attribute__((noreturn))"; 1249 getResultType().getAsStringInternal(S, Policy); 1250} 1251 1252 1253void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1254 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1255 InnerString = ' ' + InnerString; 1256 InnerString = getDecl()->getIdentifier()->getName() + InnerString; 1257} 1258 1259void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1260 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'. 1261 InnerString = ' ' + InnerString; 1262 1263 if (!Name) 1264 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' + 1265 llvm::utostr_32(Index) + InnerString; 1266 else 1267 InnerString = Name->getName() + InnerString; 1268} 1269 1270std::string 1271TemplateSpecializationType::PrintTemplateArgumentList( 1272 const TemplateArgument *Args, 1273 unsigned NumArgs, 1274 const PrintingPolicy &Policy) { 1275 std::string SpecString; 1276 SpecString += '<'; 1277 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1278 if (Arg) 1279 SpecString += ", "; 1280 1281 // Print the argument into a string. 1282 std::string ArgString; 1283 switch (Args[Arg].getKind()) { 1284 case TemplateArgument::Null: 1285 assert(false && "Null template argument"); 1286 break; 1287 1288 case TemplateArgument::Type: 1289 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy); 1290 break; 1291 1292 case TemplateArgument::Declaration: 1293 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString(); 1294 break; 1295 1296 case TemplateArgument::Integral: 1297 ArgString = Args[Arg].getAsIntegral()->toString(10, true); 1298 break; 1299 1300 case TemplateArgument::Expression: { 1301 llvm::raw_string_ostream s(ArgString); 1302 Args[Arg].getAsExpr()->printPretty(s, 0, Policy); 1303 break; 1304 } 1305 case TemplateArgument::Pack: 1306 assert(0 && "FIXME: Implement!"); 1307 break; 1308 } 1309 1310 // If this is the first argument and its string representation 1311 // begins with the global scope specifier ('::foo'), add a space 1312 // to avoid printing the diagraph '<:'. 1313 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1314 SpecString += ' '; 1315 1316 SpecString += ArgString; 1317 } 1318 1319 // If the last character of our string is '>', add another space to 1320 // keep the two '>''s separate tokens. We don't *have* to do this in 1321 // C++0x, but it's still good hygiene. 1322 if (SpecString[SpecString.size() - 1] == '>') 1323 SpecString += ' '; 1324 1325 SpecString += '>'; 1326 1327 return SpecString; 1328} 1329 1330void 1331TemplateSpecializationType:: 1332getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1333 std::string SpecString; 1334 1335 { 1336 llvm::raw_string_ostream OS(SpecString); 1337 Template.print(OS, Policy); 1338 } 1339 1340 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy); 1341 if (InnerString.empty()) 1342 InnerString.swap(SpecString); 1343 else 1344 InnerString = SpecString + ' ' + InnerString; 1345} 1346 1347void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1348 std::string MyString; 1349 1350 { 1351 llvm::raw_string_ostream OS(MyString); 1352 NNS->print(OS, Policy); 1353 } 1354 1355 std::string TypeStr; 1356 PrintingPolicy InnerPolicy(Policy); 1357 InnerPolicy.SuppressTagKind = true; 1358 InnerPolicy.SuppressScope = true; 1359 NamedType.getAsStringInternal(TypeStr, InnerPolicy); 1360 1361 MyString += TypeStr; 1362 if (InnerString.empty()) 1363 InnerString.swap(MyString); 1364 else 1365 InnerString = MyString + ' ' + InnerString; 1366} 1367 1368void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1369 std::string MyString; 1370 1371 { 1372 llvm::raw_string_ostream OS(MyString); 1373 OS << "typename "; 1374 NNS->print(OS, Policy); 1375 1376 if (const IdentifierInfo *Ident = getIdentifier()) 1377 OS << Ident->getName(); 1378 else if (const TemplateSpecializationType *Spec = getTemplateId()) { 1379 Spec->getTemplateName().print(OS, Policy, true); 1380 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1381 Spec->getArgs(), 1382 Spec->getNumArgs(), 1383 Policy); 1384 } 1385 } 1386 1387 if (InnerString.empty()) 1388 InnerString.swap(MyString); 1389 else 1390 InnerString = MyString + ' ' + InnerString; 1391} 1392 1393void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID, 1394 const ObjCInterfaceDecl *Decl, 1395 ObjCProtocolDecl **protocols, 1396 unsigned NumProtocols) { 1397 ID.AddPointer(Decl); 1398 for (unsigned i = 0; i != NumProtocols; i++) 1399 ID.AddPointer(protocols[i]); 1400} 1401 1402void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { 1403 if (getNumProtocols()) 1404 Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); 1405 else 1406 Profile(ID, getDecl(), 0, 0); 1407} 1408 1409void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, 1410 const PrintingPolicy &Policy) const { 1411 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1412 InnerString = ' ' + InnerString; 1413 1414 std::string ObjCQIString = getDecl()->getNameAsString(); 1415 if (getNumProtocols()) { 1416 ObjCQIString += '<'; 1417 bool isFirst = true; 1418 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1419 if (isFirst) 1420 isFirst = false; 1421 else 1422 ObjCQIString += ','; 1423 ObjCQIString += (*I)->getNameAsString(); 1424 } 1425 ObjCQIString += '>'; 1426 } 1427 InnerString = ObjCQIString + InnerString; 1428} 1429 1430void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString, 1431 const PrintingPolicy &Policy) const { 1432 std::string ObjCQIString; 1433 1434 if (isObjCIdType() || isObjCQualifiedIdType()) 1435 ObjCQIString = "id"; 1436 else if (isObjCClassType() || isObjCQualifiedClassType()) 1437 ObjCQIString = "Class"; 1438 else 1439 ObjCQIString = getInterfaceDecl()->getNameAsString(); 1440 1441 if (!qual_empty()) { 1442 ObjCQIString += '<'; 1443 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1444 ObjCQIString += (*I)->getNameAsString(); 1445 if (I+1 != E) 1446 ObjCQIString += ','; 1447 } 1448 ObjCQIString += '>'; 1449 } 1450 1451 PointeeType.getQualifiers().getAsStringInternal(ObjCQIString, Policy); 1452 1453 if (!isObjCIdType() && !isObjCQualifiedIdType()) 1454 ObjCQIString += " *"; // Don't forget the implicit pointer. 1455 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1456 InnerString = ' ' + InnerString; 1457 1458 InnerString = ObjCQIString + InnerString; 1459} 1460 1461void ObjCProtocolListType::getAsStringInternal(std::string &InnerString, 1462 const PrintingPolicy &Policy) const { 1463 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1464 InnerString = ' ' + InnerString; 1465 1466 std::string ObjCQIString = getBaseType().getAsString(Policy); 1467 ObjCQIString += '<'; 1468 bool isFirst = true; 1469 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1470 if (isFirst) 1471 isFirst = false; 1472 else 1473 ObjCQIString += ','; 1474 ObjCQIString += (*I)->getNameAsString(); 1475 } 1476 ObjCQIString += '>'; 1477 InnerString = ObjCQIString + InnerString; 1478} 1479 1480void ElaboratedType::getAsStringInternal(std::string &InnerString, 1481 const PrintingPolicy &Policy) const { 1482 std::string TypeStr; 1483 PrintingPolicy InnerPolicy(Policy); 1484 InnerPolicy.SuppressTagKind = true; 1485 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy); 1486 1487 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString; 1488} 1489 1490void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1491 if (Policy.SuppressTag) 1492 return; 1493 1494 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1495 InnerString = ' ' + InnerString; 1496 1497 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName(); 1498 const char *ID; 1499 if (const IdentifierInfo *II = getDecl()->getIdentifier()) 1500 ID = II->getName(); 1501 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) { 1502 Kind = 0; 1503 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 1504 ID = Typedef->getIdentifier()->getName(); 1505 } else 1506 ID = "<anonymous>"; 1507 1508 // If this is a class template specialization, print the template 1509 // arguments. 1510 if (ClassTemplateSpecializationDecl *Spec 1511 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) { 1512 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1513 std::string TemplateArgsStr 1514 = TemplateSpecializationType::PrintTemplateArgumentList( 1515 TemplateArgs.getFlatArgumentList(), 1516 TemplateArgs.flat_size(), 1517 Policy); 1518 InnerString = TemplateArgsStr + InnerString; 1519 } 1520 1521 if (!Policy.SuppressScope) { 1522 // Compute the full nested-name-specifier for this type. In C, 1523 // this will always be empty. 1524 std::string ContextStr; 1525 for (DeclContext *DC = getDecl()->getDeclContext(); 1526 !DC->isTranslationUnit(); DC = DC->getParent()) { 1527 std::string MyPart; 1528 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 1529 if (NS->getIdentifier()) 1530 MyPart = NS->getNameAsString(); 1531 } else if (ClassTemplateSpecializationDecl *Spec 1532 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 1533 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1534 std::string TemplateArgsStr 1535 = TemplateSpecializationType::PrintTemplateArgumentList( 1536 TemplateArgs.getFlatArgumentList(), 1537 TemplateArgs.flat_size(), 1538 Policy); 1539 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr; 1540 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 1541 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl()) 1542 MyPart = Typedef->getIdentifier()->getName(); 1543 else if (Tag->getIdentifier()) 1544 MyPart = Tag->getIdentifier()->getName(); 1545 } 1546 1547 if (!MyPart.empty()) 1548 ContextStr = MyPart + "::" + ContextStr; 1549 } 1550 1551 if (Kind) 1552 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString; 1553 else 1554 InnerString = ContextStr + ID + InnerString; 1555 } else 1556 InnerString = ID + InnerString; 1557} 1558