This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
CodeGenVisitor.cs
781 lines (639 loc) · 23.3 KB
/
CodeGenVisitor.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using FogCreek.Wasabi.AST;
namespace FogCreek.Wasabi
{
public class Counter
{
internal int count = 0;
public int intValue()
{
return count;
}
public void increment()
{
count++;
}
}
public class CodeGenVisitor : IVisitor, ICodeGenAcceptor
{
internal ICodeGenVisitor visitor;
internal bool instrument;
internal Counter instrumentnumber;
internal StreamWriter instrumentdb;
internal const int DbAll = -1;
internal const int DbMsSql = 0x01;
internal const int DbMySql = 0x02;
internal const int DbAccess = ~(0x03);
internal int dbtype = DbAll;
internal List<IInterceptor> functionInterceptors = new List<IInterceptor>();
public CodeGenVisitor(StreamWriter instrumentdb, ICodeGenVisitor visitor, bool instrument, Counter counter)
{
this.instrumentdb = instrumentdb;
this.visitor = visitor;
this.instrument = instrument;
this.visitor.Acceptor = this;
instrumentnumber = counter;
}
internal String classname = "";
internal String funcname = "";
private void InstrumentNode(CNode node)
{
if (!instrument || node.Token == null || node.Token.Filename.ToUpper() == "ccodecoverage.asp".ToUpper() ||
node.Token.Filename.ToUpper() == "codecoveragereport.asp".ToUpper())
return;
int num = instrumentnumber.intValue();
instrumentnumber.increment();
visitor.InstrumentNode(node, num);
instrumentdb.Write(node.Token.Filename + "," + classname + "," + funcname + "," + node.Token.LineNumber +
"," + num +
",");
bool printeddb = false;
if ((dbtype & DbMsSql) != 0)
{
instrumentdb.Write("DbMsSql");
printeddb = true;
}
if ((dbtype & DbAccess) != 0)
{
if (printeddb)
instrumentdb.Write("|");
instrumentdb.Write("DbAccess");
printeddb = true;
}
if ((dbtype & DbMySql) != 0)
{
if (printeddb)
instrumentdb.Write("|");
instrumentdb.Write("DbMySql");
printeddb = true;
}
instrumentdb.WriteLine();
}
public void EnterFunction(CFunction function)
{
foreach (var item in functionInterceptors)
item.EnterFunction(function, this);
}
public void ExitFunction(CFunction function)
{
foreach (var item in functionInterceptors)
item.ExitFunction(function, this);
}
public void VisitBlock(CStatementBlock statements)
{
visitor.VisitBlock(statements);
}
public void VisitAssignment(CAssignment assign)
{
InstrumentNode(assign);
visitor.VisitAssignment(assign);
}
public void VisitCase(CCase ccase)
{
visitor.VisitCase(ccase);
}
public void VisitInterface(CInterface iface)
{
if (canGenerate(iface))
{
classname = iface.RawName;
visitor.VisitInterface(iface);
classname = "";
}
}
public void VisitEnum(CEnum eration)
{
if (canGenerate(eration))
{
classname = eration.RawName;
visitor.VisitEnum(eration);
classname = "";
}
}
public void VisitClass(CClass cclas)
{
if (canGenerate(cclas))
{
classname = cclas.RawName;
visitor.VisitClass(cclas);
classname = "";
}
}
public void VisitComment(CComment comment)
{
visitor.VisitComment(comment);
}
public void VisitConcat(CConcat concat)
{
visitor.VisitConcat(concat);
}
public void VisitConst(CConst cconst)
{
if (canGenerate(cconst))
visitor.VisitConst(cconst);
}
public void VisitDim(CDim dim)
{
if (classname == "" && funcname == "" && !canGenerate(dim))
return;
visitor.VisitDim(dim);
}
public void VisitDo(CDo cdo)
{
InstrumentNode(cdo);
visitor.VisitDo(cdo);
}
public void VisitExit(CExit exit)
{
InstrumentNode(exit);
visitor.VisitExit(exit);
}
public void VisitReturn(CReturn _return)
{
InstrumentNode(_return);
visitor.VisitReturn(_return);
}
public void VisitFor(CFor cfor)
{
InstrumentNode(cfor);
visitor.VisitFor(cfor);
}
public void VisitForEach(CForEach _foreach)
{
InstrumentNode(_foreach);
visitor.VisitForEach(_foreach);
}
public void VisitFunction(CFunction function)
{
if (canGenerate(function))
{
funcname = function.Name;
visitor.VisitFunction(function);
funcname = "";
}
}
public void VisitHtml(CHtml html)
{
visitor.VisitHtml(html);
}
internal class DbTypeInfo
{
internal int oldtype;
internal int elsetype = -1;
internal int currenttype;
}
public Object PreAcceptThen(CIf cif, Object objstate)
{
int newtype = 0;
CExpression exp = cif.Condition;
bool not = exp is CNot;
if (not)
{
exp = ((CNot)exp).Operand;
if (exp is CParenExpression)
exp = ((CParenExpression)exp).InnerExpression;
}
if (exp is CLogic && ((CLogic)exp).Operation.Value == "or")
{
CExpression lhs = ((CLogic)exp).Left;
if (lhs is CAccess && ((CAccess)lhs).IsRootAccess &&
((CAccess)lhs).ReferenceToken.Value == "g_fmssql")
newtype |= DbMsSql;
exp = ((CLogic)exp).Right;
}
if (exp is CAccess)
{
CAccess acc = (CAccess)exp;
if (acc.IsRootAccess)
{
if (acc.ReferenceToken.Value == "g_fmssql")
newtype |= DbMsSql;
if (acc.ReferenceToken.Value == "g_fmysql")
newtype |= DbMySql;
}
}
if (newtype == 0)
return null;
if (not)
newtype = ~newtype;
DbTypeInfo state;
if (objstate == null)
{
state = new DbTypeInfo();
state.oldtype = dbtype;
}
else
state = (DbTypeInfo)objstate;
state.currenttype = newtype;
state.elsetype &= ~newtype;
dbtype = newtype;
return state;
}
public void PreAcceptElse(CIf cif, Object objstate)
{
if (objstate == null)
return;
DbTypeInfo state = (DbTypeInfo)objstate;
dbtype = state.elsetype;
}
public void PreAcceptEndIf(CIf cif, Object objstate)
{
if (objstate == null)
return;
DbTypeInfo state = (DbTypeInfo)objstate;
dbtype = state.oldtype;
}
public void VisitIf(CIf cif)
{
if (!cif.IsElseIf)
InstrumentNode(cif);
visitor.VisitIf(cif);
}
public void VisitMemberVariable(CMemberVariable membervariable)
{
visitor.VisitMemberVariable(membervariable);
}
public void VisitReDim(CReDim redim)
{
InstrumentNode(redim);
visitor.VisitReDim(redim);
}
public void VisitSelect(CSelect select)
{
InstrumentNode(select);
visitor.VisitSelect(select);
}
public void VisitSpecialEqual(CSpecialEqual specialequal)
{
InstrumentNode(specialequal);
visitor.VisitSpecialEqual(specialequal);
}
public void VisitStatement(CStatement statement)
{
if (!(statement is CNewline))
InstrumentNode(statement);
visitor.VisitStatement(statement);
}
public void VisitToken(CToken token)
{
visitor.VisitToken(token);
}
public void VisitWhile(CWhile cwhile)
{
InstrumentNode(cwhile);
visitor.VisitWhile(cwhile);
}
public void VisitWith(CWith with)
{
InstrumentNode(with);
visitor.VisitWith(with);
}
internal void VisitExpression(CExpression exp)
{
exp.Accept(visitor);
}
public void VisitAccess(CAccess access)
{
VisitExpression(access);
}
public void VisitComparison(CComparison compare)
{
VisitExpression(compare);
}
public void VisitConstantExpression(CConstantExpression constant)
{
VisitExpression(constant);
}
public void VisitDefaultAccess(CDefaultAccess access)
{
VisitExpression(access);
}
public void VisitLogic(CLogic logic)
{
VisitExpression(logic);
}
public void VisitMath(CMath math)
{
VisitExpression(math);
}
public void VisitMathUnary(CMathUnary math)
{
VisitExpression(math);
}
public void VisitMemberAccess(CMemberAccess access)
{
VisitExpression(access);
}
public void VisitNew(CNew _new)
{
VisitExpression(_new);
}
public void VisitNot(CNot not)
{
VisitExpression(not);
}
public void VisitOnError(COnError onerror)
{
onerror.Accept(visitor);
}
public void VisitParameters(CParameters parameters)
{
visitor.VisitParameters(parameters);
}
public void VisitThisAccess(CThisAccess access)
{
VisitExpression(access);
}
public void VisitBaseAccess(CBaseAccess access)
{
VisitExpression(access);
}
public void VisitWithAccess(CWithAccess access)
{
VisitExpression(access);
}
public void VisitTernary(CTernary ternary)
{
VisitExpression(ternary);
}
public void VisitParenExpression(CParenExpression exp)
{
VisitExpression(exp);
}
public void VisitVariable(CVariable var)
{
visitor.VisitVariable(var);
}
public void VisitTry(CTry _try)
{
visitor.VisitTry(_try);
}
public void VisitCatch(CCatch _catch)
{
visitor.VisitCatch(_catch);
}
public void VisitFinally(CFinally _finally)
{
visitor.VisitFinally(_finally);
}
public void VisitThrow(CThrow _throw)
{
visitor.VisitThrow(_throw);
}
public void VisitPictureOf(CPictureOfExpression pic)
{
visitor.VisitPictureOf(pic);
}
public void VisitOption(COption option)
{
foreach (var item in this.functionInterceptors)
item.Option(option, this);
visitor.VisitOption(option);
}
public void VisitLambdaExpression(CLambdaExpression lambda)
{
visitor.VisitLambdaExpression(lambda);
}
public void VisitProgram(CProgram program)
{
visitor.VisitProgram(program);
}
internal CToken CreateToken(String tokenSource, TokenTypes type)
{
return CreateToken(tokenSource, tokenSource.ToLower(), type, null);
}
internal CToken CreateToken(String tokenSource, string rawValue, TokenTypes type)
{
return CreateToken(tokenSource, rawValue, type, null);
}
internal CToken CreateTokenWithAdditionalInfo(String tokenSource, TokenTypes type)
{
return CreateTokenWithAdditionalInfo(tokenSource, tokenSource, type);
}
internal CToken filenamesrc = null;
internal CToken CreateTokenWithAdditionalInfo(String rawvalue, String sValue, TokenTypes type)
{
return CreateToken(rawvalue, sValue, type, rawvalue);
}
internal CToken CreateToken(String rawvalue, String sValue, TokenTypes type, object additionalInfo)
{
String filename = filenamesrc == null ? null : filenamesrc.Filename;
CToken result = new CToken(filename, -1, 0, "", type, sValue, rawvalue, false);
result.AdditionalInfo = additionalInfo;
return result;
}
public void VisitFile(CFile file)
{
if (!canGenerate(file))
return;
visitor.PreVisitFile(file);
filenamesrc = file.Token;
new CNewline(null).Accept(visitor);
if (instrument)
{
COption cCodeCoverage =
new COption(new CToken(file.Filename, TokenTypes.keyword, "option"),
new CToken(file.Filename, TokenTypes.keyword, "include"),
new CToken(file.Filename, TokenTypes.str, "cCodeCoverage.asp"));
cCodeCoverage.Accept(this);
}
visitor.VisitFile(file);
if (file.Attributes.contains("GenerateProcessAjaxFunction"))
{
CFunction unicoderequest = CProgram.Global.FindFunction("unicoderequest");
CFunction intrequest = CProgram.Global.FindFunction("intrequest");
CFunction boolrequest = CProgram.Global.FindFunction("boolrequest");
CToken tok = CreateTokenWithAdditionalInfo("ProcessAjax", "processajax", TokenTypes.identifier);
CFunction processAjax =
new CFunction(tok, tok.RawValue, tok.Value, TokenTypes.visPublic, CFunction.vbSub, null,
new CTypeRef(null, BuiltIns.Void));
CAccess pivotitem = new CAccess(unicoderequest.Token, unicoderequest.Token);
pivotitem.ReferenceTarget = unicoderequest;
CParameters pivotparams = new CParameters();
pivotparams.Unnamed.Add(
new CConstantExpression(CreateTokenWithAdditionalInfo("sFunction", TokenTypes.str)));
CSelect select =
new CSelect(CreateTokenWithAdditionalInfo("select", TokenTypes.controlFlow),
new CDefaultAccess(pivotitem.Token, pivotitem, pivotparams));
IEnumerator it = CProgram.Global.Functions.GetEnumerator();
List<CVariable> vars = new List<CVariable>();
while (it.MoveNext())
{
CFunction func = (CFunction)it.Current;
if (!func.Attributes.contains("ExecuteOnServer"))
continue;
CStatementBlock block = new CStatementBlock();
CParameters funcParams = new CParameters();
for (int ixArg = 0; ixArg < func.Arguments.Count - 3; ixArg++)
{
CArgument arg = func.Arguments[ixArg];
tok =
CreateTokenWithAdditionalInfo("vParam" + (ixArg + 1), "vparam" + (ixArg + 1),
TokenTypes.identifier);
if (ixArg >= vars.Count)
vars.Add(new CVariable(tok, false, new CTypeRef(null, BuiltIns.Variant), null, null, null));
CAssignment assign = new CAssignment(tok);
CAccess left = new CAccess(tok, tok);
left.ReferenceTarget = vars[ixArg];
assign.Target = left;
funcParams.Unnamed.Add(left);
CFunction rightfunc = intrequest;
if (arg.Type.RawName == "Boolean")
rightfunc = boolrequest;
else if (arg.Type.RawName == "String")
rightfunc = unicoderequest;
CParameters parameters = new CParameters();
parameters.Unnamed.Add(
new CConstantExpression(CreateTokenWithAdditionalInfo(tok.RawValue, TokenTypes.str)));
CAccess rightitem = new CAccess(rightfunc.Token, rightfunc.Token);
rightitem.ReferenceTarget = rightfunc;
assign.Source = new CDefaultAccess(rightfunc.Token, rightitem, parameters);
assign.Source.RhsAssignmentSource = true;
block.Add(assign);
}
CAccess calledItem = new CAccess(func.Token, func.Token);
CDefaultAccess called = new CDefaultAccess(func.Token, calledItem, funcParams);
calledItem.ReferenceTarget = calledItem.ReferenceTarget = func;
if (func.Attributes.contains("picture") || func.FunctionType == CFunction.vbFunction)
{
CAccess returnItem = new CAccess(processAjax.Token, processAjax.Token);
returnItem.ReferenceTarget = processAjax;
CAssignment assign = new CAssignment(processAjax.Token);
assign.Target = returnItem;
if (func.FunctionType == CFunction.vbFunction)
assign.Source = called;
else
assign.Source = new CPictureOfExpression(tok, called);
block.Add(assign);
}
else
block.Add(new CExpressionStatement(called));
tok = CreateTokenWithAdditionalInfo("case", TokenTypes.controlFlow);
CExpression exp =
new CConstantExpression(CreateTokenWithAdditionalInfo(func.RawName, TokenTypes.str));
select.Cases.Add(new CCase(tok, exp, block));
}
CDim dim = new CDim(CreateTokenWithAdditionalInfo("Dim", TokenTypes.declaration));
dim.Variables.AddRange(vars);
processAjax.Statements.Add(dim);
processAjax.Statements.Add(select);
CNewline nl = new CNewline(CreateTokenWithAdditionalInfo("\n", TokenTypes.newline));
nl.Accept(this);
processAjax.Accept(this);
nl.Accept(this);
}
}
public void VisitDirective(CDirective directive)
{
visitor.VisitDirective(directive);
}
public bool canGenerate(CFile file)
{
if (file.Attributes.contains("ExecuteAtCompiler") || file.IncludedByExecuteAtCompilerFile)
return false;
return internalCanGenerate(file);
}
protected internal virtual bool internalCanGenerate(CFile file)
{
return true;
}
public bool canGenerate(CDim dim)
{
if (dim.Variables.Count != 0 && dim.Variables[0].Attributes.contains("ExecuteAtCompiler"))
return false;
return internalCanGenerate(dim);
}
protected internal virtual bool internalCanGenerate(CDim dim)
{
if (dim.Variables.Count != 0 && dim.Variables[0].Attributes.contains("ExecuteOnClient"))
return false;
return true;
}
public bool canGenerate(CFunction function)
{
if (function.Attributes.contains("ExecuteAtCompiler") ||
(function.Class != null && function.Class.Attributes.contains("ExecuteAtCompiler")))
return false;
return internalCanGenerate(function);
}
protected internal virtual bool internalCanGenerate(CFunction function)
{
if (function.Attributes.contains("ExecuteOnClient"))
return false;
return true;
}
public bool canGenerate(CClass _class)
{
if (_class.Attributes.contains("ExecuteAtCompiler"))
return false;
return internalCanGenerate(_class);
}
protected internal virtual bool internalCanGenerate(CClass _class)
{
if (_class.Attributes.contains("ExecuteOnClient"))
return false;
return true;
}
public bool canGenerate(CConst _class)
{
if (_class.Attributes.contains("ExecuteAtCompiler"))
return false;
return internalCanGenerate(_class);
}
protected internal virtual bool internalCanGenerate(CConst _class)
{
if (_class.Attributes.contains("ExecuteOnClient"))
return false;
return true;
}
private static Regex startTrim = new Regex(@"^[\s\r\n]+");
private static Regex endTrim = new Regex(@"[\s\r\n]+$");
private static Regex interHTMLnewlines = new Regex(@"\>\s*\r?\n(\s*\r?\n)*\s*\<");
private static Regex endSelect = new Regex(@"(<\s*\/\s*select\s*>)");
private static Regex newlines = new Regex(@"\s*\r?\n(\s*\r?\n)*\s*");
public static string CleanHtml(CHtml html, ref bool startNL, ref bool endNL)
{
bool _start = startNL, _end = endNL;
String s = startTrim.Replace(html.HtmlString, delegate(Match m)
{
_start = m.Value.Contains(("\n"));
return " ";
});
s = endTrim.Replace(s, delegate(Match m)
{
_end = m.Value.Contains(("\n"));
return " ";
});
s = interHTMLnewlines.Replace(s, "> <");
s = newlines.Replace(s, "\n");
s = s.Replace("\r\n", "\n");
startNL = _start;
endNL = _end;
return s;
}
public void VisitLock(CLock @lock)
{
@lock.Accept(visitor);
}
public void VisitOnExit(COnExit onexit)
{
onexit.Accept(visitor);
}
public void VisitOptionalByRef(COptionalByRef optbyref)
{
optbyref.Accept(visitor);
}
public void VisitAttribute(CAttribute attr)
{
attr.Accept(visitor);
}
public void VisitGlobalAccess(CGlobalAccess cga)
{
cga.Accept(visitor);
}
}
}