+static struct declaration_integer *get_declaration_integer(const struct declaration *decl)
+{
+ struct declaration_field *field_decl;
+ struct declaration_integer *ret = NULL;
+
+ if (decl && bt_ctf_field_type(decl) == CTF_TYPE_INTEGER) {
+ field_decl = (struct declaration_field *) decl;
+ ret = ((struct declaration_integer *) field_decl->declaration);
+ }
+
+ return ret;
+}
+
+static struct declaration_string *get_declaration_string(const struct declaration *decl)
+{
+ struct declaration_field *field_decl;
+ struct declaration_string *ret = NULL;
+
+ if (decl && bt_ctf_field_type(decl) == CTF_TYPE_STRING) {
+ field_decl = (struct declaration_field *) decl;
+ ret = ((struct declaration_string *) field_decl->declaration);
+ }
+
+ return ret;
+}
+
+static struct declaration_array *get_declaration_array(const struct declaration *decl)
+{
+ struct declaration_field *field_decl;
+ struct declaration_array *ret = NULL;
+
+ if (decl && bt_ctf_field_type(decl) == CTF_TYPE_ARRAY) {
+ field_decl = (struct declaration_field *) decl;
+ ret = ((struct declaration_array *) field_decl->declaration);
+ }
+
+ return ret;
+}
+
+int bt_ctf_get_int_signedness(const struct declaration *decl)
+{
+ int ret;
+ struct declaration_integer *integer;
+
+ integer = get_declaration_integer(decl);
+ if (integer) {
+ ret = integer->signedness;
+ } else {
+ ret = -EINVAL;
+ bt_ctf_field_set_error(-EINVAL);
+ }
+
+ return ret;
+}
+
+int bt_ctf_get_int_base(const struct declaration *decl)
+{
+ int ret;
+ struct declaration_integer *integer;
+
+ integer = get_declaration_integer(decl);
+ if (integer) {
+ ret = integer->base;
+ } else {
+ ret = -EINVAL;
+ bt_ctf_field_set_error(-EINVAL);
+ }
+
+ return ret;
+}
+
+int bt_ctf_get_int_byte_order(const struct declaration *decl)
+{
+ int ret;
+ struct declaration_integer *integer;
+
+ integer = get_declaration_integer(decl);
+ if (integer) {
+ ret = integer->byte_order;
+ } else {
+ ret = -EINVAL;
+ bt_ctf_field_set_error(-EINVAL);
+ }
+
+ return ret;
+}
+
+ssize_t bt_ctf_get_int_len(const struct declaration *decl)
+{
+ ssize_t ret;
+ struct declaration_integer *integer;
+
+ integer = get_declaration_integer(decl);
+ if (integer) {
+ ret = (ssize_t) integer->len;
+ } else {
+ ret = -EINVAL;
+ bt_ctf_field_set_error(-EINVAL);
+ }
+
+ return ret;
+}
+
+const struct definition *bt_ctf_get_enum_int(const struct definition *field)
+{
+ struct definition_enum *def_enum;
+
+ if (!field || bt_ctf_field_type(bt_ctf_get_field_decl(field)) != CTF_TYPE_ENUM) {
+ bt_ctf_field_set_error(-EINVAL);
+ return NULL;
+ }
+ def_enum = container_of(field, struct definition_enum, p);
+ return &def_enum->integer->p;
+}
+
+const char *bt_ctf_get_enum_str(const struct definition *field)
+{
+ struct definition_enum *def_enum;
+ struct declaration_enum *decl_enum;
+ GArray *array;
+ const char *ret;
+
+ if (!field || bt_ctf_field_type(bt_ctf_get_field_decl(field)) != CTF_TYPE_ENUM) {
+ bt_ctf_field_set_error(-EINVAL);
+ return NULL;
+ }
+ def_enum = container_of(field, struct definition_enum, p);
+ decl_enum = def_enum->declaration;
+ if (get_int_signedness(&def_enum->integer->p)) {
+ array = enum_int_to_quark_set(decl_enum,
+ get_signed_int(&def_enum->integer->p));
+ } else {
+ array = enum_uint_to_quark_set(decl_enum,
+ get_unsigned_int(&def_enum->integer->p));
+ }
+ if (!array) {
+ bt_ctf_field_set_error(-ENOENT);
+ return NULL;
+ }
+
+ if (array->len == 0) {
+ g_array_unref(array);
+ bt_ctf_field_set_error(-ENOENT);
+ return NULL;
+ }
+ /* Return first string. Arbitrary choice. */
+ ret = g_quark_to_string(g_array_index(array, GQuark, 0));
+ g_array_unref(array);
+ return ret;
+}
+
+enum ctf_string_encoding bt_ctf_get_encoding(const struct declaration *decl)
+{
+ enum ctf_string_encoding ret = 0;
+ struct declaration_integer *integer;
+ struct declaration_string *string;
+
+ if (!decl)
+ goto error;
+
+ if (bt_ctf_field_type(decl) == CTF_TYPE_INTEGER) {
+ integer = get_declaration_integer(decl);
+ if (integer) {
+ ret = integer->encoding;
+ } else {
+ goto error;
+ }
+ } else if (bt_ctf_field_type(decl) == CTF_TYPE_STRING) {
+ string = get_declaration_string(decl);
+ if (string) {
+ ret = string->encoding;
+ } else {
+ goto error;
+ }
+ } else {
+ goto error;
+ }
+ return ret;
+
+error:
+ bt_ctf_field_set_error(-EINVAL);
+ return -1;
+}
+
+int bt_ctf_get_array_len(const struct declaration *decl)
+{
+ int ret;
+ struct declaration_array *array;
+
+ if (decl && bt_ctf_field_type(decl) == CTF_TYPE_ARRAY) {
+ array = get_declaration_array(decl);
+ if (array) {
+ ret = array->len;
+ } else {
+ goto error;
+ }
+ } else {
+ goto error;
+ }
+
+ return ret;
+
+error:
+ bt_ctf_field_set_error(-EINVAL);
+ return -1;
+}
+