variant.hh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. # C++ skeleton for Bison
  2. # Copyright (C) 2002-2015, 2018-2021 Free Software Foundation, Inc.
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ## --------- ##
  16. ## variant. ##
  17. ## --------- ##
  18. # b4_assert
  19. # ---------
  20. # The name of YY_ASSERT.
  21. m4_define([b4_assert],
  22. [b4_api_PREFIX[]_ASSERT])
  23. # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
  24. # ------------------------------------------------
  25. # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
  26. # YYTYPE.
  27. m4_define([b4_symbol_variant],
  28. [m4_pushdef([b4_dollar_dollar],
  29. [$2.$3< $][3 > (m4_shift3($@))])dnl
  30. switch ($1)
  31. {
  32. b4_type_foreach([_b4_type_action])[]dnl
  33. default:
  34. break;
  35. }
  36. m4_popdef([b4_dollar_dollar])dnl
  37. ])
  38. # _b4_char_sizeof_counter
  39. # -----------------------
  40. # A counter used by _b4_char_sizeof_dummy to create fresh symbols.
  41. m4_define([_b4_char_sizeof_counter],
  42. [0])
  43. # _b4_char_sizeof_dummy
  44. # ---------------------
  45. # At each call return a new C++ identifier.
  46. m4_define([_b4_char_sizeof_dummy],
  47. [m4_define([_b4_char_sizeof_counter], m4_incr(_b4_char_sizeof_counter))dnl
  48. dummy[]_b4_char_sizeof_counter])
  49. # b4_char_sizeof(SYMBOL-NUMS)
  50. # ---------------------------
  51. # To be mapped on the list of type names to produce:
  52. #
  53. # char dummy1[sizeof (type_name_1)];
  54. # char dummy2[sizeof (type_name_2)];
  55. #
  56. # for defined type names.
  57. m4_define([b4_char_sizeof],
  58. [b4_symbol_if([$1], [has_type],
  59. [
  60. m4_map([ b4_symbol_tag_comment], [$@])dnl
  61. char _b4_char_sizeof_dummy@{sizeof (b4_symbol([$1], [type]))@};
  62. ])])
  63. # b4_variant_includes
  64. # -------------------
  65. # The needed includes for variants support.
  66. m4_define([b4_variant_includes],
  67. [b4_parse_assert_if([[#include <typeinfo>
  68. #ifndef ]b4_assert[
  69. # include <cassert>
  70. # define ]b4_assert[ assert
  71. #endif
  72. ]])])
  73. ## -------------------------- ##
  74. ## Adjustments for variants. ##
  75. ## -------------------------- ##
  76. # b4_value_type_declare
  77. # ---------------------
  78. # Define semantic_type.
  79. m4_define([b4_value_type_declare],
  80. [[ /// A buffer to store and retrieve objects.
  81. ///
  82. /// Sort of a variant, but does not keep track of the nature
  83. /// of the stored data, since that knowledge is available
  84. /// via the current parser state.
  85. class semantic_type
  86. {
  87. public:
  88. /// Type of *this.
  89. typedef semantic_type self_type;
  90. /// Empty construction.
  91. semantic_type () YY_NOEXCEPT
  92. : yybuffer_ ()]b4_parse_assert_if([
  93. , yytypeid_ (YY_NULLPTR)])[
  94. {}
  95. /// Construct and fill.
  96. template <typename T>
  97. semantic_type (YY_RVREF (T) t)]b4_parse_assert_if([
  98. : yytypeid_ (&typeid (T))])[
  99. {]b4_parse_assert_if([[
  100. ]b4_assert[ (sizeof (T) <= size);]])[
  101. new (yyas_<T> ()) T (YY_MOVE (t));
  102. }
  103. #if 201103L <= YY_CPLUSPLUS
  104. /// Non copyable.
  105. semantic_type (const self_type&) = delete;
  106. /// Non copyable.
  107. self_type& operator= (const self_type&) = delete;
  108. #endif
  109. /// Destruction, allowed only if empty.
  110. ~semantic_type () YY_NOEXCEPT
  111. {]b4_parse_assert_if([
  112. ]b4_assert[ (!yytypeid_);
  113. ])[}
  114. # if 201103L <= YY_CPLUSPLUS
  115. /// Instantiate a \a T in here from \a t.
  116. template <typename T, typename... U>
  117. T&
  118. emplace (U&&... u)
  119. {]b4_parse_assert_if([[
  120. ]b4_assert[ (!yytypeid_);
  121. ]b4_assert[ (sizeof (T) <= size);
  122. yytypeid_ = & typeid (T);]])[
  123. return *new (yyas_<T> ()) T (std::forward <U>(u)...);
  124. }
  125. # else
  126. /// Instantiate an empty \a T in here.
  127. template <typename T>
  128. T&
  129. emplace ()
  130. {]b4_parse_assert_if([[
  131. ]b4_assert[ (!yytypeid_);
  132. ]b4_assert[ (sizeof (T) <= size);
  133. yytypeid_ = & typeid (T);]])[
  134. return *new (yyas_<T> ()) T ();
  135. }
  136. /// Instantiate a \a T in here from \a t.
  137. template <typename T>
  138. T&
  139. emplace (const T& t)
  140. {]b4_parse_assert_if([[
  141. ]b4_assert[ (!yytypeid_);
  142. ]b4_assert[ (sizeof (T) <= size);
  143. yytypeid_ = & typeid (T);]])[
  144. return *new (yyas_<T> ()) T (t);
  145. }
  146. # endif
  147. /// Instantiate an empty \a T in here.
  148. /// Obsolete, use emplace.
  149. template <typename T>
  150. T&
  151. build ()
  152. {
  153. return emplace<T> ();
  154. }
  155. /// Instantiate a \a T in here from \a t.
  156. /// Obsolete, use emplace.
  157. template <typename T>
  158. T&
  159. build (const T& t)
  160. {
  161. return emplace<T> (t);
  162. }
  163. /// Accessor to a built \a T.
  164. template <typename T>
  165. T&
  166. as () YY_NOEXCEPT
  167. {]b4_parse_assert_if([[
  168. ]b4_assert[ (yytypeid_);
  169. ]b4_assert[ (*yytypeid_ == typeid (T));
  170. ]b4_assert[ (sizeof (T) <= size);]])[
  171. return *yyas_<T> ();
  172. }
  173. /// Const accessor to a built \a T (for %printer).
  174. template <typename T>
  175. const T&
  176. as () const YY_NOEXCEPT
  177. {]b4_parse_assert_if([[
  178. ]b4_assert[ (yytypeid_);
  179. ]b4_assert[ (*yytypeid_ == typeid (T));
  180. ]b4_assert[ (sizeof (T) <= size);]])[
  181. return *yyas_<T> ();
  182. }
  183. /// Swap the content with \a that, of same type.
  184. ///
  185. /// Both variants must be built beforehand, because swapping the actual
  186. /// data requires reading it (with as()), and this is not possible on
  187. /// unconstructed variants: it would require some dynamic testing, which
  188. /// should not be the variant's responsibility.
  189. /// Swapping between built and (possibly) non-built is done with
  190. /// self_type::move ().
  191. template <typename T>
  192. void
  193. swap (self_type& that) YY_NOEXCEPT
  194. {]b4_parse_assert_if([[
  195. ]b4_assert[ (yytypeid_);
  196. ]b4_assert[ (*yytypeid_ == *that.yytypeid_);]])[
  197. std::swap (as<T> (), that.as<T> ());
  198. }
  199. /// Move the content of \a that to this.
  200. ///
  201. /// Destroys \a that.
  202. template <typename T>
  203. void
  204. move (self_type& that)
  205. {
  206. # if 201103L <= YY_CPLUSPLUS
  207. emplace<T> (std::move (that.as<T> ()));
  208. # else
  209. emplace<T> ();
  210. swap<T> (that);
  211. # endif
  212. that.destroy<T> ();
  213. }
  214. # if 201103L <= YY_CPLUSPLUS
  215. /// Move the content of \a that to this.
  216. template <typename T>
  217. void
  218. move (self_type&& that)
  219. {
  220. emplace<T> (std::move (that.as<T> ()));
  221. that.destroy<T> ();
  222. }
  223. #endif
  224. /// Copy the content of \a that to this.
  225. template <typename T>
  226. void
  227. copy (const self_type& that)
  228. {
  229. emplace<T> (that.as<T> ());
  230. }
  231. /// Destroy the stored \a T.
  232. template <typename T>
  233. void
  234. destroy ()
  235. {
  236. as<T> ().~T ();]b4_parse_assert_if([
  237. yytypeid_ = YY_NULLPTR;])[
  238. }
  239. private:
  240. #if YY_CPLUSPLUS < 201103L
  241. /// Non copyable.
  242. semantic_type (const self_type&);
  243. /// Non copyable.
  244. self_type& operator= (const self_type&);
  245. #endif
  246. /// Accessor to raw memory as \a T.
  247. template <typename T>
  248. T*
  249. yyas_ () YY_NOEXCEPT
  250. {
  251. void *yyp = yybuffer_.yyraw;
  252. return static_cast<T*> (yyp);
  253. }
  254. /// Const accessor to raw memory as \a T.
  255. template <typename T>
  256. const T*
  257. yyas_ () const YY_NOEXCEPT
  258. {
  259. const void *yyp = yybuffer_.yyraw;
  260. return static_cast<const T*> (yyp);
  261. }
  262. /// An auxiliary type to compute the largest semantic type.
  263. union union_type
  264. {]b4_type_foreach([b4_char_sizeof])[ };
  265. /// The size of the largest semantic type.
  266. enum { size = sizeof (union_type) };
  267. /// A buffer to store semantic values.
  268. union
  269. {
  270. /// Strongest alignment constraints.
  271. long double yyalign_me;
  272. /// A buffer large enough to store any of the semantic values.
  273. char yyraw[size];
  274. } yybuffer_;]b4_parse_assert_if([
  275. /// Whether the content is built: if defined, the name of the stored type.
  276. const std::type_info *yytypeid_;])[
  277. };
  278. ]])
  279. # How the semantic value is extracted when using variants.
  280. # b4_symbol_value(VAL, SYMBOL-NUM, [TYPE])
  281. # ----------------------------------------
  282. # See README.
  283. m4_define([b4_symbol_value],
  284. [m4_ifval([$3],
  285. [$1.as< $3 > ()],
  286. [m4_ifval([$2],
  287. [b4_symbol_if([$2], [has_type],
  288. [$1.as < b4_symbol([$2], [type]) > ()],
  289. [$1])],
  290. [$1])])])
  291. # b4_symbol_value_template(VAL, SYMBOL-NUM, [TYPE])
  292. # -------------------------------------------------
  293. # Same as b4_symbol_value, but used in a template method.
  294. m4_define([b4_symbol_value_template],
  295. [m4_ifval([$3],
  296. [$1.template as< $3 > ()],
  297. [m4_ifval([$2],
  298. [b4_symbol_if([$2], [has_type],
  299. [$1.template as < b4_symbol([$2], [type]) > ()],
  300. [$1])],
  301. [$1])])])
  302. ## ------------- ##
  303. ## make_SYMBOL. ##
  304. ## ------------- ##
  305. # _b4_includes_tokens(SYMBOL-NUM...)
  306. # ----------------------------------
  307. # Expands to non-empty iff one of the SYMBOL-NUM denotes
  308. # a token.
  309. m4_define([_b4_is_token],
  310. [b4_symbol_if([$1], [is_token], [1])])
  311. m4_define([_b4_includes_tokens],
  312. [m4_map([_b4_is_token], [$@])])
  313. # _b4_token_maker_define(SYMBOL-NUM)
  314. # ----------------------------------
  315. # Declare make_SYMBOL for SYMBOL-NUM. Use at class-level.
  316. m4_define([_b4_token_maker_define],
  317. [b4_token_visible_if([$1],
  318. [#if 201103L <= YY_CPLUSPLUS
  319. static
  320. symbol_type
  321. make_[]_b4_symbol([$1], [id]) (b4_join(
  322. b4_symbol_if([$1], [has_type],
  323. [b4_symbol([$1], [type]) v]),
  324. b4_locations_if([location_type l])))
  325. {
  326. return symbol_type (b4_join([token::b4_symbol([$1], [id])],
  327. b4_symbol_if([$1], [has_type], [std::move (v)]),
  328. b4_locations_if([std::move (l)])));
  329. }
  330. #else
  331. static
  332. symbol_type
  333. make_[]_b4_symbol([$1], [id]) (b4_join(
  334. b4_symbol_if([$1], [has_type],
  335. [const b4_symbol([$1], [type])& v]),
  336. b4_locations_if([const location_type& l])))
  337. {
  338. return symbol_type (b4_join([token::b4_symbol([$1], [id])],
  339. b4_symbol_if([$1], [has_type], [v]),
  340. b4_locations_if([l])));
  341. }
  342. #endif
  343. ])])
  344. # b4_token_kind(SYMBOL-NUM)
  345. # -------------------------
  346. # Some tokens don't have an ID.
  347. m4_define([b4_token_kind],
  348. [b4_symbol_if([$1], [has_id],
  349. [token::b4_symbol([$1], [id])],
  350. [b4_symbol([$1], [code])])])
  351. # _b4_tok_in(SYMBOL-NUM, ...)
  352. # ---------------------------
  353. # See b4_tok_in below. The SYMBOL-NUMs... are tokens only.
  354. #
  355. # We iterate over the tokens to group them by "range" of token numbers (not
  356. # symbols numbers!).
  357. #
  358. # b4_fst is the start of that range.
  359. # b4_prev is the previous value.
  360. # b4_val is the current value.
  361. # If b4_val is the successor of b4_prev in token numbers, update the latter,
  362. # otherwise emit the code for range b4_fst .. b4_prev.
  363. # $1 is also used as a terminator in the foreach, but it will not be printed.
  364. #
  365. m4_define([_b4_tok_in],
  366. [m4_pushdef([b4_prev], [$1])dnl
  367. m4_pushdef([b4_fst], [$1])dnl
  368. m4_pushdef([b4_sep], [])dnl
  369. m4_foreach([b4_val], m4_dquote(m4_shift($@, $1)),
  370. [m4_if(b4_symbol(b4_val, [code]), m4_eval(b4_symbol(b4_prev, [code]) + 1), [],
  371. [b4_sep[]m4_if(b4_fst, b4_prev,
  372. [tok == b4_token_kind(b4_fst)],
  373. [(b4_token_kind(b4_fst) <= tok && tok <= b4_token_kind(b4_prev))])[]dnl
  374. m4_define([b4_fst], b4_val)dnl
  375. m4_define([b4_sep], [
  376. || ])])dnl
  377. m4_define([b4_prev], b4_val)])dnl
  378. m4_popdef([b4_sep])dnl
  379. m4_popdef([b4_fst])dnl
  380. m4_popdef([b4_prev])dnl
  381. ])
  382. # _b4_filter_tokens(SYMBOL-NUM, ...)
  383. # ----------------------------------
  384. # Expand as the list of tokens amongst SYMBOL-NUM.
  385. m4_define([_b4_filter_tokens],
  386. [m4_pushdef([b4_sep])dnl
  387. m4_foreach([b4_val], [$@],
  388. [b4_symbol_if(b4_val, [is_token], [b4_sep[]b4_val[]m4_define([b4_sep], [,])])])dnl
  389. m4_popdef([b4_sep])dnl
  390. ])
  391. # b4_tok_in(SYMBOL-NUM, ...)
  392. # ---------------------------
  393. # A C++ conditional that checks that `tok` is a member of this list of symbol
  394. # numbers.
  395. m4_define([b4_tok_in],
  396. [_$0(_b4_filter_tokens($@))])
  397. # _b4_token_constructor_define(SYMBOL-NUM...)
  398. # -------------------------------------------
  399. # Define a unique make_symbol for all the SYMBOL-NUM (they
  400. # have the same type). Use at class-level.
  401. m4_define([_b4_token_constructor_define],
  402. [m4_ifval(_b4_includes_tokens($@),
  403. [[#if 201103L <= YY_CPLUSPLUS
  404. symbol_type (]b4_join(
  405. [int tok],
  406. b4_symbol_if([$1], [has_type],
  407. [b4_symbol([$1], [type]) v]),
  408. b4_locations_if([location_type l]))[)
  409. : super_type(]b4_join([token_type (tok)],
  410. b4_symbol_if([$1], [has_type], [std::move (v)]),
  411. b4_locations_if([std::move (l)]))[)
  412. #else
  413. symbol_type (]b4_join(
  414. [int tok],
  415. b4_symbol_if([$1], [has_type],
  416. [const b4_symbol([$1], [type])& v]),
  417. b4_locations_if([const location_type& l]))[)
  418. : super_type(]b4_join([token_type (tok)],
  419. b4_symbol_if([$1], [has_type], [v]),
  420. b4_locations_if([l]))[)
  421. #endif
  422. {]b4_parse_assert_if([[
  423. ]b4_assert[ (]b4_tok_in($@)[);
  424. ]])[}
  425. ]])])
  426. # b4_basic_symbol_constructor_define(SYMBOL-NUM)
  427. # ----------------------------------------------
  428. # Generate a constructor for basic_symbol from given type.
  429. m4_define([b4_basic_symbol_constructor_define],
  430. [[#if 201103L <= YY_CPLUSPLUS
  431. basic_symbol (]b4_join(
  432. [typename Base::kind_type t],
  433. b4_symbol_if([$1], [has_type], [b4_symbol([$1], [type])&& v]),
  434. b4_locations_if([location_type&& l]))[)
  435. : Base (t)]b4_symbol_if([$1], [has_type], [
  436. , value (std::move (v))])[]b4_locations_if([
  437. , location (std::move (l))])[
  438. {}
  439. #else
  440. basic_symbol (]b4_join(
  441. [typename Base::kind_type t],
  442. b4_symbol_if([$1], [has_type], [const b4_symbol([$1], [type])& v]),
  443. b4_locations_if([const location_type& l]))[)
  444. : Base (t)]b4_symbol_if([$1], [has_type], [
  445. , value (v)])[]b4_locations_if([
  446. , location (l)])[
  447. {}
  448. #endif
  449. ]])
  450. # b4_token_constructor_define
  451. # ---------------------------
  452. # Define the overloaded versions of make_symbol for all the value types.
  453. m4_define([b4_token_constructor_define],
  454. [ // Implementation of make_symbol for each symbol type.
  455. b4_symbol_foreach([_b4_token_maker_define])])