source: src/Parser/parser.yy@ 16f9aca

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation pthread-emulation qualifiedEnum
Last change on this file since 16f9aca was 8a1d95af, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

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

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