This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 411
config.d: use enums instead of structs for __c_long, etc. #2160
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| /** | ||
| * D header file for C99. | ||
| /*** | ||
| * D compatible types that correspond to various basic types in associated | ||
| * C and C++ compilers. | ||
| * | ||
| * Copyright: Copyright Sean Kelly 2005 - 2009. | ||
| * License: Distributed under the | ||
|
|
@@ -12,6 +13,53 @@ | |
|
|
||
| module core.stdc.config; | ||
|
|
||
| version (StdDdoc) | ||
| { | ||
| /*** | ||
| * Used for a signed integer type that corresponds in size to the associated | ||
| * C compiler's `long` type. | ||
| */ | ||
| alias c_long = int; | ||
|
|
||
| /*** | ||
| * Used for an unsigned integer type that corresponds in size to the associated | ||
| * C compiler's `unsigned long` type. | ||
| */ | ||
| alias c_ulong = uint; | ||
|
|
||
| /*** | ||
| * Used for a signed integer type that corresponds in size and mangling to the associated | ||
| * C++ compiler's `long` type. | ||
| */ | ||
| alias cpp_long = c_long; | ||
|
|
||
| /*** | ||
| * Used for an unsigned integer type that corresponds in size and mangling to the associated | ||
| * C++ compiler's `unsigned long` type. | ||
| */ | ||
| alias cpp_ulong = c_ulong; | ||
|
|
||
| /*** | ||
| * Used for a floating point type that corresponds in size and mangling to the associated | ||
| * C++ compiler's `long double` type. | ||
| */ | ||
| alias c_long_double = real; | ||
|
|
||
| /*** | ||
| * Used for an unsigned integer type that corresponds in size and mangling to the associated | ||
| * C++ compiler's `size_t` type. | ||
| */ | ||
| alias cpp_size_t = size_t; | ||
|
|
||
| /*** | ||
| * Used for a signed integer type that corresponds in size and mangling to the associated | ||
| * C++ compiler's `ptrdiff_t` type. | ||
| */ | ||
| alias cpp_ptrdiff_t = ptrdiff_t; | ||
| } | ||
| else | ||
| { | ||
|
|
||
| version (OSX) | ||
| version = Darwin; | ||
| else version (iOS) | ||
|
|
@@ -21,40 +69,10 @@ else version (TVOS) | |
| else version (WatchOS) | ||
| version = Darwin; | ||
|
|
||
| extern (C): | ||
| @trusted: // Types only. | ||
| nothrow: | ||
| @nogc: | ||
|
|
||
| version( Windows ) | ||
| { | ||
| struct __c_long | ||
| { | ||
| pure nothrow @nogc @safe: | ||
| this(int x) { lng = x; } | ||
| int lng; | ||
| alias lng this; | ||
| } | ||
|
|
||
| struct __c_ulong | ||
| { | ||
| pure nothrow @nogc @safe: | ||
| this(uint x) { lng = x; } | ||
| uint lng; | ||
| alias lng this; | ||
| } | ||
|
|
||
| /* | ||
| * This is cpp_long instead of c_long because: | ||
| * 1. Implicit casting of an int to __c_long doesn't happen, because D doesn't | ||
| * allow constructor calls in implicit conversions. | ||
| * 2. long lng; | ||
| * cast(__c_long)lng; | ||
| * does not work because lng has to be implicitly cast to an int in the constructor, | ||
| * and since that truncates it is not done. | ||
| * Both of these break existing code, so until we find a resolution the types are named | ||
| * cpp_xxxx. | ||
| */ | ||
| enum __c_long : int; | ||
| enum __c_ulong : uint; | ||
|
|
||
| alias __c_long cpp_long; | ||
| alias __c_ulong cpp_ulong; | ||
|
|
@@ -66,26 +84,19 @@ else version( Posix ) | |
| { | ||
| static if( (void*).sizeof > int.sizeof ) | ||
| { | ||
| enum __c_long : long; | ||
| enum __c_ulong : ulong; | ||
|
|
||
| alias __c_long cpp_long; | ||
| alias __c_ulong cpp_ulong; | ||
|
|
||
| alias long c_long; | ||
| alias ulong c_ulong; | ||
| } | ||
| else | ||
| { | ||
| struct __c_long | ||
| { | ||
| pure nothrow @nogc @safe: | ||
| this(int x) { lng = x; } | ||
| int lng; | ||
| alias lng this; | ||
| } | ||
|
|
||
| struct __c_ulong | ||
| { | ||
| pure nothrow @nogc @safe: | ||
| this(uint x) { lng = x; } | ||
| uint lng; | ||
| alias lng this; | ||
| } | ||
| enum __c_long : int; | ||
| enum __c_ulong : uint; | ||
|
|
||
| alias __c_long cpp_long; | ||
| alias __c_ulong cpp_ulong; | ||
|
|
@@ -103,13 +114,7 @@ version( CRuntime_Microsoft ) | |
| * to generate the correct name mangling and correct function call/return | ||
| * ABI conformance. | ||
| */ | ||
| struct __c_long_double | ||
| { | ||
| pure nothrow @nogc @safe: | ||
| this(double d) { ld = d; } | ||
| double ld; | ||
| alias ld this; | ||
| } | ||
| enum __c_long_double : double; | ||
|
|
||
| alias __c_long_double c_long_double; | ||
| } | ||
|
|
@@ -150,3 +155,15 @@ else version( SDC ) | |
| } | ||
|
|
||
| static assert(is(c_long_double), "c_long_double needs to be declared for this platform/architecture."); | ||
|
|
||
| version (Darwin) | ||
| { | ||
| alias cpp_size_t = cpp_ulong; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if we do the other way around, i.e.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I plan to do that separately. |
||
| alias cpp_ptrdiff_t = cpp_long; | ||
| } | ||
| else | ||
| { | ||
| alias cpp_size_t = size_t; | ||
| alias cpp_ptrdiff_t = ptrdiff_t; | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had no clue that it was even legal to declare an
enumwith no members. That seems very weird. Is the point of doing this instead of using analiasso that it's a distinct type fromint? I guess that that would mean that this technique is atypedefof sorts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, so that the compiler can do special mangling for that type. Although I don't see how it's any better than a struct. I proposed to add support for adding
pragma(mangle)to alias declarations but Walter doesn't seem to like that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you go down that path, you'll discover you've reinvented enums.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmdavis BTW, one use case of declaring an enum without any members is to use it as a UDA that doesn't contain any data.