From f2fdfc0c0c20e3497ff6a4a9cc9f661e5f63e397 Mon Sep 17 00:00:00 2001 From: kpouer Date: Sun, 3 Aug 2003 14:55:39 +0000 Subject: [PATCH 1/1] Methods to get used, declared and variables coming from outside in the expressions, for code analysis. a first analysis inside methods is done, but very experimental. --- .../phpdt/internal/compiler/ast/Types.java | 1 - .../internal/compiler/ast/UnaryExpression.java | 29 ++++- .../internal/compiler/ast/VarAssignation.java | 47 +++++++- .../internal/compiler/ast/VariableDeclaration.java | 134 +++++++++++++++++--- .../internal/compiler/ast/WhileStatement.java | 66 ++++++++-- 5 files changed, 243 insertions(+), 34 deletions(-) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/Types.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/Types.java index d763ea4..1b1de19 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/Types.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/Types.java @@ -3,7 +3,6 @@ package net.sourceforge.phpdt.internal.compiler.ast; /** * The php types defined here. * @author Matthieu Casanova - * @version $Reference: 1.0$ */ public interface Types { diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/UnaryExpression.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/UnaryExpression.java index fff7021..5e50628 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/UnaryExpression.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/UnaryExpression.java @@ -1,5 +1,8 @@ package net.sourceforge.phpdt.internal.compiler.ast; +import java.util.List; +import java.util.ArrayList; + /** * @author Matthieu Casanova */ @@ -7,8 +10,32 @@ public abstract class UnaryExpression extends OperatorExpression { public Expression expression; - public UnaryExpression(Expression expression, int operator, int sourceStart, int sourceEnd) { + public UnaryExpression(final Expression expression, final int operator, final int sourceStart, final int sourceEnd) { super(operator, sourceStart, sourceEnd); this.expression = expression; } + + /** + * Get the variables from outside (parameters, globals ...) + * @return the variables from outside + */ + public List getOutsideVariable() { + return new ArrayList(); + } + + /** + * get the modified variables. + * @return the variables from we change value + */ + public List getModifiedVariable() { + return expression.getModifiedVariable(); + } + + /** + * Get the variables used. + * @return the variables used + */ + public List getUsedVariable() { + return expression.getUsedVariable(); + } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/VarAssignation.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/VarAssignation.java index ff7f4f6..5fb5cd3 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/VarAssignation.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/VarAssignation.java @@ -1,5 +1,10 @@ package net.sourceforge.phpdt.internal.compiler.ast; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; + +import java.util.List; +import java.util.ArrayList; + /** * A Variable assignation. * $varname = initializer @@ -46,7 +51,7 @@ public class VarAssignation extends Expression { /** * Return the operator as String. - * @return the operator + * @return the operator */ public final String operatorToString() { switch (operator) { @@ -85,11 +90,45 @@ public class VarAssignation extends Expression { * @return the expression */ public String toStringExpression() { - final StringBuffer buff = new StringBuffer(variableName.toStringExpression()); + final String varName = variableName.toStringExpression(); + final String init = initializer.toStringExpression(); + final String operatorString = operatorToString(); + final StringBuffer buff = new StringBuffer(varName.length() + operatorString.length() + init.length() + 2); + buff.append(varName); buff.append(" ");//$NON-NLS-1$ - buff.append(operatorToString()); + buff.append(operatorString); buff.append(" ");//$NON-NLS-1$ - buff.append(initializer.toStringExpression()); + buff.append(init); return buff.toString(); } + + + /** + * Get the variables from outside (parameters, globals ...) + * @return the variables from outside + */ + public List getOutsideVariable() { + return new ArrayList(); + } + + /** + * get the modified variables. + * @return the variables from we change value + */ + public List getModifiedVariable() { + final ArrayList list = new ArrayList(); + list.addAll(variableName.getUsedVariable()); + list.addAll(initializer.getModifiedVariable()); + return list; + } + + /** + * Get the variables used. + * @return the variables used + */ + public List getUsedVariable() { + final ArrayList list = new ArrayList(); + list.addAll(initializer.getUsedVariable()); + return list; + } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/VariableDeclaration.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/VariableDeclaration.java index 5a709fd..9fafb2f 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/VariableDeclaration.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/VariableDeclaration.java @@ -1,34 +1,57 @@ package net.sourceforge.phpdt.internal.compiler.ast; import net.sourceforge.phpdt.internal.compiler.parser.Outlineable; +import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage; import net.sourceforge.phpdt.internal.ui.PHPUiImages; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.text.Position; +import java.util.List; +import java.util.ArrayList; + /** * A variable declaration. * @author Matthieu Casanova */ public class VariableDeclaration extends AbstractVariableDeclaration implements Outlineable { + public static final int EQUAL = 0; + public static final int PLUS_EQUAL = 1; + public static final int MINUS_EQUAL = 2; + public static final int STAR_EQUAL = 3; + public static final int SLASH_EQUAL = 4; + public static final int AND_EQUAL = 5; + public static final int OR_EQUAL = 6; + public static final int XOR_EQUAL = 7; + public static final int DOT_EQUAL = 8; + public static final int REM_EQUAL = 9; + public static final int TILDE_EQUAL = 10; + public static final int LSHIFT_EQUAL = 11; + public static final int RSIGNEDSHIFT_EQUAL = 12; + + /** The value for variable initialization. */ public Expression initialization; private Object parent; private boolean reference; private Position position; + + private int operator; /** * Create a variable. * @param initialization the initialization * @param name the name of the variable * @param sourceStart the start point */ - public VariableDeclaration(Object parent, - char[] name, - Expression initialization, - int sourceStart) { + public VariableDeclaration(final Object parent, + final char[] name, + final Expression initialization, + final int operator, + final int sourceStart) { super(name, sourceStart, initialization.sourceEnd); this.initialization = initialization; + this.operator = operator; this.parent = parent; position = new Position(sourceStart, sourceEnd); } @@ -38,15 +61,15 @@ public class VariableDeclaration extends AbstractVariableDeclaration implements * @param name the name of the variable * @param sourceStart the start point */ - public VariableDeclaration(Object parent, - char[] name, - int sourceStart, - int sourceEnd) { + public VariableDeclaration(final Object parent, + final char[] name, + final int sourceStart, + final int sourceEnd) { super(name, sourceStart, sourceEnd); this.parent = parent; } - public void setReference(boolean reference) { + public void setReference(final boolean reference) { this.reference = reference; } @@ -56,11 +79,13 @@ public class VariableDeclaration extends AbstractVariableDeclaration implements * @param name the name of the variable * @param sourceStart the start point */ - public VariableDeclaration(char[] name, - Expression initialization, - int sourceStart) { + public VariableDeclaration(final char[] name, + final Expression initialization, + final int operator, + final int sourceStart) { super(name, sourceStart, initialization.sourceEnd); this.initialization = initialization; + this.operator = operator; } /** @@ -68,11 +93,47 @@ public class VariableDeclaration extends AbstractVariableDeclaration implements * @param name the name of the variable * @param sourceStart the start point */ - public VariableDeclaration(char[] name, - int sourceStart) { + public VariableDeclaration(final char[] name, + final int sourceStart) { super(name, sourceStart, sourceStart + name.length); } + /** + * Return the operator as String. + * @return the operator + */ + public final String operatorToString() { + switch (operator) { + case EQUAL: + return "="; //$NON-NLS-1$ + case PLUS_EQUAL: + return "+="; //$NON-NLS-1$ + case MINUS_EQUAL: + return "-="; //$NON-NLS-1$ + case STAR_EQUAL: + return "*="; //$NON-NLS-1$ + case SLASH_EQUAL: + return "/="; //$NON-NLS-1$ + case AND_EQUAL: + return "<="; //$NON-NLS-1$ + case OR_EQUAL: + return "|=";//$NON-NLS-1$ + case XOR_EQUAL: + return "^=";//$NON-NLS-1$ + case DOT_EQUAL: + return ".="; //$NON-NLS-1$ + case REM_EQUAL: + return "%="; //$NON-NLS-1$ + case TILDE_EQUAL: + return "~="; //$NON-NLS-1$ + case LSHIFT_EQUAL: + return "<<="; //$NON-NLS-1$ + case RSIGNEDSHIFT_EQUAL: + return ">>="; //$NON-NLS-1$ + } + return " unknown operator ";//$NON-NLS-1$ + } + /** * Return the variable into String. * @return a String @@ -86,7 +147,7 @@ public class VariableDeclaration extends AbstractVariableDeclaration implements } buff.append(name); if (initialization != null) { - buff.append(" = "); //$NON-NLS-1$ + buff.append(operatorToString()); //$NON-NLS-1$ buff.append(initialization.toStringExpression()); } return buff.toString(); @@ -105,10 +166,51 @@ public class VariableDeclaration extends AbstractVariableDeclaration implements * @return the image that represents a php variable */ public ImageDescriptor getImage() { - return PHPUiImages.DESC_VAR; + return PHPUiImages.DESC_VAR; } public Position getPosition() { return position; } + + /** + * Get the name of the field as String. + * @return the name of the String + */ + public String name() { + return new String(name); + } + + /** + * Get the variables from outside (parameters, globals ...) + * @return the variables from outside + */ + public List getOutsideVariable() { + return new ArrayList(); + } + + /** + * get the modified variables. + * @return the variables from we change value + */ + public List getModifiedVariable() { + final ArrayList list = new ArrayList(); + list.add(new VariableUsage(new String(name), sourceStart)); + if (initialization != null) { + list.addAll(initialization.getModifiedVariable()); + } + return list; + } + + /** + * Get the variables used. + * @return the variables used + */ + public List getUsedVariable() { + final ArrayList list = new ArrayList(); + if (initialization != null) { + list.addAll(initialization.getModifiedVariable());//yes it's getModified variable (in a variable declaration $a = $b, $a is modified, event if you have only $a and no initialization + } + return list; + } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/WhileStatement.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/WhileStatement.java index 01e956d..24d668b 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/WhileStatement.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/WhileStatement.java @@ -1,5 +1,8 @@ package net.sourceforge.phpdt.internal.compiler.ast; +import java.util.List; +import java.util.ArrayList; + /** * A While statement. * @author Matthieu Casanova @@ -18,10 +21,10 @@ public class WhileStatement extends Statement { * @param sourceStart the starting offset * @param sourceEnd the ending offset */ - public WhileStatement(Expression condition, - Statement action, - int sourceStart, - int sourceEnd) { + public WhileStatement(final Expression condition, + final Statement action, + final int sourceStart, + final int sourceEnd) { super(sourceStart, sourceEnd); this.condition = condition; this.action = action; @@ -32,15 +35,54 @@ public class WhileStatement extends Statement { * @param tab how many tabs (not used here * @return a String */ - public String toString(int tab) { - final String s = tabString(tab); - final StringBuffer buff = new StringBuffer("while ("); //$NON-NLS-1$ - buff.append(condition.toStringExpression()).append(")"); //$NON-NLS-1$ - if (action == null) { - buff.append(" {} ;"); //$NON-NLS-1$ + public String toString(final int tab) { + final String s = tabString(tab); + final StringBuffer buff = new StringBuffer(s).append("while ("); //$NON-NLS-1$ + buff.append(condition.toStringExpression()).append(")"); //$NON-NLS-1$ + if (action == null) { + buff.append(" {} ;"); //$NON-NLS-1$ } else { - buff.append("\n").append(action.toString(tab + 1)); //$NON-NLS-1$ + buff.append("\n").append(action.toString(tab + 1)); //$NON-NLS-1$ + } + return buff.toString(); + } + + /** + * Get the variables from outside (parameters, globals ...) + * @return the variables from outside + */ + public List getOutsideVariable() { + final ArrayList list = new ArrayList(); + list.addAll(condition.getOutsideVariable()); // todo: check if unuseful + if (action != null) { + list.addAll(action.getOutsideVariable()); + } + return list; + } + + /** + * get the modified variables. + * @return the variables from we change value + */ + public List getModifiedVariable() { + final ArrayList list = new ArrayList(); + list.addAll(condition.getModifiedVariable()); + if (action != null) { + list.addAll(action.getModifiedVariable()); + } + return list; + } + + /** + * Get the variables used. + * @return the variables used + */ + public List getUsedVariable() { + final ArrayList list = new ArrayList(); + list.addAll(condition.getUsedVariable()); + if (action != null) { + list.addAll(action.getUsedVariable()); } - return buff.toString(); + return list; } } -- 1.7.1