source: src/Parser/parser.yy@ eff03a94

new-env
Last change on this file since eff03a94 was 3d56d15b, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add push/pop up/down, semantic check for CV in distribution, initial check for structure names

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