1 //===- ExprCXX.h - Classes for representing expressions ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Defines the clang::Expr interface and subclasses for C++ expressions.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_EXPRCXX_H
16 #define LLVM_CLANG_AST_EXPRCXX_H
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclarationName.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/NestedNameSpecifier.h"
24 #include "clang/AST/OperationKinds.h"
25 #include "clang/AST/Stmt.h"
26 #include "clang/AST/TemplateBase.h"
27 #include "clang/AST/Type.h"
28 #include "clang/AST/UnresolvedSet.h"
29 #include "clang/Basic/ExceptionSpecificationType.h"
30 #include "clang/Basic/ExpressionTraits.h"
31 #include "clang/Basic/LLVM.h"
32 #include "clang/Basic/Lambda.h"
33 #include "clang/Basic/LangOptions.h"
34 #include "clang/Basic/OperatorKinds.h"
35 #include "clang/Basic/SourceLocation.h"
36 #include "clang/Basic/Specifiers.h"
37 #include "clang/Basic/TypeTraits.h"
38 #include "llvm/ADT/ArrayRef.h"
39 #include "llvm/ADT/None.h"
40 #include "llvm/ADT/Optional.h"
41 #include "llvm/ADT/PointerUnion.h"
42 #include "llvm/ADT/StringRef.h"
43 #include "llvm/ADT/iterator_range.h"
44 #include "llvm/Support/Casting.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/TrailingObjects.h"
58 class NonTypeTemplateParmDecl;
59 class TemplateParameterList;
61 //===--------------------------------------------------------------------===//
63 //===--------------------------------------------------------------------===//
65 /// A call to an overloaded operator written using operator
68 /// Represents a call to an overloaded operator written using operator
69 /// syntax, e.g., "x + y" or "*p". While semantically equivalent to a
70 /// normal call, this AST node provides better information about the
71 /// syntactic representation of the call.
73 /// In a C++ template, this expression node kind will be used whenever
74 /// any of the arguments are type-dependent. In this case, the
75 /// function itself will be a (possibly empty) set of functions and
76 /// function templates that were found by name lookup at template
78 class CXXOperatorCallExpr : public CallExpr {
79 /// The overloaded operator.
80 OverloadedOperatorKind Operator;
84 // Only meaningful for floating point types.
87 SourceRange getSourceRangeImpl() const LLVM_READONLY;
90 friend class ASTStmtReader;
91 friend class ASTStmtWriter;
93 CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn,
94 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
95 SourceLocation operatorloc, FPOptions FPFeatures)
96 : CallExpr(C, CXXOperatorCallExprClass, fn, args, t, VK, operatorloc),
97 Operator(Op), FPFeatures(FPFeatures) {
98 Range = getSourceRangeImpl();
101 explicit CXXOperatorCallExpr(ASTContext& C, EmptyShell Empty)
102 : CallExpr(C, CXXOperatorCallExprClass, Empty) {}
104 /// Returns the kind of overloaded operator that this
105 /// expression refers to.
106 OverloadedOperatorKind getOperator() const { return Operator; }
108 static bool isAssignmentOp(OverloadedOperatorKind Opc) {
109 return Opc == OO_Equal || Opc == OO_StarEqual ||
110 Opc == OO_SlashEqual || Opc == OO_PercentEqual ||
111 Opc == OO_PlusEqual || Opc == OO_MinusEqual ||
112 Opc == OO_LessLessEqual || Opc == OO_GreaterGreaterEqual ||
113 Opc == OO_AmpEqual || Opc == OO_CaretEqual ||
116 bool isAssignmentOp() const { return isAssignmentOp(getOperator()); }
118 /// Is this written as an infix binary operator?
119 bool isInfixBinaryOp() const;
121 /// Returns the location of the operator symbol in the expression.
123 /// When \c getOperator()==OO_Call, this is the location of the right
124 /// parentheses; when \c getOperator()==OO_Subscript, this is the location
125 /// of the right bracket.
126 SourceLocation getOperatorLoc() const { return getRParenLoc(); }
128 SourceLocation getExprLoc() const LLVM_READONLY {
129 return (Operator < OO_Plus || Operator >= OO_Arrow ||
130 Operator == OO_PlusPlus || Operator == OO_MinusMinus)
135 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
136 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
137 SourceRange getSourceRange() const { return Range; }
139 static bool classof(const Stmt *T) {
140 return T->getStmtClass() == CXXOperatorCallExprClass;
143 // Set the FP contractability status of this operator. Only meaningful for
144 // operations on floating point types.
145 void setFPFeatures(FPOptions F) { FPFeatures = F; }
147 FPOptions getFPFeatures() const { return FPFeatures; }
149 // Get the FP contractability status of this operator. Only meaningful for
150 // operations on floating point types.
151 bool isFPContractableWithinStatement() const {
152 return FPFeatures.allowFPContractWithinStatement();
156 /// Represents a call to a member function that
157 /// may be written either with member call syntax (e.g., "obj.func()"
158 /// or "objptr->func()") or with normal function-call syntax
159 /// ("func()") within a member function that ends up calling a member
160 /// function. The callee in either case is a MemberExpr that contains
161 /// both the object argument and the member function, while the
162 /// arguments are the arguments within the parentheses (not including
163 /// the object argument).
164 class CXXMemberCallExpr : public CallExpr {
166 CXXMemberCallExpr(ASTContext &C, Expr *fn, ArrayRef<Expr*> args,
167 QualType t, ExprValueKind VK, SourceLocation RP)
168 : CallExpr(C, CXXMemberCallExprClass, fn, args, t, VK, RP) {}
170 CXXMemberCallExpr(ASTContext &C, EmptyShell Empty)
171 : CallExpr(C, CXXMemberCallExprClass, Empty) {}
173 /// Retrieves the implicit object argument for the member call.
175 /// For example, in "x.f(5)", this returns the sub-expression "x".
176 Expr *getImplicitObjectArgument() const;
178 /// Retrieves the declaration of the called method.
179 CXXMethodDecl *getMethodDecl() const;
181 /// Retrieves the CXXRecordDecl for the underlying type of
182 /// the implicit object argument.
184 /// Note that this is may not be the same declaration as that of the class
185 /// context of the CXXMethodDecl which this function is calling.
186 /// FIXME: Returns 0 for member pointer call exprs.
187 CXXRecordDecl *getRecordDecl() const;
189 SourceLocation getExprLoc() const LLVM_READONLY {
190 SourceLocation CLoc = getCallee()->getExprLoc();
194 return getLocStart();
197 static bool classof(const Stmt *T) {
198 return T->getStmtClass() == CXXMemberCallExprClass;
202 /// Represents a call to a CUDA kernel function.
203 class CUDAKernelCallExpr : public CallExpr {
205 enum { CONFIG, END_PREARG };
208 CUDAKernelCallExpr(ASTContext &C, Expr *fn, CallExpr *Config,
209 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
211 : CallExpr(C, CUDAKernelCallExprClass, fn, Config, args, t, VK, RP) {}
213 CUDAKernelCallExpr(ASTContext &C, EmptyShell Empty)
214 : CallExpr(C, CUDAKernelCallExprClass, END_PREARG, Empty) {}
216 const CallExpr *getConfig() const {
217 return cast_or_null<CallExpr>(getPreArg(CONFIG));
219 CallExpr *getConfig() { return cast_or_null<CallExpr>(getPreArg(CONFIG)); }
221 /// Sets the kernel configuration expression.
223 /// Note that this method cannot be called if config has already been set to a
225 void setConfig(CallExpr *E) {
226 assert(!getConfig() &&
227 "Cannot call setConfig if config is not null");
228 setPreArg(CONFIG, E);
229 setInstantiationDependent(isInstantiationDependent() ||
230 E->isInstantiationDependent());
231 setContainsUnexpandedParameterPack(containsUnexpandedParameterPack() ||
232 E->containsUnexpandedParameterPack());
235 static bool classof(const Stmt *T) {
236 return T->getStmtClass() == CUDAKernelCallExprClass;
240 /// Abstract class common to all of the C++ "named"/"keyword" casts.
242 /// This abstract class is inherited by all of the classes
243 /// representing "named" casts: CXXStaticCastExpr for \c static_cast,
244 /// CXXDynamicCastExpr for \c dynamic_cast, CXXReinterpretCastExpr for
245 /// reinterpret_cast, and CXXConstCastExpr for \c const_cast.
246 class CXXNamedCastExpr : public ExplicitCastExpr {
248 // the location of the casting op
251 // the location of the right parenthesis
252 SourceLocation RParenLoc;
255 SourceRange AngleBrackets;
258 friend class ASTStmtReader;
260 CXXNamedCastExpr(StmtClass SC, QualType ty, ExprValueKind VK,
261 CastKind kind, Expr *op, unsigned PathSize,
262 TypeSourceInfo *writtenTy, SourceLocation l,
263 SourceLocation RParenLoc,
264 SourceRange AngleBrackets)
265 : ExplicitCastExpr(SC, ty, VK, kind, op, PathSize, writtenTy), Loc(l),
266 RParenLoc(RParenLoc), AngleBrackets(AngleBrackets) {}
268 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize)
269 : ExplicitCastExpr(SC, Shell, PathSize) {}
272 const char *getCastName() const;
274 /// Retrieve the location of the cast operator keyword, e.g.,
276 SourceLocation getOperatorLoc() const { return Loc; }
278 /// Retrieve the location of the closing parenthesis.
279 SourceLocation getRParenLoc() const { return RParenLoc; }
281 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
282 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
283 SourceRange getAngleBrackets() const LLVM_READONLY { return AngleBrackets; }
285 static bool classof(const Stmt *T) {
286 switch (T->getStmtClass()) {
287 case CXXStaticCastExprClass:
288 case CXXDynamicCastExprClass:
289 case CXXReinterpretCastExprClass:
290 case CXXConstCastExprClass:
298 /// A C++ \c static_cast expression (C++ [expr.static.cast]).
300 /// This expression node represents a C++ static cast, e.g.,
301 /// \c static_cast<int>(1.0).
302 class CXXStaticCastExpr final
303 : public CXXNamedCastExpr,
304 private llvm::TrailingObjects<CXXStaticCastExpr, CXXBaseSpecifier *> {
305 CXXStaticCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op,
306 unsigned pathSize, TypeSourceInfo *writtenTy,
307 SourceLocation l, SourceLocation RParenLoc,
308 SourceRange AngleBrackets)
309 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize,
310 writtenTy, l, RParenLoc, AngleBrackets) {}
312 explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize)
313 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize) {}
316 friend class CastExpr;
317 friend TrailingObjects;
319 static CXXStaticCastExpr *Create(const ASTContext &Context, QualType T,
320 ExprValueKind VK, CastKind K, Expr *Op,
321 const CXXCastPath *Path,
322 TypeSourceInfo *Written, SourceLocation L,
323 SourceLocation RParenLoc,
324 SourceRange AngleBrackets);
325 static CXXStaticCastExpr *CreateEmpty(const ASTContext &Context,
328 static bool classof(const Stmt *T) {
329 return T->getStmtClass() == CXXStaticCastExprClass;
333 /// A C++ @c dynamic_cast expression (C++ [expr.dynamic.cast]).
335 /// This expression node represents a dynamic cast, e.g.,
336 /// \c dynamic_cast<Derived*>(BasePtr). Such a cast may perform a run-time
337 /// check to determine how to perform the type conversion.
338 class CXXDynamicCastExpr final
339 : public CXXNamedCastExpr,
340 private llvm::TrailingObjects<CXXDynamicCastExpr, CXXBaseSpecifier *> {
341 CXXDynamicCastExpr(QualType ty, ExprValueKind VK, CastKind kind,
342 Expr *op, unsigned pathSize, TypeSourceInfo *writtenTy,
343 SourceLocation l, SourceLocation RParenLoc,
344 SourceRange AngleBrackets)
345 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, VK, kind, op, pathSize,
346 writtenTy, l, RParenLoc, AngleBrackets) {}
348 explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize)
349 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize) {}
352 friend class CastExpr;
353 friend TrailingObjects;
355 static CXXDynamicCastExpr *Create(const ASTContext &Context, QualType T,
356 ExprValueKind VK, CastKind Kind, Expr *Op,
357 const CXXCastPath *Path,
358 TypeSourceInfo *Written, SourceLocation L,
359 SourceLocation RParenLoc,
360 SourceRange AngleBrackets);
362 static CXXDynamicCastExpr *CreateEmpty(const ASTContext &Context,
365 bool isAlwaysNull() const;
367 static bool classof(const Stmt *T) {
368 return T->getStmtClass() == CXXDynamicCastExprClass;
372 /// A C++ @c reinterpret_cast expression (C++ [expr.reinterpret.cast]).
374 /// This expression node represents a reinterpret cast, e.g.,
375 /// @c reinterpret_cast<int>(VoidPtr).
377 /// A reinterpret_cast provides a differently-typed view of a value but
378 /// (in Clang, as in most C++ implementations) performs no actual work at
380 class CXXReinterpretCastExpr final
381 : public CXXNamedCastExpr,
382 private llvm::TrailingObjects<CXXReinterpretCastExpr,
383 CXXBaseSpecifier *> {
384 CXXReinterpretCastExpr(QualType ty, ExprValueKind vk, CastKind kind,
385 Expr *op, unsigned pathSize,
386 TypeSourceInfo *writtenTy, SourceLocation l,
387 SourceLocation RParenLoc,
388 SourceRange AngleBrackets)
389 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, vk, kind, op,
390 pathSize, writtenTy, l, RParenLoc, AngleBrackets) {}
392 CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize)
393 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize) {}
396 friend class CastExpr;
397 friend TrailingObjects;
399 static CXXReinterpretCastExpr *Create(const ASTContext &Context, QualType T,
400 ExprValueKind VK, CastKind Kind,
401 Expr *Op, const CXXCastPath *Path,
402 TypeSourceInfo *WrittenTy, SourceLocation L,
403 SourceLocation RParenLoc,
404 SourceRange AngleBrackets);
405 static CXXReinterpretCastExpr *CreateEmpty(const ASTContext &Context,
408 static bool classof(const Stmt *T) {
409 return T->getStmtClass() == CXXReinterpretCastExprClass;
413 /// A C++ \c const_cast expression (C++ [expr.const.cast]).
415 /// This expression node represents a const cast, e.g.,
416 /// \c const_cast<char*>(PtrToConstChar).
418 /// A const_cast can remove type qualifiers but does not change the underlying
420 class CXXConstCastExpr final
421 : public CXXNamedCastExpr,
422 private llvm::TrailingObjects<CXXConstCastExpr, CXXBaseSpecifier *> {
423 CXXConstCastExpr(QualType ty, ExprValueKind VK, Expr *op,
424 TypeSourceInfo *writtenTy, SourceLocation l,
425 SourceLocation RParenLoc, SourceRange AngleBrackets)
426 : CXXNamedCastExpr(CXXConstCastExprClass, ty, VK, CK_NoOp, op,
427 0, writtenTy, l, RParenLoc, AngleBrackets) {}
429 explicit CXXConstCastExpr(EmptyShell Empty)
430 : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0) {}
433 friend class CastExpr;
434 friend TrailingObjects;
436 static CXXConstCastExpr *Create(const ASTContext &Context, QualType T,
437 ExprValueKind VK, Expr *Op,
438 TypeSourceInfo *WrittenTy, SourceLocation L,
439 SourceLocation RParenLoc,
440 SourceRange AngleBrackets);
441 static CXXConstCastExpr *CreateEmpty(const ASTContext &Context);
443 static bool classof(const Stmt *T) {
444 return T->getStmtClass() == CXXConstCastExprClass;
448 /// A call to a literal operator (C++11 [over.literal])
449 /// written as a user-defined literal (C++11 [lit.ext]).
451 /// Represents a user-defined literal, e.g. "foo"_bar or 1.23_xyz. While this
452 /// is semantically equivalent to a normal call, this AST node provides better
453 /// information about the syntactic representation of the literal.
455 /// Since literal operators are never found by ADL and can only be declared at
456 /// namespace scope, a user-defined literal is never dependent.
457 class UserDefinedLiteral : public CallExpr {
458 /// The location of a ud-suffix within the literal.
459 SourceLocation UDSuffixLoc;
462 friend class ASTStmtReader;
463 friend class ASTStmtWriter;
465 UserDefinedLiteral(const ASTContext &C, Expr *Fn, ArrayRef<Expr*> Args,
466 QualType T, ExprValueKind VK, SourceLocation LitEndLoc,
467 SourceLocation SuffixLoc)
468 : CallExpr(C, UserDefinedLiteralClass, Fn, Args, T, VK, LitEndLoc),
469 UDSuffixLoc(SuffixLoc) {}
471 explicit UserDefinedLiteral(const ASTContext &C, EmptyShell Empty)
472 : CallExpr(C, UserDefinedLiteralClass, Empty) {}
474 /// The kind of literal operator which is invoked.
475 enum LiteralOperatorKind {
476 /// Raw form: operator "" X (const char *)
479 /// Raw form: operator "" X<cs...> ()
482 /// operator "" X (unsigned long long)
485 /// operator "" X (long double)
488 /// operator "" X (const CharT *, size_t)
491 /// operator "" X (CharT)
495 /// Returns the kind of literal operator invocation
496 /// which this expression represents.
497 LiteralOperatorKind getLiteralOperatorKind() const;
499 /// If this is not a raw user-defined literal, get the
500 /// underlying cooked literal (representing the literal with the suffix
502 Expr *getCookedLiteral();
503 const Expr *getCookedLiteral() const {
504 return const_cast<UserDefinedLiteral*>(this)->getCookedLiteral();
507 SourceLocation getLocStart() const {
508 if (getLiteralOperatorKind() == LOK_Template)
509 return getRParenLoc();
510 return getArg(0)->getLocStart();
513 SourceLocation getLocEnd() const { return getRParenLoc(); }
515 /// Returns the location of a ud-suffix in the expression.
517 /// For a string literal, there may be multiple identical suffixes. This
518 /// returns the first.
519 SourceLocation getUDSuffixLoc() const { return UDSuffixLoc; }
521 /// Returns the ud-suffix specified for this literal.
522 const IdentifierInfo *getUDSuffix() const;
524 static bool classof(const Stmt *S) {
525 return S->getStmtClass() == UserDefinedLiteralClass;
529 /// A boolean literal, per ([C++ lex.bool] Boolean literals).
530 class CXXBoolLiteralExpr : public Expr {
535 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l)
536 : Expr(CXXBoolLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
538 Value(val), Loc(l) {}
540 explicit CXXBoolLiteralExpr(EmptyShell Empty)
541 : Expr(CXXBoolLiteralExprClass, Empty) {}
543 bool getValue() const { return Value; }
544 void setValue(bool V) { Value = V; }
546 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
547 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
549 SourceLocation getLocation() const { return Loc; }
550 void setLocation(SourceLocation L) { Loc = L; }
552 static bool classof(const Stmt *T) {
553 return T->getStmtClass() == CXXBoolLiteralExprClass;
557 child_range children() {
558 return child_range(child_iterator(), child_iterator());
562 /// The null pointer literal (C++11 [lex.nullptr])
564 /// Introduced in C++11, the only literal of type \c nullptr_t is \c nullptr.
565 class CXXNullPtrLiteralExpr : public Expr {
569 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l)
570 : Expr(CXXNullPtrLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false,
571 false, false, false),
574 explicit CXXNullPtrLiteralExpr(EmptyShell Empty)
575 : Expr(CXXNullPtrLiteralExprClass, Empty) {}
577 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
578 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
580 SourceLocation getLocation() const { return Loc; }
581 void setLocation(SourceLocation L) { Loc = L; }
583 static bool classof(const Stmt *T) {
584 return T->getStmtClass() == CXXNullPtrLiteralExprClass;
587 child_range children() {
588 return child_range(child_iterator(), child_iterator());
592 /// Implicit construction of a std::initializer_list<T> object from an
593 /// array temporary within list-initialization (C++11 [dcl.init.list]p5).
594 class CXXStdInitializerListExpr : public Expr {
595 Stmt *SubExpr = nullptr;
597 CXXStdInitializerListExpr(EmptyShell Empty)
598 : Expr(CXXStdInitializerListExprClass, Empty) {}
601 friend class ASTReader;
602 friend class ASTStmtReader;
604 CXXStdInitializerListExpr(QualType Ty, Expr *SubExpr)
605 : Expr(CXXStdInitializerListExprClass, Ty, VK_RValue, OK_Ordinary,
606 Ty->isDependentType(), SubExpr->isValueDependent(),
607 SubExpr->isInstantiationDependent(),
608 SubExpr->containsUnexpandedParameterPack()),
611 Expr *getSubExpr() { return static_cast<Expr*>(SubExpr); }
612 const Expr *getSubExpr() const { return static_cast<const Expr*>(SubExpr); }
614 SourceLocation getLocStart() const LLVM_READONLY {
615 return SubExpr->getLocStart();
618 SourceLocation getLocEnd() const LLVM_READONLY {
619 return SubExpr->getLocEnd();
622 /// Retrieve the source range of the expression.
623 SourceRange getSourceRange() const LLVM_READONLY {
624 return SubExpr->getSourceRange();
627 static bool classof(const Stmt *S) {
628 return S->getStmtClass() == CXXStdInitializerListExprClass;
631 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
634 /// A C++ \c typeid expression (C++ [expr.typeid]), which gets
635 /// the \c type_info that corresponds to the supplied type, or the (possibly
636 /// dynamic) type of the supplied expression.
638 /// This represents code like \c typeid(int) or \c typeid(*objPtr)
639 class CXXTypeidExpr : public Expr {
641 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
645 CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R)
646 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary,
647 // typeid is never type-dependent (C++ [temp.dep.expr]p4)
649 // typeid is value-dependent if the type or expression are
651 Operand->getType()->isDependentType(),
652 Operand->getType()->isInstantiationDependentType(),
653 Operand->getType()->containsUnexpandedParameterPack()),
654 Operand(Operand), Range(R) {}
656 CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R)
657 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary,
658 // typeid is never type-dependent (C++ [temp.dep.expr]p4)
660 // typeid is value-dependent if the type or expression are
662 Operand->isTypeDependent() || Operand->isValueDependent(),
663 Operand->isInstantiationDependent(),
664 Operand->containsUnexpandedParameterPack()),
665 Operand(Operand), Range(R) {}
667 CXXTypeidExpr(EmptyShell Empty, bool isExpr)
668 : Expr(CXXTypeidExprClass, Empty) {
670 Operand = (Expr*)nullptr;
672 Operand = (TypeSourceInfo*)nullptr;
675 /// Determine whether this typeid has a type operand which is potentially
676 /// evaluated, per C++11 [expr.typeid]p3.
677 bool isPotentiallyEvaluated() const;
679 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
681 /// Retrieves the type operand of this typeid() expression after
682 /// various required adjustments (removing reference types, cv-qualifiers).
683 QualType getTypeOperand(ASTContext &Context) const;
685 /// Retrieve source information for the type operand.
686 TypeSourceInfo *getTypeOperandSourceInfo() const {
687 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
688 return Operand.get<TypeSourceInfo *>();
691 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) {
692 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
696 Expr *getExprOperand() const {
697 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
698 return static_cast<Expr*>(Operand.get<Stmt *>());
701 void setExprOperand(Expr *E) {
702 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
706 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
707 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
708 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
709 void setSourceRange(SourceRange R) { Range = R; }
711 static bool classof(const Stmt *T) {
712 return T->getStmtClass() == CXXTypeidExprClass;
716 child_range children() {
718 return child_range(child_iterator(), child_iterator());
719 auto **begin = reinterpret_cast<Stmt **>(&Operand);
720 return child_range(begin, begin + 1);
724 /// A member reference to an MSPropertyDecl.
726 /// This expression always has pseudo-object type, and therefore it is
727 /// typically not encountered in a fully-typechecked expression except
728 /// within the syntactic form of a PseudoObjectExpr.
729 class MSPropertyRefExpr : public Expr {
731 MSPropertyDecl *TheDecl;
732 SourceLocation MemberLoc;
734 NestedNameSpecifierLoc QualifierLoc;
737 friend class ASTStmtReader;
739 MSPropertyRefExpr(Expr *baseExpr, MSPropertyDecl *decl, bool isArrow,
740 QualType ty, ExprValueKind VK,
741 NestedNameSpecifierLoc qualifierLoc,
742 SourceLocation nameLoc)
743 : Expr(MSPropertyRefExprClass, ty, VK, OK_Ordinary,
744 /*type-dependent*/ false, baseExpr->isValueDependent(),
745 baseExpr->isInstantiationDependent(),
746 baseExpr->containsUnexpandedParameterPack()),
747 BaseExpr(baseExpr), TheDecl(decl),
748 MemberLoc(nameLoc), IsArrow(isArrow),
749 QualifierLoc(qualifierLoc) {}
751 MSPropertyRefExpr(EmptyShell Empty) : Expr(MSPropertyRefExprClass, Empty) {}
753 SourceRange getSourceRange() const LLVM_READONLY {
754 return SourceRange(getLocStart(), getLocEnd());
757 bool isImplicitAccess() const {
758 return getBaseExpr() && getBaseExpr()->isImplicitCXXThis();
761 SourceLocation getLocStart() const {
762 if (!isImplicitAccess())
763 return BaseExpr->getLocStart();
764 else if (QualifierLoc)
765 return QualifierLoc.getBeginLoc();
770 SourceLocation getLocEnd() const { return getMemberLoc(); }
772 child_range children() {
773 return child_range((Stmt**)&BaseExpr, (Stmt**)&BaseExpr + 1);
776 static bool classof(const Stmt *T) {
777 return T->getStmtClass() == MSPropertyRefExprClass;
780 Expr *getBaseExpr() const { return BaseExpr; }
781 MSPropertyDecl *getPropertyDecl() const { return TheDecl; }
782 bool isArrow() const { return IsArrow; }
783 SourceLocation getMemberLoc() const { return MemberLoc; }
784 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
787 /// MS property subscript expression.
788 /// MSVC supports 'property' attribute and allows to apply it to the
789 /// declaration of an empty array in a class or structure definition.
792 /// __declspec(property(get=GetX, put=PutX)) int x[];
794 /// The above statement indicates that x[] can be used with one or more array
795 /// indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), and
796 /// p->x[a][b] = i will be turned into p->PutX(a, b, i).
797 /// This is a syntactic pseudo-object expression.
798 class MSPropertySubscriptExpr : public Expr {
799 friend class ASTStmtReader;
801 enum { BASE_EXPR, IDX_EXPR, NUM_SUBEXPRS = 2 };
803 Stmt *SubExprs[NUM_SUBEXPRS];
804 SourceLocation RBracketLoc;
806 void setBase(Expr *Base) { SubExprs[BASE_EXPR] = Base; }
807 void setIdx(Expr *Idx) { SubExprs[IDX_EXPR] = Idx; }
810 MSPropertySubscriptExpr(Expr *Base, Expr *Idx, QualType Ty, ExprValueKind VK,
811 ExprObjectKind OK, SourceLocation RBracketLoc)
812 : Expr(MSPropertySubscriptExprClass, Ty, VK, OK, Idx->isTypeDependent(),
813 Idx->isValueDependent(), Idx->isInstantiationDependent(),
814 Idx->containsUnexpandedParameterPack()),
815 RBracketLoc(RBracketLoc) {
816 SubExprs[BASE_EXPR] = Base;
817 SubExprs[IDX_EXPR] = Idx;
820 /// Create an empty array subscript expression.
821 explicit MSPropertySubscriptExpr(EmptyShell Shell)
822 : Expr(MSPropertySubscriptExprClass, Shell) {}
824 Expr *getBase() { return cast<Expr>(SubExprs[BASE_EXPR]); }
825 const Expr *getBase() const { return cast<Expr>(SubExprs[BASE_EXPR]); }
827 Expr *getIdx() { return cast<Expr>(SubExprs[IDX_EXPR]); }
828 const Expr *getIdx() const { return cast<Expr>(SubExprs[IDX_EXPR]); }
830 SourceLocation getLocStart() const LLVM_READONLY {
831 return getBase()->getLocStart();
834 SourceLocation getLocEnd() const LLVM_READONLY { return RBracketLoc; }
836 SourceLocation getRBracketLoc() const { return RBracketLoc; }
837 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
839 SourceLocation getExprLoc() const LLVM_READONLY {
840 return getBase()->getExprLoc();
843 static bool classof(const Stmt *T) {
844 return T->getStmtClass() == MSPropertySubscriptExprClass;
848 child_range children() {
849 return child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
853 /// A Microsoft C++ @c __uuidof expression, which gets
854 /// the _GUID that corresponds to the supplied type or expression.
856 /// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr)
857 class CXXUuidofExpr : public Expr {
859 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
864 CXXUuidofExpr(QualType Ty, TypeSourceInfo *Operand, StringRef UuidStr,
866 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, false,
867 Operand->getType()->isDependentType(),
868 Operand->getType()->isInstantiationDependentType(),
869 Operand->getType()->containsUnexpandedParameterPack()),
870 Operand(Operand), UuidStr(UuidStr), Range(R) {}
872 CXXUuidofExpr(QualType Ty, Expr *Operand, StringRef UuidStr, SourceRange R)
873 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, false,
874 Operand->isTypeDependent(), Operand->isInstantiationDependent(),
875 Operand->containsUnexpandedParameterPack()),
876 Operand(Operand), UuidStr(UuidStr), Range(R) {}
878 CXXUuidofExpr(EmptyShell Empty, bool isExpr)
879 : Expr(CXXUuidofExprClass, Empty) {
881 Operand = (Expr*)nullptr;
883 Operand = (TypeSourceInfo*)nullptr;
886 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
888 /// Retrieves the type operand of this __uuidof() expression after
889 /// various required adjustments (removing reference types, cv-qualifiers).
890 QualType getTypeOperand(ASTContext &Context) const;
892 /// Retrieve source information for the type operand.
893 TypeSourceInfo *getTypeOperandSourceInfo() const {
894 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
895 return Operand.get<TypeSourceInfo *>();
898 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) {
899 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
903 Expr *getExprOperand() const {
904 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
905 return static_cast<Expr*>(Operand.get<Stmt *>());
908 void setExprOperand(Expr *E) {
909 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
913 void setUuidStr(StringRef US) { UuidStr = US; }
914 StringRef getUuidStr() const { return UuidStr; }
916 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
917 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
918 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
919 void setSourceRange(SourceRange R) { Range = R; }
921 static bool classof(const Stmt *T) {
922 return T->getStmtClass() == CXXUuidofExprClass;
926 child_range children() {
928 return child_range(child_iterator(), child_iterator());
929 auto **begin = reinterpret_cast<Stmt **>(&Operand);
930 return child_range(begin, begin + 1);
934 /// Represents the \c this expression in C++.
936 /// This is a pointer to the object on which the current member function is
937 /// executing (C++ [expr.prim]p3). Example:
943 /// void test() { this->bar(); }
946 class CXXThisExpr : public Expr {
951 CXXThisExpr(SourceLocation L, QualType Type, bool isImplicit)
952 : Expr(CXXThisExprClass, Type, VK_RValue, OK_Ordinary,
953 // 'this' is type-dependent if the class type of the enclosing
954 // member function is dependent (C++ [temp.dep.expr]p2)
955 Type->isDependentType(), Type->isDependentType(),
956 Type->isInstantiationDependentType(),
957 /*ContainsUnexpandedParameterPack=*/false),
958 Loc(L), Implicit(isImplicit) {}
960 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {}
962 SourceLocation getLocation() const { return Loc; }
963 void setLocation(SourceLocation L) { Loc = L; }
965 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
966 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
968 bool isImplicit() const { return Implicit; }
969 void setImplicit(bool I) { Implicit = I; }
971 static bool classof(const Stmt *T) {
972 return T->getStmtClass() == CXXThisExprClass;
976 child_range children() {
977 return child_range(child_iterator(), child_iterator());
981 /// A C++ throw-expression (C++ [except.throw]).
983 /// This handles 'throw' (for re-throwing the current exception) and
984 /// 'throw' assignment-expression. When assignment-expression isn't
985 /// present, Op will be null.
986 class CXXThrowExpr : public Expr {
987 friend class ASTStmtReader;
990 SourceLocation ThrowLoc;
992 /// Whether the thrown variable (if any) is in scope.
993 unsigned IsThrownVariableInScope : 1;
996 // \p Ty is the void type which is used as the result type of the
997 // expression. The \p l is the location of the throw keyword. \p expr
998 // can by null, if the optional expression to throw isn't present.
999 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l,
1000 bool IsThrownVariableInScope)
1001 : Expr(CXXThrowExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
1002 expr && expr->isInstantiationDependent(),
1003 expr && expr->containsUnexpandedParameterPack()),
1004 Op(expr), ThrowLoc(l),
1005 IsThrownVariableInScope(IsThrownVariableInScope) {}
1006 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {}
1008 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); }
1009 Expr *getSubExpr() { return cast_or_null<Expr>(Op); }
1011 SourceLocation getThrowLoc() const { return ThrowLoc; }
1013 /// Determines whether the variable thrown by this expression (if any!)
1014 /// is within the innermost try block.
1016 /// This information is required to determine whether the NRVO can apply to
1018 bool isThrownVariableInScope() const { return IsThrownVariableInScope; }
1020 SourceLocation getLocStart() const LLVM_READONLY { return ThrowLoc; }
1022 SourceLocation getLocEnd() const LLVM_READONLY {
1025 return getSubExpr()->getLocEnd();
1028 static bool classof(const Stmt *T) {
1029 return T->getStmtClass() == CXXThrowExprClass;
1033 child_range children() {
1034 return child_range(&Op, Op ? &Op+1 : &Op);
1038 /// A default argument (C++ [dcl.fct.default]).
1040 /// This wraps up a function call argument that was created from the
1041 /// corresponding parameter's default argument, when the call did not
1042 /// explicitly supply arguments for all of the parameters.
1043 class CXXDefaultArgExpr final : public Expr {
1044 /// The parameter whose default is being used.
1047 /// The location where the default argument expression was used.
1050 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param)
1052 param->hasUnparsedDefaultArg()
1053 ? param->getType().getNonReferenceType()
1054 : param->getDefaultArg()->getType(),
1055 param->getDefaultArg()->getValueKind(),
1056 param->getDefaultArg()->getObjectKind(), false, false, false,
1058 Param(param), Loc(Loc) {}
1061 friend class ASTStmtReader;
1062 friend class ASTStmtWriter;
1064 CXXDefaultArgExpr(EmptyShell Empty) : Expr(CXXDefaultArgExprClass, Empty) {}
1066 // \p Param is the parameter whose default argument is used by this
1068 static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc,
1069 ParmVarDecl *Param) {
1070 return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param);
1073 // Retrieve the parameter that the argument was created from.
1074 const ParmVarDecl *getParam() const { return Param; }
1075 ParmVarDecl *getParam() { return Param; }
1077 // Retrieve the actual argument to the function call.
1078 const Expr *getExpr() const {
1079 return getParam()->getDefaultArg();
1082 return getParam()->getDefaultArg();
1085 /// Retrieve the location where this default argument was actually
1087 SourceLocation getUsedLocation() const { return Loc; }
1089 /// Default argument expressions have no representation in the
1090 /// source, so they have an empty source range.
1091 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1092 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1094 SourceLocation getExprLoc() const LLVM_READONLY { return Loc; }
1096 static bool classof(const Stmt *T) {
1097 return T->getStmtClass() == CXXDefaultArgExprClass;
1101 child_range children() {
1102 return child_range(child_iterator(), child_iterator());
1106 /// A use of a default initializer in a constructor or in aggregate
1109 /// This wraps a use of a C++ default initializer (technically,
1110 /// a brace-or-equal-initializer for a non-static data member) when it
1111 /// is implicitly used in a mem-initializer-list in a constructor
1112 /// (C++11 [class.base.init]p8) or in aggregate initialization
1113 /// (C++1y [dcl.init.aggr]p7).
1114 class CXXDefaultInitExpr : public Expr {
1115 /// The field whose default is being used.
1118 /// The location where the default initializer expression was used.
1121 CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc, FieldDecl *Field,
1124 CXXDefaultInitExpr(EmptyShell Empty) : Expr(CXXDefaultInitExprClass, Empty) {}
1127 friend class ASTReader;
1128 friend class ASTStmtReader;
1130 /// \p Field is the non-static data member whose default initializer is used
1131 /// by this expression.
1132 static CXXDefaultInitExpr *Create(const ASTContext &C, SourceLocation Loc,
1134 return new (C) CXXDefaultInitExpr(C, Loc, Field, Field->getType());
1137 /// Get the field whose initializer will be used.
1138 FieldDecl *getField() { return Field; }
1139 const FieldDecl *getField() const { return Field; }
1141 /// Get the initialization expression that will be used.
1142 const Expr *getExpr() const {
1143 assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1144 return Field->getInClassInitializer();
1147 assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1148 return Field->getInClassInitializer();
1151 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
1152 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
1154 static bool classof(const Stmt *T) {
1155 return T->getStmtClass() == CXXDefaultInitExprClass;
1159 child_range children() {
1160 return child_range(child_iterator(), child_iterator());
1164 /// Represents a C++ temporary.
1165 class CXXTemporary {
1166 /// The destructor that needs to be called.
1167 const CXXDestructorDecl *Destructor;
1169 explicit CXXTemporary(const CXXDestructorDecl *destructor)
1170 : Destructor(destructor) {}
1173 static CXXTemporary *Create(const ASTContext &C,
1174 const CXXDestructorDecl *Destructor);
1176 const CXXDestructorDecl *getDestructor() const { return Destructor; }
1178 void setDestructor(const CXXDestructorDecl *Dtor) {
1183 /// Represents binding an expression to a temporary.
1185 /// This ensures the destructor is called for the temporary. It should only be
1186 /// needed for non-POD, non-trivially destructable class types. For example:
1190 /// S() { } // User defined constructor makes S non-POD.
1191 /// ~S() { } // User defined destructor makes it non-trivial.
1194 /// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr.
1197 class CXXBindTemporaryExpr : public Expr {
1198 CXXTemporary *Temp = nullptr;
1199 Stmt *SubExpr = nullptr;
1201 CXXBindTemporaryExpr(CXXTemporary *temp, Expr* SubExpr)
1202 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(),
1203 VK_RValue, OK_Ordinary, SubExpr->isTypeDependent(),
1204 SubExpr->isValueDependent(),
1205 SubExpr->isInstantiationDependent(),
1206 SubExpr->containsUnexpandedParameterPack()),
1207 Temp(temp), SubExpr(SubExpr) {}
1210 CXXBindTemporaryExpr(EmptyShell Empty)
1211 : Expr(CXXBindTemporaryExprClass, Empty) {}
1213 static CXXBindTemporaryExpr *Create(const ASTContext &C, CXXTemporary *Temp,
1216 CXXTemporary *getTemporary() { return Temp; }
1217 const CXXTemporary *getTemporary() const { return Temp; }
1218 void setTemporary(CXXTemporary *T) { Temp = T; }
1220 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
1221 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
1222 void setSubExpr(Expr *E) { SubExpr = E; }
1224 SourceLocation getLocStart() const LLVM_READONLY {
1225 return SubExpr->getLocStart();
1228 SourceLocation getLocEnd() const LLVM_READONLY { return SubExpr->getLocEnd();}
1230 // Implement isa/cast/dyncast/etc.
1231 static bool classof(const Stmt *T) {
1232 return T->getStmtClass() == CXXBindTemporaryExprClass;
1236 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
1239 /// Represents a call to a C++ constructor.
1240 class CXXConstructExpr : public Expr {
1242 enum ConstructionKind {
1250 CXXConstructorDecl *Constructor = nullptr;
1252 SourceRange ParenOrBraceRange;
1253 unsigned NumArgs : 16;
1254 unsigned Elidable : 1;
1255 unsigned HadMultipleCandidates : 1;
1256 unsigned ListInitialization : 1;
1257 unsigned StdInitListInitialization : 1;
1258 unsigned ZeroInitialization : 1;
1259 unsigned ConstructKind : 2;
1260 Stmt **Args = nullptr;
1262 void setConstructor(CXXConstructorDecl *C) { Constructor = C; }
1265 CXXConstructExpr(const ASTContext &C, StmtClass SC, QualType T,
1267 CXXConstructorDecl *Ctor,
1269 ArrayRef<Expr *> Args,
1270 bool HadMultipleCandidates,
1271 bool ListInitialization,
1272 bool StdInitListInitialization,
1273 bool ZeroInitialization,
1274 ConstructionKind ConstructKind,
1275 SourceRange ParenOrBraceRange);
1277 /// Construct an empty C++ construction expression.
1278 CXXConstructExpr(StmtClass SC, EmptyShell Empty)
1279 : Expr(SC, Empty), NumArgs(0), Elidable(false),
1280 HadMultipleCandidates(false), ListInitialization(false),
1281 ZeroInitialization(false), ConstructKind(0) {}
1284 friend class ASTStmtReader;
1286 /// Construct an empty C++ construction expression.
1287 explicit CXXConstructExpr(EmptyShell Empty)
1288 : CXXConstructExpr(CXXConstructExprClass, Empty) {}
1290 static CXXConstructExpr *Create(const ASTContext &C, QualType T,
1292 CXXConstructorDecl *Ctor,
1294 ArrayRef<Expr *> Args,
1295 bool HadMultipleCandidates,
1296 bool ListInitialization,
1297 bool StdInitListInitialization,
1298 bool ZeroInitialization,
1299 ConstructionKind ConstructKind,
1300 SourceRange ParenOrBraceRange);
1302 /// Get the constructor that this expression will (ultimately) call.
1303 CXXConstructorDecl *getConstructor() const { return Constructor; }
1305 SourceLocation getLocation() const { return Loc; }
1306 void setLocation(SourceLocation Loc) { this->Loc = Loc; }
1308 /// Whether this construction is elidable.
1309 bool isElidable() const { return Elidable; }
1310 void setElidable(bool E) { Elidable = E; }
1312 /// Whether the referred constructor was resolved from
1313 /// an overloaded set having size greater than 1.
1314 bool hadMultipleCandidates() const { return HadMultipleCandidates; }
1315 void setHadMultipleCandidates(bool V) { HadMultipleCandidates = V; }
1317 /// Whether this constructor call was written as list-initialization.
1318 bool isListInitialization() const { return ListInitialization; }
1319 void setListInitialization(bool V) { ListInitialization = V; }
1321 /// Whether this constructor call was written as list-initialization,
1322 /// but was interpreted as forming a std::initializer_list<T> from the list
1323 /// and passing that as a single constructor argument.
1324 /// See C++11 [over.match.list]p1 bullet 1.
1325 bool isStdInitListInitialization() const { return StdInitListInitialization; }
1326 void setStdInitListInitialization(bool V) { StdInitListInitialization = V; }
1328 /// Whether this construction first requires
1329 /// zero-initialization before the initializer is called.
1330 bool requiresZeroInitialization() const { return ZeroInitialization; }
1331 void setRequiresZeroInitialization(bool ZeroInit) {
1332 ZeroInitialization = ZeroInit;
1335 /// Determine whether this constructor is actually constructing
1336 /// a base class (rather than a complete object).
1337 ConstructionKind getConstructionKind() const {
1338 return (ConstructionKind)ConstructKind;
1340 void setConstructionKind(ConstructionKind CK) {
1344 using arg_iterator = ExprIterator;
1345 using const_arg_iterator = ConstExprIterator;
1346 using arg_range = llvm::iterator_range<arg_iterator>;
1347 using arg_const_range = llvm::iterator_range<const_arg_iterator>;
1349 arg_range arguments() { return arg_range(arg_begin(), arg_end()); }
1350 arg_const_range arguments() const {
1351 return arg_const_range(arg_begin(), arg_end());
1354 arg_iterator arg_begin() { return Args; }
1355 arg_iterator arg_end() { return Args + NumArgs; }
1356 const_arg_iterator arg_begin() const { return Args; }
1357 const_arg_iterator arg_end() const { return Args + NumArgs; }
1359 Expr **getArgs() { return reinterpret_cast<Expr **>(Args); }
1360 const Expr *const *getArgs() const {
1361 return const_cast<CXXConstructExpr *>(this)->getArgs();
1363 unsigned getNumArgs() const { return NumArgs; }
1365 /// Return the specified argument.
1366 Expr *getArg(unsigned Arg) {
1367 assert(Arg < NumArgs && "Arg access out of range!");
1368 return cast<Expr>(Args[Arg]);
1370 const Expr *getArg(unsigned Arg) const {
1371 assert(Arg < NumArgs && "Arg access out of range!");
1372 return cast<Expr>(Args[Arg]);
1375 /// Set the specified argument.
1376 void setArg(unsigned Arg, Expr *ArgExpr) {
1377 assert(Arg < NumArgs && "Arg access out of range!");
1378 Args[Arg] = ArgExpr;
1381 SourceLocation getLocStart() const LLVM_READONLY;
1382 SourceLocation getLocEnd() const LLVM_READONLY;
1383 SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
1384 void setParenOrBraceRange(SourceRange Range) { ParenOrBraceRange = Range; }
1386 static bool classof(const Stmt *T) {
1387 return T->getStmtClass() == CXXConstructExprClass ||
1388 T->getStmtClass() == CXXTemporaryObjectExprClass;
1392 child_range children() {
1393 return child_range(&Args[0], &Args[0]+NumArgs);
1397 /// Represents a call to an inherited base class constructor from an
1398 /// inheriting constructor. This call implicitly forwards the arguments from
1399 /// the enclosing context (an inheriting constructor) to the specified inherited
1400 /// base class constructor.
1401 class CXXInheritedCtorInitExpr : public Expr {
1403 CXXConstructorDecl *Constructor = nullptr;
1405 /// The location of the using declaration.
1408 /// Whether this is the construction of a virtual base.
1409 unsigned ConstructsVirtualBase : 1;
1411 /// Whether the constructor is inherited from a virtual base class of the
1412 /// class that we construct.
1413 unsigned InheritedFromVirtualBase : 1;
1416 friend class ASTStmtReader;
1418 /// Construct a C++ inheriting construction expression.
1419 CXXInheritedCtorInitExpr(SourceLocation Loc, QualType T,
1420 CXXConstructorDecl *Ctor, bool ConstructsVirtualBase,
1421 bool InheritedFromVirtualBase)
1422 : Expr(CXXInheritedCtorInitExprClass, T, VK_RValue, OK_Ordinary, false,
1423 false, false, false),
1424 Constructor(Ctor), Loc(Loc),
1425 ConstructsVirtualBase(ConstructsVirtualBase),
1426 InheritedFromVirtualBase(InheritedFromVirtualBase) {
1427 assert(!T->isDependentType());
1430 /// Construct an empty C++ inheriting construction expression.
1431 explicit CXXInheritedCtorInitExpr(EmptyShell Empty)
1432 : Expr(CXXInheritedCtorInitExprClass, Empty),
1433 ConstructsVirtualBase(false), InheritedFromVirtualBase(false) {}
1435 /// Get the constructor that this expression will call.
1436 CXXConstructorDecl *getConstructor() const { return Constructor; }
1438 /// Determine whether this constructor is actually constructing
1439 /// a base class (rather than a complete object).
1440 bool constructsVBase() const { return ConstructsVirtualBase; }
1441 CXXConstructExpr::ConstructionKind getConstructionKind() const {
1442 return ConstructsVirtualBase ? CXXConstructExpr::CK_VirtualBase
1443 : CXXConstructExpr::CK_NonVirtualBase;
1446 /// Determine whether the inherited constructor is inherited from a
1447 /// virtual base of the object we construct. If so, we are not responsible
1448 /// for calling the inherited constructor (the complete object constructor
1449 /// does that), and so we don't need to pass any arguments.
1450 bool inheritedFromVBase() const { return InheritedFromVirtualBase; }
1452 SourceLocation getLocation() const LLVM_READONLY { return Loc; }
1453 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
1454 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
1456 static bool classof(const Stmt *T) {
1457 return T->getStmtClass() == CXXInheritedCtorInitExprClass;
1460 child_range children() {
1461 return child_range(child_iterator(), child_iterator());
1465 /// Represents an explicit C++ type conversion that uses "functional"
1466 /// notation (C++ [expr.type.conv]).
1472 class CXXFunctionalCastExpr final
1473 : public ExplicitCastExpr,
1474 private llvm::TrailingObjects<CXXFunctionalCastExpr, CXXBaseSpecifier *> {
1475 SourceLocation LParenLoc;
1476 SourceLocation RParenLoc;
1478 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK,
1479 TypeSourceInfo *writtenTy,
1480 CastKind kind, Expr *castExpr, unsigned pathSize,
1481 SourceLocation lParenLoc, SourceLocation rParenLoc)
1482 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind,
1483 castExpr, pathSize, writtenTy),
1484 LParenLoc(lParenLoc), RParenLoc(rParenLoc) {}
1486 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize)
1487 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize) {}
1490 friend class CastExpr;
1491 friend TrailingObjects;
1493 static CXXFunctionalCastExpr *Create(const ASTContext &Context, QualType T,
1495 TypeSourceInfo *Written,
1496 CastKind Kind, Expr *Op,
1497 const CXXCastPath *Path,
1498 SourceLocation LPLoc,
1499 SourceLocation RPLoc);
1500 static CXXFunctionalCastExpr *CreateEmpty(const ASTContext &Context,
1503 SourceLocation getLParenLoc() const { return LParenLoc; }
1504 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1505 SourceLocation getRParenLoc() const { return RParenLoc; }
1506 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1508 /// Determine whether this expression models list-initialization.
1509 bool isListInitialization() const { return LParenLoc.isInvalid(); }
1511 SourceLocation getLocStart() const LLVM_READONLY;
1512 SourceLocation getLocEnd() const LLVM_READONLY;
1514 static bool classof(const Stmt *T) {
1515 return T->getStmtClass() == CXXFunctionalCastExprClass;
1519 /// Represents a C++ functional cast expression that builds a
1520 /// temporary object.
1522 /// This expression type represents a C++ "functional" cast
1523 /// (C++[expr.type.conv]) with N != 1 arguments that invokes a
1524 /// constructor to build a temporary object. With N == 1 arguments the
1525 /// functional cast expression will be represented by CXXFunctionalCastExpr.
1528 /// struct X { X(int, float); }
1531 /// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
1534 class CXXTemporaryObjectExpr : public CXXConstructExpr {
1535 TypeSourceInfo *Type = nullptr;
1538 friend class ASTStmtReader;
1540 CXXTemporaryObjectExpr(const ASTContext &C,
1541 CXXConstructorDecl *Cons,
1543 TypeSourceInfo *TSI,
1544 ArrayRef<Expr *> Args,
1545 SourceRange ParenOrBraceRange,
1546 bool HadMultipleCandidates,
1547 bool ListInitialization,
1548 bool StdInitListInitialization,
1549 bool ZeroInitialization);
1550 explicit CXXTemporaryObjectExpr(EmptyShell Empty)
1551 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty) {}
1553 TypeSourceInfo *getTypeSourceInfo() const { return Type; }
1555 SourceLocation getLocStart() const LLVM_READONLY;
1556 SourceLocation getLocEnd() const LLVM_READONLY;
1558 static bool classof(const Stmt *T) {
1559 return T->getStmtClass() == CXXTemporaryObjectExprClass;
1563 /// A C++ lambda expression, which produces a function object
1564 /// (of unspecified type) that can be invoked later.
1568 /// void low_pass_filter(std::vector<double> &values, double cutoff) {
1569 /// values.erase(std::remove_if(values.begin(), values.end(),
1570 /// [=](double value) { return value > cutoff; });
1574 /// C++11 lambda expressions can capture local variables, either by copying
1575 /// the values of those local variables at the time the function
1576 /// object is constructed (not when it is called!) or by holding a
1577 /// reference to the local variable. These captures can occur either
1578 /// implicitly or can be written explicitly between the square
1579 /// brackets ([...]) that start the lambda expression.
1581 /// C++1y introduces a new form of "capture" called an init-capture that
1582 /// includes an initializing expression (rather than capturing a variable),
1583 /// and which can never occur implicitly.
1584 class LambdaExpr final : public Expr,
1585 private llvm::TrailingObjects<LambdaExpr, Stmt *> {
1586 /// The source range that covers the lambda introducer ([...]).
1587 SourceRange IntroducerRange;
1589 /// The source location of this lambda's capture-default ('=' or '&').
1590 SourceLocation CaptureDefaultLoc;
1592 /// The number of captures.
1593 unsigned NumCaptures : 16;
1595 /// The default capture kind, which is a value of type
1596 /// LambdaCaptureDefault.
1597 unsigned CaptureDefault : 2;
1599 /// Whether this lambda had an explicit parameter list vs. an
1600 /// implicit (and empty) parameter list.
1601 unsigned ExplicitParams : 1;
1603 /// Whether this lambda had the result type explicitly specified.
1604 unsigned ExplicitResultType : 1;
1606 /// The location of the closing brace ('}') that completes
1609 /// The location of the brace is also available by looking up the
1610 /// function call operator in the lambda class. However, it is
1611 /// stored here to improve the performance of getSourceRange(), and
1612 /// to avoid having to deserialize the function call operator from a
1613 /// module file just to determine the source range.
1614 SourceLocation ClosingBrace;
1616 /// Construct a lambda expression.
1617 LambdaExpr(QualType T, SourceRange IntroducerRange,
1618 LambdaCaptureDefault CaptureDefault,
1619 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
1620 bool ExplicitParams, bool ExplicitResultType,
1621 ArrayRef<Expr *> CaptureInits, SourceLocation ClosingBrace,
1622 bool ContainsUnexpandedParameterPack);
1624 /// Construct an empty lambda expression.
1625 LambdaExpr(EmptyShell Empty, unsigned NumCaptures)
1626 : Expr(LambdaExprClass, Empty), NumCaptures(NumCaptures),
1627 CaptureDefault(LCD_None), ExplicitParams(false),
1628 ExplicitResultType(false) {
1629 getStoredStmts()[NumCaptures] = nullptr;
1632 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
1634 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
1637 friend class ASTStmtReader;
1638 friend class ASTStmtWriter;
1639 friend TrailingObjects;
1641 /// Construct a new lambda expression.
1643 Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange,
1644 LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc,
1645 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
1646 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1647 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack);
1649 /// Construct a new lambda expression that will be deserialized from
1650 /// an external source.
1651 static LambdaExpr *CreateDeserialized(const ASTContext &C,
1652 unsigned NumCaptures);
1654 /// Determine the default capture kind for this lambda.
1655 LambdaCaptureDefault getCaptureDefault() const {
1656 return static_cast<LambdaCaptureDefault>(CaptureDefault);
1659 /// Retrieve the location of this lambda's capture-default, if any.
1660 SourceLocation getCaptureDefaultLoc() const {
1661 return CaptureDefaultLoc;
1664 /// Determine whether one of this lambda's captures is an init-capture.
1665 bool isInitCapture(const LambdaCapture *Capture) const;
1667 /// An iterator that walks over the captures of the lambda,
1668 /// both implicit and explicit.
1669 using capture_iterator = const LambdaCapture *;
1671 /// An iterator over a range of lambda captures.
1672 using capture_range = llvm::iterator_range<capture_iterator>;
1674 /// Retrieve this lambda's captures.
1675 capture_range captures() const;
1677 /// Retrieve an iterator pointing to the first lambda capture.
1678 capture_iterator capture_begin() const;
1680 /// Retrieve an iterator pointing past the end of the
1681 /// sequence of lambda captures.
1682 capture_iterator capture_end() const;
1684 /// Determine the number of captures in this lambda.
1685 unsigned capture_size() const { return NumCaptures; }
1687 /// Retrieve this lambda's explicit captures.
1688 capture_range explicit_captures() const;
1690 /// Retrieve an iterator pointing to the first explicit
1692 capture_iterator explicit_capture_begin() const;
1694 /// Retrieve an iterator pointing past the end of the sequence of
1695 /// explicit lambda captures.
1696 capture_iterator explicit_capture_end() const;
1698 /// Retrieve this lambda's implicit captures.
1699 capture_range implicit_captures() const;
1701 /// Retrieve an iterator pointing to the first implicit
1703 capture_iterator implicit_capture_begin() const;
1705 /// Retrieve an iterator pointing past the end of the sequence of
1706 /// implicit lambda captures.
1707 capture_iterator implicit_capture_end() const;
1709 /// Iterator that walks over the capture initialization
1711 using capture_init_iterator = Expr **;
1713 /// Const iterator that walks over the capture initialization
1715 using const_capture_init_iterator = Expr *const *;
1717 /// Retrieve the initialization expressions for this lambda's captures.
1718 llvm::iterator_range<capture_init_iterator> capture_inits() {
1719 return llvm::make_range(capture_init_begin(), capture_init_end());
1722 /// Retrieve the initialization expressions for this lambda's captures.
1723 llvm::iterator_range<const_capture_init_iterator> capture_inits() const {
1724 return llvm::make_range(capture_init_begin(), capture_init_end());
1727 /// Retrieve the first initialization argument for this
1728 /// lambda expression (which initializes the first capture field).
1729 capture_init_iterator capture_init_begin() {
1730 return reinterpret_cast<Expr **>(getStoredStmts());
1733 /// Retrieve the first initialization argument for this
1734 /// lambda expression (which initializes the first capture field).
1735 const_capture_init_iterator capture_init_begin() const {
1736 return reinterpret_cast<Expr *const *>(getStoredStmts());
1739 /// Retrieve the iterator pointing one past the last
1740 /// initialization argument for this lambda expression.
1741 capture_init_iterator capture_init_end() {
1742 return capture_init_begin() + NumCaptures;
1745 /// Retrieve the iterator pointing one past the last
1746 /// initialization argument for this lambda expression.
1747 const_capture_init_iterator capture_init_end() const {
1748 return capture_init_begin() + NumCaptures;
1751 /// Retrieve the source range covering the lambda introducer,
1752 /// which contains the explicit capture list surrounded by square
1753 /// brackets ([...]).
1754 SourceRange getIntroducerRange() const { return IntroducerRange; }
1756 /// Retrieve the class that corresponds to the lambda.
1758 /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the
1759 /// captures in its fields and provides the various operations permitted
1760 /// on a lambda (copying, calling).
1761 CXXRecordDecl *getLambdaClass() const;
1763 /// Retrieve the function call operator associated with this
1764 /// lambda expression.
1765 CXXMethodDecl *getCallOperator() const;
1767 /// If this is a generic lambda expression, retrieve the template
1768 /// parameter list associated with it, or else return null.
1769 TemplateParameterList *getTemplateParameterList() const;
1771 /// Whether this is a generic lambda.
1772 bool isGenericLambda() const { return getTemplateParameterList(); }
1774 /// Retrieve the body of the lambda.
1775 CompoundStmt *getBody() const;
1777 /// Determine whether the lambda is mutable, meaning that any
1778 /// captures values can be modified.
1779 bool isMutable() const;
1781 /// Determine whether this lambda has an explicit parameter
1782 /// list vs. an implicit (empty) parameter list.
1783 bool hasExplicitParameters() const { return ExplicitParams; }
1785 /// Whether this lambda had its result type explicitly specified.
1786 bool hasExplicitResultType() const { return ExplicitResultType; }
1788 static bool classof(const Stmt *T) {
1789 return T->getStmtClass() == LambdaExprClass;
1792 SourceLocation getLocStart() const LLVM_READONLY {
1793 return IntroducerRange.getBegin();
1796 SourceLocation getLocEnd() const LLVM_READONLY { return ClosingBrace; }
1798 child_range children() {
1799 // Includes initialization exprs plus body stmt
1800 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures + 1);
1804 /// An expression "T()" which creates a value-initialized rvalue of type
1805 /// T, which is a non-class type. See (C++98 [5.2.3p2]).
1806 class CXXScalarValueInitExpr : public Expr {
1807 friend class ASTStmtReader;
1809 SourceLocation RParenLoc;
1810 TypeSourceInfo *TypeInfo;
1813 /// Create an explicitly-written scalar-value initialization
1815 CXXScalarValueInitExpr(QualType Type, TypeSourceInfo *TypeInfo,
1816 SourceLocation rParenLoc)
1817 : Expr(CXXScalarValueInitExprClass, Type, VK_RValue, OK_Ordinary,
1818 false, false, Type->isInstantiationDependentType(),
1819 Type->containsUnexpandedParameterPack()),
1820 RParenLoc(rParenLoc), TypeInfo(TypeInfo) {}
1822 explicit CXXScalarValueInitExpr(EmptyShell Shell)
1823 : Expr(CXXScalarValueInitExprClass, Shell) {}
1825 TypeSourceInfo *getTypeSourceInfo() const {
1829 SourceLocation getRParenLoc() const { return RParenLoc; }
1831 SourceLocation getLocStart() const LLVM_READONLY;
1832 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1834 static bool classof(const Stmt *T) {
1835 return T->getStmtClass() == CXXScalarValueInitExprClass;
1839 child_range children() {
1840 return child_range(child_iterator(), child_iterator());
1844 /// Represents a new-expression for memory allocation and constructor
1845 /// calls, e.g: "new CXXNewExpr(foo)".
1846 class CXXNewExpr : public Expr {
1847 friend class ASTStmtReader;
1848 friend class ASTStmtWriter;
1850 /// Contains an optional array size expression, an optional initialization
1851 /// expression, and any number of optional placement arguments, in that order.
1852 Stmt **SubExprs = nullptr;
1854 /// Points to the allocation function used.
1855 FunctionDecl *OperatorNew;
1857 /// Points to the deallocation function used in case of error. May be
1859 FunctionDecl *OperatorDelete;
1861 /// The allocated type-source information, as written in the source.
1862 TypeSourceInfo *AllocatedTypeInfo;
1864 /// If the allocated type was expressed as a parenthesized type-id,
1865 /// the source range covering the parenthesized type-id.
1866 SourceRange TypeIdParens;
1868 /// Range of the entire new expression.
1871 /// Source-range of a paren-delimited initializer.
1872 SourceRange DirectInitRange;
1874 /// Was the usage ::new, i.e. is the global new to be used?
1875 unsigned GlobalNew : 1;
1877 /// Do we allocate an array? If so, the first SubExpr is the size expression.
1880 /// Should the alignment be passed to the allocation function?
1881 unsigned PassAlignment : 1;
1883 /// If this is an array allocation, does the usual deallocation
1884 /// function for the allocated type want to know the allocated size?
1885 unsigned UsualArrayDeleteWantsSize : 1;
1887 /// The number of placement new arguments.
1888 unsigned NumPlacementArgs : 26;
1890 /// What kind of initializer do we have? Could be none, parens, or braces.
1891 /// In storage, we distinguish between "none, and no initializer expr", and
1892 /// "none, but an implicit initializer expr".
1893 unsigned StoredInitializationStyle : 2;
1896 enum InitializationStyle {
1897 /// New-expression has no initializer as written.
1900 /// New-expression has a C++98 paren-delimited initializer.
1903 /// New-expression has a C++11 list-initializer.
1907 CXXNewExpr(const ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
1908 FunctionDecl *operatorDelete, bool PassAlignment,
1909 bool usualArrayDeleteWantsSize, ArrayRef<Expr*> placementArgs,
1910 SourceRange typeIdParens, Expr *arraySize,
1911 InitializationStyle initializationStyle, Expr *initializer,
1912 QualType ty, TypeSourceInfo *AllocatedTypeInfo,
1913 SourceRange Range, SourceRange directInitRange);
1914 explicit CXXNewExpr(EmptyShell Shell)
1915 : Expr(CXXNewExprClass, Shell) {}
1917 void AllocateArgsArray(const ASTContext &C, bool isArray,
1918 unsigned numPlaceArgs, bool hasInitializer);
1920 QualType getAllocatedType() const {
1921 assert(getType()->isPointerType());
1922 return getType()->getAs<PointerType>()->getPointeeType();
1925 TypeSourceInfo *getAllocatedTypeSourceInfo() const {
1926 return AllocatedTypeInfo;
1929 /// True if the allocation result needs to be null-checked.
1931 /// C++11 [expr.new]p13:
1932 /// If the allocation function returns null, initialization shall
1933 /// not be done, the deallocation function shall not be called,
1934 /// and the value of the new-expression shall be null.
1937 /// If the allocation function is a reserved placement allocation
1938 /// function that returns null, the behavior is undefined.
1940 /// An allocation function is not allowed to return null unless it
1941 /// has a non-throwing exception-specification. The '03 rule is
1942 /// identical except that the definition of a non-throwing
1943 /// exception specification is just "is it throw()?".
1944 bool shouldNullCheckAllocation(const ASTContext &Ctx) const;
1946 FunctionDecl *getOperatorNew() const { return OperatorNew; }
1947 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; }
1948 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
1949 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; }
1951 bool isArray() const { return Array; }
1953 Expr *getArraySize() {
1954 return Array ? cast<Expr>(SubExprs[0]) : nullptr;
1956 const Expr *getArraySize() const {
1957 return Array ? cast<Expr>(SubExprs[0]) : nullptr;
1960 unsigned getNumPlacementArgs() const { return NumPlacementArgs; }
1962 Expr **getPlacementArgs() {
1963 return reinterpret_cast<Expr **>(SubExprs + Array + hasInitializer());
1966 Expr *getPlacementArg(unsigned i) {
1967 assert(i < NumPlacementArgs && "Index out of range");
1968 return getPlacementArgs()[i];
1970 const Expr *getPlacementArg(unsigned i) const {
1971 assert(i < NumPlacementArgs && "Index out of range");
1972 return const_cast<CXXNewExpr*>(this)->getPlacementArg(i);
1975 bool isParenTypeId() const { return TypeIdParens.isValid(); }
1976 SourceRange getTypeIdParens() const { return TypeIdParens; }
1978 bool isGlobalNew() const { return GlobalNew; }
1980 /// Whether this new-expression has any initializer at all.
1981 bool hasInitializer() const { return StoredInitializationStyle > 0; }
1983 /// The kind of initializer this new-expression has.
1984 InitializationStyle getInitializationStyle() const {
1985 if (StoredInitializationStyle == 0)
1987 return static_cast<InitializationStyle>(StoredInitializationStyle-1);
1990 /// The initializer of this new-expression.
1991 Expr *getInitializer() {
1992 return hasInitializer() ? cast<Expr>(SubExprs[Array]) : nullptr;
1994 const Expr *getInitializer() const {
1995 return hasInitializer() ? cast<Expr>(SubExprs[Array]) : nullptr;
1998 /// Returns the CXXConstructExpr from this new-expression, or null.
1999 const CXXConstructExpr *getConstructExpr() const {
2000 return dyn_cast_or_null<CXXConstructExpr>(getInitializer());
2003 /// Indicates whether the required alignment should be implicitly passed to
2004 /// the allocation function.
2005 bool passAlignment() const {
2006 return PassAlignment;
2009 /// Answers whether the usual array deallocation function for the
2010 /// allocated type expects the size of the allocation as a
2012 bool doesUsualArrayDeleteWantSize() const {
2013 return UsualArrayDeleteWantsSize;
2016 using arg_iterator = ExprIterator;
2017 using const_arg_iterator = ConstExprIterator;
2019 llvm::iterator_range<arg_iterator> placement_arguments() {
2020 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2023 llvm::iterator_range<const_arg_iterator> placement_arguments() const {
2024 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2027 arg_iterator placement_arg_begin() {
2028 return SubExprs + Array + hasInitializer();
2030 arg_iterator placement_arg_end() {
2031 return SubExprs + Array + hasInitializer() + getNumPlacementArgs();
2033 const_arg_iterator placement_arg_begin() const {
2034 return SubExprs + Array + hasInitializer();
2036 const_arg_iterator placement_arg_end() const {
2037 return SubExprs + Array + hasInitializer() + getNumPlacementArgs();
2040 using raw_arg_iterator = Stmt **;
2042 raw_arg_iterator raw_arg_begin() { return SubExprs; }
2043 raw_arg_iterator raw_arg_end() {
2044 return SubExprs + Array + hasInitializer() + getNumPlacementArgs();
2046 const_arg_iterator raw_arg_begin() const { return SubExprs; }
2047 const_arg_iterator raw_arg_end() const {
2048 return SubExprs + Array + hasInitializer() + getNumPlacementArgs();
2051 SourceLocation getStartLoc() const { return Range.getBegin(); }
2052 SourceLocation getEndLoc() const { return Range.getEnd(); }
2054 SourceRange getDirectInitRange() const { return DirectInitRange; }
2056 SourceRange getSourceRange() const LLVM_READONLY {
2060 SourceLocation getLocStart() const LLVM_READONLY { return getStartLoc(); }
2061 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
2063 static bool classof(const Stmt *T) {
2064 return T->getStmtClass() == CXXNewExprClass;
2068 child_range children() {
2069 return child_range(raw_arg_begin(), raw_arg_end());
2073 /// Represents a \c delete expression for memory deallocation and
2074 /// destructor calls, e.g. "delete[] pArray".
2075 class CXXDeleteExpr : public Expr {
2076 /// Points to the operator delete overload that is used. Could be a member.
2077 FunctionDecl *OperatorDelete = nullptr;
2079 /// The pointer expression to be deleted.
2080 Stmt *Argument = nullptr;
2082 /// Location of the expression.
2085 /// Is this a forced global delete, i.e. "::delete"?
2086 bool GlobalDelete : 1;
2088 /// Is this the array form of delete, i.e. "delete[]"?
2091 /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is applied
2092 /// to pointer-to-array type (ArrayFormAsWritten will be false while ArrayForm
2094 bool ArrayFormAsWritten : 1;
2096 /// Does the usual deallocation function for the element type require
2097 /// a size_t argument?
2098 bool UsualArrayDeleteWantsSize : 1;
2101 friend class ASTStmtReader;
2103 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm,
2104 bool arrayFormAsWritten, bool usualArrayDeleteWantsSize,
2105 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc)
2106 : Expr(CXXDeleteExprClass, ty, VK_RValue, OK_Ordinary, false, false,
2107 arg->isInstantiationDependent(),
2108 arg->containsUnexpandedParameterPack()),
2109 OperatorDelete(operatorDelete), Argument(arg), Loc(loc),
2110 GlobalDelete(globalDelete),
2111 ArrayForm(arrayForm), ArrayFormAsWritten(arrayFormAsWritten),
2112 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {}
2113 explicit CXXDeleteExpr(EmptyShell Shell) : Expr(CXXDeleteExprClass, Shell) {}
2115 bool isGlobalDelete() const { return GlobalDelete; }
2116 bool isArrayForm() const { return ArrayForm; }
2117 bool isArrayFormAsWritten() const { return ArrayFormAsWritten; }
2119 /// Answers whether the usual array deallocation function for the
2120 /// allocated type expects the size of the allocation as a
2121 /// parameter. This can be true even if the actual deallocation
2122 /// function that we're using doesn't want a size.
2123 bool doesUsualArrayDeleteWantSize() const {
2124 return UsualArrayDeleteWantsSize;
2127 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2129 Expr *getArgument() { return cast<Expr>(Argument); }
2130 const Expr *getArgument() const { return cast<Expr>(Argument); }
2132 /// Retrieve the type being destroyed.
2134 /// If the type being destroyed is a dependent type which may or may not
2135 /// be a pointer, return an invalid type.
2136 QualType getDestroyedType() const;
2138 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
2139 SourceLocation getLocEnd() const LLVM_READONLY {return Argument->getLocEnd();}
2141 static bool classof(const Stmt *T) {
2142 return T->getStmtClass() == CXXDeleteExprClass;
2146 child_range children() { return child_range(&Argument, &Argument+1); }
2149 /// Stores the type being destroyed by a pseudo-destructor expression.
2150 class PseudoDestructorTypeStorage {
2151 /// Either the type source information or the name of the type, if
2152 /// it couldn't be resolved due to type-dependence.
2153 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type;
2155 /// The starting source location of the pseudo-destructor type.
2156 SourceLocation Location;
2159 PseudoDestructorTypeStorage() = default;
2161 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc)
2162 : Type(II), Location(Loc) {}
2164 PseudoDestructorTypeStorage(TypeSourceInfo *Info);
2166 TypeSourceInfo *getTypeSourceInfo() const {
2167 return Type.dyn_cast<TypeSourceInfo *>();
2170 IdentifierInfo *getIdentifier() const {
2171 return Type.dyn_cast<IdentifierInfo *>();
2174 SourceLocation getLocation() const { return Location; }
2177 /// Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
2179 /// A pseudo-destructor is an expression that looks like a member access to a
2180 /// destructor of a scalar type, except that scalar types don't have
2181 /// destructors. For example:
2185 /// void f(int *p) {
2190 /// Pseudo-destructors typically occur when instantiating templates such as:
2193 /// template<typename T>
2194 /// void destroy(T* ptr) {
2199 /// for scalar types. A pseudo-destructor expression has no run-time semantics
2200 /// beyond evaluating the base expression.
2201 class CXXPseudoDestructorExpr : public Expr {
2202 friend class ASTStmtReader;
2204 /// The base expression (that is being destroyed).
2205 Stmt *Base = nullptr;
2207 /// Whether the operator was an arrow ('->'); otherwise, it was a
2211 /// The location of the '.' or '->' operator.
2212 SourceLocation OperatorLoc;
2214 /// The nested-name-specifier that follows the operator, if present.
2215 NestedNameSpecifierLoc QualifierLoc;
2217 /// The type that precedes the '::' in a qualified pseudo-destructor
2219 TypeSourceInfo *ScopeType = nullptr;
2221 /// The location of the '::' in a qualified pseudo-destructor
2223 SourceLocation ColonColonLoc;
2225 /// The location of the '~'.
2226 SourceLocation TildeLoc;
2228 /// The type being destroyed, or its name if we were unable to
2229 /// resolve the name.
2230 PseudoDestructorTypeStorage DestroyedType;
2233 CXXPseudoDestructorExpr(const ASTContext &Context,
2234 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
2235 NestedNameSpecifierLoc QualifierLoc,
2236 TypeSourceInfo *ScopeType,
2237 SourceLocation ColonColonLoc,
2238 SourceLocation TildeLoc,
2239 PseudoDestructorTypeStorage DestroyedType);
2241 explicit CXXPseudoDestructorExpr(EmptyShell Shell)
2242 : Expr(CXXPseudoDestructorExprClass, Shell), IsArrow(false) {}
2244 Expr *getBase() const { return cast<Expr>(Base); }
2246 /// Determines whether this member expression actually had
2247 /// a C++ nested-name-specifier prior to the name of the member, e.g.,
2249 bool hasQualifier() const { return QualifierLoc.hasQualifier(); }
2251 /// Retrieves the nested-name-specifier that qualifies the type name,
2252 /// with source-location information.
2253 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2255 /// If the member name was qualified, retrieves the
2256 /// nested-name-specifier that precedes the member name. Otherwise, returns
2258 NestedNameSpecifier *getQualifier() const {
2259 return QualifierLoc.getNestedNameSpecifier();
2262 /// Determine whether this pseudo-destructor expression was written
2263 /// using an '->' (otherwise, it used a '.').
2264 bool isArrow() const { return IsArrow; }
2266 /// Retrieve the location of the '.' or '->' operator.
2267 SourceLocation getOperatorLoc() const { return OperatorLoc; }
2269 /// Retrieve the scope type in a qualified pseudo-destructor
2272 /// Pseudo-destructor expressions can have extra qualification within them
2273 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T().
2274 /// Here, if the object type of the expression is (or may be) a scalar type,
2275 /// \p T may also be a scalar type and, therefore, cannot be part of a
2276 /// nested-name-specifier. It is stored as the "scope type" of the pseudo-
2277 /// destructor expression.
2278 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; }
2280 /// Retrieve the location of the '::' in a qualified pseudo-destructor
2282 SourceLocation getColonColonLoc() const { return ColonColonLoc; }
2284 /// Retrieve the location of the '~'.
2285 SourceLocation getTildeLoc() const { return TildeLoc; }
2287 /// Retrieve the source location information for the type
2288 /// being destroyed.
2290 /// This type-source information is available for non-dependent
2291 /// pseudo-destructor expressions and some dependent pseudo-destructor
2292 /// expressions. Returns null if we only have the identifier for a
2293 /// dependent pseudo-destructor expression.
2294 TypeSourceInfo *getDestroyedTypeInfo() const {
2295 return DestroyedType.getTypeSourceInfo();
2298 /// In a dependent pseudo-destructor expression for which we do not
2299 /// have full type information on the destroyed type, provides the name
2300 /// of the destroyed type.
2301 IdentifierInfo *getDestroyedTypeIdentifier() const {
2302 return DestroyedType.getIdentifier();
2305 /// Retrieve the type being destroyed.
2306 QualType getDestroyedType() const;
2308 /// Retrieve the starting location of the type being destroyed.
2309 SourceLocation getDestroyedTypeLoc() const {
2310 return DestroyedType.getLocation();
2313 /// Set the name of destroyed type for a dependent pseudo-destructor
2315 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) {
2316 DestroyedType = PseudoDestructorTypeStorage(II, Loc);
2319 /// Set the destroyed type.
2320 void setDestroyedType(TypeSourceInfo *Info) {
2321 DestroyedType = PseudoDestructorTypeStorage(Info);
2324 SourceLocation getLocStart() const LLVM_READONLY {return Base->getLocStart();}
2325 SourceLocation getLocEnd() const LLVM_READONLY;
2327 static bool classof(const Stmt *T) {
2328 return T->getStmtClass() == CXXPseudoDestructorExprClass;
2332 child_range children() { return child_range(&Base, &Base + 1); }
2335 /// A type trait used in the implementation of various C++11 and
2336 /// Library TR1 trait templates.
2339 /// __is_pod(int) == true
2340 /// __is_enum(std::string) == false
2341 /// __is_trivially_constructible(vector<int>, int*, int*)
2343 class TypeTraitExpr final
2345 private llvm::TrailingObjects<TypeTraitExpr, TypeSourceInfo *> {
2346 /// The location of the type trait keyword.
2349 /// The location of the closing parenthesis.
2350 SourceLocation RParenLoc;
2352 // Note: The TypeSourceInfos for the arguments are allocated after the
2355 TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
2356 ArrayRef<TypeSourceInfo *> Args,
2357 SourceLocation RParenLoc,
2360 TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) {}
2362 size_t numTrailingObjects(OverloadToken<TypeSourceInfo *>) const {
2363 return getNumArgs();
2367 friend class ASTStmtReader;
2368 friend class ASTStmtWriter;
2369 friend TrailingObjects;
2371 /// Create a new type trait expression.
2372 static TypeTraitExpr *Create(const ASTContext &C, QualType T,
2373 SourceLocation Loc, TypeTrait Kind,
2374 ArrayRef<TypeSourceInfo *> Args,
2375 SourceLocation RParenLoc,
2378 static TypeTraitExpr *CreateDeserialized(const ASTContext &C,
2381 /// Determine which type trait this expression uses.
2382 TypeTrait getTrait() const {
2383 return static_cast<TypeTrait>(TypeTraitExprBits.Kind);
2386 bool getValue() const {
2387 assert(!isValueDependent());
2388 return TypeTraitExprBits.Value;
2391 /// Determine the number of arguments to this type trait.
2392 unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; }
2394 /// Retrieve the Ith argument.
2395 TypeSourceInfo *getArg(unsigned I) const {
2396 assert(I < getNumArgs() && "Argument out-of-range");
2397 return getArgs()[I];
2400 /// Retrieve the argument types.
2401 ArrayRef<TypeSourceInfo *> getArgs() const {
2402 return llvm::makeArrayRef(getTrailingObjects<TypeSourceInfo *>(),
2406 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
2407 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
2409 static bool classof(const Stmt *T) {
2410 return T->getStmtClass() == TypeTraitExprClass;
2414 child_range children() {
2415 return child_range(child_iterator(), child_iterator());
2419 /// An Embarcadero array type trait, as used in the implementation of
2420 /// __array_rank and __array_extent.
2424 /// __array_rank(int[10][20]) == 2
2425 /// __array_extent(int, 1) == 20
2427 class ArrayTypeTraitExpr : public Expr {
2428 /// The trait. An ArrayTypeTrait enum in MSVC compat unsigned.
2431 /// The value of the type trait. Unspecified if dependent.
2434 /// The array dimension being queried, or -1 if not used.
2437 /// The location of the type trait keyword.
2440 /// The location of the closing paren.
2441 SourceLocation RParen;
2443 /// The type being queried.
2444 TypeSourceInfo *QueriedType = nullptr;
2446 virtual void anchor();
2449 friend class ASTStmtReader;
2451 ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att,
2452 TypeSourceInfo *queried, uint64_t value,
2453 Expr *dimension, SourceLocation rparen, QualType ty)
2454 : Expr(ArrayTypeTraitExprClass, ty, VK_RValue, OK_Ordinary,
2455 false, queried->getType()->isDependentType(),
2456 (queried->getType()->isInstantiationDependentType() ||
2457 (dimension && dimension->isInstantiationDependent())),
2458 queried->getType()->containsUnexpandedParameterPack()),
2459 ATT(att), Value(value), Dimension(dimension),
2460 Loc(loc), RParen(rparen), QueriedType(queried) {}
2462 explicit ArrayTypeTraitExpr(EmptyShell Empty)
2463 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0) {}
2465 virtual ~ArrayTypeTraitExpr() = default;
2467 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
2468 SourceLocation getLocEnd() const LLVM_READONLY { return RParen; }
2470 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); }
2472 QualType getQueriedType() const { return QueriedType->getType(); }
2474 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; }
2476 uint64_t getValue() const { assert(!isTypeDependent()); return Value; }
2478 Expr *getDimensionExpression() const { return Dimension; }
2480 static bool classof(const Stmt *T) {
2481 return T->getStmtClass() == ArrayTypeTraitExprClass;
2485 child_range children() {
2486 return child_range(child_iterator(), child_iterator());
2490 /// An expression trait intrinsic.
2494 /// __is_lvalue_expr(std::cout) == true
2495 /// __is_lvalue_expr(1) == false
2497 class ExpressionTraitExpr : public Expr {
2498 /// The trait. A ExpressionTrait enum in MSVC compatible unsigned.
2501 /// The value of the type trait. Unspecified if dependent.
2504 /// The location of the type trait keyword.
2507 /// The location of the closing paren.
2508 SourceLocation RParen;
2510 /// The expression being queried.
2511 Expr* QueriedExpression = nullptr;
2514 friend class ASTStmtReader;
2516 ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et,
2517 Expr *queried, bool value,
2518 SourceLocation rparen, QualType resultType)
2519 : Expr(ExpressionTraitExprClass, resultType, VK_RValue, OK_Ordinary,
2520 false, // Not type-dependent
2521 // Value-dependent if the argument is type-dependent.
2522 queried->isTypeDependent(),
2523 queried->isInstantiationDependent(),
2524 queried->containsUnexpandedParameterPack()),
2525 ET(et), Value(value), Loc(loc), RParen(rparen),
2526 QueriedExpression(queried) {}
2528 explicit ExpressionTraitExpr(EmptyShell Empty)
2529 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false) {}
2531 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
2532 SourceLocation getLocEnd() const LLVM_READONLY { return RParen; }
2534 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); }
2536 Expr *getQueriedExpression() const { return QueriedExpression; }
2538 bool getValue() const { return Value; }
2540 static bool classof(const Stmt *T) {
2541 return T->getStmtClass() == ExpressionTraitExprClass;
2545 child_range children() {
2546 return child_range(child_iterator(), child_iterator());
2550 /// A reference to an overloaded function set, either an
2551 /// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr.
2552 class OverloadExpr : public Expr {
2553 /// The common name of these declarations.
2554 DeclarationNameInfo NameInfo;
2556 /// The nested-name-specifier that qualifies the name, if any.
2557 NestedNameSpecifierLoc QualifierLoc;
2559 /// The results. These are undesugared, which is to say, they may
2560 /// include UsingShadowDecls. Access is relative to the naming
2562 // FIXME: Allocate this data after the OverloadExpr subclass.
2563 DeclAccessPair *Results = nullptr;
2565 unsigned NumResults = 0;
2568 /// Whether the name includes info for explicit template
2569 /// keyword and arguments.
2570 bool HasTemplateKWAndArgsInfo = false;
2572 OverloadExpr(StmtClass K, const ASTContext &C,
2573 NestedNameSpecifierLoc QualifierLoc,
2574 SourceLocation TemplateKWLoc,
2575 const DeclarationNameInfo &NameInfo,
2576 const TemplateArgumentListInfo *TemplateArgs,
2577 UnresolvedSetIterator Begin, UnresolvedSetIterator End,
2578 bool KnownDependent,
2579 bool KnownInstantiationDependent,
2580 bool KnownContainsUnexpandedParameterPack);
2582 OverloadExpr(StmtClass K, EmptyShell Empty) : Expr(K, Empty) {}
2584 /// Return the optional template keyword and arguments info.
2585 ASTTemplateKWAndArgsInfo *
2586 getTrailingASTTemplateKWAndArgsInfo(); // defined far below.
2588 /// Return the optional template keyword and arguments info.
2589 const ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo() const {
2590 return const_cast<OverloadExpr *>(this)
2591 ->getTrailingASTTemplateKWAndArgsInfo();
2594 /// Return the optional template arguments.
2595 TemplateArgumentLoc *getTrailingTemplateArgumentLoc(); // defined far below
2597 void initializeResults(const ASTContext &C,
2598 UnresolvedSetIterator Begin,
2599 UnresolvedSetIterator End);
2602 friend class ASTStmtReader;
2603 friend class ASTStmtWriter;
2606 OverloadExpr *Expression;
2607 bool IsAddressOfOperand;
2608 bool HasFormOfMemberPointer;
2611 /// Finds the overloaded expression in the given expression \p E of
2614 /// \return the expression (which must be there) and true if it has
2615 /// the particular form of a member pointer expression
2616 static FindResult find(Expr *E) {
2617 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload));
2621 E = E->IgnoreParens();
2622 if (isa<UnaryOperator>(E)) {
2623 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf);
2624 E = cast<UnaryOperator>(E)->getSubExpr();
2625 auto *Ovl = cast<OverloadExpr>(E->IgnoreParens());
2627 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier());
2628 Result.IsAddressOfOperand = true;
2629 Result.Expression = Ovl;
2631 Result.HasFormOfMemberPointer = false;
2632 Result.IsAddressOfOperand = false;
2633 Result.Expression = cast<OverloadExpr>(E);
2639 /// Gets the naming class of this lookup, if any.
2640 CXXRecordDecl *getNamingClass() const;
2642 using decls_iterator = UnresolvedSetImpl::iterator;
2644 decls_iterator decls_begin() const { return UnresolvedSetIterator(Results); }
2645 decls_iterator decls_end() const {
2646 return UnresolvedSetIterator(Results + NumResults);
2648 llvm::iterator_range<decls_iterator> decls() const {
2649 return llvm::make_range(decls_begin(), decls_end());
2652 /// Gets the number of declarations in the unresolved set.
2653 unsigned getNumDecls() const { return NumResults; }
2655 /// Gets the full name info.
2656 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2658 /// Gets the name looked up.
2659 DeclarationName getName() const { return NameInfo.getName(); }
2661 /// Gets the location of the name.
2662 SourceLocation getNameLoc() const { return NameInfo.getLoc(); }
2664 /// Fetches the nested-name qualifier, if one was given.
2665 NestedNameSpecifier *getQualifier() const {
2666 return QualifierLoc.getNestedNameSpecifier();
2669 /// Fetches the nested-name qualifier with source-location
2670 /// information, if one was given.
2671 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2673 /// Retrieve the location of the template keyword preceding
2674 /// this name, if any.
2675 SourceLocation getTemplateKeywordLoc() const {
2676 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2677 return getTrailingASTTemplateKWAndArgsInfo()->TemplateKWLoc;
2680 /// Retrieve the location of the left angle bracket starting the
2681 /// explicit template argument list following the name, if any.
2682 SourceLocation getLAngleLoc() const {
2683 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2684 return getTrailingASTTemplateKWAndArgsInfo()->LAngleLoc;
2687 /// Retrieve the location of the right angle bracket ending the
2688 /// explicit template argument list following the name, if any.
2689 SourceLocation getRAngleLoc() const {
2690 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2691 return getTrailingASTTemplateKWAndArgsInfo()->RAngleLoc;
2694 /// Determines whether the name was preceded by the template keyword.
2695 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
2697 /// Determines whether this expression had explicit template arguments.
2698 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
2700 TemplateArgumentLoc const *getTemplateArgs() const {
2701 if (!hasExplicitTemplateArgs())
2703 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
2706 unsigned getNumTemplateArgs() const {
2707 if (!hasExplicitTemplateArgs())
2710 return getTrailingASTTemplateKWAndArgsInfo()->NumTemplateArgs;
2713 ArrayRef<TemplateArgumentLoc> template_arguments() const {
2714 return {getTemplateArgs(), getNumTemplateArgs()};
2717 /// Copies the template arguments into the given structure.
2718 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
2719 if (hasExplicitTemplateArgs())
2720 getTrailingASTTemplateKWAndArgsInfo()->copyInto(getTemplateArgs(), List);
2723 static bool classof(const Stmt *T) {
2724 return T->getStmtClass() == UnresolvedLookupExprClass ||
2725 T->getStmtClass() == UnresolvedMemberExprClass;
2729 /// A reference to a name which we were able to look up during
2730 /// parsing but could not resolve to a specific declaration.
2732 /// This arises in several ways:
2733 /// * we might be waiting for argument-dependent lookup;
2734 /// * the name might resolve to an overloaded function;
2736 /// * the lookup might have included a function template.
2738 /// These never include UnresolvedUsingValueDecls, which are always class
2739 /// members and therefore appear only in UnresolvedMemberLookupExprs.
2740 class UnresolvedLookupExpr final
2741 : public OverloadExpr,
2742 private llvm::TrailingObjects<
2743 UnresolvedLookupExpr, ASTTemplateKWAndArgsInfo, TemplateArgumentLoc> {
2744 friend class ASTStmtReader;
2745 friend class OverloadExpr;
2746 friend TrailingObjects;
2748 /// True if these lookup results should be extended by
2749 /// argument-dependent lookup if this is the operand of a function
2751 bool RequiresADL = false;
2753 /// True if these lookup results are overloaded. This is pretty
2754 /// trivially rederivable if we urgently need to kill this field.
2755 bool Overloaded = false;
2757 /// The naming class (C++ [class.access.base]p5) of the lookup, if
2758 /// any. This can generally be recalculated from the context chain,
2759 /// but that can be fairly expensive for unqualified lookups. If we
2760 /// want to improve memory use here, this could go in a union
2761 /// against the qualified-lookup bits.
2762 CXXRecordDecl *NamingClass = nullptr;
2764 UnresolvedLookupExpr(const ASTContext &C,
2765 CXXRecordDecl *NamingClass,
2766 NestedNameSpecifierLoc QualifierLoc,
2767 SourceLocation TemplateKWLoc,
2768 const DeclarationNameInfo &NameInfo,
2769 bool RequiresADL, bool Overloaded,
2770 const TemplateArgumentListInfo *TemplateArgs,
2771 UnresolvedSetIterator Begin, UnresolvedSetIterator End)
2772 : OverloadExpr(UnresolvedLookupExprClass, C, QualifierLoc, TemplateKWLoc,
2773 NameInfo, TemplateArgs, Begin, End, false, false, false),
2774 RequiresADL(RequiresADL),
2775 Overloaded(Overloaded), NamingClass(NamingClass) {}
2777 UnresolvedLookupExpr(EmptyShell Empty)
2778 : OverloadExpr(UnresolvedLookupExprClass, Empty) {}
2780 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
2781 return HasTemplateKWAndArgsInfo ? 1 : 0;
2785 static UnresolvedLookupExpr *Create(const ASTContext &C,
2786 CXXRecordDecl *NamingClass,
2787 NestedNameSpecifierLoc QualifierLoc,
2788 const DeclarationNameInfo &NameInfo,
2789 bool ADL, bool Overloaded,
2790 UnresolvedSetIterator Begin,
2791 UnresolvedSetIterator End) {
2792 return new(C) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
2793 SourceLocation(), NameInfo,
2794 ADL, Overloaded, nullptr, Begin, End);
2797 static UnresolvedLookupExpr *Create(const ASTContext &C,
2798 CXXRecordDecl *NamingClass,
2799 NestedNameSpecifierLoc QualifierLoc,
2800 SourceLocation TemplateKWLoc,
2801 const DeclarationNameInfo &NameInfo,
2803 const TemplateArgumentListInfo *Args,
2804 UnresolvedSetIterator Begin,
2805 UnresolvedSetIterator End);
2807 static UnresolvedLookupExpr *CreateEmpty(const ASTContext &C,
2808 bool HasTemplateKWAndArgsInfo,
2809 unsigned NumTemplateArgs);
2811 /// True if this declaration should be extended by
2812 /// argument-dependent lookup.
2813 bool requiresADL() const { return RequiresADL; }
2815 /// True if this lookup is overloaded.
2816 bool isOverloaded() const { return Overloaded; }
2818 /// Gets the 'naming class' (in the sense of C++0x
2819 /// [class.access.base]p5) of the lookup. This is the scope
2820 /// that was looked in to find these results.
2821 CXXRecordDecl *getNamingClass() const { return NamingClass; }
2823 SourceLocation getLocStart() const LLVM_READONLY {
2824 if (NestedNameSpecifierLoc l = getQualifierLoc())
2825 return l.getBeginLoc();
2826 return getNameInfo().getLocStart();
2829 SourceLocation getLocEnd() const LLVM_READONLY {
2830 if (hasExplicitTemplateArgs())
2831 return getRAngleLoc();
2832 return getNameInfo().getLocEnd();
2835 child_range children() {
2836 return child_range(child_iterator(), child_iterator());
2839 static bool classof(const Stmt *T) {
2840 return T->getStmtClass() == UnresolvedLookupExprClass;
2844 /// A qualified reference to a name whose declaration cannot
2845 /// yet be resolved.
2847 /// DependentScopeDeclRefExpr is similar to DeclRefExpr in that
2848 /// it expresses a reference to a declaration such as
2849 /// X<T>::value. The difference, however, is that an
2850 /// DependentScopeDeclRefExpr node is used only within C++ templates when
2851 /// the qualification (e.g., X<T>::) refers to a dependent type. In
2852 /// this case, X<T>::value cannot resolve to a declaration because the
2853 /// declaration will differ from one instantiation of X<T> to the
2854 /// next. Therefore, DependentScopeDeclRefExpr keeps track of the
2855 /// qualifier (X<T>::) and the name of the entity being referenced
2856 /// ("value"). Such expressions will instantiate to a DeclRefExpr once the
2857 /// declaration can be found.
2858 class DependentScopeDeclRefExpr final
2860 private llvm::TrailingObjects<DependentScopeDeclRefExpr,
2861 ASTTemplateKWAndArgsInfo,
2862 TemplateArgumentLoc> {
2863 /// The nested-name-specifier that qualifies this unresolved
2864 /// declaration name.
2865 NestedNameSpecifierLoc QualifierLoc;
2867 /// The name of the entity we will be referencing.
2868 DeclarationNameInfo NameInfo;
2870 /// Whether the name includes info for explicit template
2871 /// keyword and arguments.
2872 bool HasTemplateKWAndArgsInfo;
2874 DependentScopeDeclRefExpr(QualType T,
2875 NestedNameSpecifierLoc QualifierLoc,
2876 SourceLocation TemplateKWLoc,
2877 const DeclarationNameInfo &NameInfo,
2878 const TemplateArgumentListInfo *Args);
2880 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
2881 return HasTemplateKWAndArgsInfo ? 1 : 0;
2885 friend class ASTStmtReader;
2886 friend class ASTStmtWriter;
2887 friend TrailingObjects;
2889 static DependentScopeDeclRefExpr *Create(const ASTContext &C,
2890 NestedNameSpecifierLoc QualifierLoc,
2891 SourceLocation TemplateKWLoc,
2892 const DeclarationNameInfo &NameInfo,
2893 const TemplateArgumentListInfo *TemplateArgs);
2895 static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &C,
2896 bool HasTemplateKWAndArgsInfo,
2897 unsigned NumTemplateArgs);
2899 /// Retrieve the name that this expression refers to.
2900 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2902 /// Retrieve the name that this expression refers to.
2903 DeclarationName getDeclName() const { return NameInfo.getName(); }
2905 /// Retrieve the location of the name within the expression.
2907 /// For example, in "X<T>::value" this is the location of "value".
2908 SourceLocation getLocation() const { return NameInfo.getLoc(); }
2910 /// Retrieve the nested-name-specifier that qualifies the
2911 /// name, with source location information.
2912 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2914 /// Retrieve the nested-name-specifier that qualifies this
2916 NestedNameSpecifier *getQualifier() const {
2917 return QualifierLoc.getNestedNameSpecifier();
2920 /// Retrieve the location of the template keyword preceding
2921 /// this name, if any.
2922 SourceLocation getTemplateKeywordLoc() const {
2923 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2924 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
2927 /// Retrieve the location of the left angle bracket starting the
2928 /// explicit template argument list following the name, if any.
2929 SourceLocation getLAngleLoc() const {
2930 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2931 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
2934 /// Retrieve the location of the right angle bracket ending the
2935 /// explicit template argument list following the name, if any.
2936 SourceLocation getRAngleLoc() const {
2937 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2938 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
2941 /// Determines whether the name was preceded by the template keyword.
2942 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
2944 /// Determines whether this lookup had explicit template arguments.
2945 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
2947 /// Copies the template arguments (if present) into the given
2949 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
2950 if (hasExplicitTemplateArgs())
2951 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
2952 getTrailingObjects<TemplateArgumentLoc>(), List);
2955 TemplateArgumentLoc const *getTemplateArgs() const {
2956 if (!hasExplicitTemplateArgs())
2959 return getTrailingObjects<TemplateArgumentLoc>();
2962 unsigned getNumTemplateArgs() const {
2963 if (!hasExplicitTemplateArgs())
2966 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
2969 ArrayRef<TemplateArgumentLoc> template_arguments() const {
2970 return {getTemplateArgs(), getNumTemplateArgs()};
2973 /// Note: getLocStart() is the start of the whole DependentScopeDeclRefExpr,
2974 /// and differs from getLocation().getStart().
2975 SourceLocation getLocStart() const LLVM_READONLY {
2976 return QualifierLoc.getBeginLoc();
2979 SourceLocation getLocEnd() const LLVM_READONLY {
2980 if (hasExplicitTemplateArgs())
2981 return getRAngleLoc();
2982 return getLocation();
2985 static bool classof(const Stmt *T) {
2986 return T->getStmtClass() == DependentScopeDeclRefExprClass;
2989 child_range children() {
2990 return child_range(child_iterator(), child_iterator());
2994 /// Represents an expression -- generally a full-expression -- that
2995 /// introduces cleanups to be run at the end of the sub-expression's
2996 /// evaluation. The most common source of expression-introduced
2997 /// cleanups is temporary objects in C++, but several other kinds of
2998 /// expressions can create cleanups, including basically every
2999 /// call in ARC that returns an Objective-C pointer.
3001 /// This expression also tracks whether the sub-expression contains a
3002 /// potentially-evaluated block literal. The lifetime of a block
3003 /// literal is the extent of the enclosing scope.
3004 class ExprWithCleanups final
3006 private llvm::TrailingObjects<ExprWithCleanups, BlockDecl *> {
3008 /// The type of objects that are kept in the cleanup.
3009 /// It's useful to remember the set of blocks; we could also
3010 /// remember the set of temporaries, but there's currently
3012 using CleanupObject = BlockDecl *;
3015 friend class ASTStmtReader;
3016 friend TrailingObjects;
3020 ExprWithCleanups(EmptyShell, unsigned NumObjects);
3021 ExprWithCleanups(Expr *SubExpr, bool CleanupsHaveSideEffects,
3022 ArrayRef<CleanupObject> Objects);
3025 static ExprWithCleanups *Create(const ASTContext &C, EmptyShell empty,
3026 unsigned numObjects);
3028 static ExprWithCleanups *Create(const ASTContext &C, Expr *subexpr,
3029 bool CleanupsHaveSideEffects,
3030 ArrayRef<CleanupObject> objects);
3032 ArrayRef<CleanupObject> getObjects() const {
3033 return llvm::makeArrayRef(getTrailingObjects<CleanupObject>(),
3037 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; }
3039 CleanupObject getObject(unsigned i) const {
3040 assert(i < getNumObjects() && "Index out of range");
3041 return getObjects()[i];
3044 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
3045 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
3047 bool cleanupsHaveSideEffects() const {
3048 return ExprWithCleanupsBits.CleanupsHaveSideEffects;
3051 /// As with any mutator of the AST, be very careful
3052 /// when modifying an existing AST to preserve its invariants.
3053 void setSubExpr(Expr *E) { SubExpr = E; }
3055 SourceLocation getLocStart() const LLVM_READONLY {
3056 return SubExpr->getLocStart();
3059 SourceLocation getLocEnd() const LLVM_READONLY { return SubExpr->getLocEnd();}
3061 // Implement isa/cast/dyncast/etc.
3062 static bool classof(const Stmt *T) {
3063 return T->getStmtClass() == ExprWithCleanupsClass;
3067 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
3070 /// Describes an explicit type conversion that uses functional
3071 /// notion but could not be resolved because one or more arguments are
3074 /// The explicit type conversions expressed by
3075 /// CXXUnresolvedConstructExpr have the form <tt>T(a1, a2, ..., aN)</tt>,
3076 /// where \c T is some type and \c a1, \c a2, ..., \c aN are values, and
3077 /// either \c T is a dependent type or one or more of the <tt>a</tt>'s is
3078 /// type-dependent. For example, this would occur in a template such
3082 /// template<typename T, typename A1>
3083 /// inline T make_a(const A1& a1) {
3088 /// When the returned expression is instantiated, it may resolve to a
3089 /// constructor call, conversion function call, or some kind of type
3091 class CXXUnresolvedConstructExpr final
3093 private llvm::TrailingObjects<CXXUnresolvedConstructExpr, Expr *> {
3094 friend class ASTStmtReader;
3095 friend TrailingObjects;
3097 /// The type being constructed.
3098 TypeSourceInfo *Type = nullptr;
3100 /// The location of the left parentheses ('(').
3101 SourceLocation LParenLoc;
3103 /// The location of the right parentheses (')').
3104 SourceLocation RParenLoc;
3106 /// The number of arguments used to construct the type.
3109 CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
3110 SourceLocation LParenLoc,
3111 ArrayRef<Expr*> Args,
3112 SourceLocation RParenLoc);
3114 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs)
3115 : Expr(CXXUnresolvedConstructExprClass, Empty), NumArgs(NumArgs) {}
3118 static CXXUnresolvedConstructExpr *Create(const ASTContext &C,
3119 TypeSourceInfo *Type,
3120 SourceLocation LParenLoc,
3121 ArrayRef<Expr*> Args,
3122 SourceLocation RParenLoc);
3124 static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &C,
3127 /// Retrieve the type that is being constructed, as specified
3128 /// in the source code.
3129 QualType getTypeAsWritten() const { return Type->getType(); }
3131 /// Retrieve the type source information for the type being
3133 TypeSourceInfo *getTypeSourceInfo() const { return Type; }
3135 /// Retrieve the location of the left parentheses ('(') that
3136 /// precedes the argument list.
3137 SourceLocation getLParenLoc() const { return LParenLoc; }
3138 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
3140 /// Retrieve the location of the right parentheses (')') that
3141 /// follows the argument list.
3142 SourceLocation getRParenLoc() const { return RParenLoc; }
3143 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
3145 /// Determine whether this expression models list-initialization.
3146 /// If so, there will be exactly one subexpression, which will be
3147 /// an InitListExpr.
3148 bool isListInitialization() const { return LParenLoc.isInvalid(); }
3150 /// Retrieve the number of arguments.
3151 unsigned arg_size() const { return NumArgs; }
3153 using arg_iterator = Expr **;
3155 arg_iterator arg_begin() { return getTrailingObjects<Expr *>(); }
3156 arg_iterator arg_end() { return arg_begin() + NumArgs; }
3158 using const_arg_iterator = const Expr* const *;
3160 const_arg_iterator arg_begin() const { return getTrailingObjects<Expr *>(); }
3161 const_arg_iterator arg_end() const {
3162 return arg_begin() + NumArgs;
3165 Expr *getArg(unsigned I) {
3166 assert(I < NumArgs && "Argument index out-of-range");
3167 return *(arg_begin() + I);
3170 const Expr *getArg(unsigned I) const {
3171 assert(I < NumArgs && "Argument index out-of-range");
3172 return *(arg_begin() + I);
3175 void setArg(unsigned I, Expr *E) {
3176 assert(I < NumArgs && "Argument index out-of-range");
3177 *(arg_begin() + I) = E;
3180 SourceLocation getLocStart() const LLVM_READONLY;
3182 SourceLocation getLocEnd() const LLVM_READONLY {
3183 if (!RParenLoc.isValid() && NumArgs > 0)
3184 return getArg(NumArgs - 1)->getLocEnd();
3188 static bool classof(const Stmt *T) {
3189 return T->getStmtClass() == CXXUnresolvedConstructExprClass;
3193 child_range children() {
3194 auto **begin = reinterpret_cast<Stmt **>(arg_begin());
3195 return child_range(begin, begin + NumArgs);
3199 /// Represents a C++ member access expression where the actual
3200 /// member referenced could not be resolved because the base
3201 /// expression or the member name was dependent.
3203 /// Like UnresolvedMemberExprs, these can be either implicit or
3204 /// explicit accesses. It is only possible to get one of these with
3205 /// an implicit access if a qualifier is provided.
3206 class CXXDependentScopeMemberExpr final
3208 private llvm::TrailingObjects<CXXDependentScopeMemberExpr,
3209 ASTTemplateKWAndArgsInfo,
3210 TemplateArgumentLoc> {
3211 /// The expression for the base pointer or class reference,
3212 /// e.g., the \c x in x.f. Can be null in implicit accesses.
3215 /// The type of the base expression. Never null, even for
3216 /// implicit accesses.
3219 /// Whether this member expression used the '->' operator or
3220 /// the '.' operator.
3223 /// Whether this member expression has info for explicit template
3224 /// keyword and arguments.
3225 bool HasTemplateKWAndArgsInfo : 1;
3227 /// The location of the '->' or '.' operator.
3228 SourceLocation OperatorLoc;
3230 /// The nested-name-specifier that precedes the member name, if any.
3231 NestedNameSpecifierLoc QualifierLoc;
3233 /// In a qualified member access expression such as t->Base::f, this
3234 /// member stores the resolves of name lookup in the context of the member
3235 /// access expression, to be used at instantiation time.
3237 /// FIXME: This member, along with the QualifierLoc, could
3238 /// be stuck into a structure that is optionally allocated at the end of
3239 /// the CXXDependentScopeMemberExpr, to save space in the common case.
3240 NamedDecl *FirstQualifierFoundInScope;
3242 /// The member to which this member expression refers, which
3243 /// can be name, overloaded operator, or destructor.
3245 /// FIXME: could also be a template-id
3246 DeclarationNameInfo MemberNameInfo;
3248 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3249 return HasTemplateKWAndArgsInfo ? 1 : 0;
3252 CXXDependentScopeMemberExpr(const ASTContext &C, Expr *Base,
3253 QualType BaseType, bool IsArrow,
3254 SourceLocation OperatorLoc,
3255 NestedNameSpecifierLoc QualifierLoc,
3256 SourceLocation TemplateKWLoc,
3257 NamedDecl *FirstQualifierFoundInScope,
3258 DeclarationNameInfo MemberNameInfo,
3259 const TemplateArgumentListInfo *TemplateArgs);
3262 friend class ASTStmtReader;
3263 friend class ASTStmtWriter;
3264 friend TrailingObjects;
3266 CXXDependentScopeMemberExpr(const ASTContext &C, Expr *Base,
3267 QualType BaseType, bool IsArrow,
3268 SourceLocation OperatorLoc,
3269 NestedNameSpecifierLoc QualifierLoc,
3270 NamedDecl *FirstQualifierFoundInScope,
3271 DeclarationNameInfo MemberNameInfo);
3273 static CXXDependentScopeMemberExpr *
3274 Create(const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
3275 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
3276 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
3277 DeclarationNameInfo MemberNameInfo,
3278 const TemplateArgumentListInfo *TemplateArgs);
3280 static CXXDependentScopeMemberExpr *
3281 CreateEmpty(const ASTContext &C, bool HasTemplateKWAndArgsInfo,
3282 unsigned NumTemplateArgs);
3284 /// True if this is an implicit access, i.e. one in which the
3285 /// member being accessed was not written in the source. The source
3286 /// location of the operator is invalid in this case.
3287 bool isImplicitAccess() const;
3289 /// Retrieve the base object of this member expressions,
3290 /// e.g., the \c x in \c x.m.
3291 Expr *getBase() const {
3292 assert(!isImplicitAccess());
3293 return cast<Expr>(Base);
3296 QualType getBaseType() const { return BaseType; }
3298 /// Determine whether this member expression used the '->'
3299 /// operator; otherwise, it used the '.' operator.
3300 bool isArrow() const { return IsArrow; }
3302 /// Retrieve the location of the '->' or '.' operator.
3303 SourceLocation getOperatorLoc() const { return OperatorLoc; }
3305 /// Retrieve the nested-name-specifier that qualifies the member
3307 NestedNameSpecifier *getQualifier() const {
3308 return QualifierLoc.getNestedNameSpecifier();
3311 /// Retrieve the nested-name-specifier that qualifies the member
3312 /// name, with source location information.
3313 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3315 /// Retrieve the first part of the nested-name-specifier that was
3316 /// found in the scope of the member access expression when the member access
3317 /// was initially parsed.
3319 /// This function only returns a useful result when member access expression
3320 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration
3321 /// returned by this function describes what was found by unqualified name
3322 /// lookup for the identifier "Base" within the scope of the member access
3323 /// expression itself. At template instantiation time, this information is
3324 /// combined with the results of name lookup into the type of the object
3325 /// expression itself (the class type of x).
3326 NamedDecl *getFirstQualifierFoundInScope() const {
3327 return FirstQualifierFoundInScope;
3330 /// Retrieve the name of the member that this expression
3332 const DeclarationNameInfo &getMemberNameInfo() const {
3333 return MemberNameInfo;
3336 /// Retrieve the name of the member that this expression
3338 DeclarationName getMember() const { return MemberNameInfo.getName(); }
3340 // Retrieve the location of the name of the member that this
3341 // expression refers to.
3342 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); }
3344 /// Retrieve the location of the template keyword preceding the
3345 /// member name, if any.
3346 SourceLocation getTemplateKeywordLoc() const {
3347 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
3348 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
3351 /// Retrieve the location of the left angle bracket starting the
3352 /// explicit template argument list following the member name, if any.
3353 SourceLocation getLAngleLoc() const {
3354 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
3355 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
3358 /// Retrieve the location of the right angle bracket ending the
3359 /// explicit template argument list following the member name, if any.
3360 SourceLocation getRAngleLoc() const {
3361 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
3362 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
3365 /// Determines whether the member name was preceded by the template keyword.
3366 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
3368 /// Determines whether this member expression actually had a C++
3369 /// template argument list explicitly specified, e.g., x.f<int>.
3370 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3372 /// Copies the template arguments (if present) into the given
3374 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
3375 if (hasExplicitTemplateArgs())
3376 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
3377 getTrailingObjects<TemplateArgumentLoc>(), List);
3380 /// Retrieve the template arguments provided as part of this
3382 const TemplateArgumentLoc *getTemplateArgs() const {
3383 if (!hasExplicitTemplateArgs())
3386 return getTrailingObjects<TemplateArgumentLoc>();
3389 /// Retrieve the number of template arguments provided as part of this
3391 unsigned getNumTemplateArgs() const {
3392 if (!hasExplicitTemplateArgs())
3395 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
3398 ArrayRef<TemplateArgumentLoc> template_arguments() const {
3399 return {getTemplateArgs(), getNumTemplateArgs()};
3402 SourceLocation getLocStart() const LLVM_READONLY {
3403 if (!isImplicitAccess())
3404 return Base->getLocStart();
3406 return getQualifierLoc().getBeginLoc();
3407 return MemberNameInfo.getBeginLoc();
3410 SourceLocation getLocEnd() const LLVM_READONLY {
3411 if (hasExplicitTemplateArgs())
3412 return getRAngleLoc();
3413 return MemberNameInfo.getEndLoc();
3416 static bool classof(const Stmt *T) {
3417 return T->getStmtClass() == CXXDependentScopeMemberExprClass;
3421 child_range children() {
3422 if (isImplicitAccess())
3423 return child_range(child_iterator(), child_iterator());
3424 return child_range(&Base, &Base + 1);
3428 /// Represents a C++ member access expression for which lookup
3429 /// produced a set of overloaded functions.
3431 /// The member access may be explicit or implicit:
3435 /// int explicitAccess() { return this->a + this->A::b; }
3436 /// int implicitAccess() { return a + A::b; }
3440 /// In the final AST, an explicit access always becomes a MemberExpr.
3441 /// An implicit access may become either a MemberExpr or a
3442 /// DeclRefExpr, depending on whether the member is static.
3443 class UnresolvedMemberExpr final
3444 : public OverloadExpr,
3445 private llvm::TrailingObjects<
3446 UnresolvedMemberExpr, ASTTemplateKWAndArgsInfo, TemplateArgumentLoc> {
3447 friend class ASTStmtReader;
3448 friend class OverloadExpr;
3449 friend TrailingObjects;
3451 /// Whether this member expression used the '->' operator or
3452 /// the '.' operator.
3455 /// Whether the lookup results contain an unresolved using
3457 bool HasUnresolvedUsing : 1;
3459 /// The expression for the base pointer or class reference,
3460 /// e.g., the \c x in x.f.
3462 /// This can be null if this is an 'unbased' member expression.
3463 Stmt *Base = nullptr;
3465 /// The type of the base expression; never null.
3468 /// The location of the '->' or '.' operator.
3469 SourceLocation OperatorLoc;
3471 UnresolvedMemberExpr(const ASTContext &C, bool HasUnresolvedUsing,
3472 Expr *Base, QualType BaseType, bool IsArrow,
3473 SourceLocation OperatorLoc,
3474 NestedNameSpecifierLoc QualifierLoc,
3475 SourceLocation TemplateKWLoc,
3476 const DeclarationNameInfo &MemberNameInfo,
3477 const TemplateArgumentListInfo *TemplateArgs,
3478 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3480 UnresolvedMemberExpr(EmptyShell Empty)
3481 : OverloadExpr(UnresolvedMemberExprClass, Empty), IsArrow(false),
3482 HasUnresolvedUsing(false) {}
3484 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3485 return HasTemplateKWAndArgsInfo ? 1 : 0;
3489 static UnresolvedMemberExpr *
3490 Create(const ASTContext &C, bool HasUnresolvedUsing,
3491 Expr *Base, QualType BaseType, bool IsArrow,
3492 SourceLocation OperatorLoc,
3493 NestedNameSpecifierLoc QualifierLoc,
3494 SourceLocation TemplateKWLoc,
3495 const DeclarationNameInfo &MemberNameInfo,
3496 const TemplateArgumentListInfo *TemplateArgs,
3497 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3499 static UnresolvedMemberExpr *
3500 CreateEmpty(const ASTContext &C, bool HasTemplateKWAndArgsInfo,
3501 unsigned NumTemplateArgs);
3503 /// True if this is an implicit access, i.e., one in which the
3504 /// member being accessed was not written in the source.
3506 /// The source location of the operator is invalid in this case.
3507 bool isImplicitAccess() const;
3509 /// Retrieve the base object of this member expressions,
3510 /// e.g., the \c x in \c x.m.
3512 assert(!isImplicitAccess());
3513 return cast<Expr>(Base);
3515 const Expr *getBase() const {
3516 assert(!isImplicitAccess());
3517 return cast<Expr>(Base);
3520 QualType getBaseType() const { return BaseType; }
3522 /// Determine whether the lookup results contain an unresolved using
3524 bool hasUnresolvedUsing() const { return HasUnresolvedUsing; }
3526 /// Determine whether this member expression used the '->'
3527 /// operator; otherwise, it used the '.' operator.
3528 bool isArrow() const { return IsArrow; }
3530 /// Retrieve the location of the '->' or '.' operator.
3531 SourceLocation getOperatorLoc() const { return OperatorLoc; }
3533 /// Retrieve the naming class of this lookup.
3534 CXXRecordDecl *getNamingClass() const;
3536 /// Retrieve the full name info for the member that this expression
3538 const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); }
3540 /// Retrieve the name of the member that this expression
3542 DeclarationName getMemberName() const { return getName(); }
3544 // Retrieve the location of the name of the member that this
3545 // expression refers to.
3546 SourceLocation getMemberLoc() const { return getNameLoc(); }
3548 // Return the preferred location (the member name) for the arrow when
3549 // diagnosing a problem with this expression.
3550 SourceLocation getExprLoc() const LLVM_READONLY { return getMemberLoc(); }
3552 SourceLocation getLocStart() const LLVM_READONLY {
3553 if (!isImplicitAccess())
3554 return Base->getLocStart();
3555 if (NestedNameSpecifierLoc l = getQualifierLoc())
3556 return l.getBeginLoc();
3557 return getMemberNameInfo().getLocStart();
3560 SourceLocation getLocEnd() const LLVM_READONLY {
3561 if (hasExplicitTemplateArgs())
3562 return getRAngleLoc();
3563 return getMemberNameInfo().getLocEnd();
3566 static bool classof(const Stmt *T) {
3567 return T->getStmtClass() == UnresolvedMemberExprClass;
3571 child_range children() {
3572 if (isImplicitAccess())
3573 return child_range(child_iterator(), child_iterator());
3574 return child_range(&Base, &Base + 1);
3578 inline ASTTemplateKWAndArgsInfo *
3579 OverloadExpr::getTrailingASTTemplateKWAndArgsInfo() {
3580 if (!HasTemplateKWAndArgsInfo)
3583 if (isa<UnresolvedLookupExpr>(this))
3584 return cast<UnresolvedLookupExpr>(this)
3585 ->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
3587 return cast<UnresolvedMemberExpr>(this)
3588 ->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
3591 inline TemplateArgumentLoc *OverloadExpr::getTrailingTemplateArgumentLoc() {
3592 if (isa<UnresolvedLookupExpr>(this))
3593 return cast<UnresolvedLookupExpr>(this)
3594 ->getTrailingObjects<TemplateArgumentLoc>();
3596 return cast<UnresolvedMemberExpr>(this)
3597 ->getTrailingObjects<TemplateArgumentLoc>();
3600 /// Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
3602 /// The noexcept expression tests whether a given expression might throw. Its
3603 /// result is a boolean constant.
3604 class CXXNoexceptExpr : public Expr {
3605 friend class ASTStmtReader;
3612 CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val,
3613 SourceLocation Keyword, SourceLocation RParen)
3614 : Expr(CXXNoexceptExprClass, Ty, VK_RValue, OK_Ordinary,
3615 /*TypeDependent*/false,
3616 /*ValueDependent*/Val == CT_Dependent,
3617 Val == CT_Dependent || Operand->isInstantiationDependent(),
3618 Operand->containsUnexpandedParameterPack()),
3619 Value(Val == CT_Cannot), Operand(Operand), Range(Keyword, RParen) {}
3621 CXXNoexceptExpr(EmptyShell Empty) : Expr(CXXNoexceptExprClass, Empty) {}
3623 Expr *getOperand() const { return static_cast<Expr*>(Operand); }
3625 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
3626 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
3627 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
3629 bool getValue() const { return Value; }
3631 static bool classof(const Stmt *T) {
3632 return T->getStmtClass() == CXXNoexceptExprClass;
3636 child_range children() { return child_range(&Operand, &Operand + 1); }
3639 /// Represents a C++11 pack expansion that produces a sequence of
3642 /// A pack expansion expression contains a pattern (which itself is an
3643 /// expression) followed by an ellipsis. For example:
3646 /// template<typename F, typename ...Types>
3647 /// void forward(F f, Types &&...args) {
3648 /// f(static_cast<Types&&>(args)...);
3652 /// Here, the argument to the function object \c f is a pack expansion whose
3653 /// pattern is \c static_cast<Types&&>(args). When the \c forward function
3654 /// template is instantiated, the pack expansion will instantiate to zero or
3655 /// or more function arguments to the function object \c f.
3656 class PackExpansionExpr : public Expr {
3657 friend class ASTStmtReader;
3658 friend class ASTStmtWriter;
3660 SourceLocation EllipsisLoc;
3662 /// The number of expansions that will be produced by this pack
3663 /// expansion expression, if known.
3665 /// When zero, the number of expansions is not known. Otherwise, this value
3666 /// is the number of expansions + 1.
3667 unsigned NumExpansions;
3672 PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc,
3673 Optional<unsigned> NumExpansions)
3674 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
3675 Pattern->getObjectKind(), /*TypeDependent=*/true,
3676 /*ValueDependent=*/true, /*InstantiationDependent=*/true,
3677 /*ContainsUnexpandedParameterPack=*/false),
3678 EllipsisLoc(EllipsisLoc),
3679 NumExpansions(NumExpansions ? *NumExpansions + 1 : 0),
3682 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) {}
3684 /// Retrieve the pattern of the pack expansion.
3685 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); }
3687 /// Retrieve the pattern of the pack expansion.
3688 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); }
3690 /// Retrieve the location of the ellipsis that describes this pack
3692 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
3694 /// Determine the number of expansions that will be produced when
3695 /// this pack expansion is instantiated, if already known.
3696 Optional<unsigned> getNumExpansions() const {
3698 return NumExpansions - 1;
3703 SourceLocation getLocStart() const LLVM_READONLY {
3704 return Pattern->getLocStart();
3707 SourceLocation getLocEnd() const LLVM_READONLY { return EllipsisLoc; }
3709 static bool classof(const Stmt *T) {
3710 return T->getStmtClass() == PackExpansionExprClass;
3714 child_range children() {
3715 return child_range(&Pattern, &Pattern + 1);
3719 /// Represents an expression that computes the length of a parameter
3723 /// template<typename ...Types>
3725 /// static const unsigned value = sizeof...(Types);
3728 class SizeOfPackExpr final
3730 private llvm::TrailingObjects<SizeOfPackExpr, TemplateArgument> {
3731 friend class ASTStmtReader;
3732 friend class ASTStmtWriter;
3733 friend TrailingObjects;
3735 /// The location of the \c sizeof keyword.
3736 SourceLocation OperatorLoc;
3738 /// The location of the name of the parameter pack.
3739 SourceLocation PackLoc;
3741 /// The location of the closing parenthesis.
3742 SourceLocation RParenLoc;
3744 /// The length of the parameter pack, if known.
3746 /// When this expression is not value-dependent, this is the length of
3747 /// the pack. When the expression was parsed rather than instantiated
3748 /// (and thus is value-dependent), this is zero.
3750 /// After partial substitution into a sizeof...(X) expression (for instance,
3751 /// within an alias template or during function template argument deduction),
3752 /// we store a trailing array of partially-substituted TemplateArguments,
3753 /// and this is the length of that array.
3756 /// The parameter pack.
3757 NamedDecl *Pack = nullptr;
3759 /// Create an expression that computes the length of
3760 /// the given parameter pack.
3761 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack,
3762 SourceLocation PackLoc, SourceLocation RParenLoc,
3763 Optional<unsigned> Length, ArrayRef<TemplateArgument> PartialArgs)
3764 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary,
3765 /*TypeDependent=*/false, /*ValueDependent=*/!Length,
3766 /*InstantiationDependent=*/!Length,
3767 /*ContainsUnexpandedParameterPack=*/false),
3768 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc),
3769 Length(Length ? *Length : PartialArgs.size()), Pack(Pack) {
3770 assert((!Length || PartialArgs.empty()) &&
3771 "have partial args for non-dependent sizeof... expression");
3772 auto *Args = getTrailingObjects<TemplateArgument>();
3773 std::uninitialized_copy(PartialArgs.begin(), PartialArgs.end(), Args);
3776 /// Create an empty expression.
3777 SizeOfPackExpr(EmptyShell Empty, unsigned NumPartialArgs)
3778 : Expr(SizeOfPackExprClass, Empty), Length(NumPartialArgs) {}
3781 static SizeOfPackExpr *Create(ASTContext &Context, SourceLocation OperatorLoc,
3782 NamedDecl *Pack, SourceLocation PackLoc,
3783 SourceLocation RParenLoc,
3784 Optional<unsigned> Length = None,
3785 ArrayRef<TemplateArgument> PartialArgs = None);
3786 static SizeOfPackExpr *CreateDeserialized(ASTContext &Context,
3787 unsigned NumPartialArgs);
3789 /// Determine the location of the 'sizeof' keyword.
3790 SourceLocation getOperatorLoc() const { return OperatorLoc; }
3792 /// Determine the location of the parameter pack.
3793 SourceLocation getPackLoc() const { return PackLoc; }
3795 /// Determine the location of the right parenthesis.
3796 SourceLocation getRParenLoc() const { return RParenLoc; }
3798 /// Retrieve the parameter pack.
3799 NamedDecl *getPack() const { return Pack; }
3801 /// Retrieve the length of the parameter pack.
3803 /// This routine may only be invoked when the expression is not
3804 /// value-dependent.
3805 unsigned getPackLength() const {
3806 assert(!isValueDependent() &&
3807 "Cannot get the length of a value-dependent pack size expression");
3811 /// Determine whether this represents a partially-substituted sizeof...
3812 /// expression, such as is produced for:
3814 /// template<typename ...Ts> using X = int[sizeof...(Ts)];
3815 /// template<typename ...Us> void f(X<Us..., 1, 2, 3, Us...>);
3816 bool isPartiallySubstituted() const {
3817 return isValueDependent() && Length;
3821 ArrayRef<TemplateArgument> getPartialArguments() const {
3822 assert(isPartiallySubstituted());
3823 const auto *Args = getTrailingObjects<TemplateArgument>();
3824 return llvm::makeArrayRef(Args, Args + Length);
3827 SourceLocation getLocStart() const LLVM_READONLY { return OperatorLoc; }
3828 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
3830 static bool classof(const Stmt *T) {
3831 return T->getStmtClass() == SizeOfPackExprClass;
3835 child_range children() {
3836 return child_range(child_iterator(), child_iterator());
3840 /// Represents a reference to a non-type template parameter
3841 /// that has been substituted with a template argument.
3842 class SubstNonTypeTemplateParmExpr : public Expr {
3843 friend class ASTReader;
3844 friend class ASTStmtReader;
3846 /// The replaced parameter.
3847 NonTypeTemplateParmDecl *Param;
3849 /// The replacement expression.
3852 /// The location of the non-type template parameter reference.
3853 SourceLocation NameLoc;
3855 explicit SubstNonTypeTemplateParmExpr(EmptyShell Empty)
3856 : Expr(SubstNonTypeTemplateParmExprClass, Empty) {}
3859 SubstNonTypeTemplateParmExpr(QualType type,
3860 ExprValueKind valueKind,
3862 NonTypeTemplateParmDecl *param,
3864 : Expr(SubstNonTypeTemplateParmExprClass, type, valueKind, OK_Ordinary,
3865 replacement->isTypeDependent(), replacement->isValueDependent(),
3866 replacement->isInstantiationDependent(),
3867 replacement->containsUnexpandedParameterPack()),
3868 Param(param), Replacement(replacement), NameLoc(loc) {}
3870 SourceLocation getNameLoc() const { return NameLoc; }
3871 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; }
3872 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; }
3874 Expr *getReplacement() const { return cast<Expr>(Replacement); }
3876 NonTypeTemplateParmDecl *getParameter() const { return Param; }
3878 static bool classof(const Stmt *s) {
3879 return s->getStmtClass() == SubstNonTypeTemplateParmExprClass;
3883 child_range children() { return child_range(&Replacement, &Replacement+1); }
3886 /// Represents a reference to a non-type template parameter pack that
3887 /// has been substituted with a non-template argument pack.
3889 /// When a pack expansion in the source code contains multiple parameter packs
3890 /// and those parameter packs correspond to different levels of template
3891 /// parameter lists, this node is used to represent a non-type template
3892 /// parameter pack from an outer level, which has already had its argument pack
3893 /// substituted but that still lives within a pack expansion that itself
3894 /// could not be instantiated. When actually performing a substitution into
3895 /// that pack expansion (e.g., when all template parameters have corresponding
3896 /// arguments), this type will be replaced with the appropriate underlying
3897 /// expression at the current pack substitution index.
3898 class SubstNonTypeTemplateParmPackExpr : public Expr {
3899 friend class ASTReader;
3900 friend class ASTStmtReader;
3902 /// The non-type template parameter pack itself.
3903 NonTypeTemplateParmDecl *Param;
3905 /// A pointer to the set of template arguments that this
3906 /// parameter pack is instantiated with.
3907 const TemplateArgument *Arguments;
3909 /// The number of template arguments in \c Arguments.
3910 unsigned NumArguments;
3912 /// The location of the non-type template parameter pack reference.
3913 SourceLocation NameLoc;
3915 explicit SubstNonTypeTemplateParmPackExpr(EmptyShell Empty)
3916 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) {}
3919 SubstNonTypeTemplateParmPackExpr(QualType T,
3920 ExprValueKind ValueKind,
3921 NonTypeTemplateParmDecl *Param,
3922 SourceLocation NameLoc,
3923 const TemplateArgument &ArgPack);
3925 /// Retrieve the non-type template parameter pack being substituted.
3926 NonTypeTemplateParmDecl *getParameterPack() const { return Param; }
3928 /// Retrieve the location of the parameter pack name.
3929 SourceLocation getParameterPackLocation() const { return NameLoc; }
3931 /// Retrieve the template argument pack containing the substituted
3932 /// template arguments.
3933 TemplateArgument getArgumentPack() const;
3935 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; }
3936 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; }
3938 static bool classof(const Stmt *T) {
3939 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass;
3943 child_range children() {
3944 return child_range(child_iterator(), child_iterator());
3948 /// Represents a reference to a function parameter pack that has been
3949 /// substituted but not yet expanded.
3951 /// When a pack expansion contains multiple parameter packs at different levels,
3952 /// this node is used to represent a function parameter pack at an outer level
3953 /// which we have already substituted to refer to expanded parameters, but where
3954 /// the containing pack expansion cannot yet be expanded.
3957 /// template<typename...Ts> struct S {
3958 /// template<typename...Us> auto f(Ts ...ts) -> decltype(g(Us(ts)...));
3960 /// template struct S<int, int>;
3962 class FunctionParmPackExpr final
3964 private llvm::TrailingObjects<FunctionParmPackExpr, ParmVarDecl *> {
3965 friend class ASTReader;
3966 friend class ASTStmtReader;
3967 friend TrailingObjects;
3969 /// The function parameter pack which was referenced.
3970 ParmVarDecl *ParamPack;
3972 /// The location of the function parameter pack reference.
3973 SourceLocation NameLoc;
3975 /// The number of expansions of this pack.
3976 unsigned NumParameters;
3978 FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
3979 SourceLocation NameLoc, unsigned NumParams,
3980 ParmVarDecl *const *Params);
3983 static FunctionParmPackExpr *Create(const ASTContext &Context, QualType T,
3984 ParmVarDecl *ParamPack,
3985 SourceLocation NameLoc,
3986 ArrayRef<ParmVarDecl *> Params);
3987 static FunctionParmPackExpr *CreateEmpty(const ASTContext &Context,
3988 unsigned NumParams);
3990 /// Get the parameter pack which this expression refers to.
3991 ParmVarDecl *getParameterPack() const { return ParamPack; }
3993 /// Get the location of the parameter pack.
3994 SourceLocation getParameterPackLocation() const { return NameLoc; }
3996 /// Iterators over the parameters which the parameter pack expanded
3998 using iterator = ParmVarDecl * const *;
3999 iterator begin() const { return getTrailingObjects<ParmVarDecl *>(); }
4000 iterator end() const { return begin() + NumParameters; }
4002 /// Get the number of parameters in this parameter pack.
4003 unsigned getNumExpansions() const { return NumParameters; }
4005 /// Get an expansion of the parameter pack by index.
4006 ParmVarDecl *getExpansion(unsigned I) const { return begin()[I]; }
4008 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; }
4009 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; }
4011 static bool classof(const Stmt *T) {
4012 return T->getStmtClass() == FunctionParmPackExprClass;
4015 child_range children() {
4016 return child_range(child_iterator(), child_iterator());
4020 /// Represents a prvalue temporary that is written into memory so that
4021 /// a reference can bind to it.
4023 /// Prvalue expressions are materialized when they need to have an address
4024 /// in memory for a reference to bind to. This happens when binding a
4025 /// reference to the result of a conversion, e.g.,
4028 /// const int &r = 1.0;
4031 /// Here, 1.0 is implicitly converted to an \c int. That resulting \c int is
4032 /// then materialized via a \c MaterializeTemporaryExpr, and the reference
4033 /// binds to the temporary. \c MaterializeTemporaryExprs are always glvalues
4034 /// (either an lvalue or an xvalue, depending on the kind of reference binding
4035 /// to it), maintaining the invariant that references always bind to glvalues.
4037 /// Reference binding and copy-elision can both extend the lifetime of a
4038 /// temporary. When either happens, the expression will also track the
4039 /// declaration which is responsible for the lifetime extension.
4040 class MaterializeTemporaryExpr : public Expr {
4042 friend class ASTStmtReader;
4043 friend class ASTStmtWriter;
4046 /// The temporary-generating expression whose value will be
4050 /// The declaration which lifetime-extended this reference, if any.
4051 /// Either a VarDecl, or (for a ctor-initializer) a FieldDecl.
4052 const ValueDecl *ExtendingDecl;
4054 unsigned ManglingNumber;
4056 llvm::PointerUnion<Stmt *, ExtraState *> State;
4058 void initializeExtraState(const ValueDecl *ExtendedBy,
4059 unsigned ManglingNumber);
4062 MaterializeTemporaryExpr(QualType T, Expr *Temporary,
4063 bool BoundToLvalueReference)
4064 : Expr(MaterializeTemporaryExprClass, T,
4065 BoundToLvalueReference? VK_LValue : VK_XValue, OK_Ordinary,
4066 Temporary->isTypeDependent(), Temporary->isValueDependent(),
4067 Temporary->isInstantiationDependent(),
4068 Temporary->containsUnexpandedParameterPack()),
4071 MaterializeTemporaryExpr(EmptyShell Empty)
4072 : Expr(MaterializeTemporaryExprClass, Empty) {}
4074 Stmt *getTemporary() const {
4075 return State.is<Stmt *>() ? State.get<Stmt *>()
4076 : State.get<ExtraState *>()->Temporary;
4079 /// Retrieve the temporary-generating subexpression whose value will
4080 /// be materialized into a glvalue.
4081 Expr *GetTemporaryExpr() const { return static_cast<Expr *>(getTemporary()); }
4083 /// Retrieve the storage duration for the materialized temporary.
4084 StorageDuration getStorageDuration() const {
4085 const ValueDecl *ExtendingDecl = getExtendingDecl();
4087 return SD_FullExpression;
4088 // FIXME: This is not necessarily correct for a temporary materialized
4089 // within a default initializer.
4090 if (isa<FieldDecl>(ExtendingDecl))
4091 return SD_Automatic;
4092 // FIXME: This only works because storage class specifiers are not allowed
4093 // on decomposition declarations.
4094 if (isa<BindingDecl>(ExtendingDecl))
4095 return ExtendingDecl->getDeclContext()->isFunctionOrMethod()
4098 return cast<VarDecl>(ExtendingDecl)->getStorageDuration();
4101 /// Get the declaration which triggered the lifetime-extension of this
4102 /// temporary, if any.
4103 const ValueDecl *getExtendingDecl() const {
4104 return State.is<Stmt *>() ? nullptr
4105 : State.get<ExtraState *>()->ExtendingDecl;
4108 void setExtendingDecl(const ValueDecl *ExtendedBy, unsigned ManglingNumber);
4110 unsigned getManglingNumber() const {
4111 return State.is<Stmt *>() ? 0 : State.get<ExtraState *>()->ManglingNumber;
4114 /// Determine whether this materialized temporary is bound to an
4115 /// lvalue reference; otherwise, it's bound to an rvalue reference.
4116 bool isBoundToLvalueReference() const {
4117 return getValueKind() == VK_LValue;
4120 SourceLocation getLocStart() const LLVM_READONLY {
4121 return getTemporary()->getLocStart();
4124 SourceLocation getLocEnd() const LLVM_READONLY {
4125 return getTemporary()->getLocEnd();
4128 static bool classof(const Stmt *T) {
4129 return T->getStmtClass() == MaterializeTemporaryExprClass;
4133 child_range children() {
4134 if (State.is<Stmt *>())
4135 return child_range(State.getAddrOfPtr1(), State.getAddrOfPtr1() + 1);
4137 auto ES = State.get<ExtraState *>();
4138 return child_range(&ES->Temporary, &ES->Temporary + 1);
4142 /// Represents a folding of a pack over an operator.
4144 /// This expression is always dependent and represents a pack expansion of the
4149 /// ( expr op ... op expr )
4150 class CXXFoldExpr : public Expr {
4151 friend class ASTStmtReader;
4152 friend class ASTStmtWriter;
4154 SourceLocation LParenLoc;
4155 SourceLocation EllipsisLoc;
4156 SourceLocation RParenLoc;
4158 BinaryOperatorKind Opcode;
4161 CXXFoldExpr(QualType T, SourceLocation LParenLoc, Expr *LHS,
4162 BinaryOperatorKind Opcode, SourceLocation EllipsisLoc, Expr *RHS,
4163 SourceLocation RParenLoc)
4164 : Expr(CXXFoldExprClass, T, VK_RValue, OK_Ordinary,
4165 /*Dependent*/ true, true, true,
4166 /*ContainsUnexpandedParameterPack*/ false),
4167 LParenLoc(LParenLoc), EllipsisLoc(EllipsisLoc), RParenLoc(RParenLoc),
4173 CXXFoldExpr(EmptyShell Empty) : Expr(CXXFoldExprClass, Empty) {}
4175 Expr *getLHS() const { return static_cast<Expr*>(SubExprs[0]); }
4176 Expr *getRHS() const { return static_cast<Expr*>(SubExprs[1]); }
4178 /// Does this produce a right-associated sequence of operators?
4179 bool isRightFold() const {
4180 return getLHS() && getLHS()->containsUnexpandedParameterPack();
4183 /// Does this produce a left-associated sequence of operators?
4184 bool isLeftFold() const { return !isRightFold(); }
4186 /// Get the pattern, that is, the operand that contains an unexpanded pack.
4187 Expr *getPattern() const { return isLeftFold() ? getRHS() : getLHS(); }
4189 /// Get the operand that doesn't contain a pack, for a binary fold.
4190 Expr *getInit() const { return isLeftFold() ? getLHS() : getRHS(); }
4192 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
4193 BinaryOperatorKind getOperator() const { return Opcode; }
4195 SourceLocation getLocStart() const LLVM_READONLY {
4199 SourceLocation getLocEnd() const LLVM_READONLY {
4203 static bool classof(const Stmt *T) {
4204 return T->getStmtClass() == CXXFoldExprClass;
4208 child_range children() { return child_range(SubExprs, SubExprs + 2); }
4211 /// Represents an expression that might suspend coroutine execution;
4212 /// either a co_await or co_yield expression.
4214 /// Evaluation of this expression first evaluates its 'ready' expression. If
4215 /// that returns 'false':
4216 /// -- execution of the coroutine is suspended
4217 /// -- the 'suspend' expression is evaluated
4218 /// -- if the 'suspend' expression returns 'false', the coroutine is
4220 /// -- otherwise, control passes back to the resumer.
4221 /// If the coroutine is not suspended, or when it is resumed, the 'resume'
4222 /// expression is evaluated, and its result is the result of the overall
4224 class CoroutineSuspendExpr : public Expr {
4225 friend class ASTStmtReader;
4227 SourceLocation KeywordLoc;
4229 enum SubExpr { Common, Ready, Suspend, Resume, Count };
4231 Stmt *SubExprs[SubExpr::Count];
4232 OpaqueValueExpr *OpaqueValue = nullptr;
4235 CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, Expr *Common,
4236 Expr *Ready, Expr *Suspend, Expr *Resume,
4237 OpaqueValueExpr *OpaqueValue)
4238 : Expr(SC, Resume->getType(), Resume->getValueKind(),
4239 Resume->getObjectKind(), Resume->isTypeDependent(),
4240 Resume->isValueDependent(), Common->isInstantiationDependent(),
4241 Common->containsUnexpandedParameterPack()),
4242 KeywordLoc(KeywordLoc), OpaqueValue(OpaqueValue) {
4243 SubExprs[SubExpr::Common] = Common;
4244 SubExprs[SubExpr::Ready] = Ready;
4245 SubExprs[SubExpr::Suspend] = Suspend;
4246 SubExprs[SubExpr::Resume] = Resume;
4249 CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, QualType Ty,
4251 : Expr(SC, Ty, VK_RValue, OK_Ordinary, true, true, true,
4252 Common->containsUnexpandedParameterPack()),
4253 KeywordLoc(KeywordLoc) {
4254 assert(Common->isTypeDependent() && Ty->isDependentType() &&
4255 "wrong constructor for non-dependent co_await/co_yield expression");
4256 SubExprs[SubExpr::Common] = Common;
4257 SubExprs[SubExpr::Ready] = nullptr;
4258 SubExprs[SubExpr::Suspend] = nullptr;
4259 SubExprs[SubExpr::Resume] = nullptr;
4262 CoroutineSuspendExpr(StmtClass SC, EmptyShell Empty) : Expr(SC, Empty) {
4263 SubExprs[SubExpr::Common] = nullptr;
4264 SubExprs[SubExpr::Ready] = nullptr;
4265 SubExprs[SubExpr::Suspend] = nullptr;
4266 SubExprs[SubExpr::Resume] = nullptr;
4269 SourceLocation getKeywordLoc() const { return KeywordLoc; }
4271 Expr *getCommonExpr() const {
4272 return static_cast<Expr*>(SubExprs[SubExpr::Common]);
4275 /// getOpaqueValue - Return the opaque value placeholder.
4276 OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; }
4278 Expr *getReadyExpr() const {
4279 return static_cast<Expr*>(SubExprs[SubExpr::Ready]);
4282 Expr *getSuspendExpr() const {
4283 return static_cast<Expr*>(SubExprs[SubExpr::Suspend]);
4286 Expr *getResumeExpr() const {
4287 return static_cast<Expr*>(SubExprs[SubExpr::Resume]);
4290 SourceLocation getLocStart() const LLVM_READONLY {
4294 SourceLocation getLocEnd() const LLVM_READONLY {
4295 return getCommonExpr()->getLocEnd();
4298 child_range children() {
4299 return child_range(SubExprs, SubExprs + SubExpr::Count);
4302 static bool classof(const Stmt *T) {
4303 return T->getStmtClass() == CoawaitExprClass ||
4304 T->getStmtClass() == CoyieldExprClass;
4308 /// Represents a 'co_await' expression.
4309 class CoawaitExpr : public CoroutineSuspendExpr {
4310 friend class ASTStmtReader;
4313 CoawaitExpr(SourceLocation CoawaitLoc, Expr *Operand, Expr *Ready,
4314 Expr *Suspend, Expr *Resume, OpaqueValueExpr *OpaqueValue,
4315 bool IsImplicit = false)
4316 : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Operand, Ready,
4317 Suspend, Resume, OpaqueValue) {
4318 CoawaitBits.IsImplicit = IsImplicit;
4321 CoawaitExpr(SourceLocation CoawaitLoc, QualType Ty, Expr *Operand,
4322 bool IsImplicit = false)
4323 : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Ty, Operand) {
4324 CoawaitBits.IsImplicit = IsImplicit;
4327 CoawaitExpr(EmptyShell Empty)
4328 : CoroutineSuspendExpr(CoawaitExprClass, Empty) {}
4330 Expr *getOperand() const {
4331 // FIXME: Dig out the actual operand or store it.
4332 return getCommonExpr();
4335 bool isImplicit() const { return CoawaitBits.IsImplicit; }
4336 void setIsImplicit(bool value = true) { CoawaitBits.IsImplicit = value; }
4338 static bool classof(const Stmt *T) {
4339 return T->getStmtClass() == CoawaitExprClass;
4343 /// Represents a 'co_await' expression while the type of the promise
4345 class DependentCoawaitExpr : public Expr {
4346 friend class ASTStmtReader;
4348 SourceLocation KeywordLoc;
4352 DependentCoawaitExpr(SourceLocation KeywordLoc, QualType Ty, Expr *Op,
4353 UnresolvedLookupExpr *OpCoawait)
4354 : Expr(DependentCoawaitExprClass, Ty, VK_RValue, OK_Ordinary,
4355 /*TypeDependent*/ true, /*ValueDependent*/ true,
4356 /*InstantiationDependent*/ true,
4357 Op->containsUnexpandedParameterPack()),
4358 KeywordLoc(KeywordLoc) {
4359 // NOTE: A co_await expression is dependent on the coroutines promise
4360 // type and may be dependent even when the `Op` expression is not.
4361 assert(Ty->isDependentType() &&
4362 "wrong constructor for non-dependent co_await/co_yield expression");
4364 SubExprs[1] = OpCoawait;
4367 DependentCoawaitExpr(EmptyShell Empty)
4368 : Expr(DependentCoawaitExprClass, Empty) {}
4370 Expr *getOperand() const { return cast<Expr>(SubExprs[0]); }
4372 UnresolvedLookupExpr *getOperatorCoawaitLookup() const {
4373 return cast<UnresolvedLookupExpr>(SubExprs[1]);
4376 SourceLocation getKeywordLoc() const { return KeywordLoc; }
4378 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
4380 SourceLocation getLocEnd() const LLVM_READONLY {
4381 return getOperand()->getLocEnd();
4384 child_range children() { return child_range(SubExprs, SubExprs + 2); }
4386 static bool classof(const Stmt *T) {
4387 return T->getStmtClass() == DependentCoawaitExprClass;
4391 /// Represents a 'co_yield' expression.
4392 class CoyieldExpr : public CoroutineSuspendExpr {
4393 friend class ASTStmtReader;
4396 CoyieldExpr(SourceLocation CoyieldLoc, Expr *Operand, Expr *Ready,
4397 Expr *Suspend, Expr *Resume, OpaqueValueExpr *OpaqueValue)
4398 : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Operand, Ready,
4399 Suspend, Resume, OpaqueValue) {}
4400 CoyieldExpr(SourceLocation CoyieldLoc, QualType Ty, Expr *Operand)
4401 : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Ty, Operand) {}
4402 CoyieldExpr(EmptyShell Empty)
4403 : CoroutineSuspendExpr(CoyieldExprClass, Empty) {}
4405 Expr *getOperand() const {
4406 // FIXME: Dig out the actual operand or store it.
4407 return getCommonExpr();
4410 static bool classof(const Stmt *T) {
4411 return T->getStmtClass() == CoyieldExprClass;
4415 } // namespace clang
4417 #endif // LLVM_CLANG_AST_EXPRCXX_H