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 org.jetbrains.kotlin.js.util;
006
007 import com.intellij.util.SmartList;
008 import org.jetbrains.annotations.NotNull;
009 import org.jetbrains.annotations.Nullable;
010 import org.jetbrains.kotlin.js.backend.ast.JsBinaryOperation;
011 import org.jetbrains.kotlin.js.backend.ast.JsBinaryOperator;
012 import org.jetbrains.kotlin.js.backend.ast.JsExpression;
013 import org.jetbrains.kotlin.js.backend.ast.JsNode;
014
015 import java.util.ArrayList;
016 import java.util.List;
017
018 public final class AstUtil {
019 private AstUtil() {
020 }
021
022 /**
023 * Returns a sequence of expressions (using the binary sequence operator).
024 *
025 * @param exprs - expressions to add to sequence
026 * @return a sequence of expressions.
027 */
028 public static JsBinaryOperation newSequence(JsExpression... exprs) {
029 if (exprs.length < 2) {
030 throw new RuntimeException("newSequence expects at least two arguments");
031 }
032 JsExpression result = exprs[exprs.length - 1];
033 for (int i = exprs.length - 2; i >= 0; i--) {
034 result = new JsBinaryOperation(JsBinaryOperator.COMMA, exprs[i], result);
035 }
036 return (JsBinaryOperation) result;
037 }
038
039 @Nullable
040 public static <T extends JsNode> T deepCopy(@Nullable T node) {
041 if (node == null) return null;
042
043 //noinspection unchecked
044 return (T) node.deepCopy();
045 }
046
047 @NotNull
048 public static <T extends JsNode> List<T> deepCopy(@Nullable List<T> nodes) {
049 if (nodes == null) return new SmartList<T>();
050
051 List<T> nodesCopy = new ArrayList<T>(nodes.size());
052 for (T node : nodes) {
053 nodesCopy.add(deepCopy(node));
054 }
055
056 return nodesCopy;
057 }
058 }