View Javadoc

1   /*
2    $Id: VariableScopeCodeVisitor.java,v 1.12 2005/09/12 19:51:24 blackdrag Exp $
3   
4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5   
6    Redistribution and use of this software and associated documentation
7    ("Software"), with or without modification, are permitted provided
8    that the following conditions are met:
9   
10   1. Redistributions of source code must retain copyright
11      statements and notices.  Redistributions must also contain a
12      copy of this document.
13  
14   2. Redistributions in binary form must reproduce the
15      above copyright notice, this list of conditions and the
16      following disclaimer in the documentation and/or other
17      materials provided with the distribution.
18  
19   3. The name "groovy" must not be used to endorse or promote
20      products derived from this Software without prior written
21      permission of The Codehaus.  For written permission,
22      please contact info@codehaus.org.
23  
24   4. Products derived from this Software may not be called "groovy"
25      nor may "groovy" appear in their names without prior written
26      permission of The Codehaus. "groovy" is a registered
27      trademark of The Codehaus.
28  
29   5. Due credit should be given to The Codehaus -
30      http://groovy.codehaus.org/
31  
32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43   OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45   */
46  package org.codehaus.groovy.classgen;
47  
48  import java.util.Set;
49  
50  import org.codehaus.groovy.ast.CodeVisitorSupport;
51  import org.codehaus.groovy.ast.Parameter;
52  import org.codehaus.groovy.ast.VariableScope;
53  import org.codehaus.groovy.ast.expr.BinaryExpression;
54  import org.codehaus.groovy.ast.expr.ClosureExpression;
55  import org.codehaus.groovy.ast.expr.Expression;
56  import org.codehaus.groovy.ast.expr.MethodCallExpression;
57  import org.codehaus.groovy.ast.expr.PostfixExpression;
58  import org.codehaus.groovy.ast.expr.PrefixExpression;
59  import org.codehaus.groovy.ast.expr.VariableExpression;
60  import org.codehaus.groovy.ast.stmt.ForStatement;
61  
62  import org.codehaus.groovy.syntax.Types;
63  
64  /***
65   * A visitor which figures out which variables are in scope
66   * 
67   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
68   * @version $Revision: 1.12 $
69   */
70  public class VariableScopeCodeVisitor extends CodeVisitorSupport {
71  
72      private VariableScope scope;
73  
74      public VariableScopeCodeVisitor(VariableScope scope) {
75          this.scope = scope;
76      }
77  
78      public Set getReferencedVariables() {
79          return scope.getReferencedVariables();
80      }
81  
82      public Set getDeclaredVariables() {
83          return scope.getDeclaredVariables();
84      }
85  
86      
87  
88      public void visitBinaryExpression(BinaryExpression expression) {
89          Expression leftExpression = expression.getLeftExpression();
90          if (expression.getOperation().isA(Types.ASSIGNMENT_OPERATOR) && leftExpression instanceof VariableExpression) {
91              declareVariable((VariableExpression) leftExpression);
92          }
93          else {
94              leftExpression.visit(this);
95          }
96          expression.getRightExpression().visit(this);
97      }
98  
99      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 }