Type.cpp 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  1. //===--- Type.cpp - Type representation and manipulation ------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements type-related functionality.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/CharUnits.h"
  15. #include "clang/AST/Type.h"
  16. #include "clang/AST/DeclCXX.h"
  17. #include "clang/AST/DeclObjC.h"
  18. #include "clang/AST/DeclTemplate.h"
  19. #include "clang/AST/Expr.h"
  20. #include "clang/AST/PrettyPrinter.h"
  21. #include "clang/AST/TypeVisitor.h"
  22. #include "clang/Basic/PartialDiagnostic.h"
  23. #include "clang/Basic/Specifiers.h"
  24. #include "llvm/ADT/APSInt.h"
  25. #include "llvm/ADT/StringExtras.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <algorithm>
  28. using namespace clang;
  29. bool Qualifiers::isStrictSupersetOf(Qualifiers Other) const {
  30. return (*this != Other) &&
  31. // CVR qualifiers superset
  32. (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) &&
  33. // ObjC GC qualifiers superset
  34. ((getObjCGCAttr() == Other.getObjCGCAttr()) ||
  35. (hasObjCGCAttr() && !Other.hasObjCGCAttr())) &&
  36. // Address space superset.
  37. ((getAddressSpace() == Other.getAddressSpace()) ||
  38. (hasAddressSpace()&& !Other.hasAddressSpace())) &&
  39. // Lifetime qualifier superset.
  40. ((getObjCLifetime() == Other.getObjCLifetime()) ||
  41. (hasObjCLifetime() && !Other.hasObjCLifetime()));
  42. }
  43. const IdentifierInfo* QualType::getBaseTypeIdentifier() const {
  44. const Type* ty = getTypePtr();
  45. NamedDecl *ND = NULL;
  46. if (ty->isPointerType() || ty->isReferenceType())
  47. return ty->getPointeeType().getBaseTypeIdentifier();
  48. else if (ty->isRecordType())
  49. ND = ty->getAs<RecordType>()->getDecl();
  50. else if (ty->isEnumeralType())
  51. ND = ty->getAs<EnumType>()->getDecl();
  52. else if (ty->getTypeClass() == Type::Typedef)
  53. ND = ty->getAs<TypedefType>()->getDecl();
  54. else if (ty->isArrayType())
  55. return ty->castAsArrayTypeUnsafe()->
  56. getElementType().getBaseTypeIdentifier();
  57. if (ND)
  58. return ND->getIdentifier();
  59. return NULL;
  60. }
  61. bool QualType::isConstant(QualType T, ASTContext &Ctx) {
  62. if (T.isConstQualified())
  63. return true;
  64. if (const ArrayType *AT = Ctx.getAsArrayType(T))
  65. return AT->getElementType().isConstant(Ctx);
  66. return false;
  67. }
  68. unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
  69. QualType ElementType,
  70. const llvm::APInt &NumElements) {
  71. llvm::APSInt SizeExtended(NumElements, true);
  72. unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
  73. SizeExtended = SizeExtended.extend(std::max(SizeTypeBits,
  74. SizeExtended.getBitWidth()) * 2);
  75. uint64_t ElementSize
  76. = Context.getTypeSizeInChars(ElementType).getQuantity();
  77. llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
  78. TotalSize *= SizeExtended;
  79. return TotalSize.getActiveBits();
  80. }
  81. unsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) {
  82. unsigned Bits = Context.getTypeSize(Context.getSizeType());
  83. // GCC appears to only allow 63 bits worth of address space when compiling
  84. // for 64-bit, so we do the same.
  85. if (Bits == 64)
  86. --Bits;
  87. return Bits;
  88. }
  89. DependentSizedArrayType::DependentSizedArrayType(const ASTContext &Context,
  90. QualType et, QualType can,
  91. Expr *e, ArraySizeModifier sm,
  92. unsigned tq,
  93. SourceRange brackets)
  94. : ArrayType(DependentSizedArray, et, can, sm, tq,
  95. (et->containsUnexpandedParameterPack() ||
  96. (e && e->containsUnexpandedParameterPack()))),
  97. Context(Context), SizeExpr((Stmt*) e), Brackets(brackets)
  98. {
  99. }
  100. void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
  101. const ASTContext &Context,
  102. QualType ET,
  103. ArraySizeModifier SizeMod,
  104. unsigned TypeQuals,
  105. Expr *E) {
  106. ID.AddPointer(ET.getAsOpaquePtr());
  107. ID.AddInteger(SizeMod);
  108. ID.AddInteger(TypeQuals);
  109. E->Profile(ID, Context, true);
  110. }
  111. DependentSizedExtVectorType::DependentSizedExtVectorType(const
  112. ASTContext &Context,
  113. QualType ElementType,
  114. QualType can,
  115. Expr *SizeExpr,
  116. SourceLocation loc)
  117. : Type(DependentSizedExtVector, can, /*Dependent=*/true,
  118. /*InstantiationDependent=*/true,
  119. ElementType->isVariablyModifiedType(),
  120. (ElementType->containsUnexpandedParameterPack() ||
  121. (SizeExpr && SizeExpr->containsUnexpandedParameterPack()))),
  122. Context(Context), SizeExpr(SizeExpr), ElementType(ElementType),
  123. loc(loc)
  124. {
  125. }
  126. void
  127. DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
  128. const ASTContext &Context,
  129. QualType ElementType, Expr *SizeExpr) {
  130. ID.AddPointer(ElementType.getAsOpaquePtr());
  131. SizeExpr->Profile(ID, Context, true);
  132. }
  133. VectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType,
  134. VectorKind vecKind)
  135. : Type(Vector, canonType, vecType->isDependentType(),
  136. vecType->isInstantiationDependentType(),
  137. vecType->isVariablyModifiedType(),
  138. vecType->containsUnexpandedParameterPack()),
  139. ElementType(vecType)
  140. {
  141. VectorTypeBits.VecKind = vecKind;
  142. VectorTypeBits.NumElements = nElements;
  143. }
  144. VectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements,
  145. QualType canonType, VectorKind vecKind)
  146. : Type(tc, canonType, vecType->isDependentType(),
  147. vecType->isInstantiationDependentType(),
  148. vecType->isVariablyModifiedType(),
  149. vecType->containsUnexpandedParameterPack()),
  150. ElementType(vecType)
  151. {
  152. VectorTypeBits.VecKind = vecKind;
  153. VectorTypeBits.NumElements = nElements;
  154. }
  155. /// getArrayElementTypeNoTypeQual - If this is an array type, return the
  156. /// element type of the array, potentially with type qualifiers missing.
  157. /// This method should never be used when type qualifiers are meaningful.
  158. const Type *Type::getArrayElementTypeNoTypeQual() const {
  159. // If this is directly an array type, return it.
  160. if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
  161. return ATy->getElementType().getTypePtr();
  162. // If the canonical form of this type isn't the right kind, reject it.
  163. if (!isa<ArrayType>(CanonicalType))
  164. return 0;
  165. // If this is a typedef for an array type, strip the typedef off without
  166. // losing all typedef information.
  167. return cast<ArrayType>(getUnqualifiedDesugaredType())
  168. ->getElementType().getTypePtr();
  169. }
  170. /// getDesugaredType - Return the specified type with any "sugar" removed from
  171. /// the type. This takes off typedefs, typeof's etc. If the outer level of
  172. /// the type is already concrete, it returns it unmodified. This is similar
  173. /// to getting the canonical type, but it doesn't remove *all* typedefs. For
  174. /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
  175. /// concrete.
  176. QualType QualType::getDesugaredType(QualType T, const ASTContext &Context) {
  177. SplitQualType split = getSplitDesugaredType(T);
  178. return Context.getQualifiedType(split.first, split.second);
  179. }
  180. QualType QualType::getSingleStepDesugaredType(const ASTContext &Context) const {
  181. QualifierCollector Qs;
  182. const Type *CurTy = Qs.strip(*this);
  183. switch (CurTy->getTypeClass()) {
  184. #define ABSTRACT_TYPE(Class, Parent)
  185. #define TYPE(Class, Parent) \
  186. case Type::Class: { \
  187. const Class##Type *Ty = cast<Class##Type>(CurTy); \
  188. if (!Ty->isSugared()) \
  189. return *this; \
  190. return Context.getQualifiedType(Ty->desugar(), Qs); \
  191. break; \
  192. }
  193. #include "clang/AST/TypeNodes.def"
  194. }
  195. return *this;
  196. }
  197. SplitQualType QualType::getSplitDesugaredType(QualType T) {
  198. QualifierCollector Qs;
  199. QualType Cur = T;
  200. while (true) {
  201. const Type *CurTy = Qs.strip(Cur);
  202. switch (CurTy->getTypeClass()) {
  203. #define ABSTRACT_TYPE(Class, Parent)
  204. #define TYPE(Class, Parent) \
  205. case Type::Class: { \
  206. const Class##Type *Ty = cast<Class##Type>(CurTy); \
  207. if (!Ty->isSugared()) \
  208. return SplitQualType(Ty, Qs); \
  209. Cur = Ty->desugar(); \
  210. break; \
  211. }
  212. #include "clang/AST/TypeNodes.def"
  213. }
  214. }
  215. }
  216. SplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) {
  217. SplitQualType split = type.split();
  218. // All the qualifiers we've seen so far.
  219. Qualifiers quals = split.second;
  220. // The last type node we saw with any nodes inside it.
  221. const Type *lastTypeWithQuals = split.first;
  222. while (true) {
  223. QualType next;
  224. // Do a single-step desugar, aborting the loop if the type isn't
  225. // sugared.
  226. switch (split.first->getTypeClass()) {
  227. #define ABSTRACT_TYPE(Class, Parent)
  228. #define TYPE(Class, Parent) \
  229. case Type::Class: { \
  230. const Class##Type *ty = cast<Class##Type>(split.first); \
  231. if (!ty->isSugared()) goto done; \
  232. next = ty->desugar(); \
  233. break; \
  234. }
  235. #include "clang/AST/TypeNodes.def"
  236. }
  237. // Otherwise, split the underlying type. If that yields qualifiers,
  238. // update the information.
  239. split = next.split();
  240. if (!split.second.empty()) {
  241. lastTypeWithQuals = split.first;
  242. quals.addConsistentQualifiers(split.second);
  243. }
  244. }
  245. done:
  246. return SplitQualType(lastTypeWithQuals, quals);
  247. }
  248. QualType QualType::IgnoreParens(QualType T) {
  249. // FIXME: this seems inherently un-qualifiers-safe.
  250. while (const ParenType *PT = T->getAs<ParenType>())
  251. T = PT->getInnerType();
  252. return T;
  253. }
  254. /// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
  255. /// sugar off the given type. This should produce an object of the
  256. /// same dynamic type as the canonical type.
  257. const Type *Type::getUnqualifiedDesugaredType() const {
  258. const Type *Cur = this;
  259. while (true) {
  260. switch (Cur->getTypeClass()) {
  261. #define ABSTRACT_TYPE(Class, Parent)
  262. #define TYPE(Class, Parent) \
  263. case Class: { \
  264. const Class##Type *Ty = cast<Class##Type>(Cur); \
  265. if (!Ty->isSugared()) return Cur; \
  266. Cur = Ty->desugar().getTypePtr(); \
  267. break; \
  268. }
  269. #include "clang/AST/TypeNodes.def"
  270. }
  271. }
  272. }
  273. /// isVoidType - Helper method to determine if this is the 'void' type.
  274. bool Type::isVoidType() const {
  275. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  276. return BT->getKind() == BuiltinType::Void;
  277. return false;
  278. }
  279. bool Type::isDerivedType() const {
  280. switch (CanonicalType->getTypeClass()) {
  281. case Pointer:
  282. case VariableArray:
  283. case ConstantArray:
  284. case IncompleteArray:
  285. case FunctionProto:
  286. case FunctionNoProto:
  287. case LValueReference:
  288. case RValueReference:
  289. case Record:
  290. return true;
  291. default:
  292. return false;
  293. }
  294. }
  295. bool Type::isClassType() const {
  296. if (const RecordType *RT = getAs<RecordType>())
  297. return RT->getDecl()->isClass();
  298. return false;
  299. }
  300. bool Type::isStructureType() const {
  301. if (const RecordType *RT = getAs<RecordType>())
  302. return RT->getDecl()->isStruct();
  303. return false;
  304. }
  305. bool Type::isStructureOrClassType() const {
  306. if (const RecordType *RT = getAs<RecordType>())
  307. return RT->getDecl()->isStruct() || RT->getDecl()->isClass();
  308. return false;
  309. }
  310. bool Type::isVoidPointerType() const {
  311. if (const PointerType *PT = getAs<PointerType>())
  312. return PT->getPointeeType()->isVoidType();
  313. return false;
  314. }
  315. bool Type::isUnionType() const {
  316. if (const RecordType *RT = getAs<RecordType>())
  317. return RT->getDecl()->isUnion();
  318. return false;
  319. }
  320. bool Type::isComplexType() const {
  321. if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
  322. return CT->getElementType()->isFloatingType();
  323. return false;
  324. }
  325. bool Type::isComplexIntegerType() const {
  326. // Check for GCC complex integer extension.
  327. return getAsComplexIntegerType();
  328. }
  329. const ComplexType *Type::getAsComplexIntegerType() const {
  330. if (const ComplexType *Complex = getAs<ComplexType>())
  331. if (Complex->getElementType()->isIntegerType())
  332. return Complex;
  333. return 0;
  334. }
  335. QualType Type::getPointeeType() const {
  336. if (const PointerType *PT = getAs<PointerType>())
  337. return PT->getPointeeType();
  338. if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
  339. return OPT->getPointeeType();
  340. if (const BlockPointerType *BPT = getAs<BlockPointerType>())
  341. return BPT->getPointeeType();
  342. if (const ReferenceType *RT = getAs<ReferenceType>())
  343. return RT->getPointeeType();
  344. return QualType();
  345. }
  346. const RecordType *Type::getAsStructureType() const {
  347. // If this is directly a structure type, return it.
  348. if (const RecordType *RT = dyn_cast<RecordType>(this)) {
  349. if (RT->getDecl()->isStruct())
  350. return RT;
  351. }
  352. // If the canonical form of this type isn't the right kind, reject it.
  353. if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
  354. if (!RT->getDecl()->isStruct())
  355. return 0;
  356. // If this is a typedef for a structure type, strip the typedef off without
  357. // losing all typedef information.
  358. return cast<RecordType>(getUnqualifiedDesugaredType());
  359. }
  360. return 0;
  361. }
  362. const RecordType *Type::getAsUnionType() const {
  363. // If this is directly a union type, return it.
  364. if (const RecordType *RT = dyn_cast<RecordType>(this)) {
  365. if (RT->getDecl()->isUnion())
  366. return RT;
  367. }
  368. // If the canonical form of this type isn't the right kind, reject it.
  369. if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
  370. if (!RT->getDecl()->isUnion())
  371. return 0;
  372. // If this is a typedef for a union type, strip the typedef off without
  373. // losing all typedef information.
  374. return cast<RecordType>(getUnqualifiedDesugaredType());
  375. }
  376. return 0;
  377. }
  378. ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
  379. ObjCProtocolDecl * const *Protocols,
  380. unsigned NumProtocols)
  381. : Type(ObjCObject, Canonical, false, false, false, false),
  382. BaseType(Base)
  383. {
  384. ObjCObjectTypeBits.NumProtocols = NumProtocols;
  385. assert(getNumProtocols() == NumProtocols &&
  386. "bitfield overflow in protocol count");
  387. if (NumProtocols)
  388. memcpy(getProtocolStorage(), Protocols,
  389. NumProtocols * sizeof(ObjCProtocolDecl*));
  390. }
  391. const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
  392. // There is no sugar for ObjCObjectType's, just return the canonical
  393. // type pointer if it is the right class. There is no typedef information to
  394. // return and these cannot be Address-space qualified.
  395. if (const ObjCObjectType *T = getAs<ObjCObjectType>())
  396. if (T->getNumProtocols() && T->getInterface())
  397. return T;
  398. return 0;
  399. }
  400. bool Type::isObjCQualifiedInterfaceType() const {
  401. return getAsObjCQualifiedInterfaceType() != 0;
  402. }
  403. const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
  404. // There is no sugar for ObjCQualifiedIdType's, just return the canonical
  405. // type pointer if it is the right class.
  406. if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
  407. if (OPT->isObjCQualifiedIdType())
  408. return OPT;
  409. }
  410. return 0;
  411. }
  412. const ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const {
  413. // There is no sugar for ObjCQualifiedClassType's, just return the canonical
  414. // type pointer if it is the right class.
  415. if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
  416. if (OPT->isObjCQualifiedClassType())
  417. return OPT;
  418. }
  419. return 0;
  420. }
  421. const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
  422. if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
  423. if (OPT->getInterfaceType())
  424. return OPT;
  425. }
  426. return 0;
  427. }
  428. const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
  429. if (const PointerType *PT = getAs<PointerType>())
  430. if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
  431. return dyn_cast<CXXRecordDecl>(RT->getDecl());
  432. return 0;
  433. }
  434. CXXRecordDecl *Type::getAsCXXRecordDecl() const {
  435. if (const RecordType *RT = getAs<RecordType>())
  436. return dyn_cast<CXXRecordDecl>(RT->getDecl());
  437. else if (const InjectedClassNameType *Injected
  438. = getAs<InjectedClassNameType>())
  439. return Injected->getDecl();
  440. return 0;
  441. }
  442. namespace {
  443. class GetContainedAutoVisitor :
  444. public TypeVisitor<GetContainedAutoVisitor, AutoType*> {
  445. public:
  446. using TypeVisitor<GetContainedAutoVisitor, AutoType*>::Visit;
  447. AutoType *Visit(QualType T) {
  448. if (T.isNull())
  449. return 0;
  450. return Visit(T.getTypePtr());
  451. }
  452. // The 'auto' type itself.
  453. AutoType *VisitAutoType(const AutoType *AT) {
  454. return const_cast<AutoType*>(AT);
  455. }
  456. // Only these types can contain the desired 'auto' type.
  457. AutoType *VisitPointerType(const PointerType *T) {
  458. return Visit(T->getPointeeType());
  459. }
  460. AutoType *VisitBlockPointerType(const BlockPointerType *T) {
  461. return Visit(T->getPointeeType());
  462. }
  463. AutoType *VisitReferenceType(const ReferenceType *T) {
  464. return Visit(T->getPointeeTypeAsWritten());
  465. }
  466. AutoType *VisitMemberPointerType(const MemberPointerType *T) {
  467. return Visit(T->getPointeeType());
  468. }
  469. AutoType *VisitArrayType(const ArrayType *T) {
  470. return Visit(T->getElementType());
  471. }
  472. AutoType *VisitDependentSizedExtVectorType(
  473. const DependentSizedExtVectorType *T) {
  474. return Visit(T->getElementType());
  475. }
  476. AutoType *VisitVectorType(const VectorType *T) {
  477. return Visit(T->getElementType());
  478. }
  479. AutoType *VisitFunctionType(const FunctionType *T) {
  480. return Visit(T->getResultType());
  481. }
  482. AutoType *VisitParenType(const ParenType *T) {
  483. return Visit(T->getInnerType());
  484. }
  485. AutoType *VisitAttributedType(const AttributedType *T) {
  486. return Visit(T->getModifiedType());
  487. }
  488. };
  489. }
  490. AutoType *Type::getContainedAutoType() const {
  491. return GetContainedAutoVisitor().Visit(this);
  492. }
  493. bool Type::isIntegerType() const {
  494. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  495. return BT->getKind() >= BuiltinType::Bool &&
  496. BT->getKind() <= BuiltinType::Int128;
  497. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
  498. // Incomplete enum types are not treated as integer types.
  499. // FIXME: In C++, enum types are never integer types.
  500. return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
  501. return false;
  502. }
  503. bool Type::hasIntegerRepresentation() const {
  504. if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
  505. return VT->getElementType()->isIntegerType();
  506. else
  507. return isIntegerType();
  508. }
  509. /// \brief Determine whether this type is an integral type.
  510. ///
  511. /// This routine determines whether the given type is an integral type per
  512. /// C++ [basic.fundamental]p7. Although the C standard does not define the
  513. /// term "integral type", it has a similar term "integer type", and in C++
  514. /// the two terms are equivalent. However, C's "integer type" includes
  515. /// enumeration types, while C++'s "integer type" does not. The \c ASTContext
  516. /// parameter is used to determine whether we should be following the C or
  517. /// C++ rules when determining whether this type is an integral/integer type.
  518. ///
  519. /// For cases where C permits "an integer type" and C++ permits "an integral
  520. /// type", use this routine.
  521. ///
  522. /// For cases where C permits "an integer type" and C++ permits "an integral
  523. /// or enumeration type", use \c isIntegralOrEnumerationType() instead.
  524. ///
  525. /// \param Ctx The context in which this type occurs.
  526. ///
  527. /// \returns true if the type is considered an integral type, false otherwise.
  528. bool Type::isIntegralType(ASTContext &Ctx) const {
  529. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  530. return BT->getKind() >= BuiltinType::Bool &&
  531. BT->getKind() <= BuiltinType::Int128;
  532. if (!Ctx.getLangOptions().CPlusPlus)
  533. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
  534. return ET->getDecl()->isComplete(); // Complete enum types are integral in C.
  535. return false;
  536. }
  537. bool Type::isIntegralOrEnumerationType() const {
  538. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  539. return BT->getKind() >= BuiltinType::Bool &&
  540. BT->getKind() <= BuiltinType::Int128;
  541. // Check for a complete enum type; incomplete enum types are not properly an
  542. // enumeration type in the sense required here.
  543. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
  544. return ET->getDecl()->isComplete();
  545. return false;
  546. }
  547. bool Type::isIntegralOrUnscopedEnumerationType() const {
  548. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  549. return BT->getKind() >= BuiltinType::Bool &&
  550. BT->getKind() <= BuiltinType::Int128;
  551. // Check for a complete enum type; incomplete enum types are not properly an
  552. // enumeration type in the sense required here.
  553. // C++0x: However, if the underlying type of the enum is fixed, it is
  554. // considered complete.
  555. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
  556. return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
  557. return false;
  558. }
  559. bool Type::isBooleanType() const {
  560. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  561. return BT->getKind() == BuiltinType::Bool;
  562. return false;
  563. }
  564. bool Type::isCharType() const {
  565. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  566. return BT->getKind() == BuiltinType::Char_U ||
  567. BT->getKind() == BuiltinType::UChar ||
  568. BT->getKind() == BuiltinType::Char_S ||
  569. BT->getKind() == BuiltinType::SChar;
  570. return false;
  571. }
  572. bool Type::isWideCharType() const {
  573. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  574. return BT->getKind() == BuiltinType::WChar_S ||
  575. BT->getKind() == BuiltinType::WChar_U;
  576. return false;
  577. }
  578. bool Type::isChar16Type() const {
  579. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  580. return BT->getKind() == BuiltinType::Char16;
  581. return false;
  582. }
  583. bool Type::isChar32Type() const {
  584. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  585. return BT->getKind() == BuiltinType::Char32;
  586. return false;
  587. }
  588. /// \brief Determine whether this type is any of the built-in character
  589. /// types.
  590. bool Type::isAnyCharacterType() const {
  591. const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
  592. if (BT == 0) return false;
  593. switch (BT->getKind()) {
  594. default: return false;
  595. case BuiltinType::Char_U:
  596. case BuiltinType::UChar:
  597. case BuiltinType::WChar_U:
  598. case BuiltinType::Char16:
  599. case BuiltinType::Char32:
  600. case BuiltinType::Char_S:
  601. case BuiltinType::SChar:
  602. case BuiltinType::WChar_S:
  603. return true;
  604. }
  605. }
  606. /// isSignedIntegerType - Return true if this is an integer type that is
  607. /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
  608. /// an enum decl which has a signed representation
  609. bool Type::isSignedIntegerType() const {
  610. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
  611. return BT->getKind() >= BuiltinType::Char_S &&
  612. BT->getKind() <= BuiltinType::Int128;
  613. }
  614. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
  615. // Incomplete enum types are not treated as integer types.
  616. // FIXME: In C++, enum types are never integer types.
  617. if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
  618. return ET->getDecl()->getIntegerType()->isSignedIntegerType();
  619. }
  620. return false;
  621. }
  622. bool Type::isSignedIntegerOrEnumerationType() const {
  623. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
  624. return BT->getKind() >= BuiltinType::Char_S &&
  625. BT->getKind() <= BuiltinType::Int128;
  626. }
  627. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
  628. if (ET->getDecl()->isComplete())
  629. return ET->getDecl()->getIntegerType()->isSignedIntegerType();
  630. }
  631. return false;
  632. }
  633. bool Type::hasSignedIntegerRepresentation() const {
  634. if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
  635. return VT->getElementType()->isSignedIntegerType();
  636. else
  637. return isSignedIntegerType();
  638. }
  639. /// isUnsignedIntegerType - Return true if this is an integer type that is
  640. /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
  641. /// decl which has an unsigned representation
  642. bool Type::isUnsignedIntegerType() const {
  643. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
  644. return BT->getKind() >= BuiltinType::Bool &&
  645. BT->getKind() <= BuiltinType::UInt128;
  646. }
  647. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
  648. // Incomplete enum types are not treated as integer types.
  649. // FIXME: In C++, enum types are never integer types.
  650. if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
  651. return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
  652. }
  653. return false;
  654. }
  655. bool Type::isUnsignedIntegerOrEnumerationType() const {
  656. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
  657. return BT->getKind() >= BuiltinType::Bool &&
  658. BT->getKind() <= BuiltinType::UInt128;
  659. }
  660. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
  661. if (ET->getDecl()->isComplete())
  662. return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
  663. }
  664. return false;
  665. }
  666. bool Type::hasUnsignedIntegerRepresentation() const {
  667. if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
  668. return VT->getElementType()->isUnsignedIntegerType();
  669. else
  670. return isUnsignedIntegerType();
  671. }
  672. bool Type::isHalfType() const {
  673. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  674. return BT->getKind() == BuiltinType::Half;
  675. // FIXME: Should we allow complex __fp16? Probably not.
  676. return false;
  677. }
  678. bool Type::isFloatingType() const {
  679. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  680. return BT->getKind() >= BuiltinType::Half &&
  681. BT->getKind() <= BuiltinType::LongDouble;
  682. if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
  683. return CT->getElementType()->isFloatingType();
  684. return false;
  685. }
  686. bool Type::hasFloatingRepresentation() const {
  687. if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
  688. return VT->getElementType()->isFloatingType();
  689. else
  690. return isFloatingType();
  691. }
  692. bool Type::isRealFloatingType() const {
  693. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  694. return BT->isFloatingPoint();
  695. return false;
  696. }
  697. bool Type::isRealType() const {
  698. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  699. return BT->getKind() >= BuiltinType::Bool &&
  700. BT->getKind() <= BuiltinType::LongDouble;
  701. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
  702. return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
  703. return false;
  704. }
  705. bool Type::isArithmeticType() const {
  706. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  707. return BT->getKind() >= BuiltinType::Bool &&
  708. BT->getKind() <= BuiltinType::LongDouble;
  709. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
  710. // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
  711. // If a body isn't seen by the time we get here, return false.
  712. //
  713. // C++0x: Enumerations are not arithmetic types. For now, just return
  714. // false for scoped enumerations since that will disable any
  715. // unwanted implicit conversions.
  716. return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete();
  717. return isa<ComplexType>(CanonicalType);
  718. }
  719. bool Type::isScalarType() const {
  720. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  721. return BT->getKind() > BuiltinType::Void &&
  722. BT->getKind() <= BuiltinType::NullPtr;
  723. if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
  724. // Enums are scalar types, but only if they are defined. Incomplete enums
  725. // are not treated as scalar types.
  726. return ET->getDecl()->isComplete();
  727. return isa<PointerType>(CanonicalType) ||
  728. isa<BlockPointerType>(CanonicalType) ||
  729. isa<MemberPointerType>(CanonicalType) ||
  730. isa<ComplexType>(CanonicalType) ||
  731. isa<ObjCObjectPointerType>(CanonicalType);
  732. }
  733. Type::ScalarTypeKind Type::getScalarTypeKind() const {
  734. assert(isScalarType());
  735. const Type *T = CanonicalType.getTypePtr();
  736. if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
  737. if (BT->getKind() == BuiltinType::Bool) return STK_Bool;
  738. if (BT->getKind() == BuiltinType::NullPtr) return STK_CPointer;
  739. if (BT->isInteger()) return STK_Integral;
  740. if (BT->isFloatingPoint()) return STK_Floating;
  741. llvm_unreachable("unknown scalar builtin type");
  742. } else if (isa<PointerType>(T)) {
  743. return STK_CPointer;
  744. } else if (isa<BlockPointerType>(T)) {
  745. return STK_BlockPointer;
  746. } else if (isa<ObjCObjectPointerType>(T)) {
  747. return STK_ObjCObjectPointer;
  748. } else if (isa<MemberPointerType>(T)) {
  749. return STK_MemberPointer;
  750. } else if (isa<EnumType>(T)) {
  751. assert(cast<EnumType>(T)->getDecl()->isComplete());
  752. return STK_Integral;
  753. } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) {
  754. if (CT->getElementType()->isRealFloatingType())
  755. return STK_FloatingComplex;
  756. return STK_IntegralComplex;
  757. }
  758. llvm_unreachable("unknown scalar type");
  759. }
  760. /// \brief Determines whether the type is a C++ aggregate type or C
  761. /// aggregate or union type.
  762. ///
  763. /// An aggregate type is an array or a class type (struct, union, or
  764. /// class) that has no user-declared constructors, no private or
  765. /// protected non-static data members, no base classes, and no virtual
  766. /// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
  767. /// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
  768. /// includes union types.
  769. bool Type::isAggregateType() const {
  770. if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
  771. if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
  772. return ClassDecl->isAggregate();
  773. return true;
  774. }
  775. return isa<ArrayType>(CanonicalType);
  776. }
  777. /// isConstantSizeType - Return true if this is not a variable sized type,
  778. /// according to the rules of C99 6.7.5p3. It is not legal to call this on
  779. /// incomplete types or dependent types.
  780. bool Type::isConstantSizeType() const {
  781. assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
  782. assert(!isDependentType() && "This doesn't make sense for dependent types");
  783. // The VAT must have a size, as it is known to be complete.
  784. return !isa<VariableArrayType>(CanonicalType);
  785. }
  786. /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
  787. /// - a type that can describe objects, but which lacks information needed to
  788. /// determine its size.
  789. bool Type::isIncompleteType(NamedDecl **Def) const {
  790. if (Def)
  791. *Def = 0;
  792. switch (CanonicalType->getTypeClass()) {
  793. default: return false;
  794. case Builtin:
  795. // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
  796. // be completed.
  797. return isVoidType();
  798. case Enum: {
  799. EnumDecl *EnumD = cast<EnumType>(CanonicalType)->getDecl();
  800. if (Def)
  801. *Def = EnumD;
  802. // An enumeration with fixed underlying type is complete (C++0x 7.2p3).
  803. if (EnumD->isFixed())
  804. return false;
  805. return !EnumD->isCompleteDefinition();
  806. }
  807. case Record: {
  808. // A tagged type (struct/union/enum/class) is incomplete if the decl is a
  809. // forward declaration, but not a full definition (C99 6.2.5p22).
  810. RecordDecl *Rec = cast<RecordType>(CanonicalType)->getDecl();
  811. if (Def)
  812. *Def = Rec;
  813. return !Rec->isCompleteDefinition();
  814. }
  815. case ConstantArray:
  816. // An array is incomplete if its element type is incomplete
  817. // (C++ [dcl.array]p1).
  818. // We don't handle variable arrays (they're not allowed in C++) or
  819. // dependent-sized arrays (dependent types are never treated as incomplete).
  820. return cast<ArrayType>(CanonicalType)->getElementType()
  821. ->isIncompleteType(Def);
  822. case IncompleteArray:
  823. // An array of unknown size is an incomplete type (C99 6.2.5p22).
  824. return true;
  825. case ObjCObject:
  826. return cast<ObjCObjectType>(CanonicalType)->getBaseType()
  827. ->isIncompleteType(Def);
  828. case ObjCInterface: {
  829. // ObjC interfaces are incomplete if they are @class, not @interface.
  830. ObjCInterfaceDecl *Interface
  831. = cast<ObjCInterfaceType>(CanonicalType)->getDecl();
  832. if (Def)
  833. *Def = Interface;
  834. return !Interface->hasDefinition();
  835. }
  836. }
  837. }
  838. bool QualType::isPODType(ASTContext &Context) const {
  839. // The compiler shouldn't query this for incomplete types, but the user might.
  840. // We return false for that case. Except for incomplete arrays of PODs, which
  841. // are PODs according to the standard.
  842. if (isNull())
  843. return 0;
  844. if ((*this)->isIncompleteArrayType())
  845. return Context.getBaseElementType(*this).isPODType(Context);
  846. if ((*this)->isIncompleteType())
  847. return false;
  848. if (Context.getLangOptions().ObjCAutoRefCount) {
  849. switch (getObjCLifetime()) {
  850. case Qualifiers::OCL_ExplicitNone:
  851. return true;
  852. case Qualifiers::OCL_Strong:
  853. case Qualifiers::OCL_Weak:
  854. case Qualifiers::OCL_Autoreleasing:
  855. return false;
  856. case Qualifiers::OCL_None:
  857. break;
  858. }
  859. }
  860. QualType CanonicalType = getTypePtr()->CanonicalType;
  861. switch (CanonicalType->getTypeClass()) {
  862. // Everything not explicitly mentioned is not POD.
  863. default: return false;
  864. case Type::VariableArray:
  865. case Type::ConstantArray:
  866. // IncompleteArray is handled above.
  867. return Context.getBaseElementType(*this).isPODType(Context);
  868. case Type::ObjCObjectPointer:
  869. case Type::BlockPointer:
  870. case Type::Builtin:
  871. case Type::Complex:
  872. case Type::Pointer:
  873. case Type::MemberPointer:
  874. case Type::Vector:
  875. case Type::ExtVector:
  876. return true;
  877. case Type::Enum:
  878. return true;
  879. case Type::Record:
  880. if (CXXRecordDecl *ClassDecl
  881. = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
  882. return ClassDecl->isPOD();
  883. // C struct/union is POD.
  884. return true;
  885. }
  886. }
  887. bool QualType::isTrivialType(ASTContext &Context) const {
  888. // The compiler shouldn't query this for incomplete types, but the user might.
  889. // We return false for that case. Except for incomplete arrays of PODs, which
  890. // are PODs according to the standard.
  891. if (isNull())
  892. return 0;
  893. if ((*this)->isArrayType())
  894. return Context.getBaseElementType(*this).isTrivialType(Context);
  895. // Return false for incomplete types after skipping any incomplete array
  896. // types which are expressly allowed by the standard and thus our API.
  897. if ((*this)->isIncompleteType())
  898. return false;
  899. if (Context.getLangOptions().ObjCAutoRefCount) {
  900. switch (getObjCLifetime()) {
  901. case Qualifiers::OCL_ExplicitNone:
  902. return true;
  903. case Qualifiers::OCL_Strong:
  904. case Qualifiers::OCL_Weak:
  905. case Qualifiers::OCL_Autoreleasing:
  906. return false;
  907. case Qualifiers::OCL_None:
  908. if ((*this)->isObjCLifetimeType())
  909. return false;
  910. break;
  911. }
  912. }
  913. QualType CanonicalType = getTypePtr()->CanonicalType;
  914. if (CanonicalType->isDependentType())
  915. return false;
  916. // C++0x [basic.types]p9:
  917. // Scalar types, trivial class types, arrays of such types, and
  918. // cv-qualified versions of these types are collectively called trivial
  919. // types.
  920. // As an extension, Clang treats vector types as Scalar types.
  921. if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
  922. return true;
  923. if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
  924. if (const CXXRecordDecl *ClassDecl =
  925. dyn_cast<CXXRecordDecl>(RT->getDecl())) {
  926. // C++0x [class]p5:
  927. // A trivial class is a class that has a trivial default constructor
  928. if (!ClassDecl->hasTrivialDefaultConstructor()) return false;
  929. // and is trivially copyable.
  930. if (!ClassDecl->isTriviallyCopyable()) return false;
  931. }
  932. return true;
  933. }
  934. // No other types can match.
  935. return false;
  936. }
  937. bool QualType::isTriviallyCopyableType(ASTContext &Context) const {
  938. if ((*this)->isArrayType())
  939. return Context.getBaseElementType(*this).isTrivialType(Context);
  940. if (Context.getLangOptions().ObjCAutoRefCount) {
  941. switch (getObjCLifetime()) {
  942. case Qualifiers::OCL_ExplicitNone:
  943. return true;
  944. case Qualifiers::OCL_Strong:
  945. case Qualifiers::OCL_Weak:
  946. case Qualifiers::OCL_Autoreleasing:
  947. return false;
  948. case Qualifiers::OCL_None:
  949. if ((*this)->isObjCLifetimeType())
  950. return false;
  951. break;
  952. }
  953. }
  954. // C++0x [basic.types]p9
  955. // Scalar types, trivially copyable class types, arrays of such types, and
  956. // cv-qualified versions of these types are collectively called trivial
  957. // types.
  958. QualType CanonicalType = getCanonicalType();
  959. if (CanonicalType->isDependentType())
  960. return false;
  961. // Return false for incomplete types after skipping any incomplete array types
  962. // which are expressly allowed by the standard and thus our API.
  963. if (CanonicalType->isIncompleteType())
  964. return false;
  965. // As an extension, Clang treats vector types as Scalar types.
  966. if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
  967. return true;
  968. if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
  969. if (const CXXRecordDecl *ClassDecl =
  970. dyn_cast<CXXRecordDecl>(RT->getDecl())) {
  971. if (!ClassDecl->isTriviallyCopyable()) return false;
  972. }
  973. return true;
  974. }
  975. // No other types can match.
  976. return false;
  977. }
  978. bool Type::isLiteralType() const {
  979. if (isDependentType())
  980. return false;
  981. // C++0x [basic.types]p10:
  982. // A type is a literal type if it is:
  983. // [...]
  984. // -- an array of literal type.
  985. // Extension: variable arrays cannot be literal types, since they're
  986. // runtime-sized.
  987. if (isVariableArrayType())
  988. return false;
  989. const Type *BaseTy = getBaseElementTypeUnsafe();
  990. assert(BaseTy && "NULL element type");
  991. // Return false for incomplete types after skipping any incomplete array
  992. // types; those are expressly allowed by the standard and thus our API.
  993. if (BaseTy->isIncompleteType())
  994. return false;
  995. // C++0x [basic.types]p10:
  996. // A type is a literal type if it is:
  997. // -- a scalar type; or
  998. // As an extension, Clang treats vector types and complex types as
  999. // literal types.
  1000. if (BaseTy->isScalarType() || BaseTy->isVectorType() ||
  1001. BaseTy->isAnyComplexType())
  1002. return true;
  1003. // -- a reference type; or
  1004. if (BaseTy->isReferenceType())
  1005. return true;
  1006. // -- a class type that has all of the following properties:
  1007. if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
  1008. // -- a trivial destructor,
  1009. // -- every constructor call and full-expression in the
  1010. // brace-or-equal-initializers for non-static data members (if any)
  1011. // is a constant expression,
  1012. // -- it is an aggregate type or has at least one constexpr
  1013. // constructor or constructor template that is not a copy or move
  1014. // constructor, and
  1015. // -- all non-static data members and base classes of literal types
  1016. //
  1017. // We resolve DR1361 by ignoring the second bullet.
  1018. if (const CXXRecordDecl *ClassDecl =
  1019. dyn_cast<CXXRecordDecl>(RT->getDecl()))
  1020. return ClassDecl->isLiteral();
  1021. return true;
  1022. }
  1023. return false;
  1024. }
  1025. bool Type::isStandardLayoutType() const {
  1026. if (isDependentType())
  1027. return false;
  1028. // C++0x [basic.types]p9:
  1029. // Scalar types, standard-layout class types, arrays of such types, and
  1030. // cv-qualified versions of these types are collectively called
  1031. // standard-layout types.
  1032. const Type *BaseTy = getBaseElementTypeUnsafe();
  1033. assert(BaseTy && "NULL element type");
  1034. // Return false for incomplete types after skipping any incomplete array
  1035. // types which are expressly allowed by the standard and thus our API.
  1036. if (BaseTy->isIncompleteType())
  1037. return false;
  1038. // As an extension, Clang treats vector types as Scalar types.
  1039. if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
  1040. if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
  1041. if (const CXXRecordDecl *ClassDecl =
  1042. dyn_cast<CXXRecordDecl>(RT->getDecl()))
  1043. if (!ClassDecl->isStandardLayout())
  1044. return false;
  1045. // Default to 'true' for non-C++ class types.
  1046. // FIXME: This is a bit dubious, but plain C structs should trivially meet
  1047. // all the requirements of standard layout classes.
  1048. return true;
  1049. }
  1050. // No other types can match.
  1051. return false;
  1052. }
  1053. // This is effectively the intersection of isTrivialType and
  1054. // isStandardLayoutType. We implement it directly to avoid redundant
  1055. // conversions from a type to a CXXRecordDecl.
  1056. bool QualType::isCXX11PODType(ASTContext &Context) const {
  1057. const Type *ty = getTypePtr();
  1058. if (ty->isDependentType())
  1059. return false;
  1060. if (Context.getLangOptions().ObjCAutoRefCount) {
  1061. switch (getObjCLifetime()) {
  1062. case Qualifiers::OCL_ExplicitNone:
  1063. return true;
  1064. case Qualifiers::OCL_Strong:
  1065. case Qualifiers::OCL_Weak:
  1066. case Qualifiers::OCL_Autoreleasing:
  1067. return false;
  1068. case Qualifiers::OCL_None:
  1069. if (ty->isObjCLifetimeType())
  1070. return false;
  1071. break;
  1072. }
  1073. }
  1074. // C++11 [basic.types]p9:
  1075. // Scalar types, POD classes, arrays of such types, and cv-qualified
  1076. // versions of these types are collectively called trivial types.
  1077. const Type *BaseTy = ty->getBaseElementTypeUnsafe();
  1078. assert(BaseTy && "NULL element type");
  1079. // Return false for incomplete types after skipping any incomplete array
  1080. // types which are expressly allowed by the standard and thus our API.
  1081. if (BaseTy->isIncompleteType())
  1082. return false;
  1083. // As an extension, Clang treats vector types as Scalar types.
  1084. if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
  1085. if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
  1086. if (const CXXRecordDecl *ClassDecl =
  1087. dyn_cast<CXXRecordDecl>(RT->getDecl())) {
  1088. // C++11 [class]p10:
  1089. // A POD struct is a non-union class that is both a trivial class [...]
  1090. if (!ClassDecl->isTrivial()) return false;
  1091. // C++11 [class]p10:
  1092. // A POD struct is a non-union class that is both a trivial class and
  1093. // a standard-layout class [...]
  1094. if (!ClassDecl->isStandardLayout()) return false;
  1095. // C++11 [class]p10:
  1096. // A POD struct is a non-union class that is both a trivial class and
  1097. // a standard-layout class, and has no non-static data members of type
  1098. // non-POD struct, non-POD union (or array of such types). [...]
  1099. //
  1100. // We don't directly query the recursive aspect as the requiremets for
  1101. // both standard-layout classes and trivial classes apply recursively
  1102. // already.
  1103. }
  1104. return true;
  1105. }
  1106. // No other types can match.
  1107. return false;
  1108. }
  1109. bool Type::isPromotableIntegerType() const {
  1110. if (const BuiltinType *BT = getAs<BuiltinType>())
  1111. switch (BT->getKind()) {
  1112. case BuiltinType::Bool:
  1113. case BuiltinType::Char_S:
  1114. case BuiltinType::Char_U:
  1115. case BuiltinType::SChar:
  1116. case BuiltinType::UChar:
  1117. case BuiltinType::Short:
  1118. case BuiltinType::UShort:
  1119. case BuiltinType::WChar_S:
  1120. case BuiltinType::WChar_U:
  1121. case BuiltinType::Char16:
  1122. case BuiltinType::Char32:
  1123. return true;
  1124. default:
  1125. return false;
  1126. }
  1127. // Enumerated types are promotable to their compatible integer types
  1128. // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
  1129. if (const EnumType *ET = getAs<EnumType>()){
  1130. if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()
  1131. || ET->getDecl()->isScoped())
  1132. return false;
  1133. return true;
  1134. }
  1135. return false;
  1136. }
  1137. bool Type::isNullPtrType() const {
  1138. if (const BuiltinType *BT = getAs<BuiltinType>())
  1139. return BT->getKind() == BuiltinType::NullPtr;
  1140. return false;
  1141. }
  1142. bool Type::isSpecifierType() const {
  1143. // Note that this intentionally does not use the canonical type.
  1144. switch (getTypeClass()) {
  1145. case Builtin:
  1146. case Record:
  1147. case Enum:
  1148. case Typedef:
  1149. case Complex:
  1150. case TypeOfExpr:
  1151. case TypeOf:
  1152. case TemplateTypeParm:
  1153. case SubstTemplateTypeParm:
  1154. case TemplateSpecialization:
  1155. case Elaborated:
  1156. case DependentName:
  1157. case DependentTemplateSpecialization:
  1158. case ObjCInterface:
  1159. case ObjCObject:
  1160. case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
  1161. return true;
  1162. default:
  1163. return false;
  1164. }
  1165. }
  1166. ElaboratedTypeKeyword
  1167. TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
  1168. switch (TypeSpec) {
  1169. default: return ETK_None;
  1170. case TST_typename: return ETK_Typename;
  1171. case TST_class: return ETK_Class;
  1172. case TST_struct: return ETK_Struct;
  1173. case TST_union: return ETK_Union;
  1174. case TST_enum: return ETK_Enum;
  1175. }
  1176. }
  1177. TagTypeKind
  1178. TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
  1179. switch(TypeSpec) {
  1180. case TST_class: return TTK_Class;
  1181. case TST_struct: return TTK_Struct;
  1182. case TST_union: return TTK_Union;
  1183. case TST_enum: return TTK_Enum;
  1184. }
  1185. llvm_unreachable("Type specifier is not a tag type kind.");
  1186. }
  1187. ElaboratedTypeKeyword
  1188. TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
  1189. switch (Kind) {
  1190. case TTK_Class: return ETK_Class;
  1191. case TTK_Struct: return ETK_Struct;
  1192. case TTK_Union: return ETK_Union;
  1193. case TTK_Enum: return ETK_Enum;
  1194. }
  1195. llvm_unreachable("Unknown tag type kind.");
  1196. }
  1197. TagTypeKind
  1198. TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
  1199. switch (Keyword) {
  1200. case ETK_Class: return TTK_Class;
  1201. case ETK_Struct: return TTK_Struct;
  1202. case ETK_Union: return TTK_Union;
  1203. case ETK_Enum: return TTK_Enum;
  1204. case ETK_None: // Fall through.
  1205. case ETK_Typename:
  1206. llvm_unreachable("Elaborated type keyword is not a tag type kind.");
  1207. }
  1208. llvm_unreachable("Unknown elaborated type keyword.");
  1209. }
  1210. bool
  1211. TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
  1212. switch (Keyword) {
  1213. case ETK_None:
  1214. case ETK_Typename:
  1215. return false;
  1216. case ETK_Class:
  1217. case ETK_Struct:
  1218. case ETK_Union:
  1219. case ETK_Enum:
  1220. return true;
  1221. }
  1222. llvm_unreachable("Unknown elaborated type keyword.");
  1223. }
  1224. const char*
  1225. TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
  1226. switch (Keyword) {
  1227. case ETK_None: return "";
  1228. case ETK_Typename: return "typename";
  1229. case ETK_Class: return "class";
  1230. case ETK_Struct: return "struct";
  1231. case ETK_Union: return "union";
  1232. case ETK_Enum: return "enum";
  1233. }
  1234. llvm_unreachable("Unknown elaborated type keyword.");
  1235. }
  1236. DependentTemplateSpecializationType::DependentTemplateSpecializationType(
  1237. ElaboratedTypeKeyword Keyword,
  1238. NestedNameSpecifier *NNS, const IdentifierInfo *Name,
  1239. unsigned NumArgs, const TemplateArgument *Args,
  1240. QualType Canon)
  1241. : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true, true,
  1242. /*VariablyModified=*/false,
  1243. NNS && NNS->containsUnexpandedParameterPack()),
  1244. NNS(NNS), Name(Name), NumArgs(NumArgs) {
  1245. assert((!NNS || NNS->isDependent()) &&
  1246. "DependentTemplateSpecializatonType requires dependent qualifier");
  1247. for (unsigned I = 0; I != NumArgs; ++I) {
  1248. if (Args[I].containsUnexpandedParameterPack())
  1249. setContainsUnexpandedParameterPack();
  1250. new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
  1251. }
  1252. }
  1253. void
  1254. DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
  1255. const ASTContext &Context,
  1256. ElaboratedTypeKeyword Keyword,
  1257. NestedNameSpecifier *Qualifier,
  1258. const IdentifierInfo *Name,
  1259. unsigned NumArgs,
  1260. const TemplateArgument *Args) {
  1261. ID.AddInteger(Keyword);
  1262. ID.AddPointer(Qualifier);
  1263. ID.AddPointer(Name);
  1264. for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
  1265. Args[Idx].Profile(ID, Context);
  1266. }
  1267. bool Type::isElaboratedTypeSpecifier() const {
  1268. ElaboratedTypeKeyword Keyword;
  1269. if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
  1270. Keyword = Elab->getKeyword();
  1271. else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
  1272. Keyword = DepName->getKeyword();
  1273. else if (const DependentTemplateSpecializationType *DepTST =
  1274. dyn_cast<DependentTemplateSpecializationType>(this))
  1275. Keyword = DepTST->getKeyword();
  1276. else
  1277. return false;
  1278. return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
  1279. }
  1280. const char *Type::getTypeClassName() const {
  1281. switch (TypeBits.TC) {
  1282. #define ABSTRACT_TYPE(Derived, Base)
  1283. #define TYPE(Derived, Base) case Derived: return #Derived;
  1284. #include "clang/AST/TypeNodes.def"
  1285. }
  1286. llvm_unreachable("Invalid type class.");
  1287. }
  1288. const char *BuiltinType::getName(const PrintingPolicy &Policy) const {
  1289. switch (getKind()) {
  1290. case Void: return "void";
  1291. case Bool: return Policy.Bool ? "bool" : "_Bool";
  1292. case Char_S: return "char";
  1293. case Char_U: return "char";
  1294. case SChar: return "signed char";
  1295. case Short: return "short";
  1296. case Int: return "int";
  1297. case Long: return "long";
  1298. case LongLong: return "long long";
  1299. case Int128: return "__int128_t";
  1300. case UChar: return "unsigned char";
  1301. case UShort: return "unsigned short";
  1302. case UInt: return "unsigned int";
  1303. case ULong: return "unsigned long";
  1304. case ULongLong: return "unsigned long long";
  1305. case UInt128: return "__uint128_t";
  1306. case Half: return "half";
  1307. case Float: return "float";
  1308. case Double: return "double";
  1309. case LongDouble: return "long double";
  1310. case WChar_S:
  1311. case WChar_U: return "wchar_t";
  1312. case Char16: return "char16_t";
  1313. case Char32: return "char32_t";
  1314. case NullPtr: return "nullptr_t";
  1315. case Overload: return "<overloaded function type>";
  1316. case BoundMember: return "<bound member function type>";
  1317. case PseudoObject: return "<pseudo-object type>";
  1318. case Dependent: return "<dependent type>";
  1319. case UnknownAny: return "<unknown type>";
  1320. case ARCUnbridgedCast: return "<ARC unbridged cast type>";
  1321. case ObjCId: return "id";
  1322. case ObjCClass: return "Class";
  1323. case ObjCSel: return "SEL";
  1324. }
  1325. llvm_unreachable("Invalid builtin type.");
  1326. }
  1327. QualType QualType::getNonLValueExprType(ASTContext &Context) const {
  1328. if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
  1329. return RefType->getPointeeType();
  1330. // C++0x [basic.lval]:
  1331. // Class prvalues can have cv-qualified types; non-class prvalues always
  1332. // have cv-unqualified types.
  1333. //
  1334. // See also C99 6.3.2.1p2.
  1335. if (!Context.getLangOptions().CPlusPlus ||
  1336. (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
  1337. return getUnqualifiedType();
  1338. return *this;
  1339. }
  1340. StringRef FunctionType::getNameForCallConv(CallingConv CC) {
  1341. switch (CC) {
  1342. case CC_Default:
  1343. llvm_unreachable("no name for default cc");
  1344. case CC_C: return "cdecl";
  1345. case CC_X86StdCall: return "stdcall";
  1346. case CC_X86FastCall: return "fastcall";
  1347. case CC_X86ThisCall: return "thiscall";
  1348. case CC_X86Pascal: return "pascal";
  1349. case CC_AAPCS: return "aapcs";
  1350. case CC_AAPCS_VFP: return "aapcs-vfp";
  1351. }
  1352. llvm_unreachable("Invalid calling convention.");
  1353. }
  1354. FunctionProtoType::FunctionProtoType(QualType result, const QualType *args,
  1355. unsigned numArgs, QualType canonical,
  1356. const ExtProtoInfo &epi)
  1357. : FunctionType(FunctionProto, result, epi.Variadic, epi.TypeQuals,
  1358. epi.RefQualifier, canonical,
  1359. result->isDependentType(),
  1360. result->isInstantiationDependentType(),
  1361. result->isVariablyModifiedType(),
  1362. result->containsUnexpandedParameterPack(),
  1363. epi.ExtInfo),
  1364. NumArgs(numArgs), NumExceptions(epi.NumExceptions),
  1365. ExceptionSpecType(epi.ExceptionSpecType),
  1366. HasAnyConsumedArgs(epi.ConsumedArguments != 0)
  1367. {
  1368. // Fill in the trailing argument array.
  1369. QualType *argSlot = reinterpret_cast<QualType*>(this+1);
  1370. for (unsigned i = 0; i != numArgs; ++i) {
  1371. if (args[i]->isDependentType())
  1372. setDependent();
  1373. else if (args[i]->isInstantiationDependentType())
  1374. setInstantiationDependent();
  1375. if (args[i]->containsUnexpandedParameterPack())
  1376. setContainsUnexpandedParameterPack();
  1377. argSlot[i] = args[i];
  1378. }
  1379. if (getExceptionSpecType() == EST_Dynamic) {
  1380. // Fill in the exception array.
  1381. QualType *exnSlot = argSlot + numArgs;
  1382. for (unsigned i = 0, e = epi.NumExceptions; i != e; ++i) {
  1383. if (epi.Exceptions[i]->isDependentType())
  1384. setDependent();
  1385. else if (epi.Exceptions[i]->isInstantiationDependentType())
  1386. setInstantiationDependent();
  1387. if (epi.Exceptions[i]->containsUnexpandedParameterPack())
  1388. setContainsUnexpandedParameterPack();
  1389. exnSlot[i] = epi.Exceptions[i];
  1390. }
  1391. } else if (getExceptionSpecType() == EST_ComputedNoexcept) {
  1392. // Store the noexcept expression and context.
  1393. Expr **noexSlot = reinterpret_cast<Expr**>(argSlot + numArgs);
  1394. *noexSlot = epi.NoexceptExpr;
  1395. if (epi.NoexceptExpr) {
  1396. if (epi.NoexceptExpr->isValueDependent()
  1397. || epi.NoexceptExpr->isTypeDependent())
  1398. setDependent();
  1399. else if (epi.NoexceptExpr->isInstantiationDependent())
  1400. setInstantiationDependent();
  1401. }
  1402. }
  1403. if (epi.ConsumedArguments) {
  1404. bool *consumedArgs = const_cast<bool*>(getConsumedArgsBuffer());
  1405. for (unsigned i = 0; i != numArgs; ++i)
  1406. consumedArgs[i] = epi.ConsumedArguments[i];
  1407. }
  1408. }
  1409. FunctionProtoType::NoexceptResult
  1410. FunctionProtoType::getNoexceptSpec(ASTContext &ctx) const {
  1411. ExceptionSpecificationType est = getExceptionSpecType();
  1412. if (est == EST_BasicNoexcept)
  1413. return NR_Nothrow;
  1414. if (est != EST_ComputedNoexcept)
  1415. return NR_NoNoexcept;
  1416. Expr *noexceptExpr = getNoexceptExpr();
  1417. if (!noexceptExpr)
  1418. return NR_BadNoexcept;
  1419. if (noexceptExpr->isValueDependent())
  1420. return NR_Dependent;
  1421. llvm::APSInt value;
  1422. bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, 0,
  1423. /*evaluated*/false);
  1424. (void)isICE;
  1425. assert(isICE && "AST should not contain bad noexcept expressions.");
  1426. return value.getBoolValue() ? NR_Nothrow : NR_Throw;
  1427. }
  1428. bool FunctionProtoType::isTemplateVariadic() const {
  1429. for (unsigned ArgIdx = getNumArgs(); ArgIdx; --ArgIdx)
  1430. if (isa<PackExpansionType>(getArgType(ArgIdx - 1)))
  1431. return true;
  1432. return false;
  1433. }
  1434. void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
  1435. const QualType *ArgTys, unsigned NumArgs,
  1436. const ExtProtoInfo &epi,
  1437. const ASTContext &Context) {
  1438. // We have to be careful not to get ambiguous profile encodings.
  1439. // Note that valid type pointers are never ambiguous with anything else.
  1440. //
  1441. // The encoding grammar begins:
  1442. // type type* bool int bool
  1443. // If that final bool is true, then there is a section for the EH spec:
  1444. // bool type*
  1445. // This is followed by an optional "consumed argument" section of the
  1446. // same length as the first type sequence:
  1447. // bool*
  1448. // Finally, we have the ext info:
  1449. // int
  1450. //
  1451. // There is no ambiguity between the consumed arguments and an empty EH
  1452. // spec because of the leading 'bool' which unambiguously indicates
  1453. // whether the following bool is the EH spec or part of the arguments.
  1454. ID.AddPointer(Result.getAsOpaquePtr());
  1455. for (unsigned i = 0; i != NumArgs; ++i)
  1456. ID.AddPointer(ArgTys[i].getAsOpaquePtr());
  1457. // This method is relatively performance sensitive, so as a performance
  1458. // shortcut, use one AddInteger call instead of four for the next four
  1459. // fields.
  1460. assert(!(unsigned(epi.Variadic) & ~1) &&
  1461. !(unsigned(epi.TypeQuals) & ~255) &&
  1462. !(unsigned(epi.RefQualifier) & ~3) &&
  1463. !(unsigned(epi.ExceptionSpecType) & ~7) &&
  1464. "Values larger than expected.");
  1465. ID.AddInteger(unsigned(epi.Variadic) +
  1466. (epi.TypeQuals << 1) +
  1467. (epi.RefQualifier << 9) +
  1468. (epi.ExceptionSpecType << 11));
  1469. if (epi.ExceptionSpecType == EST_Dynamic) {
  1470. for (unsigned i = 0; i != epi.NumExceptions; ++i)
  1471. ID.AddPointer(epi.Exceptions[i].getAsOpaquePtr());
  1472. } else if (epi.ExceptionSpecType == EST_ComputedNoexcept && epi.NoexceptExpr){
  1473. epi.NoexceptExpr->Profile(ID, Context, false);
  1474. }
  1475. if (epi.ConsumedArguments) {
  1476. for (unsigned i = 0; i != NumArgs; ++i)
  1477. ID.AddBoolean(epi.ConsumedArguments[i]);
  1478. }
  1479. epi.ExtInfo.Profile(ID);
  1480. }
  1481. void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID,
  1482. const ASTContext &Ctx) {
  1483. Profile(ID, getResultType(), arg_type_begin(), NumArgs, getExtProtoInfo(),
  1484. Ctx);
  1485. }
  1486. QualType TypedefType::desugar() const {
  1487. return getDecl()->getUnderlyingType();
  1488. }
  1489. TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
  1490. : Type(TypeOfExpr, can, E->isTypeDependent(),
  1491. E->isInstantiationDependent(),
  1492. E->getType()->isVariablyModifiedType(),
  1493. E->containsUnexpandedParameterPack()),
  1494. TOExpr(E) {
  1495. }
  1496. bool TypeOfExprType::isSugared() const {
  1497. return !TOExpr->isTypeDependent();
  1498. }
  1499. QualType TypeOfExprType::desugar() const {
  1500. if (isSugared())
  1501. return getUnderlyingExpr()->getType();
  1502. return QualType(this, 0);
  1503. }
  1504. void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
  1505. const ASTContext &Context, Expr *E) {
  1506. E->Profile(ID, Context, true);
  1507. }
  1508. DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
  1509. // C++11 [temp.type]p2: "If an expression e involves a template parameter,
  1510. // decltype(e) denotes a unique dependent type." Hence a decltype type is
  1511. // type-dependent even if its expression is only instantiation-dependent.
  1512. : Type(Decltype, can, E->isInstantiationDependent(),
  1513. E->isInstantiationDependent(),
  1514. E->getType()->isVariablyModifiedType(),
  1515. E->containsUnexpandedParameterPack()),
  1516. E(E),
  1517. UnderlyingType(underlyingType) {
  1518. }
  1519. bool DecltypeType::isSugared() const { return !E->isInstantiationDependent(); }
  1520. QualType DecltypeType::desugar() const {
  1521. if (isSugared())
  1522. return getUnderlyingType();
  1523. return QualType(this, 0);
  1524. }
  1525. DependentDecltypeType::DependentDecltypeType(const ASTContext &Context, Expr *E)
  1526. : DecltypeType(E, Context.DependentTy), Context(Context) { }
  1527. void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
  1528. const ASTContext &Context, Expr *E) {
  1529. E->Profile(ID, Context, true);
  1530. }
  1531. TagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
  1532. : Type(TC, can, D->isDependentType(),
  1533. /*InstantiationDependent=*/D->isDependentType(),
  1534. /*VariablyModified=*/false,
  1535. /*ContainsUnexpandedParameterPack=*/false),
  1536. decl(const_cast<TagDecl*>(D)) {}
  1537. static TagDecl *getInterestingTagDecl(TagDecl *decl) {
  1538. for (TagDecl::redecl_iterator I = decl->redecls_begin(),
  1539. E = decl->redecls_end();
  1540. I != E; ++I) {
  1541. if (I->isCompleteDefinition() || I->isBeingDefined())
  1542. return *I;
  1543. }
  1544. // If there's no definition (not even in progress), return what we have.
  1545. return decl;
  1546. }
  1547. UnaryTransformType::UnaryTransformType(QualType BaseType,
  1548. QualType UnderlyingType,
  1549. UTTKind UKind,
  1550. QualType CanonicalType)
  1551. : Type(UnaryTransform, CanonicalType, UnderlyingType->isDependentType(),
  1552. UnderlyingType->isInstantiationDependentType(),
  1553. UnderlyingType->isVariablyModifiedType(),
  1554. BaseType->containsUnexpandedParameterPack())
  1555. , BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind)
  1556. {}
  1557. TagDecl *TagType::getDecl() const {
  1558. return getInterestingTagDecl(decl);
  1559. }
  1560. bool TagType::isBeingDefined() const {
  1561. return getDecl()->isBeingDefined();
  1562. }
  1563. CXXRecordDecl *InjectedClassNameType::getDecl() const {
  1564. return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
  1565. }
  1566. IdentifierInfo *TemplateTypeParmType::getIdentifier() const {
  1567. return isCanonicalUnqualified() ? 0 : getDecl()->getIdentifier();
  1568. }
  1569. SubstTemplateTypeParmPackType::
  1570. SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
  1571. QualType Canon,
  1572. const TemplateArgument &ArgPack)
  1573. : Type(SubstTemplateTypeParmPack, Canon, true, true, false, true),
  1574. Replaced(Param),
  1575. Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size())
  1576. {
  1577. }
  1578. TemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const {
  1579. return TemplateArgument(Arguments, NumArguments);
  1580. }
  1581. void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) {
  1582. Profile(ID, getReplacedParameter(), getArgumentPack());
  1583. }
  1584. void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID,
  1585. const TemplateTypeParmType *Replaced,
  1586. const TemplateArgument &ArgPack) {
  1587. ID.AddPointer(Replaced);
  1588. ID.AddInteger(ArgPack.pack_size());
  1589. for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
  1590. PEnd = ArgPack.pack_end();
  1591. P != PEnd; ++P)
  1592. ID.AddPointer(P->getAsType().getAsOpaquePtr());
  1593. }
  1594. bool TemplateSpecializationType::
  1595. anyDependentTemplateArguments(const TemplateArgumentListInfo &Args,
  1596. bool &InstantiationDependent) {
  1597. return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size(),
  1598. InstantiationDependent);
  1599. }
  1600. bool TemplateSpecializationType::
  1601. anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N,
  1602. bool &InstantiationDependent) {
  1603. for (unsigned i = 0; i != N; ++i) {
  1604. if (Args[i].getArgument().isDependent()) {
  1605. InstantiationDependent = true;
  1606. return true;
  1607. }
  1608. if (Args[i].getArgument().isInstantiationDependent())
  1609. InstantiationDependent = true;
  1610. }
  1611. return false;
  1612. }
  1613. bool TemplateSpecializationType::
  1614. anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N,
  1615. bool &InstantiationDependent) {
  1616. for (unsigned i = 0; i != N; ++i) {
  1617. if (Args[i].isDependent()) {
  1618. InstantiationDependent = true;
  1619. return true;
  1620. }
  1621. if (Args[i].isInstantiationDependent())
  1622. InstantiationDependent = true;
  1623. }
  1624. return false;
  1625. }
  1626. TemplateSpecializationType::
  1627. TemplateSpecializationType(TemplateName T,
  1628. const TemplateArgument *Args, unsigned NumArgs,
  1629. QualType Canon, QualType AliasedType)
  1630. : Type(TemplateSpecialization,
  1631. Canon.isNull()? QualType(this, 0) : Canon,
  1632. Canon.isNull()? T.isDependent() : Canon->isDependentType(),
  1633. Canon.isNull()? T.isDependent()
  1634. : Canon->isInstantiationDependentType(),
  1635. false,
  1636. Canon.isNull()? T.containsUnexpandedParameterPack()
  1637. : Canon->containsUnexpandedParameterPack()),
  1638. Template(T), NumArgs(NumArgs), TypeAlias(!AliasedType.isNull()) {
  1639. assert(!T.getAsDependentTemplateName() &&
  1640. "Use DependentTemplateSpecializationType for dependent template-name");
  1641. assert((T.getKind() == TemplateName::Template ||
  1642. T.getKind() == TemplateName::SubstTemplateTemplateParm ||
  1643. T.getKind() == TemplateName::SubstTemplateTemplateParmPack) &&
  1644. "Unexpected template name for TemplateSpecializationType");
  1645. bool InstantiationDependent;
  1646. (void)InstantiationDependent;
  1647. assert((!Canon.isNull() ||
  1648. T.isDependent() ||
  1649. anyDependentTemplateArguments(Args, NumArgs,
  1650. InstantiationDependent)) &&
  1651. "No canonical type for non-dependent class template specialization");
  1652. TemplateArgument *TemplateArgs
  1653. = reinterpret_cast<TemplateArgument *>(this + 1);
  1654. for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
  1655. // Update dependent and variably-modified bits.
  1656. // If the canonical type exists and is non-dependent, the template
  1657. // specialization type can be non-dependent even if one of the type
  1658. // arguments is. Given:
  1659. // template<typename T> using U = int;
  1660. // U<T> is always non-dependent, irrespective of the type T.
  1661. if (Canon.isNull() && Args[Arg].isDependent())
  1662. setDependent();
  1663. else if (Args[Arg].isInstantiationDependent())
  1664. setInstantiationDependent();
  1665. if (Args[Arg].getKind() == TemplateArgument::Type &&
  1666. Args[Arg].getAsType()->isVariablyModifiedType())
  1667. setVariablyModified();
  1668. if (Canon.isNull() && Args[Arg].containsUnexpandedParameterPack())
  1669. setContainsUnexpandedParameterPack();
  1670. new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
  1671. }
  1672. // Store the aliased type if this is a type alias template specialization.
  1673. if (TypeAlias) {
  1674. TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1);
  1675. *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType;
  1676. }
  1677. }
  1678. void
  1679. TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
  1680. TemplateName T,
  1681. const TemplateArgument *Args,
  1682. unsigned NumArgs,
  1683. const ASTContext &Context) {
  1684. T.Profile(ID);
  1685. for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
  1686. Args[Idx].Profile(ID, Context);
  1687. }
  1688. QualType
  1689. QualifierCollector::apply(const ASTContext &Context, QualType QT) const {
  1690. if (!hasNonFastQualifiers())
  1691. return QT.withFastQualifiers(getFastQualifiers());
  1692. return Context.getQualifiedType(QT, *this);
  1693. }
  1694. QualType
  1695. QualifierCollector::apply(const ASTContext &Context, const Type *T) const {
  1696. if (!hasNonFastQualifiers())
  1697. return QualType(T, getFastQualifiers());
  1698. return Context.getQualifiedType(T, *this);
  1699. }
  1700. void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
  1701. QualType BaseType,
  1702. ObjCProtocolDecl * const *Protocols,
  1703. unsigned NumProtocols) {
  1704. ID.AddPointer(BaseType.getAsOpaquePtr());
  1705. for (unsigned i = 0; i != NumProtocols; i++)
  1706. ID.AddPointer(Protocols[i]);
  1707. }
  1708. void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
  1709. Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
  1710. }
  1711. namespace {
  1712. /// \brief The cached properties of a type.
  1713. class CachedProperties {
  1714. NamedDecl::LinkageInfo LV;
  1715. bool local;
  1716. public:
  1717. CachedProperties(NamedDecl::LinkageInfo LV, bool local)
  1718. : LV(LV), local(local) {}
  1719. Linkage getLinkage() const { return LV.linkage(); }
  1720. Visibility getVisibility() const { return LV.visibility(); }
  1721. bool isVisibilityExplicit() const { return LV.visibilityExplicit(); }
  1722. bool hasLocalOrUnnamedType() const { return local; }
  1723. friend CachedProperties merge(CachedProperties L, CachedProperties R) {
  1724. NamedDecl::LinkageInfo MergedLV = L.LV;
  1725. MergedLV.merge(R.LV);
  1726. return CachedProperties(MergedLV,
  1727. L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
  1728. }
  1729. };
  1730. }
  1731. static CachedProperties computeCachedProperties(const Type *T);
  1732. namespace clang {
  1733. /// The type-property cache. This is templated so as to be
  1734. /// instantiated at an internal type to prevent unnecessary symbol
  1735. /// leakage.
  1736. template <class Private> class TypePropertyCache {
  1737. public:
  1738. static CachedProperties get(QualType T) {
  1739. return get(T.getTypePtr());
  1740. }
  1741. static CachedProperties get(const Type *T) {
  1742. ensure(T);
  1743. NamedDecl::LinkageInfo LV(T->TypeBits.getLinkage(),
  1744. T->TypeBits.getVisibility(),
  1745. T->TypeBits.isVisibilityExplicit());
  1746. return CachedProperties(LV, T->TypeBits.hasLocalOrUnnamedType());
  1747. }
  1748. static void ensure(const Type *T) {
  1749. // If the cache is valid, we're okay.
  1750. if (T->TypeBits.isCacheValid()) return;
  1751. // If this type is non-canonical, ask its canonical type for the
  1752. // relevant information.
  1753. if (!T->isCanonicalUnqualified()) {
  1754. const Type *CT = T->getCanonicalTypeInternal().getTypePtr();
  1755. ensure(CT);
  1756. T->TypeBits.CacheValidAndVisibility =
  1757. CT->TypeBits.CacheValidAndVisibility;
  1758. T->TypeBits.CachedExplicitVisibility =
  1759. CT->TypeBits.CachedExplicitVisibility;
  1760. T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage;
  1761. T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed;
  1762. return;
  1763. }
  1764. // Compute the cached properties and then set the cache.
  1765. CachedProperties Result = computeCachedProperties(T);
  1766. T->TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U;
  1767. T->TypeBits.CachedExplicitVisibility = Result.isVisibilityExplicit();
  1768. assert(T->TypeBits.isCacheValid() &&
  1769. T->TypeBits.getVisibility() == Result.getVisibility());
  1770. T->TypeBits.CachedLinkage = Result.getLinkage();
  1771. T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
  1772. }
  1773. };
  1774. }
  1775. // Instantiate the friend template at a private class. In a
  1776. // reasonable implementation, these symbols will be internal.
  1777. // It is terrible that this is the best way to accomplish this.
  1778. namespace { class Private {}; }
  1779. typedef TypePropertyCache<Private> Cache;
  1780. static CachedProperties computeCachedProperties(const Type *T) {
  1781. switch (T->getTypeClass()) {
  1782. #define TYPE(Class,Base)
  1783. #define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
  1784. #include "clang/AST/TypeNodes.def"
  1785. llvm_unreachable("didn't expect a non-canonical type here");
  1786. #define TYPE(Class,Base)
  1787. #define DEPENDENT_TYPE(Class,Base) case Type::Class:
  1788. #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
  1789. #include "clang/AST/TypeNodes.def"
  1790. // Treat instantiation-dependent types as external.
  1791. assert(T->isInstantiationDependentType());
  1792. return CachedProperties(NamedDecl::LinkageInfo(), false);
  1793. case Type::Builtin:
  1794. // C++ [basic.link]p8:
  1795. // A type is said to have linkage if and only if:
  1796. // - it is a fundamental type (3.9.1); or
  1797. return CachedProperties(NamedDecl::LinkageInfo(), false);
  1798. case Type::Record:
  1799. case Type::Enum: {
  1800. const TagDecl *Tag = cast<TagType>(T)->getDecl();
  1801. // C++ [basic.link]p8:
  1802. // - it is a class or enumeration type that is named (or has a name
  1803. // for linkage purposes (7.1.3)) and the name has linkage; or
  1804. // - it is a specialization of a class template (14); or
  1805. NamedDecl::LinkageInfo LV = Tag->getLinkageAndVisibility();
  1806. bool IsLocalOrUnnamed =
  1807. Tag->getDeclContext()->isFunctionOrMethod() ||
  1808. (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl());
  1809. return CachedProperties(LV, IsLocalOrUnnamed);
  1810. }
  1811. // C++ [basic.link]p8:
  1812. // - it is a compound type (3.9.2) other than a class or enumeration,
  1813. // compounded exclusively from types that have linkage; or
  1814. case Type::Complex:
  1815. return Cache::get(cast<ComplexType>(T)->getElementType());
  1816. case Type::Pointer:
  1817. return Cache::get(cast<PointerType>(T)->getPointeeType());
  1818. case Type::BlockPointer:
  1819. return Cache::get(cast<BlockPointerType>(T)->getPointeeType());
  1820. case Type::LValueReference:
  1821. case Type::RValueReference:
  1822. return Cache::get(cast<ReferenceType>(T)->getPointeeType());
  1823. case Type::MemberPointer: {
  1824. const MemberPointerType *MPT = cast<MemberPointerType>(T);
  1825. return merge(Cache::get(MPT->getClass()),
  1826. Cache::get(MPT->getPointeeType()));
  1827. }
  1828. case Type::ConstantArray:
  1829. case Type::IncompleteArray:
  1830. case Type::VariableArray:
  1831. return Cache::get(cast<ArrayType>(T)->getElementType());
  1832. case Type::Vector:
  1833. case Type::ExtVector:
  1834. return Cache::get(cast<VectorType>(T)->getElementType());
  1835. case Type::FunctionNoProto:
  1836. return Cache::get(cast<FunctionType>(T)->getResultType());
  1837. case Type::FunctionProto: {
  1838. const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
  1839. CachedProperties result = Cache::get(FPT->getResultType());
  1840. for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
  1841. ae = FPT->arg_type_end(); ai != ae; ++ai)
  1842. result = merge(result, Cache::get(*ai));
  1843. return result;
  1844. }
  1845. case Type::ObjCInterface: {
  1846. NamedDecl::LinkageInfo LV =
  1847. cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility();
  1848. return CachedProperties(LV, false);
  1849. }
  1850. case Type::ObjCObject:
  1851. return Cache::get(cast<ObjCObjectType>(T)->getBaseType());
  1852. case Type::ObjCObjectPointer:
  1853. return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType());
  1854. case Type::Atomic:
  1855. return Cache::get(cast<AtomicType>(T)->getValueType());
  1856. }
  1857. llvm_unreachable("unhandled type class");
  1858. // C++ [basic.link]p8:
  1859. // Names not covered by these rules have no linkage.
  1860. NamedDecl::LinkageInfo LV(NoLinkage, DefaultVisibility, false);
  1861. return CachedProperties(LV, false);
  1862. }
  1863. /// \brief Determine the linkage of this type.
  1864. Linkage Type::getLinkage() const {
  1865. Cache::ensure(this);
  1866. return TypeBits.getLinkage();
  1867. }
  1868. /// \brief Determine the linkage of this type.
  1869. Visibility Type::getVisibility() const {
  1870. Cache::ensure(this);
  1871. return TypeBits.getVisibility();
  1872. }
  1873. bool Type::isVisibilityExplicit() const {
  1874. Cache::ensure(this);
  1875. return TypeBits.isVisibilityExplicit();
  1876. }
  1877. bool Type::hasUnnamedOrLocalType() const {
  1878. Cache::ensure(this);
  1879. return TypeBits.hasLocalOrUnnamedType();
  1880. }
  1881. std::pair<Linkage,Visibility> Type::getLinkageAndVisibility() const {
  1882. Cache::ensure(this);
  1883. return std::make_pair(TypeBits.getLinkage(), TypeBits.getVisibility());
  1884. }
  1885. void Type::ClearLinkageCache() {
  1886. TypeBits.CacheValidAndVisibility = 0;
  1887. if (QualType(this, 0) != CanonicalType)
  1888. CanonicalType->TypeBits.CacheValidAndVisibility = 0;
  1889. }
  1890. Qualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const {
  1891. if (isObjCARCImplicitlyUnretainedType())
  1892. return Qualifiers::OCL_ExplicitNone;
  1893. return Qualifiers::OCL_Strong;
  1894. }
  1895. bool Type::isObjCARCImplicitlyUnretainedType() const {
  1896. assert(isObjCLifetimeType() &&
  1897. "cannot query implicit lifetime for non-inferrable type");
  1898. const Type *canon = getCanonicalTypeInternal().getTypePtr();
  1899. // Walk down to the base type. We don't care about qualifiers for this.
  1900. while (const ArrayType *array = dyn_cast<ArrayType>(canon))
  1901. canon = array->getElementType().getTypePtr();
  1902. if (const ObjCObjectPointerType *opt
  1903. = dyn_cast<ObjCObjectPointerType>(canon)) {
  1904. // Class and Class<Protocol> don't require retension.
  1905. if (opt->getObjectType()->isObjCClass())
  1906. return true;
  1907. }
  1908. return false;
  1909. }
  1910. bool Type::isObjCNSObjectType() const {
  1911. if (const TypedefType *typedefType = dyn_cast<TypedefType>(this))
  1912. return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>();
  1913. return false;
  1914. }
  1915. bool Type::isObjCRetainableType() const {
  1916. return isObjCObjectPointerType() ||
  1917. isBlockPointerType() ||
  1918. isObjCNSObjectType();
  1919. }
  1920. bool Type::isObjCIndirectLifetimeType() const {
  1921. if (isObjCLifetimeType())
  1922. return true;
  1923. if (const PointerType *OPT = getAs<PointerType>())
  1924. return OPT->getPointeeType()->isObjCIndirectLifetimeType();
  1925. if (const ReferenceType *Ref = getAs<ReferenceType>())
  1926. return Ref->getPointeeType()->isObjCIndirectLifetimeType();
  1927. if (const MemberPointerType *MemPtr = getAs<MemberPointerType>())
  1928. return MemPtr->getPointeeType()->isObjCIndirectLifetimeType();
  1929. return false;
  1930. }
  1931. /// Returns true if objects of this type have lifetime semantics under
  1932. /// ARC.
  1933. bool Type::isObjCLifetimeType() const {
  1934. const Type *type = this;
  1935. while (const ArrayType *array = type->getAsArrayTypeUnsafe())
  1936. type = array->getElementType().getTypePtr();
  1937. return type->isObjCRetainableType();
  1938. }
  1939. /// \brief Determine whether the given type T is a "bridgable" Objective-C type,
  1940. /// which is either an Objective-C object pointer type or an
  1941. bool Type::isObjCARCBridgableType() const {
  1942. return isObjCObjectPointerType() || isBlockPointerType();
  1943. }
  1944. /// \brief Determine whether the given type T is a "bridgeable" C type.
  1945. bool Type::isCARCBridgableType() const {
  1946. const PointerType *Pointer = getAs<PointerType>();
  1947. if (!Pointer)
  1948. return false;
  1949. QualType Pointee = Pointer->getPointeeType();
  1950. return Pointee->isVoidType() || Pointee->isRecordType();
  1951. }
  1952. bool Type::hasSizedVLAType() const {
  1953. if (!isVariablyModifiedType()) return false;
  1954. if (const PointerType *ptr = getAs<PointerType>())
  1955. return ptr->getPointeeType()->hasSizedVLAType();
  1956. if (const ReferenceType *ref = getAs<ReferenceType>())
  1957. return ref->getPointeeType()->hasSizedVLAType();
  1958. if (const ArrayType *arr = getAsArrayTypeUnsafe()) {
  1959. if (isa<VariableArrayType>(arr) &&
  1960. cast<VariableArrayType>(arr)->getSizeExpr())
  1961. return true;
  1962. return arr->getElementType()->hasSizedVLAType();
  1963. }
  1964. return false;
  1965. }
  1966. QualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) {
  1967. switch (type.getObjCLifetime()) {
  1968. case Qualifiers::OCL_None:
  1969. case Qualifiers::OCL_ExplicitNone:
  1970. case Qualifiers::OCL_Autoreleasing:
  1971. break;
  1972. case Qualifiers::OCL_Strong:
  1973. return DK_objc_strong_lifetime;
  1974. case Qualifiers::OCL_Weak:
  1975. return DK_objc_weak_lifetime;
  1976. }
  1977. /// Currently, the only destruction kind we recognize is C++ objects
  1978. /// with non-trivial destructors.
  1979. const CXXRecordDecl *record =
  1980. type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
  1981. if (record && record->hasDefinition() && !record->hasTrivialDestructor())
  1982. return DK_cxx_destructor;
  1983. return DK_none;
  1984. }
  1985. bool QualType::hasTrivialAssignment(ASTContext &Context, bool Copying) const {
  1986. switch (getObjCLifetime()) {
  1987. case Qualifiers::OCL_None:
  1988. break;
  1989. case Qualifiers::OCL_ExplicitNone:
  1990. return true;
  1991. case Qualifiers::OCL_Autoreleasing:
  1992. case Qualifiers::OCL_Strong:
  1993. case Qualifiers::OCL_Weak:
  1994. return !Context.getLangOptions().ObjCAutoRefCount;
  1995. }
  1996. if (const CXXRecordDecl *Record
  1997. = getTypePtr()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
  1998. return Copying ? Record->hasTrivialCopyAssignment() :
  1999. Record->hasTrivialMoveAssignment();
  2000. return true;
  2001. }
  2002. const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
  2003. QualType T) {
  2004. DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
  2005. DiagnosticsEngine::ak_qualtype);
  2006. return DB;
  2007. }
  2008. const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &PD,
  2009. QualType T) {
  2010. PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
  2011. DiagnosticsEngine::ak_qualtype);
  2012. return PD;
  2013. }