Add type hints to mk2rbc

Type hints have the format #RBC# type_hint MY_VAR list
and must be specified at the top of the Makefile. Setting
one will cause that variable to have that type for the
remainder of the Makefile. This can be used where mk2rbc's
type inference detects the wrong type and it must be
manually changed.

Bug: 224601891
Test: go test
Change-Id: I6db2c50056d0298227e1d2801a522adf8bbd1df8
This commit is contained in:
Cole Faust
2022-03-14 14:35:50 -07:00
parent ce73506a85
commit f92c9f2809
3 changed files with 98 additions and 5 deletions

View File

@@ -291,6 +291,12 @@ var presetVariables = map[string]bool{
// addVariable returns a variable with a given name. A variable is
// added if it does not exist yet.
func (ctx *parseContext) addVariable(name string) variable {
// Get the hintType before potentially changing the variable name
var hintType starlarkType
var ok bool
if hintType, ok = ctx.typeHints[name]; !ok {
hintType = starlarkTypeUnknown
}
// Heuristics: if variable's name is all lowercase, consider it local
// string variable.
isLocalVariable := name == strings.ToLower(name)
@@ -301,8 +307,8 @@ func (ctx *parseContext) addVariable(name string) variable {
}
v, found := ctx.variables[name]
if !found {
_, preset := presetVariables[name]
if vi, found := KnownVariables[name]; found {
_, preset := presetVariables[name]
switch vi.class {
case VarClassConfig:
v = &productConfigVariable{baseVariable{nam: name, typ: vi.valueType, preset: preset}}
@@ -310,10 +316,10 @@ func (ctx *parseContext) addVariable(name string) variable {
v = &otherGlobalVariable{baseVariable{nam: name, typ: vi.valueType, preset: preset}}
}
} else if isLocalVariable {
v = &localVariable{baseVariable{nam: name, typ: starlarkTypeUnknown}}
v = &localVariable{baseVariable{nam: name, typ: hintType}}
} else {
vt := starlarkTypeUnknown
if strings.HasPrefix(name, "LOCAL_") {
vt := hintType
if strings.HasPrefix(name, "LOCAL_") && vt == starlarkTypeUnknown {
// Heuristics: local variables that contribute to corresponding config variables
if cfgVarName, found := localProductConfigVariables[name]; found {
vi, found2 := KnownVariables[cfgVarName]