Revision 7
Added by Willibald K. over 8 years ago
OoaReplaceOpaqueVisitor.java | ||
---|---|---|
1 |
/**
|
|
2 |
*
|
|
3 |
* OOAS Compiler
|
|
4 |
*
|
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology.
|
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is
|
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University
|
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of
|
|
9 |
* Technology. All rights reserved.
|
|
10 |
*
|
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
|
12 |
*
|
|
13 |
* If you modify the file please update the list of contributors below to in-
|
|
14 |
* clude your name. Please also stick to the coding convention of using TABs
|
|
15 |
* to do the basic (block-level) indentation and spaces for anything after
|
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious
|
|
17 |
* what this means.) Also, remove all trailing whitespace.
|
|
18 |
*
|
|
19 |
* Contributors:
|
|
20 |
* Willibald Krenn (AIT)
|
|
21 |
* Stephan Zimmerer (AIT)
|
|
22 |
* Markus Demetz (AIT)
|
|
23 |
* Christoph Czurda (AIT)
|
|
24 |
*
|
|
25 |
*/
|
|
1 |
/** |
|
2 |
* |
|
3 |
* OOAS Compiler |
|
4 |
* |
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology. |
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is |
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University |
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of |
|
9 |
* Technology. All rights reserved. |
|
10 |
* |
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED. |
|
12 |
* |
|
13 |
* If you modify the file please update the list of contributors below to in- |
|
14 |
* clude your name. Please also stick to the coding convention of using TABs |
|
15 |
* to do the basic (block-level) indentation and spaces for anything after |
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious |
|
17 |
* what this means.) Also, remove all trailing whitespace. |
|
18 |
* |
|
19 |
* Contributors: |
|
20 |
* Willibald Krenn (AIT) |
|
21 |
* Stephan Zimmerer (AIT) |
|
22 |
* Markus Demetz (AIT) |
|
23 |
* Christoph Czurda (AIT) |
|
24 |
* |
|
25 |
*/ |
|
26 | 26 |
|
27 | 27 |
|
28 |
package org.momut.ooas.visitors;
|
|
29 |
|
|
28 |
package org.momut.ooas.visitors; |
|
29 |
|
|
30 | 30 |
import org.momut.ooas.ast.AstNodeTypeEnum; |
31 | 31 |
import org.momut.ooas.ast.IAst; |
32 | 32 |
import org.momut.ooas.ast.expressions.Expression; |
... | ... | |
46 | 46 |
import org.momut.ooas.ast.types.UlyssesType; |
47 | 47 |
import org.momut.ooas.parser.ParserState; |
48 | 48 |
import org.momut.ooas.utils.exceptions.NotImplementedException; |
49 |
|
|
50 |
/// <summary>
|
|
51 |
/// Replaces all Opaque types (types we could not resolve in the first run of the parser)
|
|
52 |
/// with the resolved ones. Note: The actual resolving has already been done, i.e., the
|
|
53 |
/// resolvedType member is already set to the correct value. Now we just want to get an
|
|
54 |
/// AST that does not contain any OpaqueTypeNodes at all.
|
|
55 |
/// </summary>
|
|
56 |
public final class OoaReplaceOpaqueVisitor extends OoaCompleteAstTraversalVisitor
|
|
57 |
{
|
|
58 |
|
|
59 |
private void ReplaceOpaque(IAst parent)
|
|
60 |
{
|
|
61 |
switch (parent.nodeType())
|
|
62 |
{
|
|
63 |
case type:
|
|
64 |
ReplaceOpaqueInType((UlyssesType)parent);
|
|
65 |
break;
|
|
66 |
case identifier:
|
|
67 |
ReplaceOpaqueInIdentifier((Identifier)parent);
|
|
68 |
break;
|
|
69 |
case expression:
|
|
70 |
ReplaceOpaqueInExpression((Expression)parent);
|
|
71 |
break;
|
|
72 |
default:
|
|
73 |
throw new NotImplementedException();
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
|
|
78 |
private void ReplaceOpaqueInExpression(Expression expression)
|
|
79 |
{
|
|
80 |
switch (expression.kind())
|
|
81 |
{
|
|
82 |
case ObjectConstr:
|
|
83 |
ObjectConstructor c = (ObjectConstructor)expression;
|
|
84 |
c.SetType(((OpaqueType)c.type()).resolvedType());
|
|
85 |
break;
|
|
86 |
case Cast:
|
|
87 |
UnaryOperator unop = (UnaryOperator)expression;
|
|
88 |
unop.SetType(((OpaqueType)unop.type()).resolvedType());
|
|
89 |
break;
|
|
90 |
default:
|
|
91 |
throw new NotImplementedException();
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
|
|
96 |
private void ReplaceOpaqueInIdentifier(Identifier identifier)
|
|
97 |
{
|
|
98 |
switch (identifier.kind())
|
|
99 |
{
|
|
100 |
case AttributeIdentifier:
|
|
101 |
AttributeIdentifier id = (AttributeIdentifier)identifier;
|
|
102 |
assert(id.type() instanceof OpaqueType); // that's why we are here
|
|
103 |
id.SetType(((OpaqueType)id.type()).resolvedType());
|
|
104 |
break;
|
|
105 |
case ExpressionVariableIdentifier:
|
|
106 |
ExpressionVariableIdentifier exprvar = (ExpressionVariableIdentifier)identifier;
|
|
107 |
assert(exprvar.type() instanceof OpaqueType);
|
|
108 |
exprvar.SetType(((OpaqueType)exprvar.type()).resolvedType());
|
|
109 |
break;
|
|
110 |
case LocalVariableIdentifier:
|
|
111 |
LocalVariableIdentifier var = (LocalVariableIdentifier)identifier;
|
|
112 |
assert(var.type() instanceof OpaqueType);
|
|
113 |
var.SetType(((OpaqueType)var.type()).resolvedType());
|
|
114 |
break;
|
|
115 |
case ParameterIdentifier:
|
|
116 |
ParameterIdentifier param = (ParameterIdentifier)identifier;
|
|
117 |
assert(param.type() instanceof OpaqueType);
|
|
118 |
param.SetType(((OpaqueType)identifier.type()).resolvedType());
|
|
119 |
break;
|
|
120 |
default:
|
|
121 |
throw new NotImplementedException();
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
private void ReplaceOpaqueInType(UlyssesType ulyssesType)
|
|
126 |
{
|
|
127 |
switch (ulyssesType.kind())
|
|
128 |
{
|
|
129 |
case FunctionType:
|
|
130 |
FunctionType func = (FunctionType)ulyssesType;
|
|
131 |
if (func.returnType() instanceof OpaqueType)
|
|
132 |
func.SetReturnType(((OpaqueType)func.returnType()).resolvedType());
|
|
133 |
|
|
134 |
for (int cntr = 0; cntr < func.parameter().size(); cntr ++)
|
|
135 |
{
|
|
136 |
UlyssesType t = func.parameter().get(cntr);
|
|
137 |
if (t.kind() == TypeKind.OpaqueType)
|
|
138 |
func.parameter().set(cntr, ((OpaqueType)t).resolvedType());
|
|
139 |
}
|
|
140 |
break;
|
|
141 |
case ListType:
|
|
142 |
ListType alist = (ListType)ulyssesType;
|
|
143 |
if (alist.innerType().kind() == TypeKind.OpaqueType)
|
|
144 |
alist.SetInnerType(((OpaqueType)alist.innerType()).resolvedType());
|
|
145 |
break;
|
|
146 |
case MapType:
|
|
147 |
MapType aMap = (MapType)ulyssesType;
|
|
148 |
if (aMap.fromType().kind() == TypeKind.OpaqueType)
|
|
149 |
aMap.SetFromType(((OpaqueType)aMap.fromType()).resolvedType());
|
|
150 |
if (aMap.toType().kind() == TypeKind.OpaqueType)
|
|
151 |
aMap.SetToType(((OpaqueType)aMap.toType()).resolvedType());
|
|
152 |
break;
|
|
153 |
/*case TypeKind.OoActionSystemType:
|
|
154 |
// handeled by the other cases...
|
|
155 |
break;*/
|
|
156 |
case TupleType:
|
|
157 |
TupleType aTuple = (TupleType)ulyssesType;
|
|
158 |
for (int cntr = 0; cntr < aTuple.innerTypes().size(); cntr ++) {
|
|
159 |
UlyssesType t = aTuple.innerTypes().get(cntr);
|
|
160 |
if (t.kind() == TypeKind.OpaqueType)
|
|
161 |
aTuple.innerTypes().set(cntr, ((OpaqueType)t).resolvedType());
|
|
162 |
}
|
|
163 |
break;
|
|
164 |
default:
|
|
165 |
throw new NotImplementedException();
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
@Override
|
|
170 |
protected void VisitAstElement(IAst subElement, IAst parent)
|
|
171 |
{
|
|
172 |
super.VisitAstElement(subElement, parent);
|
|
173 |
|
|
174 |
/*see whether the subElement is an OpaqueType, we need
|
|
175 |
to replace.*/
|
|
176 |
if ((subElement != null)
|
|
177 |
&& (subElement.nodeType() == AstNodeTypeEnum.type)
|
|
178 |
&& (((UlyssesType)subElement).kind() == TypeKind.OpaqueType))
|
|
179 |
ReplaceOpaque(parent);
|
|
180 |
}
|
|
181 |
|
|
182 |
public OoaReplaceOpaqueVisitor(ParserState aState)
|
|
183 |
{
|
|
184 |
super(aState);
|
|
185 |
}
|
|
49 |
|
|
50 |
/// <summary> |
|
51 |
/// Replaces all Opaque types (types we could not resolve in the first run of the parser) |
|
52 |
/// with the resolved ones. Note: The actual resolving has already been done, i.e., the |
|
53 |
/// resolvedType member is already set to the correct value. Now we just want to get an |
|
54 |
/// AST that does not contain any OpaqueTypeNodes at all. |
|
55 |
/// </summary> |
|
56 |
public final class OoaReplaceOpaqueVisitor extends OoaCompleteAstTraversalVisitor |
|
57 |
{ |
|
58 |
|
|
59 |
private void ReplaceOpaque(IAst parent) |
|
60 |
{ |
|
61 |
switch (parent.nodeType()) |
|
62 |
{ |
|
63 |
case type: |
|
64 |
ReplaceOpaqueInType((UlyssesType)parent); |
|
65 |
break; |
|
66 |
case identifier: |
|
67 |
ReplaceOpaqueInIdentifier((Identifier)parent); |
|
68 |
break; |
|
69 |
case expression: |
|
70 |
ReplaceOpaqueInExpression((Expression)parent); |
|
71 |
break; |
|
72 |
default: |
|
73 |
throw new NotImplementedException(); |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
|
|
78 |
private void ReplaceOpaqueInExpression(Expression expression) |
|
79 |
{ |
|
80 |
switch (expression.kind()) |
|
81 |
{ |
|
82 |
case ObjectConstr: |
|
83 |
ObjectConstructor c = (ObjectConstructor)expression; |
|
84 |
c.SetType(((OpaqueType)c.type()).resolvedType()); |
|
85 |
break; |
|
86 |
case Cast: |
|
87 |
UnaryOperator unop = (UnaryOperator)expression; |
|
88 |
unop.SetType(((OpaqueType)unop.type()).resolvedType()); |
|
89 |
break; |
|
90 |
default: |
|
91 |
throw new NotImplementedException(); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
|
|
96 |
private void ReplaceOpaqueInIdentifier(Identifier identifier) |
|
97 |
{ |
|
98 |
switch (identifier.kind()) |
|
99 |
{ |
|
100 |
case AttributeIdentifier: |
|
101 |
AttributeIdentifier id = (AttributeIdentifier)identifier; |
|
102 |
assert(id.type() instanceof OpaqueType); // that's why we are here |
|
103 |
id.SetType(((OpaqueType)id.type()).resolvedType()); |
|
104 |
break; |
|
105 |
case ExpressionVariableIdentifier: |
|
106 |
ExpressionVariableIdentifier exprvar = (ExpressionVariableIdentifier)identifier; |
|
107 |
assert(exprvar.type() instanceof OpaqueType); |
|
108 |
exprvar.SetType(((OpaqueType)exprvar.type()).resolvedType()); |
|
109 |
break; |
|
110 |
case LocalVariableIdentifier: |
|
111 |
LocalVariableIdentifier var = (LocalVariableIdentifier)identifier; |
|
112 |
assert(var.type() instanceof OpaqueType); |
|
113 |
var.SetType(((OpaqueType)var.type()).resolvedType()); |
|
114 |
break; |
|
115 |
case ParameterIdentifier: |
|
116 |
ParameterIdentifier param = (ParameterIdentifier)identifier; |
|
117 |
assert(param.type() instanceof OpaqueType); |
|
118 |
param.SetType(((OpaqueType)identifier.type()).resolvedType()); |
|
119 |
break; |
|
120 |
default: |
|
121 |
throw new NotImplementedException(); |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 |
private void ReplaceOpaqueInType(UlyssesType ulyssesType) |
|
126 |
{ |
|
127 |
switch (ulyssesType.kind()) |
|
128 |
{ |
|
129 |
case FunctionType: |
|
130 |
FunctionType func = (FunctionType)ulyssesType; |
|
131 |
if (func.returnType() instanceof OpaqueType) |
|
132 |
func.SetReturnType(((OpaqueType)func.returnType()).resolvedType()); |
|
133 |
|
|
134 |
for (int cntr = 0; cntr < func.parameter().size(); cntr ++) |
|
135 |
{ |
|
136 |
UlyssesType t = func.parameter().get(cntr); |
|
137 |
if (t.kind() == TypeKind.OpaqueType) |
|
138 |
func.parameter().set(cntr, ((OpaqueType)t).resolvedType()); |
|
139 |
} |
|
140 |
break; |
|
141 |
case ListType: |
|
142 |
ListType alist = (ListType)ulyssesType; |
|
143 |
if (alist.innerType().kind() == TypeKind.OpaqueType) |
|
144 |
alist.SetInnerType(((OpaqueType)alist.innerType()).resolvedType()); |
|
145 |
break; |
|
146 |
case MapType: |
|
147 |
MapType aMap = (MapType)ulyssesType; |
|
148 |
if (aMap.fromType().kind() == TypeKind.OpaqueType) |
|
149 |
aMap.SetFromType(((OpaqueType)aMap.fromType()).resolvedType()); |
|
150 |
if (aMap.toType().kind() == TypeKind.OpaqueType) |
|
151 |
aMap.SetToType(((OpaqueType)aMap.toType()).resolvedType()); |
|
152 |
break; |
|
153 |
/*case TypeKind.OoActionSystemType: |
|
154 |
// handeled by the other cases... |
|
155 |
break;*/ |
|
156 |
case TupleType: |
|
157 |
TupleType aTuple = (TupleType)ulyssesType; |
|
158 |
for (int cntr = 0; cntr < aTuple.innerTypes().size(); cntr ++) { |
|
159 |
UlyssesType t = aTuple.innerTypes().get(cntr); |
|
160 |
if (t.kind() == TypeKind.OpaqueType) |
|
161 |
aTuple.innerTypes().set(cntr, ((OpaqueType)t).resolvedType()); |
|
162 |
} |
|
163 |
break; |
|
164 |
default: |
|
165 |
throw new NotImplementedException(); |
|
166 |
} |
|
167 |
} |
|
168 |
|
|
169 |
@Override |
|
170 |
protected void VisitAstElement(IAst subElement, IAst parent) |
|
171 |
{ |
|
172 |
super.VisitAstElement(subElement, parent); |
|
173 |
|
|
174 |
/*see whether the subElement is an OpaqueType, we need |
|
175 |
to replace.*/ |
|
176 |
if ((subElement != null) |
|
177 |
&& (subElement.nodeType() == AstNodeTypeEnum.type) |
|
178 |
&& (((UlyssesType)subElement).kind() == TypeKind.OpaqueType)) |
|
179 |
ReplaceOpaque(parent); |
|
180 |
} |
|
181 |
|
|
182 |
public OoaReplaceOpaqueVisitor(ParserState aState) |
|
183 |
{ |
|
184 |
super(aState); |
|
185 |
} |
|
186 | 186 |
} |
Also available in: Unified diff
changing java, cpp, hpp files to unix line endings