Cthulhu  0.2.10
Cthulhu compiler collection
load_static.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "common.h" // IWYU pragma: export
4 
5 #include <enum_modules.h>
6 
7 #include "base/util.h"
8 #include "base/panic.h"
9 
10 // TODO: find a way to nicely dedup this logic
11 
13 loaded_module_t load_static_module(loader_t *loader, module_type_t mask, const char *name)
14 {
15  CTASSERT(loader != NULL);
16  CTASSERT(name != NULL);
17 
18  static_modules_t mods = get_static_modules();
19 
20  loaded_module_t mod = { .type = eModNone };
21 
22 #if CT_LANG_COUNT > 0
23  if (mask & eModLanguage)
24  {
25  for (size_t i = 0; i < CT_LANG_COUNT; i++)
26  {
27  const language_t *lang = mods.langs[i];
28  const module_info_t *info = &lang->info;
29 
30  if (!str_equal(info->id, name))
31  continue;
32 
33  if (mod.lang != NULL)
34  {
35  const language_t *old = mod.lang;
36  const module_info_t *prev = &old->info;
37  CT_NEVER("multiple static languages with the same id: %s (prev: %s, new: %s)", info->id, prev->name, info->name);
38  }
39 
40  mod.type |= eModLanguage;
41  mod.lang = lang;
42  }
43  }
44 #endif
45 
46 #if CT_PLUGIN_COUNT > 0
47  if (mask & eModPlugin)
48  {
49  for (size_t i = 0; i < CT_PLUGIN_COUNT; i++)
50  {
51  const plugin_t *plugin = mods.plugins[i];
52  const module_info_t *info = &plugin->info;
53 
54  if (!str_equal(info->id, name))
55  continue;
56 
57  if (mod.plugin != NULL)
58  {
59  const plugin_t *old = mod.plugin;
60  const module_info_t *prev = &old->info;
61  CT_NEVER("multiple static plugins with the same id: %s (prev: %s, new: %s)", info->id, prev->name, info->name);
62  }
63 
64  mod.type |= eModPlugin;
65  mod.plugin = plugin;
66  }
67  }
68 #endif
69 
70 #if CT_TARGET_COUNT > 0
71  if (mask & eModTarget)
72  {
73  for (size_t i = 0; i < CT_TARGET_COUNT; i++)
74  {
75  const target_t *target = mods.targets[i];
76  const module_info_t *info = &target->info;
77  if (!str_equal(info->id, name))
78  continue;
79 
80  if (mod.target != NULL)
81  {
82  const target_t *old = mod.target;
83  const module_info_t *prev = &old->info;
84  CT_NEVER("multiple static targets with the same id: %s (prev: %s, new: %s)", info->id, prev->name, info->name);
85  }
86 
87  mod.type |= eModTarget;
88  mod.target = target;
89  }
90  }
91 #endif
92 
93  return mod;
94 }
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
CT_NODISCARD CT_PUREFN CT_BASE_API bool str_equal(const char *lhs, const char *rhs)
compare strings equality
Definition: util.c:76
#define CT_NEVER(...)
assert that a code path is never reached
Definition: panic.h:136
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
STA_DECL loaded_module_t load_static_module(loader_t *loader, module_type_t mask, const char *name)
Definition: load_static.c:13
module_type_t
Definition: loader.h:32
a language driver support capabilities
Definition: broker.h:143
module_info_t info
common information about the language
Definition: broker.h:145
const plugin_t * plugin
Definition: loader.h:52
module_type_t type
Definition: loader.h:49
const target_t * target
Definition: loader.h:53
const language_t * lang
Definition: loader.h:51
Definition: common.h:8
common information about anything the broker supports
Definition: broker.h:99
const char * id
unique id for the module
Definition: broker.h:101
const char * name
the human readable name for the module
Definition: broker.h:104
plugin support capabilities
Definition: broker.h:202
module_info_t info
information about the plugin
Definition: broker.h:204
a codegen target backend
Definition: broker.h:232
module_info_t info
information about the target
Definition: broker.h:234