aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/SkylarkType.java
blob: 04c345fc333cb7e1f97d3bf47edf29e220dbad8c (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// 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.base.Preconditions;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.events.Location;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nullable;

/**
 * A class representing types available in Skylark.
 */
public class SkylarkType {

  private static final class Global {}

  public static final SkylarkType UNKNOWN = new SkylarkType(Object.class);
  public static final SkylarkType NONE = new SkylarkType(Environment.NoneType.class);
  public static final SkylarkType GLOBAL = new SkylarkType(Global.class);

  public static final SkylarkType STRING = new SkylarkType(String.class);
  public static final SkylarkType INT = new SkylarkType(Integer.class);
  public static final SkylarkType BOOL = new SkylarkType(Boolean.class);

  private final Class<?> type;

  // TODO(bazel-team): Change this to SkylarkType and check generics of generics etc.
  // Object.class is used for UNKNOWN.
  private Class<?> generic1;

  public static SkylarkType of(Class<?> type, Class<?> generic1) {
    return new SkylarkType(type, generic1);
  }

  public static SkylarkType of(Class<?> type) {
    if (type.equals(Object.class)) {
      return SkylarkType.UNKNOWN;
    } else if (type.equals(String.class)) {
      return SkylarkType.STRING;
    } else if (type.equals(Integer.class)) {
      return SkylarkType.INT;
    } else if (type.equals(Boolean.class)) {
      return SkylarkType.BOOL;
    }
    return new SkylarkType(type);
  }

  private SkylarkType(Class<?> type, Class<?> generic1) {
    this.type = Preconditions.checkNotNull(type);
    this.generic1 = Preconditions.checkNotNull(generic1);
  }

  private SkylarkType(Class<?> type) {
    this.type = Preconditions.checkNotNull(type);
    this.generic1 = Object.class;
  }

  public Class<?> getType() {
    return type;
  }

  Class<?> getGenericType1() {
    return generic1;
  }

  /**
   * Returns the stronger type of this and o if they are compatible. Stronger means that
   * the more information is available, e.g. STRING is stronger than UNKNOWN and
   * LIST&lt;STRING> is stronger than LIST&lt;UNKNOWN>. Note than there's no type
   * hierarchy in Skylark.
   * <p>If they are not compatible an EvalException is thrown.
   */
  SkylarkType infer(SkylarkType o, String name, Location thisLoc, Location originalLoc)
      throws EvalException {
    if (this == o) {
      return this;
    }
    if (this == UNKNOWN || this.equals(SkylarkType.NONE)) {
      return o;
    }
    if (o == UNKNOWN || o.equals(SkylarkType.NONE)) {
      return this;
    }
    if (!type.equals(o.type)) {
      throw new EvalException(thisLoc, String.format("bad %s: %s is incompatible with %s at %s",
          name,
          EvalUtils.getDataTypeNameFromClass(o.getType()),
          EvalUtils.getDataTypeNameFromClass(this.getType()),
          originalLoc));
    }
    if (generic1.equals(Object.class)) {
      return o;
    }
    if (o.generic1.equals(Object.class)) {
      return this;
    }
    if (!generic1.equals(o.generic1)) {
      throw new EvalException(thisLoc, String.format("bad %s: incompatible generic variable types "
          + "%s with %s",
          name,
          EvalUtils.getDataTypeNameFromClass(o.generic1),
          EvalUtils.getDataTypeNameFromClass(this.generic1)));
    }
    return this;
  }

  boolean isStruct() {
    return type.equals(ClassObject.class);
  }

  boolean isList() {
    return SkylarkList.class.isAssignableFrom(type);
  }

  boolean isDict() {
    return Map.class.isAssignableFrom(type);
  }

  boolean isSet() {
    return Set.class.isAssignableFrom(type);
  }

  boolean isNset() {
    // TODO(bazel-team): NestedSets are going to be a bit strange with 2 type info (validation
    // and execution time). That can be cleaned up once we have complete type inference.
    return SkylarkNestedSet.class.isAssignableFrom(type);
  }

  boolean isSimple() {
    return !isStruct() && !isDict() && !isList() && !isNset() && !isSet();
  }

  @Override
  public String toString() {
    return this == UNKNOWN ? "Unknown" : EvalUtils.getDataTypeNameFromClass(type);
  }

  // hashCode() and equals() only uses the type field

  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (!(other instanceof SkylarkType)) {
      return false;
    }
    SkylarkType o = (SkylarkType) other;
    return this.type.equals(o.type);
  }

  @Override
  public int hashCode() {
    return type.hashCode();
  }

  /**
   * A class representing the type of a Skylark function.
   */
  public static final class SkylarkFunctionType extends SkylarkType {

    private final String name;
    @Nullable private SkylarkType returnType;
    @Nullable private Location returnTypeLoc;

    public static SkylarkFunctionType of(String name) {
      return new SkylarkFunctionType(name, null);
    }

    public static SkylarkFunctionType of(String name, SkylarkType returnType) {
      return new SkylarkFunctionType(name, returnType);
    }

    private SkylarkFunctionType(String name, SkylarkType returnType) {
      super(Function.class);
      this.name = name;
      this.returnType = returnType;
    }

    public SkylarkType getReturnType() {
      return returnType;
    }

    /**
     * Sets the return type of the function type if it's compatible with the existing return type.
     * Note that setting NONE only has an effect if the return type hasn't been set previously.
     */
    public void setReturnType(SkylarkType newReturnType, Location newLoc) throws EvalException {
      if (returnType == null) {
        returnType = newReturnType;
        returnTypeLoc = newLoc;
      } else if (newReturnType != SkylarkType.NONE) {
        returnType =
            returnType.infer(newReturnType, "return type of " + name, newLoc, returnTypeLoc);
        if (returnType == newReturnType) {
          returnTypeLoc = newLoc;
        }
      }
    }
  }

  private static boolean isTypeAllowedInSkylark(Object object) {
    if (object instanceof NestedSet<?>) {
      return false;
    } else if (object instanceof List<?>) {
      return false;
    }
    return true;
  }

  /**
   * Throws EvalException if the type of the object is not allowed to be present in Skylark.
   */
  static void checkTypeAllowedInSkylark(Object object, Location loc) throws EvalException {
    if (!isTypeAllowedInSkylark(object)) {
      throw new EvalException(loc,
          "Type is not allowed in Skylark: "
          + object.getClass().getSimpleName());
    }
  }

  private static Class<?> getGenericTypeFromMethod(Method method) {
    // This is where we can infer generic type information, so SkylarkNestedSets can be
    // created in a safe way. Eventually we should probably do something with Lists and Maps too.
    ParameterizedType t = (ParameterizedType) method.getGenericReturnType();
    Type type = t.getActualTypeArguments()[0];
    if (type instanceof Class) {
      return (Class<?>) type;
    }
    if (type instanceof WildcardType) {
      WildcardType wildcard = (WildcardType) type;
      Type upperBound = wildcard.getUpperBounds()[0];
      if (upperBound instanceof Class) {
        // i.e. List<? extends SuperClass>
        return (Class<?>) upperBound;
      }
    }
    // It means someone annotated a method with @SkylarkCallable with no specific generic type info.
    // We shouldn't annotate methods which return List<?> or List<T>.
    throw new IllegalStateException("Cannot infer type from method signature " + method);
  }

  /**
   * Converts an object retrieved from a Java method to a Skylark-compatible type.
   */
  static Object convertToSkylark(Object object, Method method) {
    if (object instanceof NestedSet<?>) {
      return new SkylarkNestedSet(getGenericTypeFromMethod(method), (NestedSet<?>) object);
    } else if (object instanceof List<?>) {
      return SkylarkList.list((List<?>) object, getGenericTypeFromMethod(method));
    }
    return object;
  }

  /**
   * Converts an object to a Skylark-compatible type if possible.
   */
  public static Object convertToSkylark(Object object, Location loc) throws EvalException {
    if (object instanceof List<?>) {
      return SkylarkList.list((List<?>) object, loc);
    }
    return object;
  }

  /**
   * Converts object from a Skylark-compatible wrapper type to its original type.
   */
  public static Object convertFromSkylark(Object value) {
    if (value instanceof SkylarkList) {
      return ((SkylarkList) value).toList();
    }
    return value;
  }

  /**
   * Creates a SkylarkType from the SkylarkBuiltin annotation.
   */
  public static SkylarkType getReturnType(SkylarkBuiltin annotation) {
    if (annotation.returnType().equals(Object.class)) {
      return SkylarkType.UNKNOWN;
    }
    if (Function.class.isAssignableFrom(annotation.returnType())) {
      return SkylarkFunctionType.of(annotation.name());
    }
    return SkylarkType.of(annotation.returnType());
  }
}