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