1 package org.simantics.scl.compiler.internal.parsing.types;
3 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
4 import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
5 import org.simantics.scl.compiler.environment.AmbiguousNameException;
6 import org.simantics.scl.compiler.environment.Environments;
7 import org.simantics.scl.compiler.internal.types.TypeElaborationContext;
8 import org.simantics.scl.compiler.types.TCon;
9 import org.simantics.scl.compiler.types.Type;
10 import org.simantics.scl.compiler.types.Types;
11 import org.simantics.scl.compiler.types.kinds.Kind;
12 import org.simantics.scl.compiler.types.kinds.Kinds;
14 import gnu.trove.map.hash.TObjectIntHashMap;
15 import gnu.trove.set.hash.TIntHashSet;
19 * This class represents an abstract syntax tree node for a type variable.
20 * The only property of a type variable is its name.
22 public class TVarAst extends TypeAst {
23 public final String name;
25 public TVarAst(String name) {
30 public void toString(StringBuilder b) {
35 public Type toType(TypeTranslationContext context, Kind expectedKind) {
36 char c = name.charAt(0);
40 for(int i=1;i<name.length()-1;++i)
41 if(name.charAt(i) != ',') {
43 con = Environments.getTypeConstructorName(context.getEnvironment(), name.substring(1, name.length()-1));
44 } catch(AmbiguousNameException e) {
45 context.getErrorLog().log(location, e.getMessage());
46 return Types.metaVar(Kinds.STAR);
49 context.getErrorLog().log(location, "Didn't find type constructor " + name + ".");
50 return Types.metaVar(Kinds.STAR);
54 con = Types.con(Types.BUILTIN, name);
56 else if(Character.isLowerCase(c))
57 return context.resolveTypeVariable(location, name, expectedKind);
61 alias = Environments.getTypeAlias(context.getEnvironment(), name);
62 } catch (AmbiguousNameException e1) {
63 context.getErrorLog().log(location, e1.getMessage());
64 return Types.metaVar(Kinds.STAR);
67 if(alias.getArity() > 0) {
68 context.getErrorLog().log(location, "The alias expects " +
69 alias.getArity() + " parameters, but none are given.");
70 return Types.metaVar(Kinds.metaVar());
75 con = Environments.getTypeConstructorName(context.getEnvironment(), name);
76 } catch(AmbiguousNameException e) {
77 context.getErrorLog().log(location, e.getMessage());
78 return Types.metaVar(Kinds.STAR);
81 context.getErrorLog().log(location, "Didn't find type constructor " + name + ".");
82 return Types.metaVar(Kinds.STAR);
87 Kind providedKind = context.getKind(con);
88 context.unify(location, providedKind, expectedKind);
93 public Type toType(TypeElaborationContext context) {
94 char c = name.charAt(0);
98 for(int i=1;i<name.length()-1;++i)
99 if(name.charAt(i) != ',') {
100 con = context.resolveTypeConstructor(name.substring(1, name.length()-1));
102 System.err.println("Didn't find type constructor " + name + ".");
103 return Types.metaVar(Kinds.STAR);
107 con = Types.con(Types.BUILTIN, name);
109 else if(Character.isLowerCase(c))
110 return context.resolveTypeVariable(name);
112 con = context.resolveTypeConstructor(name);
114 System.err.println("Didn't find type constructor " + name + ".");
115 return Types.metaVar(Kinds.STAR);
124 * Translate this variable reference to an effect Type. Lower initial names are treated as
125 * type meta-variables. Capitalized names are resolved as effect name constants.
127 * Error messages about unresolved or ambiguous effect names are logged, with a new meta-variable
128 * as the return value.
131 public Type toEffect(TypeTranslationContext context) {
132 char c = name.charAt(0);
133 if(Character.isLowerCase(c))
134 return context.resolveTypeVariable(location, name, Kinds.EFFECT);
138 con = Environments.getEffectConstructorName(context.getEnvironment(), name);
139 } catch (AmbiguousNameException e) {
140 context.getErrorLog().log(location, e.getMessage());
141 return Types.metaVar(Kinds.EFFECT);
144 context.getErrorLog().log(location, "Didn't find effect constructor " + name + ".");
145 return Types.metaVar(Kinds.EFFECT);
152 public Type toEffect(TypeElaborationContext context) {
153 char c = name.charAt(0);
154 if(Character.isLowerCase(c))
155 return context.resolveTypeVariable(name);
157 Type con = context.resolveTypeConstructor(name);
159 System.err.println("Didn't find effect constructor " + name + ".");
160 return Types.metaVar(Kinds.EFFECT);
167 public int getPrecedence() {
172 public void collectReferences(TObjectIntHashMap<String> typeNameMap,
174 if(typeNameMap.containsKey(name))
175 set.add(typeNameMap.get(name));