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.List;
012
013 public final class JsFunction extends JsLiteral implements HasName {
014 private JsBlock body;
015 private List<JsParameter> params;
016 private final JsScope scope;
017 private JsName name;
018
019 public JsFunction(JsScope parentScope) {
020 this(parentScope, (JsName) null);
021 }
022
023 public JsFunction(JsScope parentScope, JsBlock body) {
024 this(parentScope, (JsName) null);
025 this.body = body;
026 }
027
028 private JsFunction(JsScope parentScope, @Nullable JsName name) {
029 this.name = name;
030 scope = new JsScope(parentScope, name == null ? null : name.getIdent());
031 }
032
033 public JsBlock getBody() {
034 return body;
035 }
036
037 @Override
038 public JsName getName() {
039 return name;
040 }
041
042 @Override
043 public Symbol getSymbol() {
044 return name;
045 }
046
047 public List<JsParameter> getParameters() {
048 if (params == null) {
049 params = new SmartList<JsParameter>();
050 }
051 return params;
052 }
053
054 public JsScope getScope() {
055 return scope;
056 }
057
058 public void setBody(JsBlock body) {
059 this.body = body;
060 }
061
062 public void setName(@Nullable JsName name) {
063 this.name = name;
064 }
065
066 @Override
067 public void accept(JsVisitor v) {
068 v.visitFunction(this);
069 }
070
071 @Override
072 public void acceptChildren(JsVisitor visitor) {
073 visitor.acceptWithInsertRemove(params);
074 visitor.accept(body);
075 }
076 }