source: src/Parser/parser.yy@ cb7caf8

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since cb7caf8 was 054514d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

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