Imported Upstream version 1.39.3
[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 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 #include <gio/gio.h>
26
27 GISourceSymbol *
28 gi_source_symbol_new (GISourceSymbolType type, GFile *file, int line)
29 {
30   GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
31   s->ref_count = 1;
32   s->source_filename = g_file_get_parse_name (file);
33   s->type = type;
34   s->line = line;
35   return s;
36 }
37
38 void
39 ctype_free (GISourceType * type)
40 {
41   g_free (type->name);
42   g_list_foreach (type->child_list, (GFunc)gi_source_symbol_unref, NULL);
43   g_list_free (type->child_list);
44   g_slice_free (GISourceType, type);
45 }
46
47 GISourceSymbol *
48 gi_source_symbol_copy (GISourceSymbol * symbol)
49 {
50   GFile *source_file = g_file_new_for_path (symbol->source_filename);
51   GISourceSymbol *new_symbol = gi_source_symbol_new (symbol->type,
52                                                      source_file,
53                                                      symbol->line);
54   new_symbol->ident = g_strdup (symbol->ident);
55
56   if (symbol->base_type)
57     new_symbol->base_type = gi_source_type_copy (symbol->base_type);
58
59   if (symbol->const_int_set) {
60     new_symbol->const_int = symbol->const_int;
61     new_symbol->const_int_is_unsigned = symbol->const_int_is_unsigned;
62     new_symbol->const_int_set = TRUE;
63   } else if (symbol->const_boolean_set) {
64     new_symbol->const_boolean = symbol->const_boolean;
65     new_symbol->const_boolean_set = TRUE;
66   } else if (symbol->const_double_set) {
67     new_symbol->const_double = symbol->const_double;
68     new_symbol->const_double_set = TRUE;
69   } else if (symbol->const_string != NULL) {
70     new_symbol->const_string = g_strdup (symbol->const_string);
71   }
72
73   return new_symbol;
74 }
75
76 GISourceSymbol *
77 gi_source_symbol_ref (GISourceSymbol * symbol)
78 {
79   symbol->ref_count++;
80   return symbol;
81 }
82
83 void
84 gi_source_symbol_unref (GISourceSymbol * symbol)
85 {
86   if (!symbol)
87     return;
88   symbol->ref_count--;
89   if (symbol->ref_count == 0)
90     {
91       g_free (symbol->ident);
92       if (symbol->base_type)
93         ctype_free (symbol->base_type);
94       g_free (symbol->const_string);
95       g_free (symbol->source_filename);
96       g_slice_free (GISourceSymbol, symbol);
97     }
98 }
99  
100 gboolean
101 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
102 {
103   return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
104 }
105
106 /* use specified type as base type of symbol */
107 void
108 gi_source_symbol_merge_type (GISourceSymbol *symbol,
109                              GISourceType   *type)
110 {
111   GISourceType **foundation_type = &(symbol->base_type);
112
113   while (*foundation_type != NULL)
114     {
115       foundation_type = &((*foundation_type)->base_type);
116     }
117   *foundation_type = type;
118 }
119
120
121 GISourceType *
122 gi_source_type_new (GISourceTypeType type)
123 {
124   GISourceType *t = g_slice_new0 (GISourceType);
125   t->type = type;
126   return t;
127 }
128
129 GISourceType *
130 gi_source_type_copy (GISourceType * type)
131 {
132   GList *l;
133   GISourceType *result = g_slice_new0 (GISourceType);
134   result->type = type->type;
135   result->storage_class_specifier = type->storage_class_specifier;
136   result->type_qualifier = type->type_qualifier;
137   result->function_specifier = type->function_specifier;
138   if (type->name)
139     result->name = g_strdup (type->name);
140   if (type->base_type)
141     result->base_type = gi_source_type_copy (type->base_type);
142   for (l = type->child_list; l; l = l->next)
143     result->child_list = g_list_append (result->child_list, gi_source_symbol_ref (l->data));
144   result->is_bitfield = type->is_bitfield;
145   return result;
146 }
147
148 GISourceType *
149 gi_source_basic_type_new (const char *name)
150 {
151   GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
152   basic_type->name = g_strdup (name);
153   return basic_type;
154 }
155
156 GISourceType *
157 gi_source_typedef_new (const char *name)
158 {
159   GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
160   typedef_->name = g_strdup (name);
161   return typedef_;
162 }
163
164 GISourceType *
165 gi_source_struct_new (const char *name)
166 {
167   GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
168   struct_->name = g_strdup (name);
169   return struct_;
170 }
171
172 GISourceType *
173 gi_source_union_new (const char *name)
174 {
175   GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
176   union_->name = g_strdup (name);
177   return union_;
178 }
179
180 GISourceType *
181 gi_source_enum_new (const char *name)
182 {
183   GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
184   enum_->name = g_strdup (name);
185   return enum_;
186 }
187
188 GISourceType *
189 gi_source_pointer_new (GISourceType * base_type)
190 {
191   GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
192   if (base_type != NULL)
193     pointer->base_type = gi_source_type_copy (base_type);
194   return pointer;
195 }
196
197 GISourceType *
198 gi_source_array_new (GISourceSymbol *size)
199 {
200   GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
201   if (size != NULL && size->type == CSYMBOL_TYPE_CONST && size->const_int_set)
202       array->child_list = g_list_append (array->child_list, size);
203   return array;
204 }
205
206 GISourceType *
207 gi_source_function_new (void)
208 {
209   GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
210   return func;
211 }
212
213 GISourceScanner *
214 gi_source_scanner_new (void)
215 {
216   GISourceScanner * scanner;
217
218   scanner = g_slice_new0 (GISourceScanner);
219   scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
220                                                   g_free, NULL);
221   scanner->files = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
222                                           g_object_unref, NULL);
223   g_queue_init (&scanner->conditionals);
224   return scanner;
225 }
226
227 static void
228 gi_source_comment_free (GISourceComment *comment)
229 {
230   g_free (comment->comment);
231   g_free (comment->filename);
232   g_slice_free (GISourceComment, comment);
233 }
234
235 void
236 gi_source_scanner_free (GISourceScanner *scanner)
237 {
238   g_object_unref (scanner->current_file);
239
240   g_hash_table_destroy (scanner->typedef_table);
241
242   g_slist_foreach (scanner->comments, (GFunc)gi_source_comment_free, NULL);
243   g_slist_free (scanner->comments);
244   g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
245   g_slist_free (scanner->symbols);
246
247   g_hash_table_unref (scanner->files);
248
249   g_queue_clear (&scanner->conditionals);
250 }
251
252 gboolean
253 gi_source_scanner_is_typedef (GISourceScanner *scanner,
254                               const char      *name)
255 {
256   gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
257   return b;
258 }
259
260 void
261 gi_source_scanner_set_macro_scan (GISourceScanner  *scanner,
262                                   gboolean          macro_scan)
263 {
264   scanner->macro_scan = macro_scan;
265 }
266
267 void
268 gi_source_scanner_add_symbol (GISourceScanner  *scanner,
269                               GISourceSymbol   *symbol)
270 {
271   if (scanner->skipping)
272     {
273       g_debug ("skipping symbol due to __GI_SCANNER__ cond: %s", symbol->ident);
274       return;
275     }
276
277   g_assert (scanner->current_file);
278
279   if (scanner->macro_scan || g_hash_table_contains (scanner->files, scanner->current_file))
280     scanner->symbols = g_slist_prepend (scanner->symbols,
281                                         gi_source_symbol_ref (symbol));
282
283   g_assert (symbol->source_filename != NULL);
284
285   switch (symbol->type)
286     {
287     case CSYMBOL_TYPE_TYPEDEF:
288       g_hash_table_insert (scanner->typedef_table,
289                            g_strdup (symbol->ident),
290                            GINT_TO_POINTER (TRUE));
291       break;
292     default:
293       break;
294     }
295 }
296
297 void
298 gi_source_scanner_take_comment (GISourceScanner *scanner,
299                                 GISourceComment *comment)
300 {
301   if (scanner->skipping)
302     {
303       g_debug ("skipping comment due to __GI_SCANNER__ cond");
304       gi_source_comment_free (comment);
305       return;
306     }
307
308   scanner->comments = g_slist_prepend (scanner->comments,
309                                        comment);
310 }
311
312 /**
313  * gi_source_scanner_get_symbols:
314  * @scanner: scanner instance
315  *
316  * Returns: (transfer container): List of GISourceSymbol.
317  *   Free resulting list with g_slist_free().
318  */
319 GSList *
320 gi_source_scanner_get_symbols (GISourceScanner  *scanner)
321 {
322   return g_slist_reverse (g_slist_copy (scanner->symbols));
323 }
324
325 /**
326  * gi_source_scanner_get_comments:
327  * @scanner: scanner instance
328  *
329  * Returns: (transfer container): List of GISourceComment.
330  *   Free resulting list with g_slist_free().
331  */
332 GSList *
333 gi_source_scanner_get_comments(GISourceScanner  *scanner)
334 {
335   return g_slist_reverse (g_slist_copy (scanner->comments));
336 }