Convert symbol_set_demangled_name to a method
[deliverable/binutils-gdb.git] / gdb / symtab.c
index aa415a9399938a44992d00c48bd88361eac68d01..c9c75e94182242c32e1c71afecf6628e6cd65f6a 100644 (file)
@@ -667,29 +667,27 @@ gdb_mangle_name (struct type *type, int method_id, int signature_id)
   return (mangled_name);
 }
 
-/* Set the demangled name of GSYMBOL to NAME.  NAME must be already
-   correctly allocated.  */
+/* See symtab.h.  */
 
 void
-symbol_set_demangled_name (struct general_symbol_info *gsymbol,
-                           const char *name,
-                           struct obstack *obstack)
+general_symbol_info::set_demangled_name (const char *name,
+                                        struct obstack *obstack)
 {
-  if (gsymbol->language () == language_ada)
+  if (language () == language_ada)
     {
       if (name == NULL)
        {
-         gsymbol->ada_mangled = 0;
-         gsymbol->language_specific.obstack = obstack;
+         ada_mangled = 0;
+         language_specific.obstack = obstack;
        }
       else
        {
-         gsymbol->ada_mangled = 1;
-         gsymbol->language_specific.demangled_name = name;
+         ada_mangled = 1;
+         language_specific.demangled_name = name;
        }
     }
   else
-    gsymbol->language_specific.demangled_name = name;
+    language_specific.demangled_name = name;
 }
 
 /* Return the demangled name of GSYMBOL.  */
