Expr.h revision 4d0ac88428b3ed7c6f3a2f4e758ea5424ecd70ae
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 - This represents access to specific components of a
467/// vector, and may occur on the left hand side or right hand side.  For example
468/// the following is legal:  "V.xy = V.zw" if V is a 4 element ocu vector.
469///
470class OCUVectorComponent : public Expr {
471  Expr *Base;
472  IdentifierInfo &Accessor;
473  SourceLocation AccessorLoc;
474public:
475  enum ComponentType {
476    Point,   // xywz
477    Color,   // rgba
478    Texture  // uv
479  };
480  OCUVectorComponent(QualType ty, Expr *base, IdentifierInfo &accessor,
481                     SourceLocation loc) : Expr(OCUVectorComponentClass, ty),
482                     Base(base), Accessor(accessor), AccessorLoc(loc) {}
483
484  const Expr *getBase() const { return Base; }
485  Expr *getBase() { return Base; }
486
487  IdentifierInfo &getAccessor() const { return Accessor; }
488
489  /// getNumComponents - Get the number of components being selected.
490  unsigned getNumComponents() const;
491
492  /// getComponentType - Determine whether the components of this access are
493  /// "point" "color" or "texture" elements.
494  ComponentType getComponentType() const;
495
496  /// containsDuplicateComponents - Return true if any element access is
497  /// repeated.
498  bool containsDuplicateComponents() const;
499
500  /// getEncodedElementAccess - Encode the elements accessed into a bit vector.
501  /// The encoding currently uses 2-bit bitfields, but clients should use the
502  /// accessors below to access them.
503  ///
504  unsigned getEncodedElementAccess() const;
505
506  /// getAccessedFieldNo - Given an encoded value and a result number, return
507  /// the input field number being accessed.
508  static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) {
509    return (EncodedVal >> (Idx*2)) & 3;
510  }
511
512  virtual SourceRange getSourceRange() const {
513    return SourceRange(getBase()->getLocStart(), AccessorLoc);
514  }
515  virtual void visit(StmtVisitor &Visitor);
516  static bool classof(const Stmt *T) {
517    return T->getStmtClass() == OCUVectorComponentClass;
518  }
519  static bool classof(const OCUVectorComponent *) { return true; }
520};
521
522/// CompoundLiteralExpr - [C99 6.5.2.5]
523///
524class CompoundLiteralExpr : public Expr {
525  Expr *Init;
526public:
527  CompoundLiteralExpr(QualType ty, Expr *init) :
528    Expr(CompoundLiteralExprClass, ty), Init(init) {}
529
530  Expr *getInitializer() const { return Init; }
531
532  virtual SourceRange getSourceRange() const { return SourceRange(); } // FIXME
533
534  virtual void visit(StmtVisitor &Visitor);
535  static bool classof(const Stmt *T) {
536    return T->getStmtClass() == CompoundLiteralExprClass;
537  }
538  static bool classof(const CompoundLiteralExpr *) { return true; }
539};
540
541/// ImplicitCastExpr - Allows us to explicitly represent implicit type
542/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
543/// float->double, short->int, etc.
544///
545class ImplicitCastExpr : public Expr {
546  Expr *Op;
547public:
548  ImplicitCastExpr(QualType ty, Expr *op) :
549    Expr(ImplicitCastExprClass, ty), Op(op) {}
550
551  Expr *getSubExpr() { return Op; }
552  const Expr *getSubExpr() const { return Op; }
553
554  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
555
556  virtual void visit(StmtVisitor &Visitor);
557  static bool classof(const Stmt *T) {
558    return T->getStmtClass() == ImplicitCastExprClass;
559  }
560  static bool classof(const ImplicitCastExpr *) { return true; }
561};
562
563/// CastExpr - [C99 6.5.4] Cast Operators.
564///
565class CastExpr : public Expr {
566  Expr *Op;
567  SourceLocation Loc; // the location of the left paren
568public:
569  CastExpr(QualType ty, Expr *op, SourceLocation l) :
570    Expr(CastExprClass, ty), Op(op), Loc(l) {}
571
572  SourceLocation getLParenLoc() const { return Loc; }
573
574  Expr *getSubExpr() const { return Op; }
575
576  virtual SourceRange getSourceRange() const {
577    return SourceRange(Loc, getSubExpr()->getSourceRange().End());
578  }
579  virtual void visit(StmtVisitor &Visitor);
580  static bool classof(const Stmt *T) {
581    return T->getStmtClass() == CastExprClass;
582  }
583  static bool classof(const CastExpr *) { return true; }
584};
585
586class BinaryOperator : public Expr {
587public:
588  enum Opcode {
589    // Operators listed in order of precedence.
590    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
591    Add, Sub,         // [C99 6.5.6] Additive operators.
592    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
593    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
594    EQ, NE,           // [C99 6.5.9] Equality operators.
595    And,              // [C99 6.5.10] Bitwise AND operator.
596    Xor,              // [C99 6.5.11] Bitwise XOR operator.
597    Or,               // [C99 6.5.12] Bitwise OR operator.
598    LAnd,             // [C99 6.5.13] Logical AND operator.
599    LOr,              // [C99 6.5.14] Logical OR operator.
600    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
601    DivAssign, RemAssign,
602    AddAssign, SubAssign,
603    ShlAssign, ShrAssign,
604    AndAssign, XorAssign,
605    OrAssign,
606    Comma             // [C99 6.5.17] Comma operator.
607  };
608
609  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy)
610    : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) {
611    assert(!isCompoundAssignmentOp() &&
612           "Use ArithAssignBinaryOperator for compound assignments");
613  }
614
615  Opcode getOpcode() const { return Opc; }
616  Expr *getLHS() const { return LHS; }
617  Expr *getRHS() const { return RHS; }
618  virtual SourceRange getSourceRange() const {
619    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
620  }
621
622  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
623  /// corresponds to, e.g. "<<=".
624  static const char *getOpcodeStr(Opcode Op);
625
626  /// predicates to categorize the respective opcodes.
627  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
628  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
629  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
630  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
631  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
632  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
633  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
634  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
635  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
636  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
637
638  virtual void visit(StmtVisitor &Visitor);
639  static bool classof(const Stmt *T) {
640    return T->getStmtClass() == BinaryOperatorClass;
641  }
642  static bool classof(const BinaryOperator *) { return true; }
643private:
644  Expr *LHS, *RHS;
645  Opcode Opc;
646protected:
647  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, bool dead)
648    : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) {
649  }
650};
651
652/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
653/// track of the type the operation is performed in.  Due to the semantics of
654/// these operators, the operands are promoted, the aritmetic performed, an
655/// implicit conversion back to the result type done, then the assignment takes
656/// place.  This captures the intermediate type which the computation is done
657/// in.
658class CompoundAssignOperator : public BinaryOperator {
659  QualType ComputationType;
660public:
661  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
662                         QualType ResType, QualType CompType)
663    : BinaryOperator(lhs, rhs, opc, ResType, true), ComputationType(CompType) {
664    assert(isCompoundAssignmentOp() &&
665           "Only should be used for compound assignments");
666  }
667
668  QualType getComputationType() const { return ComputationType; }
669
670  static bool classof(const CompoundAssignOperator *) { return true; }
671  static bool classof(const BinaryOperator *B) {
672    return B->isCompoundAssignmentOp();
673  }
674  static bool classof(const Stmt *S) {
675    return isa<BinaryOperator>(S) && classof(cast<BinaryOperator>(S));
676  }
677};
678
679/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
680/// GNU "missing LHS" extension is in use.
681///
682class ConditionalOperator : public Expr {
683  Expr *Cond, *LHS, *RHS;  // Left/Middle/Right hand sides.
684public:
685  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
686    : Expr(ConditionalOperatorClass, t), Cond(cond), LHS(lhs), RHS(rhs) {}
687
688  Expr *getCond() const { return Cond; }
689  Expr *getLHS() const { return LHS; }
690  Expr *getRHS() const { return RHS; }
691
692  virtual SourceRange getSourceRange() const {
693    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
694  }
695  virtual void visit(StmtVisitor &Visitor);
696  static bool classof(const Stmt *T) {
697    return T->getStmtClass() == ConditionalOperatorClass;
698  }
699  static bool classof(const ConditionalOperator *) { return true; }
700};
701
702/// AddrLabel - The GNU address of label extension, representing &&label.
703class AddrLabel : public Expr {
704  SourceLocation AmpAmpLoc, LabelLoc;
705  LabelStmt *Label;
706public:
707  AddrLabel(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, QualType t)
708    : Expr(AddrLabelClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
709
710  virtual SourceRange getSourceRange() const {
711    return SourceRange(AmpAmpLoc, LabelLoc);
712  }
713
714  LabelStmt *getLabel() const { return Label; }
715
716  virtual void visit(StmtVisitor &Visitor);
717  static bool classof(const Stmt *T) {
718    return T->getStmtClass() == AddrLabelClass;
719  }
720  static bool classof(const AddrLabel *) { return true; }
721};
722
723/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
724/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
725/// takes the value of the last subexpression.
726class StmtExpr : public Expr {
727  CompoundStmt *SubStmt;
728  SourceLocation LParenLoc, RParenLoc;
729public:
730  StmtExpr(CompoundStmt *substmt, QualType T,
731           SourceLocation lp, SourceLocation rp) :
732    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
733
734  CompoundStmt *getSubStmt() { return SubStmt; }
735  const CompoundStmt *getSubStmt() const { return SubStmt; }
736
737  virtual SourceRange getSourceRange() const {
738    return SourceRange(LParenLoc, RParenLoc);
739  }
740
741  virtual void visit(StmtVisitor &Visitor);
742  static bool classof(const Stmt *T) {
743    return T->getStmtClass() == StmtExprClass;
744  }
745  static bool classof(const StmtExpr *) { return true; }
746};
747
748/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
749/// This AST node represents a function that returns 1 if two *types* (not
750/// expressions) are compatible. The result of this built-in function can be
751/// used in integer constant expressions.
752class TypesCompatibleExpr : public Expr {
753  QualType Type1;
754  QualType Type2;
755  SourceLocation BuiltinLoc, RParenLoc;
756public:
757  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
758                      QualType t1, QualType t2, SourceLocation RP) :
759    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
760    BuiltinLoc(BLoc), RParenLoc(RP) {}
761
762  QualType getArgType1() const { return Type1; }
763  QualType getArgType2() const { return Type2; }
764
765  int typesAreCompatible() const { return Type::typesAreCompatible(Type1,Type2); }
766
767  virtual SourceRange getSourceRange() const {
768    return SourceRange(BuiltinLoc, RParenLoc);
769  }
770  virtual void visit(StmtVisitor &Visitor);
771  static bool classof(const Stmt *T) {
772    return T->getStmtClass() == TypesCompatibleExprClass;
773  }
774  static bool classof(const TypesCompatibleExpr *) { return true; }
775};
776
777}  // end namespace clang
778
779#endif
780