Rename "declaration" to "definition"
[babeltrace.git] / types / types.c
CommitLineData
6dc2ca62 1/*
ccd7e1c8
MD
2 * types.c
3 *
d79865b9 4 * BabelTrace - Converter
6dc2ca62
MD
5 *
6 * Types registry.
7 *
c054553d 8 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
de0ba614 9 *
ccd7e1c8
MD
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:
de0ba614 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
6dc2ca62
MD
19 */
20
4c8bfb7e 21#include <babeltrace/format.h>
6dc2ca62 22#include <glib.h>
d1708134
MD
23#include <errno.h>
24
c054553d 25static
e1151715
MD
26struct definition *
27 lookup_type_definition_scope(GQuark type_name, struct type_scope *scope)
d1708134 28{
e1151715 29 return g_hash_table_lookup(scope->type_definitions,
ac88af75 30 (gconstpointer) (unsigned long) type_name);
d1708134
MD
31}
32
e1151715 33struct definition *lookup_type_definition(GQuark type_name, struct type_scope *scope)
c054553d 34{
e1151715 35 struct definition *definition;
c054553d
MD
36
37 while (scope) {
e1151715
MD
38 definition = lookup_type_definition_scope(type_name, scope);
39 if (definition)
40 return definition;
c054553d
MD
41 scope = scope->parent_scope;
42 }
43 return NULL;
44}
45
e1151715
MD
46int register_type_definition(GQuark name, struct definition *definition,
47 struct type_scope *scope)
d1708134 48{
64893f33 49 if (!name)
6ee5115e
MD
50 return -EPERM;
51
c054553d 52 /* Only lookup in local scope */
e1151715 53 if (lookup_type_definition_scope(name, scope))
d1708134
MD
54 return -EEXIST;
55
e1151715 56 g_hash_table_insert(scope->type_definitions,
64893f33 57 (gpointer) (unsigned long) name,
e1151715
MD
58 definition);
59 definition_ref(definition);
ac88af75
MD
60 return 0;
61}
62
63static
e1151715
MD
64struct definition *
65 lookup_field_definition_scope(GQuark field_name, struct definition_scope *scope)
ac88af75 66{
e1151715 67 return g_hash_table_lookup(scope->definitions,
ac88af75
MD
68 (gconstpointer) (unsigned long) field_name);
69}
70
e1151715
MD
71struct definition *
72 lookup_field_definition(GQuark field_name, struct definition_scope *scope)
ac88af75 73{
e1151715 74 struct definition *definition;
ac88af75
MD
75
76 while (scope) {
e1151715
MD
77 definition = lookup_field_definition_scope(field_name, scope);
78 if (definition)
79 return definition;
ac88af75
MD
80 scope = scope->parent_scope;
81 }
82 return NULL;
83}
84
e1151715
MD
85int register_field_definition(GQuark field_name, struct definition *definition,
86 struct definition_scope *scope)
ac88af75
MD
87{
88 if (!field_name)
89 return -EPERM;
90
91 /* Only lookup in local scope */
e1151715 92 if (lookup_field_definition_scope(field_name, scope))
ac88af75
MD
93 return -EEXIST;
94
e1151715 95 g_hash_table_insert(scope->definitions,
ac88af75 96 (gpointer) (unsigned long) field_name,
e1151715
MD
97 definition);
98 definition_ref(definition);
d1708134
MD
99 return 0;
100}
101
e19c3d69 102void type_ref(struct type *type)
4c8bfb7e 103{
e19c3d69 104 type->ref++;
4c8bfb7e
MD
105}
106
e19c3d69 107void type_unref(struct type *type)
4c8bfb7e 108{
e19c3d69 109 if (!--type->ref)
ac88af75 110 type->type_free(type);
4c8bfb7e
MD
111}
112
e1151715 113void definition_ref(struct definition *definition)
d1708134 114{
e1151715 115 definition->ref++;
d1708134
MD
116}
117
e1151715 118void definition_unref(struct definition *definition)
d1708134 119{
e1151715
MD
120 if (!--definition->ref)
121 definition->type->definition_free(definition);
c054553d
MD
122}
123
64893f33
MD
124struct type_scope *
125 new_type_scope(struct type_scope *parent_scope)
c054553d 126{
64893f33 127 struct type_scope *scope = g_new(struct type_scope, 1);
c054553d 128
e1151715 129 scope->type_definitions = g_hash_table_new_full(g_direct_hash,
c13cbf74 130 g_direct_equal, NULL,
e1151715 131 (GDestroyNotify) definition_unref);
c13cbf74
MD
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,
c054553d 139 g_direct_equal, NULL,
e19c3d69 140 (GDestroyNotify) type_unref);
64893f33
MD
141 scope->parent_scope = parent_scope;
142 return scope;
143}
144
145void free_type_scope(struct type_scope *scope)
146{
c13cbf74
MD
147 g_hash_table_destroy(scope->enum_types);
148 g_hash_table_destroy(scope->variant_types);
149 g_hash_table_destroy(scope->struct_types);
e1151715 150 g_hash_table_destroy(scope->type_definitions);
64893f33
MD
151 g_free(scope);
152}
153
c13cbf74
MD
154static
155struct 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
162struct 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
176int 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
193static
194struct 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
201struct 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
215int 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
233static
234struct 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
241struct 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
255int 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
e1151715
MD
272struct definition_scope *
273 new_definition_scope(struct definition_scope *parent_scope)
64893f33 274{
e1151715 275 struct definition_scope *scope = g_new(struct definition_scope, 1);
64893f33 276
e1151715 277 scope->definitions = g_hash_table_new_full(g_direct_hash,
ac88af75 278 g_direct_equal, NULL,
e1151715 279 (GDestroyNotify) definition_unref);
c054553d
MD
280 scope->parent_scope = parent_scope;
281 return scope;
282}
283
e1151715 284void free_definition_scope(struct definition_scope *scope)
c054553d 285{
e1151715 286 g_hash_table_destroy(scope->definitions);
c054553d 287 g_free(scope);
d1708134 288}
This page took 0.035661 seconds and 4 git commands to generate.