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);
62 tdesc = Environments.getTypeDescriptor(context.getEnvironment(), name);
63 } catch (AmbiguousNameException e) {
64 context.getErrorLog().log(location, e.getMessage());
65 return Types.metaVar(Kinds.STAR);
68 context.getErrorLog().log(location, "Didn't find type constructor " + name + ".");
69 return Types.metaVar(Kinds.STAR);
71 if(tdesc instanceof TypeAlias) {
72 TypeAlias alias = (TypeAlias)tdesc;
73 if(alias.getArity() > 0) {
74 context.getErrorLog().log(location, "The alias expects " +
75 alias.getArity() + " parameters, but none are given.");
76 return Types.metaVar(Kinds.metaVar());
84 Kind providedKind = context.getKind(con);
85 context.unify(location, providedKind, expectedKind);
90 public Type toType(TypeElaborationContext context) {
91 char c = name.charAt(0);
95 for(int i=1;i<name.length()-1;++i)
96 if(name.charAt(i) != ',') {
97 con = context.resolveTypeConstructor(name.substring(1, name.length()-1));
99 System.err.println("Didn't find type constructor " + name + ".");
100 return Types.metaVar(Kinds.STAR);
104 con = Types.con(Types.BUILTIN, name);
106 else if(Character.isLowerCase(c))
107 return context.resolveTypeVariable(name);
109 con = context.resolveTypeConstructor(name);
111 System.err.println("Didn't find type constructor " + name + ".");
112 return Types.metaVar(Kinds.STAR);
121 * Translate this variable reference to an effect Type. Lower initial names are treated as
122 * type meta-variables. Capitalized names are resolved as effect name constants.
124 * Error messages about unresolved or ambiguous effect names are logged, with a new meta-variable
125 * as the return value.
128 public Type toEffect(TypeTranslationContext context) {
129 char c = name.charAt(0);
130 if(Character.isLowerCase(c))
131 return context.resolveTypeVariable(location, name, Kinds.EFFECT);
135 con = Environments.getEffectConstructorName(context.getEnvironment(), name);
136 } catch (AmbiguousNameException e) {
137 context.getErrorLog().log(location, e.getMessage());
138 return Types.metaVar(Kinds.EFFECT);
141 context.getErrorLog().log(location, "Didn't find effect constructor " + name + ".");
142 return Types.metaVar(Kinds.EFFECT);
149 public Type toEffect(TypeElaborationContext context) {
150 char c = name.charAt(0);
151 if(Character.isLowerCase(c))
152 return context.resolveTypeVariable(name);
154 Type con = context.resolveTypeConstructor(name);
156 System.err.println("Didn't find effect constructor " + name + ".");
157 return Types.metaVar(Kinds.EFFECT);
164 public int getPrecedence() {
169 public void collectReferences(TObjectIntHashMap<String> typeNameMap,
171 if(typeNameMap.containsKey(name))
172 set.add(typeNameMap.get(name));