1 /*
   2  * Copyright (c) 2016, 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 package org.graalvm.compiler.code;
  24 
  25 import static jdk.vm.ci.meta.MetaUtil.identityHashCodeString;
  26 
  27 import org.graalvm.compiler.graph.NodeSourcePosition;
  28 
  29 /**
  30  * This provides a mapping between a half-open range of PCs in the generated code and a
  31  * {@link NodeSourcePosition} in the original program. Depending on the backend this information may
  32  * be represented in different ways or not at all.
  33  */
  34 public final class SourceMapping {
  35 
  36     private final int startOffset;
  37 
  38     private final int endOffset;
  39 
  40     private final NodeSourcePosition sourcePosition;
  41 
  42     public SourceMapping(int startOffset, int endOffset, NodeSourcePosition sourcePosition) {
  43         this.startOffset = startOffset;
  44         this.endOffset = endOffset;
  45         this.sourcePosition = sourcePosition;
  46     }
  47 
  48     public int getStartOffset() {
  49         return startOffset;
  50     }
  51 
  52     public int getEndOffset() {
  53         return endOffset;
  54     }
  55 
  56     public NodeSourcePosition getSourcePosition() {
  57         return sourcePosition;
  58     }
  59 
  60     @Override
  61     public String toString() {
  62         return identityHashCodeString(this);
  63     }
  64 
  65     @Override
  66     public int hashCode() {
  67         throw new UnsupportedOperationException("hashCode");
  68     }
  69 
  70     @Override
  71     public boolean equals(Object obj) {
  72         if (obj instanceof SourceMapping) {
  73             SourceMapping other = (SourceMapping) obj;
  74             return other.startOffset == startOffset && other.endOffset == endOffset && other.sourcePosition.equals(sourcePosition);
  75         }
  76         return false;
  77     }
  78 
  79     public boolean contains(int offset) {
  80         return startOffset <= offset && offset < endOffset;
  81     }
  82 
  83 }