1 package org.simantics.scl.compiler.internal.parsing.types;
3 import java.util.Arrays;
5 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
6 import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
7 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
8 import org.simantics.scl.compiler.environment.AmbiguousNameException;
9 import org.simantics.scl.compiler.environment.Environments;
10 import org.simantics.scl.compiler.internal.types.TypeElaborationContext;
11 import org.simantics.scl.compiler.types.Type;
12 import org.simantics.scl.compiler.types.Types;
13 import org.simantics.scl.compiler.types.kinds.Kind;
14 import org.simantics.scl.compiler.types.kinds.Kinds;
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.TIntHashSet;
20 public class TApplyAst extends TypeAst {
21 public final TypeAst function;
22 public final TypeAst[] parameters;
24 public TApplyAst(TypeAst function, TypeAst[] parameters) {
25 this.function = function;
26 this.parameters = parameters;
30 public void toString(StringBuilder b) {
31 function.toString(b, 2);
32 for(TypeAst parameter : parameters) {
34 parameter.toString(b, 1);
39 public Type toType(TypeTranslationContext context, Kind expectedKind) {
40 if(function instanceof TVarAst) {
41 String name = ((TVarAst)function).name;
42 TypeAlias alias = null;
44 TypeDescriptor tdesc = Environments.getTypeDescriptor(context.getEnvironment(), name);
45 if(tdesc instanceof TypeAlias)
46 alias = (TypeAlias)tdesc;
47 } catch (AmbiguousNameException e) {
48 context.getErrorLog().log(location, e.getMessage());
49 return Types.metaVar(Kinds.STAR);
52 if(parameters.length != alias.getArity()) {
53 context.getErrorLog().log(location, "Wrong number of parameters are given to the type alias. Expected " +
54 alias.getArity() + " parameters, got " + parameters.length + " parameters.");
55 return Types.metaVar(Kinds.metaVar());
57 Type[] parameterTypes = new Type[parameters.length];
58 for(int i=0;i<parameters.length;++i)
59 parameterTypes[i] = parameters[i].toType(context, Kinds.metaVar());
60 return alias.body.replace(alias.parameters, parameterTypes);
64 Kind[] parameterKinds = new Kind[parameters.length];
65 Kind functionKind = expectedKind;
66 for(int i=parameters.length-1;i>=0;--i) {
67 Kind kind = Kinds.metaVar();
68 parameterKinds[i] = kind;
69 functionKind = Kinds.arrow(kind, functionKind);
71 Type functionType = function.toType(context, functionKind);
72 Type[] parameterTypes = new Type[parameters.length];
73 for(int i=0;i<parameters.length;++i) {
74 parameterTypes[i] = parameters[i].toType(context, parameterKinds[i]);
76 return Types.apply(functionType, parameterTypes);
80 public Type toType(TypeElaborationContext context) {
81 Type functionType = function.toType(context);
82 Type[] parameterTypes = new Type[parameters.length];
83 for(int i=0;i<parameters.length;++i)
84 parameterTypes[i] = parameters[i].toType(context);
85 return Types.apply(functionType, parameterTypes);
89 public int getPrecedence() {
93 public static TApplyAst apply(TypeAst f, TypeAst p) {
94 if(f instanceof TApplyAst) {
95 TApplyAst fApply = (TApplyAst)f;
96 TypeAst[] parameters = Arrays.copyOf(fApply.parameters,
97 fApply.parameters.length+1);
98 parameters[fApply.parameters.length] = p;
99 return new TApplyAst(fApply.function, parameters);
102 return new TApplyAst(f, new TypeAst[] {p});
106 public void collectReferences(TObjectIntHashMap<String> typeNameMap,
108 function.collectReferences(typeNameMap, set);
109 for(TypeAst parameter : parameters)
110 parameter.collectReferences(typeNameMap, set);