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