File: gen-any.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 (517 lines) | stat: -rw-r--r-- 16,222 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
/*
 * compiler/back_ends/c++_gen/gen_any.c
 *
 *     prints Routine to initialize the ANY Hash table.  The
 *     ANY Hash table maps the OBJECT IDENTIFIERS or INTEGERS
 *     to the correct decoding routines.
 *
 *     Also prints an enum to identify each ANY mapping.
 *
 * MS 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.
 *
 * INSERT_VDA_COMMENTS
 *
 * $Header: /baseline/SNACC/compiler/back-ends/c++-gen/gen-any.c,v 1.16 2003/12/19 20:55:49 leonberp Exp $
 * $Log: gen-any.c,v $
 * Revision 1.16  2003/12/19 20:55:49  leonberp
 * added parameter to PrintCxxOidValue() to allow printing of paren or quote to be specified.
 *
 * Revision 1.15  2003/07/07 14:52:35  nicholar
 * Eliminated headers and cleaned up include references
 *
 * Revision 1.14  2002/10/29 16:37:34  mcphersc
 * changed the any global int variable used for any defined by's
 *
 * Revision 1.13  2002/09/16 17:48:02  mcphersc
 *
 * iFixed warings
 *
 * Revision 1.12  2002/09/04 17:51:35  vracarl
 * got rid of c++ comments
 *
 * Revision 1.11  2002/05/15 14:53:10  leonberp
 * added support for new basicTypes to compiler
 *
 * Revision 1.10  2002/02/28 19:45:17  nicholar
 * Added calls to Dash2Underscore() to remove dashes in ANYs.
 *
 * Revision 1.9  2001/10/16 04:26:09  leonberp
 * removed destructor for any tables and moved it to the runtime library
 *
 * Revision 1.8  2001/10/12 15:05:45  leonberp
 * Fixed bug in InitAny destructor generator
 *
 * Revision 1.7  2001/10/12 14:53:47  leonberp
 * Fixed bug in InitAny destructor generator
 *
 * Revision 1.6  2001/10/11 18:34:31  leonberp
 * added code to generate destructor for InitAny classes so they oid and int hash tables will be cleaned up.  Also fixed the hash table clean up code to be recursive.
 *
 * Revision 1.5  2001/06/11 18:30:56  rwc
 * Re-added actual constructor of module specific ANY DEFINED BY table update.
 * vda test now works again.  NEED TO RE-TEST with multiple modules.
 *
 * Revision 1.4  2001/05/11 16:00:46  mcphersc
 * Fixed problems with Init Any's and class statement in gen-code.
 * Gen-code not allowing for "::" in some instances
 *
 * Revision 1.3  2001/04/18 16:28:56  rwc
 * Updated test for ANY DEFINED BY updates.  Now supports default CSM_Buffer (AsnAnyBuffer) result if
 * OID is not recognized.
 *
 * Revision 1.2  2000/10/24 14:54:43  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:05  leonberp
 * First CVS Version of SNACC.
 *
 * Revision 1.4  1995/07/25 18:19:11  rj
 * changed `_' to `-' in file names.
 *
 * Revision 1.3  1994/10/08  03:47:53  rj
 * since i was still irritated by cpp standing for c++ and not the C preprocessor, i renamed them to cxx (which is one known suffix for C++ source files). since the standard #define is __cplusplus, cplusplus would have been the more obvious choice, but it is a little too long.
 *
 * Revision 1.2  1994/09/01  01:06:31  rj
 * snacc_config.h removed.
 *
 * Revision 1.1  1994/08/28  09:47:58  rj
 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
 *
 */

#include <string.h>			/* REN -- 5/11/2001 -- added for string functions */
#include "asn-incl.h"
#include "asn1module.h"
#include "str-util.h"
#include "rules.h"
#include "snacc-util.h"		/* REN -- 5/11/2001 -- added for GetBuiltinType() */

#ifdef WIN32
#pragma  warning( disable : 4100 )  /* IGNORE unreferenced formal parameter */
#endif


void PrintCxxAnyEnum PROTO ((FILE *hdr, Module *m, CxxRules *r));
void PrintCxxAnyHashInitRoutine PROTO ((FILE *src, FILE *hdr, ModuleList *mods,
									   Module *m, CxxRules *r));
void PrintCxxOidValue PROTO ((FILE *f, CxxRules *r, AsnOid *oid, int parenOrQuote));

/* REN -- 5/11/2001 -- added following prototype */
static TypeDef* GetTypeDef PROTO ((Type *t));


extern int anyEnumValG;


