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