Merge in the gir-compiler branch. Thanks to Philip and Colin for their
[platform/upstream/gobject-introspection.git] / 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   return symbol;
48 }
49
50 void
51 gi_source_symbol_unref (GISourceSymbol * symbol)
52 {
53   if (!symbol)
54     return;
55   symbol->ref_count--;
56   if (symbol->ref_count == 0)
57     {
58       g_free (symbol->ident);
59       if (symbol->base_type)
60         ctype_free (symbol->base_type);
61       g_free (symbol->const_string);
62       g_slist_foreach (symbol->directives, (GFunc)gi_source_directive_free, NULL);
63       g_slist_free (symbol->directives);
64       g_slice_free (GISourceSymbol, symbol);
65     }
66 }
67  
68 gboolean
69 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
70 {
71   return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
72 }
73
74 /* use specified type as base type of symbol */
75 void
76 gi_source_symbol_merge_type (GISourceSymbol *symbol,
77                              GISourceType   *type)
78 {
79   GISourceType **foundation_type = &(symbol->base_type);
80
81   while (*foundation_type != NULL)
82     {
83       foundation_type = &((*foundation_type)->base_type);
84     }
85   *foundation_type = type;
86 }
87
88
89 GISourceType *
90 gi_source_type_new (GISourceTypeType type)
91 {
92   GISourceType *t = g_slice_new0 (GISourceType);
93   t->type = type;
94   return t;
95 }
96
97 GISourceType *
98 gi_source_type_copy (GISourceType * type)
99 {
100   GList *l;
101   GISourceType *result = g_slice_new0 (GISourceType);
102   result->type = type->type;
103   result->storage_class_specifier = type->storage_class_specifier;
104   result->type_qualifier = type->type_qualifier;
105   result->function_specifier = type->function_specifier;
106   if (type->name)
107     result->name = g_strdup (type->name);
108   if (type->base_type)
109     result->base_type = gi_source_type_copy (type->base_type);
110   for (l = type->child_list; l; l = l->next)
111     result->child_list = g_list_append (result->child_list, gi_source_symbol_ref (l->data));
112   return result;
113 }
114
115 GISourceType *
116 gi_source_basic_type_new (const char *name)
117 {
118   GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
119   basic_type->name = g_strdup (name);
120   return basic_type;
121 }
122
123 GISourceType *
124 gi_source_typedef_new (const char *name)
125 {
126   GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
127   typedef_->name = g_strdup (name);
128   return typedef_;
129 }
130
131 GISourceType *
132 gi_source_struct_new (const char *name)
133 {
134   GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
135   struct_->name = g_strdup (name);
136   return struct_;
137 }
138
139 GISourceType *
140 gi_source_union_new (const char *name)
141 {
142   GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
143   union_->name = g_strdup (name);
144   return union_;
145 }
146
147 GISourceType *
148 gi_source_enum_new (const char *name)
149 {
150   GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
151   enum_->name = g_strdup (name);
152   return enum_;
153 }
154
155 GISourceType *
156 gi_source_pointer_new (GISourceType * base_type)
157 {
158   GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
159   if (base_type != NULL)
160     pointer->base_type = gi_source_type_copy (base_type);
161   return pointer;
162 }
163
164 GISourceType *
165 gi_source_array_new (void)
166 {
167   GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
168   return array;
169 }
170
171 GISourceType *
172 gi_source_function_new (void)
173 {
174   GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
175   return func;
176 }
177
178 GISourceDirective *
179 gi_source_directive_new (const gchar *name,
180                          const gchar *value,
181                          GSList *options)
182 {
183   GISourceDirective *directive;
184     
185   directive = g_slice_new (GISourceDirective);
186   directive->name = g_strdup (name);
187   directive->value = g_strdup (value);
188   directive->options = options;
189   return directive;
190 }
191
192 void
193 gi_source_directive_free (GISourceDirective *directive)
194 {
195   g_free (directive->name);
196   g_free (directive->value);
197   g_slice_free (GISourceDirective, directive);
198 }
199
200 GISourceScanner *
201 gi_source_scanner_new (void)
202 {
203   GISourceScanner * scanner;
204
205   scanner = g_slice_new0 (GISourceScanner);
206   scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
207                                                   g_free, NULL);
208   scanner->directives_map = g_hash_table_new (g_str_hash, g_str_equal);
209   scanner->struct_or_union_or_enum_table =
210     g_hash_table_new_full (g_str_hash, g_str_equal,
211                            g_free, (GDestroyNotify)gi_source_symbol_unref);
212
213   return scanner;
214 }
215
216 void
217 gi_source_scanner_free (GISourceScanner *scanner)
218 {
219   g_free (scanner->current_filename);
220
221   g_hash_table_destroy (scanner->directives_map);
222   g_hash_table_destroy (scanner->typedef_table);
223   g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
224
225   g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
226   g_slist_free (scanner->symbols);
227
228   g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
229   g_list_free (scanner->filenames);
230
231 }
232
233 gboolean
234 gi_source_scanner_is_typedef (GISourceScanner *scanner,
235                               const char      *name)
236 {
237   gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
238   return b;
239 }
240
241 void
242 gi_source_scanner_set_macro_scan (GISourceScanner  *scanner,
243                                   gboolean          macro_scan)
244 {
245   scanner->macro_scan = macro_scan;
246 }
247
248 void
249 gi_source_scanner_add_symbol (GISourceScanner  *scanner,
250                               GISourceSymbol   *symbol)
251 {
252   gboolean found_filename = FALSE;
253   GList *l;
254
255   g_assert (scanner->current_filename);
256   for (l = scanner->filenames; l != NULL; l = l->next)
257     {
258       if (strcmp (l->data, scanner->current_filename) == 0)
259         {
260           found_filename = TRUE;
261           break;
262         }
263     }
264
265   if (found_filename || scanner->macro_scan)
266     scanner->symbols = g_slist_prepend (scanner->symbols,
267                                         gi_source_symbol_ref (symbol));
268
269   switch (symbol->type)
270     {
271     case CSYMBOL_TYPE_TYPEDEF:
272       g_hash_table_insert (scanner->typedef_table,
273                            g_strdup (symbol->ident),
274                            GINT_TO_POINTER (TRUE));
275       break;
276     case CSYMBOL_TYPE_STRUCT:
277     case CSYMBOL_TYPE_UNION:
278     case CSYMBOL_TYPE_ENUM:
279       g_hash_table_insert (scanner->struct_or_union_or_enum_table,
280                            g_strdup (symbol->ident),
281                            gi_source_symbol_ref (symbol));
282       break;
283     default:
284       break;
285     }
286 }
287
288 GSList *
289 gi_source_scanner_get_symbols (GISourceScanner  *scanner)
290 {
291   return g_slist_reverse (scanner->symbols);
292 }
293
294 GSList *
295 gi_source_scanner_get_directives(GISourceScanner  *scanner,
296                                  const gchar      *name)
297 {
298   return g_hash_table_lookup (scanner->directives_map, name);
299 }