summaryrefslogtreecommitdiff
path: root/src/analysis/type.c
blob: 6d09f4bfc596b841cf66e1b181d04c49f7ad04fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

/* Chrysalide - Outil d'analyse de fichiers binaires
 * type.h - prototypes pour la manipulation des types en tout genre
 *
 * Copyright (C) 2010-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "type.h"


#include <stdarg.h>


#include "routine.h"
#include "type-int.h"
#include "../common/extstr.h"



/* ------------------------ REPRESENTATION INTERNE DES TYPES ------------------------ */


/* Initialise la classe des types quelconques. */
static void g_data_type_class_init(GDataTypeClass *);

/* Initialise l'instance d'un type quelconque. */
static void g_data_type_init(GDataType *);



/* ---------------------------------------------------------------------------------- */
/*                          REPRESENTATION INTERNE DES TYPES                          */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour un type quelconque. */
G_DEFINE_TYPE(GDataType, g_data_type, G_TYPE_OBJECT);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des types quelconques.                  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_data_type_class_init(GDataTypeClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = instance à initialiser.                               *
*                                                                             *
*  Description : Initialise l'instance d'un type quelconque.                  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_data_type_init(GDataType *type)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type à dupliquer.                                     *
*                                                                             *
*  Description : Crée un copie d'un type existant.                            *
*                                                                             *
*  Retour      : Nouvelle instance de type identique à celle fournie.         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDataType *g_data_type_dup(const GDataType *type)
{
    return type->dup(type);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type      = type à mettre à jour.                            *
*                namespace = instance d'appartenance.                         *
*                                                                             *
*  Description : Définit le groupe d'appartenance d'un type donné.            *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_data_type_set_namespace(GDataType *type, GDataType *namespace)
{
    g_object_ref(G_OBJECT(namespace));

    type->namespace = namespace;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type   = type à convertir.                                   *
*                simple = indique si l'espace de noms doit être exclus ou non.*
*                                                                             *
*  Description : Décrit le type fourni sous forme de caractères.              *
*                                                                             *
*  Retour      : Chaîne à libérer de la mémoire après usage.                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

char *_g_data_type_to_string(const GDataType *type, bool simple)
{
    char *result;                           /* Chaîne à retourner          */
    const GDataType *parent;                /* Espace supérieur            */
    char *namespace;                        /* Groupe d'appartenance       */

    result = type->to_string(type);

    if (!simple)
        for (parent = type->namespace; parent != NULL; parent = parent->namespace)
        {
            namespace = g_data_type_to_string(parent);

            result = strprep(result, "." /* FIXME */);
            result = strprep(result, namespace);

            free(namespace);

        }

    if (type->qualifiers & TQF_RESTRICT)
        result = strprep(result, "restrict ");

    if (type->qualifiers & TQF_VOLATILE)
        result = strprep(result, "volatile ");

    if (type->qualifiers & TQF_CONST)
        result = strprep(result, "const ");

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type   = routine à afficher.                                 *
*                lang   = langage à utiliser pour la sortie humaine.          *
*                buffer = tampon mis à disposition pour la sortie.            *
*                info   = nature du cadre de destination.                     *
*                full   = besoin de descriptions étendues ?                   *
*                                                                             *
*  Description : Procède à l'impression de la description d'un type.          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
/*
void g_data_type_output(const GDataType *type, GLangOutput *lang, GBufferLine *line, bool info, bool full)
{
    type->output(type, lang, line, info, full);

}
*/


/******************************************************************************
*                                                                             *
*  Paramètres  : type      = instance à mettre à jour.                        *
*                qualifier = nouveau qualificatif pour la variable.           *
*                                                                             *
*  Description : Ajoute un qualificatif à une instance de type.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_data_type_add_qualifier(GDataType *type, TypeQualifier qualifier)
{
    type->qualifiers |= qualifier;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type à analyser.                                      *
*                large = doit-on aussi inclure les types 'référence' ?        *
*                                                                             *
*  Description : Indique la terminaison de la représentation du type.         *
*                                                                             *
*  Retour      : true ou false, selon le type fourni.                         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_data_type_is_pointer(const GDataType *type, bool large)
{
    bool result;                            /* Bilan à retourner           */

    result = G_IS_ENCAPSULATED_TYPE(type);

    if (result)
        switch (g_encapsulated_type_get_etype(G_ENCAPSULATED_TYPE(type)))
        {
            case ECT_POINTER:
                result = true;
                break;
            case ECT_REFERENCE:
            case ECT_RVALUE_REF:
                result = large;
                break;
            default:
                result = false;
                break;
        }

    return result;

}