Cthulhu  0.2.10
Cthulhu compiler collection
driver.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #include "oberon/driver.h"
4 #include "oberon/ast.h"
5 
6 #include "oberon/sema/decl.h"
7 
10 
11 #include "cthulhu/tree/query.h"
12 
13 #include "std/vector.h"
14 
15 #include "base/panic.h"
16 #include "base/util.h"
17 
18 #include "core/macros.h"
19 
21 {
22  CT_UNUSED(runtime);
23 
24  obr_t *ast = unit_get_ast(unit);
25  size_t decl_count = vector_len(ast->decls);
26 
27  tree_t *sema = unit->tree;
28 
29  for (size_t i = 0; i < decl_count; i++)
30  {
31  obr_t *decl = vector_get(ast->decls, i);
32  obr_forward_t fwd = obr_forward_decl(sema, decl);
33  tree_t *it = fwd.decl;
34 
35  obr_add_decl(sema, fwd.tag, it->name, it);
36  }
37 
38  tree_t *init = obr_add_init(sema, ast);
39  if (init != NULL)
40  {
41  // TODO: pick a better name
42  obr_add_decl(sema, eObrTagProcs, init->name, init);
43  }
44 
45  unit_update(unit, ast, sema);
46 }
47 
48 static void import_module(language_runtime_t *runtime, tree_t *sema, obr_t *include)
49 {
50  CTASSERT(include->kind == eObrImport);
51  unit_id_t id = text_view_from(include->symbol);
52  compile_unit_t *ctx = lang_get_unit(runtime, id);
53 
54  if (ctx == NULL)
55  {
56  msg_notify(sema->reports, &kEvent_ImportNotFound, include->node, "failed to find context for module '%s'", include->symbol);
57  return;
58  }
59 
60  if (ctx->tree == sema)
61  {
62  msg_notify(sema->reports, &kEvent_CirclularImport, include->node, "module cannot import itself");
63  return;
64  }
65 
66  // only search current module for the import
67 
68  tree_t *old = obr_get_namespace(sema, include->name);
69  if (old != NULL)
70  {
71  event_builder_t evt = evt_symbol_shadowed(sema->reports, include->name, tree_get_node(old), include->node);
72  msg_note(evt, "consider using import aliases; eg. `IMPORT my_%s := %s;", include->name, include->symbol);
73  }
74  else
75  {
76  obr_add_decl(sema, eObrTagImports, include->name, ctx->tree);
77  }
78 }
79 
81 {
82  obr_t *root = unit_get_ast(unit);
83 
84  size_t len = vector_len(root->imports);
85  for (size_t i = 0; i < len; i++)
86  {
87  obr_t *import = vector_get(root->imports, i);
88  import_module(runtime, unit->tree, import);
89  }
90 }
CT_PUREFN CT_TREE_API const node_t * tree_get_node(const tree_t *tree)
Definition: context.c:94
CT_PUREFN CT_BASE_API text_view_t text_view_from(const char *text)
create a new non-owning text array this is a shortcut for
Definition: util.c:191
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_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
#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_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
@ eObrImport
Definition: ast.h:89
obr_forward_t obr_forward_decl(tree_t *sema, obr_t *decl)
Definition: decl.c:273
tree_t * obr_add_init(tree_t *sema, obr_t *mod)
Definition: decl.c:304
void obr_add_decl(tree_t *sema, obr_tag_t tag, const char *name, tree_t *decl)
add decls
Definition: sema.c:43
tree_t * obr_get_namespace(tree_t *sema, const char *name)
Definition: sema.c:36
void obr_forward_decls(language_runtime_t *runtime, compile_unit_t *unit)
Definition: driver.c:20
void obr_process_imports(language_runtime_t *runtime, compile_unit_t *unit)
Definition: driver.c:80
tree_t * tree
the tree for this unit is NULL if the unit has not been forward declared yet
Definition: broker.h:289
an event builder handles adding additional information to an event
Definition: notify.h:68
tree_t * decl
Definition: decl.h:11
obr_tag_t tag
Definition: decl.h:10
Definition: ast.h:93
obr_kind_t kind
Definition: ast.h:94
char * name
Definition: ast.h:184
vector_t * decls
Definition: ast.h:191
const node_t * node
Definition: ast.h:95
char * symbol
Definition: ast.h:215
const vector_t * imports
Definition: ast.h:190
a non-owning view of text
Definition: text.h:24
Definition: tree.h:67
logger_t * reports
Definition: tree.h:227
const char * name
the name of the declaration
Definition: tree.h:163