-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Backend backport2 #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Backend backport2 #124
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
afb2195
Constructors stub
DarkDimius 0b585c6
CollectEntryPoints.
DarkDimius ffe0131
Helper method to get all members of type.
DarkDimius 9e5cfe2
Fix error in box\unbox logic.
DarkDimius 026b86d
Fix TailRec to use Label flag.
DarkDimius 54034c1
Definitions used by backend.
DarkDimius a0dd4ca
Fix invalid flags in lazy vals.
DarkDimius 2001062
Better error message if position of tree isn't set.
DarkDimius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/dotty/tools/dotc/transform/CollectEntryPoints.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package dotty.tools.dotc.transform | ||
|
||
import dotty.tools.dotc.transform.TreeTransforms.{TransformerInfo, TreeTransform, TreeTransformer} | ||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import scala.collection.mutable.ListBuffer | ||
import dotty.tools.dotc.core.{Scopes, Flags} | ||
import dotty.tools.dotc.core.Symbols.NoSymbol | ||
import scala.annotation.tailrec | ||
import dotty.tools.dotc.core._ | ||
import Symbols._ | ||
import scala.Some | ||
import dotty.tools.dotc.transform.TreeTransforms.{NXTransformations, TransformerInfo, TreeTransform, TreeTransformer} | ||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import scala.collection.mutable | ||
import dotty.tools.dotc.core.Names.Name | ||
import NameOps._ | ||
import Types._ | ||
import scala.collection.SortedSet | ||
import Decorators._ | ||
import StdNames._ | ||
import dotty.tools.dotc.util.Positions.Position | ||
import dotty.tools.dotc.config.JavaPlatform | ||
|
||
class CollectEntryPoints extends TreeTransform { | ||
|
||
/** perform context-dependant initialization */ | ||
override def init(implicit ctx: Context, info: TransformerInfo): Unit = { | ||
entryPoints = collection.immutable.TreeSet.empty[Symbol](new SymbolOrdering()) | ||
assert(ctx.platform.isInstanceOf[JavaPlatform], "Java platform specific phase") | ||
} | ||
|
||
private var entryPoints: Set[Symbol] = _ | ||
|
||
def getEntryPoints = entryPoints.toList | ||
|
||
override def name: String = "collectEntryPoints" | ||
override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { | ||
if (tree.symbol.owner.isClass && isJavaEntryPoint(tree.symbol)) { | ||
// collecting symbols for entry points here (as opposed to GenBCode where they are used) | ||
// has the advantage of saving an additional pass over all ClassDefs. | ||
entryPoints += tree.symbol | ||
} | ||
tree | ||
} | ||
|
||
def isJavaEntryPoint(sym: Symbol)(implicit ctx: Context): Boolean = { | ||
def fail(msg: String, pos: Position = sym.pos) = { | ||
ctx.warning(sym.name + | ||
s" has a main method with parameter type Array[String], but ${sym.fullName} will not be a runnable program.\n Reason: $msg", | ||
sourcePos(sym.pos) | ||
// TODO: make this next claim true, if possible | ||
// by generating valid main methods as static in module classes | ||
// not sure what the jvm allows here | ||
// + " You can still run the program by calling it as " + javaName(sym) + " instead." | ||
) | ||
false | ||
} | ||
def failNoForwarder(msg: String) = { | ||
fail(s"$msg, which means no static forwarder can be generated.\n") | ||
} | ||
val possibles = if (sym.flags is Flags.Module) (sym.info nonPrivateMember nme.main).alternatives else Nil | ||
val hasApproximate = possibles exists { | ||
m => | ||
m.info match { | ||
case MethodType(_, p :: Nil) => | ||
p.typeSymbol == defn.ArrayClass | ||
case _ => false | ||
} | ||
} | ||
def precise(implicit ctx: Context) = { | ||
val companion = sym.companionClass //sym.asClass.linkedClassOfClass | ||
val javaPlatform = ctx.platform.asInstanceOf[JavaPlatform] | ||
if (javaPlatform.hasJavaMainMethod(companion)) | ||
failNoForwarder("companion contains its own main method") | ||
else if (companion != NoSymbol && companion.info.member(nme.main) != NoSymbol) | ||
// this is only because forwarders aren't smart enough yet | ||
failNoForwarder("companion contains its own main method (implementation restriction: no main is allowed, regardless of signature)") | ||
else if (companion.flags is Flags.Trait) | ||
failNoForwarder("companion is a trait") | ||
// Now either succeeed, or issue some additional warnings for things which look like | ||
// attempts to be java main methods. | ||
else (possibles exists (x => javaPlatform.isJavaMainMethod(x.symbol))) || { | ||
possibles exists { | ||
m => | ||
m.symbol.info match { | ||
case t: PolyType => | ||
fail("main methods cannot be generic.") | ||
case t@MethodType(paramNames, paramTypes) => | ||
if (t.resultType :: paramTypes exists (_.typeSymbol.isAbstractType)) | ||
fail("main methods cannot refer to type parameters or abstract types.", m.symbol.pos) | ||
else | ||
javaPlatform.isJavaMainMethod(m.symbol) || fail("main method must have exact signature (Array[String])Unit", m.symbol.pos) | ||
case tp => | ||
fail(s"don't know what this is: $tp", m.symbol.pos) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// At this point it's a module with a main-looking method, so either succeed or warn that it isn't. | ||
hasApproximate && precise(ctx.withPhase(ctx.erasurePhase)) | ||
// Before erasure so we can identify generic mains. | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
class SymbolOrdering(implicit ctx: Context) extends Ordering[Symbol] { | ||
override def compare(x: Symbol, y: Symbol): Int = { | ||
x.fullName.toString.compareTo(y.fullName.toString) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dotty.tools.dotc.transform | ||
|
||
import TreeTransforms._ | ||
import dotty.tools.dotc.ast.tpd._ | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.StdNames._ | ||
|
||
/** This transform moves initializers from body to constructor. | ||
* Right now it's a dummy. | ||
* Awaiting for real implemetation | ||
*/ | ||
class Constructors extends TreeTransform { | ||
|
||
override def name: String = "constructors" | ||
override def transformDefDef(tree: DefDef)(implicit ctx: Context, info: TransformerInfo): Tree = { | ||
if(tree.symbol.isClassConstructor) { | ||
val claz = tree.symbol.enclosingClass.asClass | ||
val zuper = claz.info.parents.head.typeSymbol | ||
cpy.DefDef(tree, tree.mods, tree.name, tree.tparams, tree.vparamss, tree.tpt, rhs = { | ||
val parentCall = Apply(Select(Super(This(claz), tpnme.EMPTY, true), zuper.primaryConstructor), Nil) | ||
if(tree.rhs.isEmpty) parentCall | ||
else Block(List(parentCall), tree.rhs) | ||
|
||
}) | ||
} else tree | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it matters in Dotty, but in scalac these should be
def
-s, notvals
. Why? Subsequent runs of the compiler might recompileArray
(this happens if you navigate to its sources in Scala IDE, for example), which would result in new symbols for its members.That's why I split out
RunDefinitions
in scalac, so we could have correctness and speed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Dotty definitions are part of context and I would say that in case discarding is required, the better approach would be to discard context itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed we should do the same thing. (Keep them as lazyvals but break them
out into a new class) It's planned by has not been done yet. - Martin
On Tue, May 6, 2014 at 4:11 PM, Jason Zaugg [email protected]:
Martin Odersky
EPFL
JOIN US. REGISTER TODAY! http://www.scaladays.org/
Scala http://www.scaladays.org/
Days http://www.scaladays.org/
June 16th-18th, http://www.scaladays.org/
Berlin http://www.scaladays.org/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Martin, why do you need a new class for this? Isn't Definitions itself a class that can be discarded with context holding it.