clang 19.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.CapturedByCopyInLambdaWithExplicitObjectParameter = false;
1153 CXXThisExprBits.Loc = L;
1155 }
1156
1157 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {}
1158
1159public:
1160 static CXXThisExpr *Create(const ASTContext &Ctx, SourceLocation L,
1161 QualType Ty, bool IsImplicit);
1162
1163 static CXXThisExpr *CreateEmpty(const ASTContext &Ctx);
1164
1167
1170
1171 bool isImplicit() const { return CXXThisExprBits.IsImplicit; }
1172 void setImplicit(bool I) { CXXThisExprBits.IsImplicit = I; }
1173
1175 return CXXThisExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter;
1176 }
1177
1179 CXXThisExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = Set;
1181 }
1182
1183 static bool classof(const Stmt *T) {
1184 return T->getStmtClass() == CXXThisExprClass;
1185 }
1186
1187 // Iterators
1190 }
1191
1194 }
1195};
1196
1197/// A C++ throw-expression (C++ [except.throw]).
1198///
1199/// This handles 'throw' (for re-throwing the current exception) and
1200/// 'throw' assignment-expression. When assignment-expression isn't
1201/// present, Op will be null.
1202class CXXThrowExpr : public Expr {
1203 friend class ASTStmtReader;
1204
1205 /// The optional expression in the throw statement.
1206 Stmt *Operand;
1207
1208public:
1209 // \p Ty is the void type which is used as the result type of the
1210 // expression. The \p Loc is the location of the throw keyword.
1211 // \p Operand is the expression in the throw statement, and can be
1212 // null if not present.
1214 bool IsThrownVariableInScope)
1215 : Expr(CXXThrowExprClass, Ty, VK_PRValue, OK_Ordinary), Operand(Operand) {
1216 CXXThrowExprBits.ThrowLoc = Loc;
1217 CXXThrowExprBits.IsThrownVariableInScope = IsThrownVariableInScope;
1219 }
1220 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {}
1221
1222 const Expr *getSubExpr() const { return cast_or_null<Expr>(Operand); }
1223 Expr *getSubExpr() { return cast_or_null<Expr>(Operand); }
1224
1225 SourceLocation getThrowLoc() const { return CXXThrowExprBits.ThrowLoc; }
1226
1227 /// Determines whether the variable thrown by this expression (if any!)
1228 /// is within the innermost try block.
1229 ///
1230 /// This information is required to determine whether the NRVO can apply to
1231 /// this variable.
1233 return CXXThrowExprBits.IsThrownVariableInScope;
1234 }
1235
1237 SourceLocation getEndLoc() const LLVM_READONLY {
1238 if (!getSubExpr())
1239 return getThrowLoc();
1240 return getSubExpr()->getEndLoc();
1241 }
1242
1243 static bool classof(const Stmt *T) {
1244 return T->getStmtClass() == CXXThrowExprClass;
1245 }
1246
1247 // Iterators
1249 return child_range(&Operand, Operand ? &Operand + 1 : &Operand);
1250 }
1251
1253 return const_child_range(&Operand, Operand ? &Operand + 1 : &Operand);
1254 }
1255};
1256
1257/// A default argument (C++ [dcl.fct.default]).
1258///
1259/// This wraps up a function call argument that was created from the
1260/// corresponding parameter's default argument, when the call did not
1261/// explicitly supply arguments for all of the parameters.
1263 : public Expr,
1264 private llvm::TrailingObjects<CXXDefaultArgExpr, Expr *> {
1265 friend class ASTStmtReader;
1266 friend class ASTReader;
1267 friend TrailingObjects;
1268
1269 /// The parameter whose default is being used.
1270 ParmVarDecl *Param;
1271
1272 /// The context where the default argument expression was used.
1273 DeclContext *UsedContext;
1274
1276 Expr *RewrittenExpr, DeclContext *UsedContext)
1277 : Expr(SC,
1278 Param->hasUnparsedDefaultArg()
1279 ? Param->getType().getNonReferenceType()
1280 : Param->getDefaultArg()->getType(),
1281 Param->getDefaultArg()->getValueKind(),
1282 Param->getDefaultArg()->getObjectKind()),
1283 Param(Param), UsedContext(UsedContext) {
1284 CXXDefaultArgExprBits.Loc = Loc;
1285 CXXDefaultArgExprBits.HasRewrittenInit = RewrittenExpr != nullptr;
1286 if (RewrittenExpr)
1287 *getTrailingObjects<Expr *>() = RewrittenExpr;
1289 }
1290
1291 CXXDefaultArgExpr(EmptyShell Empty, bool HasRewrittenInit)
1292 : Expr(CXXDefaultArgExprClass, Empty) {
1293 CXXDefaultArgExprBits.HasRewrittenInit = HasRewrittenInit;
1294 }
1295
1296public:
1297 static CXXDefaultArgExpr *CreateEmpty(const ASTContext &C,
1298 bool HasRewrittenInit);
1299
1300 // \p Param is the parameter whose default argument is used by this
1301 // expression.
1302 static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc,
1303 ParmVarDecl *Param, Expr *RewrittenExpr,
1304 DeclContext *UsedContext);
1305 // Retrieve the parameter that the argument was created from.
1306 const ParmVarDecl *getParam() const { return Param; }
1307 ParmVarDecl *getParam() { return Param; }
1308
1309 bool hasRewrittenInit() const {
1310 return CXXDefaultArgExprBits.HasRewrittenInit;
1311 }
1312
1313 // Retrieve the argument to the function call.
1314 Expr *getExpr();
1315 const Expr *getExpr() const {
1316 return const_cast<CXXDefaultArgExpr *>(this)->getExpr();
1317 }
1318
1320 return hasRewrittenInit() ? *getTrailingObjects<Expr *>() : nullptr;
1321 }
1322
1323 const Expr *getRewrittenExpr() const {
1324 return const_cast<CXXDefaultArgExpr *>(this)->getRewrittenExpr();
1325 }
1326
1327 // Retrieve the rewritten init expression (for an init expression containing
1328 // immediate calls) with the top level FullExpr and ConstantExpr stripped off.
1331 return const_cast<CXXDefaultArgExpr *>(this)->getAdjustedRewrittenExpr();
1332 }
1333
1334 const DeclContext *getUsedContext() const { return UsedContext; }
1335 DeclContext *getUsedContext() { return UsedContext; }
1336
1337 /// Retrieve the location where this default argument was actually used.
1339
1340 /// Default argument expressions have no representation in the
1341 /// source, so they have an empty source range.
1344
1346
1347 static bool classof(const Stmt *T) {
1348 return T->getStmtClass() == CXXDefaultArgExprClass;
1349 }
1350
1351 // Iterators
1354 }
1355
1358 }
1359};
1360
1361/// A use of a default initializer in a constructor or in aggregate
1362/// initialization.
1363///
1364/// This wraps a use of a C++ default initializer (technically,
1365/// a brace-or-equal-initializer for a non-static data member) when it
1366/// is implicitly used in a mem-initializer-list in a constructor
1367/// (C++11 [class.base.init]p8) or in aggregate initialization
1368/// (C++1y [dcl.init.aggr]p7).
1370 : public Expr,
1371 private llvm::TrailingObjects<CXXDefaultInitExpr, Expr *> {
1372
1373 friend class ASTStmtReader;
1374 friend class ASTReader;
1375 friend TrailingObjects;
1376 /// The field whose default is being used.
1377 FieldDecl *Field;
1378
1379 /// The context where the default initializer expression was used.
1380 DeclContext *UsedContext;
1381
1383 FieldDecl *Field, QualType Ty, DeclContext *UsedContext,
1384 Expr *RewrittenInitExpr);
1385
1386 CXXDefaultInitExpr(EmptyShell Empty, bool HasRewrittenInit)
1387 : Expr(CXXDefaultInitExprClass, Empty) {
1388 CXXDefaultInitExprBits.HasRewrittenInit = HasRewrittenInit;
1389 }
1390
1391public:
1393 bool HasRewrittenInit);
1394 /// \p Field is the non-static data member whose default initializer is used
1395 /// by this expression.
1396 static CXXDefaultInitExpr *Create(const ASTContext &Ctx, SourceLocation Loc,
1397 FieldDecl *Field, DeclContext *UsedContext,
1398 Expr *RewrittenInitExpr);
1399
1400 bool hasRewrittenInit() const {
1401 return CXXDefaultInitExprBits.HasRewrittenInit;
1402 }
1403
1404 /// Get the field whose initializer will be used.
1405 FieldDecl *getField() { return Field; }
1406 const FieldDecl *getField() const { return Field; }
1407
1408 /// Get the initialization expression that will be used.
1409 Expr *getExpr();
1410 const Expr *getExpr() const {
1411 return const_cast<CXXDefaultInitExpr *>(this)->getExpr();
1412 }
1413
1414 /// Retrieve the initializing expression with evaluated immediate calls, if
1415 /// any.
1416 const Expr *getRewrittenExpr() const {
1417 assert(hasRewrittenInit() && "expected a rewritten init expression");
1418 return *getTrailingObjects<Expr *>();
1419 }
1420
1421 /// Retrieve the initializing expression with evaluated immediate calls, if
1422 /// any.
1424 assert(hasRewrittenInit() && "expected a rewritten init expression");
1425 return *getTrailingObjects<Expr *>();
1426 }
1427
1428 const DeclContext *getUsedContext() const { return UsedContext; }
1429 DeclContext *getUsedContext() { return UsedContext; }
1430
1431 /// Retrieve the location where this default initializer expression was
1432 /// actually used.
1434
1437
1438 static bool classof(const Stmt *T) {
1439 return T->getStmtClass() == CXXDefaultInitExprClass;
1440 }
1441
1442 // Iterators
1445 }
1446
1449 }
1450};
1451
1452/// Represents a C++ temporary.
1454 /// The destructor that needs to be called.
1455 const CXXDestructorDecl *Destructor;
1456
1457 explicit CXXTemporary(const CXXDestructorDecl *destructor)
1458 : Destructor(destructor) {}
1459
1460public:
1461 static CXXTemporary *Create(const ASTContext &C,
1462 const CXXDestructorDecl *Destructor);
1463
1464 const CXXDestructorDecl *getDestructor() const { return Destructor; }
1465
1467 Destructor = Dtor;
1468 }
1469};
1470
1471/// Represents binding an expression to a temporary.
1472///
1473/// This ensures the destructor is called for the temporary. It should only be
1474/// needed for non-POD, non-trivially destructable class types. For example:
1475///
1476/// \code
1477/// struct S {
1478/// S() { } // User defined constructor makes S non-POD.
1479/// ~S() { } // User defined destructor makes it non-trivial.
1480/// };
1481/// void test() {
1482/// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr.
1483/// }
1484/// \endcode
1486 CXXTemporary *Temp = nullptr;
1487 Stmt *SubExpr = nullptr;
1488
1489 CXXBindTemporaryExpr(CXXTemporary *temp, Expr *SubExpr)
1490 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(), VK_PRValue,
1491 OK_Ordinary),
1492 Temp(temp), SubExpr(SubExpr) {
1494 }
1495
1496public:
1498 : Expr(CXXBindTemporaryExprClass, Empty) {}
1499
1500 static CXXBindTemporaryExpr *Create(const ASTContext &C, CXXTemporary *Temp,
1501 Expr* SubExpr);
1502
1503 CXXTemporary *getTemporary() { return Temp; }
1504 const CXXTemporary *getTemporary() const { return Temp; }
1505 void setTemporary(CXXTemporary *T) { Temp = T; }
1506
1507 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
1508 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
1509 void setSubExpr(Expr *E) { SubExpr = E; }
1510
1511 SourceLocation getBeginLoc() const LLVM_READONLY {
1512 return SubExpr->getBeginLoc();
1513 }
1514
1515 SourceLocation getEndLoc() const LLVM_READONLY {
1516 return SubExpr->getEndLoc();
1517 }
1518
1519 // Implement isa/cast/dyncast/etc.
1520 static bool classof(const Stmt *T) {
1521 return T->getStmtClass() == CXXBindTemporaryExprClass;
1522 }
1523
1524 // Iterators
1525 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
1526
1528 return const_child_range(&SubExpr, &SubExpr + 1);
1529 }
1530};
1531
1533 Complete,
1537};
1538
1539/// Represents a call to a C++ constructor.
1540class CXXConstructExpr : public Expr {
1541 friend class ASTStmtReader;
1542
1543 /// A pointer to the constructor which will be ultimately called.
1544 CXXConstructorDecl *Constructor;
1545
1546 SourceRange ParenOrBraceRange;
1547
1548 /// The number of arguments.
1549 unsigned NumArgs;
1550
1551 // We would like to stash the arguments of the constructor call after
1552 // CXXConstructExpr. However CXXConstructExpr is used as a base class of
1553 // CXXTemporaryObjectExpr which makes the use of llvm::TrailingObjects
1554 // impossible.
1555 //
1556 // Instead we manually stash the trailing object after the full object
1557 // containing CXXConstructExpr (that is either CXXConstructExpr or
1558 // CXXTemporaryObjectExpr).
1559 //
1560 // The trailing objects are:
1561 //
1562 // * An array of getNumArgs() "Stmt *" for the arguments of the
1563 // constructor call.
1564
1565 /// Return a pointer to the start of the trailing arguments.
1566 /// Defined just after CXXTemporaryObjectExpr.
1567 inline Stmt **getTrailingArgs();
1568 const Stmt *const *getTrailingArgs() const {
1569 return const_cast<CXXConstructExpr *>(this)->getTrailingArgs();
1570 }
1571
1572protected:
1573 /// Build a C++ construction expression.
1575 CXXConstructorDecl *Ctor, bool Elidable,
1576 ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1577 bool ListInitialization, bool StdInitListInitialization,
1578 bool ZeroInitialization, CXXConstructionKind ConstructKind,
1579 SourceRange ParenOrBraceRange);
1580
1581 /// Build an empty C++ construction expression.
1582 CXXConstructExpr(StmtClass SC, EmptyShell Empty, unsigned NumArgs);
1583
1584 /// Return the size in bytes of the trailing objects. Used by
1585 /// CXXTemporaryObjectExpr to allocate the right amount of storage.
1586 static unsigned sizeOfTrailingObjects(unsigned NumArgs) {
1587 return NumArgs * sizeof(Stmt *);
1588 }
1589
1590public:
1591 /// Create a C++ construction expression.
1592 static CXXConstructExpr *
1593 Create(const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
1594 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
1595 bool HadMultipleCandidates, bool ListInitialization,
1596 bool StdInitListInitialization, bool ZeroInitialization,
1597 CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange);
1598
1599 /// Create an empty C++ construction expression.
1600 static CXXConstructExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs);
1601
1602 /// Get the constructor that this expression will (ultimately) call.
1603 CXXConstructorDecl *getConstructor() const { return Constructor; }
1604
1607
1608 /// Whether this construction is elidable.
1609 bool isElidable() const { return CXXConstructExprBits.Elidable; }
1610 void setElidable(bool E) { CXXConstructExprBits.Elidable = E; }
1611
1612 /// Whether the referred constructor was resolved from
1613 /// an overloaded set having size greater than 1.
1615 return CXXConstructExprBits.HadMultipleCandidates;
1616 }
1618 CXXConstructExprBits.HadMultipleCandidates = V;
1619 }
1620
1621 /// Whether this constructor call was written as list-initialization.
1623 return CXXConstructExprBits.ListInitialization;
1624 }
1626 CXXConstructExprBits.ListInitialization = V;
1627 }
1628
1629 /// Whether this constructor call was written as list-initialization,
1630 /// but was interpreted as forming a std::initializer_list<T> from the list
1631 /// and passing that as a single constructor argument.
1632 /// See C++11 [over.match.list]p1 bullet 1.
1634 return CXXConstructExprBits.StdInitListInitialization;
1635 }
1637 CXXConstructExprBits.StdInitListInitialization = V;
1638 }
1639
1640 /// Whether this construction first requires
1641 /// zero-initialization before the initializer is called.
1643 return CXXConstructExprBits.ZeroInitialization;
1644 }
1645 void setRequiresZeroInitialization(bool ZeroInit) {
1646 CXXConstructExprBits.ZeroInitialization = ZeroInit;
1647 }
1648
1649 /// Determine whether this constructor is actually constructing
1650 /// a base class (rather than a complete object).
1652 return static_cast<CXXConstructionKind>(
1653 CXXConstructExprBits.ConstructionKind);
1654 }
1656 CXXConstructExprBits.ConstructionKind = llvm::to_underlying(CK);
1657 }
1658
1661 using arg_range = llvm::iterator_range<arg_iterator>;
1662 using const_arg_range = llvm::iterator_range<const_arg_iterator>;
1663
1666 return const_arg_range(arg_begin(), arg_end());
1667 }
1668
1669 arg_iterator arg_begin() { return getTrailingArgs(); }
1671 const_arg_iterator arg_begin() const { return getTrailingArgs(); }
1673
1674 Expr **getArgs() { return reinterpret_cast<Expr **>(getTrailingArgs()); }
1675 const Expr *const *getArgs() const {
1676 return reinterpret_cast<const Expr *const *>(getTrailingArgs());
1677 }
1678
1679 /// Return the number of arguments to the constructor call.
1680 unsigned getNumArgs() const { return NumArgs; }
1681
1682 /// Return the specified argument.
1683 Expr *getArg(unsigned Arg) {
1684 assert(Arg < getNumArgs() && "Arg access out of range!");
1685 return getArgs()[Arg];
1686 }
1687 const Expr *getArg(unsigned Arg) const {
1688 assert(Arg < getNumArgs() && "Arg access out of range!");
1689 return getArgs()[Arg];
1690 }
1691
1692 /// Set the specified argument.
1693 void setArg(unsigned Arg, Expr *ArgExpr) {
1694 assert(Arg < getNumArgs() && "Arg access out of range!");
1695 getArgs()[Arg] = ArgExpr;
1696 }
1697
1699 return CXXConstructExprBits.IsImmediateEscalating;
1700 }
1701
1703 CXXConstructExprBits.IsImmediateEscalating = Set;
1704 }
1705
1706 SourceLocation getBeginLoc() const LLVM_READONLY;
1707 SourceLocation getEndLoc() const LLVM_READONLY;
1708 SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
1709 void setParenOrBraceRange(SourceRange Range) { ParenOrBraceRange = Range; }
1710
1711 static bool classof(const Stmt *T) {
1712 return T->getStmtClass() == CXXConstructExprClass ||
1713 T->getStmtClass() == CXXTemporaryObjectExprClass;
1714 }
1715
1716 // Iterators
1718 return child_range(getTrailingArgs(), getTrailingArgs() + getNumArgs());
1719 }
1720
1722 auto Children = const_cast<CXXConstructExpr *>(this)->children();
1723 return const_child_range(Children.begin(), Children.end());
1724 }
1725};
1726
1727/// Represents a call to an inherited base class constructor from an
1728/// inheriting constructor. This call implicitly forwards the arguments from
1729/// the enclosing context (an inheriting constructor) to the specified inherited
1730/// base class constructor.
1732private:
1733 CXXConstructorDecl *Constructor = nullptr;
1734
1735 /// The location of the using declaration.
1736 SourceLocation Loc;
1737
1738 /// Whether this is the construction of a virtual base.
1739 LLVM_PREFERRED_TYPE(bool)
1740 unsigned ConstructsVirtualBase : 1;
1741
1742 /// Whether the constructor is inherited from a virtual base class of the
1743 /// class that we construct.
1744 LLVM_PREFERRED_TYPE(bool)
1745 unsigned InheritedFromVirtualBase : 1;
1746
1747public:
1748 friend class ASTStmtReader;
1749
1750 /// Construct a C++ inheriting construction expression.
1752 CXXConstructorDecl *Ctor, bool ConstructsVirtualBase,
1753 bool InheritedFromVirtualBase)
1754 : Expr(CXXInheritedCtorInitExprClass, T, VK_PRValue, OK_Ordinary),
1755 Constructor(Ctor), Loc(Loc),
1756 ConstructsVirtualBase(ConstructsVirtualBase),
1757 InheritedFromVirtualBase(InheritedFromVirtualBase) {
1758 assert(!T->isDependentType());
1759 setDependence(ExprDependence::None);
1760 }
1761
1762 /// Construct an empty C++ inheriting construction expression.
1764 : Expr(CXXInheritedCtorInitExprClass, Empty),
1765 ConstructsVirtualBase(false), InheritedFromVirtualBase(false) {}
1766
1767 /// Get the constructor that this expression will call.
1768 CXXConstructorDecl *getConstructor() const { return Constructor; }
1769
1770 /// Determine whether this constructor is actually constructing
1771 /// a base class (rather than a complete object).
1772 bool constructsVBase() const { return ConstructsVirtualBase; }
1774 return ConstructsVirtualBase ? CXXConstructionKind::VirtualBase
1776 }
1777
1778 /// Determine whether the inherited constructor is inherited from a
1779 /// virtual base of the object we construct. If so, we are not responsible
1780 /// for calling the inherited constructor (the complete object constructor
1781 /// does that), and so we don't need to pass any arguments.
1782 bool inheritedFromVBase() const { return InheritedFromVirtualBase; }
1783
1784 SourceLocation getLocation() const LLVM_READONLY { return Loc; }
1785 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
1786 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
1787
1788 static bool classof(const Stmt *T) {
1789 return T->getStmtClass() == CXXInheritedCtorInitExprClass;
1790 }
1791
1794 }
1795
1798 }
1799};
1800
1801/// Represents an explicit C++ type conversion that uses "functional"
1802/// notation (C++ [expr.type.conv]).
1803///
1804/// Example:
1805/// \code
1806/// x = int(0.5);
1807/// \endcode
1809 : public ExplicitCastExpr,
1810 private llvm::TrailingObjects<CXXFunctionalCastExpr, CXXBaseSpecifier *,
1811 FPOptionsOverride> {
1812 SourceLocation LParenLoc;
1813 SourceLocation RParenLoc;
1814
1816 TypeSourceInfo *writtenTy, CastKind kind,
1817 Expr *castExpr, unsigned pathSize,
1818 FPOptionsOverride FPO, SourceLocation lParenLoc,
1819 SourceLocation rParenLoc)
1820 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, castExpr,
1821 pathSize, FPO.requiresTrailingStorage(), writtenTy),
1822 LParenLoc(lParenLoc), RParenLoc(rParenLoc) {
1823 if (hasStoredFPFeatures())
1824 *getTrailingFPFeatures() = FPO;
1825 }
1826
1827 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize,
1828 bool HasFPFeatures)
1829 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize,
1830 HasFPFeatures) {}
1831
1832 unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const {
1833 return path_size();
1834 }
1835
1836public:
1837 friend class CastExpr;
1839
1840 static CXXFunctionalCastExpr *
1841 Create(const ASTContext &Context, QualType T, ExprValueKind VK,
1842 TypeSourceInfo *Written, CastKind Kind, Expr *Op,
1843 const CXXCastPath *Path, FPOptionsOverride FPO, SourceLocation LPLoc,
1844 SourceLocation RPLoc);
1845 static CXXFunctionalCastExpr *
1846 CreateEmpty(const ASTContext &Context, unsigned PathSize, bool HasFPFeatures);
1847
1848 SourceLocation getLParenLoc() const { return LParenLoc; }
1849 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1850 SourceLocation getRParenLoc() const { return RParenLoc; }
1851 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1852
1853 /// Determine whether this expression models list-initialization.
1854 bool isListInitialization() const { return LParenLoc.isInvalid(); }
1855
1856 SourceLocation getBeginLoc() const LLVM_READONLY;
1857 SourceLocation getEndLoc() const LLVM_READONLY;
1858
1859 static bool classof(const Stmt *T) {
1860 return T->getStmtClass() == CXXFunctionalCastExprClass;
1861 }
1862};
1863
1864/// Represents a C++ functional cast expression that builds a
1865/// temporary object.
1866///
1867/// This expression type represents a C++ "functional" cast
1868/// (C++[expr.type.conv]) with N != 1 arguments that invokes a
1869/// constructor to build a temporary object. With N == 1 arguments the
1870/// functional cast expression will be represented by CXXFunctionalCastExpr.
1871/// Example:
1872/// \code
1873/// struct X { X(int, float); }
1874///
1875/// X create_X() {
1876/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
1877/// };
1878/// \endcode
1880 friend class ASTStmtReader;
1881
1882 // CXXTemporaryObjectExpr has some trailing objects belonging
1883 // to CXXConstructExpr. See the comment inside CXXConstructExpr
1884 // for more details.
1885
1886 TypeSourceInfo *TSI;
1887
1890 SourceRange ParenOrBraceRange,
1891 bool HadMultipleCandidates, bool ListInitialization,
1892 bool StdInitListInitialization,
1893 bool ZeroInitialization);
1894
1895 CXXTemporaryObjectExpr(EmptyShell Empty, unsigned NumArgs);
1896
1897public:
1898 static CXXTemporaryObjectExpr *
1899 Create(const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
1901 SourceRange ParenOrBraceRange, bool HadMultipleCandidates,
1902 bool ListInitialization, bool StdInitListInitialization,
1903 bool ZeroInitialization);
1904
1906 unsigned NumArgs);
1907
1908 TypeSourceInfo *getTypeSourceInfo() const { return TSI; }
1909
1910 SourceLocation getBeginLoc() const LLVM_READONLY;
1911 SourceLocation getEndLoc() const LLVM_READONLY;
1912
1913 static bool classof(const Stmt *T) {
1914 return T->getStmtClass() == CXXTemporaryObjectExprClass;
1915 }
1916};
1917
1918Stmt **CXXConstructExpr::getTrailingArgs() {
1919 if (auto *E = dyn_cast<CXXTemporaryObjectExpr>(this))
1920 return reinterpret_cast<Stmt **>(E + 1);
1921 assert((getStmtClass() == CXXConstructExprClass) &&
1922 "Unexpected class deriving from CXXConstructExpr!");
1923 return reinterpret_cast<Stmt **>(this + 1);
1924}
1925
1926/// A C++ lambda expression, which produces a function object
1927/// (of unspecified type) that can be invoked later.
1928///
1929/// Example:
1930/// \code
1931/// void low_pass_filter(std::vector<double> &values, double cutoff) {
1932/// values.erase(std::remove_if(values.begin(), values.end(),
1933/// [=](double value) { return value > cutoff; });
1934/// }
1935/// \endcode
1936///
1937/// C++11 lambda expressions can capture local variables, either by copying
1938/// the values of those local variables at the time the function
1939/// object is constructed (not when it is called!) or by holding a
1940/// reference to the local variable. These captures can occur either
1941/// implicitly or can be written explicitly between the square
1942/// brackets ([...]) that start the lambda expression.
1943///
1944/// C++1y introduces a new form of "capture" called an init-capture that
1945/// includes an initializing expression (rather than capturing a variable),
1946/// and which can never occur implicitly.
1947class LambdaExpr final : public Expr,
1948 private llvm::TrailingObjects<LambdaExpr, Stmt *> {
1949 // LambdaExpr has some data stored in LambdaExprBits.
1950
1951 /// The source range that covers the lambda introducer ([...]).
1952 SourceRange IntroducerRange;
1953
1954 /// The source location of this lambda's capture-default ('=' or '&').
1955 SourceLocation CaptureDefaultLoc;
1956
1957 /// The location of the closing brace ('}') that completes
1958 /// the lambda.
1959 ///
1960 /// The location of the brace is also available by looking up the
1961 /// function call operator in the lambda class. However, it is
1962 /// stored here to improve the performance of getSourceRange(), and
1963 /// to avoid having to deserialize the function call operator from a
1964 /// module file just to determine the source range.
1965 SourceLocation ClosingBrace;
1966
1967 /// Construct a lambda expression.
1968 LambdaExpr(QualType T, SourceRange IntroducerRange,
1969 LambdaCaptureDefault CaptureDefault,
1970 SourceLocation CaptureDefaultLoc, bool ExplicitParams,
1971 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1972 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack);
1973
1974 /// Construct an empty lambda expression.
1975 LambdaExpr(EmptyShell Empty, unsigned NumCaptures);
1976
1977 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
1978 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
1979
1980 void initBodyIfNeeded() const;
1981
1982public:
1983 friend class ASTStmtReader;
1984 friend class ASTStmtWriter;
1986
1987 /// Construct a new lambda expression.
1988 static LambdaExpr *
1989 Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange,
1990 LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc,
1991 bool ExplicitParams, bool ExplicitResultType,
1992 ArrayRef<Expr *> CaptureInits, SourceLocation ClosingBrace,
1993 bool ContainsUnexpandedParameterPack);
1994
1995 /// Construct a new lambda expression that will be deserialized from
1996 /// an external source.
1998 unsigned NumCaptures);
1999
2000 /// Determine the default capture kind for this lambda.
2002 return static_cast<LambdaCaptureDefault>(LambdaExprBits.CaptureDefault);
2003 }
2004
2005 /// Retrieve the location of this lambda's capture-default, if any.
2006 SourceLocation getCaptureDefaultLoc() const { return CaptureDefaultLoc; }
2007
2008 /// Determine whether one of this lambda's captures is an init-capture.
2009 bool isInitCapture(const LambdaCapture *Capture) const;
2010
2011 /// An iterator that walks over the captures of the lambda,
2012 /// both implicit and explicit.
2014
2015 /// An iterator over a range of lambda captures.
2016 using capture_range = llvm::iterator_range<capture_iterator>;
2017
2018 /// Retrieve this lambda's captures.
2019 capture_range captures() const;
2020
2021 /// Retrieve an iterator pointing to the first lambda capture.
2023
2024 /// Retrieve an iterator pointing past the end of the
2025 /// sequence of lambda captures.
2027
2028 /// Determine the number of captures in this lambda.
2029 unsigned capture_size() const { return LambdaExprBits.NumCaptures; }
2030
2031 /// Retrieve this lambda's explicit captures.
2033
2034 /// Retrieve an iterator pointing to the first explicit
2035 /// lambda capture.
2037
2038 /// Retrieve an iterator pointing past the end of the sequence of
2039 /// explicit lambda captures.
2041
2042 /// Retrieve this lambda's implicit captures.
2044
2045 /// Retrieve an iterator pointing to the first implicit
2046 /// lambda capture.
2048
2049 /// Retrieve an iterator pointing past the end of the sequence of
2050 /// implicit lambda captures.
2052
2053 /// Iterator that walks over the capture initialization
2054 /// arguments.
2056
2057 /// Const iterator that walks over the capture initialization
2058 /// arguments.
2059 /// FIXME: This interface is prone to being used incorrectly.
2061
2062 /// Retrieve the initialization expressions for this lambda's captures.
2063 llvm::iterator_range<capture_init_iterator> capture_inits() {
2064 return llvm::make_range(capture_init_begin(), capture_init_end());
2065 }
2066
2067 /// Retrieve the initialization expressions for this lambda's captures.
2068 llvm::iterator_range<const_capture_init_iterator> capture_inits() const {
2069 return llvm::make_range(capture_init_begin(), capture_init_end());
2070 }
2071
2072 /// Retrieve the first initialization argument for this
2073 /// lambda expression (which initializes the first capture field).
2075 return reinterpret_cast<Expr **>(getStoredStmts());
2076 }
2077
2078 /// Retrieve the first initialization argument for this
2079 /// lambda expression (which initializes the first capture field).
2081 return reinterpret_cast<Expr *const *>(getStoredStmts());
2082 }
2083
2084 /// Retrieve the iterator pointing one past the last
2085 /// initialization argument for this lambda expression.
2087 return capture_init_begin() + capture_size();
2088 }
2089
2090 /// Retrieve the iterator pointing one past the last
2091 /// initialization argument for this lambda expression.
2093 return capture_init_begin() + capture_size();
2094 }
2095
2096 /// Retrieve the source range covering the lambda introducer,
2097 /// which contains the explicit capture list surrounded by square
2098 /// brackets ([...]).
2099 SourceRange getIntroducerRange() const { return IntroducerRange; }
2100
2101 /// Retrieve the class that corresponds to the lambda.
2102 ///
2103 /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the
2104 /// captures in its fields and provides the various operations permitted
2105 /// on a lambda (copying, calling).
2107
2108 /// Retrieve the function call operator associated with this
2109 /// lambda expression.
2111
2112 /// Retrieve the function template call operator associated with this
2113 /// lambda expression.
2115
2116 /// If this is a generic lambda expression, retrieve the template
2117 /// parameter list associated with it, or else return null.
2119
2120 /// Get the template parameters were explicitly specified (as opposed to being
2121 /// invented by use of an auto parameter).
2123
2124 /// Get the trailing requires clause, if any.
2126
2127 /// Whether this is a generic lambda.
2129
2130 /// Retrieve the body of the lambda. This will be most of the time
2131 /// a \p CompoundStmt, but can also be \p CoroutineBodyStmt wrapping
2132 /// a \p CompoundStmt. Note that unlike functions, lambda-expressions
2133 /// cannot have a function-try-block.
2134 Stmt *getBody() const;
2135
2136 /// Retrieve the \p CompoundStmt representing the body of the lambda.
2137 /// This is a convenience function for callers who do not need
2138 /// to handle node(s) which may wrap a \p CompoundStmt.
2139 const CompoundStmt *getCompoundStmtBody() const;
2141 const auto *ConstThis = this;
2142 return const_cast<CompoundStmt *>(ConstThis->getCompoundStmtBody());
2143 }
2144
2145 /// Determine whether the lambda is mutable, meaning that any
2146 /// captures values can be modified.
2147 bool isMutable() const;
2148
2149 /// Determine whether this lambda has an explicit parameter
2150 /// list vs. an implicit (empty) parameter list.
2151 bool hasExplicitParameters() const { return LambdaExprBits.ExplicitParams; }
2152
2153 /// Whether this lambda had its result type explicitly specified.
2155 return LambdaExprBits.ExplicitResultType;
2156 }
2157
2158 static bool classof(const Stmt *T) {
2159 return T->getStmtClass() == LambdaExprClass;
2160 }
2161
2162 SourceLocation getBeginLoc() const LLVM_READONLY {
2163 return IntroducerRange.getBegin();
2164 }
2165
2166 SourceLocation getEndLoc() const LLVM_READONLY { return ClosingBrace; }
2167
2168 /// Includes the captures and the body of the lambda.
2171};
2172
2173/// An expression "T()" which creates a value-initialized rvalue of type
2174/// T, which is a non-class type. See (C++98 [5.2.3p2]).
2176 friend class ASTStmtReader;
2177
2179
2180public:
2181 /// Create an explicitly-written scalar-value initialization
2182 /// expression.
2184 SourceLocation RParenLoc)
2185 : Expr(CXXScalarValueInitExprClass, Type, VK_PRValue, OK_Ordinary),
2187 CXXScalarValueInitExprBits.RParenLoc = RParenLoc;
2189 }
2190
2192 : Expr(CXXScalarValueInitExprClass, Shell) {}
2193
2195 return TypeInfo;
2196 }
2197
2199 return CXXScalarValueInitExprBits.RParenLoc;
2200 }
2201
2202 SourceLocation getBeginLoc() const LLVM_READONLY;
2204
2205 static bool classof(const Stmt *T) {
2206 return T->getStmtClass() == CXXScalarValueInitExprClass;
2207 }
2208
2209 // Iterators
2212 }
2213
2216 }
2217};
2218
2220 /// New-expression has no initializer as written.
2221 None,
2222
2223 /// New-expression has a C++98 paren-delimited initializer.
2224 Parens,
2225
2226 /// New-expression has a C++11 list-initializer.
2227 Braces
2228};
2229
2230/// Represents a new-expression for memory allocation and constructor
2231/// calls, e.g: "new CXXNewExpr(foo)".
2232class CXXNewExpr final
2233 : public Expr,
2234 private llvm::TrailingObjects<CXXNewExpr, Stmt *, SourceRange> {
2235 friend class ASTStmtReader;
2236 friend class ASTStmtWriter;
2237 friend TrailingObjects;
2238
2239 /// Points to the allocation function used.
2240 FunctionDecl *OperatorNew;
2241
2242 /// Points to the deallocation function used in case of error. May be null.
2243 FunctionDecl *OperatorDelete;
2244
2245 /// The allocated type-source information, as written in the source.
2246 TypeSourceInfo *AllocatedTypeInfo;
2247
2248 /// Range of the entire new expression.
2249 SourceRange Range;
2250
2251 /// Source-range of a paren-delimited initializer.
2252 SourceRange DirectInitRange;
2253
2254 // CXXNewExpr is followed by several optional trailing objects.
2255 // They are in order:
2256 //
2257 // * An optional "Stmt *" for the array size expression.
2258 // Present if and ony if isArray().
2259 //
2260 // * An optional "Stmt *" for the init expression.
2261 // Present if and only if hasInitializer().
2262 //
2263 // * An array of getNumPlacementArgs() "Stmt *" for the placement new
2264 // arguments, if any.
2265 //
2266 // * An optional SourceRange for the range covering the parenthesized type-id
2267 // if the allocated type was expressed as a parenthesized type-id.
2268 // Present if and only if isParenTypeId().
2269 unsigned arraySizeOffset() const { return 0; }
2270 unsigned initExprOffset() const { return arraySizeOffset() + isArray(); }
2271 unsigned placementNewArgsOffset() const {
2272 return initExprOffset() + hasInitializer();
2273 }
2274
2275 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2277 }
2278
2279 unsigned numTrailingObjects(OverloadToken<SourceRange>) const {
2280 return isParenTypeId();
2281 }
2282
2283 /// Build a c++ new expression.
2284 CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
2285 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
2286 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
2287 SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
2288 CXXNewInitializationStyle InitializationStyle, Expr *Initializer,
2289 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
2290 SourceRange DirectInitRange);
2291
2292 /// Build an empty c++ new expression.
2293 CXXNewExpr(EmptyShell Empty, bool IsArray, unsigned NumPlacementArgs,
2294 bool IsParenTypeId);
2295
2296public:
2297 /// Create a c++ new expression.
2298 static CXXNewExpr *
2299 Create(const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew,
2300 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
2301 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
2302 SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
2303 CXXNewInitializationStyle InitializationStyle, Expr *Initializer,
2304 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
2305 SourceRange DirectInitRange);
2306
2307 /// Create an empty c++ new expression.
2308 static CXXNewExpr *CreateEmpty(const ASTContext &Ctx, bool IsArray,
2309 bool HasInit, unsigned NumPlacementArgs,
2310 bool IsParenTypeId);
2311
2313 return getType()->castAs<PointerType>()->getPointeeType();
2314 }
2315
2317 return AllocatedTypeInfo;
2318 }
2319
2320 /// True if the allocation result needs to be null-checked.
2321 ///
2322 /// C++11 [expr.new]p13:
2323 /// If the allocation function returns null, initialization shall
2324 /// not be done, the deallocation function shall not be called,
2325 /// and the value of the new-expression shall be null.
2326 ///
2327 /// C++ DR1748:
2328 /// If the allocation function is a reserved placement allocation
2329 /// function that returns null, the behavior is undefined.
2330 ///
2331 /// An allocation function is not allowed to return null unless it
2332 /// has a non-throwing exception-specification. The '03 rule is
2333 /// identical except that the definition of a non-throwing
2334 /// exception specification is just "is it throw()?".
2335 bool shouldNullCheckAllocation() const;
2336
2337 FunctionDecl *getOperatorNew() const { return OperatorNew; }
2338 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; }
2339 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2340 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; }
2341
2342 bool isArray() const { return CXXNewExprBits.IsArray; }
2343
2344 /// This might return std::nullopt even if isArray() returns true,
2345 /// since there might not be an array size expression.
2346 /// If the result is not std::nullopt, it will never wrap a nullptr.
2347 std::optional<Expr *> getArraySize() {
2348 if (!isArray())
2349 return std::nullopt;
2350
2351 if (auto *Result =
2352 cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
2353 return Result;
2354
2355 return std::nullopt;
2356 }
2357
2358 /// This might return std::nullopt even if isArray() returns true,
2359 /// since there might not be an array size expression.
2360 /// If the result is not std::nullopt, it will never wrap a nullptr.
2361 std::optional<const Expr *> getArraySize() const {
2362 if (!isArray())
2363 return std::nullopt;
2364
2365 if (auto *Result =
2366 cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
2367 return Result;
2368
2369 return std::nullopt;
2370 }
2371
2372 unsigned getNumPlacementArgs() const {
2373 return CXXNewExprBits.NumPlacementArgs;
2374 }
2375
2377 return reinterpret_cast<Expr **>(getTrailingObjects<Stmt *>() +
2378 placementNewArgsOffset());
2379 }
2380
2381 Expr *getPlacementArg(unsigned I) {
2382 assert((I < getNumPlacementArgs()) && "Index out of range!");
2383 return getPlacementArgs()[I];
2384 }
2385 const Expr *getPlacementArg(unsigned I) const {
2386 return const_cast<CXXNewExpr *>(this)->getPlacementArg(I);
2387 }
2388
2389 bool isParenTypeId() const { return CXXNewExprBits.IsParenTypeId; }
2391 return isParenTypeId() ? getTrailingObjects<SourceRange>()[0]
2392 : SourceRange();
2393 }
2394
2395 bool isGlobalNew() const { return CXXNewExprBits.IsGlobalNew; }
2396
2397 /// Whether this new-expression has any initializer at all.
2398 bool hasInitializer() const { return CXXNewExprBits.HasInitializer; }
2399
2400 /// The kind of initializer this new-expression has.
2402 return static_cast<CXXNewInitializationStyle>(
2403 CXXNewExprBits.StoredInitializationStyle);
2404 }
2405
2406 /// The initializer of this new-expression.
2408 return hasInitializer()
2409 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
2410 : nullptr;
2411 }
2412 const Expr *getInitializer() const {
2413 return hasInitializer()
2414 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
2415 : nullptr;
2416 }
2417
2418 /// Returns the CXXConstructExpr from this new-expression, or null.
2420 return dyn_cast_or_null<CXXConstructExpr>(getInitializer());
2421 }
2422
2423 /// Indicates whether the required alignment should be implicitly passed to
2424 /// the allocation function.
2425 bool passAlignment() const { return CXXNewExprBits.ShouldPassAlignment; }
2426
2427 /// Answers whether the usual array deallocation function for the
2428 /// allocated type expects the size of the allocation as a
2429 /// parameter.
2431 return CXXNewExprBits.UsualArrayDeleteWantsSize;
2432 }
2433
2436
2437 llvm::iterator_range<arg_iterator> placement_arguments() {
2438 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2439 }
2440
2441 llvm::iterator_range<const_arg_iterator> placement_arguments() const {
2442 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2443 }
2444
2446 return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
2447 }
2450 }
2452 return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
2453 }
2456 }
2457
2459
2460 raw_arg_iterator raw_arg_begin() { return getTrailingObjects<Stmt *>(); }
2462 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
2463 }
2465 return getTrailingObjects<Stmt *>();
2466 }
2468 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
2469 }
2470
2471 SourceLocation getBeginLoc() const { return Range.getBegin(); }
2472 SourceLocation getEndLoc() const { return Range.getEnd(); }
2473
2474 SourceRange getDirectInitRange() const { return DirectInitRange; }
2475 SourceRange getSourceRange() const { return Range; }
2476
2477 static bool classof(const Stmt *T) {
2478 return T->getStmtClass() == CXXNewExprClass;
2479 }
2480
2481 // Iterators
2483
2485 return const_child_range(const_cast<CXXNewExpr *>(this)->children());
2486 }
2487};
2488
2489/// Represents a \c delete expression for memory deallocation and
2490/// destructor calls, e.g. "delete[] pArray".
2491class CXXDeleteExpr : public Expr {
2492 friend class ASTStmtReader;
2493
2494 /// Points to the operator delete overload that is used. Could be a member.
2495 FunctionDecl *OperatorDelete = nullptr;
2496
2497 /// The pointer expression to be deleted.
2498 Stmt *Argument = nullptr;
2499
2500public:
2501 CXXDeleteExpr(QualType Ty, bool GlobalDelete, bool ArrayForm,
2502 bool ArrayFormAsWritten, bool UsualArrayDeleteWantsSize,
2503 FunctionDecl *OperatorDelete, Expr *Arg, SourceLocation Loc)
2504 : Expr(CXXDeleteExprClass, Ty, VK_PRValue, OK_Ordinary),
2505 OperatorDelete(OperatorDelete), Argument(Arg) {
2506 CXXDeleteExprBits.GlobalDelete = GlobalDelete;
2507 CXXDeleteExprBits.ArrayForm = ArrayForm;
2508 CXXDeleteExprBits.ArrayFormAsWritten = ArrayFormAsWritten;
2509 CXXDeleteExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
2510 CXXDeleteExprBits.Loc = Loc;
2512 }
2513
2514 explicit CXXDeleteExpr(EmptyShell Shell) : Expr(CXXDeleteExprClass, Shell) {}
2515
2516 bool isGlobalDelete() const { return CXXDeleteExprBits.GlobalDelete; }
2517 bool isArrayForm() const { return CXXDeleteExprBits.ArrayForm; }
2519 return CXXDeleteExprBits.ArrayFormAsWritten;
2520 }
2521
2522 /// Answers whether the usual array deallocation function for the
2523 /// allocated type expects the size of the allocation as a
2524 /// parameter. This can be true even if the actual deallocation
2525 /// function that we're using doesn't want a size.
2527 return CXXDeleteExprBits.UsualArrayDeleteWantsSize;
2528 }
2529
2530 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2531
2532 Expr *getArgument() { return cast<Expr>(Argument); }
2533 const Expr *getArgument() const { return cast<Expr>(Argument); }
2534
2535 /// Retrieve the type being destroyed.
2536 ///
2537 /// If the type being destroyed is a dependent type which may or may not
2538 /// be a pointer, return an invalid type.
2539 QualType getDestroyedType() const;
2540
2542 SourceLocation getEndLoc() const LLVM_READONLY {
2543 return Argument->getEndLoc();
2544 }
2545
2546 static bool classof(const Stmt *T) {
2547 return T->getStmtClass() == CXXDeleteExprClass;
2548 }
2549
2550 // Iterators
2551 child_range children() { return child_range(&Argument, &Argument + 1); }
2552
2554 return const_child_range(&Argument, &Argument + 1);
2555 }
2556};
2557
2558/// Stores the type being destroyed by a pseudo-destructor expression.
2560 /// Either the type source information or the name of the type, if
2561 /// it couldn't be resolved due to type-dependence.
2562 llvm::PointerUnion<TypeSourceInfo *, const IdentifierInfo *> Type;
2563
2564 /// The starting source location of the pseudo-destructor type.
2565 SourceLocation Location;
2566
2567public:
2569
2571 : Type(II), Location(Loc) {}
2572
2574
2576 return Type.dyn_cast<TypeSourceInfo *>();
2577 }
2578
2580 return Type.dyn_cast<const IdentifierInfo *>();
2581 }
2582
2583 SourceLocation getLocation() const { return Location; }
2584};
2585
2586/// Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
2587///
2588/// A pseudo-destructor is an expression that looks like a member access to a
2589/// destructor of a scalar type, except that scalar types don't have
2590/// destructors. For example:
2591///
2592/// \code
2593/// typedef int T;
2594/// void f(int *p) {
2595/// p->T::~T();
2596/// }
2597/// \endcode
2598///
2599/// Pseudo-destructors typically occur when instantiating templates such as:
2600///
2601/// \code
2602/// template<typename T>
2603/// void destroy(T* ptr) {
2604/// ptr->T::~T();
2605/// }
2606/// \endcode
2607///
2608/// for scalar types. A pseudo-destructor expression has no run-time semantics
2609/// beyond evaluating the base expression.
2611 friend class ASTStmtReader;
2612
2613 /// The base expression (that is being destroyed).
2614 Stmt *Base = nullptr;
2615
2616 /// Whether the operator was an arrow ('->'); otherwise, it was a
2617 /// period ('.').
2618 LLVM_PREFERRED_TYPE(bool)
2619 bool IsArrow : 1;
2620
2621 /// The location of the '.' or '->' operator.
2622 SourceLocation OperatorLoc;
2623
2624 /// The nested-name-specifier that follows the operator, if present.
2625 NestedNameSpecifierLoc QualifierLoc;
2626
2627 /// The type that precedes the '::' in a qualified pseudo-destructor
2628 /// expression.
2629 TypeSourceInfo *ScopeType = nullptr;
2630
2631 /// The location of the '::' in a qualified pseudo-destructor
2632 /// expression.
2633 SourceLocation ColonColonLoc;
2634
2635 /// The location of the '~'.
2636 SourceLocation TildeLoc;
2637
2638 /// The type being destroyed, or its name if we were unable to
2639 /// resolve the name.
2640 PseudoDestructorTypeStorage DestroyedType;
2641
2642public:
2643 CXXPseudoDestructorExpr(const ASTContext &Context,
2644 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
2645 NestedNameSpecifierLoc QualifierLoc,
2646 TypeSourceInfo *ScopeType,
2647 SourceLocation ColonColonLoc,
2648 SourceLocation TildeLoc,
2649 PseudoDestructorTypeStorage DestroyedType);
2650
2652 : Expr(CXXPseudoDestructorExprClass, Shell), IsArrow(false) {}
2653
2654 Expr *getBase() const { return cast<Expr>(Base); }
2655
2656 /// Determines whether this member expression actually had
2657 /// a C++ nested-name-specifier prior to the name of the member, e.g.,
2658 /// x->Base::foo.
2659 bool hasQualifier() const { return QualifierLoc.hasQualifier(); }
2660
2661 /// Retrieves the nested-name-specifier that qualifies the type name,
2662 /// with source-location information.
2663 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2664
2665 /// If the member name was qualified, retrieves the
2666 /// nested-name-specifier that precedes the member name. Otherwise, returns
2667 /// null.
2669 return QualifierLoc.getNestedNameSpecifier();
2670 }
2671
2672 /// Determine whether this pseudo-destructor expression was written
2673 /// using an '->' (otherwise, it used a '.').
2674 bool isArrow() const { return IsArrow; }
2675
2676 /// Retrieve the location of the '.' or '->' operator.
2677 SourceLocation getOperatorLoc() const { return OperatorLoc; }
2678
2679 /// Retrieve the scope type in a qualified pseudo-destructor
2680 /// expression.
2681 ///
2682 /// Pseudo-destructor expressions can have extra qualification within them
2683 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T().
2684 /// Here, if the object type of the expression is (or may be) a scalar type,
2685 /// \p T may also be a scalar type and, therefore, cannot be part of a
2686 /// nested-name-specifier. It is stored as the "scope type" of the pseudo-
2687 /// destructor expression.
2688 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; }
2689
2690 /// Retrieve the location of the '::' in a qualified pseudo-destructor
2691 /// expression.
2692 SourceLocation getColonColonLoc() const { return ColonColonLoc; }
2693
2694 /// Retrieve the location of the '~'.
2695 SourceLocation getTildeLoc() const { return TildeLoc; }
2696
2697 /// Retrieve the source location information for the type
2698 /// being destroyed.
2699 ///
2700 /// This type-source information is available for non-dependent
2701 /// pseudo-destructor expressions and some dependent pseudo-destructor
2702 /// expressions. Returns null if we only have the identifier for a
2703 /// dependent pseudo-destructor expression.
2705 return DestroyedType.getTypeSourceInfo();
2706 }
2707
2708 /// In a dependent pseudo-destructor expression for which we do not
2709 /// have full type information on the destroyed type, provides the name
2710 /// of the destroyed type.
2712 return DestroyedType.getIdentifier();
2713 }
2714
2715 /// Retrieve the type being destroyed.
2716 QualType getDestroyedType() const;
2717
2718 /// Retrieve the starting location of the type being destroyed.
2720 return DestroyedType.getLocation();
2721 }
2722
2723 /// Set the name of destroyed type for a dependent pseudo-destructor
2724 /// expression.
2726 DestroyedType = PseudoDestructorTypeStorage(II, Loc);
2727 }
2728
2729 /// Set the destroyed type.
2731 DestroyedType = PseudoDestructorTypeStorage(Info);
2732 }
2733
2734 SourceLocation getBeginLoc() const LLVM_READONLY {
2735 return Base->getBeginLoc();
2736 }
2737 SourceLocation getEndLoc() const LLVM_READONLY;
2738
2739 static bool classof(const Stmt *T) {
2740 return T->getStmtClass() == CXXPseudoDestructorExprClass;
2741 }
2742
2743 // Iterators
2745
2747 return const_child_range(&Base, &Base + 1);
2748 }
2749};
2750
2751/// A type trait used in the implementation of various C++11 and
2752/// Library TR1 trait templates.
2753///
2754/// \code
2755/// __is_pod(int) == true
2756/// __is_enum(std::string) == false
2757/// __is_trivially_constructible(vector<int>, int*, int*)
2758/// \endcode
2759class TypeTraitExpr final
2760 : public Expr,
2761 private llvm::TrailingObjects<TypeTraitExpr, TypeSourceInfo *> {
2762 /// The location of the type trait keyword.
2763 SourceLocation Loc;
2764
2765 /// The location of the closing parenthesis.
2766 SourceLocation RParenLoc;
2767
2768 // Note: The TypeSourceInfos for the arguments are allocated after the
2769 // TypeTraitExpr.
2770
2773 SourceLocation RParenLoc,
2774 bool Value);
2775
2776 TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) {}
2777
2778 size_t numTrailingObjects(OverloadToken<TypeSourceInfo *>) const {
2779 return getNumArgs();
2780 }
2781
2782public:
2783 friend class ASTStmtReader;
2784 friend class ASTStmtWriter;
2786
2787 /// Create a new type trait expression.
2788 static TypeTraitExpr *Create(const ASTContext &C, QualType T,
2789 SourceLocation Loc, TypeTrait Kind,
2791 SourceLocation RParenLoc,
2792 bool Value);
2793
2795 unsigned NumArgs);
2796
2797 /// Determine which type trait this expression uses.
2799 return static_cast<TypeTrait>(TypeTraitExprBits.Kind);
2800 }
2801
2802 bool getValue() const {
2803 assert(!isValueDependent());
2804 return TypeTraitExprBits.Value;
2805 }
2806
2807 /// Determine the number of arguments to this type trait.
2808 unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; }
2809
2810 /// Retrieve the Ith argument.
2811 TypeSourceInfo *getArg(unsigned I) const {
2812 assert(I < getNumArgs() && "Argument out-of-range");
2813 return getArgs()[I];
2814 }
2815
2816 /// Retrieve the argument types.
2818 return llvm::ArrayRef(getTrailingObjects<TypeSourceInfo *>(), getNumArgs());
2819 }
2820
2821 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2822 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
2823
2824 static bool classof(const Stmt *T) {
2825 return T->getStmtClass() == TypeTraitExprClass;
2826 }
2827
2828 // Iterators
2831 }
2832
2835 }
2836};
2837
2838/// An Embarcadero array type trait, as used in the implementation of
2839/// __array_rank and __array_extent.
2840///
2841/// Example:
2842/// \code
2843/// __array_rank(int[10][20]) == 2
2844/// __array_extent(int, 1) == 20
2845/// \endcode
2846class ArrayTypeTraitExpr : public Expr {
2847 /// The trait. An ArrayTypeTrait enum in MSVC compat unsigned.
2848 LLVM_PREFERRED_TYPE(ArrayTypeTrait)
2849 unsigned ATT : 2;
2850
2851 /// The value of the type trait. Unspecified if dependent.
2852 uint64_t Value = 0;
2853
2854 /// The array dimension being queried, or -1 if not used.
2855 Expr *Dimension;
2856
2857 /// The location of the type trait keyword.
2858 SourceLocation Loc;
2859
2860 /// The location of the closing paren.
2861 SourceLocation RParen;
2862
2863 /// The type being queried.
2864 TypeSourceInfo *QueriedType = nullptr;
2865
2866public:
2867 friend class ASTStmtReader;
2868
2870 TypeSourceInfo *queried, uint64_t value, Expr *dimension,
2871 SourceLocation rparen, QualType ty)
2872 : Expr(ArrayTypeTraitExprClass, ty, VK_PRValue, OK_Ordinary), ATT(att),
2873 Value(value), Dimension(dimension), Loc(loc), RParen(rparen),
2874 QueriedType(queried) {
2875 assert(att <= ATT_Last && "invalid enum value!");
2876 assert(static_cast<unsigned>(att) == ATT && "ATT overflow!");
2878 }
2879
2881 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0) {}
2882
2883 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2884 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
2885
2886 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); }
2887
2888 QualType getQueriedType() const { return QueriedType->getType(); }
2889
2890 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; }
2891
2892 uint64_t getValue() const { assert(!isTypeDependent()); return Value; }
2893
2894 Expr *getDimensionExpression() const { return Dimension; }
2895
2896 static bool classof(const Stmt *T) {
2897 return T->getStmtClass() == ArrayTypeTraitExprClass;
2898 }
2899
2900 // Iterators
2903 }
2904
2907 }
2908};
2909
2910/// An expression trait intrinsic.
2911///
2912/// Example:
2913/// \code
2914/// __is_lvalue_expr(std::cout) == true
2915/// __is_lvalue_expr(1) == false
2916/// \endcode
2918 /// The trait. A ExpressionTrait enum in MSVC compatible unsigned.
2919 LLVM_PREFERRED_TYPE(ExpressionTrait)
2920 unsigned ET : 31;
2921
2922 /// The value of the type trait. Unspecified if dependent.
2923 LLVM_PREFERRED_TYPE(bool)
2924 unsigned Value : 1;
2925
2926 /// The location of the type trait keyword.
2927 SourceLocation Loc;
2928
2929 /// The location of the closing paren.
2930 SourceLocation RParen;
2931
2932 /// The expression being queried.
2933 Expr* QueriedExpression = nullptr;
2934
2935public:
2936 friend class ASTStmtReader;
2937
2939 bool value, SourceLocation rparen, QualType resultType)
2940 : Expr(ExpressionTraitExprClass, resultType, VK_PRValue, OK_Ordinary),
2941 ET(et), Value(value), Loc(loc), RParen(rparen),
2942 QueriedExpression(queried) {
2943 assert(et <= ET_Last && "invalid enum value!");
2944 assert(static_cast<unsigned>(et) == ET && "ET overflow!");
2946 }
2947
2949 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false) {}
2950
2951 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2952 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
2953
2954 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); }
2955
2956 Expr *getQueriedExpression() const { return QueriedExpression; }
2957
2958 bool getValue() const { return Value; }
2959
2960 static bool classof(const Stmt *T) {
2961 return T->getStmtClass() == ExpressionTraitExprClass;
2962 }
2963
2964 // Iterators
2967 }
2968
2971 }
2972};
2973
2974/// A reference to an overloaded function set, either an
2975/// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr.
2976class OverloadExpr : public Expr {
2977 friend class ASTStmtReader;
2978 friend class ASTStmtWriter;
2979
2980 /// The common name of these declarations.
2981 DeclarationNameInfo NameInfo;
2982
2983 /// The nested-name-specifier that qualifies the name, if any.
2984 NestedNameSpecifierLoc QualifierLoc;
2985
2986protected:
2987 OverloadExpr(StmtClass SC, const ASTContext &Context,
2988 NestedNameSpecifierLoc QualifierLoc,
2989 SourceLocation TemplateKWLoc,
2990 const DeclarationNameInfo &NameInfo,
2991 const TemplateArgumentListInfo *TemplateArgs,
2993 bool KnownDependent, bool KnownInstantiationDependent,
2994 bool KnownContainsUnexpandedParameterPack);
2995
2996 OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
2997 bool HasTemplateKWAndArgsInfo);
2998
2999 /// Return the results. Defined after UnresolvedMemberExpr.
3002 return const_cast<OverloadExpr *>(this)->getTrailingResults();
3003 }
3004
3005 /// Return the optional template keyword and arguments info.
3006 /// Defined after UnresolvedMemberExpr.
3009 return const_cast<OverloadExpr *>(this)
3011 }
3012
3013 /// Return the optional template arguments. Defined after
3014 /// UnresolvedMemberExpr.
3017 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
3018 }
3019
3021 return OverloadExprBits.HasTemplateKWAndArgsInfo;
3022 }
3023
3024public:
3025 struct FindResult {
3029 };
3030
3031 /// Finds the overloaded expression in the given expression \p E of
3032 /// OverloadTy.
3033 ///
3034 /// \return the expression (which must be there) and true if it has
3035 /// the particular form of a member pointer expression
3036 static FindResult find(Expr *E) {
3037 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload));
3038
3040
3041 E = E->IgnoreParens();
3042 if (isa<UnaryOperator>(E)) {
3043 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf);
3044 E = cast<UnaryOperator>(E)->getSubExpr();
3045 auto *Ovl = cast<OverloadExpr>(E->IgnoreParens());
3046
3047 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier());
3048 Result.IsAddressOfOperand = true;
3049 Result.Expression = Ovl;
3050 } else {
3051 Result.HasFormOfMemberPointer = false;
3052 Result.IsAddressOfOperand = false;
3053 Result.Expression = cast<OverloadExpr>(E);
3054 }
3055
3056 return Result;
3057 }
3058
3059 /// Gets the naming class of this lookup, if any.
3060 /// Defined after UnresolvedMemberExpr.
3061 inline CXXRecordDecl *getNamingClass();
3063 return const_cast<OverloadExpr *>(this)->getNamingClass();
3064 }
3065
3067
3070 }
3073 }
3074 llvm::iterator_range<decls_iterator> decls() const {
3075 return llvm::make_range(decls_begin(), decls_end());
3076 }
3077
3078 /// Gets the number of declarations in the unresolved set.
3079 unsigned getNumDecls() const { return OverloadExprBits.NumResults; }
3080
3081 /// Gets the full name info.
3082 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
3083
3084 /// Gets the name looked up.
3085 DeclarationName getName() const { return NameInfo.getName(); }
3086
3087 /// Gets the location of the name.
3088 SourceLocation getNameLoc() const { return NameInfo.getLoc(); }
3089
3090 /// Fetches the nested-name qualifier, if one was given.
3092 return QualifierLoc.getNestedNameSpecifier();
3093 }
3094
3095 /// Fetches the nested-name qualifier with source-location
3096 /// information, if one was given.
3097 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3098
3099 /// Retrieve the location of the template keyword preceding
3100 /// this name, if any.
3103 return SourceLocation();
3105 }
3106
3107 /// Retrieve the location of the left angle bracket starting the
3108 /// explicit template argument list following the name, if any.
3111 return SourceLocation();
3113 }
3114
3115 /// Retrieve the location of the right angle bracket ending the
3116 /// explicit template argument list following the name, if any.
3119 return SourceLocation();
3121 }
3122
3123 /// Determines whether the name was preceded by the template keyword.
3125
3126 /// Determines whether this expression had explicit template arguments.
3127 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3128
3131 return nullptr;
3132 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
3133 }
3134
3135 unsigned getNumTemplateArgs() const {
3137 return 0;
3138
3140 }
3141
3143 return {getTemplateArgs(), getNumTemplateArgs()};
3144 }
3145
3146 /// Copies the template arguments into the given structure.
3150 }
3151
3152 static bool classof(const Stmt *T) {
3153 return T->getStmtClass() == UnresolvedLookupExprClass ||
3154 T->getStmtClass() == UnresolvedMemberExprClass;
3155 }
3156};
3157
3158/// A reference to a name which we were able to look up during
3159/// parsing but could not resolve to a specific declaration.
3160///
3161/// This arises in several ways:
3162/// * we might be waiting for argument-dependent lookup;
3163/// * the name might resolve to an overloaded function;
3164/// and eventually:
3165/// * the lookup might have included a function template.
3166///
3167/// These never include UnresolvedUsingValueDecls, which are always class
3168/// members and therefore appear only in UnresolvedMemberLookupExprs.
3170 : public OverloadExpr,
3171 private llvm::TrailingObjects<UnresolvedLookupExpr, DeclAccessPair,
3172 ASTTemplateKWAndArgsInfo,
3173 TemplateArgumentLoc> {
3174 friend class ASTStmtReader;
3175 friend class OverloadExpr;
3176 friend TrailingObjects;
3177
3178 /// The naming class (C++ [class.access.base]p5) of the lookup, if
3179 /// any. This can generally be recalculated from the context chain,
3180 /// but that can be fairly expensive for unqualified lookups.
3181 CXXRecordDecl *NamingClass;
3182
3183 // UnresolvedLookupExpr is followed by several trailing objects.
3184 // They are in order:
3185 //
3186 // * An array of getNumResults() DeclAccessPair for the results. These are
3187 // undesugared, which is to say, they may include UsingShadowDecls.
3188 // Access is relative to the naming class.
3189 //
3190 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3191 // template keyword and arguments. Present if and only if
3192 // hasTemplateKWAndArgsInfo().
3193 //
3194 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing
3195 // location information for the explicitly specified template arguments.
3196
3197 UnresolvedLookupExpr(const ASTContext &Context, CXXRecordDecl *NamingClass,
3198 NestedNameSpecifierLoc QualifierLoc,
3199 SourceLocation TemplateKWLoc,
3200 const DeclarationNameInfo &NameInfo, bool RequiresADL,
3201 bool Overloaded,
3202 const TemplateArgumentListInfo *TemplateArgs,
3204 bool KnownDependent);
3205
3206 UnresolvedLookupExpr(EmptyShell Empty, unsigned NumResults,
3207 bool HasTemplateKWAndArgsInfo);
3208
3209 unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const {
3210 return getNumDecls();
3211 }
3212
3213 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3214 return hasTemplateKWAndArgsInfo();
3215 }
3216
3217public:
3218 static UnresolvedLookupExpr *
3219 Create(const ASTContext &Context, CXXRecordDecl *NamingClass,
3220 NestedNameSpecifierLoc QualifierLoc,
3221 const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
3222 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3223
3224 // After canonicalization, there may be dependent template arguments in
3225 // CanonicalConverted But none of Args is dependent. When any of
3226 // CanonicalConverted dependent, KnownDependent is true.
3227 static UnresolvedLookupExpr *
3228 Create(const ASTContext &Context, CXXRecordDecl *NamingClass,
3229 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
3230 const DeclarationNameInfo &NameInfo, bool RequiresADL,
3231 const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
3232 UnresolvedSetIterator End, bool KnownDependent);
3233
3234 static UnresolvedLookupExpr *CreateEmpty(const ASTContext &Context,
3235 unsigned NumResults,
3236 bool HasTemplateKWAndArgsInfo,
3237 unsigned NumTemplateArgs);
3238
3239 /// True if this declaration should be extended by
3240 /// argument-dependent lookup.
3241 bool requiresADL() const { return UnresolvedLookupExprBits.RequiresADL; }
3242
3243 /// True if this lookup is overloaded.
3244 bool isOverloaded() const { return UnresolvedLookupExprBits.Overloaded; }
3245
3246 /// Gets the 'naming class' (in the sense of C++0x
3247 /// [class.access.base]p5) of the lookup. This is the scope
3248 /// that was looked in to find these results.
3249 CXXRecordDecl *getNamingClass() { return NamingClass; }
3250 const CXXRecordDecl *getNamingClass() const { return NamingClass; }
3251
3252 SourceLocation getBeginLoc() const LLVM_READONLY {
3254 return l.getBeginLoc();
3255 return getNameInfo().getBeginLoc();
3256 }
3257
3258 SourceLocation getEndLoc() const LLVM_READONLY {
3260 return getRAngleLoc();
3261 return getNameInfo().getEndLoc();
3262 }
3263
3266 }
3267
3270 }
3271
3272 static bool classof(const Stmt *T) {
3273 return T->getStmtClass() == UnresolvedLookupExprClass;
3274 }
3275};
3276
3277/// A qualified reference to a name whose declaration cannot
3278/// yet be resolved.
3279///
3280/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that
3281/// it expresses a reference to a declaration such as
3282/// X<T>::value. The difference, however, is that an
3283/// DependentScopeDeclRefExpr node is used only within C++ templates when
3284/// the qualification (e.g., X<T>::) refers to a dependent type. In
3285/// this case, X<T>::value cannot resolve to a declaration because the
3286/// declaration will differ from one instantiation of X<T> to the
3287/// next. Therefore, DependentScopeDeclRefExpr keeps track of the
3288/// qualifier (X<T>::) and the name of the entity being referenced
3289/// ("value"). Such expressions will instantiate to a DeclRefExpr once the
3290/// declaration can be found.
3292 : public Expr,
3293 private llvm::TrailingObjects<DependentScopeDeclRefExpr,
3294 ASTTemplateKWAndArgsInfo,
3295 TemplateArgumentLoc> {
3296 friend class ASTStmtReader;
3297 friend class ASTStmtWriter;
3298 friend TrailingObjects;
3299
3300 /// The nested-name-specifier that qualifies this unresolved
3301 /// declaration name.
3302 NestedNameSpecifierLoc QualifierLoc;
3303
3304 /// The name of the entity we will be referencing.
3305 DeclarationNameInfo NameInfo;
3306
3308 SourceLocation TemplateKWLoc,
3309 const DeclarationNameInfo &NameInfo,
3310 const TemplateArgumentListInfo *Args);
3311
3312 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3313 return hasTemplateKWAndArgsInfo();
3314 }
3315
3316 bool hasTemplateKWAndArgsInfo() const {
3317 return DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo;
3318 }
3319
3320public:
3321 static DependentScopeDeclRefExpr *
3322 Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
3323 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
3324 const TemplateArgumentListInfo *TemplateArgs);
3325
3326 static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &Context,
3327 bool HasTemplateKWAndArgsInfo,
3328 unsigned NumTemplateArgs);
3329
3330 /// Retrieve the name that this expression refers to.
3331 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
3332
3333 /// Retrieve the name that this expression refers to.
3334 DeclarationName getDeclName() const { return NameInfo.getName(); }
3335
3336 /// Retrieve the location of the name within the expression.
3337 ///
3338 /// For example, in "X<T>::value" this is the location of "value".
3339 SourceLocation getLocation() const { return NameInfo.getLoc(); }
3340
3341 /// Retrieve the nested-name-specifier that qualifies the
3342 /// name, with source location information.
3343 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3344
3345 /// Retrieve the nested-name-specifier that qualifies this
3346 /// declaration.
3348 return QualifierLoc.getNestedNameSpecifier();
3349 }
3350
3351 /// Retrieve the location of the template keyword preceding
3352 /// this name, if any.
3354 if (!hasTemplateKWAndArgsInfo())
3355 return SourceLocation();
3356 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
3357 }
3358
3359 /// Retrieve the location of the left angle bracket starting the
3360 /// explicit template argument list following the name, if any.
3362 if (!hasTemplateKWAndArgsInfo())
3363 return SourceLocation();
3364 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
3365 }
3366
3367 /// Retrieve the location of the right angle bracket ending the
3368 /// explicit template argument list following the name, if any.
3370 if (!hasTemplateKWAndArgsInfo())
3371 return SourceLocation();
3372 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
3373 }
3374
3375 /// Determines whether the name was preceded by the template keyword.
3377
3378 /// Determines whether this lookup had explicit template arguments.
3379 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3380
3381 /// Copies the template arguments (if present) into the given
3382 /// structure.
3385 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
3386 getTrailingObjects<TemplateArgumentLoc>(), List);
3387 }
3388
3391 return nullptr;
3392
3393 return getTrailingObjects<TemplateArgumentLoc>();
3394 }
3395
3396 unsigned getNumTemplateArgs() const {
3398 return 0;
3399
3400 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
3401 }
3402
3404 return {getTemplateArgs(), getNumTemplateArgs()};
3405 }
3406
3407 /// Note: getBeginLoc() is the start of the whole DependentScopeDeclRefExpr,
3408 /// and differs from getLocation().getStart().
3409 SourceLocation getBeginLoc() const LLVM_READONLY {
3410 return QualifierLoc.getBeginLoc();
3411 }
3412
3413 SourceLocation getEndLoc() const LLVM_READONLY {
3415 return getRAngleLoc();
3416 return getLocation();
3417 }
3418
3419 static bool classof(const Stmt *T) {
3420 return T->getStmtClass() == DependentScopeDeclRefExprClass;
3421 }
3422
3425 }
3426
3429 }
3430};
3431
3432/// Represents an expression -- generally a full-expression -- that
3433/// introduces cleanups to be run at the end of the sub-expression's
3434/// evaluation. The most common source of expression-introduced
3435/// cleanups is temporary objects in C++, but several other kinds of
3436/// expressions can create cleanups, including basically every
3437/// call in ARC that returns an Objective-C pointer.
3438///
3439/// This expression also tracks whether the sub-expression contains a
3440/// potentially-evaluated block literal. The lifetime of a block
3441/// literal is the extent of the enclosing scope.
3443 : public FullExpr,
3444 private llvm::TrailingObjects<
3445 ExprWithCleanups,
3446 llvm::PointerUnion<BlockDecl *, CompoundLiteralExpr *>> {
3447public:
3448 /// The type of objects that are kept in the cleanup.
3449 /// It's useful to remember the set of blocks and block-scoped compound
3450 /// literals; we could also remember the set of temporaries, but there's
3451 /// currently no need.
3452 using CleanupObject = llvm::PointerUnion<BlockDecl *, CompoundLiteralExpr *>;
3453
3454private:
3455 friend class ASTStmtReader;
3456 friend TrailingObjects;
3457
3458 ExprWithCleanups(EmptyShell, unsigned NumObjects);
3459 ExprWithCleanups(Expr *SubExpr, bool CleanupsHaveSideEffects,
3460 ArrayRef<CleanupObject> Objects);
3461
3462public:
3463 static ExprWithCleanups *Create(const ASTContext &C, EmptyShell empty,
3464 unsigned numObjects);
3465
3466 static ExprWithCleanups *Create(const ASTContext &C, Expr *subexpr,
3467 bool CleanupsHaveSideEffects,
3468 ArrayRef<CleanupObject> objects);
3469
3471 return llvm::ArrayRef(getTrailingObjects<CleanupObject>(), getNumObjects());
3472 }
3473
3474 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; }
3475
3476 CleanupObject getObject(unsigned i) const {
3477 assert(i < getNumObjects() && "Index out of range");
3478 return getObjects()[i];
3479 }
3480
3482 return ExprWithCleanupsBits.CleanupsHaveSideEffects;
3483 }
3484
3485 SourceLocation getBeginLoc() const LLVM_READONLY {
3486 return SubExpr->getBeginLoc();
3487 }
3488
3489 SourceLocation getEndLoc() const LLVM_READONLY {
3490 return SubExpr->getEndLoc();
3491 }
3492
3493 // Implement isa/cast/dyncast/etc.
3494 static bool classof(const Stmt *T) {
3495 return T->getStmtClass() == ExprWithCleanupsClass;
3496 }
3497
3498 // Iterators
3500
3502 return const_child_range(&SubExpr, &SubExpr + 1);
3503 }
3504};
3505
3506/// Describes an explicit type conversion that uses functional
3507/// notion but could not be resolved because one or more arguments are
3508/// type-dependent.
3509///
3510/// The explicit type conversions expressed by
3511/// CXXUnresolvedConstructExpr have the form <tt>T(a1, a2, ..., aN)</tt>,
3512/// where \c T is some type and \c a1, \c a2, ..., \c aN are values, and
3513/// either \c T is a dependent type or one or more of the <tt>a</tt>'s is
3514/// type-dependent. For example, this would occur in a template such
3515/// as:
3516///
3517/// \code
3518/// template<typename T, typename A1>
3519/// inline T make_a(const A1& a1) {
3520/// return T(a1);
3521/// }
3522/// \endcode
3523///
3524/// When the returned expression is instantiated, it may resolve to a
3525/// constructor call, conversion function call, or some kind of type
3526/// conversion.
3528 : public Expr,
3529 private llvm::TrailingObjects<CXXUnresolvedConstructExpr, Expr *> {
3530 friend class ASTStmtReader;
3531 friend TrailingObjects;
3532
3533 /// The type being constructed, and whether the construct expression models
3534 /// list initialization or not.
3535 llvm::PointerIntPair<TypeSourceInfo *, 1> TypeAndInitForm;
3536
3537 /// The location of the left parentheses ('(').
3538 SourceLocation LParenLoc;
3539
3540 /// The location of the right parentheses (')').
3541 SourceLocation RParenLoc;
3542
3544 SourceLocation LParenLoc, ArrayRef<Expr *> Args,
3545 SourceLocation RParenLoc, bool IsListInit);
3546
3547 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs)
3548 : Expr(CXXUnresolvedConstructExprClass, Empty) {
3549 CXXUnresolvedConstructExprBits.NumArgs = NumArgs;
3550 }
3551
3552public:
3554 Create(const ASTContext &Context, QualType T, TypeSourceInfo *TSI,
3555 SourceLocation LParenLoc, ArrayRef<Expr *> Args,
3556 SourceLocation RParenLoc, bool IsListInit);
3557
3558 static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &Context,
3559 unsigned NumArgs);
3560
3561 /// Retrieve the type that is being constructed, as specified
3562 /// in the source code.
3564
3565 /// Retrieve the type source information for the type being
3566 /// constructed.
3568 return TypeAndInitForm.getPointer();
3569 }
3570
3571 /// Retrieve the location of the left parentheses ('(') that
3572 /// precedes the argument list.
3573 SourceLocation getLParenLoc() const { return LParenLoc; }
3574 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
3575
3576 /// Retrieve the location of the right parentheses (')') that
3577 /// follows the argument list.
3578 SourceLocation getRParenLoc() const { return RParenLoc; }
3579 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
3580
3581 /// Determine whether this expression models list-initialization.
3582 /// If so, there will be exactly one subexpression, which will be
3583 /// an InitListExpr.
3584 bool isListInitialization() const { return TypeAndInitForm.getInt(); }
3585
3586 /// Retrieve the number of arguments.
3587 unsigned getNumArgs() const { return CXXUnresolvedConstructExprBits.NumArgs; }
3588
3589 using arg_iterator = Expr **;
3590 using arg_range = llvm::iterator_range<arg_iterator>;
3591
3592 arg_iterator arg_begin() { return getTrailingObjects<Expr *>(); }
3595
3596 using const_arg_iterator = const Expr* const *;
3597 using const_arg_range = llvm::iterator_range<const_arg_iterator>;
3598
3599 const_arg_iterator arg_begin() const { return getTrailingObjects<Expr *>(); }
3602 return const_arg_range(arg_begin(), arg_end());
3603 }
3604
3605 Expr *getArg(unsigned I) {
3606 assert(I < getNumArgs() && "Argument index out-of-range");
3607 return arg_begin()[I];
3608 }
3609
3610 const Expr *getArg(unsigned I) const {
3611 assert(I < getNumArgs() && "Argument index out-of-range");
3612 return arg_begin()[I];
3613 }
3614
3615 void setArg(unsigned I, Expr *E) {
3616 assert(I < getNumArgs() && "Argument index out-of-range");
3617 arg_begin()[I] = E;
3618 }
3619
3620 SourceLocation getBeginLoc() const LLVM_READONLY;
3621 SourceLocation getEndLoc() const LLVM_READONLY {
3622 if (!RParenLoc.isValid() && getNumArgs() > 0)
3623 return getArg(getNumArgs() - 1)->getEndLoc();
3624 return RParenLoc;
3625 }
3626
3627 static bool classof(const Stmt *T) {
3628 return T->getStmtClass() == CXXUnresolvedConstructExprClass;
3629 }
3630
3631 // Iterators
3633 auto **begin = reinterpret_cast<Stmt **>(arg_begin());
3634 return child_range(begin, begin + getNumArgs());
3635 }
3636
3638 auto **begin = reinterpret_cast<Stmt **>(
3639 const_cast<CXXUnresolvedConstructExpr *>(this)->arg_begin());
3640 return const_child_range(begin, begin + getNumArgs());
3641 }
3642};
3643
3644/// Represents a C++ member access expression where the actual
3645/// member referenced could not be resolved because the base
3646/// expression or the member name was dependent.
3647///
3648/// Like UnresolvedMemberExprs, these can be either implicit or
3649/// explicit accesses. It is only possible to get one of these with
3650/// an implicit access if a qualifier is provided.
3652 : public Expr,
3653 private llvm::TrailingObjects<CXXDependentScopeMemberExpr,
3654 ASTTemplateKWAndArgsInfo,
3655 TemplateArgumentLoc, NamedDecl *> {
3656 friend class ASTStmtReader;
3657 friend class ASTStmtWriter;
3658 friend TrailingObjects;
3659
3660 /// The expression for the base pointer or class reference,
3661 /// e.g., the \c x in x.f. Can be null in implicit accesses.
3662 Stmt *Base;
3663
3664 /// The type of the base expression. Never null, even for
3665 /// implicit accesses.
3666 QualType BaseType;
3667
3668 /// The nested-name-specifier that precedes the member name, if any.
3669 /// FIXME: This could be in principle store as a trailing object.
3670 /// However the performance impact of doing so should be investigated first.
3671 NestedNameSpecifierLoc QualifierLoc;
3672
3673 /// The member to which this member expression refers, which
3674 /// can be name, overloaded operator, or destructor.
3675 ///
3676 /// FIXME: could also be a template-id
3677 DeclarationNameInfo MemberNameInfo;
3678
3679 // CXXDependentScopeMemberExpr is followed by several trailing objects,
3680 // some of which optional. They are in order:
3681 //
3682 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3683 // template keyword and arguments. Present if and only if
3684 // hasTemplateKWAndArgsInfo().
3685 //
3686 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing location
3687 // information for the explicitly specified template arguments.
3688 //
3689 // * An optional NamedDecl *. In a qualified member access expression such
3690 // as t->Base::f, this member stores the resolves of name lookup in the
3691 // context of the member access expression, to be used at instantiation
3692 // time. Present if and only if hasFirstQualifierFoundInScope().
3693
3694 bool hasTemplateKWAndArgsInfo() const {
3695 return CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo;
3696 }
3697
3698 bool hasFirstQualifierFoundInScope() const {
3699 return CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope;
3700 }
3701
3702 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3703 return hasTemplateKWAndArgsInfo();
3704 }
3705
3706 unsigned numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const {
3707 return getNumTemplateArgs();
3708 }
3709
3710 unsigned numTrailingObjects(OverloadToken<NamedDecl *>) const {
3711 return hasFirstQualifierFoundInScope();
3712 }
3713
3714 CXXDependentScopeMemberExpr(const ASTContext &Ctx, Expr *Base,
3715 QualType BaseType, bool IsArrow,
3716 SourceLocation OperatorLoc,
3717 NestedNameSpecifierLoc QualifierLoc,
3718 SourceLocation TemplateKWLoc,
3719 NamedDecl *FirstQualifierFoundInScope,
3720 DeclarationNameInfo MemberNameInfo,
3721 const TemplateArgumentListInfo *TemplateArgs);
3722
3723 CXXDependentScopeMemberExpr(EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
3724 bool HasFirstQualifierFoundInScope);
3725
3726public:
3727 static CXXDependentScopeMemberExpr *
3728 Create(const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
3729 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
3730 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
3731 DeclarationNameInfo MemberNameInfo,
3732 const TemplateArgumentListInfo *TemplateArgs);
3733
3734 static CXXDependentScopeMemberExpr *
3735 CreateEmpty(const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
3736 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope);
3737
3738 /// True if this is an implicit access, i.e. one in which the
3739 /// member being accessed was not written in the source. The source
3740 /// location of the operator is invalid in this case.
3741 bool isImplicitAccess() const {
3742 if (!Base)
3743 return true;
3744 return cast<Expr>(Base)->isImplicitCXXThis();
3745 }
3746
3747 /// Retrieve the base object of this member expressions,
3748 /// e.g., the \c x in \c x.m.
3749 Expr *getBase() const {
3750 assert(!isImplicitAccess());
3751 return cast<Expr>(Base);
3752 }
3753
3754 QualType getBaseType() const { return BaseType; }
3755
3756 /// Determine whether this member expression used the '->'
3757 /// operator; otherwise, it used the '.' operator.
3758 bool isArrow() const { return CXXDependentScopeMemberExprBits.IsArrow; }
3759
3760 /// Retrieve the location of the '->' or '.' operator.
3762 return CXXDependentScopeMemberExprBits.OperatorLoc;
3763 }
3764
3765 /// Retrieve the nested-name-specifier that qualifies the member name.
3767 return QualifierLoc.getNestedNameSpecifier();
3768 }
3769
3770 /// Retrieve the nested-name-specifier that qualifies the member
3771 /// name, with source location information.
3772 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3773
3774 /// Retrieve the first part of the nested-name-specifier that was
3775 /// found in the scope of the member access expression when the member access
3776 /// was initially parsed.
3777 ///
3778 /// This function only returns a useful result when member access expression
3779 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration
3780 /// returned by this function describes what was found by unqualified name
3781 /// lookup for the identifier "Base" within the scope of the member access
3782 /// expression itself. At template instantiation time, this information is
3783 /// combined with the results of name lookup into the type of the object
3784 /// expression itself (the class type of x).
3786 if (!hasFirstQualifierFoundInScope())
3787 return nullptr;
3788 return *getTrailingObjects<NamedDecl *>();
3789 }
3790
3791 /// Retrieve the name of the member that this expression refers to.
3793 return MemberNameInfo;
3794 }
3795
3796 /// Retrieve the name of the member that this expression refers to.
3797 DeclarationName getMember() const { return MemberNameInfo.getName(); }
3798
3799 // Retrieve the location of the name of the member that this
3800 // expression refers to.
3801 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); }
3802
3803 /// Retrieve the location of the template keyword preceding the
3804 /// member name, if any.
3806 if (!hasTemplateKWAndArgsInfo())
3807 return SourceLocation();
3808 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
3809 }
3810
3811 /// Retrieve the location of the left angle bracket starting the
3812 /// explicit template argument list following the member name, if any.
3814 if (!hasTemplateKWAndArgsInfo())
3815 return SourceLocation();
3816 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
3817 }
3818
3819 /// Retrieve the location of the right angle bracket ending the
3820 /// explicit template argument list following the member name, if any.
3822 if (!hasTemplateKWAndArgsInfo())
3823 return SourceLocation();
3824 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
3825 }
3826
3827 /// Determines whether the member name was preceded by the template keyword.
3829
3830 /// Determines whether this member expression actually had a C++
3831 /// template argument list explicitly specified, e.g., x.f<int>.
3832 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3833
3834 /// Copies the template arguments (if present) into the given
3835 /// structure.
3838 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
3839 getTrailingObjects<TemplateArgumentLoc>(), List);
3840 }
3841
3842 /// Retrieve the template arguments provided as part of this
3843 /// template-id.
3846 return nullptr;
3847
3848 return getTrailingObjects<TemplateArgumentLoc>();
3849 }
3850
3851 /// Retrieve the number of template arguments provided as part of this
3852 /// template-id.
3853 unsigned getNumTemplateArgs() const {
3855 return 0;
3856
3857 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
3858 }
3859
3861 return {getTemplateArgs(), getNumTemplateArgs()};
3862 }
3863
3864 SourceLocation getBeginLoc() const LLVM_READONLY {
3865 if (!isImplicitAccess())
3866 return Base->getBeginLoc();
3867 if (getQualifier())
3868 return getQualifierLoc().getBeginLoc();
3869 return MemberNameInfo.getBeginLoc();
3870 }
3871
3872 SourceLocation getEndLoc() const LLVM_READONLY {
3874 return getRAngleLoc();
3875 return MemberNameInfo.getEndLoc();
3876 }
3877
3878 static bool classof(const Stmt *T) {
3879 return T->getStmtClass() == CXXDependentScopeMemberExprClass;
3880 }
3881
3882 // Iterators
3884 if (isImplicitAccess())
3886 return child_range(&Base, &Base + 1);
3887 }
3888
3890 if (isImplicitAccess())
3892 return const_child_range(&Base, &Base + 1);
3893 }
3894};
3895
3896/// Represents a C++ member access expression for which lookup
3897/// produced a set of overloaded functions.
3898///
3899/// The member access may be explicit or implicit:
3900/// \code
3901/// struct A {
3902/// int a, b;
3903/// int explicitAccess() { return this->a + this->A::b; }
3904/// int implicitAccess() { return a + A::b; }
3905/// };
3906/// \endcode
3907///
3908/// In the final AST, an explicit access always becomes a MemberExpr.
3909/// An implicit access may become either a MemberExpr or a
3910/// DeclRefExpr, depending on whether the member is static.
3912 : public OverloadExpr,
3913 private llvm::TrailingObjects<UnresolvedMemberExpr, DeclAccessPair,
3914 ASTTemplateKWAndArgsInfo,
3915 TemplateArgumentLoc> {
3916 friend class ASTStmtReader;
3917 friend class OverloadExpr;
3918 friend TrailingObjects;
3919
3920 /// The expression for the base pointer or class reference,
3921 /// e.g., the \c x in x.f.
3922 ///
3923 /// This can be null if this is an 'unbased' member expression.
3924 Stmt *Base;
3925
3926 /// The type of the base expression; never null.
3927 QualType BaseType;
3928
3929 /// The location of the '->' or '.' operator.
3930 SourceLocation OperatorLoc;
3931
3932 // UnresolvedMemberExpr is followed by several trailing objects.
3933 // They are in order:
3934 //
3935 // * An array of getNumResults() DeclAccessPair for the results. These are
3936 // undesugared, which is to say, they may include UsingShadowDecls.
3937 // Access is relative to the naming class.
3938 //
3939 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3940 // template keyword and arguments. Present if and only if
3941 // hasTemplateKWAndArgsInfo().
3942 //
3943 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing
3944 // location information for the explicitly specified template arguments.
3945
3946 UnresolvedMemberExpr(const ASTContext &Context, bool HasUnresolvedUsing,
3947 Expr *Base, QualType BaseType, bool IsArrow,
3948 SourceLocation OperatorLoc,
3949 NestedNameSpecifierLoc QualifierLoc,
3950 SourceLocation TemplateKWLoc,
3951 const DeclarationNameInfo &MemberNameInfo,
3952 const TemplateArgumentListInfo *TemplateArgs,
3954
3955 UnresolvedMemberExpr(EmptyShell Empty, unsigned NumResults,
3956 bool HasTemplateKWAndArgsInfo);
3957
3958 unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const {
3959 return getNumDecls();
3960 }
3961
3962 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3963 return hasTemplateKWAndArgsInfo();
3964 }
3965
3966public:
3967 static UnresolvedMemberExpr *
3968 Create(const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
3969 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
3970 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
3971 const DeclarationNameInfo &MemberNameInfo,
3972 const TemplateArgumentListInfo *TemplateArgs,
3973 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3974
3975 static UnresolvedMemberExpr *CreateEmpty(const ASTContext &Context,
3976 unsigned NumResults,
3977 bool HasTemplateKWAndArgsInfo,
3978 unsigned NumTemplateArgs);
3979
3980 /// True if this is an implicit access, i.e., one in which the
3981 /// member being accessed was not written in the source.
3982 ///
3983 /// The source location of the operator is invalid in this case.
3984 bool isImplicitAccess() const;
3985
3986 /// Retrieve the base object of this member expressions,
3987 /// e.g., the \c x in \c x.m.
3989 assert(!isImplicitAccess());
3990 return cast<Expr>(Base);
3991 }
3992 const Expr *getBase() const {
3993 assert(!isImplicitAccess());
3994 return cast<Expr>(Base);
3995 }
3996
3997 QualType getBaseType() const { return BaseType; }
3998
3999 /// Determine whether the lookup results contain an unresolved using
4000 /// declaration.
4001 bool hasUnresolvedUsing() const {
4002 return UnresolvedMemberExprBits.HasUnresolvedUsing;
4003 }
4004
4005 /// Determine whether this member expression used the '->'
4006 /// operator; otherwise, it used the '.' operator.
4007 bool isArrow() const { return UnresolvedMemberExprBits.IsArrow; }
4008
4009 /// Retrieve the location of the '->' or '.' operator.
4010 SourceLocation getOperatorLoc() const { return OperatorLoc; }
4011
4012 /// Retrieve the naming class of this lookup.
4015 return const_cast<UnresolvedMemberExpr *>(this)->getNamingClass();
4016 }
4017
4018 /// Retrieve the full name info for the member that this expression
4019 /// refers to.
4021
4022 /// Retrieve the name of the member that this expression refers to.
4024
4025 /// Retrieve the location of the name of the member that this
4026 /// expression refers to.
4028
4029 /// Return the preferred location (the member name) for the arrow when
4030 /// diagnosing a problem with this expression.
4031 SourceLocation getExprLoc() const LLVM_READONLY { return getMemberLoc(); }
4032
4033 SourceLocation getBeginLoc() const LLVM_READONLY {
4034 if (!isImplicitAccess())
4035 return Base->getBeginLoc();
4037 return l.getBeginLoc();
4038 return getMemberNameInfo().getBeginLoc();
4039 }
4040
4041 SourceLocation getEndLoc() const LLVM_READONLY {
4043 return getRAngleLoc();
4044 return getMemberNameInfo().getEndLoc();
4045 }
4046
4047 static bool classof(const Stmt *T) {
4048 return T->getStmtClass() == UnresolvedMemberExprClass;
4049 }
4050
4051 // Iterators
4053 if (isImplicitAccess())
4055 return child_range(&Base, &Base + 1);
4056 }
4057
4059 if (isImplicitAccess())
4061 return const_child_range(&Base, &Base + 1);
4062 }
4063};
4064
4066 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4067 return ULE->getTrailingObjects<DeclAccessPair>();
4068 return cast<UnresolvedMemberExpr>(this)->getTrailingObjects<DeclAccessPair>();
4069}
4070
4073 return nullptr;
4074
4075 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4076 return ULE->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
4077 return cast<UnresolvedMemberExpr>(this)
4078 ->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
4079}
4080
4082 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4083 return ULE->getTrailingObjects<TemplateArgumentLoc>();
4084 return cast<UnresolvedMemberExpr>(this)
4085 ->getTrailingObjects<TemplateArgumentLoc>();
4086}
4087
4089 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4090 return ULE->getNamingClass();
4091 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
4092}
4093
4094/// Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
4095///
4096/// The noexcept expression tests whether a given expression might throw. Its
4097/// result is a boolean constant.
4098class CXXNoexceptExpr : public Expr {
4099 friend class ASTStmtReader;
4100
4101 Stmt *Operand;
4102 SourceRange Range;
4103
4104public:
4106 SourceLocation Keyword, SourceLocation RParen)
4107 : Expr(CXXNoexceptExprClass, Ty, VK_PRValue, OK_Ordinary),
4108 Operand(Operand), Range(Keyword, RParen) {
4109 CXXNoexceptExprBits.Value = Val == CT_Cannot;
4110 setDependence(computeDependence(this, Val));
4111 }
4112
4113 CXXNoexceptExpr(EmptyShell Empty) : Expr(CXXNoexceptExprClass, Empty) {}
4114
4115 Expr *getOperand() const { return static_cast<Expr *>(Operand); }
4116
4117 SourceLocation getBeginLoc() const { return Range.getBegin(); }
4118 SourceLocation getEndLoc() const { return Range.getEnd(); }
4119 SourceRange getSourceRange() const { return Range; }
4120
4121 bool getValue() const { return CXXNoexceptExprBits.Value; }
4122
4123 static bool classof(const Stmt *T) {
4124 return T->getStmtClass() == CXXNoexceptExprClass;
4125 }
4126
4127 // Iterators
4128 child_range children() { return child_range(&Operand, &Operand + 1); }
4129
4131 return const_child_range(&Operand, &Operand + 1);
4132 }
4133};
4134
4135/// Represents a C++11 pack expansion that produces a sequence of
4136/// expressions.
4137///
4138/// A pack expansion expression contains a pattern (which itself is an
4139/// expression) followed by an ellipsis. For example:
4140///
4141/// \code
4142/// template<typename F, typename ...Types>
4143/// void forward(F f, Types &&...args) {
4144/// f(static_cast<Types&&>(args)...);
4145/// }
4146/// \endcode
4147///
4148/// Here, the argument to the function object \c f is a pack expansion whose
4149/// pattern is \c static_cast<Types&&>(args). When the \c forward function
4150/// template is instantiated, the pack expansion will instantiate to zero or
4151/// or more function arguments to the function object \c f.
4152class PackExpansionExpr : public Expr {
4153 friend class ASTStmtReader;
4154 friend class ASTStmtWriter;
4155
4156 SourceLocation EllipsisLoc;
4157
4158 /// The number of expansions that will be produced by this pack
4159 /// expansion expression, if known.
4160 ///
4161 /// When zero, the number of expansions is not known. Otherwise, this value
4162 /// is the number of expansions + 1.
4163 unsigned NumExpansions;
4164
4165 Stmt *Pattern;
4166
4167public:
4169 std::optional<unsigned> NumExpansions)
4170 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
4171 Pattern->getObjectKind()),
4172 EllipsisLoc(EllipsisLoc),
4173 NumExpansions(NumExpansions ? *NumExpansions + 1 : 0),
4174 Pattern(Pattern) {
4176 }
4177
4178 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) {}
4179
4180 /// Retrieve the pattern of the pack expansion.
4181 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); }
4182
4183 /// Retrieve the pattern of the pack expansion.
4184 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); }
4185
4186 /// Retrieve the location of the ellipsis that describes this pack
4187 /// expansion.
4188 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
4189
4190 /// Determine the number of expansions that will be produced when
4191 /// this pack expansion is instantiated, if already known.
4192 std::optional<unsigned> getNumExpansions() const {
4193 if (NumExpansions)
4194 return NumExpansions - 1;
4195
4196 return std::nullopt;
4197 }
4198
4199 SourceLocation getBeginLoc() const LLVM_READONLY {
4200 return Pattern->getBeginLoc();
4201 }
4202
4203 SourceLocation getEndLoc() const LLVM_READONLY { return EllipsisLoc; }
4204
4205 static bool classof(const Stmt *T) {
4206 return T->getStmtClass() == PackExpansionExprClass;
4207 }
4208
4209 // Iterators
4211 return child_range(&Pattern, &Pattern + 1);
4212 }
4213
4215 return const_child_range(&Pattern, &Pattern + 1);
4216 }
4217};
4218
4219/// Represents an expression that computes the length of a parameter
4220/// pack.
4221///
4222/// \code
4223/// template<typename ...Types>
4224/// struct count {
4225/// static const unsigned value = sizeof...(Types);
4226/// };
4227/// \endcode
4229 : public Expr,
4230 private llvm::TrailingObjects<SizeOfPackExpr, TemplateArgument> {
4231 friend class ASTStmtReader;
4232 friend class ASTStmtWriter;
4233 friend TrailingObjects;
4234
4235 /// The location of the \c sizeof keyword.
4236 SourceLocation OperatorLoc;
4237
4238 /// The location of the name of the parameter pack.
4239 SourceLocation PackLoc;
4240
4241 /// The location of the closing parenthesis.
4242 SourceLocation RParenLoc;
4243
4244 /// The length of the parameter pack, if known.
4245 ///
4246 /// When this expression is not value-dependent, this is the length of
4247 /// the pack. When the expression was parsed rather than instantiated
4248 /// (and thus is value-dependent), this is zero.
4249 ///
4250 /// After partial substitution into a sizeof...(X) expression (for instance,
4251 /// within an alias template or during function template argument deduction),
4252 /// we store a trailing array of partially-substituted TemplateArguments,
4253 /// and this is the length of that array.
4254 unsigned Length;
4255
4256 /// The parameter pack.
4257 NamedDecl *Pack = nullptr;
4258
4259 /// Create an expression that computes the length of
4260 /// the given parameter pack.
4261 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack,
4262 SourceLocation PackLoc, SourceLocation RParenLoc,
4263 std::optional<unsigned> Length,
4264 ArrayRef<TemplateArgument> PartialArgs)
4265 : Expr(SizeOfPackExprClass, SizeType, VK_PRValue, OK_Ordinary),
4266 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc),
4267 Length(Length ? *Length : PartialArgs.size()), Pack(Pack) {
4268 assert((!Length || PartialArgs.empty()) &&
4269 "have partial args for non-dependent sizeof... expression");
4270 auto *Args = getTrailingObjects<TemplateArgument>();
4271 std::uninitialized_copy(PartialArgs.begin(), PartialArgs.end(), Args);
4272 setDependence(Length ? ExprDependence::None
4273 : ExprDependence::ValueInstantiation);
4274 }
4275
4276 /// Create an empty expression.
4277 SizeOfPackExpr(EmptyShell Empty, unsigned NumPartialArgs)
4278 : Expr(SizeOfPackExprClass, Empty), Length(NumPartialArgs) {}
4279
4280public:
4281 static SizeOfPackExpr *
4282 Create(ASTContext &Context, SourceLocation OperatorLoc, NamedDecl *Pack,
4283 SourceLocation PackLoc, SourceLocation RParenLoc,
4284 std::optional<unsigned> Length = std::nullopt,
4285 ArrayRef<TemplateArgument> PartialArgs = std::nullopt);
4286 static SizeOfPackExpr *CreateDeserialized(ASTContext &Context,
4287 unsigned NumPartialArgs);
4288
4289 /// Determine the location of the 'sizeof' keyword.
4290 SourceLocation getOperatorLoc() const { return OperatorLoc; }
4291
4292 /// Determine the location of the parameter pack.
4293 SourceLocation getPackLoc() const { return PackLoc; }
4294
4295 /// Determine the location of the right parenthesis.
4296 SourceLocation getRParenLoc() const { return RParenLoc; }
4297
4298 /// Retrieve the parameter pack.
4299 NamedDecl *getPack() const { return Pack; }
4300
4301 /// Retrieve the length of the parameter pack.
4302 ///
4303 /// This routine may only be invoked when the expression is not
4304 /// value-dependent.
4305 unsigned getPackLength() const {
4306 assert(!isValueDependent() &&
4307 "Cannot get the length of a value-dependent pack size expression");
4308 return Length;
4309 }
4310
4311 /// Determine whether this represents a partially-substituted sizeof...
4312 /// expression, such as is produced for:
4313 ///
4314 /// template<typename ...Ts> using X = int[sizeof...(Ts)];
4315 /// template<typename ...Us> void f(X<Us..., 1, 2, 3, Us...>);
4317 return isValueDependent() && Length;
4318 }
4319
4320 /// Get
4322 assert(isPartiallySubstituted());
4323 const auto *Args = getTrailingObjects<TemplateArgument>();
4324 return llvm::ArrayRef(Args, Args + Length);
4325 }
4326
4327 SourceLocation getBeginLoc() const LLVM_READONLY { return OperatorLoc; }
4328 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
4329
4330 static bool classof(const Stmt *T) {
4331 return T->getStmtClass() == SizeOfPackExprClass;
4332 }
4333
4334 // Iterators
4337 }
4338
4341 }
4342};
4343
4345 : public Expr,
4346 private llvm::TrailingObjects<PackIndexingExpr, Expr *> {
4347 friend class ASTStmtReader;
4348 friend class ASTStmtWriter;
4349 friend TrailingObjects;
4350
4351 SourceLocation EllipsisLoc;
4352
4353 // The location of the closing bracket
4354 SourceLocation RSquareLoc;
4355
4356 // The pack being indexed, followed by the index
4357 Stmt *SubExprs[2];
4358
4359 size_t TransformedExpressions;
4360
4362 SourceLocation RSquareLoc, Expr *PackIdExpr, Expr *IndexExpr,
4363 ArrayRef<Expr *> SubstitutedExprs = {})
4364 : Expr(PackIndexingExprClass, Type, VK_LValue, OK_Ordinary),
4365 EllipsisLoc(EllipsisLoc), RSquareLoc(RSquareLoc),
4366 SubExprs{PackIdExpr, IndexExpr},
4367 TransformedExpressions(SubstitutedExprs.size()) {
4368
4369 auto *Exprs = getTrailingObjects<Expr *>();
4370 std::uninitialized_copy(SubstitutedExprs.begin(), SubstitutedExprs.end(),
4371 Exprs);
4372
4376 }
4377
4378 /// Create an empty expression.
4379 PackIndexingExpr(EmptyShell Empty) : Expr(PackIndexingExprClass, Empty) {}
4380
4381 unsigned numTrailingObjects(OverloadToken<Expr *>) const {
4382 return TransformedExpressions;
4383 }
4384
4385public:
4386 static PackIndexingExpr *Create(ASTContext &Context,
4387 SourceLocation EllipsisLoc,
4388 SourceLocation RSquareLoc, Expr *PackIdExpr,
4389 Expr *IndexExpr, std::optional<int64_t> Index,
4390 ArrayRef<Expr *> SubstitutedExprs = {});
4391 static PackIndexingExpr *CreateDeserialized(ASTContext &Context,
4392 unsigned NumTransformedExprs);
4393
4394 /// Determine the location of the 'sizeof' keyword.
4395 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
4396
4397 /// Determine the location of the parameter pack.
4398 SourceLocation getPackLoc() const { return SubExprs[0]->getBeginLoc(); }
4399
4400 /// Determine the location of the right parenthesis.
4401 SourceLocation getRSquareLoc() const { return RSquareLoc; }
4402
4403 SourceLocation getBeginLoc() const LLVM_READONLY { return getPackLoc(); }
4404 SourceLocation getEndLoc() const LLVM_READONLY { return RSquareLoc; }
4405
4406 Expr *getPackIdExpression() const { return cast<Expr>(SubExprs[0]); }
4407
4408 NamedDecl *getPackDecl() const;
4409
4410 Expr *getIndexExpr() const { return cast<Expr>(SubExprs[1]); }
4411
4412 std::optional<unsigned> getSelectedIndex() const {
4414 return std::nullopt;
4415 ConstantExpr *CE = cast<ConstantExpr>(getIndexExpr());
4416 auto Index = CE->getResultAsAPSInt();
4417 assert(Index.isNonNegative() && "Invalid index");
4418 return static_cast<unsigned>(Index.getExtValue());
4419 }
4420
4422 std::optional<unsigned> Index = getSelectedIndex();
4423 assert(Index && "extracting the indexed expression of a dependant pack");
4424 return getTrailingObjects<Expr *>()[*Index];
4425 }
4426
4428 return {getTrailingObjects<Expr *>(), TransformedExpressions};
4429 }
4430
4431 static bool classof(const Stmt *T) {
4432 return T->getStmtClass() == PackIndexingExprClass;
4433 }
4434
4435 // Iterators
4436 child_range children() { return child_range(SubExprs, SubExprs + 2); }
4437
4439 return const_child_range(SubExprs, SubExprs + 2);
4440 }
4441};
4442
4443/// Represents a reference to a non-type template parameter
4444/// that has been substituted with a template argument.
4446 friend class ASTReader;
4447 friend class ASTStmtReader;
4448
4449 /// The replacement expression.
4450 Stmt *Replacement;
4451
4452 /// The associated declaration and a flag indicating if it was a reference
4453 /// parameter. For class NTTPs, we can't determine that based on the value
4454 /// category alone.
4455 llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndRef;
4456
4457 unsigned Index : 15;
4458 unsigned PackIndex : 16;
4459
4461 : Expr(SubstNonTypeTemplateParmExprClass, Empty) {}
4462
4463public:
4465 SourceLocation Loc, Expr *Replacement,
4466 Decl *AssociatedDecl, unsigned Index,
4467 std::optional<unsigned> PackIndex, bool RefParam)
4468 : Expr(SubstNonTypeTemplateParmExprClass, Ty, ValueKind, OK_Ordinary),
4469 Replacement(Replacement),
4470 AssociatedDeclAndRef(AssociatedDecl, RefParam), Index(Index),
4471 PackIndex(PackIndex ? *PackIndex + 1 : 0) {
4472 assert(AssociatedDecl != nullptr);
4475 }
4476
4478 return SubstNonTypeTemplateParmExprBits.NameLoc;
4479 }
4482
4483 Expr *getReplacement() const { return cast<Expr>(Replacement); }
4484
4485 /// A template-like entity which owns the whole pattern being substituted.