ExprCXX.h revision b4609806e9232593ece09ce08b630836e825865c
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/// CXXOperatorCallExpr - Represents a call to an overloaded operator 27/// written using operator syntax, e.g., "x + y" or "*p". While 28/// semantically equivalent to a normal call, this AST node provides 29/// better information about the syntactic representation of the call. 30class CXXOperatorCallExpr : public CallExpr { 31public: 32 CXXOperatorCallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 33 SourceLocation operatorloc) 34 : CallExpr(CXXOperatorCallExprClass, fn, args, numargs, t, operatorloc) { } 35 36 /// getOperator - Returns the kind of overloaded operator that this 37 /// expression refers to. 38 OverloadedOperatorKind getOperator() const; 39 40 /// getOperatorLoc - Returns the location of the operator symbol in 41 /// the expression. When @c getOperator()==OO_Call, this is the 42 /// location of the right parentheses; when @c 43 /// getOperator()==OO_Subscript, this is the location of the right 44 /// bracket. 45 SourceLocation getOperatorLoc() const { return getRParenLoc(); } 46 47 virtual SourceRange getSourceRange() const; 48 49 static bool classof(const Stmt *T) { 50 return T->getStmtClass() == CXXOperatorCallExprClass; 51 } 52 static bool classof(const CXXOperatorCallExpr *) { return true; } 53}; 54 55/// CXXNamedCastExpr - Abstract class common to all of the C++ "named" 56/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c 57/// const_cast. 58/// 59/// This abstract class is inherited by all of the classes 60/// representing "named" casts, e.g., CXXStaticCastExpr, 61/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr. 62class CXXNamedCastExpr : public ExplicitCastExpr { 63private: 64 SourceLocation Loc; // the location of the casting op 65 66protected: 67 CXXNamedCastExpr(StmtClass SC, QualType ty, Expr *op, QualType writtenTy, 68 SourceLocation l) 69 : ExplicitCastExpr(SC, ty, op, writtenTy), Loc(l) {} 70 71public: 72 const char *getCastName() const; 73 74 virtual SourceRange getSourceRange() const { 75 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 76 } 77 static bool classof(const Stmt *T) { 78 switch (T->getStmtClass()) { 79 case CXXNamedCastExprClass: 80 case CXXStaticCastExprClass: 81 case CXXDynamicCastExprClass: 82 case CXXReinterpretCastExprClass: 83 case CXXConstCastExprClass: 84 return true; 85 default: 86 return false; 87 } 88 } 89 static bool classof(const CXXNamedCastExpr *) { return true; } 90 91 virtual void EmitImpl(llvm::Serializer& S) const; 92 static CXXNamedCastExpr *CreateImpl(llvm::Deserializer& D, ASTContext& C, 93 StmtClass SC); 94}; 95 96/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]). 97/// 98/// This expression node represents a C++ static cast, e.g., 99/// @c static_cast<int>(1.0). 100class CXXStaticCastExpr : public CXXNamedCastExpr { 101public: 102 CXXStaticCastExpr(QualType ty, Expr *op, QualType writtenTy, SourceLocation l) 103 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, op, writtenTy, l) {} 104 105 static bool classof(const Stmt *T) { 106 return T->getStmtClass() == CXXStaticCastExprClass; 107 } 108 static bool classof(const CXXStaticCastExpr *) { return true; } 109}; 110 111/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression 112/// (C++ [expr.dynamic.cast]), which may perform a run-time check to 113/// determine how to perform the type cast. 114/// 115/// This expression node represents a dynamic cast, e.g., 116/// @c dynamic_cast<Derived*>(BasePtr). 117class CXXDynamicCastExpr : public CXXNamedCastExpr { 118public: 119 CXXDynamicCastExpr(QualType ty, Expr *op, QualType writtenTy, SourceLocation l) 120 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, op, writtenTy, l) {} 121 122 static bool classof(const Stmt *T) { 123 return T->getStmtClass() == CXXDynamicCastExprClass; 124 } 125 static bool classof(const CXXDynamicCastExpr *) { return true; } 126}; 127 128/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++ 129/// [expr.reinterpret.cast]), which provides a differently-typed view 130/// of a value but performs no actual work at run time. 131/// 132/// This expression node represents a reinterpret cast, e.g., 133/// @c reinterpret_cast<int>(VoidPtr). 134class CXXReinterpretCastExpr : public CXXNamedCastExpr { 135public: 136 CXXReinterpretCastExpr(QualType ty, Expr *op, QualType writtenTy, 137 SourceLocation l) 138 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, op, writtenTy, l) {} 139 140 static bool classof(const Stmt *T) { 141 return T->getStmtClass() == CXXReinterpretCastExprClass; 142 } 143 static bool classof(const CXXReinterpretCastExpr *) { return true; } 144}; 145 146/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]), 147/// which can remove type qualifiers but does not change the underlying value. 148/// 149/// This expression node represents a const cast, e.g., 150/// @c const_cast<char*>(PtrToConstChar). 151class CXXConstCastExpr : public CXXNamedCastExpr { 152public: 153 CXXConstCastExpr(QualType ty, Expr *op, QualType writtenTy, 154 SourceLocation l) 155 : CXXNamedCastExpr(CXXConstCastExprClass, ty, op, writtenTy, l) {} 156 157 static bool classof(const Stmt *T) { 158 return T->getStmtClass() == CXXConstCastExprClass; 159 } 160 static bool classof(const CXXConstCastExpr *) { return true; } 161}; 162 163/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal. 164/// 165class CXXBoolLiteralExpr : public Expr { 166 bool Value; 167 SourceLocation Loc; 168public: 169 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) : 170 Expr(CXXBoolLiteralExprClass, Ty), Value(val), Loc(l) {} 171 172 bool getValue() const { return Value; } 173 174 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 175 176 static bool classof(const Stmt *T) { 177 return T->getStmtClass() == CXXBoolLiteralExprClass; 178 } 179 static bool classof(const CXXBoolLiteralExpr *) { return true; } 180 181 // Iterators 182 virtual child_iterator child_begin(); 183 virtual child_iterator child_end(); 184}; 185 186/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets 187/// the type_info that corresponds to the supplied type, or the (possibly 188/// dynamic) type of the supplied expression. 189/// 190/// This represents code like @c typeid(int) or @c typeid(*objPtr) 191class CXXTypeidExpr : public Expr { 192private: 193 bool isTypeOp : 1; 194 void *Operand; 195 SourceRange Range; 196 197public: 198 CXXTypeidExpr(bool isTypeOp, void *op, QualType Ty, const SourceRange r) : 199 Expr(CXXTypeidExprClass, Ty), isTypeOp(isTypeOp), Operand(op), Range(r) {} 200 201 bool isTypeOperand() const { return isTypeOp; } 202 QualType getTypeOperand() const { 203 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 204 return QualType::getFromOpaquePtr(Operand); 205 } 206 Expr* getExprOperand() const { 207 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 208 return static_cast<Expr*>(Operand); 209 } 210 211 virtual SourceRange getSourceRange() const { 212 return Range; 213 } 214 static bool classof(const Stmt *T) { 215 return T->getStmtClass() == CXXTypeidExprClass; 216 } 217 static bool classof(const CXXTypeidExpr *) { return true; } 218 219 // Iterators 220 virtual child_iterator child_begin(); 221 virtual child_iterator child_end(); 222 223 virtual void EmitImpl(llvm::Serializer& S) const; 224 static CXXTypeidExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 225}; 226 227/// CXXThisExpr - Represents the "this" expression in C++, which is a 228/// pointer to the object on which the current member function is 229/// executing (C++ [expr.prim]p3). Example: 230/// 231/// @code 232/// class Foo { 233/// public: 234/// void bar(); 235/// void test() { this->bar(); } 236/// }; 237/// @endcode 238class CXXThisExpr : public Expr { 239 SourceLocation Loc; 240 241public: 242 CXXThisExpr(SourceLocation L, QualType Type) 243 : Expr(CXXThisExprClass, Type), Loc(L) { } 244 245 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 246 247 static bool classof(const Stmt *T) { 248 return T->getStmtClass() == CXXThisExprClass; 249 } 250 static bool classof(const CXXThisExpr *) { return true; } 251 252 // Iterators 253 virtual child_iterator child_begin(); 254 virtual child_iterator child_end(); 255 256 virtual void EmitImpl(llvm::Serializer& S) const; 257 static CXXThisExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 258}; 259 260/// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles 261/// 'throw' and 'throw' assignment-expression. When 262/// assignment-expression isn't present, Op will be null. 263/// 264class CXXThrowExpr : public Expr { 265 Stmt *Op; 266 SourceLocation ThrowLoc; 267public: 268 // Ty is the void type which is used as the result type of the 269 // exepression. The l is the location of the throw keyword. expr 270 // can by null, if the optional expression to throw isn't present. 271 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) : 272 Expr(CXXThrowExprClass, Ty), Op(expr), ThrowLoc(l) {} 273 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 274 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 275 276 virtual SourceRange getSourceRange() const { 277 if (getSubExpr() == 0) 278 return SourceRange(ThrowLoc, ThrowLoc); 279 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 280 } 281 282 static bool classof(const Stmt *T) { 283 return T->getStmtClass() == CXXThrowExprClass; 284 } 285 static bool classof(const CXXThrowExpr *) { return true; } 286 287 // Iterators 288 virtual child_iterator child_begin(); 289 virtual child_iterator child_end(); 290}; 291 292/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 293/// function call argument that was created from the corresponding 294/// parameter's default argument, when the call did not explicitly 295/// supply arguments for all of the parameters. 296class CXXDefaultArgExpr : public Expr { 297 ParmVarDecl *Param; 298public: 299 // Param is the parameter whose default argument is used by this 300 // expression. 301 explicit CXXDefaultArgExpr(ParmVarDecl *param) 302 : Expr(CXXDefaultArgExprClass, param->getDefaultArg()->getType()), 303 Param(param) { } 304 305 // Retrieve the parameter that the argument was created from. 306 const ParmVarDecl *getParam() const { return Param; } 307 ParmVarDecl *getParam() { return Param; } 308 309 // Retrieve the actual argument to the function call. 310 const Expr *getExpr() const { return Param->getDefaultArg(); } 311 Expr *getExpr() { return Param->getDefaultArg(); } 312 313 virtual SourceRange getSourceRange() const { 314 // Default argument expressions have no representation in the 315 // source, so they have an empty source range. 316 return SourceRange(); 317 } 318 319 static bool classof(const Stmt *T) { 320 return T->getStmtClass() == CXXDefaultArgExprClass; 321 } 322 static bool classof(const CXXDefaultArgExpr *) { return true; } 323 324 // Iterators 325 virtual child_iterator child_begin(); 326 virtual child_iterator child_end(); 327 328 // Serialization 329 virtual void EmitImpl(llvm::Serializer& S) const; 330 static CXXDefaultArgExpr* CreateImpl(llvm::Deserializer& D, 331 ASTContext& C); 332}; 333 334/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 335/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 336/// x = int(0.5); 337class CXXFunctionalCastExpr : public ExplicitCastExpr { 338 SourceLocation TyBeginLoc; 339 SourceLocation RParenLoc; 340public: 341 CXXFunctionalCastExpr(QualType ty, QualType writtenTy, 342 SourceLocation tyBeginLoc, Expr *castExpr, 343 SourceLocation rParenLoc) : 344 ExplicitCastExpr(CXXFunctionalCastExprClass, ty, castExpr, writtenTy), 345 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 346 347 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 348 SourceLocation getRParenLoc() const { return RParenLoc; } 349 350 virtual SourceRange getSourceRange() const { 351 return SourceRange(TyBeginLoc, RParenLoc); 352 } 353 static bool classof(const Stmt *T) { 354 return T->getStmtClass() == CXXFunctionalCastExprClass; 355 } 356 static bool classof(const CXXFunctionalCastExpr *) { return true; } 357 358 virtual void EmitImpl(llvm::Serializer& S) const; 359 static CXXFunctionalCastExpr * 360 CreateImpl(llvm::Deserializer& D, ASTContext& C); 361}; 362 363/// CXXZeroInitValueExpr - [C++ 5.2.3p2] 364/// Expression "T()" which creates a value-initialized Rvalue of non-class 365/// type T. 366/// 367class CXXZeroInitValueExpr : public Expr { 368 SourceLocation TyBeginLoc; 369 SourceLocation RParenLoc; 370 371public: 372 CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc, 373 SourceLocation rParenLoc ) : 374 Expr(CXXZeroInitValueExprClass, ty), 375 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 376 377 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 378 SourceLocation getRParenLoc() const { return RParenLoc; } 379 380 virtual SourceRange getSourceRange() const { 381 return SourceRange(TyBeginLoc, RParenLoc); 382 } 383 384 static bool classof(const Stmt *T) { 385 return T->getStmtClass() == CXXZeroInitValueExprClass; 386 } 387 static bool classof(const CXXZeroInitValueExpr *) { return true; } 388 389 // Iterators 390 virtual child_iterator child_begin(); 391 virtual child_iterator child_end(); 392 393 virtual void EmitImpl(llvm::Serializer& S) const; 394 static CXXZeroInitValueExpr * 395 CreateImpl(llvm::Deserializer& D, ASTContext& C); 396}; 397 398/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for 399/// statement, e.g: "if (int x = f()) {...}". 400/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the 401/// decl that it references. 402/// 403class CXXConditionDeclExpr : public DeclRefExpr { 404public: 405 CXXConditionDeclExpr(SourceLocation startLoc, 406 SourceLocation eqLoc, VarDecl *var) 407 : DeclRefExpr(CXXConditionDeclExprClass, var, 408 var->getType().getNonReferenceType(), startLoc) {} 409 410 virtual void Destroy(ASTContext& Ctx); 411 412 SourceLocation getStartLoc() const { return getLocation(); } 413 414 VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); } 415 const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); } 416 417 virtual SourceRange getSourceRange() const { 418 return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd()); 419 } 420 421 static bool classof(const Stmt *T) { 422 return T->getStmtClass() == CXXConditionDeclExprClass; 423 } 424 static bool classof(const CXXConditionDeclExpr *) { return true; } 425 426 // Iterators 427 virtual child_iterator child_begin(); 428 virtual child_iterator child_end(); 429 430 // FIXME: Implement these. 431 //virtual void EmitImpl(llvm::Serializer& S) const; 432 //static CXXConditionDeclExpr * 433 // CreateImpl(llvm::Deserializer& D, ASTContext& C); 434}; 435 436} // end namespace clang 437 438#endif 439