aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/ast/visitors/DynamicNodeVisitor.java
blob: 0985a294e860f810ec7b31608ea72b45eee699e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package org.zwobble.couscous.ast.visitors;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.modifier.FieldManifestation;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.Duplication;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.Throw;
import net.bytebuddy.implementation.bytecode.TypeCreation;
import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
import net.bytebuddy.implementation.bytecode.member.FieldAccess;
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
import net.bytebuddy.implementation.bytecode.member.MethodReturn;
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
import net.bytebuddy.matcher.ElementMatchers;
import org.zwobble.couscous.ast.Node;
import org.zwobble.couscous.ast.NodeTypes;
import org.zwobble.couscous.util.ExtraIterables;
import org.zwobble.couscous.util.ExtraSets;
import org.zwobble.couscous.util.asm.Implementations;
import org.zwobble.couscous.util.asm.StackManipulationSwitch;
import org.zwobble.couscous.util.asm.TypeDescriptions;

import java.lang.reflect.Method;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import static java.util.Arrays.asList;
import static org.zwobble.couscous.util.ExtraIterables.lazyMap;
import static org.zwobble.couscous.util.ExtraLists.eagerFilter;
import static org.zwobble.couscous.util.ExtraLists.eagerMap;
import static org.zwobble.couscous.util.ExtraMaps.entry;

/**
 * Dynamically generate a method to dispatch methods based on node type.
 *
 * <p>For instance, let's say you've defined two methods in a class called {@code NodeWriter}:</p>
 *
 * <blockquote><pre>
 *     void visit(LiteralNode);
 *     void visit(VariableReferenceNode);
 * </pre></blockquote>
 *
 * Calling:
 *
 * <blockquote><pre>
 *     DynamicNodeVisitor.instantiate(nodeWriter, "visit")
 * </pre></blockquote>
 *
 * will generate a function accepting a node equivalent to:
 *
 * <blockquote><pre>
 *     switch (node.nodeType()) {
 *         case NodeTypes.LITERAL:
 *             nodeWriter.visit((LiteralNode) node);
 *             return;
 *         case NodeTypes.VARIABLE_REFERENCE:
 *             nodeWriter.visit((VariableReferenceNode) node);
 *             return;
 *         default;
 *             return;
 *     }
 * </pre></blockquote>
 *
 * <p>
 *   {@code visit} methods are found by performing reflection on the run-time type
 *   of the value passed to {@code instantiate()}. To use reflection on a super-type,
 *   or to avoid repeating the cost of looking up the generated code for that type,
 *   use {@code visitor()} instead of {@code instantiate()}.
 * </p>
 *
 * <p>
 *   In answer to "why not define a visitor interface instead?":
 *   this allows visitors to be defined over a subset of nodes,
 *   rather than requiring a visit method to be defined for every node.
 *   Further, it allows extension of the Node interface,
 *   making it easier to mix and match transformations that may add or remove particular node types
 *   (which affects which node types later stages are required to handle).
 * </p>
 */
public class DynamicNodeVisitor {
    private static final Map<Map.Entry<Class<?>, String>, Function<?, Consumer>> VISITOR_BUILDERS
        = new HashMap<>();

    public static <T> Consumer<Node> instantiate(T visitor, String methodName) {
        Map.Entry<Class<?>, String> key = entry(visitor.getClass(), methodName);
        Function builder = VISITOR_BUILDERS.get(key);
        if (builder == null) {
            builder = buildClassSupplier(visitor.getClass(), Consumer.class, methodName, MethodReturn.VOID);
            VISITOR_BUILDERS.put(key, builder);
        }
        return (Consumer<Node>) builder.apply(visitor);
    }

    public static <T> BiConsumer<Node, T> visitor(Class<T> clazz) {
        Set<String> methodNames = ExtraSets.copyOf(lazyMap(asList(clazz.getDeclaredMethods()), Method::getName));
        if (methodNames.size() != 1) {
            throw new UnsupportedOperationException("Multiple method names");
        }
        String methodName = methodNames.iterator().next();
        Function<T, Consumer> classSupplier = buildClassSupplier(clazz, Consumer.class, methodName, MethodReturn.VOID);
        return (node, visitor) -> classSupplier.apply(visitor).accept(node);
    }

