1 |
|
|
#include <stdlib.h> |
2 |
|
|
#include <string.h> |
3 |
|
|
#include <ctype.h> |
4 |
|
|
#include "gwion_util.h" |
5 |
|
|
#include "gwion_ast.h" |
6 |
|
|
#include "gwion_env.h" |
7 |
|
|
#include "vm.h" |
8 |
|
|
#include "traverse.h" |
9 |
|
|
#include "object.h" |
10 |
|
|
#include "instr.h" |
11 |
|
|
#include "emit.h" |
12 |
|
|
#include "gwion.h" |
13 |
|
|
#include "operator.h" |
14 |
|
|
#include "import.h" |
15 |
|
|
#include "gwi.h" |
16 |
|
|
#include "clean.h" |
17 |
|
|
|
18 |
|
43102 |
ANN2(1,2,3) static m_bool dl_func_init(const Gwi gwi, const restrict m_str t, |
19 |
|
|
const restrict m_str n) { |
20 |
✓✓ |
43102 |
CHECK_BB(ck_ini(gwi, ck_fdef)) |
21 |
|
43101 |
gwi->ck->name = n; |
22 |
✗✓ |
43101 |
CHECK_BB(check_typename_def(gwi, gwi->ck)) |
23 |
✓✓ |
43101 |
CHECK_OB((gwi->ck->td = str2decl(gwi, t))) |
24 |
|
43099 |
vector_init(&gwi->ck->v); |
25 |
|
43099 |
return GW_OK; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
43097 |
ANN m_int gwi_func_ini(const Gwi gwi, const restrict m_str t, const restrict m_str n) { |
29 |
|
43097 |
return dl_func_init(gwi, t, n); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
43093 |
ANN Arg_List make_dll_arg_list(const Vector v) { |
33 |
|
43093 |
Arg_List base = (Arg_List)vector_front(v), arg_list = base; |
34 |
✓✓ |
50416 |
for(m_uint i = 1; i < vector_size(v); ++i) |
35 |
|
7323 |
arg_list = (arg_list->next = (Arg_List)vector_at(v, i)); |
36 |
|
43093 |
vector_release(v); |
37 |
|
43093 |
v->ptr = NULL; |
38 |
|
43093 |
return base; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
43093 |
ANEW ANN static Func_Base* gwi_func_base(const Gwi gwi, ImportCK *ck) { |
42 |
|
43093 |
const Arg_List arg_list = make_dll_arg_list(&gwi->ck->v); |
43 |
|
43093 |
Func_Base *base = new_func_base(gwi->gwion->mp, ck->td, ck->sym, arg_list); |
44 |
|
43093 |
ck->td = NULL; |
45 |
✓✓ |
43093 |
if(ck->tmpl) { |
46 |
|
5 |
base->tmpl = gwi_tmpl(gwi); |
47 |
|
5 |
ck->tmpl = NULL; |
48 |
|
|
} |
49 |
|
43093 |
return base; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
43089 |
ANEW ANN static Func_Def import_fdef(const Gwi gwi, ImportCK *ck) { |
53 |
|
43089 |
Func_Base* base = gwi_func_base(gwi, ck); |
54 |
|
129267 |
const Func_Def fdef = new_func_def(gwi->gwion->mp, base, |
55 |
|
86178 |
NULL, ck->flag | ae_flag_builtin, loc(gwi)); |
56 |
|
43089 |
fdef->d.dl_func_ptr = (void*)(m_uint)ck->addr; |
57 |
✓✓ |
43089 |
if(base->tmpl) |
58 |
|
4 |
SET_FLAG(fdef, template); |
59 |
|
43089 |
return fdef; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
3 |
ANN static m_bool section_fdef(const Gwi gwi, const Func_Def fdef) { |
63 |
|
3 |
Section* section = new_section_func_def(gwi->gwion->mp, fdef); |
64 |
|
3 |
const Ast body = new_ast(gwi->gwion->mp, section, NULL); |
65 |
|
3 |
gwi_body(gwi, body); |
66 |
|
3 |
return GW_OK; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
3 |
ANN static m_bool error_fdef(const Gwi gwi, const Func_Def fdef) { |
70 |
|
3 |
func_def_cleaner(gwi->gwion, fdef); |
71 |
|
3 |
return GW_ERROR; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
43089 |
ANN m_int gwi_func_valid(const Gwi gwi, ImportCK *ck) { |
75 |
|
43089 |
const Func_Def fdef = import_fdef(gwi, ck); |
76 |
✓✓✓✓
|
43089 |
if(SAFE_FLAG(gwi->gwion->env->class_def, template)) |
77 |
|
3 |
/*return*/ section_fdef(gwi, fdef); |
78 |
✓✓ |
43089 |
if(traverse_func_def(gwi->gwion->env, fdef) < 0) |
79 |
|
3 |
return error_fdef(gwi, fdef); |
80 |
|
43086 |
ck_end(gwi); |
81 |
|
43086 |
return GW_OK; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
43089 |
ANN m_int gwi_func_end(const Gwi gwi, const f_xfun addr, const ae_flag flag) { |
85 |
✗✓ |
43089 |
CHECK_BB(ck_ok(gwi, ck_fdef)) |
86 |
|
43089 |
gwi->ck->addr = addr; |
87 |
|
43089 |
gwi->ck->flag = flag; |
88 |
✓✓ |
43089 |
if(gwi_func_valid(gwi, gwi->ck) > 0) |
89 |
|
43086 |
return GW_OK; |
90 |
|
3 |
return GW_ERROR; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
24129 |
ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, const restrict m_str n) { |
94 |
✗✓ |
24129 |
CHECK_BB(ck_ok(gwi, ck_fdef)) |
95 |
✓✓ |
24129 |
DECL_OB(Type_Decl*, td, = str2decl(gwi, t)) |
96 |
|
24126 |
const Var_Decl var = str2var(gwi, n); |
97 |
✓✓ |
24126 |
if(var) { |
98 |
|
24125 |
const Arg_List arg = new_arg_list(gwi->gwion->mp, td, var, NULL); |
99 |
|
24125 |
vector_add(&gwi->ck->v, (vtype)arg); |
100 |
|
24125 |
return GW_OK; |
101 |
|
|
} |
102 |
|
1 |
free_type_decl(gwi->gwion->mp, td); |
103 |
|
1 |
return GW_ERROR; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
5 |
ANN m_int gwi_fptr_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name) { |
107 |
|
5 |
return dl_func_init(gwi, type, name); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
4 |
ANN static Fptr_Def import_fptr(const Gwi gwi, ae_flag flag) { |
111 |
|
4 |
Func_Base *base = gwi_func_base(gwi, gwi->ck); |
112 |
|
4 |
return new_fptr_def(gwi->gwion->mp, base, flag | ae_flag_builtin); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
4 |
ANN Type gwi_fptr_end(const Gwi gwi, const ae_flag flag) { |
116 |
✗✓ |
4 |
CHECK_BO(ck_ok(gwi, ck_fdef)) |
117 |
✗✓ |
4 |
DECL_OO(const Fptr_Def, fptr, = import_fptr(gwi, flag)) |
118 |
|
|
// what happens if it is in a template class ? |
119 |
|
4 |
const m_bool ret = traverse_fptr_def(gwi->gwion->env, fptr); |
120 |
✓✓ |
4 |
if(fptr->base->func) // is it needed ? |
121 |
|
3 |
SET_FLAG(fptr->base->func, builtin); |
122 |
✓✓ |
4 |
const Type t = ret > 0 ? fptr->type : NULL; |
123 |
|
4 |
free_fptr_def(gwi->gwion->mp, fptr); |
124 |
✓✓ |
4 |
if(fptr->type) |
125 |
|
3 |
REM_REF(fptr->type, gwi->gwion) |
126 |
|
4 |
ck_end(gwi); |
127 |
|
4 |
return t; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
11 |
ANN void ck_clean_fdef(MemPool mp, ImportCK *ck) { |
131 |
✓✓ |
11 |
if(ck->td) |
132 |
|
6 |
free_type_decl(mp, ck->td); |
133 |
✓✓ |
11 |
if(ck->v.ptr) { |
134 |
✓✓ |
9 |
for(m_uint i = 0; i < vector_size(&ck->v); ++i) { |
135 |
|
3 |
Arg_List list = (Arg_List)vector_at(&ck->v, i); |
136 |
|
3 |
list->next = NULL; |
137 |
|
3 |
free_arg_list(mp, list); |
138 |
|
|
} |
139 |
|
6 |
vector_release(&ck->v); |
140 |
|
|
} |
141 |
✓✓ |
11 |
if(ck->tmpl) |
142 |
|
3 |
free_id_list(mp, ck->tmpl); |
143 |
|
11 |
} |