File: Cast.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; 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 (282 lines) | stat: -rw-r--r-- 7,390 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
#include "stdafx.h"
#include "Cast.h"
#include "Actuals.h"
#include "Named.h"
#include "Compiler/Type.h"
#include "Compiler/Exception.h"
#include "Compiler/Lib/Maybe.h"

namespace storm {
	namespace bs {

		// TODO: Move some of the logic from the function call wrappers in BSNamed/BSActual to the autocast
		// functionality instead (such as casting to/from references). This will make things much cleaner!

		// Our rules. We do not care about Maybe<T> here, it is done later.
		static bool mayReferTo(const Value &from, const Value &to) {
			if (to.type == null)
				return true;
			if (from.type == null)
				return false;

			// Respect inheritance.
			if (!from.type->isA(to.type))
				return false;

			// Only values can be casted to a reference automatically.
			if (to.isObject())
				if (to.ref && !from.ref)
					return false;

			return true;
		}

		struct CastCtor {
			Function *ctor;
			Int extraWeight;
		};

		// Find a cast ctor. Returns a constructor and a value indicating wheter or not we did some maybe-casts.
		static CastCtor castCtor(Value from, Value to, Scope scope, bool exact = false) {
			CastCtor result = { null, false };
			if (!to.type)
				return result;

			Array<Value> *params = new (to.type) Array<Value>(2, from);
			params->at(0) = thisPtr(to.type);
			Str *name = new (to.type) Str(Type::CTOR);

			SimplePart *part;
			if (exact)
				part = new (to.type) SimplePart(name, params);
			else
				part = new (to.type) MaybeAwarePart(name, params);

			Named *n = to.type->find(part, scope);
			if (Function *ctor = as<Function>(n)) {
				if (ctor->fnFlags() & fnAutoCast) {
					result.ctor = ctor;
					// Note if we needed some additional casts.
					result.extraWeight = part->matches(ctor, scope);
					return result;
				}
			}

			return result;
		}

		void expectCastable(Expr *from, Value to, Scope scope) {
			if (!castable(from, to, scope))
				throw new (from) TypeError(from->pos, to, from->result());
		}

		Bool castable(Expr *from, Value to, Scope scope) {
			return castable(from, to, namedDefault, scope);
		}

		Bool castable(Expr *from, Value to, NamedFlags mode, Scope scope) {
			return castPenalty(from, to, mode, scope) >= 0;
		}

		Int castPenalty(Expr *from, Value to, Scope scope) {
			return castPenalty(from, to, namedDefault, scope);
		}

		Int castPenalty(Expr *from, ExprResult fromResult, Value to, NamedFlags mode, Scope scope) {
			Int penalty = plainCastPenalty(fromResult, to, mode);
			if (penalty >= 0)
				return penalty;

			// Special cast supported directly by the Expr?
			penalty = from->castPenalty(to);
			if (penalty >= 0)
				return 100 * penalty; // Quite large penalty, so that we prefer functions without casting.

			// If 'to' is a value, we can create a reference to it without problem.
			if (!to.isObject() && to.ref) {
				Int penalty = from->castPenalty(to.asRef(false));
				if (penalty >= 0)
					return 100 * penalty;
			}

			// Find a cast ctor!
			CastCtor ctor = castCtor(fromResult.type(), to, scope);
			if (ctor.ctor) {
				// Larger than casting literals.
				return ctor.extraWeight + 1000;
			}

			return -1;
		}

		Int castPenalty(Expr *from, Value to, NamedFlags mode, Scope scope) {
			return castPenalty(from, from->result(), to, mode, scope);
		}

		Expr *castTo(Expr *from, Value to, Scope scope) {
			return castTo(from, to, namedDefault, scope);
		}

		Int plainCastPenalty(ExprResult from, Value to, NamedFlags mode) {
			// We want to allow 'casting' <nothing> into anything. This is good, since we want to be
			// able to place non-returning functions basically anywhere (especially in if-statements).
			if (from.nothing())
				return 0;
			Value f = from.type();

			if (mode & namedMatchNoInheritance)
				return (mayReferTo(f, to) && f.type == to.type) ? 0 : -1;

			// No work needed?
			if (mayReferTo(f, to)) {
				if (f.type != null && to.type != null)
					return f.type->distanceFrom(to.type);
				else
					return 0;
			}

			return -1;
		}


