diff mbox

[c++] Keep tm, div_t, ldiv_t, lconv mangling on Solaris (PR libstdc++-v3/1773)

Message ID yddei0lfq82.fsf@manam.CeBiTec.Uni-Bielefeld.DE
State New
Headers show

Commit Message

Rainer Orth Aug. 16, 2011, 1:19 p.m. UTC
Jason,

> On 08/11/2011 10:49 AM, Rainer Orth wrote:
>> There might be an alternative implementation that is less invasive to
>> the C++ frontend, though: add
>>
>> 	&&  TARGET_DECL_NAMESPACE_STD_P (decl)
>>
>> in write_unscoped_name, defaulting to true, override it in sol2.h (which
>> gets included via tm.h) and have the remaining logic in a new sol2-cxx.c
>> file.
>
> I would be OK with a target hook.  I think a cleaner place would be to hook
> decl_mangling_context and then change write_unscoped_name to use
> decl_mangling_context instead of CP_DECL_CONTEXT.

thanks for the hint.  This resulted in a far cleaner patch indeed.  It
has been bootstrapped without regressions on i386-pc-solaris2.10 and
x86_64-unknown-linux-gnu in a tree also including

	[v3] Use Solaris prototypes if possible (PR libstdc++-v3/1773)
        http://gcc.gnu.org/ml/gcc-patches/2011-08/msg00835.html

and the revised version of

	[libcpp] Correctly define __cplusplus (PR libstdc++-v3/1773)
        http://gcc.gnu.org/ml/gcc-patches/2011-08/msg00837.html

that only defines __cplusplus 199711L.

Ok for mainline now?

	Rainer


2011-08-07  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
	    Marc Glisse  <marc.glisse@normalesup.org>

	gcc:
	PR libstdc++-v3/1773
	* target.def (decl_mangling_context): New C++ hook.
	* doc/tm.texi: Regenerate.
	* config/sol2-cxx.c, config/sol2-stubs.c: New files.
	* config/sol2-protos.h: Group by source file.
	(solaris_cxx_decl_mangling_context): Declare.
	* config/sol2.h (TARGET_CXX_DECL_MANGLING_CONTEXT): Define.
	* config/t-sol2 (sol2-cxx.o, sol2-stubs.o): New targets.
	Use $<.
	* config.gcc (*-*-solaris2*): Add sol2-cxx.o to cxx_target_objs.
	Add sol2-stubs.o to extra_objs.

	gcc/cp:
	PR libstdc++-v3/1773
	* mangle.c (decl_mangling_context): Call
	targetm.cxx.decl_mangling_context.
	(write_unscoped_name): Use decl_mangling_context.

Comments

Marc Glisse Aug. 16, 2011, 3:31 p.m. UTC | #1
On Tue, 16 Aug 2011, Rainer Orth wrote:

> thanks for the hint.  This resulted in a far cleaner patch indeed.

Cool, thanks.

> 	* config/sol2-cxx.c

You are missing a closing parenthesis in there, on the line with 
DECL_NAMESPACE_STD_P. I hadn't dared modify DECL_CONTEXT(decl) because I 
was afraid of side effects.

For instance, in:
namespace std { struct tm {}; }
//void f(std::tm){}
void g(tm){}

uncommenting the middle line turns the error message from:
e.cc:3:10: error: variable or field 'g' declared void
e.cc:3:8: error: 'tm' was not declared in this scope
e.cc:3:8: note: suggested alternative:
e.cc:1:24: note:   'std::tm'

to:
e.cc:3:10: error: variable or field 'g' declared void
e.cc:3:8: error: 'tm' was not declared in this scope
e.cc:3:8: note: suggested alternative:
e.cc:1:24: note:   'tm'

It also breaks argument-dependent lookup:
namespace std { struct tm {}; void h(tm){} }
void g(std::tm x){ h(x); }

On the other hand, there probably isn't much ADL going on with these 4 
types...

It might be less invasive to have decl_mangling_context return 
global_namespace without actually modifying the expr?

(as already mentioned, I don't know much about gcc internals, I am just 
commenting from my experience writing the previous draft patch)
Rainer Orth Aug. 16, 2011, 3:38 p.m. UTC | #2
Marc,

>> 	* config/sol2-cxx.c
>
> You are missing a closing parenthesis in there, on the line with
> DECL_NAMESPACE_STD_P. I hadn't dared modify DECL_CONTEXT(decl) because I

drats, I had fixed that in my tree, but forgot to refresh the patch.

