source: src/Parser/parser.yy@ 8472c6c

ADT ast-experimental stuck-waitfor-destruct
Last change on this file since 8472c6c was c468150, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Split up ParseNode.h so that headers match implementation. May have a bit less to include total because of it.

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