This repository has been archived by the owner on Sep 9, 2019. It is now read-only.
/
Emitter.class.php
833 lines (737 loc) · 19.3 KB
/
Emitter.class.php
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
<?php namespace xp\compiler\emit;
use xp\compiler\optimize\Optimization;
use xp\compiler\optimize\Optimizations;
use xp\compiler\checks\Checks;
use xp\compiler\checks\Check;
use xp\compiler\CompilationProfile;
use xp\compiler\types\Scope;
use xp\compiler\ast\ParseTree;
use xp\compiler\ast\Node;
/**
* Base class for emitters
*
* @see xp://xp.compiler.ast.Node
*/
abstract class Emitter extends \lang\Object implements \util\log\Traceable {
protected $cat= null;
protected $messages= [
'warnings' => [],
'errors' => []
];
protected $optimizations= null;
protected $checks= null;
protected $nativeImporter= null;
protected $scope= [null];
/**
* Constructor.
*/
public function __construct() {
$this->optimizations= new Optimizations();
$this->checks= new Checks();
}
/**
* Set profile
*
* @param xp.compiler.CompilationProfile
*/
public function setProfile(CompilationProfile $profile) {
$this->clearOptimizations();
$this->clearChecks();
foreach ($profile->warnings as $impl) {
$this->checks->add($impl, false);
}
foreach ($profile->errors as $impl) {
$this->checks->add($impl, true);
}
foreach ($profile->optimizations as $impl) {
$this->optimizations->add($impl);
}
}
/**
* Adds an optimization
*
* @param xp.compiler.optimize.Optimization o
* @return xp.compiler.optimize.Optimization
*/
public function addOptimization(Optimization $o) {
$this->optimizations->add($o);
return $o;
}
/**
* Adds an optimization
*
* @param xp.compiler.optimize.Optimization o
* @return self this
*/
public function withOptimization(Optimization $o) {
$this->optimizations->add($o);
return $this;
}
/**
* Clears all optimizations
*/
public function clearOptimizations() {
$this->optimizations->clear();
}
/**
* Adds a check
*
* @param xp.compiler.checks.Checks c
* @param bool error
* @return xp.compiler.checks.Check
*/
public function addCheck(Check $c, $error= false) {
$this->checks->add($c, $error);
return $c;
}
/**
* Adds a check
*
* @param xp.compiler.checks.Checks c
* @param bool error
* @return self this
*/
public function withCheck(Check $c, $error= false) {
$this->checks->add($c, $error);
return $this;
}
/**
* Clears all checks
*/
public function clearChecks() {
$this->checks->clear();
}
/**
* Enter the given scope
*
* @param xp.compiler.types.Scope
*/
protected function enter(Scope $s) {
array_unshift($this->scope, $this->scope[0]->enter($s));
}
/**
* Leave the current scope, returning to the previous
*
*/
protected function leave() {
array_shift($this->scope);
}
/**
* Emit uses statements for a given list of types
*
* @param xp.compiler.emit.Buffer b
* @param [:bool] types
*/
protected abstract function emitUses($b, array $types);
/**
* Emit invocations
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.InvocationNode inv
*/
protected abstract function emitInvocation($b, $inv);
/**
* Emit strings
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.StringNode str
*/
protected abstract function emitString($b, $str);
/**
* Emit an array (a sequence of elements with a zero-based index)
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ArrayNode arr
*/
protected abstract function emitArray($b, $arr);
/**
* Emit a map (a key/value pair dictionary)
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.MapNode map
*/
protected abstract function emitMap($b, $map);
/**
* Emit booleans
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.BooleanNode const
*/
protected abstract function emitBoolean($b, $const);
/**
* Emit null
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.NullNode const
*/
protected abstract function emitNull($b, $const);
/**
* Emit constants
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ConstantNode const
*/
protected abstract function emitConstant($b, $const);
/**
* Emit casts
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.CastNode cast
*/
protected abstract function emitCast($b, $cast);
/**
* Emit integers
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.IntegerNode num
*/
protected abstract function emitInteger($b, $num);
/**
* Emit decimals
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.DecimalNode num
*/
protected abstract function emitDecimal($b, $num);
/**
* Emit hex numbers
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.HexNode num
*/
protected abstract function emitHex($b, $num);
/**
* Emit octal numbers
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.OctalNode num
*/
protected abstract function emitOctal($b, $num);
/**
* Emit a variable
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.VariableNode var
*/
protected abstract function emitVariable($b, $var);
/**
* Emit a member access. Helper to emitChain()
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.DynamicVariableReferenceNode access
*/
protected abstract function emitDynamicMemberAccess($b, $access);
/**
* Emit static method call
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.StaticMethodCallNode call
*/
protected abstract function emitStaticMethodCall($b, $call);
/**
* Emit instance call
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.InstanceCallNode call
*/
protected abstract function emitInstanceCall($b, $call);
/**
* Emit method call
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.MethodCallNode call
*/
protected abstract function emitMethodCall($b, $call);
/**
* Emit member access
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.StaticMemberAccessNode access
*/
protected abstract function emitStaticMemberAccess($b, $access);
/**
* Emit member access
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.MemberAccessNode access
*/
protected abstract function emitMemberAccess($b, $access);
/**
* Emit array access
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ArrayAccessNode access
*/
protected abstract function emitArrayAccess($b, $access);
/**
* Emit constant access
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ConstantAccessNode access
*/
protected abstract function emitConstantAccess($b, $access);
/**
* Emit class access
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ClassAccessNode access
*/
protected abstract function emitClassAccess($b, $access);
/**
* Emit class name access
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ClassNameAccessNode access
*/
protected abstract function emitClassNameAccess($b, $access);
/**
* Emit a braced expression
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.BracedExpressionNode const
*/
protected abstract function emitBracedExpression($b, $braced);
/**
* Emit binary operation node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.BinaryOpNode bin
*/
protected abstract function emitBinaryOp($b, $bin);
/**
* Emit unary operation node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.UnaryOpNode un
*/
protected abstract function emitUnaryOp($b, $un);
/**
* Emit ternary operator node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.TernaryNode ternary
*/
protected abstract function emitTernary($b, $ternary);
/**
* Emit comparison node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ComparisonNode cmp
*/
protected abstract function emitComparison($b, $cmp);
/**
* Emit continue statement
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ContinueNode statement
*/
protected abstract function emitContinue($b, $statement);
/**
* Emit break statement
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.BreakNode statement
*/
protected abstract function emitBreak($b, $statement);
/**
* Emit noop
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.NoopNode statement
*/
protected abstract function emitNoop($b, $statement);
/**
* Emit with statement
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.WithNode with
*/
protected abstract function emitWith($b, $with);
/**
* Emit statements
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.StatementsNode statements
*/
protected abstract function emitStatements($b, $statements);
/**
* Emit foreach loop
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ForeachNode loop
*/
protected abstract function emitForeach($b, $loop);
/**
* Emit do ... while loop
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.DoNode loop
*/
protected abstract function emitDo($b, $loop);
/**
* Emit while loop
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.WhileNode loop
*/
protected abstract function emitWhile($b, $loop);
/**
* Emit for loop
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ForNode loop
*/
protected abstract function emitFor($b, $loop);
/**
* Emit if statement
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.IfNode if
*/
protected abstract function emitIf($b, $if);
/**
* Emit a switch case
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.CaseNode case
*/
protected abstract function emitCase($b, $case);
/**
* Emit the switch default case
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.DefaultNode default
*/
protected abstract function emitDefault($b, $default);
/**
* Emit switch statement
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.SwitchNode switch
*/
protected abstract function emitSwitch($b, $switch);
/**
* Emit a try / catch block
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.TryNode try
*/
protected abstract function emitTry($b, $try);
/**
* Emit an automatic resource management (ARM) block
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ArmNode arm
*/
protected abstract function emitArm($b, $arm);
/**
* Emit a throw node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ThrowNode throw
*/
protected abstract function emitThrow($b, $throw);
/**
* Emit a finally node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.FinallyNode finally
*/
protected abstract function emitFinally($b, $finally);
/**
* Emit a dynamic instance creation node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.DynamicInstanceCreationNode new
*/
protected abstract function emitDynamicInstanceCreation($b, $new);
/**
* Emit an instance creation node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.InstanceCreationNode new
*/
protected abstract function emitInstanceCreation($b, $new);
/**
* Emit an assignment
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.AssignmentNode assign
*/
protected abstract function emitAssignment($b, $assign);
/**
* Emit an operator
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.OperatorNode method
*/
protected abstract function emitOperator($b, $operator);
/**
* Emit a lambda
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.LambdaNode lambda
*/
protected abstract function emitLambda($b, $lambda);
/**
* Emit a method
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.MethodNode method
*/
protected abstract function emitMethod($b, $method);
/**
* Emit static initializer
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.StaticInitializerNode initializer
*/
protected abstract function emitStaticInitializer($b, $initializer);
/**
* Emit a constructor
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ConstructorNode constructor
*/
protected abstract function emitConstructor($b, $constructor);
/**
* Emit a class property
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.IndexerNode indexer
*/
protected abstract function emitIndexer($b, $indexer);
/**
* Emit a class property
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.PropertyNode property
*/
protected abstract function emitProperty($b, $property);
/**
* Emit an enum member
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.EnumMemberNode member
*/
protected abstract function emitEnumMember($b, $member);
/**
* Emit a class constant
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ClassConstantNode const
*/
protected abstract function emitClassConstant($b, $const);
/**
* Emit a class field
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.FieldNode field
*/
protected abstract function emitField($b, $field);
/**
* Emit an enum declaration
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.EnumNode declaration
*/
protected abstract function emitEnum($b, $declaration);
/**
* Emit a Interface declaration
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.InterfaceNode declaration
*/
protected abstract function emitInterface($b, $declaration);
/**
* Emit a class declaration
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ClassNode declaration
*/
protected abstract function emitClass($b, $declaration);
/**
* Emit dynamic instanceof
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.DynamicInstanceOfNode instanceof
*/
protected abstract function emitDynamicInstanceOf($b, $instanceof);
/**
* Emit instanceof
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.InstanceOfNode instanceof
*/
protected abstract function emitInstanceOf($b, $instanceof);
/**
* Emit clone
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.CloneNode clone
*/
protected abstract function emitClone($b, $clone);
/**
* Emit import
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ImportNode import
*/
protected abstract function emitImport($b, $import);
/**
* Emit native import
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.NativeImportNode import
*/
protected abstract function emitNativeImport($b, $import);
/**
* Emit static import
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.StaticImportNode import
*/
protected abstract function emitStaticImport($b, $import);
/**
* Emit a return statement
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.ReturnNode new
*/
protected abstract function emitReturn($b, $return);
/**
* Emit a silence node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.SilenceOperatorNode silenced
*/
protected abstract function emitSilenceOperator($b, $silenced);
/**
* Emit a yield node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.YieldNode yield
*/
protected abstract function emitYield($b, $yield);
/**
* Emit a single node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.Node in
*/
protected function emitOne($b, $in) {
$node= $this->optimizations->optimize($in, $this->scope[0]);
$b->position($node->position);
$this->cat && $this->cat->debugf(
'@%-3d Emit %s: %s',
$node->position[0],
nameof($node),
$node->hashCode()
);
try {
$this->checks->verify($node, $this->scope[0], $this) && call_user_func(
[$this, 'emit'.substr(get_class($node), 16, -4)], // strlen('xp\\compiler\\ast\\'), strlen
$b,
$node
);
} catch (\lang\Error $e) {
$this->error('0422', 'Cannot emit '.nameof($node).': '.$e->getMessage(), $node);
} catch (\lang\Throwable $e) {
$this->error('0500', $e->toString(), $node);
}
}
/**
* Emit all given nodes
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.Node[] nodes
*/
protected abstract function emitAll($b, array $nodes);
/**
* Entry point
*
* @param xp.compiler.ast.ParseTree tree
* @param xp.compiler.types.Scope scope
* @return xp.compiler.emit.EmitterResult
*/
public abstract function emit(ParseTree $tree, Scope $scope);
/**
* Format a message
*
* @param string code
* @param string message
* @param xp.compiler.ast.Node context
* @return string
*/
protected function format($code, $message, Node $context= null) {
if ($context) { // Use given context node
$pos= $context->position;
} else { // Try to determine last context node from backtrace
$pos= [0, 0];
foreach ((new \lang\Throwable(null))->getStackTrace() as $element) {
if (
'emit' == substr($element->method, 0, 4) &&
sizeof($element->args) > 1 &&
$element->args[1] instanceof Node
) {
$pos= $element->args[1]->position;
break;
}
}
}
return sprintf('[%4s] %s at line %d, offset %d', $code, $message, $pos[0], $pos[1]);
}
/**
* Clears messages
*/
public function clearMessages() {
$this->messages= [
'warnings' => [],
'errors' => []
];
}
/**
* Issue a warning
*
* @param string code
* @param string message
* @param xp.compiler.ast.Node context
*/
public function warn($code, $message, Node $context= null) {
$message= $this->format($code, $message, $context);
$this->cat && $this->cat->warn($message);
$this->messages['warnings'][]= $message;
}
/**
* Raise an error
*
* @param string code
* @param string message
* @param xp.compiler.ast.Node context
*/
public function error($code, $message, Node $context= null) {
$message= $this->format($code, $message, $context);
$this->cat && $this->cat->error($message);
$this->messages['errors'][]= $message;
}
/**
* Get a list of all messages
*
* @return string[] messages
*/
public function messages() {
$r= [];
foreach ($this->messages as $type => $messages) {
$r+= $messages;
}
return $r;
}
/**
* Set a trace for debugging
*
* @param util.log.LogCategory cat
*/
public function setTrace($cat) {
$this->cat= $cat;
}
}