Reduct  v4.1.3-1-gd06c383
A functional and immutable language.
Loading...
Searching...
No Matches
module.h
Go to the documentation of this file.
1#ifndef REDUCT_MODULE_H
2#define REDUCT_MODULE_H 1
3
4#include <reduct/defs.h>
5#include <reduct/sync.h>
6
7#include <stdbool.h>
8#include <stddef.h>
9
10/**
11 * @file module.h
12 * @brief Module system
13 * @defgroup module
14 *
15 * The modules keep track of which file an item originates from for error reporting, or tracks resources such as open
16 * shared libraries.
17 *
18 * @{
19 */
20
21/**
22 * @brief Module initialization function type.
23 */
24typedef reduct_handle_t (*reduct_module_init_fn)(struct reduct* reduct);
25
26#define REDUCT_MODULE_ENTRY "reduct_module_init" ///< The name of the entry symbol for a Reduct module.
27
28#define REDUCT_MODULE_PATHS_INITIAL 4 ///< Initial size of the module path array.
29#define REDUCT_MODULE_PATHS_GROWTH 2 ///< Growth factor of the module path array.
30
31/**
32 * @brief Identifies a `reduct_module_t` within a Reduct structure.
33 *
34 * Avoids the need to store a `reduct_module_t*` within a `reduct_item_t` saving space.
35 */
36typedef uint16_t reduct_module_id_t;
37
38/**
39 * @brief Invalid handle value.
40 */
41#define REDUCT_MODULE_ID_NONE ((reduct_module_id_t) - 1)
42
43/**
44 * @brief Module flags.
45 * @struct reduct_module_flags_t
46 */
47typedef enum reduct_module_flags
48{
50 REDUCT_MODULE_FLAG_BUFFER_OWNED = 1 << 0, ///< The buffer is owned by the module structure and should be freed.
51 REDUCT_MODULE_FLAG_IS_LIBRARY = 1 << 1 ///< The module is a loaded shared library.
53
54/**
55 * @brief Module structure.
56 * @struct reduct_module_t
57 *
58 * Represents either a source module or a loaded shared library.
59 * Source modules have a buffer and optional AST, while library modules
60 * hold a reference to the loaded shared library handle.
61 */
62typedef struct reduct_module
63{
64 struct reduct_module* prev;
65 const char* buffer;
66 const char* end;
69 char* path;
70 reduct_lib_t lib; ///< Loaded shared library handle, must have `REDUCT_MODULE_FLAG_IS_LIBRARY` set.
72
73/**
74 * @brief Global module state structure.
75 * @struct reduct_module_global_t
76 */
87
88/**
89 * @brief Initialize the global module state.
90 *
91 * @param global Pointer to the global module state structure.
92 */
94
95/**
96 * @brief Deinitialize the global module state.
97 *
98 * @param global Pointer to the global module state structure.
99 */
101
102/**
103 * @brief Create a new module to track a buffer.
104 *
105 * @param reduct Pointer to the Reduct structure.
106 * @param buffer The input buffer containing source code.
107 * @param length The length of the input buffer.
108 * @param path The path to the input file (for error reporting and relative imports).
109 * @param flags Input flags (e.g., REDUCT_MODULE_FLAG_BUFFER_OWNED).
110 * @return A pointer to the newly created module structure.
111 */
112REDUCT_API reduct_module_t* reduct_module_new(struct reduct* reduct, const char* buffer, size_t length,
113 const char* path, reduct_module_flags_t flags);
114
115/**
116 * @brief Lookup a module structure by its ID.
117 *
118 * @param reduct Pointer to the Reduct structure.
119 * @param id The ID of the module structure.
120 * @return A pointer to the module structure, or NULL if not found.
121 */
123
124/**
125 * @brief Add a path to search when importing modules.
126 *
127 * @param reduct Pointer to the Reduct structure.
128 * @param path The directory path, will be copied.
129 */
130REDUCT_API void reduct_module_add_path(struct reduct* reduct, const char* path);
131
132/**
133 * @brief Resolve a path relative to the current execution frame or import paths.
134 *
135 * @param reduct Pointer to the Reduct structure.
136 * @param path The path string to resolve.
137 * @param pathLen The length of the path string.
138 * @param outPath Pointer to the buffer where the resolved path will be stored.
139 * @param maxLen The maximum length of the output buffer.
140 * @param checkExistence Whether to check if the file exists when resolving relative paths.
141 */
142REDUCT_API void reduct_module_resolve_path(struct reduct* reduct, const char* path, size_t pathLen, char* outPath,
143 size_t maxLen, bool checkExistence);
144
145/**
146 * @brief Import a module from a given path.
147 *
148 * @param reduct Pointer to the Reduct structure.
149 * @param path The path to the module file.
150 * @param compiler Handle to an atom storing the name of the compiler to use.
151 * @param compilerArgs Handle to an atom storing the compiler arguments.
152 * @return A handle to the root of the parsed module AST.
153 */
155 reduct_handle_t compilerArgs);
156
157/** @} */
158
159#endif
void * reduct_lib_t
Definition defs.h:38
#define REDUCT_API
Definition defs.h:24
reduct_module_flags_t
Definition module.h:48
REDUCT_API void reduct_module_global_deinit(reduct_module_global_t *global)
Deinitialize the global module state.
REDUCT_API reduct_module_t * reduct_module_lookup(struct reduct *reduct, reduct_module_id_t id)
Lookup a module structure by its ID.
REDUCT_API void reduct_module_resolve_path(struct reduct *reduct, const char *path, size_t pathLen, char *outPath, size_t maxLen, bool checkExistence)
Resolve a path relative to the current execution frame or import paths.
uint16_t reduct_module_id_t
Identifies a reduct_module_t within a Reduct structure.
Definition module.h:36
REDUCT_API reduct_module_t * reduct_module_new(struct reduct *reduct, const char *buffer, size_t length, const char *path, reduct_module_flags_t flags)
Create a new module to track a buffer.
REDUCT_API reduct_handle_t reduct_module_import(struct reduct *reduct, reduct_handle_t path, reduct_handle_t compiler, reduct_handle_t compilerArgs)
Import a module from a given path.
REDUCT_API void reduct_module_add_path(struct reduct *reduct, const char *path)
Add a path to search when importing modules.
REDUCT_API void reduct_module_global_init(reduct_module_global_t *global)
Initialize the global module state.
reduct_handle_t(* reduct_module_init_fn)(struct reduct *reduct)
Module initialization function type.
Definition module.h:24
@ REDUCT_MODULE_FLAG_BUFFER_OWNED
The buffer is owned by the module structure and should be freed.
Definition module.h:50
@ REDUCT_MODULE_FLAG_NONE
Definition module.h:49
@ REDUCT_MODULE_FLAG_IS_LIBRARY
The module is a loaded shared library.
Definition module.h:51
Handle type.
Definition defs.h:121
Global module state structure.
Definition module.h:78
reduct_module_id_t moduleNextId
Definition module.h:81
reduct_module_t * moduleHead
Definition module.h:80
reduct_rwmutex_t pathsMutex
Definition module.h:82
reduct_rwmutex_t moduleMutex
Definition module.h:79
Module structure.
Definition module.h:63
char * path
Definition module.h:69
struct reduct_module * prev
Definition module.h:64
const char * buffer
Definition module.h:65
reduct_module_flags_t flags
Definition module.h:68
const char * end
Definition module.h:66
reduct_lib_t lib
Loaded shared library handle, must have REDUCT_MODULE_FLAG_IS_LIBRARY set.
Definition module.h:70
reduct_module_id_t id
Definition module.h:67
Read-Write Mutex structure.
Definition sync.h:23
Syncronization primitives.