ExprCXX.h revision 49badde06e066d058d6c7fcf4e628a72999b65a9
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===--- ExprCXX.h - Classes for representing expressions -------*- C++ -*-===//
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// License. See LICENSE.TXT for details.
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===//
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//  This file defines the Expr interface and subclasses for C++ expressions.
11c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch//
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===//
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef LLVM_CLANG_AST_EXPRCXX_H
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define LLVM_CLANG_AST_EXPRCXX_H
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "clang/AST/Expr.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "clang/AST/Decl.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace clang {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===--------------------------------------------------------------------===//
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// C++ Expressions.
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===--------------------------------------------------------------------===//
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/// CXXNamedCastExpr - Abstract class common to all of the C++ "named"
27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c
28c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch/// const_cast.
295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu///
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// This abstract class is inherited by all of the classes
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// representing "named" casts, e.g., CXXStaticCastExpr,
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr.
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class CXXNamedCastExpr : public ExplicitCastExpr {
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)private:
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SourceLocation Loc; // the location of the casting op
36
37protected:
38  CXXNamedCastExpr(StmtClass SC, QualType ty, Expr *op, QualType writtenTy,
39                   SourceLocation l)
40    : ExplicitCastExpr(SC, ty, op, writtenTy), Loc(l) {}
41
42public:
43  const char *getCastName() const;
44
45  virtual SourceRange getSourceRange() const {
46    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
47  }
48  static bool classof(const Stmt *T) {
49    switch (T->getStmtClass()) {
50    case CXXNamedCastExprClass:
51    case CXXStaticCastExprClass:
52    case CXXDynamicCastExprClass:
53    case CXXReinterpretCastExprClass:
54    case CXXConstCastExprClass:
55      return true;
56    default:
57      return false;
58    }
59  }
60  static bool classof(const CXXNamedCastExpr *) { return true; }
61
62  virtual void EmitImpl(llvm::Serializer& S) const;
63  static CXXNamedCastExpr *CreateImpl(llvm::Deserializer& D, ASTContext& C,
64                                      StmtClass SC);
65};
66
67/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]).
68///
69/// This expression node represents a C++ static cast, e.g.,
70/// @c static_cast<int>(1.0).
71class CXXStaticCastExpr : public CXXNamedCastExpr {
72public:
73  CXXStaticCastExpr(QualType ty, Expr *op, QualType writtenTy, SourceLocation l)
74    : CXXNamedCastExpr(CXXStaticCastExprClass, ty, op, writtenTy, l) {}
75
76  static bool classof(const Stmt *T) {
77    return T->getStmtClass() == CXXStaticCastExprClass;
78  }
79  static bool classof(const CXXStaticCastExpr *) { return true; }
80};
81
82/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression
83/// (C++ [expr.dynamic.cast]), which may perform a run-time check to
84/// determine how to perform the type cast.
85///
86/// This expression node represents a dynamic cast, e.g.,
87/// @c dynamic_cast<Derived*>(BasePtr).
88class CXXDynamicCastExpr : public CXXNamedCastExpr {
89public:
90  CXXDynamicCastExpr(QualType ty, Expr *op, QualType writtenTy, SourceLocation l)
91    : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, op, writtenTy, l) {}
92
93  static bool classof(const Stmt *T) {
94    return T->getStmtClass() == CXXDynamicCastExprClass;
95  }
96  static bool classof(const CXXDynamicCastExpr *) { return true; }
97};
98
99/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++
100/// [expr.reinterpret.cast]), which provides a differently-typed view
101/// of a value but performs no actual work at run time.
102///
103/// This expression node represents a reinterpret cast, e.g.,
104/// @c reinterpret_cast<int>(VoidPtr).
105class CXXReinterpretCastExpr : public CXXNamedCastExpr {
106public:
107  CXXReinterpretCastExpr(QualType ty, Expr *op, QualType writtenTy,
108                         SourceLocation l)
109    : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, op, writtenTy, l) {}
110
111  static bool classof(const Stmt *T) {
112    return T->getStmtClass() == CXXReinterpretCastExprClass;
113  }
114  static bool classof(const CXXReinterpretCastExpr *) { return true; }
115};
116
117/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]),
118/// which can remove type qualifiers but does not change the underlying value.
119///
120/// This expression node represents a const cast, e.g.,
121/// @c const_cast<char*>(PtrToConstChar).
122class CXXConstCastExpr : public CXXNamedCastExpr {
123public:
124  CXXConstCastExpr(QualType ty, Expr *op, QualType writtenTy,
125                   SourceLocation l)
126    : CXXNamedCastExpr(CXXConstCastExprClass, ty, op, writtenTy, l) {}
127
128  static bool classof(const Stmt *T) {
129    return T->getStmtClass() == CXXConstCastExprClass;
130  }
131  static bool classof(const CXXConstCastExpr *) { return true; }
132};
133
134/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal.
135///
136class CXXBoolLiteralExpr : public Expr {
137  bool Value;
138  SourceLocation Loc;
139public:
140  CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) :
141    Expr(CXXBoolLiteralExprClass, Ty), Value(val), Loc(l) {}
142
143  bool getValue() const { return Value; }
144
145  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
146
147  static bool classof(const Stmt *T) {
148    return T->getStmtClass() == CXXBoolLiteralExprClass;
149  }
150  static bool classof(const CXXBoolLiteralExpr *) { return true; }
151
152  // Iterators
153  virtual child_iterator child_begin();
154  virtual child_iterator child_end();
155};
156
157///  CXXThrowExpr - [C++ 15] C++ Throw Expression.  This handles
158///  'throw' and 'throw' assignment-expression.  When
159///  assignment-expression isn't present, Op will be null.
160///
161class CXXThrowExpr : public Expr {
162  Stmt *Op;
163  SourceLocation ThrowLoc;
164public:
165  // Ty is the void type which is used as the result type of the
166  // exepression.  The l is the location of the throw keyword.  expr
167  // can by null, if the optional expression to throw isn't present.
168  CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) :
169    Expr(CXXThrowExprClass, Ty), Op(expr), ThrowLoc(l) {}
170  const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); }
171  Expr *getSubExpr() { return cast_or_null<Expr>(Op); }
172
173  virtual SourceRange getSourceRange() const {
174    if (getSubExpr() == 0)
175      return SourceRange(ThrowLoc, ThrowLoc);
176    return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd());
177  }
178
179  static bool classof(const Stmt *T) {
180    return T->getStmtClass() == CXXThrowExprClass;
181  }
182  static bool classof(const CXXThrowExpr *) { return true; }
183
184  // Iterators
185  virtual child_iterator child_begin();
186  virtual child_iterator child_end();
187};
188
189/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a
190/// function call argument that was created from the corresponding
191/// parameter's default argument, when the call did not explicitly
192/// supply arguments for all of the parameters.
193class CXXDefaultArgExpr : public Expr {
194  ParmVarDecl *Param;
195public:
196  // Param is the parameter whose default argument is used by this
197  // expression.
198  explicit CXXDefaultArgExpr(ParmVarDecl *param)
199    : Expr(CXXDefaultArgExprClass, param->getDefaultArg()->getType()),
200      Param(param) { }
201
202  // Retrieve the parameter that the argument was created from.
203  const ParmVarDecl *getParam() const { return Param; }
204  ParmVarDecl *getParam() { return Param; }
205
206  // Retrieve the actual argument to the function call.
207  const Expr *getExpr() const { return Param->getDefaultArg(); }
208  Expr *getExpr() { return Param->getDefaultArg(); }
209
210  virtual SourceRange getSourceRange() const {
211    // Default argument expressions have no representation in the
212    // source, so they have an empty source range.
213    return SourceRange();
214  }
215
216  static bool classof(const Stmt *T) {
217    return T->getStmtClass() == CXXDefaultArgExprClass;
218  }
219  static bool classof(const CXXDefaultArgExpr *) { return true; }
220
221  // Iterators
222  virtual child_iterator child_begin();
223  virtual child_iterator child_end();
224
225  // Serialization
226  virtual void EmitImpl(llvm::Serializer& S) const;
227  static CXXDefaultArgExpr* CreateImpl(llvm::Deserializer& D,
228                                       ASTContext& C);
229};
230
231/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion
232/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c
233/// x = int(0.5);
234class CXXFunctionalCastExpr : public ExplicitCastExpr {
235  SourceLocation TyBeginLoc;
236  SourceLocation RParenLoc;
237public:
238  CXXFunctionalCastExpr(QualType ty, QualType writtenTy,
239                        SourceLocation tyBeginLoc, Expr *castExpr,
240                        SourceLocation rParenLoc) :
241    ExplicitCastExpr(CXXFunctionalCastExprClass, ty, castExpr, writtenTy),
242    TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
243
244  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
245  SourceLocation getRParenLoc() const { return RParenLoc; }
246
247  virtual SourceRange getSourceRange() const {
248    return SourceRange(TyBeginLoc, RParenLoc);
249  }
250  static bool classof(const Stmt *T) {
251    return T->getStmtClass() == CXXFunctionalCastExprClass;
252  }
253  static bool classof(const CXXFunctionalCastExpr *) { return true; }
254
255  virtual void EmitImpl(llvm::Serializer& S) const;
256  static CXXFunctionalCastExpr *
257      CreateImpl(llvm::Deserializer& D, ASTContext& C);
258};
259
260/// CXXZeroInitValueExpr - [C++ 5.2.3p2]
261/// Expression "T()" which creates a value-initialized Rvalue of non-class
262/// type T.
263///
264class CXXZeroInitValueExpr : public Expr {
265  SourceLocation TyBeginLoc;
266  SourceLocation RParenLoc;
267
268public:
269  CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc,
270                       SourceLocation rParenLoc ) :
271    Expr(CXXZeroInitValueExprClass, ty),
272    TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {}
273
274  SourceLocation getTypeBeginLoc() const { return TyBeginLoc; }
275  SourceLocation getRParenLoc() const { return RParenLoc; }
276
277  virtual SourceRange getSourceRange() const {
278    return SourceRange(TyBeginLoc, RParenLoc);
279  }
280
281  static bool classof(const Stmt *T) {
282    return T->getStmtClass() == CXXZeroInitValueExprClass;
283  }
284  static bool classof(const CXXZeroInitValueExpr *) { return true; }
285
286  // Iterators
287  virtual child_iterator child_begin();
288  virtual child_iterator child_end();
289
290  virtual void EmitImpl(llvm::Serializer& S) const;
291  static CXXZeroInitValueExpr *
292      CreateImpl(llvm::Deserializer& D, ASTContext& C);
293};
294
295/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for
296/// statement, e.g: "if (int x = f()) {...}".
297/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the
298/// decl that it references.
299///
300class CXXConditionDeclExpr : public DeclRefExpr {
301public:
302  CXXConditionDeclExpr(SourceLocation startLoc,
303                       SourceLocation eqLoc, VarDecl *var)
304    : DeclRefExpr(CXXConditionDeclExprClass, var, var->getType(), startLoc) {}
305
306  virtual void Destroy(ASTContext& Ctx);
307
308  SourceLocation getStartLoc() const { return getLocation(); }
309
310  VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); }
311  const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); }
312
313  virtual SourceRange getSourceRange() const {
314    return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd());
315  }
316
317  static bool classof(const Stmt *T) {
318    return T->getStmtClass() == CXXConditionDeclExprClass;
319  }
320  static bool classof(const CXXConditionDeclExpr *) { return true; }
321
322  // Iterators
323  virtual child_iterator child_begin();
324  virtual child_iterator child_end();
325
326  // FIXME: Implement these.
327  //virtual void EmitImpl(llvm::Serializer& S) const;
328  //static CXXConditionDeclExpr *
329  //    CreateImpl(llvm::Deserializer& D, ASTContext& C);
330};
331
332}  // end namespace clang
333
334#endif
335