Expr.h revision 390d50a725497e99247dc104a7d2c2a255d3af14
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 "clang/Basic/IdentifierTable.h" 21#include "llvm/ADT/APSInt.h" 22#include "llvm/ADT/APFloat.h" 23 24namespace clang { 25 class IdentifierInfo; 26 class Selector; 27 class Decl; 28 class ASTContext; 29 30/// Expr - This represents one expression. Note that Expr's are subclasses of 31/// Stmt. This allows an expression to be transparently used any place a Stmt 32/// is required. 33/// 34class Expr : public Stmt { 35 QualType TR; 36protected: 37 Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {} 38public: 39 QualType getType() const { return TR; } 40 void setType(QualType t) { TR = t; } 41 42 /// SourceLocation tokens are not useful in isolation - they are low level 43 /// value objects created/interpreted by SourceManager. We assume AST 44 /// clients will have a pointer to the respective SourceManager. 45 virtual SourceRange getSourceRange() const = 0; 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 /// isConstantExpr - Return true if this expression is a valid constant expr. 106 bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const; 107 108 static bool classof(const Stmt *T) { 109 return T->getStmtClass() >= firstExprConstant && 110 T->getStmtClass() <= lastExprConstant; 111 } 112 static bool classof(const Expr *) { return true; } 113}; 114 115//===----------------------------------------------------------------------===// 116// Primary Expressions. 117//===----------------------------------------------------------------------===// 118 119/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 120/// enum, etc. 121class DeclRefExpr : public Expr { 122 ValueDecl *D; 123 SourceLocation Loc; 124public: 125 DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) : 126 Expr(DeclRefExprClass, t), D(d), Loc(l) {} 127 128 ValueDecl *getDecl() { return D; } 129 const ValueDecl *getDecl() const { return D; } 130 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 131 132 133 static bool classof(const Stmt *T) { 134 return T->getStmtClass() == DeclRefExprClass; 135 } 136 static bool classof(const DeclRefExpr *) { return true; } 137 138 // Iterators 139 virtual child_iterator child_begin(); 140 virtual child_iterator child_end(); 141}; 142 143/// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__. 144class PreDefinedExpr : public Expr { 145public: 146 enum IdentType { 147 Func, 148 Function, 149 PrettyFunction 150 }; 151 152private: 153 SourceLocation Loc; 154 IdentType Type; 155public: 156 PreDefinedExpr(SourceLocation l, QualType type, IdentType IT) 157 : Expr(PreDefinedExprClass, type), Loc(l), Type(IT) {} 158 159 IdentType getIdentType() const { return Type; } 160 161 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 162 163 static bool classof(const Stmt *T) { 164 return T->getStmtClass() == PreDefinedExprClass; 165 } 166 static bool classof(const PreDefinedExpr *) { return true; } 167 168 // Iterators 169 virtual child_iterator child_begin(); 170 virtual child_iterator child_end(); 171}; 172 173class IntegerLiteral : public Expr { 174 llvm::APInt Value; 175 SourceLocation Loc; 176public: 177 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 178 // or UnsignedLongLongTy 179 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 180 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) { 181 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 182 } 183 const llvm::APInt &getValue() const { return Value; } 184 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 185 186 static bool classof(const Stmt *T) { 187 return T->getStmtClass() == IntegerLiteralClass; 188 } 189 static bool classof(const IntegerLiteral *) { return true; } 190 191 // Iterators 192 virtual child_iterator child_begin(); 193 virtual child_iterator child_end(); 194}; 195 196class CharacterLiteral : public Expr { 197 unsigned Value; 198 SourceLocation Loc; 199public: 200 // type should be IntTy 201 CharacterLiteral(unsigned value, QualType type, SourceLocation l) 202 : Expr(CharacterLiteralClass, type), Value(value), Loc(l) { 203 } 204 SourceLocation getLoc() const { return Loc; } 205 206 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 207 208 unsigned getValue() const { return Value; } 209 210 static bool classof(const Stmt *T) { 211 return T->getStmtClass() == CharacterLiteralClass; 212 } 213 static bool classof(const CharacterLiteral *) { return true; } 214 215 // Iterators 216 virtual child_iterator child_begin(); 217 virtual child_iterator child_end(); 218}; 219 220class FloatingLiteral : public Expr { 221 llvm::APFloat Value; 222 SourceLocation Loc; 223public: 224 FloatingLiteral(const llvm::APFloat &V, QualType Type, SourceLocation L) 225 : Expr(FloatingLiteralClass, Type), Value(V), Loc(L) {} 226 227 const llvm::APFloat &getValue() const { return Value; } 228 229 /// getValueAsDouble - This returns the value as an inaccurate double. Note 230 /// that this may cause loss of precision, but is useful for debugging dumps 231 /// etc. 232 double getValueAsDouble() const { 233 // FIXME: We need something for long double here. 234 if (cast<BuiltinType>(getType())->getKind() == BuiltinType::Float) 235 return Value.convertToFloat(); 236 else 237 return Value.convertToDouble(); 238 } 239 240 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 241 242 static bool classof(const Stmt *T) { 243 return T->getStmtClass() == FloatingLiteralClass; 244 } 245 static bool classof(const FloatingLiteral *) { return true; } 246 247 // Iterators 248 virtual child_iterator child_begin(); 249 virtual child_iterator child_end(); 250}; 251 252/// ImaginaryLiteral - We support imaginary integer and floating point literals, 253/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 254/// IntegerLiteral classes. Instances of this class always have a Complex type 255/// whose element type matches the subexpression. 256/// 257class ImaginaryLiteral : public Expr { 258 Expr *Val; 259public: 260 ImaginaryLiteral(Expr *val, QualType Ty) 261 : Expr(ImaginaryLiteralClass, Ty), Val(val) {} 262 263 const Expr *getSubExpr() const { return Val; } 264 Expr *getSubExpr() { return Val; } 265 266 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 267 static bool classof(const Stmt *T) { 268 return T->getStmtClass() == ImaginaryLiteralClass; 269 } 270 static bool classof(const ImaginaryLiteral *) { return true; } 271 272 // Iterators 273 virtual child_iterator child_begin(); 274 virtual child_iterator child_end(); 275}; 276 277/// StringLiteral - This represents a string literal expression, e.g. "foo" 278/// or L"bar" (wide strings). The actual string is returned by getStrData() 279/// is NOT null-terminated, and the length of the string is determined by 280/// calling getByteLength(). 281class StringLiteral : public Expr { 282 const char *StrData; 283 unsigned ByteLength; 284 bool IsWide; 285 // if the StringLiteral was composed using token pasting, both locations 286 // are needed. If not (the common case), firstTokLoc == lastTokLoc. 287 // FIXME: if space becomes an issue, we should create a sub-class. 288 SourceLocation firstTokLoc, lastTokLoc; 289public: 290 StringLiteral(const char *strData, unsigned byteLength, bool Wide, 291 QualType t, SourceLocation b, SourceLocation e); 292 virtual ~StringLiteral(); 293 294 const char *getStrData() const { return StrData; } 295 unsigned getByteLength() const { return ByteLength; } 296 bool isWide() const { return IsWide; } 297 298 virtual SourceRange getSourceRange() const { 299 return SourceRange(firstTokLoc,lastTokLoc); 300 } 301 static bool classof(const Stmt *T) { 302 return T->getStmtClass() == StringLiteralClass; 303 } 304 static bool classof(const StringLiteral *) { return true; } 305 306 // Iterators 307 virtual child_iterator child_begin(); 308 virtual child_iterator child_end(); 309}; 310 311/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 312/// AST node is only formed if full location information is requested. 313class ParenExpr : public Expr { 314 SourceLocation L, R; 315 Expr *Val; 316public: 317 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 318 : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {} 319 320 const Expr *getSubExpr() const { return Val; } 321 Expr *getSubExpr() { return Val; } 322 SourceRange getSourceRange() const { return SourceRange(L, R); } 323 324 static bool classof(const Stmt *T) { 325 return T->getStmtClass() == ParenExprClass; 326 } 327 static bool classof(const ParenExpr *) { return true; } 328 329 // Iterators 330 virtual child_iterator child_begin(); 331 virtual child_iterator child_end(); 332}; 333 334 335/// UnaryOperator - This represents the unary-expression's (except sizeof of 336/// types), the postinc/postdec operators from postfix-expression, and various 337/// extensions. 338/// 339/// Notes on various nodes: 340/// 341/// Real/Imag - These return the real/imag part of a complex operand. If 342/// applied to a non-complex value, the former returns its operand and the 343/// later returns zero in the type of the operand. 344/// 345/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose 346/// subexpression is a compound literal with the various MemberExpr and 347/// ArraySubscriptExpr's applied to it. 348/// 349class UnaryOperator : public Expr { 350public: 351 // Note that additions to this should also update the StmtVisitor class. 352 enum Opcode { 353 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 354 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 355 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 356 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 357 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 358 SizeOf, AlignOf, // [C99 6.5.3.4] Sizeof (expr, not type) operator. 359 Real, Imag, // "__real expr"/"__imag expr" Extension. 360 Extension, // __extension__ marker. 361 OffsetOf // __builtin_offsetof 362 }; 363private: 364 Expr *Val; 365 Opcode Opc; 366 SourceLocation Loc; 367public: 368 369 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 370 : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {} 371 372 Opcode getOpcode() const { return Opc; } 373 Expr *getSubExpr() const { return Val; } 374 375 /// getOperatorLoc - Return the location of the operator. 376 SourceLocation getOperatorLoc() const { return Loc; } 377 378 /// isPostfix - Return true if this is a postfix operation, like x++. 379 static bool isPostfix(Opcode Op); 380 381 bool isPostfix() const { return isPostfix(Opc); } 382 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 383 bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; } 384 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 385 386 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 387 /// corresponds to, e.g. "sizeof" or "[pre]++" 388 static const char *getOpcodeStr(Opcode Op); 389 390 virtual SourceRange getSourceRange() const { 391 if (isPostfix()) 392 return SourceRange(Val->getLocStart(), Loc); 393 else 394 return SourceRange(Loc, Val->getLocEnd()); 395 } 396 virtual SourceLocation getExprLoc() const { return Loc; } 397 398 static bool classof(const Stmt *T) { 399 return T->getStmtClass() == UnaryOperatorClass; 400 } 401 static bool classof(const UnaryOperator *) { return true; } 402 403 // Iterators 404 virtual child_iterator child_begin(); 405 virtual child_iterator child_end(); 406}; 407 408/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of 409/// *types*. sizeof(expr) is handled by UnaryOperator. 410class SizeOfAlignOfTypeExpr : public Expr { 411 bool isSizeof; // true if sizeof, false if alignof. 412 QualType Ty; 413 SourceLocation OpLoc, RParenLoc; 414public: 415 SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType, 416 SourceLocation op, SourceLocation rp) : 417 Expr(SizeOfAlignOfTypeExprClass, resultType), 418 isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {} 419 420 bool isSizeOf() const { return isSizeof; } 421 QualType getArgumentType() const { return Ty; } 422 423 SourceLocation getOperatorLoc() const { return OpLoc; } 424 SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); } 425 426 static bool classof(const Stmt *T) { 427 return T->getStmtClass() == SizeOfAlignOfTypeExprClass; 428 } 429 static bool classof(const SizeOfAlignOfTypeExpr *) { return true; } 430 431 // Iterators 432 virtual child_iterator child_begin(); 433 virtual child_iterator child_end(); 434}; 435 436//===----------------------------------------------------------------------===// 437// Postfix Operators. 438//===----------------------------------------------------------------------===// 439 440/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 441class ArraySubscriptExpr : public Expr { 442 enum { LHS, RHS, END_EXPR=2 }; 443 Expr* SubExprs[END_EXPR]; 444 SourceLocation RBracketLoc; 445public: 446 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 447 SourceLocation rbracketloc) 448 : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) { 449 SubExprs[LHS] = lhs; 450 SubExprs[RHS] = rhs; 451 } 452 453 /// An array access can be written A[4] or 4[A] (both are equivalent). 454 /// - getBase() and getIdx() always present the normalized view: A[4]. 455 /// In this case getBase() returns "A" and getIdx() returns "4". 456 /// - getLHS() and getRHS() present the syntactic view. e.g. for 457 /// 4[A] getLHS() returns "4". 458 459 Expr *getLHS() { return SubExprs[LHS]; } 460 const Expr *getLHS() const { return SubExprs[LHS]; } 461 462 Expr *getRHS() { return SubExprs[RHS]; } 463 const Expr *getRHS() const { return SubExprs[RHS]; } 464 465 Expr *getBase() { 466 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 467 } 468 469 const Expr *getBase() const { 470 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 471 } 472 473 Expr *getIdx() { 474 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 475 } 476 477 const Expr *getIdx() const { 478 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 479 } 480 481 482 SourceRange getSourceRange() const { 483 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 484 } 485 virtual SourceLocation getExprLoc() const { return RBracketLoc; } 486 487 static bool classof(const Stmt *T) { 488 return T->getStmtClass() == ArraySubscriptExprClass; 489 } 490 static bool classof(const ArraySubscriptExpr *) { return true; } 491 492 // Iterators 493 virtual child_iterator child_begin(); 494 virtual child_iterator child_end(); 495}; 496 497 498/// CallExpr - [C99 6.5.2.2] Function Calls. 499/// 500class CallExpr : public Expr { 501 enum { FN=0, ARGS_START=1 }; 502 Expr **SubExprs; 503 unsigned NumArgs; 504 SourceLocation RParenLoc; 505public: 506 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 507 SourceLocation rparenloc); 508 ~CallExpr() { 509 delete [] SubExprs; 510 } 511 512 const Expr *getCallee() const { return SubExprs[FN]; } 513 Expr *getCallee() { return SubExprs[FN]; } 514 515 /// getNumArgs - Return the number of actual arguments to this call. 516 /// 517 unsigned getNumArgs() const { return NumArgs; } 518 519 /// getArg - Return the specified argument. 520 Expr *getArg(unsigned Arg) { 521 assert(Arg < NumArgs && "Arg access out of range!"); 522 return SubExprs[Arg+ARGS_START]; 523 } 524 const Expr *getArg(unsigned Arg) const { 525 assert(Arg < NumArgs && "Arg access out of range!"); 526 return SubExprs[Arg+ARGS_START]; 527 } 528 529 /// getNumCommas - Return the number of commas that must have been present in 530 /// this function call. 531 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 532 533 bool isBuiltinClassifyType(llvm::APSInt &Result) const; 534 535 SourceRange getSourceRange() const { 536 return SourceRange(getCallee()->getLocStart(), RParenLoc); 537 } 538 539 static bool classof(const Stmt *T) { 540 return T->getStmtClass() == CallExprClass; 541 } 542 static bool classof(const CallExpr *) { return true; } 543 544 // Iterators 545 virtual child_iterator child_begin(); 546 virtual child_iterator child_end(); 547}; 548 549/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 550/// 551class MemberExpr : public Expr { 552 Expr *Base; 553 FieldDecl *MemberDecl; 554 SourceLocation MemberLoc; 555 bool IsArrow; // True if this is "X->F", false if this is "X.F". 556public: 557 MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l) 558 : Expr(MemberExprClass, memberdecl->getType()), 559 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 560 561 Expr *getBase() const { return Base; } 562 FieldDecl *getMemberDecl() const { return MemberDecl; } 563 bool isArrow() const { return IsArrow; } 564 565 virtual SourceRange getSourceRange() const { 566 return SourceRange(getBase()->getLocStart(), MemberLoc); 567 } 568 virtual SourceLocation getExprLoc() const { return MemberLoc; } 569 570 static bool classof(const Stmt *T) { 571 return T->getStmtClass() == MemberExprClass; 572 } 573 static bool classof(const MemberExpr *) { return true; } 574 575 // Iterators 576 virtual child_iterator child_begin(); 577 virtual child_iterator child_end(); 578}; 579 580/// OCUVectorElementExpr - This represents access to specific elements of a 581/// vector, and may occur on the left hand side or right hand side. For example 582/// the following is legal: "V.xy = V.zw" if V is a 4 element ocu vector. 583/// 584class OCUVectorElementExpr : public Expr { 585 Expr *Base; 586 IdentifierInfo &Accessor; 587 SourceLocation AccessorLoc; 588public: 589 enum ElementType { 590 Point, // xywz 591 Color, // rgba 592 Texture // stpq 593 }; 594 OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 595 SourceLocation loc) 596 : Expr(OCUVectorElementExprClass, ty), 597 Base(base), Accessor(accessor), AccessorLoc(loc) {} 598 599 const Expr *getBase() const { return Base; } 600 Expr *getBase() { return Base; } 601 602 IdentifierInfo &getAccessor() const { return Accessor; } 603 604 /// getNumElements - Get the number of components being selected. 605 unsigned getNumElements() const; 606 607 /// getElementType - Determine whether the components of this access are 608 /// "point" "color" or "texture" elements. 609 ElementType getElementType() const; 610 611 /// containsDuplicateElements - Return true if any element access is 612 /// repeated. 613 bool containsDuplicateElements() const; 614 615 /// getEncodedElementAccess - Encode the elements accessed into a bit vector. 616 /// The encoding currently uses 2-bit bitfields, but clients should use the 617 /// accessors below to access them. 618 /// 619 unsigned getEncodedElementAccess() const; 620 621 /// getAccessedFieldNo - Given an encoded value and a result number, return 622 /// the input field number being accessed. 623 static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) { 624 return (EncodedVal >> (Idx*2)) & 3; 625 } 626 627 virtual SourceRange getSourceRange() const { 628 return SourceRange(getBase()->getLocStart(), AccessorLoc); 629 } 630 static bool classof(const Stmt *T) { 631 return T->getStmtClass() == OCUVectorElementExprClass; 632 } 633 static bool classof(const OCUVectorElementExpr *) { return true; } 634 635 // Iterators 636 virtual child_iterator child_begin(); 637 virtual child_iterator child_end(); 638}; 639 640/// CompoundLiteralExpr - [C99 6.5.2.5] 641/// 642class CompoundLiteralExpr : public Expr { 643 Expr *Init; 644public: 645 CompoundLiteralExpr(QualType ty, Expr *init) : 646 Expr(CompoundLiteralExprClass, ty), Init(init) {} 647 648 const Expr *getInitializer() const { return Init; } 649 Expr *getInitializer() { return Init; } 650 651 virtual SourceRange getSourceRange() const { 652 if (Init) 653 return Init->getSourceRange(); 654 return SourceRange(); 655 } 656 657 static bool classof(const Stmt *T) { 658 return T->getStmtClass() == CompoundLiteralExprClass; 659 } 660 static bool classof(const CompoundLiteralExpr *) { return true; } 661 662 // Iterators 663 virtual child_iterator child_begin(); 664 virtual child_iterator child_end(); 665}; 666 667/// ImplicitCastExpr - Allows us to explicitly represent implicit type 668/// conversions. For example: converting T[]->T*, void f()->void (*f)(), 669/// float->double, short->int, etc. 670/// 671class ImplicitCastExpr : public Expr { 672 Expr *Op; 673public: 674 ImplicitCastExpr(QualType ty, Expr *op) : 675 Expr(ImplicitCastExprClass, ty), Op(op) {} 676 677 Expr *getSubExpr() { return Op; } 678 const Expr *getSubExpr() const { return Op; } 679 680 virtual SourceRange getSourceRange() const { return Op->getSourceRange(); } 681 682 static bool classof(const Stmt *T) { 683 return T->getStmtClass() == ImplicitCastExprClass; 684 } 685 static bool classof(const ImplicitCastExpr *) { return true; } 686 687 // Iterators 688 virtual child_iterator child_begin(); 689 virtual child_iterator child_end(); 690}; 691 692/// CastExpr - [C99 6.5.4] Cast Operators. 693/// 694class CastExpr : public Expr { 695 Expr *Op; 696 SourceLocation Loc; // the location of the left paren 697public: 698 CastExpr(QualType ty, Expr *op, SourceLocation l) : 699 Expr(CastExprClass, ty), Op(op), Loc(l) {} 700 701 SourceLocation getLParenLoc() const { return Loc; } 702 703 Expr *getSubExpr() const { return Op; } 704 705 virtual SourceRange getSourceRange() const { 706 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 707 } 708 static bool classof(const Stmt *T) { 709 return T->getStmtClass() == CastExprClass; 710 } 711 static bool classof(const CastExpr *) { return true; } 712 713 // Iterators 714 virtual child_iterator child_begin(); 715 virtual child_iterator child_end(); 716}; 717 718class BinaryOperator : public Expr { 719public: 720 enum Opcode { 721 // Operators listed in order of precedence. 722 // Note that additions to this should also update the StmtVisitor class. 723 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 724 Add, Sub, // [C99 6.5.6] Additive operators. 725 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 726 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 727 EQ, NE, // [C99 6.5.9] Equality operators. 728 And, // [C99 6.5.10] Bitwise AND operator. 729 Xor, // [C99 6.5.11] Bitwise XOR operator. 730 Or, // [C99 6.5.12] Bitwise OR operator. 731 LAnd, // [C99 6.5.13] Logical AND operator. 732 LOr, // [C99 6.5.14] Logical OR operator. 733 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 734 DivAssign, RemAssign, 735 AddAssign, SubAssign, 736 ShlAssign, ShrAssign, 737 AndAssign, XorAssign, 738 OrAssign, 739 Comma // [C99 6.5.17] Comma operator. 740 }; 741private: 742 enum { LHS, RHS, END_EXPR }; 743 Expr* SubExprs[END_EXPR]; 744 Opcode Opc; 745 SourceLocation OpLoc; 746public: 747 748 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 749 SourceLocation opLoc) 750 : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) { 751 SubExprs[LHS] = lhs; 752 SubExprs[RHS] = rhs; 753 assert(!isCompoundAssignmentOp() && 754 "Use ArithAssignBinaryOperator for compound assignments"); 755 } 756 757 SourceLocation getOperatorLoc() const { return OpLoc; } 758 Opcode getOpcode() const { return Opc; } 759 Expr *getLHS() const { return SubExprs[LHS]; } 760 Expr *getRHS() const { return SubExprs[RHS]; } 761 virtual SourceRange getSourceRange() const { 762 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 763 } 764 765 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 766 /// corresponds to, e.g. "<<=". 767 static const char *getOpcodeStr(Opcode Op); 768 769 /// predicates to categorize the respective opcodes. 770 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 771 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 772 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 773 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 774 bool isRelationalOp() const { return Opc >= LT && Opc <= GE; } 775 bool isEqualityOp() const { return Opc == EQ || Opc == NE; } 776 bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; } 777 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 778 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 779 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 780 781 static bool classof(const Stmt *S) { 782 return S->getStmtClass() == BinaryOperatorClass || 783 S->getStmtClass() == CompoundAssignOperatorClass; 784 } 785 static bool classof(const BinaryOperator *) { return true; } 786 787 // Iterators 788 virtual child_iterator child_begin(); 789 virtual child_iterator child_end(); 790 791protected: 792 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 793 SourceLocation oploc, bool dead) 794 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 795 SubExprs[LHS] = lhs; 796 SubExprs[RHS] = rhs; 797 } 798}; 799 800/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 801/// track of the type the operation is performed in. Due to the semantics of 802/// these operators, the operands are promoted, the aritmetic performed, an 803/// implicit conversion back to the result type done, then the assignment takes 804/// place. This captures the intermediate type which the computation is done 805/// in. 806class CompoundAssignOperator : public BinaryOperator { 807 QualType ComputationType; 808public: 809 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 810 QualType ResType, QualType CompType, 811 SourceLocation OpLoc) 812 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 813 ComputationType(CompType) { 814 assert(isCompoundAssignmentOp() && 815 "Only should be used for compound assignments"); 816 } 817 818 QualType getComputationType() const { return ComputationType; } 819 820 static bool classof(const CompoundAssignOperator *) { return true; } 821 static bool classof(const Stmt *S) { 822 return S->getStmtClass() == CompoundAssignOperatorClass; 823 } 824}; 825 826/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 827/// GNU "missing LHS" extension is in use. 828/// 829class ConditionalOperator : public Expr { 830 enum { COND, LHS, RHS, END_EXPR }; 831 Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 832public: 833 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 834 : Expr(ConditionalOperatorClass, t) { 835 SubExprs[COND] = cond; 836 SubExprs[LHS] = lhs; 837 SubExprs[RHS] = rhs; 838 } 839 840 Expr *getCond() const { return SubExprs[COND]; } 841 Expr *getLHS() const { return SubExprs[LHS]; } 842 Expr *getRHS() const { return SubExprs[RHS]; } 843 844 virtual SourceRange getSourceRange() const { 845 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 846 } 847 static bool classof(const Stmt *T) { 848 return T->getStmtClass() == ConditionalOperatorClass; 849 } 850 static bool classof(const ConditionalOperator *) { return true; } 851 852 // Iterators 853 virtual child_iterator child_begin(); 854 virtual child_iterator child_end(); 855}; 856 857/// AddrLabelExpr - The GNU address of label extension, representing &&label. 858class AddrLabelExpr : public Expr { 859 SourceLocation AmpAmpLoc, LabelLoc; 860 LabelStmt *Label; 861public: 862 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 863 QualType t) 864 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 865 866 virtual SourceRange getSourceRange() const { 867 return SourceRange(AmpAmpLoc, LabelLoc); 868 } 869 870 LabelStmt *getLabel() const { return Label; } 871 872 static bool classof(const Stmt *T) { 873 return T->getStmtClass() == AddrLabelExprClass; 874 } 875 static bool classof(const AddrLabelExpr *) { return true; } 876 877 // Iterators 878 virtual child_iterator child_begin(); 879 virtual child_iterator child_end(); 880}; 881 882/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 883/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 884/// takes the value of the last subexpression. 885class StmtExpr : public Expr { 886 CompoundStmt *SubStmt; 887 SourceLocation LParenLoc, RParenLoc; 888public: 889 StmtExpr(CompoundStmt *substmt, QualType T, 890 SourceLocation lp, SourceLocation rp) : 891 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 892 893 CompoundStmt *getSubStmt() { return SubStmt; } 894 const CompoundStmt *getSubStmt() const { return SubStmt; } 895 896 virtual SourceRange getSourceRange() const { 897 return SourceRange(LParenLoc, RParenLoc); 898 } 899 900 static bool classof(const Stmt *T) { 901 return T->getStmtClass() == StmtExprClass; 902 } 903 static bool classof(const StmtExpr *) { return true; } 904 905 // Iterators 906 virtual child_iterator child_begin(); 907 virtual child_iterator child_end(); 908}; 909 910/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 911/// This AST node represents a function that returns 1 if two *types* (not 912/// expressions) are compatible. The result of this built-in function can be 913/// used in integer constant expressions. 914class TypesCompatibleExpr : public Expr { 915 QualType Type1; 916 QualType Type2; 917 SourceLocation BuiltinLoc, RParenLoc; 918public: 919 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 920 QualType t1, QualType t2, SourceLocation RP) : 921 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 922 BuiltinLoc(BLoc), RParenLoc(RP) {} 923 924 QualType getArgType1() const { return Type1; } 925 QualType getArgType2() const { return Type2; } 926 927 virtual SourceRange getSourceRange() const { 928 return SourceRange(BuiltinLoc, RParenLoc); 929 } 930 static bool classof(const Stmt *T) { 931 return T->getStmtClass() == TypesCompatibleExprClass; 932 } 933 static bool classof(const TypesCompatibleExpr *) { return true; } 934 935 // Iterators 936 virtual child_iterator child_begin(); 937 virtual child_iterator child_end(); 938}; 939 940/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 941/// This AST node is similar to the conditional operator (?:) in C, with 942/// the following exceptions: 943/// - the test expression much be a constant expression. 944/// - the expression returned has it's type unaltered by promotion rules. 945/// - does not evaluate the expression that was not chosen. 946class ChooseExpr : public Expr { 947 enum { COND, LHS, RHS, END_EXPR }; 948 Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 949 SourceLocation BuiltinLoc, RParenLoc; 950public: 951 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 952 SourceLocation RP) 953 : Expr(ChooseExprClass, t), 954 BuiltinLoc(BLoc), RParenLoc(RP) { 955 SubExprs[COND] = cond; 956 SubExprs[LHS] = lhs; 957 SubExprs[RHS] = rhs; 958 } 959 960 Expr *getCond() const { return SubExprs[COND]; } 961 Expr *getLHS() const { return SubExprs[LHS]; } 962 Expr *getRHS() const { return SubExprs[RHS]; } 963 964 virtual SourceRange getSourceRange() const { 965 return SourceRange(BuiltinLoc, RParenLoc); 966 } 967 static bool classof(const Stmt *T) { 968 return T->getStmtClass() == ChooseExprClass; 969 } 970 static bool classof(const ChooseExpr *) { return true; } 971 972 // Iterators 973 virtual child_iterator child_begin(); 974 virtual child_iterator child_end(); 975}; 976 977/// VAArgExpr, used for the builtin function __builtin_va_start. 978class VAArgExpr : public Expr { 979 Expr *Val; 980 SourceLocation BuiltinLoc, RParenLoc; 981public: 982 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 983 : Expr(VAArgExprClass, t), 984 Val(e), 985 BuiltinLoc(BLoc), 986 RParenLoc(RPLoc) { } 987 988 const Expr *getSubExpr() const { return Val; } 989 Expr *getSubExpr() { return Val; } 990 virtual SourceRange getSourceRange() const { 991 return SourceRange(BuiltinLoc, RParenLoc); 992 } 993 static bool classof(const Stmt *T) { 994 return T->getStmtClass() == VAArgExprClass; 995 } 996 static bool classof(const VAArgExpr *) { return true; } 997 998 // Iterators 999 virtual child_iterator child_begin(); 1000 virtual child_iterator child_end(); 1001}; 1002 1003/// InitListExpr, used for struct and array initializers. 1004class InitListExpr : public Expr { 1005 Expr **InitExprs; 1006 unsigned NumInits; 1007 SourceLocation LBraceLoc, RBraceLoc; 1008public: 1009 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1010 SourceLocation rbraceloc); 1011 ~InitListExpr() { 1012 delete [] InitExprs; 1013 } 1014 1015 unsigned getNumInits() const { return NumInits; } 1016 1017 const Expr* getInit(unsigned Init) const { 1018 assert(Init < NumInits && "Initializer access out of range!"); 1019 return InitExprs[Init]; 1020 } 1021 1022 Expr* getInit(unsigned Init) { 1023 assert(Init < NumInits && "Initializer access out of range!"); 1024 return InitExprs[Init]; 1025 } 1026 1027 void setInit(unsigned Init, Expr *expr) { 1028 assert(Init < NumInits && "Initializer access out of range!"); 1029 InitExprs[Init] = expr; 1030 } 1031 1032 virtual SourceRange getSourceRange() const { 1033 return SourceRange(LBraceLoc, RBraceLoc); 1034 } 1035 static bool classof(const Stmt *T) { 1036 return T->getStmtClass() == InitListExprClass; 1037 } 1038 static bool classof(const InitListExpr *) { return true; } 1039 1040 // Iterators 1041 virtual child_iterator child_begin(); 1042 virtual child_iterator child_end(); 1043}; 1044 1045/// ObjCStringLiteral, used for Objective-C string literals 1046/// i.e. @"foo". 1047class ObjCStringLiteral : public Expr { 1048 StringLiteral *String; 1049public: 1050 ObjCStringLiteral(StringLiteral *SL, QualType T) 1051 : Expr(ObjCStringLiteralClass, T), String(SL) {} 1052 1053 StringLiteral* getString() { return String; } 1054 1055 const StringLiteral* getString() const { return String; } 1056 1057 virtual SourceRange getSourceRange() const { 1058 return String->getSourceRange(); 1059 } 1060 1061 static bool classof(const Stmt *T) { 1062 return T->getStmtClass() == ObjCStringLiteralClass; 1063 } 1064 static bool classof(const ObjCStringLiteral *) { return true; } 1065 1066 // Iterators 1067 virtual child_iterator child_begin(); 1068 virtual child_iterator child_end(); 1069}; 1070 1071/// ObjCEncodeExpr, used for @encode in Objective-C. 1072class ObjCEncodeExpr : public Expr { 1073 QualType EncType; 1074 SourceLocation AtLoc, RParenLoc; 1075public: 1076 ObjCEncodeExpr(QualType T, QualType ET, 1077 SourceLocation at, SourceLocation rp) 1078 : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {} 1079 1080 SourceLocation getAtLoc() const { return AtLoc; } 1081 SourceLocation getRParenLoc() const { return RParenLoc; } 1082 1083 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1084 1085 QualType getEncodedType() const { return EncType; } 1086 1087 static bool classof(const Stmt *T) { 1088 return T->getStmtClass() == ObjCEncodeExprClass; 1089 } 1090 static bool classof(const ObjCEncodeExpr *) { return true; } 1091 1092 // Iterators 1093 virtual child_iterator child_begin(); 1094 virtual child_iterator child_end(); 1095}; 1096 1097/// ObjCSelectorExpr used for @selector in Objective-C. 1098class ObjCSelectorExpr : public Expr { 1099 1100 Selector SelName; 1101 1102 SourceLocation AtLoc, RParenLoc; 1103public: 1104 ObjCSelectorExpr(QualType T, Selector selInfo, 1105 SourceLocation at, SourceLocation rp) 1106 : Expr(ObjCSelectorExprClass, T), SelName(selInfo), 1107 AtLoc(at), RParenLoc(rp) {} 1108 1109 const Selector &getSelector() const { return SelName; } 1110 Selector &getSelector() { return SelName; } 1111 1112 SourceLocation getAtLoc() const { return AtLoc; } 1113 SourceLocation getRParenLoc() const { return RParenLoc; } 1114 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1115 1116 /// getNumArgs - Return the number of actual arguments to this call. 1117 unsigned getNumArgs() const { return SelName.getNumArgs(); } 1118 1119 static bool classof(const Stmt *T) { 1120 return T->getStmtClass() == ObjCSelectorExprClass; 1121 } 1122 static bool classof(const ObjCSelectorExpr *) { return true; } 1123 1124 // Iterators 1125 virtual child_iterator child_begin(); 1126 virtual child_iterator child_end(); 1127 1128}; 1129 1130/// ObjCProtocolExpr used for protocol in Objective-C. 1131class ObjCProtocolExpr : public Expr { 1132 1133 ObjcProtocolDecl *Protocol; 1134 1135 SourceLocation AtLoc, RParenLoc; 1136 public: 1137 ObjCProtocolExpr(QualType T, ObjcProtocolDecl *protocol, 1138 SourceLocation at, SourceLocation rp) 1139 : Expr(ObjCProtocolExprClass, T), Protocol(protocol), 1140 AtLoc(at), RParenLoc(rp) {} 1141 1142 ObjcProtocolDecl *getProtocol() const { return Protocol; } 1143 1144 SourceLocation getAtLoc() const { return AtLoc; } 1145 SourceLocation getRParenLoc() const { return RParenLoc; } 1146 SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); } 1147 1148 static bool classof(const Stmt *T) { 1149 return T->getStmtClass() == ObjCProtocolExprClass; 1150 } 1151 static bool classof(const ObjCProtocolExpr *) { return true; } 1152 1153 // Iterators 1154 virtual child_iterator child_begin(); 1155 virtual child_iterator child_end(); 1156 1157}; 1158 1159class ObjCMessageExpr : public Expr { 1160 enum { RECEIVER=0, ARGS_START=1 }; 1161 1162 Expr **SubExprs; 1163 1164 // A unigue name for this message. 1165 Selector SelName; 1166 1167 IdentifierInfo *ClassName; // optional - 0 for instance messages. 1168 1169 SourceLocation LBracloc, RBracloc; 1170public: 1171 // constructor for class messages. 1172 // FIXME: clsName should be typed to ObjCInterfaceType 1173 ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 1174 QualType retType, SourceLocation LBrac, SourceLocation RBrac, 1175 Expr **ArgExprs); 1176 // constructor for instance messages. 1177 ObjCMessageExpr(Expr *receiver, Selector selInfo, 1178 QualType retType, SourceLocation LBrac, SourceLocation RBrac, 1179 Expr **ArgExprs); 1180 ~ObjCMessageExpr() { 1181 delete [] SubExprs; 1182 } 1183 1184 const Expr *getReceiver() const { return SubExprs[RECEIVER]; } 1185 Expr *getReceiver() { return SubExprs[RECEIVER]; } 1186 1187 const Selector &getSelector() const { return SelName; } 1188 Selector &getSelector() { return SelName; } 1189 1190 const IdentifierInfo *getClassName() const { return ClassName; } 1191 IdentifierInfo *getClassName() { return ClassName; } 1192 1193 /// getNumArgs - Return the number of actual arguments to this call. 1194 unsigned getNumArgs() const { return SelName.getNumArgs(); } 1195 1196/// getArg - Return the specified argument. 1197 Expr *getArg(unsigned Arg) { 1198 assert(Arg < SelName.getNumArgs() && "Arg access out of range!"); 1199 return SubExprs[Arg+ARGS_START]; 1200 } 1201 const Expr *getArg(unsigned Arg) const { 1202 assert(Arg < SelName.getNumArgs() && "Arg access out of range!"); 1203 return SubExprs[Arg+ARGS_START]; 1204 } 1205 1206 SourceRange getSourceRange() const { return SourceRange(LBracloc, RBracloc); } 1207 1208 static bool classof(const Stmt *T) { 1209 return T->getStmtClass() == ObjCMessageExprClass; 1210 } 1211 static bool classof(const ObjCMessageExpr *) { return true; } 1212 1213 // Iterators 1214 virtual child_iterator child_begin(); 1215 virtual child_iterator child_end(); 1216}; 1217 1218} // end namespace clang 1219 1220#endif 1221