Expr.h revision caae7b099a0324b7c15dc89a9b70969d5d7ce996
1//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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/APValue.h" 18#include "clang/AST/Stmt.h" 19#include "clang/AST/Type.h" 20#include "llvm/ADT/APSInt.h" 21#include "llvm/ADT/APFloat.h" 22#include "llvm/ADT/SmallVector.h" 23#include <vector> 24 25namespace clang { 26 class ASTContext; 27 class APValue; 28 class Decl; 29 class IdentifierInfo; 30 class ParmVarDecl; 31 class NamedDecl; 32 class ValueDecl; 33 class BlockDecl; 34 class CXXOperatorCallExpr; 35 class CXXMemberCallExpr; 36 37/// Expr - This represents one expression. Note that Expr's are subclasses of 38/// Stmt. This allows an expression to be transparently used any place a Stmt 39/// is required. 40/// 41class Expr : public Stmt { 42 QualType TR; 43 44 /// TypeDependent - Whether this expression is type-dependent 45 /// (C++ [temp.dep.expr]). 46 bool TypeDependent : 1; 47 48 /// ValueDependent - Whether this expression is value-dependent 49 /// (C++ [temp.dep.constexpr]). 50 bool ValueDependent : 1; 51 52protected: 53 // FIXME: Eventually, this constructor should go away and we should 54 // require every subclass to provide type/value-dependence 55 // information. 56 Expr(StmtClass SC, QualType T) 57 : Stmt(SC), TypeDependent(false), ValueDependent(false) { 58 setType(T); 59 } 60 61 Expr(StmtClass SC, QualType T, bool TD, bool VD) 62 : Stmt(SC), TypeDependent(TD), ValueDependent(VD) { 63 setType(T); 64 } 65 66public: 67 QualType getType() const { return TR; } 68 void setType(QualType t) { 69 // In C++, the type of an expression is always adjusted so that it 70 // will not have reference type an expression will never have 71 // reference type (C++ [expr]p6). Use 72 // QualType::getNonReferenceType() to retrieve the non-reference 73 // type. Additionally, inspect Expr::isLvalue to determine whether 74 // an expression that is adjusted in this manner should be 75 // considered an lvalue. 76 assert((TR.isNull() || !TR->isReferenceType()) && 77 "Expressions can't have reference type"); 78 79 TR = t; 80 } 81 82 /// isValueDependent - Determines whether this expression is 83 /// value-dependent (C++ [temp.dep.constexpr]). For example, the 84 /// array bound of "Chars" in the following example is 85 /// value-dependent. 86 /// @code 87 /// template<int Size, char (&Chars)[Size]> struct meta_string; 88 /// @endcode 89 bool isValueDependent() const { return ValueDependent; } 90 91 /// isTypeDependent - Determines whether this expression is 92 /// type-dependent (C++ [temp.dep.expr]), which means that its type 93 /// could change from one template instantiation to the next. For 94 /// example, the expressions "x" and "x + y" are type-dependent in 95 /// the following code, but "y" is not type-dependent: 96 /// @code 97 /// template<typename T> 98 /// void add(T x, int y) { 99 /// x + y; 100 /// } 101 /// @endcode 102 bool isTypeDependent() const { return TypeDependent; } 103 104 /// SourceLocation tokens are not useful in isolation - they are low level 105 /// value objects created/interpreted by SourceManager. We assume AST 106 /// clients will have a pointer to the respective SourceManager. 107 virtual SourceRange getSourceRange() const = 0; 108 109 /// getExprLoc - Return the preferred location for the arrow when diagnosing 110 /// a problem with a generic expression. 111 virtual SourceLocation getExprLoc() const { return getLocStart(); } 112 113 /// hasLocalSideEffect - Return true if this immediate expression has side 114 /// effects, not counting any sub-expressions. 115 bool hasLocalSideEffect() const; 116 117 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 118 /// incomplete type other than void. Nonarray expressions that can be lvalues: 119 /// - name, where name must be a variable 120 /// - e[i] 121 /// - (e), where e must be an lvalue 122 /// - e.name, where e must be an lvalue 123 /// - e->name 124 /// - *e, the type of e cannot be a function type 125 /// - string-constant 126 /// - reference type [C++ [expr]] 127 /// 128 enum isLvalueResult { 129 LV_Valid, 130 LV_NotObjectType, 131 LV_IncompleteVoidType, 132 LV_DuplicateVectorComponents, 133 LV_InvalidExpression, 134 LV_MemberFunction 135 }; 136 isLvalueResult isLvalue(ASTContext &Ctx) const; 137 138 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 139 /// does not have an incomplete type, does not have a const-qualified type, 140 /// and if it is a structure or union, does not have any member (including, 141 /// recursively, any member or element of all contained aggregates or unions) 142 /// with a const-qualified type. 143 enum isModifiableLvalueResult { 144 MLV_Valid, 145 MLV_NotObjectType, 146 MLV_IncompleteVoidType, 147 MLV_DuplicateVectorComponents, 148 MLV_InvalidExpression, 149 MLV_LValueCast, // Specialized form of MLV_InvalidExpression. 150 MLV_IncompleteType, 151 MLV_ConstQualified, 152 MLV_ArrayType, 153 MLV_NotBlockQualified, 154 MLV_ReadonlyProperty, 155 MLV_NoSetterProperty, 156 MLV_MemberFunction 157 }; 158 isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx) const; 159 160 bool isBitField(); 161 162 /// getIntegerConstantExprValue() - Return the value of an integer 163 /// constant expression. The expression must be a valid integer 164 /// constant expression as determined by isIntegerConstantExpr. 165 llvm::APSInt getIntegerConstantExprValue(ASTContext &Ctx) const { 166 llvm::APSInt X; 167 bool success = isIntegerConstantExpr(X, Ctx); 168 success = success; 169 assert(success && "Illegal argument to getIntegerConstantExpr"); 170 return X; 171 } 172 173 /// isIntegerConstantExpr - Return true if this expression is a valid integer 174 /// constant expression, and, if so, return its value in Result. If not a 175 /// valid i-c-e, return false and fill in Loc (if specified) with the location 176 /// of the invalid expression. 177 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 178 SourceLocation *Loc = 0, 179 bool isEvaluated = true) const; 180 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 181 llvm::APSInt X; 182 return isIntegerConstantExpr(X, Ctx, Loc); 183 } 184 /// isConstantExpr - Return true if this expression is a valid constant expr. 185 bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const; 186 187 /// EvalResult is a struct with detailed info about an evaluated expression. 188 struct EvalResult { 189 /// Val - This is the scalar value the expression can be folded to. 190 APValue Val; 191 192 /// HasSideEffects - Whether the evaluated expression has side effects. 193 /// For example, (f() && 0) can be folded, but it still has side effects. 194 bool HasSideEffects; 195 196 /// Diag - If the expression is unfoldable, then Diag contains a note 197 /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret 198 /// position for the error, and DiagExpr is the expression that caused 199 /// the error. 200 /// If the expression is foldable, but not an integer constant expression, 201 /// Diag contains a note diagnostic that describes why it isn't an integer 202 /// constant expression. If the expression *is* an integer constant 203 /// expression, then Diag will be zero. 204 unsigned Diag; 205 const Expr *DiagExpr; 206 SourceLocation DiagLoc; 207 208 EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {} 209 }; 210 211 /// Evaluate - Return true if this is a constant which we can fold using 212 /// any crazy technique (that has nothing to do with language standards) that 213 /// we want to. If this function returns true, it returns the folded constant 214 /// in Result. 215 bool Evaluate(EvalResult &Result, ASTContext &Ctx) const; 216 217 /// isEvaluatable - Call Evaluate to see if this expression can be constant 218 /// folded, but discard the result. 219 bool isEvaluatable(ASTContext &Ctx) const; 220 221 /// EvaluateAsInt - Call Evaluate and return the folded integer. This 222 /// must be called on an expression that constant folds to an integer. 223 llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const; 224 225 /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 226 /// integer constant expression with the value zero, or if this is one that is 227 /// cast to void*. 228 bool isNullPointerConstant(ASTContext &Ctx) const; 229 230 /// hasGlobalStorage - Return true if this expression has static storage 231 /// duration. This means that the address of this expression is a link-time 232 /// constant. 233 bool hasGlobalStorage() const; 234 235 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return 236 /// its subexpression. If that subexpression is also a ParenExpr, 237 /// then this method recursively returns its subexpression, and so forth. 238 /// Otherwise, the method returns the current Expr. 239 Expr* IgnoreParens(); 240 241 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 242 /// or CastExprs, returning their operand. 243 Expr *IgnoreParenCasts(); 244 245 const Expr* IgnoreParens() const { 246 return const_cast<Expr*>(this)->IgnoreParens(); 247 } 248 const Expr *IgnoreParenCasts() const { 249 return const_cast<Expr*>(this)->IgnoreParenCasts(); 250 } 251 252 static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs); 253 static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs); 254 255 static bool classof(const Stmt *T) { 256 return T->getStmtClass() >= firstExprConstant && 257 T->getStmtClass() <= lastExprConstant; 258 } 259 static bool classof(const Expr *) { return true; } 260 261 static inline Expr* Create(llvm::Deserializer& D, ASTContext& C) { 262 return cast<Expr>(Stmt::Create(D, C)); 263 } 264}; 265 266 267//===----------------------------------------------------------------------===// 268// Primary Expressions. 269//===----------------------------------------------------------------------===// 270 271/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 272/// enum, etc. 273class DeclRefExpr : public Expr { 274 NamedDecl *D; 275 SourceLocation Loc; 276 277protected: 278 // FIXME: Eventually, this constructor will go away and all subclasses 279 // will have to provide the type- and value-dependent flags. 280 DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l) : 281 Expr(SC, t), D(d), Loc(l) {} 282 283 DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l, bool TD, 284 bool VD) : 285 Expr(SC, t, TD, VD), D(d), Loc(l) {} 286 287public: 288 // FIXME: Eventually, this constructor will go away and all clients 289 // will have to provide the type- and value-dependent flags. 290 DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l) : 291 Expr(DeclRefExprClass, t), D(d), Loc(l) {} 292 293 DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, bool VD) : 294 Expr(DeclRefExprClass, t, TD, VD), D(d), Loc(l) {} 295 296 NamedDecl *getDecl() { return D; } 297 const NamedDecl *getDecl() const { return D; } 298 void setDecl(NamedDecl *NewD) { D = NewD; } 299 300 SourceLocation getLocation() const { return Loc; } 301 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 302 303 static bool classof(const Stmt *T) { 304 return T->getStmtClass() == DeclRefExprClass || 305 T->getStmtClass() == CXXConditionDeclExprClass || 306 T->getStmtClass() == QualifiedDeclRefExprClass; 307 } 308 static bool classof(const DeclRefExpr *) { return true; } 309 310 // Iterators 311 virtual child_iterator child_begin(); 312 virtual child_iterator child_end(); 313 314 virtual void EmitImpl(llvm::Serializer& S) const; 315 static DeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 316}; 317 318/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__. 319class PredefinedExpr : public Expr { 320public: 321 enum IdentType { 322 Func, 323 Function, 324 PrettyFunction 325 }; 326 327private: 328 SourceLocation Loc; 329 IdentType Type; 330public: 331 PredefinedExpr(SourceLocation l, QualType type, IdentType IT) 332 : Expr(PredefinedExprClass, type), Loc(l), Type(IT) {} 333 334 IdentType getIdentType() const { return Type; } 335 336 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 337 338 static bool classof(const Stmt *T) { 339 return T->getStmtClass() == PredefinedExprClass; 340 } 341 static bool classof(const PredefinedExpr *) { return true; } 342 343 // Iterators 344 virtual child_iterator child_begin(); 345 virtual child_iterator child_end(); 346 347 virtual void EmitImpl(llvm::Serializer& S) const; 348 static PredefinedExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 349}; 350 351class IntegerLiteral : public Expr { 352 llvm::APInt Value; 353 SourceLocation Loc; 354public: 355 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 356 // or UnsignedLongLongTy 357 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 358 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) { 359 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 360 } 361 const llvm::APInt &getValue() const { return Value; } 362 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 363 364 static bool classof(const Stmt *T) { 365 return T->getStmtClass() == IntegerLiteralClass; 366 } 367 static bool classof(const IntegerLiteral *) { return true; } 368 369 // Iterators 370 virtual child_iterator child_begin(); 371 virtual child_iterator child_end(); 372 373 virtual void EmitImpl(llvm::Serializer& S) const; 374 static IntegerLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 375}; 376 377class CharacterLiteral : public Expr { 378 unsigned Value; 379 SourceLocation Loc; 380 bool IsWide; 381public: 382 // type should be IntTy 383 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l) 384 : Expr(CharacterLiteralClass, type), Value(value), Loc(l), IsWide(iswide) { 385 } 386 SourceLocation getLoc() const { return Loc; } 387 bool isWide() const { return IsWide; } 388 389 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 390 391 unsigned getValue() const { return Value; } 392 393 static bool classof(const Stmt *T) { 394 return T->getStmtClass() == CharacterLiteralClass; 395 } 396 static bool classof(const CharacterLiteral *) { return true; } 397 398 // Iterators 399 virtual child_iterator child_begin(); 400 virtual child_iterator child_end(); 401 402 virtual void EmitImpl(llvm::Serializer& S) const; 403 static CharacterLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 404}; 405 406class FloatingLiteral : public Expr { 407 llvm::APFloat Value; 408 bool IsExact : 1; 409 SourceLocation Loc; 410public: 411 FloatingLiteral(const llvm::APFloat &V, bool* isexact, 412 QualType Type, SourceLocation L) 413 : Expr(FloatingLiteralClass, Type), Value(V), IsExact(*isexact), Loc(L) {} 414 415 const llvm::APFloat &getValue() const { return Value; } 416 417 bool isExact() const { return IsExact; } 418 419 /// getValueAsApproximateDouble - This returns the value as an inaccurate 420 /// double. Note that this may cause loss of precision, but is useful for 421 /// debugging dumps, etc. 422 double getValueAsApproximateDouble() const; 423 424 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 425 426 static bool classof(const Stmt *T) { 427 return T->getStmtClass() == FloatingLiteralClass; 428 } 429 static bool classof(const FloatingLiteral *) { return true; } 430 431 // Iterators 432 virtual child_iterator child_begin(); 433 virtual child_iterator child_end(); 434 435 virtual void EmitImpl(llvm::Serializer& S) const; 436 static FloatingLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 437}; 438 439/// ImaginaryLiteral - We support imaginary integer and floating point literals, 440/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 441/// IntegerLiteral classes. Instances of this class always have a Complex type 442/// whose element type matches the subexpression. 443/// 444class ImaginaryLiteral : public Expr { 445 Stmt *Val; 446public: 447 ImaginaryLiteral(Expr *val, QualType Ty) 448 : Expr(ImaginaryLiteralClass, Ty), Val(val) {} 449 450 const Expr *getSubExpr() const { return cast<Expr>(Val); } 451 Expr *getSubExpr() { return cast<Expr>(Val); } 452 453 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 454 static bool classof(const Stmt *T) { 455 return T->getStmtClass() == ImaginaryLiteralClass; 456 } 457 static bool classof(const ImaginaryLiteral *) { return true; } 458 459 // Iterators 460 virtual child_iterator child_begin(); 461 virtual child_iterator child_end(); 462 463 virtual void EmitImpl(llvm::Serializer& S) const; 464 static ImaginaryLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 465}; 466 467/// StringLiteral - This represents a string literal expression, e.g. "foo" 468/// or L"bar" (wide strings). The actual string is returned by getStrData() 469/// is NOT null-terminated, and the length of the string is determined by 470/// calling getByteLength(). The C type for a string is always a 471/// ConstantArrayType. 472class StringLiteral : public Expr { 473 const char *StrData; 474 unsigned ByteLength; 475 bool IsWide; 476 // if the StringLiteral was composed using token pasting, both locations 477 // are needed. If not (the common case), firstTokLoc == lastTokLoc. 478 // FIXME: if space becomes an issue, we should create a sub-class. 479 SourceLocation firstTokLoc, lastTokLoc; 480public: 481 StringLiteral(const char *strData, unsigned byteLength, bool Wide, 482 QualType t, SourceLocation b, SourceLocation e); 483 virtual ~StringLiteral(); 484 485 const char *getStrData() const { return StrData; } 486 unsigned getByteLength() const { return ByteLength; } 487 bool isWide() const { return IsWide; } 488 489 virtual SourceRange getSourceRange() const { 490 return SourceRange(firstTokLoc,lastTokLoc); 491 } 492 static bool classof(const Stmt *T) { 493 return T->getStmtClass() == StringLiteralClass; 494 } 495 static bool classof(const StringLiteral *) { return true; } 496 497 // Iterators 498 virtual child_iterator child_begin(); 499 virtual child_iterator child_end(); 500 501 virtual void EmitImpl(llvm::Serializer& S) const; 502 static StringLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 503}; 504 505/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 506/// AST node is only formed if full location information is requested. 507class ParenExpr : public Expr { 508 SourceLocation L, R; 509 Stmt *Val; 510public: 511 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 512 : Expr(ParenExprClass, val->getType(), 513 val->isTypeDependent(), val->isValueDependent()), 514 L(l), R(r), Val(val) {} 515 516 const Expr *getSubExpr() const { return cast<Expr>(Val); } 517 Expr *getSubExpr() { return cast<Expr>(Val); } 518 virtual SourceRange getSourceRange() const { return SourceRange(L, R); } 519 520 static bool classof(const Stmt *T) { 521 return T->getStmtClass() == ParenExprClass; 522 } 523 static bool classof(const ParenExpr *) { return true; } 524 525 // Iterators 526 virtual child_iterator child_begin(); 527 virtual child_iterator child_end(); 528 529 virtual void EmitImpl(llvm::Serializer& S) const; 530 static ParenExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 531}; 532 533 534/// UnaryOperator - This represents the unary-expression's (except sizeof and 535/// alignof), the postinc/postdec operators from postfix-expression, and various 536/// extensions. 537/// 538/// Notes on various nodes: 539/// 540/// Real/Imag - These return the real/imag part of a complex operand. If 541/// applied to a non-complex value, the former returns its operand and the 542/// later returns zero in the type of the operand. 543/// 544/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose 545/// subexpression is a compound literal with the various MemberExpr and 546/// ArraySubscriptExpr's applied to it. 547/// 548class UnaryOperator : public Expr { 549public: 550 // Note that additions to this should also update the StmtVisitor class. 551 enum Opcode { 552 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 553 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 554 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 555 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 556 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 557 Real, Imag, // "__real expr"/"__imag expr" Extension. 558 Extension, // __extension__ marker. 559 OffsetOf // __builtin_offsetof 560 }; 561private: 562 Stmt *Val; 563 Opcode Opc; 564 SourceLocation Loc; 565public: 566 567 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 568 : Expr(UnaryOperatorClass, type, 569 input->isTypeDependent() && opc != OffsetOf, 570 input->isValueDependent()), 571 Val(input), Opc(opc), Loc(l) {} 572 573 Opcode getOpcode() const { return Opc; } 574 Expr *getSubExpr() const { return cast<Expr>(Val); } 575 576 /// getOperatorLoc - Return the location of the operator. 577 SourceLocation getOperatorLoc() const { return Loc; } 578 579 /// isPostfix - Return true if this is a postfix operation, like x++. 580 static bool isPostfix(Opcode Op); 581 582 /// isPostfix - Return true if this is a prefix operation, like --x. 583 static bool isPrefix(Opcode Op); 584 585 bool isPrefix() const { return isPrefix(Opc); } 586 bool isPostfix() const { return isPostfix(Opc); } 587 bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; } 588 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 589 bool isOffsetOfOp() const { return Opc == OffsetOf; } 590 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 591 592 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 593 /// corresponds to, e.g. "sizeof" or "[pre]++" 594 static const char *getOpcodeStr(Opcode Op); 595 596 virtual SourceRange getSourceRange() const { 597 if (isPostfix()) 598 return SourceRange(Val->getLocStart(), Loc); 599 else 600 return SourceRange(Loc, Val->getLocEnd()); 601 } 602 virtual SourceLocation getExprLoc() const { return Loc; } 603 604 static bool classof(const Stmt *T) { 605 return T->getStmtClass() == UnaryOperatorClass; 606 } 607 static bool classof(const UnaryOperator *) { return true; } 608 609 int64_t evaluateOffsetOf(ASTContext& C) const; 610 611 // Iterators 612 virtual child_iterator child_begin(); 613 virtual child_iterator child_end(); 614 615 virtual void EmitImpl(llvm::Serializer& S) const; 616 static UnaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 617}; 618 619/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of 620/// types and expressions. 621class SizeOfAlignOfExpr : public Expr { 622 bool isSizeof : 1; // true if sizeof, false if alignof. 623 bool isType : 1; // true if operand is a type, false if an expression 624 union { 625 void *Ty; 626 Stmt *Ex; 627 } Argument; 628 SourceLocation OpLoc, RParenLoc; 629public: 630 SizeOfAlignOfExpr(bool issizeof, bool istype, void *argument, 631 QualType resultType, SourceLocation op, 632 SourceLocation rp) : 633 Expr(SizeOfAlignOfExprClass, resultType), isSizeof(issizeof), 634 isType(istype), OpLoc(op), RParenLoc(rp) { 635 if (isType) 636 Argument.Ty = argument; 637 else 638 // argument was an Expr*, so cast it back to that to be safe 639 Argument.Ex = static_cast<Expr*>(argument); 640 } 641 642 virtual void Destroy(ASTContext& C); 643 644 bool isSizeOf() const { return isSizeof; } 645 bool isArgumentType() const { return isType; } 646 QualType getArgumentType() const { 647 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 648 return QualType::getFromOpaquePtr(Argument.Ty); 649 } 650 Expr *getArgumentExpr() { 651 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 652 return static_cast<Expr*>(Argument.Ex); 653 } 654 const Expr *getArgumentExpr() const { 655 return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr(); 656 } 657 658 /// Gets the argument type, or the type of the argument expression, whichever 659 /// is appropriate. 660 QualType getTypeOfArgument() const { 661 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 662 } 663 664 SourceLocation getOperatorLoc() const { return OpLoc; } 665 666 virtual SourceRange getSourceRange() const { 667 return SourceRange(OpLoc, RParenLoc); 668 } 669 670 static bool classof(const Stmt *T) { 671 return T->getStmtClass() == SizeOfAlignOfExprClass; 672 } 673 static bool classof(const SizeOfAlignOfExpr *) { return true; } 674 675 // Iterators 676 virtual child_iterator child_begin(); 677 virtual child_iterator child_end(); 678 679 virtual void EmitImpl(llvm::Serializer& S) const; 680 static SizeOfAlignOfExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 681}; 682 683//===----------------------------------------------------------------------===// 684// Postfix Operators. 685//===----------------------------------------------------------------------===// 686 687/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 688class ArraySubscriptExpr : public Expr { 689 enum { LHS, RHS, END_EXPR=2 }; 690 Stmt* SubExprs[END_EXPR]; 691 SourceLocation RBracketLoc; 692public: 693 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 694 SourceLocation rbracketloc) 695 : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) { 696 SubExprs[LHS] = lhs; 697 SubExprs[RHS] = rhs; 698 } 699 700 /// An array access can be written A[4] or 4[A] (both are equivalent). 701 /// - getBase() and getIdx() always present the normalized view: A[4]. 702 /// In this case getBase() returns "A" and getIdx() returns "4". 703 /// - getLHS() and getRHS() present the syntactic view. e.g. for 704 /// 4[A] getLHS() returns "4". 705 /// Note: Because vector element access is also written A[4] we must 706 /// predicate the format conversion in getBase and getIdx only on the 707 /// the type of the RHS, as it is possible for the LHS to be a vector of 708 /// integer type 709 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 710 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 711 712 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 713 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 714 715 Expr *getBase() { 716 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 717 } 718 719 const Expr *getBase() const { 720 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 721 } 722 723 Expr *getIdx() { 724 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 725 } 726 727 const Expr *getIdx() const { 728 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 729 } 730 731 virtual SourceRange getSourceRange() const { 732 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 733 } 734 735 virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); } 736 737 static bool classof(const Stmt *T) { 738 return T->getStmtClass() == ArraySubscriptExprClass; 739 } 740 static bool classof(const ArraySubscriptExpr *) { return true; } 741 742 // Iterators 743 virtual child_iterator child_begin(); 744 virtual child_iterator child_end(); 745 746 virtual void EmitImpl(llvm::Serializer& S) const; 747 static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 748}; 749 750 751/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 752/// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 753/// while its subclasses may represent alternative syntax that (semantically) 754/// results in a function call. For example, CXXOperatorCallExpr is 755/// a subclass for overloaded operator calls that use operator syntax, e.g., 756/// "str1 + str2" to resolve to a function call. 757class CallExpr : public Expr { 758 enum { FN=0, ARGS_START=1 }; 759 Stmt **SubExprs; 760 unsigned NumArgs; 761 SourceLocation RParenLoc; 762 763 // This version of the ctor is for deserialization. 764 CallExpr(StmtClass SC, Stmt** subexprs, unsigned numargs, QualType t, 765 SourceLocation rparenloc) 766 : Expr(SC,t), SubExprs(subexprs), 767 NumArgs(numargs), RParenLoc(rparenloc) {} 768 769protected: 770 // This version of the constructor is for derived classes. 771 CallExpr(StmtClass SC, Expr *fn, Expr **args, unsigned numargs, QualType t, 772 SourceLocation rparenloc); 773 774public: 775 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 776 SourceLocation rparenloc); 777 ~CallExpr() { 778 delete [] SubExprs; 779 } 780 781 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 782 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 783 void setCallee(Expr *F) { SubExprs[FN] = F; } 784 785 /// getNumArgs - Return the number of actual arguments to this call. 786 /// 787 unsigned getNumArgs() const { return NumArgs; } 788 789 /// getArg - Return the specified argument. 790 Expr *getArg(unsigned Arg) { 791 assert(Arg < NumArgs && "Arg access out of range!"); 792 return cast<Expr>(SubExprs[Arg+ARGS_START]); 793 } 794 const Expr *getArg(unsigned Arg) const { 795 assert(Arg < NumArgs && "Arg access out of range!"); 796 return cast<Expr>(SubExprs[Arg+ARGS_START]); 797 } 798 /// setArg - Set the specified argument. 799 void setArg(unsigned Arg, Expr *ArgExpr) { 800 assert(Arg < NumArgs && "Arg access out of range!"); 801 SubExprs[Arg+ARGS_START] = ArgExpr; 802 } 803 804 /// setNumArgs - This changes the number of arguments present in this call. 805 /// Any orphaned expressions are deleted by this, and any new operands are set 806 /// to null. 807 void setNumArgs(unsigned NumArgs); 808 809 typedef ExprIterator arg_iterator; 810 typedef ConstExprIterator const_arg_iterator; 811 812 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 813 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 814 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 815 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 816 817 /// getNumCommas - Return the number of commas that must have been present in 818 /// this function call. 819 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 820 821 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 822 /// not, return 0. 823 unsigned isBuiltinCall() const; 824 825 SourceLocation getRParenLoc() const { return RParenLoc; } 826 827 virtual SourceRange getSourceRange() const { 828 return SourceRange(getCallee()->getLocStart(), RParenLoc); 829 } 830 831 static bool classof(const Stmt *T) { 832 return T->getStmtClass() == CallExprClass || 833 T->getStmtClass() == CXXOperatorCallExprClass || 834 T->getStmtClass() == CXXMemberCallExprClass; 835 } 836 static bool classof(const CallExpr *) { return true; } 837 static bool classof(const CXXOperatorCallExpr *) { return true; } 838 static bool classof(const CXXMemberCallExpr *) { return true; } 839 840 // Iterators 841 virtual child_iterator child_begin(); 842 virtual child_iterator child_end(); 843 844 virtual void EmitImpl(llvm::Serializer& S) const; 845 static CallExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C, 846 StmtClass SC); 847}; 848 849/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 850/// 851class MemberExpr : public Expr { 852 Stmt *Base; 853 NamedDecl *MemberDecl; 854 SourceLocation MemberLoc; 855 bool IsArrow; // True if this is "X->F", false if this is "X.F". 856public: 857 MemberExpr(Expr *base, bool isarrow, NamedDecl *memberdecl, SourceLocation l, 858 QualType ty) 859 : Expr(MemberExprClass, ty), 860 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 861 862 void setBase(Expr *E) { Base = E; } 863 Expr *getBase() const { return cast<Expr>(Base); } 864 NamedDecl *getMemberDecl() const { return MemberDecl; } 865 void setMemberDecl(NamedDecl *D) { MemberDecl = D; } 866 bool isArrow() const { return IsArrow; } 867 868 virtual SourceRange getSourceRange() const { 869 return SourceRange(getBase()->getLocStart(), MemberLoc); 870 } 871 872 virtual SourceLocation getExprLoc() const { return MemberLoc; } 873 874 static bool classof(const Stmt *T) { 875 return T->getStmtClass() == MemberExprClass; 876 } 877 static bool classof(const MemberExpr *) { return true; } 878 879 // Iterators 880 virtual child_iterator child_begin(); 881 virtual child_iterator child_end(); 882 883 virtual void EmitImpl(llvm::Serializer& S) const; 884 static MemberExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 885}; 886 887/// CompoundLiteralExpr - [C99 6.5.2.5] 888/// 889class CompoundLiteralExpr : public Expr { 890 /// LParenLoc - If non-null, this is the location of the left paren in a 891 /// compound literal like "(int){4}". This can be null if this is a 892 /// synthesized compound expression. 893 SourceLocation LParenLoc; 894 Stmt *Init; 895 bool FileScope; 896public: 897 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, 898 bool fileScope) 899 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), 900 FileScope(fileScope) {} 901 902 const Expr *getInitializer() const { return cast<Expr>(Init); } 903 Expr *getInitializer() { return cast<Expr>(Init); } 904 905 bool isFileScope() const { return FileScope; } 906 907 SourceLocation getLParenLoc() const { return LParenLoc; } 908 909 virtual SourceRange getSourceRange() const { 910 // FIXME: Init should never be null. 911 if (!Init) 912 return SourceRange(); 913 if (LParenLoc.isInvalid()) 914 return Init->getSourceRange(); 915 return SourceRange(LParenLoc, Init->getLocEnd()); 916 } 917 918 static bool classof(const Stmt *T) { 919 return T->getStmtClass() == CompoundLiteralExprClass; 920 } 921 static bool classof(const CompoundLiteralExpr *) { return true; } 922 923 // Iterators 924 virtual child_iterator child_begin(); 925 virtual child_iterator child_end(); 926 927 virtual void EmitImpl(llvm::Serializer& S) const; 928 static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 929}; 930 931/// CastExpr - Base class for type casts, including both implicit 932/// casts (ImplicitCastExpr) and explicit casts that have some 933/// representation in the source code (ExplicitCastExpr's derived 934/// classes). 935class CastExpr : public Expr { 936 Stmt *Op; 937protected: 938 CastExpr(StmtClass SC, QualType ty, Expr *op) : 939 Expr(SC, ty, 940 // Cast expressions are type-dependent if the type is 941 // dependent (C++ [temp.dep.expr]p3). 942 ty->isDependentType(), 943 // Cast expressions are value-dependent if the type is 944 // dependent or if the subexpression is value-dependent. 945 ty->isDependentType() || (op && op->isValueDependent())), 946 Op(op) {} 947 948public: 949 Expr *getSubExpr() { return cast<Expr>(Op); } 950 const Expr *getSubExpr() const { return cast<Expr>(Op); } 951 952 static bool classof(const Stmt *T) { 953 StmtClass SC = T->getStmtClass(); 954 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 955 return true; 956 957 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 958 return true; 959 960 return false; 961 } 962 static bool classof(const CastExpr *) { return true; } 963 964 // Iterators 965 virtual child_iterator child_begin(); 966 virtual child_iterator child_end(); 967}; 968 969/// ImplicitCastExpr - Allows us to explicitly represent implicit type 970/// conversions, which have no direct representation in the original 971/// source code. For example: converting T[]->T*, void f()->void 972/// (*f)(), float->double, short->int, etc. 973/// 974/// In C, implicit casts always produce rvalues. However, in C++, an 975/// implicit cast whose result is being bound to a reference will be 976/// an lvalue. For example: 977/// 978/// @code 979/// class Base { }; 980/// class Derived : public Base { }; 981/// void f(Derived d) { 982/// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base 983/// } 984/// @endcode 985class ImplicitCastExpr : public CastExpr { 986 /// LvalueCast - Whether this cast produces an lvalue. 987 bool LvalueCast; 988 989public: 990 ImplicitCastExpr(QualType ty, Expr *op, bool Lvalue) : 991 CastExpr(ImplicitCastExprClass, ty, op), LvalueCast(Lvalue) { } 992 993 virtual SourceRange getSourceRange() const { 994 return getSubExpr()->getSourceRange(); 995 } 996 997 /// isLvalueCast - Whether this cast produces an lvalue. 998 bool isLvalueCast() const { return LvalueCast; } 999 1000 /// setLvalueCast - Set whether this cast produces an lvalue. 1001 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 1002 1003 static bool classof(const Stmt *T) { 1004 return T->getStmtClass() == ImplicitCastExprClass; 1005 } 1006 static bool classof(const ImplicitCastExpr *) { return true; } 1007 1008 virtual void EmitImpl(llvm::Serializer& S) const; 1009 static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1010}; 1011 1012/// ExplicitCastExpr - An explicit cast written in the source 1013/// code. 1014/// 1015/// This class is effectively an abstract class, because it provides 1016/// the basic representation of an explicitly-written cast without 1017/// specifying which kind of cast (C cast, functional cast, static 1018/// cast, etc.) was written; specific derived classes represent the 1019/// particular style of cast and its location information. 1020/// 1021/// Unlike implicit casts, explicit cast nodes have two different 1022/// types: the type that was written into the source code, and the 1023/// actual type of the expression as determined by semantic 1024/// analysis. These types may differ slightly. For example, in C++ one 1025/// can cast to a reference type, which indicates that the resulting 1026/// expression will be an lvalue. The reference type, however, will 1027/// not be used as the type of the expression. 1028class ExplicitCastExpr : public CastExpr { 1029 /// TypeAsWritten - The type that this expression is casting to, as 1030 /// written in the source code. 1031 QualType TypeAsWritten; 1032 1033protected: 1034 ExplicitCastExpr(StmtClass SC, QualType exprTy, Expr *op, QualType writtenTy) 1035 : CastExpr(SC, exprTy, op), TypeAsWritten(writtenTy) {} 1036 1037public: 1038 /// getTypeAsWritten - Returns the type that this expression is 1039 /// casting to, as written in the source code. 1040 QualType getTypeAsWritten() const { return TypeAsWritten; } 1041 1042 static bool classof(const Stmt *T) { 1043 StmtClass SC = T->getStmtClass(); 1044 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass) 1045 return true; 1046 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1047 return true; 1048 1049 return false; 1050 } 1051 static bool classof(const ExplicitCastExpr *) { return true; } 1052}; 1053 1054/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 1055/// cast in C++ (C++ [expr.cast]), which uses the syntax 1056/// (Type)expr. For example: @c (int)f. 1057class CStyleCastExpr : public ExplicitCastExpr { 1058 SourceLocation LPLoc; // the location of the left paren 1059 SourceLocation RPLoc; // the location of the right paren 1060public: 1061 CStyleCastExpr(QualType exprTy, Expr *op, QualType writtenTy, 1062 SourceLocation l, SourceLocation r) : 1063 ExplicitCastExpr(CStyleCastExprClass, exprTy, op, writtenTy), 1064 LPLoc(l), RPLoc(r) {} 1065 1066 SourceLocation getLParenLoc() const { return LPLoc; } 1067 SourceLocation getRParenLoc() const { return RPLoc; } 1068 1069 virtual SourceRange getSourceRange() const { 1070 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 1071 } 1072 static bool classof(const Stmt *T) { 1073 return T->getStmtClass() == CStyleCastExprClass; 1074 } 1075 static bool classof(const CStyleCastExpr *) { return true; } 1076 1077 virtual void EmitImpl(llvm::Serializer& S) const; 1078 static CStyleCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1079}; 1080 1081class BinaryOperator : public Expr { 1082public: 1083 enum Opcode { 1084 // Operators listed in order of precedence. 1085 // Note that additions to this should also update the StmtVisitor class. 1086 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 1087 Add, Sub, // [C99 6.5.6] Additive operators. 1088 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 1089 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 1090 EQ, NE, // [C99 6.5.9] Equality operators. 1091 And, // [C99 6.5.10] Bitwise AND operator. 1092 Xor, // [C99 6.5.11] Bitwise XOR operator. 1093 Or, // [C99 6.5.12] Bitwise OR operator. 1094 LAnd, // [C99 6.5.13] Logical AND operator. 1095 LOr, // [C99 6.5.14] Logical OR operator. 1096 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 1097 DivAssign, RemAssign, 1098 AddAssign, SubAssign, 1099 ShlAssign, ShrAssign, 1100 AndAssign, XorAssign, 1101 OrAssign, 1102 Comma // [C99 6.5.17] Comma operator. 1103 }; 1104private: 1105 enum { LHS, RHS, END_EXPR }; 1106 Stmt* SubExprs[END_EXPR]; 1107 Opcode Opc; 1108 SourceLocation OpLoc; 1109public: 1110 1111 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1112 SourceLocation opLoc) 1113 : Expr(BinaryOperatorClass, ResTy, 1114 lhs->isTypeDependent() || rhs->isTypeDependent(), 1115 lhs->isValueDependent() || rhs->isValueDependent()), 1116 Opc(opc), OpLoc(opLoc) { 1117 SubExprs[LHS] = lhs; 1118 SubExprs[RHS] = rhs; 1119 assert(!isCompoundAssignmentOp() && 1120 "Use ArithAssignBinaryOperator for compound assignments"); 1121 } 1122 1123 SourceLocation getOperatorLoc() const { return OpLoc; } 1124 Opcode getOpcode() const { return Opc; } 1125 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1126 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1127 virtual SourceRange getSourceRange() const { 1128 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 1129 } 1130 1131 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1132 /// corresponds to, e.g. "<<=". 1133 static const char *getOpcodeStr(Opcode Op); 1134 1135 /// predicates to categorize the respective opcodes. 1136 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 1137 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 1138 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 1139 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 1140 1141 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 1142 bool isRelationalOp() const { return isRelationalOp(Opc); } 1143 1144 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 1145 bool isEqualityOp() const { return isEqualityOp(Opc); } 1146 1147 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 1148 bool isLogicalOp() const { return isLogicalOp(Opc); } 1149 1150 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 1151 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 1152 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 1153 1154 static bool classof(const Stmt *S) { 1155 return S->getStmtClass() == BinaryOperatorClass || 1156 S->getStmtClass() == CompoundAssignOperatorClass; 1157 } 1158 static bool classof(const BinaryOperator *) { return true; } 1159 1160 // Iterators 1161 virtual child_iterator child_begin(); 1162 virtual child_iterator child_end(); 1163 1164 virtual void EmitImpl(llvm::Serializer& S) const; 1165 static BinaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1166 1167protected: 1168 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1169 SourceLocation oploc, bool dead) 1170 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 1171 SubExprs[LHS] = lhs; 1172 SubExprs[RHS] = rhs; 1173 } 1174}; 1175 1176/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 1177/// track of the type the operation is performed in. Due to the semantics of 1178/// these operators, the operands are promoted, the aritmetic performed, an 1179/// implicit conversion back to the result type done, then the assignment takes 1180/// place. This captures the intermediate type which the computation is done 1181/// in. 1182class CompoundAssignOperator : public BinaryOperator { 1183 QualType ComputationType; 1184public: 1185 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1186 QualType ResType, QualType CompType, 1187 SourceLocation OpLoc) 1188 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1189 ComputationType(CompType) { 1190 assert(isCompoundAssignmentOp() && 1191 "Only should be used for compound assignments"); 1192 } 1193 1194 QualType getComputationType() const { return ComputationType; } 1195 1196 static bool classof(const CompoundAssignOperator *) { return true; } 1197 static bool classof(const Stmt *S) { 1198 return S->getStmtClass() == CompoundAssignOperatorClass; 1199 } 1200 1201 virtual void EmitImpl(llvm::Serializer& S) const; 1202 static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D, 1203 ASTContext& C); 1204}; 1205 1206/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1207/// GNU "missing LHS" extension is in use. 1208/// 1209class ConditionalOperator : public Expr { 1210 enum { COND, LHS, RHS, END_EXPR }; 1211 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1212public: 1213 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 1214 : Expr(ConditionalOperatorClass, t, 1215 // FIXME: the type of the conditional operator doesn't 1216 // depend on the type of the conditional, but the standard 1217 // seems to imply that it could. File a bug! 1218 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 1219 (cond->isValueDependent() || 1220 (lhs && lhs->isValueDependent()) || 1221 (rhs && rhs->isValueDependent()))) { 1222 SubExprs[COND] = cond; 1223 SubExprs[LHS] = lhs; 1224 SubExprs[RHS] = rhs; 1225 } 1226 1227 // getCond - Return the expression representing the condition for 1228 // the ?: operator. 1229 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1230 1231 // getTrueExpr - Return the subexpression representing the value of the ?: 1232 // expression if the condition evaluates to true. In most cases this value 1233 // will be the same as getLHS() except a GCC extension allows the left 1234 // subexpression to be omitted, and instead of the condition be returned. 1235 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1236 // is only evaluated once. 1237 Expr *getTrueExpr() const { 1238 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1239 } 1240 1241 // getTrueExpr - Return the subexpression representing the value of the ?: 1242 // expression if the condition evaluates to false. This is the same as getRHS. 1243 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1244 1245 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1246 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1247 1248 virtual SourceRange getSourceRange() const { 1249 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1250 } 1251 static bool classof(const Stmt *T) { 1252 return T->getStmtClass() == ConditionalOperatorClass; 1253 } 1254 static bool classof(const ConditionalOperator *) { return true; } 1255 1256 // Iterators 1257 virtual child_iterator child_begin(); 1258 virtual child_iterator child_end(); 1259 1260 virtual void EmitImpl(llvm::Serializer& S) const; 1261 static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1262}; 1263 1264/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1265class AddrLabelExpr : public Expr { 1266 SourceLocation AmpAmpLoc, LabelLoc; 1267 LabelStmt *Label; 1268public: 1269 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 1270 QualType t) 1271 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 1272 1273 virtual SourceRange getSourceRange() const { 1274 return SourceRange(AmpAmpLoc, LabelLoc); 1275 } 1276 1277 LabelStmt *getLabel() const { return Label; } 1278 1279 static bool classof(const Stmt *T) { 1280 return T->getStmtClass() == AddrLabelExprClass; 1281 } 1282 static bool classof(const AddrLabelExpr *) { return true; } 1283 1284 // Iterators 1285 virtual child_iterator child_begin(); 1286 virtual child_iterator child_end(); 1287 1288 virtual void EmitImpl(llvm::Serializer& S) const; 1289 static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1290}; 1291 1292/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 1293/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 1294/// takes the value of the last subexpression. 1295class StmtExpr : public Expr { 1296 Stmt *SubStmt; 1297 SourceLocation LParenLoc, RParenLoc; 1298public: 1299 StmtExpr(CompoundStmt *substmt, QualType T, 1300 SourceLocation lp, SourceLocation rp) : 1301 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 1302 1303 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 1304 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 1305 1306 virtual SourceRange getSourceRange() const { 1307 return SourceRange(LParenLoc, RParenLoc); 1308 } 1309 1310 static bool classof(const Stmt *T) { 1311 return T->getStmtClass() == StmtExprClass; 1312 } 1313 static bool classof(const StmtExpr *) { return true; } 1314 1315 // Iterators 1316 virtual child_iterator child_begin(); 1317 virtual child_iterator child_end(); 1318 1319 virtual void EmitImpl(llvm::Serializer& S) const; 1320 static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1321}; 1322 1323/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 1324/// This AST node represents a function that returns 1 if two *types* (not 1325/// expressions) are compatible. The result of this built-in function can be 1326/// used in integer constant expressions. 1327class TypesCompatibleExpr : public Expr { 1328 QualType Type1; 1329 QualType Type2; 1330 SourceLocation BuiltinLoc, RParenLoc; 1331public: 1332 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1333 QualType t1, QualType t2, SourceLocation RP) : 1334 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1335 BuiltinLoc(BLoc), RParenLoc(RP) {} 1336 1337 QualType getArgType1() const { return Type1; } 1338 QualType getArgType2() const { return Type2; } 1339 1340 virtual SourceRange getSourceRange() const { 1341 return SourceRange(BuiltinLoc, RParenLoc); 1342 } 1343 static bool classof(const Stmt *T) { 1344 return T->getStmtClass() == TypesCompatibleExprClass; 1345 } 1346 static bool classof(const TypesCompatibleExpr *) { return true; } 1347 1348 // Iterators 1349 virtual child_iterator child_begin(); 1350 virtual child_iterator child_end(); 1351 1352 virtual void EmitImpl(llvm::Serializer& S) const; 1353 static TypesCompatibleExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1354}; 1355 1356/// ShuffleVectorExpr - clang-specific builtin-in function 1357/// __builtin_shufflevector. 1358/// This AST node represents a operator that does a constant 1359/// shuffle, similar to LLVM's shufflevector instruction. It takes 1360/// two vectors and a variable number of constant indices, 1361/// and returns the appropriately shuffled vector. 1362class ShuffleVectorExpr : public Expr { 1363 SourceLocation BuiltinLoc, RParenLoc; 1364 1365 // SubExprs - the list of values passed to the __builtin_shufflevector 1366 // function. The first two are vectors, and the rest are constant 1367 // indices. The number of values in this list is always 1368 // 2+the number of indices in the vector type. 1369 Stmt **SubExprs; 1370 unsigned NumExprs; 1371 1372public: 1373 ShuffleVectorExpr(Expr **args, unsigned nexpr, 1374 QualType Type, SourceLocation BLoc, 1375 SourceLocation RP) : 1376 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 1377 RParenLoc(RP), NumExprs(nexpr) { 1378 1379 SubExprs = new Stmt*[nexpr]; 1380 for (unsigned i = 0; i < nexpr; i++) 1381 SubExprs[i] = args[i]; 1382 } 1383 1384 virtual SourceRange getSourceRange() const { 1385 return SourceRange(BuiltinLoc, RParenLoc); 1386 } 1387 static bool classof(const Stmt *T) { 1388 return T->getStmtClass() == ShuffleVectorExprClass; 1389 } 1390 static bool classof(const ShuffleVectorExpr *) { return true; } 1391 1392 ~ShuffleVectorExpr() { 1393 delete [] SubExprs; 1394 } 1395 1396 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1397 /// constant expression, the actual arguments passed in, and the function 1398 /// pointers. 1399 unsigned getNumSubExprs() const { return NumExprs; } 1400 1401 /// getExpr - Return the Expr at the specified index. 1402 Expr *getExpr(unsigned Index) { 1403 assert((Index < NumExprs) && "Arg access out of range!"); 1404 return cast<Expr>(SubExprs[Index]); 1405 } 1406 const Expr *getExpr(unsigned Index) const { 1407 assert((Index < NumExprs) && "Arg access out of range!"); 1408 return cast<Expr>(SubExprs[Index]); 1409 } 1410 1411 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 1412 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 1413 return getExpr(N+2)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1414 } 1415 1416 // Iterators 1417 virtual child_iterator child_begin(); 1418 virtual child_iterator child_end(); 1419 1420 virtual void EmitImpl(llvm::Serializer& S) const; 1421 static ShuffleVectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1422}; 1423 1424/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 1425/// This AST node is similar to the conditional operator (?:) in C, with 1426/// the following exceptions: 1427/// - the test expression must be a constant expression. 1428/// - the expression returned has it's type unaltered by promotion rules. 1429/// - does not evaluate the expression that was not chosen. 1430class ChooseExpr : public Expr { 1431 enum { COND, LHS, RHS, END_EXPR }; 1432 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1433 SourceLocation BuiltinLoc, RParenLoc; 1434public: 1435 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 1436 SourceLocation RP) 1437 : Expr(ChooseExprClass, t), 1438 BuiltinLoc(BLoc), RParenLoc(RP) { 1439 SubExprs[COND] = cond; 1440 SubExprs[LHS] = lhs; 1441 SubExprs[RHS] = rhs; 1442 } 1443 1444 /// isConditionTrue - Return true if the condition is true. This is always 1445 /// statically knowable for a well-formed choosexpr. 1446 bool isConditionTrue(ASTContext &C) const; 1447 1448 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1449 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1450 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1451 1452 virtual SourceRange getSourceRange() const { 1453 return SourceRange(BuiltinLoc, RParenLoc); 1454 } 1455 static bool classof(const Stmt *T) { 1456 return T->getStmtClass() == ChooseExprClass; 1457 } 1458 static bool classof(const ChooseExpr *) { return true; } 1459 1460 // Iterators 1461 virtual child_iterator child_begin(); 1462 virtual child_iterator child_end(); 1463 1464 virtual void EmitImpl(llvm::Serializer& S) const; 1465 static ChooseExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1466}; 1467 1468/// GNUNullExpr - Implements the GNU __null extension, which is a name 1469/// for a null pointer constant that has integral type (e.g., int or 1470/// long) and is the same size and alignment as a pointer. The __null 1471/// extension is typically only used by system headers, which define 1472/// NULL as __null in C++ rather than using 0 (which is an integer 1473/// that may not match the size of a pointer). 1474class GNUNullExpr : public Expr { 1475 /// TokenLoc - The location of the __null keyword. 1476 SourceLocation TokenLoc; 1477 1478public: 1479 GNUNullExpr(QualType Ty, SourceLocation Loc) 1480 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { } 1481 1482 /// getTokenLocation - The location of the __null token. 1483 SourceLocation getTokenLocation() const { return TokenLoc; } 1484 1485 virtual SourceRange getSourceRange() const { 1486 return SourceRange(TokenLoc); 1487 } 1488 static bool classof(const Stmt *T) { 1489 return T->getStmtClass() == GNUNullExprClass; 1490 } 1491 static bool classof(const GNUNullExpr *) { return true; } 1492 1493 // Iterators 1494 virtual child_iterator child_begin(); 1495 virtual child_iterator child_end(); 1496 1497 virtual void EmitImpl(llvm::Serializer& S) const; 1498 static GNUNullExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1499}; 1500 1501/// OverloadExpr - Clang builtin function __builtin_overload. 1502/// This AST node provides a way to overload functions in C. 1503/// 1504/// The first argument is required to be a constant expression, for the number 1505/// of arguments passed to each candidate function. 1506/// 1507/// The next N arguments, where N is the value of the constant expression, 1508/// are the values to be passed as arguments. 1509/// 1510/// The rest of the arguments are values of pointer to function type, which 1511/// are the candidate functions for overloading. 1512/// 1513/// The result is a equivalent to a CallExpr taking N arguments to the 1514/// candidate function whose parameter types match the types of the N arguments. 1515/// 1516/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl); 1517/// If X and Y are long doubles, Z will assigned the result of modl(X, Y); 1518/// If X and Y are floats, Z will be assigned the result of modf(X, Y); 1519class OverloadExpr : public Expr { 1520 // SubExprs - the list of values passed to the __builtin_overload function. 1521 // SubExpr[0] is a constant expression 1522 // SubExpr[1-N] are the parameters to pass to the matching function call 1523 // SubExpr[N-...] are the candidate functions, of type pointer to function. 1524 Stmt **SubExprs; 1525 1526 // NumExprs - the size of the SubExprs array 1527 unsigned NumExprs; 1528 1529 // The index of the matching candidate function 1530 unsigned FnIndex; 1531 1532 SourceLocation BuiltinLoc; 1533 SourceLocation RParenLoc; 1534public: 1535 OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t, 1536 SourceLocation bloc, SourceLocation rploc) 1537 : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx), 1538 BuiltinLoc(bloc), RParenLoc(rploc) { 1539 SubExprs = new Stmt*[nexprs]; 1540 for (unsigned i = 0; i != nexprs; ++i) 1541 SubExprs[i] = args[i]; 1542 } 1543 ~OverloadExpr() { 1544 delete [] SubExprs; 1545 } 1546 1547 /// arg_begin - Return a pointer to the list of arguments that will be passed 1548 /// to the matching candidate function, skipping over the initial constant 1549 /// expression. 1550 typedef ConstExprIterator const_arg_iterator; 1551 const_arg_iterator arg_begin() const { return &SubExprs[0]+1; } 1552 const_arg_iterator arg_end(ASTContext& Ctx) const { 1553 return &SubExprs[0]+1+getNumArgs(Ctx); 1554 } 1555 1556 /// getNumArgs - Return the number of arguments to pass to the candidate 1557 /// functions. 1558 unsigned getNumArgs(ASTContext &Ctx) const { 1559 return getExpr(0)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1560 } 1561 1562 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1563 /// constant expression, the actual arguments passed in, and the function 1564 /// pointers. 1565 unsigned getNumSubExprs() const { return NumExprs; } 1566 1567 /// getExpr - Return the Expr at the specified index. 1568 Expr *getExpr(unsigned Index) const { 1569 assert((Index < NumExprs) && "Arg access out of range!"); 1570 return cast<Expr>(SubExprs[Index]); 1571 } 1572 1573 /// getFn - Return the matching candidate function for this OverloadExpr. 1574 Expr *getFn() const { return cast<Expr>(SubExprs[FnIndex]); } 1575 1576 virtual SourceRange getSourceRange() const { 1577 return SourceRange(BuiltinLoc, RParenLoc); 1578 } 1579 static bool classof(const Stmt *T) { 1580 return T->getStmtClass() == OverloadExprClass; 1581 } 1582 static bool classof(const OverloadExpr *) { return true; } 1583 1584 // Iterators 1585 virtual child_iterator child_begin(); 1586 virtual child_iterator child_end(); 1587 1588 virtual void EmitImpl(llvm::Serializer& S) const; 1589 static OverloadExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1590}; 1591 1592/// VAArgExpr, used for the builtin function __builtin_va_start. 1593class VAArgExpr : public Expr { 1594 Stmt *Val; 1595 SourceLocation BuiltinLoc, RParenLoc; 1596public: 1597 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 1598 : Expr(VAArgExprClass, t), 1599 Val(e), 1600 BuiltinLoc(BLoc), 1601 RParenLoc(RPLoc) { } 1602 1603 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1604 Expr *getSubExpr() { return cast<Expr>(Val); } 1605 virtual SourceRange getSourceRange() const { 1606 return SourceRange(BuiltinLoc, RParenLoc); 1607 } 1608 static bool classof(const Stmt *T) { 1609 return T->getStmtClass() == VAArgExprClass; 1610 } 1611 static bool classof(const VAArgExpr *) { return true; } 1612 1613 // Iterators 1614 virtual child_iterator child_begin(); 1615 virtual child_iterator child_end(); 1616 1617 virtual void EmitImpl(llvm::Serializer& S) const; 1618 static VAArgExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1619}; 1620 1621/// InitListExpr - used for struct and array initializers, such as: 1622/// struct foo x = { 1, { 2, 3 } }; 1623/// 1624/// Because C is somewhat loose with braces, the AST does not necessarily 1625/// directly model the C source. Instead, the semantic analyzer aims to make 1626/// the InitListExprs match up with the type of the decl being initialized. We 1627/// have the following exceptions: 1628/// 1629/// 1. Elements at the end of the list may be dropped from the initializer. 1630/// These elements are defined to be initialized to zero. For example: 1631/// int x[20] = { 1 }; 1632/// 2. Initializers may have excess initializers which are to be ignored by the 1633/// compiler. For example: 1634/// int x[1] = { 1, 2 }; 1635/// 3. Redundant InitListExprs may be present around scalar elements. These 1636/// always have a single element whose type is the same as the InitListExpr. 1637/// this can only happen for Type::isScalarType() types. 1638/// int x = { 1 }; int y[2] = { {1}, {2} }; 1639/// 1640class InitListExpr : public Expr { 1641 std::vector<Stmt *> InitExprs; 1642 SourceLocation LBraceLoc, RBraceLoc; 1643 1644 /// HadDesignators - Return true if there were any designators in this 1645 /// init list expr. FIXME: this should be replaced by storing the designators 1646 /// somehow and updating codegen. 1647 bool HadDesignators; 1648public: 1649 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1650 SourceLocation rbraceloc, bool HadDesignators); 1651 1652 unsigned getNumInits() const { return InitExprs.size(); } 1653 bool hadDesignators() const { return HadDesignators; } 1654 1655 const Expr* getInit(unsigned Init) const { 1656 assert(Init < getNumInits() && "Initializer access out of range!"); 1657 return cast<Expr>(InitExprs[Init]); 1658 } 1659 1660 Expr* getInit(unsigned Init) { 1661 assert(Init < getNumInits() && "Initializer access out of range!"); 1662 return cast<Expr>(InitExprs[Init]); 1663 } 1664 1665 void setInit(unsigned Init, Expr *expr) { 1666 assert(Init < getNumInits() && "Initializer access out of range!"); 1667 InitExprs[Init] = expr; 1668 } 1669 1670 // Dynamic removal/addition (for constructing implicit InitExpr's). 1671 void removeInit(unsigned Init) { 1672 InitExprs.erase(InitExprs.begin()+Init); 1673 } 1674 void addInit(unsigned Init, Expr *expr) { 1675 InitExprs.insert(InitExprs.begin()+Init, expr); 1676 } 1677 1678 // Explicit InitListExpr's originate from source code (and have valid source 1679 // locations). Implicit InitListExpr's are created by the semantic analyzer. 1680 bool isExplicit() { 1681 return LBraceLoc.isValid() && RBraceLoc.isValid(); 1682 } 1683 1684 virtual SourceRange getSourceRange() const { 1685 return SourceRange(LBraceLoc, RBraceLoc); 1686 } 1687 static bool classof(const Stmt *T) { 1688 return T->getStmtClass() == InitListExprClass; 1689 } 1690 static bool classof(const InitListExpr *) { return true; } 1691 1692 // Iterators 1693 virtual child_iterator child_begin(); 1694 virtual child_iterator child_end(); 1695 1696 typedef std::vector<Stmt *>::iterator iterator; 1697 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 1698 1699 iterator begin() { return InitExprs.begin(); } 1700 iterator end() { return InitExprs.end(); } 1701 reverse_iterator rbegin() { return InitExprs.rbegin(); } 1702 reverse_iterator rend() { return InitExprs.rend(); } 1703 1704 // Serailization. 1705 virtual void EmitImpl(llvm::Serializer& S) const; 1706 static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1707 1708private: 1709 // Used by serializer. 1710 InitListExpr() : Expr(InitListExprClass, QualType()) {} 1711}; 1712 1713/// @brief Represents a C99 designated initializer expression. 1714/// 1715/// A designated initializer expression (C99 6.7.8) contains one or 1716/// more designators (which can be field designators, array 1717/// designators, or GNU array-range designators) followed by an 1718/// expression that initializes the field or element(s) that the 1719/// designators refer to. For example, given: 1720/// 1721/// @code 1722/// struct point { 1723/// double x; 1724/// double y; 1725/// }; 1726/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 1727/// @endcode 1728/// 1729/// The InitListExpr contains three DesignatedInitExprs, the first of 1730/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 1731/// designators, one array designator for @c [2] followed by one field 1732/// designator for @c .y. The initalization expression will be 1.0. 1733class DesignatedInitExpr : public Expr { 1734 /// The location of the '=' or ':' prior to the actual initializer 1735 /// expression. 1736 SourceLocation EqualOrColonLoc; 1737 1738 /// Whether this designated initializer used the GNU deprecated ':' 1739 /// syntax rather than the C99 '=' syntax. 1740 bool UsesColonSyntax : 1; 1741 1742 /// The number of designators in this initializer expression. 1743 unsigned NumDesignators : 15; 1744 1745 /// The number of subexpressions of this initializer expression, 1746 /// which contains both the initializer and any additional 1747 /// expressions used by array and array-range designators. 1748 unsigned NumSubExprs : 16; 1749 1750 DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 1751 SourceLocation EqualOrColonLoc, bool UsesColonSyntax, 1752 unsigned NumSubExprs) 1753 : Expr(DesignatedInitExprClass, Ty), 1754 EqualOrColonLoc(EqualOrColonLoc), UsesColonSyntax(UsesColonSyntax), 1755 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) { } 1756 1757public: 1758 /// A field designator, e.g., ".x". 1759 struct FieldDesignator { 1760 /// Refers to the field that is being initialized. The low bit 1761 /// of this field determines whether this is actually a pointer 1762 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 1763 /// initially constructed, a field designator will store an 1764 /// IdentifierInfo*. After semantic analysis has resolved that 1765 /// name, the field designator will instead store a FieldDecl*. 1766 uintptr_t NameOrField; 1767 1768 /// The location of the '.' in the designated initializer. 1769 unsigned DotLoc; 1770 1771 /// The location of the field name in the designated initializer. 1772 unsigned FieldLoc; 1773 }; 1774 1775 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 1776 struct ArrayOrRangeDesignator { 1777 /// Location of the first index expression within the designated 1778 /// initializer expression's list of subexpressions. 1779 unsigned Index; 1780 /// The location of the '[' starting the array range designator. 1781 unsigned LBracketLoc; 1782 /// The location of the ellipsis separating the start and end 1783 /// indices. Only valid for GNU array-range designators. 1784 unsigned EllipsisLoc; 1785 /// The location of the ']' terminating the array range designator. 1786 unsigned RBracketLoc; 1787 }; 1788 1789 /// @brief Represents a single C99 designator. 1790 /// 1791 /// @todo This class is infuriatingly similar to clang::Designator, 1792 /// but minor differences (storing indices vs. storing pointers) 1793 /// keep us from reusing it. Try harder, later, to rectify these 1794 /// differences. 1795 class Designator { 1796 /// @brief The kind of designator this describes. 1797 enum { 1798 FieldDesignator, 1799 ArrayDesignator, 1800 ArrayRangeDesignator 1801 } Kind; 1802 1803 union { 1804 /// A field designator, e.g., ".x". 1805 struct FieldDesignator Field; 1806 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 1807 struct ArrayOrRangeDesignator ArrayOrRange; 1808 }; 1809 friend class DesignatedInitExpr; 1810 1811 public: 1812 /// @brief Initializes a field designator. 1813 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 1814 SourceLocation FieldLoc) 1815 : Kind(FieldDesignator) { 1816 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 1817 Field.DotLoc = DotLoc.getRawEncoding(); 1818 Field.FieldLoc = FieldLoc.getRawEncoding(); 1819 } 1820 1821 /// @brief Initializes an array designator. 1822 Designator(unsigned Index, SourceLocation LBracketLoc, 1823 SourceLocation RBracketLoc) 1824 : Kind(ArrayDesignator) { 1825 ArrayOrRange.Index = Index; 1826 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 1827 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 1828 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 1829 } 1830 1831 /// @brief Initializes a GNU array-range designator. 1832 Designator(unsigned Index, SourceLocation LBracketLoc, 1833 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 1834 : Kind(ArrayRangeDesignator) { 1835 ArrayOrRange.Index = Index; 1836 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 1837 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 1838 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 1839 } 1840 1841 bool isFieldDesignator() const { return Kind == FieldDesignator; } 1842 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 1843 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 1844 1845 IdentifierInfo * getFieldName(); 1846 1847 FieldDecl *getField() { 1848 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1849 if (Field.NameOrField & 0x01) 1850 return 0; 1851 else 1852 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 1853 } 1854 1855 void setField(FieldDecl *FD) { 1856 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1857 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 1858 } 1859 1860 SourceLocation getDotLoc() const { 1861 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1862 return SourceLocation::getFromRawEncoding(Field.DotLoc); 1863 } 1864 1865 SourceLocation getFieldLoc() const { 1866 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1867 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 1868 } 1869 1870 SourceLocation getLBracketLoc() const { 1871 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 1872 "Only valid on an array or array-range designator"); 1873 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 1874 } 1875 1876 SourceLocation getRBracketLoc() const { 1877 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 1878 "Only valid on an array or array-range designator"); 1879 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 1880 } 1881 1882 SourceLocation getEllipsisLoc() const { 1883 assert(Kind == ArrayRangeDesignator && 1884 "Only valid on an array-range designator"); 1885 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 1886 } 1887 }; 1888 1889 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 1890 unsigned NumDesignators, 1891 Expr **IndexExprs, unsigned NumIndexExprs, 1892 SourceLocation EqualOrColonLoc, 1893 bool UsesColonSyntax, Expr *Init); 1894 1895 /// @brief Returns the number of designators in this initializer. 1896 unsigned size() const { return NumDesignators; } 1897 1898 // Iterator access to the designators. 1899 typedef Designator* designators_iterator; 1900 designators_iterator designators_begin(); 1901 designators_iterator designators_end(); 1902 1903 Expr *getArrayIndex(const Designator& D); 1904 Expr *getArrayRangeStart(const Designator& D); 1905 Expr *getArrayRangeEnd(const Designator& D); 1906 1907 /// @brief Retrieve the location of the '=' that precedes the 1908 /// initializer value itself, if present. 1909 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 1910 1911 /// @brief Determines whether this designated initializer used the 1912 /// GNU 'fieldname:' syntax or the C99 '=' syntax. 1913 bool usesColonSyntax() const { return UsesColonSyntax; } 1914 1915 /// @brief Retrieve the initializer value. 1916 Expr *getInit() const { 1917 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 1918 } 1919 1920 void setInit(Expr *init) { 1921 *child_begin() = init; 1922 } 1923 1924 virtual SourceRange getSourceRange() const; 1925 1926 static bool classof(const Stmt *T) { 1927 return T->getStmtClass() == DesignatedInitExprClass; 1928 } 1929 static bool classof(const DesignatedInitExpr *) { return true; } 1930 1931 // Iterators 1932 virtual child_iterator child_begin(); 1933 virtual child_iterator child_end(); 1934}; 1935 1936//===----------------------------------------------------------------------===// 1937// Clang Extensions 1938//===----------------------------------------------------------------------===// 1939 1940 1941/// ExtVectorElementExpr - This represents access to specific elements of a 1942/// vector, and may occur on the left hand side or right hand side. For example 1943/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 1944/// 1945class ExtVectorElementExpr : public Expr { 1946 Stmt *Base; 1947 IdentifierInfo &Accessor; 1948 SourceLocation AccessorLoc; 1949public: 1950 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 1951 SourceLocation loc) 1952 : Expr(ExtVectorElementExprClass, ty), 1953 Base(base), Accessor(accessor), AccessorLoc(loc) {} 1954 1955 const Expr *getBase() const { return cast<Expr>(Base); } 1956 Expr *getBase() { return cast<Expr>(Base); } 1957 1958 IdentifierInfo &getAccessor() const { return Accessor; } 1959 1960 /// getNumElements - Get the number of components being selected. 1961 unsigned getNumElements() const; 1962 1963 /// containsDuplicateElements - Return true if any element access is 1964 /// repeated. 1965 bool containsDuplicateElements() const; 1966 1967 /// getEncodedElementAccess - Encode the elements accessed into an llvm 1968 /// aggregate Constant of ConstantInt(s). 1969 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 1970 1971 virtual SourceRange getSourceRange() const { 1972 return SourceRange(getBase()->getLocStart(), AccessorLoc); 1973 } 1974 1975 static bool classof(const Stmt *T) { 1976 return T->getStmtClass() == ExtVectorElementExprClass; 1977 } 1978 static bool classof(const ExtVectorElementExpr *) { return true; } 1979 1980 // Iterators 1981 virtual child_iterator child_begin(); 1982 virtual child_iterator child_end(); 1983 1984 virtual void EmitImpl(llvm::Serializer& S) const; 1985 static ExtVectorElementExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1986}; 1987 1988 1989/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 1990/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 1991class BlockExpr : public Expr { 1992protected: 1993 BlockDecl *TheBlock; 1994public: 1995 BlockExpr(BlockDecl *BD, QualType ty) : Expr(BlockExprClass, ty), 1996 TheBlock(BD) {} 1997 1998 BlockDecl *getBlockDecl() { return TheBlock; } 1999 2000 // Convenience functions for probing the underlying BlockDecl. 2001 SourceLocation getCaretLocation() const; 2002 const Stmt *getBody() const; 2003 Stmt *getBody(); 2004 2005 virtual SourceRange getSourceRange() const { 2006 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 2007 } 2008 2009 /// getFunctionType - Return the underlying function type for this block. 2010 const FunctionType *getFunctionType() const; 2011 2012 static bool classof(const Stmt *T) { 2013 return T->getStmtClass() == BlockExprClass; 2014 } 2015 static bool classof(const BlockExpr *) { return true; } 2016 2017 // Iterators 2018 virtual child_iterator child_begin(); 2019 virtual child_iterator child_end(); 2020 2021 virtual void EmitImpl(llvm::Serializer& S) const; 2022 static BlockExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 2023}; 2024 2025/// BlockDeclRefExpr - A reference to a declared variable, function, 2026/// enum, etc. 2027class BlockDeclRefExpr : public Expr { 2028 ValueDecl *D; 2029 SourceLocation Loc; 2030 bool IsByRef; 2031public: 2032 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef) : 2033 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef) {} 2034 2035 ValueDecl *getDecl() { return D; } 2036 const ValueDecl *getDecl() const { return D; } 2037 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 2038 2039 bool isByRef() const { return IsByRef; } 2040 2041 static bool classof(const Stmt *T) { 2042 return T->getStmtClass() == BlockDeclRefExprClass; 2043 } 2044 static bool classof(const BlockDeclRefExpr *) { return true; } 2045 2046 // Iterators 2047 virtual child_iterator child_begin(); 2048 virtual child_iterator child_end(); 2049 2050 virtual void EmitImpl(llvm::Serializer& S) const; 2051 static BlockDeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 2052}; 2053 2054} // end namespace clang 2055 2056#endif 2057