blob: 292670d5958cf5c4985c2fae931658d07ac6a2bc [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001%{
David Reissea2cba82009-03-30 21:35:00 +00002/*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 */
Mark Slee31985722006-05-24 21:45:31 +000020
21/**
22 * Thrift parser.
23 *
24 * This parser is used on a thrift definition file.
25 *
Mark Slee31985722006-05-24 21:45:31 +000026 */
27
David Reissf1454162008-06-30 20:45:47 +000028#define __STDC_LIMIT_MACROS
29#define __STDC_FORMAT_MACROS
Mark Slee31985722006-05-24 21:45:31 +000030#include <stdio.h>
Roger Meier12d70532011-12-14 23:35:28 +000031#ifndef _MSC_VER
David Reissf1454162008-06-30 20:45:47 +000032#include <inttypes.h>
Roger Meier12d70532011-12-14 23:35:28 +000033#else
34#include <stdint.h>
35#endif
David Reiss400a5432008-07-25 19:48:39 +000036#include <limits.h>
Ben Craige9576752013-10-11 08:19:16 -050037#ifdef _MSC_VER
38#include "windows/config.h"
39#endif
Mark Slee31985722006-05-24 21:45:31 +000040#include "main.h"
41#include "globals.h"
42#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000043#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000044
Ben Craige9576752013-10-11 08:19:16 -050045#ifdef _MSC_VER
46//warning C4065: switch statement contains 'default' but no 'case' labels
47#pragma warning(disable:4065)
48#endif
49
Mark Sleef5377b32006-10-10 01:42:59 +000050/**
51 * This global variable is used for automatic numbering of field indices etc.
52 * when parsing the members of a struct. Field values are automatically
53 * assigned starting from -1 and working their way down.
54 */
Mark Slee9cb7c612006-09-01 22:17:45 +000055int y_field_val = -1;
Jens Geyerae0b22c2014-09-04 23:04:21 +020056/**
57 * This global variable is used for automatic numbering of enum values.
58 * y_enum_val is the last value assigned; the next auto-assigned value will be
59 * y_enum_val+1, and then it continues working upwards. Explicitly specified
60 * enum values reset y_enum_val to that value.
61 */
62int32_t y_enum_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000063int g_arglist = 0;
Bryan Duxburyab3666e2009-09-01 23:03:47 +000064const int struct_is_struct = 0;
65const int struct_is_union = 1;
Mark Slee31985722006-05-24 21:45:31 +000066
67%}
68
Mark Sleef5377b32006-10-10 01:42:59 +000069/**
70 * This structure is used by the parser to hold the data types associated with
71 * various parse nodes.
72 */
Mark Slee31985722006-05-24 21:45:31 +000073%union {
Mark Slee30152872006-11-28 01:24:07 +000074 char* id;
David Reissf1454162008-06-30 20:45:47 +000075 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000076 double dconst;
77 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000078 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000079 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000080 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000081 t_typedef* ttypedef;
82 t_enum* tenum;
83 t_enum_value* tenumv;
84 t_const* tconst;
85 t_const_value* tconstv;
86 t_struct* tstruct;
87 t_service* tservice;
88 t_function* tfunction;
89 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000090 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000091 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +000092 t_annotation* tannot;
Bryan Duxburyc7206a42011-08-17 23:17:04 +000093 t_field_id tfieldid;
Mark Slee31985722006-05-24 21:45:31 +000094}
95
Mark Sleef5377b32006-10-10 01:42:59 +000096/**
97 * Strings identifier
98 */
Mark Slee31985722006-05-24 21:45:31 +000099%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000100%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +0000101%token<dtext> tok_doctext
Mark Sleef5377b32006-10-10 01:42:59 +0000102
103/**
Mark Slee30152872006-11-28 01:24:07 +0000104 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +0000105 */
Mark Slee31985722006-05-24 21:45:31 +0000106%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +0000107%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +0000108
Mark Sleef5377b32006-10-10 01:42:59 +0000109/**
David Reiss399442b2008-02-20 02:28:05 +0000110 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +0000111 */
Mark Sleef0712dc2006-10-25 19:03:57 +0000112%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +0000113%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +0000114%token tok_cpp_include
115%token tok_cpp_type
Mark Slee782abbb2007-01-19 00:17:02 +0000116%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000117%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000118%token tok_xsd_nillable
Mark Slee21135c32007-02-05 21:52:08 +0000119%token tok_xsd_attrs
Mark Slee9cb7c612006-09-01 22:17:45 +0000120
Mark Sleef5377b32006-10-10 01:42:59 +0000121/**
122 * Base datatype keywords
123 */
124%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000125%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000126%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000127%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000128%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000129%token tok_senum
Jens Geyer40c28d32015-10-20 23:13:02 +0200130%token tok_i8
Mark Slee9cb7c612006-09-01 22:17:45 +0000131%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000132%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000133%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000134%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000135
Mark Sleef5377b32006-10-10 01:42:59 +0000136/**
137 * Complex type keywords
138 */
Mark Slee31985722006-05-24 21:45:31 +0000139%token tok_map
140%token tok_list
141%token tok_set
142
Mark Sleef5377b32006-10-10 01:42:59 +0000143/**
144 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000145 */
David Reiss6985a422009-03-24 20:00:47 +0000146%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000147
Mark Sleef5377b32006-10-10 01:42:59 +0000148/**
149 * Thrift language keywords
150 */
Mark Slee31985722006-05-24 21:45:31 +0000151%token tok_typedef
152%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000153%token tok_xception
154%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000155%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000156%token tok_service
157%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000158%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000159%token tok_required
160%token tok_optional
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000161%token tok_union
Jens Geyer885c6792014-05-02 21:31:55 +0200162%token tok_reference
Mark Slee31985722006-05-24 21:45:31 +0000163
Mark Sleef5377b32006-10-10 01:42:59 +0000164/**
165 * Grammar nodes
166 */
167
Mark Slee31985722006-05-24 21:45:31 +0000168%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000169%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000170%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000171%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000172%type<ttype> MapType
173%type<ttype> SetType
174%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000175
David Reisscdffe262007-08-14 17:12:31 +0000176%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000177%type<ttype> TypeDefinition
178
Mark Slee31985722006-05-24 21:45:31 +0000179%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000180
David Reissa2309992008-12-10 01:52:48 +0000181%type<ttype> TypeAnnotations
182%type<ttype> TypeAnnotationList
183%type<tannot> TypeAnnotation
Jens Geyer89847df2014-05-02 23:50:04 +0200184%type<id> TypeAnnotationValue
David Reissa2309992008-12-10 01:52:48 +0000185
Mark Slee31985722006-05-24 21:45:31 +0000186%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000187%type<tfieldid> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000188%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000189%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000190%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000191%type<tstruct> FieldList
Jens Geyer885c6792014-05-02 21:31:55 +0200192%type<tbool> FieldReference
Mark Slee31985722006-05-24 21:45:31 +0000193
194%type<tenum> Enum
195%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000196%type<tenumv> EnumDef
Jens Geyerae0b22c2014-09-04 23:04:21 +0200197%type<tenumv> EnumValue
Mark Slee30152872006-11-28 01:24:07 +0000198
Mark Slee6a47fed2007-02-07 02:40:59 +0000199%type<ttypedef> Senum
200%type<tbase> SenumDefList
201%type<id> SenumDef
202
Mark Slee30152872006-11-28 01:24:07 +0000203%type<tconst> Const
204%type<tconstv> ConstValue
205%type<tconstv> ConstList
206%type<tconstv> ConstListContents
207%type<tconstv> ConstMap
208%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000209
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000210%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000211%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000212%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000213%type<tservice> Service
214
215%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000216%type<ttype> FunctionType
217%type<tservice> FunctionList
218
Mark Slee36bfa2e2007-01-19 20:09:51 +0000219%type<tstruct> Throws
220%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000221%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000222%type<tbool> XsdAll
223%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000224%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000225%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000226%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000227
David Reisscbd4bac2007-08-14 17:12:33 +0000228%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000229
Mark Slee31985722006-05-24 21:45:31 +0000230%%
231
Mark Sleef5377b32006-10-10 01:42:59 +0000232/**
233 * Thrift Grammar Implementation.
234 *
235 * For the most part this source file works its way top down from what you
236 * might expect to find in a typical .thrift file, i.e. type definitions and
237 * namespaces up top followed by service definitions using those types.
238 */
Mark Slee31985722006-05-24 21:45:31 +0000239
240Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000241 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000242 {
243 pdebug("Program -> Headers DefinitionList");
Jens Geyere8379b52014-01-25 00:59:45 +0100244 if((g_program_doctext_candidate != NULL) && (g_program_doctext_status != ALREADY_PROCESSED))
245 {
246 g_program->set_doc(g_program_doctext_candidate);
247 g_program_doctext_status = ALREADY_PROCESSED;
David Reissc2532a92007-07-30 23:46:11 +0000248 }
David Reisscbd4bac2007-08-14 17:12:33 +0000249 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000250 }
251
David Reisscbd4bac2007-08-14 17:12:33 +0000252CaptureDocText:
253 {
254 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000255 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000256 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000257 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000258 $$ = NULL;
259 }
260 }
261
262/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
263DestroyDocText:
264 {
265 if (g_parse_mode == PROGRAM) {
266 clear_doctext();
267 }
268 }
269
270/* We have to DestroyDocText here, otherwise it catches the doctext
271 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000272HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000273 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000274 {
275 pdebug("HeaderList -> HeaderList Header");
276 }
277|
278 {
279 pdebug("HeaderList -> ");
280 }
281
282Header:
283 Include
284 {
285 pdebug("Header -> Include");
286 }
BCGcc193c12015-11-12 21:02:51 -0500287| tok_namespace tok_identifier tok_identifier TypeAnnotations
Mark Sleef0712dc2006-10-25 19:03:57 +0000288 {
David Reiss79eca142008-02-27 01:55:13 +0000289 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100290 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000291 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000292 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000293 }
BCGcc193c12015-11-12 21:02:51 -0500294 if ($4 != NULL) {
295 g_program->set_namespace_annotations($2, $4->annotations_);
296 delete $4;
297 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000298 }
David Reissfb790d72010-09-02 16:41:45 +0000299| tok_namespace '*' tok_identifier
300 {
301 pdebug("Header -> tok_namespace * tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100302 declare_valid_program_doctext();
David Reissfb790d72010-09-02 16:41:45 +0000303 if (g_parse_mode == PROGRAM) {
304 g_program->set_namespace("*", $3);
305 }
306 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000307| tok_cpp_include tok_literal
308 {
309 pdebug("Header -> tok_cpp_include tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100310 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000311 if (g_parse_mode == PROGRAM) {
312 g_program->add_cpp_include($2);
313 }
314 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000315
316Include:
317 tok_include tok_literal
318 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000319 pdebug("Include -> tok_include tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100320 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000321 if (g_parse_mode == INCLUDES) {
322 std::string path = include_file(std::string($2));
323 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000324 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000325 }
326 }
327 }
Mark Slee31985722006-05-24 21:45:31 +0000328
329DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000330 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000331 {
332 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000333 if ($2 != NULL && $3 != NULL) {
334 $3->set_doc($2);
335 }
Mark Slee31985722006-05-24 21:45:31 +0000336 }
337|
338 {
339 pdebug("DefinitionList -> ");
340 }
341
342Definition:
Mark Slee30152872006-11-28 01:24:07 +0000343 Const
344 {
345 pdebug("Definition -> Const");
346 if (g_parse_mode == PROGRAM) {
347 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000348 }
David Reisscdffe262007-08-14 17:12:31 +0000349 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000350 }
351| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000352 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000353 pdebug("Definition -> TypeDefinition");
354 if (g_parse_mode == PROGRAM) {
355 g_scope->add_type($1->get_name(), $1);
356 if (g_parent_scope != NULL) {
357 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
358 }
Roger Meierba406d32013-07-15 22:41:34 +0200359 if (! g_program->is_unique_typename($1)) {
360 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
361 exit(1);
362 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000363 }
David Reisscdffe262007-08-14 17:12:31 +0000364 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000365 }
Mark Slee31985722006-05-24 21:45:31 +0000366| Service
367 {
368 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000369 if (g_parse_mode == PROGRAM) {
370 g_scope->add_service($1->get_name(), $1);
371 if (g_parent_scope != NULL) {
372 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
373 }
374 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200375 if (! g_program->is_unique_typename($1)) {
376 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
377 exit(1);
378 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000379 }
David Reisscdffe262007-08-14 17:12:31 +0000380 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000381 }
382
Mark Sleef0712dc2006-10-25 19:03:57 +0000383TypeDefinition:
384 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000385 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000386 pdebug("TypeDefinition -> Typedef");
387 if (g_parse_mode == PROGRAM) {
388 g_program->add_typedef($1);
389 }
390 }
391| Enum
392 {
393 pdebug("TypeDefinition -> Enum");
394 if (g_parse_mode == PROGRAM) {
395 g_program->add_enum($1);
396 }
397 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000398| Senum
399 {
400 pdebug("TypeDefinition -> Senum");
401 if (g_parse_mode == PROGRAM) {
402 g_program->add_typedef($1);
403 }
404 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000405| Struct
406 {
407 pdebug("TypeDefinition -> Struct");
408 if (g_parse_mode == PROGRAM) {
409 g_program->add_struct($1);
410 }
411 }
412| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000413 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000414 pdebug("TypeDefinition -> Xception");
415 if (g_parse_mode == PROGRAM) {
416 g_program->add_xception($1);
417 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000418 }
Mark Slee31985722006-05-24 21:45:31 +0000419
Jens Geyer089bcd32014-09-11 22:36:41 +0200420CommaOrSemicolonOptional:
421 ','
422 {}
423| ';'
424 {}
425|
426 {}
427
Mark Slee31985722006-05-24 21:45:31 +0000428Typedef:
Jens Geyer089bcd32014-09-11 22:36:41 +0200429 tok_typedef FieldType tok_identifier TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000430 {
David Reiss4dd78012010-03-09 05:19:08 +0000431 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200432 validate_simple_identifier( $3);
David Reisscdffe262007-08-14 17:12:31 +0000433 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000434 $$ = td;
Roger Meier30877382012-09-17 21:18:05 +0000435 if ($4 != NULL) {
436 $$->annotations_ = $4->annotations_;
437 delete $4;
438 }
Mark Slee31985722006-05-24 21:45:31 +0000439 }
440
441Enum:
Roger Meier30877382012-09-17 21:18:05 +0000442 tok_enum tok_identifier '{' EnumDefList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000443 {
444 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000445 $$ = $4;
Jens Geyer12c09f42013-08-25 14:16:27 +0200446 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000447 $$->set_name($2);
Roger Meier30877382012-09-17 21:18:05 +0000448 if ($6 != NULL) {
449 $$->annotations_ = $6->annotations_;
450 delete $6;
451 }
Jens Geyerae0b22c2014-09-04 23:04:21 +0200452
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000453 // make constants for all the enum values
454 if (g_parse_mode == PROGRAM) {
455 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
456 std::vector<t_enum_value*>::const_iterator c_iter;
457 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
458 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
459 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
460 const_val->set_enum($$);
461 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
462 if (g_parent_scope != NULL) {
463 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
464 }
465 }
466 }
Mark Slee31985722006-05-24 21:45:31 +0000467 }
468
469EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000470 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000471 {
472 pdebug("EnumDefList -> EnumDefList EnumDef");
473 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000474 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000475 }
476|
477 {
478 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000479 $$ = new t_enum(g_program);
Jens Geyerae0b22c2014-09-04 23:04:21 +0200480 y_enum_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000481 }
482
483EnumDef:
Jens Geyerae0b22c2014-09-04 23:04:21 +0200484 CaptureDocText EnumValue TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000485 {
Jens Geyerae0b22c2014-09-04 23:04:21 +0200486 pdebug("EnumDef -> EnumValue");
487 $$ = $2;
ccheeverf53b5cf2007-02-05 20:33:11 +0000488 if ($1 != NULL) {
489 $$->set_doc($1);
490 }
Jens Geyer86b309c2014-09-11 23:54:35 +0200491 if ($3 != NULL) {
Roger Meier30877382012-09-17 21:18:05 +0000492 $$->annotations_ = $3->annotations_;
493 delete $3;
494 }
Mark Slee30152872006-11-28 01:24:07 +0000495 }
496
Jens Geyerae0b22c2014-09-04 23:04:21 +0200497EnumValue:
498 tok_identifier '=' tok_int_constant
499 {
500 pdebug("EnumValue -> tok_identifier = tok_int_constant");
501 if ($3 < INT32_MIN || $3 > INT32_MAX) {
502 // Note: this used to be just a warning. However, since thrift always
503 // treats enums as i32 values, I'm changing it to a fatal error.
504 // I doubt this will affect many people, but users who run into this
505 // will have to update their thrift files to manually specify the
506 // truncated i32 value that thrift has always been using anyway.
507 failure("64-bit value supplied for enum %s will be truncated.", $1);
508 }
509 y_enum_val = static_cast<int32_t>($3);
510 $$ = new t_enum_value($1, y_enum_val);
511 }
512 |
513 tok_identifier
514 {
515 pdebug("EnumValue -> tok_identifier");
516 validate_simple_identifier( $1);
517 if (y_enum_val == INT32_MAX) {
518 failure("enum value overflow at enum %s", $1);
519 }
520 ++y_enum_val;
521 $$ = new t_enum_value($1, y_enum_val);
522 }
523
Mark Slee6a47fed2007-02-07 02:40:59 +0000524Senum:
Roger Meier30877382012-09-17 21:18:05 +0000525 tok_senum tok_identifier '{' SenumDefList '}' TypeAnnotations
Mark Slee6a47fed2007-02-07 02:40:59 +0000526 {
527 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200528 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000529 $$ = new t_typedef(g_program, $4, $2);
Roger Meier30877382012-09-17 21:18:05 +0000530 if ($6 != NULL) {
531 $$->annotations_ = $6->annotations_;
532 delete $6;
533 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000534 }
535
536SenumDefList:
537 SenumDefList SenumDef
538 {
539 pdebug("SenumDefList -> SenumDefList SenumDef");
540 $$ = $1;
541 $$->add_string_enum_val($2);
542 }
543|
544 {
545 pdebug("SenumDefList -> ");
546 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
547 $$->set_string_enum(true);
548 }
549
550SenumDef:
551 tok_literal CommaOrSemicolonOptional
552 {
553 pdebug("SenumDef -> tok_literal");
554 $$ = $1;
555 }
556
Mark Slee30152872006-11-28 01:24:07 +0000557Const:
558 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
559 {
560 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000561 if (g_parse_mode == PROGRAM) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200562 validate_simple_identifier( $3);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000563 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000564 $$ = new t_const($2, $3, $5);
565 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000566
567 g_scope->add_constant($3, $$);
568 if (g_parent_scope != NULL) {
569 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
570 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000571 } else {
572 $$ = NULL;
573 }
Mark Slee30152872006-11-28 01:24:07 +0000574 }
575
576ConstValue:
577 tok_int_constant
578 {
579 pdebug("ConstValue => tok_int_constant");
580 $$ = new t_const_value();
581 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000582 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
Roger Meier5f2d34e2013-11-16 16:43:41 +0100583 pwarning(1, "64-bit constant \"%" PRIi64"\" may not work in all languages.\n", $1);
David Reissf1454162008-06-30 20:45:47 +0000584 }
Mark Slee30152872006-11-28 01:24:07 +0000585 }
586| tok_dub_constant
587 {
588 pdebug("ConstValue => tok_dub_constant");
589 $$ = new t_const_value();
590 $$->set_double($1);
591 }
592| tok_literal
593 {
594 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000595 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000596 }
Mark Slee67fc6342006-11-29 03:37:04 +0000597| tok_identifier
598 {
599 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000600 $$ = new t_const_value();
601 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000602 }
Mark Slee30152872006-11-28 01:24:07 +0000603| ConstList
604 {
605 pdebug("ConstValue => ConstList");
606 $$ = $1;
607 }
608| ConstMap
609 {
610 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000611 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000612 }
613
614ConstList:
615 '[' ConstListContents ']'
616 {
617 pdebug("ConstList => [ ConstListContents ]");
618 $$ = $2;
619 }
620
621ConstListContents:
622 ConstListContents ConstValue CommaOrSemicolonOptional
623 {
624 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
625 $$ = $1;
626 $$->add_list($2);
627 }
628|
629 {
630 pdebug("ConstListContents =>");
631 $$ = new t_const_value();
632 $$->set_list();
633 }
634
635ConstMap:
636 '{' ConstMapContents '}'
637 {
638 pdebug("ConstMap => { ConstMapContents }");
639 $$ = $2;
640 }
641
642ConstMapContents:
643 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
644 {
645 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
646 $$ = $1;
647 $$->add_map($2, $4);
648 }
649|
650 {
651 pdebug("ConstMapContents =>");
652 $$ = new t_const_value();
653 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000654 }
655
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000656StructHead:
657 tok_struct
658 {
659 $$ = struct_is_struct;
660 }
661| tok_union
662 {
663 $$ = struct_is_union;
664 }
665
Mark Slee31985722006-05-24 21:45:31 +0000666Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000667 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000668 {
669 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200670 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000671 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000672 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000673 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000674 $$->set_name($2);
675 if ($7 != NULL) {
676 $$->annotations_ = $7->annotations_;
677 delete $7;
678 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000679 }
Ben Craige9576752013-10-11 08:19:16 -0500680
Mark Slee36bfa2e2007-01-19 20:09:51 +0000681XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000682 tok_xsd_all
683 {
684 $$ = true;
685 }
686|
687 {
688 $$ = false;
689 }
690
Mark Slee36bfa2e2007-01-19 20:09:51 +0000691XsdOptional:
692 tok_xsd_optional
693 {
694 $$ = true;
695 }
696|
697 {
698 $$ = false;
699 }
700
Mark Slee7df0e2a2007-02-06 21:03:18 +0000701XsdNillable:
702 tok_xsd_nillable
703 {
704 $$ = true;
705 }
706|
707 {
708 $$ = false;
709 }
710
Mark Slee21135c32007-02-05 21:52:08 +0000711XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000712 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000713 {
Mark Slee748d83f2007-02-07 01:20:08 +0000714 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000715 }
716|
717 {
718 $$ = NULL;
719 }
720
Mark Slee9cb7c612006-09-01 22:17:45 +0000721Xception:
Roger Meier30877382012-09-17 21:18:05 +0000722 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000723 {
724 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200725 validate_simple_identifier( $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000726 $4->set_name($2);
727 $4->set_xception(true);
728 $$ = $4;
Roger Meier30877382012-09-17 21:18:05 +0000729 if ($6 != NULL) {
730 $$->annotations_ = $6->annotations_;
731 delete $6;
732 }
Mark Slee31985722006-05-24 21:45:31 +0000733 }
734
735Service:
Roger Meier30877382012-09-17 21:18:05 +0000736 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000737 {
738 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200739 validate_simple_identifier( $2);
Mark Slee78165722007-09-10 22:08:49 +0000740 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000741 $$->set_name($2);
742 $$->set_extends($3);
Roger Meier30877382012-09-17 21:18:05 +0000743 if ($9 != NULL) {
744 $$->annotations_ = $9->annotations_;
745 delete $9;
746 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000747 }
748
Mark Slee78165722007-09-10 22:08:49 +0000749FlagArgs:
750 {
751 g_arglist = 1;
752 }
753
754UnflagArgs:
755 {
756 g_arglist = 0;
757 }
758
Mark Slee36bfa2e2007-01-19 20:09:51 +0000759Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000760 tok_extends tok_identifier
761 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000762 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000763 $$ = NULL;
764 if (g_parse_mode == PROGRAM) {
765 $$ = g_scope->get_service($2);
766 if ($$ == NULL) {
767 yyerror("Service \"%s\" has not been defined.", $2);
768 exit(1);
769 }
770 }
771 }
772|
773 {
774 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000775 }
776
777FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000778 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000779 {
780 pdebug("FunctionList -> FunctionList Function");
781 $$ = $1;
782 $1->add_function($2);
783 }
784|
785 {
786 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000787 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000788 }
789
790Function:
Roger Meier30877382012-09-17 21:18:05 +0000791 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000792 {
Jens Geyer12c09f42013-08-25 14:16:27 +0200793 validate_simple_identifier( $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000794 $6->set_name(std::string($4) + "_args");
795 $$ = new t_function($3, $4, $6, $8, $2);
796 if ($1 != NULL) {
797 $$->set_doc($1);
798 }
Roger Meier30877382012-09-17 21:18:05 +0000799 if ($9 != NULL) {
800 $$->annotations_ = $9->annotations_;
801 delete $9;
802 }
Mark Slee31985722006-05-24 21:45:31 +0000803 }
804
David Reiss6985a422009-03-24 20:00:47 +0000805Oneway:
806 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000807 {
Mark Slee52f643d2006-08-09 00:03:43 +0000808 $$ = true;
809 }
810|
811 {
812 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000813 }
814
Mark Slee36bfa2e2007-01-19 20:09:51 +0000815Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000816 tok_throws '(' FieldList ')'
817 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000818 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000819 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000820 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000821 yyerror("Throws clause may not contain non-exception types");
822 exit(1);
823 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000824 }
825|
826 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000827 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000828 }
829
Mark Slee31985722006-05-24 21:45:31 +0000830FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000831 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000832 {
833 pdebug("FieldList -> FieldList , Field");
834 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000835 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100836 yyerror("\"%d: %s\" - field identifier/name has already been used", $2->get_key(), $2->get_name().c_str());
Mark Slee6f9ac3f2007-11-28 22:28:13 +0000837 exit(1);
838 }
Mark Slee31985722006-05-24 21:45:31 +0000839 }
840|
841 {
842 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000843 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000844 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000845 }
846
847Field:
Jens Geyer885c6792014-05-02 21:31:55 +0200848 CaptureDocText FieldIdentifier FieldRequiredness FieldType FieldReference tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000849 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000850 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000851 if ($2.auto_assigned) {
Jens Geyer885c6792014-05-02 21:31:55 +0200852 pwarning(1, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $6);
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000853 if (g_strict >= 192) {
854 yyerror("Implicit field keys are deprecated and not allowed with -strict");
855 exit(1);
856 }
Mark Slee31985722006-05-24 21:45:31 +0000857 }
Jens Geyer885c6792014-05-02 21:31:55 +0200858 validate_simple_identifier($6);
859 $$ = new t_field($4, $6, $2.value);
860 $$->set_reference($5);
David Reiss8320a922007-08-14 19:59:26 +0000861 $$->set_req($3);
Jens Geyer885c6792014-05-02 21:31:55 +0200862 if ($7 != NULL) {
863 g_scope->resolve_const_value($7, $4);
864 validate_field_value($$, $7);
865 $$->set_value($7);
Mark Slee7ff32452007-02-01 05:26:18 +0000866 }
Jens Geyer885c6792014-05-02 21:31:55 +0200867 $$->set_xsd_optional($8);
868 $$->set_xsd_nillable($9);
ccheeverf53b5cf2007-02-05 20:33:11 +0000869 if ($1 != NULL) {
870 $$->set_doc($1);
871 }
David Reiss53c10e02010-03-05 07:51:51 +0000872 if ($10 != NULL) {
Jens Geyer885c6792014-05-02 21:31:55 +0200873 $$->set_xsd_attrs($10);
874 }
875 if ($11 != NULL) {
876 $$->annotations_ = $11->annotations_;
877 delete $11;
David Reiss53c10e02010-03-05 07:51:51 +0000878 }
Mark Slee31985722006-05-24 21:45:31 +0000879 }
Mark Slee7ff32452007-02-01 05:26:18 +0000880
881FieldIdentifier:
882 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000883 {
Mark Slee7ff32452007-02-01 05:26:18 +0000884 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000885 if (g_allow_neg_field_keys) {
886 /*
887 * g_allow_neg_field_keys exists to allow users to add explicitly
888 * specified key values to old .thrift files without breaking
889 * protocol compatibility.
890 */
891 if ($1 != y_field_val) {
892 /*
893 * warn if the user-specified negative value isn't what
894 * thrift would have auto-assigned.
895 */
Roger Meier5f2d34e2013-11-16 16:43:41 +0100896 pwarning(1, "Nonpositive field key (%" PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000897 "auto-assigned by thrift (%d).\n", $1, y_field_val);
898 }
899 /*
900 * Leave $1 as-is, and update y_field_val to be one less than $1.
901 * The FieldList parsing will catch any duplicate key values.
902 */
Ben Craige9576752013-10-11 08:19:16 -0500903 y_field_val = static_cast<int32_t>($1 - 1);
904 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000905 $$.auto_assigned = false;
906 } else {
Ben Craige9576752013-10-11 08:19:16 -0500907 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000908 $1);
909 $$.value = y_field_val--;
910 $$.auto_assigned = true;
911 }
912 } else {
Ben Craige9576752013-10-11 08:19:16 -0500913 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000914 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000915 }
Jens Geyer06340a42016-03-25 01:34:03 +0200916 if( (SHRT_MIN > $$.value) || ($$.value > SHRT_MAX)) {
917 pwarning(1, "Field key (%d) exceeds allowed range (%d..%d).\n",
918 $$.value, SHRT_MIN, SHRT_MAX);
919 }
Mark Slee7ff32452007-02-01 05:26:18 +0000920 }
921|
922 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000923 $$.value = y_field_val--;
924 $$.auto_assigned = true;
Jens Geyer06340a42016-03-25 01:34:03 +0200925 if( (SHRT_MIN > $$.value) || ($$.value > SHRT_MAX)) {
926 pwarning(1, "Field key (%d) exceeds allowed range (%d..%d).\n",
927 $$.value, SHRT_MIN, SHRT_MAX);
928 }
Mark Slee7ff32452007-02-01 05:26:18 +0000929 }
930
Jens Geyer885c6792014-05-02 21:31:55 +0200931FieldReference:
932 tok_reference
933 {
934 $$ = true;
935 }
936|
937 {
938 $$ = false;
939 }
940
David Reiss8320a922007-08-14 19:59:26 +0000941FieldRequiredness:
942 tok_required
943 {
David Reiss45603e92009-09-02 22:15:55 +0000944 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +0000945 }
946| tok_optional
947 {
Mark Slee78165722007-09-10 22:08:49 +0000948 if (g_arglist) {
949 if (g_parse_mode == PROGRAM) {
950 pwarning(1, "optional keyword is ignored in argument lists.\n");
951 }
David Reiss204420f2008-01-11 20:59:03 +0000952 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000953 } else {
David Reiss204420f2008-01-11 20:59:03 +0000954 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +0000955 }
David Reiss8320a922007-08-14 19:59:26 +0000956 }
957|
958 {
David Reiss204420f2008-01-11 20:59:03 +0000959 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +0000960 }
961
Mark Slee7ff32452007-02-01 05:26:18 +0000962FieldValue:
963 '=' ConstValue
964 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000965 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000966 $$ = $2;
967 } else {
968 $$ = NULL;
969 }
970 }
971|
972 {
973 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000974 }
Mark Slee31985722006-05-24 21:45:31 +0000975
Mark Slee31985722006-05-24 21:45:31 +0000976FunctionType:
977 FieldType
978 {
Mark Sleee8540632006-05-30 09:24:40 +0000979 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000980 $$ = $1;
981 }
982| tok_void
983 {
Mark Sleee8540632006-05-30 09:24:40 +0000984 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000985 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000986 }
987
988FieldType:
989 tok_identifier
990 {
Mark Sleee8540632006-05-30 09:24:40 +0000991 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000992 if (g_parse_mode == INCLUDES) {
993 // Ignore identifiers in include mode
994 $$ = NULL;
995 } else {
996 // Lookup the identifier in the current scope
997 $$ = g_scope->get_type($1);
998 if ($$ == NULL) {
jfarrelle0e83162014-04-08 22:45:01 -0400999 /*
1000 * Either this type isn't yet declared, or it's never
1001 declared. Either way allow it and we'll figure it out
1002 during generation.
1003 */
Jens Geyerd000b242014-04-13 21:58:47 +02001004 $$ = new t_typedef(g_program, $1, true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001005 }
Mark Sleee8540632006-05-30 09:24:40 +00001006 }
Mark Slee31985722006-05-24 21:45:31 +00001007 }
1008| BaseType
1009 {
Mark Sleee8540632006-05-30 09:24:40 +00001010 pdebug("FieldType -> BaseType");
1011 $$ = $1;
1012 }
1013| ContainerType
1014 {
1015 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001016 $$ = $1;
1017 }
1018
David Reissc8e30052009-07-27 17:02:42 +00001019BaseType: SimpleBaseType TypeAnnotations
1020 {
1021 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1022 if ($2 != NULL) {
1023 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1024 $$->annotations_ = $2->annotations_;
1025 delete $2;
1026 } else {
1027 $$ = $1;
1028 }
1029 }
1030
1031SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001032 tok_string
1033 {
1034 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001035 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001036 }
Mark Slee8d725a22007-04-13 01:57:12 +00001037| tok_binary
1038 {
1039 pdebug("BaseType -> tok_binary");
1040 $$ = g_type_binary;
1041 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001042| tok_slist
1043 {
1044 pdebug("BaseType -> tok_slist");
1045 $$ = g_type_slist;
1046 }
Mark Slee78f58e22006-09-02 04:17:07 +00001047| tok_bool
1048 {
1049 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001050 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001051 }
Jens Geyer40c28d32015-10-20 23:13:02 +02001052| tok_i8
Mark Slee31985722006-05-24 21:45:31 +00001053 {
Jens Geyer40c28d32015-10-20 23:13:02 +02001054 pdebug("BaseType -> tok_i8");
1055 $$ = g_type_i8;
Mark Slee31985722006-05-24 21:45:31 +00001056 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001057| tok_i16
1058 {
1059 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001060 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001061 }
Mark Slee31985722006-05-24 21:45:31 +00001062| tok_i32
1063 {
1064 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001065 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001066 }
Mark Slee31985722006-05-24 21:45:31 +00001067| tok_i64
1068 {
1069 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001070 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001071 }
Mark Sleec98d0502006-09-06 02:42:25 +00001072| tok_double
1073 {
1074 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001075 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001076 }
Mark Slee31985722006-05-24 21:45:31 +00001077
David Reissa2309992008-12-10 01:52:48 +00001078ContainerType: SimpleContainerType TypeAnnotations
1079 {
1080 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1081 $$ = $1;
1082 if ($2 != NULL) {
1083 $$->annotations_ = $2->annotations_;
1084 delete $2;
1085 }
1086 }
1087
1088SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001089 MapType
1090 {
David Reissa2309992008-12-10 01:52:48 +00001091 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001092 $$ = $1;
1093 }
1094| SetType
1095 {
David Reissa2309992008-12-10 01:52:48 +00001096 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001097 $$ = $1;
1098 }
1099| ListType
1100 {
David Reissa2309992008-12-10 01:52:48 +00001101 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001102 $$ = $1;
1103 }
1104
1105MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001106 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001107 {
1108 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001109 $$ = new t_map($4, $6);
1110 if ($2 != NULL) {
1111 ((t_container*)$$)->set_cpp_name(std::string($2));
1112 }
Mark Sleee8540632006-05-30 09:24:40 +00001113 }
1114
1115SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001116 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001117 {
1118 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001119 $$ = new t_set($4);
1120 if ($2 != NULL) {
1121 ((t_container*)$$)->set_cpp_name(std::string($2));
1122 }
Mark Sleee8540632006-05-30 09:24:40 +00001123 }
1124
1125ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001126 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001127 {
1128 pdebug("ListType -> tok_list<FieldType>");
Jens Geyer6fe77e82014-03-16 16:48:53 +02001129 check_for_list_of_bytes($3);
Mark Sleef0712dc2006-10-25 19:03:57 +00001130 $$ = new t_list($3);
1131 if ($5 != NULL) {
1132 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001133 }
1134 }
1135
Mark Slee36bfa2e2007-01-19 20:09:51 +00001136CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001137 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001138 {
Mark Sleeafc76542007-02-09 21:55:44 +00001139 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001140 }
1141|
1142 {
1143 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001144 }
1145
David Reissa2309992008-12-10 01:52:48 +00001146TypeAnnotations:
1147 '(' TypeAnnotationList ')'
1148 {
1149 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1150 $$ = $2;
1151 }
1152|
1153 {
1154 $$ = NULL;
1155 }
1156
1157TypeAnnotationList:
1158 TypeAnnotationList TypeAnnotation
1159 {
1160 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1161 $$ = $1;
1162 $$->annotations_[$2->key] = $2->val;
1163 delete $2;
1164 }
1165|
1166 {
1167 /* Just use a dummy structure to hold the annotations. */
1168 $$ = new t_struct(g_program);
1169 }
1170
1171TypeAnnotation:
Jens Geyer89847df2014-05-02 23:50:04 +02001172 tok_identifier TypeAnnotationValue CommaOrSemicolonOptional
David Reissa2309992008-12-10 01:52:48 +00001173 {
Jens Geyer89847df2014-05-02 23:50:04 +02001174 pdebug("TypeAnnotation -> TypeAnnotationValue");
David Reissa2309992008-12-10 01:52:48 +00001175 $$ = new t_annotation;
1176 $$->key = $1;
Jens Geyer89847df2014-05-02 23:50:04 +02001177 $$->val = $2;
1178 }
1179
1180TypeAnnotationValue:
1181 '=' tok_literal
1182 {
1183 pdebug("TypeAnnotationValue -> = tok_literal");
1184 $$ = $2;
1185 }
1186|
1187 {
1188 pdebug("TypeAnnotationValue ->");
1189 $$ = strdup("1");
David Reissa2309992008-12-10 01:52:48 +00001190 }
1191
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001192%%