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