From e0949633787945ca7ca8b2498f2bbcbf1255ff4b Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 18 Oct 2020 21:32:15 +0200
Subject: Updated the code for template types.

---
 plugins/itanium/component.c                    |  4 ++
 plugins/pychrysalide/analysis/types/template.c | 73 +++++++++++++++++++-------
 src/analysis/types/template.c                  | 14 +++--
 src/analysis/types/template.h                  |  2 +-
 4 files changed, 70 insertions(+), 23 deletions(-)

diff --git a/plugins/itanium/component.c b/plugins/itanium/component.c
index 3791c4c..d891c51 100644
--- a/plugins/itanium/component.c
+++ b/plugins/itanium/component.c
@@ -1500,6 +1500,7 @@ GDataType *itd_translate_component_to_type(const itanium_component *comp, Routin
                 }
 
                 g_template_type_set_name(G_TEMPLATE_TYPE(result), name);
+                free(name);
 
                 if (ns != NULL)
                     itd_prepend_namespace_to_type(result, ns);
@@ -1816,7 +1817,10 @@ GDataType *itd_translate_component_to_type(const itanium_component *comp, Routin
                     result = NULL;
                 }
                 else
+                {
                     g_template_type_add_param(G_TEMPLATE_TYPE(result), param);
+                    g_object_unref(G_OBJECT(param));
+                }
 
             }
 
