]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
libctf, include: API review: ctf_add_qualifier
authorNick Alcock <nick.alcock@oracle.com>
Tue, 18 Nov 2025 10:25:23 +0000 (10:25 +0000)
committerNick Alcock <nick.alcock@oracle.com>
Tue, 9 Dec 2025 13:02:33 +0000 (13:02 +0000)
We don't need three separate functions just to add cvr-quals. So replace
ctf_add_const, ctf_add_restrict and ctf_add_volatile with one new
function, ctf_add_qualifier, and use it in the deduplicator rather than
its current use of the libctf-internal ctf_add_reftype.  This also lets
us move various pointer-only stuff like ptrtab updating into
ctf_add_pointer, where it belongs.

(We don't export ctf_add_reftype because pointers are too conceptually
different from qualifiers to add with the same function.)

include/ctf-api.h
libctf/ctf-create.c
libctf/ctf-dedup.c
libctf/ctf-impl.h
libctf/libctf.ver

index 42869211e4ab40df0ebbf9f1a7dac1e2a53e2d4f..c2f8dcae4ac2e0d856165abee36a47d190de98be 100644 (file)
@@ -298,6 +298,7 @@ typedef int ctf_func_type_flags_t;
   _CTF_ITEM (ECTF_NOTINTFP, "Type is not an integer, float, or enum.") \
   _CTF_ITEM (ECTF_NOTARRAY, "Type is not an array.") \
   _CTF_ITEM (ECTF_NOTREF, "Type does not reference another type.") \
+  _CTF_ITEM (ECTF_NOTQUAL, "Type is not a cvr-qual.") \
   _CTF_ITEM (ECTF_NAMELEN, "Buffer is too small to hold type name.") \
   _CTF_ITEM (ECTF_NOTYPE, "No type found corresponding to name.") \
   _CTF_ITEM (ECTF_SYNTAX, "Syntax error in type name.") \
@@ -1001,7 +1002,6 @@ extern ctf_dict_t *ctf_create (ctf_error_t *);
 
 extern ctf_id_t ctf_add_array (ctf_dict_t *, uint32_t,
                               const ctf_arinfo_t *);
-extern ctf_id_t ctf_add_const (ctf_dict_t *, uint32_t, ctf_id_t);
 
 /* enums are created signed by default: an encoding of NULL is permitted and
    means "use the default".  If you want an unsigned enum, use
@@ -1050,7 +1050,6 @@ extern ctf_id_t ctf_add_pointer (ctf_dict_t *, uint32_t, ctf_id_t);
 extern ctf_id_t ctf_add_type (ctf_dict_t *, ctf_dict_t *, ctf_id_t);
 extern ctf_id_t ctf_add_typedef (ctf_dict_t *, uint32_t, const char *,
                                 ctf_id_t);
-extern ctf_id_t ctf_add_restrict (ctf_dict_t *, uint32_t, ctf_id_t);
 
 /* Add type and decl tags to whole types or (for decl tags) specific
    components of types (parameter count for functions, member count for structs
@@ -1099,7 +1098,6 @@ extern ctf_id_t ctf_add_struct (ctf_dict_t *, uint32_t flag, const char *name,
    variables, etc, to point to.  */
 
 extern ctf_id_t ctf_add_unknown (ctf_dict_t *, uint32_t, const char *);
-extern ctf_id_t ctf_add_volatile (ctf_dict_t *, uint32_t, ctf_id_t);
 
 /* Add an enumerator to an enum or enum64.  If the enum is non-root, so are all
    the constants added to it by ctf_add_enumerator.  */
@@ -1119,6 +1117,8 @@ extern ctf_ret_t ctf_add_member_bitfield (ctf_dict_t *, ctf_id_t souid,
                                          const char *, ctf_id_t type,
                                          size_t bit_offset, int bit_width);
 
+extern ctf_id_t ctf_add_qualifier (ctf_dict_t *, uint32_t, ctf_kind_t cvr_qual, ctf_id_t);
+
 /* ctf_add_variable adds variables to no datasec at all;
    ctf_add_section_variable adds them to the given datasec, or to no datasec at
    all if the datasec is NULL.  */
index 675dd3abc9452eeae648cc46162dbaf52112323a..0f5d735d4af374815d9501ca99c14345ad985b9d 100644 (file)
@@ -709,45 +709,27 @@ ctf_add_encoded (ctf_dict_t *fp, uint32_t flag,
   return dtd->dtd_type;
 }
 
