source: src/Parser/parser.yy@ a1cfa0c

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
Last change on this file since a1cfa0c was 284da8c, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

cleanup, fix distribution order, add generic declaration/instantiation, extra qualifier check

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