blob: bc94e8ebc9b95ef4634798c3aab19dab22333066 [file] [log] [blame]
Daniel Dunbar2eecaab2008-08-23 03:10:25 +00001//===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes implement wrappers around llvm::Value in order to
11// fully represent the range of values for C L- and R- values.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CLANG_CODEGEN_CGVALUE_H
16#define CLANG_CODEGEN_CGVALUE_H
17
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000019#include "clang/AST/Type.h"
20
Daniel Dunbar46f45b92008-09-09 01:06:48 +000021namespace llvm {
22 class Constant;
23 class Value;
24}
25
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000026namespace clang {
Daniel Dunbar85c59ed2008-08-29 08:11:39 +000027 class ObjCPropertyRefExpr;
28
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000029namespace CodeGen {
John McCall4b9c2d22011-11-06 09:01:30 +000030 class AggValueSlot;
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +000031 class CGBitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000032
33/// RValue - This trivial value class is used to represent the result of an
34/// expression that is evaluated. It can be one of three things: either a
35/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
36/// address of an aggregate value in memory.
37class RValue {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000038 enum Flavor { Scalar, Complex, Aggregate };
Mike Stump1eb44332009-09-09 15:08:12 +000039
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000040 // Stores first value and flavor.
41 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
42 // Stores second value and volatility.
43 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
44
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000045public:
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000046 bool isScalar() const { return V1.getInt() == Scalar; }
47 bool isComplex() const { return V1.getInt() == Complex; }
48 bool isAggregate() const { return V1.getInt() == Aggregate; }
Mike Stump1eb44332009-09-09 15:08:12 +000049
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000050 bool isVolatileQualified() const { return V2.getInt(); }
Mike Stump8b3d93a2009-05-23 20:21:36 +000051
Mike Stump519202d2009-11-03 16:11:57 +000052 /// getScalarVal() - Return the Value* of this scalar value.
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000053 llvm::Value *getScalarVal() const {
54 assert(isScalar() && "Not a scalar!");
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000055 return V1.getPointer();
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000056 }
57
58 /// getComplexVal - Return the real/imag components of this complex value.
59 ///
60 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000061 return std::make_pair(V1.getPointer(), V2.getPointer());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000062 }
Mike Stump1eb44332009-09-09 15:08:12 +000063
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000064 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
65 llvm::Value *getAggregateAddr() const {
66 assert(isAggregate() && "Not an aggregate!");
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000067 return V1.getPointer();
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000068 }
Mike Stump1eb44332009-09-09 15:08:12 +000069
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000070 static RValue get(llvm::Value *V) {
71 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000072 ER.V1.setPointer(V);
73 ER.V1.setInt(Scalar);
74 ER.V2.setInt(false);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000075 return ER;
76 }
77 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
78 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000079 ER.V1.setPointer(V1);
80 ER.V2.setPointer(V2);
81 ER.V1.setInt(Complex);
82 ER.V2.setInt(false);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000083 return ER;
84 }
85 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000086 return getComplex(C.first, C.second);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000087 }
Mike Stump8b3d93a2009-05-23 20:21:36 +000088 // FIXME: Aggregate rvalues need to retain information about whether they are
89 // volatile or not. Remove default to find all places that probably get this
90 // wrong.
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000091 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000092 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000093 ER.V1.setPointer(V);
94 ER.V1.setInt(Aggregate);
95 ER.V2.setInt(Volatile);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000096 return ER;
97 }
98};
99
100
101/// LValue - This represents an lvalue references. Because C/C++ allow
102/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
103/// bitrange.
104class LValue {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000105 enum {
106 Simple, // This is a normal l-value, use getAddress().
107 VectorElt, // This is a vector element l-value (V[i]), use getVector*
108 BitField, // This is a bitfield l-value, use getBitfield*.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000109 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
John McCall119a1c62010-12-04 02:32:38 +0000110 PropertyRef // This is an Objective-C property reference, use
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000111 // getPropertyRefExpr
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000112 } LVType;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000113
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000114 llvm::Value *V;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000116 union {
117 // Index into a vector subscript: V[i]
118 llvm::Value *VectorIdx;
119
120 // ExtVector element subset: V.xyx
121 llvm::Constant *VectorElts;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000123 // BitField start bit and size
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000124 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000125
126 // Obj-C property reference expression
127 const ObjCPropertyRefExpr *PropertyRefExpr;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000128 };
129
John McCalla07398e2011-06-16 04:16:24 +0000130 QualType Type;
131
John McCall0953e762009-09-24 19:53:00 +0000132 // 'const' is unused here
133 Qualifiers Quals;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000134
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000135 /// The alignment to use when accessing this lvalue.
Daniel Dunbar77c05902010-08-26 06:02:12 +0000136 unsigned short Alignment;
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000137
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000138 // objective-c's ivar
139 bool Ivar:1;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000140
141 // objective-c's ivar is an array
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000142 bool ObjIsArray:1;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000144 // LValue is non-gc'able for any reason, including being a parameter or local
145 // variable.
146 bool NonGC: 1;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000147
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000148 // Lvalue is a global reference of an objective-c object
149 bool GlobalObjCRef : 1;
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000150
151 // Lvalue is a thread local reference
152 bool ThreadLocalRef : 1;
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000153
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000154 Expr *BaseIvarExp;
Dan Gohman3d5aff52010-10-14 23:06:10 +0000155
156 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
157 llvm::MDNode *TBAAInfo;
158
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000159private:
John McCalla07398e2011-06-16 04:16:24 +0000160 void Initialize(QualType Type, Qualifiers Quals, unsigned Alignment = 0,
Dan Gohman3d5aff52010-10-14 23:06:10 +0000161 llvm::MDNode *TBAAInfo = 0) {
John McCalla07398e2011-06-16 04:16:24 +0000162 this->Type = Type;
John McCall0953e762009-09-24 19:53:00 +0000163 this->Quals = Quals;
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000164 this->Alignment = Alignment;
165 assert(this->Alignment == Alignment && "Alignment exceeds allowed max!");
166
167 // Initialize Objective-C flags.
John McCall0953e762009-09-24 19:53:00 +0000168 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000169 this->ThreadLocalRef = false;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000170 this->BaseIvarExp = 0;
Dan Gohman3d5aff52010-10-14 23:06:10 +0000171 this->TBAAInfo = TBAAInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000174public:
175 bool isSimple() const { return LVType == Simple; }
176 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000177 bool isBitField() const { return LVType == BitField; }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000178 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000179 bool isPropertyRef() const { return LVType == PropertyRef; }
180
John McCall0953e762009-09-24 19:53:00 +0000181 bool isVolatileQualified() const { return Quals.hasVolatile(); }
182 bool isRestrictQualified() const { return Quals.hasRestrict(); }
183 unsigned getVRQualifiers() const {
184 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattner1bd885e2009-02-16 22:25:49 +0000185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
John McCalla07398e2011-06-16 04:16:24 +0000187 QualType getType() const { return Type; }
188
189 Qualifiers::ObjCLifetime getObjCLifetime() const {
190 return Quals.getObjCLifetime();
191 }
192
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000193 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000194 void setObjCIvar(bool Value) { Ivar = Value; }
195
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000196 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000197 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbarea619172010-08-21 03:22:38 +0000198
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000199 bool isNonGC () const { return NonGC; }
Daniel Dunbarea619172010-08-21 03:22:38 +0000200 void setNonGC(bool Value) { NonGC = Value; }
201
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000202 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000203 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
204
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000205 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000206 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
207
208 bool isObjCWeak() const {
209 return Quals.getObjCGCAttr() == Qualifiers::Weak;
210 }
211 bool isObjCStrong() const {
212 return Quals.getObjCGCAttr() == Qualifiers::Strong;
213 }
John McCalla07398e2011-06-16 04:16:24 +0000214
215 bool isVolatile() const {
216 return Quals.hasVolatile();
217 }
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000218
219 Expr *getBaseIvarExp() const { return BaseIvarExp; }
220 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000221
Dan Gohman3d5aff52010-10-14 23:06:10 +0000222 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
223 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
224
Daniel Dunbar99ad7df2010-08-21 03:29:54 +0000225 const Qualifiers &getQuals() const { return Quals; }
226 Qualifiers &getQuals() { return Quals; }
227
John McCall0953e762009-09-24 19:53:00 +0000228 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000229
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000230 unsigned getAlignment() const { return Alignment; }
231
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000232 // simple lvalue
233 llvm::Value *getAddress() const { assert(isSimple()); return V; }
John McCalla07398e2011-06-16 04:16:24 +0000234 void setAddress(llvm::Value *address) {
235 assert(isSimple());
236 V = address;
237 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000238
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000239 // vector elt lvalue
240 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
241 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000242
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000243 // extended vector elements.
244 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
245 llvm::Constant *getExtVectorElts() const {
246 assert(isExtVectorElt());
247 return VectorElts;
248 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000249
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000250 // bitfield lvalue
Daniel Dunbar7f289642010-04-08 02:59:45 +0000251 llvm::Value *getBitFieldBaseAddr() const {
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000252 assert(isBitField());
253 return V;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000254 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000255 const CGBitFieldInfo &getBitFieldInfo() const {
256 assert(isBitField());
257 return *BitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000258 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000259
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000260 // property ref lvalue
John McCall119a1c62010-12-04 02:32:38 +0000261 llvm::Value *getPropertyRefBaseAddr() const {
262 assert(isPropertyRef());
263 return V;
264 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000265 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
266 assert(isPropertyRef());
267 return PropertyRefExpr;
268 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000269
John McCalla07398e2011-06-16 04:16:24 +0000270 static LValue MakeAddr(llvm::Value *address, QualType type,
271 unsigned alignment, ASTContext &Context,
Dan Gohman3d5aff52010-10-14 23:06:10 +0000272 llvm::MDNode *TBAAInfo = 0) {
John McCalla07398e2011-06-16 04:16:24 +0000273 Qualifiers qs = type.getQualifiers();
274 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbarf1fbda32010-08-21 03:58:45 +0000275
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000276 LValue R;
277 R.LVType = Simple;
John McCalla07398e2011-06-16 04:16:24 +0000278 R.V = address;
279 R.Initialize(type, qs, alignment, TBAAInfo);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000280 return R;
281 }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000283 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
John McCalla07398e2011-06-16 04:16:24 +0000284 QualType type) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000285 LValue R;
286 R.LVType = VectorElt;
287 R.V = Vec;
288 R.VectorIdx = Idx;
John McCalla07398e2011-06-16 04:16:24 +0000289 R.Initialize(type, type.getQualifiers());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000290 return R;
291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000293 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
John McCalla07398e2011-06-16 04:16:24 +0000294 QualType type) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000295 LValue R;
296 R.LVType = ExtVectorElt;
297 R.V = Vec;
298 R.VectorElts = Elts;
John McCalla07398e2011-06-16 04:16:24 +0000299 R.Initialize(type, type.getQualifiers());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000300 return R;
301 }
302
Daniel Dunbar7f289642010-04-08 02:59:45 +0000303 /// \brief Create a new object to represent a bit-field access.
304 ///
305 /// \param BaseValue - The base address of the structure containing the
306 /// bit-field.
307 /// \param Info - The information describing how to perform the bit-field
308 /// access.
John McCalla07398e2011-06-16 04:16:24 +0000309 static LValue MakeBitfield(llvm::Value *BaseValue,
310 const CGBitFieldInfo &Info,
311 QualType type) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000312 LValue R;
313 R.LVType = BitField;
Daniel Dunbar7f289642010-04-08 02:59:45 +0000314 R.V = BaseValue;
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000315 R.BitFieldInfo = &Info;
John McCalla07398e2011-06-16 04:16:24 +0000316 R.Initialize(type, type.getQualifiers());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000317 return R;
318 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000319
Mike Stumpf5408fe2009-05-16 07:57:57 +0000320 // FIXME: It is probably bad that we aren't emitting the target when we build
321 // the lvalue. However, this complicates the code a bit, and I haven't figured
322 // out how to make it go wrong yet.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000323 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E,
John McCall119a1c62010-12-04 02:32:38 +0000324 llvm::Value *Base) {
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000325 LValue R;
326 R.LVType = PropertyRef;
John McCall119a1c62010-12-04 02:32:38 +0000327 R.V = Base;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000328 R.PropertyRefExpr = E;
John McCalla07398e2011-06-16 04:16:24 +0000329 R.Initialize(QualType(), Qualifiers());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000330 return R;
331 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000332};
333
John McCall558d2ab2010-09-15 10:14:12 +0000334/// An aggregate value slot.
335class AggValueSlot {
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000336 /// The address.
John McCall6fa29162010-09-16 03:16:41 +0000337 llvm::Value *Addr;
John McCallf85e1932011-06-15 23:02:42 +0000338
339 // Qualifiers
340 Qualifiers Quals;
John McCall7c2349b2011-08-25 20:40:09 +0000341
John McCallfd71fb82011-08-26 08:02:37 +0000342 /// DestructedFlag - This is set to true if some external code is
343 /// responsible for setting up a destructor for the slot. Otherwise
344 /// the code which constructs it should push the appropriate cleanup.
345 bool DestructedFlag : 1;
346
347 /// ObjCGCFlag - This is set to true if writing to the memory in the
348 /// slot might require calling an appropriate Objective-C GC
349 /// barrier. The exact interaction here is unnecessarily mysterious.
350 bool ObjCGCFlag : 1;
Chris Lattner1b726772010-12-02 07:07:26 +0000351
John McCallfd71fb82011-08-26 08:02:37 +0000352 /// ZeroedFlag - This is set to true if the memory in the slot is
353 /// known to be zero before the assignment into it. This means that
354 /// zero fields don't need to be set.
John McCall7c2349b2011-08-25 20:40:09 +0000355 bool ZeroedFlag : 1;
John McCall558d2ab2010-09-15 10:14:12 +0000356
John McCallfd71fb82011-08-26 08:02:37 +0000357 /// AliasedFlag - This is set to true if the slot might be aliased
358 /// and it's not undefined behavior to access it through such an
359 /// alias. Note that it's always undefined behavior to access a C++
360 /// object that's under construction through an alias derived from
361 /// outside the construction process.
362 ///
363 /// This flag controls whether calls that produce the aggregate
364 /// value may be evaluated directly into the slot, or whether they
365 /// must be evaluated into an unaliased temporary and then memcpy'ed
366 /// over. Since it's invalid in general to memcpy a non-POD C++
367 /// object, it's important that this flag never be set when
368 /// evaluating an expression which constructs such an object.
John McCall410ffb22011-08-25 23:04:34 +0000369 bool AliasedFlag : 1;
370
John McCall558d2ab2010-09-15 10:14:12 +0000371public:
John McCall410ffb22011-08-25 23:04:34 +0000372 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall7c2349b2011-08-25 20:40:09 +0000373 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCall410ffb22011-08-25 23:04:34 +0000374 enum IsZeroed_t { IsNotZeroed, IsZeroed };
John McCall7c2349b2011-08-25 20:40:09 +0000375 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
376
John McCall558d2ab2010-09-15 10:14:12 +0000377 /// ignored - Returns an aggregate value slot indicating that the
378 /// aggregate value is being ignored.
379 static AggValueSlot ignored() {
380 AggValueSlot AV;
John McCall6fa29162010-09-16 03:16:41 +0000381 AV.Addr = 0;
John McCallf85e1932011-06-15 23:02:42 +0000382 AV.Quals = Qualifiers();
John McCallfd71fb82011-08-26 08:02:37 +0000383 AV.DestructedFlag = AV.ObjCGCFlag = AV.ZeroedFlag = AV.AliasedFlag = false;
John McCall558d2ab2010-09-15 10:14:12 +0000384 return AV;
385 }
386
387 /// forAddr - Make a slot for an aggregate value.
388 ///
John McCall7c2349b2011-08-25 20:40:09 +0000389 /// \param quals - The qualifiers that dictate how the slot should
390 /// be initialied. Only 'volatile' and the Objective-C lifetime
391 /// qualifiers matter.
John McCallf85e1932011-06-15 23:02:42 +0000392 ///
John McCall7c2349b2011-08-25 20:40:09 +0000393 /// \param isDestructed - true if something else is responsible
394 /// for calling destructors on this object
395 /// \param needsGC - true if the slot is potentially located
John McCalld1a5f132010-09-16 03:13:23 +0000396 /// somewhere that ObjC GC calls should be emitted for
John McCall7c2349b2011-08-25 20:40:09 +0000397 static AggValueSlot forAddr(llvm::Value *addr, Qualifiers quals,
398 IsDestructed_t isDestructed,
399 NeedsGCBarriers_t needsGC,
John McCall44184392011-08-26 07:31:35 +0000400 IsAliased_t isAliased,
John McCall7c2349b2011-08-25 20:40:09 +0000401 IsZeroed_t isZeroed = IsNotZeroed) {
John McCall558d2ab2010-09-15 10:14:12 +0000402 AggValueSlot AV;
John McCall7c2349b2011-08-25 20:40:09 +0000403 AV.Addr = addr;
404 AV.Quals = quals;
John McCallfd71fb82011-08-26 08:02:37 +0000405 AV.DestructedFlag = isDestructed;
406 AV.ObjCGCFlag = needsGC;
John McCall7c2349b2011-08-25 20:40:09 +0000407 AV.ZeroedFlag = isZeroed;
John McCall410ffb22011-08-25 23:04:34 +0000408 AV.AliasedFlag = isAliased;
John McCall558d2ab2010-09-15 10:14:12 +0000409 return AV;
410 }
411
John McCall7c2349b2011-08-25 20:40:09 +0000412 static AggValueSlot forLValue(LValue LV, IsDestructed_t isDestructed,
413 NeedsGCBarriers_t needsGC,
John McCall44184392011-08-26 07:31:35 +0000414 IsAliased_t isAliased,
John McCall7c2349b2011-08-25 20:40:09 +0000415 IsZeroed_t isZeroed = IsNotZeroed) {
John McCallf85e1932011-06-15 23:02:42 +0000416 return forAddr(LV.getAddress(), LV.getQuals(),
John McCall410ffb22011-08-25 23:04:34 +0000417 isDestructed, needsGC, isAliased, isZeroed);
John McCall558d2ab2010-09-15 10:14:12 +0000418 }
Fariborz Jahanian8a970052010-10-22 22:05:03 +0000419
John McCallfd71fb82011-08-26 08:02:37 +0000420 IsDestructed_t isExternallyDestructed() const {
421 return IsDestructed_t(DestructedFlag);
John McCall558d2ab2010-09-15 10:14:12 +0000422 }
John McCallfd71fb82011-08-26 08:02:37 +0000423 void setExternallyDestructed(bool destructed = true) {
424 DestructedFlag = destructed;
John McCall558d2ab2010-09-15 10:14:12 +0000425 }
426
John McCallf85e1932011-06-15 23:02:42 +0000427 Qualifiers getQualifiers() const { return Quals; }
428
John McCall558d2ab2010-09-15 10:14:12 +0000429 bool isVolatile() const {
John McCallf85e1932011-06-15 23:02:42 +0000430 return Quals.hasVolatile();
431 }
432
433 Qualifiers::ObjCLifetime getObjCLifetime() const {
434 return Quals.getObjCLifetime();
John McCall558d2ab2010-09-15 10:14:12 +0000435 }
436
John McCall7c2349b2011-08-25 20:40:09 +0000437 NeedsGCBarriers_t requiresGCollection() const {
John McCallfd71fb82011-08-26 08:02:37 +0000438 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000439 }
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000440
John McCall558d2ab2010-09-15 10:14:12 +0000441 llvm::Value *getAddr() const {
John McCall6fa29162010-09-16 03:16:41 +0000442 return Addr;
John McCall558d2ab2010-09-15 10:14:12 +0000443 }
444
445 bool isIgnored() const {
John McCall6fa29162010-09-16 03:16:41 +0000446 return Addr == 0;
John McCall558d2ab2010-09-15 10:14:12 +0000447 }
448
John McCall410ffb22011-08-25 23:04:34 +0000449 IsAliased_t isPotentiallyAliased() const {
450 return IsAliased_t(AliasedFlag);
451 }
452
John McCall558d2ab2010-09-15 10:14:12 +0000453 RValue asRValue() const {
454 return RValue::getAggregate(getAddr(), isVolatile());
455 }
John McCall4b9c2d22011-11-06 09:01:30 +0000456
John McCall7c2349b2011-08-25 20:40:09 +0000457 void setZeroed(bool V = true) { ZeroedFlag = V; }
458 IsZeroed_t isZeroed() const {
459 return IsZeroed_t(ZeroedFlag);
Chris Lattner1b726772010-12-02 07:07:26 +0000460 }
John McCall558d2ab2010-09-15 10:14:12 +0000461};
462
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000463} // end namespace CodeGen
464} // end namespace clang
465
466#endif