Type.cpp revision c8f2c61f4f667c2bc3e4e74b274fa397a4232393
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 case Complex: 907 case TypeOfExpr: 908 case TypeOf: 909 case TemplateTypeParm: 910 case TemplateSpecialization: 911 case QualifiedName: 912 case Typename: 913 case ObjCInterface: 914 case ObjCQualifiedInterface: 915 case ObjCQualifiedId: 916 return true; 917 default: 918 return false; 919 } 920} 921 922const char *BuiltinType::getName(bool CPlusPlus) const { 923 switch (getKind()) { 924 default: assert(0 && "Unknown builtin type!"); 925 case Void: return "void"; 926 case Bool: return CPlusPlus? "bool" : "_Bool"; 927 case Char_S: return "char"; 928 case Char_U: return "char"; 929 case SChar: return "signed char"; 930 case Short: return "short"; 931 case Int: return "int"; 932 case Long: return "long"; 933 case LongLong: return "long long"; 934 case Int128: return "__int128_t"; 935 case UChar: return "unsigned char"; 936 case UShort: return "unsigned short"; 937 case UInt: return "unsigned int"; 938 case ULong: return "unsigned long"; 939 case ULongLong: return "unsigned long long"; 940 case UInt128: return "__uint128_t"; 941 case Float: return "float"; 942 case Double: return "double"; 943 case LongDouble: return "long double"; 944 case WChar: return "wchar_t"; 945 case NullPtr: return "nullptr_t"; 946 case Overload: return "<overloaded function type>"; 947 case Dependent: return "<dependent type>"; 948 } 949} 950 951void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 952 arg_type_iterator ArgTys, 953 unsigned NumArgs, bool isVariadic, 954 unsigned TypeQuals, bool hasExceptionSpec, 955 bool anyExceptionSpec, unsigned NumExceptions, 956 exception_iterator Exs) { 957 ID.AddPointer(Result.getAsOpaquePtr()); 958 for (unsigned i = 0; i != NumArgs; ++i) 959 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 960 ID.AddInteger(isVariadic); 961 ID.AddInteger(TypeQuals); 962 ID.AddInteger(hasExceptionSpec); 963 if (hasExceptionSpec) { 964 ID.AddInteger(anyExceptionSpec); 965 for(unsigned i = 0; i != NumExceptions; ++i) 966 ID.AddPointer(Exs[i].getAsOpaquePtr()); 967 } 968} 969 970void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { 971 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), 972 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), 973 getNumExceptions(), exception_begin()); 974} 975 976void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID, 977 const ObjCInterfaceDecl *Decl, 978 ObjCProtocolDecl **protocols, 979 unsigned NumProtocols) { 980 ID.AddPointer(Decl); 981 for (unsigned i = 0; i != NumProtocols; i++) 982 ID.AddPointer(protocols[i]); 983} 984 985void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { 986 Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); 987} 988 989void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID, 990 ObjCProtocolDecl **protocols, 991 unsigned NumProtocols) { 992 for (unsigned i = 0; i != NumProtocols; i++) 993 ID.AddPointer(protocols[i]); 994} 995 996void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) { 997 Profile(ID, &Protocols[0], getNumProtocols()); 998} 999 1000/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 1001/// potentially looking through *all* consequtive typedefs. This returns the 1002/// sum of the type qualifiers, so if you have: 1003/// typedef const int A; 1004/// typedef volatile A B; 1005/// looking through the typedefs for B will give you "const volatile A". 1006/// 1007QualType TypedefType::LookThroughTypedefs() const { 1008 // Usually, there is only a single level of typedefs, be fast in that case. 1009 QualType FirstType = getDecl()->getUnderlyingType(); 1010 if (!isa<TypedefType>(FirstType)) 1011 return FirstType; 1012 1013 // Otherwise, do the fully general loop. 1014 unsigned TypeQuals = 0; 1015 const TypedefType *TDT = this; 1016 while (1) { 1017 QualType CurType = TDT->getDecl()->getUnderlyingType(); 1018 1019 1020 /// FIXME: 1021 /// FIXME: This is incorrect for ExtQuals! 1022 /// FIXME: 1023 TypeQuals |= CurType.getCVRQualifiers(); 1024 1025 TDT = dyn_cast<TypedefType>(CurType); 1026 if (TDT == 0) 1027 return QualType(CurType.getTypePtr(), TypeQuals); 1028 } 1029} 1030 1031TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 1032 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { 1033 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1034} 1035 1036TagType::TagType(TypeClass TC, TagDecl *D, QualType can) 1037 : Type(TC, can, D->isDependentType()), decl(D, 0) {} 1038 1039bool RecordType::classof(const TagType *TT) { 1040 return isa<RecordDecl>(TT->getDecl()); 1041} 1042 1043bool EnumType::classof(const TagType *TT) { 1044 return isa<EnumDecl>(TT->getDecl()); 1045} 1046 1047bool 1048TemplateSpecializationType:: 1049anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) { 1050 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) { 1051 switch (Args[Idx].getKind()) { 1052 case TemplateArgument::Type: 1053 if (Args[Idx].getAsType()->isDependentType()) 1054 return true; 1055 break; 1056 1057 case TemplateArgument::Declaration: 1058 case TemplateArgument::Integral: 1059 // Never dependent 1060 break; 1061 1062 case TemplateArgument::Expression: 1063 if (Args[Idx].getAsExpr()->isTypeDependent() || 1064 Args[Idx].getAsExpr()->isValueDependent()) 1065 return true; 1066 break; 1067 } 1068 } 1069 1070 return false; 1071} 1072 1073TemplateSpecializationType:: 1074TemplateSpecializationType(TemplateName T, const TemplateArgument *Args, 1075 unsigned NumArgs, QualType Canon) 1076 : Type(TemplateSpecialization, 1077 Canon.isNull()? QualType(this, 0) : Canon, 1078 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), 1079 Template(T), NumArgs(NumArgs) 1080{ 1081 assert((!Canon.isNull() || 1082 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && 1083 "No canonical type for non-dependent class template specialization"); 1084 1085 TemplateArgument *TemplateArgs 1086 = reinterpret_cast<TemplateArgument *>(this + 1); 1087 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) 1088 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); 1089} 1090 1091void TemplateSpecializationType::Destroy(ASTContext& C) { 1092 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1093 // FIXME: Not all expressions get cloned, so we can't yet perform 1094 // this destruction. 1095 // if (Expr *E = getArg(Arg).getAsExpr()) 1096 // E->Destroy(C); 1097 } 1098} 1099 1100TemplateSpecializationType::iterator 1101TemplateSpecializationType::end() const { 1102 return begin() + getNumArgs(); 1103} 1104 1105const TemplateArgument & 1106TemplateSpecializationType::getArg(unsigned Idx) const { 1107 assert(Idx < getNumArgs() && "Template argument out of range"); 1108 return getArgs()[Idx]; 1109} 1110 1111void 1112TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 1113 TemplateName T, 1114 const TemplateArgument *Args, 1115 unsigned NumArgs) { 1116 T.Profile(ID); 1117 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 1118 Args[Idx].Profile(ID); 1119} 1120 1121//===----------------------------------------------------------------------===// 1122// Type Printing 1123//===----------------------------------------------------------------------===// 1124 1125void QualType::dump(const char *msg) const { 1126 PrintingPolicy Policy; 1127 std::string R = "identifier"; 1128 getAsStringInternal(R, Policy); 1129 if (msg) 1130 fprintf(stderr, "%s: %s\n", msg, R.c_str()); 1131 else 1132 fprintf(stderr, "%s\n", R.c_str()); 1133} 1134void QualType::dump() const { 1135 dump(""); 1136} 1137 1138void Type::dump() const { 1139 std::string S = "identifier"; 1140 getAsStringInternal(S, PrintingPolicy()); 1141 fprintf(stderr, "%s\n", S.c_str()); 1142} 1143 1144 1145 1146static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { 1147 // Note: funkiness to ensure we get a space only between quals. 1148 bool NonePrinted = true; 1149 if (TypeQuals & QualType::Const) 1150 S += "const", NonePrinted = false; 1151 if (TypeQuals & QualType::Volatile) 1152 S += (NonePrinted+" volatile"), NonePrinted = false; 1153 if (TypeQuals & QualType::Restrict) 1154 S += (NonePrinted+" restrict"), NonePrinted = false; 1155} 1156 1157std::string QualType::getAsString() const { 1158 std::string S; 1159 getAsStringInternal(S, PrintingPolicy()); 1160 return S; 1161} 1162 1163void 1164QualType::getAsStringInternal(std::string &S, 1165 const PrintingPolicy &Policy) const { 1166 if (isNull()) { 1167 S += "NULL TYPE"; 1168 return; 1169 } 1170 1171 if (Policy.SuppressTypeSpecifiers && getTypePtr()->isSpecifierType()) 1172 return; 1173 1174 // Print qualifiers as appropriate. 1175 if (unsigned Tq = getCVRQualifiers()) { 1176 std::string TQS; 1177 AppendTypeQualList(TQS, Tq); 1178 if (!S.empty()) 1179 S = TQS + ' ' + S; 1180 else 1181 S = TQS; 1182 } 1183 1184 getTypePtr()->getAsStringInternal(S, Policy); 1185} 1186 1187void BuiltinType::getAsStringInternal(std::string &S, 1188 const PrintingPolicy &Policy) const { 1189 if (S.empty()) { 1190 S = getName(Policy.CPlusPlus); 1191 } else { 1192 // Prefix the basic type, e.g. 'int X'. 1193 S = ' ' + S; 1194 S = getName(Policy.CPlusPlus) + S; 1195 } 1196} 1197 1198void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1199 // FIXME: Once we get bitwidth attribute, write as 1200 // "int __attribute__((bitwidth(x)))". 1201 std::string prefix = "__clang_fixedwidth"; 1202 prefix += llvm::utostr_32(Width); 1203 prefix += (char)(Signed ? 'S' : 'U'); 1204 if (S.empty()) { 1205 S = prefix; 1206 } else { 1207 // Prefix the basic type, e.g. 'int X'. 1208 S = prefix + S; 1209 } 1210} 1211 1212 1213void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1214 ElementType->getAsStringInternal(S, Policy); 1215 S = "_Complex " + S; 1216} 1217 1218void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1219 bool NeedsSpace = false; 1220 if (AddressSpace) { 1221 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S; 1222 NeedsSpace = true; 1223 } 1224 if (GCAttrType != QualType::GCNone) { 1225 if (NeedsSpace) 1226 S += ' '; 1227 S += "__attribute__((objc_gc("; 1228 if (GCAttrType == QualType::Weak) 1229 S += "weak"; 1230 else 1231 S += "strong"; 1232 S += ")))"; 1233 } 1234 BaseType->getAsStringInternal(S, Policy); 1235} 1236 1237void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1238 S = '*' + S; 1239 1240 // Handle things like 'int (*A)[4];' correctly. 1241 // FIXME: this should include vectors, but vectors use attributes I guess. 1242 if (isa<ArrayType>(getPointeeType())) 1243 S = '(' + S + ')'; 1244 1245 getPointeeType().getAsStringInternal(S, Policy); 1246} 1247 1248void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1249 S = '^' + S; 1250 PointeeType.getAsStringInternal(S, Policy); 1251} 1252 1253void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1254 S = '&' + S; 1255 1256 // Handle things like 'int (&A)[4];' correctly. 1257 // FIXME: this should include vectors, but vectors use attributes I guess. 1258 if (isa<ArrayType>(getPointeeType())) 1259 S = '(' + S + ')'; 1260 1261 getPointeeType().getAsStringInternal(S, Policy); 1262} 1263 1264void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1265 S = "&&" + S; 1266 1267 // Handle things like 'int (&&A)[4];' correctly. 1268 // FIXME: this should include vectors, but vectors use attributes I guess. 1269 if (isa<ArrayType>(getPointeeType())) 1270 S = '(' + S + ')'; 1271 1272 getPointeeType().getAsStringInternal(S, Policy); 1273} 1274 1275void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1276 std::string C; 1277 Class->getAsStringInternal(C, Policy); 1278 C += "::*"; 1279 S = C + S; 1280 1281 // Handle things like 'int (Cls::*A)[4];' correctly. 1282 // FIXME: this should include vectors, but vectors use attributes I guess. 1283 if (isa<ArrayType>(getPointeeType())) 1284 S = '(' + S + ')'; 1285 1286 getPointeeType().getAsStringInternal(S, Policy); 1287} 1288 1289void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1290 S += '['; 1291 S += llvm::utostr(getSize().getZExtValue()); 1292 S += ']'; 1293 1294 getElementType().getAsStringInternal(S, Policy); 1295} 1296 1297void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1298 S += "[]"; 1299 1300 getElementType().getAsStringInternal(S, Policy); 1301} 1302 1303void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1304 S += '['; 1305 1306 if (getIndexTypeQualifier()) { 1307 AppendTypeQualList(S, getIndexTypeQualifier()); 1308 S += ' '; 1309 } 1310 1311 if (getSizeModifier() == Static) 1312 S += "static"; 1313 else if (getSizeModifier() == Star) 1314 S += '*'; 1315 1316 if (getSizeExpr()) { 1317 std::string SStr; 1318 llvm::raw_string_ostream s(SStr); 1319 getSizeExpr()->printPretty(s, 0, Policy); 1320 S += s.str(); 1321 } 1322 S += ']'; 1323 1324 getElementType().getAsStringInternal(S, Policy); 1325} 1326 1327void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1328 S += '['; 1329 1330 if (getIndexTypeQualifier()) { 1331 AppendTypeQualList(S, getIndexTypeQualifier()); 1332 S += ' '; 1333 } 1334 1335 if (getSizeModifier() == Static) 1336 S += "static"; 1337 else if (getSizeModifier() == Star) 1338 S += '*'; 1339 1340 if (getSizeExpr()) { 1341 std::string SStr; 1342 llvm::raw_string_ostream s(SStr); 1343 getSizeExpr()->printPretty(s, 0, Policy); 1344 S += s.str(); 1345 } 1346 S += ']'; 1347 1348 getElementType().getAsStringInternal(S, Policy); 1349} 1350 1351void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1352 // FIXME: We prefer to print the size directly here, but have no way 1353 // to get the size of the type. 1354 S += " __attribute__((__vector_size__("; 1355 S += llvm::utostr_32(NumElements); // convert back to bytes. 1356 S += " * sizeof(" + ElementType.getAsString() + "))))"; 1357 ElementType.getAsStringInternal(S, Policy); 1358} 1359 1360void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1361 S += " __attribute__((ext_vector_type("; 1362 S += llvm::utostr_32(NumElements); 1363 S += ")))"; 1364 ElementType.getAsStringInternal(S, Policy); 1365} 1366 1367void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1368 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. 1369 InnerString = ' ' + InnerString; 1370 std::string Str; 1371 llvm::raw_string_ostream s(Str); 1372 getUnderlyingExpr()->printPretty(s, 0, Policy); 1373 InnerString = "typeof " + s.str() + InnerString; 1374} 1375 1376void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1377 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. 1378 InnerString = ' ' + InnerString; 1379 std::string Tmp; 1380 getUnderlyingType().getAsStringInternal(Tmp, Policy); 1381 InnerString = "typeof(" + Tmp + ")" + InnerString; 1382} 1383 1384void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1385 // If needed for precedence reasons, wrap the inner part in grouping parens. 1386 if (!S.empty()) 1387 S = "(" + S + ")"; 1388 1389 S += "()"; 1390 getResultType().getAsStringInternal(S, Policy); 1391} 1392 1393void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { 1394 // If needed for precedence reasons, wrap the inner part in grouping parens. 1395 if (!S.empty()) 1396 S = "(" + S + ")"; 1397 1398 S += "("; 1399 std::string Tmp; 1400 PrintingPolicy ParamPolicy(Policy); 1401 ParamPolicy.SuppressTypeSpecifiers = false; 1402 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { 1403 if (i) S += ", "; 1404 getArgType(i).getAsStringInternal(Tmp, ParamPolicy); 1405 S += Tmp; 1406 Tmp.clear(); 1407 } 1408 1409 if (isVariadic()) { 1410 if (getNumArgs()) 1411 S += ", "; 1412 S += "..."; 1413 } else if (getNumArgs() == 0) { 1414 // Do not emit int() if we have a proto, emit 'int(void)'. 1415 S += "void"; 1416 } 1417 1418 S += ")"; 1419 getResultType().getAsStringInternal(S, Policy); 1420} 1421 1422 1423void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1424 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1425 InnerString = ' ' + InnerString; 1426 InnerString = getDecl()->getIdentifier()->getName() + InnerString; 1427} 1428 1429void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1430 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'. 1431 InnerString = ' ' + InnerString; 1432 1433 if (!Name) 1434 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' + 1435 llvm::utostr_32(Index) + InnerString; 1436 else 1437 InnerString = Name->getName() + InnerString; 1438} 1439 1440std::string 1441TemplateSpecializationType::PrintTemplateArgumentList( 1442 const TemplateArgument *Args, 1443 unsigned NumArgs, 1444 const PrintingPolicy &Policy) { 1445 std::string SpecString; 1446 SpecString += '<'; 1447 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1448 if (Arg) 1449 SpecString += ", "; 1450 1451 // Print the argument into a string. 1452 std::string ArgString; 1453 switch (Args[Arg].getKind()) { 1454 case TemplateArgument::Type: 1455 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy); 1456 break; 1457 1458 case TemplateArgument::Declaration: 1459 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString(); 1460 break; 1461 1462 case TemplateArgument::Integral: 1463 ArgString = Args[Arg].getAsIntegral()->toString(10, true); 1464 break; 1465 1466 case TemplateArgument::Expression: { 1467 llvm::raw_string_ostream s(ArgString); 1468 Args[Arg].getAsExpr()->printPretty(s, 0, Policy); 1469 break; 1470 } 1471 } 1472 1473 // If this is the first argument and its string representation 1474 // begins with the global scope specifier ('::foo'), add a space 1475 // to avoid printing the diagraph '<:'. 1476 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1477 SpecString += ' '; 1478 1479 SpecString += ArgString; 1480 } 1481 1482 // If the last character of our string is '>', add another space to 1483 // keep the two '>''s separate tokens. We don't *have* to do this in 1484 // C++0x, but it's still good hygiene. 1485 if (SpecString[SpecString.size() - 1] == '>') 1486 SpecString += ' '; 1487 1488 SpecString += '>'; 1489 1490 return SpecString; 1491} 1492 1493void 1494TemplateSpecializationType:: 1495getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1496 std::string SpecString; 1497 1498 { 1499 llvm::raw_string_ostream OS(SpecString); 1500 Template.print(OS, Policy); 1501 } 1502 1503 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy); 1504 if (InnerString.empty()) 1505 InnerString.swap(SpecString); 1506 else 1507 InnerString = SpecString + ' ' + InnerString; 1508} 1509 1510void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1511 std::string MyString; 1512 1513 { 1514 llvm::raw_string_ostream OS(MyString); 1515 NNS->print(OS, Policy); 1516 } 1517 1518 std::string TypeStr; 1519 PrintingPolicy InnerPolicy(Policy); 1520 InnerPolicy.SuppressTagKind = true; 1521 NamedType.getAsStringInternal(TypeStr, InnerPolicy); 1522 1523 MyString += TypeStr; 1524 if (InnerString.empty()) 1525 InnerString.swap(MyString); 1526 else 1527 InnerString = MyString + ' ' + InnerString; 1528} 1529 1530void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1531 std::string MyString; 1532 1533 { 1534 llvm::raw_string_ostream OS(MyString); 1535 OS << "typename "; 1536 NNS->print(OS, Policy); 1537 1538 if (const IdentifierInfo *Ident = getIdentifier()) 1539 OS << Ident->getName(); 1540 else if (const TemplateSpecializationType *Spec = getTemplateId()) { 1541 Spec->getTemplateName().print(OS, Policy, true); 1542 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1543 Spec->getArgs(), 1544 Spec->getNumArgs(), 1545 Policy); 1546 } 1547 } 1548 1549 if (InnerString.empty()) 1550 InnerString.swap(MyString); 1551 else 1552 InnerString = MyString + ' ' + InnerString; 1553} 1554 1555void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1556 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1557 InnerString = ' ' + InnerString; 1558 InnerString = getDecl()->getIdentifier()->getName() + InnerString; 1559} 1560 1561void 1562ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString, 1563 const PrintingPolicy &Policy) const { 1564 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1565 InnerString = ' ' + InnerString; 1566 std::string ObjCQIString = getDecl()->getNameAsString(); 1567 ObjCQIString += '<'; 1568 bool isFirst = true; 1569 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1570 if (isFirst) 1571 isFirst = false; 1572 else 1573 ObjCQIString += ','; 1574 ObjCQIString += (*I)->getNameAsString(); 1575 } 1576 ObjCQIString += '>'; 1577 InnerString = ObjCQIString + InnerString; 1578} 1579 1580void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1581 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1582 InnerString = ' ' + InnerString; 1583 std::string ObjCQIString = "id"; 1584 ObjCQIString += '<'; 1585 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1586 ObjCQIString += (*I)->getNameAsString(); 1587 if (I+1 != E) 1588 ObjCQIString += ','; 1589 } 1590 ObjCQIString += '>'; 1591 InnerString = ObjCQIString + InnerString; 1592} 1593 1594void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { 1595 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1596 InnerString = ' ' + InnerString; 1597 1598 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName(); 1599 const char *ID; 1600 if (const IdentifierInfo *II = getDecl()->getIdentifier()) 1601 ID = II->getName(); 1602 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) { 1603 Kind = 0; 1604 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 1605 ID = Typedef->getIdentifier()->getName(); 1606 } else 1607 ID = "<anonymous>"; 1608 1609 // If this is a class template specialization, print the template 1610 // arguments. 1611 if (ClassTemplateSpecializationDecl *Spec 1612 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) { 1613 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1614 std::string TemplateArgsStr 1615 = TemplateSpecializationType::PrintTemplateArgumentList( 1616 TemplateArgs.getFlatArgumentList(), 1617 TemplateArgs.flat_size(), 1618 Policy); 1619 InnerString = TemplateArgsStr + InnerString; 1620 } 1621 1622 if (Kind) { 1623 // Compute the full nested-name-specifier for this type. In C, 1624 // this will always be empty. 1625 std::string ContextStr; 1626 for (DeclContext *DC = getDecl()->getDeclContext(); 1627 !DC->isTranslationUnit(); DC = DC->getParent()) { 1628 std::string MyPart; 1629 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 1630 if (NS->getIdentifier()) 1631 MyPart = NS->getNameAsString(); 1632 } else if (ClassTemplateSpecializationDecl *Spec 1633 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 1634 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1635 std::string TemplateArgsStr 1636 = TemplateSpecializationType::PrintTemplateArgumentList( 1637 TemplateArgs.getFlatArgumentList(), 1638 TemplateArgs.flat_size(), 1639 Policy); 1640 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr; 1641 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 1642 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl()) 1643 MyPart = Typedef->getIdentifier()->getName(); 1644 else if (Tag->getIdentifier()) 1645 MyPart = Tag->getIdentifier()->getName(); 1646 } 1647 1648 if (!MyPart.empty()) 1649 ContextStr = MyPart + "::" + ContextStr; 1650 } 1651 1652 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString; 1653 } else 1654 InnerString = ID + InnerString; 1655} 1656