diff options
author | 2018-04-16 15:26:42 -0700 | |
---|---|---|
committer | 2018-04-16 15:28:30 -0700 | |
commit | cea083af0962ee580ff3635eef974c5fbba01f87 (patch) | |
tree | 1c8298dfd15e904e4b323984266432e141cea20d /src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java | |
parent | 0c5b4c440e28d347a69196159c9aff23e4c43d5c (diff) |
Create @SkylarkConstructor annotation, to annotate certain global-namespace @SkylarkCallable methods as representing constructors of other skylark types.
This change also demonstrates the new pattern on the global Label() constructor.
As an added bonus, it fixes documentation of that constructor. The old documentation used to read "Label.Label(...)" and the new documentation reads "Label(...)".
RELNOTES: None.
PiperOrigin-RevId: 193109338
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java | 58 |
1 files changed, 24 insertions, 34 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java index 985fbea325..a8ee1e007d 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java @@ -74,6 +74,7 @@ import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.skylarkinterface.Param; import com.google.devtools.build.lib.skylarkinterface.ParamType; import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkConstructor; import com.google.devtools.build.lib.skylarkinterface.SkylarkGlobalLibrary; import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter; import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature; @@ -1020,17 +1021,16 @@ public class SkylarkRuleClassFunctions { ImmutableList.of( SkylarkProvider.class, SkylarkDefinedAspect.class, SkylarkRuleFunction.class); - @SkylarkSignature( + @SkylarkCallable( name = "Label", doc = "Creates a Label referring to a BUILD target. Use " + "this function only when you want to give a default value for the label attributes. " + "The argument must refer to an absolute label. " + "Example: <br><pre class=language-python>Label(\"//tools:default\")</pre>", - returnType = Label.class, - objectType = Label.class, parameters = { - @Param(name = "label_string", type = String.class, doc = "the label string."), + @Param(name = "label_string", type = String.class, legacyNamed = true, + doc = "the label string."), @Param( name = "relative_to_caller_repository", type = Boolean.class, @@ -1050,36 +1050,26 @@ public class SkylarkRuleClassFunctions { useLocation = true, useEnvironment = true ) - private static final BuiltinFunction label = - new BuiltinFunction("Label") { - @SuppressWarnings({"unchecked", "unused"}) - public Label invoke( - String labelString, Boolean relativeToCallerRepository, Location loc, Environment env) - throws EvalException { - Label parentLabel = null; - if (relativeToCallerRepository) { - parentLabel = env.getCallerLabel(); - } else { - parentLabel = env.getGlobals().getTransitiveLabel(); - } - try { - if (parentLabel != null) { - LabelValidator.parseAbsoluteLabel(labelString); - labelString = parentLabel.getRelative(labelString).getUnambiguousCanonicalForm(); - } - return labelCache.get(labelString); - } catch (LabelValidator.BadLabelException | LabelSyntaxException | ExecutionException e) { - throw new EvalException(loc, "Illegal absolute label syntax: " + labelString); - } - } - }; - - // We want the Label ctor to show up under the Label documentation, but to be a "global - // function." Thus, we create a global Label object here, which just points to the Skylark - // function above. - @SkylarkSignature(name = "Label", - documented = false) - private static final BuiltinFunction globalLabel = label; + @SkylarkConstructor(objectType = Label.class) + public Label label( + String labelString, Boolean relativeToCallerRepository, Location loc, Environment env) + throws EvalException { + Label parentLabel = null; + if (relativeToCallerRepository) { + parentLabel = env.getCallerLabel(); + } else { + parentLabel = env.getGlobals().getTransitiveLabel(); + } + try { + if (parentLabel != null) { + LabelValidator.parseAbsoluteLabel(labelString); + labelString = parentLabel.getRelative(labelString).getUnambiguousCanonicalForm(); + } + return labelCache.get(labelString); + } catch (LabelValidator.BadLabelException | LabelSyntaxException | ExecutionException e) { + throw new EvalException(loc, "Illegal absolute label syntax: " + labelString); + } + } @SkylarkSignature( name = "FileType", |