Bug 563591 – Flags not recognized when there is no introspection data
[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_slice_free (GISourceSymbol, symbol);
64     }
65 }
66  
67 gboolean
68 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
69 {
70   return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
71 }
72
73 /* use specified type as base type of symbol */
74 void
75 gi_source_symbol_merge_type (GISourceSymbol *symbol,
76                              GISourceType   *type)
77 {
78   GISourceType **foundation_type = &(symbol->base_type);
79
80   while (*foundation_type != NULL)
81     {
82       foundation_type = &((*foundation_type)->base_type);
83     }
84   *foundation_type = type;
85 }
86
87
88 GISourceType *
89 gi_source_type_new (GISourceTypeType type)
90 {
91   GISourceType *t = g_slice_new0 (GISourceType);
92   t->type = type;
93   return t;
94 }
95
96 GISourceType *
97 gi_source_type_copy (GISourceType * type)
98 {
99   GList *l;
100   GISourceType *result = g_slice_new0 (GISourceType);
101   result->type = type->type;
102   result->storage_class_specifier = type->storage_class_specifier;
103   result->type_qualifier = type->type_qualifier;
104   result->function_specifier = type->function_specifier;
105   if (type->name)
106     result->name = g_strdup (type->name);
107   if (type->base_type)
108     result->base_type = gi_source_type_copy (type->base_type);
109   for (l = type->child_list; l; l = l->next)
110     result->child_list = g_list_append (result->child_list, gi_source_symbol_ref (l->data));
111   result->is_bitfield = type->is_bitfield;
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 (GISourceSymbol *size)
166 {
167   GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
168   if (size != NULL && size->type == CSYMBOL_TYPE_CONST && size->const_int_set)
169       array->child_list = g_list_append (array->child_list, size);
170   return array;
171 }
172
173 GISourceType *
174 gi_source_function_new (void)
175 {
176   GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
177   return func;
178 }
179
180 GISourceScanner *
181 gi_source_scanner_new (void)
182 {
183   GISourceScanner * scanner;
184
185   scanner = g_slice_new0 (GISourceScanner);
186   scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
187                                                   g_free, NULL);
188   scanner->struct_or_union_or_enum_table =
189     g_hash_table_new_full (g_str_hash, g_str_equal,
190                            g_free, (GDestroyNotify)gi_source_symbol_unref);
191
192   return scanner;
193 }
194
195 void
196 gi_source_scanner_free (GISourceScanner *scanner)
197 {
198   g_free (scanner->current_filename);
199
200   g_hash_table_destroy (scanner->typedef_table);
201   g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
202
203   g_slist_foreach (scanner->comments, (GFunc)g_free, NULL);
204   g_slist_free (scanner->comments);
205   g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
206   g_slist_free (scanner->symbols);
207
208   g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
209   g_list_free (scanner->filenames);
210
211 }
212
213 gboolean
214 gi_source_scanner_is_typedef (GISourceScanner *scanner,
215                               const char      *name)
216 {
217   gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
218   return b;
219 }
220
221 void
222 gi_source_scanner_set_macro_scan (GISourceScanner  *scanner,
223                                   gboolean          macro_scan)
224 {
225   scanner->macro_scan = macro_scan;
226 }
227
228 void
229 gi_source_scanner_add_symbol (GISourceScanner  *scanner,
230                               GISourceSymbol   *symbol)
231 {
232   gboolean found_filename = FALSE;
233   GList *l;
234
235   g_assert (scanner->current_filename);
236   for (l = scanner->filenames; l != NULL; l = l->next)
237     {
238       if (strcmp (l->data, scanner->current_filename) == 0)
239         {
240           found_filename = TRUE;
241           break;
242         }
243     }
244
245   if (found_filename || scanner->macro_scan)
246     scanner->symbols = g_slist_prepend (scanner->symbols,
247                                         gi_source_symbol_ref (symbol));
248
249   switch (symbol->type)
250     {
251     case CSYMBOL_TYPE_TYPEDEF:
252       g_hash_table_insert (scanner->typedef_table,
253                            g_strdup (symbol->ident),
254                            GINT_TO_POINTER (TRUE));
255       break;
256     case CSYMBOL_TYPE_STRUCT:
257     case CSYMBOL_TYPE_UNION:
258     case CSYMBOL_TYPE_ENUM:
259       g_hash_table_insert (scanner->struct_or_union_or_enum_table,
260                            g_strdup (symbol->ident),
261                            gi_source_symbol_ref (symbol));
262       break;
263     default:
264       break;
265     }
266 }
267
268 GSList *
269 gi_source_scanner_get_symbols (GISourceScanner  *scanner)
270 {
271   return g_slist_reverse (scanner->symbols);
272 }
273
274 GSList *
275 gi_source_scanner_get_comments(GISourceScanner  *scanner)
276 {
277   return g_slist_reverse (scanner->comments);
278 }