Skip to content

Commit

Permalink
line numbers for undefined ids & more
Browse files Browse the repository at this point in the history
  • Loading branch information
akuznetsov committed Dec 29, 2023
1 parent 0824a7a commit 9777bae
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 177 deletions.
8 changes: 4 additions & 4 deletions src/main/javassist/compiler/CodeGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected abstract String resolveClassName(ASTList name)
/* Expands a simple class name to java.lang.*.
* For example, this converts Object into java/lang/Object.
*/
protected abstract String resolveClassName(String jvmClassName)
protected abstract String resolveClassName(String jvmClassName, int lineNumber)
throws CompileError;

/**
Expand Down Expand Up @@ -802,7 +802,7 @@ private static boolean isPlusPlusExpr(ASTree expr) {
@Override
public void atDeclarator(Declarator d) throws CompileError {
d.setLocalVar(getMaxLocals());
d.setClassName(resolveClassName(d.getClassName()));
d.setClassName(resolveClassName(d.getClassName(), d.getLineNumber()));

int size;
if (is2word(d.getType(), d.getArrayDim()))
Expand Down Expand Up @@ -1656,7 +1656,7 @@ public void atClassObject(Expr expr) throws CompileError {
int i = cname.indexOf("[L");
if (i >= 0) {
String name = cname.substring(i + 2, cname.length() - 1);
String name2 = resolveClassName(name);
String name2 = resolveClassName(name, expr.getLineNumber());
if (!name.equals(name2)) {
/* For example, to obtain String[].class,
* "[Ljava.lang.String;" (not "[Ljava/lang/String"!)
Expand All @@ -1673,7 +1673,7 @@ public void atClassObject(Expr expr) throws CompileError {
}
}
else {
cname = resolveClassName(MemberResolver.javaToJvmName(cname));
cname = resolveClassName(MemberResolver.javaToJvmName(cname), expr.getLineNumber());
cname = MemberResolver.jvmToJavaName(cname);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/javassist/compiler/CompileError.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CompileError(String s, int lineNumber) {
reason = String.format("line %d: %s", lineNumber, s);
}

public CompileError(String s) {
private CompileError(String s) {
reason = s;
lex = null;
}
Expand Down
28 changes: 13 additions & 15 deletions src/main/javassist/compiler/Javac.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public Javac(Bytecode b, CtClass thisClass) {
*/
public CtMember compile(String src) throws CompileError {
int startLine = gen.thisClass.getLinesCount();
Parser p = new Parser(new Lex(src, startLine));
Lex lex = new Lex(src, startLine);
Parser p = new Parser(lex);
ASTList mem = p.parseMember1(stable);
try {
if (mem instanceof FieldDecl)
Expand All @@ -107,11 +108,8 @@ public CtMember compile(String src) throws CompileError {
decl.getClassFile2());
return cb;
}
catch (BadBytecode bb) {
throw new CompileError(bb.getMessage());
}
catch (CannotCompileException e) {
throw new CompileError(e.getMessage());
catch (BadBytecode | CannotCompileException bb) {
throw new CompileError(bb.getMessage(), lex.getLineNumber());
}
}

Expand Down Expand Up @@ -186,7 +184,7 @@ private CtBehavior compileMethod(Parser p, MethodDecl md)
return method;
}
catch (NotFoundException e) {
throw new CompileError(e.toString());
throw new CompileError(e.toString(), md.getLineNumber());
}
}

Expand Down Expand Up @@ -222,7 +220,7 @@ public Bytecode compileBody(CtBehavior method, String src)
Stmnt s = p.parseStatement(stb);
if (p.hasMore())
throw new CompileError(
"the method/constructor body must be surrounded by {}");
"the method/constructor body must be surrounded by {}", s.getLineNumber());

boolean callSuper = false;
if (method instanceof CtConstructor)
Expand All @@ -234,7 +232,7 @@ public Bytecode compileBody(CtBehavior method, String src)
return bytecode;
}
catch (NotFoundException e) {
throw new CompileError(e.toString());
throw new CompileError(e.toString(), -1);
}
}

Expand Down Expand Up @@ -446,7 +444,7 @@ public void recordProceed(String target, String method)

