Extract symbol-writing function from parsers
authorTom Tromey <tom@tromey.com>
Fri, 5 Feb 2021 14:11:01 +0000 (07:11 -0700)
committerTom Tromey <tom@tromey.com>
Fri, 5 Feb 2021 14:11:01 +0000 (07:11 -0700)
I noticed that several parsers shared the same code to write a symbol
reference to an expression.  This patch factors this code out into a
new function.

Regression tested on x86-64 Fedora 32.

gdb/ChangeLog
2021-02-05  Tom Tromey  <tom@tromey.com>

* parser-defs.h (write_exp_symbol_reference): Declare.
* parse.c (write_exp_symbol_reference): New function.
* p-exp.y (variable): Use write_exp_symbol_reference.
* m2-exp.y (variable): Use write_exp_symbol_reference.
* f-exp.y (variable): Use write_exp_symbol_reference.
* d-exp.y (PrimaryExpression): Use write_exp_symbol_reference.
* c-exp.y (variable): Use write_exp_symbol_reference.

gdb/ChangeLog
gdb/c-exp.y
gdb/d-exp.y
gdb/f-exp.y
gdb/m2-exp.y
gdb/p-exp.y
gdb/parse.c
gdb/parser-defs.h

index 07fe6a0184485c31538b6eb99b6d3b41b8f3a319..553b5f03056a879dd485c303189918a824f4bb6d 100644 (file)
@@ -1,3 +1,13 @@
+2021-02-05  Tom Tromey  <tom@tromey.com>
+
+       * parser-defs.h (write_exp_symbol_reference): Declare.
+       * parse.c (write_exp_symbol_reference): New function.
+       * p-exp.y (variable): Use write_exp_symbol_reference.
+       * m2-exp.y (variable): Use write_exp_symbol_reference.
+       * f-exp.y (variable): Use write_exp_symbol_reference.
+       * d-exp.y (PrimaryExpression): Use write_exp_symbol_reference.
+       * c-exp.y (variable): Use write_exp_symbol_reference.
+
 2021-02-05  Tom de Vries  <tdevries@suse.de>
 
        PR exp/27265
index 71610e909682a804b14e67a78fe0e47f558295a6..13b06f39bbf02f3f1fa341f17f68d98540d151e2 100644 (file)
@@ -1104,30 +1104,12 @@ variable:       qualified_name
        |       COLONCOLON name_not_typename
                        {
                          std::string name = copy_name ($2.stoken);
-                         struct symbol *sym;
-                         struct bound_minimal_symbol msymbol;
-
-                         sym
+                         struct block_symbol sym
                            = lookup_symbol (name.c_str (),
                                             (const struct block *) NULL,
-                                            VAR_DOMAIN, NULL).symbol;
-                         if (sym)
-                           {
-                             write_exp_elt_opcode (pstate, OP_VAR_VALUE);
-                             write_exp_elt_block (pstate, NULL);
-                             write_exp_elt_sym (pstate, sym);
-                             write_exp_elt_opcode (pstate, OP_VAR_VALUE);
-                             break;
-                           }
-
-                         msymbol = lookup_bound_minimal_symbol (name.c_str ());
-                         if (msymbol.minsym != NULL)
-                           write_exp_msymbol (pstate, msymbol);
-                         else if (!have_full_symbols () && !have_partial_symbols ())
-                           error (_("No symbol table is loaded.  Use the \"file\" command."));
-                         else
-                           error (_("No symbol \"%s\" in current context."),
-                                  name.c_str ());
+                                            VAR_DOMAIN, NULL);
+                         write_exp_symbol_reference (pstate, name.c_str (),
+                                                     sym);
                        }
        ;
 
