clang 18.0.0git
ExprCXX.h
Go to the documentation of this file.
1//===- ExprCXX.h - Classes for representing expressions ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Defines the clang::Expr interface and subclasses for C++ expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_EXPRCXX_H
15#define LLVM_CLANG_AST_EXPRCXX_H
16
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclBase.h"
21#include "clang/AST/DeclCXX.h"
25#include "clang/AST/Expr.h"
28#include "clang/AST/Stmt.h"
29#include "clang/AST/StmtCXX.h"
31#include "clang/AST/Type.h"
35#include "clang/Basic/LLVM.h"
36#include "clang/Basic/Lambda.h"
42#include "llvm/ADT/ArrayRef.h"
43#include "llvm/ADT/PointerUnion.h"
44#include "llvm/ADT/StringRef.h"
45#include "llvm/ADT/iterator_range.h"
46#include "llvm/Support/Casting.h"
47#include "llvm/Support/Compiler.h"
48#include "llvm/Support/TrailingObjects.h"
49#include <cassert>
50#include <cstddef>
51#include <cstdint>
52#include <memory>
53#include <optional>
54
55namespace clang {
56
57class ASTContext;
58class DeclAccessPair;
59class IdentifierInfo;
60class LambdaCapture;
61class NonTypeTemplateParmDecl;
62class TemplateParameterList;
63
64//===--------------------------------------------------------------------===//
65// C++ Expressions.
66//===--------------------------------------------------------------------===//
67
68/// A call to an overloaded operator written using operator
69/// syntax.
70///
71/// Represents a call to an overloaded operator written using operator
72/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a
73/// normal call, this AST node provides better information about the
74/// syntactic representation of the call.
75///
76/// In a C++ template, this expression node kind will be used whenever
77/// any of the arguments are type-dependent. In this case, the
78/// function itself will be a (possibly empty) set of functions and
79/// function templates that were found by name lookup at template
80/// definition time.
81class CXXOperatorCallExpr final : public CallExpr {
82 friend class ASTStmtReader;
83 friend class ASTStmtWriter;
84
85 SourceRange Range;
86
87 // CXXOperatorCallExpr has some trailing objects belonging
88 // to CallExpr. See CallExpr for the details.
89
90 SourceRange getSourceRangeImpl() const LLVM_READONLY;
91
93 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
94 SourceLocation OperatorLoc, FPOptionsOverride FPFeatures,
96
97 CXXOperatorCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
98
99public:
100 static CXXOperatorCallExpr *
101 Create(const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn,
102 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
103 SourceLocation OperatorLoc, FPOptionsOverride FPFeatures,
105
106 static CXXOperatorCallExpr *CreateEmpty(const ASTContext &Ctx,
107 unsigned NumArgs, bool HasFPFeatures,
108 EmptyShell Empty);
109
110 /// Returns the kind of overloaded operator that this expression refers to.
112 return static_cast<OverloadedOperatorKind>(
113 CXXOperatorCallExprBits.OperatorKind);
114 }
115
117 return Opc == OO_Equal || Opc == OO_StarEqual || Opc == OO_SlashEqual ||
118 Opc == OO_PercentEqual || Opc == OO_PlusEqual ||
119 Opc == OO_MinusEqual || Opc == OO_LessLessEqual ||
120 Opc == OO_GreaterGreaterEqual || Opc == OO_AmpEqual ||
121 Opc == OO_CaretEqual || Opc == OO_PipeEqual;
122 }
123 bool isAssignmentOp() const { return isAssignmentOp(getOperator()); }
124
126 switch (Opc) {
127 case OO_EqualEqual:
128 case OO_ExclaimEqual:
129 case OO_Greater:
130 case OO_GreaterEqual:
131 case OO_Less:
132 case OO_LessEqual:
133 case OO_Spaceship:
134 return true;
135 default:
136 return false;
137 }
138 }
139 bool isComparisonOp() const { return isComparisonOp(getOperator()); }
140
141 /// Is this written as an infix binary operator?
142 bool isInfixBinaryOp() const;
143
144 /// Returns the location of the operator symbol in the expression.
145 ///
146 /// When \c getOperator()==OO_Call, this is the location of the right
147 /// parentheses; when \c getOperator()==OO_Subscript, this is the location
148 /// of the right bracket.
150
151 SourceLocation getExprLoc() const LLVM_READONLY {
153 return (Operator < OO_Plus || Operator >= OO_Arrow ||
154 Operator == OO_PlusPlus || Operator == OO_MinusMinus)
155 ? getBeginLoc()
156 : getOperatorLoc();
157 }
158
159 SourceLocation getBeginLoc() const { return Range.getBegin(); }
160 SourceLocation getEndLoc() const { return Range.getEnd(); }
161 SourceRange getSourceRange() const { return Range; }
162
163 static bool classof(const Stmt *T) {
164 return T->getStmtClass() == CXXOperatorCallExprClass;
165 }
166};
167
168/// Represents a call to a member function that
169/// may be written either with member call syntax (e.g., "obj.func()"
170/// or "objptr->func()") or with normal function-call syntax
171/// ("func()") within a member function that ends up calling a member
172/// function. The callee in either case is a MemberExpr that contains
173/// both the object argument and the member function, while the
174/// arguments are the arguments within the parentheses (not including
175/// the object argument).
176class CXXMemberCallExpr final : public CallExpr {
177 // CXXMemberCallExpr has some trailing objects belonging
178 // to CallExpr. See CallExpr for the details.
179
182 FPOptionsOverride FPOptions, unsigned MinNumArgs);
183
184 CXXMemberCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
185
186public:
187 static CXXMemberCallExpr *Create(const ASTContext &Ctx, Expr *Fn,
188 ArrayRef<Expr *> Args, QualType Ty,
190 FPOptionsOverride FPFeatures,
191 unsigned MinNumArgs = 0);
192
193 static CXXMemberCallExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs,
194 bool HasFPFeatures, EmptyShell Empty);
195
196 /// Retrieve the implicit object argument for the member call.
197 ///
198 /// For example, in "x.f(5)", this returns the sub-expression "x".
200
201 /// Retrieve the type of the object argument.
202 ///
203 /// Note that this always returns a non-pointer type.
204 QualType getObjectType() const;
205
206 /// Retrieve the declaration of the called method.
208
209 /// Retrieve the CXXRecordDecl for the underlying type of
210 /// the implicit object argument.
211 ///
212 /// Note that this is may not be the same declaration as that of the class
213 /// context of the CXXMethodDecl which this function is calling.
214 /// FIXME: Returns 0 for member pointer call exprs.
216
217 SourceLocation getExprLoc() const LLVM_READONLY {
219 if (CLoc.isValid())
220 return CLoc;
221
222 return getBeginLoc();
223 }
224
225 static bool classof(const Stmt *T) {
226 return T->getStmtClass() == CXXMemberCallExprClass;
227 }
228};
229
230/// Represents a call to a CUDA kernel function.
231class CUDAKernelCallExpr final : public CallExpr {
232 friend class ASTStmtReader;
233
234 enum { CONFIG, END_PREARG };
235
236 // CUDAKernelCallExpr has some trailing objects belonging
237 // to CallExpr. See CallExpr for the details.
238
241 FPOptionsOverride FPFeatures, unsigned MinNumArgs);
242
243 CUDAKernelCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
244
245public:
246 static CUDAKernelCallExpr *Create(const ASTContext &Ctx, Expr *Fn,
247 CallExpr *Config, ArrayRef<Expr *> Args,
248 QualType Ty, ExprValueKind VK,
250 FPOptionsOverride FPFeatures,
251 unsigned MinNumArgs = 0);
252
253 static CUDAKernelCallExpr *CreateEmpty(const ASTContext &Ctx,
254 unsigned NumArgs, bool HasFPFeatures,
255 EmptyShell Empty);
256
257 const CallExpr *getConfig() const {
258 return cast_or_null<CallExpr>(getPreArg(CONFIG));
259 }
260 CallExpr *getConfig() { return cast_or_null<CallExpr>(getPreArg(CONFIG)); }
261
262 static bool classof(const Stmt *T) {
263 return T->getStmtClass() == CUDAKernelCallExprClass;
264 }
265};
266
267/// A rewritten comparison expression that was originally written using
268/// operator syntax.
269///
270/// In C++20, the following rewrites are performed:
271/// - <tt>a == b</tt> -> <tt>b == a</tt>
272/// - <tt>a != b</tt> -> <tt>!(a == b)</tt>
273/// - <tt>a != b</tt> -> <tt>!(b == a)</tt>
274/// - For \c \@ in \c <, \c <=, \c >, \c >=, \c <=>:
275/// - <tt>a @ b</tt> -> <tt>(a <=> b) @ 0</tt>
276/// - <tt>a @ b</tt> -> <tt>0 @ (b <=> a)</tt>
277///
278/// This expression provides access to both the original syntax and the
279/// rewritten expression.
280///
281/// Note that the rewritten calls to \c ==, \c <=>, and \c \@ are typically
282/// \c CXXOperatorCallExprs, but could theoretically be \c BinaryOperators.
284 friend class ASTStmtReader;
285
286 /// The rewritten semantic form.
287 Stmt *SemanticForm;
288
289public:
290 CXXRewrittenBinaryOperator(Expr *SemanticForm, bool IsReversed)
291 : Expr(CXXRewrittenBinaryOperatorClass, SemanticForm->getType(),
292 SemanticForm->getValueKind(), SemanticForm->getObjectKind()),
293 SemanticForm(SemanticForm) {
294 CXXRewrittenBinaryOperatorBits.IsReversed = IsReversed;
296 }
298 : Expr(CXXRewrittenBinaryOperatorClass, Empty), SemanticForm() {}
299
300 /// Get an equivalent semantic form for this expression.
301 Expr *getSemanticForm() { return cast<Expr>(SemanticForm); }
302 const Expr *getSemanticForm() const { return cast<Expr>(SemanticForm); }
303
305 /// The original opcode, prior to rewriting.
307 /// The original left-hand side.
308 const Expr *LHS;
309 /// The original right-hand side.
310 const Expr *RHS;
311 /// The inner \c == or \c <=> operator expression.
313 };
314
315 /// Decompose this operator into its syntactic form.
316 DecomposedForm getDecomposedForm() const LLVM_READONLY;
317
318 /// Determine whether this expression was rewritten in reverse form.
319 bool isReversed() const { return CXXRewrittenBinaryOperatorBits.IsReversed; }
320
323 static StringRef getOpcodeStr(BinaryOperatorKind Op) {
325 }
326 StringRef getOpcodeStr() const {
328 }
329 bool isComparisonOp() const { return true; }
330 bool isAssignmentOp() const { return false; }
331
332 const Expr *getLHS() const { return getDecomposedForm().LHS; }
333 const Expr *getRHS() const { return getDecomposedForm().RHS; }
334
335 SourceLocation getOperatorLoc() const LLVM_READONLY {
337 }
338 SourceLocation getExprLoc() const LLVM_READONLY { return getOperatorLoc(); }
339
340 /// Compute the begin and end locations from the decomposed form.
341 /// The locations of the semantic form are not reliable if this is
342 /// a reversed expression.
343 //@{
344 SourceLocation getBeginLoc() const LLVM_READONLY {
346 }
347 SourceLocation getEndLoc() const LLVM_READONLY {
348 return getDecomposedForm().RHS->getEndLoc();
349 }
350 SourceRange getSourceRange() const LLVM_READONLY {
352 return SourceRange(DF.LHS->getBeginLoc(), DF.RHS->getEndLoc());
353 }
354 //@}
355
357 return child_range(&SemanticForm, &SemanticForm + 1);
358 }
359
360 static bool classof(const Stmt *T) {
361 return T->getStmtClass() == CXXRewrittenBinaryOperatorClass;
362 }
363};
364
365/// Abstract class common to all of the C++ "named"/"keyword" casts.
366///
367/// This abstract class is inherited by all of the classes
368/// representing "named" casts: CXXStaticCastExpr for \c static_cast,
369/// CXXDynamicCastExpr for \c dynamic_cast, CXXReinterpretCastExpr for
370/// reinterpret_cast, CXXConstCastExpr for \c const_cast and
371/// CXXAddrspaceCastExpr for addrspace_cast (in OpenCL).
373private:
374 // the location of the casting op
375 SourceLocation Loc;
376
377 // the location of the right parenthesis
378 SourceLocation RParenLoc;
379
380 // range for '<' '>'
381 SourceRange AngleBrackets;
382
383protected:
384 friend class ASTStmtReader;
385
387 Expr *op, unsigned PathSize, bool HasFPFeatures,
388 TypeSourceInfo *writtenTy, SourceLocation l,
389 SourceLocation RParenLoc, SourceRange AngleBrackets)
390 : ExplicitCastExpr(SC, ty, VK, kind, op, PathSize, HasFPFeatures,
391 writtenTy),
392 Loc(l), RParenLoc(RParenLoc), AngleBrackets(AngleBrackets) {}
393
394 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize,
395 bool HasFPFeatures)
396 : ExplicitCastExpr(SC, Shell, PathSize, HasFPFeatures) {}
397
398public:
399 const char *getCastName() const;
400
401 /// Retrieve the location of the cast operator keyword, e.g.,
402 /// \c static_cast.
403 SourceLocation getOperatorLoc() const { return Loc; }
404
405 /// Retrieve the location of the closing parenthesis.
406 SourceLocation getRParenLoc() const { return RParenLoc; }
407
408 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
409 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
410 SourceRange getAngleBrackets() const LLVM_READONLY { return AngleBrackets; }
411
412 static bool classof(const Stmt *T) {
413 switch (T->getStmtClass()) {
414 case CXXStaticCastExprClass:
415 case CXXDynamicCastExprClass:
416 case CXXReinterpretCastExprClass:
417 case CXXConstCastExprClass:
418 case CXXAddrspaceCastExprClass:
419 return true;
420 default:
421 return false;
422 }
423 }
424};
425
426/// A C++ \c static_cast expression (C++ [expr.static.cast]).
427///
428/// This expression node represents a C++ static cast, e.g.,
429/// \c static_cast<int>(1.0).
431 : public CXXNamedCastExpr,
432 private llvm::TrailingObjects<CXXStaticCastExpr, CXXBaseSpecifier *,
433 FPOptionsOverride> {
435 unsigned pathSize, TypeSourceInfo *writtenTy,
437 SourceLocation RParenLoc, SourceRange AngleBrackets)
438 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize,
439 FPO.requiresTrailingStorage(), writtenTy, l, RParenLoc,
440 AngleBrackets) {
442 *getTrailingFPFeatures() = FPO;
443 }
444
445 explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize,
446 bool HasFPFeatures)
447 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize,
448 HasFPFeatures) {}
449
450 unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const {
451 return path_size();
452 }
453
454public:
455 friend class CastExpr;
457
458 static CXXStaticCastExpr *
459 Create(const ASTContext &Context, QualType T, ExprValueKind VK, CastKind K,
460 Expr *Op, const CXXCastPath *Path, TypeSourceInfo *Written,
462 SourceRange AngleBrackets);
463 static CXXStaticCastExpr *CreateEmpty(const ASTContext &Context,
464 unsigned PathSize, bool hasFPFeatures);
465
466 static bool classof(const Stmt *T) {
467 return T->getStmtClass() == CXXStaticCastExprClass;
468 }
469};
470
471/// A C++ @c dynamic_cast expression (C++ [expr.dynamic.cast]).
472///
473/// This expression node represents a dynamic cast, e.g.,
474/// \c dynamic_cast<Derived*>(BasePtr). Such a cast may perform a run-time
475/// check to determine how to perform the type conversion.
477 : public CXXNamedCastExpr,
478 private llvm::TrailingObjects<CXXDynamicCastExpr, CXXBaseSpecifier *> {
480 unsigned pathSize, TypeSourceInfo *writtenTy,
481 SourceLocation l, SourceLocation RParenLoc,
482 SourceRange AngleBrackets)
483 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, VK, kind, op, pathSize,
484 /*HasFPFeatures*/ false, writtenTy, l, RParenLoc,
485 AngleBrackets) {}
486
487 explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize)
488 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize,
489 /*HasFPFeatures*/ false) {}
490
491public:
492 friend class CastExpr;
494
495 static CXXDynamicCastExpr *Create(const ASTContext &Context, QualType T,
496 ExprValueKind VK, CastKind Kind, Expr *Op,
497 const CXXCastPath *Path,
498 TypeSourceInfo *Written, SourceLocation L,
499 SourceLocation RParenLoc,
500 SourceRange AngleBrackets);
501
502 static CXXDynamicCastExpr *CreateEmpty(const ASTContext &Context,
503 unsigned pathSize);
504
505 bool isAlwaysNull() const;
506
507 static bool classof(const Stmt *T) {
508 return T->getStmtClass() == CXXDynamicCastExprClass;
509 }
510};
511
512/// A C++ @c reinterpret_cast expression (C++ [expr.reinterpret.cast]).
513///
514/// This expression node represents a reinterpret cast, e.g.,
515/// @c reinterpret_cast<int>(VoidPtr).
516///
517/// A reinterpret_cast provides a differently-typed view of a value but
518/// (in Clang, as in most C++ implementations) performs no actual work at
519/// run time.
521 : public CXXNamedCastExpr,
522 private llvm::TrailingObjects<CXXReinterpretCastExpr,
523 CXXBaseSpecifier *> {
525 unsigned pathSize, TypeSourceInfo *writtenTy,
526 SourceLocation l, SourceLocation RParenLoc,
527 SourceRange AngleBrackets)
528 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, vk, kind, op,
529 pathSize, /*HasFPFeatures*/ false, writtenTy, l,
530 RParenLoc, AngleBrackets) {}
531
532 CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize)
533 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize,
534 /*HasFPFeatures*/ false) {}
535
536public:
537 friend class CastExpr;
539
540 static CXXReinterpretCastExpr *Create(const ASTContext &Context, QualType T,
541 ExprValueKind VK, CastKind Kind,
542 Expr *Op, const CXXCastPath *Path,
543 TypeSourceInfo *WrittenTy, SourceLocation L,
544 SourceLocation RParenLoc,
545 SourceRange AngleBrackets);
546 static CXXReinterpretCastExpr *CreateEmpty(const ASTContext &Context,
547 unsigned pathSize);
548
549 static bool classof(const Stmt *T) {
550 return T->getStmtClass() == CXXReinterpretCastExprClass;
551 }
552};
553
554/// A C++ \c const_cast expression (C++ [expr.const.cast]).
555///
556/// This expression node represents a const cast, e.g.,
557/// \c const_cast<char*>(PtrToConstChar).
558///
559/// A const_cast can remove type qualifiers but does not change the underlying
560/// value.
562 : public CXXNamedCastExpr,
563 private llvm::TrailingObjects<CXXConstCastExpr, CXXBaseSpecifier *> {
565 TypeSourceInfo *writtenTy, SourceLocation l,
566 SourceLocation RParenLoc, SourceRange AngleBrackets)
567 : CXXNamedCastExpr(CXXConstCastExprClass, ty, VK, CK_NoOp, op, 0,
568 /*HasFPFeatures*/ false, writtenTy, l, RParenLoc,
569 AngleBrackets) {}
570
571 explicit CXXConstCastExpr(EmptyShell Empty)
572 : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0,
573 /*HasFPFeatures*/ false) {}
574
575public:
576 friend class CastExpr;
578
579 static CXXConstCastExpr *Create(const ASTContext &Context, QualType T,
580 ExprValueKind VK, Expr *Op,
581 TypeSourceInfo *WrittenTy, SourceLocation L,
582 SourceLocation RParenLoc,
583 SourceRange AngleBrackets);
584 static CXXConstCastExpr *CreateEmpty(const ASTContext &Context);
585
586 static bool classof(const Stmt *T) {
587 return T->getStmtClass() == CXXConstCastExprClass;
588 }
589};
590
591/// A C++ addrspace_cast expression (currently only enabled for OpenCL).
592///
593/// This expression node represents a cast between pointers to objects in
594/// different address spaces e.g.,
595/// \c addrspace_cast<global int*>(PtrToGenericInt).
596///
597/// A addrspace_cast can cast address space type qualifiers but does not change
598/// the underlying value.
600 : public CXXNamedCastExpr,
601 private llvm::TrailingObjects<CXXAddrspaceCastExpr, CXXBaseSpecifier *> {
603 TypeSourceInfo *writtenTy, SourceLocation l,
604 SourceLocation RParenLoc, SourceRange AngleBrackets)
605 : CXXNamedCastExpr(CXXAddrspaceCastExprClass, ty, VK, Kind, op, 0,
606 /*HasFPFeatures*/ false, writtenTy, l, RParenLoc,
607 AngleBrackets) {}
608
609 explicit CXXAddrspaceCastExpr(EmptyShell Empty)
610 : CXXNamedCastExpr(CXXAddrspaceCastExprClass, Empty, 0,
611 /*HasFPFeatures*/ false) {}
612
613public:
614 friend class CastExpr;
616
617 static CXXAddrspaceCastExpr *
618 Create(const ASTContext &Context, QualType T, ExprValueKind VK, CastKind Kind,
619 Expr *Op, TypeSourceInfo *WrittenTy, SourceLocation L,
620 SourceLocation RParenLoc, SourceRange AngleBrackets);
621 static CXXAddrspaceCastExpr *CreateEmpty(const ASTContext &Context);
622
623 static bool classof(const Stmt *T) {
624 return T->getStmtClass() == CXXAddrspaceCastExprClass;
625 }
626};
627
628/// A call to a literal operator (C++11 [over.literal])
629/// written as a user-defined literal (C++11 [lit.ext]).
630///
631/// Represents a user-defined literal, e.g. "foo"_bar or 1.23_xyz. While this
632/// is semantically equivalent to a normal call, this AST node provides better
633/// information about the syntactic representation of the literal.
634///
635/// Since literal operators are never found by ADL and can only be declared at
636/// namespace scope, a user-defined literal is never dependent.
637class UserDefinedLiteral final : public CallExpr {
638 friend class ASTStmtReader;
639 friend class ASTStmtWriter;
640
641 /// The location of a ud-suffix within the literal.
642 SourceLocation UDSuffixLoc;
643
644 // UserDefinedLiteral has some trailing objects belonging
645 // to CallExpr. See CallExpr for the details.
646
648 ExprValueKind VK, SourceLocation LitEndLoc,
649 SourceLocation SuffixLoc, FPOptionsOverride FPFeatures);
650
651 UserDefinedLiteral(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
652
653public:
654 static UserDefinedLiteral *Create(const ASTContext &Ctx, Expr *Fn,
655 ArrayRef<Expr *> Args, QualType Ty,
656 ExprValueKind VK, SourceLocation LitEndLoc,
657 SourceLocation SuffixLoc,
658 FPOptionsOverride FPFeatures);
659
660 static UserDefinedLiteral *CreateEmpty(const ASTContext &Ctx,
661 unsigned NumArgs, bool HasFPOptions,
662 EmptyShell Empty);
663
664 /// The kind of literal operator which is invoked.
666 /// Raw form: operator "" X (const char *)
668
669 /// Raw form: operator "" X<cs...> ()
671
672 /// operator "" X (unsigned long long)
674
675 /// operator "" X (long double)
677
678 /// operator "" X (const CharT *, size_t)
680
681 /// operator "" X (CharT)
683 };
684
685 /// Returns the kind of literal operator invocation
686 /// which this expression represents.
688
689 /// If this is not a raw user-defined literal, get the
690 /// underlying cooked literal (representing the literal with the suffix
691 /// removed).
693 const Expr *getCookedLiteral() const {
694 return const_cast<UserDefinedLiteral*>(this)->getCookedLiteral();
695 }
696
699 return getRParenLoc();
700 return getArg(0)->getBeginLoc();
701 }
702
704
705 /// Returns the location of a ud-suffix in the expression.
706 ///
707 /// For a string literal, there may be multiple identical suffixes. This
708 /// returns the first.
709 SourceLocation getUDSuffixLoc() const { return UDSuffixLoc; }
710
711 /// Returns the ud-suffix specified for this literal.
712 const IdentifierInfo *getUDSuffix() const;
713
714 static bool classof(const Stmt *S) {
715 return S->getStmtClass() == UserDefinedLiteralClass;
716 }
717};
718
719/// A boolean literal, per ([C++ lex.bool] Boolean literals).
720class CXXBoolLiteralExpr : public Expr {
721public:
723 : Expr(CXXBoolLiteralExprClass, Ty, VK_PRValue, OK_Ordinary) {
724 CXXBoolLiteralExprBits.Value = Val;
725 CXXBoolLiteralExprBits.Loc = Loc;
726 setDependence(ExprDependence::None);
727 }
728
730 : Expr(CXXBoolLiteralExprClass, Empty) {}
731
732 static CXXBoolLiteralExpr *Create(const ASTContext &C, bool Val, QualType Ty,
733 SourceLocation Loc) {
734 return new (C) CXXBoolLiteralExpr(Val, Ty, Loc);
735 }
736
737 bool getValue() const { return CXXBoolLiteralExprBits.Value; }
738 void setValue(bool V) { CXXBoolLiteralExprBits.Value = V; }
739
742
745
746 static bool classof(const Stmt *T) {
747 return T->getStmtClass() == CXXBoolLiteralExprClass;
748 }
749
750 // Iterators
753 }
754
757 }
758};
759
760/// The null pointer literal (C++11 [lex.nullptr])
761///
762/// Introduced in C++11, the only literal of type \c nullptr_t is \c nullptr.
763/// This also implements the null pointer literal in C23 (C23 6.4.1) which is
764/// intended to have the same semantics as the feature in C++.
766public:
768 : Expr(CXXNullPtrLiteralExprClass, Ty, VK_PRValue, OK_Ordinary) {
770 setDependence(ExprDependence::None);
771 }
772
774 : Expr(CXXNullPtrLiteralExprClass, Empty) {}
775
778
781
782 static bool classof(const Stmt *T) {
783 return T->getStmtClass() == CXXNullPtrLiteralExprClass;
784 }
785
788 }
789
792 }
793};
794
795/// Implicit construction of a std::initializer_list<T> object from an
796/// array temporary within list-initialization (C++11 [dcl.init.list]p5).
798 Stmt *SubExpr = nullptr;
799
801 : Expr(CXXStdInitializerListExprClass, Empty) {}
802
803public:
804 friend class ASTReader;
805 friend class ASTStmtReader;
806
808 : Expr(CXXStdInitializerListExprClass, Ty, VK_PRValue, OK_Ordinary),
809 SubExpr(SubExpr) {
811 }
812
813 Expr *getSubExpr() { return static_cast<Expr*>(SubExpr); }
814 const Expr *getSubExpr() const { return static_cast<const Expr*>(SubExpr); }
815
816 SourceLocation getBeginLoc() const LLVM_READONLY {
817 return SubExpr->getBeginLoc();
818 }
819
820 SourceLocation getEndLoc() const LLVM_READONLY {
821 return SubExpr->getEndLoc();
822 }
823
824 /// Retrieve the source range of the expression.
825 SourceRange getSourceRange() const LLVM_READONLY {
826 return SubExpr->getSourceRange();
827 }
828
829 static bool classof(const Stmt *S) {
830 return S->getStmtClass() == CXXStdInitializerListExprClass;
831 }
832
833 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
834
836 return const_child_range(&SubExpr, &SubExpr + 1);
837 }
838};
839
840/// A C++ \c typeid expression (C++ [expr.typeid]), which gets
841/// the \c type_info that corresponds to the supplied type, or the (possibly
842/// dynamic) type of the supplied expression.
843///
844/// This represents code like \c typeid(int) or \c typeid(*objPtr)
845class CXXTypeidExpr : public Expr {
846 friend class ASTStmtReader;
847
848private:
849 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
850 SourceRange Range;
851
852public:
854 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
855 Range(R) {
857 }
858
860 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
861 Range(R) {
863 }
864
865 CXXTypeidExpr(EmptyShell Empty, bool isExpr)
866 : Expr(CXXTypeidExprClass, Empty) {
867 if (isExpr)
868 Operand = (Expr*)nullptr;
869 else
870 Operand = (TypeSourceInfo*)nullptr;
871 }
872
873 /// Determine whether this typeid has a type operand which is potentially
874 /// evaluated, per C++11 [expr.typeid]p3.
875 bool isPotentiallyEvaluated() const;
876
877 /// Best-effort check if the expression operand refers to a most derived
878 /// object. This is not a strong guarantee.
879 bool isMostDerived(ASTContext &Context) const;
880
881 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
882
883 /// Retrieves the type operand of this typeid() expression after
884 /// various required adjustments (removing reference types, cv-qualifiers).
885 QualType getTypeOperand(ASTContext &Context) const;
886
887 /// Retrieve source information for the type operand.
889 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
890 return Operand.get<TypeSourceInfo *>();
891 }
893 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
894 return static_cast<Expr*>(Operand.get<Stmt *>());
895 }
896
897 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
898 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
899 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
900 void setSourceRange(SourceRange R) { Range = R; }
901
902 static bool classof(const Stmt *T) {
903 return T->getStmtClass() == CXXTypeidExprClass;
904 }
905
906 // Iterators
908 if (isTypeOperand())
910 auto **begin = reinterpret_cast<Stmt **>(&Operand);
911 return child_range(begin, begin + 1);
912 }
913
915 if (isTypeOperand())
917
918 auto **begin =
919 reinterpret_cast<Stmt **>(&const_cast<CXXTypeidExpr *>(this)->Operand);
920 return const_child_range(begin, begin + 1);
921 }
922};
923
924/// A member reference to an MSPropertyDecl.
925///
926/// This expression always has pseudo-object type, and therefore it is
927/// typically not encountered in a fully-typechecked expression except
928/// within the syntactic form of a PseudoObjectExpr.
929class MSPropertyRefExpr : public Expr {
930 Expr *BaseExpr;
931 MSPropertyDecl *TheDecl;
932 SourceLocation MemberLoc;
933 bool IsArrow;
934 NestedNameSpecifierLoc QualifierLoc;
935
936public:
937 friend class ASTStmtReader;
938
940 QualType ty, ExprValueKind VK,
941 NestedNameSpecifierLoc qualifierLoc, SourceLocation nameLoc)
942 : Expr(MSPropertyRefExprClass, ty, VK, OK_Ordinary), BaseExpr(baseExpr),
943 TheDecl(decl), MemberLoc(nameLoc), IsArrow(isArrow),
944 QualifierLoc(qualifierLoc) {
946 }
947
948 MSPropertyRefExpr(EmptyShell Empty) : Expr(MSPropertyRefExprClass, Empty) {}
949
950 SourceRange getSourceRange() const LLVM_READONLY {
951 return SourceRange(getBeginLoc(), getEndLoc());
952 }
953
954 bool isImplicitAccess() const {
956 }
957
959 if (!isImplicitAccess())
960 return BaseExpr->getBeginLoc();
961 else if (QualifierLoc)
962 return QualifierLoc.getBeginLoc();
963 else
964 return MemberLoc;
965 }
966
968
970 return child_range((Stmt**)&BaseExpr, (Stmt**)&BaseExpr + 1);
971 }
972
974 auto Children = const_cast<MSPropertyRefExpr *>(this)->children();
975 return const_child_range(Children.begin(), Children.end());
976 }
977
978 static bool classof(const Stmt *T) {
979 return T->getStmtClass() == MSPropertyRefExprClass;
980 }
981
982 Expr *getBaseExpr() const { return BaseExpr; }
983 MSPropertyDecl *getPropertyDecl() const { return TheDecl; }
984 bool isArrow() const { return IsArrow; }
985 SourceLocation getMemberLoc() const { return MemberLoc; }
986 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
987};
988
989/// MS property subscript expression.
990/// MSVC supports 'property' attribute and allows to apply it to the
991/// declaration of an empty array in a class or structure definition.
992/// For example:
993/// \code
994/// __declspec(property(get=GetX, put=PutX)) int x[];
995/// \endcode
996/// The above statement indicates that x[] can be used with one or more array
997/// indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), and
998/// p->x[a][b] = i will be turned into p->PutX(a, b, i).
999/// This is a syntactic pseudo-object expression.
1001 friend class ASTStmtReader;
1002
1003 enum { BASE_EXPR, IDX_EXPR, NUM_SUBEXPRS = 2 };
1004
1005 Stmt *SubExprs[NUM_SUBEXPRS];
1006 SourceLocation RBracketLoc;
1007
1008 void setBase(Expr *Base) { SubExprs[BASE_EXPR] = Base; }
1009 void setIdx(Expr *Idx) { SubExprs[IDX_EXPR] = Idx; }
1010
1011public:
1013 ExprObjectKind OK, SourceLocation RBracketLoc)
1014 : Expr(MSPropertySubscriptExprClass, Ty, VK, OK),
1015 RBracketLoc(RBracketLoc) {
1016 SubExprs[BASE_EXPR] = Base;
1017 SubExprs[IDX_EXPR] = Idx;
1019 }
1020
1021 /// Create an empty array subscript expression.
1023 : Expr(MSPropertySubscriptExprClass, Shell) {}
1024
1025 Expr *getBase() { return cast<Expr>(SubExprs[BASE_EXPR]); }
1026 const Expr *getBase() const { return cast<Expr>(SubExprs[BASE_EXPR]); }
1027
1028 Expr *getIdx() { return cast<Expr>(SubExprs[IDX_EXPR]); }
1029 const Expr *getIdx() const { return cast<Expr>(SubExprs[IDX_EXPR]); }
1030
1031 SourceLocation getBeginLoc() const LLVM_READONLY {
1032 return getBase()->getBeginLoc();
1033 }
1034
1035 SourceLocation getEndLoc() const LLVM_READONLY { return RBracketLoc; }
1036
1037 SourceLocation getRBracketLoc() const { return RBracketLoc; }
1038 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
1039
1040 SourceLocation getExprLoc() const LLVM_READONLY {
1041 return getBase()->getExprLoc();
1042 }
1043
1044 static bool classof(const Stmt *T) {
1045 return T->getStmtClass() == MSPropertySubscriptExprClass;
1046 }
1047
1048 // Iterators
1050 return child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
1051 }
1052
1054 return const_child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
1055 }
1056};
1057
1058/// A Microsoft C++ @c __uuidof expression, which gets
1059/// the _GUID that corresponds to the supplied type or expression.
1060///
1061/// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr)
1062class CXXUuidofExpr : public Expr {
1063 friend class ASTStmtReader;
1064
1065private:
1066 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
1067 MSGuidDecl *Guid;
1068 SourceRange Range;
1069
1070public:
1072 SourceRange R)
1073 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
1074 Guid(Guid), Range(R) {
1076 }
1077
1079 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
1080 Guid(Guid), Range(R) {
1082 }
1083
1084 CXXUuidofExpr(EmptyShell Empty, bool isExpr)
1085 : Expr(CXXUuidofExprClass, Empty) {
1086 if (isExpr)
1087 Operand = (Expr*)nullptr;
1088 else
1089 Operand = (TypeSourceInfo*)nullptr;
1090 }
1091
1092 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
1093
1094 /// Retrieves the type operand of this __uuidof() expression after
1095 /// various required adjustments (removing reference types, cv-qualifiers).
1096 QualType getTypeOperand(ASTContext &Context) const;
1097
1098 /// Retrieve source information for the type operand.
1100 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
1101 return Operand.get<TypeSourceInfo *>();
1102 }
1104 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
1105 return static_cast<Expr*>(Operand.get<Stmt *>());
1106 }
1107
1108 MSGuidDecl *getGuidDecl() const { return Guid; }
1109
1110 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
1111 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
1112 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
1113 void setSourceRange(SourceRange R) { Range = R; }
1114
1115 static bool classof(const Stmt *T) {
1116 return T->getStmtClass() == CXXUuidofExprClass;
1117 }
1118
1119 // Iterators
1121 if (isTypeOperand())
1123 auto **begin = reinterpret_cast<Stmt **>(&Operand);
1124 return child_range(begin, begin + 1);
1125 }
1126
1128 if (isTypeOperand())
1130 auto **begin =
1131 reinterpret_cast<Stmt **>(&const_cast<CXXUuidofExpr *>(this)->Operand);
1132 return const_child_range(begin, begin + 1);
1133 }
1134};
1135
1136/// Represents the \c this expression in C++.
1137///
1138/// This is a pointer to the object on which the current member function is
1139/// executing (C++ [expr.prim]p3). Example:
1140///
1141/// \code
1142/// class Foo {
1143/// public:
1144/// void bar();
1145/// void test() { this->bar(); }
1146/// };
1147/// \endcode
1148class CXXThisExpr : public Expr {
1149 CXXThisExpr(SourceLocation L, QualType Ty, bool IsImplicit, ExprValueKind VK)
1150 : Expr(CXXThisExprClass, Ty, VK, OK_Ordinary) {
1151 CXXThisExprBits.IsImplicit = IsImplicit;
1152 CXXThisExprBits.Loc = L;
1154 }
1155
1156 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {}
1157
1158public:
1159 static CXXThisExpr *Create(const ASTContext &Ctx, SourceLocation L,
1160 QualType Ty, bool IsImplicit);
1161
1162 static CXXThisExpr *CreateEmpty(const ASTContext &Ctx);
1163
1166
1169
1170 bool isImplicit() const { return CXXThisExprBits.IsImplicit; }
1171 void setImplicit(bool I) { CXXThisExprBits.IsImplicit = I; }
1172
1173 static bool classof(const Stmt *T) {
1174 return T->getStmtClass() == CXXThisExprClass;
1175 }
1176
1177 // Iterators
1180 }
1181
1184 }
1185};
1186
1187/// A C++ throw-expression (C++ [except.throw]).
1188///
1189/// This handles 'throw' (for re-throwing the current exception) and
1190/// 'throw' assignment-expression. When assignment-expression isn't
1191/// present, Op will be null.
1192class CXXThrowExpr : public Expr {
1193 friend class ASTStmtReader;
1194
1195 /// The optional expression in the throw statement.
1196 Stmt *Operand;
1197
1198public:
1199 // \p Ty is the void type which is used as the result type of the
1200 // expression. The \p Loc is the location of the throw keyword.
1201 // \p Operand is the expression in the throw statement, and can be
1202 // null if not present.
1204 bool IsThrownVariableInScope)
1205 : Expr(CXXThrowExprClass, Ty, VK_PRValue, OK_Ordinary), Operand(Operand) {
1206 CXXThrowExprBits.ThrowLoc = Loc;
1207 CXXThrowExprBits.IsThrownVariableInScope = IsThrownVariableInScope;
1209 }
1210 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {}
1211
1212 const Expr *getSubExpr() const { return cast_or_null<Expr>(Operand); }
1213 Expr *getSubExpr() { return cast_or_null<Expr>(Operand); }
1214
1215 SourceLocation getThrowLoc() const { return CXXThrowExprBits.ThrowLoc; }
1216
1217 /// Determines whether the variable thrown by this expression (if any!)
1218 /// is within the innermost try block.
1219 ///
1220 /// This information is required to determine whether the NRVO can apply to
1221 /// this variable.
1223 return CXXThrowExprBits.IsThrownVariableInScope;
1224 }
1225
1227 SourceLocation getEndLoc() const LLVM_READONLY {
1228 if (!getSubExpr())
1229 return getThrowLoc();
1230 return getSubExpr()->getEndLoc();
1231 }
1232
1233 static bool classof(const Stmt *T) {
1234 return T->getStmtClass() == CXXThrowExprClass;
1235 }
1236
1237 // Iterators
1239 return child_range(&Operand, Operand ? &Operand + 1 : &Operand);
1240 }
1241
1243 return const_child_range(&Operand, Operand ? &Operand + 1 : &Operand);
1244 }
1245};
1246
1247/// A default argument (C++ [dcl.fct.default]).
1248///
1249/// This wraps up a function call argument that was created from the
1250/// corresponding parameter's default argument, when the call did not
1251/// explicitly supply arguments for all of the parameters.
1253 : public Expr,
1254 private llvm::TrailingObjects<CXXDefaultArgExpr, Expr *> {
1255 friend class ASTStmtReader;
1256 friend class ASTReader;
1257 friend TrailingObjects;
1258
1259 /// The parameter whose default is being used.
1260 ParmVarDecl *Param;
1261
1262 /// The context where the default argument expression was used.
1263 DeclContext *UsedContext;
1264
1266 Expr *RewrittenExpr, DeclContext *UsedContext)
1267 : Expr(SC,
1268 Param->hasUnparsedDefaultArg()
1269 ? Param->getType().getNonReferenceType()
1270 : Param->getDefaultArg()->getType(),
1271 Param->getDefaultArg()->getValueKind(),
1272 Param->getDefaultArg()->getObjectKind()),
1273 Param(Param), UsedContext(UsedContext) {
1274 CXXDefaultArgExprBits.Loc = Loc;
1275 CXXDefaultArgExprBits.HasRewrittenInit = RewrittenExpr != nullptr;
1276 if (RewrittenExpr)
1277 *getTrailingObjects<Expr *>() = RewrittenExpr;
1279 }
1280
1281 CXXDefaultArgExpr(EmptyShell Empty, bool HasRewrittenInit)
1282 : Expr(CXXDefaultArgExprClass, Empty) {
1283 CXXDefaultArgExprBits.HasRewrittenInit = HasRewrittenInit;
1284 }
1285
1286public:
1287 static CXXDefaultArgExpr *CreateEmpty(const ASTContext &C,
1288 bool HasRewrittenInit);
1289
1290 // \p Param is the parameter whose default argument is used by this
1291 // expression.
1292 static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc,
1293 ParmVarDecl *Param, Expr *RewrittenExpr,
1294 DeclContext *UsedContext);
1295 // Retrieve the parameter that the argument was created from.
1296 const ParmVarDecl *getParam() const { return Param; }
1297 ParmVarDecl *getParam() { return Param; }
1298
1299 bool hasRewrittenInit() const {
1300 return CXXDefaultArgExprBits.HasRewrittenInit;
1301 }
1302
1303 // Retrieve the argument to the function call.
1304 Expr *getExpr();
1305 const Expr *getExpr() const {
1306 return const_cast<CXXDefaultArgExpr *>(this)->getExpr();
1307 }
1308
1310 return hasRewrittenInit() ? *getTrailingObjects<Expr *>() : nullptr;
1311 }
1312
1313 const Expr *getRewrittenExpr() const {
1314 return const_cast<CXXDefaultArgExpr *>(this)->getRewrittenExpr();
1315 }
1316
1317 // Retrieve the rewritten init expression (for an init expression containing
1318 // immediate calls) with the top level FullExpr and ConstantExpr stripped off.
1321 return const_cast<CXXDefaultArgExpr *>(this)->getAdjustedRewrittenExpr();
1322 }
1323
1324 const DeclContext *getUsedContext() const { return UsedContext; }
1325 DeclContext *getUsedContext() { return UsedContext; }
1326
1327 /// Retrieve the location where this default argument was actually used.
1329
1330 /// Default argument expressions have no representation in the
1331 /// source, so they have an empty source range.
1334
1336
1337 static bool classof(const Stmt *T) {
1338 return T->getStmtClass() == CXXDefaultArgExprClass;
1339 }
1340
1341 // Iterators
1344 }
1345
1348 }
1349};
1350
1351/// A use of a default initializer in a constructor or in aggregate
1352/// initialization.
1353///
1354/// This wraps a use of a C++ default initializer (technically,
1355/// a brace-or-equal-initializer for a non-static data member) when it
1356/// is implicitly used in a mem-initializer-list in a constructor
1357/// (C++11 [class.base.init]p8) or in aggregate initialization
1358/// (C++1y [dcl.init.aggr]p7).
1360 : public Expr,
1361 private llvm::TrailingObjects<CXXDefaultInitExpr, Expr *> {
1362
1363 friend class ASTStmtReader;
1364 friend class ASTReader;
1365 friend TrailingObjects;
1366 /// The field whose default is being used.
1367 FieldDecl *Field;
1368
1369 /// The context where the default initializer expression was used.
1370 DeclContext *UsedContext;
1371
1373 FieldDecl *Field, QualType Ty, DeclContext *UsedContext,
1374 Expr *RewrittenInitExpr);
1375
1376 CXXDefaultInitExpr(EmptyShell Empty, bool HasRewrittenInit)
1377 : Expr(CXXDefaultInitExprClass, Empty) {
1378 CXXDefaultInitExprBits.HasRewrittenInit = HasRewrittenInit;
1379 }
1380
1381public:
1383 bool HasRewrittenInit);
1384 /// \p Field is the non-static data member whose default initializer is used
1385 /// by this expression.
1386 static CXXDefaultInitExpr *Create(const ASTContext &Ctx, SourceLocation Loc,
1387 FieldDecl *Field, DeclContext *UsedContext,
1388 Expr *RewrittenInitExpr);
1389
1390 bool hasRewrittenInit() const {
1391 return CXXDefaultInitExprBits.HasRewrittenInit;
1392 }
1393
1394 /// Get the field whose initializer will be used.
1395 FieldDecl *getField() { return Field; }
1396 const FieldDecl *getField() const { return Field; }
1397
1398 /// Get the initialization expression that will be used.
1399 Expr *getExpr();
1400 const Expr *getExpr() const {
1401 return const_cast<CXXDefaultInitExpr *>(this)->getExpr();
1402 }
1403
1404 /// Retrieve the initializing expression with evaluated immediate calls, if
1405 /// any.
1406 const Expr *getRewrittenExpr() const {
1407 assert(hasRewrittenInit() && "expected a rewritten init expression");
1408 return *getTrailingObjects<Expr *>();
1409 }
1410
1411 /// Retrieve the initializing expression with evaluated immediate calls, if
1412 /// any.
1414 assert(hasRewrittenInit() && "expected a rewritten init expression");
1415 return *getTrailingObjects<Expr *>();
1416 }
1417
1418 const DeclContext *getUsedContext() const { return UsedContext; }
1419 DeclContext *getUsedContext() { return UsedContext; }
1420
1421 /// Retrieve the location where this default initializer expression was
1422 /// actually used.
1424
1427
1428 static bool classof(const Stmt *T) {
1429 return T->getStmtClass() == CXXDefaultInitExprClass;
1430 }
1431
1432 // Iterators
1435 }
1436
1439 }
1440};
1441
1442/// Represents a C++ temporary.
1444 /// The destructor that needs to be called.
1445 const CXXDestructorDecl *Destructor;
1446
1447 explicit CXXTemporary(const CXXDestructorDecl *destructor)
1448 : Destructor(destructor) {}
1449
1450public:
1451 static CXXTemporary *Create(const ASTContext &C,
1452 const CXXDestructorDecl *Destructor);
1453
1454 const CXXDestructorDecl *getDestructor() const { return Destructor; }
1455
1457 Destructor = Dtor;
1458 }
1459};
1460
1461/// Represents binding an expression to a temporary.
1462///
1463/// This ensures the destructor is called for the temporary. It should only be
1464/// needed for non-POD, non-trivially destructable class types. For example:
1465///
1466/// \code
1467/// struct S {
1468/// S() { } // User defined constructor makes S non-POD.
1469/// ~S() { } // User defined destructor makes it non-trivial.
1470/// };
1471/// void test() {
1472/// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr.
1473/// }
1474/// \endcode
1476 CXXTemporary *Temp = nullptr;
1477 Stmt *SubExpr = nullptr;
1478
1479 CXXBindTemporaryExpr(CXXTemporary *temp, Expr *SubExpr)
1480 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(), VK_PRValue,
1481 OK_Ordinary),
1482 Temp(temp), SubExpr(SubExpr) {
1484 }
1485
1486public:
1488 : Expr(CXXBindTemporaryExprClass, Empty) {}
1489
1490 static CXXBindTemporaryExpr *Create(const ASTContext &C, CXXTemporary *Temp,
1491 Expr* SubExpr);
1492
1493 CXXTemporary *getTemporary() { return Temp; }
1494 const CXXTemporary *getTemporary() const { return Temp; }
1495 void setTemporary(CXXTemporary *T) { Temp = T; }
1496
1497 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
1498 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
1499 void setSubExpr(Expr *E) { SubExpr = E; }
1500
1501 SourceLocation getBeginLoc() const LLVM_READONLY {
1502 return SubExpr->getBeginLoc();
1503 }
1504
1505 SourceLocation getEndLoc() const LLVM_READONLY {
1506 return SubExpr->getEndLoc();
1507 }
1508
1509 // Implement isa/cast/dyncast/etc.
1510 static bool classof(const Stmt *T) {
1511 return T->getStmtClass() == CXXBindTemporaryExprClass;
1512 }
1513
1514 // Iterators
1515 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
1516
1518 return const_child_range(&SubExpr, &SubExpr + 1);
1519 }
1520};
1521
1522/// Represents a call to a C++ constructor.
1523class CXXConstructExpr : public Expr {
1524 friend class ASTStmtReader;
1525
1526public:
1533
1534private:
1535 /// A pointer to the constructor which will be ultimately called.
1536 CXXConstructorDecl *Constructor;
1537
1538 SourceRange ParenOrBraceRange;
1539
1540 /// The number of arguments.
1541 unsigned NumArgs;
1542
1543 // We would like to stash the arguments of the constructor call after
1544 // CXXConstructExpr. However CXXConstructExpr is used as a base class of
1545 // CXXTemporaryObjectExpr which makes the use of llvm::TrailingObjects
1546 // impossible.
1547 //
1548 // Instead we manually stash the trailing object after the full object
1549 // containing CXXConstructExpr (that is either CXXConstructExpr or
1550 // CXXTemporaryObjectExpr).
1551 //
1552 // The trailing objects are:
1553 //
1554 // * An array of getNumArgs() "Stmt *" for the arguments of the
1555 // constructor call.
1556
1557 /// Return a pointer to the start of the trailing arguments.
1558 /// Defined just after CXXTemporaryObjectExpr.
1559 inline Stmt **getTrailingArgs();
1560 const Stmt *const *getTrailingArgs() const {
1561 return const_cast<CXXConstructExpr *>(this)->getTrailingArgs();
1562 }
1563
1564protected:
1565 /// Build a C++ construction expression.
1566 CXXConstructExpr(StmtClass SC, QualType Ty, SourceLocation Loc,
1567 CXXConstructorDecl *Ctor, bool Elidable,
1568 ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1569 bool ListInitialization, bool StdInitListInitialization,
1570 bool ZeroInitialization, ConstructionKind ConstructKind,
1571 SourceRange ParenOrBraceRange);
1572
1573 /// Build an empty C++ construction expression.
1574 CXXConstructExpr(StmtClass SC, EmptyShell Empty, unsigned NumArgs);
1575
1576 /// Return the size in bytes of the trailing objects. Used by
1577 /// CXXTemporaryObjectExpr to allocate the right amount of storage.
1578 static unsigned sizeOfTrailingObjects(unsigned NumArgs) {
1579 return NumArgs * sizeof(Stmt *);
1580 }
1581
1582public:
1583 /// Create a C++ construction expression.
1584 static CXXConstructExpr *
1585 Create(const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
1586 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
1587 bool HadMultipleCandidates, bool ListInitialization,
1588 bool StdInitListInitialization, bool ZeroInitialization,
1589 ConstructionKind ConstructKind, SourceRange ParenOrBraceRange);
1590
1591 /// Create an empty C++ construction expression.
1592 static CXXConstructExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs);
1593
1594 /// Get the constructor that this expression will (ultimately) call.
1595 CXXConstructorDecl *getConstructor() const { return Constructor; }
1596
1599
1600 /// Whether this construction is elidable.
1601 bool isElidable() const { return CXXConstructExprBits.Elidable; }
1602 void setElidable(bool E) { CXXConstructExprBits.Elidable = E; }
1603
1604 /// Whether the referred constructor was resolved from
1605 /// an overloaded set having size greater than 1.
1607 return CXXConstructExprBits.HadMultipleCandidates;
1608 }
1610 CXXConstructExprBits.HadMultipleCandidates = V;
1611 }
1612
1613 /// Whether this constructor call was written as list-initialization.
1615 return CXXConstructExprBits.ListInitialization;
1616 }
1618 CXXConstructExprBits.ListInitialization = V;
1619 }
1620
1621 /// Whether this constructor call was written as list-initialization,
1622 /// but was interpreted as forming a std::initializer_list<T> from the list
1623 /// and passing that as a single constructor argument.
1624 /// See C++11 [over.match.list]p1 bullet 1.
1626 return CXXConstructExprBits.StdInitListInitialization;
1627 }
1629 CXXConstructExprBits.StdInitListInitialization = V;
1630 }
1631
1632 /// Whether this construction first requires
1633 /// zero-initialization before the initializer is called.
1635 return CXXConstructExprBits.ZeroInitialization;
1636 }
1637 void setRequiresZeroInitialization(bool ZeroInit) {
1638 CXXConstructExprBits.ZeroInitialization = ZeroInit;
1639 }
1640
1641 /// Determine whether this constructor is actually constructing
1642 /// a base class (rather than a complete object).
1644 return static_cast<ConstructionKind>(CXXConstructExprBits.ConstructionKind);
1645 }
1647 CXXConstructExprBits.ConstructionKind = CK;
1648 }
1649
1652 using arg_range = llvm::iterator_range<arg_iterator>;
1653 using const_arg_range = llvm::iterator_range<const_arg_iterator>;
1654
1657 return const_arg_range(arg_begin(), arg_end());
1658 }
1659
1660 arg_iterator arg_begin() { return getTrailingArgs(); }
1662 const_arg_iterator arg_begin() const { return getTrailingArgs(); }
1664
1665 Expr **getArgs() { return reinterpret_cast<Expr **>(getTrailingArgs()); }
1666 const Expr *const *getArgs() const {
1667 return reinterpret_cast<const Expr *const *>(getTrailingArgs());
1668 }
1669
1670 /// Return the number of arguments to the constructor call.
1671 unsigned getNumArgs() const { return NumArgs; }
1672
1673 /// Return the specified argument.
1674 Expr *getArg(unsigned Arg) {
1675 assert(Arg < getNumArgs() && "Arg access out of range!");
1676 return getArgs()[Arg];
1677 }
1678 const Expr *getArg(unsigned Arg) const {
1679 assert(Arg < getNumArgs() && "Arg access out of range!");
1680 return getArgs()[Arg];
1681 }
1682
1683 /// Set the specified argument.
1684 void setArg(unsigned Arg, Expr *ArgExpr) {
1685 assert(Arg < getNumArgs() && "Arg access out of range!");
1686 getArgs()[Arg] = ArgExpr;
1687 }
1688
1690 return CXXConstructExprBits.IsImmediateEscalating;
1691 }
1692
1694 CXXConstructExprBits.IsImmediateEscalating = Set;
1695 }
1696
1697 SourceLocation getBeginLoc() const LLVM_READONLY;
1698 SourceLocation getEndLoc() const LLVM_READONLY;
1699 SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
1700 void setParenOrBraceRange(SourceRange Range) { ParenOrBraceRange = Range; }
1701
1702 static bool classof(const Stmt *T) {
1703 return T->getStmtClass() == CXXConstructExprClass ||
1704 T->getStmtClass() == CXXTemporaryObjectExprClass;
1705 }
1706
1707 // Iterators
1709 return child_range(getTrailingArgs(), getTrailingArgs() + getNumArgs());
1710 }
1711
1713 auto Children = const_cast<CXXConstructExpr *>(this)->children();
1714 return const_child_range(Children.begin(), Children.end());
1715 }
1716};
1717
1718/// Represents a call to an inherited base class constructor from an
1719/// inheriting constructor. This call implicitly forwards the arguments from
1720/// the enclosing context (an inheriting constructor) to the specified inherited
1721/// base class constructor.
1723private:
1724 CXXConstructorDecl *Constructor = nullptr;
1725
1726 /// The location of the using declaration.
1727 SourceLocation Loc;
1728
1729 /// Whether this is the construction of a virtual base.
1730 unsigned ConstructsVirtualBase : 1;
1731
1732 /// Whether the constructor is inherited from a virtual base class of the
1733 /// class that we construct.
1734 unsigned InheritedFromVirtualBase : 1;
1735
1736public:
1737 friend class ASTStmtReader;
1738
1739 /// Construct a C++ inheriting construction expression.
1741 CXXConstructorDecl *Ctor, bool ConstructsVirtualBase,
1742 bool InheritedFromVirtualBase)
1743 : Expr(CXXInheritedCtorInitExprClass, T, VK_PRValue, OK_Ordinary),
1744 Constructor(Ctor), Loc(Loc),
1745 ConstructsVirtualBase(ConstructsVirtualBase),
1746 InheritedFromVirtualBase(InheritedFromVirtualBase) {
1747 assert(!T->isDependentType());
1748 setDependence(ExprDependence::None);
1749 }
1750
1751 /// Construct an empty C++ inheriting construction expression.
1753 : Expr(CXXInheritedCtorInitExprClass, Empty),
1754 ConstructsVirtualBase(false), InheritedFromVirtualBase(false) {}
1755
1756 /// Get the constructor that this expression will call.
1757 CXXConstructorDecl *getConstructor() const { return Constructor; }
1758
1759 /// Determine whether this constructor is actually constructing
1760 /// a base class (rather than a complete object).
1761 bool constructsVBase() const { return ConstructsVirtualBase; }
1763 return ConstructsVirtualBase ? CXXConstructExpr::CK_VirtualBase
1765 }
1766
1767 /// Determine whether the inherited constructor is inherited from a
1768 /// virtual base of the object we construct. If so, we are not responsible
1769 /// for calling the inherited constructor (the complete object constructor
1770 /// does that), and so we don't need to pass any arguments.
1771 bool inheritedFromVBase() const { return InheritedFromVirtualBase; }
1772
1773 SourceLocation getLocation() const LLVM_READONLY { return Loc; }
1774 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
1775 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
1776
1777 static bool classof(const Stmt *T) {
1778 return T->getStmtClass() == CXXInheritedCtorInitExprClass;
1779 }
1780
1783 }
1784
1787 }
1788};
1789
1790/// Represents an explicit C++ type conversion that uses "functional"
1791/// notation (C++ [expr.type.conv]).
1792///
1793/// Example:
1794/// \code
1795/// x = int(0.5);
1796/// \endcode
1798 : public ExplicitCastExpr,
1799 private llvm::TrailingObjects<CXXFunctionalCastExpr, CXXBaseSpecifier *,
1800 FPOptionsOverride> {
1801 SourceLocation LParenLoc;
1802 SourceLocation RParenLoc;
1803
1805 TypeSourceInfo *writtenTy, CastKind kind,
1806 Expr *castExpr, unsigned pathSize,
1807 FPOptionsOverride FPO, SourceLocation lParenLoc,
1808 SourceLocation rParenLoc)
1809 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, castExpr,
1810 pathSize, FPO.requiresTrailingStorage(), writtenTy),
1811 LParenLoc(lParenLoc), RParenLoc(rParenLoc) {
1812 if (hasStoredFPFeatures())
1813 *getTrailingFPFeatures() = FPO;
1814 }
1815
1816 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize,
1817 bool HasFPFeatures)
1818 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize,
1819 HasFPFeatures) {}
1820
1821 unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const {
1822 return path_size();
1823 }
1824
1825public:
1826 friend class CastExpr;
1828
1829 static CXXFunctionalCastExpr *
1830 Create(const ASTContext &Context, QualType T, ExprValueKind VK,
1831 TypeSourceInfo *Written, CastKind Kind, Expr *Op,
1832 const CXXCastPath *Path, FPOptionsOverride FPO, SourceLocation LPLoc,
1833 SourceLocation RPLoc);
1834 static CXXFunctionalCastExpr *
1835 CreateEmpty(const ASTContext &Context, unsigned PathSize, bool HasFPFeatures);
1836
1837 SourceLocation getLParenLoc() const { return LParenLoc; }
1838 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1839 SourceLocation getRParenLoc() const { return RParenLoc; }
1840 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1841
1842 /// Determine whether this expression models list-initialization.
1843 bool isListInitialization() const { return LParenLoc.isInvalid(); }
1844
1845 SourceLocation getBeginLoc() const LLVM_READONLY;
1846 SourceLocation getEndLoc() const LLVM_READONLY;
1847
1848 static bool classof(const Stmt *T) {
1849 return T->getStmtClass() == CXXFunctionalCastExprClass;
1850 }
1851};
1852
1853/// Represents a C++ functional cast expression that builds a
1854/// temporary object.
1855///
1856/// This expression type represents a C++ "functional" cast
1857/// (C++[expr.type.conv]) with N != 1 arguments that invokes a
1858/// constructor to build a temporary object. With N == 1 arguments the
1859/// functional cast expression will be represented by CXXFunctionalCastExpr.
1860/// Example:
1861/// \code
1862/// struct X { X(int, float); }
1863///
1864/// X create_X() {
1865/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
1866/// };
1867/// \endcode
1869 friend class ASTStmtReader;
1870
1871 // CXXTemporaryObjectExpr has some trailing objects belonging
1872 // to CXXConstructExpr. See the comment inside CXXConstructExpr
1873 // for more details.
1874
1875 TypeSourceInfo *TSI;
1876
1879 SourceRange ParenOrBraceRange,
1880 bool HadMultipleCandidates, bool ListInitialization,
1881 bool StdInitListInitialization,
1882 bool ZeroInitialization);
1883
1884 CXXTemporaryObjectExpr(EmptyShell Empty, unsigned NumArgs);
1885
1886public:
1887 static CXXTemporaryObjectExpr *
1888 Create(const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
1890 SourceRange ParenOrBraceRange, bool HadMultipleCandidates,
1891 bool ListInitialization, bool StdInitListInitialization,
1892 bool ZeroInitialization);
1893
1895 unsigned NumArgs);
1896
1897 TypeSourceInfo *getTypeSourceInfo() const { return TSI; }
1898
1899 SourceLocation getBeginLoc() const LLVM_READONLY;
1900 SourceLocation getEndLoc() const LLVM_READONLY;
1901
1902 static bool classof(const Stmt *T) {
1903 return T->getStmtClass() == CXXTemporaryObjectExprClass;
1904 }
1905};
1906
1907Stmt **CXXConstructExpr::getTrailingArgs() {
1908 if (auto *E = dyn_cast<CXXTemporaryObjectExpr>(this))
1909 return reinterpret_cast<Stmt **>(E + 1);
1910 assert((getStmtClass() == CXXConstructExprClass) &&
1911 "Unexpected class deriving from CXXConstructExpr!");
1912 return reinterpret_cast<Stmt **>(this + 1);
1913}
1914
1915/// A C++ lambda expression, which produces a function object
1916/// (of unspecified type) that can be invoked later.
1917///
1918/// Example:
1919/// \code
1920/// void low_pass_filter(std::vector<double> &values, double cutoff) {
1921/// values.erase(std::remove_if(values.begin(), values.end(),
1922/// [=](double value) { return value > cutoff; });
1923/// }
1924/// \endcode
1925///
1926/// C++11 lambda expressions can capture local variables, either by copying
1927/// the values of those local variables at the time the function
1928/// object is constructed (not when it is called!) or by holding a
1929/// reference to the local variable. These captures can occur either
1930/// implicitly or can be written explicitly between the square
1931/// brackets ([...]) that start the lambda expression.
1932///
1933/// C++1y introduces a new form of "capture" called an init-capture that
1934/// includes an initializing expression (rather than capturing a variable),
1935/// and which can never occur implicitly.
1936class LambdaExpr final : public Expr,
1937 private llvm::TrailingObjects<LambdaExpr, Stmt *> {
1938 // LambdaExpr has some data stored in LambdaExprBits.
1939
1940 /// The source range that covers the lambda introducer ([...]).
1941 SourceRange IntroducerRange;
1942
1943 /// The source location of this lambda's capture-default ('=' or '&').
1944 SourceLocation CaptureDefaultLoc;
1945
1946 /// The location of the closing brace ('}') that completes
1947 /// the lambda.
1948 ///
1949 /// The location of the brace is also available by looking up the
1950 /// function call operator in the lambda class. However, it is
1951 /// stored here to improve the performance of getSourceRange(), and
1952 /// to avoid having to deserialize the function call operator from a
1953 /// module file just to determine the source range.
1954 SourceLocation ClosingBrace;
1955
1956 /// Construct a lambda expression.
1957 LambdaExpr(QualType T, SourceRange IntroducerRange,
1958 LambdaCaptureDefault CaptureDefault,
1959 SourceLocation CaptureDefaultLoc, bool ExplicitParams,
1960 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1961 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack);
1962
1963 /// Construct an empty lambda expression.
1964 LambdaExpr(EmptyShell Empty, unsigned NumCaptures);
1965
1966 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
1967 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
1968
1969 void initBodyIfNeeded() const;
1970
1971public:
1972 friend class ASTStmtReader;
1973 friend class ASTStmtWriter;
1975
1976 /// Construct a new lambda expression.
1977 static LambdaExpr *
1978 Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange,
1979 LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc,
1980 bool ExplicitParams, bool ExplicitResultType,
1981 ArrayRef<Expr *> CaptureInits, SourceLocation ClosingBrace,
1982 bool ContainsUnexpandedParameterPack);
1983
1984 /// Construct a new lambda expression that will be deserialized from
1985 /// an external source.
1987 unsigned NumCaptures);
1988
1989 /// Determine the default capture kind for this lambda.
1991 return static_cast<LambdaCaptureDefault>(LambdaExprBits.CaptureDefault);
1992 }
1993
1994 /// Retrieve the location of this lambda's capture-default, if any.
1995 SourceLocation getCaptureDefaultLoc() const { return CaptureDefaultLoc; }
1996
1997 /// Determine whether one of this lambda's captures is an init-capture.
1998 bool isInitCapture(const LambdaCapture *Capture) const;
1999
2000 /// An iterator that walks over the captures of the lambda,
2001 /// both implicit and explicit.
2003
2004 /// An iterator over a range of lambda captures.
2005 using capture_range = llvm::iterator_range<capture_iterator>;
2006
2007 /// Retrieve this lambda's captures.
2008 capture_range captures() const;
2009
2010 /// Retrieve an iterator pointing to the first lambda capture.
2012
2013 /// Retrieve an iterator pointing past the end of the
2014 /// sequence of lambda captures.
2016
2017 /// Determine the number of captures in this lambda.
2018 unsigned capture_size() const { return LambdaExprBits.NumCaptures; }
2019
2020 /// Retrieve this lambda's explicit captures.
2022
2023 /// Retrieve an iterator pointing to the first explicit
2024 /// lambda capture.
2026
2027 /// Retrieve an iterator pointing past the end of the sequence of
2028 /// explicit lambda captures.
2030
2031 /// Retrieve this lambda's implicit captures.
2033
2034 /// Retrieve an iterator pointing to the first implicit
2035 /// lambda capture.
2037
2038 /// Retrieve an iterator pointing past the end of the sequence of
2039 /// implicit lambda captures.
2041
2042 /// Iterator that walks over the capture initialization
2043 /// arguments.
2045
2046 /// Const iterator that walks over the capture initialization
2047 /// arguments.
2048 /// FIXME: This interface is prone to being used incorrectly.
2050
2051 /// Retrieve the initialization expressions for this lambda's captures.
2052 llvm::iterator_range<capture_init_iterator> capture_inits() {
2053 return llvm::make_range(capture_init_begin(), capture_init_end());
2054 }
2055
2056 /// Retrieve the initialization expressions for this lambda's captures.
2057 llvm::iterator_range<const_capture_init_iterator> capture_inits() const {
2058 return llvm::make_range(capture_init_begin(), capture_init_end());
2059 }
2060
2061 /// Retrieve the first initialization argument for this
2062 /// lambda expression (which initializes the first capture field).
2064 return reinterpret_cast<Expr **>(getStoredStmts());
2065 }
2066
2067 /// Retrieve the first initialization argument for this
2068 /// lambda expression (which initializes the first capture field).
2070 return reinterpret_cast<Expr *const *>(getStoredStmts());
2071 }
2072
2073 /// Retrieve the iterator pointing one past the last
2074 /// initialization argument for this lambda expression.
2076 return capture_init_begin() + capture_size();
2077 }
2078
2079 /// Retrieve the iterator pointing one past the last
2080 /// initialization argument for this lambda expression.
2082 return capture_init_begin() + capture_size();
2083 }
2084
2085 /// Retrieve the source range covering the lambda introducer,
2086 /// which contains the explicit capture list surrounded by square
2087 /// brackets ([...]).
2088 SourceRange getIntroducerRange() const { return IntroducerRange; }
2089
2090 /// Retrieve the class that corresponds to the lambda.
2091 ///
2092 /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the
2093 /// captures in its fields and provides the various operations permitted
2094 /// on a lambda (copying, calling).
2096
2097 /// Retrieve the function call operator associated with this
2098 /// lambda expression.
2100
2101 /// Retrieve the function template call operator associated with this
2102 /// lambda expression.
2104
2105 /// If this is a generic lambda expression, retrieve the template
2106 /// parameter list associated with it, or else return null.
2108
2109 /// Get the template parameters were explicitly specified (as opposed to being
2110 /// invented by use of an auto parameter).
2112
2113 /// Get the trailing requires clause, if any.
2115
2116 /// Whether this is a generic lambda.
2118
2119 /// Retrieve the body of the lambda. This will be most of the time
2120 /// a \p CompoundStmt, but can also be \p CoroutineBodyStmt wrapping
2121 /// a \p CompoundStmt. Note that unlike functions, lambda-expressions
2122 /// cannot have a function-try-block.
2123 Stmt *getBody() const;
2124
2125 /// Retrieve the \p CompoundStmt representing the body of the lambda.
2126 /// This is a convenience function for callers who do not need
2127 /// to handle node(s) which may wrap a \p CompoundStmt.
2128 const CompoundStmt *getCompoundStmtBody() const;
2130 const auto *ConstThis = this;
2131 return const_cast<CompoundStmt *>(ConstThis->getCompoundStmtBody());
2132 }
2133
2134 /// Determine whether the lambda is mutable, meaning that any
2135 /// captures values can be modified.
2136 bool isMutable() const;
2137
2138 /// Determine whether this lambda has an explicit parameter
2139 /// list vs. an implicit (empty) parameter list.
2140 bool hasExplicitParameters() const { return LambdaExprBits.ExplicitParams; }
2141
2142 /// Whether this lambda had its result type explicitly specified.
2144 return LambdaExprBits.ExplicitResultType;
2145 }
2146
2147 static bool classof(const Stmt *T) {
2148 return T->getStmtClass() == LambdaExprClass;
2149 }
2150
2151 SourceLocation getBeginLoc() const LLVM_READONLY {
2152 return IntroducerRange.getBegin();
2153 }
2154
2155 SourceLocation getEndLoc() const LLVM_READONLY { return ClosingBrace; }
2156
2157 /// Includes the captures and the body of the lambda.
2160};
2161
2162/// An expression "T()" which creates a value-initialized rvalue of type
2163/// T, which is a non-class type. See (C++98 [5.2.3p2]).
2165 friend class ASTStmtReader;
2166
2168
2169public:
2170 /// Create an explicitly-written scalar-value initialization
2171 /// expression.
2173 SourceLocation RParenLoc)
2174 : Expr(CXXScalarValueInitExprClass, Type, VK_PRValue, OK_Ordinary),
2176 CXXScalarValueInitExprBits.RParenLoc = RParenLoc;
2178 }
2179
2181 : Expr(CXXScalarValueInitExprClass, Shell) {}
2182
2184 return TypeInfo;
2185 }
2186
2188 return CXXScalarValueInitExprBits.RParenLoc;
2189 }
2190
2191 SourceLocation getBeginLoc() const LLVM_READONLY;
2193
2194 static bool classof(const Stmt *T) {
2195 return T->getStmtClass() == CXXScalarValueInitExprClass;
2196 }
2197
2198 // Iterators
2201 }
2202
2205 }
2206};
2207
2208/// Represents a new-expression for memory allocation and constructor
2209/// calls, e.g: "new CXXNewExpr(foo)".
2210class CXXNewExpr final
2211 : public Expr,
2212 private llvm::TrailingObjects<CXXNewExpr, Stmt *, SourceRange> {
2213 friend class ASTStmtReader;
2214 friend class ASTStmtWriter;
2215 friend TrailingObjects;
2216
2217 /// Points to the allocation function used.
2218 FunctionDecl *OperatorNew;
2219
2220 /// Points to the deallocation function used in case of error. May be null.
2221 FunctionDecl *OperatorDelete;
2222
2223 /// The allocated type-source information, as written in the source.
2224 TypeSourceInfo *AllocatedTypeInfo;
2225
2226 /// Range of the entire new expression.
2227 SourceRange Range;
2228
2229 /// Source-range of a paren-delimited initializer.
2230 SourceRange DirectInitRange;
2231
2232 // CXXNewExpr is followed by several optional trailing objects.
2233 // They are in order:
2234 //
2235 // * An optional "Stmt *" for the array size expression.
2236 // Present if and ony if isArray().
2237 //
2238 // * An optional "Stmt *" for the init expression.
2239 // Present if and only if hasInitializer().
2240 //
2241 // * An array of getNumPlacementArgs() "Stmt *" for the placement new
2242 // arguments, if any.
2243 //
2244 // * An optional SourceRange for the range covering the parenthesized type-id
2245 // if the allocated type was expressed as a parenthesized type-id.
2246 // Present if and only if isParenTypeId().
2247 unsigned arraySizeOffset() const { return 0; }
2248 unsigned initExprOffset() const { return arraySizeOffset() + isArray(); }
2249 unsigned placementNewArgsOffset() const {
2250 return initExprOffset() + hasInitializer();
2251 }
2252
2253 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2255 }
2256
2257 unsigned numTrailingObjects(OverloadToken<SourceRange>) const {
2258 return isParenTypeId();
2259 }
2260
2261public:
2263 /// New-expression has no initializer as written.
2265
2266 /// New-expression has a C++98 paren-delimited initializer.
2268
2269 /// New-expression has a C++11 list-initializer.
2270 ListInit
2272
2273private:
2274 /// Build a c++ new expression.
2275 CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
2276 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
2277 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
2278 SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
2280 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
2281 SourceRange DirectInitRange);
2282
2283 /// Build an empty c++ new expression.
2284 CXXNewExpr(EmptyShell Empty, bool IsArray, unsigned NumPlacementArgs,
2285 bool IsParenTypeId);
2286
2287public:
2288 /// Create a c++ new expression.
2289 static CXXNewExpr *
2290 Create(const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew,
2291 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
2292 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
2293 SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
2295 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
2296 SourceRange DirectInitRange);
2297
2298 /// Create an empty c++ new expression.
2299 static CXXNewExpr *CreateEmpty(const ASTContext &Ctx, bool IsArray,
2300 bool HasInit, unsigned NumPlacementArgs,
2301 bool IsParenTypeId);
2302
2304 return getType()->castAs<PointerType>()->getPointeeType();
2305 }
2306
2308 return AllocatedTypeInfo;
2309 }
2310
2311 /// True if the allocation result needs to be null-checked.
2312 ///
2313 /// C++11 [expr.new]p13:
2314 /// If the allocation function returns null, initialization shall
2315 /// not be done, the deallocation function shall not be called,
2316 /// and the value of the new-expression shall be null.
2317 ///
2318 /// C++ DR1748:
2319 /// If the allocation function is a reserved placement allocation
2320 /// function that returns null, the behavior is undefined.
2321 ///
2322 /// An allocation function is not allowed to return null unless it
2323 /// has a non-throwing exception-specification. The '03 rule is
2324 /// identical except that the definition of a non-throwing
2325 /// exception specification is just "is it throw()?".
2326 bool shouldNullCheckAllocation() const;
2327
2328 FunctionDecl *getOperatorNew() const { return OperatorNew; }
2329 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; }
2330 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2331 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; }
2332
2333 bool isArray() const { return CXXNewExprBits.IsArray; }
2334
2335 /// This might return std::nullopt even if isArray() returns true,
2336 /// since there might not be an array size expression.
2337 /// If the result is not std::nullopt, it will never wrap a nullptr.
2338 std::optional<Expr *> getArraySize() {
2339 if (!isArray())
2340 return std::nullopt;
2341
2342 if (auto *Result =
2343 cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
2344 return Result;
2345
2346 return std::nullopt;
2347 }
2348
2349 /// This might return std::nullopt even if isArray() returns true,
2350 /// since there might not be an array size expression.
2351 /// If the result is not std::nullopt, it will never wrap a nullptr.
2352 std::optional<const Expr *> getArraySize() const {
2353 if (!isArray())
2354 return std::nullopt;
2355
2356 if (auto *Result =
2357 cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
2358 return Result;
2359
2360 return std::nullopt;
2361 }
2362
2363 unsigned getNumPlacementArgs() const {
2364 return CXXNewExprBits.NumPlacementArgs;
2365 }
2366
2368 return reinterpret_cast<Expr **>(getTrailingObjects<Stmt *>() +
2369 placementNewArgsOffset());
2370 }
2371
2372 Expr *getPlacementArg(unsigned I) {
2373 assert((I < getNumPlacementArgs()) && "Index out of range!");
2374 return getPlacementArgs()[I];
2375 }
2376 const Expr *getPlacementArg(unsigned I) const {
2377 return const_cast<CXXNewExpr *>(this)->getPlacementArg(I);
2378 }
2379
2380 bool isParenTypeId() const { return CXXNewExprBits.IsParenTypeId; }
2382 return isParenTypeId() ? getTrailingObjects<SourceRange>()[0]
2383 : SourceRange();
2384 }
2385
2386 bool isGlobalNew() const { return CXXNewExprBits.IsGlobalNew; }
2387
2388 /// Whether this new-expression has any initializer at all.
2389 bool hasInitializer() const {
2390 return CXXNewExprBits.StoredInitializationStyle > 0;
2391 }
2392
2393 /// The kind of initializer this new-expression has.
2395 if (CXXNewExprBits.StoredInitializationStyle == 0)
2396 return NoInit;
2397 return static_cast<InitializationStyle>(
2398 CXXNewExprBits.StoredInitializationStyle - 1);
2399 }
2400
2401 /// The initializer of this new-expression.
2403 return hasInitializer()
2404 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
2405 : nullptr;
2406 }
2407 const Expr *getInitializer() const {
2408 return hasInitializer()
2409 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
2410 : nullptr;
2411 }
2412
2413 /// Returns the CXXConstructExpr from this new-expression, or null.
2415 return dyn_cast_or_null<CXXConstructExpr>(getInitializer());
2416 }
2417
2418 /// Indicates whether the required alignment should be implicitly passed to
2419 /// the allocation function.
2420 bool passAlignment() const { return CXXNewExprBits.ShouldPassAlignment; }
2421
2422 /// Answers whether the usual array deallocation function for the
2423 /// allocated type expects the size of the allocation as a
2424 /// parameter.
2426 return CXXNewExprBits.UsualArrayDeleteWantsSize;
2427 }
2428
2431
2432 llvm::iterator_range<arg_iterator> placement_arguments() {
2433 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2434 }
2435
2436 llvm::iterator_range<const_arg_iterator> placement_arguments() const {
2437 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2438 }
2439
2441 return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
2442 }
2445 }
2447 return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
2448 }
2451 }
2452
2454
2455 raw_arg_iterator raw_arg_begin() { return getTrailingObjects<Stmt *>(); }
2457 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
2458 }
2460 return getTrailingObjects<Stmt *>();
2461 }
2463 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
2464 }
2465
2466 SourceLocation getBeginLoc() const { return Range.getBegin(); }
2467 SourceLocation getEndLoc() const { return Range.getEnd(); }
2468
2469 SourceRange getDirectInitRange() const { return DirectInitRange; }
2470 SourceRange getSourceRange() const { return Range; }
2471
2472 static bool classof(const Stmt *T) {
2473 return T->getStmtClass() == CXXNewExprClass;
2474 }
2475
2476 // Iterators
2478
2480 return const_child_range(const_cast<CXXNewExpr *>(this)->children());
2481 }
2482};
2483
2484/// Represents a \c delete expression for memory deallocation and
2485/// destructor calls, e.g. "delete[] pArray".
2486class CXXDeleteExpr : public Expr {
2487 friend class ASTStmtReader;
2488
2489 /// Points to the operator delete overload that is used. Could be a member.
2490 FunctionDecl *OperatorDelete = nullptr;
2491
2492 /// The pointer expression to be deleted.
2493 Stmt *Argument = nullptr;
2494
2495public:
2496 CXXDeleteExpr(QualType Ty, bool GlobalDelete, bool ArrayForm,
2497 bool ArrayFormAsWritten, bool UsualArrayDeleteWantsSize,
2498 FunctionDecl *OperatorDelete, Expr *Arg, SourceLocation Loc)
2499 : Expr(CXXDeleteExprClass, Ty, VK_PRValue, OK_Ordinary),
2500 OperatorDelete(OperatorDelete), Argument(Arg) {
2501 CXXDeleteExprBits.GlobalDelete = GlobalDelete;
2502 CXXDeleteExprBits.ArrayForm = ArrayForm;
2503 CXXDeleteExprBits.ArrayFormAsWritten = ArrayFormAsWritten;
2504 CXXDeleteExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
2505 CXXDeleteExprBits.Loc = Loc;
2507 }
2508
2509 explicit CXXDeleteExpr(EmptyShell Shell) : Expr(CXXDeleteExprClass, Shell) {}
2510
2511 bool isGlobalDelete() const { return CXXDeleteExprBits.GlobalDelete; }
2512 bool isArrayForm() const { return CXXDeleteExprBits.ArrayForm; }
2514 return CXXDeleteExprBits.ArrayFormAsWritten;
2515 }
2516
2517 /// Answers whether the usual array deallocation function for the
2518 /// allocated type expects the size of the allocation as a
2519 /// parameter. This can be true even if the actual deallocation
2520 /// function that we're using doesn't want a size.
2522 return CXXDeleteExprBits.UsualArrayDeleteWantsSize;
2523 }
2524
2525 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2526
2527 Expr *getArgument() { return cast<Expr>(Argument); }
2528 const Expr *getArgument() const { return cast<Expr>(Argument); }
2529
2530 /// Retrieve the type being destroyed.
2531 ///
2532 /// If the type being destroyed is a dependent type which may or may not
2533 /// be a pointer, return an invalid type.
2534 QualType getDestroyedType() const;
2535
2537 SourceLocation getEndLoc() const LLVM_READONLY {
2538 return Argument->getEndLoc();
2539 }
2540
2541 static bool classof(const Stmt *T) {
2542 return T->getStmtClass() == CXXDeleteExprClass;
2543 }
2544
2545 // Iterators
2546 child_range children() { return child_range(&Argument, &Argument + 1); }
2547
2549 return const_child_range(&Argument, &Argument + 1);
2550 }
2551};
2552
2553/// Stores the type being destroyed by a pseudo-destructor expression.
2555 /// Either the type source information or the name of the type, if
2556 /// it couldn't be resolved due to type-dependence.
2557 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type;
2558
2559 /// The starting source location of the pseudo-destructor type.
2560 SourceLocation Location;
2561
2562public:
2564
2566 : Type(II), Location(Loc) {}
2567
2569
2571 return Type.dyn_cast<TypeSourceInfo *>();
2572 }
2573
2575 return Type.dyn_cast<IdentifierInfo *>();
2576 }
2577
2578 SourceLocation getLocation() const { return Location; }
2579};
2580
2581/// Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
2582///
2583/// A pseudo-destructor is an expression that looks like a member access to a
2584/// destructor of a scalar type, except that scalar types don't have
2585/// destructors. For example:
2586///
2587/// \code
2588/// typedef int T;
2589/// void f(int *p) {
2590/// p->T::~T();
2591/// }
2592/// \endcode
2593///
2594/// Pseudo-destructors typically occur when instantiating templates such as:
2595///
2596/// \code
2597/// template<typename T>
2598/// void destroy(T* ptr) {
2599/// ptr->T::~T();
2600/// }
2601/// \endcode
2602///
2603/// for scalar types. A pseudo-destructor expression has no run-time semantics
2604/// beyond evaluating the base expression.
2606 friend class ASTStmtReader;
2607
2608 /// The base expression (that is being destroyed).
2609 Stmt *Base = nullptr;
2610
2611 /// Whether the operator was an arrow ('->'); otherwise, it was a
2612 /// period ('.').
2613 bool IsArrow : 1;
2614
2615 /// The location of the '.' or '->' operator.
2616 SourceLocation OperatorLoc;
2617
2618 /// The nested-name-specifier that follows the operator, if present.
2619 NestedNameSpecifierLoc QualifierLoc;
2620
2621 /// The type that precedes the '::' in a qualified pseudo-destructor
2622 /// expression.
2623 TypeSourceInfo *ScopeType = nullptr;
2624
2625 /// The location of the '::' in a qualified pseudo-destructor
2626 /// expression.
2627 SourceLocation ColonColonLoc;
2628
2629 /// The location of the '~'.
2630 SourceLocation TildeLoc;
2631
2632 /// The type being destroyed, or its name if we were unable to
2633 /// resolve the name.
2634 PseudoDestructorTypeStorage DestroyedType;
2635
2636public:
2637 CXXPseudoDestructorExpr(const ASTContext &Context,
2638 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
2639 NestedNameSpecifierLoc QualifierLoc,
2640 TypeSourceInfo *ScopeType,
2641 SourceLocation ColonColonLoc,
2642 SourceLocation TildeLoc,
2643 PseudoDestructorTypeStorage DestroyedType);
2644
2646 : Expr(CXXPseudoDestructorExprClass, Shell), IsArrow(false) {}
2647
2648 Expr *getBase() const { return cast<Expr>(Base); }
2649
2650 /// Determines whether this member expression actually had
2651 /// a C++ nested-name-specifier prior to the name of the member, e.g.,
2652 /// x->Base::foo.
2653 bool hasQualifier() const { return QualifierLoc.hasQualifier(); }
2654
2655 /// Retrieves the nested-name-specifier that qualifies the type name,
2656 /// with source-location information.
2657 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2658
2659 /// If the member name was qualified, retrieves the
2660 /// nested-name-specifier that precedes the member name. Otherwise, returns
2661 /// null.
2663 return QualifierLoc.getNestedNameSpecifier();
2664 }
2665
2666 /// Determine whether this pseudo-destructor expression was written
2667 /// using an '->' (otherwise, it used a '.').
2668 bool isArrow() const { return IsArrow; }
2669
2670 /// Retrieve the location of the '.' or '->' operator.
2671 SourceLocation getOperatorLoc() const { return OperatorLoc; }
2672
2673 /// Retrieve the scope type in a qualified pseudo-destructor
2674 /// expression.
2675 ///
2676 /// Pseudo-destructor expressions can have extra qualification within them
2677 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T().
2678 /// Here, if the object type of the expression is (or may be) a scalar type,
2679 /// \p T may also be a scalar type and, therefore, cannot be part of a
2680 /// nested-name-specifier. It is stored as the "scope type" of the pseudo-
2681 /// destructor expression.
2682 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; }
2683
2684 /// Retrieve the location of the '::' in a qualified pseudo-destructor
2685 /// expression.
2686 SourceLocation getColonColonLoc() const { return ColonColonLoc; }
2687
2688 /// Retrieve the location of the '~'.
2689 SourceLocation getTildeLoc() const { return TildeLoc; }
2690
2691 /// Retrieve the source location information for the type
2692 /// being destroyed.
2693 ///
2694 /// This type-source information is available for non-dependent
2695 /// pseudo-destructor expressions and some dependent pseudo-destructor
2696 /// expressions. Returns null if we only have the identifier for a
2697 /// dependent pseudo-destructor expression.
2699 return DestroyedType.getTypeSourceInfo();
2700 }
2701
2702 /// In a dependent pseudo-destructor expression for which we do not
2703 /// have full type information on the destroyed type, provides the name
2704 /// of the destroyed type.
2706 return DestroyedType.getIdentifier();
2707 }
2708
2709 /// Retrieve the type being destroyed.
2710 QualType getDestroyedType() const;
2711
2712 /// Retrieve the starting location of the type being destroyed.
2714 return DestroyedType.getLocation();
2715 }
2716
2717 /// Set the name of destroyed type for a dependent pseudo-destructor
2718 /// expression.
2720 DestroyedType = PseudoDestructorTypeStorage(II, Loc);
2721 }
2722
2723 /// Set the destroyed type.
2725 DestroyedType = PseudoDestructorTypeStorage(Info);
2726 }
2727
2728 SourceLocation getBeginLoc() const LLVM_READONLY {
2729 return Base->getBeginLoc();
2730 }
2731 SourceLocation getEndLoc() const LLVM_READONLY;
2732
2733 static bool classof(const Stmt *T) {
2734 return T->getStmtClass() == CXXPseudoDestructorExprClass;
2735 }
2736
2737 // Iterators
2739
2741 return const_child_range(&Base, &Base + 1);
2742 }
2743};
2744
2745/// A type trait used in the implementation of various C++11 and
2746/// Library TR1 trait templates.
2747///
2748/// \code
2749/// __is_pod(int) == true
2750/// __is_enum(std::string) == false
2751/// __is_trivially_constructible(vector<int>, int*, int*)
2752/// \endcode
2753class TypeTraitExpr final
2754 : public Expr,
2755 private llvm::TrailingObjects<TypeTraitExpr, TypeSourceInfo *> {
2756 /// The location of the type trait keyword.
2757 SourceLocation Loc;
2758
2759 /// The location of the closing parenthesis.
2760 SourceLocation RParenLoc;
2761
2762 // Note: The TypeSourceInfos for the arguments are allocated after the
2763 // TypeTraitExpr.
2764
2767 SourceLocation RParenLoc,
2768 bool Value);
2769
2770 TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) {}
2771
2772 size_t numTrailingObjects(OverloadToken<TypeSourceInfo *>) const {
2773 return getNumArgs();
2774 }
2775
2776public:
2777 friend class ASTStmtReader;
2778 friend class ASTStmtWriter;
2780
2781 /// Create a new type trait expression.
2782 static TypeTraitExpr *Create(const ASTContext &C, QualType T,
2783 SourceLocation Loc, TypeTrait Kind,
2785 SourceLocation RParenLoc,
2786 bool Value);
2787
2789 unsigned NumArgs);
2790
2791 /// Determine which type trait this expression uses.
2793 return static_cast<TypeTrait>(TypeTraitExprBits.Kind);
2794 }
2795
2796 bool getValue() const {
2797 assert(!isValueDependent());
2798 return TypeTraitExprBits.Value;
2799 }
2800
2801 /// Determine the number of arguments to this type trait.
2802 unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; }
2803
2804 /// Retrieve the Ith argument.
2805 TypeSourceInfo *getArg(unsigned I) const {
2806 assert(I < getNumArgs() && "Argument out-of-range");
2807 return getArgs()[I];
2808 }
2809
2810 /// Retrieve the argument types.
2812 return llvm::ArrayRef(getTrailingObjects<TypeSourceInfo *>(), getNumArgs());
2813 }
2814
2815 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2816 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
2817
2818 static bool classof(const Stmt *T) {
2819 return T->getStmtClass() == TypeTraitExprClass;
2820 }
2821
2822 // Iterators
2825 }
2826
2829 }
2830};
2831
2832/// An Embarcadero array type trait, as used in the implementation of
2833/// __array_rank and __array_extent.
2834///
2835/// Example:
2836/// \code
2837/// __array_rank(int[10][20]) == 2
2838/// __array_extent(int, 1) == 20
2839/// \endcode
2840class ArrayTypeTraitExpr : public Expr {
2841 /// The trait. An ArrayTypeTrait enum in MSVC compat unsigned.
2842 unsigned ATT : 2;
2843
2844 /// The value of the type trait. Unspecified if dependent.
2845 uint64_t Value = 0;
2846
2847 /// The array dimension being queried, or -1 if not used.
2848 Expr *Dimension;
2849
2850 /// The location of the type trait keyword.
2851 SourceLocation Loc;
2852
2853 /// The location of the closing paren.
2854 SourceLocation RParen;
2855
2856 /// The type being queried.
2857 TypeSourceInfo *QueriedType = nullptr;
2858
2859public:
2860 friend class ASTStmtReader;
2861
2863 TypeSourceInfo *queried, uint64_t value, Expr *dimension,
2864 SourceLocation rparen, QualType ty)
2865 : Expr(ArrayTypeTraitExprClass, ty, VK_PRValue, OK_Ordinary), ATT(att),
2866 Value(value), Dimension(dimension), Loc(loc), RParen(rparen),
2867 QueriedType(queried) {
2868 assert(att <= ATT_Last && "invalid enum value!");
2869 assert(static_cast<unsigned>(att) == ATT && "ATT overflow!");
2871 }
2872
2874 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0) {}
2875
2876 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2877 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
2878
2879 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); }
2880
2881 QualType getQueriedType() const { return QueriedType->getType(); }
2882
2883 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; }
2884
2885 uint64_t getValue() const { assert(!isTypeDependent()); return Value; }
2886
2887 Expr *getDimensionExpression() const { return Dimension; }
2888
2889 static bool classof(const Stmt *T) {
2890 return T->getStmtClass() == ArrayTypeTraitExprClass;
2891 }
2892
2893 // Iterators
2896 }
2897
2900 }
2901};
2902
2903/// An expression trait intrinsic.
2904///
2905/// Example:
2906/// \code
2907/// __is_lvalue_expr(std::cout) == true
2908/// __is_lvalue_expr(1) == false
2909/// \endcode
2911 /// The trait. A ExpressionTrait enum in MSVC compatible unsigned.
2912 unsigned ET : 31;
2913
2914 /// The value of the type trait. Unspecified if dependent.
2915 unsigned Value : 1;
2916
2917 /// The location of the type trait keyword.
2918 SourceLocation Loc;
2919
2920 /// The location of the closing paren.
2921 SourceLocation RParen;
2922
2923 /// The expression being queried.
2924 Expr* QueriedExpression = nullptr;
2925
2926public:
2927 friend class ASTStmtReader;
2928
2930 bool value, SourceLocation rparen, QualType resultType)
2931 : Expr(ExpressionTraitExprClass, resultType, VK_PRValue, OK_Ordinary),
2932 ET(et), Value(value), Loc(loc), RParen(rparen),
2933 QueriedExpression(queried) {
2934 assert(et <= ET_Last && "invalid enum value!");
2935 assert(static_cast<unsigned>(et) == ET && "ET overflow!");
2937 }
2938
2940 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false) {}
2941
2942 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2943 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
2944
2945 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); }
2946
2947 Expr *getQueriedExpression() const { return QueriedExpression; }
2948
2949 bool getValue() const { return Value; }
2950
2951 static bool classof(const Stmt *T) {
2952 return T->getStmtClass() == ExpressionTraitExprClass;
2953 }
2954
2955 // Iterators
2958 }
2959
2962 }
2963};
2964
2965/// A reference to an overloaded function set, either an
2966/// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr.
2967class OverloadExpr : public Expr {
2968 friend class ASTStmtReader;
2969 friend class ASTStmtWriter;
2970
2971 /// The common name of these declarations.
2972 DeclarationNameInfo NameInfo;
2973
2974 /// The nested-name-specifier that qualifies the name, if any.
2975 NestedNameSpecifierLoc QualifierLoc;
2976
2977protected:
2978 OverloadExpr(StmtClass SC, const ASTContext &Context,
2979 NestedNameSpecifierLoc QualifierLoc,
2980 SourceLocation TemplateKWLoc,
2981 const DeclarationNameInfo &NameInfo,
2982 const TemplateArgumentListInfo *TemplateArgs,
2984 bool KnownDependent, bool KnownInstantiationDependent,
2985 bool KnownContainsUnexpandedParameterPack);
2986
2987 OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
2988 bool HasTemplateKWAndArgsInfo);
2989
2990 /// Return the results. Defined after UnresolvedMemberExpr.
2993 return const_cast<OverloadExpr *>(this)->getTrailingResults();
2994 }
2995
2996 /// Return the optional template keyword and arguments info.
2997 /// Defined after UnresolvedMemberExpr.
3000 return const_cast<OverloadExpr *>(this)
3002 }
3003
3004 /// Return the optional template arguments. Defined after
3005 /// UnresolvedMemberExpr.
3008 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
3009 }
3010
3012 return OverloadExprBits.HasTemplateKWAndArgsInfo;
3013 }
3014
3015public:
3016 struct FindResult {
3020 };
3021
3022 /// Finds the overloaded expression in the given expression \p E of
3023 /// OverloadTy.
3024 ///
3025 /// \return the expression (which must be there) and true if it has
3026 /// the particular form of a member pointer expression
3027 static FindResult find(Expr *E) {
3028 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload));
3029
3031
3032 E = E->IgnoreParens();
3033 if (isa<UnaryOperator>(E)) {
3034 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf);
3035 E = cast<UnaryOperator>(E)->getSubExpr();
3036 auto *Ovl = cast<OverloadExpr>(E->IgnoreParens());
3037
3038 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier());
3039 Result.IsAddressOfOperand = true;
3040 Result.Expression = Ovl;
3041 } else {
3042 Result.HasFormOfMemberPointer = false;
3043 Result.IsAddressOfOperand = false;
3044 Result.Expression = cast<OverloadExpr>(E);
3045 }
3046
3047 return Result;
3048 }
3049
3050 /// Gets the naming class of this lookup, if any.
3051 /// Defined after UnresolvedMemberExpr.
3052 inline CXXRecordDecl *getNamingClass();
3054 return const_cast<OverloadExpr *>(this)->getNamingClass();
3055 }
3056
3058
3061 }
3064 }
3065 llvm::iterator_range<decls_iterator> decls() const {
3066 return llvm::make_range(decls_begin(), decls_end());
3067 }
3068
3069 /// Gets the number of declarations in the unresolved set.
3070 unsigned getNumDecls() const { return OverloadExprBits.NumResults; }
3071
3072 /// Gets the full name info.
3073 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
3074
3075 /// Gets the name looked up.
3076 DeclarationName getName() const { return NameInfo.getName(); }
3077
3078 /// Gets the location of the name.
3079 SourceLocation getNameLoc() const { return NameInfo.getLoc(); }
3080
3081 /// Fetches the nested-name qualifier, if one was given.
3083 return QualifierLoc.getNestedNameSpecifier();
3084 }
3085
3086 /// Fetches the nested-name qualifier with source-location
3087 /// information, if one was given.
3088 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3089
3090 /// Retrieve the location of the template keyword preceding
3091 /// this name, if any.
3094 return SourceLocation();
3096 }
3097
3098 /// Retrieve the location of the left angle bracket starting the
3099 /// explicit template argument list following the name, if any.
3102 return SourceLocation();
3104 }
3105
3106 /// Retrieve the location of the right angle bracket ending the
3107 /// explicit template argument list following the name, if any.
3110 return SourceLocation();
3112 }
3113
3114 /// Determines whether the name was preceded by the template keyword.
3116
3117 /// Determines whether this expression had explicit template arguments.
3118 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3119
3122 return nullptr;
3123 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
3124 }
3125
3126 unsigned getNumTemplateArgs() const {
3128 return 0;
3129
3131 }
3132
3134 return {getTemplateArgs(), getNumTemplateArgs()};
3135 }
3136
3137 /// Copies the template arguments into the given structure.
3141 }
3142
3143 static bool classof(const Stmt *T) {
3144 return T->getStmtClass() == UnresolvedLookupExprClass ||
3145 T->getStmtClass() == UnresolvedMemberExprClass;
3146 }
3147};
3148
3149/// A reference to a name which we were able to look up during
3150/// parsing but could not resolve to a specific declaration.
3151///
3152/// This arises in several ways:
3153/// * we might be waiting for argument-dependent lookup;
3154/// * the name might resolve to an overloaded function;
3155/// and eventually:
3156/// * the lookup might have included a function template.
3157///
3158/// These never include UnresolvedUsingValueDecls, which are always class
3159/// members and therefore appear only in UnresolvedMemberLookupExprs.
3161 : public OverloadExpr,
3162 private llvm::TrailingObjects<UnresolvedLookupExpr, DeclAccessPair,
3163 ASTTemplateKWAndArgsInfo,
3164 TemplateArgumentLoc> {
3165 friend class ASTStmtReader;
3166 friend class OverloadExpr;
3167 friend TrailingObjects;
3168
3169 /// The naming class (C++ [class.access.base]p5) of the lookup, if
3170 /// any. This can generally be recalculated from the context chain,
3171 /// but that can be fairly expensive for unqualified lookups.
3172 CXXRecordDecl *NamingClass;
3173
3174 // UnresolvedLookupExpr is followed by several trailing objects.
3175 // They are in order:
3176 //
3177 // * An array of getNumResults() DeclAccessPair for the results. These are
3178 // undesugared, which is to say, they may include UsingShadowDecls.
3179 // Access is relative to the naming class.
3180 //
3181 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3182 // template keyword and arguments. Present if and only if
3183 // hasTemplateKWAndArgsInfo().
3184 //
3185 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing
3186 // location information for the explicitly specified template arguments.
3187
3188 UnresolvedLookupExpr(const ASTContext &Context, CXXRecordDecl *NamingClass,
3189 NestedNameSpecifierLoc QualifierLoc,
3190 SourceLocation TemplateKWLoc,
3191 const DeclarationNameInfo &NameInfo, bool RequiresADL,
3192 bool Overloaded,
3193 const TemplateArgumentListInfo *TemplateArgs,
3195
3196 UnresolvedLookupExpr(EmptyShell Empty, unsigned NumResults,
3197 bool HasTemplateKWAndArgsInfo);
3198
3199 unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const {
3200 return getNumDecls();
3201 }
3202
3203 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3204 return hasTemplateKWAndArgsInfo();
3205 }
3206
3207public:
3208 static UnresolvedLookupExpr *
3209 Create(const ASTContext &Context, CXXRecordDecl *NamingClass,
3210 NestedNameSpecifierLoc QualifierLoc,
3211 const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
3212 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3213
3214 static UnresolvedLookupExpr *
3215 Create(const ASTContext &Context, CXXRecordDecl *NamingClass,
3216 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
3217 const DeclarationNameInfo &NameInfo, bool RequiresADL,
3218 const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
3219 UnresolvedSetIterator End);
3220
3221 static UnresolvedLookupExpr *CreateEmpty(const ASTContext &Context,
3222 unsigned NumResults,
3223 bool HasTemplateKWAndArgsInfo,
3224 unsigned NumTemplateArgs);
3225
3226 /// True if this declaration should be extended by
3227 /// argument-dependent lookup.
3228 bool requiresADL() const { return UnresolvedLookupExprBits.RequiresADL; }
3229
3230 /// True if this lookup is overloaded.
3231 bool isOverloaded() const { return UnresolvedLookupExprBits.Overloaded; }
3232
3233 /// Gets the 'naming class' (in the sense of C++0x
3234 /// [class.access.base]p5) of the lookup. This is the scope
3235 /// that was looked in to find these results.
3236 CXXRecordDecl *getNamingClass() { return NamingClass; }
3237 const CXXRecordDecl *getNamingClass() const { return NamingClass; }
3238
3239 SourceLocation getBeginLoc() const LLVM_READONLY {
3241 return l.getBeginLoc();
3242 return getNameInfo().getBeginLoc();
3243 }
3244
3245 SourceLocation getEndLoc() const LLVM_READONLY {
3247 return getRAngleLoc();
3248 return getNameInfo().getEndLoc();
3249 }
3250
3253 }
3254
3257 }
3258
3259 static bool classof(const Stmt *T) {
3260 return T->getStmtClass() == UnresolvedLookupExprClass;
3261 }
3262};
3263
3264/// A qualified reference to a name whose declaration cannot
3265/// yet be resolved.
3266///
3267/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that
3268/// it expresses a reference to a declaration such as
3269/// X<T>::value. The difference, however, is that an
3270/// DependentScopeDeclRefExpr node is used only within C++ templates when
3271/// the qualification (e.g., X<T>::) refers to a dependent type. In
3272/// this case, X<T>::value cannot resolve to a declaration because the
3273/// declaration will differ from one instantiation of X<T> to the
3274/// next. Therefore, DependentScopeDeclRefExpr keeps track of the
3275/// qualifier (X<T>::) and the name of the entity being referenced
3276/// ("value"). Such expressions will instantiate to a DeclRefExpr once the
3277/// declaration can be found.
3279 : public Expr,
3280 private llvm::TrailingObjects<DependentScopeDeclRefExpr,
3281 ASTTemplateKWAndArgsInfo,
3282 TemplateArgumentLoc> {
3283 friend class ASTStmtReader;
3284 friend class ASTStmtWriter;
3285 friend TrailingObjects;
3286
3287 /// The nested-name-specifier that qualifies this unresolved
3288 /// declaration name.
3289 NestedNameSpecifierLoc QualifierLoc;
3290
3291 /// The name of the entity we will be referencing.
3292 DeclarationNameInfo NameInfo;
3293
3295 SourceLocation TemplateKWLoc,
3296 const DeclarationNameInfo &NameInfo,
3297 const TemplateArgumentListInfo *Args);
3298
3299 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3300 return hasTemplateKWAndArgsInfo();
3301 }
3302
3303 bool hasTemplateKWAndArgsInfo() const {
3304 return DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo;
3305 }
3306
3307public:
3308 static DependentScopeDeclRefExpr *
3309 Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
3310 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
3311 const TemplateArgumentListInfo *TemplateArgs);
3312
3313 static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &Context,
3314 bool HasTemplateKWAndArgsInfo,
3315 unsigned NumTemplateArgs);
3316
3317 /// Retrieve the name that this expression refers to.
3318 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
3319
3320 /// Retrieve the name that this expression refers to.
3321 DeclarationName getDeclName() const { return NameInfo.getName(); }
3322
3323 /// Retrieve the location of the name within the expression.
3324 ///
3325 /// For example, in "X<T>::value" this is the location of "value".
3326 SourceLocation getLocation() const { return NameInfo.getLoc(); }
3327
3328 /// Retrieve the nested-name-specifier that qualifies the
3329 /// name, with source location information.
3330 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3331
3332 /// Retrieve the nested-name-specifier that qualifies this
3333 /// declaration.
3335 return QualifierLoc.getNestedNameSpecifier();
3336 }
3337
3338 /// Retrieve the location of the template keyword preceding
3339 /// this name, if any.
3341 if (!hasTemplateKWAndArgsInfo())
3342 return SourceLocation();
3343 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
3344 }
3345
3346 /// Retrieve the location of the left angle bracket starting the
3347 /// explicit template argument list following the name, if any.
3349 if (!hasTemplateKWAndArgsInfo())
3350 return SourceLocation();
3351 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
3352 }
3353
3354 /// Retrieve the location of the right angle bracket ending the
3355 /// explicit template argument list following the name, if any.
3357 if (!hasTemplateKWAndArgsInfo())
3358 return SourceLocation();
3359 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
3360 }
3361
3362 /// Determines whether the name was preceded by the template keyword.
3364
3365 /// Determines whether this lookup had explicit template arguments.
3366 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3367
3368 /// Copies the template arguments (if present) into the given
3369 /// structure.
3372 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
3373 getTrailingObjects<TemplateArgumentLoc>(), List);
3374 }
3375
3378 return nullptr;
3379
3380 return getTrailingObjects<TemplateArgumentLoc>();
3381 }
3382
3383 unsigned getNumTemplateArgs() const {
3385 return 0;
3386
3387 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
3388 }
3389
3391 return {getTemplateArgs(), getNumTemplateArgs()};
3392 }
3393
3394 /// Note: getBeginLoc() is the start of the whole DependentScopeDeclRefExpr,
3395 /// and differs from getLocation().getStart().
3396 SourceLocation getBeginLoc() const LLVM_READONLY {
3397 return QualifierLoc.getBeginLoc();
3398 }
3399
3400 SourceLocation getEndLoc() const LLVM_READONLY {
3402 return getRAngleLoc();
3403 return getLocation();
3404 }
3405
3406 static bool classof(const Stmt *T) {
3407 return T->getStmtClass() == DependentScopeDeclRefExprClass;
3408 }
3409
3412 }
3413
3416 }
3417};
3418
3419/// Represents an expression -- generally a full-expression -- that
3420/// introduces cleanups to be run at the end of the sub-expression's
3421/// evaluation. The most common source of expression-introduced
3422/// cleanups is temporary objects in C++, but several other kinds of
3423/// expressions can create cleanups, including basically every
3424/// call in ARC that returns an Objective-C pointer.
3425///
3426/// This expression also tracks whether the sub-expression contains a
3427/// potentially-evaluated block literal. The lifetime of a block
3428/// literal is the extent of the enclosing scope.
3430 : public FullExpr,
3431 private llvm::TrailingObjects<
3432 ExprWithCleanups,
3433 llvm::PointerUnion<BlockDecl *, CompoundLiteralExpr *>> {
3434public:
3435 /// The type of objects that are kept in the cleanup.
3436 /// It's useful to remember the set of blocks and block-scoped compound
3437 /// literals; we could also remember the set of temporaries, but there's
3438 /// currently no need.
3439 using CleanupObject = llvm::PointerUnion<BlockDecl *, CompoundLiteralExpr *>;
3440
3441private:
3442 friend class ASTStmtReader;
3443 friend TrailingObjects;
3444
3445 ExprWithCleanups(EmptyShell, unsigned NumObjects);
3446 ExprWithCleanups(Expr *SubExpr, bool CleanupsHaveSideEffects,
3447 ArrayRef<CleanupObject> Objects);
3448
3449public:
3450 static ExprWithCleanups *Create(const ASTContext &C, EmptyShell empty,
3451 unsigned numObjects);
3452
3453 static ExprWithCleanups *Create(const ASTContext &C, Expr *subexpr,
3454 bool CleanupsHaveSideEffects,
3455 ArrayRef<CleanupObject> objects);
3456
3458 return llvm::ArrayRef(getTrailingObjects<CleanupObject>(), getNumObjects());
3459 }
3460
3461 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; }
3462
3463 CleanupObject getObject(unsigned i) const {
3464 assert(i < getNumObjects() && "Index out of range");
3465 return getObjects()[i];
3466 }
3467
3469 return ExprWithCleanupsBits.CleanupsHaveSideEffects;
3470 }
3471
3472 SourceLocation getBeginLoc() const LLVM_READONLY {
3473 return SubExpr->getBeginLoc();
3474 }
3475
3476 SourceLocation getEndLoc() const LLVM_READONLY {
3477 return SubExpr->getEndLoc();
3478 }
3479
3480 // Implement isa/cast/dyncast/etc.
3481 static bool classof(const Stmt *T) {
3482 return T->getStmtClass() == ExprWithCleanupsClass;
3483 }
3484
3485 // Iterators
3487
3489 return const_child_range(&SubExpr, &SubExpr + 1);
3490 }
3491};
3492
3493/// Describes an explicit type conversion that uses functional
3494/// notion but could not be resolved because one or more arguments are
3495/// type-dependent.
3496///
3497/// The explicit type conversions expressed by
3498/// CXXUnresolvedConstructExpr have the form <tt>T(a1, a2, ..., aN)</tt>,
3499/// where \c T is some type and \c a1, \c a2, ..., \c aN are values, and
3500/// either \c T is a dependent type or one or more of the <tt>a</tt>'s is
3501/// type-dependent. For example, this would occur in a template such
3502/// as:
3503///
3504/// \code
3505/// template<typename T, typename A1>
3506/// inline T make_a(const A1& a1) {
3507/// return T(a1);
3508/// }
3509/// \endcode
3510///
3511/// When the returned expression is instantiated, it may resolve to a
3512/// constructor call, conversion function call, or some kind of type
3513/// conversion.
3515 : public Expr,
3516 private llvm::TrailingObjects<CXXUnresolvedConstructExpr, Expr *> {
3517 friend class ASTStmtReader;
3518 friend TrailingObjects;
3519
3520 /// The type being constructed, and whether the construct expression models
3521 /// list initialization or not.
3522 llvm::PointerIntPair<TypeSourceInfo *, 1> TypeAndInitForm;
3523
3524 /// The location of the left parentheses ('(').
3525 SourceLocation LParenLoc;
3526
3527 /// The location of the right parentheses (')').
3528 SourceLocation RParenLoc;
3529
3531 SourceLocation LParenLoc, ArrayRef<Expr *> Args,
3532 SourceLocation RParenLoc, bool IsListInit);
3533
3534 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs)
3535 : Expr(CXXUnresolvedConstructExprClass, Empty) {
3536 CXXUnresolvedConstructExprBits.NumArgs = NumArgs;
3537 }
3538
3539public:
3541 Create(const ASTContext &Context, QualType T, TypeSourceInfo *TSI,
3542 SourceLocation LParenLoc, ArrayRef<Expr *> Args,
3543 SourceLocation RParenLoc, bool IsListInit);
3544
3545 static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &Context,
3546 unsigned NumArgs);
3547
3548 /// Retrieve the type that is being constructed, as specified
3549 /// in the source code.
3551
3552 /// Retrieve the type source information for the type being
3553 /// constructed.
3555 return TypeAndInitForm.getPointer();
3556 }
3557
3558 /// Retrieve the location of the left parentheses ('(') that
3559 /// precedes the argument list.
3560 SourceLocation getLParenLoc() const { return LParenLoc; }
3561 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
3562
3563 /// Retrieve the location of the right parentheses (')') that
3564 /// follows the argument list.
3565 SourceLocation getRParenLoc() const { return RParenLoc; }
3566 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
3567
3568 /// Determine whether this expression models list-initialization.
3569 /// If so, there will be exactly one subexpression, which will be
3570 /// an InitListExpr.
3571 bool isListInitialization() const { return TypeAndInitForm.getInt(); }
3572
3573 /// Retrieve the number of arguments.
3574 unsigned getNumArgs() const { return CXXUnresolvedConstructExprBits.NumArgs; }
3575
3576 using arg_iterator = Expr **;
3577 using arg_range = llvm::iterator_range<arg_iterator>;
3578
3579 arg_iterator arg_begin() { return getTrailingObjects<Expr *>(); }
3582
3583 using const_arg_iterator = const Expr* const *;
3584 using const_arg_range = llvm::iterator_range<const_arg_iterator>;
3585
3586 const_arg_iterator arg_begin() const { return getTrailingObjects<Expr *>(); }
3589 return const_arg_range(arg_begin(), arg_end());
3590 }
3591
3592 Expr *getArg(unsigned I) {
3593 assert(I < getNumArgs() && "Argument index out-of-range");
3594 return arg_begin()[I];
3595 }
3596
3597 const Expr *getArg(unsigned I) const {
3598 assert(I < getNumArgs() && "Argument index out-of-range");
3599 return arg_begin()[I];
3600 }
3601
3602 void setArg(unsigned I, Expr *E) {
3603 assert(I < getNumArgs() && "Argument index out-of-range");
3604 arg_begin()[I] = E;
3605 }
3606
3607 SourceLocation getBeginLoc() const LLVM_READONLY;
3608 SourceLocation getEndLoc() const LLVM_READONLY {
3609 if (!RParenLoc.isValid() && getNumArgs() > 0)
3610 return getArg(getNumArgs() - 1)->getEndLoc();
3611 return RParenLoc;
3612 }
3613
3614 static bool classof(const Stmt *T) {
3615 return T->getStmtClass() == CXXUnresolvedConstructExprClass;
3616 }
3617
3618 // Iterators
3620 auto **begin = reinterpret_cast<Stmt **>(arg_begin());
3621 return child_range(begin, begin + getNumArgs());
3622 }
3623
3625 auto **begin = reinterpret_cast<Stmt **>(
3626 const_cast<CXXUnresolvedConstructExpr *>(this)->arg_begin());
3627 return const_child_range(begin, begin + getNumArgs());
3628 }
3629};
3630
3631/// Represents a C++ member access expression where the actual
3632/// member referenced could not be resolved because the base
3633/// expression or the member name was dependent.
3634///
3635/// Like UnresolvedMemberExprs, these can be either implicit or
3636/// explicit accesses. It is only possible to get one of these with
3637/// an implicit access if a qualifier is provided.
3639 : public Expr,
3640 private llvm::TrailingObjects<CXXDependentScopeMemberExpr,
3641 ASTTemplateKWAndArgsInfo,
3642 TemplateArgumentLoc, NamedDecl *> {
3643 friend class ASTStmtReader;
3644 friend class ASTStmtWriter;
3645 friend TrailingObjects;
3646
3647 /// The expression for the base pointer or class reference,
3648 /// e.g., the \c x in x.f. Can be null in implicit accesses.
3649 Stmt *Base;
3650
3651 /// The type of the base expression. Never null, even for
3652 /// implicit accesses.
3653 QualType BaseType;
3654
3655 /// The nested-name-specifier that precedes the member name, if any.
3656 /// FIXME: This could be in principle store as a trailing object.
3657 /// However the performance impact of doing so should be investigated first.
3658 NestedNameSpecifierLoc QualifierLoc;
3659
3660 /// The member to which this member expression refers, which
3661 /// can be name, overloaded operator, or destructor.
3662 ///
3663 /// FIXME: could also be a template-id
3664 DeclarationNameInfo MemberNameInfo;
3665
3666 // CXXDependentScopeMemberExpr is followed by several trailing objects,
3667 // some of which optional. They are in order:
3668 //
3669 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3670 // template keyword and arguments. Present if and only if
3671 // hasTemplateKWAndArgsInfo().
3672 //
3673 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing location
3674 // information for the explicitly specified template arguments.
3675 //
3676 // * An optional NamedDecl *. In a qualified member access expression such
3677 // as t->Base::f, this member stores the resolves of name lookup in the
3678 // context of the member access expression, to be used at instantiation
3679 // time. Present if and only if hasFirstQualifierFoundInScope().
3680
3681 bool hasTemplateKWAndArgsInfo() const {
3682 return CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo;
3683 }
3684
3685 bool hasFirstQualifierFoundInScope() const {
3686 return CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope;
3687 }
3688
3689 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3690 return hasTemplateKWAndArgsInfo();
3691 }
3692
3693 unsigned numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const {
3694 return getNumTemplateArgs();
3695 }
3696
3697 unsigned numTrailingObjects(OverloadToken<NamedDecl *>) const {
3698 return hasFirstQualifierFoundInScope();
3699 }
3700
3701 CXXDependentScopeMemberExpr(const ASTContext &Ctx, Expr *Base,
3702 QualType BaseType, bool IsArrow,
3703 SourceLocation OperatorLoc,
3704 NestedNameSpecifierLoc QualifierLoc,
3705 SourceLocation TemplateKWLoc,
3706 NamedDecl *FirstQualifierFoundInScope,
3707 DeclarationNameInfo MemberNameInfo,
3708 const TemplateArgumentListInfo *TemplateArgs);
3709
3710 CXXDependentScopeMemberExpr(EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
3711 bool HasFirstQualifierFoundInScope);
3712
3713public:
3714 static CXXDependentScopeMemberExpr *
3715 Create(const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
3716 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
3717 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
3718 DeclarationNameInfo MemberNameInfo,
3719 const TemplateArgumentListInfo *TemplateArgs);
3720
3721 static CXXDependentScopeMemberExpr *
3722 CreateEmpty(const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
3723 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope);
3724
3725 /// True if this is an implicit access, i.e. one in which the
3726 /// member being accessed was not written in the source. The source
3727 /// location of the operator is invalid in this case.
3728 bool isImplicitAccess() const {
3729 if (!Base)
3730 return true;
3731 return cast<Expr>(Base)->isImplicitCXXThis();
3732 }
3733
3734 /// Retrieve the base object of this member expressions,
3735 /// e.g., the \c x in \c x.m.
3736 Expr *getBase() const {
3737 assert(!isImplicitAccess());
3738 return cast<Expr>(Base);
3739 }
3740
3741 QualType getBaseType() const { return BaseType; }
3742
3743 /// Determine whether this member expression used the '->'
3744 /// operator; otherwise, it used the '.' operator.
3745 bool isArrow() const { return CXXDependentScopeMemberExprBits.IsArrow; }
3746
3747 /// Retrieve the location of the '->' or '.' operator.
3749 return CXXDependentScopeMemberExprBits.OperatorLoc;
3750 }
3751
3752 /// Retrieve the nested-name-specifier that qualifies the member name.
3754 return QualifierLoc.getNestedNameSpecifier();
3755 }
3756
3757 /// Retrieve the nested-name-specifier that qualifies the member
3758 /// name, with source location information.
3759 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3760
3761 /// Retrieve the first part of the nested-name-specifier that was
3762 /// found in the scope of the member access expression when the member access
3763 /// was initially parsed.
3764 ///
3765 /// This function only returns a useful result when member access expression
3766 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration
3767 /// returned by this function describes what was found by unqualified name
3768 /// lookup for the identifier "Base" within the scope of the member access
3769 /// expression itself. At template instantiation time, this information is
3770 /// combined with the results of name lookup into the type of the object
3771 /// expression itself (the class type of x).
3773 if (!hasFirstQualifierFoundInScope())
3774 return nullptr;
3775 return *getTrailingObjects<NamedDecl *>();
3776 }
3777
3778 /// Retrieve the name of the member that this expression refers to.
3780 return MemberNameInfo;
3781 }
3782
3783 /// Retrieve the name of the member that this expression refers to.
3784 DeclarationName getMember() const { return MemberNameInfo.getName(); }
3785
3786 // Retrieve the location of the name of the member that this
3787 // expression refers to.
3788 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); }
3789
3790 /// Retrieve the location of the template keyword preceding the
3791 /// member name, if any.
3793 if (!hasTemplateKWAndArgsInfo())
3794 return SourceLocation();
3795 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
3796 }
3797
3798 /// Retrieve the location of the left angle bracket starting the
3799 /// explicit template argument list following the member name, if any.
3801 if (!hasTemplateKWAndArgsInfo())
3802 return SourceLocation();
3803 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
3804 }
3805
3806 /// Retrieve the location of the right angle bracket ending the
3807 /// explicit template argument list following the member name, if any.
3809 if (!hasTemplateKWAndArgsInfo())
3810 return SourceLocation();
3811 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
3812 }
3813
3814 /// Determines whether the member name was preceded by the template keyword.
3816
3817 /// Determines whether this member expression actually had a C++
3818 /// template argument list explicitly specified, e.g., x.f<int>.
3819 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3820
3821 /// Copies the template arguments (if present) into the given
3822 /// structure.
3825 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
3826 getTrailingObjects<TemplateArgumentLoc>(), List);
3827 }
3828
3829 /// Retrieve the template arguments provided as part of this
3830 /// template-id.
3833 return nullptr;
3834
3835 return getTrailingObjects<TemplateArgumentLoc>();
3836 }
3837
3838 /// Retrieve the number of template arguments provided as part of this
3839 /// template-id.
3840 unsigned getNumTemplateArgs() const {
3842 return 0;
3843
3844 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
3845 }
3846
3848 return {getTemplateArgs(), getNumTemplateArgs()};
3849 }
3850
3851 SourceLocation getBeginLoc() const LLVM_READONLY {
3852 if (!isImplicitAccess())
3853 return Base->getBeginLoc();
3854 if (getQualifier())
3855 return getQualifierLoc().getBeginLoc();
3856 return MemberNameInfo.getBeginLoc();
3857 }
3858
3859 SourceLocation getEndLoc() const LLVM_READONLY {
3861 return getRAngleLoc();
3862 return MemberNameInfo.getEndLoc();
3863 }
3864
3865 static bool classof(const Stmt *T) {
3866 return T->getStmtClass() == CXXDependentScopeMemberExprClass;
3867 }
3868
3869 // Iterators
3871 if (isImplicitAccess())
3873 return child_range(&Base, &Base + 1);
3874 }
3875
3877 if (isImplicitAccess())
3879 return const_child_range(&Base, &Base + 1);
3880 }
3881};
3882
3883/// Represents a C++ member access expression for which lookup
3884/// produced a set of overloaded functions.
3885///
3886/// The member access may be explicit or implicit:
3887/// \code
3888/// struct A {
3889/// int a, b;
3890/// int explicitAccess() { return this->a + this->A::b; }
3891/// int implicitAccess() { return a + A::b; }
3892/// };
3893/// \endcode
3894///
3895/// In the final AST, an explicit access always becomes a MemberExpr.
3896/// An implicit access may become either a MemberExpr or a
3897/// DeclRefExpr, depending on whether the member is static.
3899 : public OverloadExpr,
3900 private llvm::TrailingObjects<UnresolvedMemberExpr, DeclAccessPair,
3901 ASTTemplateKWAndArgsInfo,
3902 TemplateArgumentLoc> {
3903 friend class ASTStmtReader;
3904 friend class OverloadExpr;
3905 friend TrailingObjects;
3906
3907 /// The expression for the base pointer or class reference,
3908 /// e.g., the \c x in x.f.
3909 ///
3910 /// This can be null if this is an 'unbased' member expression.
3911 Stmt *Base;
3912
3913 /// The type of the base expression; never null.
3914 QualType BaseType;
3915
3916 /// The location of the '->' or '.' operator.
3917 SourceLocation OperatorLoc;
3918
3919 // UnresolvedMemberExpr is followed by several trailing objects.
3920 // They are in order:
3921 //
3922 // * An array of getNumResults() DeclAccessPair for the results. These are
3923 // undesugared, which is to say, they may include UsingShadowDecls.
3924 // Access is relative to the naming class.
3925 //
3926 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3927 // template keyword and arguments. Present if and only if
3928 // hasTemplateKWAndArgsInfo().
3929 //
3930 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing
3931 // location information for the explicitly specified template arguments.
3932
3933 UnresolvedMemberExpr(const ASTContext &Context, bool HasUnresolvedUsing,
3934 Expr *Base, QualType BaseType, bool IsArrow,
3935 SourceLocation OperatorLoc,
3936 NestedNameSpecifierLoc QualifierLoc,
3937 SourceLocation TemplateKWLoc,
3938 const DeclarationNameInfo &MemberNameInfo,
3939 const TemplateArgumentListInfo *TemplateArgs,
3941
3942 UnresolvedMemberExpr(EmptyShell Empty, unsigned NumResults,
3943 bool HasTemplateKWAndArgsInfo);
3944
3945 unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const {
3946 return getNumDecls();
3947 }
3948
3949 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3950 return hasTemplateKWAndArgsInfo();
3951 }
3952
3953public:
3954 static UnresolvedMemberExpr *
3955 Create(const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
3956 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
3957 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
3958 const DeclarationNameInfo &MemberNameInfo,
3959 const TemplateArgumentListInfo *TemplateArgs,
3960 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3961
3962 static UnresolvedMemberExpr *CreateEmpty(const ASTContext &Context,
3963 unsigned NumResults,
3964 bool HasTemplateKWAndArgsInfo,
3965 unsigned NumTemplateArgs);
3966
3967 /// True if this is an implicit access, i.e., one in which the
3968 /// member being accessed was not written in the source.
3969 ///
3970 /// The source location of the operator is invalid in this case.
3971 bool isImplicitAccess() const;
3972
3973 /// Retrieve the base object of this member expressions,
3974 /// e.g., the \c x in \c x.m.
3976 assert(!isImplicitAccess());
3977 return cast<Expr>(Base);
3978 }
3979 const Expr *getBase() const {
3980 assert(!isImplicitAccess());
3981 return cast<Expr>(Base);
3982 }
3983
3984 QualType getBaseType() const { return BaseType; }
3985
3986 /// Determine whether the lookup results contain an unresolved using
3987 /// declaration.
3988 bool hasUnresolvedUsing() const {
3989 return UnresolvedMemberExprBits.HasUnresolvedUsing;
3990 }
3991
3992 /// Determine whether this member expression used the '->'
3993 /// operator; otherwise, it used the '.' operator.
3994 bool isArrow() const { return UnresolvedMemberExprBits.IsArrow; }
3995
3996 /// Retrieve the location of the '->' or '.' operator.
3997 SourceLocation getOperatorLoc() const { return OperatorLoc; }
3998
3999 /// Retrieve the naming class of this lookup.
4002 return const_cast<UnresolvedMemberExpr *>(this)->getNamingClass();
4003 }
4004
4005 /// Retrieve the full name info for the member that this expression
4006 /// refers to.
4008
4009 /// Retrieve the name of the member that this expression refers to.
4011
4012 /// Retrieve the location of the name of the member that this
4013 /// expression refers to.
4015
4016 /// Return the preferred location (the member name) for the arrow when
4017 /// diagnosing a problem with this expression.
4018 SourceLocation getExprLoc() const LLVM_READONLY { return getMemberLoc(); }
4019
4020 SourceLocation getBeginLoc() const LLVM_READONLY {
4021 if (!isImplicitAccess())
4022 return Base->getBeginLoc();
4024 return l.getBeginLoc();
4025 return getMemberNameInfo().getBeginLoc();
4026 }
4027
4028 SourceLocation getEndLoc() const LLVM_READONLY {
4030 return getRAngleLoc();
4031 return getMemberNameInfo().getEndLoc();
4032 }
4033
4034 static bool classof(const Stmt *T) {
4035 return T->getStmtClass() == UnresolvedMemberExprClass;
4036 }
4037
4038 // Iterators
4040 if (isImplicitAccess())
4042 return child_range(&Base, &Base + 1);
4043 }
4044
4046 if (isImplicitAccess())
4048 return const_child_range(&Base, &Base + 1);
4049 }
4050};
4051
4053 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4054 return ULE->getTrailingObjects<DeclAccessPair>();
4055 return cast<UnresolvedMemberExpr>(this)->getTrailingObjects<DeclAccessPair>();
4056}
4057
4060 return nullptr;
4061
4062 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4063 return ULE->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
4064 return cast<UnresolvedMemberExpr>(this)
4065 ->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
4066}
4067
4069 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4070 return ULE->getTrailingObjects<TemplateArgumentLoc>();
4071 return cast<UnresolvedMemberExpr>(this)
4072 ->getTrailingObjects<TemplateArgumentLoc>();
4073}
4074
4076 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4077 return ULE->getNamingClass();
4078 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
4079}
4080
4081/// Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
4082///
4083/// The noexcept expression tests whether a given expression might throw. Its
4084/// result is a boolean constant.
4085class CXXNoexceptExpr : public Expr {
4086 friend class ASTStmtReader;
4087
4088 Stmt *Operand;
4089 SourceRange Range;
4090
4091public:
4093 SourceLocation Keyword, SourceLocation RParen)
4094 : Expr(CXXNoexceptExprClass, Ty, VK_PRValue, OK_Ordinary),
4095 Operand(Operand), Range(Keyword, RParen) {
4096 CXXNoexceptExprBits.Value = Val == CT_Cannot;
4097 setDependence(computeDependence(this, Val));
4098 }
4099
4100 CXXNoexceptExpr(EmptyShell Empty) : Expr(CXXNoexceptExprClass, Empty) {}
4101
4102 Expr *getOperand() const { return static_cast<Expr *>(Operand); }
4103
4104 SourceLocation getBeginLoc() const { return Range.getBegin(); }
4105 SourceLocation getEndLoc() const { return Range.getEnd(); }
4106 SourceRange getSourceRange() const { return Range; }
4107
4108 bool getValue() const { return CXXNoexceptExprBits.Value; }
4109
4110 static bool classof(const Stmt *T) {
4111 return T->getStmtClass() == CXXNoexceptExprClass;
4112 }
4113
4114 // Iterators
4115 child_range children() { return child_range(&Operand, &Operand + 1); }
4116
4118 return const_child_range(&Operand, &Operand + 1);
4119 }
4120};
4121
4122/// Represents a C++11 pack expansion that produces a sequence of
4123/// expressions.
4124///
4125/// A pack expansion expression contains a pattern (which itself is an
4126/// expression) followed by an ellipsis. For example:
4127///
4128/// \code
4129/// template<typename F, typename ...Types>
4130/// void forward(F f, Types &&...args) {
4131/// f(static_cast<Types&&>(args)...);
4132/// }
4133/// \endcode
4134///
4135/// Here, the argument to the function object \c f is a pack expansion whose
4136/// pattern is \c static_cast<Types&&>(args). When the \c forward function
4137/// template is instantiated, the pack expansion will instantiate to zero or
4138/// or more function arguments to the function object \c f.
4139class PackExpansionExpr : public Expr {
4140 friend class ASTStmtReader;
4141 friend class ASTStmtWriter;
4142
4143 SourceLocation EllipsisLoc;
4144
4145 /// The number of expansions that will be produced by this pack
4146 /// expansion expression, if known.
4147 ///
4148 /// When zero, the number of expansions is not known. Otherwise, this value
4149 /// is the number of expansions + 1.
4150 unsigned NumExpansions;
4151
4152 Stmt *Pattern;
4153
4154public:
4156 std::optional<unsigned> NumExpansions)
4157 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
4158 Pattern->getObjectKind()),
4159 EllipsisLoc(EllipsisLoc),
4160 NumExpansions(NumExpansions ? *NumExpansions + 1 : 0),
4161 Pattern(Pattern) {
4163 }
4164
4165 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) {}
4166
4167 /// Retrieve the pattern of the pack expansion.
4168 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); }
4169
4170 /// Retrieve the pattern of the pack expansion.
4171 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); }
4172
4173 /// Retrieve the location of the ellipsis that describes this pack
4174 /// expansion.
4175 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
4176
4177 /// Determine the number of expansions that will be produced when
4178 /// this pack expansion is instantiated, if already known.
4179 std::optional<unsigned> getNumExpansions() const {
4180 if (NumExpansions)
4181 return NumExpansions - 1;
4182
4183 return std::nullopt;
4184 }
4185
4186 SourceLocation getBeginLoc() const LLVM_READONLY {
4187 return Pattern->getBeginLoc();
4188 }
4189
4190 SourceLocation getEndLoc() const LLVM_READONLY { return EllipsisLoc; }
4191
4192 static bool classof(const Stmt *T) {
4193 return T->getStmtClass() == PackExpansionExprClass;
4194 }
4195
4196 // Iterators
4198 return child_range(&Pattern, &Pattern + 1);
4199 }
4200
4202 return const_child_range(&Pattern, &Pattern + 1);
4203 }
4204};
4205
4206/// Represents an expression that computes the length of a parameter
4207/// pack.
4208///
4209/// \code
4210/// template<typename ...Types>
4211/// struct count {
4212/// static const unsigned value = sizeof...(Types);
4213/// };
4214/// \endcode
4216 : public Expr,
4217 private llvm::TrailingObjects<SizeOfPackExpr, TemplateArgument> {
4218 friend class ASTStmtReader;
4219 friend class ASTStmtWriter;
4220 friend TrailingObjects;
4221
4222 /// The location of the \c sizeof keyword.
4223 SourceLocation OperatorLoc;
4224
4225 /// The location of the name of the parameter pack.
4226 SourceLocation PackLoc;
4227
4228 /// The location of the closing parenthesis.
4229 SourceLocation RParenLoc;
4230
4231 /// The length of the parameter pack, if known.
4232 ///
4233 /// When this expression is not value-dependent, this is the length of
4234 /// the pack. When the expression was parsed rather than instantiated
4235 /// (and thus is value-dependent), this is zero.
4236 ///
4237 /// After partial substitution into a sizeof...(X) expression (for instance,
4238 /// within an alias template or during function template argument deduction),
4239 /// we store a trailing array of partially-substituted TemplateArguments,
4240 /// and this is the length of that array.
4241 unsigned Length;
4242
4243 /// The parameter pack.
4244 NamedDecl *Pack = nullptr;
4245
4246 /// Create an expression that computes the length of
4247 /// the given parameter pack.
4248 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack,
4249 SourceLocation PackLoc, SourceLocation RParenLoc,
4250 std::optional<unsigned> Length,
4251 ArrayRef<TemplateArgument> PartialArgs)
4252 : Expr(SizeOfPackExprClass, SizeType, VK_PRValue, OK_Ordinary),
4253 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc),
4254 Length(Length ? *Length : PartialArgs.size()), Pack(Pack) {
4255 assert((!Length || PartialArgs.empty()) &&
4256 "have partial args for non-dependent sizeof... expression");
4257 auto *Args = getTrailingObjects<TemplateArgument>();
4258 std::uninitialized_copy(PartialArgs.begin(), PartialArgs.end(), Args);
4259 setDependence(Length ? ExprDependence::None
4260 : ExprDependence::ValueInstantiation);
4261 }
4262
4263 /// Create an empty expression.
4264 SizeOfPackExpr(EmptyShell Empty, unsigned NumPartialArgs)
4265 : Expr(SizeOfPackExprClass, Empty), Length(NumPartialArgs) {}
4266
4267public:
4268 static SizeOfPackExpr *
4269 Create(ASTContext &Context, SourceLocation OperatorLoc, NamedDecl *Pack,
4270 SourceLocation PackLoc, SourceLocation RParenLoc,
4271 std::optional<unsigned> Length = std::nullopt,
4272 ArrayRef<TemplateArgument> PartialArgs = std::nullopt);
4273 static SizeOfPackExpr *CreateDeserialized(ASTContext &Context,
4274 unsigned NumPartialArgs);
4275
4276 /// Determine the location of the 'sizeof' keyword.
4277 SourceLocation getOperatorLoc() const { return OperatorLoc; }
4278
4279 /// Determine the location of the parameter pack.
4280 SourceLocation getPackLoc() const { return PackLoc; }
4281
4282 /// Determine the location of the right parenthesis.
4283 SourceLocation getRParenLoc() const { return RParenLoc; }
4284
4285 /// Retrieve the parameter pack.
4286 NamedDecl *getPack() const { return Pack; }
4287
4288 /// Retrieve the length of the parameter pack.
4289 ///
4290 /// This routine may only be invoked when the expression is not
4291 /// value-dependent.
4292 unsigned getPackLength() const {
4293 assert(!isValueDependent() &&
4294 "Cannot get the length of a value-dependent pack size expression");
4295 return Length;
4296 }
4297
4298 /// Determine whether this represents a partially-substituted sizeof...
4299 /// expression, such as is produced for:
4300 ///
4301 /// template<typename ...Ts> using X = int[sizeof...(Ts)];
4302 /// template<typename ...Us> void f(X<Us..., 1, 2, 3, Us...>);
4304 return isValueDependent() && Length;
4305 }
4306
4307 /// Get
4309 assert(isPartiallySubstituted());
4310 const auto *Args = getTrailingObjects<TemplateArgument>();
4311 return llvm::ArrayRef(Args, Args + Length);
4312 }
4313
4314 SourceLocation getBeginLoc() const LLVM_READONLY { return OperatorLoc; }
4315 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
4316
4317 static bool classof(const Stmt *T) {
4318 return T->getStmtClass() == SizeOfPackExprClass;
4319 }
4320
4321 // Iterators
4324 }
4325
4328 }
4329};
4330
4331/// Represents a reference to a non-type template parameter
4332/// that has been substituted with a template argument.
4334 friend class ASTReader;
4335 friend class ASTStmtReader;
4336
4337 /// The replacement expression.
4338 Stmt *Replacement;
4339
4340 /// The associated declaration and a flag indicating if it was a reference
4341 /// parameter. For class NTTPs, we can't determine that based on the value
4342 /// category alone.
4343 llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndRef;
4344
4345 unsigned Index : 15;
4346 unsigned PackIndex : 16;
4347
4349 : Expr(SubstNonTypeTemplateParmExprClass, Empty) {}
4350
4351public:
4353 SourceLocation Loc, Expr *Replacement,
4354 Decl *AssociatedDecl, unsigned Index,
4355 std::optional<unsigned> PackIndex, bool RefParam)
4356 : Expr(SubstNonTypeTemplateParmExprClass, Ty, ValueKind, OK_Ordinary),
4357 Replacement(Replacement),
4358 AssociatedDeclAndRef(AssociatedDecl, RefParam), Index(Index),
4359 PackIndex(PackIndex ? *PackIndex + 1 : 0) {
4360 assert(AssociatedDecl != nullptr);
4363 }
4364
4366 return SubstNonTypeTemplateParmExprBits.NameLoc;
4367 }
4370
4371 Expr *getReplacement() const { return cast<Expr>(Replacement); }
4372
4373 /// A template-like entity which owns the whole pattern being substituted.
4374 /// This will own a set of template parameters.
4375 Decl *getAssociatedDecl() const { return AssociatedDeclAndRef.getPointer(); }
4376
4377 /// Returns the index of the replaced parameter in the associated declaration.
4378 /// This should match the result of `getParameter()->getIndex()`.
4379 unsigned getIndex() const { return Index; }
4380
4381 std::optional<unsigned> getPackIndex() const {
4382 if (PackIndex == 0)
4383 return std::nullopt;
4384 return PackIndex - 1;
4385 }
4386
4388
4389 bool isReferenceParameter() const { return AssociatedDeclAndRef.getInt(); }
4390
4391 /// Determine the substituted type of the template parameter.
4392 QualType getParameterType(const ASTContext &Ctx) const;
4393
4394 static bool classof(const Stmt *s) {
4395 return s->getStmtClass() == SubstNonTypeTemplateParmExprClass;
4396 }
4397
4398 // Iterators
4399 child_range children() { return child_range(&Replacement, &Replacement + 1); }
4400
4402 return const_child_range(&Replacement, &Replacement + 1);
4403 }
4404};
4405
4406/// Represents a reference to a non-type template parameter pack that
4407/// has been substituted with a non-template argument pack.
4408///
4409/// When a pack expansion in the source code contains multiple parameter packs
4410/// and those parameter packs correspond to different levels of template
4411/// parameter lists, this node is used to represent a non-type template
4412/// parameter pack from an outer level, which has already had its argument pack
4413/// substituted but that still lives within a pack expansion that itself
4414/// could not be instantiated. When actually performing a substitution into
4415/// that pack expansion (e.g., when all template parameters have corresponding
4416/// arguments), this type will be replaced with the appropriate underlying
4417/// expression at the current pack substitution index.
4419 friend class ASTReader;
4420 friend class ASTStmtReader;
4421
4422 /// The non-type template parameter pack itself.
4423 Decl *AssociatedDecl;
4424
4425 /// A pointer to the set of template arguments that this
4426 /// parameter pack is instantiated with.
4427 const TemplateArgument *Arguments;
4428
4429 /// The number of template arguments in \c Arguments.
4430 unsigned NumArguments : 16;
4431
4432 unsigned Index : 16;
4433
4434 /// The location of the non-type template parameter pack reference.
4435 SourceLocation NameLoc;
4436
4438 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) {}
4439
4440public:
4442 SourceLocation NameLoc,
4443 const TemplateArgument &ArgPack,
4444 Decl *AssociatedDecl, unsigned Index);
4445
4446 /// A template-like entity which owns the whole pattern being substituted.
4447 /// This will own a set of template parameters.
4448 Decl *getAssociatedDecl() const { return AssociatedDecl; }
4449
4450 /// Returns the index of the replaced parameter in the associated declaration.
4451 /// This should match the result of `getParameterPack()->getIndex()`.
4452 unsigned getIndex() const { return Index; }
4453
4454 /// Retrieve the non-type template parameter pack being substituted.
4456
4457 /// Retrieve the location of the parameter pack name.
4458 SourceLocation getParameterPackLocation() const { return NameLoc; }
4459
4460 /// Retrieve the template argument pack containing the substituted
4461 /// template arguments.
4463
4464 SourceLocation getBeginLoc() const LLVM_READONLY { return NameLoc; }
4465 SourceLocation getEndLoc() const LLVM_READONLY { return NameLoc; }
4466
4467 static bool classof(const Stmt *T) {
4468 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass;
4469 }
4470
4471 // Iterators
4474 }
4475
4478 }
4479};
4480
4481/// Represents a reference to a function parameter pack or init-capture pack
4482/// that has been substituted but not yet expanded.
4483///
4484/// When a pack expansion contains multiple parameter packs at different levels,
4485/// this node is used to represent a function parameter pack at an outer level
4486/// which we have already substituted to refer to expanded parameters, but where
4487/// the containing pack expansion cannot yet be expanded.
4488///
4489/// \code
4490/// template<typename...Ts> struct S {
4491/// template<typename...Us> auto f(Ts ...ts) -> decltype(g(Us(ts)...));
4492/// };
4493/// template struct S<int, int>;
4494/// \endcode
4496 : public Expr,
4497 private llvm::TrailingObjects<FunctionParmPackExpr, VarDecl *> {
4498 friend class ASTReader;
4499 friend class ASTStmtReader;
4500 friend TrailingObjects;
4501
4502 /// The function parameter pack which was referenced.
4503 VarDecl *ParamPack;
4504
4505 /// The location of the function parameter pack reference.
4506 SourceLocation NameLoc;
4507
4508 /// The number of expansions of this pack.
45