Skip to content

Commit

Permalink
Java CG: allow quotes as polymorphic types
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwvj committed Oct 30, 2018
1 parent d9c9271 commit 3ddca59
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 56 deletions.
Expand Up @@ -34,7 +34,7 @@ public class Utils
public static final Object BOOL = new Object();
public static final Object CHAR = new Object();
public static final Object TOKEN = new Object();
// Only basic types and unions of basic types can currently be used as polymorphic type arguments
// Only basic types, quotes and unions of these types can currently be used as polymorphic type arguments
public static final Object TYPE_NOT_SUPPORTED = new Object();

public static boolean isVoidValue(Object value)
Expand Down Expand Up @@ -316,17 +316,17 @@ else if(type == TOKEN)
}
else if(type == TYPE_NOT_SUPPORTED)
{
throw new IllegalArgumentException("Only basic types and unions of basic types " +
throw new IllegalArgumentException("Only basic types, quotes and unions of these types " +
"can currently be used as polymorphic type arguments");
}

if(type instanceof Class)
else if(type instanceof Class)
{
return ((Class) type).isInstance(exp);
}
else
{
return false;
// If we are checking if a value is a quote
return exp == type;
}
}

Expand Down
Expand Up @@ -20,8 +20,13 @@ return [fun[nat](0),
fun[char](42),
fun[token](mk_token("x")),
fun[token](1),
fun[<A>](<A>),
fun[<B>](<A>),
fun2[bool,real](2.5),
fun2[bool,nat](2.5)];
fun2[bool,nat](2.5),
fun2[<A>,<B>](<B>),
fun2[<B>,<C>](<A>)];

functions

fun[@T] : ? -> bool
Expand Down
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testResult type="org.overture.codegen.tests.exec.IsExpJavaGenTest">
<result>
<output object="rO0ABXNyAChvcmcub3ZlcnR1cmUuaW50ZXJwcmV0ZXIudmFsdWVzLlNlcVZhbHVlAAAAAAAAAAECAAFMAAZ2YWx1ZXN0ACtMb3JnL292ZXJ0dXJlL2ludGVycHJldGVyL3ZhbHVlcy9WYWx1ZUxpc3Q7eHIAJW9yZy5vdmVydHVyZS5pbnRlcnByZXRlci52YWx1ZXMuVmFsdWUAAAAAAAAAAQIAAHhwc3IAKW9yZy5vdmVydHVyZS5pbnRlcnByZXRlci52YWx1ZXMuVmFsdWVMaXN0Uhk0i4Rc9FoCAAB4cgAQamF2YS51dGlsLlZlY3RvctmXfVuAO68BAwADSQARY2FwYWNpdHlJbmNyZW1lbnRJAAxlbGVtZW50Q291bnRbAAtlbGVtZW50RGF0YXQAE1tMamF2YS9sYW5nL09iamVjdDt4cAAAAAAAAAASdXIAE1tMamF2YS5sYW5nLk9iamVjdDuQzlifEHMpbAIAAHhwAAAAFHNyACxvcmcub3ZlcnR1cmUuaW50ZXJwcmV0ZXIudmFsdWVzLkJvb2xlYW5WYWx1ZQAAAAAAAAABAgABWgAFdmFsdWV4cQB+AAIBc3EAfgAKAHNxAH4ACgFzcQB+AAoAc3EAfgAKAXNxAH4ACgBzcQB+AAoBc3EAfgAKAHNxAH4ACgFzcQB+AAoAc3EAfgAKAXNxAH4ACgBzcQB+AAoBc3EAfgAKAHNxAH4ACgFzcQB+AAoAc3EAfgAKAXNxAH4ACgBwcHg=" resource="IsExpPolyType.vdmpp" value="[true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false]"/>
<output object="rO0ABXNyAChvcmcub3ZlcnR1cmUuaW50ZXJwcmV0ZXIudmFsdWVzLlNlcVZhbHVlAAAAAAAAAAECAAFMAAZ2YWx1ZXN0ACtMb3JnL292ZXJ0dXJlL2ludGVycHJldGVyL3ZhbHVlcy9WYWx1ZUxpc3Q7eHIAJW9yZy5vdmVydHVyZS5pbnRlcnByZXRlci52YWx1ZXMuVmFsdWUAAAAAAAAAAQIAAHhwc3IAKW9yZy5vdmVydHVyZS5pbnRlcnByZXRlci52YWx1ZXMuVmFsdWVMaXN0Uhk0i4Rc9FoCAAB4cgAQamF2YS51dGlsLlZlY3RvctmXfVuAO68BAwADSQARY2FwYWNpdHlJbmNyZW1lbnRJAAxlbGVtZW50Q291bnRbAAtlbGVtZW50RGF0YXQAE1tMamF2YS9sYW5nL09iamVjdDt4cAAAAAAAAAAWdXIAE1tMamF2YS5sYW5nLk9iamVjdDuQzlifEHMpbAIAAHhwAAAAKHNyACxvcmcub3ZlcnR1cmUuaW50ZXJwcmV0ZXIudmFsdWVzLkJvb2xlYW5WYWx1ZQAAAAAAAAABAgABWgAFdmFsdWV4cQB+AAIBc3EAfgAKAHNxAH4ACgFzcQB+AAoAc3EAfgAKAXNxAH4ACgBzcQB+AAoBc3EAfgAKAHNxAH4ACgFzcQB+AAoAc3EAfgAKAXNxAH4ACgBzcQB+AAoBc3EAfgAKAHNxAH4ACgFzcQB+AAoAc3EAfgAKAXNxAH4ACgBzcQB+AAoBc3EAfgAKAHNxAH4ACgFzcQB+AAoAcHBwcHBwcHBwcHBwcHBwcHBweA==" resource="IsExpPolyType.vdmpp" value="[true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false]"/>
</result>
</testResult>
Expand Up @@ -23,8 +23,13 @@ public class Entry {
fun(42L, Utils.CHAR),
fun(new Token(SeqUtil.seq('x')), Utils.TOKEN),
fun(1L, Utils.TOKEN),
fun(quotes.AQuote.getInstance(), quotes.AQuote.getInstance()),
fun(quotes.AQuote.getInstance(), quotes.BQuote.getInstance()),
fun2(2.5, Utils.BOOL, Utils.REAL),
fun2(2.5, Utils.BOOL, Utils.NAT));
fun2(2.5, Utils.BOOL, Utils.NAT),
fun2(quotes.BQuote.getInstance(), quotes.AQuote.getInstance(), quotes.BQuote.getInstance()),
fun2(
quotes.AQuote.getInstance(), quotes.BQuote.getInstance(), quotes.CQuote.getInstance()));
}

