source: src/Parser/parser.yy@ 9d32bc8

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since 9d32bc8 was 6d539f83, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

rename functions

  • Property mode set to 100644
File size: 123.9 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[9335ecc]7// parser.yy --
[974906e2]8//
[c11e31c]9// Author : Peter A. Buhr
10// Created On : Sat Sep 1 20:22:55 2001
[936e9f4]11// Last Modified By : Peter A. Buhr
[6d539f83]12// Last Modified On : Mon Apr 30 09:20:47 2018
13// Update Count : 3207
[974906e2]14//
[c11e31c]15
[de62360d]16// This grammar is based on the ANSI99/11 C grammar, specifically parts of EXPRESSION and STATEMENTS, and on the C
17// grammar by James A. Roskind, specifically parts of DECLARATIONS and EXTERNAL DEFINITIONS. While parts have been
18// copied, important changes have been made in all sections; these changes are sufficient to constitute a new grammar.
19// In particular, this grammar attempts to be more syntactically precise, i.e., it parses less incorrect language syntax
20// that must be subsequently rejected by semantic checks. Nevertheless, there are still several semantic checks
21// required and many are noted in the grammar. Finally, the grammar is extended with GCC and CFA language extensions.
[c11e31c]22
[de62360d]23// Acknowledgments to Richard Bilson, Glen Ditchfield, and Rodolfo Gabriel Esteves who all helped when I got stuck with
24// the grammar.
[c11e31c]25
26// The root language for this grammar is ANSI99/11 C. All of ANSI99/11 is parsed, except for:
27//
28// 1. designation with '=' (use ':' instead)
29//
[de62360d]30// Most of the syntactic extensions from ANSI90 to ANSI11 C are marked with the comment "C99/C11". This grammar also has
31// two levels of extensions. The first extensions cover most of the GCC C extensions, except for:
[c11e31c]32//
[e7aed49]33// 1. designation with and without '=' (use ':' instead)
[c0aa336]34// 2. attributes not allowed in parenthesis of declarator
[c11e31c]35//
[de62360d]36// All of the syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for Cforall
37// (CFA), which fixes several of C's outstanding problems and extends C with many modern language concepts. All of the
38// syntactic extensions for CFA C are marked with the comment "CFA". As noted above, there is one unreconcileable
39// parsing problem between C99 and CFA with respect to designators; this is discussed in detail before the "designation"
40// grammar rule.
[51b73452]41
42%{
[b87a5ed]43#define YYDEBUG_LEXER_TEXT (yylval) // lexer loads this up each time
44#define YYDEBUG 1 // get the pretty debugging code to compile
[201aeb9]45#define YYERROR_VERBOSE // more information in syntax errors
[51b73452]46
47#undef __GNUC_MINOR__
48
49#include <cstdio>
50#include <stack>
[9ed4f94]51using namespace std;
52
[51b73452]53#include "ParseNode.h"
[984dce6]54#include "TypedefTable.h"
[1db21619]55#include "TypeData.h"
[51b73452]56#include "LinkageSpec.h"
[9ed4f94]57#include "Common/SemanticError.h" // error_str
[513e165]58#include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo...
[51b73452]59
[cbaee0d]60extern DeclarationNode * parseTree;
[8b7ee09]61extern LinkageSpec::Spec linkage;
[0da3e2c]62extern TypedefTable typedefTable;
63
[2298f728]64stack< LinkageSpec::Spec > linkageStack;
[7bf7fb9]65
[15697ff]66bool appendStr( string & to, string & from ) {
67 // 1. Multiple strings are concatenated into a single string but not combined internally. The reason is that
[ea0c5e3]68 // "\x12" "3" is treated as 2 characters versus 1 because "escape sequences are converted into single members of
[15697ff]69 // the execution character set just prior to adjacent string literal concatenation" (C11, Section 6.4.5-8). It is
70 // easier to let the C compiler handle this case.
71 //
72 // 2. String encodings are transformed into canonical form (one encoding at start) so the encoding can be found
73 // without searching the string, e.g.: "abc" L"def" L"ghi" => L"abc" "def" "ghi". Multiple encodings must match,
74 // i.e., u"a" U"b" L"c" is disallowed.
75
76 if ( from[0] != '"' ) { // encoding ?
77 if ( to[0] != '"' ) { // encoding ?
[ea0c5e3]78 if ( to[0] != from[0] || to[1] != from[1] ) { // different encodings ?
[15697ff]79 yyerror( "non-matching string encodings for string-literal concatenation" );
80 return false; // parse error, must call YYERROR in action
[ea0c5e3]81 } else if ( from[1] == '8' ) {
82 from.erase( 0, 1 ); // remove 2nd encoding
[15697ff]83 } // if
84 } else {
[ea0c5e3]85 if ( from[1] == '8' ) { // move encoding to start
86 to = "u8" + to;
87 from.erase( 0, 1 ); // remove 2nd encoding
88 } else {
89 to = from[0] + to;
90 } // if
[15697ff]91 } // if
92 from.erase( 0, 1 ); // remove 2nd encoding
93 } // if
94 to += " " + from; // concatenated into single string
95 return true;
[7bf7fb9]96} // appendStr
[c0aa336]97
98DeclarationNode * distAttr( DeclarationNode * specifier, DeclarationNode * declList ) {
99 // distribute declaration_specifier across all declared variables, e.g., static, const, __attribute__.
100 DeclarationNode * cur = declList, * cl = (new DeclarationNode)->addType( specifier );
101 //cur->addType( specifier );
102 for ( cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
103 cl->cloneBaseType( cur );
104 } // for
105 declList->addType( cl );
106// delete cl;
107 return declList;
108} // distAttr
109
110void distExt( DeclarationNode * declaration ) {
111 // distribute EXTENSION across all declarations
112 for ( DeclarationNode *iter = declaration; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
113 iter->set_extension( true );
114 } // for
115} // distExt
[fdca7c6]116
[c38ae92]117// There is an ambiguity for inline generic-routine return-types and generic routines.
118// forall( otype T ) struct S { int i; } bar( T ) {}
119// Does the forall bind to the struct or the routine, and how would it be possible to explicitly specify the binding.
120// forall( otype T ) struct S { int T; } forall( otype W ) bar( W ) {}
121
122void rebindForall( DeclarationNode * declSpec, DeclarationNode * funcDecl ) {
123 if ( declSpec->type->kind == TypeData::Aggregate ) { // return is aggregate definition
124 funcDecl->type->forall = declSpec->type->aggregate.params; // move forall from aggregate to function type
125 declSpec->type->aggregate.params = nullptr;
126 } // if
127} // rebindForall
128
[dc7db63]129NameExpr * build_postfix_name( const string * name ) {
130 NameExpr * new_name = build_varref( new string( "?`" + *name ) );
131 delete name;
132 return new_name;
133} // build_postfix_name
134
[9997fee]135bool forall = false, xxx = false; // aggregate have one or more forall qualifiers ?
[d48e529]136
[201aeb9]137// https://www.gnu.org/software/bison/manual/bison.html#Location-Type
138#define YYLLOC_DEFAULT(Cur, Rhs, N) \
139if ( N ) { \
140 (Cur).first_line = YYRHSLOC( Rhs, 1 ).first_line; \
141 (Cur).first_column = YYRHSLOC( Rhs, 1 ).first_column; \
142 (Cur).last_line = YYRHSLOC( Rhs, N ).last_line; \
143 (Cur).last_column = YYRHSLOC( Rhs, N ).last_column; \
144 (Cur).filename = YYRHSLOC( Rhs, 1 ).filename; \
145} else { \
146 (Cur).first_line = (Cur).last_line = YYRHSLOC( Rhs, 0 ).last_line; \
147 (Cur).first_column = (Cur).last_column = YYRHSLOC( Rhs, 0 ).last_column; \
148 (Cur).filename = YYRHSLOC( Rhs, 0 ).filename; \
149}
[51b73452]150%}
151
[15697ff]152%define parse.error verbose
153
[201aeb9]154// Types declaration for productions
[a67b60e]155%union
156{
157 Token tok;
158 ParseNode * pn;
159 ExpressionNode * en;
160 DeclarationNode * decl;
161 DeclarationNode::Aggregate aggKey;
162 DeclarationNode::TypeClass tclass;
163 StatementNode * sn;
[135b431]164 WaitForStmt * wfs;
[e612146c]165 Expression * constant;
[936e9f4]166 IfCtl * ifctl;
[a67b60e]167 ForCtl * fctl;
168 LabelNode * label;
169 InitializerNode * in;
170 OperKinds op;
171 std::string * str;
172 bool flag;
[307a732]173 CatchStmt::Kind catch_kind;
[a67b60e]174}
175
[c11e31c]176//************************* TERMINAL TOKENS ********************************
[51b73452]177
[c11e31c]178// keywords
[51b73452]179%token TYPEDEF
[a7c90d4]180%token EXTERN STATIC AUTO REGISTER
181%token THREADLOCAL // C11
182%token INLINE FORTRAN // C99, extension ISO/IEC 9899:1999 Section J.5.9(1)
183%token NORETURN // C11
[51b73452]184%token CONST VOLATILE
[b87a5ed]185%token RESTRICT // C99
[a7c90d4]186%token ATOMIC // C11
[51d6d6a]187%token FORALL MUTEX VIRTUAL // CFA
[72457b6]188%token VOID CHAR SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED
[b87a5ed]189%token BOOL COMPLEX IMAGINARY // C99
[201aeb9]190%token INT128 FLOAT80 FLOAT128 // GCC
[72457b6]191%token ZERO_T ONE_T // CFA
192%token VALIST // GCC
[b87a5ed]193%token TYPEOF LABEL // GCC
[51b73452]194%token ENUM STRUCT UNION
[c27fb59]195%token EXCEPTION // CFA
[d3bc0ad]196%token COROUTINE MONITOR THREAD // CFA
[a7c90d4]197%token OTYPE FTYPE DTYPE TTYPE TRAIT // CFA
[5721a6d]198%token SIZEOF OFFSETOF
[b87a5ed]199%token ATTRIBUTE EXTENSION // GCC
[51b73452]200%token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
[114014c]201%token CHOOSE DISABLE ENABLE FALLTHRU FALLTHROUGH TRY CATCH CATCHRESUME FINALLY THROW THROWRESUME AT WITH WHEN WAITFOR // CFA
[b87a5ed]202%token ASM // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
[a7c90d4]203%token ALIGNAS ALIGNOF GENERIC STATICASSERT // C11
[51b73452]204
[c11e31c]205// names and constants: lexer differentiates between identifier and typedef names
[b87a5ed]206%token<tok> IDENTIFIER QUOTED_IDENTIFIER TYPEDEFname TYPEGENname
[5b2edbc]207%token<tok> TIMEOUT WOR
[b87a5ed]208%token<tok> ATTR_IDENTIFIER ATTR_TYPEDEFname ATTR_TYPEGENname
[1b29996]209%token<tok> INTEGERconstant CHARACTERconstant STRINGliteral
[61fc4f6]210%token<tok> DIRECTIVE
[1b29996]211// Floating point constant is broken into three kinds of tokens because of the ambiguity with tuple indexing and
212// overloading constants 0/1, e.g., x.1 is lexed as (x)(.1), where (.1) is a factional constant, but is semantically
213// converted into the tuple index (.)(1). e.g., 3.x
[930f69e]214%token<tok> FLOATING_DECIMALconstant FLOATING_FRACTIONconstant FLOATINGconstant
[51b73452]215
[c11e31c]216// multi-character operators
[b87a5ed]217%token ARROW // ->
218%token ICR DECR // ++ --
219%token LS RS // << >>
220%token LE GE EQ NE // <= >= == !=
221%token ANDAND OROR // && ||
222%token ELLIPSIS // ...
223
[e5f2a67]224%token EXPassign MULTassign DIVassign MODassign // \= *= /= %=
[b87a5ed]225%token PLUSassign MINUSassign // += -=
226%token LSassign RSassign // <<= >>=
227%token ANDassign ERassign ORassign // &= ^= |=
[51b73452]228
[e7aed49]229%token ATassign // @=
[097e2b0]230
[6165ce7]231%type<tok> identifier no_attr_identifier
[84d58c5]232%type<tok> identifier_or_type_name no_attr_identifier_or_type_name attr_name
[5b2edbc]233%type<tok> quasi_keyword
[ab57786]234%type<constant> string_literal
235%type<str> string_literal_list
[51b73452]236
[c11e31c]237// expressions
[d1625f8]238%type<en> constant
[b87a5ed]239%type<en> tuple tuple_expression_list
[9706554]240%type<op> ptrref_operator unary_operator assignment_operator
[b87a5ed]241%type<en> primary_expression postfix_expression unary_expression
[994d080]242%type<en> cast_expression exponential_expression multiplicative_expression additive_expression
243%type<en> shift_expression relational_expression equality_expression
244%type<en> AND_expression exclusive_OR_expression inclusive_OR_expression
245%type<en> logical_AND_expression logical_OR_expression
246%type<en> conditional_expression constant_expression assignment_expression assignment_expression_opt
[b87a5ed]247%type<en> comma_expression comma_expression_opt
[994d080]248%type<en> argument_expression_list argument_expression default_initialize_opt
[936e9f4]249%type<ifctl> if_control_expression
[2f22cc4]250%type<fctl> for_control_expression
[51b73452]251%type<en> subrange
[c0aa336]252%type<decl> asm_name_opt
[7f5566b]253%type<en> asm_operands_opt asm_operands_list asm_operand
254%type<label> label_list
[d1625f8]255%type<en> asm_clobbers_list_opt
[7f5566b]256%type<flag> asm_volatile_opt
[cbce272]257%type<en> handler_predicate_opt
[51b73452]258
[c11e31c]259// statements
[9bd6105]260%type<sn> statement labeled_statement compound_statement
261%type<sn> statement_decl statement_decl_list statement_list_nodecl
262%type<sn> selection_statement
[6a276a0]263%type<sn> switch_clause_list_opt switch_clause_list
[9bd6105]264%type<en> case_value
265%type<sn> case_clause case_value_list case_label case_label_list
[8b47e50]266%type<sn> iteration_statement jump_statement
[9bd6105]267%type<sn> expression_statement asm_statement
[c453ac4]268%type<sn> with_statement
269%type<en> with_clause_opt
[51d6d6a]270%type<sn> exception_statement handler_clause finally_clause
271%type<catch_kind> handler_key
[b6b3c42]272%type<sn> mutex_statement
[9bd6105]273%type<en> when_clause when_clause_opt waitfor timeout
[135b431]274%type<sn> waitfor_statement
[51d6d6a]275%type<wfs> waitfor_clause
[51b73452]276
[c11e31c]277// declarations
[c0aa336]278%type<decl> abstract_declarator abstract_ptr abstract_array abstract_function array_dimension multi_array_dimension
279%type<decl> abstract_parameter_declarator abstract_parameter_ptr abstract_parameter_array abstract_parameter_function array_parameter_dimension array_parameter_1st_dimension
280%type<decl> abstract_parameter_declaration
[51b73452]281
282%type<aggKey> aggregate_key
[d0ffed1]283%type<decl> aggregate_type aggregate_type_nobody
[51b73452]284
[9997fee]285%type<decl> assertion assertion_list assertion_list_opt
[51b73452]286
287%type<en> bit_subrange_size_opt bit_subrange_size
288
[84d58c5]289%type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type indirect_type
[51b73452]290
[4040425]291%type<decl> trait_declaration trait_declaration_list trait_declaring_list trait_specifier
[51b73452]292
293%type<decl> declaration declaration_list declaration_list_opt declaration_qualifier_list
[d0ffed1]294%type<decl> declaration_specifier declaration_specifier_nobody declarator declaring_list
[51b73452]295
[d0ffed1]296%type<decl> elaborated_type elaborated_type_nobody
[51b73452]297
[d0ffed1]298%type<decl> enumerator_list enum_type enum_type_nobody
[51b73452]299%type<en> enumerator_value_opt
300
301%type<decl> exception_declaration external_definition external_definition_list external_definition_list_opt
302
303%type<decl> field_declaration field_declaration_list field_declarator field_declaring_list
[8780e30]304%type<en> field field_list field_name fraction_constants
[51b73452]305
[4d51835]306%type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
[51b73452]307
[d3bc0ad]308%type<decl> identifier_parameter_declarator identifier_parameter_ptr identifier_parameter_array identifier_parameter_function
309%type<decl> identifier_list
[51b73452]310
[c0aa336]311%type<decl> cfa_abstract_array cfa_abstract_declarator_no_tuple cfa_abstract_declarator_tuple
312%type<decl> cfa_abstract_function cfa_abstract_parameter_declaration cfa_abstract_parameter_list
313%type<decl> cfa_abstract_ptr cfa_abstract_tuple
[51b73452]314
[c0aa336]315%type<decl> cfa_array_parameter_1st_dimension
[51b73452]316
[c0aa336]317%type<decl> cfa_trait_declaring_list cfa_declaration cfa_field_declaring_list
318%type<decl> cfa_function_declaration cfa_function_return cfa_function_specifier
[51b73452]319
[c0aa336]320%type<decl> cfa_identifier_parameter_array cfa_identifier_parameter_declarator_no_tuple
321%type<decl> cfa_identifier_parameter_declarator_tuple cfa_identifier_parameter_ptr
[51b73452]322
[2a8427c6]323%type<decl> cfa_parameter_declaration cfa_parameter_list cfa_parameter_type_list_opt
[51b73452]324
[c0aa336]325%type<decl> cfa_typedef_declaration cfa_variable_declaration cfa_variable_specifier
[51b73452]326
[b9be000b]327%type<decl> c_declaration static_assert
[c0aa336]328%type<decl> KR_function_declarator KR_function_no_ptr KR_function_ptr KR_function_array
329%type<decl> KR_declaration_list KR_declaration_list_opt
[51b73452]330
[2a8427c6]331%type<decl> parameter_declaration parameter_list parameter_type_list_opt
[51b73452]332
[2871210]333%type<decl> paren_identifier paren_type
[51b73452]334
[0da3e2c]335%type<decl> storage_class storage_class_list
[51b73452]336
[d0ffed1]337%type<decl> sue_declaration_specifier sue_declaration_specifier_nobody sue_type_specifier sue_type_specifier_nobody
[51b73452]338
339%type<tclass> type_class
340%type<decl> type_declarator type_declarator_name type_declaring_list
341
[84d58c5]342%type<decl> type_declaration_specifier type_type_specifier type_name typegen_name
343%type<decl> typedef typedef_declaration typedef_expression
[c0aa336]344
345%type<decl> variable_type_redeclarator type_ptr type_array type_function
346
347%type<decl> type_parameter_redeclarator type_parameter_ptr type_parameter_array type_parameter_function
[51b73452]348
[84d58c5]349%type<decl> type type_no_function
350%type<decl> type_parameter type_parameter_list type_initializer_opt
[51b73452]351
[84d58c5]352%type<en> type_list
[51b73452]353
[a16a7ec]354%type<decl> type_qualifier type_qualifier_name forall type_qualifier_list_opt type_qualifier_list
[d3bc0ad]355%type<decl> type_specifier type_specifier_nobody
[51b73452]356
[c0aa336]357%type<decl> variable_declarator variable_ptr variable_array variable_function
358%type<decl> variable_abstract_declarator variable_abstract_ptr variable_abstract_array variable_abstract_function
[51b73452]359
[44a81853]360%type<decl> attribute_list_opt attribute_list attribute_name_list attribute attribute_name
[1db21619]361
[c11e31c]362// initializers
[51b73452]363%type<in> initializer initializer_list initializer_opt
364
[c11e31c]365// designators
[51b73452]366%type<en> designator designator_list designation
367
368
[65d6de4]369// Handle shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string is ambiguous:
370// .---------. matches IF '(' comma_expression ')' statement . (reduce)
371// if ( C ) S1 else S2
372// `-----------------' matches IF '(' comma_expression ')' statement . (shift) ELSE statement */
[5b2edbc]373// Similar issues exit with the waitfor statement.
[51b73452]374
[1efa1e1]375// Order of these lines matters (low-to-high precedence). THEN is left associative over WOR/TIMEOUT/ELSE, WOR is left
376// associative over TIMEOUT/ELSE, and TIMEOUT is left associative over ELSE.
[5b2edbc]377%precedence THEN // rule precedence for IF/WAITFOR statement
378%precedence WOR // token precedence for start of WOR in WAITFOR statement
379%precedence TIMEOUT // token precedence for start of TIMEOUT in WAITFOR statement
380%precedence ELSE // token precedence for start of else clause in IF/WAITFOR statement
[51b73452]381
[65d6de4]382// Handle shift/reduce conflict for generic type by shifting the '(' token. For example, this string is ambiguous:
383// forall( otype T ) struct Foo { T v; };
384// .-----. matches pointer to function returning a generic (which is impossible without a type)
385// Foo ( *fp )( int );
386// `---' matches start of TYPEGENname '('
387// Must be:
[a16a7ec]388// Foo( int ) ( *fp )( int );
[65d6de4]389
390// Order of these lines matters (low-to-high precedence).
391%precedence TYPEGENname
392%precedence '('
393
[f38e7d7]394%locations // support location tracking for error messages
[930f69e]395
[b87a5ed]396%start translation_unit // parse-tree root
[51b73452]397
398%%
[c11e31c]399//************************* Namespace Management ********************************
400
[de62360d]401// The grammar in the ANSI C standard is not strictly context-free, since it relies upon the distinct terminal symbols
[930f69e]402// "identifier", "TYPEDEFname", and "TYPEGENname" that are lexically identical. While it is possible to write a purely
403// context-free grammar, such a grammar would obscure the relationship between syntactic and semantic constructs.
404// Hence, this grammar uses the ANSI style.
[c11e31c]405//
[de62360d]406// Cforall compounds this problem by introducing type names local to the scope of a declaration (for instance, those
[9059213]407// introduced through "forall" qualifiers), and by introducing "type generators" -- parameterized types. This latter
[de62360d]408// type name creates a third class of identifiers that must be distinguished by the scanner.
[c11e31c]409//
[de62360d]410// Since the scanner cannot distinguish among the different classes of identifiers without some context information, it
[9059213]411// accesses a data structure (TypedefTable) to allow classification of an identifier that it has just read. Semantic
412// actions during the parser update this data structure when the class of identifiers change.
[c11e31c]413//
[de62360d]414// Because the Cforall language is block-scoped, there is the possibility that an identifier can change its class in a
415// local scope; it must revert to its original class at the end of the block. Since type names can be local to a
416// particular declaration, each declaration is itself a scope. This requires distinguishing between type names that are
417// local to the current declaration scope and those that persist past the end of the declaration (i.e., names defined in
[9059213]418// "typedef" or "otype" declarations).
[c11e31c]419//
[de62360d]420// The non-terminals "push" and "pop" derive the empty string; their only use is to denote the opening and closing of
421// scopes. Every push must have a matching pop, although it is regrettable the matching pairs do not always occur
422// within the same rule. These non-terminals may appear in more contexts than strictly necessary from a semantic point
423// of view. Unfortunately, these extra rules are necessary to prevent parsing conflicts -- the parser may not have
424// enough context and look-ahead information to decide whether a new scope is necessary, so the effect of these extra
425// rules is to open a new scope unconditionally. As the grammar evolves, it may be neccesary to add or move around
426// "push" and "pop" nonterminals to resolve conflicts of this sort.
[51b73452]427
428push:
[ab57786]429 { typedefTable.enterScope(); }
[4d51835]430 ;
[51b73452]431
432pop:
[ab57786]433 { typedefTable.leaveScope(); }
[4d51835]434 ;
[51b73452]435
[c11e31c]436//************************* CONSTANTS ********************************
[51b73452]437
438constant:
[de62360d]439 // ENUMERATIONconstant is not included here; it is treated as a variable with type "enumeration constant".
[ab57786]440 INTEGERconstant { $$ = new ExpressionNode( build_constantInteger( *$1 ) ); }
[930f69e]441 | FLOATING_DECIMALconstant { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
442 | FLOATING_FRACTIONconstant { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
[ab57786]443 | FLOATINGconstant { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
444 | CHARACTERconstant { $$ = new ExpressionNode( build_constantChar( *$1 ) ); }
[4d51835]445 ;
[51b73452]446
[5b2edbc]447quasi_keyword: // CFA
448 TIMEOUT
449 | WOR
450 ;
451
[51b73452]452identifier:
[4d51835]453 IDENTIFIER
454 | ATTR_IDENTIFIER // CFA
[5b2edbc]455 | quasi_keyword
[4d51835]456 ;
[51b73452]457
458no_attr_identifier:
[4d51835]459 IDENTIFIER
[5b2edbc]460 | quasi_keyword
[4d51835]461 ;
[51b73452]462
[ab57786]463string_literal:
464 string_literal_list { $$ = build_constantStr( *$1 ); }
465 ;
466
[b87a5ed]467string_literal_list: // juxtaposed strings are concatenated
[ab57786]468 STRINGliteral { $$ = $1; } // conversion from tok to str
[7bf7fb9]469 | string_literal_list STRINGliteral
470 {
[15697ff]471 if ( ! appendStr( *$1, *$2 ) ) YYERROR; // append 2nd juxtaposed string to 1st
[7bf7fb9]472 delete $2; // allocated by lexer
[ab57786]473 $$ = $1; // conversion from tok to str
[7bf7fb9]474 }
[4d51835]475 ;
[51b73452]476
[c11e31c]477//************************* EXPRESSIONS ********************************
[51b73452]478
479primary_expression:
[4d51835]480 IDENTIFIER // typedef name cannot be used as a variable name
[d1625f8]481 { $$ = new ExpressionNode( build_varref( $1 ) ); }
[5b2edbc]482 | quasi_keyword
483 { $$ = new ExpressionNode( build_varref( $1 ) ); }
[1b29996]484 | tuple
[4d51835]485 | '(' comma_expression ')'
486 { $$ = $2; }
487 | '(' compound_statement ')' // GCC, lambda expression
[513e165]488 { $$ = new ExpressionNode( new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >($2) ) ) ); }
[7e419e7]489 | constant '`' IDENTIFIER // CFA, postfix call
[dc7db63]490 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $3 ) ), $1 ) ); }
[7e419e7]491 | string_literal '`' IDENTIFIER // CFA, postfix call
[dc7db63]492 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $3 ) ), new ExpressionNode( $1 ) ) ); }
[7e419e7]493 | IDENTIFIER '`' IDENTIFIER // CFA, postfix call
[dc7db63]494 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $3 ) ), new ExpressionNode( build_varref( $1 ) ) ) ); }
[7e419e7]495 | tuple '`' IDENTIFIER // CFA, postfix call
[dc7db63]496 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $3 ) ), $1 ) ); }
[7e419e7]497 | '(' comma_expression ')' '`' IDENTIFIER // CFA, postfix call
[dc7db63]498 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $5 ) ), $2 ) ); }
[84d58c5]499 | type_name '.' no_attr_identifier // CFA, nested type
[fae90d5f]500 { SemanticError( yylloc, "Qualified names are currently unimplemented." ); $$ = nullptr; }
[84d58c5]501 | type_name '.' '[' push field_list pop ']' // CFA, nested type / tuple field selector
[fae90d5f]502 { SemanticError( yylloc, "Qualified names are currently unimplemented." ); $$ = nullptr; }
[24c3b67]503 | GENERIC '(' assignment_expression ',' generic_assoc_list ')' // C11
[fae90d5f]504 { SemanticError( yylloc, "_Generic is currently unimplemented." ); $$ = nullptr; }
[24c3b67]505 ;
506
507generic_assoc_list: // C11
508 | generic_association
509 | generic_assoc_list ',' generic_association
510 ;
511
512generic_association: // C11
513 type_no_function ':' assignment_expression
514 | DEFAULT ':' assignment_expression
[4d51835]515 ;
[51b73452]516
517postfix_expression:
[4d51835]518 primary_expression
519 | postfix_expression '[' push assignment_expression pop ']'
[c6b1105]520 // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a
[de62360d]521 // matrix with x[i,j] instead of x[i][j]. While this change is not backwards compatible, there seems to be
522 // little advantage to this feature and many disadvantages. It is possible to write x[(i,j)] in CFA, which is
523 // equivalent to the old x[i,j].
[d1625f8]524 { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, $1, $4 ) ); }
[bd3d9e4]525 | postfix_expression '{' argument_expression_list '}' // CFA, constructor call
526 {
527 Token fn;
528 fn.str = new std::string( "?{}" ); // location undefined - use location of '{'?
529 $$ = new ExpressionNode( new ConstructorExpr( build_func( new ExpressionNode( build_varref( fn ) ), (ExpressionNode *)( $1 )->set_last( $3 ) ) ) );
530 }
[4d51835]531 | postfix_expression '(' argument_expression_list ')'
[d1625f8]532 { $$ = new ExpressionNode( build_func( $1, $3 ) ); }
[4d51835]533 | postfix_expression '.' no_attr_identifier
[d1625f8]534 { $$ = new ExpressionNode( build_fieldSel( $1, build_varref( $3 ) ) ); }
[4d51835]535 | postfix_expression '.' '[' push field_list pop ']' // CFA, tuple field selector
[3b58d91]536 { $$ = new ExpressionNode( build_fieldSel( $1, build_tuple( $5 ) ) ); }
[930f69e]537 | postfix_expression FLOATING_FRACTIONconstant // CFA, tuple index
538 { $$ = new ExpressionNode( build_fieldSel( $1, build_field_name_FLOATING_FRACTIONconstant( *$2 ) ) ); }
[4d51835]539 | postfix_expression ARROW no_attr_identifier
[861799c7]540 {
541 $$ = new ExpressionNode( build_pfieldSel( $1, *$3 == "0" || *$3 == "1" ? build_constantInteger( *$3 ) : build_varref( $3 ) ) );
542 }
[4d51835]543 | postfix_expression ARROW '[' push field_list pop ']' // CFA, tuple field selector
[3b58d91]544 { $$ = new ExpressionNode( build_pfieldSel( $1, build_tuple( $5 ) ) ); }
[861799c7]545 | postfix_expression ARROW INTEGERconstant // CFA, tuple index
546 { $$ = new ExpressionNode( build_pfieldSel( $1, build_constantInteger( *$3 ) ) ); }
[4d51835]547 | postfix_expression ICR
[d1625f8]548 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::IncrPost, $1 ) ); }
[4d51835]549 | postfix_expression DECR
[d1625f8]550 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::DecrPost, $1 ) ); }
[84d58c5]551 | '(' type_no_function ')' '{' initializer_list comma_opt '}' // C99, compound-literal
[d1625f8]552 { $$ = new ExpressionNode( build_compoundLiteral( $2, new InitializerNode( $5, true ) ) ); }
[e6b3942]553 | '(' type_no_function ')' '@' '{' initializer_list comma_opt '}' // CFA, explicit C compound-literal
[f810e09]554 { $$ = new ExpressionNode( build_compoundLiteral( $2, (new InitializerNode( $6, true ))->set_maybeConstructed( false ) ) ); }
[ecb27a7]555 | '^' primary_expression '{' argument_expression_list '}' // CFA
[097e2b0]556 {
[9706554]557 Token fn;
[ecb27a7]558 fn.str = new string( "^?{}" ); // location undefined
559 $$ = new ExpressionNode( build_func( new ExpressionNode( build_varref( fn ) ), (ExpressionNode *)( $2 )->set_last( $4 ) ) );
[097e2b0]560 }
[4d51835]561 ;
[51b73452]562
563argument_expression_list:
[4d51835]564 argument_expression
565 | argument_expression_list ',' argument_expression
[1d4580a]566 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]567 ;
[51b73452]568
569argument_expression:
[4d51835]570 // empty
[738e304]571 { $$ = nullptr; }
572 // | '@' // use default argument
573 // { $$ = new ExpressionNode( build_constantInteger( *new string( "2" ) ) ); }
[4d51835]574 | assignment_expression
575 ;
[b87a5ed]576
577field_list: // CFA, tuple field selector
[4d51835]578 field
[1d4580a]579 | field_list ',' field { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]580 ;
[b87a5ed]581
582field: // CFA, tuple field selector
[faddbd8]583 field_name
[930f69e]584 | FLOATING_DECIMALconstant field
585 { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), maybeMoveBuild<Expression>( $2 ) ) ); }
586 | FLOATING_DECIMALconstant '[' push field_list pop ']'
587 { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), build_tuple( $4 ) ) ); }
[faddbd8]588 | field_name '.' field
[bf32bb8]589 { $$ = new ExpressionNode( build_fieldSel( $1, maybeMoveBuild<Expression>( $3 ) ) ); }
[faddbd8]590 | field_name '.' '[' push field_list pop ']'
[bf32bb8]591 { $$ = new ExpressionNode( build_fieldSel( $1, build_tuple( $5 ) ) ); }
[faddbd8]592 | field_name ARROW field
[bf32bb8]593 { $$ = new ExpressionNode( build_pfieldSel( $1, maybeMoveBuild<Expression>( $3 ) ) ); }
[faddbd8]594 | field_name ARROW '[' push field_list pop ']'
[bf32bb8]595 { $$ = new ExpressionNode( build_pfieldSel( $1, build_tuple( $5 ) ) ); }
[4d51835]596 ;
[51b73452]597
[faddbd8]598field_name:
[1b29996]599 INTEGERconstant fraction_constants
[8780e30]600 { $$ = new ExpressionNode( build_field_name_fraction_constants( build_constantInteger( *$1 ), $2 ) ); }
[1b29996]601 | FLOATINGconstant fraction_constants
[8780e30]602 { $$ = new ExpressionNode( build_field_name_fraction_constants( build_field_name_FLOATINGconstant( *$1 ), $2 ) ); }
[1b29996]603 | no_attr_identifier fraction_constants
[4cb935e]604 {
[84d58c5]605 $$ = new ExpressionNode( build_field_name_fraction_constants( build_varref( $1 ), $2 ) );
606 }
[1b29996]607 ;
608
609fraction_constants:
610 // empty
[8780e30]611 { $$ = nullptr; }
[930f69e]612 | fraction_constants FLOATING_FRACTIONconstant
[8780e30]613 {
[930f69e]614 Expression * constant = build_field_name_FLOATING_FRACTIONconstant( *$2 );
[8780e30]615 $$ = $1 != nullptr ? new ExpressionNode( build_fieldSel( $1, constant ) ) : new ExpressionNode( constant );
616 }
[faddbd8]617 ;
618
[51b73452]619unary_expression:
[4d51835]620 postfix_expression
[c6b1105]621 // first location where constant/string can have operator applied: sizeof 3/sizeof "abc" still requires
622 // semantics checks, e.g., ++3, 3--, *3, &&3
[51b1202]623 | constant
624 { $$ = $1; }
[ab57786]625 | string_literal
[d1625f8]626 { $$ = new ExpressionNode( $1 ); }
[4d51835]627 | EXTENSION cast_expression // GCC
[e04ef3a]628 { $$ = $2->set_extension( true ); }
[c6b1105]629 // '*' ('&') is separated from unary_operator because of shift/reduce conflict in:
630 // { * X; } // dereference X
631 // { * int X; } // CFA declaration of pointer to int
[51e076e]632 | ptrref_operator cast_expression // CFA
[9706554]633 {
634 switch ( $1 ) {
[d9e2280]635 case OperKinds::AddressOf:
[db70fe4]636 $$ = new ExpressionNode( new AddressExpr( maybeMoveBuild< Expression >( $2 ) ) );
[9706554]637 break;
[d9e2280]638 case OperKinds::PointTo:
[d1625f8]639 $$ = new ExpressionNode( build_unary_val( $1, $2 ) );
[9706554]640 break;
[5809461]641 case OperKinds::And:
[db70fe4]642 $$ = new ExpressionNode( new AddressExpr( new AddressExpr( maybeMoveBuild< Expression >( $2 ) ) ) );
[5809461]643 break;
[9706554]644 default:
645 assert( false );
646 }
647 }
[4d51835]648 | unary_operator cast_expression
[d1625f8]649 { $$ = new ExpressionNode( build_unary_val( $1, $2 ) ); }
[dd51906]650 | ICR unary_expression
[d1625f8]651 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Incr, $2 ) ); }
[dd51906]652 | DECR unary_expression
[d1625f8]653 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Decr, $2 ) ); }
[4d51835]654 | SIZEOF unary_expression
[db70fe4]655 { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuild< Expression >( $2 ) ) ); }
[84d58c5]656 | SIZEOF '(' type_no_function ')'
[db70fe4]657 { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuildType( $3 ) ) ); }
[d1625f8]658 | ALIGNOF unary_expression // GCC, variable alignment
[db70fe4]659 { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuild< Expression >( $2 ) ) ); }
[a2e0687]660 | ALIGNOF '(' type_no_function ')' // GCC, type alignment
[db70fe4]661 { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuildType( $3 ) ) ); }
[84d58c5]662 | OFFSETOF '(' type_no_function ',' no_attr_identifier ')'
[d1625f8]663 { $$ = new ExpressionNode( build_offsetOf( $3, build_varref( $5 ) ) ); }
[4d51835]664 | ATTR_IDENTIFIER
[db70fe4]665 { $$ = new ExpressionNode( new AttrExpr( build_varref( $1 ), maybeMoveBuild< Expression >( (ExpressionNode *)nullptr ) ) ); }
[4d51835]666 | ATTR_IDENTIFIER '(' argument_expression ')'
[db70fe4]667 { $$ = new ExpressionNode( new AttrExpr( build_varref( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
[84d58c5]668 | ATTR_IDENTIFIER '(' type ')'
[db70fe4]669 { $$ = new ExpressionNode( new AttrExpr( build_varref( $1 ), maybeMoveBuildType( $3 ) ) ); }
[4d51835]670 ;
[51b73452]671
[dd51906]672ptrref_operator:
[d9e2280]673 '*' { $$ = OperKinds::PointTo; }
674 | '&' { $$ = OperKinds::AddressOf; }
[c6b1105]675 // GCC, address of label must be handled by semantic check for ref,ref,label
[9f07232]676 | ANDAND { $$ = OperKinds::And; }
[dd51906]677 ;
678
[51b73452]679unary_operator:
[d9e2280]680 '+' { $$ = OperKinds::UnPlus; }
681 | '-' { $$ = OperKinds::UnMinus; }
682 | '!' { $$ = OperKinds::Neg; }
683 | '~' { $$ = OperKinds::BitNeg; }
[4d51835]684 ;
[51b73452]685
686cast_expression:
[4d51835]687 unary_expression
[84d58c5]688 | '(' type_no_function ')' cast_expression
[d1625f8]689 { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
[fae90d5f]690 | '(' COROUTINE '&' ')' cast_expression // CFA
[9a705dc8]691 { $$ = new ExpressionNode( build_keyword_cast( KeywordCastExpr::Coroutine, $5 ) ); }
[fae90d5f]692 | '(' THREAD '&' ')' cast_expression // CFA
[9a705dc8]693 { $$ = new ExpressionNode( build_keyword_cast( KeywordCastExpr::Thread, $5 ) ); }
[fae90d5f]694 | '(' MONITOR '&' ')' cast_expression // CFA
[9a705dc8]695 { $$ = new ExpressionNode( build_keyword_cast( KeywordCastExpr::Monitor, $5 ) ); }
[72457b6]696 // VIRTUAL cannot be opt because of look ahead issues
[fae90d5f]697 | '(' VIRTUAL ')' cast_expression // CFA
[513e165]698 { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild< Expression >( $4 ), maybeMoveBuildType( nullptr ) ) ); }
[fae90d5f]699 | '(' VIRTUAL type_no_function ')' cast_expression // CFA
[513e165]700 { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild< Expression >( $5 ), maybeMoveBuildType( $3 ) ) ); }
[84d58c5]701// | '(' type_no_function ')' tuple
[1b29996]702// { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
[4d51835]703 ;
[51b73452]704
[994d080]705exponential_expression:
[4d51835]706 cast_expression
[994d080]707 | exponential_expression '\\' cast_expression
[e5f2a67]708 { $$ = new ExpressionNode( build_binary_val( OperKinds::Exp, $1, $3 ) ); }
[994d080]709 ;
710
711multiplicative_expression:
712 exponential_expression
713 | multiplicative_expression '*' exponential_expression
[d1625f8]714 { $$ = new ExpressionNode( build_binary_val( OperKinds::Mul, $1, $3 ) ); }
[994d080]715 | multiplicative_expression '/' exponential_expression
[d1625f8]716 { $$ = new ExpressionNode( build_binary_val( OperKinds::Div, $1, $3 ) ); }
[994d080]717 | multiplicative_expression '%' exponential_expression
[d1625f8]718 { $$ = new ExpressionNode( build_binary_val( OperKinds::Mod, $1, $3 ) ); }
[4d51835]719 ;
[51b73452]720
721additive_expression:
[4d51835]722 multiplicative_expression
723 | additive_expression '+' multiplicative_expression
[d1625f8]724 { $$ = new ExpressionNode( build_binary_val( OperKinds::Plus, $1, $3 ) ); }
[4d51835]725 | additive_expression '-' multiplicative_expression
[d1625f8]726 { $$ = new ExpressionNode( build_binary_val( OperKinds::Minus, $1, $3 ) ); }
[4d51835]727 ;
[51b73452]728
729shift_expression:
[4d51835]730 additive_expression
731 | shift_expression LS additive_expression
[d1625f8]732 { $$ = new ExpressionNode( build_binary_val( OperKinds::LShift, $1, $3 ) ); }
[4d51835]733 | shift_expression RS additive_expression
[d1625f8]734 { $$ = new ExpressionNode( build_binary_val( OperKinds::RShift, $1, $3 ) ); }
[4d51835]735 ;
[51b73452]736
737relational_expression:
[4d51835]738 shift_expression
739 | relational_expression '<' shift_expression
[d1625f8]740 { $$ = new ExpressionNode( build_binary_val( OperKinds::LThan, $1, $3 ) ); }
[4d51835]741 | relational_expression '>' shift_expression
[d1625f8]742 { $$ = new ExpressionNode( build_binary_val( OperKinds::GThan, $1, $3 ) ); }
[4d51835]743 | relational_expression LE shift_expression
[d1625f8]744 { $$ = new ExpressionNode( build_binary_val( OperKinds::LEThan, $1, $3 ) ); }
[4d51835]745 | relational_expression GE shift_expression
[d1625f8]746 { $$ = new ExpressionNode( build_binary_val( OperKinds::GEThan, $1, $3 ) ); }
[4d51835]747 ;
[51b73452]748
749equality_expression:
[4d51835]750 relational_expression
751 | equality_expression EQ relational_expression
[d1625f8]752 { $$ = new ExpressionNode( build_binary_val( OperKinds::Eq, $1, $3 ) ); }
[4d51835]753 | equality_expression NE relational_expression
[d1625f8]754 { $$ = new ExpressionNode( build_binary_val( OperKinds::Neq, $1, $3 ) ); }
[4d51835]755 ;
[51b73452]756
757AND_expression:
[4d51835]758 equality_expression
759 | AND_expression '&' equality_expression
[d1625f8]760 { $$ = new ExpressionNode( build_binary_val( OperKinds::BitAnd, $1, $3 ) ); }
[4d51835]761 ;
[51b73452]762
763exclusive_OR_expression:
[4d51835]764 AND_expression
765 | exclusive_OR_expression '^' AND_expression
[d1625f8]766 { $$ = new ExpressionNode( build_binary_val( OperKinds::Xor, $1, $3 ) ); }
[4d51835]767 ;
[51b73452]768
769inclusive_OR_expression:
[4d51835]770 exclusive_OR_expression
771 | inclusive_OR_expression '|' exclusive_OR_expression
[d1625f8]772 { $$ = new ExpressionNode( build_binary_val( OperKinds::BitOr, $1, $3 ) ); }
[4d51835]773 ;
[51b73452]774
775logical_AND_expression:
[4d51835]776 inclusive_OR_expression
777 | logical_AND_expression ANDAND inclusive_OR_expression
[d1625f8]778 { $$ = new ExpressionNode( build_and_or( $1, $3, true ) ); }
[4d51835]779 ;
[51b73452]780
781logical_OR_expression:
[4d51835]782 logical_AND_expression
783 | logical_OR_expression OROR logical_AND_expression
[d1625f8]784 { $$ = new ExpressionNode( build_and_or( $1, $3, false ) ); }
[4d51835]785 ;
[51b73452]786
787conditional_expression:
[4d51835]788 logical_OR_expression
789 | logical_OR_expression '?' comma_expression ':' conditional_expression
[d1625f8]790 { $$ = new ExpressionNode( build_cond( $1, $3, $5 ) ); }
[fae90d5f]791 // FIX ME: computes $1 twice
[4d51835]792 | logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, omitted first operand
[d1625f8]793 { $$ = new ExpressionNode( build_cond( $1, $1, $4 ) ); }
[4d51835]794 ;
[51b73452]795
796constant_expression:
[4d51835]797 conditional_expression
798 ;
[51b73452]799
800assignment_expression:
[4d51835]801 // CFA, assignment is separated from assignment_operator to ensure no assignment operations for tuples
802 conditional_expression
803 | unary_expression assignment_operator assignment_expression
[cda7889]804 { $$ = new ExpressionNode( build_binary_val( $2, $1, $3 ) ); }
[2a8427c6]805 | unary_expression '=' '{' initializer_list comma_opt '}'
[fae90d5f]806 { SemanticError( yylloc, "Initializer assignment is currently unimplemented." ); $$ = nullptr; }
[4d51835]807 ;
[51b73452]808
809assignment_expression_opt:
[4d51835]810 // empty
[d1625f8]811 { $$ = nullptr; }
[4d51835]812 | assignment_expression
813 ;
[b87a5ed]814
[9706554]815assignment_operator:
[d9e2280]816 '=' { $$ = OperKinds::Assign; }
[a839867]817 | ATassign { $$ = OperKinds::AtAssn; }
[e5f2a67]818 | EXPassign { $$ = OperKinds::ExpAssn; }
[d9e2280]819 | MULTassign { $$ = OperKinds::MulAssn; }
820 | DIVassign { $$ = OperKinds::DivAssn; }
821 | MODassign { $$ = OperKinds::ModAssn; }
822 | PLUSassign { $$ = OperKinds::PlusAssn; }
823 | MINUSassign { $$ = OperKinds::MinusAssn; }
824 | LSassign { $$ = OperKinds::LSAssn; }
825 | RSassign { $$ = OperKinds::RSAssn; }
826 | ANDassign { $$ = OperKinds::AndAssn; }
827 | ERassign { $$ = OperKinds::ERAssn; }
828 | ORassign { $$ = OperKinds::OrAssn; }
[413ad05]829 ;
[9706554]830
[b87a5ed]831tuple: // CFA, tuple
[de62360d]832 // CFA, one assignment_expression is factored out of comma_expression to eliminate a shift/reduce conflict with
[c0aa336]833 // comma_expression in cfa_identifier_parameter_array and cfa_abstract_array
[1b29996]834// '[' ']'
835// { $$ = new ExpressionNode( build_tuple() ); }
836// '[' push assignment_expression pop ']'
837// { $$ = new ExpressionNode( build_tuple( $3 ) ); }
838 '[' push ',' tuple_expression_list pop ']'
[1d4580a]839 { $$ = new ExpressionNode( build_tuple( (ExpressionNode *)(new ExpressionNode( nullptr ) )->set_last( $4 ) ) ); }
[4d51835]840 | '[' push assignment_expression ',' tuple_expression_list pop ']'
[1d4580a]841 { $$ = new ExpressionNode( build_tuple( (ExpressionNode *)$3->set_last( $5 ) ) ); }
[4d51835]842 ;
[51b73452]843
844tuple_expression_list:
[4d51835]845 assignment_expression_opt
846 | tuple_expression_list ',' assignment_expression_opt
[1d4580a]847 { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]848 ;
[51b73452]849
850comma_expression:
[4d51835]851 assignment_expression
[9706554]852 | comma_expression ',' assignment_expression
[513e165]853 { $$ = new ExpressionNode( new CommaExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
[4d51835]854 ;
[51b73452]855
856comma_expression_opt:
[4d51835]857 // empty
[58dd019]858 { $$ = nullptr; }
[4d51835]859 | comma_expression
860 ;
[51b73452]861
[c11e31c]862//*************************** STATEMENTS *******************************
[51b73452]863
864statement:
[4d51835]865 labeled_statement
866 | compound_statement
867 | expression_statement { $$ = $1; }
868 | selection_statement
869 | iteration_statement
870 | jump_statement
[8b47e50]871 | with_statement
[b6b3c42]872 | mutex_statement
[5b2edbc]873 | waitfor_statement
[4d51835]874 | exception_statement
[2a8427c6]875 | enable_disable_statement
[fae90d5f]876 { SemanticError( yylloc, "enable/disable statement is currently unimplemented." ); $$ = nullptr; }
[4d51835]877 | asm_statement
[61fc4f6]878 | DIRECTIVE
[6d539f83]879 { $$ = new StatementNode( build_directive( $1 ) ); }
[b9be000b]880 ;
[51b73452]881
882labeled_statement:
[44a81853]883 // labels cannot be identifiers 0 or 1 or ATTR_IDENTIFIER
884 identifier_or_type_name ':' attribute_list_opt statement
[1db21619]885 {
[44a81853]886 $$ = $4->add_label( $1, $3 );
[1db21619]887 }
[4d51835]888 ;
[51b73452]889
890compound_statement:
[4d51835]891 '{' '}'
[e82aa9df]892 { $$ = new StatementNode( build_compound( (StatementNode *)0 ) ); }
[4d51835]893 | '{'
[de62360d]894 // Two scopes are necessary because the block itself has a scope, but every declaration within the block also
[3a2128f]895 // requires its own scope.
[4d51835]896 push push
[51b1202]897 local_label_declaration_opt // GCC, local labels
[9bd6105]898 statement_decl_list // C99, intermix declarations and statements
[c0aa336]899 pop '}'
[e82aa9df]900 { $$ = new StatementNode( build_compound( $5 ) ); }
[4d51835]901 ;
[b87a5ed]902
[9bd6105]903statement_decl_list: // C99
904 statement_decl
905 | statement_decl_list push statement_decl
[1d4580a]906 { if ( $1 != 0 ) { $1->set_last( $3 ); $$ = $1; } }
[4d51835]907 ;
[51b73452]908
[9bd6105]909statement_decl:
[4d51835]910 declaration // CFA, new & old style declarations
[e82aa9df]911 { $$ = new StatementNode( $1 ); }
[4d51835]912 | EXTENSION declaration // GCC
[c0aa336]913 {
914 distExt( $2 );
[e82aa9df]915 $$ = new StatementNode( $2 );
[8e9cbb2]916 }
[4d51835]917 | function_definition
[e82aa9df]918 { $$ = new StatementNode( $1 ); }
[c0aa336]919 | EXTENSION function_definition // GCC
920 {
921 distExt( $2 );
922 $$ = new StatementNode( $2 );
923 }
[4d51835]924 | statement pop
925 ;
[51b73452]926
[9bd6105]927statement_list_nodecl:
[4d51835]928 statement
[9bd6105]929 | statement_list_nodecl statement
[1d4580a]930 { if ( $1 != 0 ) { $1->set_last( $2 ); $$ = $1; } }
[4d51835]931 ;
[51b73452]932
933expression_statement:
[4d51835]934 comma_expression_opt ';'
[e82aa9df]935 { $$ = new StatementNode( build_expr( $1 ) ); }
[4d51835]936 ;
[51b73452]937
938selection_statement:
[51d6d6a]939 IF '(' push if_control_expression ')' statement %prec THEN
[4d51835]940 // explicitly deal with the shift/reduce conflict on if/else
[6d49ea3]941 { $$ = new StatementNode( build_if( $4, $6, nullptr ) ); }
942 | IF '(' push if_control_expression ')' statement ELSE statement
943 { $$ = new StatementNode( build_if( $4, $6, $8 ) ); }
[4cc585b]944 | SWITCH '(' comma_expression ')' case_clause
[6a276a0]945 { $$ = new StatementNode( build_switch( true, $3, $5 ) ); }
[4d51835]946 | SWITCH '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt '}' // CFA
[4e06c1e]947 {
[6a276a0]948 StatementNode *sw = new StatementNode( build_switch( true, $3, $8 ) );
[4e06c1e]949 // The semantics of the declaration list is changed to include associated initialization, which is performed
950 // *before* the transfer to the appropriate case clause by hoisting the declarations into a compound
951 // statement around the switch. Statements after the initial declaration list can never be executed, and
[8688ce1]952 // therefore, are removed from the grammar even though C allows it. The change also applies to choose
953 // statement.
[a7741435]954 $$ = $7 ? new StatementNode( build_compound( (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
[4e06c1e]955 }
[4d51835]956 | CHOOSE '(' comma_expression ')' case_clause // CFA
[6a276a0]957 { $$ = new StatementNode( build_switch( false, $3, $5 ) ); }
958 | CHOOSE '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt '}' // CFA
[4e06c1e]959 {
[6a276a0]960 StatementNode *sw = new StatementNode( build_switch( false, $3, $8 ) );
[a7741435]961 $$ = $7 ? new StatementNode( build_compound( (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
[4e06c1e]962 }
[4d51835]963 ;
[b87a5ed]964
[936e9f4]965if_control_expression:
[6d49ea3]966 comma_expression pop
[936e9f4]967 { $$ = new IfCtl( nullptr, $1 ); }
[4cc585b]968 | c_declaration pop // no semi-colon
[936e9f4]969 { $$ = new IfCtl( $1, nullptr ); }
[4cc585b]970 | cfa_declaration pop // no semi-colon
[936e9f4]971 { $$ = new IfCtl( $1, nullptr ); }
[6d49ea3]972 | declaration comma_expression // semi-colon separated
[936e9f4]973 { $$ = new IfCtl( $1, $2 ); }
974 ;
975
[de62360d]976// CASE and DEFAULT clauses are only allowed in the SWITCH statement, precluding Duff's device. In addition, a case
977// clause allows a list of values and subranges.
[b87a5ed]978
979case_value: // CFA
[4d51835]980 constant_expression { $$ = $1; }
981 | constant_expression ELLIPSIS constant_expression // GCC, subrange
[db70fe4]982 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
[4d51835]983 | subrange // CFA, subrange
984 ;
[b87a5ed]985
986case_value_list: // CFA
[e82aa9df]987 case_value { $$ = new StatementNode( build_case( $1 ) ); }
[064e3ff]988 // convert case list, e.g., "case 1, 3, 5:" into "case 1: case 3: case 5"
[e82aa9df]989 | case_value_list ',' case_value { $$ = (StatementNode *)($1->set_last( new StatementNode( build_case( $3 ) ) ) ); }
[4d51835]990 ;
[b87a5ed]991
992case_label: // CFA
[8688ce1]993 CASE case_value_list ':' { $$ = $2; }
[e82aa9df]994 | DEFAULT ':' { $$ = new StatementNode( build_default() ); }
[4d51835]995 // A semantic check is required to ensure only one default clause per switch/choose statement.
996 ;
[b87a5ed]997
[6a276a0]998//label_list_opt:
999// // empty
1000// | identifier_or_type_name ':'
1001// | label_list_opt identifier_or_type_name ':'
1002// ;
1003
[b87a5ed]1004case_label_list: // CFA
[4d51835]1005 case_label
[1d4580a]1006 | case_label_list case_label { $$ = (StatementNode *)( $1->set_last( $2 )); }
[4d51835]1007 ;
[b87a5ed]1008
1009case_clause: // CFA
[e82aa9df]1010 case_label_list statement { $$ = $1->append_last_case( new StatementNode( build_compound( $2 ) ) ); }
[4d51835]1011 ;
[b87a5ed]1012
1013switch_clause_list_opt: // CFA
[4d51835]1014 // empty
[58dd019]1015 { $$ = nullptr; }
[4d51835]1016 | switch_clause_list
1017 ;
[b87a5ed]1018
1019switch_clause_list: // CFA
[9bd6105]1020 case_label_list statement_list_nodecl
[e82aa9df]1021 { $$ = $1->append_last_case( new StatementNode( build_compound( $2 ) ) ); }
[9bd6105]1022 | switch_clause_list case_label_list statement_list_nodecl
[e82aa9df]1023 { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( new StatementNode( build_compound( $3 ) ) ) ) ); }
[4d51835]1024 ;
[b87a5ed]1025
[51b73452]1026iteration_statement:
[4d51835]1027 WHILE '(' comma_expression ')' statement
[e82aa9df]1028 { $$ = new StatementNode( build_while( $3, $5 ) ); }
[4d51835]1029 | DO statement WHILE '(' comma_expression ')' ';'
[5b639ee]1030 { $$ = new StatementNode( build_while( $5, $2, true ) ); }
[4d51835]1031 | FOR '(' push for_control_expression ')' statement
[e82aa9df]1032 { $$ = new StatementNode( build_for( $4, $6 ) ); }
[4d51835]1033 ;
[51b73452]1034
1035for_control_expression:
[4d51835]1036 comma_expression_opt pop ';' comma_expression_opt ';' comma_expression_opt
[2f22cc4]1037 { $$ = new ForCtl( $1, $4, $6 ); }
[4d51835]1038 | declaration comma_expression_opt ';' comma_expression_opt // C99
[2f22cc4]1039 { $$ = new ForCtl( $1, $2, $4 ); }
[d1625f8]1040 ;
[51b73452]1041
1042jump_statement:
[44a81853]1043 GOTO identifier_or_type_name ';'
[ab57786]1044 { $$ = new StatementNode( build_branch( $2, BranchStmt::Goto ) ); }
[4d51835]1045 | GOTO '*' comma_expression ';' // GCC, computed goto
[4e06c1e]1046 // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
[de62360d]1047 // whereas normal operator precedence yields goto (*i)+3;
[e82aa9df]1048 { $$ = new StatementNode( build_computedgoto( $3 ) ); }
[6a276a0]1049 // A semantic check is required to ensure fallthru appears only in the body of a choose statement.
1050 | fall_through_name ';' // CFA
1051 { $$ = new StatementNode( build_branch( BranchStmt::FallThrough ) ); }
1052 | fall_through_name identifier_or_type_name ';' // CFA
1053 { $$ = new StatementNode( build_branch( $2, BranchStmt::FallThrough ) ); }
1054 | fall_through_name DEFAULT ';' // CFA
1055 { $$ = new StatementNode( build_branch( BranchStmt::FallThroughDefault ) ); }
[4d51835]1056 | CONTINUE ';'
[de62360d]1057 // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
[ab57786]1058 { $$ = new StatementNode( build_branch( BranchStmt::Continue ) ); }
[44a81853]1059 | CONTINUE identifier_or_type_name ';' // CFA, multi-level continue
[de62360d]1060 // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1061 // the target of the transfer appears only at the start of an iteration statement.
[ab57786]1062 { $$ = new StatementNode( build_branch( $2, BranchStmt::Continue ) ); }
[4d51835]1063 | BREAK ';'
[de62360d]1064 // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
[ab57786]1065 { $$ = new StatementNode( build_branch( BranchStmt::Break ) ); }
[44a81853]1066 | BREAK identifier_or_type_name ';' // CFA, multi-level exit
[de62360d]1067 // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1068 // the target of the transfer appears only at the start of an iteration statement.
[ab57786]1069 { $$ = new StatementNode( build_branch( $2, BranchStmt::Break ) ); }
[4d51835]1070 | RETURN comma_expression_opt ';'
[e82aa9df]1071 { $$ = new StatementNode( build_return( $2 ) ); }
[2a8427c6]1072 | RETURN '{' initializer_list comma_opt '}'
[fae90d5f]1073 { SemanticError( yylloc, "Initializer return is currently unimplemented." ); $$ = nullptr; }
[8cc5cb0]1074 | THROW assignment_expression_opt ';' // handles rethrow
[e82aa9df]1075 { $$ = new StatementNode( build_throw( $2 ) ); }
[8cc5cb0]1076 | THROWRESUME assignment_expression_opt ';' // handles reresume
[daf1af8]1077 { $$ = new StatementNode( build_resume( $2 ) ); }
[8cc5cb0]1078 | THROWRESUME assignment_expression_opt AT assignment_expression ';' // handles reresume
[daf1af8]1079 { $$ = new StatementNode( build_resume_at( $2, $4 ) ); }
[4d51835]1080 ;
[51b73452]1081
[6a276a0]1082fall_through_name: // CFA
1083 FALLTHRU
1084 | FALLTHROUGH
1085 ;
1086
[8b47e50]1087with_statement:
[5b2edbc]1088 WITH '(' tuple_expression_list ')' statement
[a378ca7]1089 {
1090 $$ = new StatementNode( build_with( $3, $5 ) );
1091 }
[5b2edbc]1092 ;
1093
[b6b3c42]1094// If MUTEX becomes a general qualifier, there are shift/reduce conflicts, so change syntax to "with mutex".
1095mutex_statement:
1096 MUTEX '(' argument_expression_list ')' statement
[fae90d5f]1097 { SemanticError( yylloc, "Mutex statement is currently unimplemented." ); $$ = nullptr; }
[b6b3c42]1098 ;
1099
[51d6d6a]1100when_clause:
[6a276a0]1101 WHEN '(' comma_expression ')' { $$ = $3; }
[51d6d6a]1102 ;
1103
[5b2edbc]1104when_clause_opt:
1105 // empty
[135b431]1106 { $$ = nullptr; }
[51d6d6a]1107 | when_clause
[5b2edbc]1108 ;
1109
1110waitfor:
1111 WAITFOR '(' identifier ')'
[135b431]1112 {
1113 $$ = new ExpressionNode( new NameExpr( *$3 ) );
1114 delete $3;
1115 }
[5b2edbc]1116 | WAITFOR '(' identifier ',' argument_expression_list ')'
[135b431]1117 {
1118 $$ = new ExpressionNode( new NameExpr( *$3 ) );
1119 $$->set_last( $5 );
1120 delete $3;
1121 }
[5b2edbc]1122 ;
1123
1124timeout:
[6a276a0]1125 TIMEOUT '(' comma_expression ')' { $$ = $3; }
[5b2edbc]1126 ;
1127
1128waitfor_clause:
[51d6d6a]1129 when_clause_opt waitfor statement %prec THEN
[135b431]1130 { $$ = build_waitfor( $2, $3, $1 ); }
[5b2edbc]1131 | when_clause_opt waitfor statement WOR waitfor_clause
[135b431]1132 { $$ = build_waitfor( $2, $3, $1, $5 ); }
[51d6d6a]1133 | when_clause_opt timeout statement %prec THEN
[135b431]1134 { $$ = build_waitfor_timeout( $2, $3, $1 ); }
[5b2edbc]1135 | when_clause_opt ELSE statement
[135b431]1136 { $$ = build_waitfor_timeout( nullptr, $3, $1 ); }
[51d6d6a]1137 // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless)
1138 | when_clause_opt timeout statement WOR when_clause ELSE statement
[135b431]1139 { $$ = build_waitfor_timeout( $2, $3, $1, $7, $5 ); }
[5b2edbc]1140 ;
1141
1142waitfor_statement:
[51d6d6a]1143 when_clause_opt waitfor statement %prec THEN
[135b431]1144 { $$ = new StatementNode( build_waitfor( $2, $3, $1 ) ); }
[5b2edbc]1145 | when_clause_opt waitfor statement WOR waitfor_clause
[135b431]1146 { $$ = new StatementNode( build_waitfor( $2, $3, $1, $5 ) ); }
[8b47e50]1147 ;
1148
[51b73452]1149exception_statement:
[cfaabe2c]1150 TRY compound_statement handler_clause
[e82aa9df]1151 { $$ = new StatementNode( build_try( $2, $3, 0 ) ); }
[4d51835]1152 | TRY compound_statement finally_clause
[e82aa9df]1153 { $$ = new StatementNode( build_try( $2, 0, $3 ) ); }
[cfaabe2c]1154 | TRY compound_statement handler_clause finally_clause
[e82aa9df]1155 { $$ = new StatementNode( build_try( $2, $3, $4 ) ); }
[4d51835]1156 ;
[51b73452]1157
1158handler_clause:
[cbce272]1159 handler_key '(' push push exception_declaration pop handler_predicate_opt ')' compound_statement pop
1160 { $$ = new StatementNode( build_catch( $1, $5, $7, $9 ) ); }
[dc2b4d6]1161 | handler_clause handler_key '(' push push exception_declaration pop handler_predicate_opt ')' compound_statement pop
[cbce272]1162 { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( $2, $6, $8, $10 ) ) ); }
[994d080]1163 ;
1164
1165handler_predicate_opt:
1166 //empty
[cbce272]1167 { $$ = nullptr; }
[6a276a0]1168 | ';' conditional_expression { $$ = $2; }
[307a732]1169 ;
1170
1171handler_key:
[6a276a0]1172 CATCH { $$ = CatchStmt::Terminate; }
1173 | CATCHRESUME { $$ = CatchStmt::Resume; }
[4d51835]1174 ;
[51b73452]1175
1176finally_clause:
[6a276a0]1177 FINALLY compound_statement { $$ = new StatementNode( build_finally( $2 ) ); }
[4d51835]1178 ;
[51b73452]1179
1180exception_declaration:
[d0ffed1]1181 // No SUE declaration in parameter list.
1182 type_specifier_nobody
1183 | type_specifier_nobody declarator
[4d51835]1184 {
1185 typedefTable.addToEnclosingScope( TypedefTable::ID );
1186 $$ = $2->addType( $1 );
1187 }
[d0ffed1]1188 | type_specifier_nobody variable_abstract_declarator
[4d51835]1189 { $$ = $2->addType( $1 ); }
[c0aa336]1190 | cfa_abstract_declarator_tuple no_attr_identifier // CFA
[4d51835]1191 {
1192 typedefTable.addToEnclosingScope( TypedefTable::ID );
1193 $$ = $1->addName( $2 );
1194 }
[c0aa336]1195 | cfa_abstract_declarator_tuple // CFA
[4d51835]1196 ;
[51b73452]1197
[2a8427c6]1198enable_disable_statement:
1199 enable_disable_key identifier_list compound_statement
1200 ;
1201
1202enable_disable_key:
1203 ENABLE
1204 | DISABLE
1205 ;
1206
[51b73452]1207asm_statement:
[ab57786]1208 ASM asm_volatile_opt '(' string_literal ')' ';'
[6d539f83]1209 { $$ = new StatementNode( build_asm( $2, $4, 0 ) ); }
[ab57786]1210 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ')' ';' // remaining GCC
[6d539f83]1211 { $$ = new StatementNode( build_asm( $2, $4, $6 ) ); }
[ab57786]1212 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ')' ';'
[6d539f83]1213 { $$ = new StatementNode( build_asm( $2, $4, $6, $8 ) ); }
[ab57786]1214 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
[6d539f83]1215 { $$ = new StatementNode( build_asm( $2, $4, $6, $8, $10 ) ); }
[ab57786]1216 | ASM asm_volatile_opt GOTO '(' string_literal ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' label_list ')' ';'
[6d539f83]1217 { $$ = new StatementNode( build_asm( $2, $5, 0, $8, $10, $12 ) ); }
[7f5566b]1218 ;
1219
1220asm_volatile_opt: // GCC
1221 // empty
1222 { $$ = false; }
1223 | VOLATILE
1224 { $$ = true; }
[4d51835]1225 ;
[b87a5ed]1226
1227asm_operands_opt: // GCC
[4d51835]1228 // empty
[58dd019]1229 { $$ = nullptr; } // use default argument
[4d51835]1230 | asm_operands_list
1231 ;
[b87a5ed]1232
1233asm_operands_list: // GCC
[4d51835]1234 asm_operand
1235 | asm_operands_list ',' asm_operand
[1d4580a]1236 { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]1237 ;
[b87a5ed]1238
1239asm_operand: // GCC
[ab57786]1240 string_literal '(' constant_expression ')'
[513e165]1241 { $$ = new ExpressionNode( new AsmExpr( maybeMoveBuild< Expression >( (ExpressionNode *)nullptr ), $1, maybeMoveBuild< Expression >( $3 ) ) ); }
[ab57786]1242 | '[' constant_expression ']' string_literal '(' constant_expression ')'
[513e165]1243 { $$ = new ExpressionNode( new AsmExpr( maybeMoveBuild< Expression >( $2 ), $4, maybeMoveBuild< Expression >( $6 ) ) ); }
[7f5566b]1244 ;
1245
[4e06c1e]1246asm_clobbers_list_opt: // GCC
[7f5566b]1247 // empty
[58dd019]1248 { $$ = nullptr; } // use default argument
[ab57786]1249 | string_literal
[d1625f8]1250 { $$ = new ExpressionNode( $1 ); }
[ab57786]1251 | asm_clobbers_list_opt ',' string_literal
[513e165]1252 // set_last returns ParseNode *
[e82aa9df]1253 { $$ = (ExpressionNode *)$1->set_last( new ExpressionNode( $3 ) ); }
[4d51835]1254 ;
[b87a5ed]1255
[7f5566b]1256label_list:
1257 no_attr_identifier
[ab57786]1258 {
1259 $$ = new LabelNode(); $$->labels.push_back( *$1 );
1260 delete $1; // allocated by lexer
1261 }
[7f5566b]1262 | label_list ',' no_attr_identifier
[ab57786]1263 {
1264 $$ = $1; $1->labels.push_back( *$3 );
1265 delete $3; // allocated by lexer
1266 }
[4d51835]1267 ;
[51b73452]1268
[c11e31c]1269//******************************* DECLARATIONS *********************************
[51b73452]1270
[b87a5ed]1271declaration_list_opt: // used at beginning of switch statement
[4d51835]1272 pop
[58dd019]1273 { $$ = nullptr; }
[4d51835]1274 | declaration_list
1275 ;
[51b73452]1276
1277declaration_list:
[4d51835]1278 declaration
1279 | declaration_list push declaration
1280 { $$ = $1->appendList( $3 ); }
1281 ;
[51b73452]1282
[c0aa336]1283KR_declaration_list_opt: // used to declare parameter types in K&R style functions
[4cc585b]1284 // empty
[58dd019]1285 { $$ = nullptr; }
[c0aa336]1286 | KR_declaration_list
[4d51835]1287 ;
[51b73452]1288
[c0aa336]1289KR_declaration_list:
[4cc585b]1290 push c_declaration pop ';'
1291 { $$ = $2; }
1292 | KR_declaration_list push c_declaration pop ';'
[4d51835]1293 { $$ = $1->appendList( $3 ); }
1294 ;
[b87a5ed]1295
[51b1202]1296local_label_declaration_opt: // GCC, local label
[4d51835]1297 // empty
[51b1202]1298 | local_label_declaration_list
[4d51835]1299 ;
[b87a5ed]1300
[51b1202]1301local_label_declaration_list: // GCC, local label
1302 LABEL local_label_list ';'
1303 | local_label_declaration_list LABEL local_label_list ';'
[4d51835]1304 ;
[b87a5ed]1305
[51b1202]1306local_label_list: // GCC, local label
[7f5566b]1307 no_attr_identifier_or_type_name {}
[51b1202]1308 | local_label_list ',' no_attr_identifier_or_type_name {}
[4d51835]1309 ;
[b87a5ed]1310
[936e9f4]1311declaration: // old & new style declarations
[4cc585b]1312 c_declaration pop ';'
1313 | cfa_declaration pop ';' // CFA
[b9be000b]1314 | static_assert
[4d51835]1315 ;
[b87a5ed]1316
[b9be000b]1317static_assert:
1318 STATICASSERT '(' constant_expression ',' string_literal ')' ';' // C11
[f6e3e34]1319 { $$ = DeclarationNode::newStaticAssert( $3, $5 ); }
[b9be000b]1320
[de62360d]1321// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1322// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1323// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1324// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1325// the declaration list (as in Pascal). ANSI C and the new CFA declarations may appear together in the same program
1326// block, but cannot be mixed within a specific declaration.
[c11e31c]1327//
[b87a5ed]1328// CFA C
1329// [10] int x; int x[10]; // array of 10 integers
1330// [10] * char y; char *y[10]; // array of 10 pointers to char
1331
[c0aa336]1332cfa_declaration: // CFA
[4cc585b]1333 cfa_variable_declaration
1334 | cfa_typedef_declaration
1335 | cfa_function_declaration
1336 | type_declaring_list
1337 | trait_specifier
[4d51835]1338 ;
[b87a5ed]1339
[c0aa336]1340cfa_variable_declaration: // CFA
1341 cfa_variable_specifier initializer_opt
[4d51835]1342 {
1343 typedefTable.addToEnclosingScope( TypedefTable::ID );
[3cfe27f]1344 $$ = $1->addInitializer( $2 );
[4d51835]1345 }
[c0aa336]1346 | declaration_qualifier_list cfa_variable_specifier initializer_opt
[de62360d]1347 // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1348 // them as a type_qualifier cannot appear in that context.
[4d51835]1349 {
1350 typedefTable.addToEnclosingScope( TypedefTable::ID );
[3cfe27f]1351 $$ = $2->addQualifiers( $1 )->addInitializer( $3 );;
[4d51835]1352 }
[c0aa336]1353 | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
[4d51835]1354 {
1355 typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
[3cfe27f]1356 $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) );
[4d51835]1357 }
1358 ;
[b87a5ed]1359
[c0aa336]1360cfa_variable_specifier: // CFA
[de62360d]1361 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1362 // storage-class
[c0aa336]1363 cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
[4d51835]1364 {
1365 typedefTable.setNextIdentifier( *$2 );
[58dd019]1366 $$ = $1->addName( $2 )->addAsmName( $3 );
[4d51835]1367 }
[c0aa336]1368 | cfa_abstract_tuple identifier_or_type_name asm_name_opt
[4d51835]1369 {
1370 typedefTable.setNextIdentifier( *$2 );
[58dd019]1371 $$ = $1->addName( $2 )->addAsmName( $3 );
[4d51835]1372 }
[c0aa336]1373 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
[4d51835]1374 {
1375 typedefTable.setNextIdentifier( *$3 );
[58dd019]1376 $$ = $2->addQualifiers( $1 )->addName( $3 )->addAsmName( $4 );
[4d51835]1377 }
1378 ;
[b87a5ed]1379
[c0aa336]1380cfa_function_declaration: // CFA
1381 cfa_function_specifier
[4d51835]1382 {
1383 typedefTable.addToEnclosingScope( TypedefTable::ID );
1384 $$ = $1;
1385 }
[c0aa336]1386 | type_qualifier_list cfa_function_specifier
[4d51835]1387 {
1388 typedefTable.addToEnclosingScope( TypedefTable::ID );
1389 $$ = $2->addQualifiers( $1 );
1390 }
[c0aa336]1391 | declaration_qualifier_list cfa_function_specifier
[4d51835]1392 {
1393 typedefTable.addToEnclosingScope( TypedefTable::ID );
1394 $$ = $2->addQualifiers( $1 );
1395 }
[c0aa336]1396 | declaration_qualifier_list type_qualifier_list cfa_function_specifier
[4d51835]1397 {
1398 typedefTable.addToEnclosingScope( TypedefTable::ID );
1399 $$ = $3->addQualifiers( $1 )->addQualifiers( $2 );
1400 }
[481115f]1401 | cfa_function_declaration pop ',' push identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')'
[4d51835]1402 {
[481115f]1403 // Append the return type at the start (left-hand-side) to each identifier in the list.
1404 DeclarationNode * ret = new DeclarationNode;
1405 ret->type = maybeClone( $1->type->base );
[2a8427c6]1406 $$ = $1->appendList( DeclarationNode::newFunction( $5, ret, $8, nullptr ) );
[4d51835]1407 }
1408 ;
[b87a5ed]1409
[c0aa336]1410cfa_function_specifier: // CFA
1411// '[' ']' identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')' // S/R conflict
[1b29996]1412// {
1413// $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1414// }
[c0aa336]1415// '[' ']' identifier '(' push cfa_parameter_type_list_opt pop ')'
[2871210]1416// {
1417// typedefTable.setNextIdentifier( *$5 );
1418// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1419// }
[c0aa336]1420// | '[' ']' TYPEDEFname '(' push cfa_parameter_type_list_opt pop ')'
[2871210]1421// {
1422// typedefTable.setNextIdentifier( *$5 );
1423// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1424// }
1425// | '[' ']' typegen_name
1426 // identifier_or_type_name must be broken apart because of the sequence:
[4d51835]1427 //
[c0aa336]1428 // '[' ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
[4d51835]1429 // '[' ']' type_specifier
1430 //
[2871210]1431 // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1432 // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
[c0aa336]1433 cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')'
1434 // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
[4d51835]1435 {
[2a8427c6]1436 $$ = DeclarationNode::newFunction( $2, $1, $5, 0 );
[4d51835]1437 }
[c0aa336]1438 | cfa_function_return identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')'
[4d51835]1439 {
[2a8427c6]1440 $$ = DeclarationNode::newFunction( $2, $1, $5, 0 );
[4d51835]1441 }
1442 ;
[b87a5ed]1443
[c0aa336]1444cfa_function_return: // CFA
1445 '[' push cfa_parameter_list pop ']'
[4d51835]1446 { $$ = DeclarationNode::newTuple( $3 ); }
[c0aa336]1447 | '[' push cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ']'
1448 // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the
[de62360d]1449 // ']'.
[4d51835]1450 { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1451 ;
[b87a5ed]1452
[c0aa336]1453cfa_typedef_declaration: // CFA
1454 TYPEDEF cfa_variable_specifier
[4d51835]1455 {
[de62360d]1456 typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1457 $$ = $2->addTypedef();
1458 }
[c0aa336]1459 | TYPEDEF cfa_function_specifier
[4d51835]1460 {
[de62360d]1461 typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1462 $$ = $2->addTypedef();
1463 }
[c0aa336]1464 | cfa_typedef_declaration pop ',' push no_attr_identifier
[4d51835]1465 {
[de62360d]1466 typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
[4d51835]1467 $$ = $1->appendList( $1->cloneType( $5 ) );
1468 }
1469 ;
[b87a5ed]1470
[de62360d]1471// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1472// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
[51b73452]1473
1474typedef_declaration:
[4d51835]1475 TYPEDEF type_specifier declarator
1476 {
[de62360d]1477 typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1478 $$ = $3->addType( $2 )->addTypedef();
1479 }
1480 | typedef_declaration pop ',' push declarator
1481 {
[de62360d]1482 typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1483 $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1484 }
[de62360d]1485 | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
[4d51835]1486 {
[de62360d]1487 typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1488 $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1489 }
1490 | type_specifier TYPEDEF declarator
1491 {
[de62360d]1492 typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1493 $$ = $3->addType( $1 )->addTypedef();
1494 }
1495 | type_specifier TYPEDEF type_qualifier_list declarator
1496 {
[de62360d]1497 typedefTable.addToEnclosingScope( TypedefTable::TD );
1498 $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
[4d51835]1499 }
1500 ;
[b87a5ed]1501
[721f17a]1502typedef_expression:
1503 // GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
[4d51835]1504 TYPEDEF no_attr_identifier '=' assignment_expression
1505 {
[de62360d]1506 typedefTable.addToEnclosingScope( *$2, TypedefTable::TD );
[c0aa336]1507 $$ = DeclarationNode::newName( 0 ); // unimplemented
[4d51835]1508 }
1509 | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1510 {
[de62360d]1511 typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
[c0aa336]1512 $$ = DeclarationNode::newName( 0 ); // unimplemented
[4d51835]1513 }
1514 ;
[51b73452]1515
[c0aa336]1516//c_declaration:
1517// declaring_list pop ';'
1518// | typedef_declaration pop ';'
1519// | typedef_expression pop ';' // GCC, naming expression type
1520// | sue_declaration_specifier pop ';'
1521// ;
1522//
1523//declaring_list:
1524// // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1525// // storage-class
1526// declarator asm_name_opt initializer_opt
1527// {
1528// typedefTable.addToEnclosingScope( TypedefTable::ID );
1529// $$ = ( $2->addType( $1 ))->addAsmName( $3 )->addInitializer( $4 );
1530// }
1531// | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1532// {
1533// typedefTable.addToEnclosingScope( TypedefTable::ID );
1534// $$ = $1->appendList( $1->cloneBaseType( $4->addAsmName( $5 )->addInitializer( $6 ) ) );
1535// }
1536// ;
1537
1538c_declaration:
[4cc585b]1539 declaration_specifier declaring_list
[c0aa336]1540 {
1541 $$ = distAttr( $1, $2 );
1542 }
[4cc585b]1543 | typedef_declaration
1544 | typedef_expression // GCC, naming expression type
1545 | sue_declaration_specifier
[4d51835]1546 ;
[51b73452]1547
1548declaring_list:
[de62360d]1549 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1550 // storage-class
[c0aa336]1551 declarator asm_name_opt initializer_opt
[4d51835]1552 {
1553 typedefTable.addToEnclosingScope( TypedefTable::ID );
[c0aa336]1554 $$ = $1->addAsmName( $2 )->addInitializer( $3 );
[4d51835]1555 }
1556 | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1557 {
1558 typedefTable.addToEnclosingScope( TypedefTable::ID );
[c0aa336]1559 $$ = $1->appendList( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) );
[4d51835]1560 }
1561 ;
[b87a5ed]1562
1563declaration_specifier: // type specifier + storage class
[4d51835]1564 basic_declaration_specifier
1565 | sue_declaration_specifier
[84d58c5]1566 | type_declaration_specifier
[4d51835]1567 ;
[b87a5ed]1568
[d0ffed1]1569declaration_specifier_nobody: // type specifier + storage class - {...}
1570 // Preclude SUE declarations in restricted scopes:
1571 //
1572 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1573 //
1574 // because it is impossible to call f due to name equivalence.
1575 basic_declaration_specifier
1576 | sue_declaration_specifier_nobody
[84d58c5]1577 | type_declaration_specifier
[d0ffed1]1578 ;
1579
1580type_specifier: // type specifier
[4d51835]1581 basic_type_specifier
1582 | sue_type_specifier
[84d58c5]1583 | type_type_specifier
[4d51835]1584 ;
[b87a5ed]1585
[d0ffed1]1586type_specifier_nobody: // type specifier - {...}
1587 // Preclude SUE declarations in restricted scopes:
1588 //
1589 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1590 //
1591 // because it is impossible to call f due to name equivalence.
1592 basic_type_specifier
1593 | sue_type_specifier_nobody
[84d58c5]1594 | type_type_specifier
[d0ffed1]1595 ;
1596
[b87a5ed]1597type_qualifier_list_opt: // GCC, used in asm_statement
[4d51835]1598 // empty
[58dd019]1599 { $$ = nullptr; }
[4d51835]1600 | type_qualifier_list
1601 ;
[51b73452]1602
1603type_qualifier_list:
[de62360d]1604 // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
[4d51835]1605 //
[de62360d]1606 // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1607 // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1608 // appeared only once.
[4d51835]1609 type_qualifier
1610 | type_qualifier_list type_qualifier
1611 { $$ = $1->addQualifiers( $2 ); }
1612 ;
[51b73452]1613
1614type_qualifier:
[4d51835]1615 type_qualifier_name
1616 | attribute
1617 ;
[51b73452]1618
1619type_qualifier_name:
[4d51835]1620 CONST
[738e304]1621 { $$ = DeclarationNode::newTypeQualifier( Type::Const ); }
[4d51835]1622 | RESTRICT
[738e304]1623 { $$ = DeclarationNode::newTypeQualifier( Type::Restrict ); }
[4d51835]1624 | VOLATILE
[738e304]1625 { $$ = DeclarationNode::newTypeQualifier( Type::Volatile ); }
[4d51835]1626 | ATOMIC
[738e304]1627 { $$ = DeclarationNode::newTypeQualifier( Type::Atomic ); }
[a16a7ec]1628 | forall
1629 ;
1630
1631forall:
1632 FORALL '('
[4d51835]1633 {
1634 typedefTable.enterScope();
1635 }
1636 type_parameter_list ')' // CFA
1637 {
1638 typedefTable.leaveScope();
1639 $$ = DeclarationNode::newForall( $4 );
1640 }
1641 ;
[51b73452]1642
1643declaration_qualifier_list:
[4d51835]1644 storage_class_list
[de62360d]1645 | type_qualifier_list storage_class_list // remaining OBSOLESCENT (see 2 )
[4d51835]1646 { $$ = $1->addQualifiers( $2 ); }
1647 | declaration_qualifier_list type_qualifier_list storage_class_list
1648 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1649 ;
[51b73452]1650
1651storage_class_list:
[de62360d]1652 // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1653 // only one of each is specified, except for inline, which can appear with the others.
[4d51835]1654 //
[de62360d]1655 // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1656 // specifiers in a declaration.
[4d51835]1657 storage_class
1658 | storage_class_list storage_class
1659 { $$ = $1->addQualifiers( $2 ); }
1660 ;
[51b73452]1661
1662storage_class:
[4d51835]1663 EXTERN
[68fe077a]1664 { $$ = DeclarationNode::newStorageClass( Type::Extern ); }
[4d51835]1665 | STATIC
[68fe077a]1666 { $$ = DeclarationNode::newStorageClass( Type::Static ); }
[4d51835]1667 | AUTO
[68fe077a]1668 { $$ = DeclarationNode::newStorageClass( Type::Auto ); }
[4d51835]1669 | REGISTER
[68fe077a]1670 { $$ = DeclarationNode::newStorageClass( Type::Register ); }
[dd020c0]1671 | THREADLOCAL // C11
[68fe077a]1672 { $$ = DeclarationNode::newStorageClass( Type::Threadlocal ); }
[dd020c0]1673 // Put function specifiers here to simplify parsing rules, but separate them semantically.
[4d51835]1674 | INLINE // C99
[ddfd945]1675 { $$ = DeclarationNode::newFuncSpecifier( Type::Inline ); }
[4d51835]1676 | FORTRAN // C99
[ddfd945]1677 { $$ = DeclarationNode::newFuncSpecifier( Type::Fortran ); }
[68cd1ce]1678 | NORETURN // C11
[ddfd945]1679 { $$ = DeclarationNode::newFuncSpecifier( Type::Noreturn ); }
[4d51835]1680 ;
[51b73452]1681
1682basic_type_name:
[201aeb9]1683 VOID
[4d51835]1684 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1685 | BOOL // C99
1686 { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
[201aeb9]1687 | CHAR
1688 { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1689 | INT
1690 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1691 | INT128
1692 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 ); }
1693 | FLOAT
1694 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1695 | FLOAT80
1696 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float80 ); }
1697 | FLOAT128
1698 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float128 ); }
1699 | DOUBLE
1700 { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
[4d51835]1701 | COMPLEX // C99
[5b639ee]1702 { $$ = DeclarationNode::newComplexType( DeclarationNode::Complex ); }
[4d51835]1703 | IMAGINARY // C99
[5b639ee]1704 { $$ = DeclarationNode::newComplexType( DeclarationNode::Imaginary ); }
[201aeb9]1705 | SIGNED
1706 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Signed ); }
1707 | UNSIGNED
1708 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Unsigned ); }
1709 | SHORT
1710 { $$ = DeclarationNode::newLength( DeclarationNode::Short ); }
1711 | LONG
1712 { $$ = DeclarationNode::newLength( DeclarationNode::Long ); }
[72457b6]1713 | VALIST // GCC, __builtin_va_list
1714 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
[4d51835]1715 ;
[51b73452]1716
1717basic_declaration_specifier:
[4d51835]1718 // A semantic check is necessary for conflicting storage classes.
1719 basic_type_specifier
1720 | declaration_qualifier_list basic_type_specifier
1721 { $$ = $2->addQualifiers( $1 ); }
1722 | basic_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1723 { $$ = $1->addQualifiers( $2 ); }
1724 | basic_declaration_specifier storage_class type_qualifier_list
1725 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1726 | basic_declaration_specifier storage_class basic_type_specifier
1727 { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1728 ;
[51b73452]1729
1730basic_type_specifier:
[84d58c5]1731 direct_type
[f38e7d7]1732 // Cannot have type modifiers, e.g., short, long, etc.
[84d58c5]1733 | type_qualifier_list_opt indirect_type type_qualifier_list_opt
[4d51835]1734 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1735 ;
[51b73452]1736
[84d58c5]1737direct_type:
[4d51835]1738 basic_type_name
1739 | type_qualifier_list basic_type_name
1740 { $$ = $2->addQualifiers( $1 ); }
[84d58c5]1741 | direct_type type_qualifier
[4d51835]1742 { $$ = $1->addQualifiers( $2 ); }
[84d58c5]1743 | direct_type basic_type_name
[4d51835]1744 { $$ = $1->addType( $2 ); }
1745 ;
[51b73452]1746
[84d58c5]1747indirect_type:
1748 TYPEOF '(' type ')' // GCC: typeof(x) y;
[4d51835]1749 { $$ = $3; }
1750 | TYPEOF '(' comma_expression ')' // GCC: typeof(a+b) y;
1751 { $$ = DeclarationNode::newTypeof( $3 ); }
[84d58c5]1752 | ATTR_TYPEGENname '(' type ')' // CFA: e.g., @type(x) y;
[4d51835]1753 { $$ = DeclarationNode::newAttr( $1, $3 ); }
1754 | ATTR_TYPEGENname '(' comma_expression ')' // CFA: e.g., @type(a+b) y;
1755 { $$ = DeclarationNode::newAttr( $1, $3 ); }
[f38e7d7]1756 | ZERO_T // CFA
1757 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Zero ); }
1758 | ONE_T // CFA
1759 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::One ); }
[4d51835]1760 ;
[51b73452]1761
[d0ffed1]1762sue_declaration_specifier: // struct, union, enum + storage class + type specifier
[4d51835]1763 sue_type_specifier
1764 | declaration_qualifier_list sue_type_specifier
1765 { $$ = $2->addQualifiers( $1 ); }
1766 | sue_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1767 { $$ = $1->addQualifiers( $2 ); }
1768 | sue_declaration_specifier storage_class type_qualifier_list
1769 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1770 ;
[51b73452]1771
[d0ffed1]1772sue_type_specifier: // struct, union, enum + type specifier
1773 elaborated_type
[fdca7c6]1774 | type_qualifier_list
1775 { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
1776 elaborated_type
1777 { $$ = $3->addQualifiers( $1 ); }
[4d51835]1778 | sue_type_specifier type_qualifier
1779 { $$ = $1->addQualifiers( $2 ); }
1780 ;
[51b73452]1781
[d0ffed1]1782sue_declaration_specifier_nobody: // struct, union, enum - {...} + storage class + type specifier
1783 sue_type_specifier_nobody
1784 | declaration_qualifier_list sue_type_specifier_nobody
1785 { $$ = $2->addQualifiers( $1 ); }
1786 | sue_declaration_specifier_nobody storage_class // remaining OBSOLESCENT (see 2)
1787 { $$ = $1->addQualifiers( $2 ); }
1788 | sue_declaration_specifier_nobody storage_class type_qualifier_list
1789 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1790 ;
1791
1792sue_type_specifier_nobody: // struct, union, enum - {...} + type specifier
1793 elaborated_type_nobody
1794 | type_qualifier_list elaborated_type_nobody
1795 { $$ = $2->addQualifiers( $1 ); }
1796 | sue_type_specifier_nobody type_qualifier
1797 { $$ = $1->addQualifiers( $2 ); }
1798 ;
1799
[84d58c5]1800type_declaration_specifier:
1801 type_type_specifier
1802 | declaration_qualifier_list type_type_specifier
[4d51835]1803 { $$ = $2->addQualifiers( $1 ); }
[84d58c5]1804 | type_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
[4d51835]1805 { $$ = $1->addQualifiers( $2 ); }
[84d58c5]1806 | type_declaration_specifier storage_class type_qualifier_list
[4d51835]1807 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1808 ;
[b87a5ed]1809
[84d58c5]1810type_type_specifier: // typedef types
1811 type_name
1812 | type_qualifier_list type_name
1813 { $$ = $2->addQualifiers( $1 ); }
1814 | type_type_specifier type_qualifier
1815 { $$ = $1->addQualifiers( $2 ); }
1816 ;
1817
1818type_name:
[4d51835]1819 TYPEDEFname
1820 { $$ = DeclarationNode::newFromTypedef( $1 ); }
[84d58c5]1821 | '.' TYPEDEFname
1822 { $$ = DeclarationNode::newFromTypedef( $2 ); } // FIX ME
1823 | type_name '.' TYPEDEFname
1824 { $$ = DeclarationNode::newFromTypedef( $3 ); } // FIX ME
1825 | typegen_name
1826 | '.' typegen_name
1827 { $$ = $2; } // FIX ME
1828 | type_name '.' typegen_name
1829 { $$ = $3; } // FIX ME
1830 ;
1831
1832typegen_name: // CFA
[65d6de4]1833 TYPEGENname
1834 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1835 | TYPEGENname '(' ')'
[67cf18c]1836 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1837 | TYPEGENname '(' type_list ')'
[84d58c5]1838 { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
[4d51835]1839 ;
[51b73452]1840
[d0ffed1]1841elaborated_type: // struct, union, enum
[c0aa336]1842 aggregate_type
1843 | enum_type
[4d51835]1844 ;
[51b73452]1845
[d0ffed1]1846elaborated_type_nobody: // struct, union, enum - {...}
1847 aggregate_type_nobody
1848 | enum_type_nobody
1849 ;
1850
1851aggregate_type: // struct, union
[c0aa336]1852 aggregate_key attribute_list_opt '{' field_declaration_list '}'
[6f95000]1853 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), nullptr, $4, true )->addQualifiers( $2 ); }
[c0aa336]1854 | aggregate_key attribute_list_opt no_attr_identifier_or_type_name
[fdca7c6]1855 {
1856 typedefTable.makeTypedef( *$3 ); // create typedef
1857 if ( forall ) typedefTable.changeKind( *$3, TypedefTable::TG ); // possibly update
1858 forall = false; // reset
1859 }
[c0aa336]1860 '{' field_declaration_list '}'
1861 { $$ = DeclarationNode::newAggregate( $1, $3, nullptr, $6, true )->addQualifiers( $2 ); }
[84d58c5]1862 | aggregate_key attribute_list_opt '(' type_list ')' '{' field_declaration_list '}' // CFA
[6f95000]1863 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), $4, $7, false )->addQualifiers( $2 ); }
[d0ffed1]1864 | aggregate_type_nobody
1865 ;
1866
1867aggregate_type_nobody: // struct, union - {...}
[84d58c5]1868 aggregate_key attribute_list_opt no_attr_identifier
1869 {
1870 typedefTable.makeTypedef( *$3 );
[9997fee]1871 if ( forall ) typedefTable.changeKind( *$3, TypedefTable::TG ); // possibly update
1872 forall = false; // reset
[84d58c5]1873 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1874 }
1875 | aggregate_key attribute_list_opt TYPEDEFname
[d0ffed1]1876 {
1877 typedefTable.makeTypedef( *$3 );
1878 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1879 }
[84d58c5]1880 | aggregate_key attribute_list_opt typegen_name // CFA
[65d6de4]1881 {
1882 // Create new generic declaration with same name as previous forward declaration, where the IDENTIFIER is
1883 // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and
1884 // delete newFromTypeGen.
1885 $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
1886 $3->type->symbolic.name = nullptr;
1887 $3->type->symbolic.actuals = nullptr;
1888 delete $3;
1889 }
[4d51835]1890 ;
[51b73452]1891
1892aggregate_key:
[c0aa336]1893 STRUCT
[4d51835]1894 { $$ = DeclarationNode::Struct; }
[c0aa336]1895 | UNION
[4d51835]1896 { $$ = DeclarationNode::Union; }
[c27fb59]1897 | EXCEPTION
1898 { $$ = DeclarationNode::Exception; }
[d3bc0ad]1899 | COROUTINE
[409433da]1900 { $$ = DeclarationNode::Coroutine; }
[d3bc0ad]1901 | MONITOR
[409433da]1902 { $$ = DeclarationNode::Monitor; }
[d3bc0ad]1903 | THREAD
[409433da]1904 { $$ = DeclarationNode::Thread; }
[4d51835]1905 ;
[51b73452]1906
1907field_declaration_list:
[5d125e4]1908 // empty
[58dd019]1909 { $$ = nullptr; }
[4d51835]1910 | field_declaration_list field_declaration
[a7741435]1911 { $$ = $1 ? $1->appendList( $2 ) : $2; }
[4d51835]1912 ;
[51b73452]1913
1914field_declaration:
[c0aa336]1915 cfa_field_declaring_list ';' // CFA, new style field declaration
1916 | EXTENSION cfa_field_declaring_list ';' // GCC
1917 {
1918 distExt( $2 ); // mark all fields in list
[8e9cbb2]1919 $$ = $2;
1920 }
[c0aa336]1921 | type_specifier field_declaring_list ';'
1922 {
1923 $$ = distAttr( $1, $2 ); }
1924 | EXTENSION type_specifier field_declaring_list ';' // GCC
1925 {
1926 distExt( $3 ); // mark all fields in list
1927 $$ = distAttr( $2, $3 );
1928 }
[b9be000b]1929 | static_assert
[4d51835]1930 ;
[b87a5ed]1931
[c0aa336]1932cfa_field_declaring_list: // CFA, new style field declaration
1933 cfa_abstract_declarator_tuple // CFA, no field name
1934 | cfa_abstract_declarator_tuple no_attr_identifier_or_type_name
[4d51835]1935 { $$ = $1->addName( $2 ); }
[c0aa336]1936 | cfa_field_declaring_list ',' no_attr_identifier_or_type_name
[4d51835]1937 { $$ = $1->appendList( $1->cloneType( $3 ) ); }
[c0aa336]1938 | cfa_field_declaring_list ',' // CFA, no field name
[4d51835]1939 { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1940 ;
[51b73452]1941
1942field_declaring_list:
[c0aa336]1943 field_declarator
[4d51835]1944 | field_declaring_list ',' attribute_list_opt field_declarator
[c0aa336]1945 { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
[4d51835]1946 ;
[51b73452]1947
1948field_declarator:
[4d51835]1949 // empty
1950 { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
[6f95000]1951 // '@'
1952 // { $$ = DeclarationNode::newName( new string( DeclarationNode::anonymous.newName() ) ); } // CFA, no field name
[4d51835]1953 | bit_subrange_size // no field name
1954 { $$ = DeclarationNode::newBitfield( $1 ); }
1955 | variable_declarator bit_subrange_size_opt
1956 // A semantic check is required to ensure bit_subrange only appears on base type int.
1957 { $$ = $1->addBitfield( $2 ); }
[c6b1105]1958 | variable_type_redeclarator bit_subrange_size_opt
[4d51835]1959 // A semantic check is required to ensure bit_subrange only appears on base type int.
1960 { $$ = $1->addBitfield( $2 ); }
1961 | variable_abstract_declarator // CFA, no field name
1962 ;
[51b73452]1963
1964bit_subrange_size_opt:
[4d51835]1965 // empty
[58dd019]1966 { $$ = nullptr; }
[4d51835]1967 | bit_subrange_size
1968 { $$ = $1; }
1969 ;
[51b73452]1970
1971bit_subrange_size:
[4d51835]1972 ':' constant_expression
1973 { $$ = $2; }
1974 ;
[51b73452]1975
[d0ffed1]1976enum_type: // enum
[c0aa336]1977 ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
[6f95000]1978 { $$ = DeclarationNode::newEnum( new string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }
[c0aa336]1979 | ENUM attribute_list_opt no_attr_identifier_or_type_name
[d0ffed1]1980 { typedefTable.makeTypedef( *$3 ); }
1981 '{' enumerator_list comma_opt '}'
1982 { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
1983 | enum_type_nobody
1984 ;
1985
1986enum_type_nobody: // enum - {...}
1987 ENUM attribute_list_opt no_attr_identifier_or_type_name
[45161b4d]1988 {
[c0aa336]1989 typedefTable.makeTypedef( *$3 );
[ca1a547]1990 $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 );
[45161b4d]1991 }
[4d51835]1992 ;
[51b73452]1993
1994enumerator_list:
[2871210]1995 no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1996 { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
[2871210]1997 | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1998 { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1999 ;
[51b73452]2000
2001enumerator_value_opt:
[4d51835]2002 // empty
[58dd019]2003 { $$ = nullptr; }
[4d51835]2004 | '=' constant_expression
2005 { $$ = $2; }
2006 ;
[51b73452]2007
[2a8427c6]2008cfa_parameter_type_list_opt: // CFA, abstract + real
[4d51835]2009 // empty
[2a8427c6]2010 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2011 | ELLIPSIS
[58dd019]2012 { $$ = nullptr; }
[2a8427c6]2013 | cfa_abstract_parameter_list
[c0aa336]2014 | cfa_parameter_list
2015 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
[4d51835]2016 { $$ = $1->appendList( $5 ); }
[c0aa336]2017 | cfa_abstract_parameter_list pop ',' push ELLIPSIS
[4d51835]2018 { $$ = $1->addVarArgs(); }
[c0aa336]2019 | cfa_parameter_list pop ',' push ELLIPSIS
[4d51835]2020 { $$ = $1->addVarArgs(); }
2021 ;
[b87a5ed]2022
[c0aa336]2023cfa_parameter_list: // CFA
2024 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
2025 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
2026 cfa_parameter_declaration
2027 | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]2028 { $$ = $1->appendList( $5 ); }
[c0aa336]2029 | cfa_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]2030 { $$ = $1->appendList( $5 ); }
[c0aa336]2031 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]2032 { $$ = $1->appendList( $5 )->appendList( $9 ); }
2033 ;
[b87a5ed]2034
[c0aa336]2035cfa_abstract_parameter_list: // CFA, new & old style abstract
2036 cfa_abstract_parameter_declaration
2037 | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
[4d51835]2038 { $$ = $1->appendList( $5 ); }
2039 ;
[51b73452]2040
2041parameter_type_list_opt:
[4d51835]2042 // empty
[58dd019]2043 { $$ = nullptr; }
[2a8427c6]2044 | ELLIPSIS
2045 { $$ = nullptr; }
2046 | parameter_list
[4d51835]2047 | parameter_list pop ',' push ELLIPSIS
2048 { $$ = $1->addVarArgs(); }
2049 ;
[b87a5ed]2050
2051parameter_list: // abstract + real
[4d51835]2052 abstract_parameter_declaration
2053 | parameter_declaration
2054 | parameter_list pop ',' push abstract_parameter_declaration
2055 { $$ = $1->appendList( $5 ); }
2056 | parameter_list pop ',' push parameter_declaration
2057 { $$ = $1->appendList( $5 ); }
2058 ;
[51b73452]2059
[de62360d]2060// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
[2871210]2061// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
[51b73452]2062
[c0aa336]2063cfa_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]2064 parameter_declaration
[994d080]2065 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initialize_opt
[4d51835]2066 { $$ = $1->addName( $2 ); }
[994d080]2067 | cfa_abstract_tuple identifier_or_type_name default_initialize_opt
[c0aa336]2068 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
[4d51835]2069 { $$ = $1->addName( $2 ); }
[994d080]2070 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initialize_opt
[4d51835]2071 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
[c0aa336]2072 | cfa_function_specifier
[4d51835]2073 ;
[b87a5ed]2074
[c0aa336]2075cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]2076 abstract_parameter_declaration
[c0aa336]2077 | cfa_identifier_parameter_declarator_no_tuple
2078 | cfa_abstract_tuple
2079 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2080 | type_qualifier_list cfa_abstract_tuple
[4d51835]2081 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]2082 | cfa_abstract_function
[4d51835]2083 ;
[51b73452]2084
2085parameter_declaration:
[d0ffed1]2086 // No SUE declaration in parameter list.
[994d080]2087 declaration_specifier_nobody identifier_parameter_declarator default_initialize_opt
[4d51835]2088 {
2089 typedefTable.addToEnclosingScope( TypedefTable::ID );
[a7741435]2090 $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
[4d51835]2091 }
[994d080]2092 | declaration_specifier_nobody type_parameter_redeclarator default_initialize_opt
[4d51835]2093 {
2094 typedefTable.addToEnclosingScope( TypedefTable::ID );
[a7741435]2095 $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
[4d51835]2096 }
2097 ;
[51b73452]2098
2099abstract_parameter_declaration:
[994d080]2100 declaration_specifier_nobody default_initialize_opt
[e7cc8cb]2101 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
[994d080]2102 | declaration_specifier_nobody abstract_parameter_declarator default_initialize_opt
[e7cc8cb]2103 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[4d51835]2104 ;
[51b73452]2105
[c11e31c]2106// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
[de62360d]2107// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2108// identifiers. The ANSI-style parameter-list can redefine a typedef name.
[51b73452]2109
[b87a5ed]2110identifier_list: // K&R-style parameter list => no types
[4d51835]2111 no_attr_identifier
2112 { $$ = DeclarationNode::newName( $1 ); }
2113 | identifier_list ',' no_attr_identifier
2114 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2115 ;
[51b73452]2116
[2871210]2117identifier_or_type_name:
[4d51835]2118 identifier
2119 | TYPEDEFname
2120 | TYPEGENname
2121 ;
[51b73452]2122
[2871210]2123no_attr_identifier_or_type_name:
[4d51835]2124 no_attr_identifier
2125 | TYPEDEFname
[721f17a]2126 | TYPEGENname
[4d51835]2127 ;
[b87a5ed]2128
[84d58c5]2129type_no_function: // sizeof, alignof, cast (constructor)
[c0aa336]2130 cfa_abstract_declarator_tuple // CFA
[4d51835]2131 | type_specifier
[c0aa336]2132 | type_specifier abstract_declarator
[4d51835]2133 { $$ = $2->addType( $1 ); }
2134 ;
[b87a5ed]2135
[84d58c5]2136type: // typeof, assertion
2137 type_no_function
[c0aa336]2138 | cfa_abstract_function // CFA
[4d51835]2139 ;
[51b73452]2140
2141initializer_opt:
[4d51835]2142 // empty
[58dd019]2143 { $$ = nullptr; }
[2871210]2144 | '=' initializer
2145 { $$ = $2; }
[641c3d0]2146 | '=' VOID
2147 { $$ = nullptr; }
[097e2b0]2148 | ATassign initializer
[974906e2]2149 { $$ = $2->set_maybeConstructed( false ); }
[4d51835]2150 ;
[51b73452]2151
2152initializer:
[de62360d]2153 assignment_expression { $$ = new InitializerNode( $1 ); }
2154 | '{' initializer_list comma_opt '}' { $$ = new InitializerNode( $2, true ); }
[4d51835]2155 ;
[51b73452]2156
2157initializer_list:
[097e2b0]2158 // empty
[58dd019]2159 { $$ = nullptr; }
[097e2b0]2160 | initializer
[4d51835]2161 | designation initializer { $$ = $2->set_designators( $1 ); }
[1d4580a]2162 | initializer_list ',' initializer { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
[4d51835]2163 | initializer_list ',' designation initializer
[1d4580a]2164 { $$ = (InitializerNode *)( $1->set_last( $4->set_designators( $3 ) ) ); }
[4d51835]2165 ;
[b87a5ed]2166
[de62360d]2167// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2168// '=' to separator the designator from the initializer value, as in:
[c11e31c]2169//
[b87a5ed]2170// int x[10] = { [1] = 3 };
[c11e31c]2171//
[de62360d]2172// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
2173// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2174// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2175// shift/reduce conflicts
[51b73452]2176
2177designation:
[4d51835]2178 designator_list ':' // C99, CFA uses ":" instead of "="
[84d58c5]2179 | no_attr_identifier ':' // GCC, field name
[d1625f8]2180 { $$ = new ExpressionNode( build_varref( $1 ) ); }
[4d51835]2181 ;
[51b73452]2182
[b87a5ed]2183designator_list: // C99
[4d51835]2184 designator
[2871210]2185 | designator_list designator
[1d4580a]2186 { $$ = (ExpressionNode *)( $1->set_last( $2 ) ); }
[d1625f8]2187 //| designator_list designator { $$ = new ExpressionNode( $1, $2 ); }
[4d51835]2188 ;
[51b73452]2189
2190designator:
[84d58c5]2191 '.' no_attr_identifier // C99, field name
[d1625f8]2192 { $$ = new ExpressionNode( build_varref( $2 ) ); }
[4d51835]2193 | '[' push assignment_expression pop ']' // C99, single array element
[de62360d]2194 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
[d1625f8]2195 { $$ = $3; }
[4d51835]2196 | '[' push subrange pop ']' // CFA, multiple array elements
[d1625f8]2197 { $$ = $3; }
[4d51835]2198 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
[db70fe4]2199 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $3 ), maybeMoveBuild< Expression >( $5 ) ) ); }
[4d51835]2200 | '.' '[' push field_list pop ']' // CFA, tuple field selector
[d1625f8]2201 { $$ = $4; }
[4d51835]2202 ;
[51b73452]2203
[de62360d]2204// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2205// rather than an object-oriented type system. This required four groups of extensions:
[c11e31c]2206//
2207// Overloading: function, data, and operator identifiers may be overloaded.
2208//
[de62360d]2209// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2210// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2211// definitions of new types. Type declarations with storage class "extern" provide opaque types.
[c11e31c]2212//
[de62360d]2213// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2214// site. A polymorphic function is not a template; it is a function, with an address and a type.
[c11e31c]2215//
2216// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
[de62360d]2217// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2218// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
2219// types provide the operations declared by a specification. Assertions are normally used to declare requirements
2220// on type arguments of polymorphic functions.
[c11e31c]2221
[b87a5ed]2222type_parameter_list: // CFA
[67cf18c]2223 type_parameter
2224 { $$ = $1; }
2225 | type_parameter_list ',' type_parameter
[4d51835]2226 { $$ = $1->appendList( $3 ); }
2227 ;
[b87a5ed]2228
[84d58c5]2229type_initializer_opt: // CFA
2230 // empty
2231 { $$ = nullptr; }
2232 | '=' type
2233 { $$ = $2; }
2234 ;
2235
[b87a5ed]2236type_parameter: // CFA
[2871210]2237 type_class no_attr_identifier_or_type_name
2238 { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
[67cf18c]2239 type_initializer_opt assertion_list_opt
2240 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
[4d51835]2241 | type_specifier identifier_parameter_declarator
[9997fee]2242 | assertion_list
2243 { $$ = DeclarationNode::newTypeParam( DeclarationNode::Dtype, new string( DeclarationNode::anonymous.newName() ) )->addAssertions( $1 ); }
[4d51835]2244 ;
[b87a5ed]2245
2246type_class: // CFA
[4040425]2247 OTYPE
[5b639ee]2248 { $$ = DeclarationNode::Otype; }
[4d51835]2249 | DTYPE
2250 { $$ = DeclarationNode::Dtype; }
[8f60f0b]2251 | FTYPE
2252 { $$ = DeclarationNode::Ftype; }
2253 | TTYPE
2254 { $$ = DeclarationNode::Ttype; }
[4d51835]2255 ;
[b87a5ed]2256
2257assertion_list_opt: // CFA
[4d51835]2258 // empty
[58dd019]2259 { $$ = nullptr; }
[9997fee]2260 | assertion_list
2261 ;
2262
2263assertion_list: // CFA
2264 assertion
2265 | assertion_list assertion
[a7741435]2266 { $$ = $1 ? $1->appendList( $2 ) : $2; }
[4d51835]2267 ;
[b87a5ed]2268
2269assertion: // CFA
[84d58c5]2270 '|' no_attr_identifier_or_type_name '(' type_list ')'
[4d51835]2271 {
[4040425]2272 typedefTable.openTrait( *$2 );
2273 $$ = DeclarationNode::newTraitUse( $2, $4 );
[4d51835]2274 }
[4040425]2275 | '|' '{' push trait_declaration_list '}'
[4d51835]2276 { $$ = $4; }
[84d58c5]2277 | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list '}' '(' type_list ')'
[58dd019]2278 { $$ = nullptr; }
[4d51835]2279 ;
[b87a5ed]2280
[84d58c5]2281type_list: // CFA
2282 type
[513e165]2283 { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
[4d51835]2284 | assignment_expression
[84d58c5]2285 | type_list ',' type
[513e165]2286 { $$ = (ExpressionNode *)( $1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) ) ); }
[84d58c5]2287 | type_list ',' assignment_expression
[1d4580a]2288 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]2289 ;
[b87a5ed]2290
2291type_declaring_list: // CFA
[4040425]2292 OTYPE type_declarator
[4d51835]2293 { $$ = $2; }
[4040425]2294 | storage_class_list OTYPE type_declarator
[4d51835]2295 { $$ = $3->addQualifiers( $1 ); }
2296 | type_declaring_list ',' type_declarator
[a7c90d4]2297 { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
[4d51835]2298 ;
[b87a5ed]2299
2300type_declarator: // CFA
[4d51835]2301 type_declarator_name assertion_list_opt
2302 { $$ = $1->addAssertions( $2 ); }
[84d58c5]2303 | type_declarator_name assertion_list_opt '=' type
[4d51835]2304 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2305 ;
[b87a5ed]2306
2307type_declarator_name: // CFA
[2871210]2308 no_attr_identifier_or_type_name
[4d51835]2309 {
[de62360d]2310 typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
[4d51835]2311 $$ = DeclarationNode::newTypeDecl( $1, 0 );
2312 }
[84d58c5]2313 | no_attr_identifier_or_type_name '(' push type_parameter_list pop ')'
[4d51835]2314 {
[de62360d]2315 typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
[4d51835]2316 $$ = DeclarationNode::newTypeDecl( $1, $4 );
2317 }
2318 ;
[b87a5ed]2319
[4040425]2320trait_specifier: // CFA
2321 TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
[4d51835]2322 {
[de62360d]2323 typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4040425]2324 $$ = DeclarationNode::newTrait( $2, $5, 0 );
[4d51835]2325 }
[4040425]2326 | TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
[4d51835]2327 {
[4040425]2328 typedefTable.enterTrait( *$2 );
[4d51835]2329 typedefTable.enterScope();
2330 }
[4040425]2331 trait_declaration_list '}'
[4d51835]2332 {
[4040425]2333 typedefTable.leaveTrait();
[de62360d]2334 typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4040425]2335 $$ = DeclarationNode::newTrait( $2, $5, $10 );
[4d51835]2336 }
2337 ;
[b87a5ed]2338
[84d58c5]2339trait_declaration_list: // CFA
[4040425]2340 trait_declaration
2341 | trait_declaration_list push trait_declaration
[4d51835]2342 { $$ = $1->appendList( $3 ); }
2343 ;
[b87a5ed]2344
[84d58c5]2345trait_declaration: // CFA
[c0aa336]2346 cfa_trait_declaring_list pop ';'
[4040425]2347 | trait_declaring_list pop ';'
[4d51835]2348 ;
[b87a5ed]2349
[c0aa336]2350cfa_trait_declaring_list: // CFA
2351 cfa_variable_specifier
[4d51835]2352 {
2353 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2354 $$ = $1;
2355 }
[c0aa336]2356 | cfa_function_specifier
[4d51835]2357 {
2358 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2359 $$ = $1;
2360 }
[c0aa336]2361 | cfa_trait_declaring_list pop ',' push identifier_or_type_name
[4d51835]2362 {
[de62360d]2363 typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
[4d51835]2364 $$ = $1->appendList( $1->cloneType( $5 ) );
2365 }
2366 ;
[b87a5ed]2367
[4040425]2368trait_declaring_list: // CFA
[4d51835]2369 type_specifier declarator
2370 {
2371 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2372 $$ = $2->addType( $1 );
2373 }
[4040425]2374 | trait_declaring_list pop ',' push declarator
[4d51835]2375 {
2376 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2377 $$ = $1->appendList( $1->cloneBaseType( $5 ) );
2378 }
2379 ;
[51b73452]2380
[c11e31c]2381//***************************** EXTERNAL DEFINITIONS *****************************
[51b73452]2382
2383translation_unit:
[4d51835]2384 // empty
2385 {} // empty input file
2386 | external_definition_list
[a7741435]2387 { parseTree = parseTree ? parseTree->appendList( $1 ) : $1; }
[4d51835]2388 ;
[51b73452]2389
2390external_definition_list:
[4d51835]2391 external_definition
[9997fee]2392 | external_definition_list { forall = xxx; } push external_definition
2393 { $$ = $1 ? $1->appendList( $4 ) : $4; }
[4d51835]2394 ;
[51b73452]2395
2396external_definition_list_opt:
[4d51835]2397 // empty
[58dd019]2398 { $$ = nullptr; }
[4d51835]2399 | external_definition_list
2400 ;
[51b73452]2401
2402external_definition:
[4d51835]2403 declaration
2404 | external_function_definition
[e994912]2405 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
2406 {
[6d539f83]2407 $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( false, $3, 0 ) ) );
[e994912]2408 }
[c0aa336]2409 | EXTERN STRINGliteral // C++-style linkage specifier
[4d51835]2410 {
[3b8e52c]2411 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
[d55d7a6]2412 linkage = LinkageSpec::linkageUpdate( yylloc, linkage, $2 );
[4d51835]2413 }
[c0aa336]2414 '{' external_definition_list_opt '}'
[4d51835]2415 {
2416 linkage = linkageStack.top();
2417 linkageStack.pop();
2418 $$ = $5;
2419 }
[c0aa336]2420 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
2421 {
2422 distExt( $2 ); // mark all fields in list
[8e9cbb2]2423 $$ = $2;
2424 }
[9997fee]2425 | type_qualifier_list
2426 {
2427 if ( $1->type->forall ) xxx = forall = true; // remember generic type
2428 }
2429 push '{' external_definition_list '}' // CFA, namespace
2430 {
2431 for ( DeclarationNode * iter = $5; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
2432 iter->addQualifiers( $1->clone() );
2433 } // for
2434 xxx = false;
2435 delete $1;
2436 $$ = $5;
2437 }
2438 | declaration_qualifier_list
2439 {
2440 if ( $1->type->forall ) xxx = forall = true; // remember generic type
2441 }
2442 push '{' external_definition_list '}' // CFA, namespace
2443 {
2444 for ( DeclarationNode * iter = $5; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
2445 iter->addQualifiers( $1->clone() );
2446 } // for
2447 xxx = false;
2448 delete $1;
2449 $$ = $5;
2450 }
2451 | declaration_qualifier_list type_qualifier_list
2452 {
2453 if ( $1->type->forall ) xxx = forall = true; // remember generic type
2454 }
2455 push '{' external_definition_list '}' // CFA, namespace
2456 {
2457 for ( DeclarationNode * iter = $6; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
2458 iter->addQualifiers( $1->clone() );
2459 iter->addQualifiers( $2->clone() );
2460 } // for
2461 xxx = false;
2462 delete $1;
2463 delete $2;
2464 $$ = $6;
2465 }
[4d51835]2466 ;
2467
2468external_function_definition:
2469 function_definition
[de62360d]2470 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
[c6b1105]2471 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2472 // Parsing is possible because function_definition does not appear in the context of an expression (nested
2473 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2474 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
[4d51835]2475 | function_declarator compound_statement
2476 {
2477 typedefTable.addToEnclosingScope( TypedefTable::ID );
2478 typedefTable.leaveScope();
2479 $$ = $1->addFunctionBody( $2 );
2480 }
[4cc585b]2481 | KR_function_declarator KR_declaration_list_opt compound_statement
[4d51835]2482 {
2483 typedefTable.addToEnclosingScope( TypedefTable::ID );
2484 typedefTable.leaveScope();
[4cc585b]2485 $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 );
[4d51835]2486 }
2487 ;
[51b73452]2488
[8b47e50]2489with_clause_opt:
2490 // empty
[9997fee]2491 { $$ = nullptr; forall = false; }
[578e6037]2492 | WITH '(' tuple_expression_list ')'
[9997fee]2493 { $$ = $3; forall = false; }
[8b47e50]2494 ;
2495
[51b73452]2496function_definition:
[8b47e50]2497 cfa_function_declaration with_clause_opt compound_statement // CFA
[4d51835]2498 {
2499 typedefTable.addToEnclosingScope( TypedefTable::ID );
2500 typedefTable.leaveScope();
[481115f]2501 // Add the function body to the last identifier in the function definition list, i.e., foo3:
2502 // [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
[5fcba14]2503 $1->get_last()->addFunctionBody( $3, $2 );
[481115f]2504 $$ = $1;
[4d51835]2505 }
[8b47e50]2506 | declaration_specifier function_declarator with_clause_opt compound_statement
[4d51835]2507 {
[c38ae92]2508 rebindForall( $1, $2 );
[4d51835]2509 typedefTable.addToEnclosingScope( TypedefTable::ID );
2510 typedefTable.leaveScope();
[5fcba14]2511 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
[4d51835]2512 }
[a16a7ec]2513 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]2514 | type_qualifier_list function_declarator with_clause_opt compound_statement
[4d51835]2515 {
2516 typedefTable.addToEnclosingScope( TypedefTable::ID );
2517 typedefTable.leaveScope();
[5fcba14]2518 $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 );
[4d51835]2519 }
[a16a7ec]2520 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]2521 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
[4d51835]2522 {
2523 typedefTable.addToEnclosingScope( TypedefTable::ID );
2524 typedefTable.leaveScope();
[5fcba14]2525 $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 );
[4d51835]2526 }
[a16a7ec]2527 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]2528 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
[4d51835]2529 {
2530 typedefTable.addToEnclosingScope( TypedefTable::ID );
2531 typedefTable.leaveScope();
[5fcba14]2532 $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 );
[4d51835]2533 }
2534
2535 // Old-style K&R function definition, OBSOLESCENT (see 4)
[4cc585b]2536 | declaration_specifier KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
[4d51835]2537 {
[c38ae92]2538 rebindForall( $1, $2 );
[4d51835]2539 typedefTable.addToEnclosingScope( TypedefTable::ID );
2540 typedefTable.leaveScope();
[5fcba14]2541 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
[4d51835]2542 }
[a16a7ec]2543 // handles default int return type, OBSOLESCENT (see 1)
[4cc585b]2544 | type_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
[4d51835]2545 {
2546 typedefTable.addToEnclosingScope( TypedefTable::ID );
2547 typedefTable.leaveScope();
[5fcba14]2548 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 );
[4d51835]2549 }
[a16a7ec]2550 // handles default int return type, OBSOLESCENT (see 1)
[4cc585b]2551 | declaration_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
[4d51835]2552 {
2553 typedefTable.addToEnclosingScope( TypedefTable::ID );
2554 typedefTable.leaveScope();
[5fcba14]2555 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 );
[4d51835]2556 }
[a16a7ec]2557 // handles default int return type, OBSOLESCENT (see 1)
[4cc585b]2558 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
[4d51835]2559 {
2560 typedefTable.addToEnclosingScope( TypedefTable::ID );
2561 typedefTable.leaveScope();
[5fcba14]2562 $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 );
[4d51835]2563 }
2564 ;
[51b73452]2565
2566declarator:
[4d51835]2567 variable_declarator
[c6b1105]2568 | variable_type_redeclarator
[4d51835]2569 | function_declarator
2570 ;
[51b73452]2571
2572subrange:
[4d51835]2573 constant_expression '~' constant_expression // CFA, integer subrange
[db70fe4]2574 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
[4d51835]2575 ;
[b87a5ed]2576
2577asm_name_opt: // GCC
[4d51835]2578 // empty
[58dd019]2579 { $$ = nullptr; }
2580 | ASM '(' string_literal ')' attribute_list_opt
[c0aa336]2581 {
2582 DeclarationNode * name = new DeclarationNode();
2583 name->asmName = $3;
2584 $$ = name->addQualifiers( $5 );
2585 }
[4d51835]2586 ;
[b87a5ed]2587
2588attribute_list_opt: // GCC
[4d51835]2589 // empty
[58dd019]2590 { $$ = nullptr; }
[4d51835]2591 | attribute_list
2592 ;
[b87a5ed]2593
2594attribute_list: // GCC
[4d51835]2595 attribute
2596 | attribute_list attribute
[1db21619]2597 { $$ = $2->addQualifiers( $1 ); }
[4d51835]2598 ;
[b87a5ed]2599
2600attribute: // GCC
[44a81853]2601 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
2602 { $$ = $4; }
[4d51835]2603 ;
[b87a5ed]2604
[44a81853]2605attribute_name_list: // GCC
2606 attribute_name
2607 | attribute_name_list ',' attribute_name
[c0aa336]2608 { $$ = $3->addQualifiers( $1 ); }
[4d51835]2609 ;
[b87a5ed]2610
[44a81853]2611attribute_name: // GCC
[4d51835]2612 // empty
[44a81853]2613 { $$ = nullptr; }
2614 | attr_name
2615 { $$ = DeclarationNode::newAttribute( $1 ); }
2616 | attr_name '(' argument_expression_list ')'
2617 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
[4d51835]2618 ;
[b87a5ed]2619
[44a81853]2620attr_name: // GCC
2621 IDENTIFIER
[5b2edbc]2622 | quasi_keyword
[44a81853]2623 | TYPEDEFname
2624 | TYPEGENname
[114014c]2625 | FALLTHROUGH
2626 { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
[44a81853]2627 | CONST
[9ff56e7]2628 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
[4d51835]2629 ;
[51b73452]2630
[c11e31c]2631// ============================================================================
[de62360d]2632// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2633// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2634// in:
[c11e31c]2635//
[b87a5ed]2636// int (*f())[10] { ... };
2637// ... (*f())[3] += 1; // definition mimics usage
[c11e31c]2638//
[de62360d]2639// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2640// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
[c11e31c]2641// ============================================================================
2642
2643// ----------------------------------------------------------------------------
[de62360d]2644// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2645// to define a variable or function prototype, e.g.:
[c11e31c]2646//
[b87a5ed]2647// valid declaration invalid definition
2648// ----------------- ------------------
[4d51835]2649// int f; int f {}
2650// int *f; int *f {}
[b87a5ed]2651// int f[10]; int f[10] {}
[4d51835]2652// int (*f)(int); int (*f)(int) {}
[c11e31c]2653//
[de62360d]2654// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2655// variable_declarator and function_declarator.
[c11e31c]2656// ----------------------------------------------------------------------------
2657
[de62360d]2658// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2659// declaring an array of functions versus a pointer to an array of functions.
[51b73452]2660
2661variable_declarator:
[4d51835]2662 paren_identifier attribute_list_opt
[1db21619]2663 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2664 | variable_ptr
2665 | variable_array attribute_list_opt
[1db21619]2666 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2667 | variable_function attribute_list_opt
[1db21619]2668 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2669 ;
[51b73452]2670
2671paren_identifier:
[4d51835]2672 identifier
2673 {
[de62360d]2674 typedefTable.setNextIdentifier( *$1 );
[4d51835]2675 $$ = DeclarationNode::newName( $1 );
2676 }
2677 | '(' paren_identifier ')' // redundant parenthesis
2678 { $$ = $2; }
2679 ;
[51b73452]2680
2681variable_ptr:
[dd51906]2682 ptrref_operator variable_declarator
[ce8c12f]2683 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2684 | ptrref_operator type_qualifier_list variable_declarator
[ce8c12f]2685 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2686 | '(' variable_ptr ')' attribute_list_opt
2687 { $$ = $2->addQualifiers( $4 ); } // redundant parenthesis
[4d51835]2688 ;
[51b73452]2689
2690variable_array:
[4d51835]2691 paren_identifier array_dimension
2692 { $$ = $1->addArray( $2 ); }
2693 | '(' variable_ptr ')' array_dimension
2694 { $$ = $2->addArray( $4 ); }
2695 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
2696 { $$ = $2->addArray( $4 ); }
2697 | '(' variable_array ')' // redundant parenthesis
2698 { $$ = $2; }
2699 ;
[51b73452]2700
2701variable_function:
[4d51835]2702 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2703 { $$ = $2->addParamList( $6 ); }
2704 | '(' variable_function ')' // redundant parenthesis
2705 { $$ = $2; }
2706 ;
[51b73452]2707
[c6b1105]2708// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2709// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2710// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
[51b73452]2711
2712function_declarator:
[4d51835]2713 function_no_ptr attribute_list_opt
[1db21619]2714 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2715 | function_ptr
2716 | function_array attribute_list_opt
[1db21619]2717 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2718 ;
[51b73452]2719
2720function_no_ptr:
[4d51835]2721 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2722 { $$ = $1->addParamList( $4 ); }
2723 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2724 { $$ = $2->addParamList( $6 ); }
2725 | '(' function_no_ptr ')' // redundant parenthesis
2726 { $$ = $2; }
2727 ;
[51b73452]2728
2729function_ptr:
[dd51906]2730 ptrref_operator function_declarator
[ce8c12f]2731 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2732 | ptrref_operator type_qualifier_list function_declarator
[ce8c12f]2733 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[4d51835]2734 | '(' function_ptr ')'
2735 { $$ = $2; }
2736 ;
[51b73452]2737
2738function_array:
[4d51835]2739 '(' function_ptr ')' array_dimension
2740 { $$ = $2->addArray( $4 ); }
2741 | '(' function_array ')' multi_array_dimension // redundant parenthesis
2742 { $$ = $2->addArray( $4 ); }
2743 | '(' function_array ')' // redundant parenthesis
2744 { $$ = $2; }
2745 ;
[51b73452]2746
[c0aa336]2747// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
2748//
2749// f( a, b, c ) int a, *b, c[]; {}
2750//
2751// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
2752// returning arrays and functions versus pointers to arrays and functions.
[51b73452]2753
[c0aa336]2754KR_function_declarator:
2755 KR_function_no_ptr
2756 | KR_function_ptr
2757 | KR_function_array
[4d51835]2758 ;
[51b73452]2759
[c0aa336]2760KR_function_no_ptr:
[4d51835]2761 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
2762 { $$ = $1->addIdList( $3 ); }
[c0aa336]2763 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
2764 { $$ = $2->addParamList( $6 ); }
2765 | '(' KR_function_no_ptr ')' // redundant parenthesis
[4d51835]2766 { $$ = $2; }
2767 ;
[51b73452]2768
[c0aa336]2769KR_function_ptr:
2770 ptrref_operator KR_function_declarator
[ce8c12f]2771 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]2772 | ptrref_operator type_qualifier_list KR_function_declarator
[ce8c12f]2773 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2774 | '(' KR_function_ptr ')'
[4d51835]2775 { $$ = $2; }
2776 ;
[51b73452]2777
[c0aa336]2778KR_function_array:
2779 '(' KR_function_ptr ')' array_dimension
[4d51835]2780 { $$ = $2->addArray( $4 ); }
[c0aa336]2781 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
[4d51835]2782 { $$ = $2->addArray( $4 ); }
[c0aa336]2783 | '(' KR_function_array ')' // redundant parenthesis
[4d51835]2784 { $$ = $2; }
2785 ;
[51b73452]2786
[2871210]2787// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
[c11e31c]2788//
[b87a5ed]2789// typedef int foo;
2790// {
2791// int foo; // redefine typedef name in new scope
2792// }
[c11e31c]2793//
[de62360d]2794// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2795// and functions versus pointers to arrays and functions.
[51b73452]2796
[c6b1105]2797variable_type_redeclarator:
[2871210]2798 paren_type attribute_list_opt
[1db21619]2799 { $$ = $1->addQualifiers( $2 ); }
[2871210]2800 | type_ptr
2801 | type_array attribute_list_opt
[1db21619]2802 { $$ = $1->addQualifiers( $2 ); }
[2871210]2803 | type_function attribute_list_opt
[1db21619]2804 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2805 ;
[51b73452]2806
[2871210]2807paren_type:
2808 typedef
2809 | '(' paren_type ')'
[4d51835]2810 { $$ = $2; }
2811 ;
[51b73452]2812
[2871210]2813type_ptr:
[c6b1105]2814 ptrref_operator variable_type_redeclarator
[ce8c12f]2815 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c6b1105]2816 | ptrref_operator type_qualifier_list variable_type_redeclarator
[ce8c12f]2817 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2818 | '(' type_ptr ')' attribute_list_opt
2819 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2820 ;
[51b73452]2821
[2871210]2822type_array:
2823 paren_type array_dimension
[4d51835]2824 { $$ = $1->addArray( $2 ); }
[2871210]2825 | '(' type_ptr ')' array_dimension
[4d51835]2826 { $$ = $2->addArray( $4 ); }
[2871210]2827 | '(' type_array ')' multi_array_dimension // redundant parenthesis
[4d51835]2828 { $$ = $2->addArray( $4 ); }
[2871210]2829 | '(' type_array ')' // redundant parenthesis
[4d51835]2830 { $$ = $2; }
2831 ;
[51b73452]2832
[2871210]2833type_function:
2834 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2835 { $$ = $1->addParamList( $4 ); }
[2871210]2836 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2837 { $$ = $2->addParamList( $6 ); }
[2871210]2838 | '(' type_function ')' // redundant parenthesis
[4d51835]2839 { $$ = $2; }
2840 ;
[51b73452]2841
[c0aa336]2842// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
2843// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
2844// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
2845// pointers to arrays and functions.
[51b73452]2846
2847identifier_parameter_declarator:
[4d51835]2848 paren_identifier attribute_list_opt
[1db21619]2849 { $$ = $1->addQualifiers( $2 ); }
[b6b3c42]2850 | '&' MUTEX paren_identifier attribute_list_opt
2851 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
[4d51835]2852 | identifier_parameter_ptr
2853 | identifier_parameter_array attribute_list_opt
[1db21619]2854 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2855 | identifier_parameter_function attribute_list_opt
[1db21619]2856 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2857 ;
[51b73452]2858
2859identifier_parameter_ptr:
[dd51906]2860 ptrref_operator identifier_parameter_declarator
[ce8c12f]2861 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2862 | ptrref_operator type_qualifier_list identifier_parameter_declarator
[ce8c12f]2863 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2864 | '(' identifier_parameter_ptr ')' attribute_list_opt
2865 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2866 ;
[51b73452]2867
2868identifier_parameter_array:
[4d51835]2869 paren_identifier array_parameter_dimension
2870 { $$ = $1->addArray( $2 ); }
2871 | '(' identifier_parameter_ptr ')' array_dimension
2872 { $$ = $2->addArray( $4 ); }
2873 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2874 { $$ = $2->addArray( $4 ); }
2875 | '(' identifier_parameter_array ')' // redundant parenthesis
2876 { $$ = $2; }
2877 ;
[51b73452]2878
2879identifier_parameter_function:
[4d51835]2880 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2881 { $$ = $1->addParamList( $4 ); }
2882 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2883 { $$ = $2->addParamList( $6 ); }
2884 | '(' identifier_parameter_function ')' // redundant parenthesis
2885 { $$ = $2; }
2886 ;
[b87a5ed]2887
[de62360d]2888// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2889// e.g.:
[c11e31c]2890//
[b87a5ed]2891// typedef int foo;
[114014c]2892// forall( otype T ) struct foo;
[b87a5ed]2893// int f( int foo ); // redefine typedef name in new scope
[c11e31c]2894//
[c0aa336]2895// and allows the C99 array options, which can only appear in a parameter list.
[51b73452]2896
[2871210]2897type_parameter_redeclarator:
[4d51835]2898 typedef attribute_list_opt
[1db21619]2899 { $$ = $1->addQualifiers( $2 ); }
[b6b3c42]2900 | '&' MUTEX typedef attribute_list_opt
2901 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
[2871210]2902 | type_parameter_ptr
2903 | type_parameter_array attribute_list_opt
[1db21619]2904 { $$ = $1->addQualifiers( $2 ); }
[2871210]2905 | type_parameter_function attribute_list_opt
[1db21619]2906 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2907 ;
[51b73452]2908
2909typedef:
[4d51835]2910 TYPEDEFname
2911 {
[de62360d]2912 typedefTable.setNextIdentifier( *$1 );
[4d51835]2913 $$ = DeclarationNode::newName( $1 );
2914 }
[2871210]2915 | TYPEGENname
2916 {
2917 typedefTable.setNextIdentifier( *$1 );
2918 $$ = DeclarationNode::newName( $1 );
2919 }
[4d51835]2920 ;
[51b73452]2921
[2871210]2922type_parameter_ptr:
[dd51906]2923 ptrref_operator type_parameter_redeclarator
[ce8c12f]2924 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2925 | ptrref_operator type_qualifier_list type_parameter_redeclarator
[ce8c12f]2926 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2927 | '(' type_parameter_ptr ')' attribute_list_opt
2928 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2929 ;
[51b73452]2930
[2871210]2931type_parameter_array:
[4d51835]2932 typedef array_parameter_dimension
2933 { $$ = $1->addArray( $2 ); }
[2871210]2934 | '(' type_parameter_ptr ')' array_parameter_dimension
[4d51835]2935 { $$ = $2->addArray( $4 ); }
2936 ;
[51b73452]2937
[2871210]2938type_parameter_function:
[4d51835]2939 typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2940 { $$ = $1->addParamList( $4 ); }
[2871210]2941 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2942 { $$ = $2->addParamList( $6 ); }
2943 ;
[b87a5ed]2944
[de62360d]2945// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2946// which the type applies, e.g.:
[c11e31c]2947//
[b87a5ed]2948// sizeof( int );
[c0aa336]2949// sizeof( int * );
[b87a5ed]2950// sizeof( int [10] );
[c0aa336]2951// sizeof( int (*)() );
2952// sizeof( int () );
[c11e31c]2953//
[de62360d]2954// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2955// and functions versus pointers to arrays and functions.
[51b73452]2956
2957abstract_declarator:
[4d51835]2958 abstract_ptr
2959 | abstract_array attribute_list_opt
[1db21619]2960 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2961 | abstract_function attribute_list_opt
[1db21619]2962 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2963 ;
[51b73452]2964
2965abstract_ptr:
[dd51906]2966 ptrref_operator
[ce8c12f]2967 { $$ = DeclarationNode::newPointer( 0, $1 ); }
[dd51906]2968 | ptrref_operator type_qualifier_list
[ce8c12f]2969 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]2970 | ptrref_operator abstract_declarator
[ce8c12f]2971 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2972 | ptrref_operator type_qualifier_list abstract_declarator
[ce8c12f]2973 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2974 | '(' abstract_ptr ')' attribute_list_opt
2975 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2976 ;
[51b73452]2977
2978abstract_array:
[4d51835]2979 array_dimension
2980 | '(' abstract_ptr ')' array_dimension
2981 { $$ = $2->addArray( $4 ); }
2982 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
2983 { $$ = $2->addArray( $4 ); }
2984 | '(' abstract_array ')' // redundant parenthesis
2985 { $$ = $2; }
2986 ;
[51b73452]2987
2988abstract_function:
[4d51835]2989 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[2298f728]2990 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
[4d51835]2991 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2992 { $$ = $2->addParamList( $6 ); }
2993 | '(' abstract_function ')' // redundant parenthesis
2994 { $$ = $2; }
2995 ;
[51b73452]2996
2997array_dimension:
[4d51835]2998 // Only the first dimension can be empty.
[2871210]2999 '[' ']'
[4d51835]3000 { $$ = DeclarationNode::newArray( 0, 0, false ); }
[2871210]3001 | '[' ']' multi_array_dimension
3002 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
[4d51835]3003 | multi_array_dimension
3004 ;
[51b73452]3005
3006multi_array_dimension:
[4d51835]3007 '[' push assignment_expression pop ']'
3008 { $$ = DeclarationNode::newArray( $3, 0, false ); }
3009 | '[' push '*' pop ']' // C99
3010 { $$ = DeclarationNode::newVarArray( 0 ); }
3011 | multi_array_dimension '[' push assignment_expression pop ']'
3012 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
3013 | multi_array_dimension '[' push '*' pop ']' // C99
3014 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
3015 ;
[51b73452]3016
[c11e31c]3017// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
3018// identifier to which the type applies, e.g.:
3019//
[c0aa336]3020// int f( int ); // not handled here
3021// int f( int * ); // abstract function-prototype parameter; no parameter name specified
3022// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
[b87a5ed]3023// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]3024//
[de62360d]3025// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
[c0aa336]3026// and functions versus pointers to arrays and functions. In addition, the pattern handles the
3027// special meaning of parenthesis around a typedef name:
3028//
3029// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
3030// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
3031// not as redundant parentheses around the identifier."
3032//
3033// For example:
3034//
3035// typedef float T;
3036// int f( int ( T [5] ) ); // see abstract_parameter_declarator
3037// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
3038// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
3039// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
3040//
3041// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
3042// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
3043// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
3044// functions.
[51b73452]3045
3046abstract_parameter_declarator:
[4d51835]3047 abstract_parameter_ptr
[b6b3c42]3048 | '&' MUTEX attribute_list_opt
3049 { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
[4d51835]3050 | abstract_parameter_array attribute_list_opt
[1db21619]3051 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3052 | abstract_parameter_function attribute_list_opt
[1db21619]3053 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3054 ;
[51b73452]3055
3056abstract_parameter_ptr:
[dd51906]3057 ptrref_operator
[ce8c12f]3058 { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
[dd51906]3059 | ptrref_operator type_qualifier_list
[ce8c12f]3060 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]3061 | ptrref_operator abstract_parameter_declarator
[ce8c12f]3062 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[dd51906]3063 | ptrref_operator type_qualifier_list abstract_parameter_declarator
[ce8c12f]3064 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]3065 | '(' abstract_parameter_ptr ')' attribute_list_opt
3066 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3067 ;
[51b73452]3068
3069abstract_parameter_array:
[4d51835]3070 array_parameter_dimension
3071 | '(' abstract_parameter_ptr ')' array_parameter_dimension
3072 { $$ = $2->addArray( $4 ); }
3073 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
3074 { $$ = $2->addArray( $4 ); }
3075 | '(' abstract_parameter_array ')' // redundant parenthesis
3076 { $$ = $2; }
3077 ;
[51b73452]3078
3079abstract_parameter_function:
[4d51835]3080 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[2298f728]3081 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
[4d51835]3082 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3083 { $$ = $2->addParamList( $6 ); }
3084 | '(' abstract_parameter_function ')' // redundant parenthesis
3085 { $$ = $2; }
3086 ;
[51b73452]3087
3088array_parameter_dimension:
[4d51835]3089 // Only the first dimension can be empty or have qualifiers.
3090 array_parameter_1st_dimension
3091 | array_parameter_1st_dimension multi_array_dimension
3092 { $$ = $1->addArray( $2 ); }
3093 | multi_array_dimension
3094 ;
[51b73452]3095
[c11e31c]3096// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3097//
[de62360d]3098// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3099// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
[51b73452]3100
3101array_parameter_1st_dimension:
[2871210]3102 '[' ']'
[4d51835]3103 { $$ = DeclarationNode::newArray( 0, 0, false ); }
3104 // multi_array_dimension handles the '[' '*' ']' case
3105 | '[' push type_qualifier_list '*' pop ']' // remaining C99
3106 { $$ = DeclarationNode::newVarArray( $3 ); }
3107 | '[' push type_qualifier_list pop ']'
3108 { $$ = DeclarationNode::newArray( 0, $3, false ); }
3109 // multi_array_dimension handles the '[' assignment_expression ']' case
3110 | '[' push type_qualifier_list assignment_expression pop ']'
3111 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3112 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
3113 { $$ = DeclarationNode::newArray( $5, $4, true ); }
3114 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
3115 { $$ = DeclarationNode::newArray( $5, $3, true ); }
3116 ;
[b87a5ed]3117
[c0aa336]3118// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
[c11e31c]3119//
[c0aa336]3120// struct S {
3121// int;
3122// int *;
3123// int [10];
3124// int (*)();
3125// };
[51b73452]3126
3127variable_abstract_declarator:
[4d51835]3128 variable_abstract_ptr
3129 | variable_abstract_array attribute_list_opt
[1db21619]3130 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3131 | variable_abstract_function attribute_list_opt
[1db21619]3132 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3133 ;
[51b73452]3134
3135variable_abstract_ptr:
[dd51906]3136 ptrref_operator
[ce8c12f]3137 { $$ = DeclarationNode::newPointer( 0, $1 ); }
[dd51906]3138 | ptrref_operator type_qualifier_list
[ce8c12f]3139 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]3140 | ptrref_operator variable_abstract_declarator
[ce8c12f]3141 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3142 | ptrref_operator type_qualifier_list variable_abstract_declarator
[ce8c12f]3143 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]3144 | '(' variable_abstract_ptr ')' attribute_list_opt
3145 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3146 ;
[51b73452]3147
3148variable_abstract_array:
[4d51835]3149 array_dimension
3150 | '(' variable_abstract_ptr ')' array_dimension
3151 { $$ = $2->addArray( $4 ); }
3152 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3153 { $$ = $2->addArray( $4 ); }
3154 | '(' variable_abstract_array ')' // redundant parenthesis
3155 { $$ = $2; }
3156 ;
[51b73452]3157
3158variable_abstract_function:
[4d51835]3159 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3160 { $$ = $2->addParamList( $6 ); }
3161 | '(' variable_abstract_function ')' // redundant parenthesis
3162 { $$ = $2; }
3163 ;
[b87a5ed]3164
[de62360d]3165// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3166// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
[b87a5ed]3167
[c0aa336]3168cfa_identifier_parameter_declarator_tuple: // CFA
3169 cfa_identifier_parameter_declarator_no_tuple
3170 | cfa_abstract_tuple
3171 | type_qualifier_list cfa_abstract_tuple
[4d51835]3172 { $$ = $2->addQualifiers( $1 ); }
3173 ;
[b87a5ed]3174
[c0aa336]3175cfa_identifier_parameter_declarator_no_tuple: // CFA
3176 cfa_identifier_parameter_ptr
3177 | cfa_identifier_parameter_array
[4d51835]3178 ;
[b87a5ed]3179
[c0aa336]3180cfa_identifier_parameter_ptr: // CFA
[d0ffed1]3181 // No SUE declaration in parameter list.
3182 ptrref_operator type_specifier_nobody
[ce8c12f]3183 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[d0ffed1]3184 | type_qualifier_list ptrref_operator type_specifier_nobody
[ce8c12f]3185 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3186 | ptrref_operator cfa_abstract_function
[ce8c12f]3187 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3188 | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]3189 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3190 | ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]3191 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3192 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]3193 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]3194 ;
[b87a5ed]3195
[c0aa336]3196cfa_identifier_parameter_array: // CFA
[de62360d]3197 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3198 // shift/reduce conflict with new-style empty (void) function return type.
[d0ffed1]3199 '[' ']' type_specifier_nobody
[2871210]3200 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]3201 | cfa_array_parameter_1st_dimension type_specifier_nobody
[4d51835]3202 { $$ = $2->addNewArray( $1 ); }
[d0ffed1]3203 | '[' ']' multi_array_dimension type_specifier_nobody
[2871210]3204 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]3205 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
[4d51835]3206 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[d0ffed1]3207 | multi_array_dimension type_specifier_nobody
[4d51835]3208 { $$ = $2->addNewArray( $1 ); }
[9059213]3209
[c0aa336]3210 | '[' ']' cfa_identifier_parameter_ptr
[2871210]3211 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]3212 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
[4d51835]3213 { $$ = $2->addNewArray( $1 ); }
[c0aa336]3214 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
[2871210]3215 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]3216 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]3217 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[c0aa336]3218 | multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]3219 { $$ = $2->addNewArray( $1 ); }
3220 ;
[51b73452]3221
[c0aa336]3222cfa_array_parameter_1st_dimension:
[4d51835]3223 '[' push type_qualifier_list '*' pop ']' // remaining C99
3224 { $$ = DeclarationNode::newVarArray( $3 ); }
3225 | '[' push type_qualifier_list assignment_expression pop ']'
3226 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3227 | '[' push declaration_qualifier_list assignment_expression pop ']'
3228 // declaration_qualifier_list must be used because of shift/reduce conflict with
3229 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3230 // appear in this context.
3231 { $$ = DeclarationNode::newArray( $4, $3, true ); }
3232 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3233 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
3234 ;
[b87a5ed]3235
[de62360d]3236// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3237// identifier to which the type applies, e.g.:
[c11e31c]3238//
[b87a5ed]3239// [int] f( int ); // abstract variable parameter; no parameter name specified
3240// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]3241//
3242// These rules need LR(3):
3243//
[c0aa336]3244// cfa_abstract_tuple identifier_or_type_name
3245// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
[c11e31c]3246//
3247// since a function return type can be syntactically identical to a tuple type:
3248//
[b87a5ed]3249// [int, int] t;
3250// [int, int] f( int );
[c11e31c]3251//
[2871210]3252// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
[c0aa336]3253// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3254// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3255// duplicated when appearing with cfa_function_specifier.
[b87a5ed]3256
[c0aa336]3257cfa_abstract_declarator_tuple: // CFA
3258 cfa_abstract_tuple
3259 | type_qualifier_list cfa_abstract_tuple
[4d51835]3260 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]3261 | cfa_abstract_declarator_no_tuple
[4d51835]3262 ;
[b87a5ed]3263
[c0aa336]3264cfa_abstract_declarator_no_tuple: // CFA
3265 cfa_abstract_ptr
3266 | cfa_abstract_array
[4d51835]3267 ;
[b87a5ed]3268
[c0aa336]3269cfa_abstract_ptr: // CFA
[dd51906]3270 ptrref_operator type_specifier
[ce8c12f]3271 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3272 | type_qualifier_list ptrref_operator type_specifier
[ce8c12f]3273 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3274 | ptrref_operator cfa_abstract_function
[ce8c12f]3275 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3276 | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]3277 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3278 | ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]3279 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3280 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]3281 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]3282 ;
[b87a5ed]3283
[c0aa336]3284cfa_abstract_array: // CFA
[de62360d]3285 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3286 // empty (void) function return type.
[2871210]3287 '[' ']' type_specifier
[2298f728]3288 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[2871210]3289 | '[' ']' multi_array_dimension type_specifier
[2298f728]3290 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[4d51835]3291 | multi_array_dimension type_specifier
3292 { $$ = $2->addNewArray( $1 ); }
[c0aa336]3293 | '[' ']' cfa_abstract_ptr
[2298f728]3294 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]3295 | '[' ']' multi_array_dimension cfa_abstract_ptr
[2298f728]3296 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]3297 | multi_array_dimension cfa_abstract_ptr
[4d51835]3298 { $$ = $2->addNewArray( $1 ); }
3299 ;
[b87a5ed]3300
[c0aa336]3301cfa_abstract_tuple: // CFA
3302 '[' push cfa_abstract_parameter_list pop ']'
[4d51835]3303 { $$ = DeclarationNode::newTuple( $3 ); }
3304 ;
[b87a5ed]3305
[c0aa336]3306cfa_abstract_function: // CFA
3307// '[' ']' '(' cfa_parameter_type_list_opt ')'
[1b29996]3308// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
[c0aa336]3309 cfa_abstract_tuple '(' push cfa_parameter_type_list_opt pop ')'
[2298f728]3310 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[c0aa336]3311 | cfa_function_return '(' push cfa_parameter_type_list_opt pop ')'
[2298f728]3312 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[4d51835]3313 ;
[b87a5ed]3314
[de62360d]3315// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3316// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
[c11e31c]3317//
[de62360d]3318// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3319// the declaration specifiers in a declaration is an obsolescent feature."
[c11e31c]3320//
3321// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3322// prototype-format parameter type declarators) is an obsolescent feature."
3323//
[de62360d]3324// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3325// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
[51b73452]3326
[c11e31c]3327//************************* MISCELLANEOUS ********************************
[51b73452]3328
[b87a5ed]3329comma_opt: // redundant comma
[4d51835]3330 // empty
3331 | ','
3332 ;
[51b73452]3333
[994d080]3334default_initialize_opt:
[4d51835]3335 // empty
[58dd019]3336 { $$ = nullptr; }
[4d51835]3337 | '=' assignment_expression
3338 { $$ = $2; }
3339 ;
[51b73452]3340
3341%%
[c11e31c]3342// ----end of grammar----
[51b73452]3343
[c11e31c]3344// Local Variables: //
[b87a5ed]3345// mode: c++ //
[de62360d]3346// tab-width: 4 //
[c11e31c]3347// compile-command: "make install" //
3348// End: //
Note: See TracBrowser for help on using the repository browser.