11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.graphio;
26
27 import java.io.Closeable;
28 import java.io.IOException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import java.nio.channels.WritableByteChannel;
32 import java.util.Collections;
33 import java.util.Map;
34
35 /**
36 * Instance of output to dump informations about a compiler compilations.
37 *
38 * @param <G> the type of graph this instance handles
39 * @param <M> the type of methods this instance handles
40 */
41 public final class GraphOutput<G, M> implements Closeable {
42 private final GraphProtocol<G, ?, ?, ?, ?, M, ?, ?, ?, ?> printer;
43
44 private GraphOutput(GraphProtocol<G, ?, ?, ?, ?, M, ?, ?, ?, ?> p) {
45 this.printer = p;
46 }
47
48 /**
49 * Creates new builder to configure a future instance of {@link GraphOutput}.
50 *
51 * @param <G> the type of the graph
52 * @param <N> the type of the nodes
53 * @param <C> the type of the node classes
54 * @param <P> the type of the ports
55 *
56 * @param structure description of the structure of the graph
57 * @return the builder to configure
58 */
59 public static <G, N, C, P> Builder<G, N, ?> newBuilder(GraphStructure<G, N, C, P> structure) {
60 return new Builder<>(structure);
61 }
90 }
91
92 /**
93 * Ends compilation group.
94 *
95 * @throws IOException
96 */
97 public void endGroup() throws IOException {
98 printer.endGroup();
99 }
100
101 /**
102 * Closes the output. Closes allocated resources and associated output channel.
103 */
104 @Override
105 public void close() {
106 printer.close();
107 }
108
109 /**
110 * Builder to configure and create an instance of {@link GraphOutput}.
111 *
112 * @param <G> the type of the (root element of) graph
113 * @param <N> the type of the nodes
114 * @param <M> the type of the methods
115 */
116 public static final class Builder<G, N, M> {
117 private final GraphStructure<G, N, ?, ?> structure;
118 private ElementsAndLocations<M, ?, ?> elementsAndLocations;
119
120 private GraphTypes types = DefaultGraphTypes.DEFAULT;
121 private GraphBlocks<G, ?, N> blocks = DefaultGraphBlocks.empty();
122 private int major = 4;
123 private int minor = 0;
124
125 Builder(GraphStructure<G, N, ?, ?> structure) {
126 this.structure = structure;
127 }
128
129 /**
130 * Chooses which version of the protocol to use. The default version is <code>4.0</code>
131 * (when the {@link GraphOutput} & co. classes were introduced). The default can be changed
132 * to other known versions manually by calling this method.
133 *
134 * @param majorVersion by default 4, newer version may be known
135 * @param minorVersion usually 0
136 * @return this builder
137 * @since 0.28
138 */
139 public Builder<G, N, M> protocolVersion(int majorVersion, int minorVersion) {
140 this.major = majorVersion;
141 this.minor = minorVersion;
142 return this;
143 }
144
145 /**
146 * Associates different implementation of types.
147 *
148 * @param graphTypes implementation of types and enum recognition
149 * @return this builder
150 */
151 public Builder<G, N, M> types(GraphTypes graphTypes) {
152 this.types = graphTypes;
153 return this;
154 }
155
156 /**
157 * Associates implementation of blocks.
158 *
159 * @param graphBlocks the blocks implementation
160 * @return this builder
161 */
162 public Builder<G, N, M> blocks(GraphBlocks<G, ?, N> graphBlocks) {
163 this.blocks = graphBlocks;
164 return this;
165 }
209 * {@code channel}, internal constant pool and {@link #protocolVersion(int, int) protocol
210 * version}.
211 * <p>
212 * Both GraphOutput (the {@code parent} and the returned one) has to be used in
213 * synchronization - e.g. only one
214 * {@link GraphOutput#beginGroup(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object, int, java.util.Map)
215 * begin}, {@link GraphOutput#endGroup() end} of group or
216 * {@link GraphOutput#print(java.lang.Object, java.util.Map, int, java.lang.String, java.lang.Object...)
217 * printing} can be on at a given moment.
218 *
219 * @param parent the output to inherit {@code channel} and protocol version from
220 * @return new output sharing {@code channel} and other internals with {@code parent}
221 */
222 public GraphOutput<G, M> build(GraphOutput<?, ?> parent) {
223 return buildImpl(elementsAndLocations, parent);
224 }
225
226 private <L, P> GraphOutput<G, M> buildImpl(ElementsAndLocations<M, L, P> e, WritableByteChannel channel) throws IOException {
227 // @formatter:off
228 ProtocolImpl<G, N, ?, ?, ?, M, ?, ?, ?, ?> p = new ProtocolImpl<>(
229 major, minor, structure, types, blocks,
230 e == null ? null : e.elements,
231 e == null ? null : e.locations, channel
232 );
233 // @formatter:on
234 return new GraphOutput<>(p);
235 }
236
237 private <L, P> GraphOutput<G, M> buildImpl(ElementsAndLocations<M, L, P> e, GraphOutput<?, ?> parent) {
238 // @formatter:off
239 ProtocolImpl<G, N, ?, ?, ?, M, ?, ?, ?, ?> p = new ProtocolImpl<>(
240 parent.printer, structure, types, blocks,
241 e == null ? null : e.elements,
242 e == null ? null : e.locations
243 );
244 // @formatter:on
245 return new GraphOutput<>(p);
246 }
247 }
248
249 private static final class ElementsAndLocations<M, P, L> {
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.graphio;
26
27 import java.io.Closeable;
28 import java.io.IOException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import java.nio.ByteBuffer;
32 import java.nio.channels.WritableByteChannel;
33 import java.util.Collections;
34 import java.util.Map;
35
36 /**
37 * Instance of output to dump informations about a compiler compilations.
38 *
39 * @param <G> the type of graph this instance handles
40 * @param <M> the type of methods this instance handles
41 * @since 1.0 a {@link WritableByteChannel} is implemented
42 */
43 public final class GraphOutput<G, M> implements Closeable, WritableByteChannel {
44 private final GraphProtocol<G, ?, ?, ?, ?, M, ?, ?, ?, ?> printer;
45
46 private GraphOutput(GraphProtocol<G, ?, ?, ?, ?, M, ?, ?, ?, ?> p) {
47 this.printer = p;
48 }
49
50 /**
51 * Creates new builder to configure a future instance of {@link GraphOutput}.
52 *
53 * @param <G> the type of the graph
54 * @param <N> the type of the nodes
55 * @param <C> the type of the node classes
56 * @param <P> the type of the ports
57 *
58 * @param structure description of the structure of the graph
59 * @return the builder to configure
60 */
61 public static <G, N, C, P> Builder<G, N, ?> newBuilder(GraphStructure<G, N, C, P> structure) {
62 return new Builder<>(structure);
63 }
92 }
93
94 /**
95 * Ends compilation group.
96 *
97 * @throws IOException
98 */
99 public void endGroup() throws IOException {
100 printer.endGroup();
101 }
102
103 /**
104 * Closes the output. Closes allocated resources and associated output channel.
105 */
106 @Override
107 public void close() {
108 printer.close();
109 }
110
111 /**
112 * Checks if the {@link GraphOutput} is open.
113 *
114 * @return true if the {@link GraphOutput} is open.
115 * @since 1.0
116 */
117 @Override
118 public boolean isOpen() {
119 return printer.isOpen();
120 }
121
122 /**
123 * Writes raw bytes into {@link GraphOutput}.
124 *
125 * @param src the bytes to write
126 * @return the number of bytes written, possibly zero
127 * @throws IOException in case of IO error
128 * @since 1.0
129 */
130 @Override
131 public int write(ByteBuffer src) throws IOException {
132 return printer.write(src);
133 }
134
135 /**
136 * Builder to configure and create an instance of {@link GraphOutput}.
137 *
138 * @param <G> the type of the (root element of) graph
139 * @param <N> the type of the nodes
140 * @param <M> the type of the methods
141 */
142 public static final class Builder<G, N, M> {
143 private final GraphStructure<G, N, ?, ?> structure;
144 private ElementsAndLocations<M, ?, ?> elementsAndLocations;
145
146 private GraphTypes types = DefaultGraphTypes.DEFAULT;
147 private GraphBlocks<G, ?, N> blocks = DefaultGraphBlocks.empty();
148 private int major = 4;
149 private int minor = 0;
150 private boolean embeddedGraphOutput;
151
152 Builder(GraphStructure<G, N, ?, ?> structure) {
153 this.structure = structure;
154 }
155
156 /**
157 * Chooses which version of the protocol to use. The default version is <code>4.0</code>
158 * (when the {@link GraphOutput} & co. classes were introduced). The default can be changed
159 * to other known versions manually by calling this method.
160 *
161 * @param majorVersion by default 4, newer version may be known
162 * @param minorVersion usually 0
163 * @return this builder
164 * @since 0.28
165 */
166 public Builder<G, N, M> protocolVersion(int majorVersion, int minorVersion) {
167 this.major = majorVersion;
168 this.minor = minorVersion;
169 return this;
170 }
171
172 /**
173 * Sets {@link GraphOutput} as embedded. The embedded {@link GraphOutput} shares
174 * {@link WritableByteChannel channel} with another already open non parent
175 * {@link GraphOutput}. The embedded {@link GraphOutput} flushes data after each
176 * {@link GraphOutput#print print}, {@link GraphOutput#beginGroup beginGroup} and
177 * {@link GraphOutput#endGroup endGroup} call.
178 *
179 * @param embedded if {@code true} the builder creates an embedded {@link GraphOutput}
180 * @return this builder
181 * @since 1.0
182 */
183 public Builder<G, N, M> embedded(boolean embedded) {
184 this.embeddedGraphOutput = embedded;
185 return this;
186 }
187
188 /**
189 * Associates different implementation of types.
190 *
191 * @param graphTypes implementation of types and enum recognition
192 * @return this builder
193 */
194 public Builder<G, N, M> types(GraphTypes graphTypes) {
195 this.types = graphTypes;
196 return this;
197 }
198
199 /**
200 * Associates implementation of blocks.
201 *
202 * @param graphBlocks the blocks implementation
203 * @return this builder
204 */
205 public Builder<G, N, M> blocks(GraphBlocks<G, ?, N> graphBlocks) {
206 this.blocks = graphBlocks;
207 return this;
208 }
252 * {@code channel}, internal constant pool and {@link #protocolVersion(int, int) protocol
253 * version}.
254 * <p>
255 * Both GraphOutput (the {@code parent} and the returned one) has to be used in
256 * synchronization - e.g. only one
257 * {@link GraphOutput#beginGroup(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object, int, java.util.Map)
258 * begin}, {@link GraphOutput#endGroup() end} of group or
259 * {@link GraphOutput#print(java.lang.Object, java.util.Map, int, java.lang.String, java.lang.Object...)
260 * printing} can be on at a given moment.
261 *
262 * @param parent the output to inherit {@code channel} and protocol version from
263 * @return new output sharing {@code channel} and other internals with {@code parent}
264 */
265 public GraphOutput<G, M> build(GraphOutput<?, ?> parent) {
266 return buildImpl(elementsAndLocations, parent);
267 }
268
269 private <L, P> GraphOutput<G, M> buildImpl(ElementsAndLocations<M, L, P> e, WritableByteChannel channel) throws IOException {
270 // @formatter:off
271 ProtocolImpl<G, N, ?, ?, ?, M, ?, ?, ?, ?> p = new ProtocolImpl<>(
272 major, minor, embeddedGraphOutput, structure, types, blocks,
273 e == null ? null : e.elements,
274 e == null ? null : e.locations, channel
275 );
276 // @formatter:on
277 return new GraphOutput<>(p);
278 }
279
280 private <L, P> GraphOutput<G, M> buildImpl(ElementsAndLocations<M, L, P> e, GraphOutput<?, ?> parent) {
281 // @formatter:off
282 ProtocolImpl<G, N, ?, ?, ?, M, ?, ?, ?, ?> p = new ProtocolImpl<>(
283 parent.printer, structure, types, blocks,
284 e == null ? null : e.elements,
285 e == null ? null : e.locations
286 );
287 // @formatter:on
288 return new GraphOutput<>(p);
289 }
290 }
291
292 private static final class ElementsAndLocations<M, P, L> {
|