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
|
#pragma once
#include "Core/Map.h"
#include "Core/Set.h"
#include "Core/Array.h"
#include "Compiler/Function.h"
#include "Compiler/Syntax/SStr.h"
#include "Compiler/Syntax/Node.h"
#include "Compiler/Variable.h"
#include "Block.h"
#include "Param.h"
namespace storm {
namespace bs {
STORM_PKG(lang.bs);
class CtorBody;
class Class;
/**
* Raw constructor call, much like BSRawFn.
*/
class BSRawCtor : public Function {
STORM_CLASS;
public:
// Create.
STORM_CTOR BSRawCtor(Array<ValParam> *params, SrcPos pos);
// We may need to run on a specific thread based on the current actual parameters.
virtual code::Var STORM_FN findThread(CodeGen *s, Array<code::Operand> *params);
// Add parameters. Outputs 'threadParam' if not null, and remaining parameters for positional access.
void addParams(Block *to, LocalVar *&threadParam, Array<LocalVar *> *¶ms);
// Create the body. Expected to work until 'clearBody' is called.
virtual CtorBody *STORM_FN createBody();
// Called when we know we don't need the body anymore, i.e. 'createBody' may stop
// returning sensible results.
virtual void STORM_FN clearBody();
protected:
// Re-compile at next execution.
void STORM_FN reset();
private:
// Parameter names.
Array<ValParam> *params;
// Generate code.
CodeGen *CODECALL generateCode();
// Do we need a 'hidden' thread parameter?
bool needsThread;
};
/**
* A constructor. Enforces that the parent constructor is called.
*/
class BSCtor : public BSRawCtor {
STORM_CLASS;
public:
// Create. If body is null, we will generate the default ctor.
BSCtor(Array<ValParam> *params, Scope scope, MAYBE(syntax::Node *) body, SrcPos pos);
// Scope.
Scope scope;
// Body.
MAYBE(syntax::Node *) body;
// Create the body.
virtual CtorBody *STORM_FN createBody();
virtual void STORM_FN clearBody();
private:
// Default ctor body.
CtorBody *defaultParse();
};
/**
* A constructor using a pre-created syntax tree.
*/
class BSTreeCtor : public BSRawCtor {
STORM_CLASS;
public:
// Create.
STORM_CTOR BSTreeCtor(Array<ValParam> *params, SrcPos pos);
// Set the root of the syntax tree. Resetting the body also makes the function re-compile.
void STORM_ASSIGN body(CtorBody *body);
// Create the body.
virtual CtorBody *STORM_FN createBody();
virtual void STORM_FN clearBody();
private:
// Body.
MAYBE(CtorBody *) root;
};
/**
* Body of the constructor.
*/
class CtorBody : public ExprBlock {
STORM_CLASS;
public:
STORM_CTOR CtorBody(BSCtor *owner);
STORM_CTOR CtorBody(BSRawCtor *owner, Scope scope);
// Owning function.
Function *owner;
// Temporary storage of the actual LocalVar that stores the parameter we need to capture.
LocalVar *threadParam;
// Other formal parameters, in the order they appear in the formal parameter list.
Array<LocalVar *> *parameters;
// Stores a copy of the parameter used to store the thread. Not reference counted.
// This is to make it impossible to overwrite the thread parameter before passing it
// to the TObject's constructor, like this:
// ctor(Thread a) { a = x; init(); }.
code::Var thread;
// Is a call to the super class' constructor present?
Bool superCalled;
// Is an initialization block present?
Bool initDone;
// Make sure we're properly initialized.
void STORM_FN checkInit();
// We don't need to create a separate block, we can just use the root block instead.
virtual void STORM_FN code(CodeGen *state, CodeResult *to);
};
/**
* Call the constructor of the super class.
*
* After this, 'this' will be available in the function, but the type will be that of the
* super class, not the current class. That only happens after the current class is properly
* initialized by using InitBlock.
*/
class SuperCall : public Expr {
STORM_CLASS;
public:
// Create.
STORM_CTOR SuperCall(SrcPos pos, CtorBody *block, Actuals *params);
// Result (always void).
virtual ExprResult STORM_FN result();
// Code.
virtual void STORM_FN code(CodeGen *s, CodeResult *r);
// To string.
virtual void STORM_FN toS(StrBuf *to) const;
private:
// Parameters.
Actuals *params;
// The variable for the 'this' pointer.
LocalVar *thisVar;
// Our type.
Value thisPtr;
// Scope.
CtorBody *block;
};
/**
* Initializer.
*/
class Initializer : public Object {
STORM_CLASS;
public:
// Assignment initialization.
STORM_CTOR Initializer(syntax::SStr *name, Expr *expr);
// Custom constructor.
STORM_CTOR Initializer(syntax::SStr *name, Actuals *params);
// Name of the variable.
syntax::SStr *name;
// Initialize by assignment (may be null).
MAYBE(Expr *) expr;
// Initialize to (may be null).
MAYBE(Actuals *) params;
// Output.
virtual void STORM_FN toS(StrBuf *to) const;
};
/**
* Initialize the class, optionally also calling the constructor of the super class.
*
* After this, 'this' will be available and of the proper type.
*/
class InitBlock : public Expr {
STORM_CLASS;
public:
// Create. Missing 'params' means that no parameters were provided.
STORM_CTOR InitBlock(SrcPos pos, CtorBody *block, MAYBE(Actuals *) params);
STORM_CTOR InitBlock(SrcPos pos, CtorBody *block, MAYBE(Actuals *) params, Array<Initializer *> *init);
// Add a new initializer.
void STORM_FN init(Initializer *init);
// Result.
virtual ExprResult STORM_FN result();
// Code.
virtual void STORM_FN code(CodeGen *s, CodeResult *r);
// To string.
virtual void STORM_FN toS(StrBuf *to) const;
// Don't isolate the initializer, that will break things.
virtual Bool STORM_FN isolate();
private:
// Member of.
Value thisPtr;
// This parameter.
LocalVar *thisVar;
// Scope.
Scope scope;
// Initializers. Remember the order so that we can initialize members in the order they
// appear in the source code.
Array<Initializer *> *initializers;
// Remember which variables have initializers.
Set<Str *> *initialized;
// Init.
void init(CtorBody *block, MAYBE(Actuals *) params);
// Initialize a variable.
void initVar(CodeGen *s, MemberVar *var, Initializer *to);
void initVarCtor(CodeGen *s, MemberVar *var, Actuals *to, Bool explicitCast);
void initVarAssign(CodeGen *s, MemberVar *var, Expr *to);
// Initialize a variable with its default constructor.
void initVarDefault(CodeGen *s, MemberVar *var);
// Initialize a variable with its default initializer.
void initVarInitializer(CodeGen *s, MemberVar *var);
};
/**
* Delegate initialization to another constructor. Can not be used together with separate
* 'super' or 'init' calls.
*/
class DelegateCall : public Expr {
STORM_CLASS;
public:
// Create.
STORM_CTOR DelegateCall(SrcPos pos, CtorBody *block, Actuals *params);
// Result.
virtual ExprResult STORM_FN result();
// Code.
virtual void STORM_FN code(CodeGen *s, CodeResult *r);
// To string.
virtual void STORM_FN toS(StrBuf *to) const;
// Don't isolate the delegate call.
virtual Bool STORM_FN isolate();
private:
// Member of.
Value thisPtr;
// This parameter.
LocalVar *thisVar;
// Block.
CtorBody *block;
// Actual parameters to the other constructor.
Actuals *params;
};
}
}
|