GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/import/fdef.c Lines: 94 94 100.0 %
Date: 2020-09-12 17:36:58 Branches: 41 46 89.1 %

Line Branch Exec Source
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
41981
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
41981
  CHECK_BB(ck_ini(gwi, ck_fdef))
21
41980
  gwi->ck->name = n;
22
41980
  CHECK_BB(check_typename_def(gwi, gwi->ck))
23
41980
  CHECK_OB((gwi->ck->td = str2decl(gwi, t)))
24
41978
  vector_init(&gwi->ck->v);
25
41978
  return GW_OK;
26
}
27
28
41976
ANN m_int gwi_func_ini(const Gwi gwi, const restrict m_str t, const restrict m_str n) {
29
41976
  return dl_func_init(gwi, t, n);
30
}
31
32
41972
ANN Arg_List make_dll_arg_list(const Vector v) {
33
41972
  Arg_List base = (Arg_List)vector_front(v), arg_list = base;
34
49105
  for(m_uint i = 1; i < vector_size(v); ++i)
35
7133
    arg_list = (arg_list->next = (Arg_List)vector_at(v, i));
36
41972
  vector_release(v);
37
41972
  v->ptr = NULL;
38
41972
  return base;
39
}
40
41
41972
ANEW ANN static Func_Base* gwi_func_base(const Gwi gwi, ImportCK *ck) {
42
41972
  const Arg_List arg_list = make_dll_arg_list(&gwi->ck->v);
43
41972
  Func_Base *base = new_func_base(gwi->gwion->mp, ck->td, ck->sym, arg_list);
44
41972
  ck->td = NULL;
45
41972
  if(ck->tmpl) {
46
5
    base->tmpl = gwi_tmpl(gwi);
47
5
    ck->tmpl = NULL;
48
  }
49
41972
  return base;
50
}
51
52
41968
ANEW ANN static Func_Def import_fdef(const Gwi gwi, ImportCK *ck) {
53
41968
  Func_Base* base = gwi_func_base(gwi, ck);
54
125904
  const Func_Def fdef = new_func_def(gwi->gwion->mp, base,
55
83936
    NULL, ck->flag | ae_flag_builtin, loc(gwi));
56
41968
  fdef->d.dl_func_ptr = (void*)(m_uint)ck->addr;
57
41968
  if(base->tmpl)
58
4
    SET_FLAG(fdef, template);
59
41968
  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
41968
ANN m_int gwi_func_valid(const Gwi gwi, ImportCK *ck) {
75
41968
  const Func_Def fdef = import_fdef(gwi, ck);
76

41968
  if(SAFE_FLAG(gwi->gwion->env->class_def, template))
77
3
    /*return*/ section_fdef(gwi, fdef);
78
41968
  if(traverse_func_def(gwi->gwion->env, fdef) < 0)
79
3
    return error_fdef(gwi, fdef);
80
41965
  ck_end(gwi);
81
41965
  return GW_OK;
82
}
83
84
41968
ANN m_int gwi_func_end(const Gwi gwi, const f_xfun addr, const ae_flag flag) {
85
41968
  CHECK_BB(ck_ok(gwi, ck_fdef))
86
41968
  gwi->ck->addr = addr;
87
41968
  gwi->ck->flag = flag;
88
41968
  if(gwi_func_valid(gwi, gwi->ck) > 0)
89
41965
    return GW_OK;
90
3
  return GW_ERROR;
91
}
92
93
23502
ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, const restrict m_str n) {
94
23502
  CHECK_BB(ck_ok(gwi, ck_fdef))
95
23502
  DECL_OB(Type_Decl*, td, = str2decl(gwi, t))
96
23499
  const Var_Decl var = str2var(gwi, n);
97
23499
  if(var) {
98
23498
    const Arg_List arg = new_arg_list(gwi->gwion->mp, td, var, NULL);
99
23498
    vector_add(&gwi->ck->v, (vtype)arg);
100
23498
    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
}