source: src/Parser/parser.yy@ 04cccaf

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 04cccaf was 3007c0b, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add error checks in the parser for generic parameters that are expressions [fixes #99]

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