aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/dotty/tools/dotc/core/NameOps.scala
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/src/dotty/tools/dotc/core/NameOps.scala')
-rw-r--r--compiler/src/dotty/tools/dotc/core/NameOps.scala26
1 files changed, 25 insertions, 1 deletions
diff --git a/compiler/src/dotty/tools/dotc/core/NameOps.scala b/compiler/src/dotty/tools/dotc/core/NameOps.scala
index ea905c19f..240ad359b 100644
--- a/compiler/src/dotty/tools/dotc/core/NameOps.scala
+++ b/compiler/src/dotty/tools/dotc/core/NameOps.scala
@@ -196,6 +196,31 @@ object NameOps {
if (name.isModuleClassName) name.stripModuleClassSuffix.freshened.moduleClassName
else likeTyped(ctx.freshName(name ++ NameTransformer.NAME_JOIN_STRING)))
+ /** Name with variance prefix: `+` for covariant, `-` for contravariant */
+ def withVariance(v: Int): N =
+ if (hasVariance) dropVariance.withVariance(v)
+ else v match {
+ case -1 => likeTyped('-' +: name)
+ case 1 => likeTyped('+' +: name)
+ case 0 => name
+ }
+
+ /** Does name have a `+`/`-` variance prefix? */
+ def hasVariance: Boolean =
+ name.nonEmpty && name.head == '+' || name.head == '-'
+
+ /** Drop variance prefix if name has one */
+ def dropVariance: N = if (hasVariance) likeTyped(name.tail) else name
+
+ /** The variance as implied by the variance prefix, or 0 if there is
+ * no variance prefix.
+ */
+ def variance = name.head match {
+ case '-' => -1
+ case '+' => 1
+ case _ => 0
+ }
+
/** Translate a name into a list of simple TypeNames and TermNames.
* In all segments before the last, type/term is determined by whether
* the following separator char is '.' or '#'. The last segment
@@ -271,7 +296,6 @@ object NameOps {
else -1
}
-
/** The number of hops specified in an outer-select name */
def outerSelectHops: Int = {
require(isOuterSelect)