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