    static <T, F> Function<T, F> buildClassSupplier(Class<T> clazz, Class<F> function, String methodName, MethodReturn methodReturn) {
        Class<? extends F> visitorClass = buildClass(clazz, function, methodName, methodReturn);

        try {
            return new ByteBuddy()
                .subclass(Function.class)

                .method(ElementMatchers.isAbstract())
                .intercept(Implementations.stackManipulation(target -> new StackManipulation.Compound(
                    TypeCreation.of(new TypeDescription.ForLoadedType(visitorClass)),
                    Duplication.SINGLE,
                    MethodVariableAccess.REFERENCE.loadOffset(1),
                    TypeCasting.to(new TypeDescription.ForLoadedType(clazz)),
                    MethodInvocation.invoke(TypeDescriptions.findConstructor(visitorClass, clazz)),
                    MethodReturn.REFERENCE
                )))

                .make()
                .load(DynamicNodeVisitor.class.getClassLoader())
                .getLoaded()
                .newInstance();
        } catch (InstantiationException | IllegalAccessException exception) {
            throw new RuntimeException(exception);
        }
    }

    private static <T, F> Class<? extends F> buildClass(Class<T> clazz, Class<F> function, String methodName, MethodReturn methodReturn) {
        List<Method> visitMethods = eagerFilter(
            asList(clazz.getMethods()),
            method -> isVisitMethod(methodName, method)
        );

        Optional<Method> defaultMethod = ExtraIterables.find(
            asList(clazz.getMethods()),
            method ->
                method.getName().equals(methodName) &&
                method.getParameterCount() == 1 &&
                method.getParameterTypes()[0].equals(Node.class)
        );

        return new ByteBuddy()
            .subclass(function)

            .defineField("visitor", clazz, Visibility.PRIVATE, FieldManifestation.FINAL)

            .defineConstructor(Visibility.PUBLIC)
            .withParameters(clazz)
            .intercept(Implementations.stackManipulation(target -> new StackManipulation.Compound(
                // this()
                MethodVariableAccess.REFERENCE.loadOffset(0),
                MethodInvocation.invoke(TypeDescriptions.findConstructor(Object.class)),

                // this.visitor = visitor
                MethodVariableAccess.REFERENCE.loadOffset(0),
                MethodVariableAccess.REFERENCE.loadOffset(1),
                FieldAccess.forField(TypeDescriptions.findField(target, "visitor")).putter(),

                // return
                MethodReturn.VOID
            )))

            .method(ElementMatchers.isAbstract())
            .intercept(Implementations.stackManipulation(target -> {
                MethodDescription typeMethod = TypeDescriptions.findMethod(Node.class, "nodeType");
                return new StackManipulation.Compound(
                    MethodVariableAccess.REFERENCE.loadOffset(1),
                    MethodInvocation.invoke(typeMethod),
                    new StackManipulationSwitch(
                        defaultMethod
                            .map(defaultMethodValue ->
                                delegateToMethod(target, defaultMethodValue, Node.class, methodReturn))
                            .orElseGet(() -> new StackManipulation.Compound(
                                TypeCreation.of(new TypeDescription.ForLoadedType(UnsupportedOperationException.class)),
                                Duplication.SINGLE,
                                MethodInvocation.invoke(TypeDescriptions.findConstructor(UnsupportedOperationException.class)),
                                Throw.INSTANCE
                            )),
                        eagerMap(visitMethods, method -> {
                            Class<?> nodeClass = method.getParameterTypes()[0];
                            return StackManipulationSwitch.switchCase(
                                NodeTypes.forClass(nodeClass),
                                delegateToMethod(target, method, nodeClass, methodReturn)
                            );
                        })
                    )
                );
            }))
            .make()
            .load(DynamicNodeVisitor.class.getClassLoader())
            .getLoaded();
    }

    private static StackManipulation.Compound delegateToMethod(Implementation.Target target, Method method, Class<?> nodeClass, MethodReturn methodReturn) {
        return new StackManipulation.Compound(
            MethodVariableAccess.REFERENCE.loadOffset(0),
            FieldAccess.forField(TypeDescriptions.findField(target, "visitor")).getter(),
            MethodVariableAccess.REFERENCE.loadOffset(1),
            TypeCasting.to(new TypeDescription.ForLoadedType(nodeClass)),
            MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(method)),
            methodReturn
        );
    }

    private static boolean isVisitMethod(String methodName, Method method) {
        return
            method.getName().equals(methodName) &&
            method.getParameterCount() == 1 &&
            Node.class.isAssignableFrom(method.getParameterTypes()[0]) &&
            !method.getParameterTypes()[0].equals(Node.class);
    }
}