ExprCXX.h revision 987a14bf5883ef6e5d07f1c83eb6d41a8212a78c
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#include "clang/AST/Decl.h" 19 20namespace clang { 21 22//===--------------------------------------------------------------------===// 23// C++ Expressions. 24//===--------------------------------------------------------------------===// 25 26/// CXXCastExpr - [C++ 5.2.7, 5.2.9, 5.2.10, 5.2.11] C++ Cast Operators. 27/// 28class CXXCastExpr : public Expr { 29public: 30 enum Opcode { 31 DynamicCast, 32 StaticCast, 33 ReinterpretCast, 34 ConstCast 35 }; 36private: 37 QualType Ty; 38 Opcode Opc; 39 Stmt *Op; 40 SourceLocation Loc; // the location of the casting op 41public: 42 CXXCastExpr(Opcode op, QualType ty, Expr *expr, SourceLocation l) 43 : Expr(CXXCastExprClass, ty), Ty(ty), Opc(op), Op(expr), Loc(l) {} 44 45 QualType getDestType() const { return Ty; } 46 Expr *getSubExpr() const { return cast<Expr>(Op); } 47 48 Opcode getOpcode() const { return Opc; } 49 50 /// getOpcodeStr - Turn an Opcode enum value into the string it represents, 51 /// e.g. "reinterpret_cast". 52 static const char *getOpcodeStr(Opcode Op) { 53 // FIXME: move out of line. 54 switch (Op) { 55 default: assert(0 && "Not a C++ cast expression"); 56 case CXXCastExpr::ConstCast: return "const_cast"; 57 case CXXCastExpr::DynamicCast: return "dynamic_cast"; 58 case CXXCastExpr::ReinterpretCast: return "reinterpret_cast"; 59 case CXXCastExpr::StaticCast: return "static_cast"; 60 } 61 } 62 63 virtual SourceRange getSourceRange() const { 64 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 65 } 66 static bool classof(const Stmt *T) { 67 return T->getStmtClass() == CXXCastExprClass; 68 } 69 static bool classof(const CXXCastExpr *) { return true; } 70 71 // Iterators 72 virtual child_iterator child_begin(); 73 virtual child_iterator child_end(); 74}; 75 76/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal. 77/// 78class CXXBoolLiteralExpr : public Expr { 79 bool Value; 80 SourceLocation Loc; 81public: 82 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) : 83 Expr(CXXBoolLiteralExprClass, Ty), Value(val), Loc(l) {} 84 85 bool getValue() const { return Value; } 86 87 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 88 89 static bool classof(const Stmt *T) { 90 return T->getStmtClass() == CXXBoolLiteralExprClass; 91 } 92 static bool classof(const CXXBoolLiteralExpr *) { return true; } 93 94 // Iterators 95 virtual child_iterator child_begin(); 96 virtual child_iterator child_end(); 97}; 98 99/// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles 100/// 'throw' and 'throw' assignment-expression. When 101/// assignment-expression isn't present, Op will be null. 102/// 103class CXXThrowExpr : public Expr { 104 Stmt *Op; 105 SourceLocation ThrowLoc; 106public: 107 // Ty is the void type which is used as the result type of the 108 // exepression. The l is the location of the throw keyword. expr 109 // can by null, if the optional expression to throw isn't present. 110 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) : 111 Expr(CXXThrowExprClass, Ty), Op(expr), ThrowLoc(l) {} 112 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 113 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 114 115 virtual SourceRange getSourceRange() const { 116 if (getSubExpr() == 0) 117 return SourceRange(ThrowLoc, ThrowLoc); 118 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 119 } 120 121 static bool classof(const Stmt *T) { 122 return T->getStmtClass() == CXXThrowExprClass; 123 } 124 static bool classof(const CXXThrowExpr *) { return true; } 125 126 // Iterators 127 virtual child_iterator child_begin(); 128 virtual child_iterator child_end(); 129}; 130 131/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 132/// function call argument that was created from the corresponding 133/// parameter's default argument, when the call did not explicitly 134/// supply arguments for all of the parameters. 135class CXXDefaultArgExpr : public Expr { 136 ParmVarDecl *Param; 137public: 138 // Param is the parameter whose default argument is used by this 139 // expression. 140 explicit CXXDefaultArgExpr(ParmVarDecl *param) 141 : Expr(CXXDefaultArgExprClass, param->getDefaultArg()->getType()), 142 Param(param) { } 143 144 // Retrieve the parameter that the argument was created from. 145 const ParmVarDecl *getParam() const { return Param; } 146 ParmVarDecl *getParam() { return Param; } 147 148 // Retrieve the actual argument to the function call. 149 const Expr *getExpr() const { return Param->getDefaultArg(); } 150 Expr *getExpr() { return Param->getDefaultArg(); } 151 152 virtual SourceRange getSourceRange() const { 153 // Default argument expressions have no representation in the 154 // source, so they have an empty source range. 155 return SourceRange(); 156 } 157 158 static bool classof(const Stmt *T) { 159 return T->getStmtClass() == CXXDefaultArgExprClass; 160 } 161 static bool classof(const CXXDefaultArgExpr *) { return true; } 162 163 // Iterators 164 virtual child_iterator child_begin(); 165 virtual child_iterator child_end(); 166 167 // Serialization 168 virtual void EmitImpl(llvm::Serializer& S) const; 169 static CXXDefaultArgExpr* CreateImpl(llvm::Deserializer& D, 170 ASTContext& C); 171}; 172 173/// CXXFunctionalCastExpr - [C++ 5.2.3p1] Explicit type conversion 174/// (functional notation). 175/// Example: "x = int(0.5);" 176/// 177class CXXFunctionalCastExpr : public CastExpr { 178 SourceLocation TyBeginLoc; 179 SourceLocation RParenLoc; 180public: 181 CXXFunctionalCastExpr(QualType ty, SourceLocation tyBeginLoc, Expr *castExpr, 182 SourceLocation rParenLoc) : 183 CastExpr(CXXFunctionalCastExprClass, ty, castExpr), 184 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 185 186 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 187 SourceLocation getRParenLoc() const { return RParenLoc; } 188 189 virtual SourceRange getSourceRange() const { 190 return SourceRange(TyBeginLoc, RParenLoc); 191 } 192 static bool classof(const Stmt *T) { 193 return T->getStmtClass() == CXXFunctionalCastExprClass; 194 } 195 static bool classof(const CXXFunctionalCastExpr *) { return true; } 196 197 virtual void EmitImpl(llvm::Serializer& S) const; 198 static CXXFunctionalCastExpr * 199 CreateImpl(llvm::Deserializer& D, ASTContext& C); 200}; 201 202/// CXXZeroInitValueExpr - [C++ 5.2.3p2] 203/// Expression "T()" which creates a value-initialized Rvalue of non-class 204/// type T. 205/// 206class CXXZeroInitValueExpr : public Expr { 207 SourceLocation TyBeginLoc; 208 SourceLocation RParenLoc; 209 210public: 211 CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc, 212 SourceLocation rParenLoc ) : 213 Expr(CXXZeroInitValueExprClass, ty), 214 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 215 216 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 217 SourceLocation getRParenLoc() const { return RParenLoc; } 218 219 virtual SourceRange getSourceRange() const { 220 return SourceRange(TyBeginLoc, RParenLoc); 221 } 222 223 static bool classof(const Stmt *T) { 224 return T->getStmtClass() == CXXZeroInitValueExprClass; 225 } 226 static bool classof(const CXXZeroInitValueExpr *) { return true; } 227 228 // Iterators 229 virtual child_iterator child_begin(); 230 virtual child_iterator child_end(); 231 232 virtual void EmitImpl(llvm::Serializer& S) const; 233 static CXXZeroInitValueExpr * 234 CreateImpl(llvm::Deserializer& D, ASTContext& C); 235}; 236 237} // end namespace clang 238 239#endif 240