typealias: Fix reduce conflict in grammar for abstract declarators
[babeltrace.git] / include / babeltrace / types.h
... / ...
CommitLineData
1#ifndef _BABELTRACE_TYPES_H
2#define _BABELTRACE_TYPES_H
3
4/*
5 * BabelTrace
6 *
7 * Type Header
8 *
9 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22#include <babeltrace/align.h>
23#include <babeltrace/list.h>
24#include <stdbool.h>
25#include <stdint.h>
26#include <limits.h>
27#include <string.h>
28#include <glib.h>
29#include <assert.h>
30
31/* Preallocate this many fields for structures */
32#define DEFAULT_NR_STRUCT_FIELDS 8
33
34/*
35 * Always update stream_pos with move_pos and init_pos.
36 */
37struct stream_pos {
38 char *base; /* Base address */
39 size_t offset; /* Offset from base, in bits */
40 int dummy; /* Dummy position, for length calculation */
41};
42
43static inline
44void init_pos(struct stream_pos *pos, char *base)
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
80char *get_pos_addr(struct stream_pos *pos)
81{
82 /* Only makes sense to get the address after aligning on CHAR_BIT */
83 assert(!(pos->offset % CHAR_BIT));
84 return pos->base + (pos->offset / CHAR_BIT);
85}
86
87struct format;
88struct definition;
89
90/* type scope */
91struct declaration_scope {
92 /* Hash table mapping type name GQuark to "struct declaration" */
93 /* Used for both typedef and typealias. */
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;
99 /* Hash table mapping enum name GQuark to "struct type_enum" */
100 GHashTable *enum_declarations;
101 struct declaration_scope *parent_scope;
102};
103
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;
109 /*
110 * Complete "path" leading to this definition scope.
111 * Includes dynamic scope name '.' field name '.' field name '.' ....
112 * Array of GQuark elements (which are each separated by dots).
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.
116 */
117 GArray *scope_path; /* array of GQuark */
118};
119
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,
127 CTF_TYPE_UNTAGGED_VARIANT,
128 CTF_TYPE_VARIANT,
129 CTF_TYPE_ARRAY,
130 CTF_TYPE_SEQUENCE,
131 NR_CTF_TYPES,
132};
133
134struct declaration {
135 enum ctf_type_id id;
136 GQuark name; /* type name */
137 size_t alignment; /* type alignment, in bits */
138 int ref; /* number of references to the type */
139 /*
140 * declaration_free called with declaration ref is decremented to 0.
141 */
142 void (*declaration_free)(struct declaration *declaration);
143 struct definition *
144 (*definition_new)(struct declaration *declaration,
145 struct definition_scope *parent_scope,
146 GQuark field_name, int index);
147 /*
148 * definition_free called with definition ref is decremented to 0.
149 */
150 void (*definition_free)(struct definition *definition);
151 /*
152 * Definition copy function. Knows how to find the child
153 * definition from the parent definition.
154 */
155 void (*copy)(struct stream_pos *dest, const struct format *fdest,
156 struct stream_pos *src, const struct format *fsrc,
157 struct definition *definition);
158};
159
160struct definition {
161 struct declaration *declaration;
162 int index; /* Position of the definition in its container */
163 int ref; /* number of references to the definition */
164};
165
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 */
171struct declaration_integer {
172 struct declaration p;
173 size_t len; /* length, in bits. */
174 int byte_order; /* byte order */
175 int signedness;
176};
177
178struct definition_integer {
179 struct definition p;
180 struct declaration_integer *declaration;
181 /* Last values read */
182 union {
183 uint64_t _unsigned;
184 int64_t _signed;
185 } value;
186};
187
188struct declaration_float {
189 struct declaration p;
190 struct declaration_integer *sign;
191 struct declaration_integer *mantissa;
192 struct declaration_integer *exp;
193 int byte_order;
194 /* TODO: we might want to express more info about NaN, +inf and -inf */
195};
196
197struct definition_float {
198 struct definition p;
199 struct declaration_float *declaration;
200 /* Last values read */
201 long double value;
202};
203
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 */
236struct enum_table {
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) */
240};
241
242struct declaration_enum {
243 struct declaration p;
244 struct declaration_integer *integer_declaration;
245 struct enum_table table;
246};
247
248struct definition_enum {
249 struct definition p;
250 struct definition_integer *integer;
251 struct declaration_enum *declaration;
252 /* Last GQuark values read. Keeping a reference on the GQuark array. */
253 GArray *value;
254};
255
256struct declaration_string {
257 struct declaration p;
258};
259
260struct definition_string {
261 struct definition p;
262 struct declaration_string *declaration;
263 char *value; /* freed at definition_string teardown */
264};
265
266struct declaration_field {
267 GQuark name;
268 struct declaration *declaration;
269};
270
271struct field {
272 GQuark name;
273 struct definition *definition;
274};
275
276struct declaration_struct {
277 struct declaration p;
278 GHashTable *fields_by_name; /* Tuples (field name, field index) */
279 struct declaration_scope *scope;
280 GArray *fields; /* Array of declaration_field */
281};
282
283struct definition_struct {
284 struct definition p;
285 struct declaration_struct *declaration;
286 struct definition_scope *scope;
287 GArray *fields; /* Array of struct field */
288};
289
290struct declaration_untagged_variant {
291 struct declaration p;
292 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
293 struct declaration_scope *scope;
294 GArray *fields; /* Array of declaration_field */
295};
296
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. */
304struct definition_variant {
305 struct definition p;
306 struct declaration_variant *declaration;
307 struct definition_scope *scope;
308 struct definition *enum_tag;
309 GArray *fields; /* Array of struct field */
310 struct field *current_field; /* Last field read */
311};
312
313struct declaration_array {
314 struct declaration p;
315 size_t len;
316 struct declaration *elem;
317 struct declaration_scope *scope;
318};
319
320struct definition_array {
321 struct definition p;
322 struct declaration_array *declaration;
323 struct definition_scope *scope;
324 struct field current_element; /* struct field */
325};
326
327struct declaration_sequence {
328 struct declaration p;
329 struct declaration_integer *len_declaration;
330 struct declaration *elem;
331 struct declaration_scope *scope;
332};
333
334struct definition_sequence {
335 struct definition p;
336 struct declaration_sequence *declaration;
337 struct definition_scope *scope;
338 struct definition_integer *len;
339 struct field current_element; /* struct field */
340};
341
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);
347
348/*
349 * Type scopes also contain a separate registry for struct, variant and
350 * enum types. Those register types rather than type definitions, so
351 * that a named variant can be declared without specifying its target
352 * "choice" tag field immediately.
353 */
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);
375
376/*
377 * field_definition is for field definitions. They are registered into
378 * definition scopes.
379 */
380struct definition *
381 lookup_definition(GArray *cur_path, /* array of GQuark */
382 GArray *lookup_path, /* array of GQuark */
383 struct definition_scope *scope);
384int register_field_definition(GQuark field_name,
385 struct definition *definition,
386 struct definition_scope *scope);
387struct definition_scope *
388 new_definition_scope(struct definition_scope *parent_scope,
389 GQuark field_name);
390void set_dynamic_definition_scope(struct definition *definition,
391 struct definition_scope *scope,
392 const char *root_name);
393void free_definition_scope(struct definition_scope *scope);
394
395void declaration_ref(struct declaration *declaration);
396void declaration_unref(struct declaration *declaration);
397
398void definition_ref(struct definition *definition);
399void definition_unref(struct definition *definition);
400
401struct declaration_integer *integer_declaration_new(size_t len, int byte_order,
402 int signedness, size_t alignment);
403
404/*
405 * mantissa_len is the length of the number of bytes represented by the mantissa
406 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
407 */
408struct declaration_float *float_declaration_new(size_t mantissa_len,
409 size_t exp_len, int byte_order,
410 size_t alignment);
411
412/*
413 * A GQuark can be translated to/from strings with g_quark_from_string() and
414 * g_quark_to_string().
415 */
416
417/*
418 * Returns a GArray of GQuark or NULL.
419 * Caller must release the GArray with g_array_unref().
420 */
421GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
422 uint64_t v);
423
424/*
425 * Returns a GArray of GQuark or NULL.
426 * Caller must release the GArray with g_array_unref().
427 */
428GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
429 uint64_t v);
430
431/*
432 * Returns a GArray of struct enum_range or NULL.
433 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
434 * release it).
435 */
436GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
437 GQuark q);
438void enum_signed_insert(struct declaration_enum *enum_declaration,
439 int64_t start, int64_t end, GQuark q);
440void enum_unsigned_insert(struct declaration_enum *enum_declaration,
441 uint64_t start, uint64_t end, GQuark q);
442size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration);
443
444struct declaration_enum *
445 enum_declaration_new(const char *name,
446 struct declaration_integer *integer_declaration);
447
448struct declaration_struct *
449 struct_declaration_new(const char *name,
450 struct declaration_scope *parent_scope);
451void struct_declaration_add_field(struct declaration_struct *struct_declaration,
452 const char *field_name,
453 struct declaration *field_declaration);
454/*
455 * Returns the index of a field within a structure.
456 */
457unsigned long struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
458 GQuark field_name);
459/*
460 * field returned only valid as long as the field structure is not appended to.
461 */
462struct declaration_field *
463struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
464 unsigned long index);
465struct field *
466struct_get_field_from_index(struct definition_struct *struct_definition,
467 unsigned long index);
468
469/*
470 * The tag enumeration is validated to ensure that it contains only mappings
471 * from numeric values to a single tag. Overlapping tag value ranges are
472 * therefore forbidden.
473 */
474struct declaration_untagged_variant *untagged_variant_declaration_new(const char *name,
475 struct declaration_scope *parent_scope);
476struct declaration_variant *variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
477 const char *tag);
478
479void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
480 const char *field_name,
481 struct declaration *field_declaration);
482struct declaration_field *
483 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
484 GQuark tag);
485/*
486 * Returns 0 on success, -EPERM on error.
487 */
488int variant_definition_set_tag(struct definition_variant *variant,
489 struct definition *enum_tag);
490/*
491 * Returns the field selected by the current tag value.
492 * field returned only valid as long as the variant structure is not appended
493 * to.
494 */
495struct field *variant_get_current_field(struct definition_variant *variant);
496
497/*
498 * elem_declaration passed as parameter now belongs to the array. No
499 * need to free it explicitly. "len" is the number of elements in the
500 * array.
501 */
502struct declaration_array *
503 array_declaration_new(const char *name,
504 size_t len, struct declaration *elem_declaration,
505 struct declaration_scope *parent_scope);
506
507/*
508 * int_declaration and elem_declaration passed as parameter now belong
509 * to the sequence. No need to free them explicitly.
510 */
511struct declaration_sequence *
512 sequence_declaration_new(const char *name,
513 struct declaration_integer *len_declaration,
514 struct declaration *elem_declaration,
515 struct declaration_scope *parent_scope);
516
517/*
518 * in: path (dot separated), out: q (GArray of GQuark)
519 */
520void append_scope_path(const char *path, GArray *q);
521
522#endif /* _BABELTRACE_declarationS_H */
This page took 0.023608 seconds and 4 git commands to generate.