File: types.c

package info (click to toggle)
esnacc 1.8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,452 kB
  • sloc: ansic: 42,340; cpp: 13,880; yacc: 2,682; tcl: 1,587; lex: 688; sh: 573; makefile: 111
file content (650 lines) | stat: -rw-r--r-- 19,471 bytes parent folder | download | duplicates (4)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * compiler/back_ends/idl_gen/types.c  - fills in IDL type information
 *
 * MS 91/92
 * Copyright (C) 1991, 1992 Michael Sample
 *           and the University of British Columbia
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * $Header: /baseline/SNACC/compiler/back-ends/idl-gen/types.c,v 1.7 2004/03/25 19:20:16 gronej Exp $
 * $Log: types.c,v $
 * Revision 1.7  2004/03/25 19:20:16  gronej
 * fixed some linux warnings
 *
 * Revision 1.6  2004/01/14 19:07:53  gronej
 * Updated Compiler to accept and process relative-oid's
 *
 * Revision 1.5  2003/07/07 14:51:57  nicholar
 * Eliminated headers and cleaned up include references
 *
 * Revision 1.4  2002/09/16 17:53:53  mcphersc
 * Fixed warnings
 *
 * Revision 1.3  2002/09/04 18:02:27  vracarl
 * got rid of c++ comments
 *
 * Revision 1.2  2000/10/24 14:54:49  rwc
 * Updated to remove high-level warnings (level 4 on MSVC++) for an easier build.
 * SOME warnings persist due to difficulty in modifying the SNACC compiler to
 * properly build clean source; also some files are built by Lex/Yacc.
 *
 * Revision 1.1.1.1  2000/08/21 20:36:04  leonberp
 * First CVS Version of SNACC.
 *
 * Revision 1.1  1997/01/01 20:25:40  rj
 * first draft
 *
 */

#include <ctype.h>
#include <string.h>
#include "asn-incl.h"
#include "asn1module.h"
#include "snacc-util.h"
#include "str-util.h"
#include "rules.h"


static DefinedObj	*definedNamesG;


/* unexported prototypes */

void FillIDLTypeDefInfo PROTO ((IDLRules *r, Module *m, TypeDef *td));

static void FillIDLFieldNames PROTO ((IDLRules *r, NamedTypeList *firstSibling));

static void FillIDLTypeRefInfo PROTO ((IDLRules *r, Module *m, TypeDef *td, Type *parent, Type *t));

static void FillIDLStructElmts PROTO ((IDLRules *r, Module *m, TypeDef *td, Type *parent, NamedTypeList *t));

static void FillIDLChoiceElmts PROTO ((IDLRules *r, Module *m, TypeDef *td, Type *parent, NamedTypeList *first));

static int IsIDLPtr PROTO ((IDLRules *r, TypeDef *td, Type *parent, Type *t));

void FillIDLTDIDefaults PROTO ((IDLRules *r, IDLTDI *ctdi, TypeDef *td));


/*
 *  allocates and fills all the idlTypeInfos
 *  in the type trees for every module in the list
 */
void
FillIDLTypeInfo PARAMS ((r, modList),
    IDLRules *r _AND_
    ModuleList *modList)
{
    TypeDef *td;
    Module *m;

    /*
     * go through each module's type defs and fill
     * in the C type and enc/dec routines etc
     */
    definedNamesG = NULL;

    FOR_EACH_LIST_ELMT (m, modList)
    {
        FOR_EACH_LIST_ELMT (td, m->typeDefs)
            FillIDLTypeDefInfo (r, m, td);
    }

    /*
     * now that type def info is filled in
     * set up set/seq/list/choice elements that ref
     * those definitions
     */
    FOR_EACH_LIST_ELMT (m, modList)
    {
        FOR_EACH_LIST_ELMT (td, m->typeDefs)
            FillIDLTypeRefInfo (r, m, td, NULL, td->type);
    }

    /*
     * modules compiled together (ie one call to snacc with
     * multiple args) likely to be C compiled together so
     * need a unique routines/types/defines/enum values
     * since assuming they share same name space.
     *  All Typedefs, union, struct & enum Tags, and defined values
     * (enum consts), #define names
     *  are assumed to share the same name space
     */

    /* done with checking for name conflicts */
    FreeDefinedObjs (&definedNamesG);

}  /* FillIDLTypeInfo */


