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