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