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