Skip to content

Commit b95822a

Browse files
committed
Work on overload resolution. Implement type substitution for methods.
1 parent bbfea50 commit b95822a

File tree

4 files changed

+275
-119
lines changed

4 files changed

+275
-119
lines changed

Grammars/CSharp/CSharp.Grammar/CSharp/AST/Expr.nitra

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace DotNet
7575
// Arguments.ParameterScope = ParameterScope;
7676
// Arguments.Scope = Scope.HideWith(ParameterScope);
7777
// Arguments.IndexIn = 0;
78-
// Arguments.ParentParameterScope = Ref.Symbol.ParameterScope;
78+
// Arguments.ParentParameterScope = ???;
7979
// Arguments.Used = true;
8080
//
8181
// stage 1:
@@ -367,9 +367,8 @@ namespace DotNet
367367
Left.ParameterScope = ParameterScope;
368368

369369
Arguments.ParameterScope = ParameterScope;
370-
Arguments.ScopeIn = ScopeIn.HideWith(ParameterScope);
370+
Arguments.ScopeIn = ScopeIn;
371371
Arguments.IndexIn = 0;
372-
Arguments.ParentParameterScope = Func.Symbol.ParameterScope;
373372
Arguments.Used = true;
374373

375374

@@ -399,7 +398,7 @@ namespace DotNet
399398
// Arguments.ParameterScope = ParameterScope;
400399
// Arguments.Scope = Scope.HideWith(ParameterScope);
401400
// Arguments.IndexIn = 0;
402-
// Arguments.ParentParameterScope = Method.Symbol.ParameterScope;
401+
// Arguments.ParentParameterScope = ???;
403402
// Arguments.Used = true;
404403
// Type = Method.Symbol.ReturnType;
405404
Type = context.GetVoidType();
@@ -512,7 +511,6 @@ namespace DotNet
512511
{
513512
inout Index : int;
514513
IndexOut = IndexIn + 1;
515-
in ParentParameterScope : TableScope;
516514
// Name : Reference?;
517515
// Modifier : ArgumentModifier?;
518516
}
@@ -524,7 +522,7 @@ namespace DotNet
524522
Expr.ParameterScope = ParameterScope;
525523
Expr.ScopeIn = ScopeOut;
526524
Expr.Used = true;
527-
ExpectedType = ParentParameterScope.GetParameterType(IndexIn);
525+
//ExpectedType = ???;
528526
Type = Expr.Type;
529527
stage 1:
530528
out ArgumentType : TypeSymbol = Type;

