Expr.h revision a7ad98ff0919d6a24ea7c46634ea29bea551c1a0
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().  The C type for a string is always a
323/// ConstantArrayType.
324class StringLiteral : public Expr {
325  const char *StrData;
326  unsigned ByteLength;
327  bool IsWide;
328  // if the StringLiteral was composed using token pasting, both locations
329  // are needed. If not (the common case), firstTokLoc == lastTokLoc.
330  // FIXME: if space becomes an issue, we should create a sub-class.
331  SourceLocation firstTokLoc, lastTokLoc;
332public:
333  StringLiteral(const char *strData, unsigned byteLength, bool Wide,
334                QualType t, SourceLocation b, SourceLocation e);
335  virtual ~StringLiteral();
336
337  const char *getStrData() const { return StrData; }
338  unsigned getByteLength() const { return ByteLength; }
339  bool isWide() const { return IsWide; }
340
341  virtual SourceRange getSourceRange() const {
342    return SourceRange(firstTokLoc,lastTokLoc);
343  }
344  static bool classof(const Stmt *T) {
345    return T->getStmtClass() == StringLiteralClass;
346  }
347  static bool classof(const StringLiteral *) { return true; }
348
349  // Iterators
350  virtual child_iterator child_begin();
351  virtual child_iterator child_end();
352
353  virtual void EmitImpl(llvm::Serializer& S) const;
354  static StringLiteral* CreateImpl(llvm::Deserializer& D);
355};
356
357/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
358/// AST node is only formed if full location information is requested.
359class ParenExpr : public Expr {
360  SourceLocation L, R;
361  Expr *Val;
362public:
363  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
364    : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {}
365
366  const Expr *getSubExpr() const { return Val; }
367  Expr *getSubExpr() { return Val; }
368  SourceRange getSourceRange() const { return SourceRange(L, R); }
369
370  static bool classof(const Stmt *T) {
371    return T->getStmtClass() == ParenExprClass;
372  }
373  static bool classof(const ParenExpr *) { return true; }
374
375  // Iterators
376  virtual child_iterator child_begin();
377  virtual child_iterator child_end();
378
379  virtual void EmitImpl(llvm::Serializer& S) const;
380  static ParenExpr* CreateImpl(llvm::Deserializer& D);
381};
382
383
384/// UnaryOperator - This represents the unary-expression's (except sizeof of
385/// types), the postinc/postdec operators from postfix-expression, and various
386/// extensions.
387///
388/// Notes on various nodes:
389///
390/// Real/Imag - These return the real/imag part of a complex operand.  If
391///   applied to a non-complex value, the former returns its operand and the
392///   later returns zero in the type of the operand.
393///
394/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose
395///   subexpression is a compound literal with the various MemberExpr and
396///   ArraySubscriptExpr's applied to it.
397///
398class UnaryOperator : public Expr {
399public:
400  // Note that additions to this should also update the StmtVisitor class.
401  enum Opcode {
402    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
403    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
404    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
405    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
406    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
407    SizeOf, AlignOf,  // [C99 6.5.3.4] Sizeof (expr, not type) operator.
408    Real, Imag,       // "__real expr"/"__imag expr" Extension.
409    Extension,        // __extension__ marker.
410    OffsetOf          // __builtin_offsetof
411  };
412private:
413  Expr *Val;
414  Opcode Opc;
415  SourceLocation Loc;
416public:
417
418  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
419    : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {}
420
421  Opcode getOpcode() const { return Opc; }
422  Expr *getSubExpr() const { return Val; }
423
424  /// getOperatorLoc - Return the location of the operator.
425  SourceLocation getOperatorLoc() const { return Loc; }
426
427  /// isPostfix - Return true if this is a postfix operation, like x++.
428  static bool isPostfix(Opcode Op);
429
430  bool isPostfix() const { return isPostfix(Opc); }
431  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
432  bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; }
433  bool isOffsetOfOp() const { return Opc == OffsetOf; }
434  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
435
436  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
437  /// corresponds to, e.g. "sizeof" or "[pre]++"
438  static const char *getOpcodeStr(Opcode Op);
439
440  virtual SourceRange getSourceRange() const {
441    if (isPostfix())
442      return SourceRange(Val->getLocStart(), Loc);
443    else
444      return SourceRange(Loc, Val->getLocEnd());
445  }
446  virtual SourceLocation getExprLoc() const { return Loc; }
447
448  static bool classof(const Stmt *T) {
449    return T->getStmtClass() == UnaryOperatorClass;
450  }
451  static bool classof(const UnaryOperator *) { return true; }
452
453  int64_t evaluateOffsetOf(ASTContext& C) const;
454
455  // Iterators
456  virtual child_iterator child_begin();
457  virtual child_iterator child_end();
458
459  virtual void EmitImpl(llvm::Serializer& S) const;
460  static UnaryOperator* CreateImpl(llvm::Deserializer& D);
461};
462
463/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of
464/// *types*.  sizeof(expr) is handled by UnaryOperator.
465class SizeOfAlignOfTypeExpr : public Expr {
466  bool isSizeof;  // true if sizeof, false if alignof.
467  QualType Ty;
468  SourceLocation OpLoc, RParenLoc;
469public:
470  SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType,
471                        SourceLocation op, SourceLocation rp) :
472    Expr(SizeOfAlignOfTypeExprClass, resultType),
473    isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {}
474
475  bool isSizeOf() const { return isSizeof; }
476  QualType getArgumentType() const { return Ty; }
477
478  SourceLocation getOperatorLoc() const { return OpLoc; }
479  SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); }
480
481  static bool classof(const Stmt *T) {
482    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
483  }
484  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
485
486  // Iterators
487  virtual child_iterator child_begin();
488  virtual child_iterator child_end();
489
490  virtual void EmitImpl(llvm::Serializer& S) const;
491  static SizeOfAlignOfTypeExpr* CreateImpl(llvm::Deserializer& D);
492};
493
494//===----------------------------------------------------------------------===//
495// Postfix Operators.
496//===----------------------------------------------------------------------===//
497
498/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
499class ArraySubscriptExpr : public Expr {
500  enum { LHS, RHS, END_EXPR=2 };
501  Expr* SubExprs[END_EXPR];
502  SourceLocation RBracketLoc;
503public:
504  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
505                     SourceLocation rbracketloc)
506  : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) {
507    SubExprs[LHS] = lhs;
508    SubExprs[RHS] = rhs;
509  }
510
511  /// An array access can be written A[4] or 4[A] (both are equivalent).
512  /// - getBase() and getIdx() always present the normalized view: A[4].
513  ///    In this case getBase() returns "A" and getIdx() returns "4".
514  /// - getLHS() and getRHS() present the syntactic view. e.g. for
515  ///    4[A] getLHS() returns "4".
516  /// Note: Because vector element access is also written A[4] we must
517  /// predicate the format conversion in getBase and getIdx only on the
518  /// the type of the RHS, as it is possible for the LHS to be a vector of
519  /// integer type
520  Expr *getLHS() { return SubExprs[LHS]; }
521  const Expr *getLHS() const { return SubExprs[LHS]; }
522
523  Expr *getRHS() { return SubExprs[RHS]; }
524  const Expr *getRHS() const { return SubExprs[RHS]; }
525
526  Expr *getBase() {
527    return (getRHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
528  }
529
530  const Expr *getBase() const {
531    return (getRHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
532  }
533
534  Expr *getIdx() {
535    return (getRHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
536  }
537
538  const Expr *getIdx() const {
539    return (getRHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
540  }
541
542
543  SourceRange getSourceRange() const {
544    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
545  }
546  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
547
548  static bool classof(const Stmt *T) {
549    return T->getStmtClass() == ArraySubscriptExprClass;
550  }
551  static bool classof(const ArraySubscriptExpr *) { return true; }
552
553  // Iterators
554  virtual child_iterator child_begin();
555  virtual child_iterator child_end();
556
557  virtual void EmitImpl(llvm::Serializer& S) const;
558  static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D);
559};
560
561
562/// CallExpr - [C99 6.5.2.2] Function Calls.
563///
564class CallExpr : public Expr {
565  enum { FN=0, ARGS_START=1 };
566  Expr **SubExprs;
567  unsigned NumArgs;
568  SourceLocation RParenLoc;
569
570  // This version of the ctor is for deserialization.
571  CallExpr(Expr** subexprs, unsigned numargs, QualType t,
572           SourceLocation rparenloc)
573  : Expr(CallExprClass,t), SubExprs(subexprs),
574    NumArgs(numargs), RParenLoc(rparenloc) {}
575
576public:
577  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
578           SourceLocation rparenloc);
579  ~CallExpr() {
580    delete [] SubExprs;
581  }
582
583  const Expr *getCallee() const { return SubExprs[FN]; }
584  Expr *getCallee() { return SubExprs[FN]; }
585  void setCallee(Expr *F) { SubExprs[FN] = F; }
586
587  /// getNumArgs - Return the number of actual arguments to this call.
588  ///
589  unsigned getNumArgs() const { return NumArgs; }
590
591  /// getArg - Return the specified argument.
592  Expr *getArg(unsigned Arg) {
593    assert(Arg < NumArgs && "Arg access out of range!");
594    return SubExprs[Arg+ARGS_START];
595  }
596  const Expr *getArg(unsigned Arg) const {
597    assert(Arg < NumArgs && "Arg access out of range!");
598    return SubExprs[Arg+ARGS_START];
599  }
600  /// setArg - Set the specified argument.
601  void setArg(unsigned Arg, Expr *ArgExpr) {
602    assert(Arg < NumArgs && "Arg access out of range!");
603    SubExprs[Arg+ARGS_START] = ArgExpr;
604  }
605
606  /// setNumArgs - This changes the number of arguments present in this call.
607  /// Any orphaned expressions are deleted by this, and any new operands are set
608  /// to null.
609  void setNumArgs(unsigned NumArgs);
610
611  typedef Expr **arg_iterator;
612  typedef Expr * const *arg_const_iterator;
613  arg_iterator arg_begin() { return SubExprs+ARGS_START; }
614  arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
615  arg_const_iterator arg_begin() const { return SubExprs+ARGS_START; }
616  arg_const_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs(); }
617
618
619  /// getNumCommas - Return the number of commas that must have been present in
620  /// this function call.
621  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
622
623  bool isBuiltinClassifyType(llvm::APSInt &Result) const;
624
625  /// isBuiltinConstantExpr - Return true if this built-in call is constant.
626  bool isBuiltinConstantExpr() const;
627
628  SourceLocation getRParenLoc() const { return RParenLoc; }
629  SourceRange getSourceRange() const {
630    return SourceRange(getCallee()->getLocStart(), RParenLoc);
631  }
632
633  static bool classof(const Stmt *T) {
634    return T->getStmtClass() == CallExprClass;
635  }
636  static bool classof(const CallExpr *) { return true; }
637
638  // Iterators
639  virtual child_iterator child_begin();
640  virtual child_iterator child_end();
641
642  virtual void EmitImpl(llvm::Serializer& S) const;
643  static CallExpr* CreateImpl(llvm::Deserializer& D);
644};
645
646/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
647///
648class MemberExpr : public Expr {
649  Expr *Base;
650  FieldDecl *MemberDecl;
651  SourceLocation MemberLoc;
652  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
653public:
654  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l,
655             QualType ty)
656    : Expr(MemberExprClass, ty),
657      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
658
659  Expr *getBase() const { return Base; }
660  FieldDecl *getMemberDecl() const { return MemberDecl; }
661  bool isArrow() const { return IsArrow; }
662
663  virtual SourceRange getSourceRange() const {
664    return SourceRange(getBase()->getLocStart(), MemberLoc);
665  }
666  virtual SourceLocation getExprLoc() const { return MemberLoc; }
667
668  static bool classof(const Stmt *T) {
669    return T->getStmtClass() == MemberExprClass;
670  }
671  static bool classof(const MemberExpr *) { return true; }
672
673  // Iterators
674  virtual child_iterator child_begin();
675  virtual child_iterator child_end();
676
677  virtual void EmitImpl(llvm::Serializer& S) const;
678  static MemberExpr* CreateImpl(llvm::Deserializer& D);
679};
680
681/// OCUVectorElementExpr - This represents access to specific elements of a
682/// vector, and may occur on the left hand side or right hand side.  For example
683/// the following is legal:  "V.xy = V.zw" if V is a 4 element ocu vector.
684///
685class OCUVectorElementExpr : public Expr {
686  Expr *Base;
687  IdentifierInfo &Accessor;
688  SourceLocation AccessorLoc;
689public:
690  enum ElementType {
691    Point,   // xywz
692    Color,   // rgba
693    Texture  // stpq
694  };
695  OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
696                       SourceLocation loc)
697    : Expr(OCUVectorElementExprClass, ty),
698      Base(base), Accessor(accessor), AccessorLoc(loc) {}
699
700  const Expr *getBase() const { return Base; }
701  Expr *getBase() { return Base; }
702
703  IdentifierInfo &getAccessor() const { return Accessor; }
704
705  /// getNumElements - Get the number of components being selected.
706  unsigned getNumElements() const;
707
708  /// getElementType - Determine whether the components of this access are
709  /// "point" "color" or "texture" elements.
710  ElementType getElementType() const;
711
712  /// containsDuplicateElements - Return true if any element access is
713  /// repeated.
714  bool containsDuplicateElements() const;
715
716  /// getEncodedElementAccess - Encode the elements accessed into a bit vector.
717  /// The encoding currently uses 2-bit bitfields, but clients should use the
718  /// accessors below to access them.
719  ///
720  unsigned getEncodedElementAccess() const;
721
722  /// getAccessedFieldNo - Given an encoded value and a result number, return
723  /// the input field number being accessed.
724  static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) {
725    return (EncodedVal >> (Idx*2)) & 3;
726  }
727
728  virtual SourceRange getSourceRange() const {
729    return SourceRange(getBase()->getLocStart(), AccessorLoc);
730  }
731  static bool classof(const Stmt *T) {
732    return T->getStmtClass() == OCUVectorElementExprClass;
733  }
734  static bool classof(const OCUVectorElementExpr *) { return true; }
735
736  // Iterators
737  virtual child_iterator child_begin();
738  virtual child_iterator child_end();
739};
740
741/// CompoundLiteralExpr - [C99 6.5.2.5]
742///
743class CompoundLiteralExpr : public Expr {
744  /// LParenLoc - If non-null, this is the location of the left paren in a
745  /// compound literal like "(int){4}".  This can be null if this is a
746  /// synthesized compound expression.
747  SourceLocation LParenLoc;
748  Expr *Init;
749  bool FileScope;
750public:
751  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, bool fileScope) :
752    Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), FileScope(fileScope) {}
753
754  const Expr *getInitializer() const { return Init; }
755  Expr *getInitializer() { return Init; }
756
757  bool isFileScope() const { return FileScope; }
758
759  SourceLocation getLParenLoc() const { return LParenLoc; }
760
761  virtual SourceRange getSourceRange() const {
762    // FIXME: Init should never be null.
763    if (!Init)
764      return SourceRange();
765    if (LParenLoc.isInvalid())
766      return Init->getSourceRange();
767    return SourceRange(LParenLoc, Init->getLocEnd());
768  }
769
770  static bool classof(const Stmt *T) {
771    return T->getStmtClass() == CompoundLiteralExprClass;
772  }
773  static bool classof(const CompoundLiteralExpr *) { return true; }
774
775  // Iterators
776  virtual child_iterator child_begin();
777  virtual child_iterator child_end();
778
779  virtual void EmitImpl(llvm::Serializer& S) const;
780  static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D);
781};
782
783/// ImplicitCastExpr - Allows us to explicitly represent implicit type
784/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
785/// float->double, short->int, etc.
786///
787class ImplicitCastExpr : public Expr {
788  Expr *Op;
789public:
790  ImplicitCastExpr(QualType ty, Expr *op) :
791    Expr(ImplicitCastExprClass, ty), Op(op) {}
792
793  Expr *getSubExpr() { return Op; }
794  const Expr *getSubExpr() const { return Op; }
795
796  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
797
798  static bool classof(const Stmt *T) {
799    return T->getStmtClass() == ImplicitCastExprClass;
800  }
801  static bool classof(const ImplicitCastExpr *) { return true; }
802
803  // Iterators
804  virtual child_iterator child_begin();
805  virtual child_iterator child_end();
806
807  virtual void EmitImpl(llvm::Serializer& S) const;
808  static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D);
809};
810
811/// CastExpr - [C99 6.5.4] Cast Operators.
812///
813class CastExpr : public Expr {
814  Expr *Op;
815  SourceLocation Loc; // the location of the left paren
816public:
817  CastExpr(QualType ty, Expr *op, SourceLocation l) :
818    Expr(CastExprClass, ty), Op(op), Loc(l) {}
819
820  SourceLocation getLParenLoc() const { return Loc; }
821
822  Expr *getSubExpr() const { return Op; }
823
824  virtual SourceRange getSourceRange() const {
825    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
826  }
827  static bool classof(const Stmt *T) {
828    return T->getStmtClass() == CastExprClass;
829  }
830  static bool classof(const CastExpr *) { return true; }
831
832  // Iterators
833  virtual child_iterator child_begin();
834  virtual child_iterator child_end();
835
836  virtual void EmitImpl(llvm::Serializer& S) const;
837  static CastExpr* CreateImpl(llvm::Deserializer& D);
838};
839
840class BinaryOperator : public Expr {
841public:
842  enum Opcode {
843    // Operators listed in order of precedence.
844    // Note that additions to this should also update the StmtVisitor class.
845    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
846    Add, Sub,         // [C99 6.5.6] Additive operators.
847    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
848    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
849    EQ, NE,           // [C99 6.5.9] Equality operators.
850    And,              // [C99 6.5.10] Bitwise AND operator.
851    Xor,              // [C99 6.5.11] Bitwise XOR operator.
852    Or,               // [C99 6.5.12] Bitwise OR operator.
853    LAnd,             // [C99 6.5.13] Logical AND operator.
854    LOr,              // [C99 6.5.14] Logical OR operator.
855    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
856    DivAssign, RemAssign,
857    AddAssign, SubAssign,
858    ShlAssign, ShrAssign,
859    AndAssign, XorAssign,
860    OrAssign,
861    Comma             // [C99 6.5.17] Comma operator.
862  };
863private:
864  enum { LHS, RHS, END_EXPR };
865  Expr* SubExprs[END_EXPR];
866  Opcode Opc;
867  SourceLocation OpLoc;
868public:
869
870  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
871                 SourceLocation opLoc)
872    : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) {
873    SubExprs[LHS] = lhs;
874    SubExprs[RHS] = rhs;
875    assert(!isCompoundAssignmentOp() &&
876           "Use ArithAssignBinaryOperator for compound assignments");
877  }
878
879  SourceLocation getOperatorLoc() const { return OpLoc; }
880  Opcode getOpcode() const { return Opc; }
881  Expr *getLHS() const { return SubExprs[LHS]; }
882  Expr *getRHS() const { return SubExprs[RHS]; }
883  virtual SourceRange getSourceRange() const {
884    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
885  }
886
887  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
888  /// corresponds to, e.g. "<<=".
889  static const char *getOpcodeStr(Opcode Op);
890
891  /// predicates to categorize the respective opcodes.
892  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
893  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
894  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
895  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
896  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
897  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
898  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
899  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
900  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
901  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
902
903  static bool classof(const Stmt *S) {
904    return S->getStmtClass() == BinaryOperatorClass ||
905           S->getStmtClass() == CompoundAssignOperatorClass;
906  }
907  static bool classof(const BinaryOperator *) { return true; }
908
909  // Iterators
910  virtual child_iterator child_begin();
911  virtual child_iterator child_end();
912
913  virtual void EmitImpl(llvm::Serializer& S) const;
914  static BinaryOperator* CreateImpl(llvm::Deserializer& D);
915
916protected:
917  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
918                 SourceLocation oploc, bool dead)
919    : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
920    SubExprs[LHS] = lhs;
921    SubExprs[RHS] = rhs;
922  }
923};
924
925/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
926/// track of the type the operation is performed in.  Due to the semantics of
927/// these operators, the operands are promoted, the aritmetic performed, an
928/// implicit conversion back to the result type done, then the assignment takes
929/// place.  This captures the intermediate type which the computation is done
930/// in.
931class CompoundAssignOperator : public BinaryOperator {
932  QualType ComputationType;
933public:
934  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
935                         QualType ResType, QualType CompType,
936                         SourceLocation OpLoc)
937    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
938      ComputationType(CompType) {
939    assert(isCompoundAssignmentOp() &&
940           "Only should be used for compound assignments");
941  }
942
943  QualType getComputationType() const { return ComputationType; }
944
945  static bool classof(const CompoundAssignOperator *) { return true; }
946  static bool classof(const Stmt *S) {
947    return S->getStmtClass() == CompoundAssignOperatorClass;
948  }
949
950  virtual void EmitImpl(llvm::Serializer& S) const;
951  static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D);
952};
953
954/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
955/// GNU "missing LHS" extension is in use.
956///
957class ConditionalOperator : public Expr {
958  enum { COND, LHS, RHS, END_EXPR };
959  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
960public:
961  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
962    : Expr(ConditionalOperatorClass, t) {
963    SubExprs[COND] = cond;
964    SubExprs[LHS] = lhs;
965    SubExprs[RHS] = rhs;
966  }
967
968  // getCond - Return the expression representing the condition for
969  //  the ?: operator.
970  Expr *getCond() const { return SubExprs[COND]; }
971
972  // getTrueExpr - Return the subexpression representing the value of the ?:
973  //  expression if the condition evaluates to true.  In most cases this value
974  //  will be the same as getLHS() except a GCC extension allows the left
975  //  subexpression to be omitted, and instead of the condition be returned.
976  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
977  //  is only evaluated once.
978  Expr *getTrueExpr() const {
979    return SubExprs[LHS] ? SubExprs[COND] : SubExprs[LHS];
980  }
981
982  // getTrueExpr - Return the subexpression representing the value of the ?:
983  // expression if the condition evaluates to false. This is the same as getRHS.
984  Expr *getFalseExpr() const { return SubExprs[RHS]; }
985
986  Expr *getLHS() const { return SubExprs[LHS]; }
987  Expr *getRHS() const { return SubExprs[RHS]; }
988
989  virtual SourceRange getSourceRange() const {
990    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
991  }
992  static bool classof(const Stmt *T) {
993    return T->getStmtClass() == ConditionalOperatorClass;
994  }
995  static bool classof(const ConditionalOperator *) { return true; }
996
997  // Iterators
998  virtual child_iterator child_begin();
999  virtual child_iterator child_end();
1000
1001  virtual void EmitImpl(llvm::Serializer& S) const;
1002  static ConditionalOperator* CreateImpl(llvm::Deserializer& D);
1003};
1004
1005/// AddrLabelExpr - The GNU address of label extension, representing &&label.
1006class AddrLabelExpr : public Expr {
1007  SourceLocation AmpAmpLoc, LabelLoc;
1008  LabelStmt *Label;
1009public:
1010  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
1011                QualType t)
1012    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
1013
1014  virtual SourceRange getSourceRange() const {
1015    return SourceRange(AmpAmpLoc, LabelLoc);
1016  }
1017
1018  LabelStmt *getLabel() const { return Label; }
1019
1020  static bool classof(const Stmt *T) {
1021    return T->getStmtClass() == AddrLabelExprClass;
1022  }
1023  static bool classof(const AddrLabelExpr *) { return true; }
1024
1025  // Iterators
1026  virtual child_iterator child_begin();
1027  virtual child_iterator child_end();
1028
1029  virtual void EmitImpl(llvm::Serializer& S) const;
1030  static AddrLabelExpr* CreateImpl(llvm::Deserializer& D);
1031};
1032
1033/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
1034/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
1035/// takes the value of the last subexpression.
1036class StmtExpr : public Expr {
1037  CompoundStmt *SubStmt;
1038  SourceLocation LParenLoc, RParenLoc;
1039public:
1040  StmtExpr(CompoundStmt *substmt, QualType T,
1041           SourceLocation lp, SourceLocation rp) :
1042    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
1043
1044  CompoundStmt *getSubStmt() { return SubStmt; }
1045  const CompoundStmt *getSubStmt() const { return SubStmt; }
1046
1047  virtual SourceRange getSourceRange() const {
1048    return SourceRange(LParenLoc, RParenLoc);
1049  }
1050
1051  static bool classof(const Stmt *T) {
1052    return T->getStmtClass() == StmtExprClass;
1053  }
1054  static bool classof(const StmtExpr *) { return true; }
1055
1056  // Iterators
1057  virtual child_iterator child_begin();
1058  virtual child_iterator child_end();
1059
1060  virtual void EmitImpl(llvm::Serializer& S) const;
1061  static StmtExpr* CreateImpl(llvm::Deserializer& D);
1062};
1063
1064/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
1065/// This AST node represents a function that returns 1 if two *types* (not
1066/// expressions) are compatible. The result of this built-in function can be
1067/// used in integer constant expressions.
1068class TypesCompatibleExpr : public Expr {
1069  QualType Type1;
1070  QualType Type2;
1071  SourceLocation BuiltinLoc, RParenLoc;
1072public:
1073  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
1074                      QualType t1, QualType t2, SourceLocation RP) :
1075    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
1076    BuiltinLoc(BLoc), RParenLoc(RP) {}
1077
1078  QualType getArgType1() const { return Type1; }
1079  QualType getArgType2() const { return Type2; }
1080
1081  virtual SourceRange getSourceRange() const {
1082    return SourceRange(BuiltinLoc, RParenLoc);
1083  }
1084  static bool classof(const Stmt *T) {
1085    return T->getStmtClass() == TypesCompatibleExprClass;
1086  }
1087  static bool classof(const TypesCompatibleExpr *) { return true; }
1088
1089  // Iterators
1090  virtual child_iterator child_begin();
1091  virtual child_iterator child_end();
1092};
1093
1094/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
1095/// This AST node is similar to the conditional operator (?:) in C, with
1096/// the following exceptions:
1097/// - the test expression much be a constant expression.
1098/// - the expression returned has it's type unaltered by promotion rules.
1099/// - does not evaluate the expression that was not chosen.
1100class ChooseExpr : public Expr {
1101  enum { COND, LHS, RHS, END_EXPR };
1102  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1103  SourceLocation BuiltinLoc, RParenLoc;
1104public:
1105  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
1106             SourceLocation RP)
1107    : Expr(ChooseExprClass, t),
1108      BuiltinLoc(BLoc), RParenLoc(RP) {
1109      SubExprs[COND] = cond;
1110      SubExprs[LHS] = lhs;
1111      SubExprs[RHS] = rhs;
1112    }
1113
1114  /// isConditionTrue - Return true if the condition is true.  This is always
1115  /// statically knowable for a well-formed choosexpr.
1116  bool isConditionTrue(ASTContext &C) const;
1117
1118  Expr *getCond() const { return SubExprs[COND]; }
1119  Expr *getLHS() const { return SubExprs[LHS]; }
1120  Expr *getRHS() const { return SubExprs[RHS]; }
1121
1122  virtual SourceRange getSourceRange() const {
1123    return SourceRange(BuiltinLoc, RParenLoc);
1124  }
1125  static bool classof(const Stmt *T) {
1126    return T->getStmtClass() == ChooseExprClass;
1127  }
1128  static bool classof(const ChooseExpr *) { return true; }
1129
1130  // Iterators
1131  virtual child_iterator child_begin();
1132  virtual child_iterator child_end();
1133};
1134
1135/// OverloadExpr - Clang builtin-in function __builtin_overload.
1136/// This AST node provides a way to overload functions in C.
1137///
1138/// The first argument is required to be a constant expression, for the number
1139/// of arguments passed to each candidate function.
1140///
1141/// The next N arguments, where N is the value of the constant expression,
1142/// are the values to be passed as arguments.
1143///
1144/// The rest of the arguments are values of pointer to function type, which
1145/// are the candidate functions for overloading.
1146///
1147/// The result is a equivalent to a CallExpr taking N arguments to the
1148/// candidate function whose parameter types match the types of the N arguments.
1149///
1150/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl);
1151/// If X and Y are long doubles, Z will assigned the result of modl(X, Y);
1152/// If X and Y are floats, Z will be assigned the result of modf(X, Y);
1153class OverloadExpr : public Expr {
1154  // SubExprs - the list of values passed to the __builtin_overload function.
1155  // SubExpr[0] is a constant expression
1156  // SubExpr[1-N] are the parameters to pass to the matching function call
1157  // SubExpr[N-...] are the candidate functions, of type pointer to function.
1158  Expr **SubExprs;
1159
1160  // NumExprs - the size of the SubExprs array
1161  unsigned NumExprs;
1162
1163  // The index of the matching candidate function
1164  unsigned FnIndex;
1165
1166  SourceLocation BuiltinLoc;
1167  SourceLocation RParenLoc;
1168public:
1169  OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t,
1170               SourceLocation bloc, SourceLocation rploc)
1171    : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx),
1172      BuiltinLoc(bloc), RParenLoc(rploc) {
1173    SubExprs = new Expr*[nexprs];
1174    for (unsigned i = 0; i != nexprs; ++i)
1175      SubExprs[i] = args[i];
1176  }
1177  ~OverloadExpr() {
1178    delete [] SubExprs;
1179  }
1180
1181  /// arg_begin - Return a pointer to the list of arguments that will be passed
1182  /// to the matching candidate function, skipping over the initial constant
1183  /// expression.
1184  typedef Expr * const *arg_const_iterator;
1185  arg_const_iterator arg_begin() const { return SubExprs+1; }
1186
1187  /// getNumArgs - Return the number of arguments to pass to the candidate
1188  /// functions.
1189  unsigned getNumArgs(ASTContext &Ctx) const {
1190    llvm::APSInt constEval(32);
1191    (void) SubExprs[0]->isIntegerConstantExpr(constEval, Ctx);
1192    return constEval.getZExtValue();
1193  }
1194
1195  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1196  /// constant expression, the actual arguments passed in, and the function
1197  /// pointers.
1198  unsigned getNumSubExprs() const { return NumExprs; }
1199
1200  /// getExpr - Return the Expr at the specified index.
1201  Expr *getExpr(unsigned Index) {
1202    assert((Index < NumExprs) && "Arg access out of range!");
1203    return SubExprs[Index];
1204  }
1205
1206  /// getFn - Return the matching candidate function for this OverloadExpr.
1207  Expr *getFn() const { return SubExprs[FnIndex]; }
1208
1209  virtual SourceRange getSourceRange() const {
1210    return SourceRange(BuiltinLoc, RParenLoc);
1211  }
1212  static bool classof(const Stmt *T) {
1213    return T->getStmtClass() == OverloadExprClass;
1214  }
1215  static bool classof(const OverloadExpr *) { return true; }
1216
1217  // Iterators
1218  virtual child_iterator child_begin();
1219  virtual child_iterator child_end();
1220};
1221
1222/// VAArgExpr, used for the builtin function __builtin_va_start.
1223class VAArgExpr : public Expr {
1224  Expr *Val;
1225  SourceLocation BuiltinLoc, RParenLoc;
1226public:
1227  VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
1228    : Expr(VAArgExprClass, t),
1229      Val(e),
1230      BuiltinLoc(BLoc),
1231      RParenLoc(RPLoc) { }
1232
1233  const Expr *getSubExpr() const { return Val; }
1234  Expr *getSubExpr() { return Val; }
1235  virtual SourceRange getSourceRange() const {
1236    return SourceRange(BuiltinLoc, RParenLoc);
1237  }
1238  static bool classof(const Stmt *T) {
1239    return T->getStmtClass() == VAArgExprClass;
1240  }
1241  static bool classof(const VAArgExpr *) { return true; }
1242
1243  // Iterators
1244  virtual child_iterator child_begin();
1245  virtual child_iterator child_end();
1246};
1247
1248/// InitListExpr - used for struct and array initializers, such as:
1249///    struct foo x = { 1, { 2, 3 } };
1250///
1251/// Because C is somewhat loose with braces, the AST does not necessarily
1252/// directly model the C source.  Instead, the semantic analyzer aims to make
1253/// the InitListExprs match up with the type of the decl being initialized.  We
1254/// have the following exceptions:
1255///
1256///  1. Elements at the end of the list may be dropped from the initializer.
1257///     These elements are defined to be initialized to zero.  For example:
1258///         int x[20] = { 1 };
1259///  2. Initializers may have excess initializers which are to be ignored by the
1260///     compiler.  For example:
1261///         int x[1] = { 1, 2 };
1262///  3. Redundant InitListExprs may be present around scalar elements.  These
1263///     always have a single element whose type is the same as the InitListExpr.
1264///     this can only happen for Type::isScalarType() types.
1265///         int x = { 1 };  int y[2] = { {1}, {2} };
1266///
1267class InitListExpr : public Expr {
1268  Expr **InitExprs;
1269  unsigned NumInits;
1270  SourceLocation LBraceLoc, RBraceLoc;
1271public:
1272  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
1273               SourceLocation rbraceloc);
1274  ~InitListExpr() {
1275    delete [] InitExprs;
1276  }
1277
1278  unsigned getNumInits() const { return NumInits; }
1279
1280  const Expr* getInit(unsigned Init) const {
1281    assert(Init < NumInits && "Initializer access out of range!");
1282    return InitExprs[Init];
1283  }
1284
1285  Expr* getInit(unsigned Init) {
1286    assert(Init < NumInits && "Initializer access out of range!");
1287    return InitExprs[Init];
1288  }
1289
1290  void setInit(unsigned Init, Expr *expr) {
1291    assert(Init < NumInits && "Initializer access out of range!");
1292    InitExprs[Init] = expr;
1293  }
1294
1295  virtual SourceRange getSourceRange() const {
1296    return SourceRange(LBraceLoc, RBraceLoc);
1297  }
1298  static bool classof(const Stmt *T) {
1299    return T->getStmtClass() == InitListExprClass;
1300  }
1301  static bool classof(const InitListExpr *) { return true; }
1302
1303  // Iterators
1304  virtual child_iterator child_begin();
1305  virtual child_iterator child_end();
1306
1307  virtual void EmitImpl(llvm::Serializer& S) const;
1308  static InitListExpr* CreateImpl(llvm::Deserializer& D);
1309
1310private:
1311  // Used by serializer.
1312  InitListExpr() : Expr(InitListExprClass, QualType()),
1313                   InitExprs(NULL), NumInits(0) {}
1314};
1315
1316/// ObjCStringLiteral, used for Objective-C string literals
1317/// i.e. @"foo".
1318class ObjCStringLiteral : public Expr {
1319  StringLiteral *String;
1320  SourceLocation AtLoc;
1321public:
1322  ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L)
1323    : Expr(ObjCStringLiteralClass, T), String(SL), AtLoc(L) {}
1324
1325  StringLiteral* getString() { return String; }
1326
1327  const StringLiteral* getString() const { return String; }
1328
1329  SourceLocation getAtLoc() const { return AtLoc; }
1330
1331  virtual SourceRange getSourceRange() const {
1332    return SourceRange(AtLoc, String->getLocEnd());
1333  }
1334
1335  static bool classof(const Stmt *T) {
1336    return T->getStmtClass() == ObjCStringLiteralClass;
1337  }
1338  static bool classof(const ObjCStringLiteral *) { return true; }
1339
1340  // Iterators
1341  virtual child_iterator child_begin();
1342  virtual child_iterator child_end();
1343
1344  virtual void EmitImpl(llvm::Serializer& S) const;
1345  static ObjCStringLiteral* CreateImpl(llvm::Deserializer& D);
1346};
1347
1348/// ObjCEncodeExpr, used for @encode in Objective-C.
1349class ObjCEncodeExpr : public Expr {
1350  QualType EncType;
1351  SourceLocation AtLoc, RParenLoc;
1352public:
1353  ObjCEncodeExpr(QualType T, QualType ET,
1354                 SourceLocation at, SourceLocation rp)
1355    : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {}
1356
1357  SourceLocation getAtLoc() const { return AtLoc; }
1358  SourceLocation getRParenLoc() const { return RParenLoc; }
1359
1360  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1361
1362  QualType getEncodedType() const { return EncType; }
1363
1364  static bool classof(const Stmt *T) {
1365    return T->getStmtClass() == ObjCEncodeExprClass;
1366  }
1367  static bool classof(const ObjCEncodeExpr *) { return true; }
1368
1369  // Iterators
1370  virtual child_iterator child_begin();
1371  virtual child_iterator child_end();
1372
1373  virtual void EmitImpl(llvm::Serializer& S) const;
1374  static ObjCEncodeExpr* CreateImpl(llvm::Deserializer& D);
1375};
1376
1377/// ObjCSelectorExpr used for @selector in Objective-C.
1378class ObjCSelectorExpr : public Expr {
1379  Selector SelName;
1380  SourceLocation AtLoc, RParenLoc;
1381public:
1382  ObjCSelectorExpr(QualType T, Selector selInfo,
1383                   SourceLocation at, SourceLocation rp)
1384  : Expr(ObjCSelectorExprClass, T), SelName(selInfo),
1385  AtLoc(at), RParenLoc(rp) {}
1386
1387  const Selector &getSelector() const { return SelName; }
1388  Selector &getSelector() { return SelName; }
1389
1390  SourceLocation getAtLoc() const { return AtLoc; }
1391  SourceLocation getRParenLoc() const { return RParenLoc; }
1392  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1393
1394  /// getNumArgs - Return the number of actual arguments to this call.
1395  unsigned getNumArgs() const { return SelName.getNumArgs(); }
1396
1397  static bool classof(const Stmt *T) {
1398    return T->getStmtClass() == ObjCSelectorExprClass;
1399  }
1400  static bool classof(const ObjCSelectorExpr *) { return true; }
1401
1402  // Iterators
1403  virtual child_iterator child_begin();
1404  virtual child_iterator child_end();
1405
1406  virtual void EmitImpl(llvm::Serializer& S) const;
1407  static ObjCSelectorExpr* CreateImpl(llvm::Deserializer& D);
1408};
1409
1410/// ObjCProtocolExpr used for protocol in Objective-C.
1411class ObjCProtocolExpr : public Expr {
1412  ObjCProtocolDecl *Protocol;
1413  SourceLocation AtLoc, RParenLoc;
1414public:
1415  ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
1416                   SourceLocation at, SourceLocation rp)
1417  : Expr(ObjCProtocolExprClass, T), Protocol(protocol),
1418  AtLoc(at), RParenLoc(rp) {}
1419
1420  ObjCProtocolDecl *getProtocol() const { return Protocol; }
1421
1422  SourceLocation getAtLoc() const { return AtLoc; }
1423  SourceLocation getRParenLoc() const { return RParenLoc; }
1424  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1425
1426  static bool classof(const Stmt *T) {
1427    return T->getStmtClass() == ObjCProtocolExprClass;
1428  }
1429  static bool classof(const ObjCProtocolExpr *) { return true; }
1430
1431  // Iterators
1432  virtual child_iterator child_begin();
1433  virtual child_iterator child_end();
1434};
1435
1436/// ObjCIvarRefExpr - A reference to an ObjC instance variable.
1437class ObjCIvarRefExpr : public Expr {
1438  class ObjCIvarDecl *D;
1439  SourceLocation Loc;
1440  Expr *Base;
1441  bool IsArrow:1;      // True if this is "X->F", false if this is "X.F".
1442  bool IsFreeIvar:1;   // True if ivar reference has no base (self assumed).
1443
1444public:
1445  ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t, SourceLocation l, Expr *base=0,
1446                  bool arrow = false, bool freeIvar = false) :
1447    Expr(ObjCIvarRefExprClass, t), D(d), Loc(l), Base(base), IsArrow(arrow),
1448    IsFreeIvar(freeIvar) {}
1449
1450  ObjCIvarDecl *getDecl() { return D; }
1451  const ObjCIvarDecl *getDecl() const { return D; }
1452  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
1453  Expr *const getBase() const { return Base; }
1454  void setBase(Expr * base) { Base = base; }
1455  const bool isArrow() const { return IsArrow; }
1456  const bool isFreeIvar() const { return IsFreeIvar; }
1457
1458  SourceLocation getLocation() const { return Loc; }
1459
1460  static bool classof(const Stmt *T) {
1461    return T->getStmtClass() == ObjCIvarRefExprClass;
1462  }
1463  static bool classof(const ObjCIvarRefExpr *) { return true; }
1464
1465  // Iterators
1466  virtual child_iterator child_begin();
1467  virtual child_iterator child_end();
1468
1469  virtual void EmitImpl(llvm::Serializer& S) const;
1470  static ObjCIvarRefExpr* CreateImpl(llvm::Deserializer& D);
1471};
1472
1473class ObjCMessageExpr : public Expr {
1474  enum { RECEIVER=0, ARGS_START=1 };
1475
1476  Expr **SubExprs;
1477
1478  unsigned NumArgs;
1479
1480  // A unigue name for this message.
1481  Selector SelName;
1482
1483  // A method prototype for this message (optional).
1484  // FIXME: Since method decls contain the selector, and most messages have a
1485  // prototype, consider devising a scheme for unifying SelName/MethodProto.
1486  ObjCMethodDecl *MethodProto;
1487
1488  IdentifierInfo *ClassName; // optional - 0 for instance messages.
1489
1490  SourceLocation LBracloc, RBracloc;
1491public:
1492  // constructor for class messages.
1493  // FIXME: clsName should be typed to ObjCInterfaceType
1494  ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
1495                  QualType retType, ObjCMethodDecl *methDecl,
1496                  SourceLocation LBrac, SourceLocation RBrac,
1497                  Expr **ArgExprs, unsigned NumArgs);
1498  // constructor for instance messages.
1499  ObjCMessageExpr(Expr *receiver, Selector selInfo,
1500                  QualType retType, ObjCMethodDecl *methDecl,
1501                  SourceLocation LBrac, SourceLocation RBrac,
1502                  Expr **ArgExprs, unsigned NumArgs);
1503  ~ObjCMessageExpr() {
1504    delete [] SubExprs;
1505  }
1506
1507  const Expr *getReceiver() const { return SubExprs[RECEIVER]; }
1508  Expr *getReceiver() { return SubExprs[RECEIVER]; }
1509
1510  const Selector &getSelector() const { return SelName; }
1511  Selector &getSelector() { return SelName; }
1512
1513  const ObjCMethodDecl *getMethodDecl() const { return MethodProto; }
1514  ObjCMethodDecl *getMethodDecl() { return MethodProto; }
1515
1516  const IdentifierInfo *getClassName() const { return ClassName; }
1517  IdentifierInfo *getClassName() { return ClassName; }
1518
1519  /// getNumArgs - Return the number of actual arguments to this call.
1520  unsigned getNumArgs() const { return NumArgs; }
1521
1522/// getArg - Return the specified argument.
1523  Expr *getArg(unsigned Arg) {
1524    assert(Arg < NumArgs && "Arg access out of range!");
1525    return SubExprs[Arg+ARGS_START];
1526  }
1527  const Expr *getArg(unsigned Arg) const {
1528    assert(Arg < NumArgs && "Arg access out of range!");
1529    return SubExprs[Arg+ARGS_START];
1530  }
1531  /// setArg - Set the specified argument.
1532  void setArg(unsigned Arg, Expr *ArgExpr) {
1533    assert(Arg < NumArgs && "Arg access out of range!");
1534    SubExprs[Arg+ARGS_START] = ArgExpr;
1535  }
1536  SourceRange getSourceRange() const { return SourceRange(LBracloc, RBracloc); }
1537
1538  static bool classof(const Stmt *T) {
1539    return T->getStmtClass() == ObjCMessageExprClass;
1540  }
1541  static bool classof(const ObjCMessageExpr *) { return true; }
1542
1543  // Iterators
1544  virtual child_iterator child_begin();
1545  virtual child_iterator child_end();
1546};
1547
1548}  // end namespace clang
1549
1550#endif
1551