Skip to content

Commit

Permalink
Initialize imported packages first
Browse files Browse the repository at this point in the history
  • Loading branch information
upamanyus committed Dec 10, 2024
1 parent 7d01afb commit ca441d6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions goose.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Ctx struct {
// Set of global variables in the package being translated.
globalVars map[string]glang.Type
globalVarsOrdered []string
importNames []string

inits []glang.Expr
}
Expand Down Expand Up @@ -2393,6 +2394,7 @@ func (ctx *Ctx) imports(d []ast.Spec) []glang.Decl {
// path. We can get this information by using the *types.Package
// returned by Check (or the pkg.Types field from *packages.Package).
decls = append(decls, glang.ImportDecl{Path: importPath})
ctx.importNames = append(ctx.importNames, ctx.info.PkgNameOf(s).Name())
}
return decls
}
Expand Down Expand Up @@ -2446,6 +2448,7 @@ func (ctx *Ctx) initFunctions() []glang.Decl {
defineFunc := glang.FuncDecl{Name: "define'"}
ctx.dep.setCurrentName("define'")
var e glang.Expr

if len(ctx.globalVarsOrdered) == 0 {
e = glang.DoExpr{Expr: glang.Tt}
} else {
Expand Down Expand Up @@ -2534,15 +2537,19 @@ func (ctx *Ctx) initFunctions() []glang.Decl {
}
}

// FIXME: initialize imported packages.

e = glang.NewDoSeq(glang.NewCallExpr(glang.GallinaIdent("define'"), glang.Tt), e)

for _, importName := range ctx.importNames {
e = glang.NewDoSeq(glang.GallinaIdent(importName+"."+"initialize'"), e)
}

e = glang.IfExpr{
Cond: glang.NewCallExpr(glang.GallinaIdent("globals.is_uninitialized"),
glang.GallinaIdent("global_id'")),
Then: e,
Else: glang.DoExpr{Expr: glang.Tt},
}

initFunc.Body = glang.NewCallExpr(glang.GallinaIdent("exception_do"), e)

return []glang.Decl{packageIdDecl, defineFunc, initFunc}
Expand Down

0 comments on commit ca441d6

Please sign in to comment.