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