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