Expr.h revision e6a82b2c29ad05534841e5f8fd033fb17b6f61e2
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
225/// StringLiteral - This represents a string literal expression, e.g. "foo"
226/// or L"bar" (wide strings).  The actual string is returned by getStrData()
227/// is NOT null-terminated, and the length of the string is determined by
228/// calling getByteLength().
229class StringLiteral : public Expr {
230  const char *StrData;
231  unsigned ByteLength;
232  bool IsWide;
233  // if the StringLiteral was composed using token pasting, both locations
234  // are needed. If not (the common case), firstTokLoc == lastTokLoc.
235  // FIXME: if space becomes an issue, we should create a sub-class.
236  SourceLocation firstTokLoc, lastTokLoc;
237public:
238  StringLiteral(const char *strData, unsigned byteLength, bool Wide,
239                QualType t, SourceLocation b, SourceLocation e);
240  virtual ~StringLiteral();
241
242  const char *getStrData() const { return StrData; }
243  unsigned getByteLength() const { return ByteLength; }
244  bool isWide() const { return IsWide; }
245
246  virtual SourceRange getSourceRange() const {
247    return SourceRange(firstTokLoc,lastTokLoc);
248  }
249  virtual void visit(StmtVisitor &Visitor);
250  static bool classof(const Stmt *T) {
251    return T->getStmtClass() == StringLiteralClass;
252  }
253  static bool classof(const StringLiteral *) { return true; }
254};
255
256/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
257/// AST node is only formed if full location information is requested.
258class ParenExpr : public Expr {
259  SourceLocation L, R;
260  Expr *Val;
261public:
262  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
263    : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {}
264
265  const Expr *getSubExpr() const { return Val; }
266  Expr *getSubExpr() { return Val; }
267  SourceRange getSourceRange() const { return SourceRange(L, R); }
268
269  virtual void visit(StmtVisitor &Visitor);
270  static bool classof(const Stmt *T) {
271    return T->getStmtClass() == ParenExprClass;
272  }
273  static bool classof(const ParenExpr *) { return true; }
274};
275
276
277/// UnaryOperator - This represents the unary-expression's (except sizeof of
278/// types), the postinc/postdec operators from postfix-expression, and various
279/// extensions.
280class UnaryOperator : public Expr {
281public:
282  enum Opcode {
283    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
284    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
285    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
286    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
287    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
288    SizeOf, AlignOf,  // [C99 6.5.3.4] Sizeof (expr, not type) operator.
289    Real, Imag,       // "__real expr"/"__imag expr" Extension.
290    Extension         // __extension__ marker.
291  };
292private:
293  Expr *Val;
294  Opcode Opc;
295  SourceLocation Loc;
296public:
297
298  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
299    : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {}
300
301  Opcode getOpcode() const { return Opc; }
302  Expr *getSubExpr() const { return Val; }
303
304  /// getOperatorLoc - Return the location of the operator.
305  SourceLocation getOperatorLoc() const { return Loc; }
306
307  /// isPostfix - Return true if this is a postfix operation, like x++.
308  static bool isPostfix(Opcode Op);
309
310  bool isPostfix() const { return isPostfix(Opc); }
311  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
312  bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; }
313  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
314
315  /// getDecl - a recursive routine that derives the base decl for an
316  /// expression. For example, it will return the declaration for "s" from
317  /// the following complex expression "s.zz[2].bb.vv".
318  static bool isAddressable(Expr *e);
319
320  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
321  /// corresponds to, e.g. "sizeof" or "[pre]++"
322  static const char *getOpcodeStr(Opcode Op);
323
324  virtual SourceRange getSourceRange() const {
325    if (isPostfix())
326      return SourceRange(Val->getLocStart(), Loc);
327    else
328      return SourceRange(Loc, Val->getLocEnd());
329  }
330  virtual SourceLocation getExprLoc() const { return Loc; }
331
332  virtual void visit(StmtVisitor &Visitor);
333  static bool classof(const Stmt *T) {
334    return T->getStmtClass() == UnaryOperatorClass;
335  }
336  static bool classof(const UnaryOperator *) { return true; }
337};
338
339/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of
340/// *types*.  sizeof(expr) is handled by UnaryOperator.
341class SizeOfAlignOfTypeExpr : public Expr {
342  bool isSizeof;  // true if sizeof, false if alignof.
343  QualType Ty;
344  SourceLocation OpLoc, RParenLoc;
345public:
346  SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType,
347                        SourceLocation op, SourceLocation rp) :
348    Expr(SizeOfAlignOfTypeExprClass, resultType),
349    isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {}
350
351  bool isSizeOf() const { return isSizeof; }
352  QualType getArgumentType() const { return Ty; }
353
354  SourceLocation getOperatorLoc() const { return OpLoc; }
355  SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); }
356
357  virtual void visit(StmtVisitor &Visitor);
358  static bool classof(const Stmt *T) {
359    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
360  }
361  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
362};
363
364//===----------------------------------------------------------------------===//
365// Postfix Operators.
366//===----------------------------------------------------------------------===//
367
368/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
369class ArraySubscriptExpr : public Expr {
370  Expr *Base, *Idx;
371  SourceLocation RBracketLoc;
372public:
373  ArraySubscriptExpr(Expr *base, Expr *idx, QualType t,
374                     SourceLocation rbracketloc) :
375    Expr(ArraySubscriptExprClass, t),
376    Base(base), Idx(idx), RBracketLoc(rbracketloc) {}
377
378  Expr *getBase() { return Base; }
379  const Expr *getBase() const { return Base; }
380  Expr *getIdx() { return Idx; }
381  const Expr *getIdx() const { return Idx; }
382
383  SourceRange getSourceRange() const {
384    return SourceRange(Base->getLocStart(), RBracketLoc);
385  }
386  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
387
388  virtual void visit(StmtVisitor &Visitor);
389  static bool classof(const Stmt *T) {
390    return T->getStmtClass() == ArraySubscriptExprClass;
391  }
392  static bool classof(const ArraySubscriptExpr *) { return true; }
393};
394
395
396/// CallExpr - [C99 6.5.2.2] Function Calls.
397///
398class CallExpr : public Expr {
399  Expr *Fn;
400  Expr **Args;
401  unsigned NumArgs;
402  SourceLocation RParenLoc;
403public:
404  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
405           SourceLocation rparenloc);
406  ~CallExpr() {
407    delete [] Args;
408  }
409
410  const Expr *getCallee() const { return Fn; }
411  Expr *getCallee() { return Fn; }
412
413  /// getNumArgs - Return the number of actual arguments to this call.
414  ///
415  unsigned getNumArgs() const { return NumArgs; }
416
417  /// getArg - Return the specified argument.
418  Expr *getArg(unsigned Arg) {
419    assert(Arg < NumArgs && "Arg access out of range!");
420    return Args[Arg];
421  }
422  const Expr *getArg(unsigned Arg) const {
423    assert(Arg < NumArgs && "Arg access out of range!");
424    return Args[Arg];
425  }
426
427  /// getNumCommas - Return the number of commas that must have been present in
428  /// this function call.
429  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
430
431  bool isBuiltinClassifyType(llvm::APSInt &Result) const;
432
433  SourceRange getSourceRange() const {
434    return SourceRange(Fn->getLocStart(), RParenLoc);
435  }
436
437  virtual void visit(StmtVisitor &Visitor);
438  static bool classof(const Stmt *T) {
439    return T->getStmtClass() == CallExprClass;
440  }
441  static bool classof(const CallExpr *) { return true; }
442};
443
444/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
445///
446class MemberExpr : public Expr {
447  Expr *Base;
448  FieldDecl *MemberDecl;
449  SourceLocation MemberLoc;
450  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
451public:
452  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l)
453    : Expr(MemberExprClass, memberdecl->getType()),
454      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
455
456  Expr *getBase() const { return Base; }
457  FieldDecl *getMemberDecl() const { return MemberDecl; }
458  bool isArrow() const { return IsArrow; }
459
460  virtual SourceRange getSourceRange() const {
461    return SourceRange(getBase()->getLocStart(), MemberLoc);
462  }
463  virtual SourceLocation getExprLoc() const { return MemberLoc; }
464
465  virtual void visit(StmtVisitor &Visitor);
466  static bool classof(const Stmt *T) {
467    return T->getStmtClass() == MemberExprClass;
468  }
469  static bool classof(const MemberExpr *) { return true; }
470};
471
472/// OCUVectorElementExpr - This represents access to specific elements of a
473/// vector, and may occur on the left hand side or right hand side.  For example
474/// the following is legal:  "V.xy = V.zw" if V is a 4 element ocu vector.
475///
476class OCUVectorElementExpr : public Expr {
477  Expr *Base;
478  IdentifierInfo &Accessor;
479  SourceLocation AccessorLoc;
480public:
481  enum ElementType {
482    Point,   // xywz
483    Color,   // rgba
484    Texture  // stpq
485  };
486  OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
487                       SourceLocation loc)
488    : Expr(OCUVectorElementExprClass, ty),
489      Base(base), Accessor(accessor), AccessorLoc(loc) {}
490
491  const Expr *getBase() const { return Base; }
492  Expr *getBase() { return Base; }
493
494  IdentifierInfo &getAccessor() const { return Accessor; }
495
496  /// getNumElements - Get the number of components being selected.
497  unsigned getNumElements() const;
498
499  /// getElementType - Determine whether the components of this access are
500  /// "point" "color" or "texture" elements.
501  ElementType getElementType() const;
502
503  /// containsDuplicateElements - Return true if any element access is
504  /// repeated.
505  bool containsDuplicateElements() const;
506
507  /// getEncodedElementAccess - Encode the elements accessed into a bit vector.
508  /// The encoding currently uses 2-bit bitfields, but clients should use the
509  /// accessors below to access them.
510  ///
511  unsigned getEncodedElementAccess() const;
512
513  /// getAccessedFieldNo - Given an encoded value and a result number, return
514  /// the input field number being accessed.
515  static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) {
516    return (EncodedVal >> (Idx*2)) & 3;
517  }
518
519  virtual SourceRange getSourceRange() const {
520    return SourceRange(getBase()->getLocStart(), AccessorLoc);
521  }
522  virtual void visit(StmtVisitor &Visitor);
523  static bool classof(const Stmt *T) {
524    return T->getStmtClass() == OCUVectorElementExprClass;
525  }
526  static bool classof(const OCUVectorElementExpr *) { return true; }
527};
528
529/// CompoundLiteralExpr - [C99 6.5.2.5]
530///
531class CompoundLiteralExpr : public Expr {
532  Expr *Init;
533public:
534  CompoundLiteralExpr(QualType ty, Expr *init) :
535    Expr(CompoundLiteralExprClass, ty), Init(init) {}
536
537  const Expr *getInitializer() const { return Init; }
538  Expr *getInitializer() { return Init; }
539
540  virtual SourceRange getSourceRange() const { return Init->getSourceRange(); }
541
542  virtual void visit(StmtVisitor &Visitor);
543  static bool classof(const Stmt *T) {
544    return T->getStmtClass() == CompoundLiteralExprClass;
545  }
546  static bool classof(const CompoundLiteralExpr *) { return true; }
547};
548
549/// ImplicitCastExpr - Allows us to explicitly represent implicit type
550/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
551/// float->double, short->int, etc.
552///
553class ImplicitCastExpr : public Expr {
554  Expr *Op;
555public:
556  ImplicitCastExpr(QualType ty, Expr *op) :
557    Expr(ImplicitCastExprClass, ty), Op(op) {}
558
559  Expr *getSubExpr() { return Op; }
560  const Expr *getSubExpr() const { return Op; }
561
562  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
563
564  virtual void visit(StmtVisitor &Visitor);
565  static bool classof(const Stmt *T) {
566    return T->getStmtClass() == ImplicitCastExprClass;
567  }
568  static bool classof(const ImplicitCastExpr *) { return true; }
569};
570
571/// CastExpr - [C99 6.5.4] Cast Operators.
572///
573class CastExpr : public Expr {
574  Expr *Op;
575  SourceLocation Loc; // the location of the left paren
576public:
577  CastExpr(QualType ty, Expr *op, SourceLocation l) :
578    Expr(CastExprClass, ty), Op(op), Loc(l) {}
579
580  SourceLocation getLParenLoc() const { return Loc; }
581
582  Expr *getSubExpr() const { return Op; }
583
584  virtual SourceRange getSourceRange() const {
585    return SourceRange(Loc, getSubExpr()->getSourceRange().End());
586  }
587  virtual void visit(StmtVisitor &Visitor);
588  static bool classof(const Stmt *T) {
589    return T->getStmtClass() == CastExprClass;
590  }
591  static bool classof(const CastExpr *) { return true; }
592};
593
594class BinaryOperator : public Expr {
595public:
596  enum Opcode {
597    // Operators listed in order of precedence.
598    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
599    Add, Sub,         // [C99 6.5.6] Additive operators.
600    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
601    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
602    EQ, NE,           // [C99 6.5.9] Equality operators.
603    And,              // [C99 6.5.10] Bitwise AND operator.
604    Xor,              // [C99 6.5.11] Bitwise XOR operator.
605    Or,               // [C99 6.5.12] Bitwise OR operator.
606    LAnd,             // [C99 6.5.13] Logical AND operator.
607    LOr,              // [C99 6.5.14] Logical OR operator.
608    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
609    DivAssign, RemAssign,
610    AddAssign, SubAssign,
611    ShlAssign, ShrAssign,
612    AndAssign, XorAssign,
613    OrAssign,
614    Comma             // [C99 6.5.17] Comma operator.
615  };
616
617  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy)
618    : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) {
619    assert(!isCompoundAssignmentOp() &&
620           "Use ArithAssignBinaryOperator for compound assignments");
621  }
622
623  Opcode getOpcode() const { return Opc; }
624  Expr *getLHS() const { return LHS; }
625  Expr *getRHS() const { return RHS; }
626  virtual SourceRange getSourceRange() const {
627    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
628  }
629
630  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
631  /// corresponds to, e.g. "<<=".
632  static const char *getOpcodeStr(Opcode Op);
633
634  /// predicates to categorize the respective opcodes.
635  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
636  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
637  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
638  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
639  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
640  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
641  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
642  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
643  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
644  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
645
646  virtual void visit(StmtVisitor &Visitor);
647  static bool classof(const Stmt *T) {
648    return T->getStmtClass() == BinaryOperatorClass;
649  }
650  static bool classof(const BinaryOperator *) { return true; }
651private:
652  Expr *LHS, *RHS;
653  Opcode Opc;
654protected:
655  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, bool dead)
656    : Expr(BinaryOperatorClass, ResTy), LHS(lhs), RHS(rhs), Opc(opc) {
657  }
658};
659
660/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
661/// track of the type the operation is performed in.  Due to the semantics of
662/// these operators, the operands are promoted, the aritmetic performed, an
663/// implicit conversion back to the result type done, then the assignment takes
664/// place.  This captures the intermediate type which the computation is done
665/// in.
666class CompoundAssignOperator : public BinaryOperator {
667  QualType ComputationType;
668public:
669  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
670                         QualType ResType, QualType CompType)
671    : BinaryOperator(lhs, rhs, opc, ResType, true), ComputationType(CompType) {
672    assert(isCompoundAssignmentOp() &&
673           "Only should be used for compound assignments");
674  }
675
676  QualType getComputationType() const { return ComputationType; }
677
678  static bool classof(const CompoundAssignOperator *) { return true; }
679  static bool classof(const BinaryOperator *B) {
680    return B->isCompoundAssignmentOp();
681  }
682  static bool classof(const Stmt *S) {
683    return isa<BinaryOperator>(S) && classof(cast<BinaryOperator>(S));
684  }
685};
686
687/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
688/// GNU "missing LHS" extension is in use.
689///
690class ConditionalOperator : public Expr {
691  Expr *Cond, *LHS, *RHS;  // Left/Middle/Right hand sides.
692public:
693  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
694    : Expr(ConditionalOperatorClass, t), Cond(cond), LHS(lhs), RHS(rhs) {}
695
696  Expr *getCond() const { return Cond; }
697  Expr *getLHS() const { return LHS; }
698  Expr *getRHS() const { return RHS; }
699
700  virtual SourceRange getSourceRange() const {
701    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
702  }
703  virtual void visit(StmtVisitor &Visitor);
704  static bool classof(const Stmt *T) {
705    return T->getStmtClass() == ConditionalOperatorClass;
706  }
707  static bool classof(const ConditionalOperator *) { return true; }
708};
709
710/// AddrLabelExpr - The GNU address of label extension, representing &&label.
711class AddrLabelExpr : public Expr {
712  SourceLocation AmpAmpLoc, LabelLoc;
713  LabelStmt *Label;
714public:
715  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
716                QualType t)
717    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
718
719  virtual SourceRange getSourceRange() const {
720    return SourceRange(AmpAmpLoc, LabelLoc);
721  }
722
723  LabelStmt *getLabel() const { return Label; }
724
725  virtual void visit(StmtVisitor &Visitor);
726  static bool classof(const Stmt *T) {
727    return T->getStmtClass() == AddrLabelExprClass;
728  }
729  static bool classof(const AddrLabelExpr *) { return true; }
730};
731
732/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
733/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
734/// takes the value of the last subexpression.
735class StmtExpr : public Expr {
736  CompoundStmt *SubStmt;
737  SourceLocation LParenLoc, RParenLoc;
738public:
739  StmtExpr(CompoundStmt *substmt, QualType T,
740           SourceLocation lp, SourceLocation rp) :
741    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
742
743  CompoundStmt *getSubStmt() { return SubStmt; }
744  const CompoundStmt *getSubStmt() const { return SubStmt; }
745
746  virtual SourceRange getSourceRange() const {
747    return SourceRange(LParenLoc, RParenLoc);
748  }
749
750  virtual void visit(StmtVisitor &Visitor);
751  static bool classof(const Stmt *T) {
752    return T->getStmtClass() == StmtExprClass;
753  }
754  static bool classof(const StmtExpr *) { return true; }
755};
756
757/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
758/// This AST node represents a function that returns 1 if two *types* (not
759/// expressions) are compatible. The result of this built-in function can be
760/// used in integer constant expressions.
761class TypesCompatibleExpr : public Expr {
762  QualType Type1;
763  QualType Type2;
764  SourceLocation BuiltinLoc, RParenLoc;
765public:
766  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
767                      QualType t1, QualType t2, SourceLocation RP) :
768    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
769    BuiltinLoc(BLoc), RParenLoc(RP) {}
770
771  QualType getArgType1() const { return Type1; }
772  QualType getArgType2() const { return Type2; }
773
774  int typesAreCompatible() const { return Type::typesAreCompatible(Type1,Type2); }
775
776  virtual SourceRange getSourceRange() const {
777    return SourceRange(BuiltinLoc, RParenLoc);
778  }
779  virtual void visit(StmtVisitor &Visitor);
780  static bool classof(const Stmt *T) {
781    return T->getStmtClass() == TypesCompatibleExprClass;
782  }
783  static bool classof(const TypesCompatibleExpr *) { return true; }
784};
785
786/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
787/// This AST node is similar to the conditional operator (?:) in C, with
788/// the following exceptions:
789/// - the test expression much be a constant expression.
790/// - the expression returned has it's type unaltered by promotion rules.
791/// - does not evaluate the expression that was not chosen.
792class ChooseExpr : public Expr {
793  Expr *Cond, *LHS, *RHS;  // First, second, and third arguments.
794  SourceLocation BuiltinLoc, RParenLoc;
795public:
796  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
797             SourceLocation RP)
798    : Expr(ChooseExprClass, t),
799      Cond(cond), LHS(lhs), RHS(rhs), BuiltinLoc(BLoc), RParenLoc(RP) {}
800
801  Expr *getCond() { return Cond; }
802  Expr *getLHS() { return LHS; }
803  Expr *getRHS() { return RHS; }
804
805  const Expr *getCond() const { return Cond; }
806  const Expr *getLHS() const { return LHS; }
807  const Expr *getRHS() const { return RHS; }
808
809  virtual SourceRange getSourceRange() const {
810    return SourceRange(BuiltinLoc, RParenLoc);
811  }
812  virtual void visit(StmtVisitor &Visitor);
813  static bool classof(const Stmt *T) {
814    return T->getStmtClass() == ChooseExprClass;
815  }
816  static bool classof(const ChooseExpr *) { return true; }
817};
818
819}  // end namespace clang
820
821#endif
822