From 2f44f09b6ae3efb57be9583d5972feaf7140cfdd Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 8 Nov 2026 12:55:41 +0100 Subject: [PATCH] Add C and C++ library support import "zlib.h" reads the header (cc -E + a small C declaration parser), links via pkg-config or link "name", and builds .c/.cpp files next to the program. C structs become Dino structs, pointers become pointer types (nil = NULL), T* parameters take variables or lists. Adds dino header, editor completion/hover for C names, docs chapter 13, a raylib example, and tests. Co-Authored-By: Claude Opus 5.5 --- README.md | 9 +- SPEC.md | 23 +- docs/11-cheatsheet.md | 11 +- docs/12-calculator-walkthrough.md | 2 + docs/13-c-libraries.md | 304 ++++++ docs/README.md | 1 + examples/c/bounce.dino | 52 + src/ast.rs | 9 +- src/cheader.rs | 1702 +++++++++++++++++++++++++++++ src/check.rs | 398 ++++++- src/codegen_c.rs | 158 ++- src/driver.rs | 121 +- src/fmt.rs | 7 +- src/hir.rs | 46 +- src/index.rs | 66 +- src/lib.rs | 1 + src/loader.rs | 2 +- src/lsp.rs | 88 +- src/main.rs | 57 + src/parser.rs | 64 +- src/runtime.c | 13 + src/types.rs | 4 + tests/clib.rs | 300 +++++ tests/fmt.rs | 9 + tests/lsp.rs | 30 + tests/run.rs | 11 + 26 files changed, 3418 insertions(+), 70 deletions(-) create mode 100644 docs/13-c-libraries.md create mode 100644 examples/c/bounce.dino create mode 100644 src/cheader.rs create mode 100644 tests/clib.rs diff --git a/README.md b/README.md index b867b01..e794015 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,8 @@ error: age cannot be negative missing is `T?`. Both must be handled. - **Modern basics.** Enums that carry data, exhaustive `match`, generics, interfaces, closures, maps, characters, modules. +- **C and C++ libraries.** `import "raylib.h"` and call it: Dino reads the header itself, + links the library, and builds your own `.c`/`.cpp` files next to the program. - **Tooling included.** A formatter, a language server, and editor files, all in the one `dino` program. @@ -96,17 +98,18 @@ dino run hello.dino | `dino emit-c file.dino` | Print the generated C. | | `dino fmt [--check] path...` | Format code (`--check` only reports). | | `dino lsp` | Start the language server (editors run this for you). | +| `dino header file.h [word]` | Show what Dino can use from a C header. | ## Learn the language The [`docs/`](docs/README.md) folder is a short course, written to be read in order: first steps, values, control flow, functions, lists and maps, structs and enums, errors, -memory, generics and interfaces, modules, and a full cheatsheet. Every example in it is run by +memory, generics and interfaces, modules, C and C++ libraries, and a full cheatsheet. Every example in it is run by the test suite, so what you read is what happens. -The last chapter builds a calculator over five files. It lives in +Chapter 12 builds a calculator over five files. It lives in [`examples/calc`](examples/calc), next to many smaller programs in -[`examples/basics`](examples/basics). +[`examples/basics`](examples/basics). [`examples/c`](examples/c) has a small raylib game. For the exact rules of the language, see [`SPEC.md`](SPEC.md). diff --git a/SPEC.md b/SPEC.md index 2f0b8c6..8983326 100644 --- a/SPEC.md +++ b/SPEC.md @@ -167,7 +167,7 @@ Targets: game dev, CLI tools, desktop apps. from another file (fields are private too unless written `pub x: int`). Each file has its own imports and its own names, so two files may both define `f`. Import cycles and missing files are errors; errors point at the file they are in. Names of module types print with the module - (`utils.Vec2{...}`). Importing a C library (`import c "x.h"`) is not supported yet. + (`utils.Vec2{...}`). C headers: see **C libraries** below. - **Built-in modules**: `os` (`read_file write_file append_file exists exit env read_line`, and the value `args`), `math` (`sqrt pow floor ceil round trunc sin cos tan asin acos atan atan2 exp log log10 hypot min max abs PI E`), `time` (`now sleep`). Text has methods: `upper lower trim split lines fields replace @@ -184,7 +184,7 @@ Targets: game dev, CLI tools, desktop apps. `string(x)` (or `"{c}"`) makes text. `for c in s` / `for i, c in s` walk characters (`i` = byte position, invalid UTF-8 comes out as U+FFFD). `s[i]` is the character starting at byte `i` (panics out of range or in the middle of a character); strings can't be assigned to. Methods `is_digit is_letter is_alnum is_space - is_upper is_lower upper lower` (ASCII plus Latin-1 letters). Not yet: `s.chars()`, `[]char`→string. + is_upper is_lower upper lower` (ASCII plus Latin-1 letters). `s.chars()` gives a `[]char`. Not yet: `[]char`→string. - **Formatter** `dino fmt [--check] ..` (`src/fmt.rs`): text based, so comments and blank lines survive. 4-space indent, spaces around binary operators and after `,` `:`, tight `Point{...}` / map literals and `[]T`, at most one blank line, trailing `//` comments of neighbouring lines aligned. It never moves line breaks (a one-line @@ -194,7 +194,24 @@ Targets: game dev, CLI tools, desktop apps. dependencies): full-text sync, error diagnostics from the real checker (imports read from disk or from open buffers; errors in other files shown at the top of the importing file; UTF-16 columns), whole-file formatting. `editors/vscode` (extension + TextMate grammar), `editors/vim` (syntax), `editors/README.md` (Neovim, Helix, others). - Not yet: hover, go to definition, completion, rename; the compiler reports only the first error. + Also outline, go to definition, hover, completion and signature help from a token index (`src/index.rs`), + including names from C headers. Not yet: references, rename; the compiler reports only the first error. +- **C libraries** (`src/cheader.rs`): `import "zlib.h"` reads the header (no hand-written declarations): it runs + `cc -E -dD` and parses the declarations, enum values and object-like `#define`s from the header and the headers it + includes itself (in a shared folder like /usr/include: the header and what it includes, minus the C library). + Names are used as `zlib.name` (prefix = file name without `.h`; `import z "zlib.h"` picks one); + `import "zlib.h" { a, b }` makes only those names visible, without a prefix. Linking: `pkg-config` with names + guessed from the header path, else `import "x.h" link "x"` (`-lx`). A header next to the program adds `-I` and + builds the `.c`/`.cpp`/`.cc` file with the same name into the program (C++ with `c++`, linked with `-lstdc++`). + Types: C ints -> sized numbers (`int` i32, `long` int, `size_t` u64), `double` float, `char` i8 (takes `'a'`), + `char *` -> string (copied both ways, NULL -> ""), enums -> `i32` constants, plain structs -> Dino structs with the + C field names (`raylib.Color`; converted field by field at calls), other pointers -> pointer types + (`Ty::CPtr`, `stdio.FILE`, `nil` = NULL, `==` compares); `void *` (`pointer`) mixes with every pointer type. + `T *` parameters take a variable (the address is passed; counts as a change for inferred mutation) or a list + (its items); `const T *` also takes any value. Constants: numbers, strings, and struct literals like + `CLITERAL(Color){...}` (emitted as the macro). Programs that import headers are built with `-std=gnu11`. + Not supported (a clear error when called): variadic functions, callbacks, unions/arrays by value, lists of + structs, `long double`, function-like macros. Dino never frees C memory. `dino header x.h [word]` lists what Dino sees. - **String slicing**: `s[a..b]`, `s[a..]`, `s[..b]` (byte positions, bounds checked; panics with "the length is N"). - **Running**: `dino run file.dino a b c` passes `a b c` to the program (`os.args`). - **Example**: `examples/calc/` is a multi-file calculator (tokenizer, parser, evaluator, shared scope); `session.txt` / `session.out` are checked by a test. diff --git a/docs/11-cheatsheet.md b/docs/11-cheatsheet.md index c60c7c2..6820134 100644 --- a/docs/11-cheatsheet.md +++ b/docs/11-cheatsheet.md @@ -119,8 +119,17 @@ import os // built-in: os math time pub func f() {} // visible to other files ``` +## C libraries +```dino +import "zlib.h" // zlib.crc32(...), zlib.Z_OK, types like zlib.gzFile +import "raylib.h" { InitWindow } // only these names, used bare +import "foo.h" link "foo" // when pkg-config doesn't know it (libfoo.so) +var f = stdio.fopen("x.txt", "r") // C pointers: compare with nil, close them yourself +lib.fill(buf, n) // T* parameters take a variable or a list +``` + ## Commands -`dino run f.dino args…` · `dino build f.dino -o out` · `dino check f.dino` · `dino emit-c f.dino` · `dino fmt path` (`--check` to only report) · `dino lsp` (editor server) +`dino run f.dino args…` · `dino build f.dino -o out` · `dino check f.dino` · `dino emit-c f.dino` · `dino fmt path` (`--check` to only report) · `dino lsp` (editor server) · `dino header x.h [word]` (what a C header offers) ## Things that stop the program (exit code 2) Divide by zero · list index/slice out of range · `pop` on an empty list · reading a missing diff --git a/docs/12-calculator-walkthrough.md b/docs/12-calculator-walkthrough.md index 1bb2f4c..cf2b11c 100644 --- a/docs/12-calculator-walkthrough.md +++ b/docs/12-calculator-walkthrough.md @@ -97,3 +97,5 @@ run_line(scope, line) - Add a function `sin(x)` next to `sqrt`. - Add `%` as an operator in the tokenizer, parser and `apply`. - Make `sum` accept any number of arguments. + +Next: [C and C++ libraries](13-c-libraries.md) diff --git a/docs/13-c-libraries.md b/docs/13-c-libraries.md new file mode 100644 index 0000000..122b212 --- /dev/null +++ b/docs/13-c-libraries.md @@ -0,0 +1,304 @@ +# 13. C and C++ libraries + +A huge amount of useful code is written in C: graphics (raylib, SDL), compression (zlib), +databases (SQLite), and the C library that comes with every system. Dino can use it +directly. You don't write any declarations: Dino reads the library's **header file** (the +`.h` file that lists what the library has) and works out the rest. + +## Importing a header + +Put the header's name in quotes. Its functions, types and constants are then used with the +header's name in front, the same way as your own files (`import utils` gives `utils.name`). + +```dino +import "string.h" +import "stdlib.h" + +func main() { + print(string.strlen("hello")) + print(stdlib.abs(-7), stdlib.atoi("42") + 1) +} +``` +```text +5 +7 43 +``` + +The name in front is the file name without `.h`: `"zlib.h"` gives `zlib`, `"SDL2/SDL.h"` +gives `SDL`, `"my-lib.h"` gives `my_lib`. To choose another one, write it before the quotes: +`import z "zlib.h"` gives `z.crc32(...)`. + +## Picking a few names + +When you only want some things from a header, list them in braces. Those names are then +used **without** a prefix, and nothing else from the header is visible: + +```dino +import "math.h" { cbrt, hypot } + +func main() { + print(cbrt(27.0), hypot(3.0, 4.0)) +} +``` +```text +3.0 5.0 +``` + +(`math.h` is the C math library. Dino's own `import math` is usually nicer; this is just a +small example.) + +## How C types look in Dino + +| In C | In Dino | +|------|---------| +| `int`, `unsigned int`, `short`, ... | `i32`, `u32`, `i16`, ... | +| `long`, `long long`, `int64_t` | `int` | +| `size_t`, `unsigned long`, `uint64_t` | `u64` | +| `double` / `float` | `float` / `f32` | +| `char` | `i8` (you can pass a character like `'a'`) | +| `bool` | `bool` | +| `const char *` and `char *` | `string` | +| an `enum` | its values are constants (`i32`) | +| a `struct` with plain fields | a Dino struct: `raylib.Color{255, 0, 0, 255}` | +| any other pointer (`FILE *`, `SDL_Window *`) | a pointer type: `stdio.FILE`, `SDL.SDL_Window` | +| `NULL` | `nil` | + +Numbers follow the usual Dino rule: a number written in the code fits any number type, but +a variable has to be converted, for example `i32(n)` when the C function takes an `int`. + +Text works both ways: a Dino string is handed to C as a C string, and a C string that comes +back becomes a Dino string (a copy, so it stays valid). + +## Pointers: things you only pass around + +Many C libraries give you a pointer to something they manage, like an open file or a +window. In Dino you keep it in a variable, hand it back to the library, and compare it with +`nil` to see if it is missing: + +```dino +import "stdio.h" +import os + +func main() { + var f = stdio.fopen("/tmp/dino-hello.txt", "w") + if f == nil { + print("can't open the file") + return + } + stdio.fputs("hello from C\n", f) + stdio.fclose(f) + + var text, err = os.read_file("/tmp/dino-hello.txt") + print(text.trim(), err) +} +``` +```text +hello from C nil +``` + +The type can be written like any other: `var f: stdio.FILE` starts out as `nil`. + +**Dino does not free C things for you.** Whatever the library opens or creates, close or +free it with the library's own function (`fclose`, `sqlite3_close`, `UnloadTexture`, ...). + +## Functions that write into your variables + +Some C functions give back extra results by writing into a variable you pass them (in C: +a pointer parameter like `double *out`). Pass a Dino **variable**, and the function changes +it. Pass a **list** where the C function wants room for several values (a buffer): + +```dino +import "math.h" { modf } +import "string.h" { memset } + +func main() { + var whole = 0.0 + var frac = modf(3.75, whole) // modf writes the whole part into `whole` + print(frac, whole) + + var buf: []u8 = [0, 0, 0] + memset(buf, 65, 3) // fills the list's items + print(buf) +} +``` +```text +0.75 3.0 +[65, 65, 65] +``` + +A list given to C must already have as many items as the function will write; the C +function can't make it longer. + +The same goes for libraries that hand you a new pointer through a parameter (in C: +`sqlite3 **db`). Make a variable of that pointer type and pass it; afterwards it is set: + +```text +var db: sqlite3.sqlite3 +sqlite3.sqlite3_open("notes.db", db) // db is now open (or still nil if it failed) +``` + +## Constants + +Values from `#define` and `enum` work like Dino constants: `stdio.EOF`, `zlib.Z_OK`, +`raylib.KEY_SPACE`. raylib's colours (`raylib.RED`, `raylib.RAYWHITE`) are struct constants +of type `raylib.Color`. + +## Linking: where the library's code is + +The header only describes a library; its code is in a library file that has to be linked +in. Dino asks the system (`pkg-config`) for that, so for most installed libraries +`import "raylib.h"` or `import "zlib.h"` is all you need. If a library isn't known to +`pkg-config`, name it yourself: for the file `libfoo.so`, write + +```text +import "foo.h" link "foo" +``` + +If it is missing, building stops with a message that says so. + +## A window with raylib + +[raylib](https://www.raylib.com) is a simple library for games and graphics. With it +installed, this opens a window and draws a circle: + +```dino +import "raylib.h" + +func main() { + raylib.InitWindow(800, 450, "Hello from Dino") + raylib.SetTargetFPS(60) + var x: f32 = 400.0 + for !raylib.WindowShouldClose() { + if raylib.IsKeyDown(raylib.KEY_RIGHT) { x += 4.0 } + if raylib.IsKeyDown(raylib.KEY_LEFT) { x -= 4.0 } + raylib.BeginDrawing() + raylib.ClearBackground(raylib.RAYWHITE) + raylib.DrawCircleV(raylib.Vector2{x, 225.0}, 40.0, raylib.MAROON) + raylib.DrawText("arrow keys move the ball", 10, 10, 20, raylib.DARKGRAY) + raylib.EndDrawing() + } + raylib.CloseWindow() +} +``` + +A bigger example with bouncing balls is in +[`examples/c/bounce.dino`](../examples/c/bounce.dino). + +## Your own C code + +Put a header and its `.c` file next to your program. `import "shapes.h"` reads the header, +and Dino compiles `shapes.c` and builds it into your program. + +```c +// shapes.h +typedef struct { double w, h; } Size; +double area(Size s); +void grow(Size *s, double by); +``` + +```c +// shapes.c +#include "shapes.h" +double area(Size s) { return s.w * s.h; } +void grow(Size *s, double by) { s->w *= by; s->h *= by; } +``` + +```dino +import "shapes.h" + +func main() { + var s = shapes.Size{2.0, 3.0} + shapes.grow(s, 10.0) // grow changes `s` + print(shapes.area(s), s.w) +} +``` +```text +600.0 20.0 +``` + +## C++ + +C++ code can't be called from C or Dino directly: C++ functions get scrambled ("mangled") +names when compiled, and classes and templates don't exist in C. The usual fix is a small +set of plain functions marked `extern "C"`. Write them in a `.cpp` file next to the header, +and Dino compiles it with your C++ compiler (`c++`) and links in the C++ library: + +```c +// counter.h: plain C, also readable by C++ +#ifdef __cplusplus +extern "C" { +#endif +typedef struct Counter Counter; +Counter *counter_new(void); +void counter_add(Counter *c, int n); +int counter_total(const Counter *c); +void counter_free(Counter *c); +#ifdef __cplusplus +} +#endif +``` + +```cpp +// counter.cpp: free to use any C++ inside +#include "counter.h" +#include +#include +struct Counter { std::vector items; }; +extern "C" { +Counter *counter_new(void) { return new Counter{}; } +void counter_add(Counter *c, int n) { c->items.push_back(n); } +int counter_total(const Counter *c) { return std::accumulate(c->items.begin(), c->items.end(), 0); } +void counter_free(Counter *c) { delete c; } +} +``` + +```dino +import "counter.h" + +func main() { + var c = counter.counter_new() + for i in 1..5 { counter.counter_add(c, i32(i)) } + print(counter.counter_total(c)) + counter.counter_free(c) +} +``` +```text +10 +``` + +The same trick works for a big C++ library: wrap the few things you need in `extern "C"` +functions. + +## Seeing what a header has + +`dino header` lists everything Dino can use from a header, written as Dino. Add a word to +only see names that contain it: + +```text +dino header zlib.h compress +``` +```text +import "zlib.h" // use the names below as zlib.name +// builds with: -DWITH_GZFILEOP -lz + +// functions +func compress(dest: []u8 or u8 variable, destLen: []u64 or u64 variable, source: []u8 or u8, sourceLen: u64): i32 +... +``` + +In an editor with Dino support, completion after `zlib.` and hovering a name show the same. + +## What doesn't work yet + +Dino tells you when you call one of these, and why: + +- functions with a variable number of arguments, like `printf` +- functions that take a callback (a function pointer) +- `union` types, and structs with arrays inside (you can still pass pointers to them) +- a list of structs where C wants several structs in a row +- C macros that take arguments (`#define MAX(a, b) ...`) + +When you need one of these, write a small function in your own `.c` file that does the C +part, and call that from Dino. + +Next: back to the [overview](README.md) diff --git a/docs/README.md b/docs/README.md index 265662e..3df2b74 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,6 +21,7 @@ These notes teach it from zero. Read them in order, or jump to the | 10 | [Files and the standard library](10-modules-and-stdlib.md) | `import`, `os`, `math`, `time` | | 11 | [Cheatsheet](11-cheatsheet.md) | Everything on one page | | 12 | [A real program](12-calculator-walkthrough.md) | A calculator, split over five files | +| 13 | [C and C++ libraries](13-c-libraries.md) | `import "raylib.h"`, your own C code | ## How to read the examples diff --git a/examples/c/bounce.dino b/examples/c/bounce.dino new file mode 100644 index 0000000..68c6122 --- /dev/null +++ b/examples/c/bounce.dino @@ -0,0 +1,52 @@ +// A ball bouncing around a window, drawn with raylib (https://www.raylib.com). +// Needs raylib installed: `dino run bounce.dino`. Space adds a ball, Escape quits. + +import "raylib.h" + +const WIDTH = 800 +const HEIGHT = 450 + +struct Ball { + pos: raylib.Vector2 + speed: raylib.Vector2 + radius: f32 + color: raylib.Color + + func step(dt: f32) { + pos.x += speed.x * dt + pos.y += speed.y * dt + if pos.x < radius || pos.x > f32(WIDTH) - radius { speed.x = -speed.x } + if pos.y < radius || pos.y > f32(HEIGHT) - radius { speed.y = -speed.y } + } +} + +func new_ball(n: int): Ball { + var colors = [raylib.RED, raylib.GOLD, raylib.SKYBLUE, raylib.LIME, raylib.VIOLET] + var x = f32(100 + n * 37 % 600) + var y = f32(80 + n * 53 % 290) + return Ball{raylib.Vector2{x, y}, raylib.Vector2{f32(180 + n * 20), 140.0}, 20.0, colors[n % colors.len()]} +} + +func main() { + raylib.InitWindow(WIDTH, HEIGHT, "Dino + raylib") + raylib.SetTargetFPS(60) + var balls = [new_ball(0)] + for !raylib.WindowShouldClose() { + if raylib.IsKeyPressed(raylib.KEY_SPACE) { + balls.push(new_ball(balls.len())) + } + var dt = raylib.GetFrameTime() + for i in 0..balls.len() { + balls[i].step(dt) + } + + raylib.BeginDrawing() + raylib.ClearBackground(raylib.RAYWHITE) + for b in balls { + raylib.DrawCircleV(b.pos, b.radius, b.color) + } + raylib.DrawText("balls: {balls.len()} (space adds one)", 10, 10, 20, raylib.DARKGRAY) + raylib.EndDrawing() + } + raylib.CloseWindow() +} diff --git a/src/ast.rs b/src/ast.rs index 4bf7ec1..724b4d6 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -27,12 +27,19 @@ pub enum Item { Const(VarDecl), } -/// `import os`, `import a.b`, or `import c "SDL2/SDL.h"`. +/// `import os`, `import a.b`, or a C header: `import "zlib.h"`, `import "zlib.h" { compress }`, +/// `import "mylib.h" link "mylib"` (and `import z "zlib.h"` to choose the name). #[derive(Debug, Clone, PartialEq)] pub struct Import { pub alias: Option, pub path: String, pub span: Span, + /// A C header (the path was written in quotes). + pub c: bool, + /// `{ a, b }`: only these names, used without a prefix. + pub names: Option>, + /// `link "z"`: the library to link with. + pub link: Option, } #[derive(Debug, Clone, PartialEq)] diff --git a/src/cheader.rs b/src/cheader.rs new file mode 100644 index 0000000..daf4c55 --- /dev/null +++ b/src/cheader.rs @@ -0,0 +1,1702 @@ +//! Reads a C header for `import "zlib.h"`: runs the C preprocessor on it, then picks out the +//! functions, structs, constants and pointer types that Dino code can use. +//! +//! The generated C program includes the header itself, so nothing here has to be exact about +//! how C declares things: it only has to know enough to check calls and convert values. + +use std::collections::{HashMap, HashSet}; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::sync::{Mutex, OnceLock}; + +use crate::types::{Num, Ty}; + +/// A C type as Dino sees it. +#[derive(Debug, Clone, PartialEq)] +pub enum CTy { + Void, + /// A number or `bool` (never `Str`). + Num(Ty), + /// `char *` / `const char *`: Dino strings. + Str, + /// A pointer Dino can only pass around: `SDL_Window *`, `gzFile`, `void *` (`"pointer"`). + Handle(String), + /// A struct passed by value, by its Dino name. + Struct(String), + /// A pointer to a number or struct: filled in by the function (or only read, if `konst`). + Ptr { to: Box, konst: bool }, + /// Something Dino can't pass; the text says what. + Unsupported(String), +} + +#[derive(Debug, Clone)] +pub struct Func { + pub name: String, + pub params: Vec<(String, CTy)>, + pub ret: CTy, + /// Why the function can't be called from Dino, if it can't. + pub problem: Option, +} + +#[derive(Debug, Clone)] +pub struct Struct { + /// The Dino name (the typedef name if there is one, else the struct tag). + pub name: String, + /// How C spells the type: `Color` or `struct timeval`. + pub c: String, + pub fields: Vec<(String, CTy)>, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Value { + Int(i64), + Float(f64), + Str(String), + /// A macro that stands for a struct value, like raylib's `RED`: the struct's Dino name. + Struct(String), +} + +#[derive(Debug, Clone)] +pub struct Const { + pub name: String, + pub value: Value, +} + +/// Everything Dino knows about one header. +#[derive(Debug, Clone, Default)] +pub struct Lib { + /// As written in the import: `zlib.h`, `SDL2/SDL.h`. + pub header: String, + /// The default name in Dino code: `zlib`, `SDL`. + pub prefix: String, + /// Flags for compiling (`-I...`, `-D...`) and for linking (`-l...`). + pub cflags: Vec, + pub libs: Vec, + /// C or C++ files next to a header of the user's, built into the program. + pub sources: Vec, + pub funcs: Vec, + pub structs: Vec, + pub consts: Vec, + /// Pointer types that can be written as Dino types: `gzFile`, `SDL_Window`. + pub handles: Vec, +} + +impl Lib { + pub fn func(&self, name: &str) -> Option<&Func> { + self.funcs.iter().find(|f| f.name == name) + } + pub fn strukt(&self, name: &str) -> Option<&Struct> { + self.structs.iter().find(|s| s.name == name) + } + pub fn konst(&self, name: &str) -> Option<&Const> { + self.consts.iter().find(|c| c.name == name) + } + pub fn has_handle(&self, name: &str) -> bool { + self.handles.iter().any(|h| h == name) + } + /// How a C type reads in Dino. + pub fn type_text(&self, t: &CTy) -> String { + match t { + CTy::Void => "nothing".into(), + CTy::Num(t) => t.to_string(), + CTy::Str => "string".into(), + CTy::Handle(n) if n == "pointer" => "pointer".into(), + CTy::Handle(n) | CTy::Struct(n) => format!("{}.{n}", self.prefix), + CTy::Ptr { to, konst } if matches!(**to, CTy::Struct(_) | CTy::Handle(_)) => { + format!("{} {}", self.type_text(to), if *konst { "value" } else { "variable" }) + } + CTy::Ptr { to, konst: true } => format!("[]{0} or {0}", self.type_text(to)), + CTy::Ptr { to, konst: false } => format!("[]{0} or {0} variable", self.type_text(to)), + CTy::Unsupported(_) => "?".into(), + } + } + + /// `crc32(crc: u64, buf: u8 or []u8, len: u32): u64` + pub fn signature(&self, f: &Func) -> String { + let params: Vec = f + .params + .iter() + .map(|(n, t)| if n.is_empty() { self.type_text(t) } else { format!("{n}: {}", self.type_text(t)) }) + .collect(); + let ret = if f.ret == CTy::Void { String::new() } else { format!(": {}", self.type_text(&f.ret)) }; + format!("{}({}){ret}", f.name, params.join(", ")) + } + + /// One line about a name of the header, for hover and `dino header`. + pub fn describe(&self, name: &str) -> Option { + if let Some(f) = self.func(name) { + let mut s = format!("func {}", self.signature(f)); + if let Some(p) = &f.problem { + s.push_str(&format!("\n(can't be called from Dino: it {p})")); + } + return Some(s); + } + if let Some(st) = self.strukt(name) { + let fields: Vec = st.fields.iter().map(|(n, t)| format!("{n}: {}", self.type_text(t))).collect(); + return Some(format!("struct {}.{} {{ {} }}", self.prefix, st.name, fields.join(", "))); + } + if let Some(c) = self.konst(name) { + let v = match &c.value { + Value::Int(i) => i.to_string(), + Value::Float(f) => format!("{f:?}"), + Value::Str(s) => format!("{s:?}"), + Value::Struct(s) => format!("a {}.{s}", self.prefix), + }; + return Some(format!("const {name} = {v}")); + } + if self.has_handle(name) { + return Some(format!("{}.{name}: a C pointer (pass it around; compare it with nil)", self.prefix)); + } + None + } + + /// Every name the header gives Dino, for messages and completion. + pub fn names(&self) -> Vec<&str> { + let mut v: Vec<&str> = self.funcs.iter().filter(|f| f.problem.is_none()).map(|f| f.name.as_str()).collect(); + v.extend(self.structs.iter().map(|s| s.name.as_str())); + v.extend(self.consts.iter().map(|c| c.name.as_str())); + v.extend(self.handles.iter().map(|h| h.as_str())); + v + } +} + +/// The name a header gets in Dino code: `SDL2/SDL.h` -> `SDL`, `my-lib.h` -> `my_lib`. +pub fn prefix_of(header: &str) -> String { + let file = header.rsplit('/').next().unwrap_or(header); + let stem = file.split('.').next().unwrap_or(file); + let mut out: String = stem.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' }).collect(); + if out.is_empty() || out.starts_with(|c: char| c.is_ascii_digit()) { + out.insert(0, '_'); + } + out +} + +/// Read a header. `dir` is the folder of the Dino file that imports it (local headers and their +/// `.c`/`.cpp` files are found there); `link` is the library named with `link "..."`. +pub fn load(header: &str, dir: &Path, link: Option<&str>) -> Result { + type Key = (String, PathBuf, Option, Option); + static CACHE: OnceLock>>> = OnceLock::new(); + // a header of the user's may change while an editor is open: it is part of the key + let stamp = std::fs::metadata(dir.join(header)).and_then(|m| m.modified()).ok(); + let key = (header.to_string(), dir.to_path_buf(), link.map(str::to_string), stamp); + let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new())); + if let Some(r) = cache.lock().unwrap().get(&key) { + return r.clone(); + } + let r = load_uncached(header, dir, link); + cache.lock().unwrap().insert(key, r.clone()); + r +} + +fn load_uncached(header: &str, dir: &Path, link: Option<&str>) -> Result { + let mut lib = Lib { header: header.to_string(), prefix: prefix_of(header), ..Default::default() }; + // a header of the user's, next to the program + let local = dir.join(header); + if local.is_file() { + lib.cflags.push(format!("-I{}", local.parent().unwrap_or(dir).display())); + let stem = local.with_extension(""); + for ext in ["c", "cpp", "cc", "cxx"] { + let src = stem.with_extension(ext); + if src.is_file() { + lib.sources.push(src); + } + } + } + match link { + Some(l) => { + for part in l.split_whitespace() { + lib.libs.push(if part.starts_with('-') { part.to_string() } else { format!("-l{part}") }); + } + } + None if lib.sources.is_empty() => { + if let Some((cflags, libs)) = pkg_config(header) { + lib.cflags.extend(cflags); + lib.libs.extend(libs); + } + } + None => {} + } + let text = preprocess(header, &lib.cflags)?; + let mut p = Parser::new(lex(&text)); + p.run(); + p.finish(&mut lib); + Ok(lib) +} + +/// Ask pkg-config for the flags of the library a header belongs to, trying likely package names. +fn pkg_config(header: &str) -> Option<(Vec, Vec)> { + let no_ext = header.rsplit_once('.').map_or(header, |x| x.0); + let mut names: Vec = Vec::new(); + for part in no_ext.split('/').filter(|p| !p.is_empty()) { + for n in [part.to_string(), part.to_lowercase(), format!("lib{}", part.to_lowercase())] { + if !names.contains(&n) { + names.push(n); + } + } + } + for n in names { + let out = Command::new("pkg-config").args(["--cflags", "--libs", &n]).output().ok()?; + if out.status.success() { + let text = String::from_utf8_lossy(&out.stdout).to_string(); + let (mut c, mut l) = (Vec::new(), Vec::new()); + for f in text.split_whitespace() { + if f.starts_with("-l") || f.starts_with("-L") || f.starts_with("-Wl") || f == "-pthread" { + l.push(f.to_string()); + } + if !f.starts_with("-l") && !f.starts_with("-L") && !f.starts_with("-Wl") { + c.push(f.to_string()); + } + } + return Some((c, l)); + } + } + None +} + +/// The C compiler to use: `$CC`, else `cc`. +pub fn cc() -> String { + std::env::var("CC").unwrap_or_else(|_| "cc".to_string()) +} + +fn preprocess(header: &str, cflags: &[String]) -> Result { + let out = Command::new(cc()) + .args(["-E", "-dD", "-x", "c", "-std=gnu11"]) + .args(cflags) + .arg("-") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .and_then(|mut child| { + use std::io::Write; + let line = format!("#include \"{header}\"\n"); + child.stdin.take().unwrap().write_all(line.as_bytes())?; + child.wait_with_output() + }) + .map_err(|e| format!("cannot run the C compiler `{}` to read `{header}`: {e}", cc()))?; + if !out.status.success() { + let err = String::from_utf8_lossy(&out.stderr); + if err.contains("No such file") || err.contains("not found") { + return Err(format!( + "cannot find the C header `{header}`: install the library's development package, or put the header next to your program" + )); + } + let first = err.lines().find(|l| l.contains("error")).unwrap_or("").trim().to_string(); + return Err(format!("the C header `{header}` has errors: {first}")); + } + Ok(String::from_utf8_lossy(&out.stdout).to_string()) +} + +// ---------- tokens ---------- + +#[derive(Debug, Clone, PartialEq)] +enum T { + Id(String), + Num(String), + Str(String), + Chr(i64), + P(&'static str), +} + +#[derive(Debug, Clone, PartialEq)] +struct Tok { + t: T, + /// Index into the list of files. + file: u32, +} + +struct Lexed { + toks: Vec, + files: Vec, + /// The header itself (the first file entered from the input). + main: Option, + /// The file that included each file. + parents: HashMap, + /// `#define NAME body` (object-like macros only), in order, minus the ones `#undef`ined. + defines: Vec<(String, Vec, u32)>, +} + +const PUNCT: &[&str] = &[ + "...", "<<=", ">>=", "->", "++", "--", "<<", ">>", "<=", ">=", "==", "!=", "&&", "||", "+=", "-=", "*=", "/=", "%=", + "&=", "|=", "^=", "##", "{", "}", "(", ")", "[", "]", ";", ",", ":", "*", "=", "&", "|", "^", "~", "!", "?", "<", ">", + "+", "-", "/", "%", ".", "#", +]; + +fn lex(text: &str) -> Lexed { + let mut files: Vec = Vec::new(); + let mut file_ids: HashMap = HashMap::new(); + let mut cur: u32 = 0; + let mut main: Option = None; + let mut toks = Vec::new(); + let mut parents: HashMap = HashMap::new(); + let mut defines: Vec<(String, Vec, u32)> = Vec::new(); + for line in text.lines() { + let trimmed = line.trim_start(); + if let Some(rest) = trimmed.strip_prefix('#') { + let rest = rest.trim_start(); + if rest.starts_with(|c: char| c.is_ascii_digit()) { + // `# 12 "/usr/include/zlib.h" 1` + let mut parts = rest.splitn(2, ' '); + parts.next(); + let tail = parts.next().unwrap_or(""); + if let Some(q) = tail.strip_prefix('"') { + let name = q[..q.find('"').unwrap_or(q.len())].to_string(); + let flags = &q[q.find('"').map_or(q.len(), |i| i + 1)..]; + let id = *file_ids.entry(name.clone()).or_insert_with(|| { + files.push(name.clone()); + (files.len() - 1) as u32 + }); + let from_input = files.get(cur as usize).map_or(false, |f| f == ""); + let entering = flags.split_whitespace().any(|f| f == "1"); + if main.is_none() && entering && from_input { + main = Some(id); + } + if entering && id != cur { + parents.entry(id).or_insert(cur); + } + cur = id; + } + } else if let Some(def) = rest.strip_prefix("define ") { + let def = def.trim_start(); + let name_end = def.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')).unwrap_or(def.len()); + let name = def[..name_end].to_string(); + // function-like macros can't be used from Dino + if !def[name_end..].starts_with('(') && !name.is_empty() { + let body = lex_line(&def[name_end..], cur); + defines.retain(|d| d.0 != name); + defines.push((name, body, cur)); + } + } else if let Some(u) = rest.strip_prefix("undef ") { + let u = u.trim(); + defines.retain(|d| d.0 != u); + } + continue; + } + toks.extend(lex_line(line, cur)); + } + Lexed { toks, files, main, parents, defines } +} + +fn lex_line(line: &str, file: u32) -> Vec { + let b = line.as_bytes(); + let mut i = 0; + let mut out = Vec::new(); + while i < b.len() { + let c = b[i]; + if c.is_ascii_whitespace() { + i += 1; + } else if c == b'/' && b.get(i + 1) == Some(&b'/') { + break; + } else if c == b'/' && b.get(i + 1) == Some(&b'*') { + i += 2; + while i + 1 < b.len() && !(b[i] == b'*' && b[i + 1] == b'/') { + i += 1; + } + i += 2; + } else if c.is_ascii_alphabetic() || c == b'_' { + let s = i; + while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') { + i += 1; + } + let word = &line[s..i]; + // `L"..."`, `u8"..."`: wide strings are not for Dino; keep the prefix as a name + out.push(Tok { t: T::Id(word.to_string()), file }); + } else if c.is_ascii_digit() || (c == b'.' && b.get(i + 1).map_or(false, |d| d.is_ascii_digit())) { + let s = i; + let hex = line[s..].starts_with("0x") || line[s..].starts_with("0X"); + while i < b.len() { + let sign = (b[i] == b'+' || b[i] == b'-') + && (matches!(b[i - 1], b'p' | b'P') || !hex && matches!(b[i - 1], b'e' | b'E')); + if !(b[i].is_ascii_alphanumeric() || b[i] == b'.' || b[i] == b'_' || sign) { + break; + } + i += 1; + } + out.push(Tok { t: T::Num(line[s..i].to_string()), file }); + } else if c == b'"' { + i += 1; + let mut s = String::new(); + while i < b.len() && b[i] != b'"' { + if b[i] == b'\\' && i + 1 < b.len() { + i += 1; + match b[i] { + b'n' => s.push('\n'), + b't' => s.push('\t'), + b'r' => s.push('\r'), + b'0' => s.push('\0'), + x => s.push(x as char), + } + i += 1; + } else { + let ch = line[i..].chars().next().unwrap(); + s.push(ch); + i += ch.len_utf8(); + } + } + i += 1; + out.push(Tok { t: T::Str(s), file }); + } else if c == b'\'' { + i += 1; + let v = if b.get(i) == Some(&b'\\') { + i += 1; + let v = match b.get(i) { + Some(b'n') => 10, + Some(b't') => 9, + Some(b'r') => 13, + Some(b'0') => 0, + Some(x) => *x as i64, + None => 0, + }; + i += 1; + v + } else { + let ch = line[i.min(line.len())..].chars().next().unwrap_or('\0'); + i += ch.len_utf8().max(1); + ch as i64 + }; + while i < b.len() && b[i] != b'\'' { + i += 1; + } + i += 1; + out.push(Tok { t: T::Chr(v), file }); + } else { + let rest = &line[i..]; + match PUNCT.iter().find(|p| rest.starts_with(**p)) { + Some(p) => { + out.push(Tok { t: T::P(p), file }); + i += p.len(); + } + None => i += rest.chars().next().map_or(1, |c| c.len_utf8()), + } + } + } + out +} + +// ---------- C declarations ---------- + +/// A C type as written, before Dino decides what it can do with it. +#[derive(Debug, Clone, PartialEq)] +enum Raw { + Void, + /// Plain `char` (strings are `char *`). + Char, + Int { bits: u8, signed: bool }, + Float(u8), + Bool, + /// A typedef name. + Named(String), + Struct { tag: String, union: bool }, + Enum, + Ptr(Box, bool), + Array(Box), + Func { ret: Box, params: Vec<(String, Raw)>, variadic: bool }, + Unknown(String), +} + +struct RawStruct { + fields: Option>, + /// Something in the body Dino can't copy field by field (an array, an unnamed member). + problem: Option, + union: bool, +} + +struct Parser { + toks: Vec, + pos: usize, + files: Vec, + main: Option, + parents: HashMap, + typedefs: HashMap, + /// Typedef names in the order they were declared, with the file they come from. + typedef_order: Vec<(String, u32)>, + structs: HashMap, + /// (struct tag, the file it was declared in) + struct_order: Vec<(String, u32)>, + funcs: Vec<(String, Raw, u32)>, + enum_consts: Vec<(String, i64, u32)>, + defines: Vec<(String, Vec, u32)>, + anon: u32, +} + +fn is_qualifier(w: &str) -> bool { + matches!( + w, + "const" | "volatile" | "restrict" | "__restrict" | "__restrict__" | "extern" | "static" | "inline" | "__inline" + | "__inline__" | "_Noreturn" | "register" | "auto" | "_Thread_local" | "__thread" | "__extension__" + | "_Nullable" | "_Nonnull" | "_Null_unspecified" | "__const" | "__volatile__" | "_Atomic" + ) +} + +fn is_skip_call(w: &str) -> bool { + matches!(w, "__attribute__" | "__attribute" | "__declspec" | "__asm__" | "__asm" | "asm" | "_Alignas" | "__alignas") +} + +fn is_type_word(w: &str) -> bool { + matches!( + w, + "void" | "char" | "short" | "int" | "long" | "float" | "double" | "signed" | "unsigned" | "__signed__" | "__signed" + | "_Bool" | "bool" | "struct" | "union" | "enum" | "_Complex" | "__complex__" | "__int128" | "_Float128" + | "__float128" | "_Float64" | "_Float32" | "_Float64x" | "_Float32x" | "_Float16" | "__typeof__" | "typeof" + | "__typeof" | "__builtin_va_list" + ) +} + +impl Parser { + fn new(l: Lexed) -> Self { + Parser { + toks: l.toks, + pos: 0, + files: l.files, + main: l.main, + parents: l.parents, + typedefs: HashMap::new(), + typedef_order: Vec::new(), + structs: HashMap::new(), + struct_order: Vec::new(), + funcs: Vec::new(), + enum_consts: Vec::new(), + defines: l.defines, + anon: 0, + } + } + + fn peek(&self) -> Option<&T> { + self.toks.get(self.pos).map(|t| &t.t) + } + fn peek_at(&self, k: usize) -> Option<&T> { + self.toks.get(self.pos + k).map(|t| &t.t) + } + fn is(&self, p: &str) -> bool { + matches!(self.peek(), Some(T::P(x)) if *x == p) + } + fn eat(&mut self, p: &str) -> bool { + if self.is(p) { + self.pos += 1; + true + } else { + false + } + } + fn file(&self) -> u32 { + self.toks.get(self.pos).map_or(0, |t| t.file) + } + + /// Skip a balanced `( ... )`, `[ ... ]` or `{ ... }` starting at the current token. + fn skip_group(&mut self) { + let mut depth = 0i32; + while let Some(t) = self.peek() { + match t { + T::P("(") | T::P("[") | T::P("{") => depth += 1, + T::P(")") | T::P("]") | T::P("}") => depth -= 1, + _ => {} + } + self.pos += 1; + if depth <= 0 { + break; + } + } + } + + /// Skip `__attribute__((...))`, `__asm__("...")` and similar. + fn skip_extras(&mut self) { + loop { + match self.peek() { + Some(T::Id(w)) if is_skip_call(w) => { + self.pos += 1; + while matches!(self.peek(), Some(T::Id(w)) if w == "volatile" || w == "__volatile__") { + self.pos += 1; + } + if self.is("(") { + self.skip_group(); + } + } + Some(T::Id(w)) if is_qualifier(w) && w != "const" && w != "__const" => self.pos += 1, + _ => break, + } + } + } + + /// Skip to just after the `;` that ends the current declaration. + fn recover(&mut self) { + while let Some(t) = self.peek() { + match t { + T::P(";") => { + self.pos += 1; + return; + } + T::P("(") | T::P("[") | T::P("{") => self.skip_group(), + _ => self.pos += 1, + } + } + } + + fn run(&mut self) { + while self.pos < self.toks.len() { + let start = self.pos; + match self.peek().cloned() { + Some(T::P(";")) => self.pos += 1, + Some(T::Id(w)) if w == "_Static_assert" || w == "static_assert" => self.recover(), + _ => { + if self.declaration().is_none() { + self.pos = start.max(self.pos); + self.recover(); + } + } + } + if self.pos == start { + self.pos += 1; + } + } + } + + /// One top-level declaration. `None` if it could not be read (the caller skips it). + fn declaration(&mut self) -> Option<()> { + let is_typedef = { + // `typedef` can come after other words (`__extension__ typedef ...`) + let mut k = 0; + let mut found = false; + while let Some(T::Id(w)) = self.peek_at(k) { + if w == "typedef" { + found = true; + break; + } + if !is_qualifier(w) { + break; + } + k += 1; + } + found + }; + let file = self.file(); + let (base, konst) = self.specifiers()?; + if self.eat(";") { + return Some(()); + } + loop { + let (name, ty) = self.declarator(base.clone(), konst)?; + self.skip_extras(); + if let Some(name) = name { + if is_typedef { + if !self.typedefs.contains_key(&name) { + self.typedef_order.push((name.clone(), file)); + } + self.typedefs.insert(name, ty); + } else if let Raw::Func { .. } = ty { + if !self.funcs.iter().any(|f| f.0 == name) { + self.funcs.push((name, ty, file)); + } + } + } + if self.is("{") { + // an inline function's body + self.skip_group(); + return Some(()); + } + if self.eat("=") { + while !self.is(",") && !self.is(";") && self.peek().is_some() { + if self.is("(") || self.is("{") || self.is("[") { + self.skip_group(); + } else { + self.pos += 1; + } + } + } + if self.eat(",") { + continue; + } + if self.eat(";") { + return Some(()); + } + return None; + } + } + + /// The type words of a declaration (`static const unsigned long`, `struct S {...}`, a typedef name). + fn specifiers(&mut self) -> Option<(Raw, bool)> { + let mut konst = false; + let (mut signed, mut unsigned, mut short, mut long, mut int, mut chr) = (false, false, false, 0, false, false); + let mut base: Option = None; + loop { + let Some(T::Id(w)) = self.peek().cloned() else { break }; + match w.as_str() { + "const" | "__const" => { + konst = true; + self.pos += 1; + } + "typedef" => self.pos += 1, + w if is_qualifier(w) => self.pos += 1, + w if is_skip_call(w) => self.skip_extras(), + "signed" | "__signed__" | "__signed" => { + signed = true; + self.pos += 1; + } + "unsigned" => { + unsigned = true; + self.pos += 1; + } + "short" => { + short = true; + self.pos += 1; + } + "long" => { + long += 1; + self.pos += 1; + } + "int" => { + int = true; + self.pos += 1; + } + "char" => { + chr = true; + self.pos += 1; + } + "void" => { + base = Some(Raw::Void); + self.pos += 1; + } + "float" | "_Float32" => { + base = Some(Raw::Float(32)); + self.pos += 1; + } + "double" | "_Float64" => { + base = Some(if long > 0 { Raw::Unknown("long double".into()) } else { Raw::Float(64) }); + self.pos += 1; + } + "_Bool" | "bool" => { + base = Some(Raw::Bool); + self.pos += 1; + } + "struct" | "union" => { + self.pos += 1; + base = Some(self.struct_spec(w == "union")?); + } + "enum" => { + self.pos += 1; + self.enum_spec()?; + base = Some(Raw::Enum); + } + "__typeof__" | "typeof" | "__typeof" => { + self.pos += 1; + if self.is("(") { + self.skip_group(); + } + base = Some(Raw::Unknown("typeof".into())); + } + "_Complex" | "__complex__" | "__int128" | "_Float128" | "__float128" | "_Float64x" | "_Float32x" | "_Float16" => { + base = Some(Raw::Unknown(w.clone())); + self.pos += 1; + } + "__builtin_va_list" => { + base = Some(Raw::Unknown("va_list".into())); + self.pos += 1; + } + _ => { + let have_type = base.is_some() || signed || unsigned || short || long > 0 || int || chr; + if have_type { + break; + } + if self.typedefs.contains_key(&w) { + base = Some(Raw::Named(w.clone())); + self.pos += 1; + } else { + // an unknown type name (from a header we could not read fully) + let next = self.peek_at(1).cloned(); + if matches!(next, Some(T::Id(_)) | Some(T::P("*"))) { + base = Some(Raw::Unknown(w.clone())); + self.pos += 1; + } else { + break; + } + } + } + } + } + let base = match base { + Some(b) => b, + None if chr => { + if unsigned { + Raw::Int { bits: 8, signed: false } + } else if signed { + Raw::Int { bits: 8, signed: true } + } else { + Raw::Char + } + } + None if short => Raw::Int { bits: 16, signed: !unsigned }, + None if long > 0 => Raw::Int { bits: 64, signed: !unsigned }, + None if int || signed || unsigned => Raw::Int { bits: 32, signed: !unsigned }, + None => return None, + }; + Some((base, konst)) + } + + fn struct_spec(&mut self, union: bool) -> Option { + self.skip_extras(); + let tag = match self.peek().cloned() { + Some(T::Id(t)) => { + self.pos += 1; + t + } + _ => { + self.anon += 1; + format!("__anon{}", self.anon) + } + }; + self.skip_extras(); + if !self.structs.contains_key(&tag) { + self.struct_order.push((tag.clone(), self.file())); + self.structs.insert(tag.clone(), RawStruct { fields: None, problem: None, union }); + } + if self.is("{") { + self.pos += 1; + let mut fields = Vec::new(); + let mut problem: Option = None; + while !self.is("}") { + self.peek()?; + let start = self.pos; + let Some((base, konst)) = self.specifiers() else { + self.pos = start; + self.recover(); + problem.get_or_insert("has a field Dino can't read".into()); + continue; + }; + if self.eat(";") { + problem.get_or_insert("has an unnamed member".into()); + continue; + } + loop { + let (name, ty) = self.declarator(base.clone(), konst)?; + self.skip_extras(); + if self.eat(":") { + // a bit field: copying it field by field still works + while !self.is(",") && !self.is(";") && self.peek().is_some() { + self.pos += 1; + } + } + match name { + Some(n) => fields.push((n, ty)), + None => { + problem.get_or_insert("has an unnamed member".into()); + } + } + if self.eat(",") { + continue; + } + break; + } + if !self.eat(";") { + self.recover(); + } + } + self.pos += 1; + self.skip_extras(); + let s = self.structs.get_mut(&tag).unwrap(); + s.fields = Some(fields); + s.problem = problem; + } + Some(Raw::Struct { tag, union }) + } + + fn enum_spec(&mut self) -> Option<()> { + self.skip_extras(); + if let Some(T::Id(_)) = self.peek() { + self.pos += 1; + } + if self.eat(":") { + self.specifiers()?; + } + if !self.is("{") { + return Some(()); + } + self.pos += 1; + let mut next: i64 = 0; + while !self.is("}") { + let file = self.file(); + let Some(T::Id(name)) = self.peek().cloned() else { return None }; + self.pos += 1; + self.skip_extras(); + if self.eat("=") { + let start = self.pos; + let mut depth = 0; + while let Some(t) = self.peek() { + match t { + T::P("(") => depth += 1, + T::P(")") => depth -= 1, + T::P(",") | T::P("}") if depth == 0 => break, + _ => {} + } + self.pos += 1; + } + let expr = self.toks[start..self.pos].to_vec(); + let known: HashMap = self.enum_consts.iter().map(|(n, v, _)| (n.clone(), *v)).collect(); + match Eval::new(&expr, &|n| known.get(n).map(|v| Number::I(*v))).run() { + Some(Number::I(v)) => next = v, + _ => {} + } + } + self.enum_consts.push((name, next, file)); + next = next.wrapping_add(1); + if !self.eat(",") { + break; + } + } + if !self.eat("}") { + return None; + } + self.skip_extras(); + Some(()) + } + + /// Wrap `t` in the pointers that follow (`* const *`). + fn pointers(&mut self, mut t: Raw, mut pointee_const: bool) -> Raw { + while self.eat("*") { + t = Raw::Ptr(Box::new(t), pointee_const); + pointee_const = false; + loop { + match self.peek() { + Some(T::Id(w)) if w == "const" || w == "__const" => { + pointee_const = true; + self.pos += 1; + } + Some(T::Id(w)) if is_qualifier(w) || is_skip_call(w) => self.skip_extras_one(), + _ => break, + } + } + } + t + } + + fn skip_extras_one(&mut self) { + let before = self.pos; + self.skip_extras(); + if self.pos == before { + self.pos += 1; + } + } + + /// Does the `(` here start a nested declarator like `(*callback)` (and not a parameter list)? + fn nested_here(&self) -> bool { + if !self.is("(") { + return false; + } + match self.peek_at(1) { + Some(T::P("*")) | Some(T::P("^")) => true, + Some(T::P("(")) => true, + Some(T::Id(w)) => is_skip_call(w) && matches!(self.peek_at(2), Some(T::P("("))) && { + // `(__attribute__((x)) *name)` + true + } || (!self.typedefs.contains_key(w) && !is_type_word(w) && !is_qualifier(w) && matches!(self.peek_at(2), Some(T::P(")")))), + _ => false, + } + } + + fn declarator(&mut self, base: Raw, konst: bool) -> Option<(Option, Raw)> { + let t = self.pointers(base, konst); + self.skip_extras(); + if self.nested_here() { + self.pos += 1; + let inner_start = self.pos; + // find the matching `)` + let mut depth = 1; + while depth > 0 { + match self.peek()? { + T::P("(") => depth += 1, + T::P(")") => depth -= 1, + _ => {} + } + self.pos += 1; + } + let t2 = self.suffixes(t)?; + let end = self.pos; + self.pos = inner_start; + let (name, ty) = self.declarator(t2, false)?; + self.skip_extras(); + if !self.eat(")") { + return None; + } + self.pos = end; + return Some((name, ty)); + } + let name = match self.peek() { + Some(T::Id(w)) if !is_type_word(w) && !is_qualifier(w) && !is_skip_call(w) => { + let n = w.clone(); + self.pos += 1; + Some(n) + } + _ => None, + }; + self.skip_extras(); + let ty = self.suffixes(t)?; + Some((name, ty)) + } + + fn suffixes(&mut self, mut t: Raw) -> Option { + let mut dims = 0; + loop { + if self.is("[") { + self.skip_group(); + dims += 1; + } else if self.is("(") { + self.pos += 1; + let (params, variadic) = self.params()?; + t = Raw::Func { ret: Box::new(t), params, variadic }; + self.skip_extras(); + } else { + break; + } + } + for _ in 0..dims { + t = Raw::Array(Box::new(t)); + } + Some(t) + } + + /// The parameters, after the `(`; reads the closing `)` too. + fn params(&mut self) -> Option<(Vec<(String, Raw)>, bool)> { + let mut params = Vec::new(); + let mut variadic = false; + if self.eat(")") { + return Some((params, false)); + } + loop { + if self.eat("...") { + variadic = true; + } else { + self.skip_extras(); + let (base, konst) = self.specifiers()?; + let (name, ty) = self.declarator(base, konst)?; + self.skip_extras(); + let ty = match ty { + Raw::Array(inner) => Raw::Ptr(inner, konst), + f @ Raw::Func { .. } => Raw::Ptr(Box::new(f), false), + t => t, + }; + if !(ty == Raw::Void && name.is_none()) { + params.push((name.unwrap_or_default(), ty)); + } + } + if self.eat(",") { + continue; + } + if self.eat(")") { + return Some((params, variadic)); + } + return None; + } + } + + // ---------- from C declarations to what Dino can use ---------- + + fn wanted(&self, file: u32) -> bool { + let Some(main) = self.main else { return true }; + if file == main { + return true; + } + let (Some(m), Some(f)) = (self.files.get(main as usize), self.files.get(file as usize)) else { return false }; + let mdir = Path::new(m).parent().unwrap_or(Path::new("")); + let fpath = Path::new(f); + let system = ["/usr/include", "/usr/local/include", "/include"].iter().any(|d| mdir == Path::new(d)) + || mdir.to_string_lossy().contains("/lib/gcc") + || mdir.to_string_lossy().contains("/lib/clang"); + if system { + // in a shared folder like /usr/include: the header and what it includes itself, + // but not the C library + let Some(&parent) = self.parents.get(&file) else { return false }; + if parent != main && !self.wanted(parent) { + return false; + } + let fs = fpath.to_string_lossy(); + if parent != main && (fs.contains("/sys/") || fs.contains("/bits/")) { + return false; + } + let std_names = [ + "assert.h", "ctype.h", "errno.h", "fenv.h", "float.h", "inttypes.h", "limits.h", "locale.h", "math.h", + "setjmp.h", "signal.h", "stdarg.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", "stdlib.h", "string.h", + "strings.h", "time.h", "wchar.h", "wctype.h", "uchar.h", "features.h", "features-time64.h", "alloca.h", + "unistd.h", "stdc-predef.h", "endian.h", "pthread.h", "sched.h", "memory.h", "fcntl.h", "dirent.h", + "termios.h", "poll.h", "getopt.h", "libintl.h", "iconv.h", "langinfo.h", "malloc.h", "stdalign.h", + "stdnoreturn.h", "threads.h", "complex.h", "tgmath.h", "stdatomic.h", "linux", "asm", "asm-generic", + ]; + let _ = mdir; + !fs.contains("/linux/") + && !fs.contains("/asm") + && !std_names.contains(&fpath.file_name().and_then(|n| n.to_str()).unwrap_or("")) + } else { + fpath.starts_with(mdir) + } + } + + /// Follow typedefs: the type behind a name, and the last typedef name on the way. + fn resolve<'a>(&'a self, mut t: &'a Raw) -> (&'a Raw, Option<&'a str>) { + let mut last = None; + for _ in 0..64 { + match t { + Raw::Named(n) => match self.typedefs.get(n) { + Some(next) => { + last = Some(n.as_str()); + t = next; + } + None => return (t, last), + }, + _ => break, + } + } + (t, last) + } + + /// The Dino name of a struct tag: a typedef naming exactly that struct, else the tag. + fn struct_name(&self, tag: &str, names: &HashMap) -> String { + names.get(tag).cloned().unwrap_or_else(|| tag.to_string()) + } + + fn convert(&self, t: &Raw, mapped: &HashSet, names: &HashMap) -> CTy { + let (r, typedef) = self.resolve(t); + match r { + Raw::Void => CTy::Void, + Raw::Char => CTy::Num(Ty::Num(Num::I8)), + Raw::Int { bits, signed } => CTy::Num(match (bits, signed) { + (8, true) => Ty::Num(Num::I8), + (8, false) => Ty::Num(Num::U8), + (16, true) => Ty::Num(Num::I16), + (16, false) => Ty::Num(Num::U16), + (32, true) => Ty::Num(Num::I32), + (32, false) => Ty::Num(Num::U32), + (_, true) => Ty::Int, + (_, false) => Ty::Num(Num::U64), + }), + Raw::Float(32) => CTy::Num(Ty::Num(Num::F32)), + Raw::Float(_) => CTy::Num(Ty::Float), + Raw::Bool => CTy::Num(Ty::Bool), + Raw::Enum => CTy::Num(Ty::Num(Num::I32)), + Raw::Struct { tag, union } => { + let name = self.struct_name(tag, names); + if !union && mapped.contains(tag) { + CTy::Struct(name) + } else { + CTy::Unsupported(format!("passes a `{name}` by value, and Dino can't build that type")) + } + } + Raw::Ptr(inner, konst) => { + // a typedef that is itself a pointer (`gzFile`) names the handle + let (target, inner_name) = self.resolve(inner); + match target { + Raw::Char => CTy::Str, + Raw::Void => CTy::Handle(typedef.unwrap_or("pointer").to_string()), + Raw::Int { .. } | Raw::Float(_) | Raw::Bool | Raw::Enum => { + CTy::Ptr { to: Box::new(self.convert(inner, mapped, names)), konst: *konst } + } + Raw::Struct { tag, union } => { + let name = typedef.map(str::to_string).unwrap_or_else(|| self.struct_name(tag, names)); + if !union && mapped.contains(tag) && typedef.is_none() { + CTy::Ptr { to: Box::new(CTy::Struct(self.struct_name(tag, names))), konst: *konst } + } else { + let _ = inner_name; + CTy::Handle(name) + } + } + Raw::Func { .. } => CTy::Unsupported("takes a callback function, which Dino can't pass yet".into()), + // `sqlite3 **db`: the function hands back a pointer through it + Raw::Ptr(..) if typedef.is_none() => match self.convert(inner, mapped, names) { + h @ CTy::Handle(_) => CTy::Ptr { to: Box::new(h), konst: *konst }, + _ => CTy::Handle("pointer".into()), + }, + _ => CTy::Handle(typedef.unwrap_or("pointer").to_string()), + } + } + Raw::Array(_) => CTy::Unsupported("uses a C array type".into()), + Raw::Func { .. } => CTy::Unsupported("uses a function type".into()), + Raw::Named(n) | Raw::Unknown(n) => CTy::Unsupported(format!("uses the C type `{n}`, which Dino doesn't know")), + } + } + + fn finish(mut self, lib: &mut Lib) { + // struct tag -> Dino name: `typedef struct Color {...} Color;` and `typedef struct {...} Color;` + let mut names: HashMap = HashMap::new(); + for (n, _) in &self.typedef_order { + if let Some(Raw::Struct { tag, .. }) = self.typedefs.get(n) { + // prefer a public name (`FILE`) to an internal one (`__FILE`) + let better = names.get(tag).map_or(true, |old| old.starts_with('_') && !n.starts_with('_')); + if better { + names.insert(tag.clone(), n.clone()); + } + } + } + // which structs Dino can copy field by field (all fields numbers, pointers or such structs) + let mut mapped: HashSet = HashSet::new(); + loop { + let mut changed = false; + for (tag, s) in &self.structs { + if mapped.contains(tag) || s.union || s.problem.is_some() || tag.starts_with("__anon") && !names.contains_key(tag) { + continue; + } + let Some(fields) = &s.fields else { continue }; + let ok = !fields.is_empty() + && fields.iter().all(|(_, t)| match self.resolve(t).0 { + Raw::Struct { tag: inner, union: false } => mapped.contains(inner), + Raw::Array(_) | Raw::Unknown(_) | Raw::Named(_) | Raw::Struct { .. } | Raw::Func { .. } | Raw::Void => false, + _ => true, + }); + if ok { + mapped.insert(tag.clone()); + changed = true; + } + } + if !changed { + break; + } + } + let field_ty = |p: &Parser, t: &Raw| -> CTy { + match p.convert(t, &mapped, &names) { + CTy::Str | CTy::Ptr { .. } | CTy::Unsupported(_) => CTy::Handle("pointer".into()), + other => other, + } + }; + for (tag, file) in self.struct_order.clone() { + if !self.wanted(file) || !mapped.contains(&tag) { + continue; + } + let s = &self.structs[&tag]; + let name = self.struct_name(&tag, &names); + let c = if names.contains_key(&tag) { name.clone() } else { format!("struct {tag}") }; + let fields = s.fields.as_ref().unwrap().iter().map(|(n, t)| (n.clone(), field_ty(&self, t))).collect(); + if !lib.structs.iter().any(|x| x.name == name) { + lib.structs.push(Struct { name, c, fields }); + } + } + // handles: opaque struct names and pointer typedefs + let mut handles: Vec = Vec::new(); + for (tag, file) in &self.struct_order { + if self.wanted(*file) && !mapped.contains(tag) && !tag.starts_with("__anon") { + handles.push(self.struct_name(tag, &names)); + } + } + for (n, file) in &self.typedef_order { + if !self.wanted(*file) { + continue; + } + if let CTy::Handle(h) = self.convert(&Raw::Named(n.clone()), &mapped, &names) { + if h == *n { + handles.push(h); + } + } + } + for h in handles { + if !h.starts_with("__") && !lib.handles.contains(&h) && !lib.structs.iter().any(|s| s.name == h) { + lib.handles.push(h); + } + } + // functions + for (name, ty, file) in std::mem::take(&mut self.funcs) { + if !self.wanted(file) || name.starts_with("__") { + continue; + } + let Raw::Func { ret, params, variadic } = ty else { continue }; + let ret = self.convert(&ret, &mapped, &names); + let params: Vec<(String, CTy)> = + params.iter().map(|(n, t)| (n.clone(), self.convert(t, &mapped, &names))).collect(); + let mut problem = if variadic { Some("takes a variable number of arguments".to_string()) } else { None }; + for t in params.iter().map(|p| &p.1).chain([&ret]) { + if let CTy::Unsupported(why) = t { + problem.get_or_insert(why.clone()); + } + } + if let CTy::Ptr { .. } = ret { + // a returned pointer to numbers: Dino can only hold on to it + } + let ret = match ret { + CTy::Ptr { .. } => CTy::Handle("pointer".into()), + r => r, + }; + for (_, t) in ¶ms { + if let CTy::Ptr { to, .. } = t { + if let CTy::Unsupported(why) = &**to { + problem.get_or_insert(why.clone()); + } + } + } + lib.funcs.push(Func { name, params, ret, problem }); + } + // constants: enum values, then `#define NAME value` + let mut ints: HashMap = HashMap::new(); + for (n, v, file) in &self.enum_consts { + ints.insert(n.clone(), *v); + if self.wanted(*file) && !n.starts_with('_') { + lib.consts.push(Const { name: n.clone(), value: Value::Int(*v) }); + } + } + let defs: HashMap> = self.defines.iter().map(|(n, b, _)| (n.clone(), b.clone())).collect(); + for (name, body, file) in &self.defines { + if !self.wanted(*file) || name.starts_with('_') || lib.consts.iter().any(|c| c.name == *name) { + continue; + } + if let Some(v) = define_value(body, &defs, &ints, &lib.structs, 0) { + lib.consts.push(Const { name: name.clone(), value: v }); + } + } + // a header that is mostly macros (and functions with the same names as macros) is fine: + // a function hidden by a macro is still callable through the macro + let fnames: HashSet = lib.funcs.iter().map(|f| f.name.clone()).collect(); + lib.consts.retain(|c| !fnames.contains(&c.name)); + } +} + +/// The value of a `#define`: a number, a string, or a struct literal like raylib's +/// `CLITERAL(Color){ 255, 0, 0, 255 }`. +fn define_value( + body: &[Tok], + defs: &HashMap>, + ints: &HashMap, + structs: &[Struct], + depth: u32, +) -> Option { + if body.is_empty() || depth > 16 { + return None; + } + if body.iter().all(|t| matches!(t.t, T::Str(_))) { + let s: String = body.iter().map(|t| if let T::Str(s) = &t.t { s.as_str() } else { "" }).collect(); + return Some(Value::Str(s)); + } + // `(Color){...}` or `CLITERAL(Color){...}` + for w in body.windows(4) { + if let (T::P("("), T::Id(name), T::P(")"), T::P("{")) = (&w[0].t, &w[1].t, &w[2].t, &w[3].t) { + if structs.iter().any(|s| s.name == *name) { + return Some(Value::Struct(name.clone())); + } + } + } + if let [Tok { t: T::Id(other), .. }] = body { + if let Some(b) = defs.get(other) { + return define_value(b, defs, ints, structs, depth + 1); + } + } + let lookup = |n: &str| -> Option { + if let Some(v) = ints.get(n) { + return Some(Number::I(*v)); + } + match define_value(defs.get(n)?, defs, ints, structs, depth + 1)? { + Value::Int(i) => Some(Number::I(i)), + Value::Float(f) => Some(Number::F(f)), + _ => None, + } + }; + match Eval::new(body, &lookup).run()? { + Number::I(i) => Some(Value::Int(i)), + Number::F(f) => Some(Value::Float(f)), + } +} + +// ---------- constant expressions ---------- + +#[derive(Debug, Clone, Copy, PartialEq)] +enum Number { + I(i64), + F(f64), +} + +impl Number { + fn f(self) -> f64 { + match self { + Number::I(i) => i as f64, + Number::F(f) => f, + } + } + fn truthy(self) -> bool { + self.f() != 0.0 + } +} + +struct Eval<'a> { + toks: &'a [Tok], + pos: usize, + lookup: &'a dyn Fn(&str) -> Option, +} + +fn parse_number(s: &str) -> Option { + let lower = s.to_ascii_lowercase(); + let is_hex = lower.starts_with("0x"); + let is_float = !is_hex && (lower.contains('.') || lower.contains('e')) || is_hex && lower.contains('p'); + if is_float { + let t = lower.trim_end_matches(|c| c == 'f' || c == 'l'); + return t.parse::().ok().map(Number::F); + } + let t = lower.trim_end_matches(|c| c == 'u' || c == 'l'); + let v = if let Some(h) = t.strip_prefix("0x") { + u64::from_str_radix(h, 16).ok()? + } else if let Some(b) = t.strip_prefix("0b") { + u64::from_str_radix(b, 2).ok()? + } else if t.len() > 1 && t.starts_with('0') { + u64::from_str_radix(&t[1..], 8).ok()? + } else { + t.parse::().ok()? + }; + Some(Number::I(v as i64)) +} + +impl<'a> Eval<'a> { + fn new(toks: &'a [Tok], lookup: &'a dyn Fn(&str) -> Option) -> Self { + Eval { toks, pos: 0, lookup } + } + + fn run(mut self) -> Option { + let v = self.ternary()?; + if self.pos == self.toks.len() { + Some(v) + } else { + None + } + } + + fn peek(&self) -> Option<&T> { + self.toks.get(self.pos).map(|t| &t.t) + } + + fn eat(&mut self, p: &str) -> bool { + if matches!(self.peek(), Some(T::P(x)) if *x == p) { + self.pos += 1; + true + } else { + false + } + } + + fn ternary(&mut self) -> Option { + let c = self.binary(0)?; + if self.eat("?") { + let a = self.ternary()?; + if !self.eat(":") { + return None; + } + let b = self.ternary()?; + return Some(if c.truthy() { a } else { b }); + } + Some(c) + } + + fn binary(&mut self, min: u8) -> Option { + let mut l = self.unary()?; + loop { + let Some(T::P(op)) = self.peek() else { break }; + let op = *op; + let prec = match op { + "||" => 1, + "&&" => 2, + "|" => 3, + "^" => 4, + "&" => 5, + "==" | "!=" => 6, + "<" | ">" | "<=" | ">=" => 7, + "<<" | ">>" => 8, + "+" | "-" => 9, + "*" | "/" | "%" => 10, + _ => break, + }; + if prec < min { + break; + } + self.pos += 1; + let r = self.binary(prec + 1)?; + l = match (l, r) { + (Number::I(a), Number::I(b)) => Number::I(match op { + "||" => (a != 0 || b != 0) as i64, + "&&" => (a != 0 && b != 0) as i64, + "|" => a | b, + "^" => a ^ b, + "&" => a & b, + "==" => (a == b) as i64, + "!=" => (a != b) as i64, + "<" => (a < b) as i64, + ">" => (a > b) as i64, + "<=" => (a <= b) as i64, + ">=" => (a >= b) as i64, + "<<" => a.wrapping_shl(b as u32), + ">>" => a.wrapping_shr(b as u32), + "+" => a.wrapping_add(b), + "-" => a.wrapping_sub(b), + "*" => a.wrapping_mul(b), + "/" if b != 0 => a.wrapping_div(b), + "%" if b != 0 => a.wrapping_rem(b), + _ => return None, + }), + (a, b) => { + let (a, b) = (a.f(), b.f()); + match op { + "+" => Number::F(a + b), + "-" => Number::F(a - b), + "*" => Number::F(a * b), + "/" => Number::F(a / b), + "<" => Number::I((a < b) as i64), + ">" => Number::I((a > b) as i64), + _ => return None, + } + } + }; + } + Some(l) + } + + fn unary(&mut self) -> Option { + if self.eat("-") { + return Some(match self.unary()? { + Number::I(i) => Number::I(i.wrapping_neg()), + Number::F(f) => Number::F(-f), + }); + } + if self.eat("+") { + return self.unary(); + } + if self.eat("~") { + return match self.unary()? { + Number::I(i) => Some(Number::I(!i)), + _ => None, + }; + } + if self.eat("!") { + return Some(Number::I(!self.unary()?.truthy() as i64)); + } + if self.eat("(") { + // a cast like `(int)` or `(Uint32)`: skip it + let save = self.pos; + let mut is_cast = true; + let mut k = self.pos; + while let Some(t) = self.toks.get(k) { + match &t.t { + T::P(")") => break, + T::Id(w) if is_type_word(w) || is_qualifier(w) || (self.lookup)(w).is_none() => {} + T::P("*") => {} + _ => { + is_cast = false; + break; + } + } + k += 1; + } + if is_cast && k > self.pos && matches!(self.toks.get(k).map(|t| &t.t), Some(T::P(")"))) && k + 1 < self.toks.len() { + self.pos = k + 1; + let v = self.unary()?; + // `(int)` and friends keep the number; a cast to a pointer is not a constant + if self.toks[save..k].iter().any(|t| t.t == T::P("*")) { + return None; + } + return Some(v); + } + self.pos = save; + let v = self.ternary()?; + if !self.eat(")") { + return None; + } + return Some(v); + } + match self.peek()?.clone() { + T::Num(s) => { + self.pos += 1; + parse_number(&s) + } + T::Chr(c) => { + self.pos += 1; + Some(Number::I(c)) + } + T::Id(n) => { + self.pos += 1; + (self.lookup)(&n) + } + _ => None, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn read(src: &str) -> Lib { + let mut lib = Lib::default(); + let mut p = Parser::new(lex(src)); + p.run(); + p.finish(&mut lib); + lib + } + + #[test] + fn functions_structs_and_constants() { + let lib = read( + "typedef unsigned long size_t;\n\ + typedef struct Color { unsigned char r, g, b, a; } Color;\n\ + typedef struct Window Window;\n\ + typedef struct gz_s *gzFile;\n\ + enum { A = 1, B, C = A << 4 };\n\ + #define NAME \"dino\"\n\ + #define RED (Color){ 255, 0, 0, 255 }\n\ + #define FLAG (0x10u | 2)\n\ + #define PI 3.5\n\ + extern int add(int a, int b) __attribute__((__nothrow__));\n\ + const char *version(void);\n\ + void draw(Color c, float x);\n\ + Window *open_window(const char *title, int w, int h);\n\ + int fill(int *out, size_t n);\n\ + int printf(const char *fmt, ...);\n\ + void on_key(void (*cb)(int key));\n\ + static inline int twice(int x) { return x * 2; }\n\ + gzFile gzopen(const char *path, const char *mode);\n", + ); + let f = |n: &str| lib.func(n).unwrap(); + assert_eq!(f("add").params[1].1, CTy::Num(Ty::Num(Num::I32))); + assert_eq!(f("version").ret, CTy::Str); + assert_eq!(f("draw").params[0].1, CTy::Struct("Color".into())); + assert_eq!(f("open_window").ret, CTy::Handle("Window".into())); + assert_eq!(f("fill").params[0].1, CTy::Ptr { to: Box::new(CTy::Num(Ty::Num(Num::I32))), konst: false }); + assert_eq!(f("fill").params[1].1, CTy::Num(Ty::Num(Num::U64))); + assert!(f("printf").problem.is_some()); + assert!(f("on_key").problem.is_some()); + assert_eq!(f("twice").ret, CTy::Num(Ty::Num(Num::I32))); + assert_eq!(f("gzopen").ret, CTy::Handle("gzFile".into())); + assert_eq!(lib.strukt("Color").unwrap().fields.len(), 4); + assert!(lib.has_handle("Window") && lib.has_handle("gzFile")); + let c = |n: &str| lib.konst(n).map(|c| c.value.clone()); + assert_eq!(c("B"), Some(Value::Int(2))); + assert_eq!(c("C"), Some(Value::Int(16))); + assert_eq!(c("NAME"), Some(Value::Str("dino".into()))); + assert_eq!(c("RED"), Some(Value::Struct("Color".into()))); + assert_eq!(c("FLAG"), Some(Value::Int(0x12))); + assert_eq!(c("PI"), Some(Value::Float(3.5))); + } + + #[test] + #[ignore] + fn real_headers() { + for h in ["zlib.h", "raylib.h", "SDL2/SDL.h", "sqlite3.h", "stdio.h", "math.h", "string.h"] { + match load(h, Path::new("."), None) { + Ok(l) => { + let bad: Vec = l.funcs.iter().filter_map(|f| f.problem.as_ref().map(|p| format!("{}: {p}", f.name))).take(8).collect(); + eprintln!( + "{h}: {} funcs ({} unusable), {} structs, {} consts, {} handles, cflags {:?} libs {:?}\n e.g. bad {:?}\n structs {:?}\n handles {:?}", + l.funcs.len(), l.funcs.iter().filter(|f| f.problem.is_some()).count(), l.structs.len(), l.consts.len(), l.handles.len(), l.cflags, l.libs, bad, + l.structs.iter().map(|s| &s.name).take(12).collect::>(), l.handles.iter().take(12).collect::>() + ); + } + Err(e) => eprintln!("{h}: ERROR {e}"), + } + } + } + + #[test] + fn tricky_declarations() { + let lib = read( + "typedef struct gzFile_s *gzFile;\n\ + typedef long off_t;\n\ + extern off_t gztell(gzFile);\n\ + struct __pthread_mutex_s { int __lock; unsigned int __count; };\n\ + typedef struct __locale_struct *locale_t;\n\ + extern int isalpha_l(int, locale_t) __attribute__ ((__nothrow__ , __leaf__));\n", + ); + assert!(lib.func("gztell").is_some(), "{:?}", lib.funcs); + assert!(lib.func("isalpha_l").is_some(), "{:?}", lib.funcs); + } + + #[test] + fn prefixes() { + assert_eq!(prefix_of("zlib.h"), "zlib"); + assert_eq!(prefix_of("SDL2/SDL.h"), "SDL"); + assert_eq!(prefix_of("my-lib.h"), "my_lib"); + } +} diff --git a/src/check.rs b/src/check.rs index 28c2c97..165215a 100644 --- a/src/check.rs +++ b/src/check.rs @@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet}; use crate::ast::*; use crate::error::{Diag, PResult}; use crate::hir::*; +use crate::cheader::{self, CTy}; use crate::stdlib::{self, ModuleValue}; use crate::types::{Num, Ty}; @@ -17,12 +18,23 @@ pub fn check_project(main: &Program, modules: &[(String, Program)]) -> PResult) -> PResult { + let mut c = Checker::new(); + c.clibs = clibs; + c.program(main, modules) +} + /// What an `import` name stands for. #[derive(Clone, Debug)] enum Target { Builtin(&'static str), /// A file of the user's; the full import path (`utils`, `a.b`). User(String), + /// A C header (index into `clibs`): its names are used as `zlib.name`. + C(usize), + /// One name picked from a C header with `import "zlib.h" { name }`. + CItem(usize), } const BUILTINS: &[&str] = &[ @@ -146,6 +158,10 @@ struct Checker { /// While checking a generic function: what its type names (`T`) stand for. tsubst: HashMap, inst_queue: Vec, + /// The C headers the program imports. + clibs: Vec, + /// Structs that come from C headers: table name -> how C spells the type. + c_structs: HashMap, } // ---------- names used inside a function body (to find what a closure captures) ---------- @@ -410,6 +426,7 @@ fn has_call(h: &HExpr) -> bool { | HExprKind::ListPop(_) | HExprKind::MapCall { .. } | HExprKind::Builtin { .. } + | HExprKind::CCall { .. } | HExprKind::CallValue { .. } | HExprKind::IfaceCall { .. } | HExprKind::Closure { .. } @@ -514,6 +531,8 @@ impl Checker { bounds_of: HashMap::new(), tsubst: HashMap::new(), inst_queue: Vec::new(), + clibs: Vec::new(), + c_structs: HashMap::new(), } } @@ -523,6 +542,7 @@ impl Checker { let mut mods: Vec<(&str, &Program)> = modules.iter().map(|(k, p)| (k.as_str(), p)).collect(); mods.push(("", main)); self.known_modules = modules.iter().map(|(k, _)| k.clone()).collect(); + self.register_c_structs(); for (key, p) in &mods { self.enter(key); @@ -577,7 +597,11 @@ impl Checker { let types = type_order .into_iter() .map(|name| match self.structs.get(name) { - Some(info) => HTypeDef::Struct(HStruct { name: name.to_string(), fields: info.fields.clone() }), + Some(info) => HTypeDef::Struct(HStruct { + name: name.to_string(), + fields: info.fields.clone(), + c: self.c_structs.get(name).cloned(), + }), None => HTypeDef::Enum(HEnum { name: name.to_string(), variants: self.enums[name] @@ -591,7 +615,21 @@ impl Checker { let mut ifaces: Vec<(String, Vec<(String, Vec, Ty)>)> = self.ifaces.iter().map(|(k, i)| (k.clone(), i.methods.clone())).collect(); ifaces.sort_by(|a, b| a.0.cmp(&b.0)); - Ok(HProgram { types, funcs, ifaces }) + let mut c = CInfo::default(); + for lib in &self.clibs { + let add = |list: &mut Vec, items: Vec| { + for i in items { + if !list.contains(&i) { + list.push(i); + } + } + }; + add(&mut c.headers, vec![lib.header.clone()]); + add(&mut c.cflags, lib.cflags.clone()); + add(&mut c.libs, lib.libs.clone()); + add(&mut c.sources, lib.sources.iter().map(|p| p.display().to_string()).collect()); + } + Ok(HProgram { types, funcs, ifaces, c }) } /// Start checking the module `key`. @@ -1012,8 +1050,8 @@ impl Checker { } fn import(&mut self, i: &Import) -> PResult<()> { - if i.alias.is_some() { - return unsupported(i.span, "importing a C library"); + if i.c { + return self.import_c(i); } let alias = i.path.rsplit('.').next().unwrap().to_string(); if self.imports.contains_key(&alias) { @@ -1046,15 +1084,41 @@ impl Checker { fn user_alias(&self, e: &Expr) -> Option { match self.alias_target(e)? { Target::User(p) => Some(p.clone()), - Target::Builtin(_) => None, + _ => None, + } + } + + /// If `e` is the name of an imported C header, which one. + fn c_alias(&self, e: &Expr) -> Option { + match self.alias_target(e)? { + Target::C(i) => Some(*i), + _ => None, + } + } + + /// A name picked from a C header (`import "zlib.h" { name }`), not hidden by a variable. + fn c_item(&self, name: &str) -> Option { + if self.lookup(name).is_some() || self.implicit_field(name).is_some() { + return None; + } + match self.imports.get(name)? { + Target::CItem(i) => Some(*i), + _ => None, } } /// The table name for a type or function written as `name` or `module.name`. fn global_key(&self, e: &Expr) -> Option { match &e.kind { - ExprKind::Ident(n) => Some(self.q(n)), - ExprKind::Field { expr, name } => Some(format!("{}.{}", self.user_alias(expr)?, name)), + ExprKind::Ident(n) => match self.c_item(n) { + Some(i) => Some(format!("{}.{n}", self.clibs[i].prefix)), + None => Some(self.q(n)), + }, + ExprKind::Field { expr, name } => match self.alias_target(expr)? { + Target::User(p) => Some(format!("{p}.{name}")), + Target::C(i) => Some(format!("{}.{name}", self.clibs[*i].prefix)), + _ => None, + }, _ => None, } } @@ -1063,7 +1127,266 @@ impl Checker { fn module_alias(&self, e: &Expr) -> Option<&'static str> { match self.alias_target(e)? { Target::Builtin(m) => Some(m), - Target::User(_) => None, + _ => None, + } + } + + // ---------- C headers ---------- + + fn import_c(&mut self, i: &Import) -> PResult<()> { + let Some(idx) = self.clibs.iter().position(|l| l.header == i.path) else { + return Err(Diag::new(format!("the C header `{}` could not be read", i.path), i.span)); + }; + let lib = &self.clibs[idx]; + match &i.names { + None => { + let alias = i.alias.clone().unwrap_or_else(|| lib.prefix.clone()); + if self.imports.contains_key(&alias) { + return Err(Diag::new( + format!("`{alias}` is imported twice; give the header another name, like `import c{alias} \"{}\"`", i.path), + i.span, + )); + } + self.imports.insert(alias, Target::C(idx)); + } + Some(names) => { + for (n, span) in names { + if !lib.names().contains(&n.as_str()) && lib.func(n).is_none() { + let hint = similar(n, &lib.names()).map(|s| format!(" (did you mean `{s}`?)")).unwrap_or_default(); + return Err(Diag::new(format!("`{}` has no `{n}`{hint}", lib.header), *span)); + } + if self.imports.contains_key(n) { + return Err(Diag::new(format!("`{n}` is imported twice"), *span)); + } + self.imports.insert(n.clone(), Target::CItem(idx)); + } + } + } + Ok(()) + } + + /// The Dino type for a C type, if it has one. + fn c_type(&self, lib: usize, t: &CTy) -> Option { + Some(match t { + CTy::Void => Ty::Unit, + CTy::Num(t) => *t, + CTy::Str => Ty::Str, + CTy::Handle(n) if n == "pointer" => Ty::CPtr("pointer"), + CTy::Handle(n) => Ty::CPtr(Box::leak(format!("{}.{n}", self.clibs[lib].prefix).into_boxed_str())), + CTy::Struct(n) => Ty::Struct(self.structs.get(&format!("{}.{n}", self.clibs[lib].prefix))?.name), + CTy::Ptr { .. } | CTy::Unsupported(_) => return None, + }) + } + + /// Structs from C headers become Dino structs (`raylib.Color`), converted at calls. + fn register_c_structs(&mut self) { + for li in 0..self.clibs.len() { + let prefix = self.clibs[li].prefix.clone(); + for s in self.clibs[li].structs.clone() { + let key = format!("{prefix}.{}", s.name); + let name: &'static str = Box::leak(key.clone().into_boxed_str()); + self.structs.insert(key.clone(), StructInfo { name, fields: Vec::new(), methods: HashMap::new(), span: Span::default() }); + self.c_structs.insert(key, s.c.clone()); + } + for s in self.clibs[li].structs.clone() { + let key = format!("{prefix}.{}", s.name); + let fields: Vec<(String, Ty)> = + s.fields.iter().map(|(n, t)| (n.clone(), self.c_type(li, t).unwrap_or(Ty::CPtr("pointer")))).collect(); + self.structs.get_mut(&key).unwrap().fields = fields; + } + } + } + + /// The type `name` from C header `lib` (`zlib.gzFile`, `raylib.Color`). + fn c_named_type(&self, lib: usize, name: &str, span: Span) -> PResult { + let l = &self.clibs[lib]; + if l.strukt(name).is_some() { + return self.c_type(lib, &CTy::Struct(name.to_string())).ok_or_else(|| Diag::new("unknown C struct", span)); + } + if l.has_handle(name) { + return Ok(self.c_type(lib, &CTy::Handle(name.to_string())).unwrap()); + } + let types: Vec<&str> = l.structs.iter().map(|s| s.name.as_str()).chain(l.handles.iter().map(|h| h.as_str())).collect(); + let hint = similar(name, &types).map(|s| format!(" (did you mean `{s}`?)")).unwrap_or_default(); + Err(Diag::new(format!("`{}` has no type `{name}`{hint}", l.header), span)) + } + + /// A constant from a C header: `zlib.Z_OK`, `raylib.RED`. + fn c_value(&self, lib: usize, name: &str, span: Span) -> PResult { + let l = &self.clibs[lib]; + if let Some(c) = l.konst(name) { + return Ok(match &c.value { + cheader::Value::Int(i) => mk(HExprKind::Int(*i), Ty::Int), + cheader::Value::Float(f) => mk(HExprKind::Float(*f), Ty::Float), + cheader::Value::Str(s) => mk(HExprKind::Str(s.clone()), Ty::Str), + cheader::Value::Struct(sn) => { + let ty = self.c_named_type(lib, sn, span)?; + mk(HExprKind::CRaw(name.to_string()), ty) + } + }); + } + if l.func(name).is_some() { + return Err(Diag::new(format!("`{name}` is a C function; call it like `{name}(...)`"), span)); + } + if l.strukt(name).is_some() || l.has_handle(name) { + return Err(Diag::new(format!("`{name}` is a C type, not a value"), span)); + } + let hint = similar(name, &l.names()).map(|s| format!(" (did you mean `{s}`?)")).unwrap_or_default(); + Err(Diag::new(format!("`{}` has no `{name}`{hint}", l.header), span)) + } + + fn c_call(&mut self, lib: usize, name: &str, args: &[Expr], span: Span, name_span: Span) -> PResult { + let l = &self.clibs[lib]; + let Some(f) = l.func(name).cloned() else { + if l.konst(name).is_some() { + return Err(Diag::new(format!("`{name}` is a constant, not a function"), name_span)); + } + if l.strukt(name).is_some() { + return Err(Diag::new(format!("`{name}` is a C struct; build one with `{name}{{...}}`"), name_span)); + } + let hint = similar(name, &l.names()).map(|s| format!(" (did you mean `{s}`?)")).unwrap_or_default(); + return Err(Diag::new(format!("`{}` has no function `{name}`{hint}", l.header), name_span)); + }; + if let Some(why) = &f.problem { + return Err(Diag::new(format!("`{name}` can't be called from Dino: it {why}"), name_span)); + } + if args.len() != f.params.len() { + let names: Vec = f + .params + .iter() + .map(|(n, t)| if n.is_empty() { self.c_param_text(lib, t) } else { format!("{n}: {}", self.c_param_text(lib, t)) }) + .collect(); + return Err(Diag::new( + format!( + "`{name}` takes {} argument{} ({}), but {} {} given", + f.params.len(), + if f.params.len() == 1 { "" } else { "s" }, + names.join(", "), + args.len(), + if args.len() == 1 { "was" } else { "were" }, + ), + span, + )); + } + let mut hs = Vec::new(); + let mut modes = Vec::new(); + for ((pname, pt), a) in f.params.iter().zip(args) { + let (h, m) = self.c_arg(lib, name, pname, pt, a)?; + hs.push(h); + modes.push(m); + } + let ret = self.c_type(lib, &f.ret).unwrap_or(Ty::CPtr("pointer")); + Ok(mk(HExprKind::CCall { name: name.to_string(), args: hs, modes }, ret)) + } + + /// How a C parameter type reads in a message. + fn c_param_text(&self, lib: usize, t: &CTy) -> String { + match t { + CTy::Ptr { to, .. } if matches!(**to, CTy::Struct(_) | CTy::Handle(_)) => { + self.c_type(lib, to).map_or("pointer".into(), |t| format!("{t} variable")) + } + CTy::Ptr { to, konst } => match self.c_type(lib, to) { + Some(inner) if *konst => format!("{inner} or []{inner}"), + Some(inner) => format!("{inner} variable or []{inner}"), + None => "pointer".into(), + }, + t => self.c_type(lib, t).map_or("?".into(), |t| t.to_string()), + } + } + + fn c_arg(&mut self, lib: usize, fname: &str, pname: &str, pt: &CTy, a: &Expr) -> PResult<(HExpr, CArg)> { + let null = || Ok((mk(HExprKind::Bool(false), Ty::Bool), CArg::Null)); + let is_nil = matches!(a.kind, ExprKind::Nil); + let what = if pname.is_empty() { format!("this argument of `{fname}`") } else { format!("`{pname}` of `{fname}`") }; + match pt { + CTy::Str => { + if is_nil { + return null(); + } + let h = self.expr(a, Some(Ty::Str))?; + match h.ty { + Ty::Str => Ok((h, CArg::Str)), + Ty::List(Ty::Num(Num::U8 | Num::I8)) => Ok((h, CArg::Buffer)), + t => Err(Diag::new(format!("{what} is a `string` (or a `[]u8` to fill in), found `{t}`"), a.span)), + } + } + CTy::Handle(_) => { + let want = self.c_type(lib, pt).unwrap(); + if is_nil { + return Ok((mk(HExprKind::Nil, want), CArg::Value)); + } + let h = self.expr(a, Some(want))?; + match h.ty { + Ty::CPtr(x) if Ty::CPtr(x) == want || x == "pointer" || want == Ty::CPtr("pointer") => Ok((h, CArg::Value)), + Ty::List(e) if want == Ty::CPtr("pointer") && (e.is_number() || *e == Ty::Bool) => Ok((h, CArg::Buffer)), + Ty::Str if want == Ty::CPtr("pointer") => Ok((h, CArg::Str)), + t => Err(Diag::new(format!("{what} is a `{want}`, found `{t}`"), a.span)), + } + } + CTy::Ptr { to, konst } => { + let Some(inner) = self.c_type(lib, to) else { + return Err(Diag::new(format!("{what} has a type Dino can't pass"), a.span)); + }; + if is_nil { + return null(); + } + // `sqlite3 **db`: a pointer variable the function fills in + if let Ty::CPtr(want) = inner { + let h = self.expr(a, Some(inner))?; + let fits = |t: Ty| matches!(t, Ty::CPtr(x) if x == want || x == "pointer" || want == "pointer"); + if !fits(h.ty) { + return Err(Diag::new(format!("{what} is a `{inner}` variable that the function fills in, found `{}`", h.ty), a.span)); + } + if *konst { + return Ok((h, CArg::AddrIn)); + } + let thing = if pname.is_empty() { "this argument".to_string() } else { format!("`{pname}`") }; + let p = self.place(a).map_err(|_| { + Diag::new(format!("`{fname}` writes into {thing}, so it must be a variable (or a field of one)"), a.span) + })?; + return Ok((p, CArg::Addr)); + } + let h = self.expr(a, Some(inner))?; + if h.ty == Ty::list(inner) { + if matches!(inner, Ty::Struct(_)) { + return Err(Diag::new(format!("{what} wants C structs in a row; a Dino list of structs can't be passed"), a.span)); + } + return Ok((h, CArg::Buffer)); + } + if let Ty::CPtr(_) = h.ty { + return Ok((h, CArg::Value)); + } + let h = self.coerce(h, inner, a.span).map_err(|_| { + Diag::new( + format!( + "{what} is {} `{inner}` (or a list `[]{inner}`), found something else", + if *konst { "a" } else { "a variable of type" } + ), + a.span, + ) + })?; + if *konst { + return Ok((h, CArg::AddrIn)); + } + let p = self.place(a).map_err(|_| { + let thing = if pname.is_empty() { "this argument".to_string() } else { format!("`{pname}`") }; + Diag::new(format!("`{fname}` writes into {thing}, so it must be a variable (or a field of one)"), a.span) + })?; + Ok((p, CArg::Addr)) + } + CTy::Num(_) | CTy::Struct(_) => { + let want = self.c_type(lib, pt).unwrap(); + // a C `char` takes a Dino character: `'a'` + if matches!(want, Ty::Num(Num::I8 | Num::U8)) { + let h = self.expr(a, Some(want))?; + if h.ty == Ty::Char { + return Ok((mk(HExprKind::Convert(Box::new(h)), want), CArg::Value)); + } + return Ok((self.coerce(h, want, a.span)?, CArg::Value)); + } + Ok((self.expr_as(a, want)?, CArg::Value)) + } + CTy::Void | CTy::Unsupported(_) => Err(Diag::new(format!("{what} has a type Dino can't pass"), a.span)), } } @@ -1281,6 +1604,10 @@ impl Checker { "f64" => Ok(Ty::Float), n if Num::ALL.iter().any(|k| k.name() == n) => Ok(conversion_target(n).unwrap()), n if self.tsubst.contains_key(n) => Ok(self.tsubst[n]), + n if matches!(self.imports.get(n), Some(Target::CItem(..))) => { + let Some(Target::CItem(lib)) = self.imports.get(n) else { unreachable!() }; + self.c_named_type(*lib, n, span) + } n => { let key = self.q(n); if self.ifaces.contains_key(&key) { @@ -1333,6 +1660,10 @@ impl Checker { } self.instantiate_type(&key, &shown, tys, span) } + Type::Named { path, args } if path.len() == 2 && args.is_empty() && matches!(self.imports.get(&path[0]), Some(Target::C(_))) => { + let Some(Target::C(lib)) = self.imports.get(&path[0]) else { unreachable!() }; + self.c_named_type(*lib, &path[1], span) + } Type::Named { path, args } if path.len() == 2 && args.is_empty() => { let Some(Target::User(module)) = self.imports.get(&path[0]) else { return Err(Diag::new(format!("unknown type `{}.{}` (is `{}` imported?)", path[0], path[1], path[0]), span)); @@ -1496,7 +1827,7 @@ impl Checker { }, Ty::List(_) => HExprKind::ListLit { elems: Vec::new() }, Ty::Map(..) => HExprKind::MapLit { entries: Vec::new() }, - Ty::Error | Ty::Opt(_) | Ty::Weak(_) | Ty::Shared(_) | Ty::Func(_) | Ty::Iface(_) => HExprKind::Nil, + Ty::Error | Ty::Opt(_) | Ty::Weak(_) | Ty::Shared(_) | Ty::Func(_) | Ty::Iface(_) | Ty::CPtr(_) => HExprKind::Nil, Ty::Tuple(items) => HExprKind::TupleLit(items.iter().map(|t| self.zero_expr(*t)).collect()), Ty::Unit => unreachable!("nothing has no zero value"), }; @@ -2218,7 +2549,7 @@ impl Checker { Some(h) => h, None => self.expr(other, None)?, }; - if !matches!(h.ty, Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_)) { + if !matches!(h.ty, Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_) | Ty::CPtr(_)) { return Err(Diag::new( format!("a `{}` can never be nil, so comparing it with `nil` is pointless", h.ty), e.span, @@ -2253,6 +2584,10 @@ impl Checker { let m = self.module_alias(expr).unwrap(); self.module_value(m, name, e.span) } + ExprKind::Field { expr, name } if self.c_alias(expr).is_some() => { + let lib = self.c_alias(expr).unwrap(); + self.c_value(lib, name, e.span) + } ExprKind::Generic { .. } => Err(Diag::new( "a generic type is not a value; build one with `Name[types]{...}` or pick an enum case with `Name[types].Case`", e.span, @@ -2315,7 +2650,7 @@ impl Checker { e.span, )), ExprKind::Nil => match expect { - Some(t @ (Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_))) => Ok(mk(HExprKind::Nil, t)), + Some(t @ (Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_) | Ty::CPtr(_))) => Ok(mk(HExprKind::Nil, t)), _ => Err(Diag::new( "can't tell what `nil` stands for here; it needs a type like `User?` or `error` (for example `return value, nil`)", e.span, @@ -2469,6 +2804,9 @@ impl Checker { if let Some(c) = self.consts.get(&key) { return Ok(c.clone()); } + if let Some(lib) = self.c_item(name) { + return self.c_value(lib, name, span); + } if self.funcs.contains_key(&key) { return self.func_value(&key, name, span); } @@ -2860,7 +3198,7 @@ impl Checker { BinOp::Sub | BinOp::Mul | BinOp::Div => t.is_number(), BinOp::Rem => t.is_int(), BinOp::Eq | BinOp::Ne => { - t.is_number() || matches!(t, Ty::Bool | Ty::Str | Ty::Char) + t.is_number() || matches!(t, Ty::Bool | Ty::Str | Ty::Char | Ty::CPtr(_)) || matches!(t, Ty::Enum(n) if self.enums[n].variants.iter().all(|v| v.fields.is_empty())) } BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => t.is_number() || t == Ty::Str || t == Ty::Char, @@ -3132,6 +3470,14 @@ impl Checker { fn call(&mut self, callee: &Expr, args: &[Expr], span: Span) -> PResult { match &callee.kind { + ExprKind::Field { expr, name } if self.c_alias(expr).is_some() => { + let lib = self.c_alias(expr).unwrap(); + self.c_call(lib, name, args, span, callee.span) + } + ExprKind::Ident(name) if self.c_item(name).is_some() => { + let lib = self.c_item(name).unwrap(); + self.c_call(lib, name, args, span, callee.span) + } ExprKind::Field { expr, name } if self.module_alias(expr).is_some() => { let m = self.module_alias(expr).unwrap(); self.module_call(m, name, args, span, callee.span) @@ -3821,3 +4167,31 @@ fn check_mutating_calls(funcs: &[HFunc]) -> PResult<()> { } Ok(()) } + +/// The name in `names` closest to `name`, if one is close enough to be a likely typo. +fn similar<'a>(name: &str, names: &[&'a str]) -> Option<&'a str> { + fn dist(a: &str, b: &str) -> usize { + let (a, b): (Vec, Vec) = (a.chars().collect(), b.chars().collect()); + let mut prev: Vec = (0..=b.len()).collect(); + let mut prev2: Vec = prev.clone(); + for i in 1..=a.len() { + let mut cur = vec![i; b.len() + 1]; + for j in 1..=b.len() { + let cost = if a[i - 1] == b[j - 1] { 0 } else { 1 }; + cur[j] = (prev[j] + 1).min(cur[j - 1] + 1).min(prev[j - 1] + cost); + // two letters swapped count as one mistake + if i > 1 && j > 1 && a[i - 1] == b[j - 2] && a[i - 2] == b[j - 1] { + cur[j] = cur[j].min(prev2[j - 2] + 1); + } + } + prev2 = std::mem::replace(&mut prev, cur); + } + prev[b.len()] + } + let lower = name.to_lowercase(); + if let Some(n) = names.iter().find(|n| n.to_lowercase() == lower) { + return Some(n); + } + let limit = (name.chars().count() / 3).max(1); + names.iter().map(|n| (dist(&lower, &n.to_lowercase()), *n)).filter(|(d, _)| *d <= limit).min_by_key(|(d, _)| *d).map(|(_, n)| n) +} diff --git a/src/codegen_c.rs b/src/codegen_c.rs index 9d9bf19..ca26725 100644 --- a/src/codegen_c.rs +++ b/src/codegen_c.rs @@ -52,6 +52,7 @@ impl Info { let mut found = false; walk_expr(e, &mut |_| {}, &mut |x| match &x.kind { HExprKind::ListPush { .. } | HExprKind::ListPop(_) | HExprKind::MapCall { op: MapOp::Remove, .. } => found = true, + HExprKind::CCall { modes, .. } if modes.iter().any(|m| matches!(m, CArg::Addr | CArg::Buffer)) => found = true, HExprKind::MethodCall { owner, method, .. } if self.is_mut_method(owner, method) => found = true, _ => {} }); @@ -118,9 +119,32 @@ pub fn generate(p: &HProgram) -> String { } let wrappers = collect_wrappers(p); let main_fails = p.funcs.iter().any(|f| f.owner.is_none() && f.name == "main" && f.ret == Ty::Error); - let mut g = Gen { out: String::new(), indent: 0, tmp: 0, ref_ids: HashSet::new(), info, enums, cur_ret: Ty::Unit, scopes: Vec::new(), pending: Vec::new(), loops: Vec::new(), closures: 0, closure_protos: String::new(), closure_defs: String::new() }; - g.out.push_str(RUNTIME); + let mut g = Gen { out: String::new(), indent: 0, tmp: 0, ref_ids: HashSet::new(), info, enums, cur_ret: Ty::Unit, scopes: Vec::new(), pending: Vec::new(), loops: Vec::new(), closures: 0, closure_protos: String::new(), closure_defs: String::new(), c_structs: HashMap::new() }; + for t in &p.types { + if let HTypeDef::Struct(HStruct { name, c: Some(c), .. }) = t { + g.c_structs.insert(name.clone(), c.clone()); + } + } + // what building needs, for the driver (one item per line, so paths may contain spaces) + for f in &p.c.cflags { + g.out.push_str(&format!("// dino-cflag: {f}\n")); + } + for f in &p.c.libs { + g.out.push_str(&format!("// dino-lib: {f}\n")); + } + for f in &p.c.sources { + g.out.push_str(&format!("// dino-source: {f}\n")); + } + if p.c.headers.is_empty() { + g.out.push_str(RUNTIME); + } else { + // C headers are read (and built) in GNU C mode, with the whole C library visible + g.out.push_str(&RUNTIME.replacen("#define _POSIX_C_SOURCE 200809L\n", "", 1)); + } g.out.push('\n'); + for h in &p.c.headers { + g.out.push_str(&format!("#include \"{h}\"\n")); + } // forward declarations, then bodies: lists first (they only hold pointers), then structs and enums for t in &p.types { @@ -264,7 +288,10 @@ pub fn generate(p: &HProgram) -> String { } for t in &p.types { match t { - HTypeDef::Struct(s) => g.struct_helpers(s), + HTypeDef::Struct(s) => { + g.struct_helpers(s); + g.c_struct_conversions(s); + } HTypeDef::Enum(e) => g.enum_helpers(e), } } @@ -481,6 +508,7 @@ fn tag(t: Ty) -> String { tag(ft.ret) ), Ty::Tuple(items) => format!("T_{}", items.iter().map(|t| tag(*t)).collect::>().join("_")), + Ty::CPtr(_) => "ptr".into(), Ty::Unit => "void".into(), } } @@ -496,6 +524,7 @@ fn c_ty(t: Ty) -> String { Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Tuple(_) | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => tag(t), Ty::Num(n) => num_c(n).into(), Ty::Error => "dino_err".into(), + Ty::CPtr(_) => "void *".into(), Ty::Unit => "void".into(), } } @@ -599,6 +628,7 @@ fn to_str(ty: Ty, c: &str) -> String { Ty::Num(Num::F32) => format!("dino_f32_str({c})"), Ty::Num(_) => format!("dino_i64_str((int64_t)({c}))"), Ty::Error => format!("dino_err_str({c})"), + Ty::CPtr(_) => format!("dino_ptr_str({c})"), Ty::Tuple(_) => unreachable!("several values can't be turned into text"), Ty::Unit => unreachable!("nothing can't be turned into text"), } @@ -626,6 +656,8 @@ struct Gen { closures: u32, closure_protos: String, closure_defs: String, + /// Structs from C headers: table name -> how C spells the type. + c_structs: HashMap, } #[derive(Clone)] @@ -769,6 +801,116 @@ impl Gen { self.out.push_str("};\n\n"); } + /// For a struct from a C header: `S_raylib_Color_to_c` and `S_raylib_Color_from_c`, which copy + /// the fields between Dino's struct and the C one. + fn c_struct_conversions(&mut self, s: &HStruct) { + let Some(cname) = &s.c else { return }; + let c = c_struct(&s.name); + let mut to = Vec::new(); + let mut from = String::new(); + for (name, ty) in &s.fields { + let f = field_name(name); + let (into_c, out_of_c) = match ty { + Ty::Struct(n) if self.c_structs.contains_key(*n) => { + (format!("{}_to_c(v.{f})", c_struct(n)), format!("{}_from_c(v.{name})", c_struct(n))) + } + Ty::CPtr(_) => (format!("(void *)v.{f}"), format!("(void *)v.{name}")), + t => (format!("({})v.{f}", c_ty(*t)), format!("({})v.{name}", c_ty(*t))), + }; + to.push(format!(".{name} = {into_c}")); + from.push_str(&format!(" r.{f} = {out_of_c};\n")); + } + self.out.push_str(&format!( + "static {cname} {c}_to_c({c} v) {{\n {cname} r = {{ {} }};\n return r;\n}}\n\n\ + static {c} {c}_from_c({cname} v) {{\n {c} r;\n{from} return r;\n}}\n\n", + to.join(", ") + )); + } + + /// A Dino value as the C function wants it. + fn to_c_value(&self, ty: Ty, v: &str) -> String { + match ty { + Ty::Struct(n) if self.c_structs.contains_key(n) => format!("{}_to_c({v})", c_struct(n)), + _ => v.to_string(), + } + } + + /// The C type a value is converted to for a C function. + fn c_side_ty(&self, ty: Ty) -> String { + match ty { + Ty::Struct(n) if self.c_structs.contains_key(n) => self.c_structs[n].clone(), + t => c_ty(t), + } + } + + fn c_call(&mut self, name: &str, args: &[HExpr], modes: &[CArg], ty: Ty) -> String { + let vals: Vec<&HExpr> = + args.iter().zip(modes).filter(|(_, m)| !matches!(m, CArg::Addr | CArg::Null)).map(|(a, _)| a).collect(); + let mut vs = self.seq(&vals).into_iter(); + let mut parts = Vec::new(); + let mut after = Vec::new(); + for (a, m) in args.iter().zip(modes) { + match m { + CArg::Null => parts.push("NULL".to_string()), + CArg::Value => { + let v = vs.next().unwrap(); + parts.push(self.to_c_value(a.ty, &v)); + } + CArg::Str => { + let v = vs.next().unwrap(); + let t = self.fresh(); + self.line(&format!("char *{t} = dino_cstr({v});")); + after.push(format!("free({t});")); + parts.push(t); + } + CArg::Buffer => { + let v = vs.next().unwrap(); + parts.push(format!("(void *)({v}).p")); + } + CArg::AddrIn => { + let v = vs.next().unwrap(); + let t = self.fresh(); + let init = self.to_c_value(a.ty, &v); + self.line(&format!("{} {t} = {init};", self.c_side_ty(a.ty))); + parts.push(format!("(void *)&{t}")); + } + CArg::Addr => { + let p = self.place_str(a); + match a.ty { + Ty::Struct(n) if self.c_structs.contains_key(n) => { + let t = self.fresh(); + self.line(&format!("{} {t} = {}_to_c({p});", self.c_structs[n], c_struct(n))); + after.push(format!("{p} = {}_from_c({t});", c_struct(n))); + parts.push(format!("(void *)&{t}")); + } + // `void *` fits every pointer parameter (`long *` and `int64_t *` may differ in C) + _ => parts.push(format!("(void *)&{p}")), + } + } + } + } + let call = format!("{name}({})", parts.join(", ")); + let result = if ty == Ty::Unit { + self.line(&format!("{call};")); + String::new() + } else { + let conv = match ty { + Ty::Str => format!("dino_from_cstr({call})"), + Ty::Struct(n) if self.c_structs.contains_key(n) => format!("{}_from_c({call})", c_struct(n)), + Ty::CPtr(_) => format!("(void *)({call})"), + t => format!("({})({call})", c_ty(t)), + }; + let t = self.fresh(); + self.line(&format!("{} {t} = {conv};", c_ty(ty))); + self.register_temp(&t, ty); + t + }; + for l in after { + self.line(&l); + } + result + } + /// `S_Vec2_str` (how a struct looks when printed) and `S_Vec2_copy` (if it holds lists). fn struct_helpers(&mut self, s: &HStruct) { let c = c_struct(&s.name); @@ -1976,7 +2118,13 @@ static dstr {c}_str({c} l) {{ | HExprKind::Field { .. } | HExprKind::Index { .. } | HExprKind::Unwrap(_) => self.place_str(e), + HExprKind::Nil if matches!(e.ty, Ty::CPtr(_)) => "((void *)0)".to_string(), HExprKind::Nil => format!("(({}){{0}})", c_ty(e.ty)), + HExprKind::CCall { name, args, modes } => self.c_call(name, args, modes, e.ty), + HExprKind::CRaw(text) => match e.ty { + Ty::Struct(n) if self.c_structs.contains_key(n) => format!("{}_from_c({text})", c_struct(n)), + _ => format!("({text})"), + }, HExprKind::Wrap(inner) => { let v = self.expr(inner); if self.heap(inner.ty) { @@ -2064,6 +2212,8 @@ static dstr {c}_str({c} l) {{ format!("{}_alive({v})", c_ty(inner.ty)) } else if inner.ty == Ty::Error { format!("(({v}).set)") + } else if matches!(inner.ty, Ty::CPtr(_)) { + format!("(({v}) != NULL)") } else { format!("(({v}).has)") } @@ -2191,7 +2341,7 @@ static dstr {c}_str({c} l) {{ Ty::Num(Num::U64) => format!("dino_print_u64({v});"), Ty::Num(Num::F32) => format!("dino_print_f32({v});"), Ty::Num(_) => format!("dino_print_i64((int64_t)({v}));"), - Ty::Struct(_) | Ty::Enum(_) | Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Error | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => { + Ty::Struct(_) | Ty::Enum(_) | Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Error | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) | Ty::CPtr(_) => { let t = self.fresh(); self.line(&format!("dstr {t} = {};", to_str(a.ty, v))); self.register_temp(&t, Ty::Str); diff --git a/src/driver.rs b/src/driver.rs index ed97781..e7425ca 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -5,13 +5,40 @@ use std::path::{Path, PathBuf}; use std::process::Command; use std::time::{SystemTime, UNIX_EPOCH}; +use crate::ast::{Item, Program}; use crate::error::{Diag, PResult}; -use crate::{check, codegen_c, loader, parser}; +use crate::{cheader, check, codegen_c, loader, parser}; -/// Dino source -> C source, for a program in a single file. +/// Read the C headers imported by the program. `dir` is the folder of the main file. +pub fn load_c_headers(main: &Program, modules: &[(String, Program)], dir: &Path) -> PResult> { + let mut libs: Vec = Vec::new(); + let all = std::iter::once(("", main)).chain(modules.iter().map(|(k, p)| (k.as_str(), p))); + for (key, p) in all { + // `import a.b` lives in a/b.dino: its headers are found next to it + let mut here = dir.to_path_buf(); + let parts: Vec<&str> = key.split('.').collect(); + for part in &parts[..parts.len().saturating_sub(1)] { + here.push(part); + } + for item in &p.items { + let Item::Import(i) = item else { continue }; + if !i.c || libs.iter().any(|l| l.header == i.path) { + continue; + } + let lib = cheader::load(&i.path, &here, i.link.as_deref()) + .map_err(|m| Diag::new(m, i.span).in_module(key))?; + libs.push(lib); + } + } + Ok(libs) +} + +/// Dino source -> C source, for a program in a single file. C headers it imports are looked +/// for in the current folder and the system's. pub fn compile_to_c(src: &str) -> PResult { let ast = parser::parse(src)?; - let hir = check::check(&ast)?; + let libs = load_c_headers(&ast, &[], Path::new("."))?; + let hir = check::check_project_c(&ast, &[], libs)?; Ok(codegen_c::generate(&hir)) } @@ -40,7 +67,9 @@ pub fn compile_project( let loaded = loaded.map_err(&render)?; let modules: Vec<(String, crate::ast::Program)> = loaded.modules.into_iter().map(|m| (m.key, m.program)).collect(); - let hir = check::check_project(&loaded.main, &modules).map_err(&render)?; + let dir = Path::new(main_path).parent().map(Path::to_path_buf).unwrap_or_default(); + let libs = load_c_headers(&loaded.main, &modules, &dir).map_err(&render)?; + let hir = check::check_project_c(&loaded.main, &modules, libs).map_err(&render)?; Ok(codegen_c::generate(&hir)) } @@ -73,24 +102,80 @@ pub fn build_exe(c_code: &str, out: &Path) -> Result<(), String> { } /// Like `build_exe`, with explicit compiler flags (used by tests to turn on strict warnings). +/// The generated code says which C libraries it needs in `// dino-...:` lines at the top. pub fn build_exe_with(c_code: &str, out: &Path, flags: &[&str]) -> Result<(), String> { + let (mut cflags, mut libs, mut sources) = (Vec::new(), Vec::new(), Vec::new()); + for line in c_code.lines().take_while(|l| l.starts_with("// dino-")) { + let (kind, value) = line["// dino-".len()..].split_once(": ").unwrap_or(("", "")); + match kind { + "cflag" => cflags.push(value.to_string()), + "lib" => libs.push(value.to_string()), + "source" => sources.push(PathBuf::from(value)), + _ => {} + } + } + let uses_c = c_code.contains("\n#include \""); + // C headers often use GNU extensions + let flags: Vec<&str> = flags.iter().map(|f| if uses_c && *f == "-std=c11" { "-std=gnu11" } else { *f }).collect(); + let cc = cheader::cc(); + // C++ files are compiled on their own, then linked in with the C++ library + let mut objects: Vec = Vec::new(); + let mut c_sources: Vec = Vec::new(); + let cleanup = |objects: &[PathBuf]| { + for o in objects { + let _ = std::fs::remove_file(o); + } + }; + for src in &sources { + let ext = src.extension().and_then(|e| e.to_str()).unwrap_or(""); + if ext == "c" { + c_sources.push(src.clone()); + continue; + } + let obj = unique_name("cpp", ".o"); + let cxx = std::env::var("CXX").unwrap_or_else(|_| "c++".to_string()); + let r = Command::new(&cxx).args(["-c", "-O2", "-w"]).args(&cflags).arg(src).arg("-o").arg(&obj).output(); + match r { + Err(e) => { + cleanup(&objects); + return Err(format!("cannot run the C++ compiler `{cxx}` for {}: {e}", src.display())); + } + Ok(o) if !o.status.success() => { + cleanup(&objects); + return Err(format!("{} does not compile:\n{}", src.display(), String::from_utf8_lossy(&o.stderr))); + } + Ok(_) => objects.push(obj), + } + } let c_file = unique_name("prog", ".c"); std::fs::write(&c_file, c_code).map_err(|e| format!("cannot write {}: {e}", c_file.display()))?; - let cc = std::env::var("CC").unwrap_or_else(|_| "cc".to_string()); - let result = Command::new(&cc) - .args(flags) - .arg("-o") - .arg(out) - .arg(&c_file) - .arg("-lm") // the math functions - .output(); + let mut cmd = Command::new(&cc); + cmd.args(&flags).args(&cflags).arg("-o").arg(out).arg(&c_file); + for s in &c_sources { + // the user's own C file: warnings are theirs to see, but not ours to fail on + cmd.arg(s); + } + cmd.args(&objects).args(&libs).arg("-lm"); // the math functions + if !objects.is_empty() { + cmd.arg("-lstdc++"); + } + let result = cmd.output(); let _ = std::fs::remove_file(&c_file); + cleanup(&objects); match result { Err(e) => Err(format!("cannot run the C compiler `{cc}`: {e}\nInstall one (gcc or clang) or set $CC.")), - Ok(o) if !o.status.success() => Err(format!( - "the C compiler rejected the generated code (this is a bug in Dino):\n{}", - String::from_utf8_lossy(&o.stderr) - )), + Ok(o) if !o.status.success() => { + let err = String::from_utf8_lossy(&o.stderr); + if err.contains("undefined reference") || err.contains("cannot find -l") || err.contains("Undefined symbols") || err.contains("ld returned") { + Err(format!( + "linking with the C library failed:\n{err}\nIf a function is missing, name its library in the import: `import \"header.h\" link \"name\"` (for libname.so)." + )) + } else if uses_c { + Err(format!("the C compiler rejected the program:\n{err}")) + } else { + Err(format!("the C compiler rejected the generated code (this is a bug in Dino):\n{err}")) + } + } Ok(_) => Ok(()), } } @@ -100,11 +185,13 @@ pub fn build_exe_with(c_code: &str, out: &Path, flags: &[&str]) -> Result<(), St /// The error's `file` says which module it is in (`None`: the main file). pub fn diagnose( main_src: &str, + dir: &Path, mut read: impl FnMut(&str) -> Option<(String, String)>, ) -> Result<(), Diag> { let loaded = loader::load(main_src, &mut |key| read(key))?; let modules: Vec<(String, crate::ast::Program)> = loaded.modules.into_iter().map(|m| (m.key, m.program)).collect(); - check::check_project(&loaded.main, &modules)?; + let libs = load_c_headers(&loaded.main, &modules, dir)?; + check::check_project_c(&loaded.main, &modules, libs)?; Ok(()) } diff --git a/src/fmt.rs b/src/fmt.rs index f7713e5..be2b165 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -35,7 +35,7 @@ const KEYWORDS: &[&str] = &[ ]; /// Words that make a following `{` open a block (rather than a struct or map literal). -const BLOCK_WORDS: &[&str] = &["func", "if", "for", "match", "struct", "enum", "interface", "else"]; +const BLOCK_WORDS: &[&str] = &["func", "if", "for", "match", "struct", "enum", "interface", "else", "import"]; const BINARY: &[&str] = &[ "+", "-", "*", "/", "%", "==", "!=", "<", ">", "<=", ">=", "&&", "||", "=", "+=", "-=", "*=", "/=", "%=", "=>", @@ -434,14 +434,15 @@ fn gap(prev: &Piece, cur: &Piece, before_prev: Option<&Piece>, prev_is_prefix: b } "(" => { return match prev.kind { - Kind::Word => is_keyword(prev) && prev.text != "func", + // `step` is only a keyword inside `for ... step n`; before `(` it is a name + Kind::Word => is_keyword(prev) && prev.text != "func" && prev.text != "step", Kind::Punct => !matches!(pt, ")" | "]" | "}"), _ => true, }; } "[" => { return match prev.kind { - Kind::Word => is_keyword(prev), + Kind::Word => is_keyword(prev) && prev.text != "step", Kind::Punct => !matches!(pt, ")" | "]"), _ => true, }; diff --git a/src/hir.rs b/src/hir.rs index e38a127..50cd0a0 100644 --- a/src/hir.rs +++ b/src/hir.rs @@ -26,6 +26,8 @@ pub enum BuiltinShape { pub struct HProgram { /// Structs and enums in dependency order: a type comes after the types it contains by value. pub types: Vec, + /// C headers to include, and what building with them needs. + pub c: CInfo, pub funcs: Vec, /// Every interface: its table name and its methods (name, parameters, result). pub ifaces: Vec<(String, Vec<(String, Vec, Ty)>)>, @@ -85,6 +87,35 @@ pub enum HArmBody { pub struct HStruct { pub name: String, pub fields: Vec<(String, Ty)>, + /// For a struct from a C header: how C spells it (`Color`, `struct timeval`). Dino keeps its + /// own copy of the fields and converts at calls. + pub c: Option, +} + +/// What the imported C headers need. +#[derive(Debug, Clone, Default)] +pub struct CInfo { + pub headers: Vec, + pub cflags: Vec, + pub libs: Vec, + pub sources: Vec, +} + +/// How one argument of a C function is passed. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CArg { + /// As it is (numbers, pointers) or converted (structs). + Value, + /// A Dino string as a C string (`const char *`), copied for the call. + Str, + /// `nil`: a NULL pointer. + Null, + /// A variable the function writes into: its address (`int *out`). + Addr, + /// A value the function only reads through a pointer (`const Color *c`). + AddrIn, + /// The items of a list: a pointer to the first one (`const float *values`, `char *buf`). + Buffer, } #[derive(Debug, Clone)] @@ -248,6 +279,10 @@ pub enum HExprKind { /// A struct value. Every field appears exactly once; initializers are listed in the /// order they were written (so they run in that order), omitted fields last. StructLit { name: String, fields: Vec<(String, HExpr)> }, + /// A call of a function from a C header. + CCall { name: String, args: Vec, modes: Vec }, + /// A C expression with no inputs, like a macro constant (`RED`). + CRaw(String), } /// The local variable at the start of a place like `a.b.c`, if it is one. @@ -272,6 +307,7 @@ pub fn walk_expr(e: &HExpr, on_stmt: &mut dyn FnMut(&HStmt), on_expr: &mut dyn F | HExprKind::Bool(_) | HExprKind::Str(_) | HExprKind::Local(..) + | HExprKind::CRaw(_) | HExprKind::Nil => {} HExprKind::Wrap(x) | HExprKind::MakeError(x) @@ -289,7 +325,7 @@ pub fn walk_expr(e: &HExpr, on_stmt: &mut dyn FnMut(&HStmt), on_expr: &mut dyn F walk_expr(lhs, on_stmt, on_expr); walk_expr(rhs, on_stmt, on_expr); } - HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } => { + HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } | HExprKind::CCall { args, .. } => { args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)) } HExprKind::CallValue { callee, args } => { @@ -430,6 +466,11 @@ pub fn expr_mutates_root(e: &HExpr, id: u32, is_mut_method: &dyn Fn(&str, &str) place_root(list).map(|(i, _)| i) == Some(id) } HExprKind::MapCall { map, op: MapOp::Remove, .. } => place_root(map).map(|(i, _)| i) == Some(id), + // a C function writing into a variable or a list + HExprKind::CCall { args, modes, .. } => args + .iter() + .zip(modes) + .any(|(a, m)| matches!(m, CArg::Addr | CArg::Buffer) && place_root(a).map(|(i, _)| i) == Some(id)), // calling a function value may change what it captured HExprKind::CallValue { callee, .. } => place_root(callee).map(|(i, _)| i) == Some(id), // a method called through an interface may change the value inside @@ -449,6 +490,7 @@ pub fn children_mut(e: &mut HExpr, f: &mut dyn FnMut(&mut HExpr)) { | HExprKind::Bool(_) | HExprKind::Str(_) | HExprKind::Local(..) + | HExprKind::CRaw(_) | HExprKind::Nil => {} HExprKind::Wrap(x) | HExprKind::MakeError(x) @@ -470,7 +512,7 @@ pub fn children_mut(e: &mut HExpr, f: &mut dyn FnMut(&mut HExpr)) { f(lhs); f(rhs); } - HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } => { + HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } | HExprKind::CCall { args, .. } => { args.iter_mut().for_each(|a| f(a)) } HExprKind::CallValue { callee, args } => { diff --git a/src/index.rs b/src/index.rs index f5735ce..e576d5b 100644 --- a/src/index.rs +++ b/src/index.rs @@ -34,10 +34,14 @@ pub struct Sym { #[derive(Debug, Clone)] pub struct Import { - /// The name the module is used by (`utils`, or `fs` for `tools.fs`). + /// The name the module is used by (`utils`, or `fs` for `tools.fs`, or `zlib` for "zlib.h"). pub name: String, - /// The path as written (`utils`, `tools.fs`); empty for `import c "x.h"`. + /// The path as written (`utils`, `tools.fs`); empty for a C header. pub path: String, + /// For a C header: the header (`zlib.h`), the names picked in `{ }`, and `link "..."`. + pub header: Option, + pub picked: Vec, + pub link: Option, } #[derive(Debug, Clone, Default)] @@ -84,6 +88,49 @@ impl Index { } } + /// `import "zlib.h"`, `import z "zlib.h" { a, b } link "z"`: the import and the token after it. + fn c_import(&self, toks: &[crate::lexer::Token], j: usize) -> Option<(Import, usize)> { + let text = |k: usize| match toks.get(k).map(|t| &t.tok) { + Some(Tok::Str(parts)) => match parts.as_slice() { + [crate::lexer::StrPart::Text(t)] => Some(t.clone()), + _ => None, + }, + _ => None, + }; + let mut k = j + 1; + let alias = match toks.get(k).map(|t| &t.tok) { + Some(Tok::Ident(a)) if text(k + 1).is_some() => { + k += 1; + Some(a.clone()) + } + _ => None, + }; + let header = text(k)?; + k += 1; + let mut picked = Vec::new(); + if toks.get(k).map(|t| &t.tok) == Some(&Tok::LBrace) { + k += 1; + while let Some(t) = toks.get(k) { + match &t.tok { + Tok::RBrace => { + k += 1; + break; + } + Tok::Ident(n) => picked.push(n.clone()), + _ => {} + } + k += 1; + } + } + let mut link = None; + if matches!(toks.get(k).map(|t| &t.tok), Some(Tok::Ident(w)) if w == "link") { + link = text(k + 1); + k += 2; + } + let name = alias.unwrap_or_else(|| crate::cheader::prefix_of(&header)); + Some((Import { name, path: String::new(), header: Some(header), picked, link }, k)) + } + fn scan_top(&mut self) { let toks = self.toks.clone(); let mut i = 0; @@ -96,8 +143,13 @@ impl Index { } let Some(head) = toks.get(j) else { break }; match head.tok { + Tok::Import if self.c_import(&toks, j).is_some() => { + let (imp, next) = self.c_import(&toks, j).unwrap(); + self.imports.push(imp); + i = next.max(i + 1); + } Tok::Import => { - // import a.b.c | import c "header.h" + // import a.b.c let mut k = j + 1; let mut parts: Vec = Vec::new(); while let Some(Tok::Ident(s)) = toks.get(k).map(|t| &t.tok) { @@ -111,7 +163,13 @@ impl Index { } if let Some(last) = parts.last() { let aliased = matches!(toks.get(k).map(|t| &t.tok), Some(Tok::Str(_))); - self.imports.push(Import { name: last.clone(), path: if aliased { String::new() } else { parts.join(".") } }); + self.imports.push(Import { + name: last.clone(), + path: if aliased { String::new() } else { parts.join(".") }, + header: None, + picked: Vec::new(), + link: None, + }); } i = k.max(i + 1); } diff --git a/src/lib.rs b/src/lib.rs index 661dc08..c6b63a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod ast; pub mod check; +pub mod cheader; pub mod codegen_c; pub mod driver; pub mod error; diff --git a/src/loader.rs b/src/loader.rs index 5f27e5d..1f48a0d 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -35,7 +35,7 @@ fn load_imports( ) -> PResult<()> { for item in &p.items { let Item::Import(i) = item else { continue }; - if i.alias.is_some() || stdlib::MODULES.contains(&i.path.as_str()) { + if i.c || stdlib::MODULES.contains(&i.path.as_str()) { continue; } let key = i.path.clone(); diff --git a/src/lsp.rs b/src/lsp.rs index 198c09e..8d7b877 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -169,7 +169,8 @@ impl Server { let dir: Option = uri_to_path(uri).and_then(|p| p.parent().map(PathBuf::from)); let docs = &self.docs; let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - driver::diagnose(text, |key| { + let here = dir.clone().unwrap_or_default(); + driver::diagnose(text, &here, |key| { let file = dir.as_ref()?.join(format!("{}.dino", key.replace('.', "/"))); // an import that is open in the editor counts with its unsaved changes for (u, t) in docs { @@ -355,6 +356,39 @@ fn completion_kind(k: Kind) -> i64 { } impl Server { + /// The C header behind an import, read the same way the compiler reads it. + fn c_lib(&self, uri: &str, imp: &crate::index::Import) -> Option { + let header = imp.header.as_ref()?; + let dir = uri_to_path(uri)?.parent()?.to_path_buf(); + crate::cheader::load(header, &dir, imp.link.as_deref()).ok() + } + + /// If the word starting at character `a` of `line` names something from a C header + /// (`zlib.compress`, or a name picked in `import "zlib.h" { compress }`): the header and the name. + fn c_name_at(&self, uri: &str, line: &str, a: usize, word: &str) -> Option<(crate::cheader::Lib, String)> { + let ix = self.indexes.get(uri)?; + let chars: Vec = line.chars().collect(); + if a > 0 && chars[a - 1] == '.' { + let q = word_before(line, a - 1)?; + let imp = ix.imports.iter().find(|i| i.name == q && i.header.is_some() && i.picked.is_empty())?; + return Some((self.c_lib(uri, imp)?, word.to_string())); + } + let imp = ix.imports.iter().find(|i| i.picked.iter().any(|p| p == word))?; + Some((self.c_lib(uri, imp)?, word.to_string())) + } + + fn c_completion_kind(lib: &crate::cheader::Lib, name: &str) -> i64 { + if lib.func(name).is_some() { + 3 + } else if lib.strukt(name).is_some() { + 22 + } else if lib.konst(name).is_some() { + 21 + } else { + 7 + } + } + fn set_text(&mut self, uri: &str, text: &str) { self.docs.insert(uri.to_string(), text.to_string()); if let Some(ix) = Index::new(text) { @@ -540,6 +574,13 @@ impl Server { fn hover(&self, uri: &str, line: usize, ch: usize) -> Json { let line_text = self.line_of(uri, line).unwrap_or(""); let col = char_of_units(line_text, ch); + if let Some((word, a, _)) = word_at(line_text, col) { + if let Some((lib, name)) = self.c_name_at(uri, line_text, a, &word) { + if let Some(text) = lib.describe(&name) { + return hover_text(format!("{text}\n\nfrom the C header {}", lib.header)); + } + } + } // a name from a built-in module: `math.sqrt` if let Some((word, a, _)) = word_at(line_text, col) { let chars: Vec = line_text.chars().collect(); @@ -588,6 +629,16 @@ impl Server { }; if a > 0 && chars[a - 1] == '.' { let left = word_before(line_text, a - 1); + // a C header: everything Dino can use from it + if let Some(imp) = left.as_deref().and_then(|q| ix.imports.iter().find(|i| i.name == q && i.header.is_some())) { + if let Some(lib) = self.c_lib(uri, imp) { + for n in lib.names() { + let detail = lib.func(n).map(|f| lib.signature(f)).unwrap_or_default(); + push(&mut items, n, Self::c_completion_kind(&lib, n), &detail); + } + } + return Json::Arr(items); + } if let Some(q) = left.as_deref() { // a built-in module if stdlib::MODULES.contains(&q) && ix.module(q).is_some() { @@ -651,7 +702,18 @@ impl Server { push(&mut items, &s.name, completion_kind(s.kind), ""); } for imp in &ix.imports { - push(&mut items, &imp.name, 9, "module"); + if imp.picked.is_empty() { + push(&mut items, &imp.name, 9, if imp.header.is_some() { "C header" } else { "module" }); + continue; + } + let lib = self.c_lib(uri, imp); + for n in &imp.picked { + let (kind, detail) = match &lib { + Some(l) => (Self::c_completion_kind(l, n), l.func(n).map(|f| l.signature(f)).unwrap_or_default()), + None => (3, String::new()), + }; + push(&mut items, n, kind, &detail); + } } for k in [ "func", "var", "const", "import", "struct", "enum", "interface", "pub", "if", "else", "for", "in", "step", "match", @@ -706,13 +768,21 @@ impl Server { let Some(name) = word_before(open_line, oc) else { return Json::Null }; let name_start = oc - name.chars().count(); let units_before_name: usize = utf16_len(&open_line.chars().take(name_start).collect::()); - let Some(f) = self.resolve(uri, ol, units_before_name) else { return Json::Null }; - if !matches!(f.sym.kind, Kind::Func | Kind::Method) { - return Json::Null; - } - let decl = Self::declaration_text(&f); - // `pub func add(a: int, b: int): int` -> `add(a: int, b: int): int` - let label = decl.find("func ").map_or(decl.clone(), |i| decl[i + 5..].trim().to_string()); + let label = match self.c_name_at(uri, open_line, name_start, &name) { + Some((lib, n)) => match lib.func(&n) { + Some(f) => lib.signature(f), + None => return Json::Null, + }, + None => { + let Some(f) = self.resolve(uri, ol, units_before_name) else { return Json::Null }; + if !matches!(f.sym.kind, Kind::Func | Kind::Method) { + return Json::Null; + } + let decl = Self::declaration_text(&f); + // `pub func add(a: int, b: int): int` -> `add(a: int, b: int): int` + decl.find("func ").map_or(decl.clone(), |i| decl[i + 5..].trim().to_string()) + } + }; let (Some(p0), Some(p1)) = (label.find('('), label.rfind(')')) else { return Json::Null }; // the parameters: split the text between the parentheses at top-level commas let mut params: Vec = Vec::new(); diff --git a/src/main.rs b/src/main.rs index f172bfe..69a55aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ fn usage() -> ! { eprintln!(" dino check check for errors without building"); eprintln!(" dino fmt [--check] .. format code in place (--check only reports; `-` = stdin to stdout)"); eprintln!(" dino lsp run the language server (for editors)"); + eprintln!(" dino header [name] show what Dino can use from a C header"); eprintln!(" dino parse print the syntax tree"); eprintln!(" dino tokens print the tokens"); process::exit(2); @@ -35,6 +36,59 @@ fn dino_files(path: &std::path::Path, out: &mut Vec) { } } +/// `dino header zlib.h [name] [--link z]`: what Dino code can use from a C header. +fn run_header(args: &[String]) -> ! { + let mut header: Option<&str> = None; + let mut filter: Option<&str> = None; + let mut link: Option<&str> = None; + let mut it = args.iter(); + while let Some(a) = it.next() { + match a.as_str() { + "--link" => link = it.next().map(String::as_str), + _ if header.is_none() => header = Some(a), + _ => filter = Some(a), + } + } + let Some(header) = header else { usage() }; + let dir = env::current_dir().unwrap_or_default(); + let lib = dino::cheader::load(header, &dir, link).unwrap_or_else(|e| die(&format!("error: {e}"))); + let shown = |n: &str| filter.map_or(true, |f| n.to_lowercase().contains(&f.to_lowercase())); + let mut text = format!("import \"{header}\" // use the names below as {}.name\n", lib.prefix); + if !lib.libs.is_empty() || !lib.cflags.is_empty() { + text.push_str(&format!("// builds with: {}\n", lib.cflags.iter().chain(&lib.libs).cloned().collect::>().join(" "))); + } + let mut section = |title: &str, lines: Vec| { + if !lines.is_empty() { + text.push_str(&format!("\n// {title}\n")); + for l in lines { + text.push_str(&l); + text.push('\n'); + } + } + }; + section("types", lib.structs.iter().filter(|s| shown(&s.name)).map(|s| lib.describe(&s.name).unwrap()).collect()); + section( + "pointer types (pass them around, compare them with nil)", + lib.handles.iter().filter(|h| shown(h)).map(|h| format!("{}.{h}", lib.prefix)).collect(), + ); + section( + "functions", + lib.funcs.iter().filter(|f| f.problem.is_none() && shown(&f.name)).map(|f| format!("func {}", lib.signature(f))).collect(), + ); + section("constants", lib.consts.iter().filter(|c| shown(&c.name)).map(|c| lib.describe(&c.name).unwrap()).collect()); + section( + "functions Dino can't call yet", + lib.funcs + .iter() + .filter(|f| shown(&f.name)) + .filter_map(|f| f.problem.as_ref().map(|p| format!("{}: it {p}", f.name))) + .collect(), + ); + // a closed pipe (`| head`) is not an error + let _ = std::io::Write::write_all(&mut std::io::stdout(), text.as_bytes()); + process::exit(0); +} + /// `dino fmt [--check] files-or-folders...` fn run_fmt(args: &[String]) -> ! { // `dino fmt -`: text in on stdin, tidy text out on stdout (for editors) @@ -107,6 +161,9 @@ fn main() { if args.first().map(String::as_str) == Some("fmt") { run_fmt(&args[1..]); } + if args.first().map(String::as_str) == Some("header") { + run_header(&args[1..]); + } if args.len() < 2 { usage(); } diff --git a/src/parser.rs b/src/parser.rs index fb0ac4c..d4deadc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -164,21 +164,65 @@ impl Parser { fn import(&mut self) -> PResult { let span = self.bump().span; - let first = self.ident()?; - let item = if let Tok::Str(parts) = self.peek().clone() { - self.bump(); - let path = match parts.as_slice() { - [LexPart::Text(t)] => t.clone(), - _ => return Err(Diag::new("an import path must be plain text", span)), - }; - Import { alias: Some(first), path, span } + let plain_text = |p: &mut Self| -> PResult { + let t = p.bump(); + match &t.tok { + Tok::Str(parts) => match parts.as_slice() { + [LexPart::Text(t)] => Ok(t.clone()), + [] => Ok(String::new()), + _ => Err(Diag::new("this must be plain text, without `{...}` inside", t.span)), + }, + _ => Err(Diag::new("expected text in quotes here", t.span)), + } + }; + // `import "zlib.h"` or `import z "zlib.h"` + let alias = if matches!(self.peek(), Tok::Ident(_)) && matches!(self.peek_n(1), Tok::Str(_)) { + Some(self.ident()?) } else { - let mut path = first; + None + }; + let item = if matches!(self.peek(), Tok::Str(_)) { + let path = plain_text(self)?; + if path.is_empty() { + return Err(Diag::new("the name of a C header can't be empty", span)); + } + let mut names = None; + if self.eat(&Tok::LBrace) { + let mut list = Vec::new(); + loop { + self.skip_newlines(); + if self.eat(&Tok::RBrace) { + break; + } + let s = self.span(); + list.push((self.ident()?, s)); + self.skip_newlines(); + if !self.eat(&Tok::Comma) { + self.skip_newlines(); + if !self.eat(&Tok::RBrace) { + return self.unexpected("`,` or `}` after a name"); + } + break; + } + } + if list.is_empty() { + return Err(Diag::new("pick at least one name in `{ }`, or leave the braces out to get everything", span)); + } + names = Some(list); + } + let mut link = None; + if matches!(self.peek(), Tok::Ident(w) if w == "link") { + self.bump(); + link = Some(plain_text(self)?); + } + Import { alias, path, span, c: true, names, link } + } else { + let mut path = self.ident()?; while self.eat(&Tok::Dot) { path.push('.'); path.push_str(&self.ident()?); } - Import { alias: None, path, span } + Import { alias: None, path, span, c: false, names: None, link: None } }; self.terminator()?; Ok(Item::Import(item)) diff --git a/src/runtime.c b/src/runtime.c index 83222d8..7cd91f7 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -608,3 +608,16 @@ static void dino_time_sleep(double secs) { ts.tv_nsec = (long)((secs - (double)ts.tv_sec) * 1e9); nanosleep(&ts, NULL); } + +/* ---- C libraries ---- */ + +static dstr dino_from_cstr(const char *p) { + return p ? dino_new_str(p, (int64_t)strlen(p)) : dino_lit("", 0); +} + +static dstr dino_ptr_str(const void *p) { + if (!p) return dino_lit("nil", 3); + char b[32]; + int n = snprintf(b, sizeof b, "%p", p); + return dino_new_str(b, n); +} diff --git a/src/types.rs b/src/types.rs index 1a039bd..0fd65ce 100644 --- a/src/types.rs +++ b/src/types.rs @@ -85,6 +85,9 @@ pub enum Ty { Iface(&'static str), /// A function value: `func(int, string): bool`. Func(&'static FuncTy), + /// A pointer from a C library that Dino only passes around: `SDL.SDL_Window`, `zlib.gzFile`. + /// `"pointer"` (C's `void *`) goes with every other pointer type. + CPtr(&'static str), /// The "type" of a call that returns nothing. Unit, } @@ -201,6 +204,7 @@ impl fmt::Display for Ty { } f.write_str(")") } + Ty::CPtr(name) => f.write_str(name), Ty::Unit => f.write_str("nothing"), } } diff --git a/tests/clib.rs b/tests/clib.rs new file mode 100644 index 0000000..b3099a9 --- /dev/null +++ b/tests/clib.rs @@ -0,0 +1,300 @@ +//! Using C (and C++) libraries: `import "header.h"`. +//! Programs run under the address sanitizer, like the other tests. + +use std::path::PathBuf; +use std::process::Command; + +use dino::driver; + +/// A fresh folder with the given files in it. +fn folder(files: &[(&str, &str)]) -> PathBuf { + static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + let n = N.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let dir = std::env::temp_dir().join(format!("dino-clib-{}-{n}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + for (name, text) in files { + std::fs::write(dir.join(name), text).unwrap(); + } + dir +} + +/// Compile `main.dino` in a folder with these files and run it; returns what it printed. +fn run(files: &[(&str, &str)]) -> String { + let dir = folder(files); + let c = driver::compile_file(dir.join("main.dino").to_str().unwrap()).unwrap_or_else(|e| panic!("{e}")); + let exe = dir.join("prog"); + let sanitized = driver::build_exe_with(&c, &exe, &["-std=c11", "-fwrapv", "-w", "-fsanitize=address", "-g"]); + if let Err(e) = sanitized { + assert!(e.contains("asan"), "{e}"); + driver::build_exe(&c, &exe).unwrap(); + } + let o = Command::new(&exe).env("ASAN_OPTIONS", "detect_leaks=1").output().unwrap(); + let stderr = String::from_utf8_lossy(&o.stderr).to_string(); + assert!(!stderr.contains("Sanitizer"), "memory error:\n{stderr}"); + assert!(o.status.success(), "exit {:?}\n{stderr}", o.status.code()); + let _ = std::fs::remove_dir_all(&dir); + String::from_utf8_lossy(&o.stdout).to_string() +} + +/// The error message for a program that should not compile. +fn error(files: &[(&str, &str)]) -> String { + let dir = folder(files); + let r = driver::compile_file(dir.join("main.dino").to_str().unwrap()); + let _ = std::fs::remove_dir_all(&dir); + match r { + Ok(_) => panic!("expected an error"), + Err(e) => e, + } +} + +fn have_pkg(name: &str) -> bool { + Command::new("pkg-config").args(["--exists", name]).status().map_or(false, |s| s.success()) +} + +#[test] +fn the_c_library() { + let out = run(&[( + "main.dino", + "import \"math.h\" { cbrt, hypot }\nimport \"string.h\"\nimport \"stdlib.h\"\n\n\ + func main() {\n print(cbrt(27.0), hypot(3.0, 4.0))\n print(string.strlen(\"héllo\"), stdlib.abs(-4))\n print(stdlib.atoi(\"42abc\") + 1)\n}\n", + )]); + assert_eq!(out, "3.0 5.0\n6 4\n43\n"); +} + +const SHAPES_H: &str = r#" +#ifdef __cplusplus +extern "C" { +#endif +typedef struct { double w, h; } Size; +typedef struct { Size size; int id; } Box; +typedef struct Counter Counter; +enum Mode { MODE_A, MODE_B = 10, MODE_C }; +#define SHAPES_VERSION "1.2" +#define MAX_SIDES (4 * 3) +#define BIG ((unsigned)1 << 20) +#define UNIT_BOX (Box){ { 1.0, 1.0 }, 7 } +double area(Size s); +void grow(Size *s, double by); +Box make_box(int id); +int sum(const int *values, int n); +void fill(char *buf, int n); +int count_chars(const char *text, char c); +Counter *counter_new(const char *name); +void counter_add(Counter *c, int n); +int counter_total(const Counter *c); +const char *counter_name(const Counter *c); +void counter_free(Counter *c); +#ifdef __cplusplus +} +#endif +"#; + +const SHAPES_C: &str = r#" +#include "shapes.h" +#include +#include +struct Counter { char name[32]; int total; }; +double area(Size s) { return s.w * s.h; } +void grow(Size *s, double by) { s->w *= by; s->h *= by; } +Box make_box(int id) { Box b = { { 2.0, 3.0 }, id }; return b; } +int sum(const int *values, int n) { int t = 0; for (int i = 0; i < n; i++) t += values[i]; return t; } +void fill(char *buf, int n) { for (int i = 0; i < n; i++) buf[i] = 'a' + i; } +int count_chars(const char *text, char c) { int k = 0; for (; *text; text++) k += *text == c; return k; } +Counter *counter_new(const char *name) { Counter *c = calloc(1, sizeof *c); strncpy(c->name, name, 31); return c; } +void counter_add(Counter *c, int n) { c->total += n; } +int counter_total(const Counter *c) { return c->total; } +const char *counter_name(const Counter *c) { return c->name; } +void counter_free(Counter *c) { free(c); } +"#; + +const SHAPES_MAIN: &str = r#"import "shapes.h" + +struct Holder { + size: shapes.Size + func double_it() { + shapes.grow(size, 2.0) + } +} + +func main() { + var s = shapes.Size{2.0, 3.0} + print(shapes.area(s), shapes.SHAPES_VERSION, shapes.MAX_SIDES, shapes.BIG) + shapes.grow(s, 10.0) + print(s) + var h = Holder{s} + h.double_it() + print(h.size.w) + print(shapes.make_box(5), shapes.UNIT_BOX.id) + print(shapes.MODE_A, shapes.MODE_B, shapes.MODE_C) + var nums: []i32 = [1, 2, 3, 4] + print(shapes.sum(nums, i32(nums.len()))) + var buf: []u8 = [0, 0, 0] + shapes.fill(buf, 3) + print(buf) + print(shapes.count_chars("banana", 'a')) + var c = shapes.counter_new("apples") + for i in 1..5 { + shapes.counter_add(c, i32(i)) + } + print(shapes.counter_name(c), shapes.counter_total(c), c != nil) + shapes.counter_free(c) + var none: shapes.Counter + print(none == nil) +} +"#; + +const SHAPES_OUT: &str = "6.0 1.2 12 1048576\nshapes.Size{w: 20.0, h: 30.0}\n40.0\n\ +shapes.Box{size: shapes.Size{w: 2.0, h: 3.0}, id: 5} 7\n0 10 11\n10\n[97, 98, 99]\n3\napples 10 true\ntrue\n"; + +#[test] +fn your_own_c_file() { + let out = run(&[("shapes.h", SHAPES_H), ("shapes.c", SHAPES_C), ("main.dino", SHAPES_MAIN)]); + assert_eq!(out, SHAPES_OUT); +} + +#[test] +fn your_own_cpp_file() { + if Command::new("c++").arg("--version").output().is_err() { + return; + } + let cpp = r#" +#include "shapes.h" +#include +#include +#include +struct Counter { std::string name; std::vector items; }; +extern "C" { +double area(Size s) { return s.w * s.h; } +void grow(Size *s, double by) { s->w *= by; s->h *= by; } +Box make_box(int id) { return Box{ { 2.0, 3.0 }, id }; } +int sum(const int *values, int n) { return std::accumulate(values, values + n, 0); } +void fill(char *buf, int n) { for (int i = 0; i < n; i++) buf[i] = 'a' + i; } +int count_chars(const char *text, char c) { std::string s(text); int k = 0; for (char x : s) k += x == c; return k; } +Counter *counter_new(const char *name) { return new Counter{name, {}}; } +void counter_add(Counter *c, int n) { c->items.push_back(n); } +int counter_total(const Counter *c) { return std::accumulate(c->items.begin(), c->items.end(), 0); } +const char *counter_name(const Counter *c) { return c->name.c_str(); } +void counter_free(Counter *c) { delete c; } +} +"#; + let out = run(&[("shapes.h", SHAPES_H), ("shapes.cpp", cpp), ("main.dino", SHAPES_MAIN)]); + assert_eq!(out, SHAPES_OUT); +} + +#[test] +fn picked_names_are_used_bare() { + let out = run(&[ + ("shapes.h", SHAPES_H), + ("shapes.c", SHAPES_C), + ( + "main.dino", + "import \"shapes.h\" { Size, area, MAX_SIDES }\n\nfunc main() {\n var s: Size = Size{1.5, 2.0}\n print(area(s), MAX_SIDES)\n}\n", + ), + ]); + assert_eq!(out, "3.0 12\n"); +} + +#[test] +fn zlib_through_pkg_config() { + if !have_pkg("zlib") { + return; + } + let out = run(&[( + "main.dino", + r#"import "zlib.h" + +func main() { + var text = "hello hello hello hello hello hello hello" + var data: []u8 + for c in text { + data.push(u8(int(c))) + } + var size = zlib.compressBound(u64(data.len())) + var packed: []u8 + for i in 0..int(size) { + packed.push(0) + } + print(zlib.compress(packed, size, data, u64(data.len())) == zlib.Z_OK, size < u64(data.len())) + var back: []u8 + for i in 0..data.len() { + back.push(0) + } + var n = u64(back.len()) + print(zlib.uncompress(back, n, packed, size) == zlib.Z_OK, string(back) == string(data), n) + print(zlib.gzopen("/no/such/dir/x.gz", "rb") == nil) +} +"#, + )]); + assert_eq!(out, "true true\ntrue true 41\ntrue\n"); +} + +#[test] +fn sqlite_fills_in_pointer_variables() { + if !have_pkg("sqlite3") { + return; + } + let out = run(&[( + "main.dino", + r#"import "sqlite3.h" + +func main() { + var db: sqlite3.sqlite3 + print(sqlite3.sqlite3_open(":memory:", db) == sqlite3.SQLITE_OK, db != nil) + var stmt: sqlite3.sqlite3_stmt + sqlite3.sqlite3_prepare_v2(db, "select 6 * 7 union select 1 order by 1", -1, stmt, nil) + for sqlite3.sqlite3_step(stmt) == sqlite3.SQLITE_ROW { + print(sqlite3.sqlite3_column_int(stmt, 0)) + } + sqlite3.sqlite3_finalize(stmt) + print(sqlite3.sqlite3_close(db) == sqlite3.SQLITE_OK) +} +"#, + )]); + assert_eq!(out, "true true\n1\n42\ntrue\n"); +} + +#[test] +fn raylib_structs_without_a_window() { + if !have_pkg("raylib") { + return; + } + let out = run(&[( + "main.dino", + "import \"raylib.h\"\n\nfunc main() {\n var c = raylib.Fade(raylib.RED, 0.5)\n print(c, raylib.ColorToInt(raylib.Color{0, 0, 0, 255}))\n var p = raylib.Vector2{1.0, 1.0}\n print(raylib.CheckCollisionPointRec(p, raylib.Rectangle{0.0, 0.0, 2.0, 2.0}))\n}\n", + )]); + assert_eq!(out, "raylib.Color{r: 230, g: 41, b: 55, a: 127} 255\ntrue\n"); +} + +#[test] +fn errors_name_the_problem() { + let e = error(&[("main.dino", "import \"no_such_header.h\"\n\nfunc main() {}\n")]); + assert!(e.contains("cannot find the C header `no_such_header.h`"), "{e}"); + + let e = error(&[("shapes.h", SHAPES_H), ("main.dino", "import \"shapes.h\" { aera }\n\nfunc main() {}\n")]); + assert!(e.contains("`shapes.h` has no `aera` (did you mean `area`?)"), "{e}"); + + let e = error(&[("main.dino", "import \"stdio.h\"\n\nfunc main() {\n stdio.printf(\"hi\")\n}\n")]); + assert!(e.contains("`printf` can't be called from Dino: it takes a variable number of arguments"), "{e}"); + + let e = error(&[("shapes.h", SHAPES_H), ("main.dino", "import \"shapes.h\"\n\nfunc main() {\n shapes.grow(shapes.Size{1.0, 1.0}, 2.0)\n}\n")]); + assert!(e.contains("`grow` writes into `s`, so it must be a variable"), "{e}"); + + let e = error(&[("shapes.h", SHAPES_H), ("main.dino", "import \"shapes.h\"\n\nfunc main() {\n var n = 3\n print(shapes.make_box(n))\n}\n")]); + assert!(e.contains("expected `i32`, found `int` (convert it with `i32(...)`)"), "{e}"); + + let e = error(&[("shapes.h", SHAPES_H), ("main.dino", "import \"shapes.h\"\n\nfunc main() {\n print(shapes.area())\n}\n")]); + assert!(e.contains("`area` takes 1 argument (s: shapes.Size), but 0 were given"), "{e}"); +} + +#[test] +fn a_missing_library_says_how_to_link() { + let dir = folder(&[( + "main.dino", + "import \"math.h\" link \"dino_no_such_lib\"\n\nfunc main() {\n print(math.sqrt(4.0))\n}\n", + )]); + let c = driver::compile_file(dir.join("main.dino").to_str().unwrap()).unwrap(); + let e = driver::build_exe(&c, &dir.join("prog")).unwrap_err(); + let _ = std::fs::remove_dir_all(&dir); + assert!(e.contains("linking with the C library failed") && e.contains("link \"name\""), "{e}"); +} diff --git a/tests/fmt.rs b/tests/fmt.rs index 668c01f..53a73fb 100644 --- a/tests/fmt.rs +++ b/tests/fmt.rs @@ -104,3 +104,12 @@ fn doc_examples_are_stable() { } assert!(n > 20, "only {n} doc examples were formattable"); } + +#[test] +fn step_as_a_name_and_c_imports() { + let src = "import \"raylib.h\" { Color,RED }\nimport \"zlib.h\" link \"z\"\n\nfunc step(dt: f32) {}\n\nfunc main() {\n step(1.0)\n for i in 0..10 step 2 { print(i) }\n}\n"; + assert_eq!( + fmt(src), + "import \"raylib.h\" { Color, RED }\nimport \"zlib.h\" link \"z\"\n\nfunc step(dt: f32) {}\n\nfunc main() {\n step(1.0)\n for i in 0..10 step 2 { print(i) }\n}\n" + ); +} diff --git a/tests/lsp.rs b/tests/lsp.rs index 74e5066..7122bdc 100644 --- a/tests/lsp.rs +++ b/tests/lsp.rs @@ -225,3 +225,33 @@ fn signature_help_marks_the_parameter() { assert_eq!(r.get("activeParameter").and_then(Json::as_i64), Some(1), "{r:?}"); assert!(r.to_string().contains("add(a: int, b: int): int"), "{r:?}"); } + +// ---------- C headers ---------- + +fn with_header(src: &str, method: &str, line: i64, ch: i64) -> Json { + let dir = std::env::temp_dir().join(format!("dino-lsp-c-{}-{}", std::process::id(), method.replace('/', "_"))); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("geo.h"), "typedef struct { double x, y; } Point;\n#define ORIGIN_NAME \"origin\"\n/* distance */\ndouble dist(Point a, Point b);\nint clamp(int v, int lo, int hi);\n").unwrap(); + let uri = format!("file://{}/main.dino", dir.display()); + let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open(&uri, src), request(2, method, at(&uri, line, ch))]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + let _ = std::fs::remove_dir_all(&dir); + out.into_iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap().get("result").cloned().unwrap_or(Json::Null) +} + +#[test] +fn completion_lists_what_a_c_header_has() { + let r = with_header("import \"geo.h\"\n\nfunc main() {\n geo.\n}\n", "textDocument/completion", 3, 8).to_string(); + assert!(r.contains("\"dist\"") && r.contains("\"Point\"") && r.contains("\"ORIGIN_NAME\"") && r.contains("dist(a: geo.Point, b: geo.Point): float"), "{r}"); +} + +#[test] +fn hover_and_hints_for_c_functions() { + let src = "import \"geo.h\" { clamp }\n\nfunc main() {\n print(clamp(5, 0, \n}\n"; + let h = with_header(src, "textDocument/hover", 3, 11); + let v = h.path(&["contents", "value"]).and_then(Json::as_str).unwrap_or("").to_string(); + assert!(v.contains("func clamp(v: i32, lo: i32, hi: i32): i32") && v.contains("geo.h"), "{v}"); + let s = with_header(src, "textDocument/signatureHelp", 3, 22); + assert_eq!(s.get("activeParameter").and_then(Json::as_i64), Some(2), "{s:?}"); +} diff --git a/tests/run.rs b/tests/run.rs index 1413c26..5203bee 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -2213,6 +2213,17 @@ fn docs_examples_run() { } // the output block, if one follows right away let expected = rest.strip_prefix("\n```text\n").and_then(|r| r.find("```").map(|e| &r[..e])); + if code.contains("import \"") { + // C libraries: the header may be one written next to the program in the text, + // or a library that isn't installed here + match driver::compile_to_c(code) { + Err(d) if d.msg.contains("cannot find the C header") => continue, + Err(d) => panic!("{} failed:\n{code}\n{}", path.display(), d.msg), + // without printed output it is a program to look at (a window), not to run + Ok(_) if expected.is_none() => continue, + Ok(_) => {} + } + } let o = run_source(code); assert_eq!(o.code, 0, "{} failed:\n{code}\n{}", path.display(), o.stderr); if let Some(want) = expected {