File: Var.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • 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 (151 lines) | stat: -rw-r--r-- 4,010 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
#pragma once
#include "Compiler/Name.h"
#include "Compiler/Named.h"
#include "Compiler/CodeGen.h"
#include "Compiler/Syntax/SStr.h"
#include "Actuals.h"
#include "Param.h"

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

		/**
		 * Local variable.
		 */
		class LocalVar : public Named {
			STORM_CLASS;
		public:
			STORM_CTOR LocalVar(Str *name, Value val, SrcPos pos, Bool param);

			// Type.
			Value result;

			// Location (not initialized until code generation).
			VarInfo var;

			// Is this a parameter?
			Bool param;

			// Constant variable? Currently only used for the 'this' pointer.
			Bool constant;

			// Create the variable.
			void STORM_FN create(CodeGen *state);

			// Create the parameter.
			void STORM_FN createParam(CodeGen *state);

			// Is this a "true" this-variable?
			virtual Bool STORM_FN thisVariable();

		private:
			// Create debug information.
			void addInfo(code::Listing *l, code::Var var);
		};

		/**
		 * Specialization for the "this"-variable that is automatically declared inside member
		 * functions. This is so that other parts may find it and make assumptions wrt. threading etc.
		 */
		class ThisVar : public LocalVar {
			STORM_CLASS;
		public:
			// Create with default name (i.e. this).
			STORM_CTOR ThisVar(Value val, SrcPos pos, Bool param);

			// Custom name. We won't find it automatically if it is not named "this", but we can
			// still identify it as it is an instance of this class.
			STORM_CTOR ThisVar(Str *name, Value val, SrcPos pos, Bool param);

			// Is this a "true" this-variable? Yes!
			virtual Bool STORM_FN thisVariable();
		};

		// Helper to create a parameter from a ValParam class.
		LocalVar *STORM_FN createParam(EnginePtr e, ValParam param, SrcPos pos);

		class Block;

		/**
		 * Local variable declaration.
		 */
		class Var : public Expr {
			STORM_CLASS;
		public:
			// Initialize with initializer-list (like Foo(1))
			STORM_CTOR Var(Block *block, SrcName *type, syntax::SStr *name, Actuals *actual);
			STORM_CTOR Var(Block *block, Value type, syntax::SStr *name, Actuals *actual);

			// Initialize to an expression. We don't allow calls to non-cast-ctors in this case!
			STORM_CTOR Var(Block *block, SrcName *type, syntax::SStr *name, Expr *init);
			STORM_CTOR Var(Block *block, Value type, syntax::SStr *name, Expr *init);

			// Initialize to an expression (auto type).
			STORM_CTOR Var(Block *block, syntax::SStr *name, Expr *init);

			// Declared variable.
			LocalVar *var;

			// Result type.
			virtual ExprResult STORM_FN result();

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

			// Better location.
			virtual SrcPos STORM_FN largePos();

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

		private:
			// Initialize.
			void init(Block *block, const Value &type, syntax::SStr *name);

			// Set return value.
			void initTo(Expr *expr);

			// Initialize ctor call. If 'castOnly' is true, then we only allow copy-ctors and
			// cast-ctors to be called.
			void initTo(Actuals *actual, Bool castOnly);

			// Initialize to.
			Expr *initExpr;

			// Initialize using a constructor directly. When dealing with value types, this
			// will create the value in-place instead of copying the value to its location.
			// When dealing with reference types, it does not matter.
			Expr *initCtor;

			// Scope.
			Scope scope;
		};


		/**
		 * Simple variable implementation that stores its value as a reference whenever possible.
		 * Intended to be used to cache intermediate computations during AST rewrites.
		 */
		class RefVar : public Expr {
			STORM_CLASS;
		public:
			// Initialize to an expression.
			STORM_CTOR RefVar(SrcPos pos, Block *block, Str *name, Expr *init);

			// Declared variable.
			LocalVar *var;

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

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

		private:
			// Initialize to.
			Expr *init;
		};

	}
}