-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_decl.go
57 lines (53 loc) · 1.35 KB
/
func_decl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ast
import (
"github.com/dcaiafa/nitro/internal/stub/analysis"
"github.com/dcaiafa/nitro/internal/token"
)
type FuncDecl struct {
Doc *Doc
ID token.Token
Params ASTs
Rets ASTs
}
func (d *FuncDecl) RunPass(ctx *Context, pass Pass) {
ctx.RunPassChild(d, d.Params, pass)
ctx.RunPassChild(d, d.Rets, pass)
if pass == Check {
sig := new(analysis.Signature)
for i, paramAST := range d.Params {
paramAST := paramAST.(*FuncParam)
param := analysis.Param{
Name: paramAST.ID.Str,
Type: paramAST.Type.Type,
}
if paramAST.DefaultValue != nil {
param.HasDefault = true
param.DefaultExpr = paramAST.DefaultValue.Expr
} else if len(sig.Params) > 0 && sig.Params[len(sig.Params)-1].HasDefault {
ctx.Failf(token.Pos{}, "Only the last parameters are allowed to have default values")
return
}
if paramAST.VarArg {
if i != len(d.Params)-1 {
ctx.Failf(token.Pos{}, "Only the last parameter can be vararg")
return
}
param.VarArg = true
}
sig.Params = append(sig.Params, param)
}
for _, retAST := range d.Rets {
retAST := retAST.(*TypeRef)
sig.Rets = append(sig.Rets, retAST.Type)
}
fun := &analysis.Func{
Name: d.ID.Str,
Signatures: []*analysis.Signature{sig},
}
err := ctx.Analysis.AddFunc(fun)
if err != nil {
ctx.Failf(token.Pos{}, "%v", err)
return
}
}
}