source: src/Parser/parser.yy@ 85dd381

ADT ast-experimental
Last change on this file since 85dd381 was 7a24d76, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

simply grammar for forall with trait

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