001/*
002 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.graph;
024
025import jdk.internal.jvmci.common.*;
026
027/**
028 * This error is the graph/node aware extension of JVMCIError.
029 */
030public class GraalGraphJVMCIError extends JVMCIError {
031
032    private static final long serialVersionUID = -989290015525497919L;
033    private Node node;
034    private Graph graph;
035
036    /**
037     * This constructor creates a {@link GraalGraphJVMCIError} with a message assembled via
038     * {@link String#format(String, Object...)}. It always uses the ENGLISH locale in order to
039     * always generate the same output.
040     *
041     * @param msg the message that will be associated with the error, in String.format syntax
042     * @param args parameters to String.format - parameters that implement {@link Iterable} will be
043     *            expanded into a [x, x, ...] representation.
044     */
045    public GraalGraphJVMCIError(String msg, Object... args) {
046        super(msg, args);
047    }
048
049    /**
050     * This constructor creates a {@link GraalGraphJVMCIError} for a given causing Throwable
051     * instance.
052     *
053     * @param cause the original exception that contains additional information on this error
054     */
055    public GraalGraphJVMCIError(Throwable cause) {
056        super(cause);
057    }
058
059    /**
060     * This constructor creates a {@link GraalGraphJVMCIError} from a given JVMCIError instance.
061     *
062     * @param e the original JVMCIError
063     */
064    protected GraalGraphJVMCIError(JVMCIError e) {
065        super(e);
066        if (e instanceof GraalGraphJVMCIError) {
067            node = ((GraalGraphJVMCIError) e).node;
068            graph = ((GraalGraphJVMCIError) e).graph;
069        }
070    }
071
072    /**
073     * Adds a graph to the context of this VerificationError. The first graph added via this method
074     * will be returned by {@link #graph()}.
075     *
076     * @param newGraph the graph which is in a incorrect state, if the verification error was not
077     *            caused by a specific node
078     */
079    GraalGraphJVMCIError addContext(Graph newGraph) {
080        if (newGraph != this.graph) {
081            addContext("graph", newGraph);
082            if (this.graph == null) {
083                this.graph = newGraph;
084            }
085        }
086        return this;
087    }
088
089    /**
090     * Adds a node to the context of this VerificationError. The first node added via this method
091     * will be returned by {@link #node()}.
092     *
093     * @param newNode the node which is in a incorrect state, if the verification error was caused
094     *            by a node
095     */
096    public GraalGraphJVMCIError addContext(Node newNode) {
097        if (newNode != this.node) {
098            addContext("node", newNode);
099            if (this.node == null) {
100                this.node = newNode;
101            }
102        }
103        return this;
104    }
105
106    /**
107     * Transform a JVMCIError into a GraalGraphInternalError and add a graph to the context.
108     *
109     * @param e the previous error
110     * @param newGraph the graph which is in a incorrect state, if the verification error was not
111     *            caused by a specific node
112     */
113    public static GraalGraphJVMCIError transformAndAddContext(JVMCIError e, Graph newGraph) {
114        GraalGraphJVMCIError graphError;
115        if (e instanceof GraalGraphJVMCIError) {
116            graphError = (GraalGraphJVMCIError) e;
117        } else {
118            graphError = new GraalGraphJVMCIError(e);
119        }
120        return graphError.addContext(newGraph);
121    }
122
123    /**
124     * Transform a JVMCIError into a GraalGraphInternalError and add a node to the context.
125     *
126     * @param e the previous error
127     * @param newNode the node which is in a incorrect state, if the verification error was caused
128     *            by a node
129     */
130    public static GraalGraphJVMCIError transformAndAddContext(JVMCIError e, Node newNode) {
131        GraalGraphJVMCIError graphError;
132        if (e instanceof GraalGraphJVMCIError) {
133            graphError = (GraalGraphJVMCIError) e;
134        } else {
135            graphError = new GraalGraphJVMCIError(e);
136        }
137        return graphError.addContext(newNode);
138    }
139
140    public Node node() {
141        return node;
142    }
143
144    public Graph graph() {
145        return graph;
146    }
147}