-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.py
676 lines (394 loc) · 15.8 KB
/
compiler.py
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# -*- coding: utf-8 -*-
# This file is part of the pymfony package.
#
# (c) Alexandre Quercia <[email protected]>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
from __future__ import absolute_import;
from pymfony.component.system import Object;
from pymfony.component.system import ClassLoader;
from pymfony.component.system.reflection import ReflectionObject;
from pymfony.component.system.types import OrderedDict;
from pymfony.component.dependency.exception import InvalidArgumentException;
from pymfony.component.dependency.definition import Alias;
from pymfony.component.dependency.definition import Definition;
from pymfony.component.dependency.interface import CompilerPassInterface;
"""
"""
class PassConfig(Object):
"""Compiler Pass Configuration
This class has(, a default configuration embedded.):
@author: Johannes M. Schmitt <[email protected]>
@api
"""
TYPE_AFTER_REMOVING = 'AfterRemoving';
TYPE_BEFORE_OPTIMIZATION = 'BeforeOptimization';
TYPE_BEFORE_REMOVING = 'BeforeRemoving';
TYPE_OPTIMIZE = 'Optimization';
TYPE_REMOVE = 'Removing';
def __init__(self):
"""Constructor.
"""
self.__mergePass = ClassLoader.load(__name__+'pass.MergeExtensionConfigurationPass')();
self.__beforeOptimizationPasses = list();
self.__afterRemovingPasses = list();
self.__beforeRemovingPasses = list();
self.__optimizationPasses = [
ClassLoader.load(__name__+'pass.ResolveDefinitionTemplatesPass')(),
ClassLoader.load(__name__+'pass.ResolveParameterPlaceHoldersPass')(),
ClassLoader.load(__name__+'pass.CheckDefinitionValidityPass')(),
ClassLoader.load(__name__+'pass.ResolveReferencesToAliasesPass')(),
ClassLoader.load(__name__+'pass.ResolveInvalidReferencesPass')(),
ClassLoader.load(__name__+'pass.AnalyzeServiceReferencesPass')(),
ClassLoader.load(__name__+'pass.CheckCircularReferencesPass')(),
ClassLoader.load(__name__+'pass.CheckReferenceValidityPass')(),
];
self.__removingPasses = [
ClassLoader.load(__name__+'pass.RemovePrivateAliasesPass')(),
ClassLoader.load(__name__+'pass.RemoveAbstractDefinitionsPass')(),
ClassLoader.load(__name__+'pass.ReplaceAliasByActualDefinitionPass')(),
ClassLoader.load(__name__+'pass.RepeatedPass')([
ClassLoader.load(__name__+'pass.AnalyzeServiceReferencesPass')(),
ClassLoader.load(__name__+'pass.InlineServiceDefinitionsPass')(),
ClassLoader.load(__name__+'pass.AnalyzeServiceReferencesPass')(),
ClassLoader.load(__name__+'pass.RemoveUnusedDefinitionsPass')(),
]),
ClassLoader.load(__name__+'pass.CheckExceptionOnInvalidReferenceBehaviorPass')(),
];
def getPasses(self):
"""Returns all passes in order to be processed.
@return: list An array of all passes to process
@api
"""
passes = list();
if self.__mergePass:
passes.append(self.__mergePass);
passes.extend(self.__beforeOptimizationPasses);
passes.extend(self.__optimizationPasses);
passes.extend(self.__beforeRemovingPasses);
passes.extend(self.__removingPasses);
passes.extend(self.__afterRemovingPasses);
return passes;
def addPass(self, cPass, cType=TYPE_BEFORE_OPTIMIZATION):
"""Adds a pass.
@param: CompilerPassInterface pass A Compiler pass
@param string type The pass type
@raise InvalidArgumentException when a pass type doesn't exist
@api
"""
assert isinstance(cPass, CompilerPassInterface);
getPropertyName = "get{0}Passes".format(cType);
setPropertyName = "set{0}Passes".format(cType);
if not hasattr(self, getPropertyName):
raise InvalidArgumentException(
'Invalid type "{0}".'.format(cType)
);
passes = getattr(self, getPropertyName)();
passes.append(cPass);
getattr(self, setPropertyName)(passes);
def getAfterRemovingPasses(self):
"""Gets all passes for the AfterRemoving pass.
@return: list An array of passes
@api
"""
return self.__afterRemovingPasses;
def getBeforeOptimizationPasses(self):
"""Gets all passes for the BeforeOptimization pass.
@return: list An array of passes
@api
"""
return self.__beforeOptimizationPasses;
def getBeforeRemovingPasses(self):
"""Gets all passes for the BeforeRemoving pass.
@return: list An array of passes
@api
"""
return self.__beforeRemovingPasses;
def getOptimizationPasses(self):
"""Gets all passes for the Optimization pass.
@return: list An array of passes
@api
"""
return self.__optimizationPasses;
def getRemovingPasses(self):
"""Gets all passes for the Removing pass.
@return: list An array of passes
@api
"""
return self.__removingPasses;
def getMergePass(self):
"""Gets all passes for the Merge pass.
@return: CompilerPassInterface A merge pass # FIXED
@api
"""
return self.__mergePass;
def setMergePass(self, mergePass):
"""Sets the Merge Pass.
@param: CompilerPassInterface cPass The merge pass
@api
"""
assert isinstance(mergePass, CompilerPassInterface);
self.__mergePass = mergePass;
def setAfterRemovingPasses(self, passes):
"""Sets the AfterRemoving passes.
@param: list passes An array of passes
@api
"""
assert isinstance(passes, list);
self.__afterRemovingPasses = passes;
def setBeforeOptimizationPasses(self, passes):
"""Sets the BeforeOptimization passes.
@param: list passes An array of passes
@api
"""
assert isinstance(passes, list);
self.__beforeOptimizationPasses = passes;
def setBeforeRemovingPasses(self, passes):
"""Sets the BeforeRemoving passes.
@param: list passes An array of passes
@api
"""
assert isinstance(passes, list);
self.__beforeRemovingPasses = passes;
def setOptimizationPasses(self, passes):
"""Sets the Optimization passes.
@param: array passes An array of passes
@api
"""
assert isinstance(passes, list);
self.__optimizationPasses = passes;
def setRemovingPasses(self, passes):
"""Sets the Removing passes.
@param: array passes An array of passes
@api
"""
assert isinstance(passes, list);
self.__removingPasses = passes;
class Compiler(Object):
"""This class is(, used to remove circular dependencies between individual passes.):
@author: Johannes M. Schmitt <[email protected]>
@api
"""
def __init__(self):
"""Constructor.
"""
self.__passConfig = PassConfig();
self.__log = list();
self.__loggingFormatter = LoggingFormatter();
self.__serviceReferenceGraph = ServiceReferenceGraph();
def getPassConfig(self):
"""Returns the PassConfig.
@return: PassConfig The PassConfig instance
@api
"""
return self.__passConfig;
def getServiceReferenceGraph(self):
"""Returns the ServiceReferenceGraph.
@return: ServiceReferenceGraph The ServiceReferenceGraph instance
@api
"""
return self.__serviceReferenceGraph;
def getLoggingFormatter(self):
"""Returns the logging formatter which can be used by compilation passes.
@return: LoggingFormatter
"""
return self.__loggingFormatter;
def addPass(self, cPass, cType=PassConfig.TYPE_BEFORE_OPTIMIZATION):
"""Adds a pass to the PassConfig.
@param: CompilerPassInterface pass A compiler pass
@param string type The type of the pass
@api
"""
assert isinstance(cPass, CompilerPassInterface);
self.__passConfig.addPass(cPass, cType);
def addLogMessage(self, string):
"""Adds a log message.
@param: string string The log message
"""
self.__log.append(string);
def getLog(self):
"""Returns the log.
@return: list Log array
"""
return self.__log;
def compile(self, container):
"""Run the Compiler and process all Passes.
@param: ContainerBuilder container
@api
"""
for cPass in self.__passConfig.getPasses():
cPass.process(container);
class LoggingFormatter(Object):
"""Used to format logging messages during the compilation.
@author: Johannes M. Schmitt <[email protected]>
"""
def formatRemoveService(self, cpass, identifier, reason):
assert isinstance(cpass, CompilerPassInterface);
return self.format(cpass, 'Removed service "{0}"; reason: {1}'.format(identifier, reason));
def formatInlineService(self, cpass, identifier, target):
assert isinstance(cpass, CompilerPassInterface);
return self.format(cpass, 'Inlined service "{0}" to "{1}".'.format(identifier, target));
def formatUpdateReference(self, cpass, serviceId, oldDestId, newDestId):
assert isinstance(cpass, CompilerPassInterface);
return self.format(cpass, 'Changed reference of service "{0}" previously pointing to "{1}" to "{2}".'.format(serviceId, oldDestId, newDestId));
def formatResolveInheritance(self, cpass, childId, parentId):
assert isinstance(cpass, CompilerPassInterface);
return self.format(cpass, 'Resolving inheritance for "{0}" (parent: {1}).'.format(childId, parentId));
def format(self, cpass, message):
assert isinstance(cpass, CompilerPassInterface);
return '{0}: {1}'.format(ReflectionObject(cpass).getName(), message);
class ServiceReferenceGraph(Object):
"""This is a directed graph of your services.
This information can be used by your compiler passes instead of collecting
it themselves which improves performance quite a lot.
@author: Johannes M. Schmitt <[email protected]>
"""
def __init__(self):
"""Constructor.
"""
self.__nodes = None;
self.clear();
def hasNode(self, identifier):
"""Checks if the graph has a specific node.:
@param: string id Id to check
@return Boolean
"""
return identifier in self.__nodes;
def getNode(self, identifier):
"""Gets a node by identifier.:
@param: string id The id to retrieve
@return ServiceReferenceGraphNode The node matching the supplied identifier:
@raise InvalidArgumentException if no node matches the supplied identifier:
"""
if identifier not in self.__nodes :
raise InvalidArgumentException(
'There is no node with id "{0}".'.format(identifier)
);
return self.__nodes[identifier];
def getNodes(self):
"""Returns all nodes.
@return: ServiceReferenceGraphNode[] An array of all ServiceReferenceGraphNode objects
"""
return self.__nodes;
def clear(self):
"""Clears all nodes.
"""
self.__nodes = OrderedDict();
def connect(self, sourceId, sourceValue, destId, destValue = None, reference = None):
"""Connects 2 nodes together in the Graph.
@param: string sourceId
@param string sourceValue
@param string destId
@param string destValue
@param string reference
"""
sourceNode = self.__createNode(sourceId, sourceValue);
destNode = self.__createNode(destId, destValue);
edge = ServiceReferenceGraphEdge(sourceNode, destNode, reference);
sourceNode.addOutEdge(edge);
destNode.addInEdge(edge);
def __createNode(self, identifier, value):
"""Creates a graph node.
@param: string id
@param string value
@return ServiceReferenceGraphNode
"""
if identifier in self.__nodes and self.__nodes[identifier].getValue() == value :
return self.__nodes[identifier];
self.__nodes[identifier] = ServiceReferenceGraphNode(identifier, value)
return self.__nodes[identifier];
class ServiceReferenceGraphEdge(Object):
"""Represents an edge in your service graph.
Value is typically a reference.
@author: Johannes M. Schmitt <[email protected]>
"""
def __init__(self, sourceNode, destNode, value = None):
"""Constructor.
@param: ServiceReferenceGraphNode sourceNode
@param ServiceReferenceGraphNode destNode
@param string value
"""
assert isinstance(destNode, ServiceReferenceGraphNode);
assert isinstance(sourceNode, ServiceReferenceGraphNode);
self.__sourceNode = None;
self.__destNode = None;
self.__value = None;
self.__sourceNode = sourceNode;
self.__destNode = destNode;
self.__value = value;
def getValue(self):
"""Returns the value of the edge
@return: ServiceReferenceGraphNode
"""
return self.__value;
def getSourceNode(self):
"""Returns the source node
@return: ServiceReferenceGraphNode
"""
return self.__sourceNode;
def getDestNode(self):
"""Returns the destination node
@return: ServiceReferenceGraphNode
"""
return self.__destNode;
class ServiceReferenceGraphNode(Object):
"""Represents a node in your service graph.
Value is typically a definition, or an alias.
@author: Johannes M. Schmitt <[email protected]>
"""
def __init__(self, identifier, value):
"""Constructor.
@param: string id The node identifier:
@param mixed value The node value
"""
self.__id = None;
self.__inEdges = None;
self.__outEdges = None;
self.__value = None;
self.__id = identifier;
self.__value = value;
self.__inEdges = list();
self.__outEdges = list();
def addInEdge(self, edge):
"""Adds an in edge to this node.
@param: ServiceReferenceGraphEdge edge
"""
assert isinstance(edge, ServiceReferenceGraphEdge);
self.__inEdges.append(edge);
def addOutEdge(self, edge):
"""Adds an out edge to this node.
@param: ServiceReferenceGraphEdge edge
"""
assert isinstance(edge, ServiceReferenceGraphEdge);
self.__outEdges.append(edge);
def isAlias(self):
"""Checks if the value of this node is an Alias.:
@return: Boolean True if the value is an Alias instance:
"""
return isinstance(self.__value, Alias);
def isDefinition(self):
"""Checks if the value of this node is a Definition.:
@return: Boolean True if the value is a Definition instance:
"""
return isinstance(self.__value, Definition);
def getId(self):
"""Returns the identifier.:
@return: string
"""
return self.__id;
def getInEdges(self):
"""Returns the in edges.
@return: list The in ServiceReferenceGraphEdge array
"""
return self.__inEdges;
def getOutEdges(self):
"""Returns the out edges.
@return: list The out ServiceReferenceGraphEdge array
"""
return self.__outEdges;
def getValue(self):
"""Returns the value of this Node
@return: mixed The value
"""
return self.__value;