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