public Entry() {}
Expand All @@ -46,3 +51,6 @@ public class Entry {
}

##########
*Quotes*
A,B,C
##########
Expand Up @@ -15,13 +15,15 @@
import org.overture.codegen.ir.expressions.AExplicitVarExpIR;
import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
import org.overture.codegen.ir.expressions.AMethodInstantiationExpIR;
import org.overture.codegen.ir.expressions.AQuoteLiteralExpIR;
import org.overture.codegen.ir.types.ABoolBasicTypeIR;
import org.overture.codegen.ir.types.ACharBasicTypeIR;
import org.overture.codegen.ir.types.AClassTypeIR;
import org.overture.codegen.ir.types.AExternalTypeIR;
import org.overture.codegen.ir.types.AIntNumericBasicTypeIR;
import org.overture.codegen.ir.types.ANat1NumericBasicTypeIR;
import org.overture.codegen.ir.types.ANatNumericBasicTypeIR;
import org.overture.codegen.ir.types.AQuoteTypeIR;
import org.overture.codegen.ir.types.ARatNumericBasicTypeIR;
import org.overture.codegen.ir.types.ARealNumericBasicTypeIR;
import org.overture.codegen.ir.types.ATemplateTypeIR;
Expand Down Expand Up @@ -128,59 +130,68 @@ else if(func instanceof AIdentifierVarExpIR)

for(STypeIR type : methodInst.getActualTypes())
{
AExternalTypeIR runtimeUtilClass = new AExternalTypeIR();
runtimeUtilClass.setName(getUtilClass());

AExternalTypeIR anyType = new AExternalTypeIR();
anyType.setName(getTypeArgumentFieldName());

AExplicitVarExpIR typeArg = new AExplicitVarExpIR();
typeArg.setClassType(runtimeUtilClass);
typeArg.setIsLocal(false);
typeArg.setIsLambda(false);
typeArg.setType(anyType);

String name;
if(type instanceof ANatNumericBasicTypeIR)
{
name = NAT;
}
else if(type instanceof ANat1NumericBasicTypeIR)
{
name = NAT1;
}
else if(type instanceof AIntNumericBasicTypeIR)
{
name = INT;
}
else if(type instanceof ARealNumericBasicTypeIR)
{
name = REAL;
}
else if(type instanceof ARatNumericBasicTypeIR)
{
name = RAT;
}
else if(type instanceof ABoolBasicTypeIR)
{
name = BOOL;
}
else if(type instanceof ACharBasicTypeIR)
if(type instanceof AQuoteTypeIR)
{
name = CHAR;
}
else if(type instanceof ATokenBasicTypeIR)
{
name = TOKEN;
AQuoteLiteralExpIR qt = new AQuoteLiteralExpIR();
qt.setValue(((AQuoteTypeIR) type).getValue());
node.getArgs().add(qt);
}
else
{
assist.getInfo().addTransformationWarning(methodInst, "Function instantiation only works for basic types");
name = getUnsupportedTypeFieldName();
}
typeArg.setName(name);
AExternalTypeIR runtimeUtilClass = new AExternalTypeIR();
runtimeUtilClass.setName(getUtilClass());

AExternalTypeIR anyType = new AExternalTypeIR();
anyType.setName(getTypeArgumentFieldName());

AExplicitVarExpIR typeArg = new AExplicitVarExpIR();
typeArg.setClassType(runtimeUtilClass);
typeArg.setIsLocal(false);
typeArg.setIsLambda(false);
typeArg.setType(anyType);

node.getArgs().add(typeArg);
String name;
if(type instanceof ANatNumericBasicTypeIR)
{
name = NAT;
}
else if(type instanceof ANat1NumericBasicTypeIR)
{
name = NAT1;
}
else if(type instanceof AIntNumericBasicTypeIR)
{
name = INT;
}
else if(type instanceof ARealNumericBasicTypeIR)
{
name = REAL;
}
else if(type instanceof ARatNumericBasicTypeIR)
{
name = RAT;
}
else if(type instanceof ABoolBasicTypeIR)
{
name = BOOL;
}
else if(type instanceof ACharBasicTypeIR)
{
name = CHAR;
}
else if(type instanceof ATokenBasicTypeIR)
{
name = TOKEN;
}
else
{
assist.getInfo().addTransformationWarning(methodInst, "Function instantiation only works for basic types, quotes and unions of these types");
name = getUnsupportedTypeFieldName();
}
typeArg.setName(name);

node.getArgs().add(typeArg);
}
}
}
}
Expand Down

0 comments on commit 3ddca59

Please sign in to comment.