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

Split Module Definitions #1241

Closed
DylanLukes opened this issue Dec 1, 2011 · 3 comments
Closed

Split Module Definitions #1241

DylanLukes opened this issue Dec 1, 2011 · 3 comments
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@DylanLukes
Copy link

Within the same file and module, modules should be able to be split up and combined at compile-time. This would allow putting non-native functions in a native module, in turn allowing convenient use of opaque forms. For example:

mod objc {
  // Opaque type for objc objects. This might more realistically be a resource type to handle ObjC reference counting.
  tag id { _id(unsafe::id) }

  // safe version
  fn get_class(name: str) -> id {
    _id(str::as_buf(name, {|name_buf| unsafe::objc_getClass(name_buf)}))
  } 

  mod unsafe {
    // non-opaque type for objc objects
    type void  = int;
    type cls = *void;
    type id = *{isa: cls};
  }
  native mod unsafe {
    fn objc_getClass(name: str::sbuf) -> id;
  }
}
@brson
Copy link
Contributor

brson commented Mar 6, 2012

We could also just allow native function declarations in regular modules. We could even eliminate native mods altogether and just give regular mods some attributes to use by default for native functions, like:

#[native(name = "libc")]
#[native(abi = "stdcall")]
mod {
    native fn printf(...);
}

@ghost ghost assigned brson Apr 12, 2012
@jsternberg
Copy link
Contributor

Just chiming in, I like the idea of having native act on a specific function instead of on a module. I don't like the syntax presented in the brson's comment though. Instead of a native macro acting on a module, maybe the native macro should be a crate definition similar to the link macro?

For example, for linking to something like sqlite3.

// sqlite3.rc
// probably name this something different, only specifies what to link against
// (instead of searching for a rust library, searches for a normal shared lib)
#[native(name="sqlite3")];
#[link(name="sqlite3", vers="3.0", ...)];

mod sqlite3;

// sqlite3.rs
mod unsafe {
    // specify the abi
    #[native(abi="stdcall")] fn printf(...);
    // default to stdcall
    #[native] fn fprintf(...);
}

// would also work for tagging an entire module as native
#[native]
mod nativestuff {
    fn foobar();
}

That way, link definitions are kept in the crate file (which is already used to determine compilation anyway) and the information doesn't have to be repeated for every native module. The native macro would essentially be the equivalent of extern "C". It's also easily extended to working on modules.

@graydon
Copy link
Contributor

graydon commented Aug 29, 2012

I think between export globbing and the reforms planned on extern, this can safely be put to rest. We'll be able to declare extern functions one-by-one, and permitting multiple fragments of a module seems like .. a plausible way to address this space, but probably a redundant one, given all the other options. Closing.

@graydon graydon closed this as completed Aug 29, 2012
coastalwhite pushed a commit to coastalwhite/rust that referenced this issue Aug 5, 2023
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
* Now gen-c display the c file instead of the goto binnary.

Co-authored-by: Jérôme Dubois <[email protected]>
Kobzol pushed a commit to Kobzol/rust that referenced this issue Dec 30, 2024
bors pushed a commit to rust-lang-ci/rust that referenced this issue Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

4 participants