ProceedHandler h = new ProceedHandler() {
@Override
public void doit(JvstCodeGen gen, Bytecode b, ASTList args)
public void doit(JvstCodeGen gen, Bytecode b, ASTList args, int lineNumber)
throws CompileError
{
ASTree expr = new Member(m, texpr.getLineNumber());
Expand All @@ -459,7 +457,7 @@ public void doit(JvstCodeGen gen, Bytecode b, ASTList args)
}

@Override
public void setReturnType(JvstTypeChecker check, ASTList args)
public void setReturnType(JvstTypeChecker check, ASTList args, int lineNumber)
throws CompileError
{
ASTree expr = new Member(m, texpr.getLineNumber());
Expand Down Expand Up @@ -492,7 +490,7 @@ public void recordStaticProceed(String targetClass, String method)

ProceedHandler h = new ProceedHandler() {
@Override
public void doit(JvstCodeGen gen, Bytecode b, ASTList args)
public void doit(JvstCodeGen gen, Bytecode b, ASTList args, int lineNumber)
throws CompileError
{
Expr expr = Expr.make(TokenId.MEMBER,
Expand All @@ -503,7 +501,7 @@ public void doit(JvstCodeGen gen, Bytecode b, ASTList args)
}

@Override
public void setReturnType(JvstTypeChecker check, ASTList args)
public void setReturnType(JvstTypeChecker check, ASTList args, int lineNumber)
throws CompileError
{
Expr expr = Expr.make(TokenId.MEMBER,
Expand Down Expand Up @@ -538,14 +536,14 @@ public void recordSpecialProceed(String target, final String classname,

ProceedHandler h = new ProceedHandler() {
@Override
public void doit(JvstCodeGen gen, Bytecode b, ASTList args)
public void doit(JvstCodeGen gen, Bytecode b, ASTList args, int lineNumber)
throws CompileError
{
gen.compileInvokeSpecial(texpr, methodIndex, descriptor, args);
}

@Override
public void setReturnType(JvstTypeChecker c, ASTList args)
public void setReturnType(JvstTypeChecker c, ASTList args, int lineNumber)
throws CompileError
{
c.compileInvokeSpecial(texpr, classname, methodname, descriptor, args);
Expand Down
46 changes: 23 additions & 23 deletions src/main/javassist/compiler/JvstCodeGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ protected void atFieldAssign(Expr expr, int op, ASTree left,
if (arrayDim != 1 || exprType != CLASS)
throw new CompileError("invalid type for " + paramArrayName, expr.getLineNumber());

atAssignParamList(paramTypeList, bytecode);
atAssignParamList(paramTypeList, bytecode, expr.getLineNumber());
if (!doDup)
bytecode.addOpcode(POP);
}
else
super.atFieldAssign(expr, op, left, right, doDup);
}

protected void atAssignParamList(CtClass[] params, Bytecode code)
protected void atAssignParamList(CtClass[] params, Bytecode code, int lineNumber)
throws CompileError
{
if (params == null)
Expand All @@ -167,7 +167,7 @@ protected void atAssignParamList(CtClass[] params, Bytecode code)
code.addOpcode(DUP);
code.addIconst(i);
code.addOpcode(AALOAD);
compileUnwrapValue(params[i], code);
compileUnwrapValue(params[i], code, lineNumber);
code.addStore(varNo, params[i]);
varNo += is2word(exprType, arrayDim) ? 2 : 1;
}
Expand Down Expand Up @@ -201,10 +201,10 @@ else if (typename.equals(wrapperCastName)) {
protected void atCastToRtype(CastExpr expr) throws CompileError {
expr.getOprand().accept(this);
if (exprType == VOID || isRefType(exprType) || arrayDim > 0)
compileUnwrapValue(returnType, bytecode);
compileUnwrapValue(returnType, bytecode, expr.getLineNumber());
else if (returnType instanceof CtPrimitiveType) {
CtPrimitiveType pt = (CtPrimitiveType)returnType;
int destType = MemberResolver.descToType(pt.getDescriptor());
int destType = MemberResolver.descToType(pt.getDescriptor(), expr.getLineNumber());
atNumCastExpr(exprType, destType);
exprType = destType;
arrayDim = 0;
Expand All @@ -219,7 +219,7 @@ protected void atCastToWrapper(CastExpr expr) throws CompileError {
if (isRefType(exprType) || arrayDim > 0)
return; // Object type. do nothing.

CtClass clazz = resolver.lookupClass(exprType, arrayDim, className);
CtClass clazz = resolver.lookupClass(exprType, arrayDim, className, expr.getLineNumber());
if (clazz instanceof CtPrimitiveType) {
CtPrimitiveType pt = (CtPrimitiveType)clazz;
String wrapper = pt.getWrapperName();
Expand Down Expand Up @@ -249,7 +249,7 @@ public void atCallExpr(CallExpr expr) throws CompileError {
if (method instanceof Member) {
String name = ((Member)method).get();
if (procHandler != null && name.equals(proceedName)) {
procHandler.doit(this, bytecode, (ASTList)expr.oprand2());
procHandler.doit(this, bytecode, (ASTList)expr.oprand2(), expr.getLineNumber());
return;
}
else if (name.equals(cflowName)) {
Expand Down Expand Up @@ -364,7 +364,7 @@ public void atMethodArgs(ASTList args, int[] types, int[] dims,
for (int k = 0; k < n; ++k) {
CtClass p = params[k];
regno += bytecode.addLoad(regno, p);
setType(p);
setType(p, a.getLineNumber());
types[i] = exprType;
dims[i] = arrayDim;
cnames[i] = className;
Expand Down Expand Up @@ -420,7 +420,7 @@ void compileInvokeSpecial(ASTree target, int methodIndex,
atMethodArgs(args, new int[nargs], new int[nargs],
new String[nargs]);
bytecode.addInvokespecial(methodIndex, descriptor);
setReturnType(descriptor, false, false);
setReturnType(descriptor, false, false, target.getLineNumber());
addNullIfVoid();
}

Expand Down Expand Up @@ -569,7 +569,7 @@ private int recordVar(CtClass cc, String varName, int varNo,
className = jvmJavaLangObject;
}
else
setType(cc);
setType(cc, cc.getLinesCount());

Declarator decl
= new Declarator(exprType, className, arrayDim,
Expand All @@ -593,7 +593,7 @@ public void recordVariable(String typeDesc, String varName, int varNo,
while ((c = typeDesc.charAt(dim)) == '[')
++dim;

int type = MemberResolver.descToType(c);
int type = MemberResolver.descToType(c, -1);
String cname = null;
if (type == CLASS) {
if (dim == 0)
Expand Down Expand Up @@ -653,7 +653,7 @@ public static int compileParameterList(Bytecode code,
return 8;
}

protected void compileUnwrapValue(CtClass type, Bytecode code)
protected void compileUnwrapValue(CtClass type, Bytecode code, int lineNumber)
throws CompileError
{
if (type == CtClass.voidType) {
Expand All @@ -662,7 +662,7 @@ protected void compileUnwrapValue(CtClass type, Bytecode code)
}

if (exprType == VOID)
throw new CompileError("invalid type for " + returnCastName);
throw new CompileError("invalid type for " + returnCastName, lineNumber);

if (type instanceof CtPrimitiveType) {
CtPrimitiveType pt = (CtPrimitiveType)type;
Expand All @@ -671,34 +671,34 @@ protected void compileUnwrapValue(CtClass type, Bytecode code)
code.addCheckcast(wrapper);
code.addInvokevirtual(wrapper, pt.getGetMethodName(),
pt.getGetMethodDescriptor());
setType(type);
setType(type, lineNumber);
}
else {
code.addCheckcast(type);
setType(type);
setType(type, lineNumber);
}
}

/* Sets exprType, arrayDim, and className;
* If type is void, then this method does nothing.
*/
public void setType(CtClass type) throws CompileError {
setType(type, 0);
public void setType(CtClass type, int lineNumber) throws CompileError {
setType(type, 0, lineNumber);
}

private void setType(CtClass type, int dim) throws CompileError {
private void setType(CtClass type, int dim, int lineNumber) throws CompileError {
if (type.isPrimitive()) {
CtPrimitiveType pt = (CtPrimitiveType)type;
exprType = MemberResolver.descToType(pt.getDescriptor());
exprType = MemberResolver.descToType(pt.getDescriptor(), lineNumber);
arrayDim = dim;
className = null;
}
else if (type.isArray())
try {
setType(type.getComponentType(), dim + 1);
setType(type.getComponentType(), dim + 1, lineNumber);
}
catch (NotFoundException e) {
throw new CompileError("undefined type: " + type.getName());
throw new CompileError("undefined type: " + type.getName(), lineNumber);
}
else {
exprType = CLASS;
Expand All @@ -714,9 +714,9 @@ public void doNumCast(CtClass type) throws CompileError {
if (type instanceof CtPrimitiveType) {
CtPrimitiveType pt = (CtPrimitiveType)type;
atNumCastExpr(exprType,
MemberResolver.descToType(pt.getDescriptor()));
MemberResolver.descToType(pt.getDescriptor(), type.getLinesCount() - 1));
}
else
throw new CompileError("type mismatch");
throw new CompileError("type mismatch", type.getLinesCount() - 1);
}
}
24 changes: 12 additions & 12 deletions src/main/javassist/compiler/JvstTypeChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected void atCastToRtype(CastExpr expr) throws CompileError {
compileUnwrapValue(returnType, expr.getLineNumber());
else if (returnType instanceof CtPrimitiveType) {
CtPrimitiveType pt = (CtPrimitiveType)returnType;
int destType = MemberResolver.descToType(pt.getDescriptor());
int destType = MemberResolver.descToType(pt.getDescriptor(), expr.getLineNumber());
exprType = destType;
arrayDim = 0;
className = null;
Expand All @@ -139,7 +139,7 @@ protected void atCastToWrapper(CastExpr expr) throws CompileError {
if (CodeGen.isRefType(exprType) || arrayDim > 0)
return; // Object type. do nothing.

CtClass clazz = resolver.lookupClass(exprType, arrayDim, className);
CtClass clazz = resolver.lookupClass(exprType, arrayDim, className, expr.getLineNumber());
if (clazz instanceof CtPrimitiveType) {
exprType = CLASS;
arrayDim = 0;
Expand All @@ -158,7 +158,7 @@ public void atCallExpr(CallExpr expr) throws CompileError {
if (codeGen.procHandler != null
&& name.equals(codeGen.proceedName)) {
codeGen.procHandler.setReturnType(this,
(ASTList)expr.oprand2());
(ASTList)expr.oprand2(), expr.getLineNumber());
return;
}
else if (name.equals(JvstCodeGen.cflowName)) {
Expand Down Expand Up @@ -223,7 +223,7 @@ public void atMethodArgs(ASTList args, int[] types, int[] dims,
int n = params.length;
for (int k = 0; k < n; ++k) {
CtClass p = params[k];
setType(p);
setType(p, a.getLineNumber());
types[i] = exprType;
dims[i] = arrayDim;
cnames[i] = className;
Expand Down Expand Up @@ -254,7 +254,7 @@ void compileInvokeSpecial(ASTree target, String classname,
int nargs = getMethodArgsLength(args);
atMethodArgs(args, new int[nargs], new int[nargs],
new String[nargs]);
setReturnType(descriptor);
setReturnType(descriptor, target.getLineNumber());
addNullIfVoid();
}

Expand All @@ -263,29 +263,29 @@ protected void compileUnwrapValue(CtClass type, int lineNumber) throws CompileEr
if (type == CtClass.voidType)
addNullIfVoid();
else
setType(type);
setType(type, lineNumber);
}

/* Sets exprType, arrayDim, and className;
* If type is void, then this method does nothing.
*/
public void setType(CtClass type) throws CompileError {
setType(type, 0);
public void setType(CtClass type, int lineNumber) throws CompileError {
setType(type, 0, lineNumber);
}

private void setType(CtClass type, int dim) throws CompileError {
private void setType(CtClass type, int dim, int lineNumber) throws CompileError {
if (type.isPrimitive()) {
CtPrimitiveType pt = (CtPrimitiveType)type;
exprType = MemberResolver.descToType(pt.getDescriptor());
exprType = MemberResolver.descToType(pt.getDescriptor(), lineNumber);
arrayDim = dim;
className = null;
}
else if (type.isArray())
try {
setType(type.getComponentType(), dim + 1);
setType(type.getComponentType(), dim + 1, lineNumber);
}
catch (NotFoundException e) {
throw new CompileError("undefined type: " + type.getName());
throw new CompileError("undefined type: " + type.getName(), lineNumber);
}
else {
exprType = CLASS;
Expand Down
Loading

0 comments on commit 9777bae

Please sign in to comment.