Consolidate mutator contexts

Move most of the mutator context methods into BaseModuleContext
so they are available to both top down and bottom up mutators.
Only CreateModule is unique to TopDownMutatorContext, and the
dependency and variation adding methods are unique to the
BottomUpMutatorContext.

The dependency visiting methods are now available on
BottomUpMutatorContext, which requires delaying making newly
added dependencies visible to maintain the invariant that
the mutator has been called on the dependency before the
dependency can be visited by its parents.

Test: m checkbuild
Change-Id: Ie14afc02ac76d0b5a66b0e52de2aa9e17fd1bec0
This commit is contained in:
Colin Cross 2019-03-29 16:35:02 -07:00
parent 818af3bfe0
commit 99bdb2ab4f
2 changed files with 40 additions and 46 deletions

View file

@ -183,12 +183,13 @@ type moduleInfo struct {
properties []interface{}
// set during ResolveDependencies
directDeps []depInfo
missingDeps []string
missingDeps []string
newDirectDeps []depInfo
// set during updateDependencies
reverseDeps []*moduleInfo
forwardDeps []*moduleInfo
directDeps []depInfo
// used by parallelVisitAllBottomUp
waitingCount int
@ -1427,7 +1428,7 @@ func (c *Context) addDependency(module *moduleInfo, tag DependencyTag, depName s
}
if m := c.findMatchingVariant(module, possibleDeps); m != nil {
module.directDeps = append(module.directDeps, depInfo{m, tag})
module.newDirectDeps = append(module.newDirectDeps, depInfo{m, tag})
atomic.AddUint32(&c.depsModified, 1)
return nil
}
@ -1530,7 +1531,7 @@ func (c *Context) addVariationDependency(module *moduleInfo, variations []Variat
Pos: module.pos,
}}
}
module.directDeps = append(module.directDeps, depInfo{m, tag})
module.newDirectDeps = append(module.newDirectDeps, depInfo{m, tag})
atomic.AddUint32(&c.depsModified, 1)
return nil
}
@ -1575,7 +1576,7 @@ func (c *Context) addInterVariantDependency(origModule *moduleInfo, tag Dependen
origModule.Name()))
}
fromInfo.directDeps = append(fromInfo.directDeps, depInfo{toInfo, tag})
fromInfo.newDirectDeps = append(fromInfo.newDirectDeps, depInfo{toInfo, tag})
atomic.AddUint32(&c.depsModified, 1)
}
@ -2149,9 +2150,14 @@ func (c *Context) runMutator(config interface{}, mutator *mutatorInfo,
module.directDeps[j].module = dep.module.splitModules[0]
}
}
// Add in any new direct dependencies that were added by the mutator
module.directDeps = append(module.directDeps, module.newDirectDeps...)
module.newDirectDeps = nil
}
}
// Add in any new reverse dependencies that were added by the mutator
for module, deps := range reverseDeps {
sort.Sort(depSorter(deps))
module.directDeps = append(module.directDeps, deps...)

View file

@ -121,6 +121,7 @@ type DynamicDependerModule interface {
}
type BaseModuleContext interface {
Module() Module
ModuleName() string
ModuleDir() string
ModuleType() string
@ -147,19 +148,6 @@ type BaseModuleContext interface {
error(err error)
Namespace() Namespace
}
type DynamicDependerModuleContext BottomUpMutatorContext
type ModuleContext interface {
BaseModuleContext
OtherModuleName(m Module) string
OtherModuleDir(m Module) string
OtherModuleSubDir(m Module) string
OtherModuleType(m Module) string
OtherModuleErrorf(m Module, fmt string, args ...interface{})
OtherModuleDependencyTag(m Module) DependencyTag
GetDirectDepWithTag(name string, tag DependencyTag) Module
GetDirectDep(name string) (Module, DependencyTag)
@ -168,7 +156,21 @@ type ModuleContext interface {
VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
VisitDepsDepthFirst(visit func(Module))
VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
WalkDeps(visit func(child, parent Module) bool)
WalkDeps(visit func(Module, Module) bool)
OtherModuleName(m Module) string
OtherModuleDir(m Module) string
OtherModuleSubDir(m Module) string
OtherModuleType(m Module) string
OtherModuleErrorf(m Module, fmt string, args ...interface{})
OtherModuleDependencyTag(m Module) DependencyTag
OtherModuleExists(name string) bool
}
type DynamicDependerModuleContext BottomUpMutatorContext
type ModuleContext interface {
BaseModuleContext
ModuleSubDir() string
@ -199,6 +201,10 @@ func (d *baseModuleContext) moduleInfo() *moduleInfo {
return d.module
}
func (d *baseModuleContext) Module() Module {
return d.module.logicModule
}
func (d *baseModuleContext) ModuleName() string {
return d.module.Name()
}
@ -345,6 +351,11 @@ func (m *baseModuleContext) OtherModuleDependencyTag(logicModule Module) Depende
return nil
}
func (m *baseModuleContext) OtherModuleExists(name string) bool {
_, exists := m.context.nameInterface.ModuleFromName(name, m.module.namespace())
return exists
}
// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
// name, or nil if none exists. If there are multiple dependencies on the same module it returns
// the first DependencyTag.
@ -563,45 +574,27 @@ type mutatorContext struct {
newModules []*moduleInfo // brand new modules
}
type baseMutatorContext interface {
type BaseMutatorContext interface {
BaseModuleContext
OtherModuleExists(name string) bool
Rename(name string)
Module() Module
}
type EarlyMutatorContext interface {
baseMutatorContext
BaseMutatorContext
CreateVariations(...string) []Module
CreateLocalVariations(...string) []Module
}
type TopDownMutatorContext interface {
baseMutatorContext
OtherModuleName(m Module) string
OtherModuleDir(m Module) string
OtherModuleSubDir(m Module) string
OtherModuleType(m Module) string
OtherModuleErrorf(m Module, fmt string, args ...interface{})
OtherModuleDependencyTag(m Module) DependencyTag
BaseMutatorContext
CreateModule(ModuleFactory, ...interface{})
GetDirectDepWithTag(name string, tag DependencyTag) Module
GetDirectDep(name string) (Module, DependencyTag)
VisitDirectDeps(visit func(Module))
VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
VisitDepsDepthFirst(visit func(Module))
VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
WalkDeps(visit func(Module, Module) bool)
}
type BottomUpMutatorContext interface {
baseMutatorContext
BaseMutatorContext
AddDependency(module Module, tag DependencyTag, name ...string)
AddReverseDependency(module Module, tag DependencyTag, name string)
@ -791,11 +784,6 @@ func (mctx *mutatorContext) ReplaceDependencies(name string) {
mctx.replace = append(mctx.replace, replace{target, mctx.module})
}
func (mctx *mutatorContext) OtherModuleExists(name string) bool {
_, exists := mctx.context.nameInterface.ModuleFromName(name, mctx.module.namespace())
return exists
}
// Rename all variants of a module. The new name is not visible to calls to ModuleName,
// AddDependency or OtherModuleName until after this mutator pass is complete.
func (mctx *mutatorContext) Rename(name string) {