|
| 1 | +package de.peeeq.wurstscript.translation.imtojass; |
| 2 | + |
| 3 | +import de.peeeq.wurstscript.jassIm.ImFunction; |
| 4 | +import de.peeeq.wurstscript.jassIm.ImMethod; |
| 5 | +import de.peeeq.wurstscript.jassIm.ImType; |
| 6 | +import de.peeeq.wurstscript.jassIm.ImTypeArgument; |
| 7 | +import de.peeeq.wurstscript.jassIm.ImTypeClassFunc; |
| 8 | +import de.peeeq.wurstscript.jassIm.ImTypeVar; |
| 9 | +import de.peeeq.wurstscript.jassIm.ImTypeVarRef; |
| 10 | +import de.peeeq.wurstscript.jassIm.JassIm; |
| 11 | +import io.vavr.control.Either; |
| 12 | +import org.eclipse.jdt.annotation.Nullable; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.LinkedHashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * Replaces type variables by the type arguments bound to them. |
| 22 | + * <p> |
| 23 | + * Substitution used to be expressed as two parallel lists -- the type variables of a function or |
| 24 | + * class next to the type arguments given at a use of it -- paired up positionally at each point of |
| 25 | + * use. That form lost information: a {@link ImTypeArgument} is a type <i>plus</i> what is known |
| 26 | + * about it, but pairing the lists unwrapped the argument and returned the bare {@link ImType}, so |
| 27 | + * the type class binding never survived. Callers which needed it had to re-attach it by hand, and |
| 28 | + * the ones which did not know to do so silently produced an argument with no binding. |
| 29 | + * <p> |
| 30 | + * Keeping the argument whole fixes that: substituting into an argument position yields the argument |
| 31 | + * that was bound, so its binding travels with the type it belongs to. |
| 32 | + * <p> |
| 33 | + * Lookup is by identity. IM nodes do not override {@code equals}, and a type variable stands for one |
| 34 | + * particular declaration, so two variables which merely share a name are different variables. |
| 35 | + */ |
| 36 | +public final class TypeSubst { |
| 37 | + |
| 38 | + private static final TypeSubst EMPTY = |
| 39 | + new TypeSubst(Collections.emptyMap(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); |
| 40 | + |
| 41 | + private final Map<ImTypeVar, ImTypeArgument> bindings; |
| 42 | + /** Variables this substitution covers but for which no argument was supplied. */ |
| 43 | + private final List<ImTypeVar> unbound; |
| 44 | + /** Kept so that a missing argument is reported the same way it was before. */ |
| 45 | + private final List<ImTypeVar> typeVars; |
| 46 | + private final List<ImTypeArgument> typeArguments; |
| 47 | + |
| 48 | + private TypeSubst(Map<ImTypeVar, ImTypeArgument> bindings, List<ImTypeVar> unbound, |
| 49 | + List<ImTypeVar> typeVars, List<ImTypeArgument> typeArguments) { |
| 50 | + this.bindings = bindings; |
| 51 | + this.unbound = unbound; |
| 52 | + this.typeVars = typeVars; |
| 53 | + this.typeArguments = typeArguments; |
| 54 | + } |
| 55 | + |
| 56 | + public static TypeSubst empty() { |
| 57 | + return EMPTY; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Binds {@code typeVars} to {@code typeArguments} by position. |
| 62 | + * <p> |
| 63 | + * Fewer arguments than variables is allowed: the surplus variables stay unbound, and only a type |
| 64 | + * which actually mentions one of them fails. Callers rely on that, because a use may legitimately |
| 65 | + * leave the arguments off when it does not name the variable. |
| 66 | + */ |
| 67 | + public static TypeSubst of(List<ImTypeVar> typeVars, List<ImTypeArgument> typeArguments) { |
| 68 | + if (typeVars.isEmpty()) { |
| 69 | + return EMPTY; |
| 70 | + } |
| 71 | + // LinkedHashMap rather than IdentityHashMap: the keys already compare by identity, and this |
| 72 | + // keeps iteration order stable, which matters wherever substitution feeds emitted names. |
| 73 | + Map<ImTypeVar, ImTypeArgument> bindings = new LinkedHashMap<>(); |
| 74 | + List<ImTypeVar> unbound = new ArrayList<>(); |
| 75 | + for (int i = 0; i < typeVars.size(); i++) { |
| 76 | + ImTypeVar typeVar = typeVars.get(i); |
| 77 | + if (i < typeArguments.size()) { |
| 78 | + // A variable repeated in the list keeps its first argument, as positional lookup did. |
| 79 | + bindings.putIfAbsent(typeVar, typeArguments.get(i)); |
| 80 | + } else if (!bindings.containsKey(typeVar)) { |
| 81 | + unbound.add(typeVar); |
| 82 | + } |
| 83 | + } |
| 84 | + return new TypeSubst(bindings, unbound, typeVars, typeArguments); |
| 85 | + } |
| 86 | + |
| 87 | + public boolean isEmpty() { |
| 88 | + return bindings.isEmpty() && unbound.isEmpty(); |
| 89 | + } |
| 90 | + |
| 91 | + /** Applies this substitution to a type. Any binding on the replacing argument is not part of a type. */ |
| 92 | + public ImType apply(ImType type) { |
| 93 | + if (isEmpty()) { |
| 94 | + // Nothing to replace, so hand back the type itself. Worth doing: this runs for the |
| 95 | + // return type of every call, and rebuilding a class or tuple type only to get an equal |
| 96 | + // one back was pure allocation. |
| 97 | + return type; |
| 98 | + } |
| 99 | + return type.match(new TypeRewriteMatcher() { |
| 100 | + @Override |
| 101 | + public ImType case_ImTypeVarRef(ImTypeVarRef t) { |
| 102 | + ImTypeArgument replacement = resolve(t); |
| 103 | + return replacement == null ? t : replacement.getType(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + protected ImTypeArgument rewriteTypeArgument(ImTypeArgument argument) { |
| 108 | + return TypeSubst.this.apply(argument); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Applies this substitution to a type argument. |
| 115 | + * <p> |
| 116 | + * When the argument is exactly a type variable this substitution binds, the bound argument |
| 117 | + * replaces it whole, so the binding recorded at the use site reaches the body being substituted |
| 118 | + * into. A binding already present on the argument is more specific and is kept. |
| 119 | + */ |
| 120 | + public ImTypeArgument apply(ImTypeArgument argument) { |
| 121 | + if (isEmpty()) { |
| 122 | + return argument; |
| 123 | + } |
| 124 | + if (argument.getType() instanceof ImTypeVarRef ref) { |
| 125 | + ImTypeArgument replacement = resolve(ref); |
| 126 | + if (replacement != null) { |
| 127 | + return JassIm.ImTypeArgument(replacement.getType(), binding(argument, replacement)); |
| 128 | + } |
| 129 | + } |
| 130 | + return JassIm.ImTypeArgument(apply(argument.getType()), argument.getTypeClassBinding()); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Shares the map rather than copying it. A binding is only ever replaced wholesale through |
| 135 | + * {@code setTypeClassBinding}, never added to in place, so two arguments naming the same |
| 136 | + * instances can name the same map. |
| 137 | + */ |
| 138 | + private static Map<ImTypeClassFunc, Either<ImMethod, ImFunction>> binding(ImTypeArgument argument, |
| 139 | + ImTypeArgument replacement) { |
| 140 | + return argument.getTypeClassBinding().isEmpty() |
| 141 | + ? replacement.getTypeClassBinding() |
| 142 | + : argument.getTypeClassBinding(); |
| 143 | + } |
| 144 | + |
| 145 | + private @Nullable ImTypeArgument resolve(ImTypeVarRef ref) { |
| 146 | + ImTypeVar typeVar = ref.getTypeVariable(); |
| 147 | + ImTypeArgument bound = bindings.get(typeVar); |
| 148 | + if (bound != null) { |
| 149 | + return bound; |
| 150 | + } |
| 151 | + if (unbound.contains(typeVar)) { |
| 152 | + throw new RuntimeException("Could not find replacement for " + ref |
| 153 | + + " when replacing " + typeVars + " with " + typeArguments); |
| 154 | + } |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public String toString() { |
| 160 | + StringBuilder sb = new StringBuilder("["); |
| 161 | + bindings.forEach((typeVar, argument) -> { |
| 162 | + if (sb.length() > 1) { |
| 163 | + sb.append(", "); |
| 164 | + } |
| 165 | + sb.append(typeVar.getName()).append(" -> ").append(argument.getType()); |
| 166 | + }); |
| 167 | + return sb.append("]").toString(); |
| 168 | + } |
| 169 | +} |
0 commit comments