Null references (oh no, not again!)
Max Samukha
samukha at voliacable.com.removethis
Thu Mar 5 01:36:19 PST 2009
On Thu, 05 Mar 2009 10:06:14 +0100, Don <nospam at nospam.com> wrote:
>Max Samukha wrote:
>> On Thu, 05 Mar 2009 09:09:42 +0100, Don <nospam at nospam.com> wrote:
>>
>>> Denis Koroskin wrote:
>>>> On Wed, 04 Mar 2009 19:39:06 +0300, Don <nospam at nospam.com> wrote:
>>>>
>>>>> Andrei Alexandrescu wrote:
>>>>>> Don wrote:
>>>>>>> Andrei Alexandrescu wrote:
>>>>>>>> And there is no reference type with two subtypes. It's one type in
>>>>>>>> the language and one in the library. Maybe-null (the library) is a
>>>>>>>> supertype of non-null (the default).
>>>>>>> One problem I can see is with extern(C),(Windows) functions, since
>>>>>>> pointers are maybe-null in C. The name-mangling has to work out.
>>>>>>> I can't see how this can be done without the compiler knowing
>>>>>>> SOMETHING about both nullable and non-nullable types.
>>>>>>> At the bare minimum, you need to deal with maybe-null returns and
>>>>>>> reference parameters from C functions.
>>>>>> Walter is thinking of making only references non-null and leaving
>>>>>> pointers as they are. (I know, cry of horror.) But say pointers are
>>>>>> also non-null. Then:
>>>>>> extern(C) MaybeNull!(void*) malloc(size_t s);
>>>>>> will work, provided that MaybeNull has no size overhead and that
>>>>>> word-sized structs are returned in the same register as word returns
>>>>>> (I seem to remember Walter told me that's the case already).
>>>>> Here's a typical annoying Windows API function
>>>>> --------
>>>>> int GetTextCharsetInfo(
>>>>> HDC hdc, // handle to DC
>>>>> LPFONTSIGNATURE lpSig, // data buffer
>>>>> DWORD dwFlags // reserved; must be zero
>>>>> );
>>>>>
>>>>> lpSig
>>>>> [out] Pointer to a FONTSIGNATURE data structure that receives
>>>>> font-signature information. The lpSig parameter can be NULL if you do
>>>>> not need the FONTSIGNATURE information.
>>>>> ---------
>>>>> How do you do this?
>>>>>
>>>>> Don.
>>>> extern(System) int GetTextCharsetInfo(
>>>> HDC hdc,
>>>> MaybeNull!(FONTSIGNATURE*) lpSig, // or whatever
>>>> DWORD dwFlags);
>>>>
>>>> GetTextCharsetInfo(hdc, null, flags); // fine
>>>> GetTextCharsetInfo(hdc, &sig, flags); // also ok
>>>>
>>> But it needs to have the type name mangled as LPFONTSIGNATURE, not as
>>> MaybeNull!(FONTSIGNATURE*). Otherwise it can't link to Windows.
>>
>> Parameter names are not mangled for stacall, only their total size.
>> IIRC, the above will get mangled into _GetTextCharsetInfo at 12 just like
>> it would without MaybeNull
Parameter names -> parameter types
stacall -> stdcall
Sorry
>>
>Cool. So it'd only be an issue with extern(C++).
You're right, but the C++ interface is so limited that I don't think
anybody uses it.
More information about the Digitalmars-d
mailing list