]> git.saurik.com Git - bison.git/blobdiff - data/lalr1-fusion.cc
Destroy the variants that remain on the stack in case of error.
[bison.git] / data / lalr1-fusion.cc
index 7ae8ce843bad73e269f2c913f495912733c1f114..20f1cb8cbc16f7ce834626fed38eb5a6e6c84a43 100644 (file)
@@ -40,6 +40,12 @@ b4_variant_if([
 ]) # b4_variant_if
 
 
+# b4_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
+# ----------------------------------------------------
+m4_define([b4_assert_if],
+[b4_percent_define_ifdef([[assert]], [$1], [$2])])
+
+
 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
 # --------------------------------------
 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
@@ -52,7 +58,7 @@ m4_define([b4_rhs_value],
 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
 # on RHS.
 m4_define([b4_rhs_location],
-[(yystack_@{($1) - ($2)@}.location)])
+[yystack_@{($1) - ($2)@}.location])
 
 
 # b4_symbol_actions(FILENAME, LINENO,
@@ -85,13 +91,13 @@ m4_define([b4_symbol_action_],
 ])])
 
 
-# b4_symbol_variant(YYTYPE, YYVAL, ACTION)
-# ----------------------------------------
+# b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
+# ------------------------------------------------
 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
 # YYTYPE.
 m4_define([b4_symbol_variant],
 [m4_pushdef([b4_dollar_dollar],
-            [$2.$3<$][3>()])dnl
+            [$2.$3<$][3>(m4_shift3($@))])dnl
   switch ($1)
     {
 m4_map([b4_symbol_action_], m4_defn([b4_type_names]))
@@ -132,6 +138,9 @@ m4_define([b4_char_sizeof],
 ])
 
 
+m4_pushdef([b4_copyright_years],
+           [2002, 2003, 2004, 2005, 2006, 2007, 2008])
+
 m4_define([b4_parser_class_name],
           [b4_percent_define_get([[parser_class_name]])])
 
@@ -147,9 +156,8 @@ m4_include(b4_pkgdatadir/[location.cc])
 m4_changecom()
 m4_divert_push(0)dnl
 b4_defines_if(
-[@output(b4_spec_defines_file@)
-b4_copyright([Skeleton interface for Bison LALR(1) parsers in C++],
-             [2002, 2003, 2004, 2005, 2006, 2007, 2008])
+[@output(b4_spec_defines_file@)@
+b4_copyright([Skeleton interface for Bison LALR(1) parsers in C++])
 dnl FIXME: This is wrong, we want computed header guards.
 [
 /* C++ LALR(1) parser skeleton written by Akim Demaille.  */
@@ -159,6 +167,7 @@ dnl FIXME: This is wrong, we want computed header guards.
 
 ]b4_percent_code_get([[requires]])[
 
+]b4_assert_if([#include <cassert>])[
 #include <string>
 #include <iostream>
 #include "stack.hh"
@@ -175,28 +184,31 @@ dnl FIXME: This is wrong, we want computed header guards.
   /// via the current state.
   template <size_t S>
   struct variant
-  {
+  {]b4_assert_if([
+    /// Whether something is contained.
+    bool built;
+
+    /// Initially uninitialized.
+    variant ()
+      : built(false)
+    {}])[
+
     /// Instantiate a \a T in here.
     template <typename T>
     inline T&
     build()
-    {
+    {]b4_assert_if([
+      assert(!built);
+      built = true;])[
       return *new (buffer) T;
     }
 
-    /// Destroy the stored \a T.
-    template <typename T>
-    inline void
-    destroy()
-    {
-      reinterpret_cast<T&>(buffer).~T();
-    }
-
     /// Accessor to a built \a T.
     template <typename T>
     inline T&
     as()
-    {
+    {]b4_assert_if([
+      assert(built);])[
       return reinterpret_cast<T&>(buffer);
     }
 
@@ -204,10 +216,39 @@ dnl FIXME: This is wrong, we want computed header guards.
     template <typename T>
     inline const T&
     as() const
-    {
+    {]b4_assert_if([
+      assert(built);])[
       return reinterpret_cast<const T&>(buffer);
     }
 
+    /// Swap the content with \a other.
+    template <typename T>
+    inline void
+    swap(variant<S>& other)
+    {
+      std::swap(as<T>(), other.as<T>());
+    }
+
+    /// Assign the content of \a other to this.
+    /// Destroys \a other.
+    template <typename T>
+    inline void
+    build(variant<S>& other)
+    {
+      build<T>();
+      swap<T>(other);
+      other.destroy<T>();
+    }
+
+    /// Destroy the stored \a T.
+    template <typename T>
+    inline void
+    destroy()
+    {
+      as<T>().~T();]b4_assert_if([
+      built = false;])[
+    }
+
     /// A buffer large enough to store any of the semantic values.
     char buffer[S];
   };
@@ -386,10 +427,8 @@ m4_ifdef([b4_stype],
 #endif
 
 #if YYDEBUG
-    /// A type to store symbol numbers and -1.
-    typedef ]b4_int_type_for([b4_rhs])[ rhs_number_type;
     /// A `-1'-separated list of the rules' RHS.
-    static const rhs_number_type yyrhs_[];
+    static const ]b4_int_type_for([b4_rhs])[ yyrhs_[];
     /// For each rule, the index of the first RHS symbol in \a yyrhs_.
     static const ]b4_int_type_for([b4_prhs])[ yyprhs_[];
     /// For each rule, its source line number.
@@ -411,6 +450,7 @@ m4_ifdef([b4_stype],
 
     /// \brief Reclaim the memory associated to a symbol.
     /// \param yymsg        Why this token is reclaimed.
+    ///                     If null, nothing is printed at all.
     /// \param yytype       The symbol type.
     /// \param yyvalue      Its semantic value.
     /// \param yylocation   Its location.
@@ -480,9 +520,8 @@ b4_percent_code_get([[provides]])[]dnl
 
 [#endif /* ! defined PARSER_HEADER_H */]
 ])dnl
-@output(b4_parser_file_name@)
-b4_copyright([Skeleton implementation for Bison LALR(1) parsers in C++],
-             [2002, 2003, 2004, 2005, 2006, 2007, 2008])
+@output(b4_parser_file_name@)@
+b4_copyright([Skeleton implementation for Bison LALR(1) parsers in C++])
 b4_percent_code_get([[top]])[]dnl
 m4_if(b4_prefix, [yy], [],
 [
@@ -521,26 +560,26 @@ b4_percent_code_get[]dnl
 # define YYCDEBUG if (yydebug_) (*yycdebug_)
 
 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
-do {                                                   \
-  if (yydebug_)                                                \
+  do {                                                 \
+    if (yydebug_)                                       \
     {                                                  \
       *yycdebug_ << Title << ' ';                      \
       yy_symbol_print_ ((Type), (Value), (Location));  \
       *yycdebug_ << std::endl;                         \
     }                                                  \
-} while (false)
+  } while (false)
 
 # define YY_REDUCE_PRINT(Rule)         \
-do {                                   \
-  if (yydebug_)                                \
-    yy_reduce_print_ (Rule);           \
-} while (false)
+  do {                                 \
+    if (yydebug_)                       \
+      yy_reduce_print_ (Rule);         \
+  } while (false)
 
 # define YY_STACK_PRINT()              \
-do {                                   \
-  if (yydebug_)                                \
-    yystack_print_ ();                 \
-} while (false)
+  do {                                 \
+    if (yydebug_)                       \
+      yystack_print_ ();                \
+  } while (false)
 
 #else /* !YYDEBUG */
 
@@ -655,14 +694,19 @@ do {                                      \
     YYUSE (yyvalue);
     YYUSE (yylocation);
 
-    YY_SYMBOL_PRINT (yymsg, yytype, yyvalue, yylocation);
+    if (yymsg)
+      YY_SYMBOL_PRINT (yymsg, yytype, yyvalue, yylocation);
 
+    // User destructor.
     switch (yytype)
       {
   ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
        default:
          break;
-      }
+      }]b4_variant_if([
+
+    // Type destructor.
+  b4_symbol_variant([[yytype]], [[yyvalue]], [[destroy]])])[
   }
 
   ]b4_parser_class_name[::data_type::data_type ()
@@ -684,7 +728,10 @@ do {                                       \
   ]b4_parser_class_name[::yypush_ (state_type s,
                            semantic_type& v, const location_type& l)
   {
-    yystack_.push (data_type (s, v, l));
+]b4_variant_if(
+[[    yystack_.push (data_type (s, semantic_type(), l));
+    ]b4_symbol_variant([[yystos_[s]]], [[yystack_[0].value]], [build], [v])],
+[    yystack_.push (data_type (s, v, l));])[
   }
 
   void
@@ -885,16 +932,21 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[;
           break;
       }
     YY_SYMBOL_PRINT ("-> $$ =", yyr1_[yyn], yyval, yyloc);
-
-]b4_variant_if([
+]b4_variant_if([[
     // Destroy the lhs symbols.
     for (int i = 0; i < yylen; ++i)
-      {
-        b4_symbol_variant([[yystos_[yystack_@{i@}.state]]],
-                          [[yystack_@{i@}.value]],
-                          [[destroy]])
-      }])dnl
-[
+      // Destroy a variant which value may have be swapped with yyval.
+      // The value of yyval (hence maybe one of these lhs symbols)
+      // depends on what does the default contruction for this type.
+      // In the case of pointers for instance, nothing is done, so the
+      // value is junk.  Therefore do not try to report the content in
+      // the debug trace, it's junk.  Hence yymsg = 0.  Besides, that
+      // keeps exactly the same traces as with the other Bison
+      // skeletons.
+      yydestruct_ (0,
+                   yystos_[yystack_[i].state],
+                   yystack_[i].value, yystack_[i].location);]])[
+
     yypop_ (yylen);
     yylen = 0;
     YY_STACK_PRINT ();
@@ -929,9 +981,9 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[;
 
        if (yychar <= yyeof_)
          {
-         /* Return failure if at end of input.  */
-         if (yychar == yyeof_)
-           YYABORT;
+            /* Return failure if at end of input.  */
+            if (yychar == yyeof_)
+              YYABORT;
          }
        else
          {
@@ -1142,11 +1194,7 @@ b4_error_verbose_if([ tok])[)
 
 #if YYDEBUG
   /* YYRHS -- A `-1'-separated list of the rules' RHS.  */
-  const ]b4_parser_class_name[::rhs_number_type
-  ]b4_parser_class_name[::yyrhs_[] =
-  {
-  ]b4_rhs[
-  };
+  ]b4_table_define([rhs], [b4_rhs])[;
 
   /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
      YYRHS.  */
@@ -1216,11 +1264,9 @@ b4_error_verbose_if([ tok])[)
 
 ]b4_namespace_close[
 
-]b4_epilogue
-dnl
-@output(b4_dir_prefix[]stack.hh@)
-b4_copyright([Stack handling for Bison parsers in C++],
-             [2002, 2003, 2004, 2005, 2006, 2007, 2008])[
+]b4_epilogue[]dnl
+@output(b4_dir_prefix[]stack.hh@)@
+b4_copyright([Stack handling for Bison parsers in C++])[
 
 #ifndef BISON_STACK_HH
 # define BISON_STACK_HH
@@ -1318,3 +1364,4 @@ b4_copyright([Stack handling for Bison parsers in C++],
 #endif // not BISON_STACK_HH[]dnl
 ]
 m4_divert_pop(0)
+m4_popdef([b4_copyright_years])dnl