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