1/*
2 * Copyright (c) 2011, 2014, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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 */
23package org.graalvm.compiler.graph;
24
25import org.graalvm.compiler.debug.GraalError;
26
27/**
28 * This error is the graph/node aware extension of {@link GraalError}.
29 */
30public class GraalGraphError extends GraalError {
31
32    private static final long serialVersionUID = -989290015525497919L;
33    private Node node;
34    private Graph graph;
35
36    /**
37     * This constructor creates a {@link GraalGraphError} with a message assembled via
38     * {@link String#format(String, Object...)}. It always uses the ENGLISH locale in order to
39     * always generate the same output.
40     *
41     * @param msg the message that will be associated with the error, in String.format syntax
42     * @param args parameters to String.format - parameters that implement {@link Iterable} will be
43     *            expanded into a [x, x, ...] representation.
44     */
45    public GraalGraphError(String msg, Object... args) {
46        super(msg, args);
47    }
48
49    /**
50     * This constructor creates a {@link GraalGraphError} for a given causing Throwable instance.
51     *
52     * @param cause the original exception that contains additional information on this error
53     */
54    public GraalGraphError(Throwable cause) {
55        super(cause);
56    }
57
58    /**
59     * This constructor creates a {@link GraalGraphError} from a given GraalError instance.
60     *
61     * @param e the original GraalError
62     */
63    protected GraalGraphError(GraalError e) {
64        super(e);
65        if (e instanceof GraalGraphError) {
66            node = ((GraalGraphError) e).node;
67            graph = ((GraalGraphError) e).graph;
68        }
69    }
70
71    /**
72     * Adds a graph to the context of this VerificationError. The first graph added via this method
73     * will be returned by {@link #graph()}.
74     *
75     * @param newGraph the graph which is in a incorrect state, if the verification error was not
76     *            caused by a specific node
77     */
78    GraalGraphError addContext(Graph newGraph) {
79        if (newGraph != this.graph) {
80            addContext("graph", newGraph);
81            if (this.graph == null) {
82                this.graph = newGraph;
83            }
84        }
85        return this;
86    }
87
88    /**
89     * Adds a node to the context of this VerificationError. The first node added via this method
90     * will be returned by {@link #node()}.
91     *
92     * @param newNode the node which is in a incorrect state, if the verification error was caused
93     *            by a node
94     */
95    public GraalGraphError addContext(Node newNode) {
96        if (newNode != this.node) {
97            addContext("node", newNode);
98            if (this.node == null) {
99                this.node = newNode;
100            }
101        }
102        return this;
103    }
104
105    /**
106     * Transform a GraalError into a GraalGraphInternalError and add a graph to the context.
107     *
108     * @param e the previous error
109     * @param newGraph the graph which is in a incorrect state, if the verification error was not
110     *            caused by a specific node
111     */
112    public static GraalGraphError transformAndAddContext(GraalError e, Graph newGraph) {
113        GraalGraphError graphError;
114        if (e instanceof GraalGraphError) {
115            graphError = (GraalGraphError) e;
116        } else {
117            graphError = new GraalGraphError(e);
118        }
119        return graphError.addContext(newGraph);
120    }
121
122    /**
123     * Transform a GraalError into a GraalGraphInternalError and add a node to the context.
124     *
125     * @param e the previous error
126     * @param newNode the node which is in a incorrect state, if the verification error was caused
127     *            by a node
128     */
129    public static GraalGraphError transformAndAddContext(GraalError e, Node newNode) {
130        GraalGraphError graphError;
131        if (e instanceof GraalGraphError) {
132            graphError = (GraalGraphError) e;
133        } else {
134            graphError = new GraalGraphError(e);
135        }
136        return graphError.addContext(newNode);
137    }
138
139    public Node node() {
140        return node;
141    }
142
143    public Graph graph() {
144        return graph;
145    }
146}
147