Add rule hint header - #303
Closed
joaotgouveia wants to merge 1 commit into
Closed
Conversation
joaotgouveia
force-pushed
the
rule-hints
branch
from
August 15, 2026 21:56
0710871 to
8ad78ae
Compare
Contributor
|
This PR is too big. I think it's better to split it in:
I have some comments about 4.:
I was thinking that maybe you can use a mapping similar to |
Contributor
|
Also, after #302 is merged, you should modify the docs about cpp-rule-preprocessor and rules |
Contributor
Author
|
I'll close this and split it into smaller PRs then |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously,
cpp-rule-preprocessorworked by synthesizing the template arguments required by a given template rule and forcing its instantiation, sidestepping type checking. In the presence of hints, the synthesized types would inherit the hints, which also introduced problems. This solution was neither robust nor easily extensible.This PR adds a header containing hints that can be used by rule authors and refactors how the preprocessor works. Each template rule is now directly instantiated using the appropriate
SemaAPIs. Additionally, compilation errors are no longer suppressed, a compilation error that arises during preprocessing now means that a rule is malformed. This makes the preprocessor much simpler and more robust.The template arguments used to instantiate each rule are created based on the provided hints. If a template parameter does not have a default, a POD is synthesized. If a parameter has a default, that default is wrapped in a type alias and used directly.
The following rule:
Is instantiated by the preprocessor by synthesizing the following:
These changes to the preprocessor mean that, going forward, any rule that cannot be instantiated using a POD must use an appropriate hint.
Hints that correspond to classes are printed in their generic form,
T<digit>, by attaching a PreferredName attribute to the declaration. This does not work for hints defined as builtin types. These hints are instead printed asT<digit>by havingnormalizeQualTypesearch for a corresponding typedef in the enclosing namespace. This lookup is only performed forSubstTemplateTypeParmTypetypes, which ensures that there are no collisions between builtin hints and actual builtin types spelled in the rule. This is tested by thestd::byterules this patch adds.Hints that expose inner type aliases, such as
Iterator, must be parameterized over those types. Otherwise, these types are leaked into the IR. For instance, a rule such as the following:Is currently represented as
T2 & std::reverse_iterator<T1>::operator*()const in the IR. If we were to use a fixed type for theIteratorhint'sreferencetype alias, such asusing reference = long &, this rule would be incorrectly printed aslong & std::reverse_iterator<T1>::operator*() const. This is tested by thestd::reverse_iteratorrules added by this patch. If a given rule does not rely on an inner type alias, the extra template parameter can be omitted:The
__COUNTER__macro is used to ensure that every use of a given hint in a rule acts as a unique type, reflecting the intended semantics when declaring multiple template parameters.I'm working on a utility that automatically generates these
src.cppfiles. Hints are defined using a set of macros that ensure each hint is properly annotated with the information required by this synthesizer. The structs and typedefs inside theSynthesisnamespace are also used by this utility. These constructs were included in the file to avoid having to duplicaterule_hints.hin my project. All remaining synthesizer-specific helpers are kept separate.Additionally, this patch was tested by confirming that the pre-existing IR remained unchanged. These changes to the preprocessor allow it to preprocess 1911 automatically generated rules.
Other changes:
Mapper::ToString.whereclauses. Previously, the preprocessor iterated over each predicate in the clause and added any types in it to the list of generics. This meant that, for clauses such aswhere u8: std::ops::Shl<T1, Output = u8>,u8was incorrectly added as a generic parameter. This patch changes this behavior, types in the where clause that are not present on the generics list are now skipped. I believe this is correct, since awhereclause cannot be used to declare new generics anyway.UnsafeIteratortrait, which is useful for writing rules where we know that a given generic is an iterator that will be translated toPtr.<or>are printed. Printing<or>collided with the search performed byMapper::matchTemplate.