robotpy-build icon indicating copy to clipboard operation
robotpy-build copied to clipboard

Automatically Detect Template Types

Open TheTripleV opened this issue 2 years ago • 4 comments

When headers are scanned, the types used in a template should be cached. This can be used automatically fill in the template_params in create-gen.

For example (ctre pro), CoreTalonFX.hpp has a function StatusSignalValue<units::angle::turn_t> &GetPosition(); From this, we know that StatusSignalValue needs a units::angle::turn_t template param. Then, SignalStatusValue.hpp has a function SignalMeasurement<T> GetDataCopy() const {...} From this, we can propogate all of SignalStatusValue's template_params including units::angle::turn_t to SignalMeasurement.

This would require all headers to be scanned before yaml files are written.

TheTripleV avatar Jan 01 '23 19:01 TheTripleV

Also, it would be nice to be able to scan additional headers to look for template usages. For example, in the case of WPILib, I could pass in the path to all wpilib examples and tests. These headers could be scanned purely to look for template params.

TheTripleV avatar Jan 02 '23 00:01 TheTripleV

Took a stab at this. The biggest issue is getting the correct qualname for each type and I don't think that info is available.

TheTripleV avatar Jan 02 '23 19:01 TheTripleV

For the future:

def extract_templated_types(atype: str):
    match = re.fullmatch(r"(.*?)<(.*)>", atype)
    if match:
        template_type = match.group(1)
        template_params = match.group(2)
        yield (template_type.strip(), template_params.strip())
        start = 0
        in_angle_brackets = False
        for i, c in enumerate(template_params):
            if c == '<':
                in_angle_brackets = True
            elif c == '>':
                in_angle_brackets = False
            elif c == ',' and not in_angle_brackets:
                yield from extract_templated_types(template_params[start:i])
                start = i + 1
        yield from extract_templated_types(template_params[start:])

TheTripleV avatar Jan 02 '23 22:01 TheTripleV

You probably should now be able to do this pretty easily with the data from cxxheaderparser.

virtuald avatar Oct 22 '23 00:10 virtuald