001    /*
002     $Id: LabelVerifier.java 3419 2006-01-19 00:07:02Z blackdrag $
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.control;
047    
048    import java.util.Iterator;
049    import java.util.LinkedList;
050    
051    import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
052    import org.codehaus.groovy.ast.stmt.BreakStatement;
053    import org.codehaus.groovy.ast.stmt.ContinueStatement;
054    import org.codehaus.groovy.ast.stmt.DoWhileStatement;
055    import org.codehaus.groovy.ast.stmt.ForStatement;
056    import org.codehaus.groovy.ast.stmt.Statement;
057    import org.codehaus.groovy.ast.stmt.SwitchStatement;
058    import org.codehaus.groovy.ast.stmt.WhileStatement;
059    
060    /**
061     * This class checks the handling of labels in the AST
062     * 
063     * @author Jochen Theodorou
064     */
065    public class LabelVerifier extends ClassCodeVisitorSupport {
066    
067        private SourceUnit source;
068        private LinkedList visitedLabels;
069        private LinkedList continueLabels;
070        private LinkedList breakLabels;
071        boolean inLoop=false;
072        boolean inSwitch=false;
073        
074        public LabelVerifier(SourceUnit src) {
075            source = src;
076        }
077        
078        protected SourceUnit getSourceUnit() {
079            return source;
080        }
081        
082        private void init(){
083            visitedLabels = new LinkedList();
084            continueLabels = new LinkedList();
085            breakLabels = new LinkedList();
086            inLoop=false;
087            inSwitch=false;
088        }
089        
090        protected void visitClassCodeContainer(Statement code) {
091            init();
092            super.visitClassCodeContainer(code);
093            assertNoLabelsMissed();
094        }
095        
096       public void visitStatement(Statement statement) {
097           String label = statement.getStatementLabel();
098           
099           if (label!=null) {
100               for (Iterator iter = breakLabels.iterator(); iter.hasNext();) {
101                   BreakStatement element = (BreakStatement) iter.next();
102                   if (element.getLabel().equals(label)) iter.remove();
103               }
104               
105               for (Iterator iter = continueLabels.iterator(); iter.hasNext();) {
106                   ContinueStatement element = (ContinueStatement) iter.next();
107                   if (element.getLabel().equals(label)) iter.remove();
108               }
109               
110               visitedLabels.add(label);
111           }
112           
113           super.visitStatement(statement);
114    }
115        
116        public void visitForLoop(ForStatement forLoop) {
117            boolean oldInLoop = inLoop;
118            inLoop = true;
119            super.visitForLoop(forLoop);
120            inLoop = oldInLoop;
121        }
122        
123        public void visitDoWhileLoop(DoWhileStatement loop) {
124            boolean oldInLoop = inLoop;
125            inLoop = true;
126            super.visitDoWhileLoop(loop);
127            inLoop = oldInLoop;
128        }     
129        
130        public void visitWhileLoop(WhileStatement loop) {
131            boolean oldInLoop = inLoop;
132            inLoop = true;
133            super.visitWhileLoop(loop);
134            inLoop = oldInLoop;
135        }
136        
137        public void visitBreakStatement(BreakStatement statement) {
138            String label = statement.getLabel();
139            boolean hasNamedLabel = label!=null;
140            if (!hasNamedLabel && !inLoop && !inSwitch) {
141                addError("the break statement is only allowed inside loops or switches",statement);
142            } else if (hasNamedLabel && !inLoop) {
143                addError("the break statement with named label is only allowed inside loops",statement);
144            }
145            if (label!=null) {
146                boolean found=false;
147                for (Iterator iter = visitedLabels.iterator(); iter.hasNext();) {
148                    String element = (String) iter.next();
149                    if (element.equals(label)) {
150                        found = true;
151                        break;
152                    }
153                }
154                if (!found) breakLabels.add(statement);
155            }
156            
157            super.visitBreakStatement(statement);
158        }
159        
160        public void visitContinueStatement(ContinueStatement statement) {
161            String label = statement.getLabel();
162            boolean hasNamedLabel = label!=null;
163            if (!hasNamedLabel && !inLoop) {
164                addError("the continue statement is only allowed inside loops",statement);
165            } 
166            if (label!=null) {
167                boolean found=false;
168                for (Iterator iter = visitedLabels.iterator(); iter.hasNext();) {
169                    String element = (String) iter.next();
170                    if (element.equals(label)) {
171                        found = true;
172                        break;
173                    }
174                }
175                if (!found) continueLabels.add(statement);
176            }
177            
178            super.visitContinueStatement(statement);
179        }
180        
181        protected void assertNoLabelsMissed() {
182            //TODO: report multiple missing labels of the same name only once
183            for (Iterator iter = continueLabels.iterator(); iter.hasNext();) {
184                ContinueStatement element = (ContinueStatement) iter.next();
185                addError("continue to missing label",element);
186            }
187            for (Iterator iter = breakLabels.iterator(); iter.hasNext();) {
188                BreakStatement element = (BreakStatement) iter.next();
189                addError("break to missing label",element);
190            }
191        }
192        
193        public void visitSwitch(SwitchStatement statement) {
194            inSwitch=true;
195            super.visitSwitch(statement);
196        }
197    
198    }