1 /* 2 * Copyright (c) 2018, 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 com.sun.tools.jextract.tree; 24 25 import java.util.Objects; 26 import jdk.internal.clang.Cursor; 27 import jdk.internal.clang.Type; 28 import jdk.internal.clang.SourceLocation; 29 import jdk.internal.clang.SourceRange; 30 31 public class Tree { 32 private final Cursor c; 33 private final String name; 34 35 Tree(Cursor c) { 36 this(c, c.spelling()); 37 } 38 39 Tree(Cursor c, String name) { 40 this.c = Objects.requireNonNull(c); 41 this.name = Objects.requireNonNull(name); 42 } 43 44 public final Cursor cursor() { 45 return c; 46 } 47 48 public final Type type() { 49 return c.type(); 50 } 51 52 public Tree withName(String newName) { 53 return name.equals(newName)? this : new Tree(c, newName); 54 } 55 56 public final String name() { 57 return name; 58 } 59 60 public final SourceLocation location() { 61 return c.getSourceLocation(); 62 } 63 64 public final String USR() { 65 return c.USR(); 66 } 67 68 public final boolean isDeclaration() { 69 return c.isDeclaration(); 70 } 71 72 public final boolean isDefinition() { 73 return c.isDefinition(); 74 } 75 76 public final boolean isPreprocessing() { 77 return c.isPreprocessing(); 78 } 79 80 public final boolean isFromMain() { 81 return location().isFromMainFile(); 82 } 83 84 public final boolean isFromSystem() { 85 return location().isInSystemHeader(); 86 } 87 88 @Override 89 public final boolean equals(Object obj) { 90 if (this == obj) { 91 return true; 92 } 93 94 if (!(obj instanceof Tree)) { 95 return false; 96 } 97 98 Tree t = (Tree)obj; 99 return name.equals(t.name()) && c.equals(t.cursor()); 100 } 101 102 @Override 103 public final int hashCode() { 104 return name.hashCode() ^ c.hashCode(); 105 } 106 107 @Override 108 public final String toString() { 109 return Printer.Stringifier(p -> p.dumpCursor(c, true)); 110 } 111 112 public <R,D> R accept(TreeVisitor<R,D> visitor, D data) { 113 return visitor.visitTree(this, data); 114 } 115 }