1 package org.simantics.scl.compiler.internal.parsing.types;
\r
3 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
\r
4 import org.simantics.scl.compiler.internal.types.TypeElaborationContext;
\r
5 import org.simantics.scl.compiler.types.Type;
\r
6 import org.simantics.scl.compiler.types.Types;
\r
7 import org.simantics.scl.compiler.types.kinds.Kind;
\r
8 import org.simantics.scl.compiler.types.kinds.Kinds;
\r
10 import gnu.trove.map.hash.TObjectIntHashMap;
\r
11 import gnu.trove.set.hash.TIntHashSet;
\r
15 public class TFunctionAst extends TypeAst {
\r
16 public final TypeAst domain;
\r
17 public final TypeAst range;
\r
19 public TFunctionAst(TypeAst domain, TypeAst range) {
\r
20 this.domain = domain;
\r
25 public void toString(StringBuilder b) {
\r
26 domain.toString(b, 2);
\r
28 range.toString(b, 3);
\r
32 public Type toType(TypeTranslationContext context, Kind expectedKind) {
\r
33 context.unify(location, Kinds.STAR, expectedKind);
\r
34 if(range instanceof TEffectAst) {
\r
35 TEffectAst effectAst = (TEffectAst)range;
\r
36 return Types.functionE(
\r
37 domain.toType(context, Kinds.STAR),
\r
38 toEffect(context, effectAst.effects),
\r
39 effectAst.type.toType(context, Kinds.STAR));
\r
42 return Types.function(domain.toType(context, Kinds.STAR), range.toType(context, Kinds.STAR));
\r
46 public Type toType(TypeElaborationContext context) {
\r
47 if(range instanceof TEffectAst) {
\r
48 TEffectAst effectAst = (TEffectAst)range;
\r
49 return Types.functionE(
\r
50 domain.toType(context),
\r
51 toEffect(context, effectAst.effects),
\r
52 effectAst.type.toType(context));
\r
55 return Types.function(domain.toType(context), range.toType(context));
\r
58 static Type toEffect(TypeTranslationContext context, TypeAst[] effects) {
\r
59 if(effects.length == 0)
\r
60 return Types.NO_EFFECTS;
\r
61 if(effects.length == 1)
\r
62 return effects[0].toEffect(context);
\r
63 Type[] types = new Type[effects.length];
\r
64 for(int i=0;i<effects.length;++i)
\r
65 types[i] = effects[i].toEffect(context);
\r
66 return Types.union(types);
\r
69 static Type toEffect(TypeElaborationContext context, TypeAst[] effects) {
\r
70 if(effects.length == 0)
\r
71 return Types.NO_EFFECTS;
\r
72 if(effects.length == 1)
\r
73 return effects[0].toEffect(context);
\r
74 Type[] types = new Type[effects.length];
\r
75 for(int i=0;i<effects.length;++i)
\r
76 types[i] = effects[i].toEffect(context);
\r
77 return Types.union(types);
\r
81 public int getPrecedence() {
\r
86 public void collectReferences(TObjectIntHashMap<String> typeNameMap,
\r
88 domain.collectReferences(typeNameMap, set);
\r
89 range.collectReferences(typeNameMap, set);
\r