source: src/Parser/parser.yy@ 52f9804

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

Clean-up in parser. ClauseNode rework, plus internal adjustments to reduce extra code and unchecked casts.

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