@@ -722,7 +720,7 @@ general_symbol_info::set_language (enum language language,
       || language == language_objc
       || language == language_fortran)
     {
-      symbol_set_demangled_name (this, NULL, obstack);
+      set_demangled_name (NULL, obstack);
     }
   else if (language == language_ada)
     {
@@ -872,7 +870,7 @@ general_symbol_info::compute_and_set_names (gdb::string_view linkage_name,
        m_name = obstack_strndup (&per_bfd->storage_obstack,
                                  linkage_name.data (),
                                  linkage_name.length ());
-      symbol_set_demangled_name (this, NULL, &per_bfd->storage_obstack);
+      set_demangled_name (NULL, &per_bfd->storage_obstack);
 
       return;
     }
@@ -962,8 +960,7 @@ general_symbol_info::compute_and_set_names (gdb::string_view linkage_name,
     m_language = (*slot)->language;
 
   m_name = (*slot)->mangled.data ();
-  symbol_set_demangled_name (this, (*slot)->demangled.get (),
-                            &per_bfd->storage_obstack);
+  set_demangled_name ((*slot)->demangled.get (), &per_bfd->storage_obstack);
 }
 
 /* See symtab.h.  */
@@ -1790,7 +1787,7 @@ demangle_for_lookup_info::demangle_for_lookup_info
   if (lookup_name.ignore_parameters () && lang == language_cplus)
     {
       gdb::unique_xmalloc_ptr<char> without_params
-       = cp_remove_params_if_any (lookup_name.name ().c_str (),
+       = cp_remove_params_if_any (lookup_name.c_str (),
                                   lookup_name.completion_mode ());
 
       if (without_params != NULL)
@@ -1803,9 +1800,9 @@ demangle_for_lookup_info::demangle_for_lookup_info
     }
 
   if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
-    m_demangled_name = lookup_name.name ();
+    m_demangled_name = lookup_name.c_str ();
   else
-    m_demangled_name = demangle_for_lookup (lookup_name.name ().c_str (),
+    m_demangled_name = demangle_for_lookup (lookup_name.c_str (),
                                            lang, storage);
 }
 
@@ -1816,7 +1813,7 @@ lookup_name_info::match_any ()
 {
   /* Lookup any symbol that "" would complete.  I.e., this matches all
      symbol names.  */
-  static const lookup_name_info lookup_name ({}, symbol_name_match_type::FULL,
+  static const lookup_name_info lookup_name ("", symbol_name_match_type::FULL,
                                             true);
 
   return lookup_name;
@@ -2285,6 +2282,8 @@ lookup_symbol_in_objfile_symtabs (struct objfile *objfile,
                          name, domain_name (domain));
     }
 
+  struct block_symbol other;
+  other.symbol = NULL;
   for (compunit_symtab *cust : objfile->compunits ())
     {
       const struct blockvector *bv;
@@ -2295,18 +2294,36 @@ lookup_symbol_in_objfile_symtabs (struct objfile *objfile,
       block = BLOCKVECTOR_BLOCK (bv, block_index);
       result.symbol = block_lookup_symbol_primary (block, name, domain);
       result.block = block;
-      if (result.symbol != NULL)
+      if (result.symbol == NULL)
+       continue;
+      if (best_symbol (result.symbol, domain))
        {
-         if (symbol_lookup_debug > 1)
+         other = result;
+         break;
+       }
+      if (symbol_matches_domain (result.symbol->language (),
+                                SYMBOL_DOMAIN (result.symbol), domain))
+       {
+         struct symbol *better
+           = better_symbol (other.symbol, result.symbol, domain);
+         if (better != other.symbol)
            {
-             fprintf_unfiltered (gdb_stdlog, " = %s (block %s)\n",
-                                 host_address_to_string (result.symbol),
-                                 host_address_to_string (block));
+             other.symbol = better;
+             other.block = block;
            }
-         result.symbol = fixup_symbol_section (result.symbol, objfile);
-         return result;
+       }
+    }
 
+  if (other.symbol != NULL)
+    {
+      if (symbol_lookup_debug > 1)
+       {
+         fprintf_unfiltered (gdb_stdlog, " = %s (block %s)\n",
+                             host_address_to_string (other.symbol),
+                             host_address_to_string (other.block));
        }
+      other.symbol = fixup_symbol_section (other.symbol, objfile);
+      return other;
     }
 
   if (symbol_lookup_debug > 1)
@@ -2560,6 +2577,33 @@ lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index,
   return result;
 }
 
+/* Find the language for partial symbol with NAME.  */
+
+static enum language
+find_quick_global_symbol_language (const char *name, const domain_enum domain)
+{
+  for (objfile *objfile : current_program_space->objfiles ())
+    {
+      if (objfile->sf && objfile->sf->qf
+         && objfile->sf->qf->lookup_global_symbol_language)
+       continue;
+      return language_unknown;
+    }
+
+  for (objfile *objfile : current_program_space->objfiles ())
+    {
+      bool symbol_found_p;
+      enum language lang
+       = objfile->sf->qf->lookup_global_symbol_language (objfile, name, domain,
+                                                         &symbol_found_p);
+      if (!symbol_found_p)
+       continue;
+      return lang;
+    }
+
+  return language_unknown;
+}
+
 /* Private data to be used with lookup_symbol_global_iterator_cb.  */
 
 struct global_or_static_sym_lookup_data
@@ -2639,7 +2683,7 @@ lookup_global_or_static_symbol (const char *name,
       lookup_data.block_index = block_index;
       lookup_data.domain = domain;
       gdbarch_iterate_over_objfiles_in_search_order
-       (objfile != NULL ? get_objfile_arch (objfile) : target_gdbarch (),
+       (objfile != NULL ? objfile->arch () : target_gdbarch (),
         lookup_symbol_global_or_static_iterator_cb, &lookup_data, objfile);
       result = lookup_data.result;
     }
@@ -2671,17 +2715,23 @@ lookup_global_symbol (const char *name,
      global block first.  This yields "more expected" behavior, and is
      needed to support 'FILENAME'::VARIABLE lookups.  */
   const struct block *global_block = block_global_block (block);
+  symbol *sym = NULL;
   if (global_block != nullptr)
     {
-      symbol *sym = lookup_symbol_in_block (name,
-                                           symbol_name_match_type::FULL,
-                                           global_block, domain);
-      if (sym != nullptr)
+      sym = lookup_symbol_in_block (name,
+                                   symbol_name_match_type::FULL,
+                                   global_block, domain);
+      if (sym != NULL && best_symbol (sym, domain))
        return { sym, global_block };
     }
 
   struct objfile *objfile = lookup_objfile_from_block (block);
-  return lookup_global_or_static_symbol (name, GLOBAL_BLOCK, objfile, domain);
+  block_symbol bs
+    = lookup_global_or_static_symbol (name, GLOBAL_BLOCK, objfile, domain);
+  if (better_symbol (sym, bs.symbol, domain) == sym)
+    return { sym, global_block };
+  else
+    return bs;
 }
 
 bool
@@ -3620,7 +3670,7 @@ find_function_start_sal_1 (CORE_ADDR func_addr, obj_section *section,
       && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal.symtab))
          || SYMTAB_LANGUAGE (sal.symtab) == language_asm))
     {
-      struct gdbarch *gdbarch = get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
+      struct gdbarch *gdbarch = SYMTAB_OBJFILE (sal.symtab)->arch ();
 
       sal.pc = func_addr;
       if (gdbarch_skip_entrypoint_p (gdbarch))
@@ -3779,7 +3829,7 @@ skip_prologue_sal (struct symtab_and_line *sal)
       name = msymbol.minsym->linkage_name ();
     }
 
-  gdbarch = get_objfile_arch (objfile);
+  gdbarch = objfile->arch ();
 
   /* Process the prologue in two passes.  In the first pass try to skip the
      prologue (SKIP is true) and verify there is a real need for it (indicated
@@ -4513,7 +4563,7 @@ global_symbol_searcher::expand_symtabs
        {
         return file_matches (filename, filenames, basenames);
        },
-       lookup_name_info::match_any (),
+       &lookup_name_info::match_any (),
        [&] (const char *symname)
        {
         return (!preg.has_value ()
@@ -4901,7 +4951,7 @@ print_symbol_info (enum search_domain kind,
 static void
 print_msymbol_info (struct bound_minimal_symbol msymbol)
 {
-  struct gdbarch *gdbarch = get_objfile_arch (msymbol.objfile);
+  struct gdbarch *gdbarch = msymbol.objfile->arch ();
   char *tmp;
 
   if (gdbarch_addr_bit (gdbarch) <= 32)
@@ -5296,6 +5346,27 @@ completion_list_add_symbol (completion_tracker &tracker,
   completion_list_add_name (tracker, sym->language (),
                            sym->natural_name (),
                            lookup_name, text, word);
+
+  /* C++ function symbols include the parameters within both the msymbol
+     name and the symbol name.  The problem is that the msymbol name will
+     describe the parameters in the most basic way, with typedefs stripped
+     out, while the symbol name will represent the types as they appear in
+     the program.  This means we will see duplicate entries in the
+     completion tracker.  The following converts the symbol name back to
+     the msymbol name and removes the msymbol name from the completion
+     tracker.  */
+  if (sym->language () == language_cplus
+      && SYMBOL_DOMAIN (sym) == VAR_DOMAIN
+      && SYMBOL_CLASS (sym) == LOC_BLOCK)
+    {
+      /* The call to canonicalize returns the empty string if the input
+        string is already in canonical form, thanks to this we don't
+        remove the symbol we just added above.  */
+      std::string str
+       = cp_canonicalize_string_no_typedefs (sym->natural_name ());
+      if (!str.empty ())
+       tracker.remove_completion (str.c_str ());
+    }
 }
 
 /* completion_list_add_name wrapper for struct minimal_symbol.  */
