Expr.h revision c7229c338c21ef26b01ef3ecf9eec4fd373fa9ec
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 /// getDecl - a recursive routine that derives the base decl for an 387 /// expression. For example, it will return the declaration for "s" from 388 /// the following complex expression "s.zz[2].bb.vv". 389 static bool isAddressable(Expr *e); 390 391 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 392 /// corresponds to, e.g. "sizeof" or "[pre]++" 393 static const char *getOpcodeStr(Opcode Op); 394 395 virtual SourceRange getSourceRange() const { 396 if (isPostfix()) 397 return SourceRange(Val->getLocStart(), Loc); 398 else 399 return SourceRange(Loc, Val->getLocEnd()); 400 } 401 virtual SourceLocation getExprLoc() const { return Loc; } 402 403 static bool classof(const Stmt *T) { 404 return T->getStmtClass() == UnaryOperatorClass; 405 } 406 static bool classof(const UnaryOperator *) { return true; } 407 408 // Iterators 409 virtual child_iterator child_begin(); 410 virtual child_iterator child_end(); 411}; 412 413/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of 414/// *types*. sizeof(expr) is handled by UnaryOperator. 415class SizeOfAlignOfTypeExpr : public Expr { 416 bool isSizeof; // true if sizeof, false if alignof. 417 QualType Ty; 418 SourceLocation OpLoc, RParenLoc; 419public: 420 SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType, 421 SourceLocation op, SourceLocation rp) : 422 Expr(SizeOfAlignOfTypeExprClass, resultType), 423 isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {} 424 425 bool isSizeOf() const { return isSizeof; } 426 QualType getArgumentType() const { return Ty; } 427 428 SourceLocation getOperatorLoc() const { return OpLoc; } 429 SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); } 430 431 static bool classof(const Stmt *T) { 432 return T->getStmtClass() == SizeOfAlignOfTypeExprClass; 433 } 434 static bool classof(const SizeOfAlignOfTypeExpr *) { return true; } 435 436 // Iterators 437 virtual child_iterator child_begin(); 438 virtual child_iterator child_end(); 439}; 440 441//===----------------------------------------------------------------------===// 442// Postfix Operators. 443//===----------------------------------------------------------------------===// 444 445/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 446class ArraySubscriptExpr : public Expr { 447 enum { LHS, RHS, END_EXPR=2 }; 448 Expr* SubExprs[END_EXPR]; 449 SourceLocation RBracketLoc; 450public: 451 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 452 SourceLocation rbracketloc) 453 : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) { 454 SubExprs[LHS] = lhs; 455 SubExprs[RHS] = rhs; 456 } 457 458 /// An array access can be written A[4] or 4[A] (both are equivalent). 459 /// - getBase() and getIdx() always present the normalized view: A[4]. 460 /// In this case getBase() returns "A" and getIdx() returns "4". 461 /// - getLHS() and getRHS() present the syntactic view. e.g. for 462 /// 4[A] getLHS() returns "4". 463 464 Expr *getLHS() { return SubExprs[LHS]; } 465 const Expr *getLHS() const { return SubExprs[LHS]; } 466 467 Expr *getRHS() { return SubExprs[RHS]; } 468 const Expr *getRHS() const { return SubExprs[RHS]; } 469 470 Expr *getBase() { 471 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 472 } 473 474 const Expr *getBase() const { 475 return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS(); 476 } 477 478 Expr *getIdx() { 479 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 480 } 481 482 const Expr *getIdx() const { 483 return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS(); 484 } 485 486 487 SourceRange getSourceRange() const { 488 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 489 } 490 virtual SourceLocation getExprLoc() const { return RBracketLoc; } 491 492 static bool classof(const Stmt *T) { 493 return T->getStmtClass() == ArraySubscriptExprClass; 494 } 495 static bool classof(const ArraySubscriptExpr *) { return true; } 496 497 // Iterators 498 virtual child_iterator child_begin(); 499 virtual child_iterator child_end(); 500}; 501 502 503/// CallExpr - [C99 6.5.2.2] Function Calls. 504/// 505class CallExpr : public Expr { 506 enum { FN=0, ARGS_START=1 }; 507 Expr **SubExprs; 508 unsigned NumArgs; 509 SourceLocation RParenLoc; 510public: 511 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 512 SourceLocation rparenloc); 513 ~CallExpr() { 514 delete [] SubExprs; 515 } 516 517 const Expr *getCallee() const { return SubExprs[FN]; } 518 Expr *getCallee() { return SubExprs[FN]; } 519 520 /// getNumArgs - Return the number of actual arguments to this call. 521 /// 522 unsigned getNumArgs() const { return NumArgs; } 523 524 /// getArg - Return the specified argument. 525 Expr *getArg(unsigned Arg) { 526 assert(Arg < NumArgs && "Arg access out of range!"); 527 return SubExprs[Arg+ARGS_START]; 528 } 529 const Expr *getArg(unsigned Arg) const { 530 assert(Arg < NumArgs && "Arg access out of range!"); 531 return SubExprs[Arg+ARGS_START]; 532 } 533 534 /// getNumCommas - Return the number of commas that must have been present in 535 /// this function call. 536 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 537 538 bool isBuiltinClassifyType(llvm::APSInt &Result) const; 539 540 SourceRange getSourceRange() const { 541 return SourceRange(getCallee()->getLocStart(), RParenLoc); 542 } 543 544 static bool classof(const Stmt *T) { 545 return T->getStmtClass() == CallExprClass; 546 } 547 static bool classof(const CallExpr *) { return true; } 548 549 // Iterators 550 virtual child_iterator child_begin(); 551 virtual child_iterator child_end(); 552}; 553 554/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 555/// 556class MemberExpr : public Expr { 557 Expr *Base; 558 FieldDecl *MemberDecl; 559 SourceLocation MemberLoc; 560 bool IsArrow; // True if this is "X->F", false if this is "X.F". 561public: 562 MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l) 563 : Expr(MemberExprClass, memberdecl->getType()), 564 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 565 566 Expr *getBase() const { return Base; } 567 FieldDecl *getMemberDecl() const { return MemberDecl; } 568 bool isArrow() const { return IsArrow; } 569 570 virtual SourceRange getSourceRange() const { 571 return SourceRange(getBase()->getLocStart(), MemberLoc); 572 } 573 virtual SourceLocation getExprLoc() const { return MemberLoc; } 574 575 static bool classof(const Stmt *T) { 576 return T->getStmtClass() == MemberExprClass; 577 } 578 static bool classof(const MemberExpr *) { return true; } 579 580 // Iterators 581 virtual child_iterator child_begin(); 582 virtual child_iterator child_end(); 583}; 584 585/// OCUVectorElementExpr - This represents access to specific elements of a 586/// vector, and may occur on the left hand side or right hand side. For example 587/// the following is legal: "V.xy = V.zw" if V is a 4 element ocu vector. 588/// 589class OCUVectorElementExpr : public Expr { 590 Expr *Base; 591 IdentifierInfo &Accessor; 592 SourceLocation AccessorLoc; 593public: 594 enum ElementType { 595 Point, // xywz 596 Color, // rgba 597 Texture // stpq 598 }; 599 OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 600 SourceLocation loc) 601 : Expr(OCUVectorElementExprClass, ty), 602 Base(base), Accessor(accessor), AccessorLoc(loc) {} 603 604 const Expr *getBase() const { return Base; } 605 Expr *getBase() { return Base; } 606 607 IdentifierInfo &getAccessor() const { return Accessor; } 608 609 /// getNumElements - Get the number of components being selected. 610 unsigned getNumElements() const; 611 612 /// getElementType - Determine whether the components of this access are 613 /// "point" "color" or "texture" elements. 614 ElementType getElementType() const; 615 616 /// containsDuplicateElements - Return true if any element access is 617 /// repeated. 618 bool containsDuplicateElements() const; 619 620 /// getEncodedElementAccess - Encode the elements accessed into a bit vector. 621 /// The encoding currently uses 2-bit bitfields, but clients should use the 622 /// accessors below to access them. 623 /// 624 unsigned getEncodedElementAccess() const; 625 626 /// getAccessedFieldNo - Given an encoded value and a result number, return 627 /// the input field number being accessed. 628 static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) { 629 return (EncodedVal >> (Idx*2)) & 3; 630 } 631 632 virtual SourceRange getSourceRange() const { 633 return SourceRange(getBase()->getLocStart(), AccessorLoc); 634 } 635 static bool classof(const Stmt *T) { 636 return T->getStmtClass() == OCUVectorElementExprClass; 637 } 638 static bool classof(const OCUVectorElementExpr *) { return true; } 639 640 // Iterators 641 virtual child_iterator child_begin(); 642 virtual child_iterator child_end(); 643}; 644 645/// CompoundLiteralExpr - [C99 6.5.2.5] 646/// 647class CompoundLiteralExpr : public Expr { 648 Expr *Init; 649public: 650 CompoundLiteralExpr(QualType ty, Expr *init) : 651 Expr(CompoundLiteralExprClass, ty), Init(init) {} 652 653 const Expr *getInitializer() const { return Init; } 654 Expr *getInitializer() { return Init; } 655 656 virtual SourceRange getSourceRange() const { 657 if (Init) 658 return Init->getSourceRange(); 659 return SourceRange(); 660 } 661 662 static bool classof(const Stmt *T) { 663 return T->getStmtClass() == CompoundLiteralExprClass; 664 } 665 static bool classof(const CompoundLiteralExpr *) { return true; } 666 667 // Iterators 668 virtual child_iterator child_begin(); 669 virtual child_iterator child_end(); 670}; 671 672/// ImplicitCastExpr - Allows us to explicitly represent implicit type 673/// conversions. For example: converting T[]->T*, void f()->void (*f)(), 674/// float->double, short->int, etc. 675/// 676class ImplicitCastExpr : public Expr { 677 Expr *Op; 678public: 679 ImplicitCastExpr(QualType ty, Expr *op) : 680 Expr(ImplicitCastExprClass, ty), Op(op) {} 681 682 Expr *getSubExpr() { return Op; } 683 const Expr *getSubExpr() const { return Op; } 684 685 virtual SourceRange getSourceRange() const { return Op->getSourceRange(); } 686 687 static bool classof(const Stmt *T) { 688 return T->getStmtClass() == ImplicitCastExprClass; 689 } 690 static bool classof(const ImplicitCastExpr *) { return true; } 691 692 // Iterators 693 virtual child_iterator child_begin(); 694 virtual child_iterator child_end(); 695}; 696 697/// CastExpr - [C99 6.5.4] Cast Operators. 698/// 699class CastExpr : public Expr { 700 Expr *Op; 701 SourceLocation Loc; // the location of the left paren 702public: 703 CastExpr(QualType ty, Expr *op, SourceLocation l) : 704 Expr(CastExprClass, ty), Op(op), Loc(l) {} 705 706 SourceLocation getLParenLoc() const { return Loc; } 707 708 Expr *getSubExpr() const { return Op; } 709 710 virtual SourceRange getSourceRange() const { 711 return SourceRange(Loc, getSubExpr()->getSourceRange().End()); 712 } 713 static bool classof(const Stmt *T) { 714 return T->getStmtClass() == CastExprClass; 715 } 716 static bool classof(const CastExpr *) { return true; } 717 718 // Iterators 719 virtual child_iterator child_begin(); 720 virtual child_iterator child_end(); 721}; 722 723class BinaryOperator : public Expr { 724public: 725 enum Opcode { 726 // Operators listed in order of precedence. 727 // Note that additions to this should also update the StmtVisitor class. 728 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 729 Add, Sub, // [C99 6.5.6] Additive operators. 730 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 731 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 732 EQ, NE, // [C99 6.5.9] Equality operators. 733 And, // [C99 6.5.10] Bitwise AND operator. 734 Xor, // [C99 6.5.11] Bitwise XOR operator. 735 Or, // [C99 6.5.12] Bitwise OR operator. 736 LAnd, // [C99 6.5.13] Logical AND operator. 737 LOr, // [C99 6.5.14] Logical OR operator. 738 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 739 DivAssign, RemAssign, 740 AddAssign, SubAssign, 741 ShlAssign, ShrAssign, 742 AndAssign, XorAssign, 743 OrAssign, 744 Comma // [C99 6.5.17] Comma operator. 745 }; 746private: 747 enum { LHS, RHS, END_EXPR }; 748 Expr* SubExprs[END_EXPR]; 749 Opcode Opc; 750 SourceLocation OpLoc; 751public: 752 753 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 754 SourceLocation opLoc) 755 : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) { 756 SubExprs[LHS] = lhs; 757 SubExprs[RHS] = rhs; 758 assert(!isCompoundAssignmentOp() && 759 "Use ArithAssignBinaryOperator for compound assignments"); 760 } 761 762 SourceLocation getOperatorLoc() const { return OpLoc; } 763 Opcode getOpcode() const { return Opc; } 764 Expr *getLHS() const { return SubExprs[LHS]; } 765 Expr *getRHS() const { return SubExprs[RHS]; } 766 virtual SourceRange getSourceRange() const { 767 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 768 } 769 770 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 771 /// corresponds to, e.g. "<<=". 772 static const char *getOpcodeStr(Opcode Op); 773 774 /// predicates to categorize the respective opcodes. 775 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 776 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 777 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 778 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 779 bool isRelationalOp() const { return Opc >= LT && Opc <= GE; } 780 bool isEqualityOp() const { return Opc == EQ || Opc == NE; } 781 bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; } 782 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 783 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 784 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 785 786 static bool classof(const Stmt *S) { 787 return S->getStmtClass() == BinaryOperatorClass || 788 S->getStmtClass() == CompoundAssignOperatorClass; 789 } 790 static bool classof(const BinaryOperator *) { return true; } 791 792 // Iterators 793 virtual child_iterator child_begin(); 794 virtual child_iterator child_end(); 795 796protected: 797 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 798 SourceLocation oploc, bool dead) 799 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 800 SubExprs[LHS] = lhs; 801 SubExprs[RHS] = rhs; 802 } 803}; 804 805/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 806/// track of the type the operation is performed in. Due to the semantics of 807/// these operators, the operands are promoted, the aritmetic performed, an 808/// implicit conversion back to the result type done, then the assignment takes 809/// place. This captures the intermediate type which the computation is done 810/// in. 811class CompoundAssignOperator : public BinaryOperator { 812 QualType ComputationType; 813public: 814 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 815 QualType ResType, QualType CompType, 816 SourceLocation OpLoc) 817 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 818 ComputationType(CompType) { 819 assert(isCompoundAssignmentOp() && 820 "Only should be used for compound assignments"); 821 } 822 823 QualType getComputationType() const { return ComputationType; } 824 825 static bool classof(const CompoundAssignOperator *) { return true; } 826 static bool classof(const Stmt *S) { 827 return S->getStmtClass() == CompoundAssignOperatorClass; 828 } 829}; 830 831/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 832/// GNU "missing LHS" extension is in use. 833/// 834class ConditionalOperator : public Expr { 835 enum { COND, LHS, RHS, END_EXPR }; 836 Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 837public: 838 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 839 : Expr(ConditionalOperatorClass, t) { 840 SubExprs[COND] = cond; 841 SubExprs[LHS] = lhs; 842 SubExprs[RHS] = rhs; 843 } 844 845 Expr *getCond() const { return SubExprs[COND]; } 846 Expr *getLHS() const { return SubExprs[LHS]; } 847 Expr *getRHS() const { return SubExprs[RHS]; } 848 849 virtual SourceRange getSourceRange() const { 850 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 851 } 852 static bool classof(const Stmt *T) { 853 return T->getStmtClass() == ConditionalOperatorClass; 854 } 855 static bool classof(const ConditionalOperator *) { return true; } 856 857 // Iterators 858 virtual child_iterator child_begin(); 859 virtual child_iterator child_end(); 860}; 861 862/// AddrLabelExpr - The GNU address of label extension, representing &&label. 863class AddrLabelExpr : public Expr { 864 SourceLocation AmpAmpLoc, LabelLoc; 865 LabelStmt *Label; 866public: 867 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 868 QualType t) 869 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 870 871 virtual SourceRange getSourceRange() const { 872 return SourceRange(AmpAmpLoc, LabelLoc); 873 } 874 875 LabelStmt *getLabel() const { return Label; } 876 877 static bool classof(const Stmt *T) { 878 return T->getStmtClass() == AddrLabelExprClass; 879 } 880 static bool classof(const AddrLabelExpr *) { return true; } 881 882 // Iterators 883 virtual child_iterator child_begin(); 884 virtual child_iterator child_end(); 885}; 886 887/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 888/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 889/// takes the value of the last subexpression. 890class StmtExpr : public Expr { 891 CompoundStmt *SubStmt; 892 SourceLocation LParenLoc, RParenLoc; 893public: 894 StmtExpr(CompoundStmt *substmt, QualType T, 895 SourceLocation lp, SourceLocation rp) : 896 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 897 898 CompoundStmt *getSubStmt() { return SubStmt; } 899 const CompoundStmt *getSubStmt() const { return SubStmt; } 900 901 virtual SourceRange getSourceRange() const { 902 return SourceRange(LParenLoc, RParenLoc); 903 } 904 905 static bool classof(const Stmt *T) { 906 return T->getStmtClass() == StmtExprClass; 907 } 908 static bool classof(const StmtExpr *) { return true; } 909 910 // Iterators 911 virtual child_iterator child_begin(); 912 virtual child_iterator child_end(); 913}; 914 915/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 916/// This AST node represents a function that returns 1 if two *types* (not 917/// expressions) are compatible. The result of this built-in function can be 918/// used in integer constant expressions. 919class TypesCompatibleExpr : public Expr { 920 QualType Type1; 921 QualType Type2; 922 SourceLocation BuiltinLoc, RParenLoc; 923public: 924 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 925 QualType t1, QualType t2, SourceLocation RP) : 926 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 927 BuiltinLoc(BLoc), RParenLoc(RP) {} 928 929 QualType getArgType1() const { return Type1; } 930 QualType getArgType2() const { return Type2; } 931 932 int typesAreCompatible() const {return Type::typesAreCompatible(Type1,Type2);} 933 934 virtual SourceRange getSourceRange() const { 935 return SourceRange(BuiltinLoc, RParenLoc); 936 } 937 static bool classof(const Stmt *T) { 938 return T->getStmtClass() == TypesCompatibleExprClass; 939 } 940 static bool classof(const TypesCompatibleExpr *) { return true; } 941 942 // Iterators 943 virtual child_iterator child_begin(); 944 virtual child_iterator child_end(); 945}; 946 947/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 948/// This AST node is similar to the conditional operator (?:) in C, with 949/// the following exceptions: 950/// - the test expression much be a constant expression. 951/// - the expression returned has it's type unaltered by promotion rules. 952/// - does not evaluate the expression that was not chosen. 953class ChooseExpr : public Expr { 954 enum { COND, LHS, RHS, END_EXPR }; 955 Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 956 SourceLocation BuiltinLoc, RParenLoc; 957public: 958 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 959 SourceLocation RP) 960 : Expr(ChooseExprClass, t), 961 BuiltinLoc(BLoc), RParenLoc(RP) { 962 SubExprs[COND] = cond; 963 SubExprs[LHS] = lhs; 964 SubExprs[RHS] = rhs; 965 } 966 967 Expr *getCond() const { return SubExprs[COND]; } 968 Expr *getLHS() const { return SubExprs[LHS]; } 969 Expr *getRHS() const { return SubExprs[RHS]; } 970 971 virtual SourceRange getSourceRange() const { 972 return SourceRange(BuiltinLoc, RParenLoc); 973 } 974 static bool classof(const Stmt *T) { 975 return T->getStmtClass() == ChooseExprClass; 976 } 977 static bool classof(const ChooseExpr *) { return true; } 978 979 // Iterators 980 virtual child_iterator child_begin(); 981 virtual child_iterator child_end(); 982}; 983 984/// InitListExpr, used for struct and array initializers. 985class InitListExpr : public Expr { 986 Expr **InitExprs; 987 unsigned NumInits; 988 SourceLocation LBraceLoc, RBraceLoc; 989public: 990 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 991 SourceLocation rbraceloc); 992 ~InitListExpr() { 993 delete [] InitExprs; 994 } 995 996 unsigned getNumInits() const { return NumInits; } 997 998 const Expr* getInit(unsigned Init) const { 999 assert(Init < NumInits && "Initializer access out of range!"); 1000 return InitExprs[Init]; 1001 } 1002 1003 Expr* getInit(unsigned Init) { 1004 assert(Init < NumInits && "Initializer access out of range!"); 1005 return InitExprs[Init]; 1006 } 1007 1008 void setInit(unsigned Init, Expr *expr) { 1009 assert(Init < NumInits && "Initializer access out of range!"); 1010 InitExprs[Init] = expr; 1011 } 1012 1013 virtual SourceRange getSourceRange() const { 1014 return SourceRange(LBraceLoc, RBraceLoc); 1015 } 1016 static bool classof(const Stmt *T) { 1017 return T->getStmtClass() == InitListExprClass; 1018 } 1019 static bool classof(const InitListExpr *) { return true; } 1020 1021 // Iterators 1022 virtual child_iterator child_begin(); 1023 virtual child_iterator child_end(); 1024}; 1025 1026/// ObjCStringLiteral, used for Objective-C string literals 1027/// i.e. @"foo". 1028class ObjCStringLiteral : public Expr { 1029 StringLiteral *String; 1030public: 1031 ObjCStringLiteral(StringLiteral *SL, QualType T) 1032 : Expr(ObjCStringLiteralClass, T), String(SL) {} 1033 1034 StringLiteral* getString() { return String; } 1035 1036 const StringLiteral* getString() const { return String; } 1037 1038 virtual SourceRange getSourceRange() const { 1039 return String->getSourceRange(); 1040 } 1041 1042 static bool classof(const Stmt *T) { 1043 return T->getStmtClass() == ObjCStringLiteralClass; 1044 } 1045 static bool classof(const ObjCStringLiteral *) { return true; } 1046 1047 // Iterators 1048 virtual child_iterator child_begin(); 1049 virtual child_iterator child_end(); 1050}; 1051 1052/// ObjCEncodeExpr, used for @encode in Objective-C. 1053class ObjCEncodeExpr : public Expr { 1054 QualType EncType; 1055 SourceLocation EncLoc, RParenLoc; 1056public: 1057 ObjCEncodeExpr(QualType T, QualType ET, 1058 SourceLocation enc, SourceLocation rp) 1059 : Expr(ObjCEncodeExprClass, T), EncType(ET), EncLoc(enc), RParenLoc(rp) {} 1060 1061 SourceRange getSourceRange() const { return SourceRange(EncLoc, RParenLoc); } 1062 1063 QualType getEncodedType() const { return EncType; } 1064 1065 static bool classof(const Stmt *T) { 1066 return T->getStmtClass() == ObjCEncodeExprClass; 1067 } 1068 static bool classof(const ObjCEncodeExpr *) { return true; } 1069 1070 // Iterators 1071 virtual child_iterator child_begin(); 1072 virtual child_iterator child_end(); 1073}; 1074 1075class ObjCMessageExpr : public Expr { 1076 enum { RECEIVER=0, ARGS_START=1 }; 1077 1078 Expr **SubExprs; 1079 1080 // A unigue name for this message. 1081 Selector SelName; 1082 1083 IdentifierInfo *ClassName; // optional - 0 for instance messages. 1084 1085 SourceLocation LBracloc, RBracloc; 1086public: 1087 // constructor for class messages. 1088 // FIXME: clsName should be typed to ObjCInterfaceType 1089 ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 1090 QualType retType, SourceLocation LBrac, SourceLocation RBrac, 1091 Expr **ArgExprs); 1092 // constructor for instance messages. 1093 ObjCMessageExpr(Expr *receiver, Selector selInfo, 1094 QualType retType, SourceLocation LBrac, SourceLocation RBrac, 1095 Expr **ArgExprs); 1096 ~ObjCMessageExpr() { 1097 delete [] SubExprs; 1098 } 1099 1100 const Expr *getReceiver() const { return SubExprs[RECEIVER]; } 1101 Expr *getReceiver() { return SubExprs[RECEIVER]; } 1102 1103 const Selector &getSelector() const { return SelName; } 1104 Selector &getSelector() { return SelName; } 1105 1106 const IdentifierInfo *getClassName() const { return ClassName; } 1107 IdentifierInfo *getClassName() { return ClassName; } 1108 1109 /// getNumArgs - Return the number of actual arguments to this call. 1110 unsigned getNumArgs() const { return SelName.getNumArgs(); } 1111 1112/// getArg - Return the specified argument. 1113 Expr *getArg(unsigned Arg) { 1114 assert(Arg < SelName.getNumArgs() && "Arg access out of range!"); 1115 return SubExprs[Arg+ARGS_START]; 1116 } 1117 const Expr *getArg(unsigned Arg) const { 1118 assert(Arg < SelName.getNumArgs() && "Arg access out of range!"); 1119 return SubExprs[Arg+ARGS_START]; 1120 } 1121 1122 SourceRange getSourceRange() const { return SourceRange(LBracloc, RBracloc); } 1123 1124 static bool classof(const Stmt *T) { 1125 return T->getStmtClass() == ObjCMessageExprClass; 1126 } 1127 static bool classof(const ObjCMessageExpr *) { return true; } 1128 1129 // Iterators 1130 virtual child_iterator child_begin(); 1131 virtual child_iterator child_end(); 1132}; 1133 1134} // end namespace clang 1135 1136#endif 1137