source: src/Parser/parser.yy@ 69efef81

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 no_list persistent-indexer pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 69efef81 was dea36ee, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

simplify code for field declarations

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