001    /*
002     $Id: VariableScopeCodeVisitor.java,v 1.12 2005/09/12 19:51:24 blackdrag Exp $
003    
004     Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005    
006     Redistribution and use of this software and associated documentation
007     ("Software"), with or without modification, are permitted provided
008     that the following conditions are met:
009    
010     1. Redistributions of source code must retain copyright
011        statements and notices.  Redistributions must also contain a
012        copy of this document.
013    
014     2. Redistributions in binary form must reproduce the
015        above copyright notice, this list of conditions and the
016        following disclaimer in the documentation and/or other
017        materials provided with the distribution.
018    
019     3. The name "groovy" must not be used to endorse or promote
020        products derived from this Software without prior written
021        permission of The Codehaus.  For written permission,
022        please contact info@codehaus.org.
023    
024     4. Products derived from this Software may not be called "groovy"
025        nor may "groovy" appear in their names without prior written
026        permission of The Codehaus. "groovy" is a registered
027        trademark of The Codehaus.
028    
029     5. Due credit should be given to The Codehaus -
030        http://groovy.codehaus.org/
031    
032     THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033     ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034     NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
036     THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041     STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043     OF THE POSSIBILITY OF SUCH DAMAGE.
044    
045     */
046    package org.codehaus.groovy.classgen;
047    
048    import java.util.Set;
049    
050    import org.codehaus.groovy.ast.CodeVisitorSupport;
051    import org.codehaus.groovy.ast.Parameter;
052    import org.codehaus.groovy.ast.VariableScope;
053    import org.codehaus.groovy.ast.expr.BinaryExpression;
054    import org.codehaus.groovy.ast.expr.ClosureExpression;
055    import org.codehaus.groovy.ast.expr.Expression;
056    import org.codehaus.groovy.ast.expr.MethodCallExpression;
057    import org.codehaus.groovy.ast.expr.PostfixExpression;
058    import org.codehaus.groovy.ast.expr.PrefixExpression;
059    import org.codehaus.groovy.ast.expr.VariableExpression;
060    import org.codehaus.groovy.ast.stmt.ForStatement;
061    
062    import org.codehaus.groovy.syntax.Types;
063    
064    /**
065     * A visitor which figures out which variables are in scope
066     * 
067     * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
068     * @version $Revision: 1.12 $
069     */
070    public class VariableScopeCodeVisitor extends CodeVisitorSupport {
071    
072        private VariableScope scope;
073    
074        public VariableScopeCodeVisitor(VariableScope scope) {
075            this.scope = scope;
076        }
077    
078        public Set getReferencedVariables() {
079            return scope.getReferencedVariables();
080        }
081    
082        public Set getDeclaredVariables() {
083            return scope.getDeclaredVariables();
084        }
085    
086        
087    
088        public void visitBinaryExpression(BinaryExpression expression) {
089            Expression leftExpression = expression.getLeftExpression();
090            if (expression.getOperation().isA(Types.ASSIGNMENT_OPERATOR) && leftExpression instanceof VariableExpression) {
091                declareVariable((VariableExpression) leftExpression);
092            }
093            else {
094                leftExpression.visit(this);
095            }
096            expression.getRightExpression().visit(this);
097        }
098    
099        public void visitForLoop(ForStatement forLoop) {
100            declareVariable(forLoop.getVariable());
101    
102            super.visitForLoop(forLoop);
103        }
104    
105        public void visitClosureExpression(ClosureExpression expression) {
106            VariableScopeCodeVisitor visitor = createClosureVisitor(expression);
107            expression.getCode().visit(visitor);
108        }
109        
110        public void visitVariableExpression(VariableExpression expression) {
111            // check for undeclared variables?
112            String variable = expression.getName();
113            /*
114            if (!parameterSet.contains(variable)) {
115                referencedVariables.add(variable);
116            }
117            */
118            getReferencedVariables().add(variable);
119        }
120    
121    
122        public void visitPostfixExpression(PostfixExpression expression) {
123            Expression exp = expression.getExpression();
124            if (exp instanceof VariableExpression) {
125                declareVariable((VariableExpression) exp);
126            }
127            else {
128                exp.visit(this);
129            }
130        }
131    
132        public void visitPrefixExpression(PrefixExpression expression) {
133            Expression exp = expression.getExpression();
134            if (exp instanceof VariableExpression) {
135                declareVariable((VariableExpression) exp);
136            }
137            else {
138                exp.visit(this);
139            }
140        }
141    
142        public void visitMethodCallExpression(MethodCallExpression call) {
143            if (call.isImplicitThis()) {
144                getReferencedVariables().add(call.getMethod());
145            }
146            super.visitMethodCallExpression(call);
147        }
148    
149        public void setParameters(Parameter[] parameters) {
150            /*
151            parameterSet.clear();
152            for (int i = 0; i < parameters.length; i++) {
153                parameterSet.add(parameters[i].getName());
154            }
155            */
156            
157            for (int i = 0; i < parameters.length; i++) {
158                declareVariable(parameters[i].getName());
159            }
160        }
161    
162        protected void declareVariable(VariableExpression varExp) {
163            String variable = varExp.getName();
164            declareVariable(variable);
165        }
166    
167        protected void declareVariable(String variable) {
168            /*
169            if (!parameterSet.contains(variable)) {
170                declaredVariables.add(variable);
171                getReferencedVariables().add(variable);
172            }
173            */
174            getDeclaredVariables().add(variable);
175            getReferencedVariables().add(variable);
176        }
177    
178        protected VariableScopeCodeVisitor createClosureVisitor(ClosureExpression expression) {
179            VariableScope closureScope = new VariableScope(scope);
180            expression.setVariableScope(closureScope);
181            VariableScopeCodeVisitor answer = new VariableScopeCodeVisitor(closureScope);
182            answer.setParameters(expression.getParameters());
183            return answer;
184        }
185    }