diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-24 15:56:38 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-24 15:56:38 -0400 |
| commit | e920d306fb51c16fd473033b64aa5ba5c3e07409 (patch) | |
| tree | ab0a7cb47ad050f278981be1e0a4edc19b8daaee /src/compile/pointers.c | |
| parent | c62c3200c7078f58b218def14c54bbfd26677069 (diff) | |
Split into more files: promotion, sets, tables, pointers, functions.
Diffstat (limited to 'src/compile/pointers.c')
| -rw-r--r-- | src/compile/pointers.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/compile/pointers.c b/src/compile/pointers.c index cb11844d..ee67b18d 100644 --- a/src/compile/pointers.c +++ b/src/compile/pointers.c @@ -10,7 +10,9 @@ #include "../environment.h" #include "../stdlib/text.h" #include "../typecheck.h" +#include "promotion.h" +public Text_t compile_to_pointer_depth(env_t *env, ast_t *ast, int64_t target_depth, bool needs_incref) { Text_t val = compile(env, ast); type_t *t = get_type(env, ast); @@ -46,3 +48,22 @@ Text_t compile_to_pointer_depth(env_t *env, ast_t *ast, int64_t target_depth, bo return val; } + +public +Text_t compile_typed_allocation(env_t *env, ast_t *ast, type_t *pointer_type) { + // TODO: for constructors, do new(T, ...) instead of heap((T){...}) + type_t *pointed = Match(pointer_type, PointerType)->pointed; + switch (ast->tag) { + case HeapAllocate: { + return Texts("heap(", compile_to_type(env, Match(ast, HeapAllocate)->value, pointed), ")"); + } + case StackReference: { + ast_t *subject = Match(ast, StackReference)->value; + if (can_be_mutated(env, subject) && type_eq(pointed, get_type(env, subject))) + return Texts("(&", compile_lvalue(env, subject), ")"); + else return Texts("stack(", compile_to_type(env, subject, pointed), ")"); + } + default: code_err(ast, "Not an allocation!"); + } + return EMPTY_TEXT; +} |
