source: src/Parser/parser.yy@ 1bcbf02

ADT ast-experimental pthread-emulation
Last change on this file since 1bcbf02 was ed9a1ae, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Cfa now distinguishes between thread and _Thread_local.

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