+ done = 0;
+
+ /* First strip off any qualifiers, if we have a function or method. */
+ while (!done)
+ switch (ret_comp->type)
+ {
+ case DEMANGLE_COMPONENT_CONST:
+ case DEMANGLE_COMPONENT_RESTRICT:
+ case DEMANGLE_COMPONENT_VOLATILE:
+ case DEMANGLE_COMPONENT_CONST_THIS:
+ case DEMANGLE_COMPONENT_RESTRICT_THIS:
+ case DEMANGLE_COMPONENT_VOLATILE_THIS:
+ case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
+ ret_comp = d_left (ret_comp);
+ break;
+ default:
+ done = 1;
+ break;
+ }
+
+ /* If what we have now is a function, discard the argument list. */
+ if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
+ ret_comp = d_left (ret_comp);
+
+ /* If what we have now is a template, strip off the template
+ arguments. The left subtree may be a qualified name. */
+ if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
+ ret_comp = d_left (ret_comp);
+
+ /* What we have now should be a name, possibly qualified. Additional
+ qualifiers could live in the left subtree or the right subtree. Find
+ the last piece. */
+ done = 0;
+ prev_comp = NULL;
+ cur_comp = ret_comp;
+ while (!done)
+ switch (cur_comp->type)
+ {
+ case DEMANGLE_COMPONENT_QUAL_NAME:
+ case DEMANGLE_COMPONENT_LOCAL_NAME:
+ prev_comp = cur_comp;
+ cur_comp = d_right (cur_comp);
+ break;
+ case DEMANGLE_COMPONENT_TEMPLATE:
+ case DEMANGLE_COMPONENT_NAME:
+ case DEMANGLE_COMPONENT_CTOR:
+ case DEMANGLE_COMPONENT_DTOR:
+ case DEMANGLE_COMPONENT_OPERATOR:
+ case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
+ done = 1;
+ break;
+ default:
+ done = 1;
+ cur_comp = NULL;
+ break;
+ }
+
+ ret = NULL;
+ if (cur_comp != NULL && prev_comp != NULL)
+ {
+ /* We want to discard the rightmost child of PREV_COMP. */
+ *prev_comp = *d_left (prev_comp);
+ /* The ten is completely arbitrary; we don't have a good estimate. */
+ ret = cp_comp_to_string (ret_comp, 10);
+ }
+
+ xfree (storage);
+ if (demangled_name)
+ xfree (demangled_name);
+ return ret;
+}
+
+/* Return the child of COMP which is the basename of a method, variable,
+ et cetera. All scope qualifiers are discarded, but template arguments
+ will be included. The component tree may be modified. */
+
+static struct demangle_component *
+unqualified_name_from_comp (struct demangle_component *comp)
+{
+ struct demangle_component *ret_comp = comp, *last_template;
+ int done;
+
+ done = 0;
+ last_template = NULL;
+ while (!done)
+ switch (ret_comp->type)
+ {
+ case DEMANGLE_COMPONENT_QUAL_NAME:
+ case DEMANGLE_COMPONENT_LOCAL_NAME:
+ ret_comp = d_right (ret_comp);
+ break;
+ case DEMANGLE_COMPONENT_TYPED_NAME:
+ ret_comp = d_left (ret_comp);
+ break;
+ case DEMANGLE_COMPONENT_TEMPLATE:
+ gdb_assert (last_template == NULL);
+ last_template = ret_comp;
+ ret_comp = d_left (ret_comp);
+ break;
+ case DEMANGLE_COMPONENT_CONST:
+ case DEMANGLE_COMPONENT_RESTRICT:
+ case DEMANGLE_COMPONENT_VOLATILE:
+ case DEMANGLE_COMPONENT_CONST_THIS:
+ case DEMANGLE_COMPONENT_RESTRICT_THIS:
+ case DEMANGLE_COMPONENT_VOLATILE_THIS:
+ case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
+ ret_comp = d_left (ret_comp);
+ break;
+ case DEMANGLE_COMPONENT_NAME:
+ case DEMANGLE_COMPONENT_CTOR:
+ case DEMANGLE_COMPONENT_DTOR:
+ case DEMANGLE_COMPONENT_OPERATOR:
+ case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
+ done = 1;
+ break;
+ default:
+ return NULL;
+ break;
+ }
+
+ if (last_template)
+ {
+ d_left (last_template) = ret_comp;
+ return last_template;
+ }
+
+ return ret_comp;
+}