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