| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from collections.abc import Iterable |
| 4 |
|
| 5 |
|
| 6 |
class NameGenerator: |
| 7 |
"""Utility for generating distinct C names from Python names. |
| 8 |
|
| 9 |
Since C names can't use '.' (or unicode), some care is required to |
| 10 |
make C names generated from Python names unique. Also, we want to |
| 11 |
avoid generating overly long C names since they make the generated |
| 12 |
code harder to read. |
| 13 |
|
| 14 |
Note that we don't restrict ourselves to a 32-character distinguishing |
| 15 |
prefix guaranteed by the C standard since all the compilers we care |
| 16 |
about at the moment support longer names without issues. |
| 17 |
|
| 18 |
For names that are exported in a shared library (not static) use |
| 19 |
exported_name() instead. |
| 20 |
|
| 21 |
Summary of the approach: |
| 22 |
|
| 23 |
* Generate a unique name prefix from suffix of fully-qualified |
| 24 |
module name used for static names. If only compiling a single |
| 25 |
module, this can be empty. For example, if the modules are |
| 26 |
'foo.bar' and 'foo.baz', the prefixes can be 'bar_' and 'baz_', |
| 27 |
respectively. If the modules are 'bar.foo' and 'baz.foo', the |
| 28 |
prefixes will be 'bar_foo_' and 'baz_foo_'. |
| 29 |
|
| 30 |
* Replace '.' in the Python name with '___' in the C name. (And |
| 31 |
replace the unlikely but possible '___' with '___3_'. This |
| 32 |
collides '___' with '.3_', but this is OK because names |
| 33 |
may not start with a digit.) |
| 34 |
|
| 35 |
The generated should be internal to a build and thus the mapping is |
| 36 |
arbitrary. Just generating names '1', '2', ... would be correct, |
| 37 |
though not very usable. The generated names may be visible in CPU |
| 38 |
profiles and when debugging using native debuggers. |
| 39 |
""" |
| 40 |
|
| 41 |
def __init__(self, groups: Iterable[list[str]], *, separate: bool = False) -> None: |
| 42 |
"""Initialize with a list of modules in each compilation group. |
| 43 |
|
| 44 |
The names of modules are used to shorten names referring to |
| 45 |
modules, for convenience. Arbitrary module |
| 46 |
names are supported for generated names, but uncompiled modules |
| 47 |
will use long names. |
| 48 |
|
| 49 |
If separate is True, assume separate compilation. This implies |
| 50 |
that we don't have knowledge of all sources that will be linked |
| 51 |
together. In this case we won't trim module prefixes, since we |
| 52 |
don't have enough information to determine common module prefixes. |
| 53 |
""" |
| 54 |
self.module_map: dict[str, str] = {} |
| 55 |
for names in groups: |
| 56 |
if not separate: |
| 57 |
self.module_map.update(make_module_translation_map(names)) |
| 58 |
else: |
| 59 |
for name in names: |
| 60 |
self.module_map[name] = name + "." |
| 61 |
self.translations: dict[tuple[str, str], str] = {} |
| 62 |
self.used_names: set[str] = set() |
| 63 |
|
| 64 |
def private_name(self, module: str, partial_name: str | None = None) -> str: |
| 65 |
"""Return a C name usable for a static definition. |
| 66 |
|
| 67 |
Return a distinct result for each (module, partial_name) pair. |
| 68 |
|
| 69 |
The caller should add a suitable prefix to the name to avoid |
| 70 |
conflicts with other C names. Only ensure that the results of |
| 71 |
this function are unique, not that they aren't overlapping with |
| 72 |
arbitrary names. |
| 73 |
|
| 74 |
If a name is not specific to any module, the module argument can |
| 75 |
be an empty string. |
| 76 |
""" |
| 77 |
# TODO: Support unicode |
| 78 |
if partial_name is None: |
| 79 |
return exported_name(self.module_map[module].rstrip(".")) |
| 80 |
if (module, partial_name) in self.translations: |
| 81 |
return self.translations[module, partial_name] |
| 82 |
if module in self.module_map: |
| 83 |
module_prefix = self.module_map[module] |
| 84 |
elif module: |
| 85 |
module_prefix = module + "." |
| 86 |
else: |
| 87 |
module_prefix = "" |
| 88 |
actual = exported_name(f"{module_prefix}{partial_name}") |
| 89 |
self.translations[module, partial_name] = actual |
| 90 |
return actual |
| 91 |
|
| 92 |
|
| 93 |
def exported_name(fullname: str) -> str: |
| 94 |
"""Return a C name usable for an exported definition. |
| 95 |
|
| 96 |
This is like private_name(), but the output only depends on the |
| 97 |
'fullname' argument, so the names are distinct across multiple |
| 98 |
builds. |
| 99 |
""" |
| 100 |
# TODO: Support unicode |
| 101 |
return fullname.replace("___", "___3_").replace(".", "___") |
| 102 |
|
| 103 |
|
| 104 |
def make_module_translation_map(names: list[str]) -> dict[str, str]: |
| 105 |
num_instances: dict[str, int] = {} |
| 106 |
for name in names: |
| 107 |
for suffix in candidate_suffixes(name): |
| 108 |
num_instances[suffix] = num_instances.get(suffix, 0) + 1 |
| 109 |
result = {} |
| 110 |
for name in names: |
| 111 |
for suffix in candidate_suffixes(name): |
| 112 |
if num_instances[suffix] == 1: |
| 113 |
break |
| 114 |
# Takes the last suffix if none are unique |
| 115 |
result[name] = suffix |
| 116 |
return result |
| 117 |
|
| 118 |
|
| 119 |
def candidate_suffixes(fullname: str) -> list[str]: |
| 120 |
components = fullname.split(".") |
| 121 |
result = [""] |
| 122 |
for i in range(len(components)): |
| 123 |
result.append(".".join(components[-i - 1 :]) + ".") |
| 124 |
return result |