File: WeakCast.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 (204 lines) | stat: -rw-r--r-- 5,466 bytes parent folder | download | duplicates (3)
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
#pragma once
#include "Block.h"
#include "Condition.h"
#include "Compiler/Lib/Maybe.h"

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

		/**
		 * A weak cast. This is a condition that implements a type-cast that may fail. This
		 * implementation means that the failure has to be handled appropriately.
		 *
		 * This is an abstract class that implements some of the common functionality for all weak
		 * casts in the system.
		 */
		class WeakCast : public Condition {
			STORM_ABSTRACT_CLASS;
		public:
			// Create.
			STORM_CTOR WeakCast();

			/**
			 * Interface from Condition.
			 *
			 * Note: 'code' and 'trueCode' are expected to be implemented by subclasses.
			 */

			// Get a suitable position for the cast.
			virtual SrcPos STORM_FN pos() ABSTRACT;

			// Get the result variable created by this cast.
			virtual MAYBE(LocalVar *) STORM_FN result();

			// Make it more convenient to implement the 'trueCode' function.
			virtual void STORM_FN trueCode(CodeGen *state);

			/**
			 * New interface implemented by subclasses.
			 */

			// Get the name of the variable we want to save the result of the weak cast to by
			// default. May return null if there is no obvious location to store the result in.
			virtual MAYBE(Str *) STORM_FN overwrite();

			// Get the type of the variable we wish to create.
			virtual Value STORM_FN resultType() ABSTRACT;

			// Called to initialize the created variable in the 'true' branch. Only called if a
			// variable is to be initialized.
			virtual void STORM_FN initCode(CodeGen *state, LocalVar *var) ABSTRACT;

			/**
			 * New functionality provided here.
			 */

			// Set the name of the variable to store the result into.
			void STORM_FN name(syntax::SStr *name);

		protected:
			// Helper for the toS implementation. Outputs the specified variable if applicable.
			void STORM_FN output(StrBuf *to) const;

			// Default implementation of 'overwrite'. Extracts the name from an expression if possible.
			MAYBE(Str *) STORM_FN defaultOverwrite(Expr *expr);

		private:
			// Name of an explicitly specified variable, if any.
			MAYBE(syntax::SStr *) varName;

			// Created variable, if any.
			MAYBE(LocalVar *) created;
		};


		/**
		 * Weak cast using 'as'.
		 */
		class WeakAsCast : public WeakCast {
			STORM_ABSTRACT_CLASS;
		public:
			// Create.
			STORM_CTOR WeakAsCast(Block *block, Expr *expr, SrcName *type);
			STORM_CTOR WeakAsCast(Block *block, Expr *expr, Value to);

			// Get a suitable position for the cast.
			virtual SrcPos STORM_FN pos();

			// Get variable we can write to.
			virtual MAYBE(Str *) STORM_FN overwrite();

			// Get the result type.
			virtual Value STORM_FN resultType();

			// Output.
			virtual void STORM_FN toS(StrBuf *to) const;

		protected:
			// Expression to cast.
			Expr *expr;

			// Where was 'expr' stored in 'code', so that we may access it in 'trueCode'?
			code::Var exprVar;

			// Does 'exprVar' contain a reference?
			Bool exprVarRef;

			// Type to cast to.
			Value to;
		};


		/**
		 * Weak downcast using 'as'.
		 */
		class WeakDowncast : public WeakAsCast {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR WeakDowncast(Block *block, Expr *expr, SrcName *type);
			STORM_CTOR WeakDowncast(Block *block, Expr *expr, Value to);

			// Generate code.
			virtual void STORM_FN code(CodeGen *state, CodeResult *boolResult);

			// Generate code to initialize the variable.
			virtual void STORM_FN initCode(CodeGen *state, LocalVar *var);
		};


		/**
		 * Weak cast extracting values from the Variant type.
		 */
		class WeakVariantCast : public WeakAsCast {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR WeakVariantCast(Block *Block, Expr *expr, SrcName *type);
			STORM_CTOR WeakVariantCast(Block *block, Expr *expr, Value to);

			// Generate code.
			virtual void STORM_FN code(CodeGen *state, CodeResult *boolResult);

			// Generate code to initialize the variable.
			virtual void STORM_FN initCode(CodeGen *state, LocalVar *var);

		private:
			// The Variant type.
			Type *variantType;
		};


		/**
		 * Weak cast from Maybe<T> to T.
		 */
		class WeakMaybeCast : public WeakCast {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR WeakMaybeCast(Expr *expr);

			// Get a suitable position for the cast.
			virtual SrcPos STORM_FN pos();

			// Get variable we can write to.
			virtual MAYBE(Str *) STORM_FN overwrite();

			// Get the result type.
			virtual Value STORM_FN resultType();

			// Generate code.
			virtual void STORM_FN code(CodeGen *state, CodeResult *boolResult);

			// Generate code to initialize the variable.
			virtual void STORM_FN initCode(CodeGen *state, LocalVar *var);

			// Output.
			virtual void STORM_FN toS(StrBuf *to) const;

		private:
			// Expression to cast.
			Expr *expr;

			// Variable used to store 'expr'.
			code::Var exprVar;

			// Type of 'exprVar'.
			Value exprType;

			// Generate code for class types.
			void classCode(MaybeClassType *c, CodeGen *state, CodeResult *boolResult);
			void classInit(MaybeClassType *c, CodeGen *state, LocalVar *var);

			// Genereate code for value types.
			void valueCode(MaybeValueType *c, CodeGen *state, CodeResult *boolResult);
			void valueInit(MaybeValueType *c, CodeGen *state, LocalVar *var);
		};


		// Figure out what kind of weak cast to create based on the type of the lhs of expressions like <lhs> as <type>.
		WeakCast *STORM_FN weakAsCast(Block *block, Expr *expr, SrcName *type);

	}
}