Thanks. I updated the patch with these changes.

Is there any tool to automatically check the style?

On Thu, Oct 15, 2020 at 06:23:18PM +0200, Andrea Corallo wrote:
Antoni Boucher via Jit <j...@gcc.gnu.org> writes:

Hi Antoni,

Just had a quick look, please find some quite minor comments in line.

From b0edc9eb8e8d3ba9e1c6a8d061a8627c0b0cf102 Mon Sep 17 00:00:00 2001
From: Antoni Boucher <boua...@zoho.com>
Date: Sat, 1 Aug 2020 17:52:17 -0400
Subject: [PATCH] This patch add some reflection functions in the jit C api
 [PR96889]

2020-09-1  Antoni Boucher  <boua...@zoho.com>

    gcc/jit/
            PR target/96889
            * docs/topics/compatibility.rst (LIBGCCJIT_ABI_14): New ABI tag.
            * docs/topics/functions.rst: Add documentation for the
            functions gcc_jit_function_get_return_type and
            gcc_jit_function_get_param_count
            * libgccjit.c: New functions:
              * gcc_jit_function_get_return_type;
              * gcc_jit_function_get_param_count;
              * gcc_jit_function_type_get_return_type;
              * gcc_jit_function_type_get_param_count;
              * gcc_jit_function_type_get_param_type;
              * gcc_jit_type_unqualified;
              * gcc_jit_type_is_array;
              * gcc_jit_type_is_bool;
              * gcc_jit_type_is_function_ptr_type;
              * gcc_jit_type_is_int;
              * gcc_jit_type_is_pointer;
              * gcc_jit_type_is_vector;
              * gcc_jit_vector_type_get_element_type;
              * gcc_jit_vector_type_get_num_units;
              * gcc_jit_struct_get_field;
              * gcc_jit_type_is_struct;
              * gcc_jit_struct_get_field_count;
            * libgccjit.h
            * jit-recording.h: New functions (is_struct and is_vector)
            * libgccjit.map (LIBGCCJIT_ABI_14): New ABI tag.

    gcc/testsuite/
            PR target/96889
            * jit.dg/all-non-failing-tests.h: Add test-reflection.c.
            * jit.dg/test-reflection.c: New test.
---
 gcc/jit/docs/topics/compatibility.rst        |  41 +++
 gcc/jit/docs/topics/functions.rst            |  10 +
 gcc/jit/jit-recording.h                      |  16 ++
 gcc/jit/libgccjit.c                          | 254 +++++++++++++++++++
 gcc/jit/libgccjit.h                          | 117 +++++++++
 gcc/jit/libgccjit.map                        |  21 ++
 gcc/testsuite/jit.dg/all-non-failing-tests.h |  10 +
 gcc/testsuite/jit.dg/test-reflection.c       |  89 +++++++
 8 files changed, 558 insertions(+)
 create mode 100644 gcc/testsuite/jit.dg/test-reflection.c

diff --git a/gcc/jit/docs/topics/compatibility.rst 
b/gcc/jit/docs/topics/compatibility.rst
index 6bfa101ed71..16f24d20a75 100644
--- a/gcc/jit/docs/topics/compatibility.rst
+++ b/gcc/jit/docs/topics/compatibility.rst
@@ -226,3 +226,44 @@ entrypoints:
 --------------------
 ``LIBGCCJIT_ABI_14`` covers the addition of
 :func:`gcc_jit_global_set_initializer`
+
+.. _LIBGCCJIT_ABI_15:
+
+``LIBGCCJIT_ABI_15``
+--------------------
+``LIBGCCJIT_ABI_15`` covers the addition of reflection functions via API
+entrypoints:
+
+  * :func:`gcc_jit_function_get_return_type`
+
+  * :func:`gcc_jit_function_get_param_count`
+
+  * :func:`gcc_jit_type_is_array`
+
+  * :func:`gcc_jit_type_is_bool`
+
+  * :func:`gcc_jit_type_is_int`
+
+  * :func:`gcc_jit_type_is_pointer`
+
+  * :func:`gcc_jit_type_is_struct`
+
+  * :func:`gcc_jit_type_is_vector`
+
+  * :func:`gcc_jit_type_unqualified`
+
+  * :func:`gcc_jit_type_is_function_ptr_type`
+
+  * :func:`gcc_jit_function_type_get_return_type`
+
+  * :func:`gcc_jit_function_type_get_param_count`
+
+  * :func:`gcc_jit_function_type_get_param_type`
+
+  * :func:`gcc_jit_vector_type_get_num_units`
+
+  * :func:`gcc_jit_vector_type_get_element_type`
+
+  * :func:`gcc_jit_struct_get_field`
+
+  * :func:`gcc_jit_struct_get_field_count`
diff --git a/gcc/jit/docs/topics/functions.rst 
b/gcc/jit/docs/topics/functions.rst
index eb40d64010e..9819c28cda2 100644
--- a/gcc/jit/docs/topics/functions.rst
+++ b/gcc/jit/docs/topics/functions.rst
@@ -171,6 +171,16 @@ Functions
    underlying string, so it is valid to pass in a pointer to an on-stack
    buffer.

+.. function::  size_t \
+               gcc_jit_function_get_param_count (gcc_jit_function *func)
+
+   Get the number of parameters of the function.
+
+.. function::  gcc_jit_type \*
+               gcc_jit_function_get_return_type (gcc_jit_function *func)
+
+   Get the return type of the function.
+
 Blocks
 ------
 .. type:: gcc_jit_block
diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index 30e37aff387..525b8bc921d 100644
--- a/gcc/jit/jit-recording.h
+++ b/gcc/jit/jit-recording.h
@@ -538,7 +538,9 @@ public:
   virtual bool is_bool () const = 0;
   virtual type *is_pointer () = 0;
   virtual type *is_array () = 0;
+  virtual struct_ *is_struct () { return NULL; }
   virtual bool is_void () const { return false; }