void
PrintCxxAnyCode PARAMS ((src, hdr, r, mods, m),
    FILE *src _AND_
    FILE *hdr _AND_
    CxxRules *r _AND_
    ModuleList *mods _AND_
    Module *m)
{

    if (!m->hasAnys)
        return;

    PrintCxxAnyEnum (hdr, m, r);
    PrintCxxAnyHashInitRoutine (src, hdr, mods, m, r);

}  /* PrintAnyCode */



void
PrintCxxAnyEnum PARAMS ((hdr, m, r),
    FILE *hdr _AND_
    Module *m _AND_
    CxxRules *r)
{
    int firstPrinted = TRUE;
    char *modName;
/* REN -- 5/11/2001 */
/*  TypeDef *td;
     AnyRef *ar;
    AnyRefList *arl;
    int i; */
	ValueDef *vd;
	Type *t;
    char anyId[512];
/* REN -- end */

    modName = Asn1TypeName2CTypeName (m->modId->name);

    fprintf (hdr,"typedef enum %sAnyId\n", modName);
    fprintf (hdr,"{\n");

/* REN -- 5/11/2001 -- Changed method in which anyRefs get written.  Original 
code looped through the module's basic types and local type refs and wrote the 
anys that referred to them.  Corrected method loops through the valueRefs and 
writes all of the SNMP Object Types.
Removed the following loops:
     do any lib types 
    for (i = BASICTYPE_BOOLEAN; i < BASICTYPE_MACRODEF; i++)
    {
        arl = LIBTYPE_GET_ANY_REFS (i);
        if (arl != NULL)
        {
            FOR_EACH_LIST_ELMT (ar, arl)
            {
                if (!firstPrinted)
                    fprintf (hdr,",\n");
                fprintf (hdr,"    %s = %d", ar->anyIdName, anyEnumValG++);
                firstPrinted = FALSE;
            }
        }
    }

    FOR_EACH_LIST_ELMT (td, m->typeDefs)
    {
        if (td->anyRefs != NULL)
        {
            FOR_EACH_LIST_ELMT (ar, td->anyRefs)
            {
                if (!firstPrinted)
                    fprintf (hdr,",\n");
                fprintf (hdr,"    %s = %d", ar->anyIdName, anyEnumValG++);
                firstPrinted = FALSE;
            }
        }
    }

REN -- 5/11/2001 -- added the following: */
    FOR_EACH_LIST_ELMT (vd, m->valueDefs)
    {
        if (vd->value != NULL)
		{
			t = vd->value->type;
			if ((GetBuiltinType(t) == BASICTYPE_MACROTYPE) &&
				(t->basicType->a.macroType->choiceId == 
				MACROTYPE_SNMPOBJECTTYPE))
			{
				strcpy (anyId, vd->definedName);
			    Dash2Underscore (anyId, strlen (anyId));
				strcat (anyId, "_ANY_ID");

				if (!firstPrinted)
					fprintf (hdr,",\n");
				fprintf (hdr,"    %s = %d", anyId, anyEnumValG);
				anyEnumValG++;
				firstPrinted = FALSE;
            }
        }
    }
/* REN -- end */

    if (firstPrinted) /* none have been printed */
        fprintf (hdr,"/* NO INTEGER or OBJECT IDENTIFIER to ANY type relationships were defined (via MACROs or other mechanism) */\n ??? \n");

    fprintf (hdr,"\n} %sAnyId;\n\n\n", modName);
    Free (modName);

}  /* PrintAnyEnum */


