Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. 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.packages; |
| 15 | |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 16 | import net.starlark.java.eval.Printer; |
| 17 | import net.starlark.java.eval.StarlarkValue; |
| 18 | import net.starlark.java.syntax.Location; |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 19 | |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 20 | /** |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 21 | * An Info is a unit of information produced by analysis of one configured target and consumed by |
| 22 | * other targets that depend directly upon it. The result of analysis is a dictionary of Info |
| 23 | * values, each keyed by its Provider. Every Info is an instance of a Provider: if a Provider is |
| 24 | * like a Java class, then an Info is like an instance of that class. |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 25 | */ |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 26 | // TODO(adonovan): simplify the hierarchies below in these steps: |
| 27 | // - Once to_{json,proto} are gone, StructApi can be deleted; structs should never again have |
| 28 | // methods. |
| 29 | // - StructImpl.location can be pushed down into subclasses that need it, much as we did for |
adonovan | b58650a | 2020-11-10 10:27:37 -0800 | [diff] [blame] | 30 | // StructImpl.provider in CL 341102857. |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 31 | // - StructImpl is then really just a collection of helper functions for subclasses |
adonovan | b58650a | 2020-11-10 10:27:37 -0800 | [diff] [blame] | 32 | // getValue(String, Class), repr, equals, hash. Move them, and merge it into Info interface, |
| 33 | // or rename it InfoStruct or StructuredInfo if we absolutely need inheritance. |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 34 | // - Move StructProvider.STRUCT and make StructProvider private. |
| 35 | // The StructProvider.createStruct method could be a simple function like depset, select. |
| 36 | // StructProviderApi could be eliminated. |
| 37 | // - eliminate StarlarkInfo + StarlarkInfo. |
adonovan | b58650a | 2020-11-10 10:27:37 -0800 | [diff] [blame] | 38 | // - NativeInfo's get{FieldNames,Value} methods are not needed by the Starlark interpreter, |
| 39 | // since all its fields are annotated. They exist for the hash/eq/str implementations |
| 40 | // defined in StructImpl over all its subclasses, and for json.encode. More thought is |
| 41 | // needed on how to bridge between annotated methods and user-defined Structures so that |
| 42 | // they appear similar to clients like json.encode. |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 43 | // |
| 44 | // Info (result of analysis) |
| 45 | // - StructImpl (structure with fields, to_{json,proto}). Implements Structure, StructApi. |
| 46 | // - OutputGroupInfo. Fields are output group names. |
| 47 | // - NativeInfo. Fields are Java annotated methods (tricky). |
| 48 | // - dozens of subclasses |
| 49 | // - StarlarkInfo. Has table of k/v pairs. Final. Supports x+y. |
| 50 | // |
| 51 | // Provider (key for analysis result Info; class symbol for StructImpls). Implements ProviderApi. |
| 52 | // - BuiltinProvider |
| 53 | // - StructProvider (for basic 'struct' values). Callable. Implements ProviderApi. |
| 54 | // - dozens of singleton subclasses |
| 55 | // - StarlarkProvider. Callable. |
| 56 | // |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 57 | public interface Info extends StarlarkValue { |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 58 | |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 59 | /** Returns the provider that instantiated this Info. */ |
| 60 | Provider getProvider(); |
brandjon | 11bcd24 | 2017-12-27 12:01:27 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 63 | * Returns the source location where this Info (provider instance) was created, or BUILTIN if it |
| 64 | * was instantiated by Java code. |
brandjon | 11bcd24 | 2017-12-27 12:01:27 -0800 | [diff] [blame] | 65 | */ |
adonovan | 151a323 | 2020-11-06 13:30:25 -0800 | [diff] [blame] | 66 | default Location getCreationLocation() { |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 67 | return Location.BUILTIN; |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 70 | @Override |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 71 | default void repr(Printer printer) { |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 72 | printer.append("<instance of provider "); |
adonovan | a11e2d0 | 2019-12-06 07:11:35 -0800 | [diff] [blame] | 73 | printer.append(getProvider().getPrintableName()); |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 74 | printer.append(">"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 75 | } |
| 76 | } |