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.elaboration.modules.TypeDescriptor;
6 import org.simantics.scl.compiler.environment.AmbiguousNameException;
7 import org.simantics.scl.compiler.environment.Environments;
8 import org.simantics.scl.compiler.internal.types.TypeElaborationContext;
9 import org.simantics.scl.compiler.types.TCon;
10 import org.simantics.scl.compiler.types.Type;
11 import org.simantics.scl.compiler.types.Types;
12 import org.simantics.scl.compiler.types.kinds.Kind;
13 import org.simantics.scl.compiler.types.kinds.Kinds;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.TIntHashSet;
20 * This class represents an abstract syntax tree node for a type variable.
21 * The only property of a type variable is its name.
23 public class TVarAst extends TypeAst {
24 public final String name;
26 public TVarAst(String name) {
31 public void toString(StringBuilder b) {
36 public Type toType(TypeTranslationContext context, Kind expectedKind) {
37 char c = name.charAt(0);
41 for(int i=1;i<name.length()-1;++i)
42 if(name.charAt(i) != ',') {
44 con = Environments.getTypeDescriptorName(context.getEnvironment(), name.substring(1, name.length()-1));
45 } catch(AmbiguousNameException e) {
46 context.getErrorLog().log(location, e.getMessage());
47 return Types.metaVar(Kinds.STAR);
50 context.getErrorLog().log(location, "Didn't find type constructor " + name + ".");
51 return Types.metaVar(Kinds.STAR);
55 con = Types.con(Types.BUILTIN, name);
57 else if(Character.isLowerCase(c))
58 return context.resolveTypeVariable(location, name, expectedKind);
60 return context.resolveExistential(location, name, expectedKind);
64 tdesc = Environments.getTypeDescriptor(context.getEnvironment(), name);
65 } catch (AmbiguousNameException e) {
66 context.getErrorLog().log(location, e.getMessage());
67 return Types.metaVar(Kinds.STAR);
70 context.getErrorLog().log(location, "Didn't find type constructor " + name + ".");
71 return Types.metaVar(Kinds.STAR);
73 if(tdesc instanceof TypeAlias) {
74 TypeAlias alias = (TypeAlias)tdesc;
75 if(alias.getArity() > 0) {
76 context.getErrorLog().log(location, "The alias expects " +
77 alias.getArity() + " parameters, but none are given.");
78 return Types.metaVar(Kinds.metaVar());
86 Kind providedKind = context.getKind(con);
87 context.unify(location, providedKind, expectedKind);
92 public Type toType(TypeElaborationContext context) {
93 char c = name.charAt(0);
97 for(int i=1;i<name.length()-1;++i)
98 if(name.charAt(i) != ',') {
99 con = context.resolveTypeConstructor(name.substring(1, name.length()-1));
101 System.err.println("Didn't find type constructor " + name + ".");
102 return Types.metaVar(Kinds.STAR);
106 con = Types.con(Types.BUILTIN, name);
108 else if(Character.isLowerCase(c))
109 return context.resolveTypeVariable(name);
111 return context.resolveExistential(name);
113 con = context.resolveTypeConstructor(name);
115 System.err.println("Didn't find type constructor " + name + ".");
116 return Types.metaVar(Kinds.STAR);
125 * Translate this variable reference to an effect Type. Lower initial names are treated as
126 * type meta-variables. Capitalized names are resolved as effect name constants.
128 * Error messages about unresolved or ambiguous effect names are logged, with a new meta-variable
129 * as the return value.
132 public Type toEffect(TypeTranslationContext context) {
133 char c = name.charAt(0);
134 if(Character.isLowerCase(c))
135 return context.resolveTypeVariable(location, name, Kinds.EFFECT);
139 con = Environments.getEffectConstructorName(context.getEnvironment(), name);
140 } catch (AmbiguousNameException e) {
141 context.getErrorLog().log(location, e.getMessage());
142 return Types.metaVar(Kinds.EFFECT);
145 context.getErrorLog().log(location, "Didn't find effect constructor " + name + ".");
146 return Types.metaVar(Kinds.EFFECT);
153 public Type toEffect(TypeElaborationContext context) {
154 char c = name.charAt(0);
155 if(Character.isLowerCase(c))
156 return context.resolveTypeVariable(name);
158 return context.resolveExistential(name);
160 Type con = context.resolveTypeConstructor(name);
162 System.err.println("Didn't find effect constructor " + name + ".");
163 return Types.metaVar(Kinds.EFFECT);
170 public int getPrecedence() {
175 public void collectReferences(TObjectIntHashMap<String> typeNameMap,
177 if(typeNameMap.containsKey(name))
178 set.add(typeNameMap.get(name));