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