index 35cd07413d59cfc52ecb5d32dfdb1727e5374f48..c432f22bd9d0d21bb01cfc25d16a6867c58580eb 100644 (file)
@@ -464,7 +464,6 @@ PrimaryExpression:
                             been resolved, it's not likely to be found.  */
                          if (type->code () == TYPE_CODE_MODULE)
                            {
-                             struct bound_minimal_symbol msymbol;
                              struct block_symbol sym;
                              const char *type_name = TYPE_SAFE_NAME (type);
                              int type_name_len = strlen (type_name);
@@ -477,35 +476,23 @@ PrimaryExpression:
                                lookup_symbol (name.c_str (),
                                               (const struct block *) NULL,
                                               VAR_DOMAIN, NULL);
-                             if (sym.symbol)
-                               {
-                                 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);
-                                 break;
-                               }
-
-                             msymbol = lookup_bound_minimal_symbol (name.c_str ());
-                             if (msymbol.minsym != NULL)
-                               write_exp_msymbol (pstate, msymbol);
-                             else if (!have_full_symbols () && !have_partial_symbols ())
-                               error (_("No symbol table is loaded.  Use the \"file\" command."));
-                             else
-                               error (_("No symbol \"%s\" in current context."),
-                                      name.c_str ());
+                             write_exp_symbol_reference (pstate,
+                                                         name.c_str (),
+                                                         sym);
+                           }
+                         else
+                           {
+                             /* Check if the qualified name resolves as a member
+                                of an aggregate or an enum type.  */
+                             if (!type_aggregate_p (type))
+                               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);
                            }
-
-                         /* Check if the qualified name resolves as a member
-                            of an aggregate or an enum type.  */
-                         if (!type_aggregate_p (type))
-                           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);
                        }
 |      DOLLAR_VARIABLE
                { write_dollar_variable (pstate, $1); }
index 3b0f23d5d730d55c6b6e8dc2c397679aea95835d..92a70b4552da0cbe11a7a7214e2ebb500e34d75f 100644 (file)
@@ -540,32 +540,9 @@ exp        :       STRING_LITERAL
 
 variable:      name_not_typename
                        { struct block_symbol sym = $1.sym;
-
-                         if (sym.symbol)
-                           {
-                             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);
-                             break;
-                           }
-                         else
-                           {
-                             struct bound_minimal_symbol msymbol;
-                             std::string arg = copy_name ($1.stoken);
-
-                             msymbol =
-                               lookup_bound_minimal_symbol (arg.c_str ());
-                             if (msymbol.minsym != NULL)
-                               write_exp_msymbol (pstate, msymbol);
-                             else if (!have_full_symbols () && !have_partial_symbols ())
-                               error (_("No symbol table is loaded.  Use the \"file\" command."));
-                             else
-                               error (_("No symbol \"%s\" in current context."),
-                                      arg.c_str ());
-                           }
+                         std::string name = copy_name ($1.stoken);
+                         write_exp_symbol_reference (pstate, name.c_str (),
+                                                     sym);
                        }
        ;
 
index 717c6e69e95cd4d65661d2234ca3afdc9aee1165..68bae48feb6b446180ded985b7258e0608aa9ebc 100644 (file)
@@ -561,37 +561,15 @@ variable: NAME
                        { struct block_symbol sym;
                          struct field_of_this_result is_a_field_of_this;
 
+                         std::string name = copy_name ($1);
                          sym
-                           = lookup_symbol (copy_name ($1).c_str (),
+                           = lookup_symbol (name.c_str (),
                                             pstate->expression_context_block,
                                             VAR_DOMAIN,
                                             &is_a_field_of_this);
 
-                         if (sym.symbol)
-                           {
-                             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);
-                           }
-                         else
-                           {
-                             struct bound_minimal_symbol msymbol;
-                             std::string arg = copy_name ($1);
-
-                             msymbol =
-                               lookup_bound_minimal_symbol (arg.c_str ());
-                             if (msymbol.minsym != NULL)
-                               write_exp_msymbol (pstate, msymbol);
-                             else if (!have_full_symbols () && !have_partial_symbols ())
-                               error (_("No symbol table is loaded.  Use the \"symbol-file\" command."));
-                             else
-                               error (_("No symbol \"%s\" in current context."),
-                                      arg.c_str ());
-                           }
+                         write_exp_symbol_reference (pstate, name.c_str (),
+                                                     sym);
                        }
        ;
 
