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