Added Gnulib intprops module.
[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)
30 {
31   struct type *r = malloc (sizeof *r);
32   r->type = type;
33   r->sgn = sgn;
34   r->ident = ident;
35   return r;
36 }
37
38 void
39 free_type (struct type *t)
40 {
41   if (t) {
42     free (t->ident);
43     free (t);
44   }
45 }
46
47 struct decl *
48 new_decl (enum decl_type decl_type, struct type *type,
49           char *ident, char *len)
50 {
51   struct decl *r = malloc (sizeof *r);
52   r->decl_type = decl_type;
53   r->type = type;
54   r->ident = ident;
55   r->len = len;
56   return r;
57 }
58
59 void
60 free_decl (struct decl *d)
61 {
62   if (d) {
63     free_type (d->type);
64     free (d->ident);
65     free (d->len);
66     free (d);
67   }
68 }
69
70 struct enum_value *
71 new_enum_value (char *ident, char *value)
72 {
73   struct enum_value *r = malloc (sizeof *r);
74   r->ident = ident;
75   r->value = value;
76   return r;
77 }
78
79 void
80 free_enum_value (struct enum_value *v)
81 {
82   if (v) {
83     free (v->ident);
84     free (v->value);
85     free (v);
86   }
87 }
88
89 struct union_case *
90 new_union_case (enum union_case_type type, char *const_, struct decl *decl)
91 {
92   struct union_case *r = malloc (sizeof *r);
93   r->type = type;
94   r->const_ = const_;
95   r->decl = decl;
96   return r;
97 }
98
99 void
100 free_union_case (struct union_case *c)
101 {
102   if (c) {
103     free (c->const_);
104     free_decl (c->decl);
105     free (c);
106   }
107 }
108
109 struct cons *
110 new_cons (struct cons *next, void *ptr, free_fn free)
111 {
112   struct cons *r = malloc (sizeof *r);
113   r->next = next;
114   r->ptr = ptr;
115   r->free = free;
116   return r;
117 }
118
119 static struct cons *
120 list_rev_append (struct cons *xs1, struct cons *xs2)
121 {
122   if (!xs1) return xs2;
123   else {
124     struct cons *tail = xs1->next;
125     xs1->next = xs2;
126     return list_rev_append (tail, xs1);
127   }
128 }
129
130 struct cons *
131 list_rev (struct cons *xs)
132 {
133   return list_rev_append (xs, NULL);
134 }
135
136 void
137 list_free (struct cons *xs)
138 {
139   if (xs) {
140     struct cons *next = xs->next;
141     if (xs->free) xs->free (xs->ptr);
142     free (xs);
143     list_free (next);
144   }
145 }