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