Nitra/DotNetLang/DotNetLang.nproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
<Compile Include="IDotNetBackEnd.n">
121121
<SubType>Code</SubType>
122122
</Compile>
123+
<Compile Include="Scopes\SubstHelper.n">
124+
<SubType>Code</SubType>
125+
</Compile>
123126
<Compile Include="Statments\Statement.nitra">
124127
<SubType>Code</SubType>
125128
</Compile>
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
using Nitra;
2+
using Nitra.Declarations;
3+
using Nemerle;
4+
using Nemerle.Collections;
5+
using Nemerle.Text;
6+
using Nemerle.Utility;
7+
8+
using System;
9+
using System.Collections.Immutable;
10+
using System.Diagnostics;
11+
12+
using System.Linq;
13+
using SCG = System.Collections.Generic;
14+
15+
namespace DotNet
16+
{
17+
public partial class TopSubstScope : Scope
18+
{
19+
private TrySubst(ogigin : TypeSymbol) : TypeSymbol
20+
{
21+
match (ogigin)
22+
{
23+
| TypeParameterSymbol as parameterSymbol => TrySubstTypeParameterSymbol(parameterSymbol)
24+
| FunctionTypeSymbol as func => TrySubst(func)
25+
| ConstructedTypeSymbol as cts =>
26+
def typeInfo = cts.TypeInfo;
27+
def args = cts.Args;
28+
def substArgs = TrySubsts(args);
29+
def isNotChanged = args.Equals(substArgs);
30+
match (cts)
31+
{
32+
| TopConstructedTypeSymbol when isNotChanged => cts
33+
| TopConstructedTypeSymbol => AstUtils.CreateConstructedTypeSymbol(typeInfo, substArgs, DependentPropertyEvalContext(null))
34+
//| NestedConstructedTypeSymbol as cts when isNotChanged
35+
| NestedConstructedTypeSymbol as cts => AstUtils.CreateNestedConstructedTypeSymbol(cts.Parent, typeInfo, substArgs, DependentPropertyEvalContext(null))
36+
| _ => assert2(false); assert(false)
37+
}
38+
39+
| _ => ogigin
40+
}
41+
}
42+
43+
private TrySubsts(types : ImmutableArray[TypeSymbol]) : ImmutableArray[TypeSymbol]
44+
{
45+
def builder = ImmutableArray.CreateBuilder(types.Length);
46+
foreach (type in types)
47+
builder.Add(TrySubst(type));
48+
builder.MoveToImmutable()
49+
}
50+
51+
public SubstMethod(ogigin : Member.MethodSymbol) : Member.MethodSymbol
52+
{
53+
def subst = Member.MethodSymbol();
54+
CopyDeclaration (subst, ogigin);
55+
SubstCustomAttributeHost(subst, ogigin);
56+
SubstModifierHost (subst, ogigin);
57+
SubstTypeMember (subst, ogigin);
58+
SubstFunctional (subst, ogigin);
59+
SubstGenericEntity (subst, ogigin);
60+
subst
61+
}
62+
63+
public SubstProperty(ogigin : Member.PropertySymbol) : Member.PropertySymbol
64+
{
65+
def subst = Member.PropertySymbol();
66+
CopyDeclaration (subst, ogigin);
67+
SubstCustomAttributeHost(subst, ogigin);
68+
SubstModifierHost (subst, ogigin);
69+
SubstTypeMember (subst, ogigin);
70+
when (ogigin.IsTypeEvaluated)
71+
{
72+
subst.Type = TrySubst(ogigin.Type);
73+
subst.Type_Location = ogigin.Type_Location;
74+
}
75+
when (ogigin.IsGetterEvaluated)
76+
{
77+
if (ogigin.Getter is Some(value))
78+
subst.Getter = Some(SubstMethod(value));
79+
else
80+
subst.Getter = None();
81+
subst.Getter_Location = ogigin.Getter_Location;
82+
}
83+
when (ogigin.IsSetterEvaluated)
84+
{
85+
if (ogigin.Setter is Some(value))
86+
subst.Getter = Some(SubstMethod(value));
87+
else
88+
subst.Getter = None();
89+
subst.Setter_Location = ogigin.Setter_Location;
90+
}
91+
subst
92+
}
93+
94+
private TrySubst(ogigin : FunctionTypeSymbol) : FunctionTypeSymbol
95+
{
96+
mutable isNotChanged;
97+
mutable parameterTypesSubsts;
98+
mutable returnSubst;
99+
100+
when (ogigin.IsParametersEvaluated)
101+
{
102+
parameterTypesSubsts = TrySubsts(ogigin.Parameters);
103+
104+
isNotChanged = parameterTypesSubsts.SequenceEqual(ogigin.Parameters);
105+
}
106+
107+
when (ogigin.IsReturnEvaluated)
108+
{
109+
returnSubst = TrySubst(ogigin.Return);
110+
isNotChanged &= returnSubst.Equals(ogigin.Return);
111+
}
112+
113+
if (isNotChanged)
114+
ogigin
115+
else
116+
{
117+
def subst = FunctionTypeSymbol();
118+
CopyDeclaration(subst, ogigin);
119+
when (!parameterTypesSubsts.IsDefault)
120+
subst.Parameters = parameterTypesSubsts;
121+
when (returnSubst != null)
122+
subst.Return = returnSubst;
123+
subst.EvalProperties(DependentPropertyEvalContext(null));
124+
subst
125+
}
126+
}
127+
128+
private TrySubstTypeParameters(typeParameters : ImmutableArray[TypeSymbol]) : ImmutableArray[TypeSymbol]
129+
{
130+
def builder = ImmutableArray.CreateBuilder(typeParameters.Length);
131+
foreach (type in typeParameters)
132+
builder.Add(TrySubst(type));
133+
builder.MoveToImmutable()
134+
}
135+
136+
private CopyDeclaration(subst : DeclarationSymbol, ogigin : DeclarationSymbol) : void
137+
{
138+
foreach (decl in ogigin.GetDeclarationsUntyped())
139+
subst.AddDeclaration(decl);
140+
141+
when (ogigin.IsKindEvaluated)
142+
{
143+
subst.Kind = ogigin.Kind;
144+
subst.Kind_Location = ogigin.Kind_Location;
145+
}
146+
when (ogigin.IsSpanClassEvaluated)
147+
{
148+
subst.SpanClass = ogigin.SpanClass;
149+
subst.SpanClass_Location = ogigin.SpanClass_Location;
150+
}
151+
when (ogigin.IsScopeEvaluated)
152+
{
153+
subst.Scope = ogigin.Scope;
154+
subst.Scope_Location = ogigin.Scope_Location;
155+
}
156+
when (ogigin.IsDeclaredInOptEvaluated)
157+
{
158+
subst.DeclaredInOpt = ogigin.DeclaredInOpt;
159+
subst.DeclaredInOpt_Location = ogigin.DeclaredInOpt_Location;
160+
}
161+
}
162+
163+
private SubstCustomAttributeHost(subst : CustomAttributeHostDeclarationSymbol, ogigin : CustomAttributeHostDeclarationSymbol) : void
164+
{
165+
when (ogigin.IsCustomAttributesEvaluated)
166+
{
167+
subst.CustomAttributes = ogigin.CustomAttributes;
168+
subst.CustomAttributes_Location = ogigin.CustomAttributes_Location;
169+
}
170+
}
171+
172+
private SubstModifierHost(subst : ModifierHostSymbol, ogigin : ModifierHostSymbol) : void
173+
{
174+
when (ogigin.IsFlagsEvaluated)
175+
{
176+
subst.Flags = ogigin.Flags;
177+
subst.Flags_Location = ogigin.Flags_Location;
178+
}
179+
}
180+
181+
private SubstFormalParameter(ogigin : FormalParameterSymbol) : FormalParameterSymbol
182+
{
183+
def subst = FormalParameterSymbol();
184+
CopyDeclaration(subst, ogigin);
185+
SubstCustomAttributeHost(subst, ogigin);
186+
when (ogigin.IsTypeEvaluated)
187+
{
188+
subst.Type = TrySubst(ogigin.Type);
189+
subst.Type_Location = ogigin.Type_Location;
190+
}
191+
when (ogigin.IsModifierEvaluated)
192+
{
193+
subst.Modifier = ogigin.Modifier;
194+
subst.Modifier_Location = ogigin.Modifier_Location;
195+
}
196+
when (ogigin.IsIndexEvaluated)
197+
{
198+
subst.Index = ogigin.Index;
199+
subst.Index_Location = ogigin.Index_Location;
200+
}
201+
subst.EvalProperties(DependentPropertyEvalContext(null));
202+
subst
203+
}
204+
205+
private SubstFormalParameters(parameters : ImmutableArray[FormalParameterSymbol]) : ImmutableArray[FormalParameterSymbol]
206+
{
207+
def builder = ImmutableArray.CreateBuilder(parameters.Length);
208+
foreach (type in parameters)
209+
builder.Add(SubstFormalParameter(type));
210+
builder.MoveToImmutable()
211+
}
212+
213+
private SubstTypeMember(subst : TypeMemberSymbol, ogigin : TypeMemberSymbol) : void
214+
{
215+
when (ogigin.IsDeclaredInEvaluated)
216+
{
217+
subst.DeclaredIn = ogigin.DeclaredIn;
218+
subst.DeclaredIn_Location = ogigin.DeclaredIn_Location;
219+
}
220+
}
221+
222+
private SubstParametrizable(subst : FunctionalSymbol, ogigin : FunctionalSymbol) : void
223+
{
224+
// TODO: Надо сделать подстановку для table ParameterScope;
225+
when (ogigin.IsParametersEvaluated)
226+
{
227+
subst.Parameters = SubstFormalParameters(ogigin.Parameters);
228+
subst.Parameters_Location = ogigin.Parameters_Location;
229+
}
230+
}
231+
232+
private SubstFunctional(subst : FunctionalSymbol, ogigin : FunctionalSymbol) : void
233+
{
234+
SubstParametrizable(subst, ogigin);
235+
236+
when (ogigin.IsReturnTypeEvaluated)
237+
{
238+
subst.ReturnType = TrySubst(ogigin.ReturnType);
239+
subst.ReturnType_Location = ogigin.ReturnType_Location;
240+
}
241+
when (ogigin.IsTypeEvaluated)
242+
{
243+
subst.Type = TrySubst(ogigin.Type);
244+
subst.Type_Location = ogigin.Type_Location;
245+
}
246+
}
247+
248+
private SubstGenericEntity(subst : GenericEntitySymbol, ogigin : GenericEntitySymbol) : void
249+
{
250+
when (ogigin.IsTypeParametersCountEvaluated)
251+
{
252+
subst.TypeParametersCount = ogigin.TypeParametersCount;
253+
subst.TypeParametersCount_Location = ogigin.TypeParametersCount_Location;
254+
}
255+
when (ogigin.IsTypeParametersEvaluated)
256+
{
257+
subst.TypeParameters = TrySubstTypeParameters(ogigin.TypeParameters);
258+
subst.TypeParameters_Location = ogigin.TypeParameters_Location;
259+
}
260+
}
261+
} // module
262+
} // namespace

0 commit comments

Comments
 (0)