[b87a5ed] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[c11e31c] | 7 | // cfa.y --
|
---|
| 8 | //
|
---|
| 9 | // Author : Peter A. Buhr
|
---|
| 10 | // Created On : Sat Sep 1 20:22:55 2001
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[5f2f2d7] | 12 | // Last Modified On : Mon Jun 8 20:31:07 2015
|
---|
| 13 | // Update Count : 1030
|
---|
[c11e31c] | 14 | //
|
---|
| 15 |
|
---|
[b87a5ed] | 16 | // This grammar is based on the ANSI99/11 C grammar, specifically parts of EXPRESSION and STATEMENTS, and on
|
---|
| 17 | // the C grammar by James A. Roskind, specifically parts of DECLARATIONS and EXTERNAL DEFINITIONS. While
|
---|
| 18 | // parts have been copied, important changes have been made in all sections; these changes are sufficient to
|
---|
| 19 | // constitute a new grammar. In particular, this grammar attempts to be more syntactically precise, i.e., it
|
---|
| 20 | // parses less incorrect language syntax that must be subsequently rejected by semantic checks. Nevertheless,
|
---|
| 21 | // there are still several semantic checks required and many are noted in the grammar. Finally, the grammar is
|
---|
| 22 | // extended with GCC and CFA language extensions.
|
---|
[c11e31c] | 23 |
|
---|
[b87a5ed] | 24 | // Acknowledgments to Richard Bilson, Glen Ditchfield, and Rodolfo Gabriel Esteves who all helped when I got
|
---|
| 25 | // stuck with the grammar.
|
---|
[c11e31c] | 26 |
|
---|
| 27 | // The root language for this grammar is ANSI99/11 C. All of ANSI99/11 is parsed, except for:
|
---|
| 28 | //
|
---|
| 29 | // 1. designation with '=' (use ':' instead)
|
---|
| 30 | //
|
---|
[b87a5ed] | 31 | // Most of the syntactic extensions from ANSI90 to ANSI11 C are marked with the comment "C99/C11". This
|
---|
| 32 | // grammar also has two levels of extensions. The first extensions cover most of the GCC C extensions, except for:
|
---|
[c11e31c] | 33 | //
|
---|
| 34 | // 1. nested functions
|
---|
| 35 | // 2. generalized lvalues
|
---|
| 36 | // 3. designation with and without '=' (use ':' instead)
|
---|
| 37 | // 4. attributes not allowed in parenthesis of declarator
|
---|
| 38 | //
|
---|
[b87a5ed] | 39 | // All of the syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for
|
---|
| 40 | // Cforall (CFA), which fixes several of C's outstanding problems and extends C with many modern language
|
---|
| 41 | // concepts. All of the syntactic extensions for CFA C are marked with the comment "CFA". As noted above,
|
---|
| 42 | // there is one unreconcileable parsing problem between C99 and CFA with respect to designators; this is
|
---|
| 43 | // discussed in detail before the "designation" grammar rule.
|
---|
[51b73452] | 44 |
|
---|
| 45 | %{
|
---|
[b87a5ed] | 46 | #define YYDEBUG_LEXER_TEXT (yylval) // lexer loads this up each time
|
---|
| 47 | #define YYDEBUG 1 // get the pretty debugging code to compile
|
---|
[51b73452] | 48 |
|
---|
| 49 | #undef __GNUC_MINOR__
|
---|
| 50 |
|
---|
| 51 | #include <cstdio>
|
---|
| 52 | #include <stack>
|
---|
| 53 | #include "TypedefTable.h"
|
---|
| 54 | #include "lex.h"
|
---|
| 55 | #include "ParseNode.h"
|
---|
| 56 | #include "LinkageSpec.h"
|
---|
| 57 |
|
---|
[b87a5ed] | 58 | DeclarationNode *theTree = 0; // the resulting parse tree
|
---|
[51b73452] | 59 | LinkageSpec::Type linkage = LinkageSpec::Cforall;
|
---|
| 60 | std::stack< LinkageSpec::Type > linkageStack;
|
---|
| 61 | TypedefTable typedefTable;
|
---|
| 62 | %}
|
---|
| 63 |
|
---|
[c11e31c] | 64 | //************************* TERMINAL TOKENS ********************************
|
---|
[51b73452] | 65 |
|
---|
[c11e31c] | 66 | // keywords
|
---|
[51b73452] | 67 | %token TYPEDEF
|
---|
| 68 | %token AUTO EXTERN REGISTER STATIC
|
---|
[b87a5ed] | 69 | %token INLINE // C99
|
---|
| 70 | %token FORTRAN // C99, extension ISO/IEC 9899:1999 Section J.5.9(1)
|
---|
[51b73452] | 71 | %token CONST VOLATILE
|
---|
[b87a5ed] | 72 | %token RESTRICT // C99
|
---|
| 73 | %token FORALL LVALUE // CFA
|
---|
[51b73452] | 74 | %token VOID CHAR SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED
|
---|
[b87a5ed] | 75 | %token BOOL COMPLEX IMAGINARY // C99
|
---|
| 76 | %token TYPEOF LABEL // GCC
|
---|
[51b73452] | 77 | %token ENUM STRUCT UNION
|
---|
[b87a5ed] | 78 | %token TYPE FTYPE DTYPE CONTEXT // CFA
|
---|
[51b73452] | 79 | %token SIZEOF
|
---|
[b87a5ed] | 80 | %token ATTRIBUTE EXTENSION // GCC
|
---|
[51b73452] | 81 | %token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
|
---|
[b87a5ed] | 82 | %token CHOOSE FALLTHRU TRY CATCH FINALLY THROW // CFA
|
---|
| 83 | %token ASM // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
|
---|
[c11e31c] | 84 | %token ALIGNAS ALIGNOF ATOMIC GENERIC NORETURN STATICASSERT THREADLOCAL // C11
|
---|
[51b73452] | 85 |
|
---|
[c11e31c] | 86 | // names and constants: lexer differentiates between identifier and typedef names
|
---|
[b87a5ed] | 87 | %token<tok> IDENTIFIER QUOTED_IDENTIFIER TYPEDEFname TYPEGENname
|
---|
| 88 | %token<tok> ATTR_IDENTIFIER ATTR_TYPEDEFname ATTR_TYPEGENname
|
---|
| 89 | %token<tok> INTEGERconstant FLOATINGconstant CHARACTERconstant STRINGliteral
|
---|
| 90 | %token<tok> ZERO ONE // CFA
|
---|
[51b73452] | 91 |
|
---|
[c11e31c] | 92 | // multi-character operators
|
---|
[b87a5ed] | 93 | %token ARROW // ->
|
---|
| 94 | %token ICR DECR // ++ --
|
---|
| 95 | %token LS RS // << >>
|
---|
| 96 | %token LE GE EQ NE // <= >= == !=
|
---|
| 97 | %token ANDAND OROR // && ||
|
---|
| 98 | %token ELLIPSIS // ...
|
---|
| 99 |
|
---|
| 100 | %token MULTassign DIVassign MODassign // *= /= %=/
|
---|
| 101 | %token PLUSassign MINUSassign // += -=
|
---|
| 102 | %token LSassign RSassign // <<= >>=
|
---|
| 103 | %token ANDassign ERassign ORassign // &= ^= |=
|
---|
[51b73452] | 104 |
|
---|
[c11e31c] | 105 | // Types declaration
|
---|
[51b73452] | 106 | %union
|
---|
| 107 | {
|
---|
[b87a5ed] | 108 | Token tok;
|
---|
| 109 | ParseNode *pn;
|
---|
| 110 | ExpressionNode *en;
|
---|
| 111 | DeclarationNode *decl;
|
---|
| 112 | DeclarationNode::TyCon aggKey;
|
---|
| 113 | DeclarationNode::TypeClass tclass;
|
---|
| 114 | StatementNode *sn;
|
---|
| 115 | ConstantNode *constant;
|
---|
| 116 | InitializerNode *in;
|
---|
[51b73452] | 117 | }
|
---|
| 118 |
|
---|
| 119 | %type<tok> zero_one identifier no_attr_identifier no_01_identifier
|
---|
| 120 | %type<tok> identifier_or_typedef_name no_attr_identifier_or_typedef_name no_01_identifier_or_typedef_name
|
---|
| 121 | %type<constant> string_literal_list
|
---|
| 122 |
|
---|
[c11e31c] | 123 | // expressions
|
---|
[51b73452] | 124 | %type<constant> constant
|
---|
[b87a5ed] | 125 | %type<en> tuple tuple_expression_list
|
---|
| 126 | %type<en> unary_operator assignment_operator
|
---|
| 127 | %type<en> primary_expression postfix_expression unary_expression
|
---|
| 128 | %type<en> cast_expression multiplicative_expression additive_expression shift_expression
|
---|
| 129 | %type<en> relational_expression equality_expression AND_expression exclusive_OR_expression
|
---|
| 130 | %type<en> inclusive_OR_expression logical_AND_expression logical_OR_expression conditional_expression
|
---|
| 131 | %type<en> constant_expression assignment_expression assignment_expression_opt
|
---|
| 132 | %type<en> comma_expression comma_expression_opt
|
---|
| 133 | %type<en> argument_expression_list argument_expression for_control_expression assignment_opt
|
---|
[51b73452] | 134 | %type<en> subrange
|
---|
| 135 |
|
---|
[c11e31c] | 136 | // statements
|
---|
[b87a5ed] | 137 | %type<sn> labeled_statement compound_statement expression_statement selection_statement
|
---|
| 138 | %type<sn> iteration_statement jump_statement exception_statement asm_statement
|
---|
| 139 | %type<sn> fall_through_opt fall_through
|
---|
| 140 | %type<sn> statement statement_list
|
---|
| 141 | %type<sn> block_item_list block_item
|
---|
[51b73452] | 142 | %type<sn> case_clause
|
---|
[b87a5ed] | 143 | %type<en> case_value case_value_list
|
---|
| 144 | %type<sn> case_label case_label_list
|
---|
| 145 | %type<sn> switch_clause_list_opt switch_clause_list choose_clause_list_opt choose_clause_list
|
---|
| 146 | %type<pn> handler_list handler_clause finally_clause
|
---|
[51b73452] | 147 |
|
---|
[c11e31c] | 148 | // declarations
|
---|
[51b73452] | 149 | %type<decl> abstract_array abstract_declarator abstract_function abstract_parameter_array
|
---|
| 150 | %type<decl> abstract_parameter_declaration abstract_parameter_declarator abstract_parameter_function
|
---|
| 151 | %type<decl> abstract_parameter_ptr abstract_ptr
|
---|
| 152 |
|
---|
| 153 | %type<aggKey> aggregate_key
|
---|
| 154 | %type<decl> aggregate_name
|
---|
| 155 |
|
---|
| 156 | %type<decl> array_dimension array_parameter_1st_dimension array_parameter_dimension multi_array_dimension
|
---|
| 157 |
|
---|
| 158 | %type<decl> assertion assertion_list_opt
|
---|
| 159 |
|
---|
| 160 | %type<en> bit_subrange_size_opt bit_subrange_size
|
---|
| 161 |
|
---|
| 162 | %type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type_name indirect_type_name
|
---|
| 163 |
|
---|
| 164 | %type<decl> context_declaration context_declaration_list context_declaring_list context_specifier
|
---|
| 165 |
|
---|
| 166 | %type<decl> declaration declaration_list declaration_list_opt declaration_qualifier_list
|
---|
| 167 | %type<decl> declaration_specifier declarator declaring_list
|
---|
| 168 |
|
---|
| 169 | %type<decl> elaborated_type_name
|
---|
| 170 |
|
---|
| 171 | %type<decl> enumerator_list enum_name
|
---|
| 172 | %type<en> enumerator_value_opt
|
---|
| 173 |
|
---|
| 174 | %type<decl> exception_declaration external_definition external_definition_list external_definition_list_opt
|
---|
| 175 |
|
---|
| 176 | %type<decl> field_declaration field_declaration_list field_declarator field_declaring_list
|
---|
| 177 | %type<en> field field_list
|
---|
| 178 |
|
---|
[4d51835] | 179 | %type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
|
---|
[51b73452] | 180 |
|
---|
| 181 | %type<decl> identifier_parameter_array identifier_parameter_declarator identifier_parameter_function
|
---|
| 182 | %type<decl> identifier_parameter_ptr identifier_list
|
---|
| 183 |
|
---|
| 184 | %type<decl> new_abstract_array new_abstract_declarator_no_tuple new_abstract_declarator_tuple
|
---|
| 185 | %type<decl> new_abstract_function new_abstract_parameter_declaration new_abstract_parameter_list
|
---|
| 186 | %type<decl> new_abstract_ptr new_abstract_tuple
|
---|
| 187 |
|
---|
| 188 | %type<decl> new_array_parameter_1st_dimension
|
---|
| 189 |
|
---|
| 190 | %type<decl> new_context_declaring_list new_declaration new_field_declaring_list
|
---|
| 191 | %type<decl> new_function_declaration new_function_return new_function_specifier
|
---|
| 192 |
|
---|
| 193 | %type<decl> new_identifier_parameter_array new_identifier_parameter_declarator_no_tuple
|
---|
| 194 | %type<decl> new_identifier_parameter_declarator_tuple new_identifier_parameter_ptr
|
---|
| 195 |
|
---|
| 196 | %type<decl> new_parameter_declaration new_parameter_list new_parameter_type_list new_parameter_type_list_opt
|
---|
| 197 |
|
---|
| 198 | %type<decl> new_typedef_declaration new_variable_declaration new_variable_specifier
|
---|
| 199 |
|
---|
| 200 | %type<decl> old_declaration old_declaration_list old_declaration_list_opt old_function_array
|
---|
| 201 | %type<decl> old_function_declarator old_function_no_ptr old_function_ptr
|
---|
| 202 |
|
---|
| 203 | %type<decl> parameter_declaration parameter_list parameter_type_list
|
---|
| 204 | %type<decl> parameter_type_list_opt
|
---|
| 205 |
|
---|
| 206 | %type<decl> paren_identifier paren_typedef
|
---|
| 207 |
|
---|
| 208 | %type<decl> storage_class storage_class_name storage_class_list
|
---|
| 209 |
|
---|
| 210 | %type<decl> sue_declaration_specifier sue_type_specifier
|
---|
| 211 |
|
---|
| 212 | %type<tclass> type_class
|
---|
| 213 | %type<decl> type_declarator type_declarator_name type_declaring_list
|
---|
| 214 |
|
---|
| 215 | %type<decl> typedef typedef_array typedef_declaration typedef_declaration_specifier typedef_expression
|
---|
| 216 | %type<decl> typedef_function typedef_parameter_array typedef_parameter_function typedef_parameter_ptr
|
---|
| 217 | %type<decl> typedef_parameter_redeclarator typedef_ptr typedef_redeclarator typedef_type_specifier
|
---|
| 218 | %type<decl> typegen_declaration_specifier typegen_type_specifier
|
---|
| 219 |
|
---|
| 220 | %type<decl> type_name type_name_no_function
|
---|
| 221 | %type<decl> type_parameter type_parameter_list
|
---|
| 222 |
|
---|
| 223 | %type<en> type_name_list
|
---|
| 224 |
|
---|
| 225 | %type<decl> type_qualifier type_qualifier_name type_qualifier_list type_qualifier_list_opt type_specifier
|
---|
| 226 |
|
---|
| 227 | %type<decl> variable_abstract_array variable_abstract_declarator variable_abstract_function
|
---|
| 228 | %type<decl> variable_abstract_ptr variable_array variable_declarator variable_function variable_ptr
|
---|
| 229 |
|
---|
[c11e31c] | 230 | // initializers
|
---|
[51b73452] | 231 | %type<in> initializer initializer_list initializer_opt
|
---|
| 232 |
|
---|
[c11e31c] | 233 | // designators
|
---|
[51b73452] | 234 | %type<en> designator designator_list designation
|
---|
| 235 |
|
---|
| 236 |
|
---|
[b87a5ed] | 237 | // Handle single shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string
|
---|
| 238 | // is ambiguous:
|
---|
| 239 | // .---------. matches IF '(' comma_expression ')' statement
|
---|
[c11e31c] | 240 | // if ( C ) S1 else S2
|
---|
[b87a5ed] | 241 | // `-----------------' matches IF '(' comma_expression ')' statement ELSE statement */
|
---|
[51b73452] | 242 |
|
---|
[c11e31c] | 243 | %nonassoc THEN // rule precedence for IF '(' comma_expression ')' statement
|
---|
| 244 | %nonassoc ELSE // token precedence for start of else clause in IF statement
|
---|
[51b73452] | 245 |
|
---|
[b87a5ed] | 246 | %start translation_unit // parse-tree root
|
---|
[51b73452] | 247 |
|
---|
| 248 | %%
|
---|
[c11e31c] | 249 | //************************* Namespace Management ********************************
|
---|
| 250 |
|
---|
[b87a5ed] | 251 | // The grammar in the ANSI C standard is not strictly context-free, since it relies upon the distinct terminal
|
---|
| 252 | // symbols "identifier" and "TYPEDEFname" that are lexically identical. While it is possible to write a
|
---|
| 253 | // purely context-free grammar, such a grammar would obscure the relationship between syntactic and semantic
|
---|
| 254 | // constructs. Hence, this grammar uses the ANSI style.
|
---|
[c11e31c] | 255 | //
|
---|
[b87a5ed] | 256 | // Cforall compounds this problem by introducing type names local to the scope of a declaration (for instance,
|
---|
| 257 | // those introduced through "forall" qualifiers), and by introducing "type generators" -- parametrized types.
|
---|
| 258 | // This latter type name creates a third class of identifiers that must be distinguished by the scanner.
|
---|
[c11e31c] | 259 | //
|
---|
[b87a5ed] | 260 | // Since the scanner cannot distinguish among the different classes of identifiers without some context
|
---|
| 261 | // information, it accesses a data structure (the TypedefTable) to allow classification of an identifier that
|
---|
| 262 | // it has just read. Semantic actions during the parser update this data structure when the class of
|
---|
| 263 | // identifiers change.
|
---|
[c11e31c] | 264 | //
|
---|
[b87a5ed] | 265 | // Because the Cforall language is block-scoped, there is the possibility that an identifier can change its
|
---|
| 266 | // class in a local scope; it must revert to its original class at the end of the block. Since type names can
|
---|
| 267 | // be local to a particular declaration, each declaration is itself a scope. This requires distinguishing
|
---|
| 268 | // between type names that are local to the current declaration scope and those that persist past the end of
|
---|
| 269 | // the declaration (i.e., names defined in "typedef" or "type" declarations).
|
---|
[c11e31c] | 270 | //
|
---|
[b87a5ed] | 271 | // The non-terminals "push" and "pop" derive the empty string; their only use is to denote the opening and
|
---|
| 272 | // closing of scopes. Every push must have a matching pop, although it is regrettable the matching pairs do
|
---|
| 273 | // not always occur within the same rule. These non-terminals may appear in more contexts than strictly
|
---|
| 274 | // necessary from a semantic point of view. Unfortunately, these extra rules are necessary to prevent parsing
|
---|
| 275 | // conflicts -- the parser may not have enough context and look-ahead information to decide whether a new
|
---|
| 276 | // scope is necessary, so the effect of these extra rules is to open a new scope unconditionally. As the
|
---|
| 277 | // grammar evolves, it may be neccesary to add or move around "push" and "pop" nonterminals to resolve
|
---|
| 278 | // conflicts of this sort.
|
---|
[51b73452] | 279 |
|
---|
| 280 | push:
|
---|
[4d51835] | 281 | {
|
---|
| 282 | typedefTable.enterScope();
|
---|
| 283 | }
|
---|
| 284 | ;
|
---|
[51b73452] | 285 |
|
---|
| 286 | pop:
|
---|
[4d51835] | 287 | {
|
---|
| 288 | typedefTable.leaveScope();
|
---|
| 289 | }
|
---|
| 290 | ;
|
---|
[51b73452] | 291 |
|
---|
[c11e31c] | 292 | //************************* CONSTANTS ********************************
|
---|
[51b73452] | 293 |
|
---|
| 294 | constant:
|
---|
[4d51835] | 295 | // ENUMERATIONconstant is not included here; it is treated as a variable with type
|
---|
| 296 | // "enumeration constant".
|
---|
[59db689] | 297 | INTEGERconstant { $$ = new ConstantNode( ConstantNode::Integer, $1 ); }
|
---|
| 298 | | FLOATINGconstant { $$ = new ConstantNode( ConstantNode::Float, $1 ); }
|
---|
| 299 | | CHARACTERconstant { $$ = new ConstantNode( ConstantNode::Character, $1 ); }
|
---|
[4d51835] | 300 | ;
|
---|
[51b73452] | 301 |
|
---|
| 302 | identifier:
|
---|
[4d51835] | 303 | IDENTIFIER
|
---|
| 304 | | ATTR_IDENTIFIER // CFA
|
---|
| 305 | | zero_one // CFA
|
---|
| 306 | ;
|
---|
[51b73452] | 307 |
|
---|
| 308 | no_01_identifier:
|
---|
[4d51835] | 309 | IDENTIFIER
|
---|
| 310 | | ATTR_IDENTIFIER // CFA
|
---|
| 311 | ;
|
---|
[51b73452] | 312 |
|
---|
| 313 | no_attr_identifier:
|
---|
[4d51835] | 314 | IDENTIFIER
|
---|
| 315 | ;
|
---|
[51b73452] | 316 |
|
---|
[b87a5ed] | 317 | zero_one: // CFA
|
---|
[4d51835] | 318 | ZERO
|
---|
| 319 | | ONE
|
---|
| 320 | ;
|
---|
[51b73452] | 321 |
|
---|
[b87a5ed] | 322 | string_literal_list: // juxtaposed strings are concatenated
|
---|
[59db689] | 323 | STRINGliteral { $$ = new ConstantNode( ConstantNode::String, $1 ); }
|
---|
| 324 | | string_literal_list STRINGliteral { $$ = $1->appendstr( $2 ); }
|
---|
[4d51835] | 325 | ;
|
---|
[51b73452] | 326 |
|
---|
[c11e31c] | 327 | //************************* EXPRESSIONS ********************************
|
---|
[51b73452] | 328 |
|
---|
| 329 | primary_expression:
|
---|
[4d51835] | 330 | IDENTIFIER // typedef name cannot be used as a variable name
|
---|
| 331 | { $$ = new VarRefNode($1); }
|
---|
| 332 | | zero_one
|
---|
| 333 | { $$ = new VarRefNode($1); }
|
---|
| 334 | | constant
|
---|
| 335 | { $$ = $1; }
|
---|
| 336 | | string_literal_list
|
---|
| 337 | { $$ = $1; }
|
---|
| 338 | | '(' comma_expression ')'
|
---|
| 339 | { $$ = $2; }
|
---|
| 340 | | '(' compound_statement ')' // GCC, lambda expression
|
---|
| 341 | { $$ = new ValofExprNode($2); }
|
---|
| 342 | ;
|
---|
[51b73452] | 343 |
|
---|
| 344 | postfix_expression:
|
---|
[4d51835] | 345 | primary_expression
|
---|
| 346 | | postfix_expression '[' push assignment_expression pop ']'
|
---|
| 347 | // CFA, comma_expression disallowed in the context because it results in a commom user error:
|
---|
| 348 | // subscripting a matrix with x[i,j] instead of x[i][j]. While this change is not backwards
|
---|
| 349 | // compatible, there seems to be little advantage to this feature and many disadvantages. It is
|
---|
| 350 | // possible to write x[(i,j)] in CFA, which is equivalent to the old x[i,j].
|
---|
| 351 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Index), $1, $4); }
|
---|
| 352 | | postfix_expression '(' argument_expression_list ')'
|
---|
| 353 | { $$ = new CompositeExprNode($1, $3); }
|
---|
| 354 | | postfix_expression '.' no_attr_identifier
|
---|
| 355 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), $1, new VarRefNode($3)); }
|
---|
| 356 | | postfix_expression '.' '[' push field_list pop ']' // CFA, tuple field selector
|
---|
| 357 | | postfix_expression ARROW no_attr_identifier
|
---|
| 358 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), $1, new VarRefNode($3)); }
|
---|
| 359 | | postfix_expression ARROW '[' push field_list pop ']' // CFA, tuple field selector
|
---|
| 360 | | postfix_expression ICR
|
---|
| 361 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::IncrPost), $1); }
|
---|
| 362 | | postfix_expression DECR
|
---|
| 363 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::DecrPost), $1); }
|
---|
| 364 | // GCC has priority: cast_expression
|
---|
| 365 | | '(' type_name_no_function ')' '{' initializer_list comma_opt '}' // C99
|
---|
| 366 | { $$ = 0; }
|
---|
| 367 | ;
|
---|
[51b73452] | 368 |
|
---|
| 369 | argument_expression_list:
|
---|
[4d51835] | 370 | argument_expression
|
---|
| 371 | | argument_expression_list ',' argument_expression
|
---|
| 372 | { $$ = (ExpressionNode *)($1->set_link($3)); }
|
---|
| 373 | ;
|
---|
[51b73452] | 374 |
|
---|
| 375 | argument_expression:
|
---|
[4d51835] | 376 | // empty
|
---|
| 377 | { $$ = 0; } // use default argument
|
---|
| 378 | | assignment_expression
|
---|
| 379 | | no_attr_identifier ':' assignment_expression
|
---|
| 380 | { $$ = $3->set_asArgName($1); }
|
---|
| 381 | // Only a list of no_attr_identifier_or_typedef_name is allowed in this context. However, there is
|
---|
| 382 | // insufficient look ahead to distinguish between this list of parameter names and a tuple, so the
|
---|
| 383 | // tuple form must be used with an appropriate semantic check.
|
---|
| 384 | | '[' push assignment_expression pop ']' ':' assignment_expression
|
---|
| 385 | { $$ = $7->set_asArgName($3); }
|
---|
| 386 | | '[' push assignment_expression ',' tuple_expression_list pop ']' ':' assignment_expression
|
---|
| 387 | { $$ = $9->set_asArgName( new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ), (ExpressionNode *)$3->set_link( flattenCommas( $5 )))); }
|
---|
| 388 | ;
|
---|
[b87a5ed] | 389 |
|
---|
| 390 | field_list: // CFA, tuple field selector
|
---|
[4d51835] | 391 | field
|
---|
| 392 | | field_list ',' field { $$ = (ExpressionNode *)$1->set_link( $3 ); }
|
---|
| 393 | ;
|
---|
[b87a5ed] | 394 |
|
---|
| 395 | field: // CFA, tuple field selector
|
---|
[4d51835] | 396 | no_attr_identifier
|
---|
| 397 | { $$ = new VarRefNode( $1 ); }
|
---|
| 398 | | no_attr_identifier '.' field
|
---|
| 399 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $3); }
|
---|
| 400 | | no_attr_identifier '.' '[' push field_list pop ']'
|
---|
| 401 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $5); }
|
---|
| 402 | | no_attr_identifier ARROW field
|
---|
| 403 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $3); }
|
---|
| 404 | | no_attr_identifier ARROW '[' push field_list pop ']'
|
---|
| 405 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $5); }
|
---|
| 406 | ;
|
---|
[51b73452] | 407 |
|
---|
| 408 | unary_expression:
|
---|
[4d51835] | 409 | postfix_expression
|
---|
| 410 | | ICR unary_expression
|
---|
| 411 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Incr), $2); }
|
---|
| 412 | | DECR unary_expression
|
---|
| 413 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Decr), $2); }
|
---|
| 414 | | EXTENSION cast_expression // GCC
|
---|
| 415 | { $$ = $2; }
|
---|
| 416 | | unary_operator cast_expression
|
---|
| 417 | { $$ = new CompositeExprNode($1, $2); }
|
---|
| 418 | | '!' cast_expression
|
---|
| 419 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Neg), $2); }
|
---|
| 420 | | '*' cast_expression // CFA
|
---|
| 421 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PointTo), $2); }
|
---|
| 422 | // '*' is is separated from unary_operator because of shift/reduce conflict in:
|
---|
| 423 | // { * X; } // dereference X
|
---|
| 424 | // { * int X; } // CFA declaration of pointer to int
|
---|
| 425 | // '&' must be moved here if C++ reference variables are supported.
|
---|
| 426 | | SIZEOF unary_expression
|
---|
| 427 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::SizeOf), $2); }
|
---|
| 428 | | SIZEOF '(' type_name_no_function ')'
|
---|
| 429 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::SizeOf), new TypeValueNode($3)); }
|
---|
| 430 | | ATTR_IDENTIFIER
|
---|
| 431 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Attr), new VarRefNode($1)); }
|
---|
| 432 | | ATTR_IDENTIFIER '(' type_name ')'
|
---|
| 433 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Attr), new VarRefNode($1), new TypeValueNode($3)); }
|
---|
| 434 | | ATTR_IDENTIFIER '(' argument_expression ')'
|
---|
| 435 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Attr), new VarRefNode($1), $3); }
|
---|
| 436 | | ALIGNOF unary_expression // GCC, variable alignment
|
---|
| 437 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::AlignOf), $2); }
|
---|
| 438 | | ALIGNOF '(' type_name_no_function ')' // GCC, type alignment
|
---|
| 439 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::AlignOf), new TypeValueNode($3)); }
|
---|
| 440 | | ANDAND no_attr_identifier // GCC, address of label
|
---|
| 441 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LabelAddress), new VarRefNode($2, true)); }
|
---|
| 442 | ;
|
---|
[51b73452] | 443 |
|
---|
| 444 | unary_operator:
|
---|
[4d51835] | 445 | '&' { $$ = new OperatorNode(OperatorNode::AddressOf); }
|
---|
| 446 | | '+' { $$ = new OperatorNode(OperatorNode::UnPlus); }
|
---|
| 447 | | '-' { $$ = new OperatorNode(OperatorNode::UnMinus); }
|
---|
| 448 | | '~' { $$ = new OperatorNode(OperatorNode::BitNeg); }
|
---|
| 449 | ;
|
---|
[51b73452] | 450 |
|
---|
| 451 | cast_expression:
|
---|
[4d51835] | 452 | unary_expression
|
---|
| 453 | | '(' type_name_no_function ')' cast_expression
|
---|
| 454 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast), new TypeValueNode($2), $4); }
|
---|
| 455 | | '(' type_name_no_function ')' tuple
|
---|
| 456 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast), new TypeValueNode($2), $4); }
|
---|
| 457 | ;
|
---|
[51b73452] | 458 |
|
---|
| 459 | multiplicative_expression:
|
---|
[4d51835] | 460 | cast_expression
|
---|
| 461 | | multiplicative_expression '*' cast_expression
|
---|
| 462 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mul),$1,$3); }
|
---|
| 463 | | multiplicative_expression '/' cast_expression
|
---|
| 464 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Div),$1,$3); }
|
---|
| 465 | | multiplicative_expression '%' cast_expression
|
---|
| 466 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mod),$1,$3); }
|
---|
| 467 | ;
|
---|
[51b73452] | 468 |
|
---|
| 469 | additive_expression:
|
---|
[4d51835] | 470 | multiplicative_expression
|
---|
| 471 | | additive_expression '+' multiplicative_expression
|
---|
| 472 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Plus),$1,$3); }
|
---|
| 473 | | additive_expression '-' multiplicative_expression
|
---|
| 474 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Minus),$1,$3); }
|
---|
| 475 | ;
|
---|
[51b73452] | 476 |
|
---|
| 477 | shift_expression:
|
---|
[4d51835] | 478 | additive_expression
|
---|
| 479 | | shift_expression LS additive_expression
|
---|
| 480 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LShift),$1,$3); }
|
---|
| 481 | | shift_expression RS additive_expression
|
---|
| 482 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::RShift),$1,$3); }
|
---|
| 483 | ;
|
---|
[51b73452] | 484 |
|
---|
| 485 | relational_expression:
|
---|
[4d51835] | 486 | shift_expression
|
---|
| 487 | | relational_expression '<' shift_expression
|
---|
| 488 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LThan),$1,$3); }
|
---|
| 489 | | relational_expression '>' shift_expression
|
---|
| 490 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::GThan),$1,$3); }
|
---|
| 491 | | relational_expression LE shift_expression
|
---|
| 492 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LEThan),$1,$3); }
|
---|
| 493 | | relational_expression GE shift_expression
|
---|
| 494 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::GEThan),$1,$3); }
|
---|
| 495 | ;
|
---|
[51b73452] | 496 |
|
---|
| 497 | equality_expression:
|
---|
[4d51835] | 498 | relational_expression
|
---|
| 499 | | equality_expression EQ relational_expression
|
---|
| 500 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Eq), $1, $3); }
|
---|
| 501 | | equality_expression NE relational_expression
|
---|
| 502 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Neq), $1, $3); }
|
---|
| 503 | ;
|
---|
[51b73452] | 504 |
|
---|
| 505 | AND_expression:
|
---|
[4d51835] | 506 | equality_expression
|
---|
| 507 | | AND_expression '&' equality_expression
|
---|
| 508 | { $$ =new CompositeExprNode(new OperatorNode(OperatorNode::BitAnd), $1, $3); }
|
---|
| 509 | ;
|
---|
[51b73452] | 510 |
|
---|
| 511 | exclusive_OR_expression:
|
---|
[4d51835] | 512 | AND_expression
|
---|
| 513 | | exclusive_OR_expression '^' AND_expression
|
---|
| 514 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Xor), $1, $3); }
|
---|
| 515 | ;
|
---|
[51b73452] | 516 |
|
---|
| 517 | inclusive_OR_expression:
|
---|
[4d51835] | 518 | exclusive_OR_expression
|
---|
| 519 | | inclusive_OR_expression '|' exclusive_OR_expression
|
---|
| 520 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::BitOr), $1, $3); }
|
---|
| 521 | ;
|
---|
[51b73452] | 522 |
|
---|
| 523 | logical_AND_expression:
|
---|
[4d51835] | 524 | inclusive_OR_expression
|
---|
| 525 | | logical_AND_expression ANDAND inclusive_OR_expression
|
---|
| 526 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::And), $1, $3); }
|
---|
| 527 | ;
|
---|
[51b73452] | 528 |
|
---|
| 529 | logical_OR_expression:
|
---|
[4d51835] | 530 | logical_AND_expression
|
---|
| 531 | | logical_OR_expression OROR logical_AND_expression
|
---|
| 532 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Or), $1, $3); }
|
---|
| 533 | ;
|
---|
[51b73452] | 534 |
|
---|
| 535 | conditional_expression:
|
---|
[4d51835] | 536 | logical_OR_expression
|
---|
| 537 | | logical_OR_expression '?' comma_expression ':' conditional_expression
|
---|
[59db689] | 538 | { $$ = new CompositeExprNode( new OperatorNode(OperatorNode::Cond), (ExpressionNode *)mkList( (*$1, *$3, *$5) ) ); }
|
---|
[4d51835] | 539 | | logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, omitted first operand
|
---|
| 540 | { $$=new CompositeExprNode(new OperatorNode(OperatorNode::NCond),$1,$4); }
|
---|
| 541 | | logical_OR_expression '?' comma_expression ':' tuple // CFA, tuple expression
|
---|
[59db689] | 542 | { $$ = new CompositeExprNode( new OperatorNode(OperatorNode::Cond), (ExpressionNode *)mkList( (*$1, *$3, *$5) ) ); }
|
---|
[4d51835] | 543 | ;
|
---|
[51b73452] | 544 |
|
---|
| 545 | constant_expression:
|
---|
[4d51835] | 546 | conditional_expression
|
---|
| 547 | ;
|
---|
[51b73452] | 548 |
|
---|
| 549 | assignment_expression:
|
---|
[4d51835] | 550 | // CFA, assignment is separated from assignment_operator to ensure no assignment operations for tuples
|
---|
| 551 | conditional_expression
|
---|
| 552 | | unary_expression '=' assignment_expression
|
---|
| 553 | { $$ =new CompositeExprNode(new OperatorNode(OperatorNode::Assign), $1, $3); }
|
---|
| 554 | | unary_expression assignment_operator assignment_expression
|
---|
| 555 | { $$ =new CompositeExprNode($2, $1, $3); }
|
---|
| 556 | | tuple assignment_opt // CFA, tuple expression
|
---|
| 557 | { $$ = ($2 == 0) ? $1 : new CompositeExprNode( new OperatorNode( OperatorNode::Assign ), $1, $2 ); }
|
---|
| 558 | ;
|
---|
[51b73452] | 559 |
|
---|
| 560 | assignment_expression_opt:
|
---|
[4d51835] | 561 | // empty
|
---|
| 562 | { $$ = new NullExprNode; }
|
---|
| 563 | | assignment_expression
|
---|
| 564 | ;
|
---|
[b87a5ed] | 565 |
|
---|
| 566 | tuple: // CFA, tuple
|
---|
[4d51835] | 567 | // CFA, one assignment_expression is factored out of comma_expression to eliminate a shift/reduce
|
---|
| 568 | // conflict with comma_expression in new_identifier_parameter_array and new_abstract_array
|
---|
| 569 | '[' push pop ']'
|
---|
| 570 | { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ) ); }
|
---|
| 571 | | '[' push assignment_expression pop ']'
|
---|
| 572 | { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ), $3 ); }
|
---|
| 573 | | '[' push ',' tuple_expression_list pop ']'
|
---|
| 574 | { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ), (ExpressionNode *)(new NullExprNode)->set_link( $4 ) ); }
|
---|
| 575 | | '[' push assignment_expression ',' tuple_expression_list pop ']'
|
---|
| 576 | { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ), (ExpressionNode *)$3->set_link( flattenCommas( $5 ) ) ); }
|
---|
| 577 | ;
|
---|
[51b73452] | 578 |
|
---|
| 579 | tuple_expression_list:
|
---|
[4d51835] | 580 | assignment_expression_opt
|
---|
| 581 | | tuple_expression_list ',' assignment_expression_opt
|
---|
| 582 | { $$ = (ExpressionNode *)$1->set_link( $3 ); }
|
---|
| 583 | ;
|
---|
[51b73452] | 584 |
|
---|
| 585 | assignment_operator:
|
---|
[4d51835] | 586 | MULTassign { $$ = new OperatorNode(OperatorNode::MulAssn); }
|
---|
| 587 | | DIVassign { $$ = new OperatorNode(OperatorNode::DivAssn); }
|
---|
| 588 | | MODassign { $$ = new OperatorNode(OperatorNode::ModAssn); }
|
---|
| 589 | | PLUSassign { $$ = new OperatorNode(OperatorNode::PlusAssn); }
|
---|
| 590 | | MINUSassign { $$ = new OperatorNode(OperatorNode::MinusAssn); }
|
---|
| 591 | | LSassign { $$ = new OperatorNode(OperatorNode::LSAssn); }
|
---|
| 592 | | RSassign { $$ = new OperatorNode(OperatorNode::RSAssn); }
|
---|
| 593 | | ANDassign { $$ = new OperatorNode(OperatorNode::AndAssn); }
|
---|
| 594 | | ERassign { $$ = new OperatorNode(OperatorNode::ERAssn); }
|
---|
| 595 | | ORassign { $$ = new OperatorNode(OperatorNode::OrAssn); }
|
---|
| 596 | ;
|
---|
[51b73452] | 597 |
|
---|
| 598 | comma_expression:
|
---|
[4d51835] | 599 | assignment_expression
|
---|
| 600 | | comma_expression ',' assignment_expression // { $$ = (ExpressionNode *)$1->add_to_list($3); }
|
---|
| 601 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Comma),$1,$3); }
|
---|
| 602 | ;
|
---|
[51b73452] | 603 |
|
---|
| 604 | comma_expression_opt:
|
---|
[4d51835] | 605 | // empty
|
---|
| 606 | { $$ = 0; }
|
---|
| 607 | | comma_expression
|
---|
| 608 | ;
|
---|
[51b73452] | 609 |
|
---|
[c11e31c] | 610 | //*************************** STATEMENTS *******************************
|
---|
[51b73452] | 611 |
|
---|
| 612 | statement:
|
---|
[4d51835] | 613 | labeled_statement
|
---|
| 614 | | compound_statement
|
---|
| 615 | | expression_statement { $$ = $1; }
|
---|
| 616 | | selection_statement
|
---|
| 617 | | iteration_statement
|
---|
| 618 | | jump_statement
|
---|
| 619 | | exception_statement
|
---|
| 620 | | asm_statement
|
---|
| 621 | ;
|
---|
[51b73452] | 622 |
|
---|
| 623 | labeled_statement:
|
---|
[4d51835] | 624 | no_attr_identifier ':' attribute_list_opt statement
|
---|
| 625 | { $$ = $4->add_label($1);}
|
---|
| 626 | ;
|
---|
[51b73452] | 627 |
|
---|
| 628 | compound_statement:
|
---|
[4d51835] | 629 | '{' '}'
|
---|
| 630 | { $$ = new CompoundStmtNode( (StatementNode *)0 ); }
|
---|
| 631 | | '{'
|
---|
| 632 | // Two scopes are necessary because the block itself has a scope, but every declaration within the
|
---|
| 633 | // block also requires its own scope
|
---|
| 634 | push push
|
---|
| 635 | label_declaration_opt // GCC, local labels
|
---|
| 636 | block_item_list pop '}' // C99, intermix declarations and statements
|
---|
| 637 | { $$ = new CompoundStmtNode( $5 ); }
|
---|
| 638 | ;
|
---|
[b87a5ed] | 639 |
|
---|
| 640 | block_item_list: // C99
|
---|
[4d51835] | 641 | block_item
|
---|
| 642 | | block_item_list push block_item
|
---|
| 643 | { if ($1 != 0) { $1->set_link($3); $$ = $1; } }
|
---|
| 644 | ;
|
---|
[51b73452] | 645 |
|
---|
| 646 | block_item:
|
---|
[4d51835] | 647 | declaration // CFA, new & old style declarations
|
---|
| 648 | { $$ = new StatementNode( $1 ); }
|
---|
| 649 | | EXTENSION declaration // GCC
|
---|
| 650 | { $$ = new StatementNode( $2 ); }
|
---|
| 651 | | function_definition
|
---|
| 652 | { $$ = new StatementNode( $1 ); }
|
---|
| 653 | | statement pop
|
---|
| 654 | ;
|
---|
[51b73452] | 655 |
|
---|
| 656 | statement_list:
|
---|
[4d51835] | 657 | statement
|
---|
| 658 | | statement_list statement
|
---|
| 659 | { if ($1 != 0) { $1->set_link($2); $$ = $1; } }
|
---|
| 660 | ;
|
---|
[51b73452] | 661 |
|
---|
| 662 | expression_statement:
|
---|
[4d51835] | 663 | comma_expression_opt ';'
|
---|
| 664 | { $$ = new StatementNode(StatementNode::Exp, $1, 0); }
|
---|
| 665 | ;
|
---|
[51b73452] | 666 |
|
---|
| 667 | selection_statement:
|
---|
[4d51835] | 668 | IF '(' comma_expression ')' statement %prec THEN
|
---|
| 669 | // explicitly deal with the shift/reduce conflict on if/else
|
---|
| 670 | { $$ = new StatementNode(StatementNode::If, $3, $5); }
|
---|
| 671 | | IF '(' comma_expression ')' statement ELSE statement
|
---|
| 672 | { $$ = new StatementNode(StatementNode::If, $3, (StatementNode *)mkList((*$5, *$7)) ); }
|
---|
| 673 | | SWITCH '(' comma_expression ')' case_clause // CFA
|
---|
| 674 | { $$ = new StatementNode(StatementNode::Switch, $3, $5); }
|
---|
| 675 | | SWITCH '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt '}' // CFA
|
---|
| 676 | { $$ = new StatementNode(StatementNode::Switch, $3, $8); /* xxx */ }
|
---|
| 677 | // The semantics of the declaration list is changed to include any associated initialization, which is
|
---|
| 678 | // performed *before* the transfer to the appropriate case clause. Statements after the initial
|
---|
| 679 | // declaration list can never be executed, and therefore, are removed from the grammar even though C
|
---|
| 680 | // allows it.
|
---|
| 681 | | CHOOSE '(' comma_expression ')' case_clause // CFA
|
---|
| 682 | { $$ = new StatementNode(StatementNode::Choose, $3, $5); }
|
---|
| 683 | | CHOOSE '(' comma_expression ')' '{' push declaration_list_opt choose_clause_list_opt '}' // CFA
|
---|
| 684 | { $$ = new StatementNode(StatementNode::Choose, $3, $8); }
|
---|
| 685 | ;
|
---|
[b87a5ed] | 686 |
|
---|
| 687 | // CASE and DEFAULT clauses are only allowed in the SWITCH statement, precluding Duff's device. In addition, a
|
---|
| 688 | // case clause allows a list of values and subranges.
|
---|
| 689 |
|
---|
| 690 | case_value: // CFA
|
---|
[4d51835] | 691 | constant_expression { $$ = $1; }
|
---|
| 692 | | constant_expression ELLIPSIS constant_expression // GCC, subrange
|
---|
| 693 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Range),$1,$3); }
|
---|
| 694 | | subrange // CFA, subrange
|
---|
| 695 | ;
|
---|
[b87a5ed] | 696 |
|
---|
| 697 | case_value_list: // CFA
|
---|
[4d51835] | 698 | case_value
|
---|
| 699 | | case_value_list ',' case_value
|
---|
| 700 | { $$ = new CompositeExprNode(new OperatorNode( OperatorNode::TupleC ), (ExpressionNode *)(tupleContents($1))->set_link($3) ); }
|
---|
| 701 | ;
|
---|
[b87a5ed] | 702 |
|
---|
| 703 | case_label: // CFA
|
---|
[4d51835] | 704 | CASE case_value_list ':' { $$ = new StatementNode(StatementNode::Case, $2, 0); }
|
---|
| 705 | | DEFAULT ':' { $$ = new StatementNode(StatementNode::Default); }
|
---|
| 706 | // A semantic check is required to ensure only one default clause per switch/choose statement.
|
---|
| 707 | ;
|
---|
[b87a5ed] | 708 |
|
---|
| 709 | case_label_list: // CFA
|
---|
[4d51835] | 710 | case_label
|
---|
| 711 | | case_label_list case_label { $$ = (StatementNode *)($1->set_link($2)); }
|
---|
| 712 | ;
|
---|
[b87a5ed] | 713 |
|
---|
| 714 | case_clause: // CFA
|
---|
[4d51835] | 715 | case_label_list statement { $$ = $1->append_last_case($2); }
|
---|
| 716 | ;
|
---|
[b87a5ed] | 717 |
|
---|
| 718 | switch_clause_list_opt: // CFA
|
---|
[4d51835] | 719 | // empty
|
---|
| 720 | { $$ = 0; }
|
---|
| 721 | | switch_clause_list
|
---|
| 722 | ;
|
---|
[b87a5ed] | 723 |
|
---|
| 724 | switch_clause_list: // CFA
|
---|
[4d51835] | 725 | case_label_list statement_list
|
---|
| 726 | { $$ = $1->append_last_case($2); }
|
---|
| 727 | | switch_clause_list case_label_list statement_list
|
---|
| 728 | { $$ = (StatementNode *)($1->set_link($2->append_last_case($3))); }
|
---|
| 729 | ;
|
---|
[b87a5ed] | 730 |
|
---|
| 731 | choose_clause_list_opt: // CFA
|
---|
[4d51835] | 732 | // empty
|
---|
| 733 | { $$ = 0; }
|
---|
| 734 | | choose_clause_list
|
---|
| 735 | ;
|
---|
[b87a5ed] | 736 |
|
---|
| 737 | choose_clause_list: // CFA
|
---|
[4d51835] | 738 | case_label_list fall_through
|
---|
| 739 | { $$ = $1->append_last_case($2); }
|
---|
| 740 | | case_label_list statement_list fall_through_opt
|
---|
| 741 | { $$ = $1->append_last_case((StatementNode *)mkList((*$2,*$3))); }
|
---|
| 742 | | choose_clause_list case_label_list fall_through
|
---|
| 743 | { $$ = (StatementNode *)($1->set_link($2->append_last_case($3))); }
|
---|
| 744 | | choose_clause_list case_label_list statement_list fall_through_opt
|
---|
| 745 | { $$ = (StatementNode *)($1->set_link($2->append_last_case((StatementNode *)mkList((*$3,*$4))))); }
|
---|
| 746 | ;
|
---|
[b87a5ed] | 747 |
|
---|
| 748 | fall_through_opt: // CFA
|
---|
[4d51835] | 749 | // empty
|
---|
| 750 | { $$ = 0; }
|
---|
| 751 | | fall_through
|
---|
| 752 | ;
|
---|
[b87a5ed] | 753 |
|
---|
| 754 | fall_through: // CFA
|
---|
[4d51835] | 755 | FALLTHRU { $$ = new StatementNode(StatementNode::Fallthru, 0, 0); }
|
---|
| 756 | | FALLTHRU ';' { $$ = new StatementNode(StatementNode::Fallthru, 0, 0); }
|
---|
| 757 | ;
|
---|
[51b73452] | 758 |
|
---|
| 759 | iteration_statement:
|
---|
[4d51835] | 760 | WHILE '(' comma_expression ')' statement
|
---|
| 761 | { $$ = new StatementNode(StatementNode::While, $3, $5); }
|
---|
| 762 | | DO statement WHILE '(' comma_expression ')' ';'
|
---|
| 763 | { $$ = new StatementNode(StatementNode::Do, $5, $2); }
|
---|
| 764 | | FOR '(' push for_control_expression ')' statement
|
---|
| 765 | { $$ = new StatementNode(StatementNode::For, $4, $6); }
|
---|
| 766 | ;
|
---|
[51b73452] | 767 |
|
---|
| 768 | for_control_expression:
|
---|
[4d51835] | 769 | comma_expression_opt pop ';' comma_expression_opt ';' comma_expression_opt
|
---|
| 770 | { $$ = new ForCtlExprNode($1, $4, $6); }
|
---|
| 771 | | declaration comma_expression_opt ';' comma_expression_opt // C99
|
---|
| 772 | { $$ = new ForCtlExprNode($1, $2, $4); }
|
---|
| 773 | ;
|
---|
[51b73452] | 774 |
|
---|
| 775 | jump_statement:
|
---|
[4d51835] | 776 | GOTO no_attr_identifier ';'
|
---|
| 777 | { $$ = new StatementNode(StatementNode::Goto, $2); }
|
---|
| 778 | | GOTO '*' comma_expression ';' // GCC, computed goto
|
---|
| 779 | // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; =>
|
---|
| 780 | // goto *(i+3); whereas normal operator precedence yields goto (*i)+3;
|
---|
| 781 | { $$ = new StatementNode(StatementNode::Goto, $3); }
|
---|
| 782 | | CONTINUE ';'
|
---|
| 783 | // A semantic check is required to ensure this statement appears only in the body of an iteration
|
---|
| 784 | // statement.
|
---|
| 785 | { $$ = new StatementNode(StatementNode::Continue, 0, 0); }
|
---|
| 786 | | CONTINUE no_attr_identifier ';' // CFA, multi-level continue
|
---|
| 787 | // A semantic check is required to ensure this statement appears only in the body of an iteration
|
---|
| 788 | // statement, and the target of the transfer appears only at the start of an iteration statement.
|
---|
| 789 | { $$ = new StatementNode(StatementNode::Continue, $2); }
|
---|
| 790 | | BREAK ';'
|
---|
| 791 | // A semantic check is required to ensure this statement appears only in the body of an iteration
|
---|
| 792 | // statement.
|
---|
| 793 | { $$ = new StatementNode(StatementNode::Break, 0, 0); }
|
---|
| 794 | | BREAK no_attr_identifier ';' // CFA, multi-level exit
|
---|
| 795 | // A semantic check is required to ensure this statement appears only in the body of an iteration
|
---|
| 796 | // statement, and the target of the transfer appears only at the start of an iteration statement.
|
---|
| 797 | { $$ = new StatementNode(StatementNode::Break, $2 ); }
|
---|
| 798 | | RETURN comma_expression_opt ';'
|
---|
| 799 | { $$ = new StatementNode(StatementNode::Return, $2, 0); }
|
---|
| 800 | | THROW assignment_expression ';'
|
---|
| 801 | { $$ = new StatementNode(StatementNode::Throw, $2, 0); }
|
---|
| 802 | | THROW ';'
|
---|
| 803 | { $$ = new StatementNode(StatementNode::Throw, 0, 0); }
|
---|
| 804 | ;
|
---|
[51b73452] | 805 |
|
---|
| 806 | exception_statement:
|
---|
[4d51835] | 807 | TRY compound_statement handler_list
|
---|
| 808 | { $$ = new StatementNode(StatementNode::Try, 0,(StatementNode *)(mkList((*$2,*$3)))); }
|
---|
| 809 | | TRY compound_statement finally_clause
|
---|
| 810 | { $$ = new StatementNode(StatementNode::Try, 0,(StatementNode *)(mkList((*$2,*$3)))); }
|
---|
| 811 | | TRY compound_statement handler_list finally_clause
|
---|
| 812 | {
|
---|
| 813 | $3->set_link($4);
|
---|
| 814 | $$ = new StatementNode(StatementNode::Try, 0,(StatementNode *)(mkList((*$2,*$3))));
|
---|
| 815 | }
|
---|
| 816 | ;
|
---|
[51b73452] | 817 |
|
---|
| 818 | handler_list:
|
---|
[4d51835] | 819 | // There must be at least one catch clause
|
---|
| 820 | handler_clause
|
---|
| 821 | // ISO/IEC 9899:1999 Section 15.3(6) If present, a "..." handler shall be the last handler for its try
|
---|
| 822 | // block.
|
---|
| 823 | | CATCH '(' ELLIPSIS ')' compound_statement
|
---|
| 824 | { $$ = StatementNode::newCatchStmt( 0, $5, true ); }
|
---|
| 825 | | handler_clause CATCH '(' ELLIPSIS ')' compound_statement
|
---|
| 826 | { $$ = $1->set_link( StatementNode::newCatchStmt( 0, $6, true ) ); }
|
---|
| 827 | ;
|
---|
[51b73452] | 828 |
|
---|
| 829 | handler_clause:
|
---|
[4d51835] | 830 | CATCH '(' push push exception_declaration pop ')' compound_statement pop
|
---|
| 831 | { $$ = StatementNode::newCatchStmt($5, $8); }
|
---|
| 832 | | handler_clause CATCH '(' push push exception_declaration pop ')' compound_statement pop
|
---|
| 833 | { $$ = $1->set_link( StatementNode::newCatchStmt($6, $9) ); }
|
---|
| 834 | ;
|
---|
[51b73452] | 835 |
|
---|
| 836 | finally_clause:
|
---|
[4d51835] | 837 | FINALLY compound_statement
|
---|
| 838 | {
|
---|
| 839 | $$ = new StatementNode(StatementNode::Finally, 0, $2);
|
---|
| 840 | std::cout << "Just created a finally node" << std::endl;
|
---|
| 841 | }
|
---|
| 842 | ;
|
---|
[51b73452] | 843 |
|
---|
| 844 | exception_declaration:
|
---|
[4d51835] | 845 | // A semantic check is required to ensure type_specifier does not create a new type, e.g.:
|
---|
| 846 | //
|
---|
| 847 | // catch ( struct { int i; } x ) ...
|
---|
| 848 | //
|
---|
| 849 | // This new type cannot catch any thrown type because of name equivalence among types.
|
---|
| 850 | type_specifier
|
---|
| 851 | | type_specifier declarator
|
---|
| 852 | {
|
---|
| 853 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 854 | $$ = $2->addType( $1 );
|
---|
| 855 | }
|
---|
| 856 | | type_specifier variable_abstract_declarator
|
---|
| 857 | { $$ = $2->addType( $1 ); }
|
---|
| 858 | | new_abstract_declarator_tuple no_attr_identifier // CFA
|
---|
| 859 | {
|
---|
| 860 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 861 | $$ = $1->addName( $2 );
|
---|
| 862 | }
|
---|
| 863 | | new_abstract_declarator_tuple // CFA
|
---|
| 864 | ;
|
---|
[51b73452] | 865 |
|
---|
| 866 | asm_statement:
|
---|
[4d51835] | 867 | ASM type_qualifier_list_opt '(' constant_expression ')' ';'
|
---|
| 868 | { $$ = new StatementNode(StatementNode::Asm, 0, 0); }
|
---|
| 869 | | ASM type_qualifier_list_opt '(' constant_expression ':' asm_operands_opt ')' ';' // remaining GCC
|
---|
| 870 | { $$ = new StatementNode(StatementNode::Asm, 0, 0); }
|
---|
| 871 | | ASM type_qualifier_list_opt '(' constant_expression ':' asm_operands_opt ':' asm_operands_opt ')' ';'
|
---|
| 872 | { $$ = new StatementNode(StatementNode::Asm, 0, 0); }
|
---|
| 873 | | ASM type_qualifier_list_opt '(' constant_expression ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list ')' ';'
|
---|
| 874 | { $$ = new StatementNode(StatementNode::Asm, 0, 0); }
|
---|
| 875 | ;
|
---|
[b87a5ed] | 876 |
|
---|
| 877 | asm_operands_opt: // GCC
|
---|
[4d51835] | 878 | // empty
|
---|
| 879 | | asm_operands_list
|
---|
| 880 | ;
|
---|
[b87a5ed] | 881 |
|
---|
| 882 | asm_operands_list: // GCC
|
---|
[4d51835] | 883 | asm_operand
|
---|
| 884 | | asm_operands_list ',' asm_operand
|
---|
| 885 | ;
|
---|
[b87a5ed] | 886 |
|
---|
| 887 | asm_operand: // GCC
|
---|
[4d51835] | 888 | STRINGliteral '(' constant_expression ')' {}
|
---|
| 889 | ;
|
---|
[b87a5ed] | 890 |
|
---|
| 891 | asm_clobbers_list: // GCC
|
---|
[4d51835] | 892 | STRINGliteral {}
|
---|
| 893 | | asm_clobbers_list ',' STRINGliteral
|
---|
| 894 | ;
|
---|
[51b73452] | 895 |
|
---|
[c11e31c] | 896 | //******************************* DECLARATIONS *********************************
|
---|
[51b73452] | 897 |
|
---|
[b87a5ed] | 898 | declaration_list_opt: // used at beginning of switch statement
|
---|
[4d51835] | 899 | pop
|
---|
| 900 | { $$ = 0; }
|
---|
| 901 | | declaration_list
|
---|
| 902 | ;
|
---|
[51b73452] | 903 |
|
---|
| 904 | declaration_list:
|
---|
[4d51835] | 905 | declaration
|
---|
| 906 | | declaration_list push declaration
|
---|
| 907 | { $$ = $1->appendList( $3 ); }
|
---|
| 908 | ;
|
---|
[51b73452] | 909 |
|
---|
[b87a5ed] | 910 | old_declaration_list_opt: // used to declare parameter types in K&R style functions
|
---|
[4d51835] | 911 | pop
|
---|
| 912 | { $$ = 0; }
|
---|
| 913 | | old_declaration_list
|
---|
| 914 | ;
|
---|
[51b73452] | 915 |
|
---|
| 916 | old_declaration_list:
|
---|
[4d51835] | 917 | old_declaration
|
---|
| 918 | | old_declaration_list push old_declaration
|
---|
| 919 | { $$ = $1->appendList( $3 ); }
|
---|
| 920 | ;
|
---|
[b87a5ed] | 921 |
|
---|
| 922 | label_declaration_opt: // GCC, local label
|
---|
[4d51835] | 923 | // empty
|
---|
| 924 | | label_declaration_list
|
---|
| 925 | ;
|
---|
[b87a5ed] | 926 |
|
---|
| 927 | label_declaration_list: // GCC, local label
|
---|
[4d51835] | 928 | LABEL label_list ';'
|
---|
| 929 | | label_declaration_list LABEL label_list ';'
|
---|
| 930 | ;
|
---|
[b87a5ed] | 931 |
|
---|
| 932 | label_list: // GCC, local label
|
---|
[4d51835] | 933 | no_attr_identifier_or_typedef_name {}
|
---|
| 934 | | label_list ',' no_attr_identifier_or_typedef_name {}
|
---|
| 935 | ;
|
---|
[b87a5ed] | 936 |
|
---|
| 937 | declaration: // CFA, new & old style declarations
|
---|
[4d51835] | 938 | new_declaration
|
---|
| 939 | | old_declaration
|
---|
| 940 | ;
|
---|
[b87a5ed] | 941 |
|
---|
| 942 | // C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and
|
---|
| 943 | // function declarations. CFA declarations use the same declaration tokens as in C; however, CFA places
|
---|
| 944 | // declaration modifiers to the left of the base type, while C declarations place modifiers to the right of
|
---|
| 945 | // the base type. CFA declaration modifiers are interpreted from left to right and the entire type
|
---|
| 946 | // specification is distributed across all variables in the declaration list (as in Pascal). ANSI C and the
|
---|
| 947 | // new CFA declarations may appear together in the same program block, but cannot be mixed within a specific
|
---|
| 948 | // declaration.
|
---|
[c11e31c] | 949 | //
|
---|
[b87a5ed] | 950 | // CFA C
|
---|
| 951 | // [10] int x; int x[10]; // array of 10 integers
|
---|
| 952 | // [10] * char y; char *y[10]; // array of 10 pointers to char
|
---|
| 953 |
|
---|
| 954 | new_declaration: // CFA
|
---|
[4d51835] | 955 | new_variable_declaration pop ';'
|
---|
| 956 | | new_typedef_declaration pop ';'
|
---|
| 957 | | new_function_declaration pop ';'
|
---|
| 958 | | type_declaring_list pop ';'
|
---|
| 959 | | context_specifier pop ';'
|
---|
| 960 | ;
|
---|
[b87a5ed] | 961 |
|
---|
| 962 | new_variable_declaration: // CFA
|
---|
[4d51835] | 963 | new_variable_specifier initializer_opt
|
---|
| 964 | {
|
---|
| 965 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 966 | $$ = $1;
|
---|
| 967 | }
|
---|
| 968 | | declaration_qualifier_list new_variable_specifier initializer_opt
|
---|
| 969 | // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to
|
---|
| 970 | // preclude them as a type_qualifier cannot appear in that context.
|
---|
| 971 | {
|
---|
| 972 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 973 | $$ = $2->addQualifiers( $1 );
|
---|
| 974 | }
|
---|
| 975 | | new_variable_declaration pop ',' push identifier_or_typedef_name initializer_opt
|
---|
| 976 | {
|
---|
| 977 | typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
|
---|
| 978 | $$ = $1->appendList( $1->cloneType( $5 ) );
|
---|
| 979 | }
|
---|
| 980 | ;
|
---|
[b87a5ed] | 981 |
|
---|
| 982 | new_variable_specifier: // CFA
|
---|
[4d51835] | 983 | // A semantic check is required to ensure asm_name only appears on declarations with implicit or
|
---|
| 984 | // explicit static storage-class
|
---|
| 985 | new_abstract_declarator_no_tuple identifier_or_typedef_name asm_name_opt
|
---|
| 986 | {
|
---|
| 987 | typedefTable.setNextIdentifier( *$2 );
|
---|
| 988 | $$ = $1->addName( $2 );
|
---|
| 989 | }
|
---|
| 990 | | new_abstract_tuple identifier_or_typedef_name asm_name_opt
|
---|
| 991 | {
|
---|
| 992 | typedefTable.setNextIdentifier( *$2 );
|
---|
| 993 | $$ = $1->addName( $2 );
|
---|
| 994 | }
|
---|
| 995 | | type_qualifier_list new_abstract_tuple identifier_or_typedef_name asm_name_opt
|
---|
| 996 | {
|
---|
| 997 | typedefTable.setNextIdentifier( *$3 );
|
---|
| 998 | $$ = $2->addQualifiers( $1 )->addName( $3 );
|
---|
| 999 | }
|
---|
| 1000 | ;
|
---|
[b87a5ed] | 1001 |
|
---|
| 1002 | new_function_declaration: // CFA
|
---|
[4d51835] | 1003 | new_function_specifier
|
---|
| 1004 | {
|
---|
| 1005 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1006 | $$ = $1;
|
---|
| 1007 | }
|
---|
| 1008 | | type_qualifier_list new_function_specifier
|
---|
| 1009 | {
|
---|
| 1010 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1011 | $$ = $2->addQualifiers( $1 );
|
---|
| 1012 | }
|
---|
| 1013 | | declaration_qualifier_list new_function_specifier
|
---|
| 1014 | {
|
---|
| 1015 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1016 | $$ = $2->addQualifiers( $1 );
|
---|
| 1017 | }
|
---|
| 1018 | | declaration_qualifier_list type_qualifier_list new_function_specifier
|
---|
| 1019 | {
|
---|
| 1020 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1021 | $$ = $3->addQualifiers( $1 )->addQualifiers( $2 );
|
---|
| 1022 | }
|
---|
| 1023 | | new_function_declaration pop ',' push identifier_or_typedef_name
|
---|
| 1024 | {
|
---|
| 1025 | typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
|
---|
| 1026 | $$ = $1->appendList( $1->cloneType( $5 ) );
|
---|
| 1027 | }
|
---|
| 1028 | ;
|
---|
[b87a5ed] | 1029 |
|
---|
| 1030 | new_function_specifier: // CFA
|
---|
[4d51835] | 1031 | '[' push pop ']' identifier '(' push new_parameter_type_list_opt pop ')'
|
---|
| 1032 | {
|
---|
| 1033 | typedefTable.setNextIdentifier( *($5) );
|
---|
| 1034 | $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
|
---|
| 1035 | }
|
---|
| 1036 | | '[' push pop ']' TYPEDEFname '(' push new_parameter_type_list_opt pop ')'
|
---|
| 1037 | {
|
---|
| 1038 | typedefTable.setNextIdentifier( *($5) );
|
---|
| 1039 | $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
|
---|
| 1040 | }
|
---|
| 1041 | // identifier_or_typedef_name must be broken apart because of the sequence:
|
---|
| 1042 | //
|
---|
| 1043 | // '[' ']' identifier_or_typedef_name '(' new_parameter_type_list_opt ')'
|
---|
| 1044 | // '[' ']' type_specifier
|
---|
| 1045 | //
|
---|
| 1046 | // type_specifier can resolve to just TYPEDEFname (e.g. typedef int T; int f( T );). Therefore this
|
---|
| 1047 | // must be flattened to allow lookahead to the '(' without having to reduce
|
---|
| 1048 | // identifier_or_typedef_name.
|
---|
| 1049 | | new_abstract_tuple identifier_or_typedef_name '(' push new_parameter_type_list_opt pop ')'
|
---|
| 1050 | // To obtain LR(1), this rule must be factored out from function return type (see
|
---|
| 1051 | // new_abstract_declarator).
|
---|
| 1052 | {
|
---|
| 1053 | $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
|
---|
| 1054 | }
|
---|
| 1055 | | new_function_return identifier_or_typedef_name '(' push new_parameter_type_list_opt pop ')'
|
---|
| 1056 | {
|
---|
| 1057 | $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
|
---|
| 1058 | }
|
---|
| 1059 | ;
|
---|
[b87a5ed] | 1060 |
|
---|
| 1061 | new_function_return: // CFA
|
---|
[4d51835] | 1062 | '[' push new_parameter_list pop ']'
|
---|
| 1063 | { $$ = DeclarationNode::newTuple( $3 ); }
|
---|
| 1064 | | '[' push new_parameter_list pop ',' push new_abstract_parameter_list pop ']'
|
---|
| 1065 | // To obtain LR(1), the last new_abstract_parameter_list is added into this flattened rule to
|
---|
| 1066 | // lookahead to the ']'.
|
---|
| 1067 | { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
|
---|
| 1068 | ;
|
---|
[b87a5ed] | 1069 |
|
---|
| 1070 | new_typedef_declaration: // CFA
|
---|
[4d51835] | 1071 | TYPEDEF new_variable_specifier
|
---|
| 1072 | {
|
---|
| 1073 | typedefTable.addToEnclosingScope( TypedefTable::TD);
|
---|
| 1074 | $$ = $2->addTypedef();
|
---|
| 1075 | }
|
---|
| 1076 | | TYPEDEF new_function_specifier
|
---|
| 1077 | {
|
---|
| 1078 | typedefTable.addToEnclosingScope( TypedefTable::TD);
|
---|
| 1079 | $$ = $2->addTypedef();
|
---|
| 1080 | }
|
---|
| 1081 | | new_typedef_declaration pop ',' push no_attr_identifier
|
---|
| 1082 | {
|
---|
| 1083 | typedefTable.addToEnclosingScope( *$5, TypedefTable::TD);
|
---|
| 1084 | $$ = $1->appendList( $1->cloneType( $5 ) );
|
---|
| 1085 | }
|
---|
| 1086 | ;
|
---|
[b87a5ed] | 1087 |
|
---|
| 1088 | // Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is
|
---|
| 1089 | // factored out as a separate form of declaration, which syntactically precludes storage-class specifiers and
|
---|
| 1090 | // initialization.
|
---|
[51b73452] | 1091 |
|
---|
| 1092 | typedef_declaration:
|
---|
[4d51835] | 1093 | TYPEDEF type_specifier declarator
|
---|
| 1094 | {
|
---|
| 1095 | typedefTable.addToEnclosingScope( TypedefTable::TD);
|
---|
| 1096 | $$ = $3->addType( $2 )->addTypedef();
|
---|
| 1097 | }
|
---|
| 1098 | | typedef_declaration pop ',' push declarator
|
---|
| 1099 | {
|
---|
| 1100 | typedefTable.addToEnclosingScope( TypedefTable::TD);
|
---|
| 1101 | $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
|
---|
| 1102 | }
|
---|
| 1103 | | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2)
|
---|
| 1104 | {
|
---|
| 1105 | typedefTable.addToEnclosingScope( TypedefTable::TD);
|
---|
| 1106 | $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
|
---|
| 1107 | }
|
---|
| 1108 | | type_specifier TYPEDEF declarator
|
---|
| 1109 | {
|
---|
| 1110 | typedefTable.addToEnclosingScope( TypedefTable::TD);
|
---|
| 1111 | $$ = $3->addType( $1 )->addTypedef();
|
---|
| 1112 | }
|
---|
| 1113 | | type_specifier TYPEDEF type_qualifier_list declarator
|
---|
| 1114 | {
|
---|
| 1115 | typedefTable.addToEnclosingScope( TypedefTable::TD);
|
---|
| 1116 | $$ = $4->addQualifiers($1)->addTypedef()->addType($1);
|
---|
| 1117 | }
|
---|
| 1118 | ;
|
---|
[b87a5ed] | 1119 |
|
---|
| 1120 | typedef_expression: // GCC, naming expression type
|
---|
[4d51835] | 1121 | TYPEDEF no_attr_identifier '=' assignment_expression
|
---|
| 1122 | {
|
---|
| 1123 | typedefTable.addToEnclosingScope(*($2), TypedefTable::TD);
|
---|
| 1124 | $$ = DeclarationNode::newName( 0 ); // XXX
|
---|
| 1125 | }
|
---|
| 1126 | | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
|
---|
| 1127 | {
|
---|
| 1128 | typedefTable.addToEnclosingScope(*($5), TypedefTable::TD);
|
---|
| 1129 | $$ = DeclarationNode::newName( 0 ); // XXX
|
---|
| 1130 | }
|
---|
| 1131 | ;
|
---|
[51b73452] | 1132 |
|
---|
| 1133 | old_declaration:
|
---|
[4d51835] | 1134 | declaring_list pop ';'
|
---|
| 1135 | | typedef_declaration pop ';'
|
---|
| 1136 | | typedef_expression pop ';' // GCC, naming expression type
|
---|
| 1137 | | sue_declaration_specifier pop ';'
|
---|
| 1138 | ;
|
---|
[51b73452] | 1139 |
|
---|
| 1140 | declaring_list:
|
---|
[4d51835] | 1141 | // A semantic check is required to ensure asm_name only appears on declarations with implicit or
|
---|
| 1142 | // explicit static storage-class
|
---|
| 1143 | declaration_specifier declarator asm_name_opt initializer_opt
|
---|
| 1144 | {
|
---|
| 1145 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1146 | $$ = ($2->addType( $1 ))->addInitializer($4);
|
---|
| 1147 | }
|
---|
| 1148 | | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
|
---|
| 1149 | {
|
---|
| 1150 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1151 | $$ = $1->appendList( $1->cloneBaseType( $4->addInitializer($6) ) );
|
---|
| 1152 | }
|
---|
| 1153 | ;
|
---|
[b87a5ed] | 1154 |
|
---|
| 1155 | declaration_specifier: // type specifier + storage class
|
---|
[4d51835] | 1156 | basic_declaration_specifier
|
---|
| 1157 | | sue_declaration_specifier
|
---|
| 1158 | | typedef_declaration_specifier
|
---|
| 1159 | | typegen_declaration_specifier
|
---|
| 1160 | ;
|
---|
[b87a5ed] | 1161 |
|
---|
| 1162 | type_specifier: // declaration specifier - storage class
|
---|
[4d51835] | 1163 | basic_type_specifier
|
---|
| 1164 | | sue_type_specifier
|
---|
| 1165 | | typedef_type_specifier
|
---|
| 1166 | | typegen_type_specifier
|
---|
| 1167 | ;
|
---|
[b87a5ed] | 1168 |
|
---|
| 1169 | type_qualifier_list_opt: // GCC, used in asm_statement
|
---|
[4d51835] | 1170 | // empty
|
---|
| 1171 | { $$ = 0; }
|
---|
| 1172 | | type_qualifier_list
|
---|
| 1173 | ;
|
---|
[51b73452] | 1174 |
|
---|
| 1175 | type_qualifier_list:
|
---|
[4d51835] | 1176 | // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of
|
---|
| 1177 | // declaration.
|
---|
| 1178 | //
|
---|
| 1179 | // ISO/IEC 9899:1999 Section 6.7.3(4) : If the same qualifier appears more than once in the same
|
---|
| 1180 | // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as
|
---|
| 1181 | // if it appeared only once.
|
---|
| 1182 | type_qualifier
|
---|
| 1183 | | type_qualifier_list type_qualifier
|
---|
| 1184 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1185 | ;
|
---|
[51b73452] | 1186 |
|
---|
| 1187 | type_qualifier:
|
---|
[4d51835] | 1188 | type_qualifier_name
|
---|
| 1189 | | attribute
|
---|
| 1190 | { $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
|
---|
| 1191 | ;
|
---|
[51b73452] | 1192 |
|
---|
| 1193 | type_qualifier_name:
|
---|
[4d51835] | 1194 | CONST
|
---|
| 1195 | { $$ = DeclarationNode::newQualifier( DeclarationNode::Const ); }
|
---|
| 1196 | | RESTRICT
|
---|
| 1197 | { $$ = DeclarationNode::newQualifier( DeclarationNode::Restrict ); }
|
---|
| 1198 | | VOLATILE
|
---|
| 1199 | { $$ = DeclarationNode::newQualifier( DeclarationNode::Volatile ); }
|
---|
| 1200 | | LVALUE // CFA
|
---|
| 1201 | { $$ = DeclarationNode::newQualifier( DeclarationNode::Lvalue ); }
|
---|
| 1202 | | ATOMIC
|
---|
| 1203 | { $$ = DeclarationNode::newQualifier( DeclarationNode::Atomic ); }
|
---|
| 1204 | | FORALL '('
|
---|
| 1205 | {
|
---|
| 1206 | typedefTable.enterScope();
|
---|
| 1207 | }
|
---|
| 1208 | type_parameter_list ')' // CFA
|
---|
| 1209 | {
|
---|
| 1210 | typedefTable.leaveScope();
|
---|
| 1211 | $$ = DeclarationNode::newForall( $4 );
|
---|
| 1212 | }
|
---|
| 1213 | ;
|
---|
[51b73452] | 1214 |
|
---|
| 1215 | declaration_qualifier_list:
|
---|
[4d51835] | 1216 | storage_class_list
|
---|
| 1217 | | type_qualifier_list storage_class_list // remaining OBSOLESCENT (see 2)
|
---|
| 1218 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1219 | | declaration_qualifier_list type_qualifier_list storage_class_list
|
---|
| 1220 | { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
|
---|
| 1221 | ;
|
---|
[51b73452] | 1222 |
|
---|
| 1223 | storage_class_list:
|
---|
[4d51835] | 1224 | // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration
|
---|
| 1225 | // and that only one of each is specified, except for inline, which can appear with the others.
|
---|
| 1226 | //
|
---|
| 1227 | // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the
|
---|
| 1228 | // declaration specifiers in a declaration.
|
---|
| 1229 | storage_class
|
---|
| 1230 | | storage_class_list storage_class
|
---|
| 1231 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1232 | ;
|
---|
[51b73452] | 1233 |
|
---|
| 1234 | storage_class:
|
---|
[4d51835] | 1235 | storage_class_name
|
---|
| 1236 | ;
|
---|
[51b73452] | 1237 |
|
---|
| 1238 | storage_class_name:
|
---|
[4d51835] | 1239 | EXTERN
|
---|
| 1240 | { $$ = DeclarationNode::newStorageClass( DeclarationNode::Extern ); }
|
---|
| 1241 | | STATIC
|
---|
| 1242 | { $$ = DeclarationNode::newStorageClass( DeclarationNode::Static ); }
|
---|
| 1243 | | AUTO
|
---|
| 1244 | { $$ = DeclarationNode::newStorageClass( DeclarationNode::Auto ); }
|
---|
| 1245 | | REGISTER
|
---|
| 1246 | { $$ = DeclarationNode::newStorageClass( DeclarationNode::Register ); }
|
---|
| 1247 | | INLINE // C99
|
---|
| 1248 | // INLINE is essentially a storage class specifier for functions, and hence, belongs here.
|
---|
| 1249 | { $$ = DeclarationNode::newStorageClass( DeclarationNode::Inline ); }
|
---|
| 1250 | | FORTRAN // C99
|
---|
| 1251 | { $$ = DeclarationNode::newStorageClass( DeclarationNode::Fortran ); }
|
---|
| 1252 | ;
|
---|
[51b73452] | 1253 |
|
---|
| 1254 | basic_type_name:
|
---|
[4d51835] | 1255 | CHAR
|
---|
| 1256 | { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
|
---|
| 1257 | | DOUBLE
|
---|
| 1258 | { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
|
---|
| 1259 | | FLOAT
|
---|
| 1260 | { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
|
---|
| 1261 | | INT
|
---|
| 1262 | { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
|
---|
| 1263 | | LONG
|
---|
| 1264 | { $$ = DeclarationNode::newModifier( DeclarationNode::Long ); }
|
---|
| 1265 | | SHORT
|
---|
| 1266 | { $$ = DeclarationNode::newModifier( DeclarationNode::Short ); }
|
---|
| 1267 | | SIGNED
|
---|
| 1268 | { $$ = DeclarationNode::newModifier( DeclarationNode::Signed ); }
|
---|
| 1269 | | UNSIGNED
|
---|
| 1270 | { $$ = DeclarationNode::newModifier( DeclarationNode::Unsigned ); }
|
---|
| 1271 | | VOID
|
---|
| 1272 | { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
|
---|
| 1273 | | BOOL // C99
|
---|
| 1274 | { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
|
---|
| 1275 | | COMPLEX // C99
|
---|
| 1276 | { $$ = DeclarationNode::newBasicType( DeclarationNode::Complex ); }
|
---|
| 1277 | | IMAGINARY // C99
|
---|
| 1278 | { $$ = DeclarationNode::newBasicType( DeclarationNode::Imaginary ); }
|
---|
| 1279 | ;
|
---|
[51b73452] | 1280 |
|
---|
| 1281 | basic_declaration_specifier:
|
---|
[4d51835] | 1282 | // A semantic check is necessary for conflicting storage classes.
|
---|
| 1283 | basic_type_specifier
|
---|
| 1284 | | declaration_qualifier_list basic_type_specifier
|
---|
| 1285 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 1286 | | basic_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
|
---|
| 1287 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1288 | | basic_declaration_specifier storage_class type_qualifier_list
|
---|
| 1289 | { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
|
---|
| 1290 | | basic_declaration_specifier storage_class basic_type_specifier
|
---|
| 1291 | { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
|
---|
| 1292 | ;
|
---|
[51b73452] | 1293 |
|
---|
| 1294 | basic_type_specifier:
|
---|
[4d51835] | 1295 | direct_type_name
|
---|
| 1296 | | type_qualifier_list_opt indirect_type_name type_qualifier_list_opt
|
---|
| 1297 | { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
|
---|
| 1298 | ;
|
---|
[51b73452] | 1299 |
|
---|
| 1300 | direct_type_name:
|
---|
[4d51835] | 1301 | // A semantic check is necessary for conflicting type qualifiers.
|
---|
| 1302 | basic_type_name
|
---|
| 1303 | | type_qualifier_list basic_type_name
|
---|
| 1304 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 1305 | | direct_type_name type_qualifier
|
---|
| 1306 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1307 | | direct_type_name basic_type_name
|
---|
| 1308 | { $$ = $1->addType( $2 ); }
|
---|
| 1309 | ;
|
---|
[51b73452] | 1310 |
|
---|
| 1311 | indirect_type_name:
|
---|
[4d51835] | 1312 | TYPEOF '(' type_name ')' // GCC: typeof(x) y;
|
---|
| 1313 | { $$ = $3; }
|
---|
| 1314 | | TYPEOF '(' comma_expression ')' // GCC: typeof(a+b) y;
|
---|
| 1315 | { $$ = DeclarationNode::newTypeof( $3 ); }
|
---|
| 1316 | | ATTR_TYPEGENname '(' type_name ')' // CFA: e.g., @type(x) y;
|
---|
| 1317 | { $$ = DeclarationNode::newAttr( $1, $3 ); }
|
---|
| 1318 | | ATTR_TYPEGENname '(' comma_expression ')' // CFA: e.g., @type(a+b) y;
|
---|
| 1319 | { $$ = DeclarationNode::newAttr( $1, $3 ); }
|
---|
| 1320 | ;
|
---|
[51b73452] | 1321 |
|
---|
| 1322 | sue_declaration_specifier:
|
---|
[4d51835] | 1323 | sue_type_specifier
|
---|
| 1324 | | declaration_qualifier_list sue_type_specifier
|
---|
| 1325 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 1326 | | sue_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
|
---|
| 1327 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1328 | | sue_declaration_specifier storage_class type_qualifier_list
|
---|
| 1329 | { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
|
---|
| 1330 | ;
|
---|
[51b73452] | 1331 |
|
---|
| 1332 | sue_type_specifier:
|
---|
[4d51835] | 1333 | elaborated_type_name // struct, union, enum
|
---|
| 1334 | | type_qualifier_list elaborated_type_name
|
---|
| 1335 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 1336 | | sue_type_specifier type_qualifier
|
---|
| 1337 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1338 | ;
|
---|
[51b73452] | 1339 |
|
---|
| 1340 | typedef_declaration_specifier:
|
---|
[4d51835] | 1341 | typedef_type_specifier
|
---|
| 1342 | | declaration_qualifier_list typedef_type_specifier
|
---|
| 1343 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 1344 | | typedef_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
|
---|
| 1345 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1346 | | typedef_declaration_specifier storage_class type_qualifier_list
|
---|
| 1347 | { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
|
---|
| 1348 | ;
|
---|
[b87a5ed] | 1349 |
|
---|
| 1350 | typedef_type_specifier: // typedef types
|
---|
[4d51835] | 1351 | TYPEDEFname
|
---|
| 1352 | { $$ = DeclarationNode::newFromTypedef( $1 ); }
|
---|
| 1353 | | type_qualifier_list TYPEDEFname
|
---|
| 1354 | { $$ = DeclarationNode::newFromTypedef( $2 )->addQualifiers( $1 ); }
|
---|
| 1355 | | typedef_type_specifier type_qualifier
|
---|
| 1356 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1357 | ;
|
---|
[51b73452] | 1358 |
|
---|
| 1359 | elaborated_type_name:
|
---|
[4d51835] | 1360 | aggregate_name
|
---|
| 1361 | | enum_name
|
---|
| 1362 | ;
|
---|
[51b73452] | 1363 |
|
---|
| 1364 | aggregate_name:
|
---|
[4d51835] | 1365 | aggregate_key '{' field_declaration_list '}'
|
---|
| 1366 | { $$ = DeclarationNode::newAggregate( $1, 0, 0, 0, $3 ); }
|
---|
| 1367 | | aggregate_key no_attr_identifier_or_typedef_name
|
---|
| 1368 | { $$ = DeclarationNode::newAggregate( $1, $2, 0, 0, 0 ); }
|
---|
| 1369 | | aggregate_key no_attr_identifier_or_typedef_name '{' field_declaration_list '}'
|
---|
| 1370 | { $$ = DeclarationNode::newAggregate( $1, $2, 0, 0, $4 ); }
|
---|
| 1371 | | aggregate_key '(' push type_parameter_list pop ')' '{' field_declaration_list '}' // CFA
|
---|
| 1372 | { $$ = DeclarationNode::newAggregate( $1, 0, $4, 0, $8 ); }
|
---|
| 1373 | | aggregate_key '(' push type_parameter_list pop ')' no_attr_identifier_or_typedef_name // CFA
|
---|
| 1374 | { $$ = DeclarationNode::newAggregate( $1, $7, $4, 0, 0 ); }
|
---|
| 1375 | | aggregate_key '(' push type_parameter_list pop ')' no_attr_identifier_or_typedef_name '{' field_declaration_list '}' // CFA
|
---|
| 1376 | { $$ = DeclarationNode::newAggregate( $1, $7, $4, 0, $9 ); }
|
---|
| 1377 | | aggregate_key '(' push type_parameter_list pop ')' '(' type_name_list ')' '{' field_declaration_list '}' // CFA
|
---|
| 1378 | { $$ = DeclarationNode::newAggregate( $1, 0, $4, $8, $11 ); }
|
---|
| 1379 | | aggregate_key '(' push type_name_list pop ')' no_attr_identifier_or_typedef_name // CFA
|
---|
| 1380 | // push and pop are only to prevent S/R conflicts
|
---|
| 1381 | { $$ = DeclarationNode::newAggregate( $1, $7, 0, $4, 0 ); }
|
---|
| 1382 | | aggregate_key '(' push type_parameter_list pop ')' '(' type_name_list ')' no_attr_identifier_or_typedef_name '{' field_declaration_list '}' // CFA
|
---|
| 1383 | { $$ = DeclarationNode::newAggregate( $1, $10, $4, $8, $12 ); }
|
---|
| 1384 | ;
|
---|
[51b73452] | 1385 |
|
---|
| 1386 | aggregate_key:
|
---|
[4d51835] | 1387 | STRUCT attribute_list_opt
|
---|
| 1388 | { $$ = DeclarationNode::Struct; }
|
---|
| 1389 | | UNION attribute_list_opt
|
---|
| 1390 | { $$ = DeclarationNode::Union; }
|
---|
| 1391 | ;
|
---|
[51b73452] | 1392 |
|
---|
| 1393 | field_declaration_list:
|
---|
[4d51835] | 1394 | field_declaration
|
---|
| 1395 | { $$ = $1; }
|
---|
| 1396 | | field_declaration_list field_declaration
|
---|
| 1397 | { $$ = $1->appendList( $2 ); }
|
---|
| 1398 | ;
|
---|
[51b73452] | 1399 |
|
---|
| 1400 | field_declaration:
|
---|
[4d51835] | 1401 | new_field_declaring_list ';' // CFA, new style field declaration
|
---|
| 1402 | | EXTENSION new_field_declaring_list ';' // GCC
|
---|
| 1403 | { $$ = $2; }
|
---|
| 1404 | | field_declaring_list ';'
|
---|
| 1405 | | EXTENSION field_declaring_list ';' // GCC
|
---|
| 1406 | { $$ = $2; }
|
---|
| 1407 | ;
|
---|
[b87a5ed] | 1408 |
|
---|
| 1409 | new_field_declaring_list: // CFA, new style field declaration
|
---|
[4d51835] | 1410 | new_abstract_declarator_tuple // CFA, no field name
|
---|
| 1411 | | new_abstract_declarator_tuple no_attr_identifier_or_typedef_name
|
---|
| 1412 | { $$ = $1->addName( $2 ); }
|
---|
| 1413 | | new_field_declaring_list ',' no_attr_identifier_or_typedef_name
|
---|
| 1414 | { $$ = $1->appendList( $1->cloneType( $3 ) ); }
|
---|
| 1415 | | new_field_declaring_list ',' // CFA, no field name
|
---|
| 1416 | { $$ = $1->appendList( $1->cloneType( 0 ) ); }
|
---|
| 1417 | ;
|
---|
[51b73452] | 1418 |
|
---|
| 1419 | field_declaring_list:
|
---|
[4d51835] | 1420 | type_specifier field_declarator
|
---|
| 1421 | { $$ = $2->addType( $1 ); }
|
---|
| 1422 | | field_declaring_list ',' attribute_list_opt field_declarator
|
---|
| 1423 | { $$ = $1->appendList( $1->cloneBaseType( $4 ) ); }
|
---|
| 1424 | ;
|
---|
[51b73452] | 1425 |
|
---|
| 1426 | field_declarator:
|
---|
[4d51835] | 1427 | // empty
|
---|
| 1428 | { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
|
---|
| 1429 | | bit_subrange_size // no field name
|
---|
| 1430 | { $$ = DeclarationNode::newBitfield( $1 ); }
|
---|
| 1431 | | variable_declarator bit_subrange_size_opt
|
---|
| 1432 | // A semantic check is required to ensure bit_subrange only appears on base type int.
|
---|
| 1433 | { $$ = $1->addBitfield( $2 ); }
|
---|
| 1434 | | typedef_redeclarator bit_subrange_size_opt
|
---|
| 1435 | // A semantic check is required to ensure bit_subrange only appears on base type int.
|
---|
| 1436 | { $$ = $1->addBitfield( $2 ); }
|
---|
| 1437 | | variable_abstract_declarator // CFA, no field name
|
---|
| 1438 | ;
|
---|
[51b73452] | 1439 |
|
---|
| 1440 | bit_subrange_size_opt:
|
---|
[4d51835] | 1441 | // empty
|
---|
| 1442 | { $$ = 0; }
|
---|
| 1443 | | bit_subrange_size
|
---|
| 1444 | { $$ = $1; }
|
---|
| 1445 | ;
|
---|
[51b73452] | 1446 |
|
---|
| 1447 | bit_subrange_size:
|
---|
[4d51835] | 1448 | ':' constant_expression
|
---|
| 1449 | { $$ = $2; }
|
---|
| 1450 | ;
|
---|
[51b73452] | 1451 |
|
---|
| 1452 | enum_key:
|
---|
[4d51835] | 1453 | ENUM attribute_list_opt
|
---|
| 1454 | ;
|
---|
[51b73452] | 1455 |
|
---|
| 1456 | enum_name:
|
---|
[4d51835] | 1457 | enum_key '{' enumerator_list comma_opt '}'
|
---|
| 1458 | { $$ = DeclarationNode::newEnum( 0, $3 ); }
|
---|
| 1459 | | enum_key no_attr_identifier_or_typedef_name '{' enumerator_list comma_opt '}'
|
---|
| 1460 | { $$ = DeclarationNode::newEnum( $2, $4 ); }
|
---|
| 1461 | | enum_key no_attr_identifier_or_typedef_name
|
---|
| 1462 | { $$ = DeclarationNode::newEnum( $2, 0 ); }
|
---|
| 1463 | ;
|
---|
[51b73452] | 1464 |
|
---|
| 1465 | enumerator_list:
|
---|
[4d51835] | 1466 | no_attr_identifier_or_typedef_name enumerator_value_opt
|
---|
| 1467 | { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
|
---|
| 1468 | | enumerator_list ',' no_attr_identifier_or_typedef_name enumerator_value_opt
|
---|
| 1469 | { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
|
---|
| 1470 | ;
|
---|
[51b73452] | 1471 |
|
---|
| 1472 | enumerator_value_opt:
|
---|
[4d51835] | 1473 | // empty
|
---|
| 1474 | { $$ = 0; }
|
---|
| 1475 | | '=' constant_expression
|
---|
| 1476 | { $$ = $2; }
|
---|
| 1477 | ;
|
---|
[51b73452] | 1478 |
|
---|
[c11e31c] | 1479 | // Minimum of one parameter after which ellipsis is allowed only at the end.
|
---|
[51b73452] | 1480 |
|
---|
[b87a5ed] | 1481 | new_parameter_type_list_opt: // CFA
|
---|
[4d51835] | 1482 | // empty
|
---|
| 1483 | { $$ = 0; }
|
---|
| 1484 | | new_parameter_type_list
|
---|
| 1485 | ;
|
---|
[b87a5ed] | 1486 |
|
---|
| 1487 | new_parameter_type_list: // CFA, abstract + real
|
---|
[4d51835] | 1488 | new_abstract_parameter_list
|
---|
| 1489 | | new_parameter_list
|
---|
| 1490 | | new_parameter_list pop ',' push new_abstract_parameter_list
|
---|
| 1491 | { $$ = $1->appendList( $5 ); }
|
---|
| 1492 | | new_abstract_parameter_list pop ',' push ELLIPSIS
|
---|
| 1493 | { $$ = $1->addVarArgs(); }
|
---|
| 1494 | | new_parameter_list pop ',' push ELLIPSIS
|
---|
| 1495 | { $$ = $1->addVarArgs(); }
|
---|
| 1496 | ;
|
---|
[b87a5ed] | 1497 |
|
---|
| 1498 | new_parameter_list: // CFA
|
---|
[4d51835] | 1499 | // To obtain LR(1) between new_parameter_list and new_abstract_tuple, the last
|
---|
| 1500 | // new_abstract_parameter_list is factored out from new_parameter_list, flattening the rules to get
|
---|
| 1501 | // lookahead to the ']'.
|
---|
| 1502 | new_parameter_declaration
|
---|
| 1503 | | new_abstract_parameter_list pop ',' push new_parameter_declaration
|
---|
| 1504 | { $$ = $1->appendList( $5 ); }
|
---|
| 1505 | | new_parameter_list pop ',' push new_parameter_declaration
|
---|
| 1506 | { $$ = $1->appendList( $5 ); }
|
---|
| 1507 | | new_parameter_list pop ',' push new_abstract_parameter_list pop ',' push new_parameter_declaration
|
---|
| 1508 | { $$ = $1->appendList( $5 )->appendList( $9 ); }
|
---|
| 1509 | ;
|
---|
[b87a5ed] | 1510 |
|
---|
| 1511 | new_abstract_parameter_list: // CFA, new & old style abstract
|
---|
[4d51835] | 1512 | new_abstract_parameter_declaration
|
---|
| 1513 | | new_abstract_parameter_list pop ',' push new_abstract_parameter_declaration
|
---|
| 1514 | { $$ = $1->appendList( $5 ); }
|
---|
| 1515 | ;
|
---|
[51b73452] | 1516 |
|
---|
| 1517 | parameter_type_list_opt:
|
---|
[4d51835] | 1518 | // empty
|
---|
| 1519 | { $$ = 0; }
|
---|
| 1520 | | parameter_type_list
|
---|
| 1521 | ;
|
---|
[51b73452] | 1522 |
|
---|
| 1523 | parameter_type_list:
|
---|
[4d51835] | 1524 | parameter_list
|
---|
| 1525 | | parameter_list pop ',' push ELLIPSIS
|
---|
| 1526 | { $$ = $1->addVarArgs(); }
|
---|
| 1527 | ;
|
---|
[b87a5ed] | 1528 |
|
---|
| 1529 | parameter_list: // abstract + real
|
---|
[4d51835] | 1530 | abstract_parameter_declaration
|
---|
| 1531 | | parameter_declaration
|
---|
| 1532 | | parameter_list pop ',' push abstract_parameter_declaration
|
---|
| 1533 | { $$ = $1->appendList( $5 ); }
|
---|
| 1534 | | parameter_list pop ',' push parameter_declaration
|
---|
| 1535 | { $$ = $1->appendList( $5 ); }
|
---|
| 1536 | ;
|
---|
[51b73452] | 1537 |
|
---|
[c11e31c] | 1538 | // Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different
|
---|
| 1539 | // semantics for typedef name by using typedef_parameter_redeclarator instead of typedef_redeclarator, and
|
---|
| 1540 | // function prototypes.
|
---|
[51b73452] | 1541 |
|
---|
[b87a5ed] | 1542 | new_parameter_declaration: // CFA, new & old style parameter declaration
|
---|
[4d51835] | 1543 | parameter_declaration
|
---|
| 1544 | | new_identifier_parameter_declarator_no_tuple identifier_or_typedef_name assignment_opt
|
---|
| 1545 | { $$ = $1->addName( $2 ); }
|
---|
| 1546 | | new_abstract_tuple identifier_or_typedef_name assignment_opt
|
---|
| 1547 | // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
|
---|
| 1548 | { $$ = $1->addName( $2 ); }
|
---|
| 1549 | | type_qualifier_list new_abstract_tuple identifier_or_typedef_name assignment_opt
|
---|
| 1550 | { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
|
---|
| 1551 | | new_function_specifier
|
---|
| 1552 | ;
|
---|
[b87a5ed] | 1553 |
|
---|
| 1554 | new_abstract_parameter_declaration: // CFA, new & old style parameter declaration
|
---|
[4d51835] | 1555 | abstract_parameter_declaration
|
---|
| 1556 | | new_identifier_parameter_declarator_no_tuple
|
---|
| 1557 | | new_abstract_tuple
|
---|
| 1558 | // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
|
---|
| 1559 | | type_qualifier_list new_abstract_tuple
|
---|
| 1560 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 1561 | | new_abstract_function
|
---|
| 1562 | ;
|
---|
[51b73452] | 1563 |
|
---|
| 1564 | parameter_declaration:
|
---|
[4d51835] | 1565 | declaration_specifier identifier_parameter_declarator assignment_opt
|
---|
| 1566 | {
|
---|
| 1567 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1568 | $$ = $2->addType( $1 )->addInitializer( new InitializerNode($3) );
|
---|
| 1569 | }
|
---|
| 1570 | | declaration_specifier typedef_parameter_redeclarator assignment_opt
|
---|
| 1571 | {
|
---|
| 1572 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1573 | $$ = $2->addType( $1 )->addInitializer( new InitializerNode($3) );
|
---|
| 1574 | }
|
---|
| 1575 | ;
|
---|
[51b73452] | 1576 |
|
---|
| 1577 | abstract_parameter_declaration:
|
---|
[4d51835] | 1578 | declaration_specifier
|
---|
| 1579 | | declaration_specifier abstract_parameter_declarator
|
---|
| 1580 | { $$ = $2->addType( $1 ); }
|
---|
| 1581 | ;
|
---|
[51b73452] | 1582 |
|
---|
[c11e31c] | 1583 | // ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
|
---|
| 1584 | // parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is
|
---|
| 1585 | // based only on identifiers. The ANSI-style parameter-list can redefine a typedef name.
|
---|
[51b73452] | 1586 |
|
---|
[b87a5ed] | 1587 | identifier_list: // K&R-style parameter list => no types
|
---|
[4d51835] | 1588 | no_attr_identifier
|
---|
| 1589 | { $$ = DeclarationNode::newName( $1 ); }
|
---|
| 1590 | | identifier_list ',' no_attr_identifier
|
---|
| 1591 | { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
|
---|
| 1592 | ;
|
---|
[51b73452] | 1593 |
|
---|
| 1594 | identifier_or_typedef_name:
|
---|
[4d51835] | 1595 | identifier
|
---|
| 1596 | | TYPEDEFname
|
---|
| 1597 | | TYPEGENname
|
---|
| 1598 | ;
|
---|
[51b73452] | 1599 |
|
---|
| 1600 | no_01_identifier_or_typedef_name:
|
---|
[4d51835] | 1601 | no_01_identifier
|
---|
| 1602 | | TYPEDEFname
|
---|
| 1603 | | TYPEGENname
|
---|
| 1604 | ;
|
---|
[51b73452] | 1605 |
|
---|
| 1606 | no_attr_identifier_or_typedef_name:
|
---|
[4d51835] | 1607 | no_attr_identifier
|
---|
| 1608 | | TYPEDEFname
|
---|
| 1609 | | TYPEGENname
|
---|
| 1610 | ;
|
---|
[b87a5ed] | 1611 |
|
---|
| 1612 | type_name_no_function: // sizeof, alignof, cast (constructor)
|
---|
[4d51835] | 1613 | new_abstract_declarator_tuple // CFA
|
---|
| 1614 | | type_specifier
|
---|
| 1615 | | type_specifier variable_abstract_declarator
|
---|
| 1616 | { $$ = $2->addType( $1 ); }
|
---|
| 1617 | ;
|
---|
[b87a5ed] | 1618 |
|
---|
| 1619 | type_name: // typeof, assertion
|
---|
[4d51835] | 1620 | new_abstract_declarator_tuple // CFA
|
---|
| 1621 | | new_abstract_function // CFA
|
---|
| 1622 | | type_specifier
|
---|
| 1623 | | type_specifier abstract_declarator
|
---|
| 1624 | { $$ = $2->addType( $1 ); }
|
---|
| 1625 | ;
|
---|
[51b73452] | 1626 |
|
---|
| 1627 | initializer_opt:
|
---|
[4d51835] | 1628 | // empty
|
---|
| 1629 | { $$ = 0; }
|
---|
| 1630 | | '=' initializer { $$ = $2; }
|
---|
| 1631 | ;
|
---|
[51b73452] | 1632 |
|
---|
| 1633 | initializer:
|
---|
[4d51835] | 1634 | assignment_expression { $$ = new InitializerNode($1); }
|
---|
| 1635 | | '{' initializer_list comma_opt '}' { $$ = new InitializerNode($2, true); }
|
---|
| 1636 | ;
|
---|
[51b73452] | 1637 |
|
---|
| 1638 | initializer_list:
|
---|
[4d51835] | 1639 | initializer
|
---|
| 1640 | | designation initializer { $$ = $2->set_designators( $1 ); }
|
---|
| 1641 | | initializer_list ',' initializer { $$ = (InitializerNode *)( $1->set_link($3) ); }
|
---|
| 1642 | | initializer_list ',' designation initializer
|
---|
| 1643 | { $$ = (InitializerNode *)( $1->set_link( $4->set_designators($3) ) ); }
|
---|
| 1644 | ;
|
---|
[b87a5ed] | 1645 |
|
---|
| 1646 | // There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is
|
---|
| 1647 | // use of '=' to separator the designator from the initializer value, as in:
|
---|
[c11e31c] | 1648 | //
|
---|
[b87a5ed] | 1649 | // int x[10] = { [1] = 3 };
|
---|
[c11e31c] | 1650 | //
|
---|
| 1651 | // The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this
|
---|
| 1652 | // case, CFA changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC
|
---|
| 1653 | // does uses ":" for field selection. The optional use of the "=" in GCC, or in this case ":", cannot be
|
---|
| 1654 | // supported either due to shift/reduce conflicts
|
---|
[51b73452] | 1655 |
|
---|
| 1656 | designation:
|
---|
[4d51835] | 1657 | designator_list ':' // C99, CFA uses ":" instead of "="
|
---|
| 1658 | | no_attr_identifier_or_typedef_name ':' // GCC, field name
|
---|
| 1659 | { $$ = new VarRefNode( $1 ); }
|
---|
| 1660 | ;
|
---|
[51b73452] | 1661 |
|
---|
[b87a5ed] | 1662 | designator_list: // C99
|
---|
[4d51835] | 1663 | designator
|
---|
| 1664 | | designator_list designator { $$ = (ExpressionNode *)($1->set_link( $2 )); }
|
---|
| 1665 | ;
|
---|
[51b73452] | 1666 |
|
---|
| 1667 | designator:
|
---|
[4d51835] | 1668 | '.' no_attr_identifier_or_typedef_name // C99, field name
|
---|
| 1669 | { $$ = new VarRefNode( $2 ); }
|
---|
| 1670 | | '[' push assignment_expression pop ']' // C99, single array element
|
---|
| 1671 | // assignment_expression used instead of constant_expression because of shift/reduce conflicts with
|
---|
| 1672 | // tuple.
|
---|
| 1673 | { $$ = $3; }
|
---|
| 1674 | | '[' push subrange pop ']' // CFA, multiple array elements
|
---|
| 1675 | { $$ = $3; }
|
---|
| 1676 | | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
|
---|
| 1677 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Range), $3, $5); }
|
---|
| 1678 | | '.' '[' push field_list pop ']' // CFA, tuple field selector
|
---|
| 1679 | { $$ = $4; }
|
---|
| 1680 | ;
|
---|
[51b73452] | 1681 |
|
---|
[c11e31c] | 1682 | // The CFA type system is based on parametric polymorphism, the ability to declare functions with type
|
---|
| 1683 | // parameters, rather than an object-oriented type system. This required four groups of extensions:
|
---|
| 1684 | //
|
---|
| 1685 | // Overloading: function, data, and operator identifiers may be overloaded.
|
---|
| 1686 | //
|
---|
| 1687 | // Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used
|
---|
| 1688 | // for object and incomplete types, and "ftype" is used for function types. Type declarations with
|
---|
| 1689 | // initializers provide definitions of new types. Type declarations with storage class "extern" provide
|
---|
| 1690 | // opaque types.
|
---|
| 1691 | //
|
---|
| 1692 | // Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at
|
---|
| 1693 | // the call site. A polymorphic function is not a template; it is a function, with an address and a type.
|
---|
| 1694 | //
|
---|
| 1695 | // Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
|
---|
| 1696 | // types. They serve many of the purposes of abstract classes, and specification hierarchies resemble
|
---|
| 1697 | // subclass hierarchies. Unlike classes, they can define relationships between types. Assertions declare
|
---|
| 1698 | // that a type or types provide the operations declared by a specification. Assertions are normally used
|
---|
| 1699 | // to declare requirements on type arguments of polymorphic functions.
|
---|
| 1700 |
|
---|
[b87a5ed] | 1701 | typegen_declaration_specifier: // CFA
|
---|
[4d51835] | 1702 | typegen_type_specifier
|
---|
| 1703 | | declaration_qualifier_list typegen_type_specifier
|
---|
| 1704 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 1705 | | typegen_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
|
---|
| 1706 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1707 | | typegen_declaration_specifier storage_class type_qualifier_list
|
---|
| 1708 | { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
|
---|
| 1709 | ;
|
---|
[b87a5ed] | 1710 |
|
---|
| 1711 | typegen_type_specifier: // CFA
|
---|
[4d51835] | 1712 | TYPEGENname '(' type_name_list ')'
|
---|
| 1713 | { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
|
---|
| 1714 | | type_qualifier_list TYPEGENname '(' type_name_list ')'
|
---|
| 1715 | { $$ = DeclarationNode::newFromTypeGen( $2, $4 )->addQualifiers( $1 ); }
|
---|
| 1716 | | typegen_type_specifier type_qualifier
|
---|
| 1717 | { $$ = $1->addQualifiers( $2 ); }
|
---|
| 1718 | ;
|
---|
[b87a5ed] | 1719 |
|
---|
| 1720 | type_parameter_list: // CFA
|
---|
[4d51835] | 1721 | type_parameter assignment_opt
|
---|
| 1722 | | type_parameter_list ',' type_parameter assignment_opt
|
---|
| 1723 | { $$ = $1->appendList( $3 ); }
|
---|
| 1724 | ;
|
---|
[b87a5ed] | 1725 |
|
---|
| 1726 | type_parameter: // CFA
|
---|
[4d51835] | 1727 | type_class no_attr_identifier_or_typedef_name
|
---|
| 1728 | { typedefTable.addToEnclosingScope(*($2), TypedefTable::TD); }
|
---|
| 1729 | assertion_list_opt
|
---|
| 1730 | { $$ = DeclarationNode::newTypeParam( $1, $2 )->addAssertions( $4 ); }
|
---|
| 1731 | | type_specifier identifier_parameter_declarator
|
---|
| 1732 | ;
|
---|
[b87a5ed] | 1733 |
|
---|
| 1734 | type_class: // CFA
|
---|
[4d51835] | 1735 | TYPE
|
---|
| 1736 | { $$ = DeclarationNode::Type; }
|
---|
| 1737 | | DTYPE
|
---|
| 1738 | { $$ = DeclarationNode::Ftype; }
|
---|
| 1739 | | FTYPE
|
---|
| 1740 | { $$ = DeclarationNode::Dtype; }
|
---|
| 1741 | ;
|
---|
[b87a5ed] | 1742 |
|
---|
| 1743 | assertion_list_opt: // CFA
|
---|
[4d51835] | 1744 | // empty
|
---|
| 1745 | { $$ = 0; }
|
---|
| 1746 | | assertion_list_opt assertion
|
---|
| 1747 | { $$ = $1 == 0 ? $2 : $1->appendList( $2 ); }
|
---|
| 1748 | ;
|
---|
[b87a5ed] | 1749 |
|
---|
| 1750 | assertion: // CFA
|
---|
[4d51835] | 1751 | '|' no_attr_identifier_or_typedef_name '(' type_name_list ')'
|
---|
| 1752 | {
|
---|
| 1753 | typedefTable.openContext( *($2) );
|
---|
| 1754 | $$ = DeclarationNode::newContextUse( $2, $4 );
|
---|
| 1755 | }
|
---|
| 1756 | | '|' '{' push context_declaration_list '}'
|
---|
| 1757 | { $$ = $4; }
|
---|
| 1758 | | '|' '(' push type_parameter_list pop ')' '{' push context_declaration_list '}' '(' type_name_list ')'
|
---|
| 1759 | { $$ = 0; }
|
---|
| 1760 | ;
|
---|
[b87a5ed] | 1761 |
|
---|
| 1762 | type_name_list: // CFA
|
---|
[4d51835] | 1763 | type_name
|
---|
| 1764 | { $$ = new TypeValueNode( $1 ); }
|
---|
| 1765 | | assignment_expression
|
---|
| 1766 | | type_name_list ',' type_name
|
---|
| 1767 | { $$ = (ExpressionNode *)($1->set_link(new TypeValueNode( $3 ))); }
|
---|
| 1768 | | type_name_list ',' assignment_expression
|
---|
| 1769 | { $$ = (ExpressionNode *)($1->set_link($3)); }
|
---|
| 1770 | ;
|
---|
[b87a5ed] | 1771 |
|
---|
| 1772 | type_declaring_list: // CFA
|
---|
[4d51835] | 1773 | TYPE type_declarator
|
---|
| 1774 | { $$ = $2; }
|
---|
| 1775 | | storage_class_list TYPE type_declarator
|
---|
| 1776 | { $$ = $3->addQualifiers( $1 ); }
|
---|
| 1777 | | type_declaring_list ',' type_declarator
|
---|
| 1778 | { $$ = $1->appendList( $3->copyStorageClasses( $1 ) ); }
|
---|
| 1779 | ;
|
---|
[b87a5ed] | 1780 |
|
---|
| 1781 | type_declarator: // CFA
|
---|
[4d51835] | 1782 | type_declarator_name assertion_list_opt
|
---|
| 1783 | { $$ = $1->addAssertions( $2 ); }
|
---|
| 1784 | | type_declarator_name assertion_list_opt '=' type_name
|
---|
| 1785 | { $$ = $1->addAssertions( $2 )->addType( $4 ); }
|
---|
| 1786 | ;
|
---|
[b87a5ed] | 1787 |
|
---|
| 1788 | type_declarator_name: // CFA
|
---|
[4d51835] | 1789 | no_attr_identifier_or_typedef_name
|
---|
| 1790 | {
|
---|
| 1791 | typedefTable.addToEnclosingScope(*($1), TypedefTable::TD);
|
---|
| 1792 | $$ = DeclarationNode::newTypeDecl( $1, 0 );
|
---|
| 1793 | }
|
---|
| 1794 | | no_01_identifier_or_typedef_name '(' push type_parameter_list pop ')'
|
---|
| 1795 | {
|
---|
| 1796 | typedefTable.addToEnclosingScope(*($1), TypedefTable::TG);
|
---|
| 1797 | $$ = DeclarationNode::newTypeDecl( $1, $4 );
|
---|
| 1798 | }
|
---|
| 1799 | ;
|
---|
[b87a5ed] | 1800 |
|
---|
| 1801 | context_specifier: // CFA
|
---|
[4d51835] | 1802 | CONTEXT no_attr_identifier_or_typedef_name '(' push type_parameter_list pop ')' '{' '}'
|
---|
| 1803 | {
|
---|
| 1804 | typedefTable.addToEnclosingScope(*($2), TypedefTable::ID );
|
---|
| 1805 | $$ = DeclarationNode::newContext( $2, $5, 0 );
|
---|
| 1806 | }
|
---|
| 1807 | | CONTEXT no_attr_identifier_or_typedef_name '(' push type_parameter_list pop ')' '{'
|
---|
| 1808 | {
|
---|
| 1809 | typedefTable.enterContext( *($2) );
|
---|
| 1810 | typedefTable.enterScope();
|
---|
| 1811 | }
|
---|
| 1812 | context_declaration_list '}'
|
---|
| 1813 | {
|
---|
| 1814 | typedefTable.leaveContext();
|
---|
| 1815 | typedefTable.addToEnclosingScope(*($2), TypedefTable::ID );
|
---|
| 1816 | $$ = DeclarationNode::newContext( $2, $5, $10 );
|
---|
| 1817 | }
|
---|
| 1818 | ;
|
---|
[b87a5ed] | 1819 |
|
---|
| 1820 | context_declaration_list: // CFA
|
---|
[4d51835] | 1821 | context_declaration
|
---|
| 1822 | | context_declaration_list push context_declaration
|
---|
| 1823 | { $$ = $1->appendList( $3 ); }
|
---|
| 1824 | ;
|
---|
[b87a5ed] | 1825 |
|
---|
| 1826 | context_declaration: // CFA
|
---|
[4d51835] | 1827 | new_context_declaring_list pop ';'
|
---|
| 1828 | | context_declaring_list pop ';'
|
---|
| 1829 | ;
|
---|
[b87a5ed] | 1830 |
|
---|
| 1831 | new_context_declaring_list: // CFA
|
---|
[4d51835] | 1832 | new_variable_specifier
|
---|
| 1833 | {
|
---|
| 1834 | typedefTable.addToEnclosingScope2( TypedefTable::ID );
|
---|
| 1835 | $$ = $1;
|
---|
| 1836 | }
|
---|
| 1837 | | new_function_specifier
|
---|
| 1838 | {
|
---|
| 1839 | typedefTable.addToEnclosingScope2( TypedefTable::ID );
|
---|
| 1840 | $$ = $1;
|
---|
| 1841 | }
|
---|
| 1842 | | new_context_declaring_list pop ',' push identifier_or_typedef_name
|
---|
| 1843 | {
|
---|
| 1844 | typedefTable.addToEnclosingScope2( *($5), TypedefTable::ID );
|
---|
| 1845 | $$ = $1->appendList( $1->cloneType( $5 ) );
|
---|
| 1846 | }
|
---|
| 1847 | ;
|
---|
[b87a5ed] | 1848 |
|
---|
| 1849 | context_declaring_list: // CFA
|
---|
[4d51835] | 1850 | type_specifier declarator
|
---|
| 1851 | {
|
---|
| 1852 | typedefTable.addToEnclosingScope2( TypedefTable::ID );
|
---|
| 1853 | $$ = $2->addType( $1 );
|
---|
| 1854 | }
|
---|
| 1855 | | context_declaring_list pop ',' push declarator
|
---|
| 1856 | {
|
---|
| 1857 | typedefTable.addToEnclosingScope2( TypedefTable::ID );
|
---|
| 1858 | $$ = $1->appendList( $1->cloneBaseType( $5 ) );
|
---|
| 1859 | }
|
---|
| 1860 | ;
|
---|
[51b73452] | 1861 |
|
---|
[c11e31c] | 1862 | //***************************** EXTERNAL DEFINITIONS *****************************
|
---|
[51b73452] | 1863 |
|
---|
| 1864 | translation_unit:
|
---|
[4d51835] | 1865 | // empty
|
---|
| 1866 | {} // empty input file
|
---|
| 1867 | | external_definition_list
|
---|
| 1868 | {
|
---|
| 1869 | if ( theTree ) {
|
---|
| 1870 | theTree->appendList( $1 );
|
---|
| 1871 | } else {
|
---|
| 1872 | theTree = $1;
|
---|
| 1873 | }
|
---|
| 1874 | }
|
---|
| 1875 | ;
|
---|
[51b73452] | 1876 |
|
---|
| 1877 | external_definition_list:
|
---|
[4d51835] | 1878 | external_definition
|
---|
| 1879 | | external_definition_list push external_definition
|
---|
| 1880 | { $$ = ($1 != NULL ) ? $1->appendList( $3 ) : $3; }
|
---|
| 1881 | ;
|
---|
[51b73452] | 1882 |
|
---|
| 1883 | external_definition_list_opt:
|
---|
[4d51835] | 1884 | // empty
|
---|
| 1885 | { $$ = 0; }
|
---|
| 1886 | | external_definition_list
|
---|
| 1887 | ;
|
---|
[51b73452] | 1888 |
|
---|
| 1889 | external_definition:
|
---|
[4d51835] | 1890 | declaration
|
---|
| 1891 | | external_function_definition
|
---|
| 1892 | | asm_statement // GCC, global assembler statement
|
---|
| 1893 | {}
|
---|
| 1894 | | EXTERN STRINGliteral
|
---|
| 1895 | {
|
---|
| 1896 | linkageStack.push( linkage );
|
---|
| 1897 | linkage = LinkageSpec::fromString( *$2 );
|
---|
| 1898 | }
|
---|
| 1899 | '{' external_definition_list_opt '}' // C++-style linkage specifier
|
---|
| 1900 | {
|
---|
| 1901 | linkage = linkageStack.top();
|
---|
| 1902 | linkageStack.pop();
|
---|
| 1903 | $$ = $5;
|
---|
| 1904 | }
|
---|
| 1905 | | EXTENSION external_definition
|
---|
| 1906 | { $$ = $2; }
|
---|
| 1907 | ;
|
---|
| 1908 |
|
---|
| 1909 | external_function_definition:
|
---|
| 1910 | function_definition
|
---|
| 1911 |
|
---|
| 1912 | // These rules are a concession to the "implicit int" type_specifier because there is a significant
|
---|
| 1913 | // amount of code with functions missing a type-specifier on the return type. Parsing is possible
|
---|
| 1914 | // because function_definition does not appear in the context of an expression (nested functions would
|
---|
| 1915 | // preclude this concession). A function prototype declaration must still have a type_specifier.
|
---|
| 1916 | // OBSOLESCENT (see 1)
|
---|
| 1917 | | function_declarator compound_statement
|
---|
| 1918 | {
|
---|
| 1919 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1920 | typedefTable.leaveScope();
|
---|
| 1921 | $$ = $1->addFunctionBody( $2 );
|
---|
| 1922 | }
|
---|
| 1923 | | old_function_declarator push old_declaration_list_opt compound_statement
|
---|
| 1924 | {
|
---|
| 1925 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1926 | typedefTable.leaveScope();
|
---|
| 1927 | $$ = $1->addOldDeclList( $3 )->addFunctionBody( $4 );
|
---|
| 1928 | }
|
---|
| 1929 | ;
|
---|
[51b73452] | 1930 |
|
---|
| 1931 | function_definition:
|
---|
[4d51835] | 1932 | new_function_declaration compound_statement // CFA
|
---|
| 1933 | {
|
---|
| 1934 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1935 | typedefTable.leaveScope();
|
---|
| 1936 | $$ = $1->addFunctionBody( $2 );
|
---|
| 1937 | }
|
---|
| 1938 | | declaration_specifier function_declarator compound_statement
|
---|
| 1939 | {
|
---|
| 1940 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1941 | typedefTable.leaveScope();
|
---|
| 1942 | $$ = $2->addFunctionBody( $3 )->addType( $1 );
|
---|
| 1943 | }
|
---|
| 1944 | | type_qualifier_list function_declarator compound_statement
|
---|
| 1945 | {
|
---|
| 1946 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1947 | typedefTable.leaveScope();
|
---|
| 1948 | $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
|
---|
| 1949 | }
|
---|
| 1950 | | declaration_qualifier_list function_declarator compound_statement
|
---|
| 1951 | {
|
---|
| 1952 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1953 | typedefTable.leaveScope();
|
---|
| 1954 | $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
|
---|
| 1955 | }
|
---|
| 1956 | | declaration_qualifier_list type_qualifier_list function_declarator compound_statement
|
---|
| 1957 | {
|
---|
| 1958 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1959 | typedefTable.leaveScope();
|
---|
| 1960 | $$ = $3->addFunctionBody( $4 )->addQualifiers( $2 )->addQualifiers( $1 );
|
---|
| 1961 | }
|
---|
| 1962 |
|
---|
| 1963 | // Old-style K&R function definition, OBSOLESCENT (see 4)
|
---|
| 1964 | | declaration_specifier old_function_declarator push old_declaration_list_opt compound_statement
|
---|
| 1965 | {
|
---|
| 1966 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1967 | typedefTable.leaveScope();
|
---|
| 1968 | $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addType( $1 );
|
---|
| 1969 | }
|
---|
| 1970 | | type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
|
---|
| 1971 | {
|
---|
| 1972 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1973 | typedefTable.leaveScope();
|
---|
| 1974 | $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
|
---|
| 1975 | }
|
---|
| 1976 |
|
---|
| 1977 | // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
|
---|
| 1978 | | declaration_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
|
---|
| 1979 | {
|
---|
| 1980 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1981 | typedefTable.leaveScope();
|
---|
| 1982 | $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
|
---|
| 1983 | }
|
---|
| 1984 | | declaration_qualifier_list type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
|
---|
| 1985 | {
|
---|
| 1986 | typedefTable.addToEnclosingScope( TypedefTable::ID );
|
---|
| 1987 | typedefTable.leaveScope();
|
---|
| 1988 | $$ = $3->addOldDeclList( $5 )->addFunctionBody( $6 )->addQualifiers( $2 )->addQualifiers( $1 );
|
---|
| 1989 | }
|
---|
| 1990 | ;
|
---|
[51b73452] | 1991 |
|
---|
| 1992 | declarator:
|
---|
[4d51835] | 1993 | variable_declarator
|
---|
| 1994 | | function_declarator
|
---|
| 1995 | | typedef_redeclarator
|
---|
| 1996 | ;
|
---|
[51b73452] | 1997 |
|
---|
| 1998 | subrange:
|
---|
[4d51835] | 1999 | constant_expression '~' constant_expression // CFA, integer subrange
|
---|
| 2000 | { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Range), $1, $3); }
|
---|
| 2001 | ;
|
---|
[b87a5ed] | 2002 |
|
---|
| 2003 | asm_name_opt: // GCC
|
---|
[4d51835] | 2004 | // empty
|
---|
| 2005 | | ASM '(' string_literal_list ')' attribute_list_opt
|
---|
| 2006 | ;
|
---|
[b87a5ed] | 2007 |
|
---|
| 2008 | attribute_list_opt: // GCC
|
---|
[4d51835] | 2009 | // empty
|
---|
| 2010 | | attribute_list
|
---|
| 2011 | ;
|
---|
[b87a5ed] | 2012 |
|
---|
| 2013 | attribute_list: // GCC
|
---|
[4d51835] | 2014 | attribute
|
---|
| 2015 | | attribute_list attribute
|
---|
| 2016 | ;
|
---|
[b87a5ed] | 2017 |
|
---|
| 2018 | attribute: // GCC
|
---|
[4d51835] | 2019 | ATTRIBUTE '(' '(' attribute_parameter_list ')' ')'
|
---|
| 2020 | ;
|
---|
[b87a5ed] | 2021 |
|
---|
| 2022 | attribute_parameter_list: // GCC
|
---|
[4d51835] | 2023 | attrib
|
---|
| 2024 | | attribute_parameter_list ',' attrib
|
---|
| 2025 | ;
|
---|
[b87a5ed] | 2026 |
|
---|
| 2027 | attrib: // GCC
|
---|
[4d51835] | 2028 | // empty
|
---|
| 2029 | | any_word
|
---|
| 2030 | | any_word '(' comma_expression_opt ')'
|
---|
| 2031 | ;
|
---|
[b87a5ed] | 2032 |
|
---|
| 2033 | any_word: // GCC
|
---|
[4d51835] | 2034 | identifier_or_typedef_name {}
|
---|
| 2035 | | storage_class_name {}
|
---|
| 2036 | | basic_type_name {}
|
---|
| 2037 | | type_qualifier {}
|
---|
| 2038 | ;
|
---|
[51b73452] | 2039 |
|
---|
[c11e31c] | 2040 | // ============================================================================
|
---|
| 2041 | // The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are
|
---|
| 2042 | // necessary because the type of an identifier in wrapped around the identifier in the same form as its usage
|
---|
| 2043 | // in an expression, as in:
|
---|
| 2044 | //
|
---|
[b87a5ed] | 2045 | // int (*f())[10] { ... };
|
---|
| 2046 | // ... (*f())[3] += 1; // definition mimics usage
|
---|
[c11e31c] | 2047 | //
|
---|
| 2048 | // Because these patterns are highly recursive, changes at a lower level in the recursion require copying some
|
---|
| 2049 | // or all of the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a
|
---|
| 2050 | // particular context.
|
---|
| 2051 | // ============================================================================
|
---|
| 2052 |
|
---|
| 2053 | // ----------------------------------------------------------------------------
|
---|
| 2054 | // The set of valid declarators before a compound statement for defining a function is less than the set of
|
---|
| 2055 | // declarators to define a variable or function prototype, e.g.:
|
---|
| 2056 | //
|
---|
[b87a5ed] | 2057 | // valid declaration invalid definition
|
---|
| 2058 | // ----------------- ------------------
|
---|
[4d51835] | 2059 | // int f; int f {}
|
---|
| 2060 | // int *f; int *f {}
|
---|
[b87a5ed] | 2061 | // int f[10]; int f[10] {}
|
---|
[4d51835] | 2062 | // int (*f)(int); int (*f)(int) {}
|
---|
[c11e31c] | 2063 | //
|
---|
| 2064 | // To preclude this syntactic anomaly requires separating the grammar rules for variable and function
|
---|
| 2065 | // declarators, hence variable_declarator and function_declarator.
|
---|
| 2066 | // ----------------------------------------------------------------------------
|
---|
| 2067 |
|
---|
| 2068 | // This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern
|
---|
| 2069 | // precludes declaring an array of functions versus a pointer to an array of functions.
|
---|
[51b73452] | 2070 |
|
---|
| 2071 | variable_declarator:
|
---|
[4d51835] | 2072 | paren_identifier attribute_list_opt
|
---|
| 2073 | | variable_ptr
|
---|
| 2074 | | variable_array attribute_list_opt
|
---|
| 2075 | | variable_function attribute_list_opt
|
---|
| 2076 | ;
|
---|
[51b73452] | 2077 |
|
---|
| 2078 | paren_identifier:
|
---|
[4d51835] | 2079 | identifier
|
---|
| 2080 | {
|
---|
| 2081 | typedefTable.setNextIdentifier( *($1) );
|
---|
| 2082 | $$ = DeclarationNode::newName( $1 );
|
---|
| 2083 | }
|
---|
| 2084 | | '(' paren_identifier ')' // redundant parenthesis
|
---|
| 2085 | { $$ = $2; }
|
---|
| 2086 | ;
|
---|
[51b73452] | 2087 |
|
---|
| 2088 | variable_ptr:
|
---|
[4d51835] | 2089 | '*' variable_declarator
|
---|
| 2090 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2091 | | '*' type_qualifier_list variable_declarator
|
---|
| 2092 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2093 | | '(' variable_ptr ')'
|
---|
| 2094 | { $$ = $2; }
|
---|
| 2095 | ;
|
---|
[51b73452] | 2096 |
|
---|
| 2097 | variable_array:
|
---|
[4d51835] | 2098 | paren_identifier array_dimension
|
---|
| 2099 | { $$ = $1->addArray( $2 ); }
|
---|
| 2100 | | '(' variable_ptr ')' array_dimension
|
---|
| 2101 | { $$ = $2->addArray( $4 ); }
|
---|
| 2102 | | '(' variable_array ')' multi_array_dimension // redundant parenthesis
|
---|
| 2103 | { $$ = $2->addArray( $4 ); }
|
---|
| 2104 | | '(' variable_array ')' // redundant parenthesis
|
---|
| 2105 | { $$ = $2; }
|
---|
| 2106 | ;
|
---|
[51b73452] | 2107 |
|
---|
| 2108 | variable_function:
|
---|
[4d51835] | 2109 | '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2110 | { $$ = $2->addParamList( $6 ); }
|
---|
| 2111 | | '(' variable_function ')' // redundant parenthesis
|
---|
| 2112 | { $$ = $2; }
|
---|
| 2113 | ;
|
---|
[51b73452] | 2114 |
|
---|
[c11e31c] | 2115 | // This pattern parses a function declarator that is not redefining a typedef name. Because functions cannot
|
---|
| 2116 | // be nested, there is no context where a function definition can redefine a typedef name. To allow nested
|
---|
| 2117 | // functions requires further separation of variable and function declarators in typedef_redeclarator. The
|
---|
| 2118 | // pattern precludes returning arrays and functions versus pointers to arrays and functions.
|
---|
[51b73452] | 2119 |
|
---|
| 2120 | function_declarator:
|
---|
[4d51835] | 2121 | function_no_ptr attribute_list_opt
|
---|
| 2122 | | function_ptr
|
---|
| 2123 | | function_array attribute_list_opt
|
---|
| 2124 | ;
|
---|
[51b73452] | 2125 |
|
---|
| 2126 | function_no_ptr:
|
---|
[4d51835] | 2127 | paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2128 | { $$ = $1->addParamList( $4 ); }
|
---|
| 2129 | | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
|
---|
| 2130 | { $$ = $2->addParamList( $6 ); }
|
---|
| 2131 | | '(' function_no_ptr ')' // redundant parenthesis
|
---|
| 2132 | { $$ = $2; }
|
---|
| 2133 | ;
|
---|
[51b73452] | 2134 |
|
---|
| 2135 | function_ptr:
|
---|
[4d51835] | 2136 | '*' function_declarator
|
---|
| 2137 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2138 | | '*' type_qualifier_list function_declarator
|
---|
| 2139 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2140 | | '(' function_ptr ')'
|
---|
| 2141 | { $$ = $2; }
|
---|
| 2142 | ;
|
---|
[51b73452] | 2143 |
|
---|
| 2144 | function_array:
|
---|
[4d51835] | 2145 | '(' function_ptr ')' array_dimension
|
---|
| 2146 | { $$ = $2->addArray( $4 ); }
|
---|
| 2147 | | '(' function_array ')' multi_array_dimension // redundant parenthesis
|
---|
| 2148 | { $$ = $2->addArray( $4 ); }
|
---|
| 2149 | | '(' function_array ')' // redundant parenthesis
|
---|
| 2150 | { $$ = $2; }
|
---|
| 2151 | ;
|
---|
[51b73452] | 2152 |
|
---|
[c11e31c] | 2153 | // This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4) that is not redefining a
|
---|
| 2154 | // typedef name (see function_declarator for additional comments). The pattern precludes returning arrays and
|
---|
| 2155 | // functions versus pointers to arrays and functions.
|
---|
[51b73452] | 2156 |
|
---|
| 2157 | old_function_declarator:
|
---|
[4d51835] | 2158 | old_function_no_ptr
|
---|
| 2159 | | old_function_ptr
|
---|
| 2160 | | old_function_array
|
---|
| 2161 | ;
|
---|
[51b73452] | 2162 |
|
---|
| 2163 | old_function_no_ptr:
|
---|
[4d51835] | 2164 | paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
|
---|
| 2165 | { $$ = $1->addIdList( $3 ); }
|
---|
| 2166 | | '(' old_function_ptr ')' '(' identifier_list ')'
|
---|
| 2167 | { $$ = $2->addIdList( $5 ); }
|
---|
| 2168 | | '(' old_function_no_ptr ')' // redundant parenthesis
|
---|
| 2169 | { $$ = $2; }
|
---|
| 2170 | ;
|
---|
[51b73452] | 2171 |
|
---|
| 2172 | old_function_ptr:
|
---|
[4d51835] | 2173 | '*' old_function_declarator
|
---|
| 2174 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2175 | | '*' type_qualifier_list old_function_declarator
|
---|
| 2176 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2177 | | '(' old_function_ptr ')'
|
---|
| 2178 | { $$ = $2; }
|
---|
| 2179 | ;
|
---|
[51b73452] | 2180 |
|
---|
| 2181 | old_function_array:
|
---|
[4d51835] | 2182 | '(' old_function_ptr ')' array_dimension
|
---|
| 2183 | { $$ = $2->addArray( $4 ); }
|
---|
| 2184 | | '(' old_function_array ')' multi_array_dimension // redundant parenthesis
|
---|
| 2185 | { $$ = $2->addArray( $4 ); }
|
---|
| 2186 | | '(' old_function_array ')' // redundant parenthesis
|
---|
| 2187 | { $$ = $2; }
|
---|
| 2188 | ;
|
---|
[51b73452] | 2189 |
|
---|
[c11e31c] | 2190 | // This pattern parses a declaration for a variable or function prototype that redefines a typedef name, e.g.:
|
---|
| 2191 | //
|
---|
[b87a5ed] | 2192 | // typedef int foo;
|
---|
| 2193 | // {
|
---|
| 2194 | // int foo; // redefine typedef name in new scope
|
---|
| 2195 | // }
|
---|
[c11e31c] | 2196 | //
|
---|
| 2197 | // The pattern precludes declaring an array of functions versus a pointer to an array of functions, and
|
---|
| 2198 | // returning arrays and functions versus pointers to arrays and functions.
|
---|
[51b73452] | 2199 |
|
---|
| 2200 | typedef_redeclarator:
|
---|
[4d51835] | 2201 | paren_typedef attribute_list_opt
|
---|
| 2202 | | typedef_ptr
|
---|
| 2203 | | typedef_array attribute_list_opt
|
---|
| 2204 | | typedef_function attribute_list_opt
|
---|
| 2205 | ;
|
---|
[51b73452] | 2206 |
|
---|
| 2207 | paren_typedef:
|
---|
[4d51835] | 2208 | TYPEDEFname
|
---|
| 2209 | {
|
---|
| 2210 | typedefTable.setNextIdentifier( *($1) );
|
---|
| 2211 | $$ = DeclarationNode::newName( $1 );
|
---|
| 2212 | }
|
---|
| 2213 | | '(' paren_typedef ')'
|
---|
| 2214 | { $$ = $2; }
|
---|
| 2215 | ;
|
---|
[51b73452] | 2216 |
|
---|
| 2217 | typedef_ptr:
|
---|
[4d51835] | 2218 | '*' typedef_redeclarator
|
---|
| 2219 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2220 | | '*' type_qualifier_list typedef_redeclarator
|
---|
| 2221 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2222 | | '(' typedef_ptr ')'
|
---|
| 2223 | { $$ = $2; }
|
---|
| 2224 | ;
|
---|
[51b73452] | 2225 |
|
---|
| 2226 | typedef_array:
|
---|
[4d51835] | 2227 | paren_typedef array_dimension
|
---|
| 2228 | { $$ = $1->addArray( $2 ); }
|
---|
| 2229 | | '(' typedef_ptr ')' array_dimension
|
---|
| 2230 | { $$ = $2->addArray( $4 ); }
|
---|
| 2231 | | '(' typedef_array ')' multi_array_dimension // redundant parenthesis
|
---|
| 2232 | { $$ = $2->addArray( $4 ); }
|
---|
| 2233 | | '(' typedef_array ')' // redundant parenthesis
|
---|
| 2234 | { $$ = $2; }
|
---|
| 2235 | ;
|
---|
[51b73452] | 2236 |
|
---|
| 2237 | typedef_function:
|
---|
[4d51835] | 2238 | paren_typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2239 | { $$ = $1->addParamList( $4 ); }
|
---|
| 2240 | | '(' typedef_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2241 | { $$ = $2->addParamList( $6 ); }
|
---|
| 2242 | | '(' typedef_function ')' // redundant parenthesis
|
---|
| 2243 | { $$ = $2; }
|
---|
| 2244 | ;
|
---|
[51b73452] | 2245 |
|
---|
[c11e31c] | 2246 | // This pattern parses a declaration for a parameter variable or function prototype that is not redefining a
|
---|
| 2247 | // typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern
|
---|
| 2248 | // precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
|
---|
| 2249 | // and functions versus pointers to arrays and functions.
|
---|
[51b73452] | 2250 |
|
---|
| 2251 | identifier_parameter_declarator:
|
---|
[4d51835] | 2252 | paren_identifier attribute_list_opt
|
---|
| 2253 | | identifier_parameter_ptr
|
---|
| 2254 | | identifier_parameter_array attribute_list_opt
|
---|
| 2255 | | identifier_parameter_function attribute_list_opt
|
---|
| 2256 | ;
|
---|
[51b73452] | 2257 |
|
---|
| 2258 | identifier_parameter_ptr:
|
---|
[4d51835] | 2259 | '*' identifier_parameter_declarator
|
---|
| 2260 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2261 | | '*' type_qualifier_list identifier_parameter_declarator
|
---|
| 2262 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2263 | | '(' identifier_parameter_ptr ')'
|
---|
| 2264 | { $$ = $2; }
|
---|
| 2265 | ;
|
---|
[51b73452] | 2266 |
|
---|
| 2267 | identifier_parameter_array:
|
---|
[4d51835] | 2268 | paren_identifier array_parameter_dimension
|
---|
| 2269 | { $$ = $1->addArray( $2 ); }
|
---|
| 2270 | | '(' identifier_parameter_ptr ')' array_dimension
|
---|
| 2271 | { $$ = $2->addArray( $4 ); }
|
---|
| 2272 | | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
|
---|
| 2273 | { $$ = $2->addArray( $4 ); }
|
---|
| 2274 | | '(' identifier_parameter_array ')' // redundant parenthesis
|
---|
| 2275 | { $$ = $2; }
|
---|
| 2276 | ;
|
---|
[51b73452] | 2277 |
|
---|
| 2278 | identifier_parameter_function:
|
---|
[4d51835] | 2279 | paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2280 | { $$ = $1->addParamList( $4 ); }
|
---|
| 2281 | | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2282 | { $$ = $2->addParamList( $6 ); }
|
---|
| 2283 | | '(' identifier_parameter_function ')' // redundant parenthesis
|
---|
| 2284 | { $$ = $2; }
|
---|
| 2285 | ;
|
---|
[b87a5ed] | 2286 |
|
---|
| 2287 | // This pattern parses a declaration for a parameter variable or function prototype that is redefining a
|
---|
| 2288 | // typedef name, e.g.:
|
---|
[c11e31c] | 2289 | //
|
---|
[b87a5ed] | 2290 | // typedef int foo;
|
---|
| 2291 | // int f( int foo ); // redefine typedef name in new scope
|
---|
[c11e31c] | 2292 | //
|
---|
[b87a5ed] | 2293 | // and allows the C99 array options, which can only appear in a parameter list. In addition, the pattern
|
---|
| 2294 | // handles the special meaning of parenthesis around a typedef name:
|
---|
[c11e31c] | 2295 | //
|
---|
[b87a5ed] | 2296 | // ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
|
---|
| 2297 | // parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
|
---|
| 2298 | // not as redundant parentheses around the identifier."
|
---|
[c11e31c] | 2299 | //
|
---|
| 2300 | // which precludes the following cases:
|
---|
| 2301 | //
|
---|
[b87a5ed] | 2302 | // typedef float T;
|
---|
[4d51835] | 2303 | // int f( int ( T [5] ) ); // see abstract_parameter_declarator
|
---|
[b87a5ed] | 2304 | // int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
|
---|
| 2305 | // int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
|
---|
| 2306 | // int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
|
---|
[c11e31c] | 2307 | //
|
---|
[b87a5ed] | 2308 | // In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type
|
---|
| 2309 | // list, and not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes
|
---|
| 2310 | // declaring an array of functions versus a pointer to an array of functions, and returning arrays and
|
---|
| 2311 | // functions versus pointers to arrays and functions.
|
---|
[51b73452] | 2312 |
|
---|
| 2313 | typedef_parameter_redeclarator:
|
---|
[4d51835] | 2314 | typedef attribute_list_opt
|
---|
| 2315 | | typedef_parameter_ptr
|
---|
| 2316 | | typedef_parameter_array attribute_list_opt
|
---|
| 2317 | | typedef_parameter_function attribute_list_opt
|
---|
| 2318 | ;
|
---|
[51b73452] | 2319 |
|
---|
| 2320 | typedef:
|
---|
[4d51835] | 2321 | TYPEDEFname
|
---|
| 2322 | {
|
---|
| 2323 | typedefTable.setNextIdentifier( *($1) );
|
---|
| 2324 | $$ = DeclarationNode::newName( $1 );
|
---|
| 2325 | }
|
---|
| 2326 | ;
|
---|
[51b73452] | 2327 |
|
---|
| 2328 | typedef_parameter_ptr:
|
---|
[4d51835] | 2329 | '*' typedef_parameter_redeclarator
|
---|
| 2330 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2331 | | '*' type_qualifier_list typedef_parameter_redeclarator
|
---|
| 2332 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2333 | | '(' typedef_parameter_ptr ')'
|
---|
| 2334 | { $$ = $2; }
|
---|
| 2335 | ;
|
---|
[51b73452] | 2336 |
|
---|
| 2337 | typedef_parameter_array:
|
---|
[4d51835] | 2338 | typedef array_parameter_dimension
|
---|
| 2339 | { $$ = $1->addArray( $2 ); }
|
---|
| 2340 | | '(' typedef_parameter_ptr ')' array_parameter_dimension
|
---|
| 2341 | { $$ = $2->addArray( $4 ); }
|
---|
| 2342 | ;
|
---|
[51b73452] | 2343 |
|
---|
| 2344 | typedef_parameter_function:
|
---|
[4d51835] | 2345 | typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2346 | { $$ = $1->addParamList( $4 ); }
|
---|
| 2347 | | '(' typedef_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2348 | { $$ = $2->addParamList( $6 ); }
|
---|
| 2349 | ;
|
---|
[b87a5ed] | 2350 |
|
---|
| 2351 | // This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no
|
---|
| 2352 | // identifier to which the type applies, e.g.:
|
---|
[c11e31c] | 2353 | //
|
---|
[b87a5ed] | 2354 | // sizeof( int );
|
---|
| 2355 | // sizeof( int [10] );
|
---|
[c11e31c] | 2356 | //
|
---|
[b87a5ed] | 2357 | // The pattern precludes declaring an array of functions versus a pointer to an array of functions, and
|
---|
| 2358 | // returning arrays and functions versus pointers to arrays and functions.
|
---|
[51b73452] | 2359 |
|
---|
| 2360 | abstract_declarator:
|
---|
[4d51835] | 2361 | abstract_ptr
|
---|
| 2362 | | abstract_array attribute_list_opt
|
---|
| 2363 | | abstract_function attribute_list_opt
|
---|
| 2364 | ;
|
---|
[51b73452] | 2365 |
|
---|
| 2366 | abstract_ptr:
|
---|
[4d51835] | 2367 | '*'
|
---|
| 2368 | { $$ = DeclarationNode::newPointer( 0 ); }
|
---|
| 2369 | | '*' type_qualifier_list
|
---|
| 2370 | { $$ = DeclarationNode::newPointer( $2 ); }
|
---|
| 2371 | | '*' abstract_declarator
|
---|
| 2372 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2373 | | '*' type_qualifier_list abstract_declarator
|
---|
| 2374 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2375 | | '(' abstract_ptr ')'
|
---|
| 2376 | { $$ = $2; }
|
---|
| 2377 | ;
|
---|
[51b73452] | 2378 |
|
---|
| 2379 | abstract_array:
|
---|
[4d51835] | 2380 | array_dimension
|
---|
| 2381 | | '(' abstract_ptr ')' array_dimension
|
---|
| 2382 | { $$ = $2->addArray( $4 ); }
|
---|
| 2383 | | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
|
---|
| 2384 | { $$ = $2->addArray( $4 ); }
|
---|
| 2385 | | '(' abstract_array ')' // redundant parenthesis
|
---|
| 2386 | { $$ = $2; }
|
---|
| 2387 | ;
|
---|
[51b73452] | 2388 |
|
---|
| 2389 | abstract_function:
|
---|
[4d51835] | 2390 | '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2391 | { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
|
---|
| 2392 | | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2393 | { $$ = $2->addParamList( $6 ); }
|
---|
| 2394 | | '(' abstract_function ')' // redundant parenthesis
|
---|
| 2395 | { $$ = $2; }
|
---|
| 2396 | ;
|
---|
[51b73452] | 2397 |
|
---|
| 2398 | array_dimension:
|
---|
[4d51835] | 2399 | // Only the first dimension can be empty.
|
---|
| 2400 | '[' push pop ']'
|
---|
| 2401 | { $$ = DeclarationNode::newArray( 0, 0, false ); }
|
---|
| 2402 | | '[' push pop ']' multi_array_dimension
|
---|
| 2403 | { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $5 ); }
|
---|
| 2404 | | multi_array_dimension
|
---|
| 2405 | ;
|
---|
[51b73452] | 2406 |
|
---|
| 2407 | multi_array_dimension:
|
---|
[4d51835] | 2408 | '[' push assignment_expression pop ']'
|
---|
| 2409 | { $$ = DeclarationNode::newArray( $3, 0, false ); }
|
---|
| 2410 | | '[' push '*' pop ']' // C99
|
---|
| 2411 | { $$ = DeclarationNode::newVarArray( 0 ); }
|
---|
| 2412 | | multi_array_dimension '[' push assignment_expression pop ']'
|
---|
| 2413 | { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
|
---|
| 2414 | | multi_array_dimension '[' push '*' pop ']' // C99
|
---|
| 2415 | { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
|
---|
| 2416 | ;
|
---|
[51b73452] | 2417 |
|
---|
[c11e31c] | 2418 | // This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
|
---|
| 2419 | // identifier to which the type applies, e.g.:
|
---|
| 2420 | //
|
---|
[b87a5ed] | 2421 | // int f( int ); // abstract variable parameter; no parameter name specified
|
---|
| 2422 | // int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
|
---|
[c11e31c] | 2423 | //
|
---|
[b87a5ed] | 2424 | // The pattern precludes declaring an array of functions versus a pointer to an array of functions, and
|
---|
| 2425 | // returning arrays and functions versus pointers to arrays and functions.
|
---|
[51b73452] | 2426 |
|
---|
| 2427 | abstract_parameter_declarator:
|
---|
[4d51835] | 2428 | abstract_parameter_ptr
|
---|
| 2429 | | abstract_parameter_array attribute_list_opt
|
---|
| 2430 | | abstract_parameter_function attribute_list_opt
|
---|
| 2431 | ;
|
---|
[51b73452] | 2432 |
|
---|
| 2433 | abstract_parameter_ptr:
|
---|
[4d51835] | 2434 | '*'
|
---|
| 2435 | { $$ = DeclarationNode::newPointer( 0 ); }
|
---|
| 2436 | | '*' type_qualifier_list
|
---|
| 2437 | { $$ = DeclarationNode::newPointer( $2 ); }
|
---|
| 2438 | | '*' abstract_parameter_declarator
|
---|
| 2439 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2440 | | '*' type_qualifier_list abstract_parameter_declarator
|
---|
| 2441 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2442 | | '(' abstract_parameter_ptr ')'
|
---|
| 2443 | { $$ = $2; }
|
---|
| 2444 | ;
|
---|
[51b73452] | 2445 |
|
---|
| 2446 | abstract_parameter_array:
|
---|
[4d51835] | 2447 | array_parameter_dimension
|
---|
| 2448 | | '(' abstract_parameter_ptr ')' array_parameter_dimension
|
---|
| 2449 | { $$ = $2->addArray( $4 ); }
|
---|
| 2450 | | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
|
---|
| 2451 | { $$ = $2->addArray( $4 ); }
|
---|
| 2452 | | '(' abstract_parameter_array ')' // redundant parenthesis
|
---|
| 2453 | { $$ = $2; }
|
---|
| 2454 | ;
|
---|
[51b73452] | 2455 |
|
---|
| 2456 | abstract_parameter_function:
|
---|
[4d51835] | 2457 | '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2458 | { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
|
---|
| 2459 | | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2460 | { $$ = $2->addParamList( $6 ); }
|
---|
| 2461 | | '(' abstract_parameter_function ')' // redundant parenthesis
|
---|
| 2462 | { $$ = $2; }
|
---|
| 2463 | ;
|
---|
[51b73452] | 2464 |
|
---|
| 2465 | array_parameter_dimension:
|
---|
[4d51835] | 2466 | // Only the first dimension can be empty or have qualifiers.
|
---|
| 2467 | array_parameter_1st_dimension
|
---|
| 2468 | | array_parameter_1st_dimension multi_array_dimension
|
---|
| 2469 | { $$ = $1->addArray( $2 ); }
|
---|
| 2470 | | multi_array_dimension
|
---|
| 2471 | ;
|
---|
[51b73452] | 2472 |
|
---|
[c11e31c] | 2473 | // The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
|
---|
| 2474 | //
|
---|
[b87a5ed] | 2475 | // ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall
|
---|
| 2476 | // appear only in a declaration of a function parameter with an array type, and then only in the
|
---|
| 2477 | // outermost array type derivation."
|
---|
[51b73452] | 2478 |
|
---|
| 2479 | array_parameter_1st_dimension:
|
---|
[4d51835] | 2480 | '[' push pop ']'
|
---|
| 2481 | { $$ = DeclarationNode::newArray( 0, 0, false ); }
|
---|
| 2482 | // multi_array_dimension handles the '[' '*' ']' case
|
---|
| 2483 | | '[' push type_qualifier_list '*' pop ']' // remaining C99
|
---|
| 2484 | { $$ = DeclarationNode::newVarArray( $3 ); }
|
---|
| 2485 | | '[' push type_qualifier_list pop ']'
|
---|
| 2486 | { $$ = DeclarationNode::newArray( 0, $3, false ); }
|
---|
| 2487 | // multi_array_dimension handles the '[' assignment_expression ']' case
|
---|
| 2488 | | '[' push type_qualifier_list assignment_expression pop ']'
|
---|
| 2489 | { $$ = DeclarationNode::newArray( $4, $3, false ); }
|
---|
| 2490 | | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
|
---|
| 2491 | { $$ = DeclarationNode::newArray( $5, $4, true ); }
|
---|
| 2492 | | '[' push type_qualifier_list STATIC assignment_expression pop ']'
|
---|
| 2493 | { $$ = DeclarationNode::newArray( $5, $3, true ); }
|
---|
| 2494 | ;
|
---|
[b87a5ed] | 2495 |
|
---|
| 2496 | // This pattern parses a declaration of an abstract variable, i.e., there is no identifier to which the type
|
---|
| 2497 | // applies, e.g.:
|
---|
[c11e31c] | 2498 | //
|
---|
[b87a5ed] | 2499 | // sizeof( int ); // abstract variable; no identifier name specified
|
---|
[c11e31c] | 2500 | //
|
---|
[b87a5ed] | 2501 | // The pattern precludes declaring an array of functions versus a pointer to an array of functions, and
|
---|
| 2502 | // returning arrays and functions versus pointers to arrays and functions.
|
---|
[51b73452] | 2503 |
|
---|
| 2504 | variable_abstract_declarator:
|
---|
[4d51835] | 2505 | variable_abstract_ptr
|
---|
| 2506 | | variable_abstract_array attribute_list_opt
|
---|
| 2507 | | variable_abstract_function attribute_list_opt
|
---|
| 2508 | ;
|
---|
[51b73452] | 2509 |
|
---|
| 2510 | variable_abstract_ptr:
|
---|
[4d51835] | 2511 | '*'
|
---|
| 2512 | { $$ = DeclarationNode::newPointer( 0 ); }
|
---|
| 2513 | | '*' type_qualifier_list
|
---|
| 2514 | { $$ = DeclarationNode::newPointer( $2 ); }
|
---|
| 2515 | | '*' variable_abstract_declarator
|
---|
| 2516 | { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2517 | | '*' type_qualifier_list variable_abstract_declarator
|
---|
| 2518 | { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
|
---|
| 2519 | | '(' variable_abstract_ptr ')'
|
---|
| 2520 | { $$ = $2; }
|
---|
| 2521 | ;
|
---|
[51b73452] | 2522 |
|
---|
| 2523 | variable_abstract_array:
|
---|
[4d51835] | 2524 | array_dimension
|
---|
| 2525 | | '(' variable_abstract_ptr ')' array_dimension
|
---|
| 2526 | { $$ = $2->addArray( $4 ); }
|
---|
| 2527 | | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
|
---|
| 2528 | { $$ = $2->addArray( $4 ); }
|
---|
| 2529 | | '(' variable_abstract_array ')' // redundant parenthesis
|
---|
| 2530 | { $$ = $2; }
|
---|
| 2531 | ;
|
---|
[51b73452] | 2532 |
|
---|
| 2533 | variable_abstract_function:
|
---|
[4d51835] | 2534 | '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
|
---|
| 2535 | { $$ = $2->addParamList( $6 ); }
|
---|
| 2536 | | '(' variable_abstract_function ')' // redundant parenthesis
|
---|
| 2537 | { $$ = $2; }
|
---|
| 2538 | ;
|
---|
[b87a5ed] | 2539 |
|
---|
| 2540 | // This pattern parses a new-style declaration for a parameter variable or function prototype that is either
|
---|
| 2541 | // an identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
|
---|
| 2542 |
|
---|
| 2543 | new_identifier_parameter_declarator_tuple: // CFA
|
---|
[4d51835] | 2544 | new_identifier_parameter_declarator_no_tuple
|
---|
| 2545 | | new_abstract_tuple
|
---|
| 2546 | | type_qualifier_list new_abstract_tuple
|
---|
| 2547 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 2548 | ;
|
---|
[b87a5ed] | 2549 |
|
---|
| 2550 | new_identifier_parameter_declarator_no_tuple: // CFA
|
---|
[4d51835] | 2551 | new_identifier_parameter_ptr
|
---|
| 2552 | | new_identifier_parameter_array
|
---|
| 2553 | ;
|
---|
[b87a5ed] | 2554 |
|
---|
| 2555 | new_identifier_parameter_ptr: // CFA
|
---|
[4d51835] | 2556 | '*' type_specifier
|
---|
| 2557 | { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2558 | | type_qualifier_list '*' type_specifier
|
---|
| 2559 | { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
|
---|
| 2560 | | '*' new_abstract_function
|
---|
| 2561 | { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2562 | | type_qualifier_list '*' new_abstract_function
|
---|
| 2563 | { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
|
---|
| 2564 | | '*' new_identifier_parameter_declarator_tuple
|
---|
| 2565 | { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2566 | | type_qualifier_list '*' new_identifier_parameter_declarator_tuple
|
---|
| 2567 | { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
|
---|
| 2568 | ;
|
---|
[b87a5ed] | 2569 |
|
---|
| 2570 | new_identifier_parameter_array: // CFA
|
---|
[4d51835] | 2571 | // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due
|
---|
| 2572 | // to shift/reduce conflict with new-style empty (void) function return type.
|
---|
| 2573 | '[' push pop ']' type_specifier
|
---|
| 2574 | { $$ = $5->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
|
---|
| 2575 | | new_array_parameter_1st_dimension type_specifier
|
---|
| 2576 | { $$ = $2->addNewArray( $1 ); }
|
---|
| 2577 | | '[' push pop ']' multi_array_dimension type_specifier
|
---|
| 2578 | { $$ = $6->addNewArray( $5 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
|
---|
| 2579 | | new_array_parameter_1st_dimension multi_array_dimension type_specifier
|
---|
| 2580 | { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
|
---|
| 2581 | | multi_array_dimension type_specifier
|
---|
| 2582 | { $$ = $2->addNewArray( $1 ); }
|
---|
| 2583 | | '[' push pop ']' new_identifier_parameter_ptr
|
---|
| 2584 | { $$ = $5->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
|
---|
| 2585 | | new_array_parameter_1st_dimension new_identifier_parameter_ptr
|
---|
| 2586 | { $$ = $2->addNewArray( $1 ); }
|
---|
| 2587 | | '[' push pop ']' multi_array_dimension new_identifier_parameter_ptr
|
---|
| 2588 | { $$ = $6->addNewArray( $5 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
|
---|
| 2589 | | new_array_parameter_1st_dimension multi_array_dimension new_identifier_parameter_ptr
|
---|
| 2590 | { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
|
---|
| 2591 | | multi_array_dimension new_identifier_parameter_ptr
|
---|
| 2592 | { $$ = $2->addNewArray( $1 ); }
|
---|
| 2593 | ;
|
---|
[51b73452] | 2594 |
|
---|
| 2595 | new_array_parameter_1st_dimension:
|
---|
[4d51835] | 2596 | '[' push type_qualifier_list '*' pop ']' // remaining C99
|
---|
| 2597 | { $$ = DeclarationNode::newVarArray( $3 ); }
|
---|
| 2598 | | '[' push type_qualifier_list assignment_expression pop ']'
|
---|
| 2599 | { $$ = DeclarationNode::newArray( $4, $3, false ); }
|
---|
| 2600 | | '[' push declaration_qualifier_list assignment_expression pop ']'
|
---|
| 2601 | // declaration_qualifier_list must be used because of shift/reduce conflict with
|
---|
| 2602 | // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
|
---|
| 2603 | // appear in this context.
|
---|
| 2604 | { $$ = DeclarationNode::newArray( $4, $3, true ); }
|
---|
| 2605 | | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
|
---|
| 2606 | { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
|
---|
| 2607 | ;
|
---|
[b87a5ed] | 2608 |
|
---|
| 2609 | // This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is
|
---|
| 2610 | // no identifier to which the type applies, e.g.:
|
---|
[c11e31c] | 2611 | //
|
---|
[b87a5ed] | 2612 | // [int] f( int ); // abstract variable parameter; no parameter name specified
|
---|
| 2613 | // [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
|
---|
[c11e31c] | 2614 | //
|
---|
| 2615 | // These rules need LR(3):
|
---|
| 2616 | //
|
---|
[b87a5ed] | 2617 | // new_abstract_tuple identifier_or_typedef_name
|
---|
| 2618 | // '[' new_parameter_list ']' identifier_or_typedef_name '(' new_parameter_type_list_opt ')'
|
---|
[c11e31c] | 2619 | //
|
---|
| 2620 | // since a function return type can be syntactically identical to a tuple type:
|
---|
| 2621 | //
|
---|
[b87a5ed] | 2622 | // [int, int] t;
|
---|
| 2623 | // [int, int] f( int );
|
---|
[c11e31c] | 2624 | //
|
---|
| 2625 | // Therefore, it is necessary to look at the token after identifier_or_typedef_name to know when to reduce
|
---|
[b87a5ed] | 2626 | // new_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the
|
---|
| 2627 | // necessary lookahead. To accomplish this, new_abstract_declarator has an entry point without tuple, and
|
---|
| 2628 | // tuple declarations are duplicated when appearing with new_function_specifier.
|
---|
| 2629 |
|
---|
| 2630 | new_abstract_declarator_tuple: // CFA
|
---|
[4d51835] | 2631 | new_abstract_tuple
|
---|
| 2632 | | type_qualifier_list new_abstract_tuple
|
---|
| 2633 | { $$ = $2->addQualifiers( $1 ); }
|
---|
| 2634 | | new_abstract_declarator_no_tuple
|
---|
| 2635 | ;
|
---|
[b87a5ed] | 2636 |
|
---|
| 2637 | new_abstract_declarator_no_tuple: // CFA
|
---|
[4d51835] | 2638 | new_abstract_ptr
|
---|
| 2639 | | new_abstract_array
|
---|
| 2640 | ;
|
---|
[b87a5ed] | 2641 |
|
---|
| 2642 | new_abstract_ptr: // CFA
|
---|
[4d51835] | 2643 | '*' type_specifier
|
---|
| 2644 | { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2645 | | type_qualifier_list '*' type_specifier
|
---|
| 2646 | { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
|
---|
| 2647 | | '*' new_abstract_function
|
---|
| 2648 | { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2649 | | type_qualifier_list '*' new_abstract_function
|
---|
| 2650 | { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
|
---|
| 2651 | | '*' new_abstract_declarator_tuple
|
---|
| 2652 | { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
|
---|
| 2653 | | type_qualifier_list '*' new_abstract_declarator_tuple
|
---|
| 2654 | { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
|
---|
| 2655 | ;
|
---|
[b87a5ed] | 2656 |
|
---|
| 2657 | new_abstract_array: // CFA
|
---|
[4d51835] | 2658 | // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce
|
---|
| 2659 | // conflict with empty (void) function return type.
|
---|
| 2660 | '[' push pop ']' type_specifier
|
---|
| 2661 | { $$ = $5->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
|
---|
| 2662 | | '[' push pop ']' multi_array_dimension type_specifier
|
---|
| 2663 | { $$ = $6->addNewArray( $5 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
|
---|
| 2664 | | multi_array_dimension type_specifier
|
---|
| 2665 | { $$ = $2->addNewArray( $1 ); }
|
---|
| 2666 | | '[' push pop ']' new_abstract_ptr
|
---|
| 2667 | { $$ = $5->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
|
---|
| 2668 | | '[' push pop ']' multi_array_dimension new_abstract_ptr
|
---|
| 2669 | { $$ = $6->addNewArray( $5 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
|
---|
| 2670 | | multi_array_dimension new_abstract_ptr
|
---|
| 2671 | { $$ = $2->addNewArray( $1 ); }
|
---|
| 2672 | ;
|
---|
[b87a5ed] | 2673 |
|
---|
| 2674 | new_abstract_tuple: // CFA
|
---|
[4d51835] | 2675 | '[' push new_abstract_parameter_list pop ']'
|
---|
| 2676 | { $$ = DeclarationNode::newTuple( $3 ); }
|
---|
| 2677 | ;
|
---|
[b87a5ed] | 2678 |
|
---|
| 2679 | new_abstract_function: // CFA
|
---|
[4d51835] | 2680 | '[' push pop ']' '(' new_parameter_type_list_opt ')'
|
---|
| 2681 | { $$ = DeclarationNode::newFunction( 0, DeclarationNode::newTuple( 0 ), $6, 0 ); }
|
---|
| 2682 | | new_abstract_tuple '(' push new_parameter_type_list_opt pop ')'
|
---|
| 2683 | { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
|
---|
| 2684 | | new_function_return '(' push new_parameter_type_list_opt pop ')'
|
---|
| 2685 | { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
|
---|
| 2686 | ;
|
---|
[b87a5ed] | 2687 |
|
---|
| 2688 | // 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration
|
---|
| 2689 | // specifiers in each declaration, and in the specifier-qualifier list in each structure declaration and
|
---|
| 2690 | // type name."
|
---|
[c11e31c] | 2691 | //
|
---|
[b87a5ed] | 2692 | // 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the
|
---|
| 2693 | // beginning of the declaration specifiers in a declaration is an obsolescent feature."
|
---|
[c11e31c] | 2694 | //
|
---|
| 2695 | // 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
|
---|
| 2696 | // prototype-format parameter type declarators) is an obsolescent feature."
|
---|
| 2697 | //
|
---|
[b87a5ed] | 2698 | // 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter
|
---|
| 2699 | // identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an
|
---|
| 2700 | // obsolescent feature.
|
---|
[51b73452] | 2701 |
|
---|
[c11e31c] | 2702 | //************************* MISCELLANEOUS ********************************
|
---|
[51b73452] | 2703 |
|
---|
[b87a5ed] | 2704 | comma_opt: // redundant comma
|
---|
[4d51835] | 2705 | // empty
|
---|
| 2706 | | ','
|
---|
| 2707 | ;
|
---|
[51b73452] | 2708 |
|
---|
| 2709 | assignment_opt:
|
---|
[4d51835] | 2710 | // empty
|
---|
| 2711 | { $$ = 0; }
|
---|
| 2712 | | '=' assignment_expression
|
---|
| 2713 | { $$ = $2; }
|
---|
| 2714 | ;
|
---|
[51b73452] | 2715 |
|
---|
| 2716 | %%
|
---|
[c11e31c] | 2717 | // ----end of grammar----
|
---|
[51b73452] | 2718 |
|
---|
[5f2f2d7] | 2719 | void yyerror( const char * ) {
|
---|
| 2720 | std::cout << "Error ";
|
---|
[b87a5ed] | 2721 | if ( yyfilename ) {
|
---|
[5f2f2d7] | 2722 | std::cout << "in file " << yyfilename << " ";
|
---|
| 2723 | } // if
|
---|
| 2724 | std::cout << "at line " << yylineno << " reading token \"" << *(yylval.tok.str) << "\"" << std::endl;
|
---|
[51b73452] | 2725 | }
|
---|
| 2726 |
|
---|
[c11e31c] | 2727 | // Local Variables: //
|
---|
[b87a5ed] | 2728 | // tab-width: 4 //
|
---|
| 2729 | // mode: c++ //
|
---|
[c11e31c] | 2730 | // compile-command: "make install" //
|
---|
| 2731 | // End: //
|
---|