1
|
package org.momut.ooas.visitors;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
|
5
|
import org.momut.ooas.ast.expressions.Expression;
|
6
|
import org.momut.ooas.ast.expressions.ExpressionKind;
|
7
|
import org.momut.ooas.ast.expressions.ListConstructor;
|
8
|
import org.momut.ooas.ast.expressions.UnaryOperator;
|
9
|
import org.momut.ooas.ast.types.ListType;
|
10
|
import org.momut.ooas.ast.types.Type;
|
11
|
import org.momut.ooas.ast.types.TypeKind;
|
12
|
|
13
|
public class OoaStaticListCastVisitor extends OoaExpressionVisitor
|
14
|
{
|
15
|
private final Type m_leafType;
|
16
|
private ListType m_listType;
|
17
|
|
18
|
public OoaStaticListCastVisitor(ListType targetType)
|
19
|
{
|
20
|
m_leafType = leafType(targetType);
|
21
|
m_listType = targetType;
|
22
|
}
|
23
|
|
24
|
@Override
|
25
|
public void visit(ListConstructor listConstructor)
|
26
|
{
|
27
|
final ArrayList<Expression> casts = new ArrayList<>();
|
28
|
for (final Expression elems: listConstructor.elements()) {
|
29
|
if(elems.kind() == ExpressionKind.ListConstr){
|
30
|
final ListType parent = m_listType;
|
31
|
m_listType = (ListType) m_listType.innerType();
|
32
|
VisitSub(elems, listConstructor);
|
33
|
m_listType = parent;
|
34
|
}
|
35
|
else
|
36
|
{
|
37
|
if(elems.kind() == ExpressionKind.Cast)
|
38
|
elems.SetType(m_leafType);
|
39
|
else
|
40
|
{
|
41
|
final Expression cast = new UnaryOperator(ExpressionKind.Cast, elems, elems.line(), elems.pos());
|
42
|
cast.SetType(m_leafType);
|
43
|
casts.add(cast);
|
44
|
}
|
45
|
}
|
46
|
}
|
47
|
if(!casts.isEmpty())
|
48
|
listConstructor.SetElements(casts);
|
49
|
|
50
|
listConstructor.SetType(m_listType);
|
51
|
}
|
52
|
|
53
|
private Type leafType(Type type)
|
54
|
{
|
55
|
Type result = type;
|
56
|
|
57
|
while(result.kind() == TypeKind.ListType)
|
58
|
result = ((ListType)result).innerType();
|
59
|
|
60
|
return result;
|
61
|
}
|
62
|
|
63
|
}
|