Skip to content

Commit

Permalink
[Codegen] Generate Swift bindings (#3122)
Browse files Browse the repository at this point in the history
* update method hbs template

* track init.hbs

* update part_property.hbs

* complete Swift class template

* organize unit test structure, write simple test for swift method rendering

* implement convenience function for engine creation

* additional test

* adjust test input/outputs

* remove extra newline from template

* add comment

* implement from_grammer function and get_type_str for creating native Swift types

* add get_method_name function for extracting Swift-appropriate name

* fix devcontainer

* add copyright notice to each Rust file

* track Cargo lock and ignore target

* fix src/tests/mod.rs

* init manifest

* impl mapping for ImportInfo

* impl additional TryFrom's for manifest types

* add mappings for ObjectInfo

* cleanup and add comment

* convert G-type to PropertyInfo

* format tabs

* expand g-type conversion to TypeInfo

* handle additional variants

* get rid of TryFrom impl

* implement g-type conversion for MethodInfo

* reorder code

* stard with C header directory lookup

* remove attempt at C header directory processing

* separate impl from manifest file

* remove unused imports

* rename ObjectInfo to StructInfo, update EnumInfo fields

* expand on process_c_header_dir

* continue on process_c_header_dir

* check both structs and enums for methods

* proceed with function decl

* remove prefix for method name

* remove unused found_* variables

* track GStruct

* write process_c_header_dir output to a directory

* use match in convert items

* don't remove prefix of functions

* create output dir if not exists

* display manifest output in yaml

* handle Init and Deinit methods

* handle properties, skip none-exported methods

* handle include header

* handle TWData and TWString explicitly

* adjust serde representation

* add TW_DATA and TW_STRING tags where appropriate

* replicate manifest type variants from grammar

* rename MethodInfo to FunctionInfo

* prefix swift structs with Swift*

* adjust Swift types, implement C type mapping

* complete conversion from manifest to SwiftFunction

* remove tags field on ParamInfo

* rename TWData to Data and TWString to String

* add process_func function

* implement process_struct_methods

* generate property templates

* handle inits

* fix partial inclusion of templates

* include class directly into file tempalte

* experiment with templates more

* add entire template in one file

* expand deinit section

* remove parital templates, cleanup lib.rs

* reorder code in codegen/swift

* add simple CLI interface

* add parser-headers command to cli

* remove warnings

* handle both Const and Mutable for TWSting/TWData

* remove comment

* remove deprecated test

* remove .h suffix on imports

* support enum rendering

* make render_file_info return Result<Option<T>>

* cleanup file template a little

* remove TW prefix from type names and filenames

* add skip tag

* generate header grammer to out/

* remove redundant check

* add RenderInput and RenderOutput structs, generate multiple files for each FileInfo

* rename main manifest parser

* render enums separately in enum/ dir, render enum extensions

* add comments

* track templates

* avoid rendering empty structs/extensions

* cleanup enum skip mechanism

* add Proto typedefs to manifest

* remove imports from FileInfo

* generate Protobuf bindings

* convert first char of function and property names to lowercase

* track proto template

* rename enum dir to Enum

* rename deter_as to defer_as

* strip prefix from enum variant

* add default counter values for enum variants

* setup is_public in enum template rendering

* replace variant tuple with EnumVariantInfo

* add custom handler for TWStellarPassphrase

* add custom handler for TWHRP

* adjust template to enum variant changes

* track markers for enum in C grammer parser

* add value_type field to EnumInfo

* add inherited parents to enums

* tiny formatting addition to enum template

* add 'Address' parent to struct where appropriate

* rename functions

* rename parents to superclasses

* fix markers in enum tests

* render deinits

* fix formatting in extension.hbs

* fix enum generation

* classes are always final, add is_nullable for inits

* handle equality operator

* only classes are final, not structs

* handle enum explicitly

* wrap structs and enums as rawValue

* wrap structs and enums differently, correctly handle optional params

* fix wrappers for return value

* set this.rawValue for underlying C FFI call if non-static

* fix how underlying enum extension is called

* fix template regarding extra comma

* remove static option from properties

* add SwiftSelfParam

* adjust template for extensions

* add temporary handling of HRP object name

* adjust handling of UnsafeRawPointer

* update templates

* remove is_static field from PropertyInfo

* wrap returnin enum accordingly

* distinguish between class and struct when initaiting obj

* unwrap enum result

* add and track typed Swift operations in process_object_properties

* update SwiftProperty struct based on changes

* convert process_object_methods

* cleanup and fix errors

* adjust template to adjusted structure design

* handle skipping self-reference correctly

* adjust init to struct changes

* merge Call and CallDefer

* implement CallOptional

* deprecate TryFrom<ParamInfo> for SwiftParam

* implement support for guarded calls

* remove unused imports

* handle struct and enum returns correctly

* handle structs/enums correctly for properties too

* unwrap enum on return

* fix C FFI call in equal operator

* set is_public for methods in manifest

* skip non-exported inits

* handle optional structs

* guard call for properties

* convert enum variant names to camelCase

* cleanup templates

* add special handler for non-conventional TWStoredKey function names

* handle single void param in C header grammer

* checkout correctly in grammer

* add custom handlers for non-conventional variant names

* add extra ripemd handler

* add special handler for sha512_256

* add special handler for Hash functions

* add special handler for AES

* convert manifest and swift modules into directories

* split logic over files

* deprecate tags, just use bool fields

* remove dbg statements

* skip specific files entirely

* deprecate parser, track manifest directly

* reference variables directly from main

* add IoError and YamlError variants to Error type

* seperate swift codgen into individual files

* clear some TODOs

* implement Display for SwiftType

* comment on properties function and add comments

* add docs to function mapping

* cleanup

* unify C FFI call handler

* format special name handler

* note comment on unwrap

* add rustfmt::skip on special handling

* rename handle_* to param_*

* add a wrapper for return value

* descripte swift operation variants

* add additional comments

* update manifest with TWBase*.yaml

* add proper error types and handlings

* handle unwraps in main

* small fix

* formatting

* add copyright header

* trigger CI

* deprecate tags field from TWBase*.yaml

* update TWDataVector

* expand TWCommonProto

* replace Swift type () with Void

* track TWLiquidStakingProto.yaml

* add string description to TWCurve

* fix outdated path in println

* undo changes to tool and swift

* automatically add year in copyright header in templates

* remove deprecated skip_self field in templates

* only use one call to current_year

* separate string generation from swift type conversion

* don't render empty protos, stip Proto suffix correctly

* reorder structs

* add convenience function pretty_name

* move render functions into render module

* add comments to Swift types

* rename render types/functions

* init tests module, test single_struct, add expected input/output

* test template rendering for with optional types

* test single class

* add tests for private fields

* fix spacing for private properties

* simplify struct tests

* test private enum

* add convenience method for enum comparision

* set options.swift in legacy codegen to false

* add build step for swift codegen-v2 generation

* add test for enum extensions

* add processor for deinits

* test for non-associated properties

* track non-associated properties tests

* remove unnecessary to_string calls

* format

* create function partial for hbs template

* implement function and init partials

* implement partial property

* use partials in enum extensions

* add partial templates to RenderInput

* move templates to swift subfolder

* typo

* convert enum values to strings, use hex values in enums where required

* trigger CI

* add recommended changes by @satoshiotomakan

* update codgen-v2/README.md
  • Loading branch information
lamafab authored May 11, 2023
1 parent 2eee63c commit 0f70467
Show file tree
Hide file tree
Showing 155 changed files with 10,819 additions and 2,975 deletions.
232 changes: 232 additions & 0 deletions codegen-v2/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions codegen-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ path = "src/main.rs"
[dependencies]
serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.95"
serde_yaml = "0.9.21"
handlebars = "4.3.6"
heck = "0.4.1"
17 changes: 4 additions & 13 deletions codegen-v2/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
# About

This is a _work-in-progress_ parser meant to deprecate the existing Ruby parser
in `codegen/`. This project will progess over multiple stages (PRs).

- [x] Write a (minimal) parser with full coverage of the C headers in `include/` ([#3065](https://github.com/trustwallet/wallet-core/pull/3065)).
- [ ] Implement a templating engine, write easy to read templates.
- [ ] Start with Swift codegen, replicate existing API (no breakage).
- [ ] Complete codegen for remaining languages.
- [ ] Extend the parser to handle all valid C header syntax.
- [ ] Add explicit error types (right now it's just `Error::Todo`).
- [ ] Seperate parser logic from C header grammer (no refactoring required, just reorganization).
- [ ] Update the entire building system.
in `codegen/`. As of now, we only support Swift binding generation. This project
will progess over multiple stages (PRs).

## Execution

```bash
$ cd codegen-v2
$ cargo run
$ cargo run -- swift
```

This command reads the C headers in `include/` and generates a
`include_manifest.json` file.
The bindings are saved to `bindings/`.
Loading

0 comments on commit 0f70467

Please sign in to comment.