1 # Value type. -*- Autotest -*-
3 # Copyright (C) 2013-2015, 2018-2021 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 AT_BANNER([[Value type tests.]])
21 ## ----------------------------------- ##
22 ## %union vs. %define api.value.type. ##
23 ## ----------------------------------- ##
25 AT_SETUP([[%union vs. %define api.value.type]])
28 [[%union { int ival; }
29 %define api.value.type union-directive
34 AT_BISON_CHECK([[input.y]], [[1]], [[]],
35 [[input.y:2.1-38: error: '%union' and '%define api.value.type' cannot be used together
40 ## ---------------------------------------- ##
41 ## %yacc vs. %define api.value.type union. ##
42 ## ---------------------------------------- ##
44 AT_SETUP([[%yacc vs. %define api.value.type union]])
48 %define api.value.type union
53 AT_BISON_CHECK([[input.y]], [[1]], [[]],
54 [[input.y:2.1-28: error: '%yacc' and '%define api.value.type "union"' cannot be used together
60 ## ---------------- ##
62 ## ---------------- ##
64 # _AT_TEST($1: BISON-DIRECTIVES,
65 # $2: MORE-BISON-DIRECTIVES,
71 # --------------------------------------
72 # Compile the grammar and check the expected result.
73 # BISON-DIRECTIVES are passed to AT_SETUP, contrary to MORE-BISON-DIRECTIVES.
75 # Use REQUIREMENT e.g. skip the test if the compiler does not meet the
77 m4_pushdef([_AT_TEST],
80 AT_KEYWORDS([api.value.type])
82 AT_BISON_OPTION_PUSHDEFS([%debug $1 $2])
83 AT_DATA_GRAMMAR([test.y],
103 ]AT_YYLEX_DEFINE([$4], [$5])[
107 AT_LANG_FOR_EACH_STD([
108 AT_GLR2_CC_IF([AT_REQUIRE_CXX_STD(11, [echo "$at_std not supported"; continue])])
110 AT_FULL_COMPILE([[test]])
111 AT_PARSER_CHECK([[test]], 0, [[$6
115 AT_BISON_OPTION_POPDEFS
119 # AT_TEST($1: BISON-DIRECTIVES,
120 # $2: MORE-BISON-DIRECTIVES,
123 # $5: SCANNER-ACTION,
126 # --------------------------------------
127 # Check with and without %header, to avoid regressions. It turns out
128 # that in that case yacc.c calls the set-up of the %union twice,
129 # because YYSTYPE is defined once in the header, and once in the
130 # implementation file (eventually it'd be better to include the header
131 # file, but that's another story). Unfortunately running these macros
132 # a second time doubled the side-effects and resulted in a double
133 # definition of the union members.
134 m4_pushdef([AT_TEST],
135 [_AT_TEST([$1], [$2], [$3], [$4], [$5], [$6], [$7])
136 _AT_TEST([$1 %header], [$2], [$3], [$4], [$5], [$6], [$7])
139 m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc], [glr2.cc]],
141 AT_TEST([%skeleton "]b4_skel["
142 %define api.value.type {double}],
144 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
147 AT_VAL = (res - '0') / 10.0],
150 # A typedef which looks like a Bison keyword, but it's using braces.
151 AT_TEST([%skeleton "]b4_skel["
152 %define api.value.type {variant}],
153 [%code requires { typedef double variant; }],
154 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
157 AT_VAL = (res - '0') / 10.0],
160 # A user defined struct.
161 AT_TEST([%skeleton "]b4_skel["
162 %define api.value.type {struct foo}],
163 [%code requires { struct foo { float fval; int ival; }; }],
165 { printf ("%d %2.1f\n", $1.ival + $2.ival, $1.fval + $2.fval); }],
169 AT_VAL.ival = (res - '0') * 10;
170 AT_VAL.fval = YY_CAST (float, res - '0') / 10.f;
174 # A user defined struct that uses pointers.
175 AT_TEST([%skeleton "]b4_skel["
176 %define api.value.type {struct bar}],
188 %token <up->ival> '1' '2'
189 %printer { ]AT_CXX_IF([[yyo << $$]],
190 [[fprintf (yyo, "%d", $$)]])[; } <up->ival>
194 printf ("%d %d\n", $1, $<up->ival>2);
201 AT_VAL.up = YY_CAST (struct u *, malloc (sizeof *AT_VAL.up));
203 AT_VAL.up->ival = res - '0';
208 # A user defined union.
209 AT_TEST([%skeleton "]b4_skel["
210 %define api.value.type {union foo}],
211 [%code requires { union foo { float fval; int ival; }; }],
212 ['1' '2' { printf ("%d %2.1f\n", $1.ival, $2.fval); }],
220 # A %union and a named %union. In C++ named %union is an error.
221 m4_foreach([b4_union],
222 [m4_bmatch(b4_skel, [\.cc$],
224 [[%union], [%union foo],
225 [%define api.value.union.name foo; %union]])],
226 [AT_TEST([%skeleton "]b4_skel["
227 ]b4_union[ { float fval; int ival; };],
230 ['1' '2' { printf ("%d %2.1f\n", $1, $2); }],
238 # A Bison-defined union.
239 # The token names are not available directly in C++, we use their
240 # codes to keep it simple between C and C++.
241 AT_TEST([%skeleton "]b4_skel["
242 %define api.value.type union],
243 [%token <int> ONE 101;
244 %token <float> TWO 102 THREE 103;
245 %printer { ]AT_CXX_IF([[yyo << $$]],
246 [[fprintf (yyo, "%d", $$)]])[; } <int>
247 %printer { ]AT_CXX_IF([[yyo << $$]],
248 [[fprintf (yyo, "%f", $$)]])[; } <float>
250 [ONE TWO THREE { printf ("%d %2.1f %2.1f\n", $1, $2, $3); }],
251 [{ 101, 102, 103, EOF }],
257 AT_VAL.THREE = 3.3f],
260 # A Bison-defined variant, for lalr1.cc only.
261 m4_if(b4_skel, [lalr1.cc], [
262 AT_TEST([%skeleton "]b4_skel["
263 %define api.value.type variant],
265 %token <std::string> '2';],
266 ['1' '2' { std::cout << $1 << ", " << $2 << '\n'; }],
271 AT_VAL.build<std::string> ("two");],
274 # Test a regression where we passed user types (we can include
275 # commas) to a CPP macro.
276 AT_TEST([%skeleton "]b4_skel["
277 %define api.value.type variant],
278 [%token <std::pair<int, int>> '1';
279 %token <std::pair<std::string, std::string>> '2';],
282 std::cout << $1.first << ':' << $1.second << ", "
283 << $2.first << ':' << $2.second << '\n';
287 AT_VAL.build (std::make_pair (10, 11));
289 AT_VAL.build (std::pair<std::string, std::string> ("two", "deux"));],
292 # Type-based token constructors on move-only types, and types with commas.
293 AT_TEST([%skeleton "]b4_skel["
294 %define api.value.type variant
295 %define api.token.constructor],
296 [[%token <std::pair<int, int>> '1' '2';]],
299 std::cout << $1.first << ':' << $1.second << ", "
300 << $2.first << ':' << $2.second << '\n';
303 [[typedef yy::parser::symbol_type symbol;
305 return symbol (res, std::make_pair (res - '0', res - '0' + 1));
307 return symbol (res)]],
310 # Move-only types, and variadic emplace.
311 AT_TEST([%skeleton "]b4_skel["
312 %code requires { #include <memory> }
313 %define api.value.type variant],
314 [[%token <std::unique_ptr<int>> '1';
315 %token <std::pair<int, int>> '2';]],
316 ['1' '2' { std::cout << *$1 << ", "
317 << $2.first << ':' << $2.second << '\n'; }],
320 ]AT_VAL[.emplace <std::unique_ptr<int>>
321 (std::make_unique <int> (10));
323 ]AT_VAL[.emplace <std::pair<int, int>> (21, 22);]],
325 [AT_REQUIRE_CXX_STD(14, [echo "$at_std not supported"; continue])])
327 # Token constructors on move-only types, and types with commas.
328 AT_TEST([%skeleton "]b4_skel["
329 %code requires { #include <memory> }
330 %define api.value.type variant
331 %define api.token.constructor],
332 [[%token <std::unique_ptr<int>> ONE;
333 %token <std::pair<int, int>> TWO;
335 [ONE TWO { std::cout << *$1 << ", "
336 << $2.first << ':' << $2.second << '\n'; }],
339 return yy::parser::make_ONE (std::make_unique<int> (10));
341 return yy::parser::make_TWO (std::make_pair (21, 22));
343 return yy::parser::make_EOI ()]],
345 [AT_REQUIRE_CXX_STD(14, [echo "$at_std not supported"; continue])])
347 # Type-based token constructors on move-only types, and types with commas.
348 AT_TEST([%skeleton "]b4_skel["
349 %code requires { #include <memory> }
350 %define api.value.type variant
351 %define api.token.constructor],
352 [[%token <std::unique_ptr<int>> '1';
353 %token <std::pair<int, int>> '2';]],
354 ['1' '2' { std::cout << *$1 << ", "
355 << $2.first << ':' << $2.second << '\n'; }],
358 return {res, std::make_unique<int> (10)};
360 return {res, std::make_pair (21, 22)};
364 [AT_REQUIRE_CXX_STD(14, [echo "$at_std not supported"; continue])])
370 m4_popdef([_AT_TEST])
373 ## ------------------- ##
374 ## C++: Named %union. ##
375 ## ------------------- ##
377 m4_foreach([b4_skel], [[lalr1.cc], [glr.cc]],
378 [AT_SETUP([b4_skel: Named %union])
380 [%skeleton "]b4_skel["
381 %union foo { float fval; int ival; };
385 AT_BISON_CHECK([input.y], 1, [],
386 [[input.y:2.8-10: error: named %union is invalid in C++