Commit Graph

25783 Commits

Author SHA1 Message Date
Daniel Guzman
4610360699 Return error.WouldBlock when connect returns .WSAEWOULDBLOCK 2023-09-18 04:25:52 -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
Banacial
d2a937838e
std.zig.c_translation.zig: fix L suffix with sized ints
Closes #15066
2023-09-18 13:27:32 +03:00
nikneym
ada02ac6fe wasi: update sock_recv and sock_send according to preview1 2023-09-18 00:27:27 -07:00
Andrew Kelley
c970dbdfec LLVM: cache LLVM struct field indexes
This is an optimization to avoid O(N) field index lookups. It's also
nicer in terms of DRY; the only tradeoff is memory usage.
2023-09-18 00:09:30 -07:00
Andrew Kelley
48e2ba3b3c
Merge pull request #17179 from mlugg/fix/translate-c
translate-c fixes
2023-09-17 13:54:35 -07:00
Ryan Liptak
bc626e8b89 Make std.os.getenv always a compile error on Windows
The _environ variable that is populated when linking libc on Windows does not support Unicode keys/values (or, at least, the encoding is not necessarily UTF-8). So, for Unicode support, _wenviron would need to be used instead. However, this means that the keys/values would be encoded as UTF-16, so they would need to be converted to UTF-8 before being returned by `os.getenv`. This would require allocation which is not part of the `os.getenv` API, so `os.getenv` is not implementable on Windows even when linking libc.

Closes https://github.com/ziglang/zig/issues/8456
2023-09-17 11:05:06 -07:00
Loris Cro
c1e94b28a3 autodoc: split json payload per field
this will make s3 re-enable compression for the stdlib's autodoc
and improve loading times (and data usage) for users

alongside this commit the deploy script for the official website is also
being updated
2023-09-17 18:23:21 +02:00
Loris Cro
33f3a4d840 autodoc: rename the doctest section in the frontend to 'Usage Examples' 2023-09-17 18:23:21 +02: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
mlugg
28caaea093
AstGen: allow closure over known-runtime values within @TypeOf
AstGen emits an error when a closure over a known-runtime value crosses
a namespace boundary. This usually makes sense: however, this usage is
actually valid if the capture is within a `@TypeOf` operand. Sema
already has a special case to allow such closure within `@TypeOf` when
AstGen could not determine a value to be runtime-known. This commit
simply introduces analagous logic to AstGen to allow `var`s to cross
namespace boundaries within `@TypeOf`.
2023-09-17 12:41:11 +01:00
Andrew Kelley
f40f81cbfb
Merge pull request #16929 from truemedian/more-http
std.http: handle Expect: 100-continue, improve redirect logic, add Client.fetch for simple requests
2023-09-17 00:41:30 -07:00
dundargoc
5389af2c1c Fix minimum cmake version in README 2023-09-16 22:17:11 -07:00
Stephen Gutekanst
55f0d8b41c std: correct getcontext for Android bionic libc
I have updated Felix's ZigAndroidTemplate to work with the latest
version of Zig and we are exploring adding Android support to Mach engine.

`std.c.getcontext` is _referenced_ but not _used_, and Android's bionic libc
does not implement `getcontext`. `std.os.linux.getcontext` also cannot be
used with bionic libc, so it seems prudent to just disable this extern for now.

This may not be the perfect long-term fix, but I have a golden rebuttal to that:
before I was unable to compile Zig applications for Android, and now I can.

