1 //===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes implement wrappers around llvm::Value in order to
11 // fully represent the range of values for C L- and R- values.
13 //===----------------------------------------------------------------------===//
15 #ifndef CLANG_CODEGEN_CGVALUE_H
16 #define CLANG_CODEGEN_CGVALUE_H
18 #include "clang/AST/Type.h"
26 class ObjCPropertyRefExpr;
27 class ObjCImplicitSetterGetterRefExpr;
32 /// RValue - This trivial value class is used to represent the result of an
33 /// expression that is evaluated. It can be one of three things: either a
34 /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
35 /// address of an aggregate value in memory.
37 enum Flavor { Scalar, Complex, Aggregate };
39 // Stores first value and flavor.
40 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
41 // Stores second value and volatility.
42 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
45 bool isScalar() const { return V1.getInt() == Scalar; }
46 bool isComplex() const { return V1.getInt() == Complex; }
47 bool isAggregate() const { return V1.getInt() == Aggregate; }
49 bool isVolatileQualified() const { return V2.getInt(); }
51 /// getScalarVal() - Return the Value* of this scalar value.
52 llvm::Value *getScalarVal() const {
53 assert(isScalar() && "Not a scalar!");
54 return V1.getPointer();
57 /// getComplexVal - Return the real/imag components of this complex value.
59 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
60 return std::make_pair(V1.getPointer(), V2.getPointer());
63 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
64 llvm::Value *getAggregateAddr() const {
65 assert(isAggregate() && "Not an aggregate!");
66 return V1.getPointer();
69 static RValue get(llvm::Value *V) {
76 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
80 ER.V1.setInt(Complex);
84 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
85 return getComplex(C.first, C.second);
87 // FIXME: Aggregate rvalues need to retain information about whether they are
88 // volatile or not. Remove default to find all places that probably get this
90 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
93 ER.V1.setInt(Aggregate);
94 ER.V2.setInt(Volatile);
100 /// LValue - This represents an lvalue references. Because C/C++ allow
101 /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
107 Simple, // This is a normal l-value, use getAddress().
108 VectorElt, // This is a vector element l-value (V[i]), use getVector*
109 BitField, // This is a bitfield l-value, use getBitfield*.
110 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
111 PropertyRef, // This is an Objective-C property reference, use
112 // getPropertyRefExpr
113 KVCRef // This is an objective-c 'implicit' property ref,
120 // Index into a vector subscript: V[i]
121 llvm::Value *VectorIdx;
123 // ExtVector element subset: V.xyx
124 llvm::Constant *VectorElts;
126 // BitField start bit and size
127 const CGBitFieldInfo *BitFieldInfo;
129 // Obj-C property reference expression
130 const ObjCPropertyRefExpr *PropertyRefExpr;
132 // ObjC 'implicit' property reference expression
133 const ObjCImplicitSetterGetterRefExpr *KVCRefExpr;
136 // 'const' is unused here
139 // objective-c's ivar
142 // objective-c's ivar is an array
145 // LValue is non-gc'able for any reason, including being a parameter or local
149 // Lvalue is a global reference of an objective-c object
150 bool GlobalObjCRef : 1;
154 void SetQualifiers(Qualifiers Quals) {
157 // FIXME: Convenient place to set objc flags to 0. This should really be
158 // done in a user-defined constructor instead.
159 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
160 this->BaseIvarExp = 0;
164 bool isSimple() const { return LVType == Simple; }
165 bool isVectorElt() const { return LVType == VectorElt; }
166 bool isBitField() const { return LVType == BitField; }
167 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
168 bool isPropertyRef() const { return LVType == PropertyRef; }
169 bool isKVCRef() const { return LVType == KVCRef; }
171 bool isVolatileQualified() const { return Quals.hasVolatile(); }
172 bool isRestrictQualified() const { return Quals.hasRestrict(); }
173 unsigned getVRQualifiers() const {
174 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
177 bool isObjCIvar() const { return Ivar; }
178 bool isObjCArray() const { return ObjIsArray; }
179 bool isNonGC () const { return NonGC; }
180 bool isGlobalObjCRef() const { return GlobalObjCRef; }
181 bool isObjCWeak() const { return Quals.getObjCGCAttr() == Qualifiers::Weak; }
182 bool isObjCStrong() const { return Quals.getObjCGCAttr() == Qualifiers::Strong; }
184 Expr *getBaseIvarExp() const { return BaseIvarExp; }
185 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
187 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
189 static void SetObjCIvar(LValue& R, bool iValue) {
192 static void SetObjCArray(LValue& R, bool iValue) {
193 R.ObjIsArray = iValue;
195 static void SetGlobalObjCRef(LValue& R, bool iValue) {
196 R.GlobalObjCRef = iValue;
199 static void SetObjCNonGC(LValue& R, bool iValue) {
204 llvm::Value *getAddress() const { assert(isSimple()); return V; }
207 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
208 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
210 // extended vector elements.
211 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
212 llvm::Constant *getExtVectorElts() const {
213 assert(isExtVectorElt());
218 llvm::Value *getBitFieldBaseAddr() const {
219 assert(isBitField());
222 const CGBitFieldInfo &getBitFieldInfo() const {
223 assert(isBitField());
224 return *BitFieldInfo;
227 // property ref lvalue
228 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
229 assert(isPropertyRef());
230 return PropertyRefExpr;
233 // 'implicit' property ref lvalue
234 const ObjCImplicitSetterGetterRefExpr *getKVCRefExpr() const {
239 static LValue MakeAddr(llvm::Value *V, Qualifiers Quals) {
243 R.SetQualifiers(Quals);
247 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
250 R.LVType = VectorElt;
253 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
257 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
260 R.LVType = ExtVectorElt;
263 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
267 /// \brief Create a new object to represent a bit-field access.
269 /// \param BaseValue - The base address of the structure containing the
271 /// \param Info - The information describing how to perform the bit-field
273 static LValue MakeBitfield(llvm::Value *BaseValue, const CGBitFieldInfo &Info,
278 R.BitFieldInfo = &Info;
279 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
283 // FIXME: It is probably bad that we aren't emitting the target when we build
284 // the lvalue. However, this complicates the code a bit, and I haven't figured
285 // out how to make it go wrong yet.
286 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E,
289 R.LVType = PropertyRef;
290 R.PropertyRefExpr = E;
291 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
295 static LValue MakeKVCRef(const ObjCImplicitSetterGetterRefExpr *E,
300 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
305 } // end namespace CodeGen
306 } // end namespace clang