robotpy / robotpy-build

(mostly) automated C++ wrapping for Python
BSD 3-Clause "New" or "Revised" License
35 stars 16 forks source link

Automatically Detect Template Types #191

Open TheTripleV opened 1 year ago

TheTripleV commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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:])
virtuald commented 10 months ago

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