Commit Graph

343 Commits

Author SHA1 Message Date
Veikka Tuominen
0274e2f1fd translate-c: check variable types being demoted to opaque 2022-05-29 12:07:29 +03:00
TwoClocks
36b4658752 translate-c: check record fields for opaque demotions 2022-05-29 12:06:37 +03:00
Veikka Tuominen
ee651c3cd3 Revert "reserve correct space for bitfields"
This reverts commit 22cb693889.
2022-05-29 12:04:50 +03:00
TwoClocks
22cb693889 reserve correct space for bitfields 2022-05-28 17:31:26 +03:00
fn ⌃ ⌥
d46973ee4f translate-c: improve error messages when expecting specific tokens
Old:
  unable to translate C expr: expected ')'
New:
  unable to translate C expr: expected ')' instead got '...'
2022-01-17 17:16:48 +02:00
riverbl
aa5514fcf2 translate-c: Fix issues translating macro define of hex float constant
* Fix incorrect result when the first digit after the decimal point is not 0-9 - eg 0x0.ap0
* Fix compiler panic when the number starts with `0X` with a capital `X` - eg 0X0p0
* Fix compiler panic when the number has a decimal point immediately after `0x` - eg 0x.0p0
2022-01-17 16:49:46 +02:00
riverbl
c4681b4889 translate-c: Fix macro define of float constant using scientific notation
Fixes compiler attempting to use null value when translating macro define of float constant using scientific notation with no decimal point
2022-01-12 13:25:50 -05:00
Evan Haas
f4b3f1d602 translate-c: Handle typedef'ed void return type for functions.
Fixes #10356
2022-01-11 12:42:23 -05:00
Isaac Freund
9f9f215305
stage1, stage2: rename c_void to anyopaque (#10316)
zig fmt now replaces c_void with anyopaque to make updating
code easy.
2021-12-19 00:24:45 -05:00
Evan Haas
3fefdc1a0b translate-c: Allow negative denominator in remainder (%) operator
Fixes #10176
2021-11-20 13:52:07 -05:00
Stéphan Kochen
d949180ab0 translate-c: create inline fn for always_inline 2021-10-20 17:58:30 -04:00
Andrew Kelley
6115cf2240 migrate from std.Target.current to @import("builtin").target
closes #9388
closes #9321
2021-10-04 23:48:55 -07:00
Josh Soref
664941bf14
Spelling corrections (#9833)
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2021-09-24 13:39:20 -04:00
Evan Haas
c0e84e3ee0 translate-c: translate valueless macros as empty strings 2021-09-01 17:54:07 -07:00
Evan Haas
f5553bfefc translate-c: Only consider public decls in isBuiltinDefined 2021-09-01 17:54:07 -07:00
Evan Haas
df589eecd6 translate-c: improve handling of undefined identifiers 2021-09-01 17:54:07 -07:00
Evan Haas
89dd2b7ac2 translate-c: emit compileError for undefined identifiers in macros 2021-09-01 17:54:07 -07:00
Veikka Tuominen
be9b490f84 translate-c: remove now unnecessary mangling of primitive type shadowing
Closes #6382
2021-08-29 15:11:38 +03:00
Evan Haas
bc18e93825 translate-c: better codegen for pointer index by int literal
#8589 introduced correct handling of signed (possibly negative) array access
of pointers. Since unadorned integer literals in C are signed, this resulted
in inefficient generated code when indexing a pointer by a non-negative
integer literal.
2021-08-03 08:38:15 +03:00
Evan Haas
98eea963de
translate-c: fix import path in translation failure comment 2021-07-28 08:06:22 -07:00
Evan Haas
dc4fa83dd7
translate-c: add framework for special-casing macros
Some macros (for example any macro that uses token pasting) cannot be
directly translated to Zig, but may nevertheless still admit a Zig
implementation. This provides a mechanism for matching macros against
templates and mapping them to functions implemented in c_translation.zig.

A macro matches a template if it contains the same sequence of tokens, except
that the name and parameters may be renamed. No attempt is made to
semantically analyze the macro. For example the following two macros are
considered equivalent:

```C
```

But the following two are not:

```C
```
2021-07-22 08:09:46 -07:00
Evan Haas
3e67ef5c9f translate-c: Handle underscore when used as an identifier
Use `@` syntax to escape `_` when used as an identifier.

Remove the stage1 astgen prohibition against assigning from `_`

Note: there a few stage1 bugs preventing `_` from being used as an identifier
for a local variable or function parameter; these will be fixed by stage2.
They are unlikely to arise in real C code since identifiers starting with
underscore are reserved for the implementation.
2021-07-19 12:56:23 +03:00
Evan Haas
b5659e0233 translate-c: Don't discard variables unless necessary
Closes #9205
2021-07-05 17:51:53 -04:00
Evan Haas
8a5fb4c248 translate-c: Ensure all local variables and function params are used 2021-06-23 10:47:47 -07:00
Evan Haas
0e7897a9a2 translate-c: Remove usage of extern enum
Translate enum types as the underlying integer type. Translate enum constants
as top-level integer constants of the correct type (which does not necessarily
match the enum integer type).

If an enum constant's type cannot be translated for some reason, omit it.

See discussion https://github.com/ziglang/zig/issues/2115#issuecomment-827968279

Fixes #9153
2021-06-23 08:44:25 +03:00
Veikka Tuominen
699b6cdf01 translate-c: move utility functions to a separate namespace 2021-06-14 20:13:34 +03:00
xackus
aa83cbb697 translate-c: better typename parsing 2021-06-13 16:47:42 +03:00
Veikka Tuominen
6664679c8c translate-c: don't bother with unwrapping pointers
Dereferencing a c pointer implicitly includes an unwrap,
manually adding it just causes bugs.
2021-06-13 15:23:31 +03:00
Evan Haas
ea4a25287e translate-c: better support for static local variables
Don't move static local variables into the top-level scope since this
can cause name clashes if subsequently-defined variables or parameters
in different scopes share the name.

Instead, use a variable within a struct so that the variable's lexical
scope does not change. This solution was suggested by @LemonBoy

Note that a similar name-shadowing problem exists with `extern` variables
declared within block scope, but a different solution will be needed since
they do need to be moved to the top-level scope and we can't rename them.
2021-06-12 23:12:37 +03:00
xackus
b5c117d051 translate-c: fix enums that require c_uint type 2021-06-12 11:55:39 +03:00
Evan Haas
45212e3b33 translate-c: Implement flexible arrays
Fixes #8759
2021-06-11 21:31:39 +03:00
Evan Haas
fa6546ba74 translate-c: properly handle enums used as boolean expressions 2021-06-07 15:51:42 +03:00
Veikka Tuominen
c6a0a4e728 translate-c: support designated initializers in macros 2021-06-01 23:40:43 -04:00
Isaac Freund
608bc1cbd5
stage2: disallow 1.e9 and 0x1.p9 as float literals
Instead require `1e9` and `0x1p9`, disallowing the trailing dot.

This change to the grammar is consistent with forbidding `1.` and `0x1.`
as float literals and ensures there is only one way to do things here.
2021-05-31 19:51:11 +00:00
Evan Haas
d321a4b765 translate-c: Translate FnDecl's that appear within functions 2021-05-23 15:42:36 +03:00
Isaac Freund
563ea60a86
translate-c: use inline keyword instead of callconv(.Inline) 2021-05-20 21:59:46 +02:00
Evan Haas
35c694d614 translate-c: Demote initialization of opaque types
This fixes a segfault in translate-c that would previously occur when
initializing structs with unnamed bitfields, due to a failed assertion in
`transInitListExprRecord`. Unnamed bitfields do not have initializers,
so `transInitListExprRecord` erroneously assumes that `init_count` equals
the number of fields in the record.

Since attempting to initialize an opaque type is a syntax error in Zig,
we can just demote any attempts to initialize them.
2021-05-18 09:04:19 +03:00
Evan Haas
36da8d02b5 translate-c: translate global (file scope) assembly 2021-05-15 11:53:01 +03:00
LemonBoy
ae15022406 translate-c: Fix casting of function pointers
The @ptrCast(X, @alignCast(@alignOf(T), Y)) pattern is only correct if T
is not a function type or a pointer, in that case the @alignOf refers to
the pointer itself and not to the pointee type.
2021-04-25 16:50:41 +02:00
LemonBoy
cf98dfbe22 Remove translate-c test using long double literal
Removed until the rendering logic is adapted to deal with 80bit (and
bigger) floats, those are used sparingly in the wild so it's not much of
a loss.
2021-04-24 20:35:04 +02:00
LemonBoy
be551d85b7 translate-c: Group field access LHS if necessary 2021-04-20 15:16:25 +03:00
Evan Haas
d4d21dd46d translate-c: better handling of int -> enum casts
In std.meta.cast when casting to an enum type from an integer type, first
do a C-style cast from the source value to the tag type of the enum.
This ensures that we don't get an error due to the source value not being
representable by the enum.

In transCCast() use std.meta.cast instead of directly emitting the cast
operation since the enum's underlying type may not be known at translation
time due to an MSVC bug, see https://github.com/ziglang/zig/issues/8003

Fixes #6011
2021-04-15 22:46:22 -04:00
Evan Haas
e761e0ac18 translate-c: wrap switch statements in a while (true) loop
This allows `break` statements to be directly translated from the original C.
Add a break statement as the last statement of the while loop to ensure we
don't have an infinite loop if no breaks / returns are hit in the switch.

Fixes #8387
2021-04-13 11:45:03 -07:00
xackus
e4563860fe translate-c: fix calls with no args in macros 2021-04-04 20:43:13 +03:00
Evan Haas
ab9324e604 translate-c: intcast compound assignment operand if different-sized integer
Use transCCast to cast the RHS of compound assignment if necessary.
2021-03-28 15:21:12 +03:00
Veikka Tuominen
df0f7f4692 translate-c: stop creating unnamed decls for typedefs child types 2021-03-25 16:59:26 +02:00
Evan Haas
dce612ac2b translate-c: Ensure assignments are within a block when necessary
Ensures that if an assignment statement is the sole statement within a
C if statement, for loop, do loop, or do while loop, then when translated
it resides within a block, even though it does not in the original C.

Fixes the following invalid translation:

`if (1) if (1) 2;` -> `if (true) if (true) _ = @as(c_int, 2);`

To this:
```zig
    if (true) if (true) {
        _ = @as(c_int, 2);
    };
```

Fixes #8159
2021-03-22 10:48:08 +02:00
Veikka Tuominen
ac7217e1f5 translate-c: preserve zero fractional part in float literals 2021-03-18 22:46:00 +02:00
Evan Haas
b54514d9dd
translate-c: Use [N:0] arrays when initializer is a string literal (#8264)
* translate-c: Use [N:0] arrays when initializer is a string literal

Translate incomplete arrays as [N:0] when initialized by a string literal.
This preserves a bit more of the type information from the original C program.

Fixes #8215
2021-03-18 14:41:04 +02:00
Evan Haas
715370a10a translate-c: demote usage of un-implemented builtins 2021-03-17 09:06:47 +02:00