Bug 37862 - Parenthesised indirection alters class member access
Summary: Parenthesised indirection alters class member access
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2008-10-17 08:52 UTC by Nick Clifton
Modified: 2009-02-10 23:11 UTC (History)
4 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2008-10-18 11:11:07


Attachments
Allow postfix parser to pass cp_id_kind information back to the primary parser (4.75 KB, patch)
2008-11-10 13:36 UTC, Nick Clifton
Details | Diff
Testcase for the bug (312 bytes, patch)
2008-11-10 16:22 UTC, Nick Clifton
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Clifton 2008-10-17 08:52:52 UTC
Hi Guys,

Compiling and then executing this program:
#include <stdio.h>

class A {
public:
 virtual void get() { printf ("A\n"); }
};
class B:public A {
public:
 virtual void get() { printf ("B\n"); }
};
class C:public B {
};
int main (void)
{
 C c;
 C* p = &c;

 p->A::get();
 (p->A::get)();

 return 0;
}

Results in:

  A
  B

Being displayed, rather than:

  A
  A

As far as I can tell the parentheses around the class member access should not change anything.

Cheers
  Nick
Comment 1 Richard Biener 2008-10-18 11:11:07 UTC
Confirmed.
Comment 2 Nick Clifton 2008-11-10 13:36:14 UTC
Created attachment 16644 [details]
Allow postfix parser to pass cp_id_kind information back to the primary parser
Comment 3 Nick Clifton 2008-11-10 13:49:30 UTC
Hi Guys,

  I have uploaded a potential patch for the problem.  It fixes the testcase originally provided and does not introduce any regressions into the g++ testsuite for an i686-pc-linux-gnu toolchain.  

That's the good news.  The bad news is that I am not sure if the patch will be acceptable since I am not a C++ expert.  The problem I believe is that the status of the cp_id_kind enum which is computed by cp_parser_postfix_dot_deref_expression() when it is parsing "A::get" is not passed back to cp_parse_primary_expression() which is currently parsing "(p->A::get)".  So it goes with its default value, which allows overloading of virtual functions, and so the wrong member function is selected.

The patch attempts to fix this problem by allowing the cp_id_kind enum computed in cp_parser_postfix_dot_deref_expression to be passes back, via a long chain of intermediary functions, to cp_parser_primary_expression.  My concern is that I am not familiar enough with the C++ parser to tell if the patch breaks some other parser requirement.  (One that is not tested by the g++ testsuite).

So - is this patch acceptable ?

Cheers
  Nick
Comment 4 Nick Clifton 2008-11-10 16:22:03 UTC
Created attachment 16645 [details]
Testcase for the bug
Comment 5 Nick Clifton 2008-11-10 16:22:46 UTC
Oops - almost forgot - the bug needs a testcase for the g++ testsuite, so I have uploaded a patch to add that as well.

Cheers
  Nick
Comment 6 Nick Clifton 2009-01-14 13:00:34 UTC
Subject: Bug 37862

Author: nickc
Date: Wed Jan 14 13:00:21 2009
New Revision: 143369

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=143369
Log:
        PR c++/37862
        * parser.c: Pass cp_id_kind computed in
        cp_parser_postfix_dot_deref_expression to
        cp_parser_primary_expression.

        * g++.cp/parse/pr37862.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/parse/pr37862.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/parser.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Jason Merrill 2009-02-10 23:11:46 UTC
Fixed.