> was afraid of side effects.
>
> For instance, in:
> namespace std { struct tm {}; }
> //void f(std::tm){}
> void g(tm){}
>
> uncommenting the middle line turns the error message from:
> e.cc:3:10: error: variable or field 'g' declared void
> e.cc:3:8: error: 'tm' was not declared in this scope
> e.cc:3:8: note: suggested alternative:
> e.cc:1:24: note:   'std::tm'
>
> to:
> e.cc:3:10: error: variable or field 'g' declared void
> e.cc:3:8: error: 'tm' was not declared in this scope
> e.cc:3:8: note: suggested alternative:
> e.cc:1:24: note:   'tm'
>
> It also breaks argument-dependent lookup:
> namespace std { struct tm {}; void h(tm){} }
> void g(std::tm x){ h(x); }
>
> On the other hand, there probably isn't much ADL going on with these 4
> types...
>
> It might be less invasive to have decl_mangling_context return
> global_namespace without actually modifying the expr?

Maybe.  Let's wait for Jason's comments.

> (as already mentioned, I don't know much about gcc internals, I am just
> commenting from my experience writing the previous draft patch)

Me neither, especially when it gets to the non-port-specific code.
Without your draft, I wouldn't probably have arrived at anything
remotely sensible.

Thanks.
        Rainer
Jason Merrill Aug. 16, 2011, 9:07 p.m. UTC | #3
On 08/16/2011 11:31 AM, Marc Glisse wrote:
> It might be less invasive to have decl_mangling_context return
> global_namespace without actually modifying the expr?

Please.

Jason
diff mbox

Patch

# HG changeset patch
# Parent da2cbe8d1768a3ceb7ca3b9b7a739434168793e3
Keep tm, div_t, ldiv_t, lconv mangling on Solaris (PR libstdc++-v3/1773)

diff --git a/gcc/config.gcc b/gcc/config.gcc
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -718,8 +718,8 @@  case ${target} in
   tm_p_file="${tm_p_file} sol2-protos.h"
   tmake_file="${tmake_file} t-sol2 t-slibgcc-dummy"
   c_target_objs="${c_target_objs} sol2-c.o"
-  cxx_target_objs="${cxx_target_objs} sol2-c.o"
-  extra_objs="sol2.o"
+  cxx_target_objs="${cxx_target_objs} sol2-c.o sol2-cxx.o"
+  extra_objs="sol2.o sol2-stubs.o"
   extra_options="${extra_options} sol2.opt"
   case ${enable_threads}:${have_pthread_h}:${have_thread_h} in
     "":yes:* | yes:yes:* )
diff --git a/gcc/config/sol2-cxx.c b/gcc/config/sol2-cxx.c
new file mode 100644
--- /dev/null
+++ b/gcc/config/sol2-cxx.c
@@ -0,0 +1,65 @@ 
+/* C++ specific Solaris system support.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "cp/cp-tree.h"
+#include "tm.h"
+#include "tm_p.h"
+
+/* Before GCC 4.7, g++ defined __cplusplus 1 to avoid coping with the C++98
+   overloads in Solaris system headers.  Since this was fixed, 4 structure
+   types would move to namespace std, breaking the Solaris libstdc++ ABI.
+   To avoid this, we forcefully keep those types in the global namespace.
+   This can be removed once the next major version of libstdc++ is
+   released.  */
+
+/* Cache the identifiers of the affected types to speed up lookup.  */
+#define NUM_FGID 4
+static GTY(()) tree force_global_identifiers[NUM_FGID];
+
+/* Check if DECL is one of the affected types and move it to the global
+   namespace if so.  */
+void
+solaris_cxx_decl_mangling_context (tree decl)
+{
+  static bool init = false;
+  int i = 0;
+
+  if (!init)
+    {
+      force_global_identifiers[i++] = get_identifier ("div_t");
+      force_global_identifiers[i++] = get_identifier ("ldiv_t");
+      force_global_identifiers[i++] = get_identifier ("lconv");
+      force_global_identifiers[i++] = get_identifier ("tm");
+      init = true;
+    }
+
+  if (!(DECL_P (decl) && DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl)))
+    return;
+
+  for (i = 0; i < NUM_FGID; i++)
+    if (DECL_NAME (decl) == force_global_identifiers[i])
+      {
+	DECL_CONTEXT (decl) = global_namespace;
+	return;
+      }
+}
diff --git a/gcc/config/sol2-protos.h b/gcc/config/sol2-protos.h
--- a/gcc/config/sol2-protos.h
+++ b/gcc/config/sol2-protos.h
@@ -18,9 +18,15 @@  You should have received a copy of the G
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
-extern void solaris_insert_attributes (tree, tree *);
-extern void solaris_register_pragmas (void);
-extern void solaris_output_init_fini (FILE *, tree);
+/* In sol2.c.  */
 extern void solaris_assemble_visibility (tree, int);
 extern void solaris_elf_asm_comdat_section (const char *, unsigned int, tree);
 extern void solaris_file_end (void);
