File: Cast.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • 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 (62 lines) | stat: -rw-r--r-- 2,371 bytes parent folder | download | duplicates (4)
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
#pragma once
#include "Expr.h"
#include "Compiler/Scope.h"
#include "Compiler/NamePart.h"

namespace storm {
	namespace bs {
		STORM_PKG(lang.bs);

		/**
		 * Functions for automatic casting in Basic Storm. The casting rules are inspired from C++,
		 * but are generally more restrictive. Basic Storm only uses constructors marked with the
		 * flag namedAutoCast. Aside from that, a few expressions can be casted automatically.
		 */

		// Is the value 'from' castable to 'to'?
		Bool castable(Expr *from, Value to, NamedFlags mode, Scope scope);
		Bool STORM_FN castable(Expr *from, Value to, Scope scope);

		// Same as 'castable', but throws an exception on failure.
		void STORM_FN expectCastable(Expr *from, Value to, Scope scope);

		// What is the penalty of casting 'from' to 'to'? Returns -1 if not possible. Note: We will
		// always allow casting '<nothing>' into anything. This is convenient when implementing
		// if-statements for example.
		Int castPenalty(Expr *from, Value to, NamedFlags mode, Scope scope);
		Int STORM_FN castPenalty(Expr *from, Value to, Scope scope);

		// What is the penalty of casting 'from' to 'to', disregarding any automatic type conversions.
		Int plainCastPenalty(ExprResult from, Value to, NamedFlags mode);

		// Return an expression that returns the type 'to'. If nothing special needs to be done,
		// simply returns 'from'. Note that the returned expr may differ in the 'ref' member!
		Expr *castTo(Expr *from, Value to, NamedFlags mode, Scope scope);
		MAYBE(Expr *) STORM_FN castTo(Expr *from, Value to, Scope scope);

		// Same as 'castTo', but will throw a type error if it is not possible to do the cast.
		Expr *STORM_FN expectCastTo(Expr *from, Value to, Scope scope);

		// Get the lowest common type that can store both expressions.
		ExprResult STORM_FN common(Expr *a, Expr *b, Scope scope);

		// Check if 'ctor' is implicitly callable.
		Bool STORM_FN implicitlyCallableCtor(Function *ctor);


		/**
		 * Class used to find suitable constructors. This works like the plain SimplePart
		 * implementation, but is aware of "maybe" instances.
		 */
		class MaybeAwarePart : public SimplePart {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR MaybeAwarePart(Str *name, Array<Value> *params);

			// Custom weight function.
			virtual Int STORM_FN matches(Named *candidate, Scope source) const;
		};

	}
}