| 1 |
This file describes some of the typecheckers internal organization and conventions. |
| 2 |
It is not meant to be complete; rather it is a living document that will be updated |
| 3 |
as needed. |
| 4 |
|
| 5 |
Read this file first before starting to make changes in the code. |
| 6 |
|
| 7 |
# |
| 8 |
### Overall organization |
| 9 |
|
| 10 |
There are two almost identical typecheckers: |
| 11 |
|
| 12 |
- cmd/compile/internal/types2 (or types2 for short) |
| 13 |
- go/types |
| 14 |
|
| 15 |
types2 is internal and used by the compiler. |
| 16 |
go/types is the std library typechecker and its API must remain strictly |
| 17 |
backward-compatible. |
| 18 |
The types2 API closely matches the go/types API but may not have some |
| 19 |
deprecated functions anymore (which we need to maintain in go/types). |
| 20 |
|
| 21 |
They differ primarily in what syntax tree they operate on: |
| 22 |
|
| 23 |
- types2 uses the syntax tree defined by cmd/compile/internal/syntax |
| 24 |
- go/types uses the syntax tree defined by go/ast |
| 25 |
|
| 26 |
We aim to keep the respective sources very closely in sync. |
| 27 |
**Any change will need to be made to both typechecker source bases**. |
| 28 |
|
| 29 |
Many go/types files can be generated automatically from the |
| 30 |
corresponding types2 sources. |
| 31 |
This is done via a generator (go/types/generate_test.go) which may be invoked via |
| 32 |
`go generate` in the go/types directory. |
| 33 |
Generated files are clearly marked with a comment at the top and should not |
| 34 |
be modified by hand. |
| 35 |
For this reason, it is usually best to make changes to the types2 sources first. |
| 36 |
The changes only need to be ported by hand for the go/types files that cannot |
| 37 |
be generated yet. |
| 38 |
|
| 39 |
New files may be added to the list of generated files by adding a respective |
| 40 |
entry to the table in generate_test.go (and possibly describing any necessary |
| 41 |
source transformations). |
| 42 |
|
| 43 |
In the following, examples and commands are based on types2 but usually apply |
| 44 |
directly to go/types. |
| 45 |
|
| 46 |
|
| 47 |
# |
| 48 |
### Tests |
| 49 |
|
| 50 |
There is a comprehensive suite of tests in the form of annotated source files. |
| 51 |
The tests are in: |
| 52 |
|
| 53 |
- src/internal/types/testdata/ (shared between go/types and types2) |
| 54 |
- ./testdata/local (typechecker local tests, for rare situations only) |
| 55 |
|
| 56 |
Tests are .go files annotated with `/* ERROR "msg" */` or `/* ERRORx "msg" */` |
| 57 |
comments (or the respective line comment form). |
| 58 |
For each such error comment, typechecking the respective file is expected to |
| 59 |
report an error at the position of the syntactic token _immediately preceding_ |
| 60 |
the comment. |
| 61 |
For `ERROR`, the `"msg"` string must be a substring of the error message |
| 62 |
reported by the typechecker; |
| 63 |
for `ERRORx`, the `"msg"` string must be a regular expresspion matching the |
| 64 |
reported error. |
| 65 |
|
| 66 |
For each issue #NNNN that is fixed in the typecheckers, a test |
| 67 |
should be added as src/internal/types/testdata/fixedbugs/issueNNNN.go. |
| 68 |
|
| 69 |
|
| 70 |
# |
| 71 |
### Debugging |
| 72 |
|
| 73 |
The pre-existing template ./testdata/manual.go is convenient for debugging |
| 74 |
on-off situations. Simply populate it with the code of interest and then |
| 75 |
run `go test -run Manual` which will typecheck that file. |
| 76 |
|
| 77 |
Useful debugging flags (together with `go test -run Manual`): |
| 78 |
|
| 79 |
- -halt (panic and produce a stack trace where the first error is reported) |
| 80 |
- -v (produce a typechecking trace) |
| 81 |
- -verify (verify `ERROR` comments in manual.go) |
| 82 |
|
| 83 |
|
| 84 |
# |
| 85 |
### Frequently used types and variables |
| 86 |
|
| 87 |
#### Checker |
| 88 |
|
| 89 |
File: check.go |
| 90 |
|
| 91 |
A `Checker` maintains all typechecking state relevant for typechecking a package. |
| 92 |
Typically the receiver type for typechecker methods. |
| 93 |
|
| 94 |
|
| 95 |
#### operand |
| 96 |
|
| 97 |
File: operand.go |
| 98 |
|
| 99 |
An `operand` describes the type and value (if any) of an expression. |
| 100 |
The `operandMode` describes the kind of expression (constant, variable, etc.). |
| 101 |
Operands are the primary result of typechecking an expression. |
| 102 |
If typechecking of an expression fails, the resulting operand has mode `invalid`. |
| 103 |
|
| 104 |
|
| 105 |
#### Typ |
| 106 |
|
| 107 |
File: universe.go |
| 108 |
|
| 109 |
The `Typ` array provides access to all predeclared basic types. |
| 110 |
`Typ[Invalid]` is used to denote an invalid type. |
| 111 |
|
| 112 |
|
| 113 |
# |
| 114 |
### Internal coding conventions |
| 115 |
|
| 116 |
#### Predicates |
| 117 |
|
| 118 |
File: predicates.go (commonly used predicates only) |
| 119 |
|
| 120 |
Predicates are typically named in form `isX`, such as `isInteger`. |
| 121 |
|
| 122 |
#### Type-checking expressions |
| 123 |
|
| 124 |
Typically, there is a Checker method for typechecking a particular expression. |
| 125 |
For instance, there is a method `Checker.unary` that typechecks unary expressions. |
| 126 |
The basic form of such a function f is as follows: |
| 127 |
``` |
| 128 |
func (check *Checker) f(x *operand, e syntax.Expr, /* addition arguments, if any */) |
| 129 |
``` |
| 130 |
The result of typechecking expression `e` is returned via the operand `x` |
| 131 |
(which sometimes also serves as incoming argument). |
| 132 |
If an error occurred the function f will report the error and try to continue |
| 133 |
as best as it can, but it may return an invalid operand (`x.mode == invalid`). |
| 134 |
Callers may need to explicitly check for invalid operands. |
| 135 |
|
| 136 |
|
| 137 |
# |
| 138 |
### TODO |
| 139 |
|
| 140 |
Add more relevant content. |