Skip to content

Commit

Permalink
Updated comments / TODO's
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchiecarroll committed Oct 5, 2024
1 parent ed31602 commit 8cd49d6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src/go2cs2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ var keywords = NewHashSet[string]([]string{
*/

func main() {
// TODO: Add option to process an entire package (all files in a directory)
if len(os.Args) < 2 {
log.Fatalln("Usage: go run main.go <input.go> [output.cs]")
}
Expand All @@ -133,8 +134,14 @@ func main() {
}

fset := token.NewFileSet()

files := []*ast.File{}

// TODO: Handle option to parse all files in the package (dir)
file, err := parser.ParseFile(fset, inputFileName, nil, parser.ParseComments|parser.SkipObjectResolution)

files = append(files, file)

if err != nil {
log.Fatalf("Failed to parse input source file \"%s\": %s\n", inputFileName, err)
}
Expand All @@ -151,7 +158,7 @@ func main() {
Uses: make(map[*ast.Ident]types.Object),
}

pkg, err := conf.Check(".", fset, []*ast.File{file}, info)
pkg, err := conf.Check(".", fset, files, info)

if err != nil {
log.Fatalf("Failed to parse types from input source file \"%s\": %s\n", inputFileName, err)
Expand Down Expand Up @@ -198,6 +205,12 @@ func main() {
identEscapesHeap: map[*ast.Ident]bool{},
}

// TODO: To consider a package as a whole, all files in the package should
// be parsed and processed. Since global variables could be defined in any
// file in the package, we need to process `performGlobalVariableAnalysis`
// for all files in the package before further processing files with the
// `visitFile` function.

visitor.visitFile(file)

outputFile.WriteString(visitor.targetFile.String())
Expand Down Expand Up @@ -426,7 +439,10 @@ func (v *Visitor) getStringLiteral(str string) (result string, isRawStr bool) {
}

func getSanitizedIdentifier(identifier string) string {
if keywords.Contains(identifier) || strings.HasPrefix(identifier, AddressPrefix) || strings.HasSuffix(identifier, ClassSuffix) {
if keywords.Contains(identifier) ||
strings.HasPrefix(identifier, AddressPrefix) ||
strings.HasSuffix(identifier, ClassSuffix) ||
strings.Contains(identifier, ShadowVarMarker) {
return "@" + identifier
}

Expand Down
12 changes: 10 additions & 2 deletions src/go2cs2/performEscapeAnalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import (
// scope and thus needs to be heap allocated. This is important for C# code generation
// since Go allows variables to escape the current scope automatically, adding them to
// the heap, behind the scenes. C# does not have this feature, so we need to manually
// determine if a variable needs to be heap allocated.
// determine if a variable needs to be heap allocated. The map that is created as a
// result of this analysis is called `identEscapesHeap`.

// Implementation of the escape analysis is currently very basic and only covers cases
// within a single function considering options where a variable "may" escape. It does
// not consider cases where a variable is passed to another function and may not need
// to be heap allocated and could be handled by using C# ref structure operations.
func (v *Visitor) performEscapeAnalysis(ident *ast.Ident, parentBlock *ast.BlockStmt) {
if parentBlock == nil {
return
Expand Down Expand Up @@ -71,7 +76,10 @@ func (v *Visitor) performEscapeAnalysis(ident *ast.Ident, parentBlock *ast.Block
// Check if ident is used in an address-of operation
if n.Op == token.AND {
if containsIdent(n.X) {
// The address of the ident is taken
// The address of the ident is taken, simple response
// is to just assume it escapes. Future iterations
// may be able to provide more nuance and use C# ref
// structure operations to avoid heap allocation
escapes = true
return false
}
Expand Down
3 changes: 2 additions & 1 deletion src/go2cs2/performVariableAnalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
// in the Go source code. The analysis is performed on global variables and on a per-function
// basis. The goal is to identify reassignments and shadowed variables so these can be handled
// correctly during the conversion process since C# does not allow redeclarations or shadowing
// of variables with the same name.
// of variables with the same name. The two derivative functions that use the results of this
// analysis are `getIdentName` and `isReassignment`.

// Perform variable analysis on the global ValueSpec declarations
func (v *Visitor) performGlobalVariableAnalysis(decls []ast.Decl) {
Expand Down
1 change: 1 addition & 0 deletions src/go2cs2/visitFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (v *Visitor) visitFile(file *ast.File) {
v.writeOutputLn(UsingsMarker)
v.writeOutputLn("public static %spartial class %s%s {", UnsafeMarker, file.Name.Name, ClassSuffix)

// TODO: Move processing of global variables to pre-visit operations
v.performGlobalVariableAnalysis(file.Decls)

for _, decl := range file.Decls {
Expand Down

0 comments on commit 8cd49d6

Please sign in to comment.