]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/c-gen/gen-free.c
0a90c179afd009cfe8d6905e1a84092ddea6f1d5
[apple/security.git] / SecuritySNACCRuntime / compiler / back-ends / c-gen / gen-free.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/c-gen/gen-free.c - routines for printing C hierachical free 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-free.c,v 1.1.1.1 2001/05/18 23:14:09 mb Exp $
33 * $Log: gen-free.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:42:47 rj
44 * file name has been shortened for redundant part: c-gen/gen-c-free -> c-gen/gen-free.
45 *
46 * changed `_' to `-' in file names.
47 *
48 * Revision 1.2 1994/09/01 00:23:29 rj
49 * snacc_config.h and other superfluous .h files removed.
50 *
51 * Revision 1.1 1994/08/28 09:48:26 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-free.h"
67
68 static char *returnTypeG = "void";
69 static char *valueArgNameG = "v";
70 static CRules *genFreeCRulesG;
71
72 /* non-exported prototypes */
73
74 static void PrintCFreePrototype PROTO ((FILE *hdr, TypeDef *td));
75 static void PrintCFreeDeclaration PROTO ((FILE *src, TypeDef *td));
76 static void PrintCFreeDefine PROTO ((FILE *hdr, TypeDef *td));
77 static void PrintCFreeLocals PROTO ((FILE *src,TypeDef *td));
78 static void PrintCFreeElmts PROTO ((FILE *src, TypeDef *td, Type *parent, NamedTypeList *elmts, char *varName));
79 static void PrintCElmtFree PROTO ((FILE *src, TypeDef *td, Type *parent, Type *e, char *varName));
80 static void PrintCFreeListElmts PROTO ((FILE *src, TypeDef *td, Type *t, char *varName));
81 static void PrintCFreeListRoutineBody PROTO ((FILE *src, TypeDef *td, Type *t,char *varName));
82 static void PrintCFreeListDefine PROTO ((FILE *hdr,TypeDef *td));
83 static void PrintCFreeChoiceElmts PROTO ((FILE *src, TypeDef *td, Type *t, char *varName));
84
85
86
87 void
88 PrintCFree PARAMS ((src, hdr, r, mods, m, td),
89 FILE *src _AND_
90 FILE *hdr _AND_
91 CRules *r _AND_
92 ModuleList *mods _AND_
93 Module *m _AND_
94 TypeDef *td)
95 {
96 NamedType *e;
97 CTDI *ctdi;
98 CTypeId rhsTypeId; /* cTypeId of the type that defined this typedef */
99
100 genFreeCRulesG = r;
101
102 ctdi = td->cTypeDefInfo;
103 if ((ctdi == NULL) || (td->type->cTypeRefInfo == NULL))
104 {
105 fprintf (stderr,"PrintCFree: ERROR - no type info\n");
106 return;
107 }
108
109 if (!ctdi->genFreeRoutine)
110 return;
111
112 rhsTypeId = td->type->cTypeRefInfo->cTypeId;
113 switch (rhsTypeId)
114 {
115 case C_ANY:
116 case C_ANYDEFINEDBY:
117 case C_LIB:
118 case C_TYPEREF:
119 PrintCFreeDefine (hdr, td);
120 fprintf (hdr,"\n\n");
121 break;
122
123 case C_CHOICE:
124 PrintCFreePrototype (hdr, td);
125 PrintCFreeDeclaration (src, td);
126 fprintf (src,"{\n");
127 PrintCFreeLocals (src, td);
128 fprintf (src," if (%s == NULL)\n", valueArgNameG);
129 fprintf (src," return;\n", valueArgNameG);
130 PrintCFreeChoiceElmts (src, td, td->type, valueArgNameG);
131 fprintf (src,"} /* %s */",td->cTypeDefInfo->freeRoutineName);
132 fprintf (hdr,"\n\n");
133 fprintf (src,"\n\n");
134 break;
135
136 case C_STRUCT:
137 PrintCFreePrototype (hdr, td);
138 PrintCFreeDeclaration (src, td);
139 fprintf (src,"{\n");
140 PrintCFreeLocals (src, td);
141 fprintf (src," if (%s == NULL)\n", valueArgNameG);
142 fprintf (src," return;\n", valueArgNameG);
143 PrintCFreeElmts (src, td, td->type, td->type->basicType->a.set, valueArgNameG);
144 fprintf (src,"} /* %s */", td->cTypeDefInfo->freeRoutineName);
145 fprintf (hdr,"\n\n");
146 fprintf (src,"\n\n");
147 break;
148
149
150 case C_LIST:
151 PrintCFreePrototype (hdr, td);
152 PrintCFreeDeclaration (src, td);
153 fprintf (src,"{\n");
154 PrintCFreeLocals (src, td);
155 fprintf (src," if (%s == NULL)\n", valueArgNameG);
156 fprintf (src," return;\n", valueArgNameG);
157 PrintCFreeListRoutineBody (src, td, td->type, valueArgNameG);
158 fprintf (src,"} /* %s */", td->cTypeDefInfo->freeRoutineName);
159 fprintf (hdr,"\n\n");
160 fprintf (src,"\n\n");
161 break;
162
163
164
165 case C_NO_TYPE:
166 break;
167
168 default:
169 fprintf (stderr,"PrintCFree: ERROR - unknown c type id\n");
170 break;
171 }
172
173 } /* PrintCFree */
174
175
176
177 /*
178 * Prints prototype for encode routine in hdr file
179 */
180 static void
181 PrintCFreePrototype PARAMS ((hdr, td),
182 FILE *hdr _AND_
183 TypeDef *td)
184 {
185 CTDI *ctdi;
186
187 ctdi = td->cTypeDefInfo;
188 fprintf (hdr,"%s %s PROTO ((%s *v));\n", returnTypeG, ctdi->freeRoutineName, ctdi->cTypeName);
189
190 } /* PrintCFreePrototype */
191
192
193
194 /*
195 * Prints declarations of encode routine for the given type def
196 */
197 static void
198 PrintCFreeDeclaration PARAMS ((src, td),
199 FILE *src _AND_
200 TypeDef *td)
201 {
202 CTDI *ctdi;
203
204 ctdi = td->cTypeDefInfo;
205 fprintf (src,"%s\n%s PARAMS ((v),\n%s *v)\n", returnTypeG, ctdi->freeRoutineName, ctdi->cTypeName);
206
207 } /* PrintCFreeDeclaration */
208
209
210
211
212 static void
213 PrintCFreeDefine PARAMS ((hdr, td),
214 FILE *hdr _AND_
215 TypeDef *td)
216 {
217
218 fprintf(hdr, "#define %s %s ", td->cTypeDefInfo->freeRoutineName, td->type->cTypeRefInfo->freeRoutineName);
219
220 /*
221 fprintf(hdr, "#define %s(v) ", td->cTypeDefInfo->freeRoutineName);
222 fprintf (hdr, "%s (v)", td->type->cTypeRefInfo->freeRoutineName);
223 */
224 } /* PrintCFreeDefine */
225
226
227
228
229 static void
230 PrintCFreeLocals PARAMS ((src, td),
231 FILE *src _AND_
232 TypeDef *td)
233 {
234 fprintf (src, "\n");
235
236 if ((td->type->basicType->choiceId == BASICTYPE_SETOF) ||
237 (td->type->basicType->choiceId == BASICTYPE_SEQUENCEOF))
238 {
239 fprintf (src," AsnListNode *l;\n");
240 fprintf (src," AsnListNode *tmp;\n");
241 }
242
243 } /* PrintCFreeLocals */
244
245
246
247 static void
248 PrintCFreeElmts PARAMS ((src, td, parent, elmts, varName),
249 FILE *src _AND_
250 TypeDef *td _AND_
251 Type *parent _AND_
252 NamedTypeList *elmts _AND_
253 char *varName)
254 {
255 NamedType *e;
256
257 if (elmts == NULL)
258 {
259 fprintf (src,"/* ERROR? - expected elmts for this type*/\n");
260 return;
261 }
262
263 FOR_EACH_LIST_ELMT (e, elmts)
264 PrintCElmtFree (src, td, parent, e->type, varName);
265
266 } /* PrintCBerElmtsEncodeCode */
267
268
269
270 /*
271 * Prints code for encoding the elmts of a SEQ or SET
272 */
273 static void
274 PrintCElmtFree PARAMS ((src, td, parent, e, varName),
275 FILE *src _AND_
276 TypeDef *td _AND_
277 Type *parent _AND_
278 Type *e _AND_
279 char *varName)
280 {
281 CTRI *ctri;
282 char elmtVarRef[MAX_VAR_REF];
283 Type *tmpType;
284
285 if ((e == NULL) || (e->cTypeRefInfo == NULL))
286 return;
287
288 ctri = e->cTypeRefInfo;
289
290 /* build ref to the elmt */
291 MakeVarPtrRef (genFreeCRulesG, td, parent, e, varName, elmtVarRef);
292
293 /* if optional then put in NULL check */
294 if (e->optional || (e->defaultVal != NULL))
295 fprintf (src, " if (%s (%s))\n {\n", ctri->optTestRoutineName, elmtVarRef);
296
297 /* free contents of elmt first */
298 switch (ctri->cTypeId)
299 {
300 case C_ANY:
301 case C_ANYDEFINEDBY:
302 case C_LIB:
303 case C_TYPEREF:
304 fprintf (src," %s (%s);\n", ctri->freeRoutineName, elmtVarRef);
305 break;
306
307 case C_LIST:
308 PrintCFreeListElmts (src, td, e, elmtVarRef);
309 break;
310
311 /*
312 * this follwing shouldn't happen since embedded
313 * choices/struct are moved to separate typedefs
314 * in normalize.c.
315 */
316 case C_CHOICE:
317 PrintCFreeChoiceElmts (src, td, e, elmtVarRef);
318 break;
319
320 case C_STRUCT:
321 PrintCFreeElmts (src, td, e, e->basicType->a.set, elmtVarRef);
322 break;
323
324
325 case C_NO_TYPE:
326 break;
327
328 default:
329 fprintf (stderr,"PrintCElmtFree: ERROR - unknown c type id\n");
330 break;
331 }
332
333 /* free elmt itself if it is ref'd by ptr */
334 if (ctri->isPtr)
335 fprintf (src," Asn1Free (%s);\n",elmtVarRef);
336
337 /* write closing brkt for NULL check for optional elmts */
338 if (e->optional || (e->defaultVal != NULL))
339 fprintf (src, " }\n");
340
341 fprintf (src,"\n");
342
343 } /* PrintCElmtFree */
344
345
346 static void
347 PrintCFreeListDefine PARAMS ((hdr, td),
348 FILE *hdr _AND_
349 TypeDef *td)
350 {
351 fprintf(hdr, "#define %s(v) ", td->cTypeDefInfo->freeRoutineName);
352 fprintf (hdr, "ASN1_FREE_LIST (v, %s)", td->type->cTypeRefInfo->freeRoutineName);
353 }
354
355
356 static void
357 PrintCFreeListRoutineBody PARAMS ((src, td, t, varName),
358 FILE *src _AND_
359 TypeDef *td _AND_
360 Type *t _AND_
361 char *varName)
362 {
363 Type *e;
364 CTRI *ctri;
365 char *elmtVarRef;
366
367 fprintf (src," for (l = FIRST_LIST_NODE (%s); l != NULL; )\n", varName);
368 fprintf (src," {\n");
369
370 e = t->basicType->a.setOf;
371 ctri = e->cTypeRefInfo;
372 elmtVarRef = "(l->data)";
373 switch (ctri->cTypeId)
374 {
375 case C_LIB:
376 case C_TYPEREF:
377 fprintf (src," %s (%s);\n", ctri->freeRoutineName, elmtVarRef);
378 break;
379
380 case C_LIST:
381 PrintCFreeListElmts (src, td, e, elmtVarRef);
382 break;
383
384 /*
385 * this follwing shouldn't happen since embedded
386 * choices/struct are moved to separate typedefs
387 * in normalize.c.
388 */
389 case C_CHOICE:
390 PrintCFreeChoiceElmts (src, td, e, elmtVarRef);
391 break;
392
393 case C_STRUCT:
394 PrintCFreeElmts (src, td, e, e->basicType->a.set, elmtVarRef);
395 break;
396
397
398 case C_NO_TYPE:
399 break;
400
401 default:
402 fprintf (stderr,"PrintCElmtFree: ERROR - unknown c type id\n");
403 break;
404 }
405
406 fprintf (src," tmp = l->next;\n");
407 fprintf (src," Asn1Free (l->data);\n");
408 fprintf (src," Asn1Free (l);\n");
409 fprintf (src," l = tmp;\n");
410 fprintf (src," }\n");
411 }
412
413 static void
414 PrintCFreeListElmts PARAMS ((src, td, t, varName),
415 FILE *src _AND_
416 TypeDef *td _AND_
417 Type *t _AND_
418 char *varName)
419 {
420 Type *e;
421 CTRI *ctri;
422 char *elmtVarRef;
423
424 fprintf (src," {\n");
425 fprintf (src," AsnListNode *l;\n");
426 fprintf (src," AsnListNode *tmp;\n");
427 fprintf (src," for (l = FIRST_LIST_NODE (%s); l != NULL; )\n", varName);
428 fprintf (src," {\n");
429
430
431 e = t->basicType->a.setOf;
432 ctri = e->cTypeRefInfo;
433 elmtVarRef = "(l->data)";
434 switch (ctri->cTypeId)
435 {
436 case C_LIB:
437 case C_TYPEREF:
438 fprintf (src," %s (%s);\n", ctri->freeRoutineName, elmtVarRef);
439 break;
440
441 case C_LIST:
442 PrintCFreeListElmts (src, td, e, elmtVarRef);
443 break;
444
445 /*
446 * this follwing shouldn't happen since embedded
447 * choices/struct are moved to separate typedefs
448 * in normalize.c.
449 */
450 case C_CHOICE:
451 PrintCFreeChoiceElmts (src, td, e, elmtVarRef);
452 break;
453
454 case C_STRUCT:
455 PrintCFreeElmts (src, td, e, e->basicType->a.set, elmtVarRef);
456 break;
457
458
459 case C_NO_TYPE:
460 break;
461
462 default:
463 fprintf (stderr,"PrintCElmtFree: ERROR - unknown c type id\n");
464 break;
465 }
466
467 fprintf (src," tmp = l->next;\n");
468 fprintf (src," Asn1Free (l->data);\n");
469 fprintf (src," Asn1Free (l);\n");
470 fprintf (src," l = tmp;\n");
471 fprintf (src," }\n");
472 fprintf (src," }\n");
473 } /* PrintCFreeListELmts */
474
475
476
477 static void
478 PrintCFreeChoiceElmts PARAMS ((src, td, t, varName),
479 FILE *src _AND_
480 TypeDef *td _AND_
481 Type *t _AND_
482 char *varName)
483 {
484 NamedType *e;
485 CTRI *ctri;
486 void *tmp;
487
488 ctri = t->cTypeRefInfo;
489
490 fprintf (src," switch (%s->%s)\n {\n", varName, ctri->choiceIdEnumFieldName);
491
492 FOR_EACH_LIST_ELMT (e, t->basicType->a.choice)
493 {
494 tmp = (void*)CURR_LIST_NODE (t->basicType->a.choice);
495
496 if (e->type == NULL)
497 continue;
498
499 ctri = e->type->cTypeRefInfo;
500
501 if (e->type->cTypeRefInfo == NULL)
502 fprintf (src, " case ????:\n");
503 else if (ctri->isPtr)
504 {
505 fprintf (src, " case %s:\n", ctri->choiceIdSymbol);
506 PrintCElmtFree (src, td, t, e->type, varName);
507 fprintf (src," break;\n\n");
508 }
509
510 SET_CURR_LIST_NODE (t->basicType->a.choice, tmp);
511 }
512
513 fprintf (src, " }\n");
514 }