<img width="828" alt="image" src="https://github.com/hexops/mach/assets/3173176/1e29142b-0419-4459-9c8b-75d92f87f822">

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2023-09-16 14:22:09 -07:00
Krzysztof Wolicki
2651363c9d
autodoc: Implement various missing expressions in ex (#17129)
Co-authored-by: Loris Cro <kappaloris@gmail.com>
2023-09-16 17:38:06 +02:00
Krzysztof Wolicki
f2026e7dd6 autodoc: Implement builtin function rendering.
Implement unary ops handling.
Fix getType in main.js
Minor cleanup of builtin function handling.
2023-09-16 17:35:11 +02:00
Krzysztof Wolicki
9a326b22d5 autodoc: Fix styling of the navbar 2023-09-16 17:31:43 +02:00
Krzysztof Wolicki
da28379d6c autodoc: Remove unnecessary Expr tag 2023-09-16 17:30:49 +02:00
Krzysztof Wolicki
8a9aa9e112 autodoc: Handle ref ZIR instruction 2023-09-16 17:30:49 +02:00
mlugg
6df78c3bc1 Sema: mark pointers to inline functions as comptime-only
This is supposed to be the case, similar to how pointers to generic
functions are comptime-only (several pieces of logic already assumed
this). These types being considered runtime was causing `dbg_var_val`
AIR instructions to be wrongly emitted for such values, causing codegen
backends to create a runtime reference to the inline function, which (at
least on the LLVM backend) triggers an error.

Resolves: #38
2023-09-15 21:46:38 -07:00
Andrew Kelley
61b70778bd
Merge pull request #17156 from mlugg/destructure
compiler: implement destructuring syntax
2023-09-15 14:51:52 -07:00
mlugg
94529ffb62 package manager: write deps in a flat format, eliminating the FQN concept
The new `@depedencies` module contains generated code like the
following (where strings like "abc123" represent hashes):

```zig
pub const root_deps = [_]struct { []const u8, []const u8 }{
    .{ "foo", "abc123" },
};

pub const packages = struct {
    pub const abc123 = struct {
        pub const build_root = "/home/mlugg/.cache/zig/blah/abc123";
        pub const build_zig = @import("abc123");
        pub const deps = [_]struct { []const u8, []const u8 }{
            .{ "bar", "abc123" },
            .{ "name", "ghi789" },
        };
    };
};
```

Each package contains a build root string, the build.zig import, and a
mapping from dependency names to package hashes. There is also such a
mapping for the root package dependencies.

In theory, we could now remove the `dep_prefix` field from `std.Build`,
since its main purpose is now handled differently. I believe this is a
desirable goal, as it doesn't really make sense to assign a single FQN
to any package (because it may appear in many different places in the
package hierarchy). This commit does not remove that field, as it's used
non-trivially in a few places in the build runner and compiler tests:
this will be a future enhancement.

Resolves: #16354
Resolves: #17135
2023-09-15 14:04:23 -07:00
mlugg
f366d9f879 compiler: start using destructure syntax 2023-09-15 11:42:08 -07:00
mlugg
ded628e4fa
update zig1.wasm
This is required for the compiler to make use of the new destructuring
syntax.

Signed-off-by: Andrew Kelley <andrew@ziglang.org>
2023-09-15 11:41:46 -07:00
mlugg
aec821c083 langref: apply grammar changes
This applies the changes to the grammar introduced by the new
destructuring syntax, as well as some existing changes which were not
copied into the langref.
2023-09-15 11:34:07 -07:00
mlugg
88f5315ddf compiler: implement destructuring syntax
This change implements the following syntax into the compiler:

```zig
const x: u32, var y, foo.bar = .{ 1, 2, 3 };
```

A destructure expression may only appear within a block (i.e. not at
comtainer scope). The LHS consists of a sequence of comma-separated var
decls and/or lvalue expressions. The RHS is a normal expression.

A new result location type, `destructure`, is used, which contains
result pointers for each component of the destructure. This means that
when the RHS is a more complicated expression, peer type resolution is
not used: each result value is individually destructured and written to
the result pointers. RLS is always used for destructure expressions,
meaning every `const` on the LHS of such an expression creates a true
stack allocation.

Aside from anonymous array literals, Sema is capable of destructuring
the following types:
* Tuples
* Arrays
* Vectors

A destructure may be prefixed with the `comptime` keyword, in which case
the entire destructure is evaluated at comptime: this means all `var`s
in the LHS are `comptime var`s, every lvalue expression is evaluated at
comptime, and the RHS is evaluated at comptime. If every LHS is a
`const`, this is not allowed: as with single declarations, the user
should instead mark the RHS as `comptime`.

There are a few subtleties in the grammar changes here. For one thing,
if every LHS is an lvalue expression (rather than a var decl), a
destructure is considered an expression. This makes, for instance,
`if (cond) x, y = .{ 1, 2 };` valid Zig code. A destructure is allowed
in almost every context where a standard assignment expression is
permitted. The exception is `switch` prongs, which cannot be
destructures as the comma is ambiguous with the end of the prong.

A follow-up commit will begin utilizing this syntax in the Zig compiler.

Resolves: #498
2023-09-15 11:33:53 -07:00
mlugg
50ef10eb49
Sema: add missing compile error for runtime-known const with comptime-only type
When RLS is used to initialize a value with a comptime-only type, the
usual "value with comptime-only type depends on runtime control flow"
error message isn't hit, because we don't use results from a block. When
we reach `make_ptr_const`, we must validate that the value is
comptime-known.
2023-09-15 14:29:57 +01:00
mlugg
6a349648cb
build: don't skip compiler build with -Dno-bin
This option exists for fast iteration during compiler development, since
avoiding codegen (but still performing semantic analysis) causes compile
errors to appear around twice as fast. This option was broken when the
`emit_bin` property on `std.Build.Step.Compile` was removed.

Now, when this option is passed, we add a direct install dependency on
the compile step itself, so that it is always run.
2023-09-15 14:29:54 +01:00
bfredl
1a0e6bcdb1 bpf: remove unhelpful "comptime" and fix union order
Insn.st() can be used with dynamic size just like Insn.stx(), which is
relevant in a code generation context.

using ImmOrReg caused an error as its fields were ordered differently than
Source.
2023-09-15 01:05:52 -07:00
mlugg
cba7e8a4e9 AstGen: do not forward result pointers through @as
The `coerce_result_ptr` instruction is highly problematic and leads to
unintentional memory reinterpretation in some cases. It is more correct
to simply not forward result pointers through this builtin.

`coerce_result_ptr` is still used for struct and array initializations,
where it can still cause issues. Eliminating this usage will be a future
change.

Resolves: #16991
2023-09-15 01:05:02 -07:00
Andrew Kelley
8592c5cdac compiler: rework capture scopes in-memory layout
* Use 32-bit integers instead of pointers for compactness and
  serialization friendliness.
* Use a separate hash map for runtime and comptime capture scopes,
  avoiding the 1-bit union tag.
* Use a compact array representation instead of a tree of hash maps.
* Eliminate the only instance of ref-counting in the compiler, instead
  relying on garbage collection (not implemented yet but is the plan for
  almost all long-lived objects related to incremental compilation).

Because a code modification may need to access capture scope data, this
makes capture scope data long-lived state. My goal is to get incremental
compilation state serialization down to a single pwritev syscall, by
unifying the on-disk representation with the in-memory representation.
This commit eliminates the last remaining pointer field of
`Module.Decl`.
2023-09-15 00:55:07 -07:00
Federico Stra
4f952c7e0e
std.math.log_int: implement integer logarithm without using float math 2023-09-14 19:33:56 +00:00
Ryan Liptak
30e1883834 Add -includes option to zig libc
Prints the detected libc include paths for the target and exits
2023-09-14 11:04:34 -07:00
Ryan Liptak
c9613e3d5c ComptimeStringMap: Add version that takes an equality function
This will allow users to construct e.g. a ComptimeStringMap that uses case-insensitive ASCII comparison.

Note: the previous ComptimeStringMap API is unchanged (i.e. this does not break any existing code).
2023-09-14 11:03:15 -07:00
Rafael Fernández López
69982339ed std: add compile error when using std.os.getenv on the wasi target
`std.process.getEnvMap` or `std.process.getEnvVarOwned` should be used
instead, requiring allocation.
2023-09-14 20:04:35 +03:00
Ryan Liptak
742030a8f2 fs tests: Skip UNC path types in Dir.rename tests
Follow up to https://github.com/ziglang/zig/pull/17136. The `Dir.rename files` test has now also been seen to fail in CI, so now all rename tests are skipped for the UNC path type. This is a heavy handed approach to hopefully get rid of any flakiness related to rename & UNC paths. See https://github.com/ziglang/zig/issues/17134
2023-09-14 16:30:41 +03:00
Jakub Konka
8fb4a4efba
Merge pull request #17146 from ziglang/elf-linker
elf: upstream zld/ELF functionality, part 2
2023-09-14 01:45:23 +02:00
Jonathan Marler
223f62acbd
std.json: add fmt function (#17055)
Adds std.json.fmt which returns a std.fmt Formatter that formats any
given value using std.json.stringify.
2023-09-13 18:24:59 -04:00
Andrew Kelley
0e2f002a7b
Merge pull request #17046 from tiehuis/improve-hash-tests
improve std/hash test coverage
2023-09-13 18:22:56 -04:00
Jakub Konka
59a586a878 cmake: add new zig source files 2023-09-13 22:40:01 +02:00
Jakub Konka
fc86b80b3b elf: correctly handle overflows on non-64bit hosts 2023-09-13 22:38:44 +02:00
Jakub Konka
d4c1e85a13 elf: skip writing non-alloc and zerofill atoms 2023-09-13 21:51:43 +02:00
Jakub Konka
a9f1b994bd elf: allocate locals and globals in objects 2023-09-13 21:51:43 +02:00
Jakub Konka
d37cb60621 elf: re-enable linking compiler_rt 2023-09-13 21:51:43 +02:00
Jakub Konka
ce88df497c elf: do not store Symbol's index in Symbol 2023-09-13 21:51:43 +02:00
Jakub Konka
dbde746f9d elf: parse archives 2023-09-13 21:51:43 +02:00
Jakub Konka
5eef7577d1 elf: handle more relocs with GOT relaxation 2023-09-13 21:51:43 +02:00
Jakub Konka
7a16a97671 x86_64: add simple disassembler interface to the encoder 2023-09-13 21:51:43 +02:00
Jakub Konka
9de0df76a8 elf: allocate .bss section and matching PHDR 2023-09-13 21:51:43 +02:00
Jakub Konka
10a99f8f64
Merge pull request #17141 from ziglang/elf-linker
elf: enable Zig's ELF linker on non-incremental codepaths (LLVM, clang)
2023-09-13 21:49:10 +02:00