Historically, we did not support calling (register-type) within a
module--while there were instances of such calls (even in our core
library!) they were not properly handled in two ways:
1. They weren't added to the right place in the type environment.
2. Their corresponding emitted code wasn't correct.
This commit enables registering types in modules by making a few fixes:
1. Fix the logic in environment mutations -- before, adding a type to an
environment would result in traversing the environment chain
incorrectly. This is now fixed (we traverse modules and retrieve a
type environment only at the end of the traversal).
2. Fix the typedef emission for registered types--it previously emitted
the C code for the type path, not for the type definition (it called
pathToC not tyToC).
This will now allow authors of wrapper libraries to add more structure
to their Carp wrapper APIs.
This commit also adds a test to check the behavior.
* feat: add box templates and box type
This commit adds an implementation of Boxes, memory manged heap
allocated values.
Boxes are implemented as C pointers, with no additional structure but
are treated as structs in Carp. To facilitate this, we need to add them
as a clause to our special type emissions (TypesToC) as they'd otherwise
be emitted like other struct types.
Co-authored-by: Veit Heller <veit@veitheller.de>
* fix: slight memory management fix for Box
Make sure we free the box!
* test: add tests for box (including memory checks)
* Revert "fix: Ignore clang nitpick"
This reverts commit 70ec6d46d4.
* fix: update example/functor.carp
Now that a builtin type named Box exists, the definitions in this file
cause a conflict. I've renamed the "Box" type in the functor example to
remove the conflict.
* feat: add Box.peek
Box.peek allows users to transform a reference to a box into a a
reference to the box's contained value. The returned reference will have
the same lifetime as the box. This function allows callers to manipulate
the value in a box without re-allocation, for example:
```clojure
(deftype Num [val Int])
(let-do [box (Box.init (Num.init 0))]
(Num.set-val! (Box.peek &box) 1)
@(Num.val (Box.peek &box)))
```
This commit also includes tests for Box.peek.
Co-authored-by: TimDeve <TimDeve@users.noreply.github.com>
Co-authored-by: Veit Heller <veit@veitheller.de>
Co-authored-by: Erik Svedäng <erik@coherence.io>
Co-authored-by: TimDeve <TimDeve@users.noreply.github.com>
Adds -c|--emitcname flag to headerparse to always emit the C identifier
in `register` definitions (useful when the register will be in a
module).
Fixes a bug where the kebab case flag would not output C identifiers
making the emitted C identifiers not match with the ones in the headers.
Adds docs entry about headerparse in CInterop doc.
Makes headerparse emit `CChar` instead of `Char` when encountering a
signature containing `char`.
Previously, the forms cast to the type Unit would still result in
variable assignment emissions, which produces invalid C.
Consider the case:
```clojure
;; type of System.exit is (Int -> a)
(defn main []
(the () (System.exit 0)))
```
This previously produced bad variable assignments. It now works as
expected and emits only the function call.
* Bug fix for #1064 and #843
Removes broken fix for #843 in Emit.hs, thus fixing #1064. And then
this commit focuses on fixing the memory management side of things,
so that we don't add deleters for symbols in the left-hand-side of
match case expressions if we are matching on a ref (e.g. using
match-ref).
* Add sumtype memory tests
* fix: Don't pass 'If' to InvalidObj when Obj actually is 'Mod'
* fix: Better error message
* fix: Better error
* fix: Show the name of the module in the error message
* fix: Use `root` to get correct location for InvalidObj error
* fix: keep old xobj for now
* fix: don't instantiate dummy fields for external types
For ANSI C compatibility reasons, we add a dummy field for memberless
types defined in Carp (see commit 59ef5bbf2b). When registering a type
with no fields, `(register-type A [])`, we'd also attempt to set our
dummy field in the Carp generated initializer for the type. However, the
registered type is totally opaque from the perspective of Carp, and we
can't assume it has a field corresponding to our dummy field.
This commit changes our handling of __dummy in initializers to avoid
setting it for registered types.
* feat: automatically implement str and prn for registered types
This commit makes the auto-generated str and prn functions for
registered types implement the str and prn interfaces, removing the need
for users to call implements on these functions explicitly.
It alters the signature of `autoDerive` in Primitives.hs slightly to
make it more flexible (since registered types have no delete or copy
functions that we can add to the implementation lists of these
interfaces).
* docs: add docs on register-type to CInterop.md
The new documentation clarifies the usage of `register-type` and accounts
for the changes in the prior two commits.
* fix: fix function signatures for generic memberless initers
Filter out dummy field arguments.
* docs: Add details about type name overrides to CInterop.md
* docs: clarify that users can implement delete for registered types
These macros apply an operation to the current value of a variable and
then set the variable to the result of the application. They are
effectively sugar for writing `(set! <var> (<op> <var> <val>))` and
should be familiar to those who have programmed in imperative languages
like C.
In Carp, all the underlying operations these macros use are interfaces,
so one can flexibly use them for more than just numeric types.
Example usage:
```clojure
(let-do [dial 0]
;; crank it up to 11!
(while-do (dial < 12)
(++ dial))
dial)
;; expanded
(let-do [dial 0]
;; crank it up to 11!
(while-do (dial < 12)
(set! dial (inc dial)))
dial)
```
This commit builds on the emit-c feature by permitting C typed values to
be used anywhere in Carp code.
For example, if one wants to use the literal C macro `EDOM`:
```clojure
(register EDOM C "EDOM")
(Int.+ 1 EDOM)
=> 34
```
when compiled, this will produce the call:
```c
Int__PLUS(1, EDOM)
```
So it provides a quite flexible means of using C macros directly. It is,
of course, also radically unsafe. Anyone registering and using values of
the C type better be cautious.
One can get pretty crazy with this feature:
```clojure
(register comment-it C "// commented out;")
(Int.+ 1 comment-it)
=> int _11 = Int__PLUS_(1, // commented out;)
int* _12 = &_11; // ref
String _13 = IntRef_str(_12);
```
This commit fixes issue #1302, whereby types defined in modules were not
recognized as valid member types by our validation routines.
We simply need to account for types defined in modules in the global
env, threading the global env along through validation (previously we
only passed the top-level type env, which contains no modules).
A call to sequence in our handling of polymorphics would result in
discarding legitimate interface implementation results in rare cases.
Calling rights instead fixes the issue. We also need to take the union
of the results to ensure we account for cases such as polymorphically
realized interfaces (e.g. the `prn` function for a (Maybe String), other
wise, we don't resolve to concrete polymorphics for types that have
ad hoc polymorphic members.
Fixes#1307
* feat: Add additional ignore macros
This commit adds two new ignore macros, ignore*, which wraps an
arbitrary number of forms in calls to `ignore` and ignore-do, which
wraps an arbitrary number of forms in ignore, then bundles the whole in
a do call, effectively executing each form only for side effects and
ignoring all results.
* docs: Update ignore* docs
Link to `ignore` doc
Co-authored-by: Veit Heller <veit@veitheller.de>
* fix: Call ignore* in ignore-do
ignore-all was an old name that no longer exists!
* test: Add test for ignore-do
Co-authored-by: Veit Heller <veit@veitheller.de>
* refactor: refactor concretize module
This commit primarily refactors the concretize module, breaking out the
local definitions for visit functions into top level functions that are
hopefully easier to change. I've also modified some monadic code in the
interest of terseness.
This commit adds some additional patterns for XObj forms as well.
* refactor: Only export called functions in Concretize
Adds an export list to Concretize so that the module encapsulates those
functions that are only used internally within the module.
* refactor: better names in concretize functions
Clarify the names of variables in visitor type functions.
* refactor: ensure findType returns a type
Adds an additional check to findType that ensures the retrieved binder
is in fact a type and not another object. This is necessary for certain
contexts like type concretization since modules may also be designated
by symbols that refer to types.
* fix: ensure nested polymorphic types are emitted
This commit fixes an issue whereby nested polymorphic types would not be
emitted by the compiler, even though their member functions were
emitted.
In order to support this, we need to update a couple of functions to
take the global environment (to find nested types, which live in
modules) in addition to the top level type environment. Additionally, we
had to update scoring to account for nested names.
fixes#1293
* test: add tests for nested polymorphic types
Adds regression tests to ensure nested polymorphic types are concretized
and emitted correctly.
* refactor: refactor concretize module
This commit primarily refactors the concretize module, breaking out the
local definitions for visit functions into top level functions that are
hopefully easier to change. I've also modified some monadic code in the
interest of terseness.
This commit adds some additional patterns for XObj forms as well.
* refactor: Only export called functions in Concretize
Adds an export list to Concretize so that the module encapsulates those
functions that are only used internally within the module.
* refactor: better names in concretize functions
Clarify the names of variables in visitor type functions.
Previously, there was an edge case whereby types that depended on other
types that were not scored yet and functions that in turn depended on
these types could be assigned the same score. When this happened, we'd
sometimes incorrectly emit functions before the struct types they
depended on.
Since functions are always at the bottom of a type hierarchy (they
always depend on their members), there's a simple fix: we just add 1 to
ensure functions are emitted after type declarations in all possible
cases.
* feat: add machine-info primitive draft
* feat: rename machine-info to structured-info
* fix: use the right primitive name in structured-info error
* refactor: remove unnecessary code