source: src/Parser/parser.yy@ ee23a8d

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since ee23a8d was 9380add, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

formatting, remove spurious semi-colon at end of vtable rule

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