remove UnkeyedInstanceShim from ref transformer

Since references are always within the scope of a single module, and we
can connect all module instance outputs for proper ordering, the
existing transformer works directly with only module paths as opposed to
module instances.

TODO: TransformApplyReferences for more precise module instance
targeting?
This commit is contained in:
James Bardin 2020-03-04 21:54:47 -05:00
parent 521bdcc241
commit b1df763541
2 changed files with 21 additions and 10 deletions

View File

@ -77,3 +77,17 @@ func (m Module) Call() (Module, ModuleCall) {
Name: callName, Name: callName,
} }
} }
// Ancestors returns a slice containing the receiver and all of its ancestor
// modules, all the way up to (and including) the root module. The result is
// ordered by depth, with the root module always first.
//
// Since the result always includes the root module, a caller may choose to
// ignore it by slicing the result with [1:].
func (m Module) Ancestors() []Module {
ret := make([]Module, 0, len(m)+1)
for i := 0; i <= len(m); i++ {
ret = append(ret, m[:i])
}
return ret
}

View File

@ -285,7 +285,7 @@ func (m *ReferenceMap) References(v dag.Vertex) []dag.Vertex {
return matches return matches
} }
func (m *ReferenceMap) mapKey(path addrs.ModuleInstance, addr addrs.Referenceable) string { func (m *ReferenceMap) mapKey(path addrs.Module, addr addrs.Referenceable) string {
return fmt.Sprintf("%s|%s", path.String(), addr.String()) return fmt.Sprintf("%s|%s", path.String(), addr.String())
} }
@ -295,7 +295,7 @@ func (m *ReferenceMap) mapKey(path addrs.ModuleInstance, addr addrs.Referenceabl
// //
// Only GraphNodeSubPath implementations can be referenced, so this method will // Only GraphNodeSubPath implementations can be referenced, so this method will
// panic if the given vertex does not implement that interface. // panic if the given vertex does not implement that interface.
func vertexReferenceablePath(v dag.Vertex) addrs.ModuleInstance { func vertexReferenceablePath(v dag.Vertex) addrs.Module {
sp, ok := v.(GraphNodeModulePath) sp, ok := v.(GraphNodeModulePath)
if !ok { if !ok {
// Only nodes with paths can participate in a reference map. // Only nodes with paths can participate in a reference map.
@ -306,11 +306,11 @@ func vertexReferenceablePath(v dag.Vertex) addrs.ModuleInstance {
// Vertex is referenced from a different module than where it was // Vertex is referenced from a different module than where it was
// declared. // declared.
path, _ := outside.ReferenceOutside() path, _ := outside.ReferenceOutside()
return path.UnkeyedInstanceShim() return path
} }
// Vertex is referenced from the same module as where it was declared. // Vertex is referenced from the same module as where it was declared.
return sp.ModulePath().UnkeyedInstanceShim() return sp.ModulePath()
} }
// vertexReferencePath returns the path in which references _from_ the given // vertexReferencePath returns the path in which references _from_ the given
@ -318,7 +318,7 @@ func vertexReferenceablePath(v dag.Vertex) addrs.ModuleInstance {
// //
// Only GraphNodeSubPath implementations can have references, so this method // Only GraphNodeSubPath implementations can have references, so this method
// will panic if the given vertex does not implement that interface. // will panic if the given vertex does not implement that interface.
func vertexReferencePath(v dag.Vertex) addrs.ModuleInstance { func vertexReferencePath(v dag.Vertex) addrs.Module {
sp, ok := v.(GraphNodeModulePath) sp, ok := v.(GraphNodeModulePath)
if !ok { if !ok {
// Only nodes with paths can participate in a reference map. // Only nodes with paths can participate in a reference map.
@ -329,12 +329,12 @@ func vertexReferencePath(v dag.Vertex) addrs.ModuleInstance {
// Vertex makes references to objects in a different module than where // Vertex makes references to objects in a different module than where
// it was declared. // it was declared.
_, path := outside.ReferenceOutside() _, path := outside.ReferenceOutside()
return path.UnkeyedInstanceShim() return path
} }
// Vertex makes references to objects in the same module as where it // Vertex makes references to objects in the same module as where it
// was declared. // was declared.
return sp.ModulePath().UnkeyedInstanceShim() return sp.ModulePath()
} }
// referenceMapKey produces keys for the "edges" map. "referrer" is the vertex // referenceMapKey produces keys for the "edges" map. "referrer" is the vertex
@ -386,11 +386,8 @@ func NewReferenceMap(vs []dag.Vertex) *ReferenceMap {
// an instance key) or as the bare module call itself (the "module" // an instance key) or as the bare module call itself (the "module"
// block in the parent module that created the instance). // block in the parent module that created the instance).
callPath, call := addr.Call() callPath, call := addr.Call()
callInstPath, callInst := addr.CallInstance()
callKey := m.mapKey(callPath, call) callKey := m.mapKey(callPath, call)
callInstKey := m.mapKey(callInstPath, callInst)
vertices[callKey] = append(vertices[callKey], v) vertices[callKey] = append(vertices[callKey], v)
vertices[callInstKey] = append(vertices[callInstKey], v)
} }
} }