Expr.h revision 53202857c60214d80950a975e6e52aebf30bd16a
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(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 1045 1046 /// \brief Build an empty member reference expression. 1047 explicit MemberExpr(EmptyShell Empty) : Expr(MemberExprClass, Empty) { } 1048 1049 void setBase(Expr *E) { Base = E; } 1050 Expr *getBase() const { return cast<Expr>(Base); } 1051 1052 /// \brief Retrieve the member declaration to which this expression refers. 1053 /// 1054 /// The returned declaration will either be a FieldDecl or (in C++) 1055 /// a CXXMethodDecl. 1056 NamedDecl *getMemberDecl() const { return MemberDecl; } 1057 void setMemberDecl(NamedDecl *D) { MemberDecl = D; } 1058 1059 bool isArrow() const { return IsArrow; } 1060 void setArrow(bool A) { IsArrow = A; } 1061 1062 /// getMemberLoc - Return the location of the "member", in X->F, it is the 1063 /// location of 'F'. 1064 SourceLocation getMemberLoc() const { return MemberLoc; } 1065 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1066 1067 virtual SourceRange getSourceRange() const { 1068 // If we have an implicit base (like a C++ implicit this), 1069 // make sure not to return its location 1070 SourceLocation BaseLoc = getBase()->getLocStart(); 1071 if (BaseLoc.isInvalid()) 1072 return SourceRange(MemberLoc, MemberLoc); 1073 return SourceRange(BaseLoc, MemberLoc); 1074 } 1075 1076 virtual SourceLocation getExprLoc() const { return MemberLoc; } 1077 1078 static bool classof(const Stmt *T) { 1079 return T->getStmtClass() == MemberExprClass; 1080 } 1081 static bool classof(const MemberExpr *) { return true; } 1082 1083 // Iterators 1084 virtual child_iterator child_begin(); 1085 virtual child_iterator child_end(); 1086}; 1087 1088/// CompoundLiteralExpr - [C99 6.5.2.5] 1089/// 1090class CompoundLiteralExpr : public Expr { 1091 /// LParenLoc - If non-null, this is the location of the left paren in a 1092 /// compound literal like "(int){4}". This can be null if this is a 1093 /// synthesized compound expression. 1094 SourceLocation LParenLoc; 1095 Stmt *Init; 1096 bool FileScope; 1097public: 1098 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, 1099 bool fileScope) 1100 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), 1101 FileScope(fileScope) {} 1102 1103 /// \brief Construct an empty compound literal. 1104 explicit CompoundLiteralExpr(EmptyShell Empty) 1105 : Expr(CompoundLiteralExprClass, Empty) { } 1106 1107 const Expr *getInitializer() const { return cast<Expr>(Init); } 1108 Expr *getInitializer() { return cast<Expr>(Init); } 1109 void setInitializer(Expr *E) { Init = E; } 1110 1111 bool isFileScope() const { return FileScope; } 1112 void setFileScope(bool FS) { FileScope = FS; } 1113 1114 SourceLocation getLParenLoc() const { return LParenLoc; } 1115 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1116 1117 virtual SourceRange getSourceRange() const { 1118 // FIXME: Init should never be null. 1119 if (!Init) 1120 return SourceRange(); 1121 if (LParenLoc.isInvalid()) 1122 return Init->getSourceRange(); 1123 return SourceRange(LParenLoc, Init->getLocEnd()); 1124 } 1125 1126 static bool classof(const Stmt *T) { 1127 return T->getStmtClass() == CompoundLiteralExprClass; 1128 } 1129 static bool classof(const CompoundLiteralExpr *) { return true; } 1130 1131 // Iterators 1132 virtual child_iterator child_begin(); 1133 virtual child_iterator child_end(); 1134}; 1135 1136/// CastExpr - Base class for type casts, including both implicit 1137/// casts (ImplicitCastExpr) and explicit casts that have some 1138/// representation in the source code (ExplicitCastExpr's derived 1139/// classes). 1140class CastExpr : public Expr { 1141 Stmt *Op; 1142protected: 1143 CastExpr(StmtClass SC, QualType ty, Expr *op) : 1144 Expr(SC, ty, 1145 // Cast expressions are type-dependent if the type is 1146 // dependent (C++ [temp.dep.expr]p3). 1147 ty->isDependentType(), 1148 // Cast expressions are value-dependent if the type is 1149 // dependent or if the subexpression is value-dependent. 1150 ty->isDependentType() || (op && op->isValueDependent())), 1151 Op(op) {} 1152 1153 /// \brief Construct an empty cast. 1154 CastExpr(StmtClass SC, EmptyShell Empty) 1155 : Expr(SC, Empty) { } 1156 1157public: 1158 Expr *getSubExpr() { return cast<Expr>(Op); } 1159 const Expr *getSubExpr() const { return cast<Expr>(Op); } 1160 void setSubExpr(Expr *E) { Op = E; } 1161 1162 static bool classof(const Stmt *T) { 1163 StmtClass SC = T->getStmtClass(); 1164 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1165 return true; 1166 1167 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 1168 return true; 1169 1170 return false; 1171 } 1172 static bool classof(const CastExpr *) { return true; } 1173 1174 // Iterators 1175 virtual child_iterator child_begin(); 1176 virtual child_iterator child_end(); 1177}; 1178 1179/// ImplicitCastExpr - Allows us to explicitly represent implicit type 1180/// conversions, which have no direct representation in the original 1181/// source code. For example: converting T[]->T*, void f()->void 1182/// (*f)(), float->double, short->int, etc. 1183/// 1184/// In C, implicit casts always produce rvalues. However, in C++, an 1185/// implicit cast whose result is being bound to a reference will be 1186/// an lvalue. For example: 1187/// 1188/// @code 1189/// class Base { }; 1190/// class Derived : public Base { }; 1191/// void f(Derived d) { 1192/// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base 1193/// } 1194/// @endcode 1195class ImplicitCastExpr : public CastExpr { 1196 /// LvalueCast - Whether this cast produces an lvalue. 1197 bool LvalueCast; 1198 1199public: 1200 ImplicitCastExpr(QualType ty, Expr *op, bool Lvalue) : 1201 CastExpr(ImplicitCastExprClass, ty, op), LvalueCast(Lvalue) { } 1202 1203 /// \brief Construct an empty implicit cast. 1204 explicit ImplicitCastExpr(EmptyShell Shell) 1205 : CastExpr(ImplicitCastExprClass, Shell) { } 1206 1207 1208 virtual SourceRange getSourceRange() const { 1209 return getSubExpr()->getSourceRange(); 1210 } 1211 1212 /// isLvalueCast - Whether this cast produces an lvalue. 1213 bool isLvalueCast() const { return LvalueCast; } 1214 1215 /// setLvalueCast - Set whether this cast produces an lvalue. 1216 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 1217 1218 static bool classof(const Stmt *T) { 1219 return T->getStmtClass() == ImplicitCastExprClass; 1220 } 1221 static bool classof(const ImplicitCastExpr *) { return true; } 1222}; 1223 1224/// ExplicitCastExpr - An explicit cast written in the source 1225/// code. 1226/// 1227/// This class is effectively an abstract class, because it provides 1228/// the basic representation of an explicitly-written cast without 1229/// specifying which kind of cast (C cast, functional cast, static 1230/// cast, etc.) was written; specific derived classes represent the 1231/// particular style of cast and its location information. 1232/// 1233/// Unlike implicit casts, explicit cast nodes have two different 1234/// types: the type that was written into the source code, and the 1235/// actual type of the expression as determined by semantic 1236/// analysis. These types may differ slightly. For example, in C++ one 1237/// can cast to a reference type, which indicates that the resulting 1238/// expression will be an lvalue. The reference type, however, will 1239/// not be used as the type of the expression. 1240class ExplicitCastExpr : public CastExpr { 1241 /// TypeAsWritten - The type that this expression is casting to, as 1242 /// written in the source code. 1243 QualType TypeAsWritten; 1244 1245protected: 1246 ExplicitCastExpr(StmtClass SC, QualType exprTy, Expr *op, QualType writtenTy) 1247 : CastExpr(SC, exprTy, op), TypeAsWritten(writtenTy) {} 1248 1249 /// \brief Construct an empty explicit cast. 1250 ExplicitCastExpr(StmtClass SC, EmptyShell Shell) 1251 : CastExpr(SC, Shell) { } 1252 1253public: 1254 /// getTypeAsWritten - Returns the type that this expression is 1255 /// casting to, as written in the source code. 1256 QualType getTypeAsWritten() const { return TypeAsWritten; } 1257 void setTypeAsWritten(QualType T) { TypeAsWritten = T; } 1258 1259 static bool classof(const Stmt *T) { 1260 StmtClass SC = T->getStmtClass(); 1261 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass) 1262 return true; 1263 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass) 1264 return true; 1265 1266 return false; 1267 } 1268 static bool classof(const ExplicitCastExpr *) { return true; } 1269}; 1270 1271/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 1272/// cast in C++ (C++ [expr.cast]), which uses the syntax 1273/// (Type)expr. For example: @c (int)f. 1274class CStyleCastExpr : public ExplicitCastExpr { 1275 SourceLocation LPLoc; // the location of the left paren 1276 SourceLocation RPLoc; // the location of the right paren 1277public: 1278 CStyleCastExpr(QualType exprTy, Expr *op, QualType writtenTy, 1279 SourceLocation l, SourceLocation r) : 1280 ExplicitCastExpr(CStyleCastExprClass, exprTy, op, writtenTy), 1281 LPLoc(l), RPLoc(r) {} 1282 1283 /// \brief Construct an empty C-style explicit cast. 1284 explicit CStyleCastExpr(EmptyShell Shell) 1285 : ExplicitCastExpr(CStyleCastExprClass, Shell) { } 1286 1287 SourceLocation getLParenLoc() const { return LPLoc; } 1288 void setLParenLoc(SourceLocation L) { LPLoc = L; } 1289 1290 SourceLocation getRParenLoc() const { return RPLoc; } 1291 void setRParenLoc(SourceLocation L) { RPLoc = L; } 1292 1293 virtual SourceRange getSourceRange() const { 1294 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 1295 } 1296 static bool classof(const Stmt *T) { 1297 return T->getStmtClass() == CStyleCastExprClass; 1298 } 1299 static bool classof(const CStyleCastExpr *) { return true; } 1300}; 1301 1302/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 1303/// 1304/// This expression node kind describes a builtin binary operation, 1305/// such as "x + y" for integer values "x" and "y". The operands will 1306/// already have been converted to appropriate types (e.g., by 1307/// performing promotions or conversions). 1308/// 1309/// In C++, where operators may be overloaded, a different kind of 1310/// expression node (CXXOperatorCallExpr) is used to express the 1311/// invocation of an overloaded operator with operator syntax. Within 1312/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 1313/// used to store an expression "x + y" depends on the subexpressions 1314/// for x and y. If neither x or y is type-dependent, and the "+" 1315/// operator resolves to a built-in operation, BinaryOperator will be 1316/// used to express the computation (x and y may still be 1317/// value-dependent). If either x or y is type-dependent, or if the 1318/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 1319/// be used to express the computation. 1320class BinaryOperator : public Expr { 1321public: 1322 enum Opcode { 1323 // Operators listed in order of precedence. 1324 // Note that additions to this should also update the StmtVisitor class. 1325 PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators. 1326 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 1327 Add, Sub, // [C99 6.5.6] Additive operators. 1328 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 1329 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 1330 EQ, NE, // [C99 6.5.9] Equality operators. 1331 And, // [C99 6.5.10] Bitwise AND operator. 1332 Xor, // [C99 6.5.11] Bitwise XOR operator. 1333 Or, // [C99 6.5.12] Bitwise OR operator. 1334 LAnd, // [C99 6.5.13] Logical AND operator. 1335 LOr, // [C99 6.5.14] Logical OR operator. 1336 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 1337 DivAssign, RemAssign, 1338 AddAssign, SubAssign, 1339 ShlAssign, ShrAssign, 1340 AndAssign, XorAssign, 1341 OrAssign, 1342 Comma // [C99 6.5.17] Comma operator. 1343 }; 1344private: 1345 enum { LHS, RHS, END_EXPR }; 1346 Stmt* SubExprs[END_EXPR]; 1347 Opcode Opc; 1348 SourceLocation OpLoc; 1349public: 1350 1351 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1352 SourceLocation opLoc) 1353 : Expr(BinaryOperatorClass, ResTy, 1354 lhs->isTypeDependent() || rhs->isTypeDependent(), 1355 lhs->isValueDependent() || rhs->isValueDependent()), 1356 Opc(opc), OpLoc(opLoc) { 1357 SubExprs[LHS] = lhs; 1358 SubExprs[RHS] = rhs; 1359 assert(!isCompoundAssignmentOp() && 1360 "Use ArithAssignBinaryOperator for compound assignments"); 1361 } 1362 1363 /// \brief Construct an empty binary operator. 1364 explicit BinaryOperator(EmptyShell Empty) 1365 : Expr(BinaryOperatorClass, Empty), Opc(Comma) { } 1366 1367 SourceLocation getOperatorLoc() const { return OpLoc; } 1368 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1369 1370 Opcode getOpcode() const { return Opc; } 1371 void setOpcode(Opcode O) { Opc = O; } 1372 1373 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1374 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1375 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1376 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1377 1378 virtual SourceRange getSourceRange() const { 1379 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 1380 } 1381 1382 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1383 /// corresponds to, e.g. "<<=". 1384 static const char *getOpcodeStr(Opcode Op); 1385 1386 /// \brief Retrieve the binary opcode that corresponds to the given 1387 /// overloaded operator. 1388 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 1389 1390 /// \brief Retrieve the overloaded operator kind that corresponds to 1391 /// the given binary opcode. 1392 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 1393 1394 /// predicates to categorize the respective opcodes. 1395 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 1396 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 1397 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 1398 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 1399 1400 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 1401 bool isRelationalOp() const { return isRelationalOp(Opc); } 1402 1403 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 1404 bool isEqualityOp() const { return isEqualityOp(Opc); } 1405 1406 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 1407 bool isLogicalOp() const { return isLogicalOp(Opc); } 1408 1409 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 1410 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 1411 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 1412 1413 static bool classof(const Stmt *S) { 1414 return S->getStmtClass() == BinaryOperatorClass || 1415 S->getStmtClass() == CompoundAssignOperatorClass; 1416 } 1417 static bool classof(const BinaryOperator *) { return true; } 1418 1419 // Iterators 1420 virtual child_iterator child_begin(); 1421 virtual child_iterator child_end(); 1422 1423protected: 1424 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 1425 SourceLocation oploc, bool dead) 1426 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) { 1427 SubExprs[LHS] = lhs; 1428 SubExprs[RHS] = rhs; 1429 } 1430 1431 BinaryOperator(StmtClass SC, EmptyShell Empty) 1432 : Expr(SC, Empty), Opc(MulAssign) { } 1433}; 1434 1435/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 1436/// track of the type the operation is performed in. Due to the semantics of 1437/// these operators, the operands are promoted, the aritmetic performed, an 1438/// implicit conversion back to the result type done, then the assignment takes 1439/// place. This captures the intermediate type which the computation is done 1440/// in. 1441class CompoundAssignOperator : public BinaryOperator { 1442 QualType ComputationLHSType; 1443 QualType ComputationResultType; 1444public: 1445 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1446 QualType ResType, QualType CompLHSType, 1447 QualType CompResultType, 1448 SourceLocation OpLoc) 1449 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1450 ComputationLHSType(CompLHSType), 1451 ComputationResultType(CompResultType) { 1452 assert(isCompoundAssignmentOp() && 1453 "Only should be used for compound assignments"); 1454 } 1455 1456 /// \brief Build an empty compound assignment operator expression. 1457 explicit CompoundAssignOperator(EmptyShell Empty) 1458 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 1459 1460 // The two computation types are the type the LHS is converted 1461 // to for the computation and the type of the result; the two are 1462 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 1463 QualType getComputationLHSType() const { return ComputationLHSType; } 1464 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 1465 1466 QualType getComputationResultType() const { return ComputationResultType; } 1467 void setComputationResultType(QualType T) { ComputationResultType = T; } 1468 1469 static bool classof(const CompoundAssignOperator *) { return true; } 1470 static bool classof(const Stmt *S) { 1471 return S->getStmtClass() == CompoundAssignOperatorClass; 1472 } 1473}; 1474 1475/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1476/// GNU "missing LHS" extension is in use. 1477/// 1478class ConditionalOperator : public Expr { 1479 enum { COND, LHS, RHS, END_EXPR }; 1480 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1481public: 1482 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 1483 : Expr(ConditionalOperatorClass, t, 1484 // FIXME: the type of the conditional operator doesn't 1485 // depend on the type of the conditional, but the standard 1486 // seems to imply that it could. File a bug! 1487 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 1488 (cond->isValueDependent() || 1489 (lhs && lhs->isValueDependent()) || 1490 (rhs && rhs->isValueDependent()))) { 1491 SubExprs[COND] = cond; 1492 SubExprs[LHS] = lhs; 1493 SubExprs[RHS] = rhs; 1494 } 1495 1496 /// \brief Build an empty conditional operator. 1497 explicit ConditionalOperator(EmptyShell Empty) 1498 : Expr(ConditionalOperatorClass, Empty) { } 1499 1500 // getCond - Return the expression representing the condition for 1501 // the ?: operator. 1502 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1503 void setCond(Expr *E) { SubExprs[COND] = E; } 1504 1505 // getTrueExpr - Return the subexpression representing the value of the ?: 1506 // expression if the condition evaluates to true. In most cases this value 1507 // will be the same as getLHS() except a GCC extension allows the left 1508 // subexpression to be omitted, and instead of the condition be returned. 1509 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1510 // is only evaluated once. 1511 Expr *getTrueExpr() const { 1512 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1513 } 1514 1515 // getTrueExpr - Return the subexpression representing the value of the ?: 1516 // expression if the condition evaluates to false. This is the same as getRHS. 1517 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1518 1519 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1520 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1521 1522 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1523 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1524 1525 virtual SourceRange getSourceRange() const { 1526 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1527 } 1528 static bool classof(const Stmt *T) { 1529 return T->getStmtClass() == ConditionalOperatorClass; 1530 } 1531 static bool classof(const ConditionalOperator *) { return true; } 1532 1533 // Iterators 1534 virtual child_iterator child_begin(); 1535 virtual child_iterator child_end(); 1536}; 1537 1538/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1539class AddrLabelExpr : public Expr { 1540 SourceLocation AmpAmpLoc, LabelLoc; 1541 LabelStmt *Label; 1542public: 1543 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 1544 QualType t) 1545 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 1546 1547 /// \brief Build an empty address of a label expression. 1548 explicit AddrLabelExpr(EmptyShell Empty) 1549 : Expr(AddrLabelExprClass, Empty) { } 1550 1551 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 1552 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 1553 SourceLocation getLabelLoc() const { return LabelLoc; } 1554 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 1555 1556 virtual SourceRange getSourceRange() const { 1557 return SourceRange(AmpAmpLoc, LabelLoc); 1558 } 1559 1560 LabelStmt *getLabel() const { return Label; } 1561 void setLabel(LabelStmt *S) { Label = S; } 1562 1563 static bool classof(const Stmt *T) { 1564 return T->getStmtClass() == AddrLabelExprClass; 1565 } 1566 static bool classof(const AddrLabelExpr *) { return true; } 1567 1568 // Iterators 1569 virtual child_iterator child_begin(); 1570 virtual child_iterator child_end(); 1571}; 1572 1573/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 1574/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 1575/// takes the value of the last subexpression. 1576class StmtExpr : public Expr { 1577 Stmt *SubStmt; 1578 SourceLocation LParenLoc, RParenLoc; 1579public: 1580 StmtExpr(CompoundStmt *substmt, QualType T, 1581 SourceLocation lp, SourceLocation rp) : 1582 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 1583 1584 /// \brief Build an empty statement expression. 1585 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 1586 1587 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 1588 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 1589 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 1590 1591 virtual SourceRange getSourceRange() const { 1592 return SourceRange(LParenLoc, RParenLoc); 1593 } 1594 1595 SourceLocation getLParenLoc() const { return LParenLoc; } 1596 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1597 SourceLocation getRParenLoc() const { return RParenLoc; } 1598 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1599 1600 static bool classof(const Stmt *T) { 1601 return T->getStmtClass() == StmtExprClass; 1602 } 1603 static bool classof(const StmtExpr *) { return true; } 1604 1605 // Iterators 1606 virtual child_iterator child_begin(); 1607 virtual child_iterator child_end(); 1608}; 1609 1610/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 1611/// This AST node represents a function that returns 1 if two *types* (not 1612/// expressions) are compatible. The result of this built-in function can be 1613/// used in integer constant expressions. 1614class TypesCompatibleExpr : public Expr { 1615 QualType Type1; 1616 QualType Type2; 1617 SourceLocation BuiltinLoc, RParenLoc; 1618public: 1619 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1620 QualType t1, QualType t2, SourceLocation RP) : 1621 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1622 BuiltinLoc(BLoc), RParenLoc(RP) {} 1623 1624 /// \brief Build an empty __builtin_type_compatible_p expression. 1625 explicit TypesCompatibleExpr(EmptyShell Empty) 1626 : Expr(TypesCompatibleExprClass, Empty) { } 1627 1628 QualType getArgType1() const { return Type1; } 1629 void setArgType1(QualType T) { Type1 = T; } 1630 QualType getArgType2() const { return Type2; } 1631 void setArgType2(QualType T) { Type2 = T; } 1632 1633 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 1634 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 1635 1636 SourceLocation getRParenLoc() const { return RParenLoc; } 1637 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1638 1639 virtual SourceRange getSourceRange() const { 1640 return SourceRange(BuiltinLoc, RParenLoc); 1641 } 1642 static bool classof(const Stmt *T) { 1643 return T->getStmtClass() == TypesCompatibleExprClass; 1644 } 1645 static bool classof(const TypesCompatibleExpr *) { return true; } 1646 1647 // Iterators 1648 virtual child_iterator child_begin(); 1649 virtual child_iterator child_end(); 1650}; 1651 1652/// ShuffleVectorExpr - clang-specific builtin-in function 1653/// __builtin_shufflevector. 1654/// This AST node represents a operator that does a constant 1655/// shuffle, similar to LLVM's shufflevector instruction. It takes 1656/// two vectors and a variable number of constant indices, 1657/// and returns the appropriately shuffled vector. 1658class ShuffleVectorExpr : public Expr { 1659 SourceLocation BuiltinLoc, RParenLoc; 1660 1661 // SubExprs - the list of values passed to the __builtin_shufflevector 1662 // function. The first two are vectors, and the rest are constant 1663 // indices. The number of values in this list is always 1664 // 2+the number of indices in the vector type. 1665 Stmt **SubExprs; 1666 unsigned NumExprs; 1667 1668public: 1669 ShuffleVectorExpr(Expr **args, unsigned nexpr, 1670 QualType Type, SourceLocation BLoc, 1671 SourceLocation RP) : 1672 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 1673 RParenLoc(RP), NumExprs(nexpr) { 1674 1675 SubExprs = new Stmt*[nexpr]; 1676 for (unsigned i = 0; i < nexpr; i++) 1677 SubExprs[i] = args[i]; 1678 } 1679 1680 /// \brief Build an empty vector-shuffle expression. 1681 explicit ShuffleVectorExpr(EmptyShell Empty) 1682 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 1683 1684 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 1685 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 1686 1687 SourceLocation getRParenLoc() const { return RParenLoc; } 1688 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1689 1690 virtual SourceRange getSourceRange() const { 1691 return SourceRange(BuiltinLoc, RParenLoc); 1692 } 1693 static bool classof(const Stmt *T) { 1694 return T->getStmtClass() == ShuffleVectorExprClass; 1695 } 1696 static bool classof(const ShuffleVectorExpr *) { return true; } 1697 1698 ~ShuffleVectorExpr() { 1699 delete [] SubExprs; 1700 } 1701 1702 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1703 /// constant expression, the actual arguments passed in, and the function 1704 /// pointers. 1705 unsigned getNumSubExprs() const { return NumExprs; } 1706 1707 /// getExpr - Return the Expr at the specified index. 1708 Expr *getExpr(unsigned Index) { 1709 assert((Index < NumExprs) && "Arg access out of range!"); 1710 return cast<Expr>(SubExprs[Index]); 1711 } 1712 const Expr *getExpr(unsigned Index) const { 1713 assert((Index < NumExprs) && "Arg access out of range!"); 1714 return cast<Expr>(SubExprs[Index]); 1715 } 1716 1717 void setExprs(Expr ** Exprs, unsigned NumExprs); 1718 1719 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 1720 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 1721 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue(); 1722 } 1723 1724 // Iterators 1725 virtual child_iterator child_begin(); 1726 virtual child_iterator child_end(); 1727}; 1728 1729/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 1730/// This AST node is similar to the conditional operator (?:) in C, with 1731/// the following exceptions: 1732/// - the test expression must be a integer constant expression. 1733/// - the expression returned acts like the chosen subexpression in every 1734/// visible way: the type is the same as that of the chosen subexpression, 1735/// and all predicates (whether it's an l-value, whether it's an integer 1736/// constant expression, etc.) return the same result as for the chosen 1737/// sub-expression. 1738class ChooseExpr : public Expr { 1739 enum { COND, LHS, RHS, END_EXPR }; 1740 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1741 SourceLocation BuiltinLoc, RParenLoc; 1742public: 1743 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 1744 SourceLocation RP) 1745 : Expr(ChooseExprClass, t), 1746 BuiltinLoc(BLoc), RParenLoc(RP) { 1747 SubExprs[COND] = cond; 1748 SubExprs[LHS] = lhs; 1749 SubExprs[RHS] = rhs; 1750 } 1751 1752 /// \brief Build an empty __builtin_choose_expr. 1753 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 1754 1755 /// isConditionTrue - Return whether the condition is true (i.e. not 1756 /// equal to zero). 1757 bool isConditionTrue(ASTContext &C) const; 1758 1759 /// getChosenSubExpr - Return the subexpression chosen according to the 1760 /// condition. 1761 Expr *getChosenSubExpr(ASTContext &C) const { 1762 return isConditionTrue(C) ? getLHS() : getRHS(); 1763 } 1764 1765 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1766 void setCond(Expr *E) { SubExprs[COND] = E; } 1767 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1768 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1769 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1770 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1771 1772 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 1773 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 1774 1775 SourceLocation getRParenLoc() const { return RParenLoc; } 1776 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1777 1778 virtual SourceRange getSourceRange() const { 1779 return SourceRange(BuiltinLoc, RParenLoc); 1780 } 1781 static bool classof(const Stmt *T) { 1782 return T->getStmtClass() == ChooseExprClass; 1783 } 1784 static bool classof(const ChooseExpr *) { return true; } 1785 1786 // Iterators 1787 virtual child_iterator child_begin(); 1788 virtual child_iterator child_end(); 1789}; 1790 1791/// GNUNullExpr - Implements the GNU __null extension, which is a name 1792/// for a null pointer constant that has integral type (e.g., int or 1793/// long) and is the same size and alignment as a pointer. The __null 1794/// extension is typically only used by system headers, which define 1795/// NULL as __null in C++ rather than using 0 (which is an integer 1796/// that may not match the size of a pointer). 1797class GNUNullExpr : public Expr { 1798 /// TokenLoc - The location of the __null keyword. 1799 SourceLocation TokenLoc; 1800 1801public: 1802 GNUNullExpr(QualType Ty, SourceLocation Loc) 1803 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { } 1804 1805 /// \brief Build an empty GNU __null expression. 1806 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 1807 1808 /// getTokenLocation - The location of the __null token. 1809 SourceLocation getTokenLocation() const { return TokenLoc; } 1810 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 1811 1812 virtual SourceRange getSourceRange() const { 1813 return SourceRange(TokenLoc); 1814 } 1815 static bool classof(const Stmt *T) { 1816 return T->getStmtClass() == GNUNullExprClass; 1817 } 1818 static bool classof(const GNUNullExpr *) { return true; } 1819 1820 // Iterators 1821 virtual child_iterator child_begin(); 1822 virtual child_iterator child_end(); 1823}; 1824 1825/// VAArgExpr, used for the builtin function __builtin_va_start. 1826class VAArgExpr : public Expr { 1827 Stmt *Val; 1828 SourceLocation BuiltinLoc, RParenLoc; 1829public: 1830 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 1831 : Expr(VAArgExprClass, t), 1832 Val(e), 1833 BuiltinLoc(BLoc), 1834 RParenLoc(RPLoc) { } 1835 1836 /// \brief Create an empty __builtin_va_start expression. 1837 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 1838 1839 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1840 Expr *getSubExpr() { return cast<Expr>(Val); } 1841 void setSubExpr(Expr *E) { Val = E; } 1842 1843 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 1844 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 1845 1846 SourceLocation getRParenLoc() const { return RParenLoc; } 1847 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1848 1849 virtual SourceRange getSourceRange() const { 1850 return SourceRange(BuiltinLoc, RParenLoc); 1851 } 1852 static bool classof(const Stmt *T) { 1853 return T->getStmtClass() == VAArgExprClass; 1854 } 1855 static bool classof(const VAArgExpr *) { return true; } 1856 1857 // Iterators 1858 virtual child_iterator child_begin(); 1859 virtual child_iterator child_end(); 1860}; 1861 1862/// @brief Describes an C or C++ initializer list. 1863/// 1864/// InitListExpr describes an initializer list, which can be used to 1865/// initialize objects of different types, including 1866/// struct/class/union types, arrays, and vectors. For example: 1867/// 1868/// @code 1869/// struct foo x = { 1, { 2, 3 } }; 1870/// @endcode 1871/// 1872/// Prior to semantic analysis, an initializer list will represent the 1873/// initializer list as written by the user, but will have the 1874/// placeholder type "void". This initializer list is called the 1875/// syntactic form of the initializer, and may contain C99 designated 1876/// initializers (represented as DesignatedInitExprs), initializations 1877/// of subobject members without explicit braces, and so on. Clients 1878/// interested in the original syntax of the initializer list should 1879/// use the syntactic form of the initializer list. 1880/// 1881/// After semantic analysis, the initializer list will represent the 1882/// semantic form of the initializer, where the initializations of all 1883/// subobjects are made explicit with nested InitListExpr nodes and 1884/// C99 designators have been eliminated by placing the designated 1885/// initializations into the subobject they initialize. Additionally, 1886/// any "holes" in the initialization, where no initializer has been 1887/// specified for a particular subobject, will be replaced with 1888/// implicitly-generated ImplicitValueInitExpr expressions that 1889/// value-initialize the subobjects. Note, however, that the 1890/// initializer lists may still have fewer initializers than there are 1891/// elements to initialize within the object. 1892/// 1893/// Given the semantic form of the initializer list, one can retrieve 1894/// the original syntactic form of that initializer list (if it 1895/// exists) using getSyntacticForm(). Since many initializer lists 1896/// have the same syntactic and semantic forms, getSyntacticForm() may 1897/// return NULL, indicating that the current initializer list also 1898/// serves as its syntactic form. 1899class InitListExpr : public Expr { 1900 std::vector<Stmt *> InitExprs; 1901 SourceLocation LBraceLoc, RBraceLoc; 1902 1903 /// Contains the initializer list that describes the syntactic form 1904 /// written in the source code. 1905 InitListExpr *SyntacticForm; 1906 1907 /// If this initializer list initializes a union, specifies which 1908 /// field within the union will be initialized. 1909 FieldDecl *UnionFieldInit; 1910 1911 /// Whether this initializer list originally had a GNU array-range 1912 /// designator in it. This is a temporary marker used by CodeGen. 1913 bool HadArrayRangeDesignator; 1914 1915public: 1916 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1917 SourceLocation rbraceloc); 1918 1919 /// \brief Build an empty initializer list. 1920 explicit InitListExpr(EmptyShell Empty) : Expr(InitListExprClass, Empty) { } 1921 1922 unsigned getNumInits() const { return InitExprs.size(); } 1923 1924 const Expr* getInit(unsigned Init) const { 1925 assert(Init < getNumInits() && "Initializer access out of range!"); 1926 return cast_or_null<Expr>(InitExprs[Init]); 1927 } 1928 1929 Expr* getInit(unsigned Init) { 1930 assert(Init < getNumInits() && "Initializer access out of range!"); 1931 return cast_or_null<Expr>(InitExprs[Init]); 1932 } 1933 1934 void setInit(unsigned Init, Expr *expr) { 1935 assert(Init < getNumInits() && "Initializer access out of range!"); 1936 InitExprs[Init] = expr; 1937 } 1938 1939 /// \brief Reserve space for some number of initializers. 1940 void reserveInits(unsigned NumInits); 1941 1942 /// @brief Specify the number of initializers 1943 /// 1944 /// If there are more than @p NumInits initializers, the remaining 1945 /// initializers will be destroyed. If there are fewer than @p 1946 /// NumInits initializers, NULL expressions will be added for the 1947 /// unknown initializers. 1948 void resizeInits(ASTContext &Context, unsigned NumInits); 1949 1950 /// @brief Updates the initializer at index @p Init with the new 1951 /// expression @p expr, and returns the old expression at that 1952 /// location. 1953 /// 1954 /// When @p Init is out of range for this initializer list, the 1955 /// initializer list will be extended with NULL expressions to 1956 /// accomodate the new entry. 1957 Expr *updateInit(unsigned Init, Expr *expr); 1958 1959 /// \brief If this initializes a union, specifies which field in the 1960 /// union to initialize. 1961 /// 1962 /// Typically, this field is the first named field within the 1963 /// union. However, a designated initializer can specify the 1964 /// initialization of a different field within the union. 1965 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; } 1966 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; } 1967 1968 // Explicit InitListExpr's originate from source code (and have valid source 1969 // locations). Implicit InitListExpr's are created by the semantic analyzer. 1970 bool isExplicit() { 1971 return LBraceLoc.isValid() && RBraceLoc.isValid(); 1972 } 1973 1974 SourceLocation getLBraceLoc() const { return LBraceLoc; } 1975 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 1976 SourceLocation getRBraceLoc() const { return RBraceLoc; } 1977 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 1978 1979 /// @brief Retrieve the initializer list that describes the 1980 /// syntactic form of the initializer. 1981 /// 1982 /// 1983 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 1984 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 1985 1986 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 1987 void sawArrayRangeDesignator(bool ARD = true) { 1988 HadArrayRangeDesignator = ARD; 1989 } 1990 1991 virtual SourceRange getSourceRange() const { 1992 return SourceRange(LBraceLoc, RBraceLoc); 1993 } 1994 static bool classof(const Stmt *T) { 1995 return T->getStmtClass() == InitListExprClass; 1996 } 1997 static bool classof(const InitListExpr *) { return true; } 1998 1999 // Iterators 2000 virtual child_iterator child_begin(); 2001 virtual child_iterator child_end(); 2002 2003 typedef std::vector<Stmt *>::iterator iterator; 2004 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 2005 2006 iterator begin() { return InitExprs.begin(); } 2007 iterator end() { return InitExprs.end(); } 2008 reverse_iterator rbegin() { return InitExprs.rbegin(); } 2009 reverse_iterator rend() { return InitExprs.rend(); } 2010}; 2011 2012/// @brief Represents a C99 designated initializer expression. 2013/// 2014/// A designated initializer expression (C99 6.7.8) contains one or 2015/// more designators (which can be field designators, array 2016/// designators, or GNU array-range designators) followed by an 2017/// expression that initializes the field or element(s) that the 2018/// designators refer to. For example, given: 2019/// 2020/// @code 2021/// struct point { 2022/// double x; 2023/// double y; 2024/// }; 2025/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 2026/// @endcode 2027/// 2028/// The InitListExpr contains three DesignatedInitExprs, the first of 2029/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 2030/// designators, one array designator for @c [2] followed by one field 2031/// designator for @c .y. The initalization expression will be 1.0. 2032class DesignatedInitExpr : public Expr { 2033public: 2034 /// \brief Forward declaration of the Designator class. 2035 class Designator; 2036 2037private: 2038 /// The location of the '=' or ':' prior to the actual initializer 2039 /// expression. 2040 SourceLocation EqualOrColonLoc; 2041 2042 /// Whether this designated initializer used the GNU deprecated 2043 /// syntax rather than the C99 '=' syntax. 2044 bool GNUSyntax : 1; 2045 2046 /// The number of designators in this initializer expression. 2047 unsigned NumDesignators : 15; 2048 2049 /// \brief The designators in this designated initialization 2050 /// expression. 2051 Designator *Designators; 2052 2053 /// The number of subexpressions of this initializer expression, 2054 /// which contains both the initializer and any additional 2055 /// expressions used by array and array-range designators. 2056 unsigned NumSubExprs : 16; 2057 2058 2059 DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 2060 const Designator *Designators, 2061 SourceLocation EqualOrColonLoc, bool GNUSyntax, 2062 unsigned NumSubExprs); 2063 2064 explicit DesignatedInitExpr(unsigned NumSubExprs) 2065 : Expr(DesignatedInitExprClass, EmptyShell()), 2066 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 2067 2068public: 2069 /// A field designator, e.g., ".x". 2070 struct FieldDesignator { 2071 /// Refers to the field that is being initialized. The low bit 2072 /// of this field determines whether this is actually a pointer 2073 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 2074 /// initially constructed, a field designator will store an 2075 /// IdentifierInfo*. After semantic analysis has resolved that 2076 /// name, the field designator will instead store a FieldDecl*. 2077 uintptr_t NameOrField; 2078 2079 /// The location of the '.' in the designated initializer. 2080 unsigned DotLoc; 2081 2082 /// The location of the field name in the designated initializer. 2083 unsigned FieldLoc; 2084 }; 2085 2086 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2087 struct ArrayOrRangeDesignator { 2088 /// Location of the first index expression within the designated 2089 /// initializer expression's list of subexpressions. 2090 unsigned Index; 2091 /// The location of the '[' starting the array range designator. 2092 unsigned LBracketLoc; 2093 /// The location of the ellipsis separating the start and end 2094 /// indices. Only valid for GNU array-range designators. 2095 unsigned EllipsisLoc; 2096 /// The location of the ']' terminating the array range designator. 2097 unsigned RBracketLoc; 2098 }; 2099 2100 /// @brief Represents a single C99 designator. 2101 /// 2102 /// @todo This class is infuriatingly similar to clang::Designator, 2103 /// but minor differences (storing indices vs. storing pointers) 2104 /// keep us from reusing it. Try harder, later, to rectify these 2105 /// differences. 2106 class Designator { 2107 /// @brief The kind of designator this describes. 2108 enum { 2109 FieldDesignator, 2110 ArrayDesignator, 2111 ArrayRangeDesignator 2112 } Kind; 2113 2114 union { 2115 /// A field designator, e.g., ".x". 2116 struct FieldDesignator Field; 2117 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2118 struct ArrayOrRangeDesignator ArrayOrRange; 2119 }; 2120 friend class DesignatedInitExpr; 2121 2122 public: 2123 Designator() {} 2124 2125 /// @brief Initializes a field designator. 2126 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 2127 SourceLocation FieldLoc) 2128 : Kind(FieldDesignator) { 2129 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 2130 Field.DotLoc = DotLoc.getRawEncoding(); 2131 Field.FieldLoc = FieldLoc.getRawEncoding(); 2132 } 2133 2134 /// @brief Initializes an array designator. 2135 Designator(unsigned Index, SourceLocation LBracketLoc, 2136 SourceLocation RBracketLoc) 2137 : Kind(ArrayDesignator) { 2138 ArrayOrRange.Index = Index; 2139 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2140 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 2141 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2142 } 2143 2144 /// @brief Initializes a GNU array-range designator. 2145 Designator(unsigned Index, SourceLocation LBracketLoc, 2146 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 2147 : Kind(ArrayRangeDesignator) { 2148 ArrayOrRange.Index = Index; 2149 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2150 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 2151 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2152 } 2153 2154 bool isFieldDesignator() const { return Kind == FieldDesignator; } 2155 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 2156 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 2157 2158 IdentifierInfo * getFieldName(); 2159 2160 FieldDecl *getField() { 2161 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2162 if (Field.NameOrField & 0x01) 2163 return 0; 2164 else 2165 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 2166 } 2167 2168 void setField(FieldDecl *FD) { 2169 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2170 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 2171 } 2172 2173 SourceLocation getDotLoc() const { 2174 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2175 return SourceLocation::getFromRawEncoding(Field.DotLoc); 2176 } 2177 2178 SourceLocation getFieldLoc() const { 2179 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2180 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 2181 } 2182 2183 SourceLocation getLBracketLoc() const { 2184 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2185 "Only valid on an array or array-range designator"); 2186 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 2187 } 2188 2189 SourceLocation getRBracketLoc() const { 2190 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2191 "Only valid on an array or array-range designator"); 2192 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 2193 } 2194 2195 SourceLocation getEllipsisLoc() const { 2196 assert(Kind == ArrayRangeDesignator && 2197 "Only valid on an array-range designator"); 2198 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 2199 } 2200 2201 unsigned getFirstExprIndex() const { 2202 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2203 "Only valid on an array or array-range designator"); 2204 return ArrayOrRange.Index; 2205 } 2206 2207 SourceLocation getStartLocation() const { 2208 if (Kind == FieldDesignator) 2209 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 2210 else 2211 return getLBracketLoc(); 2212 } 2213 }; 2214 2215 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 2216 unsigned NumDesignators, 2217 Expr **IndexExprs, unsigned NumIndexExprs, 2218 SourceLocation EqualOrColonLoc, 2219 bool GNUSyntax, Expr *Init); 2220 2221 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 2222 2223 /// @brief Returns the number of designators in this initializer. 2224 unsigned size() const { return NumDesignators; } 2225 2226 // Iterator access to the designators. 2227 typedef Designator* designators_iterator; 2228 designators_iterator designators_begin() { return Designators; } 2229 designators_iterator designators_end() { 2230 return Designators + NumDesignators; 2231 } 2232 2233 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 2234 2235 void setDesignators(const Designator *Desigs, unsigned NumDesigs); 2236 2237 Expr *getArrayIndex(const Designator& D); 2238 Expr *getArrayRangeStart(const Designator& D); 2239 Expr *getArrayRangeEnd(const Designator& D); 2240 2241 /// @brief Retrieve the location of the '=' that precedes the 2242 /// initializer value itself, if present. 2243 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 2244 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 2245 2246 /// @brief Determines whether this designated initializer used the 2247 /// deprecated GNU syntax for designated initializers. 2248 bool usesGNUSyntax() const { return GNUSyntax; } 2249 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 2250 2251 /// @brief Retrieve the initializer value. 2252 Expr *getInit() const { 2253 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 2254 } 2255 2256 void setInit(Expr *init) { 2257 *child_begin() = init; 2258 } 2259 2260 /// \brief Retrieve the total number of subexpressions in this 2261 /// designated initializer expression, including the actual 2262 /// initialized value and any expressions that occur within array 2263 /// and array-range designators. 2264 unsigned getNumSubExprs() const { return NumSubExprs; } 2265 2266 Expr *getSubExpr(unsigned Idx) { 2267 assert(Idx < NumSubExprs && "Subscript out of range"); 2268 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2269 Ptr += sizeof(DesignatedInitExpr); 2270 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 2271 } 2272 2273 void setSubExpr(unsigned Idx, Expr *E) { 2274 assert(Idx < NumSubExprs && "Subscript out of range"); 2275 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 2276 Ptr += sizeof(DesignatedInitExpr); 2277 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 2278 } 2279 2280 /// \brief Replaces the designator at index @p Idx with the series 2281 /// of designators in [First, Last). 2282 void ExpandDesignator(unsigned Idx, const Designator *First, 2283 const Designator *Last); 2284 2285 virtual SourceRange getSourceRange() const; 2286 2287 virtual void Destroy(ASTContext &C); 2288 2289 static bool classof(const Stmt *T) { 2290 return T->getStmtClass() == DesignatedInitExprClass; 2291 } 2292 static bool classof(const DesignatedInitExpr *) { return true; } 2293 2294 // Iterators 2295 virtual child_iterator child_begin(); 2296 virtual child_iterator child_end(); 2297}; 2298 2299/// \brief Represents an implicitly-generated value initialization of 2300/// an object of a given type. 2301/// 2302/// Implicit value initializations occur within semantic initializer 2303/// list expressions (InitListExpr) as placeholders for subobject 2304/// initializations not explicitly specified by the user. 2305/// 2306/// \see InitListExpr 2307class ImplicitValueInitExpr : public Expr { 2308public: 2309 explicit ImplicitValueInitExpr(QualType ty) 2310 : Expr(ImplicitValueInitExprClass, ty) { } 2311 2312 /// \brief Construct an empty implicit value initialization. 2313 explicit ImplicitValueInitExpr(EmptyShell Empty) 2314 : Expr(ImplicitValueInitExprClass, Empty) { } 2315 2316 static bool classof(const Stmt *T) { 2317 return T->getStmtClass() == ImplicitValueInitExprClass; 2318 } 2319 static bool classof(const ImplicitValueInitExpr *) { return true; } 2320 2321 virtual SourceRange getSourceRange() const { 2322 return SourceRange(); 2323 } 2324 2325 // Iterators 2326 virtual child_iterator child_begin(); 2327 virtual child_iterator child_end(); 2328}; 2329 2330//===----------------------------------------------------------------------===// 2331// Clang Extensions 2332//===----------------------------------------------------------------------===// 2333 2334 2335/// ExtVectorElementExpr - This represents access to specific elements of a 2336/// vector, and may occur on the left hand side or right hand side. For example 2337/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 2338/// 2339/// Note that the base may have either vector or pointer to vector type, just 2340/// like a struct field reference. 2341/// 2342class ExtVectorElementExpr : public Expr { 2343 Stmt *Base; 2344 IdentifierInfo *Accessor; 2345 SourceLocation AccessorLoc; 2346public: 2347 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 2348 SourceLocation loc) 2349 : Expr(ExtVectorElementExprClass, ty), 2350 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 2351 2352 /// \brief Build an empty vector element expression. 2353 explicit ExtVectorElementExpr(EmptyShell Empty) 2354 : Expr(ExtVectorElementExprClass, Empty) { } 2355 2356 const Expr *getBase() const { return cast<Expr>(Base); } 2357 Expr *getBase() { return cast<Expr>(Base); } 2358 void setBase(Expr *E) { Base = E; } 2359 2360 IdentifierInfo &getAccessor() const { return *Accessor; } 2361 void setAccessor(IdentifierInfo *II) { Accessor = II; } 2362 2363 SourceLocation getAccessorLoc() const { return AccessorLoc; } 2364 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 2365 2366 /// getNumElements - Get the number of components being selected. 2367 unsigned getNumElements() const; 2368 2369 /// containsDuplicateElements - Return true if any element access is 2370 /// repeated. 2371 bool containsDuplicateElements() const; 2372 2373 /// getEncodedElementAccess - Encode the elements accessed into an llvm 2374 /// aggregate Constant of ConstantInt(s). 2375 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 2376 2377 virtual SourceRange getSourceRange() const { 2378 return SourceRange(getBase()->getLocStart(), AccessorLoc); 2379 } 2380 2381 /// isArrow - Return true if the base expression is a pointer to vector, 2382 /// return false if the base expression is a vector. 2383 bool isArrow() const; 2384 2385 static bool classof(const Stmt *T) { 2386 return T->getStmtClass() == ExtVectorElementExprClass; 2387 } 2388 static bool classof(const ExtVectorElementExpr *) { return true; } 2389 2390 // Iterators 2391 virtual child_iterator child_begin(); 2392 virtual child_iterator child_end(); 2393}; 2394 2395 2396/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 2397/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 2398class BlockExpr : public Expr { 2399protected: 2400 BlockDecl *TheBlock; 2401 bool HasBlockDeclRefExprs; 2402public: 2403 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs) 2404 : Expr(BlockExprClass, ty), 2405 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {} 2406 2407 /// \brief Build an empty block expression. 2408 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 2409 2410 const BlockDecl *getBlockDecl() const { return TheBlock; } 2411 BlockDecl *getBlockDecl() { return TheBlock; } 2412 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 2413 2414 // Convenience functions for probing the underlying BlockDecl. 2415 SourceLocation getCaretLocation() const; 2416 const Stmt *getBody() const; 2417 Stmt *getBody(); 2418 2419 const Stmt *getBody(ASTContext &C) const { return getBody(); } 2420 Stmt *getBody(ASTContext &C) { return getBody(); } 2421 2422 virtual SourceRange getSourceRange() const { 2423 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 2424 } 2425 2426 /// getFunctionType - Return the underlying function type for this block. 2427 const FunctionType *getFunctionType() const; 2428 2429 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr 2430 /// inside of the block that reference values outside the block. 2431 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; } 2432 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; } 2433 2434 static bool classof(const Stmt *T) { 2435 return T->getStmtClass() == BlockExprClass; 2436 } 2437 static bool classof(const BlockExpr *) { return true; } 2438 2439 // Iterators 2440 virtual child_iterator child_begin(); 2441 virtual child_iterator child_end(); 2442}; 2443 2444/// BlockDeclRefExpr - A reference to a declared variable, function, 2445/// enum, etc. 2446class BlockDeclRefExpr : public Expr { 2447 ValueDecl *D; 2448 SourceLocation Loc; 2449 bool IsByRef; 2450public: 2451 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef) : 2452 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef) {} 2453 2454 // \brief Build an empty reference to a declared variable in a 2455 // block. 2456 explicit BlockDeclRefExpr(EmptyShell Empty) 2457 : Expr(BlockDeclRefExprClass, Empty) { } 2458 2459 ValueDecl *getDecl() { return D; } 2460 const ValueDecl *getDecl() const { return D; } 2461 void setDecl(ValueDecl *VD) { D = VD; } 2462 2463 SourceLocation getLocation() const { return Loc; } 2464 void setLocation(SourceLocation L) { Loc = L; } 2465 2466 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 2467 2468 bool isByRef() const { return IsByRef; } 2469 void setByRef(bool BR) { IsByRef = BR; } 2470 2471 static bool classof(const Stmt *T) { 2472 return T->getStmtClass() == BlockDeclRefExprClass; 2473 } 2474 static bool classof(const BlockDeclRefExpr *) { return true; } 2475 2476 // Iterators 2477 virtual child_iterator child_begin(); 2478 virtual child_iterator child_end(); 2479}; 2480 2481} // end namespace clang 2482 2483#endif 2484