Header generation working.
[portablexdr.git] / rpcgen_ast.c
1 /* -*- C -*-
2  * rpcgen - Generate XDR bindings automatically.
3  * Copyright (C) 2008 Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "rpcgen_int.h"
27
28 struct type *
29 new_type (enum type_enum type, int sgn, char *ident, char *len)
30 {
31   struct type *r = malloc (sizeof *r);
32   r->type = type;
33   r->sgn = sgn;
34   r->ident = ident;
35   r->len = len;
36   return r;
37 }
38
39 void
40 free_type (struct type *t)
41 {
42   if (t) {
43     free (t->ident);
44     free (t->len);
45     free (t);
46   }
47 }
48
49 struct decl *
50 new_decl (enum decl_type decl_type, struct type *type,
51           char *ident, char *len)
52 {
53   struct decl *r = malloc (sizeof *r);
54   r->decl_type = decl_type;
55   r->type = type;
56   r->ident = ident;
57   r->len = len;
58   return r;
59 }
60
61 void
62 free_decl (struct decl *d)
63 {
64   if (d) {
65     free_type (d->type);
66     free (d->ident);
67     free (d->len);
68     free (d);
69   }
70 }
71
72 struct enum_value *
73 new_enum_value (char *ident, char *value)
74 {
75   struct enum_value *r = malloc (sizeof *r);
76   r->ident = ident;
77   r->value = value;
78   return r;
79 }
80
81 void
82 free_enum_value (struct enum_value *v)
83 {
84   if (v) {
85     free (v->ident);
86     free (v->value);
87     free (v);
88   }
89 }
90
91 struct union_case *
92 new_union_case (enum union_case_type type, char *const_, struct decl *decl)
93 {
94   struct union_case *r = malloc (sizeof *r);
95   r->type = type;
96   r->const_ = const_;
97   r->decl = decl;
98   return r;
99 }
100
101 void
102 free_union_case (struct union_case *c)
103 {
104   if (c) {
105     free (c->const_);
106     free_decl (c->decl);
107     free (c);
108   }
109 }
110
111 struct cons *
112 new_cons (struct cons *next, void *ptr, free_fn free)
113 {
114   struct cons *r = malloc (sizeof *r);
115   r->next = next;
116   r->ptr = ptr;
117   r->free = free;
118   return r;
119 }
120
121 static struct cons *
122 list_rev_append (struct cons *xs1, struct cons *xs2)
123 {
124   if (!xs1) return xs2;
125   else {
126     struct cons *tail = xs1->next;
127     xs1->next = xs2;
128     return list_rev_append (tail, xs1);
129   }
130 }
131
132 struct cons *
133 list_rev (struct cons *xs)
134 {
135   return list_rev_append (xs, NULL);
136 }
137
138 void
139 list_free (struct cons *xs)
140 {
141   if (xs) {
142     struct cons *next = xs->next;
143     if (xs->free) xs->free (xs->ptr);
144     free (xs);
145     list_free (next);
146   }
147 }