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