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