-ctf_id_t
-ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, ctf_kind_t kind)
+static ctf_id_t
+ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, ctf_kind_t kind,
+                ctf_dict_t **reffp)
 {
+  ctf_dict_t *tmp = fp;
   ctf_dtdef_t *dtd;
-  ctf_dict_t *typedict = fp;
-  ctf_dict_t *refdict = fp;
-  int child = fp->ctf_flags & LCTF_CHILD;
-  uint32_t type_idx;
-  uint32_t ref_idx;
+
+  if (ref != 0 && ctf_lookup_by_id (&tmp, ref, NULL) == NULL)
+    return CTF_ERR;            /* errno is set for us.  */
 
   if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
     return (ctf_set_typed_errno (fp, EINVAL));
 
-  if (ref != 0 && ctf_lookup_by_id (&refdict, ref, NULL) == NULL)
-    return CTF_ERR;            /* errno is set for us.  */
-
   if ((dtd = ctf_add_generic (fp, flag, NULL, kind, 0, 0, 0, NULL)) == NULL)
     return CTF_ERR;            /* errno is set for us.  */
 
   dtd->dtd_data->ctt_info = CTF_TYPE_INFO (kind, 0, 0);
   dtd->dtd_data->ctt_type = (uint32_t) ref;
 
-  if (kind != CTF_K_POINTER)
-    return dtd->dtd_type;
-
-  /* If we are adding a pointer, update the ptrtab, pointing at this type from
-     the type it points to.  Note that ctf_typemax is at this point one higher
-     than we want to check against, because it's just been incremented for the
-     addition of this type.  The pptrtab is lazily-updated as needed, so is not
-     touched here.  */
-
-  typedict = ctf_get_dict (fp, dtd->dtd_type);
-
-  type_idx = ctf_type_to_index (typedict, dtd->dtd_type);
-  ref_idx = ctf_type_to_index (refdict, ref);
-
-  if (ctf_type_ischild (fp, ref) == child
-      && ref_idx < fp->ctf_typemax)
-    fp->ctf_ptrtab[ref_idx] = type_idx;
+  if (reffp)
+    *reffp = tmp;
 
   return dtd->dtd_type;
 }
@@ -827,7 +809,34 @@ ctf_add_btf_float (ctf_dict_t *fp, uint32_t flag,
 ctf_id_t
 ctf_add_pointer (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
 {
-  return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER));
+  int child = fp->ctf_flags & LCTF_CHILD;
+
+  ctf_dict_t *typedict = fp;
+  ctf_dict_t *refdict;
+  ctf_id_t type;
+  uint32_t type_idx;
+  uint32_t ref_idx;
+
+  if ((type = ctf_add_reftype (fp, flag, ref,
+                              CTF_K_POINTER, &refdict)) == CTF_ERR)
+    return CTF_ERR;                    /* errno is set for us.  */
+
+  /* Update the ptrtab, pointing at this type from the type it points to.
+     Note that ctf_typemax is at this point one higher than we want to check
+     against, because it's just been incremented for the addition of this
+     type.  The pptrtab is lazily-updated as needed, so is not touched
+     here.  */
+
+  typedict = ctf_get_dict (fp, type);
+
+  type_idx = ctf_type_to_index (typedict, type);
+  ref_idx = ctf_type_to_index (refdict, ref);
+
+  if (ctf_type_ischild (fp, ref) == child
+      && ref_idx < fp->ctf_typemax)
+    fp->ctf_ptrtab[ref_idx] = type_idx;
+
+  return type;
 }
 
 ctf_id_t
@@ -1404,21 +1413,17 @@ ctf_add_typedef (ctf_dict_t *fp, uint32_t flag, const char *name,
 }
 
 ctf_id_t
