Skip to content

Extend cpp-rule-preprocessor supported C++ rules#187

Open
joaotgouveia wants to merge 2 commits into
Cpp2Rust:masterfrom
joaotgouveia:src-parsing-improvements
Open

Extend cpp-rule-preprocessor supported C++ rules#187
joaotgouveia wants to merge 2 commits into
Cpp2Rust:masterfrom
joaotgouveia:src-parsing-improvements

Conversation

@joaotgouveia

Copy link
Copy Markdown
Contributor

Extends the set of rules that cpp-rule-preprocessor can handle.

The biggest additions are createMirrorType and support for adding type hints to IR rules in the form of default template arguments.
One limitation on the kinds of rules that could be processed was that the synthesized PODs did not necessarily exhibit all of the characteristics required to instantiate a given C++ construct.

This patch adds support for specifying default template arguments in rules, which are used as a reference for the characteristics that the synthesized structs must exhibit.
This information is then used by createMirrorType.

When the provided type is a non-template class, the synthesized struct simply inherits from it.
When the provided type is a class template, the synthesized struct becomes an instantiation of a synthesized class template that inherits from it.
Roughly:

// rule with hint
template <typename T1, typename T2 = std::allocator<T1>>
void f1() { (...) }

// what is roughly synthesized:

// POD, same as before
struct T1 {};

// synthesized class template that inherits from the hint
template <typename _Tp>
class Mirror : public std::allocator<_Tp> {};

// instantiation of the synthesized class template
template <>
class Mirror<T1> {};

Using a synthesized class template rather than inheriting directly allows the synthesized struct to match the hinted type structurally.
This is useful when instantiating constructs that perform structural checks, such as:

// bits/alloc_traits.h
static_assert(is_same<
  typename __replace_first_arg<_Tp, typename _Tp::value_type>::type, _Tp>::value,
  "allocator_traits<A>::rebind_alloc<A::value_type> must be A");

These changes enable cpp-rule-preprocessor to parse 479 automatically generated rule sources for std::vector and its specializations.

@nunoplopes

Copy link
Copy Markdown
Contributor

This needs to add a few rules that make use of this functionality.

@joaotgouveia joaotgouveia force-pushed the src-parsing-improvements branch from 93e8653 to 81d131d Compare June 12, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants