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