blob: ed45d6079f98debacba164c028fcc7b806cc1d43 [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
laurentlbd6983672017-06-29 14:53:12 +020016import com.google.common.collect.Streams;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import com.google.devtools.build.lib.events.Location;
18import com.google.devtools.build.lib.syntax.FuncallExpression.MethodDescriptor;
Laurent Le Brun57badf42017-01-02 15:12:24 +000019import com.google.devtools.build.lib.util.SpellChecker;
brandjone2ffd5d2017-06-27 18:14:54 +020020import java.io.IOException;
laurentlbd6983672017-06-29 14:53:12 +020021import java.util.Optional;
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
brandjon990622b2017-07-11 19:56:45 +020026 private final Expression object;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027
Florian Weikert6f864c32015-07-23 11:26:39 +000028 private final Identifier field;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029
brandjon990622b2017-07-11 19:56:45 +020030 public DotExpression(Expression object, Identifier field) {
31 this.object = object;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010032 this.field = field;
33 }
34
brandjon990622b2017-07-11 19:56:45 +020035 public Expression getObject() {
36 return object;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037 }
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 {
brandjon990622b2017-07-11 19:56:45 +020045 object.prettyPrint(buffer);
brandjone2ffd5d2017-06-27 18:14:54 +020046 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 {
brandjon990622b2017-07-11 19:56:45 +020052 Object objValue = object.eval(env);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053 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 }
Googler055d6c62018-05-22 13:21:04 -070066 throw getMissingFieldException(objValue, name, loc, "field");
67 }
68
69 static EvalException getMissingFieldException(
70 Object objValue, String name, Location loc, String accessName) {
Laurent Le Brun57badf42017-01-02 15:12:24 +000071 String suffix = "";
Googler055d6c62018-05-22 13:21:04 -070072 EvalException toSuppress = null;
Laurent Le Brun57badf42017-01-02 15:12:24 +000073 if (objValue instanceof ClassObject) {
brandjond331fa72017-12-28 07:38:31 -080074 String customErrorMessage = ((ClassObject) objValue).getErrorMessageForUnknownField(name);
Laurent Le Brun57badf42017-01-02 15:12:24 +000075 if (customErrorMessage != null) {
Googler055d6c62018-05-22 13:21:04 -070076 return new EvalException(loc, customErrorMessage);
Laurent Le Brun57badf42017-01-02 15:12:24 +000077 }
Googler055d6c62018-05-22 13:21:04 -070078 try {
79 suffix = SpellChecker.didYouMean(name, ((ClassObject) objValue).getFieldNames());
80 } catch (EvalException ee) {
81 toSuppress = ee;
82 }
83 } else {
84 suffix =
85 SpellChecker.didYouMean(
86 name,
87 FuncallExpression.getStructFieldNames(
88 objValue instanceof Class ? (Class<?>) objValue : objValue.getClass()));
Laurent Le Brun57badf42017-01-02 15:12:24 +000089 }
Googler055d6c62018-05-22 13:21:04 -070090 if (suffix.isEmpty() && hasMethod(objValue, name)) {
91 // If looking up the field failed, then we know that this method must have struct_field=false
92 suffix = ", however, a method of that name exists";
93 }
94 EvalException ee =
95 new EvalException(
96 loc,
97 String.format(
98 "object of type '%s' has no %s '%s'%s",
99 EvalUtils.getDataTypeName(objValue), accessName, name, suffix));
100 if (toSuppress != null) {
101 ee.addSuppressed(toSuppress);
102 }
103 return ee;
104 }
105
106 /** Returns whether the given object has a method with the given name. */
107 static boolean hasMethod(Object obj, String name) {
108 Class<?> cls = obj instanceof Class ? (Class<?>) obj : obj.getClass();
109 if (Runtime.getBuiltinRegistry().getFunctionNames(cls).contains(name)) {
110 return true;
111 }
112
113 return FuncallExpression.getMethodNames(cls).contains(name);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100114 }
115
116 /**
117 * Returns the field of the given name of the struct objValue, or null if no such field exists.
118 */
Francois-Rene Rideau4e994102015-09-17 22:41:28 +0000119 public static Object eval(Object objValue, String name,
cparsons48032932018-04-18 09:39:02 -0700120 Location loc, Environment env) throws EvalException, InterruptedException {
nharmatad223a2e2018-03-08 12:45:49 -0800121 if (objValue instanceof SkylarkClassObject) {
122 try {
123 return ((SkylarkClassObject) objValue).getValue(name);
124 } catch (IllegalArgumentException ex) {
125 throw new EvalException(loc, ex);
126 }
127 } else if (objValue instanceof ClassObject) {
Florian Weikert3f610e82015-08-18 14:37:46 +0000128 Object result = null;
129 try {
130 result = ((ClassObject) objValue).getValue(name);
131 } catch (IllegalArgumentException ex) {
132 throw new EvalException(loc, ex);
133 }
Florian Weikerteee8be62015-07-30 13:28:26 +0000134 // ClassObjects may have fields that are annotated with @SkylarkCallable.
135 // Since getValue() does not know about those, we cannot expect that result is a valid object.
136 if (result != null) {
Francois-Rene Rideau4e994102015-09-17 22:41:28 +0000137 result = SkylarkType.convertToSkylark(result, env);
Florian Weikerteee8be62015-07-30 13:28:26 +0000138 // If we access NestedSets using ClassObject.getValue() we won't know the generic type,
139 // so we have to disable it. This should not happen.
140 SkylarkType.checkTypeAllowedInSkylark(result, loc);
141 return result;
142 }
Laurent Le Brun427bd972015-05-20 13:28:44 +0000143 }
Florian Weikerteee8be62015-07-30 13:28:26 +0000144
Laurent Le Brun57badf42017-01-02 15:12:24 +0000145 Iterable<MethodDescriptor> methods =
146 objValue instanceof Class<?>
147 ? FuncallExpression.getMethods((Class<?>) objValue, name)
148 : FuncallExpression.getMethods(objValue.getClass(), name);
Damien Martin-Guillerez2d32c582016-08-04 14:29:18 +0000149
150 if (methods != null) {
laurentlbd6983672017-06-29 14:53:12 +0200151 Optional<MethodDescriptor> method =
152 Streams.stream(methods)
153 .filter(methodDescriptor -> methodDescriptor.getAnnotation().structField())
154 .findFirst();
155 if (method.isPresent() && method.get().getAnnotation().structField()) {
156 return FuncallExpression.callMethod(
157 method.get(), name, objValue, new Object[] {}, loc, env);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100158 }
159 }
Florian Weikerta9dd72a2015-11-09 14:09:18 +0000160
Laurent Le Brun427bd972015-05-20 13:28:44 +0000161 return null;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100162 }
163
164 @Override
165 public void accept(SyntaxTreeVisitor visitor) {
166 visitor.visit(this);
167 }
laurentlbaf682d12017-08-24 20:32:02 +0200168
169 @Override
170 public Kind kind() {
171 return Kind.DOT;
172 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100173}