source: src/Parser/parser.yy@ 81bd7e3

ADT ast-experimental
Last change on this file since 81bd7e3 was 32d6fdc, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Clean-up in the parser %union.

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