File: Value.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (312 lines) | stat: -rw-r--r-- 6,643 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "stdafx.h"
#include "Value.h"
#include "Type.h"
#include "Engine.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"
#include "Utils/TypeInfo.h"

namespace storm {

	Value::Value() : type(null), ref(false) {}

	Value::Value(MAYBE(Type *) t) : type(t), ref(false) {}

	Value::Value(Type *t, Bool ref) : type(t), ref(ref) {}

	void Value::deepCopy(CloneEnv *env) {}

	Value Value::asRef() const {
		return Value(type, true);
	}

	Value Value::asRef(Bool ref) const {
		return Value(type, ref);
	}

	Bool Value::operator ==(Value o) const {
		// Treat null and null& the same.
		if (type == null && o.type == null)
			return true;
		return type == o.type
			&& ref == o.ref;
	}

	Bool Value::operator !=(Value o) const {
		return !(*this == o);
	}

	Bool Value::mayReferTo(MAYBE(Type *) o) const {
		if (type == null)
			return true; // void can refer to all types.
		if (o == null)
			return false; // no types may refer to void.
		return o->isA(type); // otherise: use inheritance
	}

	Bool Value::mayReferTo(Value v) const {
		// For objects: We can not create references from values.
		// For values, we need to be able to. In the future, maybe const refs from values?
		if (ref && !v.ref)
			if (isObject() || v.isObject())
				return false;
		return mayReferTo(v.type);
	}

	Bool Value::mayStore(MAYBE(Type *) o) const {
		if (type == o)
			return true;
		if (type == null) // note: implies o != null
			return false;
		if (o == null) // note: implies type != null
			return false;
		// Only check inheritance for objects. We don't have to make sure that both are objects,
		// since 'isA' will fail if one is an object and another is a value.
		if ((type->typeFlags() & typeClass) == typeClass)
			return o->isA(type);
		return false;
	}

	Bool Value::mayStore(Value v) const {
		if (ref != v.ref)
			return false;
		if (mayStore(v.type))
			return true;

		if (ref && type) {
			// If both are references, then we can use 'isA' for values as well!
			return v.type->isA(type);
		}

		return false;
	}

	Bool Value::matches(Value v, NamedFlags flags) const {
		if ((flags & namedStrictRef) && (ref != v.ref))
			return false;

		if (flags & namedMatchNoInheritance) {
			// TODO: We should probably be a bit more restrictive with ref vs no ref?
			return type == v.type;
		} else {
			return mayReferTo(v);
		}
	}

	Value common(Value a, Value b) {
		if (a.type == null || b.type == null)
			return Value();

		for (Type *t = a.type; t; t = t->super()) {
			if (b.type->isA(t))
				return Value(t);
		}
		return Value();
	}

	Value thisPtr(Type *t) {
		if ((t->typeFlags() & typeValue) == typeValue)
			return Value(t, true);
		else
			return Value(t, false);
	}

	Size Value::size() const {
		if (!type) {
			return Size();
		} else if (ref) {
			return Size::sPtr;
		} else if (isValue()) {
			return type->size();
		} else {
			return Size::sPtr;
		}
	}

	static TypeKind::T convert(code::primitive::Kind k) {
		switch (k) {
		case code::primitive::none:
			return TypeKind::nothing;
		case code::primitive::pointer:
			return TypeKind::ptr;
		case code::primitive::integer:
			return TypeKind::signedNr;
		case code::primitive::real:
			return TypeKind::floatNr;
		}
		return TypeKind::nothing;
	}

	BasicTypeInfo Value::typeInfo() const {
		BasicTypeInfo::Kind kind = TypeKind::nothing;

		if (!type) {
			kind = TypeKind::nothing;
		} else if (ref) {
			kind = TypeKind::ptr;
		} else {
			code::TypeDesc *desc = type->typeDesc();
			if (code::PrimitiveDesc *p = as<code::PrimitiveDesc>(desc)) {
				kind = convert(p->v.kind());
			} else if (as<code::SimpleDesc>(desc)) {
				kind = TypeKind::userTrivial;
			} else if (as<code::ComplexDesc>(desc)) {
				kind = TypeKind::userComplex;
			} else {
				assert(false);
			}
		}

		BasicTypeInfo r = {
			size().current(),
			kind,
		};
		return r;
	}

	code::TypeDesc *Value::desc(Engine &e) const {
		if (!type)
			return e.voidDesc();
		if (ref)
			return e.ptrDesc();
		return type->typeDesc();
	}

	code::TypeDesc *desc(EnginePtr e, Value v) {
		return v.desc(e.v);
	}

	Bool Value::returnInReg() const {
		if (ref)
			return true;
		if (!type)
			return true;
		return isAsmType();
	}

	Bool Value::isValue() const {
		if (!type)
			return false;
		return (type->typeFlags() & typeValue) == typeValue;
	}

	Bool Value::isObject() const {
		if (!type)
			return false;
		return (type->typeFlags() & typeClass) == typeClass;
	}

	Bool Value::isClass() const {
		if (!isObject())
			return false;
		return type->isA(StormInfo<Object>::type(type->engine));
	}

	Bool Value::isActor() const {
		if (!isObject())
			return false;
		return type->isA(StormInfo<TObject>::type(type->engine));
	}

	Bool Value::isPrimitive() const {
		if (!type)
			return false;

		if (type->typeFlags() & typeClass)
			return false;

		// Check if it is a primitive.
		code::PrimitiveDesc *desc = as<code::PrimitiveDesc>(type->typeDesc());
		if (!desc)
			return false;

		// Exclude pointers, those need to be treated differently.
		return desc->v.kind() != code::primitive::pointer;
	}

	Bool Value::isAsmType() const {
		if (!type)
			return false;

		if (isObject())
			return true;

		// If it is a primitive type.
		if (as<code::PrimitiveDesc>(type->typeDesc()))
			return true;

		return false;
	}

	Bool Value::isPtr() const {
		if (!type)
			return false;
		if ((type->typeFlags() & typeClass) == typeClass)
			return true;
		if (code::PrimitiveDesc *desc = as<code::PrimitiveDesc>(type->typeDesc()))
			return desc->v.kind() == code::primitive::pointer;
		return false;
	}

	code::Operand Value::copyCtor() const {
		if (isValue() && !isPrimitive()) {
			Function *ctor = type->copyCtor();
			if (!ctor) {
				WARNING(L"Not returning a proper copy-constructor!");
				return code::Operand();
			}

			return code::Ref(ctor->ref());
		} else {
			return code::Operand();
		}
	}

	code::Operand Value::destructor() const {
		if (isValue() && !isPrimitive()) {
			if (Function *dtor = type->destructor())
				return dtor->ref();
		}

		return code::Operand();
	}

	void Value::toS(StrBuf *to) const {
		if (type) {
			*to << type->identifier();
			if (ref)
				*to << S("&");
		} else {
			*to << S("void");
		}
	}

	wostream &operator <<(wostream &to, const Value &v) {
		if (v.type) {
			to << v.type->identifier()->c_str();
			if (v.ref)
				to << L"&";
		} else {
			to << L"void";
		}
		return to;
	}

#ifdef VISUAL_STUDIO
	Array<Value> *valList(Engine &e, nat count, ...) {
		Array<Value> *r = new (e) Array<Value>();
		r->reserve(count);

		va_list l;
		va_start(l, count);

		for (nat i = 0; i < count; i++) {
			r->push(va_arg(l, Value));
		}

		va_end(l);
		return r;
	}
#endif

}