mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 07:32:44 +00:00
9ea2076663
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. |
||
---|---|---|
.. | ||
behavior | ||
c_abi | ||
cases | ||
link | ||
src | ||
standalone | ||
assemble_and_link.zig | ||
behavior.zig | ||
cases.zig | ||
cbe.zig | ||
compare_output.zig | ||
compile_errors.zig | ||
gen_h.zig | ||
link.zig | ||
llvm_targets.zig | ||
nvptx.zig | ||
run_translated_c.zig | ||
stack_traces.zig | ||
standalone.zig | ||
tests.zig | ||
translate_c.zig |