File: funcref.c

package info (click to toggle)
libi8x 0.0.5-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 7,192 kB
  • ctags: 1,365
  • sloc: ansic: 6,874; python: 1,339; makefile: 146; sh: 102
file content (201 lines) | stat: -rw-r--r-- 4,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Copyright (C) 2016-17 Red Hat, Inc.
   This file is part of the Infinity Note Execution Library.

   The Infinity Note Execution Library is free software; you can
   redistribute it and/or modify it under the terms of the GNU Lesser
   General Public License as published by the Free Software
   Foundation; either version 2.1 of the License, or (at your option)
   any later version.

   The Infinity Note Execution Library is distributed in the hope that
   it will be useful, but WITHOUT ANY WARRANTY; without even the
   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
   PURPOSE.  See the GNU Lesser General Public License for more
   details.

   You should have received a copy of the GNU Lesser General Public
   License along with the Infinity Note Execution Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <string.h>
#include "libi8x-private.h"
#include "funcref-private.h"

static i8x_err_e
i8x_funcref_init (struct i8x_funcref *ref, const char *fullname,
		  struct i8x_type *type)
{
  ref->fullname = strdup (fullname);
  if (ref->fullname == NULL)
    return i8x_out_of_memory (i8x_funcref_get_ctx (ref));

  if (strstr (fullname, "::__") != NULL)
    ref->is_private = true;

  ref->type = i8x_type_ref (type);

  ref->num_args = i8x_list_size (i8x_type_get_ptypes (type));
  ref->num_rets = i8x_list_size (i8x_type_get_rtypes (type));

  return I8X_OK;
}

static void
i8x_funcref_unlink (struct i8x_object *ob)
{
  struct i8x_funcref *ref = (struct i8x_funcref *) ob;

  ref->type = i8x_type_unref (ref->type);

  i8x_ctx_forget_funcref (ref);
}

static void
i8x_funcref_free (struct i8x_object *ob)
{
  struct i8x_funcref *ref = (struct i8x_funcref *) ob;

  if (ref->fullname != NULL)
    free (ref->fullname);
}

const struct i8x_object_ops i8x_funcref_ops =
  {
    "funcref",				/* Object name.  */
    sizeof (struct i8x_funcref),	/* Object size.  */
    i8x_funcref_unlink,			/* Unlink function.  */
    i8x_funcref_free,			/* Free function.  */
  };

i8x_err_e
i8x_funcref_new (struct i8x_ctx *ctx, const char *fullname,
		 struct i8x_type *type, struct i8x_funcref **ref)
{
  struct i8x_funcref *r;
  i8x_err_e err;

  err = i8x_ob_new (ctx, &i8x_funcref_ops, &r);
  if (err != I8X_OK)
    return err;

  err = i8x_funcref_init (r, fullname, type);
  if (err != I8X_OK)
    {
      r = i8x_funcref_unref (r);

      return err;
    }

  dbg (ctx, "funcref %p is %s\n", r, fullname);

  *ref = r;

  return I8X_OK;
}

I8X_EXPORT const char *
i8x_funcref_get_fullname (struct i8x_funcref *ref)
{
  return ref->fullname;
}

I8X_EXPORT bool
i8x_funcref_is_private (struct i8x_funcref *ref)
{
  return ref->is_private;
}

I8X_EXPORT size_t
i8x_funcref_get_num_params (struct i8x_funcref *ref)
{
  return ref->num_args;
}

I8X_EXPORT size_t
i8x_funcref_get_num_returns (struct i8x_funcref *ref)
{
  return ref->num_rets;
}

void
i8x_func_register (struct i8x_func *func)
{
  struct i8x_funcref *ref = i8x_func_get_funcref (func);

  ref->regcount++;

  if (ref->regcount != 1)
    func = NULL;

  ref->unique = func;
}

void
i8x_func_unregister (struct i8x_func *func)
{
  struct i8x_funcref *ref = i8x_func_get_funcref (func);

  ref->regcount--;

  if (ref->regcount == 1)
    {
      struct i8x_ctx *ctx = i8x_funcref_get_ctx (ref);
      struct i8x_listitem *li;

      func = NULL;
      i8x_list_foreach (i8x_ctx_get_functions (ctx), li)
	{
	  func = i8x_listitem_get_func (li);

	  if (i8x_func_get_funcref (func) == ref)
	    break;

	  func = NULL;
	}
    }
  else
    func = NULL;

  ref->unique = func;
}

void
i8x_funcref_reset_is_resolved (struct i8x_funcref *ref)
{
  struct i8x_func *resolved = ref->unique;
  struct i8x_code *interp_impl = NULL;
  i8x_nat_fn_t *native_impl = NULL;

  if (resolved != NULL)
    {
      interp_impl = i8x_func_get_interp_impl (resolved);
      native_impl = i8x_func_get_native_impl (resolved);

      i8x_assert ((interp_impl != NULL) ^ (native_impl != NULL));
    }

  ref->resolved = resolved;
  ref->interp_impl = interp_impl;
  ref->native_impl = native_impl;

}

void
i8x_funcref_mark_unresolved (struct i8x_funcref *ref)
{
  ref->resolved = NULL;
  ref->interp_impl = NULL;
  ref->native_impl = NULL;
}

I8X_EXPORT bool
i8x_funcref_is_resolved (struct i8x_funcref *ref)
{
  return ref->resolved != NULL;
}

I8X_EXPORT struct i8x_type *
i8x_funcref_get_type (struct i8x_funcref *ref)
{
  return ref->type;
}