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    
009    /**
010     * Represents a JavaScript expression that references a name.
011     */
012    public final class JsNameRef extends JsExpressionImpl implements HasName {
013        private String ident;
014        private JsName name;
015        private JsExpression qualifier;
016    
017        public JsNameRef(JsName name) {
018            this.name = name;
019        }
020    
021        public JsNameRef(String ident) {
022            this.ident = ident;
023        }
024    
025        public JsNameRef(String ident, JsExpression qualifier) {
026            this.ident = ident;
027            this.qualifier = qualifier;
028        }
029    
030        public JsNameRef(String ident, String qualifier) {
031            this(ident, new JsNameRef(qualifier));
032        }
033    
034        public JsNameRef(JsName name, JsExpression qualifier) {
035            this.name = name;
036            this.qualifier = qualifier;
037        }
038    
039        public String getIdent() {
040            return (name == null) ? ident : name.getIdent();
041        }
042    
043        @Override
044        public JsName getName() {
045            return name;
046        }
047    
048        @Override
049        public Symbol getSymbol() {
050            return name;
051        }
052    
053        public JsExpression getQualifier() {
054            return qualifier;
055        }
056    
057        @Override
058        public boolean isLeaf() {
059            return qualifier == null;
060        }
061    
062        public void resolve(JsName name) {
063            this.name = name;
064            ident = null;
065        }
066    
067        public void setQualifier(JsExpression qualifier) {
068            this.qualifier = qualifier;
069        }
070    
071        @Override
072        public void accept(JsVisitor v) {
073            v.visitNameRef(this);
074        }
075    
076        @Override
077        public void acceptChildren(JsVisitor visitor) {
078            if (qualifier != null) {
079               visitor.accept(qualifier);
080            }
081        }
082    }