File: init.c

package info (click to toggle)
libconvert-binary-c-perl 0.76-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,356 kB
  • sloc: ansic: 63,674; perl: 18,584; yacc: 2,143; makefile: 28
file content (314 lines) | stat: -rw-r--r-- 8,899 bytes parent folder | download | duplicates (2)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*******************************************************************************
*
* MODULE: init.c
*
********************************************************************************
*
* DESCRIPTION: C::B::C initializer
*
********************************************************************************
*
* $Project: /Convert-Binary-C $
* $Author: mhx $
* $Date: 2011/04/10 12:32:14 +0200 $
* $Revision: 18 $
* $Source: /cbc/init.c $
*
********************************************************************************
*
* Copyright (c) 2002-2011 Marcus Holland-Moritz. All rights reserved.
* This program is free software; you can redistribute it and/or modify
* it under the same terms as Perl itself.
*
*******************************************************************************/

/*===== GLOBAL INCLUDES ======================================================*/

#define PERL_NO_GET_CONTEXT
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>

#include "ppport.h"


/*===== LOCAL INCLUDES =======================================================*/

#include "util/list.h"

#include "cbc/idl.h"
#include "cbc/init.h"
#include "cbc/util.h"


/*===== DEFINES ==============================================================*/

#define INDENT                                \
        STMT_START {                          \
          if (level > 0)                      \
            add_indent(aTHX_ string, level);  \
        } STMT_END

#define APPEND_COMMA                          \
        STMT_START {                          \
          if (first)                          \
            first = 0;                        \
          else                                \
            sv_catpv(string, ",\n");          \
        } STMT_END

#define ENTER_LEVEL                           \
        STMT_START {                          \
          INDENT;                             \
          sv_catpv(string, "{\n");            \
        } STMT_END

#define LEAVE_LEVEL                           \
        STMT_START {                          \
          sv_catpv(string, "\n");             \
          INDENT;                             \
          sv_catpv(string, "}");              \
        } STMT_END


/*===== TYPEDEFS =============================================================*/

/*===== STATIC FUNCTION PROTOTYPES ===========================================*/

static void get_init_str_struct(pTHX_ CBC *THIS, Struct *pStruct, SV *init,
                                IDList *idl, int level, SV *string);
static void get_init_str_type(pTHX_ CBC *THIS, TypeSpec *pTS, Declarator *pDecl,
                              int dimension, SV *init, IDList *idl, int level, SV *string);


/*===== EXTERNAL VARIABLES ===================================================*/

/*===== GLOBAL VARIABLES =====================================================*/

/*===== STATIC VARIABLES =====================================================*/

/*===== STATIC FUNCTIONS =====================================================*/

/*******************************************************************************
*
*   ROUTINE: get_init_str_struct
*
*   WRITTEN BY: Marcus Holland-Moritz             ON: Jun 2003
*   CHANGED BY:                                   ON:
*
********************************************************************************
*
* DESCRIPTION:
*
*   ARGUMENTS:
*
*     RETURNS:
*
*******************************************************************************/

static void get_init_str_struct(pTHX_ CBC *THIS, Struct *pStruct, SV *init,
                                IDList *idl, int level, SV *string)
{
  ListIterator       sdi;
  StructDeclaration *pStructDecl;
  Declarator        *pDecl;
  HV                *hash = NULL;
  int                first = 1;

  CT_DEBUG(MAIN, (XSCLASS "::get_init_str_struct( THIS=%p, pStruct=%p, "
           "init=%p, idl=%p, level=%d, string=%p )",
           THIS, pStruct, init, idl, level, string));

  if (DEFINED(init))
  {
    SV *h;
    if (SvROK(init) && SvTYPE(h = SvRV(init)) == SVt_PVHV)
      hash = (HV *) h;
    else
      WARN((aTHX_ "'%s' should be a hash reference", idl_to_str(aTHX_ idl)));
  }

  ENTER_LEVEL;
  IDLIST_PUSH(idl, ID);

  LL_foreach(pStructDecl, sdi, pStruct->declarations)
  {
    if (pStructDecl->declarators)
    {
      ListIterator di;

      LL_foreach(pDecl, di, pStructDecl->declarators)
      {
        SV **e;

        /* skip unnamed bitfield members right here */
        if (pDecl->bitfield_flag && pDecl->identifier[0] == '\0')
          continue;

        /* skip flexible array members */
        if (pDecl->array_flag && pDecl->size == 0)
          continue;

        e = hash ? hv_fetch(hash, pDecl->identifier, CTT_IDLEN(pDecl), 0) : NULL;
        if(e)
          SvGETMAGIC(*e);

        IDLIST_SET_ID(idl, pDecl->identifier);
        APPEND_COMMA;

        get_init_str_type(aTHX_ THIS, &pStructDecl->type, pDecl, 0,
                          e ? *e : NULL, idl, level+1, string);

        /* only initialize first union member */
        if (pStruct->tflags & T_UNION)
          goto handle_end;
      }
    }
    else
    {
      TypeSpec *pTS = &pStructDecl->type;
      FOLLOW_AND_CHECK_TSPTR(pTS);
      APPEND_COMMA;
      IDLIST_POP(idl);
      get_init_str_struct(aTHX_ THIS, (Struct *) pTS->ptr,
                          init, idl, level+1, string);
      IDLIST_PUSH(idl, ID);

      /* only initialize first union member */
      if (pStruct->tflags & T_UNION)
        goto handle_end;
    }
  }

handle_end:
  IDLIST_POP(idl);
  LEAVE_LEVEL;
}

