From 9412fdcc2ad8ee72eef3a88b3cb693447a48e17a Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 8 Mar 2021 07:27:57 -0700 Subject: Convert d-exp.y to use operations This converts the D parser to generate operations rather than exp_elements. gdb/ChangeLog 2021-03-08 Tom Tromey * d-exp.y: Create operations. (d_parse): Update. --- gdb/ChangeLog | 5 ++ gdb/d-exp.y | 249 ++++++++++++++++++++++++++++++---------------------------- 2 files changed, 134 insertions(+), 120 deletions(-) (limited to 'gdb') diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 6262ade..124eaaf 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,10 @@ 2021-03-08 Tom Tromey + * d-exp.y: Create operations. + (d_parse): Update. + +2021-03-08 Tom Tromey + * go-exp.y: Create operations. (go_language::parser): Update. diff --git a/gdb/d-exp.y b/gdb/d-exp.y index c432f22..1a75431 100644 --- a/gdb/d-exp.y +++ b/gdb/d-exp.y @@ -52,6 +52,7 @@ #include "charset.h" #include "block.h" #include "type-stack.h" +#include "expop.h" #define parse_type(ps) builtin_type (ps->gdbarch ()) #define parse_d_type(ps) builtin_d_type (ps->gdbarch ()) @@ -77,6 +78,8 @@ static void yyerror (const char *); static int type_aggregate_p (struct type *); +using namespace expr; + %} /* Although the yacc "value" of an expression is not used, @@ -191,53 +194,63 @@ Expression: CommaExpression: AssignExpression | AssignExpression ',' CommaExpression - { write_exp_elt_opcode (pstate, BINOP_COMMA); } + { pstate->wrap2 (); } ; AssignExpression: ConditionalExpression | ConditionalExpression '=' AssignExpression - { write_exp_elt_opcode (pstate, BINOP_ASSIGN); } + { pstate->wrap2 (); } | ConditionalExpression ASSIGN_MODIFY AssignExpression - { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); - write_exp_elt_opcode (pstate, $2); - write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); } + { + operation_up rhs = pstate->pop (); + operation_up lhs = pstate->pop (); + pstate->push_new + ($2, std::move (lhs), std::move (rhs)); + } ; ConditionalExpression: OrOrExpression | OrOrExpression '?' Expression ':' ConditionalExpression - { write_exp_elt_opcode (pstate, TERNOP_COND); } + { + operation_up last = pstate->pop (); + operation_up mid = pstate->pop (); + operation_up first = pstate->pop (); + pstate->push_new + (std::move (first), std::move (mid), + std::move (last)); + } ; OrOrExpression: AndAndExpression | OrOrExpression OROR AndAndExpression - { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); } + { pstate->wrap2 (); } ; AndAndExpression: OrExpression | AndAndExpression ANDAND OrExpression - { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); } + { pstate->wrap2 (); } ; OrExpression: XorExpression | OrExpression '|' XorExpression - { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); } + { pstate->wrap2 (); } ; XorExpression: AndExpression | XorExpression '^' AndExpression - { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); } + { pstate->wrap2 (); } ; AndExpression: CmpExpression | AndExpression '&' CmpExpression - { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); } + { pstate->wrap2 (); } ; CmpExpression: @@ -249,120 +262,121 @@ CmpExpression: EqualExpression: ShiftExpression EQUAL ShiftExpression - { write_exp_elt_opcode (pstate, BINOP_EQUAL); } + { pstate->wrap2 (); } | ShiftExpression NOTEQUAL ShiftExpression - { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); } + { pstate->wrap2 (); } ; IdentityExpression: ShiftExpression IDENTITY ShiftExpression - { write_exp_elt_opcode (pstate, BINOP_EQUAL); } + { pstate->wrap2 (); } | ShiftExpression NOTIDENTITY ShiftExpression - { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); } + { pstate->wrap2 (); } ; RelExpression: ShiftExpression '<' ShiftExpression - { write_exp_elt_opcode (pstate, BINOP_LESS); } + { pstate->wrap2 (); } | ShiftExpression LEQ ShiftExpression - { write_exp_elt_opcode (pstate, BINOP_LEQ); } + { pstate->wrap2 (); } | ShiftExpression '>' ShiftExpression - { write_exp_elt_opcode (pstate, BINOP_GTR); } + { pstate->wrap2 (); } | ShiftExpression GEQ ShiftExpression - { write_exp_elt_opcode (pstate, BINOP_GEQ); } + { pstate->wrap2 (); } ; ShiftExpression: AddExpression | ShiftExpression LSH AddExpression - { write_exp_elt_opcode (pstate, BINOP_LSH); } + { pstate->wrap2 (); } | ShiftExpression RSH AddExpression - { write_exp_elt_opcode (pstate, BINOP_RSH); } + { pstate->wrap2 (); } ; AddExpression: MulExpression | AddExpression '+' MulExpression - { write_exp_elt_opcode (pstate, BINOP_ADD); } + { pstate->wrap2 (); } | AddExpression '-' MulExpression - { write_exp_elt_opcode (pstate, BINOP_SUB); } + { pstate->wrap2 (); } | AddExpression '~' MulExpression - { write_exp_elt_opcode (pstate, BINOP_CONCAT); } + { pstate->wrap2 (); } ; MulExpression: UnaryExpression | MulExpression '*' UnaryExpression - { write_exp_elt_opcode (pstate, BINOP_MUL); } + { pstate->wrap2 (); } | MulExpression '/' UnaryExpression - { write_exp_elt_opcode (pstate, BINOP_DIV); } + { pstate->wrap2 (); } | MulExpression '%' UnaryExpression - { write_exp_elt_opcode (pstate, BINOP_REM); } + { pstate->wrap2 (); } UnaryExpression: '&' UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_ADDR); } + { pstate->wrap (); } | INCREMENT UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); } + { pstate->wrap (); } | DECREMENT UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); } + { pstate->wrap (); } | '*' UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_IND); } + { pstate->wrap (); } | '-' UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_NEG); } + { pstate->wrap (); } | '+' UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_PLUS); } + { pstate->wrap (); } | '!' UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); } + { pstate->wrap (); } | '~' UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); } + { pstate->wrap (); } | TypeExp '.' SIZEOF_KEYWORD - { write_exp_elt_opcode (pstate, UNOP_SIZEOF); } + { pstate->wrap (); } | CastExpression | PowExpression ; CastExpression: CAST_KEYWORD '(' TypeExp ')' UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); } + { pstate->wrap2 (); } /* C style cast is illegal D, but is still recognised in the grammar, so we keep this around for convenience. */ | '(' TypeExp ')' UnaryExpression - { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); } - + { pstate->wrap2 (); } ; PowExpression: PostfixExpression | PostfixExpression HATHAT UnaryExpression - { write_exp_elt_opcode (pstate, BINOP_EXP); } + { pstate->wrap2 (); } ; PostfixExpression: PrimaryExpression | PostfixExpression '.' COMPLETE - { struct stoken s; - pstate->mark_struct_expression (); - write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); - s.ptr = ""; - s.length = 0; - write_exp_string (pstate, s); - write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); } + { + structop_base_operation *op + = new structop_ptr_operation (pstate->pop (), ""); + pstate->mark_struct_expression (op); + pstate->push (operation_up (op)); + } | PostfixExpression '.' IDENTIFIER - { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); - write_exp_string (pstate, $3); - write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); } + { + pstate->push_new + (pstate->pop (), copy_name ($3)); + } | PostfixExpression '.' IDENTIFIER COMPLETE - { pstate->mark_struct_expression (); - write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); - write_exp_string (pstate, $3); - write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); } + { + structop_base_operation *op + = new structop_operation (pstate->pop (), copy_name ($3)); + pstate->mark_struct_expression (op); + pstate->push (operation_up (op)); + } | PostfixExpression '.' SIZEOF_KEYWORD - { write_exp_elt_opcode (pstate, UNOP_SIZEOF); } + { pstate->wrap (); } | PostfixExpression INCREMENT - { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); } + { pstate->wrap (); } | PostfixExpression DECREMENT - { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); } + { pstate->wrap (); } | CallExpression | IndexExpression | SliceExpression @@ -385,21 +399,25 @@ CallExpression: PostfixExpression '(' { pstate->start_arglist (); } ArgumentList_opt ')' - { write_exp_elt_opcode (pstate, OP_FUNCALL); - write_exp_elt_longcst (pstate, pstate->end_arglist ()); - write_exp_elt_opcode (pstate, OP_FUNCALL); } + { + std::vector args + = pstate->pop_vector (pstate->end_arglist ()); + pstate->push_new + (pstate->pop (), std::move (args)); + } ; IndexExpression: PostfixExpression '[' ArgumentList ']' { if (pstate->arglist_len > 0) { - write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT); - write_exp_elt_longcst (pstate, pstate->arglist_len); - write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT); + std::vector args + = pstate->pop_vector (pstate->arglist_len); + pstate->push_new + (pstate->pop (), std::move (args)); } else - write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); + pstate->wrap2 (); } ; @@ -407,7 +425,14 @@ SliceExpression: PostfixExpression '[' ']' { /* Do nothing. */ } | PostfixExpression '[' AssignExpression DOTDOT AssignExpression ']' - { write_exp_elt_opcode (pstate, TERNOP_SLICE); } + { + operation_up last = pstate->pop (); + operation_up mid = pstate->pop (); + operation_up first = pstate->pop (); + pstate->push_new + (std::move (first), std::move (mid), + std::move (last)); + } ; PrimaryExpression: @@ -427,28 +452,26 @@ PrimaryExpression: { if (symbol_read_needs_frame (sym.symbol)) pstate->block_tracker->update (sym); - write_exp_elt_opcode (pstate, OP_VAR_VALUE); - write_exp_elt_block (pstate, sym.block); - write_exp_elt_sym (pstate, sym.symbol); - write_exp_elt_opcode (pstate, OP_VAR_VALUE); + pstate->push_new (sym.symbol, + sym.block); } else if (is_a_field_of_this.type != NULL) { /* It hangs off of `this'. Must not inadvertently convert from a method call to data ref. */ pstate->block_tracker->update (sym); - write_exp_elt_opcode (pstate, OP_THIS); - write_exp_elt_opcode (pstate, OP_THIS); - write_exp_elt_opcode (pstate, STRUCTOP_PTR); - write_exp_string (pstate, $1); - write_exp_elt_opcode (pstate, STRUCTOP_PTR); + operation_up thisop + = make_operation (); + pstate->push_new + (std::move (thisop), std::move (copy)); } else { /* Lookup foreign name in global static symbols. */ msymbol = lookup_bound_minimal_symbol (copy.c_str ()); if (msymbol.minsym != NULL) - write_exp_msymbol (pstate, msymbol); + pstate->push_new + (msymbol.minsym, msymbol.objfile); else if (!have_full_symbols () && !have_partial_symbols ()) error (_("No symbol table is loaded. Use the \"file\" command")); else @@ -476,9 +499,7 @@ PrimaryExpression: lookup_symbol (name.c_str (), (const struct block *) NULL, VAR_DOMAIN, NULL); - write_exp_symbol_reference (pstate, - name.c_str (), - sym); + pstate->push_symbol (name.c_str (), sym); } else { @@ -488,65 +509,54 @@ PrimaryExpression: error (_("`%s' is not defined as an aggregate type."), TYPE_SAFE_NAME (type)); - write_exp_elt_opcode (pstate, OP_SCOPE); - write_exp_elt_type (pstate, type); - write_exp_string (pstate, $3); - write_exp_elt_opcode (pstate, OP_SCOPE); + pstate->push_new + (type, copy_name ($3)); } } | DOLLAR_VARIABLE - { write_dollar_variable (pstate, $1); } + { pstate->push_dollar ($1); } | NAME_OR_INT { YYSTYPE val; parse_number (pstate, $1.ptr, $1.length, 0, &val); - write_exp_elt_opcode (pstate, OP_LONG); - write_exp_elt_type (pstate, val.typed_val_int.type); - write_exp_elt_longcst (pstate, - (LONGEST) val.typed_val_int.val); - write_exp_elt_opcode (pstate, OP_LONG); } + pstate->push_new + (val.typed_val_int.type, val.typed_val_int.val); } | NULL_KEYWORD { struct type *type = parse_d_type (pstate)->builtin_void; type = lookup_pointer_type (type); - write_exp_elt_opcode (pstate, OP_LONG); - write_exp_elt_type (pstate, type); - write_exp_elt_longcst (pstate, (LONGEST) 0); - write_exp_elt_opcode (pstate, OP_LONG); } + pstate->push_new (type, 0); } | TRUE_KEYWORD - { write_exp_elt_opcode (pstate, OP_BOOL); - write_exp_elt_longcst (pstate, (LONGEST) 1); - write_exp_elt_opcode (pstate, OP_BOOL); } + { pstate->push_new (true); } | FALSE_KEYWORD - { write_exp_elt_opcode (pstate, OP_BOOL); - write_exp_elt_longcst (pstate, (LONGEST) 0); - write_exp_elt_opcode (pstate, OP_BOOL); } + { pstate->push_new (false); } | INTEGER_LITERAL - { write_exp_elt_opcode (pstate, OP_LONG); - write_exp_elt_type (pstate, $1.type); - write_exp_elt_longcst (pstate, (LONGEST)($1.val)); - write_exp_elt_opcode (pstate, OP_LONG); } + { pstate->push_new ($1.type, $1.val); } | FLOAT_LITERAL - { write_exp_elt_opcode (pstate, OP_FLOAT); - write_exp_elt_type (pstate, $1.type); - write_exp_elt_floatcst (pstate, $1.val); - write_exp_elt_opcode (pstate, OP_FLOAT); } + { + float_data data; + std::copy (std::begin ($1.val), std::end ($1.val), + std::begin (data)); + pstate->push_new ($1.type, data); + } | CHARACTER_LITERAL { struct stoken_vector vec; vec.len = 1; vec.tokens = &$1; - write_exp_string_vector (pstate, $1.type, &vec); } + pstate->push_c_string (0, &vec); } | StringExp { int i; - write_exp_string_vector (pstate, 0, &$1); + pstate->push_c_string (0, &$1); for (i = 0; i < $1.len; ++i) free ($1.tokens[i].ptr); free ($1.tokens); } | ArrayLiteral - { write_exp_elt_opcode (pstate, OP_ARRAY); - write_exp_elt_longcst (pstate, (LONGEST) 0); - write_exp_elt_longcst (pstate, (LONGEST) $1 - 1); - write_exp_elt_opcode (pstate, OP_ARRAY); } + { + std::vector args + = pstate->pop_vector ($1); + pstate->push_new + (0, $1 - 1, std::move (args)); + } | TYPEOF_KEYWORD '(' Expression ')' - { write_exp_elt_opcode (pstate, OP_TYPEOF); } + { pstate->wrap (); } ; ArrayLiteral: @@ -595,14 +605,10 @@ TypeExp: '(' TypeExp ')' { /* Do nothing. */ } | BasicType - { write_exp_elt_opcode (pstate, OP_TYPE); - write_exp_elt_type (pstate, $1); - write_exp_elt_opcode (pstate, OP_TYPE); } + { pstate->push_new ($1); } | BasicType BasicType2 { $$ = type_stack->follow_types ($1); - write_exp_elt_opcode (pstate, OP_TYPE); - write_exp_elt_type (pstate, $$); - write_exp_elt_opcode (pstate, OP_TYPE); + pstate->push_new ($$); } ; @@ -1622,7 +1628,10 @@ d_parse (struct parser_state *par_state) popping = 0; name_obstack.clear (); - return yyparse (); + int result = yyparse (); + if (!result) + pstate->set_operation (pstate->pop ()); + return result; } static void -- cgit v1.1