GDB (API)
/home/stan/gdb/src/gdb/parse.c
Go to the documentation of this file.
00001 /* Parse expressions for GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    Modified from expread.y by the Department of Computer Science at the
00006    State University of New York at Buffalo, 1991.
00007 
00008    This file is part of GDB.
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 3 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License
00021    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00022 
00023 /* Parse an expression from text in a string,
00024    and return the result as a struct expression pointer.
00025    That structure contains arithmetic operations in reverse polish,
00026    with constants represented by operations that are followed by special data.
00027    See expression.h for the details of the format.
00028    What is important here is that it can be built up sequentially
00029    during the process of parsing; the lower levels of the tree always
00030    come first in the result.  */
00031 
00032 #include "defs.h"
00033 #include <ctype.h>
00034 #include "arch-utils.h"
00035 #include "gdb_string.h"
00036 #include "symtab.h"
00037 #include "gdbtypes.h"
00038 #include "frame.h"
00039 #include "expression.h"
00040 #include "value.h"
00041 #include "command.h"
00042 #include "language.h"
00043 #include "f-lang.h"
00044 #include "parser-defs.h"
00045 #include "gdbcmd.h"
00046 #include "symfile.h"            /* for overlay functions */
00047 #include "inferior.h"
00048 #include "doublest.h"
00049 #include "gdb_assert.h"
00050 #include "block.h"
00051 #include "source.h"
00052 #include "objfiles.h"
00053 #include "exceptions.h"
00054 #include "user-regs.h"
00055 
00056 /* Standard set of definitions for printing, dumping, prefixifying,
00057  * and evaluating expressions.  */
00058 
00059 const struct exp_descriptor exp_descriptor_standard = 
00060   {
00061     print_subexp_standard,
00062     operator_length_standard,
00063     operator_check_standard,
00064     op_name_standard,
00065     dump_subexp_body_standard,
00066     evaluate_subexp_standard
00067   };
00068 
00069 /* Global variables declared in parser-defs.h (and commented there).  */
00070 struct expression *expout;
00071 int expout_size;
00072 int expout_ptr;
00073 const struct block *expression_context_block;
00074 CORE_ADDR expression_context_pc;
00075 const struct block *innermost_block;
00076 int arglist_len;
00077 static struct type_stack type_stack;
00078 const char *lexptr;
00079 const char *prev_lexptr;
00080 int paren_depth;
00081 int comma_terminates;
00082 
00083 /* True if parsing an expression to attempt completion.  */
00084 int parse_completion;
00085 
00086 /* The index of the last struct expression directly before a '.' or
00087    '->'.  This is set when parsing and is only used when completing a
00088    field name.  It is -1 if no dereference operation was found.  */
00089 static int expout_last_struct = -1;
00090 
00091 /* If we are completing a tagged type name, this will be nonzero.  */
00092 static enum type_code expout_tag_completion_type = TYPE_CODE_UNDEF;
00093 
00094 /* The token for tagged type name completion.  */
00095 static char *expout_completion_name;
00096 
00097 
00098 static unsigned int expressiondebug = 0;
00099 static void
00100 show_expressiondebug (struct ui_file *file, int from_tty,
00101                       struct cmd_list_element *c, const char *value)
00102 {
00103   fprintf_filtered (file, _("Expression debugging is %s.\n"), value);
00104 }
00105 
00106 
00107 /* Non-zero if an expression parser should set yydebug.  */
00108 int parser_debug;
00109 
00110 static void
00111 show_parserdebug (struct ui_file *file, int from_tty,
00112                   struct cmd_list_element *c, const char *value)
00113 {
00114   fprintf_filtered (file, _("Parser debugging is %s.\n"), value);
00115 }
00116 
00117 
00118 static void free_funcalls (void *ignore);
00119 
00120 static int prefixify_subexp (struct expression *, struct expression *, int,
00121                              int);
00122 
00123 static struct expression *parse_exp_in_context (const char **, CORE_ADDR,
00124                                                 const struct block *, int, 
00125                                                 int, int *);
00126 static struct expression *parse_exp_in_context_1 (const char **, CORE_ADDR,
00127                                                   const struct block *, int,
00128                                                   int, int *);
00129 
00130 void _initialize_parse (void);
00131 
00132 /* Data structure for saving values of arglist_len for function calls whose
00133    arguments contain other function calls.  */
00134 
00135 struct funcall
00136   {
00137     struct funcall *next;
00138     int arglist_len;
00139   };
00140 
00141 static struct funcall *funcall_chain;
00142 
00143 /* Begin counting arguments for a function call,
00144    saving the data about any containing call.  */
00145 
00146 void
00147 start_arglist (void)
00148 {
00149   struct funcall *new;
00150 
00151   new = (struct funcall *) xmalloc (sizeof (struct funcall));
00152   new->next = funcall_chain;
00153   new->arglist_len = arglist_len;
00154   arglist_len = 0;
00155   funcall_chain = new;
00156 }
00157 
00158 /* Return the number of arguments in a function call just terminated,
00159    and restore the data for the containing function call.  */
00160 
00161 int
00162 end_arglist (void)
00163 {
00164   int val = arglist_len;
00165   struct funcall *call = funcall_chain;
00166 
00167   funcall_chain = call->next;
00168   arglist_len = call->arglist_len;
00169   xfree (call);
00170   return val;
00171 }
00172 
00173 /* Free everything in the funcall chain.
00174    Used when there is an error inside parsing.  */
00175 
00176 static void
00177 free_funcalls (void *ignore)
00178 {
00179   struct funcall *call, *next;
00180 
00181   for (call = funcall_chain; call; call = next)
00182     {
00183       next = call->next;
00184       xfree (call);
00185     }
00186 }
00187 
00188 /* This page contains the functions for adding data to the struct expression
00189    being constructed.  */
00190 
00191 /* See definition in parser-defs.h.  */
00192 
00193 void
00194 initialize_expout (int initial_size, const struct language_defn *lang,
00195                    struct gdbarch *gdbarch)
00196 {
00197   expout_size = initial_size;
00198   expout_ptr = 0;
00199   expout = xmalloc (sizeof (struct expression)
00200                     + EXP_ELEM_TO_BYTES (expout_size));
00201   expout->language_defn = lang;
00202   expout->gdbarch = gdbarch;
00203 }
00204 
00205 /* See definition in parser-defs.h.  */
00206 
00207 void
00208 reallocate_expout (void)
00209 {
00210   /* Record the actual number of expression elements, and then
00211      reallocate the expression memory so that we free up any
00212      excess elements.  */
00213 
00214   expout->nelts = expout_ptr;
00215   expout = xrealloc ((char *) expout,
00216                      sizeof (struct expression)
00217                      + EXP_ELEM_TO_BYTES (expout_ptr));
00218 }
00219 
00220 /* Add one element to the end of the expression.  */
00221 
00222 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
00223    a register through here.  */
00224 
00225 static void
00226 write_exp_elt (const union exp_element *expelt)
00227 {
00228   if (expout_ptr >= expout_size)
00229     {
00230       expout_size *= 2;
00231       expout = (struct expression *)
00232         xrealloc ((char *) expout, sizeof (struct expression)
00233                   + EXP_ELEM_TO_BYTES (expout_size));
00234     }
00235   expout->elts[expout_ptr++] = *expelt;
00236 }
00237 
00238 void
00239 write_exp_elt_opcode (enum exp_opcode expelt)
00240 {
00241   union exp_element tmp;
00242 
00243   memset (&tmp, 0, sizeof (union exp_element));
00244   tmp.opcode = expelt;
00245   write_exp_elt (&tmp);
00246 }
00247 
00248 void
00249 write_exp_elt_sym (struct symbol *expelt)
00250 {
00251   union exp_element tmp;
00252 
00253   memset (&tmp, 0, sizeof (union exp_element));
00254   tmp.symbol = expelt;
00255   write_exp_elt (&tmp);
00256 }
00257 
00258 void
00259 write_exp_elt_block (const struct block *b)
00260 {
00261   union exp_element tmp;
00262 
00263   memset (&tmp, 0, sizeof (union exp_element));
00264   tmp.block = b;
00265   write_exp_elt (&tmp);
00266 }
00267 
00268 void
00269 write_exp_elt_objfile (struct objfile *objfile)
00270 {
00271   union exp_element tmp;
00272 
00273   memset (&tmp, 0, sizeof (union exp_element));
00274   tmp.objfile = objfile;
00275   write_exp_elt (&tmp);
00276 }
00277 
00278 void
00279 write_exp_elt_longcst (LONGEST expelt)
00280 {
00281   union exp_element tmp;
00282 
00283   memset (&tmp, 0, sizeof (union exp_element));
00284   tmp.longconst = expelt;
00285   write_exp_elt (&tmp);
00286 }
00287 
00288 void
00289 write_exp_elt_dblcst (DOUBLEST expelt)
00290 {
00291   union exp_element tmp;
00292 
00293   memset (&tmp, 0, sizeof (union exp_element));
00294   tmp.doubleconst = expelt;
00295   write_exp_elt (&tmp);
00296 }
00297 
00298 void
00299 write_exp_elt_decfloatcst (gdb_byte expelt[16])
00300 {
00301   union exp_element tmp;
00302   int index;
00303 
00304   for (index = 0; index < 16; index++)
00305     tmp.decfloatconst[index] = expelt[index];
00306 
00307   write_exp_elt (&tmp);
00308 }
00309 
00310 void
00311 write_exp_elt_type (struct type *expelt)
00312 {
00313   union exp_element tmp;
00314 
00315   memset (&tmp, 0, sizeof (union exp_element));
00316   tmp.type = expelt;
00317   write_exp_elt (&tmp);
00318 }
00319 
00320 void
00321 write_exp_elt_intern (struct internalvar *expelt)
00322 {
00323   union exp_element tmp;
00324 
00325   memset (&tmp, 0, sizeof (union exp_element));
00326   tmp.internalvar = expelt;
00327   write_exp_elt (&tmp);
00328 }
00329 
00330 /* Add a string constant to the end of the expression.
00331 
00332    String constants are stored by first writing an expression element
00333    that contains the length of the string, then stuffing the string
00334    constant itself into however many expression elements are needed
00335    to hold it, and then writing another expression element that contains
00336    the length of the string.  I.e. an expression element at each end of
00337    the string records the string length, so you can skip over the 
00338    expression elements containing the actual string bytes from either
00339    end of the string.  Note that this also allows gdb to handle
00340    strings with embedded null bytes, as is required for some languages.
00341 
00342    Don't be fooled by the fact that the string is null byte terminated,
00343    this is strictly for the convenience of debugging gdb itself.
00344    Gdb does not depend up the string being null terminated, since the
00345    actual length is recorded in expression elements at each end of the
00346    string.  The null byte is taken into consideration when computing how
00347    many expression elements are required to hold the string constant, of
00348    course.  */
00349 
00350 
00351 void
00352 write_exp_string (struct stoken str)
00353 {
00354   int len = str.length;
00355   int lenelt;
00356   char *strdata;
00357 
00358   /* Compute the number of expression elements required to hold the string
00359      (including a null byte terminator), along with one expression element
00360      at each end to record the actual string length (not including the
00361      null byte terminator).  */
00362 
00363   lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
00364 
00365   /* Ensure that we have enough available expression elements to store
00366      everything.  */
00367 
00368   if ((expout_ptr + lenelt) >= expout_size)
00369     {
00370       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
00371       expout = (struct expression *)
00372         xrealloc ((char *) expout, (sizeof (struct expression)
00373                                     + EXP_ELEM_TO_BYTES (expout_size)));
00374     }
00375 
00376   /* Write the leading length expression element (which advances the current
00377      expression element index), then write the string constant followed by a
00378      terminating null byte, and then write the trailing length expression
00379      element.  */
00380 
00381   write_exp_elt_longcst ((LONGEST) len);
00382   strdata = (char *) &expout->elts[expout_ptr];
00383   memcpy (strdata, str.ptr, len);
00384   *(strdata + len) = '\0';
00385   expout_ptr += lenelt - 2;
00386   write_exp_elt_longcst ((LONGEST) len);
00387 }
00388 
00389 /* Add a vector of string constants to the end of the expression.
00390 
00391    This adds an OP_STRING operation, but encodes the contents
00392    differently from write_exp_string.  The language is expected to
00393    handle evaluation of this expression itself.
00394    
00395    After the usual OP_STRING header, TYPE is written into the
00396    expression as a long constant.  The interpretation of this field is
00397    up to the language evaluator.
00398    
00399    Next, each string in VEC is written.  The length is written as a
00400    long constant, followed by the contents of the string.  */
00401 
00402 void
00403 write_exp_string_vector (int type, struct stoken_vector *vec)
00404 {
00405   int i, n_slots, len;
00406 
00407   /* Compute the size.  We compute the size in number of slots to
00408      avoid issues with string padding.  */
00409   n_slots = 0;
00410   for (i = 0; i < vec->len; ++i)
00411     {
00412       /* One slot for the length of this element, plus the number of
00413          slots needed for this string.  */
00414       n_slots += 1 + BYTES_TO_EXP_ELEM (vec->tokens[i].length);
00415     }
00416 
00417   /* One more slot for the type of the string.  */
00418   ++n_slots;
00419 
00420   /* Now compute a phony string length.  */
00421   len = EXP_ELEM_TO_BYTES (n_slots) - 1;
00422 
00423   n_slots += 4;
00424   if ((expout_ptr + n_slots) >= expout_size)
00425     {
00426       expout_size = max (expout_size * 2, expout_ptr + n_slots + 10);
00427       expout = (struct expression *)
00428         xrealloc ((char *) expout, (sizeof (struct expression)
00429                                     + EXP_ELEM_TO_BYTES (expout_size)));
00430     }
00431 
00432   write_exp_elt_opcode (OP_STRING);
00433   write_exp_elt_longcst (len);
00434   write_exp_elt_longcst (type);
00435 
00436   for (i = 0; i < vec->len; ++i)
00437     {
00438       write_exp_elt_longcst (vec->tokens[i].length);
00439       memcpy (&expout->elts[expout_ptr], vec->tokens[i].ptr,
00440               vec->tokens[i].length);
00441       expout_ptr += BYTES_TO_EXP_ELEM (vec->tokens[i].length);
00442     }
00443 
00444   write_exp_elt_longcst (len);
00445   write_exp_elt_opcode (OP_STRING);
00446 }
00447 
00448 /* Add a bitstring constant to the end of the expression.
00449 
00450    Bitstring constants are stored by first writing an expression element
00451    that contains the length of the bitstring (in bits), then stuffing the
00452    bitstring constant itself into however many expression elements are
00453    needed to hold it, and then writing another expression element that
00454    contains the length of the bitstring.  I.e. an expression element at
00455    each end of the bitstring records the bitstring length, so you can skip
00456    over the expression elements containing the actual bitstring bytes from
00457    either end of the bitstring.  */
00458 
00459 void
00460 write_exp_bitstring (struct stoken str)
00461 {
00462   int bits = str.length;        /* length in bits */
00463   int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
00464   int lenelt;
00465   char *strdata;
00466 
00467   /* Compute the number of expression elements required to hold the bitstring,
00468      along with one expression element at each end to record the actual
00469      bitstring length in bits.  */
00470 
00471   lenelt = 2 + BYTES_TO_EXP_ELEM (len);
00472 
00473   /* Ensure that we have enough available expression elements to store
00474      everything.  */
00475 
00476   if ((expout_ptr + lenelt) >= expout_size)
00477     {
00478       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
00479       expout = (struct expression *)
00480         xrealloc ((char *) expout, (sizeof (struct expression)
00481                                     + EXP_ELEM_TO_BYTES (expout_size)));
00482     }
00483 
00484   /* Write the leading length expression element (which advances the current
00485      expression element index), then write the bitstring constant, and then
00486      write the trailing length expression element.  */
00487 
00488   write_exp_elt_longcst ((LONGEST) bits);
00489   strdata = (char *) &expout->elts[expout_ptr];
00490   memcpy (strdata, str.ptr, len);
00491   expout_ptr += lenelt - 2;
00492   write_exp_elt_longcst ((LONGEST) bits);
00493 }
00494 
00495 /* Add the appropriate elements for a minimal symbol to the end of
00496    the expression.  */
00497 
00498 void
00499 write_exp_msymbol (struct bound_minimal_symbol bound_msym)
00500 {
00501   struct minimal_symbol *msymbol = bound_msym.minsym;
00502   struct objfile *objfile = bound_msym.objfile;
00503   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00504 
00505   CORE_ADDR addr = SYMBOL_VALUE_ADDRESS (msymbol);
00506   struct obj_section *section = SYMBOL_OBJ_SECTION (objfile, msymbol);
00507   enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol);
00508   CORE_ADDR pc;
00509 
00510   /* The minimal symbol might point to a function descriptor;
00511      resolve it to the actual code address instead.  */
00512   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
00513   if (pc != addr)
00514     {
00515       struct bound_minimal_symbol ifunc_msym = lookup_minimal_symbol_by_pc (pc);
00516 
00517       /* In this case, assume we have a code symbol instead of
00518          a data symbol.  */
00519 
00520       if (ifunc_msym.minsym != NULL
00521           && MSYMBOL_TYPE (ifunc_msym.minsym) == mst_text_gnu_ifunc
00522           && SYMBOL_VALUE_ADDRESS (ifunc_msym.minsym) == pc)
00523         {
00524           /* A function descriptor has been resolved but PC is still in the
00525              STT_GNU_IFUNC resolver body (such as because inferior does not
00526              run to be able to call it).  */
00527 
00528           type = mst_text_gnu_ifunc;
00529         }
00530       else
00531         type = mst_text;
00532       section = NULL;
00533       addr = pc;
00534     }
00535 
00536   if (overlay_debugging)
00537     addr = symbol_overlayed_address (addr, section);
00538 
00539   write_exp_elt_opcode (OP_LONG);
00540   /* Let's make the type big enough to hold a 64-bit address.  */
00541   write_exp_elt_type (objfile_type (objfile)->builtin_core_addr);
00542   write_exp_elt_longcst ((LONGEST) addr);
00543   write_exp_elt_opcode (OP_LONG);
00544 
00545   if (section && section->the_bfd_section->flags & SEC_THREAD_LOCAL)
00546     {
00547       write_exp_elt_opcode (UNOP_MEMVAL_TLS);
00548       write_exp_elt_objfile (objfile);
00549       write_exp_elt_type (objfile_type (objfile)->nodebug_tls_symbol);
00550       write_exp_elt_opcode (UNOP_MEMVAL_TLS);
00551       return;
00552     }
00553 
00554   write_exp_elt_opcode (UNOP_MEMVAL);
00555   switch (type)
00556     {
00557     case mst_text:
00558     case mst_file_text:
00559     case mst_solib_trampoline:
00560       write_exp_elt_type (objfile_type (objfile)->nodebug_text_symbol);
00561       break;
00562 
00563     case mst_text_gnu_ifunc:
00564       write_exp_elt_type (objfile_type (objfile)
00565                                                ->nodebug_text_gnu_ifunc_symbol);
00566       break;
00567 
00568     case mst_data:
00569     case mst_file_data:
00570     case mst_bss:
00571     case mst_file_bss:
00572       write_exp_elt_type (objfile_type (objfile)->nodebug_data_symbol);
00573       break;
00574 
00575     case mst_slot_got_plt:
00576       write_exp_elt_type (objfile_type (objfile)->nodebug_got_plt_symbol);
00577       break;
00578 
00579     default:
00580       write_exp_elt_type (objfile_type (objfile)->nodebug_unknown_symbol);
00581       break;
00582     }
00583   write_exp_elt_opcode (UNOP_MEMVAL);
00584 }
00585 
00586 /* Mark the current index as the starting location of a structure
00587    expression.  This is used when completing on field names.  */
00588 
00589 void
00590 mark_struct_expression (void)
00591 {
00592   gdb_assert (parse_completion
00593               && expout_tag_completion_type == TYPE_CODE_UNDEF);
00594   expout_last_struct = expout_ptr;
00595 }
00596 
00597 /* Indicate that the current parser invocation is completing a tag.
00598    TAG is the type code of the tag, and PTR and LENGTH represent the
00599    start of the tag name.  */
00600 
00601 void
00602 mark_completion_tag (enum type_code tag, const char *ptr, int length)
00603 {
00604   gdb_assert (parse_completion
00605               && expout_tag_completion_type == TYPE_CODE_UNDEF
00606               && expout_completion_name == NULL
00607               && expout_last_struct == -1);
00608   gdb_assert (tag == TYPE_CODE_UNION
00609               || tag == TYPE_CODE_STRUCT
00610               || tag == TYPE_CODE_CLASS
00611               || tag == TYPE_CODE_ENUM);
00612   expout_tag_completion_type = tag;
00613   expout_completion_name = xmalloc (length + 1);
00614   memcpy (expout_completion_name, ptr, length);
00615   expout_completion_name[length] = '\0';
00616 }
00617 
00618 
00619 /* Recognize tokens that start with '$'.  These include:
00620 
00621    $regname     A native register name or a "standard
00622    register name".
00623 
00624    $variable    A convenience variable with a name chosen
00625    by the user.
00626 
00627    $digits              Value history with index <digits>, starting
00628    from the first value which has index 1.
00629 
00630    $$digits     Value history with index <digits> relative
00631    to the last value.  I.e. $$0 is the last
00632    value, $$1 is the one previous to that, $$2
00633    is the one previous to $$1, etc.
00634 
00635    $ | $0 | $$0 The last value in the value history.
00636 
00637    $$           An abbreviation for the second to the last
00638    value in the value history, I.e. $$1  */
00639 
00640 void
00641 write_dollar_variable (struct stoken str)
00642 {
00643   struct symbol *sym = NULL;
00644   struct bound_minimal_symbol msym;
00645   struct internalvar *isym = NULL;
00646 
00647   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
00648      and $$digits (equivalent to $<-digits> if you could type that).  */
00649 
00650   int negate = 0;
00651   int i = 1;
00652   /* Double dollar means negate the number and add -1 as well.
00653      Thus $$ alone means -1.  */
00654   if (str.length >= 2 && str.ptr[1] == '$')
00655     {
00656       negate = 1;
00657       i = 2;
00658     }
00659   if (i == str.length)
00660     {
00661       /* Just dollars (one or two).  */
00662       i = -negate;
00663       goto handle_last;
00664     }
00665   /* Is the rest of the token digits?  */
00666   for (; i < str.length; i++)
00667     if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
00668       break;
00669   if (i == str.length)
00670     {
00671       i = atoi (str.ptr + 1 + negate);
00672       if (negate)
00673         i = -i;
00674       goto handle_last;
00675     }
00676 
00677   /* Handle tokens that refer to machine registers:
00678      $ followed by a register name.  */
00679   i = user_reg_map_name_to_regnum (parse_gdbarch,
00680                                    str.ptr + 1, str.length - 1);
00681   if (i >= 0)
00682     goto handle_register;
00683 
00684   /* Any names starting with $ are probably debugger internal variables.  */
00685 
00686   isym = lookup_only_internalvar (copy_name (str) + 1);
00687   if (isym)
00688     {
00689       write_exp_elt_opcode (OP_INTERNALVAR);
00690       write_exp_elt_intern (isym);
00691       write_exp_elt_opcode (OP_INTERNALVAR);
00692       return;
00693     }
00694 
00695   /* On some systems, such as HP-UX and hppa-linux, certain system routines 
00696      have names beginning with $ or $$.  Check for those, first.  */
00697 
00698   sym = lookup_symbol (copy_name (str), (struct block *) NULL,
00699                        VAR_DOMAIN, NULL);
00700   if (sym)
00701     {
00702       write_exp_elt_opcode (OP_VAR_VALUE);
00703       write_exp_elt_block (block_found);        /* set by lookup_symbol */
00704       write_exp_elt_sym (sym);
00705       write_exp_elt_opcode (OP_VAR_VALUE);
00706       return;
00707     }
00708   msym = lookup_bound_minimal_symbol (copy_name (str));
00709   if (msym.minsym)
00710     {
00711       write_exp_msymbol (msym);
00712       return;
00713     }
00714 
00715   /* Any other names are assumed to be debugger internal variables.  */
00716 
00717   write_exp_elt_opcode (OP_INTERNALVAR);
00718   write_exp_elt_intern (create_internalvar (copy_name (str) + 1));
00719   write_exp_elt_opcode (OP_INTERNALVAR);
00720   return;
00721 handle_last:
00722   write_exp_elt_opcode (OP_LAST);
00723   write_exp_elt_longcst ((LONGEST) i);
00724   write_exp_elt_opcode (OP_LAST);
00725   return;
00726 handle_register:
00727   write_exp_elt_opcode (OP_REGISTER);
00728   str.length--;
00729   str.ptr++;
00730   write_exp_string (str);
00731   write_exp_elt_opcode (OP_REGISTER);
00732   return;
00733 }
00734 
00735 
00736 const char *
00737 find_template_name_end (const char *p)
00738 {
00739   int depth = 1;
00740   int just_seen_right = 0;
00741   int just_seen_colon = 0;
00742   int just_seen_space = 0;
00743 
00744   if (!p || (*p != '<'))
00745     return 0;
00746 
00747   while (*++p)
00748     {
00749       switch (*p)
00750         {
00751         case '\'':
00752         case '\"':
00753         case '{':
00754         case '}':
00755           /* In future, may want to allow these??  */
00756           return 0;
00757         case '<':
00758           depth++;              /* start nested template */
00759           if (just_seen_colon || just_seen_right || just_seen_space)
00760             return 0;           /* but not after : or :: or > or space */
00761           break;
00762         case '>':
00763           if (just_seen_colon || just_seen_right)
00764             return 0;           /* end a (nested?) template */
00765           just_seen_right = 1;  /* but not after : or :: */
00766           if (--depth == 0)     /* also disallow >>, insist on > > */
00767             return ++p;         /* if outermost ended, return */
00768           break;
00769         case ':':
00770           if (just_seen_space || (just_seen_colon > 1))
00771             return 0;           /* nested class spec coming up */
00772           just_seen_colon++;    /* we allow :: but not :::: */
00773           break;
00774         case ' ':
00775           break;
00776         default:
00777           if (!((*p >= 'a' && *p <= 'z') ||     /* allow token chars */
00778                 (*p >= 'A' && *p <= 'Z') ||
00779                 (*p >= '0' && *p <= '9') ||
00780                 (*p == '_') || (*p == ',') ||   /* commas for template args */
00781                 (*p == '&') || (*p == '*') ||   /* pointer and ref types */
00782                 (*p == '(') || (*p == ')') ||   /* function types */
00783                 (*p == '[') || (*p == ']')))    /* array types */
00784             return 0;
00785         }
00786       if (*p != ' ')
00787         just_seen_space = 0;
00788       if (*p != ':')
00789         just_seen_colon = 0;
00790       if (*p != '>')
00791         just_seen_right = 0;
00792     }
00793   return 0;
00794 }
00795 
00796 
00797 /* Return a null-terminated temporary copy of the name of a string token.
00798 
00799    Tokens that refer to names do so with explicit pointer and length,
00800    so they can share the storage that lexptr is parsing.
00801    When it is necessary to pass a name to a function that expects
00802    a null-terminated string, the substring is copied out
00803    into a separate block of storage.
00804 
00805    N.B. A single buffer is reused on each call.  */
00806 
00807 char *
00808 copy_name (struct stoken token)
00809 {
00810   /* A temporary buffer for identifiers, so we can null-terminate them.
00811      We allocate this with xrealloc.  parse_exp_1 used to allocate with
00812      alloca, using the size of the whole expression as a conservative
00813      estimate of the space needed.  However, macro expansion can
00814      introduce names longer than the original expression; there's no
00815      practical way to know beforehand how large that might be.  */
00816   static char *namecopy;
00817   static size_t namecopy_size;
00818 
00819   /* Make sure there's enough space for the token.  */
00820   if (namecopy_size < token.length + 1)
00821     {
00822       namecopy_size = token.length + 1;
00823       namecopy = xrealloc (namecopy, token.length + 1);
00824     }
00825       
00826   memcpy (namecopy, token.ptr, token.length);
00827   namecopy[token.length] = 0;
00828 
00829   return namecopy;
00830 }
00831 
00832 
00833 /* See comments on parser-defs.h.  */
00834 
00835 int
00836 prefixify_expression (struct expression *expr)
00837 {
00838   int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
00839   struct expression *temp;
00840   int inpos = expr->nelts, outpos = 0;
00841 
00842   temp = (struct expression *) alloca (len);
00843 
00844   /* Copy the original expression into temp.  */
00845   memcpy (temp, expr, len);
00846 
00847   return prefixify_subexp (temp, expr, inpos, outpos);
00848 }
00849 
00850 /* Return the number of exp_elements in the postfix subexpression 
00851    of EXPR whose operator is at index ENDPOS - 1 in EXPR.  */
00852 
00853 int
00854 length_of_subexp (struct expression *expr, int endpos)
00855 {
00856   int oplen, args;
00857 
00858   operator_length (expr, endpos, &oplen, &args);
00859 
00860   while (args > 0)
00861     {
00862       oplen += length_of_subexp (expr, endpos - oplen);
00863       args--;
00864     }
00865 
00866   return oplen;
00867 }
00868 
00869 /* Sets *OPLENP to the length of the operator whose (last) index is 
00870    ENDPOS - 1 in EXPR, and sets *ARGSP to the number of arguments that
00871    operator takes.  */
00872 
00873 void
00874 operator_length (const struct expression *expr, int endpos, int *oplenp,
00875                  int *argsp)
00876 {
00877   expr->language_defn->la_exp_desc->operator_length (expr, endpos,
00878                                                      oplenp, argsp);
00879 }
00880 
00881 /* Default value for operator_length in exp_descriptor vectors.  */
00882 
00883 void
00884 operator_length_standard (const struct expression *expr, int endpos,
00885                           int *oplenp, int *argsp)
00886 {
00887   int oplen = 1;
00888   int args = 0;
00889   enum f90_range_type range_type;
00890   int i;
00891 
00892   if (endpos < 1)
00893     error (_("?error in operator_length_standard"));
00894 
00895   i = (int) expr->elts[endpos - 1].opcode;
00896 
00897   switch (i)
00898     {
00899       /* C++  */
00900     case OP_SCOPE:
00901       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
00902       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
00903       break;
00904 
00905     case OP_LONG:
00906     case OP_DOUBLE:
00907     case OP_DECFLOAT:
00908     case OP_VAR_VALUE:
00909       oplen = 4;
00910       break;
00911 
00912     case OP_TYPE:
00913     case OP_BOOL:
00914     case OP_LAST:
00915     case OP_INTERNALVAR:
00916     case OP_VAR_ENTRY_VALUE:
00917       oplen = 3;
00918       break;
00919 
00920     case OP_COMPLEX:
00921       oplen = 3;
00922       args = 2;
00923       break;
00924 
00925     case OP_FUNCALL:
00926     case OP_F77_UNDETERMINED_ARGLIST:
00927       oplen = 3;
00928       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
00929       break;
00930 
00931     case TYPE_INSTANCE:
00932       oplen = 4 + longest_to_int (expr->elts[endpos - 2].longconst);
00933       args = 1;
00934       break;
00935 
00936     case OP_OBJC_MSGCALL:       /* Objective C message (method) call.  */
00937       oplen = 4;
00938       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
00939       break;
00940 
00941     case UNOP_MAX:
00942     case UNOP_MIN:
00943       oplen = 3;
00944       break;
00945 
00946     case UNOP_CAST_TYPE:
00947     case UNOP_DYNAMIC_CAST:
00948     case UNOP_REINTERPRET_CAST:
00949     case UNOP_MEMVAL_TYPE:
00950       oplen = 1;
00951       args = 2;
00952       break;
00953 
00954     case BINOP_VAL:
00955     case UNOP_CAST:
00956     case UNOP_MEMVAL:
00957       oplen = 3;
00958       args = 1;
00959       break;
00960 
00961     case UNOP_MEMVAL_TLS:
00962       oplen = 4;
00963       args = 1;
00964       break;
00965 
00966     case UNOP_ABS:
00967     case UNOP_CAP:
00968     case UNOP_CHR:
00969     case UNOP_FLOAT:
00970     case UNOP_HIGH:
00971     case UNOP_ODD:
00972     case UNOP_ORD:
00973     case UNOP_TRUNC:
00974     case OP_TYPEOF:
00975     case OP_DECLTYPE:
00976     case OP_TYPEID:
00977       oplen = 1;
00978       args = 1;
00979       break;
00980 
00981     case OP_ADL_FUNC:
00982       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
00983       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
00984       oplen++;
00985       oplen++;
00986       break;
00987 
00988     case STRUCTOP_STRUCT:
00989     case STRUCTOP_PTR:
00990       args = 1;
00991       /* fall through */
00992     case OP_REGISTER:
00993     case OP_M2_STRING:
00994     case OP_STRING:
00995     case OP_OBJC_NSSTRING:      /* Objective C Foundation Class
00996                                    NSString constant.  */
00997     case OP_OBJC_SELECTOR:      /* Objective C "@selector" pseudo-op.  */
00998     case OP_NAME:
00999       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
01000       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
01001       break;
01002 
01003     case OP_ARRAY:
01004       oplen = 4;
01005       args = longest_to_int (expr->elts[endpos - 2].longconst);
01006       args -= longest_to_int (expr->elts[endpos - 3].longconst);
01007       args += 1;
01008       break;
01009 
01010     case TERNOP_COND:
01011     case TERNOP_SLICE:
01012       args = 3;
01013       break;
01014 
01015       /* Modula-2 */
01016     case MULTI_SUBSCRIPT:
01017       oplen = 3;
01018       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
01019       break;
01020 
01021     case BINOP_ASSIGN_MODIFY:
01022       oplen = 3;
01023       args = 2;
01024       break;
01025 
01026       /* C++ */
01027     case OP_THIS:
01028       oplen = 2;
01029       break;
01030 
01031     case OP_F90_RANGE:
01032       oplen = 3;
01033 
01034       range_type = longest_to_int (expr->elts[endpos - 2].longconst);
01035       switch (range_type)
01036         {
01037         case LOW_BOUND_DEFAULT:
01038         case HIGH_BOUND_DEFAULT:
01039           args = 1;
01040           break;
01041         case BOTH_BOUND_DEFAULT:
01042           args = 0;
01043           break;
01044         case NONE_BOUND_DEFAULT:
01045           args = 2;
01046           break;
01047         }
01048 
01049       break;
01050 
01051     default:
01052       args = 1 + (i < (int) BINOP_END);
01053     }
01054 
01055   *oplenp = oplen;
01056   *argsp = args;
01057 }
01058 
01059 /* Copy the subexpression ending just before index INEND in INEXPR
01060    into OUTEXPR, starting at index OUTBEG.
01061    In the process, convert it from suffix to prefix form.
01062    If EXPOUT_LAST_STRUCT is -1, then this function always returns -1.
01063    Otherwise, it returns the index of the subexpression which is the
01064    left-hand-side of the expression at EXPOUT_LAST_STRUCT.  */
01065 
01066 static int
01067 prefixify_subexp (struct expression *inexpr,
01068                   struct expression *outexpr, int inend, int outbeg)
01069 {
01070   int oplen;
01071   int args;
01072   int i;
01073   int *arglens;
01074   int result = -1;
01075 
01076   operator_length (inexpr, inend, &oplen, &args);
01077 
01078   /* Copy the final operator itself, from the end of the input
01079      to the beginning of the output.  */
01080   inend -= oplen;
01081   memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
01082           EXP_ELEM_TO_BYTES (oplen));
01083   outbeg += oplen;
01084 
01085   if (expout_last_struct == inend)
01086     result = outbeg - oplen;
01087 
01088   /* Find the lengths of the arg subexpressions.  */
01089   arglens = (int *) alloca (args * sizeof (int));
01090   for (i = args - 1; i >= 0; i--)
01091     {
01092       oplen = length_of_subexp (inexpr, inend);
01093       arglens[i] = oplen;
01094       inend -= oplen;
01095     }
01096 
01097   /* Now copy each subexpression, preserving the order of
01098      the subexpressions, but prefixifying each one.
01099      In this loop, inend starts at the beginning of
01100      the expression this level is working on
01101      and marches forward over the arguments.
01102      outbeg does similarly in the output.  */
01103   for (i = 0; i < args; i++)
01104     {
01105       int r;
01106 
01107       oplen = arglens[i];
01108       inend += oplen;
01109       r = prefixify_subexp (inexpr, outexpr, inend, outbeg);
01110       if (r != -1)
01111         {
01112           /* Return immediately.  We probably have only parsed a
01113              partial expression, so we don't want to try to reverse
01114              the other operands.  */
01115           return r;
01116         }
01117       outbeg += oplen;
01118     }
01119 
01120   return result;
01121 }
01122 
01123 /* Read an expression from the string *STRINGPTR points to,
01124    parse it, and return a pointer to a struct expression that we malloc.
01125    Use block BLOCK as the lexical context for variable names;
01126    if BLOCK is zero, use the block of the selected stack frame.
01127    Meanwhile, advance *STRINGPTR to point after the expression,
01128    at the first nonwhite character that is not part of the expression
01129    (possibly a null character).
01130 
01131    If COMMA is nonzero, stop if a comma is reached.  */
01132 
01133 struct expression *
01134 parse_exp_1 (const char **stringptr, CORE_ADDR pc, const struct block *block,
01135              int comma)
01136 {
01137   return parse_exp_in_context (stringptr, pc, block, comma, 0, NULL);
01138 }
01139 
01140 static struct expression *
01141 parse_exp_in_context (const char **stringptr, CORE_ADDR pc,
01142                       const struct block *block,
01143                       int comma, int void_context_p, int *out_subexp)
01144 {
01145   return parse_exp_in_context_1 (stringptr, pc, block, comma,
01146                                  void_context_p, out_subexp);
01147 }
01148 
01149 /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
01150    no value is expected from the expression.
01151    OUT_SUBEXP is set when attempting to complete a field name; in this
01152    case it is set to the index of the subexpression on the
01153    left-hand-side of the struct op.  If not doing such completion, it
01154    is left untouched.  */
01155 
01156 static struct expression *
01157 parse_exp_in_context_1 (const char **stringptr, CORE_ADDR pc,
01158                         const struct block *block,
01159                         int comma, int void_context_p, int *out_subexp)
01160 {
01161   volatile struct gdb_exception except;
01162   struct cleanup *old_chain, *inner_chain;
01163   const struct language_defn *lang = NULL;
01164   int subexp;
01165 
01166   lexptr = *stringptr;
01167   prev_lexptr = NULL;
01168 
01169   paren_depth = 0;
01170   type_stack.depth = 0;
01171   expout_last_struct = -1;
01172   expout_tag_completion_type = TYPE_CODE_UNDEF;
01173   xfree (expout_completion_name);
01174   expout_completion_name = NULL;
01175 
01176   comma_terminates = comma;
01177 
01178   if (lexptr == 0 || *lexptr == 0)
01179     error_no_arg (_("expression to compute"));
01180 
01181   old_chain = make_cleanup (free_funcalls, 0 /*ignore*/);
01182   funcall_chain = 0;
01183 
01184   expression_context_block = block;
01185 
01186   /* If no context specified, try using the current frame, if any.  */
01187   if (!expression_context_block)
01188     expression_context_block = get_selected_block (&expression_context_pc);
01189   else if (pc == 0)
01190     expression_context_pc = BLOCK_START (expression_context_block);
01191   else
01192     expression_context_pc = pc;
01193 
01194   /* Fall back to using the current source static context, if any.  */
01195 
01196   if (!expression_context_block)
01197     {
01198       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
01199       if (cursal.symtab)
01200         expression_context_block
01201           = BLOCKVECTOR_BLOCK (BLOCKVECTOR (cursal.symtab), STATIC_BLOCK);
01202       if (expression_context_block)
01203         expression_context_pc = BLOCK_START (expression_context_block);
01204     }
01205 
01206   if (language_mode == language_mode_auto && block != NULL)
01207     {
01208       /* Find the language associated to the given context block.
01209          Default to the current language if it can not be determined.
01210 
01211          Note that using the language corresponding to the current frame
01212          can sometimes give unexpected results.  For instance, this
01213          routine is often called several times during the inferior
01214          startup phase to re-parse breakpoint expressions after
01215          a new shared library has been loaded.  The language associated
01216          to the current frame at this moment is not relevant for
01217          the breakpoint.  Using it would therefore be silly, so it seems
01218          better to rely on the current language rather than relying on
01219          the current frame language to parse the expression.  That's why
01220          we do the following language detection only if the context block
01221          has been specifically provided.  */
01222       struct symbol *func = block_linkage_function (block);
01223 
01224       if (func != NULL)
01225         lang = language_def (SYMBOL_LANGUAGE (func));
01226       if (lang == NULL || lang->la_language == language_unknown)
01227         lang = current_language;
01228     }
01229   else
01230     lang = current_language;
01231 
01232   /* get_current_arch may reset CURRENT_LANGUAGE via select_frame.
01233      While we need CURRENT_LANGUAGE to be set to LANG (for lookup_symbol
01234      and others called from *.y) ensure CURRENT_LANGUAGE gets restored
01235      to the value matching SELECTED_FRAME as set by get_current_arch.  */
01236   initialize_expout (10, lang, get_current_arch ());
01237   inner_chain = make_cleanup_restore_current_language ();
01238   set_language (lang->la_language);
01239 
01240   TRY_CATCH (except, RETURN_MASK_ALL)
01241     {
01242       if (lang->la_parser ())
01243         lang->la_error (NULL);
01244     }
01245   if (except.reason < 0)
01246     {
01247       if (! parse_completion)
01248         {
01249           xfree (expout);
01250           throw_exception (except);
01251         }
01252     }
01253 
01254   reallocate_expout ();
01255 
01256   /* Convert expression from postfix form as generated by yacc
01257      parser, to a prefix form.  */
01258 
01259   if (expressiondebug)
01260     dump_raw_expression (expout, gdb_stdlog,
01261                          "before conversion to prefix form");
01262 
01263   subexp = prefixify_expression (expout);
01264   if (out_subexp)
01265     *out_subexp = subexp;
01266 
01267   lang->la_post_parser (&expout, void_context_p);
01268 
01269   if (expressiondebug)
01270     dump_prefix_expression (expout, gdb_stdlog);
01271 
01272   do_cleanups (inner_chain);
01273   discard_cleanups (old_chain);
01274 
01275   *stringptr = lexptr;
01276   return expout;
01277 }
01278 
01279 /* Parse STRING as an expression, and complain if this fails
01280    to use up all of the contents of STRING.  */
01281 
01282 struct expression *
01283 parse_expression (const char *string)
01284 {
01285   struct expression *exp;
01286 
01287   exp = parse_exp_1 (&string, 0, 0, 0);
01288   if (*string)
01289     error (_("Junk after end of expression."));
01290   return exp;
01291 }
01292 
01293 /* Parse STRING as an expression.  If parsing ends in the middle of a
01294    field reference, return the type of the left-hand-side of the
01295    reference; furthermore, if the parsing ends in the field name,
01296    return the field name in *NAME.  If the parsing ends in the middle
01297    of a field reference, but the reference is somehow invalid, throw
01298    an exception.  In all other cases, return NULL.  Returned non-NULL
01299    *NAME must be freed by the caller.  */
01300 
01301 struct type *
01302 parse_expression_for_completion (const char *string, char **name,
01303                                  enum type_code *code)
01304 {
01305   struct expression *exp = NULL;
01306   struct value *val;
01307   int subexp;
01308   volatile struct gdb_exception except;
01309 
01310   TRY_CATCH (except, RETURN_MASK_ERROR)
01311     {
01312       parse_completion = 1;
01313       exp = parse_exp_in_context (&string, 0, 0, 0, 0, &subexp);
01314     }
01315   parse_completion = 0;
01316   if (except.reason < 0 || ! exp)
01317     return NULL;
01318 
01319   if (expout_tag_completion_type != TYPE_CODE_UNDEF)
01320     {
01321       *code = expout_tag_completion_type;
01322       *name = expout_completion_name;
01323       expout_completion_name = NULL;
01324       return NULL;
01325     }
01326 
01327   if (expout_last_struct == -1)
01328     {
01329       xfree (exp);
01330       return NULL;
01331     }
01332 
01333   *name = extract_field_op (exp, &subexp);
01334   if (!*name)
01335     {
01336       xfree (exp);
01337       return NULL;
01338     }
01339 
01340   /* This might throw an exception.  If so, we want to let it
01341      propagate.  */
01342   val = evaluate_subexpression_type (exp, subexp);
01343   /* (*NAME) is a part of the EXP memory block freed below.  */
01344   *name = xstrdup (*name);
01345   xfree (exp);
01346 
01347   return value_type (val);
01348 }
01349 
01350 /* A post-parser that does nothing.  */
01351 
01352 void
01353 null_post_parser (struct expression **exp, int void_context_p)
01354 {
01355 }
01356 
01357 /* Parse floating point value P of length LEN.
01358    Return 0 (false) if invalid, 1 (true) if valid.
01359    The successfully parsed number is stored in D.
01360    *SUFFIX points to the suffix of the number in P.
01361 
01362    NOTE: This accepts the floating point syntax that sscanf accepts.  */
01363 
01364 int
01365 parse_float (const char *p, int len, DOUBLEST *d, const char **suffix)
01366 {
01367   char *copy;
01368   int n, num;
01369 
01370   copy = xmalloc (len + 1);
01371   memcpy (copy, p, len);
01372   copy[len] = 0;
01373 
01374   num = sscanf (copy, "%" DOUBLEST_SCAN_FORMAT "%n", d, &n);
01375   xfree (copy);
01376 
01377   /* The sscanf man page suggests not making any assumptions on the effect
01378      of %n on the result, so we don't.
01379      That is why we simply test num == 0.  */
01380   if (num == 0)
01381     return 0;
01382 
01383   *suffix = p + n;
01384   return 1;
01385 }
01386 
01387 /* Parse floating point value P of length LEN, using the C syntax for floats.
01388    Return 0 (false) if invalid, 1 (true) if valid.
01389    The successfully parsed number is stored in *D.
01390    Its type is taken from builtin_type (gdbarch) and is stored in *T.  */
01391 
01392 int
01393 parse_c_float (struct gdbarch *gdbarch, const char *p, int len,
01394                DOUBLEST *d, struct type **t)
01395 {
01396   const char *suffix;
01397   int suffix_len;
01398   const struct builtin_type *builtin_types = builtin_type (gdbarch);
01399 
01400   if (! parse_float (p, len, d, &suffix))
01401     return 0;
01402 
01403   suffix_len = p + len - suffix;
01404 
01405   if (suffix_len == 0)
01406     *t = builtin_types->builtin_double;
01407   else if (suffix_len == 1)
01408     {
01409       /* Handle suffixes: 'f' for float, 'l' for long double.  */
01410       if (tolower (*suffix) == 'f')
01411         *t = builtin_types->builtin_float;
01412       else if (tolower (*suffix) == 'l')
01413         *t = builtin_types->builtin_long_double;
01414       else
01415         return 0;
01416     }
01417   else
01418     return 0;
01419 
01420   return 1;
01421 }
01422 
01423 /* Stuff for maintaining a stack of types.  Currently just used by C, but
01424    probably useful for any language which declares its types "backwards".  */
01425 
01426 /* Ensure that there are HOWMUCH open slots on the type stack STACK.  */
01427 
01428 static void
01429 type_stack_reserve (struct type_stack *stack, int howmuch)
01430 {
01431   if (stack->depth + howmuch >= stack->size)
01432     {
01433       stack->size *= 2;
01434       if (stack->size < howmuch)
01435         stack->size = howmuch;
01436       stack->elements = xrealloc (stack->elements,
01437                                   stack->size * sizeof (union type_stack_elt));
01438     }
01439 }
01440 
01441 /* Ensure that there is a single open slot in the global type stack.  */
01442 
01443 static void
01444 check_type_stack_depth (void)
01445 {
01446   type_stack_reserve (&type_stack, 1);
01447 }
01448 
01449 /* A helper function for insert_type and insert_type_address_space.
01450    This does work of expanding the type stack and inserting the new
01451    element, ELEMENT, into the stack at location SLOT.  */
01452 
01453 static void
01454 insert_into_type_stack (int slot, union type_stack_elt element)
01455 {
01456   check_type_stack_depth ();
01457 
01458   if (slot < type_stack.depth)
01459     memmove (&type_stack.elements[slot + 1], &type_stack.elements[slot],
01460              (type_stack.depth - slot) * sizeof (union type_stack_elt));
01461   type_stack.elements[slot] = element;
01462   ++type_stack.depth;
01463 }
01464 
01465 /* Insert a new type, TP, at the bottom of the type stack.  If TP is
01466    tp_pointer or tp_reference, it is inserted at the bottom.  If TP is
01467    a qualifier, it is inserted at slot 1 (just above a previous
01468    tp_pointer) if there is anything on the stack, or simply pushed if
01469    the stack is empty.  Other values for TP are invalid.  */
01470 
01471 void
01472 insert_type (enum type_pieces tp)
01473 {
01474   union type_stack_elt element;
01475   int slot;
01476 
01477   gdb_assert (tp == tp_pointer || tp == tp_reference
01478               || tp == tp_const || tp == tp_volatile);
01479 
01480   /* If there is anything on the stack (we know it will be a
01481      tp_pointer), insert the qualifier above it.  Otherwise, simply
01482      push this on the top of the stack.  */
01483   if (type_stack.depth && (tp == tp_const || tp == tp_volatile))
01484     slot = 1;
01485   else
01486     slot = 0;
01487 
01488   element.piece = tp;
01489   insert_into_type_stack (slot, element);
01490 }
01491 
01492 void
01493 push_type (enum type_pieces tp)
01494 {
01495   check_type_stack_depth ();
01496   type_stack.elements[type_stack.depth++].piece = tp;
01497 }
01498 
01499 void
01500 push_type_int (int n)
01501 {
01502   check_type_stack_depth ();
01503   type_stack.elements[type_stack.depth++].int_val = n;
01504 }
01505 
01506 /* Insert a tp_space_identifier and the corresponding address space
01507    value into the stack.  STRING is the name of an address space, as
01508    recognized by address_space_name_to_int.  If the stack is empty,
01509    the new elements are simply pushed.  If the stack is not empty,
01510    this function assumes that the first item on the stack is a
01511    tp_pointer, and the new values are inserted above the first
01512    item.  */
01513 
01514 void
01515 insert_type_address_space (char *string)
01516 {
01517   union type_stack_elt element;
01518   int slot;
01519 
01520   /* If there is anything on the stack (we know it will be a
01521      tp_pointer), insert the address space qualifier above it.
01522      Otherwise, simply push this on the top of the stack.  */
01523   if (type_stack.depth)
01524     slot = 1;
01525   else
01526     slot = 0;
01527 
01528   element.piece = tp_space_identifier;
01529   insert_into_type_stack (slot, element);
01530   element.int_val = address_space_name_to_int (parse_gdbarch, string);
01531   insert_into_type_stack (slot, element);
01532 }
01533 
01534 enum type_pieces
01535 pop_type (void)
01536 {
01537   if (type_stack.depth)
01538     return type_stack.elements[--type_stack.depth].piece;
01539   return tp_end;
01540 }
01541 
01542 int
01543 pop_type_int (void)
01544 {
01545   if (type_stack.depth)
01546     return type_stack.elements[--type_stack.depth].int_val;
01547   /* "Can't happen".  */
01548   return 0;
01549 }
01550 
01551 /* Pop a type list element from the global type stack.  */
01552 
01553 static VEC (type_ptr) *
01554 pop_typelist (void)
01555 {
01556   gdb_assert (type_stack.depth);
01557   return type_stack.elements[--type_stack.depth].typelist_val;
01558 }
01559 
01560 /* Pop a type_stack element from the global type stack.  */
01561 
01562 static struct type_stack *
01563 pop_type_stack (void)
01564 {
01565   gdb_assert (type_stack.depth);
01566   return type_stack.elements[--type_stack.depth].stack_val;
01567 }
01568 
01569 /* Append the elements of the type stack FROM to the type stack TO.
01570    Always returns TO.  */
01571 
01572 struct type_stack *
01573 append_type_stack (struct type_stack *to, struct type_stack *from)
01574 {
01575   type_stack_reserve (to, from->depth);
01576 
01577   memcpy (&to->elements[to->depth], &from->elements[0],
01578           from->depth * sizeof (union type_stack_elt));
01579   to->depth += from->depth;
01580 
01581   return to;
01582 }
01583 
01584 /* Push the type stack STACK as an element on the global type stack.  */
01585 
01586 void
01587 push_type_stack (struct type_stack *stack)
01588 {
01589   check_type_stack_depth ();
01590   type_stack.elements[type_stack.depth++].stack_val = stack;
01591   push_type (tp_type_stack);
01592 }
01593 
01594 /* Copy the global type stack into a newly allocated type stack and
01595    return it.  The global stack is cleared.  The returned type stack
01596    must be freed with type_stack_cleanup.  */
01597 
01598 struct type_stack *
01599 get_type_stack (void)
01600 {
01601   struct type_stack *result = XNEW (struct type_stack);
01602 
01603   *result = type_stack;
01604   type_stack.depth = 0;
01605   type_stack.size = 0;
01606   type_stack.elements = NULL;
01607 
01608   return result;
01609 }
01610 
01611 /* A cleanup function that destroys a single type stack.  */
01612 
01613 void
01614 type_stack_cleanup (void *arg)
01615 {
01616   struct type_stack *stack = arg;
01617 
01618   xfree (stack->elements);
01619   xfree (stack);
01620 }
01621 
01622 /* Push a function type with arguments onto the global type stack.
01623    LIST holds the argument types.  If the final item in LIST is NULL,
01624    then the function will be varargs.  */
01625 
01626 void
01627 push_typelist (VEC (type_ptr) *list)
01628 {
01629   check_type_stack_depth ();
01630   type_stack.elements[type_stack.depth++].typelist_val = list;
01631   push_type (tp_function_with_arguments);
01632 }
01633 
01634 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
01635    as modified by all the stuff on the stack.  */
01636 struct type *
01637 follow_types (struct type *follow_type)
01638 {
01639   int done = 0;
01640   int make_const = 0;
01641   int make_volatile = 0;
01642   int make_addr_space = 0;
01643   int array_size;
01644 
01645   while (!done)
01646     switch (pop_type ())
01647       {
01648       case tp_end:
01649         done = 1;
01650         if (make_const)
01651           follow_type = make_cv_type (make_const, 
01652                                       TYPE_VOLATILE (follow_type), 
01653                                       follow_type, 0);
01654         if (make_volatile)
01655           follow_type = make_cv_type (TYPE_CONST (follow_type), 
01656                                       make_volatile, 
01657                                       follow_type, 0);
01658         if (make_addr_space)
01659           follow_type = make_type_with_address_space (follow_type, 
01660                                                       make_addr_space);
01661         make_const = make_volatile = 0;
01662         make_addr_space = 0;
01663         break;
01664       case tp_const:
01665         make_const = 1;
01666         break;
01667       case tp_volatile:
01668         make_volatile = 1;
01669         break;
01670       case tp_space_identifier:
01671         make_addr_space = pop_type_int ();
01672         break;
01673       case tp_pointer:
01674         follow_type = lookup_pointer_type (follow_type);
01675         if (make_const)
01676           follow_type = make_cv_type (make_const, 
01677                                       TYPE_VOLATILE (follow_type), 
01678                                       follow_type, 0);
01679         if (make_volatile)
01680           follow_type = make_cv_type (TYPE_CONST (follow_type), 
01681                                       make_volatile, 
01682                                       follow_type, 0);
01683         if (make_addr_space)
01684           follow_type = make_type_with_address_space (follow_type, 
01685                                                       make_addr_space);
01686         make_const = make_volatile = 0;
01687         make_addr_space = 0;
01688         break;
01689       case tp_reference:
01690         follow_type = lookup_reference_type (follow_type);
01691         if (make_const)
01692           follow_type = make_cv_type (make_const, 
01693                                       TYPE_VOLATILE (follow_type), 
01694                                       follow_type, 0);
01695         if (make_volatile)
01696           follow_type = make_cv_type (TYPE_CONST (follow_type), 
01697                                       make_volatile, 
01698                                       follow_type, 0);
01699         if (make_addr_space)
01700           follow_type = make_type_with_address_space (follow_type, 
01701                                                       make_addr_space);
01702         make_const = make_volatile = 0;
01703         make_addr_space = 0;
01704         break;
01705       case tp_array:
01706         array_size = pop_type_int ();
01707         /* FIXME-type-allocation: need a way to free this type when we are
01708            done with it.  */
01709         follow_type =
01710           lookup_array_range_type (follow_type,
01711                                    0, array_size >= 0 ? array_size - 1 : 0);
01712         if (array_size < 0)
01713           TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (follow_type) = 1;
01714         break;
01715       case tp_function:
01716         /* FIXME-type-allocation: need a way to free this type when we are
01717            done with it.  */
01718         follow_type = lookup_function_type (follow_type);
01719         break;
01720 
01721       case tp_function_with_arguments:
01722         {
01723           VEC (type_ptr) *args = pop_typelist ();
01724 
01725           follow_type
01726             = lookup_function_type_with_arguments (follow_type,
01727                                                    VEC_length (type_ptr, args),
01728                                                    VEC_address (type_ptr,
01729                                                                 args));
01730           VEC_free (type_ptr, args);
01731         }
01732         break;
01733 
01734       case tp_type_stack:
01735         {
01736           struct type_stack *stack = pop_type_stack ();
01737           /* Sort of ugly, but not really much worse than the
01738              alternatives.  */
01739           struct type_stack save = type_stack;
01740 
01741           type_stack = *stack;
01742           follow_type = follow_types (follow_type);
01743           gdb_assert (type_stack.depth == 0);
01744 
01745           type_stack = save;
01746         }
01747         break;
01748       default:
01749         gdb_assert_not_reached ("unrecognized tp_ value in follow_types");
01750       }
01751   return follow_type;
01752 }
01753 
01754 /* This function avoids direct calls to fprintf 
01755    in the parser generated debug code.  */
01756 void
01757 parser_fprintf (FILE *x, const char *y, ...)
01758 { 
01759   va_list args;
01760 
01761   va_start (args, y);
01762   if (x == stderr)
01763     vfprintf_unfiltered (gdb_stderr, y, args); 
01764   else
01765     {
01766       fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n");
01767       vfprintf_unfiltered (gdb_stderr, y, args);
01768     }
01769   va_end (args);
01770 }
01771 
01772 /* Implementation of the exp_descriptor method operator_check.  */
01773 
01774 int
01775 operator_check_standard (struct expression *exp, int pos,
01776                          int (*objfile_func) (struct objfile *objfile,
01777                                               void *data),
01778                          void *data)
01779 {
01780   const union exp_element *const elts = exp->elts;
01781   struct type *type = NULL;
01782   struct objfile *objfile = NULL;
01783 
01784   /* Extended operators should have been already handled by exp_descriptor
01785      iterate method of its specific language.  */
01786   gdb_assert (elts[pos].opcode < OP_EXTENDED0);
01787 
01788   /* Track the callers of write_exp_elt_type for this table.  */
01789 
01790   switch (elts[pos].opcode)
01791     {
01792     case BINOP_VAL:
01793     case OP_COMPLEX:
01794     case OP_DECFLOAT:
01795     case OP_DOUBLE:
01796     case OP_LONG:
01797     case OP_SCOPE:
01798     case OP_TYPE:
01799     case UNOP_CAST:
01800     case UNOP_MAX:
01801     case UNOP_MEMVAL:
01802     case UNOP_MIN:
01803       type = elts[pos + 1].type;
01804       break;
01805 
01806     case TYPE_INSTANCE:
01807       {
01808         LONGEST arg, nargs = elts[pos + 1].longconst;
01809 
01810         for (arg = 0; arg < nargs; arg++)
01811           {
01812             struct type *type = elts[pos + 2 + arg].type;
01813             struct objfile *objfile = TYPE_OBJFILE (type);
01814 
01815             if (objfile && (*objfile_func) (objfile, data))
01816               return 1;
01817           }
01818       }
01819       break;
01820 
01821     case UNOP_MEMVAL_TLS:
01822       objfile = elts[pos + 1].objfile;
01823       type = elts[pos + 2].type;
01824       break;
01825 
01826     case OP_VAR_VALUE:
01827       {
01828         const struct block *const block = elts[pos + 1].block;
01829         const struct symbol *const symbol = elts[pos + 2].symbol;
01830 
01831         /* Check objfile where the variable itself is placed.
01832            SYMBOL_OBJ_SECTION (symbol) may be NULL.  */
01833         if ((*objfile_func) (SYMBOL_SYMTAB (symbol)->objfile, data))
01834           return 1;
01835 
01836         /* Check objfile where is placed the code touching the variable.  */
01837         objfile = lookup_objfile_from_block (block);
01838 
01839         type = SYMBOL_TYPE (symbol);
01840       }
01841       break;
01842     }
01843 
01844   /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL.  */
01845 
01846   if (type && TYPE_OBJFILE (type)
01847       && (*objfile_func) (TYPE_OBJFILE (type), data))
01848     return 1;
01849   if (objfile && (*objfile_func) (objfile, data))
01850     return 1;
01851 
01852   return 0;
01853 }
01854 
01855 /* Call OBJFILE_FUNC for any TYPE and OBJFILE found being referenced by EXP.
01856    The functions are never called with NULL OBJFILE.  Functions get passed an
01857    arbitrary caller supplied DATA pointer.  If any of the functions returns
01858    non-zero value then (any other) non-zero value is immediately returned to
01859    the caller.  Otherwise zero is returned after iterating through whole EXP.
01860    */
01861 
01862 static int
01863 exp_iterate (struct expression *exp,
01864              int (*objfile_func) (struct objfile *objfile, void *data),
01865              void *data)
01866 {
01867   int endpos;
01868 
01869   for (endpos = exp->nelts; endpos > 0; )
01870     {
01871       int pos, args, oplen = 0;
01872 
01873       operator_length (exp, endpos, &oplen, &args);
01874       gdb_assert (oplen > 0);
01875 
01876       pos = endpos - oplen;
01877       if (exp->language_defn->la_exp_desc->operator_check (exp, pos,
01878                                                            objfile_func, data))
01879         return 1;
01880 
01881       endpos = pos;
01882     }
01883 
01884   return 0;
01885 }
01886 
01887 /* Helper for exp_uses_objfile.  */
01888 
01889 static int
01890 exp_uses_objfile_iter (struct objfile *exp_objfile, void *objfile_voidp)
01891 {
01892   struct objfile *objfile = objfile_voidp;
01893 
01894   if (exp_objfile->separate_debug_objfile_backlink)
01895     exp_objfile = exp_objfile->separate_debug_objfile_backlink;
01896 
01897   return exp_objfile == objfile;
01898 }
01899 
01900 /* Return 1 if EXP uses OBJFILE (and will become dangling when OBJFILE
01901    is unloaded), otherwise return 0.  OBJFILE must not be a separate debug info
01902    file.  */
01903 
01904 int
01905 exp_uses_objfile (struct expression *exp, struct objfile *objfile)
01906 {
01907   gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
01908 
01909   return exp_iterate (exp, exp_uses_objfile_iter, objfile);
01910 }
01911 
01912 void
01913 _initialize_parse (void)
01914 {
01915   type_stack.size = 0;
01916   type_stack.depth = 0;
01917   type_stack.elements = NULL;
01918 
01919   add_setshow_zuinteger_cmd ("expression", class_maintenance,
01920                              &expressiondebug,
01921                              _("Set expression debugging."),
01922                              _("Show expression debugging."),
01923                              _("When non-zero, the internal representation "
01924                                "of expressions will be printed."),
01925                              NULL,
01926                              show_expressiondebug,
01927                              &setdebuglist, &showdebuglist);
01928   add_setshow_boolean_cmd ("parser", class_maintenance,
01929                             &parser_debug,
01930                            _("Set parser debugging."),
01931                            _("Show parser debugging."),
01932                            _("When non-zero, expression parser "
01933                              "tracing will be enabled."),
01934                             NULL,
01935                             show_parserdebug,
01936                             &setdebuglist, &showdebuglist);
01937 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines