]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/types/TForAllAst.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / parsing / types / TForAllAst.java
1 package org.simantics.scl.compiler.internal.parsing.types;
2
3 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
4 import org.simantics.scl.compiler.internal.types.TypeElaborationContext;
5 import org.simantics.scl.compiler.types.TVar;
6 import org.simantics.scl.compiler.types.Type;
7 import org.simantics.scl.compiler.types.Types;
8 import org.simantics.scl.compiler.types.kinds.Kind;
9 import org.simantics.scl.compiler.types.kinds.Kinds;
10
11 import gnu.trove.map.hash.TObjectIntHashMap;
12 import gnu.trove.set.hash.TIntHashSet;
13
14
15
16 public class TForAllAst extends TypeAst {
17     public final String[] vars;
18     public final TypeAst type;
19     
20     public TForAllAst(String[] vars, TypeAst type) {
21         this.vars = vars;
22         this.type = type;
23     }
24     
25     @Override
26     public void toString(StringBuilder b) {
27         b.append("forall");
28         for(String var : vars) {
29             b.append(' ');
30             b.append(var);
31         }
32         b.append(". ");
33         type.toString(b);
34     }
35
36     @Override
37     public Type toType(TypeTranslationContext context, Kind expectedKind) {
38         context.unify(location, Kinds.STAR, expectedKind);
39         TVar[] oldVars = new TVar[vars.length];
40         for(int i=0;i<vars.length;++i)
41             oldVars[i] = context.pushTypeVar(vars[i]);
42         Type result = type.toType(context, Kinds.STAR);
43         for(int i=vars.length-1;i>=0;--i)
44             result = Types.forAll(context.popTypeVar(vars[i], oldVars[i]), result);
45         return result;
46     }
47     
48     @Override
49     public Type toType(TypeElaborationContext context) {
50         TVar[] oldVars = new TVar[vars.length];
51         for(int i=0;i<vars.length;++i)
52             oldVars[i] = context.push(vars[i]);
53         Type result = type.toType(context);
54         for(int i=vars.length-1;i>=0;--i)
55             result = Types.forAll(context.pop(vars[i], oldVars[i]), result);
56         return result;
57     }
58     
59     @Override
60     public int getPrecedence() {
61         return 2;
62     }
63
64     @Override
65     public void collectReferences(TObjectIntHashMap<String> typeNameMap,
66             TIntHashSet set) {
67         type.collectReferences(typeNameMap, set);
68     }
69 }