Expr.h revision 9fdc8a9aba187772c470e52314fce5d1c5b42647
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  static bool classof(const Stmt *T) {
107    return T->getStmtClass() >= firstExprConstant &&
108           T->getStmtClass() <= lastExprConstant;
109  }
110  static bool classof(const Expr *) { return true; }
111};
112
113//===----------------------------------------------------------------------===//
114// Primary Expressions.
115//===----------------------------------------------------------------------===//
116
117/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
118/// enum, etc.
119class DeclRefExpr : public Expr {
120  Decl *D; // a ValueDecl or EnumConstantDecl
121  SourceLocation Loc;
122public:
123  DeclRefExpr(Decl *d, QualType t, SourceLocation l) :
124    Expr(DeclRefExprClass, t), D(d), Loc(l) {}
125
126  Decl *getDecl() { return D; }
127  const Decl *getDecl() const { return D; }
128  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
129
130
131  static bool classof(const Stmt *T) {
132    return T->getStmtClass() == DeclRefExprClass;
133  }
134  static bool classof(const DeclRefExpr *) { return true; }
135
136  // Iterators
137  virtual child_iterator child_begin();
138  virtual child_iterator child_end();
139};
140
141/// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__.
142class PreDefinedExpr : public Expr {
143public:
144  enum IdentType {
145    Func,
146    Function,
147    PrettyFunction
148  };
149
150private:
151  SourceLocation Loc;
152  IdentType Type;
153public:
154  PreDefinedExpr(SourceLocation l, QualType type, IdentType IT)
155    : Expr(PreDefinedExprClass, type), Loc(l), Type(IT) {}
156
157  IdentType getIdentType() const { return Type; }
158
159  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
160
161  static bool classof(const Stmt *T) {
162    return T->getStmtClass() == PreDefinedExprClass;
163  }
164  static bool classof(const PreDefinedExpr *) { return true; }
165
166  // Iterators
167  virtual child_iterator child_begin();
168  virtual child_iterator child_end();
169};
170
171class IntegerLiteral : public Expr {
172  llvm::APInt Value;
173  SourceLocation Loc;
174public:
175  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
176  // or UnsignedLongLongTy
177  IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
178    : Expr(IntegerLiteralClass, type), Value(V), Loc(l) {
179    assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
180  }
181  const llvm::APInt &getValue() const { return Value; }
182  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
183
184  static bool classof(const Stmt *T) {
185    return T->getStmtClass() == IntegerLiteralClass;
186  }
187  static bool classof(const IntegerLiteral *) { return true; }
188
189  // Iterators
190  virtual child_iterator child_begin();
191  virtual child_iterator child_end();
192};
193
194class CharacterLiteral : public Expr {
195  unsigned Value;
196  SourceLocation Loc;
197public:
198  // type should be IntTy
199  CharacterLiteral(unsigned value, QualType type, SourceLocation l)
200    : Expr(CharacterLiteralClass, type), Value(value), Loc(l) {
201  }
202  SourceLocation getLoc() const { return Loc; }
203
204  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
205
206  unsigned getValue() const { return Value; }
207
208  static bool classof(const Stmt *T) {
209    return T->getStmtClass() == CharacterLiteralClass;
210  }
211  static bool classof(const CharacterLiteral *) { return true; }
212
213  // Iterators
214  virtual child_iterator child_begin();
215  virtual child_iterator child_end();
216};
217
218class FloatingLiteral : public Expr {
219  float Value; // FIXME
220  SourceLocation Loc;
221public:
222  FloatingLiteral(float value, QualType type, SourceLocation l)
223    : Expr(FloatingLiteralClass, type), Value(value), Loc(l) {}
224
225  float getValue() const { return Value; }
226
227  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
228
229  static bool classof(const Stmt *T) {
230    return T->getStmtClass() == FloatingLiteralClass;
231  }
232  static bool classof(const FloatingLiteral *) { return true; }
233
234  // Iterators
235  virtual child_iterator child_begin();
236  virtual child_iterator child_end();
237};
238
239/// StringLiteral - This represents a string literal expression, e.g. "foo"
240/// or L"bar" (wide strings).  The actual string is returned by getStrData()
241/// is NOT null-terminated, and the length of the string is determined by
242/// calling getByteLength().
243class StringLiteral : public Expr {
244  const char *StrData;
245  unsigned ByteLength;
246  bool IsWide;
247  // if the StringLiteral was composed using token pasting, both locations
248  // are needed. If not (the common case), firstTokLoc == lastTokLoc.
249  // FIXME: if space becomes an issue, we should create a sub-class.
250  SourceLocation firstTokLoc, lastTokLoc;
251public:
252  StringLiteral(const char *strData, unsigned byteLength, bool Wide,
253                QualType t, SourceLocation b, SourceLocation e);
254  virtual ~StringLiteral();
255
256  const char *getStrData() const { return StrData; }
257  unsigned getByteLength() const { return ByteLength; }
258  bool isWide() const { return IsWide; }
259
260  virtual SourceRange getSourceRange() const {
261    return SourceRange(firstTokLoc,lastTokLoc);
262  }
263  static bool classof(const Stmt *T) {
264    return T->getStmtClass() == StringLiteralClass;
265  }
266  static bool classof(const StringLiteral *) { return true; }
267
268  // Iterators
269  virtual child_iterator child_begin();
270  virtual child_iterator child_end();
271};
272
273/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
274/// AST node is only formed if full location information is requested.
275class ParenExpr : public Expr {
276  SourceLocation L, R;
277  Expr *Val;
278public:
279  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
280    : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {}
281
282  const Expr *getSubExpr() const { return Val; }
283  Expr *getSubExpr() { return Val; }
284  SourceRange getSourceRange() const { return SourceRange(L, R); }
285
286  static bool classof(const Stmt *T) {
287    return T->getStmtClass() == ParenExprClass;
288  }
289  static bool classof(const ParenExpr *) { return true; }
290
291  // Iterators
292  virtual child_iterator child_begin();
293  virtual child_iterator child_end();
294};
295
296
297/// UnaryOperator - This represents the unary-expression's (except sizeof of
298/// types), the postinc/postdec operators from postfix-expression, and various
299/// extensions.
300class UnaryOperator : public Expr {
301public:
302  // Note that additions to this should also update the StmtVisitor class.
303  enum Opcode {
304    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
305    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
306    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
307    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
308    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
309    SizeOf, AlignOf,  // [C99 6.5.3.4] Sizeof (expr, not type) operator.
310    Real, Imag,       // "__real expr"/"__imag expr" Extension.
311    Extension         // __extension__ marker.
312  };
313private:
314  Expr *Val;
315  Opcode Opc;
316  SourceLocation Loc;
317public:
318
319  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
320    : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {}
321
322  Opcode getOpcode() const { return Opc; }
323  Expr *getSubExpr() const { return Val; }
324
325  /// getOperatorLoc - Return the location of the operator.
326  SourceLocation getOperatorLoc() const { return Loc; }
327
328  /// isPostfix - Return true if this is a postfix operation, like x++.
329  static bool isPostfix(Opcode Op);
330
331  bool isPostfix() const { return isPostfix(Opc); }
332  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
333  bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; }
334  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
335
336  /// getDecl - a recursive routine that derives the base decl for an
337  /// expression. For example, it will return the declaration for "s" from
338  /// the following complex expression "s.zz[2].bb.vv".
339  static bool isAddressable(Expr *e);
340
341  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
342  /// corresponds to, e.g. "sizeof" or "[pre]++"
343  static const char *getOpcodeStr(Opcode Op);
344
345  virtual SourceRange getSourceRange() const {
346    if (isPostfix())
347      return SourceRange(Val->getLocStart(), Loc);
348    else
349      return SourceRange(Loc, Val->getLocEnd());
350  }
351  virtual SourceLocation getExprLoc() const { return Loc; }
352
353  static bool classof(const Stmt *T) {
354    return T->getStmtClass() == UnaryOperatorClass;
355  }
356  static bool classof(const UnaryOperator *) { return true; }
357
358  // Iterators
359  virtual child_iterator child_begin();
360  virtual child_iterator child_end();
361};
362
363/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of
364/// *types*.  sizeof(expr) is handled by UnaryOperator.
365class SizeOfAlignOfTypeExpr : public Expr {
366  bool isSizeof;  // true if sizeof, false if alignof.
367  QualType Ty;
368  SourceLocation OpLoc, RParenLoc;
369public:
370  SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType,
371                        SourceLocation op, SourceLocation rp) :
372    Expr(SizeOfAlignOfTypeExprClass, resultType),
373    isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {}
374
375  bool isSizeOf() const { return isSizeof; }
376  QualType getArgumentType() const { return Ty; }
377
378  SourceLocation getOperatorLoc() const { return OpLoc; }
379  SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); }
380
381  static bool classof(const Stmt *T) {
382    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
383  }
384  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
385
386  // Iterators
387  virtual child_iterator child_begin();
388  virtual child_iterator child_end();
389};
390
391//===----------------------------------------------------------------------===//
392// Postfix Operators.
393//===----------------------------------------------------------------------===//
394
395/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
396class ArraySubscriptExpr : public Expr {
397  enum { LHS, RHS, END_EXPR=2 };
398  Expr* SubExprs[END_EXPR];
399  SourceLocation RBracketLoc;
400public:
401  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
402                     SourceLocation rbracketloc) :
403    Expr(ArraySubscriptExprClass, t),
404    RBracketLoc(rbracketloc) {
405      SubExprs[LHS] = lhs;
406      SubExprs[RHS] = rhs;
407    }
408
409  /// An array access can be written A[4] or 4[A] (both are equivalent).
410  /// - getBase() and getIdx() always present the normalized view: A[4].
411  ///    In this case getBase() returns "A" and getIdx() returns "4".
412  /// - getLHS() and getRHS() present the syntactic view. e.g. for
413  ///    4[A] getLHS() returns "4".
414
415  Expr *getLHS() { return SubExprs[LHS]; }
416  const Expr *getLHS() const { return SubExprs[LHS]; }
417
418  Expr *getRHS() { return SubExprs[RHS]; }
419  const Expr *getRHS() const { return SubExprs[RHS]; }
420
421  Expr *getBase() {
422    return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
423  }
424
425  const Expr *getBase() const {
426    return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
427  }
428
429  Expr *getIdx() {
430    return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
431  }
432
433  const Expr *getIdx() const {
434    return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
435  }
436
437
438  SourceRange getSourceRange() const {
439    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
440  }
441  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
442
443  static bool classof(const Stmt *T) {
444    return T->getStmtClass() == ArraySubscriptExprClass;
445  }
446  static bool classof(const ArraySubscriptExpr *) { return true; }
447
448  // Iterators
449  virtual child_iterator child_begin();
450  virtual child_iterator child_end();
451};
452
453
454/// CallExpr - [C99 6.5.2.2] Function Calls.
455///
456class CallExpr : public Expr {
457  enum { FN=0, ARGS_START=1 };
458  Expr **SubExprs;
459  unsigned NumArgs;
460  SourceLocation RParenLoc;
461public:
462  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
463           SourceLocation rparenloc);
464  ~CallExpr() {
465    delete [] SubExprs;
466  }
467
468  const Expr *getCallee() const { return SubExprs[FN]; }
469  Expr *getCallee() { return SubExprs[FN]; }
470
471  /// getNumArgs - Return the number of actual arguments to this call.
472  ///
473  unsigned getNumArgs() const { return NumArgs; }
474
475  /// getArg - Return the specified argument.
476  Expr *getArg(unsigned Arg) {
477    assert(Arg < NumArgs && "Arg access out of range!");
478    return SubExprs[Arg+ARGS_START];
479  }
480  const Expr *getArg(unsigned Arg) const {
481    assert(Arg < NumArgs && "Arg access out of range!");
482    return SubExprs[Arg+ARGS_START];
483  }
484
485  /// getNumCommas - Return the number of commas that must have been present in
486  /// this function call.
487  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
488
489  bool isBuiltinClassifyType(llvm::APSInt &Result) const;
490
491  SourceRange getSourceRange() const {
492    return SourceRange(getCallee()->getLocStart(), RParenLoc);
493  }
494
495  static bool classof(const Stmt *T) {
496    return T->getStmtClass() == CallExprClass;
497  }
498  static bool classof(const CallExpr *) { return true; }
499
500  // Iterators
501  virtual child_iterator child_begin();
502  virtual child_iterator child_end();
503};
504
505/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
506///
507class MemberExpr : public Expr {
508  Expr *Base;
509  FieldDecl *MemberDecl;
510  SourceLocation MemberLoc;
511  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
512public:
513  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l)
514    : Expr(MemberExprClass, memberdecl->getType()),
515      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
516
517  Expr *getBase() const { return Base; }
518  FieldDecl *getMemberDecl() const { return MemberDecl; }
519  bool isArrow() const { return IsArrow; }
520
521  virtual SourceRange getSourceRange() const {
522    return SourceRange(getBase()->getLocStart(), MemberLoc);
523  }
524  virtual SourceLocation getExprLoc() const { return MemberLoc; }
525
526  static bool classof(const Stmt *T) {
527    return T->getStmtClass() == MemberExprClass;
528  }
529  static bool classof(const MemberExpr *) { return true; }
530
531  // Iterators
532  virtual child_iterator child_begin();
533  virtual child_iterator child_end();
534};
535
536/// OCUVectorElementExpr - This represents access to specific elements of a
537/// vector, and may occur on the left hand side or right hand side.  For example
538/// the following is legal:  "V.xy = V.zw" if V is a 4 element ocu vector.
539///
540class OCUVectorElementExpr : public Expr {
541  Expr *Base;
542  IdentifierInfo &Accessor;
543  SourceLocation AccessorLoc;
544public:
545  enum ElementType {
546    Point,   // xywz
547    Color,   // rgba
548    Texture  // stpq
549  };
550  OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
551                       SourceLocation loc)
552    : Expr(OCUVectorElementExprClass, ty),
553      Base(base), Accessor(accessor), AccessorLoc(loc) {}
554
555  const Expr *getBase() const { return Base; }
556  Expr *getBase() { return Base; }
557
558  IdentifierInfo &getAccessor() const { return Accessor; }
559
560  /// getNumElements - Get the number of components being selected.
561  unsigned getNumElements() const;
562
563  /// getElementType - Determine whether the components of this access are
564  /// "point" "color" or "texture" elements.
565  ElementType getElementType() const;
566
567  /// containsDuplicateElements - Return true if any element access is
568  /// repeated.
569  bool containsDuplicateElements() const;
570
571  /// getEncodedElementAccess - Encode the elements accessed into a bit vector.
572  /// The encoding currently uses 2-bit bitfields, but clients should use the
573  /// accessors below to access them.
574  ///
575  unsigned getEncodedElementAccess() const;
576
577  /// getAccessedFieldNo - Given an encoded value and a result number, return
578  /// the input field number being accessed.
579  static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) {
580    return (EncodedVal >> (Idx*2)) & 3;
581  }
582
583  virtual SourceRange getSourceRange() const {
584    return SourceRange(getBase()->getLocStart(), AccessorLoc);
585  }
586  static bool classof(const Stmt *T) {
587    return T->getStmtClass() == OCUVectorElementExprClass;
588  }
589  static bool classof(const OCUVectorElementExpr *) { return true; }
590
591  // Iterators
592  virtual child_iterator child_begin();
593  virtual child_iterator child_end();
594};
595
596/// CompoundLiteralExpr - [C99 6.5.2.5]
597///
598class CompoundLiteralExpr : public Expr {
599  Expr *Init;
600public:
601  CompoundLiteralExpr(QualType ty, Expr *init) :
602    Expr(CompoundLiteralExprClass, ty), Init(init) {}
603
604  const Expr *getInitializer() const { return Init; }
605  Expr *getInitializer() { return Init; }
606
607  virtual SourceRange getSourceRange() const { return Init->getSourceRange(); }
608
609  static bool classof(const Stmt *T) {
610    return T->getStmtClass() == CompoundLiteralExprClass;
611  }
612  static bool classof(const CompoundLiteralExpr *) { return true; }
613
614  // Iterators
615  virtual child_iterator child_begin();
616  virtual child_iterator child_end();
617};
618
619/// ImplicitCastExpr - Allows us to explicitly represent implicit type
620/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
621/// float->double, short->int, etc.
622///
623class ImplicitCastExpr : public Expr {
624  Expr *Op;
625public:
626  ImplicitCastExpr(QualType ty, Expr *op) :
627    Expr(ImplicitCastExprClass, ty), Op(op) {}
628
629  Expr *getSubExpr() { return Op; }
630  const Expr *getSubExpr() const { return Op; }
631
632  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
633
634  static bool classof(const Stmt *T) {
635    return T->getStmtClass() == ImplicitCastExprClass;
636  }
637  static bool classof(const ImplicitCastExpr *) { return true; }
638
639  // Iterators
640  virtual child_iterator child_begin();
641  virtual child_iterator child_end();
642};
643
644/// CastExpr - [C99 6.5.4] Cast Operators.
645///
646class CastExpr : public Expr {
647  Expr *Op;
648  SourceLocation Loc; // the location of the left paren
649public:
650  CastExpr(QualType ty, Expr *op, SourceLocation l) :
651    Expr(CastExprClass, ty), Op(op), Loc(l) {}
652
653  SourceLocation getLParenLoc() const { return Loc; }
654
655  Expr *getSubExpr() const { return Op; }
656
657  virtual SourceRange getSourceRange() const {
658    return SourceRange(Loc, getSubExpr()->getSourceRange().End());
659  }
660  static bool classof(const Stmt *T) {
661    return T->getStmtClass() == CastExprClass;
662  }
663  static bool classof(const CastExpr *) { return true; }
664
665  // Iterators
666  virtual child_iterator child_begin();
667  virtual child_iterator child_end();
668};
669
670class BinaryOperator : public Expr {
671public:
672  enum Opcode {
673    // Operators listed in order of precedence.
674    // Note that additions to this should also update the StmtVisitor class.
675    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
676    Add, Sub,         // [C99 6.5.6] Additive operators.
677    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
678    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
679    EQ, NE,           // [C99 6.5.9] Equality operators.
680    And,              // [C99 6.5.10] Bitwise AND operator.
681    Xor,              // [C99 6.5.11] Bitwise XOR operator.
682    Or,               // [C99 6.5.12] Bitwise OR operator.
683    LAnd,             // [C99 6.5.13] Logical AND operator.
684    LOr,              // [C99 6.5.14] Logical OR operator.
685    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
686    DivAssign, RemAssign,
687    AddAssign, SubAssign,
688    ShlAssign, ShrAssign,
689    AndAssign, XorAssign,
690    OrAssign,
691    Comma             // [C99 6.5.17] Comma operator.
692  };
693
694  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy)
695    : Expr(BinaryOperatorClass, ResTy), Opc(opc) {
696    SubExprs[LHS] = lhs;
697    SubExprs[RHS] = rhs;
698    assert(!isCompoundAssignmentOp() &&
699           "Use ArithAssignBinaryOperator for compound assignments");
700  }
701
702  Opcode getOpcode() const { return Opc; }
703  Expr *getLHS() const { return SubExprs[LHS]; }
704  Expr *getRHS() const { return SubExprs[RHS]; }
705  virtual SourceRange getSourceRange() const {
706    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
707  }
708
709  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
710  /// corresponds to, e.g. "<<=".
711  static const char *getOpcodeStr(Opcode Op);
712
713  /// predicates to categorize the respective opcodes.
714  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
715  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
716  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
717  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
718  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
719  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
720  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
721  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
722  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
723  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
724
725  static bool classof(const Stmt *T) {
726    return T->getStmtClass() == BinaryOperatorClass;
727  }
728  static bool classof(const BinaryOperator *) { return true; }
729
730  // Iterators
731  virtual child_iterator child_begin();
732  virtual child_iterator child_end();
733
734private:
735  enum { LHS, RHS, END_EXPR };
736  Expr* SubExprs[END_EXPR];
737  Opcode Opc;
738
739protected:
740  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, bool dead)
741    : Expr(BinaryOperatorClass, ResTy), Opc(opc) {
742    SubExprs[LHS] = lhs;
743    SubExprs[RHS] = rhs;
744  }
745};
746
747/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
748/// track of the type the operation is performed in.  Due to the semantics of
749/// these operators, the operands are promoted, the aritmetic performed, an
750/// implicit conversion back to the result type done, then the assignment takes
751/// place.  This captures the intermediate type which the computation is done
752/// in.
753class CompoundAssignOperator : public BinaryOperator {
754  QualType ComputationType;
755public:
756  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
757                         QualType ResType, QualType CompType)
758    : BinaryOperator(lhs, rhs, opc, ResType, true), ComputationType(CompType) {
759    assert(isCompoundAssignmentOp() &&
760           "Only should be used for compound assignments");
761  }
762
763  QualType getComputationType() const { return ComputationType; }
764
765  static bool classof(const CompoundAssignOperator *) { return true; }
766  static bool classof(const BinaryOperator *B) {
767    return B->isCompoundAssignmentOp();
768  }
769  static bool classof(const Stmt *S) {
770    return isa<BinaryOperator>(S) && classof(cast<BinaryOperator>(S));
771  }
772};
773
774/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
775/// GNU "missing LHS" extension is in use.
776///
777class ConditionalOperator : public Expr {
778  enum { COND, LHS, RHS, END_EXPR };
779  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
780public:
781  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
782    : Expr(ConditionalOperatorClass, t) {
783    SubExprs[COND] = cond;
784    SubExprs[LHS] = lhs;
785    SubExprs[RHS] = rhs;
786  }
787
788  Expr *getCond() const { return SubExprs[COND]; }
789  Expr *getLHS() const { return SubExprs[LHS]; }
790  Expr *getRHS() const { return SubExprs[RHS]; }
791
792  virtual SourceRange getSourceRange() const {
793    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
794  }
795  static bool classof(const Stmt *T) {
796    return T->getStmtClass() == ConditionalOperatorClass;
797  }
798  static bool classof(const ConditionalOperator *) { return true; }
799
800  // Iterators
801  virtual child_iterator child_begin();
802  virtual child_iterator child_end();
803};
804
805/// AddrLabelExpr - The GNU address of label extension, representing &&label.
806class AddrLabelExpr : public Expr {
807  SourceLocation AmpAmpLoc, LabelLoc;
808  LabelStmt *Label;
809public:
810  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
811                QualType t)
812    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
813
814  virtual SourceRange getSourceRange() const {
815    return SourceRange(AmpAmpLoc, LabelLoc);
816  }
817
818  LabelStmt *getLabel() const { return Label; }
819
820  static bool classof(const Stmt *T) {
821    return T->getStmtClass() == AddrLabelExprClass;
822  }
823  static bool classof(const AddrLabelExpr *) { return true; }
824
825  // Iterators
826  virtual child_iterator child_begin();
827  virtual child_iterator child_end();
828};
829
830/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
831/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
832/// takes the value of the last subexpression.
833class StmtExpr : public Expr {
834  CompoundStmt *SubStmt;
835  SourceLocation LParenLoc, RParenLoc;
836public:
837  StmtExpr(CompoundStmt *substmt, QualType T,
838           SourceLocation lp, SourceLocation rp) :
839    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
840
841  CompoundStmt *getSubStmt() { return SubStmt; }
842  const CompoundStmt *getSubStmt() const { return SubStmt; }
843
844  virtual SourceRange getSourceRange() const {
845    return SourceRange(LParenLoc, RParenLoc);
846  }
847
848  static bool classof(const Stmt *T) {
849    return T->getStmtClass() == StmtExprClass;
850  }
851  static bool classof(const StmtExpr *) { return true; }
852
853  // Iterators
854  virtual child_iterator child_begin();
855  virtual child_iterator child_end();
856};
857
858/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
859/// This AST node represents a function that returns 1 if two *types* (not
860/// expressions) are compatible. The result of this built-in function can be
861/// used in integer constant expressions.
862class TypesCompatibleExpr : public Expr {
863  QualType Type1;
864  QualType Type2;
865  SourceLocation BuiltinLoc, RParenLoc;
866public:
867  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
868                      QualType t1, QualType t2, SourceLocation RP) :
869    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
870    BuiltinLoc(BLoc), RParenLoc(RP) {}
871
872  QualType getArgType1() const { return Type1; }
873  QualType getArgType2() const { return Type2; }
874
875  int typesAreCompatible() const { return Type::typesAreCompatible(Type1,Type2); }
876
877  virtual SourceRange getSourceRange() const {
878    return SourceRange(BuiltinLoc, RParenLoc);
879  }
880  static bool classof(const Stmt *T) {
881    return T->getStmtClass() == TypesCompatibleExprClass;
882  }
883  static bool classof(const TypesCompatibleExpr *) { return true; }
884
885  // Iterators
886  virtual child_iterator child_begin();
887  virtual child_iterator child_end();
888};
889
890/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
891/// This AST node is similar to the conditional operator (?:) in C, with
892/// the following exceptions:
893/// - the test expression much be a constant expression.
894/// - the expression returned has it's type unaltered by promotion rules.
895/// - does not evaluate the expression that was not chosen.
896class ChooseExpr : public Expr {
897  enum { COND, LHS, RHS, END_EXPR };
898  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
899  SourceLocation BuiltinLoc, RParenLoc;
900public:
901  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
902             SourceLocation RP)
903    : Expr(ChooseExprClass, t),
904      BuiltinLoc(BLoc), RParenLoc(RP) {
905      SubExprs[COND] = cond;
906      SubExprs[LHS] = lhs;
907      SubExprs[RHS] = rhs;
908    }
909
910  Expr *getCond() const { return SubExprs[COND]; }
911  Expr *getLHS() const { return SubExprs[LHS]; }
912  Expr *getRHS() const { return SubExprs[RHS]; }
913
914  virtual SourceRange getSourceRange() const {
915    return SourceRange(BuiltinLoc, RParenLoc);
916  }
917  static bool classof(const Stmt *T) {
918    return T->getStmtClass() == ChooseExprClass;
919  }
920  static bool classof(const ChooseExpr *) { return true; }
921
922  // Iterators
923  virtual child_iterator child_begin();
924  virtual child_iterator child_end();
925};
926
927/// ObjCStringLiteral, used for Objective-C string literals
928/// i.e. @"foo".
929class ObjCStringLiteral : public Expr {
930  StringLiteral *String;
931public:
932  ObjCStringLiteral(StringLiteral *SL, QualType T)
933    : Expr(ObjCStringLiteralClass, T), String(SL) {}
934
935  StringLiteral* getString() { return String; }
936
937  const StringLiteral* getString() const { return String; }
938
939  virtual SourceRange getSourceRange() const {
940    return String->getSourceRange();
941  }
942
943  static bool classof(const Stmt *T) {
944    return T->getStmtClass() == ObjCStringLiteralClass;
945  }
946  static bool classof(const ObjCStringLiteral *) { return true; }
947
948  // Iterators
949  virtual child_iterator child_begin();
950  virtual child_iterator child_end();
951};
952
953/// ObjCEncodeExpr, used for @encode in Objective-C.
954class ObjCEncodeExpr : public Expr {
955  QualType EncType;
956  SourceLocation EncLoc, RParenLoc;
957public:
958  ObjCEncodeExpr(QualType T, QualType ET,
959                 SourceLocation enc, SourceLocation rp)
960    : Expr(ObjCEncodeExprClass, T), EncType(ET), EncLoc(enc), RParenLoc(rp) {}
961
962  SourceRange getSourceRange() const { return SourceRange(EncLoc, RParenLoc); }
963
964  QualType getEncodedType() const { return EncType; }
965
966  static bool classof(const Stmt *T) {
967    return T->getStmtClass() == ObjCEncodeExprClass;
968  }
969  static bool classof(const ObjCEncodeExpr *) { return true; }
970
971  // Iterators
972  virtual child_iterator child_begin();
973  virtual child_iterator child_end();
974};
975
976}  // end namespace clang
977
978#endif
979