diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-09-18 02:05:35 +0100 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2023-09-18 14:12:33 +0300 |
| commit | 9ea2076663730ab6ac9cad5cb5f84e58198d4d95 (patch) | |
| tree | 2069985d954463dd2e9ef9309d62b44640b8fe99 /lib/libcxx/src/valarray.cpp | |
| parent | d2a937838e26ad0bb380b843dee9781a96a01ef5 (diff) | |
| download | zig-9ea2076663730ab6ac9cad5cb5f84e58198d4d95.tar.gz zig-9ea2076663730ab6ac9cad5cb5f84e58198d4d95.zip | |
translate-c: prevent variable names conflicting with type names
This introduces the concept of a "weak global name" into translate-c.
translate-c consists of two passes. The first is important, because it
discovers all global names, which are used to prevent naming conflicts:
whenever we see an identifier in the second pass, we can mangle it if it
conflicts with any global or any other in-scope identifier.
Unfortunately, this is a bit tricky for structs, unions, and enums. In
C, these types are not represented by normal identifers, but by separate
tags - `struct foo` does not prevent an unrelated identifier `foo`
existing. In general, we want to translate type names to user-friendly
ones such as `struct_foo` and `foo` where possible, but we can't
guarantee such names will not conflict with real variable names.
This is where weak global names come in. In the initial pass, when a
global type declaration is seen, `struct_foo` and `foo` are both added
as weak global names. This essentially means that we will use these
names for the type *if possible*, but if there is another global with
the same name, we will mangle the type name instead. Then, when actually
translating the declaration, we check whether there's a "true" global
with a conflicting name, in which case we mangle our name. If the
user-friendly alias `foo` conflicts, we do not attempt to mangle it: we
just don't emit it, because a mangled alias isn't particularly helpful.
Diffstat (limited to 'lib/libcxx/src/valarray.cpp')
0 files changed, 0 insertions, 0 deletions
