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