]> git.saurik.com Git - apple/security.git/blame - SecuritySNACCRuntime/compiler/back-ends/c-gen/gen-print.c
Security-28.tar.gz
[apple/security.git] / SecuritySNACCRuntime / compiler / back-ends / c-gen / gen-print.c
CommitLineData
bac41a7b
A
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/c-gen/gen-print.c - routines for printing C hierachical print routines
21 *
22 * Mike Sample
23 * 92/04
24 * Copyright (C) 1991, 1992 Michael Sample
25 * and the University of British Columbia
26 *
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
31 *
32 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/compiler/back-ends/c-gen/gen-print.c,v 1.1.1.1 2001/05/18 23:14:09 mb Exp $
33 * $Log: gen-print.c,v $
34 * Revision 1.1.1.1 2001/05/18 23:14:09 mb
35 * Move from private repository to open source repository
36 *
37 * Revision 1.2 2001/05/05 00:59:28 rmurphy
38 * Adding darwin license headers
39 *
40 * Revision 1.1.1.1 1999/03/16 18:06:42 aram
41 * Originals from SMIME Free Library.
42 *
43 * Revision 1.3 1995/07/25 18:43:18 rj
44 * file name has been shortened for redundant part: c-gen/gen-c-print -> c-gen/gen-print.
45 *
46 * changed `_' to `-' in file names.
47 *
48 * Revision 1.2 1994/09/01 00:23:43 rj
49 * snacc_config.h and other superfluous .h files removed.
50 *
51 * Revision 1.1 1994/08/28 09:48:28 rj
52 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
53 *
54 */
55
56#include <stdio.h>
57
58#include "asn-incl.h"
59#include "asn1module.h"
60#include "mem.h"
61#include "define.h"
62#include "rules.h"
63#include "type-info.h"
64#include "str-util.h"
65#include "util.h"
66#include "gen-print.h"
67
68static char *returnTypeG = "void";
69static char *valueArgNameG = "v";
70static char *fileTypeNameG = "FILE*";
71static char *indentTypeNameG = "unsigned short int";
72static CRules *genPrintCRulesG;
73/* non-exported prototypes */
74
75static void PrintCPrintPrototype PROTO ((FILE *hdr, TypeDef *td));
76static void PrintCPrintDeclaration PROTO ((FILE *src, TypeDef *td));
77static void PrintCPrintDefine PROTO ((FILE *hdr, TypeDef *td));
78static void PrintCPrintLocals PROTO ((FILE *src,TypeDef *td));
79/*
80static void PrintCPrintElmts PROTO ((FILE *src, TypeDef *td, Type *parent, NamedTypeList *elmts, char *varName));
81*/
82static void PrintCChoiceElmtPrint PROTO ((FILE *src, TypeDef *td, Type *parent, NamedTypeList *elmts, NamedType *e, char *varName));
83
84
85static void PrintCElmtPrintWithIndent PROTO ((FILE *src, TypeDef *td, Type *parent, NamedTypeList *elmts, NamedType *e, char *varName, int allOpt));
86
87static void PrintCChoicePrintRoutine PROTO ((FILE *src, FILE *hdr, CRules *r, ModuleList *mods, Module *m, TypeDef *td));
88
89static void PrintCSetPrintRoutine PROTO ((FILE *src, FILE *hdr, CRules *r, ModuleList *mods, Module *m, TypeDef *td));
90static void PrintCSeqPrintRoutine PROTO ((FILE *src, FILE *hdr, CRules *r, ModuleList *mods, Module *m, TypeDef *td));
91static void PrintCSeqOfPrintRoutine PROTO ((FILE *src, FILE *hdr, CRules *r, ModuleList *mods, Module *m, TypeDef *td));
92static void PrintCSetOfPrintRoutine PROTO ((FILE *src, FILE *hdr, CRules *r, ModuleList *mods, Module *m, TypeDef *td));
93
94
95
96void
97PrintCPrinter PARAMS ((src, hdr, r, mods, m, td),
98 FILE *src _AND_
99 FILE *hdr _AND_
100 CRules *r _AND_
101 ModuleList *mods _AND_
102 Module *m _AND_
103 TypeDef *td)
104{
105 if ((td->cTypeDefInfo == NULL) || !(td->cTypeDefInfo->genPrintRoutine))
106 return;
107
108 genPrintCRulesG = r;
109 switch (td->type->basicType->choiceId)
110 {
111 case BASICTYPE_IMPORTTYPEREF: /* type references */
112 case BASICTYPE_LOCALTYPEREF:
113 case BASICTYPE_BOOLEAN: /* library type */
114 case BASICTYPE_REAL: /* library type */
115 case BASICTYPE_OCTETSTRING: /* library type */
116 case BASICTYPE_NULL: /* library type */
117 case BASICTYPE_OID: /* library type */
118 case BASICTYPE_INTEGER: /* library type */
119 case BASICTYPE_BITSTRING: /* library type */
120 case BASICTYPE_ENUMERATED: /* library type */
121 case BASICTYPE_ANYDEFINEDBY: /* ANY types */
122 case BASICTYPE_ANY:
123 PrintCPrintDefine (hdr, td);
124 fprintf (hdr, "\n\n");
125 break;
126
127 case BASICTYPE_SETOF:
128 PrintCSetOfPrintRoutine (src, hdr, r, mods, m, td);
129 break;
130
131 case BASICTYPE_SEQUENCEOF:
132 PrintCSeqOfPrintRoutine (src, hdr, r, mods, m, td);
133 break;
134
135 case BASICTYPE_CHOICE:
136 PrintCChoicePrintRoutine (src, hdr, r, mods, m, td);
137 break;
138
139 case BASICTYPE_SET:
140 PrintCSetPrintRoutine (src, hdr, r, mods, m, td);
141 break;
142
143
144 case BASICTYPE_SEQUENCE:
145 PrintCSeqPrintRoutine (src, hdr, r, mods, m, td);
146 break;
147
148 default:
149 break;
150 }
151} /* PrintCPrint */
152
153
154/*
155 * Prints prototype for encode routine in hdr file
156 */
157static void
158PrintCPrintPrototype PARAMS ((hdr, td),
159 FILE *hdr _AND_
160 TypeDef *td)
161{
162 CTDI *ctdi;
163
164 ctdi = td->cTypeDefInfo;
165 fprintf (hdr,"%s %s PROTO ((%s f, %s *v, %s indent));\n", returnTypeG, ctdi->printRoutineName, fileTypeNameG, ctdi->cTypeName, indentTypeNameG);
166
167} /* PrintCPrintPrototype */
168
169
170
171/*
172 * Prints declarations of encode routine for the given type def
173 */
174static void
175PrintCPrintDeclaration PARAMS ((src, td),
176 FILE *src _AND_
177 TypeDef *td)
178{
179 CTDI *ctdi;
180
181 ctdi = td->cTypeDefInfo;
182 fprintf (src,"%s\n%s PARAMS ((f, v, indent),\n%s f _AND_\n%s *v _AND_\n%s indent)\n", returnTypeG, ctdi->printRoutineName, fileTypeNameG, ctdi->cTypeName, indentTypeNameG);
183
184} /* PrintCPrintDeclaration */
185
186
187
188
189static void
190PrintCPrintDefine PARAMS ((hdr, td),
191 FILE *hdr _AND_
192 TypeDef *td)
193{
194 fprintf(hdr, "#define %s %s", td->cTypeDefInfo->printRoutineName, td->type->cTypeRefInfo->printRoutineName);
195/*
196 fprintf(hdr, "#define %s(f, v, indent) ", td->cTypeDefInfo->printRoutineName);
197 fprintf (hdr, "%s (f, v, indent)", td->type->cTypeRefInfo->printRoutineName);
198*/
199} /* PrintCPrintDefine */
200
201
202
203
204static void
205PrintCPrintLocals PARAMS ((src, td),
206 FILE *src _AND_
207 TypeDef *td)
208{
209 /* none yet */
210} /* PrintCPrintLocals */
211
212
213/*
214static void
215PrintCPrintElmts PARAMS ((src, td, parent, elmts, varName),
216 FILE *src _AND_
217 TypeDef *td _AND_
218 Type *parent _AND_
219 NamedTypeList *elmts _AND_
220 char *varName)
221{
222 NamedType *e;
223
224
225 FOR_EACH_LIST_ELMT (e, elmts)
226 PrintCElmtPrint (src, td, parent, elmts, e, varName);
227} PrintCBerElmtsEncodeCode */
228
229
230
231/*
232 * Prints code for printing a CHOICE element
233 *
234 */
235static void
236PrintCChoiceElmtPrint PARAMS ((src, td, parent, elmts, e, varName),
237 FILE *src _AND_
238 TypeDef *td _AND_
239 Type *parent _AND_
240 NamedTypeList *elmts _AND_
241 NamedType *e _AND_
242 char *varName)
243{
244 CTRI *ctri;
245 char elmtVarRef[MAX_VAR_REF];
246 Type *tmpType;
247 int inTailOpts;
248
249 ctri = e->type->cTypeRefInfo;
250
251
252 /* build ref to the elmt */
253 MakeVarPtrRef (genPrintCRulesG, td, parent, e->type, varName, elmtVarRef);
254
255 if (e->fieldName != NULL)
256 {
257 fprintf (src," fprintf (f,\"%s \");\n", e->fieldName);
258 fprintf (src," %s (f, %s, indent + stdIndentG);\n", e->type->cTypeRefInfo->printRoutineName, elmtVarRef);
259 }
260 else
261 {
262 fprintf (src," %s (f, %s, indent + stdIndentG);\n", e->type->cTypeRefInfo->printRoutineName, elmtVarRef);
263 }
264
265} /* PrintCChoiceElmtPrint */
266
267/*
268 * Prints code for printing an elmt of a SEQ or SET
269 *
270 * Does funny things to print commas correctly
271 * eg for the following type
272 * Foo ::= SET
273 * {
274 * A, --> print A ",\n"
275 * B, B ",\n"
276 * C OPTIONAL, C ",\n" if C present
277 * D, D ",\n"
278 * E, E ",\n"
279 * F, F <- nothing after last non-opt
280 * before tail opts.
281 * G OPTIONAL, ",\n" G
282 * H OPTIONAL ",\n" H "\n"
283 * }
284
285 */
286static void
287PrintCElmtPrintWithIndent PARAMS ((src, td, parent, elmts, e, varName, allOpt),
288 FILE *src _AND_
289 TypeDef *td _AND_
290 Type *parent _AND_
291 NamedTypeList *elmts _AND_
292 NamedType *e _AND_
293 char *varName _AND_
294 int allOpt)
295{
296 CTRI *ctri;
297 char elmtVarRef[MAX_VAR_REF];
298 Type *tmpType;
299 int inTailOpts;
300
301 ctri = e->type->cTypeRefInfo;
302
303 /* this assumes the elmts->curr == e */
304 inTailOpts = IsTailOptional (elmts);
305
306 /* build ref to the elmt */
307 MakeVarPtrRef (genPrintCRulesG, td, parent, e->type, varName, elmtVarRef);
308
309 /* if optional then put in NULL check */
310 if (e->type->optional || (e->type->defaultVal != NULL))
311 fprintf (src, " if (%s (%s))\n {\n", ctri->optTestRoutineName, elmtVarRef);
312
313 if (allOpt)
314 {
315 if (e != FIRST_LIST_ELMT (elmts))
316 {
317 fprintf (src, " if (!nonePrinted)\n");
318 fprintf (src, " fprintf (f,\",\\n\");\n");
319 }
320 fprintf (src, " nonePrinted = FALSE;\n");
321 }
322 else if ((inTailOpts) && (e != FIRST_LIST_ELMT (elmts)))
323 fprintf (src, " fprintf (f,\",\\n\");\n");
324
325 fprintf (src," Indent (f, indent + stdIndentG);\n");
326
327 if (e->fieldName != NULL)
328 fprintf (src," fprintf (f,\"%s \");\n", e->fieldName);
329
330 fprintf (src," %s (f, %s, indent + stdIndentG);\n", e->type->cTypeRefInfo->printRoutineName, elmtVarRef);
331
332 if ((e != LAST_LIST_ELMT (elmts)) &&
333 (!inTailOpts) &&
334 (!NextIsTailOptional (elmts)))
335 fprintf (src," fprintf (f, \",\\n\");\n");
336
337
338 /* write closing brkt for NULL check for optional elmts */
339 if (e->type->optional || (e->type->defaultVal != NULL))
340 fprintf (src, " }\n");
341
342 if (e == LAST_LIST_ELMT (elmts))
343 fprintf (src," fprintf (f,\"\\n\");\n");
344
345} /* PrintCElmtPrintWithIndent */
346
347
348static void
349PrintCChoicePrintRoutine PARAMS ((src, hdr, r, mods, m, td),
350 FILE *src _AND_
351 FILE *hdr _AND_
352 CRules *r _AND_
353 ModuleList *mods _AND_
354 Module *m _AND_
355 TypeDef *td)
356{
357 NamedType *e;
358
359 PrintCPrintPrototype (hdr,td);
360
361 PrintCPrintDeclaration (src, td);
362 fprintf (src,"{\n");
363 PrintCPrintLocals (src,td);
364 fprintf (src," switch (%s->%s)\n", valueArgNameG, td->type->cTypeRefInfo->choiceIdEnumFieldName);
365 fprintf (src," {\n");
366
367 FOR_EACH_LIST_ELMT (e, td->type->basicType->a.choice)
368 {
369 fprintf (src," case %s:\n",e->type->cTypeRefInfo->choiceIdSymbol);
370 fprintf (src," ");
371 PrintCChoiceElmtPrint (src, td, td->type, td->type->basicType->a.choice, e, valueArgNameG);
372 fprintf (src," break;\n\n");
373 }
374 fprintf (src," }\n");
375/* fprintf (src," fprintf (f,\"\\n\");\n"); */
376
377 fprintf (src,"} /* %s */\n\n", td->cTypeDefInfo->printRoutineName);
378
379} /* PrintCChoicePrintRoutine */
380
381
382
383static void
384PrintCSetPrintRoutine PARAMS ((src, hdr, r, mods, m, td),
385 FILE *src _AND_
386 FILE *hdr _AND_
387 CRules *r _AND_
388 ModuleList *mods _AND_
389 Module *m _AND_
390 TypeDef *td)
391{
392 NamedType *e;
393 int allOpt;
394
395 PrintCPrintPrototype (hdr,td);
396
397 PrintCPrintDeclaration (src, td);
398 fprintf (src,"{\n");
399 PrintCPrintLocals (src,td);
400
401 allOpt = AllElmtsOptional (td->type->basicType->a.set);
402 /*
403 * print extra local variable so commas are handled correctly
404 * when all elements are optional
405 */
406 if (allOpt)
407 fprintf (src," int nonePrinted = TRUE;\n\n");
408
409 fprintf (src," if (%s == NULL)\n", valueArgNameG);
410 fprintf (src," return;\n\n");
411
412 fprintf (src," fprintf (f,\"{ -- SET --\\n\");\n\n");
413
414
415 FOR_EACH_LIST_ELMT (e, td->type->basicType->a.set)
416 {
417 PrintCElmtPrintWithIndent (src, td, td->type, td->type->basicType->a.set, e, valueArgNameG, allOpt);
418 }
419 fprintf (src," Indent (f, indent);\n");
420 fprintf (src," fprintf (f,\"}\");\n");
421
422 fprintf (src,"} /* %s */\n\n", td->cTypeDefInfo->printRoutineName);
423
424} /* PrintCSetPrintRoutine */
425
426
427
428static void
429PrintCSeqPrintRoutine PARAMS ((src, hdr, r, mods, m, td),
430 FILE *src _AND_
431 FILE *hdr _AND_
432 CRules *r _AND_
433 ModuleList *mods _AND_
434 Module *m _AND_
435 TypeDef *td)
436{
437 NamedType *e;
438 int allOpt;
439
440 PrintCPrintPrototype (hdr,td);
441
442 PrintCPrintDeclaration (src, td);
443 fprintf (src,"{\n");
444 PrintCPrintLocals (src,td);
445
446 allOpt = AllElmtsOptional (td->type->basicType->a.set);
447 /*
448 * print extra local variable so commas are handled correctly
449 * when all elements are optional
450 */
451 if (allOpt)
452 fprintf (src," int nonePrinted = TRUE;\n\n");
453
454 fprintf (src," if (%s == NULL)\n", valueArgNameG);
455 fprintf (src," return;\n\n");
456
457 fprintf (src," fprintf (f,\"{ -- SEQUENCE --\\n\");\n\n");
458
459 FOR_EACH_LIST_ELMT (e, td->type->basicType->a.sequence)
460 {
461 PrintCElmtPrintWithIndent (src, td, td->type, td->type->basicType->a.sequence, e, valueArgNameG, allOpt);
462 }
463 fprintf (src," Indent (f, indent);\n");
464 fprintf (src," fprintf (f,\"}\");\n");
465
466 fprintf (src,"} /* %s */\n\n", td->cTypeDefInfo->printRoutineName);
467} /* PrintCSeqPrintRoutine */
468
469
470
471static void
472PrintCSetOfPrintRoutine PARAMS ((src, hdr, r, mods, m, td),
473 FILE *src _AND_
474 FILE *hdr _AND_
475 CRules *r _AND_
476 ModuleList *mods _AND_
477 Module *m _AND_
478 TypeDef *td)
479{
480 NamedType *e;
481
482 PrintCPrintPrototype (hdr,td);
483
484 PrintCPrintDeclaration (src, td);
485 fprintf (src,"{\n");
486 PrintCPrintLocals (src,td);
487
488 fprintf (src," %s *tmp;\n", td->type->basicType->a.setOf->cTypeRefInfo->cTypeName);
489
490 fprintf (src," if (%s == NULL)\n", valueArgNameG);
491 fprintf (src," return;\n");
492
493 fprintf (src," fprintf (f,\"{ -- SET OF -- \\n\");\n");
494
495 fprintf (src," FOR_EACH_LIST_ELMT (tmp, %s)\n", valueArgNameG);
496 fprintf (src," {\n");
497 fprintf (src," Indent (f, indent+ stdIndentG);\n");
498 fprintf (src," %s (f, tmp, indent + stdIndentG);\n", td->type->basicType->a.setOf->cTypeRefInfo->printRoutineName);
499 fprintf (src," if (tmp != (%s*)LAST_LIST_ELMT (%s))\n", td->type->basicType->a.setOf->cTypeRefInfo->cTypeName, valueArgNameG);
500 fprintf (src," fprintf (f,\",\\n\");\n");
501 fprintf (src," }\n");
502 fprintf (src," fprintf (f,\"\\n\");\n");
503 fprintf (src," Indent (f, indent);\n");
504 fprintf (src," fprintf (f,\"}\");\n");
505
506 fprintf (src,"} /* %s */\n\n", td->cTypeDefInfo->printRoutineName);
507
508} /* PrintCSetOfPrintRoutine */
509
510static void
511PrintCSeqOfPrintRoutine PARAMS ((src, hdr, r, mods, m, td),
512 FILE *src _AND_
513 FILE *hdr _AND_
514 CRules *r _AND_
515 ModuleList *mods _AND_
516 Module *m _AND_
517 TypeDef *td)
518{
519 NamedType *e;
520
521 PrintCPrintPrototype (hdr,td);
522
523 PrintCPrintDeclaration (src, td);
524 fprintf (src,"{\n");
525 PrintCPrintLocals (src,td);
526
527 fprintf (src," %s *tmp;\n", td->type->basicType->a.setOf->cTypeRefInfo->cTypeName);
528
529 fprintf (src," if (%s == NULL)\n", valueArgNameG);
530 fprintf (src," return;\n");
531
532 fprintf (src," fprintf (f,\"{ -- SEQUENCE OF -- \\n\");\n");
533
534 fprintf (src," FOR_EACH_LIST_ELMT (tmp, %s)\n", valueArgNameG);
535 fprintf (src," {\n");
536 fprintf (src," Indent (f, indent+ stdIndentG);\n");
537 fprintf (src," %s (f, tmp, indent + stdIndentG);\n", td->type->basicType->a.setOf->cTypeRefInfo->printRoutineName);
538 fprintf (src," if (tmp != (%s*)LAST_LIST_ELMT (%s))\n", td->type->basicType->a.setOf->cTypeRefInfo->cTypeName, valueArgNameG);
539 fprintf (src," fprintf (f,\",\\n\");\n");
540 fprintf (src," }\n");
541 fprintf (src," fprintf (f,\"\\n\");\n");
542 fprintf (src," Indent (f, indent);\n");
543 fprintf (src," fprintf (f,\"}\");\n");
544
545 fprintf (src,"} /* %s */\n\n", td->cTypeDefInfo->printRoutineName);
546
547} /* PrintCSeqOfPrintRoutine */