How can we make it easier to experiment with the compiler?

Walter Bright newshound2 at digitalmars.com
Mon May 24 22:18:35 UTC 2021


On 5/24/2021 3:51 AM, Iain Buclaw wrote:
> For instance, how else would we be able to infer `isReturnOnStack` without a 
> `TypeFunction`?
Challenge accepted!

Let's see. The only things the TypeFunction for are:

1. the return type
2. the function linkage
3. if the function returns a ref

Pass those in instead, and the need for TypeFunction goes away.

https://github.com/dlang/dmd/blob/master/src/dmd/target.d#L762

(1) can be further broken down into "is it a POD", etc.

Breaking all this info out of a TypeFunction takes some code, but this 
"decomposition" can be done by a wrapper function in another module.

The end result is target.d can be completely independent of the compiler's 
internal AST structures.

But wait! There's more!

Notice how isReturnOnStack depends on several random global variables like os, 
is64bit, and isPOSIX. They can be passed in as arguments, too (or passed in as a 
const ref to a struct containing those as members).

isReturnOnStack() then becomes a pure function! (and safe, nogc, nothrow, all 
that good stuff)

Those initialize() functions go away, too.

The beauty now becomes that we can (at last!) easily and correctly write 
unittests for it. target.d now becomes INDEPENDENT of the rest of the compiler.

How sweet it will be!


More information about the Digitalmars-d mailing list