Cthulhu  0.2.10
Cthulhu compiler collection
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
driver.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #include "ctu/driver.h"
6 #include "ctu/ast.h"
7 
8 #include "ctu/sema/sema.h"
9 #include "ctu/sema/decl.h"
10 
11 #include "cthulhu/tree/tree.h"
12 #include "cthulhu/tree/query.h"
13 
14 #include "std/vector.h"
15 #include "std/map.h"
16 #include "std/str.h"
17 
18 #include "base/panic.h"
19 
20 #include "core/macros.h"
21 
25 
26 void ctu_init(language_runtime_t *runtime, tree_t *root)
27 {
28  ctu_rt_mod(runtime, root);
29 }
30 
32 {
33  CT_UNUSED(runtime);
34 
35  ctu_t *ast = unit_get_ast(unit);
36 
37  const vector_t *decls = ast->decls;
38  size_t len = vector_len(decls);
39  tree_t *mod = unit->tree;
40 
41  for (size_t i = 0; i < len; i++)
42  {
43  ctu_t *decl = vector_get(decls, i);
44 
45  ctu_forward_t fwd = ctu_forward_decl(mod, decl);
46  ctu_add_decl(mod, fwd.tag, decl->name, fwd.decl);
47  }
48 
49  unit_update(unit, ast, mod);
50 }
51 
52 static tree_t *get_import(tree_t *sema, const char *name)
53 {
54  map_t *imports = tree_module_tag(sema, eCtuTagImports);
55  tree_t *it = map_get(imports, name);
56  if (it != NULL)
57  return it;
58 
59  map_t *mods = tree_module_tag(sema, eCtuTagModules);
60  return map_get(mods, name);
61 }
62 
63 static void import_module(language_runtime_t *runtime, tree_t *sema, ctu_t *include)
64 {
65  CTASSERT(include->kind == eCtuImport);
66  arena_t *arena = runtime->arena;
67  unit_id_t id = build_unit_id(include->import_path, arena);
68  compile_unit_t *ctx = lang_get_unit(runtime, id);
69 
70  if (ctx == NULL)
71  {
72  msg_notify(sema->reports, &kEvent_ImportNotFound, include->node, "import `%s` not found", str_join("::", include->import_path, arena));
73  return;
74  }
75 
76  if (ctx->tree == sema)
77  {
78  msg_notify(sema->reports, &kEvent_CirclularImport, include->node, "module cannot import itself");
79  return;
80  }
81 
82  // only search the current module for the import
83  // if we did this recursively we would reach the root module
84  // which always has the imported module
85  tree_t *old = get_import(sema, include->name);
86  if (old != NULL)
87  {
88  event_builder_t evt = evt_symbol_shadowed(sema->reports, include->name, tree_get_node(old), include->node);
89  msg_note(evt, "consider using import aliases; eg. `import %s as my_%s`",
90  str_join("::", include->import_path, arena),
91  include->name
92  );
93  }
94  else
95  {
96  ctu_add_decl(sema, eCtuTagImports, include->name, ctx->tree);
97  }
98 }
99 
101 {
102  ctu_t *ast = unit_get_ast(unit);
103 
104  size_t len = vector_len(ast->imports);
105  for (size_t i = 0; i < len; i++)
106  {
107  ctu_t *it = vector_get(ast->imports, i);
108  import_module(runtime, unit->tree, it);
109  }
110 }
CT_PUREFN CT_TREE_API const node_t * tree_get_node(const tree_t *tree)
Definition: context.c:94
@ eCtuImport
Definition: ast.h:82
ctu_forward_t ctu_forward_decl(tree_t *sema, ctu_t *decl)
Definition: decl.c:345
void ctu_rt_mod(language_runtime_t *runtime, tree_t *root)
Definition: sema.c:281
void ctu_add_decl(tree_t *sema, ctu_tag_t tag, const char *name, tree_t *decl)
Definition: sema.c:129
void ctu_forward_decls(language_runtime_t *runtime, compile_unit_t *unit)
Definition: driver.c:31
void ctu_process_imports(language_runtime_t *runtime, compile_unit_t *unit)
Definition: driver.c:100
void ctu_init(language_runtime_t *runtime, tree_t *root)
Definition: driver.c:26
CT_BROKER_API void * unit_get_ast(compile_unit_t *unit)
Definition: broker.c:511
CT_BROKER_API void unit_update(compile_unit_t *unit, void *ast, tree_t *tree)
Definition: broker.c:519
CT_BROKER_API compile_unit_t * lang_get_unit(language_runtime_t *runtime, unit_id_t id)
Definition: broker.c:494
CT_BROKER_API text_view_t build_unit_id(const vector_t *parts, arena_t *arena)
Definition: broker.c:532
CT_EVENTS_API event_builder_t evt_symbol_shadowed(logger_t *logger, const char *name, const node_t *prev, const node_t *next)
signal that a declaration would shadow a previous declaration
Definition: events.c:47
CT_NODISCARD CT_PUREFN CT_STD_API void * map_get(const map_t *map, const void *key)
get a value from a map
Definition: map.c:324
#define CT_UNUSED(x)
mark a variable as unused
Definition: macros.h:46
CT_NOTIFY_API event_builder_t msg_notify(INOUT_NOTNULL logger_t *logs, const diagnostic_t *diagnostic, const node_t *node, STA_FORMAT_STRING const char *fmt,...)
notify the logger of a new message
CT_NOTIFY_API void msg_note(event_builder_t builder, STA_FORMAT_STRING const char *fmt,...)
add a note to an existing message
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
CT_NODISCARD CT_STD_API char * str_join(const char *sep, const vector_t *parts, arena_t *arena)
join strings
Definition: str.c:274
CT_TREE_API map_t * tree_module_tag(const tree_t *self, size_t tag)
Definition: sema.c:125
CT_NODISCARD CT_PUREFN CT_STD_API void * vector_get(const vector_t *vector, size_t index)
get a value from a vector
Definition: vector.c:134
CT_NODISCARD CT_PUREFN CT_STD_API size_t vector_len(const vector_t *vector)
get the length of a vector
Definition: vector.c:152
an allocator object
Definition: arena.h:86
tree_t * tree
the tree for this unit is NULL if the unit has not been forward declared yet
Definition: broker.h:289
ctu_tag_t tag
Definition: decl.h:8
tree_t * decl
Definition: decl.h:9
Definition: ast.h:86
ctu_kind_t kind
Definition: ast.h:87
const vector_t * import_path
Definition: ast.h:98
const vector_t * imports
Definition: ast.h:245
const vector_t * decls
Definition: ast.h:246
const node_t * node
Definition: ast.h:88
char * name
Definition: ast.h:92
an event builder handles adding additional information to an event
Definition: notify.h:68
arena_t * arena
default memory arena
Definition: broker.h:266
an unordered hash map
Definition: map.h:38
a non-owning view of text
Definition: text.h:24
Definition: tree.h:67
logger_t * reports
Definition: tree.h:227
a generic vector of pointers
Definition: vector.c:16