Skip to content

Commit 1bb6dc6

Browse files
author
aBothe
committed
[Resolver] #130 Fixed 'int[string]' resolution.
1 parent 41655fc commit 1bb6dc6

File tree

4 files changed

+62
-46
lines changed

4 files changed

+62
-46
lines changed

DParser2/Resolver/DType.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public class ArrayType : AssocArrayType
266266
public readonly bool IsStaticArray;
267267

268268
public ArrayType(AbstractType ValueType, ISyntaxRegion td)
269-
: base(ValueType, new PrimitiveType(DTokens.Int, 0), td) { FixedLength = -1; }
269+
: base(ValueType, null, td) { FixedLength = -1; }
270270

271271
public ArrayType(AbstractType ValueType, int ArrayLength, ISyntaxRegion td)
272272
: this(ValueType, td)
@@ -300,11 +300,8 @@ public class AssocArrayType : DerivedDataType
300300
public bool IsString
301301
{
302302
get{
303-
var kt = DResolver.StripMemberSymbols (KeyType);
304-
return (kt == null || (kt is PrimitiveType &&
305-
((kt as PrimitiveType).TypeToken == DTokens.Int))) &&
306-
(kt = DResolver.StripMemberSymbols (ValueType)) is PrimitiveType &&
307-
DTokens.IsBasicType_Character((kt as PrimitiveType).TypeToken);
303+
var pt = DResolver.StripMemberSymbols(ValueType) as PrimitiveType;
304+
return this is ArrayType && pt != null && DTokens.IsBasicType_Character(pt.TypeToken);
308305
}
309306
}
310307

