-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlabelobj.c
129 lines (116 loc) · 3.67 KB
/
labelobj.c
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
/*
$Id: labelobj.c 2675 2021-05-20 20:53:26Z soci $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "labelobj.h"
#include <string.h>
#include "values.h"
#include "error.h"
#include "unicode.h"
#include "file.h"
#include "strobj.h"
#include "typeobj.h"
static Type obj;
Type *const LABEL_OBJ = &obj;
static FAST_CALL void destroy(Obj *o1) {
Label *v1 = Label(o1);
const struct file_s *cfile = v1->file_list->file;
if (not_in_file(v1->name.data, cfile)) free((char *)v1->name.data);
if (v1->name.data != v1->cfname.data) free((uint8_t *)v1->cfname.data);
val_destroy(v1->value);
}
static FAST_CALL void garbage(Obj *o1, int i) {
Label *v1 = Label(o1);
Obj *v;
const struct file_s *cfile;
switch (i) {
case -1:
v1->value->refcount--;
return;
case 0:
cfile = v1->file_list->file;
if (not_in_file(v1->name.data, cfile)) free((char *)v1->name.data);
if (v1->name.data != v1->cfname.data) free((uint8_t *)v1->cfname.data);
return;
case 1:
v = v1->value;
if ((v->refcount & SIZE_MSB) != 0) {
v->refcount -= SIZE_MSB - 1;
v->obj->garbage(v, 1);
} else v->refcount++;
return;
}
}
static FAST_CALL bool same(const Obj *o1, const Obj *o2) {
const Label *v1 = Label(o1), *v2 = Label(o2);
return o1->obj == o2->obj
&& v1->epoint.pos == v2->epoint.pos
&& v1->epoint.line == v2->epoint.line
&& v1->file_list == v2->file_list
&& v1->strength == v2->strength
&& str_cmp(&v1->name, &v2->name) == 0
&& (v1->value == v2->value || v1->value->obj->same(v1->value, v2->value));
}
static MUST_CHECK Obj *repr(Obj *o1, linepos_t epoint, size_t maxlen) {
Label *v1 = Label(o1);
size_t len, len2;
uint8_t *s;
Str *v;
if (epoint == NULL) return NULL;
switch (v1->name.data[0]) {
case '+':
case '-': len = 1; break;
case '#':
case '.': len = 0; break;
default: len = v1->name.len;
}
len2 = len + 10;
if (len2 > maxlen) return NULL;
v = new_str2(len2);
if (v == NULL) return NULL;
v->chars = calcpos(v1->name.data, len);
s = v->data;
memcpy(s, "<label '", 8);
s += 8;
memcpy(s, v1->name.data, len);
s[len] = '\'';
s[len + 1] = '>';
return Obj(v);
}
static MUST_CHECK Obj *str(Obj *o1, linepos_t UNUSED(epoint), size_t maxlen) {
Label *v1 = Label(o1);
size_t len, chars;
Str *v;
switch (v1->name.data[0]) {
case '+':
case '-': len = 1; break;
case '#':
case '.': return NULL;
default: len = v1->name.len;
}
chars = calcpos(v1->name.data, len);
if (chars > maxlen) return NULL;
v = new_str2(len);
if (v == NULL) return NULL;
v->chars = chars;
memcpy(v->data, v1->name.data, len);
return Obj(v);
}
void labelobj_init(void) {
Type *type = new_type(&obj, T_LABEL, "label", sizeof(Label));
type->destroy = destroy;
type->garbage = garbage;
type->same = same;
type->repr = repr;
type->str = str;
}