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

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

Remove some of the warnings on the new clang

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