]> Git Repo - binutils.git/blob - gdb/parse.c
* gdb.fortran/types.exp: Escape brackets in expect patterns
[binutils.git] / gdb / parse.c
1 /* Parse expressions for GDB.
2    Copyright (C) 1986, 1989, 1990, 1991, 1994 Free Software Foundation, Inc.
3    Modified from expread.y by the Department of Computer Science at the
4    State University of New York at Buffalo, 1991.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* Parse an expression from text in a string,
23    and return the result as a  struct expression  pointer.
24    That structure contains arithmetic operations in reverse polish,
25    with constants represented by operations that are followed by special data.
26    See expression.h for the details of the format.
27    What is important here is that it can be built up sequentially
28    during the process of parsing; the lower levels of the tree always
29    come first in the result.  */
30    
31 #include "defs.h"
32 #include "gdb_string.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "frame.h"
36 #include "expression.h"
37 #include "value.h"
38 #include "command.h"
39 #include "language.h"
40 #include "parser-defs.h"
41 \f
42 /* Global variables declared in parser-defs.h (and commented there).  */
43 struct expression *expout;
44 int expout_size;
45 int expout_ptr;
46 struct block *expression_context_block;
47 struct block *innermost_block;
48 int arglist_len;
49 union type_stack_elt *type_stack;
50 int type_stack_depth, type_stack_size;
51 char *lexptr;
52 char *namecopy;
53 int paren_depth;
54 int comma_terminates;
55 \f
56 static void
57 free_funcalls PARAMS ((void));
58
59 static void
60 prefixify_expression PARAMS ((struct expression *));
61
62 static void
63 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
64
65 /* Data structure for saving values of arglist_len for function calls whose
66    arguments contain other function calls.  */
67
68 struct funcall
69   {
70     struct funcall *next;
71     int arglist_len;
72   };
73
74 static struct funcall *funcall_chain;
75
76 /* Assign machine-independent names to certain registers 
77    (unless overridden by the REGISTER_NAMES table) */
78
79 #ifdef NO_STD_REGS
80 unsigned num_std_regs = 0;
81 struct std_regs std_regs[1];
82 #else
83 struct std_regs std_regs[] = {
84
85 #ifdef PC_REGNUM
86         { "pc", PC_REGNUM },
87 #endif
88 #ifdef FP_REGNUM
89         { "fp", FP_REGNUM },
90 #endif
91 #ifdef SP_REGNUM
92         { "sp", SP_REGNUM },
93 #endif
94 #ifdef PS_REGNUM
95         { "ps", PS_REGNUM },
96 #endif
97
98 };
99
100 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
101
102 #endif
103
104 /* The generic method for targets to specify how their registers are named.
105    The mapping can be derived from three sources: reg_names; std_regs; or
106    a target specific alias hook. */
107
108 int
109 target_map_name_to_register (str, len)
110      char *str;
111      int len;
112 {
113   int i;
114
115   /* First try target specific aliases. We try these first because on some 
116      systems standard names can be context dependent (eg. $pc on a 
117      multiprocessor can be could be any of several PCs).  */
118 #ifdef REGISTER_NAME_ALIAS_HOOK
119   i =  REGISTER_NAME_ALIAS_HOOK (str, len);
120   if (i >= 0)
121     return i;
122 #endif
123
124   /* Search architectural register name space. */
125   for (i = 0; i < NUM_REGS; i++)
126     if (reg_names[i] && len == strlen (reg_names[i])
127         && STREQN (str, reg_names[i], len))
128       {
129         return i;
130       }
131
132   /* Try standard aliases */
133   for (i = 0; i < num_std_regs; i++)
134     if (std_regs[i].name && len == strlen (std_regs[i].name)
135         && STREQN (str, std_regs[i].name, len))
136       {
137         return std_regs[i].regnum;
138       }
139
140   return -1;
141 }
142
143 /* Begin counting arguments for a function call,
144    saving the data about any containing call.  */
145
146 void
147 start_arglist ()
148 {
149   register struct funcall *new;
150
151   new = (struct funcall *) xmalloc (sizeof (struct funcall));
152   new->next = funcall_chain;
153   new->arglist_len = arglist_len;
154   arglist_len = 0;
155   funcall_chain = new;
156 }
157
158 /* Return the number of arguments in a function call just terminated,
159    and restore the data for the containing function call.  */
160
161 int
162 end_arglist ()
163 {
164   register int val = arglist_len;
165   register struct funcall *call = funcall_chain;
166   funcall_chain = call->next;
167   arglist_len = call->arglist_len;
168   free ((PTR)call);
169   return val;
170 }
171
172 /* Free everything in the funcall chain.
173    Used when there is an error inside parsing.  */
174
175 static void
176 free_funcalls ()
177 {
178   register struct funcall *call, *next;
179
180   for (call = funcall_chain; call; call = next)
181     {
182       next = call->next;
183       free ((PTR)call);
184     }
185 }
186 \f
187 /* This page contains the functions for adding data to the  struct expression
188    being constructed.  */
189
190 /* Add one element to the end of the expression.  */
191
192 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
193    a register through here */
194
195 void
196 write_exp_elt (expelt)
197      union exp_element expelt;
198 {
199   if (expout_ptr >= expout_size)
200     {
201       expout_size *= 2;
202       expout = (struct expression *)
203         xrealloc ((char *) expout, sizeof (struct expression)
204                   + EXP_ELEM_TO_BYTES (expout_size));
205     }
206   expout->elts[expout_ptr++] = expelt;
207 }
208
209 void
210 write_exp_elt_opcode (expelt)
211      enum exp_opcode expelt;
212 {
213   union exp_element tmp;
214
215   tmp.opcode = expelt;
216
217   write_exp_elt (tmp);
218 }
219
220 void
221 write_exp_elt_sym (expelt)
222      struct symbol *expelt;
223 {
224   union exp_element tmp;
225
226   tmp.symbol = expelt;
227
228   write_exp_elt (tmp);
229 }
230
231 void
232 write_exp_elt_block (b)
233      struct block *b;
234 {
235   union exp_element tmp;
236   tmp.block = b;
237   write_exp_elt (tmp);
238 }
239
240 void
241 write_exp_elt_longcst (expelt)
242      LONGEST expelt;
243 {
244   union exp_element tmp;
245
246   tmp.longconst = expelt;
247
248   write_exp_elt (tmp);
249 }
250
251 void
252 write_exp_elt_dblcst (expelt)
253      DOUBLEST expelt;
254 {
255   union exp_element tmp;
256
257   tmp.doubleconst = expelt;
258
259   write_exp_elt (tmp);
260 }
261
262 void
263 write_exp_elt_type (expelt)
264      struct type *expelt;
265 {
266   union exp_element tmp;
267
268   tmp.type = expelt;
269
270   write_exp_elt (tmp);
271 }
272
273 void
274 write_exp_elt_intern (expelt)
275      struct internalvar *expelt;
276 {
277   union exp_element tmp;
278
279   tmp.internalvar = expelt;
280
281   write_exp_elt (tmp);
282 }
283
284 /* Add a string constant to the end of the expression.
285
286    String constants are stored by first writing an expression element
287    that contains the length of the string, then stuffing the string
288    constant itself into however many expression elements are needed
289    to hold it, and then writing another expression element that contains
290    the length of the string.  I.E. an expression element at each end of
291    the string records the string length, so you can skip over the 
292    expression elements containing the actual string bytes from either
293    end of the string.  Note that this also allows gdb to handle
294    strings with embedded null bytes, as is required for some languages.
295
296    Don't be fooled by the fact that the string is null byte terminated,
297    this is strictly for the convenience of debugging gdb itself.  Gdb
298    Gdb does not depend up the string being null terminated, since the
299    actual length is recorded in expression elements at each end of the
300    string.  The null byte is taken into consideration when computing how
301    many expression elements are required to hold the string constant, of
302    course. */
303
304
305 void
306 write_exp_string (str)
307      struct stoken str;
308 {
309   register int len = str.length;
310   register int lenelt;
311   register char *strdata;
312
313   /* Compute the number of expression elements required to hold the string
314      (including a null byte terminator), along with one expression element
315      at each end to record the actual string length (not including the
316      null byte terminator). */
317
318   lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
319
320   /* Ensure that we have enough available expression elements to store
321      everything. */
322
323   if ((expout_ptr + lenelt) >= expout_size)
324     {
325       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
326       expout = (struct expression *)
327         xrealloc ((char *) expout, (sizeof (struct expression)
328                                     + EXP_ELEM_TO_BYTES (expout_size)));
329     }
330
331   /* Write the leading length expression element (which advances the current
332      expression element index), then write the string constant followed by a
333      terminating null byte, and then write the trailing length expression
334      element. */
335
336   write_exp_elt_longcst ((LONGEST) len);
337   strdata = (char *) &expout->elts[expout_ptr];
338   memcpy (strdata, str.ptr, len);
339   *(strdata + len) = '\0';
340   expout_ptr += lenelt - 2;
341   write_exp_elt_longcst ((LONGEST) len);
342 }
343
344 /* Add a bitstring constant to the end of the expression.
345
346    Bitstring constants are stored by first writing an expression element
347    that contains the length of the bitstring (in bits), then stuffing the
348    bitstring constant itself into however many expression elements are
349    needed to hold it, and then writing another expression element that
350    contains the length of the bitstring.  I.E. an expression element at
351    each end of the bitstring records the bitstring length, so you can skip
352    over the expression elements containing the actual bitstring bytes from
353    either end of the bitstring. */
354
355 void
356 write_exp_bitstring (str)
357      struct stoken str;
358 {
359   register int bits = str.length;       /* length in bits */
360   register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
361   register int lenelt;
362   register char *strdata;
363
364   /* Compute the number of expression elements required to hold the bitstring,
365      along with one expression element at each end to record the actual
366      bitstring length in bits. */
367
368   lenelt = 2 + BYTES_TO_EXP_ELEM (len);
369
370   /* Ensure that we have enough available expression elements to store
371      everything. */
372
373   if ((expout_ptr + lenelt) >= expout_size)
374     {
375       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
376       expout = (struct expression *)
377         xrealloc ((char *) expout, (sizeof (struct expression)
378                                     + EXP_ELEM_TO_BYTES (expout_size)));
379     }
380
381   /* Write the leading length expression element (which advances the current
382      expression element index), then write the bitstring constant, and then
383      write the trailing length expression element. */
384
385   write_exp_elt_longcst ((LONGEST) bits);
386   strdata = (char *) &expout->elts[expout_ptr];
387   memcpy (strdata, str.ptr, len);
388   expout_ptr += lenelt - 2;
389   write_exp_elt_longcst ((LONGEST) bits);
390 }
391
392 /* Add the appropriate elements for a minimal symbol to the end of
393    the expression.  The rationale behind passing in text_symbol_type and
394    data_symbol_type was so that Modula-2 could pass in WORD for
395    data_symbol_type.  Perhaps it still is useful to have those types vary
396    based on the language, but they no longer have names like "int", so
397    the initial rationale is gone.  */
398
399 static struct type *msym_text_symbol_type;
400 static struct type *msym_data_symbol_type;
401 static struct type *msym_unknown_symbol_type;
402
403 void
404 write_exp_msymbol (msymbol, text_symbol_type, data_symbol_type)
405      struct minimal_symbol *msymbol;
406      struct type *text_symbol_type;
407      struct type *data_symbol_type;
408 {
409   write_exp_elt_opcode (OP_LONG);
410   write_exp_elt_type (lookup_pointer_type (builtin_type_void));
411   write_exp_elt_longcst ((LONGEST) SYMBOL_VALUE_ADDRESS (msymbol));
412   write_exp_elt_opcode (OP_LONG);
413
414   write_exp_elt_opcode (UNOP_MEMVAL);
415   switch (msymbol -> type)
416     {
417     case mst_text:
418     case mst_file_text:
419     case mst_solib_trampoline:
420       write_exp_elt_type (msym_text_symbol_type);
421       break;
422
423     case mst_data:
424     case mst_file_data:
425     case mst_bss:
426     case mst_file_bss:
427       write_exp_elt_type (msym_data_symbol_type);
428       break;
429
430     default:
431       write_exp_elt_type (msym_unknown_symbol_type);
432       break;
433     }
434   write_exp_elt_opcode (UNOP_MEMVAL);
435 }
436 \f
437 /* Recognize tokens that start with '$'.  These include:
438
439         $regname        A native register name or a "standard
440                         register name".
441
442         $variable       A convenience variable with a name chosen
443                         by the user.
444
445         $digits         Value history with index <digits>, starting
446                         from the first value which has index 1.
447
448         $$digits        Value history with index <digits> relative
449                         to the last value.  I.E. $$0 is the last
450                         value, $$1 is the one previous to that, $$2
451                         is the one previous to $$1, etc.
452
453         $ | $0 | $$0    The last value in the value history.
454
455         $$              An abbreviation for the second to the last
456                         value in the value history, I.E. $$1
457
458    */
459
460 void
461 write_dollar_variable (str)
462      struct stoken str;
463 {
464   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
465      and $$digits (equivalent to $<-digits> if you could type that). */
466
467   int negate = 0;
468   int i = 1;
469   /* Double dollar means negate the number and add -1 as well.
470      Thus $$ alone means -1.  */
471   if (str.length >= 2 && str.ptr[1] == '$')
472     {
473       negate = 1;
474       i = 2;
475     }
476   if (i == str.length)
477     {
478       /* Just dollars (one or two) */
479       i = - negate;
480       goto handle_last;
481     }
482   /* Is the rest of the token digits?  */
483   for (; i < str.length; i++)
484     if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
485       break;
486   if (i == str.length)
487     {
488       i = atoi (str.ptr + 1 + negate);
489       if (negate)
490         i = - i;
491       goto handle_last;
492     }
493   
494   /* Handle tokens that refer to machine registers:
495      $ followed by a register name.  */
496   i = target_map_name_to_register( str.ptr + 1, str.length - 1 );
497   if( i >= 0 )
498     goto handle_register;
499
500   /* Any other names starting in $ are debugger internal variables.  */
501
502   write_exp_elt_opcode (OP_INTERNALVAR);
503   write_exp_elt_intern (lookup_internalvar (copy_name (str) + 1));
504   write_exp_elt_opcode (OP_INTERNALVAR); 
505   return;
506  handle_last:
507   write_exp_elt_opcode (OP_LAST);
508   write_exp_elt_longcst ((LONGEST) i);
509   write_exp_elt_opcode (OP_LAST);
510   return;
511  handle_register:
512   write_exp_elt_opcode (OP_REGISTER);
513   write_exp_elt_longcst (i);
514   write_exp_elt_opcode (OP_REGISTER); 
515   return;
516 }
517 \f
518 /* Return a null-terminated temporary copy of the name
519    of a string token.  */
520
521 char *
522 copy_name (token)
523      struct stoken token;
524 {
525   memcpy (namecopy, token.ptr, token.length);
526   namecopy[token.length] = 0;
527   return namecopy;
528 }
529 \f
530 /* Reverse an expression from suffix form (in which it is constructed)
531    to prefix form (in which we can conveniently print or execute it).  */
532
533 static void
534 prefixify_expression (expr)
535      register struct expression *expr;
536 {
537   register int len =
538     sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
539   register struct expression *temp;
540   register int inpos = expr->nelts, outpos = 0;
541
542   temp = (struct expression *) alloca (len);
543
544   /* Copy the original expression into temp.  */
545   memcpy (temp, expr, len);
546
547   prefixify_subexp (temp, expr, inpos, outpos);
548 }
549
550 /* Return the number of exp_elements in the subexpression of EXPR
551    whose last exp_element is at index ENDPOS - 1 in EXPR.  */
552
553 int
554 length_of_subexp (expr, endpos)
555      register struct expression *expr;
556      register int endpos;
557 {
558   register int oplen = 1;
559   register int args = 0;
560   register int i;
561
562   if (endpos < 1)
563     error ("?error in length_of_subexp");
564
565   i = (int) expr->elts[endpos - 1].opcode;
566
567   switch (i)
568     {
569       /* C++  */
570     case OP_SCOPE:
571       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
572       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
573       break;
574
575     case OP_LONG:
576     case OP_DOUBLE:
577     case OP_VAR_VALUE:
578       oplen = 4;
579       break;
580
581     case OP_TYPE:
582     case OP_BOOL:
583     case OP_LAST:
584     case OP_REGISTER:
585     case OP_INTERNALVAR:
586       oplen = 3;
587       break;
588
589     case OP_COMPLEX:
590       oplen = 1; 
591       args = 2;
592       break; 
593
594     case OP_FUNCALL:
595     case OP_F77_UNDETERMINED_ARGLIST:
596       oplen = 3;
597       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
598       break;
599
600     case UNOP_MAX:
601     case UNOP_MIN:
602       oplen = 3;
603       break;
604
605    case BINOP_VAL:
606    case UNOP_CAST:
607    case UNOP_MEMVAL:
608       oplen = 3;
609       args = 1;
610       break;
611
612     case UNOP_ABS:
613     case UNOP_CAP:
614     case UNOP_CHR:
615     case UNOP_FLOAT:
616     case UNOP_HIGH:
617     case UNOP_ODD:
618     case UNOP_ORD:
619     case UNOP_TRUNC:
620       oplen = 1;
621       args = 1;
622       break;
623
624     case OP_LABELED:
625     case STRUCTOP_STRUCT:
626     case STRUCTOP_PTR:
627       args = 1;
628       /* fall through */
629     case OP_M2_STRING:
630     case OP_STRING:
631     case OP_NAME:
632     case OP_EXPRSTRING:
633       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
634       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
635       break;
636
637     case OP_BITSTRING:
638       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
639       oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
640       oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
641       break;
642
643     case OP_ARRAY:
644       oplen = 4;
645       args = longest_to_int (expr->elts[endpos - 2].longconst);
646       args -= longest_to_int (expr->elts[endpos - 3].longconst);
647       args += 1;
648       break;
649
650     case TERNOP_COND:
651     case TERNOP_SLICE:
652     case TERNOP_SLICE_COUNT:
653       args = 3;
654       break;
655
656       /* Modula-2 */
657    case MULTI_SUBSCRIPT:
658       oplen = 3;
659       args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
660       break;
661
662     case BINOP_ASSIGN_MODIFY:
663       oplen = 3;
664       args = 2;
665       break;
666
667       /* C++ */
668     case OP_THIS:
669       oplen = 2;
670       break;
671
672     default:
673       args = 1 + (i < (int) BINOP_END);
674     }
675
676   while (args > 0)
677     {
678       oplen += length_of_subexp (expr, endpos - oplen);
679       args--;
680     }
681
682   return oplen;
683 }
684
685 /* Copy the subexpression ending just before index INEND in INEXPR
686    into OUTEXPR, starting at index OUTBEG.
687    In the process, convert it from suffix to prefix form.  */
688
689 static void
690 prefixify_subexp (inexpr, outexpr, inend, outbeg)
691      register struct expression *inexpr;
692      struct expression *outexpr;
693      register int inend;
694      int outbeg;
695 {
696   register int oplen = 1;
697   register int args = 0;
698   register int i;
699   int *arglens;
700   enum exp_opcode opcode;
701
702   /* Compute how long the last operation is (in OPLEN),
703      and also how many preceding subexpressions serve as
704      arguments for it (in ARGS).  */
705
706   opcode = inexpr->elts[inend - 1].opcode;
707   switch (opcode)
708     {
709       /* C++  */
710     case OP_SCOPE:
711       oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
712       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
713       break;
714
715     case OP_LONG:
716     case OP_DOUBLE:
717     case OP_VAR_VALUE:
718       oplen = 4;
719       break;
720
721     case OP_TYPE:
722     case OP_BOOL:
723     case OP_LAST:
724     case OP_REGISTER:
725     case OP_INTERNALVAR:
726       oplen = 3;
727       break;
728
729     case OP_COMPLEX:
730       oplen = 1; 
731       args = 2; 
732       break; 
733
734     case OP_FUNCALL:
735     case OP_F77_UNDETERMINED_ARGLIST:
736       oplen = 3;
737       args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
738       break;
739
740     case UNOP_MIN:
741     case UNOP_MAX:
742       oplen = 3;
743       break;
744
745     case UNOP_CAST:
746     case UNOP_MEMVAL:
747       oplen = 3;
748       args = 1;
749       break;
750
751     case UNOP_ABS:
752     case UNOP_CAP:
753     case UNOP_CHR:
754     case UNOP_FLOAT:
755     case UNOP_HIGH:
756     case UNOP_ODD:
757     case UNOP_ORD:
758     case UNOP_TRUNC:
759       oplen=1;
760       args=1;
761       break;
762
763     case STRUCTOP_STRUCT:
764     case STRUCTOP_PTR:
765     case OP_LABELED:
766       args = 1;
767       /* fall through */
768     case OP_M2_STRING:
769     case OP_STRING:
770     case OP_NAME:
771     case OP_EXPRSTRING:
772       oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
773       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
774       break;
775
776     case OP_BITSTRING:
777       oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
778       oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
779       oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
780       break;
781
782     case OP_ARRAY:
783       oplen = 4;
784       args = longest_to_int (inexpr->elts[inend - 2].longconst);
785       args -= longest_to_int (inexpr->elts[inend - 3].longconst);
786       args += 1;
787       break;
788
789     case TERNOP_COND:
790     case TERNOP_SLICE:
791     case TERNOP_SLICE_COUNT:
792       args = 3;
793       break;
794
795     case BINOP_ASSIGN_MODIFY:
796       oplen = 3;
797       args = 2;
798       break;
799
800       /* Modula-2 */
801    case MULTI_SUBSCRIPT:
802       oplen = 3;
803       args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
804       break;
805
806       /* C++ */
807     case OP_THIS:
808       oplen = 2;
809       break;
810
811     default:
812       args = 1 + ((int) opcode < (int) BINOP_END);
813     }
814
815   /* Copy the final operator itself, from the end of the input
816      to the beginning of the output.  */
817   inend -= oplen;
818   memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
819           EXP_ELEM_TO_BYTES (oplen));
820   outbeg += oplen;
821
822   /* Find the lengths of the arg subexpressions.  */
823   arglens = (int *) alloca (args * sizeof (int));
824   for (i = args - 1; i >= 0; i--)
825     {
826       oplen = length_of_subexp (inexpr, inend);
827       arglens[i] = oplen;
828       inend -= oplen;
829     }
830
831   /* Now copy each subexpression, preserving the order of
832      the subexpressions, but prefixifying each one.
833      In this loop, inend starts at the beginning of
834      the expression this level is working on
835      and marches forward over the arguments.
836      outbeg does similarly in the output.  */
837   for (i = 0; i < args; i++)
838     {
839       oplen = arglens[i];
840       inend += oplen;
841       prefixify_subexp (inexpr, outexpr, inend, outbeg);
842       outbeg += oplen;
843     }
844 }
845 \f
846 /* This page contains the two entry points to this file.  */
847
848 /* Read an expression from the string *STRINGPTR points to,
849    parse it, and return a pointer to a  struct expression  that we malloc.
850    Use block BLOCK as the lexical context for variable names;
851    if BLOCK is zero, use the block of the selected stack frame.
852    Meanwhile, advance *STRINGPTR to point after the expression,
853    at the first nonwhite character that is not part of the expression
854    (possibly a null character).
855
856    If COMMA is nonzero, stop if a comma is reached.  */
857
858 struct expression *
859 parse_exp_1 (stringptr, block, comma)
860      char **stringptr;
861      struct block *block;
862      int comma;
863 {
864   struct cleanup *old_chain;
865
866   lexptr = *stringptr;
867
868   paren_depth = 0;
869   type_stack_depth = 0;
870
871   comma_terminates = comma;
872
873   if (lexptr == 0 || *lexptr == 0)
874     error_no_arg ("expression to compute");
875
876   old_chain = make_cleanup (free_funcalls, 0);
877   funcall_chain = 0;
878
879   expression_context_block = block ? block : get_selected_block ();
880
881   namecopy = (char *) alloca (strlen (lexptr) + 1);
882   expout_size = 10;
883   expout_ptr = 0;
884   expout = (struct expression *)
885     xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
886   expout->language_defn = current_language;
887   make_cleanup (free_current_contents, &expout);
888
889   if (current_language->la_parser ())
890     current_language->la_error (NULL);
891
892   discard_cleanups (old_chain);
893
894   /* Record the actual number of expression elements, and then
895      reallocate the expression memory so that we free up any
896      excess elements. */
897
898   expout->nelts = expout_ptr;
899   expout = (struct expression *)
900     xrealloc ((char *) expout,
901               sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
902
903   /* Convert expression from postfix form as generated by yacc
904      parser, to a prefix form. */
905
906   DUMP_EXPRESSION (expout, gdb_stdout, "before conversion to prefix form");
907   prefixify_expression (expout);
908   DUMP_EXPRESSION (expout, gdb_stdout, "after conversion to prefix form");
909
910   *stringptr = lexptr;
911   return expout;
912 }
913
914 /* Parse STRING as an expression, and complain if this fails
915    to use up all of the contents of STRING.  */
916
917 struct expression *
918 parse_expression (string)
919      char *string;
920 {
921   register struct expression *exp;
922   exp = parse_exp_1 (&string, 0, 0);
923   if (*string)
924     error ("Junk after end of expression.");
925   return exp;
926 }
927 \f
928 /* Stuff for maintaining a stack of types.  Currently just used by C, but
929    probably useful for any language which declares its types "backwards".  */
930
931 void 
932 push_type (tp)
933      enum type_pieces tp;
934 {
935   if (type_stack_depth == type_stack_size)
936     {
937       type_stack_size *= 2;
938       type_stack = (union type_stack_elt *)
939         xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
940     }
941   type_stack[type_stack_depth++].piece = tp;
942 }
943
944 void
945 push_type_int (n)
946      int n;
947 {
948   if (type_stack_depth == type_stack_size)
949     {
950       type_stack_size *= 2;
951       type_stack = (union type_stack_elt *)
952         xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
953     }
954   type_stack[type_stack_depth++].int_val = n;
955 }
956
957 enum type_pieces 
958 pop_type ()
959 {
960   if (type_stack_depth)
961     return type_stack[--type_stack_depth].piece;
962   return tp_end;
963 }
964
965 int
966 pop_type_int ()
967 {
968   if (type_stack_depth)
969     return type_stack[--type_stack_depth].int_val;
970   /* "Can't happen".  */
971   return 0;
972 }
973
974 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
975    as modified by all the stuff on the stack.  */
976 struct type *
977 follow_types (follow_type)
978      struct type *follow_type;
979 {
980   int done = 0;
981   int array_size;
982   struct type *range_type;
983
984   while (!done)
985     switch (pop_type ())
986       {
987       case tp_end:
988         done = 1;
989         break;
990       case tp_pointer:
991         follow_type = lookup_pointer_type (follow_type);
992         break;
993       case tp_reference:
994         follow_type = lookup_reference_type (follow_type);
995         break;
996       case tp_array:
997         array_size = pop_type_int ();
998         /* FIXME-type-allocation: need a way to free this type when we are
999            done with it.  */
1000         range_type =
1001           create_range_type ((struct type *) NULL,
1002                              builtin_type_int, 0,
1003                              array_size >= 0 ? array_size - 1 : 0);
1004         follow_type =
1005           create_array_type ((struct type *) NULL,
1006                              follow_type, range_type);
1007         if (array_size < 0)
1008           TYPE_ARRAY_UPPER_BOUND_TYPE(follow_type)
1009             = BOUND_CANNOT_BE_DETERMINED;
1010         break;
1011       case tp_function:
1012         /* FIXME-type-allocation: need a way to free this type when we are
1013            done with it.  */
1014         follow_type = lookup_function_type (follow_type);
1015         break;
1016       }
1017   return follow_type;
1018 }
1019 \f
1020 void
1021 _initialize_parse ()
1022 {
1023   type_stack_size = 80;
1024   type_stack_depth = 0;
1025   type_stack = (union type_stack_elt *)
1026     xmalloc (type_stack_size * sizeof (*type_stack));
1027
1028   msym_text_symbol_type =
1029     init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
1030   TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
1031   msym_data_symbol_type =
1032     init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
1033                "<data variable, no debug info>", NULL);
1034   msym_unknown_symbol_type =
1035     init_type (TYPE_CODE_INT, 1, 0,
1036                "<variable (not text or data), no debug info>",
1037                NULL);
1038 }
This page took 0.085708 seconds and 4 git commands to generate.