+extern void solaris_insert_attributes (tree, tree *);
+extern void solaris_output_init_fini (FILE *, tree);
+
+/* In sol2-c.c.  */
+extern void solaris_register_pragmas (void);
+
+/* In sol2-cxx.c.  */
+extern void solaris_cxx_decl_mangling_context (tree);
diff --git a/gcc/config/sol2-stubs.c b/gcc/config/sol2-stubs.c
new file mode 100644
--- /dev/null
+++ b/gcc/config/sol2-stubs.c
@@ -0,0 +1,33 @@ 
+/* Stubs for C++ specific Solaris system support.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "tm.h"
+#include "tm_p.h"
+
+/* Stub implemenation of TARGET_CXX_DECL_MANGLING_CONTEXT for non-C++
+   frontends.  */
+void
+solaris_cxx_decl_mangling_context (tree decl ATTRIBUTE_UNUSED)
+{
+  return;
+}
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -217,6 +217,8 @@  along with GCC; see the file COPYING3.  
 /* Allow macro expansion in #pragma pack.  */
 #define HANDLE_PRAGMA_PACK_WITH_EXPANSION
 
+#define TARGET_CXX_DECL_MANGLING_CONTEXT solaris_cxx_decl_mangling_context
+
 /* Solaris/x86 as and gas support unquoted section names.  */
 #define SECTION_NAME_FORMAT	"%s"
 
diff --git a/gcc/config/t-sol2 b/gcc/config/t-sol2
--- a/gcc/config/t-sol2
+++ b/gcc/config/t-sol2
@@ -20,14 +20,22 @@ 
 sol2-c.o: $(srcdir)/config/sol2-c.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
   tree.h c-family/c-format.h $(C_PRAGMA_H) $(C_COMMON_H) $(CPPLIB_H) \
   intl.h $(TM_H) $(TM_P_H)
-	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
-	  $(srcdir)/config/sol2-c.c
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
+
+# Solaris-specific C++ mangling.
+sol2-cxx.o: $(srcdir)/config/sol2-cxx.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
+  tree.h cp/cp-tree.h $(TM_H) $(TM_P_H)
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
+
+# Corresponding stub routines.
+sol2-stubs.o: $(srcdir)/config/sol2-stubs.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
+  tree.h $(TM_H) $(TM_P_H)
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
 
 # Solaris-specific attributes
 sol2.o: $(srcdir)/config/sol2.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
   tree.h output.h $(TM_H) $(TARGET_H) $(TM_P_H) $(GGC_H)
-	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
-	  $(srcdir)/config/sol2.c
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
 
 # This is required by gcc/ada/gcc-interface/Makefile.in.
 TARGET_LIBGCC2_CFLAGS = -fPIC
diff --git a/gcc/cp/mangle.c b/gcc/cp/mangle.c
--- a/gcc/cp/mangle.c
+++ b/gcc/cp/mangle.c
@@ -747,6 +747,8 @@  write_encoding (const tree decl)
 static tree
 decl_mangling_context (tree decl)
 {
+  targetm.cxx.decl_mangling_context (decl);
+
   if (TREE_CODE (decl) == TYPE_DECL
       && LAMBDA_TYPE_P (TREE_TYPE (decl)))
     {
@@ -857,7 +859,7 @@  write_name (tree decl, const int ignore_
 static void
 write_unscoped_name (const tree decl)
 {
-  tree context = CP_DECL_CONTEXT (decl);
+  tree context = decl_mangling_context (decl);
 
   MANGLE_TRACE_TREE ("unscoped-name", decl);
 
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -10173,6 +10173,10 @@  unloaded. The default is to return false
 @var{type} is a C++ class (i.e., RECORD_TYPE or UNION_TYPE) that has just been defined.  Use this hook to make adjustments to the class (eg, tweak visibility or perform any other required target modifications).
 @end deftypefn
 
+@deftypefn {Target Hook} void TARGET_CXX_DECL_MANGLING_CONTEXT (tree @var{decl})
+Perform target-specific modifications to the mangling context of @var{decl}.
+@end deftypefn
+
 @node Named Address Spaces
 @section Adding support for named address spaces
 @cindex named address spaces
diff --git a/gcc/target.def b/gcc/target.def
--- a/gcc/target.def
+++ b/gcc/target.def
@@ -2451,6 +2451,12 @@  DEFHOOK
  void, (tree type),
  hook_void_tree)
 
+DEFHOOK
+(decl_mangling_context,
+ "Perform target-specific modifications to the mangling context of @var{decl}.",
+ void, (tree decl),
+ hook_void_tree)
+
 HOOK_VECTOR_END (cxx)
 
 /* Functions and data for emulated TLS support.  */