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