Expr.h revision 3fd1dfa22f2642c627ad2139b048b99fc5507ef3
1//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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/AST/DeclObjC.h"
21#include "clang/Basic/IdentifierTable.h"
22#include "llvm/ADT/APSInt.h"
23#include "llvm/ADT/APFloat.h"
24
25namespace clang {
26  class IdentifierInfo;
27  class Selector;
28  class Decl;
29  class ASTContext;
30
31/// Expr - This represents one expression.  Note that Expr's are subclasses of
32/// Stmt.  This allows an expression to be transparently used any place a Stmt
33/// is required.
34///
35class Expr : public Stmt {
36  QualType TR;
37protected:
38  Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {}
39public:
40  QualType getType() const { return TR; }
41  void setType(QualType t) { TR = t; }
42
43  /// SourceLocation tokens are not useful in isolation - they are low level
44  /// value objects created/interpreted by SourceManager. We assume AST
45  /// clients will have a pointer to the respective SourceManager.
46  virtual SourceRange getSourceRange() const = 0;
47
48  /// getExprLoc - Return the preferred location for the arrow when diagnosing
49  /// a problem with a generic expression.
50  virtual SourceLocation getExprLoc() const { return getLocStart(); }
51
52  /// hasLocalSideEffect - Return true if this immediate expression has side
53  /// effects, not counting any sub-expressions.
54  bool hasLocalSideEffect() const;
55
56  /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
57  /// incomplete type other than void. Nonarray expressions that can be lvalues:
58  ///  - name, where name must be a variable
59  ///  - e[i]
60  ///  - (e), where e must be an lvalue
61  ///  - e.name, where e must be an lvalue
62  ///  - e->name
63  ///  - *e, the type of e cannot be a function type
64  ///  - string-constant
65  ///  - reference type [C++ [expr]]
66  ///
67  enum isLvalueResult {
68    LV_Valid,
69    LV_NotObjectType,
70    LV_IncompleteVoidType,
71    LV_DuplicateVectorComponents,
72    LV_InvalidExpression
73  };
74  isLvalueResult isLvalue() const;
75
76  /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
77  /// does not have an incomplete type, does not have a const-qualified type,
78  /// and if it is a structure or union, does not have any member (including,
79  /// recursively, any member or element of all contained aggregates or unions)
80  /// with a const-qualified type.
81  enum isModifiableLvalueResult {
82    MLV_Valid,
83    MLV_NotObjectType,
84    MLV_IncompleteVoidType,
85    MLV_DuplicateVectorComponents,
86    MLV_InvalidExpression,
87    MLV_IncompleteType,
88    MLV_ConstQualified,
89    MLV_ArrayType
90  };
91  isModifiableLvalueResult isModifiableLvalue() const;
92
93  bool isNullPointerConstant(ASTContext &Ctx) const;
94
95  /// isIntegerConstantExpr - Return true if this expression is a valid integer
96  /// constant expression, and, if so, return its value in Result.  If not a
97  /// valid i-c-e, return false and fill in Loc (if specified) with the location
98  /// of the invalid expression.
99  bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
100                             SourceLocation *Loc = 0,
101                             bool isEvaluated = true) const;
102  bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
103    llvm::APSInt X(32);
104    return isIntegerConstantExpr(X, Ctx, Loc);
105  }
106  /// isConstantExpr - Return true if this expression is a valid constant expr.
107  bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const;
108
109  /// hasStaticStorage - Return true if this expression has static storage
110  /// duration.  This means that the address of this expression is a link-time
111  /// constant.
112  bool hasStaticStorage() const;
113
114  /// IgnoreParens - Ignore parentheses.  If this Expr is a ParenExpr, return
115  ///  its subexpression.  If that subexpression is also a ParenExpr,
116  ///  then this method recursively returns its subexpression, and so forth.
117  ///  Otherwise, the method returns the current Expr.
118  Expr* IgnoreParens();
119
120  const Expr* IgnoreParens() const {
121    return const_cast<Expr*>(this)->IgnoreParens();
122  }
123
124  static bool classof(const Stmt *T) {
125    return T->getStmtClass() >= firstExprConstant &&
126           T->getStmtClass() <= lastExprConstant;
127  }
128  static bool classof(const Expr *) { return true; }
129
130  static inline Expr* Create(llvm::Deserializer& D) {
131    return cast<Expr>(Stmt::Create(D));
132  }
133};
134
135//===----------------------------------------------------------------------===//
136// Primary Expressions.
137//===----------------------------------------------------------------------===//
138
139/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
140/// enum, etc.
141class DeclRefExpr : public Expr {
142  ValueDecl *D;
143  SourceLocation Loc;
144public:
145  DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) :
146    Expr(DeclRefExprClass, t), D(d), Loc(l) {}
147
148  ValueDecl *getDecl() { return D; }
149  const ValueDecl *getDecl() const { return D; }
150  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
151
152
153  static bool classof(const Stmt *T) {
154    return T->getStmtClass() == DeclRefExprClass;
155  }
156  static bool classof(const DeclRefExpr *) { return true; }
157
158  // Iterators
159  virtual child_iterator child_begin();
160  virtual child_iterator child_end();
161
162  virtual void EmitImpl(llvm::Serializer& S) const;
163  static DeclRefExpr* CreateImpl(llvm::Deserializer& D);
164};
165
166/// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__.
167class PreDefinedExpr : public Expr {
168public:
169  enum IdentType {
170    Func,
171    Function,
172    PrettyFunction
173  };
174
175private:
176  SourceLocation Loc;
177  IdentType Type;
178public:
179  PreDefinedExpr(SourceLocation l, QualType type, IdentType IT)
180    : Expr(PreDefinedExprClass, type), Loc(l), Type(IT) {}
181
182  IdentType getIdentType() const { return Type; }
183
184  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
185
186  static bool classof(const Stmt *T) {
187    return T->getStmtClass() == PreDefinedExprClass;
188  }
189  static bool classof(const PreDefinedExpr *) { return true; }
190
191  // Iterators
192  virtual child_iterator child_begin();
193  virtual child_iterator child_end();
194
195  virtual void EmitImpl(llvm::Serializer& S) const;
196  static PreDefinedExpr* CreateImpl(llvm::Deserializer& D);
197};
198
199class IntegerLiteral : public Expr {
200  llvm::APInt Value;
201  SourceLocation Loc;
202public:
203  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
204  // or UnsignedLongLongTy
205  IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
206    : Expr(IntegerLiteralClass, type), Value(V), Loc(l) {
207    assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
208  }
209  const llvm::APInt &getValue() const { return Value; }
210  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
211
212  static bool classof(const Stmt *T) {
213    return T->getStmtClass() == IntegerLiteralClass;
214  }
215  static bool classof(const IntegerLiteral *) { return true; }
216
217  // Iterators
218  virtual child_iterator child_begin();
219  virtual child_iterator child_end();
220
221  virtual void EmitImpl(llvm::Serializer& S) const;
222  static IntegerLiteral* CreateImpl(llvm::Deserializer& D);
223};
224
225class CharacterLiteral : public Expr {
226  unsigned Value;
227  SourceLocation Loc;
228public:
229  // type should be IntTy
230  CharacterLiteral(unsigned value, QualType type, SourceLocation l)
231    : Expr(CharacterLiteralClass, type), Value(value), Loc(l) {
232  }
233  SourceLocation getLoc() const { return Loc; }
234
235  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
236
237  unsigned getValue() const { return Value; }
238
239  static bool classof(const Stmt *T) {
240    return T->getStmtClass() == CharacterLiteralClass;
241  }
242  static bool classof(const CharacterLiteral *) { return true; }
243
244  // Iterators
245  virtual child_iterator child_begin();
246  virtual child_iterator child_end();
247
248  virtual void EmitImpl(llvm::Serializer& S) const;
249  static CharacterLiteral* CreateImpl(llvm::Deserializer& D);
250};
251
252class FloatingLiteral : public Expr {
253  llvm::APFloat Value;
254  bool IsExact : 1;
255  SourceLocation Loc;
256public:
257  FloatingLiteral(const llvm::APFloat &V, bool* isexact,
258                  QualType Type, SourceLocation L)
259    : Expr(FloatingLiteralClass, Type), Value(V), IsExact(*isexact), Loc(L) {}
260
261  const llvm::APFloat &getValue() const { return Value; }
262
263  bool isExact() const { return IsExact; }
264
265  /// getValueAsDouble - This returns the value as an inaccurate double.  Note
266  /// that this may cause loss of precision, but is useful for debugging dumps
267  /// etc.
268  double getValueAsDouble() const {
269    // FIXME: We need something for long double here.
270    if (cast<BuiltinType>(getType())->getKind() == BuiltinType::Float)
271      return Value.convertToFloat();
272    else
273      return Value.convertToDouble();
274  }
275
276  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
277
278  static bool classof(const Stmt *T) {
279    return T->getStmtClass() == FloatingLiteralClass;
280  }
281  static bool classof(const FloatingLiteral *) { return true; }
282
283  // Iterators
284  virtual child_iterator child_begin();
285  virtual child_iterator child_end();
286
287  virtual void EmitImpl(llvm::Serializer& S) const;
288  static FloatingLiteral* CreateImpl(llvm::Deserializer& D);
289};
290
291/// ImaginaryLiteral - We support imaginary integer and floating point literals,
292/// like "1.0i".  We represent these as a wrapper around FloatingLiteral and
293/// IntegerLiteral classes.  Instances of this class always have a Complex type
294/// whose element type matches the subexpression.
295///
296class ImaginaryLiteral : public Expr {
297  Expr *Val;
298public:
299  ImaginaryLiteral(Expr *val, QualType Ty)
300    : Expr(ImaginaryLiteralClass, Ty), Val(val) {}
301
302  const Expr *getSubExpr() const { return Val; }
303  Expr *getSubExpr() { return Val; }
304
305  virtual SourceRange getSourceRange() const { return Val->getSourceRange(); }
306  static bool classof(const Stmt *T) {
307    return T->getStmtClass() == ImaginaryLiteralClass;
308  }
309  static bool classof(const ImaginaryLiteral *) { return true; }
310
311  // Iterators
312  virtual child_iterator child_begin();
313  virtual child_iterator child_end();
314
315  virtual void EmitImpl(llvm::Serializer& S) const;
316  static ImaginaryLiteral* CreateImpl(llvm::Deserializer& D);
317};
318
319/// StringLiteral - This represents a string literal expression, e.g. "foo"
320/// or L"bar" (wide strings).  The actual string is returned by getStrData()
321/// is NOT null-terminated, and the length of the string is determined by
322/// calling getByteLength().
323class StringLiteral : public Expr {
324  const char *StrData;
325  unsigned ByteLength;
326  bool IsWide;
327  // if the StringLiteral was composed using token pasting, both locations
328  // are needed. If not (the common case), firstTokLoc == lastTokLoc.
329  // FIXME: if space becomes an issue, we should create a sub-class.
330  SourceLocation firstTokLoc, lastTokLoc;
331public:
332  StringLiteral(const char *strData, unsigned byteLength, bool Wide,
333                QualType t, SourceLocation b, SourceLocation e);
334  virtual ~StringLiteral();
335
336  const char *getStrData() const { return StrData; }
337  unsigned getByteLength() const { return ByteLength; }
338  bool isWide() const { return IsWide; }
339
340  virtual SourceRange getSourceRange() const {
341    return SourceRange(firstTokLoc,lastTokLoc);
342  }
343  static bool classof(const Stmt *T) {
344    return T->getStmtClass() == StringLiteralClass;
345  }
346  static bool classof(const StringLiteral *) { return true; }
347
348  // Iterators
349  virtual child_iterator child_begin();
350  virtual child_iterator child_end();
351
352  virtual void EmitImpl(llvm::Serializer& S) const;
353  static StringLiteral* CreateImpl(llvm::Deserializer& D);
354};
355
356/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
357/// AST node is only formed if full location information is requested.
358class ParenExpr : public Expr {
359  SourceLocation L, R;
360  Expr *Val;
361public:
362  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
363    : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {}
364
365  const Expr *getSubExpr() const { return Val; }
366  Expr *getSubExpr() { return Val; }
367  SourceRange getSourceRange() const { return SourceRange(L, R); }
368
369  static bool classof(const Stmt *T) {
370    return T->getStmtClass() == ParenExprClass;
371  }
372  static bool classof(const ParenExpr *) { return true; }
373
374  // Iterators
375  virtual child_iterator child_begin();
376  virtual child_iterator child_end();
377
378  virtual void EmitImpl(llvm::Serializer& S) const;
379  static ParenExpr* CreateImpl(llvm::Deserializer& D);
380};
381
382
383/// UnaryOperator - This represents the unary-expression's (except sizeof of
384/// types), the postinc/postdec operators from postfix-expression, and various
385/// extensions.
386///
387/// Notes on various nodes:
388///
389/// Real/Imag - These return the real/imag part of a complex operand.  If
390///   applied to a non-complex value, the former returns its operand and the
391///   later returns zero in the type of the operand.
392///
393/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose
394///   subexpression is a compound literal with the various MemberExpr and
395///   ArraySubscriptExpr's applied to it.
396///
397class UnaryOperator : public Expr {
398public:
399  // Note that additions to this should also update the StmtVisitor class.
400  enum Opcode {
401    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
402    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
403    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
404    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
405    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
406    SizeOf, AlignOf,  // [C99 6.5.3.4] Sizeof (expr, not type) operator.
407    Real, Imag,       // "__real expr"/"__imag expr" Extension.
408    Extension,        // __extension__ marker.
409    OffsetOf          // __builtin_offsetof
410  };
411private:
412  Expr *Val;
413  Opcode Opc;
414  SourceLocation Loc;
415public:
416
417  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
418    : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {}
419
420  Opcode getOpcode() const { return Opc; }
421  Expr *getSubExpr() const { return Val; }
422
423  /// getOperatorLoc - Return the location of the operator.
424  SourceLocation getOperatorLoc() const { return Loc; }
425
426  /// isPostfix - Return true if this is a postfix operation, like x++.
427  static bool isPostfix(Opcode Op);
428
429  bool isPostfix() const { return isPostfix(Opc); }
430  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
431  bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; }
432  bool isOffsetOfOp() const { return Opc == OffsetOf; }
433  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
434
435  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
436  /// corresponds to, e.g. "sizeof" or "[pre]++"
437  static const char *getOpcodeStr(Opcode Op);
438
439  virtual SourceRange getSourceRange() const {
440    if (isPostfix())
441      return SourceRange(Val->getLocStart(), Loc);
442    else
443      return SourceRange(Loc, Val->getLocEnd());
444  }
445  virtual SourceLocation getExprLoc() const { return Loc; }
446
447  static bool classof(const Stmt *T) {
448    return T->getStmtClass() == UnaryOperatorClass;
449  }
450  static bool classof(const UnaryOperator *) { return true; }
451
452  int64_t evaluateOffsetOf(ASTContext& C) const;
453
454  // Iterators
455  virtual child_iterator child_begin();
456  virtual child_iterator child_end();
457
458  virtual void EmitImpl(llvm::Serializer& S) const;
459  static UnaryOperator* CreateImpl(llvm::Deserializer& D);
460};
461
462/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of
463/// *types*.  sizeof(expr) is handled by UnaryOperator.
464class SizeOfAlignOfTypeExpr : public Expr {
465  bool isSizeof;  // true if sizeof, false if alignof.
466  QualType Ty;
467  SourceLocation OpLoc, RParenLoc;
468public:
469  SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType,
470                        SourceLocation op, SourceLocation rp) :
471    Expr(SizeOfAlignOfTypeExprClass, resultType),
472    isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {}
473
474  bool isSizeOf() const { return isSizeof; }
475  QualType getArgumentType() const { return Ty; }
476
477  SourceLocation getOperatorLoc() const { return OpLoc; }
478  SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); }
479
480  static bool classof(const Stmt *T) {
481    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
482  }
483  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
484
485  // Iterators
486  virtual child_iterator child_begin();
487  virtual child_iterator child_end();
488
489  virtual void EmitImpl(llvm::Serializer& S) const;
490  static SizeOfAlignOfTypeExpr* CreateImpl(llvm::Deserializer& D);
491};
492
493//===----------------------------------------------------------------------===//
494// Postfix Operators.
495//===----------------------------------------------------------------------===//
496
497/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
498class ArraySubscriptExpr : public Expr {
499  enum { LHS, RHS, END_EXPR=2 };
500  Expr* SubExprs[END_EXPR];
501  SourceLocation RBracketLoc;
502public:
503  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
504                     SourceLocation rbracketloc)
505  : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) {
506    SubExprs[LHS] = lhs;
507    SubExprs[RHS] = rhs;
508  }
509
510  /// An array access can be written A[4] or 4[A] (both are equivalent).
511  /// - getBase() and getIdx() always present the normalized view: A[4].
512  ///    In this case getBase() returns "A" and getIdx() returns "4".
513  /// - getLHS() and getRHS() present the syntactic view. e.g. for
514  ///    4[A] getLHS() returns "4".
515  /// Note: Because vector element access is also written A[4] we must
516  /// predicate the format conversion in getBase and getIdx only on the
517  /// the type of the RHS, as it is possible for the LHS to be a vector of
518  /// integer type
519  Expr *getLHS() { return SubExprs[LHS]; }
520  const Expr *getLHS() const { return SubExprs[LHS]; }
521
522  Expr *getRHS() { return SubExprs[RHS]; }
523  const Expr *getRHS() const { return SubExprs[RHS]; }
524
525  Expr *getBase() {
526    return (getRHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
527  }
528
529  const Expr *getBase() const {
530    return (getRHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
531  }
532
533  Expr *getIdx() {
534    return (getRHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
535  }
536
537  const Expr *getIdx() const {
538    return (getRHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
539  }
540
541
542  SourceRange getSourceRange() const {
543    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
544  }
545  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
546
547  static bool classof(const Stmt *T) {
548    return T->getStmtClass() == ArraySubscriptExprClass;
549  }
550  static bool classof(const ArraySubscriptExpr *) { return true; }
551
552  // Iterators
553  virtual child_iterator child_begin();
554  virtual child_iterator child_end();
555
556  virtual void EmitImpl(llvm::Serializer& S) const;
557  static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D);
558};
559
560
561/// CallExpr - [C99 6.5.2.2] Function Calls.
562///
563class CallExpr : public Expr {
564  enum { FN=0, ARGS_START=1 };
565  Expr **SubExprs;
566  unsigned NumArgs;
567  SourceLocation RParenLoc;
568
569  // This version of the ctor is for deserialization.
570  CallExpr(Expr** subexprs, unsigned numargs, QualType t,
571           SourceLocation rparenloc)
572  : Expr(CallExprClass,t), SubExprs(subexprs),
573    NumArgs(numargs), RParenLoc(rparenloc) {}
574
575public:
576  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
577           SourceLocation rparenloc);
578  ~CallExpr() {
579    delete [] SubExprs;
580  }
581
582  const Expr *getCallee() const { return SubExprs[FN]; }
583  Expr *getCallee() { return SubExprs[FN]; }
584  void setCallee(Expr *F) { SubExprs[FN] = F; }
585
586  /// getNumArgs - Return the number of actual arguments to this call.
587  ///
588  unsigned getNumArgs() const { return NumArgs; }
589
590  /// getArg - Return the specified argument.
591  Expr *getArg(unsigned Arg) {
592    assert(Arg < NumArgs && "Arg access out of range!");
593    return SubExprs[Arg+ARGS_START];
594  }
595  const Expr *getArg(unsigned Arg) const {
596    assert(Arg < NumArgs && "Arg access out of range!");
597    return SubExprs[Arg+ARGS_START];
598  }
599  /// setArg - Set the specified argument.
600  void setArg(unsigned Arg, Expr *ArgExpr) {
601    assert(Arg < NumArgs && "Arg access out of range!");
602    SubExprs[Arg+ARGS_START] = ArgExpr;
603  }
604
605  /// setNumArgs - This changes the number of arguments present in this call.
606  /// Any orphaned expressions are deleted by this, and any new operands are set
607  /// to null.
608  void setNumArgs(unsigned NumArgs);
609
610  typedef Expr **arg_iterator;
611  typedef Expr * const *arg_const_iterator;
612  arg_iterator arg_begin() { return SubExprs+ARGS_START; }
613  arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
614  arg_const_iterator arg_begin() const { return SubExprs+ARGS_START; }
615  arg_const_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs(); }
616
617
618  /// getNumCommas - Return the number of commas that must have been present in
619  /// this function call.
620  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
621
622  bool isBuiltinClassifyType(llvm::APSInt &Result) const;
623
624  SourceLocation getRParenLoc() const { return RParenLoc; }
625  SourceRange getSourceRange() const {
626    return SourceRange(getCallee()->getLocStart(), RParenLoc);
627  }
628
629  static bool classof(const Stmt *T) {
630    return T->getStmtClass() == CallExprClass;
631  }
632  static bool classof(const CallExpr *) { return true; }
633
634  // Iterators
635  virtual child_iterator child_begin();
636  virtual child_iterator child_end();
637
638  virtual void EmitImpl(llvm::Serializer& S) const;
639  static CallExpr* CreateImpl(llvm::Deserializer& D);
640};
641
642/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
643///
644class MemberExpr : public Expr {
645  Expr *Base;
646  FieldDecl *MemberDecl;
647  SourceLocation MemberLoc;
648  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
649public:
650  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l)
651    : Expr(MemberExprClass, memberdecl->getType()),
652      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
653
654  Expr *getBase() const { return Base; }
655  FieldDecl *getMemberDecl() const { return MemberDecl; }
656  bool isArrow() const { return IsArrow; }
657
658  virtual SourceRange getSourceRange() const {
659    return SourceRange(getBase()->getLocStart(), MemberLoc);
660  }
661  virtual SourceLocation getExprLoc() const { return MemberLoc; }
662
663  static bool classof(const Stmt *T) {
664    return T->getStmtClass() == MemberExprClass;
665  }
666  static bool classof(const MemberExpr *) { return true; }
667
668  // Iterators
669  virtual child_iterator child_begin();
670  virtual child_iterator child_end();
671
672  virtual void EmitImpl(llvm::Serializer& S) const;
673  static MemberExpr* CreateImpl(llvm::Deserializer& D);
674};
675
676/// OCUVectorElementExpr - This represents access to specific elements of a
677/// vector, and may occur on the left hand side or right hand side.  For example
678/// the following is legal:  "V.xy = V.zw" if V is a 4 element ocu vector.
679///
680class OCUVectorElementExpr : public Expr {
681  Expr *Base;
682  IdentifierInfo &Accessor;
683  SourceLocation AccessorLoc;
684public:
685  enum ElementType {
686    Point,   // xywz
687    Color,   // rgba
688    Texture  // stpq
689  };
690  OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
691                       SourceLocation loc)
692    : Expr(OCUVectorElementExprClass, ty),
693      Base(base), Accessor(accessor), AccessorLoc(loc) {}
694
695  const Expr *getBase() const { return Base; }
696  Expr *getBase() { return Base; }
697
698  IdentifierInfo &getAccessor() const { return Accessor; }
699
700  /// getNumElements - Get the number of components being selected.
701  unsigned getNumElements() const;
702
703  /// getElementType - Determine whether the components of this access are
704  /// "point" "color" or "texture" elements.
705  ElementType getElementType() const;
706
707  /// containsDuplicateElements - Return true if any element access is
708  /// repeated.
709  bool containsDuplicateElements() const;
710
711  /// getEncodedElementAccess - Encode the elements accessed into a bit vector.
712  /// The encoding currently uses 2-bit bitfields, but clients should use the
713  /// accessors below to access them.
714  ///
715  unsigned getEncodedElementAccess() const;
716
717  /// getAccessedFieldNo - Given an encoded value and a result number, return
718  /// the input field number being accessed.
719  static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) {
720    return (EncodedVal >> (Idx*2)) & 3;
721  }
722
723  virtual SourceRange getSourceRange() const {
724    return SourceRange(getBase()->getLocStart(), AccessorLoc);
725  }
726  static bool classof(const Stmt *T) {
727    return T->getStmtClass() == OCUVectorElementExprClass;
728  }
729  static bool classof(const OCUVectorElementExpr *) { return true; }
730
731  // Iterators
732  virtual child_iterator child_begin();
733  virtual child_iterator child_end();
734};
735
736/// CompoundLiteralExpr - [C99 6.5.2.5]
737///
738class CompoundLiteralExpr : public Expr {
739  /// LParenLoc - If non-null, this is the location of the left paren in a
740  /// compound literal like "(int){4}".  This can be null if this is a
741  /// synthesized compound expression.
742  SourceLocation LParenLoc;
743  Expr *Init;
744  bool FileScope;
745public:
746  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, bool fileScope) :
747    Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), FileScope(fileScope) {}
748
749  const Expr *getInitializer() const { return Init; }
750  Expr *getInitializer() { return Init; }
751
752  bool isFileScope() const { return FileScope; }
753
754  SourceLocation getLParenLoc() const { return LParenLoc; }
755
756  virtual SourceRange getSourceRange() const {
757    // FIXME: Init should never be null.
758    if (!Init)
759      return SourceRange();
760    if (LParenLoc.isInvalid())
761      return Init->getSourceRange();
762    return SourceRange(LParenLoc, Init->getLocEnd());
763  }
764
765  static bool classof(const Stmt *T) {
766    return T->getStmtClass() == CompoundLiteralExprClass;
767  }
768  static bool classof(const CompoundLiteralExpr *) { return true; }
769
770  // Iterators
771  virtual child_iterator child_begin();
772  virtual child_iterator child_end();
773
774  virtual void EmitImpl(llvm::Serializer& S) const;
775  static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D);
776};
777
778/// ImplicitCastExpr - Allows us to explicitly represent implicit type
779/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
780/// float->double, short->int, etc.
781///
782class ImplicitCastExpr : public Expr {
783  Expr *Op;
784public:
785  ImplicitCastExpr(QualType ty, Expr *op) :
786    Expr(ImplicitCastExprClass, ty), Op(op) {}
787
788  Expr *getSubExpr() { return Op; }
789  const Expr *getSubExpr() const { return Op; }
790
791  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
792
793  static bool classof(const Stmt *T) {
794    return T->getStmtClass() == ImplicitCastExprClass;
795  }
796  static bool classof(const ImplicitCastExpr *) { return true; }
797
798  // Iterators
799  virtual child_iterator child_begin();
800  virtual child_iterator child_end();
801
802  virtual void EmitImpl(llvm::Serializer& S) const;
803  static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D);
804};
805
806/// CastExpr - [C99 6.5.4] Cast Operators.
807///
808class CastExpr : public Expr {
809  Expr *Op;
810  SourceLocation Loc; // the location of the left paren
811public:
812  CastExpr(QualType ty, Expr *op, SourceLocation l) :
813    Expr(CastExprClass, ty), Op(op), Loc(l) {}
814
815  SourceLocation getLParenLoc() const { return Loc; }
816
817  Expr *getSubExpr() const { return Op; }
818
819  virtual SourceRange getSourceRange() const {
820    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
821  }
822  static bool classof(const Stmt *T) {
823    return T->getStmtClass() == CastExprClass;
824  }
825  static bool classof(const CastExpr *) { return true; }
826
827  // Iterators
828  virtual child_iterator child_begin();
829  virtual child_iterator child_end();
830
831  virtual void EmitImpl(llvm::Serializer& S) const;
832  static CastExpr* CreateImpl(llvm::Deserializer& D);
833};
834
835class BinaryOperator : public Expr {
836public:
837  enum Opcode {
838    // Operators listed in order of precedence.
839    // Note that additions to this should also update the StmtVisitor class.
840    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
841    Add, Sub,         // [C99 6.5.6] Additive operators.
842    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
843    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
844    EQ, NE,           // [C99 6.5.9] Equality operators.
845    And,              // [C99 6.5.10] Bitwise AND operator.
846    Xor,              // [C99 6.5.11] Bitwise XOR operator.
847    Or,               // [C99 6.5.12] Bitwise OR operator.
848    LAnd,             // [C99 6.5.13] Logical AND operator.
849    LOr,              // [C99 6.5.14] Logical OR operator.
850    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
851    DivAssign, RemAssign,
852    AddAssign, SubAssign,
853    ShlAssign, ShrAssign,
854    AndAssign, XorAssign,
855    OrAssign,
856    Comma             // [C99 6.5.17] Comma operator.
857  };
858private:
859  enum { LHS, RHS, END_EXPR };
860  Expr* SubExprs[END_EXPR];
861  Opcode Opc;
862  SourceLocation OpLoc;
863public:
864
865  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
866                 SourceLocation opLoc)
867    : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) {
868    SubExprs[LHS] = lhs;
869    SubExprs[RHS] = rhs;
870    assert(!isCompoundAssignmentOp() &&
871           "Use ArithAssignBinaryOperator for compound assignments");
872  }
873
874  SourceLocation getOperatorLoc() const { return OpLoc; }
875  Opcode getOpcode() const { return Opc; }
876  Expr *getLHS() const { return SubExprs[LHS]; }
877  Expr *getRHS() const { return SubExprs[RHS]; }
878  virtual SourceRange getSourceRange() const {
879    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
880  }
881
882  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
883  /// corresponds to, e.g. "<<=".
884  static const char *getOpcodeStr(Opcode Op);
885
886  /// predicates to categorize the respective opcodes.
887  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
888  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
889  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
890  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
891  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
892  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
893  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
894  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
895  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
896  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
897
898  static bool classof(const Stmt *S) {
899    return S->getStmtClass() == BinaryOperatorClass ||
900           S->getStmtClass() == CompoundAssignOperatorClass;
901  }
902  static bool classof(const BinaryOperator *) { return true; }
903
904  // Iterators
905  virtual child_iterator child_begin();
906  virtual child_iterator child_end();
907
908  virtual void EmitImpl(llvm::Serializer& S) const;
909  static BinaryOperator* CreateImpl(llvm::Deserializer& D);
910
911protected:
912  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
913                 SourceLocation oploc, bool dead)
914    : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
915    SubExprs[LHS] = lhs;
916    SubExprs[RHS] = rhs;
917  }
918};
919
920/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
921/// track of the type the operation is performed in.  Due to the semantics of
922/// these operators, the operands are promoted, the aritmetic performed, an
923/// implicit conversion back to the result type done, then the assignment takes
924/// place.  This captures the intermediate type which the computation is done
925/// in.
926class CompoundAssignOperator : public BinaryOperator {
927  QualType ComputationType;
928public:
929  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
930                         QualType ResType, QualType CompType,
931                         SourceLocation OpLoc)
932    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
933      ComputationType(CompType) {
934    assert(isCompoundAssignmentOp() &&
935           "Only should be used for compound assignments");
936  }
937
938  QualType getComputationType() const { return ComputationType; }
939
940  static bool classof(const CompoundAssignOperator *) { return true; }
941  static bool classof(const Stmt *S) {
942    return S->getStmtClass() == CompoundAssignOperatorClass;
943  }
944
945  virtual void EmitImpl(llvm::Serializer& S) const;
946  static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D);
947};
948
949/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
950/// GNU "missing LHS" extension is in use.
951///
952class ConditionalOperator : public Expr {
953  enum { COND, LHS, RHS, END_EXPR };
954  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
955public:
956  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
957    : Expr(ConditionalOperatorClass, t) {
958    SubExprs[COND] = cond;
959    SubExprs[LHS] = lhs;
960    SubExprs[RHS] = rhs;
961  }
962
963  // getCond - Return the expression representing the condition for
964  //  the ?: operator.
965  Expr *getCond() const { return SubExprs[COND]; }
966
967  // getTrueExpr - Return the subexpression representing the value of the ?:
968  //  expression if the condition evaluates to true.  In most cases this value
969  //  will be the same as getLHS() except a GCC extension allows the left
970  //  subexpression to be omitted, and instead of the condition be returned.
971  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
972  //  is only evaluated once.
973  Expr *getTrueExpr() const {
974    return SubExprs[LHS] ? SubExprs[COND] : SubExprs[LHS];
975  }
976
977  // getTrueExpr - Return the subexpression representing the value of the ?:
978  // expression if the condition evaluates to false. This is the same as getRHS.
979  Expr *getFalseExpr() const { return SubExprs[RHS]; }
980
981  Expr *getLHS() const { return SubExprs[LHS]; }
982  Expr *getRHS() const { return SubExprs[RHS]; }
983
984  virtual SourceRange getSourceRange() const {
985    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
986  }
987  static bool classof(const Stmt *T) {
988    return T->getStmtClass() == ConditionalOperatorClass;
989  }
990  static bool classof(const ConditionalOperator *) { return true; }
991
992  // Iterators
993  virtual child_iterator child_begin();
994  virtual child_iterator child_end();
995
996  virtual void EmitImpl(llvm::Serializer& S) const;
997  static ConditionalOperator* CreateImpl(llvm::Deserializer& D);
998};
999
1000/// AddrLabelExpr - The GNU address of label extension, representing &&label.
1001class AddrLabelExpr : public Expr {
1002  SourceLocation AmpAmpLoc, LabelLoc;
1003  LabelStmt *Label;
1004public:
1005  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
1006                QualType t)
1007    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
1008
1009  virtual SourceRange getSourceRange() const {
1010    return SourceRange(AmpAmpLoc, LabelLoc);
1011  }
1012
1013  LabelStmt *getLabel() const { return Label; }
1014
1015  static bool classof(const Stmt *T) {
1016    return T->getStmtClass() == AddrLabelExprClass;
1017  }
1018  static bool classof(const AddrLabelExpr *) { return true; }
1019
1020  // Iterators
1021  virtual child_iterator child_begin();
1022  virtual child_iterator child_end();
1023
1024  virtual void EmitImpl(llvm::Serializer& S) const;
1025  static AddrLabelExpr* CreateImpl(llvm::Deserializer& D);
1026};
1027
1028/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
1029/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
1030/// takes the value of the last subexpression.
1031class StmtExpr : public Expr {
1032  CompoundStmt *SubStmt;
1033  SourceLocation LParenLoc, RParenLoc;
1034public:
1035  StmtExpr(CompoundStmt *substmt, QualType T,
1036           SourceLocation lp, SourceLocation rp) :
1037    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
1038
1039  CompoundStmt *getSubStmt() { return SubStmt; }
1040  const CompoundStmt *getSubStmt() const { return SubStmt; }
1041
1042  virtual SourceRange getSourceRange() const {
1043    return SourceRange(LParenLoc, RParenLoc);
1044  }
1045
1046  static bool classof(const Stmt *T) {
1047    return T->getStmtClass() == StmtExprClass;
1048  }
1049  static bool classof(const StmtExpr *) { return true; }
1050
1051  // Iterators
1052  virtual child_iterator child_begin();
1053  virtual child_iterator child_end();
1054
1055  virtual void EmitImpl(llvm::Serializer& S) const;
1056  static StmtExpr* CreateImpl(llvm::Deserializer& D);
1057};
1058
1059/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
1060/// This AST node represents a function that returns 1 if two *types* (not
1061/// expressions) are compatible. The result of this built-in function can be
1062/// used in integer constant expressions.
1063class TypesCompatibleExpr : public Expr {
1064  QualType Type1;
1065  QualType Type2;
1066  SourceLocation BuiltinLoc, RParenLoc;
1067public:
1068  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
1069                      QualType t1, QualType t2, SourceLocation RP) :
1070    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
1071    BuiltinLoc(BLoc), RParenLoc(RP) {}
1072
1073  QualType getArgType1() const { return Type1; }
1074  QualType getArgType2() const { return Type2; }
1075
1076  virtual SourceRange getSourceRange() const {
1077    return SourceRange(BuiltinLoc, RParenLoc);
1078  }
1079  static bool classof(const Stmt *T) {
1080    return T->getStmtClass() == TypesCompatibleExprClass;
1081  }
1082  static bool classof(const TypesCompatibleExpr *) { return true; }
1083
1084  // Iterators
1085  virtual child_iterator child_begin();
1086  virtual child_iterator child_end();
1087};
1088
1089/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
1090/// This AST node is similar to the conditional operator (?:) in C, with
1091/// the following exceptions:
1092/// - the test expression much be a constant expression.
1093/// - the expression returned has it's type unaltered by promotion rules.
1094/// - does not evaluate the expression that was not chosen.
1095class ChooseExpr : public Expr {
1096  enum { COND, LHS, RHS, END_EXPR };
1097  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1098  SourceLocation BuiltinLoc, RParenLoc;
1099public:
1100  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
1101             SourceLocation RP)
1102    : Expr(ChooseExprClass, t),
1103      BuiltinLoc(BLoc), RParenLoc(RP) {
1104      SubExprs[COND] = cond;
1105      SubExprs[LHS] = lhs;
1106      SubExprs[RHS] = rhs;
1107    }
1108
1109  /// isConditionTrue - Return true if the condition is true.  This is always
1110  /// statically knowable for a well-formed choosexpr.
1111  bool isConditionTrue(ASTContext &C) const;
1112
1113  Expr *getCond() const { return SubExprs[COND]; }
1114  Expr *getLHS() const { return SubExprs[LHS]; }
1115  Expr *getRHS() const { return SubExprs[RHS]; }
1116
1117  virtual SourceRange getSourceRange() const {
1118    return SourceRange(BuiltinLoc, RParenLoc);
1119  }
1120  static bool classof(const Stmt *T) {
1121    return T->getStmtClass() == ChooseExprClass;
1122  }
1123  static bool classof(const ChooseExpr *) { return true; }
1124
1125  // Iterators
1126  virtual child_iterator child_begin();
1127  virtual child_iterator child_end();
1128};
1129
1130/// OverloadExpr - Clang builtin-in function __builtin_overload.
1131/// This AST node provides a way to overload functions in C
1132/// i.e. float Z = __builtin_overload(2, X, Y, modf, mod, modl);
1133/// would pick whichever of the functions modf, mod, and modl that took two
1134/// arguments of the same type as X and Y.
1135class OverloadExpr : public Expr {
1136  Expr **SubExprs;
1137  unsigned NumArgs;
1138  unsigned FnIndex;
1139  SourceLocation BuiltinLoc;
1140  SourceLocation RParenLoc;
1141public:
1142  OverloadExpr(Expr **args, unsigned narg, unsigned idx, QualType t,
1143               SourceLocation bloc, SourceLocation rploc)
1144    : Expr(OverloadExprClass, t), NumArgs(narg), FnIndex(idx), BuiltinLoc(bloc),
1145      RParenLoc(rploc) {
1146    SubExprs = new Expr*[narg];
1147    for (unsigned i = 0; i != narg; ++i)
1148      SubExprs[i] = args[i];
1149  }
1150  ~OverloadExpr() {
1151    delete [] SubExprs;
1152  }
1153
1154  typedef Expr * const *arg_const_iterator;
1155  arg_const_iterator arg_begin() const { return SubExprs+1; }
1156
1157  /// getNumArgs - Return the number of actual arguments to this call.
1158  ///
1159  unsigned getNumArgs() const { return NumArgs; }
1160
1161  /// getArg - Return the specified argument.
1162  Expr *getArg(unsigned Arg) {
1163    assert(Arg < NumArgs && "Arg access out of range!");
1164    return SubExprs[Arg];
1165  }
1166  Expr *getFn() { return SubExprs[FnIndex]; }
1167
1168  virtual SourceRange getSourceRange() const {
1169    return SourceRange(BuiltinLoc, RParenLoc);
1170  }
1171  static bool classof(const Stmt *T) {
1172    return T->getStmtClass() == OverloadExprClass;
1173  }
1174  static bool classof(const OverloadExpr *) { return true; }
1175
1176  // Iterators
1177  virtual child_iterator child_begin();
1178  virtual child_iterator child_end();
1179};
1180
1181/// VAArgExpr, used for the builtin function __builtin_va_start.
1182class VAArgExpr : public Expr {
1183  Expr *Val;
1184  SourceLocation BuiltinLoc, RParenLoc;
1185public:
1186  VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
1187    : Expr(VAArgExprClass, t),
1188      Val(e),
1189      BuiltinLoc(BLoc),
1190      RParenLoc(RPLoc) { }
1191
1192  const Expr *getSubExpr() const { return Val; }
1193  Expr *getSubExpr() { return Val; }
1194  virtual SourceRange getSourceRange() const {
1195    return SourceRange(BuiltinLoc, RParenLoc);
1196  }
1197  static bool classof(const Stmt *T) {
1198    return T->getStmtClass() == VAArgExprClass;
1199  }
1200  static bool classof(const VAArgExpr *) { return true; }
1201
1202  // Iterators
1203  virtual child_iterator child_begin();
1204  virtual child_iterator child_end();
1205};
1206
1207/// InitListExpr - used for struct and array initializers, such as:
1208///    struct foo x = { 1, { 2, 3 } };
1209///
1210/// Because C is somewhat loose with braces, the AST does not necessarily
1211/// directly model the C source.  Instead, the semantic analyzer aims to make
1212/// the InitListExprs match up with the type of the decl being initialized.  We
1213/// have the following exceptions:
1214///
1215///  1. Elements at the end of the list may be dropped from the initializer.
1216///     These elements are defined to be initialized to zero.  For example:
1217///         int x[20] = { 1 };
1218///  2. Initializers may have excess initializers which are to be ignored by the
1219///     compiler.  For example:
1220///         int x[1] = { 1, 2 };
1221///  3. Redundant InitListExprs may be present around scalar elements.  These
1222///     always have a single element whose type is the same as the InitListExpr.
1223///     this can only happen for Type::isScalarType() types.
1224///         int x = { 1 };  int y[2] = { {1}, {2} };
1225///
1226class InitListExpr : public Expr {
1227  Expr **InitExprs;
1228  unsigned NumInits;
1229  SourceLocation LBraceLoc, RBraceLoc;
1230public:
1231  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
1232               SourceLocation rbraceloc);
1233  ~InitListExpr() {
1234    delete [] InitExprs;
1235  }
1236
1237  unsigned getNumInits() const { return NumInits; }
1238
1239  const Expr* getInit(unsigned Init) const {
1240    assert(Init < NumInits && "Initializer access out of range!");
1241    return InitExprs[Init];
1242  }
1243
1244  Expr* getInit(unsigned Init) {
1245    assert(Init < NumInits && "Initializer access out of range!");
1246    return InitExprs[Init];
1247  }
1248
1249  void setInit(unsigned Init, Expr *expr) {
1250    assert(Init < NumInits && "Initializer access out of range!");
1251    InitExprs[Init] = expr;
1252  }
1253
1254  virtual SourceRange getSourceRange() const {
1255    return SourceRange(LBraceLoc, RBraceLoc);
1256  }
1257  static bool classof(const Stmt *T) {
1258    return T->getStmtClass() == InitListExprClass;
1259  }
1260  static bool classof(const InitListExpr *) { return true; }
1261
1262  // Iterators
1263  virtual child_iterator child_begin();
1264  virtual child_iterator child_end();
1265
1266  virtual void EmitImpl(llvm::Serializer& S) const;
1267  static InitListExpr* CreateImpl(llvm::Deserializer& D);
1268
1269private:
1270  // Used by serializer.
1271  InitListExpr() : Expr(InitListExprClass, QualType()),
1272                   InitExprs(NULL), NumInits(0) {}
1273};
1274
1275/// ObjCStringLiteral, used for Objective-C string literals
1276/// i.e. @"foo".
1277class ObjCStringLiteral : public Expr {
1278  StringLiteral *String;
1279  SourceLocation AtLoc;
1280public:
1281  ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L)
1282    : Expr(ObjCStringLiteralClass, T), String(SL), AtLoc(L) {}
1283
1284  StringLiteral* getString() { return String; }
1285
1286  const StringLiteral* getString() const { return String; }
1287
1288  SourceLocation getAtLoc() const { return AtLoc; }
1289
1290  virtual SourceRange getSourceRange() const {
1291    return SourceRange(AtLoc, String->getLocEnd());
1292  }
1293
1294  static bool classof(const Stmt *T) {
1295    return T->getStmtClass() == ObjCStringLiteralClass;
1296  }
1297  static bool classof(const ObjCStringLiteral *) { return true; }
1298
1299  // Iterators
1300  virtual child_iterator child_begin();
1301  virtual child_iterator child_end();
1302
1303  virtual void EmitImpl(llvm::Serializer& S) const;
1304  static ObjCStringLiteral* CreateImpl(llvm::Deserializer& D);
1305};
1306
1307/// ObjCEncodeExpr, used for @encode in Objective-C.
1308class ObjCEncodeExpr : public Expr {
1309  QualType EncType;
1310  SourceLocation AtLoc, RParenLoc;
1311public:
1312  ObjCEncodeExpr(QualType T, QualType ET,
1313                 SourceLocation at, SourceLocation rp)
1314    : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {}
1315
1316  SourceLocation getAtLoc() const { return AtLoc; }
1317  SourceLocation getRParenLoc() const { return RParenLoc; }
1318
1319  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1320
1321  QualType getEncodedType() const { return EncType; }
1322
1323  static bool classof(const Stmt *T) {
1324    return T->getStmtClass() == ObjCEncodeExprClass;
1325  }
1326  static bool classof(const ObjCEncodeExpr *) { return true; }
1327
1328  // Iterators
1329  virtual child_iterator child_begin();
1330  virtual child_iterator child_end();
1331
1332  virtual void EmitImpl(llvm::Serializer& S) const;
1333  static ObjCEncodeExpr* CreateImpl(llvm::Deserializer& D);
1334};
1335
1336/// ObjCSelectorExpr used for @selector in Objective-C.
1337class ObjCSelectorExpr : public Expr {
1338  Selector SelName;
1339  SourceLocation AtLoc, RParenLoc;
1340public:
1341  ObjCSelectorExpr(QualType T, Selector selInfo,
1342                   SourceLocation at, SourceLocation rp)
1343  : Expr(ObjCSelectorExprClass, T), SelName(selInfo),
1344  AtLoc(at), RParenLoc(rp) {}
1345
1346  const Selector &getSelector() const { return SelName; }
1347  Selector &getSelector() { return SelName; }
1348
1349  SourceLocation getAtLoc() const { return AtLoc; }
1350  SourceLocation getRParenLoc() const { return RParenLoc; }
1351  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1352
1353  /// getNumArgs - Return the number of actual arguments to this call.
1354  unsigned getNumArgs() const { return SelName.getNumArgs(); }
1355
1356  static bool classof(const Stmt *T) {
1357    return T->getStmtClass() == ObjCSelectorExprClass;
1358  }
1359  static bool classof(const ObjCSelectorExpr *) { return true; }
1360
1361  // Iterators
1362  virtual child_iterator child_begin();
1363  virtual child_iterator child_end();
1364
1365  virtual void EmitImpl(llvm::Serializer& S) const;
1366  static ObjCSelectorExpr* CreateImpl(llvm::Deserializer& D);
1367};
1368
1369/// ObjCProtocolExpr used for protocol in Objective-C.
1370class ObjCProtocolExpr : public Expr {
1371  ObjCProtocolDecl *Protocol;
1372  SourceLocation AtLoc, RParenLoc;
1373public:
1374  ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
1375                   SourceLocation at, SourceLocation rp)
1376  : Expr(ObjCProtocolExprClass, T), Protocol(protocol),
1377  AtLoc(at), RParenLoc(rp) {}
1378
1379  ObjCProtocolDecl *getProtocol() const { return Protocol; }
1380
1381  SourceLocation getAtLoc() const { return AtLoc; }
1382  SourceLocation getRParenLoc() const { return RParenLoc; }
1383  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1384
1385  static bool classof(const Stmt *T) {
1386    return T->getStmtClass() == ObjCProtocolExprClass;
1387  }
1388  static bool classof(const ObjCProtocolExpr *) { return true; }
1389
1390  // Iterators
1391  virtual child_iterator child_begin();
1392  virtual child_iterator child_end();
1393};
1394
1395/// ObjCIvarRefExpr - A reference to an ObjC instance variable.
1396class ObjCIvarRefExpr : public Expr {
1397  class ObjCIvarDecl *D;
1398  SourceLocation Loc;
1399  Expr *Base;
1400  bool IsArrow:1;      // True if this is "X->F", false if this is "X.F".
1401  bool IsFreeIvar:1;   // True if ivar reference has no base (self assumed).
1402
1403public:
1404  ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t, SourceLocation l, Expr *base=0,
1405                  bool arrow = false, bool freeIvar = false) :
1406    Expr(ObjCIvarRefExprClass, t), D(d), Loc(l), Base(base), IsArrow(arrow),
1407    IsFreeIvar(freeIvar) {}
1408
1409  ObjCIvarDecl *getDecl() { return D; }
1410  const ObjCIvarDecl *getDecl() const { return D; }
1411  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
1412  Expr *const getBase() const { return Base; }
1413  void setBase(Expr * base) { Base = base; }
1414  const bool isArrow() const { return IsArrow; }
1415  const bool isFreeIvar() const { return IsFreeIvar; }
1416
1417  SourceLocation getLocation() const { return Loc; }
1418
1419  static bool classof(const Stmt *T) {
1420    return T->getStmtClass() == ObjCIvarRefExprClass;
1421  }
1422  static bool classof(const ObjCIvarRefExpr *) { return true; }
1423
1424  // Iterators
1425  virtual child_iterator child_begin();
1426  virtual child_iterator child_end();
1427
1428  virtual void EmitImpl(llvm::Serializer& S) const;
1429  static ObjCIvarRefExpr* CreateImpl(llvm::Deserializer& D);
1430};
1431
1432class ObjCMessageExpr : public Expr {
1433  enum { RECEIVER=0, ARGS_START=1 };
1434
1435  Expr **SubExprs;
1436
1437  unsigned NumArgs;
1438
1439  // A unigue name for this message.
1440  Selector SelName;
1441
1442  // A method prototype for this message (optional).
1443  // FIXME: Since method decls contain the selector, and most messages have a
1444  // prototype, consider devising a scheme for unifying SelName/MethodProto.
1445  ObjCMethodDecl *MethodProto;
1446
1447  IdentifierInfo *ClassName; // optional - 0 for instance messages.
1448
1449  SourceLocation LBracloc, RBracloc;
1450public:
1451  // constructor for class messages.
1452  // FIXME: clsName should be typed to ObjCInterfaceType
1453  ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
1454                  QualType retType, ObjCMethodDecl *methDecl,
1455                  SourceLocation LBrac, SourceLocation RBrac,
1456                  Expr **ArgExprs, unsigned NumArgs);
1457  // constructor for instance messages.
1458  ObjCMessageExpr(Expr *receiver, Selector selInfo,
1459                  QualType retType, ObjCMethodDecl *methDecl,
1460                  SourceLocation LBrac, SourceLocation RBrac,
1461                  Expr **ArgExprs, unsigned NumArgs);
1462  ~ObjCMessageExpr() {
1463    delete [] SubExprs;
1464  }
1465
1466  const Expr *getReceiver() const { return SubExprs[RECEIVER]; }
1467  Expr *getReceiver() { return SubExprs[RECEIVER]; }
1468
1469  const Selector &getSelector() const { return SelName; }
1470  Selector &getSelector() { return SelName; }
1471
1472  const ObjCMethodDecl *getMethodDecl() const { return MethodProto; }
1473  ObjCMethodDecl *getMethodDecl() { return MethodProto; }
1474
1475  const IdentifierInfo *getClassName() const { return ClassName; }
1476  IdentifierInfo *getClassName() { return ClassName; }
1477
1478  /// getNumArgs - Return the number of actual arguments to this call.
1479  unsigned getNumArgs() const { return NumArgs; }
1480
1481/// getArg - Return the specified argument.
1482  Expr *getArg(unsigned Arg) {
1483    assert(Arg < NumArgs && "Arg access out of range!");
1484    return SubExprs[Arg+ARGS_START];
1485  }
1486  const Expr *getArg(unsigned Arg) const {
1487    assert(Arg < NumArgs && "Arg access out of range!");
1488    return SubExprs[Arg+ARGS_START];
1489  }
1490  /// setArg - Set the specified argument.
1491  void setArg(unsigned Arg, Expr *ArgExpr) {
1492    assert(Arg < NumArgs && "Arg access out of range!");
1493    SubExprs[Arg+ARGS_START] = ArgExpr;
1494  }
1495  SourceRange getSourceRange() const { return SourceRange(LBracloc, RBracloc); }
1496
1497  static bool classof(const Stmt *T) {
1498    return T->getStmtClass() == ObjCMessageExprClass;
1499  }
1500  static bool classof(const ObjCMessageExpr *) { return true; }
1501
1502  // Iterators
1503  virtual child_iterator child_begin();
1504  virtual child_iterator child_end();
1505};
1506
1507}  // end namespace clang
1508
1509#endif
1510