blob: 61e1ada5b50f28c0d8374cacfca5b26d037aa542 [file] [log] [blame]
Dmitry Lomovcdb6ef52016-08-05 08:38:26 +00001// 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.
14package com.google.devtools.build.lib.packages;
15
adonovan450c7ad2020-09-14 13:00:21 -070016import net.starlark.java.eval.Printer;
17import net.starlark.java.eval.StarlarkValue;
18import net.starlark.java.syntax.Location;
Dmitry Lomovcdb6ef52016-08-05 08:38:26 +000019
cparsons4ebf6c02018-08-17 14:49:36 -070020/**
adonovana11e2d02019-12-06 07:11:35 -080021 * 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.
cparsons4ebf6c02018-08-17 14:49:36 -070025 */
adonovandd000462020-11-06 12:57:38 -080026// 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
adonovanb58650a2020-11-10 10:27:37 -080030// StructImpl.provider in CL 341102857.
adonovandd000462020-11-06 12:57:38 -080031// - StructImpl is then really just a collection of helper functions for subclasses
adonovanb58650a2020-11-10 10:27:37 -080032// 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.
adonovandd000462020-11-06 12:57:38 -080034// - 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.
adonovanb58650a2020-11-10 10:27:37 -080038// - 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.
adonovandd000462020-11-06 12:57:38 -080043//
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//
adonovana11e2d02019-12-06 07:11:35 -080057public interface Info extends StarlarkValue {
Dmitry Lomovcdb6ef52016-08-05 08:38:26 +000058
adonovana11e2d02019-12-06 07:11:35 -080059 /** Returns the provider that instantiated this Info. */
60 Provider getProvider();
brandjon11bcd242017-12-27 12:01:27 -080061
62 /**
adonovana11e2d02019-12-06 07:11:35 -080063 * Returns the source location where this Info (provider instance) was created, or BUILTIN if it
64 * was instantiated by Java code.
brandjon11bcd242017-12-27 12:01:27 -080065 */
adonovan151a3232020-11-06 13:30:25 -080066 default Location getCreationLocation() {
adonovana11e2d02019-12-06 07:11:35 -080067 return Location.BUILTIN;
Dmitry Lomovcdb6ef52016-08-05 08:38:26 +000068 }
69
Dmitry Lomovcdb6ef52016-08-05 08:38:26 +000070 @Override
adonovana11e2d02019-12-06 07:11:35 -080071 default void repr(Printer printer) {
cparsons4ebf6c02018-08-17 14:49:36 -070072 printer.append("<instance of provider ");
adonovana11e2d02019-12-06 07:11:35 -080073 printer.append(getProvider().getPrintableName());
cparsons4ebf6c02018-08-17 14:49:36 -070074 printer.append(">");
Dmitry Lomovcdb6ef52016-08-05 08:38:26 +000075 }
76}