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