+  virtual vector_type *is_vector () { return NULL; }
   virtual bool has_known_size () const { return true; }

   bool is_numeric () const
@@ -595,6 +597,8 @@ public:
   bool is_bool () const FINAL OVERRIDE;
   type *is_pointer () FINAL OVERRIDE { return dereference (); }
   type *is_array () FINAL OVERRIDE { return NULL; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
+  struct_ *is_struct () FINAL OVERRIDE { return NULL; }
   bool is_void () const FINAL OVERRIDE { return m_kind == GCC_JIT_TYPE_VOID; }

 public:
@@ -629,6 +633,8 @@ public:
   bool is_bool () const FINAL OVERRIDE { return false; }
   type *is_pointer () FINAL OVERRIDE { return m_other_type; }
   type *is_array () FINAL OVERRIDE { return NULL; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
+  struct_ *is_struct () FINAL OVERRIDE { return NULL; }

 private:
   string * make_debug_string () FINAL OVERRIDE;
@@ -655,6 +661,7 @@ public:
   bool is_bool () const FINAL OVERRIDE { return m_other_type->is_bool (); }
   type *is_pointer () FINAL OVERRIDE { return m_other_type->is_pointer (); }
   type *is_array () FINAL OVERRIDE { return m_other_type->is_array (); }
+  struct_ *is_struct () FINAL OVERRIDE { return m_other_type->is_struct (); }

 protected:
   type *m_other_type;
@@ -737,6 +744,8 @@ public:

   void replay_into (replayer *) FINAL OVERRIDE;

+  vector_type *is_vector () FINAL OVERRIDE { return this; }
+
 private:
   string * make_debug_string () FINAL OVERRIDE;
   void write_reproducer (reproducer &r) FINAL OVERRIDE;
@@ -765,6 +774,8 @@ class array_type : public type
   bool is_bool () const FINAL OVERRIDE { return false; }
   type *is_pointer () FINAL OVERRIDE { return NULL; }
   type *is_array () FINAL OVERRIDE { return m_element_type; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
+  struct_ *is_struct () FINAL OVERRIDE { return NULL; }
   int num_elements () { return m_num_elements; }

   void replay_into (replayer *) FINAL OVERRIDE;
@@ -799,6 +810,8 @@ public:
   bool is_bool () const FINAL OVERRIDE { return false; }
   type *is_pointer () FINAL OVERRIDE { return NULL; }
   type *is_array () FINAL OVERRIDE { return NULL; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
+  struct_ *is_struct () FINAL OVERRIDE { return NULL; }

   void replay_into (replayer *) FINAL OVERRIDE;

@@ -912,6 +925,7 @@ public:
   bool is_bool () const FINAL OVERRIDE { return false; }
   type *is_pointer () FINAL OVERRIDE { return NULL; }
   type *is_array () FINAL OVERRIDE { return NULL; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }

   bool has_known_size () const FINAL OVERRIDE { return m_fields != NULL; }

@@ -943,6 +957,8 @@ public:

   const char *access_as_type (reproducer &r) FINAL OVERRIDE;

+  struct_ *is_struct () FINAL OVERRIDE { return this; }
+
 private:
   string * make_debug_string () FINAL OVERRIDE;
   void write_reproducer (reproducer &r) FINAL OVERRIDE;
diff --git a/gcc/jit/libgccjit.c b/gcc/jit/libgccjit.c
index a00aefc7108..94a0f1955ec 100644
--- a/gcc/jit/libgccjit.c
+++ b/gcc/jit/libgccjit.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "tm.h"
 #include "timevar.h"
 #include "typed-splay-tree.h"
 #include "cppbuiltin.h"
@@ -60,6 +61,14 @@ struct gcc_jit_struct : public gcc::jit::recording::struct_
 {
 };

+struct gcc_jit_function_type : public gcc::jit::recording::function_type
+{
+};
+
+struct gcc_jit_vector_type : public gcc::jit::recording::vector_type
+{
+};
+
 struct gcc_jit_field : public gcc::jit::recording::field
 {
 };
@@ -510,6 +519,195 @@ gcc_jit_type_get_volatile (gcc_jit_type *type)
   return (gcc_jit_type *)type->get_volatile ();
 }

+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_array method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_is_array (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->is_array ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_bool method, in
+   jit-recording.c.  */
+
+int
+gcc_jit_type_is_bool (gcc_jit_type *type)
+{
+  RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
+
+  return type->is_bool ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_pointer method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_is_pointer (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->is_pointer ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_int method, in
+   jit-recording.c.  */
+
+int
+gcc_jit_type_is_int (gcc_jit_type *type)
+{
+  RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
+
+  return type->is_int ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_vector method, in
+   jit-recording.c.  */
+
+gcc_jit_vector_type *
+gcc_jit_type_is_vector (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::vector_type *vector_type = type->is_vector();
                                                                  ^^^
                                                                space here
+  return (gcc_jit_vector_type *)vector_type;
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_struct method, in
+   jit-recording.c.  */
+
+gcc_jit_struct *
+gcc_jit_type_is_struct (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::struct_ *struct_type = type->is_struct();

Likewise

+  return (gcc_jit_struct *)struct_type;
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::vector_type::get_num_units method, in
+   jit-recording.c.  */
+
+ssize_t
+gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
+{
+  RETURN_VAL_IF_FAIL (vector_type, -1, NULL, NULL, "NULL vector_type");
+  return vector_type->get_num_units ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::vector_type::get_element_type method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type)
+{
+  RETURN_NULL_IF_FAIL (vector_type, NULL, NULL, "NULL vector_type");
+  return (gcc_jit_type *)vector_type->get_element_type ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::unqualified method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_unqualified (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->unqualified ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::dyn_cast_function_type method, in
+   jit-recording.c.  */
+
+gcc_jit_function_type *
+gcc_jit_type_is_function_ptr_type (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::type *func_ptr_type = type->dereference ();
+  RETURN_NULL_IF_FAIL (func_ptr_type, NULL, NULL, "NULL type");
+  gcc::jit::recording::function_type *func_type = 
func_ptr_type->dyn_cast_function_type();

Likewise (also exceeded 80 characters).

+  RETURN_NULL_IF_FAIL (func_type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_function_type *)func_type;
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_return_type method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
+{
+  RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL struct_type");
+  return (gcc_jit_type *)function_type->get_return_type ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_param_types method, in
+   jit-recording.c.  */
+
+ssize_t
+gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
+{
+  RETURN_VAL_IF_FAIL (function_type, -1, NULL, NULL, "NULL struct_type");
+  return function_type->get_param_types ().length();

Likewise

+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_param_types method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type, 
int index)

Excedes 80 characters

+{
+  RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL struct_type");
+  int num_params = function_type->get_param_types ().length ();
+  gcc::jit::recording::context *ctxt = function_type->m_ctxt;
+  RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
+                              ctxt, NULL,
+                              "index of %d is too large (%s has %d params)",
+                              index,
+                              function_type->get_debug_string (),
+                              num_params);
+  return (gcc_jit_type *)function_type->get_param_types ()[index];
+}
+
 /* Public entrypoint.  See description in libgccjit.h.

    After error-checking, the real work is done by the
@@ -731,6 +929,33 @@ gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
                           (gcc::jit::recording::field **)fields);
 }

+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::fields::get_field method in
+   jit-recording.c.  */
+extern gcc_jit_field *
+gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
+                          int index)
+{
+  RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct type");
+  return (gcc_jit_field *)struct_type->get_fields()->get_field(index);

Space before (

+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, this calls the trivial
+   gcc::jit::recording::struct_::get_fields method in
+   jit-recording.h.  */
+
+ssize_t
+gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
+{
+  RETURN_VAL_IF_FAIL (struct_type, -1, NULL, NULL, "NULL struct type");
+  return struct_type->get_fields()->length();

Likewise

+}
+
 /* Public entrypoint.  See description in libgccjit.h.

    After error-checking, the real work is done by the
@@ -1012,6 +1237,35 @@ gcc_jit_function_get_param (gcc_jit_function *func, int 
index)
   return static_cast <gcc_jit_param *> (func->get_param (index));
 }

+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function::get_params method, in
+   jit-recording.h.
+  */
+
+ssize_t
+gcc_jit_function_get_param_count (gcc_jit_function *func)
+{
+  RETURN_VAL_IF_FAIL (func, -1, NULL, NULL, "NULL function");
+  gcc::jit::recording::context *ctxt = func->m_ctxt;
+  JIT_LOG_FUNC (ctxt->get_logger ());
+  return func->get_params ().length ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function::get_return_type method, in
+   jit-recording.h.  */
+
+gcc_jit_type *
+gcc_jit_function_get_return_type (gcc_jit_function *func)
+{
+    RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
+    return (gcc_jit_type *)func->get_return_type ();
+}
+
 /* Public entrypoint.  See description in libgccjit.h.

    After error-checking, the real work is done by the
diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h
index 7134841bb07..b0a3d941e9c 100644
--- a/gcc/jit/libgccjit.h
+++ b/gcc/jit/libgccjit.h
@@ -96,6 +96,12 @@ typedef struct gcc_jit_field gcc_jit_field;
    the layout for, or an opaque type.  */
 typedef struct gcc_jit_struct gcc_jit_struct;

+/* A gcc_jit_function_type encapsulates a function type.  */
+typedef struct gcc_jit_function_type gcc_jit_function_type;
+
+/* A gcc_jit_vector_type encapsulates a vector type.  */
+typedef struct gcc_jit_vector_type gcc_jit_vector_type;
+
 /* A gcc_jit_function encapsulates a function: either one that you're
    creating yourself, or a reference to one that you're dynamically
    linking to within the rest of the process.  */
@@ -586,6 +592,60 @@ gcc_jit_type_get_const (gcc_jit_type *type);
 extern gcc_jit_type *
 gcc_jit_type_get_volatile (gcc_jit_type *type);

+/* Given type "T", return TRUE if the type is an array.  */
+extern gcc_jit_type *
+gcc_jit_type_is_array (gcc_jit_type *type);
+
+/* Given type "T", return TRUE if the type is a bool.  */
+extern int
+gcc_jit_type_is_bool (gcc_jit_type *type);
+
+/* Given type "T", return the function type if it is one.  */
+extern gcc_jit_function_type *
+gcc_jit_type_is_function_ptr_type (gcc_jit_type *type);
+
+/* Given a function type, return its return type. */

GNU stile wants dot + 2 spaces at the end of a comment.

+extern gcc_jit_type *
+gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type);
+
+/* Given a function type, return its number of parameters. */

Likewise

+extern ssize_t
+gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type);
+
+/* Given a function type, return the type of the specified parameter. */

Likewise

+extern gcc_jit_type *
+gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type, 
int index);

Exceeds 80 characters

+
+/* Given type "T", return the type pointed by the pointer type
+ * or NULL if it's not a pointer.  */
+extern gcc_jit_type *
+gcc_jit_type_is_pointer (gcc_jit_type *type);
+
+/* Given type "T", return TRUE if the type is an int.  */
+extern int
+gcc_jit_type_is_int (gcc_jit_type *type);
+
+/* Given type "T", return the unqualified type, i.e. for a
+ * decorated type, return the type it wraps.  */
+extern gcc_jit_type *
+gcc_jit_type_unqualified (gcc_jit_type *type);
+
+/* Given type "T", return TRUE if the type is a vector type.  */
+extern gcc_jit_vector_type *
+gcc_jit_type_is_vector (gcc_jit_type *type);
+
+/* Given type "T", return the struct type or NULL.  */
+extern gcc_jit_struct *
+gcc_jit_type_is_struct (gcc_jit_type *type);
+
+/* Given a vector type, return the number of units it contains. */

One space more before */

+extern ssize_t
+gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type);
+
+/* Given a vector type, return the type of its elements. */

Likewise

+extern gcc_jit_type *
+gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type);
+
 /* Given type "T", get type "T[N]" (for a constant N).  */
 extern gcc_jit_type *
 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
@@ -647,6 +707,15 @@ gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
                           int num_fields,
                           gcc_jit_field **fields);

+/* Get a field by index. */

Likewise

+extern gcc_jit_field *
+gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
+                          int index);
+
+/* Get the number of fields. */

Likewise

+extern ssize_t
+gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type);
+
 /* Unions work similarly to structs.  */
 extern gcc_jit_type *
 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
@@ -740,6 +809,14 @@ gcc_jit_function_as_object (gcc_jit_function *func);
 extern gcc_jit_param *
 gcc_jit_function_get_param (gcc_jit_function *func, int index);

+/* Get the number of params of a function.  */
+extern ssize_t
+gcc_jit_function_get_param_count (gcc_jit_function *func);
+
+/* Get the return type of a function.  */
+extern gcc_jit_type *
+gcc_jit_function_get_return_type (gcc_jit_function *func);
+
 /* Emit the function in graphviz format.  */
 extern void
 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
@@ -1518,6 +1595,46 @@ gcc_jit_version_minor (void);
 extern int
 gcc_jit_version_patchlevel (void);

+#define LIBGCCJIT_HAVE_gcc_jit_function_reflection
+
+/* Reflection functions to get the number of parameters, return type of
+   a function and whether a type is a bool from the C API.
+
+   This API entrypoint was added in LIBGCCJIT_ABI_15; you can test for its
+   presence using
+     #ifdef LIBGCCJIT_HAVE_gcc_jit_function_reflection
+*/
+extern gcc_jit_type *
+gcc_jit_function_get_return_type (gcc_jit_function *func);
+extern ssize_t
+gcc_jit_function_get_param_count (gcc_jit_function *func);
+extern gcc_jit_type *
+gcc_jit_type_is_array (gcc_jit_type *type);
+extern int
+gcc_jit_type_is_bool (gcc_jit_type *type);
+extern gcc_jit_function_type *
+gcc_jit_type_is_function_ptr_type (gcc_jit_type *type);
+extern gcc_jit_type *
+gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type);
+extern ssize_t
+gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type);
+extern gcc_jit_type *
+gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type, 
int index);

Exceeds 80 characters

+extern int
+gcc_jit_type_is_int (gcc_jit_type *type);
+extern gcc_jit_type *
+gcc_jit_type_is_pointer (gcc_jit_type *type);
+extern gcc_jit_vector_type *
+gcc_jit_type_is_vector (gcc_jit_type *type);
+extern gcc_jit_struct *
+gcc_jit_type_is_struct (gcc_jit_type *type);
+extern ssize_t
+gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type);
+extern gcc_jit_type *
+gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type);
+extern gcc_jit_type *
+gcc_jit_type_unqualified (gcc_jit_type *type);

Apologies for the nit picking :)

 Andrea
