Expr.h revision 4ed9d662047bb65315e1fe1b0a4175c87f5fe627
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#include "llvm/ADT/APFloat.h"
22
23namespace clang {
24  class IdentifierInfo;
25  class SelectorInfo;
26  class Decl;
27  class ASTContext;
28
29/// Expr - This represents one expression.  Note that Expr's are subclasses of
30/// Stmt.  This allows an expression to be transparently used any place a Stmt
31/// is required.
32///
33class Expr : public Stmt {
34  QualType TR;
35protected:
36  Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {}
37public:
38  QualType getType() const { return TR; }
39  void setType(QualType t) { TR = t; }
40
41  /// SourceLocation tokens are not useful in isolation - they are low level
42  /// value objects created/interpreted by SourceManager. We assume AST
43  /// clients will have a pointer to the respective SourceManager.
44  virtual SourceRange getSourceRange() const = 0;
45
46  /// getExprLoc - Return the preferred location for the arrow when diagnosing
47  /// a problem with a generic expression.
48  virtual SourceLocation getExprLoc() const { return getLocStart(); }
49
50  /// hasLocalSideEffect - Return true if this immediate expression has side
51  /// effects, not counting any sub-expressions.
52  bool hasLocalSideEffect() const;
53
54  /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
55  /// incomplete type other than void. Nonarray expressions that can be lvalues:
56  ///  - name, where name must be a variable
57  ///  - e[i]
58  ///  - (e), where e must be an lvalue
59  ///  - e.name, where e must be an lvalue
60  ///  - e->name
61  ///  - *e, the type of e cannot be a function type
62  ///  - string-constant
63  ///  - reference type [C++ [expr]]
64  ///
65  enum isLvalueResult {
66    LV_Valid,
67    LV_NotObjectType,
68    LV_IncompleteVoidType,
69    LV_DuplicateVectorComponents,
70    LV_InvalidExpression
71  };
72  isLvalueResult isLvalue() const;
73
74  /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
75  /// does not have an incomplete type, does not have a const-qualified type,
76  /// and if it is a structure or union, does not have any member (including,
77  /// recursively, any member or element of all contained aggregates or unions)
78  /// with a const-qualified type.
79  enum isModifiableLvalueResult {
80    MLV_Valid,
81    MLV_NotObjectType,
82    MLV_IncompleteVoidType,
83    MLV_DuplicateVectorComponents,
84    MLV_InvalidExpression,
85    MLV_IncompleteType,
86    MLV_ConstQualified,
87    MLV_ArrayType
88  };
89  isModifiableLvalueResult isModifiableLvalue() const;
90
91  bool isNullPointerConstant(ASTContext &Ctx) const;
92
93  /// isIntegerConstantExpr - Return true if this expression is a valid integer
94  /// constant expression, and, if so, return its value in Result.  If not a
95  /// valid i-c-e, return false and fill in Loc (if specified) with the location
96  /// of the invalid expression.
97  bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
98                             SourceLocation *Loc = 0,
99                             bool isEvaluated = true) const;
100  bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
101    llvm::APSInt X(32);
102    return isIntegerConstantExpr(X, Ctx, Loc);
103  }
104  /// isConstantExpr - Return true if this expression is a valid constant expr.
105  bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const;
106
107  static bool classof(const Stmt *T) {
108    return T->getStmtClass() >= firstExprConstant &&
109           T->getStmtClass() <= lastExprConstant;
110  }
111  static bool classof(const Expr *) { return true; }
112};
113
114//===----------------------------------------------------------------------===//
115// Primary Expressions.
116//===----------------------------------------------------------------------===//
117
118/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
119/// enum, etc.
120class DeclRefExpr : public Expr {
121  ValueDecl *D;
122  SourceLocation Loc;
123public:
124  DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) :
125    Expr(DeclRefExprClass, t), D(d), Loc(l) {}
126
127  ValueDecl *getDecl() { return D; }
128  const ValueDecl *getDecl() const { return D; }
129  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
130
131
132  static bool classof(const Stmt *T) {
133    return T->getStmtClass() == DeclRefExprClass;
134  }
135  static bool classof(const DeclRefExpr *) { return true; }
136
137  // Iterators
138  virtual child_iterator child_begin();
139  virtual child_iterator child_end();
140};
141
142/// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__.
143class PreDefinedExpr : public Expr {
144public:
145  enum IdentType {
146    Func,
147    Function,
148    PrettyFunction
149  };
150
151private:
152  SourceLocation Loc;
153  IdentType Type;
154public:
155  PreDefinedExpr(SourceLocation l, QualType type, IdentType IT)
156    : Expr(PreDefinedExprClass, type), Loc(l), Type(IT) {}
157
158  IdentType getIdentType() const { return Type; }
159
160  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
161
162  static bool classof(const Stmt *T) {
163    return T->getStmtClass() == PreDefinedExprClass;
164  }
165  static bool classof(const PreDefinedExpr *) { return true; }
166
167  // Iterators
168  virtual child_iterator child_begin();
169  virtual child_iterator child_end();
170};
171
172class IntegerLiteral : public Expr {
173  llvm::APInt Value;
174  SourceLocation Loc;
175public:
176  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
177  // or UnsignedLongLongTy
178  IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
179    : Expr(IntegerLiteralClass, type), Value(V), Loc(l) {
180    assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
181  }
182  const llvm::APInt &getValue() const { return Value; }
183  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
184
185  static bool classof(const Stmt *T) {
186    return T->getStmtClass() == IntegerLiteralClass;
187  }
188  static bool classof(const IntegerLiteral *) { return true; }
189
190  // Iterators
191  virtual child_iterator child_begin();
192  virtual child_iterator child_end();
193};
194
195class CharacterLiteral : public Expr {
196  unsigned Value;
197  SourceLocation Loc;
198public:
199  // type should be IntTy
200  CharacterLiteral(unsigned value, QualType type, SourceLocation l)
201    : Expr(CharacterLiteralClass, type), Value(value), Loc(l) {
202  }
203  SourceLocation getLoc() const { return Loc; }
204
205  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
206
207  unsigned getValue() const { return Value; }
208
209  static bool classof(const Stmt *T) {
210    return T->getStmtClass() == CharacterLiteralClass;
211  }
212  static bool classof(const CharacterLiteral *) { return true; }
213
214  // Iterators
215  virtual child_iterator child_begin();
216  virtual child_iterator child_end();
217};
218
219class FloatingLiteral : public Expr {
220  llvm::APFloat Value;
221  SourceLocation Loc;
222public:
223  FloatingLiteral(const llvm::APFloat &V, QualType Type, SourceLocation L)
224    : Expr(FloatingLiteralClass, Type), Value(V), Loc(L) {}
225
226  const llvm::APFloat &getValue() const { return Value; }
227
228  /// getValueAsDouble - This returns the value as an inaccurate double.  Note
229  /// that this may cause loss of precision, but is useful for debugging dumps
230  /// etc.
231  double getValueAsDouble() const {
232    // FIXME: We need something for long double here.
233    if (cast<BuiltinType>(getType())->getKind() == BuiltinType::Float)
234      return Value.convertToFloat();
235    else
236      return Value.convertToDouble();
237  }
238
239  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
240
241  static bool classof(const Stmt *T) {
242    return T->getStmtClass() == FloatingLiteralClass;
243  }
244  static bool classof(const FloatingLiteral *) { return true; }
245
246  // Iterators
247  virtual child_iterator child_begin();
248  virtual child_iterator child_end();
249};
250
251/// ImaginaryLiteral - We support imaginary integer and floating point literals,
252/// like "1.0i".  We represent these as a wrapper around FloatingLiteral and
253/// IntegerLiteral classes.  Instances of this class always have a Complex type
254/// whose element type matches the subexpression.
255///
256class ImaginaryLiteral : public Expr {
257  Expr *Val;
258public:
259  ImaginaryLiteral(Expr *val, QualType Ty)
260    : Expr(ImaginaryLiteralClass, Ty), Val(val) {}
261
262  const Expr *getSubExpr() const { return Val; }
263  Expr *getSubExpr() { return Val; }
264
265  virtual SourceRange getSourceRange() const { return Val->getSourceRange(); }
266  static bool classof(const Stmt *T) {
267    return T->getStmtClass() == ImaginaryLiteralClass;
268  }
269  static bool classof(const ImaginaryLiteral *) { return true; }
270
271  // Iterators
272  virtual child_iterator child_begin();
273  virtual child_iterator child_end();
274};
275
276/// StringLiteral - This represents a string literal expression, e.g. "foo"
277/// or L"bar" (wide strings).  The actual string is returned by getStrData()
278/// is NOT null-terminated, and the length of the string is determined by
279/// calling getByteLength().
280class StringLiteral : public Expr {
281  const char *StrData;
282  unsigned ByteLength;
283  bool IsWide;
284  // if the StringLiteral was composed using token pasting, both locations
285  // are needed. If not (the common case), firstTokLoc == lastTokLoc.
286  // FIXME: if space becomes an issue, we should create a sub-class.
287  SourceLocation firstTokLoc, lastTokLoc;
288public:
289  StringLiteral(const char *strData, unsigned byteLength, bool Wide,
290                QualType t, SourceLocation b, SourceLocation e);
291  virtual ~StringLiteral();
292
293  const char *getStrData() const { return StrData; }
294  unsigned getByteLength() const { return ByteLength; }
295  bool isWide() const { return IsWide; }
296
297  virtual SourceRange getSourceRange() const {
298    return SourceRange(firstTokLoc,lastTokLoc);
299  }
300  static bool classof(const Stmt *T) {
301    return T->getStmtClass() == StringLiteralClass;
302  }
303  static bool classof(const StringLiteral *) { return true; }
304
305  // Iterators
306  virtual child_iterator child_begin();
307  virtual child_iterator child_end();
308};
309
310/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
311/// AST node is only formed if full location information is requested.
312class ParenExpr : public Expr {
313  SourceLocation L, R;
314  Expr *Val;
315public:
316  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
317    : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {}
318
319  const Expr *getSubExpr() const { return Val; }
320  Expr *getSubExpr() { return Val; }
321  SourceRange getSourceRange() const { return SourceRange(L, R); }
322
323  static bool classof(const Stmt *T) {
324    return T->getStmtClass() == ParenExprClass;
325  }
326  static bool classof(const ParenExpr *) { return true; }
327
328  // Iterators
329  virtual child_iterator child_begin();
330  virtual child_iterator child_end();
331};
332
333
334/// UnaryOperator - This represents the unary-expression's (except sizeof of
335/// types), the postinc/postdec operators from postfix-expression, and various
336/// extensions.
337///
338/// Notes on various nodes:
339///
340/// Real/Imag - These return the real/imag part of a complex operand.  If
341///   applied to a non-complex value, the former returns its operand and the
342///   later returns zero in the type of the operand.
343///
344/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose
345///   subexpression is a compound literal with the various MemberExpr and
346///   ArraySubscriptExpr's applied to it.
347///
348class UnaryOperator : public Expr {
349public:
350  // Note that additions to this should also update the StmtVisitor class.
351  enum Opcode {
352    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
353    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
354    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
355    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
356    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
357    SizeOf, AlignOf,  // [C99 6.5.3.4] Sizeof (expr, not type) operator.
358    Real, Imag,       // "__real expr"/"__imag expr" Extension.
359    Extension,        // __extension__ marker.
360    OffsetOf          // __builtin_offsetof
361  };
362private:
363  Expr *Val;
364  Opcode Opc;
365  SourceLocation Loc;
366public:
367
368  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
369    : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {}
370
371  Opcode getOpcode() const { return Opc; }
372  Expr *getSubExpr() const { return Val; }
373
374  /// getOperatorLoc - Return the location of the operator.
375  SourceLocation getOperatorLoc() const { return Loc; }
376
377  /// isPostfix - Return true if this is a postfix operation, like x++.
378  static bool isPostfix(Opcode Op);
379
380  bool isPostfix() const { return isPostfix(Opc); }
381  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
382  bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; }
383  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
384
385  /// getDecl - a recursive routine that derives the base decl for an
386  /// expression. For example, it will return the declaration for "s" from
387  /// the following complex expression "s.zz[2].bb.vv".
388  static bool isAddressable(Expr *e);
389
390  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
391  /// corresponds to, e.g. "sizeof" or "[pre]++"
392  static const char *getOpcodeStr(Opcode Op);
393
394  virtual SourceRange getSourceRange() const {
395    if (isPostfix())
396      return SourceRange(Val->getLocStart(), Loc);
397    else
398      return SourceRange(Loc, Val->getLocEnd());
399  }
400  virtual SourceLocation getExprLoc() const { return Loc; }
401
402  static bool classof(const Stmt *T) {
403    return T->getStmtClass() == UnaryOperatorClass;
404  }
405  static bool classof(const UnaryOperator *) { return true; }
406
407  // Iterators
408  virtual child_iterator child_begin();
409  virtual child_iterator child_end();
410};
411
412/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of
413/// *types*.  sizeof(expr) is handled by UnaryOperator.
414class SizeOfAlignOfTypeExpr : public Expr {
415  bool isSizeof;  // true if sizeof, false if alignof.
416  QualType Ty;
417  SourceLocation OpLoc, RParenLoc;
418public:
419  SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType,
420                        SourceLocation op, SourceLocation rp) :
421    Expr(SizeOfAlignOfTypeExprClass, resultType),
422    isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {}
423
424  bool isSizeOf() const { return isSizeof; }
425  QualType getArgumentType() const { return Ty; }
426
427  SourceLocation getOperatorLoc() const { return OpLoc; }
428  SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); }
429
430  static bool classof(const Stmt *T) {
431    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
432  }
433  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
434
435  // Iterators
436  virtual child_iterator child_begin();
437  virtual child_iterator child_end();
438};
439
440//===----------------------------------------------------------------------===//
441// Postfix Operators.
442//===----------------------------------------------------------------------===//
443
444/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
445class ArraySubscriptExpr : public Expr {
446  enum { LHS, RHS, END_EXPR=2 };
447  Expr* SubExprs[END_EXPR];
448  SourceLocation RBracketLoc;
449public:
450  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
451                     SourceLocation rbracketloc)
452  : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) {
453    SubExprs[LHS] = lhs;
454    SubExprs[RHS] = rhs;
455  }
456
457  /// An array access can be written A[4] or 4[A] (both are equivalent).
458  /// - getBase() and getIdx() always present the normalized view: A[4].
459  ///    In this case getBase() returns "A" and getIdx() returns "4".
460  /// - getLHS() and getRHS() present the syntactic view. e.g. for
461  ///    4[A] getLHS() returns "4".
462
463  Expr *getLHS() { return SubExprs[LHS]; }
464  const Expr *getLHS() const { return SubExprs[LHS]; }
465
466  Expr *getRHS() { return SubExprs[RHS]; }
467  const Expr *getRHS() const { return SubExprs[RHS]; }
468
469  Expr *getBase() {
470    return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
471  }
472
473  const Expr *getBase() const {
474    return (getLHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
475  }
476
477  Expr *getIdx() {
478    return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
479  }
480
481  const Expr *getIdx() const {
482    return (getLHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
483  }
484
485
486  SourceRange getSourceRange() const {
487    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
488  }
489  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
490
491  static bool classof(const Stmt *T) {
492    return T->getStmtClass() == ArraySubscriptExprClass;
493  }
494  static bool classof(const ArraySubscriptExpr *) { return true; }
495
496  // Iterators
497  virtual child_iterator child_begin();
498  virtual child_iterator child_end();
499};
500
501
502/// CallExpr - [C99 6.5.2.2] Function Calls.
503///
504class CallExpr : public Expr {
505  enum { FN=0, ARGS_START=1 };
506  Expr **SubExprs;
507  unsigned NumArgs;
508  SourceLocation RParenLoc;
509public:
510  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
511           SourceLocation rparenloc);
512  ~CallExpr() {
513    delete [] SubExprs;
514  }
515
516  const Expr *getCallee() const { return SubExprs[FN]; }
517  Expr *getCallee() { return SubExprs[FN]; }
518
519  /// getNumArgs - Return the number of actual arguments to this call.
520  ///
521  unsigned getNumArgs() const { return NumArgs; }
522
523  /// getArg - Return the specified argument.
524  Expr *getArg(unsigned Arg) {
525    assert(Arg < NumArgs && "Arg access out of range!");
526    return SubExprs[Arg+ARGS_START];
527  }
528  const Expr *getArg(unsigned Arg) const {
529    assert(Arg < NumArgs && "Arg access out of range!");
530    return SubExprs[Arg+ARGS_START];
531  }
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().End());
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  int typesAreCompatible() const {return Type::typesAreCompatible(Type1,Type2);}
932
933  virtual SourceRange getSourceRange() const {
934    return SourceRange(BuiltinLoc, RParenLoc);
935  }
936  static bool classof(const Stmt *T) {
937    return T->getStmtClass() == TypesCompatibleExprClass;
938  }
939  static bool classof(const TypesCompatibleExpr *) { return true; }
940
941  // Iterators
942  virtual child_iterator child_begin();
943  virtual child_iterator child_end();
944};
945
946/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
947/// This AST node is similar to the conditional operator (?:) in C, with
948/// the following exceptions:
949/// - the test expression much be a constant expression.
950/// - the expression returned has it's type unaltered by promotion rules.
951/// - does not evaluate the expression that was not chosen.
952class ChooseExpr : public Expr {
953  enum { COND, LHS, RHS, END_EXPR };
954  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
955  SourceLocation BuiltinLoc, RParenLoc;
956public:
957  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
958             SourceLocation RP)
959    : Expr(ChooseExprClass, t),
960      BuiltinLoc(BLoc), RParenLoc(RP) {
961      SubExprs[COND] = cond;
962      SubExprs[LHS] = lhs;
963      SubExprs[RHS] = rhs;
964    }
965
966  Expr *getCond() const { return SubExprs[COND]; }
967  Expr *getLHS() const { return SubExprs[LHS]; }
968  Expr *getRHS() const { return SubExprs[RHS]; }
969
970  virtual SourceRange getSourceRange() const {
971    return SourceRange(BuiltinLoc, RParenLoc);
972  }
973  static bool classof(const Stmt *T) {
974    return T->getStmtClass() == ChooseExprClass;
975  }
976  static bool classof(const ChooseExpr *) { return true; }
977
978  // Iterators
979  virtual child_iterator child_begin();
980  virtual child_iterator child_end();
981};
982
983/// InitListExpr, used for struct and array initializers.
984class InitListExpr : public Expr {
985  Expr **InitExprs;
986  unsigned NumInits;
987  SourceLocation LBraceLoc, RBraceLoc;
988public:
989  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
990               SourceLocation rbraceloc);
991  ~InitListExpr() {
992    delete [] InitExprs;
993  }
994
995  unsigned getNumInits() const { return NumInits; }
996
997  const Expr* getInit(unsigned Init) const {
998    assert(Init < NumInits && "Initializer access out of range!");
999    return InitExprs[Init];
1000  }
1001
1002  Expr* getInit(unsigned Init) {
1003    assert(Init < NumInits && "Initializer access out of range!");
1004    return InitExprs[Init];
1005  }
1006
1007  void setInit(unsigned Init, Expr *expr) {
1008    assert(Init < NumInits && "Initializer access out of range!");
1009    InitExprs[Init] = expr;
1010  }
1011
1012  virtual SourceRange getSourceRange() const {
1013    return SourceRange(LBraceLoc, RBraceLoc);
1014  }
1015  static bool classof(const Stmt *T) {
1016    return T->getStmtClass() == InitListExprClass;
1017  }
1018  static bool classof(const InitListExpr *) { return true; }
1019
1020  // Iterators
1021  virtual child_iterator child_begin();
1022  virtual child_iterator child_end();
1023};
1024
1025/// ObjCStringLiteral, used for Objective-C string literals
1026/// i.e. @"foo".
1027class ObjCStringLiteral : public Expr {
1028  StringLiteral *String;
1029public:
1030  ObjCStringLiteral(StringLiteral *SL, QualType T)
1031    : Expr(ObjCStringLiteralClass, T), String(SL) {}
1032
1033  StringLiteral* getString() { return String; }
1034
1035  const StringLiteral* getString() const { return String; }
1036
1037  virtual SourceRange getSourceRange() const {
1038    return String->getSourceRange();
1039  }
1040
1041  static bool classof(const Stmt *T) {
1042    return T->getStmtClass() == ObjCStringLiteralClass;
1043  }
1044  static bool classof(const ObjCStringLiteral *) { return true; }
1045
1046  // Iterators
1047  virtual child_iterator child_begin();
1048  virtual child_iterator child_end();
1049};
1050
1051/// ObjCEncodeExpr, used for @encode in Objective-C.
1052class ObjCEncodeExpr : public Expr {
1053  QualType EncType;
1054  SourceLocation EncLoc, RParenLoc;
1055public:
1056  ObjCEncodeExpr(QualType T, QualType ET,
1057                 SourceLocation enc, SourceLocation rp)
1058    : Expr(ObjCEncodeExprClass, T), EncType(ET), EncLoc(enc), RParenLoc(rp) {}
1059
1060  SourceRange getSourceRange() const { return SourceRange(EncLoc, RParenLoc); }
1061
1062  QualType getEncodedType() const { return EncType; }
1063
1064  static bool classof(const Stmt *T) {
1065    return T->getStmtClass() == ObjCEncodeExprClass;
1066  }
1067  static bool classof(const ObjCEncodeExpr *) { return true; }
1068
1069  // Iterators
1070  virtual child_iterator child_begin();
1071  virtual child_iterator child_end();
1072};
1073
1074class ObjCMessageExpr : public Expr {
1075  enum { RECEIVER=0, ARGS_START=1 };
1076
1077  Expr **SubExprs;
1078
1079  // A unigue name for this message.
1080  SelectorInfo *Selector;
1081
1082  IdentifierInfo *ClassName; // optional - 0 for instance messages.
1083
1084  SourceLocation LBracloc, RBracloc;
1085public:
1086  // constructor for class messages.
1087  // FIXME: clsName should be typed to ObjCInterfaceType
1088  ObjCMessageExpr(IdentifierInfo *clsName, SelectorInfo *selInfo,
1089                  QualType retType, SourceLocation LBrac, SourceLocation RBrac,
1090                  Expr **ArgExprs);
1091  // constructor for instance messages.
1092  ObjCMessageExpr(Expr *receiver, SelectorInfo *selInfo,
1093                  QualType retType, SourceLocation LBrac, SourceLocation RBrac,
1094                  Expr **ArgExprs);
1095  ~ObjCMessageExpr() {
1096    delete [] SubExprs;
1097  }
1098
1099  const Expr *getReceiver() const { return SubExprs[RECEIVER]; }
1100  Expr *getReceiver() { return SubExprs[RECEIVER]; }
1101
1102  /// getNumArgs - Return the number of actual arguments to this call.
1103  ///
1104  unsigned getNumArgs() const;
1105
1106  /// getArg - Return the specified argument.
1107  Expr *getArg(unsigned Arg);
1108  const Expr *getArg(unsigned Arg) const;
1109
1110  SourceRange getSourceRange() const { return SourceRange(LBracloc, RBracloc); }
1111
1112  static bool classof(const Stmt *T) {
1113    return T->getStmtClass() == ObjCMessageExprClass;
1114  }
1115  static bool classof(const ObjCMessageExpr *) { return true; }
1116
1117  // Iterators
1118  virtual child_iterator child_begin();
1119  virtual child_iterator child_end();
1120};
1121
1122}  // end namespace clang
1123
1124#endif
1125