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