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 /// \brief 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/DeclCXX.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/LambdaCapture.h"
22 #include "clang/AST/TemplateBase.h"
23 #include "clang/AST/UnresolvedSet.h"
24 #include "clang/Basic/ExpressionTraits.h"
25 #include "clang/Basic/TypeTraits.h"
26 #include "llvm/Support/Compiler.h"
32 class TemplateArgumentListInfo;
35 //===--------------------------------------------------------------------===//
37 //===--------------------------------------------------------------------===//
39 /// \brief A call to an overloaded operator written using operator
42 /// Represents a call to an overloaded operator written using operator
43 /// syntax, e.g., "x + y" or "*p". While semantically equivalent to a
44 /// normal call, this AST node provides better information about the
45 /// syntactic representation of the call.
47 /// In a C++ template, this expression node kind will be used whenever
48 /// any of the arguments are type-dependent. In this case, the
49 /// function itself will be a (possibly empty) set of functions and
50 /// function templates that were found by name lookup at template
52 class CXXOperatorCallExpr : public CallExpr {
53 /// \brief The overloaded operator.
54 OverloadedOperatorKind Operator;
57 // Record the FP_CONTRACT state that applies to this operator call. Only
58 // meaningful for floating point types. For other types this value can be
60 unsigned FPContractable : 1;
62 SourceRange getSourceRangeImpl() const LLVM_READONLY;
64 CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn,
65 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
66 SourceLocation operatorloc, bool fpContractable)
67 : CallExpr(C, CXXOperatorCallExprClass, fn, args, t, VK, operatorloc),
68 Operator(Op), FPContractable(fpContractable) {
69 Range = getSourceRangeImpl();
71 explicit CXXOperatorCallExpr(ASTContext& C, EmptyShell Empty) :
72 CallExpr(C, CXXOperatorCallExprClass, Empty) { }
75 /// \brief Returns the kind of overloaded operator that this
76 /// expression refers to.
77 OverloadedOperatorKind getOperator() const { return Operator; }
79 /// \brief Returns the location of the operator symbol in the expression.
81 /// When \c getOperator()==OO_Call, this is the location of the right
82 /// parentheses; when \c getOperator()==OO_Subscript, this is the location
83 /// of the right bracket.
84 SourceLocation getOperatorLoc() const { return getRParenLoc(); }
86 SourceLocation getExprLoc() const LLVM_READONLY {
87 return (Operator < OO_Plus || Operator >= OO_Arrow ||
88 Operator == OO_PlusPlus || Operator == OO_MinusMinus)
93 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
94 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
95 SourceRange getSourceRange() const { return Range; }
97 static bool classof(const Stmt *T) {
98 return T->getStmtClass() == CXXOperatorCallExprClass;
101 // Set the FP contractability status of this operator. Only meaningful for
102 // operations on floating point types.
103 void setFPContractable(bool FPC) { FPContractable = FPC; }
105 // Get the FP contractability status of this operator. Only meaningful for
106 // operations on floating point types.
107 bool isFPContractable() const { return FPContractable; }
109 friend class ASTStmtReader;
110 friend class ASTStmtWriter;
113 /// Represents a call to a member function that
114 /// may be written either with member call syntax (e.g., "obj.func()"
115 /// or "objptr->func()") or with normal function-call syntax
116 /// ("func()") within a member function that ends up calling a member
117 /// function. The callee in either case is a MemberExpr that contains
118 /// both the object argument and the member function, while the
119 /// arguments are the arguments within the parentheses (not including
120 /// the object argument).
121 class CXXMemberCallExpr : public CallExpr {
123 CXXMemberCallExpr(ASTContext &C, Expr *fn, ArrayRef<Expr*> args,
124 QualType t, ExprValueKind VK, SourceLocation RP)
125 : CallExpr(C, CXXMemberCallExprClass, fn, args, t, VK, RP) {}
127 CXXMemberCallExpr(ASTContext &C, EmptyShell Empty)
128 : CallExpr(C, CXXMemberCallExprClass, Empty) { }
130 /// \brief Retrieves the implicit object argument for the member call.
132 /// For example, in "x.f(5)", this returns the sub-expression "x".
133 Expr *getImplicitObjectArgument() const;
135 /// \brief Retrieves the declaration of the called method.
136 CXXMethodDecl *getMethodDecl() const;
138 /// \brief Retrieves the CXXRecordDecl for the underlying type of
139 /// the implicit object argument.
141 /// Note that this is may not be the same declaration as that of the class
142 /// context of the CXXMethodDecl which this function is calling.
143 /// FIXME: Returns 0 for member pointer call exprs.
144 CXXRecordDecl *getRecordDecl() const;
146 SourceLocation getExprLoc() const LLVM_READONLY {
147 SourceLocation CLoc = getCallee()->getExprLoc();
151 return getLocStart();
154 static bool classof(const Stmt *T) {
155 return T->getStmtClass() == CXXMemberCallExprClass;
159 /// \brief Represents a call to a CUDA kernel function.
160 class CUDAKernelCallExpr : public CallExpr {
162 enum { CONFIG, END_PREARG };
165 CUDAKernelCallExpr(ASTContext &C, Expr *fn, CallExpr *Config,
166 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
168 : CallExpr(C, CUDAKernelCallExprClass, fn, Config, args, t, VK, RP) {}
170 CUDAKernelCallExpr(ASTContext &C, EmptyShell Empty)
171 : CallExpr(C, CUDAKernelCallExprClass, END_PREARG, Empty) { }
173 const CallExpr *getConfig() const {
174 return cast_or_null<CallExpr>(getPreArg(CONFIG));
176 CallExpr *getConfig() { return cast_or_null<CallExpr>(getPreArg(CONFIG)); }
178 /// \brief Sets the kernel configuration expression.
180 /// Note that this method cannot be called if config has already been set to a
182 void setConfig(CallExpr *E) {
183 assert(!getConfig() &&
184 "Cannot call setConfig if config is not null");
185 setPreArg(CONFIG, E);
186 setInstantiationDependent(isInstantiationDependent() ||
187 E->isInstantiationDependent());
188 setContainsUnexpandedParameterPack(containsUnexpandedParameterPack() ||
189 E->containsUnexpandedParameterPack());
192 static bool classof(const Stmt *T) {
193 return T->getStmtClass() == CUDAKernelCallExprClass;
197 /// \brief Abstract class common to all of the C++ "named"/"keyword" casts.
199 /// This abstract class is inherited by all of the classes
200 /// representing "named" casts: CXXStaticCastExpr for \c static_cast,
201 /// CXXDynamicCastExpr for \c dynamic_cast, CXXReinterpretCastExpr for
202 /// reinterpret_cast, and CXXConstCastExpr for \c const_cast.
203 class CXXNamedCastExpr : public ExplicitCastExpr {
205 SourceLocation Loc; // the location of the casting op
206 SourceLocation RParenLoc; // the location of the right parenthesis
207 SourceRange AngleBrackets; // range for '<' '>'
210 CXXNamedCastExpr(StmtClass SC, QualType ty, ExprValueKind VK,
211 CastKind kind, Expr *op, unsigned PathSize,
212 TypeSourceInfo *writtenTy, SourceLocation l,
213 SourceLocation RParenLoc,
214 SourceRange AngleBrackets)
215 : ExplicitCastExpr(SC, ty, VK, kind, op, PathSize, writtenTy), Loc(l),
216 RParenLoc(RParenLoc), AngleBrackets(AngleBrackets) {}
218 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize)
219 : ExplicitCastExpr(SC, Shell, PathSize) { }
221 friend class ASTStmtReader;
224 const char *getCastName() const;
226 /// \brief Retrieve the location of the cast operator keyword, e.g.,
228 SourceLocation getOperatorLoc() const { return Loc; }
230 /// \brief Retrieve the location of the closing parenthesis.
231 SourceLocation getRParenLoc() const { return RParenLoc; }
233 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
234 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
235 SourceRange getAngleBrackets() const LLVM_READONLY { return AngleBrackets; }
237 static bool classof(const Stmt *T) {
238 switch (T->getStmtClass()) {
239 case CXXStaticCastExprClass:
240 case CXXDynamicCastExprClass:
241 case CXXReinterpretCastExprClass:
242 case CXXConstCastExprClass:
250 /// \brief A C++ \c static_cast expression (C++ [expr.static.cast]).
252 /// This expression node represents a C++ static cast, e.g.,
253 /// \c static_cast<int>(1.0).
254 class CXXStaticCastExpr final
255 : public CXXNamedCastExpr,
256 private llvm::TrailingObjects<CXXStaticCastExpr, CXXBaseSpecifier *> {
257 CXXStaticCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op,
258 unsigned pathSize, TypeSourceInfo *writtenTy,
259 SourceLocation l, SourceLocation RParenLoc,
260 SourceRange AngleBrackets)
261 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize,
262 writtenTy, l, RParenLoc, AngleBrackets) {}
264 explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize)
265 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize) { }
268 static CXXStaticCastExpr *Create(const ASTContext &Context, QualType T,
269 ExprValueKind VK, CastKind K, Expr *Op,
270 const CXXCastPath *Path,
271 TypeSourceInfo *Written, SourceLocation L,
272 SourceLocation RParenLoc,
273 SourceRange AngleBrackets);
274 static CXXStaticCastExpr *CreateEmpty(const ASTContext &Context,
277 static bool classof(const Stmt *T) {
278 return T->getStmtClass() == CXXStaticCastExprClass;
281 friend TrailingObjects;
282 friend class CastExpr;
285 /// \brief A C++ @c dynamic_cast expression (C++ [expr.dynamic.cast]).
287 /// This expression node represents a dynamic cast, e.g.,
288 /// \c dynamic_cast<Derived*>(BasePtr). Such a cast may perform a run-time
289 /// check to determine how to perform the type conversion.
290 class CXXDynamicCastExpr final
291 : public CXXNamedCastExpr,
292 private llvm::TrailingObjects<CXXDynamicCastExpr, CXXBaseSpecifier *> {
293 CXXDynamicCastExpr(QualType ty, ExprValueKind VK, CastKind kind,
294 Expr *op, unsigned pathSize, TypeSourceInfo *writtenTy,
295 SourceLocation l, SourceLocation RParenLoc,
296 SourceRange AngleBrackets)
297 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, VK, kind, op, pathSize,
298 writtenTy, l, RParenLoc, AngleBrackets) {}
300 explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize)
301 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize) { }
304 static CXXDynamicCastExpr *Create(const ASTContext &Context, QualType T,
305 ExprValueKind VK, CastKind Kind, Expr *Op,
306 const CXXCastPath *Path,
307 TypeSourceInfo *Written, SourceLocation L,
308 SourceLocation RParenLoc,
309 SourceRange AngleBrackets);
311 static CXXDynamicCastExpr *CreateEmpty(const ASTContext &Context,
314 bool isAlwaysNull() const;
316 static bool classof(const Stmt *T) {
317 return T->getStmtClass() == CXXDynamicCastExprClass;
320 friend TrailingObjects;
321 friend class CastExpr;
324 /// \brief A C++ @c reinterpret_cast expression (C++ [expr.reinterpret.cast]).
326 /// This expression node represents a reinterpret cast, e.g.,
327 /// @c reinterpret_cast<int>(VoidPtr).
329 /// A reinterpret_cast provides a differently-typed view of a value but
330 /// (in Clang, as in most C++ implementations) performs no actual work at
332 class CXXReinterpretCastExpr final
333 : public CXXNamedCastExpr,
334 private llvm::TrailingObjects<CXXReinterpretCastExpr,
335 CXXBaseSpecifier *> {
336 CXXReinterpretCastExpr(QualType ty, ExprValueKind vk, CastKind kind,
337 Expr *op, unsigned pathSize,
338 TypeSourceInfo *writtenTy, SourceLocation l,
339 SourceLocation RParenLoc,
340 SourceRange AngleBrackets)
341 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, vk, kind, op,
342 pathSize, writtenTy, l, RParenLoc, AngleBrackets) {}
344 CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize)
345 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize) { }
348 static CXXReinterpretCastExpr *Create(const ASTContext &Context, QualType T,
349 ExprValueKind VK, CastKind Kind,
350 Expr *Op, const CXXCastPath *Path,
351 TypeSourceInfo *WrittenTy, SourceLocation L,
352 SourceLocation RParenLoc,
353 SourceRange AngleBrackets);
354 static CXXReinterpretCastExpr *CreateEmpty(const ASTContext &Context,
357 static bool classof(const Stmt *T) {
358 return T->getStmtClass() == CXXReinterpretCastExprClass;
361 friend TrailingObjects;
362 friend class CastExpr;
365 /// \brief A C++ \c const_cast expression (C++ [expr.const.cast]).
367 /// This expression node represents a const cast, e.g.,
368 /// \c const_cast<char*>(PtrToConstChar).
370 /// A const_cast can remove type qualifiers but does not change the underlying
372 class CXXConstCastExpr final
373 : public CXXNamedCastExpr,
374 private llvm::TrailingObjects<CXXConstCastExpr, CXXBaseSpecifier *> {
375 CXXConstCastExpr(QualType ty, ExprValueKind VK, Expr *op,
376 TypeSourceInfo *writtenTy, SourceLocation l,
377 SourceLocation RParenLoc, SourceRange AngleBrackets)
378 : CXXNamedCastExpr(CXXConstCastExprClass, ty, VK, CK_NoOp, op,
379 0, writtenTy, l, RParenLoc, AngleBrackets) {}
381 explicit CXXConstCastExpr(EmptyShell Empty)
382 : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0) { }
385 static CXXConstCastExpr *Create(const ASTContext &Context, QualType T,
386 ExprValueKind VK, Expr *Op,
387 TypeSourceInfo *WrittenTy, SourceLocation L,
388 SourceLocation RParenLoc,
389 SourceRange AngleBrackets);
390 static CXXConstCastExpr *CreateEmpty(const ASTContext &Context);
392 static bool classof(const Stmt *T) {
393 return T->getStmtClass() == CXXConstCastExprClass;
396 friend TrailingObjects;
397 friend class CastExpr;
400 /// \brief A call to a literal operator (C++11 [over.literal])
401 /// written as a user-defined literal (C++11 [lit.ext]).
403 /// Represents a user-defined literal, e.g. "foo"_bar or 1.23_xyz. While this
404 /// is semantically equivalent to a normal call, this AST node provides better
405 /// information about the syntactic representation of the literal.
407 /// Since literal operators are never found by ADL and can only be declared at
408 /// namespace scope, a user-defined literal is never dependent.
409 class UserDefinedLiteral : public CallExpr {
410 /// \brief The location of a ud-suffix within the literal.
411 SourceLocation UDSuffixLoc;
414 UserDefinedLiteral(const ASTContext &C, Expr *Fn, ArrayRef<Expr*> Args,
415 QualType T, ExprValueKind VK, SourceLocation LitEndLoc,
416 SourceLocation SuffixLoc)
417 : CallExpr(C, UserDefinedLiteralClass, Fn, Args, T, VK, LitEndLoc),
418 UDSuffixLoc(SuffixLoc) {}
419 explicit UserDefinedLiteral(const ASTContext &C, EmptyShell Empty)
420 : CallExpr(C, UserDefinedLiteralClass, Empty) {}
422 /// The kind of literal operator which is invoked.
423 enum LiteralOperatorKind {
424 LOK_Raw, ///< Raw form: operator "" X (const char *)
425 LOK_Template, ///< Raw form: operator "" X<cs...> ()
426 LOK_Integer, ///< operator "" X (unsigned long long)
427 LOK_Floating, ///< operator "" X (long double)
428 LOK_String, ///< operator "" X (const CharT *, size_t)
429 LOK_Character ///< operator "" X (CharT)
432 /// \brief Returns the kind of literal operator invocation
433 /// which this expression represents.
434 LiteralOperatorKind getLiteralOperatorKind() const;
436 /// \brief If this is not a raw user-defined literal, get the
437 /// underlying cooked literal (representing the literal with the suffix
439 Expr *getCookedLiteral();
440 const Expr *getCookedLiteral() const {
441 return const_cast<UserDefinedLiteral*>(this)->getCookedLiteral();
444 SourceLocation getLocStart() const {
445 if (getLiteralOperatorKind() == LOK_Template)
446 return getRParenLoc();
447 return getArg(0)->getLocStart();
449 SourceLocation getLocEnd() const { return getRParenLoc(); }
452 /// \brief Returns the location of a ud-suffix in the expression.
454 /// For a string literal, there may be multiple identical suffixes. This
455 /// returns the first.
456 SourceLocation getUDSuffixLoc() const { return UDSuffixLoc; }
458 /// \brief Returns the ud-suffix specified for this literal.
459 const IdentifierInfo *getUDSuffix() const;
461 static bool classof(const Stmt *S) {
462 return S->getStmtClass() == UserDefinedLiteralClass;
465 friend class ASTStmtReader;
466 friend class ASTStmtWriter;
469 /// \brief A boolean literal, per ([C++ lex.bool] Boolean literals).
471 class CXXBoolLiteralExpr : public Expr {
475 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) :
476 Expr(CXXBoolLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
478 Value(val), Loc(l) {}
480 explicit CXXBoolLiteralExpr(EmptyShell Empty)
481 : Expr(CXXBoolLiteralExprClass, Empty) { }
483 bool getValue() const { return Value; }
484 void setValue(bool V) { Value = V; }
486 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
487 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
489 SourceLocation getLocation() const { return Loc; }
490 void setLocation(SourceLocation L) { Loc = L; }
492 static bool classof(const Stmt *T) {
493 return T->getStmtClass() == CXXBoolLiteralExprClass;
497 child_range children() {
498 return child_range(child_iterator(), child_iterator());
502 /// \brief The null pointer literal (C++11 [lex.nullptr])
504 /// Introduced in C++11, the only literal of type \c nullptr_t is \c nullptr.
505 class CXXNullPtrLiteralExpr : public Expr {
508 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l) :
509 Expr(CXXNullPtrLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
513 explicit CXXNullPtrLiteralExpr(EmptyShell Empty)
514 : Expr(CXXNullPtrLiteralExprClass, Empty) { }
516 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
517 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
519 SourceLocation getLocation() const { return Loc; }
520 void setLocation(SourceLocation L) { Loc = L; }
522 static bool classof(const Stmt *T) {
523 return T->getStmtClass() == CXXNullPtrLiteralExprClass;
526 child_range children() {
527 return child_range(child_iterator(), child_iterator());
531 /// \brief Implicit construction of a std::initializer_list<T> object from an
532 /// array temporary within list-initialization (C++11 [dcl.init.list]p5).
533 class CXXStdInitializerListExpr : public Expr {
536 CXXStdInitializerListExpr(EmptyShell Empty)
537 : Expr(CXXStdInitializerListExprClass, Empty), SubExpr(nullptr) {}
540 CXXStdInitializerListExpr(QualType Ty, Expr *SubExpr)
541 : Expr(CXXStdInitializerListExprClass, Ty, VK_RValue, OK_Ordinary,
542 Ty->isDependentType(), SubExpr->isValueDependent(),
543 SubExpr->isInstantiationDependent(),
544 SubExpr->containsUnexpandedParameterPack()),
547 Expr *getSubExpr() { return static_cast<Expr*>(SubExpr); }
548 const Expr *getSubExpr() const { return static_cast<const Expr*>(SubExpr); }
550 SourceLocation getLocStart() const LLVM_READONLY {
551 return SubExpr->getLocStart();
553 SourceLocation getLocEnd() const LLVM_READONLY {
554 return SubExpr->getLocEnd();
556 SourceRange getSourceRange() const LLVM_READONLY {
557 return SubExpr->getSourceRange();
560 static bool classof(const Stmt *S) {
561 return S->getStmtClass() == CXXStdInitializerListExprClass;
564 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
566 friend class ASTReader;
567 friend class ASTStmtReader;
570 /// A C++ \c typeid expression (C++ [expr.typeid]), which gets
571 /// the \c type_info that corresponds to the supplied type, or the (possibly
572 /// dynamic) type of the supplied expression.
574 /// This represents code like \c typeid(int) or \c typeid(*objPtr)
575 class CXXTypeidExpr : public Expr {
577 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
581 CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R)
582 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary,
583 // typeid is never type-dependent (C++ [temp.dep.expr]p4)
585 // typeid is value-dependent if the type or expression are dependent
586 Operand->getType()->isDependentType(),
587 Operand->getType()->isInstantiationDependentType(),
588 Operand->getType()->containsUnexpandedParameterPack()),
589 Operand(Operand), Range(R) { }
591 CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R)
592 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary,
593 // typeid is never type-dependent (C++ [temp.dep.expr]p4)
595 // typeid is value-dependent if the type or expression are dependent
596 Operand->isTypeDependent() || Operand->isValueDependent(),
597 Operand->isInstantiationDependent(),
598 Operand->containsUnexpandedParameterPack()),
599 Operand(Operand), Range(R) { }
601 CXXTypeidExpr(EmptyShell Empty, bool isExpr)
602 : Expr(CXXTypeidExprClass, Empty) {
604 Operand = (Expr*)nullptr;
606 Operand = (TypeSourceInfo*)nullptr;
609 /// Determine whether this typeid has a type operand which is potentially
610 /// evaluated, per C++11 [expr.typeid]p3.
611 bool isPotentiallyEvaluated() const;
613 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
615 /// \brief Retrieves the type operand of this typeid() expression after
616 /// various required adjustments (removing reference types, cv-qualifiers).
617 QualType getTypeOperand(ASTContext &Context) const;
619 /// \brief Retrieve source information for the type operand.
620 TypeSourceInfo *getTypeOperandSourceInfo() const {
621 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
622 return Operand.get<TypeSourceInfo *>();
625 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) {
626 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
630 Expr *getExprOperand() const {
631 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
632 return static_cast<Expr*>(Operand.get<Stmt *>());
635 void setExprOperand(Expr *E) {
636 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
640 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
641 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
642 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
643 void setSourceRange(SourceRange R) { Range = R; }
645 static bool classof(const Stmt *T) {
646 return T->getStmtClass() == CXXTypeidExprClass;
650 child_range children() {
652 return child_range(child_iterator(), child_iterator());
653 Stmt **begin = reinterpret_cast<Stmt**>(&Operand);
654 return child_range(begin, begin + 1);
658 /// \brief A member reference to an MSPropertyDecl.
660 /// This expression always has pseudo-object type, and therefore it is
661 /// typically not encountered in a fully-typechecked expression except
662 /// within the syntactic form of a PseudoObjectExpr.
663 class MSPropertyRefExpr : public Expr {
665 MSPropertyDecl *TheDecl;
666 SourceLocation MemberLoc;
668 NestedNameSpecifierLoc QualifierLoc;
671 MSPropertyRefExpr(Expr *baseExpr, MSPropertyDecl *decl, bool isArrow,
672 QualType ty, ExprValueKind VK,
673 NestedNameSpecifierLoc qualifierLoc,
674 SourceLocation nameLoc)
675 : Expr(MSPropertyRefExprClass, ty, VK, OK_Ordinary,
676 /*type-dependent*/ false, baseExpr->isValueDependent(),
677 baseExpr->isInstantiationDependent(),
678 baseExpr->containsUnexpandedParameterPack()),
679 BaseExpr(baseExpr), TheDecl(decl),
680 MemberLoc(nameLoc), IsArrow(isArrow),
681 QualifierLoc(qualifierLoc) {}
683 MSPropertyRefExpr(EmptyShell Empty) : Expr(MSPropertyRefExprClass, Empty) {}
685 SourceRange getSourceRange() const LLVM_READONLY {
686 return SourceRange(getLocStart(), getLocEnd());
688 bool isImplicitAccess() const {
689 return getBaseExpr() && getBaseExpr()->isImplicitCXXThis();
691 SourceLocation getLocStart() const {
692 if (!isImplicitAccess())
693 return BaseExpr->getLocStart();
694 else if (QualifierLoc)
695 return QualifierLoc.getBeginLoc();
699 SourceLocation getLocEnd() const { return getMemberLoc(); }
701 child_range children() {
702 return child_range((Stmt**)&BaseExpr, (Stmt**)&BaseExpr + 1);
704 static bool classof(const Stmt *T) {
705 return T->getStmtClass() == MSPropertyRefExprClass;
708 Expr *getBaseExpr() const { return BaseExpr; }
709 MSPropertyDecl *getPropertyDecl() const { return TheDecl; }
710 bool isArrow() const { return IsArrow; }
711 SourceLocation getMemberLoc() const { return MemberLoc; }
712 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
714 friend class ASTStmtReader;
717 /// MS property subscript expression.
718 /// MSVC supports 'property' attribute and allows to apply it to the
719 /// declaration of an empty array in a class or structure definition.
722 /// __declspec(property(get=GetX, put=PutX)) int x[];
724 /// The above statement indicates that x[] can be used with one or more array
725 /// indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), and
726 /// p->x[a][b] = i will be turned into p->PutX(a, b, i).
727 /// This is a syntactic pseudo-object expression.
728 class MSPropertySubscriptExpr : public Expr {
729 friend class ASTStmtReader;
730 enum { BASE_EXPR, IDX_EXPR, NUM_SUBEXPRS = 2 };
731 Stmt *SubExprs[NUM_SUBEXPRS];
732 SourceLocation RBracketLoc;
734 void setBase(Expr *Base) { SubExprs[BASE_EXPR] = Base; }
735 void setIdx(Expr *Idx) { SubExprs[IDX_EXPR] = Idx; }
738 MSPropertySubscriptExpr(Expr *Base, Expr *Idx, QualType Ty, ExprValueKind VK,
739 ExprObjectKind OK, SourceLocation RBracketLoc)
740 : Expr(MSPropertySubscriptExprClass, Ty, VK, OK, Idx->isTypeDependent(),
741 Idx->isValueDependent(), Idx->isInstantiationDependent(),
742 Idx->containsUnexpandedParameterPack()),
743 RBracketLoc(RBracketLoc) {
744 SubExprs[BASE_EXPR] = Base;
745 SubExprs[IDX_EXPR] = Idx;
748 /// \brief Create an empty array subscript expression.
749 explicit MSPropertySubscriptExpr(EmptyShell Shell)
750 : Expr(MSPropertySubscriptExprClass, Shell) {}
752 Expr *getBase() { return cast<Expr>(SubExprs[BASE_EXPR]); }
753 const Expr *getBase() const { return cast<Expr>(SubExprs[BASE_EXPR]); }
755 Expr *getIdx() { return cast<Expr>(SubExprs[IDX_EXPR]); }
756 const Expr *getIdx() const { return cast<Expr>(SubExprs[IDX_EXPR]); }
758 SourceLocation getLocStart() const LLVM_READONLY {
759 return getBase()->getLocStart();
761 SourceLocation getLocEnd() const LLVM_READONLY { return RBracketLoc; }
763 SourceLocation getRBracketLoc() const { return RBracketLoc; }
764 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
766 SourceLocation getExprLoc() const LLVM_READONLY {
767 return getBase()->getExprLoc();
770 static bool classof(const Stmt *T) {
771 return T->getStmtClass() == MSPropertySubscriptExprClass;
775 child_range children() {
776 return child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
780 /// A Microsoft C++ @c __uuidof expression, which gets
781 /// the _GUID that corresponds to the supplied type or expression.
783 /// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr)
784 class CXXUuidofExpr : public Expr {
786 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
791 CXXUuidofExpr(QualType Ty, TypeSourceInfo *Operand, StringRef UuidStr,
793 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, false,
794 Operand->getType()->isDependentType(),
795 Operand->getType()->isInstantiationDependentType(),
796 Operand->getType()->containsUnexpandedParameterPack()),
797 Operand(Operand), UuidStr(UuidStr), Range(R) {}
799 CXXUuidofExpr(QualType Ty, Expr *Operand, StringRef UuidStr, SourceRange R)
800 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, false,
801 Operand->isTypeDependent(), Operand->isInstantiationDependent(),
802 Operand->containsUnexpandedParameterPack()),
803 Operand(Operand), UuidStr(UuidStr), Range(R) {}
805 CXXUuidofExpr(EmptyShell Empty, bool isExpr)
806 : Expr(CXXUuidofExprClass, Empty) {
808 Operand = (Expr*)nullptr;
810 Operand = (TypeSourceInfo*)nullptr;
813 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
815 /// \brief Retrieves the type operand of this __uuidof() expression after
816 /// various required adjustments (removing reference types, cv-qualifiers).
817 QualType getTypeOperand(ASTContext &Context) const;
819 /// \brief Retrieve source information for the type operand.
820 TypeSourceInfo *getTypeOperandSourceInfo() const {
821 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
822 return Operand.get<TypeSourceInfo *>();
825 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) {
826 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
830 Expr *getExprOperand() const {
831 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
832 return static_cast<Expr*>(Operand.get<Stmt *>());
835 void setExprOperand(Expr *E) {
836 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
840 void setUuidStr(StringRef US) { UuidStr = US; }
841 StringRef getUuidStr() const { return UuidStr; }
843 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
844 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
845 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
846 void setSourceRange(SourceRange R) { Range = R; }
848 static bool classof(const Stmt *T) {
849 return T->getStmtClass() == CXXUuidofExprClass;
853 child_range children() {
855 return child_range(child_iterator(), child_iterator());
856 Stmt **begin = reinterpret_cast<Stmt**>(&Operand);
857 return child_range(begin, begin + 1);
861 /// \brief Represents the \c this expression in C++.
863 /// This is a pointer to the object on which the current member function is
864 /// executing (C++ [expr.prim]p3). Example:
870 /// void test() { this->bar(); }
873 class CXXThisExpr : public Expr {
878 CXXThisExpr(SourceLocation L, QualType Type, bool isImplicit)
879 : Expr(CXXThisExprClass, Type, VK_RValue, OK_Ordinary,
880 // 'this' is type-dependent if the class type of the enclosing
881 // member function is dependent (C++ [temp.dep.expr]p2)
882 Type->isDependentType(), Type->isDependentType(),
883 Type->isInstantiationDependentType(),
884 /*ContainsUnexpandedParameterPack=*/false),
885 Loc(L), Implicit(isImplicit) { }
887 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {}
889 SourceLocation getLocation() const { return Loc; }
890 void setLocation(SourceLocation L) { Loc = L; }
892 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
893 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
895 bool isImplicit() const { return Implicit; }
896 void setImplicit(bool I) { Implicit = I; }
898 static bool classof(const Stmt *T) {
899 return T->getStmtClass() == CXXThisExprClass;
903 child_range children() {
904 return child_range(child_iterator(), child_iterator());
908 /// \brief A C++ throw-expression (C++ [except.throw]).
910 /// This handles 'throw' (for re-throwing the current exception) and
911 /// 'throw' assignment-expression. When assignment-expression isn't
912 /// present, Op will be null.
913 class CXXThrowExpr : public Expr {
915 SourceLocation ThrowLoc;
916 /// \brief Whether the thrown variable (if any) is in scope.
917 unsigned IsThrownVariableInScope : 1;
919 friend class ASTStmtReader;
922 // \p Ty is the void type which is used as the result type of the
923 // expression. The \p l is the location of the throw keyword. \p expr
924 // can by null, if the optional expression to throw isn't present.
925 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l,
926 bool IsThrownVariableInScope) :
927 Expr(CXXThrowExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
928 expr && expr->isInstantiationDependent(),
929 expr && expr->containsUnexpandedParameterPack()),
930 Op(expr), ThrowLoc(l), IsThrownVariableInScope(IsThrownVariableInScope) {}
931 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {}
933 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); }
934 Expr *getSubExpr() { return cast_or_null<Expr>(Op); }
936 SourceLocation getThrowLoc() const { return ThrowLoc; }
938 /// \brief Determines whether the variable thrown by this expression (if any!)
939 /// is within the innermost try block.
941 /// This information is required to determine whether the NRVO can apply to
943 bool isThrownVariableInScope() const { return IsThrownVariableInScope; }
945 SourceLocation getLocStart() const LLVM_READONLY { return ThrowLoc; }
946 SourceLocation getLocEnd() const LLVM_READONLY {
949 return getSubExpr()->getLocEnd();
952 static bool classof(const Stmt *T) {
953 return T->getStmtClass() == CXXThrowExprClass;
957 child_range children() {
958 return child_range(&Op, Op ? &Op+1 : &Op);
962 /// \brief A default argument (C++ [dcl.fct.default]).
964 /// This wraps up a function call argument that was created from the
965 /// corresponding parameter's default argument, when the call did not
966 /// explicitly supply arguments for all of the parameters.
967 class CXXDefaultArgExpr final : public Expr {
968 /// \brief The parameter whose default is being used.
971 /// \brief The location where the default argument expression was used.
974 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param)
976 param->hasUnparsedDefaultArg()
977 ? param->getType().getNonReferenceType()
978 : param->getDefaultArg()->getType(),
979 param->getDefaultArg()->getValueKind(),
980 param->getDefaultArg()->getObjectKind(), false, false, false, false),
981 Param(param), Loc(Loc) { }
984 CXXDefaultArgExpr(EmptyShell Empty) : Expr(CXXDefaultArgExprClass, Empty) {}
986 // \p Param is the parameter whose default argument is used by this
988 static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc,
989 ParmVarDecl *Param) {
990 return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param);
993 // Retrieve the parameter that the argument was created from.
994 const ParmVarDecl *getParam() const { return Param; }
995 ParmVarDecl *getParam() { return Param; }
997 // Retrieve the actual argument to the function call.
998 const Expr *getExpr() const {
999 return getParam()->getDefaultArg();
1002 return getParam()->getDefaultArg();
1005 /// \brief Retrieve the location where this default argument was actually
1007 SourceLocation getUsedLocation() const { return Loc; }
1009 /// Default argument expressions have no representation in the
1010 /// source, so they have an empty source range.
1011 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1012 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1014 SourceLocation getExprLoc() const LLVM_READONLY { return Loc; }
1016 static bool classof(const Stmt *T) {
1017 return T->getStmtClass() == CXXDefaultArgExprClass;
1021 child_range children() {
1022 return child_range(child_iterator(), child_iterator());
1025 friend class ASTStmtReader;
1026 friend class ASTStmtWriter;
1029 /// \brief A use of a default initializer in a constructor or in aggregate
1032 /// This wraps a use of a C++ default initializer (technically,
1033 /// a brace-or-equal-initializer for a non-static data member) when it
1034 /// is implicitly used in a mem-initializer-list in a constructor
1035 /// (C++11 [class.base.init]p8) or in aggregate initialization
1036 /// (C++1y [dcl.init.aggr]p7).
1037 class CXXDefaultInitExpr : public Expr {
1038 /// \brief The field whose default is being used.
1041 /// \brief The location where the default initializer expression was used.
1044 CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc, FieldDecl *Field,
1047 CXXDefaultInitExpr(EmptyShell Empty) : Expr(CXXDefaultInitExprClass, Empty) {}
1050 /// \p Field is the non-static data member whose default initializer is used
1051 /// by this expression.
1052 static CXXDefaultInitExpr *Create(const ASTContext &C, SourceLocation Loc,
1054 return new (C) CXXDefaultInitExpr(C, Loc, Field, Field->getType());
1057 /// \brief Get the field whose initializer will be used.
1058 FieldDecl *getField() { return Field; }
1059 const FieldDecl *getField() const { return Field; }
1061 /// \brief Get the initialization expression that will be used.
1062 const Expr *getExpr() const {
1063 assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1064 return Field->getInClassInitializer();
1067 assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1068 return Field->getInClassInitializer();
1071 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
1072 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
1074 static bool classof(const Stmt *T) {
1075 return T->getStmtClass() == CXXDefaultInitExprClass;
1079 child_range children() {
1080 return child_range(child_iterator(), child_iterator());
1083 friend class ASTReader;
1084 friend class ASTStmtReader;
1087 /// \brief Represents a C++ temporary.
1088 class CXXTemporary {
1089 /// \brief The destructor that needs to be called.
1090 const CXXDestructorDecl *Destructor;
1092 explicit CXXTemporary(const CXXDestructorDecl *destructor)
1093 : Destructor(destructor) { }
1096 static CXXTemporary *Create(const ASTContext &C,
1097 const CXXDestructorDecl *Destructor);
1099 const CXXDestructorDecl *getDestructor() const { return Destructor; }
1100 void setDestructor(const CXXDestructorDecl *Dtor) {
1105 /// \brief Represents binding an expression to a temporary.
1107 /// This ensures the destructor is called for the temporary. It should only be
1108 /// needed for non-POD, non-trivially destructable class types. For example:
1112 /// S() { } // User defined constructor makes S non-POD.
1113 /// ~S() { } // User defined destructor makes it non-trivial.
1116 /// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr.
1119 class CXXBindTemporaryExpr : public Expr {
1124 CXXBindTemporaryExpr(CXXTemporary *temp, Expr* SubExpr)
1125 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(),
1126 VK_RValue, OK_Ordinary, SubExpr->isTypeDependent(),
1127 SubExpr->isValueDependent(),
1128 SubExpr->isInstantiationDependent(),
1129 SubExpr->containsUnexpandedParameterPack()),
1130 Temp(temp), SubExpr(SubExpr) { }
1133 CXXBindTemporaryExpr(EmptyShell Empty)
1134 : Expr(CXXBindTemporaryExprClass, Empty), Temp(nullptr), SubExpr(nullptr) {}
1136 static CXXBindTemporaryExpr *Create(const ASTContext &C, CXXTemporary *Temp,
1139 CXXTemporary *getTemporary() { return Temp; }
1140 const CXXTemporary *getTemporary() const { return Temp; }
1141 void setTemporary(CXXTemporary *T) { Temp = T; }
1143 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
1144 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
1145 void setSubExpr(Expr *E) { SubExpr = E; }
1147 SourceLocation getLocStart() const LLVM_READONLY {
1148 return SubExpr->getLocStart();
1150 SourceLocation getLocEnd() const LLVM_READONLY { return SubExpr->getLocEnd();}
1152 // Implement isa/cast/dyncast/etc.
1153 static bool classof(const Stmt *T) {
1154 return T->getStmtClass() == CXXBindTemporaryExprClass;
1158 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
1161 /// \brief Represents a call to a C++ constructor.
1162 class CXXConstructExpr : public Expr {
1164 enum ConstructionKind {
1172 CXXConstructorDecl *Constructor;
1175 SourceRange ParenOrBraceRange;
1176 unsigned NumArgs : 16;
1177 unsigned Elidable : 1;
1178 unsigned HadMultipleCandidates : 1;
1179 unsigned ListInitialization : 1;
1180 unsigned StdInitListInitialization : 1;
1181 unsigned ZeroInitialization : 1;
1182 unsigned ConstructKind : 2;
1185 void setConstructor(CXXConstructorDecl *C) { Constructor = C; }
1188 CXXConstructExpr(const ASTContext &C, StmtClass SC, QualType T,
1190 CXXConstructorDecl *Ctor,
1192 ArrayRef<Expr *> Args,
1193 bool HadMultipleCandidates,
1194 bool ListInitialization,
1195 bool StdInitListInitialization,
1196 bool ZeroInitialization,
1197 ConstructionKind ConstructKind,
1198 SourceRange ParenOrBraceRange);
1200 /// \brief Construct an empty C++ construction expression.
1201 CXXConstructExpr(StmtClass SC, EmptyShell Empty)
1202 : Expr(SC, Empty), Constructor(nullptr), NumArgs(0), Elidable(false),
1203 HadMultipleCandidates(false), ListInitialization(false),
1204 ZeroInitialization(false), ConstructKind(0), Args(nullptr)
1208 /// \brief Construct an empty C++ construction expression.
1209 explicit CXXConstructExpr(EmptyShell Empty)
1210 : CXXConstructExpr(CXXConstructExprClass, Empty) {}
1212 static CXXConstructExpr *Create(const ASTContext &C, QualType T,
1214 CXXConstructorDecl *Ctor,
1216 ArrayRef<Expr *> Args,
1217 bool HadMultipleCandidates,
1218 bool ListInitialization,
1219 bool StdInitListInitialization,
1220 bool ZeroInitialization,
1221 ConstructionKind ConstructKind,
1222 SourceRange ParenOrBraceRange);
1224 /// \brief Get the constructor that this expression will (ultimately) call.
1225 CXXConstructorDecl *getConstructor() const { return Constructor; }
1227 SourceLocation getLocation() const { return Loc; }
1228 void setLocation(SourceLocation Loc) { this->Loc = Loc; }
1230 /// \brief Whether this construction is elidable.
1231 bool isElidable() const { return Elidable; }
1232 void setElidable(bool E) { Elidable = E; }
1234 /// \brief Whether the referred constructor was resolved from
1235 /// an overloaded set having size greater than 1.
1236 bool hadMultipleCandidates() const { return HadMultipleCandidates; }
1237 void setHadMultipleCandidates(bool V) { HadMultipleCandidates = V; }
1239 /// \brief Whether this constructor call was written as list-initialization.
1240 bool isListInitialization() const { return ListInitialization; }
1241 void setListInitialization(bool V) { ListInitialization = V; }
1243 /// \brief Whether this constructor call was written as list-initialization,
1244 /// but was interpreted as forming a std::initializer_list<T> from the list
1245 /// and passing that as a single constructor argument.
1246 /// See C++11 [over.match.list]p1 bullet 1.
1247 bool isStdInitListInitialization() const { return StdInitListInitialization; }
1248 void setStdInitListInitialization(bool V) { StdInitListInitialization = V; }
1250 /// \brief Whether this construction first requires
1251 /// zero-initialization before the initializer is called.
1252 bool requiresZeroInitialization() const { return ZeroInitialization; }
1253 void setRequiresZeroInitialization(bool ZeroInit) {
1254 ZeroInitialization = ZeroInit;
1257 /// \brief Determine whether this constructor is actually constructing
1258 /// a base class (rather than a complete object).
1259 ConstructionKind getConstructionKind() const {
1260 return (ConstructionKind)ConstructKind;
1262 void setConstructionKind(ConstructionKind CK) {
1266 typedef ExprIterator arg_iterator;
1267 typedef ConstExprIterator const_arg_iterator;
1268 typedef llvm::iterator_range<arg_iterator> arg_range;
1269 typedef llvm::iterator_range<const_arg_iterator> arg_const_range;
1271 arg_range arguments() { return arg_range(arg_begin(), arg_end()); }
1272 arg_const_range arguments() const {
1273 return arg_const_range(arg_begin(), arg_end());
1276 arg_iterator arg_begin() { return Args; }
1277 arg_iterator arg_end() { return Args + NumArgs; }
1278 const_arg_iterator arg_begin() const { return Args; }
1279 const_arg_iterator arg_end() const { return Args + NumArgs; }
1281 Expr **getArgs() { return reinterpret_cast<Expr **>(Args); }
1282 const Expr *const *getArgs() const {
1283 return const_cast<CXXConstructExpr *>(this)->getArgs();
1285 unsigned getNumArgs() const { return NumArgs; }
1287 /// \brief Return the specified argument.
1288 Expr *getArg(unsigned Arg) {
1289 assert(Arg < NumArgs && "Arg access out of range!");
1290 return cast<Expr>(Args[Arg]);
1292 const Expr *getArg(unsigned Arg) const {
1293 assert(Arg < NumArgs && "Arg access out of range!");
1294 return cast<Expr>(Args[Arg]);
1297 /// \brief Set the specified argument.
1298 void setArg(unsigned Arg, Expr *ArgExpr) {
1299 assert(Arg < NumArgs && "Arg access out of range!");
1300 Args[Arg] = ArgExpr;
1303 SourceLocation getLocStart() const LLVM_READONLY;
1304 SourceLocation getLocEnd() const LLVM_READONLY;
1305 SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
1306 void setParenOrBraceRange(SourceRange Range) { ParenOrBraceRange = Range; }
1308 static bool classof(const Stmt *T) {
1309 return T->getStmtClass() == CXXConstructExprClass ||
1310 T->getStmtClass() == CXXTemporaryObjectExprClass;
1314 child_range children() {
1315 return child_range(&Args[0], &Args[0]+NumArgs);
1318 friend class ASTStmtReader;
1321 /// \brief Represents a call to an inherited base class constructor from an
1322 /// inheriting constructor. This call implicitly forwards the arguments from
1323 /// the enclosing context (an inheriting constructor) to the specified inherited
1324 /// base class constructor.
1325 class CXXInheritedCtorInitExpr : public Expr {
1327 CXXConstructorDecl *Constructor;
1329 /// The location of the using declaration.
1332 /// Whether this is the construction of a virtual base.
1333 unsigned ConstructsVirtualBase : 1;
1335 /// Whether the constructor is inherited from a virtual base class of the
1336 /// class that we construct.
1337 unsigned InheritedFromVirtualBase : 1;
1340 /// \brief Construct a C++ inheriting construction expression.
1341 CXXInheritedCtorInitExpr(SourceLocation Loc, QualType T,
1342 CXXConstructorDecl *Ctor, bool ConstructsVirtualBase,
1343 bool InheritedFromVirtualBase)
1344 : Expr(CXXInheritedCtorInitExprClass, T, VK_RValue, OK_Ordinary, false,
1345 false, false, false),
1346 Constructor(Ctor), Loc(Loc),
1347 ConstructsVirtualBase(ConstructsVirtualBase),
1348 InheritedFromVirtualBase(InheritedFromVirtualBase) {
1349 assert(!T->isDependentType());
1352 /// \brief Construct an empty C++ inheriting construction expression.
1353 explicit CXXInheritedCtorInitExpr(EmptyShell Empty)
1354 : Expr(CXXInheritedCtorInitExprClass, Empty), Constructor(nullptr),
1355 ConstructsVirtualBase(false), InheritedFromVirtualBase(false) {}
1357 /// \brief Get the constructor that this expression will call.
1358 CXXConstructorDecl *getConstructor() const { return Constructor; }
1360 /// \brief Determine whether this constructor is actually constructing
1361 /// a base class (rather than a complete object).
1362 bool constructsVBase() const { return ConstructsVirtualBase; }
1363 CXXConstructExpr::ConstructionKind getConstructionKind() const {
1364 return ConstructsVirtualBase ? CXXConstructExpr::CK_VirtualBase
1365 : CXXConstructExpr::CK_NonVirtualBase;
1368 /// \brief Determine whether the inherited constructor is inherited from a
1369 /// virtual base of the object we construct. If so, we are not responsible
1370 /// for calling the inherited constructor (the complete object constructor
1371 /// does that), and so we don't need to pass any arguments.
1372 bool inheritedFromVBase() const { return InheritedFromVirtualBase; }
1374 SourceLocation getLocation() const LLVM_READONLY { return Loc; }
1375 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
1376 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
1378 static bool classof(const Stmt *T) {
1379 return T->getStmtClass() == CXXInheritedCtorInitExprClass;
1381 child_range children() {
1382 return child_range(child_iterator(), child_iterator());
1385 friend class ASTStmtReader;
1388 /// \brief Represents an explicit C++ type conversion that uses "functional"
1389 /// notation (C++ [expr.type.conv]).
1395 class CXXFunctionalCastExpr final
1396 : public ExplicitCastExpr,
1397 private llvm::TrailingObjects<CXXFunctionalCastExpr, CXXBaseSpecifier *> {
1398 SourceLocation LParenLoc;
1399 SourceLocation RParenLoc;
1401 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK,
1402 TypeSourceInfo *writtenTy,
1403 CastKind kind, Expr *castExpr, unsigned pathSize,
1404 SourceLocation lParenLoc, SourceLocation rParenLoc)
1405 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind,
1406 castExpr, pathSize, writtenTy),
1407 LParenLoc(lParenLoc), RParenLoc(rParenLoc) {}
1409 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize)
1410 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize) { }
1413 static CXXFunctionalCastExpr *Create(const ASTContext &Context, QualType T,
1415 TypeSourceInfo *Written,
1416 CastKind Kind, Expr *Op,
1417 const CXXCastPath *Path,
1418 SourceLocation LPLoc,
1419 SourceLocation RPLoc);
1420 static CXXFunctionalCastExpr *CreateEmpty(const ASTContext &Context,
1423 SourceLocation getLParenLoc() const { return LParenLoc; }
1424 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1425 SourceLocation getRParenLoc() const { return RParenLoc; }
1426 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1428 SourceLocation getLocStart() const LLVM_READONLY;
1429 SourceLocation getLocEnd() const LLVM_READONLY;
1431 static bool classof(const Stmt *T) {
1432 return T->getStmtClass() == CXXFunctionalCastExprClass;
1435 friend TrailingObjects;
1436 friend class CastExpr;
1439 /// @brief Represents a C++ functional cast expression that builds a
1440 /// temporary object.
1442 /// This expression type represents a C++ "functional" cast
1443 /// (C++[expr.type.conv]) with N != 1 arguments that invokes a
1444 /// constructor to build a temporary object. With N == 1 arguments the
1445 /// functional cast expression will be represented by CXXFunctionalCastExpr.
1448 /// struct X { X(int, float); }
1451 /// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
1454 class CXXTemporaryObjectExpr : public CXXConstructExpr {
1455 TypeSourceInfo *Type;
1458 CXXTemporaryObjectExpr(const ASTContext &C,
1459 CXXConstructorDecl *Cons,
1460 TypeSourceInfo *Type,
1461 ArrayRef<Expr *> Args,
1462 SourceRange ParenOrBraceRange,
1463 bool HadMultipleCandidates,
1464 bool ListInitialization,
1465 bool StdInitListInitialization,
1466 bool ZeroInitialization);
1467 explicit CXXTemporaryObjectExpr(EmptyShell Empty)
1468 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty), Type() { }
1470 TypeSourceInfo *getTypeSourceInfo() const { return Type; }
1472 SourceLocation getLocStart() const LLVM_READONLY;
1473 SourceLocation getLocEnd() const LLVM_READONLY;
1475 static bool classof(const Stmt *T) {
1476 return T->getStmtClass() == CXXTemporaryObjectExprClass;
1479 friend class ASTStmtReader;
1482 /// \brief A C++ lambda expression, which produces a function object
1483 /// (of unspecified type) that can be invoked later.
1487 /// void low_pass_filter(std::vector<double> &values, double cutoff) {
1488 /// values.erase(std::remove_if(values.begin(), values.end(),
1489 /// [=](double value) { return value > cutoff; });
1493 /// C++11 lambda expressions can capture local variables, either by copying
1494 /// the values of those local variables at the time the function
1495 /// object is constructed (not when it is called!) or by holding a
1496 /// reference to the local variable. These captures can occur either
1497 /// implicitly or can be written explicitly between the square
1498 /// brackets ([...]) that start the lambda expression.
1500 /// C++1y introduces a new form of "capture" called an init-capture that
1501 /// includes an initializing expression (rather than capturing a variable),
1502 /// and which can never occur implicitly.
1503 class LambdaExpr final
1505 private llvm::TrailingObjects<LambdaExpr, Stmt *, unsigned, VarDecl *> {
1506 /// \brief The source range that covers the lambda introducer ([...]).
1507 SourceRange IntroducerRange;
1509 /// \brief The source location of this lambda's capture-default ('=' or '&').
1510 SourceLocation CaptureDefaultLoc;
1512 /// \brief The number of captures.
1513 unsigned NumCaptures : 16;
1515 /// \brief The default capture kind, which is a value of type
1516 /// LambdaCaptureDefault.
1517 unsigned CaptureDefault : 2;
1519 /// \brief Whether this lambda had an explicit parameter list vs. an
1520 /// implicit (and empty) parameter list.
1521 unsigned ExplicitParams : 1;
1523 /// \brief Whether this lambda had the result type explicitly specified.
1524 unsigned ExplicitResultType : 1;
1526 /// \brief Whether there are any array index variables stored at the end of
1527 /// this lambda expression.
1528 unsigned HasArrayIndexVars : 1;
1530 /// \brief The location of the closing brace ('}') that completes
1533 /// The location of the brace is also available by looking up the
1534 /// function call operator in the lambda class. However, it is
1535 /// stored here to improve the performance of getSourceRange(), and
1536 /// to avoid having to deserialize the function call operator from a
1537 /// module file just to determine the source range.
1538 SourceLocation ClosingBrace;
1540 size_t numTrailingObjects(OverloadToken<Stmt *>) const {
1541 return NumCaptures + 1;
1544 size_t numTrailingObjects(OverloadToken<unsigned>) const {
1545 return HasArrayIndexVars ? NumCaptures + 1 : 0;
1548 /// \brief Construct a lambda expression.
1549 LambdaExpr(QualType T, SourceRange IntroducerRange,
1550 LambdaCaptureDefault CaptureDefault,
1551 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
1552 bool ExplicitParams, bool ExplicitResultType,
1553 ArrayRef<Expr *> CaptureInits, ArrayRef<VarDecl *> ArrayIndexVars,
1554 ArrayRef<unsigned> ArrayIndexStarts, SourceLocation ClosingBrace,
1555 bool ContainsUnexpandedParameterPack);
1557 /// \brief Construct an empty lambda expression.
1558 LambdaExpr(EmptyShell Empty, unsigned NumCaptures, bool HasArrayIndexVars)
1559 : Expr(LambdaExprClass, Empty),
1560 NumCaptures(NumCaptures), CaptureDefault(LCD_None), ExplicitParams(false),
1561 ExplicitResultType(false), HasArrayIndexVars(true) {
1562 getStoredStmts()[NumCaptures] = nullptr;
1565 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
1567 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
1569 /// \brief Retrieve the mapping from captures to the first array index
1571 unsigned *getArrayIndexStarts() { return getTrailingObjects<unsigned>(); }
1573 const unsigned *getArrayIndexStarts() const {
1574 return getTrailingObjects<unsigned>();
1577 /// \brief Retrieve the complete set of array-index variables.
1578 VarDecl **getArrayIndexVars() { return getTrailingObjects<VarDecl *>(); }
1580 VarDecl *const *getArrayIndexVars() const {
1581 return getTrailingObjects<VarDecl *>();
1585 /// \brief Construct a new lambda expression.
1587 Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange,
1588 LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc,
1589 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
1590 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1591 ArrayRef<VarDecl *> ArrayIndexVars,
1592 ArrayRef<unsigned> ArrayIndexStarts, SourceLocation ClosingBrace,
1593 bool ContainsUnexpandedParameterPack);
1595 /// \brief Construct a new lambda expression that will be deserialized from
1596 /// an external source.
1597 static LambdaExpr *CreateDeserialized(const ASTContext &C,
1598 unsigned NumCaptures,
1599 unsigned NumArrayIndexVars);
1601 /// \brief Determine the default capture kind for this lambda.
1602 LambdaCaptureDefault getCaptureDefault() const {
1603 return static_cast<LambdaCaptureDefault>(CaptureDefault);
1606 /// \brief Retrieve the location of this lambda's capture-default, if any.
1607 SourceLocation getCaptureDefaultLoc() const {
1608 return CaptureDefaultLoc;
1611 /// \brief Determine whether one of this lambda's captures is an init-capture.
1612 bool isInitCapture(const LambdaCapture *Capture) const;
1614 /// \brief An iterator that walks over the captures of the lambda,
1615 /// both implicit and explicit.
1616 typedef const LambdaCapture *capture_iterator;
1618 /// \brief An iterator over a range of lambda captures.
1619 typedef llvm::iterator_range<capture_iterator> capture_range;
1621 /// \brief Retrieve this lambda's captures.
1622 capture_range captures() const;
1624 /// \brief Retrieve an iterator pointing to the first lambda capture.
1625 capture_iterator capture_begin() const;
1627 /// \brief Retrieve an iterator pointing past the end of the
1628 /// sequence of lambda captures.
1629 capture_iterator capture_end() const;
1631 /// \brief Determine the number of captures in this lambda.
1632 unsigned capture_size() const { return NumCaptures; }
1634 /// \brief Retrieve this lambda's explicit captures.
1635 capture_range explicit_captures() const;
1637 /// \brief Retrieve an iterator pointing to the first explicit
1639 capture_iterator explicit_capture_begin() const;
1641 /// \brief Retrieve an iterator pointing past the end of the sequence of
1642 /// explicit lambda captures.
1643 capture_iterator explicit_capture_end() const;
1645 /// \brief Retrieve this lambda's implicit captures.
1646 capture_range implicit_captures() const;
1648 /// \brief Retrieve an iterator pointing to the first implicit
1650 capture_iterator implicit_capture_begin() const;
1652 /// \brief Retrieve an iterator pointing past the end of the sequence of
1653 /// implicit lambda captures.
1654 capture_iterator implicit_capture_end() const;
1656 /// \brief Iterator that walks over the capture initialization
1658 typedef Expr **capture_init_iterator;
1660 /// \brief Const iterator that walks over the capture initialization
1662 typedef Expr *const *const_capture_init_iterator;
1664 /// \brief Retrieve the initialization expressions for this lambda's captures.
1665 llvm::iterator_range<capture_init_iterator> capture_inits() {
1666 return llvm::make_range(capture_init_begin(), capture_init_end());
1669 /// \brief Retrieve the initialization expressions for this lambda's captures.
1670 llvm::iterator_range<const_capture_init_iterator> capture_inits() const {
1671 return llvm::make_range(capture_init_begin(), capture_init_end());
1674 /// \brief Retrieve the first initialization argument for this
1675 /// lambda expression (which initializes the first capture field).
1676 capture_init_iterator capture_init_begin() {
1677 return reinterpret_cast<Expr **>(getStoredStmts());
1680 /// \brief Retrieve the first initialization argument for this
1681 /// lambda expression (which initializes the first capture field).
1682 const_capture_init_iterator capture_init_begin() const {
1683 return reinterpret_cast<Expr *const *>(getStoredStmts());
1686 /// \brief Retrieve the iterator pointing one past the last
1687 /// initialization argument for this lambda expression.
1688 capture_init_iterator capture_init_end() {
1689 return capture_init_begin() + NumCaptures;
1692 /// \brief Retrieve the iterator pointing one past the last
1693 /// initialization argument for this lambda expression.
1694 const_capture_init_iterator capture_init_end() const {
1695 return capture_init_begin() + NumCaptures;
1698 /// \brief Retrieve the set of index variables used in the capture
1699 /// initializer of an array captured by copy.
1701 /// \param Iter The iterator that points at the capture initializer for
1702 /// which we are extracting the corresponding index variables.
1704 getCaptureInitIndexVars(const_capture_init_iterator Iter) const;
1706 /// \brief Retrieve the source range covering the lambda introducer,
1707 /// which contains the explicit capture list surrounded by square
1708 /// brackets ([...]).
1709 SourceRange getIntroducerRange() const { return IntroducerRange; }
1711 /// \brief Retrieve the class that corresponds to the lambda.
1713 /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the
1714 /// captures in its fields and provides the various operations permitted
1715 /// on a lambda (copying, calling).
1716 CXXRecordDecl *getLambdaClass() const;
1718 /// \brief Retrieve the function call operator associated with this
1719 /// lambda expression.
1720 CXXMethodDecl *getCallOperator() const;
1722 /// \brief If this is a generic lambda expression, retrieve the template
1723 /// parameter list associated with it, or else return null.
1724 TemplateParameterList *getTemplateParameterList() const;
1726 /// \brief Whether this is a generic lambda.
1727 bool isGenericLambda() const { return getTemplateParameterList(); }
1729 /// \brief Retrieve the body of the lambda.
1730 CompoundStmt *getBody() const;
1732 /// \brief Determine whether the lambda is mutable, meaning that any
1733 /// captures values can be modified.
1734 bool isMutable() const;
1736 /// \brief Determine whether this lambda has an explicit parameter
1737 /// list vs. an implicit (empty) parameter list.
1738 bool hasExplicitParameters() const { return ExplicitParams; }
1740 /// \brief Whether this lambda had its result type explicitly specified.
1741 bool hasExplicitResultType() const { return ExplicitResultType; }
1743 static bool classof(const Stmt *T) {
1744 return T->getStmtClass() == LambdaExprClass;
1747 SourceLocation getLocStart() const LLVM_READONLY {
1748 return IntroducerRange.getBegin();
1750 SourceLocation getLocEnd() const LLVM_READONLY { return ClosingBrace; }
1752 child_range children() {
1753 // Includes initialization exprs plus body stmt
1754 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures + 1);
1757 friend TrailingObjects;
1758 friend class ASTStmtReader;
1759 friend class ASTStmtWriter;
1762 /// An expression "T()" which creates a value-initialized rvalue of type
1763 /// T, which is a non-class type. See (C++98 [5.2.3p2]).
1764 class CXXScalarValueInitExpr : public Expr {
1765 SourceLocation RParenLoc;
1766 TypeSourceInfo *TypeInfo;
1768 friend class ASTStmtReader;
1771 /// \brief Create an explicitly-written scalar-value initialization
1773 CXXScalarValueInitExpr(QualType Type, TypeSourceInfo *TypeInfo,
1774 SourceLocation rParenLoc)
1775 : Expr(CXXScalarValueInitExprClass, Type, VK_RValue, OK_Ordinary,
1776 false, false, Type->isInstantiationDependentType(),
1777 Type->containsUnexpandedParameterPack()),
1778 RParenLoc(rParenLoc), TypeInfo(TypeInfo) {}
1780 explicit CXXScalarValueInitExpr(EmptyShell Shell)
1781 : Expr(CXXScalarValueInitExprClass, Shell) { }
1783 TypeSourceInfo *getTypeSourceInfo() const {
1787 SourceLocation getRParenLoc() const { return RParenLoc; }
1789 SourceLocation getLocStart() const LLVM_READONLY;
1790 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1792 static bool classof(const Stmt *T) {
1793 return T->getStmtClass() == CXXScalarValueInitExprClass;
1797 child_range children() {
1798 return child_range(child_iterator(), child_iterator());
1802 /// \brief Represents a new-expression for memory allocation and constructor
1803 /// calls, e.g: "new CXXNewExpr(foo)".
1804 class CXXNewExpr : public Expr {
1805 /// Contains an optional array size expression, an optional initialization
1806 /// expression, and any number of optional placement arguments, in that order.
1808 /// \brief Points to the allocation function used.
1809 FunctionDecl *OperatorNew;
1810 /// \brief Points to the deallocation function used in case of error. May be
1812 FunctionDecl *OperatorDelete;
1814 /// \brief The allocated type-source information, as written in the source.
1815 TypeSourceInfo *AllocatedTypeInfo;
1817 /// \brief If the allocated type was expressed as a parenthesized type-id,
1818 /// the source range covering the parenthesized type-id.
1819 SourceRange TypeIdParens;
1821 /// \brief Range of the entire new expression.
1824 /// \brief Source-range of a paren-delimited initializer.
1825 SourceRange DirectInitRange;
1827 /// Was the usage ::new, i.e. is the global new to be used?
1828 unsigned GlobalNew : 1;
1829 /// Do we allocate an array? If so, the first SubExpr is the size expression.
1831 /// If this is an array allocation, does the usual deallocation
1832 /// function for the allocated type want to know the allocated size?
1833 unsigned UsualArrayDeleteWantsSize : 1;
1834 /// The number of placement new arguments.
1835 unsigned NumPlacementArgs : 13;
1836 /// What kind of initializer do we have? Could be none, parens, or braces.
1837 /// In storage, we distinguish between "none, and no initializer expr", and
1838 /// "none, but an implicit initializer expr".
1839 unsigned StoredInitializationStyle : 2;
1841 friend class ASTStmtReader;
1842 friend class ASTStmtWriter;
1844 enum InitializationStyle {
1845 NoInit, ///< New-expression has no initializer as written.
1846 CallInit, ///< New-expression has a C++98 paren-delimited initializer.
1847 ListInit ///< New-expression has a C++11 list-initializer.
1850 CXXNewExpr(const ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
1851 FunctionDecl *operatorDelete, bool usualArrayDeleteWantsSize,
1852 ArrayRef<Expr*> placementArgs,
1853 SourceRange typeIdParens, Expr *arraySize,
1854 InitializationStyle initializationStyle, Expr *initializer,
1855 QualType ty, TypeSourceInfo *AllocatedTypeInfo,
1856 SourceRange Range, SourceRange directInitRange);
1857 explicit CXXNewExpr(EmptyShell Shell)
1858 : Expr(CXXNewExprClass, Shell), SubExprs(nullptr) { }
1860 void AllocateArgsArray(const ASTContext &C, bool isArray,
1861 unsigned numPlaceArgs, bool hasInitializer);
1863 QualType getAllocatedType() const {
1864 assert(getType()->isPointerType());
1865 return getType()->getAs<PointerType>()->getPointeeType();
1868 TypeSourceInfo *getAllocatedTypeSourceInfo() const {
1869 return AllocatedTypeInfo;
1872 /// \brief True if the allocation result needs to be null-checked.
1874 /// C++11 [expr.new]p13:
1875 /// If the allocation function returns null, initialization shall
1876 /// not be done, the deallocation function shall not be called,
1877 /// and the value of the new-expression shall be null.
1880 /// If the allocation function is a reserved placement allocation
1881 /// function that returns null, the behavior is undefined.
1883 /// An allocation function is not allowed to return null unless it
1884 /// has a non-throwing exception-specification. The '03 rule is
1885 /// identical except that the definition of a non-throwing
1886 /// exception specification is just "is it throw()?".
1887 bool shouldNullCheckAllocation(const ASTContext &Ctx) const;
1889 FunctionDecl *getOperatorNew() const { return OperatorNew; }
1890 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; }
1891 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
1892 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; }
1894 bool isArray() const { return Array; }
1895 Expr *getArraySize() {
1896 return Array ? cast<Expr>(SubExprs[0]) : nullptr;
1898 const Expr *getArraySize() const {
1899 return Array ? cast<Expr>(SubExprs[0]) : nullptr;
1902 unsigned getNumPlacementArgs() const { return NumPlacementArgs; }
1903 Expr **getPlacementArgs() {
1904 return reinterpret_cast<Expr **>(SubExprs + Array + hasInitializer());
1907 Expr *getPlacementArg(unsigned i) {
1908 assert(i < NumPlacementArgs && "Index out of range");
1909 return getPlacementArgs()[i];
1911 const Expr *getPlacementArg(unsigned i) const {
1912 assert(i < NumPlacementArgs && "Index out of range");
1913 return const_cast<CXXNewExpr*>(this)->getPlacementArg(i);
1916 bool isParenTypeId() const { return TypeIdParens.isValid(); }
1917 SourceRange getTypeIdParens() const { return TypeIdParens; }
1919 bool isGlobalNew() const { return GlobalNew; }
1921 /// \brief Whether this new-expression has any initializer at all.
1922 bool hasInitializer() const { return StoredInitializationStyle > 0; }
1924 /// \brief The kind of initializer this new-expression has.
1925 InitializationStyle getInitializationStyle() const {
1926 if (StoredInitializationStyle == 0)
1928 return static_cast<InitializationStyle>(StoredInitializationStyle-1);
1931 /// \brief The initializer of this new-expression.
1932 Expr *getInitializer() {
1933 return hasInitializer() ? cast<Expr>(SubExprs[Array]) : nullptr;
1935 const Expr *getInitializer() const {
1936 return hasInitializer() ? cast<Expr>(SubExprs[Array]) : nullptr;
1939 /// \brief Returns the CXXConstructExpr from this new-expression, or null.
1940 const CXXConstructExpr* getConstructExpr() const {
1941 return dyn_cast_or_null<CXXConstructExpr>(getInitializer());
1944 /// Answers whether the usual array deallocation function for the
1945 /// allocated type expects the size of the allocation as a
1947 bool doesUsualArrayDeleteWantSize() const {
1948 return UsualArrayDeleteWantsSize;
1951 typedef ExprIterator arg_iterator;
1952 typedef ConstExprIterator const_arg_iterator;
1954 llvm::iterator_range<arg_iterator> placement_arguments() {
1955 return llvm::make_range(placement_arg_begin(), placement_arg_end());
1958 llvm::iterator_range<const_arg_iterator> placement_arguments() const {
1959 return llvm::make_range(placement_arg_begin(), placement_arg_end());
1962 arg_iterator placement_arg_begin() {
1963 return SubExprs + Array + hasInitializer();
1965 arg_iterator placement_arg_end() {
1966 return SubExprs + Array + hasInitializer() + getNumPlacementArgs();
1968 const_arg_iterator placement_arg_begin() const {
1969 return SubExprs + Array + hasInitializer();
1971 const_arg_iterator placement_arg_end() const {
1972 return SubExprs + Array + hasInitializer() + getNumPlacementArgs();
1975 typedef Stmt **raw_arg_iterator;
1976 raw_arg_iterator raw_arg_begin() { return SubExprs; }
1977 raw_arg_iterator raw_arg_end() {
1978 return SubExprs + Array + hasInitializer() + getNumPlacementArgs();
1980 const_arg_iterator raw_arg_begin() const { return SubExprs; }
1981 const_arg_iterator raw_arg_end() const {
1982 return SubExprs + Array + hasInitializer() + getNumPlacementArgs();
1985 SourceLocation getStartLoc() const { return Range.getBegin(); }
1986 SourceLocation getEndLoc() const { return Range.getEnd(); }
1988 SourceRange getDirectInitRange() const { return DirectInitRange; }
1990 SourceRange getSourceRange() const LLVM_READONLY {
1993 SourceLocation getLocStart() const LLVM_READONLY { return getStartLoc(); }
1994 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1996 static bool classof(const Stmt *T) {
1997 return T->getStmtClass() == CXXNewExprClass;
2001 child_range children() {
2002 return child_range(raw_arg_begin(), raw_arg_end());
2006 /// \brief Represents a \c delete expression for memory deallocation and
2007 /// destructor calls, e.g. "delete[] pArray".
2008 class CXXDeleteExpr : public Expr {
2009 /// Points to the operator delete overload that is used. Could be a member.
2010 FunctionDecl *OperatorDelete;
2011 /// The pointer expression to be deleted.
2013 /// Location of the expression.
2015 /// Is this a forced global delete, i.e. "::delete"?
2016 bool GlobalDelete : 1;
2017 /// Is this the array form of delete, i.e. "delete[]"?
2019 /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is applied
2020 /// to pointer-to-array type (ArrayFormAsWritten will be false while ArrayForm
2022 bool ArrayFormAsWritten : 1;
2023 /// Does the usual deallocation function for the element type require
2024 /// a size_t argument?
2025 bool UsualArrayDeleteWantsSize : 1;
2027 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm,
2028 bool arrayFormAsWritten, bool usualArrayDeleteWantsSize,
2029 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc)
2030 : Expr(CXXDeleteExprClass, ty, VK_RValue, OK_Ordinary, false, false,
2031 arg->isInstantiationDependent(),
2032 arg->containsUnexpandedParameterPack()),
2033 OperatorDelete(operatorDelete), Argument(arg), Loc(loc),
2034 GlobalDelete(globalDelete),
2035 ArrayForm(arrayForm), ArrayFormAsWritten(arrayFormAsWritten),
2036 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) { }
2037 explicit CXXDeleteExpr(EmptyShell Shell)
2038 : Expr(CXXDeleteExprClass, Shell), OperatorDelete(nullptr),
2039 Argument(nullptr) {}
2041 bool isGlobalDelete() const { return GlobalDelete; }
2042 bool isArrayForm() const { return ArrayForm; }
2043 bool isArrayFormAsWritten() const { return ArrayFormAsWritten; }
2045 /// Answers whether the usual array deallocation function for the
2046 /// allocated type expects the size of the allocation as a
2047 /// parameter. This can be true even if the actual deallocation
2048 /// function that we're using doesn't want a size.
2049 bool doesUsualArrayDeleteWantSize() const {
2050 return UsualArrayDeleteWantsSize;
2053 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2055 Expr *getArgument() { return cast<Expr>(Argument); }
2056 const Expr *getArgument() const { return cast<Expr>(Argument); }
2058 /// \brief Retrieve the type being destroyed.
2060 /// If the type being destroyed is a dependent type which may or may not
2061 /// be a pointer, return an invalid type.
2062 QualType getDestroyedType() const;
2064 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
2065 SourceLocation getLocEnd() const LLVM_READONLY {return Argument->getLocEnd();}
2067 static bool classof(const Stmt *T) {
2068 return T->getStmtClass() == CXXDeleteExprClass;
2072 child_range children() { return child_range(&Argument, &Argument+1); }
2074 friend class ASTStmtReader;
2077 /// \brief Stores the type being destroyed by a pseudo-destructor expression.
2078 class PseudoDestructorTypeStorage {
2079 /// \brief Either the type source information or the name of the type, if
2080 /// it couldn't be resolved due to type-dependence.
2081 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type;
2083 /// \brief The starting source location of the pseudo-destructor type.
2084 SourceLocation Location;
2087 PseudoDestructorTypeStorage() { }
2089 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc)
2090 : Type(II), Location(Loc) { }
2092 PseudoDestructorTypeStorage(TypeSourceInfo *Info);
2094 TypeSourceInfo *getTypeSourceInfo() const {
2095 return Type.dyn_cast<TypeSourceInfo *>();
2098 IdentifierInfo *getIdentifier() const {
2099 return Type.dyn_cast<IdentifierInfo *>();
2102 SourceLocation getLocation() const { return Location; }
2105 /// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
2107 /// A pseudo-destructor is an expression that looks like a member access to a
2108 /// destructor of a scalar type, except that scalar types don't have
2109 /// destructors. For example:
2113 /// void f(int *p) {
2118 /// Pseudo-destructors typically occur when instantiating templates such as:
2121 /// template<typename T>
2122 /// void destroy(T* ptr) {
2127 /// for scalar types. A pseudo-destructor expression has no run-time semantics
2128 /// beyond evaluating the base expression.
2129 class CXXPseudoDestructorExpr : public Expr {
2130 /// \brief The base expression (that is being destroyed).
2133 /// \brief Whether the operator was an arrow ('->'); otherwise, it was a
2137 /// \brief The location of the '.' or '->' operator.
2138 SourceLocation OperatorLoc;
2140 /// \brief The nested-name-specifier that follows the operator, if present.
2141 NestedNameSpecifierLoc QualifierLoc;
2143 /// \brief The type that precedes the '::' in a qualified pseudo-destructor
2145 TypeSourceInfo *ScopeType;
2147 /// \brief The location of the '::' in a qualified pseudo-destructor
2149 SourceLocation ColonColonLoc;
2151 /// \brief The location of the '~'.
2152 SourceLocation TildeLoc;
2154 /// \brief The type being destroyed, or its name if we were unable to
2155 /// resolve the name.
2156 PseudoDestructorTypeStorage DestroyedType;
2158 friend class ASTStmtReader;
2161 CXXPseudoDestructorExpr(const ASTContext &Context,
2162 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
2163 NestedNameSpecifierLoc QualifierLoc,
2164 TypeSourceInfo *ScopeType,
2165 SourceLocation ColonColonLoc,
2166 SourceLocation TildeLoc,
2167 PseudoDestructorTypeStorage DestroyedType);
2169 explicit CXXPseudoDestructorExpr(EmptyShell Shell)
2170 : Expr(CXXPseudoDestructorExprClass, Shell),
2171 Base(nullptr), IsArrow(false), QualifierLoc(), ScopeType(nullptr) { }
2173 Expr *getBase() const { return cast<Expr>(Base); }
2175 /// \brief Determines whether this member expression actually had
2176 /// a C++ nested-name-specifier prior to the name of the member, e.g.,
2178 bool hasQualifier() const { return QualifierLoc.hasQualifier(); }
2180 /// \brief Retrieves the nested-name-specifier that qualifies the type name,
2181 /// with source-location information.
2182 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2184 /// \brief If the member name was qualified, retrieves the
2185 /// nested-name-specifier that precedes the member name. Otherwise, returns
2187 NestedNameSpecifier *getQualifier() const {
2188 return QualifierLoc.getNestedNameSpecifier();
2191 /// \brief Determine whether this pseudo-destructor expression was written
2192 /// using an '->' (otherwise, it used a '.').
2193 bool isArrow() const { return IsArrow; }
2195 /// \brief Retrieve the location of the '.' or '->' operator.
2196 SourceLocation getOperatorLoc() const { return OperatorLoc; }
2198 /// \brief Retrieve the scope type in a qualified pseudo-destructor
2201 /// Pseudo-destructor expressions can have extra qualification within them
2202 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T().
2203 /// Here, if the object type of the expression is (or may be) a scalar type,
2204 /// \p T may also be a scalar type and, therefore, cannot be part of a
2205 /// nested-name-specifier. It is stored as the "scope type" of the pseudo-
2206 /// destructor expression.
2207 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; }
2209 /// \brief Retrieve the location of the '::' in a qualified pseudo-destructor
2211 SourceLocation getColonColonLoc() const { return ColonColonLoc; }
2213 /// \brief Retrieve the location of the '~'.
2214 SourceLocation getTildeLoc() const { return TildeLoc; }
2216 /// \brief Retrieve the source location information for the type
2217 /// being destroyed.
2219 /// This type-source information is available for non-dependent
2220 /// pseudo-destructor expressions and some dependent pseudo-destructor
2221 /// expressions. Returns null if we only have the identifier for a
2222 /// dependent pseudo-destructor expression.
2223 TypeSourceInfo *getDestroyedTypeInfo() const {
2224 return DestroyedType.getTypeSourceInfo();
2227 /// \brief In a dependent pseudo-destructor expression for which we do not
2228 /// have full type information on the destroyed type, provides the name
2229 /// of the destroyed type.
2230 IdentifierInfo *getDestroyedTypeIdentifier() const {
2231 return DestroyedType.getIdentifier();
2234 /// \brief Retrieve the type being destroyed.
2235 QualType getDestroyedType() const;
2237 /// \brief Retrieve the starting location of the type being destroyed.
2238 SourceLocation getDestroyedTypeLoc() const {
2239 return DestroyedType.getLocation();
2242 /// \brief Set the name of destroyed type for a dependent pseudo-destructor
2244 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) {
2245 DestroyedType = PseudoDestructorTypeStorage(II, Loc);
2248 /// \brief Set the destroyed type.
2249 void setDestroyedType(TypeSourceInfo *Info) {
2250 DestroyedType = PseudoDestructorTypeStorage(Info);
2253 SourceLocation getLocStart() const LLVM_READONLY {return Base->getLocStart();}
2254 SourceLocation getLocEnd() const LLVM_READONLY;
2256 static bool classof(const Stmt *T) {
2257 return T->getStmtClass() == CXXPseudoDestructorExprClass;
2261 child_range children() { return child_range(&Base, &Base + 1); }
2264 /// \brief A type trait used in the implementation of various C++11 and
2265 /// Library TR1 trait templates.
2268 /// __is_pod(int) == true
2269 /// __is_enum(std::string) == false
2270 /// __is_trivially_constructible(vector<int>, int*, int*)
2272 class TypeTraitExpr final
2274 private llvm::TrailingObjects<TypeTraitExpr, TypeSourceInfo *> {
2275 /// \brief The location of the type trait keyword.
2278 /// \brief The location of the closing parenthesis.
2279 SourceLocation RParenLoc;
2281 // Note: The TypeSourceInfos for the arguments are allocated after the
2284 TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
2285 ArrayRef<TypeSourceInfo *> Args,
2286 SourceLocation RParenLoc,
2289 TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) { }
2291 size_t numTrailingObjects(OverloadToken<TypeSourceInfo *>) const {
2292 return getNumArgs();
2296 /// \brief Create a new type trait expression.
2297 static TypeTraitExpr *Create(const ASTContext &C, QualType T,
2298 SourceLocation Loc, TypeTrait Kind,
2299 ArrayRef<TypeSourceInfo *> Args,
2300 SourceLocation RParenLoc,
2303 static TypeTraitExpr *CreateDeserialized(const ASTContext &C,
2306 /// \brief Determine which type trait this expression uses.
2307 TypeTrait getTrait() const {
2308 return static_cast<TypeTrait>(TypeTraitExprBits.Kind);
2311 bool getValue() const {
2312 assert(!isValueDependent());
2313 return TypeTraitExprBits.Value;
2316 /// \brief Determine the number of arguments to this type trait.
2317 unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; }
2319 /// \brief Retrieve the Ith argument.
2320 TypeSourceInfo *getArg(unsigned I) const {
2321 assert(I < getNumArgs() && "Argument out-of-range");
2322 return getArgs()[I];
2325 /// \brief Retrieve the argument types.
2326 ArrayRef<TypeSourceInfo *> getArgs() const {
2327 return llvm::makeArrayRef(getTrailingObjects<TypeSourceInfo *>(),
2331 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
2332 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
2334 static bool classof(const Stmt *T) {
2335 return T->getStmtClass() == TypeTraitExprClass;
2339 child_range children() {
2340 return child_range(child_iterator(), child_iterator());
2343 friend TrailingObjects;
2344 friend class ASTStmtReader;
2345 friend class ASTStmtWriter;
2348 /// \brief An Embarcadero array type trait, as used in the implementation of
2349 /// __array_rank and __array_extent.
2353 /// __array_rank(int[10][20]) == 2
2354 /// __array_extent(int, 1) == 20
2356 class ArrayTypeTraitExpr : public Expr {
2357 virtual void anchor();
2359 /// \brief The trait. An ArrayTypeTrait enum in MSVC compat unsigned.
2362 /// \brief The value of the type trait. Unspecified if dependent.
2365 /// \brief The array dimension being queried, or -1 if not used.
2368 /// \brief The location of the type trait keyword.
2371 /// \brief The location of the closing paren.
2372 SourceLocation RParen;
2374 /// \brief The type being queried.
2375 TypeSourceInfo *QueriedType;
2378 ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att,
2379 TypeSourceInfo *queried, uint64_t value,
2380 Expr *dimension, SourceLocation rparen, QualType ty)
2381 : Expr(ArrayTypeTraitExprClass, ty, VK_RValue, OK_Ordinary,
2382 false, queried->getType()->isDependentType(),
2383 (queried->getType()->isInstantiationDependentType() ||
2384 (dimension && dimension->isInstantiationDependent())),
2385 queried->getType()->containsUnexpandedParameterPack()),
2386 ATT(att), Value(value), Dimension(dimension),
2387 Loc(loc), RParen(rparen), QueriedType(queried) { }
2390 explicit ArrayTypeTraitExpr(EmptyShell Empty)
2391 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0), Value(false),
2394 virtual ~ArrayTypeTraitExpr() { }
2396 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
2397 SourceLocation getLocEnd() const LLVM_READONLY { return RParen; }
2399 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); }
2401 QualType getQueriedType() const { return QueriedType->getType(); }
2403 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; }
2405 uint64_t getValue() const { assert(!isTypeDependent()); return Value; }
2407 Expr *getDimensionExpression() const { return Dimension; }
2409 static bool classof(const Stmt *T) {
2410 return T->getStmtClass() == ArrayTypeTraitExprClass;
2414 child_range children() {
2415 return child_range(child_iterator(), child_iterator());
2418 friend class ASTStmtReader;
2421 /// \brief An expression trait intrinsic.
2425 /// __is_lvalue_expr(std::cout) == true
2426 /// __is_lvalue_expr(1) == false
2428 class ExpressionTraitExpr : public Expr {
2429 /// \brief The trait. A ExpressionTrait enum in MSVC compatible unsigned.
2431 /// \brief The value of the type trait. Unspecified if dependent.
2434 /// \brief The location of the type trait keyword.
2437 /// \brief The location of the closing paren.
2438 SourceLocation RParen;
2440 /// \brief The expression being queried.
2441 Expr* QueriedExpression;
2443 ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et,
2444 Expr *queried, bool value,
2445 SourceLocation rparen, QualType resultType)
2446 : Expr(ExpressionTraitExprClass, resultType, VK_RValue, OK_Ordinary,
2447 false, // Not type-dependent
2448 // Value-dependent if the argument is type-dependent.
2449 queried->isTypeDependent(),
2450 queried->isInstantiationDependent(),
2451 queried->containsUnexpandedParameterPack()),
2452 ET(et), Value(value), Loc(loc), RParen(rparen),
2453 QueriedExpression(queried) { }
2455 explicit ExpressionTraitExpr(EmptyShell Empty)
2456 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false),
2457 QueriedExpression() { }
2459 SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
2460 SourceLocation getLocEnd() const LLVM_READONLY { return RParen; }
2462 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); }
2464 Expr *getQueriedExpression() const { return QueriedExpression; }
2466 bool getValue() const { return Value; }
2468 static bool classof(const Stmt *T) {
2469 return T->getStmtClass() == ExpressionTraitExprClass;
2473 child_range children() {
2474 return child_range(child_iterator(), child_iterator());
2477 friend class ASTStmtReader;
2481 /// \brief A reference to an overloaded function set, either an
2482 /// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr.
2483 class OverloadExpr : public Expr {
2484 /// \brief The common name of these declarations.
2485 DeclarationNameInfo NameInfo;
2487 /// \brief The nested-name-specifier that qualifies the name, if any.
2488 NestedNameSpecifierLoc QualifierLoc;
2490 /// The results. These are undesugared, which is to say, they may
2491 /// include UsingShadowDecls. Access is relative to the naming
2493 // FIXME: Allocate this data after the OverloadExpr subclass.
2494 DeclAccessPair *Results;
2495 unsigned NumResults;
2498 /// \brief Whether the name includes info for explicit template
2499 /// keyword and arguments.
2500 bool HasTemplateKWAndArgsInfo;
2502 /// \brief Return the optional template keyword and arguments info.
2503 ASTTemplateKWAndArgsInfo *
2504 getTrailingASTTemplateKWAndArgsInfo(); // defined far below.
2506 /// \brief Return the optional template keyword and arguments info.
2507 const ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo() const {
2508 return const_cast<OverloadExpr *>(this)
2509 ->getTrailingASTTemplateKWAndArgsInfo();
2512 /// Return the optional template arguments.
2513 TemplateArgumentLoc *getTrailingTemplateArgumentLoc(); // defined far below
2515 OverloadExpr(StmtClass K, const ASTContext &C,
2516 NestedNameSpecifierLoc QualifierLoc,
2517 SourceLocation TemplateKWLoc,
2518 const DeclarationNameInfo &NameInfo,
2519 const TemplateArgumentListInfo *TemplateArgs,
2520 UnresolvedSetIterator Begin, UnresolvedSetIterator End,
2521 bool KnownDependent,
2522 bool KnownInstantiationDependent,
2523 bool KnownContainsUnexpandedParameterPack);
2525 OverloadExpr(StmtClass K, EmptyShell Empty)
2526 : Expr(K, Empty), QualifierLoc(), Results(nullptr), NumResults(0),
2527 HasTemplateKWAndArgsInfo(false) { }
2529 void initializeResults(const ASTContext &C,
2530 UnresolvedSetIterator Begin,
2531 UnresolvedSetIterator End);
2535 OverloadExpr *Expression;
2536 bool IsAddressOfOperand;
2537 bool HasFormOfMemberPointer;
2540 /// \brief Finds the overloaded expression in the given expression \p E of
2543 /// \return the expression (which must be there) and true if it has
2544 /// the particular form of a member pointer expression
2545 static FindResult find(Expr *E) {
2546 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload));
2550 E = E->IgnoreParens();
2551 if (isa<UnaryOperator>(E)) {
2552 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf);
2553 E = cast<UnaryOperator>(E)->getSubExpr();
2554 OverloadExpr *Ovl = cast<OverloadExpr>(E->IgnoreParens());
2556 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier());
2557 Result.IsAddressOfOperand = true;
2558 Result.Expression = Ovl;
2560 Result.HasFormOfMemberPointer = false;
2561 Result.IsAddressOfOperand = false;
2562 Result.Expression = cast<OverloadExpr>(E);
2568 /// \brief Gets the naming class of this lookup, if any.
2569 CXXRecordDecl *getNamingClass() const;
2571 typedef UnresolvedSetImpl::iterator decls_iterator;
2572 decls_iterator decls_begin() const { return UnresolvedSetIterator(Results); }
2573 decls_iterator decls_end() const {
2574 return UnresolvedSetIterator(Results + NumResults);
2576 llvm::iterator_range<decls_iterator> decls() const {
2577 return llvm::make_range(decls_begin(), decls_end());
2580 /// \brief Gets the number of declarations in the unresolved set.
2581 unsigned getNumDecls() const { return NumResults; }
2583 /// \brief Gets the full name info.
2584 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2586 /// \brief Gets the name looked up.
2587 DeclarationName getName() const { return NameInfo.getName(); }
2589 /// \brief Gets the location of the name.
2590 SourceLocation getNameLoc() const { return NameInfo.getLoc(); }
2592 /// \brief Fetches the nested-name qualifier, if one was given.
2593 NestedNameSpecifier *getQualifier() const {
2594 return QualifierLoc.getNestedNameSpecifier();
2597 /// \brief Fetches the nested-name qualifier with source-location
2598 /// information, if one was given.
2599 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2601 /// \brief Retrieve the location of the template keyword preceding
2602 /// this name, if any.
2603 SourceLocation getTemplateKeywordLoc() const {
2604 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2605 return getTrailingASTTemplateKWAndArgsInfo()->TemplateKWLoc;
2608 /// \brief Retrieve the location of the left angle bracket starting the
2609 /// explicit template argument list following the name, if any.
2610 SourceLocation getLAngleLoc() const {
2611 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2612 return getTrailingASTTemplateKWAndArgsInfo()->LAngleLoc;
2615 /// \brief Retrieve the location of the right angle bracket ending the
2616 /// explicit template argument list following the name, if any.
2617 SourceLocation getRAngleLoc() const {
2618 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2619 return getTrailingASTTemplateKWAndArgsInfo()->RAngleLoc;
2622 /// \brief Determines whether the name was preceded by the template keyword.
2623 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
2625 /// \brief Determines whether this expression had explicit template arguments.
2626 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
2628 TemplateArgumentLoc const *getTemplateArgs() const {
2629 if (!hasExplicitTemplateArgs())
2631 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
2634 unsigned getNumTemplateArgs() const {
2635 if (!hasExplicitTemplateArgs())
2638 return getTrailingASTTemplateKWAndArgsInfo()->NumTemplateArgs;
2641 ArrayRef<TemplateArgumentLoc> template_arguments() const {
2642 return {getTemplateArgs(), getNumTemplateArgs()};
2645 /// \brief Copies the template arguments into the given structure.
2646 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
2647 if (hasExplicitTemplateArgs())
2648 getTrailingASTTemplateKWAndArgsInfo()->copyInto(getTemplateArgs(), List);
2651 static bool classof(const Stmt *T) {
2652 return T->getStmtClass() == UnresolvedLookupExprClass ||
2653 T->getStmtClass() == UnresolvedMemberExprClass;
2656 friend class ASTStmtReader;
2657 friend class ASTStmtWriter;
2660 /// \brief A reference to a name which we were able to look up during
2661 /// parsing but could not resolve to a specific declaration.
2663 /// This arises in several ways:
2664 /// * we might be waiting for argument-dependent lookup;
2665 /// * the name might resolve to an overloaded function;
2667 /// * the lookup might have included a function template.
2669 /// These never include UnresolvedUsingValueDecls, which are always class
2670 /// members and therefore appear only in UnresolvedMemberLookupExprs.
2671 class UnresolvedLookupExpr final
2672 : public OverloadExpr,
2673 private llvm::TrailingObjects<
2674 UnresolvedLookupExpr, ASTTemplateKWAndArgsInfo, TemplateArgumentLoc> {
2675 /// True if these lookup results should be extended by
2676 /// argument-dependent lookup if this is the operand of a function
2680 /// True if these lookup results are overloaded. This is pretty
2681 /// trivially rederivable if we urgently need to kill this field.
2684 /// The naming class (C++ [class.access.base]p5) of the lookup, if
2685 /// any. This can generally be recalculated from the context chain,
2686 /// but that can be fairly expensive for unqualified lookups. If we
2687 /// want to improve memory use here, this could go in a union
2688 /// against the qualified-lookup bits.
2689 CXXRecordDecl *NamingClass;
2691 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
2692 return HasTemplateKWAndArgsInfo ? 1 : 0;
2695 UnresolvedLookupExpr(const ASTContext &C,
2696 CXXRecordDecl *NamingClass,
2697 NestedNameSpecifierLoc QualifierLoc,
2698 SourceLocation TemplateKWLoc,
2699 const DeclarationNameInfo &NameInfo,
2700 bool RequiresADL, bool Overloaded,
2701 const TemplateArgumentListInfo *TemplateArgs,
2702 UnresolvedSetIterator Begin, UnresolvedSetIterator End)
2703 : OverloadExpr(UnresolvedLookupExprClass, C, QualifierLoc, TemplateKWLoc,
2704 NameInfo, TemplateArgs, Begin, End, false, false, false),
2705 RequiresADL(RequiresADL),
2706 Overloaded(Overloaded), NamingClass(NamingClass)
2709 UnresolvedLookupExpr(EmptyShell Empty)
2710 : OverloadExpr(UnresolvedLookupExprClass, Empty),
2711 RequiresADL(false), Overloaded(false), NamingClass(nullptr)
2714 friend TrailingObjects;
2715 friend class OverloadExpr;
2716 friend class ASTStmtReader;
2719 static UnresolvedLookupExpr *Create(const ASTContext &C,
2720 CXXRecordDecl *NamingClass,
2721 NestedNameSpecifierLoc QualifierLoc,
2722 const DeclarationNameInfo &NameInfo,
2723 bool ADL, bool Overloaded,
2724 UnresolvedSetIterator Begin,
2725 UnresolvedSetIterator End) {
2726 return new(C) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
2727 SourceLocation(), NameInfo,
2728 ADL, Overloaded, nullptr, Begin, End);
2731 static UnresolvedLookupExpr *Create(const ASTContext &C,
2732 CXXRecordDecl *NamingClass,
2733 NestedNameSpecifierLoc QualifierLoc,
2734 SourceLocation TemplateKWLoc,
2735 const DeclarationNameInfo &NameInfo,
2737 const TemplateArgumentListInfo *Args,
2738 UnresolvedSetIterator Begin,
2739 UnresolvedSetIterator End);
2741 static UnresolvedLookupExpr *CreateEmpty(const ASTContext &C,
2742 bool HasTemplateKWAndArgsInfo,
2743 unsigned NumTemplateArgs);
2745 /// True if this declaration should be extended by
2746 /// argument-dependent lookup.
2747 bool requiresADL() const { return RequiresADL; }
2749 /// True if this lookup is overloaded.
2750 bool isOverloaded() const { return Overloaded; }
2752 /// Gets the 'naming class' (in the sense of C++0x
2753 /// [class.access.base]p5) of the lookup. This is the scope
2754 /// that was looked in to find these results.
2755 CXXRecordDecl *getNamingClass() const { return NamingClass; }
2757 SourceLocation getLocStart() const LLVM_READONLY {
2758 if (NestedNameSpecifierLoc l = getQualifierLoc())
2759 return l.getBeginLoc();
2760 return getNameInfo().getLocStart();
2762 SourceLocation getLocEnd() const LLVM_READONLY {
2763 if (hasExplicitTemplateArgs())
2764 return getRAngleLoc();
2765 return getNameInfo().getLocEnd();
2768 child_range children() {
2769 return child_range(child_iterator(), child_iterator());
2772 static bool classof(const Stmt *T) {
2773 return T->getStmtClass() == UnresolvedLookupExprClass;
2777 /// \brief A qualified reference to a name whose declaration cannot
2778 /// yet be resolved.
2780 /// DependentScopeDeclRefExpr is similar to DeclRefExpr in that
2781 /// it expresses a reference to a declaration such as
2782 /// X<T>::value. The difference, however, is that an
2783 /// DependentScopeDeclRefExpr node is used only within C++ templates when
2784 /// the qualification (e.g., X<T>::) refers to a dependent type. In
2785 /// this case, X<T>::value cannot resolve to a declaration because the
2786 /// declaration will differ from one instantiation of X<T> to the
2787 /// next. Therefore, DependentScopeDeclRefExpr keeps track of the
2788 /// qualifier (X<T>::) and the name of the entity being referenced
2789 /// ("value"). Such expressions will instantiate to a DeclRefExpr once the
2790 /// declaration can be found.
2791 class DependentScopeDeclRefExpr final
2793 private llvm::TrailingObjects<DependentScopeDeclRefExpr,
2794 ASTTemplateKWAndArgsInfo,
2795 TemplateArgumentLoc> {
2796 /// \brief The nested-name-specifier that qualifies this unresolved
2797 /// declaration name.
2798 NestedNameSpecifierLoc QualifierLoc;
2800 /// \brief The name of the entity we will be referencing.
2801 DeclarationNameInfo NameInfo;
2803 /// \brief Whether the name includes info for explicit template
2804 /// keyword and arguments.
2805 bool HasTemplateKWAndArgsInfo;
2807 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
2808 return HasTemplateKWAndArgsInfo ? 1 : 0;
2811 DependentScopeDeclRefExpr(QualType T,
2812 NestedNameSpecifierLoc QualifierLoc,
2813 SourceLocation TemplateKWLoc,
2814 const DeclarationNameInfo &NameInfo,
2815 const TemplateArgumentListInfo *Args);
2818 static DependentScopeDeclRefExpr *Create(const ASTContext &C,
2819 NestedNameSpecifierLoc QualifierLoc,
2820 SourceLocation TemplateKWLoc,
2821 const DeclarationNameInfo &NameInfo,
2822 const TemplateArgumentListInfo *TemplateArgs);
2824 static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &C,
2825 bool HasTemplateKWAndArgsInfo,
2826 unsigned NumTemplateArgs);
2828 /// \brief Retrieve the name that this expression refers to.
2829 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2831 /// \brief Retrieve the name that this expression refers to.
2832 DeclarationName getDeclName() const { return NameInfo.getName(); }
2834 /// \brief Retrieve the location of the name within the expression.
2836 /// For example, in "X<T>::value" this is the location of "value".
2837 SourceLocation getLocation() const { return NameInfo.getLoc(); }
2839 /// \brief Retrieve the nested-name-specifier that qualifies the
2840 /// name, with source location information.
2841 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2843 /// \brief Retrieve the nested-name-specifier that qualifies this
2845 NestedNameSpecifier *getQualifier() const {
2846 return QualifierLoc.getNestedNameSpecifier();
2849 /// \brief Retrieve the location of the template keyword preceding
2850 /// this name, if any.
2851 SourceLocation getTemplateKeywordLoc() const {
2852 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2853 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
2856 /// \brief Retrieve the location of the left angle bracket starting the
2857 /// explicit template argument list following the name, if any.
2858 SourceLocation getLAngleLoc() const {
2859 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2860 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
2863 /// \brief Retrieve the location of the right angle bracket ending the
2864 /// explicit template argument list following the name, if any.
2865 SourceLocation getRAngleLoc() const {
2866 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
2867 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
2870 /// Determines whether the name was preceded by the template keyword.
2871 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
2873 /// Determines whether this lookup had explicit template arguments.
2874 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
2876 /// \brief Copies the template arguments (if present) into the given
2878 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
2879 if (hasExplicitTemplateArgs())
2880 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
2881 getTrailingObjects<TemplateArgumentLoc>(), List);
2884 TemplateArgumentLoc const *getTemplateArgs() const {
2885 if (!hasExplicitTemplateArgs())
2888 return getTrailingObjects<TemplateArgumentLoc>();
2891 unsigned getNumTemplateArgs() const {
2892 if (!hasExplicitTemplateArgs())
2895 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
2898 ArrayRef<TemplateArgumentLoc> template_arguments() const {
2899 return {getTemplateArgs(), getNumTemplateArgs()};
2902 /// Note: getLocStart() is the start of the whole DependentScopeDeclRefExpr,
2903 /// and differs from getLocation().getStart().
2904 SourceLocation getLocStart() const LLVM_READONLY {
2905 return QualifierLoc.getBeginLoc();
2907 SourceLocation getLocEnd() const LLVM_READONLY {
2908 if (hasExplicitTemplateArgs())
2909 return getRAngleLoc();
2910 return getLocation();
2913 static bool classof(const Stmt *T) {
2914 return T->getStmtClass() == DependentScopeDeclRefExprClass;
2917 child_range children() {
2918 return child_range(child_iterator(), child_iterator());
2921 friend TrailingObjects;
2922 friend class ASTStmtReader;
2923 friend class ASTStmtWriter;
2926 /// Represents an expression -- generally a full-expression -- that
2927 /// introduces cleanups to be run at the end of the sub-expression's
2928 /// evaluation. The most common source of expression-introduced
2929 /// cleanups is temporary objects in C++, but several other kinds of
2930 /// expressions can create cleanups, including basically every
2931 /// call in ARC that returns an Objective-C pointer.
2933 /// This expression also tracks whether the sub-expression contains a
2934 /// potentially-evaluated block literal. The lifetime of a block
2935 /// literal is the extent of the enclosing scope.
2936 class ExprWithCleanups final
2938 private llvm::TrailingObjects<ExprWithCleanups, BlockDecl *> {
2940 /// The type of objects that are kept in the cleanup.
2941 /// It's useful to remember the set of blocks; we could also
2942 /// remember the set of temporaries, but there's currently
2944 typedef BlockDecl *CleanupObject;
2949 ExprWithCleanups(EmptyShell, unsigned NumObjects);
2950 ExprWithCleanups(Expr *SubExpr, bool CleanupsHaveSideEffects,
2951 ArrayRef<CleanupObject> Objects);
2953 friend TrailingObjects;
2954 friend class ASTStmtReader;
2957 static ExprWithCleanups *Create(const ASTContext &C, EmptyShell empty,
2958 unsigned numObjects);
2960 static ExprWithCleanups *Create(const ASTContext &C, Expr *subexpr,
2961 bool CleanupsHaveSideEffects,
2962 ArrayRef<CleanupObject> objects);
2964 ArrayRef<CleanupObject> getObjects() const {
2965 return llvm::makeArrayRef(getTrailingObjects<CleanupObject>(),
2969 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; }
2971 CleanupObject getObject(unsigned i) const {
2972 assert(i < getNumObjects() && "Index out of range");
2973 return getObjects()[i];
2976 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
2977 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
2978 bool cleanupsHaveSideEffects() const {
2979 return ExprWithCleanupsBits.CleanupsHaveSideEffects;
2982 /// As with any mutator of the AST, be very careful
2983 /// when modifying an existing AST to preserve its invariants.
2984 void setSubExpr(Expr *E) { SubExpr = E; }
2986 SourceLocation getLocStart() const LLVM_READONLY {
2987 return SubExpr->getLocStart();
2989 SourceLocation getLocEnd() const LLVM_READONLY { return SubExpr->getLocEnd();}
2991 // Implement isa/cast/dyncast/etc.
2992 static bool classof(const Stmt *T) {
2993 return T->getStmtClass() == ExprWithCleanupsClass;
2997 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
3000 /// \brief Describes an explicit type conversion that uses functional
3001 /// notion but could not be resolved because one or more arguments are
3004 /// The explicit type conversions expressed by
3005 /// CXXUnresolvedConstructExpr have the form <tt>T(a1, a2, ..., aN)</tt>,
3006 /// where \c T is some type and \c a1, \c a2, ..., \c aN are values, and
3007 /// either \c T is a dependent type or one or more of the <tt>a</tt>'s is
3008 /// type-dependent. For example, this would occur in a template such
3012 /// template<typename T, typename A1>
3013 /// inline T make_a(const A1& a1) {
3018 /// When the returned expression is instantiated, it may resolve to a
3019 /// constructor call, conversion function call, or some kind of type
3021 class CXXUnresolvedConstructExpr final
3023 private llvm::TrailingObjects<CXXUnresolvedConstructExpr, Expr *> {
3024 /// \brief The type being constructed.
3025 TypeSourceInfo *Type;
3027 /// \brief The location of the left parentheses ('(').
3028 SourceLocation LParenLoc;
3030 /// \brief The location of the right parentheses (')').
3031 SourceLocation RParenLoc;
3033 /// \brief The number of arguments used to construct the type.
3036 CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
3037 SourceLocation LParenLoc,
3038 ArrayRef<Expr*> Args,
3039 SourceLocation RParenLoc);
3041 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs)
3042 : Expr(CXXUnresolvedConstructExprClass, Empty), Type(), NumArgs(NumArgs) { }
3044 friend TrailingObjects;
3045 friend class ASTStmtReader;
3048 static CXXUnresolvedConstructExpr *Create(const ASTContext &C,
3049 TypeSourceInfo *Type,
3050 SourceLocation LParenLoc,
3051 ArrayRef<Expr*> Args,
3052 SourceLocation RParenLoc);
3054 static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &C,
3057 /// \brief Retrieve the type that is being constructed, as specified
3058 /// in the source code.
3059 QualType getTypeAsWritten() const { return Type->getType(); }
3061 /// \brief Retrieve the type source information for the type being
3063 TypeSourceInfo *getTypeSourceInfo() const { return Type; }
3065 /// \brief Retrieve the location of the left parentheses ('(') that
3066 /// precedes the argument list.
3067 SourceLocation getLParenLoc() const { return LParenLoc; }
3068 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
3070 /// \brief Retrieve the location of the right parentheses (')') that
3071 /// follows the argument list.
3072 SourceLocation getRParenLoc() const { return RParenLoc; }
3073 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
3075 /// \brief Retrieve the number of arguments.
3076 unsigned arg_size() const { return NumArgs; }
3078 typedef Expr** arg_iterator;
3079 arg_iterator arg_begin() { return getTrailingObjects<Expr *>(); }
3080 arg_iterator arg_end() { return arg_begin() + NumArgs; }
3082 typedef const Expr* const * const_arg_iterator;
3083 const_arg_iterator arg_begin() const { return getTrailingObjects<Expr *>(); }
3084 const_arg_iterator arg_end() const {
3085 return arg_begin() + NumArgs;
3088 Expr *getArg(unsigned I) {
3089 assert(I < NumArgs && "Argument index out-of-range");
3090 return *(arg_begin() + I);
3093 const Expr *getArg(unsigned I) const {
3094 assert(I < NumArgs && "Argument index out-of-range");
3095 return *(arg_begin() + I);
3098 void setArg(unsigned I, Expr *E) {
3099 assert(I < NumArgs && "Argument index out-of-range");
3100 *(arg_begin() + I) = E;
3103 SourceLocation getLocStart() const LLVM_READONLY;
3104 SourceLocation getLocEnd() const LLVM_READONLY {
3105 if (!RParenLoc.isValid() && NumArgs > 0)
3106 return getArg(NumArgs - 1)->getLocEnd();
3110 static bool classof(const Stmt *T) {
3111 return T->getStmtClass() == CXXUnresolvedConstructExprClass;
3115 child_range children() {
3116 Stmt **begin = reinterpret_cast<Stmt **>(arg_begin());
3117 return child_range(begin, begin + NumArgs);
3121 /// \brief Represents a C++ member access expression where the actual
3122 /// member referenced could not be resolved because the base
3123 /// expression or the member name was dependent.
3125 /// Like UnresolvedMemberExprs, these can be either implicit or
3126 /// explicit accesses. It is only possible to get one of these with
3127 /// an implicit access if a qualifier is provided.
3128 class CXXDependentScopeMemberExpr final
3130 private llvm::TrailingObjects<CXXDependentScopeMemberExpr,
3131 ASTTemplateKWAndArgsInfo,
3132 TemplateArgumentLoc> {
3133 /// \brief The expression for the base pointer or class reference,
3134 /// e.g., the \c x in x.f. Can be null in implicit accesses.
3137 /// \brief The type of the base expression. Never null, even for
3138 /// implicit accesses.
3141 /// \brief Whether this member expression used the '->' operator or
3142 /// the '.' operator.
3145 /// \brief Whether this member expression has info for explicit template
3146 /// keyword and arguments.
3147 bool HasTemplateKWAndArgsInfo : 1;
3149 /// \brief The location of the '->' or '.' operator.
3150 SourceLocation OperatorLoc;
3152 /// \brief The nested-name-specifier that precedes the member name, if any.
3153 NestedNameSpecifierLoc QualifierLoc;
3155 /// \brief In a qualified member access expression such as t->Base::f, this
3156 /// member stores the resolves of name lookup in the context of the member
3157 /// access expression, to be used at instantiation time.
3159 /// FIXME: This member, along with the QualifierLoc, could
3160 /// be stuck into a structure that is optionally allocated at the end of
3161 /// the CXXDependentScopeMemberExpr, to save space in the common case.
3162 NamedDecl *FirstQualifierFoundInScope;
3164 /// \brief The member to which this member expression refers, which
3165 /// can be name, overloaded operator, or destructor.
3167 /// FIXME: could also be a template-id
3168 DeclarationNameInfo MemberNameInfo;
3170 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3171 return HasTemplateKWAndArgsInfo ? 1 : 0;
3174 CXXDependentScopeMemberExpr(const ASTContext &C, Expr *Base,
3175 QualType BaseType, bool IsArrow,
3176 SourceLocation OperatorLoc,
3177 NestedNameSpecifierLoc QualifierLoc,
3178 SourceLocation TemplateKWLoc,
3179 NamedDecl *FirstQualifierFoundInScope,
3180 DeclarationNameInfo MemberNameInfo,
3181 const TemplateArgumentListInfo *TemplateArgs);
3184 CXXDependentScopeMemberExpr(const ASTContext &C, Expr *Base,
3185 QualType BaseType, bool IsArrow,
3186 SourceLocation OperatorLoc,
3187 NestedNameSpecifierLoc QualifierLoc,
3188 NamedDecl *FirstQualifierFoundInScope,
3189 DeclarationNameInfo MemberNameInfo);
3191 static CXXDependentScopeMemberExpr *
3192 Create(const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
3193 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
3194 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
3195 DeclarationNameInfo MemberNameInfo,
3196 const TemplateArgumentListInfo *TemplateArgs);
3198 static CXXDependentScopeMemberExpr *
3199 CreateEmpty(const ASTContext &C, bool HasTemplateKWAndArgsInfo,
3200 unsigned NumTemplateArgs);
3202 /// \brief True if this is an implicit access, i.e. one in which the
3203 /// member being accessed was not written in the source. The source
3204 /// location of the operator is invalid in this case.
3205 bool isImplicitAccess() const;
3207 /// \brief Retrieve the base object of this member expressions,
3208 /// e.g., the \c x in \c x.m.
3209 Expr *getBase() const {
3210 assert(!isImplicitAccess());
3211 return cast<Expr>(Base);
3214 QualType getBaseType() const { return BaseType; }
3216 /// \brief Determine whether this member expression used the '->'
3217 /// operator; otherwise, it used the '.' operator.
3218 bool isArrow() const { return IsArrow; }
3220 /// \brief Retrieve the location of the '->' or '.' operator.
3221 SourceLocation getOperatorLoc() const { return OperatorLoc; }
3223 /// \brief Retrieve the nested-name-specifier that qualifies the member
3225 NestedNameSpecifier *getQualifier() const {
3226 return QualifierLoc.getNestedNameSpecifier();
3229 /// \brief Retrieve the nested-name-specifier that qualifies the member
3230 /// name, with source location information.
3231 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3234 /// \brief Retrieve the first part of the nested-name-specifier that was
3235 /// found in the scope of the member access expression when the member access
3236 /// was initially parsed.
3238 /// This function only returns a useful result when member access expression
3239 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration
3240 /// returned by this function describes what was found by unqualified name
3241 /// lookup for the identifier "Base" within the scope of the member access
3242 /// expression itself. At template instantiation time, this information is
3243 /// combined with the results of name lookup into the type of the object
3244 /// expression itself (the class type of x).
3245 NamedDecl *getFirstQualifierFoundInScope() const {
3246 return FirstQualifierFoundInScope;
3249 /// \brief Retrieve the name of the member that this expression
3251 const DeclarationNameInfo &getMemberNameInfo() const {
3252 return MemberNameInfo;
3255 /// \brief Retrieve the name of the member that this expression
3257 DeclarationName getMember() const { return MemberNameInfo.getName(); }
3259 // \brief Retrieve the location of the name of the member that this
3260 // expression refers to.
3261 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); }
3263 /// \brief Retrieve the location of the template keyword preceding the
3264 /// member name, if any.
3265 SourceLocation getTemplateKeywordLoc() const {
3266 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
3267 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
3270 /// \brief Retrieve the location of the left angle bracket starting the
3271 /// explicit template argument list following the member name, if any.
3272 SourceLocation getLAngleLoc() const {
3273 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
3274 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
3277 /// \brief Retrieve the location of the right angle bracket ending the
3278 /// explicit template argument list following the member name, if any.
3279 SourceLocation getRAngleLoc() const {
3280 if (!HasTemplateKWAndArgsInfo) return SourceLocation();
3281 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
3284 /// Determines whether the member name was preceded by the template keyword.
3285 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
3287 /// \brief Determines whether this member expression actually had a C++
3288 /// template argument list explicitly specified, e.g., x.f<int>.
3289 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3291 /// \brief Copies the template arguments (if present) into the given
3293 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
3294 if (hasExplicitTemplateArgs())
3295 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
3296 getTrailingObjects<TemplateArgumentLoc>(), List);
3299 /// \brief Retrieve the template arguments provided as part of this
3301 const TemplateArgumentLoc *getTemplateArgs() const {
3302 if (!hasExplicitTemplateArgs())
3305 return getTrailingObjects<TemplateArgumentLoc>();
3308 /// \brief Retrieve the number of template arguments provided as part of this
3310 unsigned getNumTemplateArgs() const {
3311 if (!hasExplicitTemplateArgs())
3314 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
3317 ArrayRef<TemplateArgumentLoc> template_arguments() const {
3318 return {getTemplateArgs(), getNumTemplateArgs()};
3321 SourceLocation getLocStart() const LLVM_READONLY {
3322 if (!isImplicitAccess())
3323 return Base->getLocStart();
3325 return getQualifierLoc().getBeginLoc();
3326 return MemberNameInfo.getBeginLoc();
3329 SourceLocation getLocEnd() const LLVM_READONLY {
3330 if (hasExplicitTemplateArgs())
3331 return getRAngleLoc();
3332 return MemberNameInfo.getEndLoc();
3335 static bool classof(const Stmt *T) {
3336 return T->getStmtClass() == CXXDependentScopeMemberExprClass;
3340 child_range children() {
3341 if (isImplicitAccess())
3342 return child_range(child_iterator(), child_iterator());
3343 return child_range(&Base, &Base + 1);
3346 friend TrailingObjects;
3347 friend class ASTStmtReader;
3348 friend class ASTStmtWriter;
3351 /// \brief Represents a C++ member access expression for which lookup
3352 /// produced a set of overloaded functions.
3354 /// The member access may be explicit or implicit:
3358 /// int explicitAccess() { return this->a + this->A::b; }
3359 /// int implicitAccess() { return a + A::b; }
3363 /// In the final AST, an explicit access always becomes a MemberExpr.
3364 /// An implicit access may become either a MemberExpr or a
3365 /// DeclRefExpr, depending on whether the member is static.
3366 class UnresolvedMemberExpr final
3367 : public OverloadExpr,
3368 private llvm::TrailingObjects<
3369 UnresolvedMemberExpr, ASTTemplateKWAndArgsInfo, TemplateArgumentLoc> {
3370 /// \brief Whether this member expression used the '->' operator or
3371 /// the '.' operator.
3374 /// \brief Whether the lookup results contain an unresolved using
3376 bool HasUnresolvedUsing : 1;
3378 /// \brief The expression for the base pointer or class reference,
3379 /// e.g., the \c x in x.f.
3381 /// This can be null if this is an 'unbased' member expression.
3384 /// \brief The type of the base expression; never null.
3387 /// \brief The location of the '->' or '.' operator.
3388 SourceLocation OperatorLoc;
3390 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3391 return HasTemplateKWAndArgsInfo ? 1 : 0;
3394 UnresolvedMemberExpr(const ASTContext &C, bool HasUnresolvedUsing,
3395 Expr *Base, QualType BaseType, bool IsArrow,
3396 SourceLocation OperatorLoc,
3397 NestedNameSpecifierLoc QualifierLoc,
3398 SourceLocation TemplateKWLoc,
3399 const DeclarationNameInfo &MemberNameInfo,
3400 const TemplateArgumentListInfo *TemplateArgs,
3401 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3403 UnresolvedMemberExpr(EmptyShell Empty)
3404 : OverloadExpr(UnresolvedMemberExprClass, Empty), IsArrow(false),
3405 HasUnresolvedUsing(false), Base(nullptr) { }
3407 friend TrailingObjects;
3408 friend class OverloadExpr;
3409 friend class ASTStmtReader;
3412 static UnresolvedMemberExpr *
3413 Create(const ASTContext &C, bool HasUnresolvedUsing,
3414 Expr *Base, QualType BaseType, bool IsArrow,
3415 SourceLocation OperatorLoc,
3416 NestedNameSpecifierLoc QualifierLoc,
3417 SourceLocation TemplateKWLoc,
3418 const DeclarationNameInfo &MemberNameInfo,
3419 const TemplateArgumentListInfo *TemplateArgs,
3420 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3422 static UnresolvedMemberExpr *
3423 CreateEmpty(const ASTContext &C, bool HasTemplateKWAndArgsInfo,
3424 unsigned NumTemplateArgs);
3426 /// \brief True if this is an implicit access, i.e., one in which the
3427 /// member being accessed was not written in the source.
3429 /// The source location of the operator is invalid in this case.
3430 bool isImplicitAccess() const;
3432 /// \brief Retrieve the base object of this member expressions,
3433 /// e.g., the \c x in \c x.m.
3435 assert(!isImplicitAccess());
3436 return cast<Expr>(Base);
3438 const Expr *getBase() const {
3439 assert(!isImplicitAccess());
3440 return cast<Expr>(Base);
3443 QualType getBaseType() const { return BaseType; }
3445 /// \brief Determine whether the lookup results contain an unresolved using
3447 bool hasUnresolvedUsing() const { return HasUnresolvedUsing; }
3449 /// \brief Determine whether this member expression used the '->'
3450 /// operator; otherwise, it used the '.' operator.
3451 bool isArrow() const { return IsArrow; }
3453 /// \brief Retrieve the location of the '->' or '.' operator.
3454 SourceLocation getOperatorLoc() const { return OperatorLoc; }
3456 /// \brief Retrieve the naming class of this lookup.
3457 CXXRecordDecl *getNamingClass() const;
3459 /// \brief Retrieve the full name info for the member that this expression
3461 const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); }
3463 /// \brief Retrieve the name of the member that this expression
3465 DeclarationName getMemberName() const { return getName(); }
3467 // \brief Retrieve the location of the name of the member that this
3468 // expression refers to.
3469 SourceLocation getMemberLoc() const { return getNameLoc(); }
3471 // \brief Return the preferred location (the member name) for the arrow when
3472 // diagnosing a problem with this expression.
3473 SourceLocation getExprLoc() const LLVM_READONLY { return getMemberLoc(); }
3475 SourceLocation getLocStart() const LLVM_READONLY {
3476 if (!isImplicitAccess())
3477 return Base->getLocStart();
3478 if (NestedNameSpecifierLoc l = getQualifierLoc())
3479 return l.getBeginLoc();
3480 return getMemberNameInfo().getLocStart();
3482 SourceLocation getLocEnd() const LLVM_READONLY {
3483 if (hasExplicitTemplateArgs())
3484 return getRAngleLoc();
3485 return getMemberNameInfo().getLocEnd();
3488 static bool classof(const Stmt *T) {
3489 return T->getStmtClass() == UnresolvedMemberExprClass;
3493 child_range children() {
3494 if (isImplicitAccess())
3495 return child_range(child_iterator(), child_iterator());
3496 return child_range(&Base, &Base + 1);
3500 inline ASTTemplateKWAndArgsInfo *
3501 OverloadExpr::getTrailingASTTemplateKWAndArgsInfo() {
3502 if (!HasTemplateKWAndArgsInfo)
3505 if (isa<UnresolvedLookupExpr>(this))
3506 return cast<UnresolvedLookupExpr>(this)
3507 ->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
3509 return cast<UnresolvedMemberExpr>(this)
3510 ->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
3513 inline TemplateArgumentLoc *OverloadExpr::getTrailingTemplateArgumentLoc() {
3514 if (isa<UnresolvedLookupExpr>(this))
3515 return cast<UnresolvedLookupExpr>(this)
3516 ->getTrailingObjects<TemplateArgumentLoc>();
3518 return cast<UnresolvedMemberExpr>(this)
3519 ->getTrailingObjects<TemplateArgumentLoc>();
3522 /// \brief Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
3524 /// The noexcept expression tests whether a given expression might throw. Its
3525 /// result is a boolean constant.
3526 class CXXNoexceptExpr : public Expr {
3531 friend class ASTStmtReader;
3534 CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val,
3535 SourceLocation Keyword, SourceLocation RParen)
3536 : Expr(CXXNoexceptExprClass, Ty, VK_RValue, OK_Ordinary,
3537 /*TypeDependent*/false,
3538 /*ValueDependent*/Val == CT_Dependent,
3539 Val == CT_Dependent || Operand->isInstantiationDependent(),
3540 Operand->containsUnexpandedParameterPack()),
3541 Value(Val == CT_Cannot), Operand(Operand), Range(Keyword, RParen)
3544 CXXNoexceptExpr(EmptyShell Empty)
3545 : Expr(CXXNoexceptExprClass, Empty)
3548 Expr *getOperand() const { return static_cast<Expr*>(Operand); }
3550 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
3551 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
3552 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
3554 bool getValue() const { return Value; }
3556 static bool classof(const Stmt *T) {
3557 return T->getStmtClass() == CXXNoexceptExprClass;
3561 child_range children() { return child_range(&Operand, &Operand + 1); }
3564 /// \brief Represents a C++11 pack expansion that produces a sequence of
3567 /// A pack expansion expression contains a pattern (which itself is an
3568 /// expression) followed by an ellipsis. For example:
3571 /// template<typename F, typename ...Types>
3572 /// void forward(F f, Types &&...args) {
3573 /// f(static_cast<Types&&>(args)...);
3577 /// Here, the argument to the function object \c f is a pack expansion whose
3578 /// pattern is \c static_cast<Types&&>(args). When the \c forward function
3579 /// template is instantiated, the pack expansion will instantiate to zero or
3580 /// or more function arguments to the function object \c f.
3581 class PackExpansionExpr : public Expr {
3582 SourceLocation EllipsisLoc;
3584 /// \brief The number of expansions that will be produced by this pack
3585 /// expansion expression, if known.
3587 /// When zero, the number of expansions is not known. Otherwise, this value
3588 /// is the number of expansions + 1.
3589 unsigned NumExpansions;
3593 friend class ASTStmtReader;
3594 friend class ASTStmtWriter;
3597 PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc,
3598 Optional<unsigned> NumExpansions)
3599 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
3600 Pattern->getObjectKind(), /*TypeDependent=*/true,
3601 /*ValueDependent=*/true, /*InstantiationDependent=*/true,
3602 /*ContainsUnexpandedParameterPack=*/false),
3603 EllipsisLoc(EllipsisLoc),
3604 NumExpansions(NumExpansions? *NumExpansions + 1 : 0),
3605 Pattern(Pattern) { }
3607 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) { }
3609 /// \brief Retrieve the pattern of the pack expansion.
3610 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); }
3612 /// \brief Retrieve the pattern of the pack expansion.
3613 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); }
3615 /// \brief Retrieve the location of the ellipsis that describes this pack
3617 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
3619 /// \brief Determine the number of expansions that will be produced when
3620 /// this pack expansion is instantiated, if already known.
3621 Optional<unsigned> getNumExpansions() const {
3623 return NumExpansions - 1;
3628 SourceLocation getLocStart() const LLVM_READONLY {
3629 return Pattern->getLocStart();
3631 SourceLocation getLocEnd() const LLVM_READONLY { return EllipsisLoc; }
3633 static bool classof(const Stmt *T) {
3634 return T->getStmtClass() == PackExpansionExprClass;
3638 child_range children() {
3639 return child_range(&Pattern, &Pattern + 1);
3644 /// \brief Represents an expression that computes the length of a parameter
3648 /// template<typename ...Types>
3650 /// static const unsigned value = sizeof...(Types);
3653 class SizeOfPackExpr final
3655 private llvm::TrailingObjects<SizeOfPackExpr, TemplateArgument> {
3656 /// \brief The location of the \c sizeof keyword.
3657 SourceLocation OperatorLoc;
3659 /// \brief The location of the name of the parameter pack.
3660 SourceLocation PackLoc;
3662 /// \brief The location of the closing parenthesis.
3663 SourceLocation RParenLoc;
3665 /// \brief The length of the parameter pack, if known.
3667 /// When this expression is not value-dependent, this is the length of
3668 /// the pack. When the expression was parsed rather than instantiated
3669 /// (and thus is value-dependent), this is zero.
3671 /// After partial substitution into a sizeof...(X) expression (for instance,
3672 /// within an alias template or during function template argument deduction),
3673 /// we store a trailing array of partially-substituted TemplateArguments,
3674 /// and this is the length of that array.
3677 /// \brief The parameter pack.
3680 friend TrailingObjects;
3681 friend class ASTStmtReader;
3682 friend class ASTStmtWriter;
3684 /// \brief Create an expression that computes the length of
3685 /// the given parameter pack.
3686 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack,
3687 SourceLocation PackLoc, SourceLocation RParenLoc,
3688 Optional<unsigned> Length, ArrayRef<TemplateArgument> PartialArgs)
3689 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary,
3690 /*TypeDependent=*/false, /*ValueDependent=*/!Length,
3691 /*InstantiationDependent=*/!Length,
3692 /*ContainsUnexpandedParameterPack=*/false),
3693 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc),
3694 Length(Length ? *Length : PartialArgs.size()), Pack(Pack) {
3695 assert((!Length || PartialArgs.empty()) &&
3696 "have partial args for non-dependent sizeof... expression");
3697 TemplateArgument *Args = getTrailingObjects<TemplateArgument>();
3698 std::uninitialized_copy(PartialArgs.begin(), PartialArgs.end(), Args);
3701 /// \brief Create an empty expression.
3702 SizeOfPackExpr(EmptyShell Empty, unsigned NumPartialArgs)
3703 : Expr(SizeOfPackExprClass, Empty), Length(NumPartialArgs), Pack() {}
3706 static SizeOfPackExpr *Create(ASTContext &Context, SourceLocation OperatorLoc,
3707 NamedDecl *Pack, SourceLocation PackLoc,
3708 SourceLocation RParenLoc,
3709 Optional<unsigned> Length = None,
3710 ArrayRef<TemplateArgument> PartialArgs = None);
3711 static SizeOfPackExpr *CreateDeserialized(ASTContext &Context,
3712 unsigned NumPartialArgs);
3714 /// \brief Determine the location of the 'sizeof' keyword.
3715 SourceLocation getOperatorLoc() const { return OperatorLoc; }
3717 /// \brief Determine the location of the parameter pack.
3718 SourceLocation getPackLoc() const { return PackLoc; }
3720 /// \brief Determine the location of the right parenthesis.
3721 SourceLocation getRParenLoc() const { return RParenLoc; }
3723 /// \brief Retrieve the parameter pack.
3724 NamedDecl *getPack() const { return Pack; }
3726 /// \brief Retrieve the length of the parameter pack.
3728 /// This routine may only be invoked when the expression is not
3729 /// value-dependent.
3730 unsigned getPackLength() const {
3731 assert(!isValueDependent() &&
3732 "Cannot get the length of a value-dependent pack size expression");
3736 /// \brief Determine whether this represents a partially-substituted sizeof...
3737 /// expression, such as is produced for:
3739 /// template<typename ...Ts> using X = int[sizeof...(Ts)];
3740 /// template<typename ...Us> void f(X<Us..., 1, 2, 3, Us...>);
3741 bool isPartiallySubstituted() const {
3742 return isValueDependent() && Length;
3746 ArrayRef<TemplateArgument> getPartialArguments() const {
3747 assert(isPartiallySubstituted());
3748 const TemplateArgument *Args = getTrailingObjects<TemplateArgument>();
3749 return llvm::makeArrayRef(Args, Args + Length);
3752 SourceLocation getLocStart() const LLVM_READONLY { return OperatorLoc; }
3753 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
3755 static bool classof(const Stmt *T) {
3756 return T->getStmtClass() == SizeOfPackExprClass;
3760 child_range children() {
3761 return child_range(child_iterator(), child_iterator());
3765 /// \brief Represents a reference to a non-type template parameter
3766 /// that has been substituted with a template argument.
3767 class SubstNonTypeTemplateParmExpr : public Expr {
3768 /// \brief The replaced parameter.
3769 NonTypeTemplateParmDecl *Param;
3771 /// \brief The replacement expression.
3774 /// \brief The location of the non-type template parameter reference.
3775 SourceLocation NameLoc;
3777 friend class ASTReader;
3778 friend class ASTStmtReader;
3779 explicit SubstNonTypeTemplateParmExpr(EmptyShell Empty)
3780 : Expr(SubstNonTypeTemplateParmExprClass, Empty) { }
3783 SubstNonTypeTemplateParmExpr(QualType type,
3784 ExprValueKind valueKind,
3786 NonTypeTemplateParmDecl *param,
3788 : Expr(SubstNonTypeTemplateParmExprClass, type, valueKind, OK_Ordinary,
3789 replacement->isTypeDependent(), replacement->isValueDependent(),
3790 replacement->isInstantiationDependent(),
3791 replacement->containsUnexpandedParameterPack()),
3792 Param(param), Replacement(replacement), NameLoc(loc) {}
3794 SourceLocation getNameLoc() const { return NameLoc; }
3795 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; }
3796 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; }
3798 Expr *getReplacement() const { return cast<Expr>(Replacement); }
3800 NonTypeTemplateParmDecl *getParameter() const { return Param; }
3802 static bool classof(const Stmt *s) {
3803 return s->getStmtClass() == SubstNonTypeTemplateParmExprClass;
3807 child_range children() { return child_range(&Replacement, &Replacement+1); }
3810 /// \brief Represents a reference to a non-type template parameter pack that
3811 /// has been substituted with a non-template argument pack.
3813 /// When a pack expansion in the source code contains multiple parameter packs
3814 /// and those parameter packs correspond to different levels of template
3815 /// parameter lists, this node is used to represent a non-type template
3816 /// parameter pack from an outer level, which has already had its argument pack
3817 /// substituted but that still lives within a pack expansion that itself
3818 /// could not be instantiated. When actually performing a substitution into
3819 /// that pack expansion (e.g., when all template parameters have corresponding
3820 /// arguments), this type will be replaced with the appropriate underlying
3821 /// expression at the current pack substitution index.
3822 class SubstNonTypeTemplateParmPackExpr : public Expr {
3823 /// \brief The non-type template parameter pack itself.
3824 NonTypeTemplateParmDecl *Param;
3826 /// \brief A pointer to the set of template arguments that this
3827 /// parameter pack is instantiated with.
3828 const TemplateArgument *Arguments;
3830 /// \brief The number of template arguments in \c Arguments.
3831 unsigned NumArguments;
3833 /// \brief The location of the non-type template parameter pack reference.
3834 SourceLocation NameLoc;
3836 friend class ASTReader;
3837 friend class ASTStmtReader;
3838 explicit SubstNonTypeTemplateParmPackExpr(EmptyShell Empty)
3839 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) { }
3842 SubstNonTypeTemplateParmPackExpr(QualType T,
3843 NonTypeTemplateParmDecl *Param,
3844 SourceLocation NameLoc,
3845 const TemplateArgument &ArgPack);
3847 /// \brief Retrieve the non-type template parameter pack being substituted.
3848 NonTypeTemplateParmDecl *getParameterPack() const { return Param; }
3850 /// \brief Retrieve the location of the parameter pack name.
3851 SourceLocation getParameterPackLocation() const { return NameLoc; }
3853 /// \brief Retrieve the template argument pack containing the substituted
3854 /// template arguments.
3855 TemplateArgument getArgumentPack() const;
3857 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; }
3858 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; }
3860 static bool classof(const Stmt *T) {
3861 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass;
3865 child_range children() {
3866 return child_range(child_iterator(), child_iterator());
3870 /// \brief Represents a reference to a function parameter pack that has been
3871 /// substituted but not yet expanded.
3873 /// When a pack expansion contains multiple parameter packs at different levels,
3874 /// this node is used to represent a function parameter pack at an outer level
3875 /// which we have already substituted to refer to expanded parameters, but where
3876 /// the containing pack expansion cannot yet be expanded.
3879 /// template<typename...Ts> struct S {
3880 /// template<typename...Us> auto f(Ts ...ts) -> decltype(g(Us(ts)...));
3882 /// template struct S<int, int>;
3884 class FunctionParmPackExpr final
3886 private llvm::TrailingObjects<FunctionParmPackExpr, ParmVarDecl *> {
3887 /// \brief The function parameter pack which was referenced.
3888 ParmVarDecl *ParamPack;
3890 /// \brief The location of the function parameter pack reference.
3891 SourceLocation NameLoc;
3893 /// \brief The number of expansions of this pack.
3894 unsigned NumParameters;
3896 FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
3897 SourceLocation NameLoc, unsigned NumParams,
3898 ParmVarDecl *const *Params);
3900 friend TrailingObjects;
3901 friend class ASTReader;
3902 friend class ASTStmtReader;
3905 static FunctionParmPackExpr *Create(const ASTContext &Context, QualType T,
3906 ParmVarDecl *ParamPack,
3907 SourceLocation NameLoc,
3908 ArrayRef<ParmVarDecl *> Params);
3909 static FunctionParmPackExpr *CreateEmpty(const ASTContext &Context,
3910 unsigned NumParams);
3912 /// \brief Get the parameter pack which this expression refers to.
3913 ParmVarDecl *getParameterPack() const { return ParamPack; }
3915 /// \brief Get the location of the parameter pack.
3916 SourceLocation getParameterPackLocation() const { return NameLoc; }
3918 /// \brief Iterators over the parameters which the parameter pack expanded
3920 typedef ParmVarDecl * const *iterator;
3921 iterator begin() const { return getTrailingObjects<ParmVarDecl *>(); }
3922 iterator end() const { return begin() + NumParameters; }
3924 /// \brief Get the number of parameters in this parameter pack.
3925 unsigned getNumExpansions() const { return NumParameters; }
3927 /// \brief Get an expansion of the parameter pack by index.
3928 ParmVarDecl *getExpansion(unsigned I) const { return begin()[I]; }
3930 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; }
3931 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; }
3933 static bool classof(const Stmt *T) {
3934 return T->getStmtClass() == FunctionParmPackExprClass;
3937 child_range children() {
3938 return child_range(child_iterator(), child_iterator());
3942 /// \brief Represents a prvalue temporary that is written into memory so that
3943 /// a reference can bind to it.
3945 /// Prvalue expressions are materialized when they need to have an address
3946 /// in memory for a reference to bind to. This happens when binding a
3947 /// reference to the result of a conversion, e.g.,
3950 /// const int &r = 1.0;
3953 /// Here, 1.0 is implicitly converted to an \c int. That resulting \c int is
3954 /// then materialized via a \c MaterializeTemporaryExpr, and the reference
3955 /// binds to the temporary. \c MaterializeTemporaryExprs are always glvalues
3956 /// (either an lvalue or an xvalue, depending on the kind of reference binding
3957 /// to it), maintaining the invariant that references always bind to glvalues.
3959 /// Reference binding and copy-elision can both extend the lifetime of a
3960 /// temporary. When either happens, the expression will also track the
3961 /// declaration which is responsible for the lifetime extension.
3962 class MaterializeTemporaryExpr : public Expr {
3965 /// \brief The temporary-generating expression whose value will be
3969 /// \brief The declaration which lifetime-extended this reference, if any.
3970 /// Either a VarDecl, or (for a ctor-initializer) a FieldDecl.
3971 const ValueDecl *ExtendingDecl;
3973 unsigned ManglingNumber;
3975 llvm::PointerUnion<Stmt *, ExtraState *> State;
3977 friend class ASTStmtReader;
3978 friend class ASTStmtWriter;
3980 void initializeExtraState(const ValueDecl *ExtendedBy,
3981 unsigned ManglingNumber);
3984 MaterializeTemporaryExpr(QualType T, Expr *Temporary,
3985 bool BoundToLvalueReference)
3986 : Expr(MaterializeTemporaryExprClass, T,
3987 BoundToLvalueReference? VK_LValue : VK_XValue, OK_Ordinary,
3988 Temporary->isTypeDependent(), Temporary->isValueDependent(),
3989 Temporary->isInstantiationDependent(),
3990 Temporary->containsUnexpandedParameterPack()),
3993 MaterializeTemporaryExpr(EmptyShell Empty)
3994 : Expr(MaterializeTemporaryExprClass, Empty) { }
3996 Stmt *getTemporary() const {
3997 return State.is<Stmt *>() ? State.get<Stmt *>()
3998 : State.get<ExtraState *>()->Temporary;
4001 /// \brief Retrieve the temporary-generating subexpression whose value will
4002 /// be materialized into a glvalue.
4003 Expr *GetTemporaryExpr() const { return static_cast<Expr *>(getTemporary()); }
4005 /// \brief Retrieve the storage duration for the materialized temporary.
4006 StorageDuration getStorageDuration() const {
4007 const ValueDecl *ExtendingDecl = getExtendingDecl();
4009 return SD_FullExpression;
4010 // FIXME: This is not necessarily correct for a temporary materialized
4011 // within a default initializer.
4012 if (isa<FieldDecl>(ExtendingDecl))
4013 return SD_Automatic;
4014 return cast<VarDecl>(ExtendingDecl)->getStorageDuration();
4017 /// \brief Get the declaration which triggered the lifetime-extension of this
4018 /// temporary, if any.
4019 const ValueDecl *getExtendingDecl() const {
4020 return State.is<Stmt *>() ? nullptr
4021 : State.get<ExtraState *>()->ExtendingDecl;
4024 void setExtendingDecl(const ValueDecl *ExtendedBy, unsigned ManglingNumber);
4026 unsigned getManglingNumber() const {
4027 return State.is<Stmt *>() ? 0 : State.get<ExtraState *>()->ManglingNumber;
4030 /// \brief Determine whether this materialized temporary is bound to an
4031 /// lvalue reference; otherwise, it's bound to an rvalue reference.
4032 bool isBoundToLvalueReference() const {
4033 return getValueKind() == VK_LValue;
4036 SourceLocation getLocStart() const LLVM_READONLY {
4037 return getTemporary()->getLocStart();
4039 SourceLocation getLocEnd() const LLVM_READONLY {
4040 return getTemporary()->getLocEnd();
4043 static bool classof(const Stmt *T) {
4044 return T->getStmtClass() == MaterializeTemporaryExprClass;
4048 child_range children() {
4049 if (State.is<Stmt *>())
4050 return child_range(State.getAddrOfPtr1(), State.getAddrOfPtr1() + 1);
4052 auto ES = State.get<ExtraState *>();
4053 return child_range(&ES->Temporary, &ES->Temporary + 1);
4057 /// \brief Represents a folding of a pack over an operator.
4059 /// This expression is always dependent and represents a pack expansion of the
4064 /// ( expr op ... op expr )
4065 class CXXFoldExpr : public Expr {
4066 SourceLocation LParenLoc;
4067 SourceLocation EllipsisLoc;
4068 SourceLocation RParenLoc;
4070 BinaryOperatorKind Opcode;
4072 friend class ASTStmtReader;
4073 friend class ASTStmtWriter;
4075 CXXFoldExpr(QualType T, SourceLocation LParenLoc, Expr *LHS,
4076 BinaryOperatorKind Opcode, SourceLocation EllipsisLoc, Expr *RHS,
4077 SourceLocation RParenLoc)
4078 : Expr(CXXFoldExprClass, T, VK_RValue, OK_Ordinary,
4079 /*Dependent*/ true, true, true,
4080 /*ContainsUnexpandedParameterPack*/ false),
4081 LParenLoc(LParenLoc), EllipsisLoc(EllipsisLoc), RParenLoc(RParenLoc),
4086 CXXFoldExpr(EmptyShell Empty) : Expr(CXXFoldExprClass, Empty) {}
4088 Expr *getLHS() const { return static_cast<Expr*>(SubExprs[0]); }
4089 Expr *getRHS() const { return static_cast<Expr*>(SubExprs[1]); }
4091 /// Does this produce a right-associated sequence of operators?
4092 bool isRightFold() const {
4093 return getLHS() && getLHS()->containsUnexpandedParameterPack();
4095 /// Does this produce a left-associated sequence of operators?
4096 bool isLeftFold() const { return !isRightFold(); }
4097 /// Get the pattern, that is, the operand that contains an unexpanded pack.
4098 Expr *getPattern() const { return isLeftFold() ? getRHS() : getLHS(); }
4099 /// Get the operand that doesn't contain a pack, for a binary fold.
4100 Expr *getInit() const { return isLeftFold() ? getLHS() : getRHS(); }
4102 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
4103 BinaryOperatorKind getOperator() const { return Opcode; }
4105 SourceLocation getLocStart() const LLVM_READONLY {
4108 SourceLocation getLocEnd() const LLVM_READONLY {
4112 static bool classof(const Stmt *T) {
4113 return T->getStmtClass() == CXXFoldExprClass;
4117 child_range children() { return child_range(SubExprs, SubExprs + 2); }
4120 /// \brief Represents an expression that might suspend coroutine execution;
4121 /// either a co_await or co_yield expression.
4123 /// Evaluation of this expression first evaluates its 'ready' expression. If
4124 /// that returns 'false':
4125 /// -- execution of the coroutine is suspended
4126 /// -- the 'suspend' expression is evaluated
4127 /// -- if the 'suspend' expression returns 'false', the coroutine is
4129 /// -- otherwise, control passes back to the resumer.
4130 /// If the coroutine is not suspended, or when it is resumed, the 'resume'
4131 /// expression is evaluated, and its result is the result of the overall
4133 class CoroutineSuspendExpr : public Expr {
4134 SourceLocation KeywordLoc;
4136 enum SubExpr { Common, Ready, Suspend, Resume, Count };
4137 Stmt *SubExprs[SubExpr::Count];
4139 friend class ASTStmtReader;
4141 CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, Expr *Common,
4142 Expr *Ready, Expr *Suspend, Expr *Resume)
4143 : Expr(SC, Resume->getType(), Resume->getValueKind(),
4144 Resume->getObjectKind(), Resume->isTypeDependent(),
4145 Resume->isValueDependent(), Common->isInstantiationDependent(),
4146 Common->containsUnexpandedParameterPack()),
4147 KeywordLoc(KeywordLoc) {
4148 SubExprs[SubExpr::Common] = Common;
4149 SubExprs[SubExpr::Ready] = Ready;
4150 SubExprs[SubExpr::Suspend] = Suspend;
4151 SubExprs[SubExpr::Resume] = Resume;
4153 CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, QualType Ty,
4155 : Expr(SC, Ty, VK_RValue, OK_Ordinary, true, true, true,
4156 Common->containsUnexpandedParameterPack()),
4157 KeywordLoc(KeywordLoc) {
4158 assert(Common->isTypeDependent() && Ty->isDependentType() &&
4159 "wrong constructor for non-dependent co_await/co_yield expression");
4160 SubExprs[SubExpr::Common] = Common;
4161 SubExprs[SubExpr::Ready] = nullptr;
4162 SubExprs[SubExpr::Suspend] = nullptr;
4163 SubExprs[SubExpr::Resume] = nullptr;
4165 CoroutineSuspendExpr(StmtClass SC, EmptyShell Empty) : Expr(SC, Empty) {
4166 SubExprs[SubExpr::Common] = nullptr;
4167 SubExprs[SubExpr::Ready] = nullptr;
4168 SubExprs[SubExpr::Suspend] = nullptr;
4169 SubExprs[SubExpr::Resume] = nullptr;
4172 SourceLocation getKeywordLoc() const { return KeywordLoc; }
4173 Expr *getCommonExpr() const {
4174 return static_cast<Expr*>(SubExprs[SubExpr::Common]);
4177 Expr *getReadyExpr() const {
4178 return static_cast<Expr*>(SubExprs[SubExpr::Ready]);
4180 Expr *getSuspendExpr() const {
4181 return static_cast<Expr*>(SubExprs[SubExpr::Suspend]);
4183 Expr *getResumeExpr() const {
4184 return static_cast<Expr*>(SubExprs[SubExpr::Resume]);
4187 SourceLocation getLocStart() const LLVM_READONLY {
4190 SourceLocation getLocEnd() const LLVM_READONLY {
4191 return getCommonExpr()->getLocEnd();
4194 child_range children() {
4195 return child_range(SubExprs, SubExprs + SubExpr::Count);
4198 static bool classof(const Stmt *T) {
4199 return T->getStmtClass() == CoawaitExprClass ||
4200 T->getStmtClass() == CoyieldExprClass;
4204 /// \brief Represents a 'co_await' expression.
4205 class CoawaitExpr : public CoroutineSuspendExpr {
4206 friend class ASTStmtReader;
4208 CoawaitExpr(SourceLocation CoawaitLoc, Expr *Operand, Expr *Ready,
4209 Expr *Suspend, Expr *Resume)
4210 : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Operand, Ready,
4212 CoawaitExpr(SourceLocation CoawaitLoc, QualType Ty, Expr *Operand)
4213 : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Ty, Operand) {}
4214 CoawaitExpr(EmptyShell Empty)
4215 : CoroutineSuspendExpr(CoawaitExprClass, Empty) {}
4217 Expr *getOperand() const {
4218 // FIXME: Dig out the actual operand or store it.
4219 return getCommonExpr();
4222 static bool classof(const Stmt *T) {
4223 return T->getStmtClass() == CoawaitExprClass;
4227 /// \brief Represents a 'co_yield' expression.
4228 class CoyieldExpr : public CoroutineSuspendExpr {
4229 friend class ASTStmtReader;
4231 CoyieldExpr(SourceLocation CoyieldLoc, Expr *Operand, Expr *Ready,
4232 Expr *Suspend, Expr *Resume)
4233 : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Operand, Ready,
4235 CoyieldExpr(SourceLocation CoyieldLoc, QualType Ty, Expr *Operand)
4236 : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Ty, Operand) {}
4237 CoyieldExpr(EmptyShell Empty)
4238 : CoroutineSuspendExpr(CoyieldExprClass, Empty) {}
4240 Expr *getOperand() const {
4241 // FIXME: Dig out the actual operand or store it.
4242 return getCommonExpr();
4245 static bool classof(const Stmt *T) {
4246 return T->getStmtClass() == CoyieldExprClass;
4250 } // end namespace clang