gpt4 book ai didi

scala - 为什么 Scala 的编译器使用 "-print"选项显示 Scala 的特定功能?

转载 作者:行者123 更新时间:2023-12-01 08:35:30 25 4
gpt4 key购买 nike

Scala 编译器 (2.9.2) 的帮助菜单说

-print   Print program with Scala-specific features removed.

但以下带有 -print 选项的调用显示了 Scala 特有的功能:

C:\Users\John\Test Scala Project\src\main\scala>type test.scala
trait A

C:\Users\John\Test Scala Project\src\main\scala>scalac -print test.scala
[[syntax trees at end of cleanup]]// Scala source: test.scala
package <empty> {
abstract trait A extends java.lang.Object
}

为什么仍然显示特征?我本来期望纯 Java 代码。

最佳答案

描述确实是骗人的,但它从未说过它会打印 Java 代码。无论如何它都做不到:Scala 生成有效的字节码,但不能生成可以直接翻译成 Java 的字节码。

它的作用是在 cleanup 阶段之后生成代码。现在,如果你在 Scala 2.9.2 上尝试 -Xshow-phases,你会看到:

    phase name  id  description
---------- -- -----------
parser 1 parse source into ASTs, perform simple desugaring
namer 2 resolve names, attach symbols to named trees
packageobjects 3 load package objects
typer 4 the meat and potatoes: type the trees
superaccessors 5 add super accessors in traits and nested classes
pickler 6 serialize symbol tables
refchecks 7 reference/override checking, translate nested objects
selectiveanf 8
liftcode 9 reify trees
selectivecps 10
uncurry 11 uncurry, translate function values to anonymous classes
tailcalls 12 replace tail calls by jumps
specialize 13 @specialized-driven class and method specialization
explicitouter 14 this refs to outer pointers, translate patterns
erasure 15 erase types, add interfaces for traits
lazyvals 16 allocate bitmaps, translate lazy vals into lazified defs
lambdalift 17 move nested functions to top level
constructors 18 move field definitions into constructors
flatten 19 eliminate inner classes
mixin 20 mixin composition
cleanup 21 platform-specific cleanups, generate reflective calls
icode 22 generate portable intermediate code
inliner 23 optimization: do inlining
closelim 24 optimization: eliminate uncalled closures
dce 25 optimization: eliminate dead code
jvm 26 generate JVM bytecode
terminal 27 The last phase in the compiler chain

请注意,cleanupicode 之前的最后一个阶段,这才是重点。 -print 参数会在更改抽象语法树的所有内容都进行后打印内容。当 AST 即将准备好进行代码生成时。

关于scala - 为什么 Scala 的编译器使用 "-print"选项显示 Scala 的特定功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11235690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com