>From 21fff4bdad3691f7ad5a77735062ce96701db511 Mon Sep 17 00:00:00 2001
From: Antoni Boucher <boua...@zoho.com>
Date: Sat, 1 Aug 2020 17:52:17 -0400
Subject: [PATCH] This patch add some reflection functions in the jit C api
 [PR96889]

2020-09-1  Antoni Boucher  <boua...@zoho.com>

    gcc/jit/
            PR target/96889
            * docs/topics/compatibility.rst (LIBGCCJIT_ABI_14): New ABI tag.
            * docs/topics/functions.rst: Add documentation for the
            functions gcc_jit_function_get_return_type and
            gcc_jit_function_get_param_count
            * libgccjit.c: New functions:
              * gcc_jit_function_get_return_type;
              * gcc_jit_function_get_param_count;
              * gcc_jit_function_type_get_return_type;
              * gcc_jit_function_type_get_param_count;
              * gcc_jit_function_type_get_param_type;
              * gcc_jit_type_unqualified;
              * gcc_jit_type_is_array;
              * gcc_jit_type_is_bool;
              * gcc_jit_type_is_function_ptr_type;
              * gcc_jit_type_is_int;
              * gcc_jit_type_is_pointer;
              * gcc_jit_type_is_vector;
              * gcc_jit_vector_type_get_element_type;
              * gcc_jit_vector_type_get_num_units;
              * gcc_jit_struct_get_field;
              * gcc_jit_type_is_struct;
              * gcc_jit_struct_get_field_count;
            * libgccjit.h
            * jit-recording.h: New functions (is_struct and is_vector)
            * libgccjit.map (LIBGCCJIT_ABI_14): New ABI tag.

    gcc/testsuite/
            PR target/96889
            * jit.dg/all-non-failing-tests.h: Add test-reflection.c.
            * jit.dg/test-reflection.c: New test.
