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/Decl.h"
18#include "clang/AST/DeclBase.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclarationName.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/OperationKinds.h"
24#include "clang/AST/Stmt.h"
25#include "clang/AST/TemplateBase.h"
26#include "clang/AST/Type.h"
27#include "clang/AST/UnresolvedSet.h"
28#include "clang/Basic/ExceptionSpecificationType.h"
29#include "clang/Basic/ExpressionTraits.h"
30#include "clang/Basic/LLVM.h"
31#include "clang/Basic/Lambda.h"
32#include "clang/Basic/LangOptions.h"
33#include "clang/Basic/OperatorKinds.h"
34#include "clang/Basic/SourceLocation.h"
35#include "clang/Basic/Specifiers.h"
36#include "clang/Basic/TypeTraits.h"
37#include "llvm/ADT/ArrayRef.h"
38#include "llvm/ADT/None.h"
39#include "llvm/ADT/Optional.h"
40#include "llvm/ADT/PointerUnion.h"
41#include "llvm/ADT/StringRef.h"
42#include "llvm/ADT/iterator_range.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/Compiler.h"
45#include "llvm/Support/TrailingObjects.h"
46#include <cassert>
47#include <cstddef>
48#include <cstdint>
49#include <memory>
50
51namespace clang {
52
53class ASTContext;
54class DeclAccessPair;
55class IdentifierInfo;
56class LambdaCapture;
57class NonTypeTemplateParmDecl;
58class TemplateParameterList;
59
60//===--------------------------------------------------------------------===//
61// C++ Expressions.
62//===--------------------------------------------------------------------===//
63
64/// A call to an overloaded operator written using operator
65/// syntax.
66///
67/// Represents a call to an overloaded operator written using operator
68/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a
69/// normal call, this AST node provides better information about the
70/// syntactic representation of the call.
71///
72/// In a C++ template, this expression node kind will be used whenever
73/// any of the arguments are type-dependent. In this case, the
74/// function itself will be a (possibly empty) set of functions and
75/// function templates that were found by name lookup at template
76/// definition time.
77class CXXOperatorCallExpr final : public CallExpr {
78 friend class ASTStmtReader;
79 friend class ASTStmtWriter;
80
81 SourceRange Range;
82
83 // CXXOperatorCallExpr has some trailing objects belonging
84 // to CallExpr. See CallExpr for the details.
85
86 SourceRange getSourceRangeImpl() const LLVM_READONLY;
87
88 CXXOperatorCallExpr(OverloadedOperatorKind OpKind, Expr *Fn,
89 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
90 SourceLocation OperatorLoc, FPOptions FPFeatures,
91 ADLCallKind UsesADL);
92
93 CXXOperatorCallExpr(unsigned NumArgs, EmptyShell Empty);
94
95public:
96 static CXXOperatorCallExpr *
97 Create(const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn,
98 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
99 SourceLocation OperatorLoc, FPOptions FPFeatures,
100 ADLCallKind UsesADL = NotADL);
101
102 static CXXOperatorCallExpr *CreateEmpty(const ASTContext &Ctx,
103 unsigned NumArgs, EmptyShell Empty);
104
105 /// Returns the kind of overloaded operator that this expression refers to.
106 OverloadedOperatorKind getOperator() const {
107 return static_cast<OverloadedOperatorKind>(
108 CXXOperatorCallExprBits.OperatorKind);
109 }
110
111 static bool isAssignmentOp(OverloadedOperatorKind Opc) {
112 return Opc == OO_Equal || Opc == OO_StarEqual || Opc == OO_SlashEqual ||
113 Opc == OO_PercentEqual || Opc == OO_PlusEqual ||
114 Opc == OO_MinusEqual || Opc == OO_LessLessEqual ||
115 Opc == OO_GreaterGreaterEqual || Opc == OO_AmpEqual ||
116 Opc == OO_CaretEqual || Opc == OO_PipeEqual;
117 }
118 bool isAssignmentOp() const { return isAssignmentOp(getOperator()); }
119
120 /// Is this written as an infix binary operator?
121 bool isInfixBinaryOp() const;
122
123 /// Returns the location of the operator symbol in the expression.
124 ///
125 /// When \c getOperator()==OO_Call, this is the location of the right
126 /// parentheses; when \c getOperator()==OO_Subscript, this is the location
127 /// of the right bracket.
128 SourceLocation getOperatorLoc() const { return getRParenLoc(); }
129
130 SourceLocation getExprLoc() const LLVM_READONLY {
131 OverloadedOperatorKind Operator = getOperator();
132 return (Operator < OO_Plus || Operator >= OO_Arrow ||
133 Operator == OO_PlusPlus || Operator == OO_MinusMinus)
134 ? getBeginLoc()
135 : getOperatorLoc();
136 }
137
138 SourceLocation getBeginLoc() const { return Range.getBegin(); }
139 SourceLocation getEndLoc() const { return Range.getEnd(); }
140 SourceRange getSourceRange() const { return Range; }
141
142 static bool classof(const Stmt *T) {
143 return T->getStmtClass() == CXXOperatorCallExprClass;
144 }
145
146 // Set the FP contractability status of this operator. Only meaningful for
147 // operations on floating point types.
148 void setFPFeatures(FPOptions F) {
149 CXXOperatorCallExprBits.FPFeatures = F.getInt();
150 }
151 FPOptions getFPFeatures() const {
152 return FPOptions(CXXOperatorCallExprBits.FPFeatures);
153 }
154
155 // Get the FP contractability status of this operator. Only meaningful for
156 // operations on floating point types.
157 bool isFPContractableWithinStatement() const {
158 return getFPFeatures().allowFPContractWithinStatement();
159 }
160};
161
162/// Represents a call to a member function that
163/// may be written either with member call syntax (e.g., "obj.func()"
164/// or "objptr->func()") or with normal function-call syntax
165/// ("func()") within a member function that ends up calling a member
166/// function. The callee in either case is a MemberExpr that contains
167/// both the object argument and the member function, while the
168/// arguments are the arguments within the parentheses (not including
169/// the object argument).
170class CXXMemberCallExpr final : public CallExpr {
171 // CXXMemberCallExpr has some trailing objects belonging
172 // to CallExpr. See CallExpr for the details.
173
174 CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args, QualType Ty,
175 ExprValueKind VK, SourceLocation RP, unsigned MinNumArgs);
176
177 CXXMemberCallExpr(unsigned NumArgs, EmptyShell Empty);
178
179public:
180 static CXXMemberCallExpr *Create(const ASTContext &Ctx, Expr *Fn,
181 ArrayRef<Expr *> Args, QualType Ty,
182 ExprValueKind VK, SourceLocation RP,
183 unsigned MinNumArgs = 0);
184
185 static CXXMemberCallExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs,
186 EmptyShell Empty);
187
188 /// Retrieves the implicit object argument for the member call.
189 ///
190 /// For example, in "x.f(5)", this returns the sub-expression "x".
191 Expr *getImplicitObjectArgument() const;
192
193 /// Retrieves the declaration of the called method.
194 CXXMethodDecl *getMethodDecl() const;
195
196 /// Retrieves the CXXRecordDecl for the underlying type of
197 /// the implicit object argument.
198 ///
199 /// Note that this is may not be the same declaration as that of the class
200 /// context of the CXXMethodDecl which this function is calling.
201 /// FIXME: Returns 0 for member pointer call exprs.
202 CXXRecordDecl *getRecordDecl() const;
203
204 SourceLocation getExprLoc() const LLVM_READONLY {
205 SourceLocation CLoc = getCallee()->getExprLoc();
206 if (CLoc.isValid())
207 return CLoc;
208
209 return getBeginLoc();
210 }
211
212 static bool classof(const Stmt *T) {
213 return T->getStmtClass() == CXXMemberCallExprClass;
214 }
215};
216
217/// Represents a call to a CUDA kernel function.
218class CUDAKernelCallExpr final : public CallExpr {
219 enum { CONFIG, END_PREARG };
220
221 // CUDAKernelCallExpr has some trailing objects belonging
222 // to CallExpr. See CallExpr for the details.
223
224 CUDAKernelCallExpr(Expr *Fn, CallExpr *Config, ArrayRef<Expr *> Args,
225 QualType Ty, ExprValueKind VK, SourceLocation RP,
226 unsigned MinNumArgs);
227
228 CUDAKernelCallExpr(unsigned NumArgs, EmptyShell Empty);
229
230public:
231 static CUDAKernelCallExpr *Create(const ASTContext &Ctx, Expr *Fn,
232 CallExpr *Config, ArrayRef<Expr *> Args,
233 QualType Ty, ExprValueKind VK,
234 SourceLocation RP, unsigned MinNumArgs = 0);
235
236 static CUDAKernelCallExpr *CreateEmpty(const ASTContext &Ctx,
237 unsigned NumArgs, EmptyShell Empty);
238
239 const CallExpr *getConfig() const {
240 return cast_or_null<CallExpr>(getPreArg(CONFIG));
241 }
242 CallExpr *getConfig() { return cast_or_null<CallExpr>(getPreArg(CONFIG)); }
243
244 /// Sets the kernel configuration expression.
245 ///
246 /// Note that this method cannot be called if config has already been set to a
247 /// non-null value.
248 void setConfig(CallExpr *E) {
249 assert(!getConfig() &&
250 "Cannot call setConfig if config is not null");
251 setPreArg(CONFIG, E);
252 setInstantiationDependent(isInstantiationDependent() ||
253 E->isInstantiationDependent());
254 setContainsUnexpandedParameterPack(containsUnexpandedParameterPack() ||
255 E->containsUnexpandedParameterPack());
256 }
257
258 static bool classof(const Stmt *T) {
259 return T->getStmtClass() == CUDAKernelCallExprClass;
260 }
261};
262
263/// Abstract class common to all of the C++ "named"/"keyword" casts.
264///
265/// This abstract class is inherited by all of the classes
266/// representing "named" casts: CXXStaticCastExpr for \c static_cast,
267/// CXXDynamicCastExpr for \c dynamic_cast, CXXReinterpretCastExpr for
268/// reinterpret_cast, and CXXConstCastExpr for \c const_cast.
269class CXXNamedCastExpr : public ExplicitCastExpr {
270private:
271 // the location of the casting op
272 SourceLocation Loc;
273
274 // the location of the right parenthesis
275 SourceLocation RParenLoc;
276
277 // range for '<' '>'
278 SourceRange AngleBrackets;
279
280protected:
281 friend class ASTStmtReader;
282
283 CXXNamedCastExpr(StmtClass SC, QualType ty, ExprValueKind VK,
284 CastKind kind, Expr *op, unsigned PathSize,
285 TypeSourceInfo *writtenTy, SourceLocation l,
286 SourceLocation RParenLoc,
287 SourceRange AngleBrackets)
288 : ExplicitCastExpr(SC, ty, VK, kind, op, PathSize, writtenTy), Loc(l),
289 RParenLoc(RParenLoc), AngleBrackets(AngleBrackets) {}
290
291 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize)
292 : ExplicitCastExpr(SC, Shell, PathSize) {}
293
294public:
295 const char *getCastName() const;
296
297 /// Retrieve the location of the cast operator keyword, e.g.,
298 /// \c static_cast.
299 SourceLocation getOperatorLoc() const { return Loc; }
300
301 /// Retrieve the location of the closing parenthesis.
302 SourceLocation getRParenLoc() const { return RParenLoc; }
303
304 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
305 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
306 SourceRange getAngleBrackets() const LLVM_READONLY { return AngleBrackets; }
307
308 static bool classof(const Stmt *T) {
309 switch (T->getStmtClass()) {
310 case CXXStaticCastExprClass:
311 case CXXDynamicCastExprClass:
312 case CXXReinterpretCastExprClass:
313 case CXXConstCastExprClass:
314 return true;
315 default:
316 return false;
317 }
318 }
319};
320
321/// A C++ \c static_cast expression (C++ [expr.static.cast]).
322///
323/// This expression node represents a C++ static cast, e.g.,
324/// \c static_cast<int>(1.0).
325class CXXStaticCastExpr final
326 : public CXXNamedCastExpr,
327 private llvm::TrailingObjects<CXXStaticCastExpr, CXXBaseSpecifier *> {
328 CXXStaticCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op,
329 unsigned pathSize, TypeSourceInfo *writtenTy,
330 SourceLocation l, SourceLocation RParenLoc,
331 SourceRange AngleBrackets)
332 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize,
333 writtenTy, l, RParenLoc, AngleBrackets) {}
334
335 explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize)
336 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize) {}
337
338public:
339 friend class CastExpr;
340 friend TrailingObjects;
341
342 static CXXStaticCastExpr *Create(const ASTContext &Context, QualType T,
343 ExprValueKind VK, CastKind K, Expr *Op,
344 const CXXCastPath *Path,
345 TypeSourceInfo *Written, SourceLocation L,
346 SourceLocation RParenLoc,
347 SourceRange AngleBrackets);
348 static CXXStaticCastExpr *CreateEmpty(const ASTContext &Context,
349 unsigned PathSize);
350
351 static bool classof(const Stmt *T) {
352 return T->getStmtClass() == CXXStaticCastExprClass;
353 }
354};
355
356/// A C++ @c dynamic_cast expression (C++ [expr.dynamic.cast]).
357///
358/// This expression node represents a dynamic cast, e.g.,
359/// \c dynamic_cast<Derived*>(BasePtr). Such a cast may perform a run-time
360/// check to determine how to perform the type conversion.
361class CXXDynamicCastExpr final
362 : public CXXNamedCastExpr,
363 private llvm::TrailingObjects<CXXDynamicCastExpr, CXXBaseSpecifier *> {
364 CXXDynamicCastExpr(QualType ty, ExprValueKind VK, CastKind kind,
365 Expr *op, unsigned pathSize, TypeSourceInfo *writtenTy,
366 SourceLocation l, SourceLocation RParenLoc,
367 SourceRange AngleBrackets)
368 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, VK, kind, op, pathSize,
369 writtenTy, l, RParenLoc, AngleBrackets) {}
370
371 explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize)
372 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize) {}
373
374public:
375 friend class CastExpr;
376 friend TrailingObjects;
377
378 static CXXDynamicCastExpr *Create(const ASTContext &Context, QualType T,
379 ExprValueKind VK, CastKind Kind, Expr *Op,
380 const CXXCastPath *Path,
381 TypeSourceInfo *Written, SourceLocation L,
382 SourceLocation RParenLoc,
383 SourceRange AngleBrackets);
384
385 static CXXDynamicCastExpr *CreateEmpty(const ASTContext &Context,
386 unsigned pathSize);
387
388 bool isAlwaysNull() const;
389
390 static bool classof(const Stmt *T) {
391 return T->getStmtClass() == CXXDynamicCastExprClass;
392 }
393};
394
395/// A C++ @c reinterpret_cast expression (C++ [expr.reinterpret.cast]).
396///
397/// This expression node represents a reinterpret cast, e.g.,
398/// @c reinterpret_cast<int>(VoidPtr).
399///
400/// A reinterpret_cast provides a differently-typed view of a value but
401/// (in Clang, as in most C++ implementations) performs no actual work at
402/// run time.
403class CXXReinterpretCastExpr final
404 : public CXXNamedCastExpr,
405 private llvm::TrailingObjects<CXXReinterpretCastExpr,
406 CXXBaseSpecifier *> {
407 CXXReinterpretCastExpr(QualType ty, ExprValueKind vk, CastKind kind,
408 Expr *op, unsigned pathSize,
409 TypeSourceInfo *writtenTy, SourceLocation l,
410 SourceLocation RParenLoc,
411 SourceRange AngleBrackets)
412 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, vk, kind, op,
413 pathSize, writtenTy, l, RParenLoc, AngleBrackets) {}
414
415 CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize)
416 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize) {}
417
418public:
419 friend class CastExpr;
420 friend TrailingObjects;
421
422 static CXXReinterpretCastExpr *Create(const ASTContext &Context, QualType T,
423 ExprValueKind VK, CastKind Kind,
424 Expr *Op, const CXXCastPath *Path,
425 TypeSourceInfo *WrittenTy, SourceLocation L,
426 SourceLocation RParenLoc,
427 SourceRange AngleBrackets);
428 static CXXReinterpretCastExpr *CreateEmpty(const ASTContext &Context,
429 unsigned pathSize);
430
431 static bool classof(const Stmt *T) {
432 return T->getStmtClass() == CXXReinterpretCastExprClass;
433 }
434};
435
436/// A C++ \c const_cast expression (C++ [expr.const.cast]).
437///
438/// This expression node represents a const cast, e.g.,
439/// \c const_cast<char*>(PtrToConstChar).
440///
441/// A const_cast can remove type qualifiers but does not change the underlying
442/// value.
443class CXXConstCastExpr final
444 : public CXXNamedCastExpr,
445 private llvm::TrailingObjects<CXXConstCastExpr, CXXBaseSpecifier *> {
446 CXXConstCastExpr(QualType ty, ExprValueKind VK, Expr *op,
447 TypeSourceInfo *writtenTy, SourceLocation l,
448 SourceLocation RParenLoc, SourceRange AngleBrackets)
449 : CXXNamedCastExpr(CXXConstCastExprClass, ty, VK, CK_NoOp, op,
450 0, writtenTy, l, RParenLoc, AngleBrackets) {}
451
452 explicit CXXConstCastExpr(EmptyShell Empty)
453 : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0) {}
454
455public:
456 friend class CastExpr;
457 friend TrailingObjects;
458
459 static CXXConstCastExpr *Create(const ASTContext &Context, QualType T,
460 ExprValueKind VK, Expr *Op,
461 TypeSourceInfo *WrittenTy, SourceLocation L,
462 SourceLocation RParenLoc,
463 SourceRange AngleBrackets);
464 static CXXConstCastExpr *CreateEmpty(const ASTContext &Context);
465
466 static bool classof(const Stmt *T) {
467 return T->getStmtClass() == CXXConstCastExprClass;
468 }
469};
470
471/// A call to a literal operator (C++11 [over.literal])
472/// written as a user-defined literal (C++11 [lit.ext]).
473///
474/// Represents a user-defined literal, e.g. "foo"_bar or 1.23_xyz. While this
475/// is semantically equivalent to a normal call, this AST node provides better
476/// information about the syntactic representation of the literal.
477///
478/// Since literal operators are never found by ADL and can only be declared at
479/// namespace scope, a user-defined literal is never dependent.
480class UserDefinedLiteral final : public CallExpr {
481 friend class ASTStmtReader;
482 friend class ASTStmtWriter;
483
484 /// The location of a ud-suffix within the literal.
485 SourceLocation UDSuffixLoc;
486
487 // UserDefinedLiteral has some trailing objects belonging
488 // to CallExpr. See CallExpr for the details.
489
490 UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args, QualType Ty,
491 ExprValueKind VK, SourceLocation LitEndLoc,
492 SourceLocation SuffixLoc);
493
494 UserDefinedLiteral(unsigned NumArgs, EmptyShell Empty);
495
496public:
497 static UserDefinedLiteral *Create(const ASTContext &Ctx, Expr *Fn,
498 ArrayRef<Expr *> Args, QualType Ty,
499 ExprValueKind VK, SourceLocation LitEndLoc,
500 SourceLocation SuffixLoc);
501
502 static UserDefinedLiteral *CreateEmpty(const ASTContext &Ctx,
503 unsigned NumArgs, EmptyShell Empty);
504
505 /// The kind of literal operator which is invoked.
506 enum LiteralOperatorKind {
507 /// Raw form: operator "" X (const char *)
508 LOK_Raw,
509
510 /// Raw form: operator "" X<cs...> ()
511 LOK_Template,
512
513 /// operator "" X (unsigned long long)
514 LOK_Integer,
515
516 /// operator "" X (long double)
517 LOK_Floating,
518
519 /// operator "" X (const CharT *, size_t)
520 LOK_String,
521
522 /// operator "" X (CharT)
523 LOK_Character
524 };
525
526 /// Returns the kind of literal operator invocation
527 /// which this expression represents.
528 LiteralOperatorKind getLiteralOperatorKind() const;
529
530 /// If this is not a raw user-defined literal, get the
531 /// underlying cooked literal (representing the literal with the suffix
532 /// removed).
533 Expr *getCookedLiteral();
534 const Expr *getCookedLiteral() const {
535 return const_cast<UserDefinedLiteral*>(this)->getCookedLiteral();
536 }
537
538 SourceLocation getBeginLoc() const {
539 if (getLiteralOperatorKind() == LOK_Template)
540 return getRParenLoc();
541 return getArg(0)->getBeginLoc();
542 }
543
544 SourceLocation getEndLoc() const { return getRParenLoc(); }
545
546 /// Returns the location of a ud-suffix in the expression.
547 ///
548 /// For a string literal, there may be multiple identical suffixes. This
549 /// returns the first.
550 SourceLocation getUDSuffixLoc() const { return UDSuffixLoc; }
551
552 /// Returns the ud-suffix specified for this literal.
553 const IdentifierInfo *getUDSuffix() const;
554
555 static bool classof(const Stmt *S) {
556 return S->getStmtClass() == UserDefinedLiteralClass;
557 }
558};
559
560/// A boolean literal, per ([C++ lex.bool] Boolean literals).
561class CXXBoolLiteralExpr : public Expr {
562public:
563 CXXBoolLiteralExpr(bool Val, QualType Ty, SourceLocation Loc)
564 : Expr(CXXBoolLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
565 false, false) {
566 CXXBoolLiteralExprBits.Value = Val;
567 CXXBoolLiteralExprBits.Loc = Loc;
568 }
569
570 explicit CXXBoolLiteralExpr(EmptyShell Empty)
571 : Expr(CXXBoolLiteralExprClass, Empty) {}
572
573 bool getValue() const { return CXXBoolLiteralExprBits.Value; }
574 void setValue(bool V) { CXXBoolLiteralExprBits.Value = V; }
575
576 SourceLocation getBeginLoc() const { return getLocation(); }
577 SourceLocation getEndLoc() const { return getLocation(); }
578
579 SourceLocation getLocation() const { return CXXBoolLiteralExprBits.Loc; }
580 void setLocation(SourceLocation L) { CXXBoolLiteralExprBits.Loc = L; }
581
582 static bool classof(const Stmt *T) {
583 return T->getStmtClass() == CXXBoolLiteralExprClass;
584 }
585
586 // Iterators
587 child_range children() {
588 return child_range(child_iterator(), child_iterator());
589 }
590};
591
592/// The null pointer literal (C++11 [lex.nullptr])
593///
594/// Introduced in C++11, the only literal of type \c nullptr_t is \c nullptr.
595class CXXNullPtrLiteralExpr : public Expr {
596public:
597 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation Loc)
598 : Expr(CXXNullPtrLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false,
599 false, false, false) {
600 CXXNullPtrLiteralExprBits.Loc = Loc;
601 }
602
603 explicit CXXNullPtrLiteralExpr(EmptyShell Empty)
604 : Expr(CXXNullPtrLiteralExprClass, Empty) {}
605
606 SourceLocation getBeginLoc() const { return getLocation(); }
607 SourceLocation getEndLoc() const { return getLocation(); }
608
609 SourceLocation getLocation() const { return CXXNullPtrLiteralExprBits.Loc; }
610 void setLocation(SourceLocation L) { CXXNullPtrLiteralExprBits.Loc = L; }
611
612 static bool classof(const Stmt *T) {
613 return T->getStmtClass() == CXXNullPtrLiteralExprClass;
614 }
615
616 child_range children() {
617 return child_range(child_iterator(), child_iterator());
618 }
619};
620
621/// Implicit construction of a std::initializer_list<T> object from an
622/// array temporary within list-initialization (C++11 [dcl.init.list]p5).
623class CXXStdInitializerListExpr : public Expr {
624 Stmt *SubExpr = nullptr;
625
626 CXXStdInitializerListExpr(EmptyShell Empty)
627 : Expr(CXXStdInitializerListExprClass, Empty) {}
628
629public:
630 friend class ASTReader;
631 friend class ASTStmtReader;
632
633 CXXStdInitializerListExpr(QualType Ty, Expr *SubExpr)
634 : Expr(CXXStdInitializerListExprClass, Ty, VK_RValue, OK_Ordinary,
635 Ty->isDependentType(), SubExpr->isValueDependent(),
636 SubExpr->isInstantiationDependent(),
637 SubExpr->containsUnexpandedParameterPack()),
638 SubExpr(SubExpr) {}
639
640 Expr *getSubExpr() { return static_cast<Expr*>(SubExpr); }
641 const Expr *getSubExpr() const { return static_cast<const Expr*>(SubExpr); }
642
643 SourceLocation getBeginLoc() const LLVM_READONLY {
644 return SubExpr->getBeginLoc();
645 }
646
647 SourceLocation getEndLoc() const LLVM_READONLY {
648 return SubExpr->getEndLoc();
649 }
650
651 /// Retrieve the source range of the expression.
652 SourceRange getSourceRange() const LLVM_READONLY {
653 return SubExpr->getSourceRange();
654 }
655
656 static bool classof(const Stmt *S) {
657 return S->getStmtClass() == CXXStdInitializerListExprClass;
658 }
659
660 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
661};
662
663/// A C++ \c typeid expression (C++ [expr.typeid]), which gets
664/// the \c type_info that corresponds to the supplied type, or the (possibly
665/// dynamic) type of the supplied expression.
666///
667/// This represents code like \c typeid(int) or \c typeid(*objPtr)
668class CXXTypeidExpr : public Expr {
669private:
670 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
671 SourceRange Range;
672
673public:
674 CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R)
675 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary,
676 // typeid is never type-dependent (C++ [temp.dep.expr]p4)
677 false,
678 // typeid is value-dependent if the type or expression are
679 // dependent
680 Operand->getType()->isDependentType(),
681 Operand->getType()->isInstantiationDependentType(),
682 Operand->getType()->containsUnexpandedParameterPack()),
683 Operand(Operand), Range(R) {}
684
685 CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R)
686 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary,
687 // typeid is never type-dependent (C++ [temp.dep.expr]p4)
688 false,
689 // typeid is value-dependent if the type or expression are
690 // dependent
691 Operand->isTypeDependent() || Operand->isValueDependent(),
692 Operand->isInstantiationDependent(),
693 Operand->containsUnexpandedParameterPack()),
694 Operand(Operand), Range(R) {}
695
696 CXXTypeidExpr(EmptyShell Empty, bool isExpr)
697 : Expr(CXXTypeidExprClass, Empty) {
698 if (isExpr)
699 Operand = (Expr*)nullptr;
700 else
701 Operand = (TypeSourceInfo*)nullptr;
702 }
703
704 /// Determine whether this typeid has a type operand which is potentially
705 /// evaluated, per C++11 [expr.typeid]p3.
706 bool isPotentiallyEvaluated() const;
707
708 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
709
710 /// Retrieves the type operand of this typeid() expression after
711 /// various required adjustments (removing reference types, cv-qualifiers).
712 QualType getTypeOperand(ASTContext &Context) const;
713
714 /// Retrieve source information for the type operand.
715 TypeSourceInfo *getTypeOperandSourceInfo() const {
716 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
717 return Operand.get<TypeSourceInfo *>();
718 }
719
720 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) {
721 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
722 Operand = TSI;
723 }
724
725 Expr *getExprOperand() const {
726 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
727 return static_cast<Expr*>(Operand.get<Stmt *>());
728 }
729
730 void setExprOperand(Expr *E) {
731 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
732 Operand = E;
733 }
734
735 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
736 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
737 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
738 void setSourceRange(SourceRange R) { Range = R; }
739
740 static bool classof(const Stmt *T) {
741 return T->getStmtClass() == CXXTypeidExprClass;
742 }
743
744 // Iterators
745 child_range children() {
746 if (isTypeOperand())
747 return child_range(child_iterator(), child_iterator());
748 auto **begin = reinterpret_cast<Stmt **>(&Operand);
749 return child_range(begin, begin + 1);
750 }
751};
752
753/// A member reference to an MSPropertyDecl.
754///
755/// This expression always has pseudo-object type, and therefore it is
756/// typically not encountered in a fully-typechecked expression except
757/// within the syntactic form of a PseudoObjectExpr.
758class MSPropertyRefExpr : public Expr {
759 Expr *BaseExpr;
760 MSPropertyDecl *TheDecl;
761 SourceLocation MemberLoc;
762 bool IsArrow;
763 NestedNameSpecifierLoc QualifierLoc;
764
765public:
766 friend class ASTStmtReader;
767
768 MSPropertyRefExpr(Expr *baseExpr, MSPropertyDecl *decl, bool isArrow,
769 QualType ty, ExprValueKind VK,
770 NestedNameSpecifierLoc qualifierLoc,
771 SourceLocation nameLoc)
772 : Expr(MSPropertyRefExprClass, ty, VK, OK_Ordinary,
773 /*type-dependent*/ false, baseExpr->isValueDependent(),
774 baseExpr->isInstantiationDependent(),
775 baseExpr->containsUnexpandedParameterPack()),
776 BaseExpr(baseExpr), TheDecl(decl),
777 MemberLoc(nameLoc), IsArrow(isArrow),
778 QualifierLoc(qualifierLoc) {}
779
780 MSPropertyRefExpr(EmptyShell Empty) : Expr(MSPropertyRefExprClass, Empty) {}
781
782 SourceRange getSourceRange() const LLVM_READONLY {
783 return SourceRange(getBeginLoc(), getEndLoc());
784 }
785
786 bool isImplicitAccess() const {
787 return getBaseExpr() && getBaseExpr()->isImplicitCXXThis();
788 }
789
790 SourceLocation getBeginLoc() const {
791 if (!isImplicitAccess())
792 return BaseExpr->getBeginLoc();
793 else if (QualifierLoc)
794 return QualifierLoc.getBeginLoc();
795 else
796 return MemberLoc;
797 }
798
799 SourceLocation getEndLoc() const { return getMemberLoc(); }
800
801 child_range children() {
802 return child_range((Stmt**)&BaseExpr, (Stmt**)&BaseExpr + 1);
803 }
804
805 static bool classof(const Stmt *T) {
806 return T->getStmtClass() == MSPropertyRefExprClass;
807 }
808
809 Expr *getBaseExpr() const { return BaseExpr; }
810 MSPropertyDecl *getPropertyDecl() const { return TheDecl; }
811 bool isArrow() const { return IsArrow; }
812 SourceLocation getMemberLoc() const { return MemberLoc; }
813 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
814};
815
816/// MS property subscript expression.
817/// MSVC supports 'property' attribute and allows to apply it to the
818/// declaration of an empty array in a class or structure definition.
819/// For example:
820/// \code
821/// __declspec(property(get=GetX, put=PutX)) int x[];
822/// \endcode
823/// The above statement indicates that x[] can be used with one or more array
824/// indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), and
825/// p->x[a][b] = i will be turned into p->PutX(a, b, i).
826/// This is a syntactic pseudo-object expression.
827class MSPropertySubscriptExpr : public Expr {
828 friend class ASTStmtReader;
829
830 enum { BASE_EXPR, IDX_EXPR, NUM_SUBEXPRS = 2 };
831
832 Stmt *SubExprs[NUM_SUBEXPRS];
833 SourceLocation RBracketLoc;
834
835 void setBase(Expr *Base) { SubExprs[BASE_EXPR] = Base; }
836 void setIdx(Expr *Idx) { SubExprs[IDX_EXPR] = Idx; }
837
838public:
839 MSPropertySubscriptExpr(Expr *Base, Expr *Idx, QualType Ty, ExprValueKind VK,
840 ExprObjectKind OK, SourceLocation RBracketLoc)
841 : Expr(MSPropertySubscriptExprClass, Ty, VK, OK, Idx->isTypeDependent(),
842 Idx->isValueDependent(), Idx->isInstantiationDependent(),
843 Idx->containsUnexpandedParameterPack()),
844 RBracketLoc(RBracketLoc) {
845 SubExprs[BASE_EXPR] = Base;
846 SubExprs[IDX_EXPR] = Idx;
847 }
848
849 /// Create an empty array subscript expression.
850 explicit MSPropertySubscriptExpr(EmptyShell Shell)
851 : Expr(MSPropertySubscriptExprClass, Shell) {}
852
853 Expr *getBase() { return cast<Expr>(SubExprs[BASE_EXPR]); }
854 const Expr *getBase() const { return cast<Expr>(SubExprs[BASE_EXPR]); }
855
856 Expr *getIdx() { return cast<Expr>(SubExprs[IDX_EXPR]); }
857 const Expr *getIdx() const { return cast<Expr>(SubExprs[IDX_EXPR]); }
858
859 SourceLocation getBeginLoc() const LLVM_READONLY {
860 return getBase()->getBeginLoc();
861 }
862
863 SourceLocation getEndLoc() const LLVM_READONLY { return RBracketLoc; }
864
865 SourceLocation getRBracketLoc() const { return RBracketLoc; }
866 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
867
868 SourceLocation getExprLoc() const LLVM_READONLY {
869 return getBase()->getExprLoc();
870 }
871
872 static bool classof(const Stmt *T) {
873 return T->getStmtClass() == MSPropertySubscriptExprClass;
874 }
875
876 // Iterators
877 child_range children() {
878 return child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
879 }
880};
881
882/// A Microsoft C++ @c __uuidof expression, which gets
883/// the _GUID that corresponds to the supplied type or expression.
884///
885/// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr)
886class CXXUuidofExpr : public Expr {
887private:
888 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
889 StringRef UuidStr;
890 SourceRange Range;
891
892public:
893 CXXUuidofExpr(QualType Ty, TypeSourceInfo *Operand, StringRef UuidStr,
894 SourceRange R)
895 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, false,
896 Operand->getType()->isDependentType(),
897 Operand->getType()->isInstantiationDependentType(),
898 Operand->getType()->containsUnexpandedParameterPack()),
899 Operand(Operand), UuidStr(UuidStr), Range(R) {}
900
901 CXXUuidofExpr(QualType Ty, Expr *Operand, StringRef UuidStr, SourceRange R)
902 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, false,
903 Operand->isTypeDependent(), Operand->isInstantiationDependent(),
904 Operand->containsUnexpandedParameterPack()),
905 Operand(Operand), UuidStr(UuidStr), Range(R) {}
906
907 CXXUuidofExpr(EmptyShell Empty, bool isExpr)
908 : Expr(CXXUuidofExprClass, Empty) {
909 if (isExpr)
910 Operand = (Expr*)nullptr;
911 else
912 Operand = (TypeSourceInfo*)nullptr;
913 }
914
915 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
916
917 /// Retrieves the type operand of this __uuidof() expression after
918 /// various required adjustments (removing reference types, cv-qualifiers).
919 QualType getTypeOperand(ASTContext &Context) const;
920
921 /// Retrieve source information for the type operand.
922 TypeSourceInfo *getTypeOperandSourceInfo() const {
923 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
924 return Operand.get<TypeSourceInfo *>();
925 }
926
927 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) {
928 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
929 Operand = TSI;
930 }
931
932 Expr *getExprOperand() const {
933 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
934 return static_cast<Expr*>(Operand.get<Stmt *>());
935 }
936
937 void setExprOperand(Expr *E) {
938 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
939 Operand = E;
940 }
941
942 void setUuidStr(StringRef US) { UuidStr = US; }
943 StringRef getUuidStr() const { return UuidStr; }
944
945 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
946 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
947 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
948 void setSourceRange(SourceRange R) { Range = R; }
949
950 static bool classof(const Stmt *T) {
951 return T->getStmtClass() == CXXUuidofExprClass;
952 }
953
954 // Iterators
955 child_range children() {
956 if (isTypeOperand())
957 return child_range(child_iterator(), child_iterator());
958 auto **begin = reinterpret_cast<Stmt **>(&Operand);
959 return child_range(begin, begin + 1);
960 }
961};
962
963/// Represents the \c this expression in C++.
964///
965/// This is a pointer to the object on which the current member function is
966/// executing (C++ [expr.prim]p3). Example:
967///
968/// \code
969/// class Foo {
970/// public:
971/// void bar();
972/// void test() { this->bar(); }
973/// };
974/// \endcode
975class CXXThisExpr : public Expr {
976public:
977 CXXThisExpr(SourceLocation L, QualType Ty, bool IsImplicit)
978 : Expr(CXXThisExprClass, Ty, VK_RValue, OK_Ordinary,
979 // 'this' is type-dependent if the class type of the enclosing
980 // member function is dependent (C++ [temp.dep.expr]p2)
981 Ty->isDependentType(), Ty->isDependentType(),
982 Ty->isInstantiationDependentType(),
983 /*ContainsUnexpandedParameterPack=*/false) {
984 CXXThisExprBits.IsImplicit = IsImplicit;
985 CXXThisExprBits.Loc = L;
986 }
987
988 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {}
989
990 SourceLocation getLocation() const { return CXXThisExprBits.Loc; }
991 void setLocation(SourceLocation L) { CXXThisExprBits.Loc = L; }
992
993 SourceLocation getBeginLoc() const { return getLocation(); }
994 SourceLocation getEndLoc() const { return getLocation(); }
995
996 bool isImplicit() const { return CXXThisExprBits.IsImplicit; }
997 void setImplicit(bool I) { CXXThisExprBits.IsImplicit = I; }
998
999 static bool classof(const Stmt *T) {
1000 return T->getStmtClass() == CXXThisExprClass;
1001 }
1002
1003 // Iterators
1004 child_range children() {
1005 return child_range(child_iterator(), child_iterator());
1006 }
1007};
1008
1009/// A C++ throw-expression (C++ [except.throw]).
1010///
1011/// This handles 'throw' (for re-throwing the current exception) and
1012/// 'throw' assignment-expression. When assignment-expression isn't
1013/// present, Op will be null.
1014class CXXThrowExpr : public Expr {
1015 friend class ASTStmtReader;
1016
1017 /// The optional expression in the throw statement.
1018 Stmt *Operand;
1019
1020public:
1021 // \p Ty is the void type which is used as the result type of the
1022 // expression. The \p Loc is the location of the throw keyword.
1023 // \p Operand is the expression in the throw statement, and can be
1024 // null if not present.
1025 CXXThrowExpr(Expr *Operand, QualType Ty, SourceLocation Loc,
1026 bool IsThrownVariableInScope)
1027 : Expr(CXXThrowExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
1028 Operand && Operand->isInstantiationDependent(),
1029 Operand && Operand->containsUnexpandedParameterPack()),
1030 Operand(Operand) {
1031 CXXThrowExprBits.ThrowLoc = Loc;
1032 CXXThrowExprBits.IsThrownVariableInScope = IsThrownVariableInScope;
1033 }
1034 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {}
1035
1036 const Expr *getSubExpr() const { return cast_or_null<Expr>(Operand); }
1037 Expr *getSubExpr() { return cast_or_null<Expr>(Operand); }
1038
1039 SourceLocation getThrowLoc() const { return CXXThrowExprBits.ThrowLoc; }
1040
1041 /// Determines whether the variable thrown by this expression (if any!)
1042 /// is within the innermost try block.
1043 ///
1044 /// This information is required to determine whether the NRVO can apply to
1045 /// this variable.
1046 bool isThrownVariableInScope() const {
1047 return CXXThrowExprBits.IsThrownVariableInScope;
1048 }
1049
1050 SourceLocation getBeginLoc() const { return getThrowLoc(); }
1051 SourceLocation getEndLoc() const LLVM_READONLY {
1052 if (!getSubExpr())
1053 return getThrowLoc();
1054 return getSubExpr()->getEndLoc();
1055 }
1056
1057 static bool classof(const Stmt *T) {
1058 return T->getStmtClass() == CXXThrowExprClass;
1059 }
1060
1061 // Iterators
1062 child_range children() {
1063 return child_range(&Operand, Operand ? &Operand + 1 : &Operand);
1064 }
1065};
1066
1067/// A default argument (C++ [dcl.fct.default]).
1068///
1069/// This wraps up a function call argument that was created from the
1070/// corresponding parameter's default argument, when the call did not
1071/// explicitly supply arguments for all of the parameters.
1072class CXXDefaultArgExpr final : public Expr {
1073 friend class ASTStmtReader;
1074
1075 /// The parameter whose default is being used.
1076 ParmVarDecl *Param;
1077
1078 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *Param)
1079 : Expr(SC,
1080 Param->hasUnparsedDefaultArg()
1081 ? Param->getType().getNonReferenceType()
1082 : Param->getDefaultArg()->getType(),
1083 Param->getDefaultArg()->getValueKind(),
1084 Param->getDefaultArg()->getObjectKind(), false, false, false,
1085 false),
1086 Param(Param) {
1087 CXXDefaultArgExprBits.Loc = Loc;
1088 }
1089
1090public:
1091 CXXDefaultArgExpr(EmptyShell Empty) : Expr(CXXDefaultArgExprClass, Empty) {}
1092
1093 // \p Param is the parameter whose default argument is used by this
1094 // expression.
1095 static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc,
1096 ParmVarDecl *Param) {
1097 return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param);
1098 }
1099
1100 // Retrieve the parameter that the argument was created from.
1101 const ParmVarDecl *getParam() const { return Param; }
1102 ParmVarDecl *getParam() { return Param; }
1103
1104 // Retrieve the actual argument to the function call.
1105 const Expr *getExpr() const { return getParam()->getDefaultArg(); }
1106 Expr *getExpr() { return getParam()->getDefaultArg(); }
1107
1108 /// Retrieve the location where this default argument was actually used.
1109 SourceLocation getUsedLocation() const { return CXXDefaultArgExprBits.Loc; }
1110
1111 /// Default argument expressions have no representation in the
1112 /// source, so they have an empty source range.
1113 SourceLocation getBeginLoc() const { return SourceLocation(); }
1114 SourceLocation getEndLoc() const { return SourceLocation(); }
1115
1116 SourceLocation getExprLoc() const { return getUsedLocation(); }
1117
1118 static bool classof(const Stmt *T) {
1119 return T->getStmtClass() == CXXDefaultArgExprClass;
1120 }
1121
1122 // Iterators
1123 child_range children() {
1124 return child_range(child_iterator(), child_iterator());
1125 }
1126};
1127
1128/// A use of a default initializer in a constructor or in aggregate
1129/// initialization.
1130///
1131/// This wraps a use of a C++ default initializer (technically,
1132/// a brace-or-equal-initializer for a non-static data member) when it
1133/// is implicitly used in a mem-initializer-list in a constructor
1134/// (C++11 [class.base.init]p8) or in aggregate initialization
1135/// (C++1y [dcl.init.aggr]p7).
1136class CXXDefaultInitExpr : public Expr {
1137 friend class ASTReader;
1138 friend class ASTStmtReader;
1139
1140 /// The field whose default is being used.
1141 FieldDecl *Field;
1142
1143 CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
1144 FieldDecl *Field, QualType Ty);
1145
1146 CXXDefaultInitExpr(EmptyShell Empty) : Expr(CXXDefaultInitExprClass, Empty) {}
1147
1148public:
1149 /// \p Field is the non-static data member whose default initializer is used
1150 /// by this expression.
1151 static CXXDefaultInitExpr *Create(const ASTContext &Ctx, SourceLocation Loc,
1152 FieldDecl *Field) {
1153 return new (Ctx) CXXDefaultInitExpr(Ctx, Loc, Field, Field->getType());
1154 }
1155
1156 /// Get the field whose initializer will be used.
1157 FieldDecl *getField() { return Field; }
1158 const FieldDecl *getField() const { return Field; }
1159
1160 /// Get the initialization expression that will be used.
1161 const Expr *getExpr() const {
1162 assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1163 return Field->getInClassInitializer();
1164 }
1165 Expr *getExpr() {
1166 assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1167 return Field->getInClassInitializer();
1168 }
1169
1170 SourceLocation getBeginLoc() const { return CXXDefaultInitExprBits.Loc; }
1171 SourceLocation getEndLoc() const { return CXXDefaultInitExprBits.Loc; }
1172
1173 static bool classof(const Stmt *T) {
1174 return T->getStmtClass() == CXXDefaultInitExprClass;
1175 }
1176
1177 // Iterators
1178 child_range children() {
1179 return child_range(child_iterator(), child_iterator());
1180 }
1181};
1182
1183/// Represents a C++ temporary.
1184class CXXTemporary {
1185 /// The destructor that needs to be called.
1186 const CXXDestructorDecl *Destructor;
1187
1188 explicit CXXTemporary(const CXXDestructorDecl *destructor)
1189 : Destructor(destructor) {}
1190
1191public:
1192 static CXXTemporary *Create(const ASTContext &C,
1193 const CXXDestructorDecl *Destructor);
1194
1195 const CXXDestructorDecl *getDestructor() const { return Destructor; }
1196
1197 void setDestructor(const CXXDestructorDecl *Dtor) {
1198 Destructor = Dtor;
1199 }
1200};
1201
1202/// Represents binding an expression to a temporary.
1203///
1204/// This ensures the destructor is called for the temporary. It should only be
1205/// needed for non-POD, non-trivially destructable class types. For example:
1206///
1207/// \code
1208/// struct S {
1209/// S() { } // User defined constructor makes S non-POD.
1210/// ~S() { } // User defined destructor makes it non-trivial.
1211/// };
1212/// void test() {
1213/// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr.
1214/// }
1215/// \endcode
1216class CXXBindTemporaryExpr : public Expr {
1217 CXXTemporary *Temp = nullptr;
1218 Stmt *SubExpr = nullptr;
1219
1220 CXXBindTemporaryExpr(CXXTemporary *temp, Expr* SubExpr)
1221 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(),
1222 VK_RValue, OK_Ordinary, SubExpr->isTypeDependent(),
1223 SubExpr->isValueDependent(),
1224 SubExpr->isInstantiationDependent(),
1225 SubExpr->containsUnexpandedParameterPack()),
1226 Temp(temp), SubExpr(SubExpr) {}
1227
1228public:
1229 CXXBindTemporaryExpr(EmptyShell Empty)
1230 : Expr(CXXBindTemporaryExprClass, Empty) {}
1231
1232 static CXXBindTemporaryExpr *Create(const ASTContext &C, CXXTemporary *Temp,
1233 Expr* SubExpr);
1234
1235 CXXTemporary *getTemporary() { return Temp; }
1236 const CXXTemporary *getTemporary() const { return Temp; }
1237 void setTemporary(CXXTemporary *T) { Temp = T; }
1238
1239 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
1240 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
1241 void setSubExpr(Expr *E) { SubExpr = E; }
1242
1243 SourceLocation getBeginLoc() const LLVM_READONLY {
1244 return SubExpr->getBeginLoc();
1245 }
1246
1247 SourceLocation getEndLoc() const LLVM_READONLY {
1248 return SubExpr->getEndLoc();
1249 }
1250
1251 // Implement isa/cast/dyncast/etc.
1252 static bool classof(const Stmt *T) {
1253 return T->getStmtClass() == CXXBindTemporaryExprClass;
1254 }
1255
1256 // Iterators
1257 child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
1258};
1259
1260/// Represents a call to a C++ constructor.
1261class CXXConstructExpr : public Expr {
1262 friend class ASTStmtReader;
1263
1264public:
1265 enum ConstructionKind {
1266 CK_Complete,
1267 CK_NonVirtualBase,
1268 CK_VirtualBase,
1269 CK_Delegating
1270 };
1271
1272private:
1273 /// A pointer to the constructor which will be ultimately called.
1274 CXXConstructorDecl *Constructor;
1275
1276 SourceRange ParenOrBraceRange;
1277
1278 /// The number of arguments.
1279 unsigned NumArgs;
1280
1281 // We would like to stash the arguments of the constructor call after
1282 // CXXConstructExpr. However CXXConstructExpr is used as a base class of
1283 // CXXTemporaryObjectExpr which makes the use of llvm::TrailingObjects
1284 // impossible.
1285 //
1286 // Instead we manually stash the trailing object after the full object
1287 // containing CXXConstructExpr (that is either CXXConstructExpr or
1288 // CXXTemporaryObjectExpr).
1289 //
1290 // The trailing objects are:
1291 //
1292 // * An array of getNumArgs() "Stmt *" for the arguments of the
1293 // constructor call.
1294
1295 /// Return a pointer to the start of the trailing arguments.
1296 /// Defined just after CXXTemporaryObjectExpr.
1297 inline Stmt **getTrailingArgs();
1298 const Stmt *const *getTrailingArgs() const {
1299 return const_cast<CXXConstructExpr *>(this)->getTrailingArgs();
1300 }
1301
1302protected:
1303 /// Build a C++ construction expression.
1304 CXXConstructExpr(StmtClass SC, QualType Ty, SourceLocation Loc,
1305 CXXConstructorDecl *Ctor, bool Elidable,
1306 ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1307 bool ListInitialization, bool StdInitListInitialization,
1308 bool ZeroInitialization, ConstructionKind ConstructKind,
1309 SourceRange ParenOrBraceRange);
1310
1311 /// Build an empty C++ construction expression.
1312 CXXConstructExpr(StmtClass SC, EmptyShell Empty, unsigned NumArgs);
1313
1314 /// Return the size in bytes of the trailing objects. Used by
1315 /// CXXTemporaryObjectExpr to allocate the right amount of storage.
1316 static unsigned sizeOfTrailingObjects(unsigned NumArgs) {
1317 return NumArgs * sizeof(Stmt *);
1318 }
1319
1320public:
1321 /// Create a C++ construction expression.
1322 static CXXConstructExpr *
1323 Create(const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
1324 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
1325 bool HadMultipleCandidates, bool ListInitialization,
1326 bool StdInitListInitialization, bool ZeroInitialization,
1327 ConstructionKind ConstructKind, SourceRange ParenOrBraceRange);
1328
1329 /// Create an empty C++ construction expression.
1330 static CXXConstructExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs);
1331
1332 /// Get the constructor that this expression will (ultimately) call.
1333 CXXConstructorDecl *getConstructor() const { return Constructor; }
1334
1335 SourceLocation getLocation() const { return CXXConstructExprBits.Loc; }
1336 void setLocation(SourceLocation Loc) { CXXConstructExprBits.Loc = Loc; }
1337
1338 /// Whether this construction is elidable.
1339 bool isElidable() const { return CXXConstructExprBits.Elidable; }
1340 void setElidable(bool E) { CXXConstructExprBits.Elidable = E; }
1341
1342 /// Whether the referred constructor was resolved from
1343 /// an overloaded set having size greater than 1.
1344 bool hadMultipleCandidates() const {
1345 return CXXConstructExprBits.HadMultipleCandidates;
1346 }
1347 void setHadMultipleCandidates(bool V) {
1348 CXXConstructExprBits.HadMultipleCandidates = V;
1349 }
1350
1351 /// Whether this constructor call was written as list-initialization.
1352 bool isListInitialization() const {
1353 return CXXConstructExprBits.ListInitialization;
1354 }
1355 void setListInitialization(bool V) {
1356 CXXConstructExprBits.ListInitialization = V;
1357 }
1358
1359 /// Whether this constructor call was written as list-initialization,
1360 /// but was interpreted as forming a std::initializer_list<T> from the list
1361 /// and passing that as a single constructor argument.
1362 /// See C++11 [over.match.list]p1 bullet 1.
1363 bool isStdInitListInitialization() const {
1364 return CXXConstructExprBits.StdInitListInitialization;
1365 }
1366 void setStdInitListInitialization(bool V) {
1367 CXXConstructExprBits.StdInitListInitialization = V;
1368 }
1369
1370 /// Whether this construction first requires
1371 /// zero-initialization before the initializer is called.
1372 bool requiresZeroInitialization() const {
1373 return CXXConstructExprBits.ZeroInitialization;
1374 }
1375 void setRequiresZeroInitialization(bool ZeroInit) {
1376 CXXConstructExprBits.ZeroInitialization = ZeroInit;
1377 }
1378
1379 /// Determine whether this constructor is actually constructing
1380 /// a base class (rather than a complete object).
1381 ConstructionKind getConstructionKind() const {
1382 return static_cast<ConstructionKind>(CXXConstructExprBits.ConstructionKind);
1383 }
1384 void setConstructionKind(ConstructionKind CK) {
1385 CXXConstructExprBits.ConstructionKind = CK;
1386 }
1387
1388 using arg_iterator = ExprIterator;
1389 using const_arg_iterator = ConstExprIterator;
1390 using arg_range = llvm::iterator_range<arg_iterator>;
1391 using const_arg_range = llvm::iterator_range<const_arg_iterator>;
1392
1393 arg_range arguments() { return arg_range(arg_begin(), arg_end()); }
1394 const_arg_range arguments() const {
1395 return const_arg_range(arg_begin(), arg_end());
1396 }
1397
1398 arg_iterator arg_begin() { return getTrailingArgs(); }
1399 arg_iterator arg_end() { return arg_begin() + getNumArgs(); }
1400 const_arg_iterator arg_begin() const { return getTrailingArgs(); }
1401 const_arg_iterator arg_end() const { return arg_begin() + getNumArgs(); }
1402
1403 Expr **getArgs() { return reinterpret_cast<Expr **>(getTrailingArgs()); }
1404 const Expr *const *getArgs() const {
1405 return reinterpret_cast<const Expr *const *>(getTrailingArgs());
1406 }
1407
1408 /// Return the number of arguments to the constructor call.
1409 unsigned getNumArgs() const { return NumArgs; }
1410
1411 /// Return the specified argument.
1412 Expr *getArg(unsigned Arg) {
1413 assert(Arg < getNumArgs() && "Arg access out of range!");
1414 return getArgs()[Arg];
1415 }
1416 const Expr *getArg(unsigned Arg) const {
1417 assert(Arg < getNumArgs() && "Arg access out of range!");
1418 return getArgs()[Arg];
1419 }
1420
1421 /// Set the specified argument.
1422 void setArg(unsigned Arg, Expr *ArgExpr) {
1423 assert(Arg < getNumArgs() && "Arg access out of range!");
1424 getArgs()[Arg] = ArgExpr;
1425 }
1426
1427 SourceLocation getBeginLoc() const LLVM_READONLY;
1428 SourceLocation getEndLoc() const LLVM_READONLY;
1429 SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
1430 void setParenOrBraceRange(SourceRange Range) { ParenOrBraceRange = Range; }
1431
1432 static bool classof(const Stmt *T) {
1433 return T->getStmtClass() == CXXConstructExprClass ||
1434 T->getStmtClass() == CXXTemporaryObjectExprClass;
1435 }
1436
1437 // Iterators
1438 child_range children() {
1439 return child_range(getTrailingArgs(), getTrailingArgs() + getNumArgs());
1440 }
1441};
1442
1443/// Represents a call to an inherited base class constructor from an
1444/// inheriting constructor. This call implicitly forwards the arguments from
1445/// the enclosing context (an inheriting constructor) to the specified inherited
1446/// base class constructor.
1447class CXXInheritedCtorInitExpr : public Expr {
1448private:
1449 CXXConstructorDecl *Constructor = nullptr;
1450
1451 /// The location of the using declaration.
1452 SourceLocation Loc;
1453
1454 /// Whether this is the construction of a virtual base.
1455 unsigned ConstructsVirtualBase : 1;
1456
1457 /// Whether the constructor is inherited from a virtual base class of the
1458 /// class that we construct.
1459 unsigned InheritedFromVirtualBase : 1;
1460
1461public:
1462 friend class ASTStmtReader;
1463
1464 /// Construct a C++ inheriting construction expression.
1465 CXXInheritedCtorInitExpr(SourceLocation Loc, QualType T,
1466 CXXConstructorDecl *Ctor, bool ConstructsVirtualBase,
1467 bool InheritedFromVirtualBase)
1468 : Expr(CXXInheritedCtorInitExprClass, T, VK_RValue, OK_Ordinary, false,
1469 false, false, false),
1470 Constructor(Ctor), Loc(Loc),
1471 ConstructsVirtualBase(ConstructsVirtualBase),
1472 InheritedFromVirtualBase(InheritedFromVirtualBase) {
1473 assert(!T->isDependentType());
1474 }
1475
1476 /// Construct an empty C++ inheriting construction expression.
1477 explicit CXXInheritedCtorInitExpr(EmptyShell Empty)
1478 : Expr(CXXInheritedCtorInitExprClass, Empty),
1479 ConstructsVirtualBase(false), InheritedFromVirtualBase(false) {}
1480
1481 /// Get the constructor that this expression will call.
1482 CXXConstructorDecl *getConstructor() const { return Constructor; }
1483
1484 /// Determine whether this constructor is actually constructing
1485 /// a base class (rather than a complete object).
1486 bool constructsVBase() const { return ConstructsVirtualBase; }
1487 CXXConstructExpr::ConstructionKind getConstructionKind() const {
1488 return ConstructsVirtualBase ? CXXConstructExpr::CK_VirtualBase
1489 : CXXConstructExpr::CK_NonVirtualBase;
1490 }
1491
1492 /// Determine whether the inherited constructor is inherited from a
1493 /// virtual base of the object we construct. If so, we are not responsible
1494 /// for calling the inherited constructor (the complete object constructor
1495 /// does that), and so we don't need to pass any arguments.
1496 bool inheritedFromVBase() const { return InheritedFromVirtualBase; }
1497
1498 SourceLocation getLocation() const LLVM_READONLY { return Loc; }
1499 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
1500 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
1501
1502 static bool classof(const Stmt *T) {
1503 return T->getStmtClass() == CXXInheritedCtorInitExprClass;
1504 }
1505
1506 child_range children() {
1507 return child_range(child_iterator(), child_iterator());
1508 }
1509};
1510
1511/// Represents an explicit C++ type conversion that uses "functional"
1512/// notation (C++ [expr.type.conv]).
1513///
1514/// Example:
1515/// \code
1516/// x = int(0.5);
1517/// \endcode
1518class CXXFunctionalCastExpr final
1519 : public ExplicitCastExpr,
1520 private llvm::TrailingObjects<CXXFunctionalCastExpr, CXXBaseSpecifier *> {
1521 SourceLocation LParenLoc;
1522 SourceLocation RParenLoc;
1523
1524 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK,
1525 TypeSourceInfo *writtenTy,
1526 CastKind kind, Expr *castExpr, unsigned pathSize,
1527 SourceLocation lParenLoc, SourceLocation rParenLoc)
1528 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind,
1529 castExpr, pathSize, writtenTy),
1530 LParenLoc(lParenLoc), RParenLoc(rParenLoc) {}
1531
1532 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize)
1533 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize) {}
1534
1535public:
1536 friend class CastExpr;
1537 friend TrailingObjects;
1538
1539 static CXXFunctionalCastExpr *Create(const ASTContext &Context, QualType T,
1540 ExprValueKind VK,
1541 TypeSourceInfo *Written,
1542 CastKind Kind, Expr *Op,
1543 const CXXCastPath *Path,
1544 SourceLocation LPLoc,
1545 SourceLocation RPLoc);
1546 static CXXFunctionalCastExpr *CreateEmpty(const ASTContext &Context,
1547 unsigned PathSize);
1548
1549 SourceLocation getLParenLoc() const { return LParenLoc; }
1550 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1551 SourceLocation getRParenLoc() const { return RParenLoc; }
1552 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1553
1554 /// Determine whether this expression models list-initialization.
1555 bool isListInitialization() const { return LParenLoc.isInvalid(); }
1556
1557 SourceLocation getBeginLoc() const LLVM_READONLY;
1558 SourceLocation getEndLoc() const LLVM_READONLY;
1559
1560 static bool classof(const Stmt *T) {
1561 return T->getStmtClass() == CXXFunctionalCastExprClass;
1562 }
1563};
1564
1565/// Represents a C++ functional cast expression that builds a
1566/// temporary object.
1567///
1568/// This expression type represents a C++ "functional" cast
1569/// (C++[expr.type.conv]) with N != 1 arguments that invokes a
1570/// constructor to build a temporary object. With N == 1 arguments the
1571/// functional cast expression will be represented by CXXFunctionalCastExpr.
1572/// Example:
1573/// \code
1574/// struct X { X(int, float); }
1575///
1576/// X create_X() {
1577/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
1578/// };
1579/// \endcode
1580class CXXTemporaryObjectExpr final : public CXXConstructExpr {
1581 friend class ASTStmtReader;
1582
1583 // CXXTemporaryObjectExpr has some trailing objects belonging
1584 // to CXXConstructExpr. See the comment inside CXXConstructExpr
1585 // for more details.
1586
1587 TypeSourceInfo *TSI;
1588
1589 CXXTemporaryObjectExpr(CXXConstructorDecl *Cons, QualType Ty,
1590 TypeSourceInfo *TSI, ArrayRef<Expr *> Args,
1591 SourceRange ParenOrBraceRange,
1592 bool HadMultipleCandidates, bool ListInitialization,
1593 bool StdInitListInitialization,
1594 bool ZeroInitialization);
1595
1596 CXXTemporaryObjectExpr(EmptyShell Empty, unsigned NumArgs);
1597
1598public:
1599 static CXXTemporaryObjectExpr *
1600 Create(const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
1601 TypeSourceInfo *TSI, ArrayRef<Expr *> Args,
1602 SourceRange ParenOrBraceRange, bool HadMultipleCandidates,
1603 bool ListInitialization, bool StdInitListInitialization,
1604 bool ZeroInitialization);
1605
1606 static CXXTemporaryObjectExpr *CreateEmpty(const ASTContext &Ctx,
1607 unsigned NumArgs);
1608
1609 TypeSourceInfo *getTypeSourceInfo() const { return TSI; }
1610
1611 SourceLocation getBeginLoc() const LLVM_READONLY;
1612 SourceLocation getEndLoc() const LLVM_READONLY;
1613
1614 static bool classof(const Stmt *T) {
1615 return T->getStmtClass() == CXXTemporaryObjectExprClass;
1616 }
1617};
1618
1619Stmt **CXXConstructExpr::getTrailingArgs() {
1620 if (auto *E = dyn_cast<CXXTemporaryObjectExpr>(this))
1621 return reinterpret_cast<Stmt **>(E + 1);
1622 assert((getStmtClass() == CXXConstructExprClass) &&
1623 "Unexpected class deriving from CXXConstructExpr!");
1624 return reinterpret_cast<Stmt **>(this + 1);
1625}
1626
1627/// A C++ lambda expression, which produces a function object
1628/// (of unspecified type) that can be invoked later.
1629///
1630/// Example:
1631/// \code
1632/// void low_pass_filter(std::vector<double> &values, double cutoff) {
1633/// values.erase(std::remove_if(values.begin(), values.end(),
1634/// [=](double value) { return value > cutoff; });
1635/// }
1636/// \endcode
1637///
1638/// C++11 lambda expressions can capture local variables, either by copying
1639/// the values of those local variables at the time the function
1640/// object is constructed (not when it is called!) or by holding a
1641/// reference to the local variable. These captures can occur either
1642/// implicitly or can be written explicitly between the square
1643/// brackets ([...]) that start the lambda expression.
1644///
1645/// C++1y introduces a new form of "capture" called an init-capture that
1646/// includes an initializing expression (rather than capturing a variable),
1647/// and which can never occur implicitly.
1648class LambdaExpr final : public Expr,
1649 private llvm::TrailingObjects<LambdaExpr, Stmt *> {
1650 /// The source range that covers the lambda introducer ([...]).
1651 SourceRange IntroducerRange;
1652
1653 /// The source location of this lambda's capture-default ('=' or '&').
1654 SourceLocation CaptureDefaultLoc;
1655
1656 /// The number of captures.
1657 unsigned NumCaptures : 16;
1658
1659 /// The default capture kind, which is a value of type
1660 /// LambdaCaptureDefault.
1661 unsigned CaptureDefault : 2;
1662
1663 /// Whether this lambda had an explicit parameter list vs. an
1664 /// implicit (and empty) parameter list.
1665 unsigned ExplicitParams : 1;
1666
1667 /// Whether this lambda had the result type explicitly specified.
1668 unsigned ExplicitResultType : 1;
1669
1670 /// The location of the closing brace ('}') that completes
1671 /// the lambda.
1672 ///
1673 /// The location of the brace is also available by looking up the
1674 /// function call operator in the lambda class. However, it is
1675 /// stored here to improve the performance of getSourceRange(), and
1676 /// to avoid having to deserialize the function call operator from a
1677 /// module file just to determine the source range.
1678 SourceLocation ClosingBrace;
1679
1680 /// Construct a lambda expression.
1681 LambdaExpr(QualType T, SourceRange IntroducerRange,
1682 LambdaCaptureDefault CaptureDefault,
1683 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
1684 bool ExplicitParams, bool ExplicitResultType,
1685 ArrayRef<Expr *> CaptureInits, SourceLocation ClosingBrace,
1686 bool ContainsUnexpandedParameterPack);
1687
1688 /// Construct an empty lambda expression.
1689 LambdaExpr(EmptyShell Empty, unsigned NumCaptures)
1690 : Expr(LambdaExprClass, Empty), NumCaptures(NumCaptures),
1691 CaptureDefault(LCD_None), ExplicitParams(false),
1692 ExplicitResultType(false) {
1693 getStoredStmts()[NumCaptures] = nullptr;
1694 }
1695
1696 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
1697
1698 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
1699
1700public:
1701 friend class ASTStmtReader;
1702 friend class ASTStmtWriter;
1703 friend TrailingObjects;
1704
1705 /// Construct a new lambda expression.
1706 static LambdaExpr *
1707 Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange,
1708 LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc,
1709 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
1710 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1711 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack);
1712
1713 /// Construct a new lambda expression that will be deserialized from
1714 /// an external source.
1715 static LambdaExpr *CreateDeserialized(const ASTContext &C,
1716 unsigned NumCaptures);
1717
1718 /// Determine the default capture kind for this lambda.
1719 LambdaCaptureDefault getCaptureDefault() const {
1720 return static_cast<LambdaCaptureDefault>(CaptureDefault);
1721 }
1722
1723 /// Retrieve the location of this lambda's capture-default, if any.
1724 SourceLocation getCaptureDefaultLoc() const {
1725 return CaptureDefaultLoc;
1726 }
1727
1728 /// Determine whether one of this lambda's captures is an init-capture.
1729 bool isInitCapture(const LambdaCapture *Capture) const;
1730
1731 /// An iterator that walks over the captures of the lambda,
1732 /// both implicit and explicit.
1733 using capture_iterator = const LambdaCapture *;
1734
1735 /// An iterator over a range of lambda captures.
1736 using capture_range = llvm::iterator_range<capture_iterator>;
1737
1738 /// Retrieve this lambda's captures.
1739 capture_range captures() const;
1740
1741 /// Retrieve an iterator pointing to the first lambda capture.
1742 capture_iterator capture_begin() const;
1743
1744 /// Retrieve an iterator pointing past the end of the
1745 /// sequence of lambda captures.
1746 capture_iterator capture_end() const;
1747
1748 /// Determine the number of captures in this lambda.
1749 unsigned capture_size() const { return NumCaptures; }
1750
1751 /// Retrieve this lambda's explicit captures.
1752 capture_range explicit_captures() const;
1753
1754 /// Retrieve an iterator pointing to the first explicit
1755 /// lambda capture.
1756 capture_iterator explicit_capture_begin() const;
1757
1758 /// Retrieve an iterator pointing past the end of the sequence of
1759 /// explicit lambda captures.
1760 capture_iterator explicit_capture_end() const;
1761
1762 /// Retrieve this lambda's implicit captures.
1763 capture_range implicit_captures() const;
1764
1765 /// Retrieve an iterator pointing to the first implicit
1766 /// lambda capture.
1767 capture_iterator implicit_capture_begin() const;
1768
1769 /// Retrieve an iterator pointing past the end of the sequence of
1770 /// implicit lambda captures.
1771 capture_iterator implicit_capture_end() const;
1772
1773 /// Iterator that walks over the capture initialization
1774 /// arguments.
1775 using capture_init_iterator = Expr **;
1776
1777 /// Const iterator that walks over the capture initialization
1778 /// arguments.
1779 using const_capture_init_iterator = Expr *const *;
1780
1781 /// Retrieve the initialization expressions for this lambda's captures.
1782 llvm::iterator_range<capture_init_iterator> capture_inits() {
1783 return llvm::make_range(capture_init_begin(), capture_init_end());
1784 }
1785
1786 /// Retrieve the initialization expressions for this lambda's captures.
1787 llvm::iterator_range<const_capture_init_iterator> capture_inits() const {
1788 return llvm::make_range(capture_init_begin(), capture_init_end());
1789 }
1790
1791 /// Retrieve the first initialization argument for this
1792 /// lambda expression (which initializes the first capture field).
1793 capture_init_iterator capture_init_begin() {
1794 return reinterpret_cast<Expr **>(getStoredStmts());
1795 }
1796
1797 /// Retrieve the first initialization argument for this
1798 /// lambda expression (which initializes the first capture field).
1799 const_capture_init_iterator capture_init_begin() const {
1800 return reinterpret_cast<Expr *const *>(getStoredStmts());
1801 }
1802
1803 /// Retrieve the iterator pointing one past the last
1804 /// initialization argument for this lambda expression.
1805 capture_init_iterator capture_init_end() {
1806 return capture_init_begin() + NumCaptures;
1807 }
1808
1809 /// Retrieve the iterator pointing one past the last
1810 /// initialization argument for this lambda expression.
1811 const_capture_init_iterator capture_init_end() const {
1812 return capture_init_begin() + NumCaptures;
1813 }
1814
1815 /// Retrieve the source range covering the lambda introducer,
1816 /// which contains the explicit capture list surrounded by square
1817 /// brackets ([...]).
1818 SourceRange getIntroducerRange() const { return IntroducerRange; }
1819
1820 /// Retrieve the class that corresponds to the lambda.
1821 ///
1822 /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the
1823 /// captures in its fields and provides the various operations permitted
1824 /// on a lambda (copying, calling).
1825 CXXRecordDecl *getLambdaClass() const;
1826
1827 /// Retrieve the function call operator associated with this
1828 /// lambda expression.
1829 CXXMethodDecl *getCallOperator() const;
1830
1831 /// If this is a generic lambda expression, retrieve the template
1832 /// parameter list associated with it, or else return null.
1833 TemplateParameterList *getTemplateParameterList() const;
1834
1835 /// Whether this is a generic lambda.
1836 bool isGenericLambda() const { return getTemplateParameterList(); }
1837
1838 /// Retrieve the body of the lambda.
1839 CompoundStmt *getBody() const;
1840
1841 /// Determine whether the lambda is mutable, meaning that any
1842 /// captures values can be modified.
1843 bool isMutable() const;
1844
1845 /// Determine whether this lambda has an explicit parameter
1846 /// list vs. an implicit (empty) parameter list.
1847 bool hasExplicitParameters() const { return ExplicitParams; }
1848
1849 /// Whether this lambda had its result type explicitly specified.
1850 bool hasExplicitResultType() const { return ExplicitResultType; }
1851
1852 static bool classof(const Stmt *T) {
1853 return T->getStmtClass() == LambdaExprClass;
1854 }
1855
1856 SourceLocation getBeginLoc() const LLVM_READONLY {
1857 return IntroducerRange.getBegin();
1858 }
1859
1860 SourceLocation getEndLoc() const LLVM_READONLY { return ClosingBrace; }
1861
1862 child_range children() {
1863 // Includes initialization exprs plus body stmt
1864 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures + 1);
1865 }
1866};
1867
1868/// An expression "T()" which creates a value-initialized rvalue of type
1869/// T, which is a non-class type. See (C++98 [5.2.3p2]).
1870class CXXScalarValueInitExpr : public Expr {
1871 friend class ASTStmtReader;
1872
1873 TypeSourceInfo *TypeInfo;
1874
1875public:
1876 /// Create an explicitly-written scalar-value initialization
1877 /// expression.
1878 CXXScalarValueInitExpr(QualType Type, TypeSourceInfo *TypeInfo,
1879 SourceLocation RParenLoc)
1880 : Expr(CXXScalarValueInitExprClass, Type, VK_RValue, OK_Ordinary, false,
1881 false, Type->isInstantiationDependentType(),
1882 Type->containsUnexpandedParameterPack()),
1883 TypeInfo(TypeInfo) {
1884 CXXScalarValueInitExprBits.RParenLoc = RParenLoc;
1885 }
1886
1887 explicit CXXScalarValueInitExpr(EmptyShell Shell)
1888 : Expr(CXXScalarValueInitExprClass, Shell) {}
1889
1890 TypeSourceInfo *getTypeSourceInfo() const {
1891 return TypeInfo;
1892 }
1893
1894 SourceLocation getRParenLoc() const {
1895 return CXXScalarValueInitExprBits.RParenLoc;
1896 }
1897
1898 SourceLocation getBeginLoc() const LLVM_READONLY;
1899 SourceLocation getEndLoc() const { return getRParenLoc(); }
1900
1901 static bool classof(const Stmt *T) {
1902 return T->getStmtClass() == CXXScalarValueInitExprClass;
1903 }
1904
1905 // Iterators
1906 child_range children() {
1907 return child_range(child_iterator(), child_iterator());
1908 }
1909};
1910
1911/// Represents a new-expression for memory allocation and constructor
1912/// calls, e.g: "new CXXNewExpr(foo)".
1913class CXXNewExpr final
1914 : public Expr,
1915 private llvm::TrailingObjects<CXXNewExpr, Stmt *, SourceRange> {
1916 friend class ASTStmtReader;
1917 friend class ASTStmtWriter;
1918 friend TrailingObjects;
1919
1920 /// Points to the allocation function used.
1921 FunctionDecl *OperatorNew;
1922
1923 /// Points to the deallocation function used in case of error. May be null.
1924 FunctionDecl *OperatorDelete;
1925
1926 /// The allocated type-source information, as written in the source.
1927 TypeSourceInfo *AllocatedTypeInfo;
1928
1929 /// Range of the entire new expression.
1930 SourceRange Range;
1931
1932 /// Source-range of a paren-delimited initializer.
1933 SourceRange DirectInitRange;
1934
1935 // CXXNewExpr is followed by several optional trailing objects.
1936 // They are in order:
1937 //
1938 // * An optional "Stmt *" for the array size expression.
1939 // Present if and ony if isArray().
1940 //
1941 // * An optional "Stmt *" for the init expression.
1942 // Present if and only if hasInitializer().
1943 //
1944 // * An array of getNumPlacementArgs() "Stmt *" for the placement new
1945 // arguments, if any.
1946 //
1947 // * An optional SourceRange for the range covering the parenthesized type-id
1948 // if the allocated type was expressed as a parenthesized type-id.
1949 // Present if and only if isParenTypeId().
1950 unsigned arraySizeOffset() const { return 0; }
1951 unsigned initExprOffset() const { return arraySizeOffset() + isArray(); }
1952 unsigned placementNewArgsOffset() const {
1953 return initExprOffset() + hasInitializer();
1954 }
1955
1956 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1957 return isArray() + hasInitializer() + getNumPlacementArgs();
1958 }
1959
1960 unsigned numTrailingObjects(OverloadToken<SourceRange>) const {
1961 return isParenTypeId();
1962 }
1963
1964public:
1965 enum InitializationStyle {
1966 /// New-expression has no initializer as written.
1967 NoInit,
1968
1969 /// New-expression has a C++98 paren-delimited initializer.
1970 CallInit,
1971
1972 /// New-expression has a C++11 list-initializer.
1973 ListInit
1974 };
1975
1976private:
1977 /// Build a c++ new expression.
1978 CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
1979 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
1980 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
1981 SourceRange TypeIdParens, Expr *ArraySize,
1982 InitializationStyle InitializationStyle, Expr *Initializer,
1983 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
1984 SourceRange DirectInitRange);
1985
1986 /// Build an empty c++ new expression.
1987 CXXNewExpr(EmptyShell Empty, bool IsArray, unsigned NumPlacementArgs,
1988 bool IsParenTypeId);
1989
1990public:
1991 /// Create a c++ new expression.
1992 static CXXNewExpr *
1993 Create(const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew,
1994 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
1995 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
1996 SourceRange TypeIdParens, Expr *ArraySize,
1997 InitializationStyle InitializationStyle, Expr *Initializer,
1998 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
1999 SourceRange DirectInitRange);
2000
2001 /// Create an empty c++ new expression.
2002 static CXXNewExpr *CreateEmpty(const ASTContext &Ctx, bool IsArray,
2003 bool HasInit, unsigned NumPlacementArgs,
2004 bool IsParenTypeId);
2005
2006 QualType getAllocatedType() const {
2007 assert(getType()->isPointerType());
2008 return getType()->getAs<PointerType>()->getPointeeType();
2009 }
2010
2011 TypeSourceInfo *getAllocatedTypeSourceInfo() const {
2012 return AllocatedTypeInfo;
2013 }
2014
2015 /// True if the allocation result needs to be null-checked.
2016 ///
2017 /// C++11 [expr.new]p13:
2018 /// If the allocation function returns null, initialization shall
2019 /// not be done, the deallocation function shall not be called,
2020 /// and the value of the new-expression shall be null.
2021 ///
2022 /// C++ DR1748:
2023 /// If the allocation function is a reserved placement allocation
2024 /// function that returns null, the behavior is undefined.
2025 ///
2026 /// An allocation function is not allowed to return null unless it
2027 /// has a non-throwing exception-specification. The '03 rule is
2028 /// identical except that the definition of a non-throwing
2029 /// exception specification is just "is it throw()?".
2030 bool shouldNullCheckAllocation() const;
2031
2032 FunctionDecl *getOperatorNew() const { return OperatorNew; }
2033 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; }
2034 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2035 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; }
2036
2037 bool isArray() const { return CXXNewExprBits.IsArray; }
2038
2039 Expr *getArraySize() {
2040 return isArray()
2041 ? cast<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()])
2042 : nullptr;
2043 }
2044 const Expr *getArraySize() const {
2045 return isArray()
2046 ? cast<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()])
2047 : nullptr;
2048 }
2049
2050 unsigned getNumPlacementArgs() const {
2051 return CXXNewExprBits.NumPlacementArgs;
2052 }
2053
2054 Expr **getPlacementArgs() {
2055 return reinterpret_cast<Expr **>(getTrailingObjects<Stmt *>() +
2056 placementNewArgsOffset());
2057 }
2058
2059 Expr *getPlacementArg(unsigned I) {
2060 assert((I < getNumPlacementArgs()) && "Index out of range!");
2061 return getPlacementArgs()[I];
2062 }
2063 const Expr *getPlacementArg(unsigned I) const {
2064 return const_cast<CXXNewExpr *>(this)->getPlacementArg(I);
2065 }
2066
2067 bool isParenTypeId() const { return CXXNewExprBits.IsParenTypeId; }
2068 SourceRange getTypeIdParens() const {
2069 return isParenTypeId() ? getTrailingObjects<SourceRange>()[0]
2070 : SourceRange();
2071 }
2072
2073 bool isGlobalNew() const { return CXXNewExprBits.IsGlobalNew; }
2074
2075 /// Whether this new-expression has any initializer at all.
2076 bool hasInitializer() const {
2077 return CXXNewExprBits.StoredInitializationStyle > 0;
2078 }
2079
2080 /// The kind of initializer this new-expression has.
2081 InitializationStyle getInitializationStyle() const {
2082 if (CXXNewExprBits.StoredInitializationStyle == 0)
2083 return NoInit;
2084 return static_cast<InitializationStyle>(
2085 CXXNewExprBits.StoredInitializationStyle - 1);
2086 }
2087
2088 /// The initializer of this new-expression.
2089 Expr *getInitializer() {
2090 return hasInitializer()
2091 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
2092 : nullptr;
2093 }
2094 const Expr *getInitializer() const {
2095 return hasInitializer()
2096 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
2097 : nullptr;
2098 }
2099
2100 /// Returns the CXXConstructExpr from this new-expression, or null.
2101 const CXXConstructExpr *getConstructExpr() const {
2102 return dyn_cast_or_null<CXXConstructExpr>(getInitializer());
2103 }
2104
2105 /// Indicates whether the required alignment should be implicitly passed to
2106 /// the allocation function.
2107 bool passAlignment() const { return CXXNewExprBits.ShouldPassAlignment; }
2108
2109 /// Answers whether the usual array deallocation function for the
2110 /// allocated type expects the size of the allocation as a
2111 /// parameter.
2112 bool doesUsualArrayDeleteWantSize() const {
2113 return CXXNewExprBits.UsualArrayDeleteWantsSize;
2114 }
2115
2116 using arg_iterator = ExprIterator;
2117 using const_arg_iterator = ConstExprIterator;
2118
2119 llvm::iterator_range<arg_iterator> placement_arguments() {
2120 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2121 }
2122
2123 llvm::iterator_range<const_arg_iterator> placement_arguments() const {
2124 return llvm::make_range(placement_arg_begin(), placement_arg_end());
2125 }
2126
2127 arg_iterator placement_arg_begin() {
2128 return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
2129 }
2130 arg_iterator placement_arg_end() {
2131 return placement_arg_begin() + getNumPlacementArgs();
2132 }
2133 const_arg_iterator placement_arg_begin() const {
2134 return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
2135 }
2136 const_arg_iterator placement_arg_end() const {
2137 return placement_arg_begin() + getNumPlacementArgs();
2138 }
2139
2140 using raw_arg_iterator = Stmt **;
2141
2142 raw_arg_iterator raw_arg_begin() { return getTrailingObjects<Stmt *>(); }
2143 raw_arg_iterator raw_arg_end() {
2144 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
2145 }
2146 const_arg_iterator raw_arg_begin() const {
2147 return getTrailingObjects<Stmt *>();
2148 }
2149 const_arg_iterator raw_arg_end() const {
2150 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
2151 }
2152
2153 SourceLocation getBeginLoc() const { return Range.getBegin(); }
2154 SourceLocation getEndLoc() const { return Range.getEnd(); }
2155
2156 SourceRange getDirectInitRange() const { return DirectInitRange; }
2157 SourceRange getSourceRange() const { return Range; }
2158
2159 static bool classof(const Stmt *T) {
2160 return T->getStmtClass() == CXXNewExprClass;
2161 }
2162
2163 // Iterators
2164 child_range children() { return child_range(raw_arg_begin(), raw_arg_end()); }
2165};
2166
2167/// Represents a \c delete expression for memory deallocation and
2168/// destructor calls, e.g. "delete[] pArray".
2169class CXXDeleteExpr : public Expr {
2170 friend class ASTStmtReader;
2171
2172 /// Points to the operator delete overload that is used. Could be a member.
2173 FunctionDecl *OperatorDelete = nullptr;
2174
2175 /// The pointer expression to be deleted.
2176 Stmt *Argument = nullptr;
2177
2178public:
2179 CXXDeleteExpr(QualType Ty, bool GlobalDelete, bool ArrayForm,
2180 bool ArrayFormAsWritten, bool UsualArrayDeleteWantsSize,
2181 FunctionDecl *OperatorDelete, Expr *Arg, SourceLocation Loc)
2182 : Expr(CXXDeleteExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
2183 Arg->isInstantiationDependent(),
2184 Arg->containsUnexpandedParameterPack()),
2185 OperatorDelete(OperatorDelete), Argument(Arg) {
2186 CXXDeleteExprBits.GlobalDelete = GlobalDelete;
2187 CXXDeleteExprBits.ArrayForm = ArrayForm;
2188 CXXDeleteExprBits.ArrayFormAsWritten = ArrayFormAsWritten;
2189 CXXDeleteExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
2190 CXXDeleteExprBits.Loc = Loc;
2191 }
2192
2193 explicit CXXDeleteExpr(EmptyShell Shell) : Expr(CXXDeleteExprClass, Shell) {}
2194
2195 bool isGlobalDelete() const { return CXXDeleteExprBits.GlobalDelete; }
2196 bool isArrayForm() const { return CXXDeleteExprBits.ArrayForm; }
2197 bool isArrayFormAsWritten() const {
2198 return CXXDeleteExprBits.ArrayFormAsWritten;
2199 }
2200
2201 /// Answers whether the usual array deallocation function for the
2202 /// allocated type expects the size of the allocation as a
2203 /// parameter. This can be true even if the actual deallocation
2204 /// function that we're using doesn't want a size.
2205 bool doesUsualArrayDeleteWantSize() const {
2206 return CXXDeleteExprBits.UsualArrayDeleteWantsSize;
2207 }
2208
2209 FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2210
2211 Expr *getArgument() { return cast<Expr>(Argument); }
2212 const Expr *getArgument() const { return cast<Expr>(Argument); }
2213
2214 /// Retrieve the type being destroyed.
2215 ///
2216 /// If the type being destroyed is a dependent type which may or may not
2217 /// be a pointer, return an invalid type.
2218 QualType getDestroyedType() const;
2219
2220 SourceLocation getBeginLoc() const { return CXXDeleteExprBits.Loc; }
2221 SourceLocation getEndLoc() const LLVM_READONLY {
2222 return Argument->getEndLoc();
2223 }
2224
2225 static bool classof(const Stmt *T) {
2226 return T->getStmtClass() == CXXDeleteExprClass;
2227 }
2228
2229 // Iterators
2230 child_range children() { return child_range(&Argument, &Argument + 1); }
2231};
2232
2233/// Stores the type being destroyed by a pseudo-destructor expression.
2234class PseudoDestructorTypeStorage {
2235 /// Either the type source information or the name of the type, if
2236 /// it couldn't be resolved due to type-dependence.
2237 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type;
2238
2239 /// The starting source location of the pseudo-destructor type.
2240 SourceLocation Location;
2241
2242public:
2243 PseudoDestructorTypeStorage() = default;
2244
2245 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc)
2246 : Type(II), Location(Loc) {}
2247
2248 PseudoDestructorTypeStorage(TypeSourceInfo *Info);
2249
2250 TypeSourceInfo *getTypeSourceInfo() const {
2251 return Type.dyn_cast<TypeSourceInfo *>();
2252 }
2253
2254 IdentifierInfo *getIdentifier() const {
2255 return Type.dyn_cast<IdentifierInfo *>();
2256 }
2257
2258 SourceLocation getLocation() const { return Location; }
2259};
2260
2261/// Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
2262///
2263/// A pseudo-destructor is an expression that looks like a member access to a
2264/// destructor of a scalar type, except that scalar types don't have
2265/// destructors. For example:
2266///
2267/// \code
2268/// typedef int T;
2269/// void f(int *p) {
2270/// p->T::~T();
2271/// }
2272/// \endcode
2273///
2274/// Pseudo-destructors typically occur when instantiating templates such as:
2275///
2276/// \code
2277/// template<typename T>
2278/// void destroy(T* ptr) {
2279/// ptr->T::~T();
2280/// }
2281/// \endcode
2282///
2283/// for scalar types. A pseudo-destructor expression has no run-time semantics
2284/// beyond evaluating the base expression.
2285class CXXPseudoDestructorExpr : public Expr {
2286 friend class ASTStmtReader;
2287
2288 /// The base expression (that is being destroyed).
2289 Stmt *Base = nullptr;
2290
2291 /// Whether the operator was an arrow ('->'); otherwise, it was a
2292 /// period ('.').
2293 bool IsArrow : 1;
2294
2295 /// The location of the '.' or '->' operator.
2296 SourceLocation OperatorLoc;
2297
2298 /// The nested-name-specifier that follows the operator, if present.
2299 NestedNameSpecifierLoc QualifierLoc;
2300
2301 /// The type that precedes the '::' in a qualified pseudo-destructor
2302 /// expression.
2303 TypeSourceInfo *ScopeType = nullptr;
2304
2305 /// The location of the '::' in a qualified pseudo-destructor
2306 /// expression.
2307 SourceLocation ColonColonLoc;
2308
2309 /// The location of the '~'.
2310 SourceLocation TildeLoc;
2311
2312 /// The type being destroyed, or its name if we were unable to
2313 /// resolve the name.
2314 PseudoDestructorTypeStorage DestroyedType;
2315
2316public:
2317 CXXPseudoDestructorExpr(const ASTContext &Context,
2318 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
2319 NestedNameSpecifierLoc QualifierLoc,
2320 TypeSourceInfo *ScopeType,
2321 SourceLocation ColonColonLoc,
2322 SourceLocation TildeLoc,
2323 PseudoDestructorTypeStorage DestroyedType);
2324
2325 explicit CXXPseudoDestructorExpr(EmptyShell Shell)
2326 : Expr(CXXPseudoDestructorExprClass, Shell), IsArrow(false) {}
2327
2328 Expr *getBase() const { return cast<Expr>(Base); }
2329
2330 /// Determines whether this member expression actually had
2331 /// a C++ nested-name-specifier prior to the name of the member, e.g.,
2332 /// x->Base::foo.
2333 bool hasQualifier() const { return QualifierLoc.hasQualifier(); }
2334
2335 /// Retrieves the nested-name-specifier that qualifies the type name,
2336 /// with source-location information.
2337 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2338
2339 /// If the member name was qualified, retrieves the
2340 /// nested-name-specifier that precedes the member name. Otherwise, returns
2341 /// null.
2342 NestedNameSpecifier *getQualifier() const {
2343 return QualifierLoc.getNestedNameSpecifier();
2344 }
2345
2346 /// Determine whether this pseudo-destructor expression was written
2347 /// using an '->' (otherwise, it used a '.').
2348 bool isArrow() const { return IsArrow; }
2349
2350 /// Retrieve the location of the '.' or '->' operator.
2351 SourceLocation getOperatorLoc() const { return OperatorLoc; }
2352
2353 /// Retrieve the scope type in a qualified pseudo-destructor
2354 /// expression.
2355 ///
2356 /// Pseudo-destructor expressions can have extra qualification within them
2357 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T().
2358 /// Here, if the object type of the expression is (or may be) a scalar type,
2359 /// \p T may also be a scalar type and, therefore, cannot be part of a
2360 /// nested-name-specifier. It is stored as the "scope type" of the pseudo-
2361 /// destructor expression.
2362 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; }
2363
2364 /// Retrieve the location of the '::' in a qualified pseudo-destructor
2365 /// expression.
2366 SourceLocation getColonColonLoc() const { return ColonColonLoc; }
2367
2368 /// Retrieve the location of the '~'.
2369 SourceLocation getTildeLoc() const { return TildeLoc; }
2370
2371 /// Retrieve the source location information for the type
2372 /// being destroyed.
2373 ///
2374 /// This type-source information is available for non-dependent
2375 /// pseudo-destructor expressions and some dependent pseudo-destructor
2376 /// expressions. Returns null if we only have the identifier for a
2377 /// dependent pseudo-destructor expression.
2378 TypeSourceInfo *getDestroyedTypeInfo() const {
2379 return DestroyedType.getTypeSourceInfo();
2380 }
2381
2382 /// In a dependent pseudo-destructor expression for which we do not
2383 /// have full type information on the destroyed type, provides the name
2384 /// of the destroyed type.
2385 IdentifierInfo *getDestroyedTypeIdentifier() const {
2386 return DestroyedType.getIdentifier();
2387 }
2388
2389 /// Retrieve the type being destroyed.
2390 QualType getDestroyedType() const;
2391
2392 /// Retrieve the starting location of the type being destroyed.
2393 SourceLocation getDestroyedTypeLoc() const {
2394 return DestroyedType.getLocation();
2395 }
2396
2397 /// Set the name of destroyed type for a dependent pseudo-destructor
2398 /// expression.
2399 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) {
2400 DestroyedType = PseudoDestructorTypeStorage(II, Loc);
2401 }
2402
2403 /// Set the destroyed type.
2404 void setDestroyedType(TypeSourceInfo *Info) {
2405 DestroyedType = PseudoDestructorTypeStorage(Info);
2406 }
2407
2408 SourceLocation getBeginLoc() const LLVM_READONLY {
2409 return Base->getBeginLoc();
2410 }
2411 SourceLocation getEndLoc() const LLVM_READONLY;
2412
2413 static bool classof(const Stmt *T) {
2414 return T->getStmtClass() == CXXPseudoDestructorExprClass;
2415 }
2416
2417 // Iterators
2418 child_range children() { return child_range(&Base, &Base + 1); }
2419};
2420
2421/// A type trait used in the implementation of various C++11 and
2422/// Library TR1 trait templates.
2423///
2424/// \code
2425/// __is_pod(int) == true
2426/// __is_enum(std::string) == false
2427/// __is_trivially_constructible(vector<int>, int*, int*)
2428/// \endcode
2429class TypeTraitExpr final
2430 : public Expr,
2431 private llvm::TrailingObjects<TypeTraitExpr, TypeSourceInfo *> {
2432 /// The location of the type trait keyword.
2433 SourceLocation Loc;
2434
2435 /// The location of the closing parenthesis.
2436 SourceLocation RParenLoc;
2437
2438 // Note: The TypeSourceInfos for the arguments are allocated after the
2439 // TypeTraitExpr.
2440
2441 TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
2442 ArrayRef<TypeSourceInfo *> Args,
2443 SourceLocation RParenLoc,
2444 bool Value);
2445
2446 TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) {}
2447
2448 size_t numTrailingObjects(OverloadToken<TypeSourceInfo *>) const {
2449 return getNumArgs();
2450 }
2451
2452public:
2453 friend class ASTStmtReader;
2454 friend class ASTStmtWriter;
2455 friend TrailingObjects;
2456
2457 /// Create a new type trait expression.
2458 static TypeTraitExpr *Create(const ASTContext &C, QualType T,
2459 SourceLocation Loc, TypeTrait Kind,
2460 ArrayRef<TypeSourceInfo *> Args,
2461 SourceLocation RParenLoc,
2462 bool Value);
2463
2464 static TypeTraitExpr *CreateDeserialized(const ASTContext &C,
2465 unsigned NumArgs);
2466
2467 /// Determine which type trait this expression uses.
2468 TypeTrait getTrait() const {
2469 return static_cast<TypeTrait>(TypeTraitExprBits.Kind);
2470 }
2471
2472 bool getValue() const {
2473 assert(!isValueDependent());
2474 return TypeTraitExprBits.Value;
2475 }
2476
2477 /// Determine the number of arguments to this type trait.
2478 unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; }
2479
2480 /// Retrieve the Ith argument.
2481 TypeSourceInfo *getArg(unsigned I) const {
2482 assert(I < getNumArgs() && "Argument out-of-range");
2483 return getArgs()[I];
2484 }
2485
2486 /// Retrieve the argument types.
2487 ArrayRef<TypeSourceInfo *> getArgs() const {
2488 return llvm::makeArrayRef(getTrailingObjects<TypeSourceInfo *>(),
2489 getNumArgs());
2490 }
2491
2492 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2493 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
2494
2495 static bool classof(const Stmt *T) {
2496 return T->getStmtClass() == TypeTraitExprClass;
2497 }
2498
2499 // Iterators
2500 child_range children() {
2501 return child_range(child_iterator(), child_iterator());
2502 }
2503};
2504
2505/// An Embarcadero array type trait, as used in the implementation of
2506/// __array_rank and __array_extent.
2507///
2508/// Example:
2509/// \code
2510/// __array_rank(int[10][20]) == 2
2511/// __array_extent(int, 1) == 20
2512/// \endcode
2513class ArrayTypeTraitExpr : public Expr {
2514 /// The trait. An ArrayTypeTrait enum in MSVC compat unsigned.
2515 unsigned ATT : 2;
2516
2517 /// The value of the type trait. Unspecified if dependent.
2518 uint64_t Value = 0;
2519
2520 /// The array dimension being queried, or -1 if not used.
2521 Expr *Dimension;
2522
2523 /// The location of the type trait keyword.
2524 SourceLocation Loc;
2525
2526 /// The location of the closing paren.
2527 SourceLocation RParen;
2528
2529 /// The type being queried.
2530 TypeSourceInfo *QueriedType = nullptr;
2531
2532public:
2533 friend class ASTStmtReader;
2534
2535 ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att,
2536 TypeSourceInfo *queried, uint64_t value,
2537 Expr *dimension, SourceLocation rparen, QualType ty)
2538 : Expr(ArrayTypeTraitExprClass, ty, VK_RValue, OK_Ordinary,
2539 false, queried->getType()->isDependentType(),
2540 (queried->getType()->isInstantiationDependentType() ||
2541 (dimension && dimension->isInstantiationDependent())),
2542 queried->getType()->containsUnexpandedParameterPack()),
2543 ATT(att), Value(value), Dimension(dimension),
2544 Loc(loc), RParen(rparen), QueriedType(queried) {}
2545
2546 explicit ArrayTypeTraitExpr(EmptyShell Empty)
2547 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0) {}
2548
2549 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2550 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
2551
2552 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); }
2553
2554 QualType getQueriedType() const { return QueriedType->getType(); }
2555
2556 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; }
2557
2558 uint64_t getValue() const { assert(!isTypeDependent()); return Value; }
2559
2560 Expr *getDimensionExpression() const { return Dimension; }
2561
2562 static bool classof(const Stmt *T) {
2563 return T->getStmtClass() == ArrayTypeTraitExprClass;
2564 }
2565
2566 // Iterators
2567 child_range children() {
2568 return child_range(child_iterator(), child_iterator());
2569 }
2570};
2571
2572/// An expression trait intrinsic.
2573///
2574/// Example:
2575/// \code
2576/// __is_lvalue_expr(std::cout) == true
2577/// __is_lvalue_expr(1) == false
2578/// \endcode
2579class ExpressionTraitExpr : public Expr {
2580 /// The trait. A ExpressionTrait enum in MSVC compatible unsigned.
2581 unsigned ET : 31;
2582
2583 /// The value of the type trait. Unspecified if dependent.
2584 unsigned Value : 1;
2585
2586 /// The location of the type trait keyword.
2587 SourceLocation Loc;
2588
2589 /// The location of the closing paren.
2590 SourceLocation RParen;
2591
2592 /// The expression being queried.
2593 Expr* QueriedExpression = nullptr;
2594
2595public:
2596 friend class ASTStmtReader;
2597
2598 ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et,
2599 Expr *queried, bool value,
2600 SourceLocation rparen, QualType resultType)
2601 : Expr(ExpressionTraitExprClass, resultType, VK_RValue, OK_Ordinary,
2602 false, // Not type-dependent
2603 // Value-dependent if the argument is type-dependent.
2604 queried->isTypeDependent(),
2605 queried->isInstantiationDependent(),
2606 queried->containsUnexpandedParameterPack()),
2607 ET(et), Value(value), Loc(loc), RParen(rparen),
2608 QueriedExpression(queried) {}
2609
2610 explicit ExpressionTraitExpr(EmptyShell Empty)
2611 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false) {}
2612
2613 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
2614 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
2615
2616 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); }
2617
2618 Expr *getQueriedExpression() const { return QueriedExpression; }
2619
2620 bool getValue() const { return Value; }
2621
2622 static bool classof(const Stmt *T) {
2623 return T->getStmtClass() == ExpressionTraitExprClass;
2624 }
2625
2626 // Iterators
2627 child_range children() {
2628 return child_range(child_iterator(), child_iterator());
2629 }
2630};
2631
2632/// A reference to an overloaded function set, either an
2633/// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr.
2634class OverloadExpr : public Expr {
2635 friend class ASTStmtReader;
2636 friend class ASTStmtWriter;
2637
2638 /// The common name of these declarations.
2639 DeclarationNameInfo NameInfo;
2640
2641 /// The nested-name-specifier that qualifies the name, if any.
2642 NestedNameSpecifierLoc QualifierLoc;
2643
2644protected:
2645 OverloadExpr(StmtClass SC, const ASTContext &Context,
2646 NestedNameSpecifierLoc QualifierLoc,
2647 SourceLocation TemplateKWLoc,
2648 const DeclarationNameInfo &NameInfo,
2649 const TemplateArgumentListInfo *TemplateArgs,
2650 UnresolvedSetIterator Begin, UnresolvedSetIterator End,
2651 bool KnownDependent, bool KnownInstantiationDependent,
2652 bool KnownContainsUnexpandedParameterPack);
2653
2654 OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
2655 bool HasTemplateKWAndArgsInfo);
2656
2657 /// Return the results. Defined after UnresolvedMemberExpr.
2658 inline DeclAccessPair *getTrailingResults();
2659 const DeclAccessPair *getTrailingResults() const {
2660 return const_cast<OverloadExpr *>(this)->getTrailingResults();
2661 }
2662
2663 /// Return the optional template keyword and arguments info.
2664 /// Defined after UnresolvedMemberExpr.
2665 inline ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo();
2666 const ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo() const {
2667 return const_cast<OverloadExpr *>(this)
2668 ->getTrailingASTTemplateKWAndArgsInfo();
2669 }
2670
2671 /// Return the optional template arguments. Defined after
2672 /// UnresolvedMemberExpr.
2673 inline TemplateArgumentLoc *getTrailingTemplateArgumentLoc();
2674 const TemplateArgumentLoc *getTrailingTemplateArgumentLoc() const {
2675 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
2676 }
2677
2678 bool hasTemplateKWAndArgsInfo() const {
2679 return OverloadExprBits.HasTemplateKWAndArgsInfo;
2680 }
2681
2682public:
2683 struct FindResult {
2684 OverloadExpr *Expression;
2685 bool IsAddressOfOperand;
2686 bool HasFormOfMemberPointer;
2687 };
2688
2689 /// Finds the overloaded expression in the given expression \p E of
2690 /// OverloadTy.
2691 ///
2692 /// \return the expression (which must be there) and true if it has
2693 /// the particular form of a member pointer expression
2694 static FindResult find(Expr *E) {
2695 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload));
2696
2697 FindResult Result;
2698
2699 E = E->IgnoreParens();
2700 if (isa<UnaryOperator>(E)) {
2701 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf);
2702 E = cast<UnaryOperator>(E)->getSubExpr();
2703 auto *Ovl = cast<OverloadExpr>(E->IgnoreParens());
2704
2705 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier());
2706 Result.IsAddressOfOperand = true;
2707 Result.Expression = Ovl;
2708 } else {
2709 Result.HasFormOfMemberPointer = false;
2710 Result.IsAddressOfOperand = false;
2711 Result.Expression = cast<OverloadExpr>(E);
2712 }
2713
2714 return Result;
2715 }
2716
2717 /// Gets the naming class of this lookup, if any.
2718 /// Defined after UnresolvedMemberExpr.
2719 inline CXXRecordDecl *getNamingClass();
2720 const CXXRecordDecl *getNamingClass() const {
2721 return const_cast<OverloadExpr *>(this)->getNamingClass();
2722 }
2723
2724 using decls_iterator = UnresolvedSetImpl::iterator;
2725
2726 decls_iterator decls_begin() const {
2727 return UnresolvedSetIterator(getTrailingResults());
2728 }
2729 decls_iterator decls_end() const {
2730 return UnresolvedSetIterator(getTrailingResults() + getNumDecls());
2731 }
2732 llvm::iterator_range<decls_iterator> decls() const {
2733 return llvm::make_range(decls_begin(), decls_end());
2734 }
2735
2736 /// Gets the number of declarations in the unresolved set.
2737 unsigned getNumDecls() const { return OverloadExprBits.NumResults; }
2738
2739 /// Gets the full name info.
2740 const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2741
2742 /// Gets the name looked up.
2743 DeclarationName getName() const { return NameInfo.getName(); }
2744
2745 /// Gets the location of the name.
2746 SourceLocation getNameLoc() const { return NameInfo.getLoc(); }
2747
2748 /// Fetches the nested-name qualifier, if one was given.
2749 NestedNameSpecifier *getQualifier() const {
2750 return QualifierLoc.getNestedNameSpecifier();
2751 }
2752
2753 /// Fetches the nested-name qualifier with source-location
2754 /// information, if one was given.
2755 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2756
2757 /// Retrieve the location of the template keyword preceding
2758 /// this name, if any.
2759 SourceLocation getTemplateKeywordLoc() const {
2760 if (!hasTemplateKWAndArgsInfo())
2761 return SourceLocation();
2762 return getTrailingASTTemplateKWAndArgsInfo()->TemplateKWLoc;
2763 }
2764
2765 /// Retrieve the location of the left angle bracket starting the
2766 /// explicit template argument list following the name, if any.
2767 SourceLocation getLAngleLoc() const {
2768 if (!hasTemplateKWAndArgsInfo())
2769 return SourceLocation();
2770 return getTrailingASTTemplateKWAndArgsInfo()->LAngleLoc;
2771 }
2772
2773 /// Retrieve the location of the right angle bracket ending the
2774 /// explicit template argument list following the name, if any.
2775 SourceLocation getRAngleLoc() const {
2776 if (!hasTemplateKWAndArgsInfo())
2777 return SourceLocation();
2778 return getTrailingASTTemplateKWAndArgsInfo()->RAngleLoc;
2779 }
2780
2781 /// Determines whether the name was preceded by the template keyword.
2782 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
2783
2784 /// Determines whether this expression had explicit template arguments.
2785 bool hasExplicitTempl