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)
51 gi_source_symbol_unref (GISourceSymbol * symbol)
56 if (symbol->ref_count == 0)
58 g_free (symbol->ident);
59 if (symbol->base_type)
60 ctype_free (symbol->base_type);
61 g_free (symbol->const_string);
62 g_slist_foreach (symbol->directives, (GFunc)gi_source_directive_free, NULL);
63 g_slist_free (symbol->directives);
64 g_slice_free (GISourceSymbol, symbol);
69 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
71 return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
74 /* use specified type as base type of symbol */
76 gi_source_symbol_merge_type (GISourceSymbol *symbol,
79 GISourceType **foundation_type = &(symbol->base_type);
81 while (*foundation_type != NULL)
83 foundation_type = &((*foundation_type)->base_type);
85 *foundation_type = type;
90 gi_source_type_new (GISourceTypeType type)
92 GISourceType *t = g_slice_new0 (GISourceType);
98 gi_source_type_copy (GISourceType * type)
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;
107 result->name = g_strdup (type->name);
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));
116 gi_source_basic_type_new (const char *name)
118 GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
119 basic_type->name = g_strdup (name);
124 gi_source_typedef_new (const char *name)
126 GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
127 typedef_->name = g_strdup (name);
132 gi_source_struct_new (const char *name)
134 GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
135 struct_->name = g_strdup (name);
140 gi_source_union_new (const char *name)
142 GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
143 union_->name = g_strdup (name);
148 gi_source_enum_new (const char *name)
150 GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
151 enum_->name = g_strdup (name);
156 gi_source_pointer_new (GISourceType * base_type)
158 GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
159 if (base_type != NULL)
160 pointer->base_type = gi_source_type_copy (base_type);
165 gi_source_array_new (void)
167 GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
172 gi_source_function_new (void)
174 GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
179 gi_source_directive_new (const gchar *name,
183 GISourceDirective *directive;
185 directive = g_slice_new (GISourceDirective);
186 directive->name = g_strdup (name);
187 directive->value = g_strdup (value);
188 directive->options = options;
193 gi_source_directive_free (GISourceDirective *directive)
195 g_free (directive->name);
196 g_free (directive->value);
197 g_slice_free (GISourceDirective, directive);
201 gi_source_scanner_new (void)
203 GISourceScanner * scanner;
205 scanner = g_slice_new0 (GISourceScanner);
206 scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
208 scanner->directives_map = g_hash_table_new (g_str_hash, g_str_equal);
209 scanner->struct_or_union_or_enum_table =
210 g_hash_table_new_full (g_str_hash, g_str_equal,
211 g_free, (GDestroyNotify)gi_source_symbol_unref);
217 gi_source_scanner_free (GISourceScanner *scanner)
219 g_free (scanner->current_filename);
221 g_hash_table_destroy (scanner->directives_map);
222 g_hash_table_destroy (scanner->typedef_table);
223 g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
225 g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
226 g_slist_free (scanner->symbols);
228 g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
229 g_list_free (scanner->filenames);
234 gi_source_scanner_is_typedef (GISourceScanner *scanner,
237 gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
242 gi_source_scanner_set_macro_scan (GISourceScanner *scanner,
245 scanner->macro_scan = macro_scan;
249 gi_source_scanner_add_symbol (GISourceScanner *scanner,
250 GISourceSymbol *symbol)
252 gboolean found_filename = FALSE;
255 g_assert (scanner->current_filename);
256 for (l = scanner->filenames; l != NULL; l = l->next)
258 if (strcmp (l->data, scanner->current_filename) == 0)
260 found_filename = TRUE;
265 if (found_filename || scanner->macro_scan)
266 scanner->symbols = g_slist_prepend (scanner->symbols,
267 gi_source_symbol_ref (symbol));
269 switch (symbol->type)
271 case CSYMBOL_TYPE_TYPEDEF:
272 g_hash_table_insert (scanner->typedef_table,
273 g_strdup (symbol->ident),
274 GINT_TO_POINTER (TRUE));
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));
289 gi_source_scanner_get_symbols (GISourceScanner *scanner)
291 return g_slist_reverse (scanner->symbols);
295 gi_source_scanner_get_directives(GISourceScanner *scanner,
298 return g_hash_table_lookup (scanner->directives_map, name);