Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
antonsviridov-src committed Sep 11, 2024
1 parent d314dd8 commit 88cd420
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ public static Semanticdb.AssignTree assignTree(Semanticdb.Tree lhs, Semanticdb.T
return Semanticdb.AssignTree.newBuilder().setLhs(lhs).setRhs(rhs).build();
}

public static Semanticdb.CastTree castTree(
Semanticdb.Type type, Semanticdb.Tree value) {
return Semanticdb.CastTree.newBuilder().setTpe(type).setValue(value).build();
}
// SemanticDB Constants


public static Semanticdb.AnnotationTree annotationTree(
Semanticdb.Type type, Iterable<Semanticdb.Tree> parameters) {
return Semanticdb.AnnotationTree.newBuilder().setTpe(type).addAllParameters(parameters).build();
Expand Down
7 changes: 7 additions & 0 deletions semanticdb-java/src/main/protobuf/semanticdb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ message Tree {
AssignTree assign_tree = 10;
BinaryOperatorTree binop_tree = 11;
UnaryOperatorTree unaryop_tree = 12;
CastTree cast_tree = 13;
// -- OUT OF SPEC -- //
}
}
Expand Down Expand Up @@ -347,6 +348,12 @@ message AnnotationTree {
repeated Tree parameters = 2;
}


message CastTree {
Type tpe = 1;
Tree value = 2;
}

message AssignTree {
Tree lhs = 1;
Tree rhs = 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package com.sourcegraph.semanticdb_javac;

import com.sun.source.tree.Tree;
import com.sun.source.tree.*;
import com.sun.source.util.Trees;
import javax.lang.model.element.Element;
import javax.lang.model.util.Types;
import javax.lang.model.type.TypeMirror;
import com.sun.source.util.TreePath;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.BinaryTree;
import com.sun.source.tree.UnaryTree;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ParenthesizedTree;

import java.util.HashMap;
import java.util.ArrayList;
Expand All @@ -44,6 +31,7 @@ public SemanticdbTrees(
this.types = types;
this.trees = trees;
this.nodes = nodes;
this.typeVisitor = new SemanticdbTypeVisitor(globals, locals, types);
}

private final GlobalSymbolsCache globals;
Expand All @@ -52,6 +40,7 @@ public SemanticdbTrees(
private final Types types;
private final Trees trees;
private final HashMap<Tree, TreePath> nodes;
private final SemanticdbTypeVisitor typeVisitor;

public List<Semanticdb.AnnotationTree> annotations(Tree node) {
if (!(node instanceof ClassTree)
Expand Down Expand Up @@ -101,10 +90,18 @@ public Semanticdb.AnnotationTree annotationBuilder(AnnotationTree annotation) {
Element annotationSym = trees.getElement(annotationTreePath);

Semanticdb.Type type =
new SemanticdbTypeVisitor(globals, locals, types).semanticdbType(annotationSym.asType());
typeVisitor.semanticdbType(annotationSym.asType());
return annotationTree(type, params);
}

private TypeMirror getTreeType(Tree tree) {
TreePath path = nodes.get(tree);
System.out.println("Path: " + path);
Element sym = trees.getElement(path);
System.out.println("SYM: " + sym);
return sym.asType();
}

private Semanticdb.Tree annotationParameter(ExpressionTree expr) {
if (expr instanceof MemberSelectTree) {
TreePath expressionTreePath = nodes.get(expr);
Expand Down Expand Up @@ -164,6 +161,10 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) {
} else if (expr instanceof ParenthesizedTree) {
ParenthesizedTree parenExpr = (ParenthesizedTree) expr;
return annotationParameter(parenExpr.getExpression());
} else if (expr instanceof TypeCastTree) {
TypeCastTree tree = (TypeCastTree) expr;
System.out.println(typeVisitor.semanticdbType(getTreeType(tree.getType())));
// tree.getType()
}
throw new IllegalArgumentException(
semanticdbUri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.TypeCastTree;
import com.sun.source.tree.TypeParameterTree;
import com.sun.source.tree.ParameterizedTypeTree;

Expand Down Expand Up @@ -223,6 +224,9 @@ void resolveNodes() {
resolveMemberSelectTree((MemberSelectTree) node, entry.getValue());
} else if (node instanceof NewClassTree) {
resolveNewClassTree((NewClassTree) node, entry.getValue());
} else if (node instanceof TypeCastTree) {
resolveCastTree((TypeCastTree) node, entry.getValue());
System.out.println("Skipping resolution of [[\n" + node.getClass() + node.toString() + "\n]]");
}
}
}
Expand Down Expand Up @@ -337,6 +341,31 @@ private void resolveMemberSelectTree(MemberSelectTree node, TreePath treePath) {
}
}


private void resolveCastTree(TypeCastTree node, TreePath treePath) {
// ignore anonymous classes - otherwise there will be a local reference to itself
System.out.println("helo");
// if (node.getIdentifier() != null && node.getClassBody() == null) {
// Element sym = trees.getElement(treePath);
// if (sym != null) {
// TreePath parentPath = treePath.getParentPath();
// Element parentSym = trees.getElement(parentPath);

// if (parentSym == null || parentSym.getKind() != ElementKind.ENUM_CONSTANT) {
// TreePath identifierTreePath = nodes.get(node.getIdentifier());
// Element identifierSym = trees.getElement(identifierTreePath);
// emitSymbolOccurrence(
// sym,
// node,
// identifierSym.getSimpleName(),
// Role.REFERENCE,
// CompilerRange.FROM_TEXT_SEARCH);
// }
// }
// }
}


private void resolveNewClassTree(NewClassTree node, TreePath treePath) {
// ignore anonymous classes - otherwise there will be a local reference to itself
if (node.getIdentifier() != null && node.getClassBody() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ interface Foo {

@Nullable(("what"))
Foo test4();

@Bar((double) -1)
double test();
}

0 comments on commit 88cd420

Please sign in to comment.