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

Ambiguous module resolution causes erroneous name mangling #288

Closed
sacredbirdman opened this issue Sep 6, 2018 · 5 comments · Fixed by #296
Closed

Ambiguous module resolution causes erroneous name mangling #288

sacredbirdman opened this issue Sep 6, 2018 · 5 comments · Fixed by #296

Comments

@sacredbirdman
Copy link

If the code uses two modules which have similarly named functions, it may be able to infer the correct one but the name mangling doesn't keep up.
This code has "init"-functions from two modules (Vector2, GLFW) and Carp infers that GLFW is the correct one but produces incorrect C-code:

(load "GLFW.carp")
(use GLFW)

(load "OpenGL.carp")
(load "Vector.carp")

(use Vector2)

(defn-do main []
  (let-do [_ (init)
           window (create-window 640 480 (cstr "GLFW <3 CARP") NULL NULL)]
    (make-context-current window))
  0)

The error message from C-compiler is

./out//main.c:4591:18: warning: implicit declaration of
      function 'GLFW_init' is invalid in C99
      [-Wimplicit-function-declaration]
        int _7 = GLFW_init();
@hellerve
Copy link
Member

hellerve commented Sep 6, 2018

Oh, that’s interesting! As per the definition of GLFW.init in the GLFW module, the emitter should emit a call to glfwInit, which is an actual glfw function.

It doesn’t, though, and that might be because it’s both an interface function (init being part of every struct’s definition) and an external C function that is renamed.

Great find!

@eriksvedang
Copy link
Collaborator

Thanks for finding this! I’ll solve it unless anyone else wants to get their hands dirty :)

@sacredbirdman sacredbirdman changed the title Ambiguous module resolution causes erroneus name mangling Ambiguous module resolution causes erroneous name mangling Sep 7, 2018
@hellerve
Copy link
Member

I’m currently working on this. It’s weird. If I drop into a REPL with this and call (c main) to look at the generated C code, the appropriate name is used. If we actually compile, however, the name is resolved erroneously. This leads me to believe that the error isn’t actually happening in the annotation/constraint solving step, but later.

@eriksvedang
Copy link
Collaborator

Oh, that’s even weirder... but a good clue. I’ll investigate and see if I can give you some pointers

@eriksvedang
Copy link
Collaborator

I think the problem is line 234 in Concretize.hs, when a multi symbol is turned into a simple symbol – it sets the SymbolMode (see definition in Obj.hs at line 30) to LookupGlobal which isn't necessarily true:

normalSymbol = XObj (Sym singlePath (LookupGlobal CarpLand AFunction)) i (Just suffixed)

Instead it should probably copy the SymbolMode from the definition at the path named singlePath in the code above.

Right now the function that figures out those things is doesNotBelongToAnInterface in Qualify.hs, line 104. Perhaps a little bit of code duplication could be OK to avoid having to move it out of there, since that (inner) function is pretty entangled with the setFullyQualifiedSymbols function. Perhaps you can find a nicer solution though..!

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

Successfully merging a pull request may close this issue.

3 participants