1 /*
   2  * Copyright (c) 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 package jdk.internal.clang;
  24 
  25 import java.nio.ByteBuffer;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.util.Objects;
  29 
  30 public class SourceLocation extends StructType {
  31 
  32     SourceLocation(ByteBuffer buf) {
  33         super(buf);
  34     }
  35 
  36     protected SourceLocation(ByteBuffer buf, boolean copy) {
  37         super(buf, copy);
  38     }
  39 
  40     public native Location getFileLocation();
  41     public native Location getExpansionLocation();
  42     public native Location getSpellingLocation();
  43     public native boolean isInSystemHeader();
  44     public native boolean isFromMainFile();
  45 
  46     @Override
  47     public boolean equals(Object other) {
  48         if (this == other) {
  49             return true;
  50         }
  51         if (!(other instanceof SourceLocation)) {
  52             return false;
  53         }
  54         SourceLocation sloc = (SourceLocation)other;
  55         return Objects.equals(getFileLocation(), sloc.getFileLocation());
  56     }
  57 
  58     @Override
  59     public int hashCode() {
  60         return getFileLocation().hashCode();
  61     }
  62 
  63     public final static class Location {
  64         private final Path path;
  65         private final int line;
  66         private final int column;
  67         private final int offset;
  68 
  69         private Location(String filename, int line, int column, int offset) {
  70             if (filename == null || filename.isEmpty()) {
  71                 this.path = null;
  72             } else {
  73                 this.path = Paths.get(filename);
  74             }
  75 
  76             this.line = line;
  77             this.column = column;
  78             this.offset = offset;
  79         }
  80 
  81         public Path path() {
  82             return path;
  83         }
  84 
  85         public int line() {
  86             return line;
  87         }
  88 
  89         public int column() {
  90             return column;
  91         }
  92 
  93         public int offset() {
  94             return offset;
  95         }
  96 
  97         @Override
  98         public boolean equals(Object other) {
  99             if (this == other) {
 100                 return true;
 101             }
 102             if (!(other instanceof Location)) {
 103                 return false;
 104             }
 105             Location loc = (Location)other;
 106             return Objects.equals(path, loc.path) &&
 107                 line == loc.line && column == loc.column &&
 108                 offset == loc.offset;
 109         }
 110 
 111         @Override
 112         public int hashCode() {
 113             return Objects.hashCode(path) ^ line ^ column ^ offset;
 114         }
 115     }
 116 }