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