Declaration scope lookup
[babeltrace.git] / types / variant.c
1 /*
2 * variant.c
3 *
4 * BabelTrace - Variant Type Converter
5 *
6 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
21 #include <errno.h>
22
23 static
24 struct declaration *_variant_declaration_new(struct type *type,
25 struct declaration_scope *parent_scope);
26 static
27 void _variant_declaration_free(struct declaration *declaration);
28
29 void variant_copy(struct stream_pos *dest, const struct format *fdest,
30 struct stream_pos *src, const struct format *fsrc,
31 struct declaration *declaration)
32 {
33 struct declaration_variant *variant =
34 container_of(declaration, struct declaration_variant, p);
35 struct type_variant *variant_type = variant->type;
36 struct field *field;
37 struct type *field_type;
38
39 fsrc->variant_begin(src, variant_type);
40 fdest->variant_begin(dest, variant_type);
41
42 field = variant_get_current_field(variant);
43 field_type = field->declaration->type;
44 field_type->copy(dest, fdest, src, fsrc, field->declaration);
45
46 fsrc->variant_end(src, variant_type);
47 fdest->variant_end(dest, variant_type);
48 }
49
50 static
51 void _variant_type_free(struct type *type)
52 {
53 struct type_variant *variant_type =
54 container_of(type, struct type_variant, p);
55 unsigned long i;
56
57 g_hash_table_destroy(variant_type->fields_by_tag);
58
59 for (i = 0; i < variant_type->fields->len; i++) {
60 struct type_field *type_field =
61 &g_array_index(variant_type->fields,
62 struct type_field, i);
63 type_unref(type_field->type);
64 }
65 g_array_free(variant_type->fields, true);
66 g_free(variant_type);
67 }
68
69 struct type_variant *variant_type_new(const char *name)
70 {
71 struct type_variant *variant_type;
72 struct type *type;
73
74 variant_type = g_new(struct type_variant, 1);
75 type = &variant_type->p;
76 variant_type->fields_by_tag = g_hash_table_new(g_direct_hash,
77 g_direct_equal);
78 variant_type->fields = g_array_sized_new(FALSE, TRUE,
79 sizeof(struct type_field),
80 DEFAULT_NR_STRUCT_FIELDS);
81 type->name = g_quark_from_string(name);
82 type->alignment = 1;
83 type->copy = variant_copy;
84 type->type_free = _variant_type_free;
85 type->declaration_new = _variant_declaration_new;
86 type->declaration_free = _variant_declaration_free;
87 type->ref = 1;
88 return variant_type;
89 }
90
91 static
92 struct declaration *
93 _variant_declaration_new(struct type *type,
94 struct declaration_scope *parent_scope)
95 {
96 struct type_variant *variant_type =
97 container_of(type, struct type_variant, p);
98 struct declaration_variant *variant;
99 unsigned long i;
100
101 variant = g_new(struct declaration_variant, 1);
102 type_ref(&variant_type->p);
103 variant->p.type = type;
104 variant->type = variant_type;
105 variant->p.ref = 1;
106 variant->scope = new_declaration_scope(parent_scope);
107 variant->fields = g_array_sized_new(FALSE, TRUE,
108 sizeof(struct field),
109 DEFAULT_NR_STRUCT_FIELDS);
110 g_array_set_size(variant->fields, variant_type->fields->len);
111 for (i = 0; i < variant_type->fields->len; i++) {
112 struct type_field *type_field =
113 &g_array_index(variant_type->fields,
114 struct type_field, i);
115 struct field *field = &g_array_index(variant->fields,
116 struct field, i);
117
118 field->name = type_field->name;
119 field->declaration =
120 type_field->type->declaration_new(type_field->type,
121 variant->scope);
122 }
123 variant->current_field = NULL;
124 return &variant->p;
125 }
126
127 static
128 void _variant_declaration_free(struct declaration *declaration)
129 {
130 struct declaration_variant *variant =
131 container_of(declaration, struct declaration_variant, p);
132 unsigned long i;
133
134 assert(variant->fields->len == variant->type->fields->len);
135 for (i = 0; i < variant->fields->len; i++) {
136 struct field *field = &g_array_index(variant->fields,
137 struct field, i);
138 declaration_unref(field->declaration);
139 }
140 free_declaration_scope(variant->scope);
141 type_unref(variant->p.type);
142 g_free(variant);
143 }
144
145 void variant_type_add_field(struct type_variant *variant_type,
146 const char *tag_name,
147 struct type *tag_type)
148 {
149 struct type_field *field;
150 unsigned long index;
151
152 g_array_set_size(variant_type->fields, variant_type->fields->len + 1);
153 index = variant_type->fields->len - 1; /* last field (new) */
154 field = &g_array_index(variant_type->fields, struct type_field, index);
155 field->name = g_quark_from_string(tag_name);
156 type_ref(tag_type);
157 field->type = tag_type;
158 /* Keep index in hash rather than pointer, because array can relocate */
159 g_hash_table_insert(variant_type->fields_by_tag,
160 (gpointer) (unsigned long) field->name,
161 (gpointer) index);
162 /*
163 * Alignment of variant is based on the alignment of its currently
164 * selected choice, so we leave variant alignment as-is (statically
165 * speaking).
166 */
167 }
168
169 struct type_field *
170 struct_type_get_field_from_tag(struct type_variant *variant_type, GQuark tag)
171 {
172 unsigned long index;
173
174 index = (unsigned long) g_hash_table_lookup(variant_type->fields_by_tag,
175 (gconstpointer) (unsigned long) tag);
176 return &g_array_index(variant_type->fields, struct type_field, index);
177 }
178
179 /*
180 * tag_instance is assumed to be an enumeration.
181 */
182 int variant_declaration_set_tag(struct declaration_variant *variant,
183 struct declaration *enum_tag)
184 {
185 struct declaration_enum *_enum =
186 container_of(variant->enum_tag, struct declaration_enum, p);
187 struct type_enum *enum_type = _enum->type;
188 int missing_field = 0;
189 unsigned long i;
190
191 /*
192 * Strictly speaking, each enumerator must map to a field of the
193 * variant. However, we are even stricter here by requiring that each
194 * variant choice map to an enumerator too. We then validate that the
195 * number of enumerators equals the number of variant choices.
196 */
197 if (variant->type->fields->len != enum_get_nr_enumerators(enum_type))
198 return -EPERM;
199
200 for (i = 0; i < variant->type->fields->len; i++) {
201 struct type_field *field_type =
202 &g_array_index(variant->type->fields,
203 struct type_field, i);
204 if (!enum_quark_to_range_set(enum_type, field_type->name)) {
205 missing_field = 1;
206 break;
207 }
208 }
209 if (missing_field)
210 return -EPERM;
211
212 /*
213 * Check the enumeration: it must map each value to one and only one
214 * enumerator tag.
215 * TODO: we should also check that each range map to one and only one
216 * tag. For the moment, we will simply check this dynamically in
217 * variant_type_get_current_field().
218 */
219
220 /* Set the enum tag field */
221 variant->enum_tag = enum_tag;
222 return 0;
223 }
224
225 /*
226 * field returned only valid as long as the field structure is not appended to.
227 */
228 struct field *variant_get_current_field(struct declaration_variant *variant)
229 {
230 struct declaration_enum *_enum =
231 container_of(variant->enum_tag, struct declaration_enum, p);
232 struct type_variant *variant_type = variant->type;
233 unsigned long index;
234 GArray *tag_array;
235 GQuark tag;
236
237 tag_array = _enum->value;
238 /*
239 * The 1 to 1 mapping from enumeration to value should have been already
240 * checked. (see TODO above)
241 */
242 assert(tag_array->len == 1);
243 tag = g_array_index(tag_array, GQuark, 0);
244 index = (unsigned long) g_hash_table_lookup(variant_type->fields_by_tag,
245 (gconstpointer) (unsigned long) tag);
246 variant->current_field = &g_array_index(variant->fields, struct field, index);
247 return variant->current_field;
248 }
This page took 0.039103 seconds and 5 git commands to generate.