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