Make some objects SkylarkValues

Skylark's Printer.BasePrinter doesn't guarantee it will call `.toString` on
objects of unknown types, and in the future that won't be the case anymore.
In order to keep their current string representations objects should implement
the SkylarkValue interface by providing an explicit implementation of `repr`.

PiperOrigin-RevId: 161526182
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkPath.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkPath.java
index 72505fb..264a034 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkPath.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkPath.java
@@ -19,6 +19,8 @@
 import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
 import com.google.devtools.build.lib.vfs.Path;
 import java.io.IOException;
 
@@ -34,7 +36,7 @@
   category = SkylarkModuleCategory.NONE,
   doc = "A structure representing a file to be used inside a repository."
 )
-final class SkylarkPath {
+final class SkylarkPath implements SkylarkValue {
   private final Path path;
 
   SkylarkPath(Path path) {
@@ -118,4 +120,9 @@
   public String toString() {
     return path.toString();
   }
+
+  @Override
+  public void repr(SkylarkPrinter printer) {
+    printer.append(toString());
+  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java b/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java
index 8640f26..e276793 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java
@@ -17,6 +17,8 @@
 import com.google.common.collect.ComparisonChain;
 import com.google.common.collect.Interner;
 import com.google.devtools.build.lib.concurrent.BlazeInterners;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
 import com.google.devtools.build.lib.util.Preconditions;
 import com.google.devtools.build.lib.vfs.Canonicalizer;
 import com.google.devtools.build.lib.vfs.PathFragment;
@@ -27,12 +29,13 @@
 /**
  * Uniquely identifies a package, given a repository name and a package's path fragment.
  *
- * <p>The repository the build is happening in is the <i>default workspace</i>, and is identified
- * by the workspace name "". Other repositories can be named in the WORKSPACE file.  These
- * workspaces are prefixed by {@literal @}.</p>
+ * <p>The repository the build is happening in is the <i>default workspace</i>, and is identified by
+ * the workspace name "". Other repositories can be named in the WORKSPACE file. These workspaces
+ * are prefixed by {@literal @}.
  */
 @Immutable
-public final class PackageIdentifier implements Comparable<PackageIdentifier>, Serializable {
+public final class PackageIdentifier
+    implements Comparable<PackageIdentifier>, Serializable, SkylarkValue {
 
   private static final Interner<PackageIdentifier> INTERNER = BlazeInterners.newWeakInterner();
 
@@ -216,4 +219,9 @@
         .compare(pkgName, that.pkgName)
         .result();
   }
+
+  @Override
+  public void repr(SkylarkPrinter printer) {
+    printer.repr(toString());
+  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/AppleCommandLineOptions.java b/src/main/java/com/google/devtools/build/lib/rules/apple/AppleCommandLineOptions.java
index 748f1df..2256156 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/apple/AppleCommandLineOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/apple/AppleCommandLineOptions.java
@@ -26,6 +26,8 @@
 import com.google.devtools.build.lib.rules.apple.ApplePlatform.PlatformType;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
 import com.google.devtools.common.options.Converters.CommaSeparatedOptionListConverter;
 import com.google.devtools.common.options.EnumConverter;
 import com.google.devtools.common.options.Option;
@@ -406,7 +408,7 @@
             + "\"embedded\", and \"embedded_markers\""
   )
   @Immutable
-  public enum AppleBitcodeMode {
+  public enum AppleBitcodeMode implements SkylarkValue {
 
     /** Do not compile bitcode. */
     NONE("none", ImmutableList.<String>of()),
@@ -435,6 +437,11 @@
       return mode;
     }
 
+    @Override
+    public void repr(SkylarkPrinter printer) {
+      printer.append(mode);
+    }
+
     /** Returns the names of any crosstool features that correspond to this bitcode mode. */
     public ImmutableList<String> getFeatureNames() {
       return featureNames;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java b/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java
index 8e047c3..d81e698 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java
@@ -22,6 +22,8 @@
 import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
 import com.google.devtools.build.lib.util.Preconditions;
 import java.util.HashMap;
 import java.util.Locale;
@@ -34,7 +36,7 @@
   doc = "Distinguishes between various apple platforms."
 )
 @Immutable
-public enum ApplePlatform {
+public enum ApplePlatform implements SkylarkValue {
   IOS_DEVICE("ios_device", "iPhoneOS", PlatformType.IOS, true),
   IOS_SIMULATOR("ios_simulator", "iPhoneSimulator", PlatformType.IOS, false),
   MACOS("macos", "MacOSX", PlatformType.MACOS, true),
@@ -196,6 +198,11 @@
     return new SkylarkClassObject(constructor, fields);
   }
 
+  @Override
+  public void repr(SkylarkPrinter printer) {
+    printer.append(toString());
+  }
+
   /**
    * Value used to describe Apple platform "type". A {@link ApplePlatform} is implied from a
    * platform type (for example, watchOS) together with a cpu value (for example, armv7).
@@ -207,7 +214,7 @@
     doc = "Describes Apple platform \"type\", such as iOS, tvOS, macOS etc."
   )
   @Immutable
-  public enum PlatformType {
+  public enum PlatformType implements SkylarkValue {
     IOS("ios"),
     WATCHOS("watchos"),
     TVOS("tvos"),
@@ -253,5 +260,10 @@
       }
       return new SkylarkClassObject(constructor, fields);
     }
+
+    @Override
+    public void repr(SkylarkPrinter printer) {
+      printer.append(toString());
+    }
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/DottedVersion.java b/src/main/java/com/google/devtools/build/lib/rules/apple/DottedVersion.java
index 2a38bfe..bdbc910 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/apple/DottedVersion.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/apple/DottedVersion.java
@@ -24,6 +24,8 @@
 import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
 import java.util.ArrayList;
 import java.util.Objects;
 import java.util.regex.Matcher;
@@ -38,7 +40,9 @@
  *
  * <p>Dotted versions are ordered using natural integer sorting on components in order from first to
  * last where any missing element is considered to have the value 0 if they don't contain any
- * non-numeric characters. For example: <pre>
+ * non-numeric characters. For example:
+ *
+ * <pre>
  *   3.1.25 > 3.1.1
  *   3.1.20 > 3.1.2
  *   3.1.1 > 3.1
@@ -51,7 +55,9 @@
  * component with a smaller integer. If the integers are the same, the alphabetic sequences are
  * compared lexicographically, and if <i>they</i> turn out to be the same, the final (optional)
  * integer is compared. As with the leading integer, this final integer is considered to be 0 if not
- * present. For example: <pre>
+ * present. For example:
+ *
+ * <pre>
  *   3.1.1 > 3.1.1beta3
  *   3.1.1beta1 > 3.1.0
  *   3.1 > 3.1.0alpha1
@@ -72,7 +78,7 @@
           + "1.2.3.4."
 )
 @Immutable
-public final class DottedVersion implements Comparable<DottedVersion> {
+public final class DottedVersion implements Comparable<DottedVersion>, SkylarkValue {
   private static final Splitter DOT_SPLITTER = Splitter.on('.');
   private static final Pattern COMPONENT_PATTERN = Pattern.compile("(\\d+)(?:([a-z]+)(\\d*))?");
   private static final String ILLEGAL_VERSION =
@@ -238,6 +244,11 @@
     return ZERO_COMPONENT;
   }
 
+  @Override
+  public void repr(SkylarkPrinter printer) {
+    printer.append(stringRepresentation);
+  }
+
   private static final class Component implements Comparable<Component> {
     private final int firstNumber;
     private final String alphaSequence;
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Printer.java b/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
index 12cc792..3a6c60d 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
@@ -20,6 +20,7 @@
 import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
 import com.google.devtools.build.lib.syntax.SkylarkList.Tuple;
+import com.google.devtools.build.lib.vfs.Path;
 import com.google.devtools.build.lib.vfs.PathFragment;
 import java.io.IOException;
 import java.util.Formattable;
@@ -351,10 +352,10 @@
       } else if (o instanceof Integer || o instanceof Double) {
         this.append(o.toString());
 
-      } else if (o == Boolean.TRUE) {
+      } else if (Boolean.TRUE.equals(o)) {
         this.append("True");
 
-      } else if (o == Boolean.FALSE) {
+      } else if (Boolean.FALSE.equals(o)) {
         this.append("False");
 
       } else if (o instanceof Map<?, ?>) {
@@ -374,6 +375,9 @@
       } else if (o instanceof PathFragment) {
         this.append(((PathFragment) o).getPathString());
 
+      } else if (o instanceof Path) {
+        append(o.toString());
+
       } else if (o instanceof Class<?>) {
         this.append(EvalUtils.getDataTypeNameFromClass((Class<?>) o));