index 5a43e89052403feb9dbe49a4077a5fe61c92b51c..b025ac36070d9a88950305c5998c633a936fa223 100644 (file)
@@ -691,33 +691,12 @@ variable: qualified_name
        |       COLONCOLON name
                        {
                          std::string name = copy_name ($2);
-                         struct symbol *sym;
-                         struct bound_minimal_symbol msymbol;
-
-                         sym =
-                           lookup_symbol (name.c_str (),
-                                          (const struct block *) NULL,
-                                          VAR_DOMAIN, NULL).symbol;
-                         if (sym)
-                           {
-                             write_exp_elt_opcode (pstate, OP_VAR_VALUE);
-                             write_exp_elt_block (pstate, NULL);
-                             write_exp_elt_sym (pstate, sym);
-                             write_exp_elt_opcode (pstate, OP_VAR_VALUE);
-                             break;
-                           }
 
-                         msymbol
-                           = lookup_bound_minimal_symbol (name.c_str ());
-                         if (msymbol.minsym != NULL)
-                           write_exp_msymbol (pstate, msymbol);
-                         else if (!have_full_symbols ()
-                                  && !have_partial_symbols ())
-                           error (_("No symbol table is loaded.  "
-                                  "Use the \"file\" command."));
-                         else
-                           error (_("No symbol \"%s\" in current context."),
-                                  name.c_str ());
+                         struct block_symbol sym
+                           = lookup_symbol (name.c_str (), nullptr,
+                                            VAR_DOMAIN, nullptr);
+                         write_exp_symbol_reference (pstate, name.c_str (),
+                                                     sym);
                        }
        ;
 
index 933960fa30ce5cba3e19c93dc294b98045a94473..08fde89d8f3d9b6da51eca01d326ffe1aa818f0b 100644 (file)
@@ -650,6 +650,32 @@ handle_register:
   return;
 }
 
+/* See parser-defs.h.  */
+
+void
+write_exp_symbol_reference (struct parser_state *pstate, const char *name,
+                           struct block_symbol sym)
+{
+  if (sym.symbol != nullptr)
+    {
+      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, NULL);
+      write_exp_elt_sym (pstate, sym.symbol);
+      write_exp_elt_opcode (pstate, OP_VAR_VALUE);
+    }
+  else
+    {
+      struct bound_minimal_symbol msymbol = lookup_bound_minimal_symbol (name);
+      if (msymbol.minsym != NULL)
+       write_exp_msymbol (pstate, msymbol);
+      else if (!have_full_symbols () && !have_partial_symbols ())
+       error (_("No symbol table is loaded.  Use the \"file\" command."));
+      else
+       error (_("No symbol \"%s\" in current context."), name);
+    }
+}
 
 const char *
 find_template_name_end (const char *p)
index 5ff5d52240564fd5c69dc3d7bbe0ddb4ddc47fb3..466230e4ad09408a55689a672f4ff334c9644857 100644 (file)
@@ -334,6 +334,16 @@ extern void write_exp_msymbol (struct expr_builder *,
 
 extern void write_dollar_variable (struct parser_state *, struct stoken str);
 
+/* Write a reference to a symbol to the expression being built in PS.
+   NAME is the name of the symbol to write; SYM is the symbol.  If SYM
+   is nullptr (meaning the 'symbol' member), a minimal symbol will be
+   searched for and used if available.  Throws an exception if SYM is
+   nullptr and no minimal symbol can be found.  */
+
+extern void write_exp_symbol_reference (struct parser_state *ps,
+                                       const char *name,
+                                       struct block_symbol sym);
+
 extern const char *find_template_name_end (const char *);
 
 extern std::string copy_name (struct stoken);