1 /* GObject introspection: public scanner api
3 * Copyright (C) 2007 Jürg Billeter
4 * Copyright (C) 2008 Johan Dahlin
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.
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.
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.
23 #include "sourcescanner.h"
26 gi_source_symbol_new (GISourceSymbolType type)
28 GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
35 ctype_free (GISourceType * type)
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);
44 gi_source_symbol_ref (GISourceSymbol * symbol)
50 gi_source_symbol_unref (GISourceSymbol * symbol)
53 if (symbol->ref_count == 0)
55 g_free (symbol->ident);
56 if (symbol->base_type)
57 ctype_free (symbol->base_type);
58 g_free (symbol->const_string);
59 g_slist_foreach (symbol->directives, (GFunc)gi_source_directive_free, NULL);
60 g_slist_free (symbol->directives);
61 g_slice_free (GISourceSymbol, symbol);
66 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
68 return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
71 /* use specified type as base type of symbol */
73 gi_source_symbol_merge_type (GISourceSymbol *symbol,
76 GISourceType **foundation_type = &(symbol->base_type);
78 while (*foundation_type != NULL)
80 foundation_type = &((*foundation_type)->base_type);
82 *foundation_type = type;
87 gi_source_type_new (GISourceTypeType type)
89 GISourceType *t = g_slice_new0 (GISourceType);
95 gi_source_type_copy (GISourceType * type)
98 GISourceType *result = g_slice_new0 (GISourceType);
99 result->type = type->type;
100 result->storage_class_specifier = type->storage_class_specifier;
101 result->type_qualifier = type->type_qualifier;
102 result->function_specifier = type->function_specifier;
104 result->name = g_strdup (type->name);
106 result->base_type = gi_source_type_copy (type->base_type);
107 for (l = type->child_list; l; l = l->next)
108 result->child_list = g_list_append (result->child_list, gi_source_symbol_ref (l->data));
113 gi_source_basic_type_new (const char *name)
115 GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
116 basic_type->name = g_strdup (name);
121 gi_source_typedef_new (const char *name)
123 GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
124 typedef_->name = g_strdup (name);
129 gi_source_struct_new (const char *name)
131 GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
132 struct_->name = g_strdup (name);
137 gi_source_union_new (const char *name)
139 GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
140 union_->name = g_strdup (name);
145 gi_source_enum_new (const char *name)
147 GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
148 enum_->name = g_strdup (name);
153 gi_source_pointer_new (GISourceType * base_type)
155 GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
156 if (base_type != NULL)
157 pointer->base_type = gi_source_type_copy (base_type);
162 gi_source_array_new (void)
164 GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
169 gi_source_function_new (void)
171 GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
176 gi_source_directive_new (const gchar *name,
180 GISourceDirective *directive;
182 directive = g_slice_new (GISourceDirective);
183 directive->name = g_strdup (name);
184 directive->value = g_strdup (value);
185 directive->options = options;
190 gi_source_directive_free (GISourceDirective *directive)
192 g_free (directive->name);
193 g_free (directive->value);
194 g_slice_free (GISourceDirective, directive);
198 gi_source_scanner_new (void)
200 GISourceScanner * scanner;
202 scanner = g_slice_new0 (GISourceScanner);
203 scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
205 scanner->directives_map = g_hash_table_new (g_str_hash, g_str_equal);
206 scanner->struct_or_union_or_enum_table =
207 g_hash_table_new_full (g_str_hash, g_str_equal,
208 g_free, (GDestroyNotify)gi_source_symbol_unref);
214 gi_source_scanner_free (GISourceScanner *scanner)
216 g_free (scanner->current_filename);
218 g_hash_table_destroy (scanner->directives_map);
219 g_hash_table_destroy (scanner->typedef_table);
220 g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
222 g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
223 g_slist_free (scanner->symbols);
225 g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
226 g_list_free (scanner->filenames);
231 gi_source_scanner_is_typedef (GISourceScanner *scanner,
234 gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
239 gi_source_scanner_set_macro_scan (GISourceScanner *scanner,
242 scanner->macro_scan = macro_scan;
246 gi_source_scanner_add_symbol (GISourceScanner *scanner,
247 GISourceSymbol *symbol)
249 gboolean found_filename = FALSE;
252 g_assert (scanner->current_filename);
253 for (l = scanner->filenames; l != NULL; l = l->next)
255 if (strcmp (l->data, scanner->current_filename) == 0)
257 found_filename = TRUE;
262 if (found_filename || scanner->macro_scan)
263 scanner->symbols = g_slist_prepend (scanner->symbols,
264 gi_source_symbol_ref (symbol));
266 switch (symbol->type)
268 case CSYMBOL_TYPE_TYPEDEF:
269 g_hash_table_insert (scanner->typedef_table,
270 g_strdup (symbol->ident),
271 GINT_TO_POINTER (TRUE));
273 case CSYMBOL_TYPE_STRUCT:
274 case CSYMBOL_TYPE_UNION:
275 case CSYMBOL_TYPE_ENUM:
276 g_hash_table_insert (scanner->struct_or_union_or_enum_table,
277 g_strdup (symbol->ident),
278 gi_source_symbol_ref (symbol));
286 gi_source_scanner_get_symbols (GISourceScanner *scanner)
288 return g_slist_reverse (scanner->symbols);
292 gi_source_scanner_get_directives(GISourceScanner *scanner,
295 return g_hash_table_lookup (scanner->directives_map, name);