1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013 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
29enum 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
45struct 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.  */
72struct arg_type_info *type_get_simple(enum arg_type type);
73struct arg_type_info *type_get_voidptr(void);
74
75/* Initialize INFO so it becomes ARGTYPE_STRUCT.  The created
76 * structure contains no fields.  Use type_struct_add to populate the
77 * structure.  */
78void type_init_struct(struct arg_type_info *info);
79
80/* Add a new field of type FIELD_INFO to a structure INFO.  If OWN,
81 * the field type is owned and destroyed together with INFO.  */
82int type_struct_add(struct arg_type_info *info,
83		    struct arg_type_info *field_info, int own);
84
85/* Get IDX-th field of structure type INFO.  */
86struct arg_type_info *type_struct_get(struct arg_type_info *info, size_t idx);
87
88/* Return number of fields of structure type INFO.  */
89size_t type_struct_size(struct arg_type_info *info);
90
91/* Return number of elements of an aggregate type INFO.  This can be
92 * either ARGTYPE_STRUCT or ARGTYPE_ARRAY of constant length.  If
93 * ARGTYPE_ARRAY does not have a constant length, this returns -1.  */
94size_t type_aggregate_size(struct arg_type_info *info);
95
96/* Initialize INFO so it becomes ARGTYPE_ARRAY.  The element type is
97 * passed in ELEMENT_INFO, and array length in LENGTH_EXPR.  If,
98 * respectively, OWN_INFO and OWN_LENGTH are true, the pointee and
99 * length are owned and destroyed together with INFO.  */
100void type_init_array(struct arg_type_info *info,
101		     struct arg_type_info *element_info, int own_info,
102		     struct expr_node *length_expr, int own_length);
103
104/* Initialize INFO so it becomes ARGTYPE_POINTER.  The pointee type is
105 * passed in POINTEE_INFO.  If OWN_INFO, the pointee type is owned and
106 * destroyed together with INFO.  */
107void type_init_pointer(struct arg_type_info *info,
108		       struct arg_type_info *pointee_info, int own_info);
109
110/* Release any memory associated with INFO.  Doesn't free INFO
111 * itself.  */
112void type_destroy(struct arg_type_info *info);
113
114/* Copy type INFO into the area pointed to by RETP.  Return 0 on
115 * success or a negative value on failure.  */
116int type_clone(struct arg_type_info *retp, const struct arg_type_info *info);
117
118/* Compute a size of given type.  Return (size_t)-1 for error.  */
119size_t type_sizeof(struct process *proc, struct arg_type_info *type);
120
121/* Compute an alignment necessary for elements of this type.  Return
122 * (size_t)-1 for error.  */
123size_t type_alignof(struct process *proc, struct arg_type_info *type);
124
125/* Align value SZ to ALIGNMENT and return the result.  */
126size_t align(size_t sz, size_t alignment);
127
128/* Return ELT-th element of compound type TYPE.  This is useful for
129 * arrays and structures.  */
130struct arg_type_info *type_element(struct arg_type_info *type, size_t elt);
131
132/* Compute an offset of EMT-th element of type TYPE.  This works for
133 * arrays and structures.  Return (size_t)-1 for error.  */
134size_t type_offsetof(struct process *proc,
135		     struct arg_type_info *type, size_t elt);
136
137/* Whether TYPE is an integral type as defined by the C standard.  */
138int type_is_integral(enum arg_type type);
139
140/* Whether TYPE, which shall be integral, is a signed type.  */
141int type_is_signed(enum arg_type type);
142
143/* If INFO is floating point equivalent type, return the corresponding
144 * floating point type.  Otherwise return NULL.  Floating point
145 * equivalent types are either ARGTYPE_FLOAT, or ARGTYPE_DOUBLE, or
146 * ARGTYPE_STRUCT whose sole member is a floating point equivalent
147 * type.  */
148struct arg_type_info *type_get_fp_equivalent(struct arg_type_info *info);
149
150/* If INFO is homogeneous floating-point aggregate, return the
151 * corresponding floating point type, and set *COUNTP to number of
152 * fields of the structure.  Otherwise return NULL.  INFO is a HFA if
153 * it's an aggregate whose each field is either a HFA, or a
154 * floating-point type.  */
155struct arg_type_info *type_get_hfa_type(struct arg_type_info *info,
156					size_t *countp);
157
158
159#endif /* TYPE_H */
160