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 | package com.google.devtools.build.lib.syntax; |
| 15 | |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 16 | import com.google.common.base.Predicate; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.collect.Iterables; |
| 18 | import com.google.devtools.build.lib.events.Location; |
| 19 | import com.google.devtools.build.lib.syntax.FuncallExpression.MethodDescriptor; |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.util.SpellChecker; |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame^] | 21 | import java.io.IOException; |
Florian Weikert | a9dd72a | 2015-11-09 14:09:18 +0000 | [diff] [blame] | 22 | |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 23 | /** Syntax node for a dot expression. e.g. obj.field, but not obj.method() */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | public final class DotExpression extends Expression { |
| 25 | |
| 26 | private final Expression obj; |
| 27 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 28 | private final Identifier field; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 30 | public DotExpression(Expression obj, Identifier field) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | this.obj = obj; |
| 32 | this.field = field; |
| 33 | } |
| 34 | |
| 35 | public Expression getObj() { |
| 36 | return obj; |
| 37 | } |
| 38 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 39 | public Identifier getField() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | return field; |
| 41 | } |
| 42 | |
| 43 | @Override |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame^] | 44 | public void prettyPrint(Appendable buffer) throws IOException { |
| 45 | obj.prettyPrint(buffer); |
| 46 | buffer.append('.'); |
| 47 | field.prettyPrint(buffer); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 51 | Object doEval(Environment env) throws EvalException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | Object objValue = obj.eval(env); |
| 53 | String name = field.getName(); |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 54 | Object result = eval(objValue, name, getLocation(), env); |
Florian Weikert | a9dd72a | 2015-11-09 14:09:18 +0000 | [diff] [blame] | 55 | return checkResult(objValue, result, name, getLocation()); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Throws the correct error message if the result is null depending on the objValue. |
| 60 | */ |
| 61 | public static Object checkResult(Object objValue, Object result, String name, Location loc) |
Florian Weikert | 6edbf3b | 2015-11-09 21:33:26 +0000 | [diff] [blame] | 62 | throws EvalException { |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 63 | if (result != null) { |
| 64 | return result; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | } |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 66 | String suffix = ""; |
| 67 | if (objValue instanceof ClassObject) { |
| 68 | String customErrorMessage = ((ClassObject) objValue).errorMessage(name); |
| 69 | if (customErrorMessage != null) { |
| 70 | throw new EvalException(loc, customErrorMessage); |
| 71 | } |
| 72 | suffix = SpellChecker.didYouMean(name, ((ClassObject) objValue).getKeys()); |
| 73 | } |
| 74 | throw new EvalException( |
| 75 | loc, |
| 76 | String.format( |
| 77 | "object of type '%s' has no field '%s'%s", |
| 78 | EvalUtils.getDataTypeName(objValue), name, suffix)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the field of the given name of the struct objValue, or null if no such field exists. |
| 83 | */ |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 84 | public static Object eval(Object objValue, String name, |
| 85 | Location loc, Environment env) throws EvalException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | if (objValue instanceof ClassObject) { |
Florian Weikert | 3f610e8 | 2015-08-18 14:37:46 +0000 | [diff] [blame] | 87 | Object result = null; |
| 88 | try { |
| 89 | result = ((ClassObject) objValue).getValue(name); |
| 90 | } catch (IllegalArgumentException ex) { |
| 91 | throw new EvalException(loc, ex); |
| 92 | } |
Florian Weikert | eee8be6 | 2015-07-30 13:28:26 +0000 | [diff] [blame] | 93 | // ClassObjects may have fields that are annotated with @SkylarkCallable. |
| 94 | // Since getValue() does not know about those, we cannot expect that result is a valid object. |
| 95 | if (result != null) { |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 96 | result = SkylarkType.convertToSkylark(result, env); |
Florian Weikert | eee8be6 | 2015-07-30 13:28:26 +0000 | [diff] [blame] | 97 | // If we access NestedSets using ClassObject.getValue() we won't know the generic type, |
| 98 | // so we have to disable it. This should not happen. |
| 99 | SkylarkType.checkTypeAllowedInSkylark(result, loc); |
| 100 | return result; |
| 101 | } |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 102 | } |
Florian Weikert | eee8be6 | 2015-07-30 13:28:26 +0000 | [diff] [blame] | 103 | |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 104 | Iterable<MethodDescriptor> methods = |
| 105 | objValue instanceof Class<?> |
| 106 | ? FuncallExpression.getMethods((Class<?>) objValue, name) |
| 107 | : FuncallExpression.getMethods(objValue.getClass(), name); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 108 | |
| 109 | if (methods != null) { |
| 110 | methods = |
| 111 | Iterables.filter( |
| 112 | methods, |
| 113 | new Predicate<MethodDescriptor>() { |
| 114 | @Override |
| 115 | public boolean apply(MethodDescriptor methodDescriptor) { |
| 116 | return methodDescriptor.getAnnotation().structField(); |
| 117 | } |
| 118 | }); |
| 119 | if (methods.iterator().hasNext()) { |
| 120 | MethodDescriptor method = Iterables.getOnlyElement(methods); |
| 121 | if (method.getAnnotation().structField()) { |
| 122 | return FuncallExpression.callMethod(method, name, objValue, new Object[] {}, loc, env); |
| 123 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
Florian Weikert | a9dd72a | 2015-11-09 14:09:18 +0000 | [diff] [blame] | 126 | |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 127 | return null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void accept(SyntaxTreeVisitor visitor) { |
| 132 | visitor.visit(this); |
| 133 | } |
| 134 | |
| 135 | @Override |
Laurent Le Brun | 2e78d61 | 2015-04-15 09:06:46 +0000 | [diff] [blame] | 136 | void validate(ValidationEnvironment env) throws EvalException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 137 | obj.validate(env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 138 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | } |