Expr.h revision fa2192042f223b5122a9e17719930f77634fd31f
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 ComputationType; 1328public: 1329 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 1330 QualType ResType, QualType CompType, 1331 SourceLocation OpLoc) 1332 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 1333 ComputationType(CompType) { 1334 assert(isCompoundAssignmentOp() && 1335 "Only should be used for compound assignments"); 1336 } 1337 1338 QualType getComputationType() const { return ComputationType; } 1339 1340 static bool classof(const CompoundAssignOperator *) { return true; } 1341 static bool classof(const Stmt *S) { 1342 return S->getStmtClass() == CompoundAssignOperatorClass; 1343 } 1344 1345 virtual void EmitImpl(llvm::Serializer& S) const; 1346 static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D, 1347 ASTContext& C); 1348}; 1349 1350/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 1351/// GNU "missing LHS" extension is in use. 1352/// 1353class ConditionalOperator : public Expr { 1354 enum { COND, LHS, RHS, END_EXPR }; 1355 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1356public: 1357 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 1358 : Expr(ConditionalOperatorClass, t, 1359 // FIXME: the type of the conditional operator doesn't 1360 // depend on the type of the conditional, but the standard 1361 // seems to imply that it could. File a bug! 1362 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 1363 (cond->isValueDependent() || 1364 (lhs && lhs->isValueDependent()) || 1365 (rhs && rhs->isValueDependent()))) { 1366 SubExprs[COND] = cond; 1367 SubExprs[LHS] = lhs; 1368 SubExprs[RHS] = rhs; 1369 } 1370 1371 // getCond - Return the expression representing the condition for 1372 // the ?: operator. 1373 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1374 1375 // getTrueExpr - Return the subexpression representing the value of the ?: 1376 // expression if the condition evaluates to true. In most cases this value 1377 // will be the same as getLHS() except a GCC extension allows the left 1378 // subexpression to be omitted, and instead of the condition be returned. 1379 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 1380 // is only evaluated once. 1381 Expr *getTrueExpr() const { 1382 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 1383 } 1384 1385 // getTrueExpr - Return the subexpression representing the value of the ?: 1386 // expression if the condition evaluates to false. This is the same as getRHS. 1387 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 1388 1389 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 1390 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1391 1392 virtual SourceRange getSourceRange() const { 1393 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 1394 } 1395 static bool classof(const Stmt *T) { 1396 return T->getStmtClass() == ConditionalOperatorClass; 1397 } 1398 static bool classof(const ConditionalOperator *) { return true; } 1399 1400 // Iterators 1401 virtual child_iterator child_begin(); 1402 virtual child_iterator child_end(); 1403 1404 virtual void EmitImpl(llvm::Serializer& S) const; 1405 static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1406}; 1407 1408/// AddrLabelExpr - The GNU address of label extension, representing &&label. 1409class AddrLabelExpr : public Expr { 1410 SourceLocation AmpAmpLoc, LabelLoc; 1411 LabelStmt *Label; 1412public: 1413 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 1414 QualType t) 1415 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 1416 1417 virtual SourceRange getSourceRange() const { 1418 return SourceRange(AmpAmpLoc, LabelLoc); 1419 } 1420 1421 LabelStmt *getLabel() const { return Label; } 1422 1423 static bool classof(const Stmt *T) { 1424 return T->getStmtClass() == AddrLabelExprClass; 1425 } 1426 static bool classof(const AddrLabelExpr *) { return true; } 1427 1428 // Iterators 1429 virtual child_iterator child_begin(); 1430 virtual child_iterator child_end(); 1431 1432 virtual void EmitImpl(llvm::Serializer& S) const; 1433 static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1434}; 1435 1436/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 1437/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 1438/// takes the value of the last subexpression. 1439class StmtExpr : public Expr { 1440 Stmt *SubStmt; 1441 SourceLocation LParenLoc, RParenLoc; 1442public: 1443 StmtExpr(CompoundStmt *substmt, QualType T, 1444 SourceLocation lp, SourceLocation rp) : 1445 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 1446 1447 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 1448 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 1449 1450 virtual SourceRange getSourceRange() const { 1451 return SourceRange(LParenLoc, RParenLoc); 1452 } 1453 1454 SourceLocation getLParenLoc() const { return LParenLoc; } 1455 SourceLocation getRParenLoc() const { return RParenLoc; } 1456 1457 static bool classof(const Stmt *T) { 1458 return T->getStmtClass() == StmtExprClass; 1459 } 1460 static bool classof(const StmtExpr *) { return true; } 1461 1462 // Iterators 1463 virtual child_iterator child_begin(); 1464 virtual child_iterator child_end(); 1465 1466 virtual void EmitImpl(llvm::Serializer& S) const; 1467 static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1468}; 1469 1470/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p. 1471/// This AST node represents a function that returns 1 if two *types* (not 1472/// expressions) are compatible. The result of this built-in function can be 1473/// used in integer constant expressions. 1474class TypesCompatibleExpr : public Expr { 1475 QualType Type1; 1476 QualType Type2; 1477 SourceLocation BuiltinLoc, RParenLoc; 1478public: 1479 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 1480 QualType t1, QualType t2, SourceLocation RP) : 1481 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2), 1482 BuiltinLoc(BLoc), RParenLoc(RP) {} 1483 1484 QualType getArgType1() const { return Type1; } 1485 QualType getArgType2() const { return Type2; } 1486 1487 virtual SourceRange getSourceRange() const { 1488 return SourceRange(BuiltinLoc, RParenLoc); 1489 } 1490 static bool classof(const Stmt *T) { 1491 return T->getStmtClass() == TypesCompatibleExprClass; 1492 } 1493 static bool classof(const TypesCompatibleExpr *) { return true; } 1494 1495 // Iterators 1496 virtual child_iterator child_begin(); 1497 virtual child_iterator child_end(); 1498 1499 virtual void EmitImpl(llvm::Serializer& S) const; 1500 static TypesCompatibleExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1501}; 1502 1503/// ShuffleVectorExpr - clang-specific builtin-in function 1504/// __builtin_shufflevector. 1505/// This AST node represents a operator that does a constant 1506/// shuffle, similar to LLVM's shufflevector instruction. It takes 1507/// two vectors and a variable number of constant indices, 1508/// and returns the appropriately shuffled vector. 1509class ShuffleVectorExpr : public Expr { 1510 SourceLocation BuiltinLoc, RParenLoc; 1511 1512 // SubExprs - the list of values passed to the __builtin_shufflevector 1513 // function. The first two are vectors, and the rest are constant 1514 // indices. The number of values in this list is always 1515 // 2+the number of indices in the vector type. 1516 Stmt **SubExprs; 1517 unsigned NumExprs; 1518 1519public: 1520 ShuffleVectorExpr(Expr **args, unsigned nexpr, 1521 QualType Type, SourceLocation BLoc, 1522 SourceLocation RP) : 1523 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc), 1524 RParenLoc(RP), NumExprs(nexpr) { 1525 1526 SubExprs = new Stmt*[nexpr]; 1527 for (unsigned i = 0; i < nexpr; i++) 1528 SubExprs[i] = args[i]; 1529 } 1530 1531 virtual SourceRange getSourceRange() const { 1532 return SourceRange(BuiltinLoc, RParenLoc); 1533 } 1534 static bool classof(const Stmt *T) { 1535 return T->getStmtClass() == ShuffleVectorExprClass; 1536 } 1537 static bool classof(const ShuffleVectorExpr *) { return true; } 1538 1539 ~ShuffleVectorExpr() { 1540 delete [] SubExprs; 1541 } 1542 1543 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 1544 /// constant expression, the actual arguments passed in, and the function 1545 /// pointers. 1546 unsigned getNumSubExprs() const { return NumExprs; } 1547 1548 /// getExpr - Return the Expr at the specified index. 1549 Expr *getExpr(unsigned Index) { 1550 assert((Index < NumExprs) && "Arg access out of range!"); 1551 return cast<Expr>(SubExprs[Index]); 1552 } 1553 const Expr *getExpr(unsigned Index) const { 1554 assert((Index < NumExprs) && "Arg access out of range!"); 1555 return cast<Expr>(SubExprs[Index]); 1556 } 1557 1558 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 1559 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 1560 return getExpr(N+2)->getIntegerConstantExprValue(Ctx).getZExtValue(); 1561 } 1562 1563 // Iterators 1564 virtual child_iterator child_begin(); 1565 virtual child_iterator child_end(); 1566 1567 virtual void EmitImpl(llvm::Serializer& S) const; 1568 static ShuffleVectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1569}; 1570 1571/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 1572/// This AST node is similar to the conditional operator (?:) in C, with 1573/// the following exceptions: 1574/// - the test expression must be a integer constant expression. 1575/// - the expression returned acts like the chosen subexpression in every 1576/// visible way: the type is the same as that of the chosen subexpression, 1577/// and all predicates (whether it's an l-value, whether it's an integer 1578/// constant expression, etc.) return the same result as for the chosen 1579/// sub-expression. 1580class ChooseExpr : public Expr { 1581 enum { COND, LHS, RHS, END_EXPR }; 1582 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 1583 SourceLocation BuiltinLoc, RParenLoc; 1584public: 1585 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 1586 SourceLocation RP) 1587 : Expr(ChooseExprClass, t), 1588 BuiltinLoc(BLoc), RParenLoc(RP) { 1589 SubExprs[COND] = cond; 1590 SubExprs[LHS] = lhs; 1591 SubExprs[RHS] = rhs; 1592 } 1593 1594 /// isConditionTrue - Return whether the condition is true (i.e. not 1595 /// equal to zero). 1596 bool isConditionTrue(ASTContext &C) const; 1597 1598 /// getChosenSubExpr - Return the subexpression chosen according to the 1599 /// condition. 1600 Expr *getChosenSubExpr(ASTContext &C) const { 1601 return isConditionTrue(C) ? getLHS() : getRHS(); 1602 } 1603 1604 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 1605 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1606 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1607 1608 virtual SourceRange getSourceRange() const { 1609 return SourceRange(BuiltinLoc, RParenLoc); 1610 } 1611 static bool classof(const Stmt *T) { 1612 return T->getStmtClass() == ChooseExprClass; 1613 } 1614 static bool classof(const ChooseExpr *) { return true; } 1615 1616 // Iterators 1617 virtual child_iterator child_begin(); 1618 virtual child_iterator child_end(); 1619 1620 virtual void EmitImpl(llvm::Serializer& S) const; 1621 static ChooseExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1622}; 1623 1624/// GNUNullExpr - Implements the GNU __null extension, which is a name 1625/// for a null pointer constant that has integral type (e.g., int or 1626/// long) and is the same size and alignment as a pointer. The __null 1627/// extension is typically only used by system headers, which define 1628/// NULL as __null in C++ rather than using 0 (which is an integer 1629/// that may not match the size of a pointer). 1630class GNUNullExpr : public Expr { 1631 /// TokenLoc - The location of the __null keyword. 1632 SourceLocation TokenLoc; 1633 1634public: 1635 GNUNullExpr(QualType Ty, SourceLocation Loc) 1636 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { } 1637 1638 /// getTokenLocation - The location of the __null token. 1639 SourceLocation getTokenLocation() const { return TokenLoc; } 1640 1641 virtual SourceRange getSourceRange() const { 1642 return SourceRange(TokenLoc); 1643 } 1644 static bool classof(const Stmt *T) { 1645 return T->getStmtClass() == GNUNullExprClass; 1646 } 1647 static bool classof(const GNUNullExpr *) { return true; } 1648 1649 // Iterators 1650 virtual child_iterator child_begin(); 1651 virtual child_iterator child_end(); 1652 1653 virtual void EmitImpl(llvm::Serializer& S) const; 1654 static GNUNullExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1655}; 1656 1657/// VAArgExpr, used for the builtin function __builtin_va_start. 1658class VAArgExpr : public Expr { 1659 Stmt *Val; 1660 SourceLocation BuiltinLoc, RParenLoc; 1661public: 1662 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 1663 : Expr(VAArgExprClass, t), 1664 Val(e), 1665 BuiltinLoc(BLoc), 1666 RParenLoc(RPLoc) { } 1667 1668 const Expr *getSubExpr() const { return cast<Expr>(Val); } 1669 Expr *getSubExpr() { return cast<Expr>(Val); } 1670 virtual SourceRange getSourceRange() const { 1671 return SourceRange(BuiltinLoc, RParenLoc); 1672 } 1673 static bool classof(const Stmt *T) { 1674 return T->getStmtClass() == VAArgExprClass; 1675 } 1676 static bool classof(const VAArgExpr *) { return true; } 1677 1678 // Iterators 1679 virtual child_iterator child_begin(); 1680 virtual child_iterator child_end(); 1681 1682 virtual void EmitImpl(llvm::Serializer& S) const; 1683 static VAArgExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1684}; 1685 1686/// @brief Describes an C or C++ initializer list. 1687/// 1688/// InitListExpr describes an initializer list, which can be used to 1689/// initialize objects of different types, including 1690/// struct/class/union types, arrays, and vectors. For example: 1691/// 1692/// @code 1693/// struct foo x = { 1, { 2, 3 } }; 1694/// @endcode 1695/// 1696/// Prior to semantic analysis, an initializer list will represent the 1697/// initializer list as written by the user, but will have the 1698/// placeholder type "void". This initializer list is called the 1699/// syntactic form of the initializer, and may contain C99 designated 1700/// initializers (represented as DesignatedInitExprs), initializations 1701/// of subobject members without explicit braces, and so on. Clients 1702/// interested in the original syntax of the initializer list should 1703/// use the syntactic form of the initializer list. 1704/// 1705/// After semantic analysis, the initializer list will represent the 1706/// semantic form of the initializer, where the initializations of all 1707/// subobjects are made explicit with nested InitListExpr nodes and 1708/// C99 designators have been eliminated by placing the designated 1709/// initializations into the subobject they initialize. Additionally, 1710/// any "holes" in the initialization, where no initializer has been 1711/// specified for a particular subobject, will be replaced with 1712/// implicitly-generated ImplicitValueInitExpr expressions that 1713/// value-initialize the subobjects. Note, however, that the 1714/// initializer lists may still have fewer initializers than there are 1715/// elements to initialize within the object. 1716/// 1717/// Given the semantic form of the initializer list, one can retrieve 1718/// the original syntactic form of that initializer list (if it 1719/// exists) using getSyntacticForm(). Since many initializer lists 1720/// have the same syntactic and semantic forms, getSyntacticForm() may 1721/// return NULL, indicating that the current initializer list also 1722/// serves as its syntactic form. 1723class InitListExpr : public Expr { 1724 std::vector<Stmt *> InitExprs; 1725 SourceLocation LBraceLoc, RBraceLoc; 1726 1727 /// Contains the initializer list that describes the syntactic form 1728 /// written in the source code. 1729 InitListExpr *SyntacticForm; 1730 1731 /// If this initializer list initializes a union, specifies which 1732 /// field within the union will be initialized. 1733 FieldDecl *UnionFieldInit; 1734 1735 /// Whether this initializer list originally had a GNU array-range 1736 /// designator in it. This is a temporary marker used by CodeGen. 1737 bool HadArrayRangeDesignator; 1738 1739public: 1740 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits, 1741 SourceLocation rbraceloc); 1742 1743 unsigned getNumInits() const { return InitExprs.size(); } 1744 1745 const Expr* getInit(unsigned Init) const { 1746 assert(Init < getNumInits() && "Initializer access out of range!"); 1747 return cast_or_null<Expr>(InitExprs[Init]); 1748 } 1749 1750 Expr* getInit(unsigned Init) { 1751 assert(Init < getNumInits() && "Initializer access out of range!"); 1752 return cast_or_null<Expr>(InitExprs[Init]); 1753 } 1754 1755 void setInit(unsigned Init, Expr *expr) { 1756 assert(Init < getNumInits() && "Initializer access out of range!"); 1757 InitExprs[Init] = expr; 1758 } 1759 1760 /// \brief Reserve space for some number of initializers. 1761 void reserveInits(unsigned NumInits); 1762 1763 /// @brief Specify the number of initializers 1764 /// 1765 /// If there are more than @p NumInits initializers, the remaining 1766 /// initializers will be destroyed. If there are fewer than @p 1767 /// NumInits initializers, NULL expressions will be added for the 1768 /// unknown initializers. 1769 void resizeInits(ASTContext &Context, unsigned NumInits); 1770 1771 /// @brief Updates the initializer at index @p Init with the new 1772 /// expression @p expr, and returns the old expression at that 1773 /// location. 1774 /// 1775 /// When @p Init is out of range for this initializer list, the 1776 /// initializer list will be extended with NULL expressions to 1777 /// accomodate the new entry. 1778 Expr *updateInit(unsigned Init, Expr *expr); 1779 1780 /// \brief If this initializes a union, specifies which field in the 1781 /// union to initialize. 1782 /// 1783 /// Typically, this field is the first named field within the 1784 /// union. However, a designated initializer can specify the 1785 /// initialization of a different field within the union. 1786 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; } 1787 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; } 1788 1789 // Explicit InitListExpr's originate from source code (and have valid source 1790 // locations). Implicit InitListExpr's are created by the semantic analyzer. 1791 bool isExplicit() { 1792 return LBraceLoc.isValid() && RBraceLoc.isValid(); 1793 } 1794 1795 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 1796 1797 /// @brief Retrieve the initializer list that describes the 1798 /// syntactic form of the initializer. 1799 /// 1800 /// 1801 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 1802 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 1803 1804 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 1805 void sawArrayRangeDesignator() { 1806 HadArrayRangeDesignator = true; 1807 } 1808 1809 virtual SourceRange getSourceRange() const { 1810 return SourceRange(LBraceLoc, RBraceLoc); 1811 } 1812 static bool classof(const Stmt *T) { 1813 return T->getStmtClass() == InitListExprClass; 1814 } 1815 static bool classof(const InitListExpr *) { return true; } 1816 1817 // Iterators 1818 virtual child_iterator child_begin(); 1819 virtual child_iterator child_end(); 1820 1821 typedef std::vector<Stmt *>::iterator iterator; 1822 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator; 1823 1824 iterator begin() { return InitExprs.begin(); } 1825 iterator end() { return InitExprs.end(); } 1826 reverse_iterator rbegin() { return InitExprs.rbegin(); } 1827 reverse_iterator rend() { return InitExprs.rend(); } 1828 1829 // Serailization. 1830 virtual void EmitImpl(llvm::Serializer& S) const; 1831 static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1832 1833private: 1834 // Used by serializer. 1835 InitListExpr() : Expr(InitListExprClass, QualType()) {} 1836}; 1837 1838/// @brief Represents a C99 designated initializer expression. 1839/// 1840/// A designated initializer expression (C99 6.7.8) contains one or 1841/// more designators (which can be field designators, array 1842/// designators, or GNU array-range designators) followed by an 1843/// expression that initializes the field or element(s) that the 1844/// designators refer to. For example, given: 1845/// 1846/// @code 1847/// struct point { 1848/// double x; 1849/// double y; 1850/// }; 1851/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 1852/// @endcode 1853/// 1854/// The InitListExpr contains three DesignatedInitExprs, the first of 1855/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 1856/// designators, one array designator for @c [2] followed by one field 1857/// designator for @c .y. The initalization expression will be 1.0. 1858class DesignatedInitExpr : public Expr { 1859 /// The location of the '=' or ':' prior to the actual initializer 1860 /// expression. 1861 SourceLocation EqualOrColonLoc; 1862 1863 /// Whether this designated initializer used the GNU deprecated ':' 1864 /// syntax rather than the C99 '=' syntax. 1865 bool UsesColonSyntax : 1; 1866 1867 /// The number of designators in this initializer expression. 1868 unsigned NumDesignators : 15; 1869 1870 /// The number of subexpressions of this initializer expression, 1871 /// which contains both the initializer and any additional 1872 /// expressions used by array and array-range designators. 1873 unsigned NumSubExprs : 16; 1874 1875 DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 1876 SourceLocation EqualOrColonLoc, bool UsesColonSyntax, 1877 unsigned NumSubExprs) 1878 : Expr(DesignatedInitExprClass, Ty), 1879 EqualOrColonLoc(EqualOrColonLoc), UsesColonSyntax(UsesColonSyntax), 1880 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) { } 1881 1882public: 1883 /// A field designator, e.g., ".x". 1884 struct FieldDesignator { 1885 /// Refers to the field that is being initialized. The low bit 1886 /// of this field determines whether this is actually a pointer 1887 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 1888 /// initially constructed, a field designator will store an 1889 /// IdentifierInfo*. After semantic analysis has resolved that 1890 /// name, the field designator will instead store a FieldDecl*. 1891 uintptr_t NameOrField; 1892 1893 /// The location of the '.' in the designated initializer. 1894 unsigned DotLoc; 1895 1896 /// The location of the field name in the designated initializer. 1897 unsigned FieldLoc; 1898 }; 1899 1900 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 1901 struct ArrayOrRangeDesignator { 1902 /// Location of the first index expression within the designated 1903 /// initializer expression's list of subexpressions. 1904 unsigned Index; 1905 /// The location of the '[' starting the array range designator. 1906 unsigned LBracketLoc; 1907 /// The location of the ellipsis separating the start and end 1908 /// indices. Only valid for GNU array-range designators. 1909 unsigned EllipsisLoc; 1910 /// The location of the ']' terminating the array range designator. 1911 unsigned RBracketLoc; 1912 }; 1913 1914 /// @brief Represents a single C99 designator. 1915 /// 1916 /// @todo This class is infuriatingly similar to clang::Designator, 1917 /// but minor differences (storing indices vs. storing pointers) 1918 /// keep us from reusing it. Try harder, later, to rectify these 1919 /// differences. 1920 class Designator { 1921 /// @brief The kind of designator this describes. 1922 enum { 1923 FieldDesignator, 1924 ArrayDesignator, 1925 ArrayRangeDesignator 1926 } Kind; 1927 1928 union { 1929 /// A field designator, e.g., ".x". 1930 struct FieldDesignator Field; 1931 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 1932 struct ArrayOrRangeDesignator ArrayOrRange; 1933 }; 1934 friend class DesignatedInitExpr; 1935 1936 public: 1937 /// @brief Initializes a field designator. 1938 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 1939 SourceLocation FieldLoc) 1940 : Kind(FieldDesignator) { 1941 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 1942 Field.DotLoc = DotLoc.getRawEncoding(); 1943 Field.FieldLoc = FieldLoc.getRawEncoding(); 1944 } 1945 1946 /// @brief Initializes an array designator. 1947 Designator(unsigned Index, SourceLocation LBracketLoc, 1948 SourceLocation RBracketLoc) 1949 : Kind(ArrayDesignator) { 1950 ArrayOrRange.Index = Index; 1951 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 1952 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 1953 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 1954 } 1955 1956 /// @brief Initializes a GNU array-range designator. 1957 Designator(unsigned Index, SourceLocation LBracketLoc, 1958 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 1959 : Kind(ArrayRangeDesignator) { 1960 ArrayOrRange.Index = Index; 1961 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 1962 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 1963 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 1964 } 1965 1966 bool isFieldDesignator() const { return Kind == FieldDesignator; } 1967 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 1968 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 1969 1970 IdentifierInfo * getFieldName(); 1971 1972 FieldDecl *getField() { 1973 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1974 if (Field.NameOrField & 0x01) 1975 return 0; 1976 else 1977 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 1978 } 1979 1980 void setField(FieldDecl *FD) { 1981 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1982 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 1983 } 1984 1985 SourceLocation getDotLoc() const { 1986 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1987 return SourceLocation::getFromRawEncoding(Field.DotLoc); 1988 } 1989 1990 SourceLocation getFieldLoc() const { 1991 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1992 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 1993 } 1994 1995 SourceLocation getLBracketLoc() const { 1996 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 1997 "Only valid on an array or array-range designator"); 1998 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 1999 } 2000 2001 SourceLocation getRBracketLoc() const { 2002 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2003 "Only valid on an array or array-range designator"); 2004 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 2005 } 2006 2007 SourceLocation getEllipsisLoc() const { 2008 assert(Kind == ArrayRangeDesignator && 2009 "Only valid on an array-range designator"); 2010 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 2011 } 2012 2013 SourceLocation getStartLocation() const { 2014 if (Kind == FieldDesignator) 2015 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 2016 else 2017 return getLBracketLoc(); 2018 } 2019 }; 2020 2021 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 2022 unsigned NumDesignators, 2023 Expr **IndexExprs, unsigned NumIndexExprs, 2024 SourceLocation EqualOrColonLoc, 2025 bool UsesColonSyntax, Expr *Init); 2026 2027 /// @brief Returns the number of designators in this initializer. 2028 unsigned size() const { return NumDesignators; } 2029 2030 // Iterator access to the designators. 2031 typedef Designator* designators_iterator; 2032 designators_iterator designators_begin(); 2033 designators_iterator designators_end(); 2034 2035 Expr *getArrayIndex(const Designator& D); 2036 Expr *getArrayRangeStart(const Designator& D); 2037 Expr *getArrayRangeEnd(const Designator& D); 2038 2039 /// @brief Retrieve the location of the '=' that precedes the 2040 /// initializer value itself, if present. 2041 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 2042 2043 /// @brief Determines whether this designated initializer used the 2044 /// GNU 'fieldname:' syntax or the C99 '=' syntax. 2045 bool usesColonSyntax() const { return UsesColonSyntax; } 2046 2047 /// @brief Retrieve the initializer value. 2048 Expr *getInit() const { 2049 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 2050 } 2051 2052 void setInit(Expr *init) { 2053 *child_begin() = init; 2054 } 2055 2056 virtual SourceRange getSourceRange() const; 2057 2058 static bool classof(const Stmt *T) { 2059 return T->getStmtClass() == DesignatedInitExprClass; 2060 } 2061 static bool classof(const DesignatedInitExpr *) { return true; } 2062 2063 // Iterators 2064 virtual child_iterator child_begin(); 2065 virtual child_iterator child_end(); 2066}; 2067 2068/// \brief Represents an implicitly-generated value initialization of 2069/// an object of a given type. 2070/// 2071/// Implicit value initializations occur within semantic initializer 2072/// list expressions (InitListExpr) as placeholders for subobject 2073/// initializations not explicitly specified by the user. 2074/// 2075/// \see InitListExpr 2076class ImplicitValueInitExpr : public Expr { 2077public: 2078 explicit ImplicitValueInitExpr(QualType ty) 2079 : Expr(ImplicitValueInitExprClass, ty) { } 2080 2081 static bool classof(const Stmt *T) { 2082 return T->getStmtClass() == ImplicitValueInitExprClass; 2083 } 2084 static bool classof(const ImplicitValueInitExpr *) { return true; } 2085 2086 virtual SourceRange getSourceRange() const { 2087 return SourceRange(); 2088 } 2089 2090 // Iterators 2091 virtual child_iterator child_begin(); 2092 virtual child_iterator child_end(); 2093}; 2094 2095//===----------------------------------------------------------------------===// 2096// Clang Extensions 2097//===----------------------------------------------------------------------===// 2098 2099 2100/// ExtVectorElementExpr - This represents access to specific elements of a 2101/// vector, and may occur on the left hand side or right hand side. For example 2102/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 2103/// 2104/// Note that the base may have either vector or pointer to vector type, just 2105/// like a struct field reference. 2106/// 2107class ExtVectorElementExpr : public Expr { 2108 Stmt *Base; 2109 IdentifierInfo &Accessor; 2110 SourceLocation AccessorLoc; 2111public: 2112 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 2113 SourceLocation loc) 2114 : Expr(ExtVectorElementExprClass, ty), 2115 Base(base), Accessor(accessor), AccessorLoc(loc) {} 2116 2117 const Expr *getBase() const { return cast<Expr>(Base); } 2118 Expr *getBase() { return cast<Expr>(Base); } 2119 2120 IdentifierInfo &getAccessor() const { return Accessor; } 2121 2122 /// getNumElements - Get the number of components being selected. 2123 unsigned getNumElements() const; 2124 2125 /// containsDuplicateElements - Return true if any element access is 2126 /// repeated. 2127 bool containsDuplicateElements() const; 2128 2129 /// getEncodedElementAccess - Encode the elements accessed into an llvm 2130 /// aggregate Constant of ConstantInt(s). 2131 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 2132 2133 virtual SourceRange getSourceRange() const { 2134 return SourceRange(getBase()->getLocStart(), AccessorLoc); 2135 } 2136 2137 /// isArrow - Return true if the base expression is a pointer to vector, 2138 /// return false if the base expression is a vector. 2139 bool isArrow() const; 2140 2141 static bool classof(const Stmt *T) { 2142 return T->getStmtClass() == ExtVectorElementExprClass; 2143 } 2144 static bool classof(const ExtVectorElementExpr *) { return true; } 2145 2146 // Iterators 2147 virtual child_iterator child_begin(); 2148 virtual child_iterator child_end(); 2149 2150 virtual void EmitImpl(llvm::Serializer& S) const; 2151 static ExtVectorElementExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 2152}; 2153 2154 2155/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 2156/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 2157class BlockExpr : public Expr { 2158protected: 2159 BlockDecl *TheBlock; 2160 bool HasBlockDeclRefExprs; 2161public: 2162 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs) 2163 : Expr(BlockExprClass, ty), 2164 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {} 2165 2166 const BlockDecl *getBlockDecl() const { return TheBlock; } 2167 BlockDecl *getBlockDecl() { return TheBlock; } 2168 2169 // Convenience functions for probing the underlying BlockDecl. 2170 SourceLocation getCaretLocation() const; 2171 const Stmt *getBody() const; 2172 Stmt *getBody(); 2173 2174 virtual SourceRange getSourceRange() const { 2175 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 2176 } 2177 2178 /// getFunctionType - Return the underlying function type for this block. 2179 const FunctionType *getFunctionType() const; 2180 2181 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr 2182 /// contained inside. 2183 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; } 2184 2185 static bool classof(const Stmt *T) { 2186 return T->getStmtClass() == BlockExprClass; 2187 } 2188 static bool classof(const BlockExpr *) { return true; } 2189 2190 // Iterators 2191 virtual child_iterator child_begin(); 2192 virtual child_iterator child_end(); 2193 2194 virtual void EmitImpl(llvm::Serializer& S) const; 2195 static BlockExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 2196}; 2197 2198/// BlockDeclRefExpr - A reference to a declared variable, function, 2199/// enum, etc. 2200class BlockDeclRefExpr : public Expr { 2201 ValueDecl *D; 2202 SourceLocation Loc; 2203 bool IsByRef; 2204public: 2205 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef) : 2206 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef) {} 2207 2208 ValueDecl *getDecl() { return D; } 2209 const ValueDecl *getDecl() const { return D; } 2210 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 2211 2212 bool isByRef() const { return IsByRef; } 2213 2214 static bool classof(const Stmt *T) { 2215 return T->getStmtClass() == BlockDeclRefExprClass; 2216 } 2217 static bool classof(const BlockDeclRefExpr *) { return true; } 2218 2219 // Iterators 2220 virtual child_iterator child_begin(); 2221 virtual child_iterator child_end(); 2222 2223 virtual void EmitImpl(llvm::Serializer& S) const; 2224 static BlockDeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 2225}; 2226 2227} // end namespace clang 2228 2229#endif 2230