ExprCXX.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the subclesses of Expr class declared in ExprCXX.h
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Attr.h"
  15. #include "clang/AST/DeclCXX.h"
  16. #include "clang/AST/DeclTemplate.h"
  17. #include "clang/AST/ExprCXX.h"
  18. #include "clang/AST/TypeLoc.h"
  19. #include "clang/Basic/IdentifierTable.h"
  20. using namespace clang;
  21. //===----------------------------------------------------------------------===//
  22. // Child Iterators for iterating over subexpressions/substatements
  23. //===----------------------------------------------------------------------===//
  24. bool CXXTypeidExpr::isPotentiallyEvaluated() const {
  25. if (isTypeOperand())
  26. return false;
  27. // C++11 [expr.typeid]p3:
  28. // When typeid is applied to an expression other than a glvalue of
  29. // polymorphic class type, [...] the expression is an unevaluated operand.
  30. const Expr *E = getExprOperand();
  31. if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
  32. if (RD->isPolymorphic() && E->isGLValue())
  33. return true;
  34. return false;
  35. }
  36. QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
  37. assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
  38. Qualifiers Quals;
  39. return Context.getUnqualifiedArrayType(
  40. Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
  41. }
  42. QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
  43. assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
  44. Qualifiers Quals;
  45. return Context.getUnqualifiedArrayType(
  46. Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
  47. }
  48. // static
  49. const UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT,
  50. bool *RDHasMultipleGUIDsPtr) {
  51. // Optionally remove one level of pointer, reference or array indirection.
  52. const Type *Ty = QT.getTypePtr();
  53. if (QT->isPointerType() || QT->isReferenceType())
  54. Ty = QT->getPointeeType().getTypePtr();
  55. else if (QT->isArrayType())
  56. Ty = Ty->getBaseElementTypeUnsafe();
  57. const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
  58. if (!RD)
  59. return nullptr;
  60. if (const UuidAttr *Uuid = RD->getMostRecentDecl()->getAttr<UuidAttr>())
  61. return Uuid;
  62. // __uuidof can grab UUIDs from template arguments.
  63. if (const ClassTemplateSpecializationDecl *CTSD =
  64. dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
  65. const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
  66. const UuidAttr *UuidForRD = nullptr;
  67. for (const TemplateArgument &TA : TAL.asArray()) {
  68. bool SeenMultipleGUIDs = false;
  69. const UuidAttr *UuidForTA = nullptr;
  70. if (TA.getKind() == TemplateArgument::Type)
  71. UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
  72. else if (TA.getKind() == TemplateArgument::Declaration)
  73. UuidForTA =
  74. GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
  75. // If the template argument has a UUID, there are three cases:
  76. // - This is the first UUID seen for this RecordDecl.
  77. // - This is a different UUID than previously seen for this RecordDecl.
  78. // - This is the same UUID than previously seen for this RecordDecl.
  79. if (UuidForTA) {
  80. if (!UuidForRD)
  81. UuidForRD = UuidForTA;
  82. else if (UuidForRD != UuidForTA)
  83. SeenMultipleGUIDs = true;
  84. }
  85. // Seeing multiple UUIDs means that we couldn't find a UUID
  86. if (SeenMultipleGUIDs) {
  87. if (RDHasMultipleGUIDsPtr)
  88. *RDHasMultipleGUIDsPtr = true;
  89. return nullptr;
  90. }
  91. }
  92. return UuidForRD;
  93. }
  94. return nullptr;
  95. }
  96. StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
  97. StringRef Uuid;
  98. if (isTypeOperand())
  99. Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid();
  100. else {
  101. // Special case: __uuidof(0) means an all-zero GUID.
  102. Expr *Op = getExprOperand();
  103. if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
  104. Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
  105. else
  106. Uuid = "00000000-0000-0000-0000-000000000000";
  107. }
  108. return Uuid;
  109. }
  110. // CXXScalarValueInitExpr
  111. SourceLocation CXXScalarValueInitExpr::getLocStart() const {
  112. return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
  113. }
  114. // CXXNewExpr
  115. CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
  116. FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
  117. bool usualArrayDeleteWantsSize,
  118. ArrayRef<Expr*> placementArgs,
  119. SourceRange typeIdParens, Expr *arraySize,
  120. InitializationStyle initializationStyle,
  121. Expr *initializer, QualType ty,
  122. TypeSourceInfo *allocatedTypeInfo,
  123. SourceRange Range, SourceRange directInitRange)
  124. : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
  125. ty->isDependentType(), ty->isDependentType(),
  126. ty->isInstantiationDependentType(),
  127. ty->containsUnexpandedParameterPack()),
  128. SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
  129. AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
  130. Range(Range), DirectInitRange(directInitRange),
  131. GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
  132. assert((initializer != nullptr || initializationStyle == NoInit) &&
  133. "Only NoInit can have no initializer.");
  134. StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
  135. AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
  136. initializer != nullptr);
  137. unsigned i = 0;
  138. if (Array) {
  139. if (arraySize->isInstantiationDependent())
  140. ExprBits.InstantiationDependent = true;
  141. if (arraySize->containsUnexpandedParameterPack())
  142. ExprBits.ContainsUnexpandedParameterPack = true;
  143. SubExprs[i++] = arraySize;
  144. }
  145. if (initializer) {
  146. if (initializer->isInstantiationDependent())
  147. ExprBits.InstantiationDependent = true;
  148. if (initializer->containsUnexpandedParameterPack())
  149. ExprBits.ContainsUnexpandedParameterPack = true;
  150. SubExprs[i++] = initializer;
  151. }
  152. for (unsigned j = 0; j != placementArgs.size(); ++j) {
  153. if (placementArgs[j]->isInstantiationDependent())
  154. ExprBits.InstantiationDependent = true;
  155. if (placementArgs[j]->containsUnexpandedParameterPack())
  156. ExprBits.ContainsUnexpandedParameterPack = true;
  157. SubExprs[i++] = placementArgs[j];
  158. }
  159. switch (getInitializationStyle()) {
  160. case CallInit:
  161. this->Range.setEnd(DirectInitRange.getEnd()); break;
  162. case ListInit:
  163. this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
  164. default:
  165. if (TypeIdParens.isValid())
  166. this->Range.setEnd(TypeIdParens.getEnd());
  167. break;
  168. }
  169. }
  170. void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
  171. unsigned numPlaceArgs, bool hasInitializer){
  172. assert(SubExprs == nullptr && "SubExprs already allocated");
  173. Array = isArray;
  174. NumPlacementArgs = numPlaceArgs;
  175. unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
  176. SubExprs = new (C) Stmt*[TotalSize];
  177. }
  178. bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
  179. return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
  180. Ctx) &&
  181. !getOperatorNew()->isReservedGlobalPlacementOperator();
  182. }
  183. // CXXDeleteExpr
  184. QualType CXXDeleteExpr::getDestroyedType() const {
  185. const Expr *Arg = getArgument();
  186. // The type-to-delete may not be a pointer if it's a dependent type.
  187. const QualType ArgType = Arg->getType();
  188. if (ArgType->isDependentType() && !ArgType->isPointerType())
  189. return QualType();
  190. return ArgType->getAs<PointerType>()->getPointeeType();
  191. }
  192. // CXXPseudoDestructorExpr
  193. PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
  194. : Type(Info)
  195. {
  196. Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
  197. }
  198. CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
  199. Expr *Base, bool isArrow, SourceLocation OperatorLoc,
  200. NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
  201. SourceLocation ColonColonLoc, SourceLocation TildeLoc,
  202. PseudoDestructorTypeStorage DestroyedType)
  203. : Expr(CXXPseudoDestructorExprClass,
  204. Context.BoundMemberTy,
  205. VK_RValue, OK_Ordinary,
  206. /*isTypeDependent=*/(Base->isTypeDependent() ||
  207. (DestroyedType.getTypeSourceInfo() &&
  208. DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
  209. /*isValueDependent=*/Base->isValueDependent(),
  210. (Base->isInstantiationDependent() ||
  211. (QualifierLoc &&
  212. QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
  213. (ScopeType &&
  214. ScopeType->getType()->isInstantiationDependentType()) ||
  215. (DestroyedType.getTypeSourceInfo() &&
  216. DestroyedType.getTypeSourceInfo()->getType()
  217. ->isInstantiationDependentType())),
  218. // ContainsUnexpandedParameterPack
  219. (Base->containsUnexpandedParameterPack() ||
  220. (QualifierLoc &&
  221. QualifierLoc.getNestedNameSpecifier()
  222. ->containsUnexpandedParameterPack()) ||
  223. (ScopeType &&
  224. ScopeType->getType()->containsUnexpandedParameterPack()) ||
  225. (DestroyedType.getTypeSourceInfo() &&
  226. DestroyedType.getTypeSourceInfo()->getType()
  227. ->containsUnexpandedParameterPack()))),
  228. Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
  229. OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
  230. ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
  231. DestroyedType(DestroyedType) { }
  232. QualType CXXPseudoDestructorExpr::getDestroyedType() const {
  233. if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
  234. return TInfo->getType();
  235. return QualType();
  236. }
  237. SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
  238. SourceLocation End = DestroyedType.getLocation();
  239. if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
  240. End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
  241. return End;
  242. }
  243. // UnresolvedLookupExpr
  244. UnresolvedLookupExpr *
  245. UnresolvedLookupExpr::Create(const ASTContext &C,
  246. CXXRecordDecl *NamingClass,
  247. NestedNameSpecifierLoc QualifierLoc,
  248. SourceLocation TemplateKWLoc,
  249. const DeclarationNameInfo &NameInfo,
  250. bool ADL,
  251. const TemplateArgumentListInfo *Args,
  252. UnresolvedSetIterator Begin,
  253. UnresolvedSetIterator End)
  254. {
  255. assert(Args || TemplateKWLoc.isValid());
  256. unsigned num_args = Args ? Args->size() : 0;
  257. void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
  258. ASTTemplateKWAndArgsInfo::sizeFor(num_args));
  259. return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
  260. TemplateKWLoc, NameInfo,
  261. ADL, /*Overload*/ true, Args,
  262. Begin, End);
  263. }
  264. UnresolvedLookupExpr *
  265. UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
  266. bool HasTemplateKWAndArgsInfo,
  267. unsigned NumTemplateArgs) {
  268. std::size_t size = sizeof(UnresolvedLookupExpr);
  269. if (HasTemplateKWAndArgsInfo)
  270. size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
  271. void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
  272. UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
  273. E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
  274. return E;
  275. }
  276. OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
  277. NestedNameSpecifierLoc QualifierLoc,
  278. SourceLocation TemplateKWLoc,
  279. const DeclarationNameInfo &NameInfo,
  280. const TemplateArgumentListInfo *TemplateArgs,
  281. UnresolvedSetIterator Begin,
  282. UnresolvedSetIterator End,
  283. bool KnownDependent,
  284. bool KnownInstantiationDependent,
  285. bool KnownContainsUnexpandedParameterPack)
  286. : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
  287. KnownDependent,
  288. (KnownInstantiationDependent ||
  289. NameInfo.isInstantiationDependent() ||
  290. (QualifierLoc &&
  291. QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
  292. (KnownContainsUnexpandedParameterPack ||
  293. NameInfo.containsUnexpandedParameterPack() ||
  294. (QualifierLoc &&
  295. QualifierLoc.getNestedNameSpecifier()
  296. ->containsUnexpandedParameterPack()))),
  297. NameInfo(NameInfo), QualifierLoc(QualifierLoc),
  298. Results(nullptr), NumResults(End - Begin),
  299. HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
  300. TemplateKWLoc.isValid()) {
  301. NumResults = End - Begin;
  302. if (NumResults) {
  303. // Determine whether this expression is type-dependent.
  304. for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
  305. if ((*I)->getDeclContext()->isDependentContext() ||
  306. isa<UnresolvedUsingValueDecl>(*I)) {
  307. ExprBits.TypeDependent = true;
  308. ExprBits.ValueDependent = true;
  309. ExprBits.InstantiationDependent = true;
  310. }
  311. }
  312. Results = static_cast<DeclAccessPair *>(
  313. C.Allocate(sizeof(DeclAccessPair) * NumResults,
  314. llvm::alignOf<DeclAccessPair>()));
  315. memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
  316. }
  317. // If we have explicit template arguments, check for dependent
  318. // template arguments and whether they contain any unexpanded pack
  319. // expansions.
  320. if (TemplateArgs) {
  321. bool Dependent = false;
  322. bool InstantiationDependent = false;
  323. bool ContainsUnexpandedParameterPack = false;
  324. getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
  325. Dependent,
  326. InstantiationDependent,
  327. ContainsUnexpandedParameterPack);
  328. if (Dependent) {
  329. ExprBits.TypeDependent = true;
  330. ExprBits.ValueDependent = true;
  331. }
  332. if (InstantiationDependent)
  333. ExprBits.InstantiationDependent = true;
  334. if (ContainsUnexpandedParameterPack)
  335. ExprBits.ContainsUnexpandedParameterPack = true;
  336. } else if (TemplateKWLoc.isValid()) {
  337. getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
  338. }
  339. if (isTypeDependent())
  340. setType(C.DependentTy);
  341. }
  342. void OverloadExpr::initializeResults(const ASTContext &C,
  343. UnresolvedSetIterator Begin,
  344. UnresolvedSetIterator End) {
  345. assert(!Results && "Results already initialized!");
  346. NumResults = End - Begin;
  347. if (NumResults) {
  348. Results = static_cast<DeclAccessPair *>(
  349. C.Allocate(sizeof(DeclAccessPair) * NumResults,
  350. llvm::alignOf<DeclAccessPair>()));
  351. memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
  352. }
  353. }
  354. CXXRecordDecl *OverloadExpr::getNamingClass() const {
  355. if (isa<UnresolvedLookupExpr>(this))
  356. return cast<UnresolvedLookupExpr>(this)->getNamingClass();
  357. else
  358. return cast<UnresolvedMemberExpr>(this)->getNamingClass();
  359. }
  360. // DependentScopeDeclRefExpr
  361. DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
  362. NestedNameSpecifierLoc QualifierLoc,
  363. SourceLocation TemplateKWLoc,
  364. const DeclarationNameInfo &NameInfo,
  365. const TemplateArgumentListInfo *Args)
  366. : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
  367. true, true,
  368. (NameInfo.isInstantiationDependent() ||
  369. (QualifierLoc &&
  370. QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
  371. (NameInfo.containsUnexpandedParameterPack() ||
  372. (QualifierLoc &&
  373. QualifierLoc.getNestedNameSpecifier()
  374. ->containsUnexpandedParameterPack()))),
  375. QualifierLoc(QualifierLoc), NameInfo(NameInfo),
  376. HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
  377. {
  378. if (Args) {
  379. bool Dependent = true;
  380. bool InstantiationDependent = true;
  381. bool ContainsUnexpandedParameterPack
  382. = ExprBits.ContainsUnexpandedParameterPack;
  383. getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
  384. Dependent,
  385. InstantiationDependent,
  386. ContainsUnexpandedParameterPack);
  387. ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
  388. } else if (TemplateKWLoc.isValid()) {
  389. getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
  390. }
  391. }
  392. DependentScopeDeclRefExpr *
  393. DependentScopeDeclRefExpr::Create(const ASTContext &C,
  394. NestedNameSpecifierLoc QualifierLoc,
  395. SourceLocation TemplateKWLoc,
  396. const DeclarationNameInfo &NameInfo,
  397. const TemplateArgumentListInfo *Args) {
  398. assert(QualifierLoc && "should be created for dependent qualifiers");
  399. std::size_t size = sizeof(DependentScopeDeclRefExpr);
  400. if (Args)
  401. size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
  402. else if (TemplateKWLoc.isValid())
  403. size += ASTTemplateKWAndArgsInfo::sizeFor(0);
  404. void *Mem = C.Allocate(size);
  405. return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
  406. TemplateKWLoc, NameInfo, Args);
  407. }
  408. DependentScopeDeclRefExpr *
  409. DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
  410. bool HasTemplateKWAndArgsInfo,
  411. unsigned NumTemplateArgs) {
  412. std::size_t size = sizeof(DependentScopeDeclRefExpr);
  413. if (HasTemplateKWAndArgsInfo)
  414. size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
  415. void *Mem = C.Allocate(size);
  416. DependentScopeDeclRefExpr *E
  417. = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
  418. SourceLocation(),
  419. DeclarationNameInfo(), nullptr);
  420. E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
  421. return E;
  422. }
  423. SourceLocation CXXConstructExpr::getLocStart() const {
  424. if (isa<CXXTemporaryObjectExpr>(this))
  425. return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
  426. return Loc;
  427. }
  428. SourceLocation CXXConstructExpr::getLocEnd() const {
  429. if (isa<CXXTemporaryObjectExpr>(this))
  430. return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
  431. if (ParenOrBraceRange.isValid())
  432. return ParenOrBraceRange.getEnd();
  433. SourceLocation End = Loc;
  434. for (unsigned I = getNumArgs(); I > 0; --I) {
  435. const Expr *Arg = getArg(I-1);
  436. if (!Arg->isDefaultArgument()) {
  437. SourceLocation NewEnd = Arg->getLocEnd();
  438. if (NewEnd.isValid()) {
  439. End = NewEnd;
  440. break;
  441. }
  442. }
  443. }
  444. return End;
  445. }
  446. SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
  447. OverloadedOperatorKind Kind = getOperator();
  448. if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
  449. if (getNumArgs() == 1)
  450. // Prefix operator
  451. return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
  452. else
  453. // Postfix operator
  454. return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
  455. } else if (Kind == OO_Arrow) {
  456. return getArg(0)->getSourceRange();
  457. } else if (Kind == OO_Call) {
  458. return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
  459. } else if (Kind == OO_Subscript) {
  460. return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
  461. } else if (getNumArgs() == 1) {
  462. return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
  463. } else if (getNumArgs() == 2) {
  464. return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
  465. } else {
  466. return getOperatorLoc();
  467. }
  468. }
  469. Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
  470. const Expr *Callee = getCallee()->IgnoreParens();
  471. if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
  472. return MemExpr->getBase();
  473. if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
  474. if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
  475. return BO->getLHS();
  476. // FIXME: Will eventually need to cope with member pointers.
  477. return nullptr;
  478. }
  479. CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
  480. if (const MemberExpr *MemExpr =
  481. dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
  482. return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
  483. // FIXME: Will eventually need to cope with member pointers.
  484. return nullptr;
  485. }
  486. CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
  487. Expr* ThisArg = getImplicitObjectArgument();
  488. if (!ThisArg)
  489. return nullptr;
  490. if (ThisArg->getType()->isAnyPointerType())
  491. return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
  492. return ThisArg->getType()->getAsCXXRecordDecl();
  493. }
  494. //===----------------------------------------------------------------------===//
  495. // Named casts
  496. //===----------------------------------------------------------------------===//
  497. /// getCastName - Get the name of the C++ cast being used, e.g.,
  498. /// "static_cast", "dynamic_cast", "reinterpret_cast", or
  499. /// "const_cast". The returned pointer must not be freed.
  500. const char *CXXNamedCastExpr::getCastName() const {
  501. switch (getStmtClass()) {
  502. case CXXStaticCastExprClass: return "static_cast";
  503. case CXXDynamicCastExprClass: return "dynamic_cast";
  504. case CXXReinterpretCastExprClass: return "reinterpret_cast";
  505. case CXXConstCastExprClass: return "const_cast";
  506. default: return "<invalid cast>";
  507. }
  508. }
  509. CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
  510. ExprValueKind VK,
  511. CastKind K, Expr *Op,
  512. const CXXCastPath *BasePath,
  513. TypeSourceInfo *WrittenTy,
  514. SourceLocation L,
  515. SourceLocation RParenLoc,
  516. SourceRange AngleBrackets) {
  517. unsigned PathSize = (BasePath ? BasePath->size() : 0);
  518. void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
  519. + PathSize * sizeof(CXXBaseSpecifier*));
  520. CXXStaticCastExpr *E =
  521. new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
  522. RParenLoc, AngleBrackets);
  523. if (PathSize) E->setCastPath(*BasePath);
  524. return E;
  525. }
  526. CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
  527. unsigned PathSize) {
  528. void *Buffer =
  529. C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
  530. return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
  531. }
  532. CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
  533. ExprValueKind VK,
  534. CastKind K, Expr *Op,
  535. const CXXCastPath *BasePath,
  536. TypeSourceInfo *WrittenTy,
  537. SourceLocation L,
  538. SourceLocation RParenLoc,
  539. SourceRange AngleBrackets) {
  540. unsigned PathSize = (BasePath ? BasePath->size() : 0);
  541. void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
  542. + PathSize * sizeof(CXXBaseSpecifier*));
  543. CXXDynamicCastExpr *E =
  544. new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
  545. RParenLoc, AngleBrackets);
  546. if (PathSize) E->setCastPath(*BasePath);
  547. return E;
  548. }
  549. CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
  550. unsigned PathSize) {
  551. void *Buffer =
  552. C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
  553. return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
  554. }
  555. /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
  556. /// to always be null. For example:
  557. ///
  558. /// struct A { };
  559. /// struct B final : A { };
  560. /// struct C { };
  561. ///
  562. /// C *f(B* b) { return dynamic_cast<C*>(b); }
  563. bool CXXDynamicCastExpr::isAlwaysNull() const
  564. {
  565. QualType SrcType = getSubExpr()->getType();
  566. QualType DestType = getType();
  567. if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
  568. SrcType = SrcPTy->getPointeeType();
  569. DestType = DestType->castAs<PointerType>()->getPointeeType();
  570. }
  571. if (DestType->isVoidType())
  572. return false;
  573. const CXXRecordDecl *SrcRD =
  574. cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
  575. if (!SrcRD->hasAttr<FinalAttr>())
  576. return false;
  577. const CXXRecordDecl *DestRD =
  578. cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
  579. return !DestRD->isDerivedFrom(SrcRD);
  580. }
  581. CXXReinterpretCastExpr *
  582. CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
  583. ExprValueKind VK, CastKind K, Expr *Op,
  584. const CXXCastPath *BasePath,
  585. TypeSourceInfo *WrittenTy, SourceLocation L,
  586. SourceLocation RParenLoc,
  587. SourceRange AngleBrackets) {
  588. unsigned PathSize = (BasePath ? BasePath->size() : 0);
  589. void *Buffer =
  590. C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
  591. CXXReinterpretCastExpr *E =
  592. new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
  593. RParenLoc, AngleBrackets);
  594. if (PathSize) E->setCastPath(*BasePath);
  595. return E;
  596. }
  597. CXXReinterpretCastExpr *
  598. CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
  599. void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
  600. + PathSize * sizeof(CXXBaseSpecifier*));
  601. return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
  602. }
  603. CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
  604. ExprValueKind VK, Expr *Op,
  605. TypeSourceInfo *WrittenTy,
  606. SourceLocation L,
  607. SourceLocation RParenLoc,
  608. SourceRange AngleBrackets) {
  609. return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
  610. }
  611. CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
  612. return new (C) CXXConstCastExpr(EmptyShell());
  613. }
  614. CXXFunctionalCastExpr *
  615. CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
  616. TypeSourceInfo *Written, CastKind K, Expr *Op,
  617. const CXXCastPath *BasePath,
  618. SourceLocation L, SourceLocation R) {
  619. unsigned PathSize = (BasePath ? BasePath->size() : 0);
  620. void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
  621. + PathSize * sizeof(CXXBaseSpecifier*));
  622. CXXFunctionalCastExpr *E =
  623. new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
  624. if (PathSize) E->setCastPath(*BasePath);
  625. return E;
  626. }
  627. CXXFunctionalCastExpr *
  628. CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
  629. void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
  630. + PathSize * sizeof(CXXBaseSpecifier*));
  631. return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
  632. }
  633. SourceLocation CXXFunctionalCastExpr::getLocStart() const {
  634. return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
  635. }
  636. SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
  637. return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
  638. }
  639. UserDefinedLiteral::LiteralOperatorKind
  640. UserDefinedLiteral::getLiteralOperatorKind() const {
  641. if (getNumArgs() == 0)
  642. return LOK_Template;
  643. if (getNumArgs() == 2)
  644. return LOK_String;
  645. assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
  646. QualType ParamTy =
  647. cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
  648. if (ParamTy->isPointerType())
  649. return LOK_Raw;
  650. if (ParamTy->isAnyCharacterType())
  651. return LOK_Character;
  652. if (ParamTy->isIntegerType())
  653. return LOK_Integer;
  654. if (ParamTy->isFloatingType())
  655. return LOK_Floating;
  656. llvm_unreachable("unknown kind of literal operator");
  657. }
  658. Expr *UserDefinedLiteral::getCookedLiteral() {
  659. #ifndef NDEBUG
  660. LiteralOperatorKind LOK = getLiteralOperatorKind();
  661. assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
  662. #endif
  663. return getArg(0);
  664. }
  665. const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
  666. return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
  667. }
  668. CXXDefaultArgExpr *
  669. CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
  670. ParmVarDecl *Param, Expr *SubExpr) {
  671. void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
  672. return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
  673. SubExpr);
  674. }
  675. CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
  676. FieldDecl *Field, QualType T)
  677. : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
  678. T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
  679. ? VK_XValue
  680. : VK_RValue,
  681. /*FIXME*/ OK_Ordinary, false, false, false, false),
  682. Field(Field), Loc(Loc) {
  683. assert(Field->hasInClassInitializer());
  684. }
  685. CXXTemporary *CXXTemporary::Create(const ASTContext &C,
  686. const CXXDestructorDecl *Destructor) {
  687. return new (C) CXXTemporary(Destructor);
  688. }
  689. CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
  690. CXXTemporary *Temp,
  691. Expr* SubExpr) {
  692. assert((SubExpr->getType()->isRecordType() ||
  693. SubExpr->getType()->isArrayType()) &&
  694. "Expression bound to a temporary must have record or array type!");
  695. return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
  696. }
  697. CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
  698. CXXConstructorDecl *Cons,
  699. TypeSourceInfo *Type,
  700. ArrayRef<Expr*> Args,
  701. SourceRange ParenOrBraceRange,
  702. bool HadMultipleCandidates,
  703. bool ListInitialization,
  704. bool StdInitListInitialization,
  705. bool ZeroInitialization)
  706. : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
  707. Type->getType().getNonReferenceType(),
  708. Type->getTypeLoc().getBeginLoc(),
  709. Cons, false, Args,
  710. HadMultipleCandidates,
  711. ListInitialization,
  712. StdInitListInitialization,
  713. ZeroInitialization,
  714. CXXConstructExpr::CK_Complete, ParenOrBraceRange),
  715. Type(Type) {
  716. }
  717. SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
  718. return Type->getTypeLoc().getBeginLoc();
  719. }
  720. SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
  721. SourceLocation Loc = getParenOrBraceRange().getEnd();
  722. if (Loc.isInvalid() && getNumArgs())
  723. Loc = getArg(getNumArgs()-1)->getLocEnd();
  724. return Loc;
  725. }
  726. CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
  727. SourceLocation Loc,
  728. CXXConstructorDecl *D, bool Elidable,
  729. ArrayRef<Expr*> Args,
  730. bool HadMultipleCandidates,
  731. bool ListInitialization,
  732. bool StdInitListInitialization,
  733. bool ZeroInitialization,
  734. ConstructionKind ConstructKind,
  735. SourceRange ParenOrBraceRange) {
  736. return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
  737. Elidable, Args,
  738. HadMultipleCandidates, ListInitialization,
  739. StdInitListInitialization,
  740. ZeroInitialization, ConstructKind,
  741. ParenOrBraceRange);
  742. }
  743. CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
  744. QualType T, SourceLocation Loc,
  745. CXXConstructorDecl *D, bool elidable,
  746. ArrayRef<Expr*> args,
  747. bool HadMultipleCandidates,
  748. bool ListInitialization,
  749. bool StdInitListInitialization,
  750. bool ZeroInitialization,
  751. ConstructionKind ConstructKind,
  752. SourceRange ParenOrBraceRange)
  753. : Expr(SC, T, VK_RValue, OK_Ordinary,
  754. T->isDependentType(), T->isDependentType(),
  755. T->isInstantiationDependentType(),
  756. T->containsUnexpandedParameterPack()),
  757. Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
  758. NumArgs(args.size()),
  759. Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
  760. ListInitialization(ListInitialization),
  761. StdInitListInitialization(StdInitListInitialization),
  762. ZeroInitialization(ZeroInitialization),
  763. ConstructKind(ConstructKind), Args(nullptr)
  764. {
  765. if (NumArgs) {
  766. Args = new (C) Stmt*[args.size()];
  767. for (unsigned i = 0; i != args.size(); ++i) {
  768. assert(args[i] && "NULL argument in CXXConstructExpr");
  769. if (args[i]->isValueDependent())
  770. ExprBits.ValueDependent = true;
  771. if (args[i]->isInstantiationDependent())
  772. ExprBits.InstantiationDependent = true;
  773. if (args[i]->containsUnexpandedParameterPack())
  774. ExprBits.ContainsUnexpandedParameterPack = true;
  775. Args[i] = args[i];
  776. }
  777. }
  778. }
  779. LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
  780. LambdaCaptureKind Kind, VarDecl *Var,
  781. SourceLocation EllipsisLoc)
  782. : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
  783. {
  784. unsigned Bits = 0;
  785. if (Implicit)
  786. Bits |= Capture_Implicit;
  787. switch (Kind) {
  788. case LCK_This:
  789. assert(!Var && "'this' capture cannot have a variable!");
  790. break;
  791. case LCK_ByCopy:
  792. Bits |= Capture_ByCopy;
  793. // Fall through
  794. case LCK_ByRef:
  795. assert(Var && "capture must have a variable!");
  796. break;
  797. case LCK_VLAType:
  798. assert(!Var && "VLA type capture cannot have a variable!");
  799. Bits |= Capture_ByCopy;
  800. break;
  801. }
  802. DeclAndBits.setInt(Bits);
  803. }
  804. LambdaCaptureKind LambdaCapture::getCaptureKind() const {
  805. Decl *D = DeclAndBits.getPointer();
  806. bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
  807. if (!D)
  808. return CapByCopy ? LCK_VLAType : LCK_This;
  809. return CapByCopy ? LCK_ByCopy : LCK_ByRef;
  810. }
  811. LambdaExpr::LambdaExpr(QualType T,
  812. SourceRange IntroducerRange,
  813. LambdaCaptureDefault CaptureDefault,
  814. SourceLocation CaptureDefaultLoc,
  815. ArrayRef<Capture> Captures,
  816. bool ExplicitParams,
  817. bool ExplicitResultType,
  818. ArrayRef<Expr *> CaptureInits,
  819. ArrayRef<VarDecl *> ArrayIndexVars,
  820. ArrayRef<unsigned> ArrayIndexStarts,
  821. SourceLocation ClosingBrace,
  822. bool ContainsUnexpandedParameterPack)
  823. : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
  824. T->isDependentType(), T->isDependentType(), T->isDependentType(),
  825. ContainsUnexpandedParameterPack),
  826. IntroducerRange(IntroducerRange),
  827. CaptureDefaultLoc(CaptureDefaultLoc),
  828. NumCaptures(Captures.size()),
  829. CaptureDefault(CaptureDefault),
  830. ExplicitParams(ExplicitParams),
  831. ExplicitResultType(ExplicitResultType),
  832. ClosingBrace(ClosingBrace)
  833. {
  834. assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
  835. CXXRecordDecl *Class = getLambdaClass();
  836. CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
  837. // FIXME: Propagate "has unexpanded parameter pack" bit.
  838. // Copy captures.
  839. const ASTContext &Context = Class->getASTContext();
  840. Data.NumCaptures = NumCaptures;
  841. Data.NumExplicitCaptures = 0;
  842. Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
  843. Capture *ToCapture = Data.Captures;
  844. for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
  845. if (Captures[I].isExplicit())
  846. ++Data.NumExplicitCaptures;
  847. *ToCapture++ = Captures[I];
  848. }
  849. // Copy initialization expressions for the non-static data members.
  850. Stmt **Stored = getStoredStmts();
  851. for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
  852. *Stored++ = CaptureInits[I];
  853. // Copy the body of the lambda.
  854. *Stored++ = getCallOperator()->getBody();
  855. // Copy the array index variables, if any.
  856. HasArrayIndexVars = !ArrayIndexVars.empty();
  857. if (HasArrayIndexVars) {
  858. assert(ArrayIndexStarts.size() == NumCaptures);
  859. memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
  860. sizeof(VarDecl *) * ArrayIndexVars.size());
  861. memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
  862. sizeof(unsigned) * Captures.size());
  863. getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
  864. }
  865. }
  866. LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
  867. CXXRecordDecl *Class,
  868. SourceRange IntroducerRange,
  869. LambdaCaptureDefault CaptureDefault,
  870. SourceLocation CaptureDefaultLoc,
  871. ArrayRef<Capture> Captures,
  872. bool ExplicitParams,
  873. bool ExplicitResultType,
  874. ArrayRef<Expr *> CaptureInits,
  875. ArrayRef<VarDecl *> ArrayIndexVars,
  876. ArrayRef<unsigned> ArrayIndexStarts,
  877. SourceLocation ClosingBrace,
  878. bool ContainsUnexpandedParameterPack) {
  879. // Determine the type of the expression (i.e., the type of the
  880. // function object we're creating).
  881. QualType T = Context.getTypeDeclType(Class);
  882. unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
  883. if (!ArrayIndexVars.empty()) {
  884. Size += sizeof(unsigned) * (Captures.size() + 1);
  885. // Realign for following VarDecl array.
  886. Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
  887. Size += sizeof(VarDecl *) * ArrayIndexVars.size();
  888. }
  889. void *Mem = Context.Allocate(Size);
  890. return new (Mem) LambdaExpr(T, IntroducerRange,
  891. CaptureDefault, CaptureDefaultLoc, Captures,
  892. ExplicitParams, ExplicitResultType,
  893. CaptureInits, ArrayIndexVars, ArrayIndexStarts,
  894. ClosingBrace, ContainsUnexpandedParameterPack);
  895. }
  896. LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
  897. unsigned NumCaptures,
  898. unsigned NumArrayIndexVars) {
  899. unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
  900. if (NumArrayIndexVars)
  901. Size += sizeof(VarDecl) * NumArrayIndexVars
  902. + sizeof(unsigned) * (NumCaptures + 1);
  903. void *Mem = C.Allocate(Size);
  904. return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
  905. }
  906. bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
  907. return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
  908. (getCallOperator() == C->getCapturedVar()->getDeclContext()));
  909. }
  910. LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
  911. return getLambdaClass()->getLambdaData().Captures;
  912. }
  913. LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
  914. return capture_begin() + NumCaptures;
  915. }
  916. LambdaExpr::capture_range LambdaExpr::captures() const {
  917. return capture_range(capture_begin(), capture_end());
  918. }
  919. LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
  920. return capture_begin();
  921. }
  922. LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
  923. struct CXXRecordDecl::LambdaDefinitionData &Data
  924. = getLambdaClass()->getLambdaData();
  925. return Data.Captures + Data.NumExplicitCaptures;
  926. }
  927. LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
  928. return capture_range(explicit_capture_begin(), explicit_capture_end());
  929. }
  930. LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
  931. return explicit_capture_end();
  932. }
  933. LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
  934. return capture_end();
  935. }
  936. LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
  937. return capture_range(implicit_capture_begin(), implicit_capture_end());
  938. }
  939. ArrayRef<VarDecl *>
  940. LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
  941. assert(HasArrayIndexVars && "No array index-var data?");
  942. unsigned Index = Iter - capture_init_begin();
  943. assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
  944. "Capture index out-of-range");
  945. VarDecl **IndexVars = getArrayIndexVars();
  946. unsigned *IndexStarts = getArrayIndexStarts();
  947. return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
  948. IndexVars + IndexStarts[Index + 1]);
  949. }
  950. CXXRecordDecl *LambdaExpr::getLambdaClass() const {
  951. return getType()->getAsCXXRecordDecl();
  952. }
  953. CXXMethodDecl *LambdaExpr::getCallOperator() const {
  954. CXXRecordDecl *Record = getLambdaClass();
  955. return Record->getLambdaCallOperator();
  956. }
  957. TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
  958. CXXRecordDecl *Record = getLambdaClass();
  959. return Record->getGenericLambdaTemplateParameterList();
  960. }
  961. CompoundStmt *LambdaExpr::getBody() const {
  962. if (!getStoredStmts()[NumCaptures])
  963. getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
  964. return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
  965. }
  966. bool LambdaExpr::isMutable() const {
  967. return !getCallOperator()->isConst();
  968. }
  969. ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
  970. ArrayRef<CleanupObject> objects)
  971. : Expr(ExprWithCleanupsClass, subexpr->getType(),
  972. subexpr->getValueKind(), subexpr->getObjectKind(),
  973. subexpr->isTypeDependent(), subexpr->isValueDependent(),
  974. subexpr->isInstantiationDependent(),
  975. subexpr->containsUnexpandedParameterPack()),
  976. SubExpr(subexpr) {
  977. ExprWithCleanupsBits.NumObjects = objects.size();
  978. for (unsigned i = 0, e = objects.size(); i != e; ++i)
  979. getObjectsBuffer()[i] = objects[i];
  980. }
  981. ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
  982. ArrayRef<CleanupObject> objects) {
  983. size_t size = sizeof(ExprWithCleanups)
  984. + objects.size() * sizeof(CleanupObject);
  985. void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
  986. return new (buffer) ExprWithCleanups(subexpr, objects);
  987. }
  988. ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
  989. : Expr(ExprWithCleanupsClass, empty) {
  990. ExprWithCleanupsBits.NumObjects = numObjects;
  991. }
  992. ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
  993. EmptyShell empty,
  994. unsigned numObjects) {
  995. size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
  996. void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
  997. return new (buffer) ExprWithCleanups(empty, numObjects);
  998. }
  999. CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
  1000. SourceLocation LParenLoc,
  1001. ArrayRef<Expr*> Args,
  1002. SourceLocation RParenLoc)
  1003. : Expr(CXXUnresolvedConstructExprClass,
  1004. Type->getType().getNonReferenceType(),
  1005. (Type->getType()->isLValueReferenceType() ? VK_LValue
  1006. :Type->getType()->isRValueReferenceType()? VK_XValue
  1007. :VK_RValue),
  1008. OK_Ordinary,
  1009. Type->getType()->isDependentType(), true, true,
  1010. Type->getType()->containsUnexpandedParameterPack()),
  1011. Type(Type),
  1012. LParenLoc(LParenLoc),
  1013. RParenLoc(RParenLoc),
  1014. NumArgs(Args.size()) {
  1015. Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
  1016. for (unsigned I = 0; I != Args.size(); ++I) {
  1017. if (Args[I]->containsUnexpandedParameterPack())
  1018. ExprBits.ContainsUnexpandedParameterPack = true;
  1019. StoredArgs[I] = Args[I];
  1020. }
  1021. }
  1022. CXXUnresolvedConstructExpr *
  1023. CXXUnresolvedConstructExpr::Create(const ASTContext &C,
  1024. TypeSourceInfo *Type,
  1025. SourceLocation LParenLoc,
  1026. ArrayRef<Expr*> Args,
  1027. SourceLocation RParenLoc) {
  1028. void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
  1029. sizeof(Expr *) * Args.size());
  1030. return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
  1031. }
  1032. CXXUnresolvedConstructExpr *
  1033. CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
  1034. Stmt::EmptyShell Empty;
  1035. void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
  1036. sizeof(Expr *) * NumArgs);
  1037. return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
  1038. }
  1039. SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
  1040. return Type->getTypeLoc().getBeginLoc();
  1041. }
  1042. CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
  1043. Expr *Base, QualType BaseType,
  1044. bool IsArrow,
  1045. SourceLocation OperatorLoc,
  1046. NestedNameSpecifierLoc QualifierLoc,
  1047. SourceLocation TemplateKWLoc,
  1048. NamedDecl *FirstQualifierFoundInScope,
  1049. DeclarationNameInfo MemberNameInfo,
  1050. const TemplateArgumentListInfo *TemplateArgs)
  1051. : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
  1052. VK_LValue, OK_Ordinary, true, true, true,
  1053. ((Base && Base->containsUnexpandedParameterPack()) ||
  1054. (QualifierLoc &&
  1055. QualifierLoc.getNestedNameSpecifier()
  1056. ->containsUnexpandedParameterPack()) ||
  1057. MemberNameInfo.containsUnexpandedParameterPack())),
  1058. Base(Base), BaseType(BaseType), IsArrow(IsArrow),
  1059. HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
  1060. TemplateKWLoc.isValid()),
  1061. OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
  1062. FirstQualifierFoundInScope(FirstQualifierFoundInScope),
  1063. MemberNameInfo(MemberNameInfo) {
  1064. if (TemplateArgs) {
  1065. bool Dependent = true;
  1066. bool InstantiationDependent = true;
  1067. bool ContainsUnexpandedParameterPack = false;
  1068. getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
  1069. Dependent,
  1070. InstantiationDependent,
  1071. ContainsUnexpandedParameterPack);
  1072. if (ContainsUnexpandedParameterPack)
  1073. ExprBits.ContainsUnexpandedParameterPack = true;
  1074. } else if (TemplateKWLoc.isValid()) {
  1075. getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
  1076. }
  1077. }
  1078. CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
  1079. Expr *Base, QualType BaseType,
  1080. bool IsArrow,
  1081. SourceLocation OperatorLoc,
  1082. NestedNameSpecifierLoc QualifierLoc,
  1083. NamedDecl *FirstQualifierFoundInScope,
  1084. DeclarationNameInfo MemberNameInfo)
  1085. : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
  1086. VK_LValue, OK_Ordinary, true, true, true,
  1087. ((Base && Base->containsUnexpandedParameterPack()) ||
  1088. (QualifierLoc &&
  1089. QualifierLoc.getNestedNameSpecifier()->
  1090. containsUnexpandedParameterPack()) ||
  1091. MemberNameInfo.containsUnexpandedParameterPack())),
  1092. Base(Base), BaseType(BaseType), IsArrow(IsArrow),
  1093. HasTemplateKWAndArgsInfo(false),
  1094. OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
  1095. FirstQualifierFoundInScope(FirstQualifierFoundInScope),
  1096. MemberNameInfo(MemberNameInfo) { }
  1097. CXXDependentScopeMemberExpr *
  1098. CXXDependentScopeMemberExpr::Create(const ASTContext &C,
  1099. Expr *Base, QualType BaseType, bool IsArrow,
  1100. SourceLocation OperatorLoc,
  1101. NestedNameSpecifierLoc QualifierLoc,
  1102. SourceLocation TemplateKWLoc,
  1103. NamedDecl *FirstQualifierFoundInScope,
  1104. DeclarationNameInfo MemberNameInfo,
  1105. const TemplateArgumentListInfo *TemplateArgs) {
  1106. if (!TemplateArgs && !TemplateKWLoc.isValid())
  1107. return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
  1108. IsArrow, OperatorLoc,
  1109. QualifierLoc,
  1110. FirstQualifierFoundInScope,
  1111. MemberNameInfo);
  1112. unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
  1113. std::size_t size = sizeof(CXXDependentScopeMemberExpr)
  1114. + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
  1115. void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
  1116. return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
  1117. IsArrow, OperatorLoc,
  1118. QualifierLoc,
  1119. TemplateKWLoc,
  1120. FirstQualifierFoundInScope,
  1121. MemberNameInfo, TemplateArgs);
  1122. }
  1123. CXXDependentScopeMemberExpr *
  1124. CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
  1125. bool HasTemplateKWAndArgsInfo,
  1126. unsigned NumTemplateArgs) {
  1127. if (!HasTemplateKWAndArgsInfo)
  1128. return new (C) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
  1129. 0, SourceLocation(),
  1130. NestedNameSpecifierLoc(),
  1131. nullptr, DeclarationNameInfo());
  1132. std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
  1133. ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
  1134. void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
  1135. CXXDependentScopeMemberExpr *E
  1136. = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
  1137. 0, SourceLocation(),
  1138. NestedNameSpecifierLoc(),
  1139. SourceLocation(), nullptr,
  1140. DeclarationNameInfo(), nullptr);
  1141. E->HasTemplateKWAndArgsInfo = true;
  1142. return E;
  1143. }
  1144. bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
  1145. if (!Base)
  1146. return true;
  1147. return cast<Expr>(Base)->isImplicitCXXThis();
  1148. }
  1149. static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
  1150. UnresolvedSetIterator end) {
  1151. do {
  1152. NamedDecl *decl = *begin;
  1153. if (isa<UnresolvedUsingValueDecl>(decl))
  1154. return false;
  1155. // Unresolved member expressions should only contain methods and
  1156. // method templates.
  1157. if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
  1158. ->isStatic())
  1159. return false;
  1160. } while (++begin != end);
  1161. return true;
  1162. }
  1163. UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
  1164. bool HasUnresolvedUsing,
  1165. Expr *Base, QualType BaseType,
  1166. bool IsArrow,
  1167. SourceLocation OperatorLoc,
  1168. NestedNameSpecifierLoc QualifierLoc,
  1169. SourceLocation TemplateKWLoc,
  1170. const DeclarationNameInfo &MemberNameInfo,
  1171. const TemplateArgumentListInfo *TemplateArgs,
  1172. UnresolvedSetIterator Begin,
  1173. UnresolvedSetIterator End)
  1174. : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
  1175. MemberNameInfo, TemplateArgs, Begin, End,
  1176. // Dependent
  1177. ((Base && Base->isTypeDependent()) ||
  1178. BaseType->isDependentType()),
  1179. ((Base && Base->isInstantiationDependent()) ||
  1180. BaseType->isInstantiationDependentType()),
  1181. // Contains unexpanded parameter pack
  1182. ((Base && Base->containsUnexpandedParameterPack()) ||
  1183. BaseType->containsUnexpandedParameterPack())),
  1184. IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
  1185. Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
  1186. // Check whether all of the members are non-static member functions,
  1187. // and if so, mark give this bound-member type instead of overload type.
  1188. if (hasOnlyNonStaticMemberFunctions(Begin, End))
  1189. setType(C.BoundMemberTy);
  1190. }
  1191. bool UnresolvedMemberExpr::isImplicitAccess() const {
  1192. if (!Base)
  1193. return true;
  1194. return cast<Expr>(Base)->isImplicitCXXThis();
  1195. }
  1196. UnresolvedMemberExpr *
  1197. UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
  1198. Expr *Base, QualType BaseType, bool IsArrow,
  1199. SourceLocation OperatorLoc,
  1200. NestedNameSpecifierLoc QualifierLoc,
  1201. SourceLocation TemplateKWLoc,
  1202. const DeclarationNameInfo &MemberNameInfo,
  1203. const TemplateArgumentListInfo *TemplateArgs,
  1204. UnresolvedSetIterator Begin,
  1205. UnresolvedSetIterator End) {
  1206. std::size_t size = sizeof(UnresolvedMemberExpr);
  1207. if (TemplateArgs)
  1208. size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
  1209. else if (TemplateKWLoc.isValid())
  1210. size += ASTTemplateKWAndArgsInfo::sizeFor(0);
  1211. void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
  1212. return new (Mem) UnresolvedMemberExpr(C,
  1213. HasUnresolvedUsing, Base, BaseType,
  1214. IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
  1215. MemberNameInfo, TemplateArgs, Begin, End);
  1216. }
  1217. UnresolvedMemberExpr *
  1218. UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
  1219. bool HasTemplateKWAndArgsInfo,
  1220. unsigned NumTemplateArgs) {
  1221. std::size_t size = sizeof(UnresolvedMemberExpr);
  1222. if (HasTemplateKWAndArgsInfo)
  1223. size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
  1224. void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
  1225. UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
  1226. E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
  1227. return E;
  1228. }
  1229. CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
  1230. // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
  1231. // If there was a nested name specifier, it names the naming class.
  1232. // It can't be dependent: after all, we were actually able to do the
  1233. // lookup.
  1234. CXXRecordDecl *Record = nullptr;
  1235. auto *NNS = getQualifier();
  1236. if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
  1237. const Type *T = getQualifier()->getAsType();
  1238. assert(T && "qualifier in member expression does not name type");
  1239. Record = T->getAsCXXRecordDecl();
  1240. assert(Record && "qualifier in member expression does not name record");
  1241. }
  1242. // Otherwise the naming class must have been the base class.
  1243. else {
  1244. QualType BaseType = getBaseType().getNonReferenceType();
  1245. if (isArrow()) {
  1246. const PointerType *PT = BaseType->getAs<PointerType>();
  1247. assert(PT && "base of arrow member access is not pointer");
  1248. BaseType = PT->getPointeeType();
  1249. }
  1250. Record = BaseType->getAsCXXRecordDecl();
  1251. assert(Record && "base of member expression does not name record");
  1252. }
  1253. return Record;
  1254. }
  1255. SubstNonTypeTemplateParmPackExpr::
  1256. SubstNonTypeTemplateParmPackExpr(QualType T,
  1257. NonTypeTemplateParmDecl *Param,
  1258. SourceLocation NameLoc,
  1259. const TemplateArgument &ArgPack)
  1260. : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
  1261. true, true, true, true),
  1262. Param(Param), Arguments(ArgPack.pack_begin()),
  1263. NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
  1264. TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
  1265. return TemplateArgument(Arguments, NumArguments);
  1266. }
  1267. FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
  1268. SourceLocation NameLoc,
  1269. unsigned NumParams,
  1270. Decl * const *Params)
  1271. : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
  1272. true, true, true, true),
  1273. ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
  1274. if (Params)
  1275. std::uninitialized_copy(Params, Params + NumParams,
  1276. reinterpret_cast<Decl**>(this+1));
  1277. }
  1278. FunctionParmPackExpr *
  1279. FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
  1280. ParmVarDecl *ParamPack, SourceLocation NameLoc,
  1281. ArrayRef<Decl *> Params) {
  1282. return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
  1283. sizeof(ParmVarDecl*) * Params.size()))
  1284. FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
  1285. }
  1286. FunctionParmPackExpr *
  1287. FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
  1288. unsigned NumParams) {
  1289. return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
  1290. sizeof(ParmVarDecl*) * NumParams))
  1291. FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
  1292. }
  1293. void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
  1294. unsigned ManglingNumber) {
  1295. // We only need extra state if we have to remember more than just the Stmt.
  1296. if (!ExtendedBy)
  1297. return;
  1298. // We may need to allocate extra storage for the mangling number and the
  1299. // extended-by ValueDecl.
  1300. if (!State.is<ExtraState *>()) {
  1301. auto ES = new (ExtendedBy->getASTContext()) ExtraState;
  1302. ES->Temporary = State.get<Stmt *>();
  1303. State = ES;
  1304. }
  1305. auto ES = State.get<ExtraState *>();
  1306. ES->ExtendingDecl = ExtendedBy;
  1307. ES->ManglingNumber = ManglingNumber;
  1308. }
  1309. TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
  1310. ArrayRef<TypeSourceInfo *> Args,
  1311. SourceLocation RParenLoc,
  1312. bool Value)
  1313. : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
  1314. /*TypeDependent=*/false,
  1315. /*ValueDependent=*/false,
  1316. /*InstantiationDependent=*/false,
  1317. /*ContainsUnexpandedParameterPack=*/false),
  1318. Loc(Loc), RParenLoc(RParenLoc)
  1319. {
  1320. TypeTraitExprBits.Kind = Kind;
  1321. TypeTraitExprBits.Value = Value;
  1322. TypeTraitExprBits.NumArgs = Args.size();
  1323. TypeSourceInfo **ToArgs = getTypeSourceInfos();
  1324. for (unsigned I = 0, N = Args.size(); I != N; ++I) {
  1325. if (Args[I]->getType()->isDependentType())
  1326. setValueDependent(true);
  1327. if (Args[I]->getType()->isInstantiationDependentType())
  1328. setInstantiationDependent(true);
  1329. if (Args[I]->getType()->containsUnexpandedParameterPack())
  1330. setContainsUnexpandedParameterPack(true);
  1331. ToArgs[I] = Args[I];
  1332. }
  1333. }
  1334. TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
  1335. SourceLocation Loc,
  1336. TypeTrait Kind,
  1337. ArrayRef<TypeSourceInfo *> Args,
  1338. SourceLocation RParenLoc,
  1339. bool Value) {
  1340. unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
  1341. void *Mem = C.Allocate(Size);
  1342. return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
  1343. }
  1344. TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
  1345. unsigned NumArgs) {
  1346. unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
  1347. void *Mem = C.Allocate(Size);
  1348. return new (Mem) TypeTraitExpr(EmptyShell());
  1349. }
  1350. void ArrayTypeTraitExpr::anchor() { }