-
Notifications
You must be signed in to change notification settings - Fork 0
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
Minimal default name mangling #331
Comments
Would they really in any reasonable library? |
I read that as grounds to not prefix by default. Anybody who uses a library that causes such collisions should work around them by configuring a name mangler that uses prefixes. The name mangling DSL makes it easy to do so, so I think that this is a very reasonable approach. Do the following strategies look like what you have in mind?
The example from #306 (reproduced below) is a good one. typedef struct _linked_list {
int x;
struct _linked_list* next; /* linked_list doesn't exist here yet */
} linked_list; Following the above strategies, we would generate the following. data C_linked_list = C_linked_list {
c_linked_list_x :: FC.CInt
, c_linked_list_next :: FC.Ptr C_linked_list
}
newtype Linked_list = Linked_list {
un_Linked_list :: C_linked_list
} Since that is a common convention, however, perhaps it would be worthwhile to handle it specially? For example, we could generate the following, dropping the underscore and treating it like the C names are the same (no data Linked_list = Linked_list {
linked_list_x :: FC.CInt
, linked_list_next :: FC.Ptr Linked_list
} |
We can't. Firstly it's quite tricky to implement. Currently translation only considers the C names (and those only in quite special cases). The name mangling is an utility. And secondly, the less surprising (i.e. more predictable) the pipeline is the better. TL;DR lets keep it simple |
Anonymous structures for named fields are not being named correctly. Some name mangling functionality is now implemented in If we do this, then users are not able to customize how such structures are named. I can change the implementation to be consistent with the new name mangling defaults, but it would of course not be consistent with the Implementing name mangling in (just) Thoughts? |
Notes while reviewing the new defaults:
Unrelated issues:
|
This commit changes name mangling so that there are minimal changes by default, not attempting to create Haskell-style names. One significant difference with the previous implementation is that it can now add a prefix if/when the first character is not valid, used to handle leading underscores when creating type constructors. `MkHsName` is removed, replaced by `mkHsNamePrefixInvalid` and `mkHsNameDropInvalid` functions that are now passed to `translateName` like other options. `ctxFieldVarSingleConstr` is removed since constructors now have the same name as types by default, and we do not generate sum types anyway. If we do so in the future, we need to decide how to do name mangling for constructors as well as accessors. `HsBindgen.C.Fold.Type.mkDefnName` is changed to follow the conventions of the new defaults.
This commit changes name mangling so that there are minimal changes by default, not attempting to create Haskell-style names. One significant difference with the previous implementation is that it can now add a prefix if/when the first character is not valid, used to handle leading underscores when creating type constructors. `MkHsName` is removed, replaced by `mkHsNamePrefixInvalid` and `mkHsNameDropInvalid` functions that are now passed to `translateName` like other options. `ctxFieldVarSingleConstr` is removed since constructors now have the same name as types by default, and we do not generate sum types anyway. If we do so in the future, we need to decide how to do name mangling for constructors as well as accessors. `HsBindgen.C.Fold.Type.mkDefnName` is changed to follow the conventions of the new defaults.
I ended up needing to work on this in the I had to change how overrides work. The initial implementation allowed users to override name mangling by specifying the C name. This is problematic when the name of an anonymous structure/union needs to be overridden. Specifying a C name worked when some name mangling was performed while creating the C AST, but it does not work when all name mangling is done by the name mangler. In the new implementation, users override by specifying the translated Haskell name, which allows overriding names for anonymous structures/unions. Note that since overrides must now be performed after creating the Haskell name, the parameter order of The next commit refactors |
In #312 (comment) @phadej wrote:
In #312 (comment) @TravisCardwell replied:
I discussed this with @edsko yesterday.
We currently provide two sets of name manglers. The default (
defaultNameMangler
) creates Haskell-style type/constructor names while variables are minimally mangled. It is safer thanhaskellNameMangler
, which creates Haskell-style variable names as well. By "safety," I mean risk of name collision. "Safe" means relatively low risk. In addition, the API enables developers to create their own name manglers.@edsko is fine with making the default do as little as necessary to make valid Haskell names. Users who want Haskell-style names can use the
haskellNameMangler
.@phadej Do you think it is worth adding a prefix in order to avoid case conversion? Example: C types
foo
andFoo
may both exist.If we do not prefix every type constructor name, we have to decide what to do about leading underscores. Should we include a prefix only in that case?
The text was updated successfully, but these errors were encountered: