Type.cpp revision 72564e73277e29f6db3305d1f27ba408abb7ed88
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 "llvm/ADT/StringExtras.h" 21 22using namespace clang; 23 24bool QualType::isConstant(ASTContext &Ctx) const { 25 if (isConstQualified()) 26 return true; 27 28 if (getTypePtr()->isArrayType()) 29 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx); 30 31 return false; 32} 33 34void Type::Destroy(ASTContext& C) { 35 this->~Type(); 36 C.Deallocate(this); 37} 38 39void VariableArrayType::Destroy(ASTContext& C) { 40 SizeExpr->Destroy(C); 41 this->~VariableArrayType(); 42 C.Deallocate(this); 43} 44 45void DependentSizedArrayType::Destroy(ASTContext& C) { 46 SizeExpr->Destroy(C); 47 this->~DependentSizedArrayType(); 48 C.Deallocate(this); 49} 50 51/// getArrayElementTypeNoTypeQual - If this is an array type, return the 52/// element type of the array, potentially with type qualifiers missing. 53/// This method should never be used when type qualifiers are meaningful. 54const Type *Type::getArrayElementTypeNoTypeQual() const { 55 // If this is directly an array type, return it. 56 if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) 57 return ATy->getElementType().getTypePtr(); 58 59 // If the canonical form of this type isn't the right kind, reject it. 60 if (!isa<ArrayType>(CanonicalType)) { 61 // Look through type qualifiers 62 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType())) 63 return AT->getElementType().getTypePtr(); 64 return 0; 65 } 66 67 // If this is a typedef for an array type, strip the typedef off without 68 // losing all typedef information. 69 return getDesugaredType()->getArrayElementTypeNoTypeQual(); 70} 71 72/// getDesugaredType - Return the specified type with any "sugar" removed from 73/// type type. This takes off typedefs, typeof's etc. If the outer level of 74/// the type is already concrete, it returns it unmodified. This is similar 75/// to getting the canonical type, but it doesn't remove *all* typedefs. For 76/// example, it return "T*" as "T*", (not as "int*"), because the pointer is 77/// concrete. 78QualType Type::getDesugaredType() const { 79 if (const TypedefType *TDT = dyn_cast<TypedefType>(this)) 80 return TDT->LookThroughTypedefs(); 81 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this)) 82 return TOE->getUnderlyingExpr()->getType(); 83 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this)) 84 return TOT->getUnderlyingType(); 85 if (const ClassTemplateSpecializationType *Spec 86 = dyn_cast<ClassTemplateSpecializationType>(this)) 87 return Spec->getCanonicalTypeInternal(); 88 89 // FIXME: remove this cast. 90 return QualType(const_cast<Type*>(this), 0); 91} 92 93/// isVoidType - Helper method to determine if this is the 'void' type. 94bool Type::isVoidType() const { 95 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 96 return BT->getKind() == BuiltinType::Void; 97 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType)) 98 return AS->getBaseType()->isVoidType(); 99 return false; 100} 101 102bool Type::isObjectType() const { 103 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType)) 104 return false; 105 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType)) 106 return AS->getBaseType()->isObjectType(); 107 return !CanonicalType->isIncompleteType(); 108} 109 110bool Type::isDerivedType() const { 111 switch (CanonicalType->getTypeClass()) { 112 case ExtQual: 113 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType(); 114 case Pointer: 115 case VariableArray: 116 case ConstantArray: 117 case IncompleteArray: 118 case FunctionProto: 119 case FunctionNoProto: 120 case Reference: 121 case Record: 122 case CXXRecord: 123 return true; 124 default: 125 return false; 126 } 127} 128 129bool Type::isClassType() const { 130 if (const RecordType *RT = getAsRecordType()) 131 return RT->getDecl()->isClass(); 132 return false; 133} 134bool Type::isStructureType() const { 135 if (const RecordType *RT = getAsRecordType()) 136 return RT->getDecl()->isStruct(); 137 return false; 138} 139bool Type::isUnionType() const { 140 if (const RecordType *RT = getAsRecordType()) 141 return RT->getDecl()->isUnion(); 142 return false; 143} 144 145bool Type::isComplexType() const { 146 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 147 return CT->getElementType()->isFloatingType(); 148 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType)) 149 return AS->getBaseType()->isComplexType(); 150 return false; 151} 152 153bool Type::isComplexIntegerType() const { 154 // Check for GCC complex integer extension. 155 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 156 return CT->getElementType()->isIntegerType(); 157 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType)) 158 return AS->getBaseType()->isComplexIntegerType(); 159 return false; 160} 161 162const ComplexType *Type::getAsComplexIntegerType() const { 163 // Are we directly a complex type? 164 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) { 165 if (CTy->getElementType()->isIntegerType()) 166 return CTy; 167 return 0; 168 } 169 170 // If the canonical form of this type isn't what we want, reject it. 171 if (!isa<ComplexType>(CanonicalType)) { 172 // Look through type qualifiers (e.g. ExtQualType's). 173 if (isa<ComplexType>(CanonicalType.getUnqualifiedType())) 174 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType(); 175 return 0; 176 } 177 178 // If this is a typedef for a complex type, strip the typedef off without 179 // losing all typedef information. 180 return getDesugaredType()->getAsComplexIntegerType(); 181} 182 183const BuiltinType *Type::getAsBuiltinType() const { 184 // If this is directly a builtin type, return it. 185 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this)) 186 return BTy; 187 188 // If the canonical form of this type isn't a builtin type, reject it. 189 if (!isa<BuiltinType>(CanonicalType)) { 190 // Look through type qualifiers (e.g. ExtQualType's). 191 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType())) 192 return CanonicalType.getUnqualifiedType()->getAsBuiltinType(); 193 return 0; 194 } 195 196 // If this is a typedef for a builtin type, strip the typedef off without 197 // losing all typedef information. 198 return getDesugaredType()->getAsBuiltinType(); 199} 200 201const FunctionType *Type::getAsFunctionType() const { 202 // If this is directly a function type, return it. 203 if (const FunctionType *FTy = dyn_cast<FunctionType>(this)) 204 return FTy; 205 206 // If the canonical form of this type isn't the right kind, reject it. 207 if (!isa<FunctionType>(CanonicalType)) { 208 // Look through type qualifiers 209 if (isa<FunctionType>(CanonicalType.getUnqualifiedType())) 210 return CanonicalType.getUnqualifiedType()->getAsFunctionType(); 211 return 0; 212 } 213 214 // If this is a typedef for a function type, strip the typedef off without 215 // losing all typedef information. 216 return getDesugaredType()->getAsFunctionType(); 217} 218 219const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const { 220 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType()); 221} 222 223const FunctionProtoType *Type::getAsFunctionProtoType() const { 224 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType()); 225} 226 227 228const PointerType *Type::getAsPointerType() const { 229 // If this is directly a pointer type, return it. 230 if (const PointerType *PTy = dyn_cast<PointerType>(this)) 231 return PTy; 232 233 // If the canonical form of this type isn't the right kind, reject it. 234 if (!isa<PointerType>(CanonicalType)) { 235 // Look through type qualifiers 236 if (isa<PointerType>(CanonicalType.getUnqualifiedType())) 237 return CanonicalType.getUnqualifiedType()->getAsPointerType(); 238 return 0; 239 } 240 241 // If this is a typedef for a pointer type, strip the typedef off without 242 // losing all typedef information. 243 return getDesugaredType()->getAsPointerType(); 244} 245 246const BlockPointerType *Type::getAsBlockPointerType() const { 247 // If this is directly a block pointer type, return it. 248 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this)) 249 return PTy; 250 251 // If the canonical form of this type isn't the right kind, reject it. 252 if (!isa<BlockPointerType>(CanonicalType)) { 253 // Look through type qualifiers 254 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType())) 255 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType(); 256 return 0; 257 } 258 259 // If this is a typedef for a block pointer type, strip the typedef off 260 // without losing all typedef information. 261 return getDesugaredType()->getAsBlockPointerType(); 262} 263 264const ReferenceType *Type::getAsReferenceType() const { 265 // If this is directly a reference type, return it. 266 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this)) 267 return RTy; 268 269 // If the canonical form of this type isn't the right kind, reject it. 270 if (!isa<ReferenceType>(CanonicalType)) { 271 // Look through type qualifiers 272 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType())) 273 return CanonicalType.getUnqualifiedType()->getAsReferenceType(); 274 return 0; 275 } 276 277 // If this is a typedef for a reference type, strip the typedef off without 278 // losing all typedef information. 279 return getDesugaredType()->getAsReferenceType(); 280} 281 282const MemberPointerType *Type::getAsMemberPointerType() const { 283 // If this is directly a member pointer type, return it. 284 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this)) 285 return MTy; 286 287 // If the canonical form of this type isn't the right kind, reject it. 288 if (!isa<MemberPointerType>(CanonicalType)) { 289 // Look through type qualifiers 290 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType())) 291 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType(); 292 return 0; 293 } 294 295 // If this is a typedef for a member pointer type, strip the typedef off 296 // without losing all typedef information. 297 return getDesugaredType()->getAsMemberPointerType(); 298} 299 300/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length 301/// array types and types that contain variable array types in their 302/// declarator 303bool Type::isVariablyModifiedType() const { 304 // A VLA is a variably modified type. 305 if (isVariableArrayType()) 306 return true; 307 308 // An array can contain a variably modified type 309 if (const Type *T = getArrayElementTypeNoTypeQual()) 310 return T->isVariablyModifiedType(); 311 312 // A pointer can point to a variably modified type. 313 // Also, C++ references and member pointers can point to a variably modified 314 // type, where VLAs appear as an extension to C++, and should be treated 315 // correctly. 316 if (const PointerType *PT = getAsPointerType()) 317 return PT->getPointeeType()->isVariablyModifiedType(); 318 if (const ReferenceType *RT = getAsReferenceType()) 319 return RT->getPointeeType()->isVariablyModifiedType(); 320 if (const MemberPointerType *PT = getAsMemberPointerType()) 321 return PT->getPointeeType()->isVariablyModifiedType(); 322 323 // A function can return a variably modified type 324 // This one isn't completely obvious, but it follows from the 325 // definition in C99 6.7.5p3. Because of this rule, it's 326 // illegal to declare a function returning a variably modified type. 327 if (const FunctionType *FT = getAsFunctionType()) 328 return FT->getResultType()->isVariablyModifiedType(); 329 330 return false; 331} 332 333const RecordType *Type::getAsRecordType() const { 334 // If this is directly a record type, return it. 335 if (const RecordType *RTy = dyn_cast<RecordType>(this)) 336 return RTy; 337 338 // If the canonical form of this type isn't the right kind, reject it. 339 if (!isa<RecordType>(CanonicalType)) { 340 // Look through type qualifiers 341 if (isa<RecordType>(CanonicalType.getUnqualifiedType())) 342 return CanonicalType.getUnqualifiedType()->getAsRecordType(); 343 return 0; 344 } 345 346 // If this is a typedef for a record type, strip the typedef off without 347 // losing all typedef information. 348 return getDesugaredType()->getAsRecordType(); 349} 350 351const TagType *Type::getAsTagType() const { 352 // If this is directly a tag type, return it. 353 if (const TagType *TagTy = dyn_cast<TagType>(this)) 354 return TagTy; 355 356 // If the canonical form of this type isn't the right kind, reject it. 357 if (!isa<TagType>(CanonicalType)) { 358 // Look through type qualifiers 359 if (isa<TagType>(CanonicalType.getUnqualifiedType())) 360 return CanonicalType.getUnqualifiedType()->getAsTagType(); 361 return 0; 362 } 363 364 // If this is a typedef for a tag type, strip the typedef off without 365 // losing all typedef information. 366 return getDesugaredType()->getAsTagType(); 367} 368 369const RecordType *Type::getAsStructureType() const { 370 // If this is directly a structure type, return it. 371 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 372 if (RT->getDecl()->isStruct()) 373 return RT; 374 } 375 376 // If the canonical form of this type isn't the right kind, reject it. 377 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 378 if (!RT->getDecl()->isStruct()) 379 return 0; 380 381 // If this is a typedef for a structure type, strip the typedef off without 382 // losing all typedef information. 383 return getDesugaredType()->getAsStructureType(); 384 } 385 // Look through type qualifiers 386 if (isa<RecordType>(CanonicalType.getUnqualifiedType())) 387 return CanonicalType.getUnqualifiedType()->getAsStructureType(); 388 return 0; 389} 390 391const RecordType *Type::getAsUnionType() const { 392 // If this is directly a union type, return it. 393 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 394 if (RT->getDecl()->isUnion()) 395 return RT; 396 } 397 398 // If the canonical form of this type isn't the right kind, reject it. 399 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 400 if (!RT->getDecl()->isUnion()) 401 return 0; 402 403 // If this is a typedef for a union type, strip the typedef off without 404 // losing all typedef information. 405 return getDesugaredType()->getAsUnionType(); 406 } 407 408 // Look through type qualifiers 409 if (isa<RecordType>(CanonicalType.getUnqualifiedType())) 410 return CanonicalType.getUnqualifiedType()->getAsUnionType(); 411 return 0; 412} 413 414const EnumType *Type::getAsEnumType() const { 415 // Check the canonicalized unqualified type directly; the more complex 416 // version is unnecessary because there isn't any typedef information 417 // to preserve. 418 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType()); 419} 420 421const ComplexType *Type::getAsComplexType() const { 422 // Are we directly a complex type? 423 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) 424 return CTy; 425 426 // If the canonical form of this type isn't the right kind, reject it. 427 if (!isa<ComplexType>(CanonicalType)) { 428 // Look through type qualifiers 429 if (isa<ComplexType>(CanonicalType.getUnqualifiedType())) 430 return CanonicalType.getUnqualifiedType()->getAsComplexType(); 431 return 0; 432 } 433 434 // If this is a typedef for a complex type, strip the typedef off without 435 // losing all typedef information. 436 return getDesugaredType()->getAsComplexType(); 437} 438 439const VectorType *Type::getAsVectorType() const { 440 // Are we directly a vector type? 441 if (const VectorType *VTy = dyn_cast<VectorType>(this)) 442 return VTy; 443 444 // If the canonical form of this type isn't the right kind, reject it. 445 if (!isa<VectorType>(CanonicalType)) { 446 // Look through type qualifiers 447 if (isa<VectorType>(CanonicalType.getUnqualifiedType())) 448 return CanonicalType.getUnqualifiedType()->getAsVectorType(); 449 return 0; 450 } 451 452 // If this is a typedef for a vector type, strip the typedef off without 453 // losing all typedef information. 454 return getDesugaredType()->getAsVectorType(); 455} 456 457const ExtVectorType *Type::getAsExtVectorType() const { 458 // Are we directly an OpenCU vector type? 459 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this)) 460 return VTy; 461 462 // If the canonical form of this type isn't the right kind, reject it. 463 if (!isa<ExtVectorType>(CanonicalType)) { 464 // Look through type qualifiers 465 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType())) 466 return CanonicalType.getUnqualifiedType()->getAsExtVectorType(); 467 return 0; 468 } 469 470 // If this is a typedef for an extended vector type, strip the typedef off 471 // without losing all typedef information. 472 return getDesugaredType()->getAsExtVectorType(); 473} 474 475const ObjCInterfaceType *Type::getAsObjCInterfaceType() const { 476 // There is no sugar for ObjCInterfaceType's, just return the canonical 477 // type pointer if it is the right class. There is no typedef information to 478 // return and these cannot be Address-space qualified. 479 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType()); 480} 481 482const ObjCQualifiedInterfaceType * 483Type::getAsObjCQualifiedInterfaceType() const { 484 // There is no sugar for ObjCQualifiedInterfaceType's, just return the 485 // canonical type pointer if it is the right class. 486 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType()); 487} 488 489const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const { 490 // There is no sugar for ObjCQualifiedIdType's, just return the canonical 491 // type pointer if it is the right class. 492 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType()); 493} 494 495const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const { 496 // There is no sugar for template type parameters, so just return 497 // the canonical type pointer if it is the right class. 498 // FIXME: can these be address-space qualified? 499 return dyn_cast<TemplateTypeParmType>(CanonicalType); 500} 501 502const ClassTemplateSpecializationType * 503Type::getClassTemplateSpecializationType() const { 504 // There is no sugar for class template specialization types, so 505 // just return the canonical type pointer if it is the right class. 506 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType); 507} 508 509 510bool Type::isIntegerType() const { 511 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 512 return BT->getKind() >= BuiltinType::Bool && 513 BT->getKind() <= BuiltinType::LongLong; 514 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 515 // Incomplete enum types are not treated as integer types. 516 // FIXME: In C++, enum types are never integer types. 517 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 518 return true; 519 if (isa<FixedWidthIntType>(CanonicalType)) 520 return true; 521 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 522 return VT->getElementType()->isIntegerType(); 523 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 524 return EXTQT->getBaseType()->isIntegerType(); 525 return false; 526} 527 528bool Type::isIntegralType() const { 529 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 530 return BT->getKind() >= BuiltinType::Bool && 531 BT->getKind() <= BuiltinType::LongLong; 532 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 533 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 534 return true; // Complete enum types are integral. 535 // FIXME: In C++, enum types are never integral. 536 if (isa<FixedWidthIntType>(CanonicalType)) 537 return true; 538 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 539 return EXTQT->getBaseType()->isIntegralType(); 540 return false; 541} 542 543bool Type::isEnumeralType() const { 544 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 545 return TT->getDecl()->isEnum(); 546 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 547 return EXTQT->getBaseType()->isEnumeralType(); 548 return false; 549} 550 551bool Type::isBooleanType() const { 552 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 553 return BT->getKind() == BuiltinType::Bool; 554 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 555 return EXTQT->getBaseType()->isBooleanType(); 556 return false; 557} 558 559bool Type::isCharType() const { 560 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 561 return BT->getKind() == BuiltinType::Char_U || 562 BT->getKind() == BuiltinType::UChar || 563 BT->getKind() == BuiltinType::Char_S || 564 BT->getKind() == BuiltinType::SChar; 565 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 566 return EXTQT->getBaseType()->isCharType(); 567 return false; 568} 569 570bool Type::isWideCharType() const { 571 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 572 return BT->getKind() == BuiltinType::WChar; 573 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 574 return EXTQT->getBaseType()->isWideCharType(); 575 return false; 576} 577 578/// isSignedIntegerType - Return true if this is an integer type that is 579/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 580/// an enum decl which has a signed representation, or a vector of signed 581/// integer element type. 582bool Type::isSignedIntegerType() const { 583 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 584 return BT->getKind() >= BuiltinType::Char_S && 585 BT->getKind() <= BuiltinType::LongLong; 586 } 587 588 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 589 return ET->getDecl()->getIntegerType()->isSignedIntegerType(); 590 591 if (const FixedWidthIntType *FWIT = 592 dyn_cast<FixedWidthIntType>(CanonicalType)) 593 return FWIT->isSigned(); 594 595 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 596 return VT->getElementType()->isSignedIntegerType(); 597 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 598 return EXTQT->getBaseType()->isSignedIntegerType(); 599 return false; 600} 601 602/// isUnsignedIntegerType - Return true if this is an integer type that is 603/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 604/// decl which has an unsigned representation, or a vector of unsigned integer 605/// element type. 606bool Type::isUnsignedIntegerType() const { 607 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 608 return BT->getKind() >= BuiltinType::Bool && 609 BT->getKind() <= BuiltinType::ULongLong; 610 } 611 612 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 613 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); 614 615 if (const FixedWidthIntType *FWIT = 616 dyn_cast<FixedWidthIntType>(CanonicalType)) 617 return !FWIT->isSigned(); 618 619 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 620 return VT->getElementType()->isUnsignedIntegerType(); 621 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 622 return EXTQT->getBaseType()->isUnsignedIntegerType(); 623 return false; 624} 625 626bool Type::isFloatingType() const { 627 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 628 return BT->getKind() >= BuiltinType::Float && 629 BT->getKind() <= BuiltinType::LongDouble; 630 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 631 return CT->getElementType()->isFloatingType(); 632 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 633 return VT->getElementType()->isFloatingType(); 634 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 635 return EXTQT->getBaseType()->isFloatingType(); 636 return false; 637} 638 639bool Type::isRealFloatingType() const { 640 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 641 return BT->getKind() >= BuiltinType::Float && 642 BT->getKind() <= BuiltinType::LongDouble; 643 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 644 return VT->getElementType()->isRealFloatingType(); 645 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 646 return EXTQT->getBaseType()->isRealFloatingType(); 647 return false; 648} 649 650bool Type::isRealType() const { 651 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 652 return BT->getKind() >= BuiltinType::Bool && 653 BT->getKind() <= BuiltinType::LongDouble; 654 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 655 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); 656 if (isa<FixedWidthIntType>(CanonicalType)) 657 return true; 658 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 659 return VT->getElementType()->isRealType(); 660 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 661 return EXTQT->getBaseType()->isRealType(); 662 return false; 663} 664 665bool Type::isArithmeticType() const { 666 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 667 return BT->getKind() >= BuiltinType::Bool && 668 BT->getKind() <= BuiltinType::LongDouble; 669 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 670 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). 671 // If a body isn't seen by the time we get here, return false. 672 return ET->getDecl()->isDefinition(); 673 if (isa<FixedWidthIntType>(CanonicalType)) 674 return true; 675 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 676 return EXTQT->getBaseType()->isArithmeticType(); 677 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); 678} 679 680bool Type::isScalarType() const { 681 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 682 return BT->getKind() != BuiltinType::Void; 683 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { 684 // Enums are scalar types, but only if they are defined. Incomplete enums 685 // are not treated as scalar types. 686 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) 687 return true; 688 return false; 689 } 690 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 691 return EXTQT->getBaseType()->isScalarType(); 692 if (isa<FixedWidthIntType>(CanonicalType)) 693 return true; 694 return isa<PointerType>(CanonicalType) || 695 isa<BlockPointerType>(CanonicalType) || 696 isa<MemberPointerType>(CanonicalType) || 697 isa<ComplexType>(CanonicalType) || 698 isa<ObjCQualifiedIdType>(CanonicalType) || 699 isa<ObjCQualifiedClassType>(CanonicalType); 700} 701 702/// \brief Determines whether the type is a C++ aggregate type or C 703/// aggregate or union type. 704/// 705/// An aggregate type is an array or a class type (struct, union, or 706/// class) that has no user-declared constructors, no private or 707/// protected non-static data members, no base classes, and no virtual 708/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type 709/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also 710/// includes union types. 711bool Type::isAggregateType() const { 712 if (const CXXRecordType *CXXClassType = dyn_cast<CXXRecordType>(CanonicalType)) 713 return CXXClassType->getDecl()->isAggregate(); 714 if (isa<RecordType>(CanonicalType)) 715 return true; 716 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 717 return EXTQT->getBaseType()->isAggregateType(); 718 return isa<ArrayType>(CanonicalType); 719} 720 721/// isConstantSizeType - Return true if this is not a variable sized type, 722/// according to the rules of C99 6.7.5p3. It is not legal to call this on 723/// incomplete types or dependent types. 724bool Type::isConstantSizeType() const { 725 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) 726 return EXTQT->getBaseType()->isConstantSizeType(); 727 assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); 728 assert(!isDependentType() && "This doesn't make sense for dependent types"); 729 // The VAT must have a size, as it is known to be complete. 730 return !isa<VariableArrayType>(CanonicalType); 731} 732 733/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) 734/// - a type that can describe objects, but which lacks information needed to 735/// determine its size. 736bool Type::isIncompleteType() const { 737 switch (CanonicalType->getTypeClass()) { 738 default: return false; 739 case ExtQual: 740 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType(); 741 case Builtin: 742 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never 743 // be completed. 744 return isVoidType(); 745 case Record: 746 case CXXRecord: 747 case Enum: 748 // A tagged type (struct/union/enum/class) is incomplete if the decl is a 749 // forward declaration, but not a full definition (C99 6.2.5p22). 750 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); 751 case IncompleteArray: 752 // An array of unknown size is an incomplete type (C99 6.2.5p22). 753 return true; 754 } 755} 756 757/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) 758bool Type::isPODType() const { 759 // The compiler shouldn't query this for incomplete types, but the user might. 760 // We return false for that case. 761 if (isIncompleteType()) 762 return false; 763 764 switch (CanonicalType->getTypeClass()) { 765 // Everything not explicitly mentioned is not POD. 766 default: return false; 767 case ExtQual: 768 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType(); 769 case VariableArray: 770 case ConstantArray: 771 // IncompleteArray is caught by isIncompleteType() above. 772 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType(); 773 774 case Builtin: 775 case Complex: 776 case Pointer: 777 case MemberPointer: 778 case Vector: 779 case ExtVector: 780 case ObjCQualifiedId: 781 return true; 782 783 case Enum: 784 return true; 785 786 case Record: 787 // C struct/union is POD. 788 return true; 789 790 case CXXRecord: 791 return cast<CXXRecordType>(CanonicalType)->getDecl()->isPOD(); 792 } 793} 794 795bool Type::isPromotableIntegerType() const { 796 if (const BuiltinType *BT = getAsBuiltinType()) 797 switch (BT->getKind()) { 798 case BuiltinType::Bool: 799 case BuiltinType::Char_S: 800 case BuiltinType::Char_U: 801 case BuiltinType::SChar: 802 case BuiltinType::UChar: 803 case BuiltinType::Short: 804 case BuiltinType::UShort: 805 return true; 806 default: 807 return false; 808 } 809 return false; 810} 811 812const char *BuiltinType::getName() const { 813 switch (getKind()) { 814 default: assert(0 && "Unknown builtin type!"); 815 case Void: return "void"; 816 case Bool: return "_Bool"; 817 case Char_S: return "char"; 818 case Char_U: return "char"; 819 case SChar: return "signed char"; 820 case Short: return "short"; 821 case Int: return "int"; 822 case Long: return "long"; 823 case LongLong: return "long long"; 824 case UChar: return "unsigned char"; 825 case UShort: return "unsigned short"; 826 case UInt: return "unsigned int"; 827 case ULong: return "unsigned long"; 828 case ULongLong: return "unsigned long long"; 829 case Float: return "float"; 830 case Double: return "double"; 831 case LongDouble: return "long double"; 832 case WChar: return "wchar_t"; 833 case Overload: return "<overloaded function type>"; 834 case Dependent: return "<dependent type>"; 835 } 836} 837 838void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 839 arg_type_iterator ArgTys, 840 unsigned NumArgs, bool isVariadic, 841 unsigned TypeQuals) { 842 ID.AddPointer(Result.getAsOpaquePtr()); 843 for (unsigned i = 0; i != NumArgs; ++i) 844 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 845 ID.AddInteger(isVariadic); 846 ID.AddInteger(TypeQuals); 847} 848 849void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { 850 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), 851 getTypeQuals()); 852} 853 854void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID, 855 const ObjCInterfaceDecl *Decl, 856 ObjCProtocolDecl **protocols, 857 unsigned NumProtocols) { 858 ID.AddPointer(Decl); 859 for (unsigned i = 0; i != NumProtocols; i++) 860 ID.AddPointer(protocols[i]); 861} 862 863void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { 864 Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); 865} 866 867void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID, 868 ObjCProtocolDecl **protocols, 869 unsigned NumProtocols) { 870 for (unsigned i = 0; i != NumProtocols; i++) 871 ID.AddPointer(protocols[i]); 872} 873 874void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) { 875 Profile(ID, &Protocols[0], getNumProtocols()); 876} 877 878/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 879/// potentially looking through *all* consequtive typedefs. This returns the 880/// sum of the type qualifiers, so if you have: 881/// typedef const int A; 882/// typedef volatile A B; 883/// looking through the typedefs for B will give you "const volatile A". 884/// 885QualType TypedefType::LookThroughTypedefs() const { 886 // Usually, there is only a single level of typedefs, be fast in that case. 887 QualType FirstType = getDecl()->getUnderlyingType(); 888 if (!isa<TypedefType>(FirstType)) 889 return FirstType; 890 891 // Otherwise, do the fully general loop. 892 unsigned TypeQuals = 0; 893 const TypedefType *TDT = this; 894 while (1) { 895 QualType CurType = TDT->getDecl()->getUnderlyingType(); 896 897 898 /// FIXME: 899 /// FIXME: This is incorrect for ExtQuals! 900 /// FIXME: 901 TypeQuals |= CurType.getCVRQualifiers(); 902 903 TDT = dyn_cast<TypedefType>(CurType); 904 if (TDT == 0) 905 return QualType(CurType.getTypePtr(), TypeQuals); 906 } 907} 908 909TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 910 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { 911 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 912} 913 914bool RecordType::classof(const TagType *TT) { 915 return isa<RecordDecl>(TT->getDecl()); 916} 917 918bool CXXRecordType::classof(const TagType *TT) { 919 return isa<CXXRecordDecl>(TT->getDecl()); 920} 921 922bool EnumType::classof(const TagType *TT) { 923 return isa<EnumDecl>(TT->getDecl()); 924} 925 926void 927ClassTemplateSpecializationType:: 928packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) { 929 const unsigned BitsPerWord = sizeof(uintptr_t) * 8; 930 931 for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0; 932 PW != NumPackedWords; ++PW) { 933 uintptr_t Word = 0; 934 for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) { 935 Word <<= 1; 936 Word |= Values[Arg]; 937 } 938 Words[PW] = Word; 939 } 940} 941 942ClassTemplateSpecializationType:: 943ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs, 944 uintptr_t *Args, bool *ArgIsType, 945 QualType Canon) 946 : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false), 947 Template(T), NumArgs(NumArgs) 948{ 949 uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1); 950 951 // Pack the argument-is-type values into the words just after the 952 // class template specialization type. 953 packBooleanValues(NumArgs, ArgIsType, Data); 954 955 // Copy the template arguments after the packed words. 956 Data += getNumPackedWords(NumArgs); 957 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) 958 Data[Arg] = Args[Arg]; 959} 960 961void ClassTemplateSpecializationType::Destroy(ASTContext& C) { 962 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) 963 if (!isArgType(Arg)) 964 getArgAsExpr(Arg)->Destroy(C); 965} 966 967uintptr_t 968ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const { 969 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1); 970 Data += getNumPackedWords(NumArgs); 971 return Data[Arg]; 972} 973 974bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const { 975 const unsigned BitsPerWord = sizeof(uintptr_t) * 8; 976 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1); 977 Data += Arg / BitsPerWord; 978 return (*Data >> ((NumArgs - Arg) % BitsPerWord - 1)) & 0x01; 979} 980 981//===----------------------------------------------------------------------===// 982// Type Printing 983//===----------------------------------------------------------------------===// 984 985void QualType::dump(const char *msg) const { 986 std::string R = "identifier"; 987 getAsStringInternal(R); 988 if (msg) 989 fprintf(stderr, "%s: %s\n", msg, R.c_str()); 990 else 991 fprintf(stderr, "%s\n", R.c_str()); 992} 993void QualType::dump() const { 994 dump(""); 995} 996 997void Type::dump() const { 998 std::string S = "identifier"; 999 getAsStringInternal(S); 1000 fprintf(stderr, "%s\n", S.c_str()); 1001} 1002 1003 1004 1005static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { 1006 // Note: funkiness to ensure we get a space only between quals. 1007 bool NonePrinted = true; 1008 if (TypeQuals & QualType::Const) 1009 S += "const", NonePrinted = false; 1010 if (TypeQuals & QualType::Volatile) 1011 S += (NonePrinted+" volatile"), NonePrinted = false; 1012 if (TypeQuals & QualType::Restrict) 1013 S += (NonePrinted+" restrict"), NonePrinted = false; 1014} 1015 1016void QualType::getAsStringInternal(std::string &S) const { 1017 if (isNull()) { 1018 S += "NULL TYPE"; 1019 return; 1020 } 1021 1022 // Print qualifiers as appropriate. 1023 if (unsigned Tq = getCVRQualifiers()) { 1024 std::string TQS; 1025 AppendTypeQualList(TQS, Tq); 1026 if (!S.empty()) 1027 S = TQS + ' ' + S; 1028 else 1029 S = TQS; 1030 } 1031 1032 getTypePtr()->getAsStringInternal(S); 1033} 1034 1035void BuiltinType::getAsStringInternal(std::string &S) const { 1036 if (S.empty()) { 1037 S = getName(); 1038 } else { 1039 // Prefix the basic type, e.g. 'int X'. 1040 S = ' ' + S; 1041 S = getName() + S; 1042 } 1043} 1044 1045void FixedWidthIntType::getAsStringInternal(std::string &S) const { 1046 // FIXME: Once we get bitwidth attribute, write as 1047 // "int __attribute__((bitwidth(x)))". 1048 std::string prefix = "__clang_fixedwidth"; 1049 prefix += llvm::utostr_32(Width); 1050 prefix += (char)(Signed ? 'S' : 'U'); 1051 if (S.empty()) { 1052 S = prefix; 1053 } else { 1054 // Prefix the basic type, e.g. 'int X'. 1055 S = prefix + S; 1056 } 1057} 1058 1059 1060void ComplexType::getAsStringInternal(std::string &S) const { 1061 ElementType->getAsStringInternal(S); 1062 S = "_Complex " + S; 1063} 1064 1065void ExtQualType::getAsStringInternal(std::string &S) const { 1066 bool NeedsSpace = false; 1067 if (AddressSpace) { 1068 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S; 1069 NeedsSpace = true; 1070 } 1071 if (GCAttrType != QualType::GCNone) { 1072 if (NeedsSpace) 1073 S += ' '; 1074 S += "__attribute__((objc_gc("; 1075 if (GCAttrType == QualType::Weak) 1076 S += "weak"; 1077 else 1078 S += "strong"; 1079 S += ")))"; 1080 } 1081 BaseType->getAsStringInternal(S); 1082} 1083 1084void PointerType::getAsStringInternal(std::string &S) const { 1085 S = '*' + S; 1086 1087 // Handle things like 'int (*A)[4];' correctly. 1088 // FIXME: this should include vectors, but vectors use attributes I guess. 1089 if (isa<ArrayType>(getPointeeType())) 1090 S = '(' + S + ')'; 1091 1092 getPointeeType().getAsStringInternal(S); 1093} 1094 1095void BlockPointerType::getAsStringInternal(std::string &S) const { 1096 S = '^' + S; 1097 PointeeType.getAsStringInternal(S); 1098} 1099 1100void ReferenceType::getAsStringInternal(std::string &S) const { 1101 S = '&' + S; 1102 1103 // Handle things like 'int (&A)[4];' correctly. 1104 // FIXME: this should include vectors, but vectors use attributes I guess. 1105 if (isa<ArrayType>(getPointeeType())) 1106 S = '(' + S + ')'; 1107 1108 getPointeeType().getAsStringInternal(S); 1109} 1110 1111void MemberPointerType::getAsStringInternal(std::string &S) const { 1112 std::string C; 1113 Class->getAsStringInternal(C); 1114 C += "::*"; 1115 S = C + S; 1116 1117 // Handle things like 'int (&A)[4];' correctly. 1118 // FIXME: this should include vectors, but vectors use attributes I guess. 1119 if (isa<ArrayType>(getPointeeType())) 1120 S = '(' + S + ')'; 1121 1122 getPointeeType().getAsStringInternal(S); 1123} 1124 1125void ConstantArrayType::getAsStringInternal(std::string &S) const { 1126 S += '['; 1127 S += llvm::utostr(getSize().getZExtValue()); 1128 S += ']'; 1129 1130 getElementType().getAsStringInternal(S); 1131} 1132 1133void IncompleteArrayType::getAsStringInternal(std::string &S) const { 1134 S += "[]"; 1135 1136 getElementType().getAsStringInternal(S); 1137} 1138 1139void VariableArrayType::getAsStringInternal(std::string &S) const { 1140 S += '['; 1141 1142 if (getIndexTypeQualifier()) { 1143 AppendTypeQualList(S, getIndexTypeQualifier()); 1144 S += ' '; 1145 } 1146 1147 if (getSizeModifier() == Static) 1148 S += "static"; 1149 else if (getSizeModifier() == Star) 1150 S += '*'; 1151 1152 if (getSizeExpr()) { 1153 std::string SStr; 1154 llvm::raw_string_ostream s(SStr); 1155 getSizeExpr()->printPretty(s); 1156 S += s.str(); 1157 } 1158 S += ']'; 1159 1160 getElementType().getAsStringInternal(S); 1161} 1162 1163void DependentSizedArrayType::getAsStringInternal(std::string &S) const { 1164 S += '['; 1165 1166 if (getIndexTypeQualifier()) { 1167 AppendTypeQualList(S, getIndexTypeQualifier()); 1168 S += ' '; 1169 } 1170 1171 if (getSizeModifier() == Static) 1172 S += "static"; 1173 else if (getSizeModifier() == Star) 1174 S += '*'; 1175 1176 if (getSizeExpr()) { 1177 std::string SStr; 1178 llvm::raw_string_ostream s(SStr); 1179 getSizeExpr()->printPretty(s); 1180 S += s.str(); 1181 } 1182 S += ']'; 1183 1184 getElementType().getAsStringInternal(S); 1185} 1186 1187void VectorType::getAsStringInternal(std::string &S) const { 1188 // FIXME: We prefer to print the size directly here, but have no way 1189 // to get the size of the type. 1190 S += " __attribute__((__vector_size__("; 1191 S += llvm::utostr_32(NumElements); // convert back to bytes. 1192 S += " * sizeof(" + ElementType.getAsString() + "))))"; 1193 ElementType.getAsStringInternal(S); 1194} 1195 1196void ExtVectorType::getAsStringInternal(std::string &S) const { 1197 S += " __attribute__((ext_vector_type("; 1198 S += llvm::utostr_32(NumElements); 1199 S += ")))"; 1200 ElementType.getAsStringInternal(S); 1201} 1202 1203void TypeOfExprType::getAsStringInternal(std::string &InnerString) const { 1204 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. 1205 InnerString = ' ' + InnerString; 1206 std::string Str; 1207 llvm::raw_string_ostream s(Str); 1208 getUnderlyingExpr()->printPretty(s); 1209 InnerString = "typeof(" + s.str() + ")" + InnerString; 1210} 1211 1212void TypeOfType::getAsStringInternal(std::string &InnerString) const { 1213 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. 1214 InnerString = ' ' + InnerString; 1215 std::string Tmp; 1216 getUnderlyingType().getAsStringInternal(Tmp); 1217 InnerString = "typeof(" + Tmp + ")" + InnerString; 1218} 1219 1220void FunctionNoProtoType::getAsStringInternal(std::string &S) const { 1221 // If needed for precedence reasons, wrap the inner part in grouping parens. 1222 if (!S.empty()) 1223 S = "(" + S + ")"; 1224 1225 S += "()"; 1226 getResultType().getAsStringInternal(S); 1227} 1228 1229void FunctionProtoType::getAsStringInternal(std::string &S) const { 1230 // If needed for precedence reasons, wrap the inner part in grouping parens. 1231 if (!S.empty()) 1232 S = "(" + S + ")"; 1233 1234 S += "("; 1235 std::string Tmp; 1236 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { 1237 if (i) S += ", "; 1238 getArgType(i).getAsStringInternal(Tmp); 1239 S += Tmp; 1240 Tmp.clear(); 1241 } 1242 1243 if (isVariadic()) { 1244 if (getNumArgs()) 1245 S += ", "; 1246 S += "..."; 1247 } else if (getNumArgs() == 0) { 1248 // Do not emit int() if we have a proto, emit 'int(void)'. 1249 S += "void"; 1250 } 1251 1252 S += ")"; 1253 getResultType().getAsStringInternal(S); 1254} 1255 1256 1257void TypedefType::getAsStringInternal(std::string &InnerString) const { 1258 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1259 InnerString = ' ' + InnerString; 1260 InnerString = getDecl()->getIdentifier()->getName() + InnerString; 1261} 1262 1263void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const { 1264 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'. 1265 InnerString = ' ' + InnerString; 1266 1267 if (!Name) 1268 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' + 1269 llvm::utostr_32(Index) + InnerString; 1270 else 1271 InnerString = Name->getName() + InnerString; 1272} 1273 1274void 1275ClassTemplateSpecializationType:: 1276getAsStringInternal(std::string &InnerString) const { 1277 std::string SpecString = Template->getNameAsString(); 1278 SpecString += '<'; 1279 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1280 if (Arg) 1281 SpecString += ", "; 1282 1283 // Print the argument into a string. 1284 std::string ArgString; 1285 if (isArgType(Arg)) 1286 getArgAsType(Arg).getAsStringInternal(ArgString); 1287 else { 1288 llvm::raw_string_ostream s(ArgString); 1289 getArgAsExpr(Arg)->printPretty(s); 1290 } 1291 1292 // If this is the first argument and its string representation 1293 // begins with the global scope specifier ('::foo'), add a space 1294 // to avoid printing the diagraph '<:'. 1295 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1296 SpecString += ' '; 1297 1298 SpecString += ArgString; 1299 } 1300 1301 // If the last character of our string is '>', add another space to 1302 // keep the two '>''s separate tokens. We don't *have* to do this in 1303 // C++0x, but it's still good hygiene. 1304 if (SpecString[SpecString.size() - 1] == '>') 1305 SpecString += ' '; 1306 1307 SpecString += '>'; 1308 1309 if (InnerString.empty()) 1310 InnerString.swap(SpecString); 1311 else 1312 InnerString = SpecString + ' ' + InnerString; 1313} 1314 1315void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const { 1316 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1317 InnerString = ' ' + InnerString; 1318 InnerString = getDecl()->getIdentifier()->getName() + InnerString; 1319} 1320 1321void ObjCQualifiedInterfaceType::getAsStringInternal( 1322 std::string &InnerString) const { 1323 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1324 InnerString = ' ' + InnerString; 1325 std::string ObjCQIString = getDecl()->getNameAsString(); 1326 ObjCQIString += '<'; 1327 bool isFirst = true; 1328 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1329 if (isFirst) 1330 isFirst = false; 1331 else 1332 ObjCQIString += ','; 1333 ObjCQIString += (*I)->getNameAsString(); 1334 } 1335 ObjCQIString += '>'; 1336 InnerString = ObjCQIString + InnerString; 1337} 1338 1339void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const { 1340 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1341 InnerString = ' ' + InnerString; 1342 std::string ObjCQIString = "id"; 1343 ObjCQIString += '<'; 1344 int num = getNumProtocols(); 1345 for (int i = 0; i < num; i++) { 1346 ObjCQIString += getProtocols(i)->getNameAsString(); 1347 if (i < num-1) 1348 ObjCQIString += ','; 1349 } 1350 ObjCQIString += '>'; 1351 InnerString = ObjCQIString + InnerString; 1352} 1353 1354void TagType::getAsStringInternal(std::string &InnerString) const { 1355 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1356 InnerString = ' ' + InnerString; 1357 1358 const char *Kind = getDecl()->getKindName(); 1359 const char *ID; 1360 if (const IdentifierInfo *II = getDecl()->getIdentifier()) 1361 ID = II->getName(); 1362 else 1363 ID = "<anonymous>"; 1364 1365 InnerString = std::string(Kind) + " " + ID + InnerString; 1366} 1367