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

Unable to importc objc class hierarchy #894

Closed
gradha opened this issue Feb 9, 2014 · 2 comments
Closed

Unable to importc objc class hierarchy #894

gradha opened this issue Feb 9, 2014 · 2 comments

Comments

@gradha
Copy link
Contributor

gradha commented Feb 9, 2014

The importc pragma works ok for single objc classes, but can't be used to model hierarchies of external classes. The following code illustrates in valid Nimrod code what I want to do:

block:
  {.passL: "-lobjc".}
  {.passL: "-framework AppKit".}

type
  TNSObject* = object of TObject
  PNSObject* = ref TNSObject

  TNSString* = object of TNSObject
  PNSString* = ref TNSString

proc check_isa*(x: PNSObject) =
  echo "The isa value is ", repr(x)

when isMainModule:
  var a: PNSString
  check_isa(a)

Essentially, this models the objc NSObject with NSString being a subclass. Now, trying to do the same with importc:

type
  TNSObject* {.importc: "NSObject", inheritable,
    header: """<Foundation/Foundation.h>""".} = object
  PNSObject* = ptr TNSObject

  TNSString* {.importc: "NSString".} = object of TNSObject
  PNSString* = ptr TNSString

Nimrod compiles fine but fails during backend compilation:

/private/tmp/cycle/nimcache/cycle_h.m:109:27: error: 'NSString' does not have a
      member named 'Sup'
        checkisa_80015(&a_80026->Sup);
                        ~~~~~~~  ^
/private/tmp/cycle/nimcache/cycle_h.m:115:17: error: application of 'sizeof' to
      interface 'NSObject' is not supported on this architecture and platform
NTI80007.size = sizeof(NSObject);
                ^     ~~~~~~~~~~
2 errors generated.
Error: execution of an external program failed

As far as I can see in the generated code, the compiler is generating code for the imported classes as if they were normal nimrod structs, hence the access of the Sup field or trying to initialize the structure with sizeof.

How could this be avoided? It seems that the only way to make something like this work is teach the codegen part about objc classes. If on the Nimrod side they can't be expressed as object of XXX then the only other solution I can see is adding converters for all types to their base types or manual casting between them.

Maybe this has been already solved for C++ interfacing?

@gradha
Copy link
Contributor Author

gradha commented Feb 10, 2014

Testing with c2nim suggested marking classes as pure but still the same C code was being generated.

@Varriount
Copy link
Contributor

Moved to nim-lang/c2nim#1

Clyybber pushed a commit to Clyybber/Nim that referenced this issue Sep 16, 2023
<!--- The Pull Request (=PR) message is what will get automatically used
as
the commit message when the PR is merged. Make sure that no line is
longer
than 72 characters -->

Removed obsolete comments related to nimsuggest from the `nimsuggest`
and `suggest` modules.
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

No branches or pull requests

2 participants