move jet signature read/write utils into stdlib module

This commit is contained in:
Stepan Koltsov
2011-12-14 19:07:49 +04:00
committed by Nikolay Krasko
parent 8136547c4c
commit 789e6f3fc6
7 changed files with 35 additions and 31 deletions
@@ -2,12 +2,11 @@ package org.jetbrains.jet.lang.resolve.java;
import com.google.common.collect.Lists;
import com.intellij.psi.*;
import jet.typeinfo.internal.signature.JetSignatureReader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.resolve.java.signature.JetSignatureReader;
import org.jetbrains.jet.lang.resolve.java.signature.JetSignatureVisitor;
import org.jetbrains.jet.lang.types.*;
import java.util.Collections;
@@ -1,11 +1,10 @@
package org.jetbrains.jet.lang.resolve.java;
import jet.typeinfo.TypeInfoVariance;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.java.signature.JetSignatureReader;
import org.jetbrains.jet.lang.resolve.java.signature.JetSignatureVisitor;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import jet.typeinfo.internal.signature.JetSignatureVisitor;
import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetStandardClasses;
import org.jetbrains.jet.lang.types.JetStandardLibrary;
@@ -34,7 +33,7 @@ public abstract class JetTypeJetSignatureReader implements JetSignatureVisitor {
@Override
public void visitFormalTypeParameter(String name, Variance variance) {
public void visitFormalTypeParameter(String name, TypeInfoVariance variance) {
throw new IllegalStateException();
}
@@ -1,81 +0,0 @@
package org.jetbrains.jet.lang.resolve.java.signature;
import org.jetbrains.jet.lang.types.Variance;
/**
* @author Stepan Koltsov
*/
public class JetSignatureAdapter implements JetSignatureVisitor {
@Override
public void visitFormalTypeParameter(String name, Variance variance) {
}
@Override
public JetSignatureVisitor visitClassBound() {
return this;
}
@Override
public JetSignatureVisitor visitInterfaceBound() {
return this;
}
@Override
public JetSignatureVisitor visitSuperclass() {
return this;
}
@Override
public JetSignatureVisitor visitInterface() {
return this;
}
@Override
public JetSignatureVisitor visitParameterType() {
return this;
}
@Override
public JetSignatureVisitor visitReturnType() {
return this;
}
@Override
public JetSignatureVisitor visitExceptionType() {
return this;
}
@Override
public void visitBaseType(char descriptor, boolean nullable) {
}
@Override
public void visitTypeVariable(String name, boolean nullable) {
}
@Override
public JetSignatureVisitor visitArrayType(boolean nullable) {
return this;
}
@Override
public void visitClassType(String name, boolean nullable) {
}
@Override
public void visitInnerClassType(String name, boolean nullable) {
}
@Override
public void visitTypeArgument() {
}
@Override
public JetSignatureVisitor visitTypeArgument(char wildcard) {
return this;
}
@Override
public void visitEnd() {
}
}
@@ -1,184 +0,0 @@
package org.jetbrains.jet.lang.resolve.java.signature;
import org.jetbrains.jet.lang.types.Variance;
import org.objectweb.asm.signature.SignatureReader;
import org.objectweb.asm.signature.SignatureVisitor;
/**
* @author Stepan Koltsov
*
* @see SignatureReader
*/
public class JetSignatureReader {
private final String signature;
public JetSignatureReader(String signature) {
this.signature = signature;
}
public void accept(final JetSignatureVisitor v) {
String signature = this.signature;
int len = signature.length();
int pos;
char c;
if (signature.charAt(0) == '<') {
pos = 2;
do {
Variance variance;
if (signature.substring(pos).startsWith("in ")) {
variance = Variance.IN_VARIANCE;
pos += "in ".length();
} else if (signature.substring(pos).startsWith("out ")) {
variance = Variance.OUT_VARIANCE;
pos += "out ".length();
} else {
variance = Variance.INVARIANT;
pos += "".length();
}
int end = signature.indexOf(':', pos);
v.visitFormalTypeParameter(signature.substring(pos - 1, end), variance);
pos = end + 1;
c = signature.charAt(pos);
if (c == 'L' || c == '[' || c == 'T') {
pos = parseType(signature, pos, v.visitClassBound());
}
while ((c = signature.charAt(pos++)) == ':') {
pos = parseType(signature, pos, v.visitInterfaceBound());
}
} while (c != '>');
} else {
pos = 0;
}
if (signature.charAt(pos) == '(') {
pos++;
while (signature.charAt(pos) != ')') {
pos = parseType(signature, pos, v.visitParameterType());
}
pos = parseType(signature, pos + 1, v.visitReturnType());
while (pos < len) {
pos = parseType(signature, pos + 1, v.visitExceptionType());
}
} else {
pos = parseType(signature, pos, v.visitSuperclass());
while (pos < len) {
pos = parseType(signature, pos, v.visitInterface());
}
}
}
public int acceptType(JetSignatureVisitor v) {
return parseType(this.signature, 0, v);
}
public void acceptTypeOnly(JetSignatureVisitor v) {
int r = acceptType(v);
if (r != signature.length()) {
throw new IllegalStateException();
}
}
private static int parseType(
final String signature,
int pos,
final JetSignatureVisitor v)
{
char c;
int start, end;
boolean visited, inner;
String name;
boolean nullable = false;
if (signature.charAt(pos) == '?') {
nullable = true;
pos++;
}
switch (c = signature.charAt(pos++)) {
case 'Z':
case 'C':
case 'B':
case 'S':
case 'I':
case 'F':
case 'J':
case 'D':
case 'V':
v.visitBaseType(c, nullable);
return pos;
case '[':
return parseType(signature, pos, v.visitArrayType(nullable));
case 'T':
end = signature.indexOf(';', pos);
v.visitTypeVariable(signature.substring(pos, end), nullable);
return end + 1;
default: // case 'L':
start = pos;
visited = false;
inner = false;
for (;;) {
switch (c = signature.charAt(pos++)) {
case '.':
case ';':
if (!visited) {
name = signature.substring(start, pos - 1);
if (inner) {
v.visitInnerClassType(name, nullable);
} else {
v.visitClassType(name, nullable);
}
}
if (c == ';') {
v.visitEnd();
return pos;
}
start = pos;
visited = false;
inner = true;
break;
case '<':
name = signature.substring(start, pos - 1);
if (inner) {
v.visitInnerClassType(name, nullable);
} else {
v.visitClassType(name, nullable);
}
visited = true;
top: for (;;) {
switch (c = signature.charAt(pos)) {
case '>':
break top;
case '*':
++pos;
v.visitTypeArgument();
break;
case '+':
case '-':
pos = parseType(signature,
pos + 1,
v.visitTypeArgument(c));
break;
default:
pos = parseType(signature,
pos,
v.visitTypeArgument('='));
break;
}
}
}
}
}
}
}
@@ -1,142 +0,0 @@
package org.jetbrains.jet.lang.resolve.java.signature;
import org.jetbrains.jet.lang.types.Variance;
import org.objectweb.asm.signature.SignatureVisitor;
/**
* @author Stepan Koltsov
*
* @see SignatureVisitor
* @url http://confluence.jetbrains.net/display/JET/Jet+Signatures
*/
public interface JetSignatureVisitor {
/**
* Wildcard for an "extends" type argument.
*/
char EXTENDS = '+';
/**
* Wildcard for a "super" type argument.
*/
char SUPER = '-';
/**
* Wildcard for a normal type argument.
*/
char INSTANCEOF = '=';
/**
* Visits a formal type parameter.
*
* @param name the name of the formal parameter.
*/
void visitFormalTypeParameter(String name, Variance variance);
/**
* Visits the class bound of the last visited formal type parameter.
*
* @return a non null visitor to visit the signature of the class bound.
*/
JetSignatureVisitor visitClassBound();
/**
* Visits an interface bound of the last visited formal type parameter.
*
* @return a non null visitor to visit the signature of the interface bound.
*/
JetSignatureVisitor visitInterfaceBound();
/**
* Visits the type of the super class.
*
* @return a non null visitor to visit the signature of the super class
* type.
*/
JetSignatureVisitor visitSuperclass();
/**
* Visits the type of an interface implemented by the class.
*
* @return a non null visitor to visit the signature of the interface type.
*/
JetSignatureVisitor visitInterface();
/**
* Visits the type of a method parameter.
*
* @return a non null visitor to visit the signature of the parameter type.
*/
JetSignatureVisitor visitParameterType();
/**
* Visits the return type of the method.
*
* @return a non null visitor to visit the signature of the return type.
*/
JetSignatureVisitor visitReturnType();
/**
* Visits the type of a method exception.
*
* @return a non null visitor to visit the signature of the exception type.
*/
JetSignatureVisitor visitExceptionType();
/**
* Visits a signature corresponding to a primitive type.
*
* @param descriptor the descriptor of the primitive type, or 'V' for
* <tt>void</tt>.
*/
void visitBaseType(char descriptor, boolean nullable);
/**
* Visits a signature corresponding to a type variable.
*
* @param name the name of the type variable.
*/
void visitTypeVariable(String name, boolean nullable);
/**
* Visits a signature corresponding to an array type.
*
* @return a non null visitor to visit the signature of the array element
* type.
*/
JetSignatureVisitor visitArrayType(boolean nullable);
/**
* Starts the visit of a signature corresponding to a class or interface
* type.
*
* @param name the internal name of the class or interface.
*/
void visitClassType(String name, boolean nullable);
/**
* Visits an inner class.
*
* @param name the local name of the inner class in its enclosing class.
*/
void visitInnerClassType(String name, boolean nullable);
/**
* Visits an unbounded type argument of the last visited class or inner
* class type.
*/
void visitTypeArgument();
/**
* Visits a type argument of the last visited class or inner class type.
*
* @param wildcard '+', '-' or '='.
* @return a non null visitor to visit the signature of the type argument.
*/
JetSignatureVisitor visitTypeArgument(char wildcard);
/**
* Ends the visit of a signature corresponding to a class or interface type.
*/
void visitEnd();
}
@@ -1,214 +0,0 @@
package org.jetbrains.jet.lang.resolve.java.signature;
import org.jetbrains.jet.lang.types.Variance;
import org.objectweb.asm.signature.SignatureWriter;
/**
* @author Stepan Koltsov
*
* @see SignatureWriter
*/
public class JetSignatureWriter implements JetSignatureVisitor {
/**
* Buffer used to construct the signature.
*/
private final StringBuffer buf = new StringBuffer();
/**
* Indicates if the signature contains formal type parameters.
*/
private boolean hasFormals;
/**
* Indicates if the signature contains method parameter types.
*/
private boolean hasParameters;
/**
* Stack used to keep track of class types that have arguments. Each element
* of this stack is a boolean encoded in one bit. The top of the stack is
* the lowest order bit. Pushing false = *2, pushing true = *2+1, popping =
* /2.
*/
private int argumentStack;
/**
* Constructs a new {@link SignatureWriter} object.
*/
public JetSignatureWriter() {
}
// ------------------------------------------------------------------------
// Implementation of the SignatureVisitor interface
// ------------------------------------------------------------------------
@Override
public void visitFormalTypeParameter(final String name, Variance variance) {
if (!hasFormals) {
hasFormals = true;
buf.append('<');
}
switch (variance) {
case OUT_VARIANCE:
buf.append("out ");
break;
case IN_VARIANCE:
buf.append("in ");
break;
case INVARIANT:
break;
default:
throw new IllegalStateException();
}
buf.append(name);
buf.append(':');
}
@Override
public JetSignatureWriter visitClassBound() {
return this;
}
@Override
public JetSignatureWriter visitInterfaceBound() {
buf.append(':');
return this;
}
@Override
public JetSignatureWriter visitSuperclass() {
endFormals();
return this;
}
@Override
public JetSignatureWriter visitInterface() {
return this;
}
@Override
public JetSignatureWriter visitParameterType() {
endFormals();
if (!hasParameters) {
hasParameters = true;
buf.append('(');
}
return this;
}
@Override
public JetSignatureWriter visitReturnType() {
endFormals();
if (!hasParameters) {
buf.append('(');
}
buf.append(')');
return this;
}
@Override
public JetSignatureWriter visitExceptionType() {
buf.append('^');
return this;
}
private void visitNullabe(boolean nullable) {
if (nullable) {
buf.append('?');
}
}
@Override
public void visitBaseType(final char descriptor, boolean nullable) {
visitNullabe(nullable);
buf.append(descriptor);
}
@Override
public void visitTypeVariable(final String name, boolean nullable) {
visitNullabe(nullable);
buf.append('T');
buf.append(name);
buf.append(';');
}
@Override
public JetSignatureWriter visitArrayType(boolean nullable) {
visitNullabe(nullable);
buf.append('[');
return this;
}
@Override
public void visitClassType(final String name, boolean nullable) {
visitNullabe(nullable);
buf.append('L');
buf.append(name);
argumentStack *= 2;
}
@Override
public void visitInnerClassType(final String name, boolean nullable) {
endArguments();
visitNullabe(nullable);
buf.append('.');
buf.append(name);
argumentStack *= 2;
}
@Override
public void visitTypeArgument() {
if (argumentStack % 2 == 0) {
++argumentStack;
buf.append('<');
}
buf.append('*');
}
@Override
public JetSignatureWriter visitTypeArgument(final char wildcard) {
if (argumentStack % 2 == 0) {
++argumentStack;
buf.append('<');
}
if (wildcard != '=') {
buf.append(wildcard);
}
return this;
}
@Override
public void visitEnd() {
endArguments();
buf.append(';');
}
public String toString() {
return buf.toString();
}
// ------------------------------------------------------------------------
// Utility methods
// ------------------------------------------------------------------------
/**
* Ends the formal type parameters section of the signature.
*/
private void endFormals() {
if (hasFormals) {
hasFormals = false;
buf.append('>');
}
}
/**
* Ends the type arguments of a class or inner class type.
*/
private void endArguments() {
if (argumentStack % 2 != 0) {
buf.append('>');
}
argumentStack /= 2;
}
}