Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 30, 2019 How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
I want to get a mangled name of a D function by `core.demangle.mangle`, but I'm in trouble because there are no ways to express a type of a function, which is used for a template argument of `mangle`. For example, it is wrong to use the type `int function(int,int)` to express the type of `int add(int,int)`. Because it expresses the type of a function POINTER, not just a function. The fuction name in a binary compiled this function is "_D3addFiiZi", but `mangle!(int function(int,int))("add")` returns "_D3addPFiiZi", which includes "P" meaning POINTER. How can I get the former one? Thanks. |
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sobaya | On Wednesday, 30 January 2019 at 05:14:20 UTC, Sobaya wrote:
> I want to get a mangled name of a D function by `core.demangle.mangle`, but I'm in trouble because there are no ways to express a type of a function, which is used for a template argument of `mangle`.
>
> For example, it is wrong to use the type `int function(int,int)` to express the type of `int add(int,int)`.
> Because it expresses the type of a function POINTER, not just a function.
>
> The fuction name in a binary compiled this function is "_D3addFiiZi", but `mangle!(int function(int,int))("add")` returns "_D3addPFiiZi", which includes "P" meaning POINTER.
>
> How can I get the former one?
>
> Thanks.
import std.stdio;
alias int* PINT;
void main()
{
auto x= Add(1,2);
writeln(x);
writeln(&x);
executeShell("pause");
}
private PINT Add(int a,int b)
{
return cast(PINT)(a+b);
}
////////////////CODE END//////////
It works ok.
|
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Wednesday, 30 January 2019 at 05:40:50 UTC, FrankLike wrote: > On Wednesday, 30 January 2019 at 05:14:20 UTC, Sobaya wrote: >> I want to get a mangled name of a D function by `core.demangle.mangle`, but I'm in trouble because there are no ways to express a type of a function, which is used for a template argument of `mangle`. >> >> For example, it is wrong to use the type `int function(int,int)` to express the type of `int add(int,int)`. >> Because it expresses the type of a function POINTER, not just a function. >> >> The fuction name in a binary compiled this function is "_D3addFiiZi", but `mangle!(int function(int,int))("add")` returns "_D3addPFiiZi", which includes "P" meaning POINTER. >> >> How can I get the former one? >> >> Thanks. import std.stdio; import std.process:executeShell; import core.demangle; void main() { assert(mangle!(int function(int))("a.b") == "_D1a1bPFiZi"); executeShell("pause"); } > ////////////////CODE END////////// > Yes,"_D1a1bPFiZi",which includes "P". |
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Wednesday, 30 January 2019 at 06:02:02 UTC, FrankLike wrote:
> On Wednesday, 30 January 2019 at 05:40:50 UTC, FrankLike wrote:
>> On Wednesday, 30 January 2019 at 05:14:20 UTC, Sobaya wrote:
>>> [...]
>
> import std.stdio;
> import std.process:executeShell;
> import core.demangle;
>
> void main()
> {
> assert(mangle!(int function(int))("a.b") == "_D1a1bPFiZi");
> executeShell("pause");
> }
>> ////////////////CODE END//////////
>> Yes,"_D1a1bPFiZi",which includes "P".
I want a mangled function name without "P", not one with "P".
|
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sobaya | On 01/29/2019 09:14 PM, Sobaya wrote:
> I want to get a mangled name of a D function by `core.demangle.mangle`, but I'm in trouble because there are no ways to express a type of a function, which is used for a template argument of `mangle`.
>
> For example, it is wrong to use the type `int function(int,int)` to express the type of `int add(int,int)`.
> Because it expresses the type of a function POINTER, not just a function.
>
> The fuction name in a binary compiled this function is "_D3addFiiZi", but `mangle!(int function(int,int))("add")` returns "_D3addPFiiZi", which includes "P" meaning POINTER.
>
> How can I get the former one?
>
> Thanks.
>
typeof works:
import core.demangle;
int add(int, int);
void main() {
alias F = typeof(add);
pragma(msg, mangle!F("add"));
}
Ali
|
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sobaya | On 1/30/19 12:14 AM, Sobaya wrote:
> I want to get a mangled name of a D function by `core.demangle.mangle`, but I'm in trouble because there are no ways to express a type of a function, which is used for a template argument of `mangle`.
>
> For example, it is wrong to use the type `int function(int,int)` to express the type of `int add(int,int)`.
> Because it expresses the type of a function POINTER, not just a function.
>
> The fuction name in a binary compiled this function is "_D3addFiiZi", but `mangle!(int function(int,int))("add")` returns "_D3addPFiiZi", which includes "P" meaning POINTER.
>
> How can I get the former one?
Why not use add.mangleof?
-Steve
|
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 01/30/2019 07:47 AM, Steven Schveighoffer wrote: > On 1/30/19 12:14 AM, Sobaya wrote: >> I want to get a mangled name of a D function by >> `core.demangle.mangle`, but I'm in trouble because there are no ways >> to express a type of a function, which is used for a template argument >> of `mangle`. >> >> For example, it is wrong to use the type `int function(int,int)` to >> express the type of `int add(int,int)`. >> Because it expresses the type of a function POINTER, not just a function. >> >> The fuction name in a binary compiled this function is "_D3addFiiZi", >> but `mangle!(int function(int,int))("add")` returns "_D3addPFiiZi", >> which includes "P" meaning POINTER. >> >> How can I get the former one? > > Why not use add.mangleof? > > -Steve add.mangleof includes the module name as well (_D6deneme3addFiiZi) but the OP wanted without (_D3addFiiZi). I wonder why the inconsistency. On the other hand, .mangleof produces just "add" when the function is extern(C). (?) import core.demangle; extern(C) int add(int, int); void main() { alias F = typeof(add); pragma(msg, mangle!F("add")); pragma(msg, add.mangleof); } Output: _D3addUiiZi add <-- Is that correct? Ali |
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wed, Jan 30, 2019 at 10:39:21AM -0800, Ali Çehreli via Digitalmars-d-learn wrote: [...] > I wonder why the inconsistency. On the other hand, .mangleof produces > just "add" when the function is extern(C). (?) [...] For extern(C), this is correct behaviour, because that's how a C function would be mangled (i.e., not mangled at all). T -- In a world without fences, who needs Windows and Gates? -- Christian Surchi |
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wed, 30 Jan 2019 10:39:21 -0800, Ali Çehreli wrote:
> import core.demangle;
>
> extern(C) int add(int, int);
>
> void main() {
> alias F = typeof(add);
> pragma(msg, mangle!F("add"));
> pragma(msg, add.mangleof);
> }
>
> Output:
>
> _D3addUiiZi
> add <-- Is that correct?
`add.mangleof` is correct. In fact, it's definitively correct.
typeof(add) is extern(C) int function(int, int). Based on this output, core.demangle seems like it's not taking the extern(C) portion into account.
|
January 30, 2019 Re: How can I express the type of a function in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Neia Neutuladh | On Wed, Jan 30, 2019 at 07:51:17PM +0000, Neia Neutuladh via Digitalmars-d-learn wrote: > On Wed, 30 Jan 2019 10:39:21 -0800, Ali Çehreli wrote: > > import core.demangle; > > > > extern(C) int add(int, int); > > > > void main() { > > alias F = typeof(add); > > pragma(msg, mangle!F("add")); > > pragma(msg, add.mangleof); > > } > > > > Output: > > > > _D3addUiiZi > > add <-- Is that correct? > > `add.mangleof` is correct. In fact, it's definitively correct. > > typeof(add) is extern(C) int function(int, int). Based on this output, > core.demangle seems like it's not taking the extern(C) portion into > account. That would be a bug in core.demangle. T -- "I suspect the best way to deal with procrastination is to put off the procrastination itself until later. I've been meaning to try this, but haven't gotten around to it yet. " -- swr |
Copyright © 1999-2021 by the D Language Foundation