ImportC: When targeting Windows, define wchar_t as wchar.#23364
Open
just-harry wants to merge 3 commits into
Open
ImportC: When targeting Windows, define wchar_t as wchar.#23364just-harry wants to merge 3 commits into
wchar_t as wchar.#23364just-harry wants to merge 3 commits into
Conversation
56de97e to
4d321c4
Compare
Contributor
|
Can confirm, reading C2y the spec is, well let's just say you want to do that with a clear head. Matching MSVC should be a good place to begin. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The purpose of ImportC is to make it convenient to use C libraries in D.
Presently, ImportC does not handle C's
wchar_tpseudo-type in any special way, thuswchar_twill be defined as an integer type bystddef.h.Consequently, when C code that uses
wchar_tis imported into D, uses ofwchar_twill translate into uses of a D integer type.This is jolly for C, as C will happily convert string-literals to contiguous-integer-spans.
D, unlike C, will not happily convert string-literals to contiguous-integer-spans: in D, character types are distinct from integer types, unlike in C.
The ultimate result of this is that when a user attempts to use ImportC to
#include <windows.h>(or some other header) and whip up a "Hello, World!", they can't simply:they instead have to:
Mocking ImportC and D on the orange site afterwards is an optional follow-on step.
This is an objectively horrible user-experience, and should be remedied.
This PR makes D's character types available to C via three aliases in the
__builtinsmodule:__importc_char;__importc_wchar;__importc_dchar.(
__importc_charis somewhat superfluous, but it's provided for consistency and ease of code-generation.)On Windows,
wchar_tis an unsigned 16-bit integer which represents a UTF-16 code-unit. MSVC goes a step further and defineswchar_t/__wchar_tas built-in compiler-provided types.Windows' and MSVC's definition of
wchar_tmatches D's definition ofwchar.Thus, in
importc.h, when targeting Windows, this PR typedefswchar_tas__importc_wcharand defines_WCHAR_T_DEFINEDand_NATIVE_WCHAR_T_DEFINEDas1appropriately.If
_MSC_VERis defined,__wchar_tis also typedef-ed so as to match MSVC's behaviour.The reason why I began this PR's description with ImportC's raison d'être and a user story in an obnoxious patter is because a previous attempt to handle
wchar_tin ImportC was rejected/abandoned—mainly due to concerns about its implementation, and its mishandling of non-Windowswchar_t.I think this PR's way of implementing
wchar_tshould be fine:typedef <type> wchar_t;is howwchar_tis typically defined instddef.h.Likewise, this PR is something of a Pareto implementation as it simply does not do anything about
wchar_ton non-Windows targets.Outside of Windows,
wchar_tcan be four bytes, or two bytes, or one byte, or it could be something else! Moreover, it can be signed or unsigned.Thus, handling
wchar_ton non-Windows platforms will require a more complicated/involved solution.However, given that usage of
wchar_tis much more common on Windows than it is on other platforms (I'm asserting this without evidence): I think handlingwchar_ton Windows, and engaging the Somebody Else's Problem field forwchar_telsewhere, is highly beneficial to ImportC's usability.P.S. Sorry for the wall of text. Thank you for reading.