@samitouri / QOSamiQemu / commits / 23ece6f887

qom/object: Add object_resolve_and_typecheck()

Add a new function object_resolve_and_typecheck(), whose purpose is to look up the object at a given QOM path, confirm that it is the expected type, and return it. This is similar to the existing object_resolve_path_type(), but it insists on a non-ambiguous path. We were already using this functionality internally to object.c as part of the object_resolve_link() function, so this patch implements the new function by pulling the link-property specific parts out of the more generic resolve-and-typecheck part. The motivation for this function is that we want to allow devices to provide an array of link properties; for that we will need to be able to provide the expected type of the linked object in a different way to the single-item link properties. Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Message-id: 20260327111700.795099-2-peter.maydell@linaro.org

Peter Maydell committed Mar 27, 2026 at 11:15 UTC 23ece6f88752167296c9525f15ef5935a469d374
2 files changed +40 -18
include/qom/object.h
+17
@@ -1742,6 +1742,23 @@ ObjectProperty *object_class_property_add_link(ObjectClass *oc,
1742 Object *val, Error **errp),
1743 ObjectPropertyLinkFlags flags);
1744
1745 +/**
1746 + * object_resolve_and_typecheck:
1747 + * @path: path to look up
1748 + * @name: name of property we are resolving for (used only in error messages)
1749 + * @target_type: QOM type we expect @path to resolve to
1750 + * @errp: error
1751 + *
1752 + * Look up the object at @path and return it. If it does not have the
1753 + * correct type @target_type, return NULL and set @errp.
1754 + *
1755 + * This is similar to object_resolve_path_type(), but it insists on a
1756 + * non-ambiguous path and it produces error messages that are
1757 + * specialised to the use case of setting a link property on an object.
1758 + */
1759 +Object *object_resolve_and_typecheck(const char *path, const char *name,
1760 + const char *target_type, Error **errp);
1761 +
1762 /**
1763 * object_property_add_str:
1764 * @obj: the object to add a property to
qom/object.c
+23 -18
@@ -1835,26 +1835,12 @@ static void object_get_link_property(Object *obj, Visitor *v,
1835 }
1836 }
1837
1838 -/*
1839 - * object_resolve_link:
1840 - *
1841 - * Lookup an object and ensure its type matches the link property type. This
1842 - * is similar to object_resolve_path() except type verification against the
1843 - * link property is performed.
1844 - *
1845 - * Returns: The matched object or NULL on path lookup failures.
1846 - */
1847 -static Object *object_resolve_link(Object *obj, const char *name,
1848 - const char *path, Error **errp)
1838 +Object *object_resolve_and_typecheck(const char *path, const char *name,
1839 + const char *target_type, Error **errp)
1840 {
1850 - const char *type;
1851 - char *target_type;
1841 bool ambiguous = false;
1842 Object *target;
1843
1855 - /* Go from link<FOO> to FOO. */
1856 - type = object_property_get_type(obj, name, NULL);
1857 - target_type = g_strndup(&type[5], strlen(type) - 6);
1844 target = object_resolve_path_type(path, target_type, &ambiguous);
1845
1846 if (ambiguous) {
@@ -1871,11 +1857,30 @@ static Object *object_resolve_link(Object *obj, const char *name,
1857 }
1858 target = NULL;
1859 }
1874 - g_free(target_type);
1875 -
1860 return target;
1861 }
1862
1863 +/*
1864 + * object_resolve_link:
1865 + *
1866 + * Lookup an object and ensure its type matches the link property type. This
1867 + * is similar to object_resolve_path() except type verification against the
1868 + * link property is performed.
1869 + *
1870 + * Returns: The matched object or NULL on path lookup failures.
1871 + */
1872 +static Object *object_resolve_link(Object *obj, const char *name,
1873 + const char *path, Error **errp)
1874 +{
1875 + const char *type;
1876 + g_autofree char *target_type = NULL;
1877 +
1878 + /* Go from link<FOO> to FOO. */
1879 + type = object_property_get_type(obj, name, NULL);
1880 + target_type = g_strndup(&type[5], strlen(type) - 6);
1881 + return object_resolve_and_typecheck(path, name, target_type, errp);
1882 +}
1883 +
1884 static void object_set_link_property(Object *obj, Visitor *v,
1885 const char *name, void *opaque,
1886 Error **errp)