# 5.2 代码结构

> &#x20;兼容性说明：
>
> &#x20;在前言中提到 Go 的编译器团队当前正在重写类型检查系统，ir 包便是该过程中剥离出来的一部分，与 IR Tree 相关的代码后期应该都会移动到该包内。在老版本中，IR Tree 的生成与类型检查是同时完成的，新版本的实现做了隔离，在代码结构上更加清晰。
>
> &#x20;由于类型检查器的重写还没有完成，而 IR Tree 是很多后续处理的基础，因此现在 IR Tree 中依然关联了很多旧版本类型检查器的代码，主要包括目录 `cmd/compile/internal/types` 与 `cmd/compile/internal/typecheck` 中的代码。但我们并不需要将这些代码全部弄懂，只需要了解涉及到的一些基本的思路就可以继续学习下去，也许等到重写工作全部完成之后代码逻辑会更加清晰。

生成 IR Tree 的代码主要位于包 ir 中，在目录 `cmd/compile/internal/ir/` 下；还有部分在 `cmd/compile/internal/noder` 中。其中入口函数是 `cmd/compile/internal/noder/irgen.go` 中的 `irgen.generate()` ，ir 包是重写类型检查器时单独剥离出来的逻辑，因此该函数在新的类型检查函数 `check2()` 的最后阶段被调用。

所生成的 IR Tree 保存在全局变量 `typecheck.Target` 内，该变量定义在文件 `cmd/compile/internal/typecheck.target.go` 中，包含着当前被编译的包的所有信息，是所有后续操作的基础。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gocompiler.shizhz.me/golang-bian-yi-qi-ir-tree/2.-dai-ma-jie-gou.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
