Skip to content

Commit

Permalink
C style compound literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Feb 14, 2025
1 parent 4bd3dfb commit bcf1105
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 54 deletions.
13 changes: 6 additions & 7 deletions src/content/docs/FAQ/allfeatures.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,12 @@ Runtime type methods: `inner`, `kind`, `len`, `names`, `sizeof`.

### Changed

1. Compound literals use `Type { ... }` rather than `(Type) { ... }`
2. Operator precedence of bit operations is higher than `+` and `-`.
3. Well defined-evaluation order: left-to-right, assignment after expression evaluation.
4. `sizeof` is `$sizeof` and only works on expressions. Use `Type.sizeof` on types.
5. `alignof` is `$alignof` for expressions. Types use `Type.alignof`.
6. Narrowing conversions are only allowed if all sub-expressions is as small or smaller than the type.
7. Widening conversions are only allowed on simple expressions (i.e. most binary expressions and some unary may not be widened)
1. Operator precedence of bit operations is higher than `+` and `-`.
2. Well defined-evaluation order: left-to-right, assignment after expression evaluation.
3. `sizeof` is `$sizeof` and only works on expressions. Use `Type.sizeof` on types.
4. `alignof` is `$alignof` for expressions. Types use `Type.alignof`.
5. Narrowing conversions are only allowed if all sub-expressions is as small or smaller than the type.
6. Widening conversions are only allowed on simple expressions (i.e. most binary expressions and some unary may not be widened)

### Removed

Expand Down
19 changes: 0 additions & 19 deletions src/content/docs/FAQ/changesfromc.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,6 @@ is very common. By offering zero initialization by default this **avoids a whole
but this adds a lot of extra boilerplate.
- C3 also offers a way to opt out of zero-initialization, so the change comes at no performance loss.

##### Compound literal syntax changed

```c
// C style:
call_foo((Foo) { 1, 2, 3 });

// C++ style (1):
call_foo(Foo(1, 2, 3));

// C++ style (2):
call_foo(Foo { 1, 2, 3 });

// C3:
call_foo(Foo { 1, 2, 3 } );

// C3 with inference:
call_foo({ 1, 2, 3 });
```
##### Bitfields replaced by bitstructs

Bitfields are replaced by bitstructs that have a well-defined encapsulating type, and
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/Implementation Details/specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ not `n[array] = 33`. This is a change from C.
Compound literals have the format

```
compound_literal ::= TYPE_IDENTIFIER '(' initializer_list ')'
compound_literal ::= (type) initializer_list
initializer_list ::= '{' (initializer_param (',' initializer_param)* ','?)? '}'
initializer_param ::= expression | designator '=' expression
designator ::= array_designator | range_designator | field_designator
Expand Down
20 changes: 10 additions & 10 deletions src/content/docs/Language Fundamentals/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Expressions have a well-defined evaluation order:

## Compound literals

C3 has C's compound literals, but unlike C's cast style syntax `(MyStruct) { 1, 2 }`,
it uses C++ syntax: `MyStruct { 1, 2 }`.
C3 has C's compound literals:

```c3
struct Foo
{
Expand All @@ -40,7 +40,7 @@ fn void test1(Foo x) { ... }
...
test1(Foo { 1, 2.0 });
test1((Foo){ 1, 2.0 });
```

Arrays follow the same syntax:
Expand All @@ -49,10 +49,10 @@ fn void test2(int[3] x) { ... }
...
test2(int[3] { 1, 2, 3 });
test2((int[3]){ 1, 2, 3 });
```

Note that when it's possible, inferring the type is allowed, so we have for the above examples:
Note that when it's possible, inferring the type is allowed and preferred, so we have for the above examples:
```c3
test1({ 1, 2.0 });
test2({ 1, 2, 3 });
Expand All @@ -65,13 +65,13 @@ Passing a [slice](/language-common/arrays/#slice)
fn void test(int[] y) { ... }
// Using &&
test(&&int[3]{ 1, 2, 3 });
test(&&(int[3]){ 1, 2, 3 });
// Explicitly slicing:
test(int[3]{ 1, 2, 3 }[..]);
test(((int[3]){ 1, 2, 3 })[..]);
// Using a slice directly as a temporary:
test(int[]{ 1, 2, 3 });
test((int[]){ 1, 2, 3 });
// Same as above but with inferred type:
test({ 1, 2, 3 });
Expand All @@ -83,8 +83,8 @@ Passing the pointer to an [array](/language-common/arrays)
fn void test1(int[3]* z) { ... }
fn void test2(int* z) { ... }
test1(&&int[3]{ 1, 2, 3 });
test2(&&int[3]{ 1, 2, 3 });
test1(&&(int[3]){ 1, 2, 3 });
test2(&&(int[3]){ 1, 2, 3 });
```

## Constant expressions
Expand Down
17 changes: 0 additions & 17 deletions src/content/docs/Language Overview/primer.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,6 @@ int a;
int b @noinit;
```

## Compound Literals

Compound literals use C++ style brace initialization, not cast style like in C.
For convenience, assigning to a struct will infer the type even if it's not an initializer.

```c
// C
Foo f = { 1, 2 };
f = (Foo) { 1, 2 };
callFoo((Foo) { 2, 3 });

// C3
Foo f = { 1, 2 };
f = { 1, 2 };
callFoo(Foo{ 2, 3 });
```
## typedef and #define becomes 'def'

`typedef` is replaced by `def`:
Expand Down

0 comments on commit bcf1105

Please sign in to comment.