Declaration scope lookup
[babeltrace.git] / include / babeltrace / types.h
CommitLineData
d79865b9
MD
1#ifndef _BABELTRACE_TYPES_H
2#define _BABELTRACE_TYPES_H
3
4/*
5 * BabelTrace
6 *
fc93b2bd 7 * Type Header
d79865b9 8 *
c054553d 9 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d79865b9 10 *
ccd7e1c8
MD
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
d79865b9 17 *
ccd7e1c8
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
d79865b9
MD
20 */
21
bed864a7 22#include <babeltrace/align.h>
d65d8abb 23#include <babeltrace/list.h>
4c8bfb7e
MD
24#include <stdbool.h>
25#include <stdint.h>
26#include <limits.h>
bed864a7 27#include <string.h>
4c8bfb7e 28#include <glib.h>
8eab883c 29#include <assert.h>
bed864a7 30
11796b96
MD
31/* Preallocate this many fields for structures */
32#define DEFAULT_NR_STRUCT_FIELDS 8
33
bed864a7
MD
34/*
35 * Always update stream_pos with move_pos and init_pos.
36 */
37struct stream_pos {
47e0f2e2 38 char *base; /* Base address */
bed864a7
MD
39 size_t offset; /* Offset from base, in bits */
40 int dummy; /* Dummy position, for length calculation */
41};
42
43static inline
47e0f2e2 44void init_pos(struct stream_pos *pos, char *base)
bed864a7
MD
45{
46 pos->base = base; /* initial base, page-aligned */
47 pos->offset = 0;
48 pos->dummy = false;
49}
50
51/*
52 * move_pos - move position of a relative bit offset
53 *
54 * TODO: allow larger files by updating base too.
55 */
56static inline
57void move_pos(struct stream_pos *pos, size_t offset)
58{
59 pos->offset = pos->offset + offset;
60}
61
62/*
63 * align_pos - align position on a bit offset (> 0)
64 *
65 * TODO: allow larger files by updating base too.
66 */
67static inline
68void align_pos(struct stream_pos *pos, size_t offset)
69{
70 pos->offset += offset_align(pos->offset, offset);
71}
72
73static inline
74void copy_pos(struct stream_pos *dest, struct stream_pos *src)
75{
76 memcpy(dest, src, sizeof(struct stream_pos));
77}
78
79static inline
47e0f2e2 80char *get_pos_addr(struct stream_pos *pos)
bed864a7
MD
81{
82 /* Only makes sense to get the address after aligning on CHAR_BIT */
4c8bfb7e 83 assert(!(pos->offset % CHAR_BIT));
bed864a7
MD
84 return pos->base + (pos->offset / CHAR_BIT);
85}
fc93b2bd 86
4c8bfb7e 87struct format;
e19c3d69 88struct declaration;
4c8bfb7e 89
6b71274a
MD
90/* Type declaration scope */
91struct declaration_scope {
e19c3d69
MD
92 /* Hash table mapping type name GQuark to struct type */
93 GHashTable *types;
ac88af75
MD
94 /* Hash table mapping field name GQuark to struct declaration */
95 GHashTable *declarations;
6b71274a
MD
96 struct declaration_scope *parent_scope;
97};
98
e19c3d69 99struct type {
fc93b2bd
MD
100 GQuark name; /* type name */
101 size_t alignment; /* type alignment, in bits */
e19c3d69 102 int ref; /* number of references to the type */
c054553d
MD
103 /*
104 * type_free called with type ref is decremented to 0.
105 */
106 void (*type_free)(struct type *type);
e19c3d69
MD
107 struct declaration *
108 (*declaration_new)(struct type *type,
109 struct declaration_scope *parent_scope);
c054553d 110 /*
e19c3d69
MD
111 * declaration_free called with declaration ref is decremented to 0.
112 */
113 void (*declaration_free)(struct declaration *declaration);
114 /*
115 * Declaration copy function. Knows how to find the child declaration
116 * from the parent declaration.
fc93b2bd 117 */
4c8bfb7e
MD
118 void (*copy)(struct stream_pos *dest, const struct format *fdest,
119 struct stream_pos *src, const struct format *fsrc,
e19c3d69 120 struct declaration *declaration);
c054553d
MD
121};
122
e19c3d69
MD
123struct declaration {
124 struct type *type;
125 int ref; /* number of references to the declaration */
fc93b2bd
MD
126};
127
bed864a7
MD
128/*
129 * Because we address in bits, bitfields end up being exactly the same as
130 * integers, except that their read/write functions must be able to deal with
131 * read/write non aligned on CHAR_BIT.
132 */
e19c3d69
MD
133struct type_integer {
134 struct type p;
7fe00194
MD
135 size_t len; /* length, in bits. */
136 int byte_order; /* byte order */
137 int signedness;
fc93b2bd
MD
138};
139
e19c3d69
MD
140struct declaration_integer {
141 struct declaration p;
142 struct type_integer *type;
c054553d
MD
143 /* Last values read */
144 union {
145 uint64_t _unsigned;
146 int64_t _signed;
147 } value;
148};
149
e19c3d69
MD
150struct type_float {
151 struct type p;
152 struct type_integer *sign;
153 struct type_integer *mantissa;
154 struct type_integer *exp;
fc93b2bd 155 int byte_order;
0a46062b 156 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
157};
158
e19c3d69
MD
159struct declaration_float {
160 struct declaration p;
161 struct type_float *type;
c054553d
MD
162 /* Last values read */
163 long double value;
164};
165
d65d8abb
MD
166/*
167 * enum_val_equal assumes that signed and unsigned memory layout overlap.
168 */
169struct enum_range {
170 union {
171 int64_t _signed;
172 uint64_t _unsigned;
173 } start; /* lowest range value */
174 union {
175 int64_t _signed;
176 uint64_t _unsigned;
177 } end; /* highest range value */
178};
179
180struct enum_range_to_quark {
181 struct cds_list_head node;
182 struct enum_range range;
183 GQuark quark;
184};
185
186/*
187 * We optimize the common case (range of size 1: single value) by creating a
188 * hash table mapping values to quark sets. We then lookup the ranges to
189 * complete the quark set.
190 *
191 * TODO: The proper structure to hold the range to quark set mapping would be an
192 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
193 * time. Using a simple O(n) list search for now for implementation speed and
194 * given that we can expect to have a _relatively_ small number of enumeration
195 * ranges. This might become untrue if we are fed with symbol tables often
196 * required to lookup function names from instruction pointer value.
197 */
448d3cc7 198struct enum_table {
d65d8abb
MD
199 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
200 struct cds_list_head range_to_quark; /* (range, GQuark) */
201 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
202};
203
e19c3d69
MD
204struct type_enum {
205 struct type p;
206 struct type_integer *integer_type;
448d3cc7 207 struct enum_table table;
fc93b2bd
MD
208};
209
e19c3d69
MD
210struct declaration_enum {
211 struct declaration p;
212 struct declaration_integer *integer;
213 struct type_enum *type;
c054553d
MD
214 /* Last GQuark values read. Keeping a reference on the GQuark array. */
215 GArray *value;
216};
217
c054553d
MD
218struct type_string {
219 struct type p;
c054553d
MD
220};
221
e19c3d69
MD
222struct declaration_string {
223 struct declaration p;
224 struct type_string *type;
225 char *value; /* freed at declaration_string teardown */
11796b96
MD
226};
227
e19c3d69
MD
228struct type_field {
229 GQuark name;
c054553d
MD
230 struct type *type;
231};
232
e19c3d69 233struct field {
ac88af75 234 GQuark name;
6ee5115e 235 struct declaration *declaration;
c054553d
MD
236};
237
238struct type_struct {
239 struct type p;
e19c3d69
MD
240 GHashTable *fields_by_name; /* Tuples (field name, field index) */
241 GArray *fields; /* Array of type_field */
242};
243
244struct declaration_struct {
245 struct declaration p;
246 struct type_struct *type;
c054553d
MD
247 struct declaration_scope *scope;
248 GArray *fields; /* Array of struct field */
249};
250
e19c3d69
MD
251struct type_variant {
252 struct type p;
c054553d 253 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
e19c3d69 254 GArray *fields; /* Array of type_field */
c054553d
MD
255};
256
e19c3d69
MD
257struct declaration_variant {
258 struct declaration p;
259 struct type_variant *type;
c054553d 260 struct declaration_scope *scope;
6ee5115e 261 struct declaration *enum_tag;
c054553d
MD
262 GArray *fields; /* Array of struct field */
263 struct field *current_field; /* Last field read */
11796b96
MD
264};
265
e19c3d69
MD
266struct type_array {
267 struct type p;
11796b96 268 size_t len;
e19c3d69 269 struct type *elem;
11796b96
MD
270};
271
e19c3d69
MD
272struct declaration_array {
273 struct declaration p;
274 struct type_array *type;
c054553d
MD
275 struct declaration_scope *scope;
276 struct field current_element; /* struct field */
277};
278
c054553d
MD
279struct type_sequence {
280 struct type p;
e19c3d69
MD
281 struct type_integer *len_type;
282 struct type *elem;
283};
284
285struct declaration_sequence {
286 struct declaration p;
287 struct type_sequence *type;
c054553d 288 struct declaration_scope *scope;
e19c3d69 289 struct declaration_integer *len;
c054553d
MD
290 struct field current_element; /* struct field */
291};
292
ac88af75 293struct type *lookup_type(GQuark type_name, struct declaration_scope *scope);
e19c3d69 294int register_type(struct type *type, struct declaration_scope *scope);
c054553d 295
ac88af75
MD
296struct declaration *
297 lookup_declaration(GQuark field_name, struct declaration_scope *scope);
298int register_declaration(GQuark field_name, struct declaration *declaration,
299 struct declaration_scope *scope);
300
e19c3d69
MD
301void type_ref(struct type *type);
302void type_unref(struct type *type);
c054553d
MD
303
304struct declaration_scope *
e19c3d69 305 new_declaration_scope(struct declaration_scope *parent_scope);
c054553d 306void free_declaration_scope(struct declaration_scope *scope);
4c8bfb7e 307
e19c3d69
MD
308void declaration_ref(struct declaration *declaration);
309void declaration_unref(struct declaration *declaration);
698f0fe4 310
0a46062b
MD
311/* Nameless types can be created by passing a NULL name */
312
e19c3d69
MD
313struct type_integer *integer_type_new(const char *name,
314 size_t len, int byte_order,
315 int signedness, size_t alignment);
0a46062b 316
11d43b90
MD
317/*
318 * mantissa_len is the length of the number of bytes represented by the mantissa
319 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
320 */
e19c3d69
MD
321struct type_float *float_type_new(const char *name,
322 size_t mantissa_len,
323 size_t exp_len, int byte_order,
324 size_t alignment);
0a46062b 325
448d3cc7
MD
326/*
327 * A GQuark can be translated to/from strings with g_quark_from_string() and
328 * g_quark_to_string().
329 */
47e0f2e2
MD
330
331/*
332 * Returns a GArray of GQuark or NULL.
333 * Caller must release the GArray with g_array_unref().
334 */
e19c3d69 335GArray *enum_uint_to_quark_set(const struct type_enum *enum_type, uint64_t v);
d65d8abb
MD
336
337/*
47e0f2e2 338 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
339 * Caller must release the GArray with g_array_unref().
340 */
e19c3d69 341GArray *enum_int_to_quark_set(const struct type_enum *enum_type, uint64_t v);
47e0f2e2
MD
342
343/*
344 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
345 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
346 * release it).
47e0f2e2 347 */
e19c3d69
MD
348GArray *enum_quark_to_range_set(const struct type_enum *enum_type, GQuark q);
349void enum_signed_insert(struct type_enum *enum_type,
d65d8abb 350 int64_t start, int64_t end, GQuark q);
e19c3d69 351void enum_unsigned_insert(struct type_enum *enum_type,
d65d8abb 352 uint64_t start, uint64_t end, GQuark q);
e19c3d69 353size_t enum_get_nr_enumerators(struct type_enum *enum_type);
448d3cc7 354
e19c3d69
MD
355struct type_enum *enum_type_new(const char *name,
356 struct type_integer *integer_type);
448d3cc7 357
e19c3d69
MD
358struct type_struct *struct_type_new(const char *name);
359void struct_type_add_field(struct type_struct *struct_type,
360 const char *field_name, struct type *field_type);
11796b96
MD
361/*
362 * Returns the index of a field within a structure.
363 */
e19c3d69
MD
364unsigned long struct_type_lookup_field_index(struct type_struct *struct_type,
365 GQuark field_name);
11796b96
MD
366/*
367 * field returned only valid as long as the field structure is not appended to.
368 */
e19c3d69
MD
369struct type_field *
370struct_type_get_field_from_index(struct type_struct *struct_type,
11796b96 371 unsigned long index);
e19c3d69
MD
372struct field *
373struct_get_field_from_index(struct declaration_struct *struct_declaration,
374 unsigned long index);
11796b96 375
c054553d
MD
376/*
377 * The tag enumeration is validated to ensure that it contains only mappings
378 * from numeric values to a single tag. Overlapping tag value ranges are
379 * therefore forbidden.
380 */
e19c3d69
MD
381struct type_variant *variant_type_new(const char *name);
382void variant_type_add_field(struct type_variant *variant_type,
383 const char *tag_name, struct type *tag_type);
384struct type_field *
385variant_type_get_field_from_tag(struct type_variant *variant_type, GQuark tag);
c054553d
MD
386/*
387 * Returns 0 on success, -EPERM on error.
388 */
e19c3d69
MD
389int variant_declaration_set_tag(struct declaration_variant *variant,
390 struct declaration *enum_tag);
c054553d
MD
391/*
392 * Returns the field selected by the current tag value.
393 * field returned only valid as long as the variant structure is not appended
394 * to.
395 */
396struct field *
e19c3d69 397variant_get_current_field(struct declaration_variant *variant);
c054553d 398
d06d03db 399/*
e19c3d69 400 * elem_type passed as parameter now belongs to the array. No need to free it
c054553d 401 * explicitly. "len" is the number of elements in the array.
d06d03db 402 */
e19c3d69
MD
403struct type_array *array_type_new(const char *name,
404 size_t len, struct type *elem_type);
11796b96 405
d06d03db 406/*
e19c3d69 407 * int_type and elem_type passed as parameter now belong to the sequence. No
c054553d 408 * need to free them explicitly.
d06d03db 409 */
e19c3d69
MD
410struct type_sequence *sequence_type_new(const char *name,
411 struct type_integer *len_type,
412 struct type *elem_type);
11796b96 413
d79865b9 414#endif /* _BABELTRACE_TYPES_H */
This page took 0.041473 seconds and 4 git commands to generate.