Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 17 | import com.google.common.annotations.VisibleForTesting; |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 18 | import com.google.common.base.Joiner; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.CacheLoader; |
| 22 | import com.google.common.cache.LoadingCache; |
| 23 | import com.google.common.collect.ImmutableList; |
| 24 | import com.google.common.collect.ImmutableMap; |
dmarting | 9ae2adc | 2017-10-05 03:07:12 +0200 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableSortedMap; |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 26 | import com.google.common.collect.Iterables; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.common.collect.Lists; |
| 28 | import com.google.devtools.build.lib.events.Location; |
janakr | 76de1ad | 2018-03-03 11:12:36 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.skylarkinterface.Param; |
Dmitry Lomov | d22e1de | 2017-09-25 08:53:50 -0400 | [diff] [blame] | 31 | import com.google.devtools.build.lib.skylarkinterface.ParamType; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; |
Jon Brandvein | 6696db2 | 2016-10-04 20:18:19 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.skylarkinterface.SkylarkInterfaceUtils; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | import com.google.devtools.build.lib.syntax.EvalException.EvalExceptionWithJavaCause; |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.syntax.Runtime.NoneType; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.util.Pair; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | import com.google.devtools.build.lib.util.StringUtilities; |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 39 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | import java.lang.reflect.InvocationTargetException; |
| 41 | import java.lang.reflect.Method; |
| 42 | import java.lang.reflect.Modifier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | import java.util.Collections; |
dmarting | 9ae2adc | 2017-10-05 03:07:12 +0200 | [diff] [blame] | 44 | import java.util.Comparator; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import java.util.HashMap; |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 46 | import java.util.HashSet; |
Pedro Liberal Fernandez | 837dbc1 | 2016-08-18 14:13:01 +0000 | [diff] [blame] | 47 | import java.util.LinkedHashMap; |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 48 | import java.util.LinkedHashSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | import java.util.List; |
| 50 | import java.util.Map; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 51 | import java.util.Set; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | import java.util.concurrent.ExecutionException; |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 53 | import java.util.stream.Collectors; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 54 | import javax.annotation.Nullable; |
| 55 | |
janakr | 76de1ad | 2018-03-03 11:12:36 -0800 | [diff] [blame] | 56 | /** Syntax node for a function call expression. */ |
| 57 | @AutoCodec |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | public final class FuncallExpression extends Expression { |
| 59 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | /** |
| 61 | * A value class to store Methods with their corresponding SkylarkCallable annotations. |
| 62 | * This is needed because the annotation is sometimes in a superclass. |
| 63 | */ |
| 64 | public static final class MethodDescriptor { |
| 65 | private final Method method; |
| 66 | private final SkylarkCallable annotation; |
| 67 | |
| 68 | private MethodDescriptor(Method method, SkylarkCallable annotation) { |
| 69 | this.method = method; |
| 70 | this.annotation = annotation; |
| 71 | } |
| 72 | |
| 73 | Method getMethod() { |
| 74 | return method; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the SkylarkCallable annotation corresponding to this method. |
| 79 | */ |
| 80 | public SkylarkCallable getAnnotation() { |
| 81 | return annotation; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private static final LoadingCache<Class<?>, Map<String, List<MethodDescriptor>>> methodCache = |
| 86 | CacheBuilder.newBuilder() |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 87 | .initialCapacity(10) |
| 88 | .maximumSize(100) |
| 89 | .build( |
| 90 | new CacheLoader<Class<?>, Map<String, List<MethodDescriptor>>>() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 92 | @Override |
| 93 | public Map<String, List<MethodDescriptor>> load(Class<?> key) throws Exception { |
| 94 | Map<String, List<MethodDescriptor>> methodMap = new HashMap<>(); |
| 95 | for (Method method : key.getMethods()) { |
| 96 | // Synthetic methods lead to false multiple matches |
| 97 | if (method.isSynthetic()) { |
| 98 | continue; |
| 99 | } |
Jon Brandvein | 6696db2 | 2016-10-04 20:18:19 +0000 | [diff] [blame] | 100 | SkylarkCallable callable = SkylarkInterfaceUtils.getSkylarkCallable(method); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 101 | if (callable == null) { |
| 102 | continue; |
| 103 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 104 | String name = callable.name(); |
| 105 | if (name.isEmpty()) { |
| 106 | name = StringUtilities.toPythonStyleFunctionName(method.getName()); |
| 107 | } |
| 108 | if (methodMap.containsKey(name)) { |
| 109 | methodMap.get(name).add(new MethodDescriptor(method, callable)); |
| 110 | } else { |
| 111 | methodMap.put( |
| 112 | name, Lists.newArrayList(new MethodDescriptor(method, callable))); |
| 113 | } |
| 114 | } |
| 115 | return ImmutableMap.copyOf(methodMap); |
| 116 | } |
| 117 | }); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 119 | private static final LoadingCache<Class<?>, Map<String, MethodDescriptor>> fieldCache = |
| 120 | CacheBuilder.newBuilder() |
| 121 | .initialCapacity(10) |
| 122 | .maximumSize(100) |
| 123 | .build( |
| 124 | new CacheLoader<Class<?>, Map<String, MethodDescriptor>>() { |
| 125 | |
| 126 | @Override |
| 127 | public Map<String, MethodDescriptor> load(Class<?> key) throws Exception { |
| 128 | ImmutableMap.Builder<String, MethodDescriptor> fieldMap = ImmutableMap.builder(); |
| 129 | HashSet<String> fieldNamesForCollisions = new HashSet<>(); |
| 130 | List<MethodDescriptor> fieldMethods = |
| 131 | methodCache |
| 132 | .get(key) |
| 133 | .values() |
| 134 | .stream() |
| 135 | .flatMap(List::stream) |
| 136 | .filter( |
| 137 | methodDescriptor -> methodDescriptor.getAnnotation().structField()) |
| 138 | .collect(Collectors.toList()); |
| 139 | |
| 140 | for (MethodDescriptor fieldMethod : fieldMethods) { |
| 141 | SkylarkCallable callable = fieldMethod.getAnnotation(); |
| 142 | String name = callable.name(); |
| 143 | if (name.isEmpty()) { |
| 144 | name = |
| 145 | StringUtilities.toPythonStyleFunctionName( |
| 146 | fieldMethod.getMethod().getName()); |
| 147 | } |
| 148 | // TODO(b/72113542): Validate with annotation processor instead of at runtime. |
| 149 | if (!fieldNamesForCollisions.add(name)) { |
| 150 | throw new IllegalArgumentException( |
| 151 | String.format( |
| 152 | "Class %s has two structField methods named %s defined", |
| 153 | key.getName(), name)); |
| 154 | } |
| 155 | fieldMap.put(name, fieldMethod); |
| 156 | } |
| 157 | return fieldMap.build(); |
| 158 | } |
| 159 | }); |
| 160 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 161 | /** |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 162 | * Returns a map of methods and corresponding SkylarkCallable annotations of the methods of the |
| 163 | * classObj class reachable from Skylark. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 164 | */ |
| 165 | public static ImmutableMap<Method, SkylarkCallable> collectSkylarkMethodsWithAnnotation( |
| 166 | Class<?> classObj) { |
dmarting | 9ae2adc | 2017-10-05 03:07:12 +0200 | [diff] [blame] | 167 | ImmutableSortedMap.Builder<Method, SkylarkCallable> methodMap |
| 168 | = ImmutableSortedMap.orderedBy(Comparator.comparing(Object::toString)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 169 | for (Method method : classObj.getMethods()) { |
| 170 | // Synthetic methods lead to false multiple matches |
| 171 | if (!method.isSynthetic()) { |
Jon Brandvein | 6696db2 | 2016-10-04 20:18:19 +0000 | [diff] [blame] | 172 | SkylarkCallable annotation = SkylarkInterfaceUtils.getSkylarkCallable(classObj, method); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 173 | if (annotation != null) { |
| 174 | methodMap.put(method, annotation); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return methodMap.build(); |
| 179 | } |
| 180 | |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 181 | private static class ArgumentListConversionResult { |
| 182 | private final ImmutableList<Object> arguments; |
| 183 | private final String error; |
| 184 | |
| 185 | private ArgumentListConversionResult(ImmutableList<Object> arguments, String error) { |
| 186 | this.arguments = arguments; |
| 187 | this.error = error; |
| 188 | } |
| 189 | |
| 190 | public static ArgumentListConversionResult fromArgumentList(ImmutableList<Object> arguments) { |
| 191 | return new ArgumentListConversionResult(arguments, null); |
| 192 | } |
| 193 | |
| 194 | public static ArgumentListConversionResult fromError(String error) { |
| 195 | return new ArgumentListConversionResult(null, error); |
| 196 | } |
| 197 | |
| 198 | public String getError() { |
| 199 | return error; |
| 200 | } |
| 201 | |
| 202 | public ImmutableList<Object> getArguments() { |
| 203 | return arguments; |
| 204 | } |
| 205 | } |
| 206 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 207 | /** |
| 208 | * An exception class to handle exceptions in direct Java API calls. |
| 209 | */ |
| 210 | public static final class FuncallException extends Exception { |
| 211 | |
| 212 | public FuncallException(String msg) { |
| 213 | super(msg); |
| 214 | } |
| 215 | } |
| 216 | |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 217 | private final Expression function; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 218 | |
michajlo | 5f39475 | 2017-10-06 23:51:10 +0200 | [diff] [blame] | 219 | private final ImmutableList<Argument.Passed> arguments; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 220 | |
| 221 | private final int numPositionalArgs; |
| 222 | |
michajlo | 5f39475 | 2017-10-06 23:51:10 +0200 | [diff] [blame] | 223 | public FuncallExpression(Expression function, ImmutableList<Argument.Passed> arguments) { |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 224 | this.function = Preconditions.checkNotNull(function); |
| 225 | this.arguments = Preconditions.checkNotNull(arguments); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 226 | this.numPositionalArgs = countPositionalArguments(); |
| 227 | } |
| 228 | |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 229 | /** Returns the function that is called. */ |
| 230 | public Expression getFunction() { |
| 231 | return this.function; |
| 232 | } |
| 233 | |
| 234 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 235 | * Returns the number of positional arguments. |
| 236 | */ |
| 237 | private int countPositionalArguments() { |
| 238 | int num = 0; |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 239 | for (Argument.Passed arg : arguments) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 240 | if (arg.isPositional()) { |
| 241 | num++; |
| 242 | } |
| 243 | } |
| 244 | return num; |
| 245 | } |
| 246 | |
| 247 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 248 | * Returns an (immutable, ordered) list of function arguments. The first n are |
| 249 | * positional and the remaining ones are keyword args, where n = |
| 250 | * getNumPositionalArguments(). |
| 251 | */ |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 252 | public List<Argument.Passed> getArguments() { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 253 | return Collections.unmodifiableList(arguments); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns the number of arguments which are positional; the remainder are |
| 258 | * keyword arguments. |
| 259 | */ |
| 260 | public int getNumPositionalArguments() { |
| 261 | return numPositionalArgs; |
| 262 | } |
| 263 | |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 264 | @Override |
| 265 | public void prettyPrint(Appendable buffer) throws IOException { |
| 266 | function.prettyPrint(buffer); |
| 267 | buffer.append('('); |
| 268 | String sep = ""; |
| 269 | for (Argument.Passed arg : arguments) { |
| 270 | buffer.append(sep); |
| 271 | arg.prettyPrint(buffer); |
| 272 | sep = ", "; |
| 273 | } |
| 274 | buffer.append(')'); |
| 275 | } |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 276 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 277 | @Override |
| 278 | public String toString() { |
vladmos | 6ff634d | 2017-07-05 10:25:01 -0400 | [diff] [blame] | 279 | Printer.LengthLimitedPrinter printer = new Printer.LengthLimitedPrinter(); |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 280 | printer.append(function.toString()); |
| 281 | printer.printAbbreviatedList(arguments, "(", ", ", ")", null, |
Florian Weikert | 2591e19 | 2015-10-05 14:24:51 +0000 | [diff] [blame] | 282 | Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_COUNT, |
| 283 | Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_STRING_LENGTH); |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 284 | return printer.toString(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 285 | } |
| 286 | |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 287 | /** Returns the Skylark callable Method of objClass with structField=true and the given name. */ |
| 288 | public static MethodDescriptor getStructField(Class<?> objClass, String methodName) { |
| 289 | try { |
| 290 | return fieldCache.get(objClass).get(methodName); |
| 291 | } catch (ExecutionException e) { |
| 292 | throw new IllegalStateException("Method loading failed: " + e); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** Returns the list of names of Skylark callable Methods of objClass with structField=true. */ |
| 297 | public static Set<String> getStructFieldNames(Class<?> objClass) { |
| 298 | try { |
| 299 | return fieldCache.get(objClass).keySet(); |
| 300 | } catch (ExecutionException e) { |
| 301 | throw new IllegalStateException("Method loading failed: " + e); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** Returns the list of Skylark callable Methods of objClass with the given name. */ |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 306 | public static List<MethodDescriptor> getMethods(Class<?> objClass, String methodName) { |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 307 | try { |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 308 | return methodCache.get(objClass).get(methodName); |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 309 | } catch (ExecutionException e) { |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 310 | throw new IllegalStateException("Method loading failed: " + e); |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 311 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 315 | * Returns a set of the Skylark name of all Skylark callable methods for object of type {@code |
| 316 | * objClass}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 317 | */ |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 318 | public static Set<String> getMethodNames(Class<?> objClass) { |
| 319 | try { |
| 320 | return methodCache.get(objClass).keySet(); |
| 321 | } catch (ExecutionException e) { |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 322 | throw new IllegalStateException("Method loading failed: " + e); |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 323 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 324 | } |
| 325 | |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 326 | /** |
| 327 | * Invokes the given structField=true method and returns the result. |
| 328 | * |
| 329 | * @param methodDescriptor the descriptor of the method to invoke |
| 330 | * @param fieldName the name of the struct field |
| 331 | * @param obj the object on which to invoke the method |
| 332 | * @return the method return value |
| 333 | * @throws EvalException if there was an issue evaluating the method |
| 334 | */ |
| 335 | public static Object invokeStructField( |
| 336 | MethodDescriptor methodDescriptor, String fieldName, Object obj) throws EvalException { |
| 337 | Preconditions.checkArgument(methodDescriptor.getAnnotation().structField()); |
| 338 | return callMethod(methodDescriptor, fieldName, obj, new Object[0], Location.BUILTIN, null); |
| 339 | } |
| 340 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 341 | static Object callMethod(MethodDescriptor methodDescriptor, String methodName, Object obj, |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 342 | Object[] args, Location loc, Environment env) throws EvalException { |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 343 | try { |
| 344 | Method method = methodDescriptor.getMethod(); |
| 345 | if (obj == null && !Modifier.isStatic(method.getModifiers())) { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 346 | throw new EvalException(loc, "method '" + methodName + "' is not static"); |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 347 | } |
| 348 | // This happens when the interface is public but the implementation classes |
| 349 | // have reduced visibility. |
| 350 | method.setAccessible(true); |
| 351 | Object result = method.invoke(obj, args); |
| 352 | if (method.getReturnType().equals(Void.TYPE)) { |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 353 | return Runtime.NONE; |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 354 | } |
| 355 | if (result == null) { |
| 356 | if (methodDescriptor.getAnnotation().allowReturnNones()) { |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 357 | return Runtime.NONE; |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 358 | } else { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 359 | throw new EvalException( |
| 360 | loc, |
| 361 | "method invocation returned None, please file a bug report: " |
| 362 | + methodName |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 363 | + Printer.printAbbreviatedList( |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 364 | ImmutableList.copyOf(args), "(", ", ", ")", null)); |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 367 | // TODO(bazel-team): get rid of this, by having everyone use the Skylark data structures |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 368 | result = SkylarkType.convertToSkylark(result, method, env); |
Francois-Rene Rideau | 6c10eac | 2015-09-17 19:17:20 +0000 | [diff] [blame] | 369 | if (result != null && !EvalUtils.isSkylarkAcceptable(result.getClass())) { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 370 | throw new EvalException( |
| 371 | loc, |
| 372 | Printer.format( |
| 373 | "method '%s' returns an object of invalid type %r", methodName, result.getClass())); |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 374 | } |
| 375 | return result; |
| 376 | } catch (IllegalAccessException e) { |
| 377 | // TODO(bazel-team): Print a nice error message. Maybe the method exists |
| 378 | // and an argument is missing or has the wrong type. |
| 379 | throw new EvalException(loc, "Method invocation failed: " + e); |
| 380 | } catch (InvocationTargetException e) { |
| 381 | if (e.getCause() instanceof FuncallException) { |
| 382 | throw new EvalException(loc, e.getCause().getMessage()); |
| 383 | } else if (e.getCause() != null) { |
| 384 | throw new EvalExceptionWithJavaCause(loc, e.getCause()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 385 | } else { |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 386 | // This is unlikely to happen |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 387 | throw new EvalException(loc, "method invocation failed: " + e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 388 | } |
| 389 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // TODO(bazel-team): If there's exactly one usable method, this works. If there are multiple |
| 393 | // matching methods, it still can be a problem. Figure out how the Java compiler does it |
| 394 | // exactly and copy that behaviour. |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 395 | // Throws an EvalException when it cannot find a matching function. |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 396 | private Pair<MethodDescriptor, List<Object>> findJavaMethod( |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 397 | Class<?> objClass, |
| 398 | String methodName, |
| 399 | List<Object> args, |
| 400 | Map<String, Object> kwargs, |
| 401 | Environment environment) |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 402 | throws EvalException { |
| 403 | Pair<MethodDescriptor, List<Object>> matchingMethod = null; |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 404 | List<MethodDescriptor> methods = getMethods(objClass, methodName); |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 405 | ArgumentListConversionResult argumentListConversionResult = null; |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 406 | if (methods != null) { |
| 407 | for (MethodDescriptor method : methods) { |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 408 | if (method.getAnnotation().structField()) { |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 409 | // TODO(cparsons): Allow structField methods to accept interpreter-supplied arguments. |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 410 | return new Pair<>(method, null); |
| 411 | } else { |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 412 | argumentListConversionResult = convertArgumentList(args, kwargs, method, environment); |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 413 | if (argumentListConversionResult.getArguments() != null) { |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 414 | if (matchingMethod == null) { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 415 | matchingMethod = new Pair<>(method, argumentListConversionResult.getArguments()); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 416 | } else { |
| 417 | throw new EvalException( |
| 418 | getLocation(), |
| 419 | String.format( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 420 | "type '%s' has multiple matches for function %s", |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 421 | EvalUtils.getDataTypeNameFromClass(objClass), |
| 422 | formatMethod(methodName, args, kwargs))); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 423 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 427 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 428 | if (matchingMethod == null) { |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 429 | String errorMessage; |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 430 | if (ClassObject.class.isAssignableFrom(objClass)) { |
| 431 | errorMessage = String.format("struct has no method '%s'", methodName); |
| 432 | } else if (argumentListConversionResult == null |
| 433 | || argumentListConversionResult.getError() == null) { |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 434 | errorMessage = |
| 435 | String.format( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 436 | "type '%s' has no method %s", |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 437 | EvalUtils.getDataTypeNameFromClass(objClass), |
| 438 | formatMethod(methodName, args, kwargs)); |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 439 | |
| 440 | } else { |
| 441 | errorMessage = |
| 442 | String.format( |
cparsons | a95884e | 2018-03-21 11:06:13 -0700 | [diff] [blame] | 443 | "%s, in method call %s of '%s'", |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 444 | argumentListConversionResult.getError(), |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 445 | formatMethod(methodName, args, kwargs), |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 446 | EvalUtils.getDataTypeNameFromClass(objClass)); |
| 447 | } |
| 448 | throw new EvalException(getLocation(), errorMessage); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 449 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 450 | return matchingMethod; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 453 | private static SkylarkType getType(Param param) { |
Dmitry Lomov | d22e1de | 2017-09-25 08:53:50 -0400 | [diff] [blame] | 454 | if (param.allowedTypes().length > 0) { |
| 455 | Preconditions.checkState(Object.class.equals(param.type())); |
| 456 | SkylarkType result = SkylarkType.BOTTOM; |
| 457 | for (ParamType paramType : param.allowedTypes()) { |
| 458 | SkylarkType t = |
| 459 | paramType.generic1() != Object.class |
| 460 | ? SkylarkType.of(paramType.type(), paramType.generic1()) |
| 461 | : SkylarkType.of(paramType.type()); |
| 462 | result = SkylarkType.Union.of(result, t); |
| 463 | } |
| 464 | return result; |
| 465 | } else { |
| 466 | SkylarkType type = |
| 467 | param.generic1() != Object.class |
| 468 | ? SkylarkType.of(param.type(), param.generic1()) |
| 469 | : SkylarkType.of(param.type()); |
| 470 | return type; |
| 471 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Constructs the parameters list to actually pass to the method, filling with default values if |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 476 | * any. If there is a type or argument mismatch, returns a result containing an error message. |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 477 | */ |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 478 | private ArgumentListConversionResult convertArgumentList( |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 479 | List<Object> args, |
| 480 | Map<String, Object> kwargs, |
| 481 | MethodDescriptor method, |
| 482 | Environment environment) { |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 483 | ImmutableList.Builder<Object> builder = ImmutableList.builder(); |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 484 | Class<?>[] javaMethodSignatureParams = method.getMethod().getParameterTypes(); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 485 | SkylarkCallable callable = method.getAnnotation(); |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 486 | int numExtraInterpreterParams = 0; |
| 487 | numExtraInterpreterParams += callable.useLocation() ? 1 : 0; |
| 488 | numExtraInterpreterParams += callable.useAst() ? 1 : 0; |
| 489 | numExtraInterpreterParams += callable.useEnvironment() ? 1 : 0; |
cparsons | 73e1016 | 2018-03-22 10:02:02 -0700 | [diff] [blame^] | 490 | numExtraInterpreterParams += callable.useSkylarkSemantics() ? 1 : 0; |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 491 | |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 492 | int mandatoryPositionals = callable.mandatoryPositionals(); |
| 493 | if (mandatoryPositionals < 0) { |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 494 | if (callable.parameters().length > 0) { |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 495 | mandatoryPositionals = 0; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 496 | } else { |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 497 | mandatoryPositionals = javaMethodSignatureParams.length - numExtraInterpreterParams; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 500 | if (mandatoryPositionals > args.size()) { |
| 501 | return ArgumentListConversionResult.fromError("too few arguments"); |
| 502 | } |
| 503 | if (args.size() > mandatoryPositionals + callable.parameters().length) { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 504 | return ArgumentListConversionResult.fromError("too many arguments"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 505 | } |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 506 | // First process the legacy positional parameters. |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 507 | int i = 0; |
| 508 | if (mandatoryPositionals > 0) { |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 509 | for (Class<?> param : javaMethodSignatureParams) { |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 510 | Object value = args.get(i); |
| 511 | if (!param.isAssignableFrom(value.getClass())) { |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 512 | return ArgumentListConversionResult.fromError( |
| 513 | String.format( |
Carmi Grushko | 1229160 | 2016-10-26 19:07:41 +0000 | [diff] [blame] | 514 | "Cannot convert parameter at position %d from type %s to type %s", |
| 515 | i, EvalUtils.getDataTypeName(value), param.toString())); |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 516 | } |
| 517 | builder.add(value); |
| 518 | i++; |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 519 | if (i >= mandatoryPositionals) { |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 520 | // Stops for specified parameters instead. |
| 521 | break; |
| 522 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 525 | |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 526 | // Then the parameters specified in callable.parameters() |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 527 | Set<String> keys = new LinkedHashSet<>(kwargs.keySet()); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 528 | for (Param param : callable.parameters()) { |
| 529 | SkylarkType type = getType(param); |
Googler | e5da53c | 2016-09-21 12:34:44 +0000 | [diff] [blame] | 530 | if (param.noneable()) { |
| 531 | type = SkylarkType.Union.of(type, SkylarkType.NONE); |
| 532 | } |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 533 | Object value = null; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 534 | if (i < args.size()) { |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 535 | value = args.get(i); |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 536 | if (!param.positional()) { |
| 537 | return ArgumentListConversionResult.fromError( |
| 538 | String.format("Parameter '%s' is not positional", param.name())); |
| 539 | } else if (!type.contains(value)) { |
| 540 | return ArgumentListConversionResult.fromError( |
| 541 | String.format( |
cparsons | a95884e | 2018-03-21 11:06:13 -0700 | [diff] [blame] | 542 | "expected value of type '%s' for parameter '%s'", |
| 543 | type.toString(), param.name())); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 544 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 545 | i++; |
| 546 | } else if (param.named() && keys.remove(param.name())) { |
| 547 | // Named parameters |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 548 | value = kwargs.get(param.name()); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 549 | if (!type.contains(value)) { |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 550 | return ArgumentListConversionResult.fromError( |
| 551 | String.format( |
cparsons | a95884e | 2018-03-21 11:06:13 -0700 | [diff] [blame] | 552 | "expected value of type '%s' for parameter '%s'", |
| 553 | type.toString(), param.name())); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 554 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 555 | } else { |
| 556 | // Use default value |
| 557 | if (param.defaultValue().isEmpty()) { |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 558 | return ArgumentListConversionResult.fromError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 559 | String.format("parameter '%s' has no default value", param.name())); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 560 | } |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 561 | value = SkylarkSignatureProcessor.getDefaultValue(param, null); |
| 562 | } |
| 563 | builder.add(value); |
| 564 | if (!param.noneable() && value instanceof NoneType) { |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 565 | return ArgumentListConversionResult.fromError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 566 | String.format("parameter '%s' cannot be None", param.name())); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 567 | } |
| 568 | } |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 569 | if (i < args.size()) { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 570 | return ArgumentListConversionResult.fromError("too many arguments"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 571 | } |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 572 | if (!keys.isEmpty()) { |
| 573 | return ArgumentListConversionResult.fromError( |
| 574 | String.format("unexpected keyword%s %s", |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 575 | keys.size() > 1 ? "s" : "", |
| 576 | Joiner.on(",").join(Iterables.transform(keys, s -> "'" + s + "'")))); |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 577 | } |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 578 | |
| 579 | // Then add any skylark-info arguments (for example the Environment). |
| 580 | if (callable.useLocation()) { |
| 581 | builder.add(getLocation()); |
| 582 | } |
| 583 | if (callable.useAst()) { |
| 584 | builder.add(this); |
| 585 | } |
| 586 | if (callable.useEnvironment()) { |
| 587 | builder.add(environment); |
| 588 | } |
cparsons | 73e1016 | 2018-03-22 10:02:02 -0700 | [diff] [blame^] | 589 | if (callable.useSkylarkSemantics()) { |
| 590 | builder.add(environment.getSemantics()); |
| 591 | } |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 592 | |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 593 | return ArgumentListConversionResult.fromArgumentList(builder.build()); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 594 | } |
| 595 | |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 596 | private static String formatMethod(String name, List<Object> args, Map<String, Object> kwargs) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 597 | StringBuilder sb = new StringBuilder(); |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 598 | sb.append(name).append("("); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 599 | boolean first = true; |
| 600 | for (Object obj : args) { |
| 601 | if (!first) { |
| 602 | sb.append(", "); |
| 603 | } |
Francois-Rene Rideau | cbebd63 | 2015-02-11 16:56:37 +0000 | [diff] [blame] | 604 | sb.append(EvalUtils.getDataTypeName(obj)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 605 | first = false; |
| 606 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 607 | for (Map.Entry<String, Object> kwarg : kwargs.entrySet()) { |
| 608 | if (!first) { |
| 609 | sb.append(", "); |
| 610 | } |
| 611 | sb.append(EvalUtils.getDataTypeName(kwarg.getValue())); |
| 612 | sb.append(" "); |
| 613 | sb.append(kwarg.getKey()); |
Googler | 344449d | 2016-09-15 18:25:35 +0000 | [diff] [blame] | 614 | first = false; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 615 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 616 | return sb.append(")").toString(); |
| 617 | } |
| 618 | |
| 619 | /** |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 620 | * Add one named argument to the keyword map, and returns whether that name has been encountered |
| 621 | * before. |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 622 | */ |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 623 | private static boolean addKeywordArgAndCheckIfDuplicate( |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 624 | Map<String, Object> kwargs, |
| 625 | String name, |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 626 | Object value) { |
| 627 | return kwargs.put(name, value) != null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 631 | * Add multiple arguments to the keyword map (**kwargs), and returns all the names of those |
| 632 | * arguments that have been encountered before or {@code null} if there are no such names. |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 633 | */ |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 634 | @Nullable |
| 635 | private static ImmutableList<String> addKeywordArgsAndReturnDuplicates( |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 636 | Map<String, Object> kwargs, |
| 637 | Object items, |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 638 | Location location) |
| 639 | throws EvalException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 640 | if (!(items instanceof Map<?, ?>)) { |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 641 | throw new EvalException( |
| 642 | location, |
Laurent Le Brun | 1159cc2 | 2017-01-04 12:27:39 +0000 | [diff] [blame] | 643 | "argument after ** must be a dictionary, not '" + EvalUtils.getDataTypeName(items) + "'"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 644 | } |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 645 | ImmutableList.Builder<String> duplicatesBuilder = null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 646 | for (Map.Entry<?, ?> entry : ((Map<?, ?>) items).entrySet()) { |
| 647 | if (!(entry.getKey() instanceof String)) { |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 648 | throw new EvalException( |
Laurent Le Brun | 1159cc2 | 2017-01-04 12:27:39 +0000 | [diff] [blame] | 649 | location, |
| 650 | "keywords must be strings, not '" + EvalUtils.getDataTypeName(entry.getKey()) + "'"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 651 | } |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 652 | String argName = (String) entry.getKey(); |
| 653 | if (addKeywordArgAndCheckIfDuplicate(kwargs, argName, entry.getValue())) { |
| 654 | if (duplicatesBuilder == null) { |
| 655 | duplicatesBuilder = ImmutableList.builder(); |
| 656 | } |
| 657 | duplicatesBuilder.add(argName); |
| 658 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 659 | } |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 660 | return duplicatesBuilder == null ? null : duplicatesBuilder.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 661 | } |
| 662 | |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 663 | /** |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 664 | * Checks whether the given object is a {@link BaseFunction}. |
| 665 | * |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 666 | * @throws EvalException If not a BaseFunction. |
| 667 | */ |
shreyax | 529d7ea | 2017-09-22 17:39:06 -0400 | [diff] [blame] | 668 | private static BaseFunction checkCallable(Object functionValue, Location location) |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 669 | throws EvalException { |
| 670 | if (functionValue instanceof BaseFunction) { |
| 671 | return (BaseFunction) functionValue; |
| 672 | } else { |
| 673 | throw new EvalException( |
| 674 | location, "'" + EvalUtils.getDataTypeName(functionValue) + "' object is not callable"); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /** |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 679 | * Call a method depending on the type of an object it is called on. |
| 680 | * |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 681 | * @param positionals The first object is expected to be the object the method is called on. |
| 682 | * @param call the original expression that caused this call, needed for rules especially |
| 683 | */ |
shreyax | 529d7ea | 2017-09-22 17:39:06 -0400 | [diff] [blame] | 684 | private Object invokeObjectMethod( |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 685 | String method, |
| 686 | ImmutableList<Object> positionals, |
| 687 | ImmutableMap<String, Object> keyWordArgs, |
| 688 | FuncallExpression call, |
| 689 | Environment env) |
| 690 | throws EvalException, InterruptedException { |
| 691 | Location location = call.getLocation(); |
| 692 | Object value = positionals.get(0); |
| 693 | ImmutableList<Object> positionalArgs = positionals.subList(1, positionals.size()); |
brandjon | dc2c550 | 2017-12-07 14:30:04 -0800 | [diff] [blame] | 694 | BaseFunction function = Runtime.getBuiltinRegistry().getFunction(value.getClass(), method); |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 695 | Object fieldValue = |
| 696 | (value instanceof ClassObject) ? ((ClassObject) value).getValue(method) : null; |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 697 | if (function != null) { |
| 698 | if (!isNamespace(value.getClass())) { |
| 699 | // Use self as an implicit parameter in front. |
| 700 | positionalArgs = positionals; |
| 701 | } |
| 702 | return function.call( |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 703 | positionalArgs, ImmutableMap.copyOf(keyWordArgs), call, env); |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 704 | } else if (fieldValue != null) { |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 705 | if (!(fieldValue instanceof BaseFunction)) { |
| 706 | throw new EvalException( |
| 707 | location, String.format("struct field '%s' is not a function", method)); |
| 708 | } |
| 709 | function = (BaseFunction) fieldValue; |
| 710 | return function.call( |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 711 | positionalArgs, ImmutableMap.copyOf(keyWordArgs), call, env); |
Laurent Le Brun | 88df1f5 | 2015-12-23 13:31:44 +0000 | [diff] [blame] | 712 | } else { |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 713 | // When calling a Java method, the name is not in the Environment, |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 714 | // so evaluating 'function' would fail. |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 715 | Class<?> objClass; |
| 716 | Object obj; |
| 717 | if (value instanceof Class<?>) { |
| 718 | // Static call |
| 719 | obj = null; |
| 720 | objClass = (Class<?>) value; |
| 721 | } else { |
| 722 | obj = value; |
| 723 | objClass = value.getClass(); |
| 724 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 725 | Pair<MethodDescriptor, List<Object>> javaMethod = |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 726 | call.findJavaMethod(objClass, method, positionalArgs, keyWordArgs, env); |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 727 | if (javaMethod.first.getAnnotation().structField()) { |
| 728 | // Not a method but a callable attribute |
| 729 | try { |
| 730 | return callFunction(javaMethod.first.getMethod().invoke(obj), env); |
| 731 | } catch (IllegalAccessException e) { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 732 | throw new EvalException(getLocation(), "method invocation failed: " + e); |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 733 | } catch (InvocationTargetException e) { |
| 734 | if (e.getCause() instanceof FuncallException) { |
| 735 | throw new EvalException(getLocation(), e.getCause().getMessage()); |
| 736 | } else if (e.getCause() != null) { |
| 737 | throw new EvalExceptionWithJavaCause(getLocation(), e.getCause()); |
| 738 | } else { |
| 739 | // This is unlikely to happen |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 740 | throw new EvalException(getLocation(), "method invocation failed: " + e); |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 744 | return callMethod(javaMethod.first, method, obj, javaMethod.second.toArray(), location, env); |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 748 | @SuppressWarnings("unchecked") |
| 749 | private void evalArguments(ImmutableList.Builder<Object> posargs, Map<String, Object> kwargs, |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 750 | Environment env) |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 751 | throws EvalException, InterruptedException { |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 752 | // Optimize allocations for the common case where they are no duplicates. |
| 753 | ImmutableList.Builder<String> duplicatesBuilder = null; |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 754 | // Iterate over the arguments. We assume all positional arguments come before any keyword |
| 755 | // or star arguments, because the argument list was already validated by |
| 756 | // Argument#validateFuncallArguments, as called by the Parser, |
| 757 | // which should be the only place that build FuncallExpression-s. |
michajlo | 5f39475 | 2017-10-06 23:51:10 +0200 | [diff] [blame] | 758 | // Argument lists are typically short and functions are frequently called, so go by index |
| 759 | // (O(1) for ImmutableList) to avoid the iterator overhead. |
| 760 | for (int i = 0; i < arguments.size(); i++) { |
| 761 | Argument.Passed arg = arguments.get(i); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 762 | Object value = arg.getValue().eval(env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 763 | if (arg.isPositional()) { |
| 764 | posargs.add(value); |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 765 | } else if (arg.isStar()) { // expand the starArg |
Laurent Le Brun | 54b9d2c | 2017-02-20 14:04:57 +0000 | [diff] [blame] | 766 | if (!(value instanceof Iterable)) { |
| 767 | throw new EvalException( |
| 768 | getLocation(), |
| 769 | "argument after * must be an iterable, not " + EvalUtils.getDataTypeName(value)); |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 770 | } |
Laurent Le Brun | 54b9d2c | 2017-02-20 14:04:57 +0000 | [diff] [blame] | 771 | posargs.addAll((Iterable<Object>) value); |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 772 | } else if (arg.isStarStar()) { // expand the kwargs |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 773 | ImmutableList<String> duplicates = |
| 774 | addKeywordArgsAndReturnDuplicates(kwargs, value, getLocation()); |
| 775 | if (duplicates != null) { |
| 776 | if (duplicatesBuilder == null) { |
| 777 | duplicatesBuilder = ImmutableList.builder(); |
| 778 | } |
| 779 | duplicatesBuilder.addAll(duplicates); |
| 780 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 781 | } else { |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 782 | if (addKeywordArgAndCheckIfDuplicate(kwargs, arg.getName(), value)) { |
| 783 | if (duplicatesBuilder == null) { |
| 784 | duplicatesBuilder = ImmutableList.builder(); |
| 785 | } |
| 786 | duplicatesBuilder.add(arg.getName()); |
| 787 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 788 | } |
| 789 | } |
nharmata | 701da6e | 2018-02-27 14:49:17 -0800 | [diff] [blame] | 790 | if (duplicatesBuilder != null) { |
| 791 | ImmutableList<String> dups = duplicatesBuilder.build(); |
| 792 | throw new EvalException( |
| 793 | getLocation(), |
| 794 | "duplicate keyword" |
| 795 | + (dups.size() > 1 ? "s" : "") |
| 796 | + " '" |
| 797 | + Joiner.on("', '").join(dups) |
| 798 | + "' in call to " |
| 799 | + function); |
| 800 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 803 | @VisibleForTesting |
| 804 | public static boolean isNamespace(Class<?> classObject) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 805 | return classObject.isAnnotationPresent(SkylarkModule.class) |
| 806 | && classObject.getAnnotation(SkylarkModule.class).namespace(); |
| 807 | } |
| 808 | |
| 809 | @Override |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 810 | Object doEval(Environment env) throws EvalException, InterruptedException { |
fzaiser | e0f1333 | 2017-08-14 12:00:51 +0200 | [diff] [blame] | 811 | // TODO: Remove this special case once method resolution and invocation are supported as |
| 812 | // separate steps. |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 813 | if (function instanceof DotExpression) { |
| 814 | return invokeObjectMethod(env, (DotExpression) function); |
| 815 | } |
fzaiser | e0f1333 | 2017-08-14 12:00:51 +0200 | [diff] [blame] | 816 | Object funcValue = function.eval(env); |
| 817 | return callFunction(funcValue, env); |
Florian Weikert | 3f610e8 | 2015-08-18 14:37:46 +0000 | [diff] [blame] | 818 | } |
| 819 | |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 820 | /** Invokes object.function() and returns the result. */ |
| 821 | private Object invokeObjectMethod(Environment env, DotExpression dot) |
| 822 | throws EvalException, InterruptedException { |
| 823 | Object objValue = dot.getObject().eval(env); |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 824 | ImmutableList.Builder<Object> posargs = new ImmutableList.Builder<>(); |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 825 | posargs.add(objValue); |
Francois-Rene Rideau | 5dcdbf9 | 2015-02-19 18:36:17 +0000 | [diff] [blame] | 826 | // We copy this into an ImmutableMap in the end, but we can't use an ImmutableMap.Builder, or |
| 827 | // we'd still have to have a HashMap on the side for the sake of properly handling duplicates. |
Pedro Liberal Fernandez | 837dbc1 | 2016-08-18 14:13:01 +0000 | [diff] [blame] | 828 | Map<String, Object> kwargs = new LinkedHashMap<>(); |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 829 | evalArguments(posargs, kwargs, env); |
| 830 | return invokeObjectMethod( |
fzaiser | 8c27a89 | 2017-08-11 17:26:44 +0200 | [diff] [blame] | 831 | dot.getField().getName(), posargs.build(), ImmutableMap.copyOf(kwargs), this, env); |
Florian Weikert | 3f610e8 | 2015-08-18 14:37:46 +0000 | [diff] [blame] | 832 | } |
Googler | a34d507 | 2015-03-05 13:51:28 +0000 | [diff] [blame] | 833 | |
Florian Weikert | 3f610e8 | 2015-08-18 14:37:46 +0000 | [diff] [blame] | 834 | /** |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 835 | * Calls a function object |
| 836 | */ |
| 837 | private Object callFunction(Object funcValue, Environment env) |
| 838 | throws EvalException, InterruptedException { |
Florian Weikert | 3f610e8 | 2015-08-18 14:37:46 +0000 | [diff] [blame] | 839 | ImmutableList.Builder<Object> posargs = new ImmutableList.Builder<>(); |
| 840 | // We copy this into an ImmutableMap in the end, but we can't use an ImmutableMap.Builder, or |
| 841 | // we'd still have to have a HashMap on the side for the sake of properly handling duplicates. |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 842 | Map<String, Object> kwargs = new LinkedHashMap<>(); |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 843 | BaseFunction function = checkCallable(funcValue, getLocation()); |
| 844 | evalArguments(posargs, kwargs, env); |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 845 | return function.call(posargs.build(), ImmutableMap.copyOf(kwargs), this, env); |
Florian Weikert | 3f610e8 | 2015-08-18 14:37:46 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 848 | /** |
| 849 | * Returns the value of the argument 'name' (or null if there is none). |
| 850 | * This function is used to associate debugging information to rules created by skylark "macros". |
| 851 | */ |
| 852 | @Nullable |
| 853 | public String getNameArg() { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 854 | for (Argument.Passed arg : arguments) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 855 | if (arg != null) { |
| 856 | String name = arg.getName(); |
| 857 | if (name != null && name.equals("name")) { |
| 858 | Expression expr = arg.getValue(); |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 859 | return (expr instanceof StringLiteral) ? ((StringLiteral) expr).getValue() : null; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | } |
| 863 | return null; |
| 864 | } |
| 865 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 866 | @Override |
| 867 | public void accept(SyntaxTreeVisitor visitor) { |
| 868 | visitor.visit(this); |
| 869 | } |
| 870 | |
| 871 | @Override |
laurentlb | af682d1 | 2017-08-24 20:32:02 +0200 | [diff] [blame] | 872 | public Kind kind() { |
| 873 | return Kind.FUNCALL; |
| 874 | } |
| 875 | |
| 876 | @Override |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 877 | protected boolean isNewScope() { |
| 878 | return true; |
| 879 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 880 | } |