Skip to content

Commit

Permalink
(root cause) add unary tree to semanticdb and handle it
Browse files Browse the repository at this point in the history
  • Loading branch information
antonsviridov-src committed Aug 5, 2024
1 parent f8f1dc0 commit e4128b7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ public static Semanticdb.BinaryOperatorTree binopTree(
.build();
}

public static Semanticdb.Tree tree(Semanticdb.UnaryOperatorTree unaryOperatorTree) {
return Semanticdb.Tree.newBuilder().setUnaryopTree(unaryOperatorTree).build();
}

public static Semanticdb.UnaryOperatorTree unaryOpTree(
Semanticdb.UnaryOperator operator, Semanticdb.Tree rhs) {
return Semanticdb.UnaryOperatorTree.newBuilder().setOp(operator).setTree(rhs).build();
}

public static Semanticdb.Tree tree(Semanticdb.AssignTree assignTree) {
return Semanticdb.Tree.newBuilder().setAssignTree(assignTree).build();
}
Expand Down
17 changes: 17 additions & 0 deletions semanticdb-java/src/main/protobuf/semanticdb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ message Tree {
AnnotationTree annotation_tree = 9;
AssignTree assign_tree = 10;
BinaryOperatorTree binop_tree = 11;
UnaryOperatorTree unaryop_tree = 12;
// -- OUT OF SPEC -- //
}
}
Expand Down Expand Up @@ -378,6 +379,22 @@ enum BinaryOperator {
GREATER_THAN_EQUAL = 17;
LESS_THAN_EQUAL = 18;
}

message UnaryOperatorTree {
UnaryOperator op = 1;
Tree tree = 2;
}

enum UnaryOperator {
UNARY_MINUS = 0;
UNARY_PLUS = 1;
UNARY_POSTFIX_INCREMENT = 2;
UNARY_POSTFIX_DECREMENT = 3;
UNARY_PREFIX_DECREMENT = 4;
UNARY_PREFIX_INCREMENT = 5;
UNARY_BITWISE_COMPLEMENT = 6;
UNARY_LOGICAL_COMPLEMENT = 7;
}
// -- OUT OF SPEC -- //

message Constant {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -78,7 +79,8 @@ public Semanticdb.AnnotationTree annotationBuilder(AnnotationTree annotation) {
ArrayList<Semanticdb.Tree> params = new ArrayList<>(annotation.getArguments().size());

for (ExpressionTree param : annotation.getArguments()) {
// anecdotally not always AssignmentTree in some situations when a compilation unit can't
// anecdotally not always AssignmentTree in some situations when a compilation
// unit can't
// resolve symbols fully
if (param instanceof AssignmentTree) {
AssignmentTree assign = (AssignmentTree) param;
Expand Down Expand Up @@ -152,6 +154,12 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) {
annotationParameter(binExpr.getLeftOperand()),
semanticdbBinaryOperator(expr.getKind()),
annotationParameter(binExpr.getRightOperand())));
} else if (expr instanceof UnaryTree) {
UnaryTree unaryExpr = (UnaryTree) expr;
return tree(
unaryOpTree(
semanticdbUnaryOperator(unaryExpr.getKind()),
annotationParameter(unaryExpr.getExpression())));
}
throw new IllegalArgumentException(
semanticdbUri
Expand Down Expand Up @@ -206,4 +214,15 @@ private Semanticdb.BinaryOperator semanticdbBinaryOperator(Tree.Kind kind) {
semanticdbUri + ": unexpected binary expression operator kind " + kind);
}
}

private Semanticdb.UnaryOperator semanticdbUnaryOperator(Tree.Kind kind) {
switch (kind) {
case UNARY_MINUS:
return Semanticdb.UnaryOperator.UNARY_MINUS;

default:
throw new IllegalStateException(
semanticdbUri + ": unexpected unary expression operator kind " + kind);
}
}
}

0 comments on commit e4128b7

Please sign in to comment.