aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/ast/MethodNode.java
blob: 0f910241d55fccbc988d7372757013c6009b8036 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package org.zwobble.couscous.ast;

import com.google.common.collect.ImmutableList;
import org.zwobble.couscous.types.Type;
import org.zwobble.couscous.ast.visitors.NodeMapper;
import org.zwobble.couscous.ast.visitors.NodeTransformer;
import org.zwobble.couscous.types.Types;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static org.zwobble.couscous.util.ExtraLists.eagerMap;
import static org.zwobble.couscous.util.ExtraLists.list;

public class MethodNode implements Node {
    public static Builder staticMethod(String name) {
        return builder(name).isStatic(true);
    }
    public static Builder builder(String name) {
        return new Builder(name);
    }

    public static class Builder {
        private final ImmutableList.Builder<AnnotationNode> annotations =
            ImmutableList.builder();
        private boolean isStatic = false;
        private boolean isAbstract = false;
        private final String name;
        private final ImmutableList.Builder<FormalArgumentNode> arguments =
            ImmutableList.builder();
        private Type returnType = Types.VOID;
        private final ImmutableList.Builder<StatementNode> body =
            ImmutableList.builder();
        private final ImmutableList.Builder<MethodSignature> overrides =
            ImmutableList.builder();

        public Builder(String name) {
            this.name = name;
        }
        
        public Builder annotations(List<AnnotationNode> annotations) {
            this.annotations.addAll(annotations);
            return this;
        }
        
        public Builder isStatic(boolean isStatic) {
            this.isStatic = isStatic;
            return this;
        }

        public Builder isAbstract() {
            this.isAbstract = true;
            return this;
        }
        
        public Builder argument(FormalArgumentNode argument) {
            arguments.add(argument);
            return this;
        }
        
        public Builder arguments(List<FormalArgumentNode> arguments) {
            this.arguments.addAll(arguments);
            return this;
        }

        public Builder returns(Type returnType) {
            this.returnType = returnType;
            return this;
        }
        
        public Builder statement(StatementNode statement) {
            this.body.add(statement);
            return this;
        }
        
        public Builder body(List<StatementNode> statements) {
            this.body.addAll(statements);
            return this;
        }
        
        public MethodNode build() {
            return new MethodNode(
                annotations.build(),
                isStatic,
                name,
                list(),
                arguments.build(),
                returnType,
                isAbstract ? Optional.empty() : Optional.of(body.build()),
                overrides.build());
        }
    }
    
    public static MethodNode method(
        List<AnnotationNode> annotations,
        boolean isStatic,
        String name,
        List<FormalTypeParameterNode> typeParameters,
        List<FormalArgumentNode> arguments,
        Type returnType,
        Optional<List<StatementNode>> body,
        List<MethodSignature> overrides)
    {
        return new MethodNode(annotations, isStatic, name, typeParameters, arguments, returnType, body, overrides);
    }
    
    private final List<AnnotationNode> annotations;
    private final boolean isStatic;
    private final String name;
    private final List<FormalTypeParameterNode> typeParameters;
    private final List<FormalArgumentNode> arguments;
    private final Type returnType;
    private final Optional<List<StatementNode>> body;
    private final List<MethodSignature> overrides;

    private MethodNode(
        List<AnnotationNode> annotations,
        boolean isStatic,
        String name,
        List<FormalTypeParameterNode> typeParameters,
        List<FormalArgumentNode> arguments,
        Type returnType,
        Optional<List<StatementNode>> body,
        List<MethodSignature> overrides)
    {
        this.annotations = annotations;
        this.isStatic = isStatic;
        this.name = name;
        this.typeParameters = typeParameters;
        this.arguments = arguments;
        this.returnType = returnType;
        this.body = body;
        this.overrides = overrides;
    }
    
    public List<AnnotationNode> getAnnotations() {
        return annotations;
    }
    
    public boolean isStatic() {
        return isStatic;
    }
    
    public String getName() {
        return name;
    }

    public List<FormalTypeParameterNode> getTypeParameters() {
        return typeParameters;
    }

    public List<FormalArgumentNode> getArguments() {
        return arguments;
    }

    public Type getReturnType() {
        return returnType;
    }
    
    public Optional<List<StatementNode>> getBody() {
        return body;
    }

    public List<MethodSignature> getOverrides() {
        return overrides;
    }

    public boolean isAbstract() {
        return !body.isPresent();
    }

    public MethodSignature signature() {
        return new MethodSignature(
            name,
            eagerMap(typeParameters, parameter -> parameter.getType()),
            eagerMap(arguments, argument -> argument.getType()),
            returnType);
    }

    public MethodNode mapBody(Function<List<StatementNode>, List<StatementNode>> function) {
        return new MethodNode(annotations, isStatic, name, typeParameters, arguments, returnType, body.map(function), overrides);
    }

    @Override
    public <T> T accept(NodeMapper<T> visitor) {
        return visitor.visit(this);
    }

    public MethodNode transform(NodeTransformer transformer) {
        return new MethodNode(
            transformer.transformAnnotations(annotations),
            isStatic,
            transformer.transformMethodName(signature()),
            transformer.transformFormalTypeParameters(typeParameters),
            transformer.transformFormalArguments(arguments),
            transformer.transform(returnType),
            body.map(transformer::transformStatements),
            eagerMap(overrides, transformer::transform));
    }

    @Override
    public String toString() {
        return "MethodNode(" +
            "annotations=" + annotations +
            ", isStatic=" + isStatic +
            ", name=" + name +
            ", typeParameters=" + typeParameters +
            ", arguments=" + arguments +
            ", returnType=" + returnType +
            ", body=" + body +
            ", overrides=" + overrides +
            ')';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MethodNode that = (MethodNode) o;

        if (isStatic != that.isStatic) return false;
        if (!annotations.equals(that.annotations)) return false;
        if (!name.equals(that.name)) return false;
        if (!typeParameters.equals(that.typeParameters)) return false;
        if (!arguments.equals(that.arguments)) return false;
        if (!returnType.equals(that.returnType)) return false;
        if (!body.equals(that.body)) return false;
        return overrides.equals(that.overrides);

    }

    @Override
    public int hashCode() {
        int result = annotations.hashCode();
        result = 31 * result + (isStatic ? 1 : 0);
        result = 31 * result + name.hashCode();
        result = 31 * result + typeParameters.hashCode();
        result = 31 * result + arguments.hashCode();
        result = 31 * result + returnType.hashCode();
        result = 31 * result + body.hashCode();
        result = 31 * result + overrides.hashCode();
        return result;
    }
}