ExprCXX.h revision 02bbfa33590dfe3107e801fb526b7ab0bdfd00ee
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
21namespace clang {
22
23  class CXXConstructorDecl;
24  class CXXTempVarDecl;
25
26//===--------------------------------------------------------------------===//
27// C++ Expressions.
28//===--------------------------------------------------------------------===//
29
30/// \brief A call to an overloaded operator written using operator
31/// syntax.
32///
33/// Represents a call to an overloaded operator written using operator
34/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a
35/// normal call, this AST node provides better information about the
36/// syntactic representation of the call.
37///
38/// In a C++ template, this expression node kind will be used whenever
39/// any of the arguments are type-dependent. In this case, the
40/// function itself will be a (possibly empty) set of functions and
41/// function templates that were found by name lookup at template
42/// definition time.
43class CXXOperatorCallExpr : public CallExpr {
44  /// \brief The overloaded operator.
45  OverloadedOperatorKind Operator;
46
47public:
48  CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn,
49                      Expr **args, unsigned numargs, QualType t,
50                      SourceLocation operatorloc)
51    : CallExpr(C, CXXOperatorCallExprClass, fn, args, numargs, t, operatorloc),
52      Operator(Op) {}
53
54  /// getOperator - Returns the kind of overloaded operator that this
55  /// expression refers to.
56  OverloadedOperatorKind getOperator() const { return Operator; }
57
58  /// getOperatorLoc - Returns the location of the operator symbol in
59  /// the expression. When @c getOperator()==OO_Call, this is the
60  /// location of the right parentheses; when @c
61  /// getOperator()==OO_Subscript, this is the location of the right
62  /// bracket.
63  SourceLocation getOperatorLoc() const { return getRParenLoc(); }
64
65  virtual SourceRange getSourceRange() const;
66
67  static bool classof(const Stmt *T) {
68    return T->getStmtClass() == CXXOperatorCallExprClass;
69  }
70  static bool classof(const CXXOperatorCallExpr *) { return true; }
71};
72
73/// CXXMemberCallExpr - Represents a call to a member function that
74/// may be written either with member call syntax (e.g., "obj.func()"
75/// or "objptr->func()") or with normal function-call syntax
76/// ("func()") within a member function that ends up calling a member
77/// function. The callee in either case is a MemberExpr that contains
78/// both the object argument and the member function, while the
79/// arguments are the arguments within the parentheses (not including
80/// the object argument).
81class CXXMemberCallExpr : public CallExpr {
82public:
83  CXXMemberCallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
84                    QualType t, SourceLocation rparenloc)
85    : CallExpr(C, CXXMemberCallExprClass, fn, args, numargs, t, rparenloc) {}
86
87  /// getImplicitObjectArgument - Retrieves the implicit object
88  /// argument for the member call. For example, in "x.f(5)", this
89  /// operation would return "x".
90  Expr *getImplicitObjectArgument();
91
92  static bool classof(const Stmt *T) {
93    return T->getStmtClass() == CXXMemberCallExprClass;
94  }
95  static bool classof(const CXXMemberCallExpr *) { return true; }
96};
97
98/// CXXNamedCastExpr - Abstract class common to all of the C++ "named"
99/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c
100/// const_cast.
101///
102/// This abstract class is inherited by all of the classes
103/// representing "named" casts, e.g., CXXStaticCastExpr,
104/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr.
105class CXXNamedCastExpr : public ExplicitCastExpr {
106private:
107  SourceLocation Loc; // the location of the casting op
108
109protected:
110  CXXNamedCastExpr(StmtClass SC, QualType ty, Expr *op, QualType writtenTy,
111                   SourceLocation l)
112    : ExplicitCastExpr(SC, ty, op, writtenTy), Loc(l) {}
113
114public:
115  const char *getCastName() const;
116
117  virtual SourceRange getSourceRange() const {
118    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
119  }
120  static bool classof(const Stmt *T) {
121    switch (T->getStmtClass()) {
122    case CXXNamedCastExprClass:
123    case CXXStaticCastExprClass:
124    case CXXDynamicCastExprClass:
125    case CXXReinterpretCastExprClass:
126    case CXXConstCastExprClass:
127      return true;
128    default:
129      return false;
130    }
131  }
132  static bool classof(const CXXNamedCastExpr *) { return true; }
133};
134
135/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]).
136///
137/// This expression node represents a C++ static cast, e.g.,
138/// @c static_cast<int>(1.0).
139class CXXStaticCastExpr : public CXXNamedCastExpr {
140public:
141  CXXStaticCastExpr(QualType ty, Expr *op, QualType writtenTy, SourceLocation l)
142    : CXXNamedCastExpr(CXXStaticCastExprClass, ty, op, writtenTy, l) {}
143
144  static bool classof(const Stmt *T) {
145    return T->getStmtClass() == CXXStaticCastExprClass;
146  }
147  static bool classof(const CXXStaticCastExpr *) { return true; }
148};
149
150/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression
151/// (C++ [expr.dynamic.cast]), which may perform a run-time check to
152/// determine how to perform the type cast.
153///
154/// This expression node represents a dynamic cast, e.g.,
155/// @c dynamic_cast<Derived*>(BasePtr).
156class CXXDynamicCastExpr : public CXXNamedCastExpr {
157public:
158  CXXDynamicCastExpr(QualType ty, Expr *op, QualType writtenTy, SourceLocation l)
159    : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, op, writtenTy, l) {}
160
161  static bool classof(const Stmt *T) {
162    return T->getStmtClass() == CXXDynamicCastExprClass;
163  }
164  static bool classof(const CXXDynamicCastExpr *) { return true; }
165};
166
167/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++
168/// [expr.reinterpret.cast]), which provides a differently-typed view
169/// of a value but performs no actual work at run time.
170///
171/// This expression node represents a reinterpret cast, e.g.,
172/// @c reinterpret_cast<int>(VoidPtr).
173class CXXReinterpretCastExpr : public CXXNamedCastExpr {
174public:
175  CXXReinterpretCastExpr(QualType ty, Expr *op, QualType writtenTy,
176                         SourceLocation l)
177    : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, op, writtenTy, l) {}
178
179  static bool classof(const Stmt *T) {
180    return T->getStmtClass() == CXXReinterpretCastExprClass;
181  }
182  static bool classof(const CXXReinterpretCastExpr *) { return true; }
183};
184
185/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]),
186/// which can remove type qualifiers but does not change the underlying value.
187///
188/// This expression node represents a const cast, e.g.,
189/// @c const_cast<char*>(PtrToConstChar).
190class CXXConstCastExpr : public CXXNamedCastExpr {
191public:
192  CXXConstCastExpr(QualType ty, Expr *op, QualType writtenTy,
193                   SourceLocation l)
194    : CXXNamedCastExpr(CXXConstCastExprClass, ty, op, writtenTy, l) {}
195
196  static bool classof(const Stmt *T) {
197    return T->getStmtClass() == CXXConstCastExprClass;
198  }
199  static bool classof(const CXXConstCastExpr *) { return true; }
200};
201
202/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal.
203///
204class CXXBoolLiteralExpr : public Expr {
205  bool Value;
206  SourceLocation Loc;
207public:
208  CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) :
209    Expr(CXXBoolLiteralExprClass, Ty), Value(val), Loc(l) {}
210
211  bool getValue() const { return Value; }
212
213  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
214
215  static bool classof(const Stmt *T) {
216    return T->getStmtClass() == CXXBoolLiteralExprClass;
217  }
218  static bool classof(const CXXBoolLiteralExpr *) { return true; }
219
220  // Iterators
221  virtual child_iterator child_begin();
222  virtual child_iterator child_end();
223};
224
225/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets
226/// the type_info that corresponds to the supplied type, or the (possibly
227/// dynamic) type of the supplied expression.
228///
229/// This represents code like @c typeid(int) or @c typeid(*objPtr)
230class CXXTypeidExpr : public Expr {
231private:
232  bool isTypeOp : 1;
233  union {
234    void *Ty;
235    Stmt *Ex;
236  } Operand;
237  SourceRange Range;
238
239public:
240  CXXTypeidExpr(bool isTypeOp, void *op, QualType Ty, const SourceRange r) :
241      Expr(CXXTypeidExprClass, Ty,
242        // typeid is never type-dependent (C++ [temp.dep.expr]p4)
243        false,
244        // typeid is value-dependent if the type or expression are dependent
245        (isTypeOp ? QualType::getFromOpaquePtr(op)->isDependentType()
246                  : static_cast<Expr*>(op)->isValueDependent())),
247      isTypeOp(isTypeOp), Range(r) {
248    if (isTypeOp)
249      Operand.Ty = op;
250    else
251      // op was an Expr*, so cast it back to that to be safe
252      Operand.Ex = static_cast<Expr*>(op);
253  }
254
255  bool isTypeOperand() const { return isTypeOp; }
256  QualType getTypeOperand() const {
257    assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
258    return QualType::getFromOpaquePtr(Operand.Ty);
259  }
260  Expr* getExprOperand() const {
261    assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
262    return static_cast<Expr*>(Operand.Ex);
263  }
264
265  virtual SourceRange getSourceRange() const {
266    return Range;
267  }
268  static bool classof(const Stmt *T) {
269    return T->getStmtClass() == CXXTypeidExprClass;
270  }
271  static bool classof(const CXXTypeidExpr *) { return true; }
272
273  // Iterators
274  virtual child_iterator child_begin();
275  virtual child_iterator child_end();
276};
277
278/// CXXThisExpr - Represents the "this" expression in C++, which is a
279/// pointer to the object on which the current member function is
280/// executing (C++ [expr.prim]p3). Example:
281///
282/// @code
283/// class Foo {
284/// public:
285///   void bar();
286///   void test() { this->bar(); }
287/// };
288/// @endcode
289class CXXThisExpr : public Expr {
290  SourceLocation Loc;
291
292public:
293  CXXThisExpr(SourceLocation L, QualType Type)
294    : Expr(CXXThisExprClass, Type,
295           // 'this' is type-dependent if the class type of the enclosing
296           // member function is dependent (C++ [temp.dep.expr]p2)
297           Type->isDependentType(), Type->isDependentType()),
298      Loc(L) { }
299
300  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
301
302  static bool classof(const Stmt *T) {
303    return T->getStmtClass() == CXXThisExprClass;
304  }
305  static bool classof(const CXXThisExpr *) { return true; }
306
307  // Iterators
308  virtual child_iterator child_begin();
309  virtual child_iterator child_end();
310};
311
312///  CXXThrowExpr - [C++ 15] C++ Throw Expression.  This handles
313///  'throw' and 'throw' assignment-expression.  When
314///  assignment-expression isn't present, Op will be null.
315///
316class CXXThrowExpr : public Expr {
317  Stmt *Op;
318  SourceLocation ThrowLoc;
319public:
320  // Ty is the void type which is used as the result type of the
321  // exepression.  The l is the location of the throw keyword.  expr
322  // can by null, if the optional expression to throw isn't present.
323  CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) :
324    Expr(CXXThrowExprClass, Ty, false, false), Op(expr), ThrowLoc(l) {}
325  const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); }
326  Expr *getSubExpr() { return cast_or_null<Expr>(Op); }
327
328  virtual SourceRange getSourceRange() const {
329    if (getSubExpr() == 0)
330      return SourceRange(ThrowLoc, ThrowLoc);
331    return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd());
332  }
333
334  static bool classof(const Stmt *T) {
335    return T->getStmtClass() == CXXThrowExprClass;
336  }
337  static bool classof(const CXXThrowExpr *) { return true; }
338
339  // Iterators
340  virtual child_iterator child_begin();
341  virtual child_iterator child_end();
342};
343
344/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a
345/// function call argument that was created from the corresponding
346/// parameter's default argument, when the call did not explicitly
347/// supply arguments for all of the parameters.
348class CXXDefaultArgExpr : public Expr {
349  ParmVarDecl *Param;
350public:
351  // Param is the parameter whose default argument is used by this
352  // expression.
353  explicit CXXDefaultArgExpr(ParmVarDecl *param)
354    : Expr(CXXDefaultArgExprClass,
355           param->hasUnparsedDefaultArg()? param->getType().getNonReferenceType()
356                                         : param->getDefaultArg()->getType()),
357      Param(param) { }
358
359  // Retrieve the parameter that the argument was created from.
360  const ParmVarDecl *getParam() const { return Param; }
361  ParmVarDecl *getParam() { return Param; }
362
363  // Retrieve the actual argument to the function call.
364  const Expr *getExpr() const { return Param->getDefaultArg(); }
365  Expr *getExpr() { return Param->getDefaultArg(); }
366
367  virtual SourceRange getSourceRange() const {
368    // Default argument expressions have no representation in the
369    // source, so they have an empty source range.
370    return SourceRange();
371  }
372
373  static bool classof(const Stmt *T) {
374    return T->getStmtClass() == CXXDefaultArgExprClass;
375  }
376  static bool classof(const CXXDefaultArgExpr *) { return true; }
377
378  // Iterators
379  virtual child_iterator child_begin();
380  virtual child_iterator child_end();
381};
382
383/// CXXConstructExpr - Represents a call to a C++ constructor.
384class CXXConstructExpr : public Expr {
385  VarDecl *VD;
386  CXXConstructorDecl *Constructor;
387
388  bool Elidable;
389
390  Stmt **Args;
391  unsigned NumArgs;
392
393
394protected:
395  CXXConstructExpr(ASTContext &C, StmtClass SC, VarDecl *vd, QualType T,
396                   CXXConstructorDecl *d, bool elidable,
397                   Expr **args, unsigned numargs);
398  ~CXXConstructExpr() { }
399
400public:
401  static CXXConstructExpr *Create(ASTContext &C, VarDecl *VD, QualType T,
402                                  CXXConstructorDecl *D, bool Elidable,
403                                  Expr **Args, unsigned NumArgs);
404
405  void Destroy(ASTContext &C);
406
407  const VarDecl* getVarDecl() const { return VD; }
408  const CXXConstructorDecl* getConstructor() const { return Constructor; }
409
410  typedef ExprIterator arg_iterator;
411  typedef ConstExprIterator const_arg_iterator;
412
413  arg_iterator arg_begin() { return Args; }
414  arg_iterator arg_end() { return Args + NumArgs; }
415  const_arg_iterator arg_begin() const { return Args; }
416  const_arg_iterator arg_end() const { return Args + NumArgs; }
417
418  unsigned getNumArgs() const { return NumArgs; }
419
420  virtual SourceRange getSourceRange() const { return SourceRange(); }
421
422  static bool classof(const Stmt *T) {
423    return T->getStmtClass() == CXXConstructExprClass ||
424      T->getStmtClass() == CXXTemporaryObjectExprClass;
425  }
426  static bool classof(const CXXConstructExpr *) { return true; }
427
428  // Iterators
429  virtual child_iterator child_begin();
430  virtual child_iterator child_end();
431};
432
433/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion
434/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c
435/// x = int(0.5);
436class CXXFunctionalCastExpr : public ExplicitCastExpr {
437  SourceLocation TyBeginLoc;
438  SourceLocation RParenLoc;
439public:
440  CXXFunctionalCastExpr(QualType ty, QualType writtenTy,
441                        SourceLocation tyBeginLoc, Expr *castExpr,
442                        SourceLocation rParenLoc) :
443    ExplicitCastExpr(CXXFunctionalCastExprClass, ty, castExpr, writtenTy),
444    TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
445
446  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
447  SourceLocation getRParenLoc() const { return RParenLoc; }
448
449  virtual SourceRange getSourceRange() const {
450    return SourceRange(TyBeginLoc, RParenLoc);
451  }
452  static bool classof(const Stmt *T) {
453    return T->getStmtClass() == CXXFunctionalCastExprClass;
454  }
455  static bool classof(const CXXFunctionalCastExpr *) { return true; }
456};
457
458/// @brief Represents a C++ functional cast expression that builds a
459/// temporary object.
460///
461/// This expression type represents a C++ "functional" cast
462/// (C++[expr.type.conv]) with N != 1 arguments that invokes a
463/// constructor to build a temporary object. If N == 0 but no
464/// constructor will be called (because the functional cast is
465/// performing a value-initialized an object whose class type has no
466/// user-declared constructors), CXXZeroInitValueExpr will represent
467/// the functional cast. Finally, with N == 1 arguments the functional
468/// cast expression will be represented by CXXFunctionalCastExpr.
469/// Example:
470/// @code
471/// struct X { X(int, float); }
472///
473/// X create_X() {
474///   return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
475/// };
476/// @endcode
477class CXXTemporaryObjectExpr : public CXXConstructExpr {
478  SourceLocation TyBeginLoc;
479  SourceLocation RParenLoc;
480
481public:
482  CXXTemporaryObjectExpr(ASTContext &C, VarDecl *vd,
483                         CXXConstructorDecl *Cons, QualType writtenTy,
484                         SourceLocation tyBeginLoc, Expr **Args,
485                         unsigned NumArgs, SourceLocation rParenLoc);
486
487  ~CXXTemporaryObjectExpr() { }
488
489  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
490  SourceLocation getRParenLoc() const { return RParenLoc; }
491
492  virtual SourceRange getSourceRange() const {
493    return SourceRange(TyBeginLoc, RParenLoc);
494  }
495  static bool classof(const Stmt *T) {
496    return T->getStmtClass() == CXXTemporaryObjectExprClass;
497  }
498  static bool classof(const CXXTemporaryObjectExpr *) { return true; }
499};
500
501/// CXXZeroInitValueExpr - [C++ 5.2.3p2]
502/// Expression "T()" which creates a value-initialized rvalue of type
503/// T, which is either a non-class type or a class type without any
504/// user-defined constructors.
505///
506class CXXZeroInitValueExpr : public Expr {
507  SourceLocation TyBeginLoc;
508  SourceLocation RParenLoc;
509
510public:
511  CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc,
512                       SourceLocation rParenLoc ) :
513    Expr(CXXZeroInitValueExprClass, ty),
514    TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
515
516  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
517  SourceLocation getRParenLoc() const { return RParenLoc; }
518
519  /// @brief Whether this initialization expression was
520  /// implicitly-generated.
521  bool isImplicit() const {
522    return TyBeginLoc.isInvalid() && RParenLoc.isInvalid();
523  }
524
525  virtual SourceRange getSourceRange() const {
526    return SourceRange(TyBeginLoc, RParenLoc);
527  }
528
529  static bool classof(const Stmt *T) {
530    return T->getStmtClass() == CXXZeroInitValueExprClass;
531  }
532  static bool classof(const CXXZeroInitValueExpr *) { return true; }
533
534  // Iterators
535  virtual child_iterator child_begin();
536  virtual child_iterator child_end();
537};
538
539/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for
540/// statement, e.g: "if (int x = f()) {...}".
541/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the
542/// decl that it references.
543///
544class CXXConditionDeclExpr : public DeclRefExpr {
545public:
546  CXXConditionDeclExpr(SourceLocation startLoc,
547                       SourceLocation eqLoc, VarDecl *var)
548    : DeclRefExpr(CXXConditionDeclExprClass, var,
549                  var->getType().getNonReferenceType(), startLoc) {}
550
551  virtual void Destroy(ASTContext& Ctx);
552
553  SourceLocation getStartLoc() const { return getLocation(); }
554
555  VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); }
556  const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); }
557
558  virtual SourceRange getSourceRange() const {
559    return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd());
560  }
561
562  static bool classof(const Stmt *T) {
563    return T->getStmtClass() == CXXConditionDeclExprClass;
564  }
565  static bool classof(const CXXConditionDeclExpr *) { return true; }
566
567  // Iterators
568  virtual child_iterator child_begin();
569  virtual child_iterator child_end();
570};
571
572/// CXXNewExpr - A new expression for memory allocation and constructor calls,
573/// e.g: "new CXXNewExpr(foo)".
574class CXXNewExpr : public Expr {
575  // Was the usage ::new, i.e. is the global new to be used?
576  bool GlobalNew : 1;
577  // Was the form (type-id) used? Otherwise, it was new-type-id.
578  bool ParenTypeId : 1;
579  // Is there an initializer? If not, built-ins are uninitialized, else they're
580  // value-initialized.
581  bool Initializer : 1;
582  // Do we allocate an array? If so, the first SubExpr is the size expression.
583  bool Array : 1;
584  // The number of placement new arguments.
585  unsigned NumPlacementArgs : 14;
586  // The number of constructor arguments. This may be 1 even for non-class
587  // types; use the pseudo copy constructor.
588  unsigned NumConstructorArgs : 14;
589  // Contains an optional array size expression, any number of optional
590  // placement arguments, and any number of optional constructor arguments,
591  // in that order.
592  Stmt **SubExprs;
593  // Points to the allocation function used.
594  FunctionDecl *OperatorNew;
595  // Points to the deallocation function used in case of error. May be null.
596  FunctionDecl *OperatorDelete;
597  // Points to the constructor used. Cannot be null if AllocType is a record;
598  // it would still point at the default constructor (even an implicit one).
599  // Must be null for all other types.
600  CXXConstructorDecl *Constructor;
601
602  SourceLocation StartLoc;
603  SourceLocation EndLoc;
604
605public:
606  CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, Expr **placementArgs,
607             unsigned numPlaceArgs, bool ParenTypeId, Expr *arraySize,
608             CXXConstructorDecl *constructor, bool initializer,
609             Expr **constructorArgs, unsigned numConsArgs,
610             FunctionDecl *operatorDelete, QualType ty,
611             SourceLocation startLoc, SourceLocation endLoc);
612  ~CXXNewExpr() {
613    delete[] SubExprs;
614  }
615
616  QualType getAllocatedType() const {
617    assert(getType()->isPointerType());
618    return getType()->getAsPointerType()->getPointeeType();
619  }
620
621  FunctionDecl *getOperatorNew() const { return OperatorNew; }
622  FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
623  CXXConstructorDecl *getConstructor() const { return Constructor; }
624
625  bool isArray() const { return Array; }
626  Expr *getArraySize() {
627    return Array ? cast<Expr>(SubExprs[0]) : 0;
628  }
629  const Expr *getArraySize() const {
630    return Array ? cast<Expr>(SubExprs[0]) : 0;
631  }
632
633  unsigned getNumPlacementArgs() const { return NumPlacementArgs; }
634  Expr *getPlacementArg(unsigned i) {
635    assert(i < NumPlacementArgs && "Index out of range");
636    return cast<Expr>(SubExprs[Array + i]);
637  }
638  const Expr *getPlacementArg(unsigned i) const {
639    assert(i < NumPlacementArgs && "Index out of range");
640    return cast<Expr>(SubExprs[Array + i]);
641  }
642
643  bool isGlobalNew() const { return GlobalNew; }
644  bool isParenTypeId() const { return ParenTypeId; }
645  bool hasInitializer() const { return Initializer; }
646
647  unsigned getNumConstructorArgs() const { return NumConstructorArgs; }
648  Expr *getConstructorArg(unsigned i) {
649    assert(i < NumConstructorArgs && "Index out of range");
650    return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
651  }
652  const Expr *getConstructorArg(unsigned i) const {
653    assert(i < NumConstructorArgs && "Index out of range");
654    return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
655  }
656
657  typedef ExprIterator arg_iterator;
658  typedef ConstExprIterator const_arg_iterator;
659
660  arg_iterator placement_arg_begin() {
661    return SubExprs + Array;
662  }
663  arg_iterator placement_arg_end() {
664    return SubExprs + Array + getNumPlacementArgs();
665  }
666  const_arg_iterator placement_arg_begin() const {
667    return SubExprs + Array;
668  }
669  const_arg_iterator placement_arg_end() const {
670    return SubExprs + Array + getNumPlacementArgs();
671  }
672
673  arg_iterator constructor_arg_begin() {
674    return SubExprs + Array + getNumPlacementArgs();
675  }
676  arg_iterator constructor_arg_end() {
677    return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs();
678  }
679  const_arg_iterator constructor_arg_begin() const {
680    return SubExprs + Array + getNumPlacementArgs();
681  }
682  const_arg_iterator constructor_arg_end() const {
683    return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs();
684  }
685
686  virtual SourceRange getSourceRange() const {
687    return SourceRange(StartLoc, EndLoc);
688  }
689
690  static bool classof(const Stmt *T) {
691    return T->getStmtClass() == CXXNewExprClass;
692  }
693  static bool classof(const CXXNewExpr *) { return true; }
694
695  // Iterators
696  virtual child_iterator child_begin();
697  virtual child_iterator child_end();
698};
699
700/// CXXDeleteExpr - A delete expression for memory deallocation and destructor
701/// calls, e.g. "delete[] pArray".
702class CXXDeleteExpr : public Expr {
703  // Is this a forced global delete, i.e. "::delete"?
704  bool GlobalDelete : 1;
705  // Is this the array form of delete, i.e. "delete[]"?
706  bool ArrayForm : 1;
707  // Points to the operator delete overload that is used. Could be a member.
708  FunctionDecl *OperatorDelete;
709  // The pointer expression to be deleted.
710  Stmt *Argument;
711  // Location of the expression.
712  SourceLocation Loc;
713public:
714  CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm,
715                FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc)
716    : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete),
717      ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg),
718      Loc(loc) { }
719
720  bool isGlobalDelete() const { return GlobalDelete; }
721  bool isArrayForm() const { return ArrayForm; }
722
723  FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
724
725  Expr *getArgument() { return cast<Expr>(Argument); }
726  const Expr *getArgument() const { return cast<Expr>(Argument); }
727
728  virtual SourceRange getSourceRange() const {
729    return SourceRange(Loc, Argument->getLocEnd());
730  }
731
732  static bool classof(const Stmt *T) {
733    return T->getStmtClass() == CXXDeleteExprClass;
734  }
735  static bool classof(const CXXDeleteExpr *) { return true; }
736
737  // Iterators
738  virtual child_iterator child_begin();
739  virtual child_iterator child_end();
740};
741
742/// \brief Represents the name of a function that has not been
743/// resolved to any declaration.
744///
745/// Unresolved function names occur when a function name is
746/// encountered prior to an open parentheses ('(') in a C++ function
747/// call, and the function name itself did not resolve to a
748/// declaration. These function names can only be resolved when they
749/// form the postfix-expression of a function call, so that
750/// argument-dependent lookup finds declarations corresponding to
751/// these functions.
752
753/// @code
754/// template<typename T> void f(T x) {
755///   g(x); // g is an unresolved function name (that is also a dependent name)
756/// }
757/// @endcode
758class UnresolvedFunctionNameExpr : public Expr {
759  /// The name that was present in the source
760  DeclarationName Name;
761
762  /// The location of this name in the source code
763  SourceLocation Loc;
764
765public:
766  UnresolvedFunctionNameExpr(DeclarationName N, QualType T, SourceLocation L)
767    : Expr(UnresolvedFunctionNameExprClass, T, false, false), Name(N), Loc(L) { }
768
769  /// \brief Retrieves the name that occurred in the source code.
770  DeclarationName getName() const { return Name; }
771
772  /// getLocation - Retrieves the location in the source code where
773  /// the name occurred.
774  SourceLocation getLocation() const { return Loc; }
775
776  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
777
778  static bool classof(const Stmt *T) {
779    return T->getStmtClass() == UnresolvedFunctionNameExprClass;
780  }
781  static bool classof(const UnresolvedFunctionNameExpr *) { return true; }
782
783  // Iterators
784  virtual child_iterator child_begin();
785  virtual child_iterator child_end();
786};
787
788/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the
789/// implementation of TR1/C++0x type trait templates.
790/// Example:
791/// __is_pod(int) == true
792/// __is_enum(std::string) == false
793class UnaryTypeTraitExpr : public Expr {
794  /// UTT - The trait.
795  UnaryTypeTrait UTT;
796
797  /// Loc - The location of the type trait keyword.
798  SourceLocation Loc;
799
800  /// RParen - The location of the closing paren.
801  SourceLocation RParen;
802
803  /// QueriedType - The type we're testing.
804  QualType QueriedType;
805
806public:
807  UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried,
808                     SourceLocation rparen, QualType ty)
809    : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()),
810      UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { }
811
812  virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);}
813
814  UnaryTypeTrait getTrait() const { return UTT; }
815
816  QualType getQueriedType() const { return QueriedType; }
817
818  bool EvaluateTrait() const;
819
820  static bool classof(const Stmt *T) {
821    return T->getStmtClass() == UnaryTypeTraitExprClass;
822  }
823  static bool classof(const UnaryTypeTraitExpr *) { return true; }
824
825  // Iterators
826  virtual child_iterator child_begin();
827  virtual child_iterator child_end();
828};
829
830/// QualifiedDeclRefExpr - A reference to a declared variable,
831/// function, enum, etc., that includes a qualification, e.g.,
832/// "N::foo".
833class QualifiedDeclRefExpr : public DeclRefExpr {
834  /// QualifierRange - The source range that covers the
835  /// nested-name-specifier.
836  SourceRange QualifierRange;
837
838  /// \brief The nested-name-specifier that qualifies this declaration
839  /// name.
840  NestedNameSpecifier *NNS;
841
842public:
843  QualifiedDeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD,
844                       bool VD, SourceRange R, NestedNameSpecifier *NNS)
845    : DeclRefExpr(QualifiedDeclRefExprClass, d, t, l, TD, VD),
846      QualifierRange(R), NNS(NNS) { }
847
848  /// \brief Retrieve the source range of the nested-name-specifier.
849  SourceRange getQualifierRange() const { return QualifierRange; }
850
851  /// \brief Retrieve the nested-name-specifier that qualifies this
852  /// declaration.
853  NestedNameSpecifier *getQualifier() const { return NNS; }
854
855  virtual SourceRange getSourceRange() const {
856    return SourceRange(QualifierRange.getBegin(), getLocation());
857  }
858
859  static bool classof(const Stmt *T) {
860    return T->getStmtClass() == QualifiedDeclRefExprClass;
861  }
862  static bool classof(const QualifiedDeclRefExpr *) { return true; }
863};
864
865/// \brief A qualified reference to a name whose declaration cannot
866/// yet be resolved.
867///
868/// UnresolvedDeclRefExpr is similar to QualifiedDeclRefExpr in that
869/// it expresses a qualified reference to a declaration such as
870/// X<T>::value. The difference, however, is that an
871/// UnresolvedDeclRefExpr node is used only within C++ templates when
872/// the qualification (e.g., X<T>::) refers to a dependent type. In
873/// this case, X<T>::value cannot resolve to a declaration because the
874/// declaration will differ from on instantiation of X<T> to the
875/// next. Therefore, UnresolvedDeclRefExpr keeps track of the
876/// qualifier (X<T>::) and the name of the entity being referenced
877/// ("value"). Such expressions will instantiate to
878/// QualifiedDeclRefExprs.
879class UnresolvedDeclRefExpr : public Expr {
880  /// The name of the entity we will be referencing.
881  DeclarationName Name;
882
883  /// Location of the name of the declaration we're referencing.
884  SourceLocation Loc;
885
886  /// QualifierRange - The source range that covers the
887  /// nested-name-specifier.
888  SourceRange QualifierRange;
889
890  /// \brief The nested-name-specifier that qualifies this unresolved
891  /// declaration name.
892  NestedNameSpecifier *NNS;
893
894public:
895  UnresolvedDeclRefExpr(DeclarationName N, QualType T, SourceLocation L,
896                        SourceRange R, NestedNameSpecifier *NNS)
897    : Expr(UnresolvedDeclRefExprClass, T, true, true),
898      Name(N), Loc(L), QualifierRange(R), NNS(NNS) { }
899
900  /// \brief Retrieve the name that this expression refers to.
901  DeclarationName getDeclName() const { return Name; }
902
903  /// \brief Retrieve the location of the name within the expression.
904  SourceLocation getLocation() const { return Loc; }
905
906  /// \brief Retrieve the source range of the nested-name-specifier.
907  SourceRange getQualifierRange() const { return QualifierRange; }
908
909  /// \brief Retrieve the nested-name-specifier that qualifies this
910  /// declaration.
911  NestedNameSpecifier *getQualifier() const { return NNS; }
912
913  virtual SourceRange getSourceRange() const {
914    return SourceRange(QualifierRange.getBegin(), getLocation());
915  }
916
917  static bool classof(const Stmt *T) {
918    return T->getStmtClass() == UnresolvedDeclRefExprClass;
919  }
920  static bool classof(const UnresolvedDeclRefExpr *) { return true; }
921
922  virtual StmtIterator child_begin();
923  virtual StmtIterator child_end();
924};
925
926/// CXXDestroyExpr - Represents an implicit call to a C++ destructor.
927class CXXDestroyExpr : public Expr {
928  VarDecl *VD;
929
930  CXXDestroyExpr(VarDecl* vd, QualType T)
931  : Expr(CXXDestroyExprClass, T, false, vd->getType()->isDependentType()),
932    VD(vd) { }
933
934public:
935  static CXXDestroyExpr *Create(ASTContext &C, VarDecl *vd);
936
937  virtual SourceRange getSourceRange() const { return SourceRange(); }
938
939  // Implement isa/cast/dyncast/etc.
940  static bool classof(const Stmt *T) {
941    return T->getStmtClass() == CXXDestroyExprClass;
942  }
943  static bool classof(const CXXDestroyExpr *) { return true; }
944
945  // Iterators
946  virtual child_iterator child_begin();
947  virtual child_iterator child_end();
948};
949
950class CXXExprWithCleanup : public Expr {
951  Stmt *SubExpr;
952
953  CXXTempVarDecl **Decls;
954  unsigned NumDecls;
955
956public:
957  CXXExprWithCleanup(Expr *subexpr, CXXTempVarDecl **decls, unsigned numdecls);
958  ~CXXExprWithCleanup();
959
960  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
961  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
962
963  virtual SourceRange getSourceRange() const { return SourceRange(); }
964
965  // Implement isa/cast/dyncast/etc.
966  static bool classof(const Stmt *T) {
967    return T->getStmtClass() == CXXExprWithCleanupClass;
968  }
969  static bool classof(const CXXExprWithCleanup *) { return true; }
970
971  // Iterators
972  virtual child_iterator child_begin();
973  virtual child_iterator child_end();
974};
975
976}  // end namespace clang
977
978#endif
979