1 /*
2 * Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.incubator.code;
27
28 import com.sun.tools.javac.api.JavacScope;
29 import com.sun.tools.javac.api.JavacTrees;
30 import com.sun.tools.javac.code.Symbol.ClassSymbol;
31 import com.sun.tools.javac.comp.Attr;
32 import com.sun.tools.javac.model.JavacElements;
33 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
34 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
35 import com.sun.tools.javac.tree.TreeMaker;
36 import com.sun.tools.javac.util.Context;
37 import jdk.incubator.code.dialect.core.CoreOp.FuncOp;
38 import jdk.incubator.code.dialect.core.CoreType;
39 import jdk.incubator.code.dialect.core.FunctionType;
40 import jdk.incubator.code.dialect.java.JavaOp;
41 import jdk.incubator.code.dialect.java.MethodRef;
42 import jdk.incubator.code.extern.OpWriter;
43 import jdk.incubator.code.internal.ReflectMethods;
44 import jdk.internal.access.SharedSecrets;
45
46 import javax.annotation.processing.ProcessingEnvironment;
47 import javax.lang.model.element.ExecutableElement;
48 import javax.lang.model.element.Modifier;
49 import java.lang.reflect.Method;
50 import java.lang.reflect.Proxy;
51 import java.util.*;
52 import java.util.function.BiFunction;
53
54 /**
55 * The abstract implementation of an operation. All concrete operations extend this class.
56 *
57 * <h2>Operation implementation requirements</h2>
58 * <p>
59 * A concrete operation class must satisfy the following requirements:
60 * <ul>
61 * <li>
62 * implement {@link #resultType()} to return the result type of operation instances;
63 * <li>
64 * implement {@link #transform(CodeContext, CodeTransformer)} to return a newly constructed, unplaced copy whose
65 * concrete class is the concrete operation class;
66 * <li>
67 * call an appropriate {@code AbstractOp} superclass constructor from each concrete operation constructor. Constructors
68 * for new operations pass the operation's operands to {@link #AbstractOp(List)}. Constructors for transformed copies
69 * can pass the input operation and code context to {@link #AbstractOp(AbstractOp, CodeContext)};
70 * <li>
71 * override {@link #bodies()} if instances may have bodies. If the operation class implements {@link Op.Nested}, then
72 * {@code bodies()} must return one or more bodies;
73 * <li>
74 * override {@link #successors()} if instances may have successors. If the operation class implements
75 * {@link Op.BlockTerminating}, then {@code successors()} must return one or more successors;
76 * <li>
77 * copy mutable constructor arguments that define successors, bodies, and operation-specific state, ensuring they are
78 * all fixed when construction completes; and
79 * <li>
80 * return unmodifiable views or immutable values from accessors that expose successors, bodies, and operation-specific
81 * state.
82 * </ul>
83 * <p>
84 * A concrete operation class may additionally:
85 * <ul>
86 * <li>
87 * override {@link #externalizeOpName()} and {@link #externalize()} to define an external form;
88 * <li>
89 * implement {@link Op.Lowerable} to define a lowering; and
90 * <li>
91 * provide operation-specific accessors for operation-specific state.
92 * </ul>
93 */
94 public non-sealed abstract class AbstractOp implements Op {
95
96 // Set when op is placed in a block or as a root operation, otherwise null when unplaced
97 // @@@ stable value?
98 Result result;
99
100 // null if not specified
101 // @@@ stable value?
102 Location location;
103
104 final List<Value> operands;
105
106 /**
107 * Constructs an operation with a list of operands.
108 *
109 * @param operands the list of operands, a copy of the list is performed if required.
110 * @throws IllegalArgumentException if an operand's declaring block is built.
111 */
112 protected AbstractOp(List<? extends Value> operands) {
113 for (Value operand : operands) {
114 if (operand.isBuilt()) {
115 throw new IllegalArgumentException("Operand's declaring block is built: " + operand);
116 }
117 }
118 this.operands = List.copyOf(operands);
119 }
120
121 /**
122 * Constructs an operation with operands mapped from, and location copied from, the given operation.
123 * <p>
124 * The constructed operation's operands are the values mapped, in order, from the given operation's operands using
125 * the given code context. The new operation's location is the given operation's location, if any.
126 *
127 * @param that the operation
128 * @param cc the code context
129 * @throws IllegalArgumentException if an operation's operand has no context mapping
130 * @throws IllegalArgumentException if a mapped value's declaring block is built.
131 */
132 protected AbstractOp(AbstractOp that, CodeContext cc) {
133 this(cc.getValues(that.operands));
134 this.location = that.location;
135 }
136
137 @Override
138 public final void setLocation(Location l) {
139 // @@@ Fail if location != null?
140 if (isRoot() || (result != null && result.block.isBuilt())) {
141 throw new IllegalStateException("Built operation");
142 }
143
144 location = l;
145 }
146
147 @Override
148 public final Location location() {
149 return location;
150 }
151
152 @Override
153 public final Block parent() {
154 if (isRoot() || result == null) {
155 return null;
156 }
157
158 if (!result.block.isBuilt()) {
159 throw new IllegalStateException("Parent block is unobservable");
160 }
161
162 return result.block;
163 }
164
165 @Override
166 public final List<Body> children() {
167 return bodies();
168 }
169
170 /**
171 * {@inheritDoc}
172 * @implSpec this implementation returns an unmodifiable empty list.
173 */
174 @Override
175 public List<Body> bodies() {
176 return List.of();
177 }
178
179 @Override
180 public final Result result() {
181 return result == Result.ROOT_RESULT ? null : result;
182 }
183
184 @Override
185 public final List<Value> operands() {
186 return operands;
187 }
188
189 /**
190 * {@inheritDoc}
191 * @implSpec this implementation returns an unmodifiable empty list.
192 */
193 @Override
194 public List<Block.Reference> successors() {
195 return List.of();
196 }
197
198 @Override
199 public final FunctionType opSignature() {
200 List<CodeType> operandTypes = operands.stream().map(Value::type).toList();
201 return CoreType.functionType(resultType(), operandTypes);
202 }
203
204 @Override
205 public final List<Value> capturedValues() {
206 Set<Value> cvs = new LinkedHashSet<>();
207
208 Deque<Body> bodyStack = new ArrayDeque<>();
209 for (Body childBody : bodies()) {
210 Body.capturedValues(cvs, bodyStack, childBody);
211 }
212 return new ArrayList<>(cvs);
213 }
214
215 @Override
216 public final void buildAsRoot() {
217 if (result == Result.ROOT_RESULT) {
218 return;
219 }
220 if (!bodies().stream().allMatch(Body::isIsolated)) {
221 throw new IllegalStateException("One of the operation bodies is not isolated");
222 }
223 if (!operands().isEmpty()) {
224 throw new IllegalStateException("Operation has operands");
225 }
226 if (!successors().isEmpty()) {
227 throw new IllegalStateException("Operation has successors");
228 }
229 if (result != null) {
230 throw new IllegalStateException("Operation is placed in a block");
231 }
232 result = Result.ROOT_RESULT;
233 }
234
235 @Override
236 public final boolean isRoot() {
237 return result == Result.ROOT_RESULT;
238 }
239
240 @Override
241 public final boolean isPlacedInBlock() {
242 return !isRoot() && result != null;
243 }
244
245 /**
246 * {@inheritDoc}
247 * @implSpec this implementation returns the result of the expression {@code this.getClass().getName()}.
248 */
249 @Override
250 public String externalizeOpName() {
251 return this.getClass().getName();
252 }
253
254 /**
255 * {@inheritDoc}
256 * @implSpec this implementation returns an unmodifiable empty map.
257 */
258 @Override
259 public Map<String, Object> externalize() {
260 return Map.of();
261 }
262
263 @Override
264 public final String toText() {
265 return OpWriter.toText(this);
266 }
267 }