addrs: UniqueKey and UniqueKeyer

Many times now we've seen situations where we need to use addresses
as map keys, but not all of our address types are comparable and thus
we tend to end up using string representations as keys instead.

That's problematic because conversion to string uses type information
and some of the address types have string representations that are
ambiguous with one another.

UniqueKey therefore represents an opaque key that is unique for each
functionally-distinct address across all types that implement
UniqueKeyer.

For this initial commit I've implemented UniqueKeyer only for the
Referenceable family of types. These are an easy case because they
were all already comparable (intentionally) anyway. Later commits
can implement UniqueKeyer for other types that are not naturally
comparable, such as any which include a ModuleInstance.

This also includes a new type addrs.Set which wraps a map as a set
of addresses, using the unique keys to ensure that there can be only
one element for each distinct address.
This commit is contained in:
Martin Atkins
2021-07-10 15:18:19 -07:00
parent 7cd333dea1
commit f3a57db293
14 changed files with 223 additions and 0 deletions

View File

@@ -15,6 +15,12 @@ func (c ModuleCall) String() string {
return "module." + c.Name
}
func (c ModuleCall) UniqueKey() UniqueKey {
return c // A ModuleCall is its own UniqueKey
}
func (c ModuleCall) uniqueKeySigil() {}
// Instance returns the address of an instance of the receiver identified by
// the given key.
func (c ModuleCall) Instance(key InstanceKey) ModuleCallInstance {
@@ -79,6 +85,12 @@ func (c ModuleCallInstance) String() string {
return fmt.Sprintf("module.%s%s", c.Call.Name, c.Key)
}
func (c ModuleCallInstance) UniqueKey() UniqueKey {
return c // A ModuleCallInstance is its own UniqueKey
}
func (c ModuleCallInstance) uniqueKeySigil() {}
func (c ModuleCallInstance) Absolute(moduleAddr ModuleInstance) ModuleInstance {
ret := make(ModuleInstance, len(moduleAddr), len(moduleAddr)+1)
copy(ret, moduleAddr)
@@ -118,6 +130,12 @@ func (m ModuleCallOutput) String() string {
return fmt.Sprintf("%s.%s", m.Call.String(), m.Name)
}
func (m ModuleCallOutput) UniqueKey() UniqueKey {
return m // A ModuleCallOutput is its own UniqueKey
}
func (m ModuleCallOutput) uniqueKeySigil() {}
// ModuleCallInstanceOutput is the address of a particular named output produced by
// an instance of a module call.
type ModuleCallInstanceOutput struct {
@@ -139,6 +157,12 @@ func (co ModuleCallInstanceOutput) String() string {
return fmt.Sprintf("%s.%s", co.Call.String(), co.Name)
}
func (co ModuleCallInstanceOutput) UniqueKey() UniqueKey {
return co // A ModuleCallInstanceOutput is its own UniqueKey
}
func (co ModuleCallInstanceOutput) uniqueKeySigil() {}
// AbsOutputValue returns the absolute output value address that corresponds
// to the receving module call output address, once resolved in the given
// calling module.