ExprCXX.h revision 129e2df52ed7e0434b3f1cf1867fd6a5cb083ff6
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/Decl.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                   QualType writtenTy, SourceLocation l)
122    : ExplicitCastExpr(SC, ty, kind, op, writtenTy), Loc(l) {}
123
124public:
125  const char *getCastName() const;
126
127  /// \brief Retrieve the location of the cast operator keyword, e.g.,
128  /// "static_cast".
129  SourceLocation getOperatorLoc() const { return Loc; }
130  void setOperatorLoc(SourceLocation L) { Loc = L; }
131
132  virtual SourceRange getSourceRange() const {
133    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
134  }
135  static bool classof(const Stmt *T) {
136    switch (T->getStmtClass()) {
137    case CXXNamedCastExprClass:
138    case CXXStaticCastExprClass:
139    case CXXDynamicCastExprClass:
140    case CXXReinterpretCastExprClass:
141    case CXXConstCastExprClass:
142      return true;
143    default:
144      return false;
145    }
146  }
147  static bool classof(const CXXNamedCastExpr *) { return true; }
148};
149
150/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]).
151///
152/// This expression node represents a C++ static cast, e.g.,
153/// @c static_cast<int>(1.0).
154class CXXStaticCastExpr : public CXXNamedCastExpr {
155public:
156  CXXStaticCastExpr(QualType ty, CastKind kind, Expr *op,
157                    QualType writtenTy, SourceLocation l)
158    : CXXNamedCastExpr(CXXStaticCastExprClass, ty, kind, op, writtenTy, l) {}
159
160  static bool classof(const Stmt *T) {
161    return T->getStmtClass() == CXXStaticCastExprClass;
162  }
163  static bool classof(const CXXStaticCastExpr *) { return true; }
164};
165
166/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression
167/// (C++ [expr.dynamic.cast]), which may perform a run-time check to
168/// determine how to perform the type cast.
169///
170/// This expression node represents a dynamic cast, e.g.,
171/// @c dynamic_cast<Derived*>(BasePtr).
172class CXXDynamicCastExpr : public CXXNamedCastExpr {
173public:
174  CXXDynamicCastExpr(QualType ty, CastKind kind, Expr *op, QualType writtenTy,
175                     SourceLocation l)
176    : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, kind, op, writtenTy, l) {}
177
178  static bool classof(const Stmt *T) {
179    return T->getStmtClass() == CXXDynamicCastExprClass;
180  }
181  static bool classof(const CXXDynamicCastExpr *) { return true; }
182};
183
184/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++
185/// [expr.reinterpret.cast]), which provides a differently-typed view
186/// of a value but performs no actual work at run time.
187///
188/// This expression node represents a reinterpret cast, e.g.,
189/// @c reinterpret_cast<int>(VoidPtr).
190class CXXReinterpretCastExpr : public CXXNamedCastExpr {
191public:
192  CXXReinterpretCastExpr(QualType ty, CastKind kind, Expr *op,
193                         QualType writtenTy, SourceLocation l)
194    : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, kind, op,
195                       writtenTy, l) {}
196
197  static bool classof(const Stmt *T) {
198    return T->getStmtClass() == CXXReinterpretCastExprClass;
199  }
200  static bool classof(const CXXReinterpretCastExpr *) { return true; }
201};
202
203/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]),
204/// which can remove type qualifiers but does not change the underlying value.
205///
206/// This expression node represents a const cast, e.g.,
207/// @c const_cast<char*>(PtrToConstChar).
208class CXXConstCastExpr : public CXXNamedCastExpr {
209public:
210  CXXConstCastExpr(QualType ty, Expr *op, QualType writtenTy,
211                   SourceLocation l)
212    : CXXNamedCastExpr(CXXConstCastExprClass, ty, CK_NoOp, op, writtenTy, l) {}
213
214  static bool classof(const Stmt *T) {
215    return T->getStmtClass() == CXXConstCastExprClass;
216  }
217  static bool classof(const CXXConstCastExpr *) { return true; }
218};
219
220/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal.
221///
222class CXXBoolLiteralExpr : public Expr {
223  bool Value;
224  SourceLocation Loc;
225public:
226  CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) :
227    Expr(CXXBoolLiteralExprClass, Ty), Value(val), Loc(l) {}
228
229  bool getValue() const { return Value; }
230
231  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
232
233  static bool classof(const Stmt *T) {
234    return T->getStmtClass() == CXXBoolLiteralExprClass;
235  }
236  static bool classof(const CXXBoolLiteralExpr *) { return true; }
237
238  // Iterators
239  virtual child_iterator child_begin();
240  virtual child_iterator child_end();
241};
242
243/// CXXNullPtrLiteralExpr - [C++0x 2.14.7] C++ Pointer Literal
244class CXXNullPtrLiteralExpr : public Expr {
245  SourceLocation Loc;
246public:
247  CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l) :
248    Expr(CXXNullPtrLiteralExprClass, Ty), Loc(l) {}
249
250  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
251
252  static bool classof(const Stmt *T) {
253    return T->getStmtClass() == CXXNullPtrLiteralExprClass;
254  }
255  static bool classof(const CXXNullPtrLiteralExpr *) { return true; }
256
257  virtual child_iterator child_begin();
258  virtual child_iterator child_end();
259};
260
261/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets
262/// the type_info that corresponds to the supplied type, or the (possibly
263/// dynamic) type of the supplied expression.
264///
265/// This represents code like @c typeid(int) or @c typeid(*objPtr)
266class CXXTypeidExpr : public Expr {
267private:
268  bool isTypeOp : 1;
269  union {
270    void *Ty;
271    Stmt *Ex;
272  } Operand;
273  SourceRange Range;
274
275public:
276  CXXTypeidExpr(bool isTypeOp, void *op, QualType Ty, const SourceRange r) :
277      Expr(CXXTypeidExprClass, Ty,
278        // typeid is never type-dependent (C++ [temp.dep.expr]p4)
279        false,
280        // typeid is value-dependent if the type or expression are dependent
281        (isTypeOp ? QualType::getFromOpaquePtr(op)->isDependentType()
282                  : static_cast<Expr*>(op)->isValueDependent())),
283      isTypeOp(isTypeOp), Range(r) {
284    if (isTypeOp)
285      Operand.Ty = op;
286    else
287      // op was an Expr*, so cast it back to that to be safe
288      Operand.Ex = static_cast<Expr*>(op);
289  }
290
291  bool isTypeOperand() const { return isTypeOp; }
292  QualType getTypeOperand() const {
293    assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
294    return QualType::getFromOpaquePtr(Operand.Ty);
295  }
296  Expr* getExprOperand() const {
297    assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
298    return static_cast<Expr*>(Operand.Ex);
299  }
300
301  virtual SourceRange getSourceRange() const {
302    return Range;
303  }
304  static bool classof(const Stmt *T) {
305    return T->getStmtClass() == CXXTypeidExprClass;
306  }
307  static bool classof(const CXXTypeidExpr *) { return true; }
308
309  // Iterators
310  virtual child_iterator child_begin();
311  virtual child_iterator child_end();
312};
313
314/// CXXThisExpr - Represents the "this" expression in C++, which is a
315/// pointer to the object on which the current member function is
316/// executing (C++ [expr.prim]p3). Example:
317///
318/// @code
319/// class Foo {
320/// public:
321///   void bar();
322///   void test() { this->bar(); }
323/// };
324/// @endcode
325class CXXThisExpr : public Expr {
326  SourceLocation Loc;
327
328public:
329  CXXThisExpr(SourceLocation L, QualType Type)
330    : Expr(CXXThisExprClass, Type,
331           // 'this' is type-dependent if the class type of the enclosing
332           // member function is dependent (C++ [temp.dep.expr]p2)
333           Type->isDependentType(), Type->isDependentType()),
334      Loc(L) { }
335
336  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
337
338  static bool classof(const Stmt *T) {
339    return T->getStmtClass() == CXXThisExprClass;
340  }
341  static bool classof(const CXXThisExpr *) { return true; }
342
343  // Iterators
344  virtual child_iterator child_begin();
345  virtual child_iterator child_end();
346};
347
348///  CXXThrowExpr - [C++ 15] C++ Throw Expression.  This handles
349///  'throw' and 'throw' assignment-expression.  When
350///  assignment-expression isn't present, Op will be null.
351///
352class CXXThrowExpr : public Expr {
353  Stmt *Op;
354  SourceLocation ThrowLoc;
355public:
356  // Ty is the void type which is used as the result type of the
357  // exepression.  The l is the location of the throw keyword.  expr
358  // can by null, if the optional expression to throw isn't present.
359  CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) :
360    Expr(CXXThrowExprClass, Ty, false, false), Op(expr), ThrowLoc(l) {}
361  const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); }
362  Expr *getSubExpr() { return cast_or_null<Expr>(Op); }
363  void setSubExpr(Expr *E) { Op = E; }
364
365  SourceLocation getThrowLoc() const { return ThrowLoc; }
366  void setThrowLoc(SourceLocation L) { ThrowLoc = L; }
367
368  virtual SourceRange getSourceRange() const {
369    if (getSubExpr() == 0)
370      return SourceRange(ThrowLoc, ThrowLoc);
371    return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd());
372  }
373
374  static bool classof(const Stmt *T) {
375    return T->getStmtClass() == CXXThrowExprClass;
376  }
377  static bool classof(const CXXThrowExpr *) { return true; }
378
379  // Iterators
380  virtual child_iterator child_begin();
381  virtual child_iterator child_end();
382};
383
384/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a
385/// function call argument that was created from the corresponding
386/// parameter's default argument, when the call did not explicitly
387/// supply arguments for all of the parameters.
388class CXXDefaultArgExpr : public Expr {
389  ParmVarDecl *Param;
390
391protected:
392  CXXDefaultArgExpr(StmtClass SC, ParmVarDecl *param)
393    : Expr(SC, param->hasUnparsedDefaultArg() ?
394           param->getType().getNonReferenceType()
395           : param->getDefaultArg()->getType()),
396    Param(param) { }
397
398public:
399  // Param is the parameter whose default argument is used by this
400  // expression.
401  static CXXDefaultArgExpr *Create(ASTContext &C, ParmVarDecl *Param) {
402    return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Param);
403  }
404
405  // Retrieve the parameter that the argument was created from.
406  const ParmVarDecl *getParam() const { return Param; }
407  ParmVarDecl *getParam() { return Param; }
408
409  // Retrieve the actual argument to the function call.
410  const Expr *getExpr() const { return Param->getDefaultArg(); }
411  Expr *getExpr() { return Param->getDefaultArg(); }
412
413  virtual SourceRange getSourceRange() const {
414    // Default argument expressions have no representation in the
415    // source, so they have an empty source range.
416    return SourceRange();
417  }
418
419  static bool classof(const Stmt *T) {
420    return T->getStmtClass() == CXXDefaultArgExprClass;
421  }
422  static bool classof(const CXXDefaultArgExpr *) { return true; }
423
424  // Iterators
425  virtual child_iterator child_begin();
426  virtual child_iterator child_end();
427};
428
429/// CXXTemporary - Represents a C++ temporary.
430class CXXTemporary {
431  /// Destructor - The destructor that needs to be called.
432  const CXXDestructorDecl *Destructor;
433
434  CXXTemporary(const CXXDestructorDecl *destructor)
435    : Destructor(destructor) { }
436  ~CXXTemporary() { }
437
438public:
439  static CXXTemporary *Create(ASTContext &C,
440                              const CXXDestructorDecl *Destructor);
441
442  void Destroy(ASTContext &Ctx);
443
444  const CXXDestructorDecl *getDestructor() const { return Destructor; }
445};
446
447/// CXXBindTemporaryExpr - Represents binding an expression to a temporary,
448/// so its destructor can be called later.
449class CXXBindTemporaryExpr : public Expr {
450  CXXTemporary *Temp;
451
452  Stmt *SubExpr;
453
454  CXXBindTemporaryExpr(CXXTemporary *temp, Expr* subexpr)
455   : Expr(CXXBindTemporaryExprClass,
456          subexpr->getType()), Temp(temp), SubExpr(subexpr) { }
457  ~CXXBindTemporaryExpr() { }
458
459protected:
460  virtual void DoDestroy(ASTContext &C);
461
462public:
463  static CXXBindTemporaryExpr *Create(ASTContext &C, CXXTemporary *Temp,
464                                      Expr* SubExpr);
465
466  CXXTemporary *getTemporary() { return Temp; }
467  const CXXTemporary *getTemporary() const { return Temp; }
468
469  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
470  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
471  void setSubExpr(Expr *E) { SubExpr = E; }
472
473  virtual SourceRange getSourceRange() const {
474    return SubExpr->getSourceRange();
475  }
476
477  // Implement isa/cast/dyncast/etc.
478  static bool classof(const Stmt *T) {
479    return T->getStmtClass() == CXXBindTemporaryExprClass;
480  }
481  static bool classof(const CXXBindTemporaryExpr *) { return true; }
482
483  // Iterators
484  virtual child_iterator child_begin();
485  virtual child_iterator child_end();
486};
487
488/// CXXConstructExpr - Represents a call to a C++ constructor.
489class CXXConstructExpr : public Expr {
490  CXXConstructorDecl *Constructor;
491
492  bool Elidable;
493
494  Stmt **Args;
495  unsigned NumArgs;
496
497protected:
498  CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
499                   CXXConstructorDecl *d, bool elidable,
500                   Expr **args, unsigned numargs);
501  ~CXXConstructExpr() { }
502
503  virtual void DoDestroy(ASTContext &C);
504
505public:
506  /// \brief Construct an empty C++ construction expression that will store
507  /// \p numargs arguments.
508  CXXConstructExpr(EmptyShell Empty, ASTContext &C, unsigned numargs);
509
510  static CXXConstructExpr *Create(ASTContext &C, QualType T,
511                                  CXXConstructorDecl *D, bool Elidable,
512                                  Expr **Args, unsigned NumArgs);
513
514
515  CXXConstructorDecl* getConstructor() const { return Constructor; }
516  void setConstructor(CXXConstructorDecl *C) { Constructor = C; }
517
518  /// \brief Whether this construction is elidable.
519  bool isElidable() const { return Elidable; }
520  void setElidable(bool E) { Elidable = E; }
521
522  typedef ExprIterator arg_iterator;
523  typedef ConstExprIterator const_arg_iterator;
524
525  arg_iterator arg_begin() { return Args; }
526  arg_iterator arg_end() { return Args + NumArgs; }
527  const_arg_iterator arg_begin() const { return Args; }
528  const_arg_iterator arg_end() const { return Args + NumArgs; }
529
530  unsigned getNumArgs() const { return NumArgs; }
531
532  /// getArg - Return the specified argument.
533  Expr *getArg(unsigned Arg) {
534    assert(Arg < NumArgs && "Arg access out of range!");
535    return cast<Expr>(Args[Arg]);
536  }
537  const Expr *getArg(unsigned Arg) const {
538    assert(Arg < NumArgs && "Arg access out of range!");
539    return cast<Expr>(Args[Arg]);
540  }
541
542  /// setArg - Set the specified argument.
543  void setArg(unsigned Arg, Expr *ArgExpr) {
544    assert(Arg < NumArgs && "Arg access out of range!");
545    Args[Arg] = ArgExpr;
546  }
547
548  virtual SourceRange getSourceRange() const {
549    // FIXME: Should we know where the parentheses are, if there are any?
550    if (NumArgs == 0)
551      return SourceRange();
552
553    return SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
554  }
555
556  static bool classof(const Stmt *T) {
557    return T->getStmtClass() == CXXConstructExprClass ||
558      T->getStmtClass() == CXXTemporaryObjectExprClass;
559  }
560  static bool classof(const CXXConstructExpr *) { return true; }
561
562  // Iterators
563  virtual child_iterator child_begin();
564  virtual child_iterator child_end();
565};
566
567/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion
568/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c
569/// x = int(0.5);
570class CXXFunctionalCastExpr : public ExplicitCastExpr {
571  SourceLocation TyBeginLoc;
572  SourceLocation RParenLoc;
573public:
574  CXXFunctionalCastExpr(QualType ty, QualType writtenTy,
575                        SourceLocation tyBeginLoc, CastKind kind,
576                        Expr *castExpr, SourceLocation rParenLoc)
577    : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, kind, castExpr,
578                       writtenTy),
579      TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
580
581  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
582  SourceLocation getRParenLoc() const { return RParenLoc; }
583
584  virtual SourceRange getSourceRange() const {
585    return SourceRange(TyBeginLoc, RParenLoc);
586  }
587  static bool classof(const Stmt *T) {
588    return T->getStmtClass() == CXXFunctionalCastExprClass;
589  }
590  static bool classof(const CXXFunctionalCastExpr *) { return true; }
591};
592
593/// @brief Represents a C++ functional cast expression that builds a
594/// temporary object.
595///
596/// This expression type represents a C++ "functional" cast
597/// (C++[expr.type.conv]) with N != 1 arguments that invokes a
598/// constructor to build a temporary object. If N == 0 but no
599/// constructor will be called (because the functional cast is
600/// performing a value-initialized an object whose class type has no
601/// user-declared constructors), CXXZeroInitValueExpr will represent
602/// the functional cast. Finally, with N == 1 arguments the functional
603/// cast expression will be represented by CXXFunctionalCastExpr.
604/// Example:
605/// @code
606/// struct X { X(int, float); }
607///
608/// X create_X() {
609///   return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
610/// };
611/// @endcode
612class CXXTemporaryObjectExpr : public CXXConstructExpr {
613  SourceLocation TyBeginLoc;
614  SourceLocation RParenLoc;
615
616public:
617  CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons,
618                         QualType writtenTy, SourceLocation tyBeginLoc,
619                         Expr **Args,unsigned NumArgs,
620                         SourceLocation rParenLoc);
621
622  ~CXXTemporaryObjectExpr() { }
623
624  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
625  SourceLocation getRParenLoc() const { return RParenLoc; }
626
627  virtual SourceRange getSourceRange() const {
628    return SourceRange(TyBeginLoc, RParenLoc);
629  }
630  static bool classof(const Stmt *T) {
631    return T->getStmtClass() == CXXTemporaryObjectExprClass;
632  }
633  static bool classof(const CXXTemporaryObjectExpr *) { return true; }
634};
635
636/// CXXZeroInitValueExpr - [C++ 5.2.3p2]
637/// Expression "T()" which creates a value-initialized rvalue of type
638/// T, which is either a non-class type or a class type without any
639/// user-defined constructors.
640///
641class CXXZeroInitValueExpr : public Expr {
642  SourceLocation TyBeginLoc;
643  SourceLocation RParenLoc;
644
645public:
646  CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc,
647                       SourceLocation rParenLoc ) :
648    Expr(CXXZeroInitValueExprClass, ty, false, false),
649    TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
650
651  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
652  SourceLocation getRParenLoc() const { return RParenLoc; }
653
654  /// @brief Whether this initialization expression was
655  /// implicitly-generated.
656  bool isImplicit() const {
657    return TyBeginLoc.isInvalid() && RParenLoc.isInvalid();
658  }
659
660  virtual SourceRange getSourceRange() const {
661    return SourceRange(TyBeginLoc, RParenLoc);
662  }
663
664  static bool classof(const Stmt *T) {
665    return T->getStmtClass() == CXXZeroInitValueExprClass;
666  }
667  static bool classof(const CXXZeroInitValueExpr *) { return true; }
668
669  // Iterators
670  virtual child_iterator child_begin();
671  virtual child_iterator child_end();
672};
673
674/// CXXNewExpr - A new expression for memory allocation and constructor calls,
675/// e.g: "new CXXNewExpr(foo)".
676class CXXNewExpr : public Expr {
677  // Was the usage ::new, i.e. is the global new to be used?
678  bool GlobalNew : 1;
679  // Was the form (type-id) used? Otherwise, it was new-type-id.
680  bool ParenTypeId : 1;
681  // Is there an initializer? If not, built-ins are uninitialized, else they're
682  // value-initialized.
683  bool Initializer : 1;
684  // Do we allocate an array? If so, the first SubExpr is the size expression.
685  bool Array : 1;
686  // The number of placement new arguments.
687  unsigned NumPlacementArgs : 14;
688  // The number of constructor arguments. This may be 1 even for non-class
689  // types; use the pseudo copy constructor.
690  unsigned NumConstructorArgs : 14;
691  // Contains an optional array size expression, any number of optional
692  // placement arguments, and any number of optional constructor arguments,
693  // in that order.
694  Stmt **SubExprs;
695  // Points to the allocation function used.
696  FunctionDecl *OperatorNew;
697  // Points to the deallocation function used in case of error. May be null.
698  FunctionDecl *OperatorDelete;
699  // Points to the constructor used. Cannot be null if AllocType is a record;
700  // it would still point at the default constructor (even an implicit one).
701  // Must be null for all other types.
702  CXXConstructorDecl *Constructor;
703
704  SourceLocation StartLoc;
705  SourceLocation EndLoc;
706
707public:
708  CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, Expr **placementArgs,
709             unsigned numPlaceArgs, bool ParenTypeId, Expr *arraySize,
710             CXXConstructorDecl *constructor, bool initializer,
711             Expr **constructorArgs, unsigned numConsArgs,
712             FunctionDecl *operatorDelete, QualType ty,
713             SourceLocation startLoc, SourceLocation endLoc);
714  ~CXXNewExpr() {
715    delete[] SubExprs;
716  }
717
718  QualType getAllocatedType() const {
719    assert(getType()->isPointerType());
720    return getType()->getAs<PointerType>()->getPointeeType();
721  }
722
723  FunctionDecl *getOperatorNew() const { return OperatorNew; }
724  FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
725  CXXConstructorDecl *getConstructor() const { return Constructor; }
726
727  bool isArray() const { return Array; }
728  Expr *getArraySize() {
729    return Array ? cast<Expr>(SubExprs[0]) : 0;
730  }
731  const Expr *getArraySize() const {
732    return Array ? cast<Expr>(SubExprs[0]) : 0;
733  }
734
735  unsigned getNumPlacementArgs() const { return NumPlacementArgs; }
736  Expr *getPlacementArg(unsigned i) {
737    assert(i < NumPlacementArgs && "Index out of range");
738    return cast<Expr>(SubExprs[Array + i]);
739  }
740  const Expr *getPlacementArg(unsigned i) const {
741    assert(i < NumPlacementArgs && "Index out of range");
742    return cast<Expr>(SubExprs[Array + i]);
743  }
744
745  bool isGlobalNew() const { return GlobalNew; }
746  bool isParenTypeId() const { return ParenTypeId; }
747  bool hasInitializer() const { return Initializer; }
748
749  unsigned getNumConstructorArgs() const { return NumConstructorArgs; }
750  Expr *getConstructorArg(unsigned i) {
751    assert(i < NumConstructorArgs && "Index out of range");
752    return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
753  }
754  const Expr *getConstructorArg(unsigned i) const {
755    assert(i < NumConstructorArgs && "Index out of range");
756    return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
757  }
758
759  typedef ExprIterator arg_iterator;
760  typedef ConstExprIterator const_arg_iterator;
761
762  arg_iterator placement_arg_begin() {
763    return SubExprs + Array;
764  }
765  arg_iterator placement_arg_end() {
766    return SubExprs + Array + getNumPlacementArgs();
767  }
768  const_arg_iterator placement_arg_begin() const {
769    return SubExprs + Array;
770  }
771  const_arg_iterator placement_arg_end() const {
772    return SubExprs + Array + getNumPlacementArgs();
773  }
774
775  arg_iterator constructor_arg_begin() {
776    return SubExprs + Array + getNumPlacementArgs();
777  }
778  arg_iterator constructor_arg_end() {
779    return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs();
780  }
781  const_arg_iterator constructor_arg_begin() const {
782    return SubExprs + Array + getNumPlacementArgs();
783  }
784  const_arg_iterator constructor_arg_end() const {
785    return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs();
786  }
787
788  virtual SourceRange getSourceRange() const {
789    return SourceRange(StartLoc, EndLoc);
790  }
791
792  static bool classof(const Stmt *T) {
793    return T->getStmtClass() == CXXNewExprClass;
794  }
795  static bool classof(const CXXNewExpr *) { return true; }
796
797  // Iterators
798  virtual child_iterator child_begin();
799  virtual child_iterator child_end();
800};
801
802/// CXXDeleteExpr - A delete expression for memory deallocation and destructor
803/// calls, e.g. "delete[] pArray".
804class CXXDeleteExpr : public Expr {
805  // Is this a forced global delete, i.e. "::delete"?
806  bool GlobalDelete : 1;
807  // Is this the array form of delete, i.e. "delete[]"?
808  bool ArrayForm : 1;
809  // Points to the operator delete overload that is used. Could be a member.
810  FunctionDecl *OperatorDelete;
811  // The pointer expression to be deleted.
812  Stmt *Argument;
813  // Location of the expression.
814  SourceLocation Loc;
815public:
816  CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm,
817                FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc)
818    : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete),
819      ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg),
820      Loc(loc) { }
821
822  bool isGlobalDelete() const { return GlobalDelete; }
823  bool isArrayForm() const { return ArrayForm; }
824
825  FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
826
827  Expr *getArgument() { return cast<Expr>(Argument); }
828  const Expr *getArgument() const { return cast<Expr>(Argument); }
829
830  virtual SourceRange getSourceRange() const {
831    return SourceRange(Loc, Argument->getLocEnd());
832  }
833
834  static bool classof(const Stmt *T) {
835    return T->getStmtClass() == CXXDeleteExprClass;
836  }
837  static bool classof(const CXXDeleteExpr *) { return true; }
838
839  // Iterators
840  virtual child_iterator child_begin();
841  virtual child_iterator child_end();
842};
843
844/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
845///
846/// Example:
847///
848/// \code
849/// template<typename T>
850/// void destroy(T* ptr) {
851///   ptr->~T();
852/// }
853/// \endcode
854///
855/// When the template is parsed, the expression \c ptr->~T will be stored as
856/// a member reference expression. If it then instantiated with a scalar type
857/// as a template argument for T, the resulting expression will be a
858/// pseudo-destructor expression.
859class CXXPseudoDestructorExpr : public Expr {
860  /// \brief The base expression (that is being destroyed).
861  Stmt *Base;
862
863  /// \brief Whether the operator was an arrow ('->'); otherwise, it was a
864  /// period ('.').
865  bool IsArrow : 1;
866
867  /// \brief The location of the '.' or '->' operator.
868  SourceLocation OperatorLoc;
869
870  /// \brief The nested-name-specifier that follows the operator, if present.
871  NestedNameSpecifier *Qualifier;
872
873  /// \brief The source range that covers the nested-name-specifier, if
874  /// present.
875  SourceRange QualifierRange;
876
877  /// \brief The type being destroyed.
878  QualType DestroyedType;
879
880  /// \brief The location of the type after the '~'.
881  SourceLocation DestroyedTypeLoc;
882
883public:
884  CXXPseudoDestructorExpr(ASTContext &Context,
885                          Expr *Base, bool isArrow, SourceLocation OperatorLoc,
886                          NestedNameSpecifier *Qualifier,
887                          SourceRange QualifierRange,
888                          QualType DestroyedType,
889                          SourceLocation DestroyedTypeLoc)
890    : Expr(CXXPseudoDestructorExprClass,
891           Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
892                                                          false, 0)),
893           /*isTypeDependent=*/false,
894           /*isValueDependent=*/Base->isValueDependent()),
895      Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
896      OperatorLoc(OperatorLoc), Qualifier(Qualifier),
897      QualifierRange(QualifierRange), DestroyedType(DestroyedType),
898      DestroyedTypeLoc(DestroyedTypeLoc) { }
899
900  void setBase(Expr *E) { Base = E; }
901  Expr *getBase() const { return cast<Expr>(Base); }
902
903  /// \brief Determines whether this member expression actually had
904  /// a C++ nested-name-specifier prior to the name of the member, e.g.,
905  /// x->Base::foo.
906  bool hasQualifier() const { return Qualifier != 0; }
907
908  /// \brief If the member name was qualified, retrieves the source range of
909  /// the nested-name-specifier that precedes the member name. Otherwise,
910  /// returns an empty source range.
911  SourceRange getQualifierRange() const { return QualifierRange; }
912
913  /// \brief If the member name was qualified, retrieves the
914  /// nested-name-specifier that precedes the member name. Otherwise, returns
915  /// NULL.
916  NestedNameSpecifier *getQualifier() const { return Qualifier; }
917
918  /// \brief Determine whether this pseudo-destructor expression was written
919  /// using an '->' (otherwise, it used a '.').
920  bool isArrow() const { return IsArrow; }
921  void setArrow(bool A) { IsArrow = A; }
922
923  /// \brief Retrieve the location of the '.' or '->' operator.
924  SourceLocation getOperatorLoc() const { return OperatorLoc; }
925
926  /// \brief Retrieve the type that is being destroyed.
927  QualType getDestroyedType() const { return DestroyedType; }
928
929  /// \brief Retrieve the location of the type being destroyed.
930  SourceLocation getDestroyedTypeLoc() const { return DestroyedTypeLoc; }
931
932  virtual SourceRange getSourceRange() const {
933    return SourceRange(Base->getLocStart(), DestroyedTypeLoc);
934  }
935
936  static bool classof(const Stmt *T) {
937    return T->getStmtClass() == CXXPseudoDestructorExprClass;
938  }
939  static bool classof(const CXXPseudoDestructorExpr *) { return true; }
940
941  // Iterators
942  virtual child_iterator child_begin();
943  virtual child_iterator child_end();
944};
945
946/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the
947/// implementation of TR1/C++0x type trait templates.
948/// Example:
949/// __is_pod(int) == true
950/// __is_enum(std::string) == false
951class UnaryTypeTraitExpr : public Expr {
952  /// UTT - The trait.
953  UnaryTypeTrait UTT;
954
955  /// Loc - The location of the type trait keyword.
956  SourceLocation Loc;
957
958  /// RParen - The location of the closing paren.
959  SourceLocation RParen;
960
961  /// QueriedType - The type we're testing.
962  QualType QueriedType;
963
964public:
965  UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried,
966                     SourceLocation rparen, QualType ty)
967    : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()),
968      UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { }
969
970  virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);}
971
972  UnaryTypeTrait getTrait() const { return UTT; }
973
974  QualType getQueriedType() const { return QueriedType; }
975
976  bool EvaluateTrait(ASTContext&) const;
977
978  static bool classof(const Stmt *T) {
979    return T->getStmtClass() == UnaryTypeTraitExprClass;
980  }
981  static bool classof(const UnaryTypeTraitExpr *) { return true; }
982
983  // Iterators
984  virtual child_iterator child_begin();
985  virtual child_iterator child_end();
986};
987
988/// \brief A reference to a name which we were able to look up during
989/// parsing but could not resolve to a specific declaration.  This
990/// arises in several ways:
991///   * we might be waiting for argument-dependent lookup
992///   * the name might resolve to an overloaded function
993/// and eventually:
994///   * the lookup might have included a function template
995/// These never include UnresolvedUsingValueDecls, which are always
996/// class members and therefore appear only in
997/// UnresolvedMemberLookupExprs.
998class UnresolvedLookupExpr : public Expr {
999  /// The results.  These are undesugared, which is to say, they may
1000  /// include UsingShadowDecls.
1001  UnresolvedSet Results;
1002
1003  /// The name declared.
1004  DeclarationName Name;
1005
1006  /// The qualifier given, if any.
1007  NestedNameSpecifier *Qualifier;
1008
1009  /// The source range of the nested name specifier.
1010  SourceRange QualifierRange;
1011
1012  /// The location of the name.
1013  SourceLocation NameLoc;
1014
1015  /// True if these lookup results should be extended by
1016  /// argument-dependent lookup if this is the operand of a function
1017  /// call.
1018  bool RequiresADL;
1019
1020  /// True if these lookup results are overloaded.  This is pretty
1021  /// trivially rederivable if we urgently need to kill this field.
1022  bool Overloaded;
1023
1024  /// True if the name looked up had explicit template arguments.
1025  /// This requires all the results to be function templates.
1026  bool HasExplicitTemplateArgs;
1027
1028  UnresolvedLookupExpr(QualType T, bool Dependent,
1029                       NestedNameSpecifier *Qualifier, SourceRange QRange,
1030                       DeclarationName Name, SourceLocation NameLoc,
1031                       bool RequiresADL, bool Overloaded, bool HasTemplateArgs)
1032    : Expr(UnresolvedLookupExprClass, T, Dependent, Dependent),
1033      Name(Name), Qualifier(Qualifier), QualifierRange(QRange),
1034      NameLoc(NameLoc), RequiresADL(RequiresADL), Overloaded(Overloaded),
1035      HasExplicitTemplateArgs(HasTemplateArgs)
1036  {}
1037
1038public:
1039  static UnresolvedLookupExpr *Create(ASTContext &C,
1040                                      bool Dependent,
1041                                      NestedNameSpecifier *Qualifier,
1042                                      SourceRange QualifierRange,
1043                                      DeclarationName Name,
1044                                      SourceLocation NameLoc,
1045                                      bool ADL, bool Overloaded) {
1046    return new(C) UnresolvedLookupExpr(Dependent ? C.DependentTy : C.OverloadTy,
1047                                       Dependent, Qualifier, QualifierRange,
1048                                       Name, NameLoc, ADL, Overloaded, false);
1049  }
1050
1051  static UnresolvedLookupExpr *Create(ASTContext &C,
1052                                      bool Dependent,
1053                                      NestedNameSpecifier *Qualifier,
1054                                      SourceRange QualifierRange,
1055                                      DeclarationName Name,
1056                                      SourceLocation NameLoc,
1057                                      bool ADL,
1058                                      const TemplateArgumentListInfo &Args);
1059
1060  /// Computes whether an unresolved lookup on the given declarations
1061  /// and optional template arguments is type- and value-dependent.
1062  static bool ComputeDependence(NamedDecl * const *Begin,
1063                                NamedDecl * const *End,
1064                                const TemplateArgumentListInfo *Args);
1065
1066  void addDecl(NamedDecl *Decl) {
1067    Results.addDecl(Decl);
1068  }
1069
1070  typedef UnresolvedSet::iterator decls_iterator;
1071  decls_iterator decls_begin() const { return Results.begin(); }
1072  decls_iterator decls_end() const { return Results.end(); }
1073
1074  /// True if this declaration should be extended by
1075  /// argument-dependent lookup.
1076  bool requiresADL() const { return RequiresADL; }
1077
1078  /// True if this lookup is overloaded.
1079  bool isOverloaded() const { return Overloaded; }
1080
1081  /// Fetches the name looked up.
1082  DeclarationName getName() const { return Name; }
1083
1084  /// Gets the location of the name.
1085  SourceLocation getNameLoc() const { return NameLoc; }
1086
1087  /// Fetches the nested-name qualifier, if one was given.
1088  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1089
1090  /// Fetches the range of the nested-name qualifier.
1091  SourceRange getQualifierRange() const { return QualifierRange; }
1092
1093  /// Determines whether this lookup had explicit template arguments.
1094  bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
1095
1096  // Note that, inconsistently with the explicit-template-argument AST
1097  // nodes, users are *forbidden* from calling these methods on objects
1098  // without explicit template arguments.
1099
1100  /// Gets a reference to the explicit template argument list.
1101  const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const {
1102    assert(hasExplicitTemplateArgs());
1103    return *reinterpret_cast<const ExplicitTemplateArgumentList*>(this + 1);
1104  }
1105
1106  /// \brief Copies the template arguments (if present) into the given
1107  /// structure.
1108  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1109    getExplicitTemplateArgs().copyInto(List);
1110  }
1111
1112  SourceLocation getLAngleLoc() const {
1113    return getExplicitTemplateArgs().LAngleLoc;
1114  }
1115
1116  SourceLocation getRAngleLoc() const {
1117    return getExplicitTemplateArgs().RAngleLoc;
1118  }
1119
1120  TemplateArgumentLoc const *getTemplateArgs() const {
1121    return getExplicitTemplateArgs().getTemplateArgs();
1122  }
1123
1124  unsigned getNumTemplateArgs() const {
1125    return getExplicitTemplateArgs().NumTemplateArgs;
1126  }
1127
1128  virtual SourceRange getSourceRange() const {
1129    SourceRange Range(NameLoc);
1130    if (Qualifier) Range.setBegin(QualifierRange.getBegin());
1131    if (hasExplicitTemplateArgs()) Range.setEnd(getRAngleLoc());
1132    return Range;
1133  }
1134
1135  virtual StmtIterator child_begin();
1136  virtual StmtIterator child_end();
1137
1138  static bool classof(const Stmt *T) {
1139    return T->getStmtClass() == UnresolvedLookupExprClass;
1140  }
1141  static bool classof(const UnresolvedLookupExpr *) { return true; }
1142};
1143
1144/// \brief A qualified reference to a name whose declaration cannot
1145/// yet be resolved.
1146///
1147/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that
1148/// it expresses a reference to a declaration such as
1149/// X<T>::value. The difference, however, is that an
1150/// DependentScopeDeclRefExpr node is used only within C++ templates when
1151/// the qualification (e.g., X<T>::) refers to a dependent type. In
1152/// this case, X<T>::value cannot resolve to a declaration because the
1153/// declaration will differ from on instantiation of X<T> to the
1154/// next. Therefore, DependentScopeDeclRefExpr keeps track of the
1155/// qualifier (X<T>::) and the name of the entity being referenced
1156/// ("value"). Such expressions will instantiate to a DeclRefExpr once the
1157/// declaration can be found.
1158class DependentScopeDeclRefExpr : public Expr {
1159  /// The name of the entity we will be referencing.
1160  DeclarationName Name;
1161
1162  /// Location of the name of the declaration we're referencing.
1163  SourceLocation Loc;
1164
1165  /// QualifierRange - The source range that covers the
1166  /// nested-name-specifier.
1167  SourceRange QualifierRange;
1168
1169  /// \brief The nested-name-specifier that qualifies this unresolved
1170  /// declaration name.
1171  NestedNameSpecifier *Qualifier;
1172
1173  /// \brief Whether the name includes explicit template arguments.
1174  bool HasExplicitTemplateArgs;
1175
1176  DependentScopeDeclRefExpr(QualType T,
1177                            NestedNameSpecifier *Qualifier,
1178                            SourceRange QualifierRange,
1179                            DeclarationName Name,
1180                            SourceLocation NameLoc,
1181                            bool HasExplicitTemplateArgs)
1182    : Expr(DependentScopeDeclRefExprClass, T, true, true),
1183      Name(Name), Loc(NameLoc),
1184      QualifierRange(QualifierRange), Qualifier(Qualifier),
1185      HasExplicitTemplateArgs(HasExplicitTemplateArgs)
1186  {}
1187
1188public:
1189  static DependentScopeDeclRefExpr *Create(ASTContext &C,
1190                                           NestedNameSpecifier *Qualifier,
1191                                           SourceRange QualifierRange,
1192                                           DeclarationName Name,
1193                                           SourceLocation NameLoc,
1194                              const TemplateArgumentListInfo *TemplateArgs = 0);
1195
1196  /// \brief Retrieve the name that this expression refers to.
1197  DeclarationName getDeclName() const { return Name; }
1198
1199  /// \brief Retrieve the location of the name within the expression.
1200  SourceLocation getLocation() const { return Loc; }
1201
1202  /// \brief Retrieve the source range of the nested-name-specifier.
1203  SourceRange getQualifierRange() const { return QualifierRange; }
1204
1205  /// \brief Retrieve the nested-name-specifier that qualifies this
1206  /// declaration.
1207  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1208
1209  /// Determines whether this lookup had explicit template arguments.
1210  bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
1211
1212  // Note that, inconsistently with the explicit-template-argument AST
1213  // nodes, users are *forbidden* from calling these methods on objects
1214  // without explicit template arguments.
1215
1216  /// Gets a reference to the explicit template argument list.
1217  const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const {
1218    assert(hasExplicitTemplateArgs());
1219    return *reinterpret_cast<const ExplicitTemplateArgumentList*>(this + 1);
1220  }
1221
1222  /// \brief Copies the template arguments (if present) into the given
1223  /// structure.
1224  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1225    getExplicitTemplateArgs().copyInto(List);
1226  }
1227
1228  SourceLocation getLAngleLoc() const {
1229    return getExplicitTemplateArgs().LAngleLoc;
1230  }
1231
1232  SourceLocation getRAngleLoc() const {
1233    return getExplicitTemplateArgs().RAngleLoc;
1234  }
1235
1236  TemplateArgumentLoc const *getTemplateArgs() const {
1237    return getExplicitTemplateArgs().getTemplateArgs();
1238  }
1239
1240  unsigned getNumTemplateArgs() const {
1241    return getExplicitTemplateArgs().NumTemplateArgs;
1242  }
1243
1244  virtual SourceRange getSourceRange() const {
1245    SourceRange Range(QualifierRange.getBegin(), getLocation());
1246    if (hasExplicitTemplateArgs())
1247      Range.setEnd(getRAngleLoc());
1248    return Range;
1249  }
1250
1251  static bool classof(const Stmt *T) {
1252    return T->getStmtClass() == DependentScopeDeclRefExprClass;
1253  }
1254  static bool classof(const DependentScopeDeclRefExpr *) { return true; }
1255
1256  virtual StmtIterator child_begin();
1257  virtual StmtIterator child_end();
1258};
1259
1260class CXXExprWithTemporaries : public Expr {
1261  Stmt *SubExpr;
1262
1263  CXXTemporary **Temps;
1264  unsigned NumTemps;
1265
1266  bool ShouldDestroyTemps;
1267
1268  CXXExprWithTemporaries(Expr *SubExpr, CXXTemporary **Temps,
1269                         unsigned NumTemps, bool ShouldDestroyTemps);
1270  ~CXXExprWithTemporaries();
1271
1272protected:
1273  virtual void DoDestroy(ASTContext &C);
1274
1275public:
1276  static CXXExprWithTemporaries *Create(ASTContext &C, Expr *SubExpr,
1277                                        CXXTemporary **Temps, unsigned NumTemps,
1278                                        bool ShouldDestroyTemporaries);
1279
1280  unsigned getNumTemporaries() const { return NumTemps; }
1281  CXXTemporary *getTemporary(unsigned i) {
1282    assert(i < NumTemps && "Index out of range");
1283    return Temps[i];
1284  }
1285  const CXXTemporary *getTemporary(unsigned i) const {
1286    assert(i < NumTemps && "Index out of range");
1287    return Temps[i];
1288  }
1289
1290  bool shouldDestroyTemporaries() const { return ShouldDestroyTemps; }
1291
1292  void removeLastTemporary() { NumTemps--; }
1293
1294  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
1295  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
1296  void setSubExpr(Expr *E) { SubExpr = E; }
1297
1298  virtual SourceRange getSourceRange() const {
1299    return SubExpr->getSourceRange();
1300  }
1301
1302  // Implement isa/cast/dyncast/etc.
1303  static bool classof(const Stmt *T) {
1304    return T->getStmtClass() == CXXExprWithTemporariesClass;
1305  }
1306  static bool classof(const CXXExprWithTemporaries *) { return true; }
1307
1308  // Iterators
1309  virtual child_iterator child_begin();
1310  virtual child_iterator child_end();
1311};
1312
1313/// \brief Describes an explicit type conversion that uses functional
1314/// notion but could not be resolved because one or more arguments are
1315/// type-dependent.
1316///
1317/// The explicit type conversions expressed by
1318/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN),
1319/// where \c T is some type and \c a1, a2, ..., aN are values, and
1320/// either \C T is a dependent type or one or more of the \c a's is
1321/// type-dependent. For example, this would occur in a template such
1322/// as:
1323///
1324/// \code
1325///   template<typename T, typename A1>
1326///   inline T make_a(const A1& a1) {
1327///     return T(a1);
1328///   }
1329/// \endcode
1330///
1331/// When the returned expression is instantiated, it may resolve to a
1332/// constructor call, conversion function call, or some kind of type
1333/// conversion.
1334class CXXUnresolvedConstructExpr : public Expr {
1335  /// \brief The starting location of the type
1336  SourceLocation TyBeginLoc;
1337
1338  /// \brief The type being constructed.
1339  QualType Type;
1340
1341  /// \brief The location of the left parentheses ('(').
1342  SourceLocation LParenLoc;
1343
1344  /// \brief The location of the right parentheses (')').
1345  SourceLocation RParenLoc;
1346
1347  /// \brief The number of arguments used to construct the type.
1348  unsigned NumArgs;
1349
1350  CXXUnresolvedConstructExpr(SourceLocation TyBegin,
1351                             QualType T,
1352                             SourceLocation LParenLoc,
1353                             Expr **Args,
1354                             unsigned NumArgs,
1355                             SourceLocation RParenLoc);
1356
1357public:
1358  static CXXUnresolvedConstructExpr *Create(ASTContext &C,
1359                                            SourceLocation TyBegin,
1360                                            QualType T,
1361                                            SourceLocation LParenLoc,
1362                                            Expr **Args,
1363                                            unsigned NumArgs,
1364                                            SourceLocation RParenLoc);
1365
1366  /// \brief Retrieve the source location where the type begins.
1367  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
1368  void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; }
1369
1370  /// \brief Retrieve the type that is being constructed, as specified
1371  /// in the source code.
1372  QualType getTypeAsWritten() const { return Type; }
1373  void setTypeAsWritten(QualType T) { Type = T; }
1374
1375  /// \brief Retrieve the location of the left parentheses ('(') that
1376  /// precedes the argument list.
1377  SourceLocation getLParenLoc() const { return LParenLoc; }
1378  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1379
1380  /// \brief Retrieve the location of the right parentheses (')') that
1381  /// follows the argument list.
1382  SourceLocation getRParenLoc() const { return RParenLoc; }
1383  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1384
1385  /// \brief Retrieve the number of arguments.
1386  unsigned arg_size() const { return NumArgs; }
1387
1388  typedef Expr** arg_iterator;
1389  arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); }
1390  arg_iterator arg_end() { return arg_begin() + NumArgs; }
1391
1392  Expr *getArg(unsigned I) {
1393    assert(I < NumArgs && "Argument index out-of-range");
1394    return *(arg_begin() + I);
1395  }
1396
1397  virtual SourceRange getSourceRange() const {
1398    return SourceRange(TyBeginLoc, RParenLoc);
1399  }
1400  static bool classof(const Stmt *T) {
1401    return T->getStmtClass() == CXXUnresolvedConstructExprClass;
1402  }
1403  static bool classof(const CXXUnresolvedConstructExpr *) { return true; }
1404
1405  // Iterators
1406  virtual child_iterator child_begin();
1407  virtual child_iterator child_end();
1408};
1409
1410/// \brief Represents a C++ member access expression where the actual
1411/// member referenced could not be resolved because the base
1412/// expression or the member name was dependent.
1413class CXXDependentScopeMemberExpr : public Expr {
1414  /// \brief The expression for the base pointer or class reference,
1415  /// e.g., the \c x in x.f.
1416  Stmt *Base;
1417
1418  /// \brief Whether this member expression used the '->' operator or
1419  /// the '.' operator.
1420  bool IsArrow : 1;
1421
1422  /// \brief Whether this member expression has explicitly-specified template
1423  /// arguments.
1424  bool HasExplicitTemplateArgumentList : 1;
1425
1426  /// \brief The location of the '->' or '.' operator.
1427  SourceLocation OperatorLoc;
1428
1429  /// \brief The nested-name-specifier that precedes the member name, if any.
1430  NestedNameSpecifier *Qualifier;
1431
1432  /// \brief The source range covering the nested name specifier.
1433  SourceRange QualifierRange;
1434
1435  /// \brief In a qualified member access expression such as t->Base::f, this
1436  /// member stores the resolves of name lookup in the context of the member
1437  /// access expression, to be used at instantiation time.
1438  ///
1439  /// FIXME: This member, along with the Qualifier and QualifierRange, could
1440  /// be stuck into a structure that is optionally allocated at the end of
1441  /// the CXXDependentScopeMemberExpr, to save space in the common case.
1442  NamedDecl *FirstQualifierFoundInScope;
1443
1444  /// \brief The member to which this member expression refers, which
1445  /// can be name, overloaded operator, or destructor.
1446  /// FIXME: could also be a template-id
1447  DeclarationName Member;
1448
1449  /// \brief The location of the member name.
1450  SourceLocation MemberLoc;
1451
1452  /// \brief Retrieve the explicit template argument list that followed the
1453  /// member template name, if any.
1454  ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() {
1455    if (!HasExplicitTemplateArgumentList)
1456      return 0;
1457
1458    return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
1459  }
1460
1461  /// \brief Retrieve the explicit template argument list that followed the
1462  /// member template name, if any.
1463  const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const {
1464    return const_cast<CXXDependentScopeMemberExpr *>(this)
1465             ->getExplicitTemplateArgumentList();
1466  }
1467
1468  CXXDependentScopeMemberExpr(ASTContext &C,
1469                          Expr *Base, bool IsArrow,
1470                          SourceLocation OperatorLoc,
1471                          NestedNameSpecifier *Qualifier,
1472                          SourceRange QualifierRange,
1473                          NamedDecl *FirstQualifierFoundInScope,
1474                          DeclarationName Member,
1475                          SourceLocation MemberLoc,
1476                          const TemplateArgumentListInfo *TemplateArgs);
1477
1478public:
1479  CXXDependentScopeMemberExpr(ASTContext &C,
1480                          Expr *Base, bool IsArrow,
1481                          SourceLocation OperatorLoc,
1482                          NestedNameSpecifier *Qualifier,
1483                          SourceRange QualifierRange,
1484                          NamedDecl *FirstQualifierFoundInScope,
1485                          DeclarationName Member,
1486                          SourceLocation MemberLoc)
1487  : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, true, true),
1488    Base(Base), IsArrow(IsArrow), HasExplicitTemplateArgumentList(false),
1489    OperatorLoc(OperatorLoc),
1490    Qualifier(Qualifier), QualifierRange(QualifierRange),
1491    FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1492    Member(Member), MemberLoc(MemberLoc) { }
1493
1494  static CXXDependentScopeMemberExpr *
1495  Create(ASTContext &C,
1496         Expr *Base, bool IsArrow,
1497         SourceLocation OperatorLoc,
1498         NestedNameSpecifier *Qualifier,
1499         SourceRange QualifierRange,
1500         NamedDecl *FirstQualifierFoundInScope,
1501         DeclarationName Member,
1502         SourceLocation MemberLoc,
1503         const TemplateArgumentListInfo *TemplateArgs);
1504
1505  /// \brief Retrieve the base object of this member expressions,
1506  /// e.g., the \c x in \c x.m.
1507  Expr *getBase() { return cast<Expr>(Base); }
1508  void setBase(Expr *E) { Base = E; }
1509
1510  /// \brief Determine whether this member expression used the '->'
1511  /// operator; otherwise, it used the '.' operator.
1512  bool isArrow() const { return IsArrow; }
1513  void setArrow(bool A) { IsArrow = A; }
1514
1515  /// \brief Retrieve the location of the '->' or '.' operator.
1516  SourceLocation getOperatorLoc() const { return OperatorLoc; }
1517  void setOperatorLoc(SourceLocation L) { OperatorLoc = L; }
1518
1519  /// \brief Retrieve the nested-name-specifier that qualifies the member
1520  /// name.
1521  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1522
1523  /// \brief Retrieve the source range covering the nested-name-specifier
1524  /// that qualifies the member name.
1525  SourceRange getQualifierRange() const { return QualifierRange; }
1526
1527  /// \brief Retrieve the first part of the nested-name-specifier that was
1528  /// found in the scope of the member access expression when the member access
1529  /// was initially parsed.
1530  ///
1531  /// This function only returns a useful result when member access expression
1532  /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration
1533  /// returned by this function describes what was found by unqualified name
1534  /// lookup for the identifier "Base" within the scope of the member access
1535  /// expression itself. At template instantiation time, this information is
1536  /// combined with the results of name lookup into the type of the object
1537  /// expression itself (the class type of x).
1538  NamedDecl *getFirstQualifierFoundInScope() const {
1539    return FirstQualifierFoundInScope;
1540  }
1541
1542  /// \brief Retrieve the name of the member that this expression
1543  /// refers to.
1544  DeclarationName getMember() const { return Member; }
1545  void setMember(DeclarationName N) { Member = N; }
1546
1547  // \brief Retrieve the location of the name of the member that this
1548  // expression refers to.
1549  SourceLocation getMemberLoc() const { return MemberLoc; }
1550  void setMemberLoc(SourceLocation L) { MemberLoc = L; }
1551
1552  /// \brief Determines whether this member expression actually had a C++
1553  /// template argument list explicitly specified, e.g., x.f<int>.
1554  bool hasExplicitTemplateArgumentList() const {
1555    return HasExplicitTemplateArgumentList;
1556  }
1557
1558  /// \brief Copies the template arguments (if present) into the given
1559  /// structure.
1560  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1561    if (hasExplicitTemplateArgumentList())
1562      getExplicitTemplateArgumentList()->copyInto(List);
1563  }
1564
1565  /// \brief Retrieve the location of the left angle bracket following the
1566  /// member name ('<'), if any.
1567  SourceLocation getLAngleLoc() const {
1568    if (!HasExplicitTemplateArgumentList)
1569      return SourceLocation();
1570
1571    return getExplicitTemplateArgumentList()->LAngleLoc;
1572  }
1573
1574  /// \brief Retrieve the template arguments provided as part of this
1575  /// template-id.
1576  const TemplateArgumentLoc *getTemplateArgs() const {
1577    if (!HasExplicitTemplateArgumentList)
1578      return 0;
1579
1580    return getExplicitTemplateArgumentList()->getTemplateArgs();
1581  }
1582
1583  /// \brief Retrieve the number of template arguments provided as part of this
1584  /// template-id.
1585  unsigned getNumTemplateArgs() const {
1586    if (!HasExplicitTemplateArgumentList)
1587      return 0;
1588
1589    return getExplicitTemplateArgumentList()->NumTemplateArgs;
1590  }
1591
1592  /// \brief Retrieve the location of the right angle bracket following the
1593  /// template arguments ('>').
1594  SourceLocation getRAngleLoc() const {
1595    if (!HasExplicitTemplateArgumentList)
1596      return SourceLocation();
1597
1598    return getExplicitTemplateArgumentList()->RAngleLoc;
1599  }
1600
1601  virtual SourceRange getSourceRange() const {
1602    if (HasExplicitTemplateArgumentList)
1603      return SourceRange(Base->getSourceRange().getBegin(),
1604                         getRAngleLoc());
1605
1606    return SourceRange(Base->getSourceRange().getBegin(),
1607                       MemberLoc);
1608  }
1609
1610  static bool classof(const Stmt *T) {
1611    return T->getStmtClass() == CXXDependentScopeMemberExprClass;
1612  }
1613  static bool classof(const CXXDependentScopeMemberExpr *) { return true; }
1614
1615  // Iterators
1616  virtual child_iterator child_begin();
1617  virtual child_iterator child_end();
1618};
1619
1620/// \brief Represents a C++ member access expression for which lookup
1621/// produced a set of overloaded functions.  These are replaced with
1622/// MemberExprs in the final AST.
1623class UnresolvedMemberExpr : public Expr {
1624  /// The results.  These are undesugared, which is to say, they may
1625  /// include UsingShadowDecls.
1626  UnresolvedSet Results;
1627
1628  /// \brief The expression for the base pointer or class reference,
1629  /// e.g., the \c x in x.f.
1630  Stmt *Base;
1631
1632  /// \brief Whether this member expression used the '->' operator or
1633  /// the '.' operator.
1634  bool IsArrow : 1;
1635
1636  /// \brief Whether the lookup results contain an unresolved using
1637  /// declaration.
1638  bool HasUnresolvedUsing : 1;
1639
1640  /// \brief Whether this member expression has explicitly-specified template
1641  /// arguments.
1642  bool HasExplicitTemplateArgs : 1;
1643
1644  /// \brief The location of the '->' or '.' operator.
1645  SourceLocation OperatorLoc;
1646
1647  /// \brief The nested-name-specifier that precedes the member name, if any.
1648  NestedNameSpecifier *Qualifier;
1649
1650  /// \brief The source range covering the nested name specifier.
1651  SourceRange QualifierRange;
1652
1653  /// \brief The member to which this member expression refers, which
1654  /// can be a name or an overloaded operator.
1655  DeclarationName MemberName;
1656
1657  /// \brief The location of the member name.
1658  SourceLocation MemberLoc;
1659
1660  /// \brief Retrieve the explicit template argument list that followed the
1661  /// member template name.
1662  ExplicitTemplateArgumentList *getExplicitTemplateArgs() {
1663    assert(HasExplicitTemplateArgs);
1664    return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
1665  }
1666
1667  /// \brief Retrieve the explicit template argument list that followed the
1668  /// member template name, if any.
1669  const ExplicitTemplateArgumentList *getExplicitTemplateArgs() const {
1670    return const_cast<UnresolvedMemberExpr*>(this)->getExplicitTemplateArgs();
1671  }
1672
1673  UnresolvedMemberExpr(QualType T, bool Dependent,
1674                       bool HasUnresolvedUsing,
1675                       Expr *Base, bool IsArrow,
1676                       SourceLocation OperatorLoc,
1677                       NestedNameSpecifier *Qualifier,
1678                       SourceRange QualifierRange,
1679                       DeclarationName Member,
1680                       SourceLocation MemberLoc,
1681                       const TemplateArgumentListInfo *TemplateArgs);
1682
1683public:
1684  static UnresolvedMemberExpr *
1685  Create(ASTContext &C, bool Dependent, bool HasUnresolvedUsing,
1686         Expr *Base, bool IsArrow,
1687         SourceLocation OperatorLoc,
1688         NestedNameSpecifier *Qualifier,
1689         SourceRange QualifierRange,
1690         DeclarationName Member,
1691         SourceLocation MemberLoc,
1692         const TemplateArgumentListInfo *TemplateArgs);
1693
1694  /// Adds a declaration to the unresolved set.  By assumption, all of
1695  /// these happen at initialization time and properties like
1696  /// 'Dependent' and 'HasUnresolvedUsing' take them into account.
1697  void addDecl(NamedDecl *Decl) {
1698    Results.addDecl(Decl);
1699  }
1700
1701  typedef UnresolvedSet::iterator decls_iterator;
1702  decls_iterator decls_begin() const { return Results.begin(); }
1703  decls_iterator decls_end() const { return Results.end(); }
1704
1705  unsigned getNumDecls() const { return Results.size(); }
1706
1707  /// \brief Retrieve the base object of this member expressions,
1708  /// e.g., the \c x in \c x.m.
1709  Expr *getBase() { return cast<Expr>(Base); }
1710  void setBase(Expr *E) { Base = E; }
1711
1712  /// \brief Determine whether this member expression used the '->'
1713  /// operator; otherwise, it used the '.' operator.
1714  bool isArrow() const { return IsArrow; }
1715  void setArrow(bool A) { IsArrow = A; }
1716
1717  /// \brief Retrieve the location of the '->' or '.' operator.
1718  SourceLocation getOperatorLoc() const { return OperatorLoc; }
1719  void setOperatorLoc(SourceLocation L) { OperatorLoc = L; }
1720
1721  /// \brief Retrieve the nested-name-specifier that qualifies the member
1722  /// name.
1723  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1724
1725  /// \brief Retrieve the source range covering the nested-name-specifier
1726  /// that qualifies the member name.
1727  SourceRange getQualifierRange() const { return QualifierRange; }
1728
1729  /// \brief Retrieve the name of the member that this expression
1730  /// refers to.
1731  DeclarationName getMemberName() const { return MemberName; }
1732  void setMemberName(DeclarationName N) { MemberName = N; }
1733
1734  // \brief Retrieve the location of the name of the member that this
1735  // expression refers to.
1736  SourceLocation getMemberLoc() const { return MemberLoc; }
1737  void setMemberLoc(SourceLocation L) { MemberLoc = L; }
1738
1739  /// \brief Determines whether this member expression actually had a C++
1740  /// template argument list explicitly specified, e.g., x.f<int>.
1741  bool hasExplicitTemplateArgs() const {
1742    return HasExplicitTemplateArgs;
1743  }
1744
1745  /// \brief Copies the template arguments into the given structure.
1746  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1747    getExplicitTemplateArgs()->copyInto(List);
1748  }
1749
1750  /// \brief Retrieve the location of the left angle bracket following
1751  /// the member name ('<').
1752  SourceLocation getLAngleLoc() const {
1753    return getExplicitTemplateArgs()->LAngleLoc;
1754  }
1755
1756  /// \brief Retrieve the template arguments provided as part of this
1757  /// template-id.
1758  const TemplateArgumentLoc *getTemplateArgs() const {
1759    return getExplicitTemplateArgs()->getTemplateArgs();
1760  }
1761
1762  /// \brief Retrieve the number of template arguments provided as
1763  /// part of this template-id.
1764  unsigned getNumTemplateArgs() const {
1765    return getExplicitTemplateArgs()->NumTemplateArgs;
1766  }
1767
1768  /// \brief Retrieve the location of the right angle bracket
1769  /// following the template arguments ('>').
1770  SourceLocation getRAngleLoc() const {
1771    return getExplicitTemplateArgs()->RAngleLoc;
1772  }
1773
1774  virtual SourceRange getSourceRange() const {
1775    SourceRange Range = Base->getSourceRange();
1776    if (hasExplicitTemplateArgs())
1777      Range.setEnd(getRAngleLoc());
1778    else
1779      Range.setEnd(MemberLoc);
1780    return Range;
1781  }
1782
1783  static bool classof(const Stmt *T) {
1784    return T->getStmtClass() == UnresolvedMemberExprClass;
1785  }
1786  static bool classof(const UnresolvedMemberExpr *) { return true; }
1787
1788  // Iterators
1789  virtual child_iterator child_begin();
1790  virtual child_iterator child_end();
1791};
1792
1793}  // end namespace clang
1794
1795#endif
1796