/*******************************************************************************
*
*   ROUTINE: get_init_str_type
*
*   WRITTEN BY: Marcus Holland-Moritz             ON: Jun 2003
*   CHANGED BY:                                   ON:
*
********************************************************************************
*
* DESCRIPTION:
*
*   ARGUMENTS:
*
*     RETURNS:
*
*******************************************************************************/

static void get_init_str_type(pTHX_ CBC *THIS, TypeSpec *pTS, Declarator *pDecl,
                              int dimension, SV *init, IDList *idl, int level, SV *string)
{
  CT_DEBUG(MAIN, (XSCLASS "::get_init_str_type( THIS=%p, pTS=%p, pDecl=%p, "
           "dimension=%d, init=%p, idl=%p, level=%d, string=%p )",
           THIS, pTS, pDecl, dimension, init, idl, level, string));

  if (pDecl && pDecl->array_flag && dimension < LL_count(pDecl->ext.array))
  {
    AV *ary = NULL;
    long i, s = ((Value *) LL_get(pDecl->ext.array, dimension))->iv;
    int first = 1;

    if (DEFINED(init))
    {
      SV *sv;
      if (SvROK(init) && SvTYPE(sv = SvRV(init)) == SVt_PVAV)
        ary = (AV *) sv;
      else
        WARN((aTHX_ "'%s' should be an array reference",
                    idl_to_str(aTHX_ idl)));
    }

    ENTER_LEVEL;
    IDLIST_PUSH(idl, IX);

    for (i = 0; i < s; ++i)
    {
      SV **e = ary ? av_fetch(ary, i, 0) : NULL;

      if (e)
        SvGETMAGIC(*e);

      IDLIST_SET_IX(idl, i);
      APPEND_COMMA;

      get_init_str_type(aTHX_ THIS, pTS, pDecl, dimension+1,
                        e ? *e : NULL, idl, level+1, string);
    }

    IDLIST_POP(idl);
    LEAVE_LEVEL;
  }
  else
  {
    if (pDecl && pDecl->pointer_flag)
      goto handle_basic;
    else if(pTS->tflags & T_TYPE)
    {
      Typedef *pTD = (Typedef *) pTS->ptr;
      get_init_str_type(aTHX_ THIS, pTD->pType, pTD->pDecl, 0, init, idl, level, string);
    }
    else if(pTS->tflags & T_COMPOUND)
    {
      Struct *pStruct = pTS->ptr;
      if (pStruct->declarations == NULL)
        WARN_UNDEF_STRUCT(pStruct);
      get_init_str_struct(aTHX_ THIS, pStruct, init, idl, level, string);
    }
    else
    {
handle_basic:
      INDENT;
      if (DEFINED(init))
      {
        if (SvROK(init))
          WARN((aTHX_ "'%s' should be a scalar value", idl_to_str(aTHX_ idl)));
        sv_catsv(string, init);
      }
      else
        sv_catpvn(string, "0", 1);
    }
  }
}

/*===== FUNCTIONS ============================================================*/

/*******************************************************************************
*
*   ROUTINE: get_initializer_string
*
*   WRITTEN BY: Marcus Holland-Moritz             ON: Jun 2003
*   CHANGED BY:                                   ON:
*
********************************************************************************
*
* DESCRIPTION:
*
*   ARGUMENTS:
*
*     RETURNS:
*
*******************************************************************************/

SV *get_initializer_string(pTHX_ CBC *THIS, MemberInfo *pMI, SV *init, const char *name)
{
  SV *string = newSVpvn("", 0);
  IDList idl;

  IDLIST_INIT(&idl);
  IDLIST_PUSH(&idl, ID);
  IDLIST_SET_ID(&idl, name);

  get_init_str_type(aTHX_ THIS, &pMI->type, pMI->pDecl, pMI->level, init, &idl, 0, string);

  IDLIST_FREE(&idl);

  return string;
}