]> granicus.if.org Git - clang/blob - include/clang/AST/ExprCXX.h
add parsing, ast building and pretty printing support for C++ throw expressions.
[clang] / include / clang / AST / ExprCXX.h
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
19 namespace 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 }  // end namespace clang
131
132 #endif