---
 gcc/jit/docs/topics/compatibility.rst        |  41 +++
 gcc/jit/docs/topics/functions.rst            |  10 +
 gcc/jit/jit-recording.h                      |  16 ++
 gcc/jit/libgccjit.c                          | 256 +++++++++++++++++++
 gcc/jit/libgccjit.h                          | 119 +++++++++
 gcc/jit/libgccjit.map                        |  21 ++
 gcc/testsuite/jit.dg/all-non-failing-tests.h |  10 +
 gcc/testsuite/jit.dg/test-reflection.c       |  89 +++++++
 8 files changed, 562 insertions(+)
 create mode 100644 gcc/testsuite/jit.dg/test-reflection.c

diff --git a/gcc/jit/docs/topics/compatibility.rst 
b/gcc/jit/docs/topics/compatibility.rst
index 6bfa101ed71..16f24d20a75 100644
--- a/gcc/jit/docs/topics/compatibility.rst
+++ b/gcc/jit/docs/topics/compatibility.rst
@@ -226,3 +226,44 @@ entrypoints:
 --------------------
 ``LIBGCCJIT_ABI_14`` covers the addition of
 :func:`gcc_jit_global_set_initializer`
+
+.. _LIBGCCJIT_ABI_15:
+
+``LIBGCCJIT_ABI_15``
+--------------------
+``LIBGCCJIT_ABI_15`` covers the addition of reflection functions via API
+entrypoints:
+
+  * :func:`gcc_jit_function_get_return_type`
+
+  * :func:`gcc_jit_function_get_param_count`
+
+  * :func:`gcc_jit_type_is_array`
+
+  * :func:`gcc_jit_type_is_bool`
+
+  * :func:`gcc_jit_type_is_int`
+
+  * :func:`gcc_jit_type_is_pointer`
+
+  * :func:`gcc_jit_type_is_struct`
+
+  * :func:`gcc_jit_type_is_vector`
+
+  * :func:`gcc_jit_type_unqualified`
+
+  * :func:`gcc_jit_type_is_function_ptr_type`
+
+  * :func:`gcc_jit_function_type_get_return_type`
+
+  * :func:`gcc_jit_function_type_get_param_count`
+
+  * :func:`gcc_jit_function_type_get_param_type`
+
+  * :func:`gcc_jit_vector_type_get_num_units`
+
+  * :func:`gcc_jit_vector_type_get_element_type`
+
+  * :func:`gcc_jit_struct_get_field`
+
+  * :func:`gcc_jit_struct_get_field_count`
diff --git a/gcc/jit/docs/topics/functions.rst 
b/gcc/jit/docs/topics/functions.rst
index eb40d64010e..9819c28cda2 100644
--- a/gcc/jit/docs/topics/functions.rst
+++ b/gcc/jit/docs/topics/functions.rst
@@ -171,6 +171,16 @@ Functions
    underlying string, so it is valid to pass in a pointer to an on-stack
    buffer.
 
