source: src/Parser/parser.yy@ 3f1059e

ADT ast-experimental pthread-emulation
Last change on this file since 3f1059e was dbedd71, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

update for-control with corrected @ usage for negative range

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