Rename "type" to "declaration"
[babeltrace.git] / types / types.c
1 /*
2 * declarations.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_typedef_declaration_scope(GQuark declaration_name,
28 struct declaration_scope *scope)
29 {
30 return g_hash_table_lookup(scope->typedef_declarations,
31 (gconstpointer) (unsigned long) declaration_name);
32 }
33
34 struct definition *lookup_typedef_declaration(GQuark declaration_name,
35 struct declaration_scope *scope)
36 {
37 struct definition *definition;
38
39 while (scope) {
40 definition = lookup_typedef_declaration_scope(declaration_name,
41 scope);
42 if (definition)
43 return definition;
44 scope = scope->parent_scope;
45 }
46 return NULL;
47 }
48
49 int register_typedef_declaration(GQuark name, struct declaration *declaration,
50 struct declaration_scope *scope)
51 {
52 if (!name)
53 return -EPERM;
54
55 /* Only lookup in local scope */
56 if (lookup_typedef_declaration_scope(name, scope))
57 return -EEXIST;
58
59 g_hash_table_insert(scope->typedef_declarations,
60 (gpointer) (unsigned long) name,
61 declaration);
62 declaration_ref(declaration);
63 return 0;
64 }
65
66 static
67 struct definition *
68 lookup_field_definition_scope(GQuark field_name,
69 struct definition_scope *scope)
70 {
71 return g_hash_table_lookup(scope->definitions,
72 (gconstpointer) (unsigned long) field_name);
73 }
74
75 struct definition *
76 lookup_field_definition(GQuark field_name,
77 struct definition_scope *scope)
78 {
79 struct definition *definition;
80
81 while (scope) {
82 definition = lookup_field_definition_scope(field_name, scope);
83 if (definition)
84 return definition;
85 scope = scope->parent_scope;
86 }
87 return NULL;
88 }
89
90 int register_field_definition(GQuark field_name, struct definition *definition,
91 struct definition_scope *scope)
92 {
93 if (!field_name)
94 return -EPERM;
95
96 /* Only lookup in local scope */
97 if (lookup_field_definition_scope(field_name, scope))
98 return -EEXIST;
99
100 g_hash_table_insert(scope->definitions,
101 (gpointer) (unsigned long) field_name,
102 definition);
103 definition_ref(definition);
104 return 0;
105 }
106
107 void declaration_ref(struct declaration *declaration)
108 {
109 declaration->ref++;
110 }
111
112 void declaration_unref(struct declaration *declaration)
113 {
114 if (!--declaration->ref)
115 declaration->declaration_free(declaration);
116 }
117
118 void definition_ref(struct definition *definition)
119 {
120 definition->ref++;
121 }
122
123 void definition_unref(struct definition *definition)
124 {
125 if (!--definition->ref)
126 definition->declaration->definition_free(definition);
127 }
128
129 struct declaration_scope *
130 new_declaration_scope(struct declaration_scope *parent_scope)
131 {
132 struct declaration_scope *scope = g_new(struct declaration_scope, 1);
133
134 scope->typedef_declarations = g_hash_table_new_full(g_direct_hash,
135 g_direct_equal, NULL,
136 (GDestroyNotify) definition_unref);
137 scope->struct_declarations = g_hash_table_new_full(g_direct_hash,
138 g_direct_equal, NULL,
139 (GDestroyNotify) declaration_unref);
140 scope->variant_declarations = g_hash_table_new_full(g_direct_hash,
141 g_direct_equal, NULL,
142 (GDestroyNotify) declaration_unref);
143 scope->enum_declarations = g_hash_table_new_full(g_direct_hash,
144 g_direct_equal, NULL,
145 (GDestroyNotify) declaration_unref);
146 scope->parent_scope = parent_scope;
147 return scope;
148 }
149
150 void free_declaration_scope(struct declaration_scope *scope)
151 {
152 g_hash_table_destroy(scope->enum_declarations);
153 g_hash_table_destroy(scope->variant_declarations);
154 g_hash_table_destroy(scope->struct_declarations);
155 g_hash_table_destroy(scope->typedef_declarations);
156 g_free(scope);
157 }
158
159 static
160 struct declaration_struct *lookup_struct_declaration_scope(GQuark struct_name,
161 struct declaration_scope *scope)
162 {
163 return g_hash_table_lookup(scope->struct_declarations,
164 (gconstpointer) (unsigned long) struct_name);
165 }
166
167 struct declaration_struct *lookup_struct_declaration(GQuark struct_name,
168 struct declaration_scope *scope)
169 {
170 struct declaration_struct *declaration;
171
172 while (scope) {
173 declaration = lookup_struct_declaration_scope(struct_name, scope);
174 if (declaration)
175 return declaration;
176 scope = scope->parent_scope;
177 }
178 return NULL;
179 }
180
181 int register_struct_declaration(GQuark struct_name,
182 struct declaration_struct *struct_declaration,
183 struct declaration_scope *scope)
184 {
185 if (!struct_name)
186 return -EPERM;
187
188 /* Only lookup in local scope */
189 if (lookup_struct_declaration_scope(struct_name, scope))
190 return -EEXIST;
191
192 g_hash_table_insert(scope->struct_declarations,
193 (gpointer) (unsigned long) struct_name,
194 struct_declaration);
195 declaration_ref(&struct_declaration->p);
196 return 0;
197 }
198
199 static
200 struct declaration_variant *
201 lookup_variant_declaration_scope(GQuark variant_name,
202 struct declaration_scope *scope)
203 {
204 return g_hash_table_lookup(scope->variant_declarations,
205 (gconstpointer) (unsigned long) variant_name);
206 }
207
208 struct declaration_variant *
209 lookup_variant_declaration(GQuark variant_name,
210 struct declaration_scope *scope)
211 {
212 struct declaration_variant *declaration;
213
214 while (scope) {
215 declaration = lookup_variant_declaration_scope(variant_name, scope);
216 if (declaration)
217 return declaration;
218 scope = scope->parent_scope;
219 }
220 return NULL;
221 }
222
223 int register_variant_declaration(GQuark variant_name,
224 struct declaration_variant *variant_declaration,
225 struct declaration_scope *scope)
226 {
227 if (!variant_name)
228 return -EPERM;
229
230 /* Only lookup in local scope */
231 if (lookup_variant_declaration_scope(variant_name, scope))
232 return -EEXIST;
233
234 g_hash_table_insert(scope->variant_declarations,
235 (gpointer) (unsigned long) variant_name,
236 variant_declaration);
237 declaration_ref(&variant_declaration->p);
238 return 0;
239 }
240
241 static
242 struct declaration_enum *
243 lookup_enum_declaration_scope(GQuark enum_name,
244 struct declaration_scope *scope)
245 {
246 return g_hash_table_lookup(scope->enum_declarations,
247 (gconstpointer) (unsigned long) enum_name);
248 }
249
250 struct declaration_enum *
251 lookup_enum_declaration(GQuark enum_name,
252 struct declaration_scope *scope)
253 {
254 struct declaration_enum *declaration;
255
256 while (scope) {
257 declaration = lookup_enum_declaration_scope(enum_name, scope);
258 if (declaration)
259 return declaration;
260 scope = scope->parent_scope;
261 }
262 return NULL;
263 }
264
265 int register_enum_declaration(GQuark enum_name,
266 struct declaration_enum *enum_declaration,
267 struct declaration_scope *scope)
268 {
269 if (!enum_name)
270 return -EPERM;
271
272 /* Only lookup in local scope */
273 if (lookup_enum_declaration_scope(enum_name, scope))
274 return -EEXIST;
275
276 g_hash_table_insert(scope->enum_declarations,
277 (gpointer) (unsigned long) enum_name,
278 enum_declaration);
279 declaration_ref(&enum_declaration->p);
280 return 0;
281 }
282
283 struct definition_scope *
284 new_definition_scope(struct definition_scope *parent_scope)
285 {
286 struct definition_scope *scope = g_new(struct definition_scope, 1);
287
288 scope->definitions = g_hash_table_new_full(g_direct_hash,
289 g_direct_equal, NULL,
290 (GDestroyNotify) definition_unref);
291 scope->parent_scope = parent_scope;
292 return scope;
293 }
294
295 void free_definition_scope(struct definition_scope *scope)
296 {
297 g_hash_table_destroy(scope->definitions);
298 g_free(scope);
299 }
This page took 0.034543 seconds and 4 git commands to generate.