Separate declaration scope from type scope
[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 type *
27 lookup_type_scope(GQuark type_name, struct type_scope *scope)
28 {
29 return g_hash_table_lookup(scope->types,
30 (gconstpointer) (unsigned long) type_name);
31 }
32
33 struct type *lookup_type(GQuark type_name, struct type_scope *scope)
34 {
35 struct type *type;
36
37 while (scope) {
38 type = lookup_type_scope(type_name, scope);
39 if (type)
40 return type;
41 scope = scope->parent_scope;
42 }
43 return NULL;
44 }
45
46 int register_type(GQuark name, struct type *type, struct type_scope *scope)
47 {
48 g_assert(name == type->name);
49
50 if (!name)
51 return -EPERM;
52
53 /* Only lookup in local scope */
54 if (lookup_type_scope(name, scope))
55 return -EEXIST;
56
57 g_hash_table_insert(scope->types,
58 (gpointer) (unsigned long) name,
59 type);
60 type_ref(type);
61 return 0;
62 }
63
64 static
65 struct declaration *
66 lookup_declaration_scope(GQuark field_name, struct declaration_scope *scope)
67 {
68 return g_hash_table_lookup(scope->declarations,
69 (gconstpointer) (unsigned long) field_name);
70 }
71
72 struct declaration *
73 lookup_declaration(GQuark field_name, struct declaration_scope *scope)
74 {
75 struct declaration *declaration;
76
77 while (scope) {
78 declaration = lookup_declaration_scope(field_name, scope);
79 if (declaration)
80 return declaration;
81 scope = scope->parent_scope;
82 }
83 return NULL;
84 }
85
86 int register_declaration(GQuark field_name, struct declaration *declaration,
87 struct declaration_scope *scope)
88 {
89 if (!field_name)
90 return -EPERM;
91
92 /* Only lookup in local scope */
93 if (lookup_declaration_scope(field_name, scope))
94 return -EEXIST;
95
96 g_hash_table_insert(scope->declarations,
97 (gpointer) (unsigned long) field_name,
98 declaration);
99 declaration_ref(declaration);
100 return 0;
101 }
102
103 void type_ref(struct type *type)
104 {
105 type->ref++;
106 }
107
108 void type_unref(struct type *type)
109 {
110 if (!--type->ref)
111 type->type_free(type);
112 }
113
114 void declaration_ref(struct declaration *declaration)
115 {
116 declaration->ref++;
117 }
118
119 void declaration_unref(struct declaration *declaration)
120 {
121 if (!--declaration->ref)
122 declaration->type->declaration_free(declaration);
123 }
124
125 struct type_scope *
126 new_type_scope(struct type_scope *parent_scope)
127 {
128 struct type_scope *scope = g_new(struct type_scope, 1);
129
130 scope->types = g_hash_table_new_full(g_direct_hash,
131 g_direct_equal, NULL,
132 (GDestroyNotify) type_unref);
133 scope->parent_scope = parent_scope;
134 return scope;
135 }
136
137 void free_type_scope(struct type_scope *scope)
138 {
139 g_hash_table_destroy(scope->types);
140 g_free(scope);
141 }
142
143 struct declaration_scope *
144 new_declaration_scope(struct declaration_scope *parent_scope)
145 {
146 struct declaration_scope *scope = g_new(struct declaration_scope, 1);
147
148 scope->declarations = g_hash_table_new_full(g_direct_hash,
149 g_direct_equal, NULL,
150 (GDestroyNotify) declaration_unref);
151 scope->parent_scope = parent_scope;
152 return scope;
153 }
154
155 void free_declaration_scope(struct declaration_scope *scope)
156 {
157 g_hash_table_destroy(scope->declarations);
158 g_free(scope);
159 }
This page took 0.031718 seconds and 4 git commands to generate.