ExprCXX.h revision c42e1183846228a7fa5143ad76507d6d60f5c6f3
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/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets 158/// the type_info that corresponds to the supplied type, or the (possibly 159/// dynamic) type of the supplied expression. 160/// 161/// This represents code like @c typeid(int) or @c typeid(*objPtr) 162class CXXTypeidExpr : public Expr { 163private: 164 bool isTypeOp : 1; 165 void *Operand; 166 SourceRange Range; 167 168public: 169 CXXTypeidExpr(bool isTypeOp, void *op, QualType Ty, const SourceRange r) : 170 Expr(CXXTypeidExprClass, Ty), isTypeOp(isTypeOp), Operand(op), Range(r) {} 171 172 bool isTypeOperand() const { return isTypeOp; } 173 QualType getTypeOperand() const { 174 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 175 return QualType::getFromOpaquePtr(Operand); 176 } 177 Expr* getExprOperand() const { 178 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 179 return static_cast<Expr*>(Operand); 180 } 181 182 virtual SourceRange getSourceRange() const { 183 return Range; 184 } 185 static bool classof(const Stmt *T) { 186 return T->getStmtClass() == CXXTypeidExprClass; 187 } 188 static bool classof(const CXXTypeidExpr *) { return true; } 189 190 // Iterators 191 virtual child_iterator child_begin(); 192 virtual child_iterator child_end(); 193 194 virtual void EmitImpl(llvm::Serializer& S) const; 195 static CXXTypeidExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 196}; 197 198/// CXXThisExpr - Represents the "this" expression in C++, which is a 199/// pointer to the object on which the current member function is 200/// executing (C++ [expr.prim]p3). Example: 201/// 202/// @code 203/// class Foo { 204/// public: 205/// void bar(); 206/// void test() { this->bar(); } 207/// }; 208/// @endcode 209class CXXThisExpr : public Expr { 210 SourceLocation Loc; 211 212public: 213 CXXThisExpr(SourceLocation L, QualType Type) 214 : Expr(CXXThisExprClass, Type), Loc(L) { } 215 216 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 217 218 static bool classof(const Stmt *T) { 219 return T->getStmtClass() == CXXThisExprClass; 220 } 221 static bool classof(const CXXThisExpr *) { return true; } 222 223 // Iterators 224 virtual child_iterator child_begin(); 225 virtual child_iterator child_end(); 226 227 virtual void EmitImpl(llvm::Serializer& S) const; 228 static CXXThisExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 229}; 230 231/// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles 232/// 'throw' and 'throw' assignment-expression. When 233/// assignment-expression isn't present, Op will be null. 234/// 235class CXXThrowExpr : public Expr { 236 Stmt *Op; 237 SourceLocation ThrowLoc; 238public: 239 // Ty is the void type which is used as the result type of the 240 // exepression. The l is the location of the throw keyword. expr 241 // can by null, if the optional expression to throw isn't present. 242 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) : 243 Expr(CXXThrowExprClass, Ty), Op(expr), ThrowLoc(l) {} 244 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 245 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 246 247 virtual SourceRange getSourceRange() const { 248 if (getSubExpr() == 0) 249 return SourceRange(ThrowLoc, ThrowLoc); 250 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 251 } 252 253 static bool classof(const Stmt *T) { 254 return T->getStmtClass() == CXXThrowExprClass; 255 } 256 static bool classof(const CXXThrowExpr *) { return true; } 257 258 // Iterators 259 virtual child_iterator child_begin(); 260 virtual child_iterator child_end(); 261}; 262 263/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 264/// function call argument that was created from the corresponding 265/// parameter's default argument, when the call did not explicitly 266/// supply arguments for all of the parameters. 267class CXXDefaultArgExpr : public Expr { 268 ParmVarDecl *Param; 269public: 270 // Param is the parameter whose default argument is used by this 271 // expression. 272 explicit CXXDefaultArgExpr(ParmVarDecl *param) 273 : Expr(CXXDefaultArgExprClass, param->getDefaultArg()->getType()), 274 Param(param) { } 275 276 // Retrieve the parameter that the argument was created from. 277 const ParmVarDecl *getParam() const { return Param; } 278 ParmVarDecl *getParam() { return Param; } 279 280 // Retrieve the actual argument to the function call. 281 const Expr *getExpr() const { return Param->getDefaultArg(); } 282 Expr *getExpr() { return Param->getDefaultArg(); } 283 284 virtual SourceRange getSourceRange() const { 285 // Default argument expressions have no representation in the 286 // source, so they have an empty source range. 287 return SourceRange(); 288 } 289 290 static bool classof(const Stmt *T) { 291 return T->getStmtClass() == CXXDefaultArgExprClass; 292 } 293 static bool classof(const CXXDefaultArgExpr *) { return true; } 294 295 // Iterators 296 virtual child_iterator child_begin(); 297 virtual child_iterator child_end(); 298 299 // Serialization 300 virtual void EmitImpl(llvm::Serializer& S) const; 301 static CXXDefaultArgExpr* CreateImpl(llvm::Deserializer& D, 302 ASTContext& C); 303}; 304 305/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 306/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 307/// x = int(0.5); 308class CXXFunctionalCastExpr : public ExplicitCastExpr { 309 SourceLocation TyBeginLoc; 310 SourceLocation RParenLoc; 311public: 312 CXXFunctionalCastExpr(QualType ty, QualType writtenTy, 313 SourceLocation tyBeginLoc, Expr *castExpr, 314 SourceLocation rParenLoc) : 315 ExplicitCastExpr(CXXFunctionalCastExprClass, ty, castExpr, writtenTy), 316 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 317 318 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 319 SourceLocation getRParenLoc() const { return RParenLoc; } 320 321 virtual SourceRange getSourceRange() const { 322 return SourceRange(TyBeginLoc, RParenLoc); 323 } 324 static bool classof(const Stmt *T) { 325 return T->getStmtClass() == CXXFunctionalCastExprClass; 326 } 327 static bool classof(const CXXFunctionalCastExpr *) { return true; } 328 329 virtual void EmitImpl(llvm::Serializer& S) const; 330 static CXXFunctionalCastExpr * 331 CreateImpl(llvm::Deserializer& D, ASTContext& C); 332}; 333 334/// CXXZeroInitValueExpr - [C++ 5.2.3p2] 335/// Expression "T()" which creates a value-initialized Rvalue of non-class 336/// type T. 337/// 338class CXXZeroInitValueExpr : public Expr { 339 SourceLocation TyBeginLoc; 340 SourceLocation RParenLoc; 341 342public: 343 CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc, 344 SourceLocation rParenLoc ) : 345 Expr(CXXZeroInitValueExprClass, ty), 346 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 347 348 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 349 SourceLocation getRParenLoc() const { return RParenLoc; } 350 351 virtual SourceRange getSourceRange() const { 352 return SourceRange(TyBeginLoc, RParenLoc); 353 } 354 355 static bool classof(const Stmt *T) { 356 return T->getStmtClass() == CXXZeroInitValueExprClass; 357 } 358 static bool classof(const CXXZeroInitValueExpr *) { return true; } 359 360 // Iterators 361 virtual child_iterator child_begin(); 362 virtual child_iterator child_end(); 363 364 virtual void EmitImpl(llvm::Serializer& S) const; 365 static CXXZeroInitValueExpr * 366 CreateImpl(llvm::Deserializer& D, ASTContext& C); 367}; 368 369/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for 370/// statement, e.g: "if (int x = f()) {...}". 371/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the 372/// decl that it references. 373/// 374class CXXConditionDeclExpr : public DeclRefExpr { 375public: 376 CXXConditionDeclExpr(SourceLocation startLoc, 377 SourceLocation eqLoc, VarDecl *var) 378 : DeclRefExpr(CXXConditionDeclExprClass, var, 379 var->getType().getNonReferenceType(), startLoc) {} 380 381 virtual void Destroy(ASTContext& Ctx); 382 383 SourceLocation getStartLoc() const { return getLocation(); } 384 385 VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); } 386 const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); } 387 388 virtual SourceRange getSourceRange() const { 389 return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd()); 390 } 391 392 static bool classof(const Stmt *T) { 393 return T->getStmtClass() == CXXConditionDeclExprClass; 394 } 395 static bool classof(const CXXConditionDeclExpr *) { return true; } 396 397 // Iterators 398 virtual child_iterator child_begin(); 399 virtual child_iterator child_end(); 400 401 // FIXME: Implement these. 402 //virtual void EmitImpl(llvm::Serializer& S) const; 403 //static CXXConditionDeclExpr * 404 // CreateImpl(llvm::Deserializer& D, ASTContext& C); 405}; 406 407} // end namespace clang 408 409#endif 410