source: src/Parser/parser.yy@ 7e4c4f4

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 resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 7e4c4f4 was 65d6de4, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add TYPEGENname to typegen_name, and fix shift/reduce in favour of shift

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