ExprCXX.h revision fceb0a8adba9d25db99a4d73e9655c2831a96ecd
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 CXXTempVarDecl;
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  CXXDestructorDecl *Destructor;
419
420  CXXTemporary(CXXDestructorDecl *destructor)
421    : Destructor(destructor) { }
422
423public:
424  static CXXTemporary *Create(ASTContext &C, CXXDestructorDecl *Destructor);
425};
426
427/// CXXBindTemporaryExpr - Represents binding an expression to a temporary,
428/// so its destructor can be called later.
429class CXXBindTemporaryExpr : public Expr {
430  CXXTemporary *Temp;
431
432  Stmt *SubExpr;
433
434  CXXBindTemporaryExpr(CXXTemporary *temp, Expr* subexpr)
435   : Expr(CXXBindTemporaryExprClass,
436          subexpr->getType()), Temp(temp), SubExpr(subexpr) { }
437
438public:
439  static CXXBindTemporaryExpr *Create(ASTContext &C, CXXTemporary *Temp,
440                                      Expr* SubExpr);
441
442  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
443  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
444
445  virtual SourceRange getSourceRange() const { return SourceRange(); }
446
447  // Implement isa/cast/dyncast/etc.
448  static bool classof(const Stmt *T) {
449    return T->getStmtClass() == CXXBindTemporaryExprClass;
450  }
451  static bool classof(const CXXBindTemporaryExpr *) { return true; }
452
453  // Iterators
454  virtual child_iterator child_begin();
455  virtual child_iterator child_end();
456};
457
458/// CXXConstructExpr - Represents a call to a C++ constructor.
459class CXXConstructExpr : public Expr {
460  VarDecl *VD;
461  CXXConstructorDecl *Constructor;
462
463  bool Elidable;
464
465  Stmt **Args;
466  unsigned NumArgs;
467
468
469protected:
470  CXXConstructExpr(ASTContext &C, StmtClass SC, VarDecl *vd, QualType T,
471                   CXXConstructorDecl *d, bool elidable,
472                   Expr **args, unsigned numargs);
473  ~CXXConstructExpr() { }
474
475public:
476  static CXXConstructExpr *Create(ASTContext &C, VarDecl *VD, QualType T,
477                                  CXXConstructorDecl *D, bool Elidable,
478                                  Expr **Args, unsigned NumArgs);
479
480  void Destroy(ASTContext &C);
481
482  VarDecl* getVarDecl() const { return VD; }
483  CXXConstructorDecl* getConstructor() const { return Constructor; }
484
485  /// \brief Whether this construction is elidable.
486  bool isElidable() const { return Elidable; }
487
488  typedef ExprIterator arg_iterator;
489  typedef ConstExprIterator const_arg_iterator;
490
491  arg_iterator arg_begin() { return Args; }
492  arg_iterator arg_end() { return Args + NumArgs; }
493  const_arg_iterator arg_begin() const { return Args; }
494  const_arg_iterator arg_end() const { return Args + NumArgs; }
495
496  unsigned getNumArgs() const { return NumArgs; }
497
498  virtual SourceRange getSourceRange() const { return SourceRange(); }
499
500  static bool classof(const Stmt *T) {
501    return T->getStmtClass() == CXXConstructExprClass ||
502      T->getStmtClass() == CXXTemporaryObjectExprClass;
503  }
504  static bool classof(const CXXConstructExpr *) { return true; }
505
506  // Iterators
507  virtual child_iterator child_begin();
508  virtual child_iterator child_end();
509};
510
511/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion
512/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c
513/// x = int(0.5);
514class CXXFunctionalCastExpr : public ExplicitCastExpr {
515  SourceLocation TyBeginLoc;
516  SourceLocation RParenLoc;
517public:
518  CXXFunctionalCastExpr(QualType ty, QualType writtenTy,
519                        SourceLocation tyBeginLoc, Expr *castExpr,
520                        SourceLocation rParenLoc) :
521    ExplicitCastExpr(CXXFunctionalCastExprClass, ty, castExpr, writtenTy),
522    TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
523
524  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
525  SourceLocation getRParenLoc() const { return RParenLoc; }
526
527  virtual SourceRange getSourceRange() const {
528    return SourceRange(TyBeginLoc, RParenLoc);
529  }
530  static bool classof(const Stmt *T) {
531    return T->getStmtClass() == CXXFunctionalCastExprClass;
532  }
533  static bool classof(const CXXFunctionalCastExpr *) { return true; }
534};
535
536/// @brief Represents a C++ functional cast expression that builds a
537/// temporary object.
538///
539/// This expression type represents a C++ "functional" cast
540/// (C++[expr.type.conv]) with N != 1 arguments that invokes a
541/// constructor to build a temporary object. If N == 0 but no
542/// constructor will be called (because the functional cast is
543/// performing a value-initialized an object whose class type has no
544/// user-declared constructors), CXXZeroInitValueExpr will represent
545/// the functional cast. Finally, with N == 1 arguments the functional
546/// cast expression will be represented by CXXFunctionalCastExpr.
547/// Example:
548/// @code
549/// struct X { X(int, float); }
550///
551/// X create_X() {
552///   return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
553/// };
554/// @endcode
555class CXXTemporaryObjectExpr : public CXXConstructExpr {
556  SourceLocation TyBeginLoc;
557  SourceLocation RParenLoc;
558
559public:
560  CXXTemporaryObjectExpr(ASTContext &C, VarDecl *vd,
561                         CXXConstructorDecl *Cons, QualType writtenTy,
562                         SourceLocation tyBeginLoc, Expr **Args,
563                         unsigned NumArgs, SourceLocation rParenLoc);
564
565  ~CXXTemporaryObjectExpr() { }
566
567  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
568  SourceLocation getRParenLoc() const { return RParenLoc; }
569
570  virtual SourceRange getSourceRange() const {
571    return SourceRange(TyBeginLoc, RParenLoc);
572  }
573  static bool classof(const Stmt *T) {
574    return T->getStmtClass() == CXXTemporaryObjectExprClass;
575  }
576  static bool classof(const CXXTemporaryObjectExpr *) { return true; }
577};
578
579/// CXXZeroInitValueExpr - [C++ 5.2.3p2]
580/// Expression "T()" which creates a value-initialized rvalue of type
581/// T, which is either a non-class type or a class type without any
582/// user-defined constructors.
583///
584class CXXZeroInitValueExpr : public Expr {
585  SourceLocation TyBeginLoc;
586  SourceLocation RParenLoc;
587
588public:
589  CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc,
590                       SourceLocation rParenLoc ) :
591    Expr(CXXZeroInitValueExprClass, ty, false, false),
592    TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
593
594  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
595  SourceLocation getRParenLoc() const { return RParenLoc; }
596
597  /// @brief Whether this initialization expression was
598  /// implicitly-generated.
599  bool isImplicit() const {
600    return TyBeginLoc.isInvalid() && RParenLoc.isInvalid();
601  }
602
603  virtual SourceRange getSourceRange() const {
604    return SourceRange(TyBeginLoc, RParenLoc);
605  }
606
607  CXXZeroInitValueExpr* Clone(ASTContext &C) const;
608
609  static bool classof(const Stmt *T) {
610    return T->getStmtClass() == CXXZeroInitValueExprClass;
611  }
612  static bool classof(const CXXZeroInitValueExpr *) { return true; }
613
614  // Iterators
615  virtual child_iterator child_begin();
616  virtual child_iterator child_end();
617};
618
619/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for
620/// statement, e.g: "if (int x = f()) {...}".
621/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the
622/// decl that it references.
623///
624class CXXConditionDeclExpr : public DeclRefExpr {
625public:
626  CXXConditionDeclExpr(SourceLocation startLoc,
627                       SourceLocation eqLoc, VarDecl *var)
628    : DeclRefExpr(CXXConditionDeclExprClass, var,
629                  var->getType().getNonReferenceType(), startLoc,
630                  var->getType()->isDependentType(),
631                  /*FIXME:integral constant?*/
632                    var->getType()->isDependentType()) {}
633
634  virtual void Destroy(ASTContext& Ctx);
635
636  SourceLocation getStartLoc() const { return getLocation(); }
637
638  VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); }
639  const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); }
640
641  virtual SourceRange getSourceRange() const {
642    return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd());
643  }
644
645  static bool classof(const Stmt *T) {
646    return T->getStmtClass() == CXXConditionDeclExprClass;
647  }
648  static bool classof(const CXXConditionDeclExpr *) { return true; }
649
650  // Iterators
651  virtual child_iterator child_begin();
652  virtual child_iterator child_end();
653};
654
655/// CXXNewExpr - A new expression for memory allocation and constructor calls,
656/// e.g: "new CXXNewExpr(foo)".
657class CXXNewExpr : public Expr {
658  // Was the usage ::new, i.e. is the global new to be used?
659  bool GlobalNew : 1;
660  // Was the form (type-id) used? Otherwise, it was new-type-id.
661  bool ParenTypeId : 1;
662  // Is there an initializer? If not, built-ins are uninitialized, else they're
663  // value-initialized.
664  bool Initializer : 1;
665  // Do we allocate an array? If so, the first SubExpr is the size expression.
666  bool Array : 1;
667  // The number of placement new arguments.
668  unsigned NumPlacementArgs : 14;
669  // The number of constructor arguments. This may be 1 even for non-class
670  // types; use the pseudo copy constructor.
671  unsigned NumConstructorArgs : 14;
672  // Contains an optional array size expression, any number of optional
673  // placement arguments, and any number of optional constructor arguments,
674  // in that order.
675  Stmt **SubExprs;
676  // Points to the allocation function used.
677  FunctionDecl *OperatorNew;
678  // Points to the deallocation function used in case of error. May be null.
679  FunctionDecl *OperatorDelete;
680  // Points to the constructor used. Cannot be null if AllocType is a record;
681  // it would still point at the default constructor (even an implicit one).
682  // Must be null for all other types.
683  CXXConstructorDecl *Constructor;
684
685  SourceLocation StartLoc;
686  SourceLocation EndLoc;
687
688public:
689  CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, Expr **placementArgs,
690             unsigned numPlaceArgs, bool ParenTypeId, Expr *arraySize,
691             CXXConstructorDecl *constructor, bool initializer,
692             Expr **constructorArgs, unsigned numConsArgs,
693             FunctionDecl *operatorDelete, QualType ty,
694             SourceLocation startLoc, SourceLocation endLoc);
695  ~CXXNewExpr() {
696    delete[] SubExprs;
697  }
698
699  QualType getAllocatedType() const {
700    assert(getType()->isPointerType());
701    return getType()->getAsPointerType()->getPointeeType();
702  }
703
704  FunctionDecl *getOperatorNew() const { return OperatorNew; }
705  FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
706  CXXConstructorDecl *getConstructor() const { return Constructor; }
707
708  bool isArray() const { return Array; }
709  Expr *getArraySize() {
710    return Array ? cast<Expr>(SubExprs[0]) : 0;
711  }
712  const Expr *getArraySize() const {
713    return Array ? cast<Expr>(SubExprs[0]) : 0;
714  }
715
716  unsigned getNumPlacementArgs() const { return NumPlacementArgs; }
717  Expr *getPlacementArg(unsigned i) {
718    assert(i < NumPlacementArgs && "Index out of range");
719    return cast<Expr>(SubExprs[Array + i]);
720  }
721  const Expr *getPlacementArg(unsigned i) const {
722    assert(i < NumPlacementArgs && "Index out of range");
723    return cast<Expr>(SubExprs[Array + i]);
724  }
725
726  bool isGlobalNew() const { return GlobalNew; }
727  bool isParenTypeId() const { return ParenTypeId; }
728  bool hasInitializer() const { return Initializer; }
729
730  unsigned getNumConstructorArgs() const { return NumConstructorArgs; }
731  Expr *getConstructorArg(unsigned i) {
732    assert(i < NumConstructorArgs && "Index out of range");
733    return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
734  }
735  const Expr *getConstructorArg(unsigned i) const {
736    assert(i < NumConstructorArgs && "Index out of range");
737    return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]);
738  }
739
740  typedef ExprIterator arg_iterator;
741  typedef ConstExprIterator const_arg_iterator;
742
743  arg_iterator placement_arg_begin() {
744    return SubExprs + Array;
745  }
746  arg_iterator placement_arg_end() {
747    return SubExprs + Array + getNumPlacementArgs();
748  }
749  const_arg_iterator placement_arg_begin() const {
750    return SubExprs + Array;
751  }
752  const_arg_iterator placement_arg_end() const {
753    return SubExprs + Array + getNumPlacementArgs();
754  }
755
756  arg_iterator constructor_arg_begin() {
757    return SubExprs + Array + getNumPlacementArgs();
758  }
759  arg_iterator constructor_arg_end() {
760    return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs();
761  }
762  const_arg_iterator constructor_arg_begin() const {
763    return SubExprs + Array + getNumPlacementArgs();
764  }
765  const_arg_iterator constructor_arg_end() const {
766    return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs();
767  }
768
769  virtual SourceRange getSourceRange() const {
770    return SourceRange(StartLoc, EndLoc);
771  }
772
773  static bool classof(const Stmt *T) {
774    return T->getStmtClass() == CXXNewExprClass;
775  }
776  static bool classof(const CXXNewExpr *) { return true; }
777
778  // Iterators
779  virtual child_iterator child_begin();
780  virtual child_iterator child_end();
781};
782
783/// CXXDeleteExpr - A delete expression for memory deallocation and destructor
784/// calls, e.g. "delete[] pArray".
785class CXXDeleteExpr : public Expr {
786  // Is this a forced global delete, i.e. "::delete"?
787  bool GlobalDelete : 1;
788  // Is this the array form of delete, i.e. "delete[]"?
789  bool ArrayForm : 1;
790  // Points to the operator delete overload that is used. Could be a member.
791  FunctionDecl *OperatorDelete;
792  // The pointer expression to be deleted.
793  Stmt *Argument;
794  // Location of the expression.
795  SourceLocation Loc;
796public:
797  CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm,
798                FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc)
799    : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete),
800      ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg),
801      Loc(loc) { }
802
803  bool isGlobalDelete() const { return GlobalDelete; }
804  bool isArrayForm() const { return ArrayForm; }
805
806  FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
807
808  Expr *getArgument() { return cast<Expr>(Argument); }
809  const Expr *getArgument() const { return cast<Expr>(Argument); }
810
811  virtual SourceRange getSourceRange() const {
812    return SourceRange(Loc, Argument->getLocEnd());
813  }
814
815  static bool classof(const Stmt *T) {
816    return T->getStmtClass() == CXXDeleteExprClass;
817  }
818  static bool classof(const CXXDeleteExpr *) { return true; }
819
820  // Iterators
821  virtual child_iterator child_begin();
822  virtual child_iterator child_end();
823};
824
825/// \brief Represents the name of a function that has not been
826/// resolved to any declaration.
827///
828/// Unresolved function names occur when a function name is
829/// encountered prior to an open parentheses ('(') in a C++ function
830/// call, and the function name itself did not resolve to a
831/// declaration. These function names can only be resolved when they
832/// form the postfix-expression of a function call, so that
833/// argument-dependent lookup finds declarations corresponding to
834/// these functions.
835
836/// @code
837/// template<typename T> void f(T x) {
838///   g(x); // g is an unresolved function name (that is also a dependent name)
839/// }
840/// @endcode
841class UnresolvedFunctionNameExpr : public Expr {
842  /// The name that was present in the source
843  DeclarationName Name;
844
845  /// The location of this name in the source code
846  SourceLocation Loc;
847
848public:
849  UnresolvedFunctionNameExpr(DeclarationName N, QualType T, SourceLocation L)
850    : Expr(UnresolvedFunctionNameExprClass, T, false, false), Name(N), Loc(L) { }
851
852  /// \brief Retrieves the name that occurred in the source code.
853  DeclarationName getName() const { return Name; }
854
855  /// getLocation - Retrieves the location in the source code where
856  /// the name occurred.
857  SourceLocation getLocation() const { return Loc; }
858
859  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
860
861  UnresolvedFunctionNameExpr* Clone(ASTContext &C) const;
862
863  static bool classof(const Stmt *T) {
864    return T->getStmtClass() == UnresolvedFunctionNameExprClass;
865  }
866  static bool classof(const UnresolvedFunctionNameExpr *) { return true; }
867
868  // Iterators
869  virtual child_iterator child_begin();
870  virtual child_iterator child_end();
871};
872
873/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the
874/// implementation of TR1/C++0x type trait templates.
875/// Example:
876/// __is_pod(int) == true
877/// __is_enum(std::string) == false
878class UnaryTypeTraitExpr : public Expr {
879  /// UTT - The trait.
880  UnaryTypeTrait UTT;
881
882  /// Loc - The location of the type trait keyword.
883  SourceLocation Loc;
884
885  /// RParen - The location of the closing paren.
886  SourceLocation RParen;
887
888  /// QueriedType - The type we're testing.
889  QualType QueriedType;
890
891public:
892  UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried,
893                     SourceLocation rparen, QualType ty)
894    : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()),
895      UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { }
896
897  virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);}
898
899  UnaryTypeTrait getTrait() const { return UTT; }
900
901  QualType getQueriedType() const { return QueriedType; }
902
903  bool EvaluateTrait() const;
904
905  static bool classof(const Stmt *T) {
906    return T->getStmtClass() == UnaryTypeTraitExprClass;
907  }
908  static bool classof(const UnaryTypeTraitExpr *) { return true; }
909
910  // Iterators
911  virtual child_iterator child_begin();
912  virtual child_iterator child_end();
913};
914
915/// QualifiedDeclRefExpr - A reference to a declared variable,
916/// function, enum, etc., that includes a qualification, e.g.,
917/// "N::foo".
918class QualifiedDeclRefExpr : public DeclRefExpr {
919  /// QualifierRange - The source range that covers the
920  /// nested-name-specifier.
921  SourceRange QualifierRange;
922
923  /// \brief The nested-name-specifier that qualifies this declaration
924  /// name.
925  NestedNameSpecifier *NNS;
926
927public:
928  QualifiedDeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD,
929                       bool VD, SourceRange R, NestedNameSpecifier *NNS)
930    : DeclRefExpr(QualifiedDeclRefExprClass, d, t, l, TD, VD),
931      QualifierRange(R), NNS(NNS) { }
932
933  /// \brief Retrieve the source range of the nested-name-specifier.
934  SourceRange getQualifierRange() const { return QualifierRange; }
935
936  /// \brief Retrieve the nested-name-specifier that qualifies this
937  /// declaration.
938  NestedNameSpecifier *getQualifier() const { return NNS; }
939
940  virtual SourceRange getSourceRange() const {
941    return SourceRange(QualifierRange.getBegin(), getLocation());
942  }
943
944  static bool classof(const Stmt *T) {
945    return T->getStmtClass() == QualifiedDeclRefExprClass;
946  }
947  static bool classof(const QualifiedDeclRefExpr *) { return true; }
948};
949
950/// \brief A qualified reference to a name whose declaration cannot
951/// yet be resolved.
952///
953/// UnresolvedDeclRefExpr is similar to QualifiedDeclRefExpr in that
954/// it expresses a qualified reference to a declaration such as
955/// X<T>::value. The difference, however, is that an
956/// UnresolvedDeclRefExpr node is used only within C++ templates when
957/// the qualification (e.g., X<T>::) refers to a dependent type. In
958/// this case, X<T>::value cannot resolve to a declaration because the
959/// declaration will differ from on instantiation of X<T> to the
960/// next. Therefore, UnresolvedDeclRefExpr keeps track of the
961/// qualifier (X<T>::) and the name of the entity being referenced
962/// ("value"). Such expressions will instantiate to
963/// QualifiedDeclRefExprs.
964class UnresolvedDeclRefExpr : public Expr {
965  /// The name of the entity we will be referencing.
966  DeclarationName Name;
967
968  /// Location of the name of the declaration we're referencing.
969  SourceLocation Loc;
970
971  /// QualifierRange - The source range that covers the
972  /// nested-name-specifier.
973  SourceRange QualifierRange;
974
975  /// \brief The nested-name-specifier that qualifies this unresolved
976  /// declaration name.
977  NestedNameSpecifier *NNS;
978
979public:
980  UnresolvedDeclRefExpr(DeclarationName N, QualType T, SourceLocation L,
981                        SourceRange R, NestedNameSpecifier *NNS)
982    : Expr(UnresolvedDeclRefExprClass, T, true, true),
983      Name(N), Loc(L), QualifierRange(R), NNS(NNS) { }
984
985  /// \brief Retrieve the name that this expression refers to.
986  DeclarationName getDeclName() const { return Name; }
987
988  /// \brief Retrieve the location of the name within the expression.
989  SourceLocation getLocation() const { return Loc; }
990
991  /// \brief Retrieve the source range of the nested-name-specifier.
992  SourceRange getQualifierRange() const { return QualifierRange; }
993
994  /// \brief Retrieve the nested-name-specifier that qualifies this
995  /// declaration.
996  NestedNameSpecifier *getQualifier() const { return NNS; }
997
998  virtual SourceRange getSourceRange() const {
999    return SourceRange(QualifierRange.getBegin(), getLocation());
1000  }
1001
1002  static bool classof(const Stmt *T) {
1003    return T->getStmtClass() == UnresolvedDeclRefExprClass;
1004  }
1005  static bool classof(const UnresolvedDeclRefExpr *) { return true; }
1006
1007  virtual StmtIterator child_begin();
1008  virtual StmtIterator child_end();
1009};
1010
1011class CXXExprWithTemporaries : public Expr {
1012  Stmt *SubExpr;
1013
1014  CXXTempVarDecl **Decls;
1015  unsigned NumDecls;
1016
1017public:
1018  CXXExprWithTemporaries(Expr *subexpr, CXXTempVarDecl **decls,
1019                         unsigned numdecls);
1020  ~CXXExprWithTemporaries();
1021
1022  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
1023  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
1024
1025  virtual SourceRange getSourceRange() const { return SourceRange(); }
1026
1027  // Implement isa/cast/dyncast/etc.
1028  static bool classof(const Stmt *T) {
1029    return T->getStmtClass() == CXXExprWithTemporariesClass;
1030  }
1031  static bool classof(const CXXExprWithTemporaries *) { return true; }
1032
1033  // Iterators
1034  virtual child_iterator child_begin();
1035  virtual child_iterator child_end();
1036};
1037
1038/// \brief Describes an explicit type conversion that uses functional
1039/// notion but could not be resolved because one or more arguments are
1040/// type-dependent.
1041///
1042/// The explicit type conversions expressed by
1043/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN),
1044/// where \c T is some type and \c a1, a2, ..., aN are values, and
1045/// either \C T is a dependent type or one or more of the \c a's is
1046/// type-dependent. For example, this would occur in a template such
1047/// as:
1048///
1049/// \code
1050///   template<typename T, typename A1>
1051///   inline T make_a(const A1& a1) {
1052///     return T(a1);
1053///   }
1054/// \endcode
1055///
1056/// When the returned expression is instantiated, it may resolve to a
1057/// constructor call, conversion function call, or some kind of type
1058/// conversion.
1059class CXXUnresolvedConstructExpr : public Expr {
1060  /// \brief The starting location of the type
1061  SourceLocation TyBeginLoc;
1062
1063  /// \brief The type being constructed.
1064  QualType Type;
1065
1066  /// \brief The location of the left parentheses ('(').
1067  SourceLocation LParenLoc;
1068
1069  /// \brief The location of the right parentheses (')').
1070  SourceLocation RParenLoc;
1071
1072  /// \brief The number of arguments used to construct the type.
1073  unsigned NumArgs;
1074
1075  CXXUnresolvedConstructExpr(SourceLocation TyBegin,
1076                             QualType T,
1077                             SourceLocation LParenLoc,
1078                             Expr **Args,
1079                             unsigned NumArgs,
1080                             SourceLocation RParenLoc);
1081
1082public:
1083  static CXXUnresolvedConstructExpr *Create(ASTContext &C,
1084                                            SourceLocation TyBegin,
1085                                            QualType T,
1086                                            SourceLocation LParenLoc,
1087                                            Expr **Args,
1088                                            unsigned NumArgs,
1089                                            SourceLocation RParenLoc);
1090
1091  /// \brief Retrieve the source location where the type begins.
1092  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
1093  void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; }
1094
1095  /// \brief Retrieve the type that is being constructed, as specified
1096  /// in the source code.
1097  QualType getTypeAsWritten() const { return Type; }
1098  void setTypeAsWritten(QualType T) { Type = T; }
1099
1100  /// \brief Retrieve the location of the left parentheses ('(') that
1101  /// precedes the argument list.
1102  SourceLocation getLParenLoc() const { return LParenLoc; }
1103  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1104
1105  /// \brief Retrieve the location of the right parentheses (')') that
1106  /// follows the argument list.
1107  SourceLocation getRParenLoc() const { return RParenLoc; }
1108  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1109
1110  /// \brief Retrieve the number of arguments.
1111  unsigned arg_size() const { return NumArgs; }
1112
1113  typedef Expr** arg_iterator;
1114  arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); }
1115  arg_iterator arg_end() { return arg_begin() + NumArgs; }
1116
1117  Expr *getArg(unsigned I) {
1118    assert(I < NumArgs && "Argument index out-of-range");
1119    return *(arg_begin() + I);
1120  }
1121
1122  virtual SourceRange getSourceRange() const {
1123    return SourceRange(TyBeginLoc, RParenLoc);
1124  }
1125  static bool classof(const Stmt *T) {
1126    return T->getStmtClass() == CXXUnresolvedConstructExprClass;
1127  }
1128  static bool classof(const CXXUnresolvedConstructExpr *) { return true; }
1129
1130  // Iterators
1131  virtual child_iterator child_begin();
1132  virtual child_iterator child_end();
1133};
1134
1135/// \brief
1136class CXXUnresolvedMemberExpr : public Expr {
1137  /// \brief The expression for the base pointer or class reference,
1138  /// e.g., the \c x in x.f.
1139  Stmt *Base;
1140
1141  /// \brief Whether this member expression used the '->' operator or
1142  /// the '.' operator.
1143  bool IsArrow;
1144
1145  /// \brief The location of the '->' or '.' operator.
1146  SourceLocation OperatorLoc;
1147
1148  /// \brief The member to which this member expression refers, which
1149  /// can be name, overloaded operator, or destructor.
1150  /// FIXME: could also be a template-id, and we might have a
1151  /// nested-name-specifier as well.
1152  DeclarationName Member;
1153
1154  /// \brief The location of the member name.
1155  SourceLocation MemberLoc;
1156
1157public:
1158  CXXUnresolvedMemberExpr(ASTContext &C,
1159                          Expr *Base, bool IsArrow,
1160                          SourceLocation OperatorLoc,
1161                          DeclarationName Member,
1162                          SourceLocation MemberLoc)
1163    : Expr(CXXUnresolvedMemberExprClass, C.DependentTy, true, true),
1164      Base(Base), IsArrow(IsArrow), OperatorLoc(OperatorLoc),
1165      Member(Member), MemberLoc(MemberLoc) { }
1166
1167  /// \brief Retrieve the base object of this member expressions,
1168  /// e.g., the \c x in \c x.m.
1169  Expr *getBase() { return cast<Expr>(Base); }
1170  void setBase(Expr *E) { Base = E; }
1171
1172  /// \brief Determine whether this member expression used the '->'
1173  /// operator; otherwise, it used the '.' operator.
1174  bool isArrow() const { return IsArrow; }
1175  void setArrow(bool A) { IsArrow = A; }
1176
1177  /// \brief Retrieve the location of the '->' or '.' operator.
1178  SourceLocation getOperatorLoc() const { return OperatorLoc; }
1179  void setOperatorLoc(SourceLocation L) { OperatorLoc = L; }
1180
1181  /// \brief Retrieve the name of the member that this expression
1182  /// refers to.
1183  DeclarationName getMember() const { return Member; }
1184  void setMember(DeclarationName N) { Member = N; }
1185
1186  // \brief Retrieve the location of the name of the member that this
1187  // expression refers to.
1188  SourceLocation getMemberLoc() const { return MemberLoc; }
1189  void setMemberLoc(SourceLocation L) { MemberLoc = L; }
1190
1191  virtual SourceRange getSourceRange() const {
1192    return SourceRange(Base->getSourceRange().getBegin(),
1193                       MemberLoc);
1194  }
1195  static bool classof(const Stmt *T) {
1196    return T->getStmtClass() == CXXUnresolvedMemberExprClass;
1197  }
1198  static bool classof(const CXXUnresolvedMemberExpr *) { return true; }
1199
1200  // Iterators
1201  virtual child_iterator child_begin();
1202  virtual child_iterator child_end();
1203};
1204
1205}  // end namespace clang
1206
1207#endif
1208