Expr.h revision 77ed8e4edf6ed78c53fb20ec3210aff2a59c9d87
1//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the Expr interface and subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_EXPR_H 15#define LLVM_CLANG_AST_EXPR_H 16 17#include "clang/AST/Stmt.h" 18#include "clang/AST/Type.h" 19#include "clang/AST/Decl.h" 20#include "llvm/ADT/APSInt.h" 21 22namespace clang { 23 class IdentifierInfo; 24 class Decl; 25 class ASTContext; 26 27/// Expr - This represents one expression. Note that Expr's are subclasses of 28/// Stmt. This allows an expression to be transparently used any place a Stmt 29/// is required. 30/// 31class Expr : public Stmt { 32 QualType TR; 33protected: 34 Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {} 35 ~Expr() {} 36public: 37 QualType getType() const { return TR; } 38 void setType(QualType t) { TR = t; } 39 40 /// SourceLocation tokens are not useful in isolation - they are low level 41 /// value objects created/interpreted by SourceManager. We assume AST 42 /// clients will have a pointer to the respective SourceManager. 43 virtual SourceRange getSourceRange() const = 0; 44 SourceLocation getLocStart() const { return getSourceRange().Begin(); } 45 SourceLocation getLocEnd() const { return getSourceRange().End(); } 46 47 /// getExprLoc - Return the preferred location for the arrow when diagnosing 48 /// a problem with a generic expression. 49 virtual SourceLocation getExprLoc() const { return getLocStart(); } 50 51 /// hasLocalSideEffect - Return true if this immediate expression has side 52 /// effects, not counting any sub-expressions. 53 bool hasLocalSideEffect() const; 54 55 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 56 /// incomplete type other than void. Nonarray expressions that can be lvalues: 57 /// - name, where name must be a variable 58 /// - e[i] 59 /// - (e), where e must be an lvalue 60 /// - e.name, where e must be an lvalue 61 /// - e->name 62 /// - *e, the type of e cannot be a function type 63 /// - string-constant 64 /// - reference type [C++ [expr]] 65 /// 66 enum isLvalueResult { 67 LV_Valid, 68 LV_NotObjectType, 69 LV_IncompleteVoidType, 70 LV_DuplicateVectorComponents, 71 LV_InvalidExpression 72 }; 73 isLvalueResult isLvalue() const; 74 75 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 76 /// does not have an incomplete type, does not have a const-qualified type, 77 /// and if it is a structure or union, does not have any member (including, 78 /// recursively, any member or element of all contained aggregates or unions) 79 /// with a const-qualified type. 80 enum isModifiableLvalueResult { 81 MLV_Valid, 82 MLV_NotObjectType, 83 MLV_IncompleteVoidType, 84 MLV_DuplicateVectorComponents, 85 MLV_InvalidExpression, 86 MLV_IncompleteType, 87 MLV_ConstQualified, 88 MLV_ArrayType 89 }; 90 isModifiableLvalueResult isModifiableLvalue() const; 91 92 bool isNullPointerConstant(ASTContext &Ctx) const; 93 94 /// isIntegerConstantExpr - Return true if this expression is a valid integer 95 /// constant expression, and, if so, return its value in Result. If not a 96 /// valid i-c-e, return false and fill in Loc (if specified) with the location 97 /// of the invalid expression. 98 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 99 SourceLocation *Loc = 0, 100 bool isEvaluated = true) const; 101 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 102 llvm::APSInt X(32); 103 return isIntegerConstantExpr(X, Ctx, Loc); 104 } 105 106 static bool classof(const Stmt *T) { 107 return T->getStmtClass() >= firstExprConstant && 108 T->getStmtClass() <= lastExprConstant; 109 } 110 static bool classof(const Expr *) { return true; } 111}; 112 113//===----------------------------------------------------------------------===// 114// Primary Expressions. 115//===----------------------------------------------------------------------===// 116 117/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 118/// enum, etc. 119class DeclRefExpr : public Expr { 120 Decl *D; // a ValueDecl or EnumConstantDecl 121 SourceLocation Loc; 122public: 123 DeclRefExpr(Decl *d, QualType t, SourceLocation l) : 124 Expr(DeclRefExprClass, t), D(d), Loc(l) {} 125 126 Decl *getDecl() { return D; } 127 const Decl *getDecl() const { return D; } 128 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 129 130 131 static bool classof(const Stmt *T) { 132 return T->getStmtClass() == DeclRefExprClass; 133 } 134 static bool classof(const DeclRefExpr *) { return true; } 135 136 // Iterators 137 virtual child_iterator child_begin(); 138 virtual child_iterator child_end(); 139}; 140 141/// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__. 142class PreDefinedExpr : public Expr { 143public: 144 enum IdentType { 145 Func, 146 Function, 147 PrettyFunction 148 }; 149 150private: 151 SourceLocation Loc; 152 IdentType Type; 153public: 154 PreDefinedExpr(SourceLocation l, QualType type, IdentType IT) 155 : Expr(PreDefinedExprClass, type), Loc(l), Type(IT) {} 156 157 IdentType getIdentType() const { return Type; } 158 159 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 160 161 static bool classof(const Stmt *T) { 162 return T->getStmtClass() == PreDefinedExprClass; 163 } 164 static bool classof(const PreDefinedExpr *) { return true; } 165 166 // Iterators 167 virtual child_iterator child_begin(); 168 virtual child_iterator child_end(); 169}; 170 171class IntegerLiteral : public Expr { 172 llvm::APInt Value; 173 SourceLocation Loc; 174public: 175 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 176 // or UnsignedLongLongTy 177 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 178 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) { 179 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 180 } 181 const llvm::APInt &getValue() const { return Value; } 182 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 183 184 static bool classof(const Stmt *T) { 185 return T->getStmtClass() == IntegerLiteralClass; 186 } 187 static bool classof(const IntegerLiteral *) { return true; } 188 189 // Iterators 190 virtual child_iterator child_begin(); 191 virtual child_iterator child_end(); 192}; 193 194class CharacterLiteral : public Expr { 195 unsigned Value; 196 SourceLocation Loc; 197public: 198 // type should be IntTy 199 CharacterLiteral(unsigned value, QualType type, SourceLocation l) 200 : Expr(CharacterLiteralClass, type), Value(value), Loc(l) { 201 } 202 SourceLocation getLoc() const { return Loc; } 203 204 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 205 206 unsigned getValue() const { return Value; } 207 208 static bool classof(const Stmt *T) { 209 return T->getStmtClass() == CharacterLiteralClass; 210 } 211 static bool classof(const CharacterLiteral *) { return true; } 212 213 // Iterators 214 virtual child_iterator child_begin(); 215 virtual child_iterator child_end(); 216}; 217 218class FloatingLiteral : public Expr { 219 float Value; // FIXME 220 SourceLocation Loc; 221public: 222 FloatingLiteral(float value, QualType type, SourceLocation l) 223 : Expr(FloatingLiteralClass, type), Value(value), Loc(l) {} 224 225 float getValue() const { return Value; } 226 227 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 228 229 static bool classof(const Stmt *T) { 230 return T->getStmtClass() == FloatingLiteralClass; 231 } 232 static bool classof(const FloatingLiteral *) { return true; } 233 234 // Iterators 235 virtual child_iterator child_begin(); 236 virtual child_iterator child_end(); 237}; 238 239/// StringLiteral - This represents a string literal expression, e.g. "foo" 240/// or L"bar" (wide strings). The actual string is returned by getStrData() 241/// is NOT null-terminated, and the length of the string is determined by 242/// calling getByteLength(). 243class StringLiteral : public Expr { 244 const char *StrData; 245 unsigned ByteLength; 246 bool IsWide; 247 // if the StringLiteral was composed using token pasting, both locations 248 // are needed. If not (the common case), firstTokLoc == lastTokLoc. 249 // FIXME: if space becomes an issue, we should create a sub-class. 250 SourceLocation firstTokLoc, lastTokLoc; 251public: 252 StringLiteral(const char *strData, unsigned byteLength, bool Wide, 253 QualType t, SourceLocation b, SourceLocation e); 254 virtual ~StringLiteral(); 255 256 const char *getStrData() const { return StrData; } 257 unsigned getByteLength() const { return ByteLength; } 258 bool isWide() const { return IsWide; } 259 260 virtual SourceRange getSourceRange() const { 261 return SourceRange(firstTokLoc,lastTokLoc); 262 } 263 static bool classof(const Stmt *T) { 264 return T->getStmtClass() == StringLiteralClass; 265 } 266 static bool classof(const StringLiteral *) { return true; } 267 268 // Iterators 269 virtual child_iterator child_begin(); 270 virtual child_iterator child_end(); 271}; 272 273/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 274/// AST node is only formed if full location information is requested. 275class ParenExpr : public Expr { 276 SourceLocation L, R; 277 Expr *Val; 278public: 279 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 280 : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {} 281 282 const Expr *getSubExpr() const { return Val; } 283 Expr *getSubExpr() { return Val; } 284 SourceRange getSourceRange() const { return SourceRange(L, R); } 285 286 static bool classof(const Stmt *T) { 287 return T->getStmtClass() == ParenExprClass; 288 } 289 static bool classof(const ParenExpr *) { return true; } 290 291 // Iterators 292 virtual child_iterator child_begin(); 293 virtual child_iterator child_end(); 294}; 295 296 297/// UnaryOperator - This represents the unary-expression's (except sizeof of 298/// types), the postinc/postdec operators from postfix-expression, and various 299/// extensions. 300class UnaryOperator : public Expr { 301public: 302 // Note that additions to this should also update the StmtVisitor class. 303 enum Opcode { 304 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 305 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 306 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 307 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 308 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 309 SizeOf, AlignOf, // [C99 6.5.3.4] Sizeof (expr, not type) operator. 310 Real, Imag, // "__real expr"/"__imag expr" Extension. 311 Extension // __extension__ marker. 312 }; 313private: 314 Expr *Val; 315 Opcode Opc; 316 SourceLocation Loc; 317public: 318 319 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 320 : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {} 321 322 Opcode getOpcode() const { return Opc; } 323 Expr *getSubExpr() const { return Val; } 324 325 /// getOperatorLoc - Return the location of the operator. 326 SourceLocation getOperatorLoc() const { return Loc; } 327 328 /// isPostfix - Return true if this is a postfix operation, like x++. 329 static bool isPostfix(Opcode Op); 330 331 bool isPostfix() const { return isPostfix(Opc); } 332 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 333 bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; } 334 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 335 336 /// getDecl - a recursive routine that derives the base decl for an 337 /// expression. For example, it will return the declaration for "s" from 338 /// the following complex expression "s.zz[2].bb.vv". 339 static bool isAddressable(Expr *e); 340 341 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 342 /// corresponds to, e.g. "sizeof" or "[pre]++" 343 static const char *getOpcodeStr(Opcode Op); 344 345 virtual SourceRange getSourceRange() const { 346 if (isPostfix()) 347 return SourceRange(Val->getLocStart(), Loc); 348 else 349 return SourceRange(Loc, Val->getLocEnd()); 350 } 351 virtual SourceLocation getExprLoc() const { return Loc; } 352 353 static bool classof(const Stmt *T) { 354 return T->getStmtClass() == UnaryOperatorClass; 355 } 356 static bool classof(const UnaryOperator *) { return true; } 357 358 // Iterators 359 virtual child_iterator child_begin(); 360 virtual child_iterator child_end(); 361}; 362 363/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of 364/// *types*. sizeof(expr) is handled by UnaryOperator. 365class SizeOfAlignOfTypeExpr : public Expr { 366 bool isSizeof; // true if sizeof, false if alignof. 367 QualType Ty; 368 SourceLocation OpLoc, RParenLoc; 369public: 370 SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType, 371 SourceLocation op, SourceLocation rp) : 372 Expr(SizeOfAlignOfTypeExprClass, resultType), 373 isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {} 374 375 bool isSizeOf() const { return isSizeof; } 376 QualType getArgumentType() const { return Ty; } 377 378 SourceLocation getOperatorLoc() const { return OpLoc; } 379 SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); } 380 381 static bool classof(const Stmt *T) { 382 return T->getStmtClass() == SizeOfAlignOfTypeExprClass; 383 } 384 static bool classof(const SizeOfAlignOfTypeExpr *) { return true; } 385 386 // Iterators 387 virtual child_iterator child_begin(); 388 virtual child_iterator child_end(); 389}; 390 391//===----------------------------------------------------------------------===// 392// Postfix Operators. 393//===----------------------------------------------------------------------===// 394 395/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 396class ArraySubscriptExpr : public Expr { 397 enum { LHS, RHS, END_EXPR=2 }; 398 Expr* SubExprs[END_EXPR]; 399 SourceLocation RBracketLoc; 400public: 401 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 402 SourceLocation rbracketloc) : 403 Expr(ArraySubscriptExprClass, t), 404 RBracketLoc(rbracketloc) { 405 SubExprs[LHS] = lhs; 406 SubExprs[RHS] = rhs; 407 } 408 409 /// An array access can be written A[4] or 4[A] (both are equivalent). 410 /// - getBase() and getIdx() always present the normalized view: A[4]. 411 /// In this case getBase() returns "A" and getIdx() returns "4". 412 /// - getLHS() and getRHS() present the syntactic view. e.g. for 413 /// 4[A] getLHS() returns "4". 414 415 Expr *getLHS() { return SubExprs[LHS]; } 416 const Expr *getLHS() const { return SubExprs[LHS]; } 417 418 Expr *getRHS() { return SubExprs[RHS]; } 419 const Expr *getRHS() const { return SubExprs[RHS]; } 420 421 Expr *getBase() { 422 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 423 } 424 425 const Expr *getBase() const { 426 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 427 } 428 429 Expr *getIdx() { 430 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 431 } 432 433 const Expr *getIdx() const { 434 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 435 } 436 437 438 SourceRange getSourceRange() const { 439 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 440 } 441 virtual SourceLocation getExprLoc() const { return RBracketLoc; } 442 443 static bool classof(const Stmt *T) { 444 return T->getStmtClass() == ArraySubscriptExprClass; 445 } 446 static bool classof(const ArraySubscriptExpr *) { return true; } 447 448 // Iterators 449 virtual child_iterator child_begin(); 450 virtual child_iterator child_end(); 451}; 452 453 454/// CallExpr - [C99 6.5.2.2] Function Calls. 455/// 456class CallExpr : public Expr { 457 enum { FN=0, ARGS_START=1 }; 458 Expr **SubExprs; 459 unsigned NumArgs; 460 SourceLocation RParenLoc; 461public: 462 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 463 SourceLocation rparenloc); 464 ~CallExpr() { 465 delete [] SubExprs; 466 } 467 468 const Expr *getCallee() const { return SubExprs[FN]; } 469 Expr *getCallee() { return SubExprs[FN]; } 470 471 /// getNumArgs - Return the number of actual arguments to this call. 472 /// 473 unsigned getNumArgs() const { return NumArgs; } 474 475 /// getArg - Return the specified argument. 476 Expr *getArg(unsigned Arg) { 477 assert(Arg < NumArgs && "Arg access out of range!"); 478 return SubExprs[Arg+ARGS_START]; 479 } 480 const Expr *getArg(unsigned Arg) const { 481 assert(Arg < NumArgs && "Arg access out of range!"); 482 return SubExprs[Arg+ARGS_START]; 483 } 484 485 /// getNumCommas - Return the number of commas that must have been present in 486 /// this function call. 487 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 488 489 bool isBuiltinClassifyType(llvm::APSInt &Result) const; 490 491 SourceRange getSourceRange() const { 492 return SourceRange(getCallee()->getLocStart(), RParenLoc); 493 } 494 495 static bool classof(const Stmt *T) { 496 return T->getStmtClass() == CallExprClass; 497 } 498 static bool classof(const CallExpr *) { return true; } 499 500 // Iterators 501 virtual child_iterator child_begin(); 502 virtual child_iterator child_end(); 503}; 504 505/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 506/// 507class MemberExpr : public Expr { 508 Expr *Base; 509 FieldDecl *MemberDecl; 510 SourceLocation MemberLoc; 511 bool IsArrow; // True if this is "X->F", false if this is "X.F". 512public: 513 MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l) 514 : Expr(MemberExprClass, memberdecl->getType()), 515 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 516 517 Expr *getBase() const { return Base; } 518 FieldDecl *getMemberDecl() const { return MemberDecl; } 519 bool isArrow() const { return IsArrow; } 520 521 virtual SourceRange getSourceRange() const { 522 return SourceRange(getBase()->getLocStart(), MemberLoc); 523 } 524 virtual SourceLocation getExprLoc() const { return MemberLoc; } 525 526 static bool classof(const Stmt *T) { 527 return T->getStmtClass() == MemberExprClass; 528 } 529 static bool classof(const MemberExpr *) { return true; } 530}; 531 532/// OCUVectorElementExpr - This represents access to specific elements of a 533/// vector, and may occur on the left hand side or right hand side. For example 534/// the following is legal: "V.xy = V.zw" if V is a 4 element ocu vector. 535/// 536class OCUVectorElementExpr : public Expr { 537 Expr *Base; 538 IdentifierInfo &Accessor; 539 SourceLocation AccessorLoc; 540public: 541 enum ElementType { 542 Point, // xywz 543 Color, // rgba 544 Texture // stpq 545 }; 546 OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 547 SourceLocation loc) 548 : Expr(OCUVectorElementExprClass, ty), 549 Base(base), Accessor(accessor), AccessorLoc(loc) {} 550 551 const Expr *getBase() const { return Base; } 552 Expr *getBase() { return Base; } 553 554 IdentifierInfo &getAccessor() const { return Accessor; } 555 556 /// getNumElements - Get the number of components being selected. 557 unsigned getNumElements() const; 558 559 /// getElementType - Determine whether the components of this access are 560 /// "point" "color" or "texture" elements. 561 ElementType getElementType() const; 562 563 /// containsDuplicateElements - Return true if any element access is 564 /// repeated. 565 bool containsDuplicateElements() const; 566 567 /// getEncodedElementAccess - Encode the elements accessed into a bit vector. 568 /// The encoding currently uses 2-bit bitfields, but clients should use the 569 /// accessors below to access them. 570 /// 571 unsigned getEncodedElementAccess() const; 572 573 /// getAccessedFieldNo - Given an encoded value and a result number, return 574 /// the input field number being accessed. 575 static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) { 576 return (EncodedVal >> (Idx*2)) & 3; 577 } 578 579 virtual SourceRange getSourceRange() const { 580 return SourceRange(getBase()->getLocStart(), AccessorLoc); 581 } 582 static bool classof(const Stmt *T) { 583 return T->getStmtClass() == OCUVectorElementExprClass; 584 } 585 static bool classof(const OCUVectorElementExpr *) { return true; } 586}; 587 588/// CompoundLiteralExpr - [C99 6.5.2.5] 589/// 590class CompoundLiteralExpr : public Expr { 591 Expr *Init; 592public: 593 CompoundLiteralExpr(QualType ty, Expr *init) : 594 Expr(CompoundLiteralExprClass, ty), Init(init) {} 595 596 const Expr *getInitializer() const { return Init; } 597 Expr *getInitializer() { return Init; } 598 599 virtual SourceRange getSourceRange() const { return Init->getSourceRange(); } 600 601 static bool classof(const Stmt *T) { 602 return T->getStmtClass() == CompoundLiteralExprClass; 603 } 604 static bool classof(const CompoundLiteralExpr *) { return true; } 605}; 606 607/// ImplicitCastExpr - Allows us to explicitly represent implicit type 608/// conversions. For example: converting T[]->T*, void f()->void (*f)(), 609/// float->double, short->int, etc. 610/// 611class ImplicitCastExpr : public Expr { 612 Expr *Op; 613public: 614 ImplicitCastExpr(QualType ty, Expr *op) : 615 Expr(ImplicitCastExprClass, ty), Op(op) {} 616 617 Expr *getSubExpr() { return Op; } 618 const Expr *getSubExpr() const { return Op; } 619 620 virtual SourceRange getSourceRange() const { return Op->getSourceRange(); } 621 622 static bool classof(const Stmt *T) { 623 return T->getStmtClass() == ImplicitCastExprClass; 624 } 625 static bool classof(const ImplicitCastExpr *) { return true; } 626}; 627 628/// CastExpr - [C99 6.5.4] Cast Operators. 629/// 630class CastExpr : public Expr { 631 Expr *Op; 632 SourceLocation Loc; // the location of the left paren 633public: 634 CastExpr(QualType ty, Expr *op, SourceLocation l) : 635 Expr(CastExprClass, ty), Op(op), Loc(l) {} 636 637 SourceLocation getLParenLoc() const { return Loc; } 638 639 Expr *getSubExpr() const { return Op; } 640 641 virtual SourceRange getSourceRange() const { 642 return SourceRange(Loc, getSubExpr()->getSourceRange().End()); 643 } 644 static bool classof(const Stmt *T) { 645 return T->getStmtClass() == CastExprClass; 646 } 647 static bool classof(const CastExpr *) { return true; } 648}; 649 650class BinaryOperator : public Expr { 651public: 652 enum Opcode { 653 // Operators listed in order of precedence. 654 // Note that additions to this should also update the StmtVisitor class. 655 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 656 Add, Sub, // [C99 6.5.6] Additive operators. 657 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 658 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 659 EQ, NE, // [C99 6.5.9] Equality operators. 660 And, // [C99 6.5.10] Bitwise AND operator. 661 Xor, // [C99 6.5.11] Bitwise XOR operator. 662 Or, // [C99 6.5.12] Bitwise OR operator. 663 LAnd, // [C99 6.5.13] Logical AND operator. 664 LOr, // [C99 6.5.14] Logical OR operator. 665 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 666 DivAssign, RemAssign, 667 AddAssign, SubAssign, 668 ShlAssign, ShrAssign, 669 AndAssign, XorAssign, 670 OrAssign, 671 Comma // [C99 6.5.17] Comma operator. 672 }; 673 674 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy) 675 : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) { 676 assert(!isCompoundAssignmentOp() && 677 "Use ArithAssignBinaryOperator for compound assignments"); 678 } 679 680 Opcode getOpcode() const { return Opc; } 681 Expr *getLHS() const { return LHS; } 682 Expr *getRHS() const { return RHS; } 683 virtual SourceRange getSourceRange() const { 684 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 685 } 686 687 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 688 /// corresponds to, e.g. "<<=". 689 static const char *getOpcodeStr(Opcode Op); 690 691 /// predicates to categorize the respective opcodes. 692 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 693 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 694 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 695 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 696 bool isRelationalOp() const { return Opc >= LT && Opc <= GE; } 697 bool isEqualityOp() const { return Opc == EQ || Opc == NE; } 698 bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; } 699 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 700 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 701 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 702 703 static bool classof(const Stmt *T) { 704 return T->getStmtClass() == BinaryOperatorClass; 705 } 706 static bool classof(const BinaryOperator *) { return true; } 707private: 708 Expr *LHS, *RHS; 709 Opcode Opc; 710protected: 711 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, bool dead) 712 : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) { 713 } 714}; 715 716/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 717/// track of the type the operation is performed in. Due to the semantics of 718/// these operators, the operands are promoted, the aritmetic performed, an 719/// implicit conversion back to the result type done, then the assignment takes 720/// place. This captures the intermediate type which the computation is done 721/// in. 722class CompoundAssignOperator : public BinaryOperator { 723 QualType ComputationType; 724public: 725 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 726 QualType ResType, QualType CompType) 727 : BinaryOperator(lhs, rhs, opc, ResType, true), ComputationType(CompType) { 728 assert(isCompoundAssignmentOp() && 729 "Only should be used for compound assignments"); 730 } 731 732 QualType getComputationType() const { return ComputationType; } 733 734 static bool classof(const CompoundAssignOperator *) { return true; } 735 static bool classof(const BinaryOperator *B) { 736 return B->isCompoundAssignmentOp(); 737 } 738 static bool classof(const Stmt *S) { 739 return isa<BinaryOperator>(S) && classof(cast<BinaryOperator>(S)); 740 } 741}; 742 743/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 744/// GNU "missing LHS" extension is in use. 745/// 746class ConditionalOperator : public Expr { 747 Expr *Cond, *LHS, *RHS; // Left/Middle/Right hand sides. 748public: 749 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 750 : Expr(ConditionalOperatorClass, t), Cond(cond), LHS(lhs), RHS(rhs) {} 751 752 Expr *getCond() const { return Cond; } 753 Expr *getLHS() const { return LHS; } 754 Expr *getRHS() const { return RHS; } 755 756 virtual SourceRange getSourceRange() const { 757 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 758 } 759 static bool classof(const Stmt *T) { 760 return T->getStmtClass() == ConditionalOperatorClass; 761 } 762 static bool classof(const ConditionalOperator *) { return true; } 763}; 764 765/// AddrLabelExpr - The GNU address of label extension, representing &&label. 766class AddrLabelExpr : public Expr { 767 SourceLocation AmpAmpLoc, LabelLoc; 768 LabelStmt *Label; 769public: 770 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 771 QualType t) 772 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 773 774 virtual SourceRange getSourceRange() const { 775 return SourceRange(AmpAmpLoc, LabelLoc); 776 } 777 778 LabelStmt *getLabel() const { return Label; } 779 780 static bool classof(const Stmt *T) { 781 return T->getStmtClass() == AddrLabelExprClass; 782 } 783 static bool classof(const AddrLabelExpr *) { return true; } 784}; 785 786/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 787/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 788/// takes the value of the last subexpression. 789class StmtExpr : public Expr { 790 CompoundStmt *SubStmt; 791 SourceLocation LParenLoc, RParenLoc; 792public: 793 StmtExpr(CompoundStmt *substmt, QualType T, 794 SourceLocation lp, SourceLocation rp) : 795 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 796 797 CompoundStmt *getSubStmt() { return SubStmt; } 798 const CompoundStmt *getSubStmt() const { return SubStmt; } 799 800 virtual SourceRange getSourceRange() const { 801 return SourceRange(LParenLoc, RParenLoc); 802 } 803 804 static bool classof(const Stmt *T) { 805 return T->getStmtClass() == StmtExprClass; 806 } 807 static bool classof(const StmtExpr *) { return true; } 808}; 809 810/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 811/// This AST node represents a function that returns 1 if two *types* (not 812/// expressions) are compatible. The result of this built-in function can be 813/// used in integer constant expressions. 814class TypesCompatibleExpr : public Expr { 815 QualType Type1; 816 QualType Type2; 817 SourceLocation BuiltinLoc, RParenLoc; 818public: 819 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 820 QualType t1, QualType t2, SourceLocation RP) : 821 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 822 BuiltinLoc(BLoc), RParenLoc(RP) {} 823 824 QualType getArgType1() const { return Type1; } 825 QualType getArgType2() const { return Type2; } 826 827 int typesAreCompatible() const { return Type::typesAreCompatible(Type1,Type2); } 828 829 virtual SourceRange getSourceRange() const { 830 return SourceRange(BuiltinLoc, RParenLoc); 831 } 832 static bool classof(const Stmt *T) { 833 return T->getStmtClass() == TypesCompatibleExprClass; 834 } 835 static bool classof(const TypesCompatibleExpr *) { return true; } 836}; 837 838/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 839/// This AST node is similar to the conditional operator (?:) in C, with 840/// the following exceptions: 841/// - the test expression much be a constant expression. 842/// - the expression returned has it's type unaltered by promotion rules. 843/// - does not evaluate the expression that was not chosen. 844class ChooseExpr : public Expr { 845 Expr *Cond, *LHS, *RHS; // First, second, and third arguments. 846 SourceLocation BuiltinLoc, RParenLoc; 847public: 848 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 849 SourceLocation RP) 850 : Expr(ChooseExprClass, t), 851 Cond(cond), LHS(lhs), RHS(rhs), BuiltinLoc(BLoc), RParenLoc(RP) {} 852 853 Expr *getCond() { return Cond; } 854 Expr *getLHS() { return LHS; } 855 Expr *getRHS() { return RHS; } 856 857 const Expr *getCond() const { return Cond; } 858 const Expr *getLHS() const { return LHS; } 859 const Expr *getRHS() const { return RHS; } 860 861 virtual SourceRange getSourceRange() const { 862 return SourceRange(BuiltinLoc, RParenLoc); 863 } 864 static bool classof(const Stmt *T) { 865 return T->getStmtClass() == ChooseExprClass; 866 } 867 static bool classof(const ChooseExpr *) { return true; } 868}; 869 870/// ObjCStringLiteral, used for Objective-C string literals 871/// i.e. @"foo". 872class ObjCStringLiteral : public Expr { 873 StringLiteral *String; 874public: 875 ObjCStringLiteral(StringLiteral *SL, QualType T) 876 : Expr(ObjCStringLiteralClass, T), String(SL) {} 877 878 StringLiteral* getString() { return String; } 879 880 const StringLiteral* getString() const { return String; } 881 882 virtual SourceRange getSourceRange() const { 883 return String->getSourceRange(); 884 } 885 886 static bool classof(const Stmt *T) { 887 return T->getStmtClass() == ObjCStringLiteralClass; 888 } 889 static bool classof(const ObjCStringLiteral *) { return true; } 890}; 891 892/// ObjCEncodeExpr, used for @encode in Objective-C. 893class ObjCEncodeExpr : public Expr { 894 QualType EncType; 895 SourceLocation EncLoc, RParenLoc; 896public: 897 ObjCEncodeExpr(QualType T, QualType ET, 898 SourceLocation enc, SourceLocation rp) 899 : Expr(ObjCEncodeExprClass, T), EncType(ET), EncLoc(enc), RParenLoc(rp) {} 900 901 SourceRange getSourceRange() const { return SourceRange(EncLoc, RParenLoc); } 902 903 QualType getEncodedType() const { return EncType; } 904 905 static bool classof(const Stmt *T) { 906 return T->getStmtClass() == ObjCEncodeExprClass; 907 } 908 static bool classof(const ObjCEncodeExpr *) { return true; } 909}; 910 911} // end namespace clang 912 913#endif 914