ExprCXX.h revision 9e979557eea3875c9e3d100c68188233dd7f46c0
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/AST/Expr.h"
18
19namespace clang {
20
21  //===--------------------------------------------------------------------===//
22  // C++ Expressions.
23  //===--------------------------------------------------------------------===//
24
25  /// CXXCastExpr - [C++ 5.2.7, 5.2.9, 5.2.10, 5.2.11] C++ Cast Operators.
26  ///
27  class CXXCastExpr : public Expr {
28  public:
29    enum Opcode {
30      DynamicCast,
31      StaticCast,
32      ReinterpretCast,
33      ConstCast
34    };
35  private:
36    QualType Ty;
37    Opcode Opc;
38    Expr *Op;
39    SourceLocation Loc; // the location of the casting op
40  public:
41    CXXCastExpr(Opcode op, QualType ty, Expr *expr, SourceLocation l)
42      : Expr(CXXCastExprClass, ty), Ty(ty), Opc(op), Op(expr), Loc(l) {}
43
44    QualType getDestType() const { return Ty; }
45    Expr *getSubExpr() const { return Op; }
46
47    Opcode getOpcode() const { return Opc; }
48
49    /// getOpcodeStr - Turn an Opcode enum value into the string it represents,
50    /// e.g. "reinterpret_cast".
51    static const char *getOpcodeStr(Opcode Op) {
52      // FIXME: move out of line.
53      switch (Op) {
54      default: assert(0 && "Not a C++ cast expression");
55      case CXXCastExpr::ConstCast:       return "const_cast";
56      case CXXCastExpr::DynamicCast:     return "dynamic_cast";
57      case CXXCastExpr::ReinterpretCast: return "reinterpret_cast";
58      case CXXCastExpr::StaticCast:      return "static_cast";
59      }
60    }
61
62    virtual SourceRange getSourceRange() const {
63      return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
64    }
65    static bool classof(const Stmt *T) {
66      return T->getStmtClass() == CXXCastExprClass;
67    }
68    static bool classof(const CXXCastExpr *) { return true; }
69
70    // Iterators
71    virtual child_iterator child_begin();
72    virtual child_iterator child_end();
73  };
74
75  /// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal.
76  ///
77  class CXXBoolLiteralExpr : public Expr {
78    bool Value;
79    SourceLocation Loc;
80  public:
81    CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) :
82      Expr(CXXBoolLiteralExprClass, Ty), Value(val), Loc(l) {}
83
84    bool getValue() const { return Value; }
85
86    virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
87
88    static bool classof(const Stmt *T) {
89      return T->getStmtClass() == CXXBoolLiteralExprClass;
90    }
91    static bool classof(const CXXBoolLiteralExpr *) { return true; }
92
93    // Iterators
94    virtual child_iterator child_begin();
95    virtual child_iterator child_end();
96  };
97
98  ///  CXXThrowExpr - [C++ 15] C++ Throw Expression.  This handles
99  ///  'throw' and 'throw' assignment-expression.  When
100  ///  assignment-expression isn't present, Op will be null.
101  ///
102  class CXXThrowExpr : public Expr {
103    Expr *Op;
104    SourceLocation ThrowLoc;
105  public:
106    // Ty is the void type which is used as the result type of the
107    // exepression.  The l is the location of the throw keyword.  expr
108    // can by null, if the optional expression to throw isn't present.
109    CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) :
110      Expr(CXXThrowExprClass, Ty), Op(expr), ThrowLoc(l) {}
111    const Expr *getSubExpr() const { return Op; }
112    Expr *getSubExpr() { return Op; }
113
114    virtual SourceRange getSourceRange() const {
115      if (getSubExpr() == 0)
116        return SourceRange(ThrowLoc, ThrowLoc);
117      return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd());
118    }
119
120    static bool classof(const Stmt *T) {
121      return T->getStmtClass() == CXXThrowExprClass;
122    }
123    static bool classof(const CXXThrowExpr *) { return true; }
124
125    // Iterators
126    virtual child_iterator child_begin();
127    virtual child_iterator child_end();
128  };
129
130  /// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a
131  /// function call argument that was created from the corresponding
132  /// parameter's default argument, when the call did not explicitly
133  /// supply arguments for all of the parameters.
134  class CXXDefaultArgExpr : public Expr {
135    ParmVarDecl *Param;
136  public:
137    // Param is the parameter whose default argument is used by this
138    // expression.
139    explicit CXXDefaultArgExpr(ParmVarDecl *param)
140      : Expr(CXXDefaultArgExprClass, param->getDefaultArg()->getType()),
141        Param(param) { }
142
143    // Retrieve the parameter that the argument was created from.
144    const ParmVarDecl *getParam() const { return Param; }
145    ParmVarDecl *getParam() { return Param; }
146
147    // Retrieve the actual argument to the function call.
148    const Expr *getExpr() const { return Param->getDefaultArg(); }
149    Expr *getExpr() { return Param->getDefaultArg(); }
150
151    virtual SourceRange getSourceRange() const {
152      // Default argument expressions have no representation in the
153      // source, so they have an empty source range.
154      return SourceRange();
155    }
156
157    static bool classof(const Stmt *T) {
158      return T->getStmtClass() == CXXDefaultArgExprClass;
159    }
160    static bool classof(const CXXDefaultArgExpr *) { return true; }
161
162    // Iterators
163    virtual child_iterator child_begin();
164    virtual child_iterator child_end();
165
166    // Serialization
167    virtual void EmitImpl(llvm::Serializer& S) const;
168    static CXXDefaultArgExpr* CreateImpl(llvm::Deserializer& D,
169                                         ASTContext& C);
170  };
171}  // end namespace clang
172
173#endif
174