gdb/tui: fairer distribution of excess space during apply
[binutils-gdb.git] / gdb / expop.h
1 /* Definitions for expressions in GDB
2
3 Copyright (C) 2020-2022 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 #ifndef EXPOP_H
21 #define EXPOP_H
22
23 #include "block.h"
24 #include "c-lang.h"
25 #include "cp-abi.h"
26 #include "expression.h"
27 #include "objfiles.h"
28 #include "gdbsupport/traits.h"
29 #include "gdbsupport/enum-flags.h"
30
31 struct agent_expr;
32 struct axs_value;
33
34 extern void gen_expr_binop (struct expression *exp,
35 enum exp_opcode op,
36 expr::operation *lhs, expr::operation *rhs,
37 struct agent_expr *ax, struct axs_value *value);
38 extern void gen_expr_structop (struct expression *exp,
39 enum exp_opcode op,
40 expr::operation *lhs,
41 const char *name,
42 struct agent_expr *ax, struct axs_value *value);
43 extern void gen_expr_unop (struct expression *exp,
44 enum exp_opcode op,
45 expr::operation *lhs,
46 struct agent_expr *ax, struct axs_value *value);
47
48 extern struct value *eval_op_scope (struct type *expect_type,
49 struct expression *exp,
50 enum noside noside,
51 struct type *type, const char *string);
52 extern struct value *eval_op_var_msym_value (struct type *expect_type,
53 struct expression *exp,
54 enum noside noside,
55 bool outermost_p,
56 bound_minimal_symbol msymbol);
57 extern struct value *eval_op_var_entry_value (struct type *expect_type,
58 struct expression *exp,
59 enum noside noside, symbol *sym);
60 extern struct value *eval_op_func_static_var (struct type *expect_type,
61 struct expression *exp,
62 enum noside noside,
63 value *func, const char *var);
64 extern struct value *eval_op_register (struct type *expect_type,
65 struct expression *exp,
66 enum noside noside, const char *name);
67 extern struct value *eval_op_ternop (struct type *expect_type,
68 struct expression *exp,
69 enum noside noside,
70 struct value *array, struct value *low,
71 struct value *upper);
72 extern struct value *eval_op_structop_struct (struct type *expect_type,
73 struct expression *exp,
74 enum noside noside,
75 struct value *arg1,
76 const char *string);
77 extern struct value *eval_op_structop_ptr (struct type *expect_type,
78 struct expression *exp,
79 enum noside noside,
80 struct value *arg1,
81 const char *string);
82 extern struct value *eval_op_member (struct type *expect_type,
83 struct expression *exp,
84 enum noside noside,
85 struct value *arg1, struct value *arg2);
86 extern struct value *eval_op_add (struct type *expect_type,
87 struct expression *exp,
88 enum noside noside,
89 struct value *arg1, struct value *arg2);
90 extern struct value *eval_op_sub (struct type *expect_type,
91 struct expression *exp,
92 enum noside noside,
93 struct value *arg1, struct value *arg2);
94 extern struct value *eval_op_binary (struct type *expect_type,
95 struct expression *exp,
96 enum noside noside, enum exp_opcode op,
97 struct value *arg1, struct value *arg2);
98 extern struct value *eval_op_subscript (struct type *expect_type,
99 struct expression *exp,
100 enum noside noside, enum exp_opcode op,
101 struct value *arg1,
102 struct value *arg2);
103 extern struct value *eval_op_equal (struct type *expect_type,
104 struct expression *exp,
105 enum noside noside, enum exp_opcode op,
106 struct value *arg1,
107 struct value *arg2);
108 extern struct value *eval_op_notequal (struct type *expect_type,
109 struct expression *exp,
110 enum noside noside, enum exp_opcode op,
111 struct value *arg1,
112 struct value *arg2);
113 extern struct value *eval_op_less (struct type *expect_type,
114 struct expression *exp,
115 enum noside noside, enum exp_opcode op,
116 struct value *arg1,
117 struct value *arg2);
118 extern struct value *eval_op_gtr (struct type *expect_type,
119 struct expression *exp,
120 enum noside noside, enum exp_opcode op,
121 struct value *arg1,
122 struct value *arg2);
123 extern struct value *eval_op_geq (struct type *expect_type,
124 struct expression *exp,
125 enum noside noside, enum exp_opcode op,
126 struct value *arg1,
127 struct value *arg2);
128 extern struct value *eval_op_leq (struct type *expect_type,
129 struct expression *exp,
130 enum noside noside, enum exp_opcode op,
131 struct value *arg1,
132 struct value *arg2);
133 extern struct value *eval_op_repeat (struct type *expect_type,
134 struct expression *exp,
135 enum noside noside, enum exp_opcode op,
136 struct value *arg1,
137 struct value *arg2);
138 extern struct value *eval_op_plus (struct type *expect_type,
139 struct expression *exp,
140 enum noside noside, enum exp_opcode op,
141 struct value *arg1);
142 extern struct value *eval_op_neg (struct type *expect_type,
143 struct expression *exp,
144 enum noside noside, enum exp_opcode op,
145 struct value *arg1);
146 extern struct value *eval_op_complement (struct type *expect_type,
147 struct expression *exp,
148 enum noside noside,
149 enum exp_opcode op,
150 struct value *arg1);
151 extern struct value *eval_op_lognot (struct type *expect_type,
152 struct expression *exp,
153 enum noside noside,
154 enum exp_opcode op,
155 struct value *arg1);
156 extern struct value *eval_op_preinc (struct type *expect_type,
157 struct expression *exp,
158 enum noside noside,
159 enum exp_opcode op,
160 struct value *arg1);
161 extern struct value *eval_op_predec (struct type *expect_type,
162 struct expression *exp,
163 enum noside noside,
164 enum exp_opcode op,
165 struct value *arg1);
166 extern struct value *eval_op_postinc (struct type *expect_type,
167 struct expression *exp,
168 enum noside noside,
169 enum exp_opcode op,
170 struct value *arg1);
171 extern struct value *eval_op_postdec (struct type *expect_type,
172 struct expression *exp,
173 enum noside noside,
174 enum exp_opcode op,
175 struct value *arg1);
176 extern struct value *eval_op_ind (struct type *expect_type,
177 struct expression *exp,
178 enum noside noside,
179 struct value *arg1);
180 extern struct value *eval_op_type (struct type *expect_type,
181 struct expression *exp,
182 enum noside noside, struct type *type);
183 extern struct value *eval_op_alignof (struct type *expect_type,
184 struct expression *exp,
185 enum noside noside,
186 struct value *arg1);
187 extern struct value *eval_op_memval (struct type *expect_type,
188 struct expression *exp,
189 enum noside noside,
190 struct value *arg1, struct type *type);
191 extern struct value *eval_binop_assign_modify (struct type *expect_type,
192 struct expression *exp,
193 enum noside noside,
194 enum exp_opcode op,
195 struct value *arg1,
196 struct value *arg2);
197
198 namespace expr
199 {
200
201 class ada_component;
202
203 /* The check_objfile overloads are used to check whether a particular
204 component of some operation references an objfile. The passed-in
205 objfile will never be a debug objfile. */
206
207 /* See if EXP_OBJFILE matches OBJFILE. */
208 static inline bool
209 check_objfile (struct objfile *exp_objfile, struct objfile *objfile)
210 {
211 if (exp_objfile->separate_debug_objfile_backlink)
212 exp_objfile = exp_objfile->separate_debug_objfile_backlink;
213 return exp_objfile == objfile;
214 }
215
216 static inline bool
217 check_objfile (struct type *type, struct objfile *objfile)
218 {
219 struct objfile *ty_objfile = type->objfile_owner ();
220 if (ty_objfile != nullptr)
221 return check_objfile (ty_objfile, objfile);
222 return false;
223 }
224
225 static inline bool
226 check_objfile (struct symbol *sym, struct objfile *objfile)
227 {
228 return check_objfile (symbol_objfile (sym), objfile);
229 }
230
231 static inline bool
232 check_objfile (const struct block *block, struct objfile *objfile)
233 {
234 return check_objfile (block_objfile (block), objfile);
235 }
236
237 static inline bool
238 check_objfile (const block_symbol &sym, struct objfile *objfile)
239 {
240 return (check_objfile (sym.symbol, objfile)
241 || check_objfile (sym.block, objfile));
242 }
243
244 static inline bool
245 check_objfile (bound_minimal_symbol minsym, struct objfile *objfile)
246 {
247 return check_objfile (minsym.objfile, objfile);
248 }
249
250 static inline bool
251 check_objfile (internalvar *ivar, struct objfile *objfile)
252 {
253 return false;
254 }
255
256 static inline bool
257 check_objfile (const std::string &str, struct objfile *objfile)
258 {
259 return false;
260 }
261
262 static inline bool
263 check_objfile (const operation_up &op, struct objfile *objfile)
264 {
265 return op->uses_objfile (objfile);
266 }
267
268 static inline bool
269 check_objfile (enum exp_opcode val, struct objfile *objfile)
270 {
271 return false;
272 }
273
274 static inline bool
275 check_objfile (ULONGEST val, struct objfile *objfile)
276 {
277 return false;
278 }
279
280 template<typename T>
281 static inline bool
282 check_objfile (enum_flags<T> val, struct objfile *objfile)
283 {
284 return false;
285 }
286
287 template<typename T>
288 static inline bool
289 check_objfile (const std::vector<T> &collection, struct objfile *objfile)
290 {
291 for (const auto &item : collection)
292 {
293 if (check_objfile (item, objfile))
294 return true;
295 }
296 return false;
297 }
298
299 template<typename S, typename T>
300 static inline bool
301 check_objfile (const std::pair<S, T> &item, struct objfile *objfile)
302 {
303 return (check_objfile (item.first, objfile)
304 || check_objfile (item.second, objfile));
305 }
306
307 extern bool check_objfile (const std::unique_ptr<ada_component> &comp,
308 struct objfile *objfile);
309
310 static inline void
311 dump_for_expression (struct ui_file *stream, int depth,
312 const operation_up &op)
313 {
314 op->dump (stream, depth);
315 }
316
317 extern void dump_for_expression (struct ui_file *stream, int depth,
318 enum exp_opcode op);
319 extern void dump_for_expression (struct ui_file *stream, int depth,
320 const std::string &str);
321 extern void dump_for_expression (struct ui_file *stream, int depth,
322 struct type *type);
323 extern void dump_for_expression (struct ui_file *stream, int depth,
324 CORE_ADDR addr);
325 extern void dump_for_expression (struct ui_file *stream, int depth,
326 internalvar *ivar);
327 extern void dump_for_expression (struct ui_file *stream, int depth,
328 symbol *sym);
329 extern void dump_for_expression (struct ui_file *stream, int depth,
330 const block_symbol &sym);
331 extern void dump_for_expression (struct ui_file *stream, int depth,
332 bound_minimal_symbol msym);
333 extern void dump_for_expression (struct ui_file *stream, int depth,
334 const block *bl);
335 extern void dump_for_expression (struct ui_file *stream, int depth,
336 type_instance_flags flags);
337 extern void dump_for_expression (struct ui_file *stream, int depth,
338 enum c_string_type_values flags);
339 extern void dump_for_expression (struct ui_file *stream, int depth,
340 enum range_flag flags);
341 extern void dump_for_expression (struct ui_file *stream, int depth,
342 const std::unique_ptr<ada_component> &comp);
343
344 template<typename T>
345 void
346 dump_for_expression (struct ui_file *stream, int depth,
347 const std::vector<T> &vals)
348 {
349 gdb_printf (stream, _("%*sVector:\n"), depth, "");
350 for (auto &item : vals)
351 dump_for_expression (stream, depth + 1, item);
352 }
353
354 template<typename X, typename Y>
355 void
356 dump_for_expression (struct ui_file *stream, int depth,
357 const std::pair<X, Y> &vals)
358 {
359 dump_for_expression (stream, depth, vals.first);
360 dump_for_expression (stream, depth, vals.second);
361 }
362
363 /* Base class for most concrete operations. This class holds data,
364 specified via template parameters, and supplies generic
365 implementations of the 'dump' and 'uses_objfile' methods. */
366 template<typename... Arg>
367 class tuple_holding_operation : public operation
368 {
369 public:
370
371 explicit tuple_holding_operation (Arg... args)
372 : m_storage (std::forward<Arg> (args)...)
373 {
374 }
375
376 DISABLE_COPY_AND_ASSIGN (tuple_holding_operation);
377
378 bool uses_objfile (struct objfile *objfile) const override
379 {
380 return do_check_objfile<0, Arg...> (objfile, m_storage);
381 }
382
383 void dump (struct ui_file *stream, int depth) const override
384 {
385 dump_for_expression (stream, depth, opcode ());
386 do_dump<0, Arg...> (stream, depth + 1, m_storage);
387 }
388
389 protected:
390
391 /* Storage for the data. */
392 std::tuple<Arg...> m_storage;
393
394 private:
395
396 /* do_dump does the work of dumping the data. */
397 template<int I, typename... T>
398 typename std::enable_if<I == sizeof... (T), void>::type
399 do_dump (struct ui_file *stream, int depth, const std::tuple<T...> &value)
400 const
401 {
402 }
403
404 template<int I, typename... T>
405 typename std::enable_if<I < sizeof... (T), void>::type
406 do_dump (struct ui_file *stream, int depth, const std::tuple<T...> &value)
407 const
408 {
409 dump_for_expression (stream, depth, std::get<I> (value));
410 do_dump<I + 1, T...> (stream, depth, value);
411 }
412
413 /* do_check_objfile does the work of checking whether this object
414 refers to OBJFILE. */
415 template<int I, typename... T>
416 typename std::enable_if<I == sizeof... (T), bool>::type
417 do_check_objfile (struct objfile *objfile, const std::tuple<T...> &value)
418 const
419 {
420 return false;
421 }
422
423 template<int I, typename... T>
424 typename std::enable_if<I < sizeof... (T), bool>::type
425 do_check_objfile (struct objfile *objfile, const std::tuple<T...> &value)
426 const
427 {
428 if (check_objfile (std::get<I> (value), objfile))
429 return true;
430 return do_check_objfile<I + 1, T...> (objfile, value);
431 }
432 };
433
434 /* The check_constant overloads are used to decide whether a given
435 concrete operation is a constant. This is done by checking the
436 operands. */
437
438 static inline bool
439 check_constant (const operation_up &item)
440 {
441 return item->constant_p ();
442 }
443
444 static inline bool
445 check_constant (bound_minimal_symbol msym)
446 {
447 return false;
448 }
449
450 static inline bool
451 check_constant (struct type *type)
452 {
453 return true;
454 }
455
456 static inline bool
457 check_constant (const struct block *block)
458 {
459 return true;
460 }
461
462 static inline bool
463 check_constant (const std::string &str)
464 {
465 return true;
466 }
467
468 static inline bool
469 check_constant (ULONGEST cst)
470 {
471 return true;
472 }
473
474 static inline bool
475 check_constant (struct symbol *sym)
476 {
477 enum address_class sc = sym->aclass ();
478 return (sc == LOC_BLOCK
479 || sc == LOC_CONST
480 || sc == LOC_CONST_BYTES
481 || sc == LOC_LABEL);
482 }
483
484 static inline bool
485 check_constant (const block_symbol &sym)
486 {
487 /* We know the block is constant, so we only need to check the
488 symbol. */
489 return check_constant (sym.symbol);
490 }
491
492 template<typename T>
493 static inline bool
494 check_constant (const std::vector<T> &collection)
495 {
496 for (const auto &item : collection)
497 if (!check_constant (item))
498 return false;
499 return true;
500 }
501
502 template<typename S, typename T>
503 static inline bool
504 check_constant (const std::pair<S, T> &item)
505 {
506 return check_constant (item.first) && check_constant (item.second);
507 }
508
509 /* Base class for concrete operations. This class supplies an
510 implementation of 'constant_p' that works by checking the
511 operands. */
512 template<typename... Arg>
513 class maybe_constant_operation
514 : public tuple_holding_operation<Arg...>
515 {
516 public:
517
518 using tuple_holding_operation<Arg...>::tuple_holding_operation;
519
520 bool constant_p () const override
521 {
522 return do_check_constant<0, Arg...> (this->m_storage);
523 }
524
525 private:
526
527 template<int I, typename... T>
528 typename std::enable_if<I == sizeof... (T), bool>::type
529 do_check_constant (const std::tuple<T...> &value) const
530 {
531 return true;
532 }
533
534 template<int I, typename... T>
535 typename std::enable_if<I < sizeof... (T), bool>::type
536 do_check_constant (const std::tuple<T...> &value) const
537 {
538 if (!check_constant (std::get<I> (value)))
539 return false;
540 return do_check_constant<I + 1, T...> (value);
541 }
542 };
543
544 /* A floating-point constant. The constant is encoded in the target
545 format. */
546
547 typedef std::array<gdb_byte, 16> float_data;
548
549 /* An operation that holds a floating-point constant of a given
550 type.
551
552 This does not need the facilities provided by
553 tuple_holding_operation, so it does not use it. */
554 class float_const_operation
555 : public operation
556 {
557 public:
558
559 float_const_operation (struct type *type, float_data data)
560 : m_type (type),
561 m_data (data)
562 {
563 }
564
565 value *evaluate (struct type *expect_type,
566 struct expression *exp,
567 enum noside noside) override
568 {
569 return value_from_contents (m_type, m_data.data ());
570 }
571
572 enum exp_opcode opcode () const override
573 { return OP_FLOAT; }
574
575 bool constant_p () const override
576 { return true; }
577
578 void dump (struct ui_file *stream, int depth) const override;
579
580 private:
581
582 struct type *m_type;
583 float_data m_data;
584 };
585
586 class scope_operation
587 : public maybe_constant_operation<struct type *, std::string>
588 {
589 public:
590
591 using maybe_constant_operation::maybe_constant_operation;
592
593 value *evaluate (struct type *expect_type,
594 struct expression *exp,
595 enum noside noside) override
596 {
597 return eval_op_scope (expect_type, exp, noside,
598 std::get<0> (m_storage),
599 std::get<1> (m_storage).c_str ());
600 }
601
602 value *evaluate_for_address (struct expression *exp,
603 enum noside noside) override;
604
605 value *evaluate_funcall (struct type *expect_type,
606 struct expression *exp,
607 enum noside noside,
608 const std::vector<operation_up> &args) override;
609
610 enum exp_opcode opcode () const override
611 { return OP_SCOPE; }
612
613 protected:
614
615 void do_generate_ax (struct expression *exp,
616 struct agent_expr *ax,
617 struct axs_value *value,
618 struct type *cast_type)
619 override;
620 };
621
622 /* Compute the value of a variable. */
623 class var_value_operation
624 : public maybe_constant_operation<block_symbol>
625 {
626 public:
627
628 using maybe_constant_operation::maybe_constant_operation;
629
630 value *evaluate (struct type *expect_type,
631 struct expression *exp,
632 enum noside noside) override;
633
634 value *evaluate_with_coercion (struct expression *exp,
635 enum noside noside) override;
636
637 value *evaluate_for_sizeof (struct expression *exp, enum noside noside)
638 override;
639
640 value *evaluate_for_cast (struct type *expect_type,
641 struct expression *exp,
642 enum noside noside) override;
643
644 value *evaluate_for_address (struct expression *exp, enum noside noside)
645 override;
646
647 value *evaluate_funcall (struct type *expect_type,
648 struct expression *exp,
649 enum noside noside,
650 const std::vector<operation_up> &args) override;
651
652 enum exp_opcode opcode () const override
653 { return OP_VAR_VALUE; }
654
655 /* Return the symbol referenced by this object. */
656 symbol *get_symbol () const
657 {
658 return std::get<0> (m_storage).symbol;
659 }
660
661 protected:
662
663 void do_generate_ax (struct expression *exp,
664 struct agent_expr *ax,
665 struct axs_value *value,
666 struct type *cast_type)
667 override;
668 };
669
670 class long_const_operation
671 : public tuple_holding_operation<struct type *, LONGEST>
672 {
673 public:
674
675 using tuple_holding_operation::tuple_holding_operation;
676
677 value *evaluate (struct type *expect_type,
678 struct expression *exp,
679 enum noside noside) override
680 {
681 return value_from_longest (std::get<0> (m_storage),
682 std::get<1> (m_storage));
683 }
684
685 enum exp_opcode opcode () const override
686 { return OP_LONG; }
687
688 bool constant_p () const override
689 { return true; }
690
691 protected:
692
693 void do_generate_ax (struct expression *exp,
694 struct agent_expr *ax,
695 struct axs_value *value,
696 struct type *cast_type)
697 override;
698 };
699
700 class var_msym_value_operation
701 : public maybe_constant_operation<bound_minimal_symbol>
702 {
703 public:
704
705 using maybe_constant_operation::maybe_constant_operation;
706
707 value *evaluate (struct type *expect_type,
708 struct expression *exp,
709 enum noside noside) override
710 {
711 return eval_op_var_msym_value (expect_type, exp, noside, m_outermost,
712 std::get<0> (m_storage));
713 }
714
715 value *evaluate_for_sizeof (struct expression *exp, enum noside noside)
716 override;
717
718 value *evaluate_for_address (struct expression *exp, enum noside noside)
719 override;
720
721 value *evaluate_for_cast (struct type *expect_type,
722 struct expression *exp,
723 enum noside noside) override;
724
725 value *evaluate_funcall (struct type *expect_type,
726 struct expression *exp,
727 enum noside noside,
728 const std::vector<operation_up> &args) override
729 {
730 const char *name = std::get<0> (m_storage).minsym->print_name ();
731 return operation::evaluate_funcall (expect_type, exp, noside, name, args);
732 }
733
734 enum exp_opcode opcode () const override
735 { return OP_VAR_MSYM_VALUE; }
736
737 void set_outermost () override
738 {
739 m_outermost = true;
740 }
741
742 protected:
743
744 /* True if this is the outermost operation in the expression. */
745 bool m_outermost = false;
746
747 void do_generate_ax (struct expression *exp,
748 struct agent_expr *ax,
749 struct axs_value *value,
750 struct type *cast_type)
751 override;
752 };
753
754 class var_entry_value_operation
755 : public tuple_holding_operation<symbol *>
756 {
757 public:
758
759 using tuple_holding_operation::tuple_holding_operation;
760
761 value *evaluate (struct type *expect_type,
762 struct expression *exp,
763 enum noside noside) override
764 {
765 return eval_op_var_entry_value (expect_type, exp, noside,
766 std::get<0> (m_storage));
767 }
768
769 enum exp_opcode opcode () const override
770 { return OP_VAR_ENTRY_VALUE; }
771 };
772
773 class func_static_var_operation
774 : public maybe_constant_operation<operation_up, std::string>
775 {
776 public:
777
778 using maybe_constant_operation::maybe_constant_operation;
779
780 value *evaluate (struct type *expect_type,
781 struct expression *exp,
782 enum noside noside) override
783 {
784 value *func = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
785 return eval_op_func_static_var (expect_type, exp, noside, func,
786 std::get<1> (m_storage).c_str ());
787 }
788
789 enum exp_opcode opcode () const override
790 { return OP_FUNC_STATIC_VAR; }
791 };
792
793 class last_operation
794 : public tuple_holding_operation<int>
795 {
796 public:
797
798 using tuple_holding_operation::tuple_holding_operation;
799
800 value *evaluate (struct type *expect_type,
801 struct expression *exp,
802 enum noside noside) override
803 {
804 return access_value_history (std::get<0> (m_storage));
805 }
806
807 enum exp_opcode opcode () const override
808 { return OP_LAST; }
809 };
810
811 class register_operation
812 : public tuple_holding_operation<std::string>
813 {
814 public:
815
816 using tuple_holding_operation::tuple_holding_operation;
817
818 value *evaluate (struct type *expect_type,
819 struct expression *exp,
820 enum noside noside) override
821 {
822 return eval_op_register (expect_type, exp, noside,
823 std::get<0> (m_storage).c_str ());
824 }
825
826 enum exp_opcode opcode () const override
827 { return OP_REGISTER; }
828
829 /* Return the name of the register. */
830 const char *get_name () const
831 {
832 return std::get<0> (m_storage).c_str ();
833 }
834
835 protected:
836
837 void do_generate_ax (struct expression *exp,
838 struct agent_expr *ax,
839 struct axs_value *value,
840 struct type *cast_type)
841 override;
842 };
843
844 class bool_operation
845 : public tuple_holding_operation<bool>
846 {
847 public:
848
849 using tuple_holding_operation::tuple_holding_operation;
850
851 value *evaluate (struct type *expect_type,
852 struct expression *exp,
853 enum noside noside) override
854 {
855 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
856 return value_from_longest (type, std::get<0> (m_storage));
857 }
858
859 enum exp_opcode opcode () const override
860 { return OP_BOOL; }
861
862 bool constant_p () const override
863 { return true; }
864 };
865
866 class internalvar_operation
867 : public tuple_holding_operation<internalvar *>
868 {
869 public:
870
871 using tuple_holding_operation::tuple_holding_operation;
872
873 value *evaluate (struct type *expect_type,
874 struct expression *exp,
875 enum noside noside) override
876 {
877 return value_of_internalvar (exp->gdbarch,
878 std::get<0> (m_storage));
879 }
880
881 internalvar *get_internalvar () const
882 {
883 return std::get<0> (m_storage);
884 }
885
886 enum exp_opcode opcode () const override
887 { return OP_INTERNALVAR; }
888
889 protected:
890
891 void do_generate_ax (struct expression *exp,
892 struct agent_expr *ax,
893 struct axs_value *value,
894 struct type *cast_type)
895 override;
896 };
897
898 class string_operation
899 : public tuple_holding_operation<std::string>
900 {
901 public:
902
903 using tuple_holding_operation::tuple_holding_operation;
904
905 value *evaluate (struct type *expect_type,
906 struct expression *exp,
907 enum noside noside) override;
908
909 enum exp_opcode opcode () const override
910 { return OP_STRING; }
911 };
912
913 class ternop_slice_operation
914 : public maybe_constant_operation<operation_up, operation_up, operation_up>
915 {
916 public:
917
918 using maybe_constant_operation::maybe_constant_operation;
919
920 value *evaluate (struct type *expect_type,
921 struct expression *exp,
922 enum noside noside) override
923 {
924 struct value *array
925 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
926 struct value *low
927 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
928 struct value *upper
929 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
930 return eval_op_ternop (expect_type, exp, noside, array, low, upper);
931 }
932
933 enum exp_opcode opcode () const override
934 { return TERNOP_SLICE; }
935 };
936
937 class ternop_cond_operation
938 : public maybe_constant_operation<operation_up, operation_up, operation_up>
939 {
940 public:
941
942 using maybe_constant_operation::maybe_constant_operation;
943
944 value *evaluate (struct type *expect_type,
945 struct expression *exp,
946 enum noside noside) override
947 {
948 struct value *val
949 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
950
951 if (value_logical_not (val))
952 return std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
953 return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
954 }
955
956 enum exp_opcode opcode () const override
957 { return TERNOP_COND; }
958
959 protected:
960
961 void do_generate_ax (struct expression *exp,
962 struct agent_expr *ax,
963 struct axs_value *value,
964 struct type *cast_type)
965 override;
966 };
967
968 class complex_operation
969 : public maybe_constant_operation<operation_up, operation_up, struct type *>
970 {
971 public:
972
973 using maybe_constant_operation::maybe_constant_operation;
974
975 value *evaluate (struct type *expect_type,
976 struct expression *exp,
977 enum noside noside) override
978 {
979 value *real = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
980 value *imag = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
981 return value_literal_complex (real, imag,
982 std::get<2> (m_storage));
983 }
984
985 enum exp_opcode opcode () const override
986 { return OP_COMPLEX; }
987 };
988
989 class structop_base_operation
990 : public tuple_holding_operation<operation_up, std::string>
991 {
992 public:
993
994 /* Used for completion. Return the field name. */
995 const std::string &get_string () const
996 {
997 return std::get<1> (m_storage);
998 }
999
1000 /* Used for completion. Evaluate the LHS for type. */
1001 value *evaluate_lhs (struct expression *exp)
1002 {
1003 return std::get<0> (m_storage)->evaluate (nullptr, exp,
1004 EVAL_AVOID_SIDE_EFFECTS);
1005 }
1006
1007 value *evaluate_funcall (struct type *expect_type,
1008 struct expression *exp,
1009 enum noside noside,
1010 const std::vector<operation_up> &args) override;
1011
1012 protected:
1013
1014 using tuple_holding_operation::tuple_holding_operation;
1015 };
1016
1017 class structop_operation
1018 : public structop_base_operation
1019 {
1020 public:
1021
1022 using structop_base_operation::structop_base_operation;
1023
1024 value *evaluate (struct type *expect_type,
1025 struct expression *exp,
1026 enum noside noside) override
1027 {
1028 value *val =std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1029 return eval_op_structop_struct (expect_type, exp, noside, val,
1030 std::get<1> (m_storage).c_str ());
1031 }
1032
1033 enum exp_opcode opcode () const override
1034 { return STRUCTOP_STRUCT; }
1035
1036 protected:
1037
1038 void do_generate_ax (struct expression *exp,
1039 struct agent_expr *ax,
1040 struct axs_value *value,
1041 struct type *cast_type)
1042 override
1043 {
1044 gen_expr_structop (exp, STRUCTOP_STRUCT,
1045 std::get<0> (this->m_storage).get (),
1046 std::get<1> (this->m_storage).c_str (),
1047 ax, value);
1048 }
1049 };
1050
1051 class structop_ptr_operation
1052 : public structop_base_operation
1053 {
1054 public:
1055
1056 using structop_base_operation::structop_base_operation;
1057
1058 value *evaluate (struct type *expect_type,
1059 struct expression *exp,
1060 enum noside noside) override
1061 {
1062 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1063 return eval_op_structop_ptr (expect_type, exp, noside, val,
1064 std::get<1> (m_storage).c_str ());
1065 }
1066
1067 enum exp_opcode opcode () const override
1068 { return STRUCTOP_PTR; }
1069
1070 protected:
1071
1072 void do_generate_ax (struct expression *exp,
1073 struct agent_expr *ax,
1074 struct axs_value *value,
1075 struct type *cast_type)
1076 override
1077 {
1078 gen_expr_structop (exp, STRUCTOP_PTR,
1079 std::get<0> (this->m_storage).get (),
1080 std::get<1> (this->m_storage).c_str (),
1081 ax, value);
1082 }
1083 };
1084
1085 class structop_member_base
1086 : public tuple_holding_operation<operation_up, operation_up>
1087 {
1088 public:
1089
1090 using tuple_holding_operation::tuple_holding_operation;
1091
1092 value *evaluate_funcall (struct type *expect_type,
1093 struct expression *exp,
1094 enum noside noside,
1095 const std::vector<operation_up> &args) override;
1096 };
1097
1098 class structop_member_operation
1099 : public structop_member_base
1100 {
1101 public:
1102
1103 using structop_member_base::structop_member_base;
1104
1105 value *evaluate (struct type *expect_type,
1106 struct expression *exp,
1107 enum noside noside) override
1108 {
1109 value *lhs
1110 = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
1111 value *rhs
1112 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1113 return eval_op_member (expect_type, exp, noside, lhs, rhs);
1114 }
1115
1116 enum exp_opcode opcode () const override
1117 { return STRUCTOP_MEMBER; }
1118 };
1119
1120 class structop_mptr_operation
1121 : public structop_member_base
1122 {
1123 public:
1124
1125 using structop_member_base::structop_member_base;
1126
1127 value *evaluate (struct type *expect_type,
1128 struct expression *exp,
1129 enum noside noside) override
1130 {
1131 value *lhs
1132 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1133 value *rhs
1134 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1135 return eval_op_member (expect_type, exp, noside, lhs, rhs);
1136 }
1137
1138 enum exp_opcode opcode () const override
1139 { return STRUCTOP_MPTR; }
1140 };
1141
1142 class concat_operation
1143 : public maybe_constant_operation<operation_up, operation_up>
1144 {
1145 public:
1146
1147 using maybe_constant_operation::maybe_constant_operation;
1148
1149 value *evaluate (struct type *expect_type,
1150 struct expression *exp,
1151 enum noside noside) override
1152 {
1153 value *lhs
1154 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1155 value *rhs
1156 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1157 return value_concat (lhs, rhs);
1158 }
1159
1160 enum exp_opcode opcode () const override
1161 { return BINOP_CONCAT; }
1162 };
1163
1164 class add_operation
1165 : public maybe_constant_operation<operation_up, operation_up>
1166 {
1167 public:
1168
1169 using maybe_constant_operation::maybe_constant_operation;
1170
1171 value *evaluate (struct type *expect_type,
1172 struct expression *exp,
1173 enum noside noside) override
1174 {
1175 value *lhs
1176 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1177 value *rhs
1178 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1179 return eval_op_add (expect_type, exp, noside, lhs, rhs);
1180 }
1181
1182 enum exp_opcode opcode () const override
1183 { return BINOP_ADD; }
1184
1185 protected:
1186
1187 void do_generate_ax (struct expression *exp,
1188 struct agent_expr *ax,
1189 struct axs_value *value,
1190 struct type *cast_type)
1191 override
1192 {
1193 gen_expr_binop (exp, BINOP_ADD,
1194 std::get<0> (this->m_storage).get (),
1195 std::get<1> (this->m_storage).get (),
1196 ax, value);
1197 }
1198 };
1199
1200 class sub_operation
1201 : public maybe_constant_operation<operation_up, operation_up>
1202 {
1203 public:
1204
1205 using maybe_constant_operation::maybe_constant_operation;
1206
1207 value *evaluate (struct type *expect_type,
1208 struct expression *exp,
1209 enum noside noside) override
1210 {
1211 value *lhs
1212 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1213 value *rhs
1214 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1215 return eval_op_sub (expect_type, exp, noside, lhs, rhs);
1216 }
1217
1218 enum exp_opcode opcode () const override
1219 { return BINOP_SUB; }
1220
1221 protected:
1222
1223 void do_generate_ax (struct expression *exp,
1224 struct agent_expr *ax,
1225 struct axs_value *value,
1226 struct type *cast_type)
1227 override
1228 {
1229 gen_expr_binop (exp, BINOP_SUB,
1230 std::get<0> (this->m_storage).get (),
1231 std::get<1> (this->m_storage).get (),
1232 ax, value);
1233 }
1234 };
1235
1236 typedef struct value *binary_ftype (struct type *expect_type,
1237 struct expression *exp,
1238 enum noside noside, enum exp_opcode op,
1239 struct value *arg1, struct value *arg2);
1240
1241 template<enum exp_opcode OP, binary_ftype FUNC>
1242 class binop_operation
1243 : public maybe_constant_operation<operation_up, operation_up>
1244 {
1245 public:
1246
1247 using maybe_constant_operation::maybe_constant_operation;
1248
1249 value *evaluate (struct type *expect_type,
1250 struct expression *exp,
1251 enum noside noside) override
1252 {
1253 value *lhs
1254 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1255 value *rhs
1256 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1257 return FUNC (expect_type, exp, noside, OP, lhs, rhs);
1258 }
1259
1260 enum exp_opcode opcode () const override
1261 { return OP; }
1262 };
1263
1264 template<enum exp_opcode OP, binary_ftype FUNC>
1265 class usual_ax_binop_operation
1266 : public binop_operation<OP, FUNC>
1267 {
1268 public:
1269
1270 using binop_operation<OP, FUNC>::binop_operation;
1271
1272 protected:
1273
1274 void do_generate_ax (struct expression *exp,
1275 struct agent_expr *ax,
1276 struct axs_value *value,
1277 struct type *cast_type)
1278 override
1279 {
1280 gen_expr_binop (exp, OP,
1281 std::get<0> (this->m_storage).get (),
1282 std::get<1> (this->m_storage).get (),
1283 ax, value);
1284 }
1285 };
1286
1287 using exp_operation = binop_operation<BINOP_EXP, eval_op_binary>;
1288 using intdiv_operation = binop_operation<BINOP_INTDIV, eval_op_binary>;
1289 using mod_operation = binop_operation<BINOP_MOD, eval_op_binary>;
1290
1291 using mul_operation = usual_ax_binop_operation<BINOP_MUL, eval_op_binary>;
1292 using div_operation = usual_ax_binop_operation<BINOP_DIV, eval_op_binary>;
1293 using rem_operation = usual_ax_binop_operation<BINOP_REM, eval_op_binary>;
1294 using lsh_operation = usual_ax_binop_operation<BINOP_LSH, eval_op_binary>;
1295 using rsh_operation = usual_ax_binop_operation<BINOP_RSH, eval_op_binary>;
1296 using bitwise_and_operation
1297 = usual_ax_binop_operation<BINOP_BITWISE_AND, eval_op_binary>;
1298 using bitwise_ior_operation
1299 = usual_ax_binop_operation<BINOP_BITWISE_IOR, eval_op_binary>;
1300 using bitwise_xor_operation
1301 = usual_ax_binop_operation<BINOP_BITWISE_XOR, eval_op_binary>;
1302
1303 class subscript_operation
1304 : public usual_ax_binop_operation<BINOP_SUBSCRIPT, eval_op_subscript>
1305 {
1306 public:
1307 using usual_ax_binop_operation<BINOP_SUBSCRIPT,
1308 eval_op_subscript>::usual_ax_binop_operation;
1309
1310 value *evaluate_for_sizeof (struct expression *exp,
1311 enum noside noside) override;
1312 };
1313
1314 /* Implementation of comparison operations. */
1315 template<enum exp_opcode OP, binary_ftype FUNC>
1316 class comparison_operation
1317 : public usual_ax_binop_operation<OP, FUNC>
1318 {
1319 public:
1320
1321 using usual_ax_binop_operation<OP, FUNC>::usual_ax_binop_operation;
1322
1323 value *evaluate (struct type *expect_type,
1324 struct expression *exp,
1325 enum noside noside) override
1326 {
1327 value *lhs
1328 = std::get<0> (this->m_storage)->evaluate (nullptr, exp, noside);
1329 value *rhs
1330 = std::get<1> (this->m_storage)->evaluate (value_type (lhs), exp,
1331 noside);
1332 return FUNC (expect_type, exp, noside, OP, lhs, rhs);
1333 }
1334 };
1335
1336 class equal_operation
1337 : public comparison_operation<BINOP_EQUAL, eval_op_equal>
1338 {
1339 public:
1340
1341 using comparison_operation::comparison_operation;
1342
1343 operation *get_lhs () const
1344 {
1345 return std::get<0> (m_storage).get ();
1346 }
1347
1348 operation *get_rhs () const
1349 {
1350 return std::get<1> (m_storage).get ();
1351 }
1352 };
1353
1354 using notequal_operation
1355 = comparison_operation<BINOP_NOTEQUAL, eval_op_notequal>;
1356 using less_operation = comparison_operation<BINOP_LESS, eval_op_less>;
1357 using gtr_operation = comparison_operation<BINOP_GTR, eval_op_gtr>;
1358 using geq_operation = comparison_operation<BINOP_GEQ, eval_op_geq>;
1359 using leq_operation = comparison_operation<BINOP_LEQ, eval_op_leq>;
1360
1361 /* Implement the GDB '@' repeat operator. */
1362 class repeat_operation
1363 : public binop_operation<BINOP_REPEAT, eval_op_repeat>
1364 {
1365 using binop_operation<BINOP_REPEAT, eval_op_repeat>::binop_operation;
1366
1367 protected:
1368
1369 void do_generate_ax (struct expression *exp,
1370 struct agent_expr *ax,
1371 struct axs_value *value,
1372 struct type *cast_type)
1373 override;
1374 };
1375
1376 /* C-style comma operator. */
1377 class comma_operation
1378 : public maybe_constant_operation<operation_up, operation_up>
1379 {
1380 public:
1381
1382 using maybe_constant_operation::maybe_constant_operation;
1383
1384 value *evaluate (struct type *expect_type,
1385 struct expression *exp,
1386 enum noside noside) override
1387 {
1388 /* The left-hand-side is only evaluated for side effects, so don't
1389 bother in other modes. */
1390 if (noside == EVAL_NORMAL)
1391 std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1392 return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1393 }
1394
1395 enum exp_opcode opcode () const override
1396 { return BINOP_COMMA; }
1397
1398 protected:
1399
1400 void do_generate_ax (struct expression *exp,
1401 struct agent_expr *ax,
1402 struct axs_value *value,
1403 struct type *cast_type)
1404 override;
1405 };
1406
1407 typedef struct value *unary_ftype (struct type *expect_type,
1408 struct expression *exp,
1409 enum noside noside, enum exp_opcode op,
1410 struct value *arg1);
1411
1412 /* Base class for unary operations. */
1413 template<enum exp_opcode OP, unary_ftype FUNC>
1414 class unop_operation
1415 : public maybe_constant_operation<operation_up>
1416 {
1417 public:
1418
1419 using maybe_constant_operation::maybe_constant_operation;
1420
1421 value *evaluate (struct type *expect_type,
1422 struct expression *exp,
1423 enum noside noside) override
1424 {
1425 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1426 return FUNC (expect_type, exp, noside, OP, val);
1427 }
1428
1429 enum exp_opcode opcode () const override
1430 { return OP; }
1431 };
1432
1433 /* Unary operations that can also be turned into agent expressions in
1434 the "usual" way. */
1435 template<enum exp_opcode OP, unary_ftype FUNC>
1436 class usual_ax_unop_operation
1437 : public unop_operation<OP, FUNC>
1438 {
1439 using unop_operation<OP, FUNC>::unop_operation;
1440
1441 protected:
1442
1443 void do_generate_ax (struct expression *exp,
1444 struct agent_expr *ax,
1445 struct axs_value *value,
1446 struct type *cast_type)
1447 override
1448 {
1449 gen_expr_unop (exp, OP,
1450 std::get<0> (this->m_storage).get (),
1451 ax, value);
1452 }
1453 };
1454
1455 using unary_plus_operation = usual_ax_unop_operation<UNOP_PLUS, eval_op_plus>;
1456 using unary_neg_operation = usual_ax_unop_operation<UNOP_NEG, eval_op_neg>;
1457 using unary_complement_operation
1458 = usual_ax_unop_operation<UNOP_COMPLEMENT, eval_op_complement>;
1459 using unary_logical_not_operation
1460 = usual_ax_unop_operation<UNOP_LOGICAL_NOT, eval_op_lognot>;
1461
1462 /* Handle pre- and post- increment and -decrement. */
1463 template<enum exp_opcode OP, unary_ftype FUNC>
1464 class unop_incr_operation
1465 : public tuple_holding_operation<operation_up>
1466 {
1467 public:
1468
1469 using tuple_holding_operation::tuple_holding_operation;
1470
1471 value *evaluate (struct type *expect_type,
1472 struct expression *exp,
1473 enum noside noside) override
1474 {
1475 value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1476 return FUNC (expect_type, exp, noside, OP, val);
1477 }
1478
1479 enum exp_opcode opcode () const override
1480 { return OP; }
1481 };
1482
1483 using preinc_operation
1484 = unop_incr_operation<UNOP_PREINCREMENT, eval_op_preinc>;
1485 using predec_operation
1486 = unop_incr_operation<UNOP_PREDECREMENT, eval_op_predec>;
1487 using postinc_operation
1488 = unop_incr_operation<UNOP_POSTINCREMENT, eval_op_postinc>;
1489 using postdec_operation
1490 = unop_incr_operation<UNOP_POSTDECREMENT, eval_op_postdec>;
1491
1492 /* Base class for implementations of UNOP_IND. */
1493 class unop_ind_base_operation
1494 : public tuple_holding_operation<operation_up>
1495 {
1496 public:
1497
1498 using tuple_holding_operation::tuple_holding_operation;
1499
1500 value *evaluate (struct type *expect_type,
1501 struct expression *exp,
1502 enum noside noside) override
1503 {
1504 if (expect_type != nullptr && expect_type->code () == TYPE_CODE_PTR)
1505 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
1506 value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1507 return eval_op_ind (expect_type, exp, noside, val);
1508 }
1509
1510 value *evaluate_for_address (struct expression *exp,
1511 enum noside noside) override;
1512
1513 value *evaluate_for_sizeof (struct expression *exp,
1514 enum noside noside) override;
1515
1516 enum exp_opcode opcode () const override
1517 { return UNOP_IND; }
1518 };
1519
1520 /* Ordinary UNOP_IND implementation. */
1521 class unop_ind_operation
1522 : public unop_ind_base_operation
1523 {
1524 public:
1525
1526 using unop_ind_base_operation::unop_ind_base_operation;
1527
1528 protected:
1529
1530 void do_generate_ax (struct expression *exp,
1531 struct agent_expr *ax,
1532 struct axs_value *value,
1533 struct type *cast_type)
1534 override
1535 {
1536 gen_expr_unop (exp, UNOP_IND,
1537 std::get<0> (this->m_storage).get (),
1538 ax, value);
1539 }
1540 };
1541
1542 /* Implement OP_TYPE. */
1543 class type_operation
1544 : public tuple_holding_operation<struct type *>
1545 {
1546 public:
1547
1548 using tuple_holding_operation::tuple_holding_operation;
1549
1550 value *evaluate (struct type *expect_type,
1551 struct expression *exp,
1552 enum noside noside) override
1553 {
1554 return eval_op_type (expect_type, exp, noside, std::get<0> (m_storage));
1555 }
1556
1557 enum exp_opcode opcode () const override
1558 { return OP_TYPE; }
1559
1560 bool constant_p () const override
1561 { return true; }
1562 };
1563
1564 /* Implement the "typeof" operation. */
1565 class typeof_operation
1566 : public maybe_constant_operation<operation_up>
1567 {
1568 public:
1569
1570 using maybe_constant_operation::maybe_constant_operation;
1571
1572 value *evaluate (struct type *expect_type,
1573 struct expression *exp,
1574 enum noside noside) override
1575 {
1576 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1577 return std::get<0> (m_storage)->evaluate (nullptr, exp,
1578 EVAL_AVOID_SIDE_EFFECTS);
1579 else
1580 error (_("Attempt to use a type as an expression"));
1581 }
1582
1583 enum exp_opcode opcode () const override
1584 { return OP_TYPEOF; }
1585 };
1586
1587 /* Implement 'decltype'. */
1588 class decltype_operation
1589 : public maybe_constant_operation<operation_up>
1590 {
1591 public:
1592
1593 using maybe_constant_operation::maybe_constant_operation;
1594
1595 value *evaluate (struct type *expect_type,
1596 struct expression *exp,
1597 enum noside noside) override
1598 {
1599 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1600 {
1601 value *result
1602 = std::get<0> (m_storage)->evaluate (nullptr, exp,
1603 EVAL_AVOID_SIDE_EFFECTS);
1604 enum exp_opcode sub_op = std::get<0> (m_storage)->opcode ();
1605 if (sub_op == BINOP_SUBSCRIPT
1606 || sub_op == STRUCTOP_MEMBER
1607 || sub_op == STRUCTOP_MPTR
1608 || sub_op == UNOP_IND
1609 || sub_op == STRUCTOP_STRUCT
1610 || sub_op == STRUCTOP_PTR
1611 || sub_op == OP_SCOPE)
1612 {
1613 struct type *type = value_type (result);
1614
1615 if (!TYPE_IS_REFERENCE (type))
1616 {
1617 type = lookup_lvalue_reference_type (type);
1618 result = allocate_value (type);
1619 }
1620 }
1621
1622 return result;
1623 }
1624 else
1625 error (_("Attempt to use a type as an expression"));
1626 }
1627
1628 enum exp_opcode opcode () const override
1629 { return OP_DECLTYPE; }
1630 };
1631
1632 /* Implement 'typeid'. */
1633 class typeid_operation
1634 : public tuple_holding_operation<operation_up>
1635 {
1636 public:
1637
1638 using tuple_holding_operation::tuple_holding_operation;
1639
1640 value *evaluate (struct type *expect_type,
1641 struct expression *exp,
1642 enum noside noside) override
1643 {
1644 enum exp_opcode sub_op = std::get<0> (m_storage)->opcode ();
1645 enum noside sub_noside
1646 = ((sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
1647 ? EVAL_AVOID_SIDE_EFFECTS
1648 : noside);
1649
1650 value *result = std::get<0> (m_storage)->evaluate (nullptr, exp,
1651 sub_noside);
1652 if (noside != EVAL_NORMAL)
1653 return allocate_value (cplus_typeid_type (exp->gdbarch));
1654 return cplus_typeid (result);
1655 }
1656
1657 enum exp_opcode opcode () const override
1658 { return OP_TYPEID; }
1659 };
1660
1661 /* Implement the address-of operation. */
1662 class unop_addr_operation
1663 : public maybe_constant_operation<operation_up>
1664 {
1665 public:
1666
1667 using maybe_constant_operation::maybe_constant_operation;
1668
1669 value *evaluate (struct type *expect_type,
1670 struct expression *exp,
1671 enum noside noside) override
1672 {
1673 /* C++: check for and handle pointer to members. */
1674 return std::get<0> (m_storage)->evaluate_for_address (exp, noside);
1675 }
1676
1677 enum exp_opcode opcode () const override
1678 { return UNOP_ADDR; }
1679
1680 /* Return the subexpression. */
1681 const operation_up &get_expression () const
1682 {
1683 return std::get<0> (m_storage);
1684 }
1685
1686 protected:
1687
1688 void do_generate_ax (struct expression *exp,
1689 struct agent_expr *ax,
1690 struct axs_value *value,
1691 struct type *cast_type)
1692 override
1693 {
1694 gen_expr_unop (exp, UNOP_ADDR,
1695 std::get<0> (this->m_storage).get (),
1696 ax, value);
1697 }
1698 };
1699
1700 /* Implement 'sizeof'. */
1701 class unop_sizeof_operation
1702 : public maybe_constant_operation<operation_up>
1703 {
1704 public:
1705
1706 using maybe_constant_operation::maybe_constant_operation;
1707
1708 value *evaluate (struct type *expect_type,
1709 struct expression *exp,
1710 enum noside noside) override
1711 {
1712 return std::get<0> (m_storage)->evaluate_for_sizeof (exp, noside);
1713 }
1714
1715 enum exp_opcode opcode () const override
1716 { return UNOP_SIZEOF; }
1717
1718 protected:
1719
1720 void do_generate_ax (struct expression *exp,
1721 struct agent_expr *ax,
1722 struct axs_value *value,
1723 struct type *cast_type)
1724 override;
1725 };
1726
1727 /* Implement 'alignof'. */
1728 class unop_alignof_operation
1729 : public maybe_constant_operation<operation_up>
1730 {
1731 public:
1732
1733 using maybe_constant_operation::maybe_constant_operation;
1734
1735 value *evaluate (struct type *expect_type,
1736 struct expression *exp,
1737 enum noside noside) override
1738 {
1739 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
1740 EVAL_AVOID_SIDE_EFFECTS);
1741 return eval_op_alignof (expect_type, exp, noside, val);
1742 }
1743
1744 enum exp_opcode opcode () const override
1745 { return UNOP_ALIGNOF; }
1746 };
1747
1748 /* Implement UNOP_MEMVAL. */
1749 class unop_memval_operation
1750 : public tuple_holding_operation<operation_up, struct type *>
1751 {
1752 public:
1753
1754 using tuple_holding_operation::tuple_holding_operation;
1755
1756 value *evaluate (struct type *expect_type,
1757 struct expression *exp,
1758 enum noside noside) override
1759 {
1760 value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1761 return eval_op_memval (expect_type, exp, noside, val,
1762 std::get<1> (m_storage));
1763 }
1764
1765 value *evaluate_for_sizeof (struct expression *exp,
1766 enum noside noside) override;
1767
1768 value *evaluate_for_address (struct expression *exp,
1769 enum noside noside) override;
1770
1771 enum exp_opcode opcode () const override
1772 { return UNOP_MEMVAL; }
1773
1774 /* Return the type referenced by this object. */
1775 struct type *get_type () const
1776 {
1777 return std::get<1> (m_storage);
1778 }
1779
1780 protected:
1781
1782 void do_generate_ax (struct expression *exp,
1783 struct agent_expr *ax,
1784 struct axs_value *value,
1785 struct type *cast_type)
1786 override;
1787 };
1788
1789 /* Implement UNOP_MEMVAL_TYPE. */
1790 class unop_memval_type_operation
1791 : public tuple_holding_operation<operation_up, operation_up>
1792 {
1793 public:
1794
1795 using tuple_holding_operation::tuple_holding_operation;
1796
1797 value *evaluate (struct type *expect_type,
1798 struct expression *exp,
1799 enum noside noside) override
1800 {
1801 value *typeval
1802 = std::get<0> (m_storage)->evaluate (expect_type, exp,
1803 EVAL_AVOID_SIDE_EFFECTS);
1804 struct type *type = value_type (typeval);
1805 value *val = std::get<1> (m_storage)->evaluate (expect_type, exp, noside);
1806 return eval_op_memval (expect_type, exp, noside, val, type);
1807 }
1808
1809 value *evaluate_for_sizeof (struct expression *exp,
1810 enum noside noside) override;
1811
1812 value *evaluate_for_address (struct expression *exp,
1813 enum noside noside) override;
1814
1815 enum exp_opcode opcode () const override
1816 { return UNOP_MEMVAL_TYPE; }
1817
1818 protected:
1819
1820 void do_generate_ax (struct expression *exp,
1821 struct agent_expr *ax,
1822 struct axs_value *value,
1823 struct type *cast_type)
1824 override;
1825 };
1826
1827 /* Implement the 'this' expression. */
1828 class op_this_operation
1829 : public tuple_holding_operation<>
1830 {
1831 public:
1832
1833 using tuple_holding_operation::tuple_holding_operation;
1834
1835 value *evaluate (struct type *expect_type,
1836 struct expression *exp,
1837 enum noside noside) override
1838 {
1839 return value_of_this (exp->language_defn);
1840 }
1841
1842 enum exp_opcode opcode () const override
1843 { return OP_THIS; }
1844
1845 protected:
1846
1847 void do_generate_ax (struct expression *exp,
1848 struct agent_expr *ax,
1849 struct axs_value *value,
1850 struct type *cast_type)
1851 override;
1852 };
1853
1854 /* Implement the "type instance" operation. */
1855 class type_instance_operation
1856 : public tuple_holding_operation<type_instance_flags, std::vector<type *>,
1857 operation_up>
1858 {
1859 public:
1860
1861 using tuple_holding_operation::tuple_holding_operation;
1862
1863 value *evaluate (struct type *expect_type,
1864 struct expression *exp,
1865 enum noside noside) override;
1866
1867 enum exp_opcode opcode () const override
1868 { return TYPE_INSTANCE; }
1869 };
1870
1871 /* The assignment operator. */
1872 class assign_operation
1873 : public tuple_holding_operation<operation_up, operation_up>
1874 {
1875 public:
1876
1877 using tuple_holding_operation::tuple_holding_operation;
1878
1879 value *evaluate (struct type *expect_type,
1880 struct expression *exp,
1881 enum noside noside) override
1882 {
1883 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1884 /* Special-case assignments where the left-hand-side is a
1885 convenience variable -- in these, don't bother setting an
1886 expected type. This avoids a weird case where re-assigning a
1887 string or array to an internal variable could error with "Too
1888 many array elements". */
1889 struct type *xtype = (VALUE_LVAL (lhs) == lval_internalvar
1890 ? nullptr
1891 : value_type (lhs));
1892 value *rhs = std::get<1> (m_storage)->evaluate (xtype, exp, noside);
1893
1894 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1895 return lhs;
1896 if (binop_user_defined_p (BINOP_ASSIGN, lhs, rhs))
1897 return value_x_binop (lhs, rhs, BINOP_ASSIGN, OP_NULL, noside);
1898 else
1899 return value_assign (lhs, rhs);
1900 }
1901
1902 enum exp_opcode opcode () const override
1903 { return BINOP_ASSIGN; }
1904
1905 /* Return the left-hand-side of the assignment. */
1906 operation *get_lhs () const
1907 {
1908 return std::get<0> (m_storage).get ();
1909 }
1910
1911 protected:
1912
1913 void do_generate_ax (struct expression *exp,
1914 struct agent_expr *ax,
1915 struct axs_value *value,
1916 struct type *cast_type)
1917 override;
1918 };
1919
1920 /* Assignment with modification, like "+=". */
1921 class assign_modify_operation
1922 : public tuple_holding_operation<exp_opcode, operation_up, operation_up>
1923 {
1924 public:
1925
1926 using tuple_holding_operation::tuple_holding_operation;
1927
1928 value *evaluate (struct type *expect_type,
1929 struct expression *exp,
1930 enum noside noside) override
1931 {
1932 value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1933 value *rhs = std::get<2> (m_storage)->evaluate (expect_type, exp, noside);
1934 return eval_binop_assign_modify (expect_type, exp, noside,
1935 std::get<0> (m_storage), lhs, rhs);
1936 }
1937
1938 enum exp_opcode opcode () const override
1939 { return BINOP_ASSIGN_MODIFY; }
1940
1941 protected:
1942
1943 void do_generate_ax (struct expression *exp,
1944 struct agent_expr *ax,
1945 struct axs_value *value,
1946 struct type *cast_type)
1947 override;
1948 };
1949
1950 /* Not a cast! Extract a value of a given type from the contents of a
1951 value. The new value is extracted from the least significant bytes
1952 of the old value. The new value's type must be no bigger than the
1953 old values type. */
1954 class unop_extract_operation
1955 : public maybe_constant_operation<operation_up, struct type *>
1956 {
1957 public:
1958
1959 using maybe_constant_operation::maybe_constant_operation;
1960
1961 value *evaluate (struct type *expect_type, struct expression *exp,
1962 enum noside noside) override;
1963
1964 enum exp_opcode opcode () const override
1965 { return UNOP_EXTRACT; }
1966
1967 /* Return the type referenced by this object. */
1968 struct type *get_type () const
1969 {
1970 return std::get<1> (m_storage);
1971 }
1972
1973 protected:
1974
1975 void do_generate_ax (struct expression *exp,
1976 struct agent_expr *ax,
1977 struct axs_value *value,
1978 struct type *cast_type) override;
1979 };
1980
1981 /* A type cast. */
1982 class unop_cast_operation
1983 : public maybe_constant_operation<operation_up, struct type *>
1984 {
1985 public:
1986
1987 using maybe_constant_operation::maybe_constant_operation;
1988
1989 value *evaluate (struct type *expect_type,
1990 struct expression *exp,
1991 enum noside noside) override
1992 {
1993 return std::get<0> (m_storage)->evaluate_for_cast (std::get<1> (m_storage),
1994 exp, noside);
1995 }
1996
1997 enum exp_opcode opcode () const override
1998 { return UNOP_CAST; }
1999
2000 /* Return the type referenced by this object. */
2001 struct type *get_type () const
2002 {
2003 return std::get<1> (m_storage);
2004 }
2005
2006 protected:
2007
2008 void do_generate_ax (struct expression *exp,
2009 struct agent_expr *ax,
2010 struct axs_value *value,
2011 struct type *cast_type)
2012 override;
2013 };
2014
2015 /* A cast, but the type comes from an expression, not a "struct
2016 type". */
2017 class unop_cast_type_operation
2018 : public maybe_constant_operation<operation_up, operation_up>
2019 {
2020 public:
2021
2022 using maybe_constant_operation::maybe_constant_operation;
2023
2024 value *evaluate (struct type *expect_type,
2025 struct expression *exp,
2026 enum noside noside) override
2027 {
2028 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2029 EVAL_AVOID_SIDE_EFFECTS);
2030 return std::get<1> (m_storage)->evaluate_for_cast (value_type (val),
2031 exp, noside);
2032 }
2033
2034 enum exp_opcode opcode () const override
2035 { return UNOP_CAST_TYPE; }
2036
2037 protected:
2038
2039 void do_generate_ax (struct expression *exp,
2040 struct agent_expr *ax,
2041 struct axs_value *value,
2042 struct type *cast_type)
2043 override;
2044 };
2045
2046 typedef value *cxx_cast_ftype (struct type *, value *);
2047
2048 /* This implements dynamic_cast and reinterpret_cast. static_cast and
2049 const_cast are handled by the ordinary case operations. */
2050 template<exp_opcode OP, cxx_cast_ftype FUNC>
2051 class cxx_cast_operation
2052 : public maybe_constant_operation<operation_up, operation_up>
2053 {
2054 public:
2055
2056 using maybe_constant_operation::maybe_constant_operation;
2057
2058 value *evaluate (struct type *expect_type,
2059 struct expression *exp,
2060 enum noside noside) override
2061 {
2062 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2063 EVAL_AVOID_SIDE_EFFECTS);
2064 struct type *type = value_type (val);
2065 value *rhs = std::get<1> (m_storage)->evaluate (type, exp, noside);
2066 return FUNC (type, rhs);
2067 }
2068
2069 enum exp_opcode opcode () const override
2070 { return OP; }
2071 };
2072
2073 using dynamic_cast_operation = cxx_cast_operation<UNOP_DYNAMIC_CAST,
2074 value_dynamic_cast>;
2075 using reinterpret_cast_operation = cxx_cast_operation<UNOP_REINTERPRET_CAST,
2076 value_reinterpret_cast>;
2077
2078 /* Multi-dimensional subscripting. */
2079 class multi_subscript_operation
2080 : public tuple_holding_operation<operation_up, std::vector<operation_up>>
2081 {
2082 public:
2083
2084 using tuple_holding_operation::tuple_holding_operation;
2085
2086 value *evaluate (struct type *expect_type,
2087 struct expression *exp,
2088 enum noside noside) override;
2089
2090 enum exp_opcode opcode () const override
2091 { return MULTI_SUBSCRIPT; }
2092 };
2093
2094 /* The "&&" operator. */
2095 class logical_and_operation
2096 : public maybe_constant_operation<operation_up, operation_up>
2097 {
2098 public:
2099
2100 using maybe_constant_operation::maybe_constant_operation;
2101
2102 value *evaluate (struct type *expect_type,
2103 struct expression *exp,
2104 enum noside noside) override;
2105
2106 enum exp_opcode opcode () const override
2107 { return BINOP_LOGICAL_AND; }
2108
2109 protected:
2110
2111 void do_generate_ax (struct expression *exp,
2112 struct agent_expr *ax,
2113 struct axs_value *value,
2114 struct type *cast_type)
2115 override;
2116 };
2117
2118 /* The "||" operator. */
2119 class logical_or_operation
2120 : public maybe_constant_operation<operation_up, operation_up>
2121 {
2122 public:
2123
2124 using maybe_constant_operation::maybe_constant_operation;
2125
2126 value *evaluate (struct type *expect_type,
2127 struct expression *exp,
2128 enum noside noside) override;
2129
2130 enum exp_opcode opcode () const override
2131 { return BINOP_LOGICAL_OR; }
2132
2133 protected:
2134
2135 void do_generate_ax (struct expression *exp,
2136 struct agent_expr *ax,
2137 struct axs_value *value,
2138 struct type *cast_type)
2139 override;
2140 };
2141
2142 /* This class implements ADL (aka Koenig) function calls for C++. It
2143 holds the name of the function to call, the block in which the
2144 lookup should be done, and a vector of arguments. */
2145 class adl_func_operation
2146 : public tuple_holding_operation<std::string, const block *,
2147 std::vector<operation_up>>
2148 {
2149 public:
2150
2151 using tuple_holding_operation::tuple_holding_operation;
2152
2153 value *evaluate (struct type *expect_type,
2154 struct expression *exp,
2155 enum noside noside) override;
2156
2157 enum exp_opcode opcode () const override
2158 { return OP_ADL_FUNC; }
2159 };
2160
2161 /* The OP_ARRAY operation. */
2162 class array_operation
2163 : public tuple_holding_operation<int, int, std::vector<operation_up>>
2164 {
2165 public:
2166
2167 using tuple_holding_operation::tuple_holding_operation;
2168
2169 value *evaluate (struct type *expect_type,
2170 struct expression *exp,
2171 enum noside noside) override;
2172
2173 enum exp_opcode opcode () const override
2174 { return OP_ARRAY; }
2175
2176 private:
2177
2178 struct value *evaluate_struct_tuple (struct value *struct_val,
2179 struct expression *exp,
2180 enum noside noside, int nargs);
2181 };
2182
2183 /* A function call. This holds the callee operation and the
2184 arguments. */
2185 class funcall_operation
2186 : public tuple_holding_operation<operation_up, std::vector<operation_up>>
2187 {
2188 public:
2189
2190 using tuple_holding_operation::tuple_holding_operation;
2191
2192 value *evaluate (struct type *expect_type,
2193 struct expression *exp,
2194 enum noside noside) override
2195 {
2196 return std::get<0> (m_storage)->evaluate_funcall (expect_type, exp, noside,
2197 std::get<1> (m_storage));
2198 }
2199
2200 enum exp_opcode opcode () const override
2201 { return OP_FUNCALL; }
2202 };
2203
2204 } /* namespace expr */
2205
2206 #endif /* EXPOP_H */