Skip to content

Commit f3a941f

Browse files
committed
Work on PrettifyNames section
1 parent d7cc707 commit f3a941f

1 file changed

Lines changed: 48 additions & 4 deletions

File tree

docs/for-contributors/Generator/name-processing.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
A primary goal of Silk.NET is to provide a first-class .NET experience for the bindings that it provides.
44

55
One such way that Silk.NET achieves this is by transforming native identifiers into identifiers that follow the
6-
Microsoft Framework Design guidelines.
6+
Microsoft Framework Design guidelines. This is the process referred to as "prettification".
77
Of these guidelines, most notable are the guidelines relating to capitalization.
88

99
Naming Guidelines: https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines
@@ -38,17 +38,57 @@ This section explains how names flow through the SilkTouch generator pipeline.
3838
- For example, `[NativeName]` is kept and `[NameAffix]` is removed during the `StripAttributes` mod.
3939
- Tip: Disabling the `StripAttributes` mod can be helpful for debugging unwanted outputs.
4040

41+
## Test cases
42+
43+
The behavior for the name processing pipeline is heavily unit tested.
44+
Please refer to the unit tests for the corresponding section of the codebase to see detailed examples of how
45+
different inputs are transformed into their outputs.
46+
4147
## PrettifyNames
4248

4349
As seen above, `PrettifyNames` is the mod central to name processing.
4450

4551
The goal of this mod is to take all of the names from the generated bindings and transform them in bulk.
4652
This keeps other mods performant and simple, as renaming identifiers is a costly operation
4753
that involves searching the entire project for references to that identifier.
48-
Despite this, `PrettifyNames` also has the goal of remaining dumb and does this
49-
by relying on the generator config for all major decisions.
5054

51-
(TODO: Explain how other mods and the user are supposed to interact with PrettifyNames)
55+
Despite this, `PrettifyNames` also has the goal of remaining dumb and straightforward.
56+
It relies on the generator config for API-specific decisions (eg: removing/reordering affixes, overrides)
57+
and other mods for API-specific annotations (eg: API-specific prefix/suffix conventions).
58+
The rest of the processing (eg: prettification), while complex, is done uniformly.
59+
60+
This allows `PrettifyNames` to focus strictly on the common case, while edge cases are handled elsewhere.
61+
In practice, this works fairly well. Even though the configuration options are limited mostly to how affixes are
62+
handled, affixes are usually where native APIs differ in their naming conventions. Other differences fall outside
63+
the common case and are therefore handled by the generator user or by other mods.
64+
65+
Furthermore, to keep `PrettifyNames` simple and linear, each step takes the output of the previous step,
66+
with no interweaving of logic.
67+
68+
`PrettifyNames` works as follows:
69+
70+
1. All current source code is scraped to gather name information.
71+
2. The names are transformed by a series of name processors.
72+
3. Symbols corresponding to all transformed names are gathered.
73+
4. A symbol-based renamer is used to replace all references to those names with their new versions.
74+
5. Document file names are renamed using the transformed names.
75+
76+
At time of writing, these are the name processors in use:
77+
```cs
78+
var nameProcessors = new INameProcessor[]
79+
{
80+
new HandleOverridesProcessor(cfg.NameOverrides), // Overrides are user configurable
81+
new StripAffixesProcessor(visitor),
82+
new PrettifyProcessor(namePrettifier), // Acronym threshold is user configurable
83+
new ReapplyAffixesProcessor(visitor, namePrettifier, cfg.Affixes), // Affix reapplication is user configurable
84+
new PrefixIfStartsWithNumberProcessor(),
85+
new ResolveConflictsProcessor(visitor, logger),
86+
new OutputFinalNamesProcessor(),
87+
new RemoveUnmodifiedFinalNamesProcessor(),
88+
};
89+
```
90+
91+
For specifics on how these processors and other steps work, it is best to refer to the `PrettifyNames` source code.
5292

5393
## Name Splitting
5494

@@ -69,3 +109,7 @@ by relying on the generator config for all major decisions.
69109
### Affix Categories
70110

71111
(TODO: Exhaustively list each category being used in the generator. Explain which mods add the affix category. Provide examples on what the affixes look like. Provide recommendations on how each affix should be configured (i.e., should match the configuration used by `generator.json`).)
112+
113+
## Symbol-based Renamer
114+
115+
(TODO: Explain how the symbol-based renamer works and why `SymbolFinder.FindReferencesAsync` is not used.)

0 commit comments

Comments
 (0)