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