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