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