X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ftypes%2FTypes.java;h=5512f5e6e10fedf946b6d10dc421e48490256c93;hp=a706e2b8f17d3d9415b30c3ccd7eed14ea01b1f9;hb=6fd9bc1ec7e95848d0cc15d12825a65a4b57ada5;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java index a706e2b8f..5512f5e6e 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java @@ -84,21 +84,23 @@ public class Types { public static final TCon TYPEABLE = con(BUILTIN, "Typeable"); public static final TCon SERIALIZABLE = con(BUILTIN, "Serializable"); public static final TCon VEC_COMP = con(BUILTIN, "VecComp"); + public static final TCon CLASS = con(BUILTIN, "Class"); public static final TCon BINDING = con(BUILTIN, "Binding"); + public static final TCon TYPE = con(BUILTIN, "Type"); + public static final TCon DYNAMIC = con("Prelude", "Dynamic"); public static final TCon VARIANT = con(BUILTIN, "Variant"); public static final TCon ADDITIVE = con("Prelude", "Additive"); public static final TCon MONAD = con("Prelude", "Monad"); + public static final TCon MONAD_E = con("Prelude", "MonadE"); public static final TCon INTEGRAL = con("Prelude", "Integral"); public static final TCon RING = con("Prelude", "Ring"); public static final TCon ORDERED_RING = con("Prelude", "OrderedRing"); public static final TCon REAL = con("Prelude", "Real"); public static final TCon SHOW = con("Prelude", "Show"); - public static final TCon EQ = con("Prelude", "Eq"); public static final TCon ORD = con("Prelude", "Ord"); - public static final TCon HASHABLE = con("Prelude", "Hashable"); public static final TCon IO = con("Serialization", "IO"); public static final Type REF = con("Prelude", "Ref"); @@ -110,8 +112,12 @@ public class Types { public static final TUnion NO_EFFECTS = new TUnion(); public static final TCon PROC = con(BUILTIN, "Proc"); + public static final TCon EXCEPTION = con(BUILTIN, "Exception"); public static final TCon BRANCH_POINT = con(BUILTIN, "BranchPoint"); + + public static final TCon CHRContext = con(BUILTIN, "CHRContext"); + private volatile static TCon[] tupleCache = new TCon[] { UNIT, null @@ -128,7 +134,7 @@ public class Types { } }; - + public static boolean isPrimitive(Type type) { return type == BOOLEAN || type == BYTE || type == CHARACTER || type == SHORT || type == INTEGER || type == LONG || type == FLOAT || type == DOUBLE || type == STRING; @@ -147,36 +153,18 @@ public class Types { function = apply(function, parameter); return function; } - - /** - * Get the concrete type or alias type pointed to by a chain of type meta-variables, - * or the last metavariable in the link, if it is not linked to an actual type. - * Unlike {@link #canonical(Type)}, this method does not resolve type aliases. - */ - public static Type weakCanonical(Type type) { - while(true) { - if(type instanceof TMetaVar) { - TMetaVar metaVar = (TMetaVar)type; - if(metaVar.ref == null) - return type; - else - type = metaVar.ref; - } - else - return type; - } - } /** - * Get the concrete type pointed to by a chain of type meta-variables. Unlike {@link #weakCanonical(Type)} - * this method also resolves type aliases. + * Get the concrete type pointed to by a chain of type meta-variables. */ public static Type canonical(Type type) { - while(type instanceof TMetaVar) { + if(type instanceof TMetaVar) { TMetaVar metaVar = (TMetaVar)type; type = metaVar.ref; if(type == null) return metaVar; + else + return metaVar.ref = canonical(type); } return type; } @@ -572,7 +560,15 @@ public class Types { parameters.add(Types.canonical(apply.parameter)); type = canonical(apply.function); } - return new MultiApply(type, parameters.toArray(new Type[parameters.size()])); + Type[] parametersArray; + if(parameters.isEmpty()) + parametersArray = Type.EMPTY_ARRAY; + else { + parametersArray = new Type[parameters.size()]; + for(int i=0,j=parametersArray.length-1;i Type[] replace(Type[] types, THashMap map) { if(types.length == 0) return Type.EMPTY_ARRAY; @@ -1042,6 +1047,14 @@ public class Types { else return new TUnion(effects); } + + public static Type union(Type effect1, Type effect2) { + return new TUnion(effect1, effect2); + } + + public static Type union(Type effect1, Type effect2, Type effect3) { + return new TUnion(effect1, effect2, effect3); + } public static Type union(List effects) { if(effects.size() == 0) @@ -1112,17 +1125,9 @@ public class Types { return parseType(new TypeElaborationContext(environment), text); } - public static Type parseType(ITypeEnvironment environment, THashMap localTypeVars, String text) throws SCLTypeParseException { - return parseType(new TypeElaborationContext(localTypeVars, environment), text); - } - public static Type parseType(String text) throws SCLTypeParseException { return parseType(new TypeElaborationContext(DUMMY_TYPE_ENVIRONMENT), text); } - - public static Type parseType(THashMap localTypeVars, String text) throws SCLTypeParseException { - return parseType(new TypeElaborationContext(localTypeVars, DUMMY_TYPE_ENVIRONMENT), text); - } private static Type parseType(TypeElaborationContext context, String text) throws SCLTypeParseException { SCLParserImpl parser = new SCLParserImpl(new StringReader(text)); @@ -1137,4 +1142,30 @@ public class Types { e.getMessage())); } } + + public static Type instantiateAndStrip(Type type) { + while(true) { + if(type instanceof TForAll) { + TForAll forAll = (TForAll)type; + type = forAll.type.replace(forAll.var, metaVar(forAll.var.getKind())); + } + else if(type instanceof TFun) { + TFun fun = (TFun)type; + if(fun.domain instanceof TPred || fun.domain == Types.PUNIT) + type = fun.range; + else + return type; + } + else if(type instanceof TMetaVar) { + TMetaVar metaVar = (TMetaVar)type; + if(metaVar.ref == null) + return type; + else + type = metaVar.ref; + } + else + return type; + } + } + }