blob: 489e600b3ddfc794045ee0a6ea728e26843b122d [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 {
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +000030 class CGBitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000031
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.
36class RValue {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000037 enum Flavor { Scalar, Complex, Aggregate };
Mike Stump1eb44332009-09-09 15:08:12 +000038
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000039 // 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;
43
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000044public:
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000045 bool isScalar() const { return V1.getInt() == Scalar; }
46 bool isComplex() const { return V1.getInt() == Complex; }
47 bool isAggregate() const { return V1.getInt() == Aggregate; }
Mike Stump1eb44332009-09-09 15:08:12 +000048
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000049 bool isVolatileQualified() const { return V2.getInt(); }
Mike Stump8b3d93a2009-05-23 20:21:36 +000050
Mike Stump519202d2009-11-03 16:11:57 +000051 /// getScalarVal() - Return the Value* of this scalar value.
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000052 llvm::Value *getScalarVal() const {
53 assert(isScalar() && "Not a scalar!");
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000054 return V1.getPointer();
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000055 }
56
57 /// getComplexVal - Return the real/imag components of this complex value.
58 ///
59 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000060 return std::make_pair(V1.getPointer(), V2.getPointer());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000061 }
Mike Stump1eb44332009-09-09 15:08:12 +000062
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000063 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
64 llvm::Value *getAggregateAddr() const {
65 assert(isAggregate() && "Not an aggregate!");
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000066 return V1.getPointer();
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000067 }
Mike Stump1eb44332009-09-09 15:08:12 +000068
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000069 static RValue get(llvm::Value *V) {
70 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000071 ER.V1.setPointer(V);
72 ER.V1.setInt(Scalar);
73 ER.V2.setInt(false);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000074 return ER;
75 }
76 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
77 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000078 ER.V1.setPointer(V1);
79 ER.V2.setPointer(V2);
80 ER.V1.setInt(Complex);
81 ER.V2.setInt(false);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000082 return ER;
83 }
84 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000085 return getComplex(C.first, C.second);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000086 }
Mike Stump8b3d93a2009-05-23 20:21:36 +000087 // 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
89 // wrong.
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000090 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000091 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000092 ER.V1.setPointer(V);
93 ER.V1.setInt(Aggregate);
94 ER.V2.setInt(Volatile);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000095 return ER;
96 }
97};
98
99
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
102/// bitrange.
103class LValue {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000104 enum {
105 Simple, // This is a normal l-value, use getAddress().
106 VectorElt, // This is a vector element l-value (V[i]), use getVector*
107 BitField, // This is a bitfield l-value, use getBitfield*.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000108 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
John McCall119a1c62010-12-04 02:32:38 +0000109 PropertyRef // This is an Objective-C property reference, use
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000110 // getPropertyRefExpr
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000111 } LVType;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000112
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000113 llvm::Value *V;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000115 union {
116 // Index into a vector subscript: V[i]
117 llvm::Value *VectorIdx;
118
119 // ExtVector element subset: V.xyx
120 llvm::Constant *VectorElts;
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000122 // BitField start bit and size
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000123 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000124
125 // Obj-C property reference expression
126 const ObjCPropertyRefExpr *PropertyRefExpr;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000127 };
128
John McCalla07398e2011-06-16 04:16:24 +0000129 QualType Type;
130
John McCall0953e762009-09-24 19:53:00 +0000131 // 'const' is unused here
132 Qualifiers Quals;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000133
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000134 /// The alignment to use when accessing this lvalue.
Daniel Dunbar77c05902010-08-26 06:02:12 +0000135 unsigned short Alignment;
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000136
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000137 // objective-c's ivar
138 bool Ivar:1;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000139
140 // objective-c's ivar is an array
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000141 bool ObjIsArray:1;
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000143 // LValue is non-gc'able for any reason, including being a parameter or local
144 // variable.
145 bool NonGC: 1;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000146
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000147 // Lvalue is a global reference of an objective-c object
148 bool GlobalObjCRef : 1;
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000149
150 // Lvalue is a thread local reference
151 bool ThreadLocalRef : 1;
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000152
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000153 Expr *BaseIvarExp;
Dan Gohman3d5aff52010-10-14 23:06:10 +0000154
155 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
156 llvm::MDNode *TBAAInfo;
157
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000158private:
John McCalla07398e2011-06-16 04:16:24 +0000159 void Initialize(QualType Type, Qualifiers Quals, unsigned Alignment = 0,
Dan Gohman3d5aff52010-10-14 23:06:10 +0000160 llvm::MDNode *TBAAInfo = 0) {
John McCalla07398e2011-06-16 04:16:24 +0000161 this->Type = Type;
John McCall0953e762009-09-24 19:53:00 +0000162 this->Quals = Quals;
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000163 this->Alignment = Alignment;
164 assert(this->Alignment == Alignment && "Alignment exceeds allowed max!");
165
166 // Initialize Objective-C flags.
John McCall0953e762009-09-24 19:53:00 +0000167 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000168 this->ThreadLocalRef = false;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000169 this->BaseIvarExp = 0;
Dan Gohman3d5aff52010-10-14 23:06:10 +0000170 this->TBAAInfo = TBAAInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000171 }
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000173public:
174 bool isSimple() const { return LVType == Simple; }
175 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000176 bool isBitField() const { return LVType == BitField; }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000177 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000178 bool isPropertyRef() const { return LVType == PropertyRef; }
179
John McCall0953e762009-09-24 19:53:00 +0000180 bool isVolatileQualified() const { return Quals.hasVolatile(); }
181 bool isRestrictQualified() const { return Quals.hasRestrict(); }
182 unsigned getVRQualifiers() const {
183 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattner1bd885e2009-02-16 22:25:49 +0000184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
John McCalla07398e2011-06-16 04:16:24 +0000186 QualType getType() const { return Type; }
187
188 Qualifiers::ObjCLifetime getObjCLifetime() const {
189 return Quals.getObjCLifetime();
190 }
191
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000192 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000193 void setObjCIvar(bool Value) { Ivar = Value; }
194
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000195 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000196 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbarea619172010-08-21 03:22:38 +0000197
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000198 bool isNonGC () const { return NonGC; }
Daniel Dunbarea619172010-08-21 03:22:38 +0000199 void setNonGC(bool Value) { NonGC = Value; }
200
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000201 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000202 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
203
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000204 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000205 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
206
207 bool isObjCWeak() const {
208 return Quals.getObjCGCAttr() == Qualifiers::Weak;
209 }
210 bool isObjCStrong() const {
211 return Quals.getObjCGCAttr() == Qualifiers::Strong;
212 }
John McCalla07398e2011-06-16 04:16:24 +0000213
214 bool isVolatile() const {
215 return Quals.hasVolatile();
216 }
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000217
218 Expr *getBaseIvarExp() const { return BaseIvarExp; }
219 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000220
Dan Gohman3d5aff52010-10-14 23:06:10 +0000221 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
222 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
223
Daniel Dunbar99ad7df2010-08-21 03:29:54 +0000224 const Qualifiers &getQuals() const { return Quals; }
225 Qualifiers &getQuals() { return Quals; }
226
John McCall0953e762009-09-24 19:53:00 +0000227 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000228
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000229 unsigned getAlignment() const { return Alignment; }
230
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000231 // simple lvalue
232 llvm::Value *getAddress() const { assert(isSimple()); return V; }
John McCalla07398e2011-06-16 04:16:24 +0000233 void setAddress(llvm::Value *address) {
234 assert(isSimple());
235 V = address;
236 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000237
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000238 // vector elt lvalue
239 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
240 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000241
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000242 // extended vector elements.
243 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
244 llvm::Constant *getExtVectorElts() const {
245 assert(isExtVectorElt());
246 return VectorElts;
247 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000248
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000249 // bitfield lvalue
Daniel Dunbar7f289642010-04-08 02:59:45 +0000250 llvm::Value *getBitFieldBaseAddr() const {
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000251 assert(isBitField());
252 return V;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000253 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000254 const CGBitFieldInfo &getBitFieldInfo() const {
255 assert(isBitField());
256 return *BitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000257 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000258
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000259 // property ref lvalue
John McCall119a1c62010-12-04 02:32:38 +0000260 llvm::Value *getPropertyRefBaseAddr() const {
261 assert(isPropertyRef());
262 return V;
263 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000264 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
265 assert(isPropertyRef());
266 return PropertyRefExpr;
267 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000268
John McCalla07398e2011-06-16 04:16:24 +0000269 static LValue MakeAddr(llvm::Value *address, QualType type,
270 unsigned alignment, ASTContext &Context,
Dan Gohman3d5aff52010-10-14 23:06:10 +0000271 llvm::MDNode *TBAAInfo = 0) {
John McCalla07398e2011-06-16 04:16:24 +0000272 Qualifiers qs = type.getQualifiers();
273 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbarf1fbda32010-08-21 03:58:45 +0000274
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000275 LValue R;
276 R.LVType = Simple;
John McCalla07398e2011-06-16 04:16:24 +0000277 R.V = address;
278 R.Initialize(type, qs, alignment, TBAAInfo);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000279 return R;
280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000282 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
John McCalla07398e2011-06-16 04:16:24 +0000283 QualType type) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000284 LValue R;
285 R.LVType = VectorElt;
286 R.V = Vec;
287 R.VectorIdx = Idx;
John McCalla07398e2011-06-16 04:16:24 +0000288 R.Initialize(type, type.getQualifiers());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000289 return R;
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000292 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
John McCalla07398e2011-06-16 04:16:24 +0000293 QualType type) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000294 LValue R;
295 R.LVType = ExtVectorElt;
296 R.V = Vec;
297 R.VectorElts = Elts;
John McCalla07398e2011-06-16 04:16:24 +0000298 R.Initialize(type, type.getQualifiers());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000299 return R;
300 }
301
Daniel Dunbar7f289642010-04-08 02:59:45 +0000302 /// \brief Create a new object to represent a bit-field access.
303 ///
304 /// \param BaseValue - The base address of the structure containing the
305 /// bit-field.
306 /// \param Info - The information describing how to perform the bit-field
307 /// access.
John McCalla07398e2011-06-16 04:16:24 +0000308 static LValue MakeBitfield(llvm::Value *BaseValue,
309 const CGBitFieldInfo &Info,
310 QualType type) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000311 LValue R;
312 R.LVType = BitField;
Daniel Dunbar7f289642010-04-08 02:59:45 +0000313 R.V = BaseValue;
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000314 R.BitFieldInfo = &Info;
John McCalla07398e2011-06-16 04:16:24 +0000315 R.Initialize(type, type.getQualifiers());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000316 return R;
317 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000318
Mike Stumpf5408fe2009-05-16 07:57:57 +0000319 // FIXME: It is probably bad that we aren't emitting the target when we build
320 // the lvalue. However, this complicates the code a bit, and I haven't figured
321 // out how to make it go wrong yet.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000322 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E,
John McCall119a1c62010-12-04 02:32:38 +0000323 llvm::Value *Base) {
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000324 LValue R;
325 R.LVType = PropertyRef;
John McCall119a1c62010-12-04 02:32:38 +0000326 R.V = Base;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000327 R.PropertyRefExpr = E;
John McCalla07398e2011-06-16 04:16:24 +0000328 R.Initialize(QualType(), Qualifiers());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000329 return R;
330 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000331};
332
John McCall558d2ab2010-09-15 10:14:12 +0000333/// An aggregate value slot.
334class AggValueSlot {
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000335 /// The address.
John McCall6fa29162010-09-16 03:16:41 +0000336 llvm::Value *Addr;
John McCallf85e1932011-06-15 23:02:42 +0000337
338 // Qualifiers
339 Qualifiers Quals;
John McCall7c2349b2011-08-25 20:40:09 +0000340
John McCallfd71fb82011-08-26 08:02:37 +0000341 /// DestructedFlag - This is set to true if some external code is
342 /// responsible for setting up a destructor for the slot. Otherwise
343 /// the code which constructs it should push the appropriate cleanup.
344 bool DestructedFlag : 1;
345
346 /// ObjCGCFlag - This is set to true if writing to the memory in the
347 /// slot might require calling an appropriate Objective-C GC
348 /// barrier. The exact interaction here is unnecessarily mysterious.
349 bool ObjCGCFlag : 1;
Chris Lattner1b726772010-12-02 07:07:26 +0000350
John McCallfd71fb82011-08-26 08:02:37 +0000351 /// ZeroedFlag - This is set to true if the memory in the slot is
352 /// known to be zero before the assignment into it. This means that
353 /// zero fields don't need to be set.
John McCall7c2349b2011-08-25 20:40:09 +0000354 bool ZeroedFlag : 1;
John McCall558d2ab2010-09-15 10:14:12 +0000355
John McCallfd71fb82011-08-26 08:02:37 +0000356 /// AliasedFlag - This is set to true if the slot might be aliased
357 /// and it's not undefined behavior to access it through such an
358 /// alias. Note that it's always undefined behavior to access a C++
359 /// object that's under construction through an alias derived from
360 /// outside the construction process.
361 ///
362 /// This flag controls whether calls that produce the aggregate
363 /// value may be evaluated directly into the slot, or whether they
364 /// must be evaluated into an unaliased temporary and then memcpy'ed
365 /// over. Since it's invalid in general to memcpy a non-POD C++
366 /// object, it's important that this flag never be set when
367 /// evaluating an expression which constructs such an object.
John McCall410ffb22011-08-25 23:04:34 +0000368 bool AliasedFlag : 1;
369
John McCall558d2ab2010-09-15 10:14:12 +0000370public:
John McCall410ffb22011-08-25 23:04:34 +0000371 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall7c2349b2011-08-25 20:40:09 +0000372 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCall410ffb22011-08-25 23:04:34 +0000373 enum IsZeroed_t { IsNotZeroed, IsZeroed };
John McCall7c2349b2011-08-25 20:40:09 +0000374 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
375
John McCall558d2ab2010-09-15 10:14:12 +0000376 /// ignored - Returns an aggregate value slot indicating that the
377 /// aggregate value is being ignored.
378 static AggValueSlot ignored() {
379 AggValueSlot AV;
John McCall6fa29162010-09-16 03:16:41 +0000380 AV.Addr = 0;
John McCallf85e1932011-06-15 23:02:42 +0000381 AV.Quals = Qualifiers();
John McCallfd71fb82011-08-26 08:02:37 +0000382 AV.DestructedFlag = AV.ObjCGCFlag = AV.ZeroedFlag = AV.AliasedFlag = false;
John McCall558d2ab2010-09-15 10:14:12 +0000383 return AV;
384 }
385
386 /// forAddr - Make a slot for an aggregate value.
387 ///
John McCall7c2349b2011-08-25 20:40:09 +0000388 /// \param quals - The qualifiers that dictate how the slot should
389 /// be initialied. Only 'volatile' and the Objective-C lifetime
390 /// qualifiers matter.
John McCallf85e1932011-06-15 23:02:42 +0000391 ///
John McCall7c2349b2011-08-25 20:40:09 +0000392 /// \param isDestructed - true if something else is responsible
393 /// for calling destructors on this object
394 /// \param needsGC - true if the slot is potentially located
John McCalld1a5f132010-09-16 03:13:23 +0000395 /// somewhere that ObjC GC calls should be emitted for
John McCall7c2349b2011-08-25 20:40:09 +0000396 static AggValueSlot forAddr(llvm::Value *addr, Qualifiers quals,
397 IsDestructed_t isDestructed,
398 NeedsGCBarriers_t needsGC,
John McCall44184392011-08-26 07:31:35 +0000399 IsAliased_t isAliased,
John McCall7c2349b2011-08-25 20:40:09 +0000400 IsZeroed_t isZeroed = IsNotZeroed) {
John McCall558d2ab2010-09-15 10:14:12 +0000401 AggValueSlot AV;
John McCall7c2349b2011-08-25 20:40:09 +0000402 AV.Addr = addr;
403 AV.Quals = quals;
John McCallfd71fb82011-08-26 08:02:37 +0000404 AV.DestructedFlag = isDestructed;
405 AV.ObjCGCFlag = needsGC;
John McCall7c2349b2011-08-25 20:40:09 +0000406 AV.ZeroedFlag = isZeroed;
John McCall410ffb22011-08-25 23:04:34 +0000407 AV.AliasedFlag = isAliased;
John McCall558d2ab2010-09-15 10:14:12 +0000408 return AV;
409 }
410
John McCall7c2349b2011-08-25 20:40:09 +0000411 static AggValueSlot forLValue(LValue LV, IsDestructed_t isDestructed,
412 NeedsGCBarriers_t needsGC,
John McCall44184392011-08-26 07:31:35 +0000413 IsAliased_t isAliased,
John McCall7c2349b2011-08-25 20:40:09 +0000414 IsZeroed_t isZeroed = IsNotZeroed) {
John McCallf85e1932011-06-15 23:02:42 +0000415 return forAddr(LV.getAddress(), LV.getQuals(),
John McCall410ffb22011-08-25 23:04:34 +0000416 isDestructed, needsGC, isAliased, isZeroed);
John McCall558d2ab2010-09-15 10:14:12 +0000417 }
Fariborz Jahanian8a970052010-10-22 22:05:03 +0000418
John McCallfd71fb82011-08-26 08:02:37 +0000419 IsDestructed_t isExternallyDestructed() const {
420 return IsDestructed_t(DestructedFlag);
John McCall558d2ab2010-09-15 10:14:12 +0000421 }
John McCallfd71fb82011-08-26 08:02:37 +0000422 void setExternallyDestructed(bool destructed = true) {
423 DestructedFlag = destructed;
John McCall558d2ab2010-09-15 10:14:12 +0000424 }
425
John McCallf85e1932011-06-15 23:02:42 +0000426 Qualifiers getQualifiers() const { return Quals; }
427
John McCall558d2ab2010-09-15 10:14:12 +0000428 bool isVolatile() const {
John McCallf85e1932011-06-15 23:02:42 +0000429 return Quals.hasVolatile();
430 }
431
432 Qualifiers::ObjCLifetime getObjCLifetime() const {
433 return Quals.getObjCLifetime();
John McCall558d2ab2010-09-15 10:14:12 +0000434 }
435
John McCall7c2349b2011-08-25 20:40:09 +0000436 NeedsGCBarriers_t requiresGCollection() const {
John McCallfd71fb82011-08-26 08:02:37 +0000437 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000438 }
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000439
John McCall558d2ab2010-09-15 10:14:12 +0000440 llvm::Value *getAddr() const {
John McCall6fa29162010-09-16 03:16:41 +0000441 return Addr;
John McCall558d2ab2010-09-15 10:14:12 +0000442 }
443
444 bool isIgnored() const {
John McCall6fa29162010-09-16 03:16:41 +0000445 return Addr == 0;
John McCall558d2ab2010-09-15 10:14:12 +0000446 }
447
John McCall410ffb22011-08-25 23:04:34 +0000448 IsAliased_t isPotentiallyAliased() const {
449 return IsAliased_t(AliasedFlag);
450 }
451
John McCall558d2ab2010-09-15 10:14:12 +0000452 RValue asRValue() const {
453 return RValue::getAggregate(getAddr(), isVolatile());
454 }
455
John McCall7c2349b2011-08-25 20:40:09 +0000456 void setZeroed(bool V = true) { ZeroedFlag = V; }
457 IsZeroed_t isZeroed() const {
458 return IsZeroed_t(ZeroedFlag);
Chris Lattner1b726772010-12-02 07:07:26 +0000459 }
John McCall558d2ab2010-09-15 10:14:12 +0000460};
461
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000462} // end namespace CodeGen
463} // end namespace clang
464
465#endif