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