]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/types/TApplyAst.java
Merge "Remove unused import in DeleteHandler"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / parsing / types / TApplyAst.java
1 package org.simantics.scl.compiler.internal.parsing.types;
2
3 import java.util.Arrays;
4
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;
15
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.TIntHashSet;
18
19
20 public class TApplyAst extends TypeAst {
21     public final TypeAst function;
22     public final TypeAst[] parameters;
23     
24     public TApplyAst(TypeAst function, TypeAst[] parameters) {
25         this.function = function;
26         this.parameters = parameters;
27     }
28
29     @Override
30     public void toString(StringBuilder b) {
31         function.toString(b, 2);
32         for(TypeAst parameter : parameters) {
33             b.append(' ');
34             parameter.toString(b, 1);
35         }
36     }
37
38     @Override
39     public Type toType(TypeTranslationContext context, Kind expectedKind) {
40         if(function instanceof TVarAst) {
41             String name = ((TVarAst)function).name;
42             TypeAlias alias = null;
43             try {
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);
50             }
51             if(alias != null) {
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());
56                 }
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);
61             }
62         }
63         
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);
70         }        
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]);
75         }
76         return Types.apply(functionType, parameterTypes);
77     }
78
79     @Override
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);
86     }
87     
88     @Override
89     public int getPrecedence() {
90         return 1;
91     }
92
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);
100         }
101         else
102             return new TApplyAst(f, new TypeAst[] {p});
103     }
104
105     @Override
106     public void collectReferences(TObjectIntHashMap<String> typeNameMap,
107             TIntHashSet set) {
108         function.collectReferences(typeNameMap, set);
109         for(TypeAst parameter : parameters)
110             parameter.collectReferences(typeNameMap, set);
111     }
112
113 }