Retrieving the Scope of a statement

Basile B. b2.temp at gmx.com
Tue Apr 21 07:45:23 UTC 2020


On Tuesday, 21 April 2020 at 06:58:47 UTC, Basile B. wrote:
> On Tuesday, 21 April 2020 at 01:52:49 UTC, Basile B. wrote:
>> On Monday, 20 April 2020 at 16:56:51 UTC, Cristian Creteanu 
>> wrote:
>>> Hi!
>>>
>>> I am trying to implement autocomplete in DCD using DMD as a 
>>> library.
>>> [...]
>> [...]
>> to an `AstNode` you CANT use a custom visitor.
>
> Sorry maybe I've been too opiniated.
>
> in DMD, because of the lazy compilation model, visitors usually 
> don't process a whole module so:
>
> - the scope for a statement is the scope that's passed to the 
> `StatementSemanticVisitor` class created when 
> `statementSemantic()` is called for this statement.
> - the scope for an expression is the scope that's passed to the 
> `ExpressionSemanticVisitor` class created when 
> `expressionSemantic()` is called for this expression.
> - the scope for a type is the scope that's passed to the 
> `TypeSemanticVisitor` class created when `typeSemantic()` is 
> called for this type.
>
> From your original message I have the impression that you want 
> to retrieve a scope by a node, and you noticed that this is not 
> always possible. The time where a scope is always linked to a 
> node is when its semantic is run, and it's a class member. So 
> maybe (and only maybe) there's something to do with that fact 
> but as said at first glance I don't see how, without a kind of 
> callback system.

Maybe that adding this in the compiler globals:

   alias OnStatementSemanticStart = void function(Statement, 
Scope*);
   alias OnStatementSemanticDone = void function(Statement, 
Scope*);
   alias OnExpressionSemanticStart = void function(Expression, 
Scope*);
   alias OnExpressionSemanticDone = void function(Expression, 
Scope*);

   OnStatementSemanticStart onStatementSemanticStart;
   OnStatementSemanticDone onStatementSemanticDone;
   OnExpressionSemanticStart onExpressionSemanticStart;
   OnExpressionSemanticDone onExpressionSemanticDone;

and add the handlers in statementSemantic() and 
expressionSemantic()

could help to build a useful enough hash map but without 
significant memory impact on the compiler. Actually just the 
"Done" events would be usefull because of the many lowering done 
during semantics.


More information about the Digitalmars-d mailing list