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