gperl-i11n-info.c 6.97 KB
Newer Older
1 2
/* -*- mode: c; indent-tabs-mode: t; c-basic-offset: 8; -*- */

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
static GIFunctionInfo *
_find_struct_method (GIStructInfo *info, const gchar *method)
{
	/* g_struct_info_find_method is broken for class structs like
	 * GtkWidgetClass, so we search manually.  See
	 * <https://bugzilla.gnome.org/show_bug.cgi?id=700338>. */
	gint n_methods;
	gint i;
	n_methods = g_struct_info_get_n_methods (info);
	for (i = 0; i < n_methods; i++) {
		GIFunctionInfo *method_info =
			g_struct_info_get_method (info, i);
		if (strEQ (g_base_info_get_name (method_info), method))
			return method_info;
		g_base_info_unref (method_info);
	}
	return NULL;
}

22 23 24
static GIFunctionInfo *
_find_enum_method (GIEnumInfo *info, const gchar *method)
{
25
#if GI_CHECK_VERSION (1, 29, 17)
26 27 28 29 30 31 32 33 34 35
	gint n_methods;
	gint i;
	n_methods = g_enum_info_get_n_methods (info);
	for (i = 0; i < n_methods; i++) {
		GIFunctionInfo *method_info =
			g_enum_info_get_method (info, i);
		if (strEQ (g_base_info_get_name (method_info), method))
			return method_info;
		g_base_info_unref (method_info);
	}
36
#endif
37 38 39
	return NULL;
}

40 41 42 43 44 45 46
/* Caller owns return value */
static GIFunctionInfo *
get_function_info (GIRepository *repository,
                   const gchar *basename,
                   const gchar *namespace,
                   const gchar *method)
{
47
	dwarn ("%s, %s, %s\n", basename, namespace, method);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

	if (namespace) {
		GIFunctionInfo *function_info = NULL;
		GIBaseInfo *namespace_info = g_irepository_find_by_name (
			repository, basename, namespace);
		if (!namespace_info)
			ccroak ("Can't find information for namespace %s",
			       namespace);

		switch (g_base_info_get_type (namespace_info)) {
		    case GI_INFO_TYPE_OBJECT:
			function_info = g_object_info_find_method (
				(GIObjectInfo *) namespace_info,
				method);
			break;
		    case GI_INFO_TYPE_INTERFACE:
			function_info = g_interface_info_find_method (
				(GIInterfaceInfo *) namespace_info,
				method);
			break;
		    case GI_INFO_TYPE_BOXED:
		    case GI_INFO_TYPE_STRUCT:
70
			function_info = _find_struct_method (
71 72 73 74
				(GIStructInfo *) namespace_info,
				method);
			break;
                    case GI_INFO_TYPE_UNION:
75 76 77 78
			function_info = g_union_info_find_method (
				(GIUnionInfo *) namespace_info,
				method);
			break;
79 80 81 82 83 84
		    case GI_INFO_TYPE_ENUM:
		    case GI_INFO_TYPE_FLAGS:
			function_info = _find_enum_method (
				(GIEnumInfo *) namespace_info,
				method);
			break;
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		    default:
			ccroak ("Base info for namespace %s has incorrect type",
			       namespace);
		}

		if (!function_info)
			ccroak ("Can't find information for method "
			       "%s::%s", namespace, method);

		g_base_info_unref (namespace_info);

		return function_info;
	} else {
		GIBaseInfo *method_info = g_irepository_find_by_name (
			repository, basename, method);

		if (!method_info)
			ccroak ("Can't find information for method %s", method);

		switch (g_base_info_get_type (method_info)) {
		    case GI_INFO_TYPE_FUNCTION:
			return (GIFunctionInfo *) method_info;
		    default:
			ccroak ("Base info for method %s has incorrect type",
			       method);
		}
	}

	return NULL;
}

