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, GFile *file, int line)
30 GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
32 s->source_filename = g_file_get_parse_name (file);
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_copy (GISourceSymbol * symbol)
50 GFile *source_file = g_file_new_for_path (symbol->source_filename);
51 GISourceSymbol *new_symbol = gi_source_symbol_new (symbol->type,
54 new_symbol->ident = g_strdup (symbol->ident);
56 if (symbol->base_type)
57 new_symbol->base_type = gi_source_type_copy (symbol->base_type);
59 if (symbol->const_int_set) {
60 new_symbol->const_int = symbol->const_int;
61 new_symbol->const_int_is_unsigned = symbol->const_int_is_unsigned;
62 new_symbol->const_int_set = TRUE;
63 } else if (symbol->const_boolean_set) {
64 new_symbol->const_boolean = symbol->const_boolean;
65 new_symbol->const_boolean_set = TRUE;
66 } else if (symbol->const_double_set) {
67 new_symbol->const_double = symbol->const_double;
68 new_symbol->const_double_set = TRUE;
69 } else if (symbol->const_string != NULL) {
70 new_symbol->const_string = g_strdup (symbol->const_string);
77 gi_source_symbol_ref (GISourceSymbol * symbol)
84 gi_source_symbol_unref (GISourceSymbol * symbol)
89 if (symbol->ref_count == 0)
91 g_free (symbol->ident);
92 if (symbol->base_type)
93 ctype_free (symbol->base_type);
94 g_free (symbol->const_string);
95 g_free (symbol->source_filename);
96 g_slice_free (GISourceSymbol, symbol);
101 gi_source_symbol_get_const_boolean (GISourceSymbol * symbol)
103 return (symbol->const_int_set && symbol->const_int) || symbol->const_string;
106 /* use specified type as base type of symbol */
108 gi_source_symbol_merge_type (GISourceSymbol *symbol,
111 GISourceType **foundation_type = &(symbol->base_type);
113 while (*foundation_type != NULL)
115 foundation_type = &((*foundation_type)->base_type);
117 *foundation_type = type;
122 gi_source_type_new (GISourceTypeType type)
124 GISourceType *t = g_slice_new0 (GISourceType);
130 gi_source_type_copy (GISourceType * type)
133 GISourceType *result = g_slice_new0 (GISourceType);
134 result->type = type->type;
135 result->storage_class_specifier = type->storage_class_specifier;
136 result->type_qualifier = type->type_qualifier;
137 result->function_specifier = type->function_specifier;
139 result->name = g_strdup (type->name);
141 result->base_type = gi_source_type_copy (type->base_type);
142 for (l = type->child_list; l; l = l->next)
143 result->child_list = g_list_append (result->child_list, gi_source_symbol_ref (l->data));
144 result->is_bitfield = type->is_bitfield;
149 gi_source_basic_type_new (const char *name)
151 GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
152 basic_type->name = g_strdup (name);
157 gi_source_typedef_new (const char *name)
159 GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
160 typedef_->name = g_strdup (name);
165 gi_source_struct_new (const char *name)
167 GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
168 struct_->name = g_strdup (name);
173 gi_source_union_new (const char *name)
175 GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
176 union_->name = g_strdup (name);
181 gi_source_enum_new (const char *name)
183 GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
184 enum_->name = g_strdup (name);
189 gi_source_pointer_new (GISourceType * base_type)
191 GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
192 if (base_type != NULL)
193 pointer->base_type = gi_source_type_copy (base_type);
198 gi_source_array_new (GISourceSymbol *size)
200 GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
201 if (size != NULL && size->type == CSYMBOL_TYPE_CONST && size->const_int_set)
202 array->child_list = g_list_append (array->child_list, size);
207 gi_source_function_new (void)
209 GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
214 gi_source_scanner_new (void)
216 GISourceScanner * scanner;
218 scanner = g_slice_new0 (GISourceScanner);
219 scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
221 scanner->files = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
222 g_object_unref, NULL);
223 g_queue_init (&scanner->conditionals);
228 gi_source_comment_free (GISourceComment *comment)
230 g_free (comment->comment);
231 g_free (comment->filename);
232 g_slice_free (GISourceComment, comment);
236 gi_source_scanner_free (GISourceScanner *scanner)
238 g_object_unref (scanner->current_file);
240 g_hash_table_destroy (scanner->typedef_table);
242 g_slist_foreach (scanner->comments, (GFunc)gi_source_comment_free, NULL);
243 g_slist_free (scanner->comments);
244 g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
245 g_slist_free (scanner->symbols);
247 g_hash_table_unref (scanner->files);
249 g_queue_clear (&scanner->conditionals);
253 gi_source_scanner_is_typedef (GISourceScanner *scanner,
256 gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
261 gi_source_scanner_set_macro_scan (GISourceScanner *scanner,
264 scanner->macro_scan = macro_scan;
268 gi_source_scanner_add_symbol (GISourceScanner *scanner,
269 GISourceSymbol *symbol)
271 if (scanner->skipping)
273 g_debug ("skipping symbol due to __GI_SCANNER__ cond: %s", symbol->ident);
277 g_assert (scanner->current_file);
279 if (scanner->macro_scan || g_hash_table_contains (scanner->files, scanner->current_file))
280 scanner->symbols = g_slist_prepend (scanner->symbols,
281 gi_source_symbol_ref (symbol));
283 g_assert (symbol->source_filename != NULL);
285 switch (symbol->type)
287 case CSYMBOL_TYPE_TYPEDEF:
288 g_hash_table_insert (scanner->typedef_table,
289 g_strdup (symbol->ident),
290 GINT_TO_POINTER (TRUE));
298 gi_source_scanner_take_comment (GISourceScanner *scanner,
299 GISourceComment *comment)
301 if (scanner->skipping)
303 g_debug ("skipping comment due to __GI_SCANNER__ cond");
304 gi_source_comment_free (comment);
308 scanner->comments = g_slist_prepend (scanner->comments,
313 * gi_source_scanner_get_symbols:
314 * @scanner: scanner instance
316 * Returns: (transfer container): List of GISourceSymbol.
317 * Free resulting list with g_slist_free().
320 gi_source_scanner_get_symbols (GISourceScanner *scanner)
322 return g_slist_reverse (g_slist_copy (scanner->symbols));
326 * gi_source_scanner_get_comments:
327 * @scanner: scanner instance
329 * Returns: (transfer container): List of GISourceComment.
330 * Free resulting list with g_slist_free().
333 gi_source_scanner_get_comments(GISourceScanner *scanner)
335 return g_slist_reverse (g_slist_copy (scanner->comments));