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