+.. function::  size_t \
+               gcc_jit_function_get_param_count (gcc_jit_function *func)
+
+   Get the number of parameters of the function.
+
+.. function::  gcc_jit_type \*
+               gcc_jit_function_get_return_type (gcc_jit_function *func)
+
+   Get the return type of the function.
+
 Blocks
 ------
 .. type:: gcc_jit_block
diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index 30e37aff387..525b8bc921d 100644
--- a/gcc/jit/jit-recording.h
+++ b/gcc/jit/jit-recording.h
@@ -538,7 +538,9 @@ public:
   virtual bool is_bool () const = 0;
   virtual type *is_pointer () = 0;
   virtual type *is_array () = 0;
+  virtual struct_ *is_struct () { return NULL; }
   virtual bool is_void () const { return false; }
+  virtual vector_type *is_vector () { return NULL; }
   virtual bool has_known_size () const { return true; }
 
   bool is_numeric () const
@@ -595,6 +597,8 @@ public:
   bool is_bool () const FINAL OVERRIDE;
   type *is_pointer () FINAL OVERRIDE { return dereference (); }
   type *is_array () FINAL OVERRIDE { return NULL; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
+  struct_ *is_struct () FINAL OVERRIDE { return NULL; }
   bool is_void () const FINAL OVERRIDE { return m_kind == GCC_JIT_TYPE_VOID; }
 
 public:
@@ -629,6 +633,8 @@ public:
   bool is_bool () const FINAL OVERRIDE { return false; }
   type *is_pointer () FINAL OVERRIDE { return m_other_type; }
   type *is_array () FINAL OVERRIDE { return NULL; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
+  struct_ *is_struct () FINAL OVERRIDE { return NULL; }
 
 private:
   string * make_debug_string () FINAL OVERRIDE;
@@ -655,6 +661,7 @@ public:
   bool is_bool () const FINAL OVERRIDE { return m_other_type->is_bool (); }
   type *is_pointer () FINAL OVERRIDE { return m_other_type->is_pointer (); }
   type *is_array () FINAL OVERRIDE { return m_other_type->is_array (); }
+  struct_ *is_struct () FINAL OVERRIDE { return m_other_type->is_struct (); }
 
 protected:
   type *m_other_type;
@@ -737,6 +744,8 @@ public:
 
   void replay_into (replayer *) FINAL OVERRIDE;
 
+  vector_type *is_vector () FINAL OVERRIDE { return this; }
+
 private:
   string * make_debug_string () FINAL OVERRIDE;
   void write_reproducer (reproducer &r) FINAL OVERRIDE;
@@ -765,6 +774,8 @@ class array_type : public type
   bool is_bool () const FINAL OVERRIDE { return false; }
   type *is_pointer () FINAL OVERRIDE { return NULL; }
   type *is_array () FINAL OVERRIDE { return m_element_type; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
+  struct_ *is_struct () FINAL OVERRIDE { return NULL; }
   int num_elements () { return m_num_elements; }
 
   void replay_into (replayer *) FINAL OVERRIDE;
@@ -799,6 +810,8 @@ public:
   bool is_bool () const FINAL OVERRIDE { return false; }
   type *is_pointer () FINAL OVERRIDE { return NULL; }
   type *is_array () FINAL OVERRIDE { return NULL; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
+  struct_ *is_struct () FINAL OVERRIDE { return NULL; }
 
   void replay_into (replayer *) FINAL OVERRIDE;
 
@@ -912,6 +925,7 @@ public:
   bool is_bool () const FINAL OVERRIDE { return false; }
   type *is_pointer () FINAL OVERRIDE { return NULL; }
   type *is_array () FINAL OVERRIDE { return NULL; }
+  vector_type *is_vector () FINAL OVERRIDE { return NULL; }
 
   bool has_known_size () const FINAL OVERRIDE { return m_fields != NULL; }
 
@@ -943,6 +957,8 @@ public:
 
   const char *access_as_type (reproducer &r) FINAL OVERRIDE;
 
+  struct_ *is_struct () FINAL OVERRIDE { return this; }
+
 private:
   string * make_debug_string () FINAL OVERRIDE;
   void write_reproducer (reproducer &r) FINAL OVERRIDE;
diff --git a/gcc/jit/libgccjit.c b/gcc/jit/libgccjit.c
index a00aefc7108..8e6a9e85ff0 100644
--- a/gcc/jit/libgccjit.c
+++ b/gcc/jit/libgccjit.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "tm.h"
 #include "timevar.h"
 #include "typed-splay-tree.h"
 #include "cppbuiltin.h"
@@ -60,6 +61,14 @@ struct gcc_jit_struct : public gcc::jit::recording::struct_
 {
 };
 
+struct gcc_jit_function_type : public gcc::jit::recording::function_type
+{
+};
+
+struct gcc_jit_vector_type : public gcc::jit::recording::vector_type
+{
+};
+
 struct gcc_jit_field : public gcc::jit::recording::field
 {
 };
@@ -510,6 +519,197 @@ gcc_jit_type_get_volatile (gcc_jit_type *type)
   return (gcc_jit_type *)type->get_volatile ();
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_array method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_is_array (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->is_array ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_bool method, in
+   jit-recording.c.  */
+
+int
+gcc_jit_type_is_bool (gcc_jit_type *type)
+{
+  RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
+
+  return type->is_bool ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_pointer method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_is_pointer (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->is_pointer ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_int method, in
+   jit-recording.c.  */
+
+int
+gcc_jit_type_is_int (gcc_jit_type *type)
+{
+  RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
+
+  return type->is_int ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_vector method, in
+   jit-recording.c.  */
+
+gcc_jit_vector_type *
+gcc_jit_type_is_vector (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::vector_type *vector_type = type->is_vector ();
+  return (gcc_jit_vector_type *)vector_type;
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_struct method, in
+   jit-recording.c.  */
+
+gcc_jit_struct *
+gcc_jit_type_is_struct (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::struct_ *struct_type = type->is_struct ();
+  return (gcc_jit_struct *)struct_type;
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::vector_type::get_num_units method, in
+   jit-recording.c.  */
+
+ssize_t
+gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
+{
+  RETURN_VAL_IF_FAIL (vector_type, -1, NULL, NULL, "NULL vector_type");
+  return vector_type->get_num_units ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::vector_type::get_element_type method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type)
+{
+  RETURN_NULL_IF_FAIL (vector_type, NULL, NULL, "NULL vector_type");
+  return (gcc_jit_type *)vector_type->get_element_type ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::unqualified method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_unqualified (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->unqualified ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::dyn_cast_function_type method, in
+   jit-recording.c.  */
+
+gcc_jit_function_type *
+gcc_jit_type_is_function_ptr_type (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::type *func_ptr_type = type->dereference ();
+  RETURN_NULL_IF_FAIL (func_ptr_type, NULL, NULL, "NULL type");
+  gcc::jit::recording::function_type *func_type =
+    func_ptr_type->dyn_cast_function_type ();
+  RETURN_NULL_IF_FAIL (func_type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_function_type *)func_type;
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_return_type method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
+{
+  RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL struct_type");
+  return (gcc_jit_type *)function_type->get_return_type ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_param_types method, in
+   jit-recording.c.  */
+
+ssize_t
+gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
+{
+  RETURN_VAL_IF_FAIL (function_type, -1, NULL, NULL, "NULL struct_type");
+  return function_type->get_param_types ().length ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_param_types method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
+                               int index)
+{
+  RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL struct_type");
+  int num_params = function_type->get_param_types ().length ();
+  gcc::jit::recording::context *ctxt = function_type->m_ctxt;
+  RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
+                              ctxt, NULL,
+                              "index of %d is too large (%s has %d params)",
+                              index,
+                              function_type->get_debug_string (),
+                              num_params);
+  return (gcc_jit_type *)function_type->get_param_types ()[index];
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
@@ -731,6 +931,33 @@ gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
                           (gcc::jit::recording::field **)fields);
 }
 
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::fields::get_field method in
+   jit-recording.c.  */
+extern gcc_jit_field *
+gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
+                          int index)
+{
+  RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct type");
+  return (gcc_jit_field *)struct_type->get_fields ()->get_field (index);
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, this calls the trivial
+   gcc::jit::recording::struct_::get_fields method in
+   jit-recording.h.  */
+
+ssize_t
+gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
+{
+  RETURN_VAL_IF_FAIL (struct_type, -1, NULL, NULL, "NULL struct type");
+  return struct_type->get_fields ()->length ();
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
@@ -1012,6 +1239,35 @@ gcc_jit_function_get_param (gcc_jit_function *func, int 
index)
   return static_cast <gcc_jit_param *> (func->get_param (index));
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function::get_params method, in
+   jit-recording.h.
+  */
+
+ssize_t
+gcc_jit_function_get_param_count (gcc_jit_function *func)
+{
+  RETURN_VAL_IF_FAIL (func, -1, NULL, NULL, "NULL function");
+  gcc::jit::recording::context *ctxt = func->m_ctxt;
+  JIT_LOG_FUNC (ctxt->get_logger ());
+  return func->get_params ().length ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function::get_return_type method, in
+   jit-recording.h.  */
+
+gcc_jit_type *
+gcc_jit_function_get_return_type (gcc_jit_function *func)
+{
+    RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
+    return (gcc_jit_type *)func->get_return_type ();
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h
index 7134841bb07..6056d00a3ea 100644
--- a/gcc/jit/libgccjit.h
+++ b/gcc/jit/libgccjit.h
@@ -96,6 +96,12 @@ typedef struct gcc_jit_field gcc_jit_field;
    the layout for, or an opaque type.  */
 typedef struct gcc_jit_struct gcc_jit_struct;
 
+/* A gcc_jit_function_type encapsulates a function type.  */
+typedef struct gcc_jit_function_type gcc_jit_function_type;
+
+/* A gcc_jit_vector_type encapsulates a vector type.  */
+typedef struct gcc_jit_vector_type gcc_jit_vector_type;
+
 /* A gcc_jit_function encapsulates a function: either one that you're
    creating yourself, or a reference to one that you're dynamically
    linking to within the rest of the process.  */
@@ -586,6 +592,61 @@ gcc_jit_type_get_const (gcc_jit_type *type);
 extern gcc_jit_type *
 gcc_jit_type_get_volatile (gcc_jit_type *type);
 
+/* Given type "T", return TRUE if the type is an array.  */
+extern gcc_jit_type *
+gcc_jit_type_is_array (gcc_jit_type *type);
+
+/* Given type "T", return TRUE if the type is a bool.  */
+extern int
+gcc_jit_type_is_bool (gcc_jit_type *type);
+
+/* Given type "T", return the function type if it is one.  */
+extern gcc_jit_function_type *
+gcc_jit_type_is_function_ptr_type (gcc_jit_type *type);
+
+/* Given a function type, return its return type.  */
+extern gcc_jit_type *
+gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type);
+
+/* Given a function type, return its number of parameters.  */
+extern ssize_t
+gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type);
+
+/* Given a function type, return the type of the specified parameter.  */
+extern gcc_jit_type *
+gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
+                               int index);
+
+/* Given type "T", return the type pointed by the pointer type
+ * or NULL if it's not a pointer.  */
+extern gcc_jit_type *
+gcc_jit_type_is_pointer (gcc_jit_type *type);
+
+/* Given type "T", return TRUE if the type is an int.  */
+extern int
+gcc_jit_type_is_int (gcc_jit_type *type);
+
+/* Given type "T", return the unqualified type, i.e. for a
+ * decorated type, return the type it wraps.  */
+extern gcc_jit_type *
+gcc_jit_type_unqualified (gcc_jit_type *type);
+
+/* Given type "T", return TRUE if the type is a vector type.  */
+extern gcc_jit_vector_type *
+gcc_jit_type_is_vector (gcc_jit_type *type);
+
+/* Given type "T", return the struct type or NULL.  */
+extern gcc_jit_struct *
+gcc_jit_type_is_struct (gcc_jit_type *type);
+
+/* Given a vector type, return the number of units it contains.  */
+extern ssize_t
+gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type);
+
+/* Given a vector type, return the type of its elements.  */
+extern gcc_jit_type *
+gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type);
+
 /* Given type "T", get type "T[N]" (for a constant N).  */
 extern gcc_jit_type *
 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
@@ -647,6 +708,15 @@ gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
                           int num_fields,
                           gcc_jit_field **fields);
 
+/* Get a field by index.  */
+extern gcc_jit_field *
+gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
+                          int index);
+
+/* Get the number of fields.  */
+extern ssize_t
+gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type);
+
 /* Unions work similarly to structs.  */
 extern gcc_jit_type *
 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
@@ -740,6 +810,14 @@ gcc_jit_function_as_object (gcc_jit_function *func);
 extern gcc_jit_param *
 gcc_jit_function_get_param (gcc_jit_function *func, int index);
 
+/* Get the number of params of a function.  */
+extern ssize_t
+gcc_jit_function_get_param_count (gcc_jit_function *func);
+
+/* Get the return type of a function.  */
+extern gcc_jit_type *
+gcc_jit_function_get_return_type (gcc_jit_function *func);
+
 /* Emit the function in graphviz format.  */
 extern void
 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
@@ -1518,6 +1596,47 @@ gcc_jit_version_minor (void);
 extern int
 gcc_jit_version_patchlevel (void);
 
+#define LIBGCCJIT_HAVE_gcc_jit_function_reflection
+
+/* Reflection functions to get the number of parameters, return type of
+   a function and whether a type is a bool from the C API.
+
+   This API entrypoint was added in LIBGCCJIT_ABI_15; you can test for its
+   presence using
+     #ifdef LIBGCCJIT_HAVE_gcc_jit_function_reflection
+*/
+extern gcc_jit_type *
+gcc_jit_function_get_return_type (gcc_jit_function *func);
+extern ssize_t
+gcc_jit_function_get_param_count (gcc_jit_function *func);
+extern gcc_jit_type *
+gcc_jit_type_is_array (gcc_jit_type *type);
+extern int
+gcc_jit_type_is_bool (gcc_jit_type *type);
+extern gcc_jit_function_type *
+gcc_jit_type_is_function_ptr_type (gcc_jit_type *type);
+extern gcc_jit_type *
+gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type);
+extern ssize_t
+gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type);
+extern gcc_jit_type *
+gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
+                               int index);
+extern int
+gcc_jit_type_is_int (gcc_jit_type *type);
+extern gcc_jit_type *
+gcc_jit_type_is_pointer (gcc_jit_type *type);
+extern gcc_jit_vector_type *
+gcc_jit_type_is_vector (gcc_jit_type *type);
+extern gcc_jit_struct *
+gcc_jit_type_is_struct (gcc_jit_type *type);
+extern ssize_t
+gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type);
+extern gcc_jit_type *
+gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type);
+extern gcc_jit_type *
+gcc_jit_type_unqualified (gcc_jit_type *type);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map
index a6e67e781a4..84d40f5a636 100644
--- a/gcc/jit/libgccjit.map
+++ b/gcc/jit/libgccjit.map
@@ -192,3 +192,24 @@ LIBGCCJIT_ABI_14 {
   global:
     gcc_jit_global_set_initializer;
 } LIBGCCJIT_ABI_13;
+
+LIBGCCJIT_ABI_15 {
+  global:
+    gcc_jit_function_get_return_type;
+    gcc_jit_function_get_param_count;
+    gcc_jit_function_type_get_return_type;
+    gcc_jit_function_type_get_param_count;
+    gcc_jit_function_type_get_param_type;
+    gcc_jit_type_unqualified;
+    gcc_jit_type_is_array;
+    gcc_jit_type_is_bool;
+    gcc_jit_type_is_function_ptr_type;
+    gcc_jit_type_is_int;
+    gcc_jit_type_is_pointer;
+    gcc_jit_type_is_vector;
+    gcc_jit_vector_type_get_element_type;
+    gcc_jit_vector_type_get_num_units;
+    gcc_jit_struct_get_field;
+    gcc_jit_type_is_struct;
+    gcc_jit_struct_get_field_count;
+} LIBGCCJIT_ABI_14;
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h 
b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index 4202eb7798b..a269144d2df 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -258,6 +258,13 @@
 #undef create_code
 #undef verify_code
 
+/* test-reflection.c */
+#define create_code create_code_reflection
+#define verify_code verify_code_reflection
+#include "test-reflection.c"
+#undef create_code
+#undef verify_code
+
 /* test-string-literal.c */
 #define create_code create_code_string_literal
 #define verify_code verify_code_string_literal
@@ -424,6 +431,9 @@ const struct testcase testcases[] = {
   {"reading_struct ",
    create_code_reading_struct ,
    verify_code_reading_struct },
+  {"reflection",
+   create_code_reflection ,
+   verify_code_reflection },
   {"string_literal",
    create_code_string_literal,
    verify_code_string_literal},
diff --git a/gcc/testsuite/jit.dg/test-reflection.c 
b/gcc/testsuite/jit.dg/test-reflection.c
new file mode 100644
index 00000000000..40a5c43076d
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-reflection.c
@@ -0,0 +1,89 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libgccjit.h"
+
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Do nothing.  */
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  /* Get the built-in functions.  */
+  gcc_jit_function *builtin_sin =
+    gcc_jit_context_get_builtin_function (ctxt, "sin");
+
+  CHECK_VALUE (gcc_jit_function_get_param_count(builtin_sin), 1);
+
+  gcc_jit_type *double_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE);
+  CHECK_VALUE (gcc_jit_function_get_return_type(builtin_sin), double_type);
+  CHECK (!gcc_jit_type_is_int(double_type));
+
+  gcc_jit_type *bool_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL);
+  CHECK (gcc_jit_type_is_bool(bool_type));
+  CHECK (!gcc_jit_type_is_int(bool_type));
+
+  gcc_jit_type *aligned_bool_type =
+    gcc_jit_type_get_aligned(gcc_jit_context_get_type (ctxt, 
GCC_JIT_TYPE_BOOL), 8);
+  CHECK (gcc_jit_type_is_bool(aligned_bool_type));
+  CHECK (bool_type != aligned_bool_type);
+  CHECK_VALUE (gcc_jit_type_unqualified(aligned_bool_type), bool_type);
+
+  CHECK_VALUE (gcc_jit_type_unqualified(gcc_jit_type_get_const(bool_type)), 
bool_type);
+  CHECK_VALUE (gcc_jit_type_unqualified(gcc_jit_type_get_volatile(bool_type)), 
bool_type);
+
+  gcc_jit_type *int64 =
+    gcc_jit_context_get_int_type(ctxt, 8, 1);
+  CHECK (gcc_jit_type_is_int(int64));
+  gcc_jit_type *uint64 =
+    gcc_jit_context_get_int_type(ctxt, 8, 0);
+  CHECK (gcc_jit_type_is_int(uint64));
+  gcc_jit_type *int8 =
+    gcc_jit_context_get_int_type(ctxt, 1, 1);
+  CHECK (gcc_jit_type_is_int(int8));
+  gcc_jit_type *uint8 =
+    gcc_jit_context_get_int_type(ctxt, 1, 0);
+  CHECK (gcc_jit_type_is_int(uint8));
+
+  CHECK (!gcc_jit_type_is_vector(double_type));
+  gcc_jit_type *vec_type = gcc_jit_type_get_vector (double_type, 4);
+  gcc_jit_vector_type *vector_type = gcc_jit_type_is_vector(vec_type);
+  CHECK (vector_type);
+  CHECK (vec_type != double_type);
+  CHECK_VALUE (gcc_jit_vector_type_get_element_type(vector_type), double_type);
+  CHECK_VALUE (gcc_jit_vector_type_get_num_units(vector_type), 4);
+
+  CHECK (!gcc_jit_type_is_pointer(double_type));
+  CHECK_VALUE (gcc_jit_type_is_pointer(gcc_jit_type_get_pointer(double_type)), 
double_type);
+
+  gcc_jit_type* params[2] = {int8, uint64};
+  gcc_jit_type *function_ptr_type = 
gcc_jit_context_new_function_ptr_type(ctxt, NULL, int64, 2, params, 0);
+  gcc_jit_function_type *function_type = gcc_jit_type_is_function_ptr_type 
(function_ptr_type);
+  CHECK (function_type);
+  int param_count = gcc_jit_function_type_get_param_count(function_type);
+  CHECK_VALUE (param_count, 2);
+  gcc_jit_type *return_type = 
gcc_jit_function_type_get_return_type(function_type);
+  CHECK_VALUE (return_type, int64);
+  gcc_jit_type *param1 = gcc_jit_function_type_get_param_type(function_type, 
0);
+  CHECK_VALUE (param1, int8);
+  gcc_jit_type *param2 = gcc_jit_function_type_get_param_type(function_type, 
1);
+  CHECK_VALUE (param2, uint64);
+
+  gcc_jit_field *field1 = gcc_jit_context_new_field (ctxt, NULL, uint64, 
"field1");
+  gcc_jit_field *field2 = gcc_jit_context_new_field (ctxt, NULL, double_type, 
"field2");
+  gcc_jit_field *fields[2] = { field1, field2 };
+  gcc_jit_struct *struct_type = gcc_jit_context_new_struct_type (ctxt, NULL, 
"testStruct", 2, fields);
+  CHECK_VALUE (gcc_jit_struct_get_field_count(struct_type), 2);
+  CHECK_VALUE (gcc_jit_struct_get_field(struct_type, 0), field1);
+  CHECK_VALUE (gcc_jit_struct_get_field(struct_type, 1), field2);
+  gcc_jit_struct *struct_ty = 
gcc_jit_type_is_struct(gcc_jit_struct_as_type(struct_type));
+  CHECK_VALUE (struct_ty, struct_type);
+}
+
-- 
2.28.0

Reply via email to