GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/import/import_enum.c Lines: 51 51 100.0 %
Date: 2020-10-03 09:50:08 Branches: 23 30 76.7 %

Line Branch Exec Source
1
//! \file enum.c
2
//! \brief enumerations utils
3
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 "instr.h"
10
#include "object.h"
11
#include "emit.h"
12
#include "gwion.h"
13
#include "operator.h"
14
#include "import.h"
15
#include "gwi.h"
16
17
//! start an enum definition
18
//! \arg the importer
19
//! \arg string defining a primitive type
20
//! why is return type m_int ?
21
721
ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) {
22
721
  CHECK_BB(ck_ini(gwi, ck_edef))
23
720
  if(type)
24
//    CHECK_OB((gwi->ck->sym = str2sym(gwi, type)))
25
716
    CHECK_OB((gwi->ck->xid = str2sym(gwi, type)))
26
720
  vector_init(&gwi->ck->v);
27
720
  return GW_OK;
28
}
29
30
// adds the id_list to the enum
31
// change that algo?
32
1469
ANN static void add2list(struct ImportCK* ck, const ID_List list) {
33
1469
  if(!ck->tmpl)
34
720
    ck->tmpl = list;
35
  else
36
749
    ck->curr->next = list;
37
1469
  ck->curr = list;
38
1469
}
39
/*
40
void Append(DL_Enum* d, const ID_List list) {
41
  List* next = &d->base;
42
  while (*next != NULL) next = &(*next)->Next;
43
  *next = list;
44
  next->next = NULL;
45
}
46
*/
47
//! add an enum entry
48
//! \arg the importer
49
//! \arg name of the entry
50
//! TODO: change return type to m_bool
51
1470
ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) {
52
1470
  CHECK_BB(ck_ok(gwi, ck_edef))
53
1469
  DECL_OB(const ID_List, list, = str2symlist(gwi, name))
54
1469
  add2list(gwi->ck, list);
55
1469
  ALLOC_PTR(gwi->gwion->mp, addr, m_int, i);
56
1469
  vector_add(&gwi->ck->v, (vtype)addr);
57
1469
  return GW_OK;
58
}
59
60
//! set enum values
61
//! \arg the importer
62
//! \arg a vector of values
63
//! \note [internal]
64
719
ANN static void import_enum_end(const Gwi gwi, const Vector v) {
65
719
  ImportCK *ck = gwi->ck;
66
2186
  for(m_uint i = 0; i < vector_size(v); i++) {
67
1467
    const Value value = (Value)vector_at(v, i);
68
1467
    const m_uint addr = vector_at(&ck->v, i);
69
1467
    SET_FLAG(value, builtin);
70
//    ADD_REF(value->type); // what ?
71
1467
    if(!gwi->gwion->env->class_def)
72
1447
      value->d.ptr = (m_uint*)(addr ? addr : i);
73
    else
74
20
      value->d.ptr = (m_uint*)(addr ? *(m_uint*)addr : i);
75
  }
76
  // better clean ?
77
719
}
78
79
//! finish enum import
80
//! \arg the importer
81
//! TODO: check what happens in inside template class
82
719
ANN Type gwi_enum_end(const Gwi gwi) {
83
719
  CHECK_BO(ck_ok(gwi, ck_edef))
84
719
  const Gwion gwion = gwi->gwion;
85
2157
  const Enum_Def edef  = new_enum_def(gwion->mp, gwi->ck->tmpl,
86
1438
      gwi->ck->xid, loc(gwi));
87
719
  gwi->ck->tmpl = NULL;
88
719
  const m_bool ret = traverse_enum_def(gwion->env, edef);
89
719
  import_enum_end(gwi, &edef->values);
90
719
  const Type t = ret > 0 ? edef->t : NULL;
91
719
  free_enum_def(gwion->mp, edef);
92
719
  vector_release(&gwi->ck->v);
93
719
  gwi->ck->v.ptr = NULL;
94
719
  ck_end(gwi);
95
719
  return t;
96
}
97
98
1
ANN void ck_clean_edef(MemPool mp, ImportCK *ck) {
99
1
  if(ck->tmpl)
100
1
    free_id_list(mp, ck->tmpl);
101
1
  if(ck->v.ptr) {
102
2
    for(m_uint i = 0; i < vector_size(&ck->v); ++i)
103
1
      mp_free2(mp, SZ_INT, (m_uint*)vector_at(&ck->v, i));
104
1
    vector_release(&ck->v);
105
  }
106
1
}
107