257 Commits

Author SHA1 Message Date
Veikka Tuominen
7ec729b3ae aro-translate-c: move shared types to a common namespace 2023-10-01 23:51:54 +03:00
Veikka Tuominen
31ecf75311 aro-translate-c: translate enums 2023-10-01 23:51:54 +03:00
Techatrix
2adb932ad6 translate-c: convert clang errors messages into std.zig.ErrorBundle 2023-09-25 18:10:44 +03:00
Jay Petacat
f91ff9a746 translate-c: Struct fields default to zero value
C99 introduced designated initializers for structs. Omitted fields are
implicitly initialized to zero. Some C APIs are designed with this in
mind. Defaulting to zero values for translated struct fields permits Zig
code to comfortably use such an API.

Closes #8165
2023-09-18 23:09:47 -07:00
mlugg
9ea2076663 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.
2023-09-18 14:12:33 +03:00
mlugg
0fa8cf44f6
translate-c: do not translate macros which use arguments as struct/union/enum names
Consider this C macro:
```c
#define FOO(x) struct x
```

Previously, translate-c did not detect that the `x` in the body referred
to the argument, so wrongly translated this code as using the
nonexistent `struct_x`. Since undefined identifiers are noticed in
AstGen, this prevents the translated file from being usable at all.

translate-c now instead detects this case and emits an appropriate
compile error in the macro's place.
2023-09-17 12:41:11 +01:00
Evan Haas
49cb1683ff translate-c: Use canonical type of field for flexible array definition
This prevents incorrectly casting an ElaboratedType to ArrayType
if the flexible field is a typedef'ed zero-length array

Fixes #16838
2023-08-15 23:59:07 -07:00
Evan Haas
bdf5739c0a translate-c: Flexible array members must be the last field in a struct
Previously any zero-length or incomplete array within a struct was treated
as a flexible array member.
2023-08-15 23:59:07 -07:00
Jacob G-W
3c08fe931a make @typeInfo not return private decls
fixes #10731
Thanks @nektro for previous work in #14878

This change creates a small breaking change:
It removes the `is_pub` field of a decl in `@typeInfo`
2023-07-25 16:19:08 -07:00
antlilja
16d17addcb Update translate-c to new splat syntax 2023-07-12 15:35:57 -07:00
Evan Haas
0a6cd257b9 translate-c: Use @constCast and @volatileCast to remove CV-qualifiers
Instead of converting a pointer to an int and then back to a pointer.
2023-06-29 23:36:56 +03:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
mlugg
447ca4e3ff translate-c: update to new cast builtin syntax 2023-06-24 16:47:49 -07:00
Eric Joldasov
50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
Eric Joldasov
a6c8ee5231 compiler: rename "@XToY" to "@YFromX", zig fmt: rewrite them
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:24 -07:00
r00ster91
2593156068 migration: std.math.{min, min3, max, max3} -> @min & @max 2023-06-16 13:44:09 -07:00
Evin Yulo
6c2f374556 Use the word 'base' consistently instead of 'radix' 2023-06-01 00:02:16 +03:00
tison
bfe02ff61a
make @boolToInt always return a u1
Signed-off-by: tison <wander4096@gmail.com>
2023-05-24 00:01:48 +00:00
Andrew Kelley
42973d73e6 compiler: use @memcpy instead of std.mem.copy 2023-04-28 13:24:43 -07:00
Tw
131b328c10 translate-c: deduplicate global declaration
Fix #15456

Signed-off-by: Tw <weii.tan>
2023-04-27 11:13:16 +03:00
Casey Banner
bc4d9f3aa9
translate-c: fix codegen when C source has variables named the same as mangling prefixes
If the C code had variables that were named the same as the prefixes used
for name mangling, such as "tmp" or "ref", then the codegen would generate
incorrect code in some cases. This was because these aliases were immediately
visible to expressions that actually needed to use the original name.

I introduced the concept of reserving aliases without enabling them. An alias
that isn't enabled isn't visible to expression translation, but is still
reserved so that sub-expressions generate aliases that don't overlap.

