source: src/Parser/parser.yy@ 96fc67b

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 96fc67b was f9941ff, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add error message for unimplemented qualified names [fixes #54]

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