+/* Operator validation.
+ NOTE: Multi-byte operators (usually the assignment variety
+ operator) must appear before the single byte version, i.e., "+="
+ before "+". */
+static const char *operator_tokens[] =
+ {
+ "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*",
+ "/=", "/", "%=", "%", "!=", "==", "!", "&&", "<<=", "<<",
+ ">>=", ">>", "<=", "<", ">=", ">", "~", "&=", "&", "|=",
+ "||", "|", "^=", "^", "=", "()", "[]", ",", "new", "delete"
+ /* new[] and delete[] require special whitespace handling */
+ };
+
+/* A list of typedefs which should not be substituted by replace_typedefs. */
+static const char * const ignore_typedefs[] =
+ {
+ "std::istream", "std::iostream", "std::ostream", "std::string"
+ };
+
+static void
+ replace_typedefs (struct demangle_parse_info *info,
+ struct demangle_component *ret_comp);
+
+/* A convenience function to copy STRING into OBSTACK, returning a pointer
+ to the newly allocated string and saving the number of bytes saved in LEN.
+
+ It does not copy the terminating '\0' byte! */
+
+static char *
+copy_string_to_obstack (struct obstack *obstack, const char *string,
+ long *len)
+{
+ *len = strlen (string);
+ return obstack_copy (obstack, string, *len);
+}
+
+/* A cleanup wrapper for cp_demangled_name_parse_free. */
+
+static void
+do_demangled_name_parse_free_cleanup (void *data)
+{
+ struct demangle_parse_info *info = (struct demangle_parse_info *) data;
+
+ cp_demangled_name_parse_free (info);
+}
+
+/* Create a cleanup for C++ name parsing. */
+
+struct cleanup *
+make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
+{
+ return make_cleanup (do_demangled_name_parse_free_cleanup, info);
+}
+
+/* Return 1 if STRING is clearly already in canonical form. This
+ function is conservative; things which it does not recognize are
+ assumed to be non-canonical, and the parser will sort them out
+ afterwards. This speeds up the critical path for alphanumeric
+ identifiers. */
+
+static int
+cp_already_canonical (const char *string)
+{
+ /* Identifier start character [a-zA-Z_]. */
+ if (!ISIDST (string[0]))
+ return 0;
+
+ /* These are the only two identifiers which canonicalize to other
+ than themselves or an error: unsigned -> unsigned int and
+ signed -> int. */
+ if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
+ return 0;
+ else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
+ return 0;
+
+ /* Identifier character [a-zA-Z0-9_]. */
+ while (ISIDNUM (string[1]))
+ string++;
+
+ if (string[1] == '\0')
+ return 1;
+ else
+ return 0;
+}
+
+/* Inspect the given RET_COMP for its type. If it is a typedef,
+ replace the node with the typedef's tree.
+
+ Returns 1 if any typedef substitutions were made, 0 otherwise. */
+
+static int
+inspect_type (struct demangle_parse_info *info,
+ struct demangle_component *ret_comp)
+{
+ int i;
+ char *name;
+ struct symbol *sym;
+ volatile struct gdb_exception except;
+
+ /* Copy the symbol's name from RET_COMP and look it up
+ in the symbol table. */
+ name = (char *) alloca (ret_comp->u.s_name.len + 1);
+ memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
+ name[ret_comp->u.s_name.len] = '\0';
+
+ /* Ignore any typedefs that should not be substituted. */
+ for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
+ {
+ if (strcmp (name, ignore_typedefs[i]) == 0)
+ return 0;
+ }
+
+ sym = NULL;
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
+ }
+
+ if (except.reason >= 0 && sym != NULL)
+ {
+ struct type *otype = SYMBOL_TYPE (sym);
+
+ /* If the type is a typedef, replace it. */
+ if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF)
+ {
+ long len;
+ int is_anon;
+ struct type *type;
+ struct demangle_parse_info *i;
+ struct ui_file *buf;
+
+ /* Get the real type of the typedef. */
+ type = check_typedef (otype);
+
+ is_anon = (TYPE_TAG_NAME (type) == NULL
+ && (TYPE_CODE (type) == TYPE_CODE_ENUM
+ || TYPE_CODE (type) == TYPE_CODE_STRUCT
+ || TYPE_CODE (type) == TYPE_CODE_UNION));
+ if (is_anon)
+ {
+ struct type *last = otype;
+
+ /* Find the last typedef for the type. */
+ while (TYPE_TARGET_TYPE (last) != NULL
+ && (TYPE_CODE (TYPE_TARGET_TYPE (last))
+ == TYPE_CODE_TYPEDEF))
+ last = TYPE_TARGET_TYPE (last);
+
+ /* If there is only one typedef for this anonymous type,
+ do not substitute it. */
+ if (type == otype)
+ return 0;
+ else
+ /* Use the last typedef seen as the type for this
+ anonymous type. */
+ type = last;
+ }
+
+ buf = mem_fileopen ();
+ TRY_CATCH (except, RETURN_MASK_ERROR)
+ {
+ type_print (type, "", buf, -1);
+ }
+
+ /* If type_print threw an exception, there is little point
+ in continuing, so just bow out gracefully. */
+ if (except.reason < 0)
+ {
+ ui_file_delete (buf);
+ return 0;
+ }
+
+ name = ui_file_obsavestring (buf, &info->obstack, &len);
+ ui_file_delete (buf);
+
+ /* Turn the result into a new tree. Note that this
+ tree will contain pointers into NAME, so NAME cannot
+ be free'd until all typedef conversion is done and
+ the final result is converted into a string. */
+ i = cp_demangled_name_to_comp (name, NULL);
+ if (i != NULL)
+ {
+ /* Merge the two trees. */
+ cp_merge_demangle_parse_infos (info, ret_comp, i);
+
+ /* Replace any newly introduced typedefs -- but not
+ if the type is anonymous (that would lead to infinite
+ looping). */
+ if (!is_anon)
+ replace_typedefs (info, ret_comp);
+ }
+ else
+ {
+ /* This shouldn't happen unless the type printer has
+ output something that the name parser cannot grok.
+ Nonetheless, an ounce of prevention...
+
+ Canonicalize the name again, and store it in the
+ current node (RET_COMP). */
+ char *canon = cp_canonicalize_string_no_typedefs (name);
+
+ if (canon != NULL)
+ {
+ /* Copy the canonicalization into the obstack and
+ free CANON. */
+ name = copy_string_to_obstack (&info->obstack, canon, &len);
+ xfree (canon);
+ }
+
+ ret_comp->u.s_name.s = name;
+ ret_comp->u.s_name.len = len;
+ }
+
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+/* Replace any typedefs appearing in the qualified name
+ (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
+ given in INFO. */
+
+static void
+replace_typedefs_qualified_name (struct demangle_parse_info *info,
+ struct demangle_component *ret_comp)
+{
+ long len;
+ char *name;
+ struct ui_file *buf = mem_fileopen ();
+ struct demangle_component *comp = ret_comp;
+
+ /* Walk each node of the qualified name, reconstructing the name of
+ this element. With every node, check for any typedef substitutions.
+ If a substitution has occurred, replace the qualified name node
+ with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
+ substituted name. */
+ while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
+ {
+ if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
+ {
+ struct demangle_component new;
+
+ ui_file_write (buf, d_left (comp)->u.s_name.s,
+ d_left (comp)->u.s_name.len);
+ name = ui_file_obsavestring (buf, &info->obstack, &len);
+ new.type = DEMANGLE_COMPONENT_NAME;
+ new.u.s_name.s = name;
+ new.u.s_name.len = len;
+ if (inspect_type (info, &new))
+ {
+ char *n, *s;
+ long slen;
+
+ /* A typedef was substituted in NEW. Convert it to a
+ string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
+ node. */
+
+ ui_file_rewind (buf);
+ n = cp_comp_to_string (&new, 100);
+ if (n == NULL)
+ {
+ /* If something went astray, abort typedef substitutions. */
+ ui_file_delete (buf);
+ return;
+ }