/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/util/UnboundVariableCollector.java#1 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package saffron.util; import openjava.mop.Environment; import openjava.mop.OJClass; import openjava.ptree.*; import openjava.ptree.util.GenericVisitor; import java.util.Enumeration; import java.util.Hashtable; /** * @see Util#getUnboundVariables **/ public class UnboundVariableCollector extends GenericVisitor { Environment env; Hashtable mapNameToClass; UnboundVariableCollector(Environment env) { this.env = env; this.mapNameToClass = new Hashtable(); } // override GenericVisitor public void visit(Variable v) { OJClass clazz = env.lookupBind(v.toString()); if (clazz != null) { // variable's type is known in the enclosing environment, // therefore it must be inherited from that environment mapNameToClass.put(v.toString(), clazz); } } /** * Returns a parameter list <code>Class0 value0, ..., ClassN * valueN</code> containing all of the unbound variables * <code>value<i>i</i>value</code> in this cluster's expression. **/ public ParameterList getParameterList() { ParameterList parameterList = new ParameterList(); Enumeration names = mapNameToClass.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); OJClass clazz = (OJClass) mapNameToClass.get(name); parameterList.add( new Parameter(TypeName.forOJClass(clazz), name)); } return parameterList; } /** * Returns an argument list <code>value0, ..., valueN</code> containing * all variables <code>value<i>i</i>value</code> in this cluster's * expression. **/ public ExpressionList getArgumentList() { ExpressionList argumentList = new ExpressionList(); Enumeration names = mapNameToClass.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); argumentList.add(new Variable(name)); } return argumentList; } /** * Returns a set of assignments to member variables: <code>this.value0 * = value0; ...; this.valueN = valueN; return this;</code>. **/ public StatementList getAssignmentList() { StatementList statementList = new StatementList(); Enumeration names = mapNameToClass.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); statementList.add( new ExpressionStatement( new AssignmentExpression( new FieldAccess(SelfAccess.makeThis(), name), AssignmentExpression.EQUALS, new Variable(name)))); } return statementList; } /** * Returns a set of declarations of members to hold variables: * <code>private Class0 value0; ...; private ClassN valueN;</code>. **/ public MemberDeclarationList getMemberDeclarationList() { MemberDeclarationList memberDeclarationList = new MemberDeclarationList(); Enumeration names = mapNameToClass.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); OJClass clazz = (OJClass) mapNameToClass.get(name); memberDeclarationList.add( new FieldDeclaration( new ModifierList(ModifierList.PRIVATE), TypeName.forOJClass(clazz), name, null)); } return memberDeclarationList; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |