How to lookup all definitions of a name
I’m trying to look up all definitions of a name and it works only partially. Example code:
Proc foo(G1 : Graph) { Node n; // 1 Node n; // 2 Foreach(n : G1.Nodes) { // 3 } }
This is how my NaBL looks like:
ForEach(var, iter, filter, body): defines Variable var of type t of graph g in filter, body where iter has type t and t has graph g Decl(t, [var], _): defines Variable var of type t of graph g in subsequent scope where t has graph g
This is how the URIs look for the 3 definitions of n:
n1 = URI( Language("Green-Marl") , [ ID(NablNsVariable(), "n", Unique("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0")) , Subsequent("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0") , ID(NablNsProcedure(), "foo", Unique("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0")) , Anonymous("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0") ] ) n2 = URI( Language("Green-Marl") , [ ID(NablNsVariable(), "n", Unique("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0")) , Subsequent("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0") , Subsequent("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0") , ID(NablNsProcedure(), "foo", Unique("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0")) , Anonymous("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0") ] ) n3 = URI( Language("Green-Marl") , [ID(NablNsVariable(), "n", Unique("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/0")), Anonymous("/Users/vladvergu/Documents/workspaces/greenmarl/foo/foo.gm/1")] )
I use the following strategy to lookup all definitions of a name:
nabl-lookup-lexical-all(|ctx): name -> <nabl-use-subtask-all(|ctx, ns, name, [], All())> uri with uri := <nabl-uri> name; ns := <nabl-uri-namespace> uri
What is happening is the following. At
n2
, the lookup returns the definitions ofn1
andn2
as expected. Atn3
however, the lookup only returns the definition ofn3
and not the definitions forn1
andn2
.How can do this lookup (consistently)? Thank you.
Submitted by Vlad Vergu on 13 December 2013 at 19:36
Issue Log
The issue here is the URI of an external definition. This definition is on purpose in a separate anonymous scope and will never have duplicates in this scope. Aliases connect the actual definitions in the external scopes to the definition in the anonymous scope. However, when you check for duplicates, the annotated URI is used, which is the one in the anonymous scope.
To fix the issue, we need to use the inverse alias here. However, this should happen only for external definitions. A way to fix this, is to use an additional annotation, which includes URIs from the external scopes.
I added low-level queries for URIs:
nabl-uri-local-scopes
maps a list of definition URIs to a set of local scope URIs (URIs of the local scopes of the definitions)nabl-uri-external-scopes
maps a list of definition URIs to a set of external scope URIs (URIs of the scopes where the definitions become visible)nabl-uri-lexical-scopes
maps a list of definition URIs to a set of lexical scope URIs (URIs of all lexical scope levels, where the definitions are visible, including their local scope)You can apply
nabl-uri-lexical-scopes
also to a list of scope URIs to get a set of the surrounding lexical scope URIs. However, this will not include the original scope URIs.I also added low-level task creation strategies:
nabl-insert-import-tasks(|ctx, ns)
inserts tasks for looking up wildcard imports into a list of scope URIs.nabl-resolve-task(|ctx, ns, name, prop*)
creates a short-circuit resolution task for a list of scope URIs.nabl-resolve-all-task(|ctx, ns, name, prop*)
creates a full resolution task for a list of scope URIs.nabl-resolve-all-tasks(|ctx, ns, prop*)
maps a list of scope URIs to a list of full resolution tasks.These low-level strategies can be used to compose individual lookup tasks. I added an example for checking local duplicates to GM.
Thanks. I’m running into an issue though when every definition of a variable is returned twice. Let’s take an example:
Proc b(g1, g2 : Graph, pp : E_P<Bool>(g1), pp : E_P<Bool>(g2)) { }
When calling
<nabl-lookup-lexical-all-others(|ctx)>
on the 2nd pp i get 2 definitions:[def / Variable:"pp"#0 / Procedure:"b"#0 / anon 0 /, def / Variable:"pp"#0 / Procedure:"b"#0 / anon 0 /]
And this is supposedly after removing the definition of the 2nd pp from the results. Before removal the resolution results in 4 definitions:
[def / Variable:"pp"#0 / Procedure:"b"#0 / anon 0 /, def / Variable:"pp"#1 / Procedure:"b"#0 / anon 0 /, def / Variable:"pp"#0 / Procedure:"b"#0 / anon 0 /, def / Variable:"pp"#1 / Procedure:"b"#0 / anon 0 /]
Where does this over-count and how could i get rid of it?
NB: This happens both for things declared in the same scope and in subsequent scope. For example in the following program:
Proc b(g1, g2 : Graph) { N_P<Int>(g1) p; N_P<Int>(g2) p; }
Calling
<nabl-lookup-lexical-all-others(|ctx)>
on the 2nd p also results in two definitions after removal of self:[def / Variable:"p"#0 / subsq 0 / Procedure:"b"#0 / anon 0 /, def / Variable:"p"#0 / subsq 0 / Procedure:"b"#0 / anon 0 /]
By looking at the type of the returned definitions it appears that the definitions are indeed of the others but they appear multiple times for a single definition.
This is probably introduced by multiple occurrences of the same scope URI in the list of URIs passed to the resolution task. Can you try to add a
make-set
before the wildcard import tasks are inserted?
Yes. This fixed it. Thank you. Closing issue again.
For documentation’s sake, I’m pasting below the definition of
nabl-lookup-lexical-all-others(|ctx)
as written by @guwac:nabl-lookup-lexical-all-others(|ctx): name -> task with this := <get-annos; fetch(?Def(_))> name ; uri := <nabl-uri> name ; ns := <nabl-uri-namespace> uri ; scope* := <nabl-uri-all-lexical-scopes> [uri] ; import* := <make-set; nabl-insert-import-tasks(|ctx, ns)> scope* ; resolve := <nabl-resolve-all-task(|ctx, ns, name, [])> import* ; task := <task-create-remove-elem(|ctx, this)> resolve
Log in to post comments