001    // Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
002    // for details. All rights reserved. Use of this source code is governed by a
003    // BSD-style license that can be found in the LICENSE file.
004    
005    package com.google.dart.compiler.backend.js.ast;
006    
007    import com.google.dart.compiler.common.Symbol;
008    import com.intellij.util.SmartList;
009    import org.jetbrains.annotations.Nullable;
010    
011    import java.util.*;
012    
013    /**
014     * A JavaScript <code>var</code> statement.
015     */
016    public class JsVars extends SourceInfoAwareJsNode implements JsStatement, Iterable<JsVars.JsVar> {
017        private final List<JsVar> vars;
018    
019        private final boolean multiline;
020    
021        public JsVars() {
022            this(new SmartList<JsVar>(), false);
023        }
024    
025        public JsVars(boolean multiline) {
026            this(new SmartList<JsVar>(), multiline);
027        }
028    
029        public JsVars(List<JsVar> vars, boolean multiline) {
030            this.vars = vars;
031            this.multiline = multiline;
032        }
033    
034        public JsVars(JsVar var) {
035            this(Collections.singletonList(var), false);
036        }
037    
038        public JsVars(JsVar... vars) {
039            this(Arrays.asList(vars), false);
040        }
041    
042        public boolean isMultiline() {
043            return multiline;
044        }
045    
046        /**
047         * A var declared using the JavaScript <code>var</code> statement.
048         */
049        public static class JsVar extends SourceInfoAwareJsNode implements HasName {
050            private final JsName name;
051            private JsExpression initExpression;
052    
053            public JsVar(JsName name) {
054                this.name = name;
055            }
056    
057            public JsVar(JsName name, @Nullable JsExpression initExpression) {
058                this.name = name;
059                this.initExpression = initExpression;
060            }
061    
062            public JsExpression getInitExpression() {
063                return initExpression;
064            }
065    
066            @Override
067            public JsName getName() {
068                return name;
069            }
070    
071            @Override
072            public Symbol getSymbol() {
073                return name;
074            }
075    
076            public void setInitExpression(JsExpression initExpression) {
077                this.initExpression = initExpression;
078            }
079    
080            @Override
081            public void accept(JsVisitor v) {
082                v.visit(this);
083            }
084    
085            @Override
086            public void acceptChildren(JsVisitor visitor) {
087                if (initExpression != null) {
088                    visitor.accept(initExpression);
089                }
090            }
091        }
092    
093        public void add(JsVar var) {
094            vars.add(var);
095        }
096    
097        public void addAll(Collection<? extends JsVars.JsVar> vars) {
098            this.vars.addAll(vars);
099        }
100    
101        public void addAll(JsVars otherVars) {
102            this.vars.addAll(otherVars.vars);
103        }
104    
105        public void addIfHasInitializer(JsVar var) {
106            if (var.getInitExpression() != null) {
107                add(var);
108            }
109        }
110    
111        public boolean isEmpty() {
112            return vars.isEmpty();
113        }
114    
115        @Override
116        public Iterator<JsVar> iterator() {
117            return vars.iterator();
118        }
119    
120        @Override
121        public void accept(JsVisitor v) {
122            v.visitVars(this);
123        }
124    
125        @Override
126        public void acceptChildren(JsVisitor visitor) {
127            visitor.acceptWithInsertRemove(vars);
128        }
129    }