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