summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/ast
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-11-04 00:07:36 +0000
committerPaul Phillips <paulp@improving.org>2010-11-04 00:07:36 +0000
commit379af580e2c8cf0ce5309fc0b31702f79e415abe (patch)
treecf741eb0d27624d6130bb5bc7f403ec7fb05af1d /src/compiler/scala/tools/nsc/ast
parentde012b3a6d04f1e7a9fd6fecd403e0492f6ad7c1 (diff)
downloadscala-379af580e2c8cf0ce5309fc0b31702f79e415abe.tar.gz
scala-379af580e2c8cf0ce5309fc0b31702f79e415abe.tar.bz2
scala-379af580e2c8cf0ce5309fc0b31702f79e415abe.zip
Determined that half a dozen ways of checking f...
Determined that half a dozen ways of checking for varargs and by-name-ness in param lists exceeded the legal limit. Also assessed that names which are only used as type names would be a lot easier to deal with if we created them as type names up front. Performed the changes implied by the preceding along with a partial cleanup on TreeInfo which one can see hasn't had a good look in a long time. (And still hasn't.) No review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/ast')
-rw-r--r--src/compiler/scala/tools/nsc/ast/TreeGen.scala2
-rw-r--r--src/compiler/scala/tools/nsc/ast/TreeInfo.scala84
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/Parsers.scala26
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala2
4 files changed, 54 insertions, 60 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/TreeGen.scala b/src/compiler/scala/tools/nsc/ast/TreeGen.scala
index a47200a63d..073bebad04 100644
--- a/src/compiler/scala/tools/nsc/ast/TreeGen.scala
+++ b/src/compiler/scala/tools/nsc/ast/TreeGen.scala
@@ -316,7 +316,7 @@ abstract class TreeGen {
Apply(Select(monitor, Object_synchronized), List(body))
def wildcardStar(tree: Tree) =
- atPos(tree.pos) { Typed(tree, Ident(nme.WILDCARD_STAR.toTypeName)) }
+ atPos(tree.pos) { Typed(tree, Ident(nme.WILDCARD_STAR)) }
def paramToArg(vparam: Symbol) = {
val arg = Ident(vparam)
diff --git a/src/compiler/scala/tools/nsc/ast/TreeInfo.scala b/src/compiler/scala/tools/nsc/ast/TreeInfo.scala
index 443a5a724b..8be793343e 100644
--- a/src/compiler/scala/tools/nsc/ast/TreeInfo.scala
+++ b/src/compiler/scala/tools/nsc/ast/TreeInfo.scala
@@ -16,14 +16,10 @@ import util.HashSet
* @version 1.0
*/
abstract class TreeInfo {
-
val trees: SymbolTable
import trees._
import definitions.ThrowableClass
- def isTerm(tree: Tree): Boolean = tree.isTerm
- def isType(tree: Tree): Boolean = tree.isType
-
def isOwnerDefinition(tree: Tree): Boolean = tree match {
case PackageDef(_, _)
| ClassDef(_, _, _, _)
@@ -101,9 +97,9 @@ abstract class TreeInfo {
}
def mayBeVarGetter(sym: Symbol) = sym.info match {
- case PolyType(List(), _) => sym.owner.isClass && !sym.isStable
- case mt: MethodType => mt.isImplicit && sym.owner.isClass && !sym.isStable
- case _ => false
+ case PolyType(Nil, _) => sym.owner.isClass && !sym.isStable
+ case mt @ MethodType(_, _) => mt.isImplicit && sym.owner.isClass && !sym.isStable
+ case _ => false
}
def isVariableOrGetter(tree: Tree) = {
@@ -135,17 +131,14 @@ abstract class TreeInfo {
case _ => false
}
- def isSelfOrSuperConstrCall(tree: Tree): Boolean = methPart(tree) match {
- case Ident(nme.CONSTRUCTOR)
- | Select(This(_), nme.CONSTRUCTOR)
- | Select(Super(_, _), nme.CONSTRUCTOR) => true
- case _ => false
- }
+ def isSelfOrSuperConstrCall(tree: Tree) =
+ isSelfConstrCall(tree) || isSuperConstrCall(tree)
/** Is tree a variable pattern */
def isVarPattern(pat: Tree): Boolean = pat match {
- case Ident(name) => isVariableName(name) && !pat.isInstanceOf[BackQuotedIdent]
- case _ => false
+ case _: BackQuotedIdent => false
+ case x: Ident => isVariableName(x.name)
+ case _ => false
}
/** The first constructor definitions in `stats' */
@@ -162,7 +155,7 @@ abstract class TreeInfo {
/** The value definitions marked PRESUPER in this statement sequence */
def preSuperFields(stats: List[Tree]): List[ValDef] =
- for (vdef @ ValDef(mods, _, _, _) <- stats if mods hasFlag PRESUPER) yield vdef
+ stats collect { case vd: ValDef if isEarlyValDef(vd) => vd }
def isEarlyDef(tree: Tree) = tree match {
case TypeDef(mods, _, _, _) => mods hasFlag PRESUPER
@@ -180,25 +173,23 @@ abstract class TreeInfo {
case _ => false
}
- /** Is type a of the form T* ? */
+ /** Is tpt of the form T* ? */
def isRepeatedParamType(tpt: Tree) = tpt match {
- case AppliedTypeTree(Select(_, rp), _) =>
- rp == nme.REPEATED_PARAM_CLASS_NAME.toTypeName ||
- rp == nme.JAVA_REPEATED_PARAM_CLASS_NAME.toTypeName
- case TypeTree() => definitions.isRepeatedParamType(tpt.tpe)
- case _ => false
+ case TypeTree() => definitions.isRepeatedParamType(tpt.tpe)
+ case AppliedTypeTree(Select(_, nme.REPEATED_PARAM_CLASS_NAME), _) => true
+ case AppliedTypeTree(Select(_, nme.JAVA_REPEATED_PARAM_CLASS_NAME), _) => true
+ case _ => false
}
/** Is tpt a by-name parameter type? */
def isByNameParamType(tpt: Tree) = tpt match {
- case AppliedTypeTree(Select(_, n), _) => n == nme.BYNAME_PARAM_CLASS_NAME.toTypeName
- case TypeTree() => tpt.tpe.typeSymbol == definitions.ByNameParamClass
- case _ => false
+ case TypeTree() => definitions.isByNameParamType(tpt.tpe)
+ case AppliedTypeTree(Select(_, nme.BYNAME_PARAM_CLASS_NAME), _) => true
+ case _ => false
}
/** Is name a left-associative operator? */
- def isLeftAssoc(operator: Name): Boolean =
- operator.length > 0 && operator(operator.length - 1) != ':'
+ def isLeftAssoc(operator: Name) = operator.nonEmpty && (operator.endChar != ':')
private val reserved = Set[Name](nme.false_, nme.true_, nme.null_)
@@ -216,26 +207,33 @@ abstract class TreeInfo {
/** can this type be a type pattern */
def mayBeTypePat(tree: Tree): Boolean = tree match {
- case CompoundTypeTree(Template(tps, _, List())) => tps exists mayBeTypePat
- case Annotated(_, tp) => mayBeTypePat(tp)
- case AppliedTypeTree(constr, args) =>
- mayBeTypePat(constr) || args.exists(_.isInstanceOf[Bind])
- case SelectFromTypeTree(tp, _) => mayBeTypePat(tp)
- case _ => false
+ case CompoundTypeTree(Template(tps, _, Nil)) => tps exists mayBeTypePat
+ case Annotated(_, tp) => mayBeTypePat(tp)
+ case AppliedTypeTree(constr, args) => mayBeTypePat(constr) || args.exists(_.isInstanceOf[Bind])
+ case SelectFromTypeTree(tp, _) => mayBeTypePat(tp)
+ case _ => false
}
/** Is this argument node of the form <expr> : _* ?
*/
def isWildcardStarArg(tree: Tree): Boolean = tree match {
- case Typed(expr, Ident(name)) => name == nme.WILDCARD_STAR.toTypeName
- case _ => false
+ case Typed(_, Ident(nme.WILDCARD_STAR)) => true
+ case _ => false
+ }
+ def isWildcardStarArgList(trees: List[Tree]) =
+ trees.nonEmpty && isWildcardStarArg(trees.last)
+
+ /** Is the argument a (possibly bound) _ arg?
+ */
+ def isWildcardArg(tree: Tree): Boolean = unbind(tree) match {
+ case Ident(nme.WILDCARD) => true
+ case _ => false
}
/** Is this pattern node a catch-all (wildcard or variable) pattern? */
def isDefaultCase(cdef: CaseDef) = cdef match {
- case CaseDef(Ident(nme.WILDCARD), EmptyTree, _) => true
- case CaseDef(Bind(_, Ident(nme.WILDCARD)), EmptyTree, _) => true
- case _ => false
+ case CaseDef(pat, EmptyTree, _) => isWildcardArg(pat)
+ case _ => false
}
/** Does this CaseDef catch Throwable? */
@@ -280,12 +278,10 @@ abstract class TreeInfo {
*/
/** Is this pattern node a sequence-valued pattern? */
- def isSequenceValued(tree: Tree): Boolean = tree match {
- case Bind(_, body) => isSequenceValued(body)
- case ArrayValue(_, _) => true
- case Star(_) => true
- case Alternative(ts) => ts exists isSequenceValued
- case _ => false
+ def isSequenceValued(tree: Tree): Boolean = unbind(tree) match {
+ case Alternative(ts) => ts exists isSequenceValued
+ case ArrayValue(_, _) | Star(_) => true
+ case _ => false
}
/** The underlying pattern ignoring any bindings */
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
index b3aaf02195..5a63b49aac 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
@@ -443,14 +443,12 @@ self =>
def errorTermTree = Literal(Constant(null)).setPos(o2p(in.offset))
def errorPatternTree = Ident(nme.WILDCARD).setPos(o2p(in.offset))
- /** Check that type parameter is not by name T* */
- def checkNotByName(t: Tree) = t match {
- case AppliedTypeTree(Select(_, n), _) =>
- if (n == nme.BYNAME_PARAM_CLASS_NAME.toTypeName)
- syntaxError(t.pos, "no by-name parameter type allowed here", false)
- else if (n == nme.REPEATED_PARAM_CLASS_NAME.toTypeName)
- syntaxError(t.pos, "no * parameter type allowed here", false)
- case _ =>
+ /** Check that type parameter is not by name or repeated */
+ def checkNotByNameOrVarargs(tpt: Tree) = {
+ if (treeInfo isByNameParamType tpt)
+ syntaxError(tpt.pos, "no by-name parameter type allowed here", false)
+ else if (treeInfo isRepeatedParamType tpt)
+ syntaxError(tpt.pos, "no * parameter type allowed here", false)
}
/** Check that tree is a legal clause of a forSome */
@@ -867,7 +865,7 @@ self =>
makeFunctionTypeTree(ts, typ(isPattern))
}
else {
- ts foreach checkNotByName
+ ts foreach checkNotByNameOrVarargs
val tuple = atPos(start) { makeTupleType(ts, true) }
infixTypeRest(
compoundTypeRest(
@@ -1036,12 +1034,12 @@ self =>
// copy-paste (with change) from def paramType
if (in.token == ARROW) {
in.nextToken()
- val tycon = atPos(start) { rootScalaDot(nme.BYNAME_PARAM_CLASS_NAME.toTypeName) }
+ val tycon = atPos(start) { rootScalaDot(nme.BYNAME_PARAM_CLASS_NAME) }
atPos(start) { AppliedTypeTree(tycon, List(typ())) }
} else {
val t = typ()
if (isIdent && in.name == STAR) {
- val tycon = atPos(in.skipToken()) { rootScalaDot(nme.REPEATED_PARAM_CLASS_NAME.toTypeName) }
+ val tycon = atPos(in.skipToken()) { rootScalaDot(nme.REPEATED_PARAM_CLASS_NAME) }
atPos(start) { AppliedTypeTree(tycon, List(t)) }
} else t
}
@@ -1195,7 +1193,7 @@ self =>
if (isIdent && in.name == nme.STAR) {
in.nextToken()
t = atPos(t.pos.startOrPoint, colonPos) {
- Typed(t, atPos(uscorePos) { Ident(nme.WILDCARD_STAR.toTypeName) })
+ Typed(t, atPos(uscorePos) { Ident(nme.WILDCARD_STAR) })
}
} else {
syntaxErrorOrIncomplete("`*' expected", true)
@@ -1917,7 +1915,7 @@ self =>
if (in.token == ARROW) {
atPos(in.skipToken()) {
AppliedTypeTree(
- rootScalaDot(nme.BYNAME_PARAM_CLASS_NAME.toTypeName), List(typ()))
+ rootScalaDot(nme.BYNAME_PARAM_CLASS_NAME), List(typ()))
}
} else {
val t = typ()
@@ -1925,7 +1923,7 @@ self =>
in.nextToken()
atPos(t.pos.startOrPoint, t.pos.point) {
AppliedTypeTree(
- rootScalaDot(nme.REPEATED_PARAM_CLASS_NAME.toTypeName), List(t))
+ rootScalaDot(nme.REPEATED_PARAM_CLASS_NAME), List(t))
}
} else t
}
diff --git a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala
index 86c79eb733..f0d1c536c4 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala
@@ -58,7 +58,7 @@ abstract class SymbolicXMLBuilder(p: Parsers#Parser, preserveWS: Boolean)
private def LL[A](x: A*): List[List[A]] = List(List(x:_*))
private def const(x: Any) = Literal(Constant(x))
private def wild = Ident(nme.WILDCARD)
- private def wildStar = Ident(nme.WILDCARD_STAR.toTypeName)
+ private def wildStar = Ident(nme.WILDCARD_STAR)
private def _scala(name: Name) = Select(Select(Ident(nme.ROOTPKG), nme.scala_), name)
private def _scala_xml(name: Name) = Select(_scala(_xml), name)