/*
 *  allocates and fills structure holding C type definition information
 *  fo the given ASN.1 type definition.  Does not fill CTRI for contained
 *  types etc.
 */
void
FillIDLTypeDefInfo PARAMS ((r, m, td),
    IDLRules *r _AND_
    Module *m _AND_
    TypeDef *td)
{
    char *tmpName;
    IDLTDI *idltdi;

    /*
     * if IDLTDI is present this type def has already been 'filled'
     */
    if (td->idlTypeDefInfo != NULL)
        return;


    idltdi = MT (IDLTDI);
    td->idlTypeDefInfo = idltdi;

    /* get default type def attributes from table for type on rhs of ::= */

    FillIDLTDIDefaults (r, idltdi, td);


    /*
     * if defined by a ref to another type definition fill in that type
     * def's IDLTDI so can inherit (actully completly replace default
     * attributes) from it
     */
    if ((td->type->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
        (td->type->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
    {
        /*
         * Fill in IDLTDI for defining type if nec.
         * this works for importTypeRef as well since both a.localTypeRef
         * and a.importTypeRef are of type TypeRef
         */
        FillIDLTypeDefInfo (r, td->type->basicType->a.localTypeRef->module, td->type->basicType->a.localTypeRef->link);

        tmpName = idltdi->typeName; /* save typeName */
        /* copy all type def info and restore name related stuff - hack*/
        *idltdi = *td->type->basicType->a.localTypeRef->link->idlTypeDefInfo;
        idltdi->typeName = tmpName; /* restore typeName */
    }


    /*
     * check for any "--snacc" attributes that overide the current
     * idltdi fields
     * UNDEFINED FOR C++
    ParseTypeDefAttribs (idltdi, td->attrList);
     */

    m=m;  /*AVOIDS warning.*/
}  /* FillIDLTypeDefInfo */


static void
FillIDLTypeRefInfo PARAMS ((r, m, td, parent, t),
    IDLRules	*r _AND_
    Module	*m _AND_
    TypeDef	*td _AND_
    Type	*parent _AND_
    Type	*t)
{
    IDLTRI	*idltri;
    IDLTDI	*tmpidltdi;
    ValueDef	*namedElmt;
    CNamedElmt	*cne;
    CNamedElmt	**cneHndl;
    char	*elmtName;
    int		len;
    enum BasicTypeChoiceId
		basicTypeId;

    /*
     * you must check for cycles yourself before calling this
     */
    if (t->idlTypeRefInfo == NULL)
    {
        idltri = MT (IDLTRI);
        t->idlTypeRefInfo = idltri;
    }
    else
        idltri = t->idlTypeRefInfo;

    basicTypeId = t->basicType->choiceId;

    tmpidltdi = &r->typeConvTbl[basicTypeId];

    /* get base type def info from the conversion table in the rules */
    idltri->isEnc = tmpidltdi->isEnc;
    idltri->typeName = tmpidltdi->typeName;
    idltri->optTestRoutineName = tmpidltdi->optTestRoutineName;


    /*
     * convert named elmts to IDL names.
     * check for name conflict with other defined Types/Names/Values
     */
    if ((basicTypeId == BASICTYPE_INTEGER || basicTypeId == BASICTYPE_ENUMERATED || basicTypeId == BASICTYPE_BITSTRING) && !(LIST_EMPTY (t->basicType->a.integer)))
    {
        idltri->namedElmts = AsnListNew (sizeof (void *));
        FOR_EACH_LIST_ELMT (namedElmt, t->basicType->a.integer)
        {
            cneHndl = (CNamedElmt **)AsnListAppend (idltri->namedElmts);
            cne = *cneHndl = MT (CNamedElmt);
            elmtName = Asn1ValueName2CValueName (namedElmt->definedName);
#if 0
	    if (basicTypeId == BASICTYPE_BITSTRING)
#endif
	    {
		len = strlen (elmtName);
		cne->name = Malloc (len + 1 + r->maxDigitsToAppend);
		strcpy (cne->name, elmtName);
	    }
#if 0
	    else
	    {
		len = strlen (idltri->typeName) + 7 + strlen (elmtName);
		cne->name = Malloc (len + 1 + r->maxDigitsToAppend);
		strcpy (cne->name, idltri->typeName);
		strcat (cne->name, "Choice_");
		strcat (cne->name, elmtName);
	    }
#endif
            Free (elmtName); /* not very efficient */

            if (namedElmt->value->basicValue->choiceId == BASICVALUE_INTEGER)
                cne->value = namedElmt->value->basicValue->a.integer;
            else
            {
                fprintf (errFileG, "Warning: unlinked defined value. Using -9999999\n");
                cne->value = -9999999;
            }

            if (r->capitalizeNamedElmts)
                Str2UCase (cne->name, len);

            /*
             * append digits if enum value name is a keyword
             */
            MakeCxxStrUnique (definedNamesG, cne->name, r->maxDigitsToAppend, 1);
            DefineObj (&definedNamesG, cne->name);
        }
    }

    /* fill in rest of type info depending on the type */
    switch (basicTypeId)
    {
        case BASICTYPE_BOOLEAN:  /* library types */
        case BASICTYPE_INTEGER:
        case BASICTYPE_BITSTRING:
        case BASICTYPE_OCTETSTRING:
        case BASICTYPE_NULL:
        case BASICTYPE_OID:
        case BASICTYPE_RELATIVE_OID:
        case BASICTYPE_REAL:
        case BASICTYPE_ENUMERATED:
            /* don't need to do anything else */
            break;


        case BASICTYPE_SEQUENCEOF:  /* list types */
        case BASICTYPE_SETOF:
            /* fill in component type */
            FillIDLTypeRefInfo (r, m, td, t, t->basicType->a.setOf);
            break;

        case BASICTYPE_IMPORTTYPEREF:  /* type references */
        case BASICTYPE_LOCALTYPEREF:
            /*
             * grab class name from link (link is the def of the
             * the ref'd type)
             */
            if (t->basicType->a.localTypeRef->link != NULL)
            {
                /* inherit attributes from referenced type */
                tmpidltdi=  t->basicType->a.localTypeRef->link->idlTypeDefInfo;
                idltri->typeName = tmpidltdi->typeName;
                idltri->isEnc = tmpidltdi->isEnc;
                idltri->optTestRoutineName = tmpidltdi->optTestRoutineName;
            }

            break;

        case BASICTYPE_ANYDEFINEDBY:  /* ANY types */
            break;  /* these are handled now */

        case BASICTYPE_ANY:
#if 0
            PrintErrLoc (m->asn1SrcFileName, t->lineNo);
            fprintf (errFileG, "Warning - generated code for the \"ANY\" type in type \"%s\" will need modification by YOU.", td->definedName);
            fprintf (errFileG, "  The source files will have a \"/* ANY - Fix Me! */\" comment before related code.\n\n");
#endif

            break;

        case BASICTYPE_CHOICE:
           /*
            * must fill field names BEFORE filling choice elmts
            * (allows better naming for choice ids)
            */
            FillIDLFieldNames (r, t->basicType->a.choice);
            FillIDLChoiceElmts (r, m, td, t, t->basicType->a.choice);
            break;

        case BASICTYPE_SET:
        case BASICTYPE_SEQUENCE:
            FillIDLStructElmts (r, m, td, t, t->basicType->a.set);
            FillIDLFieldNames (r, t->basicType->a.set);
            break;

        case BASICTYPE_COMPONENTSOF:
        case BASICTYPE_SELECTION:
            fprintf (errFileG, "Compiler error - COMPONENTS OF or SELECTION type slipped through normalizing phase.\n");
            break;

        case BASICTYPE_UNKNOWN:
        case BASICTYPE_MACRODEF:
        case BASICTYPE_MACROTYPE:
            /* do nothing */
            break;

    default:
      break;
    }

    /*
     * figure out whether this is a ptr based on the enclosing
     * type (if any) and optionality/default
     */
    idltri->isPtr = (unsigned char)IsIDLPtr (r, td, parent, t);

    /* let user overide any defaults with the --snacc attributes */
    /* undefined for C++ ParseTypeRefAttribs (ctri, t->attrList); */


}  /* FillIDLTypeRefInfo */



static void
FillIDLStructElmts PARAMS ((r, m, td, parent, elmts),
    IDLRules *r _AND_
    Module *m _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    NamedTypeList *elmts)
{
    NamedType *et;

    FOR_EACH_LIST_ELMT (et, elmts)
    {
        FillIDLTypeRefInfo (r, m, td, parent, et->type);
    }

}  /* FillIDLStructElmts */



/*
 *  Figures out non-conflicting enum names for the
 *  choice id's
 */
static void
FillIDLChoiceElmts PARAMS ((r, m, td, parent, elmts),
    IDLRules	*r _AND_
    Module	*m _AND_
    TypeDef	*td _AND_
    Type	*parent _AND_
    NamedTypeList *elmts)
{
    NamedType	*et;
    int		idCount = 0;
    IDLTRI	*idltri;
    int		len;

    /*
     * fill in type info for elmt types first
     */
    FOR_EACH_LIST_ELMT (et, elmts)
        FillIDLTypeRefInfo (r, m, td, parent, et->type);

    /*
     * set choiceId Symbol & value
     * eg
     *  Car ::= CHOICE {          enum CarChoice {
     *     chev ChevCar,              carChoice_chev,
     *     ford FordCar,              carChoice_ford,
     *     toyota ToyotaCar           carChoice_toyota
     *  }                         };
     *                            union Car switch (CarChoice) {
     *                                ChevCar *chev;
     *                                FordCar *ford;
     *                                ToyotaCar *toyota; };
     *                            };
     * NOTE that the union is anonymous
     */
    FOR_EACH_LIST_ELMT (et, elmts)
    {
        idltri =  et->type->idlTypeRefInfo;

        if (idltri == NULL)
            continue; /* wierd type */

        idltri->choiceIdValue = idCount++;

        len = strlen (td->idlTypeDefInfo->typeName) + strlen (idltri->fieldName);
        idltri->choiceIdSymbol = Malloc (len + 6 + 1);
        strcpy (idltri->choiceIdSymbol, td->idlTypeDefInfo->typeName);
	strcat (idltri->choiceIdSymbol, "Choice_");
        strcat (idltri->choiceIdSymbol, idltri->fieldName);

        if (r->capitalizeNamedElmts)
            Str2UCase (idltri->choiceIdSymbol, len);

	Str2LCase (idltri->choiceIdSymbol, 1);
    }

}  /* FillIDLChoiceElmts */


/*
 * takes a list of "sibling" (eg same level in a structure)
 * ElmtTypes and fills sets up the c field names in
 * the IDLTRI struct
 */
static void
FillIDLFieldNames PARAMS ((r, elmts),
    IDLRules *r _AND_
    NamedTypeList *elmts)
{
    NamedType  *et;
    IDLTRI *idltri;
    DefinedObj *fieldNames;
    int        len;
    char      *tmpName;
    char      *asn1FieldName;
    char      *cFieldName;

    /*
     * Initialize fieldname data
     * allocate (if nec) and fill in CTRI fieldname if poss
     * from asn1 field name.  leave blank otherwise
     */
    fieldNames = NewObjList();
    FOR_EACH_LIST_ELMT (et, elmts)
    {
        idltri =  et->type->idlTypeRefInfo;
        if (idltri == NULL)
        {
            idltri = MT (IDLTRI);
            et->type->idlTypeRefInfo = idltri;
        }
        if (et->fieldName != NULL)
        {
            /*
             * can assume that the field names are
             * distinct because they have passed the
             * error checking step.
             * However, still call MakeCxxStrUnique
             * to change any field names that
             * conflict with C++ keywords
             */
            asn1FieldName = et->fieldName;
            tmpName = Asn1FieldName2CFieldName (asn1FieldName);
            idltri->fieldName = Malloc (strlen (tmpName) + 1 + r->maxDigitsToAppend);
            strcpy (idltri->fieldName, tmpName);
            Free (tmpName);

/*   old    idltri->fieldName = Asn1FieldName2CFieldName (asn1FieldName); */

            MakeCxxStrUnique (fieldNames, idltri->fieldName, r->maxDigitsToAppend, 1);
            DefineObj (&fieldNames, idltri->fieldName);
        }
    }


    FOR_EACH_LIST_ELMT (et, elmts)
    {
        idltri =  et->type->idlTypeRefInfo;

        /*
         * generate field names for those without them
         */
        if (idltri->fieldName == NULL)
        {
            if ((et->type->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
                 (et->type->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
            {
                /*
                 * take ref'd type name as field name
                 * convert first let to lower case
                 */
                tmpName = et->type->basicType->a.localTypeRef->link->idlTypeDefInfo->typeName;
                tmpName =  Asn1TypeName2CTypeName (tmpName);
                cFieldName = Malloc (strlen (tmpName) + r->maxDigitsToAppend +1);
                strcpy (cFieldName, tmpName);
                Free (tmpName);
                if (isupper (cFieldName[0]))
                    cFieldName[0] = (char)tolower (cFieldName[0]);
            }
            else
            {
                /*
                 * get default field name for this type
                 */
                tmpName = r->typeConvTbl[et->type->basicType->choiceId].defaultFieldName;
                cFieldName = Malloc (strlen (tmpName) + r->maxDigitsToAppend +1);
                strcpy (cFieldName, tmpName);

                if (isupper (cFieldName[0]))
                    cFieldName[0] = (char)tolower (cFieldName[0]);
            }


            len = strlen (cFieldName);

            /*
             * try to use just the type name (with lower case first char).
             * if that is already used in this type or a C++ keyword,
             * append ascii digits to field name until unique
             * in this type
             */
            MakeCxxStrUnique (fieldNames, cFieldName, r->maxDigitsToAppend, 1);
            DefineObj (&fieldNames, cFieldName);
            idltri->fieldName = cFieldName;
        }
    }
    FreeDefinedObjs (&fieldNames);
}  /* FillIDLFieldNames */



/*
 * returns true if this c type for this type should be
 * be ref'd as a ptr
 */
static int
IsIDLPtr PARAMS ((r, td, parent, t),
    IDLRules *r _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    Type *t)
{
    IDLTDI *idltdi;
    int retVal = FALSE;

    /*
     * inherit ptr attriubutes from ref'd type if any
     * otherwise grab lib c type def from the IDLRules
     */
    if ((t->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
        (t->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
    {
        idltdi = t->basicType->a.localTypeRef->link->idlTypeDefInfo;
    }
    else
        idltdi = &r->typeConvTbl[GetBuiltinType (t)];

    /* no parent means t is the root of a typedef */
    if ((parent == NULL) && (idltdi->isPtrForTypeDef))
        retVal = TRUE;

    else if ((parent != NULL) &&
             ((parent->basicType->choiceId == BASICTYPE_SET) ||
             (parent->basicType->choiceId == BASICTYPE_SEQUENCE)) &&
             (idltdi->isPtrInSetAndSeq))
        retVal = TRUE;

    else if ((parent != NULL) &&
             ((parent->basicType->choiceId == BASICTYPE_SETOF) ||
             (parent->basicType->choiceId == BASICTYPE_SEQUENCEOF)) &&
             (idltdi->isPtrInList))
        retVal = TRUE;

    else if ((parent != NULL) &&
             (parent->basicType->choiceId == BASICTYPE_CHOICE) &&
             (idltdi->isPtrInChoice))
        retVal = TRUE;

    else if (((t->optional) || (t->defaultVal != NULL)) && (idltdi->isPtrForOpt))
        retVal = TRUE;

    return retVal;
    td=td;  /*AVOIDS warning.*/
}  /* IsIDLPtr */



/* fill given idltdi with defaults from table for given typedef */
void
FillIDLTDIDefaults PARAMS ((r, idltdi, td),
    IDLRules *r _AND_
    IDLTDI *idltdi _AND_
    TypeDef *td)
{
    IDLTDI *tblidltdi;
    int typeIndex;
    char *tmpName;

    typeIndex = GetBuiltinType (td->type);

    if (typeIndex < 0)
        return;

    tblidltdi = &r->typeConvTbl[typeIndex];

    memcpy (idltdi, tblidltdi, sizeof (IDLTDI));

    /* make sure class name is unique wrt to previously defined classes */
    tmpName = Asn1TypeName2CTypeName (td->definedName);
    idltdi->typeName = Malloc (strlen (tmpName) + 2 + r->maxDigitsToAppend +1);
    strcpy (idltdi->typeName, tmpName);
    if (tblidltdi->asn1TypeId != BASICTYPE_CHOICE)
	strcat (idltdi->typeName, "_T");
    Free (tmpName);

    MakeCxxStrUnique (definedNamesG, idltdi->typeName, r->maxDigitsToAppend, 1);
    DefineObj (&definedNamesG, idltdi->typeName);

} /* FillIDLTDIDefaults */