This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNodeFactory.cs
More file actions
1429 lines (1254 loc) · 53.4 KB
/
NodeFactory.cs
File metadata and controls
1429 lines (1254 loc) · 53.4 KB
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <license file="IRFactory.cs">
//
// The use and distribution terms for this software are contained in the file
// named 'LICENSE', which can be found in the resources directory of this
// distribution.
//
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// </license>
//------------------------------------------------------------------------------
using System;
using EcmaScript.NET.Collections;
namespace EcmaScript.NET
{
/// <summary> This class allows the creation of nodes, and follows the Factory pattern.
///
/// </summary>
sealed class NodeFactory
{
internal NodeFactory (Parser parser)
{
this.parser = parser;
}
internal ScriptOrFnNode CreateScript ()
{
return new ScriptOrFnNode (Token.SCRIPT);
}
/// <summary> Script (for associating file/url names with toplevel scripts.)</summary>
internal void initScript (ScriptOrFnNode scriptNode, Node body)
{
Node children = body.FirstChild;
if (children != null) {
scriptNode.addChildrenToBack (children);
}
}
/// <summary> Leaf</summary>
internal Node CreateLeaf (int nodeType)
{
return new Node (nodeType);
}
internal Node CreateLeaf (int nodeType, int nodeOp)
{
return new Node (nodeType, nodeOp);
}
/// <summary> Statement leaf nodes.</summary>
internal Node CreateSwitch (Node expr, int lineno)
{
//
// The switch will be rewritten from:
//
// switch (expr) {
// case test1: statements1;
// ...
// default: statementsDefault;
// ...
// case testN: statementsN;
// }
//
// to:
//
// {
// switch (expr) {
// case test1: goto label1;
// ...
// case testN: goto labelN;
// }
// goto labelDefault;
// label1:
// statements1;
// ...
// labelDefault:
// statementsDefault;
// ...
// labelN:
// statementsN;
// breakLabel:
// }
//
// where inside switch each "break;" without label will be replaced
// by "goto breakLabel".
//
// If the original switch does not have the default label, then
// the transformed code would contain after the switch instead of
// goto labelDefault;
// the following goto:
// goto breakLabel;
//
Node.Jump switchNode = new Node.Jump (Token.SWITCH, expr, lineno);
Node block = new Node (Token.BLOCK, switchNode);
return block;
}
/// <summary> If caseExpression argument is null it indicate default label.</summary>
internal void addSwitchCase (Node switchBlock, Node caseExpression, Node statements)
{
if (switchBlock.Type != Token.BLOCK)
throw Context.CodeBug ();
Node.Jump switchNode = (Node.Jump)switchBlock.FirstChild;
if (switchNode.Type != Token.SWITCH)
throw Context.CodeBug ();
Node gotoTarget = Node.newTarget ();
if (caseExpression != null) {
Node.Jump caseNode = new Node.Jump (Token.CASE, caseExpression);
caseNode.target = gotoTarget;
switchNode.addChildToBack (caseNode);
}
else {
switchNode.Default = gotoTarget;
}
switchBlock.addChildToBack (gotoTarget);
switchBlock.addChildToBack (statements);
}
internal void closeSwitch (Node switchBlock)
{
if (switchBlock.Type != Token.BLOCK)
throw Context.CodeBug ();
Node.Jump switchNode = (Node.Jump)switchBlock.FirstChild;
if (switchNode.Type != Token.SWITCH)
throw Context.CodeBug ();
Node switchBreakTarget = Node.newTarget ();
// switchNode.target is only used by NodeTransformer
// to detect switch end
switchNode.target = switchBreakTarget;
Node defaultTarget = switchNode.Default;
if (defaultTarget == null) {
defaultTarget = switchBreakTarget;
}
switchBlock.addChildAfter (makeJump (Token.GOTO, defaultTarget), switchNode);
switchBlock.addChildToBack (switchBreakTarget);
}
internal Node CreateVariables (int lineno)
{
return new Node (Token.VAR, lineno);
}
internal Node CreateExprStatement (Node expr, int lineno)
{
int type;
if (parser.insideFunction ()) {
type = Token.EXPR_VOID;
}
else {
type = Token.EXPR_RESULT;
}
return new Node (type, expr, lineno);
}
internal Node CreateExprStatementNoReturn (Node expr, int lineno)
{
return new Node (Token.EXPR_VOID, expr, lineno);
}
internal Node CreateDefaultNamespace (Node expr, int lineno)
{
// default xml namespace requires activation
setRequiresActivation ();
Node n = CreateUnary (Token.DEFAULTNAMESPACE, expr);
Node result = CreateExprStatement (n, lineno);
return result;
}
/// <summary> Name</summary>
internal Node CreateName (string name)
{
checkActivationName (name, Token.NAME);
return Node.newString (Token.NAME, name);
}
/// <summary> String (for literals)</summary>
internal Node CreateString (string str)
{
return Node.newString (str);
}
/// <summary> Number (for literals)</summary>
internal Node CreateNumber (double number)
{
return Node.newNumber (number);
}
/// <summary> Catch clause of try/catch/finally</summary>
/// <param name="varName">the name of the variable to bind to the exception
/// </param>
/// <param name="catchCond">the condition under which to catch the exception.
/// May be null if no condition is given.
/// </param>
/// <param name="stmts">the statements in the catch clause
/// </param>
/// <param name="lineno">the starting line number of the catch clause
/// </param>
internal Node CreateCatch (string varName, Node catchCond, Node stmts, int lineno)
{
if (catchCond == null) {
catchCond = new Node (Token.EMPTY);
}
return new Node (Token.CATCH, CreateName (varName), catchCond, stmts, lineno);
}
/// <summary> Throw</summary>
internal Node CreateThrow (Node expr, int lineno)
{
return new Node (Token.THROW, expr, lineno);
}
/// <summary> Return</summary>
internal Node CreateReturn (Node expr, int lineno)
{
return expr == null ? new Node (Token.RETURN, lineno) : new Node (Token.RETURN, expr, lineno);
}
/// <summary> Debugger</summary>
internal Node CreateDebugger(int lineno)
{
return new Node(Token.DEBUGGER, lineno);
}
/// <summary> Label</summary>
internal Node CreateLabel (int lineno)
{
return new Node.Jump (Token.LABEL, lineno);
}
internal Node getLabelLoop (Node label)
{
return ((Node.Jump)label).Loop;
}
/// <summary> Label</summary>
internal Node CreateLabeledStatement (Node labelArg, Node statement)
{
Node.Jump label = (Node.Jump)labelArg;
// Make a target and put it _after_ the statement
// node. And in the LABEL node, so breaks get the
// right target.
Node breakTarget = Node.newTarget ();
Node block = new Node (Token.BLOCK, label, statement, breakTarget);
label.target = breakTarget;
return block;
}
/// <summary> Break (possibly labeled)</summary>
internal Node CreateBreak (Node breakStatement, int lineno)
{
Node.Jump n = new Node.Jump (Token.BREAK, lineno);
Node.Jump jumpStatement;
int t = breakStatement.Type;
if (t == Token.LOOP || t == Token.LABEL) {
jumpStatement = (Node.Jump)breakStatement;
}
else if (t == Token.BLOCK && breakStatement.FirstChild.Type == Token.SWITCH) {
jumpStatement = (Node.Jump)breakStatement.FirstChild;
}
else {
throw Context.CodeBug ();
}
n.JumpStatement = jumpStatement;
return n;
}
/// <summary> Continue (possibly labeled)</summary>
internal Node CreateContinue (Node loop, int lineno)
{
if (loop.Type != Token.LOOP)
Context.CodeBug ();
Node.Jump n = new Node.Jump (Token.CONTINUE, lineno);
n.JumpStatement = (Node.Jump)loop;
return n;
}
/// <summary> Statement block
/// Creates the empty statement block
/// Must make subsequent calls to add statements to the node
/// </summary>
internal Node CreateBlock (int lineno)
{
return new Node (Token.BLOCK, lineno);
}
internal FunctionNode CreateFunction (string name)
{
return new FunctionNode (name);
}
internal Node initFunction (FunctionNode fnNode, int functionIndex, Node statements, int functionType)
{
fnNode.itsFunctionType = functionType;
fnNode.addChildToBack (statements);
int functionCount = fnNode.FunctionCount;
if (functionCount != 0) {
// Functions containing other functions require activation objects
fnNode.itsNeedsActivation = true;
for (int i = 0; i != functionCount; ++i) {
FunctionNode fn = fnNode.getFunctionNode (i);
// nested function expression statements overrides var
if (fn.FunctionType == FunctionNode.FUNCTION_EXPRESSION_STATEMENT) {
string name = fn.FunctionName;
if (name != null && name.Length != 0) {
fnNode.removeParamOrVar (name);
}
}
}
}
if (functionType == FunctionNode.FUNCTION_EXPRESSION) {
string name = fnNode.FunctionName;
if (name != null && name.Length != 0 && !fnNode.hasParamOrVar (name)) {
// A function expression needs to have its name as a
// variable (if it isn't already allocated as a variable).
// See ECMA Ch. 13. We add code to the beginning of the
// function to initialize a local variable of the
// function's name to the function value.
fnNode.addVar (name);
Node setFn = new Node (Token.EXPR_VOID, new Node (Token.SETNAME, Node.newString (Token.BINDNAME, name), new Node (Token.THISFN)));
statements.addChildrenToFront (setFn);
}
}
// Add return to end if needed.
Node lastStmt = statements.LastChild;
if (lastStmt == null || lastStmt.Type != Token.RETURN) {
statements.addChildToBack (new Node (Token.RETURN));
}
Node result = Node.newString (Token.FUNCTION, fnNode.FunctionName);
result.putIntProp (Node.FUNCTION_PROP, functionIndex);
return result;
}
/// <summary> Add a child to the back of the given node. This function
/// breaks the Factory abstraction, but it removes a requirement
/// from implementors of Node.
/// </summary>
internal void addChildToBack (Node parent, Node child)
{
parent.addChildToBack (child);
}
/// <summary> Create loop node. The parser will later call
/// CreateWhile|CreateDoWhile|CreateFor|CreateForIn
/// to finish loop generation.
/// </summary>
internal Node CreateLoopNode (Node loopLabel, int lineno)
{
Node.Jump result = new Node.Jump (Token.LOOP, lineno);
if (loopLabel != null) {
((Node.Jump)loopLabel).Loop = result;
}
return result;
}
/// <summary> While</summary>
internal Node CreateWhile (Node loop, Node cond, Node body)
{
return CreateLoop ((Node.Jump)loop, LOOP_WHILE, body, cond, null, null);
}
/// <summary> DoWhile</summary>
internal Node CreateDoWhile (Node loop, Node body, Node cond)
{
return CreateLoop ((Node.Jump)loop, LOOP_DO_WHILE, body, cond, null, null);
}
/// <summary> For</summary>
internal Node CreateFor (Node loop, Node init, Node test, Node incr, Node body)
{
return CreateLoop ((Node.Jump)loop, LOOP_FOR, body, test, init, incr);
}
private Node CreateLoop (Node.Jump loop, int loopType, Node body, Node cond, Node init, Node incr)
{
Node bodyTarget = Node.newTarget ();
Node condTarget = Node.newTarget ();
if (loopType == LOOP_FOR && cond.Type == Token.EMPTY) {
cond = new Node (Token.TRUE);
}
Node.Jump IFEQ = new Node.Jump (Token.IFEQ, cond);
IFEQ.target = bodyTarget;
Node breakTarget = Node.newTarget ();
loop.addChildToBack (bodyTarget);
loop.addChildrenToBack (body);
if (loopType == LOOP_WHILE || loopType == LOOP_FOR) {
// propagate lineno to condition
loop.addChildrenToBack (new Node (Token.EMPTY, loop.Lineno));
}
loop.addChildToBack (condTarget);
loop.addChildToBack (IFEQ);
loop.addChildToBack (breakTarget);
loop.target = breakTarget;
Node continueTarget = condTarget;
if (loopType == LOOP_WHILE || loopType == LOOP_FOR) {
// Just add a GOTO to the condition in the do..while
loop.addChildToFront (makeJump (Token.GOTO, condTarget));
if (loopType == LOOP_FOR) {
if (init.Type != Token.EMPTY) {
if (init.Type != Token.VAR) {
init = new Node (Token.EXPR_VOID, init);
}
loop.addChildToFront (init);
}
Node incrTarget = Node.newTarget ();
loop.addChildAfter (incrTarget, body);
if (incr.Type != Token.EMPTY) {
incr = new Node (Token.EXPR_VOID, incr);
loop.addChildAfter (incr, incrTarget);
}
continueTarget = incrTarget;
}
}
loop.Continue = continueTarget;
return loop;
}
/// <summary> For .. In
///
/// </summary>
internal Node CreateForIn (Node loop, Node lhs, Node obj, Node body, bool isForEach)
{
int type = lhs.Type;
Node lvalue;
if (type == Token.VAR) {
/*
* check that there was only one variable given.
* we can't do this in the parser, because then the
* parser would have to know something about the
* 'init' node of the for-in loop.
*/
Node lastChild = lhs.LastChild;
if (lhs.FirstChild != lastChild) {
parser.ReportError ("msg.mult.index");
}
lvalue = Node.newString (Token.NAME, lastChild.String);
}
else {
lvalue = makeReference (lhs);
if (lvalue == null) {
parser.ReportError ("msg.bad.for.in.lhs");
return obj;
}
}
Node localBlock = new Node (Token.LOCAL_BLOCK);
int initType = (isForEach) ? Token.ENUM_INIT_VALUES : Token.ENUM_INIT_KEYS;
Node init = new Node (initType, obj);
init.putProp (Node.LOCAL_BLOCK_PROP, localBlock);
Node cond = new Node (Token.ENUM_NEXT);
cond.putProp (Node.LOCAL_BLOCK_PROP, localBlock);
Node id = new Node (Token.ENUM_ID);
id.putProp (Node.LOCAL_BLOCK_PROP, localBlock);
Node newBody = new Node (Token.BLOCK);
Node assign = simpleAssignment (lvalue, id);
newBody.addChildToBack (new Node (Token.EXPR_VOID, assign));
newBody.addChildToBack (body);
loop = CreateWhile (loop, cond, newBody);
loop.addChildToFront (init);
if (type == Token.VAR)
loop.addChildToFront (lhs);
localBlock.addChildToBack (loop);
return localBlock;
}
/// <summary> Try/Catch/Finally
///
/// The IRFactory tries to express as much as possible in the tree;
/// the responsibilities remaining for Codegen are to add the Java
/// handlers: (Either (but not both) of TARGET and FINALLY might not
/// be defined)
/// - a catch handler for javascript exceptions that unwraps the
/// exception onto the stack and GOTOes to the catch target
/// - a finally handler
/// ... and a goto to GOTO around these handlers.
/// </summary>
internal Node CreateTryCatchFinally (Node tryBlock, Node catchBlocks, Node finallyBlock, int lineno)
{
bool hasFinally = (finallyBlock != null) && (finallyBlock.Type != Token.BLOCK || finallyBlock.hasChildren ());
// short circuit
if (tryBlock.Type == Token.BLOCK && !tryBlock.hasChildren () && !hasFinally) {
return tryBlock;
}
bool hasCatch = catchBlocks.hasChildren ();
// short circuit
if (!hasFinally && !hasCatch) {
// bc finally might be an empty block...
return tryBlock;
}
Node handlerBlock = new Node (Token.LOCAL_BLOCK);
Node.Jump pn = new Node.Jump (Token.TRY, tryBlock, lineno);
pn.putProp (Node.LOCAL_BLOCK_PROP, handlerBlock);
if (hasCatch) {
// jump around catch code
Node endCatch = Node.newTarget ();
pn.addChildToBack (makeJump (Token.GOTO, endCatch));
// make a TARGET for the catch that the tcf node knows about
Node catchTarget = Node.newTarget ();
pn.target = catchTarget;
// mark it
pn.addChildToBack (catchTarget);
//
// Given
//
// try {
// tryBlock;
// } catch (e if condition1) {
// something1;
// ...
//
// } catch (e if conditionN) {
// somethingN;
// } catch (e) {
// somethingDefault;
// }
//
// rewrite as
//
// try {
// tryBlock;
// goto after_catch:
// } catch (x) {
// with (newCatchScope(e, x)) {
// if (condition1) {
// something1;
// goto after_catch;
// }
// }
// ...
// with (newCatchScope(e, x)) {
// if (conditionN) {
// somethingN;
// goto after_catch;
// }
// }
// with (newCatchScope(e, x)) {
// somethingDefault;
// goto after_catch;
// }
// }
// after_catch:
//
// If there is no default catch, then the last with block
// arround "somethingDefault;" is replaced by "rethrow;"
// It is assumed that catch handler generation will store
// exeception object in handlerBlock register
// Block with local for exception scope objects
Node catchScopeBlock = new Node (Token.LOCAL_BLOCK);
// expects catchblocks children to be (cond block) pairs.
Node cb = catchBlocks.FirstChild;
bool hasDefault = false;
int scopeIndex = 0;
while (cb != null) {
int catchLineNo = cb.Lineno;
Node name = cb.FirstChild;
Node cond = name.Next;
Node catchStatement = cond.Next;
cb.removeChild (name);
cb.removeChild (cond);
cb.removeChild (catchStatement);
// Add goto to the catch statement to jump out of catch
// but prefix it with LEAVEWITH since try..catch produces
// "with"code in order to limit the scope of the exception
// object.
catchStatement.addChildToBack (new Node (Token.LEAVEWITH));
catchStatement.addChildToBack (makeJump (Token.GOTO, endCatch));
// Create condition "if" when present
Node condStmt;
if (cond.Type == Token.EMPTY) {
condStmt = catchStatement;
hasDefault = true;
}
else {
condStmt = CreateIf (cond, catchStatement, null, catchLineNo);
}
// Generate code to Create the scope object and store
// it in catchScopeBlock register
Node catchScope = new Node (Token.CATCH_SCOPE, name, CreateUseLocal (handlerBlock));
catchScope.putProp (Node.LOCAL_BLOCK_PROP, catchScopeBlock);
catchScope.putIntProp (Node.CATCH_SCOPE_PROP, scopeIndex);
catchScopeBlock.addChildToBack (catchScope);
// Add with statement based on catch scope object
catchScopeBlock.addChildToBack (CreateWith (CreateUseLocal (catchScopeBlock), condStmt, catchLineNo));
// move to next cb
cb = cb.Next;
++scopeIndex;
}
pn.addChildToBack (catchScopeBlock);
if (!hasDefault) {
// Generate code to rethrow if no catch clause was executed
Node rethrow = new Node (Token.RETHROW);
rethrow.putProp (Node.LOCAL_BLOCK_PROP, handlerBlock);
pn.addChildToBack (rethrow);
}
pn.addChildToBack (endCatch);
}
if (hasFinally) {
Node finallyTarget = Node.newTarget ();
pn.Finally = finallyTarget;
// add jsr finally to the try block
pn.addChildToBack (makeJump (Token.JSR, finallyTarget));
// jump around finally code
Node finallyEnd = Node.newTarget ();
pn.addChildToBack (makeJump (Token.GOTO, finallyEnd));
pn.addChildToBack (finallyTarget);
Node fBlock = new Node (Token.FINALLY, finallyBlock);
fBlock.putProp (Node.LOCAL_BLOCK_PROP, handlerBlock);
pn.addChildToBack (fBlock);
pn.addChildToBack (finallyEnd);
}
handlerBlock.addChildToBack (pn);
return handlerBlock;
}
/// <summary> Throw, Return, Label, Break and Continue are defined in ASTFactory.</summary>
/// <summary> With</summary>
internal Node CreateWith (Node obj, Node body, int lineno)
{
setRequiresActivation ();
Node result = new Node (Token.BLOCK, lineno);
result.addChildToBack (new Node (Token.ENTERWITH, obj));
Node bodyNode = new Node (Token.WITH, body, lineno);
result.addChildrenToBack (bodyNode);
result.addChildToBack (new Node (Token.LEAVEWITH));
return result;
}
/// <summary> DOTQUERY</summary>
public Node CreateDotQuery (Node obj, Node body, int lineno)
{
setRequiresActivation ();
Node result = new Node (Token.DOTQUERY, obj, body, lineno);
return result;
}
internal Node CreateArrayLiteral (ObjArray elems, int skipCount)
{
int length = elems.size ();
int [] skipIndexes = null;
if (skipCount != 0) {
skipIndexes = new int [skipCount];
}
Node array = new Node (Token.ARRAYLIT);
for (int i = 0, j = 0; i != length; ++i) {
Node elem = (Node)elems.Get (i);
if (elem != null) {
array.addChildToBack (elem);
}
else {
skipIndexes [j] = i;
++j;
}
}
if (skipCount != 0) {
array.putProp (Node.SKIP_INDEXES_PROP, skipIndexes);
}
return array;
}
/// <summary> Object Literals
/// <BR> CreateObjectLiteral rewrites its argument as object
/// creation plus object property entries, so later compiler
/// stages don't need to know about object literals.
/// </summary>
internal Node CreateObjectLiteral (ObjArray elems)
{
int size = elems.size () / 2;
Node obj = new Node (Token.OBJECTLIT);
object [] properties;
if (size == 0) {
properties = ScriptRuntime.EmptyArgs;
}
else {
properties = new object [size];
for (int i = 0; i != size; ++i) {
properties [i] = elems.Get (2 * i);
Node value = (Node)elems.Get (2 * i + 1);
obj.addChildToBack (value);
}
}
obj.putProp (Node.OBJECT_IDS_PROP, properties);
return obj;
}
/// <summary> Regular expressions</summary>
internal Node CreateRegExp (int regexpIndex)
{
Node n = new Node (Token.REGEXP);
n.putIntProp (Node.REGEXP_PROP, regexpIndex);
return n;
}
/// <summary> If statement</summary>
internal Node CreateIf (Node cond, Node ifTrue, Node ifFalse, int lineno)
{
int condStatus = isAlwaysDefinedBoolean (cond);
if (condStatus == ALWAYS_TRUE_BOOLEAN) {
return ifTrue;
}
else if (condStatus == ALWAYS_FALSE_BOOLEAN) {
if (ifFalse != null) {
return ifFalse;
}
return new Node (Token.BLOCK, lineno);
}
Node result = new Node (Token.BLOCK, lineno);
Node ifNotTarget = Node.newTarget ();
Node.Jump IFNE = new Node.Jump (Token.IFNE, cond);
IFNE.target = ifNotTarget;
result.addChildToBack (IFNE);
result.addChildrenToBack (ifTrue);
if (ifFalse != null) {
Node endTarget = Node.newTarget ();
result.addChildToBack (makeJump (Token.GOTO, endTarget));
result.addChildToBack (ifNotTarget);
result.addChildrenToBack (ifFalse);
result.addChildToBack (endTarget);
}
else {
result.addChildToBack (ifNotTarget);
}
return result;
}
internal Node CreateCondExpr (Node cond, Node ifTrue, Node ifFalse)
{
int condStatus = isAlwaysDefinedBoolean (cond);
if (condStatus == ALWAYS_TRUE_BOOLEAN) {
return ifTrue;
}
else if (condStatus == ALWAYS_FALSE_BOOLEAN) {
return ifFalse;
}
return new Node (Token.HOOK, cond, ifTrue, ifFalse);
}
/// <summary> Unary</summary>
internal Node CreateUnary (int nodeType, Node child)
{
int childType = child.Type;
switch (nodeType) {
case Token.DELPROP: {
Node n;
if (childType == Token.NAME) {
// Transform Delete(Name "a")
// to Delete(Bind("a"), String("a"))
child.Type = Token.BINDNAME;
Node left = child;
Node right = Node.newString (child.String);
n = new Node (nodeType, left, right);
}
else if (childType == Token.GETPROP || childType == Token.GETELEM) {
Node left = child.FirstChild;
Node right = child.LastChild;
child.removeChild (left);
child.removeChild (right);
n = new Node (nodeType, left, right);
}
else if (childType == Token.GET_REF) {
Node rf = child.FirstChild;
child.removeChild (rf);
n = new Node (Token.DEL_REF, rf);
}
else {
n = new Node (Token.TRUE);
}
return n;
}
case Token.TYPEOF:
if (childType == Token.NAME) {
child.Type = Token.TYPEOFNAME;
return child;
}
break;
case Token.BITNOT:
if (childType == Token.NUMBER) {
int value = ScriptConvert.ToInt32 (child.Double);
child.Double = ~value;
return child;
}
break;
case Token.NEG:
if (childType == Token.NUMBER) {
child.Double = -child.Double;
return child;
}
break;
case Token.NOT: {
int status = isAlwaysDefinedBoolean (child);
if (status != 0) {
int type;
if (status == ALWAYS_TRUE_BOOLEAN) {
type = Token.FALSE;
}
else {
type = Token.TRUE;
}
if (childType == Token.TRUE || childType == Token.FALSE) {
child.Type = type;
return child;
}
return new Node (type);
}
break;
}
}
return new Node (nodeType, child);
}
internal Node CreateCallOrNew (int nodeType, Node child)
{
int type = Node.NON_SPECIALCALL;
if (child.Type == Token.NAME) {
string name = child.String;
if (name.Equals ("eval")) {
type = Node.SPECIALCALL_EVAL;
}
else if (name.Equals ("With")) {
type = Node.SPECIALCALL_WITH;
}
}
else if (child.Type == Token.GETPROP) {
string name = child.LastChild.String;
if (name.Equals ("eval")) {
type = Node.SPECIALCALL_EVAL;
}
}
Node node = new Node (nodeType, child);
if (type != Node.NON_SPECIALCALL) {
// Calls to these functions require activation objects.
setRequiresActivation ();
node.putIntProp (Node.SPECIALCALL_PROP, type);
}
return node;
}
internal Node CreateIncDec (int nodeType, bool post, Node child)
{
child = makeReference (child);
if (child == null) {
string msg;
if (nodeType == Token.DEC) {
msg = "msg.bad.decr";
}
else {
msg = "msg.bad.incr";
}
parser.ReportError (msg);
return null;
}
int childType = child.Type;
switch (childType) {
case Token.NAME:
case Token.GETPROP:
case Token.GETELEM:
case Token.GET_REF: {
Node n = new Node (nodeType, child);
int incrDecrMask = 0;
if (nodeType == Token.DEC) {
incrDecrMask |= Node.DECR_FLAG;
}
if (post) {
incrDecrMask |= Node.POST_FLAG;
}
n.putIntProp (Node.INCRDECR_PROP, incrDecrMask);
return n;
}
}
throw Context.CodeBug ();
}
internal Node CreatePropertyGet (Node target, string ns, string name, int memberTypeFlags)
{
if (ns == null && memberTypeFlags == 0) {
if (target == null) {
return CreateName (name);
}
checkActivationName (name, Token.GETPROP);
if (ScriptRuntime.isSpecialProperty (name)) {
Node rf = new Node (Token.REF_SPECIAL, target);
rf.putProp (Node.NAME_PROP, name);
return new Node (Token.GET_REF, rf);
}
return new Node (Token.GETPROP, target, CreateString (name));
}
Node elem = CreateString (name);
memberTypeFlags |= Node.PROPERTY_FLAG;
return CreateMemberRefGet (target, ns, elem, memberTypeFlags);
}
internal Node CreateElementGet (Node target, string ns, Node elem, int memberTypeFlags)
{
// OPT: could optimize to CreatePropertyGet
// iff elem is string that can not be number
if (ns == null && memberTypeFlags == 0) {
// stand-alone [aaa] as primary expression is array literal
// declaration and should not come here!
if (target == null)
throw Context.CodeBug ();
return new Node (Token.GETELEM, target, elem);
}
return CreateMemberRefGet (target, ns, elem, memberTypeFlags);
}
private Node CreateMemberRefGet (Node target, string ns, Node elem, int memberTypeFlags)
{
Node nsNode = null;
if (ns != null) {
// See 11.1.2 in ECMA 357
if (ns.Equals ("*")) {
nsNode = new Node (Token.NULL);
}
else {
nsNode = CreateName (ns);
}
}
Node rf;
if (target == null) {
if (ns == null) {
rf = new Node (Token.REF_NAME, elem);
}
else {
rf = new Node (Token.REF_NS_NAME, nsNode, elem);
}
}
else {
if (ns == null) {
rf = new Node (Token.REF_MEMBER, target, elem);
}
else {
rf = new Node (Token.REF_NS_MEMBER, target, nsNode, elem);
}
}
if (memberTypeFlags != 0) {
rf.putIntProp (Node.MEMBER_TYPE_PROP, memberTypeFlags);