source: src/Parser/parser.yy@ d30e3eb

ADT ast-experimental stuck-waitfor-destruct
Last change on this file since d30e3eb was 1f771fc, checked in by Mugilan Ganesan <mganesan@…>, 3 years ago

Removed list initialization and simple assignment expression rules for functions. Split variable_type_redeclarator rule into variable_type_redeclarator and function_type_redeclarator rules

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