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