source: src/Parser/parser.yy@ 44b37de

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 44b37de was 2ac218d, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

add commented out parse rules for new ftype syntax

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