blob: 131ed05972e122fa9c791cd90239ddf457b75832 [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001// Copyright 2014 Google Inc. 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.common.collect.ImmutableList;
17import com.google.devtools.build.lib.syntax.Label;
18
19import javax.annotation.Nullable;
20
21/**
22 * The interface for accessing a {@link Rule}'s attributes.
23 *
24 * <p>Since what an attribute lookup should return can be ambiguous (e.g. for configurable
25 * attributes, should we return a configuration-resolved value or the original, unresolved
26 * selection expression?), different implementations can apply different policies for how to
27 * fulfill these methods. Calling code can then use the appropriate implementation for whatever
28 * its particular needs are.
29 */
30public interface AttributeMap {
31 /**
32 * Returns the name of the rule; this is equivalent to {@code getLabel().getName()}.
33 */
34 String getName();
35
36 /**
37 * Returns the label of the rule.
38 */
39 Label getLabel();
40
41 /**
42 * Returns the value of the named rule attribute, which must be of the given type. If it does not
43 * exist or has the wrong type, throws an {@link IllegalArgumentException}.
44 */
45 <T> T get(String attributeName, Type<T> type);
46
47 /**
48 * Returns the names of all attributes covered by this map.
49 */
50 Iterable<String> getAttributeNames();
51
52 /**
53 * Returns the type of the given attribute, if it exists. Otherwise returns null.
54 */
55 @Nullable Type<?> getAttributeType(String attrName);
56
57 /**
58 * Returns the attribute definition whose name is {@code attrName}, or null
59 * if not found.
60 */
61 @Nullable Attribute getAttributeDefinition(String attrName);
62
63 /**
64 * Returns true iff the value of the specified attribute is explicitly set in the BUILD file (as
65 * opposed to its default value). This also returns true if the value from the BUILD file is the
66 * same as the default value.
67 *
68 * <p>It is probably a good idea to avoid this method in default value and implicit outputs
69 * computation, because it is confusing that setting an attribute to an empty list (for example)
70 * is different from not setting it at all.
71 */
72 boolean isAttributeValueExplicitlySpecified(String attributeName);
73
74 /**
75 * An interface which accepts {@link Attribute}s, used by {@link #visitLabels}.
76 */
77 interface AcceptsLabelAttribute {
78 /**
79 * Accept a (Label, Attribute) pair describing a dependency edge.
80 *
81 * @param label the target node of the (Rule, Label) edge.
82 * The source node should already be known.
83 * @param attribute the attribute.
84 */
85 void acceptLabelAttribute(Label label, Attribute attribute);
86 }
87
88 /**
89 * For all attributes that contain labels in their values (either by *being* a label or
90 * being a collection that includes labels), visits every label and notifies the
91 * specified observer at each visit.
92 */
93 void visitLabels(AcceptsLabelAttribute observer);
94
95 // TODO(bazel-team): These methods are here to support computed defaults that inherit
96 // package-level default values. Instead, we should auto-inherit and remove the computed
97 // defaults. If we really need to give access to package-level defaults, we should come up with
98 // a more generic interface.
99 String getPackageDefaultHdrsCheck();
100
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100101 Boolean getPackageDefaultTestOnly();
102
103 String getPackageDefaultDeprecation();
104
105 ImmutableList<String> getPackageDefaultCopts();
Googler534fb952015-03-12 11:58:54 +0000106
107 /**
108 * @return true if an attribute with the given name and type is present
109 */
110 boolean has(String attrName, Type<?> type);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100111}