aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/packages/SkylarkInfo.java
blob: c20d574f25bd01a4634100b5d1dbc163c6e1b504 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2017 The Bazel Authors. 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.packages;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.packages.NativeProvider.StructConstructor;
import com.google.devtools.build.lib.syntax.Concatable;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.EvalUtils;
import java.util.Arrays;
import java.util.Map;

/** Implementation of {@link Info} created from Skylark. */
public abstract class SkylarkInfo extends Info implements Concatable {

  public SkylarkInfo(Provider provider, Location loc) {
    super(provider, loc);
  }

  public SkylarkInfo(StructConstructor provider, Map<String, Object> values, String message) {
    super(provider, values, message);
  }

  @Override
  public Concatter getConcatter() {
    return SkylarkInfoConcatter.INSTANCE;
  }

  @Override
  public boolean isImmutable() {
    // If the provider is not yet exported the hash code of the object is subject to change
    if (!getProvider().isExported()) {
      return false;
    }
    for (Object item : getValues()) {
      if (item != null && !EvalUtils.isImmutable(item)) {
        return false;
      }
    }
    return true;
  }

  /** Return all the values stored in the object. */
  protected abstract Iterable<Object> getValues();

  /**
   * {@link SkylarkInfo} implementation that stores its values in a map. This is mainly used for the
   * Skylark {@code struct()} constructor.
   */
  static final class MapBackedSkylarkInfo extends SkylarkInfo {
    protected final ImmutableMap<String, Object> values;

    public MapBackedSkylarkInfo(Provider provider, Map<String, Object> kwargs, Location loc) {
      super(provider, loc);
      this.values = copyValues(kwargs);
    }

    public MapBackedSkylarkInfo(
        StructConstructor provider, Map<String, Object> values, String message) {
      super(provider, values, message);
      this.values = copyValues(values);
    }

    @Override
    public Object getValue(String name) {
      return values.get(name);
    }

    @Override
    public boolean hasKey(String name) {
      return values.containsKey(name);
    }

    @Override
    public ImmutableCollection<String> getKeys() {
      return values.keySet();
    }

    @Override
    protected Iterable<Object> getValues() {
      return values.values();
    }
  }

  /** Create a {@link SkylarkInfo} instance from a provider and a map. */
  public static SkylarkInfo fromMap(Provider provider, Map<String, Object> values, Location loc) {
    return new MapBackedSkylarkInfo(provider, values, loc);
  }

  /** Implementation of {@link SkylarkInfo} that stores its values in array to save space. */
  static final class CompactSkylarkInfo extends SkylarkInfo implements Concatable {
    private final ImmutableMap<String, Integer> layout;
    private final Object[] values;

    public CompactSkylarkInfo(
        Provider provider, ImmutableMap<String, Integer> layout, Object[] values, Location loc) {
      super(provider, loc);
      Preconditions.checkState(layout.size() == values.length);
      this.layout = layout;
      this.values = values;
    }

    @Override
    public Concatter getConcatter() {
      return SkylarkInfoConcatter.INSTANCE;
    }

    @Override
    public Object getValue(String name) {
      Integer index = layout.get(name);
      if (index == null) {
        return null;
      }
      return values[index];
    }

    @Override
    public boolean hasKey(String name) {
      Integer index = layout.get(name);
      return index != null && values[index] != null;
    }

    @Override
    public ImmutableCollection<String> getKeys() {
      ImmutableSet.Builder<String> result = new ImmutableSet.Builder();
      for (Map.Entry<String, Integer> entry : layout.entrySet()) {
        if (values[entry.getValue()] != null) {
          result.add(entry.getKey());
        }
      }
      return result.build();
    }

    @Override
    protected Iterable<Object> getValues() {
      return Arrays.asList(values);
    }
  }

  /** Concatter for concrete {@link SkylarkInfo} subclasses. */
  private static final class SkylarkInfoConcatter implements Concatable.Concatter {
    private static final SkylarkInfoConcatter INSTANCE = new SkylarkInfoConcatter();

    private SkylarkInfoConcatter() {}

    @Override
    public Concatable concat(Concatable left, Concatable right, Location loc) throws EvalException {
      SkylarkInfo lval = (SkylarkInfo) left;
      SkylarkInfo rval = (SkylarkInfo) right;
      Provider provider = lval.getProvider();
      if (!provider.equals(rval.getProvider())) {
        throw new EvalException(
            loc,
            String.format(
                "Cannot concat %s with %s",
                provider.getPrintableName(), rval.getProvider().getPrintableName()));
      }
      SetView<String> commonFields =
          Sets.intersection(
              ImmutableSet.copyOf(lval.getKeys()), ImmutableSet.copyOf(rval.getKeys()));
      if (!commonFields.isEmpty()) {
        throw new EvalException(
            loc,
            "Cannot concat structs with common field(s): " + Joiner.on(",").join(commonFields));
      }
      // Keep homogeneous compact concatenations compact.
      if (lval instanceof CompactSkylarkInfo
          && rval instanceof CompactSkylarkInfo
          && ((CompactSkylarkInfo) lval).layout == ((CompactSkylarkInfo) rval).layout) {
        CompactSkylarkInfo compactLeft = (CompactSkylarkInfo) lval;
        CompactSkylarkInfo compactRight = (CompactSkylarkInfo) rval;
        int nvals = compactLeft.layout.size();
        Preconditions.checkState(nvals == compactRight.layout.size());
        Object[] newValues = new Object[nvals];
        for (int i = 0; i < nvals; i++) {
          newValues[i] =
              (compactLeft.values[i] != null) ? compactLeft.values[i] : compactRight.values[i];
        }
        return new CompactSkylarkInfo(
            compactLeft.getProvider(), compactLeft.layout, newValues, loc);
      }
      ImmutableMap.Builder<String, Object> newValues = ImmutableMap.builder();
      for (String key : lval.getKeys()) {
        newValues.put(key, lval.getValue(key));
      }
      for (String key : rval.getKeys()) {
        newValues.put(key, rval.getValue(key));
      }
      return new MapBackedSkylarkInfo(provider, newValues.build(), loc);
    }
  }
}