xref: /dragonfly/contrib/gdb-7/gdb/expprint.c (revision 0db87cb7)
1 /* Print in infix form a struct expression.
2 
3    Copyright (C) 1986-2013 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "value.h"
25 #include "language.h"
26 #include "parser-defs.h"
27 #include "user-regs.h"		/* For user_reg_map_regnum_to_name.  */
28 #include "target.h"
29 #include "gdb_string.h"
30 #include "block.h"
31 #include "objfiles.h"
32 #include "gdb_assert.h"
33 #include "valprint.h"
34 
35 #include <ctype.h>
36 
37 void
38 print_expression (struct expression *exp, struct ui_file *stream)
39 {
40   int pc = 0;
41 
42   print_subexp (exp, &pc, stream, PREC_NULL);
43 }
44 
45 /* Print the subexpression of EXP that starts in position POS, on STREAM.
46    PREC is the precedence of the surrounding operator;
47    if the precedence of the main operator of this subexpression is less,
48    parentheses are needed here.  */
49 
50 void
51 print_subexp (struct expression *exp, int *pos,
52 	      struct ui_file *stream, enum precedence prec)
53 {
54   exp->language_defn->la_exp_desc->print_subexp (exp, pos, stream, prec);
55 }
56 
57 /* Standard implementation of print_subexp for use in language_defn
58    vectors.  */
59 void
60 print_subexp_standard (struct expression *exp, int *pos,
61 		       struct ui_file *stream, enum precedence prec)
62 {
63   unsigned tem;
64   const struct op_print *op_print_tab;
65   int pc;
66   unsigned nargs;
67   char *op_str;
68   int assign_modify = 0;
69   enum exp_opcode opcode;
70   enum precedence myprec = PREC_NULL;
71   /* Set to 1 for a right-associative operator.  */
72   int assoc = 0;
73   struct value *val;
74   char *tempstr = NULL;
75 
76   op_print_tab = exp->language_defn->la_op_print_tab;
77   pc = (*pos)++;
78   opcode = exp->elts[pc].opcode;
79   switch (opcode)
80     {
81       /* Common ops */
82 
83     case OP_TYPE:
84       (*pos) += 2;
85       type_print (exp->elts[pc + 1].type, "", stream, 0);
86       return;
87 
88     case OP_SCOPE:
89       myprec = PREC_PREFIX;
90       assoc = 0;
91       fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream);
92       fputs_filtered ("::", stream);
93       nargs = longest_to_int (exp->elts[pc + 2].longconst);
94       (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
95       fputs_filtered (&exp->elts[pc + 3].string, stream);
96       return;
97 
98     case OP_LONG:
99       {
100 	struct value_print_options opts;
101 
102 	get_raw_print_options (&opts);
103 	(*pos) += 3;
104 	value_print (value_from_longest (exp->elts[pc + 1].type,
105 					 exp->elts[pc + 2].longconst),
106 		     stream, &opts);
107       }
108       return;
109 
110     case OP_DOUBLE:
111       {
112 	struct value_print_options opts;
113 
114 	get_raw_print_options (&opts);
115 	(*pos) += 3;
116 	value_print (value_from_double (exp->elts[pc + 1].type,
117 					exp->elts[pc + 2].doubleconst),
118 		     stream, &opts);
119       }
120       return;
121 
122     case OP_VAR_VALUE:
123       {
124 	const struct block *b;
125 
126 	(*pos) += 3;
127 	b = exp->elts[pc + 1].block;
128 	if (b != NULL
129 	    && BLOCK_FUNCTION (b) != NULL
130 	    && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)) != NULL)
131 	  {
132 	    fputs_filtered (SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)), stream);
133 	    fputs_filtered ("::", stream);
134 	  }
135 	fputs_filtered (SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol), stream);
136       }
137       return;
138 
139     case OP_VAR_ENTRY_VALUE:
140       {
141 	(*pos) += 2;
142 	fprintf_filtered (stream, "%s@entry",
143 			  SYMBOL_PRINT_NAME (exp->elts[pc + 1].symbol));
144       }
145       return;
146 
147     case OP_LAST:
148       (*pos) += 2;
149       fprintf_filtered (stream, "$%d",
150 			longest_to_int (exp->elts[pc + 1].longconst));
151       return;
152 
153     case OP_REGISTER:
154       {
155 	const char *name = &exp->elts[pc + 2].string;
156 
157 	(*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
158 	fprintf_filtered (stream, "$%s", name);
159 	return;
160       }
161 
162     case OP_BOOL:
163       (*pos) += 2;
164       fprintf_filtered (stream, "%s",
165 			longest_to_int (exp->elts[pc + 1].longconst)
166 			? "TRUE" : "FALSE");
167       return;
168 
169     case OP_INTERNALVAR:
170       (*pos) += 2;
171       fprintf_filtered (stream, "$%s",
172 			internalvar_name (exp->elts[pc + 1].internalvar));
173       return;
174 
175     case OP_FUNCALL:
176       (*pos) += 2;
177       nargs = longest_to_int (exp->elts[pc + 1].longconst);
178       print_subexp (exp, pos, stream, PREC_SUFFIX);
179       fputs_filtered (" (", stream);
180       for (tem = 0; tem < nargs; tem++)
181 	{
182 	  if (tem != 0)
183 	    fputs_filtered (", ", stream);
184 	  print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
185 	}
186       fputs_filtered (")", stream);
187       return;
188 
189     case OP_NAME:
190       nargs = longest_to_int (exp->elts[pc + 1].longconst);
191       (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
192       fputs_filtered (&exp->elts[pc + 2].string, stream);
193       return;
194 
195     case OP_STRING:
196       {
197 	struct value_print_options opts;
198 
199 	nargs = longest_to_int (exp->elts[pc + 1].longconst);
200 	(*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
201 	/* LA_PRINT_STRING will print using the current repeat count threshold.
202 	   If necessary, we can temporarily set it to zero, or pass it as an
203 	   additional parameter to LA_PRINT_STRING.  -fnf */
204 	get_user_print_options (&opts);
205 	LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
206 			 (gdb_byte *) &exp->elts[pc + 2].string, nargs,
207 			 NULL, 0, &opts);
208       }
209       return;
210 
211     case OP_OBJC_NSSTRING:	/* Objective-C Foundation Class
212 				   NSString constant.  */
213       {
214 	struct value_print_options opts;
215 
216 	nargs = longest_to_int (exp->elts[pc + 1].longconst);
217 	(*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
218 	fputs_filtered ("@\"", stream);
219 	get_user_print_options (&opts);
220 	LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
221 			 (gdb_byte *) &exp->elts[pc + 2].string, nargs,
222 			 NULL, 0, &opts);
223 	fputs_filtered ("\"", stream);
224       }
225       return;
226 
227     case OP_OBJC_MSGCALL:
228       {			/* Objective C message (method) call.  */
229 	char *selector;
230 
231 	(*pos) += 3;
232 	nargs = longest_to_int (exp->elts[pc + 2].longconst);
233 	fprintf_unfiltered (stream, "[");
234 	print_subexp (exp, pos, stream, PREC_SUFFIX);
235 	if (0 == target_read_string (exp->elts[pc + 1].longconst,
236 				     &selector, 1024, NULL))
237 	  {
238 	    error (_("bad selector"));
239 	    return;
240 	  }
241 	if (nargs)
242 	  {
243 	    char *s, *nextS;
244 
245 	    s = alloca (strlen (selector) + 1);
246 	    strcpy (s, selector);
247 	    for (tem = 0; tem < nargs; tem++)
248 	      {
249 		nextS = strchr (s, ':');
250 		gdb_assert (nextS);	/* Make sure we found ':'.  */
251 		*nextS = '\0';
252 		fprintf_unfiltered (stream, " %s: ", s);
253 		s = nextS + 1;
254 		print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
255 	      }
256 	  }
257 	else
258 	  {
259 	    fprintf_unfiltered (stream, " %s", selector);
260 	  }
261 	fprintf_unfiltered (stream, "]");
262 	/* "selector" was malloc'd by target_read_string.  Free it.  */
263 	xfree (selector);
264 	return;
265       }
266 
267     case OP_ARRAY:
268       (*pos) += 3;
269       nargs = longest_to_int (exp->elts[pc + 2].longconst);
270       nargs -= longest_to_int (exp->elts[pc + 1].longconst);
271       nargs++;
272       tem = 0;
273       if (exp->elts[pc + 4].opcode == OP_LONG
274 	  && exp->elts[pc + 5].type
275 	     == builtin_type (exp->gdbarch)->builtin_char
276 	  && exp->language_defn->la_language == language_c)
277 	{
278 	  /* Attempt to print C character arrays using string syntax.
279 	     Walk through the args, picking up one character from each
280 	     of the OP_LONG expression elements.  If any array element
281 	     does not match our expection of what we should find for
282 	     a simple string, revert back to array printing.  Note that
283 	     the last expression element is an explicit null terminator
284 	     byte, which doesn't get printed.  */
285 	  tempstr = alloca (nargs);
286 	  pc += 4;
287 	  while (tem < nargs)
288 	    {
289 	      if (exp->elts[pc].opcode != OP_LONG
290 		  || exp->elts[pc + 1].type
291 		     != builtin_type (exp->gdbarch)->builtin_char)
292 		{
293 		  /* Not a simple array of char, use regular array
294 		     printing.  */
295 		  tem = 0;
296 		  break;
297 		}
298 	      else
299 		{
300 		  tempstr[tem++] =
301 		    longest_to_int (exp->elts[pc + 2].longconst);
302 		  pc += 4;
303 		}
304 	    }
305 	}
306       if (tem > 0)
307 	{
308 	  struct value_print_options opts;
309 
310 	  get_user_print_options (&opts);
311 	  LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
312 			   (gdb_byte *) tempstr, nargs - 1, NULL, 0, &opts);
313 	  (*pos) = pc;
314 	}
315       else
316 	{
317 	  fputs_filtered (" {", stream);
318 	  for (tem = 0; tem < nargs; tem++)
319 	    {
320 	      if (tem != 0)
321 		{
322 		  fputs_filtered (", ", stream);
323 		}
324 	      print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
325 	    }
326 	  fputs_filtered ("}", stream);
327 	}
328       return;
329 
330     case TERNOP_COND:
331       if ((int) prec > (int) PREC_COMMA)
332 	fputs_filtered ("(", stream);
333       /* Print the subexpressions, forcing parentheses
334          around any binary operations within them.
335          This is more parentheses than are strictly necessary,
336          but it looks clearer.  */
337       print_subexp (exp, pos, stream, PREC_HYPER);
338       fputs_filtered (" ? ", stream);
339       print_subexp (exp, pos, stream, PREC_HYPER);
340       fputs_filtered (" : ", stream);
341       print_subexp (exp, pos, stream, PREC_HYPER);
342       if ((int) prec > (int) PREC_COMMA)
343 	fputs_filtered (")", stream);
344       return;
345 
346     case TERNOP_SLICE:
347       print_subexp (exp, pos, stream, PREC_SUFFIX);
348       fputs_filtered ("(", stream);
349       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
350       fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
351       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
352       fputs_filtered (")", stream);
353       return;
354 
355     case STRUCTOP_STRUCT:
356       tem = longest_to_int (exp->elts[pc + 1].longconst);
357       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
358       print_subexp (exp, pos, stream, PREC_SUFFIX);
359       fputs_filtered (".", stream);
360       fputs_filtered (&exp->elts[pc + 2].string, stream);
361       return;
362 
363       /* Will not occur for Modula-2.  */
364     case STRUCTOP_PTR:
365       tem = longest_to_int (exp->elts[pc + 1].longconst);
366       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
367       print_subexp (exp, pos, stream, PREC_SUFFIX);
368       fputs_filtered ("->", stream);
369       fputs_filtered (&exp->elts[pc + 2].string, stream);
370       return;
371 
372     case STRUCTOP_MEMBER:
373       print_subexp (exp, pos, stream, PREC_SUFFIX);
374       fputs_filtered (".*", stream);
375       print_subexp (exp, pos, stream, PREC_SUFFIX);
376       return;
377 
378     case STRUCTOP_MPTR:
379       print_subexp (exp, pos, stream, PREC_SUFFIX);
380       fputs_filtered ("->*", stream);
381       print_subexp (exp, pos, stream, PREC_SUFFIX);
382       return;
383 
384     case BINOP_SUBSCRIPT:
385       print_subexp (exp, pos, stream, PREC_SUFFIX);
386       fputs_filtered ("[", stream);
387       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
388       fputs_filtered ("]", stream);
389       return;
390 
391     case UNOP_POSTINCREMENT:
392       print_subexp (exp, pos, stream, PREC_SUFFIX);
393       fputs_filtered ("++", stream);
394       return;
395 
396     case UNOP_POSTDECREMENT:
397       print_subexp (exp, pos, stream, PREC_SUFFIX);
398       fputs_filtered ("--", stream);
399       return;
400 
401     case UNOP_CAST:
402       (*pos) += 2;
403       if ((int) prec > (int) PREC_PREFIX)
404 	fputs_filtered ("(", stream);
405       fputs_filtered ("(", stream);
406       type_print (exp->elts[pc + 1].type, "", stream, 0);
407       fputs_filtered (") ", stream);
408       print_subexp (exp, pos, stream, PREC_PREFIX);
409       if ((int) prec > (int) PREC_PREFIX)
410 	fputs_filtered (")", stream);
411       return;
412 
413     case UNOP_CAST_TYPE:
414       if ((int) prec > (int) PREC_PREFIX)
415 	fputs_filtered ("(", stream);
416       fputs_filtered ("(", stream);
417       print_subexp (exp, pos, stream, PREC_PREFIX);
418       fputs_filtered (") ", stream);
419       print_subexp (exp, pos, stream, PREC_PREFIX);
420       if ((int) prec > (int) PREC_PREFIX)
421 	fputs_filtered (")", stream);
422       return;
423 
424     case UNOP_DYNAMIC_CAST:
425     case UNOP_REINTERPRET_CAST:
426       fputs_filtered (opcode == UNOP_DYNAMIC_CAST ? "dynamic_cast"
427 		      : "reinterpret_cast", stream);
428       fputs_filtered ("<", stream);
429       print_subexp (exp, pos, stream, PREC_PREFIX);
430       fputs_filtered ("> (", stream);
431       print_subexp (exp, pos, stream, PREC_PREFIX);
432       fputs_filtered (")", stream);
433       return;
434 
435     case UNOP_MEMVAL:
436       (*pos) += 2;
437       if ((int) prec > (int) PREC_PREFIX)
438 	fputs_filtered ("(", stream);
439       if (TYPE_CODE (exp->elts[pc + 1].type) == TYPE_CODE_FUNC
440 	  && exp->elts[pc + 3].opcode == OP_LONG)
441 	{
442 	  struct value_print_options opts;
443 
444 	  /* We have a minimal symbol fn, probably.  It's encoded
445 	     as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
446 	     Swallow the OP_LONG (including both its opcodes); ignore
447 	     its type; print the value in the type of the MEMVAL.  */
448 	  (*pos) += 4;
449 	  val = value_at_lazy (exp->elts[pc + 1].type,
450 			       (CORE_ADDR) exp->elts[pc + 5].longconst);
451 	  get_raw_print_options (&opts);
452 	  value_print (val, stream, &opts);
453 	}
454       else
455 	{
456 	  fputs_filtered ("{", stream);
457 	  type_print (exp->elts[pc + 1].type, "", stream, 0);
458 	  fputs_filtered ("} ", stream);
459 	  print_subexp (exp, pos, stream, PREC_PREFIX);
460 	}
461       if ((int) prec > (int) PREC_PREFIX)
462 	fputs_filtered (")", stream);
463       return;
464 
465     case UNOP_MEMVAL_TYPE:
466       if ((int) prec > (int) PREC_PREFIX)
467 	fputs_filtered ("(", stream);
468       fputs_filtered ("{", stream);
469       print_subexp (exp, pos, stream, PREC_PREFIX);
470       fputs_filtered ("} ", stream);
471       print_subexp (exp, pos, stream, PREC_PREFIX);
472       if ((int) prec > (int) PREC_PREFIX)
473 	fputs_filtered (")", stream);
474       return;
475 
476     case UNOP_MEMVAL_TLS:
477       (*pos) += 3;
478       if ((int) prec > (int) PREC_PREFIX)
479 	fputs_filtered ("(", stream);
480       fputs_filtered ("{", stream);
481       type_print (exp->elts[pc + 2].type, "", stream, 0);
482       fputs_filtered ("} ", stream);
483       print_subexp (exp, pos, stream, PREC_PREFIX);
484       if ((int) prec > (int) PREC_PREFIX)
485 	fputs_filtered (")", stream);
486       return;
487 
488     case BINOP_ASSIGN_MODIFY:
489       opcode = exp->elts[pc + 1].opcode;
490       (*pos) += 2;
491       myprec = PREC_ASSIGN;
492       assoc = 1;
493       assign_modify = 1;
494       op_str = "???";
495       for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
496 	if (op_print_tab[tem].opcode == opcode)
497 	  {
498 	    op_str = op_print_tab[tem].string;
499 	    break;
500 	  }
501       if (op_print_tab[tem].opcode != opcode)
502 	/* Not found; don't try to keep going because we don't know how
503 	   to interpret further elements.  */
504 	error (_("Invalid expression"));
505       break;
506 
507       /* C++ ops */
508 
509     case OP_THIS:
510       ++(*pos);
511       if (exp->language_defn->la_name_of_this)
512 	fputs_filtered (exp->language_defn->la_name_of_this, stream);
513       else
514 	fprintf_filtered (stream, _("<language %s has no 'this'>"),
515 			  exp->language_defn->la_name);
516       return;
517 
518       /* Modula-2 ops */
519 
520     case MULTI_SUBSCRIPT:
521       (*pos) += 2;
522       nargs = longest_to_int (exp->elts[pc + 1].longconst);
523       print_subexp (exp, pos, stream, PREC_SUFFIX);
524       fprintf_unfiltered (stream, " [");
525       for (tem = 0; tem < nargs; tem++)
526 	{
527 	  if (tem != 0)
528 	    fprintf_unfiltered (stream, ", ");
529 	  print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
530 	}
531       fprintf_unfiltered (stream, "]");
532       return;
533 
534     case BINOP_VAL:
535       (*pos) += 2;
536       fprintf_unfiltered (stream, "VAL(");
537       type_print (exp->elts[pc + 1].type, "", stream, 0);
538       fprintf_unfiltered (stream, ",");
539       print_subexp (exp, pos, stream, PREC_PREFIX);
540       fprintf_unfiltered (stream, ")");
541       return;
542 
543     case TYPE_INSTANCE:
544       {
545 	LONGEST count = exp->elts[pc + 1].longconst;
546 
547 	/* The COUNT.  */
548 	(*pos)++;
549 	fputs_unfiltered ("TypesInstance(", stream);
550 	while (count-- > 0)
551 	  {
552 	    type_print (exp->elts[(*pos)++].type, "", stream, 0);
553 	    if (count > 0)
554 	      fputs_unfiltered (",", stream);
555 	  }
556 	fputs_unfiltered (",", stream);
557 	/* Ending COUNT and ending TYPE_INSTANCE.  */
558 	(*pos) += 2;
559 	print_subexp (exp, pos, stream, PREC_PREFIX);
560 	fputs_unfiltered (")", stream);
561 	return;
562       }
563 
564       /* Default ops */
565 
566     default:
567       op_str = "???";
568       for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
569 	if (op_print_tab[tem].opcode == opcode)
570 	  {
571 	    op_str = op_print_tab[tem].string;
572 	    myprec = op_print_tab[tem].precedence;
573 	    assoc = op_print_tab[tem].right_assoc;
574 	    break;
575 	  }
576       if (op_print_tab[tem].opcode != opcode)
577 	/* Not found; don't try to keep going because we don't know how
578 	   to interpret further elements.  For example, this happens
579 	   if opcode is OP_TYPE.  */
580 	error (_("Invalid expression"));
581     }
582 
583   /* Note that PREC_BUILTIN will always emit parentheses.  */
584   if ((int) myprec < (int) prec)
585     fputs_filtered ("(", stream);
586   if ((int) opcode > (int) BINOP_END)
587     {
588       if (assoc)
589 	{
590 	  /* Unary postfix operator.  */
591 	  print_subexp (exp, pos, stream, PREC_SUFFIX);
592 	  fputs_filtered (op_str, stream);
593 	}
594       else
595 	{
596 	  /* Unary prefix operator.  */
597 	  fputs_filtered (op_str, stream);
598 	  if (myprec == PREC_BUILTIN_FUNCTION)
599 	    fputs_filtered ("(", stream);
600 	  print_subexp (exp, pos, stream, PREC_PREFIX);
601 	  if (myprec == PREC_BUILTIN_FUNCTION)
602 	    fputs_filtered (")", stream);
603 	}
604     }
605   else
606     {
607       /* Binary operator.  */
608       /* Print left operand.
609          If operator is right-associative,
610          increment precedence for this operand.  */
611       print_subexp (exp, pos, stream,
612 		    (enum precedence) ((int) myprec + assoc));
613       /* Print the operator itself.  */
614       if (assign_modify)
615 	fprintf_filtered (stream, " %s= ", op_str);
616       else if (op_str[0] == ',')
617 	fprintf_filtered (stream, "%s ", op_str);
618       else
619 	fprintf_filtered (stream, " %s ", op_str);
620       /* Print right operand.
621          If operator is left-associative,
622          increment precedence for this operand.  */
623       print_subexp (exp, pos, stream,
624 		    (enum precedence) ((int) myprec + !assoc));
625     }
626 
627   if ((int) myprec < (int) prec)
628     fputs_filtered (")", stream);
629 }
630 
631 /* Return the operator corresponding to opcode OP as
632    a string.   NULL indicates that the opcode was not found in the
633    current language table.  */
634 char *
635 op_string (enum exp_opcode op)
636 {
637   int tem;
638   const struct op_print *op_print_tab;
639 
640   op_print_tab = current_language->la_op_print_tab;
641   for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
642     if (op_print_tab[tem].opcode == op)
643       return op_print_tab[tem].string;
644   return NULL;
645 }
646 
647 /* Support for dumping the raw data from expressions in a human readable
648    form.  */
649 
650 static int dump_subexp_body (struct expression *exp, struct ui_file *, int);
651 
652 /* Name for OPCODE, when it appears in expression EXP.  */
653 
654 char *
655 op_name (struct expression *exp, enum exp_opcode opcode)
656 {
657   return exp->language_defn->la_exp_desc->op_name (opcode);
658 }
659 
660 /* Default name for the standard operator OPCODE (i.e., one defined in
661    the definition of enum exp_opcode).  */
662 
663 char *
664 op_name_standard (enum exp_opcode opcode)
665 {
666   switch (opcode)
667     {
668     default:
669       {
670 	static char buf[30];
671 
672 	xsnprintf (buf, sizeof (buf), "<unknown %d>", opcode);
673 	return buf;
674       }
675 #define OP(name)	\
676     case name:		\
677       return #name ;
678 #include "std-operator.def"
679 #undef OP
680     }
681 }
682 
683 /* Print a raw dump of expression EXP to STREAM.
684    NOTE, if non-NULL, is printed as extra explanatory text.  */
685 
686 void
687 dump_raw_expression (struct expression *exp, struct ui_file *stream,
688 		     char *note)
689 {
690   int elt;
691   char *opcode_name;
692   char *eltscan;
693   int eltsize;
694 
695   fprintf_filtered (stream, "Dump of expression @ ");
696   gdb_print_host_address (exp, stream);
697   if (note)
698     fprintf_filtered (stream, ", %s:", note);
699   fprintf_filtered (stream, "\n\tLanguage %s, %d elements, %ld bytes each.\n",
700 		    exp->language_defn->la_name, exp->nelts,
701 		    (long) sizeof (union exp_element));
702   fprintf_filtered (stream, "\t%5s  %20s  %16s  %s\n", "Index", "Opcode",
703 		    "Hex Value", "String Value");
704   for (elt = 0; elt < exp->nelts; elt++)
705     {
706       fprintf_filtered (stream, "\t%5d  ", elt);
707       opcode_name = op_name (exp, exp->elts[elt].opcode);
708 
709       fprintf_filtered (stream, "%20s  ", opcode_name);
710       print_longest (stream, 'd', 0, exp->elts[elt].longconst);
711       fprintf_filtered (stream, "  ");
712 
713       for (eltscan = (char *) &exp->elts[elt],
714 	   eltsize = sizeof (union exp_element);
715 	   eltsize-- > 0;
716 	   eltscan++)
717 	{
718 	  fprintf_filtered (stream, "%c",
719 			    isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
720 	}
721       fprintf_filtered (stream, "\n");
722     }
723 }
724 
725 /* Dump the subexpression of prefix expression EXP whose operator is at
726    position ELT onto STREAM.  Returns the position of the next
727    subexpression in EXP.  */
728 
729 int
730 dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
731 {
732   static int indent = 0;
733   int i;
734 
735   fprintf_filtered (stream, "\n");
736   fprintf_filtered (stream, "\t%5d  ", elt);
737 
738   for (i = 1; i <= indent; i++)
739     fprintf_filtered (stream, " ");
740   indent += 2;
741 
742   fprintf_filtered (stream, "%-20s  ", op_name (exp, exp->elts[elt].opcode));
743 
744   elt = dump_subexp_body (exp, stream, elt);
745 
746   indent -= 2;
747 
748   return elt;
749 }
750 
751 /* Dump the operands of prefix expression EXP whose opcode is at
752    position ELT onto STREAM.  Returns the position of the next
753    subexpression in EXP.  */
754 
755 static int
756 dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
757 {
758   return exp->language_defn->la_exp_desc->dump_subexp_body (exp, stream, elt);
759 }
760 
761 /* Default value for subexp_body in exp_descriptor vector.  */
762 
763 int
764 dump_subexp_body_standard (struct expression *exp,
765 			   struct ui_file *stream, int elt)
766 {
767   int opcode = exp->elts[elt++].opcode;
768 
769   switch (opcode)
770     {
771     case TERNOP_COND:
772     case TERNOP_SLICE:
773       elt = dump_subexp (exp, stream, elt);
774       /* FALL THROUGH */
775     case BINOP_ADD:
776     case BINOP_SUB:
777     case BINOP_MUL:
778     case BINOP_DIV:
779     case BINOP_REM:
780     case BINOP_MOD:
781     case BINOP_LSH:
782     case BINOP_RSH:
783     case BINOP_LOGICAL_AND:
784     case BINOP_LOGICAL_OR:
785     case BINOP_BITWISE_AND:
786     case BINOP_BITWISE_IOR:
787     case BINOP_BITWISE_XOR:
788     case BINOP_EQUAL:
789     case BINOP_NOTEQUAL:
790     case BINOP_LESS:
791     case BINOP_GTR:
792     case BINOP_LEQ:
793     case BINOP_GEQ:
794     case BINOP_REPEAT:
795     case BINOP_ASSIGN:
796     case BINOP_COMMA:
797     case BINOP_SUBSCRIPT:
798     case BINOP_EXP:
799     case BINOP_MIN:
800     case BINOP_MAX:
801     case BINOP_INTDIV:
802     case BINOP_ASSIGN_MODIFY:
803     case BINOP_VAL:
804     case BINOP_CONCAT:
805     case BINOP_IN:
806     case BINOP_RANGE:
807     case BINOP_END:
808     case STRUCTOP_MEMBER:
809     case STRUCTOP_MPTR:
810       elt = dump_subexp (exp, stream, elt);
811       /* FALL THROUGH */
812     case UNOP_NEG:
813     case UNOP_LOGICAL_NOT:
814     case UNOP_COMPLEMENT:
815     case UNOP_IND:
816     case UNOP_ADDR:
817     case UNOP_PREINCREMENT:
818     case UNOP_POSTINCREMENT:
819     case UNOP_PREDECREMENT:
820     case UNOP_POSTDECREMENT:
821     case UNOP_SIZEOF:
822     case UNOP_PLUS:
823     case UNOP_CAP:
824     case UNOP_CHR:
825     case UNOP_ORD:
826     case UNOP_ABS:
827     case UNOP_FLOAT:
828     case UNOP_HIGH:
829     case UNOP_MAX:
830     case UNOP_MIN:
831     case UNOP_ODD:
832     case UNOP_TRUNC:
833       elt = dump_subexp (exp, stream, elt);
834       break;
835     case OP_LONG:
836       fprintf_filtered (stream, "Type @");
837       gdb_print_host_address (exp->elts[elt].type, stream);
838       fprintf_filtered (stream, " (");
839       type_print (exp->elts[elt].type, NULL, stream, 0);
840       fprintf_filtered (stream, "), value %ld (0x%lx)",
841 			(long) exp->elts[elt + 1].longconst,
842 			(long) exp->elts[elt + 1].longconst);
843       elt += 3;
844       break;
845     case OP_DOUBLE:
846       fprintf_filtered (stream, "Type @");
847       gdb_print_host_address (exp->elts[elt].type, stream);
848       fprintf_filtered (stream, " (");
849       type_print (exp->elts[elt].type, NULL, stream, 0);
850       fprintf_filtered (stream, "), value %g",
851 			(double) exp->elts[elt + 1].doubleconst);
852       elt += 3;
853       break;
854     case OP_VAR_VALUE:
855       fprintf_filtered (stream, "Block @");
856       gdb_print_host_address (exp->elts[elt].block, stream);
857       fprintf_filtered (stream, ", symbol @");
858       gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
859       fprintf_filtered (stream, " (%s)",
860 			SYMBOL_PRINT_NAME (exp->elts[elt + 1].symbol));
861       elt += 3;
862       break;
863     case OP_VAR_ENTRY_VALUE:
864       fprintf_filtered (stream, "Entry value of symbol @");
865       gdb_print_host_address (exp->elts[elt].symbol, stream);
866       fprintf_filtered (stream, " (%s)",
867 			SYMBOL_PRINT_NAME (exp->elts[elt].symbol));
868       elt += 2;
869       break;
870     case OP_LAST:
871       fprintf_filtered (stream, "History element %ld",
872 			(long) exp->elts[elt].longconst);
873       elt += 2;
874       break;
875     case OP_REGISTER:
876       fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
877       elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
878       break;
879     case OP_INTERNALVAR:
880       fprintf_filtered (stream, "Internal var @");
881       gdb_print_host_address (exp->elts[elt].internalvar, stream);
882       fprintf_filtered (stream, " (%s)",
883 			internalvar_name (exp->elts[elt].internalvar));
884       elt += 2;
885       break;
886     case OP_FUNCALL:
887       {
888 	int i, nargs;
889 
890 	nargs = longest_to_int (exp->elts[elt].longconst);
891 
892 	fprintf_filtered (stream, "Number of args: %d", nargs);
893 	elt += 2;
894 
895 	for (i = 1; i <= nargs + 1; i++)
896 	  elt = dump_subexp (exp, stream, elt);
897       }
898       break;
899     case OP_ARRAY:
900       {
901 	int lower, upper;
902 	int i;
903 
904 	lower = longest_to_int (exp->elts[elt].longconst);
905 	upper = longest_to_int (exp->elts[elt + 1].longconst);
906 
907 	fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
908 	elt += 3;
909 
910 	for (i = 1; i <= upper - lower + 1; i++)
911 	  elt = dump_subexp (exp, stream, elt);
912       }
913       break;
914     case UNOP_DYNAMIC_CAST:
915     case UNOP_REINTERPRET_CAST:
916     case UNOP_CAST_TYPE:
917     case UNOP_MEMVAL_TYPE:
918       fprintf_filtered (stream, " (");
919       elt = dump_subexp (exp, stream, elt);
920       fprintf_filtered (stream, ")");
921       elt = dump_subexp (exp, stream, elt);
922       break;
923     case UNOP_MEMVAL:
924     case UNOP_CAST:
925       fprintf_filtered (stream, "Type @");
926       gdb_print_host_address (exp->elts[elt].type, stream);
927       fprintf_filtered (stream, " (");
928       type_print (exp->elts[elt].type, NULL, stream, 0);
929       fprintf_filtered (stream, ")");
930       elt = dump_subexp (exp, stream, elt + 2);
931       break;
932     case UNOP_MEMVAL_TLS:
933       fprintf_filtered (stream, "TLS type @");
934       gdb_print_host_address (exp->elts[elt + 1].type, stream);
935       fprintf_filtered (stream, " (__thread /* \"%s\" */ ",
936                         (exp->elts[elt].objfile == NULL ? "(null)"
937 			 : exp->elts[elt].objfile->name));
938       type_print (exp->elts[elt + 1].type, NULL, stream, 0);
939       fprintf_filtered (stream, ")");
940       elt = dump_subexp (exp, stream, elt + 3);
941       break;
942     case OP_TYPE:
943       fprintf_filtered (stream, "Type @");
944       gdb_print_host_address (exp->elts[elt].type, stream);
945       fprintf_filtered (stream, " (");
946       type_print (exp->elts[elt].type, NULL, stream, 0);
947       fprintf_filtered (stream, ")");
948       elt += 2;
949       break;
950     case OP_TYPEOF:
951     case OP_DECLTYPE:
952       fprintf_filtered (stream, "Typeof (");
953       elt = dump_subexp (exp, stream, elt);
954       fprintf_filtered (stream, ")");
955       break;
956     case STRUCTOP_STRUCT:
957     case STRUCTOP_PTR:
958       {
959 	char *elem_name;
960 	int len;
961 
962 	len = longest_to_int (exp->elts[elt].longconst);
963 	elem_name = &exp->elts[elt + 1].string;
964 
965 	fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
966 	elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
967       }
968       break;
969     case OP_SCOPE:
970       {
971 	char *elem_name;
972 	int len;
973 
974 	fprintf_filtered (stream, "Type @");
975 	gdb_print_host_address (exp->elts[elt].type, stream);
976 	fprintf_filtered (stream, " (");
977 	type_print (exp->elts[elt].type, NULL, stream, 0);
978 	fprintf_filtered (stream, ") ");
979 
980 	len = longest_to_int (exp->elts[elt + 1].longconst);
981 	elem_name = &exp->elts[elt + 2].string;
982 
983 	fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
984 	elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
985       }
986       break;
987     case TYPE_INSTANCE:
988       {
989 	LONGEST len;
990 
991 	len = exp->elts[elt++].longconst;
992 	fprintf_filtered (stream, "%s TypeInstance: ", plongest (len));
993 	while (len-- > 0)
994 	  {
995 	    fprintf_filtered (stream, "Type @");
996 	    gdb_print_host_address (exp->elts[elt].type, stream);
997 	    fprintf_filtered (stream, " (");
998 	    type_print (exp->elts[elt].type, NULL, stream, 0);
999 	    fprintf_filtered (stream, ")");
1000 	    elt++;
1001 	    if (len > 0)
1002 	      fputs_filtered (", ", stream);
1003 	  }
1004 	/* Ending LEN and ending TYPE_INSTANCE.  */
1005 	elt += 2;
1006 	elt = dump_subexp (exp, stream, elt);
1007       }
1008       break;
1009     default:
1010     case OP_NULL:
1011     case MULTI_SUBSCRIPT:
1012     case OP_F77_UNDETERMINED_ARGLIST:
1013     case OP_COMPLEX:
1014     case OP_STRING:
1015     case OP_BOOL:
1016     case OP_M2_STRING:
1017     case OP_THIS:
1018     case OP_NAME:
1019       fprintf_filtered (stream, "Unknown format");
1020     }
1021 
1022   return elt;
1023 }
1024 
1025 void
1026 dump_prefix_expression (struct expression *exp, struct ui_file *stream)
1027 {
1028   int elt;
1029 
1030   fprintf_filtered (stream, "Dump of expression @ ");
1031   gdb_print_host_address (exp, stream);
1032   fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
1033   print_expression (exp, stream);
1034   fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
1035 		    exp->language_defn->la_name, exp->nelts,
1036 		    (long) sizeof (union exp_element));
1037   fputs_filtered ("\n", stream);
1038 
1039   for (elt = 0; elt < exp->nelts;)
1040     elt = dump_subexp (exp, stream, elt);
1041   fputs_filtered ("\n", stream);
1042 }
1043