gimarshallingtests: Add string_ to boxed structure
[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, const gchar *filename, int line)
29 {
30   GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
31   s->ref_count = 1;
32   s->source_filename = g_strdup (filename);
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_ref (GISourceSymbol * symbol)
49 {
50   symbol->ref_count++;
51   return symbol;
52 }
53
54 void
55 gi_source_symbol_unref (GISourceSymbol * symbol)
56 {
57   if (!symbol)
58     return;
59   symbol->ref_count--;
60   if (symbol->ref_count == 0)
61     {
62       g_free (symbol->ident);
63       if (symbol->base_type)
64         ctype_free (symbol->base_type);
65       g_free (symbol->const_string);
66       g_free (symbol->source_filename);
67       g_slice_free (GISourceSymbol, symbol);
68     }
69 }
70  
71 gboolean
72 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
73 {
74   return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
75 }
76
77 /* use specified type as base type of symbol */
78 void
79 gi_source_symbol_merge_type (GISourceSymbol *symbol,
80                              GISourceType   *type)
81 {
82   GISourceType **foundation_type = &(symbol->base_type);
83
84   while (*foundation_type != NULL)
85     {
86       foundation_type = &((*foundation_type)->base_type);
87     }
88   *foundation_type = type;
89 }
90
91
92 GISourceType *
93 gi_source_type_new (GISourceTypeType type)
94 {
95   GISourceType *t = g_slice_new0 (GISourceType);
96   t->type = type;
97   return t;
98 }
99
100 GISourceType *
101 gi_source_type_copy (GISourceType * type)
102 {
103   GList *l;
104   GISourceType *result = g_slice_new0 (GISourceType);
105   result->type = type->type;
106   result->storage_class_specifier = type->storage_class_specifier;
107   result->type_qualifier = type->type_qualifier;
108   result->function_specifier = type->function_specifier;
109   if (type->name)
110     result->name = g_strdup (type->name);
111   if (type->base_type)
112     result->base_type = gi_source_type_copy (type->base_type);
113   for (l = type->child_list; l; l = l->next)
114     result->child_list = g_list_append (result->child_list, gi_source_symbol_ref (l->data));
115   result->is_bitfield = type->is_bitfield;
116   return result;
117 }
118
119 GISourceType *
120 gi_source_basic_type_new (const char *name)
121 {
122   GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
123   basic_type->name = g_strdup (name);
124   return basic_type;
125 }
126
127 GISourceType *
128 gi_source_typedef_new (const char *name)
129 {
130   GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
131   typedef_->name = g_strdup (name);
132   return typedef_;
133 }
134
135 GISourceType *
136 gi_source_struct_new (const char *name)
137 {
138   GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
139   struct_->name = g_strdup (name);
140   return struct_;
141 }
142
143 GISourceType *
144 gi_source_union_new (const char *name)
145 {
146   GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
147   union_->name = g_strdup (name);
148   return union_;
149 }
150
151 GISourceType *
152 gi_source_enum_new (const char *name)
153 {
154   GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
155   enum_->name = g_strdup (name);
156   return enum_;
157 }
158
159 GISourceType *
160 gi_source_pointer_new (GISourceType * base_type)
161 {
162   GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
163   if (base_type != NULL)
164     pointer->base_type = gi_source_type_copy (base_type);
165   return pointer;
166 }
167
168 GISourceType *
169 gi_source_array_new (GISourceSymbol *size)
170 {
171   GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
172   if (size != NULL && size->type == CSYMBOL_TYPE_CONST && size->const_int_set)
173       array->child_list = g_list_append (array->child_list, size);
174   return array;
175 }
176
177 GISourceType *
178 gi_source_function_new (void)
179 {
180   GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
181   return func;
182 }
183
184 GISourceScanner *
185 gi_source_scanner_new (void)
186 {
187   GISourceScanner * scanner;
188
189   scanner = g_slice_new0 (GISourceScanner);
190   scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
191                                                   g_free, NULL);
192   scanner->struct_or_union_or_enum_table =
193     g_hash_table_new_full (g_str_hash, g_str_equal,
194                            g_free, (GDestroyNotify)gi_source_symbol_unref);
195
196   return scanner;
197 }
198
199 static void
200 gi_source_comment_free (GISourceComment *comment)
201 {
202   g_free (comment->comment);
203   g_free (comment->filename);
204   g_slice_free (GISourceComment, comment);
205 }
206
207 void
208 gi_source_scanner_free (GISourceScanner *scanner)
209 {
210   g_free (scanner->current_filename);
211
212   g_hash_table_destroy (scanner->typedef_table);
213   g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
214
215   g_slist_foreach (scanner->comments, (GFunc)gi_source_comment_free, NULL);
216   g_slist_free (scanner->comments);
217   g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
218   g_slist_free (scanner->symbols);
219
220   g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
221   g_list_free (scanner->filenames);
222
223 }
224
225 gboolean
226 gi_source_scanner_is_typedef (GISourceScanner *scanner,
227                               const char      *name)
228 {
229   gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
230   return b;
231 }
232
233 void
234 gi_source_scanner_set_macro_scan (GISourceScanner  *scanner,
235                                   gboolean          macro_scan)
236 {
237   scanner->macro_scan = macro_scan;
238 }
239
240 void
241 gi_source_scanner_add_symbol (GISourceScanner  *scanner,
242                               GISourceSymbol   *symbol)
243 {
244   gboolean found_filename = FALSE;
245   GList *l;
246   GFile *current_file;
247
248   g_assert (scanner->current_filename);
249   current_file = g_file_new_for_path (scanner->current_filename);
250
251   for (l = scanner->filenames; l != NULL; l = l->next)
252     {
253       GFile *file = g_file_new_for_path (l->data);
254
255       if (g_file_equal (file, current_file))
256         {
257           found_filename = TRUE;
258           g_object_unref (file);
259           break;
260         }
261       g_object_unref (file);
262     }
263
264   if (found_filename || scanner->macro_scan)
265     scanner->symbols = g_slist_prepend (scanner->symbols,
266                                         gi_source_symbol_ref (symbol));
267   g_assert (symbol->source_filename != NULL);
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     g_object_unref (current_file);
288 }
289
290 GSList *
291 gi_source_scanner_get_symbols (GISourceScanner  *scanner)
292 {
293   return g_slist_reverse (scanner->symbols);
294 }
295
296 GSList *
297 gi_source_scanner_get_comments(GISourceScanner  *scanner)
298 {
299   return g_slist_reverse (scanner->comments);
300 }