aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/ListComprehension.java
blob: 6a13ba846bc808231b3028158cce90c20c09375c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.syntax;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Syntax node for lists comprehension expressions.
 */
public final class ListComprehension extends Expression {

  private final Expression elementExpression;
  // This cannot be a map, because we need to both preserve order _and_ allow duplicate identifiers.
  private final List<Map.Entry<Ident, Expression>> lists;

  /**
   * [elementExpr (for var in listExpr)+]
   */
  public ListComprehension(Expression elementExpression) {
    this.elementExpression = elementExpression;
    lists = new ArrayList<Map.Entry<Ident, Expression>>();
  }

  @Override
  Object eval(Environment env) throws EvalException, InterruptedException {
    if (lists.size() == 0) {
      return convert(new ArrayList<>(), env);
    }

    List<Map.Entry<Ident, Iterable<?>>> listValues = Lists.newArrayListWithCapacity(lists.size());
    int size = 1;
    for (Map.Entry<Ident, Expression> list : lists) {
      Object listValueObject = list.getValue().eval(env);
      final Iterable<?> listValue = EvalUtils.toIterable(listValueObject, getLocation());
      int listSize = EvalUtils.size(listValue);
      if (listSize == 0) {
        return convert(new ArrayList<>(), env);
      }
      size *= listSize;
      listValues.add(Maps.<Ident, Iterable<?>>immutableEntry(list.getKey(), listValue));
    }
    List<Object> resultList = Lists.newArrayListWithCapacity(size);
    evalLists(env, listValues, resultList);
    return convert(resultList, env);
  }

  private Object convert(List<Object> list, Environment env) throws EvalException {
    if (env.isSkylarkEnabled()) {
      return SkylarkList.list(list, getLocation());
    } else {
      return list;
    }
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append('[').append(elementExpression);
    for (Map.Entry<Ident, Expression> list : lists) {
      sb.append(" for ").append(list.getKey()).append(" in ").append(list.getValue());
    }
    sb.append(']');
    return sb.toString();
  }

  public Expression getElementExpression() {
    return elementExpression;
  }

  public void add(Ident ident, Expression listExpression) {
    lists.add(Maps.immutableEntry(ident, listExpression));
  }

  public List<Map.Entry<Ident, Expression>> getLists() {
    return lists;
  }

  @Override
  public void accept(SyntaxTreeVisitor visitor) {
    visitor.visit(this);
  }

  /**
   * Evaluates element expression over all combinations of list element values.
   *
   * <p>Iterates over all elements in outermost list (list at index 0) and
   * updates the value of the list variable in the environment on each
   * iteration. If there are no other lists to iterate over added evaluation
   * of the element expression to the result. Otherwise calls itself recursively
   * with all the lists except the outermost.
   */
  private void evalLists(Environment env, List<Map.Entry<Ident, Iterable<?>>> listValues,
      List<Object> result) throws EvalException, InterruptedException {
    Map.Entry<Ident, Iterable<?>> listValue = listValues.get(0);
    for (Object listElement : listValue.getValue()) {
      env.update(listValue.getKey().getName(), listElement);
      if (listValues.size() == 1) {
        result.add(elementExpression.eval(env));
      } else {
        evalLists(env, listValues.subList(1, listValues.size()), result);
      }
    }
  }

  @Override
  SkylarkType validate(ValidationEnvironment env) throws EvalException {
    for (Map.Entry<Ident, Expression> list : lists) {
      // TODO(bazel-team): Get the type of elements
      SkylarkType type = list.getValue().validate(env);
      env.checkIterable(type, getLocation());
      env.update(list.getKey().getName(), SkylarkType.UNKNOWN, getLocation());
    }
    elementExpression.validate(env);
    return SkylarkType.of(SkylarkList.class);
  }
}