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