Add constants and wrap a few more SymbolType fields
[gnome.gobject-introspection] / giscanner / sourcescanner.c
1 /* GObject introspection: public scanner api
2  *
3  * Copyright (C) 2007 Jürg Billeter
4  * Copyright (C) 2008 Johan Dahlin
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22
23 #include "sourcescanner.h"
24
25 GISourceSymbol *
26 gi_source_symbol_new (GISourceSymbolType type)
27 {
28   GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
29   s->ref_count = 1;
30   s->type = type;
31   return s;
32 }
33
34 void
35 ctype_free (GISourceType * type)
36 {
37   g_free (type->name);
38   g_list_foreach (type->child_list, (GFunc)gi_source_symbol_unref, NULL);
39   g_list_free (type->child_list);
40   g_slice_free (GISourceType, type);
41 }
42
43 GISourceSymbol *
44 gi_source_symbol_ref (GISourceSymbol * symbol)
45 {
46   symbol->ref_count++;
47 }
48
49 void
50 gi_source_symbol_unref (GISourceSymbol * symbol)
51 {
52   symbol->ref_count--;
53   if (symbol->ref_count == 0)
54     {
55       g_free (symbol->ident);
56       if (symbol->base_type)
57         ctype_free (symbol->base_type);
58       g_free (symbol->const_string);
59       g_slist_foreach (symbol->directives, (GFunc)gi_source_directive_free, NULL);
60       g_slist_free (symbol->directives);
61       g_slice_free (GISourceSymbol, symbol);
62     }
63 }
64  
65 gboolean
66 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
67 {
68   return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
69 }
70
71 /* use specified type as base type of symbol */
72 void
73 gi_source_symbol_merge_type (GISourceSymbol *symbol,
74                              GISourceType   *type)
75 {
76   GISourceType **foundation_type = &(symbol->base_type);
77
78   while (*foundation_type != NULL)
79     {
80       foundation_type = &((*foundation_type)->base_type);
81     }
82   *foundation_type = type;
83 }
84
85
86 GISourceType *
87 gi_source_type_new (GISourceTypeType type)
88 {
89   GISourceType *t = g_slice_new0 (GISourceType);
90   t->type = type;
91   return t;
92 }
93
94 GISourceType *
95 gi_source_type_copy (GISourceType * type)
96 {
97   GList *l;
98   GISourceType *result = g_slice_new0 (GISourceType);
99   result->type = type->type;
100   result->storage_class_specifier = type->storage_class_specifier;
101   result->type_qualifier = type->type_qualifier;
102   result->function_specifier = type->function_specifier;
103   if (type->name)
104     result->name = g_strdup (type->name);
105   if (type->base_type)
106     result->base_type = gi_source_type_copy (type->base_type);
107   for (l = type->child_list; l; l = l->next)
108     result->child_list = g_list_append (result->child_list, gi_source_symbol_ref (l->data));
109   return result;
110 }
111
112 GISourceType *
113 gi_source_basic_type_new (const char *name)
114 {
115   GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
116   basic_type->name = g_strdup (name);
117   return basic_type;
118 }
119
120 GISourceType *
121 gi_source_typedef_new (const char *name)
122 {
123   GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
124   typedef_->name = g_strdup (name);
125   return typedef_;
126 }
127
128 GISourceType *
129 gi_source_struct_new (const char *name)
130 {
131   GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
132   struct_->name = g_strdup (name);
133   return struct_;
134 }
135
136 GISourceType *
137 gi_source_union_new (const char *name)
138 {
139   GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
140   union_->name = g_strdup (name);
141   return union_;
142 }
143
144 GISourceType *
145 gi_source_enum_new (const char *name)
146 {
147   GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
148   enum_->name = g_strdup (name);
149   return enum_;
150 }
151
152 GISourceType *
153 gi_source_pointer_new (GISourceType * base_type)
154 {
155   GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
156   if (base_type != NULL)
157     pointer->base_type = gi_source_type_copy (base_type);
158   return pointer;
159 }
160
161 GISourceType *
162 gi_source_array_new (void)
163 {
164   GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
165   return array;
166 }
167
168 GISourceType *
169 gi_source_function_new (void)
170 {
171   GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
172   return func;
173 }
174
175 GISourceDirective *
176 gi_source_directive_new (const gchar *name,
177                          const gchar *value,
178                          GSList *options)
179 {
180   GISourceDirective *directive;
181     
182   directive = g_slice_new (GISourceDirective);
183   directive->name = g_strdup (name);
184   directive->value = g_strdup (value);
185   directive->options = options;
186   return directive;
187 }
188
189 void
190 gi_source_directive_free (GISourceDirective *directive)
191 {
192   g_free (directive->name);
193   g_free (directive->value);
194   g_slice_free (GISourceDirective, directive);
195 }
196
197 GISourceScanner *
198 gi_source_scanner_new (void)
199 {
200   GISourceScanner * scanner;
201
202   scanner = g_slice_new0 (GISourceScanner);
203   scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
204                                                   g_free, NULL);
205   scanner->directives_map = g_hash_table_new (g_str_hash, g_str_equal);
206   scanner->struct_or_union_or_enum_table =
207     g_hash_table_new_full (g_str_hash, g_str_equal,
208                            g_free, (GDestroyNotify)gi_source_symbol_unref);
209
210   return scanner;
211 }
212
213 void
214 gi_source_scanner_free (GISourceScanner *scanner)
215 {
216   g_free (scanner->current_filename);
217
218   g_hash_table_destroy (scanner->directives_map);
219   g_hash_table_destroy (scanner->typedef_table);
220   g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
221
222   g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
223   g_slist_free (scanner->symbols);
224
225   g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
226   g_list_free (scanner->filenames);
227
228 }
229
230 gboolean
231 gi_source_scanner_is_typedef (GISourceScanner *scanner,
232                               const char      *name)
233 {
234   gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
235   return b;
236 }
237
238 void
239 gi_source_scanner_set_macro_scan (GISourceScanner  *scanner,
240                                   gboolean          macro_scan)
241 {
242   scanner->macro_scan = macro_scan;
243 }
244
245 void
246 gi_source_scanner_add_symbol (GISourceScanner  *scanner,
247                               GISourceSymbol   *symbol)
248 {
249   gboolean found_filename = FALSE;
250   GList *l;
251
252   for (l = scanner->filenames; l != NULL; l = l->next)
253     {
254       if (strcmp (l->data, scanner->current_filename) == 0)
255         {
256           found_filename = TRUE;
257           break;
258         }
259     }
260
261   if (found_filename || scanner->macro_scan)
262     scanner->symbols = g_slist_prepend (scanner->symbols,
263                                         gi_source_symbol_ref (symbol));
264
265   switch (symbol->type)
266     {
267     case CSYMBOL_TYPE_TYPEDEF:
268       g_hash_table_insert (scanner->typedef_table,
269                            g_strdup (symbol->ident),
270                            GINT_TO_POINTER (TRUE));
271       break;
272     case CSYMBOL_TYPE_STRUCT:
273     case CSYMBOL_TYPE_UNION:
274     case CSYMBOL_TYPE_ENUM:
275       g_hash_table_insert (scanner->struct_or_union_or_enum_table,
276                            g_strdup (symbol->ident),
277                            gi_source_symbol_ref (symbol));
278       break;
279     default:
280       break;
281     }
282 }
283
284 GSList *
285 gi_source_scanner_get_symbols (GISourceScanner  *scanner)
286 {
287   return g_slist_reverse (scanner->symbols);
288 }