GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/import/import_enum.c Lines: 51 51 100.0 %
Date: 2020-09-21 18:02:52 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
720
ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) {
22
720
  CHECK_BB(ck_ini(gwi, ck_edef))
23
719
  if(type)
24
//    CHECK_OB((gwi->ck->sym = str2sym(gwi, type)))
25
715
    CHECK_OB((gwi->ck->xid = str2sym(gwi, type)))
26
719
  vector_init(&gwi->ck->v);
27
719
  return GW_OK;
28
}
29
30
// adds the id_list to the enum
31
// change that algo?
32
1467
ANN static void add2list(struct ImportCK* ck, const ID_List list) {
33
1467
  if(!ck->tmpl)
34
719
    ck->tmpl = list;
35
  else
36
748
    ck->curr->next = list;
37
1467
  ck->curr = list;
38
1467
}
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
1468
ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) {
52
1468
  CHECK_BB(ck_ok(gwi, ck_edef))
53
1467
  DECL_OB(const ID_List, list, = str2symlist(gwi, name))
54
1467
  add2list(gwi->ck, list);
55
1467
  ALLOC_PTR(gwi->gwion->mp, addr, m_int, i);
56
1467
  vector_add(&gwi->ck->v, (vtype)addr);
57
1467
  return GW_OK;
58
}
59
60
//! set enum values
61
//! \arg the importer
62
//! \arg a vector of values
63
//! \note [internal]
64
718
ANN static void import_enum_end(const Gwi gwi, const Vector v) {
65
718
  ImportCK *ck = gwi->ck;
66
2183
  for(m_uint i = 0; i < vector_size(v); i++) {
67
1465
    const Value value = (Value)vector_at(v, i);
68
1465
    const m_uint addr = vector_at(&ck->v, i);
69
1465
    SET_FLAG(value, builtin);
70
//    ADD_REF(value->type); // what ?
71
1465
    if(!gwi->gwion->env->class_def)
72
1445
      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
718
}
78
79
//! finish enum import
80
//! \arg the importer
81
//! TODO: check what happens in inside template class
82
718
ANN Type gwi_enum_end(const Gwi gwi) {
83
718
  CHECK_BO(ck_ok(gwi, ck_edef))
84
718
  const Gwion gwion = gwi->gwion;
85
2154
  const Enum_Def edef  = new_enum_def(gwion->mp, gwi->ck->tmpl,
86
1436
      gwi->ck->xid, loc(gwi));
87
718
  gwi->ck->tmpl = NULL;
88
718
  const m_bool ret = traverse_enum_def(gwion->env, edef);
89
718
  import_enum_end(gwi, &edef->values);
90
718
  const Type t = ret > 0 ? edef->t : NULL;
91
718
  free_enum_def(gwion->mp, edef);
92
718
  vector_release(&gwi->ck->v);
93
718
  gwi->ck->v.ptr = NULL;
94
718
  ck_end(gwi);
95
718
  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