gdb/testsuite: Handle underlying type in gdb.cp/var-tag.exp.
[deliverable/binutils-gdb.git] / gdb / cp-namespace.c
index 279021e046f4a4dee3eb8a94c5834001262c6780..ae7c8527df50e039e9f7d8fe6cbc3a31e9f1e48a 100644 (file)
@@ -1,5 +1,5 @@
 /* Helper routines for C++ support in GDB.
-   Copyright (C) 2003-2013 Free Software Foundation, Inc.
+   Copyright (C) 2003-2014 Free Software Foundation, Inc.
 
    Contributed by David Carlton and by Kealia, Inc.
 
@@ -221,7 +221,12 @@ cp_is_anonymous (const char *namespace)
    we're looking for, BLOCK is the block that we're searching within,
    DOMAIN says what kind of symbols we're looking for, and if SYMTAB
    is non-NULL, we should store the symtab where we found the symbol
-   in it.  */
+   in it.
+
+   Class, union, and enum tag names may be used in C++ without specifying
+   class-key or enum.  If searching for a VAR_DOMAIN symbol fails,
+   this function will search STRUCT_DOMAIN.  [This is actually done in
+   cp_lookup_symbol_in_namespace.]  */
 
 struct symbol *
 cp_lookup_symbol_nonlocal (const char *name,
@@ -242,7 +247,10 @@ cp_lookup_symbol_nonlocal (const char *name,
 
 /* Look up NAME in the C++ namespace NAMESPACE.  Other arguments are
    as in cp_lookup_symbol_nonlocal.  If SEARCH is non-zero, search
-   through base classes for a matching symbol.  */
+   through base classes for a matching symbol.
+
+   If DOMAIN is VAR_DOMAIN and no matching symbol exists in that domain,
+   this function will search STRUCT_DOMAIN for a match.  */
 
 static struct symbol *
 cp_lookup_symbol_in_namespace (const char *namespace,
@@ -252,18 +260,30 @@ cp_lookup_symbol_in_namespace (const char *namespace,
 {
   if (namespace[0] == '\0')
     {
-      return lookup_symbol_file (name, block, domain, 0, search);
+      struct symbol *sym = lookup_symbol_file (name, block, domain, 0, search);
+
+      if (sym == NULL && domain == VAR_DOMAIN)
+       sym = lookup_symbol_file (name, block, STRUCT_DOMAIN, 0, search);
+
+      return sym;
     }
   else
     {
+      struct symbol *sym;
       char *concatenated_name = alloca (strlen (namespace) + 2
                                        + strlen (name) + 1);
 
       strcpy (concatenated_name, namespace);
       strcat (concatenated_name, "::");
       strcat (concatenated_name, name);
-      return lookup_symbol_file (concatenated_name, block, domain,
-                                cp_is_anonymous (namespace), search);
+      sym = lookup_symbol_file (concatenated_name, block, domain,
+                               cp_is_anonymous (namespace), search);
+
+      if (sym == NULL && domain == VAR_DOMAIN)
+       sym = lookup_symbol_file (concatenated_name, block, STRUCT_DOMAIN,
+                                 cp_is_anonymous (namespace), search);
+
+      return sym;
     }
 }
 
@@ -473,7 +493,8 @@ cp_lookup_symbol_imports_or_template (const char *scope,
          char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
          struct cleanup *cleanups = make_cleanup (xfree, name_copy);
          const struct language_defn *lang = language_def (language_cplus);
-         struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch;
+         struct gdbarch *arch
+           = get_objfile_arch (SYMBOL_SYMTAB (function)->objfile);
          const struct block *parent = BLOCK_SUPERBLOCK (block);
 
          while (1)
@@ -499,7 +520,10 @@ cp_lookup_symbol_imports_or_template (const char *scope,
                                      TYPE_N_TEMPLATE_ARGUMENTS (context),
                                      TYPE_TEMPLATE_ARGUMENTS (context));
              if (result != NULL)
-               return result;
+               {
+                 do_cleanups (cleanups);
+                 return result;
+               }
            }
 
          do_cleanups (cleanups);
@@ -512,7 +536,12 @@ cp_lookup_symbol_imports_or_template (const char *scope,
  /* Searches for NAME in the current namespace, and by applying
     relevant import statements belonging to BLOCK and its parents.
     SCOPE is the namespace scope of the context in which the search is
-    being evaluated.  */
+    being evaluated.
+
+    Class, union, and enum tag names may be used in C++ without specifying
+    class-key or enum.  If searching for a VAR_DOMAIN symbol fails,
+    this function will search STRUCT_DOMAIN.  [This is done in
+    cp_lookup_symbol_in_namespace.]  */
 
 struct symbol*
 cp_lookup_symbol_namespace (const char *scope,
@@ -665,6 +694,11 @@ lookup_symbol_file (const char *name,
            }
 
          type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
+         /* If TYPE_NAME is NULL, abandon trying to find this symbol.
+            This can happen for lambda functions compiled with clang++,
+            which outputs no name for the container class.  */
+         if (TYPE_NAME (type) == NULL)
+           return NULL;
          klass = xstrdup (TYPE_NAME (type));
          nested = xstrdup (name);
        }
@@ -684,7 +718,7 @@ lookup_symbol_file (const char *name,
 
       /* Lookup a class named KLASS.  If none is found, there is nothing
         more that can be done.  */
-      klass_sym = lookup_symbol_global (klass, block, domain);
+      klass_sym = lookup_symbol_global (klass, block, STRUCT_DOMAIN);
       if (klass_sym == NULL)
        {
          do_cleanups (cleanup);
@@ -699,6 +733,34 @@ lookup_symbol_file (const char *name,
   return sym;
 }
 
+/* Search through the base classes of PARENT_TYPE for a base class
+   named NAME and return its type.  If not found, return NULL.  */
+
+struct type *
+find_type_baseclass_by_name (struct type *parent_type, const char *name)
+{
+  int i;
+
+  CHECK_TYPEDEF (parent_type);
+  for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
+    {
+      struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
+      const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
+
+      if (base_name == NULL)
+       continue;
+
+      if (streq (base_name, name))
+       return type;
+
+      type = find_type_baseclass_by_name (type, name);
+      if (type != NULL)
+       return type;
+    }
+
+  return NULL;
+}
+
 /* Search through the base classes of PARENT_TYPE for a symbol named
    NAME in block BLOCK.  */
 
@@ -717,36 +779,40 @@ find_symbol_in_baseclass (struct type *parent_type, const char *name,
   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
     {
       size_t len;
+      struct type *base_type = TYPE_BASECLASS (parent_type, i);
       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
 
       if (base_name == NULL)
        continue;
 
       /* Search this particular base class.  */
-      sym = cp_lookup_symbol_namespace (base_name, name, block, VAR_DOMAIN);
+      sym = cp_lookup_symbol_in_namespace (base_name, name, block,
+                                          VAR_DOMAIN, 0);
       if (sym != NULL)
        break;
 
+      /* Now search all static file-level symbols.  We have to do this for
+        things like typedefs in the class.  First search in this symtab,
+        what we want is possibly there.  */
       len = strlen (base_name) + 2 + strlen (name) + 1;
       concatenated_name = xrealloc (concatenated_name, len);
       xsnprintf (concatenated_name, len, "%s::%s", base_name, name);
       sym = lookup_symbol_static (concatenated_name, block, VAR_DOMAIN);
+      if (sym != NULL)
+       break;
 
-      /* If there is currently no BLOCK, e.g., the inferior hasn't yet
-        been started, then try searching all STATIC_BLOCK symbols in
-        all objfiles.  */
-      if (block == NULL)
-       {
-         sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
-         if (sym != NULL)
-           break;
-       }
+      /* Nope.  We now have to search all static blocks in all objfiles,
+        even if block != NULL, because there's no guarantees as to which
+        symtab the symbol we want is in.  */
+      sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
+      if (sym != NULL)
+       break;
 
       /* If this class has base classes, search them next.  */
-      if (TYPE_N_BASECLASSES (TYPE_BASECLASS (parent_type, i)) > 0)
+      CHECK_TYPEDEF (base_type);
+      if (TYPE_N_BASECLASSES (base_type) > 0)
        {
-         sym = find_symbol_in_baseclass (TYPE_BASECLASS (parent_type, i),
-                                         name, block);
+         sym = find_symbol_in_baseclass (base_type, name, block);
          if (sym != NULL)
            break;
        }
@@ -776,6 +842,11 @@ cp_lookup_nested_symbol (struct type *parent_type,
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_NAMESPACE:
     case TYPE_CODE_UNION:
+    case TYPE_CODE_ENUM:
+    /* NOTE: Handle modules here as well, because Fortran is re-using the C++
+       specific code to lookup nested symbols in modules, by calling the
+       function pointer la_lookup_symbol_nonlocal, which ends up here.  */
+    case TYPE_CODE_MODULE:
       {
        /* NOTE: carlton/2003-11-10: We don't treat C++ class members
           of classes like, say, data or function members.  Instead,
@@ -794,8 +865,8 @@ cp_lookup_nested_symbol (struct type *parent_type,
        if (sym != NULL)
          return sym;
 
-       /* Now search all static file-level symbols.  Not strictly
-          correct, but more useful than an error.  We do not try to
+       /* Now search all static file-level symbols.  We have to do this
+          for things like typedefs in the class.  We do not try to
           guess any imported namespace as even the fully specified
           namespace search is already not C++ compliant and more
           assumptions could make it too magic.  */
@@ -812,6 +883,11 @@ cp_lookup_nested_symbol (struct type *parent_type,
           base classes.  */
        return find_symbol_in_baseclass (parent_type, nested_name, block);
       }
+
+    case TYPE_CODE_FUNC:
+    case TYPE_CODE_METHOD:
+      return NULL;
+
     default:
       internal_error (__FILE__, __LINE__,
                      _("cp_lookup_nested_symbol called "
This page took 0.026586 seconds and 4 git commands to generate.