Expr.h revision 90045e8ebabf50d6f1b3a67081a621b20b5e5341
1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// 2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// The LLVM Compiler Infrastructure 4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This file was developed by Chris Lattner and is distributed under 6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// the University of Illinois Open Source License. See LICENSE.TXT for details. 7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//===----------------------------------------------------------------------===// 9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This file defines the Expr interface and subclasses. 11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//===----------------------------------------------------------------------===// 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef LLVM_CLANG_AST_EXPR_H 15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define LLVM_CLANG_AST_EXPR_H 16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "clang/AST/Stmt.h" 18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "clang/AST/Type.h" 19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "clang/AST/Decl.h" 20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "llvm/ADT/APSInt.h" 21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace clang { 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch class IdentifierInfo; 24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch class Decl; 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/// Expr - This represents one expression. Note that Expr's are subclasses of 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/// Stmt. This allows an expression to be transparently used any place a Stmt 28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/// is required. 29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/// 30116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass Expr : public Stmt { 31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch QualType TR; 32116680a4aac90f2aa7413d9095a592090648e557Ben Murdochprotected: 33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {} 34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ~Expr() {} 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdochpublic: 36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch QualType getType() const { return TR; } 37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// SourceLocation tokens are not useful in isolation - they are low level 39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// value objects created/interpreted by SourceManager. We assume AST 40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// clients will have a pointer to the respective SourceManager. 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual SourceRange getSourceRange() const = 0; 42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch SourceLocation getLocStart() const { return getSourceRange().Begin(); } 43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch SourceLocation getLocEnd() const { return getSourceRange().End(); } 44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// getExprLoc - Return the preferred location for the arrow when diagnosing 46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// a problem with a generic expression. 47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual SourceLocation getExprLoc() const { return getLocStart(); } 48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// hasLocalSideEffect - Return true if this immediate expression has side 50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// effects, not counting any sub-expressions. 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool hasLocalSideEffect() const; 52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// incomplete type other than void. Nonarray expressions that can be lvalues: 55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// - name, where name must be a variable 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// - e[i] 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// - (e), where e must be an lvalue 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// - e.name, where e must be an lvalue 59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// - e->name 60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// - *e, the type of e cannot be a function type 61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// - string-constant 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// 63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch enum isLvalueResult { 64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch LV_Valid, 65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch LV_NotObjectType, 66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch LV_IncompleteVoidType, 67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch LV_InvalidExpression 68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch }; 69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch isLvalueResult isLvalue(); 70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// does not have an incomplete type, does not have a const-qualified type, 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// and if it is a structure or union, does not have any member (including, 74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// recursively, any member or element of all contained aggregates or unions) 75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// with a const-qualified type. 76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch enum isModifiableLvalueResult { 77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MLV_Valid, 78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MLV_NotObjectType, 79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MLV_IncompleteVoidType, 80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MLV_InvalidExpression, 81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MLV_IncompleteType, 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MLV_ConstQualified, 83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch MLV_ArrayType 84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch }; 85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch isModifiableLvalueResult isModifiableLvalue(); 86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool isNullPointerConstant() const; 88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// isIntegerConstantExpr - Return true if this expression is a valid integer 90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// constant expression, and, if so, return its value in Result. If not a 91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// valid i-c-e, return false and fill in Loc (if specified) with the location 92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// of the invalid expression. 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool isIntegerConstantExpr(llvm::APSInt &Result, SourceLocation *Loc = 0, 94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool isEvaluated = true) const; 95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool isIntegerConstantExpr(SourceLocation *Loc = 0) const { 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch llvm::APSInt X(32); 97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return isIntegerConstantExpr(X, Loc); 98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void visit(StmtVisitor &Visitor); 101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static bool classof(const Stmt *T) { 102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return T->getStmtClass() >= firstExprConstant && 103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch T->getStmtClass() <= lastExprConstant; 104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static bool classof(const Expr *) { return true; } 106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}; 107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//===----------------------------------------------------------------------===// 109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Primary Expressions. 110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//===----------------------------------------------------------------------===// 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/// enum, etc. 114116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass DeclRefExpr : public Expr { 115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch Decl *D; // a ValueDecl or EnumConstantDecl 116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch SourceLocation Loc; 117116680a4aac90f2aa7413d9095a592090648e557Ben Murdochpublic: 118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DeclRefExpr(Decl *d, QualType t, SourceLocation l) : 119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch Expr(DeclRefExprClass, t), D(d), Loc(l) {} 120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch Decl *getDecl() { return D; } 122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const Decl *getDecl() const { return D; } 123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void visit(StmtVisitor &Visitor); 127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static bool classof(const Stmt *T) { 128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return T->getStmtClass() == DeclRefExprClass; 129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 130 static bool classof(const DeclRefExpr *) { return true; } 131}; 132 133class IntegerLiteral : public Expr { 134 llvm::APInt Value; 135 SourceLocation Loc; 136public: 137 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 138 // or UnsignedLongLongTy 139 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 140 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) { 141 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 142 } 143 const llvm::APInt &getValue() const { return Value; } 144 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 145 146 virtual void visit(StmtVisitor &Visitor); 147 static bool classof(const Stmt *T) { 148 return T->getStmtClass() == IntegerLiteralClass; 149 } 150 static bool classof(const IntegerLiteral *) { return true; } 151}; 152 153class CharacterLiteral : public Expr { 154 unsigned Value; 155 SourceLocation Loc; 156public: 157 // type should be IntTy 158 CharacterLiteral(unsigned value, QualType type, SourceLocation l) 159 : Expr(CharacterLiteralClass, type), Value(value), Loc(l) { 160 } 161 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 162 163 unsigned getValue() const { return Value; } 164 165 virtual void visit(StmtVisitor &Visitor); 166 static bool classof(const Stmt *T) { 167 return T->getStmtClass() == CharacterLiteralClass; 168 } 169 static bool classof(const CharacterLiteral *) { return true; } 170}; 171 172class FloatingLiteral : public Expr { 173 float Value; // FIXME 174 SourceLocation Loc; 175public: 176 FloatingLiteral(float value, QualType type, SourceLocation l) 177 : Expr(FloatingLiteralClass, type), Value(value), Loc(l) {} 178 179 float getValue() const { return Value; } 180 181 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 182 183 virtual void visit(StmtVisitor &Visitor); 184 static bool classof(const Stmt *T) { 185 return T->getStmtClass() == FloatingLiteralClass; 186 } 187 static bool classof(const FloatingLiteral *) { return true; } 188}; 189 190class StringLiteral : public Expr { 191 const char *StrData; 192 unsigned ByteLength; 193 bool IsWide; 194 // if the StringLiteral was composed using token pasting, both locations 195 // are needed. If not (the common case), firstTokLoc == lastTokLoc. 196 // FIXME: if space becomes an issue, we should create a sub-class. 197 SourceLocation firstTokLoc, lastTokLoc; 198public: 199 StringLiteral(const char *strData, unsigned byteLength, bool Wide, 200 QualType t, SourceLocation b, SourceLocation e); 201 virtual ~StringLiteral(); 202 203 const char *getStrData() const { return StrData; } 204 unsigned getByteLength() const { return ByteLength; } 205 bool isWide() const { return IsWide; } 206 207 virtual SourceRange getSourceRange() const { 208 return SourceRange(firstTokLoc,lastTokLoc); 209 } 210 virtual void visit(StmtVisitor &Visitor); 211 static bool classof(const Stmt *T) { 212 return T->getStmtClass() == StringLiteralClass; 213 } 214 static bool classof(const StringLiteral *) { return true; } 215}; 216 217/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 218/// AST node is only formed if full location information is requested. 219class ParenExpr : public Expr { 220 SourceLocation L, R; 221 Expr *Val; 222public: 223 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 224 : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {} 225 226 const Expr *getSubExpr() const { return Val; } 227 Expr *getSubExpr() { return Val; } 228 SourceRange getSourceRange() const { return SourceRange(L, R); } 229 230 virtual void visit(StmtVisitor &Visitor); 231 static bool classof(const Stmt *T) { 232 return T->getStmtClass() == ParenExprClass; 233 } 234 static bool classof(const ParenExpr *) { return true; } 235}; 236 237 238/// UnaryOperator - This represents the unary-expression's (except sizeof of 239/// types), the postinc/postdec operators from postfix-expression, and various 240/// extensions. 241class UnaryOperator : public Expr { 242public: 243 enum Opcode { 244 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 245 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 246 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 247 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 248 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 249 SizeOf, AlignOf, // [C99 6.5.3.4] Sizeof (expr, not type) operator. 250 Real, Imag, // "__real expr"/"__imag expr" Extension. 251 Extension // __extension__ marker. 252 }; 253private: 254 Expr *Val; 255 Opcode Opc; 256 SourceLocation Loc; 257public: 258 259 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 260 : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {} 261 262 Opcode getOpcode() const { return Opc; } 263 Expr *getSubExpr() const { return Val; } 264 265 /// getOperatorLoc - Return the location of the operator. 266 SourceLocation getOperatorLoc() const { return Loc; } 267 268 /// isPostfix - Return true if this is a postfix operation, like x++. 269 static bool isPostfix(Opcode Op); 270 271 bool isPostfix() const { return isPostfix(Opc); } 272 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 273 bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; } 274 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 275 276 /// getDecl - a recursive routine that derives the base decl for an 277 /// expression. For example, it will return the declaration for "s" from 278 /// the following complex expression "s.zz[2].bb.vv". 279 static bool isAddressable(Expr *e); 280 281 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 282 /// corresponds to, e.g. "sizeof" or "[pre]++" 283 static const char *getOpcodeStr(Opcode Op); 284 285 virtual SourceRange getSourceRange() const { 286 if (isPostfix()) 287 return SourceRange(Val->getLocStart(), Loc); 288 else 289 return SourceRange(Loc, Val->getLocEnd()); 290 } 291 virtual SourceLocation getExprLoc() const { return Loc; } 292 293 virtual void visit(StmtVisitor &Visitor); 294 static bool classof(const Stmt *T) { 295 return T->getStmtClass() == UnaryOperatorClass; 296 } 297 static bool classof(const UnaryOperator *) { return true; } 298}; 299 300/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of 301/// *types*. sizeof(expr) is handled by UnaryOperator. 302class SizeOfAlignOfTypeExpr : public Expr { 303 bool isSizeof; // true if sizeof, false if alignof. 304 QualType Ty; 305 SourceLocation OpLoc, RParenLoc; 306public: 307 SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType, 308 SourceLocation op, SourceLocation rp) : 309 Expr(SizeOfAlignOfTypeExprClass, resultType), 310 isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {} 311 312 bool isSizeOf() const { return isSizeof; } 313 QualType getArgumentType() const { return Ty; } 314 SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); } 315 316 virtual void visit(StmtVisitor &Visitor); 317 static bool classof(const Stmt *T) { 318 return T->getStmtClass() == SizeOfAlignOfTypeExprClass; 319 } 320 static bool classof(const SizeOfAlignOfTypeExpr *) { return true; } 321}; 322 323//===----------------------------------------------------------------------===// 324// Postfix Operators. 325//===----------------------------------------------------------------------===// 326 327/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 328class ArraySubscriptExpr : public Expr { 329 Expr *Base, *Idx; 330 SourceLocation RBracketLoc; 331public: 332 ArraySubscriptExpr(Expr *base, Expr *idx, QualType t, 333 SourceLocation rbracketloc) : 334 Expr(ArraySubscriptExprClass, t), 335 Base(base), Idx(idx), RBracketLoc(rbracketloc) {} 336 337 Expr *getBase() { return Base; } 338 const Expr *getBase() const { return Base; } 339 Expr *getIdx() { return Idx; } 340 const Expr *getIdx() const { return Idx; } 341 342 SourceRange getSourceRange() const { 343 return SourceRange(Base->getLocStart(), RBracketLoc); 344 } 345 virtual SourceLocation getExprLoc() const { return RBracketLoc; } 346 347 virtual void visit(StmtVisitor &Visitor); 348 static bool classof(const Stmt *T) { 349 return T->getStmtClass() == ArraySubscriptExprClass; 350 } 351 static bool classof(const ArraySubscriptExpr *) { return true; } 352}; 353 354 355/// CallExpr - [C99 6.5.2.2] Function Calls. 356/// 357class CallExpr : public Expr { 358 Expr *Fn; 359 Expr **Args; 360 unsigned NumArgs; 361 SourceLocation RParenLoc; 362public: 363 CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 364 SourceLocation rparenloc); 365 ~CallExpr() { 366 delete [] Args; 367 } 368 369 const Expr *getCallee() const { return Fn; } 370 Expr *getCallee() { return Fn; } 371 372 /// getNumArgs - Return the number of actual arguments to this call. 373 /// 374 unsigned getNumArgs() const { return NumArgs; } 375 376 /// getArg - Return the specified argument. 377 Expr *getArg(unsigned Arg) { 378 assert(Arg < NumArgs && "Arg access out of range!"); 379 return Args[Arg]; 380 } 381 const Expr *getArg(unsigned Arg) const { 382 assert(Arg < NumArgs && "Arg access out of range!"); 383 return Args[Arg]; 384 } 385 386 /// getNumCommas - Return the number of commas that must have been present in 387 /// this function call. 388 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 389 390 SourceRange getSourceRange() const { 391 return SourceRange(Fn->getLocStart(), RParenLoc); 392 } 393 394 virtual void visit(StmtVisitor &Visitor); 395 static bool classof(const Stmt *T) { 396 return T->getStmtClass() == CallExprClass; 397 } 398 static bool classof(const CallExpr *) { return true; } 399}; 400 401/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. 402/// 403class MemberExpr : public Expr { 404 Expr *Base; 405 FieldDecl *MemberDecl; 406 SourceLocation MemberLoc; 407 bool IsArrow; // True if this is "X->F", false if this is "X.F". 408public: 409 MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l) 410 : Expr(MemberExprClass, memberdecl->getType()), 411 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {} 412 413 Expr *getBase() const { return Base; } 414 FieldDecl *getMemberDecl() const { return MemberDecl; } 415 bool isArrow() const { return IsArrow; } 416 417 virtual SourceRange getSourceRange() const { 418 return SourceRange(getBase()->getLocStart(), MemberLoc); 419 } 420 virtual SourceLocation getExprLoc() const { return MemberLoc; } 421 422 virtual void visit(StmtVisitor &Visitor); 423 static bool classof(const Stmt *T) { 424 return T->getStmtClass() == MemberExprClass; 425 } 426 static bool classof(const MemberExpr *) { return true; } 427}; 428 429/// ImplicitCastExpr - Allows us to explicitly represent implicit type 430/// conversions. For example: converting T[]->T*, void f()->void (*f)(), 431/// float->double, short->int, etc. 432/// 433class ImplicitCastExpr : public Expr { 434 Expr *Op; 435public: 436 ImplicitCastExpr(QualType ty, Expr *op) : 437 Expr(ImplicitCastExprClass, ty), Op(op) {} 438 439 Expr *getSubExpr() { return Op; } 440 const Expr *getSubExpr() const { return Op; } 441 442 virtual SourceRange getSourceRange() const { return SourceRange(); } 443 444 virtual void visit(StmtVisitor &Visitor); 445 static bool classof(const Stmt *T) { 446 return T->getStmtClass() == ImplicitCastExprClass; 447 } 448 static bool classof(const ImplicitCastExpr *) { return true; } 449}; 450 451/// CastExpr - [C99 6.5.4] Cast Operators. 452/// 453class CastExpr : public Expr { 454 QualType Ty; 455 Expr *Op; 456 SourceLocation Loc; // the location of the left paren 457public: 458 CastExpr(QualType ty, Expr *op, SourceLocation l) : 459 Expr(CastExprClass, ty), Ty(ty), Op(op), Loc(l) {} 460 461 SourceLocation getLParenLoc() const { return Loc; } 462 463 QualType getDestType() const { return Ty; } 464 Expr *getSubExpr() const { return Op; } 465 466 virtual SourceRange getSourceRange() const { 467 return SourceRange(Loc, getSubExpr()->getSourceRange().End()); 468 } 469 virtual void visit(StmtVisitor &Visitor); 470 static bool classof(const Stmt *T) { 471 return T->getStmtClass() == CastExprClass; 472 } 473 static bool classof(const CastExpr *) { return true; } 474}; 475 476 477class BinaryOperator : public Expr { 478public: 479 enum Opcode { 480 // Operators listed in order of precedence. 481 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 482 Add, Sub, // [C99 6.5.6] Additive operators. 483 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 484 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 485 EQ, NE, // [C99 6.5.9] Equality operators. 486 And, // [C99 6.5.10] Bitwise AND operator. 487 Xor, // [C99 6.5.11] Bitwise XOR operator. 488 Or, // [C99 6.5.12] Bitwise OR operator. 489 LAnd, // [C99 6.5.13] Logical AND operator. 490 LOr, // [C99 6.5.14] Logical OR operator. 491 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 492 DivAssign, RemAssign, 493 AddAssign, SubAssign, 494 ShlAssign, ShrAssign, 495 AndAssign, XorAssign, 496 OrAssign, 497 Comma // [C99 6.5.17] Comma operator. 498 }; 499 500 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy) 501 : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) { 502 assert(!isCompoundAssignmentOp() && 503 "Use ArithAssignBinaryOperator for compound assignments"); 504 } 505 506 Opcode getOpcode() const { return Opc; } 507 Expr *getLHS() const { return LHS; } 508 Expr *getRHS() const { return RHS; } 509 virtual SourceRange getSourceRange() const { 510 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 511 } 512 513 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 514 /// corresponds to, e.g. "<<=". 515 static const char *getOpcodeStr(Opcode Op); 516 517 /// predicates to categorize the respective opcodes. 518 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 519 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 520 bool isShiftOp() const { return Opc == Shl || Opc == Shr; } 521 bool isBitwiseOp() const { return Opc >= And && Opc <= Or; } 522 bool isRelationalOp() const { return Opc >= LT && Opc <= GE; } 523 bool isEqualityOp() const { return Opc == EQ || Opc == NE; } 524 bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; } 525 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 526 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 527 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 528 529 virtual void visit(StmtVisitor &Visitor); 530 static bool classof(const Stmt *T) { 531 return T->getStmtClass() == BinaryOperatorClass; 532 } 533 static bool classof(const BinaryOperator *) { return true; } 534private: 535 Expr *LHS, *RHS; 536 Opcode Opc; 537protected: 538 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, bool dead) 539 : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) { 540 } 541}; 542 543/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 544/// track of the type the operation is performed in. Due to the semantics of 545/// these operators, the operands are promoted, the aritmetic performed, an 546/// implicit conversion back to the result type done, then the assignment takes 547/// place. This captures the intermediate type which the computation is done 548/// in. 549class CompoundAssignOperator : public BinaryOperator { 550 QualType ComputationType; 551public: 552 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 553 QualType ResType, QualType CompType) 554 : BinaryOperator(lhs, rhs, opc, ResType, true), ComputationType(CompType) { 555 assert(isCompoundAssignmentOp() && 556 "Only should be used for compound assignments"); 557 } 558 559 QualType getComputationType() const { return ComputationType; } 560 561 static bool classof(const CompoundAssignOperator *) { return true; } 562 static bool classof(const BinaryOperator *B) { 563 return B->isCompoundAssignmentOp(); 564 } 565 static bool classof(const Stmt *S) { 566 return isa<BinaryOperator>(S) && classof(cast<BinaryOperator>(S)); 567 } 568}; 569 570/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 571/// GNU "missing LHS" extension is in use. 572/// 573class ConditionalOperator : public Expr { 574 Expr *Cond, *LHS, *RHS; // Left/Middle/Right hand sides. 575public: 576 ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t) 577 : Expr(ConditionalOperatorClass, t), Cond(cond), LHS(lhs), RHS(rhs) {} 578 579 Expr *getCond() const { return Cond; } 580 Expr *getLHS() const { return LHS; } 581 Expr *getRHS() const { return RHS; } 582 583 virtual SourceRange getSourceRange() const { 584 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 585 } 586 virtual void visit(StmtVisitor &Visitor); 587 static bool classof(const Stmt *T) { 588 return T->getStmtClass() == ConditionalOperatorClass; 589 } 590 static bool classof(const ConditionalOperator *) { return true; } 591}; 592 593/// AddrLabel - The GNU address of label extension, representing &&label. 594class AddrLabel : public Expr { 595 SourceLocation AmpAmpLoc, LabelLoc; 596 LabelStmt *Label; 597public: 598 AddrLabel(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, QualType t) 599 : Expr(AddrLabelClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 600 601 virtual SourceRange getSourceRange() const { 602 return SourceRange(AmpAmpLoc, LabelLoc); 603 } 604 605 LabelStmt *getLabel() const { return Label; } 606 607 virtual void visit(StmtVisitor &Visitor); 608 static bool classof(const Stmt *T) { 609 return T->getStmtClass() == AddrLabelClass; 610 } 611 static bool classof(const AddrLabel *) { return true; } 612}; 613 614} // end namespace clang 615 616#endif 617