source: src/Parser/parser.yy@ b51e5fdb

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 with_gc
Last change on this file since b51e5fdb was a16764a6, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Changed warning system to prepare for toggling warnings

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