1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221 |
- //===--- Type.cpp - Type representation and manipulation ------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- //
- // This file implements type-related functionality.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/CharUnits.h"
- #include "clang/AST/Type.h"
- #include "clang/AST/DeclCXX.h"
- #include "clang/AST/DeclObjC.h"
- #include "clang/AST/DeclTemplate.h"
- #include "clang/AST/Expr.h"
- #include "clang/AST/PrettyPrinter.h"
- #include "clang/AST/TypeVisitor.h"
- #include "clang/Basic/Specifiers.h"
- #include "llvm/ADT/APSInt.h"
- #include "llvm/ADT/StringExtras.h"
- #include "llvm/Support/raw_ostream.h"
- #include <algorithm>
- using namespace clang;
- bool Qualifiers::isStrictSupersetOf(Qualifiers Other) const {
- return (*this != Other) &&
- // CVR qualifiers superset
- (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) &&
- // ObjC GC qualifiers superset
- ((getObjCGCAttr() == Other.getObjCGCAttr()) ||
- (hasObjCGCAttr() && !Other.hasObjCGCAttr())) &&
- // Address space superset.
- ((getAddressSpace() == Other.getAddressSpace()) ||
- (hasAddressSpace()&& !Other.hasAddressSpace())) &&
- // Lifetime qualifier superset.
- ((getObjCLifetime() == Other.getObjCLifetime()) ||
- (hasObjCLifetime() && !Other.hasObjCLifetime()));
- }
- bool QualType::isConstant(QualType T, ASTContext &Ctx) {
- if (T.isConstQualified())
- return true;
- if (const ArrayType *AT = Ctx.getAsArrayType(T))
- return AT->getElementType().isConstant(Ctx);
- return false;
- }
- unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
- QualType ElementType,
- const llvm::APInt &NumElements) {
- llvm::APSInt SizeExtended(NumElements, true);
- unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
- SizeExtended = SizeExtended.extend(std::max(SizeTypeBits,
- SizeExtended.getBitWidth()) * 2);
- uint64_t ElementSize
- = Context.getTypeSizeInChars(ElementType).getQuantity();
- llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
- TotalSize *= SizeExtended;
-
- return TotalSize.getActiveBits();
- }
- unsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) {
- unsigned Bits = Context.getTypeSize(Context.getSizeType());
-
- // GCC appears to only allow 63 bits worth of address space when compiling
- // for 64-bit, so we do the same.
- if (Bits == 64)
- --Bits;
-
- return Bits;
- }
- DependentSizedArrayType::DependentSizedArrayType(const ASTContext &Context,
- QualType et, QualType can,
- Expr *e, ArraySizeModifier sm,
- unsigned tq,
- SourceRange brackets)
- : ArrayType(DependentSizedArray, et, can, sm, tq,
- (et->containsUnexpandedParameterPack() ||
- (e && e->containsUnexpandedParameterPack()))),
- Context(Context), SizeExpr((Stmt*) e), Brackets(brackets)
- {
- }
- void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
- const ASTContext &Context,
- QualType ET,
- ArraySizeModifier SizeMod,
- unsigned TypeQuals,
- Expr *E) {
- ID.AddPointer(ET.getAsOpaquePtr());
- ID.AddInteger(SizeMod);
- ID.AddInteger(TypeQuals);
- E->Profile(ID, Context, true);
- }
- DependentSizedExtVectorType::DependentSizedExtVectorType(const
- ASTContext &Context,
- QualType ElementType,
- QualType can,
- Expr *SizeExpr,
- SourceLocation loc)
- : Type(DependentSizedExtVector, can, /*Dependent=*/true,
- /*InstantiationDependent=*/true,
- ElementType->isVariablyModifiedType(),
- (ElementType->containsUnexpandedParameterPack() ||
- (SizeExpr && SizeExpr->containsUnexpandedParameterPack()))),
- Context(Context), SizeExpr(SizeExpr), ElementType(ElementType),
- loc(loc)
- {
- }
- void
- DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
- const ASTContext &Context,
- QualType ElementType, Expr *SizeExpr) {
- ID.AddPointer(ElementType.getAsOpaquePtr());
- SizeExpr->Profile(ID, Context, true);
- }
- VectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType,
- VectorKind vecKind)
- : Type(Vector, canonType, vecType->isDependentType(),
- vecType->isInstantiationDependentType(),
- vecType->isVariablyModifiedType(),
- vecType->containsUnexpandedParameterPack()),
- ElementType(vecType)
- {
- VectorTypeBits.VecKind = vecKind;
- VectorTypeBits.NumElements = nElements;
- }
- VectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements,
- QualType canonType, VectorKind vecKind)
- : Type(tc, canonType, vecType->isDependentType(),
- vecType->isInstantiationDependentType(),
- vecType->isVariablyModifiedType(),
- vecType->containsUnexpandedParameterPack()),
- ElementType(vecType)
- {
- VectorTypeBits.VecKind = vecKind;
- VectorTypeBits.NumElements = nElements;
- }
- /// getArrayElementTypeNoTypeQual - If this is an array type, return the
- /// element type of the array, potentially with type qualifiers missing.
- /// This method should never be used when type qualifiers are meaningful.
- const Type *Type::getArrayElementTypeNoTypeQual() const {
- // If this is directly an array type, return it.
- if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
- return ATy->getElementType().getTypePtr();
- // If the canonical form of this type isn't the right kind, reject it.
- if (!isa<ArrayType>(CanonicalType))
- return 0;
- // If this is a typedef for an array type, strip the typedef off without
- // losing all typedef information.
- return cast<ArrayType>(getUnqualifiedDesugaredType())
- ->getElementType().getTypePtr();
- }
- /// getDesugaredType - Return the specified type with any "sugar" removed from
- /// the type. This takes off typedefs, typeof's etc. If the outer level of
- /// the type is already concrete, it returns it unmodified. This is similar
- /// to getting the canonical type, but it doesn't remove *all* typedefs. For
- /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
- /// concrete.
- QualType QualType::getDesugaredType(QualType T, const ASTContext &Context) {
- SplitQualType split = getSplitDesugaredType(T);
- return Context.getQualifiedType(split.first, split.second);
- }
- SplitQualType QualType::getSplitDesugaredType(QualType T) {
- QualifierCollector Qs;
- QualType Cur = T;
- while (true) {
- const Type *CurTy = Qs.strip(Cur);
- switch (CurTy->getTypeClass()) {
- #define ABSTRACT_TYPE(Class, Parent)
- #define TYPE(Class, Parent) \
- case Type::Class: { \
- const Class##Type *Ty = cast<Class##Type>(CurTy); \
- if (!Ty->isSugared()) \
- return SplitQualType(Ty, Qs); \
- Cur = Ty->desugar(); \
- break; \
- }
- #include "clang/AST/TypeNodes.def"
- }
- }
- }
- SplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) {
- SplitQualType split = type.split();
- // All the qualifiers we've seen so far.
- Qualifiers quals = split.second;
- // The last type node we saw with any nodes inside it.
- const Type *lastTypeWithQuals = split.first;
- while (true) {
- QualType next;
- // Do a single-step desugar, aborting the loop if the type isn't
- // sugared.
- switch (split.first->getTypeClass()) {
- #define ABSTRACT_TYPE(Class, Parent)
- #define TYPE(Class, Parent) \
- case Type::Class: { \
- const Class##Type *ty = cast<Class##Type>(split.first); \
- if (!ty->isSugared()) goto done; \
- next = ty->desugar(); \
- break; \
- }
- #include "clang/AST/TypeNodes.def"
- }
- // Otherwise, split the underlying type. If that yields qualifiers,
- // update the information.
- split = next.split();
- if (!split.second.empty()) {
- lastTypeWithQuals = split.first;
- quals.addConsistentQualifiers(split.second);
- }
- }
- done:
- return SplitQualType(lastTypeWithQuals, quals);
- }
- QualType QualType::IgnoreParens(QualType T) {
- // FIXME: this seems inherently un-qualifiers-safe.
- while (const ParenType *PT = T->getAs<ParenType>())
- T = PT->getInnerType();
- return T;
- }
- /// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
- /// sugar off the given type. This should produce an object of the
- /// same dynamic type as the canonical type.
- const Type *Type::getUnqualifiedDesugaredType() const {
- const Type *Cur = this;
- while (true) {
- switch (Cur->getTypeClass()) {
- #define ABSTRACT_TYPE(Class, Parent)
- #define TYPE(Class, Parent) \
- case Class: { \
- const Class##Type *Ty = cast<Class##Type>(Cur); \
- if (!Ty->isSugared()) return Cur; \
- Cur = Ty->desugar().getTypePtr(); \
- break; \
- }
- #include "clang/AST/TypeNodes.def"
- }
- }
- }
- /// isVoidType - Helper method to determine if this is the 'void' type.
- bool Type::isVoidType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() == BuiltinType::Void;
- return false;
- }
- bool Type::isDerivedType() const {
- switch (CanonicalType->getTypeClass()) {
- case Pointer:
- case VariableArray:
- case ConstantArray:
- case IncompleteArray:
- case FunctionProto:
- case FunctionNoProto:
- case LValueReference:
- case RValueReference:
- case Record:
- return true;
- default:
- return false;
- }
- }
- bool Type::isClassType() const {
- if (const RecordType *RT = getAs<RecordType>())
- return RT->getDecl()->isClass();
- return false;
- }
- bool Type::isStructureType() const {
- if (const RecordType *RT = getAs<RecordType>())
- return RT->getDecl()->isStruct();
- return false;
- }
- bool Type::isStructureOrClassType() const {
- if (const RecordType *RT = getAs<RecordType>())
- return RT->getDecl()->isStruct() || RT->getDecl()->isClass();
- return false;
- }
- bool Type::isVoidPointerType() const {
- if (const PointerType *PT = getAs<PointerType>())
- return PT->getPointeeType()->isVoidType();
- return false;
- }
- bool Type::isUnionType() const {
- if (const RecordType *RT = getAs<RecordType>())
- return RT->getDecl()->isUnion();
- return false;
- }
- bool Type::isComplexType() const {
- if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
- return CT->getElementType()->isFloatingType();
- return false;
- }
- bool Type::isComplexIntegerType() const {
- // Check for GCC complex integer extension.
- return getAsComplexIntegerType();
- }
- const ComplexType *Type::getAsComplexIntegerType() const {
- if (const ComplexType *Complex = getAs<ComplexType>())
- if (Complex->getElementType()->isIntegerType())
- return Complex;
- return 0;
- }
- QualType Type::getPointeeType() const {
- if (const PointerType *PT = getAs<PointerType>())
- return PT->getPointeeType();
- if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
- return OPT->getPointeeType();
- if (const BlockPointerType *BPT = getAs<BlockPointerType>())
- return BPT->getPointeeType();
- if (const ReferenceType *RT = getAs<ReferenceType>())
- return RT->getPointeeType();
- return QualType();
- }
- const RecordType *Type::getAsStructureType() const {
- // If this is directly a structure type, return it.
- if (const RecordType *RT = dyn_cast<RecordType>(this)) {
- if (RT->getDecl()->isStruct())
- return RT;
- }
- // If the canonical form of this type isn't the right kind, reject it.
- if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
- if (!RT->getDecl()->isStruct())
- return 0;
- // If this is a typedef for a structure type, strip the typedef off without
- // losing all typedef information.
- return cast<RecordType>(getUnqualifiedDesugaredType());
- }
- return 0;
- }
- const RecordType *Type::getAsUnionType() const {
- // If this is directly a union type, return it.
- if (const RecordType *RT = dyn_cast<RecordType>(this)) {
- if (RT->getDecl()->isUnion())
- return RT;
- }
- // If the canonical form of this type isn't the right kind, reject it.
- if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
- if (!RT->getDecl()->isUnion())
- return 0;
- // If this is a typedef for a union type, strip the typedef off without
- // losing all typedef information.
- return cast<RecordType>(getUnqualifiedDesugaredType());
- }
- return 0;
- }
- ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
- ObjCProtocolDecl * const *Protocols,
- unsigned NumProtocols)
- : Type(ObjCObject, Canonical, false, false, false, false),
- BaseType(Base)
- {
- ObjCObjectTypeBits.NumProtocols = NumProtocols;
- assert(getNumProtocols() == NumProtocols &&
- "bitfield overflow in protocol count");
- if (NumProtocols)
- memcpy(getProtocolStorage(), Protocols,
- NumProtocols * sizeof(ObjCProtocolDecl*));
- }
- const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
- // There is no sugar for ObjCObjectType's, just return the canonical
- // type pointer if it is the right class. There is no typedef information to
- // return and these cannot be Address-space qualified.
- if (const ObjCObjectType *T = getAs<ObjCObjectType>())
- if (T->getNumProtocols() && T->getInterface())
- return T;
- return 0;
- }
- bool Type::isObjCQualifiedInterfaceType() const {
- return getAsObjCQualifiedInterfaceType() != 0;
- }
- const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
- // There is no sugar for ObjCQualifiedIdType's, just return the canonical
- // type pointer if it is the right class.
- if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
- if (OPT->isObjCQualifiedIdType())
- return OPT;
- }
- return 0;
- }
- const ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const {
- // There is no sugar for ObjCQualifiedClassType's, just return the canonical
- // type pointer if it is the right class.
- if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
- if (OPT->isObjCQualifiedClassType())
- return OPT;
- }
- return 0;
- }
- const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
- if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
- if (OPT->getInterfaceType())
- return OPT;
- }
- return 0;
- }
- const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
- if (const PointerType *PT = getAs<PointerType>())
- if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
- return dyn_cast<CXXRecordDecl>(RT->getDecl());
- return 0;
- }
- CXXRecordDecl *Type::getAsCXXRecordDecl() const {
- if (const RecordType *RT = getAs<RecordType>())
- return dyn_cast<CXXRecordDecl>(RT->getDecl());
- else if (const InjectedClassNameType *Injected
- = getAs<InjectedClassNameType>())
- return Injected->getDecl();
-
- return 0;
- }
- namespace {
- class GetContainedAutoVisitor :
- public TypeVisitor<GetContainedAutoVisitor, AutoType*> {
- public:
- using TypeVisitor<GetContainedAutoVisitor, AutoType*>::Visit;
- AutoType *Visit(QualType T) {
- if (T.isNull())
- return 0;
- return Visit(T.getTypePtr());
- }
- // The 'auto' type itself.
- AutoType *VisitAutoType(const AutoType *AT) {
- return const_cast<AutoType*>(AT);
- }
- // Only these types can contain the desired 'auto' type.
- AutoType *VisitPointerType(const PointerType *T) {
- return Visit(T->getPointeeType());
- }
- AutoType *VisitBlockPointerType(const BlockPointerType *T) {
- return Visit(T->getPointeeType());
- }
- AutoType *VisitReferenceType(const ReferenceType *T) {
- return Visit(T->getPointeeTypeAsWritten());
- }
- AutoType *VisitMemberPointerType(const MemberPointerType *T) {
- return Visit(T->getPointeeType());
- }
- AutoType *VisitArrayType(const ArrayType *T) {
- return Visit(T->getElementType());
- }
- AutoType *VisitDependentSizedExtVectorType(
- const DependentSizedExtVectorType *T) {
- return Visit(T->getElementType());
- }
- AutoType *VisitVectorType(const VectorType *T) {
- return Visit(T->getElementType());
- }
- AutoType *VisitFunctionType(const FunctionType *T) {
- return Visit(T->getResultType());
- }
- AutoType *VisitParenType(const ParenType *T) {
- return Visit(T->getInnerType());
- }
- AutoType *VisitAttributedType(const AttributedType *T) {
- return Visit(T->getModifiedType());
- }
- };
- }
- AutoType *Type::getContainedAutoType() const {
- return GetContainedAutoVisitor().Visit(this);
- }
- bool Type::isIntegerType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() >= BuiltinType::Bool &&
- BT->getKind() <= BuiltinType::Int128;
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
- // Incomplete enum types are not treated as integer types.
- // FIXME: In C++, enum types are never integer types.
- return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
- return false;
- }
- bool Type::hasIntegerRepresentation() const {
- if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
- return VT->getElementType()->isIntegerType();
- else
- return isIntegerType();
- }
- /// \brief Determine whether this type is an integral type.
- ///
- /// This routine determines whether the given type is an integral type per
- /// C++ [basic.fundamental]p7. Although the C standard does not define the
- /// term "integral type", it has a similar term "integer type", and in C++
- /// the two terms are equivalent. However, C's "integer type" includes
- /// enumeration types, while C++'s "integer type" does not. The \c ASTContext
- /// parameter is used to determine whether we should be following the C or
- /// C++ rules when determining whether this type is an integral/integer type.
- ///
- /// For cases where C permits "an integer type" and C++ permits "an integral
- /// type", use this routine.
- ///
- /// For cases where C permits "an integer type" and C++ permits "an integral
- /// or enumeration type", use \c isIntegralOrEnumerationType() instead.
- ///
- /// \param Ctx The context in which this type occurs.
- ///
- /// \returns true if the type is considered an integral type, false otherwise.
- bool Type::isIntegralType(ASTContext &Ctx) const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() >= BuiltinType::Bool &&
- BT->getKind() <= BuiltinType::Int128;
-
- if (!Ctx.getLangOptions().CPlusPlus)
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
- return ET->getDecl()->isComplete(); // Complete enum types are integral in C.
-
- return false;
- }
- bool Type::isIntegralOrEnumerationType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() >= BuiltinType::Bool &&
- BT->getKind() <= BuiltinType::Int128;
- // Check for a complete enum type; incomplete enum types are not properly an
- // enumeration type in the sense required here.
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
- return ET->getDecl()->isComplete();
- return false;
- }
- bool Type::isIntegralOrUnscopedEnumerationType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() >= BuiltinType::Bool &&
- BT->getKind() <= BuiltinType::Int128;
- // Check for a complete enum type; incomplete enum types are not properly an
- // enumeration type in the sense required here.
- // C++0x: However, if the underlying type of the enum is fixed, it is
- // considered complete.
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
- return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
- return false;
- }
- bool Type::isBooleanType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() == BuiltinType::Bool;
- return false;
- }
- bool Type::isCharType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() == BuiltinType::Char_U ||
- BT->getKind() == BuiltinType::UChar ||
- BT->getKind() == BuiltinType::Char_S ||
- BT->getKind() == BuiltinType::SChar;
- return false;
- }
- bool Type::isWideCharType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() == BuiltinType::WChar_S ||
- BT->getKind() == BuiltinType::WChar_U;
- return false;
- }
- /// \brief Determine whether this type is any of the built-in character
- /// types.
- bool Type::isAnyCharacterType() const {
- const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
- if (BT == 0) return false;
- switch (BT->getKind()) {
- default: return false;
- case BuiltinType::Char_U:
- case BuiltinType::UChar:
- case BuiltinType::WChar_U:
- case BuiltinType::Char16:
- case BuiltinType::Char32:
- case BuiltinType::Char_S:
- case BuiltinType::SChar:
- case BuiltinType::WChar_S:
- return true;
- }
- }
- /// isSignedIntegerType - Return true if this is an integer type that is
- /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
- /// an enum decl which has a signed representation
- bool Type::isSignedIntegerType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
- return BT->getKind() >= BuiltinType::Char_S &&
- BT->getKind() <= BuiltinType::Int128;
- }
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
- // Incomplete enum types are not treated as integer types.
- // FIXME: In C++, enum types are never integer types.
- if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
- return ET->getDecl()->getIntegerType()->isSignedIntegerType();
- }
- return false;
- }
- bool Type::isSignedIntegerOrEnumerationType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
- return BT->getKind() >= BuiltinType::Char_S &&
- BT->getKind() <= BuiltinType::Int128;
- }
-
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
- if (ET->getDecl()->isComplete())
- return ET->getDecl()->getIntegerType()->isSignedIntegerType();
- }
-
- return false;
- }
- bool Type::hasSignedIntegerRepresentation() const {
- if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
- return VT->getElementType()->isSignedIntegerType();
- else
- return isSignedIntegerType();
- }
- /// isUnsignedIntegerType - Return true if this is an integer type that is
- /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
- /// decl which has an unsigned representation
- bool Type::isUnsignedIntegerType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
- return BT->getKind() >= BuiltinType::Bool &&
- BT->getKind() <= BuiltinType::UInt128;
- }
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
- // Incomplete enum types are not treated as integer types.
- // FIXME: In C++, enum types are never integer types.
- if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
- return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
- }
- return false;
- }
- bool Type::isUnsignedIntegerOrEnumerationType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
- return BT->getKind() >= BuiltinType::Bool &&
- BT->getKind() <= BuiltinType::UInt128;
- }
-
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
- if (ET->getDecl()->isComplete())
- return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
- }
-
- return false;
- }
- bool Type::hasUnsignedIntegerRepresentation() const {
- if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
- return VT->getElementType()->isUnsignedIntegerType();
- else
- return isUnsignedIntegerType();
- }
- bool Type::isFloatingType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() >= BuiltinType::Float &&
- BT->getKind() <= BuiltinType::LongDouble;
- if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
- return CT->getElementType()->isFloatingType();
- return false;
- }
- bool Type::hasFloatingRepresentation() const {
- if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
- return VT->getElementType()->isFloatingType();
- else
- return isFloatingType();
- }
- bool Type::isRealFloatingType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->isFloatingPoint();
- return false;
- }
- bool Type::isRealType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() >= BuiltinType::Bool &&
- BT->getKind() <= BuiltinType::LongDouble;
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
- return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
- return false;
- }
- bool Type::isArithmeticType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() >= BuiltinType::Bool &&
- BT->getKind() <= BuiltinType::LongDouble;
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
- // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
- // If a body isn't seen by the time we get here, return false.
- //
- // C++0x: Enumerations are not arithmetic types. For now, just return
- // false for scoped enumerations since that will disable any
- // unwanted implicit conversions.
- return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete();
- return isa<ComplexType>(CanonicalType);
- }
- bool Type::isScalarType() const {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
- return BT->getKind() > BuiltinType::Void &&
- BT->getKind() <= BuiltinType::NullPtr;
- if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
- // Enums are scalar types, but only if they are defined. Incomplete enums
- // are not treated as scalar types.
- return ET->getDecl()->isComplete();
- return isa<PointerType>(CanonicalType) ||
- isa<BlockPointerType>(CanonicalType) ||
- isa<MemberPointerType>(CanonicalType) ||
- isa<ComplexType>(CanonicalType) ||
- isa<ObjCObjectPointerType>(CanonicalType);
- }
- Type::ScalarTypeKind Type::getScalarTypeKind() const {
- assert(isScalarType());
- const Type *T = CanonicalType.getTypePtr();
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
- if (BT->getKind() == BuiltinType::Bool) return STK_Bool;
- if (BT->getKind() == BuiltinType::NullPtr) return STK_Pointer;
- if (BT->isInteger()) return STK_Integral;
- if (BT->isFloatingPoint()) return STK_Floating;
- llvm_unreachable("unknown scalar builtin type");
- } else if (isa<PointerType>(T) ||
- isa<BlockPointerType>(T) ||
- isa<ObjCObjectPointerType>(T)) {
- return STK_Pointer;
- } else if (isa<MemberPointerType>(T)) {
- return STK_MemberPointer;
- } else if (isa<EnumType>(T)) {
- assert(cast<EnumType>(T)->getDecl()->isComplete());
- return STK_Integral;
- } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) {
- if (CT->getElementType()->isRealFloatingType())
- return STK_FloatingComplex;
- return STK_IntegralComplex;
- }
- llvm_unreachable("unknown scalar type");
- return STK_Pointer;
- }
- /// \brief Determines whether the type is a C++ aggregate type or C
- /// aggregate or union type.
- ///
- /// An aggregate type is an array or a class type (struct, union, or
- /// class) that has no user-declared constructors, no private or
- /// protected non-static data members, no base classes, and no virtual
- /// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
- /// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
- /// includes union types.
- bool Type::isAggregateType() const {
- if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
- if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
- return ClassDecl->isAggregate();
- return true;
- }
- return isa<ArrayType>(CanonicalType);
- }
- /// isConstantSizeType - Return true if this is not a variable sized type,
- /// according to the rules of C99 6.7.5p3. It is not legal to call this on
- /// incomplete types or dependent types.
- bool Type::isConstantSizeType() const {
- assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
- assert(!isDependentType() && "This doesn't make sense for dependent types");
- // The VAT must have a size, as it is known to be complete.
- return !isa<VariableArrayType>(CanonicalType);
- }
- /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
- /// - a type that can describe objects, but which lacks information needed to
- /// determine its size.
- bool Type::isIncompleteType() const {
- switch (CanonicalType->getTypeClass()) {
- default: return false;
- case Builtin:
- // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
- // be completed.
- return isVoidType();
- case Enum:
- // An enumeration with fixed underlying type is complete (C++0x 7.2p3).
- if (cast<EnumType>(CanonicalType)->getDecl()->isFixed())
- return false;
- // Fall through.
- case Record:
- // A tagged type (struct/union/enum/class) is incomplete if the decl is a
- // forward declaration, but not a full definition (C99 6.2.5p22).
- return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
- case ConstantArray:
- // An array is incomplete if its element type is incomplete
- // (C++ [dcl.array]p1).
- // We don't handle variable arrays (they're not allowed in C++) or
- // dependent-sized arrays (dependent types are never treated as incomplete).
- return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
- case IncompleteArray:
- // An array of unknown size is an incomplete type (C99 6.2.5p22).
- return true;
- case ObjCObject:
- return cast<ObjCObjectType>(CanonicalType)->getBaseType()
- ->isIncompleteType();
- case ObjCInterface:
- // ObjC interfaces are incomplete if they are @class, not @interface.
- return cast<ObjCInterfaceType>(CanonicalType)->getDecl()->isForwardDecl();
- }
- }
- bool QualType::isPODType(ASTContext &Context) const {
- // The compiler shouldn't query this for incomplete types, but the user might.
- // We return false for that case. Except for incomplete arrays of PODs, which
- // are PODs according to the standard.
- if (isNull())
- return 0;
-
- if ((*this)->isIncompleteArrayType())
- return Context.getBaseElementType(*this).isPODType(Context);
-
- if ((*this)->isIncompleteType())
- return false;
- if (Context.getLangOptions().ObjCAutoRefCount) {
- switch (getObjCLifetime()) {
- case Qualifiers::OCL_ExplicitNone:
- return true;
-
- case Qualifiers::OCL_Strong:
- case Qualifiers::OCL_Weak:
- case Qualifiers::OCL_Autoreleasing:
- return false;
- case Qualifiers::OCL_None:
- if ((*this)->isObjCLifetimeType())
- return false;
- break;
- }
- }
-
- QualType CanonicalType = getTypePtr()->CanonicalType;
- switch (CanonicalType->getTypeClass()) {
- // Everything not explicitly mentioned is not POD.
- default: return false;
- case Type::VariableArray:
- case Type::ConstantArray:
- // IncompleteArray is handled above.
- return Context.getBaseElementType(*this).isPODType(Context);
-
- case Type::ObjCObjectPointer:
- case Type::BlockPointer:
- case Type::Builtin:
- case Type::Complex:
- case Type::Pointer:
- case Type::MemberPointer:
- case Type::Vector:
- case Type::ExtVector:
- return true;
- case Type::Enum:
- return true;
- case Type::Record:
- if (CXXRecordDecl *ClassDecl
- = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
- return ClassDecl->isPOD();
- // C struct/union is POD.
- return true;
- }
- }
- bool QualType::isTrivialType(ASTContext &Context) const {
- // The compiler shouldn't query this for incomplete types, but the user might.
- // We return false for that case. Except for incomplete arrays of PODs, which
- // are PODs according to the standard.
- if (isNull())
- return 0;
-
- if ((*this)->isArrayType())
- return Context.getBaseElementType(*this).isTrivialType(Context);
-
- // Return false for incomplete types after skipping any incomplete array
- // types which are expressly allowed by the standard and thus our API.
- if ((*this)->isIncompleteType())
- return false;
-
- if (Context.getLangOptions().ObjCAutoRefCount) {
- switch (getObjCLifetime()) {
- case Qualifiers::OCL_ExplicitNone:
- return true;
-
- case Qualifiers::OCL_Strong:
- case Qualifiers::OCL_Weak:
- case Qualifiers::OCL_Autoreleasing:
- return false;
-
- case Qualifiers::OCL_None:
- if ((*this)->isObjCLifetimeType())
- return false;
- break;
- }
- }
-
- QualType CanonicalType = getTypePtr()->CanonicalType;
- if (CanonicalType->isDependentType())
- return false;
-
- // C++0x [basic.types]p9:
- // Scalar types, trivial class types, arrays of such types, and
- // cv-qualified versions of these types are collectively called trivial
- // types.
-
- // As an extension, Clang treats vector types as Scalar types.
- if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
- return true;
- if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
- if (const CXXRecordDecl *ClassDecl =
- dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- // C++0x [class]p5:
- // A trivial class is a class that has a trivial default constructor
- if (!ClassDecl->hasTrivialDefaultConstructor()) return false;
- // and is trivially copyable.
- if (!ClassDecl->isTriviallyCopyable()) return false;
- }
-
- return true;
- }
-
- // No other types can match.
- return false;
- }
- bool QualType::isTriviallyCopyableType(ASTContext &Context) const {
- if ((*this)->isArrayType())
- return Context.getBaseElementType(*this).isTrivialType(Context);
- if (Context.getLangOptions().ObjCAutoRefCount) {
- switch (getObjCLifetime()) {
- case Qualifiers::OCL_ExplicitNone:
- return true;
-
- case Qualifiers::OCL_Strong:
- case Qualifiers::OCL_Weak:
- case Qualifiers::OCL_Autoreleasing:
- return false;
-
- case Qualifiers::OCL_None:
- if ((*this)->isObjCLifetimeType())
- return false;
- break;
- }
- }
- // C++0x [basic.types]p9
- // Scalar types, trivially copyable class types, arrays of such types, and
- // cv-qualified versions of these types are collectively called trivial
- // types.
- QualType CanonicalType = getCanonicalType();
- if (CanonicalType->isDependentType())
- return false;
- // Return false for incomplete types after skipping any incomplete array types
- // which are expressly allowed by the standard and thus our API.
- if (CanonicalType->isIncompleteType())
- return false;
-
- // As an extension, Clang treats vector types as Scalar types.
- if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
- return true;
- if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
- if (const CXXRecordDecl *ClassDecl =
- dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (!ClassDecl->isTriviallyCopyable()) return false;
- }
- return true;
- }
- // No other types can match.
- return false;
- }
- bool Type::isLiteralType() const {
- if (isDependentType())
- return false;
- // C++0x [basic.types]p10:
- // A type is a literal type if it is:
- // [...]
- // -- an array of literal type
- // Extension: variable arrays cannot be literal types, since they're
- // runtime-sized.
- if (isVariableArrayType())
- return false;
- const Type *BaseTy = getBaseElementTypeUnsafe();
- assert(BaseTy && "NULL element type");
- // Return false for incomplete types after skipping any incomplete array
- // types; those are expressly allowed by the standard and thus our API.
- if (BaseTy->isIncompleteType())
- return false;
- // Objective-C lifetime types are not literal types.
- if (BaseTy->isObjCRetainableType())
- return false;
-
- // C++0x [basic.types]p10:
- // A type is a literal type if it is:
- // -- a scalar type; or
- // As an extension, Clang treats vector types as Scalar types.
- if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
- // -- a reference type; or
- if (BaseTy->isReferenceType()) return true;
- // -- a class type that has all of the following properties:
- if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
- if (const CXXRecordDecl *ClassDecl =
- dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- // -- a trivial destructor,
- if (!ClassDecl->hasTrivialDestructor()) return false;
- // -- every constructor call and full-expression in the
- // brace-or-equal-initializers for non-static data members (if any)
- // is a constant expression,
- // FIXME: C++0x: Clang doesn't yet support non-static data member
- // declarations with initializers, or constexprs.
- // -- it is an aggregate type or has at least one constexpr
- // constructor or constructor template that is not a copy or move
- // constructor, and
- if (!ClassDecl->isAggregate() &&
- !ClassDecl->hasConstExprNonCopyMoveConstructor())
- return false;
- // -- all non-static data members and base classes of literal types
- if (ClassDecl->hasNonLiteralTypeFieldsOrBases()) return false;
- }
- return true;
- }
- return false;
- }
- bool Type::isStandardLayoutType() const {
- if (isDependentType())
- return false;
- // C++0x [basic.types]p9:
- // Scalar types, standard-layout class types, arrays of such types, and
- // cv-qualified versions of these types are collectively called
- // standard-layout types.
- const Type *BaseTy = getBaseElementTypeUnsafe();
- assert(BaseTy && "NULL element type");
- // Return false for incomplete types after skipping any incomplete array
- // types which are expressly allowed by the standard and thus our API.
- if (BaseTy->isIncompleteType())
- return false;
- // As an extension, Clang treats vector types as Scalar types.
- if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
- if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
- if (const CXXRecordDecl *ClassDecl =
- dyn_cast<CXXRecordDecl>(RT->getDecl()))
- if (!ClassDecl->isStandardLayout())
- return false;
- // Default to 'true' for non-C++ class types.
- // FIXME: This is a bit dubious, but plain C structs should trivially meet
- // all the requirements of standard layout classes.
- return true;
- }
- // No other types can match.
- return false;
- }
- // This is effectively the intersection of isTrivialType and
- // isStandardLayoutType. We implement it dircetly to avoid redundant
- // conversions from a type to a CXXRecordDecl.
- bool QualType::isCXX11PODType(ASTContext &Context) const {
- const Type *ty = getTypePtr();
- if (ty->isDependentType())
- return false;
- if (Context.getLangOptions().ObjCAutoRefCount) {
- switch (getObjCLifetime()) {
- case Qualifiers::OCL_ExplicitNone:
- return true;
-
- case Qualifiers::OCL_Strong:
- case Qualifiers::OCL_Weak:
- case Qualifiers::OCL_Autoreleasing:
- return false;
- case Qualifiers::OCL_None:
- if (ty->isObjCLifetimeType())
- return false;
- break;
- }
- }
- // C++11 [basic.types]p9:
- // Scalar types, POD classes, arrays of such types, and cv-qualified
- // versions of these types are collectively called trivial types.
- const Type *BaseTy = ty->getBaseElementTypeUnsafe();
- assert(BaseTy && "NULL element type");
- // Return false for incomplete types after skipping any incomplete array
- // types which are expressly allowed by the standard and thus our API.
- if (BaseTy->isIncompleteType())
- return false;
- // As an extension, Clang treats vector types as Scalar types.
- if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
- if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
- if (const CXXRecordDecl *ClassDecl =
- dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- // C++11 [class]p10:
- // A POD struct is a non-union class that is both a trivial class [...]
- if (!ClassDecl->isTrivial()) return false;
- // C++11 [class]p10:
- // A POD struct is a non-union class that is both a trivial class and
- // a standard-layout class [...]
- if (!ClassDecl->isStandardLayout()) return false;
- // C++11 [class]p10:
- // A POD struct is a non-union class that is both a trivial class and
- // a standard-layout class, and has no non-static data members of type
- // non-POD struct, non-POD union (or array of such types). [...]
- //
- // We don't directly query the recursive aspect as the requiremets for
- // both standard-layout classes and trivial classes apply recursively
- // already.
- }
- return true;
- }
- // No other types can match.
- return false;
- }
- bool Type::isPromotableIntegerType() const {
- if (const BuiltinType *BT = getAs<BuiltinType>())
- switch (BT->getKind()) {
- case BuiltinType::Bool:
- case BuiltinType::Char_S:
- case BuiltinType::Char_U:
- case BuiltinType::SChar:
- case BuiltinType::UChar:
- case BuiltinType::Short:
- case BuiltinType::UShort:
- return true;
- default:
- return false;
- }
- // Enumerated types are promotable to their compatible integer types
- // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
- if (const EnumType *ET = getAs<EnumType>()){
- if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()
- || ET->getDecl()->isScoped())
- return false;
-
- const BuiltinType *BT
- = ET->getDecl()->getPromotionType()->getAs<BuiltinType>();
- return BT->getKind() == BuiltinType::Int
- || BT->getKind() == BuiltinType::UInt;
- }
-
- return false;
- }
- bool Type::isNullPtrType() const {
- if (const BuiltinType *BT = getAs<BuiltinType>())
- return BT->getKind() == BuiltinType::NullPtr;
- return false;
- }
- bool Type::isSpecifierType() const {
- // Note that this intentionally does not use the canonical type.
- switch (getTypeClass()) {
- case Builtin:
- case Record:
- case Enum:
- case Typedef:
- case Complex:
- case TypeOfExpr:
- case TypeOf:
- case TemplateTypeParm:
- case SubstTemplateTypeParm:
- case TemplateSpecialization:
- case Elaborated:
- case DependentName:
- case DependentTemplateSpecialization:
- case ObjCInterface:
- case ObjCObject:
- case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
- return true;
- default:
- return false;
- }
- }
- ElaboratedTypeKeyword
- TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
- switch (TypeSpec) {
- default: return ETK_None;
- case TST_typename: return ETK_Typename;
- case TST_class: return ETK_Class;
- case TST_struct: return ETK_Struct;
- case TST_union: return ETK_Union;
- case TST_enum: return ETK_Enum;
- }
- }
- TagTypeKind
- TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
- switch(TypeSpec) {
- case TST_class: return TTK_Class;
- case TST_struct: return TTK_Struct;
- case TST_union: return TTK_Union;
- case TST_enum: return TTK_Enum;
- }
-
- llvm_unreachable("Type specifier is not a tag type kind.");
- return TTK_Union;
- }
- ElaboratedTypeKeyword
- TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
- switch (Kind) {
- case TTK_Class: return ETK_Class;
- case TTK_Struct: return ETK_Struct;
- case TTK_Union: return ETK_Union;
- case TTK_Enum: return ETK_Enum;
- }
- llvm_unreachable("Unknown tag type kind.");
- }
- TagTypeKind
- TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
- switch (Keyword) {
- case ETK_Class: return TTK_Class;
- case ETK_Struct: return TTK_Struct;
- case ETK_Union: return TTK_Union;
- case ETK_Enum: return TTK_Enum;
- case ETK_None: // Fall through.
- case ETK_Typename:
- llvm_unreachable("Elaborated type keyword is not a tag type kind.");
- }
- llvm_unreachable("Unknown elaborated type keyword.");
- }
- bool
- TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
- switch (Keyword) {
- case ETK_None:
- case ETK_Typename:
- return false;
- case ETK_Class:
- case ETK_Struct:
- case ETK_Union:
- case ETK_Enum:
- return true;
- }
- llvm_unreachable("Unknown elaborated type keyword.");
- }
- const char*
- TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
- switch (Keyword) {
- case ETK_None: return "";
- case ETK_Typename: return "typename";
- case ETK_Class: return "class";
- case ETK_Struct: return "struct";
- case ETK_Union: return "union";
- case ETK_Enum: return "enum";
- }
- llvm_unreachable("Unknown elaborated type keyword.");
- return "";
- }
- DependentTemplateSpecializationType::DependentTemplateSpecializationType(
- ElaboratedTypeKeyword Keyword,
- NestedNameSpecifier *NNS, const IdentifierInfo *Name,
- unsigned NumArgs, const TemplateArgument *Args,
- QualType Canon)
- : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true, true,
- /*VariablyModified=*/false,
- NNS && NNS->containsUnexpandedParameterPack()),
- NNS(NNS), Name(Name), NumArgs(NumArgs) {
- assert((!NNS || NNS->isDependent()) &&
- "DependentTemplateSpecializatonType requires dependent qualifier");
- for (unsigned I = 0; I != NumArgs; ++I) {
- if (Args[I].containsUnexpandedParameterPack())
- setContainsUnexpandedParameterPack();
- new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
- }
- }
- void
- DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
- const ASTContext &Context,
- ElaboratedTypeKeyword Keyword,
- NestedNameSpecifier *Qualifier,
- const IdentifierInfo *Name,
- unsigned NumArgs,
- const TemplateArgument *Args) {
- ID.AddInteger(Keyword);
- ID.AddPointer(Qualifier);
- ID.AddPointer(Name);
- for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
- Args[Idx].Profile(ID, Context);
- }
- bool Type::isElaboratedTypeSpecifier() const {
- ElaboratedTypeKeyword Keyword;
- if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
- Keyword = Elab->getKeyword();
- else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
- Keyword = DepName->getKeyword();
- else if (const DependentTemplateSpecializationType *DepTST =
- dyn_cast<DependentTemplateSpecializationType>(this))
- Keyword = DepTST->getKeyword();
- else
- return false;
- return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
- }
- const char *Type::getTypeClassName() const {
- switch (TypeBits.TC) {
- #define ABSTRACT_TYPE(Derived, Base)
- #define TYPE(Derived, Base) case Derived: return #Derived;
- #include "clang/AST/TypeNodes.def"
- }
-
- llvm_unreachable("Invalid type class.");
- return 0;
- }
- const char *BuiltinType::getName(const LangOptions &LO) const {
- switch (getKind()) {
- case Void: return "void";
- case Bool: return LO.Bool ? "bool" : "_Bool";
- case Char_S: return "char";
- case Char_U: return "char";
- case SChar: return "signed char";
- case Short: return "short";
- case Int: return "int";
- case Long: return "long";
- case LongLong: return "long long";
- case Int128: return "__int128_t";
- case UChar: return "unsigned char";
- case UShort: return "unsigned short";
- case UInt: return "unsigned int";
- case ULong: return "unsigned long";
- case ULongLong: return "unsigned long long";
- case UInt128: return "__uint128_t";
- case Float: return "float";
- case Double: return "double";
- case LongDouble: return "long double";
- case WChar_S:
- case WChar_U: return "wchar_t";
- case Char16: return "char16_t";
- case Char32: return "char32_t";
- case NullPtr: return "nullptr_t";
- case Overload: return "<overloaded function type>";
- case BoundMember: return "<bound member function type>";
- case Dependent: return "<dependent type>";
- case UnknownAny: return "<unknown type>";
- case ObjCId: return "id";
- case ObjCClass: return "Class";
- case ObjCSel: return "SEL";
- }
-
- llvm_unreachable("Invalid builtin type.");
- return 0;
- }
- QualType QualType::getNonLValueExprType(ASTContext &Context) const {
- if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
- return RefType->getPointeeType();
-
- // C++0x [basic.lval]:
- // Class prvalues can have cv-qualified types; non-class prvalues always
- // have cv-unqualified types.
- //
- // See also C99 6.3.2.1p2.
- if (!Context.getLangOptions().CPlusPlus ||
- (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
- return getUnqualifiedType();
-
- return *this;
- }
- llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) {
- switch (CC) {
- case CC_Default:
- llvm_unreachable("no name for default cc");
- return "";
- case CC_C: return "cdecl";
- case CC_X86StdCall: return "stdcall";
- case CC_X86FastCall: return "fastcall";
- case CC_X86ThisCall: return "thiscall";
- case CC_X86Pascal: return "pascal";
- case CC_AAPCS: return "aapcs";
- case CC_AAPCS_VFP: return "aapcs-vfp";
- }
- llvm_unreachable("Invalid calling convention.");
- return "";
- }
- FunctionProtoType::FunctionProtoType(QualType result, const QualType *args,
- unsigned numArgs, QualType canonical,
- const ExtProtoInfo &epi)
- : FunctionType(FunctionProto, result, epi.Variadic, epi.TypeQuals,
- epi.RefQualifier, canonical,
- result->isDependentType(),
- result->isInstantiationDependentType(),
- result->isVariablyModifiedType(),
- result->containsUnexpandedParameterPack(),
- epi.ExtInfo),
- NumArgs(numArgs), NumExceptions(epi.NumExceptions),
- ExceptionSpecType(epi.ExceptionSpecType),
- HasAnyConsumedArgs(epi.ConsumedArguments != 0)
- {
- // Fill in the trailing argument array.
- QualType *argSlot = reinterpret_cast<QualType*>(this+1);
- for (unsigned i = 0; i != numArgs; ++i) {
- if (args[i]->isDependentType())
- setDependent();
- else if (args[i]->isInstantiationDependentType())
- setInstantiationDependent();
-
- if (args[i]->containsUnexpandedParameterPack())
- setContainsUnexpandedParameterPack();
- argSlot[i] = args[i];
- }
- if (getExceptionSpecType() == EST_Dynamic) {
- // Fill in the exception array.
- QualType *exnSlot = argSlot + numArgs;
- for (unsigned i = 0, e = epi.NumExceptions; i != e; ++i) {
- if (epi.Exceptions[i]->isDependentType())
- setDependent();
- else if (epi.Exceptions[i]->isInstantiationDependentType())
- setInstantiationDependent();
-
- if (epi.Exceptions[i]->containsUnexpandedParameterPack())
- setContainsUnexpandedParameterPack();
- exnSlot[i] = epi.Exceptions[i];
- }
- } else if (getExceptionSpecType() == EST_ComputedNoexcept) {
- // Store the noexcept expression and context.
- Expr **noexSlot = reinterpret_cast<Expr**>(argSlot + numArgs);
- *noexSlot = epi.NoexceptExpr;
-
- if (epi.NoexceptExpr) {
- if (epi.NoexceptExpr->isValueDependent()
- || epi.NoexceptExpr->isTypeDependent())
- setDependent();
- else if (epi.NoexceptExpr->isInstantiationDependent())
- setInstantiationDependent();
- }
- }
- if (epi.ConsumedArguments) {
- bool *consumedArgs = const_cast<bool*>(getConsumedArgsBuffer());
- for (unsigned i = 0; i != numArgs; ++i)
- consumedArgs[i] = epi.ConsumedArguments[i];
- }
- }
- FunctionProtoType::NoexceptResult
- FunctionProtoType::getNoexceptSpec(ASTContext &ctx) const {
- ExceptionSpecificationType est = getExceptionSpecType();
- if (est == EST_BasicNoexcept)
- return NR_Nothrow;
- if (est != EST_ComputedNoexcept)
- return NR_NoNoexcept;
- Expr *noexceptExpr = getNoexceptExpr();
- if (!noexceptExpr)
- return NR_BadNoexcept;
- if (noexceptExpr->isValueDependent())
- return NR_Dependent;
- llvm::APSInt value;
- bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, 0,
- /*evaluated*/false);
- (void)isICE;
- assert(isICE && "AST should not contain bad noexcept expressions.");
- return value.getBoolValue() ? NR_Nothrow : NR_Throw;
- }
- bool FunctionProtoType::isTemplateVariadic() const {
- for (unsigned ArgIdx = getNumArgs(); ArgIdx; --ArgIdx)
- if (isa<PackExpansionType>(getArgType(ArgIdx - 1)))
- return true;
-
- return false;
- }
- void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
- const QualType *ArgTys, unsigned NumArgs,
- const ExtProtoInfo &epi,
- const ASTContext &Context) {
- // We have to be careful not to get ambiguous profile encodings.
- // Note that valid type pointers are never ambiguous with anything else.
- //
- // The encoding grammar begins:
- // type type* bool int bool
- // If that final bool is true, then there is a section for the EH spec:
- // bool type*
- // This is followed by an optional "consumed argument" section of the
- // same length as the first type sequence:
- // bool*
- // Finally, we have the ext info:
- // int
- //
- // There is no ambiguity between the consumed arguments and an empty EH
- // spec because of the leading 'bool' which unambiguously indicates
- // whether the following bool is the EH spec or part of the arguments.
- ID.AddPointer(Result.getAsOpaquePtr());
- for (unsigned i = 0; i != NumArgs; ++i)
- ID.AddPointer(ArgTys[i].getAsOpaquePtr());
- // This method is relatively performance sensitive, so as a performance
- // shortcut, use one AddInteger call instead of four for the next four
- // fields.
- assert(!(unsigned(epi.Variadic) & ~1) &&
- !(unsigned(epi.TypeQuals) & ~255) &&
- !(unsigned(epi.RefQualifier) & ~3) &&
- !(unsigned(epi.ExceptionSpecType) & ~7) &&
- "Values larger than expected.");
- ID.AddInteger(unsigned(epi.Variadic) +
- (epi.TypeQuals << 1) +
- (epi.RefQualifier << 9) +
- (epi.ExceptionSpecType << 11));
- if (epi.ExceptionSpecType == EST_Dynamic) {
- for (unsigned i = 0; i != epi.NumExceptions; ++i)
- ID.AddPointer(epi.Exceptions[i].getAsOpaquePtr());
- } else if (epi.ExceptionSpecType == EST_ComputedNoexcept && epi.NoexceptExpr){
- epi.NoexceptExpr->Profile(ID, Context, false);
- }
- if (epi.ConsumedArguments) {
- for (unsigned i = 0; i != NumArgs; ++i)
- ID.AddBoolean(epi.ConsumedArguments[i]);
- }
- epi.ExtInfo.Profile(ID);
- }
- void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID,
- const ASTContext &Ctx) {
- Profile(ID, getResultType(), arg_type_begin(), NumArgs, getExtProtoInfo(),
- Ctx);
- }
- QualType TypedefType::desugar() const {
- return getDecl()->getUnderlyingType();
- }
- TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
- : Type(TypeOfExpr, can, E->isTypeDependent(),
- E->isInstantiationDependent(),
- E->getType()->isVariablyModifiedType(),
- E->containsUnexpandedParameterPack()),
- TOExpr(E) {
- }
- QualType TypeOfExprType::desugar() const {
- return getUnderlyingExpr()->getType();
- }
- void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
- const ASTContext &Context, Expr *E) {
- E->Profile(ID, Context, true);
- }
- DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
- : Type(Decltype, can, E->isTypeDependent(),
- E->isInstantiationDependent(),
- E->getType()->isVariablyModifiedType(),
- E->containsUnexpandedParameterPack()),
- E(E),
- UnderlyingType(underlyingType) {
- }
- DependentDecltypeType::DependentDecltypeType(const ASTContext &Context, Expr *E)
- : DecltypeType(E, Context.DependentTy), Context(Context) { }
- void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
- const ASTContext &Context, Expr *E) {
- E->Profile(ID, Context, true);
- }
- TagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
- : Type(TC, can, D->isDependentType(),
- /*InstantiationDependent=*/D->isDependentType(),
- /*VariablyModified=*/false,
- /*ContainsUnexpandedParameterPack=*/false),
- decl(const_cast<TagDecl*>(D)) {}
- static TagDecl *getInterestingTagDecl(TagDecl *decl) {
- for (TagDecl::redecl_iterator I = decl->redecls_begin(),
- E = decl->redecls_end();
- I != E; ++I) {
- if (I->isDefinition() || I->isBeingDefined())
- return *I;
- }
- // If there's no definition (not even in progress), return what we have.
- return decl;
- }
- UnaryTransformType::UnaryTransformType(QualType BaseType,
- QualType UnderlyingType,
- UTTKind UKind,
- QualType CanonicalType)
- : Type(UnaryTransform, CanonicalType, UnderlyingType->isDependentType(),
- UnderlyingType->isInstantiationDependentType(),
- UnderlyingType->isVariablyModifiedType(),
- BaseType->containsUnexpandedParameterPack())
- , BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind)
- {}
- TagDecl *TagType::getDecl() const {
- return getInterestingTagDecl(decl);
- }
- bool TagType::isBeingDefined() const {
- return getDecl()->isBeingDefined();
- }
- CXXRecordDecl *InjectedClassNameType::getDecl() const {
- return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
- }
- bool RecordType::classof(const TagType *TT) {
- return isa<RecordDecl>(TT->getDecl());
- }
- bool EnumType::classof(const TagType *TT) {
- return isa<EnumDecl>(TT->getDecl());
- }
- IdentifierInfo *TemplateTypeParmType::getIdentifier() const {
- return isCanonicalUnqualified() ? 0 : getDecl()->getIdentifier();
- }
- SubstTemplateTypeParmPackType::
- SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
- QualType Canon,
- const TemplateArgument &ArgPack)
- : Type(SubstTemplateTypeParmPack, Canon, true, true, false, true),
- Replaced(Param),
- Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size())
- {
- }
- TemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const {
- return TemplateArgument(Arguments, NumArguments);
- }
- void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) {
- Profile(ID, getReplacedParameter(), getArgumentPack());
- }
- void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID,
- const TemplateTypeParmType *Replaced,
- const TemplateArgument &ArgPack) {
- ID.AddPointer(Replaced);
- ID.AddInteger(ArgPack.pack_size());
- for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
- PEnd = ArgPack.pack_end();
- P != PEnd; ++P)
- ID.AddPointer(P->getAsType().getAsOpaquePtr());
- }
- bool TemplateSpecializationType::
- anyDependentTemplateArguments(const TemplateArgumentListInfo &Args,
- bool &InstantiationDependent) {
- return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size(),
- InstantiationDependent);
- }
- bool TemplateSpecializationType::
- anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N,
- bool &InstantiationDependent) {
- for (unsigned i = 0; i != N; ++i) {
- if (Args[i].getArgument().isDependent()) {
- InstantiationDependent = true;
- return true;
- }
-
- if (Args[i].getArgument().isInstantiationDependent())
- InstantiationDependent = true;
- }
- return false;
- }
- bool TemplateSpecializationType::
- anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N,
- bool &InstantiationDependent) {
- for (unsigned i = 0; i != N; ++i) {
- if (Args[i].isDependent()) {
- InstantiationDependent = true;
- return true;
- }
-
- if (Args[i].isInstantiationDependent())
- InstantiationDependent = true;
- }
- return false;
- }
- TemplateSpecializationType::
- TemplateSpecializationType(TemplateName T,
- const TemplateArgument *Args, unsigned NumArgs,
- QualType Canon, QualType AliasedType)
- : Type(TemplateSpecialization,
- Canon.isNull()? QualType(this, 0) : Canon,
- Canon.isNull()? T.isDependent() : Canon->isDependentType(),
- Canon.isNull()? T.isDependent()
- : Canon->isInstantiationDependentType(),
- false, T.containsUnexpandedParameterPack()),
- Template(T), NumArgs(NumArgs) {
- assert(!T.getAsDependentTemplateName() &&
- "Use DependentTemplateSpecializationType for dependent template-name");
- assert((T.getKind() == TemplateName::Template ||
- T.getKind() == TemplateName::SubstTemplateTemplateParm ||
- T.getKind() == TemplateName::SubstTemplateTemplateParmPack) &&
- "Unexpected template name for TemplateSpecializationType");
- bool InstantiationDependent;
- (void)InstantiationDependent;
- assert((!Canon.isNull() ||
- T.isDependent() ||
- anyDependentTemplateArguments(Args, NumArgs,
- InstantiationDependent)) &&
- "No canonical type for non-dependent class template specialization");
- TemplateArgument *TemplateArgs
- = reinterpret_cast<TemplateArgument *>(this + 1);
- for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
- // Update dependent and variably-modified bits.
- // If the canonical type exists and is non-dependent, the template
- // specialization type can be non-dependent even if one of the type
- // arguments is. Given:
- // template<typename T> using U = int;
- // U<T> is always non-dependent, irrespective of the type T.
- if (Canon.isNull() && Args[Arg].isDependent())
- setDependent();
- else if (Args[Arg].isInstantiationDependent())
- setInstantiationDependent();
-
- if (Args[Arg].getKind() == TemplateArgument::Type &&
- Args[Arg].getAsType()->isVariablyModifiedType())
- setVariablyModified();
- if (Args[Arg].containsUnexpandedParameterPack())
- setContainsUnexpandedParameterPack();
- new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
- }
- // Store the aliased type if this is a type alias template specialization.
- bool IsTypeAlias = !AliasedType.isNull();
- assert(IsTypeAlias == isTypeAlias() &&
- "allocated wrong size for type alias");
- if (IsTypeAlias) {
- TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1);
- *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType;
- }
- }
- void
- TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
- TemplateName T,
- const TemplateArgument *Args,
- unsigned NumArgs,
- const ASTContext &Context) {
- T.Profile(ID);
- for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
- Args[Idx].Profile(ID, Context);
- }
- bool TemplateSpecializationType::isTypeAlias() const {
- TemplateDecl *D = Template.getAsTemplateDecl();
- return D && isa<TypeAliasTemplateDecl>(D);
- }
- QualType
- QualifierCollector::apply(const ASTContext &Context, QualType QT) const {
- if (!hasNonFastQualifiers())
- return QT.withFastQualifiers(getFastQualifiers());
- return Context.getQualifiedType(QT, *this);
- }
- QualType
- QualifierCollector::apply(const ASTContext &Context, const Type *T) const {
- if (!hasNonFastQualifiers())
- return QualType(T, getFastQualifiers());
- return Context.getQualifiedType(T, *this);
- }
- void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
- QualType BaseType,
- ObjCProtocolDecl * const *Protocols,
- unsigned NumProtocols) {
- ID.AddPointer(BaseType.getAsOpaquePtr());
- for (unsigned i = 0; i != NumProtocols; i++)
- ID.AddPointer(Protocols[i]);
- }
- void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
- Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
- }
- namespace {
- /// \brief The cached properties of a type.
- class CachedProperties {
- char linkage;
- char visibility;
- bool local;
-
- public:
- CachedProperties(Linkage linkage, Visibility visibility, bool local)
- : linkage(linkage), visibility(visibility), local(local) {}
-
- Linkage getLinkage() const { return (Linkage) linkage; }
- Visibility getVisibility() const { return (Visibility) visibility; }
- bool hasLocalOrUnnamedType() const { return local; }
-
- friend CachedProperties merge(CachedProperties L, CachedProperties R) {
- return CachedProperties(minLinkage(L.getLinkage(), R.getLinkage()),
- minVisibility(L.getVisibility(), R.getVisibility()),
- L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
- }
- };
- }
- static CachedProperties computeCachedProperties(const Type *T);
- namespace clang {
- /// The type-property cache. This is templated so as to be
- /// instantiated at an internal type to prevent unnecessary symbol
- /// leakage.
- template <class Private> class TypePropertyCache {
- public:
- static CachedProperties get(QualType T) {
- return get(T.getTypePtr());
- }
- static CachedProperties get(const Type *T) {
- ensure(T);
- return CachedProperties(T->TypeBits.getLinkage(),
- T->TypeBits.getVisibility(),
- T->TypeBits.hasLocalOrUnnamedType());
- }
- static void ensure(const Type *T) {
- // If the cache is valid, we're okay.
- if (T->TypeBits.isCacheValid()) return;
- // If this type is non-canonical, ask its canonical type for the
- // relevant information.
- if (!T->isCanonicalUnqualified()) {
- const Type *CT = T->getCanonicalTypeInternal().getTypePtr();
- ensure(CT);
- T->TypeBits.CacheValidAndVisibility =
- CT->TypeBits.CacheValidAndVisibility;
- T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage;
- T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed;
- return;
- }
- // Compute the cached properties and then set the cache.
- CachedProperties Result = computeCachedProperties(T);
- T->TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U;
- assert(T->TypeBits.isCacheValid() &&
- T->TypeBits.getVisibility() == Result.getVisibility());
- T->TypeBits.CachedLinkage = Result.getLinkage();
- T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
- }
- };
- }
- // Instantiate the friend template at a private class. In a
- // reasonable implementation, these symbols will be internal.
- // It is terrible that this is the best way to accomplish this.
- namespace { class Private {}; }
- typedef TypePropertyCache<Private> Cache;
- static CachedProperties computeCachedProperties(const Type *T) {
- switch (T->getTypeClass()) {
- #define TYPE(Class,Base)
- #define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
- #include "clang/AST/TypeNodes.def"
- llvm_unreachable("didn't expect a non-canonical type here");
- #define TYPE(Class,Base)
- #define DEPENDENT_TYPE(Class,Base) case Type::Class:
- #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
- #include "clang/AST/TypeNodes.def"
- // Treat dependent types as external.
- assert(T->isDependentType());
- return CachedProperties(ExternalLinkage, DefaultVisibility, false);
- case Type::Builtin:
- // C++ [basic.link]p8:
- // A type is said to have linkage if and only if:
- // - it is a fundamental type (3.9.1); or
- return CachedProperties(ExternalLinkage, DefaultVisibility, false);
- case Type::Record:
- case Type::Enum: {
- const TagDecl *Tag = cast<TagType>(T)->getDecl();
- // C++ [basic.link]p8:
- // - it is a class or enumeration type that is named (or has a name
- // for linkage purposes (7.1.3)) and the name has linkage; or
- // - it is a specialization of a class template (14); or
- NamedDecl::LinkageInfo LV = Tag->getLinkageAndVisibility();
- bool IsLocalOrUnnamed =
- Tag->getDeclContext()->isFunctionOrMethod() ||
- (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl());
- return CachedProperties(LV.linkage(), LV.visibility(), IsLocalOrUnnamed);
- }
- // C++ [basic.link]p8:
- // - it is a compound type (3.9.2) other than a class or enumeration,
- // compounded exclusively from types that have linkage; or
- case Type::Complex:
- return Cache::get(cast<ComplexType>(T)->getElementType());
- case Type::Pointer:
- return Cache::get(cast<PointerType>(T)->getPointeeType());
- case Type::BlockPointer:
- return Cache::get(cast<BlockPointerType>(T)->getPointeeType());
- case Type::LValueReference:
- case Type::RValueReference:
- return Cache::get(cast<ReferenceType>(T)->getPointeeType());
- case Type::MemberPointer: {
- const MemberPointerType *MPT = cast<MemberPointerType>(T);
- return merge(Cache::get(MPT->getClass()),
- Cache::get(MPT->getPointeeType()));
- }
- case Type::ConstantArray:
- case Type::IncompleteArray:
- case Type::VariableArray:
- return Cache::get(cast<ArrayType>(T)->getElementType());
- case Type::Vector:
- case Type::ExtVector:
- return Cache::get(cast<VectorType>(T)->getElementType());
- case Type::FunctionNoProto:
- return Cache::get(cast<FunctionType>(T)->getResultType());
- case Type::FunctionProto: {
- const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
- CachedProperties result = Cache::get(FPT->getResultType());
- for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
- ae = FPT->arg_type_end(); ai != ae; ++ai)
- result = merge(result, Cache::get(*ai));
- return result;
- }
- case Type::ObjCInterface: {
- NamedDecl::LinkageInfo LV =
- cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility();
- return CachedProperties(LV.linkage(), LV.visibility(), false);
- }
- case Type::ObjCObject:
- return Cache::get(cast<ObjCObjectType>(T)->getBaseType());
- case Type::ObjCObjectPointer:
- return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType());
- }
- llvm_unreachable("unhandled type class");
- // C++ [basic.link]p8:
- // Names not covered by these rules have no linkage.
- return CachedProperties(NoLinkage, DefaultVisibility, false);
- }
- /// \brief Determine the linkage of this type.
- Linkage Type::getLinkage() const {
- Cache::ensure(this);
- return TypeBits.getLinkage();
- }
- /// \brief Determine the linkage of this type.
- Visibility Type::getVisibility() const {
- Cache::ensure(this);
- return TypeBits.getVisibility();
- }
- bool Type::hasUnnamedOrLocalType() const {
- Cache::ensure(this);
- return TypeBits.hasLocalOrUnnamedType();
- }
- std::pair<Linkage,Visibility> Type::getLinkageAndVisibility() const {
- Cache::ensure(this);
- return std::make_pair(TypeBits.getLinkage(), TypeBits.getVisibility());
- }
- void Type::ClearLinkageCache() {
- TypeBits.CacheValidAndVisibility = 0;
- if (QualType(this, 0) != CanonicalType)
- CanonicalType->TypeBits.CacheValidAndVisibility = 0;
- }
- Qualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const {
- if (isObjCARCImplicitlyUnretainedType())
- return Qualifiers::OCL_ExplicitNone;
- return Qualifiers::OCL_Strong;
- }
- bool Type::isObjCARCImplicitlyUnretainedType() const {
- assert(isObjCLifetimeType() &&
- "cannot query implicit lifetime for non-inferrable type");
- const Type *canon = getCanonicalTypeInternal().getTypePtr();
- // Walk down to the base type. We don't care about qualifiers for this.
- while (const ArrayType *array = dyn_cast<ArrayType>(canon))
- canon = array->getElementType().getTypePtr();
- if (const ObjCObjectPointerType *opt
- = dyn_cast<ObjCObjectPointerType>(canon)) {
- // Class and Class<Protocol> don't require retension.
- if (opt->getObjectType()->isObjCClass())
- return true;
- }
- return false;
- }
- bool Type::isObjCNSObjectType() const {
- if (const TypedefType *typedefType = dyn_cast<TypedefType>(this))
- return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>();
- return false;
- }
- bool Type::isObjCRetainableType() const {
- return isObjCObjectPointerType() ||
- isBlockPointerType() ||
- isObjCNSObjectType();
- }
- bool Type::isObjCIndirectLifetimeType() const {
- if (isObjCLifetimeType())
- return true;
- if (const PointerType *OPT = getAs<PointerType>())
- return OPT->getPointeeType()->isObjCIndirectLifetimeType();
- if (const ReferenceType *Ref = getAs<ReferenceType>())
- return Ref->getPointeeType()->isObjCIndirectLifetimeType();
- if (const MemberPointerType *MemPtr = getAs<MemberPointerType>())
- return MemPtr->getPointeeType()->isObjCIndirectLifetimeType();
- return false;
- }
- /// Returns true if objects of this type have lifetime semantics under
- /// ARC.
- bool Type::isObjCLifetimeType() const {
- const Type *type = this;
- while (const ArrayType *array = type->getAsArrayTypeUnsafe())
- type = array->getElementType().getTypePtr();
- return type->isObjCRetainableType();
- }
- /// \brief Determine whether the given type T is a "bridgable" Objective-C type,
- /// which is either an Objective-C object pointer type or an
- bool Type::isObjCARCBridgableType() const {
- return isObjCObjectPointerType() || isBlockPointerType();
- }
- /// \brief Determine whether the given type T is a "bridgeable" C type.
- bool Type::isCARCBridgableType() const {
- const PointerType *Pointer = getAs<PointerType>();
- if (!Pointer)
- return false;
-
- QualType Pointee = Pointer->getPointeeType();
- return Pointee->isVoidType() || Pointee->isRecordType();
- }
- bool Type::hasSizedVLAType() const {
- if (!isVariablyModifiedType()) return false;
- if (const PointerType *ptr = getAs<PointerType>())
- return ptr->getPointeeType()->hasSizedVLAType();
- if (const ReferenceType *ref = getAs<ReferenceType>())
- return ref->getPointeeType()->hasSizedVLAType();
- if (const ArrayType *arr = getAsArrayTypeUnsafe()) {
- if (isa<VariableArrayType>(arr) &&
- cast<VariableArrayType>(arr)->getSizeExpr())
- return true;
- return arr->getElementType()->hasSizedVLAType();
- }
- return false;
- }
- QualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) {
- switch (type.getObjCLifetime()) {
- case Qualifiers::OCL_None:
- case Qualifiers::OCL_ExplicitNone:
- case Qualifiers::OCL_Autoreleasing:
- break;
- case Qualifiers::OCL_Strong:
- return DK_objc_strong_lifetime;
- case Qualifiers::OCL_Weak:
- return DK_objc_weak_lifetime;
- }
- /// Currently, the only destruction kind we recognize is C++ objects
- /// with non-trivial destructors.
- const CXXRecordDecl *record =
- type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
- if (record && !record->hasTrivialDestructor())
- return DK_cxx_destructor;
- return DK_none;
- }
- bool QualType::hasTrivialCopyAssignment(ASTContext &Context) const {
- switch (getObjCLifetime()) {
- case Qualifiers::OCL_None:
- break;
-
- case Qualifiers::OCL_ExplicitNone:
- return true;
-
- case Qualifiers::OCL_Autoreleasing:
- case Qualifiers::OCL_Strong:
- case Qualifiers::OCL_Weak:
- return !Context.getLangOptions().ObjCAutoRefCount;
- }
-
- if (const CXXRecordDecl *Record
- = getTypePtr()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
- return Record->hasTrivialCopyAssignment();
-
- return true;
- }
|