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