Skip to content

Commit 88cd420

Browse files
WIP
1 parent d314dd8 commit 88cd420

File tree

5 files changed

+63
-16
lines changed

5 files changed

+63
-16
lines changed

semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ public static Semanticdb.AssignTree assignTree(Semanticdb.Tree lhs, Semanticdb.T
163163
return Semanticdb.AssignTree.newBuilder().setLhs(lhs).setRhs(rhs).build();
164164
}
165165

166+
public static Semanticdb.CastTree castTree(
167+
Semanticdb.Type type, Semanticdb.Tree value) {
168+
return Semanticdb.CastTree.newBuilder().setTpe(type).setValue(value).build();
169+
}
170+
// SemanticDB Constants
171+
172+
166173
public static Semanticdb.AnnotationTree annotationTree(
167174
Semanticdb.Type type, Iterable<Semanticdb.Tree> parameters) {
168175
return Semanticdb.AnnotationTree.newBuilder().setTpe(type).addAllParameters(parameters).build();

semanticdb-java/src/main/protobuf/semanticdb.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ message Tree {
298298
AssignTree assign_tree = 10;
299299
BinaryOperatorTree binop_tree = 11;
300300
UnaryOperatorTree unaryop_tree = 12;
301+
CastTree cast_tree = 13;
301302
// -- OUT OF SPEC -- //
302303
}
303304
}
@@ -347,6 +348,12 @@ message AnnotationTree {
347348
repeated Tree parameters = 2;
348349
}
349350

351+
352+
message CastTree {
353+
Type tpe = 1;
354+
Tree value = 2;
355+
}
356+
350357
message AssignTree {
351358
Tree lhs = 1;
352359
Tree rhs = 2;

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
package com.sourcegraph.semanticdb_javac;
22

3-
import com.sun.source.tree.Tree;
3+
import com.sun.source.tree.*;
44
import com.sun.source.util.Trees;
55
import javax.lang.model.element.Element;
66
import javax.lang.model.util.Types;
7+
import javax.lang.model.type.TypeMirror;
78
import com.sun.source.util.TreePath;
89
import com.sun.source.tree.Tree;
910
import com.sun.source.tree.Tree.Kind;
10-
import com.sun.source.tree.BinaryTree;
11-
import com.sun.source.tree.UnaryTree;
12-
import com.sun.source.tree.AssignmentTree;
13-
import com.sun.source.tree.MemberSelectTree;
14-
import com.sun.source.tree.ClassTree;
15-
import com.sun.source.tree.VariableTree;
16-
import com.sun.source.tree.MethodTree;
17-
import com.sun.source.tree.ModifiersTree;
18-
import com.sun.source.tree.IdentifierTree;
19-
import com.sun.source.tree.ExpressionTree;
20-
import com.sun.source.tree.LiteralTree;
21-
import com.sun.source.tree.NewArrayTree;
22-
import com.sun.source.tree.AnnotationTree;
23-
import com.sun.source.tree.ParenthesizedTree;
2411

2512
import java.util.HashMap;
2613
import java.util.ArrayList;
@@ -44,6 +31,7 @@ public SemanticdbTrees(
4431
this.types = types;
4532
this.trees = trees;
4633
this.nodes = nodes;
34+
this.typeVisitor = new SemanticdbTypeVisitor(globals, locals, types);
4735
}
4836

4937
private final GlobalSymbolsCache globals;
@@ -52,6 +40,7 @@ public SemanticdbTrees(
5240
private final Types types;
5341
private final Trees trees;
5442
private final HashMap<Tree, TreePath> nodes;
43+
private final SemanticdbTypeVisitor typeVisitor;
5544

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

10392
Semanticdb.Type type =
104-
new SemanticdbTypeVisitor(globals, locals, types).semanticdbType(annotationSym.asType());
93+
typeVisitor.semanticdbType(annotationSym.asType());
10594
return annotationTree(type, params);
10695
}
10796

97+
private TypeMirror getTreeType(Tree tree) {
98+
TreePath path = nodes.get(tree);
99+
System.out.println("Path: " + path);
100+
Element sym = trees.getElement(path);
101+
System.out.println("SYM: " + sym);
102+
return sym.asType();
103+
}
104+
108105
private Semanticdb.Tree annotationParameter(ExpressionTree expr) {
109106
if (expr instanceof MemberSelectTree) {
110107
TreePath expressionTreePath = nodes.get(expr);
@@ -164,6 +161,10 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) {
164161
} else if (expr instanceof ParenthesizedTree) {
165162
ParenthesizedTree parenExpr = (ParenthesizedTree) expr;
166163
return annotationParameter(parenExpr.getExpression());
164+
} else if (expr instanceof TypeCastTree) {
165+
TypeCastTree tree = (TypeCastTree) expr;
166+
System.out.println(typeVisitor.semanticdbType(getTreeType(tree.getType())));
167+
// tree.getType()
167168
}
168169
throw new IllegalArgumentException(
169170
semanticdbUri

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.sun.source.tree.ExpressionTree;
1616
import com.sun.source.tree.NewClassTree;
1717
import com.sun.source.tree.MemberSelectTree;
18+
import com.sun.source.tree.TypeCastTree;
1819
import com.sun.source.tree.TypeParameterTree;
1920
import com.sun.source.tree.ParameterizedTypeTree;
2021

@@ -223,6 +224,9 @@ void resolveNodes() {
223224
resolveMemberSelectTree((MemberSelectTree) node, entry.getValue());
224225
} else if (node instanceof NewClassTree) {
225226
resolveNewClassTree((NewClassTree) node, entry.getValue());
227+
} else if (node instanceof TypeCastTree) {
228+
resolveCastTree((TypeCastTree) node, entry.getValue());
229+
System.out.println("Skipping resolution of [[\n" + node.getClass() + node.toString() + "\n]]");
226230
}
227231
}
228232
}
@@ -337,6 +341,31 @@ private void resolveMemberSelectTree(MemberSelectTree node, TreePath treePath) {
337341
}
338342
}
339343

344+
345+
private void resolveCastTree(TypeCastTree node, TreePath treePath) {
346+
// ignore anonymous classes - otherwise there will be a local reference to itself
347+
System.out.println("helo");
348+
// if (node.getIdentifier() != null && node.getClassBody() == null) {
349+
// Element sym = trees.getElement(treePath);
350+
// if (sym != null) {
351+
// TreePath parentPath = treePath.getParentPath();
352+
// Element parentSym = trees.getElement(parentPath);
353+
354+
// if (parentSym == null || parentSym.getKind() != ElementKind.ENUM_CONSTANT) {
355+
// TreePath identifierTreePath = nodes.get(node.getIdentifier());
356+
// Element identifierSym = trees.getElement(identifierTreePath);
357+
// emitSymbolOccurrence(
358+
// sym,
359+
// node,
360+
// identifierSym.getSimpleName(),
361+
// Role.REFERENCE,
362+
// CompilerRange.FROM_TEXT_SEARCH);
363+
// }
364+
// }
365+
// }
366+
}
367+
368+
340369
private void resolveNewClassTree(NewClassTree node, TreePath treePath) {
341370
// ignore anonymous classes - otherwise there will be a local reference to itself
342371
if (node.getIdentifier() != null && node.getClassBody() == null) {

tests/minimized/src/main/java/minimized/AnnotationParameters.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ interface Foo {
2525

2626
@Nullable(("what"))
2727
Foo test4();
28+
29+
@Bar((double) -1)
30+
double test();
2831
}

0 commit comments

Comments
 (0)