Add test cases to cover the cases that would break before this change.

Co-authored-by: Veikka Tuominen <git@vexu.eu>
2023-04-26 17:30:46 +03:00
kcbanner
42ee364e7b translate-c: support brace-enclosed string initializers (c++20 9.4.2.1) 2023-04-23 21:45:08 +03:00
Andrew Kelley
1ed569e0b2 Merge remote-tracking branch 'origin/master' into llvm16 2023-03-16 17:33:24 -07:00
mlugg
a8bd55e085 translate-c: translate extern unknown-length arrays using @extern
Resolves: #14743
2023-03-12 19:07:06 +02:00
Veikka Tuominen
72443fb88c translate-c: handle more wrapper types in isAnyopaque 2023-03-02 12:48:13 +02:00
Andrew Kelley
b5b634e4e8 Merge remote-tracking branch 'origin/master' into llvm16 2023-02-19 10:20:19 -07:00
Andrew Kelley
aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Andrew Kelley
efdc94c107 Merge remote-tracking branch 'origin/master' into llvm16 2023-02-18 09:33:27 -07:00
John Schmidt
a5d25fabda
translate_c: fix typedeffed pointer subtraction
Closes #14560.
2023-02-10 15:46:29 +02:00
Andrew Kelley
fab9b7110e Merge remote-tracking branch 'origin/master' into llvm16 2023-02-03 12:49:40 -07:00
Evan Typanski
86ec26b1f0 translate-c: Fix types on assign expression bool 2023-02-01 21:45:53 +02:00
Andrew Kelley
b260bf42d3 update clang API to 16 2023-01-26 16:36:13 -07:00
Veikka Tuominen
bc97a5662d Sema: display cimport errors from clang 2022-12-14 14:08:22 +02:00
r00ster91
6b7d9b34e8 api(std.ascii): remove deprecated decls 2022-12-09 21:57:17 +01:00
Andrew Kelley
50eb7983cd remove most conditional compilation based on stage1
There are still a few occurrences of "stage1" in the standard library
and self-hosted compiler source, however, these instances need a bit
more careful inspection to ensure no breakage.
2022-12-06 20:38:54 -07:00
Andrew Kelley
b7b905d227 add behavior test for while(true) not needing else unreachable
closes #707
2022-12-06 17:57:27 -07:00
Andrew Kelley
28514476ef remove -fstage1 option
After this commit, the self-hosted compiler does not offer the option to
use stage1 as a backend anymore.
2022-12-06 12:15:04 -07:00
Veikka Tuominen
44f8714dfb translate-c: cast unsuffixed floats to f64 2022-11-20 16:10:54 +02:00
Nathan Bourgeois
e64eef366c
Translate-C Remainder Macro Fix 2022-11-03 14:07:00 +02:00
Evan Haas
c616141241 translate-c: Better support for division in macros
Perform C-style arithmetic conversions on operands to division operator
in macros

Closes #13162
2022-10-28 17:59:32 -04:00
Veikka Tuominen
4fc944dde8 translate-c: fix redefinition of label on left recursive comma operator
Closes #13239
2022-10-27 01:31:18 +03:00
Veikka Tuominen
d773b7e71f translate-c: cleanup unused parameters 2022-10-27 01:31:18 +03:00
Tau
770a1aa967 translate-c: fix #5305 2022-10-10 22:50:22 +02:00
Tau
6be16eeae9 translate-c: fix the remaining function pointer issues 2022-10-10 22:30:33 +02:00
Techcable
5b689d389f
translate-c: packed struct implies align(1) on every field
Superceeds PR #12735 (now supporting all packed structs in GNU C)
Fixes issue #12733

This stops translating C packed struct as a Zig packed struct.
Instead use a regular `extern struct` with `align(1)`.

This is because (as @Vexu explained) Zig packed structs are really just integers (not structs).

Alignment issue is more complicated. I think @ifreund was the
first to notice it in his comment on PR #12735

