1 //===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Expr interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_EXPR_H
15 #define LLVM_CLANG_AST_EXPR_H
17 #include "clang/AST/APValue.h"
18 #include "clang/AST/Stmt.h"
19 #include "clang/AST/Type.h"
20 #include "llvm/ADT/APSInt.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
35 class CXXOperatorCallExpr;
36 class CXXMemberCallExpr;
37 class TemplateArgumentLoc;
38 class TemplateArgumentListInfo;
40 /// Expr - This represents one expression. Note that Expr's are subclasses of
41 /// Stmt. This allows an expression to be transparently used any place a Stmt
44 class Expr : public Stmt {
48 /// TypeDependent - Whether this expression is type-dependent
49 /// (C++ [temp.dep.expr]).
50 bool TypeDependent : 1;
52 /// ValueDependent - Whether this expression is value-dependent
53 /// (C++ [temp.dep.constexpr]).
54 bool ValueDependent : 1;
56 Expr(StmtClass SC, QualType T, bool TD, bool VD)
57 : Stmt(SC), TypeDependent(TD), ValueDependent(VD) {
61 /// \brief Construct an empty expression.
62 explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { }
65 /// \brief Increases the reference count for this expression.
67 /// Invoke the Retain() operation when this expression
68 /// is being shared by another owner.
74 QualType getType() const { return TR; }
75 void setType(QualType t) {
76 // In C++, the type of an expression is always adjusted so that it
77 // will not have reference type an expression will never have
78 // reference type (C++ [expr]p6). Use
79 // QualType::getNonReferenceType() to retrieve the non-reference
80 // type. Additionally, inspect Expr::isLvalue to determine whether
81 // an expression that is adjusted in this manner should be
82 // considered an lvalue.
83 assert((t.isNull() || !t->isReferenceType()) &&
84 "Expressions can't have reference type");
89 /// isValueDependent - Determines whether this expression is
90 /// value-dependent (C++ [temp.dep.constexpr]). For example, the
91 /// array bound of "Chars" in the following example is
94 /// template<int Size, char (&Chars)[Size]> struct meta_string;
96 bool isValueDependent() const { return ValueDependent; }
98 /// \brief Set whether this expression is value-dependent or not.
99 void setValueDependent(bool VD) { ValueDependent = VD; }
101 /// isTypeDependent - Determines whether this expression is
102 /// type-dependent (C++ [temp.dep.expr]), which means that its type
103 /// could change from one template instantiation to the next. For
104 /// example, the expressions "x" and "x + y" are type-dependent in
105 /// the following code, but "y" is not type-dependent:
107 /// template<typename T>
108 /// void add(T x, int y) {
112 bool isTypeDependent() const { return TypeDependent; }
114 /// \brief Set whether this expression is type-dependent or not.
115 void setTypeDependent(bool TD) { TypeDependent = TD; }
117 /// SourceLocation tokens are not useful in isolation - they are low level
118 /// value objects created/interpreted by SourceManager. We assume AST
119 /// clients will have a pointer to the respective SourceManager.
120 virtual SourceRange getSourceRange() const = 0;
122 /// getExprLoc - Return the preferred location for the arrow when diagnosing
123 /// a problem with a generic expression.
124 virtual SourceLocation getExprLoc() const { return getLocStart(); }
126 /// isUnusedResultAWarning - Return true if this immediate expression should
127 /// be warned about if the result is unused. If so, fill in Loc and Ranges
128 /// with location to warn on and the source range[s] to report with the
130 bool isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
131 SourceRange &R2, ASTContext &Ctx) const;
133 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
134 /// incomplete type other than void. Nonarray expressions that can be lvalues:
135 /// - name, where name must be a variable
137 /// - (e), where e must be an lvalue
138 /// - e.name, where e must be an lvalue
140 /// - *e, the type of e cannot be a function type
141 /// - string-constant
142 /// - reference type [C++ [expr]]
143 /// - b ? x : y, where x and y are lvalues of suitable types [C++]
145 enum isLvalueResult {
148 LV_IncompleteVoidType,
149 LV_DuplicateVectorComponents,
150 LV_InvalidExpression,
152 LV_SubObjCPropertySetting,
153 LV_SubObjCPropertyGetterSetting,
156 isLvalueResult isLvalue(ASTContext &Ctx) const;
158 // Same as above, but excluding checks for non-object and void types in C
159 isLvalueResult isLvalueInternal(ASTContext &Ctx) const;
161 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
162 /// does not have an incomplete type, does not have a const-qualified type,
163 /// and if it is a structure or union, does not have any member (including,
164 /// recursively, any member or element of all contained aggregates or unions)
165 /// with a const-qualified type.
167 /// \param Loc [in] [out] - A source location which *may* be filled
168 /// in with the location of the expression making this a
169 /// non-modifiable lvalue, if specified.
170 enum isModifiableLvalueResult {
173 MLV_IncompleteVoidType,
174 MLV_DuplicateVectorComponents,
175 MLV_InvalidExpression,
176 MLV_LValueCast, // Specialized form of MLV_InvalidExpression.
180 MLV_NotBlockQualified,
181 MLV_ReadonlyProperty,
182 MLV_NoSetterProperty,
184 MLV_SubObjCPropertySetting,
185 MLV_SubObjCPropertyGetterSetting,
188 isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx,
189 SourceLocation *Loc = 0) const;
191 /// \brief If this expression refers to a bit-field, retrieve the
192 /// declaration of that bit-field.
193 FieldDecl *getBitField();
195 const FieldDecl *getBitField() const {
196 return const_cast<Expr*>(this)->getBitField();
199 /// \brief Returns whether this expression refers to a vector element.
200 bool refersToVectorElement() const;
202 /// isIntegerConstantExpr - Return true if this expression is a valid integer
203 /// constant expression, and, if so, return its value in Result. If not a
204 /// valid i-c-e, return false and fill in Loc (if specified) with the location
205 /// of the invalid expression.
206 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
207 SourceLocation *Loc = 0,
208 bool isEvaluated = true) const;
209 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
211 return isIntegerConstantExpr(X, Ctx, Loc);
213 /// isConstantInitializer - Returns true if this expression is a constant
214 /// initializer, which can be emitted at compile-time.
215 bool isConstantInitializer(ASTContext &Ctx) const;
217 /// EvalResult is a struct with detailed info about an evaluated expression.
219 /// Val - This is the value the expression can be folded to.
222 /// HasSideEffects - Whether the evaluated expression has side effects.
223 /// For example, (f() && 0) can be folded, but it still has side effects.
226 /// Diag - If the expression is unfoldable, then Diag contains a note
227 /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret
228 /// position for the error, and DiagExpr is the expression that caused
230 /// If the expression is foldable, but not an integer constant expression,
231 /// Diag contains a note diagnostic that describes why it isn't an integer
232 /// constant expression. If the expression *is* an integer constant
233 /// expression, then Diag will be zero.
235 const Expr *DiagExpr;
236 SourceLocation DiagLoc;
238 EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {}
241 /// Evaluate - Return true if this is a constant which we can fold using
242 /// any crazy technique (that has nothing to do with language standards) that
243 /// we want to. If this function returns true, it returns the folded constant
245 bool Evaluate(EvalResult &Result, ASTContext &Ctx) const;
247 /// EvaluateAsAny - The same as Evaluate, except that it also succeeds on
248 /// stack based objects.
249 bool EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const;
251 /// EvaluateAsBooleanCondition - Return true if this is a constant
252 /// which we we can fold and convert to a boolean condition using
253 /// any crazy technique that we want to.
254 bool EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const;
256 /// isEvaluatable - Call Evaluate to see if this expression can be constant
257 /// folded, but discard the result.
258 bool isEvaluatable(ASTContext &Ctx) const;
260 /// HasSideEffects - This routine returns true for all those expressions
261 /// which must be evaluated each time and must not be optimization away
262 /// or evaluated at compile time. Example is a function call, volatile
264 bool HasSideEffects(ASTContext &Ctx) const;
266 /// EvaluateAsInt - Call Evaluate and return the folded integer. This
267 /// must be called on an expression that constant folds to an integer.
268 llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const;
270 /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue
271 /// with link time known address.
272 bool EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const;
274 /// EvaluateAsAnyLValue - The same as EvaluateAsLValue, except that it
275 /// also succeeds on stack based, immutable address lvalues.
276 bool EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const;
278 /// \brief Enumeration used to describe how \c isNullPointerConstant()
279 /// should cope with value-dependent expressions.
280 enum NullPointerConstantValueDependence {
281 /// \brief Specifies that the expression should never be value-dependent.
282 NPC_NeverValueDependent = 0,
284 /// \brief Specifies that a value-dependent expression of integral or
285 /// dependent type should be considered a null pointer constant.
286 NPC_ValueDependentIsNull,
288 /// \brief Specifies that a value-dependent expression should be considered
289 /// to never be a null pointer constant.
290 NPC_ValueDependentIsNotNull
293 /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
294 /// integer constant expression with the value zero, or if this is one that is
296 bool isNullPointerConstant(ASTContext &Ctx,
297 NullPointerConstantValueDependence NPC) const;
299 /// isOBJCGCCandidate - Return true if this expression may be used in a read/
301 bool isOBJCGCCandidate(ASTContext &Ctx) const;
303 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return
304 /// its subexpression. If that subexpression is also a ParenExpr,
305 /// then this method recursively returns its subexpression, and so forth.
306 /// Otherwise, the method returns the current Expr.
307 Expr* IgnoreParens();
309 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
310 /// or CastExprs, returning their operand.
311 Expr *IgnoreParenCasts();
313 /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
314 /// value (including ptr->int casts of the same size). Strip off any
315 /// ParenExpr or CastExprs, returning their operand.
316 Expr *IgnoreParenNoopCasts(ASTContext &Ctx);
318 /// \brief Determine whether this expression is a default function argument.
320 /// Default arguments are implicitly generated in the abstract syntax tree
321 /// by semantic analysis for function calls, object constructions, etc. in
322 /// C++. Default arguments are represented by \c CXXDefaultArgExpr nodes;
323 /// this routine also looks through any implicit casts to determine whether
324 /// the expression is a default argument.
325 bool isDefaultArgument() const;
327 const Expr* IgnoreParens() const {
328 return const_cast<Expr*>(this)->IgnoreParens();
330 const Expr *IgnoreParenCasts() const {
331 return const_cast<Expr*>(this)->IgnoreParenCasts();
333 const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const {
334 return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx);
337 static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs);
338 static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs);
340 static bool classof(const Stmt *T) {
341 return T->getStmtClass() >= firstExprConstant &&
342 T->getStmtClass() <= lastExprConstant;
344 static bool classof(const Expr *) { return true; }
348 //===----------------------------------------------------------------------===//
349 // Primary Expressions.
350 //===----------------------------------------------------------------------===//
352 /// \brief Represents the qualifier that may precede a C++ name, e.g., the
353 /// "std::" in "std::sort".
354 struct NameQualifier {
355 /// \brief The nested name specifier.
356 NestedNameSpecifier *NNS;
358 /// \brief The source range covered by the nested name specifier.
362 /// \brief Represents an explicit template argument list in C++, e.g.,
363 /// the "<int>" in "sort<int>".
364 struct ExplicitTemplateArgumentList {
365 /// \brief The source location of the left angle bracket ('<');
366 SourceLocation LAngleLoc;
368 /// \brief The source location of the right angle bracket ('>');
369 SourceLocation RAngleLoc;
371 /// \brief The number of template arguments in TemplateArgs.
372 /// The actual template arguments (if any) are stored after the
373 /// ExplicitTemplateArgumentList structure.
374 unsigned NumTemplateArgs;
376 /// \brief Retrieve the template arguments
377 TemplateArgumentLoc *getTemplateArgs() {
378 return reinterpret_cast<TemplateArgumentLoc *> (this + 1);
381 /// \brief Retrieve the template arguments
382 const TemplateArgumentLoc *getTemplateArgs() const {
383 return reinterpret_cast<const TemplateArgumentLoc *> (this + 1);
386 void initializeFrom(const TemplateArgumentListInfo &List);
387 void copyInto(TemplateArgumentListInfo &List) const;
388 static std::size_t sizeFor(const TemplateArgumentListInfo &List);
391 /// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
393 class DeclRefExpr : public Expr {
395 // Flag on DecoratedD that specifies when this declaration reference
396 // expression has a C++ nested-name-specifier.
397 HasQualifierFlag = 0x01,
398 // Flag on DecoratedD that specifies when this declaration reference
399 // expression has an explicit C++ template argument list.
400 HasExplicitTemplateArgumentListFlag = 0x02
403 // DecoratedD - The declaration that we are referencing, plus two bits to
404 // indicate whether (1) the declaration's name was explicitly qualified and
405 // (2) the declaration's name was followed by an explicit template
407 llvm::PointerIntPair<ValueDecl *, 2> DecoratedD;
409 // Loc - The location of the declaration name itself.
412 /// \brief Retrieve the qualifier that preceded the declaration name, if any.
413 NameQualifier *getNameQualifier() {
414 if ((DecoratedD.getInt() & HasQualifierFlag) == 0)
417 return reinterpret_cast<NameQualifier *> (this + 1);
420 /// \brief Retrieve the qualifier that preceded the member name, if any.
421 const NameQualifier *getNameQualifier() const {
422 return const_cast<DeclRefExpr *>(this)->getNameQualifier();
425 /// \brief Retrieve the explicit template argument list that followed the
426 /// member template name, if any.
427 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() {
428 if ((DecoratedD.getInt() & HasExplicitTemplateArgumentListFlag) == 0)
431 if ((DecoratedD.getInt() & HasQualifierFlag) == 0)
432 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
434 return reinterpret_cast<ExplicitTemplateArgumentList *>(
435 getNameQualifier() + 1);
438 /// \brief Retrieve the explicit template argument list that followed the
439 /// member template name, if any.
440 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const {
441 return const_cast<DeclRefExpr *>(this)->getExplicitTemplateArgumentList();
444 DeclRefExpr(NestedNameSpecifier *Qualifier, SourceRange QualifierRange,
445 ValueDecl *D, SourceLocation NameLoc,
446 const TemplateArgumentListInfo *TemplateArgs,
450 /// \brief Computes the type- and value-dependence flags for this
451 /// declaration reference expression.
452 void computeDependence();
454 DeclRefExpr(StmtClass SC, ValueDecl *d, QualType t, SourceLocation l) :
455 Expr(SC, t, false, false), DecoratedD(d, 0), Loc(l) {
460 DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) :
461 Expr(DeclRefExprClass, t, false, false), DecoratedD(d, 0), Loc(l) {
465 /// \brief Construct an empty declaration reference expression.
466 explicit DeclRefExpr(EmptyShell Empty)
467 : Expr(DeclRefExprClass, Empty) { }
469 static DeclRefExpr *Create(ASTContext &Context,
470 NestedNameSpecifier *Qualifier,
471 SourceRange QualifierRange,
473 SourceLocation NameLoc,
475 const TemplateArgumentListInfo *TemplateArgs = 0);
477 ValueDecl *getDecl() { return DecoratedD.getPointer(); }
478 const ValueDecl *getDecl() const { return DecoratedD.getPointer(); }
479 void setDecl(ValueDecl *NewD) { DecoratedD.setPointer(NewD); }
481 SourceLocation getLocation() const { return Loc; }
482 void setLocation(SourceLocation L) { Loc = L; }
483 virtual SourceRange getSourceRange() const;
485 /// \brief Determine whether this declaration reference was preceded by a
486 /// C++ nested-name-specifier, e.g., \c N::foo.
487 bool hasQualifier() const { return DecoratedD.getInt() & HasQualifierFlag; }
489 /// \brief If the name was qualified, retrieves the source range of
490 /// the nested-name-specifier that precedes the name. Otherwise,
491 /// returns an empty source range.
492 SourceRange getQualifierRange() const {
494 return SourceRange();
496 return getNameQualifier()->Range;
499 /// \brief If the name was qualified, retrieves the nested-name-specifier
500 /// that precedes the name. Otherwise, returns NULL.
501 NestedNameSpecifier *getQualifier() const {
505 return getNameQualifier()->NNS;
508 /// \brief Determines whether this member expression actually had a C++
509 /// template argument list explicitly specified, e.g., x.f<int>.
510 bool hasExplicitTemplateArgumentList() const {
511 return DecoratedD.getInt() & HasExplicitTemplateArgumentListFlag;
514 /// \brief Copies the template arguments (if present) into the given
516 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
517 if (hasExplicitTemplateArgumentList())
518 getExplicitTemplateArgumentList()->copyInto(List);
521 /// \brief Retrieve the location of the left angle bracket following the
522 /// member name ('<'), if any.
523 SourceLocation getLAngleLoc() const {
524 if (!hasExplicitTemplateArgumentList())
525 return SourceLocation();
527 return getExplicitTemplateArgumentList()->LAngleLoc;
530 /// \brief Retrieve the template arguments provided as part of this
532 const TemplateArgumentLoc *getTemplateArgs() const {
533 if (!hasExplicitTemplateArgumentList())
536 return getExplicitTemplateArgumentList()->getTemplateArgs();
539 /// \brief Retrieve the number of template arguments provided as part of this
541 unsigned getNumTemplateArgs() const {
542 if (!hasExplicitTemplateArgumentList())
545 return getExplicitTemplateArgumentList()->NumTemplateArgs;
548 /// \brief Retrieve the location of the right angle bracket following the
549 /// template arguments ('>').
550 SourceLocation getRAngleLoc() const {
551 if (!hasExplicitTemplateArgumentList())
552 return SourceLocation();
554 return getExplicitTemplateArgumentList()->RAngleLoc;
557 static bool classof(const Stmt *T) {
558 return T->getStmtClass() == DeclRefExprClass;
560 static bool classof(const DeclRefExpr *) { return true; }
563 virtual child_iterator child_begin();
564 virtual child_iterator child_end();
567 /// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__.
568 class PredefinedExpr : public Expr {
574 /// PrettyFunctionNoVirtual - The same as PrettyFunction, except that the
575 /// 'virtual' keyword is omitted for virtual member functions.
576 PrettyFunctionNoVirtual
583 PredefinedExpr(SourceLocation l, QualType type, IdentType IT)
584 : Expr(PredefinedExprClass, type, type->isDependentType(),
585 type->isDependentType()), Loc(l), Type(IT) {}
587 /// \brief Construct an empty predefined expression.
588 explicit PredefinedExpr(EmptyShell Empty)
589 : Expr(PredefinedExprClass, Empty) { }
591 IdentType getIdentType() const { return Type; }
592 void setIdentType(IdentType IT) { Type = IT; }
594 SourceLocation getLocation() const { return Loc; }
595 void setLocation(SourceLocation L) { Loc = L; }
597 static std::string ComputeName(IdentType IT, const Decl *CurrentDecl);
599 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
601 static bool classof(const Stmt *T) {
602 return T->getStmtClass() == PredefinedExprClass;
604 static bool classof(const PredefinedExpr *) { return true; }
607 virtual child_iterator child_begin();
608 virtual child_iterator child_end();
611 class IntegerLiteral : public Expr {
615 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
616 // or UnsignedLongLongTy
617 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
618 : Expr(IntegerLiteralClass, type, false, false), Value(V), Loc(l) {
619 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
622 /// \brief Construct an empty integer literal.
623 explicit IntegerLiteral(EmptyShell Empty)
624 : Expr(IntegerLiteralClass, Empty) { }
626 const llvm::APInt &getValue() const { return Value; }
627 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
629 /// \brief Retrieve the location of the literal.
630 SourceLocation getLocation() const { return Loc; }
632 void setValue(const llvm::APInt &Val) { Value = Val; }
633 void setLocation(SourceLocation Location) { Loc = Location; }
635 static bool classof(const Stmt *T) {
636 return T->getStmtClass() == IntegerLiteralClass;
638 static bool classof(const IntegerLiteral *) { return true; }
641 virtual child_iterator child_begin();
642 virtual child_iterator child_end();
645 class CharacterLiteral : public Expr {
650 // type should be IntTy
651 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l)
652 : Expr(CharacterLiteralClass, type, false, false), Value(value), Loc(l),
656 /// \brief Construct an empty character literal.
657 CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { }
659 SourceLocation getLocation() const { return Loc; }
660 bool isWide() const { return IsWide; }
662 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
664 unsigned getValue() const { return Value; }
666 void setLocation(SourceLocation Location) { Loc = Location; }
667 void setWide(bool W) { IsWide = W; }
668 void setValue(unsigned Val) { Value = Val; }
670 static bool classof(const Stmt *T) {
671 return T->getStmtClass() == CharacterLiteralClass;
673 static bool classof(const CharacterLiteral *) { return true; }
676 virtual child_iterator child_begin();
677 virtual child_iterator child_end();
680 class FloatingLiteral : public Expr {
685 FloatingLiteral(const llvm::APFloat &V, bool isexact,
686 QualType Type, SourceLocation L)
687 : Expr(FloatingLiteralClass, Type, false, false), Value(V),
688 IsExact(isexact), Loc(L) {}
690 /// \brief Construct an empty floating-point literal.
691 explicit FloatingLiteral(EmptyShell Empty)
692 : Expr(FloatingLiteralClass, Empty), Value(0.0) { }
694 const llvm::APFloat &getValue() const { return Value; }
695 void setValue(const llvm::APFloat &Val) { Value = Val; }
697 bool isExact() const { return IsExact; }
698 void setExact(bool E) { IsExact = E; }
700 /// getValueAsApproximateDouble - This returns the value as an inaccurate
701 /// double. Note that this may cause loss of precision, but is useful for
702 /// debugging dumps, etc.
703 double getValueAsApproximateDouble() const;
705 SourceLocation getLocation() const { return Loc; }
706 void setLocation(SourceLocation L) { Loc = L; }
708 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
710 static bool classof(const Stmt *T) {
711 return T->getStmtClass() == FloatingLiteralClass;
713 static bool classof(const FloatingLiteral *) { return true; }
716 virtual child_iterator child_begin();
717 virtual child_iterator child_end();
720 /// ImaginaryLiteral - We support imaginary integer and floating point literals,
721 /// like "1.0i". We represent these as a wrapper around FloatingLiteral and
722 /// IntegerLiteral classes. Instances of this class always have a Complex type
723 /// whose element type matches the subexpression.
725 class ImaginaryLiteral : public Expr {
728 ImaginaryLiteral(Expr *val, QualType Ty)
729 : Expr(ImaginaryLiteralClass, Ty, false, false), Val(val) {}
731 /// \brief Build an empty imaginary literal.
732 explicit ImaginaryLiteral(EmptyShell Empty)
733 : Expr(ImaginaryLiteralClass, Empty) { }
735 const Expr *getSubExpr() const { return cast<Expr>(Val); }
736 Expr *getSubExpr() { return cast<Expr>(Val); }
737 void setSubExpr(Expr *E) { Val = E; }
739 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); }
740 static bool classof(const Stmt *T) {
741 return T->getStmtClass() == ImaginaryLiteralClass;
743 static bool classof(const ImaginaryLiteral *) { return true; }
746 virtual child_iterator child_begin();
747 virtual child_iterator child_end();
750 /// StringLiteral - This represents a string literal expression, e.g. "foo"
751 /// or L"bar" (wide strings). The actual string is returned by getStrData()
752 /// is NOT null-terminated, and the length of the string is determined by
753 /// calling getByteLength(). The C type for a string is always a
754 /// ConstantArrayType. In C++, the char type is const qualified, in C it is
757 /// Note that strings in C can be formed by concatenation of multiple string
758 /// literal pptokens in translation phase #6. This keeps track of the locations
759 /// of each of these pieces.
761 /// Strings in C can also be truncated and extended by assigning into arrays,
762 /// e.g. with constructs like:
763 /// char X[2] = "foobar";
764 /// In this case, getByteLength() will return 6, but the string literal will
765 /// have type "char[2]".
766 class StringLiteral : public Expr {
770 unsigned NumConcatenated;
771 SourceLocation TokLocs[1];
773 StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty, false, false) {}
776 virtual void DoDestroy(ASTContext &C);
779 /// This is the "fully general" constructor that allows representation of
780 /// strings formed from multiple concatenated tokens.
781 static StringLiteral *Create(ASTContext &C, const char *StrData,
782 unsigned ByteLength, bool Wide, QualType Ty,
783 const SourceLocation *Loc, unsigned NumStrs);
785 /// Simple constructor for string literals made from one token.
786 static StringLiteral *Create(ASTContext &C, const char *StrData,
788 bool Wide, QualType Ty, SourceLocation Loc) {
789 return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1);
792 /// \brief Construct an empty string literal.
793 static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs);
795 llvm::StringRef getString() const {
796 return llvm::StringRef(StrData, ByteLength);
798 // FIXME: These are deprecated, replace with StringRef.
799 const char *getStrData() const { return StrData; }
800 unsigned getByteLength() const { return ByteLength; }
802 /// \brief Sets the string data to the given string data.
803 void setString(ASTContext &C, llvm::StringRef Str);
805 bool isWide() const { return IsWide; }
806 void setWide(bool W) { IsWide = W; }
808 bool containsNonAsciiOrNull() const {
809 llvm::StringRef Str = getString();
810 for (unsigned i = 0, e = Str.size(); i != e; ++i)
811 if (!isascii(Str[i]) || !Str[i])
815 /// getNumConcatenated - Get the number of string literal tokens that were
816 /// concatenated in translation phase #6 to form this string literal.
817 unsigned getNumConcatenated() const { return NumConcatenated; }
819 SourceLocation getStrTokenLoc(unsigned TokNum) const {
820 assert(TokNum < NumConcatenated && "Invalid tok number");
821 return TokLocs[TokNum];
823 void setStrTokenLoc(unsigned TokNum, SourceLocation L) {
824 assert(TokNum < NumConcatenated && "Invalid tok number");
828 typedef const SourceLocation *tokloc_iterator;
829 tokloc_iterator tokloc_begin() const { return TokLocs; }
830 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; }
832 virtual SourceRange getSourceRange() const {
833 return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]);
835 static bool classof(const Stmt *T) {
836 return T->getStmtClass() == StringLiteralClass;
838 static bool classof(const StringLiteral *) { return true; }
841 virtual child_iterator child_begin();
842 virtual child_iterator child_end();
845 /// ParenExpr - This represents a parethesized expression, e.g. "(1)". This
846 /// AST node is only formed if full location information is requested.
847 class ParenExpr : public Expr {
851 ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
852 : Expr(ParenExprClass, val->getType(),
853 val->isTypeDependent(), val->isValueDependent()),
854 L(l), R(r), Val(val) {}
856 /// \brief Construct an empty parenthesized expression.
857 explicit ParenExpr(EmptyShell Empty)
858 : Expr(ParenExprClass, Empty) { }
860 const Expr *getSubExpr() const { return cast<Expr>(Val); }
861 Expr *getSubExpr() { return cast<Expr>(Val); }
862 void setSubExpr(Expr *E) { Val = E; }
864 virtual SourceRange getSourceRange() const { return SourceRange(L, R); }
866 /// \brief Get the location of the left parentheses '('.
867 SourceLocation getLParen() const { return L; }
868 void setLParen(SourceLocation Loc) { L = Loc; }
870 /// \brief Get the location of the right parentheses ')'.
871 SourceLocation getRParen() const { return R; }
872 void setRParen(SourceLocation Loc) { R = Loc; }
874 static bool classof(const Stmt *T) {
875 return T->getStmtClass() == ParenExprClass;
877 static bool classof(const ParenExpr *) { return true; }
880 virtual child_iterator child_begin();
881 virtual child_iterator child_end();
885 /// UnaryOperator - This represents the unary-expression's (except sizeof and
886 /// alignof), the postinc/postdec operators from postfix-expression, and various
889 /// Notes on various nodes:
891 /// Real/Imag - These return the real/imag part of a complex operand. If
892 /// applied to a non-complex value, the former returns its operand and the
893 /// later returns zero in the type of the operand.
895 /// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose
896 /// subexpression is a compound literal with the various MemberExpr and
897 /// ArraySubscriptExpr's applied to it.
899 class UnaryOperator : public Expr {
901 // Note that additions to this should also update the StmtVisitor class.
903 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
904 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators.
905 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators.
906 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators.
907 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators.
908 Real, Imag, // "__real expr"/"__imag expr" Extension.
909 Extension, // __extension__ marker.
910 OffsetOf // __builtin_offsetof
918 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
919 : Expr(UnaryOperatorClass, type,
920 input->isTypeDependent() && opc != OffsetOf,
921 input->isValueDependent()),
922 Val(input), Opc(opc), Loc(l) {}
924 /// \brief Build an empty unary operator.
925 explicit UnaryOperator(EmptyShell Empty)
926 : Expr(UnaryOperatorClass, Empty), Opc(AddrOf) { }
928 Opcode getOpcode() const { return Opc; }
929 void setOpcode(Opcode O) { Opc = O; }
931 Expr *getSubExpr() const { return cast<Expr>(Val); }
932 void setSubExpr(Expr *E) { Val = E; }
934 /// getOperatorLoc - Return the location of the operator.
935 SourceLocation getOperatorLoc() const { return Loc; }
936 void setOperatorLoc(SourceLocation L) { Loc = L; }
938 /// isPostfix - Return true if this is a postfix operation, like x++.
939 static bool isPostfix(Opcode Op) {
940 return Op == PostInc || Op == PostDec;
943 /// isPostfix - Return true if this is a prefix operation, like --x.
944 static bool isPrefix(Opcode Op) {
945 return Op == PreInc || Op == PreDec;
948 bool isPrefix() const { return isPrefix(Opc); }
949 bool isPostfix() const { return isPostfix(Opc); }
950 bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; }
951 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
952 bool isOffsetOfOp() const { return Opc == OffsetOf; }
953 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
954 bool isArithmeticOp() const { return isArithmeticOp(Opc); }
956 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
957 /// corresponds to, e.g. "sizeof" or "[pre]++"
958 static const char *getOpcodeStr(Opcode Op);
960 /// \brief Retrieve the unary opcode that corresponds to the given
961 /// overloaded operator.
962 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix);
964 /// \brief Retrieve the overloaded operator kind that corresponds to
965 /// the given unary opcode.
966 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc);
968 virtual SourceRange getSourceRange() const {
970 return SourceRange(Val->getLocStart(), Loc);
972 return SourceRange(Loc, Val->getLocEnd());
974 virtual SourceLocation getExprLoc() const { return Loc; }
976 static bool classof(const Stmt *T) {
977 return T->getStmtClass() == UnaryOperatorClass;
979 static bool classof(const UnaryOperator *) { return true; }
982 virtual child_iterator child_begin();
983 virtual child_iterator child_end();
986 /// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of
987 /// types and expressions.
988 class SizeOfAlignOfExpr : public Expr {
989 bool isSizeof : 1; // true if sizeof, false if alignof.
990 bool isType : 1; // true if operand is a type, false if an expression
995 SourceLocation OpLoc, RParenLoc;
998 virtual void DoDestroy(ASTContext& C);
1001 SizeOfAlignOfExpr(bool issizeof, TypeSourceInfo *TInfo,
1002 QualType resultType, SourceLocation op,
1003 SourceLocation rp) :
1004 Expr(SizeOfAlignOfExprClass, resultType,
1005 false, // Never type-dependent (C++ [temp.dep.expr]p3).
1006 // Value-dependent if the argument is type-dependent.
1007 TInfo->getType()->isDependentType()),
1008 isSizeof(issizeof), isType(true), OpLoc(op), RParenLoc(rp) {
1009 Argument.Ty = TInfo;
1012 SizeOfAlignOfExpr(bool issizeof, Expr *E,
1013 QualType resultType, SourceLocation op,
1014 SourceLocation rp) :
1015 Expr(SizeOfAlignOfExprClass, resultType,
1016 false, // Never type-dependent (C++ [temp.dep.expr]p3).
1017 // Value-dependent if the argument is type-dependent.
1018 E->isTypeDependent()),
1019 isSizeof(issizeof), isType(false), OpLoc(op), RParenLoc(rp) {
1023 /// \brief Construct an empty sizeof/alignof expression.
1024 explicit SizeOfAlignOfExpr(EmptyShell Empty)
1025 : Expr(SizeOfAlignOfExprClass, Empty) { }
1027 bool isSizeOf() const { return isSizeof; }
1028 void setSizeof(bool S) { isSizeof = S; }
1030 bool isArgumentType() const { return isType; }
1031 QualType getArgumentType() const {
1032 return getArgumentTypeInfo()->getType();
1034 TypeSourceInfo *getArgumentTypeInfo() const {
1035 assert(isArgumentType() && "calling getArgumentType() when arg is expr");
1038 Expr *getArgumentExpr() {
1039 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type");
1040 return static_cast<Expr*>(Argument.Ex);
1042 const Expr *getArgumentExpr() const {
1043 return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr();
1046 void setArgument(Expr *E) { Argument.Ex = E; isType = false; }
1047 void setArgument(TypeSourceInfo *TInfo) {
1048 Argument.Ty = TInfo;
1052 /// Gets the argument type, or the type of the argument expression, whichever
1054 QualType getTypeOfArgument() const {
1055 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType();
1058 SourceLocation getOperatorLoc() const { return OpLoc; }
1059 void setOperatorLoc(SourceLocation L) { OpLoc = L; }
1061 SourceLocation getRParenLoc() const { return RParenLoc; }
1062 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1064 virtual SourceRange getSourceRange() const {
1065 return SourceRange(OpLoc, RParenLoc);
1068 static bool classof(const Stmt *T) {
1069 return T->getStmtClass() == SizeOfAlignOfExprClass;
1071 static bool classof(const SizeOfAlignOfExpr *) { return true; }
1074 virtual child_iterator child_begin();
1075 virtual child_iterator child_end();
1078 //===----------------------------------------------------------------------===//
1079 // Postfix Operators.
1080 //===----------------------------------------------------------------------===//
1082 /// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
1083 class ArraySubscriptExpr : public Expr {
1084 enum { LHS, RHS, END_EXPR=2 };
1085 Stmt* SubExprs[END_EXPR];
1086 SourceLocation RBracketLoc;
1088 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
1089 SourceLocation rbracketloc)
1090 : Expr(ArraySubscriptExprClass, t,
1091 lhs->isTypeDependent() || rhs->isTypeDependent(),
1092 lhs->isValueDependent() || rhs->isValueDependent()),
1093 RBracketLoc(rbracketloc) {
1094 SubExprs[LHS] = lhs;
1095 SubExprs[RHS] = rhs;
1098 /// \brief Create an empty array subscript expression.
1099 explicit ArraySubscriptExpr(EmptyShell Shell)
1100 : Expr(ArraySubscriptExprClass, Shell) { }
1102 /// An array access can be written A[4] or 4[A] (both are equivalent).
1103 /// - getBase() and getIdx() always present the normalized view: A[4].
1104 /// In this case getBase() returns "A" and getIdx() returns "4".
1105 /// - getLHS() and getRHS() present the syntactic view. e.g. for
1106 /// 4[A] getLHS() returns "4".
1107 /// Note: Because vector element access is also written A[4] we must
1108 /// predicate the format conversion in getBase and getIdx only on the
1109 /// the type of the RHS, as it is possible for the LHS to be a vector of
1111 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); }
1112 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1113 void setLHS(Expr *E) { SubExprs[LHS] = E; }
1115 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); }
1116 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1117 void setRHS(Expr *E) { SubExprs[RHS] = E; }
1120 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
1123 const Expr *getBase() const {
1124 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
1128 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
1131 const Expr *getIdx() const {
1132 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
1135 virtual SourceRange getSourceRange() const {
1136 return SourceRange(getLHS()->getLocStart(), RBracketLoc);
1139 SourceLocation getRBracketLoc() const { return RBracketLoc; }
1140 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
1142 virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); }
1144 static bool classof(const Stmt *T) {
1145 return T->getStmtClass() == ArraySubscriptExprClass;
1147 static bool classof(const ArraySubscriptExpr *) { return true; }
1150 virtual child_iterator child_begin();
1151 virtual child_iterator child_end();
1155 /// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
1156 /// CallExpr itself represents a normal function call, e.g., "f(x, 2)",
1157 /// while its subclasses may represent alternative syntax that (semantically)
1158 /// results in a function call. For example, CXXOperatorCallExpr is
1159 /// a subclass for overloaded operator calls that use operator syntax, e.g.,
1160 /// "str1 + str2" to resolve to a function call.
1161 class CallExpr : public Expr {
1162 enum { FN=0, ARGS_START=1 };
1165 SourceLocation RParenLoc;
1168 // This version of the constructor is for derived classes.
1169 CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs,
1170 QualType t, SourceLocation rparenloc);
1172 virtual void DoDestroy(ASTContext& C);
1175 CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t,
1176 SourceLocation rparenloc);
1178 /// \brief Build an empty call expression.
1179 CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty);
1183 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); }
1184 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); }
1185 void setCallee(Expr *F) { SubExprs[FN] = F; }
1187 Decl *getCalleeDecl();
1188 const Decl *getCalleeDecl() const {
1189 return const_cast<CallExpr*>(this)->getCalleeDecl();
1192 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0.
1193 FunctionDecl *getDirectCallee();
1194 const FunctionDecl *getDirectCallee() const {
1195 return const_cast<CallExpr*>(this)->getDirectCallee();
1198 /// getNumArgs - Return the number of actual arguments to this call.
1200 unsigned getNumArgs() const { return NumArgs; }
1202 /// getArg - Return the specified argument.
1203 Expr *getArg(unsigned Arg) {
1204 assert(Arg < NumArgs && "Arg access out of range!");
1205 return cast<Expr>(SubExprs[Arg+ARGS_START]);
1207 const Expr *getArg(unsigned Arg) const {
1208 assert(Arg < NumArgs && "Arg access out of range!");
1209 return cast<Expr>(SubExprs[Arg+ARGS_START]);
1212 /// setArg - Set the specified argument.
1213 void setArg(unsigned Arg, Expr *ArgExpr) {
1214 assert(Arg < NumArgs && "Arg access out of range!");
1215 SubExprs[Arg+ARGS_START] = ArgExpr;
1218 /// setNumArgs - This changes the number of arguments present in this call.
1219 /// Any orphaned expressions are deleted by this, and any new operands are set
1221 void setNumArgs(ASTContext& C, unsigned NumArgs);
1223 typedef ExprIterator arg_iterator;
1224 typedef ConstExprIterator const_arg_iterator;
1226 arg_iterator arg_begin() { return SubExprs+ARGS_START; }
1227 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
1228 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; }
1229 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();}
1231 /// getNumCommas - Return the number of commas that must have been present in
1232 /// this function call.
1233 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
1235 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1237 unsigned isBuiltinCall(ASTContext &Context) const;
1239 /// getCallReturnType - Get the return type of the call expr. This is not
1240 /// always the type of the expr itself, if the return type is a reference
1242 QualType getCallReturnType() const;
1244 SourceLocation getRParenLoc() const { return RParenLoc; }
1245 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1247 virtual SourceRange getSourceRange() const {
1248 return SourceRange(getCallee()->getLocStart(), RParenLoc);
1251 static bool classof(const Stmt *T) {
1252 return T->getStmtClass() == CallExprClass ||
1253 T->getStmtClass() == CXXOperatorCallExprClass ||
1254 T->getStmtClass() == CXXMemberCallExprClass;
1256 static bool classof(const CallExpr *) { return true; }
1257 static bool classof(const CXXOperatorCallExpr *) { return true; }
1258 static bool classof(const CXXMemberCallExpr *) { return true; }
1261 virtual child_iterator child_begin();
1262 virtual child_iterator child_end();
1265 /// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F.
1267 class MemberExpr : public Expr {
1268 /// Base - the expression for the base pointer or structure references. In
1269 /// X.F, this is "X".
1272 /// MemberDecl - This is the decl being referenced by the field/member name.
1273 /// In X.F, this is the decl referenced by F.
1274 ValueDecl *MemberDecl;
1276 /// MemberLoc - This is the location of the member name.
1277 SourceLocation MemberLoc;
1279 /// IsArrow - True if this is "X->F", false if this is "X.F".
1282 /// \brief True if this member expression used a nested-name-specifier to
1283 /// refer to the member, e.g., "x->Base::f". When true, a NameQualifier
1284 /// structure is allocated immediately after the MemberExpr.
1285 bool HasQualifier : 1;
1287 /// \brief True if this member expression specified a template argument list
1288 /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList
1289 /// structure (and its TemplateArguments) are allocated immediately after
1290 /// the MemberExpr or, if the member expression also has a qualifier, after
1291 /// the NameQualifier structure.
1292 bool HasExplicitTemplateArgumentList : 1;
1294 /// \brief Retrieve the qualifier that preceded the member name, if any.
1295 NameQualifier *getMemberQualifier() {
1299 return reinterpret_cast<NameQualifier *> (this + 1);
1302 /// \brief Retrieve the qualifier that preceded the member name, if any.
1303 const NameQualifier *getMemberQualifier() const {
1304 return const_cast<MemberExpr *>(this)->getMemberQualifier();
1307 /// \brief Retrieve the explicit template argument list that followed the
1308 /// member template name, if any.
1309 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() {
1310 if (!HasExplicitTemplateArgumentList)
1314 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
1316 return reinterpret_cast<ExplicitTemplateArgumentList *>(
1317 getMemberQualifier() + 1);
1320 /// \brief Retrieve the explicit template argument list that followed the
1321 /// member template name, if any.
1322 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const {
1323 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgumentList();
1326 MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
1327 SourceRange qualrange, ValueDecl *memberdecl, SourceLocation l,
1328 const TemplateArgumentListInfo *targs, QualType ty);
1331 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl,
1332 SourceLocation l, QualType ty)
1333 : Expr(MemberExprClass, ty,
1334 base->isTypeDependent(), base->isValueDependent()),
1335 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
1336 HasQualifier(false), HasExplicitTemplateArgumentList(false) {}
1338 /// \brief Build an empty member reference expression.
1339 explicit MemberExpr(EmptyShell Empty)
1340 : Expr(MemberExprClass, Empty), HasQualifier(false),
1341 HasExplicitTemplateArgumentList(false) { }
1343 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow,
1344 NestedNameSpecifier *qual, SourceRange qualrange,
1345 ValueDecl *memberdecl,
1347 const TemplateArgumentListInfo *targs,
1350 void setBase(Expr *E) { Base = E; }
1351 Expr *getBase() const { return cast<Expr>(Base); }
1353 /// \brief Retrieve the member declaration to which this expression refers.
1355 /// The returned declaration will either be a FieldDecl or (in C++)
1356 /// a CXXMethodDecl.
1357 ValueDecl *getMemberDecl() const { return MemberDecl; }
1358 void setMemberDecl(ValueDecl *D) { MemberDecl = D; }
1360 /// \brief Determines whether this member expression actually had
1361 /// a C++ nested-name-specifier prior to the name of the member, e.g.,
1363 bool hasQualifier() const { return HasQualifier; }
1365 /// \brief If the member name was qualified, retrieves the source range of
1366 /// the nested-name-specifier that precedes the member name. Otherwise,
1367 /// returns an empty source range.
1368 SourceRange getQualifierRange() const {
1370 return SourceRange();
1372 return getMemberQualifier()->Range;
1375 /// \brief If the member name was qualified, retrieves the
1376 /// nested-name-specifier that precedes the member name. Otherwise, returns
1378 NestedNameSpecifier *getQualifier() const {
1382 return getMemberQualifier()->NNS;
1385 /// \brief Determines whether this member expression actually had a C++
1386 /// template argument list explicitly specified, e.g., x.f<int>.
1387 bool hasExplicitTemplateArgumentList() const {
1388 return HasExplicitTemplateArgumentList;
1391 /// \brief Copies the template arguments (if present) into the given
1393 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1394 if (hasExplicitTemplateArgumentList())
1395 getExplicitTemplateArgumentList()->copyInto(List);
1398 /// \brief Retrieve the location of the left angle bracket following the
1399 /// member name ('<'), if any.
1400 SourceLocation getLAngleLoc() const {
1401 if (!HasExplicitTemplateArgumentList)
1402 return SourceLocation();
1404 return getExplicitTemplateArgumentList()->LAngleLoc;
1407 /// \brief Retrieve the template arguments provided as part of this
1409 const TemplateArgumentLoc *getTemplateArgs() const {
1410 if (!HasExplicitTemplateArgumentList)
1413 return getExplicitTemplateArgumentList()->getTemplateArgs();
1416 /// \brief Retrieve the number of template arguments provided as part of this
1418 unsigned getNumTemplateArgs() const {
1419 if (!HasExplicitTemplateArgumentList)
1422 return getExplicitTemplateArgumentList()->NumTemplateArgs;
1425 /// \brief Retrieve the location of the right angle bracket following the
1426 /// template arguments ('>').
1427 SourceLocation getRAngleLoc() const {
1428 if (!HasExplicitTemplateArgumentList)
1429 return SourceLocation();
1431 return getExplicitTemplateArgumentList()->RAngleLoc;
1434 bool isArrow() const { return IsArrow; }
1435 void setArrow(bool A) { IsArrow = A; }
1437 /// getMemberLoc - Return the location of the "member", in X->F, it is the
1438 /// location of 'F'.
1439 SourceLocation getMemberLoc() const { return MemberLoc; }
1440 void setMemberLoc(SourceLocation L) { MemberLoc = L; }
1442 virtual SourceRange getSourceRange() const {
1443 // If we have an implicit base (like a C++ implicit this),
1444 // make sure not to return its location
1445 SourceLocation EndLoc = MemberLoc;
1446 if (HasExplicitTemplateArgumentList)
1447 EndLoc = getRAngleLoc();
1449 SourceLocation BaseLoc = getBase()->getLocStart();
1450 if (BaseLoc.isInvalid())
1451 return SourceRange(MemberLoc, EndLoc);
1452 return SourceRange(BaseLoc, EndLoc);
1455 virtual SourceLocation getExprLoc() const { return MemberLoc; }
1457 static bool classof(const Stmt *T) {
1458 return T->getStmtClass() == MemberExprClass;
1460 static bool classof(const MemberExpr *) { return true; }
1463 virtual child_iterator child_begin();
1464 virtual child_iterator child_end();
1467 /// CompoundLiteralExpr - [C99 6.5.2.5]
1469 class CompoundLiteralExpr : public Expr {
1470 /// LParenLoc - If non-null, this is the location of the left paren in a
1471 /// compound literal like "(int){4}". This can be null if this is a
1472 /// synthesized compound expression.
1473 SourceLocation LParenLoc;
1475 /// The type as written. This can be an incomplete array type, in
1476 /// which case the actual expression type will be different.
1477 TypeSourceInfo *TInfo;
1481 // FIXME: Can compound literals be value-dependent?
1482 CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo,
1483 QualType T, Expr *init, bool fileScope)
1484 : Expr(CompoundLiteralExprClass, T,
1485 tinfo->getType()->isDependentType(), false),
1486 LParenLoc(lparenloc), TInfo(tinfo), Init(init), FileScope(fileScope) {}
1488 /// \brief Construct an empty compound literal.
1489 explicit CompoundLiteralExpr(EmptyShell Empty)
1490 : Expr(CompoundLiteralExprClass, Empty) { }
1492 const Expr *getInitializer() const { return cast<Expr>(Init); }
1493 Expr *getInitializer() { return cast<Expr>(Init); }
1494 void setInitializer(Expr *E) { Init = E; }
1496 bool isFileScope() const { return FileScope; }
1497 void setFileScope(bool FS) { FileScope = FS; }
1499 SourceLocation getLParenLoc() const { return LParenLoc; }
1500 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1502 TypeSourceInfo *getTypeSourceInfo() const { return TInfo; }
1503 void setTypeSourceInfo(TypeSourceInfo* tinfo) { TInfo = tinfo; }
1505 virtual SourceRange getSourceRange() const {
1506 // FIXME: Init should never be null.
1508 return SourceRange();
1509 if (LParenLoc.isInvalid())
1510 return Init->getSourceRange();
1511 return SourceRange(LParenLoc, Init->getLocEnd());
1514 static bool classof(const Stmt *T) {
1515 return T->getStmtClass() == CompoundLiteralExprClass;
1517 static bool classof(const CompoundLiteralExpr *) { return true; }
1520 virtual child_iterator child_begin();
1521 virtual child_iterator child_end();
1524 /// CastExpr - Base class for type casts, including both implicit
1525 /// casts (ImplicitCastExpr) and explicit casts that have some
1526 /// representation in the source code (ExplicitCastExpr's derived
1528 class CastExpr : public Expr {
1530 /// CastKind - the kind of cast this represents.
1532 /// CK_Unknown - Unknown cast kind.
1533 /// FIXME: The goal is to get rid of this and make all casts have a
1534 /// kind so that the AST client doesn't have to try to figure out what's
1538 /// CK_BitCast - Used for reinterpret_cast.
1541 /// CK_NoOp - Used for const_cast.
1544 /// CK_BaseToDerived - Base to derived class casts.
1547 /// CK_DerivedToBase - Derived to base class casts.
1550 /// CK_Dynamic - Dynamic cast.
1553 /// CK_ToUnion - Cast to union (GCC extension).
1556 /// CK_ArrayToPointerDecay - Array to pointer decay.
1557 CK_ArrayToPointerDecay,
1559 // CK_FunctionToPointerDecay - Function to pointer decay.
1560 CK_FunctionToPointerDecay,
1562 /// CK_NullToMemberPointer - Null pointer to member pointer.
1563 CK_NullToMemberPointer,
1565 /// CK_BaseToDerivedMemberPointer - Member pointer in base class to
1566 /// member pointer in derived class.
1567 CK_BaseToDerivedMemberPointer,
1569 /// CK_DerivedToBaseMemberPointer - Member pointer in derived class to
1570 /// member pointer in base class.
1571 CK_DerivedToBaseMemberPointer,
1573 /// CK_UserDefinedConversion - Conversion using a user defined type
1574 /// conversion function.
1575 CK_UserDefinedConversion,
1577 /// CK_ConstructorConversion - Conversion by constructor
1578 CK_ConstructorConversion,
1580 /// CK_IntegralToPointer - Integral to pointer
1581 CK_IntegralToPointer,
1583 /// CK_PointerToIntegral - Pointer to integral
1584 CK_PointerToIntegral,
1586 /// CK_ToVoid - Cast to void.
1589 /// CK_VectorSplat - Casting from an integer/floating type to an extended
1590 /// vector type with the same element type as the src type. Splats the
1591 /// src expression into the destination expression.
1594 /// CK_IntegralCast - Casting between integral types of different size.
1597 /// CK_IntegralToFloating - Integral to floating point.
1598 CK_IntegralToFloating,
1600 /// CK_FloatingToIntegral - Floating point to integral.
1601 CK_FloatingToIntegral,
1603 /// CK_FloatingCast - Casting between floating types of different size.
1606 /// CK_MemberPointerToBoolean - Member pointer to boolean
1607 CK_MemberPointerToBoolean,
1609 /// CK_AnyPointerToObjCPointerCast - Casting any pointer to objective-c
1611 CK_AnyPointerToObjCPointerCast,
1612 /// CK_AnyPointerToBlockPointerCast - Casting any pointer to block
1614 CK_AnyPointerToBlockPointerCast
1622 CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op) :
1624 // Cast expressions are type-dependent if the type is
1625 // dependent (C++ [temp.dep.expr]p3).
1626 ty->isDependentType(),
1627 // Cast expressions are value-dependent if the type is
1628 // dependent or if the subexpression is value-dependent.
1629 ty->isDependentType() || (op && op->isValueDependent())),
1630 Kind(kind), Op(op) {}
1632 /// \brief Construct an empty cast.
1633 CastExpr(StmtClass SC, EmptyShell Empty)
1634 : Expr(SC, Empty) { }
1637 CastKind getCastKind() const { return Kind; }
1638 void setCastKind(CastKind K) { Kind = K; }
1639 const char *getCastKindName() const;
1641 Expr *getSubExpr() { return cast<Expr>(Op); }
1642 const Expr *getSubExpr() const { return cast<Expr>(Op); }
1643 void setSubExpr(Expr *E) { Op = E; }
1645 /// \brief Retrieve the cast subexpression as it was written in the source
1646 /// code, looking through any implicit casts or other intermediate nodes
1647 /// introduced by semantic analysis.
1648 Expr *getSubExprAsWritten();
1649 const Expr *getSubExprAsWritten() const {
1650 return const_cast<CastExpr *>(this)->getSubExprAsWritten();
1653 static bool classof(const Stmt *T) {
1654 StmtClass SC = T->getStmtClass();
1655 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass)
1658 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass)
1663 static bool classof(const CastExpr *) { return true; }
1666 virtual child_iterator child_begin();
1667 virtual child_iterator child_end();
1670 /// ImplicitCastExpr - Allows us to explicitly represent implicit type
1671 /// conversions, which have no direct representation in the original
1672 /// source code. For example: converting T[]->T*, void f()->void
1673 /// (*f)(), float->double, short->int, etc.
1675 /// In C, implicit casts always produce rvalues. However, in C++, an
1676 /// implicit cast whose result is being bound to a reference will be
1677 /// an lvalue. For example:
1681 /// class Derived : public Base { };
1682 /// void f(Derived d) {
1683 /// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base
1686 class ImplicitCastExpr : public CastExpr {
1687 /// LvalueCast - Whether this cast produces an lvalue.
1691 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, bool Lvalue) :
1692 CastExpr(ImplicitCastExprClass, ty, kind, op), LvalueCast(Lvalue) { }
1694 /// \brief Construct an empty implicit cast.
1695 explicit ImplicitCastExpr(EmptyShell Shell)
1696 : CastExpr(ImplicitCastExprClass, Shell) { }
1699 virtual SourceRange getSourceRange() const {
1700 return getSubExpr()->getSourceRange();
1703 /// isLvalueCast - Whether this cast produces an lvalue.
1704 bool isLvalueCast() const { return LvalueCast; }
1706 /// setLvalueCast - Set whether this cast produces an lvalue.
1707 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; }
1709 static bool classof(const Stmt *T) {
1710 return T->getStmtClass() == ImplicitCastExprClass;
1712 static bool classof(const ImplicitCastExpr *) { return true; }
1715 /// ExplicitCastExpr - An explicit cast written in the source
1718 /// This class is effectively an abstract class, because it provides
1719 /// the basic representation of an explicitly-written cast without
1720 /// specifying which kind of cast (C cast, functional cast, static
1721 /// cast, etc.) was written; specific derived classes represent the
1722 /// particular style of cast and its location information.
1724 /// Unlike implicit casts, explicit cast nodes have two different
1725 /// types: the type that was written into the source code, and the
1726 /// actual type of the expression as determined by semantic
1727 /// analysis. These types may differ slightly. For example, in C++ one
1728 /// can cast to a reference type, which indicates that the resulting
1729 /// expression will be an lvalue. The reference type, however, will
1730 /// not be used as the type of the expression.
1731 class ExplicitCastExpr : public CastExpr {
1732 /// TInfo - Source type info for the (written) type
1733 /// this expression is casting to.
1734 TypeSourceInfo *TInfo;
1737 ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind,
1738 Expr *op, TypeSourceInfo *writtenTy)
1739 : CastExpr(SC, exprTy, kind, op), TInfo(writtenTy) {}
1741 /// \brief Construct an empty explicit cast.
1742 ExplicitCastExpr(StmtClass SC, EmptyShell Shell)
1743 : CastExpr(SC, Shell) { }
1746 /// getTypeInfoAsWritten - Returns the type source info for the type
1747 /// that this expression is casting to.
1748 TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; }
1749 void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; }
1751 /// getTypeAsWritten - Returns the type that this expression is
1752 /// casting to, as written in the source code.
1753 QualType getTypeAsWritten() const { return TInfo->getType(); }
1755 static bool classof(const Stmt *T) {
1756 StmtClass SC = T->getStmtClass();
1757 if (SC >= CStyleCastExprClass && SC <= CStyleCastExprClass)
1759 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass)
1764 static bool classof(const ExplicitCastExpr *) { return true; }
1767 /// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style
1768 /// cast in C++ (C++ [expr.cast]), which uses the syntax
1769 /// (Type)expr. For example: @c (int)f.
1770 class CStyleCastExpr : public ExplicitCastExpr {
1771 SourceLocation LPLoc; // the location of the left paren
1772 SourceLocation RPLoc; // the location of the right paren
1774 CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op,
1775 TypeSourceInfo *writtenTy,
1776 SourceLocation l, SourceLocation r) :
1777 ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, writtenTy),
1778 LPLoc(l), RPLoc(r) {}
1780 /// \brief Construct an empty C-style explicit cast.
1781 explicit CStyleCastExpr(EmptyShell Shell)
1782 : ExplicitCastExpr(CStyleCastExprClass, Shell) { }
1784 SourceLocation getLParenLoc() const { return LPLoc; }
1785 void setLParenLoc(SourceLocation L) { LPLoc = L; }
1787 SourceLocation getRParenLoc() const { return RPLoc; }
1788 void setRParenLoc(SourceLocation L) { RPLoc = L; }
1790 virtual SourceRange getSourceRange() const {
1791 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd());
1793 static bool classof(const Stmt *T) {
1794 return T->getStmtClass() == CStyleCastExprClass;
1796 static bool classof(const CStyleCastExpr *) { return true; }
1799 /// \brief A builtin binary operation expression such as "x + y" or "x <= y".
1801 /// This expression node kind describes a builtin binary operation,
1802 /// such as "x + y" for integer values "x" and "y". The operands will
1803 /// already have been converted to appropriate types (e.g., by
1804 /// performing promotions or conversions).
1806 /// In C++, where operators may be overloaded, a different kind of
1807 /// expression node (CXXOperatorCallExpr) is used to express the
1808 /// invocation of an overloaded operator with operator syntax. Within
1809 /// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is
1810 /// used to store an expression "x + y" depends on the subexpressions
1811 /// for x and y. If neither x or y is type-dependent, and the "+"
1812 /// operator resolves to a built-in operation, BinaryOperator will be
1813 /// used to express the computation (x and y may still be
1814 /// value-dependent). If either x or y is type-dependent, or if the
1815 /// "+" resolves to an overloaded operator, CXXOperatorCallExpr will
1816 /// be used to express the computation.
1817 class BinaryOperator : public Expr {
1820 // Operators listed in order of precedence.
1821 // Note that additions to this should also update the StmtVisitor class.
1822 PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators.
1823 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators.
1824 Add, Sub, // [C99 6.5.6] Additive operators.
1825 Shl, Shr, // [C99 6.5.7] Bitwise shift operators.
1826 LT, GT, LE, GE, // [C99 6.5.8] Relational operators.
1827 EQ, NE, // [C99 6.5.9] Equality operators.
1828 And, // [C99 6.5.10] Bitwise AND operator.
1829 Xor, // [C99 6.5.11] Bitwise XOR operator.
1830 Or, // [C99 6.5.12] Bitwise OR operator.
1831 LAnd, // [C99 6.5.13] Logical AND operator.
1832 LOr, // [C99 6.5.14] Logical OR operator.
1833 Assign, MulAssign,// [C99 6.5.16] Assignment operators.
1834 DivAssign, RemAssign,
1835 AddAssign, SubAssign,
1836 ShlAssign, ShrAssign,
1837 AndAssign, XorAssign,
1839 Comma // [C99 6.5.17] Comma operator.
1842 enum { LHS, RHS, END_EXPR };
1843 Stmt* SubExprs[END_EXPR];
1845 SourceLocation OpLoc;
1848 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1849 SourceLocation opLoc)
1850 : Expr(BinaryOperatorClass, ResTy,
1851 lhs->isTypeDependent() || rhs->isTypeDependent(),
1852 lhs->isValueDependent() || rhs->isValueDependent()),
1853 Opc(opc), OpLoc(opLoc) {
1854 SubExprs[LHS] = lhs;
1855 SubExprs[RHS] = rhs;
1856 assert(!isCompoundAssignmentOp() &&
1857 "Use ArithAssignBinaryOperator for compound assignments");
1860 /// \brief Construct an empty binary operator.
1861 explicit BinaryOperator(EmptyShell Empty)
1862 : Expr(BinaryOperatorClass, Empty), Opc(Comma) { }
1864 SourceLocation getOperatorLoc() const { return OpLoc; }
1865 void setOperatorLoc(SourceLocation L) { OpLoc = L; }
1867 Opcode getOpcode() const { return Opc; }
1868 void setOpcode(Opcode O) { Opc = O; }
1870 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1871 void setLHS(Expr *E) { SubExprs[LHS] = E; }
1872 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1873 void setRHS(Expr *E) { SubExprs[RHS] = E; }
1875 virtual SourceRange getSourceRange() const {
1876 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
1879 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1880 /// corresponds to, e.g. "<<=".
1881 static const char *getOpcodeStr(Opcode Op);
1883 /// \brief Retrieve the binary opcode that corresponds to the given
1884 /// overloaded operator.
1885 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO);
1887 /// \brief Retrieve the overloaded operator kind that corresponds to
1888 /// the given binary opcode.
1889 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc);
1891 /// predicates to categorize the respective opcodes.
1892 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
1893 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
1894 static bool isShiftOp(Opcode Opc) { return Opc == Shl || Opc == Shr; }
1895 bool isShiftOp() const { return isShiftOp(Opc); }
1897 static bool isBitwiseOp(Opcode Opc) { return Opc >= And && Opc <= Or; }
1898 bool isBitwiseOp() const { return isBitwiseOp(Opc); }
1900 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; }
1901 bool isRelationalOp() const { return isRelationalOp(Opc); }
1903 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; }
1904 bool isEqualityOp() const { return isEqualityOp(Opc); }
1906 static bool isComparisonOp(Opcode Opc) { return Opc >= LT && Opc <= NE; }
1907 bool isComparisonOp() const { return isComparisonOp(Opc); }
1909 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; }
1910 bool isLogicalOp() const { return isLogicalOp(Opc); }
1912 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
1913 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
1914 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
1916 static bool classof(const Stmt *S) {
1917 return S->getStmtClass() == BinaryOperatorClass ||
1918 S->getStmtClass() == CompoundAssignOperatorClass;
1920 static bool classof(const BinaryOperator *) { return true; }
1923 virtual child_iterator child_begin();
1924 virtual child_iterator child_end();
1927 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1928 SourceLocation opLoc, bool dead)
1929 : Expr(CompoundAssignOperatorClass, ResTy,
1930 lhs->isTypeDependent() || rhs->isTypeDependent(),
1931 lhs->isValueDependent() || rhs->isValueDependent()),
1932 Opc(opc), OpLoc(opLoc) {
1933 SubExprs[LHS] = lhs;
1934 SubExprs[RHS] = rhs;
1937 BinaryOperator(StmtClass SC, EmptyShell Empty)
1938 : Expr(SC, Empty), Opc(MulAssign) { }
1941 /// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
1942 /// track of the type the operation is performed in. Due to the semantics of
1943 /// these operators, the operands are promoted, the aritmetic performed, an
1944 /// implicit conversion back to the result type done, then the assignment takes
1945 /// place. This captures the intermediate type which the computation is done
1947 class CompoundAssignOperator : public BinaryOperator {
1948 QualType ComputationLHSType;
1949 QualType ComputationResultType;
1951 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
1952 QualType ResType, QualType CompLHSType,
1953 QualType CompResultType,
1954 SourceLocation OpLoc)
1955 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
1956 ComputationLHSType(CompLHSType),
1957 ComputationResultType(CompResultType) {
1958 assert(isCompoundAssignmentOp() &&
1959 "Only should be used for compound assignments");
1962 /// \brief Build an empty compound assignment operator expression.
1963 explicit CompoundAssignOperator(EmptyShell Empty)
1964 : BinaryOperator(CompoundAssignOperatorClass, Empty) { }
1966 // The two computation types are the type the LHS is converted
1967 // to for the computation and the type of the result; the two are
1968 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr).
1969 QualType getComputationLHSType() const { return ComputationLHSType; }
1970 void setComputationLHSType(QualType T) { ComputationLHSType = T; }
1972 QualType getComputationResultType() const { return ComputationResultType; }
1973 void setComputationResultType(QualType T) { ComputationResultType = T; }
1975 static bool classof(const CompoundAssignOperator *) { return true; }
1976 static bool classof(const Stmt *S) {
1977 return S->getStmtClass() == CompoundAssignOperatorClass;
1981 /// ConditionalOperator - The ?: operator. Note that LHS may be null when the
1982 /// GNU "missing LHS" extension is in use.
1984 class ConditionalOperator : public Expr {
1985 enum { COND, LHS, RHS, END_EXPR };
1986 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1987 SourceLocation QuestionLoc, ColonLoc;
1989 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs,
1990 SourceLocation CLoc, Expr *rhs, QualType t)
1991 : Expr(ConditionalOperatorClass, t,
1992 // FIXME: the type of the conditional operator doesn't
1993 // depend on the type of the conditional, but the standard
1994 // seems to imply that it could. File a bug!
1995 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())),
1996 (cond->isValueDependent() ||
1997 (lhs && lhs->isValueDependent()) ||
1998 (rhs && rhs->isValueDependent()))),
2001 SubExprs[COND] = cond;
2002 SubExprs[LHS] = lhs;
2003 SubExprs[RHS] = rhs;
2006 /// \brief Build an empty conditional operator.
2007 explicit ConditionalOperator(EmptyShell Empty)
2008 : Expr(ConditionalOperatorClass, Empty) { }
2010 // getCond - Return the expression representing the condition for
2012 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
2013 void setCond(Expr *E) { SubExprs[COND] = E; }
2015 // getTrueExpr - Return the subexpression representing the value of the ?:
2016 // expression if the condition evaluates to true. In most cases this value
2017 // will be the same as getLHS() except a GCC extension allows the left
2018 // subexpression to be omitted, and instead of the condition be returned.
2019 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
2020 // is only evaluated once.
2021 Expr *getTrueExpr() const {
2022 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]);
2025 // getTrueExpr - Return the subexpression representing the value of the ?:
2026 // expression if the condition evaluates to false. This is the same as getRHS.
2027 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); }
2029 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); }
2030 void setLHS(Expr *E) { SubExprs[LHS] = E; }
2032 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
2033 void setRHS(Expr *E) { SubExprs[RHS] = E; }
2035 SourceLocation getQuestionLoc() const { return QuestionLoc; }
2036 void setQuestionLoc(SourceLocation L) { QuestionLoc = L; }
2038 SourceLocation getColonLoc() const { return ColonLoc; }
2039 void setColonLoc(SourceLocation L) { ColonLoc = L; }
2041 virtual SourceRange getSourceRange() const {
2042 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
2044 static bool classof(const Stmt *T) {
2045 return T->getStmtClass() == ConditionalOperatorClass;
2047 static bool classof(const ConditionalOperator *) { return true; }
2050 virtual child_iterator child_begin();
2051 virtual child_iterator child_end();
2054 /// AddrLabelExpr - The GNU address of label extension, representing &&label.
2055 class AddrLabelExpr : public Expr {
2056 SourceLocation AmpAmpLoc, LabelLoc;
2059 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
2061 : Expr(AddrLabelExprClass, t, false, false),
2062 AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
2064 /// \brief Build an empty address of a label expression.
2065 explicit AddrLabelExpr(EmptyShell Empty)
2066 : Expr(AddrLabelExprClass, Empty) { }
2068 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; }
2069 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; }
2070 SourceLocation getLabelLoc() const { return LabelLoc; }
2071 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
2073 virtual SourceRange getSourceRange() const {
2074 return SourceRange(AmpAmpLoc, LabelLoc);
2077 LabelStmt *getLabel() const { return Label; }
2078 void setLabel(LabelStmt *S) { Label = S; }
2080 static bool classof(const Stmt *T) {
2081 return T->getStmtClass() == AddrLabelExprClass;
2083 static bool classof(const AddrLabelExpr *) { return true; }
2086 virtual child_iterator child_begin();
2087 virtual child_iterator child_end();
2090 /// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
2091 /// The StmtExpr contains a single CompoundStmt node, which it evaluates and
2092 /// takes the value of the last subexpression.
2093 class StmtExpr : public Expr {
2095 SourceLocation LParenLoc, RParenLoc;
2097 // FIXME: Does type-dependence need to be computed differently?
2098 StmtExpr(CompoundStmt *substmt, QualType T,
2099 SourceLocation lp, SourceLocation rp) :
2100 Expr(StmtExprClass, T, T->isDependentType(), false),
2101 SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { }
2103 /// \brief Build an empty statement expression.
2104 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { }
2106 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); }
2107 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); }
2108 void setSubStmt(CompoundStmt *S) { SubStmt = S; }
2110 virtual SourceRange getSourceRange() const {
2111 return SourceRange(LParenLoc, RParenLoc);
2114 SourceLocation getLParenLoc() const { return LParenLoc; }
2115 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2116 SourceLocation getRParenLoc() const { return RParenLoc; }
2117 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2119 static bool classof(const Stmt *T) {
2120 return T->getStmtClass() == StmtExprClass;
2122 static bool classof(const StmtExpr *) { return true; }
2125 virtual child_iterator child_begin();
2126 virtual child_iterator child_end();
2129 /// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p.
2130 /// This AST node represents a function that returns 1 if two *types* (not
2131 /// expressions) are compatible. The result of this built-in function can be
2132 /// used in integer constant expressions.
2133 class TypesCompatibleExpr : public Expr {
2136 SourceLocation BuiltinLoc, RParenLoc;
2138 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
2139 QualType t1, QualType t2, SourceLocation RP) :
2140 Expr(TypesCompatibleExprClass, ReturnType, false, false),
2141 Type1(t1), Type2(t2), BuiltinLoc(BLoc), RParenLoc(RP) {}
2143 /// \brief Build an empty __builtin_type_compatible_p expression.
2144 explicit TypesCompatibleExpr(EmptyShell Empty)
2145 : Expr(TypesCompatibleExprClass, Empty) { }
2147 QualType getArgType1() const { return Type1; }
2148 void setArgType1(QualType T) { Type1 = T; }
2149 QualType getArgType2() const { return Type2; }
2150 void setArgType2(QualType T) { Type2 = T; }
2152 SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2153 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2155 SourceLocation getRParenLoc() const { return RParenLoc; }
2156 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2158 virtual SourceRange getSourceRange() const {
2159 return SourceRange(BuiltinLoc, RParenLoc);
2161 static bool classof(const Stmt *T) {
2162 return T->getStmtClass() == TypesCompatibleExprClass;
2164 static bool classof(const TypesCompatibleExpr *) { return true; }
2167 virtual child_iterator child_begin();
2168 virtual child_iterator child_end();
2171 /// ShuffleVectorExpr - clang-specific builtin-in function
2172 /// __builtin_shufflevector.
2173 /// This AST node represents a operator that does a constant
2174 /// shuffle, similar to LLVM's shufflevector instruction. It takes
2175 /// two vectors and a variable number of constant indices,
2176 /// and returns the appropriately shuffled vector.
2177 class ShuffleVectorExpr : public Expr {
2178 SourceLocation BuiltinLoc, RParenLoc;
2180 // SubExprs - the list of values passed to the __builtin_shufflevector
2181 // function. The first two are vectors, and the rest are constant
2182 // indices. The number of values in this list is always
2183 // 2+the number of indices in the vector type.
2188 virtual void DoDestroy(ASTContext &C);
2191 // FIXME: Can a shufflevector be value-dependent? Does type-dependence need
2192 // to be computed differently?
2193 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2194 QualType Type, SourceLocation BLoc,
2195 SourceLocation RP) :
2196 Expr(ShuffleVectorExprClass, Type, Type->isDependentType(), false),
2197 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr) {
2199 SubExprs = new (C) Stmt*[nexpr];
2200 for (unsigned i = 0; i < nexpr; i++)
2201 SubExprs[i] = args[i];
2204 /// \brief Build an empty vector-shuffle expression.
2205 explicit ShuffleVectorExpr(EmptyShell Empty)
2206 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { }
2208 SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2209 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2211 SourceLocation getRParenLoc() const { return RParenLoc; }
2212 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2214 virtual SourceRange getSourceRange() const {
2215 return SourceRange(BuiltinLoc, RParenLoc);
2217 static bool classof(const Stmt *T) {
2218 return T->getStmtClass() == ShuffleVectorExprClass;
2220 static bool classof(const ShuffleVectorExpr *) { return true; }
2222 ~ShuffleVectorExpr() {}
2224 /// getNumSubExprs - Return the size of the SubExprs array. This includes the
2225 /// constant expression, the actual arguments passed in, and the function
2227 unsigned getNumSubExprs() const { return NumExprs; }
2229 /// getExpr - Return the Expr at the specified index.
2230 Expr *getExpr(unsigned Index) {
2231 assert((Index < NumExprs) && "Arg access out of range!");
2232 return cast<Expr>(SubExprs[Index]);
2234 const Expr *getExpr(unsigned Index) const {
2235 assert((Index < NumExprs) && "Arg access out of range!");
2236 return cast<Expr>(SubExprs[Index]);
2239 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs);
2241 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) {
2242 assert((N < NumExprs - 2) && "Shuffle idx out of range!");
2243 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue();
2247 virtual child_iterator child_begin();
2248 virtual child_iterator child_end();
2251 /// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
2252 /// This AST node is similar to the conditional operator (?:) in C, with
2253 /// the following exceptions:
2254 /// - the test expression must be a integer constant expression.
2255 /// - the expression returned acts like the chosen subexpression in every
2256 /// visible way: the type is the same as that of the chosen subexpression,
2257 /// and all predicates (whether it's an l-value, whether it's an integer
2258 /// constant expression, etc.) return the same result as for the chosen
2260 class ChooseExpr : public Expr {
2261 enum { COND, LHS, RHS, END_EXPR };
2262 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
2263 SourceLocation BuiltinLoc, RParenLoc;
2265 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
2266 SourceLocation RP, bool TypeDependent, bool ValueDependent)
2267 : Expr(ChooseExprClass, t, TypeDependent, ValueDependent),
2268 BuiltinLoc(BLoc), RParenLoc(RP) {
2269 SubExprs[COND] = cond;
2270 SubExprs[LHS] = lhs;
2271 SubExprs[RHS] = rhs;
2274 /// \brief Build an empty __builtin_choose_expr.
2275 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { }
2277 /// isConditionTrue - Return whether the condition is true (i.e. not
2279 bool isConditionTrue(ASTContext &C) const;
2281 /// getChosenSubExpr - Return the subexpression chosen according to the
2283 Expr *getChosenSubExpr(ASTContext &C) const {
2284 return isConditionTrue(C) ? getLHS() : getRHS();
2287 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
2288 void setCond(Expr *E) { SubExprs[COND] = E; }
2289 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
2290 void setLHS(Expr *E) { SubExprs[LHS] = E; }
2291 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
2292 void setRHS(Expr *E) { SubExprs[RHS] = E; }
2294 SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2295 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2297 SourceLocation getRParenLoc() const { return RParenLoc; }
2298 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2300 virtual SourceRange getSourceRange() const {
2301 return SourceRange(BuiltinLoc, RParenLoc);
2303 static bool classof(const Stmt *T) {
2304 return T->getStmtClass() == ChooseExprClass;
2306 static bool classof(const ChooseExpr *) { return true; }
2309 virtual child_iterator child_begin();
2310 virtual child_iterator child_end();
2313 /// GNUNullExpr - Implements the GNU __null extension, which is a name
2314 /// for a null pointer constant that has integral type (e.g., int or
2315 /// long) and is the same size and alignment as a pointer. The __null
2316 /// extension is typically only used by system headers, which define
2317 /// NULL as __null in C++ rather than using 0 (which is an integer
2318 /// that may not match the size of a pointer).
2319 class GNUNullExpr : public Expr {
2320 /// TokenLoc - The location of the __null keyword.
2321 SourceLocation TokenLoc;
2324 GNUNullExpr(QualType Ty, SourceLocation Loc)
2325 : Expr(GNUNullExprClass, Ty, false, false), TokenLoc(Loc) { }
2327 /// \brief Build an empty GNU __null expression.
2328 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { }
2330 /// getTokenLocation - The location of the __null token.
2331 SourceLocation getTokenLocation() const { return TokenLoc; }
2332 void setTokenLocation(SourceLocation L) { TokenLoc = L; }
2334 virtual SourceRange getSourceRange() const {
2335 return SourceRange(TokenLoc);
2337 static bool classof(const Stmt *T) {
2338 return T->getStmtClass() == GNUNullExprClass;
2340 static bool classof(const GNUNullExpr *) { return true; }
2343 virtual child_iterator child_begin();
2344 virtual child_iterator child_end();
2347 /// VAArgExpr, used for the builtin function __builtin_va_start.
2348 class VAArgExpr : public Expr {
2350 SourceLocation BuiltinLoc, RParenLoc;
2352 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
2353 : Expr(VAArgExprClass, t, t->isDependentType(), false),
2356 RParenLoc(RPLoc) { }
2358 /// \brief Create an empty __builtin_va_start expression.
2359 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { }
2361 const Expr *getSubExpr() const { return cast<Expr>(Val); }
2362 Expr *getSubExpr() { return cast<Expr>(Val); }
2363 void setSubExpr(Expr *E) { Val = E; }
2365 SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2366 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2368 SourceLocation getRParenLoc() const { return RParenLoc; }
2369 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2371 virtual SourceRange getSourceRange() const {
2372 return SourceRange(BuiltinLoc, RParenLoc);
2374 static bool classof(const Stmt *T) {
2375 return T->getStmtClass() == VAArgExprClass;
2377 static bool classof(const VAArgExpr *) { return true; }
2380 virtual child_iterator child_begin();
2381 virtual child_iterator child_end();
2384 /// @brief Describes an C or C++ initializer list.
2386 /// InitListExpr describes an initializer list, which can be used to
2387 /// initialize objects of different types, including
2388 /// struct/class/union types, arrays, and vectors. For example:
2391 /// struct foo x = { 1, { 2, 3 } };
2394 /// Prior to semantic analysis, an initializer list will represent the
2395 /// initializer list as written by the user, but will have the
2396 /// placeholder type "void". This initializer list is called the
2397 /// syntactic form of the initializer, and may contain C99 designated
2398 /// initializers (represented as DesignatedInitExprs), initializations
2399 /// of subobject members without explicit braces, and so on. Clients
2400 /// interested in the original syntax of the initializer list should
2401 /// use the syntactic form of the initializer list.
2403 /// After semantic analysis, the initializer list will represent the
2404 /// semantic form of the initializer, where the initializations of all
2405 /// subobjects are made explicit with nested InitListExpr nodes and
2406 /// C99 designators have been eliminated by placing the designated
2407 /// initializations into the subobject they initialize. Additionally,
2408 /// any "holes" in the initialization, where no initializer has been
2409 /// specified for a particular subobject, will be replaced with
2410 /// implicitly-generated ImplicitValueInitExpr expressions that
2411 /// value-initialize the subobjects. Note, however, that the
2412 /// initializer lists may still have fewer initializers than there are
2413 /// elements to initialize within the object.
2415 /// Given the semantic form of the initializer list, one can retrieve
2416 /// the original syntactic form of that initializer list (if it
2417 /// exists) using getSyntacticForm(). Since many initializer lists
2418 /// have the same syntactic and semantic forms, getSyntacticForm() may
2419 /// return NULL, indicating that the current initializer list also
2420 /// serves as its syntactic form.
2421 class InitListExpr : public Expr {
2422 // FIXME: Eliminate this vector in favor of ASTContext allocation
2423 std::vector<Stmt *> InitExprs;
2424 SourceLocation LBraceLoc, RBraceLoc;
2426 /// Contains the initializer list that describes the syntactic form
2427 /// written in the source code.
2428 InitListExpr *SyntacticForm;
2430 /// If this initializer list initializes a union, specifies which
2431 /// field within the union will be initialized.
2432 FieldDecl *UnionFieldInit;
2434 /// Whether this initializer list originally had a GNU array-range
2435 /// designator in it. This is a temporary marker used by CodeGen.
2436 bool HadArrayRangeDesignator;
2439 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
2440 SourceLocation rbraceloc);
2442 /// \brief Build an empty initializer list.
2443 explicit InitListExpr(EmptyShell Empty) : Expr(InitListExprClass, Empty) { }
2445 unsigned getNumInits() const { return InitExprs.size(); }
2447 const Expr* getInit(unsigned Init) const {
2448 assert(Init < getNumInits() && "Initializer access out of range!");
2449 return cast_or_null<Expr>(InitExprs[Init]);
2452 Expr* getInit(unsigned Init) {
2453 assert(Init < getNumInits() && "Initializer access out of range!");
2454 return cast_or_null<Expr>(InitExprs[Init]);
2457 void setInit(unsigned Init, Expr *expr) {
2458 assert(Init < getNumInits() && "Initializer access out of range!");
2459 InitExprs[Init] = expr;
2462 /// \brief Reserve space for some number of initializers.
2463 void reserveInits(unsigned NumInits);
2465 /// @brief Specify the number of initializers
2467 /// If there are more than @p NumInits initializers, the remaining
2468 /// initializers will be destroyed. If there are fewer than @p
2469 /// NumInits initializers, NULL expressions will be added for the
2470 /// unknown initializers.
2471 void resizeInits(ASTContext &Context, unsigned NumInits);
2473 /// @brief Updates the initializer at index @p Init with the new
2474 /// expression @p expr, and returns the old expression at that
2477 /// When @p Init is out of range for this initializer list, the
2478 /// initializer list will be extended with NULL expressions to
2479 /// accomodate the new entry.
2480 Expr *updateInit(unsigned Init, Expr *expr);
2482 /// \brief If this initializes a union, specifies which field in the
2483 /// union to initialize.
2485 /// Typically, this field is the first named field within the
2486 /// union. However, a designated initializer can specify the
2487 /// initialization of a different field within the union.
2488 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; }
2489 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; }
2491 // Explicit InitListExpr's originate from source code (and have valid source
2492 // locations). Implicit InitListExpr's are created by the semantic analyzer.
2494 return LBraceLoc.isValid() && RBraceLoc.isValid();
2497 SourceLocation getLBraceLoc() const { return LBraceLoc; }
2498 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; }
2499 SourceLocation getRBraceLoc() const { return RBraceLoc; }
2500 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; }
2502 /// @brief Retrieve the initializer list that describes the
2503 /// syntactic form of the initializer.
2506 InitListExpr *getSyntacticForm() const { return SyntacticForm; }
2507 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; }
2509 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; }
2510 void sawArrayRangeDesignator(bool ARD = true) {
2511 HadArrayRangeDesignator = ARD;
2514 virtual SourceRange getSourceRange() const {
2515 return SourceRange(LBraceLoc, RBraceLoc);
2517 static bool classof(const Stmt *T) {
2518 return T->getStmtClass() == InitListExprClass;
2520 static bool classof(const InitListExpr *) { return true; }
2523 virtual child_iterator child_begin();
2524 virtual child_iterator child_end();
2526 typedef std::vector<Stmt *>::iterator iterator;
2527 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator;
2529 iterator begin() { return InitExprs.begin(); }
2530 iterator end() { return InitExprs.end(); }
2531 reverse_iterator rbegin() { return InitExprs.rbegin(); }
2532 reverse_iterator rend() { return InitExprs.rend(); }
2535 /// @brief Represents a C99 designated initializer expression.
2537 /// A designated initializer expression (C99 6.7.8) contains one or
2538 /// more designators (which can be field designators, array
2539 /// designators, or GNU array-range designators) followed by an
2540 /// expression that initializes the field or element(s) that the
2541 /// designators refer to. For example, given:
2548 /// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 };
2551 /// The InitListExpr contains three DesignatedInitExprs, the first of
2552 /// which covers @c [2].y=1.0. This DesignatedInitExpr will have two
2553 /// designators, one array designator for @c [2] followed by one field
2554 /// designator for @c .y. The initalization expression will be 1.0.
2555 class DesignatedInitExpr : public Expr {
2557 /// \brief Forward declaration of the Designator class.
2561 /// The location of the '=' or ':' prior to the actual initializer
2563 SourceLocation EqualOrColonLoc;
2565 /// Whether this designated initializer used the GNU deprecated
2566 /// syntax rather than the C99 '=' syntax.
2569 /// The number of designators in this initializer expression.
2570 unsigned NumDesignators : 15;
2572 /// \brief The designators in this designated initialization
2574 Designator *Designators;
2576 /// The number of subexpressions of this initializer expression,
2577 /// which contains both the initializer and any additional
2578 /// expressions used by array and array-range designators.
2579 unsigned NumSubExprs : 16;
2582 DesignatedInitExpr(ASTContext &C, QualType Ty, unsigned NumDesignators,
2583 const Designator *Designators,
2584 SourceLocation EqualOrColonLoc, bool GNUSyntax,
2585 Expr **IndexExprs, unsigned NumIndexExprs,
2588 explicit DesignatedInitExpr(unsigned NumSubExprs)
2589 : Expr(DesignatedInitExprClass, EmptyShell()),
2590 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { }
2593 virtual void DoDestroy(ASTContext &C);
2595 void DestroyDesignators(ASTContext &C);
2598 /// A field designator, e.g., ".x".
2599 struct FieldDesignator {
2600 /// Refers to the field that is being initialized. The low bit
2601 /// of this field determines whether this is actually a pointer
2602 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When
2603 /// initially constructed, a field designator will store an
2604 /// IdentifierInfo*. After semantic analysis has resolved that
2605 /// name, the field designator will instead store a FieldDecl*.
2606 uintptr_t NameOrField;
2608 /// The location of the '.' in the designated initializer.
2611 /// The location of the field name in the designated initializer.
2615 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]".
2616 struct ArrayOrRangeDesignator {
2617 /// Location of the first index expression within the designated
2618 /// initializer expression's list of subexpressions.
2620 /// The location of the '[' starting the array range designator.
2621 unsigned LBracketLoc;
2622 /// The location of the ellipsis separating the start and end
2623 /// indices. Only valid for GNU array-range designators.
2624 unsigned EllipsisLoc;
2625 /// The location of the ']' terminating the array range designator.
2626 unsigned RBracketLoc;
2629 /// @brief Represents a single C99 designator.
2631 /// @todo This class is infuriatingly similar to clang::Designator,
2632 /// but minor differences (storing indices vs. storing pointers)
2633 /// keep us from reusing it. Try harder, later, to rectify these
2636 /// @brief The kind of designator this describes.
2640 ArrayRangeDesignator
2644 /// A field designator, e.g., ".x".
2645 struct FieldDesignator Field;
2646 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]".
2647 struct ArrayOrRangeDesignator ArrayOrRange;
2649 friend class DesignatedInitExpr;
2654 /// @brief Initializes a field designator.
2655 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc,
2656 SourceLocation FieldLoc)
2657 : Kind(FieldDesignator) {
2658 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01;
2659 Field.DotLoc = DotLoc.getRawEncoding();
2660 Field.FieldLoc = FieldLoc.getRawEncoding();
2663 /// @brief Initializes an array designator.
2664 Designator(unsigned Index, SourceLocation LBracketLoc,
2665 SourceLocation RBracketLoc)
2666 : Kind(ArrayDesignator) {
2667 ArrayOrRange.Index = Index;
2668 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding();
2669 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding();
2670 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding();
2673 /// @brief Initializes a GNU array-range designator.
2674 Designator(unsigned Index, SourceLocation LBracketLoc,
2675 SourceLocation EllipsisLoc, SourceLocation RBracketLoc)
2676 : Kind(ArrayRangeDesignator) {
2677 ArrayOrRange.Index = Index;
2678 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding();
2679 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding();
2680 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding();
2683 bool isFieldDesignator() const { return Kind == FieldDesignator; }
2684 bool isArrayDesignator() const { return Kind == ArrayDesignator; }
2685 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
2687 IdentifierInfo * getFieldName();
2689 FieldDecl *getField() {
2690 assert(Kind == FieldDesignator && "Only valid on a field designator");
2691 if (Field.NameOrField & 0x01)
2694 return reinterpret_cast<FieldDecl *>(Field.NameOrField);
2697 void setField(FieldDecl *FD) {
2698 assert(Kind == FieldDesignator && "Only valid on a field designator");
2699 Field.NameOrField = reinterpret_cast<uintptr_t>(FD);
2702 SourceLocation getDotLoc() const {
2703 assert(Kind == FieldDesignator && "Only valid on a field designator");
2704 return SourceLocation::getFromRawEncoding(Field.DotLoc);
2707 SourceLocation getFieldLoc() const {
2708 assert(Kind == FieldDesignator && "Only valid on a field designator");
2709 return SourceLocation::getFromRawEncoding(Field.FieldLoc);
2712 SourceLocation getLBracketLoc() const {
2713 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2714 "Only valid on an array or array-range designator");
2715 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc);
2718 SourceLocation getRBracketLoc() const {
2719 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2720 "Only valid on an array or array-range designator");
2721 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc);
2724 SourceLocation getEllipsisLoc() const {
2725 assert(Kind == ArrayRangeDesignator &&
2726 "Only valid on an array-range designator");
2727 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc);
2730 unsigned getFirstExprIndex() const {
2731 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2732 "Only valid on an array or array-range designator");
2733 return ArrayOrRange.Index;
2736 SourceLocation getStartLocation() const {
2737 if (Kind == FieldDesignator)
2738 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc();
2740 return getLBracketLoc();
2744 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators,
2745 unsigned NumDesignators,
2746 Expr **IndexExprs, unsigned NumIndexExprs,
2747 SourceLocation EqualOrColonLoc,
2748 bool GNUSyntax, Expr *Init);
2750 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs);
2752 /// @brief Returns the number of designators in this initializer.
2753 unsigned size() const { return NumDesignators; }
2755 // Iterator access to the designators.
2756 typedef Designator* designators_iterator;
2757 designators_iterator designators_begin() { return Designators; }
2758 designators_iterator designators_end() {
2759 return Designators + NumDesignators;
2762 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; }
2764 void setDesignators(ASTContext &C, const Designator *Desigs,
2765 unsigned NumDesigs);
2767 Expr *getArrayIndex(const Designator& D);
2768 Expr *getArrayRangeStart(const Designator& D);
2769 Expr *getArrayRangeEnd(const Designator& D);
2771 /// @brief Retrieve the location of the '=' that precedes the
2772 /// initializer value itself, if present.
2773 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; }
2774 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; }
2776 /// @brief Determines whether this designated initializer used the
2777 /// deprecated GNU syntax for designated initializers.
2778 bool usesGNUSyntax() const { return GNUSyntax; }
2779 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; }
2781 /// @brief Retrieve the initializer value.
2782 Expr *getInit() const {
2783 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin());
2786 void setInit(Expr *init) {
2787 *child_begin() = init;
2790 /// \brief Retrieve the total number of subexpressions in this
2791 /// designated initializer expression, including the actual
2792 /// initialized value and any expressions that occur within array
2793 /// and array-range designators.
2794 unsigned getNumSubExprs() const { return NumSubExprs; }
2796 Expr *getSubExpr(unsigned Idx) {
2797 assert(Idx < NumSubExprs && "Subscript out of range");
2798 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2799 Ptr += sizeof(DesignatedInitExpr);
2800 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx];
2803 void setSubExpr(unsigned Idx, Expr *E) {
2804 assert(Idx < NumSubExprs && "Subscript out of range");
2805 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2806 Ptr += sizeof(DesignatedInitExpr);
2807 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E;
2810 /// \brief Replaces the designator at index @p Idx with the series
2811 /// of designators in [First, Last).
2812 void ExpandDesignator(ASTContext &C, unsigned Idx, const Designator *First,
2813 const Designator *Last);
2815 virtual SourceRange getSourceRange() const;
2817 static bool classof(const Stmt *T) {
2818 return T->getStmtClass() == DesignatedInitExprClass;
2820 static bool classof(const DesignatedInitExpr *) { return true; }
2823 virtual child_iterator child_begin();
2824 virtual child_iterator child_end();
2827 /// \brief Represents an implicitly-generated value initialization of
2828 /// an object of a given type.
2830 /// Implicit value initializations occur within semantic initializer
2831 /// list expressions (InitListExpr) as placeholders for subobject
2832 /// initializations not explicitly specified by the user.
2834 /// \see InitListExpr
2835 class ImplicitValueInitExpr : public Expr {
2837 explicit ImplicitValueInitExpr(QualType ty)
2838 : Expr(ImplicitValueInitExprClass, ty, false, false) { }
2840 /// \brief Construct an empty implicit value initialization.
2841 explicit ImplicitValueInitExpr(EmptyShell Empty)
2842 : Expr(ImplicitValueInitExprClass, Empty) { }
2844 static bool classof(const Stmt *T) {
2845 return T->getStmtClass() == ImplicitValueInitExprClass;
2847 static bool classof(const ImplicitValueInitExpr *) { return true; }
2849 virtual SourceRange getSourceRange() const {
2850 return SourceRange();
2854 virtual child_iterator child_begin();
2855 virtual child_iterator child_end();
2859 class ParenListExpr : public Expr {
2862 SourceLocation LParenLoc, RParenLoc;
2865 virtual void DoDestroy(ASTContext& C);
2868 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs,
2869 unsigned numexprs, SourceLocation rparenloc);
2873 /// \brief Build an empty paren list.
2874 //explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { }
2876 unsigned getNumExprs() const { return NumExprs; }
2878 const Expr* getExpr(unsigned Init) const {
2879 assert(Init < getNumExprs() && "Initializer access out of range!");
2880 return cast_or_null<Expr>(Exprs[Init]);
2883 Expr* getExpr(unsigned Init) {
2884 assert(Init < getNumExprs() && "Initializer access out of range!");
2885 return cast_or_null<Expr>(Exprs[Init]);
2888 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); }
2890 SourceLocation getLParenLoc() const { return LParenLoc; }
2891 SourceLocation getRParenLoc() const { return RParenLoc; }
2893 virtual SourceRange getSourceRange() const {
2894 return SourceRange(LParenLoc, RParenLoc);
2896 static bool classof(const Stmt *T) {
2897 return T->getStmtClass() == ParenListExprClass;
2899 static bool classof(const ParenListExpr *) { return true; }
2902 virtual child_iterator child_begin();
2903 virtual child_iterator child_end();
2907 //===----------------------------------------------------------------------===//
2909 //===----------------------------------------------------------------------===//
2912 /// ExtVectorElementExpr - This represents access to specific elements of a
2913 /// vector, and may occur on the left hand side or right hand side. For example
2914 /// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector.
2916 /// Note that the base may have either vector or pointer to vector type, just
2917 /// like a struct field reference.
2919 class ExtVectorElementExpr : public Expr {
2921 IdentifierInfo *Accessor;
2922 SourceLocation AccessorLoc;
2924 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
2926 : Expr(ExtVectorElementExprClass, ty, base->isTypeDependent(),
2927 base->isValueDependent()),
2928 Base(base), Accessor(&accessor), AccessorLoc(loc) {}
2930 /// \brief Build an empty vector element expression.
2931 explicit ExtVectorElementExpr(EmptyShell Empty)
2932 : Expr(ExtVectorElementExprClass, Empty) { }
2934 const Expr *getBase() const { return cast<Expr>(Base); }
2935 Expr *getBase() { return cast<Expr>(Base); }
2936 void setBase(Expr *E) { Base = E; }
2938 IdentifierInfo &getAccessor() const { return *Accessor; }
2939 void setAccessor(IdentifierInfo *II) { Accessor = II; }
2941 SourceLocation getAccessorLoc() const { return AccessorLoc; }
2942 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; }
2944 /// getNumElements - Get the number of components being selected.
2945 unsigned getNumElements() const;
2947 /// containsDuplicateElements - Return true if any element access is
2949 bool containsDuplicateElements() const;
2951 /// getEncodedElementAccess - Encode the elements accessed into an llvm
2952 /// aggregate Constant of ConstantInt(s).
2953 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const;
2955 virtual SourceRange getSourceRange() const {
2956 return SourceRange(getBase()->getLocStart(), AccessorLoc);
2959 /// isArrow - Return true if the base expression is a pointer to vector,
2960 /// return false if the base expression is a vector.
2961 bool isArrow() const;
2963 static bool classof(const Stmt *T) {
2964 return T->getStmtClass() == ExtVectorElementExprClass;
2966 static bool classof(const ExtVectorElementExpr *) { return true; }
2969 virtual child_iterator child_begin();
2970 virtual child_iterator child_end();
2974 /// BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
2975 /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body }
2976 class BlockExpr : public Expr {
2978 BlockDecl *TheBlock;
2979 bool HasBlockDeclRefExprs;
2981 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs)
2982 : Expr(BlockExprClass, ty, ty->isDependentType(), false),
2983 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {}
2985 /// \brief Build an empty block expression.
2986 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { }
2988 const BlockDecl *getBlockDecl() const { return TheBlock; }
2989 BlockDecl *getBlockDecl() { return TheBlock; }
2990 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; }
2992 // Convenience functions for probing the underlying BlockDecl.
2993 SourceLocation getCaretLocation() const;
2994 const Stmt *getBody() const;
2997 virtual SourceRange getSourceRange() const {
2998 return SourceRange(getCaretLocation(), getBody()->getLocEnd());
3001 /// getFunctionType - Return the underlying function type for this block.
3002 const FunctionType *getFunctionType() const;
3004 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr
3005 /// inside of the block that reference values outside the block.
3006 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; }
3007 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; }
3009 static bool classof(const Stmt *T) {
3010 return T->getStmtClass() == BlockExprClass;
3012 static bool classof(const BlockExpr *) { return true; }
3015 virtual child_iterator child_begin();
3016 virtual child_iterator child_end();
3019 /// BlockDeclRefExpr - A reference to a declared variable, function,
3021 class BlockDeclRefExpr : public Expr {
3025 bool ConstQualAdded : 1;
3027 // FIXME: Fix type/value dependence!
3028 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef,
3029 bool constAdded = false)
3030 : Expr(BlockDeclRefExprClass, t, false, false), D(d), Loc(l), IsByRef(ByRef),
3031 ConstQualAdded(constAdded) {}
3033 // \brief Build an empty reference to a declared variable in a
3035 explicit BlockDeclRefExpr(EmptyShell Empty)
3036 : Expr(BlockDeclRefExprClass, Empty) { }
3038 ValueDecl *getDecl() { return D; }
3039 const ValueDecl *getDecl() const { return D; }
3040 void setDecl(ValueDecl *VD) { D = VD; }
3042 SourceLocation getLocation() const { return Loc; }
3043 void setLocation(SourceLocation L) { Loc = L; }
3045 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
3047 bool isByRef() const { return IsByRef; }
3048 void setByRef(bool BR) { IsByRef = BR; }
3050 bool isConstQualAdded() const { return ConstQualAdded; }
3051 void setConstQualAdded(bool C) { ConstQualAdded = C; }
3053 static bool classof(const Stmt *T) {
3054 return T->getStmtClass() == BlockDeclRefExprClass;
3056 static bool classof(const BlockDeclRefExpr *) { return true; }
3059 virtual child_iterator child_begin();
3060 virtual child_iterator child_end();
3063 } // end namespace clang