-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathCompilerUtils.cs
665 lines (558 loc) · 24.7 KB
/
CompilerUtils.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
using ProtoCore.AST;
using ProtoCore.AST.AssociativeAST;
using ProtoCore.DSASM;
using ProtoCore.Namespace;
using ProtoCore.Properties;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ProtoCore.BuildData;
using ProtoCore.SyntaxAnalysis;
namespace ProtoCore.Utils
{
/* Variable Line Struct
* Used to present the pair value of Line number and Variable Name.
*
* @variable : string, name of the variable refered to
* @line : int, line number of the variable in the code block
*/
public struct VariableLine
{
#region Struct Data Member
public int line;
public string variable;
public string displayName;
#endregion
#region Struct Public Methods
public VariableLine(string variable, int line)
{
if (line < 0)
throw new ArgumentException("Argument cannot be negative", "line");
if (string.IsNullOrEmpty(variable))
throw new ArgumentException("Invalid variable name", "variable");
this.displayName = this.variable = variable;
this.line = line;
}
public VariableLine(int line) // Only used for NUnit purposes (with temp names).
{
if (line < 0)
throw new ArgumentException("Argument cannot be negative", "line");
this.displayName = this.variable = string.Empty; // In NUnit temp names are ignored.
this.line = line;
}
public static bool operator ==(VariableLine variableLine1, VariableLine variableLine2)
{
return variableLine1.Equals(variableLine2);
}
public static bool operator !=(VariableLine variableLine1, VariableLine variableLine2)
{
return !variableLine1.Equals(variableLine2);
}
/*
* Check if two objects have same Variable Name and Line Number.
* @return true if they have same Variable Name and Line Number or both are null
* false if otherwise
*/
public override bool Equals(object obj)
{
if (obj is VariableLine)
{
return this.Equals((VariableLine)obj);
}
return false;
}
/*
* Get Hashcode from combining Hashcode of two attribute @variable and @line
*/
public override int GetHashCode()
{
return variable.GetHashCode() ^ line;
}
#endregion
#region Struct Private Methods
private bool Equals(VariableLine variableLine)
{
return (this.variable == variableLine.variable) && (this.line == variableLine.line);
}
#endregion
}
public class ParseParam
{
private Dictionary<string, string> unboundIdentifiers;
private List<AssociativeNode> parsedNodes;
private List<AssociativeNode> commentNodes;
private List<BuildData.ErrorEntry> errors;
private List<BuildData.WarningEntry> warnings;
public ParseParam(System.Guid postfixGuid, System.String code, ElementResolver elementResolver)
{
this.PostfixGuid = postfixGuid;
this.OriginalCode = code;
this.ElementResolver = elementResolver;
this.parsedNodes = new List<AssociativeNode>();
this.commentNodes = new List<AssociativeNode>();
this.errors = new List<BuildData.ErrorEntry>();
this.warnings = new List<BuildData.WarningEntry>();
}
public void AppendUnboundIdentifier(string displayName, string identifier)
{
if (this.unboundIdentifiers == null)
this.unboundIdentifiers = new Dictionary<string, string>();
if (!this.unboundIdentifiers.ContainsKey(displayName))
this.unboundIdentifiers.Add(displayName, identifier);
}
public void AppendParsedNodes(IEnumerable<AssociativeNode> parsedNodes)
{
if (parsedNodes == null || !parsedNodes.Any())
return;
this.parsedNodes.AddRange(parsedNodes);
}
public void AppendErrors(IEnumerable<ProtoCore.BuildData.ErrorEntry> errors)
{
if (errors == null || (errors.Count() <= 0))
return;
this.errors.AddRange(errors);
}
public void AppendWarnings(IEnumerable<ProtoCore.BuildData.WarningEntry> warnings)
{
if (warnings == null || !warnings.Any())
return;
this.warnings.AddRange(warnings);
}
public void AppendComments(IEnumerable<AssociativeNode> commentNodes)
{
if (commentNodes == null || !commentNodes.Any())
return;
this.commentNodes.AddRange(commentNodes);
}
#region Public Class Properties
public System.Guid PostfixGuid { get; private set; }
public System.String OriginalCode { get; private set; }
public ElementResolver ElementResolver { get; private set; }
public IDictionary<string, string> UnboundIdentifiers
{
get { return unboundIdentifiers; }
}
public IEnumerable<AssociativeNode> ParsedNodes
{
get { return this.parsedNodes; }
}
public IEnumerable<AssociativeNode> ParsedComments
{
get { return this.commentNodes; }
}
public IEnumerable<ProtoCore.BuildData.ErrorEntry> Errors
{
get { return this.errors; }
}
public IEnumerable<ProtoCore.BuildData.WarningEntry> Warnings
{
get { return this.warnings; }
}
#endregion
}
public static class CompilerUtils
{
/// <summary>
/// Does the first pass of compilation and returns a list of wanrnings in compilation
/// </summary>
/// <param name="code"></param>
/// <param name="core"></param>
/// <param name="blockId"></param>
/// <returns></returns>
public static ProtoCore.BuildStatus PreCompile(string code, Core core, CodeBlockNode codeBlock, out int blockId)
{
blockId = ProtoCore.DSASM.Constants.kInvalidIndex;
try
{
//defining the global Assoc block that wraps the entire .ds source file
ProtoCore.LanguageCodeBlock globalBlock = new ProtoCore.LanguageCodeBlock();
globalBlock.Language = ProtoCore.Language.Associative;
globalBlock.Code = code;
//passing the global Assoc wrapper block to the compiler
ProtoCore.CompileTime.Context context = new ProtoCore.CompileTime.Context();
ProtoCore.Language id = globalBlock.Language;
core.Compilers[id].Compile(out blockId, null, globalBlock, context, codeBlockNode: codeBlock);
core.BuildStatus.ReportBuildResult();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
if (!(ex is ProtoCore.BuildHaltException))
{
throw ex;
}
}
return core.BuildStatus;
}
internal static string ToLiteral(string input)
{
using (var writer = new StringWriter())
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, new CodeGeneratorOptions { IndentString = "\t" });
var literString = writer.ToString();
literString = literString.Replace(string.Format("\" +{0}\t\"", Environment.NewLine), "");
return literString.Substring(1, literString.Length - 2);
}
}
}
public static bool TryLoadAssemblyIntoCore(Core core, string assemblyPath)
{
bool parsingPreloadFlag = core.IsParsingPreloadedAssembly;
bool parsingCbnFlag = core.IsParsingCodeBlockNode;
core.IsParsingPreloadedAssembly = true;
core.IsParsingCodeBlockNode = false;
int blockId;
string importStatement = @"import (""" + ToLiteral(assemblyPath) + @""");";
core.ResetForPrecompilation();
var status = PreCompile(importStatement, core, null, out blockId);
core.IsParsingPreloadedAssembly = parsingPreloadFlag;
core.IsParsingCodeBlockNode = parsingCbnFlag;
return status.ErrorCount == 0;
}
/// <summary>
/// Pre-compiles DS code in code block node,
/// checks for syntax, converts non-assignments to assignments,
/// stores list of AST nodes, errors and warnings
/// Evaluates and stores list of unbound identifiers
/// </summary>
/// <param name="priorNames"></param>
/// <param name="parseParams"> container for compilation related parameters </param>
/// <param name="elementResolver"> classname resolver </param>
/// <returns> true if code compilation succeeds, false otherwise </returns>
public static bool PreCompileCodeBlock(Core core, ref ParseParam parseParams, IDictionary<string, string> priorNames = null)
{
string postfixGuid = parseParams.PostfixGuid.ToString().Replace("-", "_");
// Parse code to generate AST and add temporaries to non-assignment nodes
List<AssociativeNode> astNodes;
List<AssociativeNode> comments;
ParseUserCode(core, parseParams.OriginalCode, postfixGuid, out astNodes, out comments);
parseParams.AppendErrors(core.BuildStatus.Errors);
if (parseParams.Errors.Count() > 0)
{
return false;
}
// Catch the syntax errors and errors for unsupported
// language constructs thrown by compile expression
parseParams.AppendWarnings(core.BuildStatus.Warnings);
var warnings = Check(astNodes);
parseParams.AppendWarnings(warnings);
parseParams.AppendParsedNodes(astNodes.Where(n => !n.skipMe));
parseParams.AppendComments(comments);
// Compile the code to get the resultant unboundidentifiers
// and any errors or warnings that were caught by the compiler and cache them in parseParams
return CompileCodeBlockAST(core, parseParams, priorNames);
}
private static bool CompileCodeBlockAST(Core core, ParseParam parseParams, IDictionary<string, string> priorNames)
{
var unboundIdentifiers = new Dictionary<int, List<VariableLine>>();
ProtoCore.BuildStatus buildStatus = null;
try
{
int blockId = ProtoCore.DSASM.Constants.kInvalidIndex;
bool parsingPreloadFlag = core.IsParsingPreloadedAssembly;
bool parsingCbnFlag = core.IsParsingPreloadedAssembly;
core.IsParsingPreloadedAssembly = false;
core.IsParsingCodeBlockNode = true;
core.ResetForPrecompilation();
var astNodes = parseParams.ParsedNodes;
// Lookup namespace resolution map in elementResolver to rewrite
// partial classnames with their fully qualified names in ASTs
// before passing them for pre-compilation. If partial class is not found in map,
// update Resolution map in elementResolver with fully resolved name from compiler.
var reWrittenNodes = ElementRewriter.RewriteElementNames(core.ClassTable,
parseParams.ElementResolver, astNodes, core.BuildStatus.LogSymbolConflictWarning);
if (priorNames != null)
{
// Use migration rewriter to migrate old method names to new names based on priorNameHints from LibraryServices
reWrittenNodes = MigrationRewriter.MigrateMethodNames(reWrittenNodes, priorNames, core.BuildStatus.LogDeprecatedMethodWarning);
}
// Clone a disposable copy of AST nodes for PreCompile() as Codegen mutates AST's
// while performing SSA transforms and we want to keep the original AST's
var codeblock = new CodeBlockNode();
var nodes = reWrittenNodes.OfType<AssociativeNode>().Select(NodeUtils.Clone).ToList();
codeblock.Body.AddRange(nodes);
buildStatus = PreCompile(string.Empty, core, codeblock, out blockId);
core.IsParsingCodeBlockNode = parsingCbnFlag;
core.IsParsingPreloadedAssembly = parsingPreloadFlag;
parseParams.AppendErrors(buildStatus.Errors);
parseParams.AppendWarnings(buildStatus.Warnings);
if (buildStatus.ErrorCount > 0)
{
return false;
}
IEnumerable<BuildData.WarningEntry> warnings = buildStatus.Warnings;
// Get the unboundIdentifiers from the warnings
GetInputLines(parseParams.ParsedNodes, warnings, unboundIdentifiers);
foreach (KeyValuePair<int, List<VariableLine>> kvp in unboundIdentifiers)
{
foreach (VariableLine vl in kvp.Value)
parseParams.AppendUnboundIdentifier(vl.displayName, vl.variable);
}
return true;
}
catch (Exception)
{
buildStatus = null;
return false;
}
}
private static void ParseUserCode(ProtoCore.Core core, string expression, string postfixGuid, out List<AssociativeNode> astNodes, out List<AssociativeNode> commentNodes)
{
if (expression != null)
{
expression = expression.Replace("\r\n", "\n");
try
{
ParseUserCodeCore(core, expression, out astNodes, out commentNodes);
return;
}
catch
{
// For class declarations, import statements etc. that are currently ignored
}
}
astNodes = new List<AssociativeNode>();
commentNodes = new List<AssociativeNode>();
}
private static void ParseUserCodeCore(Core core, string expression, out List<AssociativeNode> astNodes, out List<AssociativeNode> commentNodes)
{
astNodes = new List<AssociativeNode>();
core.ResetForPrecompilation();
core.IsParsingCodeBlockNode = true;
core.ParsingMode = ParseMode.AllowNonAssignment;
ParseResult parseResult = ParserUtils.ParseWithCore(expression, core);
commentNodes = ParserUtils.GetAstNodes(parseResult.CommentBlockNode);
var nodes = ParserUtils.GetAstNodes(parseResult.CodeBlockNode);
Validity.Assert(nodes != null);
int index = 0;
int typedIdentIndex = 0;
foreach (var node in nodes)
{
var n = node as AssociativeNode;
Validity.Assert(n != null);
// Append the temporaries only if it is not a function def or class decl
bool isFunctionOrClassDef = n is FunctionDefinitionNode;
if (n is ImportNode)
{
core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported);
}
else if (n is ClassDeclNode)
{
core.BuildStatus.LogSemanticError(Resources.ClassDeclarationNotSupported);
}
else if (isFunctionOrClassDef)
{
// Add node as it is
astNodes.Add(node);
}
else
{
// Handle temporary naming for temporary Binary exp. nodes and non-assignment nodes
var ben = node as BinaryExpressionNode;
if (ben != null && ben.Optr == Operator.assign)
{
var lNode = ben.LeftNode as IdentifierNode;
if (lNode != null && lNode.Value == Constants.kTempProcLeftVar)
{
string name = Constants.kTempVarForNonAssignment + index;
var newNode = new BinaryExpressionNode(new IdentifierNode(name), ben.RightNode);
astNodes.Add(newNode);
index++;
}
else
{
// Add node as it is
astNodes.Add(node);
index++;
}
}
else
{
if (node is TypedIdentifierNode)
{
// e.g. a : int = %tTypedIdent_<Index>;
var ident = new IdentifierNode(Constants.kTempVarForTypedIdentifier + typedIdentIndex);
NodeUtils.CopyNodeLocation(ident, node);
var typedNode = new BinaryExpressionNode(node as TypedIdentifierNode, ident, Operator.assign);
NodeUtils.CopyNodeLocation(typedNode, node);
astNodes.Add(typedNode);
typedIdentIndex++;
}
else
{
string name = Constants.kTempVarForNonAssignment + index;
var newNode = new BinaryExpressionNode(new IdentifierNode(name), n);
astNodes.Add(newNode);
index++;
}
}
}
}
}
private static void GetInputLines(IEnumerable<Node> astNodes,
IEnumerable<BuildData.WarningEntry> warnings,
Dictionary<int, List<VariableLine>> inputLines)
{
List<VariableLine> warningVLList = GetVarLineListFromWarning(warnings);
if (warningVLList.Count == 0)
return;
int stmtNumber = 1;
foreach (var node in astNodes)
{
// Only binary expression need warnings.
// Function definition nodes do not have input and output ports
var bNode = node as BinaryExpressionNode;
if (bNode != null)
{
var variableLineList = new List<VariableLine>();
for (int i = 0; i < warningVLList.Count; ++i)
{
var warning = warningVLList[i];
if (warning.line >= bNode.line && warning.line <= bNode.endLine)
{
if (warning.variable.StartsWith(Constants.kTempVarForTypedIdentifier))
{
warning.displayName = bNode.LeftNode.Name;
}
variableLineList.Add(warning);
}
}
if (variableLineList.Count > 0)
{
inputLines.Add(stmtNumber, variableLineList);
}
stmtNumber++;
}
}
}
private static List<VariableLine> GetVarLineListFromWarning(IEnumerable<ProtoCore.BuildData.WarningEntry> warnings)
{
List<VariableLine> result = new List<VariableLine>();
foreach (ProtoCore.BuildData.WarningEntry warningEntry in warnings)
{
if (warningEntry.ID == ProtoCore.BuildData.WarningID.IdUnboundIdentifier)
{
var varName = warningEntry.UnboundVariableSymbolNode.name;
result.Add(new VariableLine()
{
variable = varName,
line = warningEntry.Line,
displayName = varName
});
}
}
return result;
}
internal class VariableFinder : AssociativeAstVisitor
{
private string variable;
public bool Found { get; private set; }
public VariableFinder(string variable)
{
this.variable = variable;
}
public override bool VisitIdentifierNode(IdentifierNode node)
{
if (node.Name == variable)
{
Found = true;
return true;
}
if (node.ArrayDimensions == null)
{
return false;
}
return node.ArrayDimensions.Accept(this);
}
public override bool VisitIdentifierListNode(IdentifierListNode node)
{
if (node.LeftNode is IdentifierNode || node.LeftNode is IdentifierListNode)
{
if (node.RightNode is FunctionCallNode || node.RightNode is IdentifierNode)
{
node.LeftNode.Accept(this);
if (!(node.RightNode is IdentifierNode))
{
node.RightNode.Accept(this);
}
return true;
}
}
return base.VisitIdentifierListNode(node);
}
}
/// <summary>
/// Check does some sanity check, e.g., if a variable is re-defined.
/// </summary>
/// <param name="asts"></param>
private static List<WarningEntry> Check(IEnumerable<AssociativeNode> asts)
{
var warnings = new List<WarningEntry>();
HashSet<string> scope = new HashSet<string>();
foreach (var node in asts)
{
BinaryExpressionNode bnode = node as BinaryExpressionNode;
if (bnode == null || bnode.Optr != Operator.assign)
{
var fNode = node as FunctionDefinitionNode;
if (fNode != null)
{
var fbody = NodeUtils.Clone(fNode.FunctionBody) as CodeBlockNode;
var body = fbody.Body;
warnings.AddRange(Check(body));
fNode.FunctionBody.Body.Clear();
fNode.FunctionBody.Body.AddRange(body.Where(n => !n.skipMe));
}
continue;
}
IdentifierNode ident = bnode.LeftNode as IdentifierNode;
if (ident == null)
{
continue;
}
var variable = ident.Value;
if (!scope.Contains(variable))
{
scope.Add(variable);
VariableFinder finder = new VariableFinder(variable);
var langNode = bnode.RightNode as LanguageBlockNode;
if (langNode != null)
{
var cbn = langNode.CodeBlockNode as CodeBlockNode;
if (cbn != null)
{
var copy = NodeUtils.Clone(cbn) as CodeBlockNode;
warnings.AddRange(Check(copy.Body));
cbn.Body.Clear();
cbn.Body.AddRange(copy.Body.Where(n => !n.skipMe));
}
continue;
}
bnode.RightNode.Accept(finder);
if (finder.Found)
{
warnings.Add(new WarningEntry
{
Message = String.Format(Resources.VariableRecursiveReference, variable),
});
node.skipMe = true;
}
}
else if (ident.ArrayDimensions == null)
{
warnings.Add(new WarningEntry
{
Message = String.Format(Resources.VariableRedifinitionError, variable),
});
node.skipMe = true;
}
}
return warnings;
}
}
}