ExprCXX.h revision 9d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7
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/// CXXNamedCastExpr - Abstract class common to all of the C++ "named" 27/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c 28/// const_cast. 29/// 30/// This abstract class is inherited by all of the classes 31/// representing "named" casts, e.g., CXXStaticCastExpr, 32/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr. 33class CXXNamedCastExpr : public ExplicitCastExpr { 34private: 35 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, 305 var->getType().getNonReferenceType(), startLoc) {} 306 307 virtual void Destroy(ASTContext& Ctx); 308 309 SourceLocation getStartLoc() const { return getLocation(); } 310 311 VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); } 312 const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); } 313 314 virtual SourceRange getSourceRange() const { 315 return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd()); 316 } 317 318 static bool classof(const Stmt *T) { 319 return T->getStmtClass() == CXXConditionDeclExprClass; 320 } 321 static bool classof(const CXXConditionDeclExpr *) { return true; } 322 323 // Iterators 324 virtual child_iterator child_begin(); 325 virtual child_iterator child_end(); 326 327 // FIXME: Implement these. 328 //virtual void EmitImpl(llvm::Serializer& S) const; 329 //static CXXConditionDeclExpr * 330 // CreateImpl(llvm::Deserializer& D, ASTContext& C); 331}; 332 333} // end namespace clang 334 335#endif 336