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.util.AstUtil;
008 import org.jetbrains.annotations.NotNull;
009 import com.intellij.util.SmartList;
010
011 import java.util.ArrayList;
012 import java.util.List;
013
014 /**
015 * Represents a JavaScript block statement.
016 */
017 public class JsBlock extends SourceInfoAwareJsNode implements JsStatement {
018 @NotNull
019 private final List<JsStatement> statements;
020
021 public JsBlock() {
022 this(new ArrayList<JsStatement>());
023 }
024
025 public JsBlock(JsStatement statement) {
026 this(new SmartList<JsStatement>(statement));
027 }
028
029 public JsBlock(JsStatement... statements) {
030 this(new SmartList<JsStatement>(statements));
031 }
032
033 public JsBlock(@NotNull List<JsStatement> statements) {
034 this.statements = statements;
035 }
036
037 @NotNull
038 public List<JsStatement> getStatements() {
039 return statements;
040 }
041
042 public boolean isEmpty() {
043 return statements.isEmpty();
044 }
045
046 public boolean isGlobalBlock() {
047 return false;
048 }
049
050 @Override
051 public void accept(JsVisitor v) {
052 v.visitBlock(this);
053 }
054
055 @Override
056 public void acceptChildren(JsVisitor visitor) {
057 visitor.acceptWithInsertRemove(statements);
058 }
059
060 @Override
061 public void traverse(JsVisitorWithContext v, JsContext ctx) {
062 if (v.visit(this, ctx)) {
063 v.acceptStatementList(statements);
064 }
065 v.endVisit(this, ctx);
066 }
067
068 @NotNull
069 @Override
070 public JsBlock deepCopy() {
071 return new JsBlock(AstUtil.deepCopy(statements)).withMetadataFrom(this);
072 }
073 }