public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Nathan Sidwell <nathan@acm.org>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [C++ PATCH] namespace bindings
Date: Tue, 23 May 2017 19:47:00 -0000	[thread overview]
Message-ID: <7692a478-486b-2e37-ae88-e8d6e4db83e7@acm.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 127 bytes --]

There's a twisty set of function calling to get at a namespace binding. 
This patch cleans that up.

nathan
-- 
Nathan Sidwell

[-- Attachment #2: ns-bind.diff --]
[-- Type: text/x-patch, Size: 9067 bytes --]

2017-05-23  Nathan Sidwell  <nathan@acm.org>

	* name-lookup.c (find_namespace_binding): New.
	(pushdecl_maybe_friend_1): Use CP_DECL_CONTEXT.
	(set_identifier_type_value_with_scope): Use find_namespace_binding.
	(find_binding, cp_binding_level_find_binding_for_name,
	binding_for_name, namespace_binding_1): Delete.
	(push_overloaded_decl_1): Use CP_DECL_CONTEXT.
	(get_namespace_binding, set_namespace_binding,
	finish_namespace_using_decl, unqualified_namespace_lookup_1,
	qualified_lookup_using_namespace, lookup_type_scope_1,
	lookup_name_innermost_nonclass_level_1): Use find_namespace_binding.

Index: name-lookup.c
===================================================================
--- name-lookup.c	(revision 248377)
+++ name-lookup.c	(working copy)
@@ -48,7 +48,6 @@ struct scope_binding {
 };
 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
 
-static cxx_binding *binding_for_name (cp_binding_level *, tree);
 static tree push_overloaded_decl (tree, int, bool);
 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
 				    tree, int);
