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