ExprCXX.h revision 1c63b9c15d48cb8c833a4b2d6fd6c496c0766e88
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//  This file defines the Expr interface and subclasses for C++ expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_EXPRCXX_H
15#define LLVM_CLANG_AST_EXPRCXX_H
16
17#include "clang/Basic/TypeTraits.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/UnresolvedSet.h"
20#include "clang/AST/TemplateBase.h"
21
22namespace clang {
23
24  class CXXConstructorDecl;
25  class CXXDestructorDecl;
26  class CXXMethodDecl;
27  class CXXTemporary;
28  class TemplateArgumentListInfo;
29
30//===--------------------------------------------------------------------===//
31// C++ Expressions.
32//===--------------------------------------------------------------------===//
33
34/// \brief A call to an overloaded operator written using operator
35/// syntax.
36///
37/// Represents a call to an overloaded operator written using operator
38/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a
39/// normal call, this AST node provides better information about the
40/// syntactic representation of the call.
41///
42/// In a C++ template, this expression node kind will be used whenever
43/// any of the arguments are type-dependent. In this case, the
44/// function itself will be a (possibly empty) set of functions and
45/// function templates that were found by name lookup at template
46/// definition time.
47class CXXOperatorCallExpr : public CallExpr {
48  /// \brief The overloaded operator.
49  OverloadedOperatorKind Operator;
50
51public:
52  CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn,
53                      Expr **args, unsigned numargs, QualType t,
54                      SourceLocation operatorloc)
55    : CallExpr(C, CXXOperatorCallExprClass, fn, args, numargs, t, operatorloc),
56      Operator(Op) {}
57  explicit CXXOperatorCallExpr(ASTContext& C, EmptyShell Empty) :
58    CallExpr(C, CXXOperatorCallExprClass, Empty) { }
59
60
61  /// getOperator - Returns the kind of overloaded operator that this
62  /// expression refers to.
63  OverloadedOperatorKind getOperator() const { return Operator; }
64  void setOperator(OverloadedOperatorKind Kind) { Operator = Kind; }
65
66  /// getOperatorLoc - Returns the location of the operator symbol in
67  /// the expression. When @c getOperator()==OO_Call, this is the
68  /// location of the right parentheses; when @c
69  /// getOperator()==OO_Subscript, this is the location of the right
70  /// bracket.
71  SourceLocation getOperatorLoc() const { return getRParenLoc(); }
72
73  virtual SourceRange getSourceRange() const;
74
75  static bool classof(const Stmt *T) {
76    return T->getStmtClass() == CXXOperatorCallExprClass;
77  }
78  static bool classof(const CXXOperatorCallExpr *) { return true; }
79};
80
81/// CXXMemberCallExpr - Represents a call to a member function that
82/// may be written either with member call syntax (e.g., "obj.func()"
83/// or "objptr->func()") or with normal function-call syntax
84/// ("func()") within a member function that ends up calling a member
85/// function. The callee in either case is a MemberExpr that contains
86/// both the object argument and the member function, while the
87/// arguments are the arguments within the parentheses (not including
88/// the object argument).
89class CXXMemberCallExpr : public CallExpr {
90public:
91  CXXMemberCallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
92                    QualType t, SourceLocation rparenloc)
93    : CallExpr(C, CXXMemberCallExprClass, fn, args, numargs, t, rparenloc) {}
94
95  /// getImplicitObjectArgument - Retrieves the implicit object
96  /// argument for the member call. For example, in "x.f(5)", this
97  /// operation would return "x".
98  Expr *getImplicitObjectArgument();
99
100  virtual SourceRange getSourceRange() const;
101
102  static bool classof(const Stmt *T) {
103    return T->getStmtClass() == CXXMemberCallExprClass;
104  }
105  static bool classof(const CXXMemberCallExpr *) { return true; }
106};
107
108/// CXXNamedCastExpr - Abstract class common to all of the C++ "named"
109/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c
110/// const_cast.
111///
112/// This abstract class is inherited by all of the classes
113/// representing "named" casts, e.g., CXXStaticCastExpr,
114/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr.
115class CXXNamedCastExpr : public ExplicitCastExpr {
116private:
117  SourceLocation Loc; // the location of the casting op
118
119protected:
120  CXXNamedCastExpr(StmtClass SC, QualType ty, CastKind kind, Expr *op,
121                   CXXBaseSpecifierArray BasePath, TypeSourceInfo *writtenTy,
122                   SourceLocation l)
123    : ExplicitCastExpr(SC, ty, kind, op, BasePath, writtenTy), Loc(l) {}
124
125  explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell)
126    : ExplicitCastExpr(SC, Shell) { }
127
128public:
129  const char *getCastName() const;
130
131  /// \brief Retrieve the location of the cast operator keyword, e.g.,
132  /// "static_cast".
133  SourceLocation getOperatorLoc() const { return Loc; }
134  void setOperatorLoc(SourceLocation L) { Loc = L; }
135
136  virtual SourceRange getSourceRange() const {
137    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
138  }
139  static bool classof(const Stmt *T) {
140    switch (T->getStmtClass()) {
141    case CXXStaticCastExprClass:
142    case CXXDynamicCastExprClass:
143    case CXXReinterpretCastExprClass:
144    case CXXConstCastExprClass:
145      return true;
146    default:
147      return false;
148    }
149  }
150  static bool classof(const CXXNamedCastExpr *) { return true; }
151};
152
153/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]).
154///
155/// This expression node represents a C++ static cast, e.g.,
156/// @c static_cast<int>(1.0).
157class CXXStaticCastExpr : public CXXNamedCastExpr {
158public:
159  CXXStaticCastExpr(QualType ty, CastKind kind, Expr *op,
160                    CXXBaseSpecifierArray BasePath, TypeSourceInfo *writtenTy,
161                    SourceLocation l)
162    : CXXNamedCastExpr(CXXStaticCastExprClass, ty, kind, op, BasePath, writtenTy, l) {}
163
164  explicit CXXStaticCastExpr(EmptyShell Empty)
165    : CXXNamedCastExpr(CXXStaticCastExprClass, Empty) { }
166
167  static bool classof(const Stmt *T) {
168    return T->getStmtClass() == CXXStaticCastExprClass;
169  }
170  static bool classof(const CXXStaticCastExpr *) { return true; }
171};
172
173/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression
174/// (C++ [expr.dynamic.cast]), which may perform a run-time check to
175/// determine how to perform the type cast.
176///
177/// This expression node represents a dynamic cast, e.g.,
178/// @c dynamic_cast<Derived*>(BasePtr).
179class CXXDynamicCastExpr : public CXXNamedCastExpr {
180public:
181  CXXDynamicCastExpr(QualType ty, CastKind kind, Expr *op,
182                     CXXBaseSpecifierArray BasePath, TypeSourceInfo *writtenTy,
183                     SourceLocation l)
184    : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, kind, op, BasePath,
185                       writtenTy, l) {}
186
187  explicit CXXDynamicCastExpr(EmptyShell Empty)
188    : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty) { }
189
190  static bool classof(const Stmt *T) {
191    return T->getStmtClass() == CXXDynamicCastExprClass;
192  }
193  static bool classof(const CXXDynamicCastExpr *) { return true; }
194};
195
196/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++
197/// [expr.reinterpret.cast]), which provides a differently-typed view
198/// of a value but performs no actual work at run time.
199///
200/// This expression node represents a reinterpret cast, e.g.,
201/// @c reinterpret_cast<int>(VoidPtr).
202class CXXReinterpretCastExpr : public CXXNamedCastExpr {
203public:
204  CXXReinterpretCastExpr(QualType ty, CastKind kind, Expr *op,
205                         CXXBaseSpecifierArray BasePath,
206                         TypeSourceInfo *writtenTy, SourceLocation l)
207    : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, kind, op, BasePath,
208                       writtenTy, l) {}
209
210  explicit CXXReinterpretCastExpr(EmptyShell Empty)
211    : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty) { }
212
213  static bool classof(const Stmt *T) {
214    return T->getStmtClass() == CXXReinterpretCastExprClass;
215  }
216  static bool classof(const CXXReinterpretCastExpr *) { return true; }
217};
218
219/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]),
220/// which can remove type qualifiers but does not change the underlying value.
221///
222/// This expression node represents a const cast, e.g.,
223/// @c const_cast<char*>(PtrToConstChar).
224class CXXConstCastExpr : public CXXNamedCastExpr {
225public:
226  CXXConstCastExpr(QualType ty, Expr *op, TypeSourceInfo *writtenTy,
227                   SourceLocation l)
228    : CXXNamedCastExpr(CXXConstCastExprClass, ty, CK_NoOp, op,
229                       CXXBaseSpecifierArray(), writtenTy, l) {}
230
231  explicit CXXConstCastExpr(EmptyShell Empty)
232    : CXXNamedCastExpr(CXXConstCastExprClass, Empty) { }
233
234  static bool classof(const Stmt *T) {
235    return T->getStmtClass() == CXXConstCastExprClass;
236  }
237  static bool classof(const CXXConstCastExpr *) { return true; }
238};
239
240/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal.
241///
242class CXXBoolLiteralExpr : public Expr {
243  bool Value;
244  SourceLocation Loc;
245public:
246  CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) :
247    Expr(CXXBoolLiteralExprClass, Ty, false, false), Value(val), Loc(l) {}
248
249  explicit CXXBoolLiteralExpr(EmptyShell Empty)
250    : Expr(CXXBoolLiteralExprClass, Empty) { }
251
252  bool getValue() const { return Value; }
253  void setValue(bool V) { Value = V; }
254
255  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
256
257  SourceLocation getLocation() const { return Loc; }
258  void setLocation(SourceLocation L) { Loc = L; }
259
260  static bool classof(const Stmt *T) {
261    return T->getStmtClass() == CXXBoolLiteralExprClass;
262  }
263  static bool classof(const CXXBoolLiteralExpr *) { return true; }
264
265  // Iterators
266  virtual child_iterator child_begin();
267  virtual child_iterator child_end();
268};
269
270/// CXXNullPtrLiteralExpr - [C++0x 2.14.7] C++ Pointer Literal
271class CXXNullPtrLiteralExpr : public Expr {
272  SourceLocation Loc;
273public:
274  CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l) :
275    Expr(CXXNullPtrLiteralExprClass, Ty, false, false), Loc(l) {}
276
277  explicit CXXNullPtrLiteralExpr(EmptyShell Empty)
278    : Expr(CXXNullPtrLiteralExprClass, Empty) { }
279
280  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
281
282  SourceLocation getLocation() const { return Loc; }
283  void setLocation(SourceLocation L) { Loc = L; }
284
285  static bool classof(const Stmt *T) {
286    return T->getStmtClass() == CXXNullPtrLiteralExprClass;
287  }
288  static bool classof(const CXXNullPtrLiteralExpr *) { return true; }
289
290  virtual child_iterator child_begin();
291  virtual child_iterator child_end();
292};
293
294/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets
295/// the type_info that corresponds to the supplied type, or the (possibly
296/// dynamic) type of the supplied expression.
297///
298/// This represents code like @c typeid(int) or @c typeid(*objPtr)
299class CXXTypeidExpr : public Expr {
300private:
301  llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
302  SourceRange Range;
303
304public:
305  CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R)
306    : Expr(CXXTypeidExprClass, Ty,
307           // typeid is never type-dependent (C++ [temp.dep.expr]p4)
308           false,
309           // typeid is value-dependent if the type or expression are dependent
310           Operand->getType()->isDependentType()),
311      Operand(Operand), Range(R) { }
312
313  CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R)
314    : Expr(CXXTypeidExprClass, Ty,
315        // typeid is never type-dependent (C++ [temp.dep.expr]p4)
316        false,
317        // typeid is value-dependent if the type or expression are dependent
318        Operand->isTypeDependent() || Operand->isValueDependent()),
319      Operand(Operand), Range(R) { }
320
321  bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
322
323  /// \brief Retrieves the type operand of this typeid() expression after
324  /// various required adjustments (removing reference types, cv-qualifiers).
325  QualType getTypeOperand() const;
326
327  /// \brief Retrieve source information for the type operand.
328  TypeSourceInfo *getTypeOperandSourceInfo() const {
329    assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
330    return Operand.get<TypeSourceInfo *>();
331  }
332
333  Expr* getExprOperand() const {
334    assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
335    return static_cast<Expr*>(Operand.get<Stmt *>());
336  }
337
338  virtual SourceRange getSourceRange() const {
339    return Range;
340  }
341  static bool classof(const Stmt *T) {
342    return T->getStmtClass() == CXXTypeidExprClass;
343  }
344  static bool classof(const CXXTypeidExpr *) { return true; }
345
346  // Iterators
347  virtual child_iterator child_begin();
348  virtual child_iterator child_end();
349};
350
351/// CXXThisExpr - Represents the "this" expression in C++, which is a
352/// pointer to the object on which the current member function is
353/// executing (C++ [expr.prim]p3). Example:
354///
355/// @code
356/// class Foo {
357/// public:
358///   void bar();
359///   void test() { this->bar(); }
360/// };
361/// @endcode
362class CXXThisExpr : public Expr {
363  SourceLocation Loc;
364  bool Implicit : 1;
365
366public:
367  CXXThisExpr(SourceLocation L, QualType Type, bool isImplicit)
368    : Expr(CXXThisExprClass, Type,
369           // 'this' is type-dependent if the class type of the enclosing
370           // member function is dependent (C++ [temp.dep.expr]p2)
371           Type->isDependentType(), Type->isDependentType()),
372      Loc(L), Implicit(isImplicit) { }
373
374  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
375
376  bool isImplicit() const { return Implicit; }
377  void setImplicit(bool I) { Implicit = I; }
378
379  static bool classof(const Stmt *T) {
380    return T->getStmtClass() == CXXThisExprClass;
381  }
382  static bool classof(const CXXThisExpr *) { return true; }
383
384  // Iterators
385  virtual child_iterator child_begin();
386  virtual child_iterator child_end();
387};
388
389///  CXXThrowExpr - [C++ 15] C++ Throw Expression.  This handles
390///  'throw' and 'throw' assignment-expression.  When
391///  assignment-expression isn't present, Op will be null.
392///
393class CXXThrowExpr : public Expr {
394  Stmt *Op;
395  SourceLocation ThrowLoc;
396public:
397  // Ty is the void type which is used as the result type of the
398  // exepression.  The l is the location of the throw keyword.  expr
399  // can by null, if the optional expression to throw isn't present.
400  CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) :
401    Expr(CXXThrowExprClass, Ty, false, false), Op(expr), ThrowLoc(l) {}
402  const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); }
403  Expr *getSubExpr() { return cast_or_null<Expr>(Op); }
404  void setSubExpr(Expr *E) { Op = E; }
405
406  SourceLocation getThrowLoc() const { return ThrowLoc; }
407  void setThrowLoc(SourceLocation L) { ThrowLoc = L; }
408
409  virtual SourceRange getSourceRange() const {
410    if (getSubExpr() == 0)
411      return SourceRange(ThrowLoc, ThrowLoc);
412    return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd());
413  }
414
415  static bool classof(const Stmt *T) {
416    return T->getStmtClass() == CXXThrowExprClass;
417  }
418  static bool classof(const CXXThrowExpr *) { return true; }
419
420  // Iterators
421  virtual child_iterator child_begin();
422  virtual child_iterator child_end();
423};
424
425/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a
426/// function call argument that was created from the corresponding
427/// parameter's default argument, when the call did not explicitly
428/// supply arguments for all of the parameters.
429class CXXDefaultArgExpr : public Expr {
430  /// \brief The parameter whose default is being used.
431  ///
432  /// When the bit is set, the subexpression is stored after the
433  /// CXXDefaultArgExpr itself. When the bit is clear, the parameter's
434  /// actual default expression is the subexpression.
435  llvm::PointerIntPair<ParmVarDecl *, 1, bool> Param;
436
437  /// \brief The location where the default argument expression was used.
438  SourceLocation Loc;
439
440protected:
441  CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param)
442    : Expr(SC,
443           param->hasUnparsedDefaultArg()
444             ? param->getType().getNonReferenceType()
445             : param->getDefaultArg()->getType(),
446           false, false),
447      Param(param, false), Loc(Loc) { }
448
449  CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param,
450                    Expr *SubExpr)
451    : Expr(SC, SubExpr->getType(), false, false), Param(param, true), Loc(Loc)
452  {
453    *reinterpret_cast<Expr **>(this + 1) = SubExpr;
454  }
455
456protected:
457  virtual void DoDestroy(ASTContext &C);
458
459public:
460  // Param is the parameter whose default argument is used by this
461  // expression.
462  static CXXDefaultArgExpr *Create(ASTContext &C, SourceLocation Loc,
463                                   ParmVarDecl *Param) {
464    return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param);
465  }
466
467  // Param is the parameter whose default argument is used by this
468  // expression, and SubExpr is the expression that will actually be used.
469  static CXXDefaultArgExpr *Create(ASTContext &C,
470                                   SourceLocation Loc,
471                                   ParmVarDecl *Param,
472                                   Expr *SubExpr);
473
474  // Retrieve the parameter that the argument was created from.
475  const ParmVarDecl *getParam() const { return Param.getPointer(); }
476  ParmVarDecl *getParam() { return Param.getPointer(); }
477
478  // Retrieve the actual argument to the function call.
479  const Expr *getExpr() const {
480    if (Param.getInt())
481      return *reinterpret_cast<Expr const * const*> (this + 1);
482    return getParam()->getDefaultArg();
483  }
484  Expr *getExpr() {
485    if (Param.getInt())
486      return *reinterpret_cast<Expr **> (this + 1);
487    return getParam()->getDefaultArg();
488  }
489
490  /// \brief Retrieve the location where this default argument was actually
491  /// used.
492  SourceLocation getUsedLocation() const { return Loc; }
493
494  virtual SourceRange getSourceRange() const {
495    // Default argument expressions have no representation in the
496    // source, so they have an empty source range.
497    return SourceRange();
498  }
499
500  static bool classof(const Stmt *T) {
501    return T->getStmtClass() == CXXDefaultArgExprClass;
502  }
503  static bool classof(const CXXDefaultArgExpr *) { return true; }
504
505  // Iterators
506  virtual child_iterator child_begin();
507  virtual child_iterator child_end();
508};
509
510/// CXXTemporary - Represents a C++ temporary.
511class CXXTemporary {
512  /// Destructor - The destructor that needs to be called.
513  const CXXDestructorDecl *Destructor;
514
515  CXXTemporary(const CXXDestructorDecl *destructor)
516    : Destructor(destructor) { }
517  ~CXXTemporary() { }
518
519public:
520  static CXXTemporary *Create(ASTContext &C,
521                              const CXXDestructorDecl *Destructor);
522
523  void Destroy(ASTContext &Ctx);
524
525  const CXXDestructorDecl *getDestructor() const { return Destructor; }
526};
527
528/// CXXBindTemporaryExpr - Represents binding an expression to a temporary,
529/// so its destructor can be called later.
530class CXXBindTemporaryExpr : public Expr {
531  CXXTemporary *Temp;
532
533  Stmt *SubExpr;
534
535  CXXBindTemporaryExpr(CXXTemporary *temp, Expr* subexpr)
536   : Expr(CXXBindTemporaryExprClass, subexpr->getType(), false, false),
537     Temp(temp), SubExpr(subexpr) { }
538  ~CXXBindTemporaryExpr() { }
539
540protected:
541  virtual void DoDestroy(ASTContext &C);
542
543public:
544  static CXXBindTemporaryExpr *Create(ASTContext &C, CXXTemporary *Temp,
545                                      Expr* SubExpr);
546
547  CXXTemporary *getTemporary() { return Temp; }
548  const CXXTemporary *getTemporary() const { return Temp; }
549
550  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
551  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
552  void setSubExpr(Expr *E) { SubExpr = E; }
553
554  virtual SourceRange getSourceRange() const {
555    return SubExpr->getSourceRange();
556  }
557
558  // Implement isa/cast/dyncast/etc.
559  static bool classof(const Stmt *T) {
560    return T->getStmtClass() == CXXBindTemporaryExprClass;
561  }
562  static bool classof(const CXXBindTemporaryExpr *) { return true; }
563
564  // Iterators
565  virtual child_iterator child_begin();
566  virtual child_iterator child_end();
567};
568
569/// CXXBindReferenceExpr - Represents binding an expression to a reference.
570/// In the example:
571///
572/// const int &i = 10;
573///
574/// a bind reference expression is inserted to indicate that 10 is bound to
575/// a reference. (Ans also that a temporary needs to be created to hold the
576/// value).
577class CXXBindReferenceExpr : public Expr {
578  // SubExpr - The expression being bound.
579  Stmt *SubExpr;
580
581  // ExtendsLifetime - Whether binding this reference extends the lifetime of
582  // the expression being bound. FIXME: Add C++ reference.
583  bool ExtendsLifetime;
584
585  /// RequiresTemporaryCopy - Whether binding the subexpression requires a
586  /// temporary copy.
587  bool RequiresTemporaryCopy;
588
589  CXXBindReferenceExpr(Expr *subexpr, bool ExtendsLifetime,
590                       bool RequiresTemporaryCopy)
591  : Expr(CXXBindReferenceExprClass, subexpr->getType(), false, false),
592    SubExpr(subexpr), ExtendsLifetime(ExtendsLifetime),
593    RequiresTemporaryCopy(RequiresTemporaryCopy) { }
594  ~CXXBindReferenceExpr() { }
595
596protected:
597  virtual void DoDestroy(ASTContext &C);
598
599public:
600  static CXXBindReferenceExpr *Create(ASTContext &C, Expr *SubExpr,
601                                      bool ExtendsLifetime,
602                                      bool RequiresTemporaryCopy);
603
604  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
605  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
606  void setSubExpr(Expr *E) { SubExpr = E; }
607
608  virtual SourceRange getSourceRange() const {
609    return SubExpr->getSourceRange();
610  }
611
612  /// requiresTemporaryCopy - Whether binding the subexpression requires a
613  /// temporary copy.
614  bool requiresTemporaryCopy() const { return RequiresTemporaryCopy; }
615
616  // extendsLifetime - Whether binding this reference extends the lifetime of
617  // the expression being bound. FIXME: Add C++ reference.
618  bool extendsLifetime() { return ExtendsLifetime; }
619
620  // Implement isa/cast/dyncast/etc.
621  static bool classof(const Stmt *T) {
622    return T->getStmtClass() == CXXBindReferenceExprClass;
623  }
624  static bool classof(const CXXBindReferenceExpr *) { return true; }
625
626  // Iterators
627  virtual child_iterator child_begin();
628  virtual child_iterator child_end();
629};
630
631/// CXXConstructExpr - Represents a call to a C++ constructor.
632class CXXConstructExpr : public Expr {
633  CXXConstructorDecl *Constructor;
634
635  SourceLocation Loc;
636  bool Elidable : 1;
637  bool ZeroInitialization : 1;
638  bool BaseInitialization : 1;
639  Stmt **Args;
640  unsigned NumArgs;
641
642protected:
643  CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
644                   SourceLocation Loc,
645                   CXXConstructorDecl *d, bool elidable,
646                   Expr **args, unsigned numargs,
647                   bool ZeroInitialization = false,
648                   bool BaseInitialization = false);
649  ~CXXConstructExpr() { }
650
651  virtual void DoDestroy(ASTContext &C);
652
653public:
654  /// \brief Construct an empty C++ construction expression that will store
655  /// \p numargs arguments.
656  CXXConstructExpr(EmptyShell Empty, ASTContext &C, unsigned numargs);
657
658  static CXXConstructExpr *Create(ASTContext &C, QualType T,
659                                  SourceLocation Loc,
660                                  CXXConstructorDecl *D, bool Elidable,
661                                  Expr **Args, unsigned NumArgs,
662                                  bool ZeroInitialization = false,
663                                  bool BaseInitialization = false);
664
665
666  CXXConstructorDecl* getConstructor() const { return Constructor; }
667  void setConstructor(CXXConstructorDecl *C) { Constructor = C; }
668
669  SourceLocation getLocation() const { return Loc; }
670  void setLocation(SourceLocation Loc) { this->Loc = Loc; }
671
672  /// \brief Whether this construction is elidable.
673  bool isElidable() const { return Elidable; }
674  void setElidable(bool E) { Elidable = E; }
675
676  /// \brief Whether this construction first requires
677  /// zero-initialization before the initializer is called.
678  bool requiresZeroInitialization() const { return ZeroInitialization; }
679  void setRequiresZeroInitialization(bool ZeroInit) {
680    ZeroInitialization = ZeroInit;
681  }
682
683  /// \brief Determines whether this constructor is actually constructing
684  /// a base class (rather than a complete object).
685  bool isBaseInitialization() const { return BaseInitialization; }
686  void setBaseInitialization(bool BI) { BaseInitialization = BI; }
687
688  typedef ExprIterator arg_iterator;
689  typedef ConstExprIterator const_arg_iterator;
690
691  arg_iterator arg_begin() { return Args; }
692  arg_iterator arg_end() { return Args + NumArgs; }
693  const_arg_iterator arg_begin() const { return Args; }
694  const_arg_iterator arg_end() const { return Args + NumArgs; }
695
696  Expr **getArgs() const { return reinterpret_cast<Expr **>(Args); }
697  unsigned getNumArgs() const { return NumArgs; }
698
699  /// getArg - Return the specified argument.
700  Expr *getArg(unsigned Arg) {
701    assert(Arg < NumArgs && "Arg access out of range!");
702    return cast<Expr>(Args[Arg]);
703  }
704  const Expr *getArg(unsigned Arg) const {
705    assert(Arg < NumArgs && "Arg access out of range!");
706    return cast<Expr>(Args[Arg]);
707  }
708
709  /// setArg - Set the specified argument.
710  void setArg(unsigned Arg, Expr *ArgExpr) {
711    assert(Arg < NumArgs && "Arg access out of range!");
712    Args[Arg] = ArgExpr;
713  }
714
715  virtual SourceRange getSourceRange() const;
716
717  static bool classof(const Stmt *T) {
718    return T->getStmtClass() == CXXConstructExprClass ||
719      T->getStmtClass() == CXXTemporaryObjectExprClass;
720  }
721  static bool classof(const CXXConstructExpr *) { return true; }
722
723  // Iterators
724  virtual child_iterator child_begin();
725  virtual child_iterator child_end();
726};
727
728/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion
729/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c
730/// x = int(0.5);
731class CXXFunctionalCastExpr : public ExplicitCastExpr {
732  SourceLocation TyBeginLoc;
733  SourceLocation RParenLoc;
734public:
735  CXXFunctionalCastExpr(QualType ty, TypeSourceInfo *writtenTy,
736                        SourceLocation tyBeginLoc, CastKind kind,
737                        Expr *castExpr, CXXBaseSpecifierArray BasePath,
738                        SourceLocation rParenLoc)
739    : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, kind, castExpr,
740                       BasePath, writtenTy),
741      TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
742
743  explicit CXXFunctionalCastExpr(EmptyShell Shell)
744    : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell) { }
745
746  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
747  void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; }
748  SourceLocation getRParenLoc() const { return RParenLoc; }
749  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
750
751  virtual SourceRange getSourceRange() const {
752    return SourceRange(TyBeginLoc, RParenLoc);
753  }
754  static bool classof(const Stmt *T) {
755    return T->getStmtClass() == CXXFunctionalCastExprClass;
756  }
757  static bool classof(const CXXFunctionalCastExpr *) { return true; }
758};
759
760/// @brief Represents a C++ functional cast expression that builds a
761/// temporary object.
762///
763/// This expression type represents a C++ "functional" cast
764/// (C++[expr.type.conv]) with N != 1 arguments that invokes a
765/// constructor to build a temporary object. If N == 0 but no
766/// constructor will be called (because the functional cast is
767/// performing a value-initialized an object whose class type has no
768/// user-declared constructors), CXXZeroInitValueExpr will represent
769/// the functional cast. Finally, with N == 1 arguments the functional
770/// cast expression will be represented by CXXFunctionalCastExpr.
771/// Example:
772/// @code
773/// struct X { X(int, float); }
774///
775/// X create_X() {
776///   return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
777/// };
778/// @endcode
779class CXXTemporaryObjectExpr : public CXXConstructExpr {
780  SourceLocation TyBeginLoc;
781  SourceLocation RParenLoc;
782
783public:
784  CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons,
785                         QualType writtenTy, SourceLocation tyBeginLoc,
786                         Expr **Args,unsigned NumArgs,
787                         SourceLocation rParenLoc,
788                         bool ZeroInitialization = false);
789
790  ~CXXTemporaryObjectExpr() { }
791
792  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
793  SourceLocation getRParenLoc() const { return RParenLoc; }
794
795  virtual SourceRange getSourceRange() const {
796    return SourceRange(TyBeginLoc, RParenLoc);
797  }
798  static bool classof(const Stmt *T) {
799    return T->getStmtClass() == CXXTemporaryObjectExprClass;
800  }
801  static bool classof(const CXXTemporaryObjectExpr *) { return true; }
802};
803
804/// CXXZeroInitValueExpr - [C++ 5.2.3p2]
805/// Expression "T()" which creates a value-initialized rvalue of type
806/// T, which is either a non-class type or a class type without any
807/// user-defined constructors.
808///
809class CXXZeroInitValueExpr : public Expr {
810  SourceLocation TyBeginLoc;
811  SourceLocation RParenLoc;
812
813public:
814  CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc,
815                       SourceLocation rParenLoc ) :
816    Expr(CXXZeroInitValueExprClass, ty, false, false),
817    TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
818
819  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
820  SourceLocation getRParenLoc() const { return RParenLoc; }
821
822  /// @brief Whether this initialization expression was
823  /// implicitly-generated.
824  bool isImplicit() const {
825    return TyBeginLoc.isInvalid() && RParenLoc.isInvalid();
826  }
827
828  virtual SourceRange getSourceRange() const {
829    return SourceRange(TyBeginLoc, RParenLoc);
830  }
831
832  static bool classof(const Stmt *T) {
833    return T->getStmtClass() == CXXZeroInitValueExprClass;
834  }
835  static bool classof(const CXXZeroInitValueExpr *) { return true; }
836
837  // Iterators
838  virtual child_iterator child_begin();
839  virtual child_iterator child_end();
840};
841
842/// CXXNewExpr - A new expression for memory allocation and constructor calls,
843/// e.g: "new CXXNewExpr(foo)".
844class CXXNewExpr : public Expr {
845  // Was the usage ::new, i.e. is the global new to be used?
846  bool GlobalNew : 1;
847  // Was the form (type-id) used? Otherwise, it was new-type-id.
848  bool ParenTypeId : 1;
849  // Is there an initializer? If not, built-ins are uninitialized, else they're
850  // value-initialized.
851  bool Initializer : 1;
852  // Do we allocate an array? If so, the first SubExpr is the size expression.
853  bool Array : 1;
854  // The number of placement new arguments.
855  unsigned NumPlacementArgs : 14;
856  // The number of constructor arguments. This may be 1 even for non-class
857  // types; use the pseudo copy constructor.
858  unsigned NumConstructorArgs : 14;
859  // Contains an optional array size expression, any number of optional
860  // placement arguments, and any number of optional constructor arguments,
861  // in that order.
862  Stmt **SubExprs;
863  // Points to the allocation function used.
864  FunctionDecl *OperatorNew;
865  // Points to the deallocation function used in case of error. May be null.
866  FunctionDecl *OperatorDelete;
867  // Points to the constructor used. Cannot be null if AllocType is a record;
868  // it would still point at the default constructor (even an implicit one).
869  // Must be null for all other types.
870  CXXConstructorDecl *Constructor;
871
872  SourceLocation StartLoc;
873  SourceLocation EndLoc;
874
875public:
876  CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
877             Expr **placementArgs, unsigned numPlaceArgs, bool ParenTypeId,
878             Expr *arraySize, CXXConstructorDecl *constructor, bool initializer,
879             Expr **constructorArgs, unsigned numConsArgs,
880             FunctionDecl *operatorDelete, QualType ty,
881             SourceLocation startLoc, SourceLocation endLoc);
882
883  virtual void DoDestroy(ASTContext &C);
884
885  QualType getAllocatedType() const {
886    assert(getType()->isPointerType());
887    return getType()->getAs<PointerType>()->getPointeeType();
888  }
889
890  FunctionDecl *getOperatorNew() const { return OperatorNew; }
891  FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
892  CXXConstructorDecl *getConstructor() const { return Constructor; }
893
894  bool isArray() const { return Array; }
895  Expr *getArraySize() {
896    return Array ? cast<Expr>(SubExprs[0]) : 0;
897  }
898  const Expr *getArraySize() const {
899    return Array ? cast<Expr>(SubExprs[0]) : 0;
900  }
901
902  unsigned getNumPlacementArgs() const { return NumPlacementArgs; }
903  Expr *getPlacementArg(unsigned i) {
904    assert(i < NumPlacementArgs && "Index out of range");
905    return cast<Expr>(SubExprs[Array + i]);
906  }
907  const Expr *getPlacementArg(unsigned i) const {
908    assert(i < NumPlacementArgs && "Index out of range");
909    return cast<Expr>(SubExprs[Array + i]);
910  }
911
912  bool isGlobalNew() const { return GlobalNew; }
913  bool isParenTypeId() const { return ParenTypeId; }
914  bool hasInitializer() const { return Initializer; }
915
916  unsigned getNumConstructorArgs() const { return NumConstructorArgs; }
917  Expr *getConstructorArg(unsigned i) {
918    assert(i < NumConstructorArgs && "Index out of range");
919    return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
920  }
921  const Expr *getConstructorArg(unsigned i) const {
922    assert(i < NumConstructorArgs && "Index out of range");
923    return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
924  }
925
926  typedef ExprIterator arg_iterator;
927  typedef ConstExprIterator const_arg_iterator;
928
929  arg_iterator placement_arg_begin() {
930    return SubExprs + Array;
931  }
932  arg_iterator placement_arg_end() {
933    return SubExprs + Array + getNumPlacementArgs();
934  }
935  const_arg_iterator placement_arg_begin() const {
936    return SubExprs + Array;
937  }
938  const_arg_iterator placement_arg_end() const {
939    return SubExprs + Array + getNumPlacementArgs();
940  }
941
942  arg_iterator constructor_arg_begin() {
943    return SubExprs + Array + getNumPlacementArgs();
944  }
945  arg_iterator constructor_arg_end() {
946    return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs();
947  }
948  const_arg_iterator constructor_arg_begin() const {
949    return SubExprs + Array + getNumPlacementArgs();
950  }
951  const_arg_iterator constructor_arg_end() const {
952    return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs();
953  }
954
955  virtual SourceRange getSourceRange() const {
956    return SourceRange(StartLoc, EndLoc);
957  }
958
959  static bool classof(const Stmt *T) {
960    return T->getStmtClass() == CXXNewExprClass;
961  }
962  static bool classof(const CXXNewExpr *) { return true; }
963
964  // Iterators
965  virtual child_iterator child_begin();
966  virtual child_iterator child_end();
967};
968
969/// CXXDeleteExpr - A delete expression for memory deallocation and destructor
970/// calls, e.g. "delete[] pArray".
971class CXXDeleteExpr : public Expr {
972  // Is this a forced global delete, i.e. "::delete"?
973  bool GlobalDelete : 1;
974  // Is this the array form of delete, i.e. "delete[]"?
975  bool ArrayForm : 1;
976  // Points to the operator delete overload that is used. Could be a member.
977  FunctionDecl *OperatorDelete;
978  // The pointer expression to be deleted.
979  Stmt *Argument;
980  // Location of the expression.
981  SourceLocation Loc;
982public:
983  CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm,
984                FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc)
985    : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete),
986      ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg),
987      Loc(loc) { }
988
989  bool isGlobalDelete() const { return GlobalDelete; }
990  bool isArrayForm() const { return ArrayForm; }
991
992  FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
993
994  Expr *getArgument() { return cast<Expr>(Argument); }
995  const Expr *getArgument() const { return cast<Expr>(Argument); }
996
997  virtual SourceRange getSourceRange() const {
998    return SourceRange(Loc, Argument->getLocEnd());
999  }
1000
1001  static bool classof(const Stmt *T) {
1002    return T->getStmtClass() == CXXDeleteExprClass;
1003  }
1004  static bool classof(const CXXDeleteExpr *) { return true; }
1005
1006  // Iterators
1007  virtual child_iterator child_begin();
1008  virtual child_iterator child_end();
1009};
1010
1011/// \brief Structure used to store the type being destroyed by a
1012/// pseudo-destructor expression.
1013class PseudoDestructorTypeStorage {
1014  /// \brief Either the type source information or the name of the type, if
1015  /// it couldn't be resolved due to type-dependence.
1016  llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type;
1017
1018  /// \brief The starting source location of the pseudo-destructor type.
1019  SourceLocation Location;
1020
1021public:
1022  PseudoDestructorTypeStorage() { }
1023
1024  PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc)
1025    : Type(II), Location(Loc) { }
1026
1027  PseudoDestructorTypeStorage(TypeSourceInfo *Info);
1028
1029  TypeSourceInfo *getTypeSourceInfo() const {
1030    return Type.dyn_cast<TypeSourceInfo *>();
1031  }
1032
1033  IdentifierInfo *getIdentifier() const {
1034    return Type.dyn_cast<IdentifierInfo *>();
1035  }
1036
1037  SourceLocation getLocation() const { return Location; }
1038};
1039
1040/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
1041///
1042/// A pseudo-destructor is an expression that looks like a member access to a
1043/// destructor of a scalar type, except that scalar types don't have
1044/// destructors. For example:
1045///
1046/// \code
1047/// typedef int T;
1048/// void f(int *p) {
1049///   p->T::~T();
1050/// }
1051/// \endcode
1052///
1053/// Pseudo-destructors typically occur when instantiating templates such as:
1054///
1055/// \code
1056/// template<typename T>
1057/// void destroy(T* ptr) {
1058///   ptr->T::~T();
1059/// }
1060/// \endcode
1061///
1062/// for scalar types. A pseudo-destructor expression has no run-time semantics
1063/// beyond evaluating the base expression.
1064class CXXPseudoDestructorExpr : public Expr {
1065  /// \brief The base expression (that is being destroyed).
1066  Stmt *Base;
1067
1068  /// \brief Whether the operator was an arrow ('->'); otherwise, it was a
1069  /// period ('.').
1070  bool IsArrow : 1;
1071
1072  /// \brief The location of the '.' or '->' operator.
1073  SourceLocation OperatorLoc;
1074
1075  /// \brief The nested-name-specifier that follows the operator, if present.
1076  NestedNameSpecifier *Qualifier;
1077
1078  /// \brief The source range that covers the nested-name-specifier, if
1079  /// present.
1080  SourceRange QualifierRange;
1081
1082  /// \brief The type that precedes the '::' in a qualified pseudo-destructor
1083  /// expression.
1084  TypeSourceInfo *ScopeType;
1085
1086  /// \brief The location of the '::' in a qualified pseudo-destructor
1087  /// expression.
1088  SourceLocation ColonColonLoc;
1089
1090  /// \brief The location of the '~'.
1091  SourceLocation TildeLoc;
1092
1093  /// \brief The type being destroyed, or its name if we were unable to
1094  /// resolve the name.
1095  PseudoDestructorTypeStorage DestroyedType;
1096
1097public:
1098  CXXPseudoDestructorExpr(ASTContext &Context,
1099                          Expr *Base, bool isArrow, SourceLocation OperatorLoc,
1100                          NestedNameSpecifier *Qualifier,
1101                          SourceRange QualifierRange,
1102                          TypeSourceInfo *ScopeType,
1103                          SourceLocation ColonColonLoc,
1104                          SourceLocation TildeLoc,
1105                          PseudoDestructorTypeStorage DestroyedType)
1106    : Expr(CXXPseudoDestructorExprClass,
1107           Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
1108                                                          false, 0, false,
1109                                                          false, 0, 0,
1110                                                      FunctionType::ExtInfo())),
1111           /*isTypeDependent=*/(Base->isTypeDependent() ||
1112            (DestroyedType.getTypeSourceInfo() &&
1113              DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
1114           /*isValueDependent=*/Base->isValueDependent()),
1115      Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
1116      OperatorLoc(OperatorLoc), Qualifier(Qualifier),
1117      QualifierRange(QualifierRange),
1118      ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
1119      DestroyedType(DestroyedType) { }
1120
1121  void setBase(Expr *E) { Base = E; }
1122  Expr *getBase() const { return cast<Expr>(Base); }
1123
1124  /// \brief Determines whether this member expression actually had
1125  /// a C++ nested-name-specifier prior to the name of the member, e.g.,
1126  /// x->Base::foo.
1127  bool hasQualifier() const { return Qualifier != 0; }
1128
1129  /// \brief If the member name was qualified, retrieves the source range of
1130  /// the nested-name-specifier that precedes the member name. Otherwise,
1131  /// returns an empty source range.
1132  SourceRange getQualifierRange() const { return QualifierRange; }
1133
1134  /// \brief If the member name was qualified, retrieves the
1135  /// nested-name-specifier that precedes the member name. Otherwise, returns
1136  /// NULL.
1137  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1138
1139  /// \brief Determine whether this pseudo-destructor expression was written
1140  /// using an '->' (otherwise, it used a '.').
1141  bool isArrow() const { return IsArrow; }
1142  void setArrow(bool A) { IsArrow = A; }
1143
1144  /// \brief Retrieve the location of the '.' or '->' operator.
1145  SourceLocation getOperatorLoc() const { return OperatorLoc; }
1146
1147  /// \brief Retrieve the scope type in a qualified pseudo-destructor
1148  /// expression.
1149  ///
1150  /// Pseudo-destructor expressions can have extra qualification within them
1151  /// that is not part of the nested-name-specifier, e.g., \c p->T::~T().
1152  /// Here, if the object type of the expression is (or may be) a scalar type,
1153  /// \p T may also be a scalar type and, therefore, cannot be part of a
1154  /// nested-name-specifier. It is stored as the "scope type" of the pseudo-
1155  /// destructor expression.
1156  TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; }
1157
1158  /// \brief Retrieve the location of the '::' in a qualified pseudo-destructor
1159  /// expression.
1160  SourceLocation getColonColonLoc() const { return ColonColonLoc; }
1161
1162  /// \brief Retrieve the location of the '~'.
1163  SourceLocation getTildeLoc() const { return TildeLoc; }
1164
1165  /// \brief Retrieve the source location information for the type
1166  /// being destroyed.
1167  ///
1168  /// This type-source information is available for non-dependent
1169  /// pseudo-destructor expressions and some dependent pseudo-destructor
1170  /// expressions. Returns NULL if we only have the identifier for a
1171  /// dependent pseudo-destructor expression.
1172  TypeSourceInfo *getDestroyedTypeInfo() const {
1173    return DestroyedType.getTypeSourceInfo();
1174  }
1175
1176  /// \brief In a dependent pseudo-destructor expression for which we do not
1177  /// have full type information on the destroyed type, provides the name
1178  /// of the destroyed type.
1179  IdentifierInfo *getDestroyedTypeIdentifier() const {
1180    return DestroyedType.getIdentifier();
1181  }
1182
1183  /// \brief Retrieve the type being destroyed.
1184  QualType getDestroyedType() const;
1185
1186  /// \brief Retrieve the starting location of the type being destroyed.
1187  SourceLocation getDestroyedTypeLoc() const {
1188    return DestroyedType.getLocation();
1189  }
1190
1191  virtual SourceRange getSourceRange() const;
1192
1193  static bool classof(const Stmt *T) {
1194    return T->getStmtClass() == CXXPseudoDestructorExprClass;
1195  }
1196  static bool classof(const CXXPseudoDestructorExpr *) { return true; }
1197
1198  // Iterators
1199  virtual child_iterator child_begin();
1200  virtual child_iterator child_end();
1201};
1202
1203/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the
1204/// implementation of TR1/C++0x type trait templates.
1205/// Example:
1206/// __is_pod(int) == true
1207/// __is_enum(std::string) == false
1208class UnaryTypeTraitExpr : public Expr {
1209  /// UTT - The trait.
1210  UnaryTypeTrait UTT;
1211
1212  /// Loc - The location of the type trait keyword.
1213  SourceLocation Loc;
1214
1215  /// RParen - The location of the closing paren.
1216  SourceLocation RParen;
1217
1218  /// QueriedType - The type we're testing.
1219  QualType QueriedType;
1220
1221public:
1222  UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried,
1223                     SourceLocation rparen, QualType ty)
1224    : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()),
1225      UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { }
1226
1227  virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);}
1228
1229  UnaryTypeTrait getTrait() const { return UTT; }
1230
1231  QualType getQueriedType() const { return QueriedType; }
1232
1233  bool EvaluateTrait(ASTContext&) const;
1234
1235  static bool classof(const Stmt *T) {
1236    return T->getStmtClass() == UnaryTypeTraitExprClass;
1237  }
1238  static bool classof(const UnaryTypeTraitExpr *) { return true; }
1239
1240  // Iterators
1241  virtual child_iterator child_begin();
1242  virtual child_iterator child_end();
1243};
1244
1245/// \brief A reference to an overloaded function set, either an
1246/// \t UnresolvedLookupExpr or an \t UnresolvedMemberExpr.
1247class OverloadExpr : public Expr {
1248  /// The results.  These are undesugared, which is to say, they may
1249  /// include UsingShadowDecls.  Access is relative to the naming
1250  /// class.
1251  UnresolvedSet<4> Results;
1252
1253  /// The common name of these declarations.
1254  DeclarationName Name;
1255
1256  /// The scope specifier, if any.
1257  NestedNameSpecifier *Qualifier;
1258
1259  /// The source range of the scope specifier.
1260  SourceRange QualifierRange;
1261
1262  /// The location of the name.
1263  SourceLocation NameLoc;
1264
1265  /// True if the name was a template-id.
1266  bool HasExplicitTemplateArgs;
1267
1268protected:
1269  OverloadExpr(StmtClass K, QualType T, bool Dependent,
1270               NestedNameSpecifier *Qualifier, SourceRange QRange,
1271               DeclarationName Name, SourceLocation NameLoc,
1272               bool HasTemplateArgs)
1273    : Expr(K, T, Dependent, Dependent),
1274      Name(Name), Qualifier(Qualifier), QualifierRange(QRange),
1275      NameLoc(NameLoc), HasExplicitTemplateArgs(HasTemplateArgs)
1276  {}
1277
1278public:
1279  /// Computes whether an unresolved lookup on the given declarations
1280  /// and optional template arguments is type- and value-dependent.
1281  static bool ComputeDependence(UnresolvedSetIterator Begin,
1282                                UnresolvedSetIterator End,
1283                                const TemplateArgumentListInfo *Args);
1284
1285  /// Finds the overloaded expression in the given expression of
1286  /// OverloadTy.
1287  ///
1288  /// \return the expression (which must be there) and true if it is
1289  /// within an address-of operator.
1290  static llvm::PointerIntPair<OverloadExpr*,1> find(Expr *E) {
1291    assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload));
1292
1293    bool op = false;
1294    E = E->IgnoreParens();
1295    if (isa<UnaryOperator>(E))
1296      op = true, E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
1297    return llvm::PointerIntPair<OverloadExpr*,1>(cast<OverloadExpr>(E), op);
1298  }
1299
1300  void addDecls(UnresolvedSetIterator Begin, UnresolvedSetIterator End) {
1301    Results.append(Begin, End);
1302  }
1303
1304  /// Gets the naming class of this lookup, if any.
1305  CXXRecordDecl *getNamingClass() const;
1306
1307  typedef UnresolvedSetImpl::iterator decls_iterator;
1308  decls_iterator decls_begin() const { return Results.begin(); }
1309  decls_iterator decls_end() const { return Results.end(); }
1310
1311  /// Gets the decls as an unresolved set.
1312  const UnresolvedSetImpl &getDecls() { return Results; }
1313
1314  /// Gets the number of declarations in the unresolved set.
1315  unsigned getNumDecls() const { return Results.size(); }
1316
1317  /// Gets the name looked up.
1318  DeclarationName getName() const { return Name; }
1319  void setName(DeclarationName N) { Name = N; }
1320
1321  /// Gets the location of the name.
1322  SourceLocation getNameLoc() const { return NameLoc; }
1323  void setNameLoc(SourceLocation Loc) { NameLoc = Loc; }
1324
1325  /// Fetches the nested-name qualifier, if one was given.
1326  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1327
1328  /// Fetches the range of the nested-name qualifier.
1329  SourceRange getQualifierRange() const { return QualifierRange; }
1330
1331  /// \brief Determines whether this expression had an explicit
1332  /// template argument list, e.g. f<int>.
1333  bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
1334
1335  ExplicitTemplateArgumentList &getExplicitTemplateArgs(); // defined far below
1336
1337  const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const {
1338    return const_cast<OverloadExpr*>(this)->getExplicitTemplateArgs();
1339  }
1340
1341  ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() {
1342    if (hasExplicitTemplateArgs())
1343      return &getExplicitTemplateArgs();
1344    return 0;
1345  }
1346
1347  static bool classof(const Stmt *T) {
1348    return T->getStmtClass() == UnresolvedLookupExprClass ||
1349           T->getStmtClass() == UnresolvedMemberExprClass;
1350  }
1351  static bool classof(const OverloadExpr *) { return true; }
1352};
1353
1354/// \brief A reference to a name which we were able to look up during
1355/// parsing but could not resolve to a specific declaration.  This
1356/// arises in several ways:
1357///   * we might be waiting for argument-dependent lookup
1358///   * the name might resolve to an overloaded function
1359/// and eventually:
1360///   * the lookup might have included a function template
1361/// These never include UnresolvedUsingValueDecls, which are always
1362/// class members and therefore appear only in
1363/// UnresolvedMemberLookupExprs.
1364class UnresolvedLookupExpr : public OverloadExpr {
1365  /// True if these lookup results should be extended by
1366  /// argument-dependent lookup if this is the operand of a function
1367  /// call.
1368  bool RequiresADL;
1369
1370  /// True if these lookup results are overloaded.  This is pretty
1371  /// trivially rederivable if we urgently need to kill this field.
1372  bool Overloaded;
1373
1374  /// The naming class (C++ [class.access.base]p5) of the lookup, if
1375  /// any.  This can generally be recalculated from the context chain,
1376  /// but that can be fairly expensive for unqualified lookups.  If we
1377  /// want to improve memory use here, this could go in a union
1378  /// against the qualified-lookup bits.
1379  CXXRecordDecl *NamingClass;
1380
1381  UnresolvedLookupExpr(QualType T, bool Dependent, CXXRecordDecl *NamingClass,
1382                       NestedNameSpecifier *Qualifier, SourceRange QRange,
1383                       DeclarationName Name, SourceLocation NameLoc,
1384                       bool RequiresADL, bool Overloaded, bool HasTemplateArgs)
1385    : OverloadExpr(UnresolvedLookupExprClass, T, Dependent, Qualifier, QRange,
1386                   Name, NameLoc, HasTemplateArgs),
1387      RequiresADL(RequiresADL), Overloaded(Overloaded), NamingClass(NamingClass)
1388  {}
1389
1390public:
1391  static UnresolvedLookupExpr *Create(ASTContext &C,
1392                                      bool Dependent,
1393                                      CXXRecordDecl *NamingClass,
1394                                      NestedNameSpecifier *Qualifier,
1395                                      SourceRange QualifierRange,
1396                                      DeclarationName Name,
1397                                      SourceLocation NameLoc,
1398                                      bool ADL, bool Overloaded) {
1399    return new(C) UnresolvedLookupExpr(Dependent ? C.DependentTy : C.OverloadTy,
1400                                       Dependent, NamingClass,
1401                                       Qualifier, QualifierRange,
1402                                       Name, NameLoc, ADL, Overloaded, false);
1403  }
1404
1405  static UnresolvedLookupExpr *Create(ASTContext &C,
1406                                      bool Dependent,
1407                                      CXXRecordDecl *NamingClass,
1408                                      NestedNameSpecifier *Qualifier,
1409                                      SourceRange QualifierRange,
1410                                      DeclarationName Name,
1411                                      SourceLocation NameLoc,
1412                                      bool ADL,
1413                                      const TemplateArgumentListInfo &Args);
1414
1415  /// True if this declaration should be extended by
1416  /// argument-dependent lookup.
1417  bool requiresADL() const { return RequiresADL; }
1418
1419  /// True if this lookup is overloaded.
1420  bool isOverloaded() const { return Overloaded; }
1421
1422  /// Gets the 'naming class' (in the sense of C++0x
1423  /// [class.access.base]p5) of the lookup.  This is the scope
1424  /// that was looked in to find these results.
1425  CXXRecordDecl *getNamingClass() const { return NamingClass; }
1426
1427  // Note that, inconsistently with the explicit-template-argument AST
1428  // nodes, users are *forbidden* from calling these methods on objects
1429  // without explicit template arguments.
1430
1431  ExplicitTemplateArgumentList &getExplicitTemplateArgs() {
1432    assert(hasExplicitTemplateArgs());
1433    return *reinterpret_cast<ExplicitTemplateArgumentList*>(this + 1);
1434  }
1435
1436  /// Gets a reference to the explicit template argument list.
1437  const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const {
1438    assert(hasExplicitTemplateArgs());
1439    return *reinterpret_cast<const ExplicitTemplateArgumentList*>(this + 1);
1440  }
1441
1442  /// \brief Copies the template arguments (if present) into the given
1443  /// structure.
1444  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1445    getExplicitTemplateArgs().copyInto(List);
1446  }
1447
1448  SourceLocation getLAngleLoc() const {
1449    return getExplicitTemplateArgs().LAngleLoc;
1450  }
1451
1452  SourceLocation getRAngleLoc() const {
1453    return getExplicitTemplateArgs().RAngleLoc;
1454  }
1455
1456  TemplateArgumentLoc const *getTemplateArgs() const {
1457    return getExplicitTemplateArgs().getTemplateArgs();
1458  }
1459
1460  unsigned getNumTemplateArgs() const {
1461    return getExplicitTemplateArgs().NumTemplateArgs;
1462  }
1463
1464  virtual SourceRange getSourceRange() const {
1465    SourceRange Range(getNameLoc());
1466    if (getQualifier()) Range.setBegin(getQualifierRange().getBegin());
1467    if (hasExplicitTemplateArgs()) Range.setEnd(getRAngleLoc());
1468    return Range;
1469  }
1470
1471  virtual StmtIterator child_begin();
1472  virtual StmtIterator child_end();
1473
1474  static bool classof(const Stmt *T) {
1475    return T->getStmtClass() == UnresolvedLookupExprClass;
1476  }
1477  static bool classof(const UnresolvedLookupExpr *) { return true; }
1478};
1479
1480/// \brief A qualified reference to a name whose declaration cannot
1481/// yet be resolved.
1482///
1483/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that
1484/// it expresses a reference to a declaration such as
1485/// X<T>::value. The difference, however, is that an
1486/// DependentScopeDeclRefExpr node is used only within C++ templates when
1487/// the qualification (e.g., X<T>::) refers to a dependent type. In
1488/// this case, X<T>::value cannot resolve to a declaration because the
1489/// declaration will differ from on instantiation of X<T> to the
1490/// next. Therefore, DependentScopeDeclRefExpr keeps track of the
1491/// qualifier (X<T>::) and the name of the entity being referenced
1492/// ("value"). Such expressions will instantiate to a DeclRefExpr once the
1493/// declaration can be found.
1494class DependentScopeDeclRefExpr : public Expr {
1495  /// The name of the entity we will be referencing.
1496  DeclarationName Name;
1497
1498  /// Location of the name of the declaration we're referencing.
1499  SourceLocation Loc;
1500
1501  /// QualifierRange - The source range that covers the
1502  /// nested-name-specifier.
1503  SourceRange QualifierRange;
1504
1505  /// \brief The nested-name-specifier that qualifies this unresolved
1506  /// declaration name.
1507  NestedNameSpecifier *Qualifier;
1508
1509  /// \brief Whether the name includes explicit template arguments.
1510  bool HasExplicitTemplateArgs;
1511
1512  DependentScopeDeclRefExpr(QualType T,
1513                            NestedNameSpecifier *Qualifier,
1514                            SourceRange QualifierRange,
1515                            DeclarationName Name,
1516                            SourceLocation NameLoc,
1517                            bool HasExplicitTemplateArgs)
1518    : Expr(DependentScopeDeclRefExprClass, T, true, true),
1519      Name(Name), Loc(NameLoc),
1520      QualifierRange(QualifierRange), Qualifier(Qualifier),
1521      HasExplicitTemplateArgs(HasExplicitTemplateArgs)
1522  {}
1523
1524public:
1525  static DependentScopeDeclRefExpr *Create(ASTContext &C,
1526                                           NestedNameSpecifier *Qualifier,
1527                                           SourceRange QualifierRange,
1528                                           DeclarationName Name,
1529                                           SourceLocation NameLoc,
1530                              const TemplateArgumentListInfo *TemplateArgs = 0);
1531
1532  /// \brief Retrieve the name that this expression refers to.
1533  DeclarationName getDeclName() const { return Name; }
1534
1535  /// \brief Retrieve the location of the name within the expression.
1536  SourceLocation getLocation() const { return Loc; }
1537
1538  /// \brief Retrieve the source range of the nested-name-specifier.
1539  SourceRange getQualifierRange() const { return QualifierRange; }
1540
1541  /// \brief Retrieve the nested-name-specifier that qualifies this
1542  /// declaration.
1543  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1544
1545  /// Determines whether this lookup had explicit template arguments.
1546  bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
1547
1548  // Note that, inconsistently with the explicit-template-argument AST
1549  // nodes, users are *forbidden* from calling these methods on objects
1550  // without explicit template arguments.
1551
1552  /// Gets a reference to the explicit template argument list.
1553  const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const {
1554    assert(hasExplicitTemplateArgs());
1555    return *reinterpret_cast<const ExplicitTemplateArgumentList*>(this + 1);
1556  }
1557
1558  /// \brief Copies the template arguments (if present) into the given
1559  /// structure.
1560  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1561    getExplicitTemplateArgs().copyInto(List);
1562  }
1563
1564  SourceLocation getLAngleLoc() const {
1565    return getExplicitTemplateArgs().LAngleLoc;
1566  }
1567
1568  SourceLocation getRAngleLoc() const {
1569    return getExplicitTemplateArgs().RAngleLoc;
1570  }
1571
1572  TemplateArgumentLoc const *getTemplateArgs() const {
1573    return getExplicitTemplateArgs().getTemplateArgs();
1574  }
1575
1576  unsigned getNumTemplateArgs() const {
1577    return getExplicitTemplateArgs().NumTemplateArgs;
1578  }
1579
1580  virtual SourceRange getSourceRange() const {
1581    SourceRange Range(QualifierRange.getBegin(), getLocation());
1582    if (hasExplicitTemplateArgs())
1583      Range.setEnd(getRAngleLoc());
1584    return Range;
1585  }
1586
1587  static bool classof(const Stmt *T) {
1588    return T->getStmtClass() == DependentScopeDeclRefExprClass;
1589  }
1590  static bool classof(const DependentScopeDeclRefExpr *) { return true; }
1591
1592  virtual StmtIterator child_begin();
1593  virtual StmtIterator child_end();
1594};
1595
1596class CXXExprWithTemporaries : public Expr {
1597  Stmt *SubExpr;
1598
1599  CXXTemporary **Temps;
1600  unsigned NumTemps;
1601
1602  CXXExprWithTemporaries(Expr *SubExpr, CXXTemporary **Temps,
1603                         unsigned NumTemps);
1604  ~CXXExprWithTemporaries();
1605
1606protected:
1607  virtual void DoDestroy(ASTContext &C);
1608
1609public:
1610  static CXXExprWithTemporaries *Create(ASTContext &C, Expr *SubExpr,
1611                                        CXXTemporary **Temps,
1612                                        unsigned NumTemps);
1613
1614  unsigned getNumTemporaries() const { return NumTemps; }
1615  CXXTemporary *getTemporary(unsigned i) {
1616    assert(i < NumTemps && "Index out of range");
1617    return Temps[i];
1618  }
1619  const CXXTemporary *getTemporary(unsigned i) const {
1620    return const_cast<CXXExprWithTemporaries*>(this)->getTemporary(i);
1621  }
1622
1623  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
1624  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
1625  void setSubExpr(Expr *E) { SubExpr = E; }
1626
1627  virtual SourceRange getSourceRange() const {
1628    return SubExpr->getSourceRange();
1629  }
1630
1631  // Implement isa/cast/dyncast/etc.
1632  static bool classof(const Stmt *T) {
1633    return T->getStmtClass() == CXXExprWithTemporariesClass;
1634  }
1635  static bool classof(const CXXExprWithTemporaries *) { return true; }
1636
1637  // Iterators
1638  virtual child_iterator child_begin();
1639  virtual child_iterator child_end();
1640};
1641
1642/// \brief Describes an explicit type conversion that uses functional
1643/// notion but could not be resolved because one or more arguments are
1644/// type-dependent.
1645///
1646/// The explicit type conversions expressed by
1647/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN),
1648/// where \c T is some type and \c a1, a2, ..., aN are values, and
1649/// either \C T is a dependent type or one or more of the \c a's is
1650/// type-dependent. For example, this would occur in a template such
1651/// as:
1652///
1653/// \code
1654///   template<typename T, typename A1>
1655///   inline T make_a(const A1& a1) {
1656///     return T(a1);
1657///   }
1658/// \endcode
1659///
1660/// When the returned expression is instantiated, it may resolve to a
1661/// constructor call, conversion function call, or some kind of type
1662/// conversion.
1663class CXXUnresolvedConstructExpr : public Expr {
1664  /// \brief The starting location of the type
1665  SourceLocation TyBeginLoc;
1666
1667  /// \brief The type being constructed.
1668  QualType Type;
1669
1670  /// \brief The location of the left parentheses ('(').
1671  SourceLocation LParenLoc;
1672
1673  /// \brief The location of the right parentheses (')').
1674  SourceLocation RParenLoc;
1675
1676  /// \brief The number of arguments used to construct the type.
1677  unsigned NumArgs;
1678
1679  CXXUnresolvedConstructExpr(SourceLocation TyBegin,
1680                             QualType T,
1681                             SourceLocation LParenLoc,
1682                             Expr **Args,
1683                             unsigned NumArgs,
1684                             SourceLocation RParenLoc);
1685
1686public:
1687  static CXXUnresolvedConstructExpr *Create(ASTContext &C,
1688                                            SourceLocation TyBegin,
1689                                            QualType T,
1690                                            SourceLocation LParenLoc,
1691                                            Expr **Args,
1692                                            unsigned NumArgs,
1693                                            SourceLocation RParenLoc);
1694
1695  /// \brief Retrieve the source location where the type begins.
1696  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
1697  void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; }
1698
1699  /// \brief Retrieve the type that is being constructed, as specified
1700  /// in the source code.
1701  QualType getTypeAsWritten() const { return Type; }
1702  void setTypeAsWritten(QualType T) { Type = T; }
1703
1704  /// \brief Retrieve the location of the left parentheses ('(') that
1705  /// precedes the argument list.
1706  SourceLocation getLParenLoc() const { return LParenLoc; }
1707  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1708
1709  /// \brief Retrieve the location of the right parentheses (')') that
1710  /// follows the argument list.
1711  SourceLocation getRParenLoc() const { return RParenLoc; }
1712  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1713
1714  /// \brief Retrieve the number of arguments.
1715  unsigned arg_size() const { return NumArgs; }
1716
1717  typedef Expr** arg_iterator;
1718  arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); }
1719  arg_iterator arg_end() { return arg_begin() + NumArgs; }
1720
1721  typedef const Expr* const * const_arg_iterator;
1722  const_arg_iterator arg_begin() const {
1723    return reinterpret_cast<const Expr* const *>(this + 1);
1724  }
1725  const_arg_iterator arg_end() const {
1726    return arg_begin() + NumArgs;
1727  }
1728
1729  Expr *getArg(unsigned I) {
1730    assert(I < NumArgs && "Argument index out-of-range");
1731    return *(arg_begin() + I);
1732  }
1733
1734  const Expr *getArg(unsigned I) const {
1735    assert(I < NumArgs && "Argument index out-of-range");
1736    return *(arg_begin() + I);
1737  }
1738
1739  virtual SourceRange getSourceRange() const {
1740    return SourceRange(TyBeginLoc, RParenLoc);
1741  }
1742  static bool classof(const Stmt *T) {
1743    return T->getStmtClass() == CXXUnresolvedConstructExprClass;
1744  }
1745  static bool classof(const CXXUnresolvedConstructExpr *) { return true; }
1746
1747  // Iterators
1748  virtual child_iterator child_begin();
1749  virtual child_iterator child_end();
1750};
1751
1752/// \brief Represents a C++ member access expression where the actual
1753/// member referenced could not be resolved because the base
1754/// expression or the member name was dependent.
1755///
1756/// Like UnresolvedMemberExprs, these can be either implicit or
1757/// explicit accesses.  It is only possible to get one of these with
1758/// an implicit access if a qualifier is provided.
1759class CXXDependentScopeMemberExpr : public Expr {
1760  /// \brief The expression for the base pointer or class reference,
1761  /// e.g., the \c x in x.f.  Can be null in implicit accesses.
1762  Stmt *Base;
1763
1764  /// \brief The type of the base expression.  Never null, even for
1765  /// implicit accesses.
1766  QualType BaseType;
1767
1768  /// \brief Whether this member expression used the '->' operator or
1769  /// the '.' operator.
1770  bool IsArrow : 1;
1771
1772  /// \brief Whether this member expression has explicitly-specified template
1773  /// arguments.
1774  bool HasExplicitTemplateArgs : 1;
1775
1776  /// \brief The location of the '->' or '.' operator.
1777  SourceLocation OperatorLoc;
1778
1779  /// \brief The nested-name-specifier that precedes the member name, if any.
1780  NestedNameSpecifier *Qualifier;
1781
1782  /// \brief The source range covering the nested name specifier.
1783  SourceRange QualifierRange;
1784
1785  /// \brief In a qualified member access expression such as t->Base::f, this
1786  /// member stores the resolves of name lookup in the context of the member
1787  /// access expression, to be used at instantiation time.
1788  ///
1789  /// FIXME: This member, along with the Qualifier and QualifierRange, could
1790  /// be stuck into a structure that is optionally allocated at the end of
1791  /// the CXXDependentScopeMemberExpr, to save space in the common case.
1792  NamedDecl *FirstQualifierFoundInScope;
1793
1794  /// \brief The member to which this member expression refers, which
1795  /// can be name, overloaded operator, or destructor.
1796  /// FIXME: could also be a template-id
1797  DeclarationName Member;
1798
1799  /// \brief The location of the member name.
1800  SourceLocation MemberLoc;
1801
1802  /// \brief Retrieve the explicit template argument list that followed the
1803  /// member template name, if any.
1804  ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() {
1805    assert(HasExplicitTemplateArgs);
1806    return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
1807  }
1808
1809  /// \brief Retrieve the explicit template argument list that followed the
1810  /// member template name, if any.
1811  const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const {
1812    return const_cast<CXXDependentScopeMemberExpr *>(this)
1813             ->getExplicitTemplateArgumentList();
1814  }
1815
1816  CXXDependentScopeMemberExpr(ASTContext &C,
1817                          Expr *Base, QualType BaseType, bool IsArrow,
1818                          SourceLocation OperatorLoc,
1819                          NestedNameSpecifier *Qualifier,
1820                          SourceRange QualifierRange,
1821                          NamedDecl *FirstQualifierFoundInScope,
1822                          DeclarationName Member,
1823                          SourceLocation MemberLoc,
1824                          const TemplateArgumentListInfo *TemplateArgs);
1825
1826public:
1827  CXXDependentScopeMemberExpr(ASTContext &C,
1828                          Expr *Base, QualType BaseType,
1829                          bool IsArrow,
1830                          SourceLocation OperatorLoc,
1831                          NestedNameSpecifier *Qualifier,
1832                          SourceRange QualifierRange,
1833                          NamedDecl *FirstQualifierFoundInScope,
1834                          DeclarationName Member,
1835                          SourceLocation MemberLoc)
1836  : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, true, true),
1837    Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1838    HasExplicitTemplateArgs(false), OperatorLoc(OperatorLoc),
1839    Qualifier(Qualifier), QualifierRange(QualifierRange),
1840    FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1841    Member(Member), MemberLoc(MemberLoc) { }
1842
1843  static CXXDependentScopeMemberExpr *
1844  Create(ASTContext &C,
1845         Expr *Base, QualType BaseType, bool IsArrow,
1846         SourceLocation OperatorLoc,
1847         NestedNameSpecifier *Qualifier,
1848         SourceRange QualifierRange,
1849         NamedDecl *FirstQualifierFoundInScope,
1850         DeclarationName Member,
1851         SourceLocation MemberLoc,
1852         const TemplateArgumentListInfo *TemplateArgs);
1853
1854  /// \brief True if this is an implicit access, i.e. one in which the
1855  /// member being accessed was not written in the source.  The source
1856  /// location of the operator is invalid in this case.
1857  bool isImplicitAccess() const { return Base == 0; }
1858
1859  /// \brief Retrieve the base object of this member expressions,
1860  /// e.g., the \c x in \c x.m.
1861  Expr *getBase() const {
1862    assert(!isImplicitAccess());
1863    return cast<Expr>(Base);
1864  }
1865  void setBase(Expr *E) { Base = E; }
1866
1867  QualType getBaseType() const { return BaseType; }
1868
1869  /// \brief Determine whether this member expression used the '->'
1870  /// operator; otherwise, it used the '.' operator.
1871  bool isArrow() const { return IsArrow; }
1872  void setArrow(bool A) { IsArrow = A; }
1873
1874  /// \brief Retrieve the location of the '->' or '.' operator.
1875  SourceLocation getOperatorLoc() const { return OperatorLoc; }
1876  void setOperatorLoc(SourceLocation L) { OperatorLoc = L; }
1877
1878  /// \brief Retrieve the nested-name-specifier that qualifies the member
1879  /// name.
1880  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1881
1882  /// \brief Retrieve the source range covering the nested-name-specifier
1883  /// that qualifies the member name.
1884  SourceRange getQualifierRange() const { return QualifierRange; }
1885
1886  /// \brief Retrieve the first part of the nested-name-specifier that was
1887  /// found in the scope of the member access expression when the member access
1888  /// was initially parsed.
1889  ///
1890  /// This function only returns a useful result when member access expression
1891  /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration
1892  /// returned by this function describes what was found by unqualified name
1893  /// lookup for the identifier "Base" within the scope of the member access
1894  /// expression itself. At template instantiation time, this information is
1895  /// combined with the results of name lookup into the type of the object
1896  /// expression itself (the class type of x).
1897  NamedDecl *getFirstQualifierFoundInScope() const {
1898    return FirstQualifierFoundInScope;
1899  }
1900
1901  /// \brief Retrieve the name of the member that this expression
1902  /// refers to.
1903  DeclarationName getMember() const { return Member; }
1904  void setMember(DeclarationName N) { Member = N; }
1905
1906  // \brief Retrieve the location of the name of the member that this
1907  // expression refers to.
1908  SourceLocation getMemberLoc() const { return MemberLoc; }
1909  void setMemberLoc(SourceLocation L) { MemberLoc = L; }
1910
1911  /// \brief Determines whether this member expression actually had a C++
1912  /// template argument list explicitly specified, e.g., x.f<int>.
1913  bool hasExplicitTemplateArgs() const {
1914    return HasExplicitTemplateArgs;
1915  }
1916
1917  /// \brief Copies the template arguments (if present) into the given
1918  /// structure.
1919  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1920    assert(HasExplicitTemplateArgs);
1921    getExplicitTemplateArgumentList()->copyInto(List);
1922  }
1923
1924  /// \brief Retrieve the location of the left angle bracket following the
1925  /// member name ('<'), if any.
1926  SourceLocation getLAngleLoc() const {
1927    assert(HasExplicitTemplateArgs);
1928    return getExplicitTemplateArgumentList()->LAngleLoc;
1929  }
1930
1931  /// \brief Retrieve the template arguments provided as part of this
1932  /// template-id.
1933  const TemplateArgumentLoc *getTemplateArgs() const {
1934    assert(HasExplicitTemplateArgs);
1935    return getExplicitTemplateArgumentList()->getTemplateArgs();
1936  }
1937
1938  /// \brief Retrieve the number of template arguments provided as part of this
1939  /// template-id.
1940  unsigned getNumTemplateArgs() const {
1941    assert(HasExplicitTemplateArgs);
1942    return getExplicitTemplateArgumentList()->NumTemplateArgs;
1943  }
1944
1945  /// \brief Retrieve the location of the right angle bracket following the
1946  /// template arguments ('>').
1947  SourceLocation getRAngleLoc() const {
1948    assert(HasExplicitTemplateArgs);
1949    return getExplicitTemplateArgumentList()->RAngleLoc;
1950  }
1951
1952  virtual SourceRange getSourceRange() const {
1953    SourceRange Range;
1954    if (!isImplicitAccess())
1955      Range.setBegin(Base->getSourceRange().getBegin());
1956    else if (getQualifier())
1957      Range.setBegin(getQualifierRange().getBegin());
1958    else
1959      Range.setBegin(MemberLoc);
1960
1961    if (hasExplicitTemplateArgs())
1962      Range.setEnd(getRAngleLoc());
1963    else
1964      Range.setEnd(MemberLoc);
1965    return Range;
1966  }
1967
1968  static bool classof(const Stmt *T) {
1969    return T->getStmtClass() == CXXDependentScopeMemberExprClass;
1970  }
1971  static bool classof(const CXXDependentScopeMemberExpr *) { return true; }
1972
1973  // Iterators
1974  virtual child_iterator child_begin();
1975  virtual child_iterator child_end();
1976};
1977
1978/// \brief Represents a C++ member access expression for which lookup
1979/// produced a set of overloaded functions.
1980///
1981/// The member access may be explicit or implicit:
1982///    struct A {
1983///      int a, b;
1984///      int explicitAccess() { return this->a + this->A::b; }
1985///      int implicitAccess() { return a + A::b; }
1986///    };
1987///
1988/// In the final AST, an explicit access always becomes a MemberExpr.
1989/// An implicit access may become either a MemberExpr or a
1990/// DeclRefExpr, depending on whether the member is static.
1991class UnresolvedMemberExpr : public OverloadExpr {
1992  /// \brief Whether this member expression used the '->' operator or
1993  /// the '.' operator.
1994  bool IsArrow : 1;
1995
1996  /// \brief Whether the lookup results contain an unresolved using
1997  /// declaration.
1998  bool HasUnresolvedUsing : 1;
1999
2000  /// \brief The expression for the base pointer or class reference,
2001  /// e.g., the \c x in x.f.  This can be null if this is an 'unbased'
2002  /// member expression
2003  Stmt *Base;
2004
2005  /// \brief The type of the base expression;  never null.
2006  QualType BaseType;
2007
2008  /// \brief The location of the '->' or '.' operator.
2009  SourceLocation OperatorLoc;
2010
2011  UnresolvedMemberExpr(QualType T, bool Dependent,
2012                       bool HasUnresolvedUsing,
2013                       Expr *Base, QualType BaseType, bool IsArrow,
2014                       SourceLocation OperatorLoc,
2015                       NestedNameSpecifier *Qualifier,
2016                       SourceRange QualifierRange,
2017                       DeclarationName Member,
2018                       SourceLocation MemberLoc,
2019                       const TemplateArgumentListInfo *TemplateArgs);
2020
2021public:
2022  static UnresolvedMemberExpr *
2023  Create(ASTContext &C, bool Dependent, bool HasUnresolvedUsing,
2024         Expr *Base, QualType BaseType, bool IsArrow,
2025         SourceLocation OperatorLoc,
2026         NestedNameSpecifier *Qualifier,
2027         SourceRange QualifierRange,
2028         DeclarationName Member,
2029         SourceLocation MemberLoc,
2030         const TemplateArgumentListInfo *TemplateArgs);
2031
2032  /// \brief True if this is an implicit access, i.e. one in which the
2033  /// member being accessed was not written in the source.  The source
2034  /// location of the operator is invalid in this case.
2035  bool isImplicitAccess() const { return Base == 0; }
2036
2037  /// \brief Retrieve the base object of this member expressions,
2038  /// e.g., the \c x in \c x.m.
2039  Expr *getBase() {
2040    assert(!isImplicitAccess());
2041    return cast<Expr>(Base);
2042  }
2043  const Expr *getBase() const {
2044    assert(!isImplicitAccess());
2045    return cast<Expr>(Base);
2046  }
2047  void setBase(Expr *E) { Base = E; }
2048
2049  QualType getBaseType() const { return BaseType; }
2050
2051  /// \brief Determine whether this member expression used the '->'
2052  /// operator; otherwise, it used the '.' operator.
2053  bool isArrow() const { return IsArrow; }
2054  void setArrow(bool A) { IsArrow = A; }
2055
2056  /// \brief Retrieve the location of the '->' or '.' operator.
2057  SourceLocation getOperatorLoc() const { return OperatorLoc; }
2058  void setOperatorLoc(SourceLocation L) { OperatorLoc = L; }
2059
2060  /// \brief Retrieves the naming class of this lookup.
2061  CXXRecordDecl *getNamingClass() const;
2062
2063  /// \brief Retrieve the name of the member that this expression
2064  /// refers to.
2065  DeclarationName getMemberName() const { return getName(); }
2066  void setMemberName(DeclarationName N) { setName(N); }
2067
2068  // \brief Retrieve the location of the name of the member that this
2069  // expression refers to.
2070  SourceLocation getMemberLoc() const { return getNameLoc(); }
2071  void setMemberLoc(SourceLocation L) { setNameLoc(L); }
2072
2073  /// \brief Retrieve the explicit template argument list that followed the
2074  /// member template name.
2075  ExplicitTemplateArgumentList &getExplicitTemplateArgs() {
2076    assert(hasExplicitTemplateArgs());
2077    return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
2078  }
2079
2080  /// \brief Retrieve the explicit template argument list that followed the
2081  /// member template name, if any.
2082  const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const {
2083    assert(hasExplicitTemplateArgs());
2084    return *reinterpret_cast<const ExplicitTemplateArgumentList *>(this + 1);
2085  }
2086
2087  /// \brief Copies the template arguments into the given structure.
2088  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
2089    getExplicitTemplateArgs().copyInto(List);
2090  }
2091
2092  /// \brief Retrieve the location of the left angle bracket following
2093  /// the member name ('<').
2094  SourceLocation getLAngleLoc() const {
2095    return getExplicitTemplateArgs().LAngleLoc;
2096  }
2097
2098  /// \brief Retrieve the template arguments provided as part of this
2099  /// template-id.
2100  const TemplateArgumentLoc *getTemplateArgs() const {
2101    return getExplicitTemplateArgs().getTemplateArgs();
2102  }
2103
2104  /// \brief Retrieve the number of template arguments provided as
2105  /// part of this template-id.
2106  unsigned getNumTemplateArgs() const {
2107    return getExplicitTemplateArgs().NumTemplateArgs;
2108  }
2109
2110  /// \brief Retrieve the location of the right angle bracket
2111  /// following the template arguments ('>').
2112  SourceLocation getRAngleLoc() const {
2113    return getExplicitTemplateArgs().RAngleLoc;
2114  }
2115
2116  virtual SourceRange getSourceRange() const {
2117    SourceRange Range;
2118    if (!isImplicitAccess())
2119      Range.setBegin(Base->getSourceRange().getBegin());
2120    else if (getQualifier())
2121      Range.setBegin(getQualifierRange().getBegin());
2122    else
2123      Range.setBegin(getMemberLoc());
2124
2125    if (hasExplicitTemplateArgs())
2126      Range.setEnd(getRAngleLoc());
2127    else
2128      Range.setEnd(getMemberLoc());
2129    return Range;
2130  }
2131
2132  static bool classof(const Stmt *T) {
2133    return T->getStmtClass() == UnresolvedMemberExprClass;
2134  }
2135  static bool classof(const UnresolvedMemberExpr *) { return true; }
2136
2137  // Iterators
2138  virtual child_iterator child_begin();
2139  virtual child_iterator child_end();
2140};
2141
2142inline ExplicitTemplateArgumentList &OverloadExpr::getExplicitTemplateArgs() {
2143  if (isa<UnresolvedLookupExpr>(this))
2144    return cast<UnresolvedLookupExpr>(this)->getExplicitTemplateArgs();
2145  else
2146    return cast<UnresolvedMemberExpr>(this)->getExplicitTemplateArgs();
2147}
2148
2149}  // end namespace clang
2150
2151#endif
2152