@@ -62,6 +61,32 @@ static void consider_binding_level (tree
 static tree push_using_directive (tree);
 static void diagnose_name_conflict (tree, tree);
 
+/* Find the binding for NAME in namespace NS.  If CREATE_P is true,
+   make an empty binding if there wasn't one.  */
+
+static cxx_binding *
+find_namespace_binding (tree ns, tree name, bool create_p = false)
+{
+  cp_binding_level *level = NAMESPACE_LEVEL (ns);
+  cxx_binding *binding = IDENTIFIER_NAMESPACE_BINDINGS (name);
+
+  for (;binding; binding = binding->previous)
+    if (binding->scope == level)
+      return binding;
+
+  if (create_p)
+    {
+      binding = cxx_binding_make (NULL, NULL);
+      binding->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
+      binding->scope = level;
+      binding->is_local = false;
+      binding->value_is_inherited = false;
+      IDENTIFIER_NAMESPACE_BINDINGS (name) = binding;
+    }
+
+  return binding;
+}
+
 /* Add DECL to the list of things declared in B.  */
 
 static void
@@ -1698,7 +1723,7 @@ pushdecl_maybe_friend_1 (tree x, bool is
       /* In case this decl was explicitly namespace-qualified, look it
 	 up in its namespace context.  */
       if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
-	t = get_namespace_binding (DECL_CONTEXT (x), name);
+	t = get_namespace_binding (CP_DECL_CONTEXT (x), name);
       else
 	t = lookup_name_innermost_nonclass_level (name);
 
@@ -2702,9 +2727,9 @@ set_identifier_type_value_with_scope (tr
     }
   else
     {
-      cxx_binding *binding =
-	binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
-      gcc_assert (decl);
+      cxx_binding *binding
+	= find_namespace_binding (current_namespace, id, true);
+
       if (binding->value)
 	supplement_binding (binding, decl);
       else
@@ -2811,55 +2836,6 @@ make_lambda_name (void)
   return get_identifier (buf);
 }
 
-/* Return (from the stack of) the BINDING, if any, established at SCOPE.  */
-
-static inline cxx_binding *
-find_binding (cp_binding_level *scope, cxx_binding *binding)
-{
-  for (; binding != NULL; binding = binding->previous)
-    if (binding->scope == scope)
-      return binding;
-
-  return (cxx_binding *)0;
-}
-
-/* Return the binding for NAME in SCOPE, if any.  Otherwise, return NULL.  */
-
-static inline cxx_binding *
-cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
-{
-  cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
-  if (b)
-    {
-      /* Fold-in case where NAME is used only once.  */
-      if (scope == b->scope && b->previous == NULL)
-	return b;
-      return find_binding (scope, b);
-    }
-  return NULL;
-}
-
-/* Always returns a binding for name in scope.  If no binding is
-   found, make a new one.  */
-
-static cxx_binding *
-binding_for_name (cp_binding_level *scope, tree name)
-{
-  cxx_binding *result;
-
-  result = cp_binding_level_find_binding_for_name (scope, name);
-  if (result)
-    return result;
-  /* Not found, make a new one.  */
-  result = cxx_binding_make (NULL, NULL);
-  result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
-  result->scope = scope;
-  result->is_local = false;
-  result->value_is_inherited = false;
-  IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
-  return result;
-}
-
 /* Insert another USING_DECL into the current binding level, returning
    this declaration. If this is a redeclaration, do nothing, and
    return NULL_TREE if this not in namespace scope (in namespace
@@ -2978,7 +2954,7 @@ push_overloaded_decl_1 (tree decl, int f
   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
 
   if (doing_global)
-    old = get_namespace_binding (DECL_CONTEXT (decl), name);
+    old = get_namespace_binding (CP_DECL_CONTEXT (decl), name);
   else
     old = lookup_name_innermost_nonclass_level (name);
 
@@ -3965,25 +3941,6 @@ do_class_using_decl (tree scope, tree na
 }
 
 \f
-/* Return the binding value for name in scope.  */
-
-
-static tree
-namespace_binding_1 (tree scope, tree name)
-{
-  cxx_binding *binding;
-
-  if (SCOPE_FILE_SCOPE_P (scope))
-    scope = global_namespace;
-  else
-    /* Unnecessary for the global namespace because it can't be an alias. */
-    scope = ORIGINAL_NAMESPACE (scope);
-
-  binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
-
-  return binding ? binding->value : NULL_TREE;
-}
-
 /* Return the binding for NAME in NS.  If NS is NULL, look in
    global_namespace.  */
 
@@ -3993,28 +3950,26 @@ get_namespace_binding (tree ns, tree nam
   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
   if (!ns)
     ns = global_namespace;
-  tree ret = namespace_binding_1 (ns, name);
+  cxx_binding *binding = find_namespace_binding (ns, name);
   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
-  return ret;
+  return binding ? binding->value : NULL_TREE;
 }
 
 static void
 set_namespace_binding (tree scope, tree name, tree val)
 {
-  cxx_binding *b;
-
   if (scope == NULL_TREE)
     scope = global_namespace;
-  b = binding_for_name (NAMESPACE_LEVEL (scope), name);
-  if (!b->value
+  cxx_binding *binding = find_namespace_binding (scope, name, true);
+  if (!binding->value
       /* For templates and using we create a single element OVERLOAD.
 	 Look for the chain to know whether this is really augmenting
 	 an existing overload.  */
       || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val))
       || val == error_mark_node)
-    b->value = val;
+    binding->value = val;
   else
-    supplement_binding (b, val);
+    supplement_binding (binding, val);
 }
 
 /* Set value binding og NAME in the global namespace to VAL.  Does not
@@ -4366,7 +4321,7 @@ finish_namespace_using_decl (tree decl,
     return;
 
   cxx_binding *binding
-    = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
+    = find_namespace_binding (current_namespace, name, true);
 
   tree value = binding->value;
   tree type = binding->type;
@@ -4917,8 +4872,7 @@ unqualified_namespace_lookup_1 (tree nam
   for (; !val; scope = CP_DECL_CONTEXT (scope))
     {
       struct scope_binding binding = EMPTY_SCOPE_BINDING;
-      cxx_binding *b =
-	 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
+      cxx_binding *b = find_namespace_binding (scope, name);
 
       if (b)
 	ambiguous_decl (&binding, b, flags);
@@ -5021,8 +4975,7 @@ lookup_using_namespace (tree name, struc
     if (TREE_VALUE (iter) == scope)
       {
 	tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
-	cxx_binding *val1 =
-	  cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
+	cxx_binding *val1 = find_namespace_binding (used, name);
 	/* Resolve ambiguities.  */
 	if (val1)
 	  ambiguous_decl (val, val1, flags);
@@ -5092,8 +5045,7 @@ qualified_lookup_using_namespace (tree n
 	    continue;
 	  vec_safe_push (seen_inline, scope);
 
-	  binding =
-	    cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
+	  binding = find_namespace_binding (scope, name);
 	  if (binding)
 	    {
 	      ambiguous_decl (result, binding, flags);
@@ -5610,8 +5562,7 @@ lookup_type_scope_1 (tree name, tag_scop
   /* Look in namespace scope.  */
   if (!val)
     {
-      iter = cp_binding_level_find_binding_for_name
-	       (NAMESPACE_LEVEL (current_decl_namespace ()), name);
+      iter = find_namespace_binding (current_decl_namespace (), name);
 
       if (iter)
 	{
@@ -5668,13 +5619,14 @@ static tree
 lookup_name_innermost_nonclass_level_1 (tree name)
 {
   cp_binding_level *b = innermost_nonclass_level ();
+  cxx_binding *binding = NULL;
 
   if (b->kind == sk_namespace)
-    return namespace_binding_1 (current_namespace, name);
-  else if (cxx_binding *binding = find_local_binding (b, name))
-    return binding->value;
+    binding = find_namespace_binding (current_namespace, name);
   else
-    return NULL_TREE;
+    binding = find_local_binding (b, name);
+
+  return binding ? binding->value : NULL_TREE;
 }
 
 /* Wrapper for lookup_name_innermost_nonclass_level_1.  */

             reply	other threads:[~2017-05-23 19:38 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-23 19:47 Nathan Sidwell [this message]
  -- strict thread matches above, loose matches on Subject: below --
2017-05-05 20:25 Nathan Sidwell
2017-05-08 12:41 ` Andreas Schwab
2017-05-08 13:14   ` Nathan Sidwell
2017-05-08 14:33   ` Nathan Sidwell
2017-05-08 15:25     ` Andreas Schwab
2017-05-08 17:03       ` Nathan Sidwell
2017-05-08 13:06 ` Jason Merrill via gcc-patches
2017-05-08 13:34   ` Nathan Sidwell
2017-05-08 15:54   ` Nathan Sidwell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7692a478-486b-2e37-ae88-e8d6e4db83e7@acm.org \
    --to=nathan@acm.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).