gen_free.c revision 55682
1249259Sdim/*
2249259Sdim * Copyright (c) 1997 - 1999 Kungliga Tekniska H�gskolan
3249259Sdim * (Royal Institute of Technology, Stockholm, Sweden).
4249259Sdim * All rights reserved.
5249259Sdim *
6249259Sdim * Redistribution and use in source and binary forms, with or without
7249259Sdim * modification, are permitted provided that the following conditions
8249259Sdim * are met:
9249259Sdim *
10249259Sdim * 1. Redistributions of source code must retain the above copyright
11249259Sdim *    notice, this list of conditions and the following disclaimer.
12249259Sdim *
13249259Sdim * 2. Redistributions in binary form must reproduce the above copyright
14249259Sdim *    notice, this list of conditions and the following disclaimer in the
15249259Sdim *    documentation and/or other materials provided with the distribution.
16249259Sdim *
17249259Sdim * 3. Neither the name of the Institute nor the names of its contributors
18249259Sdim *    may be used to endorse or promote products derived from this software
19249259Sdim *    without specific prior written permission.
20249259Sdim *
21249259Sdim * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22249259Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23249259Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24249259Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25249259Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26251662Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27249259Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28263508Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29249259Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30249259Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31249259Sdim * SUCH DAMAGE.
32249259Sdim */
33249259Sdim
34249259Sdim#include "gen_locl.h"
35249259Sdim
36249259SdimRCSID("$Id: gen_free.c,v 1.7 1999/12/02 17:05:02 joda Exp $");
37249259Sdim
38249259Sdimstatic void
39249259Sdimfree_primitive (const char *typename, const char *name)
40249259Sdim{
41249259Sdim    fprintf (codefile, "free_%s(%s);\n", typename, name);
42249259Sdim}
43249259Sdim
44249259Sdimstatic void
45249259Sdimfree_type (const char *name, const Type *t)
46249259Sdim{
47249259Sdim  switch (t->type) {
48249259Sdim  case TType:
49249259Sdim#if 0
50249259Sdim      free_type (name, t->symbol->type);
51249259Sdim#endif
52249259Sdim      fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
53249259Sdim      break;
54249259Sdim  case TInteger:
55249259Sdim      break;
56263508Sdim  case TOctetString:
57263508Sdim      free_primitive ("octet_string", name);
58263508Sdim      break;
59249259Sdim  case TBitString: {
60249259Sdim      break;
61263508Sdim  }
62263508Sdim  case TSequence: {
63249259Sdim      Member *m;
64249259Sdim      int tag = -1;
65249259Sdim
66249259Sdim      if (t->members == NULL)
67249259Sdim	  break;
68249259Sdim
69249259Sdim      for (m = t->members; m && tag != m->val; m = m->next) {
70249259Sdim	  char *s;
71249259Sdim
72249259Sdim	  asprintf (&s, "%s(%s)->%s",
73249259Sdim		    m->optional ? "" : "&", name, m->gen_name);
74263508Sdim	  if(m->optional)
75249259Sdim	      fprintf(codefile, "if(%s) {\n", s);
76249259Sdim	  free_type (s, m->type);
77249259Sdim	  if(m->optional)
78249259Sdim	      fprintf(codefile,
79249259Sdim		      "free(%s);\n"
80249259Sdim		      "}\n",s);
81249259Sdim	  if (tag == -1)
82249259Sdim	      tag = m->val;
83249259Sdim	  free (s);
84249259Sdim      }
85249259Sdim      break;
86249259Sdim  }
87249259Sdim  case TSequenceOf: {
88249259Sdim      char *n;
89249259Sdim
90249259Sdim      fprintf (codefile, "while((%s)->len){\n", name);
91249259Sdim      asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name);
92249259Sdim      free_type(n, t->subtype);
93263508Sdim      fprintf(codefile,
94249259Sdim	      "(%s)->len--;\n"
95249259Sdim	      "}\n",
96249259Sdim	      name);
97249259Sdim      fprintf(codefile,
98249259Sdim	      "free((%s)->val);\n", name);
99249259Sdim      free(n);
100249259Sdim      break;
101249259Sdim  }
102249259Sdim  case TGeneralizedTime:
103249259Sdim      break;
104249259Sdim  case TGeneralString:
105249259Sdim      free_primitive ("general_string", name);
106249259Sdim      break;
107249259Sdim  case TApplication:
108249259Sdim      free_type (name, t->subtype);
109249259Sdim      break;
110249259Sdim  default :
111249259Sdim      abort ();
112249259Sdim  }
113249259Sdim}
114249259Sdim
115249259Sdimvoid
116249259Sdimgenerate_type_free (const Symbol *s)
117249259Sdim{
118249259Sdim  fprintf (headerfile,
119249259Sdim	   "void   free_%s  (%s *);\n",
120249259Sdim	   s->gen_name, s->gen_name);
121249259Sdim
122249259Sdim  fprintf (codefile, "void\n"
123249259Sdim	   "free_%s(%s *data)\n"
124249259Sdim	   "{\n",
125249259Sdim	   s->gen_name, s->gen_name);
126249259Sdim
127249259Sdim  free_type ("data", s->type);
128249259Sdim  fprintf (codefile, "}\n\n");
129249259Sdim}
130249259Sdim
131249259Sdim