Expr.h revision fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2
1//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the Expr interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_EXPR_H
15#define LLVM_CLANG_AST_EXPR_H
16
17#include "clang/AST/Stmt.h"
18#include "clang/AST/Type.h"
19#include "clang/AST/Decl.h"
20#include "llvm/ADT/APSInt.h"
21
22namespace clang {
23  class IdentifierInfo;
24  class Decl;
25  class ASTContext;
26
27/// Expr - This represents one expression.  Note that Expr's are subclasses of
28/// Stmt.  This allows an expression to be transparently used any place a Stmt
29/// is required.
30///
31class Expr : public Stmt {
32  QualType TR;
33protected:
34  Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {}
35  ~Expr() {}
36public:
37  QualType getType() const { return TR; }
38  void setType(QualType t) { TR = t; }
39
40  /// SourceLocation tokens are not useful in isolation - they are low level
41  /// value objects created/interpreted by SourceManager. We assume AST
42  /// clients will have a pointer to the respective SourceManager.
43  virtual SourceRange getSourceRange() const = 0;
44  SourceLocation getLocStart() const { return getSourceRange().Begin(); }
45  SourceLocation getLocEnd() const { return getSourceRange().End(); }
46
47  /// getExprLoc - Return the preferred location for the arrow when diagnosing
48  /// a problem with a generic expression.
49  virtual SourceLocation getExprLoc() const { return getLocStart(); }
50
51  /// hasLocalSideEffect - Return true if this immediate expression has side
52  /// effects, not counting any sub-expressions.
53  bool hasLocalSideEffect() const;
54
55  /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
56  /// incomplete type other than void. Nonarray expressions that can be lvalues:
57  ///  - name, where name must be a variable
58  ///  - e[i]
59  ///  - (e), where e must be an lvalue
60  ///  - e.name, where e must be an lvalue
61  ///  - e->name
62  ///  - *e, the type of e cannot be a function type
63  ///  - string-constant
64  ///  - reference type [C++ [expr]]
65  ///
66  enum isLvalueResult {
67    LV_Valid,
68    LV_NotObjectType,
69    LV_IncompleteVoidType,
70    LV_DuplicateVectorComponents,
71    LV_InvalidExpression
72  };
73  isLvalueResult isLvalue() const;
74
75  /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
76  /// does not have an incomplete type, does not have a const-qualified type,
77  /// and if it is a structure or union, does not have any member (including,
78  /// recursively, any member or element of all contained aggregates or unions)
79  /// with a const-qualified type.
80  enum isModifiableLvalueResult {
81    MLV_Valid,
82    MLV_NotObjectType,
83    MLV_IncompleteVoidType,
84    MLV_DuplicateVectorComponents,
85    MLV_InvalidExpression,
86    MLV_IncompleteType,
87    MLV_ConstQualified,
88    MLV_ArrayType
89  };
90  isModifiableLvalueResult isModifiableLvalue() const;
91
92  bool isNullPointerConstant(ASTContext &Ctx) const;
93
94  /// isIntegerConstantExpr - Return true if this expression is a valid integer
95  /// constant expression, and, if so, return its value in Result.  If not a
96  /// valid i-c-e, return false and fill in Loc (if specified) with the location
97  /// of the invalid expression.
98  bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
99                             SourceLocation *Loc = 0,
100                             bool isEvaluated = true) const;
101  bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
102    llvm::APSInt X(32);
103    return isIntegerConstantExpr(X, Ctx, Loc);
104  }
105
106  virtual void visit(StmtVisitor &Visitor);
107  static bool classof(const Stmt *T) {
108    return T->getStmtClass() >= firstExprConstant &&
109           T->getStmtClass() <= lastExprConstant;
110  }
111  static bool classof(const Expr *) { return true; }
112};
113
114//===----------------------------------------------------------------------===//
115// Primary Expressions.
116//===----------------------------------------------------------------------===//
117
118/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
119/// enum, etc.
120class DeclRefExpr : public Expr {
121  Decl *D; // a ValueDecl or EnumConstantDecl
122  SourceLocation Loc;
123public:
124  DeclRefExpr(Decl *d, QualType t, SourceLocation l) :
125    Expr(DeclRefExprClass, t), D(d), Loc(l) {}
126
127  Decl *getDecl() { return D; }
128  const Decl *getDecl() const { return D; }
129  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
130
131
132  virtual void visit(StmtVisitor &Visitor);
133  static bool classof(const Stmt *T) {
134    return T->getStmtClass() == DeclRefExprClass;
135  }
136  static bool classof(const DeclRefExpr *) { return true; }
137};
138
139/// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__.
140class PreDefinedExpr : public Expr {
141public:
142  enum IdentType {
143    Func,
144    Function,
145    PrettyFunction
146  };
147
148private:
149  SourceLocation Loc;
150  IdentType Type;
151public:
152  PreDefinedExpr(SourceLocation l, QualType type, IdentType IT)
153    : Expr(PreDefinedExprClass, type), Loc(l), Type(IT) {}
154
155  IdentType getIdentType() const { return Type; }
156
157  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
158
159  virtual void visit(StmtVisitor &Visitor);
160  static bool classof(const Stmt *T) {
161    return T->getStmtClass() == PreDefinedExprClass;
162  }
163  static bool classof(const PreDefinedExpr *) { return true; }
164};
165
166class IntegerLiteral : public Expr {
167  llvm::APInt Value;
168  SourceLocation Loc;
169public:
170  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
171  // or UnsignedLongLongTy
172  IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
173    : Expr(IntegerLiteralClass, type), Value(V), Loc(l) {
174    assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
175  }
176  const llvm::APInt &getValue() const { return Value; }
177  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
178
179  virtual void visit(StmtVisitor &Visitor);
180  static bool classof(const Stmt *T) {
181    return T->getStmtClass() == IntegerLiteralClass;
182  }
183  static bool classof(const IntegerLiteral *) { return true; }
184};
185
186class CharacterLiteral : public Expr {
187  unsigned Value;
188  SourceLocation Loc;
189public:
190  // type should be IntTy
191  CharacterLiteral(unsigned value, QualType type, SourceLocation l)
192    : Expr(CharacterLiteralClass, type), Value(value), Loc(l) {
193  }
194  SourceLocation getLoc() const { return Loc; }
195
196  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
197
198  unsigned getValue() const { return Value; }
199
200  virtual void visit(StmtVisitor &Visitor);
201  static bool classof(const Stmt *T) {
202    return T->getStmtClass() == CharacterLiteralClass;
203  }
204  static bool classof(const CharacterLiteral *) { return true; }
205};
206
207class FloatingLiteral : public Expr {
208  float Value; // FIXME
209  SourceLocation Loc;
210public:
211  FloatingLiteral(float value, QualType type, SourceLocation l)
212    : Expr(FloatingLiteralClass, type), Value(value), Loc(l) {}
213
214  float getValue() const { return Value; }
215
216  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
217
218  virtual void visit(StmtVisitor &Visitor);
219  static bool classof(const Stmt *T) {
220    return T->getStmtClass() == FloatingLiteralClass;
221  }
222  static bool classof(const FloatingLiteral *) { return true; }
223};
224
225class StringLiteral : public Expr {
226  const char *StrData;
227  unsigned ByteLength;
228  bool IsWide;
229  // if the StringLiteral was composed using token pasting, both locations
230  // are needed. If not (the common case), firstTokLoc == lastTokLoc.
231  // FIXME: if space becomes an issue, we should create a sub-class.
232  SourceLocation firstTokLoc, lastTokLoc;
233public:
234  StringLiteral(const char *strData, unsigned byteLength, bool Wide,
235                QualType t, SourceLocation b, SourceLocation e);
236  virtual ~StringLiteral();
237
238  const char *getStrData() const { return StrData; }
239  unsigned getByteLength() const { return ByteLength; }
240  bool isWide() const { return IsWide; }
241
242  virtual SourceRange getSourceRange() const {
243    return SourceRange(firstTokLoc,lastTokLoc);
244  }
245  virtual void visit(StmtVisitor &Visitor);
246  static bool classof(const Stmt *T) {
247    return T->getStmtClass() == StringLiteralClass;
248  }
249  static bool classof(const StringLiteral *) { return true; }
250};
251
252/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
253/// AST node is only formed if full location information is requested.
254class ParenExpr : public Expr {
255  SourceLocation L, R;
256  Expr *Val;
257public:
258  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
259    : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {}
260
261  const Expr *getSubExpr() const { return Val; }
262  Expr *getSubExpr() { return Val; }
263  SourceRange getSourceRange() const { return SourceRange(L, R); }
264
265  virtual void visit(StmtVisitor &Visitor);
266  static bool classof(const Stmt *T) {
267    return T->getStmtClass() == ParenExprClass;
268  }
269  static bool classof(const ParenExpr *) { return true; }
270};
271
272
273/// UnaryOperator - This represents the unary-expression's (except sizeof of
274/// types), the postinc/postdec operators from postfix-expression, and various
275/// extensions.
276class UnaryOperator : public Expr {
277public:
278  enum Opcode {
279    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
280    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
281    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
282    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
283    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
284    SizeOf, AlignOf,  // [C99 6.5.3.4] Sizeof (expr, not type) operator.
285    Real, Imag,       // "__real expr"/"__imag expr" Extension.
286    Extension         // __extension__ marker.
287  };
288private:
289  Expr *Val;
290  Opcode Opc;
291  SourceLocation Loc;
292public:
293
294  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
295    : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {}
296
297  Opcode getOpcode() const { return Opc; }
298  Expr *getSubExpr() const { return Val; }
299
300  /// getOperatorLoc - Return the location of the operator.
301  SourceLocation getOperatorLoc() const { return Loc; }
302
303  /// isPostfix - Return true if this is a postfix operation, like x++.
304  static bool isPostfix(Opcode Op);
305
306  bool isPostfix() const { return isPostfix(Opc); }
307  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
308  bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; }
309  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
310
311  /// getDecl - a recursive routine that derives the base decl for an
312  /// expression. For example, it will return the declaration for "s" from
313  /// the following complex expression "s.zz[2].bb.vv".
314  static bool isAddressable(Expr *e);
315
316  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
317  /// corresponds to, e.g. "sizeof" or "[pre]++"
318  static const char *getOpcodeStr(Opcode Op);
319
320  virtual SourceRange getSourceRange() const {
321    if (isPostfix())
322      return SourceRange(Val->getLocStart(), Loc);
323    else
324      return SourceRange(Loc, Val->getLocEnd());
325  }
326  virtual SourceLocation getExprLoc() const { return Loc; }
327
328  virtual void visit(StmtVisitor &Visitor);
329  static bool classof(const Stmt *T) {
330    return T->getStmtClass() == UnaryOperatorClass;
331  }
332  static bool classof(const UnaryOperator *) { return true; }
333};
334
335/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of
336/// *types*.  sizeof(expr) is handled by UnaryOperator.
337class SizeOfAlignOfTypeExpr : public Expr {
338  bool isSizeof;  // true if sizeof, false if alignof.
339  QualType Ty;
340  SourceLocation OpLoc, RParenLoc;
341public:
342  SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType,
343                        SourceLocation op, SourceLocation rp) :
344    Expr(SizeOfAlignOfTypeExprClass, resultType),
345    isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {}
346
347  bool isSizeOf() const { return isSizeof; }
348  QualType getArgumentType() const { return Ty; }
349
350  SourceLocation getOperatorLoc() const { return OpLoc; }
351  SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); }
352
353  virtual void visit(StmtVisitor &Visitor);
354  static bool classof(const Stmt *T) {
355    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
356  }
357  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
358};
359
360//===----------------------------------------------------------------------===//
361// Postfix Operators.
362//===----------------------------------------------------------------------===//
363
364/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
365class ArraySubscriptExpr : public Expr {
366  Expr *Base, *Idx;
367  SourceLocation RBracketLoc;
368public:
369  ArraySubscriptExpr(Expr *base, Expr *idx, QualType t,
370                     SourceLocation rbracketloc) :
371    Expr(ArraySubscriptExprClass, t),
372    Base(base), Idx(idx), RBracketLoc(rbracketloc) {}
373
374  Expr *getBase() { return Base; }
375  const Expr *getBase() const { return Base; }
376  Expr *getIdx() { return Idx; }
377  const Expr *getIdx() const { return Idx; }
378
379  SourceRange getSourceRange() const {
380    return SourceRange(Base->getLocStart(), RBracketLoc);
381  }
382  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
383
384  virtual void visit(StmtVisitor &Visitor);
385  static bool classof(const Stmt *T) {
386    return T->getStmtClass() == ArraySubscriptExprClass;
387  }
388  static bool classof(const ArraySubscriptExpr *) { return true; }
389};
390
391
392/// CallExpr - [C99 6.5.2.2] Function Calls.
393///
394class CallExpr : public Expr {
395  Expr *Fn;
396  Expr **Args;
397  unsigned NumArgs;
398  SourceLocation RParenLoc;
399public:
400  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
401           SourceLocation rparenloc);
402  ~CallExpr() {
403    delete [] Args;
404  }
405
406  const Expr *getCallee() const { return Fn; }
407  Expr *getCallee() { return Fn; }
408
409  /// getNumArgs - Return the number of actual arguments to this call.
410  ///
411  unsigned getNumArgs() const { return NumArgs; }
412
413  /// getArg - Return the specified argument.
414  Expr *getArg(unsigned Arg) {
415    assert(Arg < NumArgs && "Arg access out of range!");
416    return Args[Arg];
417  }
418  const Expr *getArg(unsigned Arg) const {
419    assert(Arg < NumArgs && "Arg access out of range!");
420    return Args[Arg];
421  }
422
423  /// getNumCommas - Return the number of commas that must have been present in
424  /// this function call.
425  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
426
427  SourceRange getSourceRange() const {
428    return SourceRange(Fn->getLocStart(), RParenLoc);
429  }
430
431  virtual void visit(StmtVisitor &Visitor);
432  static bool classof(const Stmt *T) {
433    return T->getStmtClass() == CallExprClass;
434  }
435  static bool classof(const CallExpr *) { return true; }
436};
437
438/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
439///
440class MemberExpr : public Expr {
441  Expr *Base;
442  FieldDecl *MemberDecl;
443  SourceLocation MemberLoc;
444  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
445public:
446  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l)
447    : Expr(MemberExprClass, memberdecl->getType()),
448      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
449
450  Expr *getBase() const { return Base; }
451  FieldDecl *getMemberDecl() const { return MemberDecl; }
452  bool isArrow() const { return IsArrow; }
453
454  virtual SourceRange getSourceRange() const {
455    return SourceRange(getBase()->getLocStart(), MemberLoc);
456  }
457  virtual SourceLocation getExprLoc() const { return MemberLoc; }
458
459  virtual void visit(StmtVisitor &Visitor);
460  static bool classof(const Stmt *T) {
461    return T->getStmtClass() == MemberExprClass;
462  }
463  static bool classof(const MemberExpr *) { return true; }
464};
465
466/// OCUVectorComponent
467///
468class OCUVectorComponent : public Expr {
469  Expr *Base;
470  IdentifierInfo &Accessor;
471  SourceLocation AccessorLoc;
472public:
473  enum ComponentType {
474    Point,
475    Color,
476    Texture
477  };
478  OCUVectorComponent(QualType ty, Expr *base, IdentifierInfo &accessor,
479                     SourceLocation loc) : Expr(OCUVectorComponentClass, ty),
480                     Base(base), Accessor(accessor), AccessorLoc(loc) {}
481
482  Expr *getBase() const { return Base; }
483  IdentifierInfo & getAccessor() const { return Accessor; }
484  ComponentType getComponentType() const;
485
486  bool containsDuplicateComponents() const;
487
488  virtual SourceRange getSourceRange() const {
489    return SourceRange(getBase()->getLocStart(), AccessorLoc);
490  }
491  virtual void visit(StmtVisitor &Visitor);
492  static bool classof(const Stmt *T) {
493    return T->getStmtClass() == OCUVectorComponentClass;
494  }
495  static bool classof(const OCUVectorComponent *) { return true; }
496};
497
498/// CompoundLiteralExpr - [C99 6.5.2.5]
499///
500class CompoundLiteralExpr : public Expr {
501  Expr *Init;
502public:
503  CompoundLiteralExpr(QualType ty, Expr *init) :
504    Expr(CompoundLiteralExprClass, ty), Init(init) {}
505
506  Expr *getInitializer() const { return Init; }
507
508  virtual SourceRange getSourceRange() const { return SourceRange(); } // FIXME
509
510  virtual void visit(StmtVisitor &Visitor);
511  static bool classof(const Stmt *T) {
512    return T->getStmtClass() == CompoundLiteralExprClass;
513  }
514  static bool classof(const CompoundLiteralExpr *) { return true; }
515};
516
517/// ImplicitCastExpr - Allows us to explicitly represent implicit type
518/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
519/// float->double, short->int, etc.
520///
521class ImplicitCastExpr : public Expr {
522  Expr *Op;
523public:
524  ImplicitCastExpr(QualType ty, Expr *op) :
525    Expr(ImplicitCastExprClass, ty), Op(op) {}
526
527  Expr *getSubExpr() { return Op; }
528  const Expr *getSubExpr() const { return Op; }
529
530  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
531
532  virtual void visit(StmtVisitor &Visitor);
533  static bool classof(const Stmt *T) {
534    return T->getStmtClass() == ImplicitCastExprClass;
535  }
536  static bool classof(const ImplicitCastExpr *) { return true; }
537};
538
539/// CastExpr - [C99 6.5.4] Cast Operators.
540///
541class CastExpr : public Expr {
542  Expr *Op;
543  SourceLocation Loc; // the location of the left paren
544public:
545  CastExpr(QualType ty, Expr *op, SourceLocation l) :
546    Expr(CastExprClass, ty), Op(op), Loc(l) {}
547
548  SourceLocation getLParenLoc() const { return Loc; }
549
550  Expr *getSubExpr() const { return Op; }
551
552  virtual SourceRange getSourceRange() const {
553    return SourceRange(Loc, getSubExpr()->getSourceRange().End());
554  }
555  virtual void visit(StmtVisitor &Visitor);
556  static bool classof(const Stmt *T) {
557    return T->getStmtClass() == CastExprClass;
558  }
559  static bool classof(const CastExpr *) { return true; }
560};
561
562class BinaryOperator : public Expr {
563public:
564  enum Opcode {
565    // Operators listed in order of precedence.
566    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
567    Add, Sub,         // [C99 6.5.6] Additive operators.
568    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
569    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
570    EQ, NE,           // [C99 6.5.9] Equality operators.
571    And,              // [C99 6.5.10] Bitwise AND operator.
572    Xor,              // [C99 6.5.11] Bitwise XOR operator.
573    Or,               // [C99 6.5.12] Bitwise OR operator.
574    LAnd,             // [C99 6.5.13] Logical AND operator.
575    LOr,              // [C99 6.5.14] Logical OR operator.
576    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
577    DivAssign, RemAssign,
578    AddAssign, SubAssign,
579    ShlAssign, ShrAssign,
580    AndAssign, XorAssign,
581    OrAssign,
582    Comma             // [C99 6.5.17] Comma operator.
583  };
584
585  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy)
586    : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) {
587    assert(!isCompoundAssignmentOp() &&
588           "Use ArithAssignBinaryOperator for compound assignments");
589  }
590
591  Opcode getOpcode() const { return Opc; }
592  Expr *getLHS() const { return LHS; }
593  Expr *getRHS() const { return RHS; }
594  virtual SourceRange getSourceRange() const {
595    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
596  }
597
598  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
599  /// corresponds to, e.g. "<<=".
600  static const char *getOpcodeStr(Opcode Op);
601
602  /// predicates to categorize the respective opcodes.
603  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
604  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
605  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
606  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
607  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
608  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
609  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
610  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
611  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
612  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
613
614  virtual void visit(StmtVisitor &Visitor);
615  static bool classof(const Stmt *T) {
616    return T->getStmtClass() == BinaryOperatorClass;
617  }
618  static bool classof(const BinaryOperator *) { return true; }
619private:
620  Expr *LHS, *RHS;
621  Opcode Opc;
622protected:
623  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, bool dead)
624    : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) {
625  }
626};
627
628/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
629/// track of the type the operation is performed in.  Due to the semantics of
630/// these operators, the operands are promoted, the aritmetic performed, an
631/// implicit conversion back to the result type done, then the assignment takes
632/// place.  This captures the intermediate type which the computation is done
633/// in.
634class CompoundAssignOperator : public BinaryOperator {
635  QualType ComputationType;
636public:
637  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
638                         QualType ResType, QualType CompType)
639    : BinaryOperator(lhs, rhs, opc, ResType, true), ComputationType(CompType) {
640    assert(isCompoundAssignmentOp() &&
641           "Only should be used for compound assignments");
642  }
643
644  QualType getComputationType() const { return ComputationType; }
645
646  static bool classof(const CompoundAssignOperator *) { return true; }
647  static bool classof(const BinaryOperator *B) {
648    return B->isCompoundAssignmentOp();
649  }
650  static bool classof(const Stmt *S) {
651    return isa<BinaryOperator>(S) && classof(cast<BinaryOperator>(S));
652  }
653};
654
655/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
656/// GNU "missing LHS" extension is in use.
657///
658class ConditionalOperator : public Expr {
659  Expr *Cond, *LHS, *RHS;  // Left/Middle/Right hand sides.
660public:
661  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
662    : Expr(ConditionalOperatorClass, t), Cond(cond), LHS(lhs), RHS(rhs) {}
663
664  Expr *getCond() const { return Cond; }
665  Expr *getLHS() const { return LHS; }
666  Expr *getRHS() const { return RHS; }
667
668  virtual SourceRange getSourceRange() const {
669    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
670  }
671  virtual void visit(StmtVisitor &Visitor);
672  static bool classof(const Stmt *T) {
673    return T->getStmtClass() == ConditionalOperatorClass;
674  }
675  static bool classof(const ConditionalOperator *) { return true; }
676};
677
678/// AddrLabel - The GNU address of label extension, representing &&label.
679class AddrLabel : public Expr {
680  SourceLocation AmpAmpLoc, LabelLoc;
681  LabelStmt *Label;
682public:
683  AddrLabel(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, QualType t)
684    : Expr(AddrLabelClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
685
686  virtual SourceRange getSourceRange() const {
687    return SourceRange(AmpAmpLoc, LabelLoc);
688  }
689
690  LabelStmt *getLabel() const { return Label; }
691
692  virtual void visit(StmtVisitor &Visitor);
693  static bool classof(const Stmt *T) {
694    return T->getStmtClass() == AddrLabelClass;
695  }
696  static bool classof(const AddrLabel *) { return true; }
697};
698
699/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
700/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
701/// takes the value of the last subexpression.
702class StmtExpr : public Expr {
703  CompoundStmt *SubStmt;
704  SourceLocation LParenLoc, RParenLoc;
705public:
706    StmtExpr(CompoundStmt *substmt, QualType T,
707             SourceLocation lp, SourceLocation rp)
708    : Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) {
709    }
710
711  CompoundStmt *getSubStmt() { return SubStmt; }
712  const CompoundStmt *getSubStmt() const { return SubStmt; }
713
714  virtual SourceRange getSourceRange() const {
715    return SourceRange(LParenLoc, RParenLoc);
716  }
717
718  virtual void visit(StmtVisitor &Visitor);
719  static bool classof(const Stmt *T) {
720    return T->getStmtClass() == StmtExprClass;
721  }
722  static bool classof(const StmtExpr *) { return true; }
723};
724
725}  // end namespace clang
726
727#endif
728