Cthulhu  0.2.10
Cthulhu compiler collection
main.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #include "ctu/driver.h"
4 
6 
7 #include "ctu/sema/sema.h"
8 #include "interop/compile.h"
9 
10 #include "std/vector.h"
11 #include "std/str.h"
12 
13 #include "arena/arena.h"
14 
15 #include "driver/driver.h"
16 
17 #include "ctu_bison.h" // IWYU pragma: keep
18 #include "ctu_flex.h" // IWYU pragma: keep
19 
20 CT_CALLBACKS(kCallbacks, ctu);
21 
22 static vector_t *mod_basename(const char *fp, arena_t *arena)
23 {
24  return vector_init(str_basename(fp, arena), arena);
25 }
26 
27 static const vector_t *find_mod_path(ctu_t *ast, const char *fp, arena_t *arena)
28 {
29  if (ast == NULL) { return mod_basename(fp, arena); }
30 
31  return vector_len(ast->modspec) > 0
32  ? ast->modspec
33  : mod_basename(fp, arena);
34 }
35 
36 static void ctu_postparse(language_runtime_t *runtime, scan_t *scan, void *tree)
37 {
38  ctu_t *ast = tree;
39  CTASSERT(ast->kind == eCtuModule);
40 
41  arena_t *arena = runtime->arena;
42  const vector_t *path = find_mod_path(ast, scan_path(scan), arena);
43 
44  size_t len = vector_len(ast->decls);
45  size_t sizes[eCtuTagTotal] = {
46  [eCtuTagValues] = len,
47  [eCtuTagTypes] = len,
48  [eCtuTagFunctions] = len,
49  [eCtuTagModules] = len,
50  [eCtuTagImports] = vector_len(ast->imports),
51  [eCtuTagAttribs] = len,
52  [eCtuTagSuffixes] = len,
53  };
54 
55  lang_add_unit(runtime, build_unit_id(path, arena), ast->node, ast, sizes, eCtuTagTotal);
56 }
57 
58 #define NEW_EVENT(name, ...) const diagnostic_t kEvent_##name = __VA_ARGS__;
59 #include "ctu/ctu.inc"
60 
61 static const diagnostic_t * const kDiagnosticTable[] = {
62 #define NEW_EVENT(name, ...) &kEvent_##name,
63 #include "ctu/ctu.inc"
64 };
65 
66 static const char *const kLangNames[] = CT_LANG_EXTS("ct", "ctu", "cthulhu");
67 
68 static const size_t kDeclSizes[eCtuTagTotal] = {
69  [eCtuTagValues] = 1,
70  [eCtuTagTypes] = 1,
71  [eCtuTagFunctions] = 1,
72  [eCtuTagModules] = 1,
73  [eCtuTagImports] = 1,
74  [eCtuTagAttribs] = 1,
75  [eCtuTagSuffixes] = 1,
76 };
77 
78 static const char *const kDeclNames[eCtuTagTotal] = {
79 #define DECL_TAG(id, val, name) [id] = (name),
80 #include "ctu/ctu.inc"
81 };
82 
83 CT_DRIVER_API const language_t kCtuModule = {
84  .info = {
85  .id = "lang/cthulhu",
86  .name = "Cthulhu",
87  .version = {
88  .license = "LGPLv3",
89  .desc = "Cthulhu language driver",
90  .author = "Elliot Haisley",
91  .version = CT_NEW_VERSION(0, 4, 0),
92  },
93 
94  .diagnostics = {
95  .diagnostics = kDiagnosticTable,
96  .count = sizeof(kDiagnosticTable) / sizeof(diagnostic_t*),
97  },
98  },
99 
100  .builtin = {
101  .name = CT_TEXT_VIEW("ctu\0lang"),
102  .decls = kDeclSizes,
103  .names = kDeclNames,
104  .length = eCtuTagTotal,
105  },
106 
107  .exts = kLangNames,
108 
109  .ast_size = sizeof(ctu_t),
110 
111  .fn_create = ctu_init,
112 
113  .fn_postparse = ctu_postparse,
114  .scanner = &kCallbacks,
115 
116  .fn_passes = {
117  [ePassForwardDecls] = ctu_forward_decls,
118  [ePassImportModules] = ctu_process_imports
119  }
120 };
121 
#define CT_TEXT_VIEW(str)
create a text view from a string literal
Definition: text.h:39
CT_NODISCARD CT_PUREFN CT_SCAN_API const char * scan_path(const scan_t *scan)
get the path of a scanner
Definition: scan.c:56
@ eCtuModule
Definition: ast.h:83
@ eCtuTagTotal
Definition: sema.h:17
CT_BROKER_API void lang_add_unit(language_runtime_t *runtime, unit_id_t id, const node_t *node, void *ast, const size_t *sizes, size_t count)
all runtime apis
Definition: broker.c:462
#define CT_LANG_EXTS(...)
Definition: broker.h:49
CT_BROKER_API text_view_t build_unit_id(const vector_t *parts, arena_t *arena)
Definition: broker.c:532
#define CT_NEW_VERSION(major, minor, patch)
creates a new ctu_version_t from major, minor and patch
Definition: version_def.h:20
#define CT_LANG_EXPORT(mod)
declares the entry point for a language driver module
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
CT_NODISCARD CT_STD_API char * str_basename(const char *path, arena_t *arena)
get the filename from a path
Definition: str.c:203
CT_NODISCARD CT_STD_API vector_t * vector_init(void *value, arena_t *arena)
create a new vector with a single initial value
Definition: vector.c:79
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
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_CALLBACKS(kCallbacks, ctu)
CT_DRIVER_API const language_t kCtuModule
Definition: main.c:83
Definition: io.hpp:7
an allocator object
Definition: arena.h:86
Definition: ast.h:86
ctu_kind_t kind
Definition: ast.h:87
const vector_t * imports
Definition: ast.h:245
const vector_t * decls
Definition: ast.h:246
const node_t * node
Definition: ast.h:88
const vector_t * modspec
Definition: ast.h:244
a diagnostic
Definition: diagnostic.h:27
arena_t * arena
default memory arena
Definition: broker.h:266
a language driver support capabilities
Definition: broker.h:143
module_info_t info
common information about the language
Definition: broker.h:145
const char * id
unique id for the module
Definition: broker.h:101
a source file scanner
Definition: scan.h:24
a generic vector of pointers
Definition: vector.c:16