diff --git a/_zh-cn/overviews/scala3-contribution/arch-context.md b/_zh-cn/overviews/scala3-contribution/arch-context.md new file mode 100644 index 0000000000..af025ac47c --- /dev/null +++ b/_zh-cn/overviews/scala3-contribution/arch-context.md @@ -0,0 +1,52 @@ +--- +title: Contexts +type: section +description: 本页介绍了Scala 3编译器中的symbols。 +num: 14 +previous-page: arch-lifecycle +next-page: arch-phases +language: zh-cn +--- + +`dotc`几乎没有全局状态(除了name表,它将字符串散列成唯一的name)。相反,所有可能在编译器[run]中发生变化的重要信息都被收集在一个`Context`中(在[Contexts]中定义)。 + +编译器中的大多数方法都依赖于一个隐式的匿名`Context`参数。一个典型的定义如下: +```scala +import dotty.tools.dotc.Contexts.{Context, ctx} + +def doFoo(using Context): Unit = + val current = ctx.run // 使用ctx访问Context +``` + +## 内存泄露 +> **小心:** 上下文可能会很重,所以要注意内存泄漏 + +良好的做法是确保隐式上下文不被捕获在闭包或其他长期存在的对象中,以避免在一个闭包可以在编译器运行几次后仍然存在的情况下出现空间泄漏(例如,为一个永远不需要的库类提供服务的lazy completer)。 +在这种情况下,惯例是将`Context`作为一个显式参数,以跟踪其使用情况。 + +## 上下文属性 + +| 上下文属性 | 定义 | +|-------------------|----------------------------------------| +| `compilationUnit` | current compilation unit | +| `phase` | current phase | +| `run` | current run | +| `period` | current period | +| `settings` | the config passed to the compiler | +| `reporter` | operations for logging errors/warnings | +| `definitions` | the standard built in definitions | +| `platform` | operations for the underlying platform | +| `tree` | current tree | +| `scope` | current scope | +| `typer` | current typer | +| `owner` | current owner symbol | +| `outer` | outer Context | +| `mode` | type checking mode | +| `typerState` | | +| `searchHistory` | | +| `implicits` | | +| ... | and so on | + + +[Contexts]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/core/Contexts.scala +[run]: {% link _overviews/scala3-contribution/arch-lifecycle.md %}#runs diff --git a/_zh-cn/overviews/scala3-contribution/arch-lifecycle.md b/_zh-cn/overviews/scala3-contribution/arch-lifecycle.md new file mode 100644 index 0000000000..7e1b95d249 --- /dev/null +++ b/_zh-cn/overviews/scala3-contribution/arch-lifecycle.md @@ -0,0 +1,86 @@ +--- +title: 编译器概述 +type: section +description: 本页面描述了Scala 3编译器的生命周期。 +num: 13 +previous-page: arch-intro +next-page: arch-context +language: zh-cn +--- + +在高层次上,`dotc` 是一个交互式编译器(见[what is a compiler?][whats-a-compiler]),可以经常调用,例如回答IDE的问题,提供REPL补全,或管理增量构建等等。 +这些用例中的每一个都需要一个定制的工作流程,但它们共享一个共同的核心。 + +## 编译器的生命周期简介 + +#### Core + +定制是通过扩展[Compiler]类来提供的,该类维护一个有序的[phases][Phases]列表,以及如何[run][Run]它们。 +与编译器的每一次交互都会创建一个新的运行,这是编译器的phases在输入源列表上的一次完整迭代。 +每次运行都可以创建新的定义或使旧的定义失效,`dotc`可以[track these changes over time][time]。 + +#### Runs +在运行过程中,输入源被转换为[compilation units][CompilationUnit](即与每个输入源相关的编译器状态的抽象);然后迭代:在进入下一阶段之前,对每个compilation unit应用一个phase。 + +#### Phases +一个phase是对一个compilation unit的抽象转换,它通常负责转换代表源文件的代码的树和类型。编译器的一些phase有: +- `parser`, 它将符合Scala[syntax]的文本转换为抽象语法树,即ASTs +- `typer`,检查树是否符合预期类型 +- `erasure`, 它将一个更简化的程序重新类型化,使其具有与JVM相同的类型。 +- `genBCode`, JVM后端,它将擦除的编译器树转换为Java字节码格式。 + +[You can read more about phases here][phase-categories]. + +#### Drivers + +核心编译器还需要在使用前初始化很多状态,比如[settings][ScalaSettings]和[Context][contexts]。 +为了方便,[Driver]类包含了配置编译器和以编程方式调用编译器的高级函数。对象[Main]继承自`Driver`,由`scalac`脚本调用。 + +## Code Structure + +编译器的代码可以在包[dotty.tools]中找到,包含以下子包。 +```scala +tools // 包含帮助程序和`scala`通用运行器 +├── backend // 编译器后端 (当前是 JVM 和 JS) +├── dotc // 主要的编译器,有子包: +│ ├── ast // 抽象语法树 +│   ├── classpath +│   ├── config // 编译器配置、设置、平台特定定义 +│   ├── core // 核心数据结构和操作,有特定的子包用于: +│   │   ├── classfile // 将Java类文件读入核心数据结构 +│   │   ├── tasty // 读写TASTY文件,与核心数据结构之间的转换 +│   │   └── unpickleScala2 // 将Scala2符号信息读入核心数据结构中 +│   ├── decompiler // 将TASTY当做代码漂亮的打印 +│   ├── fromtasty // 用于从TASTY重新编译的驱动器 +│   ├── interactive // 交互式编译器和代码补全 +│   ├── parsing // 扫描和解析器 +│   ├── plugins // 编辑插件定义 +│   ├── printing // 漂亮地打印树、类型和其他数据 +│   ├── profile // 对编译器进行分析的内部程序 +│   ├── quoted // quoted反射的内部构件 +│   ├── reporting // 报告错误信息、警告和其他信息 +│   ├── rewrites // 用于将Scala 2的结构重写成Scala 3的结构的帮助工具 +│   ├── sbt // 用于与Zinc编译器通信的帮助程序. +│   ├── semanticdb // 用于从树中导出semanticdb的帮助工具 +│   ├── transform // 用于树转换的Miniphases和帮助工具 +│   ├── typer // 类型检查 +│   └── util // 通用的工具类和模块 +├── io // 用于文件访问和classpath处理的帮助模块 +├── repl // REPL驱动程序和与终端的交互 +├── runner // `scala`通用运行脚本的帮助工具 +└── scripting // scala运行器的-script参数 +``` + +[whats-a-compiler]: {% link _overviews/scala3-contribution/contribution-intro.md %}#what-is-a-compiler +[Phases]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/core/Phases.scala +[CompilationUnit]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/CompilationUnit.scala +[time]: {% link _overviews/scala3-contribution/arch-time.md %} +[dotty.tools]: https://github.com/lampepfl/dotty/tree/master/compiler/src/dotty/tools +[ScalaSettings]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +[phase-categories]: {% link _overviews/scala3-contribution/arch-phases.md %}#phase-categories +[syntax]: {{ site.scala3ref }}/syntax.html +[Main]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Main.scala +[Driver]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Driver.scala +[Compiler]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Compiler.scala +[Run]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Run.scala +[contexts]: {% link _overviews/scala3-contribution/arch-context.md %}