/* Caller owns return value */
static GIFieldInfo *
get_field_info (GIBaseInfo *info, const gchar *field_name)
{
	GIInfoType info_type;
	info_type = g_base_info_get_type (info);
	switch (info_type) {
	    case GI_INFO_TYPE_BOXED:
	    case GI_INFO_TYPE_STRUCT:
	    {
		gint n_fields, i;
		n_fields = g_struct_info_get_n_fields ((GIStructInfo *) info);
		for (i = 0; i < n_fields; i++) {
			GIFieldInfo *field_info;
			field_info = g_struct_info_get_field ((GIStructInfo *) info, i);
			if (0 == strcmp (field_name, g_base_info_get_name (field_info))) {
				return field_info;
			}
			g_base_info_unref (field_info);
		}
		break;
	    }
	    case GI_INFO_TYPE_UNION:
	    {
		gint n_fields, i;
		n_fields = g_union_info_get_n_fields ((GIStructInfo *) info);
		for (i = 0; i < n_fields; i++) {
			GIFieldInfo *field_info;
			field_info = g_union_info_get_field ((GIStructInfo *) info, i);
			if (0 == strcmp (field_name, g_base_info_get_name (field_info))) {
				return field_info;
			}
			g_base_info_unref (field_info);
		}
		break;
	    }
	    default:
		break;
	}
	return NULL;
}
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/* Caller owns return value */
static GISignalInfo *
get_signal_info (GIBaseInfo *container_info, const gchar *signal_name)
{
	if (GI_IS_OBJECT_INFO (container_info)) {
		return g_object_info_find_signal (container_info, signal_name);
	} else if (GI_IS_INTERFACE_INFO (container_info)) {
#if GI_CHECK_VERSION (1, 35, 4)
		return g_interface_info_find_signal (container_info, signal_name);
#else
{
		gint n_signals;
		gint i;
		n_signals = g_interface_info_get_n_signals (container_info);
		for (i = 0; i < n_signals; i++) {
			GISignalInfo *siginfo =
				g_interface_info_get_signal (container_info, i);
			if (strEQ (g_base_info_get_name (siginfo), signal_name))
				return siginfo;
			g_base_info_unref (siginfo);
		}
		return NULL;
}
#endif
	}
	return NULL;
}

186 187
/* Caller owns return value. */
static gchar *
188
synthesize_gtype_name (GIBaseInfo *info)
189 190 191 192
{
	const gchar *namespace = g_base_info_get_namespace (info);
	const gchar *name = g_base_info_get_name (info);
	if (0 == strncmp (namespace, "GObject", 8) ||
193
	    0 == strncmp (namespace, "GLib", 5))
194 195 196 197 198 199 200 201
	{
		namespace = "G";
	}
	return g_strconcat (namespace, name, NULL);
}

/* Caller owns return value. */
static gchar *
202
synthesize_prefixed_gtype_name (GIBaseInfo *info)
203 204 205 206
{
	const gchar *namespace = g_base_info_get_namespace (info);
	const gchar *name = g_base_info_get_name (info);
	if (0 == strncmp (namespace, "GObject", 8) ||
207
	    0 == strncmp (namespace, "GLib", 5))
208 209 210 211 212 213
	{
		namespace = "G";
	}
	return g_strconcat ("GPerlI11n", namespace, name, NULL);
}

214 215 216 217
static GType
get_gtype (GIRegisteredTypeInfo *info)
{
	GType gtype = g_registered_type_info_get_g_type (info);
218 219 220 221
	/* Fall back to the registered type name, and if that doesn't work
	 * either, construct the full name and the prefixed full name and try
	 * them. */
	if (!gtype || gtype == G_TYPE_NONE) {
222 223 224 225 226
		const gchar *type_name = g_registered_type_info_get_type_name (info);
		if (type_name) {
			gtype = g_type_from_name (type_name);
		}
	}
227
	if (!gtype || gtype == G_TYPE_NONE) {
228
		gchar *full_name = synthesize_gtype_name (info);
229 230 231 232
		gtype = g_type_from_name (full_name);
		g_free (full_name);
	}
	if (!gtype || gtype == G_TYPE_NONE) {
233
		gchar *full_name = synthesize_prefixed_gtype_name (info);
234 235 236 237
		gtype = g_type_from_name (full_name);
		g_free (full_name);
	}
	return gtype ? gtype : G_TYPE_NONE;
238
}
239 240 241 242 243 244 245 246 247

static const gchar *
get_package_for_basename (const gchar *basename)
{
	SV **svp;
	HV *basename_to_package =
		get_hv ("Glib::Object::Introspection::_BASENAME_TO_PACKAGE", 0);
	g_assert (basename_to_package);
	svp = hv_fetch (basename_to_package, basename, strlen (basename), 0);
248 249
	if (!svp || !gperl_sv_is_defined (*svp))
	    return NULL;
250 251
	return SvPV_nolen (*svp);
}
252 253 254 255 256 257 258 259 260

static gboolean
is_forbidden_sub_name (const gchar *name)
{
	HV *forbidden_sub_names =
		get_hv ("Glib::Object::Introspection::_FORBIDDEN_SUB_NAMES", 0);
	g_assert (forbidden_sub_names);
	return hv_exists (forbidden_sub_names, name, strlen (name));
}