source: src/Parser/parser.yy@ b9cd3b5

ADT ast-experimental
Last change on this file since b9cd3b5 was c86b08d, checked in by caparsons <caparson@…>, 3 years ago

added support for the waituntil statement in the compiler

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