Rename "declaration" to "definition"
[babeltrace.git] / types / types.c
1 /*
2 * types.c
3 *
4 * BabelTrace - Converter
5 *
6 * Types registry.
7 *
8 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/format.h>
22 #include <glib.h>
23 #include <errno.h>
24
25 static
26 struct definition *
27 lookup_type_definition_scope(GQuark type_name, struct type_scope *scope)
28 {
29 return g_hash_table_lookup(scope->type_definitions,
30 (gconstpointer) (unsigned long) type_name);
31 }
32
33 struct definition *lookup_type_definition(GQuark type_name, struct type_scope *scope)
34 {
35 struct definition *definition;
36
37 while (scope) {
38 definition = lookup_type_definition_scope(type_name, scope);
39 if (definition)
40 return definition;
41 scope = scope->parent_scope;
42 }
43 return NULL;
44 }
45
46 int register_type_definition(GQuark name, struct definition *definition,
47 struct type_scope *scope)
48 {
49 if (!name)
50 return -EPERM;
51
52 /* Only lookup in local scope */
53 if (lookup_type_definition_scope(name, scope))
54 return -EEXIST;
55
56 g_hash_table_insert(scope->type_definitions,
57 (gpointer) (unsigned long) name,
58 definition);
59 definition_ref(definition);
60 return 0;
61 }
62
63 static
64 struct definition *
65 lookup_field_definition_scope(GQuark field_name, struct definition_scope *scope)
66 {
67 return g_hash_table_lookup(scope->definitions,
68 (gconstpointer) (unsigned long) field_name);
69 }
70
71 struct definition *
72 lookup_field_definition(GQuark field_name, struct definition_scope *scope)
73 {
74 struct definition *definition;
75
76 while (scope) {
77 definition = lookup_field_definition_scope(field_name, scope);
78 if (definition)
79 return definition;
80 scope = scope->parent_scope;
81 }
82 return NULL;
83 }
84
85 int register_field_definition(GQuark field_name, struct definition *definition,
86 struct definition_scope *scope)
87 {
88 if (!field_name)
89 return -EPERM;
90
91 /* Only lookup in local scope */
92 if (lookup_field_definition_scope(field_name, scope))
93 return -EEXIST;
94
95 g_hash_table_insert(scope->definitions,
96 (gpointer) (unsigned long) field_name,
97 definition);
98 definition_ref(definition);
99 return 0;
100 }
101
102 void type_ref(struct type *type)
103 {
104 type->ref++;
105 }
106
107 void type_unref(struct type *type)
108 {
109 if (!--type->ref)
110 type->type_free(type);
111 }
112
113 void definition_ref(struct definition *definition)
114 {
115 definition->ref++;
116 }
117
118 void definition_unref(struct definition *definition)
119 {
120 if (!--definition->ref)
121 definition->type->definition_free(definition);
122 }
123
124 struct type_scope *
125 new_type_scope(struct type_scope *parent_scope)
126 {
127 struct type_scope *scope = g_new(struct type_scope, 1);
128
129 scope->type_definitions = g_hash_table_new_full(g_direct_hash,
130 g_direct_equal, NULL,
131 (GDestroyNotify) definition_unref);
132 scope->struct_types = g_hash_table_new_full(g_direct_hash,
133 g_direct_equal, NULL,
134 (GDestroyNotify) type_unref);
135 scope->variant_types = g_hash_table_new_full(g_direct_hash,
136 g_direct_equal, NULL,
137 (GDestroyNotify) type_unref);
138 scope->enum_types = g_hash_table_new_full(g_direct_hash,
139 g_direct_equal, NULL,
140 (GDestroyNotify) type_unref);
141 scope->parent_scope = parent_scope;
142 return scope;
143 }
144
145 void free_type_scope(struct type_scope *scope)
146 {
147 g_hash_table_destroy(scope->enum_types);
148 g_hash_table_destroy(scope->variant_types);
149 g_hash_table_destroy(scope->struct_types);
150 g_hash_table_destroy(scope->type_definitions);
151 g_free(scope);
152 }
153
154 static
155 struct type_struct *lookup_struct_type_scope(GQuark struct_name,
156 struct type_scope *scope)
157 {
158 return g_hash_table_lookup(scope->struct_types,
159 (gconstpointer) (unsigned long) struct_name);
160 }
161
162 struct type_struct *lookup_struct_type(GQuark struct_name,
163 struct type_scope *scope)
164 {
165 struct type_struct *type;
166
167 while (scope) {
168 type = lookup_struct_type_scope(struct_name, scope);
169 if (type)
170 return type;
171 scope = scope->parent_scope;
172 }
173 return NULL;
174 }
175
176 int register_struct_type(GQuark struct_name, struct type_struct *struct_type,
177 struct type_scope *scope)
178 {
179 if (!struct_name)
180 return -EPERM;
181
182 /* Only lookup in local scope */
183 if (lookup_struct_type_scope(struct_name, scope))
184 return -EEXIST;
185
186 g_hash_table_insert(scope->struct_types,
187 (gpointer) (unsigned long) struct_name,
188 struct_type);
189 type_ref(&struct_type->p);
190 return 0;
191 }
192
193 static
194 struct type_variant *lookup_variant_type_scope(GQuark variant_name,
195 struct type_scope *scope)
196 {
197 return g_hash_table_lookup(scope->variant_types,
198 (gconstpointer) (unsigned long) variant_name);
199 }
200
201 struct type_variant *lookup_variant_type(GQuark variant_name,
202 struct type_scope *scope)
203 {
204 struct type_variant *type;
205
206 while (scope) {
207 type = lookup_variant_type_scope(variant_name, scope);
208 if (type)
209 return type;
210 scope = scope->parent_scope;
211 }
212 return NULL;
213 }
214
215 int register_variant_type(GQuark variant_name,
216 struct type_variant *variant_type,
217 struct type_scope *scope)
218 {
219 if (!variant_name)
220 return -EPERM;
221
222 /* Only lookup in local scope */
223 if (lookup_variant_type_scope(variant_name, scope))
224 return -EEXIST;
225
226 g_hash_table_insert(scope->variant_types,
227 (gpointer) (unsigned long) variant_name,
228 variant_type);
229 type_ref(&variant_type->p);
230 return 0;
231 }
232
233 static
234 struct type_enum *lookup_enum_type_scope(GQuark enum_name,
235 struct type_scope *scope)
236 {
237 return g_hash_table_lookup(scope->enum_types,
238 (gconstpointer) (unsigned long) enum_name);
239 }
240
241 struct type_enum *lookup_enum_type(GQuark enum_name,
242 struct type_scope *scope)
243 {
244 struct type_enum *type;
245
246 while (scope) {
247 type = lookup_enum_type_scope(enum_name, scope);
248 if (type)
249 return type;
250 scope = scope->parent_scope;
251 }
252 return NULL;
253 }
254
255 int register_enum_type(GQuark enum_name, struct type_enum *enum_type,
256 struct type_scope *scope)
257 {
258 if (!enum_name)
259 return -EPERM;
260
261 /* Only lookup in local scope */
262 if (lookup_enum_type_scope(enum_name, scope))
263 return -EEXIST;
264
265 g_hash_table_insert(scope->enum_types,
266 (gpointer) (unsigned long) enum_name,
267 enum_type);
268 type_ref(&enum_type->p);
269 return 0;
270 }
271
272 struct definition_scope *
273 new_definition_scope(struct definition_scope *parent_scope)
274 {
275 struct definition_scope *scope = g_new(struct definition_scope, 1);
276
277 scope->definitions = g_hash_table_new_full(g_direct_hash,
278 g_direct_equal, NULL,
279 (GDestroyNotify) definition_unref);
280 scope->parent_scope = parent_scope;
281 return scope;
282 }
283
284 void free_definition_scope(struct definition_scope *scope)
285 {
286 g_hash_table_destroy(scope->definitions);
287 g_free(scope);
288 }
This page took 0.03523 seconds and 5 git commands to generate.