bf1e856bbb8076fc7dc8d21ad912dc70c243de98
[gnome.gobject-introspection] / girepository / girmodule.c
1 /* GObject introspection: Typelib creation 
2  *
3  * Copyright (C) 2005 Matthias Clasen
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "girmodule.h"
25 #include "girnode.h"
26
27 #define ALIGN_VALUE(this, boundary) \
28   (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
29
30
31 GIrModule *
32 g_ir_module_new (const gchar *name, 
33                  const gchar *version,
34                  const gchar *shared_library,
35                  const gchar *c_prefix)
36 {
37   GIrModule *module;
38   
39   module = g_new0 (GIrModule, 1);
40
41   module->name = g_strdup (name);
42   module->version = g_strdup (version);
43   if (shared_library)
44       module->shared_library = g_strdup (shared_library);
45   else
46       module->shared_library = NULL;
47   module->c_prefix = g_strdup (c_prefix);
48   module->dependencies = NULL;
49   module->entries = NULL;
50
51   module->include_modules = NULL;
52   module->aliases = NULL;
53
54   return module;
55 }
56
57 void
58 g_ir_module_free (GIrModule *module)
59 {
60   GList *e;
61
62   g_free (module->name);
63
64   for (e = module->entries; e; e = e->next)
65     g_ir_node_free ((GIrNode *)e->data);
66
67   g_list_free (module->entries);
68   /* Don't free dependencies, we inherit that from the parser */
69
70   g_list_free (module->include_modules);
71
72   g_hash_table_destroy (module->aliases);
73   g_hash_table_destroy (module->disguised_structures);
74
75   g_free (module);
76 }
77
78 static void
79 add_alias_foreach (gpointer key,
80                    gpointer value,
81                    gpointer data)
82 {
83   GIrModule *module = data;
84
85   g_hash_table_replace (module->aliases, g_strdup (key), g_strdup (value));
86 }
87
88 static void
89 add_disguised_structure_foreach (gpointer key,
90                                  gpointer value,
91                                  gpointer data)
92 {
93   GIrModule *module = data;
94
95   g_hash_table_replace (module->disguised_structures, g_strdup (key), value);
96 }
97
98 void
99 g_ir_module_add_include_module (GIrModule  *module,
100                                 GIrModule  *include_module)
101 {
102   module->include_modules = g_list_prepend (module->include_modules,
103                                             include_module);
104
105   g_hash_table_foreach (include_module->aliases,
106                         add_alias_foreach,
107                         module);
108
109   g_hash_table_foreach (include_module->disguised_structures,
110                         add_disguised_structure_foreach,
111                         module);
112 }
113
114 struct AttributeWriteData
115 {
116   guint count;
117   guchar *databuf;
118   GIrNode *node;
119   GHashTable *strings;
120   guint32 *offset;
121   guint32 *offset2;
122 };
123
124 static void
125 write_attribute (gpointer key, gpointer value, gpointer datap)
126 {
127   struct AttributeWriteData *data = datap;
128   guint32 old_offset = *(data->offset);
129   AttributeBlob *blob = (AttributeBlob*)&(data->databuf[old_offset]);
130
131   *(data->offset) += sizeof (AttributeBlob);
132
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);
136
137   data->count++;
138 }
139
140 static guint
141 write_attributes (GIrModule *module,
142                    GIrNode   *node,
143                    GHashTable *strings,
144                    guchar    *data,
145                    guint32   *offset,
146                    guint32   *offset2)
147 {
148   struct AttributeWriteData wdata;
149   wdata.count = 0;
150   wdata.databuf = data;
151   wdata.node = node;
152   wdata.offset = offset;
153   wdata.offset2 = offset2;
154   wdata.strings = strings;
155
156   g_hash_table_foreach (node->attributes, write_attribute, &wdata);
157
158   return wdata.count;
159 }
160
161 GTypelib *
162 g_ir_module_build_typelib (GIrModule  *module,
163                              GList       *modules)
164 {
165   GTypelib *typelib;
166   gsize length;
167   guint i;
168   GList *e;
169   Header *header;
170   DirEntry *entry;
171   guint32 header_size;
172   guint32 dir_size;
173   guint32 n_entries;
174   guint32 n_local_entries;
175   guint32 size, offset, offset2, old_offset;
176   GHashTable *strings;
177   GHashTable *types;
178   GList *offset_ordered_nodes;
179   char *dependencies;
180   guchar *data;
181
182   header_size = ALIGN_VALUE (sizeof (Header), 4);
183   n_local_entries = g_list_length (module->entries);
184
185   /* Serialize dependencies into one string; this is convenient
186    * and not a major change to the typelib format. */
187   {
188     GString *dependencies_str = g_string_new ("");
189     GList *link;
190     for (link = module->dependencies; link; link = link->next)
191       {
192         const char *dependency = link->data;
193         if (!strcmp (dependency, module->name))
194           continue;
195         g_string_append (dependencies_str, dependency);
196         if (link->next)
197           g_string_append_c (dependencies_str, '|');
198       }
199     dependencies = g_string_free (dependencies_str, FALSE);
200     if (!dependencies[0])
201       {
202         g_free (dependencies);
203         dependencies = NULL;
204       }
205   }
206
207  restart:
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);
213
214   g_message ("%d entries (%d local), %d dependencies\n", n_entries, n_local_entries,
215              g_list_length (module->dependencies));
216   
217   dir_size = n_entries * sizeof (DirEntry);
218   size = header_size + dir_size;
219
220   size += ALIGN_VALUE (strlen (module->name) + 1, 4);
221
222   for (e = module->entries; e; e = e->next)
223     {
224       GIrNode *node = e->data;
225       
226       size += g_ir_node_get_full_size (node);
227       size += g_ir_node_get_attribute_size (node);
228
229       /* Also reset the offset here */
230       node->offset = 0;
231     }
232
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);
241
242   g_message ("allocating %d bytes (%d header, %d directory, %d entries)\n", 
243           size, header_size, dir_size, size - header_size - dir_size);
244
245   data = g_malloc0 (size);
246
247   /* fill in header */
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.
259    */
260   if (dependencies != NULL)
261     header->dependencies = write_string (dependencies, strings, data, &header_size);
262   else
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)
269                              : 0);
270   if (module->c_prefix != NULL)
271     header->c_prefix = write_string (module->c_prefix, strings, data, &header_size);
272   else
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);
293
294   /* fill in directory and content */
295   entry = (DirEntry *)&data[header->directory];
296
297   offset2 = header->directory + dir_size;
298
299   for (e = module->entries, i = 0; e; e = e->next, i++)
300     {
301       GIrTypelibBuild build;
302       GIrNode *node = e->data;
303
304       if (strchr (node->name, '.'))
305         {
306           g_error ("Names may not contain '.'");
307         }
308
309       /* we picked up implicit xref nodes, start over */
310       if (i == n_entries)
311         {
312           GList *link;
313           g_message ("Found implicit cross references, starting over");
314
315           g_hash_table_destroy (strings);
316           g_hash_table_destroy (types);
317
318           /* Reset the cached offsets */
319           for (link = offset_ordered_nodes; link; link = link->next)
320             ((GIrNode *) link->data)->offset = 0;
321
322           g_list_free (offset_ordered_nodes);
323           strings = NULL;
324
325           g_free (data);
326           data = NULL;
327
328           goto restart;
329         }
330         
331       offset = offset2;
332
333       if (node->type == G_IR_NODE_XREF)
334         {
335           const char *namespace = ((GIrNodeXRef*)node)->namespace;
336           
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);
341         }
342       else
343         {
344           old_offset = offset;
345           offset2 = offset + g_ir_node_get_size (node);
346
347           entry->blob_type = node->type;
348           entry->local = TRUE;
349           entry->offset = offset;
350           entry->name = write_string (node->name, strings, data, &offset2);
351
352           build.module = module;
353           build.modules = modules;
354           build.strings = strings;
355           build.types = types;
356           build.offset_ordered_nodes = offset_ordered_nodes;
357           build.n_attributes = header->n_attributes;
358           build.data = data;
359           g_ir_node_build_typelib (node, NULL, &build, &offset, &offset2);
360
361           offset_ordered_nodes = build.offset_ordered_nodes;
362           header->n_attributes = build.n_attributes;
363
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));
366         }
367
368       entry++;
369     }
370
371   offset_ordered_nodes = g_list_reverse (offset_ordered_nodes);
372
373   g_message ("header: %d entries, %d attributes", header->n_entries, header->n_attributes);
374
375   _g_irnode_dump_stats ();
376
377   /* Write attributes after the blobs */
378   offset = offset2;
379   header->attributes = offset;
380   offset2 = offset + header->n_attributes * header->attribute_blob_size;
381
382   for (e = offset_ordered_nodes; e; e = e->next)
383     {
384       GIrNode *node = e->data;
385
386       write_attributes (module, node, strings, data, &offset, &offset2);
387     }
388   
389   g_message ("reallocating to %d bytes", offset2);
390
391   data = g_realloc (data, offset2);
392   header = (Header*) data;
393   length = header->size = offset2;
394   typelib = g_typelib_new_from_memory (data, length);
395
396   g_hash_table_destroy (strings);
397   g_hash_table_destroy (types);
398   g_list_free (offset_ordered_nodes);
399
400   return typelib;
401 }
402