+ g_string_append(str, "const");
+ break;
+ case TYPESPEC_ID_TYPE:
+ if (type_specifier->u.type_specifier.id_type)
+ g_string_append(str, type_specifier->u.type_specifier.id_type);
+ break;
+ case TYPESPEC_STRUCT:
+ {
+ struct ctf_node *node = type_specifier->u.type_specifier.node;
+
+ if (!node->u._struct.name) {
+ fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
+ return -EINVAL;
+ }
+ g_string_append(str, "struct ");
+ g_string_append(str, node->u._struct.name);
+ break;
+ }
+ case TYPESPEC_VARIANT:
+ {
+ struct ctf_node *node = type_specifier->u.type_specifier.node;
+
+ if (!node->u.variant.name) {
+ fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
+ return -EINVAL;
+ }
+ g_string_append(str, "variant ");
+ g_string_append(str, node->u.variant.name);
+ break;
+ }
+ case TYPESPEC_ENUM:
+ {
+ struct ctf_node *node = type_specifier->u.type_specifier.node;
+
+ if (!node->u._enum.enum_id) {
+ fprintf(fd, "[error] %s: unexpected empty enum ID\n", __func__);
+ return -EINVAL;
+ }
+ g_string_append(str, "enum ");
+ g_string_append(str, node->u._enum.enum_id);
+ break;
+ }
+ case TYPESPEC_FLOATING_POINT:
+ case TYPESPEC_INTEGER:
+ case TYPESPEC_STRING:
+ default:
+ fprintf(fd, "[error] %s: unknown specifier\n", __func__);
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static
+int visit_type_specifier_list(FILE *fd, struct ctf_node *type_specifier_list, GString *str)
+{
+ struct ctf_node *iter;
+ int alias_item_nr = 0;
+ int ret;
+
+ cds_list_for_each_entry(iter, &type_specifier_list->u.type_specifier_list.head, siblings) {
+ if (alias_item_nr != 0)
+ g_string_append(str, " ");
+ alias_item_nr++;
+ ret = visit_type_specifier(fd, iter, str);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+static
+GQuark create_typealias_identifier(FILE *fd, int depth,
+ struct ctf_node *type_specifier_list,
+ struct ctf_node *node_type_declarator)
+{
+ struct ctf_node *iter;
+ GString *str;
+ char *str_c;
+ GQuark alias_q;
+ int ret;
+
+ str = g_string_new("");
+ ret = visit_type_specifier_list(fd, type_specifier_list, str);
+ if (ret) {
+ g_string_free(str, TRUE);
+ return 0;
+ }
+ cds_list_for_each_entry(iter, &node_type_declarator->u.type_declarator.pointers, siblings) {
+ g_string_append(str, " *");
+ if (iter->u.pointer.const_qualifier)
+ g_string_append(str, " const");
+ }
+ str_c = g_string_free(str, FALSE);
+ alias_q = g_quark_from_string(str_c);
+ g_free(str_c);
+ return alias_q;
+}
+
+static
+struct declaration *ctf_type_declarator_visit(FILE *fd, int depth,
+ struct ctf_node *type_specifier_list,
+ GQuark *field_name,
+ struct ctf_node *node_type_declarator,
+ struct declaration_scope *declaration_scope,
+ struct declaration *nested_declaration,
+ struct ctf_trace *trace)
+{
+ /*
+ * Visit type declarator by first taking care of sequence/array
+ * (recursively). Then, when we get to the identifier, take care
+ * of pointers.
+ */
+
+ if (node_type_declarator) {
+ assert(node_type_declarator->u.type_declarator.type != TYPEDEC_UNKNOWN);
+
+ /* TODO: gcc bitfields not supported yet. */
+ if (node_type_declarator->u.type_declarator.bitfield_len != NULL) {
+ fprintf(fd, "[error] %s: gcc bitfields are not supported yet.\n", __func__);
+ return NULL;
+ }
+ }
+
+ if (!nested_declaration) {
+ if (node_type_declarator && !cds_list_empty(&node_type_declarator->u.type_declarator.pointers)) {
+ GQuark alias_q;
+
+ /*
+ * If we have a pointer declarator, it _has_ to be present in
+ * the typealiases (else fail).
+ */
+ alias_q = create_typealias_identifier(fd, depth,
+ type_specifier_list, node_type_declarator);
+ nested_declaration = lookup_declaration(alias_q, declaration_scope);
+ if (!nested_declaration) {
+ fprintf(fd, "[error] %s: cannot find typealias \"%s\".\n", __func__, g_quark_to_string(alias_q));
+ return NULL;
+ }
+ if (nested_declaration->id == CTF_TYPE_INTEGER) {
+ struct declaration_integer *integer_declaration =
+ container_of(nested_declaration, struct declaration_integer, p);
+ /* For base to 16 for pointers (expected pretty-print) */
+ if (!integer_declaration->base) {
+ /*
+ * We need to do a copy of the
+ * integer declaration to modify it. There could be other references to
+ * it.
+ */
+ integer_declaration = integer_declaration_new(integer_declaration->len,
+ integer_declaration->byte_order, integer_declaration->signedness,
+ integer_declaration->p.alignment, 16, integer_declaration->encoding,
+ integer_declaration->clock);
+ nested_declaration = &integer_declaration->p;
+ }
+ }
+ } else {
+ nested_declaration = ctf_type_specifier_list_visit(fd, depth,
+ type_specifier_list, declaration_scope, trace);
+ }
+ }
+
+ if (!node_type_declarator)
+ return nested_declaration;
+
+ if (node_type_declarator->u.type_declarator.type == TYPEDEC_ID) {
+ if (node_type_declarator->u.type_declarator.u.id)
+ *field_name = g_quark_from_string(node_type_declarator->u.type_declarator.u.id);
+ else
+ *field_name = 0;
+ return nested_declaration;
+ } else {
+ struct declaration *declaration;
+ struct ctf_node *first;
+
+ /* TYPEDEC_NESTED */
+
+ if (!nested_declaration) {
+ fprintf(fd, "[error] %s: nested type is unknown.\n", __func__);
+ return NULL;
+ }
+
+ /* create array/sequence, pass nested_declaration as child. */
+ if (cds_list_empty(&node_type_declarator->u.type_declarator.u.nested.length)) {
+ fprintf(fd, "[error] %s: expecting length field reference or value.\n", __func__);
+ return NULL;
+ }
+ first = _cds_list_first_entry(&node_type_declarator->u.type_declarator.u.nested.length,
+ struct ctf_node, siblings);
+ assert(first->type == NODE_UNARY_EXPRESSION);
+
+ switch (first->u.unary_expression.type) {
+ case UNARY_UNSIGNED_CONSTANT:
+ {
+ struct declaration_array *array_declaration;
+ size_t len;
+
+ len = first->u.unary_expression.u.unsigned_constant;
+ array_declaration = array_declaration_new(len, nested_declaration,
+ declaration_scope);
+
+ if (!array_declaration) {
+ fprintf(fd, "[error] %s: cannot create array declaration.\n", __func__);
+ return NULL;
+ }
+ declaration = &array_declaration->p;
+ break;
+ }
+ case UNARY_STRING:
+ {
+ /* Lookup unsigned integer definition, create sequence */
+ char *length_name = concatenate_unary_strings(&node_type_declarator->u.type_declarator.u.nested.length);
+ struct declaration_sequence *sequence_declaration;
+
+ sequence_declaration = sequence_declaration_new(length_name, nested_declaration, declaration_scope);
+ if (!sequence_declaration) {
+ fprintf(fd, "[error] %s: cannot create sequence declaration.\n", __func__);
+ return NULL;
+ }
+ declaration = &sequence_declaration->p;
+ break;
+ }
+ default:
+ assert(0);
+ }
+
+ /* Pass it as content of outer container */
+ declaration = ctf_type_declarator_visit(fd, depth,
+ type_specifier_list, field_name,
+ node_type_declarator->u.type_declarator.u.nested.type_declarator,
+ declaration_scope, declaration, trace);
+ return declaration;
+ }
+}
+
+static
+int ctf_struct_type_declarators_visit(FILE *fd, int depth,
+ struct declaration_struct *struct_declaration,
+ struct ctf_node *type_specifier_list,
+ struct cds_list_head *type_declarators,
+ struct declaration_scope *declaration_scope,
+ struct ctf_trace *trace)
+{
+ struct ctf_node *iter;
+ GQuark field_name;
+
+ cds_list_for_each_entry(iter, type_declarators, siblings) {
+ struct declaration *field_declaration;
+
+ field_declaration = ctf_type_declarator_visit(fd, depth,
+ type_specifier_list,
+ &field_name, iter,
+ struct_declaration->scope,
+ NULL, trace);
+ if (!field_declaration) {
+ fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__);
+ return -EINVAL;
+ }
+
+ /* Check if field with same name already exists */
+ if (struct_declaration_lookup_field_index(struct_declaration, field_name) >= 0) {
+ fprintf(fd, "[error] %s: duplicate field %s in struct\n", __func__, g_quark_to_string(field_name));
+ return -EINVAL;
+ }
+
+ struct_declaration_add_field(struct_declaration,
+ g_quark_to_string(field_name),
+ field_declaration);
+ }
+ return 0;
+}
+
+static
+int ctf_variant_type_declarators_visit(FILE *fd, int depth,
+ struct declaration_untagged_variant *untagged_variant_declaration,
+ struct ctf_node *type_specifier_list,
+ struct cds_list_head *type_declarators,
+ struct declaration_scope *declaration_scope,
+ struct ctf_trace *trace)
+{
+ struct ctf_node *iter;
+ GQuark field_name;
+
+ cds_list_for_each_entry(iter, type_declarators, siblings) {
+ struct declaration *field_declaration;
+
+ field_declaration = ctf_type_declarator_visit(fd, depth,
+ type_specifier_list,
+ &field_name, iter,
+ untagged_variant_declaration->scope,
+ NULL, trace);
+ if (!field_declaration) {
+ fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__);
+ return -EINVAL;
+ }
+
+ if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration, field_name) != NULL) {
+ fprintf(fd, "[error] %s: duplicate field %s in variant\n", __func__, g_quark_to_string(field_name));
+ return -EINVAL;
+ }
+
+
+ untagged_variant_declaration_add_field(untagged_variant_declaration,
+ g_quark_to_string(field_name),
+ field_declaration);
+ }
+ return 0;
+}
+
+static
+int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope,
+ struct ctf_node *type_specifier_list,
+ struct cds_list_head *type_declarators,
+ struct ctf_trace *trace)
+{
+ struct ctf_node *iter;
+ GQuark identifier;
+
+ cds_list_for_each_entry(iter, type_declarators, siblings) {
+ struct declaration *type_declaration;
+ int ret;
+
+ type_declaration = ctf_type_declarator_visit(fd, depth,
+ type_specifier_list,
+ &identifier, iter,
+ scope, NULL, trace);
+ if (!type_declaration) {
+ fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
+ return -EINVAL;
+ }
+ /*
+ * Don't allow typedef and typealias of untagged
+ * variants.
+ */
+ if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
+ fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
+ declaration_unref(type_declaration);
+ return -EPERM;
+ }
+ ret = register_declaration(identifier, type_declaration, scope);
+ if (ret) {
+ type_declaration->declaration_free(type_declaration);
+ return ret;
+ }
+ }
+ return 0;
+}
+
+static
+int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope,
+ struct ctf_node *target, struct ctf_node *alias,
+ struct ctf_trace *trace)
+{
+ struct declaration *type_declaration;
+ struct ctf_node *node;
+ GQuark dummy_id;
+ GQuark alias_q;
+ int err;
+
+ /* See ctf_visitor_type_declarator() in the semantic validator. */
+
+ /*
+ * Create target type declaration.
+ */
+
+ if (cds_list_empty(&target->u.typealias_target.type_declarators))
+ node = NULL;
+ else
+ node = _cds_list_first_entry(&target->u.typealias_target.type_declarators,
+ struct ctf_node, siblings);
+ type_declaration = ctf_type_declarator_visit(fd, depth,
+ target->u.typealias_target.type_specifier_list,
+ &dummy_id, node,
+ scope, NULL, trace);
+ if (!type_declaration) {
+ fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
+ err = -EINVAL;
+ goto error;
+ }
+ /*
+ * Don't allow typedef and typealias of untagged
+ * variants.
+ */
+ if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
+ fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
+ declaration_unref(type_declaration);
+ return -EPERM;
+ }
+ /*
+ * The semantic validator does not check whether the target is
+ * abstract or not (if it has an identifier). Check it here.
+ */
+ if (dummy_id != 0) {
+ fprintf(fd, "[error] %s: expecting empty identifier\n", __func__);
+ err = -EINVAL;
+ goto error;
+ }
+ /*
+ * Create alias identifier.
+ */
+
+ node = _cds_list_first_entry(&alias->u.typealias_alias.type_declarators,
+ struct ctf_node, siblings);
+ alias_q = create_typealias_identifier(fd, depth,
+ alias->u.typealias_alias.type_specifier_list, node);
+ err = register_declaration(alias_q, type_declaration, scope);
+ if (err)
+ goto error;
+ return 0;
+
+error:
+ if (type_declaration) {
+ type_declaration->declaration_free(type_declaration);
+ }
+ return err;
+}
+
+static
+int ctf_struct_declaration_list_visit(FILE *fd, int depth,
+ struct ctf_node *iter, struct declaration_struct *struct_declaration,
+ struct ctf_trace *trace)
+{
+ int ret;
+
+ switch (iter->type) {
+ case NODE_TYPEDEF:
+ /* For each declarator, declare type and add type to struct declaration scope */
+ ret = ctf_typedef_visit(fd, depth,
+ struct_declaration->scope,
+ iter->u._typedef.type_specifier_list,
+ &iter->u._typedef.type_declarators, trace);
+ if (ret)
+ return ret;
+ break;
+ case NODE_TYPEALIAS:
+ /* Declare type with declarator and add type to struct declaration scope */
+ ret = ctf_typealias_visit(fd, depth,
+ struct_declaration->scope,
+ iter->u.typealias.target,
+ iter->u.typealias.alias, trace);
+ if (ret)
+ return ret;
+ break;
+ case NODE_STRUCT_OR_VARIANT_DECLARATION:
+ /* Add field to structure declaration */
+ ret = ctf_struct_type_declarators_visit(fd, depth,
+ struct_declaration,
+ iter->u.struct_or_variant_declaration.type_specifier_list,
+ &iter->u.struct_or_variant_declaration.type_declarators,
+ struct_declaration->scope, trace);
+ if (ret)
+ return ret;
+ break;
+ default:
+ fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
+ assert(0);
+ }
+ return 0;
+}
+
+static
+int ctf_variant_declaration_list_visit(FILE *fd, int depth,
+ struct ctf_node *iter,
+ struct declaration_untagged_variant *untagged_variant_declaration,
+ struct ctf_trace *trace)
+{
+ int ret;
+
+ switch (iter->type) {
+ case NODE_TYPEDEF:
+ /* For each declarator, declare type and add type to variant declaration scope */
+ ret = ctf_typedef_visit(fd, depth,
+ untagged_variant_declaration->scope,
+ iter->u._typedef.type_specifier_list,
+ &iter->u._typedef.type_declarators, trace);
+ if (ret)
+ return ret;
+ break;
+ case NODE_TYPEALIAS:
+ /* Declare type with declarator and add type to variant declaration scope */
+ ret = ctf_typealias_visit(fd, depth,
+ untagged_variant_declaration->scope,
+ iter->u.typealias.target,
+ iter->u.typealias.alias, trace);
+ if (ret)
+ return ret;