diff --git a/.gitignore b/.gitignore index 95d95ca1158a..ce4e4a44058a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.class *.log *~ +*.swp # sbt specific dist/* @@ -30,3 +31,10 @@ classes/ tests/partest-generated/ tests/locks/ /test-classes/ + +# Used in tests +scala-scala + +# Ignore output files but keep the directory +out/ +!out/.keep diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c8061b4f8168..000000000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: scala -script: - - sbt -Ddotty.travis.build=yes update compile test scalastyle -jdk: - - oraclejdk8 -notifications: - email: - - dotty-reports@googlegroups.com -branches: - only: - - master -before_install: - - cd .. - - git clone https://github.com/scala/scala.git - - cd dotty diff --git a/README.md b/README.md index d7d23a6257b7..ec9f72375421 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,85 @@ dotty ===== -[![Build Status](https://travis-ci.org/lampepfl/dotty.png?branch=master)](https://travis-ci.org/lampepfl/dotty) +[![Join the chat at https://gitter.im/lampepfl/dotty](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/lampepfl/dotty?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -Dotty is a platform to try out new language concepts and compiler technologies for Scala. The focus is mainly on simplification. We remove extraneous syntax (e.g. no XML literals), and try to boil down Scala's types into a smaller set of more fundamental constructors. The theory behind these constructors is researched in [DOT](http://www.cs.uwm.edu/~boyland/fool2012/papers/fool2012_submission_3.pdf), a calculus for dependent object types. +Dotty is a platform to try out new language concepts and compiler +technologies for Scala. The focus is mainly on simplification. We +remove extraneous syntax (e.g. no XML literals), and try to boil down +Scala's types into a smaller set of more fundamental constructors. The +theory behind these constructors is researched in +[DOT](http://www.cs.uwm.edu/~boyland/fool2012/papers/fool2012_submission_3.pdf), +a calculus for dependent object types. -The dotty compiler is largely a new design. It takes a more functional approach than current scalac, paired with aggressive caching to achieve good performance. At present, only the frontend (parser and type-checker) exists; the transformation and code generation phases remain to be written. Some parts, in particular those that have to do with configuration and input/output are derived from the Scala compiler. +####Current status: + _Technology preview_: currently unsupported, may be functionally incomplete or unsuitable for production use. -We expect that, over time, some of the new technologies explored in this project will find their way into future versions of Scala. At present it is too early to say which ones and when. +####Is it going to be the future Scala? +Yes, eventually. -If you want to try it out, to get started have a look at https://github.com/lampepfl/dotty/wiki/Getting-Started. +####Who's working on it? +See [github contributors page](https://github.com/lampepfl/dotty/graphs/contributors). + +####What are the features that could make me consider trying it? +| Feature | Status | +|---------------------------------------------------------------------------------------------------------|---------------------| +| Union, Intersection and [Literal singleton types](http://docs.scala-lang.org/sips/pending/42.type.html) | Implemented | +| Fast compilation(phase fusion) | Implemented | +| [Trait parameters](http://docs.scala-lang.org/sips/pending/trait-parameters.html) | Implemented | +| [@Static methods and fields](https://github.com/scala/scala.github.com/pull/491) | Implemented | +| Colored Repl | Implemented | +| Sbt incremental build | Implemented | +| Non-blocking lazy vals | Implemented | +| Option-less pattern matching(based on [name-based patmat](https://github.com/scala/scala/pull/2848)) | Implemented | +| Function arity adaptation | Implemented | +| | | +| Non-boxed arrays of value classes | In progress | +| Working contravariant implicits | In progress | +| [Auto-Specialization](https://github.com/dotty-linker/dotty) | In progress | +| [Whole program optimizer](https://github.com/dotty-linker/dotty) | In progress | +| [Library defined optimizations](https://github.com/dotty-linker/dotty) | In progress | +| | | +| HList & HMaps\Record types | Under consideration | +| Implicit functions | Under consideration | +| Effects | Under consideration | +| Auto-completion in repl | Under consideration | +| Spec Option-less pattern matching | Under consideration | +| Multiverse equality | Under consideration | +| Exhaustivity checks in pattern matching | Under consideration | +There are also plethora of small details such as [per-callsite @tailrec annotations](https://github.com/lampepfl/dotty/issues/1221) -Developers mailing list is https://groups.google.com/forum/#!forum/dotty-internals. +####What are the complications that I can have If I start using Dotty? +Dotty can use libraries compiled by scalac 2.11, but Scala scalac can't use libraries compiled by Dotty.
+No existential types.
+No macro support yet. We have big plans here.
+No early initializers. No scala.DelayedInit. Use trait parameters instead.
+Whole program optimizer will only work if all dependencies are compiled by Dotty.
+ + +####Can I write my code in a way that is going to be compatible with Scalac & Dotty? +Yes, Dotty itself is a project that can be compiled by both Dotty and Scalac.
+It's not very hard, and the biggest thing that you will likely miss is using macros. +####How can I try it out? +https://github.com/lampepfl/dotty/wiki/Getting-Started. +Here’s an example sbt project and instructions on how to set it up: https://github.com/smarter/dotty-example-project/
+We have colored REPL :-). You can invoke it by running `dotc -repl`. -  +####We also have: +Basic support for Scala.js,
+[Prototype](https://github.com/scala-native/scala-native/tree/topic/dotty-support) of compilation to x86 native code(Shabalin)
-  +####What about scalac: +Scalac is the basis for stability in scala. We expect scalac & dotty to coexist for long time. + +####Contributions are welcome! +We invite you to help us build the future of Scala.
+This is the best moment to participate, as everyone can make an impact.
-![YourKit](https://www.yourkit.com/images/yklogo.png) supports open source projects with its full-featured Java Profiler. +####SI-2712? +If scalac will put it into 2.12, we’ll mimic their behaviour. But we have bigger plans for +HK-types. -YourKit, LLC is the creator of [YourKit Java Profiler](https://www.yourkit.com/java/profiler/index.jsp). + + +Developers mailing list is https://groups.google.com/forum/#!forum/dotty-internals. diff --git a/bench/src/test/scala/TestsAsBenchmarks.scala b/bench/src/test/scala/TestsAsBenchmarks.scala index a078c9f787e0..cac34640fc26 100644 --- a/bench/src/test/scala/TestsAsBenchmarks.scala +++ b/bench/src/test/scala/TestsAsBenchmarks.scala @@ -5,21 +5,54 @@ import java.lang.reflect.Method import org.junit.runner.Request import org.junit.runner.notification.RunNotifier +import org.scalameter.Key.reports._ import org.scalameter.PerformanceTest.OnlineRegressionReport import org.scalameter.api._ +import org.scalameter.{Context, History, persistence, currentContext} import org.scalameter.reporting.RegressionReporter.Tester import scala.collection.mutable.ListBuffer +// decorator of persitor to expose info for debugging +class DecoratorPersistor(p: Persistor) extends SerializationPersistor { + + override def load(context: Context): History = { + val resultdir = currentContext(resultDir) + val scope = context.scope + val curve = context.curve + val fileName = s"$resultdir$sep$scope.$curve.dat" + + println(s"load file $fileName") + + p.load(context) + } + + override def save(context: Context, h: History) = { + val resultdir = currentContext(resultDir) + val scope = context.scope + val curve = context.curve + val fileName = s"$resultdir$sep$scope.$curve.dat" + + println(s"save file $fileName") + + p.save(context, h) + } +} abstract class TestsToBenchmarkConverter (targetClass: Class[_], filterAnnot: Class[_ <: java.lang.annotation.Annotation] = classOf[org.junit.Test].asInstanceOf[Class[_ <: java.lang.annotation.Annotation]]) extends OnlineRegressionReport { + // NOTE: use `val persistor = ...` would cause persistor ignore command line options for `resultDir` + override def persistor = new DecoratorPersistor(super.persistor) + // accept all the results, do not fail override def tester: Tester = new Tester.Accepter + // store all results + override def historian: RegressionReporter.Historian = RegressionReporter.Historian.Complete() + override def executor: Executor = LocalExecutor(warmer, aggregator, measurer) val testNames = getMethodsAnnotatedWith(targetClass, filterAnnot).map(_.getName).sorted @@ -29,15 +62,31 @@ abstract class TestsToBenchmarkConverter (name, Gen.single("test")(name).map(Request.method(targetClass, _).getRunner))}.toMap //Gen.enumeration("test")(testNames:_*) - performance of targetClass.getSimpleName config (Context(reports.resultDir -> "./tmp")) in { - for (test <- testNames) - measure.method(test) in { - using(tests(test)) curve test in { - r => - val dummy = new RunNotifier() - r.run(dummy) + def setup = + performance of targetClass.getSimpleName in { + for (test <- testNames) + measure.method(test) in { + using(tests(test)) curve test in { + r => + val dummy = new RunNotifier() + r.run(dummy) + } } - } + } + + /** workaround to fix problem in ScalaMeter + * + * NOTE: Otherwise, command line options would be ignored by HTMLReporter, as + * the HTMLReporter uses the context of tree node, which is created via + * ScalaMeter DSL before command line option `-CresultDir` takes effect + * in `PerformanceTest.main`. + * + * Following code ensures that the test tree is set up after the `-CresultDir` + * option takes effect. + **/ + override def executeTests(): Boolean = { + setup + super.executeTests() } def getMethodsAnnotatedWith(clazz: Class[_], annotation: Class[_ <: java.lang.annotation.Annotation]): List[Method] = { diff --git a/bin/dotc b/bin/dotc index 1aadfbc5592b..f6c1db6c1e14 100755 --- a/bin/dotc +++ b/bin/dotc @@ -24,6 +24,7 @@ SCALA_COMPILER_VERSION=$(getLastStringOnLineWith "scala-compiler") DOTTY_VERSION=$(getLastStringOnLineWith "version in") JLINE_VERSION=$(getLastStringOnLineWith "jline") bootcp=true +bootstrapped=false default_java_opts="-Xmx768m -Xms768m" programName=$(basename "$0") # uncomment next line to enable debug output @@ -36,19 +37,23 @@ unset verbose quiet cygwin toolcp colors saved_stty CDPATH CompilerMain=dotty.tools.dotc.Main -ReplMain=test.DottyRepl +FromTasty=dotty.tools.dotc.FromTasty +ReplMain=dotty.tools.dotc.repl.Main -# autodetecting the compiler jar. this is location where sbt 'packages' it +# autodetecting the compiler jars. this is the location where sbt 'packages' them +INTERFACES_JAR=$DOTTY_ROOT/interfaces/target/dotty-interfaces-$DOTTY_VERSION.jar MAIN_JAR=$DOTTY_ROOT/target/scala-$SCALA_BINARY_VERSION/dotty_$SCALA_BINARY_VERSION-$DOTTY_VERSION.jar TEST_JAR=$DOTTY_ROOT/target/scala-$SCALA_BINARY_VERSION/dotty_$SCALA_BINARY_VERSION-$DOTTY_VERSION-tests.jar +DOTTY_JAR=$DOTTY_ROOT/dotty.jar + function checkjar { if [ ! -f "$1" ] then echo "The script is going to build the required jar file $1 by running \"sbt $2\"" cd $DOTTY_ROOT - sbt $2 + sbt "$2" cd - if [ ! -f "$1" ] then @@ -57,11 +62,22 @@ function checkjar { else echo "The required jar file was built successfully." fi + else + NEW_FILES="$(find "$DOTTY_ROOT/$3" \( -iname "*.scala" -o -iname "*.java" \) -newer "$1")" + if [ ! -z "$NEW_FILES" ]; + then + echo "new files detected. rebuilding" + cd $DOTTY_ROOT + sbt "$2" + touch "$1" + cd - + fi fi } -checkjar $MAIN_JAR package -checkjar $TEST_JAR test:package +checkjar $INTERFACES_JAR dotty-interfaces/package interfaces +checkjar $MAIN_JAR package src +checkjar $TEST_JAR test:package test # Autodetecting the scala-library location, in case it wasn't provided by an environment variable if [ "$SCALA_LIBRARY_JAR" == "" ] @@ -81,7 +97,7 @@ fi if [ "$JLINE_JAR" == "" ] then - JLINE_JAR=$HOME/.ivy2//cache/jline/jline/jars/jline-$JLINE_VERSION.jar + JLINE_JAR=$HOME/.ivy2/cache/jline/jline/jars/jline-$JLINE_VERSION.jar fi if [ ! -f "$SCALA_LIBRARY_JAR" -o ! -f "$SCALA_REFLECT_JAR" -o ! -f "$SCALA_COMPILER_JAR" -o ! -f "$JLINE_JAR" ] @@ -137,7 +153,7 @@ onExit () { # Get debug set early for arg in "$@"; do - [[ $arg == "-d" ]] && debug=true + [[ $arg == "-debug" ]] && debug=true done # to reenable echo if we are interrupted before completing. @@ -178,11 +194,38 @@ trap onExit INT # If using the boot classpath, also pass an empty classpath # to java to suppress "." from materializing. classpathArgs () { - if [[ -n $bootcp ]]; then -echo "-Xbootclasspath/a:$SCALA_LIBRARY_JAR:$SCALA_REFLECT_JAR:$SCALA_COMPILER_JAR:$JLINE_JAR:$MAIN_JAR -classpath $MAIN_JAR:$TEST_JAR" + if [[ "true" == $bootstrapped ]]; then + checkjar $DOTTY_JAR "test:runMain dotc.build" src + toolchain="$DOTTY_JAR:$SCALA_LIBRARY_JAR:$SCALA_REFLECT_JAR:$SCALA_COMPILER_JAR:$JLINE_JAR" else -echo "-classpath $SCALA_LIBRARY_JAR:$SCALA_REFLECT_JAR:$SCALA_COMPILER_JAR:$JLINE_JAR:$MAIN_JAR:$TEST_JAR" + toolchain="$SCALA_LIBRARY_JAR:$SCALA_REFLECT_JAR:$SCALA_COMPILER_JAR:$JLINE_JAR" fi + bcpJars="$INTERFACES_JAR:$MAIN_JAR" + cpJars="$INTERFACES_JAR:$MAIN_JAR:$TEST_JAR" + + if [[ -n "$cygwin" ]]; then + if [[ "$OS" = "Windows_NT" ]] && cygpath -m .>/dev/null 2>/dev/null ; then + format=mixed + else + format=windows + fi + + if [[ -n $bootcp ]]; then + boot_classpath="$(cygpath --path --$format "$toolchain:$bcpJars")" + classpath="$(cygpath --path --$format "$cpJars")" + cpArgs="-Xbootclasspath/a:$boot_classpath -classpath $classpath" + else + classpath="$(cygpath --path --$format "$toolchain:$cpJars")" + cpArgs="-classpath $classpath" + fi + else + if [[ -n $bootcp ]]; then + cpArgs="-Xbootclasspath/a:$toolchain:$bcpJars -classpath $cpJars" + else + cpArgs="-classpath $toolchain:$cpJars" + fi + fi + echo ${cpArgs} } # e.g. path -java-home /path/to/java_home @@ -202,20 +245,21 @@ main_class=$CompilerMain while [[ $# -gt 0 ]]; do case "$1" in --) shift; for arg; do addResidual "$arg"; done; set -- ;; - -h|-help) usage; exit 1 ;; + -h|-help) help=true && shift ;; + -bootstrapped) bootstrapped=true && shift ;; -v|-verbose) verbose=true && shift ;; - -d|-debug) debug=true && shift ;; + -debug) debug=true && shift ;; -q|-quiet) quiet=true && shift ;; # Optimize for short-running applications, see https://github.com/lampepfl/dotty/issues/222 -Oshort) addJava "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" && shift ;; - -repl) main_class=$ReplMain && shift ;; + -tasty) main_class=$FromTasty && shift ;; -compile) main_class=$CompilerMain && shift ;; -run) main_class=$ReplMain && shift ;; -fsc) main_class=$FscMain && shift ;; -bootcp) bootcp=true && shift ;; - -no-bootcp) unset bootcp && shift ;; + -nobootcp) unset bootcp && shift ;; -colors) colors=true && shift ;; -no-colors) unset colors && shift ;; -jrebel) jrebel=true && shift ;; diff --git a/docs/2015-10-23-dotty-compiler-bootstraps.md b/docs/2015-10-23-dotty-compiler-bootstraps.md new file mode 100644 index 000000000000..cdd472b7f124 --- /dev/null +++ b/docs/2015-10-23-dotty-compiler-bootstraps.md @@ -0,0 +1,63 @@ +--- +layout: blog +post-type: blog +by: Martin Odersky and Dmitry Petrashko +title: We Got LiftOff! The Dotty Compiler for Scala Bootstraps. +--- + +## We Got Liftoff! + +The [Dotty project](https://github.com/lampepfl/dotty) +is a platform to develop new technology for Scala +tooling and to try out concepts of future Scala language versions. +Its compiler is a new design intended to reflect the +lessons we learned from work with the Scala compiler. A clean redesign +today will let us iterate faster with new ideas in the future. + +Today we reached an important milestone: The Dotty compiler can +compile itself, and the compiled compiler can act as a drop-in for the +original one. This is what one calls a *bootstrap*. + +## Why is this important? + +The main reason is that this gives us a some validation of the +*trustworthiness* of the compiler itself. Compilers are complex beasts, +and many things can go wrong. By far the worst things that can go +wrong are bugs where incorrect code is produced. It's not fun debugging code that looks perfectly +fine, yet gets translated to something subtly wrong by the compiler. + +Having the compiler compile itself is a good test to demonstrate that +the generated code has reached a certain level of quality. Not only is +a compiler a large program (44k lines in the case of dotty), it is +also one that exercises a large part of the language in quite +intricate ways. Moreover, bugs in the code of a compiler don't tend to +go unnoticed, precisely because every part of a compiler feeds into +other parts and all together are necessary to produce a correct +translation. + +## Are We Done Yet? + +Far from it! The compiler is still very rough. A lot more work is +needed to + + - make it more robust, in particular when analyzing incorrect programs, + - improve error messages and warnings, + - improve the efficiency of some of the generated code, + - embed it in external tools such as sbt, REPL, IDEs, + - remove restrictions on what Scala code can be compiled, + - help in migrating Scala code that will have to be changed. + +## What Are the Next Steps? + +Over the coming weeks and months, we plan to work on the following topics: + + - Make snapshot releases. + - Get the Scala standard library to compile. + - Work on SBT integration of the compiler. + - Work on IDE support. + - Investigate the best way to obtaining a REPL. + - Work on the build infrastructure. + +If you want to get your hands dirty with any of this, now is a good moment to get involved! +To get started: . + diff --git a/docs/HigherKinded-v2.md b/docs/HigherKinded-v2.md index 3216f42f4aff..2ca6424deb2c 100644 --- a/docs/HigherKinded-v2.md +++ b/docs/HigherKinded-v2.md @@ -184,7 +184,7 @@ of `Lambda$NP` would themselves be written as follows. trait Lambda$IP[type $hkArg$0, +$hkArg1] { type +Apply } extends Lambda$II trait Lambda$NI[type -$hkArg$0, $hkArg1] { type +Apply } extends Lambda$II - trait Lambda$II[type -hkArg$0, $hkArg1] { type +Apply } + trait Lambda$II[type $hkArg$0, $hkArg1] { type +Apply } `Lambda` traits are special in that they influence how type applications are expanded: If the standard type application `T[X1, ..., Xn]` @@ -204,7 +204,7 @@ which in turn simplifies to `List[List[String]]`. 2nd Example: Consider the two aliases - type RMap[K, V] = Map[V, K]] + type RMap[K, V] = Map[V, K] type RRMap[K, V] = RMap[V, K] These expand as follows: diff --git a/docs/SyntaxSummary.txt b/docs/SyntaxSummary.txt index e40b3fd6c594..d4f7ceadec50 100644 --- a/docs/SyntaxSummary.txt +++ b/docs/SyntaxSummary.txt @@ -75,11 +75,12 @@ grammar. {\small \begin{lstlisting} - Literal ::= [`-'] integerLiteral + SimpleLiteral ::= [`-'] integerLiteral | [`-'] floatingPointLiteral | booleanLiteral | characterLiteral | stringLiteral + Literal ::= SimpleLiteral | processedStringLiteral | symbolLiteral | `null' @@ -99,14 +100,16 @@ grammar. FunArgTypes ::= InfixType | `(' [ FunArgType {`,' FunArgType } ] `)' InfixType ::= RefinedType {id [nl] RefinedType} InfixOp(t1, op, t2) - RefinedType ::= WithType {Annotation | [nl] Refinement} Annotated(t, annot), RefinedTypeTree(t, ds) - WithType ::= SimpleType {`with' SimpleType} (deprecated) - SimpleType ::= SimpleType TypeArgs AppliedTypeTree(t, args) + RefinedType ::= WithType {[nl] Refinement} RefinedTypeTree(t, ds) + WithType ::= AnnotType {`with' AnnotType} (deprecated) + AnnotType ::= SimpleType {Annotation} Annotated(t, annot) + SimpleType ::= SimpleType (TypeArgs | NamedTypeArgs) AppliedTypeTree(t, args) | SimpleType `#' id SelectFromTypeTree(t, name) | StableId | Path `.' `type' SingletonTypeTree(p) | `(' ArgTypes ')' Tuple(ts) | Refinement RefinedTypeTree(EmptyTree, refinement) + | SimpleLiteral SingletonTypeTree(l) ArgType ::= Type | `_' TypeBounds ArgTypes ::= ArgType {`,' ArgType} @@ -115,18 +118,20 @@ grammar. ParamType ::= [`=>'] ParamValueType ParamValueType ::= Type [`*'] PostfixOp(t, "*") TypeArgs ::= `[' ArgTypes `]' ts + NamedTypeArg ::= id `=' ArgType NamedArg(id, t) + NamedTypeArgs ::= `[' NamedTypeArg {`,' NamedTypeArg} `]' nts Refinement ::= `{' [Dcl] {semi [Dcl]} `}' ds TypeBounds ::= [`>:' Type] [`<: Type] | INT TypeBoundsTree(lo, hi) TypeParamBounds ::= TypeBounds {`<%' Type} {`:' Type} ContextBounds(typeBounds, tps) Expr ::= FunParams `=>' Expr Function(args, expr), Function(ValDef([implicit], id, TypeTree(), EmptyTree), expr) | Expr1 - FunParams ::= Bindings - | [`implicit'] id - | `_' + FunParams ::= Bindings + | [`implicit'] id + | `_' ExprInParens ::= PostfixExpr `:' Type | Expr - BlockResult ::= (FunParams | [`implicit'] id `:' InfixType) => Block + BlockResult ::= (FunParams | [`implicit'] id `:' InfixType) `=>' Block | Expr1 Expr1 ::= `if' `(' Expr `)' {nl} Expr [[semi] else Expr] If(Parens(cond), thenp, elsep?) | `if' Expr `then' Expr [[semi] else Expr] If(cond, thenp, elsep?) @@ -157,7 +162,7 @@ grammar. | `_' | `(' ExprsInParens `)' Parens(exprs) | SimpleExpr `.' id Select(expr, id) - | SimpleExpr TypeArgs TypeApply(expr, args) + | SimpleExpr (TypeArgs | NamedTypeArgs) TypeApply(expr, args) | SimpleExpr1 ArgumentExprs Apply(expr, args) | XmlExpr ExprsInParens ::= ExprInParens {`,' ExprInParens} @@ -248,7 +253,7 @@ grammar. AccessModifier ::= (`private' | `protected') [AccessQualifier] AccessQualifier ::= `[' (id | `this') `]' - Annotation ::= `@' SimpleType {ArgumentExprs} Apply(tpe, args) + Annotation ::= `@' SimpleType {ParArgumentExprs} Apply(tpe, args) TemplateBody ::= [nl] `{' [SelfType] TemplateStat {semi TemplateStat} `} (self, stats) TemplateStat ::= Import @@ -301,7 +306,7 @@ grammar. TemplateOpt ::= [`extends' Template | [nl] TemplateBody] Template ::= ConstrApps [TemplateBody] | TemplateBody Template(constr, parents, self, stats) ConstrApps ::= ConstrApp {`with' ConstrApp} - ConstrApp ::= SimpleType {ArgumentExprs} Apply(tp, args) + ConstrApp ::= AnnotType {ArgumentExprs} Apply(tp, args) ConstrExpr ::= SelfInvocation | ConstrBlock diff --git a/docs/dotc-internals/overall-structure.md b/docs/dotc-internals/overall-structure.md new file mode 100644 index 000000000000..a80c35b4c7cb --- /dev/null +++ b/docs/dotc-internals/overall-structure.md @@ -0,0 +1,174 @@ +# Dotc's Overall Structure + +The compiler code is found in package [dotty.tools](https://github.com/lampepfl/dotty/tree/master/src/dotty/tools). It spans the +following three sub-packages: + + backend Compiler backends (currently for JVM and JS) + dotc The main compiler + io Helper modules for file access and classpath handling. + +The [dotc](https://github.com/lampepfl/dotty/tree/master/src/dotty/tools/dotc) +package contains some main classes that can be run as separate +programs. The most important one is class +[Main](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/Main.scala). +`Main` inherits from +[Driver](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/Driver.scala) which +contains the highest level functions for starting a compiler and processing some sources. +`Driver` in turn is based on two other high-level classes, +[Compiler](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/Compiler.scala) and +[Run](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/Run.scala). + +## Package Structure + +Most functionality of `dotc` is implemented in subpackages of `dotc`. Here's a list of sub-packages +and their focus. + + ast Abstract syntax trees, + config Compiler configuration, settings, platform specific definitions. + core Core data structures and operations, with specific subpackages for: + + core.classfile Reading of Java classfiles into core data structures + core.tasty Reading and writing of TASTY files to/from core data structures + core.unpickleScala2 Reading of Scala2 symbol information into core data structures + + parsing Scanner and parser + printing Pretty-printing trees, types and other data + repl The interactive REPL + reporting Reporting of error messages, warnings and other info. + rewrite Helpers for rewriting Scala 2's constructs into dotty's. + transform Miniphases and helpers for tree transformations. + typer Type-checking and other frontend phases + util General purpose utility classes and modules. + +## Contexts + +`dotc` has almost no global state (the only significant bit of global state is the name table, +which is used to hash strings into unique names). Instead, all essential bits of information that +can vary over a compiler run are collected in a +[Context](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/core/Contexts.scala). +Most methods in `dotc` take a Context value as an implicit parameter. + +Contexts give a convenient way to customize values in some part of the +call-graph. To run, e.g. some compiler function `f` at a given +phase `phase`, we invoke `f` with an explicit context parameter, like +this + + f(/*normal args*/)(ctx.withPhase(phase)) + +This assumes that `f` is defined in the way most compiler functions are: + + def f(/*normal parameters*/)(implicit ctx: Context) ... + +Compiler code follows the convention that all implicit `Context` +parameters are named `ctx`. This is important to avoid implicit +ambiguities in the case where nested methods contain each a Context +parameters. The common name ensures then that the implicit parameters +properly shadow each other. + +Sometimes we want to make sure that implicit contexts are not captured +in closures or other long-lived objects, be it because we want to +enforce that nested methods each get their own implicit context, or +because we want to avoid a space leak in the case where a closure can +survive several compiler runs. A typical case is a completer for a +symbol representing an external class, which produces the attributes +of the symbol on demand, and which might never be invoked. In that +case we follow the convention that any context parameter is explicit, +not implicit, so we can track where it is used, and that it has a name +different from `ctx`. Commonly used is `ictx` for "initialization +context". + +With these two conventions in place, it has turned out that implicit +contexts work amazingly well as a device for dependency injection and +bulk parameterization. There is of course always the danger that +an unexpected implicit will be passed, but in practice this has not turned out to +be much of a problem. + +## Compiler Phases + +Seen from a temporal perspective, the `dotc` compiler consists of a list of phases. +The current list of phases is specified in class [Compiler](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/Compiler.scala) as follows: + +```scala + def phases: List[List[Phase]] = List( + List(new FrontEnd), // Compiler frontend: scanner, parser, namer, typer + List(new PostTyper), // Additional checks and cleanups after type checking + List(new Pickler), // Generate TASTY info + List(new FirstTransform, // Some transformations to put trees into a canonical form + new CheckReentrant), // Internal use only: Check that compiled program has no data races involving global vars + List(new RefChecks, // Various checks mostly related to abstract members and overriding + new CheckStatic, // Check restrictions that apply to @static members + new ElimRepeated, // Rewrite vararg parameters and arguments + new NormalizeFlags, // Rewrite some definition flags + new ExtensionMethods, // Expand methods of value classes with extension methods + new ExpandSAMs, // Expand single abstract method closures to anonymous classes + new TailRec, // Rewrite tail recursion to loops + new LiftTry, // Put try expressions that might execute on non-empty stacks into their own methods + new ClassOf), // Expand `Predef.classOf` calls. + List(new PatternMatcher, // Compile pattern matches + new ExplicitOuter, // Add accessors to outer classes from nested ones. + new ExplicitSelf, // Make references to non-trivial self types explicit as casts + new CrossCastAnd, // Normalize selections involving intersection types. + new Splitter), // Expand selections involving union types into conditionals + List(new VCInlineMethods, // Inlines calls to value class methods + new SeqLiterals, // Express vararg arguments as arrays + new InterceptedMethods, // Special handling of `==`, `|=`, `getClass` methods + new Getters, // Replace non-private vals and vars with getter defs (fields are added later) + new ElimByName, // Expand by-name parameters and arguments + new AugmentScala2Traits, // Expand traits defined in Scala 2.11 to simulate old-style rewritings + new ResolveSuper), // Implement super accessors and add forwarders to trait methods + List(new Erasure), // Rewrite types to JVM model, erasing all type parameters, abstract types and refinements. + List(new ElimErasedValueType, // Expand erased value types to their underlying implementation types + new VCElideAllocations, // Peep-hole optimization to eliminate unnecessary value class allocations + new Mixin, // Expand trait fields and trait initializers + new LazyVals, // Expand lazy vals + new Memoize, // Add private fields to getters and setters + new LinkScala2ImplClasses, // Forward calls to the implementation classes of traits defined by Scala 2.11 + new NonLocalReturns, // Expand non-local returns + new CapturedVars, // Represent vars captured by closures as heap objects + new Constructors, // Collect initialization code in primary constructors + // Note: constructors changes decls in transformTemplate, no InfoTransformers should be added after it + new FunctionalInterfaces,// Rewrites closures to implement @specialized types of Functions. + new GetClass), // Rewrites getClass calls on primitive types. + List(new LambdaLift, // Lifts out nested functions to class scope, storing free variables in environments + // Note: in this mini-phase block scopes are incorrect. No phases that rely on scopes should be here + new ElimStaticThis, // Replace `this` references to static objects by global identifiers + new Flatten, // Lift all inner classes to package scope + new RestoreScopes), // Repair scopes rendered invalid by moving definitions in prior phases of the group + List(new ExpandPrivate, // Widen private definitions accessed from nested classes + new CollectEntryPoints, // Find classes with main methods + new LabelDefs), // Converts calls to labels to jumps + List(new GenSJSIR), // Generate .js code + List(new GenBCode) // Generate JVM bytecode + ) +``` + +Note that phases are grouped, so the `phases` method is of type +`List[List[Phase]]`. The idea is that all phases in a group are +*fused* into a single tree traversal. That way, phases can be kept +small (most phases perform a single function) without requiring an +excessive number of tree traversals (which are costly, because they +have generally bad cache locality). + +Phases fall into four categories: + + - Frontend phases: `Frontend`, `PostTyper` and `Pickler`. `FrontEnd` parses the source programs and generates + untyped abstract syntax trees, which are then typechecked and transformed into typed abstract syntax trees. + `PostTyper` performs checks and cleanups that require a fully typed program. In particular, it + + - creates super accessors representing `super` calls in traits + - creates implementations of synthetic (compiler-implemented) methods + - avoids storing parameters passed unchanged from subclass to superclass in duplicate fields. + + Finally `Pickler` serializes the typed syntax trees produced by the frontend as TASTY data structures. + + - High-level transformations: All phases from `FirstTransform` to `Erasure`. Most of these phases transform + syntax trees, expanding high-level constructs to more primitive ones. The last phase in the group, `Erasure` + translates all types into types supported directly by the JVM. To do this, it performs another type checking + pass, but using the rules of the JVM's type system instead of Scala's. + + - Low-level transformations: All phases from `ElimErasedValueType` to `LabelDefs`. These + further transform trees until they are essentially a structured version of Java bytecode. + + - Code generators: These map the transformed trees to Java classfiles or Javascript files. + + diff --git a/docs/dotc-internals/periods.md b/docs/dotc-internals/periods.md new file mode 100644 index 000000000000..a616ba8a84d7 --- /dev/null +++ b/docs/dotc-internals/periods.md @@ -0,0 +1,94 @@ +# Dotc's concept of time + +Conceptually, the `dotc` compiler's job is to maintain views of +various artifacts associated with source code at all points in time. +But what is *time* for `dotc`? In fact, it is a combination of +compiler runs and compiler phases. + +The *hours* of the compiler's clocks are measured in compiler +[runs](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/Run.scala). Every +run creates a new hour, which follows all the compiler runs (hours) that +happened before. `dotc` is designed to be used as an incremental +compiler that can support incremental builds, as well as interactions +in an IDE and a REPL. This means that new runs can occur quite +frequently. At the extreme, every keystroke in an editor or REPL can +potentially launch a new compiler run, so potentially an "hour" of +compiler time might take only a fraction of a second in real time. + +The *minutes* of the compiler's clocks are measured in phases. At every +compiler run, the compiler cycles through a number of +[phases](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/core/Phases.scala). +The list of phases is defined in the [Compiler]object +There are currently about 60 phases per run, so the minutes/hours +analogy works out roughly. After every phase the view the compiler has +of the world changes: trees are transformed, types are gradually simplified +from Scala types to JVM types, definitions are rearranged, and so on. + +Many pieces in the information compiler are time-dependent. For +instance, a Scala symbol representing a definition has a type, but +that type will usually change as one goes from the higher-level Scala +view of things to the lower-level JVM view. There are different ways +to deal with this. Many compilers change the type of a symbol +destructively according to the "current phase". Another, more +functional approach might be to have different symbols representing +the same definition at different phases, which each symbol carrying a +different immutable type. `dotc` employs yet another scheme, which is +inspired by functional reactive programming (FRP): Symbols carry not a +single type, but a function from compiler phase to type. So the type +of a symbol is a time-indexed function, where time ranges over +compiler phases. + +Typically, the definition of a symbol or other quantity remains stable +for a number of phases. This leads us to the concept of a +[period](https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/core/Periods.scala). +Conceptually, period is an interval of some given phases in a given +compiler run. Periods are conceptually represented by three pieces of +information + + - the ID of the current run, + - the ID of the phase starting the period + - the number of phases in the period + +All three pieces of information are encoded in a value class over a 32 bit integer. +Here's the API for class `Period`: + +```scala + class Period(val code: Int) extends AnyVal { + def runId: RunId // The run identifier of this period. + def firstPhaseId: PhaseId // The first phase of this period + def lastPhaseId: PhaseId // The last phase of this period + def phaseId: PhaseId // The phase identifier of this single-phase period. + + def containsPhaseId(id: PhaseId): Boolean + def contains(that: Period): Boolean + def overlaps(that: Period): Boolean + + def & (that: Period): Period + def | (that: Period): Period + } +``` + +We can access the parts of a period using `runId`, `firstPhaseId`, +`lastPhaseId`, or using `phaseId` for periods consisting only of a +single phase. They return `RunId` or `PhaseId` values, which are +aliases of `Int`. `containsPhaseId`, `contains` and `overlaps` test +whether a period contains a phase or a period as a sub-interval, or +whether the interval overlaps with another period. Finally, `&` and +`|` produce the intersection and the union of two period intervals +(the union operation `|` takes as `runId` the `runId` of its left +operand, as periods spanning different `runId`s cannot be constructed. + +Periods are constructed using two `apply` methods: + +```scala + object Period { + + /** The single-phase period consisting of given run id and phase id */ + def apply(rid: RunId, pid: PhaseId): Period } + + /** The period consisting of given run id, and lo/hi phase ids */ + def apply(rid: RunId, loPid: PhaseId, hiPid: PhaseId): Period + } +``` + +As a sentinel value there's `Nowhere`, a period that is empty. diff --git a/interfaces/src/main/java/dotty/tools/dotc/interfaces/AbstractFile.java b/interfaces/src/main/java/dotty/tools/dotc/interfaces/AbstractFile.java new file mode 100644 index 000000000000..a1b9cc5c7beb --- /dev/null +++ b/interfaces/src/main/java/dotty/tools/dotc/interfaces/AbstractFile.java @@ -0,0 +1,24 @@ +package dotty.tools.dotc.interfaces; + +import java.io.File; +import java.util.Optional; + +/** An abstract file may either be a file on disk or a virtual file. + * + * Do not rely on the identity of instances of this class. + * + * User code should not implement this interface, but it may have to + * manipulate objects of this type. + */ +public interface AbstractFile { + /** The name of this file, note that two files may have the same name. */ + String name(); + + /** The path of this file, this might be a virtual path of an unspecified format. */ + String path(); + + /** If this is a real file on disk, a `java.io.File` that corresponds to this file. + * Otherwise, an empty `Optional`. + */ + Optional jfile(); +} diff --git a/interfaces/src/main/java/dotty/tools/dotc/interfaces/CompilerCallback.java b/interfaces/src/main/java/dotty/tools/dotc/interfaces/CompilerCallback.java new file mode 100644 index 000000000000..17d2f6bbdf43 --- /dev/null +++ b/interfaces/src/main/java/dotty/tools/dotc/interfaces/CompilerCallback.java @@ -0,0 +1,28 @@ +package dotty.tools.dotc.interfaces; + +/** Set of callbacks called in response to events during the compilation process. + * + * You should implement this interface if you want to react to one or more of + * these events. + * + * @see the method `process` of `dotty.tools.dotc.Driver` for more information. + */ +public interface CompilerCallback { + /** Called when a class has been generated. + * + * @param source The source file corresponding to this class. + * Example: ./src/library/scala/collection/Seq.scala + * @param generatedClass The generated classfile for this class. + * Example: ./scala/collection/Seq$.class + * @param className The name of this class. + * Example: scala.collection.Seq$ + */ + default void onClassGenerated(SourceFile source, AbstractFile generatedClass, String className) {}; + + /** Called when every class for this file has been generated. + * + * @param source The source file. + * Example: ./src/library/scala/collection/Seq.scala + */ + default void onSourceCompiled(SourceFile source) {}; +} diff --git a/interfaces/src/main/java/dotty/tools/dotc/interfaces/Diagnostic.java b/interfaces/src/main/java/dotty/tools/dotc/interfaces/Diagnostic.java new file mode 100644 index 000000000000..436f9f8ea823 --- /dev/null +++ b/interfaces/src/main/java/dotty/tools/dotc/interfaces/Diagnostic.java @@ -0,0 +1,26 @@ +package dotty.tools.dotc.interfaces; + +import java.util.Optional; + +/** A diagnostic is a message emitted during the compilation process. + * + * It can either be an error, a warning or an information. + * + * User code should not implement this interface, but it may have to + * manipulate objects of this type. + */ +public interface Diagnostic { + public static final int ERROR = 2; + public static final int WARNING = 1; + public static final int INFO = 0; + + /** The message to report */ + String message(); + + /** Level of the diagnostic, can be either ERROR, WARNING or INFO */ + int level(); + + /** The position in a source file of the code that caused this diagnostic + * to be emitted. */ + Optional position(); +} diff --git a/interfaces/src/main/java/dotty/tools/dotc/interfaces/ReporterResult.java b/interfaces/src/main/java/dotty/tools/dotc/interfaces/ReporterResult.java new file mode 100644 index 000000000000..63c5104dff9f --- /dev/null +++ b/interfaces/src/main/java/dotty/tools/dotc/interfaces/ReporterResult.java @@ -0,0 +1,18 @@ +package dotty.tools.dotc.interfaces; + +/** Summary of the diagnostics emitted by a Reporter. + * + * User code should not implement this interface, but it may have to + * manipulate objects of this type. + */ +public interface ReporterResult { + /** Have we emitted any error ? */ + boolean hasErrors(); + /** Number of errors that have been emitted */ + int errorCount(); + + /** Have we emitted any warning ? */ + boolean hasWarnings(); + /** Number of warnings that have been emitted */ + int warningCount(); +} diff --git a/interfaces/src/main/java/dotty/tools/dotc/interfaces/SimpleReporter.java b/interfaces/src/main/java/dotty/tools/dotc/interfaces/SimpleReporter.java new file mode 100644 index 000000000000..e52537096bd9 --- /dev/null +++ b/interfaces/src/main/java/dotty/tools/dotc/interfaces/SimpleReporter.java @@ -0,0 +1,13 @@ +package dotty.tools.dotc.interfaces; + +/** Report errors, warnings and info messages during the compilation process + * + * You should implement this interface if you want to handle the diagnostics + * returned by the compiler yourself. + * + * @see the method `process` of `dotty.tools.dotc.Driver` for more information. + */ +public interface SimpleReporter { + /** Report a diagnostic. */ + void report(Diagnostic diag); +} diff --git a/interfaces/src/main/java/dotty/tools/dotc/interfaces/SourceFile.java b/interfaces/src/main/java/dotty/tools/dotc/interfaces/SourceFile.java new file mode 100644 index 000000000000..5611d388587d --- /dev/null +++ b/interfaces/src/main/java/dotty/tools/dotc/interfaces/SourceFile.java @@ -0,0 +1,13 @@ +package dotty.tools.dotc.interfaces; + +import java.io.File; + +/** A source file. + * + * User code should not implement this interface, but it may have to + * manipulate objects of this type. + */ +public interface SourceFile extends AbstractFile { + /** The content of this file as seen by the compiler. */ + char[] content(); +} diff --git a/interfaces/src/main/java/dotty/tools/dotc/interfaces/SourcePosition.java b/interfaces/src/main/java/dotty/tools/dotc/interfaces/SourcePosition.java new file mode 100644 index 000000000000..c9b42d4ad213 --- /dev/null +++ b/interfaces/src/main/java/dotty/tools/dotc/interfaces/SourcePosition.java @@ -0,0 +1,44 @@ +package dotty.tools.dotc.interfaces; + +/** A position in a source file. + * + * A position is a range between a start offset and an end offset, as well as a + * point inside this range. + * + * As a convenience, we also provide methods that return the line and the column + * corresponding to each offset. + * + * User code should not implement this interface, but it may have to + * manipulate objects of this type. + */ +public interface SourcePosition { + /** Content of the line which contains the point */ + String lineContent(); + + /** Offset to the point */ + int point(); + /** Line number of the point, starting at 0 */ + int line(); + /** Column number of the point, starting at 0 */ + int column(); + + /** Offset to the range start */ + int start(); + /** Line number of the range start, starting at 0 */ + int startLine(); + /** Column number of the range start, starting at 0 */ + int startColumn(); + + /** Offset to the range end */ + int end(); + /** Line number of the range end, starting at 0 */ + int endLine(); + /** Column number of the range end, starting at 0 */ + int endColumn(); + + /** The source file corresponding to this position. + * The values returned by `point()`, `start()` and `end()` + * are indices in the array returned by `source().content()`. + */ + SourceFile source(); +} diff --git a/project/Build.scala b/project/Build.scala index 504185e65f6c..35482303e71b 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -4,11 +4,14 @@ import java.io.{ RandomAccessFile, File } import java.nio.channels.FileLock import scala.reflect.io.Path +import org.scalajs.sbtplugin.ScalaJSPlugin +import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._ + object DottyBuild extends Build { - val travisMemLimit = List("-Xmx1g", "-Xss2m") + val jenkinsMemLimit = List("-Xmx1300m") - val TRAVIS_BUILD = "dotty.travis.build" + val JENKINS_BUILD = "dotty.jenkins.build" val agentOptions = List( // "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" @@ -17,166 +20,268 @@ object DottyBuild extends Build { // "-XX:+HeapDumpOnOutOfMemoryError", "-Xmx1g", "-Xss2m" ) - val defaults = Defaults.defaultSettings ++ Seq( - scalaVersion in Global := "2.11.5", - version in Global := "0.1-SNAPSHOT", - organization in Global := "org.scala-lang", - organizationName in Global := "LAMP/EPFL", - organizationHomepage in Global := Some(url("http://lamp.epfl.ch")), - homepage in Global := Some(url("https://github.com/lampepfl/dotty")), - - // set sources to src/, tests to test/ and resources to resources/ - scalaSource in Compile := baseDirectory.value / "src", - javaSource in Compile := baseDirectory.value / "src", - scalaSource in Test := baseDirectory.value / "test", - javaSource in Test := baseDirectory.value / "test", - resourceDirectory in Compile := baseDirectory.value / "resources", - unmanagedSourceDirectories in Compile := Seq((scalaSource in Compile).value), - unmanagedSourceDirectories in Test := Seq((scalaSource in Test).value), - - // include sources in eclipse (downloads source code for all dependencies) - //http://stackoverflow.com/questions/10472840/how-to-attach-sources-to-sbt-managed-dependencies-in-scala-ide#answer-11683728 - com.typesafe.sbteclipse.plugin.EclipsePlugin.EclipseKeys.withSource := true, - - // to get Scala 2.11 - resolvers += Resolver.sonatypeRepo("releases"), - - // get libraries onboard - partestDeps := Seq("me.d-d" % "scala-compiler" % "2.11.5-20150506-175515-8fc7635b56", - "org.scala-lang" % "scala-reflect" % scalaVersion.value, - "org.scala-lang" % "scala-library" % scalaVersion.value % "test"), - libraryDependencies ++= partestDeps.value, - libraryDependencies ++= Seq("org.scala-lang.modules" %% "scala-xml" % "1.0.1", - "org.scala-lang.modules" %% "scala-partest" % "1.0.5" % "test", - "com.novocode" % "junit-interface" % "0.11" % "test", - "jline" % "jline" % "2.12"), - - // scalac options - scalacOptions in Global ++= Seq("-feature", "-deprecation", "-language:_"), - - javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"), - - // enable improved incremental compilation algorithm - incOptions := incOptions.value.withNameHashing(true), - - // enable verbose exception messages for JUnit - testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "--run-listener=test.ContextEscapeDetector"), - testOptions in Test += Tests.Cleanup({ () => partestLockFile.delete }), - - lockPartestFile := { - // When this file is present, running `test` generates the files for - // partest. Otherwise it just executes the tests directly. - val lockDir = partestLockFile.getParentFile - lockDir.mkdirs - // Cannot have concurrent partests as they write to the same directory. - if (lockDir.list.size > 0) - throw new RuntimeException("ERROR: sbt partest: another partest is already running, pid in lock file: " + lockDir.list.toList.mkString(" ")) - partestLockFile.createNewFile - partestLockFile.deleteOnExit - }, - runPartestRunner <<= Def.taskDyn { - val jars = Seq((packageBin in Compile).value.getAbsolutePath) ++ - getJarPaths(partestDeps.value, ivyPaths.value.ivyHome) - val dottyJars = "-dottyJars " + jars.length + " " + jars.mkString(" ") - // Provide the jars required on the classpath of run tests - runTask(Test, "dotty.partest.DPConsoleRunner", dottyJars) - }, - - // Adjust classpath for running dotty - mainClass in (Compile, run) := Some("dotty.tools.dotc.Main"), - fork in run := true, - fork in Test := true, - parallelExecution in Test := false, - - // http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/sbt-setting-jvm-boot-paramaters-for-scala - javaOptions <++= (managedClasspath in Runtime, packageBin in Compile) map { (attList, bin) => - // put the Scala {library, reflect} in the classpath - val path = for { - file <- attList.map(_.data) - path = file.getAbsolutePath - } yield "-Xbootclasspath/p:" + path - // dotty itself needs to be in the bootclasspath - val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList - // System.err.println("BOOTPATH: " + fullpath) - - val travis_build = // propagate if this is a travis build - if (sys.props.isDefinedAt(TRAVIS_BUILD)) - List(s"-D$TRAVIS_BUILD=${sys.props(TRAVIS_BUILD)}") ::: travisMemLimit - else - List() - - val tuning = - if (sys.props.isDefinedAt("Oshort")) - // Optimize for short-running applications, see https://github.com/lampepfl/dotty/issues/222 - List("-XX:+TieredCompilation", "-XX:TieredStopAtLevel=1") - else - List() - - ("-DpartestParentID=" + pid) :: tuning ::: agentOptions ::: travis_build ::: fullpath - } - ) ++ addCommandAlias("partest", ";test:compile;lockPartestFile;test:test;runPartestRunner") - - lazy val dotty = Project(id = "dotty", base = file("."), settings = defaults) - - lazy val benchmarkSettings = Defaults.defaultSettings ++ Seq( - - // to get Scala 2.11 - resolvers += Resolver.sonatypeRepo("releases"), - - baseDirectory in (Test,run) := (baseDirectory in dotty).value, - - - libraryDependencies ++= Seq("com.storm-enroute" %% "scalameter" % "0.6" % Test, - "com.novocode" % "junit-interface" % "0.11"), - testFrameworks += new TestFramework("org.scalameter.ScalaMeterFramework"), - - // scalac options - scalacOptions in Global ++= Seq("-feature", "-deprecation", "-language:_"), - - javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"), - - fork in Test := true, - parallelExecution in Test := false, - - // http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/sbt-setting-jvm-boot-paramaters-for-scala - javaOptions <++= (dependencyClasspath in Runtime, packageBin in Compile) map { (attList, bin) => - // put the Scala {library, reflect, compiler} in the classpath - val path = for { - file <- attList.map(_.data) - path = file.getAbsolutePath - prefix = if (path.endsWith(".jar")) "p" else "a" - } yield "-Xbootclasspath/" + prefix + ":" + path - // dotty itself needs to be in the bootclasspath - val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList - // System.err.println("BOOTPATH: " + fullpath) - - val travis_build = // propagate if this is a travis build - if (sys.props.isDefinedAt(TRAVIS_BUILD)) - List(s"-D$TRAVIS_BUILD=${sys.props(TRAVIS_BUILD)}") - else - List() - val res = agentOptions ::: travis_build ::: fullpath - println("Running with javaOptions: " + res) - res - } - ) + override def settings: Seq[Setting[_]] = { + super.settings ++ Seq( + scalaVersion in Global := "2.11.5", + version in Global := "0.1-SNAPSHOT", + organization in Global := "org.scala-lang", + organizationName in Global := "LAMP/EPFL", + organizationHomepage in Global := Some(url("http://lamp.epfl.ch")), + homepage in Global := Some(url("https://github.com/lampepfl/dotty")), + + // scalac options + scalacOptions in Global ++= Seq("-feature", "-deprecation", "-language:existentials,higherKinds,implicitConversions"), + javacOptions in Global ++= Seq("-Xlint:unchecked", "-Xlint:deprecation") + ) + } - lazy val benchmarks = Project(id = "dotty-bench", settings = benchmarkSettings, - base = file("bench")) dependsOn(dotty % "compile->test") + /** Enforce 2.11.5. Do not let it be upgraded by dependencies. */ + private val overrideScalaVersionSetting = + ivyScala := ivyScala.value.map(_.copy(overrideScalaVersion = true)) + + lazy val `dotty-interfaces` = project.in(file("interfaces")). + settings( + // Do not append Scala versions to the generated artifacts + crossPaths := false, + // Do not depend on the Scala library + autoScalaLibrary := false, + //Remove javac invalid options in Compile doc + javacOptions in (Compile, doc) --= Seq("-Xlint:unchecked", "-Xlint:deprecation") + ) + + lazy val dotty = project.in(file(".")). + dependsOn(`dotty-interfaces`). + settings( + overrideScalaVersionSetting, + + // set sources to src/, tests to test/ and resources to resources/ + scalaSource in Compile := baseDirectory.value / "src", + javaSource in Compile := baseDirectory.value / "src", + scalaSource in Test := baseDirectory.value / "test", + javaSource in Test := baseDirectory.value / "test", + resourceDirectory in Compile := baseDirectory.value / "resources", + unmanagedSourceDirectories in Compile := Seq((scalaSource in Compile).value), + unmanagedSourceDirectories in Test := Seq((scalaSource in Test).value), + + // set system in/out for repl + connectInput in run := true, + outputStrategy := Some(StdoutOutput), + + // Generate compiler.properties, used by sbt + resourceGenerators in Compile += Def.task { + val file = (resourceManaged in Compile).value / "compiler.properties" + val contents = s"version.number=${version.value}" + IO.write(file, contents) + Seq(file) + }.taskValue, + + // include sources in eclipse (downloads source code for all dependencies) + //http://stackoverflow.com/questions/10472840/how-to-attach-sources-to-sbt-managed-dependencies-in-scala-ide#answer-11683728 + com.typesafe.sbteclipse.plugin.EclipsePlugin.EclipseKeys.withSource := true, + + // get libraries onboard + partestDeps := Seq("me.d-d" % "scala-compiler" % "2.11.5-20160322-171045-e19b30b3cd", + "org.scala-lang" % "scala-reflect" % scalaVersion.value, + "org.scala-lang" % "scala-library" % scalaVersion.value % "test"), + libraryDependencies ++= partestDeps.value, + libraryDependencies ++= Seq("org.scala-lang.modules" %% "scala-xml" % "1.0.1", + "org.scala-lang.modules" %% "scala-partest" % "1.0.11" % "test", + "com.novocode" % "junit-interface" % "0.11" % "test", + "jline" % "jline" % "2.12"), + + // enable improved incremental compilation algorithm + incOptions := incOptions.value.withNameHashing(true), + + // enable verbose exception messages for JUnit + testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "--run-listener=test.ContextEscapeDetector"), + testOptions in Test += Tests.Cleanup({ () => partestLockFile.delete }), + + lockPartestFile := { + // When this file is present, running `test` generates the files for + // partest. Otherwise it just executes the tests directly. + val lockDir = partestLockFile.getParentFile + lockDir.mkdirs + // Cannot have concurrent partests as they write to the same directory. + if (lockDir.list.size > 0) + throw new RuntimeException("ERROR: sbt partest: another partest is already running, pid in lock file: " + lockDir.list.toList.mkString(" ")) + partestLockFile.createNewFile + partestLockFile.deleteOnExit + }, + runPartestRunner <<= Def.inputTaskDyn { + // Magic! This is both an input task and a dynamic task. Apparently + // command line arguments get passed to the last task in an aliased + // sequence (see partest alias below), so this works. + val args = Def.spaceDelimited("").parsed + val jars = Seq((packageBin in Compile).value.getAbsolutePath) ++ + getJarPaths(partestDeps.value, ivyPaths.value.ivyHome) + val dottyJars = "-dottyJars " + (jars.length + 1) + " dotty.jar" + " " + jars.mkString(" ") + // Provide the jars required on the classpath of run tests + runTask(Test, "dotty.partest.DPConsoleRunner", dottyJars + " " + args.mkString(" ")) + }, + + /* Add the sources of scalajs-ir. + * To guarantee that dotty can bootstrap without depending on a version + * of scalajs-ir built with a different Scala compiler, we add its + * sources instead of depending on the binaries. + */ + ivyConfigurations += config("sourcedeps").hide, + libraryDependencies += + "org.scala-js" %% "scalajs-ir" % scalaJSVersion % "sourcedeps", + sourceGenerators in Compile += Def.task { + val s = streams.value + val cacheDir = s.cacheDirectory + val trgDir = (sourceManaged in Compile).value / "scalajs-ir-src" + + val report = updateClassifiers.value + val scalaJSIRSourcesJar = report.select( + configuration = Set("sourcedeps"), + module = (_: ModuleID).name.startsWith("scalajs-ir_"), + artifact = artifactFilter(`type` = "src")).headOption.getOrElse { + sys.error(s"Could not fetch scalajs-ir sources") + } + + FileFunction.cached(cacheDir / s"fetchScalaJSIRSource", + FilesInfo.lastModified, FilesInfo.exists) { dependencies => + s.log.info(s"Unpacking scalajs-ir sources to $trgDir...") + if (trgDir.exists) + IO.delete(trgDir) + IO.createDirectory(trgDir) + IO.unzip(scalaJSIRSourcesJar, trgDir) + (trgDir ** "*.scala").get.toSet + } (Set(scalaJSIRSourcesJar)).toSeq + }.taskValue, + + // Adjust classpath for running dotty + mainClass in (Compile, run) := Some("dotty.tools.dotc.Main"), + fork in run := true, + fork in Test := true, + parallelExecution in Test := false, + + // http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/sbt-setting-jvm-boot-paramaters-for-scala + javaOptions <++= (dependencyClasspath in Runtime, packageBin in Compile) map { (attList, bin) => + // put the Scala {library, reflect} in the classpath + val path = for { + file <- attList.map(_.data) + path = file.getAbsolutePath + } yield "-Xbootclasspath/p:" + path + // dotty itself needs to be in the bootclasspath + val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList + // System.err.println("BOOTPATH: " + fullpath) + + val travis_build = // propagate if this is a travis build + if (sys.props.isDefinedAt(JENKINS_BUILD)) + List(s"-D$JENKINS_BUILD=${sys.props(JENKINS_BUILD)}") ::: jenkinsMemLimit + else + List() + + val tuning = + if (sys.props.isDefinedAt("Oshort")) + // Optimize for short-running applications, see https://github.com/lampepfl/dotty/issues/222 + List("-XX:+TieredCompilation", "-XX:TieredStopAtLevel=1") + else + List() + + ("-DpartestParentID=" + pid) :: tuning ::: agentOptions ::: travis_build ::: fullpath + } + ). + settings( + addCommandAlias("partest", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test;runPartestRunner") ++ + addCommandAlias("partest-only", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test-only dotc.tests;runPartestRunner") ++ + addCommandAlias("partest-only-no-bootstrap", ";test:package;package; lockPartestFile;test:test-only dotc.tests;runPartestRunner") + ) + + /** A sandbox to play with the Scala.js back-end of dotty. + * + * This sandbox is compiled with dotty with support for Scala.js. It can be + * used like any regular Scala.js project. In particular, `fastOptJS` will + * produce a .js file, and `run` will run the JavaScript code with a JS VM. + * + * Simply running `dotty/run -scalajs` without this sandbox is not very + * useful, as that would not provide the linker and JS runners. + */ + lazy val sjsSandbox = project.in(file("sandbox/scalajs")). + enablePlugins(ScalaJSPlugin). + settings( + overrideScalaVersionSetting, + + /* Remove the Scala.js compiler plugin for scalac, and enable the + * Scala.js back-end of dotty instead. + */ + libraryDependencies ~= { deps => + deps.filterNot(_.name.startsWith("scalajs-compiler")) + }, + scalacOptions += "-scalajs", + + // The main class cannot be found automatically due to the empty inc.Analysis + mainClass in Compile := Some("hello.world"), + + // While developing the Scala.js back-end, it is very useful to see the trees dotc gives us + scalacOptions += "-Xprint:labelDef", + + /* Debug-friendly Scala.js optimizer options. + * In particular, typecheck the Scala.js IR found on the classpath. + */ + scalaJSOptimizerOptions ~= { + _.withCheckScalaJSIR(true).withParallel(false) + } + ). + settings(compileWithDottySettings). + settings(inConfig(Compile)(Seq( + /* Make sure jsDependencyManifest runs after compile, otherwise compile + * might remove the entire directory afterwards. + */ + jsDependencyManifest <<= jsDependencyManifest.dependsOn(compile) + ))) + + lazy val `dotty-bench` = project.in(file("bench")). + dependsOn(dotty % "compile->test"). + settings( + overrideScalaVersionSetting, + + baseDirectory in (Test,run) := (baseDirectory in dotty).value, + + libraryDependencies ++= Seq("com.storm-enroute" %% "scalameter" % "0.6" % Test, + "com.novocode" % "junit-interface" % "0.11"), + + fork in Test := true, + parallelExecution in Test := false, + + // http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/sbt-setting-jvm-boot-paramaters-for-scala + javaOptions <++= (dependencyClasspath in Runtime, packageBin in Compile) map { (attList, bin) => + // put the Scala {library, reflect, compiler} in the classpath + val path = for { + file <- attList.map(_.data) + path = file.getAbsolutePath + prefix = if (path.endsWith(".jar")) "p" else "a" + } yield "-Xbootclasspath/" + prefix + ":" + path + // dotty itself needs to be in the bootclasspath + val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList + // System.err.println("BOOTPATH: " + fullpath) + + val travis_build = // propagate if this is a travis build + if (sys.props.isDefinedAt(JENKINS_BUILD)) + List(s"-D$JENKINS_BUILD=${sys.props(JENKINS_BUILD)}") + else + List() + val res = agentOptions ::: travis_build ::: fullpath + println("Running with javaOptions: " + res) + res + } + ) // Partest tasks lazy val lockPartestFile = TaskKey[Unit]("lockPartestFile", "Creates the lock file at ./tests/locks/partest-.lock") lazy val partestLockFile = new File("." + File.separator + "tests" + File.separator + "locks" + File.separator + s"partest-$pid.lock") def pid = java.lang.Long.parseLong(java.lang.management.ManagementFactory.getRuntimeMXBean().getName().split("@")(0)) - lazy val runPartestRunner = TaskKey[Unit]("runPartestRunner", "Runs partest") + lazy val runPartestRunner = InputKey[Unit]("runPartestRunner", "Runs partest") lazy val partestDeps = SettingKey[Seq[ModuleID]]("partestDeps", "Finds jars for partest dependencies") def getJarPaths(modules: Seq[ModuleID], ivyHome: Option[File]): Seq[String] = ivyHome match { - case Some(home) => - modules.map({ module => - val file = Path(home) / Path("cache") / + case Some(home) => + modules.map({ module => + val file = Path(home) / Path("cache") / Path(module.organization) / Path(module.name) / Path("jars") / Path(module.name + "-" + module.revision + ".jar") if (!file.isFile) throw new RuntimeException("ERROR: sbt getJarPaths: dependency jar not found: " + file) @@ -184,4 +289,99 @@ object DottyBuild extends Build { }) case None => throw new RuntimeException("ERROR: sbt getJarPaths: ivyHome not defined") } + + // Compile with dotty + lazy val compileWithDottySettings = { + inConfig(Compile)(inTask(compile)(Defaults.runnerTask) ++ Seq( + // Compile with dotty + fork in compile := true, + + compile := { + val inputs = (compileInputs in compile).value + import inputs.config._ + + val s = streams.value + val logger = s.log + val cacheDir = s.cacheDirectory + + // Discover classpaths + + def cpToString(cp: Seq[File]) = + cp.map(_.getAbsolutePath).mkString(java.io.File.pathSeparator) + + val compilerCp = Attributed.data((fullClasspath in (dotty, Compile)).value) + val cpStr = cpToString(classpath ++ compilerCp) + + // List all my dependencies (recompile if any of these changes) + + val allMyDependencies = classpath filterNot (_ == classesDirectory) flatMap { cpFile => + if (cpFile.isDirectory) (cpFile ** "*.class").get + else Seq(cpFile) + } + + // Compile + + val cachedCompile = FileFunction.cached(cacheDir / "compile", + FilesInfo.lastModified, FilesInfo.exists) { dependencies => + + logger.info( + "Compiling %d Scala sources to %s..." format ( + sources.size, classesDirectory)) + + if (classesDirectory.exists) + IO.delete(classesDirectory) + IO.createDirectory(classesDirectory) + + val sourcesArgs = sources.map(_.getAbsolutePath()).toList + + /* run.run() below in doCompile() will emit a call to its + * logger.info("Running dotty.tools.dotc.Main [...]") + * which we do not want to see. We use this patched logger to + * filter out that particular message. + */ + val patchedLogger = new Logger { + def log(level: Level.Value, message: => String) = { + val msg = message + if (level != Level.Info || + !msg.startsWith("Running dotty.tools.dotc.Main")) + logger.log(level, msg) + } + def success(message: => String) = logger.success(message) + def trace(t: => Throwable) = logger.trace(t) + } + + def doCompile(sourcesArgs: List[String]): Unit = { + val run = (runner in compile).value + run.run("dotty.tools.dotc.Main", compilerCp, + "-classpath" :: cpStr :: + "-d" :: classesDirectory.getAbsolutePath() :: + options ++: + sourcesArgs, + patchedLogger) foreach sys.error + } + + // Work around the Windows limitation on command line length. + val isWindows = + System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 + if ((fork in compile).value && isWindows && + (sourcesArgs.map(_.length).sum > 1536)) { + IO.withTemporaryFile("sourcesargs", ".txt") { sourceListFile => + IO.writeLines(sourceListFile, sourcesArgs) + doCompile(List("@"+sourceListFile.getAbsolutePath())) + } + } else { + doCompile(sourcesArgs) + } + + // Output is all files in classesDirectory + (classesDirectory ** AllPassFilter).get.toSet + } + + cachedCompile((sources ++ allMyDependencies).toSet) + + // We do not have dependency analysis when compiling externally + sbt.inc.Analysis.Empty + } + )) + } } diff --git a/project/build.properties b/project/build.properties index be6c454fbaca..43b8278c68cf 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.13.5 +sbt.version=0.13.11 diff --git a/project/plugins.sbt b/project/plugins.sbt index 47bc44dfe35e..57bd465815b4 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -3,7 +3,8 @@ // e.g. addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0") // Scala IDE project file generator -addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.2.0") +addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0") -addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.6.0") +addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.8.0") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.8") diff --git a/sandbox/scalajs/hello.scala b/sandbox/scalajs/hello.scala new file mode 100644 index 000000000000..bd4aa8cc5f55 --- /dev/null +++ b/sandbox/scalajs/hello.scala @@ -0,0 +1,15 @@ +package hello + +import scala.scalajs.js + +trait MyTrait { + val x = 5 + def foo(y: Int) = x +} + +object world extends js.JSApp with MyTrait { + def main(): Unit = { + println("hello dotty.js!") + println(foo(4)) + } +} diff --git a/scripts/common b/scripts/common new file mode 100755 index 000000000000..77da64d83542 --- /dev/null +++ b/scripts/common @@ -0,0 +1,19 @@ +update() { + [[ -d $baseDir ]] || mkdir -p $baseDir + cd $baseDir + + if [ ! -d $baseDir/$3 ]; then git clone "https://github.com/$1/$2.git" $3; fi + + cd $3 + + git fetch --tags "https://github.com/$1/$2.git" + (git fetch "https://github.com/$1/$2.git" $4 && git checkout -fq FETCH_HEAD) #|| git checkout -fq $4 # || fallback is for local testing on tag + git reset --hard + echo updated $1/$2:$4 under $3. Last commits: + git log --pretty=format:"%h - %an, %aD : %s" -n 10 # show last commits + cd - +} + +export LC_ALL=en_US.UTF-8 + +sbtArgs="-Ddotty.jenkins.build=yes -Dfile.encoding=UTF-8 -ivy $baseDir/ivy2 -Dsbt.global.base=$HOME/.sbt/0.13 -sbt-dir $HOME/.sbt/0.13" diff --git a/scripts/jobs/validate/junit b/scripts/jobs/validate/junit new file mode 100755 index 000000000000..cf9b49c33fe6 --- /dev/null +++ b/scripts/jobs/validate/junit @@ -0,0 +1,9 @@ +#!/bin/bash -e + +baseDir=${WORKSPACE-`pwd`} +scriptsDir="$baseDir/scripts" +. $scriptsDir/common + +git log --pretty=format:"%h - %an, %aD : %s" -n 10 +update DarkDimius scala scala-scala dotty-library +sbt $sbtArgs update compile test diff --git a/scripts/jobs/validate/partest b/scripts/jobs/validate/partest new file mode 100755 index 000000000000..4d4eb57e615f --- /dev/null +++ b/scripts/jobs/validate/partest @@ -0,0 +1,11 @@ +#!/bin/bash -e + +baseDir=${WORKSPACE-`pwd`} +scriptsDir="$baseDir/scripts" +. $scriptsDir/common + +git log --pretty=format:"%h - %an, %aD : %s" -n 10 + +update DarkDimius scala scala-scala dotty-library +sbt $sbtArgs update compile "partest-only-no-bootstrap --show-diff --verbose" + diff --git a/scripts/jobs/validate/partest-bootstrapped b/scripts/jobs/validate/partest-bootstrapped new file mode 100755 index 000000000000..2f127f55b316 --- /dev/null +++ b/scripts/jobs/validate/partest-bootstrapped @@ -0,0 +1,11 @@ +#!/bin/bash -e + +baseDir=${WORKSPACE-`pwd`} +scriptsDir="$baseDir/scripts" +. $scriptsDir/common + +git log --pretty=format:"%h - %an, %aD : %s" -n 10 + +update DarkDimius scala scala-scala dotty-library +sbt $sbtArgs update compile "partest-only --show-diff --verbose" + diff --git a/scripts/jobs/validate/scalastyle b/scripts/jobs/validate/scalastyle new file mode 100755 index 000000000000..31ace83bae0c --- /dev/null +++ b/scripts/jobs/validate/scalastyle @@ -0,0 +1,8 @@ +#!/bin/bash -e + +baseDir=${WORKSPACE-`pwd`} +scriptsDir="$baseDir/scripts" +. $scriptsDir/common + +sbt $sbtArgs scalastyle + diff --git a/src/dotty/DottyPredef.scala b/src/dotty/DottyPredef.scala index 7c8098bd1763..9170da476f97 100644 --- a/src/dotty/DottyPredef.scala +++ b/src/dotty/DottyPredef.scala @@ -1,25 +1,13 @@ package dotty -import scala.reflect.ClassTag import scala.reflect.runtime.universe.TypeTag -import scala.Predef.??? // this is currently ineffective, because of #530 +import scala.reflect.ClassTag +import scala.Predef.??? +/** unimplemented implicit for TypeTag */ object DottyPredef { - /** implicits for ClassTag and TypeTag. Should be implemented with macros */ - implicit def classTag[T]: ClassTag[T] = scala.Predef.??? - implicit def typeTag[T]: TypeTag[T] = scala.Predef.??? - + implicit def typeTag[T]: TypeTag[T] = ??? - /** ClassTags for final classes */ - implicit val IntClassTag: ClassTag[Int] = ClassTag.Int - implicit val ByteClassTag: ClassTag[Byte] = ClassTag.Byte - implicit val ShortClassTag: ClassTag[Short] = ClassTag.Short - implicit val CharClassTag: ClassTag[Char] = ClassTag.Char - implicit val LongClassTag: ClassTag[Long] = ClassTag.Long - implicit val FloatClassTag: ClassTag[Float] = ClassTag.Float - implicit val DoubleClassTag: ClassTag[Double] = ClassTag.Double - implicit val BooleanClassTag: ClassTag[Boolean] = ClassTag.Boolean - implicit val UnitClassTag: ClassTag[Unit] = ClassTag.Unit - implicit val NullClassTag: ClassTag[Null] = ClassTag.Null - implicit val NothingClassTag: ClassTag[Nothing] = ClassTag.Nothing + implicit def arrayTag[T](implicit ctag: ClassTag[T]): ClassTag[Array[T]] = + ctag.wrap } diff --git a/src/dotty/annotation/internal/Alias.scala b/src/dotty/annotation/internal/Alias.scala index de51153f1ab4..8be83960f956 100644 --- a/src/dotty/annotation/internal/Alias.scala +++ b/src/dotty/annotation/internal/Alias.scala @@ -2,6 +2,9 @@ package dotty.annotation.internal import scala.annotation.Annotation +/** An annotation to record a Scala2 pickled alias. + * @param aliased A TermRef pointing to the aliased field. + */ class Alias(aliased: Any) extends Annotation { } diff --git a/src/dotty/annotation/internal/AnnotationDefault.scala b/src/dotty/annotation/internal/AnnotationDefault.scala index 90471b8fec38..7409b2f96b33 100644 --- a/src/dotty/annotation/internal/AnnotationDefault.scala +++ b/src/dotty/annotation/internal/AnnotationDefault.scala @@ -2,6 +2,7 @@ package dotty.annotation.internal import scala.annotation.Annotation +/** An annotation to tag Java annotation default values */ class AnnotationDefault extends Annotation { } diff --git a/src/dotty/annotation/internal/Child.scala b/src/dotty/annotation/internal/Child.scala index ac253ed8d07f..9295de73e2c6 100644 --- a/src/dotty/annotation/internal/Child.scala +++ b/src/dotty/annotation/internal/Child.scala @@ -2,6 +2,15 @@ package dotty.annotation.internal import scala.annotation.Annotation -class Child[T] extends Annotation { - -} +/** An annotation to indicate a child class or object of the annotated class. + * E.g. if we have + * + * sealed class A + * case class B() extends A + * case class C() extends A + * + * Then the class symbol `A` would carry the annotations + * `@Child[Bref] @Child[Cref]` where `Bref`, `Cref` are TypeRefs + * referring to the class symbols of `B` and `C` + */ +class Child[T] extends Annotation diff --git a/src/dotty/annotation/internal/Repeated.scala b/src/dotty/annotation/internal/Repeated.scala index 3065b7a06567..24adc051fe27 100644 --- a/src/dotty/annotation/internal/Repeated.scala +++ b/src/dotty/annotation/internal/Repeated.scala @@ -2,4 +2,9 @@ package dotty.annotation.internal import scala.annotation.Annotation +/** An annotation produced by desugaring to indicate that a + * sequence is a repeated parameter. I.e. + * + * T* is expanded by Desugar to Seq[T] @Repeated + */ final class Repeated() extends Annotation diff --git a/src/dotty/annotation/internal/SourceFile.scala b/src/dotty/annotation/internal/SourceFile.scala new file mode 100644 index 000000000000..c49fc2c8dc4a --- /dev/null +++ b/src/dotty/annotation/internal/SourceFile.scala @@ -0,0 +1,10 @@ +package dotty.annotation.internal + +import scala.annotation.Annotation + +/** An annotation to record a Scala2 pickled alias. + * @param aliased A TermRef pointing to the aliased field. + */ +class SourceFile(path: String) extends Annotation { + +} diff --git a/src/dotty/annotation/internal/UnsafeNonvariant.scala b/src/dotty/annotation/internal/UnsafeNonvariant.scala new file mode 100644 index 000000000000..43a0a114b87a --- /dev/null +++ b/src/dotty/annotation/internal/UnsafeNonvariant.scala @@ -0,0 +1,8 @@ +package dotty.annotation.internal + +import scala.annotation.Annotation + +/** This annotation is used as a marker for unsafe + * instantiations in asSeenFrom. See TypeOps.asSeenfrom for an explanation. + */ +class UnsafeNonvariant extends Annotation diff --git a/src/dotty/runtime/Arrays.scala b/src/dotty/runtime/Arrays.scala index 1fb4fe5ebc69..9ec5512ad86b 100644 --- a/src/dotty/runtime/Arrays.scala +++ b/src/dotty/runtime/Arrays.scala @@ -2,11 +2,15 @@ package dotty.runtime import scala.reflect.ClassTag +import java.lang.{reflect => jlr} + /** All but the first two operations should be short-circuited and implemented specially by * the backend. */ object Arrays { + // note: this class is magical. Do not touch it unless you know what you are doing.` + /** Creates an array of some element type determined by the given `ClassTag` * argument. The erased type of applications of this method is `Object`. */ @@ -20,35 +24,8 @@ object Arrays { arr } - /** Create an array of type T. T must be of form Array[E], with - * E being a reference type. + /** Create an array of a reference type T. */ - def newRefArray[T](length: Int): T = ??? - - /** Create a Byte[] array */ - def newByteArray(length: Int): Array[Byte] = ??? - - /** Create a Short[] array */ - def newShortArray(length: Int): Array[Short] = ??? - - /** Create a Char[] array */ - def newCharArray(length: Int): Array[Char] = ??? - - /** Create an Int[] array */ - def newIntArray(length: Int): Array[Int] = ??? - - /** Create a Long[] array */ - def newLongArray(length: Int): Array[Long] = ??? - - /** Create a Float[] array */ - def newFloatArray(length: Int): Array[Float] = ??? - - /** Create a Double[] array */ - def newDoubleArray(length: Int): Array[Double] = ??? - - /** Create a Boolean[] array */ - def newBooleanArray(length: Int): Array[Boolean] = ??? - - /** Create a scala.runtime.BoxedUnit[] array */ - def newUnitArray(length: Int): Array[Unit] = ??? + def newArray[Arr](componentType: Class[_], returnType: Class[Arr], dimensions: Array[Int]): Arr = + jlr.Array.newInstance(componentType, dimensions: _*).asInstanceOf[Arr] } diff --git a/src/dotty/runtime/LazyVals.scala b/src/dotty/runtime/LazyVals.scala index 38deec6f58ac..f09e96f57851 100644 --- a/src/dotty/runtime/LazyVals.scala +++ b/src/dotty/runtime/LazyVals.scala @@ -8,8 +8,8 @@ import scala.annotation.tailrec object LazyVals { private val unsafe = scala.concurrent.util.Unsafe.instance - final val BITS_PER_LAZY_VAL = 2 - final val LAZY_VAL_MASK = 3 + final val BITS_PER_LAZY_VAL = 2L + final val LAZY_VAL_MASK = 3L @inline def STATE(cur: Long, ord: Int) = (cur >> (ord * BITS_PER_LAZY_VAL)) & LAZY_VAL_MASK @inline def CAS(t: Object, offset: Long, e: Long, v: Int, ord: Int) = { diff --git a/src/dotty/runtime/LegacyApp.scala b/src/dotty/runtime/LegacyApp.scala new file mode 100644 index 000000000000..2c4b295d05b5 --- /dev/null +++ b/src/dotty/runtime/LegacyApp.scala @@ -0,0 +1,9 @@ +package dotty.runtime + + +/** + * replaces the `scala.App` class which relies on `DelayedInit` functionality, not supported by Dotty. + */ +class LegacyApp { + def main(args: Array[String]): Unit = () +} diff --git a/src/dotty/runtime/vc/VCPrototype.scala b/src/dotty/runtime/vc/VCPrototype.scala index 0a3abe5a25e6..212046ef8922 100644 --- a/src/dotty/runtime/vc/VCPrototype.scala +++ b/src/dotty/runtime/vc/VCPrototype.scala @@ -10,7 +10,7 @@ abstract class VCArrayPrototype[T <: VCPrototype] extends Object with Cloneable def apply(idx: Int): Object def update(idx: Int, el: T): Unit def length: Int - override def clone: Object = super.clone() + override def clone(): Object = super.clone() } diff --git a/src/dotty/tools/backend/jvm/CollectEntryPoints.scala b/src/dotty/tools/backend/jvm/CollectEntryPoints.scala index 513144bd6b3b..3ed232bc7643 100644 --- a/src/dotty/tools/backend/jvm/CollectEntryPoints.scala +++ b/src/dotty/tools/backend/jvm/CollectEntryPoints.scala @@ -40,7 +40,7 @@ class CollectEntryPoints extends MiniPhaseTransform { def phaseName: String = "Collect entry points" override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { - if ((tree.symbol ne NoSymbol) && CollectEntryPoints.isJavaEntyPoint(tree.symbol)) { + if ((tree.symbol ne NoSymbol) && CollectEntryPoints.isJavaEntryPoint(tree.symbol)) { ctx.genBCodePhase.asInstanceOf[GenBCode].registerEntryPoint(tree.symbol) } tree @@ -49,18 +49,15 @@ class CollectEntryPoints extends MiniPhaseTransform { object CollectEntryPoints{ def isJavaMainMethod(sym: Symbol)(implicit ctx: Context) = { - val d = ctx.definitions - val StringType = d.StringType - (sym.name == nme.main) && (sym.info match { - case r@MethodType(_, List(d.ArrayType(t))) => - (t.widenDealias =:= StringType) && ( - r.resultType.widenDealias =:= d.UnitType) + case r@MethodType(_, List(defn.ArrayOf(t))) => + (t.widenDealias =:= defn.StringType) && ( + r.resultType.widenDealias =:= defn.UnitType) case _ => false }) } - def isJavaEntyPoint(sym: Symbol)(implicit ctx: Context): Boolean = { + def isJavaEntryPoint(sym: Symbol)(implicit ctx: Context): Boolean = { import Types.MethodType val d = ctx.definitions val StringType = d.StringType @@ -105,7 +102,7 @@ object CollectEntryPoints{ 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 + // Now either succeed, or issue some additional warnings for things which look like // attempts to be java main methods. else (possibles exists(x=> isJavaMainMethod(x.symbol))) || { possibles exists { m => diff --git a/src/dotty/tools/backend/jvm/DottyBackendInterface.scala b/src/dotty/tools/backend/jvm/DottyBackendInterface.scala index b2f111dbe61a..a64ce5900fc8 100644 --- a/src/dotty/tools/backend/jvm/DottyBackendInterface.scala +++ b/src/dotty/tools/backend/jvm/DottyBackendInterface.scala @@ -32,7 +32,7 @@ import NameOps._ import StdNames.nme import NameOps._ -class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ +class DottyBackendInterface(outputDirectory: AbstractFile)(implicit ctx: Context) extends BackendInterface{ type Symbol = Symbols.Symbol type Type = Types.Type type Tree = tpd.Tree @@ -99,9 +99,6 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ val nme_PACKAGE: Name = StdNames.nme.PACKAGE val nme_EQEQ_LOCAL_VAR: Name = StdNames.nme.EQEQ_LOCAL_VAR - val BoxesRunTimeModule = ctx.requiredModule("scala.runtime.BoxesRunTime") - val BoxesRunTimeClass = toDenot(BoxesRunTimeModule).moduleClass.asClass - // require LambdaMetafactory: scalac uses getClassIfDefined, but we need those always. override lazy val LambdaMetaFactory = ctx.requiredClass("java.lang.invoke.LambdaMetafactory") override lazy val MethodHandle = ctx.requiredClass("java.lang.invoke.MethodHandle") @@ -133,17 +130,17 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ } val hashMethodSym: Symbol = NoSymbol // used to dispatch ## on primitives to ScalaRuntime.hash. Should be implemented by a miniphase - val externalEqualsNumNum: Symbol = ctx.requiredMethod(BoxesRunTimeClass, nme.equalsNumNum) - lazy val externalEqualsNumChar: Symbol = ??? // ctx.requiredMethod(BoxesRunTimeClass, nme.equalsNumChar) // this method is private - val externalEqualsNumObject: Symbol = ctx.requiredMethod(BoxesRunTimeClass, nme.equalsNumObject) - val externalEquals: Symbol = BoxesRunTimeClass.info.decl(nme.equals_).suchThat(toDenot(_).info.firstParamTypes.size == 2).symbol + val externalEqualsNumNum: Symbol = defn.BoxesRunTimeModule.requiredMethod(nme.equalsNumNum) + val externalEqualsNumChar: Symbol = NoSymbol // ctx.requiredMethod(BoxesRunTimeTypeRef, nme.equalsNumChar) // this method is private + val externalEqualsNumObject: Symbol = defn.BoxesRunTimeModule.requiredMethod(nme.equalsNumObject) + val externalEquals: Symbol = defn.BoxesRunTimeClass.info.decl(nme.equals_).suchThat(toDenot(_).info.firstParamTypes.size == 2).symbol val MaxFunctionArity: Int = Definitions.MaxFunctionArity - val FunctionClass: Array[Symbol] = defn.FunctionClass.asInstanceOf[Array[Symbol]] - val AbstractFunctionClass: Array[Symbol] = defn.AbstractFunctionClass.asInstanceOf[Array[Symbol]] + val FunctionClass: Array[Symbol] = defn.FunctionClassPerRun() + val AbstractFunctionClass: Array[Symbol] = defn.AbstractFunctionClassPerRun() val PartialFunctionClass: Symbol = defn.PartialFunctionClass val AbstractPartialFunctionClass: Symbol = defn.AbstractPartialFunctionClass val String_valueOf: Symbol = defn.String_valueOf_Object - lazy val Predef_classOf: Symbol = ctx.requiredMethod(toDenot(defn.ScalaPredefModule).moduleClass.asClass, nme.classOf) + lazy val Predef_classOf: Symbol = defn.ScalaPredefModule.requiredMethod(nme.classOf) lazy val AnnotationRetentionAttr = ctx.requiredClass("java.lang.annotation.Retention") lazy val AnnotationRetentionSourceAttr = ctx.requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("SOURCE") @@ -151,15 +148,14 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ lazy val AnnotationRetentionRuntimeAttr = ctx.requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("RUNTIME") lazy val JavaAnnotationClass = ctx.requiredClass("java.lang.annotation.Annotation") - - def boxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses.map{x => + def boxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses().map{x => // @darkdimius Are you sure this should be a def? (x, Erasure.Boxing.boxMethod(x.asClass)) }.toMap - def unboxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses.map(x => (x, Erasure.Boxing.unboxMethod(x.asClass))).toMap - - private val mkArrayNames: Set[String] = Set("Byte", "Float", "Char", "Double", "Boolean", "Unit", "Long", "Int", "Short", "Ref") + def unboxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses().map(x => (x, Erasure.Boxing.unboxMethod(x.asClass))).toMap - override lazy val syntheticArrayConstructors: Set[Symbol] = mkArrayNames.map(nm => ctx.requiredMethod(toDenot(defn.DottyArraysModule).moduleClass.asClass, s"new${nm}Array")) + override def isSyntheticArrayConstructor(s: Symbol) = { + s eq defn.newArrayMethod + } def isBox(sym: Symbol): Boolean = Erasure.Boxing.isBox(sym) def isUnbox(sym: Symbol): Boolean = Erasure.Boxing.isUnbox(sym) @@ -480,7 +476,7 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ implicit def positionHelper(a: Position): PositionHelper = new PositionHelper { def isDefined: Boolean = a.exists - def line: Int = sourcePos(a).line + def line: Int = sourcePos(a).line + 1 def finalPosition: Position = a } @@ -598,7 +594,8 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ isPrivate //|| (sym.isPrimaryConstructor && sym.owner.isTopLevelModuleClass) def isFinal: Boolean = sym is Flags.Final - def isStaticMember: Boolean = (sym ne NoSymbol) && ((sym is Flags.JavaStatic) || (owner is Flags.ImplClass)) + def isStaticMember: Boolean = (sym ne NoSymbol) && + ((sym is Flags.JavaStatic) || (owner is Flags.ImplClass) || toDenot(sym).hasAnnotation(ctx.definitions.ScalaStaticAnnot)) // guard against no sumbol cause this code is executed to select which call type(static\dynamic) to use to call array.clone def isBottomClass: Boolean = (sym ne defn.NullClass) && (sym ne defn.NothingClass) @@ -614,10 +611,10 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ def hasModuleFlag: Boolean = sym is Flags.Module def isSynchronized: Boolean = sym is Flags.Synchronized def isNonBottomSubClass(other: Symbol): Boolean = sym.derivesFrom(other) - def hasAnnotation(sym: Symbol): Boolean = false - def shouldEmitForwarders: Boolean = //exitingPickler { !(sym.name.toString contains '$') - (sym is Flags.Module) && !(sym is Flags.ImplClass) /// !sym.isNestedClass - def isJavaEntryPoint: Boolean = CollectEntryPoints.isJavaEntyPoint(sym) + def hasAnnotation(ann: Symbol): Boolean = toDenot(sym).hasAnnotation(ann) + def shouldEmitForwarders: Boolean = + (sym is Flags.Module) && !(sym is Flags.ImplClass) && sym.isStatic + def isJavaEntryPoint: Boolean = CollectEntryPoints.isJavaEntryPoint(sym) def isClassConstructor: Boolean = toDenot(sym).isClassConstructor @@ -625,7 +622,16 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ * True for module classes of modules that are top-level or owned only by objects. Module classes * for such objects will get a MODULE$ flag and a corresponding static initializer. */ - def isStaticModuleClass: Boolean = sym.isStatic && (sym is Flags.Module) + def isStaticModuleClass: Boolean = + (sym is Flags.Module) && { + // scalac uses atPickling here + // this would not work if modules are created after pickling + // for example by specialization + val original = toDenot(sym).initial + val validity = original.validFor + val shiftedContext = ctx.withPhase(validity.phaseId) + toDenot(sym)(shiftedContext).isStatic(shiftedContext) + } def isStaticConstructor: Boolean = isStaticMember && isClassConstructor @@ -722,7 +728,7 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ def setter(clz: Symbol): Symbol = decorateSymbol(sym).setter def moduleSuffix: String = "" // todo: validate that names already have $ suffix - def outputDirectory: AbstractFile = new PlainDirectory(new Directory(new JFile(ctx.settings.d.value))) + def outputDirectory: AbstractFile = DottyBackendInterface.this.outputDirectory def pos: Position = sym.pos def throwsAnnotations: List[Symbol] = Nil @@ -777,7 +783,7 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ def decls: List[Symbol] = tp.decls.map(_.symbol).toList def members: List[Symbol] = - tp.memberDenots(takeAllFilter, (name, buf) => buf ++= member(name).alternatives).map(_.symbol).toList + tp.memberDenots(takeAllFilter, (name, buf) => buf ++= tp.member(name).alternatives).map(_.symbol).toList def typeSymbol: Symbol = tp.widenDealias.typeSymbol @@ -831,7 +837,7 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ * meta-annotated annotations (@(ann @getter) val x = 0), so we don't emit a warning. * The type in the AnnotationInfo is an AnnotatedTpe. Tested in jvm/annotations.scala. */ - case a @ AnnotatedType(_, t) => + case a @ AnnotatedType(t, _) => debuglog(s"typeKind of annotated type $a") t.toTypeKind(ct)(storage) diff --git a/src/dotty/tools/backend/jvm/GenBCode.scala b/src/dotty/tools/backend/jvm/GenBCode.scala index d7557acecb10..8cec93977694 100644 --- a/src/dotty/tools/backend/jvm/GenBCode.scala +++ b/src/dotty/tools/backend/jvm/GenBCode.scala @@ -13,6 +13,9 @@ import dotty.tools.dotc import dotty.tools.dotc.backend.jvm.DottyPrimitives import dotty.tools.dotc.transform.Erasure +import dotty.tools.dotc.interfaces +import java.util.Optional + import scala.reflect.ClassTag import dotty.tools.dotc.core._ import Periods._ @@ -23,11 +26,13 @@ import Symbols._ import Denotations._ import Phases._ import java.lang.AssertionError +import java.io.{ File => JFile } import scala.tools.asm import scala.tools.asm.tree._ import dotty.tools.dotc.util.{Positions, DotClass} import tpd._ import StdNames._ +import scala.reflect.io.{Directory, PlainDirectory, AbstractFile} import scala.tools.nsc.backend.jvm.opt.LocalOpt @@ -36,9 +41,11 @@ class GenBCode extends Phase { private val entryPoints = new mutable.HashSet[Symbol]() def registerEntryPoint(sym: Symbol) = entryPoints += sym + def outputDir(implicit ctx: Context): AbstractFile = + new PlainDirectory(new Directory(new JFile(ctx.settings.d.value))) def run(implicit ctx: Context): Unit = { - new GenBCodePipeline(entryPoints.toList, new DottyBackendInterface()(ctx))(ctx).run(ctx.compilationUnit.tpdTree) + new GenBCodePipeline(entryPoints.toList, new DottyBackendInterface(outputDir)(ctx))(ctx).run(ctx.compilationUnit.tpdTree) entryPoints.clear() } } @@ -47,6 +54,18 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter var tree: Tree = _ + val sourceFile = ctx.compilationUnit.source + + /** Convert a `scala.reflect.io.AbstractFile` into a + * `dotty.tools.dotc.interfaces.AbstractFile`. + */ + private[this] def convertAbstractFile(absfile: scala.reflect.io.AbstractFile): interfaces.AbstractFile = + new interfaces.AbstractFile { + override def name = absfile.name + override def path = absfile.path + override def jfile = Optional.ofNullable(absfile.file) + } + final class PlainClassBuilder(cunit: CompilationUnit) extends SyncAndTryBuilder(cunit) @@ -179,7 +198,7 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter for (pickler <- ctx.compilationUnit.picklers.get(claszSymbol.asClass)) { val binary = pickler.assembleParts() val dataAttr = new CustomAttr(nme.TASTYATTR.toString, binary) - plainC.visitAttribute(dataAttr) + (if (mirrorC ne null) mirrorC else plainC).visitAttribute(dataAttr) } // -------------- bean info class, if needed -------------- @@ -300,6 +319,9 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter bytecodeWriter.close() // Statistics.stopTimer(BackendStats.bcodeTimer, bcodeStart) + if (ctx.compilerCallback != null) + ctx.compilerCallback.onSourceCompiled(sourceFile) + /* TODO Bytecode can be verified (now that all classfiles have been written to disk) * * (1) asm.util.CheckAdapter.verify() @@ -342,7 +364,7 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter tree match { case EmptyTree => () case PackageDef(_, stats) => stats foreach gen - case ValDef(name, tpt, rhs) => () // module val not emmited + case ValDef(name, tpt, rhs) => () // module val not emitted case cd: TypeDef => q1 add Item1(arrivalPos, cd, int.currentUnit) arrivalPos += 1 @@ -363,6 +385,10 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter if (outFolder == null) null else getFileForClassfile(outFolder, jclassName, ".class") bytecodeWriter.writeClass(jclassName, jclassName, jclassBytes, outFile) + + val className = jclassName.replace('/', '.') + if (ctx.compilerCallback != null) + ctx.compilerCallback.onClassGenerated(sourceFile, convertAbstractFile(outFile), className) } catch { case e: FileConflictException => diff --git a/src/dotty/tools/backend/jvm/scalaPrimitives.scala b/src/dotty/tools/backend/jvm/scalaPrimitives.scala index 8e742faf1952..0027defa7eeb 100644 --- a/src/dotty/tools/backend/jvm/scalaPrimitives.scala +++ b/src/dotty/tools/backend/jvm/scalaPrimitives.scala @@ -69,7 +69,7 @@ class DottyPrimitives(ctx: Context) { } def elementType: Type = tpe.widenDealias match { - case defn.ArrayType(el) => el + case defn.ArrayOf(el) => el case JavaArrayType(el) => el case _ => ctx.error(s"expected Array $tpe") @@ -79,42 +79,42 @@ class DottyPrimitives(ctx: Context) { code match { case APPLY => - elementType.classSymbol match { - case defn.BooleanClass => ZARRAY_GET - case defn.ByteClass => BARRAY_GET - case defn.ShortClass => SARRAY_GET - case defn.CharClass => CARRAY_GET - case defn.IntClass => IARRAY_GET - case defn.LongClass => LARRAY_GET - case defn.FloatClass => FARRAY_GET - case defn.DoubleClass => DARRAY_GET - case _ => OARRAY_GET + defn.scalaClassName(elementType) match { + case tpnme.Boolean => ZARRAY_GET + case tpnme.Byte => BARRAY_GET + case tpnme.Short => SARRAY_GET + case tpnme.Char => CARRAY_GET + case tpnme.Int => IARRAY_GET + case tpnme.Long => LARRAY_GET + case tpnme.Float => FARRAY_GET + case tpnme.Double => DARRAY_GET + case _ => OARRAY_GET } case UPDATE => - elementType.classSymbol match { - case defn.BooleanClass => ZARRAY_SET - case defn.ByteClass => BARRAY_SET - case defn.ShortClass => SARRAY_SET - case defn.CharClass => CARRAY_SET - case defn.IntClass => IARRAY_SET - case defn.LongClass => LARRAY_SET - case defn.FloatClass => FARRAY_SET - case defn.DoubleClass => DARRAY_SET - case _ => OARRAY_SET + defn.scalaClassName(elementType) match { + case tpnme.Boolean => ZARRAY_SET + case tpnme.Byte => BARRAY_SET + case tpnme.Short => SARRAY_SET + case tpnme.Char => CARRAY_SET + case tpnme.Int => IARRAY_SET + case tpnme.Long => LARRAY_SET + case tpnme.Float => FARRAY_SET + case tpnme.Double => DARRAY_SET + case _ => OARRAY_SET } case LENGTH => - elementType.classSymbol match { - case defn.BooleanClass => ZARRAY_LENGTH - case defn.ByteClass => BARRAY_LENGTH - case defn.ShortClass => SARRAY_LENGTH - case defn.CharClass => CARRAY_LENGTH - case defn.IntClass => IARRAY_LENGTH - case defn.LongClass => LARRAY_LENGTH - case defn.FloatClass => FARRAY_LENGTH - case defn.DoubleClass => DARRAY_LENGTH - case _ => OARRAY_LENGTH + defn.scalaClassName(elementType) match { + case tpnme.Boolean => ZARRAY_LENGTH + case tpnme.Byte => BARRAY_LENGTH + case tpnme.Short => SARRAY_LENGTH + case tpnme.Char => CARRAY_LENGTH + case tpnme.Int => IARRAY_LENGTH + case tpnme.Long => LARRAY_LENGTH + case tpnme.Float => FARRAY_LENGTH + case tpnme.Double => DARRAY_LENGTH + case _ => OARRAY_LENGTH } case _ => diff --git a/src/dotty/tools/backend/sjs/GenSJSIR.scala b/src/dotty/tools/backend/sjs/GenSJSIR.scala new file mode 100644 index 000000000000..819a8f0e37f6 --- /dev/null +++ b/src/dotty/tools/backend/sjs/GenSJSIR.scala @@ -0,0 +1,14 @@ +package dotty.tools.backend.sjs + +import dotty.tools.dotc.core._ +import Contexts._ +import Phases._ + +/** Generates Scala.js IR files for the compilation unit. */ +class GenSJSIR extends Phase { + def phaseName: String = "genSJSIR" + + def run(implicit ctx: Context): Unit = { + new JSCodeGen().run() + } +} diff --git a/src/dotty/tools/backend/sjs/JSCodeGen.scala b/src/dotty/tools/backend/sjs/JSCodeGen.scala new file mode 100644 index 000000000000..401e01784777 --- /dev/null +++ b/src/dotty/tools/backend/sjs/JSCodeGen.scala @@ -0,0 +1,2392 @@ +package dotty.tools.backend.sjs + +import scala.annotation.switch + +import scala.collection.mutable + +import dotty.tools.FatalError + +import dotty.tools.dotc.CompilationUnit +import dotty.tools.dotc.ast.tpd +import dotty.tools.dotc.core.Phases.Phase + +import dotty.tools.dotc.core._ +import Periods._ +import SymDenotations._ +import Contexts._ +import Decorators._ +import Flags._ +import dotty.tools.dotc.ast.Trees._ +import Types._ +import Symbols._ +import Denotations._ +import Phases._ +import StdNames._ + +import dotty.tools.dotc.transform.Erasure + +import org.scalajs.core.ir +import org.scalajs.core.ir.{ClassKind, Position, Trees => js, Types => jstpe} +import js.OptimizerHints + +import JSEncoding._ +import JSInterop._ +import ScopedVar.withScopedVars + +/** Main codegen for Scala.js IR. + * + * [[GenSJSIR]] creates one instance of `JSCodeGen` per compilation unit. + * The `run()` method processes the whole compilation unit and generates + * `.sjsir` files for it. + * + * There are 4 main levels of translation: + * + * - `genCompilationUnit()` iterates through all the type definitions in the + * compilation unit. Each generated `js.ClassDef` is serialized to an + * `.sjsir` file. + * - `genScalaClass()` and other similar methods generate the skeleton of + * classes. + * - `genMethod()` and similar methods generate the declarations of methods. + * - `genStatOrExpr()` and everything else generate the bodies of methods. + */ +class JSCodeGen()(implicit ctx: Context) { + import tpd._ + + private val jsdefn = JSDefinitions.jsdefn + private val primitives = new JSPrimitives(ctx) + + private val positionConversions = new JSPositions()(ctx) + import positionConversions.{pos2irPos, implicitPos2irPos} + + // Some state -------------------------------------------------------------- + + private val currentClassSym = new ScopedVar[Symbol] + private val currentMethodSym = new ScopedVar[Symbol] + private val localNames = new ScopedVar[LocalNameGenerator] + private val thisLocalVarIdent = new ScopedVar[Option[js.Ident]] + private val undefinedDefaultParams = new ScopedVar[mutable.Set[Symbol]] + + /** Implicitly materializes the current local name generator. */ + private implicit def implicitLocalNames: LocalNameGenerator = localNames.get + + /* See genSuperCall() + * TODO Can we avoid this unscoped var? + */ + private var isModuleInitialized: Boolean = false + + private def currentClassType = encodeClassType(currentClassSym) + + /** Returns a new fresh local identifier. */ + private def freshLocalIdent()(implicit pos: Position): js.Ident = + localNames.get.freshLocalIdent() + + /** Returns a new fresh local identifier. */ + private def freshLocalIdent(base: String)(implicit pos: Position): js.Ident = + localNames.get.freshLocalIdent(base) + + // Compilation unit -------------------------------------------------------- + + def run(): Unit = { + genCompilationUnit(ctx.compilationUnit) + } + + /** Generates the Scala.js IR for a compilation unit + * This method iterates over all the class and interface definitions + * found in the compilation unit and emits their IR (.sjsir). + * + * Some classes are never actually emitted: + * - Classes representing primitive types + * - The scala.Array class + * + * TODO Some classes representing anonymous functions are not actually emitted. + * Instead, a temporary representation of their `apply` method is built + * and recorded, so that it can be inlined as a JavaScript anonymous + * function in the method that instantiates it. + * + * Other ClassDefs are emitted according to their nature: + * * Scala.js-defined JS class -> `genScalaJSDefinedJSClass()` + * * Other raw JS type (<: js.Any) -> `genRawJSClassData()` + * * Interface -> `genInterface()` + * * Normal class -> `genClass()` + */ + private def genCompilationUnit(cunit: CompilationUnit): Unit = { + def collectTypeDefs(tree: Tree): List[TypeDef] = { + tree match { + case EmptyTree => Nil + case PackageDef(_, stats) => stats.flatMap(collectTypeDefs) + case cd: TypeDef => cd :: Nil + case _: ValDef => Nil // module instance + } + } + val allTypeDefs = collectTypeDefs(cunit.tpdTree) + + val generatedClasses = mutable.ListBuffer.empty[(Symbol, js.ClassDef)] + + // TODO Record anonymous JS function classes + + /* Finally, we emit true code for the remaining class defs. */ + for (td <- allTypeDefs) { + val sym = td.symbol + implicit val pos: Position = sym.pos + + /* Do not actually emit code for primitive types nor scala.Array. */ + val isPrimitive = + sym.isPrimitiveValueClass || sym == defn.ArrayClass + + if (!isPrimitive) { + withScopedVars( + currentClassSym := sym + ) { + val tree = if (isJSType(sym)) { + /*assert(!isRawJSFunctionDef(sym), + s"Raw JS function def should have been recorded: $cd")*/ + if (!sym.is(Trait) && isScalaJSDefinedJSClass(sym)) + genScalaJSDefinedJSClass(td) + else + genRawJSClassData(td) + } else if (sym.is(Trait)) { + genInterface(td) + } else { + genScalaClass(td) + } + + generatedClasses += ((sym, tree)) + } + } + } + + val clDefs = generatedClasses.map(_._2).toList + + for ((sym, tree) <- generatedClasses) { + val writer = new java.io.PrintWriter(System.err) + try { + new ir.Printers.IRTreePrinter(writer).print(tree) + } finally { + writer.flush() + } + genIRFile(cunit, sym, tree) + } + } + + private def genIRFile(cunit: CompilationUnit, sym: Symbol, + tree: ir.Trees.ClassDef): Unit = { + val outfile = getFileFor(cunit, sym, ".sjsir") + val output = outfile.bufferedOutput + try { + ir.InfoSerializers.serialize(output, ir.Infos.generateClassInfo(tree)) + ir.Serializers.serialize(output, tree) + } finally { + output.close() + } + } + + private def getFileFor(cunit: CompilationUnit, sym: Symbol, + suffix: String) = { + import scala.reflect.io._ + + val outputDirectory: AbstractFile = // TODO Support virtual files + new PlainDirectory(new Directory(new java.io.File(ctx.settings.d.value))) + + val pathParts = sym.fullName.toString.split("[./]") + val dir = (outputDirectory /: pathParts.init)(_.subdirectoryNamed(_)) + + var filename = pathParts.last + if (sym.is(ModuleClass)) + filename = filename + nme.MODULE_SUFFIX.toString + + dir fileNamed (filename + suffix) + } + + // Generate a class -------------------------------------------------------- + + /** Gen the IR ClassDef for a Scala class definition (maybe a module class). + */ + private def genScalaClass(td: TypeDef): js.ClassDef = { + val sym = td.symbol.asClass + implicit val pos: Position = sym.pos + + assert(!sym.is(Trait), + "genScalaClass() must be called only for normal classes: "+sym) + assert(sym.superClass != NoSymbol, sym) + + /*if (hasDefaultCtorArgsAndRawJSModule(sym)) { + reporter.error(pos, + "Implementation restriction: constructors of " + + "Scala classes cannot have default parameters " + + "if their companion module is JS native.") + }*/ + + val classIdent = encodeClassFullNameIdent(sym) + val isHijacked = false //isHijackedBoxedClass(sym) + + // Optimizer hints + + def isStdLibClassWithAdHocInlineAnnot(sym: Symbol): Boolean = { + val fullName = sym.fullName.toString + (fullName.startsWith("scala.Tuple") && !fullName.endsWith("$")) || + (fullName.startsWith("scala.collection.mutable.ArrayOps$of")) + } + + val shouldMarkInline = ( + sym.hasAnnotation(jsdefn.InlineAnnot) || + (sym.isAnonymousFunction && !sym.isSubClass(defn.PartialFunctionClass)) || + isStdLibClassWithAdHocInlineAnnot(sym)) + + val optimizerHints = { + OptimizerHints.empty + .withInline(shouldMarkInline) + .withNoinline(sym.hasAnnotation(jsdefn.NoinlineAnnot)) + } + + // Generate members (constructor + methods) + + val generatedMethods = new mutable.ListBuffer[js.MethodDef] + val exportedSymbols = new mutable.ListBuffer[Symbol] + + val tpl = td.rhs.asInstanceOf[Template] + for (tree <- tpl.constr :: tpl.body) { + tree match { + case EmptyTree => () + + case _: ValDef => + () // fields are added via genClassFields() + + case dd: DefDef => + val sym = dd.symbol + + val isExport = false //jsInterop.isExport(sym) + val isNamedExport = false /*isExport && sym.annotations.exists( + _.symbol == JSExportNamedAnnotation)*/ + + /*if (isNamedExport) + generatedMethods += genNamedExporterDef(dd) + else*/ + generatedMethods ++= genMethod(dd) + + if (isExport) { + // We add symbols that we have to export here. This way we also + // get inherited stuff that is implemented in this class. + exportedSymbols += sym + } + + case _ => + throw new FatalError("Illegal tree in body of genScalaClass(): " + tree) + } + } + + // Generate fields and add to methods + ctors + val generatedMembers = genClassFields(td) ++ generatedMethods.toList + + // Generate the exported members, constructors and accessors + val exports = { + // Hack to export hello.world + if (sym.fullName.toString == "hello.world$") { + List( + js.ModuleExportDef("hello.world"), + js.MethodDef(static = false, js.StringLiteral("main"), + Nil, jstpe.AnyType, + js.Block(List( + js.Apply(js.This()(jstpe.ClassType(classIdent.name)), js.Ident("main__V"), Nil)(jstpe.NoType), + js.Undefined())))( + OptimizerHints.empty, None)) + } else { + /* + // Generate the exported members + val memberExports = genMemberExports(sym, exportedSymbols.toList) + + // Generate exported constructors or accessors + val exportedConstructorsOrAccessors = + if (isStaticModule(sym)) genModuleAccessorExports(sym) + else genConstructorExports(sym) + + memberExports ++ exportedConstructorsOrAccessors + */ + Nil + } + } + + // Hashed definitions of the class + val hashedDefs = + ir.Hashers.hashDefs(generatedMembers ++ exports) + + // The complete class definition + val kind = + if (isStaticModule(sym)) ClassKind.ModuleClass + else if (isHijacked) ClassKind.HijackedClass + else ClassKind.Class + + val classDefinition = js.ClassDef( + classIdent, + kind, + Some(encodeClassFullNameIdent(sym.superClass)), + genClassInterfaces(sym), + None, + hashedDefs)( + optimizerHints) + + classDefinition + } + + /** Gen the IR ClassDef for a Scala.js-defined JS class. */ + private def genScalaJSDefinedJSClass(td: TypeDef): js.ClassDef = { + ??? + } + + /** Gen the IR ClassDef for a raw JS class or trait. + */ + private def genRawJSClassData(td: TypeDef): js.ClassDef = { + val sym = td.symbol.asClass + implicit val pos: Position = sym.pos + + val classIdent = encodeClassFullNameIdent(sym) + val superClass = + if (sym.is(Trait)) None + else Some(encodeClassFullNameIdent(sym.superClass)) + val jsName = + if (sym.is(Trait) || sym.is(ModuleClass)) None + else Some(fullJSNameOf(sym)) + + js.ClassDef(classIdent, ClassKind.RawJSType, + superClass, + genClassInterfaces(sym), + jsName, + Nil)( + OptimizerHints.empty) + } + + /** Gen the IR ClassDef for an interface definition. + */ + private def genInterface(td: TypeDef): js.ClassDef = { + val sym = td.symbol.asClass + implicit val pos: Position = sym.pos + + val classIdent = encodeClassFullNameIdent(sym) + + val generatedMethods = new mutable.ListBuffer[js.MethodDef] + + val tpl = td.rhs.asInstanceOf[Template] + for (tree <- tpl.constr :: tpl.body) { + tree match { + case EmptyTree => () + case dd: DefDef => generatedMethods ++= genMethod(dd) + case _ => + throw new FatalError("Illegal tree in gen of genInterface(): " + tree) + } + } + + val superInterfaces = genClassInterfaces(sym) + + // Hashed definitions of the interface + val hashedDefs = + ir.Hashers.hashDefs(generatedMethods.toList) + + js.ClassDef(classIdent, ClassKind.Interface, None, superInterfaces, None, + hashedDefs)(OptimizerHints.empty) + } + + private def genClassInterfaces(sym: ClassSymbol)( + implicit pos: Position): List[js.Ident] = { + import dotty.tools.dotc.transform.SymUtils._ + for { + intf <- sym.directlyInheritedTraits + } yield { + encodeClassFullNameIdent(intf) + } + } + + // Generate the fields of a class ------------------------------------------ + + /** Gen definitions for the fields of a class. + */ + private def genClassFields(td: TypeDef): List[js.FieldDef] = { + val classSym = td.symbol.asClass + assert(currentClassSym.get == classSym, + "genClassFields called with a ClassDef other than the current one") + + // Non-method term members are fields + (for { + f <- classSym.info.decls + if !f.is(Method) && f.isTerm + } yield { + implicit val pos: Position = f.pos + + val name = + /*if (isExposed(f)) js.StringLiteral(jsNameOf(f)) + else*/ encodeFieldSym(f) + + val irTpe = //if (!isScalaJSDefinedJSClass(classSym)) { + toIRType(f.info) + /*} else { + val tpeEnteringPosterasure = + enteringPhase(currentRun.posterasurePhase)(f.tpe) + tpeEnteringPosterasure match { + case tpe: ErasedValueType => + /* Here, we must store the field as the boxed representation of + * the value class. The default value of that field, as + * initialized at the time the instance is created, will + * therefore be null. This will not match the behavior we would + * get in a Scala class. To match the behavior, we would need to + * initialized to an instance of the boxed representation, with + * an underlying value set to the zero of its type. However we + * cannot implement that, so we live with the discrepancy. + * Anyway, scalac also has problems with uninitialized value + * class values, if they come from a generic context. + * + * TODO Evaluate how much of this needs to be adapted for dotc, + * which unboxes `null` to the zero of their underlying. + */ + jstpe.ClassType(encodeClassFullName(tpe.valueClazz)) + + case _ if f.tpe.typeSymbol == CharClass => + /* Will be initialized to null, which will unbox to '\0' when + * read. + */ + jstpe.ClassType(ir.Definitions.BoxedCharacterClass) + + case _ => + /* Other types are not boxed, so we can initialized them to + * their true zero. + */ + toIRType(f.tpe) + } + }*/ + + js.FieldDef(name, irTpe, f.is(Mutable)) + }).toList + } + + // Generate a method ------------------------------------------------------- + + private def genMethod(dd: DefDef): Option[js.MethodDef] = { + withScopedVars( + localNames := new LocalNameGenerator + ) { + genMethodWithCurrentLocalNameScope(dd) + } + } + + /** Gen JS code for a method definition in a class or in an impl class. + * On the JS side, method names are mangled to encode the full signature + * of the Scala method, as described in `JSEncoding`, to support + * overloading. + * + * Some methods are not emitted at all: + * - Primitives, since they are never actually called + * - Constructors of hijacked classes + * + * Constructors are emitted by generating their body as a statement. + * + * Other (normal) methods are emitted with `genMethodBody()`. + */ + private def genMethodWithCurrentLocalNameScope(dd: DefDef): Option[js.MethodDef] = { + implicit val pos: Position = dd.pos + val sym = dd.symbol + val vparamss = dd.vparamss + val rhs = dd.rhs + + isModuleInitialized = false + + withScopedVars( + currentMethodSym := sym, + undefinedDefaultParams := mutable.Set.empty, + thisLocalVarIdent := None + ) { + assert(vparamss.isEmpty || vparamss.tail.isEmpty, + "Malformed parameter list: " + vparamss) + val params = if (vparamss.isEmpty) Nil else vparamss.head.map(_.symbol) + + val isJSClassConstructor = + sym.isClassConstructor && isScalaJSDefinedJSClass(currentClassSym) + + val methodName: js.PropertyName = encodeMethodSym(sym) + + def jsParams = for (param <- params) yield { + implicit val pos: Position = param.pos + js.ParamDef(encodeLocalSym(param), toIRType(param.info), + mutable = false, rest = false) + } + + /*if (primitives.isPrimitive(sym)) { + None + } else*/ if (sym.is(Deferred)) { + Some(js.MethodDef(static = false, methodName, + jsParams, toIRType(patchedResultType(sym)), js.EmptyTree)( + OptimizerHints.empty, None)) + } else /*if (isJSNativeCtorDefaultParam(sym)) { + None + } else if (sym.isClassConstructor && isHijackedBoxedClass(sym.owner)) { + None + } else*/ { + /*def isTraitImplForwarder = dd.rhs match { + case app: Apply => foreignIsImplClass(app.symbol.owner) + case _ => false + }*/ + + val shouldMarkInline = { + sym.hasAnnotation(jsdefn.InlineAnnot) || + sym.isAnonymousFunction + } + + val shouldMarkNoinline = { + sym.hasAnnotation(jsdefn.NoinlineAnnot) /*&& + !isTraitImplForwarder*/ + } + + val optimizerHints = { + OptimizerHints.empty + .withInline(shouldMarkInline) + .withNoinline(shouldMarkNoinline) + } + + val methodDef = { + /*if (isJSClassConstructor) { + val body0 = genStat(rhs) + val body1 = + if (!sym.isPrimaryConstructor) body0 + else moveAllStatementsAfterSuperConstructorCall(body0) + js.MethodDef(static = false, methodName, + jsParams, jstpe.NoType, body1)(optimizerHints, None) + } else*/ if (sym.isConstructor) { + js.MethodDef(static = false, methodName, + jsParams, jstpe.NoType, + genStat(rhs))(optimizerHints, None) + } else { + val resultIRType = toIRType(patchedResultType(sym)) + genMethodDef(static = false, methodName, + params, resultIRType, rhs, optimizerHints) + } + } + + Some(methodDef) + } + } + } + + /** Generates the MethodDef of a (non-constructor) method + * + * Most normal methods are emitted straightforwardly. If the result + * type is Unit, then the body is emitted as a statement. Otherwise, it is + * emitted as an expression. + * + * Methods Scala.js-defined JS classes are compiled as static methods taking + * an explicit parameter for their `this` value. + */ + private def genMethodDef(static: Boolean, methodName: js.PropertyName, + paramsSyms: List[Symbol], resultIRType: jstpe.Type, + tree: Tree, optimizerHints: OptimizerHints): js.MethodDef = { + implicit val pos: Position = tree.pos + + ctx.debuglog("genMethod " + methodName.name) + ctx.debuglog("") + + val jsParams = for (param <- paramsSyms) yield { + implicit val pos: Position = param.pos + js.ParamDef(encodeLocalSym(param), toIRType(param.info), + mutable = false, rest = false) + } + + def genBody() = + if (resultIRType == jstpe.NoType) genStat(tree) + else genExpr(tree) + + //if (!isScalaJSDefinedJSClass(currentClassSym)) { + js.MethodDef(static, methodName, jsParams, resultIRType, genBody())( + optimizerHints, None) + /*} else { + assert(!static, tree.pos) + + withScopedVars( + thisLocalVarIdent := Some(freshLocalIdent("this")) + ) { + val thisParamDef = js.ParamDef(thisLocalVarIdent.get.get, + jstpe.AnyType, mutable = false, rest = false) + + js.MethodDef(static = true, methodName, thisParamDef :: jsParams, + resultIRType, genBody())( + optimizerHints, None) + } + }*/ + } + + // Generate statements and expressions ------------------------------------- + + /** Gen JS code for a tree in statement position (in the IR). + */ + private def genStat(tree: Tree): js.Tree = { + exprToStat(genStatOrExpr(tree, isStat = true)) + } + + /** Turn a JavaScript expression of type Unit into a statement */ + private def exprToStat(tree: js.Tree): js.Tree = { + /* Any JavaScript expression is also a statement, but at least we get rid + * of some pure expressions that come from our own codegen. + */ + implicit val pos: Position = tree.pos + tree match { + case js.Block(stats :+ expr) => js.Block(stats :+ exprToStat(expr)) + case _:js.Literal | js.This() => js.Skip() + case _ => tree + } + } + + /** Gen JS code for a tree in expression position (in the IR). + */ + private def genExpr(tree: Tree): js.Tree = { + val result = genStatOrExpr(tree, isStat = false) + assert(result.tpe != jstpe.NoType, + s"genExpr($tree) returned a tree with type NoType at pos ${tree.pos}") + result + } + + /** Gen JS code for a tree in statement or expression position (in the IR). + * + * This is the main transformation method. Each node of the Scala AST + * is transformed into an equivalent portion of the JS AST. + */ + private def genStatOrExpr(tree: Tree, isStat: Boolean): js.Tree = { + implicit val pos: Position = tree.pos + + ctx.debuglog(" " + tree) + ctx.debuglog("") + + tree match { + /** LabelDefs (for while and do..while loops) */ + /*case lblDf: LabelDef => + genLabelDef(lblDf)*/ + + /** Local val or var declaration */ + case tree @ ValDef(name, _, _) => + /* Must have been eliminated by the tail call transform performed + * by genMethodBody(). */ + assert(name != nme.THIS, + s"ValDef(_, nme.THIS, _, _) found at ${tree.pos}") + + val sym = tree.symbol + val rhs = tree.rhs + val rhsTree = genExpr(rhs) + + rhsTree match { + case js.UndefinedParam() => + /* This is an intermediate assignment for default params on a + * js.Any. Add the symbol to the corresponding set to inform + * the Ident resolver how to replace it and don't emit the symbol. + */ + undefinedDefaultParams += sym + js.Skip() + case _ => + js.VarDef(encodeLocalSym(sym), + toIRType(sym.info), sym.is(Mutable), rhsTree) + } + + case If(cond, thenp, elsep) => + js.If(genExpr(cond), genStatOrExpr(thenp, isStat), + genStatOrExpr(elsep, isStat))(toIRType(tree.tpe)) + + case Return(expr, from) => + // TODO Need to consider `from`? + js.Return(toIRType(expr.tpe) match { + case jstpe.NoType => js.Block(genStat(expr), js.Undefined()) + case _ => genExpr(expr) + }) + + /*case t: Try => + genTry(t, isStat)*/ + + case app: Apply => + genApply(app, isStat) + + case app: TypeApply => + genTypeApply(app) + + /*case app: ApplyDynamic => + genApplyDynamic(app)*/ + + case tree: This => + if (tree.symbol == currentClassSym.get) { + genThis() + } else { + assert(tree.symbol.is(Module), + "Trying to access the this of another class: " + + "tree.symbol = " + tree.symbol + + ", class symbol = " + currentClassSym.get + + " pos:" + pos) + genLoadModule(tree.symbol) + } + + case Select(qualifier, _) => + val sym = tree.symbol + if (sym.is(Module)) { + assert(!sym.is(Package), "Cannot use package as value: " + tree) + genLoadModule(sym) + } else if (sym.is(JavaStatic)) { + genLoadStaticField(sym) + } else /*if (paramAccessorLocals contains sym) { + paramAccessorLocals(sym).ref + } else if (isScalaJSDefinedJSClass(sym.owner)) { + val genQual = genExpr(qualifier) + val boxed = if (isExposed(sym)) + js.JSBracketSelect(genQual, js.StringLiteral(jsNameOf(sym))) + else + js.JSDotSelect(genQual, encodeFieldSym(sym)) + fromAny(boxed, + enteringPhase(currentRun.posterasurePhase)(sym.tpe)) + } else*/ { + js.Select(genExpr(qualifier), + encodeFieldSym(sym))(toIRType(sym.info)) + } + + case tree: Ident => + desugarIdent(tree).fold[js.Tree] { + val sym = tree.symbol + assert(!sym.is(Package), "Cannot use package as value: " + tree) + if (sym.is(Module)) { + genLoadModule(sym) + } else if (undefinedDefaultParams.contains(sym)) { + /* This is a default parameter whose assignment was moved to + * a local variable. Put an undefined param instead. + */ + js.UndefinedParam()(toIRType(sym.info)) + } else { + js.VarRef(encodeLocalSym(sym))(toIRType(sym.info)) + } + } { select => + genStatOrExpr(select, isStat) + } + + case Literal(value) => + import Constants._ + value.tag match { + case UnitTag => + js.Skip() + case BooleanTag => + js.BooleanLiteral(value.booleanValue) + case ByteTag | ShortTag | CharTag | IntTag => + js.IntLiteral(value.intValue) + case LongTag => + js.LongLiteral(value.longValue) + case FloatTag => + js.FloatLiteral(value.floatValue) + case DoubleTag => + js.DoubleLiteral(value.doubleValue) + case StringTag => + js.StringLiteral(value.stringValue) + case NullTag => + js.Null() + case ClazzTag => + genClassConstant(value.typeValue) + /*case EnumTag => + genStaticMember(value.symbolValue)*/ + } + + case Block(stats, expr) => + js.Block(stats.map(genStat) :+ genStatOrExpr(expr, isStat)) + + case Typed(expr, _) => + expr match { + case _: Super => genThis() + case _ => genExpr(expr) + } + + case Assign(lhs0, rhs) => + val sym = lhs0.symbol + if (sym.is(JavaStaticTerm)) + throw new FatalError(s"Assignment to static member ${sym.fullName} not supported") + val genRhs = genExpr(rhs) + val lhs = lhs0 match { + case lhs: Ident => desugarIdent(lhs).getOrElse(lhs) + case lhs => lhs + } + lhs match { + case lhs: Select => + val qualifier = lhs.qualifier + + def ctorAssignment = ( + currentMethodSym.get.name == nme.CONSTRUCTOR && + currentMethodSym.get.owner == qualifier.symbol && + qualifier.isInstanceOf[This] + ) + if (!sym.is(Mutable) && !ctorAssignment) + throw new FatalError(s"Assigning to immutable field ${sym.fullName} at $pos") + + val genQual = genExpr(qualifier) + + /*if (isScalaJSDefinedJSClass(sym.owner)) { + val genLhs = if (isExposed(sym)) + js.JSBracketSelect(genQual, js.StringLiteral(jsNameOf(sym))) + else + js.JSDotSelect(genQual, encodeFieldSym(sym)) + val boxedRhs = + ensureBoxed(genRhs, + enteringPhase(currentRun.posterasurePhase)(rhs.tpe)) + js.Assign(genLhs, boxedRhs) + } else {*/ + js.Assign( + js.Select(genQual, encodeFieldSym(sym))(toIRType(sym.info)), + genRhs) + //} + case _ => + js.Assign( + js.VarRef(encodeLocalSym(sym))(toIRType(sym.info)), + genRhs) + } + + /** Array constructor */ + case javaSeqLiteral: JavaSeqLiteral => + genJavaSeqLiteral(javaSeqLiteral) + + /** A Match reaching the backend is supposed to be optimized as a switch */ + /*case mtch: Match => + genMatch(mtch, isStat)*/ + + case tree: Closure => + genClosure(tree) + + /*case EmptyTree => + js.Skip()*/ + + case _ => + throw new FatalError("Unexpected tree in genExpr: " + + tree + "/" + tree.getClass + " at: " + (tree.pos: Position)) + } + } // end of genStatOrExpr() + + // !!! DUPLICATE code with DottyBackendInterface + private def desugarIdent(i: Ident): Option[Select] = { + i.tpe match { + case TermRef(prefix: TermRef, name) => + Some(tpd.ref(prefix).select(i.symbol)) + case TermRef(prefix: ThisType, name) => + Some(tpd.This(prefix.cls).select(i.symbol)) + /*case TermRef(NoPrefix, name) => + if (i.symbol is Method) Some(This(i.symbol.topLevelClass).select(i.symbol)) // workaround #342 todo: remove after fixed + else None*/ + case _ => + None + } + } + + private def qualifierOf(fun: Tree): Tree = fun match { + case fun: Ident => + fun.tpe match { + case TermRef(prefix: TermRef, _) => tpd.ref(prefix) + case TermRef(prefix: ThisType, _) => tpd.This(prefix.cls) + } + case Select(qualifier, _) => + qualifier + case TypeApply(fun, _) => + qualifierOf(fun) + } + + /** Gen JS this of the current class. + * Normally encoded straightforwardly as a JS this. + * But must be replaced by the `thisLocalVarIdent` local variable if there + * is one. + */ + private def genThis()(implicit pos: Position): js.Tree = { + /*if (tryingToGenMethodAsJSFunction) { + throw new CancelGenMethodAsJSFunction( + "Trying to generate `this` inside the body") + }*/ + + thisLocalVarIdent.fold[js.Tree] { + js.This()(currentClassType) + } { thisLocalIdent => + js.VarRef(thisLocalIdent)(currentClassType) + } + } + + /** Gen JS code for an Apply node (method call) + * + * There's a whole bunch of varieties of Apply nodes: regular method + * calls, super calls, constructor calls, isInstanceOf/asInstanceOf, + * primitives, JS calls, etc. They are further dispatched in here. + */ + private def genApply(tree: Apply, isStat: Boolean): js.Tree = { + implicit val pos: Position = tree.pos + val args = tree.args + val sym = tree.fun.symbol + + val fun = tree.fun match { + case fun: Ident => desugarIdent(fun).getOrElse(fun) + case fun => fun + } + + fun match { + case _ if isJSDefaultParam(sym) => + js.UndefinedParam()(toIRType(sym.info.finalResultType)) + + case Select(Super(_, _), _) => + genSuperCall(tree, isStat) + + case Select(New(_), nme.CONSTRUCTOR) => + genApplyNew(tree) + + case _ => + /*if (sym.isLabel) { + genLabelApply(tree) + } else*/ if (primitives.isPrimitive(tree)) { + genPrimitiveOp(tree, isStat) + } else if (Erasure.Boxing.isBox(sym)) { + // Box a primitive value (cannot be Unit) + val arg = args.head + makePrimitiveBox(genExpr(arg), arg.tpe) + } else if (Erasure.Boxing.isUnbox(sym)) { + // Unbox a primitive value (cannot be Unit) + val arg = args.head + makePrimitiveUnbox(genExpr(arg), tree.tpe) + } else { + genNormalApply(tree, isStat) + } + } + } + + /** Gen JS code for a super call, of the form Class.super[mix].fun(args). + * + * This does not include calls defined in mixin traits, as these are + * already desugared by the 'mixin' phase. Only calls to super classes + * remain. + * + * Since a class has exactly one direct superclass, and calling a method + * two classes above the current one is invalid in Scala, the `mix` item is + * irrelevant. + */ + private def genSuperCall(tree: Apply, isStat: Boolean): js.Tree = { + implicit val pos: Position = tree.pos + val Apply(fun @ Select(sup @ Super(_, mix), _), args) = tree + val sym = fun.symbol + + if (sym == defn.Any_getClass) { + // The only primitive that is also callable as super call + js.GetClass(genThis()) + } else /*if (isScalaJSDefinedJSClass(currentClassSym)) { + genJSSuperCall(tree, isStat) + } else*/ { + val superCall = genApplyMethodStatically( + genThis()(sup.pos), sym, genActualArgs(sym, args)) + + // Initialize the module instance just after the super constructor call. + if (isStaticModule(currentClassSym) && !isModuleInitialized && + currentMethodSym.get.isClassConstructor) { + isModuleInitialized = true + val thisType = jstpe.ClassType(encodeClassFullName(currentClassSym)) + val initModule = js.StoreModule(thisType, js.This()(thisType)) + js.Block(superCall, initModule) + } else { + superCall + } + } + } + + /** Gen JS code for a constructor call (new). + * Further refined into: + * * new String(...) + * * new of a hijacked boxed class + * * new of an anonymous function class that was recorded as JS function + * * new of a raw JS class + * * new Array + * * regular new + */ + private def genApplyNew(tree: Apply): js.Tree = { + implicit val pos: Position = tree.pos + + val Apply(fun @ Select(New(tpt), nme.CONSTRUCTOR), args) = tree + val ctor = fun.symbol + val tpe = tpt.tpe + + assert(ctor.isClassConstructor, + "'new' call to non-constructor: " + ctor.name) + + if (tpe.isRef(defn.StringClass)) { + genNewString(ctor, genActualArgs(ctor, args)) + } else /*if (isHijackedBoxedClass(tpe.typeSymbol)) { + genNewHijackedBoxedClass(tpe.typeSymbol, ctor, args map genExpr) + } else if (translatedAnonFunctions contains tpe.typeSymbol) { + val functionMaker = translatedAnonFunctions(tpe.typeSymbol) + functionMaker(args map genExpr) + } else*/ if (isJSType(tpe.widenDealias.typeSymbol)) { + val clsSym = tpe.widenDealias.typeSymbol + if (clsSym == jsdefn.JSObjectClass && args.isEmpty) js.JSObjectConstr(Nil) + else if (clsSym == jsdefn.JSArrayClass && args.isEmpty) js.JSArrayConstr(Nil) + else js.JSNew(genLoadJSConstructor(clsSym), genActualJSArgs(ctor, args)) + } else { + toIRType(tpe) match { + case cls: jstpe.ClassType => + js.New(cls, encodeMethodSym(ctor), genActualArgs(ctor, args)) + + case other => + throw new FatalError(s"Non ClassType cannot be instantiated: $other") + } + } + } + + /** Gen JS code for a primitive method call. */ + private def genPrimitiveOp(tree: Apply, isStat: Boolean): js.Tree = { + import scala.tools.nsc.backend.ScalaPrimitives._ + + implicit val pos: Position = tree.pos + + val Apply(fun, args) = tree + val receiver = qualifierOf(fun) + + val code = primitives.getPrimitive(tree, receiver.tpe) + + if (isArithmeticOp(code) || isLogicalOp(code) || isComparisonOp(code)) + genSimpleOp(tree, receiver :: args, code) + else if (code == CONCAT) + genStringConcat(tree, receiver, args) + else if (code == HASH) + genScalaHash(tree, receiver) + else if (isArrayOp(code)) + genArrayOp(tree, code) + else if (code == SYNCHRONIZED) + genSynchronized(tree, isStat) + else if (isCoercion(code)) + genCoercion(tree, receiver, code) + else if (code == JSPrimitives.THROW) + genThrow(tree, args) + else /*if (primitives.isJSPrimitive(code)) + genJSPrimitive(tree, receiver, args, code) + else*/ + throw new FatalError(s"Unknown primitive: ${tree.symbol.fullName} at: $pos") + } + + /** Gen JS code for a simple operation (arithmetic, logical, or comparison) */ + private def genSimpleOp(tree: Apply, args: List[Tree], code: Int): js.Tree = { + args match { + case List(arg) => genSimpleUnaryOp(tree, arg, code) + case List(lhs, rhs) => genSimpleBinaryOp(tree, lhs, rhs, code) + case _ => throw new FatalError("Incorrect arity for primitive") + } + } + + /** Gen JS code for a simple unary operation. */ + private def genSimpleUnaryOp(tree: Apply, arg: Tree, code: Int): js.Tree = { + import scala.tools.nsc.backend.ScalaPrimitives._ + + implicit val pos: Position = tree.pos + + val genArg = genExpr(arg) + val resultIRType = toIRType(tree.tpe) + + (code: @switch) match { + case POS => + genArg + + case NEG => + (resultIRType: @unchecked) match { + case jstpe.IntType => + js.BinaryOp(js.BinaryOp.Int_-, js.IntLiteral(0), genArg) + case jstpe.LongType => + js.BinaryOp(js.BinaryOp.Long_-, js.LongLiteral(0), genArg) + case jstpe.FloatType => + js.BinaryOp(js.BinaryOp.Float_-, js.FloatLiteral(0.0f), genArg) + case jstpe.DoubleType => + js.BinaryOp(js.BinaryOp.Double_-, js.DoubleLiteral(0), genArg) + } + + case NOT => + (resultIRType: @unchecked) match { + case jstpe.IntType => + js.BinaryOp(js.BinaryOp.Int_^, js.IntLiteral(-1), genArg) + case jstpe.LongType => + js.BinaryOp(js.BinaryOp.Long_^, js.LongLiteral(-1), genArg) + } + + case ZNOT => + js.UnaryOp(js.UnaryOp.Boolean_!, genArg) + + case _ => + throw new FatalError("Unknown unary operation code: " + code) + } + } + + /** Gen JS code for a simple binary operation. */ + private def genSimpleBinaryOp(tree: Apply, lhs: Tree, rhs: Tree, code: Int): js.Tree = { + import scala.tools.nsc.backend.ScalaPrimitives._ + import js.UnaryOp._ + + /* Codes for operation types, in an object so that they can be 'final val' + * and be used in switch-matches. + */ + object OpTypes { + final val DoubleOp = 1 + final val FloatOp = 2 + final val LongOp = 3 + final val IntOp = 4 + final val BooleanOp = 5 + final val AnyOp = 6 + } + import OpTypes._ + + implicit val pos: Position = tree.pos + + val lhsIRType = toIRType(lhs.tpe) + val rhsIRType = toIRType(rhs.tpe) + + val opType = (lhsIRType, rhsIRType) match { + case (jstpe.DoubleType, _) | (_, jstpe.DoubleType) => DoubleOp + case (jstpe.FloatType, _) | (_, jstpe.FloatType) => FloatOp + case (jstpe.LongType, _) | (_, jstpe.LongType) => LongOp + case (jstpe.IntType, _) | (_, jstpe.IntType) => IntOp + case (jstpe.BooleanType, jstpe.BooleanType) => BooleanOp + case _ => AnyOp + } + + if (opType == AnyOp && isUniversalEqualityOp(code)) { + genUniversalEqualityOp(lhs, rhs, code) + } else if (code == ZOR) { + js.If(genExpr(lhs), js.BooleanLiteral(true), genExpr(rhs))(jstpe.BooleanType) + } else if (code == ZAND) { + js.If(genExpr(lhs), genExpr(rhs), js.BooleanLiteral(false))(jstpe.BooleanType) + } else { + import js.BinaryOp._ + + def coerce(tree: js.Tree, opType: Int): js.Tree = (opType: @switch) match { + case DoubleOp => + if (tree.tpe == jstpe.LongType) js.UnaryOp(LongToDouble, tree) + else tree + + case FloatOp => + if (tree.tpe == jstpe.FloatType || tree.tpe == jstpe.IntType) tree + else js.UnaryOp(DoubleToFloat, coerce(tree, DoubleOp)) + + case LongOp => + if (tree.tpe == jstpe.LongType) tree + else { + assert(tree.tpe == jstpe.IntType) + js.UnaryOp(IntToLong, tree) + } + + case IntOp => + if (tree.tpe == jstpe.IntType) tree + else { + assert(tree.tpe == jstpe.LongType) + js.UnaryOp(LongToInt, tree) + } + + case BooleanOp | AnyOp => + tree + } + + val rhsOpType = code match { + case LSL | LSR | ASR => IntOp + case _ => opType + } + + val genLhs = coerce(genExpr(lhs), opType) + val genRhs = coerce(genExpr(rhs), rhsOpType) + + val op = (opType: @switch) match { + case IntOp => + (code: @switch) match { + case ADD => Int_+ + case SUB => Int_- + case MUL => Int_* + case DIV => Int_/ + case MOD => Int_% + case OR => Int_| + case AND => Int_& + case XOR => Int_^ + case LSL => Int_<< + case LSR => Int_>>> + case ASR => Int_>> + + case EQ => Num_== + case NE => Num_!= + case LT => Num_< + case LE => Num_<= + case GT => Num_> + case GE => Num_>= + } + + case FloatOp => + (code: @switch) match { + case ADD => Float_+ + case SUB => Float_- + case MUL => Float_* + case DIV => Float_/ + case MOD => Float_% + + case EQ => Num_== + case NE => Num_!= + case LT => Num_< + case LE => Num_<= + case GT => Num_> + case GE => Num_>= + } + + case DoubleOp => + (code: @switch) match { + case ADD => Double_+ + case SUB => Double_- + case MUL => Double_* + case DIV => Double_/ + case MOD => Double_% + + case EQ => Num_== + case NE => Num_!= + case LT => Num_< + case LE => Num_<= + case GT => Num_> + case GE => Num_>= + } + + case LongOp => + (code: @switch) match { + case ADD => Long_+ + case SUB => Long_- + case MUL => Long_* + case DIV => Long_/ + case MOD => Long_% + case OR => Long_| + case XOR => Long_^ + case AND => Long_& + case LSL => Long_<< + case LSR => Long_>>> + case ASR => Long_>> + + case EQ => Long_== + case NE => Long_!= + case LT => Long_< + case LE => Long_<= + case GT => Long_> + case GE => Long_>= + } + + case BooleanOp => + (code: @switch) match { + case EQ => Boolean_== + case NE => Boolean_!= + case OR => Boolean_| + case AND => Boolean_& + case XOR => Boolean_!= + } + + case AnyOp => + /* No @switch because some 2.11 version erroneously report a warning + * for switches with less than 3 non-default cases. + */ + code match { + case ID => === + case NI => !== + } + } + + js.BinaryOp(op, genLhs, genRhs) + } + } + + /** Gen JS code for a universal equality test. */ + private def genUniversalEqualityOp(lhs: Tree, rhs: Tree, code: Int)( + implicit pos: Position): js.Tree = { + + import scala.tools.nsc.backend.ScalaPrimitives._ + + val genLhs = genExpr(lhs) + val genRhs = genExpr(rhs) + + val bypassEqEq = { + // Do not call equals if we have a literal null at either side. + genLhs.isInstanceOf[js.Null] || + genRhs.isInstanceOf[js.Null] + } + + if (bypassEqEq) { + js.BinaryOp( + if (code == EQ) js.BinaryOp.=== else js.BinaryOp.!==, + genLhs, genRhs) + } else { + val body = genEqEqPrimitive(lhs.tpe, rhs.tpe, genLhs, genRhs) + if (code == EQ) body + else js.UnaryOp(js.UnaryOp.Boolean_!, body) + } + } + + private lazy val externalEqualsNumNum: Symbol = + defn.BoxesRunTimeModule.requiredMethod(nme.equalsNumNum) + private lazy val externalEqualsNumChar: Symbol = + NoSymbol // ctx.requiredMethod(BoxesRunTimeTypeRef, nme.equalsNumChar) // this method is private + private lazy val externalEqualsNumObject: Symbol = + defn.BoxesRunTimeModule.requiredMethod(nme.equalsNumObject) + private lazy val externalEquals: Symbol = + defn.BoxesRunTimeClass.info.decl(nme.equals_).suchThat(toDenot(_).info.firstParamTypes.size == 2).symbol + + /** Gen JS code for a call to Any.== */ + private def genEqEqPrimitive(ltpe: Type, rtpe: Type, lsrc: js.Tree, rsrc: js.Tree)( + implicit pos: Position): js.Tree = { + ctx.debuglog(s"$ltpe == $rtpe") + val lsym = ltpe.widenDealias.typeSymbol.asClass + val rsym = rtpe.widenDealias.typeSymbol.asClass + + /* True if the equality comparison is between values that require the + * use of the rich equality comparator + * (scala.runtime.BoxesRunTime.equals). + * This is the case when either side of the comparison might have a + * run-time type subtype of java.lang.Number or java.lang.Character, + * **which includes when either is a JS type**. + * When it is statically known that both sides are equal and subtypes of + * Number or Character, not using the rich equality is possible (their + * own equals method will do ok.) + */ + val mustUseAnyComparator: Boolean = { + isJSType(lsym) || isJSType(rsym) || { + val p = ctx.platform + val areSameFinals = lsym.is(Final) && rsym.is(Final) && (ltpe =:= rtpe) + !areSameFinals && p.isMaybeBoxed(lsym) && p.isMaybeBoxed(rsym) + } + } + + if (mustUseAnyComparator) { + val equalsMethod: Symbol = { + // scalastyle:off line.size.limit + val ptfm = ctx.platform + if (lsym.derivesFrom(defn.BoxedNumberClass)) { + if (rsym.derivesFrom(defn.BoxedNumberClass)) externalEqualsNumNum + else if (rsym.derivesFrom(defn.BoxedCharClass)) externalEqualsNumObject // will be externalEqualsNumChar in 2.12, SI-9030 + else externalEqualsNumObject + } else externalEquals + // scalastyle:on line.size.limit + } + genModuleApplyMethod(equalsMethod, List(lsrc, rsrc)) + } else { + // if (lsrc eq null) rsrc eq null else lsrc.equals(rsrc) + if (lsym == defn.StringClass) { + // String.equals(that) === (this eq that) + js.BinaryOp(js.BinaryOp.===, lsrc, rsrc) + } else { + /* This requires to evaluate both operands in local values first. + * The optimizer will eliminate them if possible. + */ + val ltemp = js.VarDef(freshLocalIdent(), lsrc.tpe, mutable = false, lsrc) + val rtemp = js.VarDef(freshLocalIdent(), rsrc.tpe, mutable = false, rsrc) + js.Block( + ltemp, + rtemp, + js.If(js.BinaryOp(js.BinaryOp.===, ltemp.ref, js.Null()), + js.BinaryOp(js.BinaryOp.===, rtemp.ref, js.Null()), + genApplyMethod(ltemp.ref, defn.Any_equals, List(rtemp.ref)))( + jstpe.BooleanType)) + } + } + } + + /** Gen JS code for string concatenation. + */ + private def genStringConcat(tree: Apply, receiver: Tree, + args: List[Tree]): js.Tree = { + implicit val pos: Position = tree.pos + + val arg = args.head + + /* Primitive number types such as scala.Int have a + * def +(s: String): String + * method, which is why we have to box the lhs sometimes. + * Otherwise, both lhs and rhs are already reference types (Any or String) + * so boxing is not necessary (in particular, rhs is never a primitive). + */ + assert(!isPrimitiveValueType(receiver.tpe) || arg.tpe.isRef(defn.StringClass)) + assert(!isPrimitiveValueType(arg.tpe)) + + val genLhs = { + val genLhs0 = genExpr(receiver) + // Box the receiver if it is a primitive value + if (!isPrimitiveValueType(receiver.tpe)) genLhs0 + else makePrimitiveBox(genLhs0, receiver.tpe) + } + + val genRhs = genExpr(arg) + + js.BinaryOp(js.BinaryOp.String_+, genLhs, genRhs) + } + + /** Gen JS code for a call to Any.## */ + private def genScalaHash(tree: Apply, receiver: Tree): js.Tree = { + implicit val pos: Position = tree.pos + + genModuleApplyMethod(defn.ScalaRuntimeModule.requiredMethod(nme.hash_), + List(genExpr(receiver))) + } + + /** Gen JS code for an array operation (get, set or length) */ + private def genArrayOp(tree: Tree, code: Int): js.Tree = { + import scala.tools.nsc.backend.ScalaPrimitives._ + + implicit val pos: Position = tree.pos + + val Apply(fun, args) = tree + val arrayObj = qualifierOf(fun) + + val genArray = genExpr(arrayObj) + val genArgs = args.map(genExpr) + + def elementType: Type = arrayObj.tpe.widenDealias match { + case defn.ArrayOf(el) => el + case JavaArrayType(el) => el + case tpe => + ctx.error(s"expected Array $tpe") + ErrorType + } + + def genSelect(): js.Tree = + js.ArraySelect(genArray, genArgs(0))(toIRType(elementType)) + + if (isArrayGet(code)) { + // get an item of the array + assert(args.length == 1, + s"Array get requires 1 argument, found ${args.length} in $tree") + genSelect() + } else if (isArraySet(code)) { + // set an item of the array + assert(args.length == 2, + s"Array set requires 2 arguments, found ${args.length} in $tree") + js.Assign(genSelect(), genArgs(1)) + } else { + // length of the array + js.ArrayLength(genArray) + } + } + + /** Gen JS code for a call to AnyRef.synchronized */ + private def genSynchronized(tree: Apply, isStat: Boolean): js.Tree = { + /* JavaScript is single-threaded, so we can drop the + * synchronization altogether. + */ + val Apply(fun, List(arg)) = tree + val receiver = qualifierOf(fun) + + val genReceiver = genExpr(receiver) + val genArg = genStatOrExpr(arg, isStat) + + genReceiver match { + case js.This() => + // common case for which there is no side-effect nor NPE + genArg + case _ => + implicit val pos: Position = tree.pos + /* TODO Check for a null receiver? + * In theory, it's UB, but that decision should be left for link time. + */ + js.Block(genReceiver, genArg) + } + } + + /** Gen JS code for a coercion */ + private def genCoercion(tree: Apply, receiver: Tree, code: Int): js.Tree = { + import scala.tools.nsc.backend.ScalaPrimitives._ + + implicit val pos: Position = tree.pos + + val source = genExpr(receiver) + + def source2int = (code: @switch) match { + case F2C | D2C | F2B | D2B | F2S | D2S | F2I | D2I => + js.UnaryOp(js.UnaryOp.DoubleToInt, source) + case L2C | L2B | L2S | L2I => + js.UnaryOp(js.UnaryOp.LongToInt, source) + case _ => + source + } + + (code: @switch) match { + // To Char, need to crop at unsigned 16-bit + case B2C | S2C | I2C | L2C | F2C | D2C => + js.BinaryOp(js.BinaryOp.Int_&, source2int, js.IntLiteral(0xffff)) + + // To Byte, need to crop at signed 8-bit + case C2B | S2B | I2B | L2B | F2B | D2B => + // note: & 0xff would not work because of negative values + js.BinaryOp(js.BinaryOp.Int_>>, + js.BinaryOp(js.BinaryOp.Int_<<, source2int, js.IntLiteral(24)), + js.IntLiteral(24)) + + // To Short, need to crop at signed 16-bit + case C2S | I2S | L2S | F2S | D2S => + // note: & 0xffff would not work because of negative values + js.BinaryOp(js.BinaryOp.Int_>>, + js.BinaryOp(js.BinaryOp.Int_<<, source2int, js.IntLiteral(16)), + js.IntLiteral(16)) + + // To Int, need to crop at signed 32-bit + case L2I | F2I | D2I => + source2int + + // Any int to Long + case C2L | B2L | S2L | I2L => + js.UnaryOp(js.UnaryOp.IntToLong, source) + + // Any double to Long + case F2L | D2L => + js.UnaryOp(js.UnaryOp.DoubleToLong, source) + + // Long to Double + case L2D => + js.UnaryOp(js.UnaryOp.LongToDouble, source) + + // Any int, or Double, to Float + case C2F | B2F | S2F | I2F | D2F => + js.UnaryOp(js.UnaryOp.DoubleToFloat, source) + + // Long to Float === Long to Double to Float + case L2F => + js.UnaryOp(js.UnaryOp.DoubleToFloat, + js.UnaryOp(js.UnaryOp.LongToDouble, source)) + + // Identities and IR upcasts + case C2C | B2B | S2S | I2I | L2L | F2F | D2D | + C2I | C2D | + B2S | B2I | B2D | + S2I | S2D | + I2D | + F2D => + source + } + } + + /** Gen a call to the special `throw` method. */ + private def genThrow(tree: Apply, args: List[Tree]): js.Tree = { + implicit val pos: Position = tree.pos + val exception = args.head + val genException = genExpr(exception) + js.Throw { + if (exception.tpe.widenDealias.typeSymbol.derivesFrom(jsdefn.JavaScriptExceptionClass)) { + genModuleApplyMethod( + jsdefn.RuntimePackage_unwrapJavaScriptException, + List(genException)) + } else { + genException + } + } + } + + /** Gen a "normal" apply (to a true method). + * + * But even these are further refined into: + * * Methods of java.lang.String, which are redirected to the + * RuntimeString trait implementation. + * * Calls to methods of raw JS types (Scala.js -> JS interop) + * * Calls to methods in impl classes of Scala2 traits. + * * Regular method call + */ + private def genNormalApply(tree: Apply, isStat: Boolean): js.Tree = { + implicit val pos: Position = tree.pos + + val fun = tree.fun match { + case fun: Ident => desugarIdent(fun).get + case fun: Select => fun + } + val receiver = fun.qualifier + val args = tree.args + val sym = fun.symbol + + def isStringMethodFromObject: Boolean = sym.name match { + case nme.toString_ | nme.equals_ | nme.hashCode_ => true + case _ => false + } + + if (sym.owner == defn.StringClass && !isStringMethodFromObject) { + genApplyMethodOfString(genExpr(receiver), sym, genActualArgs(sym, args)) + } else if (isJSType(sym.owner)) { + //if (!isScalaJSDefinedJSClass(sym.owner) || isExposed(sym)) + genApplyJSMethodGeneric(tree, sym, genExpr(receiver), genActualJSArgs(sym, args), isStat) + /*else + genApplyJSClassMethod(genExpr(receiver), sym, genActualArgs(sym, args))*/ + } else if (foreignIsImplClass(sym.owner)) { + genTraitImplApply(sym, args.map(genExpr)) + } else if (sym.isClassConstructor) { + // Calls to constructors are always statically linked + genApplyMethodStatically(genExpr(receiver), sym, genActualArgs(sym, args)) + } else { + genApplyMethod(genExpr(receiver), sym, genActualArgs(sym, args)) + } + } + + /** Gen JS code for a call to a JS method (of a subclass of `js.Any`). + * + * Basically it boils down to calling the method as a `JSBracketSelect`, + * without name mangling. But other aspects come into play: + * + * - Operator methods are translated to JS operators (not method calls) + * - `apply` is translated as a function call, i.e., `o()` instead of `o.apply()` + * - Scala varargs are turned into JS varargs (see `genPrimitiveJSArgs()`) + * - Getters and parameterless methods are translated as `JSBracketSelect` + * - Setters are translated to `Assign` to `JSBracketSelect` + */ + private def genApplyJSMethodGeneric(tree: Tree, sym: Symbol, + receiver: js.Tree, args: List[js.Tree], isStat: Boolean, + superIn: Option[Symbol] = None)( + implicit pos: Position): js.Tree = { + + implicit val pos: Position = tree.pos + + def noSpread = !args.exists(_.isInstanceOf[js.JSSpread]) + val argc = args.size // meaningful only for methods that don't have varargs + + def requireNotSuper(): Unit = { + if (superIn.isDefined) + ctx.error("Illegal super call in Scala.js-defined JS class", tree.pos) + } + + def hasExplicitJSEncoding = { + sym.hasAnnotation(jsdefn.JSNameAnnot) || + sym.hasAnnotation(jsdefn.JSBracketAccessAnnot) || + sym.hasAnnotation(jsdefn.JSBracketCallAnnot) + } + + val boxedResult = sym.name match { + case JSUnaryOpMethodName(code) if argc == 0 => + requireNotSuper() + js.JSUnaryOp(code, receiver) + + case JSBinaryOpMethodName(code) if argc == 1 => + requireNotSuper() + js.JSBinaryOp(code, receiver, args.head) + + case nme.apply if !hasExplicitJSEncoding => + requireNotSuper() + if (jsdefn.isJSThisFunctionClass(sym.owner)) + js.JSBracketMethodApply(receiver, js.StringLiteral("call"), args) + else + js.JSFunctionApply(receiver, args) + + case _ => + def jsFunName = js.StringLiteral(jsNameOf(sym)) + + def genSuperReference(propName: js.Tree): js.Tree = { + superIn.fold[js.Tree] { + js.JSBracketSelect(receiver, propName) + } { superInSym => + js.JSSuperBracketSelect( + jstpe.ClassType(encodeClassFullName(superInSym)), + receiver, propName) + } + } + + def genSelectGet(propName: js.Tree): js.Tree = + genSuperReference(propName) + + def genSelectSet(propName: js.Tree, value: js.Tree): js.Tree = + js.Assign(genSuperReference(propName), value) + + def genCall(methodName: js.Tree, args: List[js.Tree]): js.Tree = { + superIn.fold[js.Tree] { + js.JSBracketMethodApply( + receiver, methodName, args) + } { superInSym => + js.JSSuperBracketCall( + jstpe.ClassType(encodeClassFullName(superInSym)), + receiver, methodName, args) + } + } + + if (isJSGetter(sym)) { + assert(noSpread && argc == 0) + genSelectGet(jsFunName) + } else if (isJSSetter(sym)) { + assert(noSpread && argc == 1) + genSelectSet(jsFunName, args.head) + } else if (isJSBracketAccess(sym)) { + assert(noSpread && (argc == 1 || argc == 2), + s"@JSBracketAccess methods should have 1 or 2 non-varargs arguments") + args match { + case List(keyArg) => + genSelectGet(keyArg) + case List(keyArg, valueArg) => + genSelectSet(keyArg, valueArg) + } + } else if (isJSBracketCall(sym)) { + val (methodName, actualArgs) = extractFirstArg(args) + genCall(methodName, actualArgs) + } else { + genCall(jsFunName, args) + } + } + + if (isStat) { + boxedResult + } else { + val tpe = ctx.atPhase(ctx.elimErasedValueTypePhase) { implicit ctx => + sym.info.finalResultType + } + unbox(boxedResult, tpe) + } + } + + private object JSUnaryOpMethodName { + private val map = Map( + nme.UNARY_+ -> js.JSUnaryOp.+, + nme.UNARY_- -> js.JSUnaryOp.-, + nme.UNARY_~ -> js.JSUnaryOp.~, + nme.UNARY_! -> js.JSUnaryOp.! + ) + + def unapply(name: Names.TermName): Option[js.JSUnaryOp.Code] = + map.get(name) + } + + private object JSBinaryOpMethodName { + private val map = Map( + nme.ADD -> js.JSBinaryOp.+, + nme.SUB -> js.JSBinaryOp.-, + nme.MUL -> js.JSBinaryOp.*, + nme.DIV -> js.JSBinaryOp./, + nme.MOD -> js.JSBinaryOp.%, + + nme.LSL -> js.JSBinaryOp.<<, + nme.ASR -> js.JSBinaryOp.>>, + nme.LSR -> js.JSBinaryOp.>>>, + nme.OR -> js.JSBinaryOp.|, + nme.AND -> js.JSBinaryOp.&, + nme.XOR -> js.JSBinaryOp.^, + + nme.LT -> js.JSBinaryOp.<, + nme.LE -> js.JSBinaryOp.<=, + nme.GT -> js.JSBinaryOp.>, + nme.GE -> js.JSBinaryOp.>=, + + nme.ZAND -> js.JSBinaryOp.&&, + nme.ZOR -> js.JSBinaryOp.|| + ) + + def unapply(name: Names.TermName): Option[js.JSBinaryOp.Code] = + map.get(name) + } + + /** Extract the first argument in a list of actual arguments. + * + * This is nothing else than decomposing into head and tail, except that + * we assert that the first element is not a JSSpread. + */ + private def extractFirstArg(args: List[js.Tree]): (js.Tree, List[js.Tree]) = { + assert(args.nonEmpty, + "Trying to extract the first argument of an empty argument list") + val firstArg = args.head + assert(!firstArg.isInstanceOf[js.JSSpread], + "Trying to extract the first argument of an argument list starting " + + "with a Spread argument: " + firstArg) + (firstArg, args.tail) + } + + /** Gen JS code for a call to a polymorphic method. + * + * The only methods that reach the back-end as polymorphic are + * `isInstanceOf` and `asInstanceOf`. + * + * (Well, in fact `DottyRunTime.newRefArray` too, but it is handled as a + * primitive instead.) + */ + private def genTypeApply(tree: TypeApply): js.Tree = { + implicit val pos: Position = tree.pos + + val TypeApply(fun, targs) = tree + + val sym = fun.symbol + val receiver = qualifierOf(fun) + + val to = targs.head.tpe + + assert(!isPrimitiveValueType(receiver.tpe), + s"Found receiver of type test with primitive type ${receiver.tpe} at $pos") + assert(!isPrimitiveValueType(to), + s"Found target type of type test with primitive type ${receiver.tpe} at $pos") + + val genReceiver = genExpr(receiver) + + if (sym == defn.Any_asInstanceOf) { + genAsInstanceOf(genReceiver, to) + } else if (sym == defn.Any_isInstanceOf) { + genIsInstanceOf(tree, genReceiver, to) + } else { + throw new FatalError( + s"Unexpected type application $fun with symbol ${sym.fullName}") + } + } + + /** Gen JS code for a Java Seq literal. */ + private def genJavaSeqLiteral(tree: JavaSeqLiteral): js.Tree = { + implicit val pos: Position = tree.pos + + val genElems = tree.elems.map(genExpr) + val arrayType = toReferenceType(tree.tpe).asInstanceOf[jstpe.ArrayType] + js.ArrayValue(arrayType, genElems) + } + + /** Gen JS code for a closure. + * + * Input: a `Closure` tree of the form + * {{{ + * Closure(env, call, functionalInterface) + * }}} + * representing the pseudo-syntax + * {{{ + * { (p1, ..., pm) => call(env1, ..., envn, p1, ..., pm) }: functionInterface + * }}} + * where `envi` are identifiers in the local scope. The qualifier of `call` + * is also implicitly captured. + * + * Output: a `js.Closure` tree of the form + * {{{ + * js.Closure(formalCaptures, formalParams, body, actualCaptures) + * }}} + * representing the pseudo-syntax + * {{{ + * lambda( + * formalParam1, ..., formalParamM) = body + * }}} + * where the `actualCaptures` and `body` are, in general, arbitrary + * expressions. But in this case, `actualCaptures` will be identifiers from + * `env`, and the `body` will be of the form + * {{{ + * call(formalCapture1.ref, ..., formalCaptureN.ref, + * formalParam1.ref, ...formalParamM.ref) + * }}} + * + * When the `js.Closure` node is evaluated, i.e., when the closure value is + * created, the expressions of the `actualCaptures` are evaluated, and the + * results of those evaluations is "stored" in the environment of the + * closure as the corresponding `formalCapture`. + * + * When we later *call* the closure, the `formalCaptures` already have their + * values from the environment, and they are available in the `body`. The + * `formalParams` of the created closure receive their values from the + * actual arguments at the call-site of the closure, and they are also + * available in the `body`. + */ + private def genClosure(tree: Closure): js.Tree = { + implicit val pos: Position = tree.pos + val Closure(env, call, functionalInterface) = tree + + val envSize = env.size + + val (fun, args) = call match { + // case Apply(fun, args) => (fun, args) // Conjectured not to happen + case t @ Select(_, _) => (t, Nil) + case t @ Ident(_) => (t, Nil) + } + val sym = fun.symbol + + val qualifier = qualifierOf(fun) + val allCaptureValues = qualifier :: env + + val (formalCaptures, actualCaptures) = allCaptureValues.map { value => + implicit val pos: Position = value.pos + val formalIdent = value match { + case Ident(name) => freshLocalIdent(name.toString) + case This(_) => freshLocalIdent("this") + case _ => freshLocalIdent() + } + val formalCapture = + js.ParamDef(formalIdent, toIRType(value.tpe), mutable = false, rest = false) + val actualCapture = genExpr(value) + (formalCapture, actualCapture) + }.unzip + + val formalParamNames = sym.info.paramNamess.flatten.drop(envSize) + val formalParamTypes = sym.info.paramTypess.flatten.drop(envSize) + val (formalParams, actualParams) = formalParamNames.zip(formalParamTypes).map { + case (name, tpe) => + val formalParam = js.ParamDef(freshLocalIdent(name.toString), + jstpe.AnyType, mutable = false, rest = false) + val actualParam = unbox(formalParam.ref, tpe) + (formalParam, actualParam) + }.unzip + + val genBody = { + val thisCaptureRef :: argCaptureRefs = formalCaptures.map(_.ref) + val call = genApplyMethod(thisCaptureRef, sym, argCaptureRefs ::: actualParams) + box(call, sym.info.finalResultType) + } + + val closure = js.Closure(formalCaptures, formalParams, genBody, actualCaptures) + ctx.debuglog(closure.toString) + + val funInterfaceSym = functionalInterface.tpe.widenDealias.typeSymbol + if (jsdefn.isJSFunctionClass(funInterfaceSym)) { + closure + } else { + assert(!funInterfaceSym.exists || defn.isFunctionClass(funInterfaceSym), + s"Invalid functional interface $funInterfaceSym reached the back-end") + val cls = "sjsr_AnonFunction" + formalParams.size + val ctor = js.Ident("init___sjs_js_Function" + formalParams.size) + js.New(jstpe.ClassType(cls), ctor, List(closure)) + } + } + + /** Boxes a value of the given type before `elimErasedValueType`. + * + * This should be used when sending values to a JavaScript context, which + * is erased/boxed at the IR level, although it is not erased at the + * dotty/JVM level. + * + * @param expr Tree to be boxed if needed. + * @param tpeEnteringElimErasedValueType The type of `expr` as it was + * entering the `elimErasedValueType` phase. + */ + private def box(expr: js.Tree, tpeEnteringElimErasedValueType: Type)( + implicit pos: Position): js.Tree = { + + tpeEnteringElimErasedValueType match { + case tpe if isPrimitiveValueType(tpe) => + makePrimitiveBox(expr, tpe) + + /*case tpe: ErasedValueType => + val boxedClass = tpe.valueClazz + val ctor = boxedClass.primaryConstructor + genNew(boxedClass, ctor, List(expr))*/ + + case _ => + expr + } + } + + /** Unboxes a value typed as Any to the given type before `elimErasedValueType`. + * + * This should be used when receiving values from a JavaScript context, + * which is erased/boxed at the IR level, although it is not erased at the + * dotty/JVM level. + * + * @param expr Tree to be extracted. + * @param tpeEnteringElimErasedValueType The type of `expr` as it was + * entering the `elimErasedValueType` phase. + */ + private def unbox(expr: js.Tree, tpeEnteringElimErasedValueType: Type)( + implicit pos: Position): js.Tree = { + + tpeEnteringElimErasedValueType match { + case tpe if isPrimitiveValueType(tpe) => + makePrimitiveUnbox(expr, tpe) + + /*case tpe: ErasedValueType => + val boxedClass = tpe.valueClazz + val unboxMethod = boxedClass.derivedValueClassUnbox + val content = genApplyMethod( + genAsInstanceOf(expr, tpe), unboxMethod, Nil) + if (unboxMethod.tpe.resultType <:< tpe.erasedUnderlying) + content + else + fromAny(content, tpe.erasedUnderlying)*/ + + case tpe => + genAsInstanceOf(expr, tpe) + } + } + + /** Gen JS code for an asInstanceOf cast (for reference types only) */ + private def genAsInstanceOf(value: js.Tree, to: Type)( + implicit pos: Position): js.Tree = { + + val sym = to.widenDealias.typeSymbol + + if (sym == defn.ObjectClass || isJSType(sym)) { + /* asInstanceOf[Object] always succeeds, and + * asInstanceOf to a raw JS type is completely erased. + */ + value + } else { + js.AsInstanceOf(value, toReferenceType(to)) + } + } + + /** Gen JS code for an isInstanceOf test (for reference types only) */ + private def genIsInstanceOf(tree: Tree, value: js.Tree, to: Type): js.Tree = { + implicit val pos: Position = tree.pos + val sym = to.widenDealias.typeSymbol + + if (sym == defn.ObjectClass) { + js.BinaryOp(js.BinaryOp.!==, value, js.Null()) + } else if (isJSType(sym)) { + if (sym.is(Trait)) { + ctx.error( + s"isInstanceOf[${sym.fullName}] not supported because it is a JS trait", + tree.pos) + js.BooleanLiteral(true) + } else { + js.Unbox(js.JSBinaryOp( + js.JSBinaryOp.instanceof, value, genLoadJSConstructor(sym)), 'Z') + } + } else { + js.IsInstanceOf(value, toReferenceType(to)) + } + } + + /** Gen a dynamically linked call to a Scala method. */ + private def genApplyMethod(receiver: js.Tree, + methodSym: Symbol, arguments: List[js.Tree])( + implicit pos: Position): js.Tree = { + js.Apply(receiver, encodeMethodSym(methodSym), arguments)( + toIRType(patchedResultType(methodSym))) + } + + /** Gen a statically linked call to an instance method. */ + private def genApplyMethodStatically(receiver: js.Tree, method: Symbol, + arguments: List[js.Tree])(implicit pos: Position): js.Tree = { + val className = encodeClassFullName(method.owner) + val methodIdent = encodeMethodSym(method) + val resultType = toIRType(patchedResultType(method)) + js.ApplyStatically(receiver, jstpe.ClassType(className), + methodIdent, arguments)(resultType) + } + + /** Gen a call to a static method. */ + private def genApplyStatic(method: Symbol, arguments: List[js.Tree])( + implicit pos: Position): js.Tree = { + val cls = jstpe.ClassType(encodeClassFullName(method.owner)) + val methodIdent = encodeMethodSym(method) + js.ApplyStatic(cls, methodIdent, arguments)( + toIRType(patchedResultType(method))) + } + + /** Gen a call to a Scala2 impl class method. */ + private def genTraitImplApply(method: Symbol, arguments: List[js.Tree])( + implicit pos: Position): js.Tree = { + genApplyStatic(method, arguments) + } + + /** Gen a call to a non-exposed method of a non-native JS class. */ + private def genApplyJSClassMethod(receiver: js.Tree, method: Symbol, + arguments: List[js.Tree])(implicit pos: Position): js.Tree = { + genApplyStatic(method, receiver :: arguments) + } + + /** Gen a call to a method of a Scala top-level module. */ + private def genModuleApplyMethod(methodSym: Symbol, arguments: List[js.Tree])( + implicit pos: Position): js.Tree = { + genApplyMethod(genLoadModule(methodSym.owner), methodSym, arguments) + } + + /** Gen JS code for `new java.lang.String(...)`. + * + * Rewires the instantiation to calling the appropriate overload of + * `newString` in the object `scala.scalajs.runtime.RuntimeString`. + */ + private def genNewString(ctor: Symbol, arguments: List[js.Tree])( + implicit pos: Position): js.Tree = { + js.Apply( + genLoadModule(jsdefn.RuntimeStringModuleClass), + encodeRTStringCtorSym(ctor), arguments)( + jstpe.ClassType(ir.Definitions.StringClass)) + } + + /** Gen a dynamically linked call to a method of java.lang.String. + * + * Forwards the call to the module scala.scalajs.runtime.RuntimeString. + */ + private def genApplyMethodOfString(receiver: js.Tree, + methodSym: Symbol, arguments: List[js.Tree])( + implicit pos: Position): js.Tree = { + js.Apply( + genLoadModule(jsdefn.RuntimeStringModuleClass), + encodeRTStringMethodSym(methodSym), + receiver :: arguments)( + toIRType(patchedResultType(methodSym))) + } + + /** Gen a boxing operation (tpe is the primitive type) */ + private def makePrimitiveBox(expr: js.Tree, tpe: Type)( + implicit pos: Position): js.Tree = { + toReferenceType(tpe) match { + case jstpe.ClassType(cls) if ir.Definitions.isPrimitiveClass(cls) => + assert(cls.length == 1) + (cls.charAt(0): @switch) match { + case 'V' => + // must be handled at least for JS interop + js.Block(expr, js.Undefined()) + case 'C' => + genModuleApplyMethod(jsdefn.BoxesRunTime_boxToCharacter, List(expr)) + case _ => + expr // box is identity for all non-Char types + } + + case _ => + throw new FatalError( + s"makePrimitiveBox requires a primitive type, found $tpe at $pos") + } + } + + /** Gen an unboxing operation (tpe is the primitive type) */ + private def makePrimitiveUnbox(expr: js.Tree, tpe: Type)( + implicit pos: Position): js.Tree = { + toReferenceType(tpe) match { + case jstpe.ClassType(cls) if ir.Definitions.isPrimitiveClass(cls) => + assert(cls.length == 1) + (cls.charAt(0): @switch) match { + case 'V' => + // must be handled at least for JS interop + expr + case 'C' => + genModuleApplyMethod(jsdefn.BoxesRunTime_unboxToChar, List(expr)) + case primitiveCharCode => + js.Unbox(expr, primitiveCharCode) + } + + case _ => + throw new FatalError( + s"makePrimitiveUnbox requires a primitive type, found $tpe at $pos") + } + } + + /** Gen actual actual arguments to Scala method call. + * Returns a list of the transformed arguments. + * + * This tries to optimize repeated arguments (varargs) by turning them + * into js.WrappedArray instead of Scala wrapped arrays. + */ + private def genActualArgs(sym: Symbol, args: List[Tree])( + implicit pos: Position): List[js.Tree] = { + args.map(genExpr) + /*val wereRepeated = exitingPhase(currentRun.typerPhase) { + sym.tpe.params.map(p => isScalaRepeatedParamType(p.tpe)) + } + + if (wereRepeated.size > args.size) { + // Should not happen, but let's not crash + args.map(genExpr) + } else { + /* Arguments that are in excess compared to the type signature after + * erasure are lambda-lifted arguments. They cannot be repeated, hence + * the extension to `false`. + */ + for ((arg, wasRepeated) <- args.zipAll(wereRepeated, EmptyTree, false)) yield { + if (wasRepeated) { + tryGenRepeatedParamAsJSArray(arg, handleNil = false).fold { + genExpr(arg) + } { genArgs => + genNew(WrappedArrayClass, WrappedArray_ctor, + List(js.JSArrayConstr(genArgs))) + } + } else { + genExpr(arg) + } + } + }*/ + } + + /** Gen actual actual arguments to a JS method call. + * Returns a list of the transformed arguments. + * + * - TODO Repeated arguments (varargs) are expanded + * - Default arguments are omitted or replaced by undefined + * - All arguments are boxed + * + * Repeated arguments that cannot be expanded at compile time (i.e., if a + * Seq is passed to a varargs parameter with the syntax `seq: _*`) will be + * wrapped in a [[js.JSSpread]] node to be expanded at runtime. + */ + private def genActualJSArgs(sym: Symbol, args: List[Tree])( + implicit pos: Position): List[js.Tree] = { + + def paramNamesAndTypes(implicit ctx: Context): List[(Names.TermName, Type)] = + sym.info.paramNamess.flatten.zip(sym.info.paramTypess.flatten) + + val wereRepeated = ctx.atPhase(ctx.elimRepeatedPhase) { implicit ctx => + for ((name, tpe) <- paramNamesAndTypes) + yield (name -> tpe.isRepeatedParam) + }.toMap + + val paramTypes = ctx.atPhase(ctx.elimErasedValueTypePhase) { implicit ctx => + paramNamesAndTypes + }.toMap + + var reversedArgs: List[js.Tree] = Nil + + for ((arg, (paramName, paramType)) <- args.zip(paramNamesAndTypes)) { + val wasRepeated = wereRepeated.getOrElse(paramName, false) + if (wasRepeated) { + reversedArgs = + genJSRepeatedParam(arg) reverse_::: reversedArgs + } else { + val unboxedArg = genExpr(arg) + val boxedArg = unboxedArg match { + case js.UndefinedParam() => + unboxedArg + case _ => + val tpe = paramTypes.getOrElse(paramName, paramType) + box(unboxedArg, tpe) + } + reversedArgs ::= boxedArg + } + } + + /* Remove all consecutive js.UndefinedParam's at the end of the argument + * list. No check is performed whether they may be there, since they will + * only be placed where default arguments can be anyway. + */ + reversedArgs = reversedArgs.dropWhile(_.isInstanceOf[js.UndefinedParam]) + + /* Find remaining js.UndefinedParam and replace by js.Undefined. This can + * happen with named arguments or with multiple argument lists. + */ + reversedArgs = reversedArgs map { + case js.UndefinedParam() => js.Undefined() + case arg => arg + } + + reversedArgs.reverse + } + + /** Gen JS code for a repeated param of a JS method. + * + * In this case `arg` has type `Seq[T]` for some `T`, but the result should + * be an expanded list of the elements in the sequence. So this method + * takes care of the conversion. + * + * It is specialized for the shapes of tree generated by the desugaring + * of repeated params in Scala, so that these are actually expanded at + * compile-time. + * + * Otherwise, it returns a `JSSpread` with the `Seq` converted to a + * `js.Array`. + */ + private def genJSRepeatedParam(arg: Tree): List[js.Tree] = { + tryGenRepeatedParamAsJSArray(arg, handleNil = true).getOrElse { + /* Fall back to calling runtime.genTraversableOnce2jsArray + * to perform the conversion to js.Array, then wrap in a Spread + * operator. + */ + implicit val pos: Position = arg.pos + val jsArrayArg = genModuleApplyMethod( + jsdefn.RuntimePackage_genTraversableOnce2jsArray, + List(genExpr(arg))) + List(js.JSSpread(jsArrayArg)) + } + } + + /** Try and expand an actual argument to a repeated param `(xs: T*)`. + * + * This method recognizes the shapes of tree generated by the desugaring + * of repeated params in Scala, and expands them. + * If `arg` does not have the shape of a generated repeated param, this + * method returns `None`. + */ + private def tryGenRepeatedParamAsJSArray(arg: Tree, + handleNil: Boolean): Option[List[js.Tree]] = { + implicit val pos: Position = arg.pos + + // Given a method `def foo(args: T*)` + arg match { + // foo(arg1, arg2, ..., argN) where N > 0 + case MaybeAsInstanceOf(WrapArray(MaybeAsInstanceOf(array: JavaSeqLiteral))) => + /* Value classes in arrays are already boxed, so no need to use + * the type before erasure. + * TODO Is this true in dotty? + */ + Some(array.elems.map(e => box(genExpr(e), e.tpe))) + + // foo() + case Ident(_) if handleNil && arg.symbol == defn.NilModule => + Some(Nil) + + // foo(argSeq: _*) - cannot be optimized + case _ => + None + } + } + + private object MaybeAsInstanceOf { + def unapply(tree: Tree): Some[Tree] = tree match { + case TypeApply(asInstanceOf_? @ Select(base, _), _) + if asInstanceOf_?.symbol == defn.Any_asInstanceOf => + Some(base) + case _ => + Some(tree) + } + } + + private object WrapArray { + lazy val isWrapArray: Set[Symbol] = { + val names = { + defn.ScalaValueClasses().map(sym => nme.wrapXArray(sym.name)) ++ + Set(nme.wrapRefArray, nme.genericWrapArray) + } + names.map(defn.ScalaPredefModule.requiredMethod(_)).toSet + } + + def unapply(tree: Apply): Option[Tree] = tree match { + case Apply(wrapArray_?, List(wrapped)) if isWrapArray(wrapArray_?.symbol) => + Some(wrapped) + case _ => + None + } + } + + /** Gen JS code for loading a Java static field. + */ + private def genLoadStaticField(sym: Symbol)(implicit pos: Position): js.Tree = { + /* Actually, there is no static member in Scala.js. If we come here, that + * is because we found the symbol in a Java-emitted .class in the + * classpath. But the corresponding implementation in Scala.js will + * actually be a val in the companion module. + */ + + if (sym == defn.BoxedUnit_UNIT) { + js.Undefined() + } else { + val instance = genLoadModule(sym.owner) + val method = encodeStaticMemberSym(sym) + js.Apply(instance, method, Nil)(toIRType(sym.info)) + } + } + + /** Gen JS code for loading a module. + * + * Can be given either the module symbol, or its module class symbol. + */ + private def genLoadModule(sym0: Symbol)(implicit pos: Position): js.Tree = { + require(sym0.is(Module), + "genLoadModule called with non-module symbol: " + sym0) + val sym1 = if (sym0.isTerm) sym0.moduleClass else sym0 + val sym = // redirect all static methods of String to RuntimeString + if (sym1 == defn.StringModule) jsdefn.RuntimeStringModule.moduleClass + else sym1 + + if (isJSType(sym)) { + if (isScalaJSDefinedJSClass(sym)) + js.LoadJSModule(jstpe.ClassType(encodeClassFullName(sym))) + else if (sym.derivesFrom(jsdefn.JSGlobalScopeClass)) + genLoadJSGlobal() + else + genLoadNativeJSModule(sym) + } else { + js.LoadModule(jstpe.ClassType(encodeClassFullName(sym))) + } + } + + /** Gen JS code representing the constructor of a JS class. */ + private def genLoadJSConstructor(sym: Symbol)( + implicit pos: Position): js.Tree = { + assert(!isStaticModule(sym) && !sym.is(Trait), + s"genPrimitiveJSClass called with non-class $sym") + js.LoadJSConstructor(jstpe.ClassType(encodeClassFullName(sym))) + } + + /** Gen JS code representing a native JS module. */ + private def genLoadNativeJSModule(sym: Symbol)( + implicit pos: Position): js.Tree = { + require(sym.is(ModuleClass), + s"genLoadNativeJSModule called with non-module $sym") + fullJSNameOf(sym).split('.').foldLeft(genLoadJSGlobal()) { (memo, chunk) => + js.JSBracketSelect(memo, js.StringLiteral(chunk)) + } + } + + /** Gen JS code to load the JavaScript global scope. */ + private def genLoadJSGlobal()(implicit pos: Position): js.Tree = { + js.JSBracketSelect( + js.JSBracketSelect(js.JSLinkingInfo(), js.StringLiteral("envInfo")), + js.StringLiteral("global")) + } + + /** Generate a Class[_] value (e.g. coming from classOf[T]) */ + private def genClassConstant(tpe: Type)(implicit pos: Position): js.Tree = + js.ClassOf(toReferenceType(tpe)) + + private def isStaticModule(sym: Symbol): Boolean = + sym.is(Module) && sym.isStatic + + private def isPrimitiveValueType(tpe: Type): Boolean = { + tpe.widenDealias match { + case JavaArrayType(_) => false + case t => t.typeSymbol.asClass.isPrimitiveValueClass + } + } + +} diff --git a/src/dotty/tools/backend/sjs/JSDefinitions.scala b/src/dotty/tools/backend/sjs/JSDefinitions.scala new file mode 100644 index 000000000000..bd0b740318e0 --- /dev/null +++ b/src/dotty/tools/backend/sjs/JSDefinitions.scala @@ -0,0 +1,199 @@ +package dotty.tools.backend.sjs + +import dotty.tools.dotc.core._ + +import Types._ +import Contexts._ +import Symbols._ +import Names._ +import StdNames._ +import Decorators._ + +import dotty.tools.dotc.config.SJSPlatform + +object JSDefinitions { + /** The Scala.js-specific definitions for the current context. */ + def jsdefn(implicit ctx: Context): JSDefinitions = + ctx.platform.asInstanceOf[SJSPlatform].jsDefinitions +} + +final class JSDefinitions()(implicit ctx: Context) { + + lazy val InlineAnnotType: TypeRef = ctx.requiredClassRef("scala.inline") + def InlineAnnot(implicit ctx: Context) = InlineAnnotType.symbol.asClass + lazy val NoinlineAnnotType: TypeRef = ctx.requiredClassRef("scala.noinline") + def NoinlineAnnot(implicit ctx: Context) = NoinlineAnnotType.symbol.asClass + + lazy val ScalaJSJSPackageVal = ctx.requiredPackage("scala.scalajs.js") + lazy val ScalaJSJSPackageClass = ScalaJSJSPackageVal.moduleClass.asClass + lazy val JSPackage_typeOfR = ScalaJSJSPackageClass.requiredMethodRef("typeOf") + def JSPackage_typeOf(implicit ctx: Context) = JSPackage_typeOfR.symbol + lazy val JSPackage_constructorOfR = ScalaJSJSPackageClass.requiredMethodRef("constructorOf") + def JSPackage_constructorOf(implicit ctx: Context) = JSPackage_constructorOfR.symbol + lazy val JSPackage_debuggerR = ScalaJSJSPackageClass.requiredMethodRef("debugger") + def JSPackage_debugger(implicit ctx: Context) = JSPackage_debuggerR.symbol + lazy val JSPackage_nativeR = ScalaJSJSPackageClass.requiredMethodRef("native") + def JSPackage_native(implicit ctx: Context) = JSPackage_nativeR.symbol + + lazy val JSNativeAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.native") + def JSNativeAnnot(implicit ctx: Context) = JSNativeAnnotType.symbol.asClass + + lazy val JSAnyType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.Any") + def JSAnyClass(implicit ctx: Context) = JSAnyType.symbol.asClass + lazy val JSObjectType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.Object") + def JSObjectClass(implicit ctx: Context) = JSObjectType.symbol.asClass + lazy val JSBaseThisFunctionType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.ThisFunction") + def JSBaseThisFunctionClass(implicit ctx: Context) = JSBaseThisFunctionType.symbol.asClass + + lazy val JSDictionaryType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.Dictionary") + def JSDictionaryClass(implicit ctx: Context) = JSDictionaryType.symbol.asClass + lazy val JSDictionary_deleteR = JSDictionaryClass.requiredMethodRef("delete") + def JSDictionary_delete(implicit ctx: Context) = JSDictionary_deleteR.symbol + + lazy val JSGlobalScopeType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.GlobalScope") + def JSGlobalScopeClass(implicit ctx: Context) = JSGlobalScopeType.symbol.asClass + + lazy val JSArrayType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.Array") + def JSArrayClass(implicit ctx: Context) = JSArrayType.symbol.asClass + + lazy val JSFunctionType = (0 to 22).map(n => ctx.requiredClassRef("scala.scalajs.js.Function" + n)).toArray + def JSFunctionClass(n: Int)(implicit ctx: Context) = JSFunctionType(n).symbol.asClass + lazy val JSThisFunctionType = (0 to 21).map(n => ctx.requiredClassRef("scala.scalajs.js.ThisFunction" + n)).toArray + def JSThisFunctionClass(n: Int)(implicit ctx: Context) = JSThisFunctionType(n).symbol.asClass + + lazy val RuntimeExceptionType: TypeRef = ctx.requiredClassRef("java.lang.RuntimeException") + def RuntimeExceptionClass(implicit ctx: Context) = RuntimeExceptionType.symbol.asClass + lazy val JavaScriptExceptionType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.JavaScriptException") + def JavaScriptExceptionClass(implicit ctx: Context) = JavaScriptExceptionType.symbol.asClass + + lazy val JSNameAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSName") + def JSNameAnnot(implicit ctx: Context) = JSNameAnnotType.symbol.asClass + lazy val JSFullNameAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSFullName") + def JSFullNameAnnot(implicit ctx: Context) = JSFullNameAnnotType.symbol.asClass + lazy val JSBracketAccessAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSBracketAccess") + def JSBracketAccessAnnot(implicit ctx: Context) = JSBracketAccessAnnotType.symbol.asClass + lazy val JSBracketCallAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSBracketCall") + def JSBracketCallAnnot(implicit ctx: Context) = JSBracketCallAnnotType.symbol.asClass + lazy val JSExportAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSExport") + def JSExportAnnot(implicit ctx: Context) = JSExportAnnotType.symbol.asClass + lazy val JSExportDescendentObjectsAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSExportDescendentObjects") + def JSExportDescendentObjectsAnnot(implicit ctx: Context) = JSExportDescendentObjectsAnnotType.symbol.asClass + lazy val JSExportDescendentClassesAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSExportDescendentClasses") + def JSExportDescendentClassesAnnot(implicit ctx: Context) = JSExportDescendentClassesAnnotType.symbol.asClass + lazy val JSExportAllAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSExportAll") + def JSExportAllAnnot(implicit ctx: Context) = JSExportAllAnnotType.symbol.asClass + lazy val JSExportNamedAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.JSExportNamed") + def JSExportNamedAnnot(implicit ctx: Context) = JSExportNamedAnnotType.symbol.asClass + lazy val RawJSTypeAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.RawJSType") + def RawJSTypeAnnot(implicit ctx: Context) = RawJSTypeAnnotType.symbol.asClass + lazy val ExposedJSMemberAnnotType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.annotation.ExposedJSMember") + def ExposedJSMemberAnnot(implicit ctx: Context) = ExposedJSMemberAnnotType.symbol.asClass + + lazy val JSAnyModuleRef = ctx.requiredModuleRef("scala.scalajs.js.Any") + def JSAnyModule(implicit ctx: Context) = JSAnyModuleRef.symbol + lazy val JSAny_fromFunctionR = (0 to 22).map(n => JSAnyModule.requiredMethodRef("fromFunction" + n)).toArray + def JSAny_fromFunction(n: Int)(implicit ctx: Context) = JSAny_fromFunctionR(n).symbol + + lazy val JSDynamicModuleRef = ctx.requiredModuleRef("scala.scalajs.js.Dynamic") + def JSDynamicModule(implicit ctx: Context) = JSDynamicModuleRef.symbol + lazy val JSDynamic_newInstanceR = JSDynamicModule.requiredMethodRef("newInstance") + def JSDynamic_newInstance(implicit ctx: Context) = JSDynamic_newInstanceR.symbol + + lazy val JSDynamicLiteralModuleRef = JSDynamicModule.moduleClass.requiredValueRef("literal") + def JSDynamicLiteralModule(implicit ctx: Context) = JSDynamicLiteralModuleRef.symbol + lazy val JSDynamicLiteral_applyDynamicNamedR = JSDynamicLiteralModule.requiredMethodRef("applyDynamicNamed") + def JSDynamicLiteral_applyDynamicNamed(implicit ctx: Context) = JSDynamicLiteral_applyDynamicNamedR.symbol + lazy val JSDynamicLiteral_applyDynamicR = JSDynamicLiteralModule.requiredMethodRef("applyDynamic") + def JSDynamicLiteral_applyDynamic(implicit ctx: Context) = JSDynamicLiteral_applyDynamicR.symbol + + lazy val JSObjectModuleRef = ctx.requiredModuleRef("scala.scalajs.js.Object") + def JSObjectModule(implicit ctx: Context) = JSObjectModuleRef.symbol + lazy val JSObject_hasPropertyR = JSObjectModule.requiredMethodRef("hasProperty") + def JSObject_hasProperty(implicit ctx: Context) = JSObject_hasPropertyR.symbol + lazy val JSObject_propertiesR = JSObjectModule.requiredMethodRef("properties") + def JSObject_properties(implicit ctx: Context) = JSObject_propertiesR.symbol + + lazy val JSArrayModuleRef = ctx.requiredModuleRef("scala.scalajs.js.Array") + def JSArrayModule(implicit ctx: Context) = JSArrayModuleRef.symbol + lazy val JSArray_applyR = JSArrayModule.requiredMethodRef(nme.apply) + def JSArray_apply(implicit ctx: Context) = JSArray_applyR.symbol + + lazy val JSThisFunctionModuleRef = ctx.requiredModuleRef("scala.scalajs.js.ThisFunction") + def JSThisFunctionModule(implicit ctx: Context) = JSThisFunctionModuleRef.symbol + lazy val JSThisFunction_fromFunctionR = (1 to 22).map(n => JSThisFunctionModule.requiredMethodRef("fromFunction" + n)).toArray + def JSThisFunction_fromFunction(n: Int)(implicit ctx: Context) = JSThisFunction_fromFunctionR(n - 1).symbol + + lazy val JSConstructorTagModuleRef = ctx.requiredModuleRef("scala.scalajs.js.ConstructorTag") + def JSConstructorTagModule(implicit ctx: Context) = JSConstructorTagModuleRef.symbol + lazy val JSConstructorTag_materializeR = JSConstructorTagModule.requiredMethodRef("materialize") + def JSConstructorTag_materialize(implicit ctx: Context) = JSConstructorTag_materializeR.symbol + + lazy val RuntimeStringModuleRef = ctx.requiredModuleRef("scala.scalajs.runtime.RuntimeString") + def RuntimeStringModule(implicit ctx: Context) = RuntimeStringModuleRef.symbol + def RuntimeStringModuleClass(implicit ctx: Context) = RuntimeStringModule.moduleClass.asClass + + lazy val BooleanReflectiveCallType: TypeRef = ctx.requiredClassRef("scala.scalajs.runtime.BooleanReflectiveCall") + def BooleanReflectiveCallClass(implicit ctx: Context) = BooleanReflectiveCallType.symbol.asClass + lazy val NumberReflectiveCallType: TypeRef = ctx.requiredClassRef("scala.scalajs.runtime.NumberReflectiveCall") + def NumberReflectiveCallClass(implicit ctx: Context) = NumberReflectiveCallType.symbol.asClass + lazy val IntegerReflectiveCallType: TypeRef = ctx.requiredClassRef("scala.scalajs.runtime.IntegerReflectiveCall") + def IntegerReflectiveCallClass(implicit ctx: Context) = IntegerReflectiveCallType.symbol.asClass + lazy val LongReflectiveCallType: TypeRef = ctx.requiredClassRef("scala.scalajs.runtime.LongReflectiveCall") + def LongReflectiveCallClass(implicit ctx: Context) = LongReflectiveCallType.symbol.asClass + + lazy val RuntimePackageVal = ctx.requiredPackage("scala.scalajs.runtime") + lazy val RuntimePackageClass = RuntimePackageVal.moduleClass.asClass + lazy val RuntimePackage_wrapJavaScriptExceptionR = RuntimePackageClass.requiredMethodRef("wrapJavaScriptException") + def RuntimePackage_typeOf(implicit ctx: Context) = RuntimePackage_wrapJavaScriptExceptionR.symbol + lazy val RuntimePackage_unwrapJavaScriptExceptionR = RuntimePackageClass.requiredMethodRef("unwrapJavaScriptException") + def RuntimePackage_unwrapJavaScriptException(implicit ctx: Context) = RuntimePackage_unwrapJavaScriptExceptionR.symbol + lazy val RuntimePackage_genTraversableOnce2jsArrayR = RuntimePackageClass.requiredMethodRef("genTraversableOnce2jsArray") + def RuntimePackage_genTraversableOnce2jsArray(implicit ctx: Context) = RuntimePackage_genTraversableOnce2jsArrayR.symbol + lazy val RuntimePackage_jsTupleArray2jsObjectR = RuntimePackageClass.requiredMethodRef("jsTupleArray2jsObject") + def RuntimePackage_jsTupleArray2jsObject(implicit ctx: Context) = RuntimePackage_jsTupleArray2jsObjectR.symbol + lazy val RuntimePackage_constructorOfR = RuntimePackageClass.requiredMethodRef("constructorOf") + def RuntimePackage_constructorOf(implicit ctx: Context) = RuntimePackage_constructorOfR.symbol + lazy val RuntimePackage_newConstructorTagR = RuntimePackageClass.requiredMethodRef("newConstructorTag") + def RuntimePackage_newConstructorTag(implicit ctx: Context) = RuntimePackage_newConstructorTagR.symbol + lazy val RuntimePackage_propertiesOfR = RuntimePackageClass.requiredMethodRef("propertiesOf") + def RuntimePackage_propertiesOf(implicit ctx: Context) = RuntimePackage_propertiesOfR.symbol + lazy val RuntimePackage_environmentInfoR = RuntimePackageClass.requiredMethodRef("environmentInfo") + def RuntimePackage_environmentInfo(implicit ctx: Context) = RuntimePackage_environmentInfoR.symbol + lazy val RuntimePackage_linkingInfoR = RuntimePackageClass.requiredMethodRef("linkingInfo") + def RuntimePackage_linkingInfo(implicit ctx: Context) = RuntimePackage_linkingInfoR.symbol + + lazy val WrappedArrayType: TypeRef = ctx.requiredClassRef("scala.scalajs.js.WrappedArray") + def WrappedArrayClass(implicit ctx: Context) = WrappedArrayType.symbol.asClass + + lazy val ScalaRunTime_isArrayR = defn.ScalaRuntimeModule.requiredMethodRef("isArray", List(???, ???)) + def ScalaRunTime_isArray(implicit ctx: Context): Symbol = ScalaRunTime_isArrayR.symbol + + lazy val BoxesRunTime_boxToCharacterR = defn.BoxesRunTimeModule.requiredMethodRef("boxToCharacter") + def BoxesRunTime_boxToCharacter(implicit ctx: Context): Symbol = BoxesRunTime_boxToCharacterR.symbol + lazy val BoxesRunTime_unboxToCharR = defn.BoxesRunTimeModule.requiredMethodRef("unboxToChar") + def BoxesRunTime_unboxToChar(implicit ctx: Context): Symbol = BoxesRunTime_unboxToCharR.symbol + + /** If `cls` is a class in the scala package, its name, otherwise EmptyTypeName */ + private def scalajsClassName(cls: Symbol)(implicit ctx: Context): TypeName = + if (cls.isClass && cls.owner == ScalaJSJSPackageClass) cls.asClass.name + else EmptyTypeName + + /** Is the given `cls` a class of the form `scala.scalajs.js.prefixN` where + * `N` is a number. + * + * This is similar to `isVarArityClass` in `Definitions.scala`. + */ + private def isScalaJSVarArityClass(cls: Symbol, prefix: Name): Boolean = { + val name = scalajsClassName(cls) + name.startsWith(prefix) && name.drop(prefix.length).forall(_.isDigit) + } + + def isJSFunctionClass(cls: Symbol): Boolean = + isScalaJSVarArityClass(cls, nme.Function) + + private val ThisFunctionName = termName("ThisFunction") + + def isJSThisFunctionClass(cls: Symbol): Boolean = + isScalaJSVarArityClass(cls, ThisFunctionName) + +} diff --git a/src/dotty/tools/backend/sjs/JSEncoding.scala b/src/dotty/tools/backend/sjs/JSEncoding.scala new file mode 100644 index 000000000000..e8ea3258bfa2 --- /dev/null +++ b/src/dotty/tools/backend/sjs/JSEncoding.scala @@ -0,0 +1,389 @@ +package dotty.tools.backend.sjs + +import scala.collection.mutable + +import dotty.tools.FatalError + +import dotty.tools.dotc.core._ +import Periods._ +import SymDenotations._ +import Contexts._ +import Types._ +import Symbols._ +import Denotations._ +import NameOps._ +import StdNames._ + +import org.scalajs.core.ir +import ir.{Trees => js, Types => jstpe} + +import ScopedVar.withScopedVars +import JSDefinitions._ +import JSInterop._ + +/** Encoding of symbol names for JavaScript + * + * Some issues that this encoding solves: + * * Overloading: encode the full signature in the JS name + * * Same scope for fields and methods of a class + * * Global access to classes and modules (by their full name) + * + * @author Sébastien Doeraene + */ +object JSEncoding { + + /** Signature separator string (between parameter types) */ + private final val SignatureSep = "__" + + /** Name given to the local Scala.js environment variable */ + private final val ScalaJSEnvironmentName = "ScalaJS" + + implicit class SymOps(val self: Symbol) extends AnyVal { + def unexpandedName(implicit ctx: Context): Names.Name = + self.name.unexpandedName + } + + implicit class MyNameOps(val self: Names.Name) extends AnyVal { + def decoded: String = self.decode.toString + } + + // Fresh local name generator ---------------------------------------------- + + class LocalNameGenerator { + import LocalNameGenerator._ + + private val usedLocalNames = mutable.Set.empty[String] + private val localSymbolNames = mutable.Map.empty[Symbol, String] + + def localSymbolName(sym: Symbol)(implicit ctx: Context): String = + localSymbolNames.getOrElseUpdate(sym, freshName(sym.name.toString)) + + def freshLocalIdent()(implicit pos: ir.Position): js.Ident = + js.Ident(freshName(), None) + + def freshLocalIdent(base: String)(implicit pos: ir.Position): js.Ident = + js.Ident(freshName(base), Some(base)) + + private def freshName(base: String = "x"): String = { + var suffix = 1 + var longName = base + while (usedLocalNames(longName) || isReserved(longName)) { + suffix += 1 + longName = base+"$"+suffix + } + usedLocalNames += longName + mangleJSName(longName) + } + } + + private object LocalNameGenerator { + private val isReserved = + Set("arguments", "eval", ScalaJSEnvironmentName) + } + + // Encoding methods ---------------------------------------------------------- + + def encodeLabelSym(sym: Symbol)( + implicit ctx: Context, pos: ir.Position, localNames: LocalNameGenerator): js.Ident = { + require(sym.is(Flags.Label), "encodeLabelSym called with non-label symbol: " + sym) + js.Ident(localNames.localSymbolName(sym), Some(sym.unexpandedName.decoded)) + } + + private def allRefClasses(implicit ctx: Context): Set[Symbol] = { + //TODO + /*(Set(ObjectRefClass, VolatileObjectRefClass) ++ + refClass.values ++ volatileRefClass.values)*/ + Set() + } + + def encodeFieldSym(sym: Symbol)( + implicit ctx: Context, pos: ir.Position): js.Ident = { + require(sym.owner.isClass && sym.isTerm && !sym.is(Flags.Method) && !sym.is(Flags.Module), + "encodeFieldSym called with non-field symbol: " + sym) + + val name0 = encodeMemberNameInternal(sym) + val name = + if (name0.charAt(name0.length()-1) != ' ') name0 + else name0.substring(0, name0.length()-1) + + /* We have to special-case fields of Ref types (IntRef, ObjectRef, etc.) + * because they are emitted as private by our .scala source files, but + * they are considered public at use site since their symbols come from + * Java-emitted .class files. + */ + val idSuffix = + if (sym.is(Flags.Private) || allRefClasses.contains(sym.owner)) + sym.owner.asClass.baseClasses.size.toString + else + "f" + + val encodedName = name + "$" + idSuffix + js.Ident(mangleJSName(encodedName), Some(sym.unexpandedName.decoded)) + } + + def encodeMethodSym(sym: Symbol, reflProxy: Boolean = false)( + implicit ctx: Context, pos: ir.Position): js.Ident = { + val (encodedName, paramsString) = encodeMethodNameInternal(sym, reflProxy) + js.Ident(encodedName + paramsString, + Some(sym.unexpandedName.decoded + paramsString)) + } + + def encodeMethodName(sym: Symbol, reflProxy: Boolean = false)( + implicit ctx: Context): String = { + val (encodedName, paramsString) = encodeMethodNameInternal(sym, reflProxy) + encodedName + paramsString + } + + /** Encodes a method symbol of java.lang.String for use in RuntimeString. + * + * This basically means adding an initial parameter of type + * java.lang.String, which is the `this` parameter. + */ + def encodeRTStringMethodSym(sym: Symbol)( + implicit ctx: Context, pos: ir.Position): js.Ident = { + require(sym.owner == defn.StringClass) + require(!sym.isClassConstructor && !sym.is(Flags.Private)) + + val (encodedName, paramsString) = + encodeMethodNameInternal(sym, inRTClass = true) + js.Ident(encodedName + paramsString, + Some(sym.unexpandedName.decoded + paramsString)) + } + + /** Encodes a constructor symbol of java.lang.String for use in RuntimeString. + * + * - The name is rerouted to `newString` + * - The result type is set to `java.lang.String` + */ + def encodeRTStringCtorSym(sym: Symbol)( + implicit ctx: Context, pos: ir.Position): js.Ident = { + require(sym.owner == defn.StringClass) + require(sym.isClassConstructor && !sym.is(Flags.Private)) + + val paramTypeNames = sym.info.firstParamTypes.map(internalName(_)) + val paramAndResultTypeNames = paramTypeNames :+ ir.Definitions.StringClass + val paramsString = makeParamsString(paramAndResultTypeNames) + + js.Ident("newString" + paramsString, + Some(sym.unexpandedName.decoded + paramsString)) + } + + private def encodeMethodNameInternal(sym: Symbol, + reflProxy: Boolean = false, inRTClass: Boolean = false)( + implicit ctx: Context): (String, String) = { + require(sym.is(Flags.Method), "encodeMethodSym called with non-method symbol: " + sym) + + def name = encodeMemberNameInternal(sym) + + val encodedName = { + if (sym.isClassConstructor) { + "init_" + } else if (sym.is(Flags.Private)) { + (mangleJSName(name) + SignatureSep + "p" + + sym.owner.asClass.baseClasses.size.toString) + } else { + mangleJSName(name) + } + } + + val paramsString = makeParamsString(sym, reflProxy, inRTClass) + + (encodedName, paramsString) + } + + def encodeStaticMemberSym(sym: Symbol)( + implicit ctx: Context, pos: ir.Position): js.Ident = { + require(sym.is(Flags.JavaStaticTerm), + "encodeStaticMemberSym called with non-static symbol: " + sym) + js.Ident( + mangleJSName(encodeMemberNameInternal(sym)) + + makeParamsString(List(internalName(sym.info))), + Some(sym.unexpandedName.decoded)) + } + + def encodeLocalSym(sym: Symbol)( + implicit ctx: Context, pos: ir.Position, localNames: LocalNameGenerator): js.Ident = { + require(!sym.owner.isClass && sym.isTerm && !sym.is(Flags.Method) && !sym.is(Flags.Module), + "encodeLocalSym called with non-local symbol: " + sym) + js.Ident(localNames.localSymbolName(sym), Some(sym.unexpandedName.decoded)) + } + + def foreignIsImplClass(sym: Symbol)(implicit ctx: Context): Boolean = + sym.name.isImplClassName + + def encodeClassType(sym: Symbol)(implicit ctx: Context): jstpe.Type = { + if (sym == defn.ObjectClass) jstpe.AnyType + else if (isJSType(sym)) jstpe.AnyType + else { + assert(sym != defn.ArrayClass, + "encodeClassType() cannot be called with ArrayClass") + jstpe.ClassType(encodeClassFullName(sym)) + } + } + + def encodeClassFullNameIdent(sym: Symbol)( + implicit ctx: Context, pos: ir.Position): js.Ident = { + js.Ident(encodeClassFullName(sym), Some(sym.fullName.toString)) + } + + def encodeClassFullName(sym: Symbol)(implicit ctx: Context): String = { + if (sym == defn.NothingClass) ir.Definitions.RuntimeNothingClass + else if (sym == defn.NullClass) ir.Definitions.RuntimeNullClass + else ir.Definitions.encodeClassName(sym.fullName.toString) + } + + private def encodeMemberNameInternal(sym: Symbol)( + implicit ctx: Context): String = { + sym.name.toString.replace("_", "$und").replace("~", "$tilde") + } + + def toIRType(tp: Type)(implicit ctx: Context): jstpe.Type = { + val refType = toReferenceTypeInternal(tp) + refType._1 match { + case tpe: jstpe.ClassType => + val sym = refType._2 + if (sym.asClass.isPrimitiveValueClass) { + if (sym == defn.BooleanClass) + jstpe.BooleanType + else if (sym == defn.FloatClass) + jstpe.FloatType + else if (sym == defn.DoubleClass) + jstpe.DoubleType + else if (sym == defn.LongClass) + jstpe.LongType + else if (sym == defn.UnitClass) + jstpe.NoType + else + jstpe.IntType + } else { + if (sym == defn.ObjectClass || isJSType(sym)) + jstpe.AnyType + else if (sym == defn.NothingClass) + jstpe.NothingType + else if (sym == defn.NullClass) + jstpe.NullType + else + tpe + } + + case tpe: jstpe.ArrayType => + tpe + } + } + + def toReferenceType(tp: Type)(implicit ctx: Context): jstpe.ReferenceType = + toReferenceTypeInternal(tp)._1 + + private def toReferenceTypeInternal(tp: Type)( + implicit ctx: Context): (jstpe.ReferenceType, Symbol) = { + + /** + * Primitive types are represented as TypeRefs to the class symbol of, for example, scala.Int. + * The `primitiveTypeMap` maps those class symbols to the corresponding PrimitiveBType. + */ + def primitiveOrClassToRefType(sym: Symbol): (jstpe.ReferenceType, Symbol) = { + assert(sym.isClass, sym) + //assert(sym != defn.ArrayClass || isCompilingArray, sym) + (jstpe.ClassType(encodeClassFullName(sym)), sym) + } + + /** + * When compiling Array.scala, the type parameter T is not erased and shows up in method + * signatures, e.g. `def apply(i: Int): T`. A TyperRef to T is replaced by ObjectReference. + */ + def nonClassTypeRefToRefType(sym: Symbol): (jstpe.ReferenceType, Symbol) = { + //assert(sym.isType && isCompilingArray, sym) + (jstpe.ClassType(ir.Definitions.ObjectClass), defn.ObjectClass) + } + + tp.widenDealias match { + // Array type such as Array[Int] (kept by erasure) + case JavaArrayType(el) => + val elRefType = toReferenceTypeInternal(el) + (jstpe.ArrayType(elRefType._1), elRefType._2) + + case t: TypeRef => + if (!t.symbol.isClass) nonClassTypeRefToRefType(t.symbol) // See comment on nonClassTypeRefToBType + else primitiveOrClassToRefType(t.symbol) // Common reference to a type such as scala.Int or java.lang.String + + case Types.ClassInfo(_, sym, _, _, _) => + /* We get here, for example, for genLoadModule, which invokes + * toTypeKind(moduleClassSymbol.info) + */ + primitiveOrClassToRefType(sym) + + case t: MethodType => // triggers for LabelDefs + toReferenceTypeInternal(t.resultType) + + /* AnnotatedType should (probably) be eliminated by erasure. However we know it happens for + * meta-annotated annotations (@(ann @getter) val x = 0), so we don't emit a warning. + * The type in the AnnotationInfo is an AnnotatedTpe. Tested in jvm/annotations.scala. + */ + case a @ AnnotatedType(t, _) => + //debuglog(s"typeKind of annotated type $a") + toReferenceTypeInternal(t) + } + } + + /** Patches the result type of a method symbol to sanitize it. + * + * For some reason, dotc thinks that the `info.resultType`of an + * `isConstructor` method (for classes or traits) is the enclosing class + * or trait, but the bodies and usages act as if the result type was `Unit`. + * + * This method returns `UnitType` for constructor methods, and otherwise + * `sym.info.resultType`. + */ + def patchedResultType(sym: Symbol)(implicit ctx: Context): Type = + if (sym.isConstructor) defn.UnitType + else sym.info.resultType + + // Encoding of method signatures + + private def makeParamsString(sym: Symbol, reflProxy: Boolean, + inRTClass: Boolean)( + implicit ctx: Context): String = { + val tpe = sym.info + + val paramTypeNames0 = tpe.firstParamTypes.map(internalName(_)) + + val hasExplicitThisParameter = + inRTClass || isScalaJSDefinedJSClass(sym.owner) + val paramTypeNames = + if (!hasExplicitThisParameter) paramTypeNames0 + else encodeClassFullName(sym.owner) :: paramTypeNames0 + + val paramAndResultTypeNames = { + if (sym.isClassConstructor) + paramTypeNames + else if (reflProxy) + paramTypeNames :+ "" + else + paramTypeNames :+ internalName(patchedResultType(sym)) + } + makeParamsString(paramAndResultTypeNames) + } + + private def makeParamsString(paramAndResultTypeNames: List[String]) = + paramAndResultTypeNames.mkString(SignatureSep, SignatureSep, "") + + /** Computes the internal name for a type. */ + private def internalName(tpe: Type)(implicit ctx: Context): String = + encodeReferenceType(toReferenceType(tpe)) + + /** Encodes a [[Types.ReferenceType]], such as in an encoded method signature. + */ + private def encodeReferenceType(refType: jstpe.ReferenceType): String = { + refType match { + case jstpe.ClassType(encodedName) => encodedName + case jstpe.ArrayType(base, depth) => "A" * depth + base + } + } + + /** Mangles names that are illegal in JavaScript by prepending a `$`. + * Also mangles names that would collide with these mangled names. + */ + private def mangleJSName(name: String) = + if (js.isKeyword(name) || name(0).isDigit || name(0) == '$') "$" + name + else name +} diff --git a/src/dotty/tools/backend/sjs/JSInterop.scala b/src/dotty/tools/backend/sjs/JSInterop.scala new file mode 100644 index 000000000000..6d66c32064ee --- /dev/null +++ b/src/dotty/tools/backend/sjs/JSInterop.scala @@ -0,0 +1,110 @@ +package dotty.tools.backend.sjs + +import dotty.tools.dotc.core._ +import Contexts._ +import Flags._ +import Symbols._ +import NameOps._ +import StdNames._ + +import JSDefinitions._ + +/** Management of the interoperability with JavaScript. */ +object JSInterop { + + /** Is this symbol a JavaScript type? */ + def isJSType(sym: Symbol)(implicit ctx: Context): Boolean = { + //sym.hasAnnotation(jsdefn.RawJSTypeAnnot) + ctx.atPhase(ctx.erasurePhase) { implicit ctx => + sym.derivesFrom(jsdefn.JSAnyClass) + } + } + + /** Is this symbol a Scala.js-defined JS class, i.e., a non-native JS class? */ + def isScalaJSDefinedJSClass(sym: Symbol)(implicit ctx: Context): Boolean = + isJSType(sym) && !sym.hasAnnotation(jsdefn.JSNativeAnnot) + + /** Should this symbol be translated into a JS getter? + * + * This is true for any parameterless method, i.e., defined without `()`. + * Unlike `SymDenotations.isGetter`, it applies to user-defined methods as + * much as *accessor* methods created for `val`s and `var`s. + */ + def isJSGetter(sym: Symbol)(implicit ctx: Context): Boolean = { + sym.info.firstParamTypes.isEmpty && ctx.atPhase(ctx.erasurePhase) { implicit ctx => + sym.info.isParameterless + } + } + + /** Should this symbol be translated into a JS setter? + * + * This is true for any method whose name ends in `_=`. + * Unlike `SymDenotations.isGetter`, it applies to user-defined methods as + * much as *accessor* methods created for `var`s. + */ + def isJSSetter(sym: Symbol)(implicit ctx: Context): Boolean = + sym.name.isSetterName && sym.is(Method) + + /** Should this symbol be translated into a JS bracket access? + * + * This is true for methods annotated with `@JSBracketAccess`. + */ + def isJSBracketAccess(sym: Symbol)(implicit ctx: Context): Boolean = + sym.hasAnnotation(jsdefn.JSBracketAccessAnnot) + + /** Should this symbol be translated into a JS bracket call? + * + * This is true for methods annotated with `@JSBracketCall`. + */ + def isJSBracketCall(sym: Symbol)(implicit ctx: Context): Boolean = + sym.hasAnnotation(jsdefn.JSBracketCallAnnot) + + /** Is this symbol a default param accessor for a JS method? + * + * For default param accessors of *constructors*, we need to test whether + * the companion *class* of the owner is a JS type; not whether the owner + * is a JS type. + */ + def isJSDefaultParam(sym: Symbol)(implicit ctx: Context): Boolean = { + sym.name.isDefaultGetterName && { + val owner = sym.owner + if (owner.is(ModuleClass) && + sym.name.asTermName.defaultGetterToMethod == nme.CONSTRUCTOR) { + isJSType(owner.linkedClass) + } else { + isJSType(owner) + } + } + } + + /** Gets the unqualified JS name of a symbol. + * + * If it is not explicitly specified with an `@JSName` annotation, the + * JS name is inferred from the Scala name. + */ + def jsNameOf(sym: Symbol)(implicit ctx: Context): String = { + sym.getAnnotation(jsdefn.JSNameAnnot).flatMap(_.argumentConstant(0)).fold { + val base = sym.name.unexpandedName.decode.toString.stripSuffix("_=") + if (sym.is(ModuleClass)) base.stripSuffix("$") + else if (!sym.is(Method)) base.stripSuffix(" ") + else base + } { constant => + constant.stringValue + } + } + + /** Gets the fully qualified JS name of a static class of module Symbol. + * + * This is the JS name of the symbol qualified by the fully qualified JS + * name of its original owner if the latter is a native JS object. + */ + def fullJSNameOf(sym: Symbol)(implicit ctx: Context): String = { + assert(sym.isClass, s"fullJSNameOf called for non-class symbol $sym") + sym.getAnnotation(jsdefn.JSFullNameAnnot).flatMap(_.argumentConstant(0)).fold { + jsNameOf(sym) + } { constant => + constant.stringValue + } + } + +} diff --git a/src/dotty/tools/backend/sjs/JSPositions.scala b/src/dotty/tools/backend/sjs/JSPositions.scala new file mode 100644 index 000000000000..10570da00129 --- /dev/null +++ b/src/dotty/tools/backend/sjs/JSPositions.scala @@ -0,0 +1,65 @@ +package dotty.tools.backend.sjs + +import dotty.tools.dotc.core._ +import Contexts._ +import dotty.tools.dotc.util.Positions +import Positions.Position + +import org.scalajs.core.ir + +/** Conversion utilities from dotty Positions to IR Positions. */ +class JSPositions()(implicit ctx: Context) { + + /** Implicit conversion from dotty Position to ir.Position. */ + implicit def pos2irPos(pos: Positions.Position): ir.Position = { + if (!pos.exists) ir.Position.NoPosition + else { + val source = pos2irPosCache.toIRSource(ctx.compilationUnit.source) + val sourcePos = ctx.compilationUnit.source.atPos(pos) + // dotty positions are 1-based but IR positions are 0-based + ir.Position(source, sourcePos.line-1, sourcePos.column-1) + } + } + + /** Implicitly materializes an ir.Position from an implicit dotty Position. */ + implicit def implicitPos2irPos( + implicit pos: Positions.Position): ir.Position = { + pos2irPos(pos) + } + + private[this] object pos2irPosCache { // scalastyle:ignore + import dotty.tools.dotc.util._ + + private[this] var lastDotcSource: SourceFile = null + private[this] var lastIRSource: ir.Position.SourceFile = null + + def toIRSource(dotcSource: SourceFile): ir.Position.SourceFile = { + if (dotcSource != lastDotcSource) { + lastIRSource = convert(dotcSource) + lastDotcSource = dotcSource + } + lastIRSource + } + + private[this] def convert(dotcSource: SourceFile): ir.Position.SourceFile = { + dotcSource.file.file match { + case null => + new java.net.URI( + "virtualfile", // Pseudo-Scheme + dotcSource.file.path, // Scheme specific part + null // Fragment + ) + case file => + val srcURI = file.toURI + def matches(pat: java.net.URI) = pat.relativize(srcURI) != srcURI + + // TODO + /*scalaJSOpts.sourceURIMaps.collectFirst { + case ScalaJSOptions.URIMap(from, to) if matches(from) => + val relURI = from.relativize(srcURI) + to.fold(relURI)(_.resolve(relURI)) + } getOrElse*/ srcURI + } + } + } +} diff --git a/src/dotty/tools/backend/sjs/JSPrimitives.scala b/src/dotty/tools/backend/sjs/JSPrimitives.scala new file mode 100644 index 000000000000..6c3c5715cac9 --- /dev/null +++ b/src/dotty/tools/backend/sjs/JSPrimitives.scala @@ -0,0 +1,118 @@ +package dotty.tools.backend.sjs + +import dotty.tools.dotc.core._ +import Names.TermName +import StdNames._ +import Types._ +import Contexts._ +import Symbols._ + +import dotty.tools.dotc.ast.tpd._ +import dotty.tools.dotc.backend.jvm.DottyPrimitives + +import scala.collection.mutable + +object JSPrimitives { + + final val GETCLASS = 301 // jl.Object.getClass() + + final val F2JS = 302 // js.Any.fromFunctionN + final val F2JSTHIS = 303 // js.ThisFunction.fromFunctionN + + final val DYNNEW = 304 // js.Dynamic.newInstance + final val DYNLIT = 305 // js.Dynamic.literal.applyDynamic{,Named} + final val DICT_DEL = 306 // js.Dictionary.delete + final val ARR_CREATE = 307 // js.Array.apply (array literal syntax) + + final val TYPEOF = 308 // js.typeOf(x) + final val DEBUGGER = 309 // js.debugger() + final val HASPROP = 310 // js.Object.hasProperty(o, p), equiv to `p in o` in JS + final val OBJPROPS = 311 // js.Object.properties(o), equiv to `for (p in o)` in JS + final val JS_NATIVE = 312 // js.native. Marker method. Fails if tried to be emitted. + + final val UNITVAL = 313 // () value, which is undefined + final val UNITTYPE = 314 // BoxedUnit.TYPE (== classOf[Unit]) + + final val CONSTRUCTOROF = 315 // runtime.constructorOf(clazz) + final val ENV_INFO = 316 // runtime.environmentInfo + final val LINKING_INFO = 317 // runtime.linkingInfo + + final val THROW = 318 // .throw + +} + +class JSPrimitives(ctx: Context) extends DottyPrimitives(ctx) { + import JSPrimitives._ + import scala.tools.nsc.backend.ScalaPrimitives._ + + private lazy val jsPrimitives: Map[Symbol, Int] = initJSPrimitives(ctx) + + override def getPrimitive(sym: Symbol): Int = + jsPrimitives.getOrElse(sym, super.getPrimitive(sym)) + + override def getPrimitive(app: Apply, tpe: Type)(implicit ctx: Context): Int = + jsPrimitives.getOrElse(app.fun.symbol, super.getPrimitive(app, tpe)) + + override def isPrimitive(fun: Tree): Boolean = + jsPrimitives.contains(fun.symbol(ctx)) || super.isPrimitive(fun) + + /** Initialize the primitive map */ + private def initJSPrimitives(implicit ctx: Context): Map[Symbol, Int] = { + + val primitives = new mutable.HashMap[Symbol, Int]() + + // !!! Code duplicate with DottyPrimitives + /** Add a primitive operation to the map */ + def addPrimitive(s: Symbol, code: Int): Unit = { + assert(!(primitives contains s), "Duplicate primitive " + s) + primitives(s) = code + } + + def addPrimitives(cls: Symbol, method: TermName, code: Int)(implicit ctx: Context): Unit = { + val alts = cls.info.member(method).alternatives.map(_.symbol) + if (alts.isEmpty) { + ctx.error(s"Unknown primitive method $cls.$method") + } else { + for (s <- alts) + addPrimitive(s, code) + } + } + + val jsdefn = JSDefinitions.jsdefn + + addPrimitive(defn.Any_getClass, GETCLASS) + + for (i <- 0 to 22) + addPrimitive(jsdefn.JSAny_fromFunction(i), F2JS) + for (i <- 1 to 22) + addPrimitive(jsdefn.JSThisFunction_fromFunction(i), F2JSTHIS) + + addPrimitive(jsdefn.JSDynamic_newInstance, DYNNEW) + + addPrimitive(jsdefn.JSDynamicLiteral_applyDynamicNamed, DYNLIT) + addPrimitive(jsdefn.JSDynamicLiteral_applyDynamic, DYNLIT) + + addPrimitive(jsdefn.JSDictionary_delete, DICT_DEL) + + //addPrimitive(jsdefn.JSArray_create, ARR_CREATE) + + addPrimitive(jsdefn.JSPackage_typeOf, TYPEOF) + addPrimitive(jsdefn.JSPackage_debugger, DEBUGGER) + addPrimitive(jsdefn.JSPackage_native, JS_NATIVE) + + addPrimitive(jsdefn.JSObject_hasProperty, HASPROP) + addPrimitive(jsdefn.JSObject_properties, OBJPROPS) + + addPrimitive(defn.BoxedUnit_UNIT, UNITVAL) + //addPrimitive(defn.BoxedUnit_TYPE, UNITTYPE) + + //addPrimitive(jsdefn.Runtime_constructorOf, CONSTRUCTOROF) + //addPrimitive(jsdefn.Runtime_environmentInfo, ENV_INFO) + //addPrimitive(jsdefn.Runtime_linkingInfo, LINKING_INFO) + + addPrimitive(defn.throwMethod, THROW) + + primitives.toMap + } + +} diff --git a/src/dotty/tools/backend/sjs/ScopedVar.scala b/src/dotty/tools/backend/sjs/ScopedVar.scala new file mode 100644 index 000000000000..0e47f7b7946b --- /dev/null +++ b/src/dotty/tools/backend/sjs/ScopedVar.scala @@ -0,0 +1,38 @@ +package dotty.tools.backend.sjs + +import language.implicitConversions + +class ScopedVar[A](init: A) { + import ScopedVar.Assignment + + private var value = init + + def this()(implicit ev: Null <:< A) = this(ev(null)) + + def get: A = value + def :=(newValue: A): Assignment[A] = new Assignment(this, newValue) +} + +object ScopedVar { + class Assignment[T](scVar: ScopedVar[T], value: T) { + private[ScopedVar] def push(): AssignmentStackElement[T] = { + val stack = new AssignmentStackElement(scVar, scVar.value) + scVar.value = value + stack + } + } + + private class AssignmentStackElement[T](scVar: ScopedVar[T], oldValue: T) { + private[ScopedVar] def pop(): Unit = { + scVar.value = oldValue + } + } + + implicit def toValue[T](scVar: ScopedVar[T]): T = scVar.get + + def withScopedVars[T](ass: Assignment[_]*)(body: => T): T = { + val stack = ass.map(_.push()) + try body + finally stack.reverse.foreach(_.pop()) + } +} diff --git a/src/dotty/tools/dotc/Bench.scala b/src/dotty/tools/dotc/Bench.scala index 2e0e15e83d58..56b6dabbe4ca 100644 --- a/src/dotty/tools/dotc/Bench.scala +++ b/src/dotty/tools/dotc/Bench.scala @@ -8,37 +8,26 @@ package dotc import core.Contexts.Context import reporting.Reporter -/* To do: - * - simplify hk types - * - have a second look at normalization (leave at method types if pt is method type?) - * - Don't open package objects from class files if they are present in source - * - Revise the way classes are inherited - when not followed by [...] or (...), - * assume the unparameterized type and forward type parameters as we do now for the synthetic head class. +/** A main class for running compiler benchmarks. Can instantiate a given + * number of compilers and run each (sequentially) a given number of times + * on the same sources. */ object Bench extends Driver { - def resident(compiler: Compiler): Reporter = unsupported("resident") /*loop { line => - val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError)) - compiler.reporter.reset() - new compiler.Run() compile command.files - }*/ - private var numRuns = 1 + @sharable private var numRuns = 1 - def newCompiler(): Compiler = new Compiler + def newCompiler(implicit ctx: Context): Compiler = new Compiler private def ntimes(n: Int)(op: => Reporter): Reporter = (emptyReporter /: (0 until n)) ((_, _) => op) override def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter = - if (new config.Settings.Setting.SettingDecorator[Boolean](ctx.base.settings.resident).value(ctx)) - resident(compiler) - else - ntimes(numRuns) { - val start = System.nanoTime() - val r = super.doCompile(compiler, fileNames) - println(s"time elapsed: ${(System.nanoTime - start) / 1000000}ms") - r - } + ntimes(numRuns) { + val start = System.nanoTime() + val r = super.doCompile(compiler, fileNames) + println(s"time elapsed: ${(System.nanoTime - start) / 1000000}ms") + r + } def extractNumArg(args: Array[String], name: String, default: Int = 1): (Int, Array[String]) = { val pos = args indexOf name diff --git a/src/dotty/tools/dotc/CompilationUnit.scala b/src/dotty/tools/dotc/CompilationUnit.scala index 6f05b831f865..16a59250bb7f 100644 --- a/src/dotty/tools/dotc/CompilationUnit.scala +++ b/src/dotty/tools/dotc/CompilationUnit.scala @@ -2,7 +2,7 @@ package dotty.tools package dotc import dotty.tools.dotc.core.Types.Type -import dotty.tools.dotc.core.tasty.{TastyBuffer, TastyPickler} +import dotty.tools.dotc.core.tasty.{TastyUnpickler, TastyBuffer, TastyPickler} import util.SourceFile import ast.{tpd, untpd} import dotty.tools.dotc.core.Symbols._ @@ -23,4 +23,6 @@ class CompilationUnit(val source: SourceFile) { * Subsequent phases can add new sections. */ var picklers: Map[ClassSymbol, TastyPickler] = Map() + + var unpicklers: Map[ClassSymbol, TastyUnpickler] = Map() } diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala index 4899454a2f8b..b63e0236df00 100644 --- a/src/dotty/tools/dotc/Compiler.scala +++ b/src/dotty/tools/dotc/Compiler.scala @@ -5,18 +5,22 @@ import core._ import Contexts._ import Periods._ import Symbols._ +import Types._ import Scopes._ -import typer.{FrontEnd, Typer, Mode, ImportInfo, RefChecks} -import reporting.ConsoleReporter +import typer.{FrontEnd, Typer, ImportInfo, RefChecks} +import reporting.{Reporter, ConsoleReporter} import Phases.Phase -import dotty.tools.dotc.transform._ -import dotty.tools.dotc.transform.TreeTransforms.{TreeTransform, TreeTransformer} -import dotty.tools.dotc.core.DenotTransformers.DenotTransformer -import dotty.tools.dotc.core.Denotations.SingleDenotation - +import transform._ +import transform.TreeTransforms.{TreeTransform, TreeTransformer} +import core.DenotTransformers.DenotTransformer +import core.Denotations.SingleDenotation import dotty.tools.backend.jvm.{LabelDefs, GenBCode} +import dotty.tools.backend.sjs.GenSJSIR +/** The central class of the dotc compiler. The job of a compiler is to create + * runs, which process given `phases` in a given `rootContext`. + */ class Compiler { /** Meta-ordering constraint: @@ -37,44 +41,56 @@ class Compiler { */ def phases: List[List[Phase]] = List( - List(new FrontEnd), - List(new PostTyper), - List(new Pickler), - List(new FirstTransform), - List(new RefChecks, - new ElimRepeated, - new NormalizeFlags, - new ExtensionMethods, - new ExpandSAMs, - new TailRec), - List(new PatternMatcher, - new ExplicitOuter, - new Splitter), - List(new SeqLiterals, - new InterceptedMethods, - new Literalize, - new Getters, - new ElimByName, - new ResolveSuper), - List(new Erasure), - List(new ElimErasedValueType, - new VCInline, - new Mixin, - new LazyVals, - new Memoize, - new CapturedVars, // capturedVars has a transformUnit: no phases should introduce local mutable vars here - new Constructors, - new FunctionalInterfaces), - List(new LambdaLift, // in this mini-phase block scopes are incorrect. No phases that rely on scopes should be here - new Flatten, - new RestoreScopes), - List(/*new PrivateToStatic,*/ - new ExpandPrivate, - new CollectEntryPoints, - new LabelDefs, - new ElimWildcardIdents, - new TraitConstructors), - List(new GenBCode) + List(new FrontEnd), // Compiler frontend: scanner, parser, namer, typer + List(new PostTyper), // Additional checks and cleanups after type checking + List(new Pickler), // Generate TASTY info + List(new FirstTransform, // Some transformations to put trees into a canonical form + new CheckReentrant), // Internal use only: Check that compiled program has no data races involving global vars + List(new RefChecks, // Various checks mostly related to abstract members and overriding + new CheckStatic, // Check restrictions that apply to @static members + new ElimRepeated, // Rewrite vararg parameters and arguments + new NormalizeFlags, // Rewrite some definition flags + new ExtensionMethods, // Expand methods of value classes with extension methods + new ExpandSAMs, // Expand single abstract method closures to anonymous classes + new TailRec, // Rewrite tail recursion to loops + new LiftTry, // Put try expressions that might execute on non-empty stacks into their own methods + new ClassOf), // Expand `Predef.classOf` calls. + List(new PatternMatcher, // Compile pattern matches + new ExplicitOuter, // Add accessors to outer classes from nested ones. + new ExplicitSelf, // Make references to non-trivial self types explicit as casts + new CrossCastAnd, // Normalize selections involving intersection types. + new Splitter), // Expand selections involving union types into conditionals + List(new VCInlineMethods, // Inlines calls to value class methods + new SeqLiterals, // Express vararg arguments as arrays + new InterceptedMethods, // Special handling of `==`, `|=`, `getClass` methods + new Getters, // Replace non-private vals and vars with getter defs (fields are added later) + new ElimByName, // Expand by-name parameters and arguments + new AugmentScala2Traits, // Expand traits defined in Scala 2.11 to simulate old-style rewritings + new ResolveSuper, // Implement super accessors and add forwarders to trait methods + new ArrayConstructors), // Intercept creation of (non-generic) arrays and intrinsify. + List(new Erasure), // Rewrite types to JVM model, erasing all type parameters, abstract types and refinements. + List(new ElimErasedValueType, // Expand erased value types to their underlying implmementation types + new VCElideAllocations, // Peep-hole optimization to eliminate unnecessary value class allocations + new Mixin, // Expand trait fields and trait initializers + new LazyVals, // Expand lazy vals + new Memoize, // Add private fields to getters and setters + new LinkScala2ImplClasses, // Forward calls to the implementation classes of traits defined by Scala 2.11 + new NonLocalReturns, // Expand non-local returns + new CapturedVars, // Represent vars captured by closures as heap objects + new Constructors, // Collect initialization code in primary constructors + // Note: constructors changes decls in transformTemplate, no InfoTransformers should be added after it + new FunctionalInterfaces,// Rewrites closures to implement @specialized types of Functions. + new GetClass), // Rewrites getClass calls on primitive types. + List(new LambdaLift, // Lifts out nested functions to class scope, storing free variables in environments + // Note: in this mini-phase block scopes are incorrect. No phases that rely on scopes should be here + new ElimStaticThis, // Replace `this` references to static objects by global identifiers + new Flatten, // Lift all inner classes to package scope + new RestoreScopes), // Repair scopes rendered invalid by moving definitions in prior phases of the group + List(new ExpandPrivate, // Widen private definitions accessed from nested classes + new CollectEntryPoints, // Find classes with main methods + new LabelDefs), // Converts calls to labels to jumps + List(new GenSJSIR), // Generate .js code + List(new GenBCode) // Generate JVM bytecode ) var runId = 1 @@ -91,8 +107,21 @@ class Compiler { * imports For each element of RootImports, an import context */ def rootContext(implicit ctx: Context): Context = { - ctx.definitions.init(ctx) - ctx.setPhasePlan(phases) + ctx.initialize()(ctx) + val actualPhases = if (ctx.settings.scalajs.value) { + // Remove phases that Scala.js does not want + phases.mapConserve(_.filter { + case _: FunctionalInterfaces => false + case _ => true + }).filter(_.nonEmpty) + } else { + // Remove Scala.js-related phases + phases.mapConserve(_.filter { + case _: GenSJSIR => false + case _ => true + }).filter(_.nonEmpty) + } + ctx.setPhasePlan(actualPhases) val rootScope = new MutableScope val bootstrap = ctx.fresh .setPeriod(Period(nextRunId, FirstPhaseId)) @@ -102,10 +131,10 @@ class Compiler { .setOwner(defn.RootClass) .setTyper(new Typer) .setMode(Mode.ImplicitsEnabled) - .setTyperState(new MutableTyperState(ctx.typerState, new ConsoleReporter()(ctx), isCommittable = true)) - ctx.definitions.init(start) // set context of definitions to start - def addImport(ctx: Context, symf: () => Symbol) = - ctx.fresh.setImportInfo(ImportInfo.rootImport(symf)(ctx)) + .setTyperState(new MutableTyperState(ctx.typerState, ctx.typerState.reporter, isCommittable = true)) + ctx.initialize()(start) // re-initialize the base context with start + def addImport(ctx: Context, refFn: () => TermRef) = + ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx)) (start.setRunInfo(new RunInfo(start)) /: defn.RootImportFns)(addImport) } diff --git a/src/dotty/tools/dotc/Driver.scala b/src/dotty/tools/dotc/Driver.scala index dc1431acada3..2e78854c1405 100644 --- a/src/dotty/tools/dotc/Driver.scala +++ b/src/dotty/tools/dotc/Driver.scala @@ -1,44 +1,134 @@ package dotty.tools.dotc +import dotty.tools.FatalError import config.CompilerCommand import core.Contexts.{Context, ContextBase} import util.DotClass import reporting._ import scala.util.control.NonFatal +/** Run the Dotty compiler. + * + * Extending this class lets you customize many aspect of the compilation + * process, but in most cases you only need to call [[process]] on the + * existing object [[Main]]. + */ abstract class Driver extends DotClass { - val prompt = "\ndotc>" + protected def newCompiler(implicit ctx: Context): Compiler - protected def newCompiler(): Compiler - - protected def emptyReporter: Reporter = new StoreReporter + protected def emptyReporter: Reporter = new StoreReporter(null) protected def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter = - if (fileNames.nonEmpty) { - val run = compiler.newRun - run.compile(fileNames) - run.printSummary() - } else emptyReporter + if (fileNames.nonEmpty) + try { + val run = compiler.newRun + run.compile(fileNames) + run.printSummary() + } + catch { + case ex: FatalError => + ctx.error(ex.getMessage) // signals that we should fail compilation. + ctx.reporter + } + else emptyReporter protected def initCtx = (new ContextBase).initialCtx - def process(args: Array[String], rootCtx: Context): Reporter = { - val summary = CompilerCommand.distill(args)(rootCtx) - implicit val ctx: Context = initCtx.fresh.setSettings(summary.sstate) - val fileNames = CompilerCommand.checkUsage(summary) - try { - doCompile(newCompiler(), fileNames) - } catch { - case ex: FatalError => - ctx.error(ex.getMessage) // signals that we should fail compilation. - ctx.typerState.reporter - } + protected def sourcesRequired = true + + def setup(args: Array[String], rootCtx: Context): (List[String], Context) = { + val ctx = rootCtx.fresh + val summary = CompilerCommand.distill(args)(ctx) + ctx.setSettings(summary.sstate) + val fileNames = CompilerCommand.checkUsage(summary, sourcesRequired)(ctx) + (fileNames, ctx) } - def main(args: Array[String]): Unit = - sys.exit(if (process(args, initCtx).hasErrors) 1 else 0) -} + /** Entry point to the compiler that can be conveniently used with Java reflection. + * + * This entry point can easily be used without depending on the `dotty` package, + * you only need to depend on `dotty-interfaces` and call this method using + * reflection. This allows you to write code that will work against multiple + * versions of dotty without recompilation. + * + * The trade-off is that you can only pass a SimpleReporter to this method + * and not a normal Reporter which is more powerful. + * + * Usage example: [[https://github.com/lampepfl/dotty/tree/master/test/test/InterfaceEntryPointTest.scala]] + * + * @param args Arguments to pass to the compiler. + * @param simple Used to log errors, warnings, and info messages. + * The default reporter is used if this is `null`. + * @param callback Used to execute custom code during the compilation + * process. No callbacks will be executed if this is `null`. + * @return + */ + final def process(args: Array[String], simple: interfaces.SimpleReporter, + callback: interfaces.CompilerCallback): interfaces.ReporterResult = { + val reporter = if (simple == null) null else Reporter.fromSimpleReporter(simple) + process(args, reporter, callback) + } + + /** Principal entry point to the compiler. + * + * Usage example: [[https://github.com/lampepfl/dotty/tree/master/test/test/OtherEntryPointsTest.scala]] + * in method `runCompiler` + * + * @param args Arguments to pass to the compiler. + * @param reporter Used to log errors, warnings, and info messages. + * The default reporter is used if this is `null`. + * @param callback Used to execute custom code during the compilation + * process. No callbacks will be executed if this is `null`. + * @return The `Reporter` used. Use `Reporter#hasErrors` to check + * if compilation succeeded. + */ + final def process(args: Array[String], reporter: Reporter = null, + callback: interfaces.CompilerCallback = null): Reporter = { + val ctx = initCtx.fresh + if (reporter != null) + ctx.setReporter(reporter) + if (callback != null) + ctx.setCompilerCallback(callback) + process(args, ctx) + } + + /** Entry point to the compiler with no optional arguments. + * + * This overload is provided for compatibility reasons: the + * `RawCompiler` of sbt expects this method to exist and calls + * it using reflection. Keeping it means that we can change + * the other overloads without worrying about breaking compatibility + * with sbt. + */ + final def process(args: Array[String]): Reporter = + process(args, null: Reporter, null: interfaces.CompilerCallback) -class FatalError(msg: String) extends Exception + /** Entry point to the compiler using a custom `Context`. + * + * In most cases, you do not need a custom `Context` and should + * instead use one of the other overloads of `process`. However, + * the other overloads cannot be overriden, instead you + * should override this one which they call internally. + * + * Usage example: [[https://github.com/lampepfl/dotty/tree/master/test/test/OtherEntryPointsTest.scala]] + * in method `runCompilerWithContext` + * + * @param args Arguments to pass to the compiler. + * @param rootCtx The root Context to use. + * @return The `Reporter` used. Use `Reporter#hasErrors` to check + * if compilation succeeded. + */ + def process(args: Array[String], rootCtx: Context): Reporter = { + val (fileNames, ctx) = setup(args, rootCtx) + doCompile(newCompiler(ctx), fileNames)(ctx) + } + def main(args: Array[String]): Unit = { + // Preload scala.util.control.NonFatal. Otherwise, when trying to catch a StackOverflowError, + // we may try to load it but fail with another StackOverflowError and lose the original exception, + // see . + val _ = NonFatal + sys.exit(if (process(args).hasErrors) 1 else 0) + } +} diff --git a/src/dotty/tools/dotc/FromTasty.scala b/src/dotty/tools/dotc/FromTasty.scala index 019846396277..05e97f30a5de 100644 --- a/src/dotty/tools/dotc/FromTasty.scala +++ b/src/dotty/tools/dotc/FromTasty.scala @@ -12,6 +12,7 @@ import SymDenotations._ import typer.FrontEnd import Phases.Phase import util._ +import reporting.Reporter import Decorators._ import dotty.tools.dotc.transform.Pickler import tasty.DottyUnpickler @@ -29,7 +30,7 @@ import ast.tpd._ * scala dotty.tools.dotc.FromTasty -Xprint:front extMethods.T */ object FromTasty extends Driver { - override def newCompiler(): Compiler = new TASTYCompiler + override def newCompiler(implicit ctx: Context): Compiler = new TASTYCompiler class TASTYCompiler extends Compiler { @@ -63,6 +64,9 @@ object FromTasty extends Driver { } class ReadTastyTreesFromClasses extends FrontEnd { + + override def isTyper = false + override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = units.map(readTASTY) @@ -82,9 +86,10 @@ object FromTasty extends Driver { case info: ClassfileLoader => info.load(clsd) match { case Some(unpickler: DottyUnpickler) => - val (List(unpickled), source) = unpickler.body(readPositions = true) - val unit1 = new CompilationUnit(source) + val List(unpickled) = unpickler.body(readPositions = true) + val unit1 = new CompilationUnit(new SourceFile(clsd.symbol.sourceFile, Seq())) unit1.tpdTree = unpickled + unit1.unpicklers += (clsd.classSymbol -> unpickler.unpickler) force.traverse(unit1.tpdTree) unit1 case _ => diff --git a/src/dotty/tools/dotc/Main.scala b/src/dotty/tools/dotc/Main.scala index 0b136d72f7b9..a6844fbbc4c0 100644 --- a/src/dotty/tools/dotc/Main.scala +++ b/src/dotty/tools/dotc/Main.scala @@ -1,28 +1,9 @@ -/* NSC -- new Scala compiler - * Copyright 2005-2013 LAMP/EPFL - * @author Martin Odersky - */ package dotty.tools package dotc import core.Contexts.Context -import reporting.Reporter -/* To do: - */ +/** Main class of the `dotc` batch compiler. */ object Main extends Driver { - def resident(compiler: Compiler): Reporter = unsupported("resident") /*loop { line => - val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError)) - compiler.reporter.reset() - new compiler.Run() compile command.files - }*/ - - override def newCompiler(): Compiler = new Compiler - - override def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter = { - if (new config.Settings.Setting.SettingDecorator[Boolean](ctx.base.settings.resident).value(ctx)) - resident(compiler) - else - super.doCompile(compiler, fileNames) - } + override def newCompiler(implicit ctx: Context): Compiler = new Compiler } diff --git a/src/dotty/tools/dotc/Resident.scala b/src/dotty/tools/dotc/Resident.scala new file mode 100644 index 000000000000..56f6684d0964 --- /dev/null +++ b/src/dotty/tools/dotc/Resident.scala @@ -0,0 +1,58 @@ +package dotty.tools +package dotc + +import core.Contexts.Context +import reporting.Reporter +import java.io.EOFException +import scala.annotation.tailrec + +/** A compiler which stays resident between runs. This is more of a PoC than + * something that's expected to be used often + * + * Usage: + * + * > scala dotty.tools.dotc.Resident + * + * dotc> "more options and files to compile" + * + * ... + * + * dotc> :reset // reset all options to the ones passed on the command line + * + * ... + * + * dotc> :q // quit + */ +class Resident extends Driver { + + object residentCompiler extends Compiler + + override def newCompiler(implicit ctx: Context): Compiler = ??? + + override def sourcesRequired = false + + private val quit = ":q" + private val reset = ":reset" + private val prompt = "dotc> " + + private def getLine() = { + Console.print(prompt) + try scala.io.StdIn.readLine() catch { case _: EOFException => quit } + } + + final override def process(args: Array[String], rootCtx: Context): Reporter = { + @tailrec def loop(args: Array[String], prevCtx: Context): Reporter = { + var (fileNames, ctx) = setup(args, prevCtx) + doCompile(residentCompiler, fileNames)(ctx) + var nextCtx = ctx + var line = getLine() + while (line == reset) { + nextCtx = rootCtx + line = getLine() + } + if (line.startsWith(quit)) ctx.reporter + else loop(line split "\\s+", nextCtx) + } + loop(args, rootCtx) + } +} diff --git a/src/dotty/tools/dotc/Run.scala b/src/dotty/tools/dotc/Run.scala index 151288d23709..928a59214b6b 100644 --- a/src/dotty/tools/dotc/Run.scala +++ b/src/dotty/tools/dotc/Run.scala @@ -8,10 +8,12 @@ import io.PlainFile import util.{SourceFile, NoSource, Stats, SimpleMap} import reporting.Reporter import transform.TreeChecker +import rewrite.Rewrites import java.io.{BufferedWriter, OutputStreamWriter} import scala.reflect.io.VirtualFile import scala.util.control.NonFatal +/** A compiler run. Exports various methods to compile source files */ class Run(comp: Compiler)(implicit ctx: Context) { assert(comp.phases.last.last.id <= Periods.MaxPossiblePhaseId) @@ -33,14 +35,14 @@ class Run(comp: Compiler)(implicit ctx: Context) { compileSources(sources) } catch { case NonFatal(ex) => - println(i"exception occurred while compiling $units%, %") + ctx.echo(i"exception occurred while compiling $units%, %") throw ex } - /** TODO: There's a fundamental design problem here: We assmble phases using `squash` + /** TODO: There's a fundamental design problem here: We assemble phases using `squash` * when we first build the compiler. But we modify them with -Yskip, -Ystop - * on each run. That modification needs to either trasnform the tree structure, - * or we need to assmeble phases on each run, and take -Yskip, -Ystop into + * on each run. That modification needs to either transform the tree structure, + * or we need to assemble phases on each run, and take -Yskip, -Ystop into * account. I think the latter would be preferable. */ def compileSources(sources: List[SourceFile]) = @@ -50,27 +52,30 @@ class Run(comp: Compiler)(implicit ctx: Context) { } protected def compileUnits() = Stats.monitorHeartBeat { + ctx.checkSingleThreaded() val phases = ctx.squashPhases(ctx.phasePlan, ctx.settings.Yskip.value, ctx.settings.YstopBefore.value, ctx.settings.YstopAfter.value, ctx.settings.Ycheck.value) ctx.usePhases(phases) for (phase <- ctx.allPhases) if (!ctx.reporter.hasErrors) { - if (ctx.settings.verbose.value) println(s"[$phase]") + val start = System.currentTimeMillis units = phase.runOn(units) def foreachUnit(op: Context => Unit)(implicit ctx: Context): Unit = for (unit <- units) op(ctx.fresh.setPhase(phase.next).setCompilationUnit(unit)) if (ctx.settings.Xprint.value.containsPhase(phase)) foreachUnit(printTree) + ctx.informTime(s"$phase ", start) } + if (!ctx.reporter.hasErrors) Rewrites.writeBack() } private def printTree(ctx: Context) = { val unit = ctx.compilationUnit val prevPhase = ctx.phase.prev // can be a mini-phase - val squahsedPhase = ctx.squashed(prevPhase) + val squashedPhase = ctx.squashed(prevPhase) - println(s"result of $unit after ${squahsedPhase}:") - println(unit.tpdTree.show(ctx)) + ctx.echo(s"result of $unit after ${squashedPhase}:") + ctx.echo(unit.tpdTree.show(ctx)) } def compile(sourceCode: String): Unit = { @@ -81,10 +86,13 @@ class Run(comp: Compiler)(implicit ctx: Context) { compileSources(List(new SourceFile(virtualFile))) } + /** The context created for this run */ + def runContext = ctx + /** Print summary; return # of errors encountered */ def printSummary(): Reporter = { ctx.runInfo.printMaxConstraint() - val r = ctx.typerState.reporter + val r = ctx.reporter r.printSummary r } diff --git a/src/dotty/tools/dotc/ast/Desugar.scala b/src/dotty/tools/dotc/ast/Desugar.scala index f7904abc06c2..12c1e65a6756 100644 --- a/src/dotty/tools/dotc/ast/Desugar.scala +++ b/src/dotty/tools/dotc/ast/Desugar.scala @@ -9,7 +9,6 @@ import Decorators._ import language.higherKinds import collection.mutable.ListBuffer import config.Printers._ -import typer.Mode object desugar { @@ -21,6 +20,13 @@ object desugar { /** Info of a variable in a pattern: The named tree and its type */ private type VarInfo = (NameTree, Tree) + /** Names of methods that are added unconditionally to case classes */ + def isDesugaredCaseClassMethodName(name: Name)(implicit ctx: Context) = + name == nme.isDefined || + name == nme.copy || + name == nme.productArity || + name.isSelectorName + // ----- DerivedTypeTrees ----------------------------------- class SetterParamTree extends DerivedTypeTree { @@ -36,11 +42,15 @@ object desugar { /** Make sure that for all enclosing module classes their companion lasses * are completed. Reason: We need the constructor of such companion classes to * be completed so that OriginalSymbol attachments are pushed to DerivedTypeTrees - * in appy/unapply methods. + * in apply/unapply methods. */ override def ensureCompletions(implicit ctx: Context) = if (!(ctx.owner is Package)) - if (ctx.owner is ModuleClass) ctx.owner.linkedClass.ensureCompleted() + if (ctx.owner.isClass) { + ctx.owner.ensureCompleted() + if (ctx.owner is ModuleClass) + ctx.owner.linkedClass.ensureCompleted() + } else ensureCompletions(ctx.outer) /** Return info of original symbol, where all references to siblings of the @@ -60,7 +70,7 @@ object desugar { val defctx = ctx.outersIterator.dropWhile(_.scope eq ctx.scope).next var local = defctx.denotNamed(tp.name).suchThat(_ is ParamOrAccessor).symbol if (local.exists) (defctx.owner.thisType select local).dealias - else throw new Error(s"no matching symbol for ${sym.showLocated} in ${defctx.owner} / ${defctx.effectiveScope}") + else throw new Error(s"no matching symbol for ${tp.symbol.showLocated} in ${defctx.owner} / ${defctx.effectiveScope}") case _ => mapOver(tp) } @@ -118,11 +128,11 @@ object desugar { * * Expand default arguments to default getters. E.g, * - * def f(x: Int = 1)(y: String = x + "m") = ... + * def f[T: B](x: Int = 1)(y: String = x + "m") = ... * ==> - * def f(x: Int)(y: String) = ... - * def f$default$1 = 1 - * def f$default$2(x: Int) = x + "m" + * def f[T](x: Int)(y: String)(implicit evidence$0: B[T]) = ... + * def f$default$1[T] = 1 + * def f$default$2[T](x: Int) = x + "m" */ def defDef(meth: DefDef, isPrimaryConstructor: Boolean = false)(implicit ctx: Context): Tree = { val DefDef(name, tparams, vparamss, tpt, rhs) = meth @@ -132,7 +142,7 @@ object desugar { case tparam @ TypeDef(_, ContextBounds(tbounds, cxbounds)) => for (cxbound <- cxbounds) { val paramFlags: FlagSet = if (isPrimaryConstructor) PrivateLocalParamAccessor else Param - val epname = (nme.EVIDENCE_PARAM_PREFIX.toString + epbuf.length).toTermName + val epname = ctx.freshName(nme.EVIDENCE_PARAM_PREFIX).toTermName epbuf += ValDef(epname, cxbound, EmptyTree).withFlags(paramFlags | Implicit) } cpy.TypeDef(tparam)(rhs = tbounds) @@ -140,18 +150,7 @@ object desugar { tparam } - val meth1 = epbuf.toList match { - case Nil => - meth - case evidenceParams => - val vparamss1 = vparamss.reverse match { - case (vparams @ (vparam :: _)) :: rvparamss if vparam.mods is Implicit => - ((vparams ++ evidenceParams) :: rvparamss).reverse - case _ => - vparamss :+ evidenceParams - } - cpy.DefDef(meth)(tparams = tparams1, vparamss = vparamss1) - } + val meth1 = addEvidenceParams(cpy.DefDef(meth)(tparams = tparams1), epbuf.toList) /** The longest prefix of parameter lists in vparamss whose total length does not exceed `n` */ def takeUpTo(vparamss: List[List[ValDef]], n: Int): List[List[ValDef]] = vparamss match { @@ -162,19 +161,24 @@ object desugar { Nil } - def normalizedVparamss = vparamss map (_ map (vparam => + def normalizedVparamss = meth1.vparamss map (_ map (vparam => cpy.ValDef(vparam)(rhs = EmptyTree))) + def dropContextBound(tparam: TypeDef) = tparam.rhs match { + case ContextBounds(tbounds, _) => cpy.TypeDef(tparam)(rhs = tbounds) + case _ => tparam + } + def defaultGetters(vparamss: List[List[ValDef]], n: Int): List[DefDef] = vparamss match { case (vparam :: vparams) :: vparamss1 => def defaultGetter: DefDef = DefDef( name = meth.name.defaultGetterName(n), - tparams = meth.tparams map toDefParam, + tparams = meth.tparams.map(tparam => dropContextBound(toDefParam(tparam))), vparamss = takeUpTo(normalizedVparamss, n), tpt = TypeTree(), rhs = vparam.rhs - ).withMods(vparam.mods & AccessFlags) + ).withMods(Modifiers(mods.flags & AccessFlags, mods.privateWithin)) val rest = defaultGetters(vparams :: vparamss1, n + 1) if (vparam.rhs.isEmpty) rest else defaultGetter :: rest case Nil :: vparamss1 => @@ -192,6 +196,30 @@ object desugar { } } + // Add all evidence parameters in `params` as implicit parameters to `meth` */ + private def addEvidenceParams(meth: DefDef, params: List[ValDef])(implicit ctx: Context): DefDef = + params match { + case Nil => + meth + case evidenceParams => + val vparamss1 = meth.vparamss.reverse match { + case (vparams @ (vparam :: _)) :: rvparamss if vparam.mods is Implicit => + ((vparams ++ evidenceParams) :: rvparamss).reverse + case _ => + meth.vparamss :+ evidenceParams + } + cpy.DefDef(meth)(vparamss = vparamss1) + } + + /** The implicit evidence parameters of `meth`, as generated by `desugar.defDef` */ + private def evidenceParams(meth: DefDef)(implicit ctx: Context): List[ValDef] = + meth.vparamss.reverse match { + case (vparams @ (vparam :: _)) :: _ if vparam.mods is Implicit => + vparams.dropWhile(!_.name.startsWith(nme.EVIDENCE_PARAM_PREFIX)) + case _ => + Nil + } + /** Fill in empty type bounds with Nothing/Any. Expand private local type parameters as follows: * * class C[v T] @@ -203,23 +231,24 @@ object desugar { val tparam = cpy.TypeDef(tdef)(name = tdef.name.expandedName(ctx.owner)) .withMods(tdef.mods &~ PrivateLocal | ExpandedName) val alias = cpy.TypeDef(tdef)(rhs = refOfDef(tparam), tparams = Nil) - .withFlags(PrivateLocalParamAccessor | Synthetic | tdef.mods.flags & VarianceFlags) + .withMods(tdef.mods & VarianceFlags | PrivateLocalParamAccessor | Synthetic) Thicket(tparam, alias) } else tdef } - private val synthetic = Modifiers(Synthetic) + @sharable private val synthetic = Modifiers(Synthetic) private def toDefParam(tparam: TypeDef): TypeDef = - tparam.withFlags(Param) + tparam.withMods(tparam.rawMods & EmptyFlags | Param) private def toDefParam(vparam: ValDef): ValDef = - vparam.withFlags(Param | vparam.rawMods.flags & Implicit) + vparam.withMods(vparam.rawMods & Implicit | Param) /** The expansion of a class definition. See inline comments for what is involved */ def classDef(cdef: TypeDef)(implicit ctx: Context): Tree = { val TypeDef(name, impl @ Template(constr0, parents, self, _)) = cdef val mods = cdef.mods + val companionMods = mods.withFlags((mods.flags & AccessFlags).toCommonFlags) val (constr1, defaultGetters) = defDef(constr0, isPrimaryConstructor = true) match { case meth: DefDef => (meth, Nil) @@ -231,20 +260,39 @@ object desugar { // prefixed by type or val). `tparams` and `vparamss` are the type parameters that // go in `constr`, the constructor after desugaring. + /** Does `tree' look like a reference to AnyVal? Temporary test before we have inline classes */ + def isAnyVal(tree: Tree): Boolean = tree match { + case Ident(tpnme.AnyVal) => true + case Select(qual, tpnme.AnyVal) => isScala(qual) + case _ => false + } + def isScala(tree: Tree): Boolean = tree match { + case Ident(nme.scala_) => true + case Select(Ident(nme.ROOTPKG), nme.scala_) => true + case _ => false + } + + val isCaseClass = mods.is(Case) && !mods.is(Module) + val isValueClass = parents.nonEmpty && isAnyVal(parents.head) + // This is not watertight, but `extends AnyVal` will be replaced by `inline` later. + val constrTparams = constr1.tparams map toDefParam val constrVparamss = if (constr1.vparamss.isEmpty) { // ensure parameter list is non-empty - if (mods is Case) + if (isCaseClass) ctx.error("case class needs to have at least one parameter list", cdef.pos) ListOfNil } else constr1.vparamss.nestedMap(toDefParam) val constr = cpy.DefDef(constr1)(tparams = constrTparams, vparamss = constrVparamss) - // Add constructor type parameters to auxiliary constructors + // Add constructor type parameters and evidence implicit parameters + // to auxiliary constructors val normalizedBody = impl.body map { case ddef: DefDef if ddef.name.isConstructorName => - cpy.DefDef(ddef)(tparams = constrTparams) + addEvidenceParams( + cpy.DefDef(ddef)(tparams = constrTparams), + evidenceParams(constr1).map(toDefParam)) case stat => stat } @@ -282,7 +330,7 @@ object desugar { // neg/t1843-variances.scala for a test case. The test would give // two errors without @uncheckedVariance, one of them spurious. val caseClassMeths = - if (mods is Case) { + if (isCaseClass) { def syntheticProperty(name: TermName, rhs: Tree) = DefDef(name, Nil, Nil, TypeTree(), rhs).withMods(synthetic) val isDefinedMeth = syntheticProperty(nme.isDefined, Literal(Constant(true))) @@ -298,6 +346,7 @@ object desugar { case ValDef(_, tpt, _) => isRepeated(tpt) case _ => false }) + val copyMeths = if (mods.is(Abstract) || hasRepeatedParam) Nil // cannot have default arguments for repeated parameters, hence copy method is not issued else { @@ -316,14 +365,14 @@ object desugar { def anyRef = ref(defn.AnyRefAlias.typeRef) def productConstr(n: Int) = { - val tycon = ref(defn.ProductNClass(n).typeRef) + val tycon = scalaDot((tpnme.Product.toString + n).toTypeName) val targs = constrVparamss.head map (_.tpt) if (targs.isEmpty) tycon else AppliedTypeTree(tycon, targs) } - // Case classes get a ProductN parent + // Case classes and case objects get a ProductN parent var parents1 = parents - if ((mods is Case) && arity <= Definitions.MaxTupleArity) + if (mods.is(Case) && arity <= Definitions.MaxTupleArity) parents1 = parents1 :+ productConstr(arity) // The thicket which is the desugared version of the companion object @@ -332,10 +381,10 @@ object desugar { moduleDef( ModuleDef( name.toTermName, Template(emptyConstructor, parentTpt :: Nil, EmptyValDef, defs)) - .withMods(synthetic)) + .withMods(companionMods | Synthetic)) .withPos(cdef.pos).toList - // The companion object defifinitions, if a companion is needed, Nil otherwise. + // The companion object definitions, if a companion is needed, Nil otherwise. // companion definitions include: // 1. If class is a case class case class C[Ts](p1: T1, ..., pN: TN)(moreParams): // def apply[Ts](p1: T1, ..., pN: TN)(moreParams) = new C[Ts](p1, ..., pN)(moreParams) (unless C is abstract) @@ -345,7 +394,7 @@ object desugar { // (T11, ..., T1N) => ... => (TM1, ..., TMN) => C // For all other classes, the parent is AnyRef. val companions = - if (mods is Case) { + if (isCaseClass) { val parent = if (constrTparams.nonEmpty || constrVparamss.length > 1 || @@ -357,7 +406,7 @@ object desugar { if (mods is Abstract) Nil else DefDef(nme.apply, derivedTparams, derivedVparamss, TypeTree(), creatorExpr) - .withMods(synthetic | (constr1.mods.flags & DefaultParameterized)) :: Nil + .withFlags(Synthetic | (constr1.mods.flags & DefaultParameterized)) :: Nil val unapplyMeth = { val unapplyParam = makeSyntheticParameter(tpt = classTypeRef) val unapplyRHS = if (arity == 0) Literal(Constant(true)) else Ident(unapplyParam.name) @@ -367,7 +416,9 @@ object desugar { companionDefs(parent, applyMeths ::: unapplyMeth :: defaultGetters) } else if (defaultGetters.nonEmpty) - companionDefs(anyRef, defaultGetters) + companionDefs(anyRef, defaultGetters) + else if (isValueClass) + companionDefs(anyRef, Nil) else Nil @@ -375,18 +426,22 @@ object desugar { // synthetic implicit C[Ts](p11: T11, ..., p1N: T1N) ... (pM1: TM1, ..., pMN: TMN): C[Ts] = // new C[Ts](p11, ..., p1N) ... (pM1, ..., pMN) = val implicitWrappers = - if (mods is Implicit) { - if (ctx.owner is Package) - ctx.error("implicit classes may not be toplevel", cdef.pos) - if (mods is Case) - ctx.error("implicit classes may not case classes", cdef.pos) - + if (!mods.is(Implicit)) + Nil + else if (ctx.owner is Package) { + ctx.error("implicit classes may not be toplevel", cdef.pos) + Nil + } + else if (isCaseClass) { + ctx.error("implicit classes may not be case classes", cdef.pos) + Nil + } + else // implicit wrapper is typechecked in same scope as constructor, so // we can reuse the constructor parameters; no derived params are needed. DefDef(name.toTermName, constrTparams, constrVparamss, classTypeRef, creatorExpr) - .withFlags(Synthetic | Implicit) :: Nil - } - else Nil + .withMods(companionMods | Synthetic | Implicit) + .withPos(cdef.pos) :: Nil val self1 = { val selfType = if (self.tpt.isEmpty) classTypeRef else self.tpt @@ -398,7 +453,7 @@ object desugar { val originalTparams = constr1.tparams.toIterator val originalVparams = constr1.vparamss.toIterator.flatten val tparamAccessors = derivedTparams.map(_.withMods(originalTparams.next.mods)) - val caseAccessor = if (mods is Case) CaseAccessor else EmptyFlags + val caseAccessor = if (isCaseClass) CaseAccessor else EmptyFlags val vparamAccessors = derivedVparamss.flatten.map(_.withMods(originalVparams.next.mods | caseAccessor)) cpy.TypeDef(cdef)( rhs = cpy.Template(impl)(constr, parents1, self1, @@ -434,7 +489,7 @@ object desugar { val clsName = name.moduleClassName val clsRef = Ident(clsName) val modul = ValDef(name, clsRef, New(clsRef, Nil)) - .withMods(mods | ModuleCreationFlags) + .withMods(mods | ModuleCreationFlags | mods.flags & AccessFlags) .withPos(mdef.pos) val ValDef(selfName, selfTpt, _) = tmpl.self val selfMods = tmpl.self.mods @@ -444,7 +499,7 @@ object desugar { .withPos(tmpl.self.pos orElse tmpl.pos.startPos) val clsTmpl = cpy.Template(tmpl)(self = clsSelf, body = tmpl.body) val cls = TypeDef(clsName, clsTmpl) - .withMods(mods.toTypeFlags & AccessOrSynthetic | ModuleClassCreationFlags) + .withMods(mods.toTypeFlags & RetainedModuleClassFlags | ModuleClassCreationFlags) Thicket(modul, classDef(cls)) } } @@ -461,18 +516,18 @@ object desugar { /** If `pat` is a variable pattern, * - * val/var p = e + * val/var/lazy val p = e * * Otherwise, in case there is exactly one variable x_1 in pattern - * val/var p = e ==> val/var x_1 = (e: @unchecked) match (case p => (x_1)) + * val/var/lazy val p = e ==> val/var/lazy val x_1 = (e: @unchecked) match (case p => (x_1)) * * in case there are zero or more than one variables in pattern - * val/var p = e ==> private synthetic val t$ = (e: @unchecked) match (case p => (x_1, ..., x_N)) - * val/var x_1 = t$._1 + * val/var/lazy p = e ==> private synthetic [lazy] val t$ = (e: @unchecked) match (case p => (x_1, ..., x_N)) + * val/var/def x_1 = t$._1 * ... - * val/var x_N = t$._N + * val/var/def x_N = t$._N * If the original pattern variable carries a type annotation, so does the corresponding - * ValDef. + * ValDef or DefDef. */ def makePatDef(mods: Modifiers, pat: Tree, rhs: Tree)(implicit ctx: Context): Tree = pat match { case VarPattern(named, tpt) => @@ -496,16 +551,26 @@ object desugar { derivedValDef(named, tpt, matchExpr, mods) case _ => val tmpName = ctx.freshName().toTermName - val patFlags = PrivateLocal | Synthetic | (mods.flags & Lazy) - val firstDef = ValDef(tmpName, TypeTree(), matchExpr).withFlags(patFlags) + val patMods = mods & (AccessFlags | Lazy) | Synthetic + val firstDef = + ValDef(tmpName, TypeTree(), matchExpr) + .withPos(pat.pos.union(rhs.pos)).withMods(patMods) def selector(n: Int) = Select(Ident(tmpName), nme.selectorName(n)) val restDefs = for (((named, tpt), n) <- vars.zipWithIndex) - yield derivedValDef(named, tpt, selector(n), mods) + yield + if (mods is Lazy) derivedDefDef(named, tpt, selector(n), mods &~ Lazy) + else derivedValDef(named, tpt, selector(n), mods) flatTree(firstDef :: restDefs) } } + /** Expand variable identifier x to x @ _ */ + def patternVar(tree: Tree)(implicit ctx: Context) = { + val Ident(name) = tree + Bind(name, Ident(nme.WILDCARD)).withPos(tree.pos) + } + def defTree(tree: Tree)(implicit ctx: Context): Tree = tree match { case tree: ValDef => valDef(tree) case tree: TypeDef => if (tree.isClassDef) classDef(tree) else typeDef(tree) @@ -547,24 +612,56 @@ object desugar { DefDef(nme.ANON_FUN, Nil, params :: Nil, tpt, body).withMods(synthetic), Closure(Nil, Ident(nme.ANON_FUN), EmptyTree)) - /** Expand partial function + /** If `nparams` == 1, expand partial function + * * { cases } * ==> - * x$0 => x$0 match { cases } + * x$1 => x$1 match { cases } + * + * If `nparams` != 1, expand instead to + * + * (x$1, ..., x$n) => (x$0, ..., x${n-1}) match { cases } */ - def makeCaseLambda(cases: List[CaseDef])(implicit ctx: Context) = { + def makeCaseLambda(cases: List[CaseDef], nparams: Int = 1)(implicit ctx: Context) = { + val params = (1 to nparams).toList.map(makeSyntheticParameter(_)) + val selector = makeTuple(params.map(p => Ident(p.name))) + Function(params, Match(selector, cases)) + } + + /** Map n-ary function `(p1, ..., pn) => body` where n != 1 to unary function as follows: + * + * x$1 => { + * def p1 = x$1._1 + * ... + * def pn = x$1._n + * body + * } + */ + def makeTupledFunction(params: List[ValDef], body: Tree)(implicit ctx: Context): Tree = { val param = makeSyntheticParameter() - Function(param :: Nil, Match(Ident(param.name), cases)) + def selector(n: Int) = Select(refOfDef(param), nme.selectorName(n)) + val vdefs = + params.zipWithIndex.map{ + case (param, idx) => + DefDef(param.name, Nil, Nil, TypeTree(), selector(idx)).withPos(param.pos) + } + Function(param :: Nil, Block(vdefs, body)) } /** Add annotation with class `cls` to tree: * tree @cls */ def makeAnnotated(cls: Symbol, tree: Tree)(implicit ctx: Context) = - Annotated(TypedSplice(tpd.New(cls.typeRef, Nil)), tree) + Annotated(untpd.New(untpd.TypeTree(cls.typeRef), Nil), tree) + + private def derivedValDef(named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit ctx: Context) = { + val vdef = ValDef(named.name.asTermName, tpt, rhs).withMods(mods).withPos(named.pos) + val mayNeedSetter = valDef(vdef) + mayNeedSetter + } - private def derivedValDef(named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers) = - ValDef(named.name.asTermName, tpt, rhs).withMods(mods).withPos(named.pos) + private def derivedDefDef(named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers) = + DefDef(named.name.asTermName, Nil, Nil, tpt, rhs).withMods(mods).withPos(named.pos) /** Main desugaring method */ def apply(tree: Tree)(implicit ctx: Context): Tree = { @@ -598,8 +695,8 @@ object desugar { } } - /** Create tree for for-comprehension or - * where mapName and flatMapName are chosen + /** Create tree for for-comprehension `` or + * `` where mapName and flatMapName are chosen * corresponding to whether this is a for-do or a for-yield. * The creation performs the following rewrite rules: * @@ -638,7 +735,7 @@ object desugar { * TupleN(x_1, ..., x_N) * } ...) * - * If any of the P_i are variable patterns, the corresponding `x_i @ P_i' is not generated + * If any of the P_i are variable patterns, the corresponding `x_i @ P_i` is not generated * and the variable constituting P_i is used instead of x_i * * @param mapName The name to be used for maps (either map or foreach) @@ -771,7 +868,7 @@ object desugar { tree match { case SymbolLit(str) => Apply( - Select(ref(defn.SymbolClass.companionModule.termRef), nme.apply), + ref(defn.SymbolClass.companionModule.termRef), Literal(Constant(str)) :: Nil) case InterpolatedString(id, strs, elems) => Apply(Select(Apply(Ident(nme.StringContext), strs), id), elems) @@ -786,10 +883,10 @@ object desugar { makeBinop(l, op, r) case PostfixOp(t, op) => if ((ctx.mode is Mode.Type) && op == nme.raw.STAR) { - val seqClass = if (ctx.compilationUnit.isJava) defn.ArrayClass else defn.SeqClass + val seqType = if (ctx.compilationUnit.isJava) defn.ArrayType else defn.SeqType Annotated( - New(ref(defn.RepeatedAnnot.typeRef), Nil :: Nil), - AppliedTypeTree(ref(seqClass.typeRef), t)) + New(ref(defn.RepeatedAnnotType), Nil :: Nil), + AppliedTypeTree(ref(seqType), t)) } else { assert(ctx.mode.isExpr || ctx.reporter.hasErrors, ctx.mode) Select(t, op) @@ -801,22 +898,22 @@ object desugar { case Tuple(ts) => if (unboxedPairs) { def PairTypeTree(l: Tree, r: Tree) = - AppliedTypeTree(ref(defn.PairClass.typeRef), l :: r :: Nil) + AppliedTypeTree(ref(defn.PairType), l :: r :: Nil) if (ctx.mode is Mode.Type) ts.reduceRight(PairTypeTree) else if (ts.isEmpty) unitLiteral else ts.reduceRight(Pair(_, _)) } else { val arity = ts.length - def tupleClass = defn.TupleClass(arity) + def tupleTypeRef = defn.TupleType(arity) if (arity > Definitions.MaxTupleArity) { ctx.error(s"tuple too long (max allowed: ${Definitions.MaxTupleArity})", tree.pos) unitLiteral } else if (arity == 1) ts.head - else if (ctx.mode is Mode.Type) AppliedTypeTree(ref(tupleClass.typeRef), ts) + else if (ctx.mode is Mode.Type) AppliedTypeTree(ref(tupleTypeRef), ts) else if (arity == 0) unitLiteral - else Apply(ref(tupleClass.companionModule.valRef), ts) + else Apply(ref(tupleTypeRef.classSymbol.companionModule.valRef), ts) } case WhileDo(cond, body) => // {