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