Metaprogramming without templates

Stefan Koch uplink.coder at googlemail.com
Tue Jun 29 14:54:16 UTC 2021


On Tuesday, 29 June 2021 at 14:28:36 UTC, Stefan Koch wrote:
> and the output of this will be
>
> ```D
> void function (void* addr, char* objtype, char* objname, int 
> attribute) __itt_sync_create = () {
>     return ;
> };
> void function (void* addr) __itt_sync_cancel = () {
>     return ;
> };
> void function (void* addr) __itt_sync_acquired = () {
>     return ;
> };
> void function (void* addr) __itt_sync_releasing = () {
>     return ;
> };
> void function (void* addr) __itt_sync_prepare = () {
>     return ;
> };
> ```

Oops as you can see the output the result is not as intended.
The parameters are missing.
That's because we haven't run semantic yet and therefore we have 
to get the parameters from the type.

if you replace the `makeFunctionVariableWithDummyInit` function 
from the previous post with this one:

```D
VariableDeclaration makeFunctionVariableWithDummyInit(const 
FunctionDeclaration fd) {
   auto type = new TypePointer();
   type.nextOf = cast()fd.type;
   auto name = fd.name;

   auto result = new VariableDeclaration();
   result.type = type;
   result.name = name;
   auto func = new FunctionLiteral();

   typeof(func.parameters) parameters = [];
   parameters.length = fd.type.parameterTypes.length;

   foreach(i, p;fd.type.parameterTypes)
   {
    parameters[i] = new typeof(parameters[0])();
    parameters[i].name = p.identifier;
    parameters[i].type = cast()p.type;
   }
   func.parameters = parameters;
   func.fbody = makeDummyBody(fd.type.returnType);
   result._init = func;
   return result;
}
```
The output will be correct:

```D
void function (void* addr, char* objtype, char* objname, int 
attribute) __itt_sync_create = (void* addr, char* objtype, char* 
objname, int attribute) {
     return ;
};
void function (void* addr) __itt_sync_cancel = (void* addr) {
     return ;
};
void function (void* addr) __itt_sync_acquired = (void* addr) {
     return ;
};
void function (void* addr) __itt_sync_releasing = (void* addr) {
     return ;
};
void function (void* addr) __itt_sync_prepare = (void* addr) {
     return ;
};
```


More information about the Digitalmars-d mailing list