1 /* GObject introspection: Typelib creation
3 * Copyright (C) 2005 Matthias Clasen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include "girmodule.h"
27 #define ALIGN_VALUE(this, boundary) \
28 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
32 g_ir_module_new (const gchar *name,
34 const gchar *shared_library,
35 const gchar *c_prefix)
39 module = g_new0 (GIrModule, 1);
41 module->name = g_strdup (name);
42 module->version = g_strdup (version);
44 module->shared_library = g_strdup (shared_library);
46 module->shared_library = NULL;
47 module->c_prefix = g_strdup (c_prefix);
48 module->dependencies = NULL;
49 module->entries = NULL;
51 module->include_modules = NULL;
52 module->aliases = NULL;
58 g_ir_module_free (GIrModule *module)
62 g_free (module->name);
64 for (e = module->entries; e; e = e->next)
65 g_ir_node_free ((GIrNode *)e->data);
67 g_list_free (module->entries);
68 /* Don't free dependencies, we inherit that from the parser */
70 g_list_free (module->include_modules);
72 g_hash_table_destroy (module->aliases);
73 g_hash_table_destroy (module->disguised_structures);
79 add_alias_foreach (gpointer key,
83 GIrModule *module = data;
85 g_hash_table_replace (module->aliases, g_strdup (key), g_strdup (value));
89 add_disguised_structure_foreach (gpointer key,
93 GIrModule *module = data;
95 g_hash_table_replace (module->disguised_structures, g_strdup (key), value);
99 g_ir_module_add_include_module (GIrModule *module,
100 GIrModule *include_module)
102 module->include_modules = g_list_prepend (module->include_modules,
105 g_hash_table_foreach (include_module->aliases,
109 g_hash_table_foreach (include_module->disguised_structures,
110 add_disguised_structure_foreach,
114 struct AttributeWriteData
125 write_attribute (gpointer key, gpointer value, gpointer datap)
127 struct AttributeWriteData *data = datap;
128 guint32 old_offset = *(data->offset);
129 AttributeBlob *blob = (AttributeBlob*)&(data->databuf[old_offset]);
131 *(data->offset) += sizeof (AttributeBlob);
133 blob->offset = data->node->offset;
134 blob->name = write_string ((const char*) key, data->strings, data->databuf, data->offset2);
135 blob->value = write_string ((const char*) value, data->strings, data->databuf, data->offset2);
141 write_attributes (GIrModule *module,
148 struct AttributeWriteData wdata;
150 wdata.databuf = data;
152 wdata.offset = offset;
153 wdata.offset2 = offset2;
154 wdata.strings = strings;
156 g_hash_table_foreach (node->attributes, write_attribute, &wdata);
162 g_ir_module_build_typelib (GIrModule *module,
174 guint32 n_local_entries;
175 guint32 size, offset, offset2, old_offset;
178 GList *offset_ordered_nodes;
182 header_size = ALIGN_VALUE (sizeof (Header), 4);
183 n_local_entries = g_list_length (module->entries);
185 /* Serialize dependencies into one string; this is convenient
186 * and not a major change to the typelib format. */
188 GString *dependencies_str = g_string_new ("");
190 for (link = module->dependencies; link; link = link->next)
192 const char *dependency = link->data;
193 if (!strcmp (dependency, module->name))
195 g_string_append (dependencies_str, dependency);
197 g_string_append_c (dependencies_str, '|');
199 dependencies = g_string_free (dependencies_str, FALSE);
200 if (!dependencies[0])
202 g_free (dependencies);
208 _g_irnode_init_stats ();
209 strings = g_hash_table_new (g_str_hash, g_str_equal);
210 types = g_hash_table_new (g_str_hash, g_str_equal);
211 offset_ordered_nodes = NULL;
212 n_entries = g_list_length (module->entries);
214 g_message ("%d entries (%d local), %d dependencies\n", n_entries, n_local_entries,
215 g_list_length (module->dependencies));
217 dir_size = n_entries * sizeof (DirEntry);
218 size = header_size + dir_size;
220 size += ALIGN_VALUE (strlen (module->name) + 1, 4);
222 for (e = module->entries; e; e = e->next)
224 GIrNode *node = e->data;
226 size += g_ir_node_get_full_size (node);
227 size += g_ir_node_get_attribute_size (node);
229 /* Also reset the offset here */
233 /* Adjust size for strings allocated in header below specially */
234 size += ALIGN_VALUE (strlen (module->name) + 1, 4);
235 if (module->shared_library)
236 size += ALIGN_VALUE (strlen (module->shared_library) + 1, 4);
237 if (dependencies != NULL)
238 size += ALIGN_VALUE (strlen (dependencies) + 1, 4);
239 if (module->c_prefix != NULL)
240 size += ALIGN_VALUE (strlen (module->c_prefix) + 1, 4);
242 g_message ("allocating %d bytes (%d header, %d directory, %d entries)\n",
243 size, header_size, dir_size, size - header_size - dir_size);
245 data = g_malloc0 (size);
248 header = (Header *)data;
249 memcpy (header, G_IR_MAGIC, 16);
250 header->major_version = 2;
251 header->minor_version = 0;
252 header->reserved = 0;
253 header->n_entries = n_entries;
254 header->n_local_entries = n_local_entries;
255 header->n_attributes = 0;
256 header->attributes = 0; /* filled in later */
257 /* NOTE: When writing strings to the typelib here, you should also update
258 * the size calculations above.
260 if (dependencies != NULL)
261 header->dependencies = write_string (dependencies, strings, data, &header_size);
263 header->dependencies = 0;
264 header->size = 0; /* filled in later */
265 header->namespace = write_string (module->name, strings, data, &header_size);
266 header->nsversion = write_string (module->version, strings, data, &header_size);
267 header->shared_library = (module->shared_library?
268 write_string (module->shared_library, strings, data, &header_size)
270 if (module->c_prefix != NULL)
271 header->c_prefix = write_string (module->c_prefix, strings, data, &header_size);
273 header->c_prefix = 0;
274 header->directory = ALIGN_VALUE (header_size, 4);
275 header->entry_blob_size = sizeof (DirEntry);
276 header->function_blob_size = sizeof (FunctionBlob);
277 header->callback_blob_size = sizeof (CallbackBlob);
278 header->signal_blob_size = sizeof (SignalBlob);
279 header->vfunc_blob_size = sizeof (VFuncBlob);
280 header->arg_blob_size = sizeof (ArgBlob);
281 header->property_blob_size = sizeof (PropertyBlob);
282 header->field_blob_size = sizeof (FieldBlob);
283 header->value_blob_size = sizeof (ValueBlob);
284 header->constant_blob_size = sizeof (ConstantBlob);
285 header->error_domain_blob_size = sizeof (ErrorDomainBlob);
286 header->attribute_blob_size = sizeof (AttributeBlob);
287 header->signature_blob_size = sizeof (SignatureBlob);
288 header->enum_blob_size = sizeof (EnumBlob);
289 header->struct_blob_size = sizeof (StructBlob);
290 header->object_blob_size = sizeof(ObjectBlob);
291 header->interface_blob_size = sizeof (InterfaceBlob);
292 header->union_blob_size = sizeof (UnionBlob);
294 /* fill in directory and content */
295 entry = (DirEntry *)&data[header->directory];
297 offset2 = header->directory + dir_size;
299 for (e = module->entries, i = 0; e; e = e->next, i++)
301 GIrTypelibBuild build;
302 GIrNode *node = e->data;
304 if (strchr (node->name, '.'))
306 g_error ("Names may not contain '.'");
309 /* we picked up implicit xref nodes, start over */
313 g_message ("Found implicit cross references, starting over");
315 g_hash_table_destroy (strings);
316 g_hash_table_destroy (types);
318 /* Reset the cached offsets */
319 for (link = offset_ordered_nodes; link; link = link->next)
320 ((GIrNode *) link->data)->offset = 0;
322 g_list_free (offset_ordered_nodes);
333 if (node->type == G_IR_NODE_XREF)
335 const char *namespace = ((GIrNodeXRef*)node)->namespace;
337 entry->blob_type = 0;
338 entry->local = FALSE;
339 entry->offset = write_string (namespace, strings, data, &offset2);
340 entry->name = write_string (node->name, strings, data, &offset2);
345 offset2 = offset + g_ir_node_get_size (node);
347 entry->blob_type = node->type;
349 entry->offset = offset;
350 entry->name = write_string (node->name, strings, data, &offset2);
352 build.module = module;
353 build.modules = modules;
354 build.strings = strings;
356 build.offset_ordered_nodes = offset_ordered_nodes;
357 build.n_attributes = header->n_attributes;
359 g_ir_node_build_typelib (node, NULL, &build, &offset, &offset2);
361 offset_ordered_nodes = build.offset_ordered_nodes;
362 header->n_attributes = build.n_attributes;
364 if (offset2 > old_offset + g_ir_node_get_full_size (node))
365 g_error ("left a hole of %d bytes\n", offset2 - old_offset - g_ir_node_get_full_size (node));
371 offset_ordered_nodes = g_list_reverse (offset_ordered_nodes);
373 g_message ("header: %d entries, %d attributes", header->n_entries, header->n_attributes);
375 _g_irnode_dump_stats ();
377 /* Write attributes after the blobs */
379 header->attributes = offset;
380 offset2 = offset + header->n_attributes * header->attribute_blob_size;
382 for (e = offset_ordered_nodes; e; e = e->next)
384 GIrNode *node = e->data;
386 write_attributes (module, node, strings, data, &offset, &offset2);
389 g_message ("reallocating to %d bytes", offset2);
391 data = g_realloc (data, offset2);
392 header = (Header*) data;
393 length = header->size = offset2;
394 typelib = g_typelib_new_from_memory (data, length);
396 g_hash_table_destroy (strings);
397 g_hash_table_destroy (types);
398 g_list_free (offset_ordered_nodes);