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"
28 gi_source_symbol_new (GISourceSymbolType type, const gchar *filename, int line)
30 GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
32 s->source_filename = g_strdup (filename);
39 ctype_free (GISourceType * type)
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);
48 gi_source_symbol_ref (GISourceSymbol * symbol)
55 gi_source_symbol_unref (GISourceSymbol * symbol)
60 if (symbol->ref_count == 0)
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);
72 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
74 return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
77 /* use specified type as base type of symbol */
79 gi_source_symbol_merge_type (GISourceSymbol *symbol,
82 GISourceType **foundation_type = &(symbol->base_type);
84 while (*foundation_type != NULL)
86 foundation_type = &((*foundation_type)->base_type);
88 *foundation_type = type;
93 gi_source_type_new (GISourceTypeType type)
95 GISourceType *t = g_slice_new0 (GISourceType);
101 gi_source_type_copy (GISourceType * type)
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;
110 result->name = g_strdup (type->name);
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;
120 gi_source_basic_type_new (const char *name)
122 GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
123 basic_type->name = g_strdup (name);
128 gi_source_typedef_new (const char *name)
130 GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
131 typedef_->name = g_strdup (name);
136 gi_source_struct_new (const char *name)
138 GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
139 struct_->name = g_strdup (name);
144 gi_source_union_new (const char *name)
146 GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
147 union_->name = g_strdup (name);
152 gi_source_enum_new (const char *name)
154 GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
155 enum_->name = g_strdup (name);
160 gi_source_pointer_new (GISourceType * base_type)
162 GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
163 if (base_type != NULL)
164 pointer->base_type = gi_source_type_copy (base_type);
169 gi_source_array_new (GISourceSymbol *size)
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);
178 gi_source_function_new (void)
180 GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
185 gi_source_scanner_new (void)
187 GISourceScanner * scanner;
189 scanner = g_slice_new0 (GISourceScanner);
190 scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
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);
200 gi_source_comment_free (GISourceComment *comment)
202 g_free (comment->comment);
203 g_free (comment->filename);
204 g_slice_free (GISourceComment, comment);
208 gi_source_scanner_free (GISourceScanner *scanner)
210 g_free (scanner->current_filename);
212 g_hash_table_destroy (scanner->typedef_table);
213 g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
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);
220 g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
221 g_list_free (scanner->filenames);
226 gi_source_scanner_is_typedef (GISourceScanner *scanner,
229 gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
234 gi_source_scanner_set_macro_scan (GISourceScanner *scanner,
237 scanner->macro_scan = macro_scan;
241 gi_source_scanner_add_symbol (GISourceScanner *scanner,
242 GISourceSymbol *symbol)
244 gboolean found_filename = FALSE;
248 g_assert (scanner->current_filename);
249 current_file = g_file_new_for_path (scanner->current_filename);
251 for (l = scanner->filenames; l != NULL; l = l->next)
253 GFile *file = g_file_new_for_path (l->data);
255 if (g_file_equal (file, current_file))
257 found_filename = TRUE;
258 g_object_unref (file);
261 g_object_unref (file);
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);
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));
287 g_object_unref (current_file);
291 gi_source_scanner_get_symbols (GISourceScanner *scanner)
293 return g_slist_reverse (scanner->symbols);
297 gi_source_scanner_get_comments(GISourceScanner *scanner)
299 return g_slist_reverse (scanner->comments);