blob: 2ef9ccabce85d3bb17d1fc0a9edf2118ec4e9d79 [file] [log] [blame]
Dmitry Lomov654717f2017-03-02 14:39:52 +00001// Copyright 2017 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
16import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
17import com.google.devtools.build.lib.events.Location;
cparsons56fbc992018-05-01 16:08:53 -070018import com.google.devtools.build.lib.skylarkbuildapi.ProviderApi;
brandjond331fa72017-12-28 07:38:31 -080019import com.google.devtools.build.lib.syntax.ClassObject;
Dmitry Lomov654717f2017-03-02 14:39:52 +000020
21/**
cparsons4ebf6c02018-08-17 14:49:36 -070022 * Declared Provider (a constructor for {@link InfoInterface}).
Dmitry Lomov654717f2017-03-02 14:39:52 +000023 *
dslomovde965ac2017-07-31 21:07:51 +020024 * <p>Declared providers can be declared either natively ({@link NativeProvider} or in Skylark
25 * {@link SkylarkProvider}.
Dmitry Lomov654717f2017-03-02 14:39:52 +000026 *
dslomovde965ac2017-07-31 21:07:51 +020027 * <p>{@link Provider} serves both as "type identifier" for declared provider instances and as a
brandjona60c6a42017-12-21 05:53:21 -080028 * function that can be called to construct a provider. To the Skylark user, there are "providers"
29 * and "provider instances"; the former is a Java instance of this class, and the latter is a Java
cparsons4ebf6c02018-08-17 14:49:36 -070030 * instance of {@link InfoInterface}.
Dmitry Lomov654717f2017-03-02 14:39:52 +000031 *
dslomovde965ac2017-07-31 21:07:51 +020032 * <p>Prefer to use {@link Key} as a serializable identifier of {@link Provider}. In particular,
33 * {@link Key} should be used in all data structures exposed to Skyframe.
Dmitry Lomov654717f2017-03-02 14:39:52 +000034 */
Dmitry Lomov654717f2017-03-02 14:39:52 +000035@Immutable
cparsons56fbc992018-05-01 16:08:53 -070036public interface Provider extends ProviderApi {
Dmitry Lomov654717f2017-03-02 14:39:52 +000037
Dmitry Lomov654717f2017-03-02 14:39:52 +000038 /**
dslomovde965ac2017-07-31 21:07:51 +020039 * Has this {@link Provider} been exported? All native providers are always exported. Skylark
40 * providers are exported if they are assigned to top-level name in a Skylark module.
Dmitry Lomov654717f2017-03-02 14:39:52 +000041 */
cparsons2415cb42018-05-01 10:32:30 -070042 boolean isExported();
Dmitry Lomov654717f2017-03-02 14:39:52 +000043
dslomovde965ac2017-07-31 21:07:51 +020044 /** Returns a serializable representation of this {@link Provider}. */
cparsons2415cb42018-05-01 10:32:30 -070045 Key getKey();
Dmitry Lomov654717f2017-03-02 14:39:52 +000046
dslomovde965ac2017-07-31 21:07:51 +020047 /** Returns a name of this {@link Provider} that should be used in error messages. */
cparsons2415cb42018-05-01 10:32:30 -070048 String getPrintableName();
Dmitry Lomov654717f2017-03-02 14:39:52 +000049
50 /**
brandjond331fa72017-12-28 07:38:31 -080051 * Returns an error message format string for instances to use for their {@link
52 * ClassObject#getErrorMessageForUnknownField(String)}.
Dmitry Lomov654717f2017-03-02 14:39:52 +000053 *
brandjond331fa72017-12-28 07:38:31 -080054 * <p>The format string must contain one {@code '%s'} placeholder for the field name.
Dmitry Lomov654717f2017-03-02 14:39:52 +000055 */
cparsons2415cb42018-05-01 10:32:30 -070056 default String getErrorMessageFormatForUnknownField() {
57 return String.format("'%s' object has no attribute '%%s'", getPrintableName());
Dmitry Lomov654717f2017-03-02 14:39:52 +000058 }
59
60 /**
cparsons2415cb42018-05-01 10:32:30 -070061 * Returns the location at which provider was defined.
Dmitry Lomov654717f2017-03-02 14:39:52 +000062 */
cparsons2415cb42018-05-01 10:32:30 -070063 Location getLocation();
Dmitry Lomov654717f2017-03-02 14:39:52 +000064
dslomovde965ac2017-07-31 21:07:51 +020065 /** A serializable representation of {@link Provider}. */
Dmitry Lomov654717f2017-03-02 14:39:52 +000066 public abstract static class Key {}
67}