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 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"
27 gi_source_symbol_new (GISourceSymbolType type)
29 GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
36 ctype_free (GISourceType * type)
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);
45 gi_source_symbol_ref (GISourceSymbol * symbol)
52 gi_source_symbol_unref (GISourceSymbol * symbol)
57 if (symbol->ref_count == 0)
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);
68 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
70 return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
73 /* use specified type as base type of symbol */
75 gi_source_symbol_merge_type (GISourceSymbol *symbol,
78 GISourceType **foundation_type = &(symbol->base_type);
80 while (*foundation_type != NULL)
82 foundation_type = &((*foundation_type)->base_type);
84 *foundation_type = type;
89 gi_source_type_new (GISourceTypeType type)
91 GISourceType *t = g_slice_new0 (GISourceType);
97 gi_source_type_copy (GISourceType * type)
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;
106 result->name = g_strdup (type->name);
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;
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 (GISourceSymbol *size)
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);
174 gi_source_function_new (void)
176 GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
181 gi_source_scanner_new (void)
183 GISourceScanner * scanner;
185 scanner = g_slice_new0 (GISourceScanner);
186 scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
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);
196 gi_source_scanner_free (GISourceScanner *scanner)
198 g_free (scanner->current_filename);
200 g_hash_table_destroy (scanner->typedef_table);
201 g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
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);
208 g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
209 g_list_free (scanner->filenames);
214 gi_source_scanner_is_typedef (GISourceScanner *scanner,
217 gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
222 gi_source_scanner_set_macro_scan (GISourceScanner *scanner,
225 scanner->macro_scan = macro_scan;
229 gi_source_scanner_add_symbol (GISourceScanner *scanner,
230 GISourceSymbol *symbol)
232 gboolean found_filename = FALSE;
235 g_assert (scanner->current_filename);
236 for (l = scanner->filenames; l != NULL; l = l->next)
238 if (strcmp (l->data, scanner->current_filename) == 0)
240 found_filename = TRUE;
245 if (found_filename || scanner->macro_scan)
246 scanner->symbols = g_slist_prepend (scanner->symbols,
247 gi_source_symbol_ref (symbol));
249 switch (symbol->type)
251 case CSYMBOL_TYPE_TYPEDEF:
252 g_hash_table_insert (scanner->typedef_table,
253 g_strdup (symbol->ident),
254 GINT_TO_POINTER (TRUE));
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));
269 gi_source_scanner_get_symbols (GISourceScanner *scanner)
271 return g_slist_reverse (scanner->symbols);
275 gi_source_scanner_get_comments(GISourceScanner *scanner)
277 return g_slist_reverse (scanner->comments);