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