blob: 4672af11c01b6dd9204f2dcf17e2f4a2fb71029d [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.syntax;
15
Damien Martin-Guillerez2d32c582016-08-04 14:29:18 +000016import com.google.common.base.Predicate;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import com.google.common.collect.Iterables;
18import com.google.devtools.build.lib.events.Location;
19import com.google.devtools.build.lib.syntax.FuncallExpression.MethodDescriptor;
Laurent Le Brun57badf42017-01-02 15:12:24 +000020import com.google.devtools.build.lib.util.SpellChecker;
brandjone2ffd5d2017-06-27 18:14:54 +020021import java.io.IOException;
Florian Weikerta9dd72a2015-11-09 14:09:18 +000022
Damien Martin-Guillerez2d32c582016-08-04 14:29:18 +000023/** Syntax node for a dot expression. e.g. obj.field, but not obj.method() */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024public final class DotExpression extends Expression {
25
26 private final Expression obj;
27
Florian Weikert6f864c32015-07-23 11:26:39 +000028 private final Identifier field;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029
Florian Weikert6f864c32015-07-23 11:26:39 +000030 public DotExpression(Expression obj, Identifier field) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031 this.obj = obj;
32 this.field = field;
33 }
34
35 public Expression getObj() {
36 return obj;
37 }
38
Florian Weikert6f864c32015-07-23 11:26:39 +000039 public Identifier getField() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040 return field;
41 }
42
43 @Override
brandjone2ffd5d2017-06-27 18:14:54 +020044 public void prettyPrint(Appendable buffer) throws IOException {
45 obj.prettyPrint(buffer);
46 buffer.append('.');
47 field.prettyPrint(buffer);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010048 }
49
50 @Override
Florian Weikert90a15962015-09-11 13:43:10 +000051 Object doEval(Environment env) throws EvalException, InterruptedException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010052 Object objValue = obj.eval(env);
53 String name = field.getName();
Francois-Rene Rideau4e994102015-09-17 22:41:28 +000054 Object result = eval(objValue, name, getLocation(), env);
Florian Weikerta9dd72a2015-11-09 14:09:18 +000055 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 Weikert6edbf3b2015-11-09 21:33:26 +000062 throws EvalException {
Laurent Le Brun57badf42017-01-02 15:12:24 +000063 if (result != null) {
64 return result;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 }
Laurent Le Brun57badf42017-01-02 15:12:24 +000066 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 Nienhuysd08b27f2015-02-25 16:45:20 +010079 }
80
81 /**
82 * Returns the field of the given name of the struct objValue, or null if no such field exists.
83 */
Francois-Rene Rideau4e994102015-09-17 22:41:28 +000084 public static Object eval(Object objValue, String name,
85 Location loc, Environment env) throws EvalException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010086 if (objValue instanceof ClassObject) {
Florian Weikert3f610e82015-08-18 14:37:46 +000087 Object result = null;
88 try {
89 result = ((ClassObject) objValue).getValue(name);
90 } catch (IllegalArgumentException ex) {
91 throw new EvalException(loc, ex);
92 }
Florian Weikerteee8be62015-07-30 13:28:26 +000093 // 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 Rideau4e994102015-09-17 22:41:28 +000096 result = SkylarkType.convertToSkylark(result, env);
Florian Weikerteee8be62015-07-30 13:28:26 +000097 // 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 Brun427bd972015-05-20 13:28:44 +0000102 }
Florian Weikerteee8be62015-07-30 13:28:26 +0000103
Laurent Le Brun57badf42017-01-02 15:12:24 +0000104 Iterable<MethodDescriptor> methods =
105 objValue instanceof Class<?>
106 ? FuncallExpression.getMethods((Class<?>) objValue, name)
107 : FuncallExpression.getMethods(objValue.getClass(), name);
Damien Martin-Guillerez2d32c582016-08-04 14:29:18 +0000108
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 Nienhuysd08b27f2015-02-25 16:45:20 +0100124 }
125 }
Florian Weikerta9dd72a2015-11-09 14:09:18 +0000126
Laurent Le Brun427bd972015-05-20 13:28:44 +0000127 return null;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100128 }
129
130 @Override
131 public void accept(SyntaxTreeVisitor visitor) {
132 visitor.visit(this);
133 }
134
135 @Override
Laurent Le Brun2e78d612015-04-15 09:06:46 +0000136 void validate(ValidationEnvironment env) throws EvalException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100137 obj.validate(env);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100138 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100139}