Separate declaration scope from type scope
[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
ac88af75 26struct type *
64893f33 27 lookup_type_scope(GQuark type_name, struct type_scope *scope)
d1708134 28{
e19c3d69 29 return g_hash_table_lookup(scope->types,
ac88af75 30 (gconstpointer) (unsigned long) type_name);
d1708134
MD
31}
32
64893f33 33struct type *lookup_type(GQuark type_name, struct type_scope *scope)
c054553d 34{
e19c3d69 35 struct type *type;
c054553d
MD
36
37 while (scope) {
ac88af75 38 type = lookup_type_scope(type_name, scope);
e19c3d69
MD
39 if (type)
40 return type;
c054553d
MD
41 scope = scope->parent_scope;
42 }
43 return NULL;
44}
45
64893f33 46int register_type(GQuark name, struct type *type, struct type_scope *scope)
d1708134 47{
64893f33
MD
48 g_assert(name == type->name);
49
50 if (!name)
6ee5115e
MD
51 return -EPERM;
52
c054553d 53 /* Only lookup in local scope */
64893f33 54 if (lookup_type_scope(name, scope))
d1708134
MD
55 return -EEXIST;
56
e19c3d69 57 g_hash_table_insert(scope->types,
64893f33 58 (gpointer) (unsigned long) name,
e19c3d69 59 type);
ac88af75
MD
60 type_ref(type);
61 return 0;
62}
63
64static
65struct 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
72struct 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
86int 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);
d1708134
MD
100 return 0;
101}
102
e19c3d69 103void type_ref(struct type *type)
4c8bfb7e 104{
e19c3d69 105 type->ref++;
4c8bfb7e
MD
106}
107
e19c3d69 108void type_unref(struct type *type)
4c8bfb7e 109{
e19c3d69 110 if (!--type->ref)
ac88af75 111 type->type_free(type);
4c8bfb7e
MD
112}
113
e19c3d69 114void declaration_ref(struct declaration *declaration)
d1708134 115{
e19c3d69 116 declaration->ref++;
d1708134
MD
117}
118
e19c3d69 119void declaration_unref(struct declaration *declaration)
d1708134 120{
e19c3d69 121 if (!--declaration->ref)
ac88af75 122 declaration->type->declaration_free(declaration);
c054553d
MD
123}
124
64893f33
MD
125struct type_scope *
126 new_type_scope(struct type_scope *parent_scope)
c054553d 127{
64893f33 128 struct type_scope *scope = g_new(struct type_scope, 1);
c054553d 129
e19c3d69 130 scope->types = g_hash_table_new_full(g_direct_hash,
c054553d 131 g_direct_equal, NULL,
e19c3d69 132 (GDestroyNotify) type_unref);
64893f33
MD
133 scope->parent_scope = parent_scope;
134 return scope;
135}
136
137void free_type_scope(struct type_scope *scope)
138{
139 g_hash_table_destroy(scope->types);
140 g_free(scope);
141}
142
143struct declaration_scope *
144 new_declaration_scope(struct declaration_scope *parent_scope)
145{
146 struct declaration_scope *scope = g_new(struct declaration_scope, 1);
147
ac88af75
MD
148 scope->declarations = g_hash_table_new_full(g_direct_hash,
149 g_direct_equal, NULL,
150 (GDestroyNotify) declaration_unref);
c054553d
MD
151 scope->parent_scope = parent_scope;
152 return scope;
153}
154
155void free_declaration_scope(struct declaration_scope *scope)
156{
ac88af75 157 g_hash_table_destroy(scope->declarations);
c054553d 158 g_free(scope);
d1708134 159}
This page took 0.029969 seconds and 4 git commands to generate.