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