+/* A helper function for dwarf2_compute_name which determines whether DIE
+ needs to have the name of the scope prepended to the name listed in the
+ die. */
+
+static int
+die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
+{
+ struct attribute *attr;
+
+ switch (die->tag)
+ {
+ case DW_TAG_namespace:
+ case DW_TAG_typedef:
+ case DW_TAG_class_type:
+ case DW_TAG_interface_type:
+ case DW_TAG_structure_type:
+ case DW_TAG_union_type:
+ case DW_TAG_enumeration_type:
+ case DW_TAG_enumerator:
+ case DW_TAG_subprogram:
+ case DW_TAG_member:
+ return 1;
+
+ case DW_TAG_variable:
+ /* We only need to prefix "globally" visible variables. These include
+ any variable marked with DW_AT_external or any variable that
+ lives in a namespace. [Variables in anonymous namespaces
+ require prefixing, but they are not DW_AT_external.] */
+
+ if (dwarf2_attr (die, DW_AT_specification, cu))
+ {
+ struct dwarf2_cu *spec_cu = cu;
+
+ return die_needs_namespace (die_specification (die, &spec_cu),
+ spec_cu);
+ }
+
+ attr = dwarf2_attr (die, DW_AT_external, cu);
+ if (attr == NULL && die->parent->tag != DW_TAG_namespace
+ && die->parent->tag != DW_TAG_module)
+ return 0;
+ /* A variable in a lexical block of some kind does not need a
+ namespace, even though in C++ such variables may be external
+ and have a mangled name. */
+ if (die->parent->tag == DW_TAG_lexical_block
+ || die->parent->tag == DW_TAG_try_block
+ || die->parent->tag == DW_TAG_catch_block
+ || die->parent->tag == DW_TAG_subprogram)
+ return 0;
+ return 1;
+
+ default:
+ return 0;
+ }
+}
+
+/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
+ compute the physname for the object, which include a method's
+ formal parameters (C++/Java) and return type (Java).
+
+ For Ada, return the DIE's linkage name rather than the fully qualified
+ name. PHYSNAME is ignored..
+
+ The result is allocated on the objfile_obstack and canonicalized. */
+
+static const char *
+dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
+ int physname)
+{
+ if (name == NULL)
+ name = dwarf2_name (die, cu);
+
+ /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
+ compute it by typename_concat inside GDB. */
+ if (cu->language == language_ada
+ || (cu->language == language_fortran && physname))
+ {
+ /* For Ada unit, we prefer the linkage name over the name, as
+ the former contains the exported name, which the user expects
+ to be able to reference. Ideally, we want the user to be able
+ to reference this entity using either natural or linkage name,
+ but we haven't started looking at this enhancement yet. */
+ struct attribute *attr;
+
+ attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
+ if (attr == NULL)
+ attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
+ if (attr && DW_STRING (attr))
+ return DW_STRING (attr);
+ }
+
+ /* These are the only languages we know how to qualify names in. */
+ if (name != NULL
+ && (cu->language == language_cplus || cu->language == language_java
+ || cu->language == language_fortran))
+ {
+ if (die_needs_namespace (die, cu))
+ {
+ long length;
+ char *prefix;
+ struct ui_file *buf;
+
+ prefix = determine_prefix (die, cu);
+ buf = mem_fileopen ();
+ if (*prefix != '\0')
+ {
+ char *prefixed_name = typename_concat (NULL, prefix, name,
+ physname, cu);
+
+ fputs_unfiltered (prefixed_name, buf);
+ xfree (prefixed_name);
+ }
+ else
+ fputs_unfiltered (name ? name : "", buf);
+
+ /* For Java and C++ methods, append formal parameter type
+ information, if PHYSNAME. */
+
+ if (physname && die->tag == DW_TAG_subprogram
+ && (cu->language == language_cplus
+ || cu->language == language_java))
+ {
+ struct type *type = read_type_die (die, cu);
+
+ c_type_print_args (type, buf, 0, cu->language);
+
+ if (cu->language == language_java)
+ {
+ /* For java, we must append the return type to method
+ names. */
+ if (die->tag == DW_TAG_subprogram)
+ java_print_type (TYPE_TARGET_TYPE (type), "", buf,
+ 0, 0);
+ }
+ else if (cu->language == language_cplus)
+ {
+ if (TYPE_NFIELDS (type) > 0
+ && TYPE_FIELD_ARTIFICIAL (type, 0)
+ && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
+ fputs_unfiltered (" const", buf);
+ }
+ }
+
+ name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
+ &length);
+ ui_file_delete (buf);
+
+ if (cu->language == language_cplus)
+ {
+ char *cname
+ = dwarf2_canonicalize_name (name, cu,
+ &cu->objfile->objfile_obstack);
+
+ if (cname != NULL)
+ name = cname;
+ }
+ }
+ }
+
+ return name;
+}
+