compiler: allow to use local variables in init()

Because body of multiple `init()` functions constitute single
method in contract, we initialize slot with maximum amount of
locals encounterered in any of `init()` functions and clear them
before emitting body of each instance of `init()`.
This commit is contained in:
Evgenii Stratonikov 2020-10-06 18:25:40 +03:00
parent 2d9ef9219a
commit 6701e8cda0
4 changed files with 58 additions and 27 deletions

View file

@ -152,10 +152,10 @@ func (c *funcScope) analyzeVoidCalls(node ast.Node) bool {
return true
}
func (c *funcScope) countLocals() int {
func countLocals(decl *ast.FuncDecl) (int, bool) {
size := 0
hasDefer := false
ast.Inspect(c.decl, func(n ast.Node) bool {
ast.Inspect(decl, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.FuncType:
num := n.Results.NumFields()
@ -186,6 +186,11 @@ func (c *funcScope) countLocals() int {
}
return true
})
return size, hasDefer
}
func (c *funcScope) countLocals() int {
size, hasDefer := countLocals(c.decl)
if hasDefer {
c.finallyProcessedIndex = size
size++