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;
39 /// Expr - This represents one expression. Note that Expr's are subclasses of
40 /// Stmt. This allows an expression to be transparently used any place a Stmt
43 class Expr : public Stmt {
47 /// TypeDependent - Whether this expression is type-dependent
48 /// (C++ [temp.dep.expr]).
49 bool TypeDependent : 1;
51 /// ValueDependent - Whether this expression is value-dependent
52 /// (C++ [temp.dep.constexpr]).
53 bool ValueDependent : 1;
55 // FIXME: Eventually, this constructor should go away and we should
56 // require every subclass to provide type/value-dependence
58 Expr(StmtClass SC, QualType T)
59 : Stmt(SC), TypeDependent(false), ValueDependent(false) {
63 Expr(StmtClass SC, QualType T, bool TD, bool VD)
64 : Stmt(SC), TypeDependent(TD), ValueDependent(VD) {
68 /// \brief Construct an empty expression.
69 explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { }
72 /// \brief Increases the reference count for this expression.
74 /// Invoke the Retain() operation when this expression
75 /// is being shared by another owner.
81 QualType getType() const { return TR; }
82 void setType(QualType t) {
83 // In C++, the type of an expression is always adjusted so that it
84 // will not have reference type an expression will never have
85 // reference type (C++ [expr]p6). Use
86 // QualType::getNonReferenceType() to retrieve the non-reference
87 // type. Additionally, inspect Expr::isLvalue to determine whether
88 // an expression that is adjusted in this manner should be
89 // considered an lvalue.
90 assert((TR.isNull() || !TR->isReferenceType()) &&
91 "Expressions can't have reference type");
96 /// isValueDependent - Determines whether this expression is
97 /// value-dependent (C++ [temp.dep.constexpr]). For example, the
98 /// array bound of "Chars" in the following example is
101 /// template<int Size, char (&Chars)[Size]> struct meta_string;
103 bool isValueDependent() const { return ValueDependent; }
105 /// \brief Set whether this expression is value-dependent or not.
106 void setValueDependent(bool VD) { ValueDependent = VD; }
108 /// isTypeDependent - Determines whether this expression is
109 /// type-dependent (C++ [temp.dep.expr]), which means that its type
110 /// could change from one template instantiation to the next. For
111 /// example, the expressions "x" and "x + y" are type-dependent in
112 /// the following code, but "y" is not type-dependent:
114 /// template<typename T>
115 /// void add(T x, int y) {
119 bool isTypeDependent() const { return TypeDependent; }
121 /// \brief Set whether this expression is type-dependent or not.
122 void setTypeDependent(bool TD) { TypeDependent = TD; }
124 /// SourceLocation tokens are not useful in isolation - they are low level
125 /// value objects created/interpreted by SourceManager. We assume AST
126 /// clients will have a pointer to the respective SourceManager.
127 virtual SourceRange getSourceRange() const = 0;
129 /// getExprLoc - Return the preferred location for the arrow when diagnosing
130 /// a problem with a generic expression.
131 virtual SourceLocation getExprLoc() const { return getLocStart(); }
133 /// isUnusedResultAWarning - Return true if this immediate expression should
134 /// be warned about if the result is unused. If so, fill in Loc and Ranges
135 /// with location to warn on and the source range[s] to report with the
137 bool isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
138 SourceRange &R2, ASTContext &Ctx) const;
140 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
141 /// incomplete type other than void. Nonarray expressions that can be lvalues:
142 /// - name, where name must be a variable
144 /// - (e), where e must be an lvalue
145 /// - e.name, where e must be an lvalue
147 /// - *e, the type of e cannot be a function type
148 /// - string-constant
149 /// - reference type [C++ [expr]]
150 /// - b ? x : y, where x and y are lvalues of suitable types [C++]
152 enum isLvalueResult {
155 LV_IncompleteVoidType,
156 LV_DuplicateVectorComponents,
157 LV_InvalidExpression,
160 isLvalueResult isLvalue(ASTContext &Ctx) const;
162 // Same as above, but excluding checks for non-object and void types in C
163 isLvalueResult isLvalueInternal(ASTContext &Ctx) const;
165 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
166 /// does not have an incomplete type, does not have a const-qualified type,
167 /// and if it is a structure or union, does not have any member (including,
168 /// recursively, any member or element of all contained aggregates or unions)
169 /// with a const-qualified type.
171 /// \param Loc [in] [out] - A source location which *may* be filled
172 /// in with the location of the expression making this a
173 /// non-modifiable lvalue, if specified.
174 enum isModifiableLvalueResult {
177 MLV_IncompleteVoidType,
178 MLV_DuplicateVectorComponents,
179 MLV_InvalidExpression,
180 MLV_LValueCast, // Specialized form of MLV_InvalidExpression.
184 MLV_NotBlockQualified,
185 MLV_ReadonlyProperty,
186 MLV_NoSetterProperty,
189 isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx,
190 SourceLocation *Loc = 0) const;
192 /// \brief If this expression refers to a bit-field, retrieve the
193 /// declaration of that bit-field.
194 FieldDecl *getBitField();
196 const FieldDecl *getBitField() const {
197 return const_cast<Expr*>(this)->getBitField();
200 /// isIntegerConstantExpr - Return true if this expression is a valid integer
201 /// constant expression, and, if so, return its value in Result. If not a
202 /// valid i-c-e, return false and fill in Loc (if specified) with the location
203 /// of the invalid expression.
204 bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
205 SourceLocation *Loc = 0,
206 bool isEvaluated = true) const;
207 bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
209 return isIntegerConstantExpr(X, Ctx, Loc);
211 /// isConstantInitializer - Returns true if this expression is a constant
212 /// initializer, which can be emitted at compile-time.
213 bool isConstantInitializer(ASTContext &Ctx) const;
215 /// EvalResult is a struct with detailed info about an evaluated expression.
217 /// Val - This is the value the expression can be folded to.
220 /// HasSideEffects - Whether the evaluated expression has side effects.
221 /// For example, (f() && 0) can be folded, but it still has side effects.
224 /// Diag - If the expression is unfoldable, then Diag contains a note
225 /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret
226 /// position for the error, and DiagExpr is the expression that caused
228 /// If the expression is foldable, but not an integer constant expression,
229 /// Diag contains a note diagnostic that describes why it isn't an integer
230 /// constant expression. If the expression *is* an integer constant
231 /// expression, then Diag will be zero.
233 const Expr *DiagExpr;
234 SourceLocation DiagLoc;
236 EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {}
239 /// Evaluate - Return true if this is a constant which we can fold using
240 /// any crazy technique (that has nothing to do with language standards) that
241 /// we want to. If this function returns true, it returns the folded constant
243 bool Evaluate(EvalResult &Result, ASTContext &Ctx) const;
245 /// EvaluateAsAny - The same as Evaluate, except that it also succeeds on
246 /// stack based objects.
247 bool EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const;
249 /// isEvaluatable - Call Evaluate to see if this expression can be constant
250 /// folded, but discard the result.
251 bool isEvaluatable(ASTContext &Ctx) const;
253 /// EvaluateAsInt - Call Evaluate and return the folded integer. This
254 /// must be called on an expression that constant folds to an integer.
255 llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const;
257 /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue
258 /// with link time known address.
259 bool EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const;
261 /// EvaluateAsAnyLValue - The same as EvaluateAsLValue, except that it
262 /// also succeeds on stack based, immutable address lvalues.
263 bool EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const;
265 /// \brief Enumeration used to describe how \c isNullPointerConstant()
266 /// should cope with value-dependent expressions.
267 enum NullPointerConstantValueDependence {
268 /// \brief Specifies that the expression should never be value-dependent.
269 NPC_NeverValueDependent = 0,
271 /// \brief Specifies that a value-dependent expression of integral or
272 /// dependent type should be considered a null pointer constant.
273 NPC_ValueDependentIsNull,
275 /// \brief Specifies that a value-dependent expression should be considered
276 /// to never be a null pointer constant.
277 NPC_ValueDependentIsNotNull
280 /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
281 /// integer constant expression with the value zero, or if this is one that is
283 bool isNullPointerConstant(ASTContext &Ctx,
284 NullPointerConstantValueDependence NPC) const;
286 /// isOBJCGCCandidate - Return true if this expression may be used in a read/
288 bool isOBJCGCCandidate(ASTContext &Ctx) const;
290 /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return
291 /// its subexpression. If that subexpression is also a ParenExpr,
292 /// then this method recursively returns its subexpression, and so forth.
293 /// Otherwise, the method returns the current Expr.
294 Expr* IgnoreParens();
296 /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
297 /// or CastExprs, returning their operand.
298 Expr *IgnoreParenCasts();
300 /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
301 /// value (including ptr->int casts of the same size). Strip off any
302 /// ParenExpr or CastExprs, returning their operand.
303 Expr *IgnoreParenNoopCasts(ASTContext &Ctx);
305 const Expr* IgnoreParens() const {
306 return const_cast<Expr*>(this)->IgnoreParens();
308 const Expr *IgnoreParenCasts() const {
309 return const_cast<Expr*>(this)->IgnoreParenCasts();
311 const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const {
312 return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx);
315 static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs);
316 static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs);
318 static bool classof(const Stmt *T) {
319 return T->getStmtClass() >= firstExprConstant &&
320 T->getStmtClass() <= lastExprConstant;
322 static bool classof(const Expr *) { return true; }
326 //===----------------------------------------------------------------------===//
327 // Primary Expressions.
328 //===----------------------------------------------------------------------===//
330 /// \brief Represents the qualifier that may precede a C++ name, e.g., the
331 /// "std::" in "std::sort".
332 struct NameQualifier {
333 /// \brief The nested name specifier.
334 NestedNameSpecifier *NNS;
336 /// \brief The source range covered by the nested name specifier.
340 /// \brief Represents an explicit template argument list in C++, e.g.,
341 /// the "<int>" in "sort<int>".
342 struct ExplicitTemplateArgumentList {
343 /// \brief The source location of the left angle bracket ('<');
344 SourceLocation LAngleLoc;
346 /// \brief The source location of the right angle bracket ('>');
347 SourceLocation RAngleLoc;
349 /// \brief The number of template arguments in TemplateArgs.
350 /// The actual template arguments (if any) are stored after the
351 /// ExplicitTemplateArgumentList structure.
352 unsigned NumTemplateArgs;
354 /// \brief Retrieve the template arguments
355 TemplateArgumentLoc *getTemplateArgs() {
356 return reinterpret_cast<TemplateArgumentLoc *> (this + 1);
359 /// \brief Retrieve the template arguments
360 const TemplateArgumentLoc *getTemplateArgs() const {
361 return reinterpret_cast<const TemplateArgumentLoc *> (this + 1);
365 /// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
367 class DeclRefExpr : public Expr {
369 // Flag on DecoratedD that specifies when this declaration reference
370 // expression has a C++ nested-name-specifier.
371 HasQualifierFlag = 0x01,
372 // Flag on DecoratedD that specifies when this declaration reference
373 // expression has an explicit C++ template argument list.
374 HasExplicitTemplateArgumentListFlag = 0x02
377 // DecoratedD - The declaration that we are referencing, plus two bits to
378 // indicate whether (1) the declaration's name was explicitly qualified and
379 // (2) the declaration's name was followed by an explicit template
381 llvm::PointerIntPair<NamedDecl *, 2> DecoratedD;
383 // Loc - The location of the declaration name itself.
386 /// \brief Retrieve the qualifier that preceded the declaration name, if any.
387 NameQualifier *getNameQualifier() {
388 if ((DecoratedD.getInt() & HasQualifierFlag) == 0)
391 return reinterpret_cast<NameQualifier *> (this + 1);
394 /// \brief Retrieve the qualifier that preceded the member name, if any.
395 const NameQualifier *getNameQualifier() const {
396 return const_cast<DeclRefExpr *>(this)->getNameQualifier();
399 /// \brief Retrieve the explicit template argument list that followed the
400 /// member template name, if any.
401 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() {
402 if ((DecoratedD.getInt() & HasExplicitTemplateArgumentListFlag) == 0)
405 if ((DecoratedD.getInt() & HasQualifierFlag) == 0)
406 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
408 return reinterpret_cast<ExplicitTemplateArgumentList *>(
409 getNameQualifier() + 1);
412 /// \brief Retrieve the explicit template argument list that followed the
413 /// member template name, if any.
414 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const {
415 return const_cast<DeclRefExpr *>(this)->getExplicitTemplateArgumentList();
418 DeclRefExpr(NestedNameSpecifier *Qualifier, SourceRange QualifierRange,
419 NamedDecl *D, SourceLocation NameLoc,
420 bool HasExplicitTemplateArgumentList,
421 SourceLocation LAngleLoc,
422 const TemplateArgumentLoc *ExplicitTemplateArgs,
423 unsigned NumExplicitTemplateArgs,
424 SourceLocation RAngleLoc,
425 QualType T, bool TD, bool VD);
428 // FIXME: Eventually, this constructor will go away and all subclasses
429 // will have to provide the type- and value-dependent flags.
430 DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l) :
431 Expr(SC, t), DecoratedD(d, 0), Loc(l) {}
433 DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l, bool TD,
435 Expr(SC, t, TD, VD), DecoratedD(d, 0), Loc(l) {}
438 // FIXME: Eventually, this constructor will go away and all clients
439 // will have to provide the type- and value-dependent flags.
440 DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l) :
441 Expr(DeclRefExprClass, t), DecoratedD(d, 0), Loc(l) {}
443 DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, bool VD) :
444 Expr(DeclRefExprClass, t, TD, VD), DecoratedD(d, 0), Loc(l) {}
446 /// \brief Construct an empty declaration reference expression.
447 explicit DeclRefExpr(EmptyShell Empty)
448 : Expr(DeclRefExprClass, Empty) { }
450 static DeclRefExpr *Create(ASTContext &Context,
451 NestedNameSpecifier *Qualifier,
452 SourceRange QualifierRange,
454 SourceLocation NameLoc,
455 QualType T, bool TD, bool VD);
457 static DeclRefExpr *Create(ASTContext &Context,
458 NestedNameSpecifier *Qualifier,
459 SourceRange QualifierRange,
461 SourceLocation NameLoc,
462 bool HasExplicitTemplateArgumentList,
463 SourceLocation LAngleLoc,
464 const TemplateArgumentLoc *ExplicitTemplateArgs,
465 unsigned NumExplicitTemplateArgs,
466 SourceLocation RAngleLoc,
467 QualType T, bool TD, bool VD);
469 NamedDecl *getDecl() { return DecoratedD.getPointer(); }
470 const NamedDecl *getDecl() const { return DecoratedD.getPointer(); }
471 void setDecl(NamedDecl *NewD) { DecoratedD.setPointer(NewD); }
473 SourceLocation getLocation() const { return Loc; }
474 void setLocation(SourceLocation L) { Loc = L; }
475 virtual SourceRange getSourceRange() const;
477 /// \brief Determine whether this declaration reference was preceded by a
478 /// C++ nested-name-specifier, e.g., \c N::foo.
479 bool hasQualifier() const { return DecoratedD.getInt() & HasQualifierFlag; }
481 /// \brief If the name was qualified, retrieves the source range of
482 /// the nested-name-specifier that precedes the name. Otherwise,
483 /// returns an empty source range.
484 SourceRange getQualifierRange() const {
486 return SourceRange();
488 return getNameQualifier()->Range;
491 /// \brief If the name was qualified, retrieves the nested-name-specifier
492 /// that precedes the name. Otherwise, returns NULL.
493 NestedNameSpecifier *getQualifier() const {
497 return getNameQualifier()->NNS;
500 /// \brief Determines whether this member expression actually had a C++
501 /// template argument list explicitly specified, e.g., x.f<int>.
502 bool hasExplicitTemplateArgumentList() const {
503 return DecoratedD.getInt() & HasExplicitTemplateArgumentListFlag;
506 /// \brief Retrieve the location of the left angle bracket following the
507 /// member name ('<'), if any.
508 SourceLocation getLAngleLoc() const {
509 if (!hasExplicitTemplateArgumentList())
510 return SourceLocation();
512 return getExplicitTemplateArgumentList()->LAngleLoc;
515 /// \brief Retrieve the template arguments provided as part of this
517 const TemplateArgumentLoc *getTemplateArgs() const {
518 if (!hasExplicitTemplateArgumentList())
521 return getExplicitTemplateArgumentList()->getTemplateArgs();
524 /// \brief Retrieve the number of template arguments provided as part of this
526 unsigned getNumTemplateArgs() const {
527 if (!hasExplicitTemplateArgumentList())
530 return getExplicitTemplateArgumentList()->NumTemplateArgs;
533 /// \brief Retrieve the location of the right angle bracket following the
534 /// template arguments ('>').
535 SourceLocation getRAngleLoc() const {
536 if (!hasExplicitTemplateArgumentList())
537 return SourceLocation();
539 return getExplicitTemplateArgumentList()->RAngleLoc;
542 static bool classof(const Stmt *T) {
543 return T->getStmtClass() == DeclRefExprClass ||
544 T->getStmtClass() == CXXConditionDeclExprClass;
546 static bool classof(const DeclRefExpr *) { return true; }
549 virtual child_iterator child_begin();
550 virtual child_iterator child_end();
553 /// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__.
554 class PredefinedExpr : public Expr {
566 PredefinedExpr(SourceLocation l, QualType type, IdentType IT)
567 : Expr(PredefinedExprClass, type, type->isDependentType(),
568 type->isDependentType()), Loc(l), Type(IT) {}
570 /// \brief Construct an empty predefined expression.
571 explicit PredefinedExpr(EmptyShell Empty)
572 : Expr(PredefinedExprClass, Empty) { }
574 IdentType getIdentType() const { return Type; }
575 void setIdentType(IdentType IT) { Type = IT; }
577 SourceLocation getLocation() const { return Loc; }
578 void setLocation(SourceLocation L) { Loc = L; }
580 static std::string ComputeName(ASTContext &Context, IdentType IT,
581 const Decl *CurrentDecl);
583 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
585 static bool classof(const Stmt *T) {
586 return T->getStmtClass() == PredefinedExprClass;
588 static bool classof(const PredefinedExpr *) { return true; }
591 virtual child_iterator child_begin();
592 virtual child_iterator child_end();
595 class IntegerLiteral : public Expr {
599 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
600 // or UnsignedLongLongTy
601 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
602 : Expr(IntegerLiteralClass, type), Value(V), Loc(l) {
603 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
606 /// \brief Construct an empty integer literal.
607 explicit IntegerLiteral(EmptyShell Empty)
608 : Expr(IntegerLiteralClass, Empty) { }
610 const llvm::APInt &getValue() const { return Value; }
611 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
613 /// \brief Retrieve the location of the literal.
614 SourceLocation getLocation() const { return Loc; }
616 void setValue(const llvm::APInt &Val) { Value = Val; }
617 void setLocation(SourceLocation Location) { Loc = Location; }
619 static bool classof(const Stmt *T) {
620 return T->getStmtClass() == IntegerLiteralClass;
622 static bool classof(const IntegerLiteral *) { return true; }
625 virtual child_iterator child_begin();
626 virtual child_iterator child_end();
629 class CharacterLiteral : public Expr {
634 // type should be IntTy
635 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l)
636 : Expr(CharacterLiteralClass, type), Value(value), Loc(l), IsWide(iswide) {
639 /// \brief Construct an empty character literal.
640 CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { }
642 SourceLocation getLocation() const { return Loc; }
643 bool isWide() const { return IsWide; }
645 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
647 unsigned getValue() const { return Value; }
649 void setLocation(SourceLocation Location) { Loc = Location; }
650 void setWide(bool W) { IsWide = W; }
651 void setValue(unsigned Val) { Value = Val; }
653 static bool classof(const Stmt *T) {
654 return T->getStmtClass() == CharacterLiteralClass;
656 static bool classof(const CharacterLiteral *) { return true; }
659 virtual child_iterator child_begin();
660 virtual child_iterator child_end();
663 class FloatingLiteral : public Expr {
668 FloatingLiteral(const llvm::APFloat &V, bool isexact,
669 QualType Type, SourceLocation L)
670 : Expr(FloatingLiteralClass, Type), Value(V), IsExact(isexact), Loc(L) {}
672 /// \brief Construct an empty floating-point literal.
673 explicit FloatingLiteral(EmptyShell Empty)
674 : Expr(FloatingLiteralClass, Empty), Value(0.0) { }
676 const llvm::APFloat &getValue() const { return Value; }
677 void setValue(const llvm::APFloat &Val) { Value = Val; }
679 bool isExact() const { return IsExact; }
680 void setExact(bool E) { IsExact = E; }
682 /// getValueAsApproximateDouble - This returns the value as an inaccurate
683 /// double. Note that this may cause loss of precision, but is useful for
684 /// debugging dumps, etc.
685 double getValueAsApproximateDouble() const;
687 SourceLocation getLocation() const { return Loc; }
688 void setLocation(SourceLocation L) { Loc = L; }
690 // FIXME: The logic for computing the value of a predefined expr should go
691 // into a method here that takes the inner-most code decl (a block, function
692 // or objc method) that the expr lives in. This would allow sema and codegen
693 // to be consistent for things like sizeof(__func__) etc.
695 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
697 static bool classof(const Stmt *T) {
698 return T->getStmtClass() == FloatingLiteralClass;
700 static bool classof(const FloatingLiteral *) { return true; }
703 virtual child_iterator child_begin();
704 virtual child_iterator child_end();
707 /// ImaginaryLiteral - We support imaginary integer and floating point literals,
708 /// like "1.0i". We represent these as a wrapper around FloatingLiteral and
709 /// IntegerLiteral classes. Instances of this class always have a Complex type
710 /// whose element type matches the subexpression.
712 class ImaginaryLiteral : public Expr {
715 ImaginaryLiteral(Expr *val, QualType Ty)
716 : Expr(ImaginaryLiteralClass, Ty), Val(val) {}
718 /// \brief Build an empty imaginary literal.
719 explicit ImaginaryLiteral(EmptyShell Empty)
720 : Expr(ImaginaryLiteralClass, Empty) { }
722 const Expr *getSubExpr() const { return cast<Expr>(Val); }
723 Expr *getSubExpr() { return cast<Expr>(Val); }
724 void setSubExpr(Expr *E) { Val = E; }
726 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); }
727 static bool classof(const Stmt *T) {
728 return T->getStmtClass() == ImaginaryLiteralClass;
730 static bool classof(const ImaginaryLiteral *) { return true; }
733 virtual child_iterator child_begin();
734 virtual child_iterator child_end();
737 /// StringLiteral - This represents a string literal expression, e.g. "foo"
738 /// or L"bar" (wide strings). The actual string is returned by getStrData()
739 /// is NOT null-terminated, and the length of the string is determined by
740 /// calling getByteLength(). The C type for a string is always a
741 /// ConstantArrayType. In C++, the char type is const qualified, in C it is
744 /// Note that strings in C can be formed by concatenation of multiple string
745 /// literal pptokens in translation phase #6. This keeps track of the locations
746 /// of each of these pieces.
748 /// Strings in C can also be truncated and extended by assigning into arrays,
749 /// e.g. with constructs like:
750 /// char X[2] = "foobar";
751 /// In this case, getByteLength() will return 6, but the string literal will
752 /// have type "char[2]".
753 class StringLiteral : public Expr {
757 unsigned NumConcatenated;
758 SourceLocation TokLocs[1];
760 StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty) {}
763 virtual void DoDestroy(ASTContext &C);
766 /// This is the "fully general" constructor that allows representation of
767 /// strings formed from multiple concatenated tokens.
768 static StringLiteral *Create(ASTContext &C, const char *StrData,
769 unsigned ByteLength, bool Wide, QualType Ty,
770 const SourceLocation *Loc, unsigned NumStrs);
772 /// Simple constructor for string literals made from one token.
773 static StringLiteral *Create(ASTContext &C, const char *StrData,
775 bool Wide, QualType Ty, SourceLocation Loc) {
776 return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1);
779 /// \brief Construct an empty string literal.
780 static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs);
782 llvm::StringRef getString() const {
783 return llvm::StringRef(StrData, ByteLength);
785 // FIXME: These are deprecated, replace with StringRef.
786 const char *getStrData() const { return StrData; }
787 unsigned getByteLength() const { return ByteLength; }
789 /// \brief Sets the string data to the given string data.
790 void setString(ASTContext &C, llvm::StringRef Str);
792 bool isWide() const { return IsWide; }
793 void setWide(bool W) { IsWide = W; }
795 bool containsNonAsciiOrNull() const {
796 llvm::StringRef Str = getString();
797 for (unsigned i = 0, e = Str.size(); i != e; ++i)
798 if (!isascii(Str[i]) || !Str[i])
802 /// getNumConcatenated - Get the number of string literal tokens that were
803 /// concatenated in translation phase #6 to form this string literal.
804 unsigned getNumConcatenated() const { return NumConcatenated; }
806 SourceLocation getStrTokenLoc(unsigned TokNum) const {
807 assert(TokNum < NumConcatenated && "Invalid tok number");
808 return TokLocs[TokNum];
810 void setStrTokenLoc(unsigned TokNum, SourceLocation L) {
811 assert(TokNum < NumConcatenated && "Invalid tok number");
815 typedef const SourceLocation *tokloc_iterator;
816 tokloc_iterator tokloc_begin() const { return TokLocs; }
817 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; }
819 virtual SourceRange getSourceRange() const {
820 return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]);
822 static bool classof(const Stmt *T) {
823 return T->getStmtClass() == StringLiteralClass;
825 static bool classof(const StringLiteral *) { return true; }
828 virtual child_iterator child_begin();
829 virtual child_iterator child_end();
832 /// ParenExpr - This represents a parethesized expression, e.g. "(1)". This
833 /// AST node is only formed if full location information is requested.
834 class ParenExpr : public Expr {
838 ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
839 : Expr(ParenExprClass, val->getType(),
840 val->isTypeDependent(), val->isValueDependent()),
841 L(l), R(r), Val(val) {}
843 /// \brief Construct an empty parenthesized expression.
844 explicit ParenExpr(EmptyShell Empty)
845 : Expr(ParenExprClass, Empty) { }
847 const Expr *getSubExpr() const { return cast<Expr>(Val); }
848 Expr *getSubExpr() { return cast<Expr>(Val); }
849 void setSubExpr(Expr *E) { Val = E; }
851 virtual SourceRange getSourceRange() const { return SourceRange(L, R); }
853 /// \brief Get the location of the left parentheses '('.
854 SourceLocation getLParen() const { return L; }
855 void setLParen(SourceLocation Loc) { L = Loc; }
857 /// \brief Get the location of the right parentheses ')'.
858 SourceLocation getRParen() const { return R; }
859 void setRParen(SourceLocation Loc) { R = Loc; }
861 static bool classof(const Stmt *T) {
862 return T->getStmtClass() == ParenExprClass;
864 static bool classof(const ParenExpr *) { return true; }
867 virtual child_iterator child_begin();
868 virtual child_iterator child_end();
872 /// UnaryOperator - This represents the unary-expression's (except sizeof and
873 /// alignof), the postinc/postdec operators from postfix-expression, and various
876 /// Notes on various nodes:
878 /// Real/Imag - These return the real/imag part of a complex operand. If
879 /// applied to a non-complex value, the former returns its operand and the
880 /// later returns zero in the type of the operand.
882 /// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose
883 /// subexpression is a compound literal with the various MemberExpr and
884 /// ArraySubscriptExpr's applied to it.
886 class UnaryOperator : public Expr {
888 // Note that additions to this should also update the StmtVisitor class.
890 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
891 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators.
892 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators.
893 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators.
894 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators.
895 Real, Imag, // "__real expr"/"__imag expr" Extension.
896 Extension, // __extension__ marker.
897 OffsetOf // __builtin_offsetof
905 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
906 : Expr(UnaryOperatorClass, type,
907 input->isTypeDependent() && opc != OffsetOf,
908 input->isValueDependent()),
909 Val(input), Opc(opc), Loc(l) {}
911 /// \brief Build an empty unary operator.
912 explicit UnaryOperator(EmptyShell Empty)
913 : Expr(UnaryOperatorClass, Empty), Opc(AddrOf) { }
915 Opcode getOpcode() const { return Opc; }
916 void setOpcode(Opcode O) { Opc = O; }
918 Expr *getSubExpr() const { return cast<Expr>(Val); }
919 void setSubExpr(Expr *E) { Val = E; }
921 /// getOperatorLoc - Return the location of the operator.
922 SourceLocation getOperatorLoc() const { return Loc; }
923 void setOperatorLoc(SourceLocation L) { Loc = L; }
925 /// isPostfix - Return true if this is a postfix operation, like x++.
926 static bool isPostfix(Opcode Op) {
927 return Op == PostInc || Op == PostDec;
930 /// isPostfix - Return true if this is a prefix operation, like --x.
931 static bool isPrefix(Opcode Op) {
932 return Op == PreInc || Op == PreDec;
935 bool isPrefix() const { return isPrefix(Opc); }
936 bool isPostfix() const { return isPostfix(Opc); }
937 bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; }
938 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
939 bool isOffsetOfOp() const { return Opc == OffsetOf; }
940 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
941 bool isArithmeticOp() const { return isArithmeticOp(Opc); }
943 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
944 /// corresponds to, e.g. "sizeof" or "[pre]++"
945 static const char *getOpcodeStr(Opcode Op);
947 /// \brief Retrieve the unary opcode that corresponds to the given
948 /// overloaded operator.
949 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix);
951 /// \brief Retrieve the overloaded operator kind that corresponds to
952 /// the given unary opcode.
953 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc);
955 virtual SourceRange getSourceRange() const {
957 return SourceRange(Val->getLocStart(), Loc);
959 return SourceRange(Loc, Val->getLocEnd());
961 virtual SourceLocation getExprLoc() const { return Loc; }
963 static bool classof(const Stmt *T) {
964 return T->getStmtClass() == UnaryOperatorClass;
966 static bool classof(const UnaryOperator *) { return true; }
969 virtual child_iterator child_begin();
970 virtual child_iterator child_end();
973 /// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of
974 /// types and expressions.
975 class SizeOfAlignOfExpr : public Expr {
976 bool isSizeof : 1; // true if sizeof, false if alignof.
977 bool isType : 1; // true if operand is a type, false if an expression
982 SourceLocation OpLoc, RParenLoc;
985 virtual void DoDestroy(ASTContext& C);
988 SizeOfAlignOfExpr(bool issizeof, DeclaratorInfo *DInfo,
989 QualType resultType, SourceLocation op,
991 Expr(SizeOfAlignOfExprClass, resultType,
992 false, // Never type-dependent (C++ [temp.dep.expr]p3).
993 // Value-dependent if the argument is type-dependent.
994 DInfo->getType()->isDependentType()),
995 isSizeof(issizeof), isType(true), OpLoc(op), RParenLoc(rp) {
999 SizeOfAlignOfExpr(bool issizeof, Expr *E,
1000 QualType resultType, SourceLocation op,
1001 SourceLocation rp) :
1002 Expr(SizeOfAlignOfExprClass, resultType,
1003 false, // Never type-dependent (C++ [temp.dep.expr]p3).
1004 // Value-dependent if the argument is type-dependent.
1005 E->isTypeDependent()),
1006 isSizeof(issizeof), isType(false), OpLoc(op), RParenLoc(rp) {
1010 /// \brief Construct an empty sizeof/alignof expression.
1011 explicit SizeOfAlignOfExpr(EmptyShell Empty)
1012 : Expr(SizeOfAlignOfExprClass, Empty) { }
1014 bool isSizeOf() const { return isSizeof; }
1015 void setSizeof(bool S) { isSizeof = S; }
1017 bool isArgumentType() const { return isType; }
1018 QualType getArgumentType() const {
1019 return getArgumentTypeInfo()->getType();
1021 DeclaratorInfo *getArgumentTypeInfo() const {
1022 assert(isArgumentType() && "calling getArgumentType() when arg is expr");
1025 Expr *getArgumentExpr() {
1026 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type");
1027 return static_cast<Expr*>(Argument.Ex);
1029 const Expr *getArgumentExpr() const {
1030 return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr();
1033 void setArgument(Expr *E) { Argument.Ex = E; isType = false; }
1034 void setArgument(DeclaratorInfo *DInfo) {
1035 Argument.Ty = DInfo;
1039 /// Gets the argument type, or the type of the argument expression, whichever
1041 QualType getTypeOfArgument() const {
1042 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType();
1045 SourceLocation getOperatorLoc() const { return OpLoc; }
1046 void setOperatorLoc(SourceLocation L) { OpLoc = L; }
1048 SourceLocation getRParenLoc() const { return RParenLoc; }
1049 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1051 virtual SourceRange getSourceRange() const {
1052 return SourceRange(OpLoc, RParenLoc);
1055 static bool classof(const Stmt *T) {
1056 return T->getStmtClass() == SizeOfAlignOfExprClass;
1058 static bool classof(const SizeOfAlignOfExpr *) { return true; }
1061 virtual child_iterator child_begin();
1062 virtual child_iterator child_end();
1065 //===----------------------------------------------------------------------===//
1066 // Postfix Operators.
1067 //===----------------------------------------------------------------------===//
1069 /// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
1070 class ArraySubscriptExpr : public Expr {
1071 enum { LHS, RHS, END_EXPR=2 };
1072 Stmt* SubExprs[END_EXPR];
1073 SourceLocation RBracketLoc;
1075 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
1076 SourceLocation rbracketloc)
1077 : Expr(ArraySubscriptExprClass, t,
1078 lhs->isTypeDependent() || rhs->isTypeDependent(),
1079 lhs->isValueDependent() || rhs->isValueDependent()),
1080 RBracketLoc(rbracketloc) {
1081 SubExprs[LHS] = lhs;
1082 SubExprs[RHS] = rhs;
1085 /// \brief Create an empty array subscript expression.
1086 explicit ArraySubscriptExpr(EmptyShell Shell)
1087 : Expr(ArraySubscriptExprClass, Shell) { }
1089 /// An array access can be written A[4] or 4[A] (both are equivalent).
1090 /// - getBase() and getIdx() always present the normalized view: A[4].
1091 /// In this case getBase() returns "A" and getIdx() returns "4".
1092 /// - getLHS() and getRHS() present the syntactic view. e.g. for
1093 /// 4[A] getLHS() returns "4".
1094 /// Note: Because vector element access is also written A[4] we must
1095 /// predicate the format conversion in getBase and getIdx only on the
1096 /// the type of the RHS, as it is possible for the LHS to be a vector of
1098 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); }
1099 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1100 void setLHS(Expr *E) { SubExprs[LHS] = E; }
1102 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); }
1103 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1104 void setRHS(Expr *E) { SubExprs[RHS] = E; }
1107 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
1110 const Expr *getBase() const {
1111 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
1115 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
1118 const Expr *getIdx() const {
1119 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
1122 virtual SourceRange getSourceRange() const {
1123 return SourceRange(getLHS()->getLocStart(), RBracketLoc);
1126 SourceLocation getRBracketLoc() const { return RBracketLoc; }
1127 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
1129 virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); }
1131 static bool classof(const Stmt *T) {
1132 return T->getStmtClass() == ArraySubscriptExprClass;
1134 static bool classof(const ArraySubscriptExpr *) { return true; }
1137 virtual child_iterator child_begin();
1138 virtual child_iterator child_end();
1142 /// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
1143 /// CallExpr itself represents a normal function call, e.g., "f(x, 2)",
1144 /// while its subclasses may represent alternative syntax that (semantically)
1145 /// results in a function call. For example, CXXOperatorCallExpr is
1146 /// a subclass for overloaded operator calls that use operator syntax, e.g.,
1147 /// "str1 + str2" to resolve to a function call.
1148 class CallExpr : public Expr {
1149 enum { FN=0, ARGS_START=1 };
1152 SourceLocation RParenLoc;
1155 // This version of the constructor is for derived classes.
1156 CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs,
1157 QualType t, SourceLocation rparenloc);
1159 virtual void DoDestroy(ASTContext& C);
1162 CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t,
1163 SourceLocation rparenloc);
1165 /// \brief Build an empty call expression.
1166 CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty);
1170 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); }
1171 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); }
1172 void setCallee(Expr *F) { SubExprs[FN] = F; }
1174 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0.
1175 FunctionDecl *getDirectCallee();
1176 const FunctionDecl *getDirectCallee() const {
1177 return const_cast<CallExpr*>(this)->getDirectCallee();
1180 /// getNumArgs - Return the number of actual arguments to this call.
1182 unsigned getNumArgs() const { return NumArgs; }
1184 /// getArg - Return the specified argument.
1185 Expr *getArg(unsigned Arg) {
1186 assert(Arg < NumArgs && "Arg access out of range!");
1187 return cast<Expr>(SubExprs[Arg+ARGS_START]);
1189 const Expr *getArg(unsigned Arg) const {
1190 assert(Arg < NumArgs && "Arg access out of range!");
1191 return cast<Expr>(SubExprs[Arg+ARGS_START]);
1194 /// setArg - Set the specified argument.
1195 void setArg(unsigned Arg, Expr *ArgExpr) {
1196 assert(Arg < NumArgs && "Arg access out of range!");
1197 SubExprs[Arg+ARGS_START] = ArgExpr;
1200 /// setNumArgs - This changes the number of arguments present in this call.
1201 /// Any orphaned expressions are deleted by this, and any new operands are set
1203 void setNumArgs(ASTContext& C, unsigned NumArgs);
1205 typedef ExprIterator arg_iterator;
1206 typedef ConstExprIterator const_arg_iterator;
1208 arg_iterator arg_begin() { return SubExprs+ARGS_START; }
1209 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
1210 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; }
1211 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();}
1213 /// getNumCommas - Return the number of commas that must have been present in
1214 /// this function call.
1215 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
1217 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1219 unsigned isBuiltinCall(ASTContext &Context) const;
1221 /// getCallReturnType - Get the return type of the call expr. This is not
1222 /// always the type of the expr itself, if the return type is a reference
1224 QualType getCallReturnType() const;
1226 SourceLocation getRParenLoc() const { return RParenLoc; }
1227 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1229 virtual SourceRange getSourceRange() const {
1230 return SourceRange(getCallee()->getLocStart(), RParenLoc);
1233 static bool classof(const Stmt *T) {
1234 return T->getStmtClass() == CallExprClass ||
1235 T->getStmtClass() == CXXOperatorCallExprClass ||
1236 T->getStmtClass() == CXXMemberCallExprClass;
1238 static bool classof(const CallExpr *) { return true; }
1239 static bool classof(const CXXOperatorCallExpr *) { return true; }
1240 static bool classof(const CXXMemberCallExpr *) { return true; }
1243 virtual child_iterator child_begin();
1244 virtual child_iterator child_end();
1247 /// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F.
1249 class MemberExpr : public Expr {
1250 /// Base - the expression for the base pointer or structure references. In
1251 /// X.F, this is "X".
1254 /// MemberDecl - This is the decl being referenced by the field/member name.
1255 /// In X.F, this is the decl referenced by F.
1256 NamedDecl *MemberDecl;
1258 /// MemberLoc - This is the location of the member name.
1259 SourceLocation MemberLoc;
1261 /// IsArrow - True if this is "X->F", false if this is "X.F".
1264 /// \brief True if this member expression used a nested-name-specifier to
1265 /// refer to the member, e.g., "x->Base::f". When true, a NameQualifier
1266 /// structure is allocated immediately after the MemberExpr.
1267 bool HasQualifier : 1;
1269 /// \brief True if this member expression specified a template argument list
1270 /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList
1271 /// structure (and its TemplateArguments) are allocated immediately after
1272 /// the MemberExpr or, if the member expression also has a qualifier, after
1273 /// the NameQualifier structure.
1274 bool HasExplicitTemplateArgumentList : 1;
1276 /// \brief Retrieve the qualifier that preceded the member name, if any.
1277 NameQualifier *getMemberQualifier() {
1281 return reinterpret_cast<NameQualifier *> (this + 1);
1284 /// \brief Retrieve the qualifier that preceded the member name, if any.
1285 const NameQualifier *getMemberQualifier() const {
1286 return const_cast<MemberExpr *>(this)->getMemberQualifier();
1289 /// \brief Retrieve the explicit template argument list that followed the
1290 /// member template name, if any.
1291 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() {
1292 if (!HasExplicitTemplateArgumentList)
1296 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
1298 return reinterpret_cast<ExplicitTemplateArgumentList *>(
1299 getMemberQualifier() + 1);
1302 /// \brief Retrieve the explicit template argument list that followed the
1303 /// member template name, if any.
1304 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const {
1305 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgumentList();
1308 MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
1309 SourceRange qualrange, NamedDecl *memberdecl, SourceLocation l,
1310 bool has_explicit, SourceLocation langle,
1311 const TemplateArgumentLoc *targs, unsigned numtargs,
1312 SourceLocation rangle, QualType ty);
1315 MemberExpr(Expr *base, bool isarrow, NamedDecl *memberdecl, SourceLocation l,
1317 : Expr(MemberExprClass, ty,
1318 base->isTypeDependent(), base->isValueDependent()),
1319 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
1320 HasQualifier(false), HasExplicitTemplateArgumentList(false) {}
1322 /// \brief Build an empty member reference expression.
1323 explicit MemberExpr(EmptyShell Empty)
1324 : Expr(MemberExprClass, Empty), HasQualifier(false),
1325 HasExplicitTemplateArgumentList(false) { }
1327 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow,
1328 NestedNameSpecifier *qual, SourceRange qualrange,
1329 NamedDecl *memberdecl,
1332 SourceLocation langle,
1333 const TemplateArgumentLoc *targs,
1335 SourceLocation rangle,
1338 void setBase(Expr *E) { Base = E; }
1339 Expr *getBase() const { return cast<Expr>(Base); }
1341 /// \brief Retrieve the member declaration to which this expression refers.
1343 /// The returned declaration will either be a FieldDecl or (in C++)
1344 /// a CXXMethodDecl.
1345 NamedDecl *getMemberDecl() const { return MemberDecl; }
1346 void setMemberDecl(NamedDecl *D) { MemberDecl = D; }
1348 /// \brief Determines whether this member expression actually had
1349 /// a C++ nested-name-specifier prior to the name of the member, e.g.,
1351 bool hasQualifier() const { return HasQualifier; }
1353 /// \brief If the member name was qualified, retrieves the source range of
1354 /// the nested-name-specifier that precedes the member name. Otherwise,
1355 /// returns an empty source range.
1356 SourceRange getQualifierRange() const {
1358 return SourceRange();
1360 return getMemberQualifier()->Range;
1363 /// \brief If the member name was qualified, retrieves the
1364 /// nested-name-specifier that precedes the member name. Otherwise, returns
1366 NestedNameSpecifier *getQualifier() const {
1370 return getMemberQualifier()->NNS;
1373 /// \brief Determines whether this member expression actually had a C++
1374 /// template argument list explicitly specified, e.g., x.f<int>.
1375 bool hasExplicitTemplateArgumentList() {
1376 return HasExplicitTemplateArgumentList;
1379 /// \brief Retrieve the location of the left angle bracket following the
1380 /// member name ('<'), if any.
1381 SourceLocation getLAngleLoc() const {
1382 if (!HasExplicitTemplateArgumentList)
1383 return SourceLocation();
1385 return getExplicitTemplateArgumentList()->LAngleLoc;
1388 /// \brief Retrieve the template arguments provided as part of this
1390 const TemplateArgumentLoc *getTemplateArgs() const {
1391 if (!HasExplicitTemplateArgumentList)
1394 return getExplicitTemplateArgumentList()->getTemplateArgs();
1397 /// \brief Retrieve the number of template arguments provided as part of this
1399 unsigned getNumTemplateArgs() const {
1400 if (!HasExplicitTemplateArgumentList)
1403 return getExplicitTemplateArgumentList()->NumTemplateArgs;
1406 /// \brief Retrieve the location of the right angle bracket following the
1407 /// template arguments ('>').
1408 SourceLocation getRAngleLoc() const {
1409 if (!HasExplicitTemplateArgumentList)
1410 return SourceLocation();
1412 return getExplicitTemplateArgumentList()->RAngleLoc;
1415 bool isArrow() const { return IsArrow; }
1416 void setArrow(bool A) { IsArrow = A; }
1418 /// getMemberLoc - Return the location of the "member", in X->F, it is the
1419 /// location of 'F'.
1420 SourceLocation getMemberLoc() const { return MemberLoc; }
1421 void setMemberLoc(SourceLocation L) { MemberLoc = L; }
1423 virtual SourceRange getSourceRange() const {
1424 // If we have an implicit base (like a C++ implicit this),
1425 // make sure not to return its location
1426 SourceLocation EndLoc = MemberLoc;
1427 if (HasExplicitTemplateArgumentList)
1428 EndLoc = getRAngleLoc();
1430 SourceLocation BaseLoc = getBase()->getLocStart();
1431 if (BaseLoc.isInvalid())
1432 return SourceRange(MemberLoc, EndLoc);
1433 return SourceRange(BaseLoc, EndLoc);
1436 virtual SourceLocation getExprLoc() const { return MemberLoc; }
1438 static bool classof(const Stmt *T) {
1439 return T->getStmtClass() == MemberExprClass;
1441 static bool classof(const MemberExpr *) { return true; }
1444 virtual child_iterator child_begin();
1445 virtual child_iterator child_end();
1448 /// CompoundLiteralExpr - [C99 6.5.2.5]
1450 class CompoundLiteralExpr : public Expr {
1451 /// LParenLoc - If non-null, this is the location of the left paren in a
1452 /// compound literal like "(int){4}". This can be null if this is a
1453 /// synthesized compound expression.
1454 SourceLocation LParenLoc;
1458 CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init,
1460 : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init),
1461 FileScope(fileScope) {}
1463 /// \brief Construct an empty compound literal.
1464 explicit CompoundLiteralExpr(EmptyShell Empty)
1465 : Expr(CompoundLiteralExprClass, Empty) { }
1467 const Expr *getInitializer() const { return cast<Expr>(Init); }
1468 Expr *getInitializer() { return cast<Expr>(Init); }
1469 void setInitializer(Expr *E) { Init = E; }
1471 bool isFileScope() const { return FileScope; }
1472 void setFileScope(bool FS) { FileScope = FS; }
1474 SourceLocation getLParenLoc() const { return LParenLoc; }
1475 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1477 virtual SourceRange getSourceRange() const {
1478 // FIXME: Init should never be null.
1480 return SourceRange();
1481 if (LParenLoc.isInvalid())
1482 return Init->getSourceRange();
1483 return SourceRange(LParenLoc, Init->getLocEnd());
1486 static bool classof(const Stmt *T) {
1487 return T->getStmtClass() == CompoundLiteralExprClass;
1489 static bool classof(const CompoundLiteralExpr *) { return true; }
1492 virtual child_iterator child_begin();
1493 virtual child_iterator child_end();
1496 /// CastExpr - Base class for type casts, including both implicit
1497 /// casts (ImplicitCastExpr) and explicit casts that have some
1498 /// representation in the source code (ExplicitCastExpr's derived
1500 class CastExpr : public Expr {
1502 /// CastKind - the kind of cast this represents.
1504 /// CK_Unknown - Unknown cast kind.
1505 /// FIXME: The goal is to get rid of this and make all casts have a
1506 /// kind so that the AST client doesn't have to try to figure out what's
1510 /// CK_BitCast - Used for reinterpret_cast.
1513 /// CK_NoOp - Used for const_cast.
1516 /// CK_DerivedToBase - Derived to base class casts.
1519 /// CK_Dynamic - Dynamic cast.
1522 /// CK_ToUnion - Cast to union (GCC extension).
1525 /// CK_ArrayToPointerDecay - Array to pointer decay.
1526 CK_ArrayToPointerDecay,
1528 // CK_FunctionToPointerDecay - Function to pointer decay.
1529 CK_FunctionToPointerDecay,
1531 /// CK_NullToMemberPointer - Null pointer to member pointer.
1532 CK_NullToMemberPointer,
1534 /// CK_BaseToDerivedMemberPointer - Member pointer in base class to
1535 /// member pointer in derived class.
1536 CK_BaseToDerivedMemberPointer,
1538 /// CK_DerivedToBaseMemberPointer - Member pointer in derived class to
1539 /// member pointer in base class.
1540 CK_DerivedToBaseMemberPointer,
1542 /// CK_UserDefinedConversion - Conversion using a user defined type
1543 /// conversion function.
1544 CK_UserDefinedConversion,
1546 /// CK_ConstructorConversion - Conversion by constructor
1547 CK_ConstructorConversion,
1549 /// CK_IntegralToPointer - Integral to pointer
1550 CK_IntegralToPointer,
1552 /// CK_PointerToIntegral - Pointer to integral
1553 CK_PointerToIntegral,
1555 /// CK_ToVoid - Cast to void.
1558 /// CK_VectorSplat - Casting from an integer/floating type to an extended
1559 /// vector type with the same element type as the src type. Splats the
1560 /// src expression into the destination expression.
1563 /// CK_IntegralCast - Casting between integral types of different size.
1566 /// CK_IntegralToFloating - Integral to floating point.
1567 CK_IntegralToFloating,
1569 /// CK_FloatingToIntegral - Floating point to integral.
1570 CK_FloatingToIntegral,
1572 /// CK_FloatingCast - Casting between floating types of different size.
1580 CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op) :
1582 // Cast expressions are type-dependent if the type is
1583 // dependent (C++ [temp.dep.expr]p3).
1584 ty->isDependentType(),
1585 // Cast expressions are value-dependent if the type is
1586 // dependent or if the subexpression is value-dependent.
1587 ty->isDependentType() || (op && op->isValueDependent())),
1588 Kind(kind), Op(op) {}
1590 /// \brief Construct an empty cast.
1591 CastExpr(StmtClass SC, EmptyShell Empty)
1592 : Expr(SC, Empty) { }
1595 CastKind getCastKind() const { return Kind; }
1596 void setCastKind(CastKind K) { Kind = K; }
1597 const char *getCastKindName() const;
1599 Expr *getSubExpr() { return cast<Expr>(Op); }
1600 const Expr *getSubExpr() const { return cast<Expr>(Op); }
1601 void setSubExpr(Expr *E) { Op = E; }
1603 static bool classof(const Stmt *T) {
1604 StmtClass SC = T->getStmtClass();
1605 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass)
1608 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass)
1613 static bool classof(const CastExpr *) { return true; }
1616 virtual child_iterator child_begin();
1617 virtual child_iterator child_end();
1620 /// ImplicitCastExpr - Allows us to explicitly represent implicit type
1621 /// conversions, which have no direct representation in the original
1622 /// source code. For example: converting T[]->T*, void f()->void
1623 /// (*f)(), float->double, short->int, etc.
1625 /// In C, implicit casts always produce rvalues. However, in C++, an
1626 /// implicit cast whose result is being bound to a reference will be
1627 /// an lvalue. For example:
1631 /// class Derived : public Base { };
1632 /// void f(Derived d) {
1633 /// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base
1636 class ImplicitCastExpr : public CastExpr {
1637 /// LvalueCast - Whether this cast produces an lvalue.
1641 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, bool Lvalue) :
1642 CastExpr(ImplicitCastExprClass, ty, kind, op), LvalueCast(Lvalue) { }
1644 /// \brief Construct an empty implicit cast.
1645 explicit ImplicitCastExpr(EmptyShell Shell)
1646 : CastExpr(ImplicitCastExprClass, Shell) { }
1649 virtual SourceRange getSourceRange() const {
1650 return getSubExpr()->getSourceRange();
1653 /// isLvalueCast - Whether this cast produces an lvalue.
1654 bool isLvalueCast() const { return LvalueCast; }
1656 /// setLvalueCast - Set whether this cast produces an lvalue.
1657 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; }
1659 static bool classof(const Stmt *T) {
1660 return T->getStmtClass() == ImplicitCastExprClass;
1662 static bool classof(const ImplicitCastExpr *) { return true; }
1665 /// ExplicitCastExpr - An explicit cast written in the source
1668 /// This class is effectively an abstract class, because it provides
1669 /// the basic representation of an explicitly-written cast without
1670 /// specifying which kind of cast (C cast, functional cast, static
1671 /// cast, etc.) was written; specific derived classes represent the
1672 /// particular style of cast and its location information.
1674 /// Unlike implicit casts, explicit cast nodes have two different
1675 /// types: the type that was written into the source code, and the
1676 /// actual type of the expression as determined by semantic
1677 /// analysis. These types may differ slightly. For example, in C++ one
1678 /// can cast to a reference type, which indicates that the resulting
1679 /// expression will be an lvalue. The reference type, however, will
1680 /// not be used as the type of the expression.
1681 class ExplicitCastExpr : public CastExpr {
1682 /// TypeAsWritten - The type that this expression is casting to, as
1683 /// written in the source code.
1684 QualType TypeAsWritten;
1687 ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind,
1688 Expr *op, QualType writtenTy)
1689 : CastExpr(SC, exprTy, kind, op), TypeAsWritten(writtenTy) {}
1691 /// \brief Construct an empty explicit cast.
1692 ExplicitCastExpr(StmtClass SC, EmptyShell Shell)
1693 : CastExpr(SC, Shell) { }
1696 /// getTypeAsWritten - Returns the type that this expression is
1697 /// casting to, as written in the source code.
1698 QualType getTypeAsWritten() const { return TypeAsWritten; }
1699 void setTypeAsWritten(QualType T) { TypeAsWritten = T; }
1701 static bool classof(const Stmt *T) {
1702 StmtClass SC = T->getStmtClass();
1703 if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass)
1705 if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass)
1710 static bool classof(const ExplicitCastExpr *) { return true; }
1713 /// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style
1714 /// cast in C++ (C++ [expr.cast]), which uses the syntax
1715 /// (Type)expr. For example: @c (int)f.
1716 class CStyleCastExpr : public ExplicitCastExpr {
1717 SourceLocation LPLoc; // the location of the left paren
1718 SourceLocation RPLoc; // the location of the right paren
1720 CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op, QualType writtenTy,
1721 SourceLocation l, SourceLocation r) :
1722 ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, writtenTy),
1723 LPLoc(l), RPLoc(r) {}
1725 /// \brief Construct an empty C-style explicit cast.
1726 explicit CStyleCastExpr(EmptyShell Shell)
1727 : ExplicitCastExpr(CStyleCastExprClass, Shell) { }
1729 SourceLocation getLParenLoc() const { return LPLoc; }
1730 void setLParenLoc(SourceLocation L) { LPLoc = L; }
1732 SourceLocation getRParenLoc() const { return RPLoc; }
1733 void setRParenLoc(SourceLocation L) { RPLoc = L; }
1735 virtual SourceRange getSourceRange() const {
1736 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd());
1738 static bool classof(const Stmt *T) {
1739 return T->getStmtClass() == CStyleCastExprClass;
1741 static bool classof(const CStyleCastExpr *) { return true; }
1744 /// \brief A builtin binary operation expression such as "x + y" or "x <= y".
1746 /// This expression node kind describes a builtin binary operation,
1747 /// such as "x + y" for integer values "x" and "y". The operands will
1748 /// already have been converted to appropriate types (e.g., by
1749 /// performing promotions or conversions).
1751 /// In C++, where operators may be overloaded, a different kind of
1752 /// expression node (CXXOperatorCallExpr) is used to express the
1753 /// invocation of an overloaded operator with operator syntax. Within
1754 /// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is
1755 /// used to store an expression "x + y" depends on the subexpressions
1756 /// for x and y. If neither x or y is type-dependent, and the "+"
1757 /// operator resolves to a built-in operation, BinaryOperator will be
1758 /// used to express the computation (x and y may still be
1759 /// value-dependent). If either x or y is type-dependent, or if the
1760 /// "+" resolves to an overloaded operator, CXXOperatorCallExpr will
1761 /// be used to express the computation.
1762 class BinaryOperator : public Expr {
1765 // Operators listed in order of precedence.
1766 // Note that additions to this should also update the StmtVisitor class.
1767 PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators.
1768 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators.
1769 Add, Sub, // [C99 6.5.6] Additive operators.
1770 Shl, Shr, // [C99 6.5.7] Bitwise shift operators.
1771 LT, GT, LE, GE, // [C99 6.5.8] Relational operators.
1772 EQ, NE, // [C99 6.5.9] Equality operators.
1773 And, // [C99 6.5.10] Bitwise AND operator.
1774 Xor, // [C99 6.5.11] Bitwise XOR operator.
1775 Or, // [C99 6.5.12] Bitwise OR operator.
1776 LAnd, // [C99 6.5.13] Logical AND operator.
1777 LOr, // [C99 6.5.14] Logical OR operator.
1778 Assign, MulAssign,// [C99 6.5.16] Assignment operators.
1779 DivAssign, RemAssign,
1780 AddAssign, SubAssign,
1781 ShlAssign, ShrAssign,
1782 AndAssign, XorAssign,
1784 Comma // [C99 6.5.17] Comma operator.
1787 enum { LHS, RHS, END_EXPR };
1788 Stmt* SubExprs[END_EXPR];
1790 SourceLocation OpLoc;
1793 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1794 SourceLocation opLoc)
1795 : Expr(BinaryOperatorClass, ResTy,
1796 lhs->isTypeDependent() || rhs->isTypeDependent(),
1797 lhs->isValueDependent() || rhs->isValueDependent()),
1798 Opc(opc), OpLoc(opLoc) {
1799 SubExprs[LHS] = lhs;
1800 SubExprs[RHS] = rhs;
1801 assert(!isCompoundAssignmentOp() &&
1802 "Use ArithAssignBinaryOperator for compound assignments");
1805 /// \brief Construct an empty binary operator.
1806 explicit BinaryOperator(EmptyShell Empty)
1807 : Expr(BinaryOperatorClass, Empty), Opc(Comma) { }
1809 SourceLocation getOperatorLoc() const { return OpLoc; }
1810 void setOperatorLoc(SourceLocation L) { OpLoc = L; }
1812 Opcode getOpcode() const { return Opc; }
1813 void setOpcode(Opcode O) { Opc = O; }
1815 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1816 void setLHS(Expr *E) { SubExprs[LHS] = E; }
1817 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1818 void setRHS(Expr *E) { SubExprs[RHS] = E; }
1820 virtual SourceRange getSourceRange() const {
1821 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
1824 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1825 /// corresponds to, e.g. "<<=".
1826 static const char *getOpcodeStr(Opcode Op);
1828 /// \brief Retrieve the binary opcode that corresponds to the given
1829 /// overloaded operator.
1830 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO);
1832 /// \brief Retrieve the overloaded operator kind that corresponds to
1833 /// the given binary opcode.
1834 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc);
1836 /// predicates to categorize the respective opcodes.
1837 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
1838 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
1839 static bool isShiftOp(Opcode Opc) { return Opc == Shl || Opc == Shr; }
1840 bool isShiftOp() const { return isShiftOp(Opc); }
1842 static bool isBitwiseOp(Opcode Opc) { return Opc >= And && Opc <= Or; }
1843 bool isBitwiseOp() const { return isBitwiseOp(Opc); }
1845 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; }
1846 bool isRelationalOp() const { return isRelationalOp(Opc); }
1848 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; }
1849 bool isEqualityOp() const { return isEqualityOp(Opc); }
1851 static bool isComparisonOp(Opcode Opc) { return Opc >= LT && Opc <= NE; }
1852 bool isComparisonOp() const { return isComparisonOp(Opc); }
1854 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; }
1855 bool isLogicalOp() const { return isLogicalOp(Opc); }
1857 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
1858 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
1859 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
1861 static bool classof(const Stmt *S) {
1862 return S->getStmtClass() == BinaryOperatorClass ||
1863 S->getStmtClass() == CompoundAssignOperatorClass;
1865 static bool classof(const BinaryOperator *) { return true; }
1868 virtual child_iterator child_begin();
1869 virtual child_iterator child_end();
1872 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1873 SourceLocation oploc, bool dead)
1874 : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
1875 SubExprs[LHS] = lhs;
1876 SubExprs[RHS] = rhs;
1879 BinaryOperator(StmtClass SC, EmptyShell Empty)
1880 : Expr(SC, Empty), Opc(MulAssign) { }
1883 /// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
1884 /// track of the type the operation is performed in. Due to the semantics of
1885 /// these operators, the operands are promoted, the aritmetic performed, an
1886 /// implicit conversion back to the result type done, then the assignment takes
1887 /// place. This captures the intermediate type which the computation is done
1889 class CompoundAssignOperator : public BinaryOperator {
1890 QualType ComputationLHSType;
1891 QualType ComputationResultType;
1893 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
1894 QualType ResType, QualType CompLHSType,
1895 QualType CompResultType,
1896 SourceLocation OpLoc)
1897 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
1898 ComputationLHSType(CompLHSType),
1899 ComputationResultType(CompResultType) {
1900 assert(isCompoundAssignmentOp() &&
1901 "Only should be used for compound assignments");
1904 /// \brief Build an empty compound assignment operator expression.
1905 explicit CompoundAssignOperator(EmptyShell Empty)
1906 : BinaryOperator(CompoundAssignOperatorClass, Empty) { }
1908 // The two computation types are the type the LHS is converted
1909 // to for the computation and the type of the result; the two are
1910 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr).
1911 QualType getComputationLHSType() const { return ComputationLHSType; }
1912 void setComputationLHSType(QualType T) { ComputationLHSType = T; }
1914 QualType getComputationResultType() const { return ComputationResultType; }
1915 void setComputationResultType(QualType T) { ComputationResultType = T; }
1917 static bool classof(const CompoundAssignOperator *) { return true; }
1918 static bool classof(const Stmt *S) {
1919 return S->getStmtClass() == CompoundAssignOperatorClass;
1923 /// ConditionalOperator - The ?: operator. Note that LHS may be null when the
1924 /// GNU "missing LHS" extension is in use.
1926 class ConditionalOperator : public Expr {
1927 enum { COND, LHS, RHS, END_EXPR };
1928 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1929 SourceLocation QuestionLoc, ColonLoc;
1931 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs,
1932 SourceLocation CLoc, Expr *rhs, QualType t)
1933 : Expr(ConditionalOperatorClass, t,
1934 // FIXME: the type of the conditional operator doesn't
1935 // depend on the type of the conditional, but the standard
1936 // seems to imply that it could. File a bug!
1937 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())),
1938 (cond->isValueDependent() ||
1939 (lhs && lhs->isValueDependent()) ||
1940 (rhs && rhs->isValueDependent()))),
1943 SubExprs[COND] = cond;
1944 SubExprs[LHS] = lhs;
1945 SubExprs[RHS] = rhs;
1948 /// \brief Build an empty conditional operator.
1949 explicit ConditionalOperator(EmptyShell Empty)
1950 : Expr(ConditionalOperatorClass, Empty) { }
1952 // getCond - Return the expression representing the condition for
1954 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
1955 void setCond(Expr *E) { SubExprs[COND] = E; }
1957 // getTrueExpr - Return the subexpression representing the value of the ?:
1958 // expression if the condition evaluates to true. In most cases this value
1959 // will be the same as getLHS() except a GCC extension allows the left
1960 // subexpression to be omitted, and instead of the condition be returned.
1961 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
1962 // is only evaluated once.
1963 Expr *getTrueExpr() const {
1964 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]);
1967 // getTrueExpr - Return the subexpression representing the value of the ?:
1968 // expression if the condition evaluates to false. This is the same as getRHS.
1969 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); }
1971 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); }
1972 void setLHS(Expr *E) { SubExprs[LHS] = E; }
1974 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1975 void setRHS(Expr *E) { SubExprs[RHS] = E; }
1977 SourceLocation getQuestionLoc() const { return QuestionLoc; }
1978 void setQuestionLoc(SourceLocation L) { QuestionLoc = L; }
1980 SourceLocation getColonLoc() const { return ColonLoc; }
1981 void setColonLoc(SourceLocation L) { ColonLoc = L; }
1983 virtual SourceRange getSourceRange() const {
1984 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
1986 static bool classof(const Stmt *T) {
1987 return T->getStmtClass() == ConditionalOperatorClass;
1989 static bool classof(const ConditionalOperator *) { return true; }
1992 virtual child_iterator child_begin();
1993 virtual child_iterator child_end();
1996 /// AddrLabelExpr - The GNU address of label extension, representing &&label.
1997 class AddrLabelExpr : public Expr {
1998 SourceLocation AmpAmpLoc, LabelLoc;
2001 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
2003 : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
2005 /// \brief Build an empty address of a label expression.
2006 explicit AddrLabelExpr(EmptyShell Empty)
2007 : Expr(AddrLabelExprClass, Empty) { }
2009 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; }
2010 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; }
2011 SourceLocation getLabelLoc() const { return LabelLoc; }
2012 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
2014 virtual SourceRange getSourceRange() const {
2015 return SourceRange(AmpAmpLoc, LabelLoc);
2018 LabelStmt *getLabel() const { return Label; }
2019 void setLabel(LabelStmt *S) { Label = S; }
2021 static bool classof(const Stmt *T) {
2022 return T->getStmtClass() == AddrLabelExprClass;
2024 static bool classof(const AddrLabelExpr *) { return true; }
2027 virtual child_iterator child_begin();
2028 virtual child_iterator child_end();
2031 /// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
2032 /// The StmtExpr contains a single CompoundStmt node, which it evaluates and
2033 /// takes the value of the last subexpression.
2034 class StmtExpr : public Expr {
2036 SourceLocation LParenLoc, RParenLoc;
2038 StmtExpr(CompoundStmt *substmt, QualType T,
2039 SourceLocation lp, SourceLocation rp) :
2040 Expr(StmtExprClass, T), SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { }
2042 /// \brief Build an empty statement expression.
2043 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { }
2045 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); }
2046 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); }
2047 void setSubStmt(CompoundStmt *S) { SubStmt = S; }
2049 virtual SourceRange getSourceRange() const {
2050 return SourceRange(LParenLoc, RParenLoc);
2053 SourceLocation getLParenLoc() const { return LParenLoc; }
2054 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2055 SourceLocation getRParenLoc() const { return RParenLoc; }
2056 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2058 static bool classof(const Stmt *T) {
2059 return T->getStmtClass() == StmtExprClass;
2061 static bool classof(const StmtExpr *) { return true; }
2064 virtual child_iterator child_begin();
2065 virtual child_iterator child_end();
2068 /// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p.
2069 /// This AST node represents a function that returns 1 if two *types* (not
2070 /// expressions) are compatible. The result of this built-in function can be
2071 /// used in integer constant expressions.
2072 class TypesCompatibleExpr : public Expr {
2075 SourceLocation BuiltinLoc, RParenLoc;
2077 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
2078 QualType t1, QualType t2, SourceLocation RP) :
2079 Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
2080 BuiltinLoc(BLoc), RParenLoc(RP) {}
2082 /// \brief Build an empty __builtin_type_compatible_p expression.
2083 explicit TypesCompatibleExpr(EmptyShell Empty)
2084 : Expr(TypesCompatibleExprClass, Empty) { }
2086 QualType getArgType1() const { return Type1; }
2087 void setArgType1(QualType T) { Type1 = T; }
2088 QualType getArgType2() const { return Type2; }
2089 void setArgType2(QualType T) { Type2 = T; }
2091 SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2092 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2094 SourceLocation getRParenLoc() const { return RParenLoc; }
2095 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2097 virtual SourceRange getSourceRange() const {
2098 return SourceRange(BuiltinLoc, RParenLoc);
2100 static bool classof(const Stmt *T) {
2101 return T->getStmtClass() == TypesCompatibleExprClass;
2103 static bool classof(const TypesCompatibleExpr *) { return true; }
2106 virtual child_iterator child_begin();
2107 virtual child_iterator child_end();
2110 /// ShuffleVectorExpr - clang-specific builtin-in function
2111 /// __builtin_shufflevector.
2112 /// This AST node represents a operator that does a constant
2113 /// shuffle, similar to LLVM's shufflevector instruction. It takes
2114 /// two vectors and a variable number of constant indices,
2115 /// and returns the appropriately shuffled vector.
2116 class ShuffleVectorExpr : public Expr {
2117 SourceLocation BuiltinLoc, RParenLoc;
2119 // SubExprs - the list of values passed to the __builtin_shufflevector
2120 // function. The first two are vectors, and the rest are constant
2121 // indices. The number of values in this list is always
2122 // 2+the number of indices in the vector type.
2127 virtual void DoDestroy(ASTContext &C);
2130 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2131 QualType Type, SourceLocation BLoc,
2132 SourceLocation RP) :
2133 Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc),
2134 RParenLoc(RP), NumExprs(nexpr) {
2136 SubExprs = new (C) Stmt*[nexpr];
2137 for (unsigned i = 0; i < nexpr; i++)
2138 SubExprs[i] = args[i];
2141 /// \brief Build an empty vector-shuffle expression.
2142 explicit ShuffleVectorExpr(EmptyShell Empty)
2143 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { }
2145 SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2146 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2148 SourceLocation getRParenLoc() const { return RParenLoc; }
2149 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2151 virtual SourceRange getSourceRange() const {
2152 return SourceRange(BuiltinLoc, RParenLoc);
2154 static bool classof(const Stmt *T) {
2155 return T->getStmtClass() == ShuffleVectorExprClass;
2157 static bool classof(const ShuffleVectorExpr *) { return true; }
2159 ~ShuffleVectorExpr() {}
2161 /// getNumSubExprs - Return the size of the SubExprs array. This includes the
2162 /// constant expression, the actual arguments passed in, and the function
2164 unsigned getNumSubExprs() const { return NumExprs; }
2166 /// getExpr - Return the Expr at the specified index.
2167 Expr *getExpr(unsigned Index) {
2168 assert((Index < NumExprs) && "Arg access out of range!");
2169 return cast<Expr>(SubExprs[Index]);
2171 const Expr *getExpr(unsigned Index) const {
2172 assert((Index < NumExprs) && "Arg access out of range!");
2173 return cast<Expr>(SubExprs[Index]);
2176 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs);
2178 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) {
2179 assert((N < NumExprs - 2) && "Shuffle idx out of range!");
2180 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue();
2184 virtual child_iterator child_begin();
2185 virtual child_iterator child_end();
2188 /// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
2189 /// This AST node is similar to the conditional operator (?:) in C, with
2190 /// the following exceptions:
2191 /// - the test expression must be a integer constant expression.
2192 /// - the expression returned acts like the chosen subexpression in every
2193 /// visible way: the type is the same as that of the chosen subexpression,
2194 /// and all predicates (whether it's an l-value, whether it's an integer
2195 /// constant expression, etc.) return the same result as for the chosen
2197 class ChooseExpr : public Expr {
2198 enum { COND, LHS, RHS, END_EXPR };
2199 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
2200 SourceLocation BuiltinLoc, RParenLoc;
2202 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
2203 SourceLocation RP, bool TypeDependent, bool ValueDependent)
2204 : Expr(ChooseExprClass, t, TypeDependent, ValueDependent),
2205 BuiltinLoc(BLoc), RParenLoc(RP) {
2206 SubExprs[COND] = cond;
2207 SubExprs[LHS] = lhs;
2208 SubExprs[RHS] = rhs;
2211 /// \brief Build an empty __builtin_choose_expr.
2212 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { }
2214 /// isConditionTrue - Return whether the condition is true (i.e. not
2216 bool isConditionTrue(ASTContext &C) const;
2218 /// getChosenSubExpr - Return the subexpression chosen according to the
2220 Expr *getChosenSubExpr(ASTContext &C) const {
2221 return isConditionTrue(C) ? getLHS() : getRHS();
2224 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
2225 void setCond(Expr *E) { SubExprs[COND] = E; }
2226 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
2227 void setLHS(Expr *E) { SubExprs[LHS] = E; }
2228 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
2229 void setRHS(Expr *E) { SubExprs[RHS] = E; }
2231 SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2232 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2234 SourceLocation getRParenLoc() const { return RParenLoc; }
2235 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2237 virtual SourceRange getSourceRange() const {
2238 return SourceRange(BuiltinLoc, RParenLoc);
2240 static bool classof(const Stmt *T) {
2241 return T->getStmtClass() == ChooseExprClass;
2243 static bool classof(const ChooseExpr *) { return true; }
2246 virtual child_iterator child_begin();
2247 virtual child_iterator child_end();
2250 /// GNUNullExpr - Implements the GNU __null extension, which is a name
2251 /// for a null pointer constant that has integral type (e.g., int or
2252 /// long) and is the same size and alignment as a pointer. The __null
2253 /// extension is typically only used by system headers, which define
2254 /// NULL as __null in C++ rather than using 0 (which is an integer
2255 /// that may not match the size of a pointer).
2256 class GNUNullExpr : public Expr {
2257 /// TokenLoc - The location of the __null keyword.
2258 SourceLocation TokenLoc;
2261 GNUNullExpr(QualType Ty, SourceLocation Loc)
2262 : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { }
2264 /// \brief Build an empty GNU __null expression.
2265 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { }
2267 /// getTokenLocation - The location of the __null token.
2268 SourceLocation getTokenLocation() const { return TokenLoc; }
2269 void setTokenLocation(SourceLocation L) { TokenLoc = L; }
2271 virtual SourceRange getSourceRange() const {
2272 return SourceRange(TokenLoc);
2274 static bool classof(const Stmt *T) {
2275 return T->getStmtClass() == GNUNullExprClass;
2277 static bool classof(const GNUNullExpr *) { return true; }
2280 virtual child_iterator child_begin();
2281 virtual child_iterator child_end();
2284 /// VAArgExpr, used for the builtin function __builtin_va_start.
2285 class VAArgExpr : public Expr {
2287 SourceLocation BuiltinLoc, RParenLoc;
2289 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
2290 : Expr(VAArgExprClass, t),
2293 RParenLoc(RPLoc) { }
2295 /// \brief Create an empty __builtin_va_start expression.
2296 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { }
2298 const Expr *getSubExpr() const { return cast<Expr>(Val); }
2299 Expr *getSubExpr() { return cast<Expr>(Val); }
2300 void setSubExpr(Expr *E) { Val = E; }
2302 SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2303 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2305 SourceLocation getRParenLoc() const { return RParenLoc; }
2306 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2308 virtual SourceRange getSourceRange() const {
2309 return SourceRange(BuiltinLoc, RParenLoc);
2311 static bool classof(const Stmt *T) {
2312 return T->getStmtClass() == VAArgExprClass;
2314 static bool classof(const VAArgExpr *) { return true; }
2317 virtual child_iterator child_begin();
2318 virtual child_iterator child_end();
2321 /// @brief Describes an C or C++ initializer list.
2323 /// InitListExpr describes an initializer list, which can be used to
2324 /// initialize objects of different types, including
2325 /// struct/class/union types, arrays, and vectors. For example:
2328 /// struct foo x = { 1, { 2, 3 } };
2331 /// Prior to semantic analysis, an initializer list will represent the
2332 /// initializer list as written by the user, but will have the
2333 /// placeholder type "void". This initializer list is called the
2334 /// syntactic form of the initializer, and may contain C99 designated
2335 /// initializers (represented as DesignatedInitExprs), initializations
2336 /// of subobject members without explicit braces, and so on. Clients
2337 /// interested in the original syntax of the initializer list should
2338 /// use the syntactic form of the initializer list.
2340 /// After semantic analysis, the initializer list will represent the
2341 /// semantic form of the initializer, where the initializations of all
2342 /// subobjects are made explicit with nested InitListExpr nodes and
2343 /// C99 designators have been eliminated by placing the designated
2344 /// initializations into the subobject they initialize. Additionally,
2345 /// any "holes" in the initialization, where no initializer has been
2346 /// specified for a particular subobject, will be replaced with
2347 /// implicitly-generated ImplicitValueInitExpr expressions that
2348 /// value-initialize the subobjects. Note, however, that the
2349 /// initializer lists may still have fewer initializers than there are
2350 /// elements to initialize within the object.
2352 /// Given the semantic form of the initializer list, one can retrieve
2353 /// the original syntactic form of that initializer list (if it
2354 /// exists) using getSyntacticForm(). Since many initializer lists
2355 /// have the same syntactic and semantic forms, getSyntacticForm() may
2356 /// return NULL, indicating that the current initializer list also
2357 /// serves as its syntactic form.
2358 class InitListExpr : public Expr {
2359 // FIXME: Eliminate this vector in favor of ASTContext allocation
2360 std::vector<Stmt *> InitExprs;
2361 SourceLocation LBraceLoc, RBraceLoc;
2363 /// Contains the initializer list that describes the syntactic form
2364 /// written in the source code.
2365 InitListExpr *SyntacticForm;
2367 /// If this initializer list initializes a union, specifies which
2368 /// field within the union will be initialized.
2369 FieldDecl *UnionFieldInit;
2371 /// Whether this initializer list originally had a GNU array-range
2372 /// designator in it. This is a temporary marker used by CodeGen.
2373 bool HadArrayRangeDesignator;
2376 InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
2377 SourceLocation rbraceloc);
2379 /// \brief Build an empty initializer list.
2380 explicit InitListExpr(EmptyShell Empty) : Expr(InitListExprClass, Empty) { }
2382 unsigned getNumInits() const { return InitExprs.size(); }
2384 const Expr* getInit(unsigned Init) const {
2385 assert(Init < getNumInits() && "Initializer access out of range!");
2386 return cast_or_null<Expr>(InitExprs[Init]);
2389 Expr* getInit(unsigned Init) {
2390 assert(Init < getNumInits() && "Initializer access out of range!");
2391 return cast_or_null<Expr>(InitExprs[Init]);
2394 void setInit(unsigned Init, Expr *expr) {
2395 assert(Init < getNumInits() && "Initializer access out of range!");
2396 InitExprs[Init] = expr;
2399 /// \brief Reserve space for some number of initializers.
2400 void reserveInits(unsigned NumInits);
2402 /// @brief Specify the number of initializers
2404 /// If there are more than @p NumInits initializers, the remaining
2405 /// initializers will be destroyed. If there are fewer than @p
2406 /// NumInits initializers, NULL expressions will be added for the
2407 /// unknown initializers.
2408 void resizeInits(ASTContext &Context, unsigned NumInits);
2410 /// @brief Updates the initializer at index @p Init with the new
2411 /// expression @p expr, and returns the old expression at that
2414 /// When @p Init is out of range for this initializer list, the
2415 /// initializer list will be extended with NULL expressions to
2416 /// accomodate the new entry.
2417 Expr *updateInit(unsigned Init, Expr *expr);
2419 /// \brief If this initializes a union, specifies which field in the
2420 /// union to initialize.
2422 /// Typically, this field is the first named field within the
2423 /// union. However, a designated initializer can specify the
2424 /// initialization of a different field within the union.
2425 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; }
2426 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; }
2428 // Explicit InitListExpr's originate from source code (and have valid source
2429 // locations). Implicit InitListExpr's are created by the semantic analyzer.
2431 return LBraceLoc.isValid() && RBraceLoc.isValid();
2434 SourceLocation getLBraceLoc() const { return LBraceLoc; }
2435 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; }
2436 SourceLocation getRBraceLoc() const { return RBraceLoc; }
2437 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; }
2439 /// @brief Retrieve the initializer list that describes the
2440 /// syntactic form of the initializer.
2443 InitListExpr *getSyntacticForm() const { return SyntacticForm; }
2444 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; }
2446 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; }
2447 void sawArrayRangeDesignator(bool ARD = true) {
2448 HadArrayRangeDesignator = ARD;
2451 virtual SourceRange getSourceRange() const {
2452 return SourceRange(LBraceLoc, RBraceLoc);
2454 static bool classof(const Stmt *T) {
2455 return T->getStmtClass() == InitListExprClass;
2457 static bool classof(const InitListExpr *) { return true; }
2460 virtual child_iterator child_begin();
2461 virtual child_iterator child_end();
2463 typedef std::vector<Stmt *>::iterator iterator;
2464 typedef std::vector<Stmt *>::reverse_iterator reverse_iterator;
2466 iterator begin() { return InitExprs.begin(); }
2467 iterator end() { return InitExprs.end(); }
2468 reverse_iterator rbegin() { return InitExprs.rbegin(); }
2469 reverse_iterator rend() { return InitExprs.rend(); }
2472 /// @brief Represents a C99 designated initializer expression.
2474 /// A designated initializer expression (C99 6.7.8) contains one or
2475 /// more designators (which can be field designators, array
2476 /// designators, or GNU array-range designators) followed by an
2477 /// expression that initializes the field or element(s) that the
2478 /// designators refer to. For example, given:
2485 /// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 };
2488 /// The InitListExpr contains three DesignatedInitExprs, the first of
2489 /// which covers @c [2].y=1.0. This DesignatedInitExpr will have two
2490 /// designators, one array designator for @c [2] followed by one field
2491 /// designator for @c .y. The initalization expression will be 1.0.
2492 class DesignatedInitExpr : public Expr {
2494 /// \brief Forward declaration of the Designator class.
2498 /// The location of the '=' or ':' prior to the actual initializer
2500 SourceLocation EqualOrColonLoc;
2502 /// Whether this designated initializer used the GNU deprecated
2503 /// syntax rather than the C99 '=' syntax.
2506 /// The number of designators in this initializer expression.
2507 unsigned NumDesignators : 15;
2509 /// \brief The designators in this designated initialization
2511 Designator *Designators;
2513 /// The number of subexpressions of this initializer expression,
2514 /// which contains both the initializer and any additional
2515 /// expressions used by array and array-range designators.
2516 unsigned NumSubExprs : 16;
2519 DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
2520 const Designator *Designators,
2521 SourceLocation EqualOrColonLoc, bool GNUSyntax,
2522 Expr **IndexExprs, unsigned NumIndexExprs,
2525 explicit DesignatedInitExpr(unsigned NumSubExprs)
2526 : Expr(DesignatedInitExprClass, EmptyShell()),
2527 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { }
2530 virtual void DoDestroy(ASTContext &C);
2533 /// A field designator, e.g., ".x".
2534 struct FieldDesignator {
2535 /// Refers to the field that is being initialized. The low bit
2536 /// of this field determines whether this is actually a pointer
2537 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When
2538 /// initially constructed, a field designator will store an
2539 /// IdentifierInfo*. After semantic analysis has resolved that
2540 /// name, the field designator will instead store a FieldDecl*.
2541 uintptr_t NameOrField;
2543 /// The location of the '.' in the designated initializer.
2546 /// The location of the field name in the designated initializer.
2550 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]".
2551 struct ArrayOrRangeDesignator {
2552 /// Location of the first index expression within the designated
2553 /// initializer expression's list of subexpressions.
2555 /// The location of the '[' starting the array range designator.
2556 unsigned LBracketLoc;
2557 /// The location of the ellipsis separating the start and end
2558 /// indices. Only valid for GNU array-range designators.
2559 unsigned EllipsisLoc;
2560 /// The location of the ']' terminating the array range designator.
2561 unsigned RBracketLoc;
2564 /// @brief Represents a single C99 designator.
2566 /// @todo This class is infuriatingly similar to clang::Designator,
2567 /// but minor differences (storing indices vs. storing pointers)
2568 /// keep us from reusing it. Try harder, later, to rectify these
2571 /// @brief The kind of designator this describes.
2575 ArrayRangeDesignator
2579 /// A field designator, e.g., ".x".
2580 struct FieldDesignator Field;
2581 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]".
2582 struct ArrayOrRangeDesignator ArrayOrRange;
2584 friend class DesignatedInitExpr;
2589 /// @brief Initializes a field designator.
2590 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc,
2591 SourceLocation FieldLoc)
2592 : Kind(FieldDesignator) {
2593 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01;
2594 Field.DotLoc = DotLoc.getRawEncoding();
2595 Field.FieldLoc = FieldLoc.getRawEncoding();
2598 /// @brief Initializes an array designator.
2599 Designator(unsigned Index, SourceLocation LBracketLoc,
2600 SourceLocation RBracketLoc)
2601 : Kind(ArrayDesignator) {
2602 ArrayOrRange.Index = Index;
2603 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding();
2604 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding();
2605 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding();
2608 /// @brief Initializes a GNU array-range designator.
2609 Designator(unsigned Index, SourceLocation LBracketLoc,
2610 SourceLocation EllipsisLoc, SourceLocation RBracketLoc)
2611 : Kind(ArrayRangeDesignator) {
2612 ArrayOrRange.Index = Index;
2613 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding();
2614 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding();
2615 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding();
2618 bool isFieldDesignator() const { return Kind == FieldDesignator; }
2619 bool isArrayDesignator() const { return Kind == ArrayDesignator; }
2620 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
2622 IdentifierInfo * getFieldName();
2624 FieldDecl *getField() {
2625 assert(Kind == FieldDesignator && "Only valid on a field designator");
2626 if (Field.NameOrField & 0x01)
2629 return reinterpret_cast<FieldDecl *>(Field.NameOrField);
2632 void setField(FieldDecl *FD) {
2633 assert(Kind == FieldDesignator && "Only valid on a field designator");
2634 Field.NameOrField = reinterpret_cast<uintptr_t>(FD);
2637 SourceLocation getDotLoc() const {
2638 assert(Kind == FieldDesignator && "Only valid on a field designator");
2639 return SourceLocation::getFromRawEncoding(Field.DotLoc);
2642 SourceLocation getFieldLoc() const {
2643 assert(Kind == FieldDesignator && "Only valid on a field designator");
2644 return SourceLocation::getFromRawEncoding(Field.FieldLoc);
2647 SourceLocation getLBracketLoc() const {
2648 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2649 "Only valid on an array or array-range designator");
2650 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc);
2653 SourceLocation getRBracketLoc() const {
2654 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2655 "Only valid on an array or array-range designator");
2656 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc);
2659 SourceLocation getEllipsisLoc() const {
2660 assert(Kind == ArrayRangeDesignator &&
2661 "Only valid on an array-range designator");
2662 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc);
2665 unsigned getFirstExprIndex() const {
2666 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2667 "Only valid on an array or array-range designator");
2668 return ArrayOrRange.Index;
2671 SourceLocation getStartLocation() const {
2672 if (Kind == FieldDesignator)
2673 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc();
2675 return getLBracketLoc();
2679 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators,
2680 unsigned NumDesignators,
2681 Expr **IndexExprs, unsigned NumIndexExprs,
2682 SourceLocation EqualOrColonLoc,
2683 bool GNUSyntax, Expr *Init);
2685 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs);
2687 /// @brief Returns the number of designators in this initializer.
2688 unsigned size() const { return NumDesignators; }
2690 // Iterator access to the designators.
2691 typedef Designator* designators_iterator;
2692 designators_iterator designators_begin() { return Designators; }
2693 designators_iterator designators_end() {
2694 return Designators + NumDesignators;
2697 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; }
2699 void setDesignators(const Designator *Desigs, unsigned NumDesigs);
2701 Expr *getArrayIndex(const Designator& D);
2702 Expr *getArrayRangeStart(const Designator& D);
2703 Expr *getArrayRangeEnd(const Designator& D);
2705 /// @brief Retrieve the location of the '=' that precedes the
2706 /// initializer value itself, if present.
2707 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; }
2708 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; }
2710 /// @brief Determines whether this designated initializer used the
2711 /// deprecated GNU syntax for designated initializers.
2712 bool usesGNUSyntax() const { return GNUSyntax; }
2713 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; }
2715 /// @brief Retrieve the initializer value.
2716 Expr *getInit() const {
2717 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin());
2720 void setInit(Expr *init) {
2721 *child_begin() = init;
2724 /// \brief Retrieve the total number of subexpressions in this
2725 /// designated initializer expression, including the actual
2726 /// initialized value and any expressions that occur within array
2727 /// and array-range designators.
2728 unsigned getNumSubExprs() const { return NumSubExprs; }
2730 Expr *getSubExpr(unsigned Idx) {
2731 assert(Idx < NumSubExprs && "Subscript out of range");
2732 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2733 Ptr += sizeof(DesignatedInitExpr);
2734 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx];
2737 void setSubExpr(unsigned Idx, Expr *E) {
2738 assert(Idx < NumSubExprs && "Subscript out of range");
2739 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2740 Ptr += sizeof(DesignatedInitExpr);
2741 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E;
2744 /// \brief Replaces the designator at index @p Idx with the series
2745 /// of designators in [First, Last).
2746 void ExpandDesignator(unsigned Idx, const Designator *First,
2747 const Designator *Last);
2749 virtual SourceRange getSourceRange() const;
2751 static bool classof(const Stmt *T) {
2752 return T->getStmtClass() == DesignatedInitExprClass;
2754 static bool classof(const DesignatedInitExpr *) { return true; }
2757 virtual child_iterator child_begin();
2758 virtual child_iterator child_end();
2761 /// \brief Represents an implicitly-generated value initialization of
2762 /// an object of a given type.
2764 /// Implicit value initializations occur within semantic initializer
2765 /// list expressions (InitListExpr) as placeholders for subobject
2766 /// initializations not explicitly specified by the user.
2768 /// \see InitListExpr
2769 class ImplicitValueInitExpr : public Expr {
2771 explicit ImplicitValueInitExpr(QualType ty)
2772 : Expr(ImplicitValueInitExprClass, ty) { }
2774 /// \brief Construct an empty implicit value initialization.
2775 explicit ImplicitValueInitExpr(EmptyShell Empty)
2776 : Expr(ImplicitValueInitExprClass, Empty) { }
2778 static bool classof(const Stmt *T) {
2779 return T->getStmtClass() == ImplicitValueInitExprClass;
2781 static bool classof(const ImplicitValueInitExpr *) { return true; }
2783 virtual SourceRange getSourceRange() const {
2784 return SourceRange();
2788 virtual child_iterator child_begin();
2789 virtual child_iterator child_end();
2793 class ParenListExpr : public Expr {
2796 SourceLocation LParenLoc, RParenLoc;
2799 virtual void DoDestroy(ASTContext& C);
2802 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs,
2803 unsigned numexprs, SourceLocation rparenloc);
2807 /// \brief Build an empty paren list.
2808 //explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { }
2810 unsigned getNumExprs() const { return NumExprs; }
2812 const Expr* getExpr(unsigned Init) const {
2813 assert(Init < getNumExprs() && "Initializer access out of range!");
2814 return cast_or_null<Expr>(Exprs[Init]);
2817 Expr* getExpr(unsigned Init) {
2818 assert(Init < getNumExprs() && "Initializer access out of range!");
2819 return cast_or_null<Expr>(Exprs[Init]);
2822 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); }
2824 SourceLocation getLParenLoc() const { return LParenLoc; }
2825 SourceLocation getRParenLoc() const { return RParenLoc; }
2827 virtual SourceRange getSourceRange() const {
2828 return SourceRange(LParenLoc, RParenLoc);
2830 static bool classof(const Stmt *T) {
2831 return T->getStmtClass() == ParenListExprClass;
2833 static bool classof(const ParenListExpr *) { return true; }
2836 virtual child_iterator child_begin();
2837 virtual child_iterator child_end();
2841 //===----------------------------------------------------------------------===//
2843 //===----------------------------------------------------------------------===//
2846 /// ExtVectorElementExpr - This represents access to specific elements of a
2847 /// vector, and may occur on the left hand side or right hand side. For example
2848 /// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector.
2850 /// Note that the base may have either vector or pointer to vector type, just
2851 /// like a struct field reference.
2853 class ExtVectorElementExpr : public Expr {
2855 IdentifierInfo *Accessor;
2856 SourceLocation AccessorLoc;
2858 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
2860 : Expr(ExtVectorElementExprClass, ty),
2861 Base(base), Accessor(&accessor), AccessorLoc(loc) {}
2863 /// \brief Build an empty vector element expression.
2864 explicit ExtVectorElementExpr(EmptyShell Empty)
2865 : Expr(ExtVectorElementExprClass, Empty) { }
2867 const Expr *getBase() const { return cast<Expr>(Base); }
2868 Expr *getBase() { return cast<Expr>(Base); }
2869 void setBase(Expr *E) { Base = E; }
2871 IdentifierInfo &getAccessor() const { return *Accessor; }
2872 void setAccessor(IdentifierInfo *II) { Accessor = II; }
2874 SourceLocation getAccessorLoc() const { return AccessorLoc; }
2875 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; }
2877 /// getNumElements - Get the number of components being selected.
2878 unsigned getNumElements() const;
2880 /// containsDuplicateElements - Return true if any element access is
2882 bool containsDuplicateElements() const;
2884 /// getEncodedElementAccess - Encode the elements accessed into an llvm
2885 /// aggregate Constant of ConstantInt(s).
2886 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const;
2888 virtual SourceRange getSourceRange() const {
2889 return SourceRange(getBase()->getLocStart(), AccessorLoc);
2892 /// isArrow - Return true if the base expression is a pointer to vector,
2893 /// return false if the base expression is a vector.
2894 bool isArrow() const;
2896 static bool classof(const Stmt *T) {
2897 return T->getStmtClass() == ExtVectorElementExprClass;
2899 static bool classof(const ExtVectorElementExpr *) { return true; }
2902 virtual child_iterator child_begin();
2903 virtual child_iterator child_end();
2907 /// BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
2908 /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body }
2909 class BlockExpr : public Expr {
2911 BlockDecl *TheBlock;
2912 bool HasBlockDeclRefExprs;
2914 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs)
2915 : Expr(BlockExprClass, ty),
2916 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {}
2918 /// \brief Build an empty block expression.
2919 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { }
2921 const BlockDecl *getBlockDecl() const { return TheBlock; }
2922 BlockDecl *getBlockDecl() { return TheBlock; }
2923 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; }
2925 // Convenience functions for probing the underlying BlockDecl.
2926 SourceLocation getCaretLocation() const;
2927 const Stmt *getBody() const;
2930 virtual SourceRange getSourceRange() const {
2931 return SourceRange(getCaretLocation(), getBody()->getLocEnd());
2934 /// getFunctionType - Return the underlying function type for this block.
2935 const FunctionType *getFunctionType() const;
2937 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr
2938 /// inside of the block that reference values outside the block.
2939 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; }
2940 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; }
2942 static bool classof(const Stmt *T) {
2943 return T->getStmtClass() == BlockExprClass;
2945 static bool classof(const BlockExpr *) { return true; }
2948 virtual child_iterator child_begin();
2949 virtual child_iterator child_end();
2952 /// BlockDeclRefExpr - A reference to a declared variable, function,
2954 class BlockDeclRefExpr : public Expr {
2958 bool ConstQualAdded : 1;
2960 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef,
2961 bool constAdded = false) :
2962 Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef),
2963 ConstQualAdded(constAdded) {}
2965 // \brief Build an empty reference to a declared variable in a
2967 explicit BlockDeclRefExpr(EmptyShell Empty)
2968 : Expr(BlockDeclRefExprClass, Empty) { }
2970 ValueDecl *getDecl() { return D; }
2971 const ValueDecl *getDecl() const { return D; }
2972 void setDecl(ValueDecl *VD) { D = VD; }
2974 SourceLocation getLocation() const { return Loc; }
2975 void setLocation(SourceLocation L) { Loc = L; }
2977 virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
2979 bool isByRef() const { return IsByRef; }
2980 void setByRef(bool BR) { IsByRef = BR; }
2982 bool isConstQualAdded() const { return ConstQualAdded; }
2983 void setConstQualAdded(bool C) { ConstQualAdded = C; }
2985 static bool classof(const Stmt *T) {
2986 return T->getStmtClass() == BlockDeclRefExprClass;
2988 static bool classof(const BlockDeclRefExpr *) { return true; }
2991 virtual child_iterator child_begin();
2992 virtual child_iterator child_end();
2995 } // end namespace clang