Retrieving the Scope of a statement

Cristian Creteanu cristian.creteanu at pm.me
Mon Apr 20 16:56:51 UTC 2020


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? 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. 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?


More information about the Digitalmars-d mailing list