diff --git a/plugins/pychrysalide/analysis/types/template.c b/plugins/pychrysalide/analysis/types/template.c
index 830eeb6..6fc50e4 100644
--- a/plugins/pychrysalide/analysis/types/template.c
+++ b/plugins/pychrysalide/analysis/types/template.c
@@ -75,6 +75,16 @@ static PyObject *py_template_type_new(PyTypeObject *type, PyObject *args, PyObje
     PyObject *result;                       /* Instance à retourner        */
     GDataType *dtype;                       /* Version GLib du type        */
 
+#define TEMPLATE_TYPE_DOC                                                   \
+    "The TemplateType class declares an empty template type.\n"             \
+    "\n"                                                                    \
+    "Instances can be created using the following constructor:\n"           \
+    "\n"                                                                    \
+    "    TemplateType()"                                                    \
+    "\n"                                                                    \
+    "Name and template parameters have then to be filled in the created"    \
+    " declaration with the relevant methods or properties."
+
     dtype = g_template_type_new();
     result = pygobject_new(G_OBJECT(dtype));
     g_object_unref(dtype);
@@ -99,19 +109,32 @@ static PyObject *py_template_type_new(PyTypeObject *type, PyObject *args, PyObje
 
 static PyObject *py_template_type_add_param(PyObject *self, PyObject *args)
 {
+    PyObject *result;                       /* Absence de retour Python    */
     GDataType *param;                       /* Version GLib du type        */
     int ret;                                /* Bilan de lecture des args.  */
     GTemplateType *type;                    /* Version GLib du type        */
 
+#define TEMPLATE_TYPE_ADD_PARAM_METHOD PYTHON_METHOD_DEF                \
+(                                                                       \
+    add_param, "$self, param, /",                                       \
+    METH_VARARGS, py_template_type,                                     \
+    "Add an extra parameter to the template type.\n"                    \
+    "\n"                                                                \
+    "This extra parameter has to be a pychrysalide.analysis.DataType"   \
+    " instance."                                                        \
+)
+
     ret = PyArg_ParseTuple(args, "O&", convert_to_data_type, &param);
     if (!ret) return NULL;
 
     type = G_TEMPLATE_TYPE(pygobject_get(self));
 
-    g_object_ref(G_OBJECT(param));
     g_template_type_add_param(type, param);
 
-    Py_RETURN_NONE;
+    result = Py_None;
+    Py_INCREF(result);
+
+    return result;
 
 }
 
@@ -135,11 +158,26 @@ static PyObject *py_template_type_get_name(PyObject *self, void *closure)
     GTemplateType *type;                    /* Version GLib du type        */
     const char *name;                       /* Désignation humaine         */
 
+#define TEMPLATE_TYPE_NAME_ATTRIB PYTHON_GETSET_DEF_FULL    \
+(                                                           \
+    name, py_template_type,                                 \
+    "Name of the template type.\n"                          \
+    "\n"                                                    \
+    "This property is a simple string, or None is the"      \
+    " template type has no name."                           \
+)
+
     type = G_TEMPLATE_TYPE(pygobject_get(self));
 
     name = g_template_type_get_name(type);
 
-    result = PyUnicode_FromString(name);
+    if (name == NULL)
+    {
+        result = Py_None;
+        Py_INCREF(result);
+    }
+    else
+        result = PyUnicode_FromString(name);
 
     return result;
 
@@ -172,7 +210,7 @@ static int py_template_type_set_name(PyObject *self, PyObject *value, void *clos
 
     type = G_TEMPLATE_TYPE(pygobject_get(self));
 
-    g_template_type_set_name(type, strdup(PyUnicode_DATA(value)));
+    g_template_type_set_name(type, PyUnicode_DATA(value));
 
     return 0;
 
@@ -201,6 +239,15 @@ static PyObject *py_template_type_get_params(PyObject *self, void *closure)
     size_t i;                               /* Boucle de parcours          */
     GDataType *param;                       /* Paramètre du gabarit        */
 
+#define TEMPLATE_TYPE_PARAMS_ATTRIB PYTHON_GET_DEF_FULL                 \
+(                                                                       \
+    params, py_template_type,                                           \
+    "List of all parameters of the template type.\n"                    \
+    "\n"                                                                \
+    "The returned value is a tuple of pychrysalide.analysis.DataType"   \
+    " instances."                                                       \
+)
+
     type = G_TEMPLATE_TYPE(pygobject_get(self));
 
     count = g_template_type_count_params(type);
@@ -237,23 +284,13 @@ static PyObject *py_template_type_get_params(PyObject *self, void *closure)
 PyTypeObject *get_python_template_type_type(void)
 {
     static PyMethodDef py_template_type_methods[] = {
-        {
-            "add_param", py_template_type_add_param,
-            METH_VARARGS,
-            "add_param($self, type, /)\n--\n\nAdd an extra parameter to the template type."
-        },
+        TEMPLATE_TYPE_ADD_PARAM_METHOD,
         { NULL }
     };
 
     static PyGetSetDef py_template_type_getseters[] = {
-        {
-            "name", py_template_type_get_name, py_template_type_set_name,
-            "Give access to the name of the template type.", NULL
-        },
-        {
-            "params", py_template_type_get_params, NULL,
-            "Give all parameters of the template type.", NULL
-        },
+        TEMPLATE_TYPE_NAME_ATTRIB,
+        TEMPLATE_TYPE_PARAMS_ATTRIB,
         { NULL }
     };
 
@@ -266,7 +303,7 @@ PyTypeObject *get_python_template_type_type(void)
 
         .tp_flags       = Py_TPFLAGS_DEFAULT,
 
-        .tp_doc         = "PyChrysalide template type",
+        .tp_doc         = TEMPLATE_TYPE_DOC,
 
         .tp_methods     = py_template_type_methods,
         .tp_getset      = py_template_type_getseters,
diff --git a/src/analysis/types/template.c b/src/analysis/types/template.c
index 01e74e7..75879f4 100644
--- a/src/analysis/types/template.c
+++ b/src/analysis/types/template.c
@@ -238,12 +238,15 @@ const char *g_template_type_get_name(GTemplateType *type)
 *                                                                             *
 ******************************************************************************/
 
-void g_template_type_set_name(GTemplateType *type, char *name)
+void g_template_type_set_name(GTemplateType *type, const char *name)
 {
     if (type->name != NULL)
         free(type->name);
 
-    type->name = name;
+    if (name == NULL)
+        type->name = NULL;
+    else
+        type->name = strdup(name);
 
 }
 
@@ -269,12 +272,13 @@ static GDataType *g_template_type_dup(const GTemplateType *type)
     result = G_TEMPLATE_TYPE(g_template_type_new());
 
     if (type->name != NULL)
-        g_template_type_set_name(result, strdup(type->name));
+        g_template_type_set_name(result, type->name);
 
     for (i = 0; i < type->count; i++)
     {
         param = g_data_type_dup(type->params[i]);
         g_template_type_add_param(result, param);
+        g_object_unref(G_OBJECT(param));
     }
 
     return G_DATA_TYPE(result);
@@ -348,7 +352,9 @@ static char *g_template_type_to_string(const GTemplateType *type, bool include)
 
 void g_template_type_add_param(GTemplateType *type, GDataType *param)
 {
-    type->params = (GDataType **)realloc(type->params, ++type->count * sizeof(GDataType *));
+    g_object_ref(G_OBJECT(param));
+
+    type->params = realloc(type->params, ++type->count * sizeof(GDataType *));
     type->params[type->count - 1] = param;
 
 }
diff --git a/src/analysis/types/template.h b/src/analysis/types/template.h
index 02043cd..ec69327 100644
--- a/src/analysis/types/template.h
+++ b/src/analysis/types/template.h
@@ -57,7 +57,7 @@ GDataType *g_template_type_new(void);
 const char *g_template_type_get_name(GTemplateType *);
 
 /* Précise la désignation principale du type. */
-void g_template_type_set_name(GTemplateType *, char *);
+void g_template_type_set_name(GTemplateType *, const char *);
 
 /* Ajoute un paramètre à un gabarit. */
 void g_template_type_add_param(GTemplateType *, GDataType *);
-- 
cgit v0.11.2-87-g4458