Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 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 | |
| 16 | import com.google.common.collect.Iterables; |
| 17 | import com.google.devtools.build.lib.events.Location; |
| 18 | import com.google.devtools.build.lib.syntax.FuncallExpression.MethodDescriptor; |
| 19 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Syntax node for a dot expression. |
| 24 | * e.g. obj.field, but not obj.method() |
| 25 | */ |
| 26 | public final class DotExpression extends Expression { |
| 27 | |
| 28 | private final Expression obj; |
| 29 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 30 | private final Identifier field; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 32 | public DotExpression(Expression obj, Identifier field) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | this.obj = obj; |
| 34 | this.field = field; |
| 35 | } |
| 36 | |
| 37 | public Expression getObj() { |
| 38 | return obj; |
| 39 | } |
| 40 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 41 | public Identifier getField() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | return field; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public String toString() { |
| 47 | return obj + "." + field; |
| 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(); |
| 54 | Object result = eval(objValue, name, getLocation()); |
| 55 | if (result == null) { |
| 56 | if (objValue instanceof ClassObject) { |
| 57 | String customErrorMessage = ((ClassObject) objValue).errorMessage(name); |
| 58 | if (customErrorMessage != null) { |
| 59 | throw new EvalException(getLocation(), customErrorMessage); |
| 60 | } |
| 61 | } |
Francois-Rene Rideau | 8d5cce3 | 2015-06-16 23:12:04 +0000 | [diff] [blame] | 62 | throw new EvalException(getLocation(), Printer.format("Object of type '%s' has no field %r", |
| 63 | EvalUtils.getDataTypeName(objValue), name)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | } |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the field of the given name of the struct objValue, or null if no such field exists. |
| 70 | */ |
| 71 | public static Object eval(Object objValue, String name, Location loc) throws EvalException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | if (objValue instanceof ClassObject) { |
Florian Weikert | 3f610e8 | 2015-08-18 14:37:46 +0000 | [diff] [blame] | 73 | Object result = null; |
| 74 | try { |
| 75 | result = ((ClassObject) objValue).getValue(name); |
| 76 | } catch (IllegalArgumentException ex) { |
| 77 | throw new EvalException(loc, ex); |
| 78 | } |
Florian Weikert | eee8be6 | 2015-07-30 13:28:26 +0000 | [diff] [blame] | 79 | // ClassObjects may have fields that are annotated with @SkylarkCallable. |
| 80 | // Since getValue() does not know about those, we cannot expect that result is a valid object. |
| 81 | if (result != null) { |
| 82 | result = SkylarkType.convertToSkylark(result, loc); |
| 83 | // If we access NestedSets using ClassObject.getValue() we won't know the generic type, |
| 84 | // so we have to disable it. This should not happen. |
| 85 | SkylarkType.checkTypeAllowedInSkylark(result, loc); |
| 86 | return result; |
| 87 | } |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 88 | } |
Florian Weikert | eee8be6 | 2015-07-30 13:28:26 +0000 | [diff] [blame] | 89 | |
| 90 | List<MethodDescriptor> methods = |
| 91 | FuncallExpression.getMethods(objValue.getClass(), name, 0, loc); |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 92 | if (methods != null && !methods.isEmpty()) { |
| 93 | MethodDescriptor method = Iterables.getOnlyElement(methods); |
| 94 | if (method.getAnnotation().structField()) { |
| 95 | return FuncallExpression.callMethod(method, name, objValue, new Object[] {}, loc); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
Laurent Le Brun | 427bd97 | 2015-05-20 13:28:44 +0000 | [diff] [blame] | 98 | return null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void accept(SyntaxTreeVisitor visitor) { |
| 103 | visitor.visit(this); |
| 104 | } |
| 105 | |
| 106 | @Override |
Laurent Le Brun | 2e78d61 | 2015-04-15 09:06:46 +0000 | [diff] [blame] | 107 | void validate(ValidationEnvironment env) throws EvalException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | obj.validate(env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 109 | } |
| 110 | } |