		Expr *castTo(Expr *from, Value to, NamedFlags mode, Scope scope) {
			ExprResult r = from->result();
			if (r.nothing())
				return from;

			// Safeguard.
			if (castPenalty(from, r, to, mode, scope) < 0)
				return null;

			Value f = r.type();

			// No work?
			if (mayReferTo(f, to))
				return from;

			// Supported cast?
			if (from->castPenalty(to) >= 0)
				return from;

			if (!to.isObject() && to.ref)
				if (from->castPenalty(to.asRef(false)) >= 0)
					return from;

			// Cast ctor?
			CastCtor ctor = castCtor(f, to, scope);
			if (ctor.ctor) {
				Actuals *params = new (from) Actuals(from);

				// Check if we need to cast <maybe>:
				if (isMaybe(ctor.ctor->params->at(1)) && !isMaybe(f)) {
					Function *next = castCtor(f, ctor.ctor->params->at(1), scope, true).ctor;
					assert(next, L"Could not find Maybe<> constructor.");
					CtorCall *call = new (from) CtorCall(from->pos, scope, next, params);
					params = new (from) Actuals(call);
				}

				return new (from) CtorCall(from->pos, scope, ctor.ctor, params);
			}

			return null;
		}

		Expr *expectCastTo(Expr *from, Value to, Scope scope) {
			if (Expr *r = castTo(from, to, scope))
				return r;
			throw new (from) TypeError(from->pos, to, from->result());
		}

		ExprResult common(Expr *a, Expr *b, Scope scope) {
			ExprResult ar = a->result();
			ExprResult br = b->result();

			// Short-circuit in case one "branch" never returns.
			if (ar.nothing())
				return br;
			if (br.nothing())
				return ar;

			// Do we need to wrap/unwrap maybe types?
			if (isMaybe(ar.type()) || isMaybe(br.type())) {
				Value r = common(unwrapMaybe(ar.type()), unwrapMaybe(br.type()));
				if (r != Value())
					return wrapMaybe(r);
			}

			// Simple inheritance?
			ExprResult r = common(ar, br);
			if (r != ExprResult())
				return r;

			// Now, ar and br should have some type.
			Value av = ar.type();
			Value bv = br.type();

			// Simple casting?
			if (castable(a, bv, scope))
				return bv;
			if (castable(b, av, scope))
				return av;

			// Nothing possible... Return void.
			return ExprResult();
		}

		Bool implicitlyCallableCtor(Function *ctor) {
			if (ctor->params->count() != 2)
				return false;

			Value first = ctor->params->at(0);
			Value second = ctor->params->at(1);

			// This is the copy-ctor (at least, close enough).
			if (first.type == second.type)
				return true;

			// If it was explicitly declared as a cast-ctor, then it is also fine.
			if (ctor->fnFlags() & fnAutoCast)
				return true;

			// Else, we don't allow it.
			return false;
		}


		MaybeAwarePart::MaybeAwarePart(Str *name, Array<Value> *params)
			: SimplePart(name, params) {}

		Int MaybeAwarePart::matches(Named *candidate, Scope source) const {
			Array<Value> *c = candidate->params;
			if (c->count() != params->count())
				return -1;

			int distance = 0;

			for (nat i = 0; i < c->count(); i++) {
				const Value &ours = params->at(i);

				// Remove Maybe<> if needed.
				Value match = c->at(i);
				if (!isMaybe(ours)) {
					Value altered = unwrapMaybe(match);
					if (altered.type != match.type) {
						distance += 1000;
						match = altered;
					}
				}

				if (match == Value() && ours != Value())
					return -1;
				if (!match.matches(ours, candidate->flags))
					return -1;
				if (ours.type && match.type)
					distance += ours.type->distanceFrom(match.type);
			}

			return distance;
		}


	}
}