Carp/core
Scott Olsen d3f8d83c77
feat: add macros for emitting C compiler directives (#1182)
* feat: Add support for emitting literal C

This commit adds a new Obj type, C, as well as a command for emitting C
literals (which are represented by this object).

This is necessary for circumstances in which Carp's handling of
templates and emission orders prevents interop with C. For example, the
c11 macro static_assert must only take a string literal in its second
position. However, Carp's memory management will typically assign a
string literal (in Carp) to an anonymous variable, which makes them
impossible to use with static_assert. The new emit-c command will
support this use case:

```
(static-assert 0 (Unsafe.emit-c "\"message!\""))
```

The literal string "message!" will be emitted in the compiler's C output
in the position corresponding to the macro call.

We also add a special type for c literals, CTy, to prevent conflating
them with Strings. This helps maintainers define clear boundaries and
express what interop code requires the use of literal C.

Likewise, we need to emit `c_code` to represent this type in C. This
wouldn't be necessary except that Carp sometimes auto-generates
functions that refer to Carp types in their C equivalents, so we need
this for completeness. It is typed as the void pointer.

N.B. That the command is not yet exposed in Carp in this commit.

Thanks to @TimDeve for recommending the name emit-c!

* feat: Add preproc command and add emit-c to Unsafe module

This commit adds the preproc command, which enables users to emit
arbitrary C in the compilers emitted C output.

The C emitted by calls to preproc will be appended to the output after
include directives but prior to any other emissions. One can use it to
call preprocessor directives in C, define helper functions etc.

preproc takes a C type value as an argument and must be used in
conjunction with emit-c. Both functions are added to the Unsafe module
to signal their dangerousness.

One can use `register` in combination with preproc to define code
entirely in Carp and obviate the need of additional header files. For
example:

```
(Unsafe.preproc (Unsafe.emit-c "#define FOO 0"))
(Unsafe.preproc (Unsafe.emit-c "void foo() { printf(\"%d\\n\", 1); }"))

(register FOO Int)
(register foo (Fn [] ()))

(defn main []
  (do (foo)
      (IO.println &(fmt "%d" FOO))))
```

The prior example emits C that defines a FOO macro and foo function,
which are both referenced in the main function emitted by Carps normal
processing. Here's what the output looks like:

```
// .. several other includes emitted by the compiler...

void foo() { printf("%d\n", 1); }

//Types:

// Depth 3
typedef struct {
    union {
    struct {
        Long member0;
    } Just;
    // Nothing
    char __dummy;
    } u;
    char _tag;
} Maybe__Long;
```

The C passed to preproc calls is emitted prior to Carps other emissions,
ensuring the user has access to these definitions before any Carp code
is called.

* docs: Add documentation on emit-c and preproc

* feat: add macros for emitting C compiler directives

This suite of macros uses the `Unsafe.emit-c` and `Unsafe.preproc`
functions to provide macros for emitting common C compiler directives,
such as #ifdef, #define, #pragma and others.
2021-03-09 10:43:44 +01:00
..
Array.carp refactor: use quasiquoting in STDLIB and go through array in quasiquote (#1135) 2021-01-21 06:20:03 +01:00
ArrayExt.carp core: update dcostring for range-or-default 2020-05-24 12:51:11 +02:00
Bench.carp refactor: use quasiquoting in STDLIB and go through array in quasiquote (#1135) 2021-01-21 06:20:03 +01:00
Binary.carp refactor: make Maybe.zip a macro (#1138) 2021-01-19 15:59:37 +01:00
Blitable.carp feat: 'delete' interface (deciding whether a type is managed or not) (#1061) 2020-12-20 21:21:14 +01:00
Bool.carp core: add Bool.zero 2020-06-23 12:26:53 +02:00
Byte.carp core: make bit-* interfaces 2020-07-08 21:11:28 +02:00
carp_bench.h Replace return by pure. (#1009) 2020-11-24 06:09:15 +01:00
carp_binary.h all: various long fixes 2020-04-23 21:50:30 +02:00
carp_bool.h core: do not have short functions on single lines 2019-10-30 11:07:32 +01:00
carp_byte.h core: fix #688 by using the correct formatting macros\n\n(and pray that windows does c99) 2020-02-21 12:28:34 +01:00
carp_char.h Merge 2020-05-11 16:10:35 +02:00
carp_debug.h Preserve includes order in generated output. 2019-10-03 00:23:27 +02:00
carp_double.h Long type to ensure longs are actually 64 bits. 2020-04-22 10:40:06 +02:00
carp_float.h core: do not have short functions on single lines 2019-10-30 11:07:32 +01:00
carp_int.h core: do not have short functions on single lines 2019-10-30 11:07:32 +01:00
carp_io.h Hopefully portable and simple get-line. 2020-05-11 16:08:40 +02:00
carp_long.h Detect __builtin_x_overflow() based on __GNUC__ macro. 2020-05-23 19:03:07 +02:00
carp_memory.h all: various long fixes 2020-04-23 21:50:30 +02:00
carp_pattern.h Remove braces so different clang-formats output the same. 2020-05-30 09:32:50 +02:00
carp_safe_int.h Detect __builtin_x_overflow() based on __GNUC__ macro. 2020-05-23 19:03:07 +02:00
carp_stdbool.h core: remove stdbool dependency 2018-11-17 15:42:36 +01:00
carp_stdint.h fix: remove unused vars in carp_stdint.h (#1165) 2021-02-04 08:35:17 +01:00
carp_string.h fix: Pointer str & prn (#1060) 2020-12-09 06:19:07 +01:00
carp_system.h core: use static array for args 2020-08-24 11:20:52 +02:00
carp_utf8.h Unicode fixes (#994) 2020-11-22 06:45:26 +01:00
Char.carp Add some more calls to implement to make tests pass 2020-05-10 13:32:22 -04:00
Collection.carp Add some more calls to implement to make tests pass 2020-05-10 13:32:22 -04:00
Color.carp Support defining types in modules (BREAKING) (#1084) 2020-12-22 13:27:57 +01:00
Control.carp Fix signature of Control.iterate-until 2020-05-20 23:40:46 -04:00
ControlMacros.carp feat: add deprecation meta (#1136) 2021-01-26 13:22:50 +01:00
Core.carp refactor: use quasiquoting in STDLIB and go through array in quasiquote (#1135) 2021-01-21 06:20:03 +01:00
core.h feat: Add support for emitting literal C (#1178) 2021-03-04 07:29:52 +01:00
Debug.carp refactor: use quasiquoting in STDLIB and go through array in quasiquote (#1135) 2021-01-21 06:20:03 +01:00
Derive.carp docs: fix typo in make-deriver and add implementor note (#1155) 2021-01-27 19:00:31 +01:00
Double.carp Add remaining implements declarations 2020-05-10 22:53:35 -04:00
Dynamic.carp feat: add dynamic Map type (#1168) 2021-02-11 09:12:58 +01:00
Filepath.carp Return reference from unsafe-first/unsafe-last (#1027) 2020-11-27 10:19:06 +01:00
Float.carp fix: mark Float.pi as implementor of pi (#1137) 2021-01-19 10:34:01 +01:00
Format.carp feat: add fstr (#1142) 2021-01-26 06:18:16 +01:00
Function.carp docs: Expands docs around C function pointers (#1038) 2020-12-01 10:44:03 +01:00
Generics.carp core: fix typo in docs for Generis.approx 2020-05-13 16:45:11 +02:00
Gensym.carp refactor: Move code out of Macros.carp into other files (#1014) 2020-11-28 12:53:18 +01:00
Geometry.carp Generics no longer propagated. 2019-09-09 22:08:50 +02:00
GLFW.carp Added some of GLFW constants 2020-06-18 23:19:31 +02:00
Heap.carp Fix all nth usage 2019-10-31 06:23:23 -03:00
Int.carp core: make bit-* interfaces 2020-07-08 21:11:28 +02:00
Interfaces.carp feat: make empty? an interface (#1139) 2021-01-20 09:54:08 +01:00
Introspect.carp feat: add Introspect.arguments (#1163) 2021-02-01 17:03:38 +01:00
IO.carp refactor: use quasiquoting in STDLIB and go through array in quasiquote (#1135) 2021-01-21 06:20:03 +01:00
List.carp feat: add dynamic Map type (#1168) 2021-02-11 09:12:58 +01:00
Long.carp Add support for cross-compilation. 2020-10-10 20:01:18 +02:00
Macros.carp feat: add macros for emitting C compiler directives (#1182) 2021-03-09 10:43:44 +01:00
Map.carp fix: fix multiple small problems with map (#1175) 2021-02-28 22:16:11 +01:00
Maybe.carp core: make NULL Ptr a instead of a 2020-05-18 23:21:16 +02:00
Opaque.carp Update Opaque docs; fix typos 2020-06-18 09:57:28 -04:00
OpenGL.carp Add header/lib for OpenGL on each platform. 2020-06-29 11:57:06 +02:00
Pattern.carp feat: 'delete' interface (deciding whether a type is managed or not) (#1061) 2020-12-20 21:21:14 +01:00
Phantom.carp core: add docs for phantom 2020-06-17 19:06:49 +02:00
Platform.carp feat: Recognize the NetBSD platform. (#1109) 2021-01-03 13:22:56 +01:00
Pointer.carp Pointer: Add utility functions (#1012) 2020-11-30 07:06:04 +01:00
Project.carp refactor: Move code out of Macros.carp into other files (#1014) 2020-11-28 12:53:18 +01:00
Quasiquote.carp refactor: refactor quasiquotation (#1145) 2021-01-24 22:49:51 +01:00
Random.carp feat: make reseeding of Random at startup configurable (#1161) 2021-01-29 17:38:39 +01:00
Result.carp Add some more calls to implement to make tests pass 2020-05-10 13:32:22 -04:00
SafeInt.carp Added --compile-fast to compile with tcc. 2020-05-21 20:04:54 +02:00
SDL_gfx.carp Refactored flags handling (add-pkg). 2019-06-17 09:02:34 +02:00
SDL_image.carp Add support for cross-compilation. 2020-10-10 20:01:18 +02:00
SDL_mixer.carp Merge 2020-05-11 16:10:35 +02:00
SDL_ttf.carp Merge 2020-05-11 16:10:35 +02:00
SDL.carp Window manager flags and events for SDL (#1081) 2020-12-20 12:12:29 +01:00
SDLHelper.h core: fix SDL for newer versions 2020-10-15 13:14:31 +02:00
Sort.carp core: add sort flavors with custom comparators 2018-11-13 11:11:21 +01:00
StaticArray.carp refactor: use quasiquoting in STDLIB and go through array in quasiquote (#1135) 2021-01-21 06:20:03 +01:00
Statistics.carp Fix all nth usage 2019-10-31 06:23:23 -03:00
StdInt.carp Moves StdInt functions with dependency on String into String.carp 2020-10-26 19:14:50 +00:00
String.carp refactor: use quasiquoting in STDLIB and go through array in quasiquote (#1135) 2021-01-21 06:20:03 +01:00
System.carp fix: correct the type of carp-init-globals (#1169) 2021-02-04 08:40:53 +01:00
Test.carp feat: add dynamic Map type (#1168) 2021-02-11 09:12:58 +01:00
Tuples.carp refactor: use quasiquoting in STDLIB and go through array in quasiquote (#1135) 2021-01-21 06:20:03 +01:00
Unit.carp Unit: Add implementations of = (#1017) 2020-11-24 19:31:53 +01:00
Unsafe.carp core: rearrange templates 2020-05-12 21:58:40 +02:00
Vector.carp refactor: use derive in Vector modules (#1141) 2021-01-21 06:19:45 +01:00