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