Jellybaby
Jellybaby

Reputation: 986

How to access module AST from DMD mars.d

In the DMD source the tryMain function runs several optimization passes on the AST. What I am trying to do is grab the AST, run the passes manually and examine the module AST. Does one just hack the tryMain function or are there some hooks specifically for this purpose in the DMD? I would like to keep all the commandline processing that is done beforehand and then grab the AST. I have no objection to DMD continuing on its merry way doing its thing as long as I can get the AST beforehand to examine it.

Upvotes: 1

Views: 82

Answers (2)

CentralCee
CentralCee

Reputation: 40

TL;DR

If you want to work out what the compiler is doing then you will likely have to inject some code into the part of dmd you want to inspect.

The dmd compiler was (if designed at all) not written with a pass-based architecture in mind, beyond some extremely vague notions of semantic 1/2/3, so you may potentially need to tread lightly here.

If you just want to use the AST, the compiler-as-a-library functionality is good enough to get by, you obtain this AST either in ASTBase-form (i.e. just Syntax) or after all semantic analysis and lowering has been performed.

Praxis

For example, for the current implementation of the pointer lifetime checker, there is no "pointer analysis" pass as per se but rather a function call tucked away in the visitor that implements Semantic3.

// Do live analysis
if (global.params.useDIP1021 && funcdecl.fbody && funcdecl.type.ty != Terror &&
    funcdecl.type.isTypeFunction().islive)
{
    oblive(funcdecl)
}

Strange functions lying in ponds distributing semantic analysis is no basis for a compiler…

Upvotes: 1

Imperatorn
Imperatorn

Reputation: 272

You can get the AST, ask this question in the forum.dlang.org, Discord (https://discord.gg/bMZk9Q4) or IRC (irc://irc.freenode.net/d) and you will get an answer within minutes <3

Upvotes: 1

Related Questions