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

Name mangling: preserve leading and trailing underscores #312

Closed
TravisCardwell opened this issue Nov 28, 2024 · 2 comments · Fixed by #319
Closed

Name mangling: preserve leading and trailing underscores #312

TravisCardwell opened this issue Nov 28, 2024 · 2 comments · Fixed by #319
Assignees

Comments

@TravisCardwell
Copy link
Collaborator

We shall assume that leading and trailing underscores have special meaning, while underscores in between letters/numbers are just used for separation. Multiple leading/trailing underscores should be preserved.

Haskell identifiers that start with a lowercase letter may start with an underscore.

Haskell identifiers that start with an uppercase letter may not start with an underscore. For example, translating C type _foo to a Haskell type constructor that preserves the leading underscore requires a prefix before the underscore.

@phadej provided an example in #306:

typedef struct _linked_list {
  int x;
  struct _linked_list* next; /* linked_list doesn't exist here yet */
} linked_list;
data C_LinkedList = MkC_LinkedList { x :: Int, next :: Ptr C_LinkedList }
newtype CLinkedList = MkCLinkedList C_LinkedList

Note that joinWithCamelCase is still used, so leading and trailing underscores should be handled separately.

@TravisCardwell TravisCardwell self-assigned this Nov 28, 2024
@phadej
Copy link
Collaborator

phadej commented Dec 1, 2024

IMHO, the default name mangling should do as little as necessary to make valid Haskell names.

Doing underscores to camelCase like translations feels a bit tricky. Perfectly we will be able to describe the mangling rules in few sentences. Users should be able to predict what the Haskell names will be. IMHO it doesn't matter what are the "idiomatic" names in Haskell.

In particular, recently I started to dislike the C prefix. Why we have it. (Foreign.C.Types has it for a reason, but we don't share the same reasoning: there are no types to clash with).

@TravisCardwell
Copy link
Collaborator Author

This is a question for @edsko, but I think it is just a matter of taste/preference.

A few technical notes:

  • A prefix is required for type constructors to preserve leading underscores without translation or escaping, since type constructor names may not start with an underscore.

  • When preserving C names as much as possible, a prefix makes it possible to avoid converting the case of the first letter of the C name.

TravisCardwell added a commit that referenced this issue Dec 1, 2024
This is a change to `camelCaseCName`, used for type constructors by our
default name manglers.
@edsko edsko closed this as completed in #319 Dec 3, 2024
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.

2 participants