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