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