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_free (symbol->source_filename);
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));
112 result->is_bitfield = type->is_bitfield;
117 gi_source_basic_type_new (const char *name)
119 GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
120 basic_type->name = g_strdup (name);
125 gi_source_typedef_new (const char *name)
127 GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
128 typedef_->name = g_strdup (name);
133 gi_source_struct_new (const char *name)
135 GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
136 struct_->name = g_strdup (name);
141 gi_source_union_new (const char *name)
143 GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
144 union_->name = g_strdup (name);
149 gi_source_enum_new (const char *name)
151 GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
152 enum_->name = g_strdup (name);
157 gi_source_pointer_new (GISourceType * base_type)
159 GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
160 if (base_type != NULL)
161 pointer->base_type = gi_source_type_copy (base_type);
166 gi_source_array_new (GISourceSymbol *size)
168 GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
169 if (size != NULL && size->type == CSYMBOL_TYPE_CONST && size->const_int_set)
170 array->child_list = g_list_append (array->child_list, size);
175 gi_source_function_new (void)
177 GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
182 gi_source_scanner_new (void)
184 GISourceScanner * scanner;
186 scanner = g_slice_new0 (GISourceScanner);
187 scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
189 scanner->struct_or_union_or_enum_table =
190 g_hash_table_new_full (g_str_hash, g_str_equal,
191 g_free, (GDestroyNotify)gi_source_symbol_unref);
197 gi_source_scanner_free (GISourceScanner *scanner)
199 g_free (scanner->current_filename);
201 g_hash_table_destroy (scanner->typedef_table);
202 g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
204 g_slist_foreach (scanner->comments, (GFunc)g_free, NULL);
205 g_slist_free (scanner->comments);
206 g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
207 g_slist_free (scanner->symbols);
209 g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
210 g_list_free (scanner->filenames);
215 gi_source_scanner_is_typedef (GISourceScanner *scanner,
218 gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
223 gi_source_scanner_set_macro_scan (GISourceScanner *scanner,
226 scanner->macro_scan = macro_scan;
230 gi_source_scanner_add_symbol (GISourceScanner *scanner,
231 GISourceSymbol *symbol)
233 gboolean found_filename = FALSE;
236 g_assert (scanner->current_filename);
237 for (l = scanner->filenames; l != NULL; l = l->next)
239 if (strcmp (l->data, scanner->current_filename) == 0)
241 found_filename = TRUE;
246 if (found_filename || scanner->macro_scan)
247 scanner->symbols = g_slist_prepend (scanner->symbols,
248 gi_source_symbol_ref (symbol));
249 /* TODO: Refcounted string here or some other optimization */
250 if (found_filename && symbol->source_filename == NULL)
252 symbol->source_filename = g_strdup (scanner->current_filename);
255 switch (symbol->type)
257 case CSYMBOL_TYPE_TYPEDEF:
258 g_hash_table_insert (scanner->typedef_table,
259 g_strdup (symbol->ident),
260 GINT_TO_POINTER (TRUE));
262 case CSYMBOL_TYPE_STRUCT:
263 case CSYMBOL_TYPE_UNION:
264 case CSYMBOL_TYPE_ENUM:
265 g_hash_table_insert (scanner->struct_or_union_or_enum_table,
266 g_strdup (symbol->ident),
267 gi_source_symbol_ref (symbol));
275 gi_source_scanner_get_symbols (GISourceScanner *scanner)
277 return g_slist_reverse (scanner->symbols);
281 gi_source_scanner_get_comments(GISourceScanner *scanner)
283 return g_slist_reverse (scanner->comments);