Retrieving the Scope of a statement
Basile B.
b2.temp at gmx.com
Tue Apr 21 01:52:49 UTC 2020
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. After parsing & the semantic analysis of a given
> source file, given a location (line & column number), I want to
> retrieve the Scope* that the statement at the given line is
> part of so that I can get the symbol table.
>
> I noticed that every Scope has a pointer to its parent Scope
> (enclosing), but as far as I could see there's no way to access
> inner scopes with the information that there is currently in
> the Scope struct. Would it be ok to add a list of children
> scopes inside the struct?
No I think this is a wrong trail. You could be able to do that
without modifying the compiler.
> So, during semantic analysis, whenever the push method of the
> Scope structure is called I could also push the newly created
> scope inside this list of children?
>
> What I tried to do without being able to access children
> directly was to create a visitor which goes through the entire
> AST until it reaches the statement at the given location.
Yes I think you can do that.
> Once I've found this statement, a solution would be to find a
> Dsymbol inside of it and get its _scope, but for many of these
> symbols (for instance, local variables of functions) this field
> is null. I had a look at visit(VarDeclaration) in dsymbolsem.d
> and the _scope would be set for the variable declaration symbol
> if its parent was an aggregate declaration, but why isn't it
> set for variable declarations local to functions?
>
> Is there any easier way to go about this?
For functions you can try to find the `Scope` when
`ScopeStatement`s are visited because they have `.loc` (the
begining) and `.endloc` information. So if the cursor loc is
within the two values, the scope `sc` of the visitor
(StatementSemanticVisitor.sc) gives you the available symbols at
the cursor pos.
Now technically there might be a problem because I dont see how
you can plug the code to make this in `StatementSemanticVisitor`.
This would require a kind of signal/callback/event system. Since
the `Scope` is not attached to an `AstNode` you CANT use a custom
visitor.
More information about the Digitalmars-d
mailing list