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