ExprCXX.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. //===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the subclesses of Expr class declared in ExprCXX.h
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/ExprCXX.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Attr.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclAccessPair.h"
  17. #include "clang/AST/DeclBase.h"
  18. #include "clang/AST/DeclCXX.h"
  19. #include "clang/AST/DeclarationName.h"
  20. #include "clang/AST/Expr.h"
  21. #include "clang/AST/LambdaCapture.h"
  22. #include "clang/AST/NestedNameSpecifier.h"
  23. #include "clang/AST/TemplateBase.h"
  24. #include "clang/AST/Type.h"
  25. #include "clang/AST/TypeLoc.h"
  26. #include "clang/Basic/LLVM.h"
  27. #include "clang/Basic/OperatorKinds.h"
  28. #include "clang/Basic/SourceLocation.h"
  29. #include "clang/Basic/Specifiers.h"
  30. #include "llvm/ADT/ArrayRef.h"
  31. #include "llvm/Support/Casting.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include <cassert>
  34. #include <cstddef>
  35. #include <cstring>
  36. #include <memory>
  37. using namespace clang;
  38. //===----------------------------------------------------------------------===//
  39. // Child Iterators for iterating over subexpressions/substatements
  40. //===----------------------------------------------------------------------===//
  41. bool CXXOperatorCallExpr::isInfixBinaryOp() const {
  42. // An infix binary operator is any operator with two arguments other than
  43. // operator() and operator[]. Note that none of these operators can have
  44. // default arguments, so it suffices to check the number of argument
  45. // expressions.
  46. if (getNumArgs() != 2)
  47. return false;
  48. switch (getOperator()) {
  49. case OO_Call: case OO_Subscript:
  50. return false;
  51. default:
  52. return true;
  53. }
  54. }
  55. CXXRewrittenBinaryOperator::DecomposedForm
  56. CXXRewrittenBinaryOperator::getDecomposedForm() const {
  57. DecomposedForm Result = {};
  58. const Expr *E = getSemanticForm()->IgnoreImplicit();
  59. // Remove an outer '!' if it exists (only happens for a '!=' rewrite).
  60. bool SkippedNot = false;
  61. if (auto *NotEq = dyn_cast<UnaryOperator>(E)) {
  62. assert(NotEq->getOpcode() == UO_LNot);
  63. E = NotEq->getSubExpr()->IgnoreImplicit();
  64. SkippedNot = true;
  65. }
  66. // Decompose the outer binary operator.
  67. if (auto *BO = dyn_cast<BinaryOperator>(E)) {
  68. assert(!SkippedNot || BO->getOpcode() == BO_EQ);
  69. Result.Opcode = SkippedNot ? BO_NE : BO->getOpcode();
  70. Result.LHS = BO->getLHS();
  71. Result.RHS = BO->getRHS();
  72. Result.InnerBinOp = BO;
  73. } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
  74. assert(!SkippedNot || BO->getOperator() == OO_EqualEqual);
  75. assert(BO->isInfixBinaryOp());
  76. switch (BO->getOperator()) {
  77. case OO_Less: Result.Opcode = BO_LT; break;
  78. case OO_LessEqual: Result.Opcode = BO_LE; break;
  79. case OO_Greater: Result.Opcode = BO_GT; break;
  80. case OO_GreaterEqual: Result.Opcode = BO_GE; break;
  81. case OO_Spaceship: Result.Opcode = BO_Cmp; break;
  82. case OO_EqualEqual: Result.Opcode = SkippedNot ? BO_NE : BO_EQ; break;
  83. default: llvm_unreachable("unexpected binop in rewritten operator expr");
  84. }
  85. Result.LHS = BO->getArg(0);
  86. Result.RHS = BO->getArg(1);
  87. Result.InnerBinOp = BO;
  88. } else {
  89. llvm_unreachable("unexpected rewritten operator form");
  90. }
  91. // Put the operands in the right order for == and !=, and canonicalize the
  92. // <=> subexpression onto the LHS for all other forms.
  93. if (isReversed())
  94. std::swap(Result.LHS, Result.RHS);
  95. // If this isn't a spaceship rewrite, we're done.
  96. if (Result.Opcode == BO_EQ || Result.Opcode == BO_NE)
  97. return Result;
  98. // Otherwise, we expect a <=> to now be on the LHS.
  99. E = Result.LHS->IgnoreImplicit();
  100. if (auto *BO = dyn_cast<BinaryOperator>(E)) {
  101. assert(BO->getOpcode() == BO_Cmp);
  102. Result.LHS = BO->getLHS();
  103. Result.RHS = BO->getRHS();
  104. Result.InnerBinOp = BO;
  105. } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
  106. assert(BO->getOperator() == OO_Spaceship);
  107. Result.LHS = BO->getArg(0);
  108. Result.RHS = BO->getArg(1);
  109. Result.InnerBinOp = BO;
  110. } else {
  111. llvm_unreachable("unexpected rewritten operator form");
  112. }
  113. // Put the comparison operands in the right order.
  114. if (isReversed())
  115. std::swap(Result.LHS, Result.RHS);
  116. return Result;
  117. }
  118. bool CXXTypeidExpr::isPotentiallyEvaluated() const {
  119. if (isTypeOperand())
  120. return false;
  121. // C++11 [expr.typeid]p3:
  122. // When typeid is applied to an expression other than a glvalue of
  123. // polymorphic class type, [...] the expression is an unevaluated operand.
  124. const Expr *E = getExprOperand();
  125. if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
  126. if (RD->isPolymorphic() && E->isGLValue())
  127. return true;
  128. return false;
  129. }
  130. QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
  131. assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
  132. Qualifiers Quals;
  133. return Context.getUnqualifiedArrayType(
  134. Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
  135. }
  136. QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
  137. assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
  138. Qualifiers Quals;
  139. return Context.getUnqualifiedArrayType(
  140. Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
  141. }
  142. // CXXScalarValueInitExpr
  143. SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
  144. return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc();
  145. }
  146. // CXXNewExpr
  147. CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
  148. FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
  149. bool UsualArrayDeleteWantsSize,
  150. ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
  151. Optional<Expr *> ArraySize,
  152. InitializationStyle InitializationStyle,
  153. Expr *Initializer, QualType Ty,
  154. TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
  155. SourceRange DirectInitRange)
  156. : Expr(CXXNewExprClass, Ty, VK_RValue, OK_Ordinary, Ty->isDependentType(),
  157. Ty->isDependentType(), Ty->isInstantiationDependentType(),
  158. Ty->containsUnexpandedParameterPack()),
  159. OperatorNew(OperatorNew), OperatorDelete(OperatorDelete),
  160. AllocatedTypeInfo(AllocatedTypeInfo), Range(Range),
  161. DirectInitRange(DirectInitRange) {
  162. assert((Initializer != nullptr || InitializationStyle == NoInit) &&
  163. "Only NoInit can have no initializer!");
  164. CXXNewExprBits.IsGlobalNew = IsGlobalNew;
  165. CXXNewExprBits.IsArray = ArraySize.hasValue();
  166. CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment;
  167. CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
  168. CXXNewExprBits.StoredInitializationStyle =
  169. Initializer ? InitializationStyle + 1 : 0;
  170. bool IsParenTypeId = TypeIdParens.isValid();
  171. CXXNewExprBits.IsParenTypeId = IsParenTypeId;
  172. CXXNewExprBits.NumPlacementArgs = PlacementArgs.size();
  173. if (ArraySize) {
  174. if (Expr *SizeExpr = *ArraySize) {
  175. if (SizeExpr->isValueDependent())
  176. ExprBits.ValueDependent = true;
  177. if (SizeExpr->isInstantiationDependent())
  178. ExprBits.InstantiationDependent = true;
  179. if (SizeExpr->containsUnexpandedParameterPack())
  180. ExprBits.ContainsUnexpandedParameterPack = true;
  181. }
  182. getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize;
  183. }
  184. if (Initializer) {
  185. if (Initializer->isValueDependent())
  186. ExprBits.ValueDependent = true;
  187. if (Initializer->isInstantiationDependent())
  188. ExprBits.InstantiationDependent = true;
  189. if (Initializer->containsUnexpandedParameterPack())
  190. ExprBits.ContainsUnexpandedParameterPack = true;
  191. getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer;
  192. }
  193. for (unsigned I = 0; I != PlacementArgs.size(); ++I) {
  194. if (PlacementArgs[I]->isValueDependent())
  195. ExprBits.ValueDependent = true;
  196. if (PlacementArgs[I]->isInstantiationDependent())
  197. ExprBits.InstantiationDependent = true;
  198. if (PlacementArgs[I]->containsUnexpandedParameterPack())
  199. ExprBits.ContainsUnexpandedParameterPack = true;
  200. getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] =
  201. PlacementArgs[I];
  202. }
  203. if (IsParenTypeId)
  204. getTrailingObjects<SourceRange>()[0] = TypeIdParens;
  205. switch (getInitializationStyle()) {
  206. case CallInit:
  207. this->Range.setEnd(DirectInitRange.getEnd());
  208. break;
  209. case ListInit:
  210. this->Range.setEnd(getInitializer()->getSourceRange().getEnd());
  211. break;
  212. default:
  213. if (IsParenTypeId)
  214. this->Range.setEnd(TypeIdParens.getEnd());
  215. break;
  216. }
  217. }
  218. CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray,
  219. unsigned NumPlacementArgs, bool IsParenTypeId)
  220. : Expr(CXXNewExprClass, Empty) {
  221. CXXNewExprBits.IsArray = IsArray;
  222. CXXNewExprBits.NumPlacementArgs = NumPlacementArgs;
  223. CXXNewExprBits.IsParenTypeId = IsParenTypeId;
  224. }
  225. CXXNewExpr *
  226. CXXNewExpr::Create(const ASTContext &Ctx, bool IsGlobalNew,
  227. FunctionDecl *OperatorNew, FunctionDecl *OperatorDelete,
  228. bool ShouldPassAlignment, bool UsualArrayDeleteWantsSize,
  229. ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
  230. Optional<Expr *> ArraySize,
  231. InitializationStyle InitializationStyle, Expr *Initializer,
  232. QualType Ty, TypeSourceInfo *AllocatedTypeInfo,
  233. SourceRange Range, SourceRange DirectInitRange) {
  234. bool IsArray = ArraySize.hasValue();
  235. bool HasInit = Initializer != nullptr;
  236. unsigned NumPlacementArgs = PlacementArgs.size();
  237. bool IsParenTypeId = TypeIdParens.isValid();
  238. void *Mem =
  239. Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
  240. IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
  241. alignof(CXXNewExpr));
  242. return new (Mem)
  243. CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment,
  244. UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
  245. ArraySize, InitializationStyle, Initializer, Ty,
  246. AllocatedTypeInfo, Range, DirectInitRange);
  247. }
  248. CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray,
  249. bool HasInit, unsigned NumPlacementArgs,
  250. bool IsParenTypeId) {
  251. void *Mem =
  252. Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
  253. IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
  254. alignof(CXXNewExpr));
  255. return new (Mem)
  256. CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId);
  257. }
  258. bool CXXNewExpr::shouldNullCheckAllocation() const {
  259. return getOperatorNew()
  260. ->getType()
  261. ->castAs<FunctionProtoType>()
  262. ->isNothrow() &&
  263. !getOperatorNew()->isReservedGlobalPlacementOperator();
  264. }
  265. // CXXDeleteExpr
  266. QualType CXXDeleteExpr::getDestroyedType() const {
  267. const Expr *Arg = getArgument();
  268. // For a destroying operator delete, we may have implicitly converted the
  269. // pointer type to the type of the parameter of the 'operator delete'
  270. // function.
  271. while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
  272. if (ICE->getCastKind() == CK_DerivedToBase ||
  273. ICE->getCastKind() == CK_UncheckedDerivedToBase ||
  274. ICE->getCastKind() == CK_NoOp) {
  275. assert((ICE->getCastKind() == CK_NoOp ||
  276. getOperatorDelete()->isDestroyingOperatorDelete()) &&
  277. "only a destroying operator delete can have a converted arg");
  278. Arg = ICE->getSubExpr();
  279. } else
  280. break;
  281. }
  282. // The type-to-delete may not be a pointer if it's a dependent type.
  283. const QualType ArgType = Arg->getType();
  284. if (ArgType->isDependentType() && !ArgType->isPointerType())
  285. return QualType();
  286. return ArgType->castAs<PointerType>()->getPointeeType();
  287. }
  288. // CXXPseudoDestructorExpr
  289. PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
  290. : Type(Info) {
  291. Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
  292. }
  293. CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
  294. Expr *Base, bool isArrow, SourceLocation OperatorLoc,
  295. NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
  296. SourceLocation ColonColonLoc, SourceLocation TildeLoc,
  297. PseudoDestructorTypeStorage DestroyedType)
  298. : Expr(CXXPseudoDestructorExprClass,
  299. Context.BoundMemberTy,
  300. VK_RValue, OK_Ordinary,
  301. /*isTypeDependent=*/(Base->isTypeDependent() ||
  302. (DestroyedType.getTypeSourceInfo() &&
  303. DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
  304. /*isValueDependent=*/Base->isValueDependent(),
  305. (Base->isInstantiationDependent() ||
  306. (QualifierLoc &&
  307. QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
  308. (ScopeType &&
  309. ScopeType->getType()->isInstantiationDependentType()) ||
  310. (DestroyedType.getTypeSourceInfo() &&
  311. DestroyedType.getTypeSourceInfo()->getType()
  312. ->isInstantiationDependentType())),
  313. // ContainsUnexpandedParameterPack
  314. (Base->containsUnexpandedParameterPack() ||
  315. (QualifierLoc &&
  316. QualifierLoc.getNestedNameSpecifier()
  317. ->containsUnexpandedParameterPack()) ||
  318. (ScopeType &&
  319. ScopeType->getType()->containsUnexpandedParameterPack()) ||
  320. (DestroyedType.getTypeSourceInfo() &&
  321. DestroyedType.getTypeSourceInfo()->getType()
  322. ->containsUnexpandedParameterPack()))),
  323. Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
  324. OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
  325. ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
  326. DestroyedType(DestroyedType) {}
  327. QualType CXXPseudoDestructorExpr::getDestroyedType() const {
  328. if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
  329. return TInfo->getType();
  330. return QualType();
  331. }
  332. SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
  333. SourceLocation End = DestroyedType.getLocation();
  334. if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
  335. End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
  336. return End;
  337. }
  338. // UnresolvedLookupExpr
  339. UnresolvedLookupExpr::UnresolvedLookupExpr(
  340. const ASTContext &Context, CXXRecordDecl *NamingClass,
  341. NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
  342. const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
  343. const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
  344. UnresolvedSetIterator End)
  345. : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc,
  346. TemplateKWLoc, NameInfo, TemplateArgs, Begin, End, false,
  347. false, false),
  348. NamingClass(NamingClass) {
  349. UnresolvedLookupExprBits.RequiresADL = RequiresADL;
  350. UnresolvedLookupExprBits.Overloaded = Overloaded;
  351. }
  352. UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty,
  353. unsigned NumResults,
  354. bool HasTemplateKWAndArgsInfo)
  355. : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults,
  356. HasTemplateKWAndArgsInfo) {}
  357. UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
  358. const ASTContext &Context, CXXRecordDecl *NamingClass,
  359. NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
  360. bool RequiresADL, bool Overloaded, UnresolvedSetIterator Begin,
  361. UnresolvedSetIterator End) {
  362. unsigned NumResults = End - Begin;
  363. unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
  364. TemplateArgumentLoc>(NumResults, 0, 0);
  365. void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
  366. return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
  367. SourceLocation(), NameInfo, RequiresADL,
  368. Overloaded, nullptr, Begin, End);
  369. }
  370. UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
  371. const ASTContext &Context, CXXRecordDecl *NamingClass,
  372. NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
  373. const DeclarationNameInfo &NameInfo, bool RequiresADL,
  374. const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
  375. UnresolvedSetIterator End) {
  376. assert(Args || TemplateKWLoc.isValid());
  377. unsigned NumResults = End - Begin;
  378. unsigned NumTemplateArgs = Args ? Args->size() : 0;
  379. unsigned Size =
  380. totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
  381. TemplateArgumentLoc>(NumResults, 1, NumTemplateArgs);
  382. void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
  383. return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
  384. TemplateKWLoc, NameInfo, RequiresADL,
  385. /*Overloaded*/ true, Args, Begin, End);
  386. }
  387. UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty(
  388. const ASTContext &Context, unsigned NumResults,
  389. bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
  390. assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
  391. unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
  392. TemplateArgumentLoc>(
  393. NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
  394. void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
  395. return new (Mem)
  396. UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
  397. }
  398. OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context,
  399. NestedNameSpecifierLoc QualifierLoc,
  400. SourceLocation TemplateKWLoc,
  401. const DeclarationNameInfo &NameInfo,
  402. const TemplateArgumentListInfo *TemplateArgs,
  403. UnresolvedSetIterator Begin,
  404. UnresolvedSetIterator End, bool KnownDependent,
  405. bool KnownInstantiationDependent,
  406. bool KnownContainsUnexpandedParameterPack)
  407. : Expr(
  408. SC, Context.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
  409. KnownDependent,
  410. (KnownInstantiationDependent || NameInfo.isInstantiationDependent() ||
  411. (QualifierLoc &&
  412. QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
  413. (KnownContainsUnexpandedParameterPack ||
  414. NameInfo.containsUnexpandedParameterPack() ||
  415. (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
  416. ->containsUnexpandedParameterPack()))),
  417. NameInfo(NameInfo), QualifierLoc(QualifierLoc) {
  418. unsigned NumResults = End - Begin;
  419. OverloadExprBits.NumResults = NumResults;
  420. OverloadExprBits.HasTemplateKWAndArgsInfo =
  421. (TemplateArgs != nullptr ) || TemplateKWLoc.isValid();
  422. if (NumResults) {
  423. // Determine whether this expression is type-dependent.
  424. for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
  425. if ((*I)->getDeclContext()->isDependentContext() ||
  426. isa<UnresolvedUsingValueDecl>(*I)) {
  427. ExprBits.TypeDependent = true;
  428. ExprBits.ValueDependent = true;
  429. ExprBits.InstantiationDependent = true;
  430. }
  431. }
  432. // Copy the results to the trailing array past UnresolvedLookupExpr
  433. // or UnresolvedMemberExpr.
  434. DeclAccessPair *Results = getTrailingResults();
  435. memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
  436. }
  437. // If we have explicit template arguments, check for dependent
  438. // template arguments and whether they contain any unexpanded pack
  439. // expansions.
  440. if (TemplateArgs) {
  441. bool Dependent = false;
  442. bool InstantiationDependent = false;
  443. bool ContainsUnexpandedParameterPack = false;
  444. getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
  445. TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
  446. Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
  447. if (Dependent) {
  448. ExprBits.TypeDependent = true;
  449. ExprBits.ValueDependent = true;
  450. }
  451. if (InstantiationDependent)
  452. ExprBits.InstantiationDependent = true;
  453. if (ContainsUnexpandedParameterPack)
  454. ExprBits.ContainsUnexpandedParameterPack = true;
  455. } else if (TemplateKWLoc.isValid()) {
  456. getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
  457. }
  458. if (isTypeDependent())
  459. setType(Context.DependentTy);
  460. }
  461. OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
  462. bool HasTemplateKWAndArgsInfo)
  463. : Expr(SC, Empty) {
  464. OverloadExprBits.NumResults = NumResults;
  465. OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
  466. }
  467. // DependentScopeDeclRefExpr
  468. DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(
  469. QualType Ty, NestedNameSpecifierLoc QualifierLoc,
  470. SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
  471. const TemplateArgumentListInfo *Args)
  472. : Expr(
  473. DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary, true,
  474. true,
  475. (NameInfo.isInstantiationDependent() ||
  476. (QualifierLoc &&
  477. QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
  478. (NameInfo.containsUnexpandedParameterPack() ||
  479. (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
  480. ->containsUnexpandedParameterPack()))),
  481. QualifierLoc(QualifierLoc), NameInfo(NameInfo) {
  482. DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
  483. (Args != nullptr) || TemplateKWLoc.isValid();
  484. if (Args) {
  485. bool Dependent = true;
  486. bool InstantiationDependent = true;
  487. bool ContainsUnexpandedParameterPack
  488. = ExprBits.ContainsUnexpandedParameterPack;
  489. getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
  490. TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
  491. Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
  492. ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
  493. } else if (TemplateKWLoc.isValid()) {
  494. getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
  495. TemplateKWLoc);
  496. }
  497. }
  498. DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create(
  499. const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
  500. SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
  501. const TemplateArgumentListInfo *Args) {
  502. assert(QualifierLoc && "should be created for dependent qualifiers");
  503. bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
  504. std::size_t Size =
  505. totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
  506. HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
  507. void *Mem = Context.Allocate(Size);
  508. return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc,
  509. TemplateKWLoc, NameInfo, Args);
  510. }
  511. DependentScopeDeclRefExpr *
  512. DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context,
  513. bool HasTemplateKWAndArgsInfo,
  514. unsigned NumTemplateArgs) {
  515. assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
  516. std::size_t Size =
  517. totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
  518. HasTemplateKWAndArgsInfo, NumTemplateArgs);
  519. void *Mem = Context.Allocate(Size);
  520. auto *E = new (Mem) DependentScopeDeclRefExpr(
  521. QualType(), NestedNameSpecifierLoc(), SourceLocation(),
  522. DeclarationNameInfo(), nullptr);
  523. E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
  524. HasTemplateKWAndArgsInfo;
  525. return E;
  526. }
  527. SourceLocation CXXConstructExpr::getBeginLoc() const {
  528. if (isa<CXXTemporaryObjectExpr>(this))
  529. return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc();
  530. return getLocation();
  531. }
  532. SourceLocation CXXConstructExpr::getEndLoc() const {
  533. if (isa<CXXTemporaryObjectExpr>(this))
  534. return cast<CXXTemporaryObjectExpr>(this)->getEndLoc();
  535. if (ParenOrBraceRange.isValid())
  536. return ParenOrBraceRange.getEnd();
  537. SourceLocation End = getLocation();
  538. for (unsigned I = getNumArgs(); I > 0; --I) {
  539. const Expr *Arg = getArg(I-1);
  540. if (!Arg->isDefaultArgument()) {
  541. SourceLocation NewEnd = Arg->getEndLoc();
  542. if (NewEnd.isValid()) {
  543. End = NewEnd;
  544. break;
  545. }
  546. }
  547. }
  548. return End;
  549. }
  550. CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind,
  551. Expr *Fn, ArrayRef<Expr *> Args,
  552. QualType Ty, ExprValueKind VK,
  553. SourceLocation OperatorLoc,
  554. FPOptions FPFeatures,
  555. ADLCallKind UsesADL)
  556. : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
  557. OperatorLoc, /*MinNumArgs=*/0, UsesADL) {
  558. CXXOperatorCallExprBits.OperatorKind = OpKind;
  559. CXXOperatorCallExprBits.FPFeatures = FPFeatures.getInt();
  560. assert(
  561. (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) &&
  562. "OperatorKind overflow!");
  563. assert((CXXOperatorCallExprBits.FPFeatures == FPFeatures.getInt()) &&
  564. "FPFeatures overflow!");
  565. Range = getSourceRangeImpl();
  566. }
  567. CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, EmptyShell Empty)
  568. : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
  569. CXXOperatorCallExpr *CXXOperatorCallExpr::Create(
  570. const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn,
  571. ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
  572. SourceLocation OperatorLoc, FPOptions FPFeatures, ADLCallKind UsesADL) {
  573. // Allocate storage for the trailing objects of CallExpr.
  574. unsigned NumArgs = Args.size();
  575. unsigned SizeOfTrailingObjects =
  576. CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
  577. void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
  578. alignof(CXXOperatorCallExpr));
  579. return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
  580. FPFeatures, UsesADL);
  581. }
  582. CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
  583. unsigned NumArgs,
  584. EmptyShell Empty) {
  585. // Allocate storage for the trailing objects of CallExpr.
  586. unsigned SizeOfTrailingObjects =
  587. CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
  588. void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
  589. alignof(CXXOperatorCallExpr));
  590. return new (Mem) CXXOperatorCallExpr(NumArgs, Empty);
  591. }
  592. SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
  593. OverloadedOperatorKind Kind = getOperator();
  594. if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
  595. if (getNumArgs() == 1)
  596. // Prefix operator
  597. return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
  598. else
  599. // Postfix operator
  600. return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
  601. } else if (Kind == OO_Arrow) {
  602. return getArg(0)->getSourceRange();
  603. } else if (Kind == OO_Call) {
  604. return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
  605. } else if (Kind == OO_Subscript) {
  606. return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
  607. } else if (getNumArgs() == 1) {
  608. return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
  609. } else if (getNumArgs() == 2) {
  610. return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
  611. } else {
  612. return getOperatorLoc();
  613. }
  614. }
  615. CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args,
  616. QualType Ty, ExprValueKind VK,
  617. SourceLocation RP, unsigned MinNumArgs)
  618. : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP,
  619. MinNumArgs, NotADL) {}
  620. CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, EmptyShell Empty)
  621. : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
  622. CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
  623. ArrayRef<Expr *> Args, QualType Ty,
  624. ExprValueKind VK,
  625. SourceLocation RP,
  626. unsigned MinNumArgs) {
  627. // Allocate storage for the trailing objects of CallExpr.
  628. unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
  629. unsigned SizeOfTrailingObjects =
  630. CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
  631. void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
  632. alignof(CXXMemberCallExpr));
  633. return new (Mem) CXXMemberCallExpr(Fn, Args, Ty, VK, RP, MinNumArgs);
  634. }
  635. CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
  636. unsigned NumArgs,
  637. EmptyShell Empty) {
  638. // Allocate storage for the trailing objects of CallExpr.
  639. unsigned SizeOfTrailingObjects =
  640. CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
  641. void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
  642. alignof(CXXMemberCallExpr));
  643. return new (Mem) CXXMemberCallExpr(NumArgs, Empty);
  644. }
  645. Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
  646. const Expr *Callee = getCallee()->IgnoreParens();
  647. if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
  648. return MemExpr->getBase();
  649. if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
  650. if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
  651. return BO->getLHS();
  652. // FIXME: Will eventually need to cope with member pointers.
  653. return nullptr;
  654. }
  655. QualType CXXMemberCallExpr::getObjectType() const {
  656. QualType Ty = getImplicitObjectArgument()->getType();
  657. if (Ty->isPointerType())
  658. Ty = Ty->getPointeeType();
  659. return Ty;
  660. }
  661. CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
  662. if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
  663. return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
  664. // FIXME: Will eventually need to cope with member pointers.
  665. return nullptr;
  666. }
  667. CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
  668. Expr* ThisArg = getImplicitObjectArgument();
  669. if (!ThisArg)
  670. return nullptr;
  671. if (ThisArg->getType()->isAnyPointerType())
  672. return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
  673. return ThisArg->getType()->getAsCXXRecordDecl();
  674. }
  675. //===----------------------------------------------------------------------===//
  676. // Named casts
  677. //===----------------------------------------------------------------------===//
  678. /// getCastName - Get the name of the C++ cast being used, e.g.,
  679. /// "static_cast", "dynamic_cast", "reinterpret_cast", or
  680. /// "const_cast". The returned pointer must not be freed.
  681. const char *CXXNamedCastExpr::getCastName() const {
  682. switch (getStmtClass()) {
  683. case CXXStaticCastExprClass: return "static_cast";
  684. case CXXDynamicCastExprClass: return "dynamic_cast";
  685. case CXXReinterpretCastExprClass: return "reinterpret_cast";
  686. case CXXConstCastExprClass: return "const_cast";
  687. default: return "<invalid cast>";
  688. }
  689. }
  690. CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
  691. ExprValueKind VK,
  692. CastKind K, Expr *Op,
  693. const CXXCastPath *BasePath,
  694. TypeSourceInfo *WrittenTy,
  695. SourceLocation L,
  696. SourceLocation RParenLoc,
  697. SourceRange AngleBrackets) {
  698. unsigned PathSize = (BasePath ? BasePath->size() : 0);
  699. void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
  700. auto *E =
  701. new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
  702. RParenLoc, AngleBrackets);
  703. if (PathSize)
  704. std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
  705. E->getTrailingObjects<CXXBaseSpecifier *>());
  706. return E;
  707. }
  708. CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
  709. unsigned PathSize) {
  710. void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
  711. return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
  712. }
  713. CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
  714. ExprValueKind VK,
  715. CastKind K, Expr *Op,
  716. const CXXCastPath *BasePath,
  717. TypeSourceInfo *WrittenTy,
  718. SourceLocation L,
  719. SourceLocation RParenLoc,
  720. SourceRange AngleBrackets) {
  721. unsigned PathSize = (BasePath ? BasePath->size() : 0);
  722. void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
  723. auto *E =
  724. new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
  725. RParenLoc, AngleBrackets);
  726. if (PathSize)
  727. std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
  728. E->getTrailingObjects<CXXBaseSpecifier *>());
  729. return E;
  730. }
  731. CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
  732. unsigned PathSize) {
  733. void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
  734. return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
  735. }
  736. /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
  737. /// to always be null. For example:
  738. ///
  739. /// struct A { };
  740. /// struct B final : A { };
  741. /// struct C { };
  742. ///
  743. /// C *f(B* b) { return dynamic_cast<C*>(b); }
  744. bool CXXDynamicCastExpr::isAlwaysNull() const
  745. {
  746. QualType SrcType = getSubExpr()->getType();
  747. QualType DestType = getType();
  748. if (const auto *SrcPTy = SrcType->getAs<PointerType>()) {
  749. SrcType = SrcPTy->getPointeeType();
  750. DestType = DestType->castAs<PointerType>()->getPointeeType();
  751. }
  752. if (DestType->isVoidType())
  753. return false;
  754. const auto *SrcRD =
  755. cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
  756. if (!SrcRD->hasAttr<FinalAttr>())
  757. return false;
  758. const auto *DestRD =
  759. cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
  760. return !DestRD->isDerivedFrom(SrcRD);
  761. }
  762. CXXReinterpretCastExpr *
  763. CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
  764. ExprValueKind VK, CastKind K, Expr *Op,
  765. const CXXCastPath *BasePath,
  766. TypeSourceInfo *WrittenTy, SourceLocation L,
  767. SourceLocation RParenLoc,
  768. SourceRange AngleBrackets) {
  769. unsigned PathSize = (BasePath ? BasePath->size() : 0);
  770. void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
  771. auto *E =
  772. new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
  773. RParenLoc, AngleBrackets);
  774. if (PathSize)
  775. std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
  776. E->getTrailingObjects<CXXBaseSpecifier *>());
  777. return E;
  778. }
  779. CXXReinterpretCastExpr *
  780. CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
  781. void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
  782. return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
  783. }
  784. CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
  785. ExprValueKind VK, Expr *Op,
  786. TypeSourceInfo *WrittenTy,
  787. SourceLocation L,
  788. SourceLocation RParenLoc,
  789. SourceRange AngleBrackets) {
  790. return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
  791. }
  792. CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
  793. return new (C) CXXConstCastExpr(EmptyShell());
  794. }
  795. CXXFunctionalCastExpr *
  796. CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
  797. TypeSourceInfo *Written, CastKind K, Expr *Op,
  798. const CXXCastPath *BasePath,
  799. SourceLocation L, SourceLocation R) {
  800. unsigned PathSize = (BasePath ? BasePath->size() : 0);
  801. void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
  802. auto *E =
  803. new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
  804. if (PathSize)
  805. std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
  806. E->getTrailingObjects<CXXBaseSpecifier *>());
  807. return E;
  808. }
  809. CXXFunctionalCastExpr *
  810. CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
  811. void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
  812. return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
  813. }
  814. SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
  815. return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
  816. }
  817. SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
  818. return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
  819. }
  820. UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args,
  821. QualType Ty, ExprValueKind VK,
  822. SourceLocation LitEndLoc,
  823. SourceLocation SuffixLoc)
  824. : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
  825. LitEndLoc, /*MinNumArgs=*/0, NotADL),
  826. UDSuffixLoc(SuffixLoc) {}
  827. UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, EmptyShell Empty)
  828. : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
  829. UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
  830. ArrayRef<Expr *> Args,
  831. QualType Ty, ExprValueKind VK,
  832. SourceLocation LitEndLoc,
  833. SourceLocation SuffixLoc) {
  834. // Allocate storage for the trailing objects of CallExpr.
  835. unsigned NumArgs = Args.size();
  836. unsigned SizeOfTrailingObjects =
  837. CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
  838. void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
  839. alignof(UserDefinedLiteral));
  840. return new (Mem) UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc);
  841. }
  842. UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
  843. unsigned NumArgs,
  844. EmptyShell Empty) {
  845. // Allocate storage for the trailing objects of CallExpr.
  846. unsigned SizeOfTrailingObjects =
  847. CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
  848. void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
  849. alignof(UserDefinedLiteral));
  850. return new (Mem) UserDefinedLiteral(NumArgs, Empty);
  851. }
  852. UserDefinedLiteral::LiteralOperatorKind
  853. UserDefinedLiteral::getLiteralOperatorKind() const {
  854. if (getNumArgs() == 0)
  855. return LOK_Template;
  856. if (getNumArgs() == 2)
  857. return LOK_String;
  858. assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
  859. QualType ParamTy =
  860. cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
  861. if (ParamTy->isPointerType())
  862. return LOK_Raw;
  863. if (ParamTy->isAnyCharacterType())
  864. return LOK_Character;
  865. if (ParamTy->isIntegerType())
  866. return LOK_Integer;
  867. if (ParamTy->isFloatingType())
  868. return LOK_Floating;
  869. llvm_unreachable("unknown kind of literal operator");
  870. }
  871. Expr *UserDefinedLiteral::getCookedLiteral() {
  872. #ifndef NDEBUG
  873. LiteralOperatorKind LOK = getLiteralOperatorKind();
  874. assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
  875. #endif
  876. return getArg(0);
  877. }
  878. const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
  879. return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
  880. }
  881. CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
  882. FieldDecl *Field, QualType Ty,
  883. DeclContext *UsedContext)
  884. : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
  885. Ty->isLValueReferenceType() ? VK_LValue : Ty->isRValueReferenceType()
  886. ? VK_XValue
  887. : VK_RValue,
  888. /*FIXME*/ OK_Ordinary, false, false, false, false),
  889. Field(Field), UsedContext(UsedContext) {
  890. CXXDefaultInitExprBits.Loc = Loc;
  891. assert(Field->hasInClassInitializer());
  892. }
  893. CXXTemporary *CXXTemporary::Create(const ASTContext &C,
  894. const CXXDestructorDecl *Destructor) {
  895. return new (C) CXXTemporary(Destructor);
  896. }
  897. CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
  898. CXXTemporary *Temp,
  899. Expr* SubExpr) {
  900. assert((SubExpr->getType()->isRecordType() ||
  901. SubExpr->getType()->isArrayType()) &&
  902. "Expression bound to a temporary must have record or array type!");
  903. return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
  904. }
  905. CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
  906. CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI,
  907. ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
  908. bool HadMultipleCandidates, bool ListInitialization,
  909. bool StdInitListInitialization, bool ZeroInitialization)
  910. : CXXConstructExpr(
  911. CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
  912. Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
  913. ListInitialization, StdInitListInitialization, ZeroInitialization,
  914. CXXConstructExpr::CK_Complete, ParenOrBraceRange),
  915. TSI(TSI) {}
  916. CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty,
  917. unsigned NumArgs)
  918. : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {}
  919. CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create(
  920. const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
  921. TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
  922. bool HadMultipleCandidates, bool ListInitialization,
  923. bool StdInitListInitialization, bool ZeroInitialization) {
  924. unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
  925. void *Mem =
  926. Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
  927. alignof(CXXTemporaryObjectExpr));
  928. return new (Mem) CXXTemporaryObjectExpr(
  929. Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates,
  930. ListInitialization, StdInitListInitialization, ZeroInitialization);
  931. }
  932. CXXTemporaryObjectExpr *
  933. CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) {
  934. unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
  935. void *Mem =
  936. Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
  937. alignof(CXXTemporaryObjectExpr));
  938. return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs);
  939. }
  940. SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
  941. return getTypeSourceInfo()->getTypeLoc().getBeginLoc();
  942. }
  943. SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
  944. SourceLocation Loc = getParenOrBraceRange().getEnd();
  945. if (Loc.isInvalid() && getNumArgs())
  946. Loc = getArg(getNumArgs() - 1)->getEndLoc();
  947. return Loc;
  948. }
  949. CXXConstructExpr *CXXConstructExpr::Create(
  950. const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
  951. CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
  952. bool HadMultipleCandidates, bool ListInitialization,
  953. bool StdInitListInitialization, bool ZeroInitialization,
  954. ConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
  955. unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
  956. void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
  957. alignof(CXXConstructExpr));
  958. return new (Mem) CXXConstructExpr(
  959. CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args,
  960. HadMultipleCandidates, ListInitialization, StdInitListInitialization,
  961. ZeroInitialization, ConstructKind, ParenOrBraceRange);
  962. }
  963. CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx,
  964. unsigned NumArgs) {
  965. unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
  966. void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
  967. alignof(CXXConstructExpr));
  968. return new (Mem)
  969. CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs);
  970. }
  971. CXXConstructExpr::CXXConstructExpr(
  972. StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
  973. bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
  974. bool ListInitialization, bool StdInitListInitialization,
  975. bool ZeroInitialization, ConstructionKind ConstructKind,
  976. SourceRange ParenOrBraceRange)
  977. : Expr(SC, Ty, VK_RValue, OK_Ordinary, Ty->isDependentType(),
  978. Ty->isDependentType(), Ty->isInstantiationDependentType(),
  979. Ty->containsUnexpandedParameterPack()),
  980. Constructor(Ctor), ParenOrBraceRange(ParenOrBraceRange),
  981. NumArgs(Args.size()) {
  982. CXXConstructExprBits.Elidable = Elidable;
  983. CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates;
  984. CXXConstructExprBits.ListInitialization = ListInitialization;
  985. CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
  986. CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
  987. CXXConstructExprBits.ConstructionKind = ConstructKind;
  988. CXXConstructExprBits.Loc = Loc;
  989. Stmt **TrailingArgs = getTrailingArgs();
  990. for (unsigned I = 0, N = Args.size(); I != N; ++I) {
  991. assert(Args[I] && "NULL argument in CXXConstructExpr!");
  992. if (Args[I]->isValueDependent())
  993. ExprBits.ValueDependent = true;
  994. if (Args[I]->isInstantiationDependent())
  995. ExprBits.InstantiationDependent = true;
  996. if (Args[I]->containsUnexpandedParameterPack())
  997. ExprBits.ContainsUnexpandedParameterPack = true;
  998. TrailingArgs[I] = Args[I];
  999. }
  1000. }
  1001. CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty,
  1002. unsigned NumArgs)
  1003. : Expr(SC, Empty), NumArgs(NumArgs) {}
  1004. LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
  1005. LambdaCaptureKind Kind, VarDecl *Var,
  1006. SourceLocation EllipsisLoc)
  1007. : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
  1008. unsigned Bits = 0;
  1009. if (Implicit)
  1010. Bits |= Capture_Implicit;
  1011. switch (Kind) {
  1012. case LCK_StarThis:
  1013. Bits |= Capture_ByCopy;
  1014. LLVM_FALLTHROUGH;
  1015. case LCK_This:
  1016. assert(!Var && "'this' capture cannot have a variable!");
  1017. Bits |= Capture_This;
  1018. break;
  1019. case LCK_ByCopy:
  1020. Bits |= Capture_ByCopy;
  1021. LLVM_FALLTHROUGH;
  1022. case LCK_ByRef:
  1023. assert(Var && "capture must have a variable!");
  1024. break;
  1025. case LCK_VLAType:
  1026. assert(!Var && "VLA type capture cannot have a variable!");
  1027. break;
  1028. }
  1029. DeclAndBits.setInt(Bits);
  1030. }
  1031. LambdaCaptureKind LambdaCapture::getCaptureKind() const {
  1032. if (capturesVLAType())
  1033. return LCK_VLAType;
  1034. bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
  1035. if (capturesThis())
  1036. return CapByCopy ? LCK_StarThis : LCK_This;
  1037. return CapByCopy ? LCK_ByCopy : LCK_ByRef;
  1038. }
  1039. LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
  1040. LambdaCaptureDefault CaptureDefault,
  1041. SourceLocation CaptureDefaultLoc,
  1042. ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
  1043. bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
  1044. SourceLocation ClosingBrace,
  1045. bool ContainsUnexpandedParameterPack)
  1046. : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
  1047. T->isDependentType(), T->isDependentType(),
  1048. ContainsUnexpandedParameterPack),
  1049. IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
  1050. NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
  1051. ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
  1052. ClosingBrace(ClosingBrace) {
  1053. assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
  1054. CXXRecordDecl *Class = getLambdaClass();
  1055. CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
  1056. // FIXME: Propagate "has unexpanded parameter pack" bit.
  1057. // Copy captures.
  1058. const ASTContext &Context = Class->getASTContext();
  1059. Data.NumCaptures = NumCaptures;
  1060. Data.NumExplicitCaptures = 0;
  1061. Data.Captures =
  1062. (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
  1063. LambdaCapture *ToCapture = Data.Captures;
  1064. for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
  1065. if (Captures[I].isExplicit())
  1066. ++Data.NumExplicitCaptures;
  1067. *ToCapture++ = Captures[I];
  1068. }
  1069. // Copy initialization expressions for the non-static data members.
  1070. Stmt **Stored = getStoredStmts();
  1071. for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
  1072. *Stored++ = CaptureInits[I];
  1073. // Copy the body of the lambda.
  1074. *Stored++ = getCallOperator()->getBody();
  1075. }
  1076. LambdaExpr *LambdaExpr::Create(
  1077. const ASTContext &Context, CXXRecordDecl *Class,
  1078. SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
  1079. SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
  1080. bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
  1081. SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
  1082. // Determine the type of the expression (i.e., the type of the
  1083. // function object we're creating).
  1084. QualType T = Context.getTypeDeclType(Class);
  1085. unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
  1086. void *Mem = Context.Allocate(Size);
  1087. return new (Mem)
  1088. LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
  1089. Captures, ExplicitParams, ExplicitResultType, CaptureInits,
  1090. ClosingBrace, ContainsUnexpandedParameterPack);
  1091. }
  1092. LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
  1093. unsigned NumCaptures) {
  1094. unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
  1095. void *Mem = C.Allocate(Size);
  1096. return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
  1097. }
  1098. bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
  1099. return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
  1100. (getCallOperator() == C->getCapturedVar()->getDeclContext()));
  1101. }
  1102. LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
  1103. return getLambdaClass()->getLambdaData().Captures;
  1104. }
  1105. LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
  1106. return capture_begin() + NumCaptures;
  1107. }
  1108. LambdaExpr::capture_range LambdaExpr::captures() const {
  1109. return capture_range(capture_begin(), capture_end());
  1110. }
  1111. LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
  1112. return capture_begin();
  1113. }
  1114. LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
  1115. struct CXXRecordDecl::LambdaDefinitionData &Data
  1116. = getLambdaClass()->getLambdaData();
  1117. return Data.Captures + Data.NumExplicitCaptures;
  1118. }
  1119. LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
  1120. return capture_range(explicit_capture_begin(), explicit_capture_end());
  1121. }
  1122. LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
  1123. return explicit_capture_end();
  1124. }
  1125. LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
  1126. return capture_end();
  1127. }
  1128. LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
  1129. return capture_range(implicit_capture_begin(), implicit_capture_end());
  1130. }
  1131. CXXRecordDecl *LambdaExpr::getLambdaClass() const {
  1132. return getType()->getAsCXXRecordDecl();
  1133. }
  1134. CXXMethodDecl *LambdaExpr::getCallOperator() const {
  1135. CXXRecordDecl *Record = getLambdaClass();
  1136. return Record->getLambdaCallOperator();
  1137. }
  1138. FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const {
  1139. CXXRecordDecl *Record = getLambdaClass();
  1140. return Record->getDependentLambdaCallOperator();
  1141. }
  1142. TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
  1143. CXXRecordDecl *Record = getLambdaClass();
  1144. return Record->getGenericLambdaTemplateParameterList();
  1145. }
  1146. ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const {
  1147. const CXXRecordDecl *Record = getLambdaClass();
  1148. return Record->getLambdaExplicitTemplateParameters();
  1149. }
  1150. CompoundStmt *LambdaExpr::getBody() const {
  1151. // FIXME: this mutation in getBody is bogus. It should be
  1152. // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
  1153. // don't understand, that doesn't work.
  1154. if (!getStoredStmts()[NumCaptures])
  1155. *const_cast<Stmt **>(&getStoredStmts()[NumCaptures]) =
  1156. getCallOperator()->getBody();
  1157. return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
  1158. }
  1159. bool LambdaExpr::isMutable() const {
  1160. return !getCallOperator()->isConst();
  1161. }
  1162. ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
  1163. bool CleanupsHaveSideEffects,
  1164. ArrayRef<CleanupObject> objects)
  1165. : FullExpr(ExprWithCleanupsClass, subexpr) {
  1166. ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
  1167. ExprWithCleanupsBits.NumObjects = objects.size();
  1168. for (unsigned i = 0, e = objects.size(); i != e; ++i)
  1169. getTrailingObjects<CleanupObject>()[i] = objects[i];
  1170. }
  1171. ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
  1172. bool CleanupsHaveSideEffects,
  1173. ArrayRef<CleanupObject> objects) {
  1174. void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
  1175. alignof(ExprWithCleanups));
  1176. return new (buffer)
  1177. ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
  1178. }
  1179. ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
  1180. : FullExpr(ExprWithCleanupsClass, empty) {
  1181. ExprWithCleanupsBits.NumObjects = numObjects;
  1182. }
  1183. ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
  1184. EmptyShell empty,
  1185. unsigned numObjects) {
  1186. void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
  1187. alignof(ExprWithCleanups));
  1188. return new (buffer) ExprWithCleanups(empty, numObjects);
  1189. }
  1190. CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *TSI,
  1191. SourceLocation LParenLoc,
  1192. ArrayRef<Expr *> Args,
  1193. SourceLocation RParenLoc)
  1194. : Expr(CXXUnresolvedConstructExprClass,
  1195. TSI->getType().getNonReferenceType(),
  1196. (TSI->getType()->isLValueReferenceType()
  1197. ? VK_LValue
  1198. : TSI->getType()->isRValueReferenceType() ? VK_XValue
  1199. : VK_RValue),
  1200. OK_Ordinary,
  1201. TSI->getType()->isDependentType() ||
  1202. TSI->getType()->getContainedDeducedType(),
  1203. true, true, TSI->getType()->containsUnexpandedParameterPack()),
  1204. TSI(TSI), LParenLoc(LParenLoc), RParenLoc(RParenLoc) {
  1205. CXXUnresolvedConstructExprBits.NumArgs = Args.size();
  1206. auto **StoredArgs = getTrailingObjects<Expr *>();
  1207. for (unsigned I = 0; I != Args.size(); ++I) {
  1208. if (Args[I]->containsUnexpandedParameterPack())
  1209. ExprBits.ContainsUnexpandedParameterPack = true;
  1210. StoredArgs[I] = Args[I];
  1211. }
  1212. }
  1213. CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create(
  1214. const ASTContext &Context, TypeSourceInfo *TSI, SourceLocation LParenLoc,
  1215. ArrayRef<Expr *> Args, SourceLocation RParenLoc) {
  1216. void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
  1217. return new (Mem) CXXUnresolvedConstructExpr(TSI, LParenLoc, Args, RParenLoc);
  1218. }
  1219. CXXUnresolvedConstructExpr *
  1220. CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context,
  1221. unsigned NumArgs) {
  1222. void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
  1223. return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs);
  1224. }
  1225. SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
  1226. return TSI->getTypeLoc().getBeginLoc();
  1227. }
  1228. CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
  1229. const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
  1230. SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
  1231. SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
  1232. DeclarationNameInfo MemberNameInfo,
  1233. const TemplateArgumentListInfo *TemplateArgs)
  1234. : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue,
  1235. OK_Ordinary, true, true, true,
  1236. ((Base && Base->containsUnexpandedParameterPack()) ||
  1237. (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
  1238. ->containsUnexpandedParameterPack()) ||
  1239. MemberNameInfo.containsUnexpandedParameterPack())),
  1240. Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc),
  1241. MemberNameInfo(MemberNameInfo) {
  1242. CXXDependentScopeMemberExprBits.IsArrow = IsArrow;
  1243. CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
  1244. (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
  1245. CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
  1246. FirstQualifierFoundInScope != nullptr;
  1247. CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc;
  1248. if (TemplateArgs) {
  1249. bool Dependent = true;
  1250. bool InstantiationDependent = true;
  1251. bool ContainsUnexpandedParameterPack = false;
  1252. getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
  1253. TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
  1254. Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
  1255. if (ContainsUnexpandedParameterPack)
  1256. ExprBits.ContainsUnexpandedParameterPack = true;
  1257. } else if (TemplateKWLoc.isValid()) {
  1258. getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
  1259. TemplateKWLoc);
  1260. }
  1261. if (hasFirstQualifierFoundInScope())
  1262. *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope;
  1263. }
  1264. CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
  1265. EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
  1266. bool HasFirstQualifierFoundInScope)
  1267. : Expr(CXXDependentScopeMemberExprClass, Empty) {
  1268. CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
  1269. HasTemplateKWAndArgsInfo;
  1270. CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
  1271. HasFirstQualifierFoundInScope;
  1272. }
  1273. CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create(
  1274. const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
  1275. SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
  1276. SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
  1277. DeclarationNameInfo MemberNameInfo,
  1278. const TemplateArgumentListInfo *TemplateArgs) {
  1279. bool HasTemplateKWAndArgsInfo =
  1280. (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
  1281. unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
  1282. bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr;
  1283. unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
  1284. TemplateArgumentLoc, NamedDecl *>(
  1285. HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
  1286. void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
  1287. return new (Mem) CXXDependentScopeMemberExpr(
  1288. Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
  1289. FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs);
  1290. }
  1291. CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty(
  1292. const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
  1293. unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) {
  1294. assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
  1295. unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
  1296. TemplateArgumentLoc, NamedDecl *>(
  1297. HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
  1298. void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
  1299. return new (Mem) CXXDependentScopeMemberExpr(
  1300. EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope);
  1301. }
  1302. static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
  1303. UnresolvedSetIterator end) {
  1304. do {
  1305. NamedDecl *decl = *begin;
  1306. if (isa<UnresolvedUsingValueDecl>(decl))
  1307. return false;
  1308. // Unresolved member expressions should only contain methods and
  1309. // method templates.
  1310. if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
  1311. ->isStatic())
  1312. return false;
  1313. } while (++begin != end);
  1314. return true;
  1315. }
  1316. UnresolvedMemberExpr::UnresolvedMemberExpr(
  1317. const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
  1318. QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
  1319. NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
  1320. const DeclarationNameInfo &MemberNameInfo,
  1321. const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
  1322. UnresolvedSetIterator End)
  1323. : OverloadExpr(
  1324. UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc,
  1325. MemberNameInfo, TemplateArgs, Begin, End,
  1326. // Dependent
  1327. ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
  1328. ((Base && Base->isInstantiationDependent()) ||
  1329. BaseType->isInstantiationDependentType()),
  1330. // Contains unexpanded parameter pack
  1331. ((Base && Base->containsUnexpandedParameterPack()) ||
  1332. BaseType->containsUnexpandedParameterPack())),
  1333. Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
  1334. UnresolvedMemberExprBits.IsArrow = IsArrow;
  1335. UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing;
  1336. // Check whether all of the members are non-static member functions,
  1337. // and if so, mark give this bound-member type instead of overload type.
  1338. if (hasOnlyNonStaticMemberFunctions(Begin, End))
  1339. setType(Context.BoundMemberTy);
  1340. }
  1341. UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty,
  1342. unsigned NumResults,
  1343. bool HasTemplateKWAndArgsInfo)
  1344. : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults,
  1345. HasTemplateKWAndArgsInfo) {}
  1346. bool UnresolvedMemberExpr::isImplicitAccess() const {
  1347. if (!Base)
  1348. return true;
  1349. return cast<Expr>(Base)->isImplicitCXXThis();
  1350. }
  1351. UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
  1352. const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
  1353. QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
  1354. NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
  1355. const DeclarationNameInfo &MemberNameInfo,
  1356. const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
  1357. UnresolvedSetIterator End) {
  1358. unsigned NumResults = End - Begin;
  1359. bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
  1360. unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
  1361. unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
  1362. TemplateArgumentLoc>(
  1363. NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
  1364. void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
  1365. return new (Mem) UnresolvedMemberExpr(
  1366. Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc,
  1367. QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
  1368. }
  1369. UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty(
  1370. const ASTContext &Context, unsigned NumResults,
  1371. bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
  1372. assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
  1373. unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
  1374. TemplateArgumentLoc>(
  1375. NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
  1376. void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
  1377. return new (Mem)
  1378. UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
  1379. }
  1380. CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() {
  1381. // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
  1382. // If there was a nested name specifier, it names the naming class.
  1383. // It can't be dependent: after all, we were actually able to do the
  1384. // lookup.
  1385. CXXRecordDecl *Record = nullptr;
  1386. auto *NNS = getQualifier();
  1387. if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
  1388. const Type *T = getQualifier()->getAsType();
  1389. assert(T && "qualifier in member expression does not name type");
  1390. Record = T->getAsCXXRecordDecl();
  1391. assert(Record && "qualifier in member expression does not name record");
  1392. }
  1393. // Otherwise the naming class must have been the base class.
  1394. else {
  1395. QualType BaseType = getBaseType().getNonReferenceType();
  1396. if (isArrow())
  1397. BaseType = BaseType->castAs<PointerType>()->getPointeeType();
  1398. Record = BaseType->getAsCXXRecordDecl();
  1399. assert(Record && "base of member expression does not name record");
  1400. }
  1401. return Record;
  1402. }
  1403. SizeOfPackExpr *
  1404. SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
  1405. NamedDecl *Pack, SourceLocation PackLoc,
  1406. SourceLocation RParenLoc,
  1407. Optional<unsigned> Length,
  1408. ArrayRef<TemplateArgument> PartialArgs) {
  1409. void *Storage =
  1410. Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
  1411. return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
  1412. PackLoc, RParenLoc, Length, PartialArgs);
  1413. }
  1414. SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
  1415. unsigned NumPartialArgs) {
  1416. void *Storage =
  1417. Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
  1418. return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
  1419. }
  1420. SubstNonTypeTemplateParmPackExpr::
  1421. SubstNonTypeTemplateParmPackExpr(QualType T,
  1422. ExprValueKind ValueKind,
  1423. NonTypeTemplateParmDecl *Param,
  1424. SourceLocation NameLoc,
  1425. const TemplateArgument &ArgPack)
  1426. : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary,
  1427. true, true, true, true),
  1428. Param(Param), Arguments(ArgPack.pack_begin()),
  1429. NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {}
  1430. TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
  1431. return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
  1432. }
  1433. FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
  1434. SourceLocation NameLoc,
  1435. unsigned NumParams,
  1436. VarDecl *const *Params)
  1437. : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
  1438. true, true),
  1439. ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
  1440. if (Params)
  1441. std::uninitialized_copy(Params, Params + NumParams,
  1442. getTrailingObjects<VarDecl *>());
  1443. }
  1444. FunctionParmPackExpr *
  1445. FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
  1446. VarDecl *ParamPack, SourceLocation NameLoc,
  1447. ArrayRef<VarDecl *> Params) {
  1448. return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size())))
  1449. FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
  1450. }
  1451. FunctionParmPackExpr *
  1452. FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
  1453. unsigned NumParams) {
  1454. return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams)))
  1455. FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
  1456. }
  1457. void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
  1458. unsigned ManglingNumber) {
  1459. // We only need extra state if we have to remember more than just the Stmt.
  1460. if (!ExtendedBy)
  1461. return;
  1462. // We may need to allocate extra storage for the mangling number and the
  1463. // extended-by ValueDecl.
  1464. if (!State.is<ExtraState *>()) {
  1465. auto *ES = new (ExtendedBy->getASTContext()) ExtraState;
  1466. ES->Temporary = State.get<Stmt *>();
  1467. State = ES;
  1468. }
  1469. auto ES = State.get<ExtraState *>();
  1470. ES->ExtendingDecl = ExtendedBy;
  1471. ES->ManglingNumber = ManglingNumber;
  1472. }
  1473. TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
  1474. ArrayRef<TypeSourceInfo *> Args,
  1475. SourceLocation RParenLoc,
  1476. bool Value)
  1477. : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
  1478. /*TypeDependent=*/false,
  1479. /*ValueDependent=*/false,
  1480. /*InstantiationDependent=*/false,
  1481. /*ContainsUnexpandedParameterPack=*/false),
  1482. Loc(Loc), RParenLoc(RParenLoc) {
  1483. TypeTraitExprBits.Kind = Kind;
  1484. TypeTraitExprBits.Value = Value;
  1485. TypeTraitExprBits.NumArgs = Args.size();
  1486. auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
  1487. for (unsigned I = 0, N = Args.size(); I != N; ++I) {
  1488. if (Args[I]->getType()->isDependentType())
  1489. setValueDependent(true);
  1490. if (Args[I]->getType()->isInstantiationDependentType())
  1491. setInstantiationDependent(true);
  1492. if (Args[I]->getType()->containsUnexpandedParameterPack())
  1493. setContainsUnexpandedParameterPack(true);
  1494. ToArgs[I] = Args[I];
  1495. }
  1496. }
  1497. TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
  1498. SourceLocation Loc,
  1499. TypeTrait Kind,
  1500. ArrayRef<TypeSourceInfo *> Args,
  1501. SourceLocation RParenLoc,
  1502. bool Value) {
  1503. void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
  1504. return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
  1505. }
  1506. TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
  1507. unsigned NumArgs) {
  1508. void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
  1509. return new (Mem) TypeTraitExpr(EmptyShell());
  1510. }
  1511. CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config,
  1512. ArrayRef<Expr *> Args, QualType Ty,
  1513. ExprValueKind VK, SourceLocation RP,
  1514. unsigned MinNumArgs)
  1515. : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK,
  1516. RP, MinNumArgs, NotADL) {}
  1517. CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, EmptyShell Empty)
  1518. : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs,
  1519. Empty) {}
  1520. CUDAKernelCallExpr *
  1521. CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
  1522. ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
  1523. SourceLocation RP, unsigned MinNumArgs) {
  1524. // Allocate storage for the trailing objects of CallExpr.
  1525. unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
  1526. unsigned SizeOfTrailingObjects =
  1527. CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
  1528. void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
  1529. alignof(CUDAKernelCallExpr));
  1530. return new (Mem) CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, MinNumArgs);
  1531. }
  1532. CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
  1533. unsigned NumArgs,
  1534. EmptyShell Empty) {
  1535. // Allocate storage for the trailing objects of CallExpr.
  1536. unsigned SizeOfTrailingObjects =
  1537. CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
  1538. void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
  1539. alignof(CUDAKernelCallExpr));
  1540. return new (Mem) CUDAKernelCallExpr(NumArgs, Empty);
  1541. }
  1542. ConceptSpecializationExpr::ConceptSpecializationExpr(ASTContext &C,
  1543. NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc,
  1544. SourceLocation ConceptNameLoc, NamedDecl *FoundDecl,
  1545. ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten,
  1546. ArrayRef<TemplateArgument> ConvertedArgs, Optional<bool> IsSatisfied)
  1547. : Expr(ConceptSpecializationExprClass, C.BoolTy, VK_RValue, OK_Ordinary,
  1548. /*TypeDependent=*/false,
  1549. // All the flags below are set in setTemplateArguments.
  1550. /*ValueDependent=*/!IsSatisfied.hasValue(),
  1551. /*InstantiationDependent=*/false,
  1552. /*ContainsUnexpandedParameterPacks=*/false),
  1553. NestedNameSpec(NNS), TemplateKWLoc(TemplateKWLoc),
  1554. ConceptNameLoc(ConceptNameLoc), FoundDecl(FoundDecl),
  1555. NamedConcept(NamedConcept, IsSatisfied ? *IsSatisfied : true),
  1556. NumTemplateArgs(ConvertedArgs.size()) {
  1557. setTemplateArguments(ArgsAsWritten, ConvertedArgs);
  1558. }
  1559. ConceptSpecializationExpr::ConceptSpecializationExpr(EmptyShell Empty,
  1560. unsigned NumTemplateArgs)
  1561. : Expr(ConceptSpecializationExprClass, Empty),
  1562. NumTemplateArgs(NumTemplateArgs) { }
  1563. void ConceptSpecializationExpr::setTemplateArguments(
  1564. const ASTTemplateArgumentListInfo *ArgsAsWritten,
  1565. ArrayRef<TemplateArgument> Converted) {
  1566. assert(Converted.size() == NumTemplateArgs);
  1567. assert(!this->ArgsAsWritten && "setTemplateArguments can only be used once");
  1568. this->ArgsAsWritten = ArgsAsWritten;
  1569. std::uninitialized_copy(Converted.begin(), Converted.end(),
  1570. getTrailingObjects<TemplateArgument>());
  1571. bool IsInstantiationDependent = false;
  1572. bool ContainsUnexpandedParameterPack = false;
  1573. for (const TemplateArgumentLoc& LocInfo : ArgsAsWritten->arguments()) {
  1574. if (LocInfo.getArgument().isInstantiationDependent())
  1575. IsInstantiationDependent = true;
  1576. if (LocInfo.getArgument().containsUnexpandedParameterPack())
  1577. ContainsUnexpandedParameterPack = true;
  1578. if (ContainsUnexpandedParameterPack && IsInstantiationDependent)
  1579. break;
  1580. }
  1581. // Currently guaranteed by the fact concepts can only be at namespace-scope.
  1582. assert(!NestedNameSpec ||
  1583. (!NestedNameSpec.getNestedNameSpecifier()->isInstantiationDependent() &&
  1584. !NestedNameSpec.getNestedNameSpecifier()
  1585. ->containsUnexpandedParameterPack()));
  1586. setInstantiationDependent(IsInstantiationDependent);
  1587. setContainsUnexpandedParameterPack(ContainsUnexpandedParameterPack);
  1588. assert((!isValueDependent() || isInstantiationDependent()) &&
  1589. "should not be value-dependent");
  1590. }
  1591. ConceptSpecializationExpr *
  1592. ConceptSpecializationExpr::Create(ASTContext &C, NestedNameSpecifierLoc NNS,
  1593. SourceLocation TemplateKWLoc,
  1594. SourceLocation ConceptNameLoc,
  1595. NamedDecl *FoundDecl,
  1596. ConceptDecl *NamedConcept,
  1597. const ASTTemplateArgumentListInfo *ArgsAsWritten,
  1598. ArrayRef<TemplateArgument> ConvertedArgs,
  1599. Optional<bool> IsSatisfied) {
  1600. void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
  1601. ConvertedArgs.size()));
  1602. return new (Buffer) ConceptSpecializationExpr(C, NNS, TemplateKWLoc,
  1603. ConceptNameLoc, FoundDecl,
  1604. NamedConcept, ArgsAsWritten,
  1605. ConvertedArgs, IsSatisfied);
  1606. }
  1607. ConceptSpecializationExpr *
  1608. ConceptSpecializationExpr::Create(ASTContext &C, EmptyShell Empty,
  1609. unsigned NumTemplateArgs) {
  1610. void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
  1611. NumTemplateArgs));
  1612. return new (Buffer) ConceptSpecializationExpr(Empty, NumTemplateArgs);
  1613. }