Imported Upstream version 0.7.2
[platform/upstream/ltrace.git] / type.h
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4  * Copyright (C) 1997-2009 Juan Cespedes
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  */
21
22 #ifndef TYPE_H
23 #define TYPE_H
24
25 #include <stddef.h>
26 #include "forward.h"
27 #include "vect.h"
28
29 enum arg_type {
30         ARGTYPE_VOID,
31         ARGTYPE_INT,
32         ARGTYPE_UINT,
33         ARGTYPE_LONG,
34         ARGTYPE_ULONG,
35         ARGTYPE_CHAR,
36         ARGTYPE_SHORT,
37         ARGTYPE_USHORT,
38         ARGTYPE_FLOAT,
39         ARGTYPE_DOUBLE,
40         ARGTYPE_ARRAY,          /* Series of values in memory */
41         ARGTYPE_STRUCT,         /* Structure of values */
42         ARGTYPE_POINTER,        /* Pointer to some other type */
43 };
44
45 struct arg_type_info {
46         enum arg_type type;
47         union {
48                 struct vect entries;
49
50                 /* ARGTYPE_ARRAY */
51                 struct {
52                         struct arg_type_info *elt_type;
53                         struct expr_node *length;
54                         int own_info:1;
55                         int own_length:1;
56                 } array_info;
57
58                 /* ARGTYPE_POINTER */
59                 struct {
60                         struct arg_type_info *info;
61                         int own_info:1;
62                 } ptr_info;
63         } u;
64
65         struct lens *lens;
66         int own_lens;
67 };
68
69 /* Return a type info for simple type TYPE (which shall not be array,
70  * struct, or pointer.  Each call with the same TYPE yields the same
71  * arg_type_info pointer.  */
72 struct arg_type_info *type_get_simple(enum arg_type type);
73
74 /* Initialize INFO so it becomes ARGTYPE_STRUCT.  The created
75  * structure contains no fields.  Use type_struct_add to populate the
76  * structure.  */
77 void type_init_struct(struct arg_type_info *info);
78
79 /* Add a new field of type FIELD_INFO to a structure INFO.  If OWN,
80  * the field type is owned and destroyed together with INFO.  */
81 int type_struct_add(struct arg_type_info *info,
82                     struct arg_type_info *field_info, int own);
83
84 /* Get IDX-th field of structure type INFO.  */
85 struct arg_type_info *type_struct_get(struct arg_type_info *info, size_t idx);
86
87 /* Return number of fields of structure type INFO.  */
88 size_t type_struct_size(struct arg_type_info *info);
89
90 /* Return number of elements of an aggregate type INFO.  This can be
91  * either ARGTYPE_STRUCT or ARGTYPE_ARRAY of constant length.  If
92  * ARGTYPE_ARRAY does not have a constant length, this returns -1.  */
93 size_t type_aggregate_size(struct arg_type_info *info);
94
95 /* Initialize INFO so it becomes ARGTYPE_ARRAY.  The element type is
96  * passed in ELEMENT_INFO, and array length in LENGTH_EXPR.  If,
97  * respectively, OWN_INFO and OWN_LENGTH are true, the pointee and
98  * length are owned and destroyed together with INFO.  */
99 void type_init_array(struct arg_type_info *info,
100                      struct arg_type_info *element_info, int own_info,
101                      struct expr_node *length_expr, int own_length);
102
103 /* Initialize INFO so it becomes ARGTYPE_POINTER.  The pointee type is
104  * passed in POINTEE_INFO.  If OWN_INFO, the pointee type is owned and
105  * destroyed together with INFO.  */
106 void type_init_pointer(struct arg_type_info *info,
107                        struct arg_type_info *pointee_info, int own_info);
108
109 /* Release any memory associated with INFO.  Doesn't free INFO
110  * itself.  */
111 void type_destroy(struct arg_type_info *info);
112
113 /* Compute a size of given type.  Return (size_t)-1 for error.  */
114 size_t type_sizeof(struct Process *proc, struct arg_type_info *type);
115
116 /* Compute an alignment necessary for elements of this type.  Return
117  * (size_t)-1 for error.  */
118 size_t type_alignof(struct Process *proc, struct arg_type_info *type);
119
120 /* Align value SZ to ALIGNMENT and return the result.  */
121 size_t align(size_t sz, size_t alignment);
122
123 /* Return ELT-th element of compound type TYPE.  This is useful for
124  * arrays and structures.  */
125 struct arg_type_info *type_element(struct arg_type_info *type, size_t elt);
126
127 /* Compute an offset of EMT-th element of type TYPE.  This works for
128  * arrays and structures.  Return (size_t)-1 for error.  */
129 size_t type_offsetof(struct Process *proc,
130                      struct arg_type_info *type, size_t elt);
131
132 /* Whether TYPE is an integral type as defined by the C standard.  */
133 int type_is_integral(enum arg_type type);
134
135 /* Whether TYPE, which shall be integral, is a signed type.  */
136 int type_is_signed(enum arg_type type);
137
138 /* If INFO is floating point equivalent type, return the corresponding
139  * floating point type.  Otherwise return NULL.  Floating point
140  * equivalent types are either ARGTYPE_FLOAT, or ARGTYPE_DOUBLE, or
141  * ARGTYPE_STRUCT whose sole member is a floating point equivalent
142  * type.  */
143 struct arg_type_info *type_get_fp_equivalent(struct arg_type_info *info);
144
145 #endif /* TYPE_H */