Expr.h revision 8ecdb65716cd7914ffb2eeee993fa9039fcd31e8
1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// 2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// 3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// The LLVM Compiler Infrastructure 4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// 5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is distributed under the University of Illinois Open Source 6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// License. See LICENSE.TXT for details. 7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// 8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//===----------------------------------------------------------------------===// 9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// 10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file defines the Expr interface and subclasses. 11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// 12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//===----------------------------------------------------------------------===// 13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef LLVM_CLANG_AST_EXPR_H 15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define LLVM_CLANG_AST_EXPR_H 16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "clang/AST/APValue.h" 18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "clang/AST/Stmt.h" 19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "clang/AST/Type.h" 20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "clang/AST/DeclAccessPair.h" 21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "clang/AST/ASTVector.h" 22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "clang/AST/UsuallyTinyPtrVector.h" 23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "llvm/ADT/APSInt.h" 24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "llvm/ADT/APFloat.h" 25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "llvm/ADT/SmallVector.h" 26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "llvm/ADT/StringRef.h" 27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <vector> 28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace clang { 30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class ASTContext; 31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class APValue; 32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class Decl; 33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class IdentifierInfo; 34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class ParmVarDecl; 35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class NamedDecl; 36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class ValueDecl; 37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class BlockDecl; 38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class CXXBaseSpecifier; 39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class CXXOperatorCallExpr; 40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class CXXMemberCallExpr; 41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class TemplateArgumentLoc; 42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath class TemplateArgumentListInfo; 43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// \brief A simple array of base specifiers. 45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtypedef UsuallyTinyPtrVector<const CXXBaseSpecifier> CXXBaseSpecifierArray; 46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// Expr - This represents one expression. Note that Expr's are subclasses of 48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// Stmt. This allows an expression to be transparently used any place a Stmt 49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// is required. 50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// 51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathclass Expr : public Stmt { 52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath QualType TR; 53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathprotected: 55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// TypeDependent - Whether this expression is type-dependent 56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// (C++ [temp.dep.expr]). 57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool TypeDependent : 1; 58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// ValueDependent - Whether this expression is value-dependent 60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// (C++ [temp.dep.constexpr]). 61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool ValueDependent : 1; 62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Expr(StmtClass SC, QualType T, bool TD, bool VD) 64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath : Stmt(SC), TypeDependent(TD), ValueDependent(VD) { 65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath setType(T); 66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Construct an empty expression. 69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { } 70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathpublic: 72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Increases the reference count for this expression. 73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// 74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// Invoke the Retain() operation when this expression 75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// is being shared by another owner. 76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Expr *Retain() { 77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Stmt::Retain(); 78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return this; 79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath QualType getType() const { return TR; } 82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void setType(QualType t) { 83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // In C++, the type of an expression is always adjusted so that it 84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // will not have reference type an expression will never have 85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // reference type (C++ [expr]p6). Use 86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // QualType::getNonReferenceType() to retrieve the non-reference 87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // type. Additionally, inspect Expr::isLvalue to determine whether 88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // an expression that is adjusted in this manner should be 89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // considered an lvalue. 90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath assert((t.isNull() || !t->isReferenceType()) && 91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath "Expressions can't have reference type"); 92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath TR = t; 94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isValueDependent - Determines whether this expression is 97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// value-dependent (C++ [temp.dep.constexpr]). For example, the 98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// array bound of "Chars" in the following example is 99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// value-dependent. 100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// @code 101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// template<int Size, char (&Chars)[Size]> struct meta_string; 102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// @endcode 103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isValueDependent() const { return ValueDependent; } 104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Set whether this expression is value-dependent or not. 106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void setValueDependent(bool VD) { ValueDependent = VD; } 107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isTypeDependent - Determines whether this expression is 109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// type-dependent (C++ [temp.dep.expr]), which means that its type 110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// could change from one template instantiation to the next. For 111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// example, the expressions "x" and "x + y" are type-dependent in 112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// the following code, but "y" is not type-dependent: 113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// @code 114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// template<typename T> 115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// void add(T x, int y) { 116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// x + y; 117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// } 118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// @endcode 119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isTypeDependent() const { return TypeDependent; } 120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Set whether this expression is type-dependent or not. 122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void setTypeDependent(bool TD) { TypeDependent = TD; } 123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 124c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// SourceLocation tokens are not useful in isolation - they are low level 125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// value objects created/interpreted by SourceManager. We assume AST 126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// clients will have a pointer to the respective SourceManager. 127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath virtual SourceRange getSourceRange() const = 0; 128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// getExprLoc - Return the preferred location for the arrow when diagnosing 130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// a problem with a generic expression. 131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath virtual SourceLocation getExprLoc() const { return getLocStart(); } 132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isUnusedResultAWarning - Return true if this immediate expression should 134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// be warned about if the result is unused. If so, fill in Loc and Ranges 135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// with location to warn on and the source range[s] to report with the 136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// warning. 137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, 138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath SourceRange &R2, ASTContext &Ctx) const; 139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or 141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// incomplete type other than void. Nonarray expressions that can be lvalues: 142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - name, where name must be a variable 143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - e[i] 144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - (e), where e must be an lvalue 145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - e.name, where e must be an lvalue 146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - e->name 147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - *e, the type of e cannot be a function type 148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - string-constant 149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - reference type [C++ [expr]] 150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// - b ? x : y, where x and y are lvalues of suitable types [C++] 151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// 152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath enum isLvalueResult { 153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath LV_Valid, 154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath LV_NotObjectType, 155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath LV_IncompleteVoidType, 156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath LV_DuplicateVectorComponents, 157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath LV_InvalidExpression, 158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath LV_MemberFunction, 159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath LV_SubObjCPropertySetting, 160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath LV_ClassTemporary 161c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath }; 162c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath isLvalueResult isLvalue(ASTContext &Ctx) const; 163c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 164c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // Same as above, but excluding checks for non-object and void types in C 165c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath isLvalueResult isLvalueInternal(ASTContext &Ctx) const; 166c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 167c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 168c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// does not have an incomplete type, does not have a const-qualified type, 169c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// and if it is a structure or union, does not have any member (including, 170c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// recursively, any member or element of all contained aggregates or unions) 171c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// with a const-qualified type. 172c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// 173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \param Loc [in] [out] - A source location which *may* be filled 174c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// in with the location of the expression making this a 175c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// non-modifiable lvalue, if specified. 176c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath enum isModifiableLvalueResult { 177c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_Valid, 178c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_NotObjectType, 179c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_IncompleteVoidType, 180c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_DuplicateVectorComponents, 181c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_InvalidExpression, 182c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_LValueCast, // Specialized form of MLV_InvalidExpression. 183c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_IncompleteType, 184c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_ConstQualified, 185c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_ArrayType, 186c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_NotBlockQualified, 187c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_ReadonlyProperty, 188c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_NoSetterProperty, 189c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_MemberFunction, 190c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_SubObjCPropertySetting, 191c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MLV_ClassTemporary 192c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath }; 193c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx, 194c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath SourceLocation *Loc = 0) const; 195c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 196c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief If this expression refers to a bit-field, retrieve the 197c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// declaration of that bit-field. 198c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath FieldDecl *getBitField(); 199c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 200c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath const FieldDecl *getBitField() const { 201c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return const_cast<Expr*>(this)->getBitField(); 202c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 203c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 204c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Returns whether this expression refers to a vector element. 205c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool refersToVectorElement() const; 206c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 207c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isKnownToHaveBooleanValue - Return true if this is an integer expression 208c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// that is known to return 0 or 1. This happens for _Bool/bool expressions 209c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// but also int expressions which are produced by things like comparisons in 210c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// C. 211c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isKnownToHaveBooleanValue() const; 212c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 213c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isIntegerConstantExpr - Return true if this expression is a valid integer 214c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// constant expression, and, if so, return its value in Result. If not a 215c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// valid i-c-e, return false and fill in Loc (if specified) with the location 216c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// of the invalid expression. 217c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 218c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath SourceLocation *Loc = 0, 219c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isEvaluated = true) const; 220c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const { 221c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath llvm::APSInt X; 222c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return isIntegerConstantExpr(X, Ctx, Loc); 223c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 224c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isConstantInitializer - Returns true if this expression is a constant 225c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// initializer, which can be emitted at compile-time. 226c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isConstantInitializer(ASTContext &Ctx) const; 227c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 228c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// EvalResult is a struct with detailed info about an evaluated expression. 229c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath struct EvalResult { 230c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// Val - This is the value the expression can be folded to. 231c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath APValue Val; 232c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 233c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// HasSideEffects - Whether the evaluated expression has side effects. 234c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// For example, (f() && 0) can be folded, but it still has side effects. 235c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool HasSideEffects; 236c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 237c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// Diag - If the expression is unfoldable, then Diag contains a note 238c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret 239c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// position for the error, and DiagExpr is the expression that caused 240c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// the error. 241c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// If the expression is foldable, but not an integer constant expression, 242c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// Diag contains a note diagnostic that describes why it isn't an integer 243c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// constant expression. If the expression *is* an integer constant 244c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// expression, then Diag will be zero. 245c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath unsigned Diag; 246c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath const Expr *DiagExpr; 247c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath SourceLocation DiagLoc; 248c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 249c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {} 250c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath }; 251c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 252c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// Evaluate - Return true if this is a constant which we can fold using 253c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// any crazy technique (that has nothing to do with language standards) that 254c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// we want to. If this function returns true, it returns the folded constant 255c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// in Result. 256c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool Evaluate(EvalResult &Result, ASTContext &Ctx) const; 257c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 258c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// EvaluateAsAny - The same as Evaluate, except that it also succeeds on 259c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// stack based objects. 260c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const; 261c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 262c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// EvaluateAsBooleanCondition - Return true if this is a constant 263c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// which we we can fold and convert to a boolean condition using 264c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// any crazy technique that we want to. 265c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const; 266c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 267c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isEvaluatable - Call Evaluate to see if this expression can be constant 268c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// folded, but discard the result. 269c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isEvaluatable(ASTContext &Ctx) const; 270c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 271c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// HasSideEffects - This routine returns true for all those expressions 272c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// which must be evaluated each time and must not be optimization away 273c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// or evaluated at compile time. Example is a function call, volatile 274c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// variable read. 275c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool HasSideEffects(ASTContext &Ctx) const; 276c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 277c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// EvaluateAsInt - Call Evaluate and return the folded integer. This 278c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// must be called on an expression that constant folds to an integer. 279c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const; 280c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 281c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue 282c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// with link time known address. 283c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const; 284c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 285c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// EvaluateAsAnyLValue - The same as EvaluateAsLValue, except that it 286c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// also succeeds on stack based, immutable address lvalues. 287c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const; 288c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 289c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Enumeration used to describe how \c isNullPointerConstant() 290c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// should cope with value-dependent expressions. 291c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath enum NullPointerConstantValueDependence { 292c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Specifies that the expression should never be value-dependent. 293c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath NPC_NeverValueDependent = 0, 294c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 295c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Specifies that a value-dependent expression of integral or 296c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// dependent type should be considered a null pointer constant. 297c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath NPC_ValueDependentIsNull, 298c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 299c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Specifies that a value-dependent expression should be considered 300c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// to never be a null pointer constant. 301c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath NPC_ValueDependentIsNotNull 302c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath }; 303c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 304c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 305c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// integer constant expression with the value zero, or if this is one that is 306c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// cast to void*. 307c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isNullPointerConstant(ASTContext &Ctx, 308c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath NullPointerConstantValueDependence NPC) const; 309c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 310c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// isOBJCGCCandidate - Return true if this expression may be used in a read/ 311c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// write barrier. 312c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isOBJCGCCandidate(ASTContext &Ctx) const; 313c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 314c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return 315c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// its subexpression. If that subexpression is also a ParenExpr, 316c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// then this method recursively returns its subexpression, and so forth. 317c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// Otherwise, the method returns the current Expr. 318c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Expr *IgnoreParens(); 319c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 320c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 321c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// or CastExprs, returning their operand. 322c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Expr *IgnoreParenCasts(); 323c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 324c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the 325c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// value (including ptr->int casts of the same size). Strip off any 326c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// ParenExpr or CastExprs, returning their operand. 327c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Expr *IgnoreParenNoopCasts(ASTContext &Ctx); 328c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 329c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Determine whether this expression is a default function argument. 330c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// 331c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// Default arguments are implicitly generated in the abstract syntax tree 332c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// by semantic analysis for function calls, object constructions, etc. in 3337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez /// C++. Default arguments are represented by \c CXXDefaultArgExpr nodes; 3347faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez /// this routine also looks through any implicit casts to determine whether 335c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// the expression is a default argument. 336c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isDefaultArgument() const; 337c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 338c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Determine whether this expression directly creates a 339c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// temporary object (of class type). 340c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool isTemporaryObject() const { return getTemporaryObject() != 0; } 341c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 342c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief If this expression directly creates a temporary object of 3437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez /// class type, return the expression that actually constructs that 3447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez /// temporary object. 345c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath const Expr *getTemporaryObject() const; 346c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 347c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath const Expr *IgnoreParens() const { 348c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return const_cast<Expr*>(this)->IgnoreParens(); 349c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 350c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath const Expr *IgnoreParenCasts() const { 351c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return const_cast<Expr*>(this)->IgnoreParenCasts(); 352c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 353c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const { 354c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx); 355c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 356c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 357c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs); 358c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs); 359c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 360c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath static bool classof(const Stmt *T) { 361c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return T->getStmtClass() >= firstExprConstant && 362c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath T->getStmtClass() <= lastExprConstant; 363c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 364c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath static bool classof(const Expr *) { return true; } 365c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}; 366c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 367c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 368c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//===----------------------------------------------------------------------===// 369c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Primary Expressions. 370c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//===----------------------------------------------------------------------===// 371c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 372c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// \brief Represents the qualifier that may precede a C++ name, e.g., the 373c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// "std::" in "std::sort". 374c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct NameQualifier { 375c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief The nested name specifier. 376c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath NestedNameSpecifier *NNS; 377c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 378c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief The source range covered by the nested name specifier. 379c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath SourceRange Range; 380c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}; 381c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 382c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// \brief Represents an explicit template argument list in C++, e.g., 383c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/// the "<int>" in "sort<int>". 384c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct ExplicitTemplateArgumentList { 385c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief The source location of the left angle bracket ('<'); 386c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath SourceLocation LAngleLoc; 387c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 388c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief The source location of the right angle bracket ('>'); 389c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath SourceLocation RAngleLoc; 390c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 391c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief The number of template arguments in TemplateArgs. 392c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// The actual template arguments (if any) are stored after the 393c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// ExplicitTemplateArgumentList structure. 394c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath unsigned NumTemplateArgs; 395c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 396c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Retrieve the template arguments 397c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath TemplateArgumentLoc *getTemplateArgs() { 398c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return reinterpret_cast<TemplateArgumentLoc *> (this + 1); 399c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 400c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 401c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /// \brief Retrieve the template arguments 402c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath const TemplateArgumentLoc *getTemplateArgs() const { 403c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return reinterpret_cast<const TemplateArgumentLoc *> (this + 1); 404c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 405c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 406c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void initializeFrom(const TemplateArgumentListInfo &List); 407c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void copyInto(TemplateArgumentListInfo &List) const; 408c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath static std::size_t sizeFor(const TemplateArgumentListInfo &List); 409}; 410 411/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function, 412/// enum, etc. 413class DeclRefExpr : public Expr { 414 enum { 415 // Flag on DecoratedD that specifies when this declaration reference 416 // expression has a C++ nested-name-specifier. 417 HasQualifierFlag = 0x01, 418 // Flag on DecoratedD that specifies when this declaration reference 419 // expression has an explicit C++ template argument list. 420 HasExplicitTemplateArgumentListFlag = 0x02 421 }; 422 423 // DecoratedD - The declaration that we are referencing, plus two bits to 424 // indicate whether (1) the declaration's name was explicitly qualified and 425 // (2) the declaration's name was followed by an explicit template 426 // argument list. 427 llvm::PointerIntPair<ValueDecl *, 2> DecoratedD; 428 429 // Loc - The location of the declaration name itself. 430 SourceLocation Loc; 431 432 /// \brief Retrieve the qualifier that preceded the declaration name, if any. 433 NameQualifier *getNameQualifier() { 434 if ((DecoratedD.getInt() & HasQualifierFlag) == 0) 435 return 0; 436 437 return reinterpret_cast<NameQualifier *> (this + 1); 438 } 439 440 /// \brief Retrieve the qualifier that preceded the member name, if any. 441 const NameQualifier *getNameQualifier() const { 442 return const_cast<DeclRefExpr *>(this)->getNameQualifier(); 443 } 444 445 /// \brief Retrieve the explicit template argument list that followed the 446 /// member template name, if any. 447 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() { 448 if ((DecoratedD.getInt() & HasExplicitTemplateArgumentListFlag) == 0) 449 return 0; 450 451 if ((DecoratedD.getInt() & HasQualifierFlag) == 0) 452 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 453 454 return reinterpret_cast<ExplicitTemplateArgumentList *>( 455 getNameQualifier() + 1); 456 } 457 458 /// \brief Retrieve the explicit template argument list that followed the 459 /// member template name, if any. 460 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const { 461 return const_cast<DeclRefExpr *>(this)->getExplicitTemplateArgumentList(); 462 } 463 464 DeclRefExpr(NestedNameSpecifier *Qualifier, SourceRange QualifierRange, 465 ValueDecl *D, SourceLocation NameLoc, 466 const TemplateArgumentListInfo *TemplateArgs, 467 QualType T); 468 469protected: 470 /// \brief Computes the type- and value-dependence flags for this 471 /// declaration reference expression. 472 void computeDependence(); 473 474 DeclRefExpr(StmtClass SC, ValueDecl *d, QualType t, SourceLocation l) : 475 Expr(SC, t, false, false), DecoratedD(d, 0), Loc(l) { 476 computeDependence(); 477 } 478 479public: 480 DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) : 481 Expr(DeclRefExprClass, t, false, false), DecoratedD(d, 0), Loc(l) { 482 computeDependence(); 483 } 484 485 /// \brief Construct an empty declaration reference expression. 486 explicit DeclRefExpr(EmptyShell Empty) 487 : Expr(DeclRefExprClass, Empty) { } 488 489 static DeclRefExpr *Create(ASTContext &Context, 490 NestedNameSpecifier *Qualifier, 491 SourceRange QualifierRange, 492 ValueDecl *D, 493 SourceLocation NameLoc, 494 QualType T, 495 const TemplateArgumentListInfo *TemplateArgs = 0); 496 497 ValueDecl *getDecl() { return DecoratedD.getPointer(); } 498 const ValueDecl *getDecl() const { return DecoratedD.getPointer(); } 499 void setDecl(ValueDecl *NewD) { DecoratedD.setPointer(NewD); } 500 501 SourceLocation getLocation() const { return Loc; } 502 void setLocation(SourceLocation L) { Loc = L; } 503 virtual SourceRange getSourceRange() const; 504 505 /// \brief Determine whether this declaration reference was preceded by a 506 /// C++ nested-name-specifier, e.g., \c N::foo. 507 bool hasQualifier() const { return DecoratedD.getInt() & HasQualifierFlag; } 508 509 /// \brief If the name was qualified, retrieves the source range of 510 /// the nested-name-specifier that precedes the name. Otherwise, 511 /// returns an empty source range. 512 SourceRange getQualifierRange() const { 513 if (!hasQualifier()) 514 return SourceRange(); 515 516 return getNameQualifier()->Range; 517 } 518 519 /// \brief If the name was qualified, retrieves the nested-name-specifier 520 /// that precedes the name. Otherwise, returns NULL. 521 NestedNameSpecifier *getQualifier() const { 522 if (!hasQualifier()) 523 return 0; 524 525 return getNameQualifier()->NNS; 526 } 527 528 /// \brief Determines whether this member expression actually had a C++ 529 /// template argument list explicitly specified, e.g., x.f<int>. 530 bool hasExplicitTemplateArgumentList() const { 531 return DecoratedD.getInt() & HasExplicitTemplateArgumentListFlag; 532 } 533 534 /// \brief Copies the template arguments (if present) into the given 535 /// structure. 536 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 537 if (hasExplicitTemplateArgumentList()) 538 getExplicitTemplateArgumentList()->copyInto(List); 539 } 540 541 /// \brief Retrieve the location of the left angle bracket following the 542 /// member name ('<'), if any. 543 SourceLocation getLAngleLoc() const { 544 if (!hasExplicitTemplateArgumentList()) 545 return SourceLocation(); 546 547 return getExplicitTemplateArgumentList()->LAngleLoc; 548 } 549 550 /// \brief Retrieve the template arguments provided as part of this 551 /// template-id. 552 const TemplateArgumentLoc *getTemplateArgs() const { 553 if (!hasExplicitTemplateArgumentList()) 554 return 0; 555 556 return getExplicitTemplateArgumentList()->getTemplateArgs(); 557 } 558 559 /// \brief Retrieve the number of template arguments provided as part of this 560 /// template-id. 561 unsigned getNumTemplateArgs() const { 562 if (!hasExplicitTemplateArgumentList()) 563 return 0; 564 565 return getExplicitTemplateArgumentList()->NumTemplateArgs; 566 } 567 568 /// \brief Retrieve the location of the right angle bracket following the 569 /// template arguments ('>'). 570 SourceLocation getRAngleLoc() const { 571 if (!hasExplicitTemplateArgumentList()) 572 return SourceLocation(); 573 574 return getExplicitTemplateArgumentList()->RAngleLoc; 575 } 576 577 static bool classof(const Stmt *T) { 578 return T->getStmtClass() == DeclRefExprClass; 579 } 580 static bool classof(const DeclRefExpr *) { return true; } 581 582 // Iterators 583 virtual child_iterator child_begin(); 584 virtual child_iterator child_end(); 585}; 586 587/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__. 588class PredefinedExpr : public Expr { 589public: 590 enum IdentType { 591 Func, 592 Function, 593 PrettyFunction, 594 /// PrettyFunctionNoVirtual - The same as PrettyFunction, except that the 595 /// 'virtual' keyword is omitted for virtual member functions. 596 PrettyFunctionNoVirtual 597 }; 598 599private: 600 SourceLocation Loc; 601 IdentType Type; 602public: 603 PredefinedExpr(SourceLocation l, QualType type, IdentType IT) 604 : Expr(PredefinedExprClass, type, type->isDependentType(), 605 type->isDependentType()), Loc(l), Type(IT) {} 606 607 /// \brief Construct an empty predefined expression. 608 explicit PredefinedExpr(EmptyShell Empty) 609 : Expr(PredefinedExprClass, Empty) { } 610 611 IdentType getIdentType() const { return Type; } 612 void setIdentType(IdentType IT) { Type = IT; } 613 614 SourceLocation getLocation() const { return Loc; } 615 void setLocation(SourceLocation L) { Loc = L; } 616 617 static std::string ComputeName(IdentType IT, const Decl *CurrentDecl); 618 619 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 620 621 static bool classof(const Stmt *T) { 622 return T->getStmtClass() == PredefinedExprClass; 623 } 624 static bool classof(const PredefinedExpr *) { return true; } 625 626 // Iterators 627 virtual child_iterator child_begin(); 628 virtual child_iterator child_end(); 629}; 630 631class IntegerLiteral : public Expr { 632 llvm::APInt Value; 633 SourceLocation Loc; 634public: 635 // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, 636 // or UnsignedLongLongTy 637 IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l) 638 : Expr(IntegerLiteralClass, type, false, false), Value(V), Loc(l) { 639 assert(type->isIntegerType() && "Illegal type in IntegerLiteral"); 640 } 641 642 /// \brief Construct an empty integer literal. 643 explicit IntegerLiteral(EmptyShell Empty) 644 : Expr(IntegerLiteralClass, Empty) { } 645 646 const llvm::APInt &getValue() const { return Value; } 647 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 648 649 /// \brief Retrieve the location of the literal. 650 SourceLocation getLocation() const { return Loc; } 651 652 void setValue(const llvm::APInt &Val) { Value = Val; } 653 void setLocation(SourceLocation Location) { Loc = Location; } 654 655 static bool classof(const Stmt *T) { 656 return T->getStmtClass() == IntegerLiteralClass; 657 } 658 static bool classof(const IntegerLiteral *) { return true; } 659 660 // Iterators 661 virtual child_iterator child_begin(); 662 virtual child_iterator child_end(); 663}; 664 665class CharacterLiteral : public Expr { 666 unsigned Value; 667 SourceLocation Loc; 668 bool IsWide; 669public: 670 // type should be IntTy 671 CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l) 672 : Expr(CharacterLiteralClass, type, false, false), Value(value), Loc(l), 673 IsWide(iswide) { 674 } 675 676 /// \brief Construct an empty character literal. 677 CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { } 678 679 SourceLocation getLocation() const { return Loc; } 680 bool isWide() const { return IsWide; } 681 682 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 683 684 unsigned getValue() const { return Value; } 685 686 void setLocation(SourceLocation Location) { Loc = Location; } 687 void setWide(bool W) { IsWide = W; } 688 void setValue(unsigned Val) { Value = Val; } 689 690 static bool classof(const Stmt *T) { 691 return T->getStmtClass() == CharacterLiteralClass; 692 } 693 static bool classof(const CharacterLiteral *) { return true; } 694 695 // Iterators 696 virtual child_iterator child_begin(); 697 virtual child_iterator child_end(); 698}; 699 700class FloatingLiteral : public Expr { 701 llvm::APFloat Value; 702 bool IsExact : 1; 703 SourceLocation Loc; 704public: 705 FloatingLiteral(const llvm::APFloat &V, bool isexact, 706 QualType Type, SourceLocation L) 707 : Expr(FloatingLiteralClass, Type, false, false), Value(V), 708 IsExact(isexact), Loc(L) {} 709 710 /// \brief Construct an empty floating-point literal. 711 explicit FloatingLiteral(EmptyShell Empty) 712 : Expr(FloatingLiteralClass, Empty), Value(0.0) { } 713 714 const llvm::APFloat &getValue() const { return Value; } 715 void setValue(const llvm::APFloat &Val) { Value = Val; } 716 717 bool isExact() const { return IsExact; } 718 void setExact(bool E) { IsExact = E; } 719 720 /// getValueAsApproximateDouble - This returns the value as an inaccurate 721 /// double. Note that this may cause loss of precision, but is useful for 722 /// debugging dumps, etc. 723 double getValueAsApproximateDouble() const; 724 725 SourceLocation getLocation() const { return Loc; } 726 void setLocation(SourceLocation L) { Loc = L; } 727 728 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 729 730 static bool classof(const Stmt *T) { 731 return T->getStmtClass() == FloatingLiteralClass; 732 } 733 static bool classof(const FloatingLiteral *) { return true; } 734 735 // Iterators 736 virtual child_iterator child_begin(); 737 virtual child_iterator child_end(); 738}; 739 740/// ImaginaryLiteral - We support imaginary integer and floating point literals, 741/// like "1.0i". We represent these as a wrapper around FloatingLiteral and 742/// IntegerLiteral classes. Instances of this class always have a Complex type 743/// whose element type matches the subexpression. 744/// 745class ImaginaryLiteral : public Expr { 746 Stmt *Val; 747public: 748 ImaginaryLiteral(Expr *val, QualType Ty) 749 : Expr(ImaginaryLiteralClass, Ty, false, false), Val(val) {} 750 751 /// \brief Build an empty imaginary literal. 752 explicit ImaginaryLiteral(EmptyShell Empty) 753 : Expr(ImaginaryLiteralClass, Empty) { } 754 755 const Expr *getSubExpr() const { return cast<Expr>(Val); } 756 Expr *getSubExpr() { return cast<Expr>(Val); } 757 void setSubExpr(Expr *E) { Val = E; } 758 759 virtual SourceRange getSourceRange() const { return Val->getSourceRange(); } 760 static bool classof(const Stmt *T) { 761 return T->getStmtClass() == ImaginaryLiteralClass; 762 } 763 static bool classof(const ImaginaryLiteral *) { return true; } 764 765 // Iterators 766 virtual child_iterator child_begin(); 767 virtual child_iterator child_end(); 768}; 769 770/// StringLiteral - This represents a string literal expression, e.g. "foo" 771/// or L"bar" (wide strings). The actual string is returned by getStrData() 772/// is NOT null-terminated, and the length of the string is determined by 773/// calling getByteLength(). The C type for a string is always a 774/// ConstantArrayType. In C++, the char type is const qualified, in C it is 775/// not. 776/// 777/// Note that strings in C can be formed by concatenation of multiple string 778/// literal pptokens in translation phase #6. This keeps track of the locations 779/// of each of these pieces. 780/// 781/// Strings in C can also be truncated and extended by assigning into arrays, 782/// e.g. with constructs like: 783/// char X[2] = "foobar"; 784/// In this case, getByteLength() will return 6, but the string literal will 785/// have type "char[2]". 786class StringLiteral : public Expr { 787 const char *StrData; 788 unsigned ByteLength; 789 bool IsWide; 790 unsigned NumConcatenated; 791 SourceLocation TokLocs[1]; 792 793 StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty, false, false) {} 794 795protected: 796 virtual void DoDestroy(ASTContext &C); 797 798public: 799 /// This is the "fully general" constructor that allows representation of 800 /// strings formed from multiple concatenated tokens. 801 static StringLiteral *Create(ASTContext &C, const char *StrData, 802 unsigned ByteLength, bool Wide, QualType Ty, 803 const SourceLocation *Loc, unsigned NumStrs); 804 805 /// Simple constructor for string literals made from one token. 806 static StringLiteral *Create(ASTContext &C, const char *StrData, 807 unsigned ByteLength, 808 bool Wide, QualType Ty, SourceLocation Loc) { 809 return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1); 810 } 811 812 /// \brief Construct an empty string literal. 813 static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs); 814 815 llvm::StringRef getString() const { 816 return llvm::StringRef(StrData, ByteLength); 817 } 818 // FIXME: These are deprecated, replace with StringRef. 819 const char *getStrData() const { return StrData; } 820 unsigned getByteLength() const { return ByteLength; } 821 822 /// \brief Sets the string data to the given string data. 823 void setString(ASTContext &C, llvm::StringRef Str); 824 825 bool isWide() const { return IsWide; } 826 void setWide(bool W) { IsWide = W; } 827 828 bool containsNonAsciiOrNull() const { 829 llvm::StringRef Str = getString(); 830 for (unsigned i = 0, e = Str.size(); i != e; ++i) 831 if (!isascii(Str[i]) || !Str[i]) 832 return true; 833 return false; 834 } 835 /// getNumConcatenated - Get the number of string literal tokens that were 836 /// concatenated in translation phase #6 to form this string literal. 837 unsigned getNumConcatenated() const { return NumConcatenated; } 838 839 SourceLocation getStrTokenLoc(unsigned TokNum) const { 840 assert(TokNum < NumConcatenated && "Invalid tok number"); 841 return TokLocs[TokNum]; 842 } 843 void setStrTokenLoc(unsigned TokNum, SourceLocation L) { 844 assert(TokNum < NumConcatenated && "Invalid tok number"); 845 TokLocs[TokNum] = L; 846 } 847 848 typedef const SourceLocation *tokloc_iterator; 849 tokloc_iterator tokloc_begin() const { return TokLocs; } 850 tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; } 851 852 virtual SourceRange getSourceRange() const { 853 return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]); 854 } 855 static bool classof(const Stmt *T) { 856 return T->getStmtClass() == StringLiteralClass; 857 } 858 static bool classof(const StringLiteral *) { return true; } 859 860 // Iterators 861 virtual child_iterator child_begin(); 862 virtual child_iterator child_end(); 863}; 864 865/// ParenExpr - This represents a parethesized expression, e.g. "(1)". This 866/// AST node is only formed if full location information is requested. 867class ParenExpr : public Expr { 868 SourceLocation L, R; 869 Stmt *Val; 870public: 871 ParenExpr(SourceLocation l, SourceLocation r, Expr *val) 872 : Expr(ParenExprClass, val->getType(), 873 val->isTypeDependent(), val->isValueDependent()), 874 L(l), R(r), Val(val) {} 875 876 /// \brief Construct an empty parenthesized expression. 877 explicit ParenExpr(EmptyShell Empty) 878 : Expr(ParenExprClass, Empty) { } 879 880 const Expr *getSubExpr() const { return cast<Expr>(Val); } 881 Expr *getSubExpr() { return cast<Expr>(Val); } 882 void setSubExpr(Expr *E) { Val = E; } 883 884 virtual SourceRange getSourceRange() const { return SourceRange(L, R); } 885 886 /// \brief Get the location of the left parentheses '('. 887 SourceLocation getLParen() const { return L; } 888 void setLParen(SourceLocation Loc) { L = Loc; } 889 890 /// \brief Get the location of the right parentheses ')'. 891 SourceLocation getRParen() const { return R; } 892 void setRParen(SourceLocation Loc) { R = Loc; } 893 894 static bool classof(const Stmt *T) { 895 return T->getStmtClass() == ParenExprClass; 896 } 897 static bool classof(const ParenExpr *) { return true; } 898 899 // Iterators 900 virtual child_iterator child_begin(); 901 virtual child_iterator child_end(); 902}; 903 904 905/// UnaryOperator - This represents the unary-expression's (except sizeof and 906/// alignof), the postinc/postdec operators from postfix-expression, and various 907/// extensions. 908/// 909/// Notes on various nodes: 910/// 911/// Real/Imag - These return the real/imag part of a complex operand. If 912/// applied to a non-complex value, the former returns its operand and the 913/// later returns zero in the type of the operand. 914/// 915/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose 916/// subexpression is a compound literal with the various MemberExpr and 917/// ArraySubscriptExpr's applied to it. (This is only used in C) 918/// 919class UnaryOperator : public Expr { 920public: 921 // Note that additions to this should also update the StmtVisitor class. 922 enum Opcode { 923 PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators 924 PreInc, PreDec, // [C99 6.5.3.1] Prefix increment and decrement operators. 925 AddrOf, Deref, // [C99 6.5.3.2] Address and indirection operators. 926 Plus, Minus, // [C99 6.5.3.3] Unary arithmetic operators. 927 Not, LNot, // [C99 6.5.3.3] Unary arithmetic operators. 928 Real, Imag, // "__real expr"/"__imag expr" Extension. 929 Extension, // __extension__ marker. 930 OffsetOf // __builtin_offsetof 931 }; 932private: 933 Stmt *Val; 934 Opcode Opc; 935 SourceLocation Loc; 936public: 937 938 UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l) 939 : Expr(UnaryOperatorClass, type, 940 input->isTypeDependent() && opc != OffsetOf, 941 input->isValueDependent()), 942 Val(input), Opc(opc), Loc(l) {} 943 944 /// \brief Build an empty unary operator. 945 explicit UnaryOperator(EmptyShell Empty) 946 : Expr(UnaryOperatorClass, Empty), Opc(AddrOf) { } 947 948 Opcode getOpcode() const { return Opc; } 949 void setOpcode(Opcode O) { Opc = O; } 950 951 Expr *getSubExpr() const { return cast<Expr>(Val); } 952 void setSubExpr(Expr *E) { Val = E; } 953 954 /// getOperatorLoc - Return the location of the operator. 955 SourceLocation getOperatorLoc() const { return Loc; } 956 void setOperatorLoc(SourceLocation L) { Loc = L; } 957 958 /// isPostfix - Return true if this is a postfix operation, like x++. 959 static bool isPostfix(Opcode Op) { 960 return Op == PostInc || Op == PostDec; 961 } 962 963 /// isPostfix - Return true if this is a prefix operation, like --x. 964 static bool isPrefix(Opcode Op) { 965 return Op == PreInc || Op == PreDec; 966 } 967 968 bool isPrefix() const { return isPrefix(Opc); } 969 bool isPostfix() const { return isPostfix(Opc); } 970 bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; } 971 bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; } 972 bool isOffsetOfOp() const { return Opc == OffsetOf; } 973 static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; } 974 bool isArithmeticOp() const { return isArithmeticOp(Opc); } 975 976 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 977 /// corresponds to, e.g. "sizeof" or "[pre]++" 978 static const char *getOpcodeStr(Opcode Op); 979 980 /// \brief Retrieve the unary opcode that corresponds to the given 981 /// overloaded operator. 982 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); 983 984 /// \brief Retrieve the overloaded operator kind that corresponds to 985 /// the given unary opcode. 986 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 987 988 virtual SourceRange getSourceRange() const { 989 if (isPostfix()) 990 return SourceRange(Val->getLocStart(), Loc); 991 else 992 return SourceRange(Loc, Val->getLocEnd()); 993 } 994 virtual SourceLocation getExprLoc() const { return Loc; } 995 996 static bool classof(const Stmt *T) { 997 return T->getStmtClass() == UnaryOperatorClass; 998 } 999 static bool classof(const UnaryOperator *) { return true; } 1000 1001 // Iterators 1002 virtual child_iterator child_begin(); 1003 virtual child_iterator child_end(); 1004}; 1005 1006/// OffsetOfExpr - [C99 7.17] - This represents an expression of the form 1007/// offsetof(record-type, member-designator). For example, given: 1008/// @code 1009/// struct S { 1010/// float f; 1011/// double d; 1012/// }; 1013/// struct T { 1014/// int i; 1015/// struct S s[10]; 1016/// }; 1017/// @endcode 1018/// we can represent and evaluate the expression @c offsetof(struct T, s[2].d). 1019 1020class OffsetOfExpr : public Expr { 1021public: 1022 // __builtin_offsetof(type, identifier(.identifier|[expr])*) 1023 class OffsetOfNode { 1024 public: 1025 /// \brief The kind of offsetof node we have. 1026 enum Kind { 1027 Array = 0x00, 1028 Field = 0x01, 1029 Identifier = 0x02 1030 }; 1031 1032 private: 1033 enum { MaskBits = 2, Mask = 0x03 }; 1034 1035 /// \brief The source range that covers this part of the designator. 1036 SourceRange Range; 1037 1038 /// \brief The data describing the designator, which comes in three 1039 /// different forms, depending on the lower two bits. 1040 /// - An unsigned index into the array of Expr*'s stored after this node 1041 /// in memory, for [constant-expression] designators. 1042 /// - A FieldDecl*, for references to a known field. 1043 /// - An IdentifierInfo*, for references to a field with a given name 1044 /// when the class type is dependent. 1045 uintptr_t Data; 1046 1047 public: 1048 /// \brief Create an offsetof node that refers to an array element. 1049 OffsetOfNode(SourceLocation LBracketLoc, unsigned Index, 1050 SourceLocation RBracketLoc) 1051 : Range(LBracketLoc, RBracketLoc), Data((Index << 2) | Array) { } 1052 1053 /// \brief Create an offsetof node that refers to a field. 1054 OffsetOfNode(SourceLocation DotLoc, FieldDecl *Field, 1055 SourceLocation NameLoc) 1056 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 1057 Data(reinterpret_cast<uintptr_t>(Field) | OffsetOfNode::Field) { } 1058 1059 /// \brief Create an offsetof node that refers to an identifier. 1060 OffsetOfNode(SourceLocation DotLoc, IdentifierInfo *Name, 1061 SourceLocation NameLoc) 1062 : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc), 1063 Data(reinterpret_cast<uintptr_t>(Name) | Identifier) { } 1064 1065 /// \brief Determine what kind of offsetof node this is. 1066 Kind getKind() const { 1067 return static_cast<Kind>(Data & Mask); 1068 } 1069 1070 /// \brief For an array element node, returns the index into the array 1071 /// of expressions. 1072 unsigned getArrayExprIndex() const { 1073 assert(getKind() == Array); 1074 return Data >> 2; 1075 } 1076 1077 /// \brief For a field offsetof node, returns the field. 1078 FieldDecl *getField() const { 1079 assert(getKind() == Field); 1080 return reinterpret_cast<FieldDecl *> (Data & ~(uintptr_t)Mask); 1081 } 1082 1083 /// \brief For a field or identifier offsetof node, returns the name of 1084 /// the field. 1085 IdentifierInfo *getFieldName() const; 1086 1087 /// \brief Retrieve the source range that covers this offsetof node. 1088 /// 1089 /// For an array element node, the source range contains the locations of 1090 /// the square brackets. For a field or identifier node, the source range 1091 /// contains the location of the period (if there is one) and the 1092 /// identifier. 1093 SourceRange getRange() const { return Range; } 1094 }; 1095 1096private: 1097 1098 SourceLocation OperatorLoc, RParenLoc; 1099 // Base type; 1100 TypeSourceInfo *TSInfo; 1101 // Number of sub-components (i.e. instances of OffsetOfNode). 1102 unsigned NumComps; 1103 // Number of sub-expressions (i.e. array subscript expressions). 1104 unsigned NumExprs; 1105 1106 OffsetOfExpr(ASTContext &C, QualType type, 1107 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 1108 OffsetOfNode* compsPtr, unsigned numComps, 1109 Expr** exprsPtr, unsigned numExprs, 1110 SourceLocation RParenLoc); 1111 1112 explicit OffsetOfExpr(unsigned numComps, unsigned numExprs) 1113 : Expr(OffsetOfExprClass, EmptyShell()), 1114 TSInfo(0), NumComps(numComps), NumExprs(numExprs) {} 1115 1116public: 1117 1118 static OffsetOfExpr *Create(ASTContext &C, QualType type, 1119 SourceLocation OperatorLoc, TypeSourceInfo *tsi, 1120 OffsetOfNode* compsPtr, unsigned numComps, 1121 Expr** exprsPtr, unsigned numExprs, 1122 SourceLocation RParenLoc); 1123 1124 static OffsetOfExpr *CreateEmpty(ASTContext &C, 1125 unsigned NumComps, unsigned NumExprs); 1126 1127 /// getOperatorLoc - Return the location of the operator. 1128 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1129 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 1130 1131 /// \brief Return the location of the right parentheses. 1132 SourceLocation getRParenLoc() const { return RParenLoc; } 1133 void setRParenLoc(SourceLocation R) { RParenLoc = R; } 1134 1135 TypeSourceInfo *getTypeSourceInfo() const { 1136 return TSInfo; 1137 } 1138 void setTypeSourceInfo(TypeSourceInfo *tsi) { 1139 TSInfo = tsi; 1140 } 1141 1142 const OffsetOfNode &getComponent(unsigned Idx) { 1143 assert(Idx < NumComps && "Subscript out of range"); 1144 return reinterpret_cast<OffsetOfNode *> (this + 1)[Idx]; 1145 } 1146 1147 void setComponent(unsigned Idx, OffsetOfNode ON) { 1148 assert(Idx < NumComps && "Subscript out of range"); 1149 reinterpret_cast<OffsetOfNode *> (this + 1)[Idx] = ON; 1150 } 1151 1152 unsigned getNumComponents() const { 1153 return NumComps; 1154 } 1155 1156 Expr* getIndexExpr(unsigned Idx) { 1157 assert(Idx < NumExprs && "Subscript out of range"); 1158 return reinterpret_cast<Expr **>( 1159 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx]; 1160 } 1161 1162 void setIndexExpr(unsigned Idx, Expr* E) { 1163 assert(Idx < NumComps && "Subscript out of range"); 1164 reinterpret_cast<Expr **>( 1165 reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx] = E; 1166 } 1167 1168 unsigned getNumExpressions() const { 1169 return NumExprs; 1170 } 1171 1172 virtual SourceRange getSourceRange() const { 1173 return SourceRange(OperatorLoc, RParenLoc); 1174 } 1175 1176 static bool classof(const Stmt *T) { 1177 return T->getStmtClass() == OffsetOfExprClass; 1178 } 1179 1180 static bool classof(const OffsetOfExpr *) { return true; } 1181 1182 // Iterators 1183 virtual child_iterator child_begin(); 1184 virtual child_iterator child_end(); 1185}; 1186 1187/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of 1188/// types and expressions. 1189class SizeOfAlignOfExpr : public Expr { 1190 bool isSizeof : 1; // true if sizeof, false if alignof. 1191 bool isType : 1; // true if operand is a type, false if an expression 1192 union { 1193 TypeSourceInfo *Ty; 1194 Stmt *Ex; 1195 } Argument; 1196 SourceLocation OpLoc, RParenLoc; 1197 1198protected: 1199 virtual void DoDestroy(ASTContext& C); 1200 1201public: 1202 SizeOfAlignOfExpr(bool issizeof, TypeSourceInfo *TInfo, 1203 QualType resultType, SourceLocation op, 1204 SourceLocation rp) : 1205 Expr(SizeOfAlignOfExprClass, resultType, 1206 false, // Never type-dependent (C++ [temp.dep.expr]p3). 1207 // Value-dependent if the argument is type-dependent. 1208 TInfo->getType()->isDependentType()), 1209 isSizeof(issizeof), isType(true), OpLoc(op), RParenLoc(rp) { 1210 Argument.Ty = TInfo; 1211 } 1212 1213 SizeOfAlignOfExpr(bool issizeof, Expr *E, 1214 QualType resultType, SourceLocation op, 1215 SourceLocation rp) : 1216 Expr(SizeOfAlignOfExprClass, resultType, 1217 false, // Never type-dependent (C++ [temp.dep.expr]p3). 1218 // Value-dependent if the argument is type-dependent. 1219 E->isTypeDependent()), 1220 isSizeof(issizeof), isType(false), OpLoc(op), RParenLoc(rp) { 1221 Argument.Ex = E; 1222 } 1223 1224 /// \brief Construct an empty sizeof/alignof expression. 1225 explicit SizeOfAlignOfExpr(EmptyShell Empty) 1226 : Expr(SizeOfAlignOfExprClass, Empty) { } 1227 1228 bool isSizeOf() const { return isSizeof; } 1229 void setSizeof(bool S) { isSizeof = S; } 1230 1231 bool isArgumentType() const { return isType; } 1232 QualType getArgumentType() const { 1233 return getArgumentTypeInfo()->getType(); 1234 } 1235 TypeSourceInfo *getArgumentTypeInfo() const { 1236 assert(isArgumentType() && "calling getArgumentType() when arg is expr"); 1237 return Argument.Ty; 1238 } 1239 Expr *getArgumentExpr() { 1240 assert(!isArgumentType() && "calling getArgumentExpr() when arg is type"); 1241 return static_cast<Expr*>(Argument.Ex); 1242 } 1243 const Expr *getArgumentExpr() const { 1244 return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr(); 1245 } 1246 1247 void setArgument(Expr *E) { Argument.Ex = E; isType = false; } 1248 void setArgument(TypeSourceInfo *TInfo) { 1249 Argument.Ty = TInfo; 1250 isType = true; 1251 } 1252 1253 /// Gets the argument type, or the type of the argument expression, whichever 1254 /// is appropriate. 1255 QualType getTypeOfArgument() const { 1256 return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); 1257 } 1258 1259 SourceLocation getOperatorLoc() const { return OpLoc; } 1260 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 1261 1262 SourceLocation getRParenLoc() const { return RParenLoc; } 1263 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1264 1265 virtual SourceRange getSourceRange() const { 1266 return SourceRange(OpLoc, RParenLoc); 1267 } 1268 1269 static bool classof(const Stmt *T) { 1270 return T->getStmtClass() == SizeOfAlignOfExprClass; 1271 } 1272 static bool classof(const SizeOfAlignOfExpr *) { return true; } 1273 1274 // Iterators 1275 virtual child_iterator child_begin(); 1276 virtual child_iterator child_end(); 1277}; 1278 1279//===----------------------------------------------------------------------===// 1280// Postfix Operators. 1281//===----------------------------------------------------------------------===// 1282 1283/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. 1284class ArraySubscriptExpr : public Expr { 1285 enum { LHS, RHS, END_EXPR=2 }; 1286 Stmt* SubExprs[END_EXPR]; 1287 SourceLocation RBracketLoc; 1288public: 1289 ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, 1290 SourceLocation rbracketloc) 1291 : Expr(ArraySubscriptExprClass, t, 1292 lhs->isTypeDependent() || rhs->isTypeDependent(), 1293 lhs->isValueDependent() || rhs->isValueDependent()), 1294 RBracketLoc(rbracketloc) { 1295 SubExprs[LHS] = lhs; 1296 SubExprs[RHS] = rhs; 1297 } 1298 1299 /// \brief Create an empty array subscript expression. 1300 explicit ArraySubscriptExpr(EmptyShell Shell) 1301 : Expr(ArraySubscriptExprClass, Shell) { } 1302 1303 /// An array access can be written A[4] or 4[A] (both are equivalent). 1304 /// - getBase() and getIdx() always present the normalized view: A[4]. 1305 /// In this case getBase() returns "A" and getIdx() returns "4". 1306 /// - getLHS() and getRHS() present the syntactic view. e.g. for 1307 /// 4[A] getLHS() returns "4". 1308 /// Note: Because vector element access is also written A[4] we must 1309 /// predicate the format conversion in getBase and getIdx only on the 1310 /// the type of the RHS, as it is possible for the LHS to be a vector of 1311 /// integer type 1312 Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } 1313 const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 1314 void setLHS(Expr *E) { SubExprs[LHS] = E; } 1315 1316 Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } 1317 const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 1318 void setRHS(Expr *E) { SubExprs[RHS] = E; } 1319 1320 Expr *getBase() { 1321 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1322 } 1323 1324 const Expr *getBase() const { 1325 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS()); 1326 } 1327 1328 Expr *getIdx() { 1329 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1330 } 1331 1332 const Expr *getIdx() const { 1333 return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS()); 1334 } 1335 1336 virtual SourceRange getSourceRange() const { 1337 return SourceRange(getLHS()->getLocStart(), RBracketLoc); 1338 } 1339 1340 SourceLocation getRBracketLoc() const { return RBracketLoc; } 1341 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } 1342 1343 virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); } 1344 1345 static bool classof(const Stmt *T) { 1346 return T->getStmtClass() == ArraySubscriptExprClass; 1347 } 1348 static bool classof(const ArraySubscriptExpr *) { return true; } 1349 1350 // Iterators 1351 virtual child_iterator child_begin(); 1352 virtual child_iterator child_end(); 1353}; 1354 1355 1356/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). 1357/// CallExpr itself represents a normal function call, e.g., "f(x, 2)", 1358/// while its subclasses may represent alternative syntax that (semantically) 1359/// results in a function call. For example, CXXOperatorCallExpr is 1360/// a subclass for overloaded operator calls that use operator syntax, e.g., 1361/// "str1 + str2" to resolve to a function call. 1362class CallExpr : public Expr { 1363 enum { FN=0, ARGS_START=1 }; 1364 Stmt **SubExprs; 1365 unsigned NumArgs; 1366 SourceLocation RParenLoc; 1367 1368protected: 1369 // This version of the constructor is for derived classes. 1370 CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs, 1371 QualType t, SourceLocation rparenloc); 1372 1373 virtual void DoDestroy(ASTContext& C); 1374 1375public: 1376 CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t, 1377 SourceLocation rparenloc); 1378 1379 /// \brief Build an empty call expression. 1380 CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty); 1381 1382 ~CallExpr() {} 1383 1384 const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); } 1385 Expr *getCallee() { return cast<Expr>(SubExprs[FN]); } 1386 void setCallee(Expr *F) { SubExprs[FN] = F; } 1387 1388 Decl *getCalleeDecl(); 1389 const Decl *getCalleeDecl() const { 1390 return const_cast<CallExpr*>(this)->getCalleeDecl(); 1391 } 1392 1393 /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0. 1394 FunctionDecl *getDirectCallee(); 1395 const FunctionDecl *getDirectCallee() const { 1396 return const_cast<CallExpr*>(this)->getDirectCallee(); 1397 } 1398 1399 /// getNumArgs - Return the number of actual arguments to this call. 1400 /// 1401 unsigned getNumArgs() const { return NumArgs; } 1402 1403 /// getArg - Return the specified argument. 1404 Expr *getArg(unsigned Arg) { 1405 assert(Arg < NumArgs && "Arg access out of range!"); 1406 return cast<Expr>(SubExprs[Arg+ARGS_START]); 1407 } 1408 const Expr *getArg(unsigned Arg) const { 1409 assert(Arg < NumArgs && "Arg access out of range!"); 1410 return cast<Expr>(SubExprs[Arg+ARGS_START]); 1411 } 1412 1413 /// setArg - Set the specified argument. 1414 void setArg(unsigned Arg, Expr *ArgExpr) { 1415 assert(Arg < NumArgs && "Arg access out of range!"); 1416 SubExprs[Arg+ARGS_START] = ArgExpr; 1417 } 1418 1419 /// setNumArgs - This changes the number of arguments present in this call. 1420 /// Any orphaned expressions are deleted by this, and any new operands are set 1421 /// to null. 1422 void setNumArgs(ASTContext& C, unsigned NumArgs); 1423 1424 typedef ExprIterator arg_iterator; 1425 typedef ConstExprIterator const_arg_iterator; 1426 1427 arg_iterator arg_begin() { return SubExprs+ARGS_START; } 1428 arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); } 1429 const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; } 1430 const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();} 1431 1432 /// getNumCommas - Return the number of commas that must have been present in 1433 /// this function call. 1434 unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; } 1435 1436 /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 1437 /// not, return 0. 1438 unsigned isBuiltinCall(ASTContext &Context) const; 1439 1440 /// getCallReturnType - Get the return type of the call expr. This is not 1441 /// always the type of the expr itself, if the return type is a reference 1442 /// type. 1443 QualType getCallReturnType() const; 1444 1445 SourceLocation getRParenLoc() const { return RParenLoc; } 1446 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1447 1448 virtual SourceRange getSourceRange() const { 1449 return SourceRange(getCallee()->getLocStart(), RParenLoc); 1450 } 1451 1452 static bool classof(const Stmt *T) { 1453 return T->getStmtClass() == CallExprClass || 1454 T->getStmtClass() == CXXOperatorCallExprClass || 1455 T->getStmtClass() == CXXMemberCallExprClass; 1456 } 1457 static bool classof(const CallExpr *) { return true; } 1458 static bool classof(const CXXOperatorCallExpr *) { return true; } 1459 static bool classof(const CXXMemberCallExpr *) { return true; } 1460 1461 // Iterators 1462 virtual child_iterator child_begin(); 1463 virtual child_iterator child_end(); 1464}; 1465 1466/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. 1467/// 1468class MemberExpr : public Expr { 1469 /// Extra data stored in some member expressions. 1470 struct MemberNameQualifier : public NameQualifier { 1471 DeclAccessPair FoundDecl; 1472 }; 1473 1474 /// Base - the expression for the base pointer or structure references. In 1475 /// X.F, this is "X". 1476 Stmt *Base; 1477 1478 /// MemberDecl - This is the decl being referenced by the field/member name. 1479 /// In X.F, this is the decl referenced by F. 1480 ValueDecl *MemberDecl; 1481 1482 /// MemberLoc - This is the location of the member name. 1483 SourceLocation MemberLoc; 1484 1485 /// IsArrow - True if this is "X->F", false if this is "X.F". 1486 bool IsArrow : 1; 1487 1488 /// \brief True if this member expression used a nested-name-specifier to 1489 /// refer to the member, e.g., "x->Base::f", or found its member via a using 1490 /// declaration. When true, a MemberNameQualifier 1491 /// structure is allocated immediately after the MemberExpr. 1492 bool HasQualifierOrFoundDecl : 1; 1493 1494 /// \brief True if this member expression specified a template argument list 1495 /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList 1496 /// structure (and its TemplateArguments) are allocated immediately after 1497 /// the MemberExpr or, if the member expression also has a qualifier, after 1498 /// the MemberNameQualifier structure. 1499 bool HasExplicitTemplateArgumentList : 1; 1500 1501 /// \brief Retrieve the qualifier that preceded the member name, if any. 1502 MemberNameQualifier *getMemberQualifier() { 1503 assert(HasQualifierOrFoundDecl); 1504 return reinterpret_cast<MemberNameQualifier *> (this + 1); 1505 } 1506 1507 /// \brief Retrieve the qualifier that preceded the member name, if any. 1508 const MemberNameQualifier *getMemberQualifier() const { 1509 return const_cast<MemberExpr *>(this)->getMemberQualifier(); 1510 } 1511 1512 /// \brief Retrieve the explicit template argument list that followed the 1513 /// member template name, if any. 1514 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() { 1515 if (!HasExplicitTemplateArgumentList) 1516 return 0; 1517 1518 if (!HasQualifierOrFoundDecl) 1519 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 1520 1521 return reinterpret_cast<ExplicitTemplateArgumentList *>( 1522 getMemberQualifier() + 1); 1523 } 1524 1525 /// \brief Retrieve the explicit template argument list that followed the 1526 /// member template name, if any. 1527 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const { 1528 return const_cast<MemberExpr *>(this)->getExplicitTemplateArgumentList(); 1529 } 1530 1531public: 1532 MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl, 1533 SourceLocation l, QualType ty) 1534 : Expr(MemberExprClass, ty, 1535 base->isTypeDependent(), base->isValueDependent()), 1536 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow), 1537 HasQualifierOrFoundDecl(false), HasExplicitTemplateArgumentList(false) {} 1538 1539 /// \brief Build an empty member reference expression. 1540 explicit MemberExpr(EmptyShell Empty) 1541 : Expr(MemberExprClass, Empty), HasQualifierOrFoundDecl(false), 1542 HasExplicitTemplateArgumentList(false) { } 1543 1544 static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow, 1545 NestedNameSpecifier *qual, SourceRange qualrange, 1546 ValueDecl *memberdecl, DeclAccessPair founddecl, 1547 SourceLocation l, 1548 const TemplateArgumentListInfo *targs, 1549 QualType ty); 1550 1551 void setBase(Expr *E) { Base = E; } 1552 Expr *getBase() const { return cast<Expr>(Base); } 1553 1554 /// \brief Retrieve the member declaration to which this expression refers. 1555 /// 1556 /// The returned declaration will either be a FieldDecl or (in C++) 1557 /// a CXXMethodDecl. 1558 ValueDecl *getMemberDecl() const { return MemberDecl; } 1559 void setMemberDecl(ValueDecl *D) { MemberDecl = D; } 1560 1561 /// \brief Retrieves the declaration found by lookup. 1562 DeclAccessPair getFoundDecl() const { 1563 if (!HasQualifierOrFoundDecl) 1564 return DeclAccessPair::make(getMemberDecl(), 1565 getMemberDecl()->getAccess()); 1566 return getMemberQualifier()->FoundDecl; 1567 } 1568 1569 /// \brief Determines whether this member expression actually had 1570 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1571 /// x->Base::foo. 1572 bool hasQualifier() const { return getQualifier() != 0; } 1573 1574 /// \brief If the member name was qualified, retrieves the source range of 1575 /// the nested-name-specifier that precedes the member name. Otherwise, 1576 /// returns an empty source range. 1577 SourceRange getQualifierRange() const { 1578 if (!HasQualifierOrFoundDecl) 1579 return SourceRange(); 1580 1581 return getMemberQualifier()->Range; 1582 } 1583 1584 /// \brief If the member name was qualified, retrieves the 1585 /// nested-name-specifier that precedes the member name. Otherwise, returns 1586 /// NULL. 1587 NestedNameSpecifier *getQualifier() const { 1588 if (!HasQualifierOrFoundDecl) 1589 return 0; 1590 1591 return getMemberQualifier()->NNS; 1592 } 1593 1594 /// \brief Determines whether this member expression actually had a C++ 1595 /// template argument list explicitly specified, e.g., x.f<int>. 1596 bool hasExplicitTemplateArgumentList() const { 1597 return HasExplicitTemplateArgumentList; 1598 } 1599 1600 /// \brief Copies the template arguments (if present) into the given 1601 /// structure. 1602 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 1603 if (hasExplicitTemplateArgumentList()) 1604 getExplicitTemplateArgumentList()->copyInto(List); 1605 } 1606 1607 /// \brief Retrieve the location of the left angle bracket following the 1608 /// member name ('<'), if any. 1609 SourceLocation getLAngleLoc() const { 1610 if (!HasExplicitTemplateArgumentList) 1611 return SourceLocation(); 1612 1613 return getExplicitTemplateArgumentList()->LAngleLoc; 1614 } 1615 1616 /// \brief Retrieve the template arguments provided as part of this 1617 /// template-id. 1618 const TemplateArgumentLoc *getTemplateArgs() const { 1619 if (!HasExplicitTemplateArgumentList) 1620 return 0; 1621 1622 return getExplicitTemplateArgumentList()->getTemplateArgs(); 1623 } 1624 1625 /// \brief Retrieve the number of template arguments provided as part of this 1626 /// template-id. 1627 unsigned getNumTemplateArgs() const { 1628 if (!HasExplicitTemplateArgumentList) 1629 return 0; 1630 1631 return getExplicitTemplateArgumentList()->NumTemplateArgs; 1632 } 1633 1634 /// \brief Retrieve the location of the right angle bracket following the 1635 /// template arguments ('>'). 1636 SourceLocation getRAngleLoc() const { 1637 if (!HasExplicitTemplateArgumentList) 1638 return SourceLocation(); 1639 1640 return getExplicitTemplateArgumentList()->RAngleLoc; 1641 } 1642 1643 bool isArrow() const { return IsArrow; } 1644 void setArrow(bool A) { IsArrow = A; } 1645 1646 /// getMemberLoc - Return the location of the "member", in X->F, it is the 1647 /// location of 'F'. 1648 SourceLocation getMemberLoc() const { return MemberLoc; } 1649 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1650 1651 virtual SourceRange getSourceRange() const { 1652 // If we have an implicit base (like a C++ implicit this), 1653 // make sure not to return its location 1654 SourceLocation EndLoc = MemberLoc; 1655 if (HasExplicitTemplateArgumentList) 1656 EndLoc = getRAngleLoc(); 1657 1658 SourceLocation BaseLoc = getBase()->getLocStart(); 1659 if (BaseLoc.isInvalid()) 1660 return SourceRange(MemberLoc, EndLoc); 1661 return SourceRange(BaseLoc, EndLoc); 1662 } 1663 1664 virtual SourceLocation getExprLoc() const { return MemberLoc; } 1665 1666 static bool classof(const Stmt *T) { 1667 return T->getStmtClass() == MemberExprClass; 1668 } 1669 static bool classof(const MemberExpr *) { return true; } 1670 1671 // Iterators 1672 virtual child_iterator child_begin(); 1673 virtual child_iterator child_end(); 1674}; 1675 1676/// CompoundLiteralExpr - [C99 6.5.2.5] 1677/// 1678class CompoundLiteralExpr : public Expr { 1679 /// LParenLoc - If non-null, this is the location of the left paren in a 1680 /// compound literal like "(int){4}". This can be null if this is a 1681 /// synthesized compound expression. 1682 SourceLocation LParenLoc; 1683 1684 /// The type as written. This can be an incomplete array type, in 1685 /// which case the actual expression type will be different. 1686 TypeSourceInfo *TInfo; 1687 Stmt *Init; 1688 bool FileScope; 1689public: 1690 // FIXME: Can compound literals be value-dependent? 1691 CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo, 1692 QualType T, Expr *init, bool fileScope) 1693 : Expr(CompoundLiteralExprClass, T, 1694 tinfo->getType()->isDependentType(), false), 1695 LParenLoc(lparenloc), TInfo(tinfo), Init(init), FileScope(fileScope) {} 1696 1697 /// \brief Construct an empty compound literal. 1698 explicit CompoundLiteralExpr(EmptyShell Empty) 1699 : Expr(CompoundLiteralExprClass, Empty) { } 1700 1701 const Expr *getInitializer() const { return cast<Expr>(Init); } 1702 Expr *getInitializer() { return cast<Expr>(Init); } 1703 void setInitializer(Expr *E) { Init = E; } 1704 1705 bool isFileScope() const { return FileScope; } 1706 void setFileScope(bool FS) { FileScope = FS; } 1707 1708 SourceLocation getLParenLoc() const { return LParenLoc; } 1709 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1710 1711 TypeSourceInfo *getTypeSourceInfo() const { return TInfo; } 1712 void setTypeSourceInfo(TypeSourceInfo* tinfo) { TInfo = tinfo; } 1713 1714 virtual SourceRange getSourceRange() const { 1715 // FIXME: Init should never be null. 1716 if (!Init) 1717 return SourceRange(); 1718 if (LParenLoc.isInvalid()) 1719 return Init->getSourceRange(); 1720 return SourceRange(LParenLoc, Init->getLocEnd()); 1721 } 1722 1723 static bool classof(const Stmt *T) { 1724 return T->getStmtClass() == CompoundLiteralExprClass; 1725 } 1726 static bool classof(const CompoundLiteralExpr *) { return true; } 1727 1728 // Iterators 1729 virtual child_iterator child_begin(); 1730 virtual child_iterator child_end(); 1731}; 1732 1733/// CastExpr - Base class for type casts, including both implicit 1734/// casts (ImplicitCastExpr) and explicit casts that have some 1735/// representation in the source code (ExplicitCastExpr's derived 1736/// classes). 1737class CastExpr : public Expr { 1738public: 1739 /// CastKind - the kind of cast this represents. 1740 enum CastKind { 1741 /// CK_Unknown - Unknown cast kind. 1742 /// FIXME: The goal is to get rid of this and make all casts have a 1743 /// kind so that the AST client doesn't have to try to figure out what's 1744 /// going on. 1745 CK_Unknown, 1746 1747 /// CK_BitCast - Used for reinterpret_cast. 1748 CK_BitCast, 1749 1750 /// CK_NoOp - Used for const_cast. 1751 CK_NoOp, 1752 1753 /// CK_BaseToDerived - Base to derived class casts. 1754 CK_BaseToDerived, 1755 1756 /// CK_DerivedToBase - Derived to base class casts. 1757 CK_DerivedToBase, 1758 1759 /// CK_UncheckedDerivedToBase - Derived to base class casts that 1760 /// assume that the derived pointer is not null. 1761 CK_UncheckedDerivedToBase, 1762 1763 /// CK_Dynamic - Dynamic cast. 1764 CK_Dynamic, 1765 1766 /// CK_ToUnion - Cast to union (GCC extension). 1767 CK_ToUnion, 1768 1769 /// CK_ArrayToPointerDecay - Array to pointer decay. 1770 CK_ArrayToPointerDecay, 1771 1772 // CK_FunctionToPointerDecay - Function to pointer decay. 1773 CK_FunctionToPointerDecay, 1774 1775 /// CK_NullToMemberPointer - Null pointer to member pointer. 1776 CK_NullToMemberPointer, 1777 1778 /// CK_BaseToDerivedMemberPointer - Member pointer in base class to 1779 /// member pointer in derived class. 1780 CK_BaseToDerivedMemberPointer, 1781 1782 /// CK_DerivedToBaseMemberPointer - Member pointer in derived class to 1783 /// member pointer in base class. 1784 CK_DerivedToBaseMemberPointer, 1785 1786 /// CK_UserDefinedConversion - Conversion using a user defined type 1787 /// conversion function. 1788 CK_UserDefinedConversion, 1789 1790 /// CK_ConstructorConversion - Conversion by constructor 1791 CK_ConstructorConversion, 1792 1793 /// CK_IntegralToPointer - Integral to pointer 1794 CK_IntegralToPointer, 1795 1796 /// CK_PointerToIntegral - Pointer to integral 1797 CK_PointerToIntegral, 1798 1799 /// CK_ToVoid - Cast to void. 1800 CK_ToVoid, 1801 1802 /// CK_VectorSplat - Casting from an integer/floating type to an extended 1803 /// vector type with the same element type as the src type. Splats the 1804 /// src expression into the destination expression. 1805 CK_VectorSplat, 1806 1807 /// CK_IntegralCast - Casting between integral types of different size. 1808 CK_IntegralCast, 1809 1810 /// CK_IntegralToFloating - Integral to floating point. 1811 CK_IntegralToFloating, 1812 1813 /// CK_FloatingToIntegral - Floating point to integral. 1814 CK_FloatingToIntegral, 1815 1816 /// CK_FloatingCast - Casting between floating types of different size. 1817 CK_FloatingCast, 1818 1819 /// CK_MemberPointerToBoolean - Member pointer to boolean 1820 CK_MemberPointerToBoolean, 1821 1822 /// CK_AnyPointerToObjCPointerCast - Casting any pointer to objective-c 1823 /// pointer 1824 CK_AnyPointerToObjCPointerCast, 1825 /// CK_AnyPointerToBlockPointerCast - Casting any pointer to block 1826 /// pointer 1827 CK_AnyPointerToBlockPointerCast 1828 1829 }; 1830 1831private: 1832 CastKind Kind; 1833 Stmt *Op; 1834 1835 /// BasePath - For derived-to-base and base-to-derived casts, the base array 1836 /// contains the inheritance path. 1837 CXXBaseSpecifierArray BasePath; 1838 1839 void CheckBasePath() const { 1840#ifndef NDEBUG 1841 switch (getCastKind()) { 1842 case CK_DerivedToBase: 1843 case CK_UncheckedDerivedToBase: 1844 case CK_DerivedToBaseMemberPointer: 1845 case CK_BaseToDerived: 1846 case CK_BaseToDerivedMemberPointer: 1847 assert(!BasePath.empty() && "Cast kind should have a base path!"); 1848 break; 1849 1850 // These should not have an inheritance path. 1851 case CK_Unknown: 1852 case CK_BitCast: 1853 case CK_NoOp: 1854 case CK_Dynamic: 1855 case CK_ToUnion: 1856 case CK_ArrayToPointerDecay: 1857 case CK_FunctionToPointerDecay: 1858 case CK_NullToMemberPointer: 1859 case CK_UserDefinedConversion: 1860 case CK_ConstructorConversion: 1861 case CK_IntegralToPointer: 1862 case CK_PointerToIntegral: 1863 case CK_ToVoid: 1864 case CK_VectorSplat: 1865 case CK_IntegralCast: 1866 case CK_IntegralToFloating: 1867 case CK_FloatingToIntegral: 1868 case CK_FloatingCast: 1869 case CK_MemberPointerToBoolean: 1870 case CK_AnyPointerToObjCPointerCast: 1871 case CK_AnyPointerToBlockPointerCast: 1872 assert(BasePath.empty() && "Cast kind should not have a base path!"); 1873 break; 1874 } 1875#endif 1876 } 1877 1878protected: 1879 CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op, 1880 CXXBaseSpecifierArray BasePath) : 1881 Expr(SC, ty, 1882 // Cast expressions are type-dependent if the type is 1883 // dependent (C++ [temp.dep.expr]p3). 1884 ty->isDependentType(), 1885 // Cast expressions are value-dependent if the type is 1886 // dependent or if the subexpression is value-dependent. 1887 ty->isDependentType() || (op && op->isValueDependent())), 1888 Kind(kind), Op(op), BasePath(BasePath) { 1889 CheckBasePath(); 1890 } 1891 1892 /// \brief Construct an empty cast. 1893 CastExpr(StmtClass SC, EmptyShell Empty) 1894 : Expr(SC, Empty) { } 1895 1896 virtual void DoDestroy(ASTContext &C); 1897 1898public: 1899 CastKind getCastKind() const { return Kind; } 1900 void setCastKind(CastKind K) { Kind = K; } 1901 const char *getCastKindName() const; 1902 1903 Expr *getSubExpr() { return cast<Expr>(Op); } 1904 const Expr *getSubExpr() const { return cast<Expr>(Op); } 1905 void setSubExpr(Expr *E) { Op = E; } 1906 1907 /// \brief Retrieve the cast subexpression as it was written in the source 1908 /// code, looking through any implicit casts or other intermediate nodes 1909 /// introduced by semantic analysis. 1910 Expr *getSubExprAsWritten(); 1911 const Expr *getSubExprAsWritten() const { 1912 return const_cast<CastExpr *>(this)->getSubExprAsWritten(); 1913 } 1914 1915 const CXXBaseSpecifierArray& getBasePath() const { return BasePath; } 1916 1917 static bool classof(const Stmt *T) { 1918 StmtClass SC = T->getStmtClass(); 1919 if (SC >= CXXStaticCastExprClass && SC <= CXXFunctionalCastExprClass) 1920 return true; 1921 1922 if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass) 1923 return true; 1924 1925 return false; 1926 } 1927 static bool classof(const CastExpr *) { return true; } 1928 1929 // Iterators 1930 virtual child_iterator child_begin(); 1931 virtual child_iterator child_end(); 1932}; 1933 1934/// ImplicitCastExpr - Allows us to explicitly represent implicit type 1935/// conversions, which have no direct representation in the original 1936/// source code. For example: converting T[]->T*, void f()->void 1937/// (*f)(), float->double, short->int, etc. 1938/// 1939/// In C, implicit casts always produce rvalues. However, in C++, an 1940/// implicit cast whose result is being bound to a reference will be 1941/// an lvalue. For example: 1942/// 1943/// @code 1944/// class Base { }; 1945/// class Derived : public Base { }; 1946/// void f(Derived d) { 1947/// Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base 1948/// } 1949/// @endcode 1950class ImplicitCastExpr : public CastExpr { 1951 /// LvalueCast - Whether this cast produces an lvalue. 1952 bool LvalueCast; 1953 1954public: 1955 ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, 1956 CXXBaseSpecifierArray BasePath, bool Lvalue) 1957 : CastExpr(ImplicitCastExprClass, ty, kind, op, BasePath), 1958 LvalueCast(Lvalue) { } 1959 1960 /// \brief Construct an empty implicit cast. 1961 explicit ImplicitCastExpr(EmptyShell Shell) 1962 : CastExpr(ImplicitCastExprClass, Shell) { } 1963 1964 virtual SourceRange getSourceRange() const { 1965 return getSubExpr()->getSourceRange(); 1966 } 1967 1968 /// isLvalueCast - Whether this cast produces an lvalue. 1969 bool isLvalueCast() const { return LvalueCast; } 1970 1971 /// setLvalueCast - Set whether this cast produces an lvalue. 1972 void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; } 1973 1974 static bool classof(const Stmt *T) { 1975 return T->getStmtClass() == ImplicitCastExprClass; 1976 } 1977 static bool classof(const ImplicitCastExpr *) { return true; } 1978}; 1979 1980/// ExplicitCastExpr - An explicit cast written in the source 1981/// code. 1982/// 1983/// This class is effectively an abstract class, because it provides 1984/// the basic representation of an explicitly-written cast without 1985/// specifying which kind of cast (C cast, functional cast, static 1986/// cast, etc.) was written; specific derived classes represent the 1987/// particular style of cast and its location information. 1988/// 1989/// Unlike implicit casts, explicit cast nodes have two different 1990/// types: the type that was written into the source code, and the 1991/// actual type of the expression as determined by semantic 1992/// analysis. These types may differ slightly. For example, in C++ one 1993/// can cast to a reference type, which indicates that the resulting 1994/// expression will be an lvalue. The reference type, however, will 1995/// not be used as the type of the expression. 1996class ExplicitCastExpr : public CastExpr { 1997 /// TInfo - Source type info for the (written) type 1998 /// this expression is casting to. 1999 TypeSourceInfo *TInfo; 2000 2001protected: 2002 ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind, 2003 Expr *op, CXXBaseSpecifierArray BasePath, 2004 TypeSourceInfo *writtenTy) 2005 : CastExpr(SC, exprTy, kind, op, BasePath), TInfo(writtenTy) {} 2006 2007 /// \brief Construct an empty explicit cast. 2008 ExplicitCastExpr(StmtClass SC, EmptyShell Shell) 2009 : CastExpr(SC, Shell) { } 2010 2011public: 2012 /// getTypeInfoAsWritten - Returns the type source info for the type 2013 /// that this expression is casting to. 2014 TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; } 2015 void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; } 2016 2017 /// getTypeAsWritten - Returns the type that this expression is 2018 /// casting to, as written in the source code. 2019 QualType getTypeAsWritten() const { return TInfo->getType(); } 2020 2021 static bool classof(const Stmt *T) { 2022 StmtClass SC = T->getStmtClass(); 2023 if (SC >= CStyleCastExprClass && SC <= CStyleCastExprClass) 2024 return true; 2025 if (SC >= CXXStaticCastExprClass && SC <= CXXFunctionalCastExprClass) 2026 return true; 2027 2028 return false; 2029 } 2030 static bool classof(const ExplicitCastExpr *) { return true; } 2031}; 2032 2033/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style 2034/// cast in C++ (C++ [expr.cast]), which uses the syntax 2035/// (Type)expr. For example: @c (int)f. 2036class CStyleCastExpr : public ExplicitCastExpr { 2037 SourceLocation LPLoc; // the location of the left paren 2038 SourceLocation RPLoc; // the location of the right paren 2039public: 2040 CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op, 2041 CXXBaseSpecifierArray BasePath, TypeSourceInfo *writtenTy, 2042 SourceLocation l, SourceLocation r) 2043 : ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, BasePath, 2044 writtenTy), LPLoc(l), RPLoc(r) {} 2045 2046 /// \brief Construct an empty C-style explicit cast. 2047 explicit CStyleCastExpr(EmptyShell Shell) 2048 : ExplicitCastExpr(CStyleCastExprClass, Shell) { } 2049 2050 SourceLocation getLParenLoc() const { return LPLoc; } 2051 void setLParenLoc(SourceLocation L) { LPLoc = L; } 2052 2053 SourceLocation getRParenLoc() const { return RPLoc; } 2054 void setRParenLoc(SourceLocation L) { RPLoc = L; } 2055 2056 virtual SourceRange getSourceRange() const { 2057 return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd()); 2058 } 2059 static bool classof(const Stmt *T) { 2060 return T->getStmtClass() == CStyleCastExprClass; 2061 } 2062 static bool classof(const CStyleCastExpr *) { return true; } 2063}; 2064 2065/// \brief A builtin binary operation expression such as "x + y" or "x <= y". 2066/// 2067/// This expression node kind describes a builtin binary operation, 2068/// such as "x + y" for integer values "x" and "y". The operands will 2069/// already have been converted to appropriate types (e.g., by 2070/// performing promotions or conversions). 2071/// 2072/// In C++, where operators may be overloaded, a different kind of 2073/// expression node (CXXOperatorCallExpr) is used to express the 2074/// invocation of an overloaded operator with operator syntax. Within 2075/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is 2076/// used to store an expression "x + y" depends on the subexpressions 2077/// for x and y. If neither x or y is type-dependent, and the "+" 2078/// operator resolves to a built-in operation, BinaryOperator will be 2079/// used to express the computation (x and y may still be 2080/// value-dependent). If either x or y is type-dependent, or if the 2081/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will 2082/// be used to express the computation. 2083class BinaryOperator : public Expr { 2084public: 2085 enum Opcode { 2086 // Operators listed in order of precedence. 2087 // Note that additions to this should also update the StmtVisitor class. 2088 PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators. 2089 Mul, Div, Rem, // [C99 6.5.5] Multiplicative operators. 2090 Add, Sub, // [C99 6.5.6] Additive operators. 2091 Shl, Shr, // [C99 6.5.7] Bitwise shift operators. 2092 LT, GT, LE, GE, // [C99 6.5.8] Relational operators. 2093 EQ, NE, // [C99 6.5.9] Equality operators. 2094 And, // [C99 6.5.10] Bitwise AND operator. 2095 Xor, // [C99 6.5.11] Bitwise XOR operator. 2096 Or, // [C99 6.5.12] Bitwise OR operator. 2097 LAnd, // [C99 6.5.13] Logical AND operator. 2098 LOr, // [C99 6.5.14] Logical OR operator. 2099 Assign, MulAssign,// [C99 6.5.16] Assignment operators. 2100 DivAssign, RemAssign, 2101 AddAssign, SubAssign, 2102 ShlAssign, ShrAssign, 2103 AndAssign, XorAssign, 2104 OrAssign, 2105 Comma // [C99 6.5.17] Comma operator. 2106 }; 2107private: 2108 enum { LHS, RHS, END_EXPR }; 2109 Stmt* SubExprs[END_EXPR]; 2110 Opcode Opc; 2111 SourceLocation OpLoc; 2112public: 2113 2114 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2115 SourceLocation opLoc) 2116 : Expr(BinaryOperatorClass, ResTy, 2117 lhs->isTypeDependent() || rhs->isTypeDependent(), 2118 lhs->isValueDependent() || rhs->isValueDependent()), 2119 Opc(opc), OpLoc(opLoc) { 2120 SubExprs[LHS] = lhs; 2121 SubExprs[RHS] = rhs; 2122 assert(!isCompoundAssignmentOp() && 2123 "Use ArithAssignBinaryOperator for compound assignments"); 2124 } 2125 2126 /// \brief Construct an empty binary operator. 2127 explicit BinaryOperator(EmptyShell Empty) 2128 : Expr(BinaryOperatorClass, Empty), Opc(Comma) { } 2129 2130 SourceLocation getOperatorLoc() const { return OpLoc; } 2131 void setOperatorLoc(SourceLocation L) { OpLoc = L; } 2132 2133 Opcode getOpcode() const { return Opc; } 2134 void setOpcode(Opcode O) { Opc = O; } 2135 2136 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2137 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2138 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2139 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2140 2141 virtual SourceRange getSourceRange() const { 2142 return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd()); 2143 } 2144 2145 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 2146 /// corresponds to, e.g. "<<=". 2147 static const char *getOpcodeStr(Opcode Op); 2148 2149 /// \brief Retrieve the binary opcode that corresponds to the given 2150 /// overloaded operator. 2151 static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); 2152 2153 /// \brief Retrieve the overloaded operator kind that corresponds to 2154 /// the given binary opcode. 2155 static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); 2156 2157 /// predicates to categorize the respective opcodes. 2158 bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; } 2159 bool isAdditiveOp() const { return Opc == Add || Opc == Sub; } 2160 static bool isShiftOp(Opcode Opc) { return Opc == Shl || Opc == Shr; } 2161 bool isShiftOp() const { return isShiftOp(Opc); } 2162 2163 static bool isBitwiseOp(Opcode Opc) { return Opc >= And && Opc <= Or; } 2164 bool isBitwiseOp() const { return isBitwiseOp(Opc); } 2165 2166 static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; } 2167 bool isRelationalOp() const { return isRelationalOp(Opc); } 2168 2169 static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; } 2170 bool isEqualityOp() const { return isEqualityOp(Opc); } 2171 2172 static bool isComparisonOp(Opcode Opc) { return Opc >= LT && Opc <= NE; } 2173 bool isComparisonOp() const { return isComparisonOp(Opc); } 2174 2175 static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; } 2176 bool isLogicalOp() const { return isLogicalOp(Opc); } 2177 2178 bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; } 2179 bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;} 2180 bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; } 2181 2182 static bool classof(const Stmt *S) { 2183 return S->getStmtClass() == BinaryOperatorClass || 2184 S->getStmtClass() == CompoundAssignOperatorClass; 2185 } 2186 static bool classof(const BinaryOperator *) { return true; } 2187 2188 // Iterators 2189 virtual child_iterator child_begin(); 2190 virtual child_iterator child_end(); 2191 2192protected: 2193 BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, 2194 SourceLocation opLoc, bool dead) 2195 : Expr(CompoundAssignOperatorClass, ResTy, 2196 lhs->isTypeDependent() || rhs->isTypeDependent(), 2197 lhs->isValueDependent() || rhs->isValueDependent()), 2198 Opc(opc), OpLoc(opLoc) { 2199 SubExprs[LHS] = lhs; 2200 SubExprs[RHS] = rhs; 2201 } 2202 2203 BinaryOperator(StmtClass SC, EmptyShell Empty) 2204 : Expr(SC, Empty), Opc(MulAssign) { } 2205}; 2206 2207/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep 2208/// track of the type the operation is performed in. Due to the semantics of 2209/// these operators, the operands are promoted, the aritmetic performed, an 2210/// implicit conversion back to the result type done, then the assignment takes 2211/// place. This captures the intermediate type which the computation is done 2212/// in. 2213class CompoundAssignOperator : public BinaryOperator { 2214 QualType ComputationLHSType; 2215 QualType ComputationResultType; 2216public: 2217 CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc, 2218 QualType ResType, QualType CompLHSType, 2219 QualType CompResultType, 2220 SourceLocation OpLoc) 2221 : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true), 2222 ComputationLHSType(CompLHSType), 2223 ComputationResultType(CompResultType) { 2224 assert(isCompoundAssignmentOp() && 2225 "Only should be used for compound assignments"); 2226 } 2227 2228 /// \brief Build an empty compound assignment operator expression. 2229 explicit CompoundAssignOperator(EmptyShell Empty) 2230 : BinaryOperator(CompoundAssignOperatorClass, Empty) { } 2231 2232 // The two computation types are the type the LHS is converted 2233 // to for the computation and the type of the result; the two are 2234 // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). 2235 QualType getComputationLHSType() const { return ComputationLHSType; } 2236 void setComputationLHSType(QualType T) { ComputationLHSType = T; } 2237 2238 QualType getComputationResultType() const { return ComputationResultType; } 2239 void setComputationResultType(QualType T) { ComputationResultType = T; } 2240 2241 static bool classof(const CompoundAssignOperator *) { return true; } 2242 static bool classof(const Stmt *S) { 2243 return S->getStmtClass() == CompoundAssignOperatorClass; 2244 } 2245}; 2246 2247/// ConditionalOperator - The ?: operator. Note that LHS may be null when the 2248/// GNU "missing LHS" extension is in use. 2249/// 2250class ConditionalOperator : public Expr { 2251 enum { COND, LHS, RHS, END_EXPR }; 2252 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2253 SourceLocation QuestionLoc, ColonLoc; 2254public: 2255 ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, 2256 SourceLocation CLoc, Expr *rhs, QualType t) 2257 : Expr(ConditionalOperatorClass, t, 2258 // FIXME: the type of the conditional operator doesn't 2259 // depend on the type of the conditional, but the standard 2260 // seems to imply that it could. File a bug! 2261 ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())), 2262 (cond->isValueDependent() || 2263 (lhs && lhs->isValueDependent()) || 2264 (rhs && rhs->isValueDependent()))), 2265 QuestionLoc(QLoc), 2266 ColonLoc(CLoc) { 2267 SubExprs[COND] = cond; 2268 SubExprs[LHS] = lhs; 2269 SubExprs[RHS] = rhs; 2270 } 2271 2272 /// \brief Build an empty conditional operator. 2273 explicit ConditionalOperator(EmptyShell Empty) 2274 : Expr(ConditionalOperatorClass, Empty) { } 2275 2276 // getCond - Return the expression representing the condition for 2277 // the ?: operator. 2278 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2279 void setCond(Expr *E) { SubExprs[COND] = E; } 2280 2281 // getTrueExpr - Return the subexpression representing the value of the ?: 2282 // expression if the condition evaluates to true. In most cases this value 2283 // will be the same as getLHS() except a GCC extension allows the left 2284 // subexpression to be omitted, and instead of the condition be returned. 2285 // e.g: x ?: y is shorthand for x ? x : y, except that the expression "x" 2286 // is only evaluated once. 2287 Expr *getTrueExpr() const { 2288 return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]); 2289 } 2290 2291 // getTrueExpr - Return the subexpression representing the value of the ?: 2292 // expression if the condition evaluates to false. This is the same as getRHS. 2293 Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } 2294 2295 Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); } 2296 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2297 2298 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2299 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2300 2301 SourceLocation getQuestionLoc() const { return QuestionLoc; } 2302 void setQuestionLoc(SourceLocation L) { QuestionLoc = L; } 2303 2304 SourceLocation getColonLoc() const { return ColonLoc; } 2305 void setColonLoc(SourceLocation L) { ColonLoc = L; } 2306 2307 virtual SourceRange getSourceRange() const { 2308 return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd()); 2309 } 2310 static bool classof(const Stmt *T) { 2311 return T->getStmtClass() == ConditionalOperatorClass; 2312 } 2313 static bool classof(const ConditionalOperator *) { return true; } 2314 2315 // Iterators 2316 virtual child_iterator child_begin(); 2317 virtual child_iterator child_end(); 2318}; 2319 2320/// AddrLabelExpr - The GNU address of label extension, representing &&label. 2321class AddrLabelExpr : public Expr { 2322 SourceLocation AmpAmpLoc, LabelLoc; 2323 LabelStmt *Label; 2324public: 2325 AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L, 2326 QualType t) 2327 : Expr(AddrLabelExprClass, t, false, false), 2328 AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {} 2329 2330 /// \brief Build an empty address of a label expression. 2331 explicit AddrLabelExpr(EmptyShell Empty) 2332 : Expr(AddrLabelExprClass, Empty) { } 2333 2334 SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } 2335 void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } 2336 SourceLocation getLabelLoc() const { return LabelLoc; } 2337 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 2338 2339 virtual SourceRange getSourceRange() const { 2340 return SourceRange(AmpAmpLoc, LabelLoc); 2341 } 2342 2343 LabelStmt *getLabel() const { return Label; } 2344 void setLabel(LabelStmt *S) { Label = S; } 2345 2346 static bool classof(const Stmt *T) { 2347 return T->getStmtClass() == AddrLabelExprClass; 2348 } 2349 static bool classof(const AddrLabelExpr *) { return true; } 2350 2351 // Iterators 2352 virtual child_iterator child_begin(); 2353 virtual child_iterator child_end(); 2354}; 2355 2356/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). 2357/// The StmtExpr contains a single CompoundStmt node, which it evaluates and 2358/// takes the value of the last subexpression. 2359class StmtExpr : public Expr { 2360 Stmt *SubStmt; 2361 SourceLocation LParenLoc, RParenLoc; 2362public: 2363 // FIXME: Does type-dependence need to be computed differently? 2364 StmtExpr(CompoundStmt *substmt, QualType T, 2365 SourceLocation lp, SourceLocation rp) : 2366 Expr(StmtExprClass, T, T->isDependentType(), false), 2367 SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { } 2368 2369 /// \brief Build an empty statement expression. 2370 explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } 2371 2372 CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } 2373 const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } 2374 void setSubStmt(CompoundStmt *S) { SubStmt = S; } 2375 2376 virtual SourceRange getSourceRange() const { 2377 return SourceRange(LParenLoc, RParenLoc); 2378 } 2379 2380 SourceLocation getLParenLoc() const { return LParenLoc; } 2381 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2382 SourceLocation getRParenLoc() const { return RParenLoc; } 2383 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2384 2385 static bool classof(const Stmt *T) { 2386 return T->getStmtClass() == StmtExprClass; 2387 } 2388 static bool classof(const StmtExpr *) { return true; } 2389 2390 // Iterators 2391 virtual child_iterator child_begin(); 2392 virtual child_iterator child_end(); 2393}; 2394 2395/// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p. 2396/// This AST node represents a function that returns 1 if two *types* (not 2397/// expressions) are compatible. The result of this built-in function can be 2398/// used in integer constant expressions. 2399class TypesCompatibleExpr : public Expr { 2400 QualType Type1; 2401 QualType Type2; 2402 SourceLocation BuiltinLoc, RParenLoc; 2403public: 2404 TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc, 2405 QualType t1, QualType t2, SourceLocation RP) : 2406 Expr(TypesCompatibleExprClass, ReturnType, false, false), 2407 Type1(t1), Type2(t2), BuiltinLoc(BLoc), RParenLoc(RP) {} 2408 2409 /// \brief Build an empty __builtin_type_compatible_p expression. 2410 explicit TypesCompatibleExpr(EmptyShell Empty) 2411 : Expr(TypesCompatibleExprClass, Empty) { } 2412 2413 QualType getArgType1() const { return Type1; } 2414 void setArgType1(QualType T) { Type1 = T; } 2415 QualType getArgType2() const { return Type2; } 2416 void setArgType2(QualType T) { Type2 = T; } 2417 2418 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2419 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2420 2421 SourceLocation getRParenLoc() const { return RParenLoc; } 2422 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2423 2424 virtual SourceRange getSourceRange() const { 2425 return SourceRange(BuiltinLoc, RParenLoc); 2426 } 2427 static bool classof(const Stmt *T) { 2428 return T->getStmtClass() == TypesCompatibleExprClass; 2429 } 2430 static bool classof(const TypesCompatibleExpr *) { return true; } 2431 2432 // Iterators 2433 virtual child_iterator child_begin(); 2434 virtual child_iterator child_end(); 2435}; 2436 2437/// ShuffleVectorExpr - clang-specific builtin-in function 2438/// __builtin_shufflevector. 2439/// This AST node represents a operator that does a constant 2440/// shuffle, similar to LLVM's shufflevector instruction. It takes 2441/// two vectors and a variable number of constant indices, 2442/// and returns the appropriately shuffled vector. 2443class ShuffleVectorExpr : public Expr { 2444 SourceLocation BuiltinLoc, RParenLoc; 2445 2446 // SubExprs - the list of values passed to the __builtin_shufflevector 2447 // function. The first two are vectors, and the rest are constant 2448 // indices. The number of values in this list is always 2449 // 2+the number of indices in the vector type. 2450 Stmt **SubExprs; 2451 unsigned NumExprs; 2452 2453protected: 2454 virtual void DoDestroy(ASTContext &C); 2455 2456public: 2457 // FIXME: Can a shufflevector be value-dependent? Does type-dependence need 2458 // to be computed differently? 2459 ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, 2460 QualType Type, SourceLocation BLoc, 2461 SourceLocation RP) : 2462 Expr(ShuffleVectorExprClass, Type, Type->isDependentType(), false), 2463 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr) { 2464 2465 SubExprs = new (C) Stmt*[nexpr]; 2466 for (unsigned i = 0; i < nexpr; i++) 2467 SubExprs[i] = args[i]; 2468 } 2469 2470 /// \brief Build an empty vector-shuffle expression. 2471 explicit ShuffleVectorExpr(EmptyShell Empty) 2472 : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { } 2473 2474 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2475 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2476 2477 SourceLocation getRParenLoc() const { return RParenLoc; } 2478 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2479 2480 virtual SourceRange getSourceRange() const { 2481 return SourceRange(BuiltinLoc, RParenLoc); 2482 } 2483 static bool classof(const Stmt *T) { 2484 return T->getStmtClass() == ShuffleVectorExprClass; 2485 } 2486 static bool classof(const ShuffleVectorExpr *) { return true; } 2487 2488 ~ShuffleVectorExpr() {} 2489 2490 /// getNumSubExprs - Return the size of the SubExprs array. This includes the 2491 /// constant expression, the actual arguments passed in, and the function 2492 /// pointers. 2493 unsigned getNumSubExprs() const { return NumExprs; } 2494 2495 /// getExpr - Return the Expr at the specified index. 2496 Expr *getExpr(unsigned Index) { 2497 assert((Index < NumExprs) && "Arg access out of range!"); 2498 return cast<Expr>(SubExprs[Index]); 2499 } 2500 const Expr *getExpr(unsigned Index) const { 2501 assert((Index < NumExprs) && "Arg access out of range!"); 2502 return cast<Expr>(SubExprs[Index]); 2503 } 2504 2505 void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs); 2506 2507 unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) { 2508 assert((N < NumExprs - 2) && "Shuffle idx out of range!"); 2509 return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue(); 2510 } 2511 2512 // Iterators 2513 virtual child_iterator child_begin(); 2514 virtual child_iterator child_end(); 2515}; 2516 2517/// ChooseExpr - GNU builtin-in function __builtin_choose_expr. 2518/// This AST node is similar to the conditional operator (?:) in C, with 2519/// the following exceptions: 2520/// - the test expression must be a integer constant expression. 2521/// - the expression returned acts like the chosen subexpression in every 2522/// visible way: the type is the same as that of the chosen subexpression, 2523/// and all predicates (whether it's an l-value, whether it's an integer 2524/// constant expression, etc.) return the same result as for the chosen 2525/// sub-expression. 2526class ChooseExpr : public Expr { 2527 enum { COND, LHS, RHS, END_EXPR }; 2528 Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. 2529 SourceLocation BuiltinLoc, RParenLoc; 2530public: 2531 ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, 2532 SourceLocation RP, bool TypeDependent, bool ValueDependent) 2533 : Expr(ChooseExprClass, t, TypeDependent, ValueDependent), 2534 BuiltinLoc(BLoc), RParenLoc(RP) { 2535 SubExprs[COND] = cond; 2536 SubExprs[LHS] = lhs; 2537 SubExprs[RHS] = rhs; 2538 } 2539 2540 /// \brief Build an empty __builtin_choose_expr. 2541 explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } 2542 2543 /// isConditionTrue - Return whether the condition is true (i.e. not 2544 /// equal to zero). 2545 bool isConditionTrue(ASTContext &C) const; 2546 2547 /// getChosenSubExpr - Return the subexpression chosen according to the 2548 /// condition. 2549 Expr *getChosenSubExpr(ASTContext &C) const { 2550 return isConditionTrue(C) ? getLHS() : getRHS(); 2551 } 2552 2553 Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } 2554 void setCond(Expr *E) { SubExprs[COND] = E; } 2555 Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } 2556 void setLHS(Expr *E) { SubExprs[LHS] = E; } 2557 Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } 2558 void setRHS(Expr *E) { SubExprs[RHS] = E; } 2559 2560 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2561 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2562 2563 SourceLocation getRParenLoc() const { return RParenLoc; } 2564 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2565 2566 virtual SourceRange getSourceRange() const { 2567 return SourceRange(BuiltinLoc, RParenLoc); 2568 } 2569 static bool classof(const Stmt *T) { 2570 return T->getStmtClass() == ChooseExprClass; 2571 } 2572 static bool classof(const ChooseExpr *) { return true; } 2573 2574 // Iterators 2575 virtual child_iterator child_begin(); 2576 virtual child_iterator child_end(); 2577}; 2578 2579/// GNUNullExpr - Implements the GNU __null extension, which is a name 2580/// for a null pointer constant that has integral type (e.g., int or 2581/// long) and is the same size and alignment as a pointer. The __null 2582/// extension is typically only used by system headers, which define 2583/// NULL as __null in C++ rather than using 0 (which is an integer 2584/// that may not match the size of a pointer). 2585class GNUNullExpr : public Expr { 2586 /// TokenLoc - The location of the __null keyword. 2587 SourceLocation TokenLoc; 2588 2589public: 2590 GNUNullExpr(QualType Ty, SourceLocation Loc) 2591 : Expr(GNUNullExprClass, Ty, false, false), TokenLoc(Loc) { } 2592 2593 /// \brief Build an empty GNU __null expression. 2594 explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } 2595 2596 /// getTokenLocation - The location of the __null token. 2597 SourceLocation getTokenLocation() const { return TokenLoc; } 2598 void setTokenLocation(SourceLocation L) { TokenLoc = L; } 2599 2600 virtual SourceRange getSourceRange() const { 2601 return SourceRange(TokenLoc); 2602 } 2603 static bool classof(const Stmt *T) { 2604 return T->getStmtClass() == GNUNullExprClass; 2605 } 2606 static bool classof(const GNUNullExpr *) { return true; } 2607 2608 // Iterators 2609 virtual child_iterator child_begin(); 2610 virtual child_iterator child_end(); 2611}; 2612 2613/// VAArgExpr, used for the builtin function __builtin_va_arg. 2614class VAArgExpr : public Expr { 2615 Stmt *Val; 2616 SourceLocation BuiltinLoc, RParenLoc; 2617public: 2618 VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc) 2619 : Expr(VAArgExprClass, t, t->isDependentType(), false), 2620 Val(e), 2621 BuiltinLoc(BLoc), 2622 RParenLoc(RPLoc) { } 2623 2624 /// \brief Create an empty __builtin_va_arg expression. 2625 explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { } 2626 2627 const Expr *getSubExpr() const { return cast<Expr>(Val); } 2628 Expr *getSubExpr() { return cast<Expr>(Val); } 2629 void setSubExpr(Expr *E) { Val = E; } 2630 2631 SourceLocation getBuiltinLoc() const { return BuiltinLoc; } 2632 void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } 2633 2634 SourceLocation getRParenLoc() const { return RParenLoc; } 2635 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2636 2637 virtual SourceRange getSourceRange() const { 2638 return SourceRange(BuiltinLoc, RParenLoc); 2639 } 2640 static bool classof(const Stmt *T) { 2641 return T->getStmtClass() == VAArgExprClass; 2642 } 2643 static bool classof(const VAArgExpr *) { return true; } 2644 2645 // Iterators 2646 virtual child_iterator child_begin(); 2647 virtual child_iterator child_end(); 2648}; 2649 2650/// @brief Describes an C or C++ initializer list. 2651/// 2652/// InitListExpr describes an initializer list, which can be used to 2653/// initialize objects of different types, including 2654/// struct/class/union types, arrays, and vectors. For example: 2655/// 2656/// @code 2657/// struct foo x = { 1, { 2, 3 } }; 2658/// @endcode 2659/// 2660/// Prior to semantic analysis, an initializer list will represent the 2661/// initializer list as written by the user, but will have the 2662/// placeholder type "void". This initializer list is called the 2663/// syntactic form of the initializer, and may contain C99 designated 2664/// initializers (represented as DesignatedInitExprs), initializations 2665/// of subobject members without explicit braces, and so on. Clients 2666/// interested in the original syntax of the initializer list should 2667/// use the syntactic form of the initializer list. 2668/// 2669/// After semantic analysis, the initializer list will represent the 2670/// semantic form of the initializer, where the initializations of all 2671/// subobjects are made explicit with nested InitListExpr nodes and 2672/// C99 designators have been eliminated by placing the designated 2673/// initializations into the subobject they initialize. Additionally, 2674/// any "holes" in the initialization, where no initializer has been 2675/// specified for a particular subobject, will be replaced with 2676/// implicitly-generated ImplicitValueInitExpr expressions that 2677/// value-initialize the subobjects. Note, however, that the 2678/// initializer lists may still have fewer initializers than there are 2679/// elements to initialize within the object. 2680/// 2681/// Given the semantic form of the initializer list, one can retrieve 2682/// the original syntactic form of that initializer list (if it 2683/// exists) using getSyntacticForm(). Since many initializer lists 2684/// have the same syntactic and semantic forms, getSyntacticForm() may 2685/// return NULL, indicating that the current initializer list also 2686/// serves as its syntactic form. 2687class InitListExpr : public Expr { 2688 // FIXME: Eliminate this vector in favor of ASTContext allocation 2689 typedef ASTVector<Stmt *> InitExprsTy; 2690 InitExprsTy InitExprs; 2691 SourceLocation LBraceLoc, RBraceLoc; 2692 2693 /// Contains the initializer list that describes the syntactic form 2694 /// written in the source code. 2695 InitListExpr *SyntacticForm; 2696 2697 /// If this initializer list initializes a union, specifies which 2698 /// field within the union will be initialized. 2699 FieldDecl *UnionFieldInit; 2700 2701 /// Whether this initializer list originally had a GNU array-range 2702 /// designator in it. This is a temporary marker used by CodeGen. 2703 bool HadArrayRangeDesignator; 2704 2705public: 2706 InitListExpr(ASTContext &C, SourceLocation lbraceloc, 2707 Expr **initexprs, unsigned numinits, 2708 SourceLocation rbraceloc); 2709 2710 /// \brief Build an empty initializer list. 2711 explicit InitListExpr(ASTContext &C, EmptyShell Empty) 2712 : Expr(InitListExprClass, Empty), InitExprs(C) { } 2713 2714 unsigned getNumInits() const { return InitExprs.size(); } 2715 2716 const Expr* getInit(unsigned Init) const { 2717 assert(Init < getNumInits() && "Initializer access out of range!"); 2718 return cast_or_null<Expr>(InitExprs[Init]); 2719 } 2720 2721 Expr* getInit(unsigned Init) { 2722 assert(Init < getNumInits() && "Initializer access out of range!"); 2723 return cast_or_null<Expr>(InitExprs[Init]); 2724 } 2725 2726 void setInit(unsigned Init, Expr *expr) { 2727 assert(Init < getNumInits() && "Initializer access out of range!"); 2728 InitExprs[Init] = expr; 2729 } 2730 2731 /// \brief Reserve space for some number of initializers. 2732 void reserveInits(ASTContext &C, unsigned NumInits); 2733 2734 /// @brief Specify the number of initializers 2735 /// 2736 /// If there are more than @p NumInits initializers, the remaining 2737 /// initializers will be destroyed. If there are fewer than @p 2738 /// NumInits initializers, NULL expressions will be added for the 2739 /// unknown initializers. 2740 void resizeInits(ASTContext &Context, unsigned NumInits); 2741 2742 /// @brief Updates the initializer at index @p Init with the new 2743 /// expression @p expr, and returns the old expression at that 2744 /// location. 2745 /// 2746 /// When @p Init is out of range for this initializer list, the 2747 /// initializer list will be extended with NULL expressions to 2748 /// accomodate the new entry. 2749 Expr *updateInit(ASTContext &C, unsigned Init, Expr *expr); 2750 2751 /// \brief If this initializes a union, specifies which field in the 2752 /// union to initialize. 2753 /// 2754 /// Typically, this field is the first named field within the 2755 /// union. However, a designated initializer can specify the 2756 /// initialization of a different field within the union. 2757 FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; } 2758 void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; } 2759 2760 // Explicit InitListExpr's originate from source code (and have valid source 2761 // locations). Implicit InitListExpr's are created by the semantic analyzer. 2762 bool isExplicit() { 2763 return LBraceLoc.isValid() && RBraceLoc.isValid(); 2764 } 2765 2766 SourceLocation getLBraceLoc() const { return LBraceLoc; } 2767 void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } 2768 SourceLocation getRBraceLoc() const { return RBraceLoc; } 2769 void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } 2770 2771 /// @brief Retrieve the initializer list that describes the 2772 /// syntactic form of the initializer. 2773 /// 2774 /// 2775 InitListExpr *getSyntacticForm() const { return SyntacticForm; } 2776 void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; } 2777 2778 bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; } 2779 void sawArrayRangeDesignator(bool ARD = true) { 2780 HadArrayRangeDesignator = ARD; 2781 } 2782 2783 virtual SourceRange getSourceRange() const { 2784 return SourceRange(LBraceLoc, RBraceLoc); 2785 } 2786 static bool classof(const Stmt *T) { 2787 return T->getStmtClass() == InitListExprClass; 2788 } 2789 static bool classof(const InitListExpr *) { return true; } 2790 2791 // Iterators 2792 virtual child_iterator child_begin(); 2793 virtual child_iterator child_end(); 2794 2795 typedef InitExprsTy::iterator iterator; 2796 typedef InitExprsTy::reverse_iterator reverse_iterator; 2797 2798 iterator begin() { return InitExprs.begin(); } 2799 iterator end() { return InitExprs.end(); } 2800 reverse_iterator rbegin() { return InitExprs.rbegin(); } 2801 reverse_iterator rend() { return InitExprs.rend(); } 2802}; 2803 2804/// @brief Represents a C99 designated initializer expression. 2805/// 2806/// A designated initializer expression (C99 6.7.8) contains one or 2807/// more designators (which can be field designators, array 2808/// designators, or GNU array-range designators) followed by an 2809/// expression that initializes the field or element(s) that the 2810/// designators refer to. For example, given: 2811/// 2812/// @code 2813/// struct point { 2814/// double x; 2815/// double y; 2816/// }; 2817/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; 2818/// @endcode 2819/// 2820/// The InitListExpr contains three DesignatedInitExprs, the first of 2821/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two 2822/// designators, one array designator for @c [2] followed by one field 2823/// designator for @c .y. The initalization expression will be 1.0. 2824class DesignatedInitExpr : public Expr { 2825public: 2826 /// \brief Forward declaration of the Designator class. 2827 class Designator; 2828 2829private: 2830 /// The location of the '=' or ':' prior to the actual initializer 2831 /// expression. 2832 SourceLocation EqualOrColonLoc; 2833 2834 /// Whether this designated initializer used the GNU deprecated 2835 /// syntax rather than the C99 '=' syntax. 2836 bool GNUSyntax : 1; 2837 2838 /// The number of designators in this initializer expression. 2839 unsigned NumDesignators : 15; 2840 2841 /// \brief The designators in this designated initialization 2842 /// expression. 2843 Designator *Designators; 2844 2845 /// The number of subexpressions of this initializer expression, 2846 /// which contains both the initializer and any additional 2847 /// expressions used by array and array-range designators. 2848 unsigned NumSubExprs : 16; 2849 2850 2851 DesignatedInitExpr(ASTContext &C, QualType Ty, unsigned NumDesignators, 2852 const Designator *Designators, 2853 SourceLocation EqualOrColonLoc, bool GNUSyntax, 2854 Expr **IndexExprs, unsigned NumIndexExprs, 2855 Expr *Init); 2856 2857 explicit DesignatedInitExpr(unsigned NumSubExprs) 2858 : Expr(DesignatedInitExprClass, EmptyShell()), 2859 NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } 2860 2861protected: 2862 virtual void DoDestroy(ASTContext &C); 2863 2864 void DestroyDesignators(ASTContext &C); 2865 2866public: 2867 /// A field designator, e.g., ".x". 2868 struct FieldDesignator { 2869 /// Refers to the field that is being initialized. The low bit 2870 /// of this field determines whether this is actually a pointer 2871 /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When 2872 /// initially constructed, a field designator will store an 2873 /// IdentifierInfo*. After semantic analysis has resolved that 2874 /// name, the field designator will instead store a FieldDecl*. 2875 uintptr_t NameOrField; 2876 2877 /// The location of the '.' in the designated initializer. 2878 unsigned DotLoc; 2879 2880 /// The location of the field name in the designated initializer. 2881 unsigned FieldLoc; 2882 }; 2883 2884 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2885 struct ArrayOrRangeDesignator { 2886 /// Location of the first index expression within the designated 2887 /// initializer expression's list of subexpressions. 2888 unsigned Index; 2889 /// The location of the '[' starting the array range designator. 2890 unsigned LBracketLoc; 2891 /// The location of the ellipsis separating the start and end 2892 /// indices. Only valid for GNU array-range designators. 2893 unsigned EllipsisLoc; 2894 /// The location of the ']' terminating the array range designator. 2895 unsigned RBracketLoc; 2896 }; 2897 2898 /// @brief Represents a single C99 designator. 2899 /// 2900 /// @todo This class is infuriatingly similar to clang::Designator, 2901 /// but minor differences (storing indices vs. storing pointers) 2902 /// keep us from reusing it. Try harder, later, to rectify these 2903 /// differences. 2904 class Designator { 2905 /// @brief The kind of designator this describes. 2906 enum { 2907 FieldDesignator, 2908 ArrayDesignator, 2909 ArrayRangeDesignator 2910 } Kind; 2911 2912 union { 2913 /// A field designator, e.g., ".x". 2914 struct FieldDesignator Field; 2915 /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". 2916 struct ArrayOrRangeDesignator ArrayOrRange; 2917 }; 2918 friend class DesignatedInitExpr; 2919 2920 public: 2921 Designator() {} 2922 2923 /// @brief Initializes a field designator. 2924 Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, 2925 SourceLocation FieldLoc) 2926 : Kind(FieldDesignator) { 2927 Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01; 2928 Field.DotLoc = DotLoc.getRawEncoding(); 2929 Field.FieldLoc = FieldLoc.getRawEncoding(); 2930 } 2931 2932 /// @brief Initializes an array designator. 2933 Designator(unsigned Index, SourceLocation LBracketLoc, 2934 SourceLocation RBracketLoc) 2935 : Kind(ArrayDesignator) { 2936 ArrayOrRange.Index = Index; 2937 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2938 ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding(); 2939 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2940 } 2941 2942 /// @brief Initializes a GNU array-range designator. 2943 Designator(unsigned Index, SourceLocation LBracketLoc, 2944 SourceLocation EllipsisLoc, SourceLocation RBracketLoc) 2945 : Kind(ArrayRangeDesignator) { 2946 ArrayOrRange.Index = Index; 2947 ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding(); 2948 ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding(); 2949 ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding(); 2950 } 2951 2952 bool isFieldDesignator() const { return Kind == FieldDesignator; } 2953 bool isArrayDesignator() const { return Kind == ArrayDesignator; } 2954 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } 2955 2956 IdentifierInfo * getFieldName(); 2957 2958 FieldDecl *getField() { 2959 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2960 if (Field.NameOrField & 0x01) 2961 return 0; 2962 else 2963 return reinterpret_cast<FieldDecl *>(Field.NameOrField); 2964 } 2965 2966 void setField(FieldDecl *FD) { 2967 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2968 Field.NameOrField = reinterpret_cast<uintptr_t>(FD); 2969 } 2970 2971 SourceLocation getDotLoc() const { 2972 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2973 return SourceLocation::getFromRawEncoding(Field.DotLoc); 2974 } 2975 2976 SourceLocation getFieldLoc() const { 2977 assert(Kind == FieldDesignator && "Only valid on a field designator"); 2978 return SourceLocation::getFromRawEncoding(Field.FieldLoc); 2979 } 2980 2981 SourceLocation getLBracketLoc() const { 2982 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2983 "Only valid on an array or array-range designator"); 2984 return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc); 2985 } 2986 2987 SourceLocation getRBracketLoc() const { 2988 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 2989 "Only valid on an array or array-range designator"); 2990 return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc); 2991 } 2992 2993 SourceLocation getEllipsisLoc() const { 2994 assert(Kind == ArrayRangeDesignator && 2995 "Only valid on an array-range designator"); 2996 return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc); 2997 } 2998 2999 unsigned getFirstExprIndex() const { 3000 assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && 3001 "Only valid on an array or array-range designator"); 3002 return ArrayOrRange.Index; 3003 } 3004 3005 SourceLocation getStartLocation() const { 3006 if (Kind == FieldDesignator) 3007 return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); 3008 else 3009 return getLBracketLoc(); 3010 } 3011 }; 3012 3013 static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators, 3014 unsigned NumDesignators, 3015 Expr **IndexExprs, unsigned NumIndexExprs, 3016 SourceLocation EqualOrColonLoc, 3017 bool GNUSyntax, Expr *Init); 3018 3019 static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs); 3020 3021 /// @brief Returns the number of designators in this initializer. 3022 unsigned size() const { return NumDesignators; } 3023 3024 // Iterator access to the designators. 3025 typedef Designator* designators_iterator; 3026 designators_iterator designators_begin() { return Designators; } 3027 designators_iterator designators_end() { 3028 return Designators + NumDesignators; 3029 } 3030 3031 Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; } 3032 3033 void setDesignators(ASTContext &C, const Designator *Desigs, 3034 unsigned NumDesigs); 3035 3036 Expr *getArrayIndex(const Designator& D); 3037 Expr *getArrayRangeStart(const Designator& D); 3038 Expr *getArrayRangeEnd(const Designator& D); 3039 3040 /// @brief Retrieve the location of the '=' that precedes the 3041 /// initializer value itself, if present. 3042 SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } 3043 void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } 3044 3045 /// @brief Determines whether this designated initializer used the 3046 /// deprecated GNU syntax for designated initializers. 3047 bool usesGNUSyntax() const { return GNUSyntax; } 3048 void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } 3049 3050 /// @brief Retrieve the initializer value. 3051 Expr *getInit() const { 3052 return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); 3053 } 3054 3055 void setInit(Expr *init) { 3056 *child_begin() = init; 3057 } 3058 3059 /// \brief Retrieve the total number of subexpressions in this 3060 /// designated initializer expression, including the actual 3061 /// initialized value and any expressions that occur within array 3062 /// and array-range designators. 3063 unsigned getNumSubExprs() const { return NumSubExprs; } 3064 3065 Expr *getSubExpr(unsigned Idx) { 3066 assert(Idx < NumSubExprs && "Subscript out of range"); 3067 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3068 Ptr += sizeof(DesignatedInitExpr); 3069 return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx]; 3070 } 3071 3072 void setSubExpr(unsigned Idx, Expr *E) { 3073 assert(Idx < NumSubExprs && "Subscript out of range"); 3074 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 3075 Ptr += sizeof(DesignatedInitExpr); 3076 reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E; 3077 } 3078 3079 /// \brief Replaces the designator at index @p Idx with the series 3080 /// of designators in [First, Last). 3081 void ExpandDesignator(ASTContext &C, unsigned Idx, const Designator *First, 3082 const Designator *Last); 3083 3084 virtual SourceRange getSourceRange() const; 3085 3086 static bool classof(const Stmt *T) { 3087 return T->getStmtClass() == DesignatedInitExprClass; 3088 } 3089 static bool classof(const DesignatedInitExpr *) { return true; } 3090 3091 // Iterators 3092 virtual child_iterator child_begin(); 3093 virtual child_iterator child_end(); 3094}; 3095 3096/// \brief Represents an implicitly-generated value initialization of 3097/// an object of a given type. 3098/// 3099/// Implicit value initializations occur within semantic initializer 3100/// list expressions (InitListExpr) as placeholders for subobject 3101/// initializations not explicitly specified by the user. 3102/// 3103/// \see InitListExpr 3104class ImplicitValueInitExpr : public Expr { 3105public: 3106 explicit ImplicitValueInitExpr(QualType ty) 3107 : Expr(ImplicitValueInitExprClass, ty, false, false) { } 3108 3109 /// \brief Construct an empty implicit value initialization. 3110 explicit ImplicitValueInitExpr(EmptyShell Empty) 3111 : Expr(ImplicitValueInitExprClass, Empty) { } 3112 3113 static bool classof(const Stmt *T) { 3114 return T->getStmtClass() == ImplicitValueInitExprClass; 3115 } 3116 static bool classof(const ImplicitValueInitExpr *) { return true; } 3117 3118 virtual SourceRange getSourceRange() const { 3119 return SourceRange(); 3120 } 3121 3122 // Iterators 3123 virtual child_iterator child_begin(); 3124 virtual child_iterator child_end(); 3125}; 3126 3127 3128class ParenListExpr : public Expr { 3129 Stmt **Exprs; 3130 unsigned NumExprs; 3131 SourceLocation LParenLoc, RParenLoc; 3132 3133protected: 3134 virtual void DoDestroy(ASTContext& C); 3135 3136public: 3137 ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs, 3138 unsigned numexprs, SourceLocation rparenloc); 3139 3140 ~ParenListExpr() {} 3141 3142 /// \brief Build an empty paren list. 3143 //explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { } 3144 3145 unsigned getNumExprs() const { return NumExprs; } 3146 3147 const Expr* getExpr(unsigned Init) const { 3148 assert(Init < getNumExprs() && "Initializer access out of range!"); 3149 return cast_or_null<Expr>(Exprs[Init]); 3150 } 3151 3152 Expr* getExpr(unsigned Init) { 3153 assert(Init < getNumExprs() && "Initializer access out of range!"); 3154 return cast_or_null<Expr>(Exprs[Init]); 3155 } 3156 3157 Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); } 3158 3159 SourceLocation getLParenLoc() const { return LParenLoc; } 3160 SourceLocation getRParenLoc() const { return RParenLoc; } 3161 3162 virtual SourceRange getSourceRange() const { 3163 return SourceRange(LParenLoc, RParenLoc); 3164 } 3165 static bool classof(const Stmt *T) { 3166 return T->getStmtClass() == ParenListExprClass; 3167 } 3168 static bool classof(const ParenListExpr *) { return true; } 3169 3170 // Iterators 3171 virtual child_iterator child_begin(); 3172 virtual child_iterator child_end(); 3173}; 3174 3175 3176//===----------------------------------------------------------------------===// 3177// Clang Extensions 3178//===----------------------------------------------------------------------===// 3179 3180 3181/// ExtVectorElementExpr - This represents access to specific elements of a 3182/// vector, and may occur on the left hand side or right hand side. For example 3183/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. 3184/// 3185/// Note that the base may have either vector or pointer to vector type, just 3186/// like a struct field reference. 3187/// 3188class ExtVectorElementExpr : public Expr { 3189 Stmt *Base; 3190 IdentifierInfo *Accessor; 3191 SourceLocation AccessorLoc; 3192public: 3193 ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor, 3194 SourceLocation loc) 3195 : Expr(ExtVectorElementExprClass, ty, base->isTypeDependent(), 3196 base->isValueDependent()), 3197 Base(base), Accessor(&accessor), AccessorLoc(loc) {} 3198 3199 /// \brief Build an empty vector element expression. 3200 explicit ExtVectorElementExpr(EmptyShell Empty) 3201 : Expr(ExtVectorElementExprClass, Empty) { } 3202 3203 const Expr *getBase() const { return cast<Expr>(Base); } 3204 Expr *getBase() { return cast<Expr>(Base); } 3205 void setBase(Expr *E) { Base = E; } 3206 3207 IdentifierInfo &getAccessor() const { return *Accessor; } 3208 void setAccessor(IdentifierInfo *II) { Accessor = II; } 3209 3210 SourceLocation getAccessorLoc() const { return AccessorLoc; } 3211 void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } 3212 3213 /// getNumElements - Get the number of components being selected. 3214 unsigned getNumElements() const; 3215 3216 /// containsDuplicateElements - Return true if any element access is 3217 /// repeated. 3218 bool containsDuplicateElements() const; 3219 3220 /// getEncodedElementAccess - Encode the elements accessed into an llvm 3221 /// aggregate Constant of ConstantInt(s). 3222 void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const; 3223 3224 virtual SourceRange getSourceRange() const { 3225 return SourceRange(getBase()->getLocStart(), AccessorLoc); 3226 } 3227 3228 /// isArrow - Return true if the base expression is a pointer to vector, 3229 /// return false if the base expression is a vector. 3230 bool isArrow() const; 3231 3232 static bool classof(const Stmt *T) { 3233 return T->getStmtClass() == ExtVectorElementExprClass; 3234 } 3235 static bool classof(const ExtVectorElementExpr *) { return true; } 3236 3237 // Iterators 3238 virtual child_iterator child_begin(); 3239 virtual child_iterator child_end(); 3240}; 3241 3242 3243/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. 3244/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 3245class BlockExpr : public Expr { 3246protected: 3247 BlockDecl *TheBlock; 3248 bool HasBlockDeclRefExprs; 3249public: 3250 BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs) 3251 : Expr(BlockExprClass, ty, ty->isDependentType(), false), 3252 TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {} 3253 3254 /// \brief Build an empty block expression. 3255 explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } 3256 3257 const BlockDecl *getBlockDecl() const { return TheBlock; } 3258 BlockDecl *getBlockDecl() { return TheBlock; } 3259 void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } 3260 3261 // Convenience functions for probing the underlying BlockDecl. 3262 SourceLocation getCaretLocation() const; 3263 const Stmt *getBody() const; 3264 Stmt *getBody(); 3265 3266 virtual SourceRange getSourceRange() const { 3267 return SourceRange(getCaretLocation(), getBody()->getLocEnd()); 3268 } 3269 3270 /// getFunctionType - Return the underlying function type for this block. 3271 const FunctionType *getFunctionType() const; 3272 3273 /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr 3274 /// inside of the block that reference values outside the block. 3275 bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; } 3276 void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; } 3277 3278 static bool classof(const Stmt *T) { 3279 return T->getStmtClass() == BlockExprClass; 3280 } 3281 static bool classof(const BlockExpr *) { return true; } 3282 3283 // Iterators 3284 virtual child_iterator child_begin(); 3285 virtual child_iterator child_end(); 3286}; 3287 3288/// BlockDeclRefExpr - A reference to a declared variable, function, 3289/// enum, etc. 3290class BlockDeclRefExpr : public Expr { 3291 ValueDecl *D; 3292 SourceLocation Loc; 3293 bool IsByRef : 1; 3294 bool ConstQualAdded : 1; 3295public: 3296 // FIXME: Fix type/value dependence! 3297 BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef, 3298 bool constAdded = false) 3299 : Expr(BlockDeclRefExprClass, t, false, false), D(d), Loc(l), IsByRef(ByRef), 3300 ConstQualAdded(constAdded) {} 3301 3302 // \brief Build an empty reference to a declared variable in a 3303 // block. 3304 explicit BlockDeclRefExpr(EmptyShell Empty) 3305 : Expr(BlockDeclRefExprClass, Empty) { } 3306 3307 ValueDecl *getDecl() { return D; } 3308 const ValueDecl *getDecl() const { return D; } 3309 void setDecl(ValueDecl *VD) { D = VD; } 3310 3311 SourceLocation getLocation() const { return Loc; } 3312 void setLocation(SourceLocation L) { Loc = L; } 3313 3314 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 3315 3316 bool isByRef() const { return IsByRef; } 3317 void setByRef(bool BR) { IsByRef = BR; } 3318 3319 bool isConstQualAdded() const { return ConstQualAdded; } 3320 void setConstQualAdded(bool C) { ConstQualAdded = C; } 3321 3322 static bool classof(const Stmt *T) { 3323 return T->getStmtClass() == BlockDeclRefExprClass; 3324 } 3325 static bool classof(const BlockDeclRefExpr *) { return true; } 3326 3327 // Iterators 3328 virtual child_iterator child_begin(); 3329 virtual child_iterator child_end(); 3330}; 3331 3332} // end namespace clang 3333 3334#endif 3335