Cthulhu  0.2.10
Cthulhu compiler collection
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tree.c
Go to the documentation of this file.
2 
3 #include "core/macros.h"
5 
6 #include "cthulhu/tree/query.h"
7 #include "fs/fs.h"
8 #include "io/io.h"
9 #include "os/os.h"
10 #include "base/panic.h"
11 #include "cthulhu/events/events.h"
12 
13 #include "std/map.h"
14 #include "std/vector.h"
15 
16 typedef struct cfamily_emit_t
17 {
19 
20  map_t *names; // map of tree_t* -> mangled name
21 
25 
26 static void push_namespace(cfamily_emit_t *emit, const tree_t *tree)
27 {
28  vector_push(&emit->stack, (void*)tree_get_name(tree));
29 }
30 
31 static void pop_namespace(cfamily_emit_t *emit)
32 {
33  vector_drop(emit->stack);
34 }
35 
36 static void forward_decl(cfamily_emit_t *emit, const tree_t *tree)
37 {
38  switch (tree_get_kind(tree))
39  {
40  case eTreeDeclModule:
41  {
42  push_namespace(emit, tree);
43 
44  // map_t *vars = tree_module_tag(tree, eSemaTypes);
45  // map_iter_t iter = map_iter(vars);
46  // while (map_has_next(&iter))
47  // {
48  // (void)map_next(&iter);
49  // }
50 
51  pop_namespace(emit);
52  break;
53  }
54 
55  default:
56  CT_NEVER("unsupported tree kind %s", tree_kind_to_string(tree_get_kind(tree)));
57  }
58 }
59 
60 static void cfamily_pair(target_runtime_t *runtime, const tree_t *tree, fs_t *dst)
61 {
62  CT_UNUSED(tree);
63 
64  os_error_t err = eOsSuccess;
65  io_t *src = fs_open(dst, "tree.c", eOsAccessWrite | eOsAccessTruncate);
66  io_t *hdr = fs_open(dst, "tree.h", eOsAccessWrite | eOsAccessTruncate);
67 
68  if ((err = io_error(src)) != eOsSuccess)
69  {
70  evt_os_error(runtime->logger, &kEvent_FailedToCreateOutputFile, err, "failed to create tree.c");
71  goto cleanup;
72  }
73 
74  if ((err = io_error(hdr)) != eOsSuccess)
75  {
76  evt_os_error(runtime->logger, &kEvent_FailedToCreateOutputFile, err, "failed to create tree.h");
77  goto cleanup;
78  }
79 
80  io_printf(src, "/* This file was generated, do not edit */\n\n");
81  io_printf(src, "#include \"tree.h\"\n\n");
82 
83  io_printf(hdr, "/* This file was generated, do not edit */\n\n");
84  io_printf(hdr, "#pragma once\n\n");
85 
86  cfamily_emit_t emit = {
87  .stack = vector_new(4, runtime->arena),
88  .names = map_optimal(1024, kTypeInfoPtr, runtime->arena), // TODO: calculate optimal size
89  .src = src,
90  .hdr = hdr,
91  };
92 
93  forward_decl(&emit, tree);
94 
95 cleanup:
96  io_close(src);
97  io_close(hdr);
98 }
99 
100 void cfamily_tree(target_runtime_t *runtime, const tree_t *tree, target_emit_t *emit)
101 {
102  switch (emit->layout)
103  {
104  case eFileLayoutPair:
105  cfamily_pair(runtime, tree, emit->fs);
106  break;
107 
108  default:
109  CT_NEVER("unsupported file layout %s", file_layout_name(emit->layout));
110  }
111 }
CT_PUREFN CT_TREE_API tree_kind_t tree_get_kind(const tree_t *tree)
Definition: context.c:143
CT_PUREFN CT_TREE_API const char * tree_get_name(const tree_t *tree)
Definition: context.c:102
CT_BEGIN_API CT_TREE_API const char * tree_kind_to_string(tree_kind_t kind)
Definition: query.c:23
CT_CONSTFN CT_CONSTFN CT_BROKER_API const char * file_layout_name(file_layout_t layout)
Definition: broker.c:610
CT_EVENTS_API event_builder_t evt_os_error(logger_t *logger, const diagnostic_t *diagnostic, os_error_t error, const char *msg)
signal that an os error has occurred
Definition: events.c:59
CT_NODISCARD CT_FS_API io_t * fs_open(fs_t *fs, const char *path, os_access_t flags)
open a file at a given location in the filesystem
Definition: fs.c:254
CT_NODISCARD CT_STD_API map_t * map_optimal(size_t size, hash_info_t info, arena_t *arena)
create a new map with an optimal size
Definition: optimal.c:85
CT_NODISCARD CT_IO_API os_error_t io_error(const io_t *io)
get the last error from the io object
Definition: io.c:144
CT_IO_API os_error_t io_close(INOUT_NOTNULL io_t *io)
destroy an IO object
CT_IO_API size_t io_printf(io_t *io, STA_FORMAT_STRING const char *fmt,...)
printf to an io object
#define CT_UNUSED(x)
mark a variable as unused
Definition: macros.h:46
#define CT_NEVER(...)
assert that a code path is never reached
Definition: panic.h:136
CT_STD_API const hash_info_t kTypeInfoPtr
type information for a generic pointer this operates on the pointer itself and not the data it points...
Definition: typeinfo.c:41
CT_NODISCARD CT_STD_API vector_t * vector_new(size_t size, arena_t *arena)
create a new vector with an initial capacity
Definition: vector.c:63
CT_STD_API void vector_push(vector_t **vector, void *value)
push a value onto the end of a vector
Definition: vector.c:108
CT_STD_API void vector_drop(vector_t *vector)
pop a value from the end of a vector
Definition: vector.c:117
@ eOsSuccess
Definition: posix.h:24
vector_t * stack
Definition: tree.c:18
io_t * hdr
Definition: tree.c:23
map_t * names
Definition: tree.c:20
io_t * src
Definition: tree.c:22
Definition: common.h:72
io object implementation
Definition: impl.h:122
an unordered hash map
Definition: map.h:38
fs_t * fs
Definition: broker.h:311
file_layout_t layout
Definition: broker.h:309
arena_t * arena
Definition: broker.h:302
logger_t * logger
Definition: broker.h:304
Definition: tree.h:67
a generic vector of pointers
Definition: vector.c:16
void cfamily_tree(target_runtime_t *runtime, const tree_t *tree, target_emit_t *emit)
Definition: tree.c:100