ExprCXX.cpp 77 KB

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