-ctf_add_volatile (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
-{
-  return (ctf_add_reftype (fp, flag, ref, CTF_K_VOLATILE));
-}
-
-ctf_id_t
-ctf_add_const (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
-{
-  return (ctf_add_reftype (fp, flag, ref, CTF_K_CONST));
-}
-
-ctf_id_t
-ctf_add_restrict (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
+ctf_add_qualifier (ctf_dict_t *fp, uint32_t flag, ctf_kind_t cvr_qual,
+                  ctf_id_t ref)
 {
-  return (ctf_add_reftype (fp, flag, ref, CTF_K_RESTRICT));
+  if (cvr_qual != CTF_K_CONST && cvr_qual != CTF_K_VOLATILE && cvr_qual != CTF_K_RESTRICT)
+    {
+      ctf_err_warn (fp, 0, ECTF_NOTENUM, _("ctf_add_qualifier: kind %i is "
+                                          "not CTF_K_CONST, CTF_K_VOLATILE "
+                                          "or CTF_K_RESTRICT.\n"), cvr_qual);
+      return ctf_set_typed_errno (fp, ECTF_NOTQUAL);
+    }
+  return (ctf_add_reftype (fp, flag, ref, cvr_qual, NULL));
 }
 
 ctf_ret_t
@@ -2527,7 +2532,10 @@ ctf_add_type_internal (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type
       if (src_type == CTF_ERR)
        return CTF_ERR;                         /* errno is set for us.  */
 
-      dst_type = ctf_add_reftype (dst_fp, flag, src_type, kind);
+      if (kind == CTF_K_POINTER)
+       dst_type = ctf_add_pointer (dst_fp, flag, src_type);
+      else
+       dst_type = ctf_add_qualifier (dst_fp, flag, kind, src_type);
       break;
 
     case CTF_K_ARRAY:
index d0c374d767c6f8c2f6d3d7f11d1044faedc3c8a2..4a4172b7b3a06c76b35aa4c13b499e7bdb6cd178 100644 (file)
@@ -3599,9 +3599,19 @@ ctf_dedup_emit_type (const char *hval, ctf_dict_t *output, ctf_dict_t **inputs,
                                         parents, input, input_num,
                                         ref)) == CTF_ERR)
        goto err_input;                         /* errno is set for us.  */
-
-      if ((new_type = ctf_add_reftype (target, isroot, ref, kind)) == CTF_ERR)
-       goto err_target;                        /* errno is set for us.  */
+      
+      if (kind == CTF_K_POINTER)
+       {
+         errtype = _("pointer");
+         if ((new_type = ctf_add_pointer (target, isroot, ref)) == CTF_ERR)
+           goto err_target;                    /* errno is set for us.  */
+       }
+      else
+       {
+         errtype = _("cvr-qual");
+         if ((new_type = ctf_add_qualifier (target, isroot, kind, ref)) == CTF_ERR)
+           goto err_target;                    /* errno is set for us.  */
+       }
       break;
 
     case CTF_K_TYPE_TAG:
index 99971f6a3273fed6fb5bf977a0a95d37c05fe9c1..763a7c9231fdace77ce1cd61664c7fb30c1a00ba 100644 (file)
@@ -785,8 +785,6 @@ extern ctf_dtdef_t *ctf_dynamic_type (const ctf_dict_t *, ctf_id_t);
 
 extern ctf_id_t ctf_add_encoded (ctf_dict_t *, uint32_t, const char *,
                                 const ctf_encoding_t *, ctf_kind_t kind);
-extern ctf_id_t ctf_add_reftype (ctf_dict_t *, uint32_t, ctf_id_t,
-                                ctf_kind_t kind);
 extern ctf_ret_t ctf_add_funcobjt_sym_forced (ctf_dict_t *, int is_function,
                                              const char *, ctf_id_t);
 
index dda59ecb11595d3cddd6e2c246689c79e315358d..19cc16ac01211baa451df212759abf8bb0f82def 100644 (file)
@@ -97,7 +97,6 @@ LIBCTF_2.0 {
        ctf_next_copy;
 
        ctf_add_array;
-       ctf_add_const;
        ctf_add_decl_tag;
        ctf_add_decl_type_tag;
        ctf_add_enum;
@@ -108,16 +107,15 @@ LIBCTF_2.0 {
        ctf_add_function_linkage;
        ctf_add_integer;
        ctf_add_pointer;
+       ctf_add_qualifier;
        ctf_add_type;
        ctf_add_typedef;
        ctf_add_type_tag;
-       ctf_add_restrict;
        ctf_add_section_variable;
        ctf_add_slice;
        ctf_add_struct;
        ctf_add_unknown;
        ctf_add_variable;
-       ctf_add_volatile;
 
        ctf_add_enumerator;
        ctf_add_member;