source: src/Parser/parser.yy@ b93a3de

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 b93a3de was b6b3c42, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add mutex statement, and restrict mutex qualifier to only one occurrence in parameter list

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