From 6fa82c014c5dd4bc560e0fdd563102f9af7b1ed9 Mon Sep 17 00:00:00 2001 From: ilyas Date: Mon, 1 Mar 2010 14:17:44 +0000 Subject: #3060 fixed --- .../scalap/scalax/rules/scalasig/ScalaSig.scala | 9 +- .../scalax/rules/scalasig/ScalaSigPrinter.scala | 177 ++++++++++++--------- test/files/scalap/typeAnnotations/A.scala | 9 ++ test/files/scalap/typeAnnotations/result.test | 8 + 4 files changed, 123 insertions(+), 80 deletions(-) create mode 100644 test/files/scalap/typeAnnotations/A.scala create mode 100644 test/files/scalap/typeAnnotations/result.test diff --git a/src/scalap/scala/tools/scalap/scalax/rules/scalasig/ScalaSig.scala b/src/scalap/scala/tools/scalap/scalax/rules/scalasig/ScalaSig.scala index a97494ed6d..e21a0656e1 100644 --- a/src/scalap/scala/tools/scalap/scalax/rules/scalasig/ScalaSig.scala +++ b/src/scalap/scala/tools/scalap/scalax/rules/scalasig/ScalaSig.scala @@ -164,21 +164,21 @@ object ScalaSigEntryParsers extends RulesWithState with MemoisableRules { * | 5 ALIASsym len_Nat SymbolInfo * | 6 CLASSsym len_Nat SymbolInfo [thistype_Ref] * | 7 MODULEsym len_Nat SymbolInfo - * | 8 VALsym len_Nat [defaultGetter_Ref] SymbolInfo [alias_Ref] + * | 8 VALsym len_Nat [defaultGetter_Ref /* no longer needed*/] SymbolInfo [alias_Ref] * | 9 EXTref len_Nat name_Ref [owner_Ref] * | 10 EXTMODCLASSref len_Nat name_Ref [owner_Ref] * | 11 NOtpe len_Nat * | 12 NOPREFIXtpe len_Nat * | 13 THIStpe len_Nat sym_Ref * | 14 SINGLEtpe len_Nat type_Ref sym_Ref - * | 15 CONSTANTtpe len_Nat type_Ref constant_Ref + * | 15 CONSTANTtpe len_Nat constant_Ref * | 16 TYPEREFtpe len_Nat type_Ref sym_Ref {targ_Ref} * | 17 TYPEBOUNDStpe len_Nat tpe_Ref tpe_Ref * | 18 REFINEDtpe len_Nat classsym_Ref {tpe_Ref} * | 19 CLASSINFOtpe len_Nat classsym_Ref {tpe_Ref} * | 20 METHODtpe len_Nat tpe_Ref {sym_Ref} * | 21 POLYTtpe len_Nat tpe_Ref {sym_Ref} - * | 22 IMPLICITMETHODtpe len_Nat tpe_Ref {tpe_Ref} + * | 22 IMPLICITMETHODtpe len_Nat tpe_Ref {sym_Ref} /* no longer needed */ * | 52 SUPERtpe len_Nat tpe_Ref tpe_Ref * | 24 LITERALunit len_Nat * | 25 LITERALboolean len_Nat value_Long @@ -195,13 +195,12 @@ object ScalaSigEntryParsers extends RulesWithState with MemoisableRules { * | 36 LITERALenum len_Nat sym_Ref * | 40 SYMANNOT len_Nat sym_Ref AnnotInfoBody * | 41 CHILDREN len_Nat sym_Ref {sym_Ref} - * | 42 ANNOTATEDtpe len_Nat [sym_Ref] tpe_Ref {annotinfo_Ref} + * | 42 ANNOTATEDtpe len_Nat [sym_Ref /* no longer needed */] tpe_Ref {annotinfo_Ref} * | 43 ANNOTINFO len_Nat AnnotInfoBody * | 44 ANNOTARGARRAY len_Nat {constAnnotArg_Ref} * | 47 DEBRUIJNINDEXtpe len_Nat level_Nat index_Nat * | 48 EXISTENTIALtpe len_Nat type_Ref {symbol_Ref} */ - val noSymbol = 3 -^ NoSymbol val typeSymbol = symbolEntry(4) ^^ TypeSymbol as "typeSymbol" val aliasSymbol = symbolEntry(5) ^^ AliasSymbol as "alias" diff --git a/src/scalap/scala/tools/scalap/scalax/rules/scalasig/ScalaSigPrinter.scala b/src/scalap/scala/tools/scalap/scalax/rules/scalasig/ScalaSigPrinter.scala index d4345cdb69..5eb131a7c0 100644 --- a/src/scalap/scala/tools/scalap/scalax/rules/scalasig/ScalaSigPrinter.scala +++ b/src/scalap/scala/tools/scalap/scalax/rules/scalasig/ScalaSigPrinter.scala @@ -25,13 +25,24 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { case class TypeFlags(printRep: Boolean) - def printSymbol(symbol: Symbol) { printSymbol(0, symbol) } + def printSymbol(symbol: Symbol) {printSymbol(0, symbol)} + + def printSymbolAttributes(s: Symbol, onNewLine: Boolean, indent: => Unit) = s match { + case t: SymbolInfoSymbol => { + for (a <- t.attributes) { + indent; print(toString(a)) + if (onNewLine) print("\n") else print(" ") + } + } + case _ => + } def printSymbol(level: Int, symbol: Symbol) { if (!symbol.isLocal && - !(symbol.isPrivate && !printPrivates)) { + !(symbol.isPrivate && !printPrivates)) { def indent() {for (i <- 1 to level) print(" ")} + printSymbolAttributes(symbol, true, indent) symbol match { case o: ObjectSymbol => if (!isCaseClassObject(o)) { @@ -51,8 +62,9 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { case a: AliasSymbol => indent printAlias(level, a) - case t: TypeSymbol => - () + case t: TypeSymbol if !t.isParam => + indent + printTypeSymbol(level, t) case s => } } @@ -99,7 +111,7 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { private def refinementClass(c: ClassSymbol) = c.name == "" def printClass(level: Int, c: ClassSymbol) { - if (c.name == ""/*scala.tools.nsc.symtab.StdNames.LOCALCHILD.toString()*/) { + if (c.name == "" /*scala.tools.nsc.symtab.StdNames.LOCALCHILD.toString()*/ ) { print("\n") } else { printModifiers(c) @@ -125,8 +137,8 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { } def getPrinterByConstructor(c: ClassSymbol) = { - c.children.find{ - case m : MethodSymbol if m.name == CONSTRUCTOR_NAME => true + c.children.find { + case m: MethodSymbol if m.name == CONSTRUCTOR_NAME => true case _ => false } match { case Some(m: MethodSymbol) => @@ -175,7 +187,7 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { if (res.length > 1) StringUtil.decapitalize(res.substring(0, 1)) else res.toLowerCase }) - def printMethodType(t: Type, printResult: Boolean)(implicit cont : => Unit): Unit = { + def printMethodType(t: Type, printResult: Boolean)(implicit cont: => Unit): Unit = { def _pmt(mt: Type {def resultType: Type; def paramSymbols: Seq[Symbol]}) = { @@ -186,7 +198,7 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { // Printe parameter clauses print(paramEntries.mkString( - "(" + (mt match {case _ : ImplicitMethodType => "implicit "; case _ => ""}) + "(" + (mt match {case _: ImplicitMethodType => "implicit "; case _ => ""}) , ", ", ")")) // Print result type @@ -226,7 +238,7 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { printModifiers(m) if (m.isAccessor) { val indexOfSetter = m.parent.get.children.indexWhere(x => x.isInstanceOf[MethodSymbol] && - x.asInstanceOf[MethodSymbol].name == n + "_$eq") + x.asInstanceOf[MethodSymbol].name == n + "_$eq") print(if (indexOfSetter > 0) "var " else "val ") } else { print("def ") @@ -253,35 +265,40 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { printChildren(level, a) } - def printAttributes(sym: SymbolInfoSymbol) { - for (attrib <- sym.attributes) printAttribute(attrib) + def printTypeSymbol(level: Int, t: TypeSymbol) { + print("type ") + print(processName(t.name)) + printType(t.infoType) + print("\n") } - def printAttribute(attrib: AttributeInfo) { - printType(attrib.typeRef, "@") + def toString(attrib: AttributeInfo): String = { + val buffer = new StringBuffer + buffer.append(toString(attrib.typeRef, "@")) if (attrib.value.isDefined) { - print("(") - printValue(attrib.value.get) - print(")") + buffer.append("(") + buffer.append(valueToString(attrib.value.get)) + buffer.append(")") } if (!attrib.values.isEmpty) { - print(" {") + buffer.append(" {") for (name ~ value <- attrib.values) { - print(" val ") - print(processName(name)) - print(" = ") - printValue(value) + buffer.append(" val ") + buffer.append(processName(name)) + buffer.append(" = ") + buffer.append(valueToString(value)) } - printValue(attrib.value) - print(" }") + buffer.append(valueToString(attrib.value)) + buffer.append(" }") } - print(" ") + buffer.append(" ") + buffer.toString } - def printValue(value: Any): Unit = value match { - case t: Type => printType(t) + def valueToString(value: Any): String = value match { + case t: Type => toString(t) // TODO string, char, float, etc. - case _ => print(value) + case _ => value.toString } implicit object _tf extends TypeFlags(false) @@ -294,57 +311,69 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { def toString(t: Type)(implicit flags: TypeFlags): String = toString(t, "")(flags) - def toString(t: Type, sep: String)(implicit flags: TypeFlags): String = t match { - case ThisType(symbol) => sep + processName(symbol.path) + ".type" - case SingleType(typeRef, symbol) => sep + processName(symbol.path) + ".type" - case ConstantType(constant) => sep + (constant match { - case null => "scala.Null" - case _: Unit => "scala.Unit" - case _: Boolean => "scala.Boolean" - case _: Byte => "scala.Byte" - case _: Char => "scala.Char" - case _: Short => "scala.Short" - case _: Int => "scala.Int" - case _: Long => "scala.Long" - case _: Float => "scala.Float" - case _: Double => "scala.Double" - case _: String => "java.lang.String" - case c: Class[_] => "java.lang.Class[" + c.getComponentType.getCanonicalName.replace("$", ".") + "]" - }) - case TypeRefType(prefix, symbol, typeArgs) => sep + (symbol.path match { - case "scala." => flags match { - case TypeFlags(true) => toString(typeArgs.head) + "*" - case _ => "scala.Seq" + typeArgString(typeArgs) + def toString(t: Type, sep: String)(implicit flags: TypeFlags): String = { + // print type itself + t match { + case ThisType(symbol) => sep + processName(symbol.path) + ".type" + case SingleType(typeRef, symbol) => sep + processName(symbol.path) + ".type" + case ConstantType(constant) => sep + (constant match { + case null => "scala.Null" + case _: Unit => "scala.Unit" + case _: Boolean => "scala.Boolean" + case _: Byte => "scala.Byte" + case _: Char => "scala.Char" + case _: Short => "scala.Short" + case _: Int => "scala.Int" + case _: Long => "scala.Long" + case _: Float => "scala.Float" + case _: Double => "scala.Double" + case _: String => "java.lang.String" + case c: Class[_] => "java.lang.Class[" + c.getComponentType.getCanonicalName.replace("$", ".") + "]" + }) + case TypeRefType(prefix, symbol, typeArgs) => sep + (symbol.path match { + case "scala." => flags match { + case TypeFlags(true) => toString(typeArgs.head) + "*" + case _ => "scala.Seq" + typeArgString(typeArgs) + } + case "scala." => "=> " + toString(typeArgs.head) + case _ => { + val path = StringUtil.cutSubstring(symbol.path)(".package") //remove package object reference + StringUtil.trimStart(processName(path) + typeArgString(typeArgs), ".") + } + }) + case TypeBoundsType(lower, upper) => { + val lb = toString(lower) + val ub = toString(upper) + val lbs = if (!lb.equals("scala.Nothing")) " >: " + lb else "" + val ubs = if (!ub.equals("scala.Any")) " <: " + ub else "" + lbs + ubs } - case "scala." => "=> " + toString(typeArgs.head) - case _ => { - val path = StringUtil.cutSubstring(symbol.path)(".package") //remove package object reference - StringUtil.trimStart(processName(path) + typeArgString(typeArgs), ".") + case RefinedType(classSym, typeRefs) => sep + typeRefs.map(toString).mkString("", " with ", "") + case ClassInfoType(symbol, typeRefs) => sep + typeRefs.map(toString).mkString(" extends ", " with ", "") + + case ImplicitMethodType(resultType, _) => toString(resultType, sep) + case MethodType(resultType, _) => toString(resultType, sep) + + case PolyType(typeRef, symbols) => typeParamString(symbols) + toString(typeRef, sep) + case PolyTypeWithCons(typeRef, symbols, cons) => typeParamString(symbols) + processName(cons) + toString(typeRef, sep) + case AnnotatedType(typeRef, attribTreeRefs) => { + toString(typeRef, sep) } - }) - case TypeBoundsType(lower, upper) => " >: " + toString(lower) + " <: " + toString(upper) - case RefinedType(classSym, typeRefs) => sep + typeRefs.map(toString).mkString("", " with ", "") - case ClassInfoType(symbol, typeRefs) => sep + typeRefs.map(toString).mkString(" extends ", " with ", "") - - case ImplicitMethodType(resultType, _) => toString(resultType, sep) - case MethodType(resultType, _) => toString(resultType, sep) - - case PolyType(typeRef, symbols) => typeParamString(symbols) + toString(typeRef, sep) - case PolyTypeWithCons(typeRef, symbols, cons) => typeParamString(symbols) + processName(cons) + toString(typeRef, sep) - case AnnotatedType(typeRef, attribTreeRefs) => toString(typeRef, sep) - case AnnotatedWithSelfType(typeRef, symbol, attribTreeRefs) => toString(typeRef, sep) - //case DeBruijnIndexType(typeLevel, typeIndex) => - case ExistentialType(typeRef, symbols) => { - val refs = symbols.map(toString _).filter(!_.startsWith("_ ")).map("type " + _) - toString(typeRef, sep) + (if (refs.size > 0) refs.mkString(" forSome {", "; ", "}") else "") + case AnnotatedWithSelfType(typeRef, symbol, attribTreeRefs) => toString(typeRef, sep) + //case DeBruijnIndexType(typeLevel, typeIndex) => + case ExistentialType(typeRef, symbols) => { + val refs = symbols.map(toString _).filter(!_.startsWith("_ ")).map("type " + _) + toString(typeRef, sep) + (if (refs.size > 0) refs.mkString(" forSome {", "; ", "}") else "") + } + case _ => sep + t.toString } - case _ => sep + t.toString } def getVariance(t: TypeSymbol) = if (t.isCovariant) "+" else if (t.isContravariant) "-" else "" def toString(symbol: Symbol): String = symbol match { - case symbol: TypeSymbol => getVariance(symbol) + processName(symbol.name) + toString(symbol.infoType) + case symbol: TypeSymbol => (for (a <- symbol.attributes) yield toString(a)).mkString("") + + getVariance(symbol) + processName(symbol.name) + toString(symbol.infoType) case s => symbol.toString } @@ -352,11 +381,9 @@ class ScalaSigPrinter(stream: PrintStream, printPrivates: Boolean) { if (typeArgs.isEmpty) "" else typeArgs.map(toString).map(StringUtil.trimStart(_, "=> ")).mkString("[", ", ", "]") - def typeParamString(params: Seq[Symbol]): String = { - val res = if (params.isEmpty) "" + def typeParamString(params: Seq[Symbol]): String = + if (params.isEmpty) "" else params.map(toString).mkString("[", ", ", "]") - res.replace(" >: scala.Nothing", "").replace(" <: scala.Any", "") - } val _syms = Map("\\$bar" -> "|", "\\$tilde" -> "~", "\\$bang" -> "!", "\\$up" -> "^", "\\$plus" -> "+", diff --git a/test/files/scalap/typeAnnotations/A.scala b/test/files/scalap/typeAnnotations/A.scala new file mode 100644 index 0000000000..582746764e --- /dev/null +++ b/test/files/scalap/typeAnnotations/A.scala @@ -0,0 +1,9 @@ +abstract class AbsFun[@specialized R] { + @specialized val x = 10 + @specialized type T + + def compose[@specialized A](x: A, y: R): A = { + val y: A = x + x + } +} \ No newline at end of file diff --git a/test/files/scalap/typeAnnotations/result.test b/test/files/scalap/typeAnnotations/result.test new file mode 100644 index 0000000000..d48b3a2178 --- /dev/null +++ b/test/files/scalap/typeAnnotations/result.test @@ -0,0 +1,8 @@ +abstract class AbsFun[@scala.specialized R] extends java.lang.Object with scala.ScalaObject { + def this() = { /* compiled code */ } + @scala.specialized + val x : scala.Int = { /* compiled code */ } + @scala.specialized + type T + def compose[@scala.specialized A](x : A, y : R) : A = { /* compiled code */ } +} \ No newline at end of file -- cgit v1.2.3