Skip to content

Commit

Permalink
Fix bugs on local variable declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Jul 29, 2019
1 parent 5ae9e8a commit 87ecfad
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
*/

package org.jd.core.v1.service.converter.classfiletojavasyntax.model.javasyntax.declaration;

import org.jd.core.v1.model.javasyntax.declaration.FormalParameter;
import org.jd.core.v1.model.javasyntax.reference.BaseAnnotationReference;
import org.jd.core.v1.model.javasyntax.type.Type;
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable.AbstractLocalVariable;
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable.LocalVariableReference;


public class ClassFileFormalParameter extends FormalParameter implements LocalVariableReference {
protected AbstractLocalVariable localVariable;

public ClassFileFormalParameter(AbstractLocalVariable localVariable) {
super(null, null);
this.localVariable = localVariable;
}

public ClassFileFormalParameter(AbstractLocalVariable localVariable, boolean varargs) {
super(null, varargs, null);
this.localVariable = localVariable;
}

public ClassFileFormalParameter(BaseAnnotationReference annotationReferences, AbstractLocalVariable localVariable, boolean varargs) {
super(annotationReferences, null, varargs, null);
this.localVariable = localVariable;
}

public Type getType() {
return localVariable.getType();
}

public String getName() {
return localVariable.getName();
}

public void setName(String name) {
localVariable.setName(name);
}

@Override
public AbstractLocalVariable getLocalVariable() {
return localVariable;
}

@Override
public void setLocalVariable(AbstractLocalVariable localVariable) {
this.localVariable = localVariable;
}

@Override
public String toString() {
String s = "ClassFileFormalParameter{";

if (annotationReferences != null)
s += annotationReferences + " ";

Type type = localVariable.getType();

if (varargs)
s += type.createType(type.getDimension()-1) + "... ";
else
s += type + " ";

return s + localVariable.getName() + "}";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -10,8 +10,9 @@
import org.jd.core.v1.model.javasyntax.expression.LocalVariableReferenceExpression;
import org.jd.core.v1.model.javasyntax.type.Type;
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable.AbstractLocalVariable;
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable.LocalVariableReference;

public class ClassFileLocalVariableReferenceExpression extends LocalVariableReferenceExpression {
public class ClassFileLocalVariableReferenceExpression extends LocalVariableReferenceExpression implements LocalVariableReference {
protected AbstractLocalVariable localVariable;

public ClassFileLocalVariableReferenceExpression(int lineNumber, AbstractLocalVariable localVariable) {
Expand All @@ -30,10 +31,12 @@ public String getName() {
return localVariable.getName();
}

@Override
public AbstractLocalVariable getLocalVariable() {
return localVariable;
}

@Override
public void setLocalVariable(AbstractLocalVariable localVariable) {
this.localVariable = localVariable;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -20,7 +20,7 @@ public abstract class AbstractLocalVariable implements LocalVariable {
protected int toOffset;
protected String name;
protected int dimension;
protected DefaultList<ClassFileLocalVariableReferenceExpression> references = new DefaultList<>();
protected DefaultList<LocalVariableReference> references = new DefaultList<>();

public AbstractLocalVariable(int index, int offset, int dimension) {
this.declared = (offset == 0);
Expand Down Expand Up @@ -106,9 +106,13 @@ public void setToOffset(int offset) {

@Override public String getName() { return name; }

public void setName(String name) {
this.name = name;
}

public int getDimension() { return dimension; }

public DefaultList<ClassFileLocalVariableReferenceExpression> getReferences() {
public DefaultList<LocalVariableReference> getReferences() {
return references;
}

Expand All @@ -121,8 +125,7 @@ public DefaultList<ClassFileLocalVariableReferenceExpression> getReferences() {
public abstract void rightReduce(Type otherType);
public abstract void rightReduce(AbstractLocalVariable other);

@Override
public void addReference(ClassFileLocalVariableReferenceExpression reference) {
public void addReference(LocalVariableReference reference) {
references.add(reference);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand Down Expand Up @@ -98,12 +98,34 @@ public void mergeLocalVariable(AbstractLocalVariable lv) {
}
}
} else if (lv != alvToMerge) {
for (ClassFileLocalVariableReferenceExpression reference : alvToMerge.getReferences()) {
for (LocalVariableReference reference : alvToMerge.getReferences()) {
reference.setLocalVariable(lv);
}

lv.getReferences().addAll(alvToMerge.getReferences());
lv.setFromOffset(alvToMerge.getFromOffset());

if (!lv.isAssignable(alvToMerge)) {
Type type = lv.getType();
Type alvToMergeType = alvToMerge.getType();

assert (type.isPrimitive() == alvToMergeType.isPrimitive()) && (type.isObject() == alvToMergeType.isObject()) && (type.isGeneric() == alvToMergeType.isGeneric());

if (type.isPrimitive()) {
if (alvToMerge.isAssignable(lv)) {
((PrimitiveLocalVariable)lv).setPrimitiveType((PrimitiveType)type);
} else {
((PrimitiveLocalVariable)lv).setPrimitiveType(PrimitiveType.TYPE_INT);
}
} else if (type.isObject()) {
if (alvToMerge.isAssignable(lv)) {
((ObjectLocalVariable)lv).setObjectType((ObjectType)type);
} else {
((ObjectLocalVariable)lv).setObjectType(ObjectType.TYPE_OBJECT);
}
}
}

localVariableArray[index] = alvToMerge.getNext();
}
}
Expand Down Expand Up @@ -474,8 +496,7 @@ protected void updateForStatement(
}

@SuppressWarnings("unchecked")
protected void updateForStatement(
HashSet<AbstractLocalVariable> variablesToDeclare, HashSet<AbstractLocalVariable> foundVariables, ClassFileForStatement forStatement, Expressions init) {
protected void updateForStatement(HashSet<AbstractLocalVariable> variablesToDeclare, HashSet<AbstractLocalVariable> foundVariables, ClassFileForStatement forStatement, Expressions init) {

DefaultList<BinaryOperatorExpression> boes = new DefaultList<>();
DefaultList<AbstractLocalVariable> localVariables = new DefaultList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -8,15 +8,12 @@
package org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable;

import org.jd.core.v1.model.javasyntax.type.Type;
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.javasyntax.expression.ClassFileLocalVariableReferenceExpression;


public interface LocalVariable {
Type getType();

String getName();

void addReference(ClassFileLocalVariableReferenceExpression reference);

void accept(LocalVariableVisitor visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
*/

package org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable;


public interface LocalVariableReference {
AbstractLocalVariable getLocalVariable();

void setLocalVariable(AbstractLocalVariable localVariable);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand Down Expand Up @@ -95,6 +95,23 @@ public AbstractLocalVariable remove(int index, int offset) {
return null;
}

public AbstractLocalVariable get(int index, int offset) {
if (index < array.length) {
AbstractLocalVariable lv = array[index];

while (lv != null) {
if (lv.fromOffset <= offset) {
return lv;
}

assert lv != lv.getNext();
lv = lv.getNext();
}
}

return null;
}

public boolean isEmpty() {
return size == 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public Type getType() {
return (dimension == 0) ? toType : arrayType;
}

public void setObjectType(ObjectType type) {
dimension = type.getDimension();

if (dimension == 0) {
this.fromType = this.toType = (ObjectType)type;
} else {
this.arrayType = type;
}
}

@Override
public boolean isAssignable(AbstractLocalVariable other) {
if (other.getClass() == ObjectLocalVariable.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public Type getType() {
return PrimitiveTypeUtil.getPrimitiveType(flags, dimension);
}

public void setPrimitiveType(PrimitiveType type) {
assert type.getDimension() == 0;
this.flags = type.getFlags();
}

@Override
public boolean isAssignable(AbstractLocalVariable other) {
if ((other.getDimension() == 0) && (other.getClass() == PrimitiveLocalVariable.class)) {
Expand Down
Loading

0 comments on commit 87ecfad

Please sign in to comment.