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