@@ -5502,7 +5573,7 @@ find_gnu_ifunc (const symbol *sym)
          CORE_ADDR msym_addr = MSYMBOL_VALUE_ADDRESS (objfile, minsym);
          if (MSYMBOL_TYPE (minsym) == mst_data_gnu_ifunc)
            {
-             struct gdbarch *gdbarch = get_objfile_arch (objfile);
+             struct gdbarch *gdbarch = objfile->arch ();
              msym_addr
                = gdbarch_convert_from_func_ptr_addr (gdbarch,
                                                      msym_addr,
@@ -6123,6 +6194,16 @@ find_main_name (void)
 
   /* The languages above didn't identify the name of the main procedure.
      Fallback to "main".  */
+
+  /* Try to find language for main in psymtabs.  */
+  enum language lang
+    = find_quick_global_symbol_language ("main", VAR_DOMAIN);
+  if (lang != language_unknown)
+    {
+      set_main_name ("main", lang);
+      return;
+    }
+
   set_main_name ("main", language_unknown);
 }
 
@@ -6347,7 +6428,7 @@ symbol_arch (const struct symbol *symbol)
 {
   if (!SYMBOL_OBJFILE_OWNED (symbol))
     return symbol->owner.arch;
-  return get_objfile_arch (SYMTAB_OBJFILE (symbol->owner.symtab));
+  return SYMTAB_OBJFILE (symbol->owner.symtab)->arch ();
 }
 
 /* See symtab.h.  */
@@ -6380,6 +6461,9 @@ get_symbol_address (const struct symbol *sym)
 
   for (objfile *objfile : current_program_space->objfiles ())
     {
+      if (objfile->separate_debug_objfile_backlink != nullptr)
+       continue;
+
       bound_minimal_symbol minsym
        = lookup_minimal_symbol_linkage (linkage_name, objfile);
       if (minsym.minsym != nullptr)
@@ -6400,7 +6484,8 @@ get_msymbol_address (struct objfile *objf, const struct minimal_symbol *minsym)
 
   for (objfile *objfile : current_program_space->objfiles ())
     {
-      if ((objfile->flags & OBJF_MAINLINE) != 0)
+      if (objfile->separate_debug_objfile_backlink == nullptr
+         && (objfile->flags & OBJF_MAINLINE) != 0)
        {
          bound_minimal_symbol found
            = lookup_minimal_symbol_linkage (linkage_name, objfile);
@@ -6417,15 +6502,6 @@ get_msymbol_address (struct objfile *objf, const struct minimal_symbol *minsym)
 
 static struct cmd_list_element *info_module_cmdlist = NULL;
 
-/* Implement the 'info module' command, just displays some help text for
-   the available sub-commands.  */
-
-static void
-info_module_command (const char *args, int from_tty)
-{
-  help_list (info_module_cmdlist, "info module ", class_info, gdb_stdout);
-}
-
 /* See symtab.h.  */
 
 std::vector<module_symbol_search>
@@ -6784,10 +6860,10 @@ Options:\n\
                _("All module names, or those matching REGEXP."));
   set_cmd_completer_handle_brkchars (c, info_types_command_completer);
 
-  add_prefix_cmd ("module", class_info, info_module_command, _("\
+  add_basic_prefix_cmd ("module", class_info, _("\
 Print information about modules."),
-                 &info_module_cmdlist, "info module ",
-                 0, &infolist);
+                       &info_module_cmdlist, "info module ",
+                       0, &infolist);
 
   c = add_cmd ("functions", class_info, info_module_functions_command, _("\
 Display functions arranged by modules.\n\
This page took 0.035466 seconds and 4 git commands to generate.