-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDeclarationVisitor.cs
More file actions
141 lines (122 loc) · 6.18 KB
/
Copy pathDeclarationVisitor.cs
File metadata and controls
141 lines (122 loc) · 6.18 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
using HydraScript.Application.StaticAnalysis.Exceptions;
using HydraScript.Domain.FrontEnd.Parser;
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations;
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations.AfterTypesAreLoaded;
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexLiterals;
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
using HydraScript.Domain.IR.Impl.Symbols;
using HydraScript.Domain.IR.Impl.Symbols.Ids;
using HydraScript.Domain.IR.Types;
namespace HydraScript.Application.StaticAnalysis.Visitors;
internal class DeclarationVisitor : VisitorNoReturnBase<IAbstractSyntaxTreeNode>,
IVisitor<LexicalDeclaration>,
IVisitor<FunctionDeclaration>
{
private readonly IHydraScriptTypesService _typesService;
private readonly IFunctionWithUndefinedReturnStorage _functionStorage;
private readonly IMethodStorage _methodStorage;
private readonly ISymbolTableStorage _symbolTables;
private readonly IAmbiguousInvocationStorage _ambiguousInvocations;
private readonly IVisitor<TypeValue, Type> _typeBuilder;
private readonly IVisitor<FunctionDeclaration, ReturnAnalyzerResult> _returnAnalyzer;
public DeclarationVisitor(
IHydraScriptTypesService typesService,
IFunctionWithUndefinedReturnStorage functionStorage,
IMethodStorage methodStorage,
ISymbolTableStorage symbolTables,
IAmbiguousInvocationStorage ambiguousInvocations,
IVisitor<TypeValue, Type> typeBuilder,
IVisitor<FunctionDeclaration, ReturnAnalyzerResult> returnAnalyzer)
{
_typesService = typesService;
_functionStorage = functionStorage;
_methodStorage = methodStorage;
_symbolTables = symbolTables;
_ambiguousInvocations = ambiguousInvocations;
_typeBuilder = typeBuilder;
_returnAnalyzer = returnAnalyzer;
}
public override VisitUnit Visit(IAbstractSyntaxTreeNode visitable)
{
for (var i = 0; i < visitable.Count; i++)
visitable[i].Accept(This);
return default;
}
public VisitUnit Visit(LexicalDeclaration visitable)
{
for (var i = 0; i < visitable.Assignments.Count; i++)
{
var assignment = visitable.Assignments[i];
if (_symbolTables[visitable.Scope].ContainsSymbol(new VariableSymbolId(assignment.Destination.Id)))
throw new DeclarationAlreadyExists(assignment.Destination.Id);
var destinationType = assignment.DestinationType?.Accept(_typeBuilder) ?? _typesService.Undefined;
if (destinationType == _typesService.Undefined &&
assignment.Source is ImplicitLiteral or ArrayLiteral { Expressions.Count: 0 })
throw visitable.ReadOnly
? new ConstWithoutInitializer(assignment.Destination.Id)
: new CannotDefineType(assignment.Destination.Id.Segment);
_symbolTables[visitable.Scope].AddSymbol(
new VariableSymbol(
assignment.Destination.Id,
destinationType));
}
return default;
}
public VisitUnit Visit(FunctionDeclaration visitable)
{
var returnAnalyzerResult = visitable.Accept(_returnAnalyzer);
visitable.ReturnStatements = returnAnalyzerResult.ReturnStatements;
visitable.AllCodePathsEndedWithReturn = returnAnalyzerResult.CodePathEndedWithReturn;
var parentTable = _symbolTables[visitable.Parent?.Scope ?? Scope.Empty];
var parameters = new List<Type>();
for (var i = 0; i < visitable.Arguments.Count; i++)
{
parameters.Add(visitable.Arguments[i].TypeValue.Accept(_typeBuilder));
var arg = new VariableSymbol(visitable.Arguments[i].Name, parameters[i]);
arg.Initialize();
_symbolTables[visitable.Scope].AddSymbol(arg);
}
var functionSymbolId = new FunctionSymbolId(visitable.Name, parameters);
_ambiguousInvocations.Clear(functionSymbolId);
visitable.ComputedFunctionAddress = functionSymbolId.ToString();
var functionSymbol = new FunctionSymbol(
visitable.Name,
parameters,
visitable.ReturnTypeValue.Accept(_typeBuilder),
visitable.IsEmpty);
if (functionSymbolId.Equals(parentTable.FindSymbol(functionSymbolId)?.Id))
throw new OverloadAlreadyExists(visitable.Name, functionSymbolId);
if (parameters is [ObjectType methodOwner, ..] && visitable.Arguments is [{ TypeValue: TypeIdentValue }, ..])
_methodStorage.BindMethod(methodOwner, functionSymbol, functionSymbolId);
if (functionSymbol.Type.Equals(_typesService.Undefined))
{
if (visitable.HasReturnStatement)
_functionStorage.Save(functionSymbol, visitable);
else
functionSymbol.DefineReturnType(_typesService.Void);
}
parentTable.AddSymbol(functionSymbol);
for (var i = visitable.IndexOfFirstDefaultArgument; i < visitable.Arguments.Count; i++)
{
if (visitable.Arguments[i].Info.Type is ValueDtoType.Name)
throw new NamedArgumentAfterDefaultValueArgument(
visitable.Segment,
function: visitable.Name,
visitable.Arguments[i]);
var overload = new FunctionSymbolId(visitable.Name, parameters[..i]);
var existing = parentTable.FindSymbol(overload);
var functionToAdd = existing is not null && existing < functionSymbol
? existing
: functionSymbol;
parentTable.AddSymbol(functionToAdd, overload);
if (parameters is [ObjectType overloadOwner, ..] && visitable.Arguments is [{ TypeValue: TypeIdentValue }, ..])
_methodStorage.BindMethod(overloadOwner, functionToAdd, overload);
if (existing is not null && !existing.Id.Equals(overload))
{
_ambiguousInvocations.WriteCandidate(overload, existing.Id);
_ambiguousInvocations.WriteCandidate(overload, functionSymbolId);
}
}
return visitable.Statements.Accept(This);
}
}