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
17 #include "clang/AST/ASTConcept.h"
18 #include "clang/AST/ComputeDependence.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclBase.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclTemplate.h"
23 #include "clang/AST/DeclarationName.h"
24 #include "clang/AST/DependenceFlags.h"
25 #include "clang/AST/Expr.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/OperationKinds.h"
28 #include "clang/AST/Stmt.h"
29 #include "clang/AST/StmtCXX.h"
30 #include "clang/AST/TemplateBase.h"
31 #include "clang/AST/Type.h"
32 #include "clang/AST/UnresolvedSet.h"
33 #include "clang/Basic/ExceptionSpecificationType.h"
34 #include "clang/Basic/ExpressionTraits.h"
35 #include "clang/Basic/LLVM.h"
36 #include "clang/Basic/Lambda.h"
37 #include "clang/Basic/LangOptions.h"
38 #include "clang/Basic/OperatorKinds.h"
39 #include "clang/Basic/SourceLocation.h"
40 #include "clang/Basic/Specifiers.h"
41 #include "clang/Basic/TypeTraits.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
55 namespace clang {
56
57 class ASTContext;
58 class DeclAccessPair;
59 class IdentifierInfo;
60 class LambdaCapture;
61 class NonTypeTemplateParmDecl;
62 class 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.
81 class 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
92 CXXOperatorCallExpr(OverloadedOperatorKind OpKind, Expr *Fn,
93 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
94 SourceLocation OperatorLoc, FPOptionsOverride FPFeatures,
95 ADLCallKind UsesADL);
96
97 CXXOperatorCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
98
99 public:
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,
104 ADLCallKind UsesADL = NotADL);
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.
getOperator()111 OverloadedOperatorKind getOperator() const {
112 return static_cast<OverloadedOperatorKind>(
113 CXXOperatorCallExprBits.OperatorKind);
114 }
115
isAssignmentOp(OverloadedOperatorKind Opc)116 static bool isAssignmentOp(OverloadedOperatorKind Opc) {
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 }
isAssignmentOp()123 bool isAssignmentOp() const { return isAssignmentOp(getOperator()); }
124
isComparisonOp(OverloadedOperatorKind Opc)125 static bool isComparisonOp(OverloadedOperatorKind Opc) {
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 }
isComparisonOp()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.
getOperatorLoc()149 SourceLocation getOperatorLoc() const { return getRParenLoc(); }
150
getExprLoc()151 SourceLocation getExprLoc() const LLVM_READONLY {
152 OverloadedOperatorKind Operator = getOperator();
153 return (Operator < OO_Plus || Operator >= OO_Arrow ||
154 Operator == OO_PlusPlus || Operator == OO_MinusMinus)
155 ? getBeginLoc()
156 : getOperatorLoc();
157 }
158
getBeginLoc()159 SourceLocation getBeginLoc() const { return Range.getBegin(); }
getEndLoc()160 SourceLocation getEndLoc() const { return Range.getEnd(); }
getSourceRange()161 SourceRange getSourceRange() const { return Range; }
162
classof(const Stmt * T)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).
176 class CXXMemberCallExpr final : public CallExpr {
177 // CXXMemberCallExpr has some trailing objects belonging
178 // to CallExpr. See CallExpr for the details.
179
180 CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args, QualType Ty,
181 ExprValueKind VK, SourceLocation RP,
182 FPOptionsOverride FPOptions, unsigned MinNumArgs);
183
184 CXXMemberCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
185
186 public:
187 static CXXMemberCallExpr *Create(const ASTContext &Ctx, Expr *Fn,
188 ArrayRef<Expr *> Args, QualType Ty,
189 ExprValueKind VK, SourceLocation RP,
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".
199 Expr *getImplicitObjectArgument() const;
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.
207 CXXMethodDecl *getMethodDecl() const;
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.
215 CXXRecordDecl *getRecordDecl() const;
216
getExprLoc()217 SourceLocation getExprLoc() const LLVM_READONLY {
218 SourceLocation CLoc = getCallee()->getExprLoc();
219 if (CLoc.isValid())
220 return CLoc;
221
222 return getBeginLoc();
223 }
224
classof(const Stmt * T)225 static bool classof(const Stmt *T) {
226 return T->getStmtClass() == CXXMemberCallExprClass;
227 }
228 };
229
230 /// Represents a call to a CUDA kernel function.
231 class 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
239 CUDAKernelCallExpr(Expr *Fn, CallExpr *Config, ArrayRef<Expr *> Args,
240 QualType Ty, ExprValueKind VK, SourceLocation RP,
241 FPOptionsOverride FPFeatures, unsigned MinNumArgs);
242
243 CUDAKernelCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
244
245 public:
246 static CUDAKernelCallExpr *Create(const ASTContext &Ctx, Expr *Fn,
247 CallExpr *Config, ArrayRef<Expr *> Args,
248 QualType Ty, ExprValueKind VK,
249 SourceLocation RP,
250 FPOptionsOverride FPFeatures,
251 unsigned MinNumArgs = 0);
252
253 static CUDAKernelCallExpr *CreateEmpty(const ASTContext &Ctx,
254 unsigned NumArgs, bool HasFPFeatures,
255 EmptyShell Empty);
256
getConfig()257 const CallExpr *getConfig() const {
258 return cast_or_null<CallExpr>(getPreArg(CONFIG));
259 }
getConfig()260 CallExpr *getConfig() { return cast_or_null<CallExpr>(getPreArg(CONFIG)); }
261
classof(const Stmt * T)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.
283 class CXXRewrittenBinaryOperator : public Expr {
284 friend class ASTStmtReader;
285
286 /// The rewritten semantic form.
287 Stmt *SemanticForm;
288
289 public:
CXXRewrittenBinaryOperator(Expr * SemanticForm,bool IsReversed)290 CXXRewrittenBinaryOperator(Expr *SemanticForm, bool IsReversed)
291 : Expr(CXXRewrittenBinaryOperatorClass, SemanticForm->getType(),
292 SemanticForm->getValueKind(), SemanticForm->getObjectKind()),
293 SemanticForm(SemanticForm) {
294 CXXRewrittenBinaryOperatorBits.IsReversed = IsReversed;
295 setDependence(computeDependence(this));
296 }
CXXRewrittenBinaryOperator(EmptyShell Empty)297 CXXRewrittenBinaryOperator(EmptyShell Empty)
298 : Expr(CXXRewrittenBinaryOperatorClass, Empty), SemanticForm() {}
299
300 /// Get an equivalent semantic form for this expression.
getSemanticForm()301 Expr *getSemanticForm() { return cast<Expr>(SemanticForm); }
getSemanticForm()302 const Expr *getSemanticForm() const { return cast<Expr>(SemanticForm); }
303
304 struct DecomposedForm {
305 /// The original opcode, prior to rewriting.
306 BinaryOperatorKind Opcode;
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.
312 const Expr *InnerBinOp;
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.
isReversed()319 bool isReversed() const { return CXXRewrittenBinaryOperatorBits.IsReversed; }
320
getOperator()321 BinaryOperatorKind getOperator() const { return getDecomposedForm().Opcode; }
getOpcode()322 BinaryOperatorKind getOpcode() const { return getOperator(); }
getOpcodeStr(BinaryOperatorKind Op)323 static StringRef getOpcodeStr(BinaryOperatorKind Op) {
324 return BinaryOperator::getOpcodeStr(Op);
325 }
getOpcodeStr()326 StringRef getOpcodeStr() const {
327 return BinaryOperator::getOpcodeStr(getOpcode());
328 }
isComparisonOp()329 bool isComparisonOp() const { return true; }
isAssignmentOp()330 bool isAssignmentOp() const { return false; }
331
getLHS()332 const Expr *getLHS() const { return getDecomposedForm().LHS; }
getRHS()333 const Expr *getRHS() const { return getDecomposedForm().RHS; }
334
getOperatorLoc()335 SourceLocation getOperatorLoc() const LLVM_READONLY {
336 return getDecomposedForm().InnerBinOp->getExprLoc();
337 }
getExprLoc()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 //@{
getBeginLoc()344 SourceLocation getBeginLoc() const LLVM_READONLY {
345 return getDecomposedForm().LHS->getBeginLoc();
346 }
getEndLoc()347 SourceLocation getEndLoc() const LLVM_READONLY {
348 return getDecomposedForm().RHS->getEndLoc();
349 }
getSourceRange()350 SourceRange getSourceRange() const LLVM_READONLY {
351 DecomposedForm DF = getDecomposedForm();
352 return SourceRange(DF.LHS->getBeginLoc(), DF.RHS->getEndLoc());
353 }
354 //@}
355
children()356 child_range children() {
357 return child_range(&SemanticForm, &SemanticForm + 1);
358 }
359
classof(const Stmt * T)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).
372 class CXXNamedCastExpr : public ExplicitCastExpr {
373 private:
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
383 protected:
384 friend class ASTStmtReader;
385
CXXNamedCastExpr(StmtClass SC,QualType ty,ExprValueKind VK,CastKind kind,Expr * op,unsigned PathSize,bool HasFPFeatures,TypeSourceInfo * writtenTy,SourceLocation l,SourceLocation RParenLoc,SourceRange AngleBrackets)386 CXXNamedCastExpr(StmtClass SC, QualType ty, ExprValueKind VK, CastKind kind,
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
CXXNamedCastExpr(StmtClass SC,EmptyShell Shell,unsigned PathSize,bool HasFPFeatures)394 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize,
395 bool HasFPFeatures)
396 : ExplicitCastExpr(SC, Shell, PathSize, HasFPFeatures) {}
397
398 public:
399 const char *getCastName() const;
400
401 /// Retrieve the location of the cast operator keyword, e.g.,
402 /// \c static_cast.
getOperatorLoc()403 SourceLocation getOperatorLoc() const { return Loc; }
404
405 /// Retrieve the location of the closing parenthesis.
getRParenLoc()406 SourceLocation getRParenLoc() const { return RParenLoc; }
407
getBeginLoc()408 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
getEndLoc()409 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
getAngleBrackets()410 SourceRange getAngleBrackets() const LLVM_READONLY { return AngleBrackets; }
411
classof(const Stmt * T)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).
430 class CXXStaticCastExpr final
431 : public CXXNamedCastExpr,
432 private llvm::TrailingObjects<CXXStaticCastExpr, CXXBaseSpecifier *,
433 FPOptionsOverride> {
CXXStaticCastExpr(QualType ty,ExprValueKind vk,CastKind kind,Expr * op,unsigned pathSize,TypeSourceInfo * writtenTy,FPOptionsOverride FPO,SourceLocation l,SourceLocation RParenLoc,SourceRange AngleBrackets)434 CXXStaticCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op,
435 unsigned pathSize, TypeSourceInfo *writtenTy,
436 FPOptionsOverride FPO, SourceLocation l,
437 SourceLocation RParenLoc, SourceRange AngleBrackets)
438 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize,
439 FPO.requiresTrailingStorage(), writtenTy, l, RParenLoc,
440 AngleBrackets) {
441 if (hasStoredFPFeatures())
442 *getTrailingFPFeatures() = FPO;
443 }
444
CXXStaticCastExpr(EmptyShell Empty,unsigned PathSize,bool HasFPFeatures)445 explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize,
446 bool HasFPFeatures)
447 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize,
448 HasFPFeatures) {}
449
numTrailingObjects(OverloadToken<CXXBaseSpecifier * >)450 unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const {
451 return path_size();
452 }
453
454 public:
455 friend class CastExpr;
456 friend TrailingObjects;
457
458 static CXXStaticCastExpr *
459 Create(const ASTContext &Context, QualType T, ExprValueKind VK, CastKind K,
460 Expr *Op, const CXXCastPath *Path, TypeSourceInfo *Written,
461 FPOptionsOverride FPO, SourceLocation L, SourceLocation RParenLoc,
462 SourceRange AngleBrackets);
463 static CXXStaticCastExpr *CreateEmpty(const ASTContext &Context,
464 unsigned PathSize, bool hasFPFeatures);
465
classof(const Stmt * T)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.
476 class CXXDynamicCastExpr final
477 : public CXXNamedCastExpr,
478 private llvm::TrailingObjects<CXXDynamicCastExpr, CXXBaseSpecifier *> {
CXXDynamicCastExpr(QualType ty,ExprValueKind VK,CastKind kind,Expr * op,unsigned pathSize,TypeSourceInfo * writtenTy,SourceLocation l,SourceLocation RParenLoc,SourceRange AngleBrackets)479 CXXDynamicCastExpr(QualType ty, ExprValueKind VK, CastKind kind, Expr *op,
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
CXXDynamicCastExpr(EmptyShell Empty,unsigned pathSize)487 explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize)
488 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize,
489 /*HasFPFeatures*/ false) {}
490
491 public:
492 friend class CastExpr;
493 friend TrailingObjects;
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
classof(const Stmt * T)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.
520 class CXXReinterpretCastExpr final
521 : public CXXNamedCastExpr,
522 private llvm::TrailingObjects<CXXReinterpretCastExpr,
523 CXXBaseSpecifier *> {
CXXReinterpretCastExpr(QualType ty,ExprValueKind vk,CastKind kind,Expr * op,unsigned pathSize,TypeSourceInfo * writtenTy,SourceLocation l,SourceLocation RParenLoc,SourceRange AngleBrackets)524 CXXReinterpretCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op,
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
CXXReinterpretCastExpr(EmptyShell Empty,unsigned pathSize)532 CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize)
533 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize,
534 /*HasFPFeatures*/ false) {}
535
536 public:
537 friend class CastExpr;
538 friend TrailingObjects;
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
classof(const Stmt * T)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.
561 class CXXConstCastExpr final
562 : public CXXNamedCastExpr,
563 private llvm::TrailingObjects<CXXConstCastExpr, CXXBaseSpecifier *> {
CXXConstCastExpr(QualType ty,ExprValueKind VK,Expr * op,TypeSourceInfo * writtenTy,SourceLocation l,SourceLocation RParenLoc,SourceRange AngleBrackets)564 CXXConstCastExpr(QualType ty, ExprValueKind VK, Expr *op,
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
CXXConstCastExpr(EmptyShell Empty)571 explicit CXXConstCastExpr(EmptyShell Empty)
572 : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0,
573 /*HasFPFeatures*/ false) {}
574
575 public:
576 friend class CastExpr;
577 friend TrailingObjects;
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
classof(const Stmt * T)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.
599 class CXXAddrspaceCastExpr final
600 : public CXXNamedCastExpr,
601 private llvm::TrailingObjects<CXXAddrspaceCastExpr, CXXBaseSpecifier *> {
CXXAddrspaceCastExpr(QualType ty,ExprValueKind VK,CastKind Kind,Expr * op,TypeSourceInfo * writtenTy,SourceLocation l,SourceLocation RParenLoc,SourceRange AngleBrackets)602 CXXAddrspaceCastExpr(QualType ty, ExprValueKind VK, CastKind Kind, Expr *op,
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
CXXAddrspaceCastExpr(EmptyShell Empty)609 explicit CXXAddrspaceCastExpr(EmptyShell Empty)
610 : CXXNamedCastExpr(CXXAddrspaceCastExprClass, Empty, 0,
611 /*HasFPFeatures*/ false) {}
612
613 public:
614 friend class CastExpr;
615 friend TrailingObjects;
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
classof(const Stmt * T)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.
637 class 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
647 UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args, QualType Ty,
648 ExprValueKind VK, SourceLocation LitEndLoc,
649 SourceLocation SuffixLoc, FPOptionsOverride FPFeatures);
650
651 UserDefinedLiteral(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
652
653 public:
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.
665 enum LiteralOperatorKind {
666 /// Raw form: operator "" X (const char *)
667 LOK_Raw,
668
669 /// Raw form: operator "" X<cs...> ()
670 LOK_Template,
671
672 /// operator "" X (unsigned long long)
673 LOK_Integer,
674
675 /// operator "" X (long double)
676 LOK_Floating,
677
678 /// operator "" X (const CharT *, size_t)
679 LOK_String,
680
681 /// operator "" X (CharT)
682 LOK_Character
683 };
684
685 /// Returns the kind of literal operator invocation
686 /// which this expression represents.
687 LiteralOperatorKind getLiteralOperatorKind() const;
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).
692 Expr *getCookedLiteral();
getCookedLiteral()693 const Expr *getCookedLiteral() const {
694 return const_cast<UserDefinedLiteral*>(this)->getCookedLiteral();
695 }
696
getBeginLoc()697 SourceLocation getBeginLoc() const {
698 if (getLiteralOperatorKind() == LOK_Template)
699 return getRParenLoc();
700 return getArg(0)->getBeginLoc();
701 }
702
getEndLoc()703 SourceLocation getEndLoc() const { return getRParenLoc(); }
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.
getUDSuffixLoc()709 SourceLocation getUDSuffixLoc() const { return UDSuffixLoc; }
710
711 /// Returns the ud-suffix specified for this literal.
712 const IdentifierInfo *getUDSuffix() const;
713
classof(const Stmt * S)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).
720 class CXXBoolLiteralExpr : public Expr {
721 public:
CXXBoolLiteralExpr(bool Val,QualType Ty,SourceLocation Loc)722 CXXBoolLiteralExpr(bool Val, QualType Ty, SourceLocation Loc)
723 : Expr(CXXBoolLiteralExprClass, Ty, VK_PRValue, OK_Ordinary) {
724 CXXBoolLiteralExprBits.Value = Val;
725 CXXBoolLiteralExprBits.Loc = Loc;
726 setDependence(ExprDependence::None);
727 }
728
CXXBoolLiteralExpr(EmptyShell Empty)729 explicit CXXBoolLiteralExpr(EmptyShell Empty)
730 : Expr(CXXBoolLiteralExprClass, Empty) {}
731
Create(const ASTContext & C,bool Val,QualType Ty,SourceLocation Loc)732 static CXXBoolLiteralExpr *Create(const ASTContext &C, bool Val, QualType Ty,
733 SourceLocation Loc) {
734 return new (C) CXXBoolLiteralExpr(Val, Ty, Loc);
735 }
736
getValue()737 bool getValue() const { return CXXBoolLiteralExprBits.Value; }
setValue(bool V)738 void setValue(bool V) { CXXBoolLiteralExprBits.Value = V; }
739
getBeginLoc()740 SourceLocation getBeginLoc() const { return getLocation(); }
getEndLoc()741 SourceLocation getEndLoc() const { return getLocation(); }
742
getLocation()743 SourceLocation getLocation() const { return CXXBoolLiteralExprBits.Loc; }
setLocation(SourceLocation L)744 void setLocation(SourceLocation L) { CXXBoolLiteralExprBits.Loc = L; }
745
classof(const Stmt * T)746 static bool classof(const Stmt *T) {
747 return T->getStmtClass() == CXXBoolLiteralExprClass;
748 }
749
750 // Iterators
children()751 child_range children() {
752 return child_range(child_iterator(), child_iterator());
753 }
754
children()755 const_child_range children() const {
756 return const_child_range(const_child_iterator(), const_child_iterator());
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++.
765 class CXXNullPtrLiteralExpr : public Expr {
766 public:
CXXNullPtrLiteralExpr(QualType Ty,SourceLocation Loc)767 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation Loc)
768 : Expr(CXXNullPtrLiteralExprClass, Ty, VK_PRValue, OK_Ordinary) {
769 CXXNullPtrLiteralExprBits.Loc = Loc;
770 setDependence(ExprDependence::None);
771 }
772
CXXNullPtrLiteralExpr(EmptyShell Empty)773 explicit CXXNullPtrLiteralExpr(EmptyShell Empty)
774 : Expr(CXXNullPtrLiteralExprClass, Empty) {}
775
getBeginLoc()776 SourceLocation getBeginLoc() const { return getLocation(); }
getEndLoc()777 SourceLocation getEndLoc() const { return getLocation(); }
778
getLocation()779 SourceLocation getLocation() const { return CXXNullPtrLiteralExprBits.Loc; }
setLocation(SourceLocation L)780 void setLocation(SourceLocation L) { CXXNullPtrLiteralExprBits.Loc = L; }
781
classof(const Stmt * T)782 static bool classof(const Stmt *T) {
783 return T->getStmtClass() == CXXNullPtrLiteralExprClass;
784 }
785
children()786 child_range children() {
787 return child_range(child_iterator(), child_iterator());
788 }
789
children()790 const_child_range children() const {
791 return const_child_range(const_child_iterator(), const_child_iterator());
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).
797 class CXXStdInitializerListExpr : public Expr {
798 Stmt *SubExpr = nullptr;
799
CXXStdInitializerListExpr(EmptyShell Empty)800 CXXStdInitializerListExpr(EmptyShell Empty)
801 : Expr(CXXStdInitializerListExprClass, Empty) {}
802
803 public:
804 friend class ASTReader;
805 friend class ASTStmtReader;
806
CXXStdInitializerListExpr(QualType Ty,Expr * SubExpr)807 CXXStdInitializerListExpr(QualType Ty, Expr *SubExpr)
808 : Expr(CXXStdInitializerListExprClass, Ty, VK_PRValue, OK_Ordinary),
809 SubExpr(SubExpr) {
810 setDependence(computeDependence(this));
811 }
812
getSubExpr()813 Expr *getSubExpr() { return static_cast<Expr*>(SubExpr); }
getSubExpr()814 const Expr *getSubExpr() const { return static_cast<const Expr*>(SubExpr); }
815
getBeginLoc()816 SourceLocation getBeginLoc() const LLVM_READONLY {
817 return SubExpr->getBeginLoc();
818 }
819
getEndLoc()820 SourceLocation getEndLoc() const LLVM_READONLY {
821 return SubExpr->getEndLoc();
822 }
823
824 /// Retrieve the source range of the expression.
getSourceRange()825 SourceRange getSourceRange() const LLVM_READONLY {
826 return SubExpr->getSourceRange();
827 }
828
classof(const Stmt * S)829 static bool classof(const Stmt *S) {
830 return S->getStmtClass() == CXXStdInitializerListExprClass;
831 }
832
children()833 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
834
children()835 const_child_range children() const {
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)
845 class CXXTypeidExpr : public Expr {
846 friend class ASTStmtReader;
847
848 private:
849 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
850 SourceRange Range;
851
852 public:
CXXTypeidExpr(QualType Ty,TypeSourceInfo * Operand,SourceRange R)853 CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R)
854 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
855 Range(R) {
856 setDependence(computeDependence(this));
857 }
858
CXXTypeidExpr(QualType Ty,Expr * Operand,SourceRange R)859 CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R)
860 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
861 Range(R) {
862 setDependence(computeDependence(this));
863 }
864
CXXTypeidExpr(EmptyShell Empty,bool isExpr)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
isTypeOperand()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.
getTypeOperandSourceInfo()888 TypeSourceInfo *getTypeOperandSourceInfo() const {
889 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
890 return Operand.get<TypeSourceInfo *>();
891 }
getExprOperand()892 Expr *getExprOperand() const {
893 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
894 return static_cast<Expr*>(Operand.get<Stmt *>());
895 }
896
getBeginLoc()897 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
getEndLoc()898 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
getSourceRange()899 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
setSourceRange(SourceRange R)900 void setSourceRange(SourceRange R) { Range = R; }
901
classof(const Stmt * T)902 static bool classof(const Stmt *T) {
903 return T->getStmtClass() == CXXTypeidExprClass;
904 }
905
906 // Iterators
children()907 child_range children() {
908 if (isTypeOperand())
909 return child_range(child_iterator(), child_iterator());
910 auto **begin = reinterpret_cast<Stmt **>(&Operand);
911 return child_range(begin, begin + 1);
912 }
913
children()914 const_child_range children() const {
915 if (isTypeOperand())
916 return const_child_range(const_child_iterator(), const_child_iterator());
917
918 auto **begin =
919 reinterpret_cast<Stmt **>(&const_cast<CXXTypeidExpr *>(this)->Operand);
920 return const_child_range(begin, begin + 1);
921 }
922
923 /// Whether this is of a form like "typeid(*ptr)" that can throw a
924 /// std::bad_typeid if a pointer is a null pointer ([expr.typeid]p2)
925 bool hasNullCheck() const;
926 };
927
928 /// A member reference to an MSPropertyDecl.
929 ///
930 /// This expression always has pseudo-object type, and therefore it is
931 /// typically not encountered in a fully-typechecked expression except
932 /// within the syntactic form of a PseudoObjectExpr.
933 class MSPropertyRefExpr : public Expr {
934 Expr *BaseExpr;
935 MSPropertyDecl *TheDecl;
936 SourceLocation MemberLoc;
937 bool IsArrow;
938 NestedNameSpecifierLoc QualifierLoc;
939
940 public:
941 friend class ASTStmtReader;
942
MSPropertyRefExpr(Expr * baseExpr,MSPropertyDecl * decl,bool isArrow,QualType ty,ExprValueKind VK,NestedNameSpecifierLoc qualifierLoc,SourceLocation nameLoc)943 MSPropertyRefExpr(Expr *baseExpr, MSPropertyDecl *decl, bool isArrow,
944 QualType ty, ExprValueKind VK,
945 NestedNameSpecifierLoc qualifierLoc, SourceLocation nameLoc)
946 : Expr(MSPropertyRefExprClass, ty, VK, OK_Ordinary), BaseExpr(baseExpr),
947 TheDecl(decl), MemberLoc(nameLoc), IsArrow(isArrow),
948 QualifierLoc(qualifierLoc) {
949 setDependence(computeDependence(this));
950 }
951
MSPropertyRefExpr(EmptyShell Empty)952 MSPropertyRefExpr(EmptyShell Empty) : Expr(MSPropertyRefExprClass, Empty) {}
953
getSourceRange()954 SourceRange getSourceRange() const LLVM_READONLY {
955 return SourceRange(getBeginLoc(), getEndLoc());
956 }
957
isImplicitAccess()958 bool isImplicitAccess() const {
959 return getBaseExpr() && getBaseExpr()->isImplicitCXXThis();
960 }
961
getBeginLoc()962 SourceLocation getBeginLoc() const {
963 if (!isImplicitAccess())
964 return BaseExpr->getBeginLoc();
965 else if (QualifierLoc)
966 return QualifierLoc.getBeginLoc();
967 else
968 return MemberLoc;
969 }
970
getEndLoc()971 SourceLocation getEndLoc() const { return getMemberLoc(); }
972
children()973 child_range children() {
974 return child_range((Stmt**)&BaseExpr, (Stmt**)&BaseExpr + 1);
975 }
976
children()977 const_child_range children() const {
978 auto Children = const_cast<MSPropertyRefExpr *>(this)->children();
979 return const_child_range(Children.begin(), Children.end());
980 }
981
classof(const Stmt * T)982 static bool classof(const Stmt *T) {
983 return T->getStmtClass() == MSPropertyRefExprClass;
984 }
985
getBaseExpr()986 Expr *getBaseExpr() const { return BaseExpr; }
getPropertyDecl()987 MSPropertyDecl *getPropertyDecl() const { return TheDecl; }
isArrow()988 bool isArrow() const { return IsArrow; }
getMemberLoc()989 SourceLocation getMemberLoc() const { return MemberLoc; }
getQualifierLoc()990 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
991 };
992
993 /// MS property subscript expression.
994 /// MSVC supports 'property' attribute and allows to apply it to the
995 /// declaration of an empty array in a class or structure definition.
996 /// For example:
997 /// \code
998 /// __declspec(property(get=GetX, put=PutX)) int x[];
999 /// \endcode
1000 /// The above statement indicates that x[] can be used with one or more array
1001 /// indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), and
1002 /// p->x[a][b] = i will be turned into p->PutX(a, b, i).
1003 /// This is a syntactic pseudo-object expression.
1004 class MSPropertySubscriptExpr : public Expr {
1005 friend class ASTStmtReader;
1006
1007 enum { BASE_EXPR, IDX_EXPR, NUM_SUBEXPRS = 2 };
1008
1009 Stmt *SubExprs[NUM_SUBEXPRS];
1010 SourceLocation RBracketLoc;
1011
setBase(Expr * Base)1012 void setBase(Expr *Base) { SubExprs[BASE_EXPR] = Base; }
setIdx(Expr * Idx)1013 void setIdx(Expr *Idx) { SubExprs[IDX_EXPR] = Idx; }
1014
1015 public:
MSPropertySubscriptExpr(Expr * Base,Expr * Idx,QualType Ty,ExprValueKind VK,ExprObjectKind OK,SourceLocation RBracketLoc)1016 MSPropertySubscriptExpr(Expr *Base, Expr *Idx, QualType Ty, ExprValueKind VK,
1017 ExprObjectKind OK, SourceLocation RBracketLoc)
1018 : Expr(MSPropertySubscriptExprClass, Ty, VK, OK),
1019 RBracketLoc(RBracketLoc) {
1020 SubExprs[BASE_EXPR] = Base;
1021 SubExprs[IDX_EXPR] = Idx;
1022 setDependence(computeDependence(this));
1023 }
1024
1025 /// Create an empty array subscript expression.
MSPropertySubscriptExpr(EmptyShell Shell)1026 explicit MSPropertySubscriptExpr(EmptyShell Shell)
1027 : Expr(MSPropertySubscriptExprClass, Shell) {}
1028
getBase()1029 Expr *getBase() { return cast<Expr>(SubExprs[BASE_EXPR]); }
getBase()1030 const Expr *getBase() const { return cast<Expr>(SubExprs[BASE_EXPR]); }
1031
getIdx()1032 Expr *getIdx() { return cast<Expr>(SubExprs[IDX_EXPR]); }
getIdx()1033 const Expr *getIdx() const { return cast<Expr>(SubExprs[IDX_EXPR]); }
1034
getBeginLoc()1035 SourceLocation getBeginLoc() const LLVM_READONLY {
1036 return getBase()->getBeginLoc();
1037 }
1038
getEndLoc()1039 SourceLocation getEndLoc() const LLVM_READONLY { return RBracketLoc; }
1040
getRBracketLoc()1041 SourceLocation getRBracketLoc() const { return RBracketLoc; }
setRBracketLoc(SourceLocation L)1042 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
1043
getExprLoc()1044 SourceLocation getExprLoc() const LLVM_READONLY {
1045 return getBase()->getExprLoc();
1046 }
1047
classof(const Stmt * T)1048 static bool classof(const Stmt *T) {
1049 return T->getStmtClass() == MSPropertySubscriptExprClass;
1050 }
1051
1052 // Iterators
children()1053 child_range children() {
1054 return child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
1055 }
1056
children()1057 const_child_range children() const {
1058 return const_child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
1059 }
1060 };
1061
1062 /// A Microsoft C++ @c __uuidof expression, which gets
1063 /// the _GUID that corresponds to the supplied type or expression.
1064 ///
1065 /// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr)
1066 class CXXUuidofExpr : public Expr {
1067 friend class ASTStmtReader;
1068
1069 private:
1070 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
1071 MSGuidDecl *Guid;
1072 SourceRange Range;
1073
1074 public:
CXXUuidofExpr(QualType Ty,TypeSourceInfo * Operand,MSGuidDecl * Guid,SourceRange R)1075 CXXUuidofExpr(QualType Ty, TypeSourceInfo *Operand, MSGuidDecl *Guid,
1076 SourceRange R)
1077 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
1078 Guid(Guid), Range(R) {
1079 setDependence(computeDependence(this));
1080 }
1081
CXXUuidofExpr(QualType Ty,Expr * Operand,MSGuidDecl * Guid,SourceRange R)1082 CXXUuidofExpr(QualType Ty, Expr *Operand, MSGuidDecl *Guid, SourceRange R)
1083 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
1084 Guid(Guid), Range(R) {
1085 setDependence(computeDependence(this));
1086 }
1087
CXXUuidofExpr(EmptyShell Empty,bool isExpr)1088 CXXUuidofExpr(EmptyShell Empty, bool isExpr)
1089 : Expr(CXXUuidofExprClass, Empty) {
1090 if (isExpr)
1091 Operand = (Expr*)nullptr;
1092 else
1093 Operand = (TypeSourceInfo*)nullptr;
1094 }
1095
isTypeOperand()1096 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
1097
1098 /// Retrieves the type operand of this __uuidof() expression after
1099 /// various required adjustments (removing reference types, cv-qualifiers).
1100 QualType getTypeOperand(ASTContext &Context) const;
1101
1102 /// Retrieve source information for the type operand.
getTypeOperandSourceInfo()1103 TypeSourceInfo *getTypeOperandSourceInfo() const {
1104 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
1105 return Operand.get<TypeSourceInfo *>();
1106 }
getExprOperand()1107 Expr *getExprOperand() const {
1108 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
1109 return static_cast<Expr*>(Operand.get<Stmt *>());
1110 }
1111
getGuidDecl()1112 MSGuidDecl *getGuidDecl() const { return Guid; }
1113
getBeginLoc()1114 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
getEndLoc()1115 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
getSourceRange()1116 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
setSourceRange(SourceRange R)1117 void setSourceRange(SourceRange R) { Range = R; }
1118
classof(const Stmt * T)1119 static bool classof(const Stmt *T) {
1120 return T->getStmtClass() == CXXUuidofExprClass;
1121 }
1122
1123 // Iterators
children()1124 child_range children() {
1125 if (isTypeOperand())
1126 return child_range(child_iterator(), child_iterator());
1127 auto **begin = reinterpret_cast<Stmt **>(&Operand);
1128 return child_range(begin, begin + 1);
1129 }
1130
children()1131 const_child_range children() const {
1132 if (isTypeOperand())
1133 return const_child_range(const_child_iterator(), const_child_iterator());
1134 auto **begin =
1135 reinterpret_cast<Stmt **>(&const_cast<CXXUuidofExpr *>(this)->Operand);
1136 return const_child_range(begin, begin + 1);
1137 }
1138 };
1139
1140 /// Represents the \c this expression in C++.
1141 ///
1142 /// This is a pointer to the object on which the current member function is
1143 /// executing (C++ [expr.prim]p3). Example:
1144 ///
1145 /// \code
1146 /// class Foo {
1147 /// public:
1148 /// void bar();
1149 /// void test() { this->bar(); }
1150 /// };
1151 /// \endcode
1152 class CXXThisExpr : public Expr {
CXXThisExpr(SourceLocation L,QualType Ty,bool IsImplicit,ExprValueKind VK)1153 CXXThisExpr(SourceLocation L, QualType Ty, bool IsImplicit, ExprValueKind VK)
1154 : Expr(CXXThisExprClass, Ty, VK, OK_Ordinary) {
1155 CXXThisExprBits.IsImplicit = IsImplicit;
1156 CXXThisExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = false;
1157 CXXThisExprBits.Loc = L;
1158 setDependence(computeDependence(this));
1159 }
1160
CXXThisExpr(EmptyShell Empty)1161 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {}
1162
1163 public:
1164 static CXXThisExpr *Create(const ASTContext &Ctx, SourceLocation L,
1165 QualType Ty, bool IsImplicit);
1166
1167 static CXXThisExpr *CreateEmpty(const ASTContext &Ctx);
1168
getLocation()1169 SourceLocation getLocation() const { return CXXThisExprBits.Loc; }
setLocation(SourceLocation L)1170 void setLocation(SourceLocation L) { CXXThisExprBits.Loc = L; }
1171
getBeginLoc()1172 SourceLocation getBeginLoc() const { return getLocation(); }
getEndLoc()1173 SourceLocation getEndLoc() const { return getLocation(); }
1174
isImplicit()1175 bool isImplicit() const { return CXXThisExprBits.IsImplicit; }
setImplicit(bool I)1176 void setImplicit(bool I) { CXXThisExprBits.IsImplicit = I; }
1177
isCapturedByCopyInLambdaWithExplicitObjectParameter()1178 bool isCapturedByCopyInLambdaWithExplicitObjectParameter() const {
1179 return CXXThisExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter;
1180 }
1181
setCapturedByCopyInLambdaWithExplicitObjectParameter(bool Set)1182 void setCapturedByCopyInLambdaWithExplicitObjectParameter(bool Set) {
1183 CXXThisExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = Set;
1184 setDependence(computeDependence(this));
1185 }
1186
classof(const Stmt * T)1187 static bool classof(const Stmt *T) {
1188 return T->getStmtClass() == CXXThisExprClass;
1189 }
1190
1191 // Iterators
children()1192 child_range children() {
1193 return child_range(child_iterator(), child_iterator());
1194 }
1195
children()1196 const_child_range children() const {
1197 return const_child_range(const_child_iterator(), const_child_iterator());
1198 }
1199 };
1200
1201 /// A C++ throw-expression (C++ [except.throw]).
1202 ///
1203 /// This handles 'throw' (for re-throwing the current exception) and
1204 /// 'throw' assignment-expression. When assignment-expression isn't
1205 /// present, Op will be null.
1206 class CXXThrowExpr : public Expr {
1207 friend class ASTStmtReader;
1208
1209 /// The optional expression in the throw statement.
1210 Stmt *Operand;
1211
1212 public:
1213 // \p Ty is the void type which is used as the result type of the
1214 // expression. The \p Loc is the location of the throw keyword.
1215 // \p Operand is the expression in the throw statement, and can be
1216 // null if not present.
CXXThrowExpr(Expr * Operand,QualType Ty,SourceLocation Loc,bool IsThrownVariableInScope)1217 CXXThrowExpr(Expr *Operand, QualType Ty, SourceLocation Loc,
1218 bool IsThrownVariableInScope)
1219 : Expr(CXXThrowExprClass, Ty, VK_PRValue, OK_Ordinary), Operand(Operand) {
1220 CXXThrowExprBits.ThrowLoc = Loc;
1221 CXXThrowExprBits.IsThrownVariableInScope = IsThrownVariableInScope;
1222 setDependence(computeDependence(this));
1223 }
CXXThrowExpr(EmptyShell Empty)1224 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {}
1225
getSubExpr()1226 const Expr *getSubExpr() const { return cast_or_null<Expr>(Operand); }
getSubExpr()1227 Expr *getSubExpr() { return cast_or_null<Expr>(Operand); }
1228
getThrowLoc()1229 SourceLocation getThrowLoc() const { return CXXThrowExprBits.ThrowLoc; }
1230
1231 /// Determines whether the variable thrown by this expression (if any!)
1232 /// is within the innermost try block.
1233 ///
1234 /// This information is required to determine whether the NRVO can apply to
1235 /// this variable.
isThrownVariableInScope()1236 bool isThrownVariableInScope() const {
1237 return CXXThrowExprBits.IsThrownVariableInScope;
1238 }
1239
getBeginLoc()1240 SourceLocation getBeginLoc() const { return getThrowLoc(); }
getEndLoc()1241 SourceLocation getEndLoc() const LLVM_READONLY {
1242 if (!getSubExpr())
1243 return getThrowLoc();
1244 return getSubExpr()->getEndLoc();
1245 }
1246
classof(const Stmt * T)1247 static bool classof(const Stmt *T) {
1248 return T->getStmtClass() == CXXThrowExprClass;
1249 }
1250
1251 // Iterators
children()1252 child_range children() {
1253 return child_range(&Operand, Operand ? &Operand + 1 : &Operand);
1254 }
1255
children()1256 const_child_range children() const {
1257 return const_child_range(&Operand, Operand ? &Operand + 1 : &Operand);
1258 }
1259 };
1260
1261 /// A default argument (C++ [dcl.fct.default]).
1262 ///
1263 /// This wraps up a function call argument that was created from the
1264 /// corresponding parameter's default argument, when the call did not
1265 /// explicitly supply arguments for all of the parameters.
1266 class CXXDefaultArgExpr final
1267 : public Expr,
1268 private llvm::TrailingObjects<CXXDefaultArgExpr, Expr *> {
1269 friend class ASTStmtReader;
1270 friend class ASTReader;
1271 friend TrailingObjects;
1272
1273 /// The parameter whose default is being used.
1274 ParmVarDecl *Param;
1275
1276 /// The context where the default argument expression was used.
1277 DeclContext *UsedContext;
1278
CXXDefaultArgExpr(StmtClass SC,SourceLocation Loc,ParmVarDecl * Param,Expr * RewrittenExpr,DeclContext * UsedContext)1279 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *Param,
1280 Expr *RewrittenExpr, DeclContext *UsedContext)
1281 : Expr(SC,
1282 Param->hasUnparsedDefaultArg()
1283 ? Param->getType().getNonReferenceType()
1284 : Param->getDefaultArg()->getType(),
1285 Param->getDefaultArg()->getValueKind(),
1286 Param->getDefaultArg()->getObjectKind()),
1287 Param(Param), UsedContext(UsedContext) {
1288 CXXDefaultArgExprBits.Loc = Loc;
1289 CXXDefaultArgExprBits.HasRewrittenInit = RewrittenExpr != nullptr;
1290 if (RewrittenExpr)
1291 *getTrailingObjects<Expr *>() = RewrittenExpr;
1292 setDependence(computeDependence(this));
1293 }
1294
CXXDefaultArgExpr(EmptyShell Empty,bool HasRewrittenInit)1295 CXXDefaultArgExpr(EmptyShell Empty, bool HasRewrittenInit)
1296 : Expr(CXXDefaultArgExprClass, Empty) {
1297 CXXDefaultArgExprBits.HasRewrittenInit = HasRewrittenInit;
1298 }
1299
1300 public:
1301 static CXXDefaultArgExpr *CreateEmpty(const ASTContext &C,
1302 bool HasRewrittenInit);
1303
1304 // \p Param is the parameter whose default argument is used by this
1305 // expression.
1306 static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc,
1307 ParmVarDecl *Param, Expr *RewrittenExpr,
1308 DeclContext *UsedContext);
1309 // Retrieve the parameter that the argument was created from.
getParam()1310 const ParmVarDecl *getParam() const { return Param; }
getParam()1311 ParmVarDecl *getParam() { return Param; }
1312
hasRewrittenInit()1313 bool hasRewrittenInit() const {
1314 return CXXDefaultArgExprBits.HasRewrittenInit;
1315 }
1316
1317 // Retrieve the argument to the function call.
1318 Expr *getExpr();
getExpr()1319 const Expr *getExpr() const {
1320 return const_cast<CXXDefaultArgExpr *>(this)->getExpr();
1321 }
1322
getRewrittenExpr()1323 Expr *getRewrittenExpr() {
1324 return hasRewrittenInit() ? *getTrailingObjects<Expr *>() : nullptr;
1325 }
1326
getRewrittenExpr()1327 const Expr *getRewrittenExpr() const {
1328 return const_cast<CXXDefaultArgExpr *>(this)->getRewrittenExpr();
1329 }
1330
1331 // Retrieve the rewritten init expression (for an init expression containing
1332 // immediate calls) with the top level FullExpr and ConstantExpr stripped off.
1333 Expr *getAdjustedRewrittenExpr();
getAdjustedRewrittenExpr()1334 const Expr *getAdjustedRewrittenExpr() const {
1335 return const_cast<CXXDefaultArgExpr *>(this)->getAdjustedRewrittenExpr();
1336 }
1337
getUsedContext()1338 const DeclContext *getUsedContext() const { return UsedContext; }
getUsedContext()1339 DeclContext *getUsedContext() { return UsedContext; }
1340
1341 /// Retrieve the location where this default argument was actually used.
getUsedLocation()1342 SourceLocation getUsedLocation() const { return CXXDefaultArgExprBits.Loc; }
1343
1344 /// Default argument expressions have no representation in the
1345 /// source, so they have an empty source range.
getBeginLoc()1346 SourceLocation getBeginLoc() const { return SourceLocation(); }
getEndLoc()1347 SourceLocation getEndLoc() const { return SourceLocation(); }
1348
getExprLoc()1349 SourceLocation getExprLoc() const { return getUsedLocation(); }
1350
classof(const Stmt * T)1351 static bool classof(const Stmt *T) {
1352 return T->getStmtClass() == CXXDefaultArgExprClass;
1353 }
1354
1355 // Iterators
children()1356 child_range children() {
1357 return child_range(child_iterator(), child_iterator());
1358 }
1359
children()1360 const_child_range children() const {
1361 return const_child_range(const_child_iterator(), const_child_iterator());
1362 }
1363 };
1364
1365 /// A use of a default initializer in a constructor or in aggregate
1366 /// initialization.
1367 ///
1368 /// This wraps a use of a C++ default initializer (technically,
1369 /// a brace-or-equal-initializer for a non-static data member) when it
1370 /// is implicitly used in a mem-initializer-list in a constructor
1371 /// (C++11 [class.base.init]p8) or in aggregate initialization
1372 /// (C++1y [dcl.init.aggr]p7).
1373 class CXXDefaultInitExpr final
1374 : public Expr,
1375 private llvm::TrailingObjects<CXXDefaultInitExpr, Expr *> {
1376
1377 friend class ASTStmtReader;
1378 friend class ASTReader;
1379 friend TrailingObjects;
1380 /// The field whose default is being used.
1381 FieldDecl *Field;
1382
1383 /// The context where the default initializer expression was used.
1384 DeclContext *UsedContext;
1385
1386 CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
1387 FieldDecl *Field, QualType Ty, DeclContext *UsedContext,
1388 Expr *RewrittenInitExpr);
1389
CXXDefaultInitExpr(EmptyShell Empty,bool HasRewrittenInit)1390 CXXDefaultInitExpr(EmptyShell Empty, bool HasRewrittenInit)
1391 : Expr(CXXDefaultInitExprClass, Empty) {
1392 CXXDefaultInitExprBits.HasRewrittenInit = HasRewrittenInit;
1393 }
1394
1395 public:
1396 static CXXDefaultInitExpr *CreateEmpty(const ASTContext &C,
1397 bool HasRewrittenInit);
1398 /// \p Field is the non-static data member whose default initializer is used
1399 /// by this expression.
1400 static CXXDefaultInitExpr *Create(const ASTContext &Ctx, SourceLocation Loc,
1401 FieldDecl *Field, DeclContext *UsedContext,
1402 Expr *RewrittenInitExpr);
1403
hasRewrittenInit()1404 bool hasRewrittenInit() const {
1405 return CXXDefaultInitExprBits.HasRewrittenInit;
1406 }
1407
1408 /// Get the field whose initializer will be used.
getField()1409 FieldDecl *getField() { return Field; }
getField()1410 const FieldDecl *getField() const { return Field; }
1411
1412 /// Get the initialization expression that will be used.
1413 Expr *getExpr();
getExpr()1414 const Expr *getExpr() const {
1415 return const_cast<CXXDefaultInitExpr *>(this)->getExpr();
1416 }
1417
1418 /// Retrieve the initializing expression with evaluated immediate calls, if
1419 /// any.
getRewrittenExpr()1420 const Expr *getRewrittenExpr() const {
1421 assert(hasRewrittenInit() && "expected a rewritten init expression");
1422 return *getTrailingObjects<Expr *>();
1423 }
1424
1425 /// Retrieve the initializing expression with evaluated immediate calls, if
1426 /// any.
getRewrittenExpr()1427 Expr *getRewrittenExpr() {
1428 assert(hasRewrittenInit() && "expected a rewritten init expression");
1429 return *getTrailingObjects<Expr *>();
1430 }
1431
getUsedContext()1432 const DeclContext *getUsedContext() const { return UsedContext; }
getUsedContext()1433 DeclContext *getUsedContext() { return UsedContext; }
1434
1435 /// Retrieve the location where this default initializer expression was
1436 /// actually used.
getUsedLocation()1437 SourceLocation getUsedLocation() const { return getBeginLoc(); }
1438
getBeginLoc()1439 SourceLocation getBeginLoc() const { return CXXDefaultInitExprBits.Loc; }
getEndLoc()1440 SourceLocation getEndLoc() const { return CXXDefaultInitExprBits.Loc; }
1441
classof(const Stmt * T)1442 static bool classof(const Stmt *T) {
1443 return T->getStmtClass() == CXXDefaultInitExprClass;
1444 }
1445
1446 // Iterators
children()1447 child_range children() {
1448 return child_range(child_iterator(), child_iterator());
1449 }
1450
children()1451 const_child_range children() const {
1452 return const_child_range(const_child_iterator(), const_child_iterator());
1453 }
1454 };
1455
1456 /// Represents a C++ temporary.
1457 class CXXTemporary {
1458 /// The destructor that needs to be called.
1459 const CXXDestructorDecl *Destructor;
1460
CXXTemporary(const CXXDestructorDecl * destructor)1461 explicit CXXTemporary(const CXXDestructorDecl *destructor)
1462 : Destructor(destructor) {}
1463
1464 public:
1465 static CXXTemporary *Create(const ASTContext &C,
1466 const CXXDestructorDecl *Destructor);
1467
getDestructor()1468 const CXXDestructorDecl *getDestructor() const { return Destructor; }
1469
setDestructor(const CXXDestructorDecl * Dtor)1470 void setDestructor(const CXXDestructorDecl *Dtor) {
1471 Destructor = Dtor;
1472 }
1473 };
1474
1475 /// Represents binding an expression to a temporary.
1476 ///
1477 /// This ensures the destructor is called for the temporary. It should only be
1478 /// needed for non-POD, non-trivially destructable class types. For example:
1479 ///
1480 /// \code
1481 /// struct S {
1482 /// S() { } // User defined constructor makes S non-POD.
1483 /// ~S() { } // User defined destructor makes it non-trivial.
1484 /// };
1485 /// void test() {
1486 /// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr.
1487 /// }
1488 /// \endcode
1489 ///
1490 /// Destructor might be null if destructor declaration is not valid.
1491 class CXXBindTemporaryExpr : public Expr {
1492 CXXTemporary *Temp = nullptr;
1493 Stmt *SubExpr = nullptr;
1494
CXXBindTemporaryExpr(CXXTemporary * temp,Expr * SubExpr)1495 CXXBindTemporaryExpr(CXXTemporary *temp, Expr *SubExpr)
1496 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(), VK_PRValue,
1497 OK_Ordinary),
1498 Temp(temp), SubExpr(SubExpr) {
1499 setDependence(computeDependence(this));
1500 }
1501
1502 public:
CXXBindTemporaryExpr(EmptyShell Empty)1503 CXXBindTemporaryExpr(EmptyShell Empty)
1504 : Expr(CXXBindTemporaryExprClass, Empty) {}
1505
1506 static CXXBindTemporaryExpr *Create(const ASTContext &C, CXXTemporary *Temp,
1507 Expr* SubExpr);
1508
getTemporary()1509 CXXTemporary *getTemporary() { return Temp; }
getTemporary()1510 const CXXTemporary *getTemporary() const { return Temp; }
setTemporary(CXXTemporary * T)1511 void setTemporary(CXXTemporary *T) { Temp = T; }
1512
getSubExpr()1513 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
getSubExpr()1514 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
setSubExpr(Expr * E)1515 void setSubExpr(Expr *E) { SubExpr = E; }
1516
getBeginLoc()1517 SourceLocation getBeginLoc() const LLVM_READONLY {
1518 return SubExpr->getBeginLoc();
1519 }
1520
getEndLoc()1521 SourceLocation getEndLoc() const LLVM_READONLY {
1522 return SubExpr->getEndLoc();
1523 }
1524
1525 // Implement isa/cast/dyncast/etc.
classof(const Stmt * T)1526 static bool classof(const Stmt *T) {
1527 return T->getStmtClass() == CXXBindTemporaryExprClass;
1528 }
1529
1530 // Iterators
children()1531 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
1532
children()1533 const_child_range children() const {
1534 return const_child_range(&SubExpr, &SubExpr + 1);
1535 }
1536 };
1537
1538 enum class CXXConstructionKind {
1539 Complete,
1540 NonVirtualBase,
1541 VirtualBase,
1542 Delegating
1543 };
1544
1545 /// Represents a call to a C++ constructor.
1546 class CXXConstructExpr : public Expr {
1547 friend class ASTStmtReader;
1548
1549 /// A pointer to the constructor which will be ultimately called.
1550 CXXConstructorDecl *Constructor;
1551
1552 SourceRange ParenOrBraceRange;
1553
1554 /// The number of arguments.
1555 unsigned NumArgs;
1556
1557 // We would like to stash the arguments of the constructor call after
1558 // CXXConstructExpr. However CXXConstructExpr is used as a base class of
1559 // CXXTemporaryObjectExpr which makes the use of llvm::TrailingObjects
1560 // impossible.
1561 //
1562 // Instead we manually stash the trailing object after the full object
1563 // containing CXXConstructExpr (that is either CXXConstructExpr or
1564 // CXXTemporaryObjectExpr).
1565 //
1566 // The trailing objects are:
1567 //
1568 // * An array of getNumArgs() "Stmt *" for the arguments of the
1569 // constructor call.
1570
1571 /// Return a pointer to the start of the trailing arguments.
1572 /// Defined just after CXXTemporaryObjectExpr.
1573 inline Stmt **getTrailingArgs();
getTrailingArgs()1574 const Stmt *const *getTrailingArgs() const {
1575 return const_cast<CXXConstructExpr *>(this)->getTrailingArgs();
1576 }
1577
1578 protected:
1579 /// Build a C++ construction expression.
1580 CXXConstructExpr(StmtClass SC, QualType Ty, SourceLocation Loc,
1581 CXXConstructorDecl *Ctor, bool Elidable,
1582 ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1583 bool ListInitialization, bool StdInitListInitialization,
1584 bool ZeroInitialization, CXXConstructionKind ConstructKind,
1585 SourceRange ParenOrBraceRange);
1586
1587 /// Build an empty C++ construction expression.
1588 CXXConstructExpr(StmtClass SC, EmptyShell Empty, unsigned NumArgs);
1589
1590 /// Return the size in bytes of the trailing objects. Used by
1591 /// CXXTemporaryObjectExpr to allocate the right amount of storage.
sizeOfTrailingObjects(unsigned NumArgs)1592 static unsigned sizeOfTrailingObjects(unsigned NumArgs) {
1593 return NumArgs * sizeof(Stmt *);
1594 }
1595
1596 public:
1597 /// Create a C++ construction expression.
1598 static CXXConstructExpr *
1599 Create(const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
1600 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
1601 bool HadMultipleCandidates, bool ListInitialization,
1602 bool StdInitListInitialization, bool ZeroInitialization,
1603 CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange);
1604
1605 /// Create an empty C++ construction expression.
1606 static CXXConstructExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs);
1607
1608 /// Get the constructor that this expression will (ultimately) call.
getConstructor()1609 CXXConstructorDecl *getConstructor() const { return Constructor; }
1610
getLocation()1611 SourceLocation getLocation() const { return CXXConstructExprBits.Loc; }
setLocation(SourceLocation Loc)1612 void setLocation(SourceLocation Loc) { CXXConstructExprBits.Loc = Loc; }
1613
1614 /// Whether this construction is elidable.
isElidable()1615 bool isElidable() const { return CXXConstructExprBits.Elidable; }
setElidable(bool E)1616 void setElidable(bool E) { CXXConstructExprBits.Elidable = E; }
1617
1618 /// Whether the referred constructor was resolved from
1619 /// an overloaded set having size greater than 1.
hadMultipleCandidates()1620 bool hadMultipleCandidates() const {
1621 return CXXConstructExprBits.HadMultipleCandidates;
1622 }
setHadMultipleCandidates(bool V)1623 void setHadMultipleCandidates(bool V) {
1624 CXXConstructExprBits.HadMultipleCandidates = V;
1625 }
1626
1627 /// Whether this constructor call was written as list-initialization.
isListInitialization()1628 bool isListInitialization() const {
1629 return CXXConstructExprBits.ListInitialization;
1630 }
setListInitialization(bool V)1631 void setListInitialization(bool V) {
1632 CXXConstructExprBits.ListInitialization = V;
1633 }
1634
1635 /// Whether this constructor call was written as list-initialization,
1636 /// but was interpreted as forming a std::initializer_list<T> from the list
1637 /// and passing that as a single constructor argument.
1638 /// See C++11 [over.match.list]p1 bullet 1.
isStdInitListInitialization()1639 bool isStdInitListInitialization() const {
1640 return CXXConstructExprBits.StdInitListInitialization;
1641 }
setStdInitListInitialization(bool V)1642 void setStdInitListInitialization(bool V) {
1643 CXXConstructExprBits.StdInitListInitialization = V;
1644 }
1645
1646 /// Whether this construction first requires
1647 /// zero-initialization before the initializer is called.
requiresZeroInitialization()1648 bool requiresZeroInitialization() const {
1649 return CXXConstructExprBits.ZeroInitialization;
1650 }
setRequiresZeroInitialization(bool ZeroInit)1651 void setRequiresZeroInitialization(bool ZeroInit) {
1652 CXXConstructExprBits.ZeroInitialization = ZeroInit;
1653 }
1654
1655 /// Determine whether this constructor is actually constructing
1656 /// a base class (rather than a complete object).
getConstructionKind()1657 CXXConstructionKind getConstructionKind() const {
1658 return static_cast<CXXConstructionKind>(
1659 CXXConstructExprBits.ConstructionKind);
1660 }
setConstructionKind(CXXConstructionKind CK)1661 void setConstructionKind(CXXConstructionKind CK) {
1662 CXXConstructExprBits.ConstructionKind = llvm::to_underlying(CK);
1663 }
1664
1665 using arg_iterator = ExprIterator;
1666 using const_arg_iterator = ConstExprIterator;
1667 using arg_range = llvm::iterator_range<arg_iterator>;
1668 using const_arg_range = llvm::iterator_range<const_arg_iterator>;
1669
arguments()1670 arg_range arguments() { return arg_range(arg_begin(), arg_end()); }
arguments()1671 const_arg_range arguments() const {
1672 return const_arg_range(arg_begin(), arg_end());
1673 }
1674
arg_begin()1675 arg_iterator arg_begin() { return getTrailingArgs(); }
arg_end()1676 arg_iterator arg_end() { return arg_begin() + getNumArgs(); }
arg_begin()1677 const_arg_iterator arg_begin() const { return getTrailingArgs(); }
arg_end()1678 const_arg_iterator arg_end() const { return arg_begin() + getNumArgs(); }
1679
getArgs()1680 Expr **getArgs() { return reinterpret_cast<Expr **>(getTrailingArgs()); }
getArgs()1681 const Expr *const *getArgs() const {
1682 return reinterpret_cast<const Expr *const *>(getTrailingArgs());
1683 }
1684
1685 /// Return the number of arguments to the constructor call.
getNumArgs()1686 unsigned getNumArgs() const { return NumArgs; }
1687
1688 /// Return the specified argument.
getArg(unsigned Arg)1689 Expr *getArg(unsigned Arg) {
1690 assert(Arg < getNumArgs() && "Arg access out of range!");
1691 return getArgs()[Arg];
1692 }
getArg(unsigned Arg)1693 const Expr *getArg(unsigned Arg) const {
1694 assert(Arg < getNumArgs() && "Arg access out of range!");
1695 return getArgs()[Arg];
1696 }
1697
1698 /// Set the specified argument.
setArg(unsigned Arg,Expr * ArgExpr)1699 void setArg(unsigned Arg, Expr *ArgExpr) {
1700 assert(Arg < getNumArgs() && "Arg access out of range!");
1701 getArgs()[Arg] = ArgExpr;
1702 }
1703
isImmediateEscalating()1704 bool isImmediateEscalating() const {
1705 return CXXConstructExprBits.IsImmediateEscalating;
1706 }
1707
setIsImmediateEscalating(bool Set)1708 void setIsImmediateEscalating(bool Set) {
1709 CXXConstructExprBits.IsImmediateEscalating = Set;
1710 }
1711
1712 SourceLocation getBeginLoc() const LLVM_READONLY;
1713 SourceLocation getEndLoc() const LLVM_READONLY;
getParenOrBraceRange()1714 SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
setParenOrBraceRange(SourceRange Range)1715 void setParenOrBraceRange(SourceRange Range) { ParenOrBraceRange = Range; }
1716
classof(const Stmt * T)1717 static bool classof(const Stmt *T) {
1718 return T->getStmtClass() == CXXConstructExprClass ||
1719 T->getStmtClass() == CXXTemporaryObjectExprClass;
1720 }
1721
1722 // Iterators
children()1723 child_range children() {
1724 return child_range(getTrailingArgs(), getTrailingArgs() + getNumArgs());
1725 }
1726
children()1727 const_child_range children() const {
1728 auto Children = const_cast<CXXConstructExpr *>(this)->children();
1729 return const_child_range(Children.begin(), Children.end());
1730 }
1731 };
1732
1733 /// Represents a call to an inherited base class constructor from an
1734 /// inheriting constructor. This call implicitly forwards the arguments from
1735 /// the enclosing context (an inheriting constructor) to the specified inherited
1736 /// base class constructor.
1737 class CXXInheritedCtorInitExpr : public Expr {
1738 private:
1739 CXXConstructorDecl *Constructor = nullptr;
1740
1741 /// The location of the using declaration.
1742 SourceLocation Loc;
1743
1744 /// Whether this is the construction of a virtual base.
1745 LLVM_PREFERRED_TYPE(bool)
1746 unsigned ConstructsVirtualBase : 1;
1747
1748 /// Whether the constructor is inherited from a virtual base class of the
1749 /// class that we construct.
1750 LLVM_PREFERRED_TYPE(bool)
1751 unsigned InheritedFromVirtualBase : 1;
1752
1753 public:
1754 friend class ASTStmtReader;
1755
1756 /// Construct a C++ inheriting construction expression.
CXXInheritedCtorInitExpr(SourceLocation Loc,QualType T,CXXConstructorDecl * Ctor,bool ConstructsVirtualBase,bool InheritedFromVirtualBase)1757 CXXInheritedCtorInitExpr(SourceLocation Loc, QualType T,
1758 CXXConstructorDecl *Ctor, bool ConstructsVirtualBase,
1759 bool InheritedFromVirtualBase)
1760 : Expr(CXXInheritedCtorInitExprClass, T, VK_PRValue, OK_Ordinary),
1761 Constructor(Ctor), Loc(Loc),
1762 ConstructsVirtualBase(ConstructsVirtualBase),
1763 InheritedFromVirtualBase(InheritedFromVirtualBase) {
1764 assert(!T->isDependentType());
1765 setDependence(ExprDependence::None);
1766 }
1767
1768 /// Construct an empty C++ inheriting construction expression.
CXXInheritedCtorInitExpr(EmptyShell Empty)1769 explicit CXXInheritedCtorInitExpr(EmptyShell Empty)
1770 : Expr(CXXInheritedCtorInitExprClass, Empty),
1771 ConstructsVirtualBase(false), InheritedFromVirtualBase(false) {}
1772
1773 /// Get the constructor that this expression will call.
getConstructor()1774 CXXConstructorDecl *getConstructor() const { return Constructor; }
1775
1776 /// Determine whether this constructor is actually constructing
1777 /// a base class (rather than a complete object).
constructsVBase()1778 bool constructsVBase() const { return ConstructsVirtualBase; }
getConstructionKind()1779 CXXConstructionKind getConstructionKind() const {
1780 return ConstructsVirtualBase ? CXXConstructionKind::VirtualBase
1781 : CXXConstructionKind::NonVirtualBase;
1782 }
1783
1784 /// Determine whether the inherited constructor is inherited from a
1785 /// virtual base of the object we construct. If so, we are not responsible
1786 /// for calling the inherited constructor (the complete object constructor
1787 /// does that), and so we don't need to pass any arguments.
inheritedFromVBase()1788 bool inheritedFromVBase() const { return InheritedFromVirtualBase; }
1789
getLocation()1790 SourceLocation getLocation() const LLVM_READONLY { return Loc; }
getBeginLoc()1791 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
getEndLoc()1792 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
1793
classof(const Stmt * T)1794 static bool classof(const Stmt *T) {
1795 return T->getStmtClass() == CXXInheritedCtorInitExprClass;
1796 }
1797
children()1798 child_range children() {
1799 return child_range(child_iterator(), child_iterator());
1800 }
1801
children()1802 const_child_range children() const {
1803 return const_child_range(const_child_iterator(), const_child_iterator());
1804 }
1805 };
1806
1807 /// Represents an explicit C++ type conversion that uses "functional"
1808 /// notation (C++ [expr.type.conv]).
1809 ///
1810 /// Example:
1811 /// \code
1812 /// x = int(0.5);
1813 /// \endcode
1814 class CXXFunctionalCastExpr final
1815 : public ExplicitCastExpr,
1816 private llvm::TrailingObjects<CXXFunctionalCastExpr, CXXBaseSpecifier *,
1817 FPOptionsOverride> {
1818 SourceLocation LParenLoc;
1819 SourceLocation RParenLoc;
1820
CXXFunctionalCastExpr(QualType ty,ExprValueKind VK,TypeSourceInfo * writtenTy,CastKind kind,Expr * castExpr,unsigned pathSize,FPOptionsOverride FPO,SourceLocation lParenLoc,SourceLocation rParenLoc)1821 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK,
1822 TypeSourceInfo *writtenTy, CastKind kind,
1823 Expr *castExpr, unsigned pathSize,
1824 FPOptionsOverride FPO, SourceLocation lParenLoc,
1825 SourceLocation rParenLoc)
1826 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, castExpr,
1827 pathSize, FPO.requiresTrailingStorage(), writtenTy),
1828 LParenLoc(lParenLoc), RParenLoc(rParenLoc) {
1829 if (hasStoredFPFeatures())
1830 *getTrailingFPFeatures() = FPO;
1831 }
1832
CXXFunctionalCastExpr(EmptyShell Shell,unsigned PathSize,bool HasFPFeatures)1833 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize,
1834 bool HasFPFeatures)
1835 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize,
1836 HasFPFeatures) {}
1837
numTrailingObjects(OverloadToken<CXXBaseSpecifier * >)1838 unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const {
1839 return path_size();
1840 }
1841
1842 public:
1843 friend class CastExpr;
1844 friend TrailingObjects;
1845
1846 static CXXFunctionalCastExpr *
1847 Create(const ASTContext &Context, QualType T, ExprValueKind VK,
1848 TypeSourceInfo *Written, CastKind Kind, Expr *Op,
1849 const CXXCastPath *Path, FPOptionsOverride FPO, SourceLocation LPLoc,
1850 SourceLocation RPLoc);
1851 static CXXFunctionalCastExpr *
1852 CreateEmpty(const ASTContext &Context, unsigned PathSize, bool HasFPFeatures);
1853
getLParenLoc()1854 SourceLocation getLParenLoc() const { return LParenLoc; }
setLParenLoc(SourceLocation L)1855 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
getRParenLoc()1856 SourceLocation getRParenLoc() const { return RParenLoc; }
setRParenLoc(SourceLocation L)1857 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1858
1859 /// Determine whether this expression models list-initialization.
isListInitialization()1860 bool isListInitialization() const { return LParenLoc.isInvalid(); }
1861
1862 SourceLocation getBeginLoc() const LLVM_READONLY;
1863 SourceLocation getEndLoc() const LLVM_READONLY;
1864
classof(const Stmt * T)1865 static bool classof(const Stmt *T) {
1866 return T->getStmtClass() == CXXFunctionalCastExprClass;
1867 }
1868 };
1869
1870 /// Represents a C++ functional cast expression that builds a
1871 /// temporary object.
1872 ///
1873 /// This expression type represents a C++ "functional" cast
1874 /// (C++[expr.type.conv]) with N != 1 arguments that invokes a
1875 /// constructor to build a temporary object. With N == 1 arguments the
1876 /// functional cast expression will be represented by CXXFunctionalCastExpr.
1877 /// Example:
1878 /// \code
1879 /// struct X { X(int, float); }
1880 ///
1881 /// X create_X() {
1882 /// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
1883 /// };
1884 /// \endcode
1885 class CXXTemporaryObjectExpr final : public CXXConstructExpr {
1886 friend class ASTStmtReader;
1887
1888 // CXXTemporaryObjectExpr has some trailing objects belonging
1889 // to CXXConstructExpr. See the comment inside CXXConstructExpr
1890 // for more details.
1891
1892 TypeSourceInfo *TSI;
1893
1894 CXXTemporaryObjectExpr(CXXConstructorDecl *Cons, QualType Ty,
1895 TypeSourceInfo *TSI, ArrayRef<Expr *> Args,
1896 SourceRange ParenOrBraceRange,
1897 bool HadMultipleCandidates, bool ListInitialization,
1898 bool StdInitListInitialization,
1899 bool ZeroInitialization);
1900
1901 CXXTemporaryObjectExpr(EmptyShell Empty, unsigned NumArgs);
1902
1903 public:
1904 static CXXTemporaryObjectExpr *
1905 Create(const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
1906 TypeSourceInfo *TSI, ArrayRef<Expr *> Args,
1907 SourceRange ParenOrBraceRange, bool HadMultipleCandidates,
1908 bool ListInitialization, bool StdInitListInitialization,
1909 bool ZeroInitialization);
1910
1911 static CXXTemporaryObjectExpr *CreateEmpty(const ASTContext &Ctx,
1912 unsigned NumArgs);
1913
getTypeSourceInfo()1914 TypeSourceInfo *getTypeSourceInfo() const { return TSI; }
1915
1916 SourceLocation getBeginLoc() const LLVM_READONLY;
1917 SourceLocation getEndLoc() const LLVM_READONLY;
1918
classof(const Stmt * T)1919 static bool classof(const Stmt *T) {
1920 return T->getStmtClass() == CXXTemporaryObjectExprClass;
1921 }
1922 };
1923
getTrailingArgs()1924 Stmt **CXXConstructExpr::getTrailingArgs() {
1925 if (auto *E = dyn_cast<CXXTemporaryObjectExpr>(this))
1926 return reinterpret_cast<Stmt **>(E + 1);
1927 assert((getStmtClass() == CXXConstructExprClass) &&
1928 "Unexpected class deriving from CXXConstructExpr!");
1929 return reinterpret_cast<Stmt **>(this + 1);
1930 }
1931
1932 /// A C++ lambda expression, which produces a function object
1933 /// (of unspecified type) that can be invoked later.
1934 ///
1935 /// Example:
1936 /// \code
1937 /// void low_pass_filter(std::vector<double> &values, double cutoff) {
1938 /// values.erase(std::remove_if(values.begin(), values.end(),
1939 /// [=](double value) { return value > cutoff; });
1940 /// }
1941 /// \endcode
1942 ///
1943 /// C++11 lambda expressions can capture local variables, either by copying
1944 /// the values of those local variables at the time the function
1945 /// object is constructed (not when it is called!) or by holding a
1946 /// reference to the local variable. These captures can occur either
1947 /// implicitly or can be written explicitly between the square
1948 /// brackets ([...]) that start the lambda expression.
1949 ///
1950 /// C++1y introduces a new form of "capture" called an init-capture that
1951 /// includes an initializing expression (rather than capturing a variable),
1952 /// and which can never occur implicitly.
1953 class LambdaExpr final : public Expr,
1954 private llvm::TrailingObjects<LambdaExpr, Stmt *> {
1955 // LambdaExpr has some data stored in LambdaExprBits.
1956
1957 /// The source range that covers the lambda introducer ([...]).
1958 SourceRange IntroducerRange;
1959
1960 /// The source location of this lambda's capture-default ('=' or '&').
1961 SourceLocation CaptureDefaultLoc;
1962
1963 /// The location of the closing brace ('}') that completes
1964 /// the lambda.
1965 ///
1966 /// The location of the brace is also available by looking up the
1967 /// function call operator in the lambda class. However, it is
1968 /// stored here to improve the performance of getSourceRange(), and
1969 /// to avoid having to deserialize the function call operator from a
1970 /// module file just to determine the source range.
1971 SourceLocation ClosingBrace;
1972
1973 /// Construct a lambda expression.
1974 LambdaExpr(QualType T, SourceRange IntroducerRange,
1975 LambdaCaptureDefault CaptureDefault,
1976 SourceLocation CaptureDefaultLoc, bool ExplicitParams,
1977 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1978 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack);
1979
1980 /// Construct an empty lambda expression.
1981 LambdaExpr(EmptyShell Empty, unsigned NumCaptures);
1982
getStoredStmts()1983 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
getStoredStmts()1984 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
1985
1986 void initBodyIfNeeded() const;
1987
1988 public:
1989 friend class ASTStmtReader;
1990 friend class ASTStmtWriter;
1991 friend TrailingObjects;
1992
1993 /// Construct a new lambda expression.
1994 static LambdaExpr *
1995 Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange,
1996 LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc,
1997 bool ExplicitParams, bool ExplicitResultType,
1998 ArrayRef<Expr *> CaptureInits, SourceLocation ClosingBrace,
1999 bool ContainsUnexpandedParameterPack);
2000
2001 /// Construct a new lambda expression that will be deserialized from
2002 /// an external source.
2003 static LambdaExpr *CreateDeserialized(const ASTContext &C,
2004 unsigned NumCaptures);
2005
2006 /// Determine the default capture kind for this lambda.
getCaptureDefault()2007 LambdaCaptureDefault getCaptureDefault() const {
2008 return static_cast<LambdaCaptureDefault>(LambdaExprBits.CaptureDefault);
2009 }
2010
2011 /// Retrieve the location of this lambda's capture-default, if any.
getCaptureDefaultLoc()2012 SourceLocation getCaptureDefaultLoc() const { return CaptureDefaultLoc; }
2013
2014 /// Determine whether one of this lambda's captures is an init-capture.
2015 bool isInitCapture(const LambdaCapture *Capture) const;
2016
2017 /// An iterator that walks over the captures of the lambda,
2018 /// both implicit and explicit.
2019 using capture_iterator = const LambdaCapture *;
2020
2021 /// An iterator over a range of lambda captures.
2022 using capture_range = llvm::iterator_range<capture_iterator>;
2023
2024 /// Retrieve this lambda's captures.
2025 capture_range captures() const;
2026
2027 /// Retrieve an iterator pointing to the first lambda capture.
2028 capture_iterator capture_begin() const;
2029
2030 /// Retrieve an iterator pointing past the end of the
2031 /// sequence of lambda captures.
2032 capture_iterator capture_end() const;
2033
2034 /// Determine the number of captures in this lambda.
capture_size()2035 unsigned capture_size() const { return LambdaExprBits.NumCaptures; }
2036
2037 /// Retrieve this lambda's explicit captures.
2038 capture_range explicit_captures() const;
2039
2040 /// Retrieve an iterator pointing to the first explicit
2041 /// lambda capture.
2042 capture_iterator explicit_capture_begin() const;
2043
2044 /// Retrieve an iterator pointing past the end of the sequence of
2045 /// explicit lambda captures.
2046 capture_iterator explicit_capture_end() const;
2047
2048 /// Retrieve this lambda's implicit captures.
2049 capture_range implicit_captures() const;
2050
2051 /// Retrieve an iterator pointing to the first implicit
2052 /// lambda capture.
2053 capture_iterator implicit_capture_begin() const;
2054
2055 /// Retrieve an iterator pointing past the end of the sequence of
2056 /// implicit lambda captures.
2057 capture_iterator implicit_capture_end() const;
2058
2059 /// Iterator that walks over the capture initialization
2060 /// arguments.
2061 using capture_init_iterator = Expr **;
2062
2063 /// Const iterator that walks over the capture initialization
2064 /// arguments.
2065 /// FIXME: This interface is prone to being used incorrectly.
2066 using const_capture_init_iterator = Expr *const *;
2067
2068 /// Retrieve the initialization expressions for this lambda's captures.
capture_inits()2069 llvm::iterator_range<capture_init_iterator> capture_inits() {
2070 return llvm::make_range(capture_init_begin(), capture_init_end());
2071 }
2072
2073 /// Retrieve the initialization expressions for this lambda's captures.
capture_inits()2074 llvm::iterator_range<const_capture_init_iterator> capture_inits() const {
2075 return llvm::make_range(capture_init_begin(), capture_init_end());
2076 }
2077
2078 /// Retrieve the first initialization argument for this
2079 /// lambda expression (which initializes the first capture field).
capture_init_begin()2080 capture_init_iterator capture_init_begin() {
2081 return reinterpret_cast<Expr **>(getStoredStmts());
2082 }
2083
2084 /// Retrieve the first initialization argument for this
2085 /// lambda expression (which initializes the first capture field).
capture_init_begin()2086 const_capture_init_iterator capture_init_begin() const {
2087 return reinterpret_cast<Expr *const *>(getStoredStmts());
2088 }
2089
2090 /// Retrieve the iterator pointing one past the last
2091 /// initialization argument for this lambda expression.
capture_init_end()2092 capture_init_iterator capture_init_end() {
2093 return capture_init_begin() + capture_size();
2094 }
2095
2096 /// Retrieve the iterator pointing one past the last
2097 /// initialization argument for this lambda expression.
capture_init_end()2098 const_capture_init_iterator capture_init_end() const {
2099 return capture_init_begin() + capture_size();
2100 }
2101
2102 /// Retrieve the source range covering the lambda introducer,
2103 /// which contains the explicit capture list surrounded by square
2104 /// brackets ([...]).
getIntroducerRange()2105 SourceRange getIntroducerRange() const { return IntroducerRange; }
2106
2107 /// Retrieve the class that corresponds to the lambda.
2108 ///
2109 /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the
2110 /// captures in its fields and provides the various operations permitted
2111 /// on a lambda (copying, calling).
2112 CXXRecordDecl *getLambdaClass() const;
2113
2114 /// Retrieve the function call operator associated with this
2115 /// lambda expression.
2116 CXXMethodDecl *getCallOperator() const;
2117
2118 /// Retrieve the function template call operator associated with this
2119 /// lambda expression.
2120 FunctionTemplateDecl *getDependentCallOperator() const;
2121
2122 /// If this is a generic lambda expression, retrieve the template
2123 /// parameter list associated with it, or else return null.
2124 TemplateParameterList *getTemplateParameterList() const;
2125
2126 /// Get the template parameters were explicitly specified (as opposed to being
2127 /// invented by use of an auto parameter).
2128 ArrayRef<NamedDecl *> getExplicitTemplateParameters() const;
2129
2130 /// Get the trailing requires clause, if any.
2131 Expr *getTrailingRequiresClause() const;
2132
2133 /// Whether this is a generic lambda.
isGenericLambda()2134 bool isGenericLambda() const { return getTemplateParameterList(); }
2135
2136 /// Retrieve the body of the lambda. This will be most of the time
2137 /// a \p CompoundStmt, but can also be \p CoroutineBodyStmt wrapping
2138 /// a \p CompoundStmt. Note that unlike functions, lambda-expressions
2139 /// cannot have a function-try-block.
2140 Stmt *getBody() const;
2141
2142 /// Retrieve the \p CompoundStmt representing the body of the lambda.
2143 /// This is a convenience function for callers who do not need
2144 /// to handle node(s) which may wrap a \p CompoundStmt.
2145 const CompoundStmt *getCompoundStmtBody() const;
getCompoundStmtBody()2146 CompoundStmt *getCompoundStmtBody() {
2147 const auto *ConstThis = this;
2148 return const_cast<CompoundStmt *>(ConstThis->getCompoundStmtBody());
2149 }
2150
2151 /// Determine whether the lambda is mutable, meaning that any
2152 /// captures values can be modified.
2153 bool isMutable() const;
2154
2155 /// Determine whether this lambda has an explicit parameter
2156 /// list vs. an implicit (empty) parameter list.
hasExplicitParameters()2157 bool hasExplicitParameters() const { return LambdaExprBits.ExplicitParams; }
2158
2159 /// Whether this lambda had its result type explicitly specified.
hasExplicitResultType()2160 bool hasExplicitResultType() const {
2161 return LambdaExprBits.ExplicitResultType;
2162 }
2163
classof(const Stmt * T)2164 static bool classof(const Stmt *T) {
2165 return T->getStmtClass() == LambdaExprClass;
2166 }
2167
getBeginLoc()2168 SourceLocation getBeginLoc() const LLVM_READONLY {
2169 return IntroducerRange.getBegin();
2170 }
2171
getEndLoc()2172 SourceLocation getEndLoc() const LLVM_READONLY { return ClosingBrace; }
2173
2174 /// Includes the captures and the body of the lambda.
2175 child_range children();
2176 const_child_range children() const;
2177 };
2178
2179 /// An expression "T()" which creates a value-initialized rvalue of type
2180 /// T, which is a non-class type. See (C++98 [5.2.3p2]).
2181 class CXXScalarValueInitExpr : public Expr {
2182 friend class ASTStmtReader;
2183
2184 TypeSourceInfo *TypeInfo;
2185
2186 public:
2187 /// Create an explicitly-written scalar-value initialization
2188 /// expression.
CXXScalarValueInitExpr(QualType Type,TypeSourceInfo * TypeInfo,SourceLocation RParenLoc)2189 CXXScalarValueInitExpr(QualType Type, TypeSourceInfo *TypeInfo,
2190 SourceLocation RParenLoc)
2191 : Expr(CXXScalarValueInitExprClass, Type, VK_PRValue, OK_Ordinary),
2192 TypeInfo(TypeInfo) {
2193 CXXScalarValueInitExprBits.RParenLoc = RParenLoc;
2194 setDependence(computeDependence(this));
2195 }
2196
CXXScalarValueInitExpr(EmptyShell Shell)2197 explicit CXXScalarValueInitExpr(EmptyShell Shell)
2198 : Expr(CXXScalarValueInitExprClass, Shell) {}
2199
getTypeSourceInfo()2200 TypeSourceInfo *getTypeSourceInfo() const {
2201 return TypeInfo;
2202 }
2203
getRParenLoc()2204 SourceLocation getRParenLoc() const {
2205 return CXXScalarValueInitExprBits.RParenLoc;
2206 }
2207
2208 SourceLocation getBeginLoc() const LLVM_READONLY;
getEndLoc()2209 SourceLocation getEndLoc() const { return getRParenLoc(); }
2210
classof(const Stmt * T)2211 static bool classof(const Stmt *T) {
2212 return T->getStmtClass() == CXXScalarValueInitExprClass;
2213 }
2214
2215 // Iterators
children()2216 child_range children() {
2217 return child_range(child_iterator(), child_iterator());
2218 }
2219
children()2220 const_child_range children() const {
2221 return const_child_range(const_child_iterator(), const_child_iterator());
2222 }
2223 };
2224
2225 enum class CXXNewInitializationStyle {
2226 /// New-expression has no initializer as written.
2227 None,
2228
2229 /// New-expression has a C++98 paren-delimited initializer.
2230 Parens,
2231
2232 /// New-expression has a C++11 list-initializer.
2233 Braces
2234 };
2235
2236 /// Represents a new-expression for memory allocation and constructor
2237 /// calls, e.g: "new CXXNewExpr(foo)".
2238 class CXXNewExpr final
2239 : public Expr,
2240 private llvm::TrailingObjects<CXXNewExpr, Stmt *, SourceRange> {
2241 friend class ASTStmtReader;
2242 friend class ASTStmtWriter;
2243 friend TrailingObjects;
2244
2245 /// Points to the allocation function used.
2246 FunctionDecl *OperatorNew;
2247
2248 /// Points to the deallocation function used in case of error. May be null.
2249 FunctionDecl *OperatorDelete;
2250
2251 /// The allocated type-source information, as written in the source.
2252 TypeSourceInfo *AllocatedTypeInfo;
2253
2254 /// Range of the entire new expression.
2255 SourceRange Range;
2256
2257 /// Source-range of a paren-delimited initializer.
2258 SourceRange DirectInitRange;
2259
2260 // CXXNewExpr is followed by several optional trailing objects.
2261 // They are in order:
2262 //
2263 // * An optional "Stmt *" for the array size expression.
2264 // Present if and ony if isArray().
2265 //
2266 // * An optional "Stmt *" for the init expression.
2267 // Present if and only if hasInitializer().
2268 //
2269 // * An array of getNumPlacementArgs() "Stmt *" for the placement new
2270 // arguments, if any.
2271 //
2272 // * An optional SourceRange for the range covering the parenthesized type-id
2273 // if the allocated type was expressed as a parenthesized type-id.
2274 // Present if and only if isParenTypeId().
arraySizeOffset()2275 unsigned arraySizeOffset() const { return 0; }
initExprOffset()2276 unsigned initExprOffset() const { return arraySizeOffset() + isArray(); }
placementNewArgsOffset()2277 unsigned placementNewArgsOffset() const {
2278 return initExprOffset() + hasInitializer();
2279 }
2280
numTrailingObjects(OverloadToken<Stmt * >)2281 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2282 return isArray() + hasInitializer() + getNumPlacementArgs();
2283 }
2284
numTrailingObjects(OverloadToken<SourceRange>)2285 unsigned numTrailingObjects(OverloadToken<SourceRange>) const {
2286 return isParenTypeId();
2287 }
2288
2289 /// Build a c++ new expression.
2290 CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
2291 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
2292 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
2293 SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
2294 CXXNewInitializationStyle InitializationStyle, Expr *Initializer,
2295 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
2296 SourceRange DirectInitRange);
2297
2298 /// Build an empty c++ new expression.
2299 CXXNewExpr(EmptyShell Empty, bool IsArray, unsigned NumPlacementArgs,
2300 bool IsParenTypeId);
2301
2302 public:
2303 /// Create a c++ new expression.
2304 static CXXNewExpr *
2305 Create(const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew,
2306 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
2307 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
2308 SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
2309 CXXNewInitializationStyle InitializationStyle, Expr *Initializer,
2310 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
2311 SourceRange DirectInitRange);
2312
2313 /// Create an empty c++ new expression.
2314 static CXXNewExpr *CreateEmpty(const ASTContext &Ctx, bool IsArray,
2315 bool HasInit, unsigned NumPlacementArgs,
2316 bool IsParenTypeId);
2317
getAllocatedType()2318 QualType getAllocatedType() const {
2319 return getType()->castAs<PointerType>()->getPointeeType();
2320 }
2321
getAllocatedTypeSourceInfo()2322 TypeSourceInfo *getAllocatedTypeSourceInfo() const {
2323 return AllocatedTypeInfo;
2324 }
2325
2326 /// True if the allocation result needs to be null-checked.
2327 ///
2328 /// C++11 [expr.new]p13:
2329 /// If the allocation function returns null, initialization shall
2330 /// not be done, the deallocation function shall not be called,
2331 /// and the value of the new-expression shall be null.
2332 ///
2333 /// C++ DR1748:
2334 /// If the allocation function is a reserved placement allocation
2335 /// function that returns null, the behavior is undefined.
2336 ///
2337 /// An allocation function is not allowed to return null unless it
2338 /// has a non-throwing exception-specification. The '03 rule is
2339 /// identical except that the definition of a non-throwing
2340 /// exception specification is just "is it throw()?".
2341 bool shouldNullCheckAllocation() const;
2342
getOperatorNew()2343 FunctionDecl *getOperatorNew() const { return OperatorNew; }
setOperatorNew(FunctionDecl * D)2344 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; }
getOperatorDelete()2345 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
setOperatorDelete(FunctionDecl * D)2346 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; }
2347
isArray()2348 bool isArray() const { return CXXNewExprBits.IsArray; }
2349
2350 /// This might return std::nullopt even if isArray() returns true,
2351 /// since there might not be an array size expression.
2352 /// If the result is not std::nullopt, it will never wrap a nullptr.
getArraySize()2353 std::optional<Expr *> getArraySize() {
2354 if (!isArray())
2355 return std::nullopt;
2356
2357 if (auto *Result =
2358 cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
2359 return Result;
2360
2361 return std::nullopt;
2362 }
2363
2364 /// This might return std::nullopt even if isArray() returns true,
2365 /// since there might not be an array size expression.
2366 /// If the result is not std::nullopt, it will never wrap a nullptr.
getArraySize()2367 std::optional<const Expr *> getArraySize() const {
2368 if (!isArray())
2369 return std::nullopt;
2370
2371 if (auto *Result =
2372 cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
2373 return Result;
2374
2375 return std::nullopt;
2376 }
2377
getNumPlacementArgs()2378 unsigned getNumPlacementArgs() const {
2379 return CXXNewExprBits.NumPlacementArgs;
2380 }
2381
getPlacementArgs()2382 Expr **getPlacementArgs() {
2383 return reinterpret_cast<Expr **>(getTrailingObjects<Stmt *>() +
2384 placementNewArgsOffset());
2385 }
2386
getPlacementArg(unsigned I)2387 Expr *getPlacementArg(unsigned I) {
2388 assert((I < getNumPlacementArgs()) && "Index out of range!");
2389 return getPlacementArgs()[I];
2390 }
getPlacementArg(unsigned I)2391 const Expr *getPlacementArg(unsigned I) const {
2392 return const_cast<CXXNewExpr *>(this)->getPlacementArg(I);
2393 }
2394
isParenTypeId()2395 bool isParenTypeId() const { return CXXNewExprBits.IsParenTypeId; }
getTypeIdParens()2396 SourceRange getTypeIdParens() const {
2397 return isParenTypeId() ? getTrailingObjects<SourceRange>()[0]
2398 : SourceRange();
2399 }
2400
isGlobalNew()2401 bool isGlobalNew() const { return CXXNewExprBits.IsGlobalNew; }
2402
2403 /// Whether this new-expression has any initializer at all.
hasInitializer()2404 bool hasInitializer() const { return CXXNewExprBits.HasInitializer; }
2405
2406 /// The kind of initializer this new-expression has.
getInitializationStyle()2407 CXXNewInitializationStyle getInitializationStyle() const {
2408 return static_cast<CXXNewInitializationStyle>(
2409 CXXNewExprBits.StoredInitializationStyle);
2410 }
2411
2412 /// The initializer of this new-expression.
getInitializer()2413 Expr *getInitializer() {
2414 return hasInitializer()
2415 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
2416 : nullptr;
2417 }
getInitializer()2418 const Expr *getInitializer() const {
2419 return hasInitializer()
2420 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
2421 : nullptr;
2422 }
2423
2424 /// Returns the CXXConstructExpr from this new-expression, or null.
getConstructExpr()2425 const CXXConstructExpr *getConstructExpr() const {
2426 return dyn_cast_or_null<CXXConstructExpr>(getInitializer());
2427 }
2428
2429 /// Indicates whether the required alignment should be implicitly passed to
2430 /// the allocation function.
passAlignment()2431 bool passAlignment() const { return CXXNewExprBits.ShouldPassAlignment; }
2432
2433 /// Answers whether the usual array deallocation function for the
2434 /// allocated type expects the size of the allocation as a
2435 /// parameter.
doesUsualArrayDeleteWantSize()2436 bool doesUsualArrayDeleteWantSize() const {
2437 return CXXNewExprBits.UsualArrayDeleteWantsSize;
2438 }
2439
2440 using arg_iterator = ExprIterator;
2441 using const_arg_iterator = ConstExprIterator;
2442
placement_arguments()2443 llvm::iterator_range<arg_iterator> placement_arguments() {
2444 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2445 }
2446
placement_arguments()2447 llvm::iterator_range<const_arg_iterator> placement_arguments() const {
2448 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2449 }
2450
placement_arg_begin()2451 arg_iterator placement_arg_begin() {
2452 return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
2453 }
placement_arg_end()2454 arg_iterator placement_arg_end() {
2455 return placement_arg_begin() + getNumPlacementArgs();
2456 }
placement_arg_begin()2457 const_arg_iterator placement_arg_begin() const {
2458 return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
2459 }
placement_arg_end()2460 const_arg_iterator placement_arg_end() const {
2461 return placement_arg_begin() + getNumPlacementArgs();
2462 }
2463
2464 using raw_arg_iterator = Stmt **;
2465
raw_arg_begin()2466 raw_arg_iterator raw_arg_begin() { return getTrailingObjects<Stmt *>(); }
raw_arg_end()2467 raw_arg_iterator raw_arg_end() {
2468 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
2469 }
raw_arg_begin()2470 const_arg_iterator raw_arg_begin() const {
2471 return getTrailingObjects<Stmt *>();
2472 }
raw_arg_end()2473 const_arg_iterator raw_arg_end() const {
2474 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
2475 }
2476
getBeginLoc()2477 SourceLocation getBeginLoc() const { return Range.getBegin(); }
getEndLoc()2478 SourceLocation getEndLoc() const { return Range.getEnd(); }
2479
getDirectInitRange()2480 SourceRange getDirectInitRange() const { return DirectInitRange; }
getSourceRange()2481 SourceRange getSourceRange() const { return Range; }
2482
classof(const Stmt * T)2483 static bool classof(const Stmt *T) {
2484 return T->getStmtClass() == CXXNewExprClass;
2485 }
2486
2487 // Iterators
children()2488 child_range children() { return child_range(raw_arg_begin(), raw_arg_end()); }
2489
children()2490 const_child_range children() const {
2491 return const_child_range(const_cast<CXXNewExpr *>(this)->children());
2492 }
2493 };
2494
2495 /// Represents a \c delete expression for memory deallocation and
2496 /// destructor calls, e.g. "delete[] pArray".
2497 class CXXDeleteExpr : public Expr {
2498 friend class ASTStmtReader;
2499
2500 /// Points to the operator delete overload that is used. Could be a member.
2501 FunctionDecl *OperatorDelete = nullptr;
2502
2503 /// The pointer expression to be deleted.
2504 Stmt *Argument = nullptr;
2505
2506 public:
CXXDeleteExpr(QualType Ty,bool GlobalDelete,bool ArrayForm,bool ArrayFormAsWritten,bool UsualArrayDeleteWantsSize,FunctionDecl * OperatorDelete,Expr * Arg,SourceLocation Loc)2507 CXXDeleteExpr(QualType Ty, bool GlobalDelete, bool ArrayForm,
2508 bool ArrayFormAsWritten, bool UsualArrayDeleteWantsSize,
2509 FunctionDecl *OperatorDelete, Expr *Arg, SourceLocation Loc)
2510 : Expr(CXXDeleteExprClass, Ty, VK_PRValue, OK_Ordinary),
2511 OperatorDelete(OperatorDelete), Argument(Arg) {
2512 CXXDeleteExprBits.GlobalDelete = GlobalDelete;
2513 CXXDeleteExprBits.ArrayForm = ArrayForm;
2514 CXXDeleteExprBits.ArrayFormAsWritten = ArrayFormAsWritten;
2515 CXXDeleteExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
2516 CXXDeleteExprBits.Loc = Loc;
2517 setDependence(computeDependence(this));
2518 }
2519
CXXDeleteExpr(EmptyShell Shell)2520 explicit CXXDeleteExpr(EmptyShell Shell) : Expr(CXXDeleteExprClass, Shell) {}
2521
isGlobalDelete()2522 bool isGlobalDelete() const { return CXXDeleteExprBits.GlobalDelete; }
isArrayForm()2523 bool isArrayForm() const { return CXXDeleteExprBits.ArrayForm; }
isArrayFormAsWritten()2524 bool isArrayFormAsWritten() const {
2525 return CXXDeleteExprBits.ArrayFormAsWritten;
2526 }
2527
2528 /// Answers whether the usual array deallocation function for the
2529 /// allocated type expects the size of the allocation as a
2530 /// parameter. This can be true even if the actual deallocation
2531 /// function that we're using doesn't want a size.
doesUsualArrayDeleteWantSize()2532 bool doesUsualArrayDeleteWantSize() const {
2533 return CXXDeleteExprBits.UsualArrayDeleteWantsSize;
2534 }
2535
getOperatorDelete()2536 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2537
getArgument()2538 Expr *getArgument() { return cast<Expr>(Argument); }
getArgument()2539 const Expr *getArgument() const { return cast<Expr>(Argument); }
2540
2541 /// Retrieve the type being destroyed.
2542 ///
2543 /// If the type being destroyed is a dependent type which may or may not
2544 /// be a pointer, return an invalid type.
2545 QualType getDestroyedType() const;
2546
getBeginLoc()2547 SourceLocation getBeginLoc() const { return CXXDeleteExprBits.Loc; }
getEndLoc()2548 SourceLocation getEndLoc() const LLVM_READONLY {
2549 return Argument->getEndLoc();
2550 }
2551
classof(const Stmt * T)2552 static bool classof(const Stmt *T) {
2553 return T->getStmtClass() == CXXDeleteExprClass;
2554 }
2555
2556 // Iterators
children()2557 child_range children() { return child_range(&Argument, &Argument + 1); }
2558
children()2559 const_child_range children() const {
2560 return const_child_range(&Argument, &Argument + 1);
2561 }
2562 };
2563
2564 /// Stores the type being destroyed by a pseudo-destructor expression.
2565 class PseudoDestructorTypeStorage {
2566 /// Either the type source information or the name of the type, if
2567 /// it couldn't be resolved due to type-dependence.
2568 llvm::PointerUnion<TypeSourceInfo *, const IdentifierInfo *> Type;
2569
2570 /// The starting source location of the pseudo-destructor type.
2571 SourceLocation Location;
2572
2573 public:
2574 PseudoDestructorTypeStorage() = default;
2575
PseudoDestructorTypeStorage(const IdentifierInfo * II,SourceLocation Loc)2576 PseudoDestructorTypeStorage(const IdentifierInfo *II, SourceLocation Loc)
2577 : Type(II), Location(Loc) {}
2578
2579 PseudoDestructorTypeStorage(TypeSourceInfo *Info);
2580
getTypeSourceInfo()2581 TypeSourceInfo *getTypeSourceInfo() const {
2582 return Type.dyn_cast<TypeSourceInfo *>();
2583 }
2584
getIdentifier()2585 const IdentifierInfo *getIdentifier() const {
2586 return Type.dyn_cast<const IdentifierInfo *>();
2587 }
2588
getLocation()2589 SourceLocation getLocation() const { return Location; }
2590 };
2591
2592 /// Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
2593 ///
2594 /// A pseudo-destructor is an expression that looks like a member access to a
2595 /// destructor of a scalar type, except that scalar types don't have
2596 /// destructors. For example:
2597 ///
2598 /// \code
2599 /// typedef int T;
2600 /// void f(int *p) {
2601 /// p->T::~T();
2602 /// }
2603 /// \endcode
2604 ///
2605 /// Pseudo-destructors typically occur when instantiating templates such as:
2606 ///
2607 /// \code
2608 /// template<typename T>
2609 /// void destroy(T* ptr) {
2610 /// ptr->T::~T();
2611 /// }
2612 /// \endcode
2613 ///
2614 /// for scalar types. A pseudo-destructor expression has no run-time semantics
2615 /// beyond evaluating the base expression.
2616 class CXXPseudoDestructorExpr : public Expr {
2617 friend class ASTStmtReader;
2618
2619 /// The base expression (that is being destroyed).
2620 Stmt *Base = nullptr;
2621
2622 /// Whether the operator was an arrow ('->'); otherwise, it was a
2623 /// period ('.').
2624 LLVM_PREFERRED_TYPE(bool)
2625 bool IsArrow : 1;
2626
2627 /// The location of the '.' or '->' operator.
2628 SourceLocation OperatorLoc;
2629
2630 /// The nested-name-specifier that follows the operator, if present.
2631 NestedNameSpecifierLoc QualifierLoc;
2632
2633 /// The type that precedes the '::' in a qualified pseudo-destructor
2634 /// expression.
2635 TypeSourceInfo *ScopeType = nullptr;
2636
2637 /// The location of the '::' in a qualified pseudo-destructor
2638 /// expression.
2639 SourceLocation ColonColonLoc;
2640
2641 /// The location of the '~'.
2642 SourceLocation TildeLoc;
2643
2644 /// The type being destroyed, or its name if we were unable to
2645 /// resolve the name.
2646 PseudoDestructorTypeStorage DestroyedType;
2647
2648 public:
2649 CXXPseudoDestructorExpr(const ASTContext &Context,
2650 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
2651 NestedNameSpecifierLoc QualifierLoc,
2652 TypeSourceInfo *ScopeType,
2653 SourceLocation ColonColonLoc,
2654 SourceLocation TildeLoc,
2655 PseudoDestructorTypeStorage DestroyedType);
2656
CXXPseudoDestructorExpr(EmptyShell Shell)2657 explicit CXXPseudoDestructorExpr(EmptyShell Shell)
2658 : Expr(CXXPseudoDestructorExprClass, Shell), IsArrow(false) {}
2659
getBase()2660 Expr *getBase() const { return cast<Expr>(Base); }
2661
2662 /// Determines whether this member expression actually had
2663 /// a C++ nested-name-specifier prior to the name of the member, e.g.,
2664 /// x->Base::foo.
hasQualifier()2665 bool hasQualifier() const { return QualifierLoc.hasQualifier(); }
2666
2667 /// Retrieves the nested-name-specifier that qualifies the type name,
2668 /// with source-location information.
getQualifierLoc()2669 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2670
2671 /// If the member name was qualified, retrieves the
2672 /// nested-name-specifier that precedes the member name. Otherwise, returns
2673 /// null.
getQualifier()2674 NestedNameSpecifier *getQualifier() const {
2675 return QualifierLoc.getNestedNameSpecifier();
2676 }
2677
2678 /// Determine whether this pseudo-destructor expression was written
2679 /// using an '->' (otherwise, it used a '.').
isArrow()2680 bool isArrow() const { return IsArrow; }
2681
2682 /// Retrieve the location of the '.' or '->' operator.
getOperatorLoc()2683 SourceLocation getOperatorLoc() const { return OperatorLoc; }
2684
2685 /// Retrieve the scope type in a qualified pseudo-destructor
2686 /// expression.
2687 ///
2688 /// Pseudo-destructor expressions can have extra qualification within them
2689 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T().
2690 /// Here, if the object type of the expression is (or may be) a scalar type,
2691 /// \p T may also be a scalar type and, therefore, cannot be part of a
2692 /// nested-name-specifier. It is stored as the "scope type" of the pseudo-
2693 /// destructor expression.
getScopeTypeInfo()2694 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; }
2695
2696 /// Retrieve the location of the '::' in a qualified pseudo-destructor
2697 /// expression.
getColonColonLoc()2698 SourceLocation getColonColonLoc() const { return ColonColonLoc; }
2699
2700 /// Retrieve the location of the '~'.
getTildeLoc()2701 SourceLocation getTildeLoc() const { return TildeLoc; }
2702
2703 /// Retrieve the source location information for the type
2704 /// being destroyed.
2705 ///
2706 /// This type-source information is available for non-dependent
2707 /// pseudo-destructor expressions and some dependent pseudo-destructor
2708 /// expressions. Returns null if we only have the identifier for a
2709 /// dependent pseudo-destructor expression.
getDestroyedTypeInfo()2710 TypeSourceInfo *getDestroyedTypeInfo() const {
2711 return DestroyedType.getTypeSourceInfo();
2712 }
2713
2714 /// In a dependent pseudo-destructor expression for which we do not
2715 /// have full type information on the destroyed type, provides the name
2716 /// of the destroyed type.
getDestroyedTypeIdentifier()2717 const IdentifierInfo *getDestroyedTypeIdentifier() const {
2718 return DestroyedType.getIdentifier();
2719 }
2720
2721 /// Retrieve the type being destroyed.
2722 QualType getDestroyedType() const;
2723
2724 /// Retrieve the starting location of the type being destroyed.
getDestroyedTypeLoc()2725 SourceLocation getDestroyedTypeLoc() const {
2726 return DestroyedType.getLocation();
2727 }
2728
2729 /// Set the name of destroyed type for a dependent pseudo-destructor
2730 /// expression.
setDestroyedType(IdentifierInfo * II,SourceLocation Loc)2731 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) {
2732 DestroyedType = PseudoDestructorTypeStorage(II, Loc);
2733 }
2734
2735 /// Set the destroyed type.
setDestroyedType(TypeSourceInfo * Info)2736 void setDestroyedType(TypeSourceInfo *Info) {
2737 DestroyedType = PseudoDestructorTypeStorage(Info);
2738 }
2739
getBeginLoc()2740 SourceLocation getBeginLoc() const LLVM_READONLY {
2741 return Base->getBeginLoc();
2742 }
2743 SourceLocation getEndLoc() const LLVM_READONLY;
2744
classof(const Stmt * T)2745 static bool classof(const Stmt *T) {
2746 return T->getStmtClass() == CXXPseudoDestructorExprClass;
2747 }
2748
2749 // Iterators
children()2750 child_range children() { return child_range(&Base, &Base + 1); }
2751
children()2752 const_child_range children() const {
2753 return const_child_range(&Base, &Base + 1);
2754 }
2755 };
2756
2757 /// A type trait used in the implementation of various C++11 and
2758 /// Library TR1 trait templates.
2759 ///
2760 /// \code
2761 /// __is_pod(int) == true
2762 /// __is_enum(std::string) == false
2763 /// __is_trivially_constructible(vector<int>, int*, int*)
2764 /// \endcode
2765 class TypeTraitExpr final
2766 : public Expr,
2767 private llvm::TrailingObjects<TypeTraitExpr, TypeSourceInfo *> {
2768 /// The location of the type trait keyword.
2769 SourceLocation Loc;
2770
2771 /// The location of the closing parenthesis.
2772 SourceLocation RParenLoc;
2773
2774 // Note: The TypeSourceInfos for the arguments are allocated after the
2775 // TypeTraitExpr.
2776
2777 TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
2778 ArrayRef<TypeSourceInfo *> Args,
2779 SourceLocation RParenLoc,
2780 bool Value);
2781
TypeTraitExpr(EmptyShell Empty)2782 TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) {}
2783
numTrailingObjects(OverloadToken<TypeSourceInfo * >)2784 size_t numTrailingObjects(OverloadToken<TypeSourceInfo *>) const {
2785 return getNumArgs();
2786 }
2787
2788 public:
2789 friend class ASTStmtReader;
2790 friend class ASTStmtWriter;
2791 friend TrailingObjects;
2792
2793 /// Create a new type trait expression.
2794 static TypeTraitExpr *Create(const ASTContext &C, QualType T,
2795 SourceLocation Loc, TypeTrait Kind,
2796 ArrayRef<TypeSourceInfo *> Args,
2797 SourceLocation RParenLoc,
2798 bool Value);
2799
2800 static TypeTraitExpr *CreateDeserialized(const ASTContext &C,
2801 unsigned NumArgs);
2802
2803 /// Determine which type trait this expression uses.
getTrait()2804 TypeTrait getTrait() const {
2805 return static_cast<TypeTrait>(TypeTraitExprBits.Kind);
2806 }
2807
getValue()2808 bool getValue() const {
2809 assert(!isValueDependent());
2810 return TypeTraitExprBits.Value;
2811 }
2812
2813 /// Determine the number of arguments to this type trait.
getNumArgs()2814 unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; }
2815
2816 /// Retrieve the Ith argument.
getArg(unsigned I)2817 TypeSourceInfo *getArg(unsigned I) const {
2818 assert(I < getNumArgs() && "Argument out-of-range");
2819 return getArgs()[I];
2820 }
2821
2822 /// Retrieve the argument types.
getArgs()2823 ArrayRef<TypeSourceInfo *> getArgs() const {
2824 return llvm::ArrayRef(getTrailingObjects<TypeSourceInfo *>(), getNumArgs());
2825 }
2826
getBeginLoc()2827 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
getEndLoc()2828 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
2829
classof(const Stmt * T)2830 static bool classof(const Stmt *T) {
2831 return T->getStmtClass() == TypeTraitExprClass;
2832 }
2833
2834 // Iterators
children()2835 child_range children() {
2836 return child_range(child_iterator(), child_iterator());
2837 }
2838
children()2839 const_child_range children() const {
2840 return const_child_range(const_child_iterator(), const_child_iterator());
2841 }
2842 };
2843
2844 /// An Embarcadero array type trait, as used in the implementation of
2845 /// __array_rank and __array_extent.
2846 ///
2847 /// Example:
2848 /// \code
2849 /// __array_rank(int[10][20]) == 2
2850 /// __array_extent(int, 1) == 20
2851 /// \endcode
2852 class ArrayTypeTraitExpr : public Expr {
2853 /// The trait. An ArrayTypeTrait enum in MSVC compat unsigned.
2854 LLVM_PREFERRED_TYPE(ArrayTypeTrait)
2855 unsigned ATT : 2;
2856
2857 /// The value of the type trait. Unspecified if dependent.
2858 uint64_t Value = 0;
2859
2860 /// The array dimension being queried, or -1 if not used.
2861 Expr *Dimension;
2862
2863 /// The location of the type trait keyword.
2864 SourceLocation Loc;
2865
2866 /// The location of the closing paren.
2867 SourceLocation RParen;
2868
2869 /// The type being queried.
2870 TypeSourceInfo *QueriedType = nullptr;
2871
2872 public:
2873 friend class ASTStmtReader;
2874
ArrayTypeTraitExpr(SourceLocation loc,ArrayTypeTrait att,TypeSourceInfo * queried,uint64_t value,Expr * dimension,SourceLocation rparen,QualType ty)2875 ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att,
2876 TypeSourceInfo *queried, uint64_t value, Expr *dimension,
2877 SourceLocation rparen, QualType ty)
2878 : Expr(ArrayTypeTraitExprClass, ty, VK_PRValue, OK_Ordinary), ATT(att),
2879 Value(value), Dimension(dimension), Loc(loc), RParen(rparen),
2880 QueriedType(queried) {
2881 assert(att <= ATT_Last && "invalid enum value!");
2882 assert(static_cast<unsigned>(att) == ATT && "ATT overflow!");
2883 setDependence(computeDependence(this));
2884 }
2885
ArrayTypeTraitExpr(EmptyShell Empty)2886 explicit ArrayTypeTraitExpr(EmptyShell Empty)
2887 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0) {}
2888
getBeginLoc()2889 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
getEndLoc()2890 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
2891
getTrait()2892 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); }
2893
getQueriedType()2894 QualType getQueriedType() const { return QueriedType->getType(); }
2895
getQueriedTypeSourceInfo()2896 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; }
2897
getValue()2898 uint64_t getValue() const { assert(!isTypeDependent()); return Value; }
2899
getDimensionExpression()2900 Expr *getDimensionExpression() const { return Dimension; }
2901
classof(const Stmt * T)2902 static bool classof(const Stmt *T) {
2903 return T->getStmtClass() == ArrayTypeTraitExprClass;
2904 }
2905
2906 // Iterators
children()2907 child_range children() {
2908 return child_range(child_iterator(), child_iterator());
2909 }
2910
children()2911 const_child_range children() const {
2912 return const_child_range(const_child_iterator(), const_child_iterator());
2913 }
2914 };
2915
2916 /// An expression trait intrinsic.
2917 ///
2918 /// Example:
2919 /// \code
2920 /// __is_lvalue_expr(std::cout) == true
2921 /// __is_lvalue_expr(1) == false
2922 /// \endcode
2923 class ExpressionTraitExpr : public Expr {
2924 /// The trait. A ExpressionTrait enum in MSVC compatible unsigned.
2925 LLVM_PREFERRED_TYPE(ExpressionTrait)
2926 unsigned ET : 31;
2927
2928 /// The value of the type trait. Unspecified if dependent.
2929 LLVM_PREFERRED_TYPE(bool)
2930 unsigned Value : 1;
2931
2932 /// The location of the type trait keyword.
2933 SourceLocation Loc;
2934
2935 /// The location of the closing paren.
2936 SourceLocation RParen;
2937
2938 /// The expression being queried.
2939 Expr* QueriedExpression = nullptr;
2940
2941 public:
2942 friend class ASTStmtReader;
2943
ExpressionTraitExpr(SourceLocation loc,ExpressionTrait et,Expr * queried,bool value,SourceLocation rparen,QualType resultType)2944 ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et, Expr *queried,
2945 bool value, SourceLocation rparen, QualType resultType)
2946 : Expr(ExpressionTraitExprClass, resultType, VK_PRValue, OK_Ordinary),
2947 ET(et), Value(value), Loc(loc), RParen(rparen),
2948 QueriedExpression(queried) {
2949 assert(et <= ET_Last && "invalid enum value!");
2950 assert(static_cast<unsigned>(et) == ET && "ET overflow!");
2951 setDependence(computeDependence(this));
2952 }
2953
ExpressionTraitExpr(EmptyShell Empty)2954 explicit ExpressionTraitExpr(EmptyShell Empty)
2955 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false) {}
2956
getBeginLoc()2957 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
getEndLoc()2958 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
2959
getTrait()2960 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); }
2961
getQueriedExpression()2962 Expr *getQueriedExpression() const { return QueriedExpression; }
2963
getValue()2964 bool getValue() const { return Value; }
2965
classof(const Stmt * T)2966 static bool classof(const Stmt *T) {
2967 return T->getStmtClass() == ExpressionTraitExprClass;
2968 }
2969
2970 // Iterators
children()2971 child_range children() {
2972 return child_range(child_iterator(), child_iterator());
2973 }
2974
children()2975 const_child_range children() const {
2976 return const_child_range(const_child_iterator(), const_child_iterator());
2977 }
2978 };
2979
2980 /// A reference to an overloaded function set, either an
2981 /// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr.
2982 class OverloadExpr : public Expr {
2983 friend class ASTStmtReader;
2984 friend class ASTStmtWriter;
2985
2986 /// The common name of these declarations.
2987 DeclarationNameInfo NameInfo;
2988
2989 /// The nested-name-specifier that qualifies the name, if any.
2990 NestedNameSpecifierLoc QualifierLoc;
2991
2992 protected:
2993 OverloadExpr(StmtClass SC, const ASTContext &Context,
2994 NestedNameSpecifierLoc QualifierLoc,
2995 SourceLocation TemplateKWLoc,
2996 const DeclarationNameInfo &NameInfo,
2997 const TemplateArgumentListInfo *TemplateArgs,
2998 UnresolvedSetIterator Begin, UnresolvedSetIterator End,
2999 bool KnownDependent, bool KnownInstantiationDependent,
3000 bool KnownContainsUnexpandedParameterPack);
3001
3002 OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
3003 bool HasTemplateKWAndArgsInfo);
3004
3005 /// Return the results. Defined after UnresolvedMemberExpr.
3006 inline DeclAccessPair *getTrailingResults();
getTrailingResults()3007 const DeclAccessPair *getTrailingResults() const {
3008 return const_cast<OverloadExpr *>(this)->getTrailingResults();
3009 }
3010
3011 /// Return the optional template keyword and arguments info.
3012 /// Defined after UnresolvedMemberExpr.
3013 inline ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo();
getTrailingASTTemplateKWAndArgsInfo()3014 const ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo() const {
3015 return const_cast<OverloadExpr *>(this)
3016 ->getTrailingASTTemplateKWAndArgsInfo();
3017 }
3018
3019 /// Return the optional template arguments. Defined after
3020 /// UnresolvedMemberExpr.
3021 inline TemplateArgumentLoc *getTrailingTemplateArgumentLoc();
getTrailingTemplateArgumentLoc()3022 const TemplateArgumentLoc *getTrailingTemplateArgumentLoc() const {
3023 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
3024 }
3025
hasTemplateKWAndArgsInfo()3026 bool hasTemplateKWAndArgsInfo() const {
3027 return OverloadExprBits.HasTemplateKWAndArgsInfo;
3028 }
3029
3030 public:
3031 struct FindResult {
3032 OverloadExpr *Expression = nullptr;
3033 bool IsAddressOfOperand = false;
3034 bool IsAddressOfOperandWithParen = false;
3035 bool HasFormOfMemberPointer = false;
3036 };
3037
3038 /// Finds the overloaded expression in the given expression \p E of
3039 /// OverloadTy.
3040 ///
3041 /// \return the expression (which must be there) and true if it has
3042 /// the particular form of a member pointer expression
find(Expr * E)3043 static FindResult find(Expr *E) {
3044 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload));
3045
3046 FindResult Result;
3047 bool HasParen = isa<ParenExpr>(E);
3048
3049 E = E->IgnoreParens();
3050 if (isa<UnaryOperator>(E)) {
3051 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf);
3052 E = cast<UnaryOperator>(E)->getSubExpr();
3053 auto *Ovl = cast<OverloadExpr>(E->IgnoreParens());
3054
3055 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier());
3056 Result.IsAddressOfOperand = true;
3057 Result.IsAddressOfOperandWithParen = HasParen;
3058 Result.Expression = Ovl;
3059 } else {
3060 Result.Expression = cast<OverloadExpr>(E);
3061 }
3062
3063 return Result;
3064 }
3065
3066 /// Gets the naming class of this lookup, if any.
3067 /// Defined after UnresolvedMemberExpr.
3068 inline CXXRecordDecl *getNamingClass();
getNamingClass()3069 const CXXRecordDecl *getNamingClass() const {
3070 return const_cast<OverloadExpr *>(this)->getNamingClass();
3071 }
3072
3073 using decls_iterator = UnresolvedSetImpl::iterator;
3074
decls_begin()3075 decls_iterator decls_begin() const {
3076 return UnresolvedSetIterator(getTrailingResults());
3077 }
decls_end()3078 decls_iterator decls_end() const {
3079 return UnresolvedSetIterator(getTrailingResults() + getNumDecls());
3080 }
decls()3081 llvm::iterator_range<decls_iterator> decls() const {
3082 return llvm::make_range(decls_begin(), decls_end());
3083 }
3084
3085 /// Gets the number of declarations in the unresolved set.
getNumDecls()3086 unsigned getNumDecls() const { return OverloadExprBits.NumResults; }
3087
3088 /// Gets the full name info.
getNameInfo()3089 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
3090
3091 /// Gets the name looked up.
getName()3092 DeclarationName getName() const { return NameInfo.getName(); }
3093
3094 /// Gets the location of the name.
getNameLoc()3095 SourceLocation getNameLoc() const { return NameInfo.getLoc(); }
3096
3097 /// Fetches the nested-name qualifier, if one was given.
getQualifier()3098 NestedNameSpecifier *getQualifier() const {
3099 return QualifierLoc.getNestedNameSpecifier();
3100 }
3101
3102 /// Fetches the nested-name qualifier with source-location
3103 /// information, if one was given.
getQualifierLoc()3104 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3105
3106 /// Retrieve the location of the template keyword preceding
3107 /// this name, if any.
getTemplateKeywordLoc()3108 SourceLocation getTemplateKeywordLoc() const {
3109 if (!hasTemplateKWAndArgsInfo())
3110 return SourceLocation();
3111 return getTrailingASTTemplateKWAndArgsInfo()->TemplateKWLoc;
3112 }
3113
3114 /// Retrieve the location of the left angle bracket starting the
3115 /// explicit template argument list following the name, if any.
getLAngleLoc()3116 SourceLocation getLAngleLoc() const {
3117 if (!hasTemplateKWAndArgsInfo())
3118 return SourceLocation();
3119 return getTrailingASTTemplateKWAndArgsInfo()->LAngleLoc;
3120 }
3121
3122 /// Retrieve the location of the right angle bracket ending the
3123 /// explicit template argument list following the name, if any.
getRAngleLoc()3124 SourceLocation getRAngleLoc() const {
3125 if (!hasTemplateKWAndArgsInfo())
3126 return SourceLocation();
3127 return getTrailingASTTemplateKWAndArgsInfo()->RAngleLoc;
3128 }
3129
3130 /// Determines whether the name was preceded by the template keyword.
hasTemplateKeyword()3131 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
3132
3133 /// Determines whether this expression had explicit template arguments.
hasExplicitTemplateArgs()3134 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3135
getTemplateArgs()3136 TemplateArgumentLoc const *getTemplateArgs() const {
3137 if (!hasExplicitTemplateArgs())
3138 return nullptr;
3139 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
3140 }
3141
getNumTemplateArgs()3142 unsigned getNumTemplateArgs() const {
3143 if (!hasExplicitTemplateArgs())
3144 return 0;
3145
3146 return getTrailingASTTemplateKWAndArgsInfo()->NumTemplateArgs;
3147 }
3148
template_arguments()3149 ArrayRef<TemplateArgumentLoc> template_arguments() const {
3150 return {getTemplateArgs(), getNumTemplateArgs()};
3151 }
3152
3153 /// Copies the template arguments into the given structure.
copyTemplateArgumentsInto(TemplateArgumentListInfo & List)3154 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
3155 if (hasExplicitTemplateArgs())
3156 getTrailingASTTemplateKWAndArgsInfo()->copyInto(getTemplateArgs(), List);
3157 }
3158
classof(const Stmt * T)3159 static bool classof(const Stmt *T) {
3160 return T->getStmtClass() == UnresolvedLookupExprClass ||
3161 T->getStmtClass() == UnresolvedMemberExprClass;
3162 }
3163 };
3164
3165 /// A reference to a name which we were able to look up during
3166 /// parsing but could not resolve to a specific declaration.
3167 ///
3168 /// This arises in several ways:
3169 /// * we might be waiting for argument-dependent lookup;
3170 /// * the name might resolve to an overloaded function;
3171 /// * the name might resolve to a non-function template; for example, in the
3172 /// following snippet, the return expression of the member function
3173 /// 'foo()' might remain unresolved until instantiation:
3174 ///
3175 /// \code
3176 /// struct P {
3177 /// template <class T> using I = T;
3178 /// };
3179 ///
3180 /// struct Q {
3181 /// template <class T> int foo() {
3182 /// return T::template I<int>;
3183 /// }
3184 /// };
3185 /// \endcode
3186 ///
3187 /// ...which is distinct from modeling function overloads, and therefore we use
3188 /// a different builtin type 'UnresolvedTemplate' to avoid confusion. This is
3189 /// done in Sema::BuildTemplateIdExpr.
3190 ///
3191 /// and eventually:
3192 /// * the lookup might have included a function template.
3193 /// * the unresolved template gets transformed in an instantiation or gets
3194 /// diagnosed for its direct use.
3195 ///
3196 /// These never include UnresolvedUsingValueDecls, which are always class
3197 /// members and therefore appear only in UnresolvedMemberLookupExprs.
3198 class UnresolvedLookupExpr final
3199 : public OverloadExpr,
3200 private llvm::TrailingObjects<UnresolvedLookupExpr, DeclAccessPair,
3201 ASTTemplateKWAndArgsInfo,
3202 TemplateArgumentLoc> {
3203 friend class ASTStmtReader;
3204 friend class OverloadExpr;
3205 friend TrailingObjects;
3206
3207 /// The naming class (C++ [class.access.base]p5) of the lookup, if
3208 /// any. This can generally be recalculated from the context chain,
3209 /// but that can be fairly expensive for unqualified lookups.
3210 CXXRecordDecl *NamingClass;
3211
3212 // UnresolvedLookupExpr is followed by several trailing objects.
3213 // They are in order:
3214 //
3215 // * An array of getNumResults() DeclAccessPair for the results. These are
3216 // undesugared, which is to say, they may include UsingShadowDecls.
3217 // Access is relative to the naming class.
3218 //
3219 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3220 // template keyword and arguments. Present if and only if
3221 // hasTemplateKWAndArgsInfo().
3222 //
3223 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing
3224 // location information for the explicitly specified template arguments.
3225
3226 UnresolvedLookupExpr(const ASTContext &Context, CXXRecordDecl *NamingClass,
3227 NestedNameSpecifierLoc QualifierLoc,
3228 SourceLocation TemplateKWLoc,
3229 const DeclarationNameInfo &NameInfo, bool RequiresADL,
3230 const TemplateArgumentListInfo *TemplateArgs,
3231 UnresolvedSetIterator Begin, UnresolvedSetIterator End,
3232 bool KnownDependent, bool KnownInstantiationDependent);
3233
3234 UnresolvedLookupExpr(EmptyShell Empty, unsigned NumResults,
3235 bool HasTemplateKWAndArgsInfo);
3236
numTrailingObjects(OverloadToken<DeclAccessPair>)3237 unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const {
3238 return getNumDecls();
3239 }
3240
numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>)3241 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3242 return hasTemplateKWAndArgsInfo();
3243 }
3244
3245 public:
3246 static UnresolvedLookupExpr *
3247 Create(const ASTContext &Context, CXXRecordDecl *NamingClass,
3248 NestedNameSpecifierLoc QualifierLoc,
3249 const DeclarationNameInfo &NameInfo, bool RequiresADL,
3250 UnresolvedSetIterator Begin, UnresolvedSetIterator End,
3251 bool KnownDependent, bool KnownInstantiationDependent);
3252
3253 // After canonicalization, there may be dependent template arguments in
3254 // CanonicalConverted But none of Args is dependent. When any of
3255 // CanonicalConverted dependent, KnownDependent is true.
3256 static UnresolvedLookupExpr *
3257 Create(const ASTContext &Context, CXXRecordDecl *NamingClass,
3258 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
3259 const DeclarationNameInfo &NameInfo, bool RequiresADL,
3260 const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
3261 UnresolvedSetIterator End, bool KnownDependent,
3262 bool KnownInstantiationDependent);
3263
3264 static UnresolvedLookupExpr *CreateEmpty(const ASTContext &Context,
3265 unsigned NumResults,
3266 bool HasTemplateKWAndArgsInfo,
3267 unsigned NumTemplateArgs);
3268
3269 /// True if this declaration should be extended by
3270 /// argument-dependent lookup.
requiresADL()3271 bool requiresADL() const { return UnresolvedLookupExprBits.RequiresADL; }
3272
3273 /// Gets the 'naming class' (in the sense of C++0x
3274 /// [class.access.base]p5) of the lookup. This is the scope
3275 /// that was looked in to find these results.
getNamingClass()3276 CXXRecordDecl *getNamingClass() { return NamingClass; }
getNamingClass()3277 const CXXRecordDecl *getNamingClass() const { return NamingClass; }
3278
getBeginLoc()3279 SourceLocation getBeginLoc() const LLVM_READONLY {
3280 if (NestedNameSpecifierLoc l = getQualifierLoc())
3281 return l.getBeginLoc();
3282 return getNameInfo().getBeginLoc();
3283 }
3284
getEndLoc()3285 SourceLocation getEndLoc() const LLVM_READONLY {
3286 if (hasExplicitTemplateArgs())
3287 return getRAngleLoc();
3288 return getNameInfo().getEndLoc();
3289 }
3290
children()3291 child_range children() {
3292 return child_range(child_iterator(), child_iterator());
3293 }
3294
children()3295 const_child_range children() const {
3296 return const_child_range(const_child_iterator(), const_child_iterator());
3297 }
3298
classof(const Stmt * T)3299 static bool classof(const Stmt *T) {
3300 return T->getStmtClass() == UnresolvedLookupExprClass;
3301 }
3302 };
3303
3304 /// A qualified reference to a name whose declaration cannot
3305 /// yet be resolved.
3306 ///
3307 /// DependentScopeDeclRefExpr is similar to DeclRefExpr in that
3308 /// it expresses a reference to a declaration such as
3309 /// X<T>::value. The difference, however, is that an
3310 /// DependentScopeDeclRefExpr node is used only within C++ templates when
3311 /// the qualification (e.g., X<T>::) refers to a dependent type. In
3312 /// this case, X<T>::value cannot resolve to a declaration because the
3313 /// declaration will differ from one instantiation of X<T> to the
3314 /// next. Therefore, DependentScopeDeclRefExpr keeps track of the
3315 /// qualifier (X<T>::) and the name of the entity being referenced
3316 /// ("value"). Such expressions will instantiate to a DeclRefExpr once the
3317 /// declaration can be found.
3318 class DependentScopeDeclRefExpr final
3319 : public Expr,
3320 private llvm::TrailingObjects<DependentScopeDeclRefExpr,
3321 ASTTemplateKWAndArgsInfo,
3322 TemplateArgumentLoc> {
3323 friend class ASTStmtReader;
3324 friend class ASTStmtWriter;
3325 friend TrailingObjects;
3326
3327 /// The nested-name-specifier that qualifies this unresolved
3328 /// declaration name.
3329 NestedNameSpecifierLoc QualifierLoc;
3330
3331 /// The name of the entity we will be referencing.
3332 DeclarationNameInfo NameInfo;
3333
3334 DependentScopeDeclRefExpr(QualType Ty, NestedNameSpecifierLoc QualifierLoc,
3335 SourceLocation TemplateKWLoc,
3336 const DeclarationNameInfo &NameInfo,
3337 const TemplateArgumentListInfo *Args);
3338
numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>)3339 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3340 return hasTemplateKWAndArgsInfo();
3341 }
3342
hasTemplateKWAndArgsInfo()3343 bool hasTemplateKWAndArgsInfo() const {
3344 return DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo;
3345 }
3346
3347 public:
3348 static DependentScopeDeclRefExpr *
3349 Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
3350 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
3351 const TemplateArgumentListInfo *TemplateArgs);
3352
3353 static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &Context,
3354 bool HasTemplateKWAndArgsInfo,
3355 unsigned NumTemplateArgs);
3356
3357 /// Retrieve the name that this expression refers to.
getNameInfo()3358 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
3359
3360 /// Retrieve the name that this expression refers to.
getDeclName()3361 DeclarationName getDeclName() const { return NameInfo.getName(); }
3362
3363 /// Retrieve the location of the name within the expression.
3364 ///
3365 /// For example, in "X<T>::value" this is the location of "value".
getLocation()3366 SourceLocation getLocation() const { return NameInfo.getLoc(); }
3367
3368 /// Retrieve the nested-name-specifier that qualifies the
3369 /// name, with source location information.
getQualifierLoc()3370 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3371
3372 /// Retrieve the nested-name-specifier that qualifies this
3373 /// declaration.
getQualifier()3374 NestedNameSpecifier *getQualifier() const {
3375 return QualifierLoc.getNestedNameSpecifier();
3376 }
3377
3378 /// Retrieve the location of the template keyword preceding
3379 /// this name, if any.
getTemplateKeywordLoc()3380 SourceLocation getTemplateKeywordLoc() const {
3381 if (!hasTemplateKWAndArgsInfo())
3382 return SourceLocation();
3383 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
3384 }
3385
3386 /// Retrieve the location of the left angle bracket starting the
3387 /// explicit template argument list following the name, if any.
getLAngleLoc()3388 SourceLocation getLAngleLoc() const {
3389 if (!hasTemplateKWAndArgsInfo())
3390 return SourceLocation();
3391 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
3392 }
3393
3394 /// Retrieve the location of the right angle bracket ending the
3395 /// explicit template argument list following the name, if any.
getRAngleLoc()3396 SourceLocation getRAngleLoc() const {
3397 if (!hasTemplateKWAndArgsInfo())
3398 return SourceLocation();
3399 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
3400 }
3401
3402 /// Determines whether the name was preceded by the template keyword.
hasTemplateKeyword()3403 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
3404
3405 /// Determines whether this lookup had explicit template arguments.
hasExplicitTemplateArgs()3406 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3407
3408 /// Copies the template arguments (if present) into the given
3409 /// structure.
copyTemplateArgumentsInto(TemplateArgumentListInfo & List)3410 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
3411 if (hasExplicitTemplateArgs())
3412 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
3413 getTrailingObjects<TemplateArgumentLoc>(), List);
3414 }
3415
getTemplateArgs()3416 TemplateArgumentLoc const *getTemplateArgs() const {
3417 if (!hasExplicitTemplateArgs())
3418 return nullptr;
3419
3420 return getTrailingObjects<TemplateArgumentLoc>();
3421 }
3422
getNumTemplateArgs()3423 unsigned getNumTemplateArgs() const {
3424 if (!hasExplicitTemplateArgs())
3425 return 0;
3426
3427 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
3428 }
3429
template_arguments()3430 ArrayRef<TemplateArgumentLoc> template_arguments() const {
3431 return {getTemplateArgs(), getNumTemplateArgs()};
3432 }
3433
3434 /// Note: getBeginLoc() is the start of the whole DependentScopeDeclRefExpr,
3435 /// and differs from getLocation().getStart().
getBeginLoc()3436 SourceLocation getBeginLoc() const LLVM_READONLY {
3437 return QualifierLoc.getBeginLoc();
3438 }
3439
getEndLoc()3440 SourceLocation getEndLoc() const LLVM_READONLY {
3441 if (hasExplicitTemplateArgs())
3442 return getRAngleLoc();
3443 return getLocation();
3444 }
3445
classof(const Stmt * T)3446 static bool classof(const Stmt *T) {
3447 return T->getStmtClass() == DependentScopeDeclRefExprClass;
3448 }
3449
children()3450 child_range children() {
3451 return child_range(child_iterator(), child_iterator());
3452 }
3453
children()3454 const_child_range children() const {
3455 return const_child_range(const_child_iterator(), const_child_iterator());
3456 }
3457 };
3458
3459 /// Represents an expression -- generally a full-expression -- that
3460 /// introduces cleanups to be run at the end of the sub-expression's
3461 /// evaluation. The most common source of expression-introduced
3462 /// cleanups is temporary objects in C++, but several other kinds of
3463 /// expressions can create cleanups, including basically every
3464 /// call in ARC that returns an Objective-C pointer.
3465 ///
3466 /// This expression also tracks whether the sub-expression contains a
3467 /// potentially-evaluated block literal. The lifetime of a block
3468 /// literal is the extent of the enclosing scope.
3469 class ExprWithCleanups final
3470 : public FullExpr,
3471 private llvm::TrailingObjects<
3472 ExprWithCleanups,
3473 llvm::PointerUnion<BlockDecl *, CompoundLiteralExpr *>> {
3474 public:
3475 /// The type of objects that are kept in the cleanup.
3476 /// It's useful to remember the set of blocks and block-scoped compound
3477 /// literals; we could also remember the set of temporaries, but there's
3478 /// currently no need.
3479 using CleanupObject = llvm::PointerUnion<BlockDecl *, CompoundLiteralExpr *>;
3480
3481 private:
3482 friend class ASTStmtReader;
3483 friend TrailingObjects;
3484
3485 ExprWithCleanups(EmptyShell, unsigned NumObjects);
3486 ExprWithCleanups(Expr *SubExpr, bool CleanupsHaveSideEffects,
3487 ArrayRef<CleanupObject> Objects);
3488
3489 public:
3490 static ExprWithCleanups *Create(const ASTContext &C, EmptyShell empty,
3491 unsigned numObjects);
3492
3493 static ExprWithCleanups *Create(const ASTContext &C, Expr *subexpr,
3494 bool CleanupsHaveSideEffects,
3495 ArrayRef<CleanupObject> objects);
3496
getObjects()3497 ArrayRef<CleanupObject> getObjects() const {
3498 return llvm::ArrayRef(getTrailingObjects<CleanupObject>(), getNumObjects());
3499 }
3500
getNumObjects()3501 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; }
3502
getObject(unsigned i)3503 CleanupObject getObject(unsigned i) const {
3504 assert(i < getNumObjects() && "Index out of range");
3505 return getObjects()[i];
3506 }
3507
cleanupsHaveSideEffects()3508 bool cleanupsHaveSideEffects() const {
3509 return ExprWithCleanupsBits.CleanupsHaveSideEffects;
3510 }
3511
getBeginLoc()3512 SourceLocation getBeginLoc() const LLVM_READONLY {
3513 return SubExpr->getBeginLoc();
3514 }
3515
getEndLoc()3516 SourceLocation getEndLoc() const LLVM_READONLY {
3517 return SubExpr->getEndLoc();
3518 }
3519
3520 // Implement isa/cast/dyncast/etc.
classof(const Stmt * T)3521 static bool classof(const Stmt *T) {
3522 return T->getStmtClass() == ExprWithCleanupsClass;
3523 }
3524
3525 // Iterators
children()3526 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
3527
children()3528 const_child_range children() const {
3529 return const_child_range(&SubExpr, &SubExpr + 1);
3530 }
3531 };
3532
3533 /// Describes an explicit type conversion that uses functional
3534 /// notion but could not be resolved because one or more arguments are
3535 /// type-dependent.
3536 ///
3537 /// The explicit type conversions expressed by
3538 /// CXXUnresolvedConstructExpr have the form <tt>T(a1, a2, ..., aN)</tt>,
3539 /// where \c T is some type and \c a1, \c a2, ..., \c aN are values, and
3540 /// either \c T is a dependent type or one or more of the <tt>a</tt>'s is
3541 /// type-dependent. For example, this would occur in a template such
3542 /// as:
3543 ///
3544 /// \code
3545 /// template<typename T, typename A1>
3546 /// inline T make_a(const A1& a1) {
3547 /// return T(a1);
3548 /// }
3549 /// \endcode
3550 ///
3551 /// When the returned expression is instantiated, it may resolve to a
3552 /// constructor call, conversion function call, or some kind of type
3553 /// conversion.
3554 class CXXUnresolvedConstructExpr final
3555 : public Expr,
3556 private llvm::TrailingObjects<CXXUnresolvedConstructExpr, Expr *> {
3557 friend class ASTStmtReader;
3558 friend TrailingObjects;
3559
3560 /// The type being constructed, and whether the construct expression models
3561 /// list initialization or not.
3562 llvm::PointerIntPair<TypeSourceInfo *, 1> TypeAndInitForm;
3563
3564 /// The location of the left parentheses ('(').
3565 SourceLocation LParenLoc;
3566
3567 /// The location of the right parentheses (')').
3568 SourceLocation RParenLoc;
3569
3570 CXXUnresolvedConstructExpr(QualType T, TypeSourceInfo *TSI,
3571 SourceLocation LParenLoc, ArrayRef<Expr *> Args,
3572 SourceLocation RParenLoc, bool IsListInit);
3573
CXXUnresolvedConstructExpr(EmptyShell Empty,unsigned NumArgs)3574 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs)
3575 : Expr(CXXUnresolvedConstructExprClass, Empty) {
3576 CXXUnresolvedConstructExprBits.NumArgs = NumArgs;
3577 }
3578
3579 public:
3580 static CXXUnresolvedConstructExpr *
3581 Create(const ASTContext &Context, QualType T, TypeSourceInfo *TSI,
3582 SourceLocation LParenLoc, ArrayRef<Expr *> Args,
3583 SourceLocation RParenLoc, bool IsListInit);
3584
3585 static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &Context,
3586 unsigned NumArgs);
3587
3588 /// Retrieve the type that is being constructed, as specified
3589 /// in the source code.
getTypeAsWritten()3590 QualType getTypeAsWritten() const { return getTypeSourceInfo()->getType(); }
3591
3592 /// Retrieve the type source information for the type being
3593 /// constructed.
getTypeSourceInfo()3594 TypeSourceInfo *getTypeSourceInfo() const {
3595 return TypeAndInitForm.getPointer();
3596 }
3597
3598 /// Retrieve the location of the left parentheses ('(') that
3599 /// precedes the argument list.
getLParenLoc()3600 SourceLocation getLParenLoc() const { return LParenLoc; }
setLParenLoc(SourceLocation L)3601 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
3602
3603 /// Retrieve the location of the right parentheses (')') that
3604 /// follows the argument list.
getRParenLoc()3605 SourceLocation getRParenLoc() const { return RParenLoc; }
setRParenLoc(SourceLocation L)3606 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
3607
3608 /// Determine whether this expression models list-initialization.
3609 /// If so, there will be exactly one subexpression, which will be
3610 /// an InitListExpr.
isListInitialization()3611 bool isListInitialization() const { return TypeAndInitForm.getInt(); }
3612
3613 /// Retrieve the number of arguments.
getNumArgs()3614 unsigned getNumArgs() const { return CXXUnresolvedConstructExprBits.NumArgs; }
3615
3616 using arg_iterator = Expr **;
3617 using arg_range = llvm::iterator_range<arg_iterator>;
3618
arg_begin()3619 arg_iterator arg_begin() { return getTrailingObjects<Expr *>(); }
arg_end()3620 arg_iterator arg_end() { return arg_begin() + getNumArgs(); }
arguments()3621 arg_range arguments() { return arg_range(arg_begin(), arg_end()); }
3622
3623 using const_arg_iterator = const Expr* const *;
3624 using const_arg_range = llvm::iterator_range<const_arg_iterator>;
3625
arg_begin()3626 const_arg_iterator arg_begin() const { return getTrailingObjects<Expr *>(); }
arg_end()3627 const_arg_iterator arg_end() const { return arg_begin() + getNumArgs(); }
arguments()3628 const_arg_range arguments() const {
3629 return const_arg_range(arg_begin(), arg_end());
3630 }
3631
getArg(unsigned I)3632 Expr *getArg(unsigned I) {
3633 assert(I < getNumArgs() && "Argument index out-of-range");
3634 return arg_begin()[I];
3635 }
3636
getArg(unsigned I)3637 const Expr *getArg(unsigned I) const {
3638 assert(I < getNumArgs() && "Argument index out-of-range");
3639 return arg_begin()[I];
3640 }
3641
setArg(unsigned I,Expr * E)3642 void setArg(unsigned I, Expr *E) {
3643 assert(I < getNumArgs() && "Argument index out-of-range");
3644 arg_begin()[I] = E;
3645 }
3646
3647 SourceLocation getBeginLoc() const LLVM_READONLY;
getEndLoc()3648 SourceLocation getEndLoc() const LLVM_READONLY {
3649 if (!RParenLoc.isValid() && getNumArgs() > 0)
3650 return getArg(getNumArgs() - 1)->getEndLoc();
3651 return RParenLoc;
3652 }
3653
classof(const Stmt * T)3654 static bool classof(const Stmt *T) {
3655 return T->getStmtClass() == CXXUnresolvedConstructExprClass;
3656 }
3657
3658 // Iterators
children()3659 child_range children() {
3660 auto **begin = reinterpret_cast<Stmt **>(arg_begin());
3661 return child_range(begin, begin + getNumArgs());
3662 }
3663
children()3664 const_child_range children() const {
3665 auto **begin = reinterpret_cast<Stmt **>(
3666 const_cast<CXXUnresolvedConstructExpr *>(this)->arg_begin());
3667 return const_child_range(begin, begin + getNumArgs());
3668 }
3669 };
3670
3671 /// Represents a C++ member access expression where the actual
3672 /// member referenced could not be resolved because the base
3673 /// expression or the member name was dependent.
3674 ///
3675 /// Like UnresolvedMemberExprs, these can be either implicit or
3676 /// explicit accesses. It is only possible to get one of these with
3677 /// an implicit access if a qualifier is provided.
3678 class CXXDependentScopeMemberExpr final
3679 : public Expr,
3680 private llvm::TrailingObjects<CXXDependentScopeMemberExpr,
3681 ASTTemplateKWAndArgsInfo,
3682 TemplateArgumentLoc, NamedDecl *> {
3683 friend class ASTStmtReader;
3684 friend class ASTStmtWriter;
3685 friend TrailingObjects;
3686
3687 /// The expression for the base pointer or class reference,
3688 /// e.g., the \c x in x.f. Can be null in implicit accesses.
3689 Stmt *Base;
3690
3691 /// The type of the base expression. Never null, even for
3692 /// implicit accesses.
3693 QualType BaseType;
3694
3695 /// The nested-name-specifier that precedes the member name, if any.
3696 /// FIXME: This could be in principle store as a trailing object.
3697 /// However the performance impact of doing so should be investigated first.
3698 NestedNameSpecifierLoc QualifierLoc;
3699
3700 /// The member to which this member expression refers, which
3701 /// can be name, overloaded operator, or destructor.
3702 ///
3703 /// FIXME: could also be a template-id
3704 DeclarationNameInfo MemberNameInfo;
3705
3706 // CXXDependentScopeMemberExpr is followed by several trailing objects,
3707 // some of which optional. They are in order:
3708 //
3709 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3710 // template keyword and arguments. Present if and only if
3711 // hasTemplateKWAndArgsInfo().
3712 //
3713 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing location
3714 // information for the explicitly specified template arguments.
3715 //
3716 // * An optional NamedDecl *. In a qualified member access expression such
3717 // as t->Base::f, this member stores the resolves of name lookup in the
3718 // context of the member access expression, to be used at instantiation
3719 // time. Present if and only if hasFirstQualifierFoundInScope().
3720
hasTemplateKWAndArgsInfo()3721 bool hasTemplateKWAndArgsInfo() const {
3722 return CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo;
3723 }
3724
hasFirstQualifierFoundInScope()3725 bool hasFirstQualifierFoundInScope() const {
3726 return CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope;
3727 }
3728
numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>)3729 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3730 return hasTemplateKWAndArgsInfo();
3731 }
3732
numTrailingObjects(OverloadToken<TemplateArgumentLoc>)3733 unsigned numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const {
3734 return getNumTemplateArgs();
3735 }
3736
numTrailingObjects(OverloadToken<NamedDecl * >)3737 unsigned numTrailingObjects(OverloadToken<NamedDecl *>) const {
3738 return hasFirstQualifierFoundInScope();
3739 }
3740
3741 CXXDependentScopeMemberExpr(const ASTContext &Ctx, Expr *Base,
3742 QualType BaseType, bool IsArrow,
3743 SourceLocation OperatorLoc,
3744 NestedNameSpecifierLoc QualifierLoc,
3745 SourceLocation TemplateKWLoc,
3746 NamedDecl *FirstQualifierFoundInScope,
3747 DeclarationNameInfo MemberNameInfo,
3748 const TemplateArgumentListInfo *TemplateArgs);
3749
3750 CXXDependentScopeMemberExpr(EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
3751 bool HasFirstQualifierFoundInScope);
3752
3753 public:
3754 static CXXDependentScopeMemberExpr *
3755 Create(const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
3756 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
3757 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
3758 DeclarationNameInfo MemberNameInfo,
3759 const TemplateArgumentListInfo *TemplateArgs);
3760
3761 static CXXDependentScopeMemberExpr *
3762 CreateEmpty(const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
3763 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope);
3764
3765 /// True if this is an implicit access, i.e. one in which the
3766 /// member being accessed was not written in the source. The source
3767 /// location of the operator is invalid in this case.
isImplicitAccess()3768 bool isImplicitAccess() const {
3769 if (!Base)
3770 return true;
3771 return cast<Expr>(Base)->isImplicitCXXThis();
3772 }
3773
3774 /// Retrieve the base object of this member expressions,
3775 /// e.g., the \c x in \c x.m.
getBase()3776 Expr *getBase() const {
3777 assert(!isImplicitAccess());
3778 return cast<Expr>(Base);
3779 }
3780
getBaseType()3781 QualType getBaseType() const { return BaseType; }
3782
3783 /// Determine whether this member expression used the '->'
3784 /// operator; otherwise, it used the '.' operator.
isArrow()3785 bool isArrow() const { return CXXDependentScopeMemberExprBits.IsArrow; }
3786
3787 /// Retrieve the location of the '->' or '.' operator.
getOperatorLoc()3788 SourceLocation getOperatorLoc() const {
3789 return CXXDependentScopeMemberExprBits.OperatorLoc;
3790 }
3791
3792 /// Retrieve the nested-name-specifier that qualifies the member name.
getQualifier()3793 NestedNameSpecifier *getQualifier() const {
3794 return QualifierLoc.getNestedNameSpecifier();
3795 }
3796
3797 /// Retrieve the nested-name-specifier that qualifies the member
3798 /// name, with source location information.
getQualifierLoc()3799 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3800
3801 /// Retrieve the first part of the nested-name-specifier that was
3802 /// found in the scope of the member access expression when the member access
3803 /// was initially parsed.
3804 ///
3805 /// This function only returns a useful result when member access expression
3806 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration
3807 /// returned by this function describes what was found by unqualified name
3808 /// lookup for the identifier "Base" within the scope of the member access
3809 /// expression itself. At template instantiation time, this information is
3810 /// combined with the results of name lookup into the type of the object
3811 /// expression itself (the class type of x).
getFirstQualifierFoundInScope()3812 NamedDecl *getFirstQualifierFoundInScope() const {
3813 if (!hasFirstQualifierFoundInScope())
3814 return nullptr;
3815 return *getTrailingObjects<NamedDecl *>();
3816 }
3817
3818 /// Retrieve the name of the member that this expression refers to.
getMemberNameInfo()3819 const DeclarationNameInfo &getMemberNameInfo() const {
3820 return MemberNameInfo;
3821 }
3822
3823 /// Retrieve the name of the member that this expression refers to.
getMember()3824 DeclarationName getMember() const { return MemberNameInfo.getName(); }
3825
3826 // Retrieve the location of the name of the member that this
3827 // expression refers to.
getMemberLoc()3828 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); }
3829
3830 /// Retrieve the location of the template keyword preceding the
3831 /// member name, if any.
getTemplateKeywordLoc()3832 SourceLocation getTemplateKeywordLoc() const {
3833 if (!hasTemplateKWAndArgsInfo())
3834 return SourceLocation();
3835 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
3836 }
3837
3838 /// Retrieve the location of the left angle bracket starting the
3839 /// explicit template argument list following the member name, if any.
getLAngleLoc()3840 SourceLocation getLAngleLoc() const {
3841 if (!hasTemplateKWAndArgsInfo())
3842 return SourceLocation();
3843 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
3844 }
3845
3846 /// Retrieve the location of the right angle bracket ending the
3847 /// explicit template argument list following the member name, if any.
getRAngleLoc()3848 SourceLocation getRAngleLoc() const {
3849 if (!hasTemplateKWAndArgsInfo())
3850 return SourceLocation();
3851 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
3852 }
3853
3854 /// Determines whether the member name was preceded by the template keyword.
hasTemplateKeyword()3855 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
3856
3857 /// Determines whether this member expression actually had a C++
3858 /// template argument list explicitly specified, e.g., x.f<int>.
hasExplicitTemplateArgs()3859 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
3860
3861 /// Copies the template arguments (if present) into the given
3862 /// structure.
copyTemplateArgumentsInto(TemplateArgumentListInfo & List)3863 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
3864 if (hasExplicitTemplateArgs())
3865 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
3866 getTrailingObjects<TemplateArgumentLoc>(), List);
3867 }
3868
3869 /// Retrieve the template arguments provided as part of this
3870 /// template-id.
getTemplateArgs()3871 const TemplateArgumentLoc *getTemplateArgs() const {
3872 if (!hasExplicitTemplateArgs())
3873 return nullptr;
3874
3875 return getTrailingObjects<TemplateArgumentLoc>();
3876 }
3877
3878 /// Retrieve the number of template arguments provided as part of this
3879 /// template-id.
getNumTemplateArgs()3880 unsigned getNumTemplateArgs() const {
3881 if (!hasExplicitTemplateArgs())
3882 return 0;
3883
3884 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
3885 }
3886
template_arguments()3887 ArrayRef<TemplateArgumentLoc> template_arguments() const {
3888 return {getTemplateArgs(), getNumTemplateArgs()};
3889 }
3890
getBeginLoc()3891 SourceLocation getBeginLoc() const LLVM_READONLY {
3892 if (!isImplicitAccess())
3893 return Base->getBeginLoc();
3894 if (getQualifier())
3895 return getQualifierLoc().getBeginLoc();
3896 return MemberNameInfo.getBeginLoc();
3897 }
3898
getEndLoc()3899 SourceLocation getEndLoc() const LLVM_READONLY {
3900 if (hasExplicitTemplateArgs())
3901 return getRAngleLoc();
3902 return MemberNameInfo.getEndLoc();
3903 }
3904
classof(const Stmt * T)3905 static bool classof(const Stmt *T) {
3906 return T->getStmtClass() == CXXDependentScopeMemberExprClass;
3907 }
3908
3909 // Iterators
children()3910 child_range children() {
3911 if (isImplicitAccess())
3912 return child_range(child_iterator(), child_iterator());
3913 return child_range(&Base, &Base + 1);
3914 }
3915
children()3916 const_child_range children() const {
3917 if (isImplicitAccess())
3918 return const_child_range(const_child_iterator(), const_child_iterator());
3919 return const_child_range(&Base, &Base + 1);
3920 }
3921 };
3922
3923 /// Represents a C++ member access expression for which lookup
3924 /// produced a set of overloaded functions.
3925 ///
3926 /// The member access may be explicit or implicit:
3927 /// \code
3928 /// struct A {
3929 /// int a, b;
3930 /// int explicitAccess() { return this->a + this->A::b; }
3931 /// int implicitAccess() { return a + A::b; }
3932 /// };
3933 /// \endcode
3934 ///
3935 /// In the final AST, an explicit access always becomes a MemberExpr.
3936 /// An implicit access may become either a MemberExpr or a
3937 /// DeclRefExpr, depending on whether the member is static.
3938 class UnresolvedMemberExpr final
3939 : public OverloadExpr,
3940 private llvm::TrailingObjects<UnresolvedMemberExpr, DeclAccessPair,
3941 ASTTemplateKWAndArgsInfo,
3942 TemplateArgumentLoc> {
3943 friend class ASTStmtReader;
3944 friend class OverloadExpr;
3945 friend TrailingObjects;
3946
3947 /// The expression for the base pointer or class reference,
3948 /// e.g., the \c x in x.f.
3949 ///
3950 /// This can be null if this is an 'unbased' member expression.
3951 Stmt *Base;
3952
3953 /// The type of the base expression; never null.
3954 QualType BaseType;
3955
3956 /// The location of the '->' or '.' operator.
3957 SourceLocation OperatorLoc;
3958
3959 // UnresolvedMemberExpr is followed by several trailing objects.
3960 // They are in order:
3961 //
3962 // * An array of getNumResults() DeclAccessPair for the results. These are
3963 // undesugared, which is to say, they may include UsingShadowDecls.
3964 // Access is relative to the naming class.
3965 //
3966 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
3967 // template keyword and arguments. Present if and only if
3968 // hasTemplateKWAndArgsInfo().
3969 //
3970 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing
3971 // location information for the explicitly specified template arguments.
3972
3973 UnresolvedMemberExpr(const ASTContext &Context, bool HasUnresolvedUsing,
3974 Expr *Base, QualType BaseType, bool IsArrow,
3975 SourceLocation OperatorLoc,
3976 NestedNameSpecifierLoc QualifierLoc,
3977 SourceLocation TemplateKWLoc,
3978 const DeclarationNameInfo &MemberNameInfo,
3979 const TemplateArgumentListInfo *TemplateArgs,
3980 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
3981
3982 UnresolvedMemberExpr(EmptyShell Empty, unsigned NumResults,
3983 bool HasTemplateKWAndArgsInfo);
3984
numTrailingObjects(OverloadToken<DeclAccessPair>)3985 unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const {
3986 return getNumDecls();
3987 }
3988
numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>)3989 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
3990 return hasTemplateKWAndArgsInfo();
3991 }
3992
3993 public:
3994 static UnresolvedMemberExpr *
3995 Create(const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
3996 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
3997 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
3998 const DeclarationNameInfo &MemberNameInfo,
3999 const TemplateArgumentListInfo *TemplateArgs,
4000 UnresolvedSetIterator Begin, UnresolvedSetIterator End);
4001
4002 static UnresolvedMemberExpr *CreateEmpty(const ASTContext &Context,
4003 unsigned NumResults,
4004 bool HasTemplateKWAndArgsInfo,
4005 unsigned NumTemplateArgs);
4006
4007 /// True if this is an implicit access, i.e., one in which the
4008 /// member being accessed was not written in the source.
4009 ///
4010 /// The source location of the operator is invalid in this case.
4011 bool isImplicitAccess() const;
4012
4013 /// Retrieve the base object of this member expressions,
4014 /// e.g., the \c x in \c x.m.
getBase()4015 Expr *getBase() {
4016 assert(!isImplicitAccess());
4017 return cast<Expr>(Base);
4018 }
getBase()4019 const Expr *getBase() const {
4020 assert(!isImplicitAccess());
4021 return cast<Expr>(Base);
4022 }
4023
getBaseType()4024 QualType getBaseType() const { return BaseType; }
4025
4026 /// Determine whether the lookup results contain an unresolved using
4027 /// declaration.
hasUnresolvedUsing()4028 bool hasUnresolvedUsing() const {
4029 return UnresolvedMemberExprBits.HasUnresolvedUsing;
4030 }
4031
4032 /// Determine whether this member expression used the '->'
4033 /// operator; otherwise, it used the '.' operator.
isArrow()4034 bool isArrow() const { return UnresolvedMemberExprBits.IsArrow; }
4035
4036 /// Retrieve the location of the '->' or '.' operator.
getOperatorLoc()4037 SourceLocation getOperatorLoc() const { return OperatorLoc; }
4038
4039 /// Retrieve the naming class of this lookup.
4040 CXXRecordDecl *getNamingClass();
getNamingClass()4041 const CXXRecordDecl *getNamingClass() const {
4042 return const_cast<UnresolvedMemberExpr *>(this)->getNamingClass();
4043 }
4044
4045 /// Retrieve the full name info for the member that this expression
4046 /// refers to.
getMemberNameInfo()4047 const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); }
4048
4049 /// Retrieve the name of the member that this expression refers to.
getMemberName()4050 DeclarationName getMemberName() const { return getName(); }
4051
4052 /// Retrieve the location of the name of the member that this
4053 /// expression refers to.
getMemberLoc()4054 SourceLocation getMemberLoc() const { return getNameLoc(); }
4055
4056 /// Return the preferred location (the member name) for the arrow when
4057 /// diagnosing a problem with this expression.
getExprLoc()4058 SourceLocation getExprLoc() const LLVM_READONLY { return getMemberLoc(); }
4059
getBeginLoc()4060 SourceLocation getBeginLoc() const LLVM_READONLY {
4061 if (!isImplicitAccess())
4062 return Base->getBeginLoc();
4063 if (NestedNameSpecifierLoc l = getQualifierLoc())
4064 return l.getBeginLoc();
4065 return getMemberNameInfo().getBeginLoc();
4066 }
4067
getEndLoc()4068 SourceLocation getEndLoc() const LLVM_READONLY {
4069 if (hasExplicitTemplateArgs())
4070 return getRAngleLoc();
4071 return getMemberNameInfo().getEndLoc();
4072 }
4073
classof(const Stmt * T)4074 static bool classof(const Stmt *T) {
4075 return T->getStmtClass() == UnresolvedMemberExprClass;
4076 }
4077
4078 // Iterators
children()4079 child_range children() {
4080 if (isImplicitAccess())
4081 return child_range(child_iterator(), child_iterator());
4082 return child_range(&Base, &Base + 1);
4083 }
4084
children()4085 const_child_range children() const {
4086 if (isImplicitAccess())
4087 return const_child_range(const_child_iterator(), const_child_iterator());
4088 return const_child_range(&Base, &Base + 1);
4089 }
4090 };
4091
getTrailingResults()4092 DeclAccessPair *OverloadExpr::getTrailingResults() {
4093 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4094 return ULE->getTrailingObjects<DeclAccessPair>();
4095 return cast<UnresolvedMemberExpr>(this)->getTrailingObjects<DeclAccessPair>();
4096 }
4097
getTrailingASTTemplateKWAndArgsInfo()4098 ASTTemplateKWAndArgsInfo *OverloadExpr::getTrailingASTTemplateKWAndArgsInfo() {
4099 if (!hasTemplateKWAndArgsInfo())
4100 return nullptr;
4101
4102 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4103 return ULE->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
4104 return cast<UnresolvedMemberExpr>(this)
4105 ->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
4106 }
4107
getTrailingTemplateArgumentLoc()4108 TemplateArgumentLoc *OverloadExpr::getTrailingTemplateArgumentLoc() {
4109 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4110 return ULE->getTrailingObjects<TemplateArgumentLoc>();
4111 return cast<UnresolvedMemberExpr>(this)
4112 ->getTrailingObjects<TemplateArgumentLoc>();
4113 }
4114
getNamingClass()4115 CXXRecordDecl *OverloadExpr::getNamingClass() {
4116 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
4117 return ULE->getNamingClass();
4118 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
4119 }
4120
4121 /// Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
4122 ///
4123 /// The noexcept expression tests whether a given expression might throw. Its
4124 /// result is a boolean constant.
4125 class CXXNoexceptExpr : public Expr {
4126 friend class ASTStmtReader;
4127
4128 Stmt *Operand;
4129 SourceRange Range;
4130
4131 public:
CXXNoexceptExpr(QualType Ty,Expr * Operand,CanThrowResult Val,SourceLocation Keyword,SourceLocation RParen)4132 CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val,
4133 SourceLocation Keyword, SourceLocation RParen)
4134 : Expr(CXXNoexceptExprClass, Ty, VK_PRValue, OK_Ordinary),
4135 Operand(Operand), Range(Keyword, RParen) {
4136 CXXNoexceptExprBits.Value = Val == CT_Cannot;
4137 setDependence(computeDependence(this, Val));
4138 }
4139
CXXNoexceptExpr(EmptyShell Empty)4140 CXXNoexceptExpr(EmptyShell Empty) : Expr(CXXNoexceptExprClass, Empty) {}
4141
getOperand()4142 Expr *getOperand() const { return static_cast<Expr *>(Operand); }
4143
getBeginLoc()4144 SourceLocation getBeginLoc() const { return Range.getBegin(); }
getEndLoc()4145 SourceLocation getEndLoc() const { return Range.getEnd(); }
getSourceRange()4146 SourceRange getSourceRange() const { return Range; }
4147
getValue()4148 bool getValue() const { return CXXNoexceptExprBits.Value; }
4149
classof(const Stmt * T)4150 static bool classof(const Stmt *T) {
4151 return T->getStmtClass() == CXXNoexceptExprClass;
4152 }
4153
4154 // Iterators
children()4155 child_range children() { return child_range(&Operand, &Operand + 1); }
4156
children()4157 const_child_range children() const {
4158 return const_child_range(&Operand, &Operand + 1);
4159 }
4160 };
4161
4162 /// Represents a C++11 pack expansion that produces a sequence of
4163 /// expressions.
4164 ///
4165 /// A pack expansion expression contains a pattern (which itself is an
4166 /// expression) followed by an ellipsis. For example:
4167 ///
4168 /// \code
4169 /// template<typename F, typename ...Types>
4170 /// void forward(F f, Types &&...args) {
4171 /// f(static_cast<Types&&>(args)...);
4172 /// }
4173 /// \endcode
4174 ///
4175 /// Here, the argument to the function object \c f is a pack expansion whose
4176 /// pattern is \c static_cast<Types&&>(args). When the \c forward function
4177 /// template is instantiated, the pack expansion will instantiate to zero or
4178 /// or more function arguments to the function object \c f.
4179 class PackExpansionExpr : public Expr {
4180 friend class ASTStmtReader;
4181 friend class ASTStmtWriter;
4182
4183 SourceLocation EllipsisLoc;
4184
4185 /// The number of expansions that will be produced by this pack
4186 /// expansion expression, if known.
4187 ///
4188 /// When zero, the number of expansions is not known. Otherwise, this value
4189 /// is the number of expansions + 1.
4190 unsigned NumExpansions;
4191
4192 Stmt *Pattern;
4193
4194 public:
PackExpansionExpr(QualType T,Expr * Pattern,SourceLocation EllipsisLoc,std::optional<unsigned> NumExpansions)4195 PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc,
4196 std::optional<unsigned> NumExpansions)
4197 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
4198 Pattern->getObjectKind()),
4199 EllipsisLoc(EllipsisLoc),
4200 NumExpansions(NumExpansions ? *NumExpansions + 1 : 0),
4201 Pattern(Pattern) {
4202 setDependence(computeDependence(this));
4203 }
4204
PackExpansionExpr(EmptyShell Empty)4205 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) {}
4206
4207 /// Retrieve the pattern of the pack expansion.
getPattern()4208 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); }
4209
4210 /// Retrieve the pattern of the pack expansion.
getPattern()4211 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); }
4212
4213 /// Retrieve the location of the ellipsis that describes this pack
4214 /// expansion.
getEllipsisLoc()4215 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
4216
4217 /// Determine the number of expansions that will be produced when
4218 /// this pack expansion is instantiated, if already known.
getNumExpansions()4219 std::optional<unsigned> getNumExpansions() const {
4220 if (NumExpansions)
4221 return NumExpansions - 1;
4222
4223 return std::nullopt;
4224 }
4225
getBeginLoc()4226 SourceLocation getBeginLoc() const LLVM_READONLY {
4227 return Pattern->getBeginLoc();
4228 }
4229
getEndLoc()4230 SourceLocation getEndLoc() const LLVM_READONLY { return EllipsisLoc; }
4231
classof(const Stmt * T)4232 static bool classof(const Stmt *T) {
4233 return T->getStmtClass() == PackExpansionExprClass;
4234 }
4235
4236 // Iterators
children()4237 child_range children() {
4238 return child_range(&Pattern, &Pattern + 1);
4239 }
4240
children()4241 const_child_range children() const {
4242 return const_child_range(&Pattern, &Pattern + 1);
4243 }
4244 };
4245
4246 /// Represents an expression that computes the length of a parameter
4247 /// pack.
4248 ///
4249 /// \code
4250 /// template<typename ...Types>
4251 /// struct count {
4252 /// static const unsigned value = sizeof...(Types);
4253 /// };
4254 /// \endcode
4255 class SizeOfPackExpr final
4256 : public Expr,
4257 private llvm::TrailingObjects<SizeOfPackExpr, TemplateArgument> {
4258 friend class ASTStmtReader;
4259 friend class ASTStmtWriter;
4260 friend TrailingObjects;
4261
4262 /// The location of the \c sizeof keyword.
4263 SourceLocation OperatorLoc;
4264
4265 /// The location of the name of the parameter pack.
4266 SourceLocation PackLoc;
4267
4268 /// The location of the closing parenthesis.
4269 SourceLocation RParenLoc;
4270
4271 /// The length of the parameter pack, if known.
4272 ///
4273 /// When this expression is not value-dependent, this is the length of
4274 /// the pack. When the expression was parsed rather than instantiated
4275 /// (and thus is value-dependent), this is zero.
4276 ///
4277 /// After partial substitution into a sizeof...(X) expression (for instance,
4278 /// within an alias template or during function template argument deduction),
4279 /// we store a trailing array of partially-substituted TemplateArguments,
4280 /// and this is the length of that array.
4281 unsigned Length;
4282
4283 /// The parameter pack.
4284 NamedDecl *Pack = nullptr;
4285
4286 /// Create an expression that computes the length of
4287 /// the given parameter pack.
SizeOfPackExpr(QualType SizeType,SourceLocation OperatorLoc,NamedDecl * Pack,SourceLocation PackLoc,SourceLocation RParenLoc,std::optional<unsigned> Length,ArrayRef<TemplateArgument> PartialArgs)4288 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack,
4289 SourceLocation PackLoc, SourceLocation RParenLoc,
4290 std::optional<unsigned> Length,
4291 ArrayRef<TemplateArgument> PartialArgs)
4292 : Expr(SizeOfPackExprClass, SizeType, VK_PRValue, OK_Ordinary),
4293 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc),
4294 Length(Length ? *Length : PartialArgs.size()), Pack(Pack) {
4295 assert((!Length || PartialArgs.empty()) &&
4296 "have partial args for non-dependent sizeof... expression");
4297 auto *Args = getTrailingObjects<TemplateArgument>();
4298 std::uninitialized_copy(PartialArgs.begin(), PartialArgs.end(), Args);
4299 setDependence(Length ? ExprDependence::None
4300 : ExprDependence::ValueInstantiation);
4301 }
4302
4303 /// Create an empty expression.
SizeOfPackExpr(EmptyShell Empty,unsigned NumPartialArgs)4304 SizeOfPackExpr(EmptyShell Empty, unsigned NumPartialArgs)
4305 : Expr(SizeOfPackExprClass, Empty), Length(NumPartialArgs) {}
4306
4307 public:
4308 static SizeOfPackExpr *
4309 Create(ASTContext &Context, SourceLocation OperatorLoc, NamedDecl *Pack,
4310 SourceLocation PackLoc, SourceLocation RParenLoc,
4311 std::optional<unsigned> Length = std::nullopt,
4312 ArrayRef<TemplateArgument> PartialArgs = std::nullopt);
4313 static SizeOfPackExpr *CreateDeserialized(ASTContext &Context,
4314 unsigned NumPartialArgs);
4315
4316 /// Determine the location of the 'sizeof' keyword.
getOperatorLoc()4317 SourceLocation getOperatorLoc() const { return OperatorLoc; }
4318
4319 /// Determine the location of the parameter pack.
getPackLoc()4320 SourceLocation getPackLoc() const { return PackLoc; }
4321
4322 /// Determine the location of the right parenthesis.
getRParenLoc()4323 SourceLocation getRParenLoc() const { return RParenLoc; }
4324
4325 /// Retrieve the parameter pack.
getPack()4326 NamedDecl *getPack() const { return Pack; }
4327
4328 /// Retrieve the length of the parameter pack.
4329 ///
4330 /// This routine may only be invoked when the expression is not
4331 /// value-dependent.
getPackLength()4332 unsigned getPackLength() const {
4333 assert(!isValueDependent() &&
4334 "Cannot get the length of a value-dependent pack size expression");
4335 return Length;
4336 }
4337
4338 /// Determine whether this represents a partially-substituted sizeof...
4339 /// expression, such as is produced for:
4340 ///
4341 /// template<typename ...Ts> using X = int[sizeof...(Ts)];
4342 /// template<typename ...Us> void f(X<Us..., 1, 2, 3, Us...>);
isPartiallySubstituted()4343 bool isPartiallySubstituted() const {
4344 return isValueDependent() && Length;
4345 }
4346
4347 /// Get
getPartialArguments()4348 ArrayRef<TemplateArgument> getPartialArguments() const {
4349 assert(isPartiallySubstituted());
4350 const auto *Args = getTrailingObjects<TemplateArgument>();
4351 return llvm::ArrayRef(Args, Args + Length);
4352 }
4353
getBeginLoc()4354 SourceLocation getBeginLoc() const LLVM_READONLY { return OperatorLoc; }
getEndLoc()4355 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
4356
classof(const Stmt * T)4357 static bool classof(const Stmt *T) {
4358 return T->getStmtClass() == SizeOfPackExprClass;
4359 }
4360
4361 // Iterators
children()4362 child_range children() {
4363 return child_range(child_iterator(), child_iterator());
4364 }
4365
children()4366 const_child_range children() const {
4367 return const_child_range(const_child_iterator(), const_child_iterator());
4368 }
4369 };
4370
4371 class PackIndexingExpr final
4372 : public Expr,
4373 private llvm::TrailingObjects<PackIndexingExpr, Expr *> {
4374 friend class ASTStmtReader;
4375 friend class ASTStmtWriter;
4376 friend TrailingObjects;
4377
4378 SourceLocation EllipsisLoc;
4379
4380 // The location of the closing bracket
4381 SourceLocation RSquareLoc;
4382
4383 // The pack being indexed, followed by the index
4384 Stmt *SubExprs[2];
4385
4386 // The size of the trailing expressions.
4387 unsigned TransformedExpressions : 31;
4388
4389 LLVM_PREFERRED_TYPE(bool)
4390 unsigned ExpandedToEmptyPack : 1;
4391
4392 PackIndexingExpr(QualType Type, SourceLocation EllipsisLoc,
4393 SourceLocation RSquareLoc, Expr *PackIdExpr, Expr *IndexExpr,
4394 ArrayRef<Expr *> SubstitutedExprs = {},
4395 bool ExpandedToEmptyPack = false)
Expr(PackIndexingExprClass,Type,VK_LValue,OK_Ordinary)4396 : Expr(PackIndexingExprClass, Type, VK_LValue, OK_Ordinary),
4397 EllipsisLoc(EllipsisLoc), RSquareLoc(RSquareLoc),
4398 SubExprs{PackIdExpr, IndexExpr},
4399 TransformedExpressions(SubstitutedExprs.size()),
4400 ExpandedToEmptyPack(ExpandedToEmptyPack) {
4401
4402 auto *Exprs = getTrailingObjects<Expr *>();
4403 std::uninitialized_copy(SubstitutedExprs.begin(), SubstitutedExprs.end(),
4404 Exprs);
4405
4406 setDependence(computeDependence(this));
4407 if (!isInstantiationDependent())
4408 setValueKind(getSelectedExpr()->getValueKind());
4409 }
4410
4411 /// Create an empty expression.
PackIndexingExpr(EmptyShell Empty)4412 PackIndexingExpr(EmptyShell Empty) : Expr(PackIndexingExprClass, Empty) {}
4413
numTrailingObjects(OverloadToken<Expr * >)4414 unsigned numTrailingObjects(OverloadToken<Expr *>) const {
4415 return TransformedExpressions;
4416 }
4417
4418 public:
4419 static PackIndexingExpr *Create(ASTContext &Context,
4420 SourceLocation EllipsisLoc,
4421 SourceLocation RSquareLoc, Expr *PackIdExpr,
4422 Expr *IndexExpr, std::optional<int64_t> Index,
4423 ArrayRef<Expr *> SubstitutedExprs = {},
4424 bool ExpandedToEmptyPack = false);
4425 static PackIndexingExpr *CreateDeserialized(ASTContext &Context,
4426 unsigned NumTransformedExprs);
4427
4428 /// Determine if the expression was expanded to empty.
expandsToEmptyPack()4429 bool expandsToEmptyPack() const { return ExpandedToEmptyPack; }
4430
4431 /// Determine the location of the 'sizeof' keyword.
getEllipsisLoc()4432 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
4433
4434 /// Determine the location of the parameter pack.
getPackLoc()4435 SourceLocation getPackLoc() const { return SubExprs[0]->getBeginLoc(); }
4436
4437 /// Determine the location of the right parenthesis.
getRSquareLoc()4438 SourceLocation getRSquareLoc() const { return RSquareLoc; }
4439
getBeginLoc()4440 SourceLocation getBeginLoc() const LLVM_READONLY { return getPackLoc(); }
getEndLoc()4441 SourceLocation getEndLoc() const LLVM_READONLY { return RSquareLoc; }
4442
getPackIdExpression()4443 Expr *getPackIdExpression() const { return cast<Expr>(SubExprs[0]); }
4444
4445 NamedDecl *getPackDecl() const;
4446
getIndexExpr()4447 Expr *getIndexExpr() const { return cast<Expr>(SubExprs[1]); }
4448
getSelectedIndex()4449 std::optional<unsigned> getSelectedIndex() const {
4450 if (isInstantiationDependent())
4451 return std::nullopt;
4452 ConstantExpr *CE = cast<ConstantExpr>(getIndexExpr());
4453 auto Index = CE->getResultAsAPSInt();
4454 assert(Index.isNonNegative() && "Invalid index");
4455 return static_cast<unsigned>(Index.getExtValue());
4456 }
4457
getSelectedExpr()4458 Expr *getSelectedExpr() const {
4459 std::optional<unsigned> Index = getSelectedIndex();
4460 assert(Index && "extracting the indexed expression of a dependant pack");
4461 return getTrailingObjects<Expr *>()[*Index];
4462 }
4463
4464 /// Return the trailing expressions, regardless of the expansion.
getExpressions()4465 ArrayRef<Expr *> getExpressions() const {
4466 return {getTrailingObjects<Expr *>(), TransformedExpressions};
4467 }
4468
classof(const Stmt * T)4469 static bool classof(const Stmt *T) {
4470 return T->getStmtClass() == PackIndexingExprClass;
4471 }
4472
4473 // Iterators
children()4474 child_range children() { return child_range(SubExprs, SubExprs + 2); }
4475
children()4476 const_child_range children() const {
4477 return const_child_range(SubExprs, SubExprs + 2);
4478 }
4479 };
4480
4481 /// Represents a reference to a non-type template parameter
4482 /// that has been substituted with a template argument.
4483 class SubstNonTypeTemplateParmExpr : public Expr {
4484 friend class ASTReader;
4485 friend class ASTStmtReader;
4486
4487 /// The replacement expression.
4488 Stmt *Replacement;
4489
4490 /// The associated declaration and a flag indicating if it was a reference
4491 /// parameter. For class NTTPs, we can't determine that based on the value
4492 /// category alone.
4493 llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndRef;
4494
4495 unsigned Index : 15;
4496 unsigned PackIndex : 16;
4497
SubstNonTypeTemplateParmExpr(EmptyShell Empty)4498 explicit SubstNonTypeTemplateParmExpr(EmptyShell Empty)
4499 : Expr(SubstNonTypeTemplateParmExprClass, Empty) {}
4500
4501 public:
SubstNonTypeTemplateParmExpr(QualType Ty,ExprValueKind ValueKind,SourceLocation Loc,Expr * Replacement,Decl * AssociatedDecl,unsigned Index,std::optional<unsigned> PackIndex,bool RefParam)4502 SubstNonTypeTemplateParmExpr(QualType Ty, ExprValueKind ValueKind,
4503 SourceLocation Loc, Expr *Replacement,
4504 Decl *AssociatedDecl, unsigned Index,
4505 std::optional<unsigned> PackIndex, bool RefParam)
4506 : Expr(SubstNonTypeTemplateParmExprClass, Ty, ValueKind, OK_Ordinary),
4507 Replacement(Replacement),
4508 AssociatedDeclAndRef(AssociatedDecl, RefParam), Index(Index),
4509 PackIndex(PackIndex ? *PackIndex + 1 : 0) {
4510 assert(AssociatedDecl != nullptr);
4511 SubstNonTypeTemplateParmExprBits.NameLoc = Loc;
4512 setDependence(computeDependence(this));
4513 }
4514
getNameLoc()4515 SourceLocation getNameLoc() const {
4516 return SubstNonTypeTemplateParmExprBits.NameLoc;
4517 }
getBeginLoc()4518 SourceLocation getBeginLoc() const { return getNameLoc(); }
getEndLoc()4519 SourceLocation getEndLoc() const { return getNameLoc(); }
4520
getReplacement()4521 Expr *getReplacement() const { return cast<Expr>(Replacement); }
4522
4523 /// A template-like entity which owns the whole pattern being substituted.
4524 /// This will own a set of template parameters.
getAssociatedDecl()4525 Decl *getAssociatedDecl() const { return AssociatedDeclAndRef.getPointer(); }
4526
4527 /// Returns the index of the replaced parameter in the associated declaration.
4528 /// This should match the result of `getParameter()->getIndex()`.
getIndex()4529 unsigned getIndex() const { return Index; }
4530
getPackIndex()4531 std::optional<unsigned> getPackIndex() const {
4532 if (PackIndex == 0)
4533 return std::nullopt;
4534 return PackIndex - 1;
4535 }
4536
4537 NonTypeTemplateParmDecl *getParameter() const;
4538
isReferenceParameter()4539 bool isReferenceParameter() const { return AssociatedDeclAndRef.getInt(); }
4540
4541 /// Determine the substituted type of the template parameter.
4542 QualType getParameterType(const ASTContext &Ctx) const;
4543
classof(const Stmt * s)4544 static bool classof(const Stmt *s) {
4545 return s->getStmtClass() == SubstNonTypeTemplateParmExprClass;
4546 }
4547
4548 // Iterators
children()4549 child_range children() { return child_range(&Replacement, &Replacement + 1); }
4550
children()4551 const_child_range children() const {
4552 return const_child_range(&Replacement, &Replacement + 1);
4553 }
4554 };
4555
4556 /// Represents a reference to a non-type template parameter pack that
4557 /// has been substituted with a non-template argument pack.
4558 ///
4559 /// When a pack expansion in the source code contains multiple parameter packs
4560 /// and those parameter packs correspond to different levels of template
4561 /// parameter lists, this node is used to represent a non-type template
4562 /// parameter pack from an outer level, which has already had its argument pack
4563 /// substituted but that still lives within a pack expansion that itself
4564 /// could not be instantiated. When actually performing a substitution into
4565 /// that pack expansion (e.g., when all template parameters have corresponding
4566 /// arguments), this type will be replaced with the appropriate underlying
4567 /// expression at the current pack substitution index.
4568 class SubstNonTypeTemplateParmPackExpr : public Expr {
4569 friend class ASTReader;
4570 friend class ASTStmtReader;
4571
4572 /// The non-type template parameter pack itself.
4573 Decl *AssociatedDecl;
4574
4575 /// A pointer to the set of template arguments that this
4576 /// parameter pack is instantiated with.
4577 const TemplateArgument *Arguments;
4578
4579 /// The number of template arguments in \c Arguments.
4580 unsigned NumArguments : 16;
4581
4582 unsigned Index : 16;
4583
4584 /// The location of the non-type template parameter pack reference.
4585 SourceLocation NameLoc;
4586
SubstNonTypeTemplateParmPackExpr(EmptyShell Empty)4587 explicit SubstNonTypeTemplateParmPackExpr(EmptyShell Empty)
4588 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) {}
4589
4590 public:
4591 SubstNonTypeTemplateParmPackExpr(QualType T, ExprValueKind ValueKind,
4592 SourceLocation NameLoc,
4593 const TemplateArgument &ArgPack,
4594 Decl *AssociatedDecl, unsigned Index);
4595
4596 /// A template-like entity which owns the whole pattern being substituted.
4597 /// This will own a set of template parameters.
getAssociatedDecl()4598 Decl *getAssociatedDecl() const { return AssociatedDecl; }
4599
4600 /// Returns the index of the replaced parameter in the associated declaration.
4601 /// This should match the result of `getParameterPack()->getIndex()`.
getIndex()4602 unsigned getIndex() const { return Index; }
4603
4604 /// Retrieve the non-type template parameter pack being substituted.
4605 NonTypeTemplateParmDecl *getParameterPack() const;
4606
4607 /// Retrieve the location of the parameter pack name.
getParameterPackLocation()4608 SourceLocation getParameterPackLocation() const { return NameLoc; }
4609
4610 /// Retrieve the template argument pack containing the substituted
4611 /// template arguments.
4612 TemplateArgument getArgumentPack() const;
4613
getBeginLoc()4614 SourceLocation getBeginLoc() const LLVM_READONLY { return NameLoc; }
getEndLoc()4615 SourceLocation getEndLoc() const LLVM_READONLY { return NameLoc; }
4616
classof(const Stmt * T)4617 static bool classof(const Stmt *T) {
4618 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass;
4619 }
4620
4621 // Iterators
children()4622 child_range children() {
4623 return child_range(child_iterator(), child_iterator());
4624 }
4625
children()4626 const_child_range children() const {
4627 return const_child_range(const_child_iterator(), const_child_iterator());
4628 }
4629 };
4630
4631 /// Represents a reference to a function parameter pack or init-capture pack
4632 /// that has been substituted but not yet expanded.
4633 ///
4634 /// When a pack expansion contains multiple parameter packs at different levels,
4635 /// this node is used to represent a function parameter pack at an outer level
4636 /// which we have already substituted to refer to expanded parameters, but where
4637 /// the containing pack expansion cannot yet be expanded.
4638 ///
4639 /// \code
4640 /// template<typename...Ts> struct S {
4641 /// template<typename...Us> auto f(Ts ...ts) -> decltype(g(Us(ts)...));
4642 /// };
4643 /// template struct S<int, int>;
4644 /// \endcode
4645 class FunctionParmPackExpr final
4646 : public Expr,
4647 private llvm::TrailingObjects<FunctionParmPackExpr, VarDecl *> {
4648 friend class ASTReader;
4649 friend class ASTStmtReader;
4650 friend TrailingObjects;
4651
4652 /// The function parameter pack which was referenced.
4653 VarDecl *ParamPack;
4654
4655 /// The location of the function parameter pack reference.
4656 SourceLocation NameLoc;
4657
4658 /// The number of expansions of this pack.
4659 unsigned NumParameters;
4660
4661 FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
4662 SourceLocation NameLoc, unsigned NumParams,
4663 VarDecl *const *Params);
4664
4665 public:
4666 static FunctionParmPackExpr *Create(const ASTContext &Context, QualType T,
4667 VarDecl *ParamPack,
4668 SourceLocation NameLoc,
4669 ArrayRef<VarDecl *> Params);
4670 static FunctionParmPackExpr *CreateEmpty(const ASTContext &Context,
4671 unsigned NumParams);
4672
4673 /// Get the parameter pack which this expression refers to.
getParameterPack()4674 VarDecl *getParameterPack() const { return ParamPack; }
4675
4676 /// Get the location of the parameter pack.
getParameterPackLocation()4677 SourceLocation getParameterPackLocation() const { return NameLoc; }
4678
4679 /// Iterators over the parameters which the parameter pack expanded
4680 /// into.
4681 using iterator = VarDecl * const *;
begin()4682 iterator begin() const { return getTrailingObjects<VarDecl *>(); }
end()4683 iterator end() const { return begin() + NumParameters; }
4684
4685 /// Get the number of parameters in this parameter pack.
getNumExpansions()4686 unsigned getNumExpansions() const { return NumParameters; }
4687
4688 /// Get an expansion of the parameter pack by index.
getExpansion(unsigned I)4689 VarDecl *getExpansion(unsigned I) const { return begin()[I]; }
4690
getBeginLoc()4691 SourceLocation getBeginLoc() const LLVM_READONLY { return NameLoc; }
getEndLoc()4692 SourceLocation getEndLoc() const LLVM_READONLY { return NameLoc; }
4693
classof(const Stmt * T)4694 static bool classof(const Stmt *T) {
4695 return T->getStmtClass() == FunctionParmPackExprClass;
4696 }
4697
children()4698 child_range children() {
4699 return child_range(child_iterator(), child_iterator());
4700 }
4701
children()4702 const_child_range children() const {
4703 return const_child_range(const_child_iterator(), const_child_iterator());
4704 }
4705 };
4706
4707 /// Represents a prvalue temporary that is written into memory so that
4708 /// a reference can bind to it.
4709 ///
4710 /// Prvalue expressions are materialized when they need to have an address
4711 /// in memory for a reference to bind to. This happens when binding a
4712 /// reference to the result of a conversion, e.g.,
4713 ///
4714 /// \code
4715 /// const int &r = 1.0;
4716 /// \endcode
4717 ///
4718 /// Here, 1.0 is implicitly converted to an \c int. That resulting \c int is
4719 /// then materialized via a \c MaterializeTemporaryExpr, and the reference
4720 /// binds to the temporary. \c MaterializeTemporaryExprs are always glvalues
4721 /// (either an lvalue or an xvalue, depending on the kind of reference binding
4722 /// to it), maintaining the invariant that references always bind to glvalues.
4723 ///
4724 /// Reference binding and copy-elision can both extend the lifetime of a
4725 /// temporary. When either happens, the expression will also track the
4726 /// declaration which is responsible for the lifetime extension.
4727 class MaterializeTemporaryExpr : public Expr {
4728 private:
4729 friend class ASTStmtReader;
4730 friend class ASTStmtWriter;
4731
4732 llvm::PointerUnion<Stmt *, LifetimeExtendedTemporaryDecl *> State;
4733
4734 public:
4735 MaterializeTemporaryExpr(QualType T, Expr *Temporary,
4736 bool BoundToLvalueReference,
4737 LifetimeExtendedTemporaryDecl *MTD = nullptr);
4738
MaterializeTemporaryExpr(EmptyShell Empty)4739 MaterializeTemporaryExpr(EmptyShell Empty)
4740 : Expr(MaterializeTemporaryExprClass, Empty) {}
4741
4742 /// Retrieve the temporary-generating subexpression whose value will
4743 /// be materialized into a glvalue.
getSubExpr()4744 Expr *getSubExpr() const {
4745 return cast<Expr>(
4746 State.is<Stmt *>()
4747 ? State.get<Stmt *>()
4748 : State.get<LifetimeExtendedTemporaryDecl *>()->getTemporaryExpr());
4749 }
4750
4751 /// Retrieve the storage duration for the materialized temporary.
getStorageDuration()4752 StorageDuration getStorageDuration() const {
4753 return State.is<Stmt *>() ? SD_FullExpression
4754 : State.get<LifetimeExtendedTemporaryDecl *>()
4755 ->getStorageDuration();
4756 }
4757
4758 /// Get the storage for the constant value of a materialized temporary
4759 /// of static storage duration.
getOrCreateValue(bool MayCreate)4760 APValue *getOrCreateValue(bool MayCreate) const {
4761 assert(State.is<LifetimeExtendedTemporaryDecl *>() &&
4762 "the temporary has not been lifetime extended");
4763 return State.get<LifetimeExtendedTemporaryDecl *>()->getOrCreateValue(
4764 MayCreate);
4765 }
4766
getLifetimeExtendedTemporaryDecl()4767 LifetimeExtendedTemporaryDecl *getLifetimeExtendedTemporaryDecl() {
4768 return State.dyn_cast<LifetimeExtendedTemporaryDecl *>();
4769 }
4770 const LifetimeExtendedTemporaryDecl *
getLifetimeExtendedTemporaryDecl()4771 getLifetimeExtendedTemporaryDecl() const {
4772 return State.dyn_cast<LifetimeExtendedTemporaryDecl *>();
4773 }
4774
4775 /// Get the declaration which triggered the lifetime-extension of this
4776 /// temporary, if any.
getExtendingDecl()4777 ValueDecl *getExtendingDecl() {
4778 return State.is<Stmt *>() ? nullptr
4779 : State.get<LifetimeExtendedTemporaryDecl *>()
4780 ->getExtendingDecl();
4781 }
getExtendingDecl()4782 const ValueDecl *getExtendingDecl() const {
4783 return const_cast<MaterializeTemporaryExpr *>(this)->getExtendingDecl();
4784 }
4785
4786 void setExtendingDecl(ValueDecl *ExtendedBy, unsigned ManglingNumber);
4787
getManglingNumber()4788 unsigned getManglingNumber() const {
4789 return State.is<Stmt *>() ? 0
4790 : State.get<LifetimeExtendedTemporaryDecl *>()
4791 ->getManglingNumber();
4792 }
4793
4794 /// Determine whether this materialized temporary is bound to an
4795 /// lvalue reference; otherwise, it's bound to an rvalue reference.
isBoundToLvalueReference()4796 bool isBoundToLvalueReference() const { return isLValue(); }
4797
4798 /// Determine whether this temporary object is usable in constant
4799 /// expressions, as specified in C++20 [expr.const]p4.
4800 bool isUsableInConstantExpressions(const ASTContext &Context) const;
4801
getBeginLoc()4802 SourceLocation getBeginLoc() const LLVM_READONLY {
4803 return getSubExpr()->getBeginLoc();
4804 }
4805
getEndLoc()4806 SourceLocation getEndLoc() const LLVM_READONLY {
4807 return getSubExpr()->getEndLoc();
4808 }
4809
classof(const Stmt * T)4810 static bool classof(const Stmt *T) {
4811 return T->getStmtClass() == MaterializeTemporaryExprClass;
4812 }
4813
4814 // Iterators
children()4815 child_range children() {
4816 return State.is<Stmt *>()
4817 ? child_range(State.getAddrOfPtr1(), State.getAddrOfPtr1() + 1)
4818 : State.get<LifetimeExtendedTemporaryDecl *>()->childrenExpr();
4819 }
4820
children()4821 const_child_range children() const {
4822 return State.is<Stmt *>()
4823 ? const_child_range(State.getAddrOfPtr1(),
4824 State.getAddrOfPtr1() + 1)
4825 : const_cast<const LifetimeExtendedTemporaryDecl *>(
4826 State.get<LifetimeExtendedTemporaryDecl *>())
4827 ->childrenExpr();
4828 }
4829 };
4830
4831 /// Represents a folding of a pack over an operator.
4832 ///
4833 /// This expression is always dependent and represents a pack expansion of the
4834 /// forms:
4835 ///
4836 /// ( expr op ... )
4837 /// ( ... op expr )
4838 /// ( expr op ... op expr )
4839 class CXXFoldExpr : public Expr {
4840 friend class ASTStmtReader;
4841 friend class ASTStmtWriter;
4842
4843 enum SubExpr { Callee, LHS, RHS, Count };
4844
4845 SourceLocation LParenLoc;
4846 SourceLocation EllipsisLoc;
4847 SourceLocation RParenLoc;
4848 // When 0, the number of expansions is not known. Otherwise, this is one more
4849 // than the number of expansions.
4850 unsigned NumExpansions;
4851 Stmt *SubExprs[SubExpr::Count];
4852 BinaryOperatorKind Opcode;
4853
4854 public:
CXXFoldExpr(QualType T,UnresolvedLookupExpr * Callee,SourceLocation LParenLoc,Expr * LHS,BinaryOperatorKind Opcode,SourceLocation EllipsisLoc,Expr * RHS,SourceLocation RParenLoc,std::optional<unsigned> NumExpansions)4855 CXXFoldExpr(QualType T, UnresolvedLookupExpr *Callee,
4856 SourceLocation LParenLoc, Expr *LHS, BinaryOperatorKind Opcode,
4857 SourceLocation EllipsisLoc, Expr *RHS, SourceLocation RParenLoc,
4858 std::optional<unsigned> NumExpansions)
4859 : Expr(CXXFoldExprClass, T, VK_PRValue, OK_Ordinary),
4860 LParenLoc(LParenLoc), EllipsisLoc(EllipsisLoc), RParenLoc(RParenLoc),
4861 NumExpansions(NumExpansions ? *NumExpansions + 1 : 0), Opcode(Opcode) {
4862 SubExprs[SubExpr::Callee] = Callee;
4863 SubExprs[SubExpr::LHS] = LHS;
4864 SubExprs[SubExpr::RHS] = RHS;
4865 setDependence(computeDependence(this));
4866 }
4867
CXXFoldExpr(EmptyShell Empty)4868 CXXFoldExpr(EmptyShell Empty) : Expr(CXXFoldExprClass, Empty) {}
4869
getCallee()4870 UnresolvedLookupExpr *getCallee() const {
4871 return static_cast<UnresolvedLookupExpr *>(SubExprs[SubExpr::Callee]);
4872 }
getLHS()4873 Expr *getLHS() const { return static_cast<Expr*>(SubExprs[SubExpr::LHS]); }
getRHS()4874 Expr *getRHS() const { return static_cast<Expr*>(SubExprs[SubExpr::RHS]); }
4875
4876 /// Does this produce a right-associated sequence of operators?
isRightFold()4877 bool isRightFold() const {
4878 return getLHS() && getLHS()->containsUnexpandedParameterPack();
4879 }
4880
4881 /// Does this produce a left-associated sequence of operators?
isLeftFold()4882 bool isLeftFold() const { return !isRightFold(); }
4883
4884 /// Get the pattern, that is, the operand that contains an unexpanded pack.
getPattern()4885 Expr *getPattern() const { return isLeftFold() ? getRHS() : getLHS(); }
4886
4887 /// Get the operand that doesn't contain a pack, for a binary fold.
getInit()4888 Expr *getInit() const { return isLeftFold() ? getLHS() : getRHS(); }
4889
getLParenLoc()4890 SourceLocation getLParenLoc() const { return LParenLoc; }
getRParenLoc()4891 SourceLocation getRParenLoc() const { return RParenLoc; }
getEllipsisLoc()4892 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
getOperator()4893 BinaryOperatorKind getOperator() const { return Opcode; }
4894
getNumExpansions()4895 std::optional<unsigned> getNumExpansions() const {
4896 if (NumExpansions)
4897 return NumExpansions - 1;
4898 return std::nullopt;
4899 }
4900
getBeginLoc()4901 SourceLocation getBeginLoc() const LLVM_READONLY {
4902 if (LParenLoc.isValid())
4903 return LParenLoc;
4904 if (isLeftFold())
4905 return getEllipsisLoc();
4906 return getLHS()->getBeginLoc();
4907 }
4908
getEndLoc()4909 SourceLocation getEndLoc() const LLVM_READONLY {
4910 if (RParenLoc.isValid())
4911 return RParenLoc;
4912 if (isRightFold())
4913 return getEllipsisLoc();
4914 return getRHS()->getEndLoc();
4915 }
4916
classof(const Stmt * T)4917 static bool classof(const Stmt *T) {
4918 return T->getStmtClass() == CXXFoldExprClass;
4919 }
4920
4921 // Iterators
children()4922 child_range children() {
4923 return child_range(SubExprs, SubExprs + SubExpr::Count);
4924 }
4925
children()4926 const_child_range children() const {
4927 return const_child_range(SubExprs, SubExprs + SubExpr::Count);
4928 }
4929 };
4930
4931 /// Represents a list-initialization with parenthesis.
4932 ///
4933 /// As per P0960R3, this is a C++20 feature that allows aggregate to
4934 /// be initialized with a parenthesized list of values:
4935 /// ```
4936 /// struct A {
4937 /// int a;
4938 /// double b;
4939 /// };
4940 ///
4941 /// void foo() {
4942 /// A a1(0); // Well-formed in C++20
4943 /// A a2(1.5, 1.0); // Well-formed in C++20
4944 /// }
4945 /// ```
4946 /// It has some sort of similiarity to braced
4947 /// list-initialization, with some differences such as
4948 /// it allows narrowing conversion whilst braced
4949 /// list-initialization doesn't.
4950 /// ```
4951 /// struct A {
4952 /// char a;
4953 /// };
4954 /// void foo() {
4955 /// A a(1.5); // Well-formed in C++20
4956 /// A b{1.5}; // Ill-formed !
4957 /// }
4958 /// ```
4959 class CXXParenListInitExpr final
4960 : public Expr,
4961 private llvm::TrailingObjects<CXXParenListInitExpr, Expr *> {
4962 friend class TrailingObjects;
4963 friend class ASTStmtReader;
4964 friend class ASTStmtWriter;
4965
4966 unsigned NumExprs;
4967 unsigned NumUserSpecifiedExprs;
4968 SourceLocation InitLoc, LParenLoc, RParenLoc;
4969 llvm::PointerUnion<Expr *, FieldDecl *> ArrayFillerOrUnionFieldInit;
4970
CXXParenListInitExpr(ArrayRef<Expr * > Args,QualType T,unsigned NumUserSpecifiedExprs,SourceLocation InitLoc,SourceLocation LParenLoc,SourceLocation RParenLoc)4971 CXXParenListInitExpr(ArrayRef<Expr *> Args, QualType T,
4972 unsigned NumUserSpecifiedExprs, SourceLocation InitLoc,
4973 SourceLocation LParenLoc, SourceLocation RParenLoc)
4974 : Expr(CXXParenListInitExprClass, T, getValueKindForType(T), OK_Ordinary),
4975 NumExprs(Args.size()), NumUserSpecifiedExprs(NumUserSpecifiedExprs),
4976 InitLoc(InitLoc), LParenLoc(LParenLoc), RParenLoc(RParenLoc) {
4977 std::copy(Args.begin(), Args.end(), getTrailingObjects<Expr *>());
4978 assert(NumExprs >= NumUserSpecifiedExprs &&
4979 "number of user specified inits is greater than the number of "
4980 "passed inits");
4981 setDependence(computeDependence(this));
4982 }
4983
numTrailingObjects(OverloadToken<Expr * >)4984 size_t numTrailingObjects(OverloadToken<Expr *>) const { return NumExprs; }
4985
4986 public:
4987 static CXXParenListInitExpr *
4988 Create(ASTContext &C, ArrayRef<Expr *> Args, QualType T,
4989 unsigned NumUserSpecifiedExprs, SourceLocation InitLoc,
4990 SourceLocation LParenLoc, SourceLocation RParenLoc);
4991
4992 static CXXParenListInitExpr *CreateEmpty(ASTContext &C, unsigned numExprs,
4993 EmptyShell Empty);
4994
CXXParenListInitExpr(EmptyShell Empty,unsigned NumExprs)4995 explicit CXXParenListInitExpr(EmptyShell Empty, unsigned NumExprs)
4996 : Expr(CXXParenListInitExprClass, Empty), NumExprs(NumExprs),
4997 NumUserSpecifiedExprs(0) {}
4998
updateDependence()4999 void updateDependence() { setDependence(computeDependence(this)); }
5000
getInitExprs()5001 ArrayRef<Expr *> getInitExprs() {
5002 return ArrayRef(getTrailingObjects<Expr *>(), NumExprs);
5003 }
5004
getInitExprs()5005 const ArrayRef<Expr *> getInitExprs() const {
5006 return ArrayRef(getTrailingObjects<Expr *>(), NumExprs);
5007 }
5008
getUserSpecifiedInitExprs()5009 ArrayRef<Expr *> getUserSpecifiedInitExprs() {
5010 return ArrayRef(getTrailingObjects<Expr *>(), NumUserSpecifiedExprs);
5011 }
5012
getUserSpecifiedInitExprs()5013 const ArrayRef<Expr *> getUserSpecifiedInitExprs() const {
5014 return ArrayRef(getTrailingObjects<Expr *>(), NumUserSpecifiedExprs);
5015 }
5016
getBeginLoc()5017 SourceLocation getBeginLoc() const LLVM_READONLY { return LParenLoc; }
5018
getEndLoc()5019 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
5020
getInitLoc()5021 SourceLocation getInitLoc() const LLVM_READONLY { return InitLoc; }
5022
getSourceRange()5023 SourceRange getSourceRange() const LLVM_READONLY {
5024 return SourceRange(getBeginLoc(), getEndLoc());
5025 }
5026
setArrayFiller(Expr * E)5027 void setArrayFiller(Expr *E) { ArrayFillerOrUnionFieldInit = E; }
5028
getArrayFiller()5029 Expr *getArrayFiller() {
5030 return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
5031 }
5032
getArrayFiller()5033 const Expr *getArrayFiller() const {
5034 return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
5035 }
5036
setInitializedFieldInUnion(FieldDecl * FD)5037 void setInitializedFieldInUnion(FieldDecl *FD) {
5038 ArrayFillerOrUnionFieldInit = FD;
5039 }
5040
getInitializedFieldInUnion()5041 FieldDecl *getInitializedFieldInUnion() {
5042 return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>();
5043 }
5044
getInitializedFieldInUnion()5045 const FieldDecl *getInitializedFieldInUnion() const {
5046 return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>();
5047 }
5048
children()5049 child_range children() {
5050 Stmt **Begin = reinterpret_cast<Stmt **>(getTrailingObjects<Expr *>());
5051 return child_range(Begin, Begin + NumExprs);
5052 }
5053
children()5054 const_child_range children() const {
5055 Stmt *const *Begin =
5056 reinterpret_cast<Stmt *const *>(getTrailingObjects<Expr *>());
5057 return const_child_range(Begin, Begin + NumExprs);
5058 }
5059
classof(const Stmt * T)5060 static bool classof(const Stmt *T) {
5061 return T->getStmtClass() == CXXParenListInitExprClass;
5062 }
5063 };
5064
5065 /// Represents an expression that might suspend coroutine execution;
5066 /// either a co_await or co_yield expression.
5067 ///
5068 /// Evaluation of this expression first evaluates its 'ready' expression. If
5069 /// that returns 'false':
5070 /// -- execution of the coroutine is suspended
5071 /// -- the 'suspend' expression is evaluated
5072 /// -- if the 'suspend' expression returns 'false', the coroutine is
5073 /// resumed
5074 /// -- otherwise, control passes back to the resumer.
5075 /// If the coroutine is not suspended, or when it is resumed, the 'resume'
5076 /// expression is evaluated, and its result is the result of the overall
5077 /// expression.
5078 class CoroutineSuspendExpr : public Expr {
5079 friend class ASTStmtReader;
5080
5081 SourceLocation KeywordLoc;
5082
5083 enum SubExpr { Operand, Common, Ready, Suspend, Resume, Count };
5084
5085 Stmt *SubExprs[SubExpr::Count];
5086 OpaqueValueExpr *OpaqueValue = nullptr;
5087
5088 public:
5089 // These types correspond to the three C++ 'await_suspend' return variants
5090 enum class SuspendReturnType { SuspendVoid, SuspendBool, SuspendHandle };
5091
CoroutineSuspendExpr(StmtClass SC,SourceLocation KeywordLoc,Expr * Operand,Expr * Common,Expr * Ready,Expr * Suspend,Expr * Resume,OpaqueValueExpr * OpaqueValue)5092 CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, Expr *Operand,
5093 Expr *Common, Expr *Ready, Expr *Suspend, Expr *Resume,
5094 OpaqueValueExpr *OpaqueValue)
5095 : Expr(SC, Resume->getType(), Resume->getValueKind(),
5096 Resume->getObjectKind()),
5097 KeywordLoc(KeywordLoc), OpaqueValue(OpaqueValue) {
5098 SubExprs[SubExpr::Operand] = Operand;
5099 SubExprs[SubExpr::Common] = Common;
5100 SubExprs[SubExpr::Ready] = Ready;
5101 SubExprs[SubExpr::Suspend] = Suspend;
5102 SubExprs[SubExpr::Resume] = Resume;
5103 setDependence(computeDependence(this));
5104 }
5105
CoroutineSuspendExpr(StmtClass SC,SourceLocation KeywordLoc,QualType Ty,Expr * Operand,Expr * Common)5106 CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, QualType Ty,
5107 Expr *Operand, Expr *Common)
5108 : Expr(SC, Ty, VK_PRValue, OK_Ordinary), KeywordLoc(KeywordLoc) {
5109 assert(Common->isTypeDependent() && Ty->isDependentType() &&
5110 "wrong constructor for non-dependent co_await/co_yield expression");
5111 SubExprs[SubExpr::Operand] = Operand;
5112 SubExprs[SubExpr::Common] = Common;
5113 SubExprs[SubExpr::Ready] = nullptr;
5114 SubExprs[SubExpr::Suspend] = nullptr;
5115 SubExprs[SubExpr::Resume] = nullptr;
5116 setDependence(computeDependence(this));
5117 }
5118
CoroutineSuspendExpr(StmtClass SC,EmptyShell Empty)5119 CoroutineSuspendExpr(StmtClass SC, EmptyShell Empty) : Expr(SC, Empty) {
5120 SubExprs[SubExpr::Operand] = nullptr;
5121 SubExprs[SubExpr::Common] = nullptr;
5122 SubExprs[SubExpr::Ready] = nullptr;
5123 SubExprs[SubExpr::Suspend] = nullptr;
5124 SubExprs[SubExpr::Resume] = nullptr;
5125 }
5126
getCommonExpr()5127 Expr *getCommonExpr() const {
5128 return static_cast<Expr*>(SubExprs[SubExpr::Common]);
5129 }
5130
5131 /// getOpaqueValue - Return the opaque value placeholder.
getOpaqueValue()5132 OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; }
5133
getReadyExpr()5134 Expr *getReadyExpr() const {
5135 return static_cast<Expr*>(SubExprs[SubExpr::Ready]);
5136 }
5137
getSuspendExpr()5138 Expr *getSuspendExpr() const {
5139 return static_cast<Expr*>(SubExprs[SubExpr::Suspend]);
5140 }
5141
getResumeExpr()5142 Expr *getResumeExpr() const {
5143 return static_cast<Expr*>(SubExprs[SubExpr::Resume]);
5144 }
5145
5146 // The syntactic operand written in the code
getOperand()5147 Expr *getOperand() const {
5148 return static_cast<Expr *>(SubExprs[SubExpr::Operand]);
5149 }
5150
getSuspendReturnType()5151 SuspendReturnType getSuspendReturnType() const {
5152 auto *SuspendExpr = getSuspendExpr();
5153 assert(SuspendExpr);
5154
5155 auto SuspendType = SuspendExpr->getType();
5156
5157 if (SuspendType->isVoidType())
5158 return SuspendReturnType::SuspendVoid;
5159 if (SuspendType->isBooleanType())
5160 return SuspendReturnType::SuspendBool;
5161
5162 // Void pointer is the type of handle.address(), which is returned
5163 // from the await suspend wrapper so that the temporary coroutine handle
5164 // value won't go to the frame by mistake
5165 assert(SuspendType->isVoidPointerType());
5166 return SuspendReturnType::SuspendHandle;
5167 }
5168
getKeywordLoc()5169 SourceLocation getKeywordLoc() const { return KeywordLoc; }
5170
getBeginLoc()5171 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
5172
getEndLoc()5173 SourceLocation getEndLoc() const LLVM_READONLY {
5174 return getOperand()->getEndLoc();
5175 }
5176
children()5177 child_range children() {
5178 return child_range(SubExprs, SubExprs + SubExpr::Count);
5179 }
5180
children()5181 const_child_range children() const {
5182 return const_child_range(SubExprs, SubExprs + SubExpr::Count);
5183 }
5184
classof(const Stmt * T)5185 static bool classof(const Stmt *T) {
5186 return T->getStmtClass() == CoawaitExprClass ||
5187 T->getStmtClass() == CoyieldExprClass;
5188 }
5189 };
5190
5191 /// Represents a 'co_await' expression.
5192 class CoawaitExpr : public CoroutineSuspendExpr {
5193 friend class ASTStmtReader;
5194
5195 public:
5196 CoawaitExpr(SourceLocation CoawaitLoc, Expr *Operand, Expr *Common,
5197 Expr *Ready, Expr *Suspend, Expr *Resume,
5198 OpaqueValueExpr *OpaqueValue, bool IsImplicit = false)
CoroutineSuspendExpr(CoawaitExprClass,CoawaitLoc,Operand,Common,Ready,Suspend,Resume,OpaqueValue)5199 : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Operand, Common,
5200 Ready, Suspend, Resume, OpaqueValue) {
5201 CoawaitBits.IsImplicit = IsImplicit;
5202 }
5203
5204 CoawaitExpr(SourceLocation CoawaitLoc, QualType Ty, Expr *Operand,
5205 Expr *Common, bool IsImplicit = false)
CoroutineSuspendExpr(CoawaitExprClass,CoawaitLoc,Ty,Operand,Common)5206 : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Ty, Operand,
5207 Common) {
5208 CoawaitBits.IsImplicit = IsImplicit;
5209 }
5210
CoawaitExpr(EmptyShell Empty)5211 CoawaitExpr(EmptyShell Empty)
5212 : CoroutineSuspendExpr(CoawaitExprClass, Empty) {}
5213
isImplicit()5214 bool isImplicit() const { return CoawaitBits.IsImplicit; }
5215 void setIsImplicit(bool value = true) { CoawaitBits.IsImplicit = value; }
5216
classof(const Stmt * T)5217 static bool classof(const Stmt *T) {
5218 return T->getStmtClass() == CoawaitExprClass;
5219 }
5220 };
5221
5222 /// Represents a 'co_await' expression while the type of the promise
5223 /// is dependent.
5224 class DependentCoawaitExpr : public Expr {
5225 friend class ASTStmtReader;
5226
5227 SourceLocation KeywordLoc;
5228 Stmt *SubExprs[2];
5229
5230 public:
DependentCoawaitExpr(SourceLocation KeywordLoc,QualType Ty,Expr * Op,UnresolvedLookupExpr * OpCoawait)5231 DependentCoawaitExpr(SourceLocation KeywordLoc, QualType Ty, Expr *Op,
5232 UnresolvedLookupExpr *OpCoawait)
5233 : Expr(DependentCoawaitExprClass, Ty, VK_PRValue, OK_Ordinary),
5234 KeywordLoc(KeywordLoc) {
5235 // NOTE: A co_await expression is dependent on the coroutines promise
5236 // type and may be dependent even when the `Op` expression is not.
5237 assert(Ty->isDependentType() &&
5238 "wrong constructor for non-dependent co_await/co_yield expression");
5239 SubExprs[0] = Op;
5240 SubExprs[1] = OpCoawait;
5241 setDependence(computeDependence(this));
5242 }
5243
DependentCoawaitExpr(EmptyShell Empty)5244 DependentCoawaitExpr(EmptyShell Empty)
5245 : Expr(DependentCoawaitExprClass, Empty) {}
5246
getOperand()5247 Expr *getOperand() const { return cast<Expr>(SubExprs[0]); }
5248
getOperatorCoawaitLookup()5249 UnresolvedLookupExpr *getOperatorCoawaitLookup() const {
5250 return cast<UnresolvedLookupExpr>(SubExprs[1]);
5251 }
5252
getKeywordLoc()5253 SourceLocation getKeywordLoc() const { return KeywordLoc; }
5254
getBeginLoc()5255 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
5256
getEndLoc()5257 SourceLocation getEndLoc() const LLVM_READONLY {
5258 return getOperand()->getEndLoc();
5259 }
5260
children()5261 child_range children() { return child_range(SubExprs, SubExprs + 2); }
5262
children()5263 const_child_range children() const {
5264 return const_child_range(SubExprs, SubExprs + 2);
5265 }
5266
classof(const Stmt * T)5267 static bool classof(const Stmt *T) {
5268 return T->getStmtClass() == DependentCoawaitExprClass;
5269 }
5270 };
5271
5272 /// Represents a 'co_yield' expression.
5273 class CoyieldExpr : public CoroutineSuspendExpr {
5274 friend class ASTStmtReader;
5275
5276 public:
CoyieldExpr(SourceLocation CoyieldLoc,Expr * Operand,Expr * Common,Expr * Ready,Expr * Suspend,Expr * Resume,OpaqueValueExpr * OpaqueValue)5277 CoyieldExpr(SourceLocation CoyieldLoc, Expr *Operand, Expr *Common,
5278 Expr *Ready, Expr *Suspend, Expr *Resume,
5279 OpaqueValueExpr *OpaqueValue)
5280 : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Operand, Common,
5281 Ready, Suspend, Resume, OpaqueValue) {}
CoyieldExpr(SourceLocation CoyieldLoc,QualType Ty,Expr * Operand,Expr * Common)5282 CoyieldExpr(SourceLocation CoyieldLoc, QualType Ty, Expr *Operand,
5283 Expr *Common)
5284 : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Ty, Operand,
5285 Common) {}
CoyieldExpr(EmptyShell Empty)5286 CoyieldExpr(EmptyShell Empty)
5287 : CoroutineSuspendExpr(CoyieldExprClass, Empty) {}
5288
classof(const Stmt * T)5289 static bool classof(const Stmt *T) {
5290 return T->getStmtClass() == CoyieldExprClass;
5291 }
5292 };
5293
5294 /// Represents a C++2a __builtin_bit_cast(T, v) expression. Used to implement
5295 /// std::bit_cast. These can sometimes be evaluated as part of a constant
5296 /// expression, but otherwise CodeGen to a simple memcpy in general.
5297 class BuiltinBitCastExpr final
5298 : public ExplicitCastExpr,
5299 private llvm::TrailingObjects<BuiltinBitCastExpr, CXXBaseSpecifier *> {
5300 friend class ASTStmtReader;
5301 friend class CastExpr;
5302 friend TrailingObjects;
5303
5304 SourceLocation KWLoc;
5305 SourceLocation RParenLoc;
5306
5307 public:
BuiltinBitCastExpr(QualType T,ExprValueKind VK,CastKind CK,Expr * SrcExpr,TypeSourceInfo * DstType,SourceLocation KWLoc,SourceLocation RParenLoc)5308 BuiltinBitCastExpr(QualType T, ExprValueKind VK, CastKind CK, Expr *SrcExpr,
5309 TypeSourceInfo *DstType, SourceLocation KWLoc,
5310 SourceLocation RParenLoc)
5311 : ExplicitCastExpr(BuiltinBitCastExprClass, T, VK, CK, SrcExpr, 0, false,
5312 DstType),
5313 KWLoc(KWLoc), RParenLoc(RParenLoc) {}
BuiltinBitCastExpr(EmptyShell Empty)5314 BuiltinBitCastExpr(EmptyShell Empty)
5315 : ExplicitCastExpr(BuiltinBitCastExprClass, Empty, 0, false) {}
5316
getBeginLoc()5317 SourceLocation getBeginLoc() const LLVM_READONLY { return KWLoc; }
getEndLoc()5318 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
5319
classof(const Stmt * T)5320 static bool classof(const Stmt *T) {
5321 return T->getStmtClass() == BuiltinBitCastExprClass;
5322 }
5323 };
5324
5325 } // namespace clang
5326
5327 #endif // LLVM_CLANG_AST_EXPRCXX_H
5328