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