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