|
| 1 | +package com.sourcegraph.semanticdb_javac; |
| 2 | + |
| 3 | +import com.sun.tools.javac.code.Symbol; |
| 4 | +import com.sun.tools.javac.code.Type; |
| 5 | +import com.sourcegraph.semanticdb_javac.Semanticdb.*; |
| 6 | +import com.sun.tools.javac.util.List; |
| 7 | + |
| 8 | +import javax.lang.model.element.Element; |
| 9 | +import javax.lang.model.element.ElementKind; |
| 10 | +import javax.lang.model.type.*; |
| 11 | +import javax.lang.model.type.IntersectionType; |
| 12 | +import javax.lang.model.util.SimpleTypeVisitor8; |
| 13 | +import java.util.ArrayList; |
| 14 | + |
| 15 | +import static com.sourcegraph.semanticdb_javac.Debugging.pprint; |
| 16 | + |
| 17 | +public final class SemanticdbSignatures { |
| 18 | + private final GlobalSymbolsCache cache; |
| 19 | + private final LocalSymbolsCache locals; |
| 20 | + |
| 21 | + public SemanticdbSignatures(GlobalSymbolsCache cache, LocalSymbolsCache locals) { |
| 22 | + this.cache = cache; |
| 23 | + this.locals = locals; |
| 24 | + } |
| 25 | + |
| 26 | + public Signature generateSignature(Symbol sym) { |
| 27 | + if (sym instanceof Symbol.ClassSymbol) { |
| 28 | + return generateClassSignature((Symbol.ClassSymbol) sym); |
| 29 | + } else if (sym instanceof Symbol.MethodSymbol) { |
| 30 | + return generateMethodSignature((Symbol.MethodSymbol) sym); |
| 31 | + } else if (sym instanceof Symbol.VarSymbol) { |
| 32 | + return generateFieldSignature((Symbol.VarSymbol) sym); |
| 33 | + } else if (sym instanceof Symbol.TypeVariableSymbol) { |
| 34 | + return generateTypeSignature((Symbol.TypeVariableSymbol) sym); |
| 35 | + } |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + private Signature generateClassSignature(Symbol.ClassSymbol sym) { |
| 40 | + ClassSignature.Builder builder = ClassSignature.newBuilder(); |
| 41 | + |
| 42 | + builder.setTypeParameters(generateScope(sym.getTypeParameters())); |
| 43 | + |
| 44 | + if (sym.getSuperclass() != Type.noType) { |
| 45 | + builder.addParents(generateType(sym.getSuperclass())); |
| 46 | + } |
| 47 | + for (Type iType : sym.getInterfaces()) { |
| 48 | + builder.addParents(generateType(iType)); |
| 49 | + } |
| 50 | + |
| 51 | + Scope.Builder declarations = Scope.newBuilder(); |
| 52 | + for (Symbol enclosed : sym.getEnclosedElements()) { |
| 53 | + declarations.addSymlinks(cache.semanticdbSymbol(enclosed, locals)); |
| 54 | + } |
| 55 | + builder.setDeclarations(declarations); |
| 56 | + |
| 57 | + return Signature.newBuilder().setClassSignature(builder).build(); |
| 58 | + } |
| 59 | + |
| 60 | + private Signature generateMethodSignature(Symbol.MethodSymbol sym) { |
| 61 | + MethodSignature.Builder builder = MethodSignature.newBuilder(); |
| 62 | + |
| 63 | + builder.setTypeParameters(generateScope(sym.getTypeParameters())); |
| 64 | + |
| 65 | + builder.addParameterLists(generateScope(sym.params())); |
| 66 | + |
| 67 | + Semanticdb.Type returnType = generateType(sym.getReturnType()); |
| 68 | + if (returnType != null) { |
| 69 | + builder.setReturnType(returnType); |
| 70 | + } |
| 71 | + |
| 72 | + return Signature.newBuilder().setMethodSignature(builder).build(); |
| 73 | + } |
| 74 | + |
| 75 | + private Signature generateFieldSignature(Symbol.VarSymbol sym) { |
| 76 | + if (generateType(sym.type) == null) { |
| 77 | + // pprint(generateType(sym.type) + " " + sym.type + " " + cache.semanticdbSymbol(sym, |
| 78 | + // locals)); |
| 79 | + } |
| 80 | + return Signature.newBuilder() |
| 81 | + .setValueSignature(ValueSignature.newBuilder().setTpe(generateType(sym.type))) |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | + private Signature generateTypeSignature(Symbol.TypeVariableSymbol sym) { |
| 86 | + TypeSignature.Builder builder = TypeSignature.newBuilder(); |
| 87 | + |
| 88 | + pprint(sym.type.getKind() + " " + sym); |
| 89 | + |
| 90 | + builder.setTypeParameters(generateScope(sym.getTypeParameters())); |
| 91 | + |
| 92 | + builder.setUpperBound(generateType(sym.type.getUpperBound())); |
| 93 | + |
| 94 | + return Signature.newBuilder().setTypeSignature(builder).build(); |
| 95 | + } |
| 96 | + |
| 97 | + private <T extends Element> Scope generateScope(List<T> elements) { |
| 98 | + Scope.Builder scope = Scope.newBuilder(); |
| 99 | + for (T typeVar : elements) { |
| 100 | + String s = cache.semanticdbSymbol(typeVar, locals); |
| 101 | + scope.addSymlinks(s); |
| 102 | + } |
| 103 | + return scope.build(); |
| 104 | + } |
| 105 | + |
| 106 | + private Semanticdb.Type generateType(TypeMirror mirror) { |
| 107 | + return mirror.accept(new SemanticdbTypeVisitor(cache, locals), null); |
| 108 | + } |
| 109 | + |
| 110 | + /** A TypeMirror tree visitor that constructs a recursive SemanticDB Type structure. */ |
| 111 | + private static class SemanticdbTypeVisitor extends SimpleTypeVisitor8<Semanticdb.Type, Void> { |
| 112 | + private final GlobalSymbolsCache cache; |
| 113 | + private final LocalSymbolsCache locals; |
| 114 | + |
| 115 | + private SemanticdbTypeVisitor(GlobalSymbolsCache cache, LocalSymbolsCache locals) { |
| 116 | + this.cache = cache; |
| 117 | + this.locals = locals; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Semanticdb.Type visitDeclared(DeclaredType t, Void unused) { |
| 122 | + boolean isExistential = |
| 123 | + t.getTypeArguments().stream().anyMatch((type) -> type instanceof WildcardType); |
| 124 | + |
| 125 | + ArrayList<Semanticdb.Type> typeParams = new ArrayList<>(); |
| 126 | + for (TypeMirror type : t.getTypeArguments()) { |
| 127 | + Semanticdb.Type visit = super.visit(type); |
| 128 | + if (visit == null) { |
| 129 | + pprint(type + " " + visit + " " + type.getClass()); |
| 130 | + continue; |
| 131 | + } |
| 132 | + typeParams.add(visit); |
| 133 | + } |
| 134 | + |
| 135 | + if (!isExistential) { |
| 136 | + return Semanticdb.Type.newBuilder() |
| 137 | + .setTypeRef( |
| 138 | + TypeRef.newBuilder() |
| 139 | + .setSymbol(cache.semanticdbSymbol(t.asElement(), locals)) |
| 140 | + .addAllTypeArguments(typeParams)) |
| 141 | + .build(); |
| 142 | + } else { |
| 143 | + return Semanticdb.Type.newBuilder() |
| 144 | + .setExistentialType( |
| 145 | + ExistentialType.newBuilder() |
| 146 | + .setTpe( |
| 147 | + Semanticdb.Type.newBuilder() |
| 148 | + .setTypeRef( |
| 149 | + TypeRef.newBuilder() |
| 150 | + .setSymbol(cache.semanticdbSymbol(t.asElement(), locals)) |
| 151 | + .addAllTypeArguments(typeParams)))) |
| 152 | + .build(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public Semanticdb.Type visitArray(ArrayType t, Void unused) { |
| 158 | + return Semanticdb.Type.newBuilder() |
| 159 | + .setTypeRef( |
| 160 | + TypeRef.newBuilder() |
| 161 | + .setSymbol("scala/Array#") |
| 162 | + .addTypeArguments(super.visit(t.getComponentType()))) |
| 163 | + .build(); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Semanticdb.Type visitPrimitive(PrimitiveType t, Void unused) { |
| 168 | + return Semanticdb.Type.newBuilder() |
| 169 | + .setTypeRef(TypeRef.newBuilder().setSymbol(primitiveSymbol(t.getKind()))) |
| 170 | + .build(); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public Semanticdb.Type visitTypeVariable(TypeVariable t, Void unused) { |
| 175 | + return Semanticdb.Type.newBuilder() |
| 176 | + .setTypeRef( |
| 177 | + TypeRef.newBuilder().setSymbol(cache.semanticdbSymbol(t.asElement(), locals)).build()) |
| 178 | + .build(); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public Semanticdb.Type visitIntersection(IntersectionType t, Void unused) { |
| 183 | + ArrayList<Semanticdb.Type> types = new ArrayList<>(); |
| 184 | + for (TypeMirror type : t.getBounds()) { |
| 185 | + types.add(super.visit(type)); |
| 186 | + } |
| 187 | + |
| 188 | + return Semanticdb.Type.newBuilder() |
| 189 | + .setIntersectionType(Semanticdb.IntersectionType.newBuilder().addAllTypes(types).build()) |
| 190 | + .build(); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public Semanticdb.Type visitWildcard(WildcardType t, Void unused) { |
| 195 | + new Exception().printStackTrace(); |
| 196 | + return Semanticdb.Type.newBuilder() |
| 197 | + .setExistentialType(ExistentialType.newBuilder().build()) |
| 198 | + .build(); |
| 199 | + } |
| 200 | + |
| 201 | + public String primitiveSymbol(TypeKind kind) { |
| 202 | + switch (kind) { |
| 203 | + case BOOLEAN: |
| 204 | + return "scala/Boolean#"; |
| 205 | + case BYTE: |
| 206 | + return "scala/Byte#"; |
| 207 | + case SHORT: |
| 208 | + return "scala/Short#"; |
| 209 | + case INT: |
| 210 | + return "scala/Int#"; |
| 211 | + case LONG: |
| 212 | + return "scala/Long#"; |
| 213 | + case CHAR: |
| 214 | + return "scala/Char#"; |
| 215 | + case FLOAT: |
| 216 | + return "scala/Float#"; |
| 217 | + case DOUBLE: |
| 218 | + return "scala/Double#"; |
| 219 | + case VOID: |
| 220 | + return "scala/Unit#"; |
| 221 | + default: |
| 222 | + throw new IllegalArgumentException("got " + kind.name()); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments