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