DParser2/Resolver/ResultComparer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static class ResultComparer
1818
/// </summary>
1919
public static bool IsEqual(ISemantic r1, ISemantic r2)
2020
{
21+
if (r1 == r2)
22+
return true;
23+
2124
if (r1 is TemplateParameterSymbol)
2225
{
2326
var tps1 = r1 as TemplateParameterSymbol;

DParser2/Resolver/TypeResolution/TypeDeclarationResolver.cs

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -302,55 +302,27 @@ public static AbstractType ResolveKey(ArrayDecl ad, out int fixedArrayLength, ou
302302
{
303303
keyVal = null;
304304
fixedArrayLength = -1;
305-
AbstractType keyType;
306305

307306
if (ad.KeyExpression != null)
308307
{
309-
//TODO: Template instance expressions?
310-
var id_x = ad.KeyExpression as IdentifierExpression;
311-
if (id_x != null && id_x.IsIdentifier)
312-
{
313-
var id = new IdentifierDeclaration((string)id_x.Value)
314-
{
315-
Location = id_x.Location,
316-
EndLocation = id_x.EndLocation
317-
};
318-
319-
keyType = TypeDeclarationResolver.ResolveSingle(id, ctxt);
320-
321-
if (keyType != null)
322-
{
323-
var tt = keyType as MemberSymbol;
324-
325-
if (tt == null ||
326-
!(tt.Definition is DVariable) ||
327-
((DVariable)tt.Definition).Initializer == null)
328-
return keyType;
329-
}
330-
}
308+
keyVal = Evaluation.EvaluateValue(ad.KeyExpression, ctxt);
331309

332-
try
310+
if (keyVal != null)
333311
{
334-
keyVal = Evaluation.EvaluateValue(ad.KeyExpression, ctxt);
335-
336-
if (keyVal != null)
312+
// It should be mostly a number only that points out how large the final array should be
313+
var pv = Evaluation.GetVariableContents(keyVal, new StandardValueProvider(ctxt)) as PrimitiveValue;
314+
if (pv != null)
337315
{
338-
// Take the value's type as array key type
339-
keyType = keyVal.RepresentedType;
340-
341-
// It should be mostly a number only that points out how large the final array should be
342-
var pv = Evaluation.GetVariableContents(keyVal, new StandardValueProvider(ctxt)) as PrimitiveValue;
343-
if (pv != null)
344-
{
345-
fixedArrayLength = System.Convert.ToInt32(pv.Value);
316+
fixedArrayLength = System.Convert.ToInt32(pv.Value);
346317

347-
if (fixedArrayLength < 0)
348-
ctxt.LogError(ad, "Invalid array size: Length value must be greater than 0");
349-
}
350-
//TODO Is there any other type of value allowed?
318+
if (fixedArrayLength < 0)
319+
ctxt.LogError(ad, "Invalid array size: Length value must be greater than 0");
351320
}
321+
//TODO Is there any other type of value allowed?
322+
else
323+
// Take the value's type as array key type
324+
return keyVal.RepresentedType;
352325
}
353-
catch { }
354326
}
355327
else if(ad.KeyType != null)
356328
return ResolveSingle(ad.KeyType, ctxt);

Tests/ResolutionTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,50 @@ enum mye
685685
t = ExpressionTypeEvaluation.EvaluateType (x, ctxt);
686686
Assert.That (t, Is.Null);
687687
}
688+
689+
[Test]
690+
public void ArrayTypes()
691+
{
692+
var ctxt = CreateCtxt("A", @"module A;");
693+
694+
ITypeDeclaration td;
695+
AssocArrayType aa;
696+
ArrayType at;
697+
698+
td = DParser.ParseBasicType("int[int]");
699+
aa = TypeDeclarationResolver.ResolveSingle(td, ctxt) as AssocArrayType;
700+
Assert.That(aa, Is.Not.TypeOf(typeof(ArrayType)));
701+
Assert.That(aa.KeyType, Is.TypeOf(typeof(PrimitiveType)));
702+
Assert.That((aa.KeyType as PrimitiveType).TypeToken, Is.EqualTo(DTokens.Int));
703+
Assert.That(aa.ValueType, Is.TypeOf(typeof(PrimitiveType)));
704+
705+
td = DParser.ParseBasicType("int[short]");
706+
aa = TypeDeclarationResolver.ResolveSingle(td, ctxt) as AssocArrayType;
707+
Assert.That(aa, Is.Not.TypeOf(typeof(ArrayType)));
708+
Assert.That(aa.KeyType, Is.TypeOf(typeof(PrimitiveType)));
709+
Assert.That((aa.KeyType as PrimitiveType).TypeToken, Is.EqualTo(DTokens.Short));
710+
Assert.That(aa.ValueType, Is.TypeOf(typeof(PrimitiveType)));
711+
712+
td = DParser.ParseBasicType("int[string]");
713+
aa = TypeDeclarationResolver.ResolveSingle(td, ctxt) as AssocArrayType;
714+
Assert.That(aa, Is.Not.TypeOf(typeof(ArrayType)));
715+
Assert.That(aa.KeyType, Is.TypeOf(typeof(ArrayType)));
716+
Assert.That((aa.KeyType as ArrayType).IsString);
717+
Assert.That(aa.ValueType, Is.TypeOf(typeof(PrimitiveType)));
718+
aa = null;
719+
720+
td = DParser.ParseBasicType("byte[3]");
721+
at = TypeDeclarationResolver.ResolveSingle(td, ctxt) as ArrayType;
722+
Assert.That(at.FixedLength, Is.EqualTo(3));
723+
Assert.That(at.KeyType, Is.Null);
724+
Assert.That(at.ValueType, Is.TypeOf(typeof(PrimitiveType)));
725+
726+
td = DParser.ParseBasicType("byte[6L]");
727+
at = TypeDeclarationResolver.ResolveSingle(td, ctxt) as ArrayType;
728+
Assert.That(at.FixedLength, Is.EqualTo(6));
729+
Assert.That(at.KeyType, Is.Null);
730+
Assert.That(at.ValueType, Is.TypeOf(typeof(PrimitiveType)));
731+
}
688732

689733
[Test]
690734
public void ArrayIndexer()

0 commit comments

Comments
 (0)