void
PrintCxxAnyHashInitRoutine PARAMS ((src, hdr, mods, m, r),
    FILE *src _AND_
    FILE *hdr _AND_
    ModuleList *mods _AND_
    Module *m _AND_
    CxxRules *r)
{

    TypeDef *td;
    int i;
    int installedSomeHashes=0;
    struct CxxTDI *cxxtdi;
    char *modName;
/* REN -- 5/11/2001 */
/*  AnyRef *ar;
    AnyRefList *arl;
    int j; */
    enum BasicTypeChoiceId typeId;
	ValueDef *vd;
	Type *t;
	BasicValue *bv;
    char anyId[512];
	char *typeName = NULL;
/* REN -- end */

    modName = Asn1TypeName2CTypeName (m->modId->name);

    /* print Any class src file */
    fprintf (src,"// this class will automatically intialize the any hash tbl\n");
    fprintf (src,"class InitAny%s\n", modName);
    fprintf (src,"{\n");
    fprintf (src,"  public:\n");
    fprintf (src,"    InitAny%s();\n", modName);
    fprintf (src,"    /* Do not add a destructor to this class!  It could\n");
    fprintf (src,"     * cause pre-mature destruction of the ANY tables.\n");
    fprintf (src,"     * The ANY tables will be destroyed by the runtime library.\n");
    fprintf (src,"     */\n");
#if 0
    fprintf (src,"    ~InitAny%s() { AsnAny::AsnAnyDestroyHashTbls(); }\n",modName);
#endif
    fprintf (src,"};\n\n");

    fprintf (src,"static InitAny%s anyInitalizer;\n", modName);

    /* print constructor method that build hash tbl to src file*/
    fprintf (src,"InitAny%s::InitAny%s()\n", modName, modName);
    fprintf (src,"{\n");

/* REN -- 5/11/2001 -- Changed method in which anyRefs get written.  Original
code looped through the module's basic types and local type refs and wrote the
anys that referred to them.  Corrected method loops through the valueRefs and
writes all of the SNMP Object Types.
Removed the following loops:
   first print value for OID's *
     do any lib types first *
    i = 0;
    for (j = BASICTYPE_BOOLEAN; j < BASICTYPE_MACRODEF; j++)
    {
        arl = LIBTYPE_GET_ANY_REFS (j);
        if (arl != NULL)
        {
            FOR_EACH_LIST_ELMT (ar, arl)
            {
                installedSomeHashes = TRUE;
                if (ar->id->choiceId == OIDORINT_OID)
                {
                    fprintf (src,"    %s oid%d", r->typeConvTbl[BASICTYPE_OID].className, i++);
                    PrintCxxOidValue (src, r, ar->id->a.oid);
                    fprintf (src,";\n");
                }
                else if (ar->id->choiceId == OIDORINT_INTID)
                {
                    fprintf (src,"    %s int%d", r->typeConvTbl[BASICTYPE_INTEGER].className, i++);
                    PrintCxxIntValue (src, r, ar->id->a.intId);
                    fprintf (src,";\n");
                }
            }
        }
    }


    FOR_EACH_LIST_ELMT (td, m->typeDefs)
    {
        if (td->anyRefs != NULL)
        {
            cxxtdi = td->cxxTypeDefInfo;
            FOR_EACH_LIST_ELMT (ar, td->anyRefs)
            {
                installedSomeHashes = TRUE;
                if (ar->id->choiceId == OIDORINT_OID)
                {
                    fprintf (src,"    %s oid%d", r->typeConvTbl[BASICTYPE_OID].className, i++);
                    PrintCxxOidValue (src, r, ar->id->a.oid);
                    fprintf (src,";\n");
                }
                else if (ar->id->choiceId == OIDORINT_INTID)
                {
                    fprintf (src,"    %s int%d", r->typeConvTbl[BASICTYPE_INTEGER].className, i++);
                    PrintCxxIntValue (src, r, ar->id->a.intId);
                    fprintf (src,";\n");
                }
            }
        }
    }


    * now print hash init calls *
    i = 0;
    for (j = BASICTYPE_BOOLEAN; j < BASICTYPE_MACRODEF; j++)
    {
        arl = LIBTYPE_GET_ANY_REFS (j);
        if (arl != NULL)
        {
            FOR_EACH_LIST_ELMT (ar, arl)
            {
                if (ar->id->choiceId == OIDORINT_OID)
                    fprintf (src,"    AsnAny::InstallAnyByOid (oid%d, %s, new %s);\n", i++, ar->anyIdName, r->typeConvTbl[j].className);

                else
                    fprintf (src,"    AsnAny::InstallAnyByInt (int%d, %s, new %s);\n", i++, ar->anyIdName, r->typeConvTbl[j].className);

            }
        }
    }

    FOR_EACH_LIST_ELMT (td, m->typeDefs)
    {
        if (td->anyRefs != NULL)
        {
            FOR_EACH_LIST_ELMT (ar, td->anyRefs)
            {
                cxxtdi = td->cxxTypeDefInfo;

                if (ar->id->choiceId == OIDORINT_OID)
                    fprintf (src,"    AsnAny::InstallAnyByOid (oid%d, %s, new %s);\n", i++, ar->anyIdName, cxxtdi->className);

                else
                    fprintf (src,"    AsnAny::InstallAnyByInt (int%d, %s, new %s);\n", i++, ar->anyIdName, cxxtdi->className);

            }
        }
    }

REN -- 5/11/2001 -- added the following: */
	/* first print value for OID's */
    i = 0;

    FOR_EACH_LIST_ELMT (vd, m->valueDefs)
    {
        if (vd->value != NULL)
		{
			t = vd->value->type;
			if ((GetBuiltinType(t) == BASICTYPE_MACROTYPE) &&
				(t->basicType->a.macroType->choiceId == 
				MACROTYPE_SNMPOBJECTTYPE))
			{
				bv = vd->value->basicValue;
				if (bv != NULL)
				{
					installedSomeHashes = TRUE;
					if (bv->choiceId == BASICVALUE_OID)
					{
	                    fprintf (src,"    %s oid%d", 
							r->typeConvTbl[BASICTYPE_OID].className, i++);
		                PrintCxxOidValue (src, r, bv->a.oid, 1);
			            fprintf (src,";\n");
					}
				}
            }
        }
    }
    fprintf (src,"\n\n");

    /* now print hash init calls */
    i = 0;

    FOR_EACH_LIST_ELMT (vd, m->valueDefs)
    {
        if (vd->value != NULL)
		{
			t = vd->value->type;
			if ((GetBuiltinType(t) == BASICTYPE_MACROTYPE) &&
				(t->basicType->a.macroType->choiceId ==
				MACROTYPE_SNMPOBJECTTYPE))
			{
				bv = vd->value->basicValue;
				if (bv != NULL)
				{
					strcpy (anyId, vd->definedName);
				    Dash2Underscore (anyId, strlen (anyId));
					strcat (anyId, "_ANY_ID");

					installedSomeHashes = TRUE;
					t = t->basicType->a.macroType->a.snmpObjectType->syntax;

					/* If the syntax of this any is a basic type, get the
					class name from the rules table. */
					typeId = t->basicType->choiceId;
					if (((typeId >= BASICTYPE_BOOLEAN) &&
						(typeId <= BASICTYPE_SETOF)) ||
						((typeId >= BASICTYPE_NUMERIC_STR) &&
						(typeId <= BASICTYPE_T61_STR)))
					{
						typeName = r->typeConvTbl[typeId].className;
					}
					/* Else if the syntax of this any is either a locally
					defined type or an imported type, get the class name from
					the this type's ref info. */
					else
					{
						td = GetTypeDef(t);
						if (td != NULL)
						{
							cxxtdi = td->cxxTypeDefInfo;
							typeName = cxxtdi->className;
						}
						else
					        typeName = NULL;
					}

					if (typeName == NULL)
						fprintf(src, "*** ERROR *** Unknown ANY\n\n");
					else
					{
						if (bv->choiceId == BASICVALUE_OID)
						{
							fprintf (src,
								"    AsnAny::InstallAnyByOid (oid%d, %s, new %s);\n",
								i++, anyId, typeName);
						}
						else if (bv->choiceId == BASICVALUE_INTEGER)
						{
							fprintf (src,
								"    AsnAny::InstallAnyByInt (%d, %s, new %s);\n",
								bv->a.integer, anyId, typeName);
						}
					}
				}
            }
        }
    }
/* REN -- end */

    if (!installedSomeHashes)
    {
        fprintf (src,"    /* Since no INTEGER/OID to ANY type relations were defined\n");
        fprintf (src,"     * (usually done via MACROs) you must manually do the code\n");
        fprintf (src,"     * to fill the hash tbl.\n");
        fprintf (src,"     * if the ids are INTEGER use the following:\n");
        fprintf (src,"     * AsnAny::InstallAnyByInt (3, ??_ANY_ID, new <className>);\n");
        fprintf (src,"     * if the ids are OBJECT IDENTIFIERs use the following:\n");
        fprintf (src,"     * AsnAny::InstallAnyByOid (OidValue, ??_ANY_ID, new <className>);\n");
        fprintf (src,"     * put the ??_ANY_IDs in the AnyId enum.\n\n");
        fprintf (src,"     * For example if you have some thing like\n");
        fprintf (src,"     * T1 ::= SEQUENCE { id INTEGER, ANY DEFINED BY id }\n");
        fprintf (src,"     * and the id 1 maps to the type BOOLEAN use the following:\n");
        fprintf (src,"     * AsnAny::InstallAnyByInt (1, SOMEBOOL_ANY_ID, new AsnBool);\n");
        fprintf (src,"     */\n ???????\n");  /* generate compile error */
    }


    fprintf (src,"}  /* InitAny::InitAny */\n\n\n");
/* RWC;4/23/01;#endif */

/* RWC;4/23/01;    r;m;mods;hdr;src;  */ /*AVOIDS warning.*/
}  /* PrintAnyHashInitRoutine */


/* REN -- 5/11/2001 -- GetTypeDef() function added to return the type def info
for the given type. */
static TypeDef*
GetTypeDef PARAMS ((t),
    Type *t)
{
    if (t == NULL)
		return NULL;

	switch (t->basicType->choiceId)
    {
        case BASICTYPE_LOCALTYPEREF:
        case BASICTYPE_IMPORTTYPEREF:
			return t->basicType->a.localTypeRef->link;
            break;

        default:
            return NULL;
    }
    /*fprintf (errFileG, "GetTypeDef: ERROR - cannot get type def for unlinked local/import type refs\n");
    return NULL;*/

}  /* GetTypeDef */
/* REN -- end */