-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstallerContext.cs
451 lines (392 loc) · 16.4 KB
/
InstallerContext.cs
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* **************************************************************************
*
* Copyright (c) The IronSmalltalk Project.
*
* This source code is subject to terms and conditions of the
* license agreement found in the solution directory.
* See: $(SolutionDir)\License.htm ... in the root of this distribution.
* By using this source code in any fashion, you are agreeing
* to be bound by the terms of the license agreement.
*
* You must not remove this notice, or any other, from this software.
*
* **************************************************************************
*/
using System;
using System.Collections.Generic;
using System.Linq;
using IronSmalltalk.Runtime.Installer.Definitions;
using IronSmalltalk.Runtime.Internal;
using IronSmalltalk.Runtime.Bindings;
namespace IronSmalltalk.Runtime.Installer
{
/// <summary>
/// Installer context encapsulates and represents the transaction that is
/// associated with installing definitions (sources) into the smalltalk context.
/// </summary>
/// <remarks>
/// The lifespan of definition installation follows the following pattern:
/// 1. Create a new InstallerContext
/// 2. Add definitions to the InstallerContext (read and process source files).
/// ... those are kept in memory as definition objects until next phase.
/// 3. Create real runtime objects, but in the local context by calling the Install() method.
/// 4. Modify the running SmalltalkContext with the newly created objects.
/// 5. Run Initializers to initialize stuff (this is done outside the transaction).
/// </remarks>
public class InstallerContext : IInstallerContext
{
private List<GlobalBase> _globals = new List<GlobalBase>();
private List<PoolValueDefinition> _poolVariables = new List<PoolValueDefinition>();
private List<MethodDefinition> _methods = new List<MethodDefinition>();
private List<InitializerDefinition> _initializers = new List<InitializerDefinition>();
private List<SmalltalkClass> _newClasses = new List<SmalltalkClass>();
/// <summary>
/// Smalltalk context that this installation is part of.
/// </summary>
public SmalltalkRuntime Runtime { get; private set; }
/// <summary>
/// Optional error sink for reporting errors.
/// </summary>
public IInstallErrorSink ErrorSink { get; set; }
/// <summary>
/// Determines if meta-annotations (comments, documentation, etc.)
/// are installed (saved) in the corresponding runtime objects.
/// </summary>
public bool InstallMetaAnnotations { get; set; }
public InstallerContext(SmalltalkRuntime runtime)
{
if (runtime == null)
throw new ArgumentNullException("runtime");
this.Runtime = runtime;
this.InstallMetaAnnotations = false;
}
#region Preparation
/// <summary>
/// Add a class definition to the installation context.
/// </summary>
/// <param name="definition">Definition of the class to be added.</param>
public void AddClass(ClassDefinition definition)
{
this._globals.Add(definition);
}
/// <summary>
/// Add a global (variable or constant) definition to the installation context.
/// </summary>
/// <param name="definition">Definition of the global to be added.</param>
public void AddGlobal(GlobalDefinition definition)
{
this._globals.Add(definition);
}
/// <summary>
/// Add a global initializer definition to the installation context.
/// </summary>
/// <param name="definition">Definition of the global initializer to be added.</param>
public void AddGlobalInitializer(GlobalInitializer initializer)
{
this._initializers.Add(initializer);
}
/// <summary>
/// Add a (instance or class) method definition to the installation context.
/// </summary>
/// <param name="definition">Definition of the method to be added.</param>
public void AddMethod(MethodDefinition definition)
{
this._methods.Add(definition);
}
/// <summary>
/// Add a pool definition to the installation context.
/// </summary>
/// <param name="definition">Definition of the pool to be added.</param>
public void AddPool(PoolDefinition definition)
{
this._globals.Add(definition);
}
/// <summary>
/// Add a pool variable or pool constant definition to the installation context.
/// </summary>
/// <param name="definition">Definition of the pool variable or pool constant to be added.</param>
public void AddPoolVariable(PoolValueDefinition definition)
{
this._poolVariables.Add(definition);
}
/// <summary>
/// Add a pool variable or pool constant initializer definition to the installation context.
/// </summary>
/// <param name="definition">Definition of the initializer to be added.</param>
public void AddPoolVariableInitializer(PoolVariableInitializer initializer)
{
this._initializers.Add(initializer);
}
/// <summary>
/// Add a program initializer definition to the installation context.
/// </summary>
/// <param name="definition">Definition of the initializer to be added.</param>
public void AddProgramInitializer(ProgramInitializer initializer)
{
this._initializers.Add(initializer);
}
#endregion
#region Install
public SmalltalkNameScope NameScope { get; protected set; }
public bool Install()
{
this.CreateTemporaryNameSpace();
if (!this.CreateGlobalBindings())
return false;
if (!this.CreateGlobalObjects())
return false;
if (!this.ValidateGlobalObjects())
return false;
if (!this.CreatePoolVariableBindings())
return false;
if (!this.ValidateMethods())
return false;
if (!this.ValidateInitializers())
return false;
if (!this.CreateMethods())
return false;
if (!this.AddAnnotation())
return false;
this.ReplaceSmalltalkContextNameSpace();
return this.RecompileClasses(); // Must be after ReplaceSmalltalkContextNameSpace(), otherwise class cannot find subclasses.
}
protected virtual void CreateTemporaryNameSpace()
{
if (this.NameScope != null)
throw new InvalidOperationException("Install phase has commenced.");
this.NameScope = this.Runtime.GlobalScope.Copy();
}
private bool CreateGlobalBindings()
{
bool result = true;
foreach(GlobalBase def in this._globals)
result = (result & def.CreateGlobalBinding(this));
return result;
}
private bool CreateGlobalObjects()
{
bool result = true;
foreach (GlobalBase def in this._globals)
result = (result & def.CreateGlobalObject(this));
return result;
}
private bool ValidateGlobalObjects()
{
bool result = true;
foreach (GlobalBase def in this._globals)
result = (result & def.ValidateObject(this));
return result;
}
private bool CreatePoolVariableBindings()
{
bool result = true;
foreach (PoolValueDefinition def in this._poolVariables)
result = (result & def.CreatePoolVariableBinding(this));
return result;
}
private bool ValidateMethods()
{
bool result = true;
foreach (MethodDefinition def in this._methods)
result = (result & def.ValidateMethod(this));
return result;
}
private bool ValidateInitializers()
{
bool result = true;
foreach (InitializerDefinition def in this._initializers)
result = (result & def.ValidateInitializer(this));
return result;
}
private bool CreateMethods()
{
bool result = true;
foreach (MethodDefinition def in this._methods)
result = (result & def.CreateMethod(this));
return result;
}
private bool AddAnnotation()
{
bool result = true;
foreach (GlobalBase def in this._globals)
result = (result & def.AnnotateObject(this));
foreach (PoolValueDefinition def in this._poolVariables)
result = (result & def.AnnotateObject(this));
foreach (MethodDefinition def in this._methods)
result = (result & def.AnnotateObject(this));
foreach (InitializerDefinition def in this._initializers)
result = (result & def.AnnotateObject(this));
return result;
}
protected virtual void ReplaceSmalltalkContextNameSpace()
{
if (this.NameScope == null)
throw new InvalidOperationException("Install phase has not commenced.");
this.Runtime.SetGlobalScope(this.NameScope);
}
private bool RecompileClasses()
{
List<SmalltalkClass> toRecompile = new List<SmalltalkClass>();
foreach(SmalltalkClass cls in this._newClasses)
{
// Do not recompile classes that we are going to recompile anyway
bool subclassOfRecompiled = false;
foreach(SmalltalkClass c in this._newClasses)
{
if (this.InheritsFrom(cls, c))
{
subclassOfRecompiled = true;
break;
}
}
if (!subclassOfRecompiled)
toRecompile.Add(cls);
}
bool success = true;
foreach (SmalltalkClass cls in toRecompile)
{
try
{
cls.Recompile();
}
catch (SmalltalkDefinitionException ex)
{
if (this.ErrorSink != null)
this.ErrorSink.AddInstallError(ex.Message, InvalidSourceReference.Current);
success = false;
}
}
return success;
}
private bool InheritsFrom(SmalltalkClass self, SmalltalkClass cls)
{
while (self != null)
{
if (self.Superclass == cls)
return true;
self = self.Superclass;
}
return false;
}
#endregion
public void Initialize()
{
foreach (InitializerDefinition def in this._initializers)
def.Execute(this.Runtime);
}
#region IInstallerContext interface implementation
bool IInstallerContext.ReportError(ISourceReference sourceReference, string errorMessage)
{
if (this.ErrorSink != null)
this.ErrorSink.AddInstallError(errorMessage, sourceReference);
// This value has no mening to us, but makes it easier for senders to use us and return <false> directly.
return false;
}
void IInstallerContext.RegisterNewClass(SmalltalkClass cls)
{
if (cls == null)
throw new ArgumentNullException();
this._newClasses.Add(cls);
}
void IInstallerContext.AddClassBinding(ClassBinding binding)
{
this.NameScope.Classes.Add(binding);
}
void IInstallerContext.AddGlobalConstantBinding(GlobalConstantBinding binding)
{
this.NameScope.GlobalConstants.Add(binding);
}
void IInstallerContext.AddGlobalVariableBinding(GlobalVariableBinding binding)
{
this.NameScope.GlobalVariables.Add(binding);
}
void IInstallerContext.AddPoolBinding(PoolBinding binding)
{
this.NameScope.Pools.Add(binding);
}
PoolBinding IInstallerContext.GetPoolBinding(Symbol name)
{
return this.NameScope.GetPoolBinding(name);
}
PoolBinding IInstallerContext.GetPoolBinding(string name)
{
return this.NameScope.GetPoolBinding(name);
}
GlobalVariableOrConstantBinding IInstallerContext.GetGlobalVariableOrConstantBinding(Symbol name)
{
return this.NameScope.GetGlobalVariableOrConstantBinding(name);
}
GlobalVariableOrConstantBinding IInstallerContext.GetGlobalVariableOrConstantBinding(string name)
{
return this.NameScope.GetGlobalVariableOrConstantBinding(name);
}
IDiscreteGlobalBinding IInstallerContext.GetLocalGlobalBinding(Symbol name)
{
return this.NameScope.GetLocalGlobalBinding(name);
}
PoolBinding IInstallerContext.GetLocalPoolBinding(Symbol name)
{
PoolBinding binding;
this.NameScope.Pools.TryGetValue(name, out binding);
return binding;
}
ClassBinding IInstallerContext.GetLocalClassBinding(Symbol name)
{
ClassBinding binding;
this.NameScope.Classes.TryGetValue(name, out binding);
return binding;
}
ClassBinding IInstallerContext.GetClassBinding(Symbol name)
{
return this.NameScope.GetClassBinding(name);
}
ClassBinding IInstallerContext.GetClassBinding(string name)
{
return this.NameScope.GetClassBinding(name);
}
bool IInstallerContext.IsProtectedName(Symbol name)
{
return this.NameScope.IsProtectedName(name);
}
bool IInstallerContext.AnnotateObject(IAnnotetable annotetableObject, IEnumerable<KeyValuePair<string, string>> annotations)
{
if (annotetableObject == null)
return false;
if (annotations == null)
return false;
foreach (var pair in annotations)
{
if (!String.IsNullOrEmpty(pair.Key))
{
if (this.InstallMetaAnnotations || !pair.Key.StartsWith("ist.meta."))
annotetableObject.Annotate(pair.Key, pair.Value);
}
}
return true;
}
#endregion
}
public interface IInstallerContext
{
SmalltalkRuntime Runtime { get; }
void RegisterNewClass(SmalltalkClass cls);
void AddClassBinding(ClassBinding binding);
void AddGlobalConstantBinding(GlobalConstantBinding binding);
void AddGlobalVariableBinding(GlobalVariableBinding binding);
void AddPoolBinding(PoolBinding binding);
IDiscreteGlobalBinding GetLocalGlobalBinding(Symbol name);
ClassBinding GetLocalClassBinding(Symbol name);
ClassBinding GetClassBinding(Symbol name);
ClassBinding GetClassBinding(string name);
PoolBinding GetLocalPoolBinding(Symbol name);
PoolBinding GetPoolBinding(Symbol name);
PoolBinding GetPoolBinding(string name);
GlobalVariableOrConstantBinding GetGlobalVariableOrConstantBinding(Symbol name);
GlobalVariableOrConstantBinding GetGlobalVariableOrConstantBinding(string name);
bool IsProtectedName(Symbol name);
bool ReportError(ISourceReference sourceReference, string errorMessage);
bool AnnotateObject(IAnnotetable annotetableObject, IEnumerable<KeyValuePair<string, string>> annotations);
SmalltalkNameScope NameScope { get; }
}
}