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