source: src/Parser/parser.yy@ e01eb4a

ADT ast-experimental
Last change on this file since e01eb4a was 0bd46fd, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Fixed several warnings

  • Property mode set to 100644
File size: 161.0 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.
[0bd46fd]60#ifdef __clang__
[09f34a84]61#pragma GCC diagnostic ignored "-Wparentheses-equality"
[0bd46fd]62#endif
[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
[b0d9ff7]639 { $$ = new ExpressionNode( build_qualified_expr( $1, build_varref( $3 ) ) ); }
[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 '}'
[b0d9ff7]2540 { $$ = DeclarationNode::newEnum( nullptr, $4, true, false )->addQualifiers( $2 ); }
[f9c3100]2541 | ENUM attribute_list_opt identifier
[d0ffed1]2542 { typedefTable.makeTypedef( *$3 ); }
2543 '{' enumerator_list comma_opt '}'
[b0d9ff7]2544 { $$ = DeclarationNode::newEnum( $3, $6, true, false )->addQualifiers( $2 ); }
[f9c3100]2545 | ENUM attribute_list_opt typedef_name // unqualified type name
[407bde5]2546 '{' enumerator_list comma_opt '}'
[b0d9ff7]2547 { $$ = DeclarationNode::newEnum( $3->name, $5, true, false )->addQualifiers( $2 ); }
[f9c3100]2548 | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt '{' enumerator_list comma_opt '}'
[8bea701]2549 {
[ae2f2ae]2550 if ( $3->storageClasses.val != 0 || $3->type->qualifiers.val != 0 )
[a77713b]2551 { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
[f135b50]2552
[b0d9ff7]2553 $$ = DeclarationNode::newEnum( nullptr, $7, true, true, $3 )->addQualifiers( $5 );
2554 }
2555 | ENUM '(' ')' attribute_list_opt '{' enumerator_list comma_opt '}'
2556 {
2557 $$ = DeclarationNode::newEnum( nullptr, $6, true, true )->addQualifiers( $4 );
[8bea701]2558 }
[9e7236f4]2559 | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt
[8bea701]2560 {
[98337569]2561 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]2562 typedefTable.makeTypedef( *$6 );
2563 }
2564 '{' enumerator_list comma_opt '}'
2565 {
[b0d9ff7]2566 $$ = DeclarationNode::newEnum( $6, $10, true, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
[8bea701]2567 }
[b0d9ff7]2568 | ENUM '(' ')' attribute_list_opt identifier attribute_list_opt
2569 '{' enumerator_list comma_opt '}'
2570 {
2571 $$ = DeclarationNode::newEnum( $5, $8, true, true, nullptr )->addQualifiers( $4 )->addQualifiers( $6 );
[0bd46fd]2572 }
[f9c3100]2573 | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
[8bea701]2574 {
[b0d9ff7]2575 $$ = DeclarationNode::newEnum( $6->name, $9, true, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
2576 }
2577 | ENUM '(' ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
2578 {
2579 $$ = DeclarationNode::newEnum( $5->name, $8, true, true, nullptr )->addQualifiers( $4 )->addQualifiers( $6 );
[8bea701]2580 }
[d0ffed1]2581 | enum_type_nobody
2582 ;
2583
2584enum_type_nobody: // enum - {...}
[f9c3100]2585 ENUM attribute_list_opt identifier
[b0d9ff7]2586 { typedefTable.makeTypedef( *$3 ); $$ = DeclarationNode::newEnum( $3, 0, false, false )->addQualifiers( $2 ); }
[0bd46fd]2587 | ENUM attribute_list_opt type_name
[b0d9ff7]2588 { typedefTable.makeTypedef( *$3->type->symbolic.name ); $$ = DeclarationNode::newEnum( $3->type->symbolic.name, 0, false, false )->addQualifiers( $2 ); }
[4d51835]2589 ;
[51b73452]2590
2591enumerator_list:
[033ff37]2592 identifier_or_type_name enumerator_value_opt
[374cb117]2593 { $$ = DeclarationNode::newEnumValueGeneric( $1, $2 ); }
[f9c3100]2594 | INLINE type_name
[374cb117]2595 { $$ = DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ); }
[033ff37]2596 | enumerator_list ',' identifier_or_type_name enumerator_value_opt
[374cb117]2597 { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( $3, $4 ) ); }
[f9c3100]2598 | enumerator_list ',' INLINE type_name enumerator_value_opt
[374cb117]2599 { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ) ); }
[4d51835]2600 ;
[51b73452]2601
2602enumerator_value_opt:
[4d51835]2603 // empty
[58dd019]2604 { $$ = nullptr; }
[7991c7d]2605 | '=' constant_expression { $$ = new InitializerNode( $2 ); }
2606 | '=' '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $3, true ); }
2607 // | simple_assignment_operator initializer
2608 // { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
[4d51835]2609 ;
[51b73452]2610
[5a51798]2611cfa_parameter_ellipsis_list_opt: // CFA, abstract + real
[4d51835]2612 // empty
[2a8427c6]2613 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2614 | ELLIPSIS
[58dd019]2615 { $$ = nullptr; }
[2a8427c6]2616 | cfa_abstract_parameter_list
[c0aa336]2617 | cfa_parameter_list
[c0a33d2]2618 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
2619 { $$ = $1->appendList( $5 ); }
2620 | cfa_abstract_parameter_list pop ',' push ELLIPSIS
[4d51835]2621 { $$ = $1->addVarArgs(); }
[c0a33d2]2622 | cfa_parameter_list pop ',' push ELLIPSIS
[4d51835]2623 { $$ = $1->addVarArgs(); }
2624 ;
[b87a5ed]2625
[c0aa336]2626cfa_parameter_list: // CFA
2627 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
2628 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
2629 cfa_parameter_declaration
[c0a33d2]2630 | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2631 { $$ = $1->appendList( $5 ); }
2632 | cfa_parameter_list pop ',' push cfa_parameter_declaration
2633 { $$ = $1->appendList( $5 ); }
2634 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2635 { $$ = $1->appendList( $5 )->appendList( $9 ); }
[4d51835]2636 ;
[b87a5ed]2637
[c0aa336]2638cfa_abstract_parameter_list: // CFA, new & old style abstract
2639 cfa_abstract_parameter_declaration
[c0a33d2]2640 | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
2641 { $$ = $1->appendList( $5 ); }
[4d51835]2642 ;
[51b73452]2643
2644parameter_type_list_opt:
[4d51835]2645 // empty
[58dd019]2646 { $$ = nullptr; }
[2a8427c6]2647 | ELLIPSIS
2648 { $$ = nullptr; }
2649 | parameter_list
[4d51835]2650 | parameter_list pop ',' push ELLIPSIS
2651 { $$ = $1->addVarArgs(); }
2652 ;
[b87a5ed]2653
2654parameter_list: // abstract + real
[4d51835]2655 abstract_parameter_declaration
2656 | parameter_declaration
2657 | parameter_list pop ',' push abstract_parameter_declaration
2658 { $$ = $1->appendList( $5 ); }
2659 | parameter_list pop ',' push parameter_declaration
2660 { $$ = $1->appendList( $5 ); }
2661 ;
[51b73452]2662
[de62360d]2663// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
[2871210]2664// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
[51b73452]2665
[c0aa336]2666cfa_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]2667 parameter_declaration
[5a51798]2668 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initializer_opt
[4d51835]2669 { $$ = $1->addName( $2 ); }
[5a51798]2670 | cfa_abstract_tuple identifier_or_type_name default_initializer_opt
[c0aa336]2671 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
[4d51835]2672 { $$ = $1->addName( $2 ); }
[5a51798]2673 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initializer_opt
[4d51835]2674 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
[c0aa336]2675 | cfa_function_specifier
[4d51835]2676 ;
[b87a5ed]2677
[c0aa336]2678cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]2679 abstract_parameter_declaration
[c0aa336]2680 | cfa_identifier_parameter_declarator_no_tuple
2681 | cfa_abstract_tuple
2682 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2683 | type_qualifier_list cfa_abstract_tuple
[4d51835]2684 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]2685 | cfa_abstract_function
[4d51835]2686 ;
[51b73452]2687
2688parameter_declaration:
[d0ffed1]2689 // No SUE declaration in parameter list.
[5a51798]2690 declaration_specifier_nobody identifier_parameter_declarator default_initializer_opt
[7fdb94e1]2691 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[5a51798]2692 | declaration_specifier_nobody type_parameter_redeclarator default_initializer_opt
[7fdb94e1]2693 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[4d51835]2694 ;
[51b73452]2695
2696abstract_parameter_declaration:
[5a51798]2697 declaration_specifier_nobody default_initializer_opt
[e7cc8cb]2698 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
[5a51798]2699 | declaration_specifier_nobody abstract_parameter_declarator default_initializer_opt
[e7cc8cb]2700 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[4d51835]2701 ;
[51b73452]2702
[c11e31c]2703// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
[de62360d]2704// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2705// identifiers. The ANSI-style parameter-list can redefine a typedef name.
[51b73452]2706
[b87a5ed]2707identifier_list: // K&R-style parameter list => no types
[033ff37]2708 identifier
[4d51835]2709 { $$ = DeclarationNode::newName( $1 ); }
[033ff37]2710 | identifier_list ',' identifier
[4d51835]2711 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2712 ;
[51b73452]2713
[2871210]2714identifier_or_type_name:
[4d51835]2715 identifier
2716 | TYPEDEFname
2717 | TYPEGENname
2718 ;
[51b73452]2719
[84d58c5]2720type_no_function: // sizeof, alignof, cast (constructor)
[c0aa336]2721 cfa_abstract_declarator_tuple // CFA
[4d51835]2722 | type_specifier
[c0aa336]2723 | type_specifier abstract_declarator
[4d51835]2724 { $$ = $2->addType( $1 ); }
2725 ;
[b87a5ed]2726
[84d58c5]2727type: // typeof, assertion
2728 type_no_function
[c0aa336]2729 | cfa_abstract_function // CFA
[4d51835]2730 ;
[51b73452]2731
2732initializer_opt:
[4d51835]2733 // empty
[58dd019]2734 { $$ = nullptr; }
[f9c3100]2735 | simple_assignment_operator initializer { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2736 | '=' VOID { $$ = new InitializerNode( true ); }
[63b3279e]2737 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
[4d51835]2738 ;
[51b73452]2739
2740initializer:
[de62360d]2741 assignment_expression { $$ = new InitializerNode( $1 ); }
[7fdb94e1]2742 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
[4d51835]2743 ;
[51b73452]2744
[7fdb94e1]2745initializer_list_opt:
[097e2b0]2746 // empty
[58dd019]2747 { $$ = nullptr; }
[097e2b0]2748 | initializer
[4d51835]2749 | designation initializer { $$ = $2->set_designators( $1 ); }
[7fdb94e1]2750 | initializer_list_opt ',' initializer { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
[63b3279e]2751 | initializer_list_opt ',' designation initializer { $$ = (InitializerNode *)($1->set_last( $4->set_designators( $3 ) )); }
[4d51835]2752 ;
[b87a5ed]2753
[de62360d]2754// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2755// '=' to separator the designator from the initializer value, as in:
[c11e31c]2756//
[b87a5ed]2757// int x[10] = { [1] = 3 };
[c11e31c]2758//
[de62360d]2759// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
2760// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2761// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2762// shift/reduce conflicts
[51b73452]2763
2764designation:
[4d51835]2765 designator_list ':' // C99, CFA uses ":" instead of "="
[e16eb460]2766 | identifier_at ':' // GCC, field name
[d1625f8]2767 { $$ = new ExpressionNode( build_varref( $1 ) ); }
[4d51835]2768 ;
[51b73452]2769
[b87a5ed]2770designator_list: // C99
[4d51835]2771 designator
[2871210]2772 | designator_list designator
[4a063df]2773 { $$ = (ExpressionNode *)($1->set_last( $2 )); }
[d1625f8]2774 //| designator_list designator { $$ = new ExpressionNode( $1, $2 ); }
[4d51835]2775 ;
[51b73452]2776
2777designator:
[e16eb460]2778 '.' identifier_at // C99, field name
[d1625f8]2779 { $$ = new ExpressionNode( build_varref( $2 ) ); }
[c0a33d2]2780 | '[' push assignment_expression pop ']' // C99, single array element
[de62360d]2781 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
[d1625f8]2782 { $$ = $3; }
[c0a33d2]2783 | '[' push subrange pop ']' // CFA, multiple array elements
2784 { $$ = $3; }
2785 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
[408ab79]2786 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $3 ), maybeMoveBuild<Expression>( $5 ) ) ); }
[679e644]2787 | '.' '[' push field_name_list pop ']' // CFA, tuple field selector
[c0a33d2]2788 { $$ = $4; }
[4d51835]2789 ;
[51b73452]2790
[de62360d]2791// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2792// rather than an object-oriented type system. This required four groups of extensions:
[c11e31c]2793//
2794// Overloading: function, data, and operator identifiers may be overloaded.
2795//
[3ca7ef3]2796// Type declarations: "otype" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
[de62360d]2797// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2798// definitions of new types. Type declarations with storage class "extern" provide opaque types.
[c11e31c]2799//
[de62360d]2800// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2801// site. A polymorphic function is not a template; it is a function, with an address and a type.
[c11e31c]2802//
2803// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
[de62360d]2804// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2805// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
2806// types provide the operations declared by a specification. Assertions are normally used to declare requirements
2807// on type arguments of polymorphic functions.
[c11e31c]2808
[b87a5ed]2809type_parameter_list: // CFA
[67cf18c]2810 type_parameter
2811 | type_parameter_list ',' type_parameter
[4d51835]2812 { $$ = $1->appendList( $3 ); }
2813 ;
[b87a5ed]2814
[84d58c5]2815type_initializer_opt: // CFA
2816 // empty
2817 { $$ = nullptr; }
2818 | '=' type
2819 { $$ = $2; }
2820 ;
2821
[b87a5ed]2822type_parameter: // CFA
[033ff37]2823 type_class identifier_or_type_name
[408ab79]2824 {
2825 typedefTable.addToScope( *$2, TYPEDEFname, "9" );
[ec3f9c8]2826 if ( $1 == TypeDecl::Otype ) { SemanticError( yylloc, "otype keyword is deprecated, use T " ); }
2827 if ( $1 == TypeDecl::Dtype ) { SemanticError( yylloc, "dtype keyword is deprecated, use T &" ); }
2828 if ( $1 == TypeDecl::Ttype ) { SemanticError( yylloc, "ttype keyword is deprecated, use T ..." ); }
[fd54fef]2829 }
[5a51798]2830 type_initializer_opt assertion_list_opt
[67cf18c]2831 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
[5a51798]2832 | identifier_or_type_name new_type_class
2833 { typedefTable.addToScope( *$1, TYPEDEFname, "9" ); }
2834 type_initializer_opt assertion_list_opt
2835 { $$ = DeclarationNode::newTypeParam( $2, $1 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2836 | '[' identifier_or_type_name ']'
[b66d14a]2837 {
[6e50a6b]2838 typedefTable.addToScope( *$2, TYPEDIMname, "9" );
2839 $$ = DeclarationNode::newTypeParam( TypeDecl::Dimension, $2 );
[b66d14a]2840 }
[5a51798]2841 // | type_specifier identifier_parameter_declarator
[9997fee]2842 | assertion_list
[07de76b]2843 { $$ = DeclarationNode::newTypeParam( TypeDecl::Dtype, new string( DeclarationNode::anonymous.newName() ) )->addAssertions( $1 ); }
[4d51835]2844 ;
[b87a5ed]2845
[5a51798]2846new_type_class: // CFA
2847 // empty
2848 { $$ = TypeDecl::Otype; }
2849 | '&'
2850 { $$ = TypeDecl::Dtype; }
2851 | '*'
[b66d14a]2852 { $$ = TypeDecl::DStype; } // dtype + sized
[2ac218d]2853 // | '(' '*' ')'
2854 // { $$ = TypeDecl::Ftype; }
[5a51798]2855 | ELLIPSIS
2856 { $$ = TypeDecl::Ttype; }
2857 ;
2858
[b87a5ed]2859type_class: // CFA
[4040425]2860 OTYPE
[07de76b]2861 { $$ = TypeDecl::Otype; }
[4d51835]2862 | DTYPE
[07de76b]2863 { $$ = TypeDecl::Dtype; }
[8f60f0b]2864 | FTYPE
[07de76b]2865 { $$ = TypeDecl::Ftype; }
[8f60f0b]2866 | TTYPE
[07de76b]2867 { $$ = TypeDecl::Ttype; }
[4d51835]2868 ;
[b87a5ed]2869
2870assertion_list_opt: // CFA
[4d51835]2871 // empty
[58dd019]2872 { $$ = nullptr; }
[9997fee]2873 | assertion_list
2874 ;
2875
2876assertion_list: // CFA
2877 assertion
2878 | assertion_list assertion
[3ca7ef3]2879 { $$ = $1->appendList( $2 ); }
[4d51835]2880 ;
[b87a5ed]2881
2882assertion: // CFA
[033ff37]2883 '|' identifier_or_type_name '(' type_list ')'
[7fdb94e1]2884 { $$ = DeclarationNode::newTraitUse( $2, $4 ); }
[13e8427]2885 | '|' '{' push trait_declaration_list pop '}'
[4d51835]2886 { $$ = $4; }
[35718a9]2887 // | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list pop '}' '(' type_list ')'
2888 // { SemanticError( yylloc, "Generic data-type assertion is currently unimplemented." ); $$ = nullptr; }
[4d51835]2889 ;
[b87a5ed]2890
[84d58c5]2891type_list: // CFA
2892 type
[513e165]2893 { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
[4d51835]2894 | assignment_expression
[84d58c5]2895 | type_list ',' type
[4a063df]2896 { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) )); }
[84d58c5]2897 | type_list ',' assignment_expression
[6e50a6b]2898 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]2899 ;
[b87a5ed]2900
2901type_declaring_list: // CFA
[4040425]2902 OTYPE type_declarator
[4d51835]2903 { $$ = $2; }
[4040425]2904 | storage_class_list OTYPE type_declarator
[4d51835]2905 { $$ = $3->addQualifiers( $1 ); }
2906 | type_declaring_list ',' type_declarator
[a7c90d4]2907 { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
[4d51835]2908 ;
[b87a5ed]2909
2910type_declarator: // CFA
[4d51835]2911 type_declarator_name assertion_list_opt
2912 { $$ = $1->addAssertions( $2 ); }
[84d58c5]2913 | type_declarator_name assertion_list_opt '=' type
[4d51835]2914 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2915 ;
[b87a5ed]2916
2917type_declarator_name: // CFA
[033ff37]2918 identifier_or_type_name
[4d51835]2919 {
[ecae5860]2920 typedefTable.addToEnclosingScope( *$1, TYPEDEFname, "10" );
[4d51835]2921 $$ = DeclarationNode::newTypeDecl( $1, 0 );
2922 }
[033ff37]2923 | identifier_or_type_name '(' type_parameter_list ')'
[4d51835]2924 {
[ecae5860]2925 typedefTable.addToEnclosingScope( *$1, TYPEGENname, "11" );
[35718a9]2926 $$ = DeclarationNode::newTypeDecl( $1, $3 );
[4d51835]2927 }
2928 ;
[b87a5ed]2929
[4040425]2930trait_specifier: // CFA
[033ff37]2931 TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' '}'
[35718a9]2932 { $$ = DeclarationNode::newTrait( $2, $4, 0 ); }
[033ff37]2933 | TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' push trait_declaration_list pop '}'
[35718a9]2934 { $$ = DeclarationNode::newTrait( $2, $4, $8 ); }
[4d51835]2935 ;
[b87a5ed]2936
[84d58c5]2937trait_declaration_list: // CFA
[4040425]2938 trait_declaration
[13e8427]2939 | trait_declaration_list pop push trait_declaration
2940 { $$ = $1->appendList( $4 ); }
[4d51835]2941 ;
[b87a5ed]2942
[84d58c5]2943trait_declaration: // CFA
[13e8427]2944 cfa_trait_declaring_list ';'
2945 | trait_declaring_list ';'
[4d51835]2946 ;
[b87a5ed]2947
[c0aa336]2948cfa_trait_declaring_list: // CFA
2949 cfa_variable_specifier
2950 | cfa_function_specifier
2951 | cfa_trait_declaring_list pop ',' push identifier_or_type_name
[7fdb94e1]2952 { $$ = $1->appendList( $1->cloneType( $5 ) ); }
[4d51835]2953 ;
[b87a5ed]2954
[4040425]2955trait_declaring_list: // CFA
[4d51835]2956 type_specifier declarator
[7fdb94e1]2957 { $$ = $2->addType( $1 ); }
[4040425]2958 | trait_declaring_list pop ',' push declarator
[7fdb94e1]2959 { $$ = $1->appendList( $1->cloneBaseType( $5 ) ); }
[4d51835]2960 ;
[51b73452]2961
[c11e31c]2962//***************************** EXTERNAL DEFINITIONS *****************************
[51b73452]2963
2964translation_unit:
[3d56d15b]2965 // empty, input file
[4d51835]2966 | external_definition_list
[a7741435]2967 { parseTree = parseTree ? parseTree->appendList( $1 ) : $1; }
[4d51835]2968 ;
[51b73452]2969
2970external_definition_list:
[35718a9]2971 push external_definition pop
2972 { $$ = $2; }
[fc20514]2973 | external_definition_list push external_definition pop
2974 { $$ = $1 ? $1->appendList( $3 ) : $3; }
[4d51835]2975 ;
[51b73452]2976
2977external_definition_list_opt:
[4d51835]2978 // empty
[58dd019]2979 { $$ = nullptr; }
[3d56d15b]2980 | external_definition_list
2981 ;
2982
2983up:
[fc20514]2984 { typedefTable.up( forall ); forall = false; }
[3d56d15b]2985 ;
2986
2987down:
2988 { typedefTable.down(); }
[4d51835]2989 ;
[51b73452]2990
2991external_definition:
[2d019af]2992 DIRECTIVE
2993 { $$ = DeclarationNode::newDirectiveStmt( new StatementNode( build_directive( $1 ) ) ); }
2994 | declaration
[4d51835]2995 | external_function_definition
[ecae5860]2996 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
2997 {
2998 distExt( $2 ); // mark all fields in list
2999 $$ = $2;
3000 }
[e994912]3001 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
[5e25953]3002 { $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( false, $3, 0 ) ) ); }
[aac37fa]3003 | EXTERN STRINGliteral
3004 {
3005 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
3006 linkage = LinkageSpec::update( yylloc, linkage, $2 );
3007 }
[ae2f2ae]3008 up external_definition down
[aac37fa]3009 {
3010 linkage = linkageStack.top();
3011 linkageStack.pop();
3012 $$ = $5;
3013 }
[c0aa336]3014 | EXTERN STRINGliteral // C++-style linkage specifier
[4d51835]3015 {
[3b8e52c]3016 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
[d912bed]3017 linkage = LinkageSpec::update( yylloc, linkage, $2 );
[4d51835]3018 }
[3d56d15b]3019 '{' up external_definition_list_opt down '}'
[4d51835]3020 {
3021 linkage = linkageStack.top();
3022 linkageStack.pop();
[3d56d15b]3023 $$ = $6;
[8e9cbb2]3024 }
[9997fee]3025 | type_qualifier_list
3026 {
[3d56d15b]3027 if ( $1->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
[fc20514]3028 if ( $1->type->forall ) forall = true; // remember generic type
[3d56d15b]3029 }
3030 '{' up external_definition_list_opt down '}' // CFA, namespace
3031 {
[4c3ee8d]3032 distQual( $5, $1 );
[fc20514]3033 forall = false;
[3d56d15b]3034 $$ = $5;
[9997fee]3035 }
3036 | declaration_qualifier_list
3037 {
[4c3ee8d]3038 if ( $1->type && $1->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
[fc20514]3039 if ( $1->type && $1->type->forall ) forall = true; // remember generic type
[3d56d15b]3040 }
3041 '{' up external_definition_list_opt down '}' // CFA, namespace
3042 {
[4c3ee8d]3043 distQual( $5, $1 );
[fc20514]3044 forall = false;
[3d56d15b]3045 $$ = $5;
[9997fee]3046 }
3047 | declaration_qualifier_list type_qualifier_list
3048 {
[3d56d15b]3049 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]3050 if ( ($1->type && $1->type->forall) || $2->type->forall ) forall = true; // remember generic type
[9997fee]3051 }
[3d56d15b]3052 '{' up external_definition_list_opt down '}' // CFA, namespace
[9997fee]3053 {
[284da8c]3054 distQual( $6, $1->addQualifiers( $2 ) );
[fc20514]3055 forall = false;
[3d56d15b]3056 $$ = $6;
[9997fee]3057 }
[4d51835]3058 ;
3059
3060external_function_definition:
3061 function_definition
[de62360d]3062 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
[c6b1105]3063 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
3064 // Parsing is possible because function_definition does not appear in the context of an expression (nested
3065 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
3066 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
[4d51835]3067 | function_declarator compound_statement
[c0a33d2]3068 { $$ = $1->addFunctionBody( $2 ); }
[35718a9]3069 | KR_function_declarator KR_parameter_list_opt compound_statement
[c0a33d2]3070 { $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 ); }
[4d51835]3071 ;
[51b73452]3072
[8b47e50]3073with_clause_opt:
3074 // empty
[9997fee]3075 { $$ = nullptr; forall = false; }
[d8454b9]3076 | WITH '(' tuple_expression_list ')' attribute_list_opt
3077 {
3078 $$ = $3; forall = false;
3079 if ( $5 ) {
3080 SemanticError( yylloc, "Attributes cannot be associated with function body. Move attribute(s) before \"with\" clause." );
3081 $$ = nullptr;
3082 } // if
3083 }
[8b47e50]3084 ;
3085
[51b73452]3086function_definition:
[8b47e50]3087 cfa_function_declaration with_clause_opt compound_statement // CFA
[4d51835]3088 {
[481115f]3089 // Add the function body to the last identifier in the function definition list, i.e., foo3:
3090 // [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
[5fcba14]3091 $1->get_last()->addFunctionBody( $3, $2 );
[481115f]3092 $$ = $1;
[4d51835]3093 }
[8b47e50]3094 | declaration_specifier function_declarator with_clause_opt compound_statement
[4d51835]3095 {
[c38ae92]3096 rebindForall( $1, $2 );
[7fdb94e1]3097 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
3098 }
3099 | declaration_specifier variable_type_redeclarator with_clause_opt compound_statement
3100 {
3101 rebindForall( $1, $2 );
[5fcba14]3102 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
[4d51835]3103 }
[a16a7ec]3104 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]3105 | type_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]3106 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
[a16a7ec]3107 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]3108 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]3109 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
[a16a7ec]3110 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]3111 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]3112 { $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 ); }
[4d51835]3113
3114 // Old-style K&R function definition, OBSOLESCENT (see 4)
[35718a9]3115 | declaration_specifier KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[4d51835]3116 {
[c38ae92]3117 rebindForall( $1, $2 );
[5fcba14]3118 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
[4d51835]3119 }
[a16a7ec]3120 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]3121 | type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]3122 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
[a16a7ec]3123 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]3124 | declaration_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]3125 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
[a16a7ec]3126 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]3127 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]3128 { $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 ); }
[4d51835]3129 ;
[51b73452]3130
3131declarator:
[4d51835]3132 variable_declarator
[c6b1105]3133 | variable_type_redeclarator
[4d51835]3134 | function_declarator
3135 ;
[51b73452]3136
3137subrange:
[4d51835]3138 constant_expression '~' constant_expression // CFA, integer subrange
[408ab79]3139 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
[4d51835]3140 ;
[b87a5ed]3141
3142asm_name_opt: // GCC
[4d51835]3143 // empty
[58dd019]3144 { $$ = nullptr; }
3145 | ASM '(' string_literal ')' attribute_list_opt
[c0aa336]3146 {
3147 DeclarationNode * name = new DeclarationNode();
3148 name->asmName = $3;
3149 $$ = name->addQualifiers( $5 );
3150 }
[4d51835]3151 ;
[b87a5ed]3152
3153attribute_list_opt: // GCC
[4d51835]3154 // empty
[58dd019]3155 { $$ = nullptr; }
[4d51835]3156 | attribute_list
3157 ;
[b87a5ed]3158
3159attribute_list: // GCC
[4d51835]3160 attribute
3161 | attribute_list attribute
[1db21619]3162 { $$ = $2->addQualifiers( $1 ); }
[4d51835]3163 ;
[b87a5ed]3164
3165attribute: // GCC
[44a81853]3166 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
3167 { $$ = $4; }
[4d51835]3168 ;
[b87a5ed]3169
[44a81853]3170attribute_name_list: // GCC
3171 attribute_name
3172 | attribute_name_list ',' attribute_name
[c0aa336]3173 { $$ = $3->addQualifiers( $1 ); }
[4d51835]3174 ;
[b87a5ed]3175
[44a81853]3176attribute_name: // GCC
[4d51835]3177 // empty
[44a81853]3178 { $$ = nullptr; }
3179 | attr_name
3180 { $$ = DeclarationNode::newAttribute( $1 ); }
[cbbd8fd7]3181 | attr_name '(' argument_expression_list_opt ')'
[44a81853]3182 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
[4d51835]3183 ;
[b87a5ed]3184
[44a81853]3185attr_name: // GCC
3186 IDENTIFIER
[5b2edbc]3187 | quasi_keyword
[44a81853]3188 | TYPEDEFname
3189 | TYPEGENname
[114014c]3190 | FALLTHROUGH
3191 { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
[44a81853]3192 | CONST
[9ff56e7]3193 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
[4d51835]3194 ;
[51b73452]3195
[c11e31c]3196// ============================================================================
[de62360d]3197// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
3198// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
3199// in:
[c11e31c]3200//
[b87a5ed]3201// int (*f())[10] { ... };
3202// ... (*f())[3] += 1; // definition mimics usage
[c11e31c]3203//
[de62360d]3204// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
3205// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
[c11e31c]3206// ============================================================================
3207
3208// ----------------------------------------------------------------------------
[de62360d]3209// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
3210// to define a variable or function prototype, e.g.:
[c11e31c]3211//
[b87a5ed]3212// valid declaration invalid definition
3213// ----------------- ------------------
[4d51835]3214// int f; int f {}
3215// int *f; int *f {}
[b87a5ed]3216// int f[10]; int f[10] {}
[4d51835]3217// int (*f)(int); int (*f)(int) {}
[c11e31c]3218//
[de62360d]3219// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
3220// variable_declarator and function_declarator.
[c11e31c]3221// ----------------------------------------------------------------------------
3222
[de62360d]3223// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
3224// declaring an array of functions versus a pointer to an array of functions.
[51b73452]3225
[5e25953]3226paren_identifier:
[e16eb460]3227 identifier_at
[5e25953]3228 { $$ = DeclarationNode::newName( $1 ); }
3229 | '(' paren_identifier ')' // redundant parenthesis
3230 { $$ = $2; }
3231 ;
3232
[51b73452]3233variable_declarator:
[4d51835]3234 paren_identifier attribute_list_opt
[1db21619]3235 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3236 | variable_ptr
3237 | variable_array attribute_list_opt
[1db21619]3238 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3239 | variable_function attribute_list_opt
[1db21619]3240 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3241 ;
[51b73452]3242
3243variable_ptr:
[dd51906]3244 ptrref_operator variable_declarator
[ce8c12f]3245 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3246 | ptrref_operator type_qualifier_list variable_declarator
[ce8c12f]3247 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3248 | '(' variable_ptr ')' attribute_list_opt // redundant parenthesis
3249 { $$ = $2->addQualifiers( $4 ); }
3250 | '(' attribute_list variable_ptr ')' attribute_list_opt // redundant parenthesis
3251 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
[4d51835]3252 ;
[51b73452]3253
3254variable_array:
[4d51835]3255 paren_identifier array_dimension
3256 { $$ = $1->addArray( $2 ); }
3257 | '(' variable_ptr ')' array_dimension
3258 { $$ = $2->addArray( $4 ); }
[5e25953]3259 | '(' attribute_list variable_ptr ')' array_dimension
3260 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3261 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
[4d51835]3262 { $$ = $2->addArray( $4 ); }
[5e25953]3263 | '(' attribute_list variable_array ')' multi_array_dimension // redundant parenthesis
3264 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[4d51835]3265 | '(' variable_array ')' // redundant parenthesis
3266 { $$ = $2; }
[5e25953]3267 | '(' attribute_list variable_array ')' // redundant parenthesis
3268 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3269 ;
[51b73452]3270
3271variable_function:
[4d51835]3272 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3273 { $$ = $2->addParamList( $6 ); }
[5e25953]3274 | '(' attribute_list variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3275 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
[4d51835]3276 | '(' variable_function ')' // redundant parenthesis
3277 { $$ = $2; }
[5e25953]3278 | '(' attribute_list variable_function ')' // redundant parenthesis
3279 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3280 ;
[51b73452]3281
[c6b1105]3282// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
3283// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
3284// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
[51b73452]3285
3286function_declarator:
[4d51835]3287 function_no_ptr attribute_list_opt
[1db21619]3288 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3289 | function_ptr
3290 | function_array attribute_list_opt
[1db21619]3291 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3292 ;
[51b73452]3293
3294function_no_ptr:
[4d51835]3295 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3296 { $$ = $1->addParamList( $4 ); }
3297 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
3298 { $$ = $2->addParamList( $6 ); }
[5e25953]3299 | '(' attribute_list function_ptr ')' '(' push parameter_type_list_opt pop ')'
3300 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
[4d51835]3301 | '(' function_no_ptr ')' // redundant parenthesis
3302 { $$ = $2; }
[5e25953]3303 | '(' attribute_list function_no_ptr ')' // redundant parenthesis
3304 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3305 ;
[51b73452]3306
3307function_ptr:
[dd51906]3308 ptrref_operator function_declarator
[ce8c12f]3309 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3310 | ptrref_operator type_qualifier_list function_declarator
[ce8c12f]3311 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3312 | '(' function_ptr ')' attribute_list_opt
3313 { $$ = $2->addQualifiers( $4 ); }
3314 | '(' attribute_list function_ptr ')' attribute_list_opt
3315 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
[4d51835]3316 ;
[51b73452]3317
3318function_array:
[4d51835]3319 '(' function_ptr ')' array_dimension
3320 { $$ = $2->addArray( $4 ); }
[5e25953]3321 | '(' attribute_list function_ptr ')' array_dimension
3322 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[4d51835]3323 | '(' function_array ')' multi_array_dimension // redundant parenthesis
3324 { $$ = $2->addArray( $4 ); }
[5e25953]3325 | '(' attribute_list function_array ')' multi_array_dimension // redundant parenthesis
3326 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[4d51835]3327 | '(' function_array ')' // redundant parenthesis
3328 { $$ = $2; }
[5e25953]3329 | '(' attribute_list function_array ')' // redundant parenthesis
3330 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3331 ;
[51b73452]3332
[c0aa336]3333// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
3334//
3335// f( a, b, c ) int a, *b, c[]; {}
3336//
3337// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
3338// returning arrays and functions versus pointers to arrays and functions.
[51b73452]3339
[c0aa336]3340KR_function_declarator:
3341 KR_function_no_ptr
3342 | KR_function_ptr
3343 | KR_function_array
[4d51835]3344 ;
[51b73452]3345
[c0aa336]3346KR_function_no_ptr:
[4d51835]3347 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
3348 { $$ = $1->addIdList( $3 ); }
[c0a33d2]3349 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
3350 { $$ = $2->addParamList( $6 ); }
[5e25953]3351 | '(' attribute_list KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
3352 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
[c0aa336]3353 | '(' KR_function_no_ptr ')' // redundant parenthesis
[4d51835]3354 { $$ = $2; }
[5e25953]3355 | '(' attribute_list KR_function_no_ptr ')' // redundant parenthesis
3356 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3357 ;
[51b73452]3358
[c0aa336]3359KR_function_ptr:
3360 ptrref_operator KR_function_declarator
[ce8c12f]3361 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3362 | ptrref_operator type_qualifier_list KR_function_declarator
[ce8c12f]3363 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]3364 | '(' KR_function_ptr ')'
[4d51835]3365 { $$ = $2; }
[5e25953]3366 | '(' attribute_list KR_function_ptr ')'
3367 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3368 ;
[51b73452]3369
[c0aa336]3370KR_function_array:
3371 '(' KR_function_ptr ')' array_dimension
[4d51835]3372 { $$ = $2->addArray( $4 ); }
[5e25953]3373 | '(' attribute_list KR_function_ptr ')' array_dimension
3374 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[c0aa336]3375 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
[4d51835]3376 { $$ = $2->addArray( $4 ); }
[5e25953]3377 | '(' attribute_list KR_function_array ')' multi_array_dimension // redundant parenthesis
3378 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[c0aa336]3379 | '(' KR_function_array ')' // redundant parenthesis
[4d51835]3380 { $$ = $2; }
[5e25953]3381 | '(' attribute_list KR_function_array ')' // redundant parenthesis
3382 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3383 ;
[51b73452]3384
[2871210]3385// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
[c11e31c]3386//
[b87a5ed]3387// typedef int foo;
3388// {
3389// int foo; // redefine typedef name in new scope
3390// }
[c11e31c]3391//
[de62360d]3392// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3393// and functions versus pointers to arrays and functions.
[51b73452]3394
[2871210]3395paren_type:
[f9c3100]3396 typedef_name
[c4f68dc]3397 {
[f9c3100]3398 // hide type name in enclosing scope by variable name
3399 typedefTable.addToEnclosingScope( *$1->name, IDENTIFIER, "ID" );
[c4f68dc]3400 }
[2871210]3401 | '(' paren_type ')'
[4d51835]3402 { $$ = $2; }
3403 ;
[51b73452]3404
[5e25953]3405variable_type_redeclarator:
3406 paren_type attribute_list_opt
3407 { $$ = $1->addQualifiers( $2 ); }
3408 | type_ptr
3409 | type_array attribute_list_opt
3410 { $$ = $1->addQualifiers( $2 ); }
3411 | type_function attribute_list_opt
3412 { $$ = $1->addQualifiers( $2 ); }
3413 ;
3414
[2871210]3415type_ptr:
[c6b1105]3416 ptrref_operator variable_type_redeclarator
[ce8c12f]3417 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c6b1105]3418 | ptrref_operator type_qualifier_list variable_type_redeclarator
[ce8c12f]3419 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3420 | '(' type_ptr ')' attribute_list_opt // redundant parenthesis
3421 { $$ = $2->addQualifiers( $4 ); }
3422 | '(' attribute_list type_ptr ')' attribute_list_opt // redundant parenthesis
3423 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
[4d51835]3424 ;
[51b73452]3425
[2871210]3426type_array:
3427 paren_type array_dimension
[4d51835]3428 { $$ = $1->addArray( $2 ); }
[2871210]3429 | '(' type_ptr ')' array_dimension
[4d51835]3430 { $$ = $2->addArray( $4 ); }
[5e25953]3431 | '(' attribute_list type_ptr ')' array_dimension
3432 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[2871210]3433 | '(' type_array ')' multi_array_dimension // redundant parenthesis
[4d51835]3434 { $$ = $2->addArray( $4 ); }
[5e25953]3435 | '(' attribute_list type_array ')' multi_array_dimension // redundant parenthesis
3436 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[2871210]3437 | '(' type_array ')' // redundant parenthesis
[4d51835]3438 { $$ = $2; }
[5e25953]3439 | '(' attribute_list type_array ')' // redundant parenthesis
3440 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3441 ;
[51b73452]3442
[2871210]3443type_function:
3444 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]3445 { $$ = $1->addParamList( $4 ); }
[2871210]3446 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]3447 { $$ = $2->addParamList( $6 ); }
[5e25953]3448 | '(' attribute_list type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3449 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
[2871210]3450 | '(' type_function ')' // redundant parenthesis
[4d51835]3451 { $$ = $2; }
[5e25953]3452 | '(' attribute_list type_function ')' // redundant parenthesis
3453 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3454 ;
[51b73452]3455
[c0aa336]3456// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
3457// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
3458// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
3459// pointers to arrays and functions.
[51b73452]3460
3461identifier_parameter_declarator:
[4d51835]3462 paren_identifier attribute_list_opt
[1db21619]3463 { $$ = $1->addQualifiers( $2 ); }
[b6b3c42]3464 | '&' MUTEX paren_identifier attribute_list_opt
3465 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
[4d51835]3466 | identifier_parameter_ptr
3467 | identifier_parameter_array attribute_list_opt
[1db21619]3468 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3469 | identifier_parameter_function attribute_list_opt
[1db21619]3470 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3471 ;
[51b73452]3472
3473identifier_parameter_ptr:
[dd51906]3474 ptrref_operator identifier_parameter_declarator
[ce8c12f]3475 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3476 | ptrref_operator type_qualifier_list identifier_parameter_declarator
[ce8c12f]3477 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3478 | '(' identifier_parameter_ptr ')' attribute_list_opt // redundant parenthesis
[c0aa336]3479 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3480 ;
[51b73452]3481
3482identifier_parameter_array:
[4d51835]3483 paren_identifier array_parameter_dimension
3484 { $$ = $1->addArray( $2 ); }
3485 | '(' identifier_parameter_ptr ')' array_dimension
3486 { $$ = $2->addArray( $4 ); }
3487 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
3488 { $$ = $2->addArray( $4 ); }
3489 | '(' identifier_parameter_array ')' // redundant parenthesis
3490 { $$ = $2; }
3491 ;
[51b73452]3492
3493identifier_parameter_function:
[c0a33d2]3494 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3495 { $$ = $1->addParamList( $4 ); }
3496 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3497 { $$ = $2->addParamList( $6 ); }
[4d51835]3498 | '(' identifier_parameter_function ')' // redundant parenthesis
3499 { $$ = $2; }
3500 ;
[b87a5ed]3501
[de62360d]3502// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
3503// e.g.:
[c11e31c]3504//
[b87a5ed]3505// typedef int foo;
[114014c]3506// forall( otype T ) struct foo;
[b87a5ed]3507// int f( int foo ); // redefine typedef name in new scope
[c11e31c]3508//
[c0aa336]3509// and allows the C99 array options, which can only appear in a parameter list.
[51b73452]3510
[2871210]3511type_parameter_redeclarator:
[f9c3100]3512 typedef_name attribute_list_opt
[1db21619]3513 { $$ = $1->addQualifiers( $2 ); }
[f9c3100]3514 | '&' MUTEX typedef_name attribute_list_opt
[b6b3c42]3515 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
[2871210]3516 | type_parameter_ptr
3517 | type_parameter_array attribute_list_opt
[1db21619]3518 { $$ = $1->addQualifiers( $2 ); }
[2871210]3519 | type_parameter_function attribute_list_opt
[1db21619]3520 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3521 ;
[51b73452]3522
[f9c3100]3523typedef_name:
[4d51835]3524 TYPEDEFname
[7fdb94e1]3525 { $$ = DeclarationNode::newName( $1 ); }
[2871210]3526 | TYPEGENname
[7fdb94e1]3527 { $$ = DeclarationNode::newName( $1 ); }
[4d51835]3528 ;
[51b73452]3529
[2871210]3530type_parameter_ptr:
[dd51906]3531 ptrref_operator type_parameter_redeclarator
[ce8c12f]3532 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3533 | ptrref_operator type_qualifier_list type_parameter_redeclarator
[ce8c12f]3534 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3535 | '(' type_parameter_ptr ')' attribute_list_opt // redundant parenthesis
[c0aa336]3536 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3537 ;
[51b73452]3538
[2871210]3539type_parameter_array:
[f9c3100]3540 typedef_name array_parameter_dimension
[4d51835]3541 { $$ = $1->addArray( $2 ); }
[2871210]3542 | '(' type_parameter_ptr ')' array_parameter_dimension
[4d51835]3543 { $$ = $2->addArray( $4 ); }
3544 ;
[51b73452]3545
[2871210]3546type_parameter_function:
[f9c3100]3547 typedef_name '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[c0a33d2]3548 { $$ = $1->addParamList( $4 ); }
3549 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3550 { $$ = $2->addParamList( $6 ); }
[4d51835]3551 ;
[b87a5ed]3552
[de62360d]3553// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
3554// which the type applies, e.g.:
[c11e31c]3555//
[b87a5ed]3556// sizeof( int );
[c0aa336]3557// sizeof( int * );
[b87a5ed]3558// sizeof( int [10] );
[c0aa336]3559// sizeof( int (*)() );
3560// sizeof( int () );
[c11e31c]3561//
[de62360d]3562// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3563// and functions versus pointers to arrays and functions.
[51b73452]3564
3565abstract_declarator:
[4d51835]3566 abstract_ptr
3567 | abstract_array attribute_list_opt
[1db21619]3568 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3569 | abstract_function attribute_list_opt
[1db21619]3570 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3571 ;
[51b73452]3572
3573abstract_ptr:
[dd51906]3574 ptrref_operator
[ce8c12f]3575 { $$ = DeclarationNode::newPointer( 0, $1 ); }
[dd51906]3576 | ptrref_operator type_qualifier_list
[ce8c12f]3577 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]3578 | ptrref_operator abstract_declarator
[ce8c12f]3579 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3580 | ptrref_operator type_qualifier_list abstract_declarator
[ce8c12f]3581 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]3582 | '(' abstract_ptr ')' attribute_list_opt
3583 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3584 ;
[51b73452]3585
3586abstract_array:
[4d51835]3587 array_dimension
3588 | '(' abstract_ptr ')' array_dimension
3589 { $$ = $2->addArray( $4 ); }
3590 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
3591 { $$ = $2->addArray( $4 ); }
3592 | '(' abstract_array ')' // redundant parenthesis
3593 { $$ = $2; }
3594 ;
[51b73452]3595
3596abstract_function:
[c0a33d2]3597 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3598 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3599 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3600 { $$ = $2->addParamList( $6 ); }
[4d51835]3601 | '(' abstract_function ')' // redundant parenthesis
3602 { $$ = $2; }
3603 ;
[51b73452]3604
3605array_dimension:
[4d51835]3606 // Only the first dimension can be empty.
[2871210]3607 '[' ']'
[4d51835]3608 { $$ = DeclarationNode::newArray( 0, 0, false ); }
[2871210]3609 | '[' ']' multi_array_dimension
3610 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
[6a99803]3611 | '[' push assignment_expression pop ',' comma_expression ']'
3612 { $$ = DeclarationNode::newArray( $3, 0, false )->addArray( DeclarationNode::newArray( $6, 0, false ) ); }
3613 // { SemanticError( yylloc, "New array dimension is currently unimplemented." ); $$ = nullptr; }
[4d51835]3614 | multi_array_dimension
3615 ;
[51b73452]3616
3617multi_array_dimension:
[c0a33d2]3618 '[' push assignment_expression pop ']'
3619 { $$ = DeclarationNode::newArray( $3, 0, false ); }
3620 | '[' push '*' pop ']' // C99
[4d51835]3621 { $$ = DeclarationNode::newVarArray( 0 ); }
[c0a33d2]3622 | multi_array_dimension '[' push assignment_expression pop ']'
3623 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
3624 | multi_array_dimension '[' push '*' pop ']' // C99
[4d51835]3625 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
3626 ;
[51b73452]3627
[c11e31c]3628// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
3629// identifier to which the type applies, e.g.:
3630//
[c0aa336]3631// int f( int ); // not handled here
3632// int f( int * ); // abstract function-prototype parameter; no parameter name specified
3633// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
[b87a5ed]3634// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]3635//
[de62360d]3636// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
[c0aa336]3637// and functions versus pointers to arrays and functions. In addition, the pattern handles the
3638// special meaning of parenthesis around a typedef name:
3639//
3640// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
3641// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
3642// not as redundant parentheses around the identifier."
3643//
3644// For example:
3645//
3646// typedef float T;
3647// int f( int ( T [5] ) ); // see abstract_parameter_declarator
3648// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
3649// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
3650// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
3651//
3652// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
3653// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
3654// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
3655// functions.
[51b73452]3656
3657abstract_parameter_declarator:
[4d51835]3658 abstract_parameter_ptr
[b6b3c42]3659 | '&' MUTEX attribute_list_opt
3660 { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
[4d51835]3661 | abstract_parameter_array attribute_list_opt
[1db21619]3662 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3663 | abstract_parameter_function attribute_list_opt
[1db21619]3664 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3665 ;
[51b73452]3666
3667abstract_parameter_ptr:
[dd51906]3668 ptrref_operator
[ce8c12f]3669 { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
[dd51906]3670 | ptrref_operator type_qualifier_list
[ce8c12f]3671 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]3672 | ptrref_operator abstract_parameter_declarator
[ce8c12f]3673 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[dd51906]3674 | ptrref_operator type_qualifier_list abstract_parameter_declarator
[ce8c12f]3675 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3676 | '(' abstract_parameter_ptr ')' attribute_list_opt // redundant parenthesis
[c0aa336]3677 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3678 ;
[51b73452]3679
3680abstract_parameter_array:
[4d51835]3681 array_parameter_dimension
3682 | '(' abstract_parameter_ptr ')' array_parameter_dimension
3683 { $$ = $2->addArray( $4 ); }
3684 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
3685 { $$ = $2->addArray( $4 ); }
3686 | '(' abstract_parameter_array ')' // redundant parenthesis
3687 { $$ = $2; }
3688 ;
[51b73452]3689
3690abstract_parameter_function:
[c0a33d2]3691 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3692 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3693 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3694 { $$ = $2->addParamList( $6 ); }
[4d51835]3695 | '(' abstract_parameter_function ')' // redundant parenthesis
3696 { $$ = $2; }
3697 ;
[51b73452]3698
3699array_parameter_dimension:
[4d51835]3700 // Only the first dimension can be empty or have qualifiers.
3701 array_parameter_1st_dimension
3702 | array_parameter_1st_dimension multi_array_dimension
3703 { $$ = $1->addArray( $2 ); }
3704 | multi_array_dimension
3705 ;
[51b73452]3706
[c11e31c]3707// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3708//
[de62360d]3709// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3710// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
[51b73452]3711
3712array_parameter_1st_dimension:
[2871210]3713 '[' ']'
[4d51835]3714 { $$ = DeclarationNode::newArray( 0, 0, false ); }
[13e8427]3715 // multi_array_dimension handles the '[' '*' ']' case
[c0a33d2]3716 | '[' push type_qualifier_list '*' pop ']' // remaining C99
3717 { $$ = DeclarationNode::newVarArray( $3 ); }
3718 | '[' push type_qualifier_list pop ']'
3719 { $$ = DeclarationNode::newArray( 0, $3, false ); }
[13e8427]3720 // multi_array_dimension handles the '[' assignment_expression ']' case
[c0a33d2]3721 | '[' push type_qualifier_list assignment_expression pop ']'
3722 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3723 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
3724 { $$ = DeclarationNode::newArray( $5, $4, true ); }
3725 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
3726 { $$ = DeclarationNode::newArray( $5, $3, true ); }
[4d51835]3727 ;
[b87a5ed]3728
[c0aa336]3729// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
[c11e31c]3730//
[c0aa336]3731// struct S {
3732// int;
3733// int *;
3734// int [10];
3735// int (*)();
3736// };
[51b73452]3737
3738variable_abstract_declarator:
[4d51835]3739 variable_abstract_ptr
3740 | variable_abstract_array attribute_list_opt
[1db21619]3741 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3742 | variable_abstract_function attribute_list_opt
[1db21619]3743 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3744 ;
[51b73452]3745
3746variable_abstract_ptr:
[dd51906]3747 ptrref_operator
[ce8c12f]3748 { $$ = DeclarationNode::newPointer( 0, $1 ); }
[dd51906]3749 | ptrref_operator type_qualifier_list
[ce8c12f]3750 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]3751 | ptrref_operator variable_abstract_declarator
[ce8c12f]3752 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3753 | ptrref_operator type_qualifier_list variable_abstract_declarator
[ce8c12f]3754 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3755 | '(' variable_abstract_ptr ')' attribute_list_opt // redundant parenthesis
[c0aa336]3756 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3757 ;
[51b73452]3758
3759variable_abstract_array:
[4d51835]3760 array_dimension
3761 | '(' variable_abstract_ptr ')' array_dimension
3762 { $$ = $2->addArray( $4 ); }
3763 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3764 { $$ = $2->addArray( $4 ); }
3765 | '(' variable_abstract_array ')' // redundant parenthesis
3766 { $$ = $2; }
3767 ;
[51b73452]3768
3769variable_abstract_function:
[c0a33d2]3770 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3771 { $$ = $2->addParamList( $6 ); }
[4d51835]3772 | '(' variable_abstract_function ')' // redundant parenthesis
3773 { $$ = $2; }
3774 ;
[b87a5ed]3775
[de62360d]3776// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3777// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
[b87a5ed]3778
[c0aa336]3779cfa_identifier_parameter_declarator_tuple: // CFA
3780 cfa_identifier_parameter_declarator_no_tuple
3781 | cfa_abstract_tuple
3782 | type_qualifier_list cfa_abstract_tuple
[4d51835]3783 { $$ = $2->addQualifiers( $1 ); }
3784 ;
[b87a5ed]3785
[c0aa336]3786cfa_identifier_parameter_declarator_no_tuple: // CFA
3787 cfa_identifier_parameter_ptr
3788 | cfa_identifier_parameter_array
[4d51835]3789 ;
[b87a5ed]3790
[c0aa336]3791cfa_identifier_parameter_ptr: // CFA
[d0ffed1]3792 // No SUE declaration in parameter list.
3793 ptrref_operator type_specifier_nobody
[ce8c12f]3794 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[d0ffed1]3795 | type_qualifier_list ptrref_operator type_specifier_nobody
[ce8c12f]3796 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3797 | ptrref_operator cfa_abstract_function
[ce8c12f]3798 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3799 | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]3800 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3801 | ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]3802 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3803 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]3804 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]3805 ;
[b87a5ed]3806
[c0aa336]3807cfa_identifier_parameter_array: // CFA
[de62360d]3808 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3809 // shift/reduce conflict with new-style empty (void) function return type.
[d0ffed1]3810 '[' ']' type_specifier_nobody
[2871210]3811 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]3812 | cfa_array_parameter_1st_dimension type_specifier_nobody
[4d51835]3813 { $$ = $2->addNewArray( $1 ); }
[d0ffed1]3814 | '[' ']' multi_array_dimension type_specifier_nobody
[2871210]3815 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]3816 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
[4d51835]3817 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[d0ffed1]3818 | multi_array_dimension type_specifier_nobody
[4d51835]3819 { $$ = $2->addNewArray( $1 ); }
[9059213]3820
[c0aa336]3821 | '[' ']' cfa_identifier_parameter_ptr
[2871210]3822 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]3823 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
[4d51835]3824 { $$ = $2->addNewArray( $1 ); }
[c0aa336]3825 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
[2871210]3826 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]3827 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]3828 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[c0aa336]3829 | multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]3830 { $$ = $2->addNewArray( $1 ); }
3831 ;
[51b73452]3832
[c0aa336]3833cfa_array_parameter_1st_dimension:
[c0a33d2]3834 '[' push type_qualifier_list '*' pop ']' // remaining C99
3835 { $$ = DeclarationNode::newVarArray( $3 ); }
3836 | '[' push type_qualifier_list assignment_expression pop ']'
3837 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3838 | '[' push declaration_qualifier_list assignment_expression pop ']'
[4d51835]3839 // declaration_qualifier_list must be used because of shift/reduce conflict with
3840 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3841 // appear in this context.
[c0a33d2]3842 { $$ = DeclarationNode::newArray( $4, $3, true ); }
3843 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3844 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
[4d51835]3845 ;
[b87a5ed]3846
[de62360d]3847// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3848// identifier to which the type applies, e.g.:
[c11e31c]3849//
[b87a5ed]3850// [int] f( int ); // abstract variable parameter; no parameter name specified
3851// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]3852//
3853// These rules need LR(3):
3854//
[c0aa336]3855// cfa_abstract_tuple identifier_or_type_name
[40de461]3856// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
[c11e31c]3857//
3858// since a function return type can be syntactically identical to a tuple type:
3859//
[b87a5ed]3860// [int, int] t;
3861// [int, int] f( int );
[c11e31c]3862//
[2871210]3863// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
[c0aa336]3864// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3865// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3866// duplicated when appearing with cfa_function_specifier.
[b87a5ed]3867
[c0aa336]3868cfa_abstract_declarator_tuple: // CFA
3869 cfa_abstract_tuple
3870 | type_qualifier_list cfa_abstract_tuple
[4d51835]3871 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]3872 | cfa_abstract_declarator_no_tuple
[4d51835]3873 ;
[b87a5ed]3874
[c0aa336]3875cfa_abstract_declarator_no_tuple: // CFA
3876 cfa_abstract_ptr
3877 | cfa_abstract_array
[4d51835]3878 ;
[b87a5ed]3879
[c0aa336]3880cfa_abstract_ptr: // CFA
[dd51906]3881 ptrref_operator type_specifier
[ce8c12f]3882 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3883 | type_qualifier_list ptrref_operator type_specifier
[ce8c12f]3884 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3885 | ptrref_operator cfa_abstract_function
[ce8c12f]3886 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3887 | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]3888 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3889 | ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]3890 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3891 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]3892 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]3893 ;
[b87a5ed]3894
[c0aa336]3895cfa_abstract_array: // CFA
[de62360d]3896 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3897 // empty (void) function return type.
[2871210]3898 '[' ']' type_specifier
[2298f728]3899 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[2871210]3900 | '[' ']' multi_array_dimension type_specifier
[2298f728]3901 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[4d51835]3902 | multi_array_dimension type_specifier
3903 { $$ = $2->addNewArray( $1 ); }
[c0aa336]3904 | '[' ']' cfa_abstract_ptr
[2298f728]3905 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]3906 | '[' ']' multi_array_dimension cfa_abstract_ptr
[2298f728]3907 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]3908 | multi_array_dimension cfa_abstract_ptr
[4d51835]3909 { $$ = $2->addNewArray( $1 ); }
3910 ;
[b87a5ed]3911
[c0aa336]3912cfa_abstract_tuple: // CFA
[c0a33d2]3913 '[' push cfa_abstract_parameter_list pop ']'
3914 { $$ = DeclarationNode::newTuple( $3 ); }
[35718a9]3915 | '[' push type_specifier_nobody ELLIPSIS pop ']'
[13e8427]3916 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
[35718a9]3917 | '[' push type_specifier_nobody ELLIPSIS constant_expression pop ']'
[13e8427]3918 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
[4d51835]3919 ;
[b87a5ed]3920
[c0aa336]3921cfa_abstract_function: // CFA
[40de461]3922// '[' ']' '(' cfa_parameter_ellipsis_list_opt ')'
[1b29996]3923// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
[40de461]3924 cfa_abstract_tuple '(' push cfa_parameter_ellipsis_list_opt pop ')'
[c0a33d2]3925 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[40de461]3926 | cfa_function_return '(' push cfa_parameter_ellipsis_list_opt pop ')'
[c0a33d2]3927 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[4d51835]3928 ;
[b87a5ed]3929
[de62360d]3930// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3931// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
[c11e31c]3932//
[de62360d]3933// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3934// the declaration specifiers in a declaration is an obsolescent feature."
[c11e31c]3935//
3936// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3937// prototype-format parameter type declarators) is an obsolescent feature."
3938//
[de62360d]3939// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3940// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
[51b73452]3941
[c11e31c]3942//************************* MISCELLANEOUS ********************************
[51b73452]3943
[b87a5ed]3944comma_opt: // redundant comma
[4d51835]3945 // empty
3946 | ','
3947 ;
[51b73452]3948
[5a51798]3949default_initializer_opt:
[4d51835]3950 // empty
[58dd019]3951 { $$ = nullptr; }
[4d51835]3952 | '=' assignment_expression
3953 { $$ = $2; }
3954 ;
[51b73452]3955
3956%%
[a1c9ddd]3957
[c11e31c]3958// ----end of grammar----
[51b73452]3959
[c11e31c]3960// Local Variables: //
[b87a5ed]3961// mode: c++ //
[de62360d]3962// tab-width: 4 //
[c11e31c]3963// compile-command: "make install" //
3964// End: //
Note: See TracBrowser for help on using the repository browser.