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