type.h revision fcf256ceeab4b0b74cf1e18122e894aafce94fdc
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
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);
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.  */
77void 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.  */
81int 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.  */
85struct arg_type_info *type_struct_get(struct arg_type_info *info, size_t idx);
86
87/* Return number of fields of structure type INFO.  */
88size_t type_struct_size(struct arg_type_info *info);
89
90/* Initialize INFO so it becomes ARGTYPE_ARRAY.  The element type is
91 * passed in ELEMENT_INFO, and array length in LENGTH_EXPR.  If,
92 * respectively, OWN_INFO and OWN_LENGTH are true, the pointee and
93 * length are owned and destroyed together with INFO.  */
94void type_init_array(struct arg_type_info *info,
95		     struct arg_type_info *element_info, int own_info,
96		     struct expr_node *length_expr, int own_length);
97
98/* Initialize INFO so it becomes ARGTYPE_POINTER.  The pointee type is
99 * passed in POINTEE_INFO.  If OWN_INFO, the pointee type is owned and
100 * destroyed together with INFO.  */
101void type_init_pointer(struct arg_type_info *info,
102		       struct arg_type_info *pointee_info, int own_info);
103
104/* Release any memory associated with INFO.  Doesn't free INFO
105 * itself.  */
106void type_destroy(struct arg_type_info *info);
107
108/* Compute a size of given type.  Return (size_t)-1 for error.  */
109size_t type_sizeof(struct Process *proc, struct arg_type_info *type);
110
111/* Compute an alignment necessary for elements of this type.  Return
112 * (size_t)-1 for error.  */
113size_t type_alignof(struct Process *proc, struct arg_type_info *type);
114
115/* Align value SZ to ALIGNMENT and return the result.  */
116size_t align(size_t sz, size_t alignment);
117
118/* Return ELT-th element of compound type TYPE.  This is useful for
119 * arrays and structures.  */
120struct arg_type_info *type_element(struct arg_type_info *type, size_t elt);
121
122/* Compute an offset of EMT-th element of type TYPE.  This works for
123 * arrays and structures.  Return (size_t)-1 for error.  */
124size_t type_offsetof(struct Process *proc,
125		     struct arg_type_info *type, size_t elt);
126
127/* Whether TYPE is an integral type as defined by the C standard.  */
128int type_is_integral(enum arg_type type);
129
130/* Whether TYPE, which shall be integral, is a signed type.  */
131int type_is_signed(enum arg_type type);
132
133/* If INFO is floating point equivalent type, return the corresponding
134 * floating point type.  Otherwise return NULL.  Floating point
135 * equivalent types are either ARGTYPE_FLOAT, or ARGTYPE_DOUBLE, or
136 * ARGTYPE_STRUCT whose sole member is a floating point equivalent
137 * type.  */
138struct arg_type_info *type_get_fp_equivalent(struct arg_type_info *info);
139
140#endif /* TYPE_H */
141