Bug 9043 - [ABI] incorrect name mangling for array dimension in template function
Summary: [ABI] incorrect name mangling for array dimension in template function
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: 3.4.0
Assignee: Nathan Sidwell
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-12-23 09:36 UTC by catherin
Modified: 2003-12-16 19:50 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2003-11-09 00:12:20


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description catherin 2002-12-23 09:36:01 UTC
The C++ ABI for name mangling has 2 production rules for mangling
array dimensions:

quoting from the ABI:

Array types encode the dimension (number of elements) and the element type. Note that "array" parameters to functions are encoded as pointer types. For variable length arrays (C99 VLAs), the dimension (but not the '_' separator) is omitted. 

  <array-type> ::= A <positive dimension number> _ <element type>
               ::= A [<dimension expression>] _ <element type>

When the dimension is an expression involving template parameters, the second production is used. Thus, the declarations: 

    template<int I> void foo (int (&)[I + 1]) { }
    template void foo<2> (int (&)[3]);

produce the mangled name "_Z3fooILi2EEvRAplT_Li1E_i". 

gcc mangles the name in the example the same way... however 
for this testcase:

// ------------- abc.cpp ------------

template <int I> void f36(int (*)[2]) {}

static const int I=1;

template void f36<I>(int (*)[2]);

void (*g36)(int (*)[2]) = &f36<I>;

// ---------------------------------

gcc mangles the name as follows

_Z3f36ILi1EEvPALi2E_i

The array dimension '2' has been mangled as a literal expression 'Li2E'
So gcc has taken the 2nd production for the array dimension

If I'm interpreting the standard correctly, this is not
correct.  The array dimension should only be encoded as
an expression if it references the template arguments

The correct mangled name should be:
_Z3f36ILi1EEvPA2_i

(taking the first production instead of the 2nd one)

Release:
gcc 3.2

Environment:
Reading specs from /usr/lib/gcc-lib/powerpc-suse-linux/3.2/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --enable-languages=c,c++,f77,objc,java,ada --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit powerpc-suse-linux
Thread model: posix
gcc version 3.2

How-To-Repeat:
g++ -c abc.cpp
nm abc.o
Comment 1 Andrew Pinski 2003-05-25 20:18:41 UTC
Is this true with 3.3 and -fabi-version=0?
Comment 2 Andrew Pinski 2003-08-03 20:38:24 UTC
I can confirm this on the mainline (20030803) even with -fab-version=0.

_Z3f36ILi1EEvPALi2E_i
void f36<(int)1>(int (*) [(int)2])

_Z3f36ILi1EEvPA2_i
void f36<(int)1>(int (*) [2])
Comment 3 Nathan Sidwell 2003-12-16 19:49:17 UTC
2003-12-16  Nathan Sidwell  <nathan@codesourcery.com>

	PR c++/9043
	C++ ABI change: Mangling array indices in templates.
	* decl.c (compute_array_index_type): Reorganize for earlier
	template errors. Use value_dependent_expression_p for abi-2.
	* mangle.c (write_array_type): Check broken mangling for
	expression indices on abi-1
Comment 4 GCC Commits 2003-12-16 19:50:58 UTC
Subject: Bug 9043

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	nathan@gcc.gnu.org	2003-12-16 19:50:55

Modified files:
	gcc/cp         : ChangeLog decl.c mangle.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/g++.dg/abi: mangle20-1.C mangle20-2.C 

Log message:
	cp:
	PR c++/9043
	C++ ABI change: Mangling array indices in templates.
	* decl.c (compute_array_index_type): Reorganize for earlier
	template errors. Use value_dependent_expression_p for abi-2.
	* mangle.c (write_array_type): Check broken mangling for
	expression indices on abi-1
	testsuite:
	PR c++/9043
	* g++.dg/abi/mangle20-1.C: New test.
	* g++.dg/abi/mangle20-2.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3813&r2=1.3814
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&r1=1.1161&r2=1.1162
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/mangle.c.diff?cvsroot=gcc&r1=1.93&r2=1.94
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3270&r2=1.3271
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/abi/mangle20-1.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/abi/mangle20-2.C.diff?cvsroot=gcc&r1=NONE&r2=1.1