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