Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct scope problem #368

Open
Konstantin8105 opened this issue Nov 25, 2017 · 5 comments
Open

struct scope problem #368

Konstantin8105 opened this issue Nov 25, 2017 · 5 comments

Comments

@Konstantin8105
Copy link
Contributor

Konstantin8105 commented Nov 25, 2017

Let's look on next C example:

struct tt {
        int t;
};

void some(void){
        struct tt{
                char* t;
        };
        struct tt u;
        u.t = "Uuups";
}

int main(void){
        struct tt u;
        u.t = 5;
        return 0;
}

We have 2 structs with same names tt and different internal items.
What we have after transpiling?

package main
import "github.com/elliotchance/c2go/noarch"
type tt struct {
        t int
}
func some() {
        var u tt    ///// <<<----   STRUCT IS GONE.
        u.t = noarch.ByteSliceToInt([]byte("Uuups\x00"))
}
func main() {
        __init()
        var u tt
        u.t = 5
        return
}
func __init() {
}

It is happen, because Struct is global variable of program:

c2go/program/program.go

Lines 55 to 58 in a7ac059

// The definitions for defined structs.
// TODO: This field should be protected through proper getters and setters.
Structs StructRegistry
Unions StructRegistry

How to solve that?
Create a scope for structs. Preliminary steps (in my point of view):

  1. remove global variable Struct from Program
  2. add Struct var , here:
    // Now begin building the Go AST.
    err = transpileToNode(root, p)
  3. If we going inside function we have to create a copy of Struct var:
    case *ast.FunctionDecl:
    err := transpileFunctionDecl(n, p)
    if err != nil {
    return err
    }

    add for this
    case *ast.TranslationUnitDecl:

P.S. : Like I understood, same problem with unions.
P.P.S: Implementation of that issue only after success of #344.

@elliotchance
Copy link
Owner

Another solution that would require less refactoring:

  1. When you find a struct inside a function add the name of the function to the struct (so all structs are global still). For example there would be a struct tt and struct main_tt.
  2. When resolving a type use the current function first, so always search for struct main_tt, then struct tt

Keeping all types in global space will also make things like removing duplicate code easier and making the output code more simple.

@Konstantin8105
Copy link
Contributor Author

Also good example #346

@Konstantin8105
Copy link
Contributor Author

@elliotchance , For myself is not clear about a reason for duplicates.
Could you clarify?
After #384 , we haven't any duplicate for multi file C code case.

@elliotchance
Copy link
Owner

In the first example there are two structs that have the same name. If we move both of them to global space then then names will clash or one will replace the definition of the other. We would have to rename the struct inside the main function to prevent this.

@Konstantin8105
Copy link
Contributor Author

Konstantin8105 commented Dec 12, 2017

Also, example:

void rr()
{
   {
        struct tt{
                char* t;
        };
        struct tt u;
        u.t = "Uuups";
   }
   {
        struct tt{
                double t;
        };
        struct tt u;
        u.t = 42.24;
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants