Type.cpp revision cdce6d17be572ae49417022357906ce04b54137d
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/Type.h" 15#include "clang/AST/Decl.h" 16#include "clang/AST/DeclObjC.h" 17#include "clang/AST/Expr.h" 18#include "clang/Basic/IdentifierTable.h" 19#include "clang/Basic/TargetInfo.h" 20#include "llvm/Support/Streams.h" 21#include "llvm/ADT/StringExtras.h" 22#include <sstream> 23using namespace clang; 24 25void Type::Destroy(ASTContext& C) { delete this; } 26 27void FunctionTypeProto::Destroy(ASTContext& C) { 28 // Destroy the object, but don't call delete. These are malloc'd. 29 this->~FunctionTypeProto(); 30 free(this); 31} 32 33void VariableArrayType::Destroy(ASTContext& C) { 34 SizeExpr->Destroy(C); 35 delete this; 36} 37 38/// isVoidType - Helper method to determine if this is the 'void' type. 39bool Type::isVoidType() const { 40 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 41 return BT->getKind() == BuiltinType::Void; 42 return false; 43} 44 45bool Type::isObjectType() const { 46 if (isa<FunctionType>(CanonicalType)) 47 return false; 48 else if (CanonicalType->isIncompleteType()) 49 return false; 50 else 51 return true; 52} 53 54bool Type::isDerivedType() const { 55 switch (CanonicalType->getTypeClass()) { 56 case Pointer: 57 case VariableArray: 58 case ConstantArray: 59 case IncompleteArray: 60 case FunctionProto: 61 case FunctionNoProto: 62 case Reference: 63 return true; 64 case Tagged: { 65 const TagType *TT = cast<TagType>(CanonicalType); 66 return !TT->getDecl()->isEnum(); 67 } 68 default: 69 return false; 70 } 71} 72 73bool Type::isClassType() const { 74 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) 75 if (RT->getDecl()->isClass()) 76 return true; 77 return false; 78} 79bool Type::isStructureType() const { 80 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) 81 if (RT->getDecl()->isStruct()) 82 return true; 83 return false; 84} 85bool Type::isUnionType() const { 86 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) 87 if (RT->getDecl()->isUnion()) 88 return true; 89 return false; 90} 91 92bool Type::isComplexType() const { 93 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 94 return CT->getElementType()->isFloatingType(); 95 return false; 96} 97 98bool Type::isComplexIntegerType() const { 99 // Check for GCC complex integer extension. 100 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 101 return CT->getElementType()->isIntegerType(); 102 return false; 103} 104 105const ComplexType *Type::getAsComplexIntegerType() const { 106 // Are we directly a complex type? 107 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) { 108 if (CTy->getElementType()->isIntegerType()) 109 return CTy; 110 } 111 // If the canonical form of this type isn't the right kind, reject it. 112 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType); 113 if (!CTy || !CTy->getElementType()->isIntegerType()) 114 return 0; 115 116 // If this is a typedef for a complex type, strip the typedef off without 117 // losing all typedef information. 118 return getDesugaredType()->getAsComplexIntegerType(); 119} 120 121/// getDesugaredType - Return the specified type with any "sugar" removed from 122/// type type. This takes off typedefs, typeof's etc. If the outer level of 123/// the type is already concrete, it returns it unmodified. This is similar 124/// to getting the canonical type, but it doesn't remove *all* typedefs. For 125/// example, it return "T*" as "T*", (not as "int*"), because the pointer is 126/// concrete. 127const Type *Type::getDesugaredType() const { 128 if (const TypedefType *TDT = dyn_cast<TypedefType>(this)) 129 return TDT->LookThroughTypedefs().getTypePtr(); 130 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this)) 131 return TOE->getUnderlyingExpr()->getType().getTypePtr(); 132 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this)) 133 return TOT->getUnderlyingType().getTypePtr(); 134 return this; 135} 136 137 138const BuiltinType *Type::getAsBuiltinType() const { 139 // If this is directly a builtin type, return it. 140 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this)) 141 return BTy; 142 143 // If the canonical form of this type isn't a builtin type, reject it. 144 if (!isa<BuiltinType>(CanonicalType)) { 145 // Look through type qualifiers 146 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType())) 147 return CanonicalType.getUnqualifiedType()->getAsBuiltinType(); 148 return 0; 149 } 150 151 // If this is a typedef for a builtin type, strip the typedef off without 152 // losing all typedef information. 153 return getDesugaredType()->getAsBuiltinType(); 154} 155 156const FunctionType *Type::getAsFunctionType() const { 157 // If this is directly a function type, return it. 158 if (const FunctionType *FTy = dyn_cast<FunctionType>(this)) 159 return FTy; 160 161 // If the canonical form of this type isn't the right kind, reject it. 162 if (!isa<FunctionType>(CanonicalType)) { 163 // Look through type qualifiers 164 if (isa<FunctionType>(CanonicalType.getUnqualifiedType())) 165 return CanonicalType.getUnqualifiedType()->getAsFunctionType(); 166 return 0; 167 } 168 169 // If this is a typedef for a function type, strip the typedef off without 170 // losing all typedef information. 171 return getDesugaredType()->getAsFunctionType(); 172} 173 174const PointerLikeType *Type::getAsPointerLikeType() const { 175 // If this is directly a pointer-like type, return it. 176 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this)) 177 return PTy; 178 179 // If the canonical form of this type isn't the right kind, reject it. 180 if (!isa<PointerLikeType>(CanonicalType)) { 181 // Look through type qualifiers 182 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType())) 183 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType(); 184 return 0; 185 } 186 187 // If this is a typedef for a pointer type, strip the typedef off without 188 // losing all typedef information. 189 return getDesugaredType()->getAsPointerLikeType(); 190} 191 192const PointerType *Type::getAsPointerType() const { 193 // If this is directly a pointer type, return it. 194 if (const PointerType *PTy = dyn_cast<PointerType>(this)) 195 return PTy; 196 197 // If the canonical form of this type isn't the right kind, reject it. 198 if (!isa<PointerType>(CanonicalType)) { 199 // Look through type qualifiers 200 if (isa<PointerType>(CanonicalType.getUnqualifiedType())) 201 return CanonicalType.getUnqualifiedType()->getAsPointerType(); 202 return 0; 203 } 204 205 // If this is a typedef for a pointer type, strip the typedef off without 206 // losing all typedef information. 207 return getDesugaredType()->getAsPointerType(); 208} 209 210const ReferenceType *Type::getAsReferenceType() const { 211 // If this is directly a reference type, return it. 212 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this)) 213 return RTy; 214 215 // If the canonical form of this type isn't the right kind, reject it. 216 if (!isa<ReferenceType>(CanonicalType)) { 217 // Look through type qualifiers 218 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType())) 219 return CanonicalType.getUnqualifiedType()->getAsReferenceType(); 220 return 0; 221 } 222 223 // If this is a typedef for a reference type, strip the typedef off without 224 // losing all typedef information. 225 return getDesugaredType()->getAsReferenceType(); 226} 227 228const ArrayType *Type::getAsArrayType() const { 229 // If this is directly an array type, return it. 230 if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) 231 return ATy; 232 233 // If the canonical form of this type isn't the right kind, reject it. 234 if (!isa<ArrayType>(CanonicalType)) { 235 // Look through type qualifiers 236 if (isa<ArrayType>(CanonicalType.getUnqualifiedType())) 237 return CanonicalType.getUnqualifiedType()->getAsArrayType(); 238 return 0; 239 } 240 241 // If this is a typedef for an array type, strip the typedef off without 242 // losing all typedef information. 243 return getDesugaredType()->getAsArrayType(); 244} 245 246const ConstantArrayType *Type::getAsConstantArrayType() const { 247 // If this is directly a constant array type, return it. 248 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this)) 249 return ATy; 250 251 // If the canonical form of this type isn't the right kind, reject it. 252 if (!isa<ConstantArrayType>(CanonicalType)) { 253 // Look through type qualifiers 254 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType())) 255 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType(); 256 return 0; 257 } 258 259 // If this is a typedef for a constant array type, strip the typedef off 260 // without losing all typedef information. 261 return getDesugaredType()->getAsConstantArrayType(); 262} 263 264const VariableArrayType *Type::getAsVariableArrayType() const { 265 // If this is directly a variable array type, return it. 266 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this)) 267 return ATy; 268 269 // If the canonical form of this type isn't the right kind, reject it. 270 if (!isa<VariableArrayType>(CanonicalType)) { 271 // Look through type qualifiers 272 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType())) 273 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType(); 274 return 0; 275 } 276 277 // If this is a typedef for a variable array type, strip the typedef off 278 // without losing all typedef information. 279 return getDesugaredType()->getAsVariableArrayType(); 280} 281 282/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length 283/// array types and types that contain variable array types in their 284/// declarator 285bool Type::isVariablyModifiedType() const { 286 // A VLA is a veriably modified type 287 if (getAsVariableArrayType()) 288 return true; 289 290 // An array can contain a variably modified type 291 if (const ArrayType* AT = getAsArrayType()) 292 return AT->getElementType()->isVariablyModifiedType(); 293 294 // A pointer can point to a variably modified type 295 if (const PointerType* PT = getAsPointerType()) 296 return PT->getPointeeType()->isVariablyModifiedType(); 297 298 // A function can return a variably modified type 299 // This one isn't completely obvious, but it follows from the 300 // definition in C99 6.7.5p3. Because of this rule, it's 301 // illegal to declare a function returning a variably modified type. 302 if (const FunctionType* FT = getAsFunctionType()) 303 return FT->getResultType()->isVariablyModifiedType(); 304 305 return false; 306} 307 308bool Type::isIncompleteArrayType() const { 309 return isa<IncompleteArrayType>(CanonicalType); 310} 311 312const IncompleteArrayType *Type::getAsIncompleteArrayType() const { 313 // If this is directly a variable array type, return it. 314 if (const IncompleteArrayType *ATy = dyn_cast<IncompleteArrayType>(this)) 315 return ATy; 316 317 // If the canonical form of this type isn't the right kind, reject it. 318 if (!isa<IncompleteArrayType>(CanonicalType)) { 319 // Look through type qualifiers 320 if (isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType())) 321 return CanonicalType.getUnqualifiedType()->getAsIncompleteArrayType(); 322 return 0; 323 } 324 325 // If this is a typedef for a variable array type, strip the typedef off 326 // without losing all typedef information. 327 return getDesugaredType()->getAsIncompleteArrayType(); 328} 329 330const RecordType *Type::getAsRecordType() const { 331 // If this is directly a reference type, return it. 332 if (const RecordType *RTy = dyn_cast<RecordType>(this)) 333 return RTy; 334 335 // If the canonical form of this type isn't the right kind, reject it. 336 if (!isa<RecordType>(CanonicalType)) { 337 // Look through type qualifiers 338 if (isa<RecordType>(CanonicalType.getUnqualifiedType())) 339 return CanonicalType.getUnqualifiedType()->getAsRecordType(); 340 return 0; 341 } 342 343 // If this is a typedef for a record type, strip the typedef off without 344 // losing all typedef information. 345 return getDesugaredType()->getAsRecordType(); 346} 347 348const RecordType *Type::getAsStructureType() const { 349 // If this is directly a structure type, return it. 350 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 351 if (RT->getDecl()->isStruct()) 352 return RT; 353 } 354 355 // If the canonical form of this type isn't the right kind, reject it. 356 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 357 if (!RT->getDecl()->isStruct()) 358 return 0; 359 360 // If this is a typedef for a structure type, strip the typedef off without 361 // losing all typedef information. 362 return getDesugaredType()->getAsStructureType(); 363 } 364 // Look through type qualifiers 365 if (isa<RecordType>(CanonicalType.getUnqualifiedType())) 366 return CanonicalType.getUnqualifiedType()->getAsStructureType(); 367 return 0; 368} 369 370const RecordType *Type::getAsUnionType() const { 371 // If this is directly a union type, return it. 372 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 373 if (RT->getDecl()->isUnion()) 374 return RT; 375 } 376 377 // If the canonical form of this type isn't the right kind, reject it. 378 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 379 if (!RT->getDecl()->isUnion()) 380 return 0; 381 382 // If this is a typedef for a union type, strip the typedef off without 383 // losing all typedef information. 384 return getDesugaredType()->getAsUnionType(); 385 } 386 387 // Look through type qualifiers 388 if (isa<RecordType>(CanonicalType.getUnqualifiedType())) 389 return CanonicalType.getUnqualifiedType()->getAsUnionType(); 390 return 0; 391} 392 393const EnumType *Type::getAsEnumType() const { 394 // Check the canonicalized unqualified type directly; the more complex 395 // version is unnecessary because there isn't any typedef information 396 // to preserve. 397 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType()); 398} 399 400const ComplexType *Type::getAsComplexType() const { 401 // Are we directly a complex type? 402 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) 403 return CTy; 404 405 // If the canonical form of this type isn't the right kind, reject it. 406 if (!isa<ComplexType>(CanonicalType)) { 407 // Look through type qualifiers 408 if (isa<ComplexType>(CanonicalType.getUnqualifiedType())) 409 return CanonicalType.getUnqualifiedType()->getAsComplexType(); 410 return 0; 411 } 412 413 // If this is a typedef for a complex type, strip the typedef off without 414 // losing all typedef information. 415 return getDesugaredType()->getAsComplexType(); 416} 417 418const VectorType *Type::getAsVectorType() const { 419 // Are we directly a vector type? 420 if (const VectorType *VTy = dyn_cast<VectorType>(this)) 421 return VTy; 422 423 // If the canonical form of this type isn't the right kind, reject it. 424 if (!isa<VectorType>(CanonicalType)) { 425 // Look through type qualifiers 426 if (isa<VectorType>(CanonicalType.getUnqualifiedType())) 427 return CanonicalType.getUnqualifiedType()->getAsVectorType(); 428 return 0; 429 } 430 431 // If this is a typedef for a vector type, strip the typedef off without 432 // losing all typedef information. 433 return getDesugaredType()->getAsVectorType(); 434} 435 436const ExtVectorType *Type::getAsExtVectorType() const { 437 // Are we directly an OpenCU vector type? 438 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this)) 439 return VTy; 440 441 // If the canonical form of this type isn't the right kind, reject it. 442 if (!isa<ExtVectorType>(CanonicalType)) { 443 // Look through type qualifiers 444 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType())) 445 return CanonicalType.getUnqualifiedType()->getAsExtVectorType(); 446 return 0; 447 } 448 449 // If this is a typedef for an extended vector type, strip the typedef off 450 // without losing all typedef information. 451 return getDesugaredType()->getAsExtVectorType(); 452} 453 454const ObjCInterfaceType *Type::getAsObjCInterfaceType() const { 455 // There is no sugar for ObjCInterfaceType's, just return the canonical 456 // type pointer if it is the right class. 457 return dyn_cast<ObjCInterfaceType>(CanonicalType); 458} 459 460const ObjCQualifiedInterfaceType * 461Type::getAsObjCQualifiedInterfaceType() const { 462 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical 463 // type pointer if it is the right class. 464 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType); 465} 466 467const ObjCQualifiedIdType *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 return dyn_cast<ObjCQualifiedIdType>(CanonicalType); 471} 472 473 474bool Type::isIntegerType() const { 475 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 476 return BT->getKind() >= BuiltinType::Bool && 477 BT->getKind() <= BuiltinType::LongLong; 478 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 479 if (TT->getDecl()->isEnum()) 480 return true; 481 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 482 return VT->getElementType()->isIntegerType(); 483 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 484 return ASQT->getBaseType()->isIntegerType(); 485 return false; 486} 487 488bool Type::isIntegralType() const { 489 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 490 return BT->getKind() >= BuiltinType::Bool && 491 BT->getKind() <= BuiltinType::LongLong; 492 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 493 if (TT->getDecl()->isEnum()) 494 return true; 495 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 496 return ASQT->getBaseType()->isIntegralType(); 497 return false; 498} 499 500bool Type::isEnumeralType() const { 501 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 502 return TT->getDecl()->isEnum(); 503 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 504 return ASQT->getBaseType()->isEnumeralType(); 505 return false; 506} 507 508bool Type::isBooleanType() const { 509 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 510 return BT->getKind() == BuiltinType::Bool; 511 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 512 return ASQT->getBaseType()->isBooleanType(); 513 return false; 514} 515 516bool Type::isCharType() const { 517 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 518 return BT->getKind() == BuiltinType::Char_U || 519 BT->getKind() == BuiltinType::UChar || 520 BT->getKind() == BuiltinType::Char_S || 521 BT->getKind() == BuiltinType::SChar; 522 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 523 return ASQT->getBaseType()->isCharType(); 524 return false; 525} 526 527/// isSignedIntegerType - Return true if this is an integer type that is 528/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 529/// an enum decl which has a signed representation, or a vector of signed 530/// integer element type. 531bool Type::isSignedIntegerType() const { 532 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 533 return BT->getKind() >= BuiltinType::Char_S && 534 BT->getKind() <= BuiltinType::LongLong; 535 } 536 537 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 538 return ET->getDecl()->getIntegerType()->isSignedIntegerType(); 539 540 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 541 return VT->getElementType()->isSignedIntegerType(); 542 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 543 return ASQT->getBaseType()->isSignedIntegerType(); 544 return false; 545} 546 547/// isUnsignedIntegerType - Return true if this is an integer type that is 548/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 549/// decl which has an unsigned representation, or a vector of unsigned integer 550/// element type. 551bool Type::isUnsignedIntegerType() const { 552 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 553 return BT->getKind() >= BuiltinType::Bool && 554 BT->getKind() <= BuiltinType::ULongLong; 555 } 556 557 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 558 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); 559 560 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 561 return VT->getElementType()->isUnsignedIntegerType(); 562 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 563 return ASQT->getBaseType()->isUnsignedIntegerType(); 564 return false; 565} 566 567bool Type::isFloatingType() const { 568 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 569 return BT->getKind() >= BuiltinType::Float && 570 BT->getKind() <= BuiltinType::LongDouble; 571 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 572 return CT->getElementType()->isFloatingType(); 573 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 574 return VT->getElementType()->isFloatingType(); 575 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 576 return ASQT->getBaseType()->isFloatingType(); 577 return false; 578} 579 580bool Type::isRealFloatingType() const { 581 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 582 return BT->getKind() >= BuiltinType::Float && 583 BT->getKind() <= BuiltinType::LongDouble; 584 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 585 return VT->getElementType()->isRealFloatingType(); 586 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 587 return ASQT->getBaseType()->isRealFloatingType(); 588 return false; 589} 590 591bool Type::isRealType() const { 592 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 593 return BT->getKind() >= BuiltinType::Bool && 594 BT->getKind() <= BuiltinType::LongDouble; 595 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) 596 return TT->getDecl()->isEnum(); 597 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 598 return VT->getElementType()->isRealType(); 599 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 600 return ASQT->getBaseType()->isRealType(); 601 return false; 602} 603 604bool Type::isArithmeticType() const { 605 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 606 return BT->getKind() != BuiltinType::Void; 607 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 608 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). 609 // If a body isn't seen by the time we get here, return false. 610 return ET->getDecl()->isDefinition(); 611 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 612 return ASQT->getBaseType()->isArithmeticType(); 613 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); 614} 615 616bool Type::isScalarType() const { 617 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 618 return BT->getKind() != BuiltinType::Void; 619 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { 620 if (TT->getDecl()->isEnum()) 621 return true; 622 return false; 623 } 624 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 625 return ASQT->getBaseType()->isScalarType(); 626 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) || 627 isa<ObjCQualifiedIdType>(CanonicalType); 628} 629 630bool Type::isAggregateType() const { 631 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { 632 if (TT->getDecl()->isStruct()) 633 return true; 634 return false; 635 } 636 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 637 return ASQT->getBaseType()->isAggregateType(); 638 return isa<ArrayType>(CanonicalType); 639} 640 641/// isConstantSizeType - Return true if this is not a variable sized type, 642/// according to the rules of C99 6.7.5p3. It is not legal to call this on 643/// incomplete types. 644bool Type::isConstantSizeType() const { 645 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 646 return ASQT->getBaseType()->isConstantSizeType(); 647 assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); 648 // The VAT must have a size, as it is known to be complete. 649 return !isa<VariableArrayType>(CanonicalType); 650} 651 652/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) 653/// - a type that can describe objects, but which lacks information needed to 654/// determine its size. 655bool Type::isIncompleteType() const { 656 switch (CanonicalType->getTypeClass()) { 657 default: return false; 658 case ASQual: 659 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType(); 660 case Builtin: 661 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never 662 // be completed. 663 return isVoidType(); 664 case Tagged: 665 // A tagged type (struct/union/enum/class) is incomplete if the decl is a 666 // forward declaration, but not a full definition (C99 6.2.5p22). 667 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); 668 case IncompleteArray: 669 // An array of unknown size is an incomplete type (C99 6.2.5p22). 670 return true; 671 } 672} 673 674bool Type::isPromotableIntegerType() const { 675 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) 676 return ASQT->getBaseType()->isPromotableIntegerType(); 677 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType); 678 if (!BT) return false; 679 switch (BT->getKind()) { 680 case BuiltinType::Bool: 681 case BuiltinType::Char_S: 682 case BuiltinType::Char_U: 683 case BuiltinType::SChar: 684 case BuiltinType::UChar: 685 case BuiltinType::Short: 686 case BuiltinType::UShort: 687 return true; 688 default: 689 return false; 690 } 691} 692 693const char *BuiltinType::getName() const { 694 switch (getKind()) { 695 default: assert(0 && "Unknown builtin type!"); 696 case Void: return "void"; 697 case Bool: return "_Bool"; 698 case Char_S: return "char"; 699 case Char_U: return "char"; 700 case SChar: return "signed char"; 701 case Short: return "short"; 702 case Int: return "int"; 703 case Long: return "long"; 704 case LongLong: return "long long"; 705 case UChar: return "unsigned char"; 706 case UShort: return "unsigned short"; 707 case UInt: return "unsigned int"; 708 case ULong: return "unsigned long"; 709 case ULongLong: return "unsigned long long"; 710 case Float: return "float"; 711 case Double: return "double"; 712 case LongDouble: return "long double"; 713 } 714} 715 716void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 717 arg_type_iterator ArgTys, 718 unsigned NumArgs, bool isVariadic) { 719 ID.AddPointer(Result.getAsOpaquePtr()); 720 for (unsigned i = 0; i != NumArgs; ++i) 721 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 722 ID.AddInteger(isVariadic); 723} 724 725void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) { 726 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic()); 727} 728 729void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID, 730 const ObjCInterfaceDecl *Decl, 731 ObjCProtocolDecl **protocols, 732 unsigned NumProtocols) { 733 ID.AddPointer(Decl); 734 for (unsigned i = 0; i != NumProtocols; i++) 735 ID.AddPointer(protocols[i]); 736} 737 738void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { 739 Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); 740} 741 742void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID, 743 ObjCProtocolDecl **protocols, 744 unsigned NumProtocols) { 745 for (unsigned i = 0; i != NumProtocols; i++) 746 ID.AddPointer(protocols[i]); 747} 748 749void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) { 750 Profile(ID, &Protocols[0], getNumProtocols()); 751} 752 753/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 754/// potentially looking through *all* consequtive typedefs. This returns the 755/// sum of the type qualifiers, so if you have: 756/// typedef const int A; 757/// typedef volatile A B; 758/// looking through the typedefs for B will give you "const volatile A". 759/// 760QualType TypedefType::LookThroughTypedefs() const { 761 // Usually, there is only a single level of typedefs, be fast in that case. 762 QualType FirstType = getDecl()->getUnderlyingType(); 763 if (!isa<TypedefType>(FirstType)) 764 return FirstType; 765 766 // Otherwise, do the fully general loop. 767 unsigned TypeQuals = 0; 768 const TypedefType *TDT = this; 769 while (1) { 770 QualType CurType = TDT->getDecl()->getUnderlyingType(); 771 772 773 /// FIXME: 774 /// FIXME: This is incorrect for ASQuals! 775 /// FIXME: 776 TypeQuals |= CurType.getCVRQualifiers(); 777 778 TDT = dyn_cast<TypedefType>(CurType); 779 if (TDT == 0) 780 return QualType(CurType.getTypePtr(), TypeQuals); 781 } 782} 783 784bool RecordType::classof(const TagType *TT) { 785 return isa<RecordDecl>(TT->getDecl()); 786} 787 788bool EnumType::classof(const TagType *TT) { 789 return isa<EnumDecl>(TT->getDecl()); 790} 791 792 793//===----------------------------------------------------------------------===// 794// Type Printing 795//===----------------------------------------------------------------------===// 796 797void QualType::dump(const char *msg) const { 798 std::string R = "identifier"; 799 getAsStringInternal(R); 800 if (msg) 801 fprintf(stderr, "%s: %s\n", msg, R.c_str()); 802 else 803 fprintf(stderr, "%s\n", R.c_str()); 804} 805 806static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { 807 // Note: funkiness to ensure we get a space only between quals. 808 bool NonePrinted = true; 809 if (TypeQuals & QualType::Const) 810 S += "const", NonePrinted = false; 811 if (TypeQuals & QualType::Volatile) 812 S += (NonePrinted+" volatile"), NonePrinted = false; 813 if (TypeQuals & QualType::Restrict) 814 S += (NonePrinted+" restrict"), NonePrinted = false; 815} 816 817void QualType::getAsStringInternal(std::string &S) const { 818 if (isNull()) { 819 S += "NULL TYPE\n"; 820 return; 821 } 822 823 // Print qualifiers as appropriate. 824 if (unsigned Tq = getCVRQualifiers()) { 825 std::string TQS; 826 AppendTypeQualList(TQS, Tq); 827 if (!S.empty()) 828 S = TQS + ' ' + S; 829 else 830 S = TQS; 831 } 832 833 getTypePtr()->getAsStringInternal(S); 834} 835 836void BuiltinType::getAsStringInternal(std::string &S) const { 837 if (S.empty()) { 838 S = getName(); 839 } else { 840 // Prefix the basic type, e.g. 'int X'. 841 S = ' ' + S; 842 S = getName() + S; 843 } 844} 845 846void ComplexType::getAsStringInternal(std::string &S) const { 847 ElementType->getAsStringInternal(S); 848 S = "_Complex " + S; 849} 850 851void ASQualType::getAsStringInternal(std::string &S) const { 852 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S; 853 BaseType->getAsStringInternal(S); 854} 855 856void PointerType::getAsStringInternal(std::string &S) const { 857 S = '*' + S; 858 859 // Handle things like 'int (*A)[4];' correctly. 860 // FIXME: this should include vectors, but vectors use attributes I guess. 861 if (isa<ArrayType>(getPointeeType())) 862 S = '(' + S + ')'; 863 864 getPointeeType().getAsStringInternal(S); 865} 866 867void ReferenceType::getAsStringInternal(std::string &S) const { 868 S = '&' + S; 869 870 // Handle things like 'int (&A)[4];' correctly. 871 // FIXME: this should include vectors, but vectors use attributes I guess. 872 if (isa<ArrayType>(getPointeeType())) 873 S = '(' + S + ')'; 874 875 getPointeeType().getAsStringInternal(S); 876} 877 878void ConstantArrayType::getAsStringInternal(std::string &S) const { 879 S += '['; 880 S += llvm::utostr(getSize().getZExtValue()); 881 S += ']'; 882 883 getElementType().getAsStringInternal(S); 884} 885 886void IncompleteArrayType::getAsStringInternal(std::string &S) const { 887 S += "[]"; 888 889 getElementType().getAsStringInternal(S); 890} 891 892void VariableArrayType::getAsStringInternal(std::string &S) const { 893 S += '['; 894 895 if (getIndexTypeQualifier()) { 896 AppendTypeQualList(S, getIndexTypeQualifier()); 897 S += ' '; 898 } 899 900 if (getSizeModifier() == Static) 901 S += "static"; 902 else if (getSizeModifier() == Star) 903 S += '*'; 904 905 if (getSizeExpr()) { 906 std::ostringstream s; 907 getSizeExpr()->printPretty(s); 908 S += s.str(); 909 } 910 S += ']'; 911 912 getElementType().getAsStringInternal(S); 913} 914 915void VectorType::getAsStringInternal(std::string &S) const { 916 S += " __attribute__((__vector_size__("; 917 // FIXME: should multiply by element size somehow. 918 S += llvm::utostr_32(NumElements*4); // convert back to bytes. 919 S += ")))"; 920 ElementType.getAsStringInternal(S); 921} 922 923void ExtVectorType::getAsStringInternal(std::string &S) const { 924 S += " __attribute__((ext_vector_type("; 925 S += llvm::utostr_32(NumElements); 926 S += ")))"; 927 ElementType.getAsStringInternal(S); 928} 929 930void TypeOfExpr::getAsStringInternal(std::string &InnerString) const { 931 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. 932 InnerString = ' ' + InnerString; 933 std::ostringstream s; 934 getUnderlyingExpr()->printPretty(s); 935 InnerString = "typeof(" + s.str() + ")" + InnerString; 936} 937 938void TypeOfType::getAsStringInternal(std::string &InnerString) const { 939 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. 940 InnerString = ' ' + InnerString; 941 std::string Tmp; 942 getUnderlyingType().getAsStringInternal(Tmp); 943 InnerString = "typeof(" + Tmp + ")" + InnerString; 944} 945 946void FunctionTypeNoProto::getAsStringInternal(std::string &S) const { 947 // If needed for precedence reasons, wrap the inner part in grouping parens. 948 if (!S.empty()) 949 S = "(" + S + ")"; 950 951 S += "()"; 952 getResultType().getAsStringInternal(S); 953} 954 955void FunctionTypeProto::getAsStringInternal(std::string &S) const { 956 // If needed for precedence reasons, wrap the inner part in grouping parens. 957 if (!S.empty()) 958 S = "(" + S + ")"; 959 960 S += "("; 961 std::string Tmp; 962 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { 963 if (i) S += ", "; 964 getArgType(i).getAsStringInternal(Tmp); 965 S += Tmp; 966 Tmp.clear(); 967 } 968 969 if (isVariadic()) { 970 if (getNumArgs()) 971 S += ", "; 972 S += "..."; 973 } else if (getNumArgs() == 0) { 974 // Do not emit int() if we have a proto, emit 'int(void)'. 975 S += "void"; 976 } 977 978 S += ")"; 979 getResultType().getAsStringInternal(S); 980} 981 982 983void TypedefType::getAsStringInternal(std::string &InnerString) const { 984 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 985 InnerString = ' ' + InnerString; 986 InnerString = getDecl()->getIdentifier()->getName() + InnerString; 987} 988 989void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const { 990 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 991 InnerString = ' ' + InnerString; 992 InnerString = getDecl()->getIdentifier()->getName() + InnerString; 993} 994 995void ObjCQualifiedInterfaceType::getAsStringInternal( 996 std::string &InnerString) const { 997 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 998 InnerString = ' ' + InnerString; 999 std::string ObjCQIString = getDecl()->getName(); 1000 ObjCQIString += '<'; 1001 bool isFirst = true; 1002 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { 1003 if (isFirst) 1004 isFirst = false; 1005 else 1006 ObjCQIString += ','; 1007 ObjCQIString += (*I)->getName(); 1008 } 1009 ObjCQIString += '>'; 1010 InnerString = ObjCQIString + InnerString; 1011} 1012 1013void ObjCQualifiedIdType::getAsStringInternal( 1014 std::string &InnerString) const { 1015 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1016 InnerString = ' ' + InnerString; 1017 std::string ObjCQIString = "id"; 1018 ObjCQIString += '<'; 1019 int num = getNumProtocols(); 1020 for (int i = 0; i < num; i++) { 1021 ObjCQIString += getProtocols(i)->getName(); 1022 if (i < num-1) 1023 ObjCQIString += ','; 1024 } 1025 ObjCQIString += '>'; 1026 InnerString = ObjCQIString + InnerString; 1027} 1028 1029void TagType::getAsStringInternal(std::string &InnerString) const { 1030 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1031 InnerString = ' ' + InnerString; 1032 1033 const char *Kind = getDecl()->getKindName(); 1034 const char *ID; 1035 if (const IdentifierInfo *II = getDecl()->getIdentifier()) 1036 ID = II->getName(); 1037 else 1038 ID = "<anonymous>"; 1039 1040 InnerString = std::string(Kind) + " " + ID + InnerString; 1041} 1042