source: src/Parser/parser.yy@ 5c98a25

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

update for-control with explicit type declarations

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