* Add pointers to IR and implement mutable parameters.
So this PR does a few different things:
- Adds first-class pointer type in the IR
- Changes the type of get_ptr IR instruction to return a pointer
- Updates the IR verifier to deal with this
- Allows Sway to parse mutable function parameters
- Updates IR generation to generate a pointer for mutable parameters
This means Sway code like `fn mut_prim(mut b: u32)` now generates the
following IR:
```rust
fn mut_prim_0(b !8: mut ptr u64) -> (), !9
```
And functions can take a pointer, like the result of `get_ptr`:
```rust
v2 = get_ptr mut ptr u64 b, ptr u64, 0, !4
v3 = call mut_prim_0(v2), !5
```
Struct mutable parameters are also now working, where previously only
mut self parameters were working.
Part of https://github.com/FuelLabs/sway/issues/1188.
* Add ref keyword for mutable parameters.
* Address PR feedback.
Co-authored-by: Alex Hansen <alex@alex-hansen.com>
* Fix lexing of multiline comments to include the trailing '/'
Previously, both the implementation and the test missed the trailing
slash during lexing. This fixes both, closes#2355 and unblocks #2311.
* Add missing slash to sway-fmt-v2 comment test
* Add int_to_ptr IR instruction
* Simplify codegen for int_to_ptr
* Update a few more things and add a test
* Add a new __gtf intrinsic
* Add a simple test
* Use int_to_ptr to generate asm for __gtf
* Add an ir_generation test
* Update sway-ir/src/verify.rs
Co-authored-by: Toby Hutton <toby@grusly.com>
* Addressing some comments
* Update test/src/e2e_vm_tests/test_programs/should_pass/language/gtf_intrinsic/src/main.sw
Co-authored-by: Vaivaswatha N <vaivaswatha@users.noreply.github.com>
* Update sway-core/src/asm_generation/from_ir.rs
Co-authored-by: Toby Hutton <toby@grusly.com>
* Update sway-ir/src/parser.rs
Co-authored-by: Toby Hutton <toby@grusly.com>
* rename the test.. the old name was wrong
* Updating type checking of gtf to only expect u64s
* fmt
Co-authored-by: Toby Hutton <toby@grusly.com>
Co-authored-by: Vaivaswatha N <vaivaswatha@users.noreply.github.com>
* wip: item_fn handling
* wip fn_item fmt
* more wip pieces of ItemFn
* update item_fn with Ty and write
* updated statement with Ty and write
* update path and pattern with write
* update and add test
* update tests, add spans to expr and trait, and update where clause
* update literal with write
* fix has_where_clause and update_where_clause
* Add `CommentedTokenStream` to `sway_parse`
This doesn't yet collect any comments, but adds the necessary structure
and attempts to preserve the original API and behaviour where possible.
Collecting of comments to be added in a follow-up commit.
* Collect multi-line comments in CommentedTokenStream
* Collect single-line comments in CommentedTokenStream
* Add token_trees and spanned impls for CommentedTokenStream
* Add Spanned impl for CommentedTokenTree. Add comment lexing test.
* Expose `lex_commented` function from root
* Add CommentedTree and CommentedGroup aliases
* Move CommentedTokenTree impl to better location
* Clean up by using CommentedTree type alias where applicable
Co-authored-by: Alex Hansen <alex@alex-hansen.com>
Co-authored-by: Chris O'Brien <57543709+eureka-cpu@users.noreply.github.com>
Add support for raw identifiers and improve reserved keywords checking.
This commit deals with the usage and checking of reserved keywords
as identifiers, for code like:
```
fn main() {
let mut mut = 0;
}
It introduces a new error that checks if an identifier is a reserved
keyword:
```
error
--> /main.sw:4:13
|
2 |
3 | fn main() {
4 | let mut mut = 0;
| ^^^ Identifiers cannot be a reserved keyword.
5 | }
|
____
```
There was an existing issue in the standard library, which
has a library/module named `storage`.
Instead of working around this by renaming it to something else,
an alternative solution with raw identifiers is implemented.
This raw identifier feature is implemented at the lexer level,
and allows you to use keywords as identifiers in places that
generally wouldn't be allowed.
Rust and a bunch of other modern languages also provide this escape
hatch, and it seemed the simplest solution for me to handle the issue.
It activates by declaring an identifier prefixed with `r#`, just like
Rust.
The complexity on the codebase to support this feature is pretty
minimal, but if there any objections to this, I can easily remove it,
but some other solution to the issue above will need to be figured out.
Closes https://github.com/FuelLabs/sway/issues/1996.
Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
* wip
* add unit test
* update tests
* updated unimplemented cases to return span
* test now passes incorrectly
* update AttributeDecl::format()
* update test comment
* add close paren
* update Annotated for consistency
* chng return type for Annotated
* remove test and add todos
* Improve struct patterns with new warnings and rest pattern support.
This commit improves a couple aspects of handling of struct patterns.
First of all, it adds semantic checking (with a new warning) when
patterns are missing usage of all fields:
```
error
--> src/main.sw:15:9
|
13 |
14 | let z = match p {
15 | Point { x } => { x },
| ^^^^^^^^^^^ Pattern does not mention field: y
16 | };
|
____
```
Then it adds support for rest pattern, "..", which can be used as the
last token in a pattern to not have to specify all the struct fields.
The semantic AST model was updated to support modeling this pattern,
and further type checking was added to the code.
There is also a new warning for when the rest pattern doesn't appear as
the last location, or when it appears multiple times:
```
error
--> src/main.sw:17:20
|
15 |
16 | let z = match p {
17 | Point { x, .., .. } => { x },
| ^^ Unexpected rest token, must be at the end of
pattern.
18 | };
|
____
```
And lastly, tests were added to cover the changes and new functionality.
Closes https://github.com/FuelLabs/sway/issues/1568.
* Do not use an underscored identifier.
* Add some more pattern matching tests.
* Port to new config-driven tests.
Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>