2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 * compiler/back-ends/c-gen/gen-free.c - routines for printing C hierachical free routines
24 * Copyright (C) 1991, 1992 Michael Sample
25 * and the University of British Columbia
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.
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
37 * Revision 1.2 2001/05/05 00:59:28 rmurphy
38 * Adding darwin license headers
40 * Revision 1.1.1.1 1999/03/16 18:06:42 aram
41 * Originals from SMIME Free Library.
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.
46 * changed `_' to `-' in file names.
48 * Revision 1.2 1994/09/01 00:23:29 rj
49 * snacc_config.h and other superfluous .h files removed.
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.
59 #include "asn1module.h"
63 #include "type-info.h"
68 static char *returnTypeG
= "void";
69 static char *valueArgNameG
= "v";
70 static CRules
*genFreeCRulesG
;
72 /* non-exported prototypes */
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
));
88 PrintCFree
PARAMS ((src
, hdr
, r
, mods
, m
, td
),
92 ModuleList
*mods _AND_
98 CTypeId rhsTypeId
; /* cTypeId of the type that defined this typedef */
102 ctdi
= td
->cTypeDefInfo
;
103 if ((ctdi
== NULL
) || (td
->type
->cTypeRefInfo
== NULL
))
105 fprintf (stderr
,"PrintCFree: ERROR - no type info\n");
109 if (!ctdi
->genFreeRoutine
)
112 rhsTypeId
= td
->type
->cTypeRefInfo
->cTypeId
;
119 PrintCFreeDefine (hdr
, td
);
120 fprintf (hdr
,"\n\n");
124 PrintCFreePrototype (hdr
, td
);
125 PrintCFreeDeclaration (src
, td
);
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");
137 PrintCFreePrototype (hdr
, td
);
138 PrintCFreeDeclaration (src
, td
);
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");
151 PrintCFreePrototype (hdr
, td
);
152 PrintCFreeDeclaration (src
, td
);
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");
169 fprintf (stderr
,"PrintCFree: ERROR - unknown c type id\n");
178 * Prints prototype for encode routine in hdr file
181 PrintCFreePrototype
PARAMS ((hdr
, td
),
187 ctdi
= td
->cTypeDefInfo
;
188 fprintf (hdr
,"%s %s PROTO ((%s *v));\n", returnTypeG
, ctdi
->freeRoutineName
, ctdi
->cTypeName
);
190 } /* PrintCFreePrototype */
195 * Prints declarations of encode routine for the given type def
198 PrintCFreeDeclaration
PARAMS ((src
, td
),
204 ctdi
= td
->cTypeDefInfo
;
205 fprintf (src
,"%s\n%s PARAMS ((v),\n%s *v)\n", returnTypeG
, ctdi
->freeRoutineName
, ctdi
->cTypeName
);
207 } /* PrintCFreeDeclaration */
213 PrintCFreeDefine
PARAMS ((hdr
, td
),
218 fprintf(hdr
, "#define %s %s ", td
->cTypeDefInfo
->freeRoutineName
, td
->type
->cTypeRefInfo
->freeRoutineName
);
221 fprintf(hdr, "#define %s(v) ", td->cTypeDefInfo->freeRoutineName);
222 fprintf (hdr, "%s (v)", td->type->cTypeRefInfo->freeRoutineName);
224 } /* PrintCFreeDefine */
230 PrintCFreeLocals
PARAMS ((src
, td
),
236 if ((td
->type
->basicType
->choiceId
== BASICTYPE_SETOF
) ||
237 (td
->type
->basicType
->choiceId
== BASICTYPE_SEQUENCEOF
))
239 fprintf (src
," AsnListNode *l;\n");
240 fprintf (src
," AsnListNode *tmp;\n");
243 } /* PrintCFreeLocals */
248 PrintCFreeElmts
PARAMS ((src
, td
, parent
, elmts
, varName
),
252 NamedTypeList
*elmts _AND_
259 fprintf (src
,"/* ERROR? - expected elmts for this type*/\n");
263 FOR_EACH_LIST_ELMT (e
, elmts
)
264 PrintCElmtFree (src
, td
, parent
, e
->type
, varName
);
266 } /* PrintCBerElmtsEncodeCode */
271 * Prints code for encoding the elmts of a SEQ or SET
274 PrintCElmtFree
PARAMS ((src
, td
, parent
, e
, varName
),
282 char elmtVarRef
[MAX_VAR_REF
];
285 if ((e
== NULL
) || (e
->cTypeRefInfo
== NULL
))
288 ctri
= e
->cTypeRefInfo
;
290 /* build ref to the elmt */
291 MakeVarPtrRef (genFreeCRulesG
, td
, parent
, e
, varName
, elmtVarRef
);
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
);
297 /* free contents of elmt first */
298 switch (ctri
->cTypeId
)
304 fprintf (src
," %s (%s);\n", ctri
->freeRoutineName
, elmtVarRef
);
308 PrintCFreeListElmts (src
, td
, e
, elmtVarRef
);
312 * this follwing shouldn't happen since embedded
313 * choices/struct are moved to separate typedefs
317 PrintCFreeChoiceElmts (src
, td
, e
, elmtVarRef
);
321 PrintCFreeElmts (src
, td
, e
, e
->basicType
->a
.set
, elmtVarRef
);
329 fprintf (stderr
,"PrintCElmtFree: ERROR - unknown c type id\n");
333 /* free elmt itself if it is ref'd by ptr */
335 fprintf (src
," Asn1Free (%s);\n",elmtVarRef
);
337 /* write closing brkt for NULL check for optional elmts */
338 if (e
->optional
|| (e
->defaultVal
!= NULL
))
339 fprintf (src
, " }\n");
343 } /* PrintCElmtFree */
347 PrintCFreeListDefine
PARAMS ((hdr
, td
),
351 fprintf(hdr
, "#define %s(v) ", td
->cTypeDefInfo
->freeRoutineName
);
352 fprintf (hdr
, "ASN1_FREE_LIST (v, %s)", td
->type
->cTypeRefInfo
->freeRoutineName
);
357 PrintCFreeListRoutineBody
PARAMS ((src
, td
, t
, varName
),
367 fprintf (src
," for (l = FIRST_LIST_NODE (%s); l != NULL; )\n", varName
);
368 fprintf (src
," {\n");
370 e
= t
->basicType
->a
.setOf
;
371 ctri
= e
->cTypeRefInfo
;
372 elmtVarRef
= "(l->data)";
373 switch (ctri
->cTypeId
)
377 fprintf (src
," %s (%s);\n", ctri
->freeRoutineName
, elmtVarRef
);
381 PrintCFreeListElmts (src
, td
, e
, elmtVarRef
);
385 * this follwing shouldn't happen since embedded
386 * choices/struct are moved to separate typedefs
390 PrintCFreeChoiceElmts (src
, td
, e
, elmtVarRef
);
394 PrintCFreeElmts (src
, td
, e
, e
->basicType
->a
.set
, elmtVarRef
);
402 fprintf (stderr
,"PrintCElmtFree: ERROR - unknown c type id\n");
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");
414 PrintCFreeListElmts
PARAMS ((src
, td
, t
, varName
),
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");
431 e
= t
->basicType
->a
.setOf
;
432 ctri
= e
->cTypeRefInfo
;
433 elmtVarRef
= "(l->data)";
434 switch (ctri
->cTypeId
)
438 fprintf (src
," %s (%s);\n", ctri
->freeRoutineName
, elmtVarRef
);
442 PrintCFreeListElmts (src
, td
, e
, elmtVarRef
);
446 * this follwing shouldn't happen since embedded
447 * choices/struct are moved to separate typedefs
451 PrintCFreeChoiceElmts (src
, td
, e
, elmtVarRef
);
455 PrintCFreeElmts (src
, td
, e
, e
->basicType
->a
.set
, elmtVarRef
);
463 fprintf (stderr
,"PrintCElmtFree: ERROR - unknown c type id\n");
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 */
478 PrintCFreeChoiceElmts
PARAMS ((src
, td
, t
, varName
),
488 ctri
= t
->cTypeRefInfo
;
490 fprintf (src
," switch (%s->%s)\n {\n", varName
, ctri
->choiceIdEnumFieldName
);
492 FOR_EACH_LIST_ELMT (e
, t
->basicType
->a
.choice
)
494 tmp
= (void*)CURR_LIST_NODE (t
->basicType
->a
.choice
);
499 ctri
= e
->type
->cTypeRefInfo
;
501 if (e
->type
->cTypeRefInfo
== NULL
)
502 fprintf (src
, " case ????:\n");
503 else if (ctri
->isPtr
)
505 fprintf (src
, " case %s:\n", ctri
->choiceIdSymbol
);
506 PrintCElmtFree (src
, td
, t
, e
->type
, varName
);
507 fprintf (src
," break;\n\n");
510 SET_CURR_LIST_NODE (t
->basicType
->a
.choice
, tmp
);
513 fprintf (src
, " }\n");