Update typealias behavior
[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;
e1151715 88struct definition;
4c8bfb7e 89
64893f33 90/* type scope */
f6625916 91struct declaration_scope {
c13cbf74 92 /* Hash table mapping type name GQuark to "struct declaration" */
1ee8e81d 93 /* Used for both typedef and typealias. */
f6625916
MD
94 GHashTable *typedef_declarations;
95 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
96 GHashTable *struct_declarations;
97 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
98 GHashTable *variant_declarations;
c13cbf74 99 /* Hash table mapping enum name GQuark to "struct type_enum" */
f6625916
MD
100 GHashTable *enum_declarations;
101 struct declaration_scope *parent_scope;
64893f33
MD
102};
103
e1151715
MD
104/* definition scope */
105struct definition_scope {
106 /* Hash table mapping field name GQuark to "struct definition" */
107 GHashTable *definitions;
108 struct definition_scope *parent_scope;
05c749e5
MD
109 /*
110 * Complete "path" leading to this definition scope.
9e29e16e 111 * Includes dynamic scope name '.' field name '.' field name '.' ....
05c749e5 112 * Array of GQuark elements (which are each separated by dots).
9e29e16e
MD
113 * The dynamic scope name can contain dots, and is encoded into
114 * a single GQuark. Thus, scope_path[0] returns the GQuark
115 * identifying the dynamic scope.
05c749e5
MD
116 */
117 GArray *scope_path; /* array of GQuark */
6b71274a
MD
118};
119
05628561
MD
120enum ctf_type_id {
121 CTF_TYPE_UNKNOWN = 0,
122 CTF_TYPE_INTEGER,
123 CTF_TYPE_FLOAT,
124 CTF_TYPE_ENUM,
125 CTF_TYPE_STRING,
126 CTF_TYPE_STRUCT,
d60cb676 127 CTF_TYPE_UNTAGGED_VARIANT,
05628561
MD
128 CTF_TYPE_VARIANT,
129 CTF_TYPE_ARRAY,
130 CTF_TYPE_SEQUENCE,
131 NR_CTF_TYPES,
132};
133
f6625916 134struct declaration {
05628561 135 enum ctf_type_id id;
fc93b2bd
MD
136 GQuark name; /* type name */
137 size_t alignment; /* type alignment, in bits */
e19c3d69 138 int ref; /* number of references to the type */
c054553d 139 /*
f6625916 140 * declaration_free called with declaration ref is decremented to 0.
c054553d 141 */
f6625916 142 void (*declaration_free)(struct declaration *declaration);
e1151715 143 struct definition *
f6625916 144 (*definition_new)(struct declaration *declaration,
05c749e5
MD
145 struct definition_scope *parent_scope,
146 GQuark field_name, int index);
c054553d 147 /*
e1151715 148 * definition_free called with definition ref is decremented to 0.
e19c3d69 149 */
e1151715 150 void (*definition_free)(struct definition *definition);
e19c3d69 151 /*
e1151715
MD
152 * Definition copy function. Knows how to find the child
153 * definition from the parent definition.
fc93b2bd 154 */
4c8bfb7e
MD
155 void (*copy)(struct stream_pos *dest, const struct format *fdest,
156 struct stream_pos *src, const struct format *fsrc,
e1151715 157 struct definition *definition);
c054553d
MD
158};
159
e1151715 160struct definition {
f6625916 161 struct declaration *declaration;
05c749e5 162 int index; /* Position of the definition in its container */
e1151715 163 int ref; /* number of references to the definition */
fc93b2bd
MD
164};
165
bed864a7
MD
166/*
167 * Because we address in bits, bitfields end up being exactly the same as
168 * integers, except that their read/write functions must be able to deal with
169 * read/write non aligned on CHAR_BIT.
170 */
f6625916
MD
171struct declaration_integer {
172 struct declaration p;
7fe00194
MD
173 size_t len; /* length, in bits. */
174 int byte_order; /* byte order */
175 int signedness;
fc93b2bd
MD
176};
177
e1151715
MD
178struct definition_integer {
179 struct definition p;
f6625916 180 struct declaration_integer *declaration;
c054553d
MD
181 /* Last values read */
182 union {
183 uint64_t _unsigned;
184 int64_t _signed;
185 } value;
186};
187
f6625916
MD
188struct declaration_float {
189 struct declaration p;
190 struct declaration_integer *sign;
191 struct declaration_integer *mantissa;
192 struct declaration_integer *exp;
fc93b2bd 193 int byte_order;
0a46062b 194 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
195};
196
e1151715
MD
197struct definition_float {
198 struct definition p;
f6625916 199 struct declaration_float *declaration;
c054553d
MD
200 /* Last values read */
201 long double value;
202};
203
d65d8abb
MD
204/*
205 * enum_val_equal assumes that signed and unsigned memory layout overlap.
206 */
207struct enum_range {
208 union {
209 int64_t _signed;
210 uint64_t _unsigned;
211 } start; /* lowest range value */
212 union {
213 int64_t _signed;
214 uint64_t _unsigned;
215 } end; /* highest range value */
216};
217
218struct enum_range_to_quark {
219 struct cds_list_head node;
220 struct enum_range range;
221 GQuark quark;
222};
223
224/*
225 * We optimize the common case (range of size 1: single value) by creating a
226 * hash table mapping values to quark sets. We then lookup the ranges to
227 * complete the quark set.
228 *
229 * TODO: The proper structure to hold the range to quark set mapping would be an
230 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
231 * time. Using a simple O(n) list search for now for implementation speed and
232 * given that we can expect to have a _relatively_ small number of enumeration
233 * ranges. This might become untrue if we are fed with symbol tables often
234 * required to lookup function names from instruction pointer value.
235 */
448d3cc7 236struct enum_table {
d65d8abb
MD
237 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
238 struct cds_list_head range_to_quark; /* (range, GQuark) */
239 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
240};
241
f6625916
MD
242struct declaration_enum {
243 struct declaration p;
244 struct declaration_integer *integer_declaration;
448d3cc7 245 struct enum_table table;
fc93b2bd
MD
246};
247
e1151715
MD
248struct definition_enum {
249 struct definition p;
250 struct definition_integer *integer;
f6625916 251 struct declaration_enum *declaration;
c054553d
MD
252 /* Last GQuark values read. Keeping a reference on the GQuark array. */
253 GArray *value;
254};
255
f6625916
MD
256struct declaration_string {
257 struct declaration p;
c054553d
MD
258};
259
e1151715
MD
260struct definition_string {
261 struct definition p;
f6625916 262 struct declaration_string *declaration;
e1151715 263 char *value; /* freed at definition_string teardown */
11796b96
MD
264};
265
f6625916 266struct declaration_field {
e19c3d69 267 GQuark name;
f6625916 268 struct declaration *declaration;
c054553d
MD
269};
270
e19c3d69 271struct field {
ac88af75 272 GQuark name;
e1151715 273 struct definition *definition;
c054553d
MD
274};
275
f6625916
MD
276struct declaration_struct {
277 struct declaration p;
e19c3d69 278 GHashTable *fields_by_name; /* Tuples (field name, field index) */
f6625916
MD
279 struct declaration_scope *scope;
280 GArray *fields; /* Array of declaration_field */
e19c3d69
MD
281};
282
e1151715
MD
283struct definition_struct {
284 struct definition p;
f6625916 285 struct declaration_struct *declaration;
e1151715 286 struct definition_scope *scope;
c054553d
MD
287 GArray *fields; /* Array of struct field */
288};
289
d60cb676 290struct declaration_untagged_variant {
f6625916 291 struct declaration p;
c054553d 292 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
f6625916
MD
293 struct declaration_scope *scope;
294 GArray *fields; /* Array of declaration_field */
c054553d
MD
295};
296
d60cb676
MD
297struct declaration_variant {
298 struct declaration p;
299 struct declaration_untagged_variant *untagged_variant;
300 GArray *tag_name; /* Array of GQuark */
301};
302
303/* A variant needs to be tagged to be defined. */
e1151715
MD
304struct definition_variant {
305 struct definition p;
f6625916 306 struct declaration_variant *declaration;
e1151715
MD
307 struct definition_scope *scope;
308 struct definition *enum_tag;
c054553d
MD
309 GArray *fields; /* Array of struct field */
310 struct field *current_field; /* Last field read */
11796b96
MD
311};
312
f6625916
MD
313struct declaration_array {
314 struct declaration p;
11796b96 315 size_t len;
f6625916
MD
316 struct declaration *elem;
317 struct declaration_scope *scope;
11796b96
MD
318};
319
e1151715
MD
320struct definition_array {
321 struct definition p;
f6625916 322 struct declaration_array *declaration;
e1151715 323 struct definition_scope *scope;
c054553d
MD
324 struct field current_element; /* struct field */
325};
326
f6625916
MD
327struct declaration_sequence {
328 struct declaration p;
329 struct declaration_integer *len_declaration;
330 struct declaration *elem;
331 struct declaration_scope *scope;
e19c3d69
MD
332};
333
e1151715
MD
334struct definition_sequence {
335 struct definition p;
f6625916 336 struct declaration_sequence *declaration;
e1151715
MD
337 struct definition_scope *scope;
338 struct definition_integer *len;
c054553d
MD
339 struct field current_element; /* struct field */
340};
341
f6625916
MD
342int register_declaration(GQuark declaration_name,
343 struct declaration *declaration,
344 struct declaration_scope *scope);
345struct declaration *lookup_declaration(GQuark declaration_name,
346 struct declaration_scope *scope);
c13cbf74
MD
347
348/*
349 * Type scopes also contain a separate registry for struct, variant and
e1151715 350 * enum types. Those register types rather than type definitions, so
c13cbf74
MD
351 * that a named variant can be declared without specifying its target
352 * "choice" tag field immediately.
353 */
f6625916
MD
354int register_struct_declaration(GQuark struct_name,
355 struct declaration_struct *struct_declaration,
356 struct declaration_scope *scope);
357struct declaration_struct *
358 lookup_struct_declaration(GQuark struct_name,
359 struct declaration_scope *scope);
360int register_variant_declaration(GQuark variant_name,
361 struct declaration_variant *variant_declaration,
362 struct declaration_scope *scope);
363struct declaration_variant *lookup_variant_declaration(GQuark variant_name,
364 struct declaration_scope *scope);
365int register_enum_declaration(GQuark enum_name,
366 struct declaration_enum *enum_declaration,
367 struct declaration_scope *scope);
368struct declaration_enum *
369 lookup_enum_declaration(GQuark enum_name,
370 struct declaration_scope *scope);
371
372struct declaration_scope *
373 new_declaration_scope(struct declaration_scope *parent_scope);
374void free_declaration_scope(struct declaration_scope *scope);
c054553d 375
c13cbf74 376/*
e1151715
MD
377 * field_definition is for field definitions. They are registered into
378 * definition scopes.
c13cbf74 379 */
e1151715 380struct definition *
05c749e5
MD
381 lookup_definition(GArray *cur_path, /* array of GQuark */
382 GArray *lookup_path, /* array of GQuark */
383 struct definition_scope *scope);
e1151715
MD
384int register_field_definition(GQuark field_name,
385 struct definition *definition,
386 struct definition_scope *scope);
387struct definition_scope *
05c749e5
MD
388 new_definition_scope(struct definition_scope *parent_scope,
389 GQuark field_name);
6a36ddca
MD
390void set_dynamic_definition_scope(struct definition *definition,
391 struct definition_scope *scope,
d00d17d1 392 const char *root_name);
e1151715 393void free_definition_scope(struct definition_scope *scope);
4c8bfb7e 394
f6625916
MD
395void declaration_ref(struct declaration *declaration);
396void declaration_unref(struct declaration *declaration);
64893f33 397
e1151715
MD
398void definition_ref(struct definition *definition);
399void definition_unref(struct definition *definition);
698f0fe4 400
f6625916 401/* Nameless declarations can be created by passing a NULL name */
0a46062b 402
f6625916 403struct declaration_integer *integer_declaration_new(const char *name,
e19c3d69
MD
404 size_t len, int byte_order,
405 int signedness, size_t alignment);
0a46062b 406
11d43b90
MD
407/*
408 * mantissa_len is the length of the number of bytes represented by the mantissa
409 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
410 */
f6625916 411struct declaration_float *float_declaration_new(const char *name,
e19c3d69
MD
412 size_t mantissa_len,
413 size_t exp_len, int byte_order,
414 size_t alignment);
0a46062b 415
448d3cc7
MD
416/*
417 * A GQuark can be translated to/from strings with g_quark_from_string() and
418 * g_quark_to_string().
419 */
47e0f2e2
MD
420
421/*
422 * Returns a GArray of GQuark or NULL.
423 * Caller must release the GArray with g_array_unref().
424 */
f6625916
MD
425GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
426 uint64_t v);
d65d8abb
MD
427
428/*
47e0f2e2 429 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
430 * Caller must release the GArray with g_array_unref().
431 */
f6625916
MD
432GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
433 uint64_t v);
47e0f2e2
MD
434
435/*
436 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
437 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
438 * release it).
47e0f2e2 439 */
f6625916
MD
440GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
441 GQuark q);
442void enum_signed_insert(struct declaration_enum *enum_declaration,
d65d8abb 443 int64_t start, int64_t end, GQuark q);
f6625916 444void enum_unsigned_insert(struct declaration_enum *enum_declaration,
d65d8abb 445 uint64_t start, uint64_t end, GQuark q);
f6625916
MD
446size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration);
447
448struct declaration_enum *
449 enum_declaration_new(const char *name,
450 struct declaration_integer *integer_declaration);
451
452struct declaration_struct *
453 struct_declaration_new(const char *name,
454 struct declaration_scope *parent_scope);
455void struct_declaration_add_field(struct declaration_struct *struct_declaration,
456 const char *field_name,
457 struct declaration *field_declaration);
11796b96
MD
458/*
459 * Returns the index of a field within a structure.
460 */
f6625916
MD
461unsigned long struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
462 GQuark field_name);
11796b96
MD
463/*
464 * field returned only valid as long as the field structure is not appended to.
465 */
f6625916
MD
466struct declaration_field *
467struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
468 unsigned long index);
e19c3d69 469struct field *
e1151715 470struct_get_field_from_index(struct definition_struct *struct_definition,
e19c3d69 471 unsigned long index);
11796b96 472
c054553d
MD
473/*
474 * The tag enumeration is validated to ensure that it contains only mappings
475 * from numeric values to a single tag. Overlapping tag value ranges are
476 * therefore forbidden.
477 */
d60cb676 478struct declaration_untagged_variant *untagged_variant_declaration_new(const char *name,
1934b94f 479 struct declaration_scope *parent_scope);
d60cb676
MD
480struct declaration_variant *variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
481 const char *tag);
482
483void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
484 const char *field_name,
485 struct declaration *field_declaration);
f6625916 486struct declaration_field *
d60cb676 487 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
1934b94f 488 GQuark tag);
c054553d
MD
489/*
490 * Returns 0 on success, -EPERM on error.
491 */
e1151715
MD
492int variant_definition_set_tag(struct definition_variant *variant,
493 struct definition *enum_tag);
c054553d
MD
494/*
495 * Returns the field selected by the current tag value.
496 * field returned only valid as long as the variant structure is not appended
497 * to.
498 */
f6625916 499struct field *variant_get_current_field(struct definition_variant *variant);
c054553d 500
d06d03db 501/*
f6625916
MD
502 * elem_declaration passed as parameter now belongs to the array. No
503 * need to free it explicitly. "len" is the number of elements in the
504 * array.
d06d03db 505 */
f6625916
MD
506struct declaration_array *
507 array_declaration_new(const char *name,
1934b94f
MD
508 size_t len, struct declaration *elem_declaration,
509 struct declaration_scope *parent_scope);
11796b96 510
d06d03db 511/*
f6625916
MD
512 * int_declaration and elem_declaration passed as parameter now belong
513 * to the sequence. No need to free them explicitly.
d06d03db 514 */
f6625916
MD
515struct declaration_sequence *
516 sequence_declaration_new(const char *name,
1934b94f
MD
517 struct declaration_integer *len_declaration,
518 struct declaration *elem_declaration,
519 struct declaration_scope *parent_scope);
11796b96 520
d60cb676
MD
521/*
522 * in: path (dot separated), out: q (GArray of GQuark)
523 */
524void append_scope_path(const char *path, GArray *q);
525
f6625916 526#endif /* _BABELTRACE_declarationS_H */
This page took 0.048151 seconds and 4 git commands to generate.