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