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));
115 gi_source_basic_type_new (const char *name)
117 GISourceType *basic_type = gi_source_type_new (CTYPE_BASIC_TYPE);
118 basic_type->name = g_strdup (name);
123 gi_source_typedef_new (const char *name)
125 GISourceType *typedef_ = gi_source_type_new (CTYPE_TYPEDEF);
126 typedef_->name = g_strdup (name);
131 gi_source_struct_new (const char *name)
133 GISourceType *struct_ = gi_source_type_new (CTYPE_STRUCT);
134 struct_->name = g_strdup (name);
139 gi_source_union_new (const char *name)
141 GISourceType *union_ = gi_source_type_new (CTYPE_UNION);
142 union_->name = g_strdup (name);
147 gi_source_enum_new (const char *name)
149 GISourceType *enum_ = gi_source_type_new (CTYPE_ENUM);
150 enum_->name = g_strdup (name);
155 gi_source_pointer_new (GISourceType * base_type)
157 GISourceType *pointer = gi_source_type_new (CTYPE_POINTER);
158 if (base_type != NULL)
159 pointer->base_type = gi_source_type_copy (base_type);
164 gi_source_array_new (GISourceSymbol *size)
166 GISourceType *array = gi_source_type_new (CTYPE_ARRAY);
167 if (size != NULL && size->type == CSYMBOL_TYPE_CONST && size->const_int_set)
168 array->child_list = g_list_append (array->child_list, size);
173 gi_source_function_new (void)
175 GISourceType *func = gi_source_type_new (CTYPE_FUNCTION);
180 gi_source_scanner_new (void)
182 GISourceScanner * scanner;
184 scanner = g_slice_new0 (GISourceScanner);
185 scanner->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal,
187 scanner->struct_or_union_or_enum_table =
188 g_hash_table_new_full (g_str_hash, g_str_equal,
189 g_free, (GDestroyNotify)gi_source_symbol_unref);
195 gi_source_scanner_free (GISourceScanner *scanner)
197 g_free (scanner->current_filename);
199 g_hash_table_destroy (scanner->typedef_table);
200 g_hash_table_destroy (scanner->struct_or_union_or_enum_table);
202 g_slist_foreach (scanner->comments, (GFunc)g_free, NULL);
203 g_slist_free (scanner->comments);
204 g_slist_foreach (scanner->symbols, (GFunc)gi_source_symbol_unref, NULL);
205 g_slist_free (scanner->symbols);
207 g_list_foreach (scanner->filenames, (GFunc)g_free, NULL);
208 g_list_free (scanner->filenames);
213 gi_source_scanner_is_typedef (GISourceScanner *scanner,
216 gboolean b = g_hash_table_lookup (scanner->typedef_table, name) != NULL;
221 gi_source_scanner_set_macro_scan (GISourceScanner *scanner,
224 scanner->macro_scan = macro_scan;
228 gi_source_scanner_add_symbol (GISourceScanner *scanner,
229 GISourceSymbol *symbol)
231 gboolean found_filename = FALSE;
234 g_assert (scanner->current_filename);
235 for (l = scanner->filenames; l != NULL; l = l->next)
237 if (strcmp (l->data, scanner->current_filename) == 0)
239 found_filename = TRUE;
244 if (found_filename || scanner->macro_scan)
245 scanner->symbols = g_slist_prepend (scanner->symbols,
246 gi_source_symbol_ref (symbol));
248 switch (symbol->type)
250 case CSYMBOL_TYPE_TYPEDEF:
251 g_hash_table_insert (scanner->typedef_table,
252 g_strdup (symbol->ident),
253 GINT_TO_POINTER (TRUE));
255 case CSYMBOL_TYPE_STRUCT:
256 case CSYMBOL_TYPE_UNION:
257 case CSYMBOL_TYPE_ENUM:
258 g_hash_table_insert (scanner->struct_or_union_or_enum_table,
259 g_strdup (symbol->ident),
260 gi_source_symbol_ref (symbol));
268 gi_source_scanner_get_symbols (GISourceScanner *scanner)
270 return g_slist_reverse (scanner->symbols);
274 gi_source_scanner_get_comments(GISourceScanner *scanner)
276 return g_slist_reverse (scanner->comments);