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