www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to preserve function parameters scopeness

reply Ivan Butygin <ivan.butygin gmail.com> writes:
I want to extract function parameters types from declaration, 
remove some of them and construct new function pointer type from 
remaining types. Doing this in obvious way cause
parameters to lost their scope storage class: 
https://run.dlang.io/is/cQBFRW

I can extract and store scope information separately via 
ParameterStorageClassTuple but I don't know how to apply this 
information back when constructing new function pointer type. Of 
course I can CTFE string representation with full declaration and 
mixin it but it's ugly and probably slow to compile. Any ideas 
how to do this better?

I need this for my jit-bind template machinery, if someone 
interested
https://github.com/ldc-developers/ldc/blob/master/runtime/jit-rt/d/ldc/dynamic_compile.d#L120
Jun 06 2019
next sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Thursday, 6 June 2019 at 09:45:08 UTC, Ivan Butygin wrote:
 I want to extract function parameters types from declaration, 
 remove some of them and construct new function pointer type 
 from remaining types. Doing this in obvious way cause
 parameters to lost their scope storage class: 
 https://run.dlang.io/is/cQBFRW

 I can extract and store scope information separately via 
 ParameterStorageClassTuple but I don't know how to apply this 
 information back when constructing new function pointer type. 
 Of course I can CTFE string representation with full 
 declaration and mixin it but it's ugly and probably slow to 
 compile. Any ideas how to do this better?

 I need this for my jit-bind template machinery, if someone 
 interested
 https://github.com/ldc-developers/ldc/blob/master/runtime/jit-rt/d/ldc/dynamic_compile.d#L120
As far as I know, this is currently impossible without CTFE and mixins. FWIW, it's filed here: https://issues.dlang.org/show_bug.cgi?id=19787 -- Simen
Jun 06 2019
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 6 June 2019 at 09:45:08 UTC, Ivan Butygin wrote:
 I want to extract function parameters types from declaration, 
 remove some of them and construct new function pointer type 
 from remaining types.
Do you know where they are likely to be or how many will be filtered? Slicing the Params thing directly, without using Filter, can do it: void main() { alias P = Parameters!foo[1 .. $]; alias P2 = Parameters!foo[0 .. 1]; pragma(msg, P); alias F1 = ReturnType!foo function(P, P2); pragma(msg, F1); } So if you can make a helper function to find the indexes for you, you can call that and do these slices, then put them back together when defining the new function. But doing this when you don't know how many you are slicing is tricky. I suppose it could be written recursively; do one iteration of this technique for each param to be removed, then if there's another to go, run it again on the generated function until you get to what you want. (this is kinda how Filter works inside anyway, just I don't think the scopeness there is preserved through one of its non-function middle steps. Keeping the params inside a function for each middle step will sidestep this.... I think). Just I haven't actually tried that and imo it would be much prettier if you can just slice out one block lol.
Jun 06 2019
prev sibling parent reply Mike Franklin <slavo5150 yahoo.com> writes:
On Thursday, 6 June 2019 at 09:45:08 UTC, Ivan Butygin wrote:
 I want to extract function parameters types from declaration, 
 remove some of them and construct new function pointer type 
 from remaining types. Doing this in obvious way cause
 parameters to lost their scope storage class: 
 https://run.dlang.io/is/cQBFRW
I don't understand your example that well, but are you perhaps encountering this issue? https://forum.dlang.org/post/yixvtosbkuphnvxydtxr forum.dlang.org
Jun 06 2019
parent Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Thursday, 6 June 2019 at 11:26:20 UTC, Mike Franklin wrote:
 On Thursday, 6 June 2019 at 09:45:08 UTC, Ivan Butygin wrote:
 I want to extract function parameters types from declaration, 
 remove some of them and construct new function pointer type 
 from remaining types. Doing this in obvious way cause
 parameters to lost their scope storage class: 
 https://run.dlang.io/is/cQBFRW
I don't understand your example that well, but are you perhaps encountering this issue? https://forum.dlang.org/post/yixvtosbkuphnvxydtxr forum.dlang.org
The issue is that `ref`, `scope`, `return`, etc. are parameter storage classes and not type constructors. It is unrelated to your post.
Jun 07 2019