Justification of my interpretion of the C(lang) behavior
comes from a careful reading of the GCC docs for type & variable attributes:

(clang emulates gnu's packed attribute here)

The final line of the documentation for __attribute__ ((aligned)) [on types] says:

> When used on a struct, or struct member, *the aligned attribute can only increase the alignment*; in order to decrease it, the packed attribute must be specified as well.

This implies that GCC uses the `packed` attribute for alignment purposes
in addition to eliminating padding.

The documentation for __attribute__((packed)) [on types], states:

> This attribute, attached to a struct, union, or C++ class type definition, specifies that each of its members (other than zero-width bit-fields) is placed to minimize the memory required. **This is equivalent to specifying the packed attribute on each of the members**.

The key is resolving this indirection, and looking at the documentation
for __attribute__((packed)) [on fields (wierdly under "variables" section)]:

> The packed attribute specifies that a **structure member should have the smallest possible alignment** — one bit for a bit-field and one byte otherwise, unless a larger value is specified with the aligned attribute. The attribute does not apply to non-member objects.

Furthermore, alignment is the only effect of the packed attribute mentioned in the GCC docs (for "common" architecture).
Based on this, it seems safe to completely substitute C 'packed' with Zig 'align(1)'.
Target-specific or undocumented behavior potentially changes this.

Unfortunately, the current implementation of `translate-c` translates as
`packed struct` without alignment info.

Because Zig packed structs are really integers (as mentioned above),
they are the wrong interpretation and we should be using 'extern struct'.

Running `translate-c` on the following code:

```c
struct foo {
    char a;
    int b;
} __attribute__((packed));

struct bar {
    char a;
    int b;
    short c;
    __attribute__((aligned(8))) long d;
} __attribute__((packed));
```

Previously used a 'packed struct' (which was not FFI-safe on stage1).

After applying this change, the translated structures have align(1)
explicitly applied to all of their fields AS EXPECTED (unless explicitly overriden).
This makes Zig behavior for `tranlsate-c` consistent with clang/GCC.

Here is the newly produced (correct) output for the above example:

```zig
pub const struct_foo = extern struct {
    a: u8 align(1),
    b: c_int align(1),
};
pub const struct_bar = extern struct {
    a: u8 align(1),
    b: c_int align(1),
    c: c_short align(1),
    d: c_long align(8),
};
```

Also note for reference: Since the last stable release (0.9.1),
there was a change in the language spec
related to the alignment of packed structures.

The docs for Zig 0.9.1 read:
> Packed structs have 1-byte alignment.

So the old behavior of translate-c (not specifying any alignment) was possibly correct back then.

However the current docs read:

> Packed structs have the same alignment as their backing integer

Suggsestive both to the change to an integer-backed representation
which is incompatible with C's notation.
2022-10-01 15:22:10 -07:00
Evan Haas
8e631ee3e7 translate-c: Escape non-ASCII characters that appear in macros
Macro definitions are simply a slice of bytes, which may not be
UTF-8 encoded. If they are not UTF-8 encoded, escape non-printable
and non-ASCII characters as `\xNN`.

Fixes #12784
2022-09-10 01:50:17 +03:00
Veikka Tuominen
44b9a1d031 translate-c: use correct name for failDecl
Closes #12737
2022-09-08 01:00:19 +03:00
Evan Haas
e283a40d17 translate-c: convert tabs to \t in object-like macro string literals
Closes #12549
2022-09-07 14:46:58 +03:00
Veikka Tuominen
c7884af063 translate-c: do not translate packed C structs as packed Zig structs in stage2
Zig's integer backed packed structs are not compatible with C's packed structs.
2022-09-03 03:42:42 +03:00
Evan Haas
bcaa9df5b4 translate-c: Don't add self-defined macros to global name table
A self-defined macro is one of the form `#define FOO FOO`

Those types of macros have never been translated; this change will cause
any macros which refer to them to be translated as `@compileError` instead
of referring to a non-existent identifier.

Closes #12471
2022-08-26 11:24:57 +03:00