]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/idl-gen/gen-code.c
Security-54.1.3.tar.gz
[apple/security.git] / SecuritySNACCRuntime / compiler / back-ends / idl-gen / gen-code.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * compiler/back_ends/idl_gen/gen_idl_code.c - routines for printing CORBA IDL code from type trees
21 *
22 * assumes that the type tree has already been run through the
23 * IDL type generator (idl_gen/types.c).
24 *
25 * Mike Sample
26 * 92
27 * Copyright (C) 1991, 1992 Michael Sample
28 * and the University of British Columbia
29 *
30 * Copyright © 1995 Robert Joop
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/compiler/back-ends/idl-gen/gen-code.c,v 1.1.1.1 2001/05/18 23:14:09 mb Exp $
38 * $Log: gen-code.c,v $
39 * Revision 1.1.1.1 2001/05/18 23:14:09 mb
40 * Move from private repository to open source repository
41 *
42 * Revision 1.2 2001/05/05 00:59:28 rmurphy
43 * Adding darwin license headers
44 *
45 * Revision 1.1.1.1 1999/03/16 18:06:45 aram
46 * Originals from SMIME Free Library.
47 *
48 * Revision 1.2 1997/03/13 09:15:21 wan
49 * Improved dependency generation for stupid makedepends.
50 * Corrected PeekTag to peek into buffer only as far as necessary.
51 * Added installable error handler.
52 * Fixed small glitch in idl-code generator (Markku Savela <msa@msa.tte.vtt.fi>).
53 *
54 * Revision 1.1 1997/01/01 20:25:34 rj
55 * first draft
56 *
57 */
58
59 #include "snacc.h"
60
61 #if STDC_HEADERS || HAVE_STRING_H
62 #include <string.h>
63 #else
64 #include <strings.h>
65 #endif
66 #include <stdio.h>
67
68 #include "asn-incl.h"
69 #include "asn1module.h"
70 #include "define.h"
71 #include "mem.h"
72 #include "lib-types.h"
73 #include "rules.h"
74 #include "types.h"
75 #include "cond.h"
76 #include "str-util.h"
77 #include "snacc-util.h"
78 #include "print.h"
79 #include "tag-util.h" /* get GetTags/FreeTags/CountTags/TagByteLen */
80 #include "gen-vals.h"
81 #include "gen-any.h"
82 #include "gen-code.h"
83
84
85 static long int longJmpValG = -100;
86
87 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
88 static void
89 PrintComment PARAMS ((idl, m),
90 FILE *idl _AND_
91 Module *m)
92 {
93 long int t;
94
95 t = time (0);
96 fprintf (idl, "// NOTE: this is a machine generated file -- editing not recommended\n");
97 fprintf (idl, "//\n");
98 fprintf (idl, "// %s -- IDL for ASN.1 module %s\n", m->idlFileName, m->modId->name);
99 fprintf (idl, "//\n");
100 fprintf (idl, "// This file was generated by snacc on %s", ctime (&t));
101 fprintf (idl, "// UBC snacc written by Mike Sample\n");
102 fprintf (idl, "// IDL generator written by Robert Joop\n");
103 fprintf (idl, "\n");
104
105 } /* PrintComment */
106
107 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
108 static void
109 PrintIncludes PARAMS ((idl, mods, m),
110 FILE *idl _AND_
111 ModuleList *mods _AND_
112 Module *m)
113 {
114 void *tmp;
115 Module *currMod;
116
117 fprintf (idl, "#include \"ASN1Types.idl\"\n");
118 fprintf (idl, "#include \"BitString.idl\"\n");
119
120 tmp = (void *)CURR_LIST_NODE (mods); /* remember curr loc */
121 FOR_EACH_LIST_ELMT (currMod, mods)
122 fprintf (idl, "#include \"%s\"\n", currMod->idlFileName);
123 SET_CURR_LIST_NODE (mods, tmp);
124
125 } /* PrintIncludes */
126
127 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
128 static void
129 PrintTypeDecl PARAMS ((f, td),
130 FILE *f _AND_
131 TypeDef *td)
132 {
133 switch (td->type->basicType->choiceId)
134 {
135 case BASICTYPE_COMPONENTSOF:
136 case BASICTYPE_SELECTION:
137 case BASICTYPE_UNKNOWN:
138 case BASICTYPE_MACRODEF:
139 case BASICTYPE_MACROTYPE:
140 return; /* do nothing */
141
142 case BASICTYPE_ENUMERATED:
143 if (IsNewType (td->type))
144 fprintf (f, " enum %s;\n", td->idlTypeDefInfo->typeName);
145 break;
146
147 default:
148 if (IsNewType (td->type))
149 fprintf (f, " struct %s;\n", td->idlTypeDefInfo->typeName);
150 }
151
152 } /* PrintTypeDecl */
153
154 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
155 static void
156 PrintIDLTypeAndName PARAMS ((idl, mods, m, r, td, parent, t),
157 FILE *idl _AND_
158 ModuleList *mods _AND_
159 Module *m _AND_
160 IDLRules *r _AND_
161 TypeDef *td _AND_
162 Type *parent _AND_
163 Type *t)
164 {
165 if (t->optional)
166 fprintf (idl, "union %sOptional switch (boolean) { case True: %s %s; };\n", t->idlTypeRefInfo->typeName, t->idlTypeRefInfo->typeName, t->idlTypeRefInfo->fieldName);
167 else
168 fprintf (idl, "%s %s;\n", t->idlTypeRefInfo->typeName, t->idlTypeRefInfo->fieldName);
169
170 #if 0
171 if (t->idlTypeRefInfo->isPtr)
172 fprintf (idl, "*");
173 #endif
174
175 } /* PrintIDLTypeAndName */
176
177 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
178 /*
179 * prints typedef or new class given an ASN.1 type def of a primitive type
180 * or typeref. Uses inheritance to cover re-tagging and named elmts.
181 */
182 static void
183 PrintIDLSimpleDef PARAMS ((idl, r, td),
184 FILE *idl _AND_
185 IDLRules *r _AND_
186 TypeDef *td)
187 {
188 int hasNamedElmts;
189 CNamedElmt *n;
190
191 fprintf (idl, " /* ");
192 SpecialPrintType (idl, td, td->type);
193 fprintf (idl, " */\n");
194
195 if (hasNamedElmts = HasNamedElmts (td->type))
196 {
197 int tlen = strlen (td->idlTypeDefInfo->typeName) - strlen (r->typeSuffix);
198 switch (GetBuiltinType (td->type))
199 {
200 case BASICTYPE_INTEGER:
201 fprintf (idl, " typedef %s %s;\n", td->type->idlTypeRefInfo->typeName, td->idlTypeDefInfo->typeName);
202 FOR_EACH_LIST_ELMT (n, td->type->idlTypeRefInfo->namedElmts)
203 fprintf (idl, " const %s %.*s_%s = %d;\n", td->idlTypeDefInfo->typeName, tlen, td->idlTypeDefInfo->typeName, n->name, n->value);
204 break;
205 case BASICTYPE_ENUMERATED:
206 fprintf (idl, " enum %s\n", td->idlTypeDefInfo->typeName);
207 fprintf (idl, " {\n");
208 FOR_EACH_LIST_ELMT (n, td->type->idlTypeRefInfo->namedElmts)
209 {
210 char comma = (n != (CNamedElmt *)LAST_LIST_ELMT (td->type->idlTypeRefInfo->namedElmts)) ? ',' : ' ';
211 fprintf (idl, " %s%c // (original value = %d)\n", n->name, comma, n->value);
212 }
213 fprintf (idl, " };\n");
214 break;
215 case BASICTYPE_BITSTRING:
216 fprintf (idl, " typedef %s %s;\n", td->type->idlTypeRefInfo->typeName, td->idlTypeDefInfo->typeName);
217 FOR_EACH_LIST_ELMT (n, td->type->idlTypeRefInfo->namedElmts)
218 fprintf (idl, " const unsigned long %.*s_%s = %d;\n", tlen, td->idlTypeDefInfo->typeName, n->name, n->value);
219 break;
220 default:
221 fprintf (idl, " ???!\n");
222 }
223 }
224 else
225 fprintf (idl, " typedef %s %s;\n\n", td->type->idlTypeRefInfo->typeName, td->idlTypeDefInfo->typeName);
226
227 } /* PrintIDLSimpleDef */
228
229 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
230 static void
231 PrintIDLChoiceDefCode PARAMS ((idl, mods, m, r, td, parent, choice),
232 FILE *idl _AND_
233 ModuleList *mods _AND_
234 Module *m _AND_
235 IDLRules *r _AND_
236 TypeDef *td _AND_
237 Type *parent _AND_
238 Type *choice)
239 {
240 NamedType *e;
241
242 /* put class spec in idl file */
243
244 /* write out choiceId enum type */
245
246 fprintf (idl, " enum %s%s\n", td->idlTypeDefInfo->typeName, r->choiceEnumSuffix);
247 fprintf (idl, " {\n");
248 FOR_EACH_LIST_ELMT (e, choice->basicType->a.choice)
249 {
250 fprintf (idl, " %s", e->type->idlTypeRefInfo->choiceIdSymbol);
251 if (e != (NamedType *)LAST_LIST_ELMT (choice->basicType->a.choice))
252 fprintf (idl, ",\n");
253 else
254 fprintf (idl, "\n");
255 }
256 fprintf (idl, " };\n\n");
257
258 /* write out the choice element anonymous union */
259 fprintf (idl, " union %s switch (%s%s)\n", td->idlTypeDefInfo->typeName, td->idlTypeDefInfo->typeName, r->choiceEnumSuffix);
260 fprintf (idl, " {\n");
261 FOR_EACH_LIST_ELMT (e, choice->basicType->a.choice)
262 {
263 #if 0
264 fprintf (idl, " case %s: %s %s;\n", e->type->idlTypeRefInfo->choiceIdSymbol, e->type->idlTypeRefInfo->typeName, e->type->idlTypeRefInfo->fieldName);
265 #else
266 fprintf (idl, " case %s: ", e->type->idlTypeRefInfo->choiceIdSymbol);
267 PrintIDLTypeAndName (idl, mods, m, r, td, choice, e->type);
268 #endif
269 }
270 fprintf (idl, " };\n\n");
271
272 } /* PrintIDLChoiceDefCode */
273
274
275 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
276 static void
277 PrintIDLSeqDefCode PARAMS ((idl, mods, m, r, td, parent, seq),
278 FILE *idl _AND_
279 ModuleList *mods _AND_
280 Module *m _AND_
281 IDLRules *r _AND_
282 TypeDef *td _AND_
283 Type *parent _AND_
284 Type *seq)
285 {
286 NamedType *e;
287
288 /* put class spec in idl file */
289
290 fprintf (idl, " struct %s\n", td->idlTypeDefInfo->typeName);
291 fprintf (idl, " {\n");
292
293 /* write out the sequence elmts */
294 FOR_EACH_LIST_ELMT (e, seq->basicType->a.sequence)
295 {
296 fprintf (idl, " ");
297 PrintIDLTypeAndName (idl, mods, m, r, td, seq, e->type);
298 }
299
300 /* close struct definition */
301 fprintf (idl, " };\n\n\n");
302
303 } /* PrintIDLSeqDefCode */
304
305 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
306 static void
307 PrintIDLSetDefCode PARAMS ((idl, mods, m, r, td, parent, set),
308 FILE *idl _AND_
309 ModuleList *mods _AND_
310 Module *m _AND_
311 IDLRules *r _AND_
312 TypeDef *td _AND_
313 Type *parent _AND_
314 Type *set)
315 {
316 NamedType *e;
317
318 /* put class spec in idl file */
319
320 fprintf (idl, " struct %s\n", td->idlTypeDefInfo->typeName);
321 fprintf (idl, " {\n");
322
323 /* write out the set elmts */
324 FOR_EACH_LIST_ELMT (e, set->basicType->a.set)
325 {
326 fprintf (idl, " ");
327 PrintIDLTypeAndName (idl, mods, m, r, td, set, e->type);
328 }
329
330 fprintf (idl, " };\n\n");
331
332 } /* PrintIDLSetDefCode */
333
334 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
335 static void
336 PrintCxxSetOfDefCode PARAMS ((idl, mods, m, r, td, parent, setOf),
337 FILE *idl _AND_
338 ModuleList *mods _AND_
339 Module *m _AND_
340 IDLRules *r _AND_
341 TypeDef *td _AND_
342 Type *parent _AND_
343 Type *setOf)
344 {
345 char *lcn; /* list class name */
346 char *ecn; /* (list) elmt class name */
347
348 lcn = td->idlTypeDefInfo->typeName;
349 ecn = setOf->basicType->a.setOf->idlTypeRefInfo->typeName;
350 fprintf (idl, " typedef sequence<%s> %s;\n", ecn, lcn);
351
352 } /* PrintCxxSetOfDefCode */
353
354
355 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
356 static void
357 PrintCxxAnyDefCode PARAMS ((idl, mods, m, r, td, parent, any),
358 FILE *idl _AND_
359 ModuleList *mods _AND_
360 Module *m _AND_
361 IDLRules *r _AND_
362 TypeDef *td _AND_
363 Type *parent _AND_
364 Type *any)
365 {
366 fprintf (idl, " /* ");
367 SpecialPrintType (idl, td, td->type);
368 fprintf (idl, " */\n");
369 fprintf (idl, " typedef %s %s;\n\n", td->type->idlTypeRefInfo->typeName, td->idlTypeDefInfo->typeName);
370 } /* PrintCxxAnyDefCode */
371
372
373 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
374 static void
375 PrintIDLTypeDefCode PARAMS ((idl, mods, m, r, td),
376 FILE *idl _AND_
377 ModuleList *mods _AND_
378 Module *m _AND_
379 IDLRules *r _AND_
380 TypeDef *td)
381 {
382 switch (td->type->basicType->choiceId)
383 {
384 case BASICTYPE_BOOLEAN: /* library type */
385 case BASICTYPE_REAL: /* library type */
386 case BASICTYPE_OCTETSTRING: /* library type */
387 case BASICTYPE_NULL: /* library type */
388 case BASICTYPE_OID: /* library type */
389 case BASICTYPE_INTEGER: /* library type */
390 case BASICTYPE_BITSTRING: /* library type */
391 case BASICTYPE_ENUMERATED: /* library type */
392 PrintIDLSimpleDef (idl, r, td);
393 break;
394
395 case BASICTYPE_SEQUENCEOF: /* list types */
396 case BASICTYPE_SETOF:
397 PrintCxxSetOfDefCode (idl, mods, m, r, td, NULL, td->type);
398 break;
399
400 case BASICTYPE_IMPORTTYPEREF: /* type references */
401 case BASICTYPE_LOCALTYPEREF:
402 /*
403 * if this type has been re-tagged then
404 * must create new class instead of using a typedef
405 */
406 PrintIDLSimpleDef (idl, r, td);
407 break;
408
409 case BASICTYPE_ANYDEFINEDBY: /* ANY types */
410 case BASICTYPE_ANY:
411 /*
412 fprintf (stderr, " ANY types require modification. ");
413 fprintf (stderr, " The source files will have a \" ANY - Fix Me! \" comment before related code.\n\n");
414 */
415 PrintCxxAnyDefCode (idl, mods, m, r, td, NULL, td->type);
416 break;
417
418 case BASICTYPE_CHOICE:
419 PrintIDLChoiceDefCode (idl, mods, m, r, td, NULL, td->type);
420 break;
421
422 case BASICTYPE_SET:
423 PrintIDLSetDefCode (idl, mods, m, r, td, NULL, td->type);
424 break;
425
426 case BASICTYPE_SEQUENCE:
427 PrintIDLSeqDefCode (idl, mods, m, r, td, NULL, td->type);
428 break;
429
430 case BASICTYPE_COMPONENTSOF:
431 case BASICTYPE_SELECTION:
432 case BASICTYPE_UNKNOWN:
433 case BASICTYPE_MACRODEF:
434 case BASICTYPE_MACROTYPE:
435 /* do nothing */
436 break;
437 }
438 } /* PrintIDLTypeDefCode */
439
440 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
441 void
442 PrintIDLCode PARAMS ((idl, mods, m, r, longJmpVal),
443 FILE *idl _AND_
444 ModuleList *mods _AND_
445 Module *m _AND_
446 IDLRules *r _AND_
447 long int longJmpVal _AND_
448 int printValues)
449 {
450 TypeDef *td;
451 ValueDef *vd;
452
453 longJmpValG = longJmpVal;
454
455 PrintComment (idl, m);
456
457 PrintConditionalIncludeOpen (idl, m->idlFileName);
458
459 PrintIncludes (idl, mods, m);
460
461 fprintf (idl, "\n");
462 fprintf (idl, "module %s\n{\n\n", m->idlname);
463
464 fprintf (idl, " //----------------------------------------------------------------------------\n");
465 fprintf (idl, " // type declarations:\n\n");
466 FOR_EACH_LIST_ELMT (td, m->typeDefs)
467 PrintTypeDecl (idl, td);
468 fprintf (idl, "\n");
469
470 if (printValues)
471 {
472 fprintf (idl, " //----------------------------------------------------------------------------\n");
473 fprintf (idl, " // value definitions:\n\n");
474 FOR_EACH_LIST_ELMT (vd, m->valueDefs)
475 PrintIDLValueDef (idl, r, vd);
476 fprintf (idl, "\n");
477 }
478
479 fprintf (idl, " //----------------------------------------------------------------------------\n");
480 fprintf (idl, " // type definitions:\n\n");
481
482 #if 0
483 PrintIDLAnyCode (idl, r, mods, m);
484 #endif
485
486 FOR_EACH_LIST_ELMT (td, m->typeDefs)
487 {
488 PrintIDLTypeDefCode (idl, mods, m, r, td);
489 fputc ('\n', idl);
490 }
491
492 fprintf (idl, "}; // end of module %s\n", m->idlname);
493
494 PrintConditionalIncludeClose (idl, m->idlFileName);
495
496 } /* PrintIDLCode */
497
498 /*\[banner "EOF"]-----------------------------------------------------------------------------------------------------------------*/