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