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 
  24 package jdk.internal.clang;
  25 
  26 import static clang.Index_h.CXCursor;
  27 import static clang.Index_h.CXSourceLocation;
  28 import static clang.Index_h.CXSourceRange;
  29 
  30 import java.foreign.memory.Pointer;
  31 import java.foreign.Scope;
  32 import java.util.ArrayList;
  33 import java.util.stream.Stream;
  34 
  35 public class Cursor {
  36 
  37     private final CXCursor cursor;
  38     private final int kind;
  39 
  40     Cursor(CXCursor cursor) {
  41         this.cursor = cursor;
  42         kind = LibClang.lib.clang_getCursorKind(cursor);
  43     }
  44     public boolean isDeclaration() {
  45         return LibClang.lib.clang_isDeclaration(kind) != 0;
  46     }
  47 
  48     public boolean isPreprocessing() { return LibClang.lib.clang_isPreprocessing(kind) != 0; }
  49 
  50     public boolean isInvalid() {
  51         return LibClang.lib.clang_isInvalid(kind) != 0;
  52     }
  53 
  54     public boolean isDefinition() {
  55         return LibClang.lib.clang_isCursorDefinition(cursor) != 0;
  56     }
  57 
  58     public boolean isAnonymousStruct() { return LibClang.lib.clang_Cursor_isAnonymous(cursor) != 0; }
  59 
  60     public boolean isMacroFunctionLike() {
  61         return LibClang.lib.clang_Cursor_isMacroFunctionLike(cursor) != 0;
  62     }
  63 
  64     public String spelling() {
  65         return LibClang.CXStrToString(
  66                 LibClang.lib.clang_getCursorSpelling(cursor));
  67     }
  68 
  69     public String USR() {
  70         return LibClang.CXStrToString(
  71                 LibClang.lib.clang_getCursorUSR(cursor));
  72     }
  73 
  74     public boolean equalCursor(Cursor other) {
  75         return LibClang.lib.clang_equalCursors(cursor, other.cursor) != 0;
  76     }
  77 
  78     public Type type() {
  79         return new Type(LibClang.lib.clang_getCursorType(cursor));
  80     }
  81 
  82     public Type getEnumDeclIntegerType() {
  83         return new Type(LibClang.lib.clang_getEnumDeclIntegerType(cursor));
  84     }
  85 
  86     public Cursor getDefinition() {
  87         return new Cursor(LibClang.lib.clang_getCursorDefinition(cursor));
  88     }
  89 
  90     public SourceLocation getSourceLocation() {
  91         CXSourceLocation loc = LibClang.lib.clang_getCursorLocation(cursor);
  92         if (LibClang.lib.clang_equalLocations(loc, LibClang.lib.clang_getNullLocation()) != 0) {
  93             return null;
  94         }
  95         return new SourceLocation(loc);
  96     }
  97 
  98     public SourceRange getExtent() {
  99         CXSourceRange range = LibClang.lib.clang_getCursorExtent(cursor);
 100         if (LibClang.lib.clang_Range_isNull(range) != 0) {
 101             return null;
 102         }
 103         return new SourceRange(range);
 104     }
 105 
 106     public int numberOfArgs() {
 107         return LibClang.lib.clang_Cursor_getNumArguments(cursor);
 108     }
 109 
 110     public Cursor getArgument(int idx) {
 111         return new Cursor(LibClang.lib.clang_Cursor_getArgument(cursor, idx));
 112     }
 113 
 114     // C long long, 64-bit
 115     public long getEnumConstantValue() {
 116         return LibClang.lib.clang_getEnumConstantDeclValue(cursor);
 117     }
 118 
 119     // C unsigned long long, 64-bit
 120     public long getEnumConstantUnsignedValue() {
 121         return LibClang.lib.clang_getEnumConstantDeclUnsignedValue(cursor);
 122     }
 123 
 124     public boolean isBitField() {
 125         return LibClang.lib.clang_Cursor_isBitField(cursor) != 0;
 126     }
 127 
 128     public int getBitFieldWidth() {
 129         return LibClang.lib.clang_getFieldDeclBitWidth(cursor);
 130     }
 131 
 132     public CursorKind kind() {
 133         return CursorKind.valueOf(kind);
 134     }
 135 
 136     public Stream<Cursor> children() {
 137         final ArrayList<Cursor> ar = new ArrayList<>();
 138         // FIXME: need a way to pass ar down as user data d
 139         try (Scope sc = Scope.globalScope().fork()) {
 140             LibClang.lib.clang_visitChildren(cursor, sc.allocateCallback((c, p, d) -> {
 141                 ar.add(new Cursor(c));
 142                 return LibClang.lib.CXChildVisit_Continue();
 143             }), Pointer.ofNull());
 144             return ar.stream();
 145         }
 146     }
 147 
 148     public Stream<Cursor> allChildren() {
 149         return children().flatMap(c -> Stream.concat(Stream.of(c), c.children()));
 150     }
 151 
 152     public String getMangling() {
 153         return LibClang.CXStrToString(
 154                 LibClang.lib.clang_Cursor_getMangling(cursor));
 155     }
 156 
 157     public TranslationUnit getTranslationUnit() {
 158         return new TranslationUnit(LibClang.lib.clang_Cursor_getTranslationUnit(cursor));
 159     }
 160 
 161     public EvalResult eval() {
 162         //this throws because, for now, the eval API is never needed when parsing Index.h
 163         //which only contains simple numeric macros which can be parsed w/o libclang support.
 164         throw new UnsupportedOperationException("Eval API missing!");
 165     }
 166 
 167     @Override
 168     public boolean equals(Object other) {
 169         if (this == other) {
 170             return true;
 171         }
 172         if (!(other instanceof Cursor)) {
 173             return false;
 174         }
 175         return (LibClang.lib.clang_equalCursors(cursor, ((Cursor)other).cursor) != 0);
 176     }
 177 
 178     @Override
 179     public int hashCode() {
 180         return spelling().hashCode();
 181     }
 182 }