bug-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Destructor miscompilation with C++ variants?


From: Michael Catanzaro
Subject: Destructor miscompilation with C++ variants?
Date: Sun, 28 Sep 2014 12:54:15 -0500

Hi,

With Bison 3.0.2, the following trivial grammar using the lalr1.cc
skeleton and variants for the semantic types compiles fine only if the
destructor is commented out. If the destructor is present, I get the
following error when running g++:

In file included from test.tab.cc:46:0:
test.yy: In destructor
‘yy::parser::basic_symbol<Base>::~basic_symbol()’:
test.yy:8:18: error: ‘yysym’ was not declared in this scope
 %destructor { delete $$; } nonterminal

Here is the grammar:

// test.yy
//
// Compile: bison test.yy
//          g++ test.tab.cc

%skeleton "lalr1.cc"
%define api.token.constructor
%define api.value.type variant
%defines

%token TERMINAL
%type <int*> nonterminal
%destructor { delete $$; } nonterminal

%code
{
    yy::parser::symbol_type yylex()
    {
        return yy::parser::make_TERMINAL();
    }
}

%%
nonterminal: TERMINAL { $$ = new int; }

%%
void yy::parser::error(const std::string&) {}
int main() {}
// End of file

Here is the destructor that is generated:

  template <typename Base>
  inline
  parser::basic_symbol<Base>::~basic_symbol ()
  {
    // User destructor.
    symbol_number_type yytype = this->type_get ();
    switch (yytype)
    {
      case 5: // nonterminal

#line 8 "test.yy" // lalr1.cc:372
        { delete yysym.value.template as< int* > (); }
#line 713 "test.tab.hh" // lalr1.cc:372
        break;

   default:
      break;
    }

    // Type destructor.
    switch (yytype)
    {
      case 5: // nonterminal
        value.template destroy< int* > ();
        break;

      default:
        break;
    }

  }

Is this a bug? At first I thought that destructors may not be supported
with variants, but section 10.1.2.2 of the manual [1] implies otherwise
when it says "variants also typically simplify Bison printers and
destructors." Destructors are important for C++98 users as otherwise
there's no reasonable way to use polymorphic types as semantic values
without leaking memory. (C++11 users would use a shared pointer
instead.)

Thanks,

Michael

[1]
https://www.gnu.org/software/bison/manual/html_node/C_002b_002b-Variants.html

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]