79d89cdc1ab89931f5af36ca8181b0b49dce7fef
[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 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 #include <string.h>
25
26 GISourceSymbol *
27 gi_source_symbol_new (GISourceSymbolType type)
28 {
29   GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
30   s->ref_count = 1;
31   s->type = type;
32   return s;
33 }
34
35 void
36 ctype_free (GISourceType * type)
37 {
38   g_free (type->name);
39   g_list_foreach (type->child_list, (GFunc)gi_source_symbol_unref, NULL);
40   g_list_free (type->child_list);
41   g_slice_free (GISourceType, type);
42 }
43
44 GISourceSymbol *
45 gi_source_symbol_ref (GISourceSymbol * symbol)
46 {
47   symbol->ref_count++;
48   return symbol;
49 }
50
51 void
52 gi_source_symbol_unref (GISourceSymbol * symbol)
53 {
54   if (!symbol)
55     return;
56   symbol->ref_count--;
57   if (symbol->ref_count == 0)
58     {
59       g_free (symbol->ident);
60       if (symbol->base_type)
61         ctype_free (symbol->base_type);
62       g_free (symbol->const_string);
63       g_free (symbol->source_filename);
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   result->is_bitfield = type->is_bitfield;
113   return result;
114 }
115
116 GISourceType *
117 gi_source_basic_type_new (const char *name)
118 {
119   GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
120   basic_type->name = g_strdup (name);
121   return basic_type;
122 }
123
124 GISourceType *
125 gi_source_typedef_new (const char *name)
126 {
127   GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
128   typedef_->name = g_strdup (name);
129   return typedef_;
130 }
131
132 GISourceType *
133 gi_source_struct_new (const char *name)
134 {
135   GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
136   struct_->name = g_strdup (name);
137   return struct_;
138 }
139
140 GISourceType *
141 gi_source_union_new (const char *name)
142 {
143   GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
144   union_->name = g_strdup (name);
145   return union_;
146 }
147
148 GISourceType *
149 gi_source_enum_new (const char *name)
150 {
151   GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
152   enum_->name = g_strdup (name);
153   return enum_;
154 }
155
156 GISourceType *
157 gi_source_pointer_new (GISourceType * base_type)
158 {
159   GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
160   if (base_type != NULL)
161     pointer->base_type = gi_source_type_copy (base_type);
162   return pointer;
163 }
164
165 GISourceType *
166 gi_source_array_new (GISourceSymbol *size)
167 {
168   GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
169   if (size != NULL && size->type == CSYMBOL_TYPE_CONST && size->const_int_set)
170       array->child_list = g_list_append (array->child_list, size);
171   return array;
172 }
173
174 GISourceType *
175 gi_source_function_new (void)
176 {
177   GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
178   return func;
179 }
180
181 GISourceScanner *
182 gi_source_scanner_new (void)
183 {
184   GISourceScanner * scanner;
185
186   scanner = g_slice_new0 (GISourceScanner);
187   scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
188                                                   g_free, NULL);
189   scanner->struct_or_union_or_enum_table =
190     g_hash_table_new_full (g_str_hash, g_str_equal,
191                            g_free, (GDestroyNotify)gi_source_symbol_unref);
192
193   return scanner;
194 }
195
196 void
197 gi_source_scanner_free (GISourceScanner *scanner)
198 {
199   g_free (scanner->current_filename);
200
201   g_hash_table_destroy (scanner->typedef_table);
202   g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
203
204   g_slist_foreach (scanner->comments, (GFunc)g_free, NULL);
205   g_slist_free (scanner->comments);
206   g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
207   g_slist_free (scanner->symbols);
208
209   g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
210   g_list_free (scanner->filenames);
211
212 }
213
214 gboolean
215 gi_source_scanner_is_typedef (GISourceScanner *scanner,
216                               const char      *name)
217 {
218   gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
219   return b;
220 }
221
222 void
223 gi_source_scanner_set_macro_scan (GISourceScanner  *scanner,
224                                   gboolean          macro_scan)
225 {
226   scanner->macro_scan = macro_scan;
227 }
228
229 void
230 gi_source_scanner_add_symbol (GISourceScanner  *scanner,
231                               GISourceSymbol   *symbol)
232 {
233   gboolean found_filename = FALSE;
234   GList *l;
235
236   g_assert (scanner->current_filename);
237   for (l = scanner->filenames; l != NULL; l = l->next)
238     {
239       if (strcmp (l->data, scanner->current_filename) == 0)
240         {
241           found_filename = TRUE;
242           break;
243         }
244     }
245
246   if (found_filename || scanner->macro_scan)
247     scanner->symbols = g_slist_prepend (scanner->symbols,
248                                         gi_source_symbol_ref (symbol));
249   /* TODO: Refcounted string here or some other optimization */
250   if (found_filename && symbol->source_filename == NULL)
251     {
252       symbol->source_filename = g_strdup (scanner->current_filename);
253     }
254
255   switch (symbol->type)
256     {
257     case CSYMBOL_TYPE_TYPEDEF:
258       g_hash_table_insert (scanner->typedef_table,
259                            g_strdup (symbol->ident),
260                            GINT_TO_POINTER (TRUE));
261       break;
262     case CSYMBOL_TYPE_STRUCT:
263     case CSYMBOL_TYPE_UNION:
264     case CSYMBOL_TYPE_ENUM:
265       g_hash_table_insert (scanner->struct_or_union_or_enum_table,
266                            g_strdup (symbol->ident),
267                            gi_source_symbol_ref (symbol));
268       break;
269     default:
270       break;
271     }
272 }
273
274 GSList *
275 gi_source_scanner_get_symbols (GISourceScanner  *scanner)
276 {
277   return g_slist_reverse (scanner->symbols);
278 }
279
280 GSList *
281 gi_source_scanner_get_comments(GISourceScanner  *scanner)
282 {
283   return g_slist_reverse (scanner->comments);
284 }