ODR warning for "enum string_repr_result"
[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 (sym->objfile (), 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 value *evaluate_funcall (struct type *expect_type,
1001 struct expression *exp,
1002 enum noside noside,
1003 const std::vector<operation_up> &args) override;
1004
1005 /* Try to complete this operation in the context of EXP. TRACKER is
1006 the completion tracker to update. Return true if completion was
1007 possible, false otherwise. */
1008 virtual bool complete (struct expression *exp, completion_tracker &tracker)
1009 {
1010 return complete (exp, tracker, "");
1011 }
1012
1013 protected:
1014
1015 /* Do the work of the public 'complete' method. PREFIX is prepended
1016 to each result. */
1017 bool complete (struct expression *exp, completion_tracker &tracker,
1018 const char *prefix);
1019
1020 using tuple_holding_operation::tuple_holding_operation;
1021 };
1022
1023 class structop_operation
1024 : public structop_base_operation
1025 {
1026 public:
1027
1028 using structop_base_operation::structop_base_operation;
1029
1030 value *evaluate (struct type *expect_type,
1031 struct expression *exp,
1032 enum noside noside) override
1033 {
1034 value *val =std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1035 return eval_op_structop_struct (expect_type, exp, noside, val,
1036 std::get<1> (m_storage).c_str ());
1037 }
1038
1039 enum exp_opcode opcode () const override
1040 { return STRUCTOP_STRUCT; }
1041
1042 protected:
1043
1044 void do_generate_ax (struct expression *exp,
1045 struct agent_expr *ax,
1046 struct axs_value *value,
1047 struct type *cast_type)
1048 override
1049 {
1050 gen_expr_structop (exp, STRUCTOP_STRUCT,
1051 std::get<0> (this->m_storage).get (),
1052 std::get<1> (this->m_storage).c_str (),
1053 ax, value);
1054 }
1055 };
1056
1057 class structop_ptr_operation
1058 : public structop_base_operation
1059 {
1060 public:
1061
1062 using structop_base_operation::structop_base_operation;
1063
1064 value *evaluate (struct type *expect_type,
1065 struct expression *exp,
1066 enum noside noside) override
1067 {
1068 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1069 return eval_op_structop_ptr (expect_type, exp, noside, val,
1070 std::get<1> (m_storage).c_str ());
1071 }
1072
1073 enum exp_opcode opcode () const override
1074 { return STRUCTOP_PTR; }
1075
1076 protected:
1077
1078 void do_generate_ax (struct expression *exp,
1079 struct agent_expr *ax,
1080 struct axs_value *value,
1081 struct type *cast_type)
1082 override
1083 {
1084 gen_expr_structop (exp, STRUCTOP_PTR,
1085 std::get<0> (this->m_storage).get (),
1086 std::get<1> (this->m_storage).c_str (),
1087 ax, value);
1088 }
1089 };
1090
1091 class structop_member_base
1092 : public tuple_holding_operation<operation_up, operation_up>
1093 {
1094 public:
1095
1096 using tuple_holding_operation::tuple_holding_operation;
1097
1098 value *evaluate_funcall (struct type *expect_type,
1099 struct expression *exp,
1100 enum noside noside,
1101 const std::vector<operation_up> &args) override;
1102 };
1103
1104 class structop_member_operation
1105 : public structop_member_base
1106 {
1107 public:
1108
1109 using structop_member_base::structop_member_base;
1110
1111 value *evaluate (struct type *expect_type,
1112 struct expression *exp,
1113 enum noside noside) override
1114 {
1115 value *lhs
1116 = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
1117 value *rhs
1118 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1119 return eval_op_member (expect_type, exp, noside, lhs, rhs);
1120 }
1121
1122 enum exp_opcode opcode () const override
1123 { return STRUCTOP_MEMBER; }
1124 };
1125
1126 class structop_mptr_operation
1127 : public structop_member_base
1128 {
1129 public:
1130
1131 using structop_member_base::structop_member_base;
1132
1133 value *evaluate (struct type *expect_type,
1134 struct expression *exp,
1135 enum noside noside) override
1136 {
1137 value *lhs
1138 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1139 value *rhs
1140 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1141 return eval_op_member (expect_type, exp, noside, lhs, rhs);
1142 }
1143
1144 enum exp_opcode opcode () const override
1145 { return STRUCTOP_MPTR; }
1146 };
1147
1148 class concat_operation
1149 : public maybe_constant_operation<operation_up, operation_up>
1150 {
1151 public:
1152
1153 using maybe_constant_operation::maybe_constant_operation;
1154
1155 value *evaluate (struct type *expect_type,
1156 struct expression *exp,
1157 enum noside noside) override
1158 {
1159 value *lhs
1160 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1161 value *rhs
1162 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1163 return value_concat (lhs, rhs);
1164 }
1165
1166 enum exp_opcode opcode () const override
1167 { return BINOP_CONCAT; }
1168 };
1169
1170 class add_operation
1171 : public maybe_constant_operation<operation_up, operation_up>
1172 {
1173 public:
1174
1175 using maybe_constant_operation::maybe_constant_operation;
1176
1177 value *evaluate (struct type *expect_type,
1178 struct expression *exp,
1179 enum noside noside) override
1180 {
1181 value *lhs
1182 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1183 value *rhs
1184 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1185 return eval_op_add (expect_type, exp, noside, lhs, rhs);
1186 }
1187
1188 enum exp_opcode opcode () const override
1189 { return BINOP_ADD; }
1190
1191 protected:
1192
1193 void do_generate_ax (struct expression *exp,
1194 struct agent_expr *ax,
1195 struct axs_value *value,
1196 struct type *cast_type)
1197 override
1198 {
1199 gen_expr_binop (exp, BINOP_ADD,
1200 std::get<0> (this->m_storage).get (),
1201 std::get<1> (this->m_storage).get (),
1202 ax, value);
1203 }
1204 };
1205
1206 class sub_operation
1207 : public maybe_constant_operation<operation_up, operation_up>
1208 {
1209 public:
1210
1211 using maybe_constant_operation::maybe_constant_operation;
1212
1213 value *evaluate (struct type *expect_type,
1214 struct expression *exp,
1215 enum noside noside) override
1216 {
1217 value *lhs
1218 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1219 value *rhs
1220 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1221 return eval_op_sub (expect_type, exp, noside, lhs, rhs);
1222 }
1223
1224 enum exp_opcode opcode () const override
1225 { return BINOP_SUB; }
1226
1227 protected:
1228
1229 void do_generate_ax (struct expression *exp,
1230 struct agent_expr *ax,
1231 struct axs_value *value,
1232 struct type *cast_type)
1233 override
1234 {
1235 gen_expr_binop (exp, BINOP_SUB,
1236 std::get<0> (this->m_storage).get (),
1237 std::get<1> (this->m_storage).get (),
1238 ax, value);
1239 }
1240 };
1241
1242 typedef struct value *binary_ftype (struct type *expect_type,
1243 struct expression *exp,
1244 enum noside noside, enum exp_opcode op,
1245 struct value *arg1, struct value *arg2);
1246
1247 template<enum exp_opcode OP, binary_ftype FUNC>
1248 class binop_operation
1249 : public maybe_constant_operation<operation_up, operation_up>
1250 {
1251 public:
1252
1253 using maybe_constant_operation::maybe_constant_operation;
1254
1255 value *evaluate (struct type *expect_type,
1256 struct expression *exp,
1257 enum noside noside) override
1258 {
1259 value *lhs
1260 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1261 value *rhs
1262 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1263 return FUNC (expect_type, exp, noside, OP, lhs, rhs);
1264 }
1265
1266 enum exp_opcode opcode () const override
1267 { return OP; }
1268 };
1269
1270 template<enum exp_opcode OP, binary_ftype FUNC>
1271 class usual_ax_binop_operation
1272 : public binop_operation<OP, FUNC>
1273 {
1274 public:
1275
1276 using binop_operation<OP, FUNC>::binop_operation;
1277
1278 protected:
1279
1280 void do_generate_ax (struct expression *exp,
1281 struct agent_expr *ax,
1282 struct axs_value *value,
1283 struct type *cast_type)
1284 override
1285 {
1286 gen_expr_binop (exp, OP,
1287 std::get<0> (this->m_storage).get (),
1288 std::get<1> (this->m_storage).get (),
1289 ax, value);
1290 }
1291 };
1292
1293 using exp_operation = binop_operation<BINOP_EXP, eval_op_binary>;
1294 using intdiv_operation = binop_operation<BINOP_INTDIV, eval_op_binary>;
1295 using mod_operation = binop_operation<BINOP_MOD, eval_op_binary>;
1296
1297 using mul_operation = usual_ax_binop_operation<BINOP_MUL, eval_op_binary>;
1298 using div_operation = usual_ax_binop_operation<BINOP_DIV, eval_op_binary>;
1299 using rem_operation = usual_ax_binop_operation<BINOP_REM, eval_op_binary>;
1300 using lsh_operation = usual_ax_binop_operation<BINOP_LSH, eval_op_binary>;
1301 using rsh_operation = usual_ax_binop_operation<BINOP_RSH, eval_op_binary>;
1302 using bitwise_and_operation
1303 = usual_ax_binop_operation<BINOP_BITWISE_AND, eval_op_binary>;
1304 using bitwise_ior_operation
1305 = usual_ax_binop_operation<BINOP_BITWISE_IOR, eval_op_binary>;
1306 using bitwise_xor_operation
1307 = usual_ax_binop_operation<BINOP_BITWISE_XOR, eval_op_binary>;
1308
1309 class subscript_operation
1310 : public usual_ax_binop_operation<BINOP_SUBSCRIPT, eval_op_subscript>
1311 {
1312 public:
1313 using usual_ax_binop_operation<BINOP_SUBSCRIPT,
1314 eval_op_subscript>::usual_ax_binop_operation;
1315
1316 value *evaluate_for_sizeof (struct expression *exp,
1317 enum noside noside) override;
1318 };
1319
1320 /* Implementation of comparison operations. */
1321 template<enum exp_opcode OP, binary_ftype FUNC>
1322 class comparison_operation
1323 : public usual_ax_binop_operation<OP, FUNC>
1324 {
1325 public:
1326
1327 using usual_ax_binop_operation<OP, FUNC>::usual_ax_binop_operation;
1328
1329 value *evaluate (struct type *expect_type,
1330 struct expression *exp,
1331 enum noside noside) override
1332 {
1333 value *lhs
1334 = std::get<0> (this->m_storage)->evaluate (nullptr, exp, noside);
1335 value *rhs
1336 = std::get<1> (this->m_storage)->evaluate (value_type (lhs), exp,
1337 noside);
1338 return FUNC (expect_type, exp, noside, OP, lhs, rhs);
1339 }
1340 };
1341
1342 class equal_operation
1343 : public comparison_operation<BINOP_EQUAL, eval_op_equal>
1344 {
1345 public:
1346
1347 using comparison_operation::comparison_operation;
1348
1349 operation *get_lhs () const
1350 {
1351 return std::get<0> (m_storage).get ();
1352 }
1353
1354 operation *get_rhs () const
1355 {
1356 return std::get<1> (m_storage).get ();
1357 }
1358 };
1359
1360 using notequal_operation
1361 = comparison_operation<BINOP_NOTEQUAL, eval_op_notequal>;
1362 using less_operation = comparison_operation<BINOP_LESS, eval_op_less>;
1363 using gtr_operation = comparison_operation<BINOP_GTR, eval_op_gtr>;
1364 using geq_operation = comparison_operation<BINOP_GEQ, eval_op_geq>;
1365 using leq_operation = comparison_operation<BINOP_LEQ, eval_op_leq>;
1366
1367 /* Implement the GDB '@' repeat operator. */
1368 class repeat_operation
1369 : public binop_operation<BINOP_REPEAT, eval_op_repeat>
1370 {
1371 using binop_operation<BINOP_REPEAT, eval_op_repeat>::binop_operation;
1372
1373 protected:
1374
1375 void do_generate_ax (struct expression *exp,
1376 struct agent_expr *ax,
1377 struct axs_value *value,
1378 struct type *cast_type)
1379 override;
1380 };
1381
1382 /* C-style comma operator. */
1383 class comma_operation
1384 : public maybe_constant_operation<operation_up, operation_up>
1385 {
1386 public:
1387
1388 using maybe_constant_operation::maybe_constant_operation;
1389
1390 value *evaluate (struct type *expect_type,
1391 struct expression *exp,
1392 enum noside noside) override
1393 {
1394 /* The left-hand-side is only evaluated for side effects, so don't
1395 bother in other modes. */
1396 if (noside == EVAL_NORMAL)
1397 std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1398 return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1399 }
1400
1401 enum exp_opcode opcode () const override
1402 { return BINOP_COMMA; }
1403
1404 protected:
1405
1406 void do_generate_ax (struct expression *exp,
1407 struct agent_expr *ax,
1408 struct axs_value *value,
1409 struct type *cast_type)
1410 override;
1411 };
1412
1413 typedef struct value *unary_ftype (struct type *expect_type,
1414 struct expression *exp,
1415 enum noside noside, enum exp_opcode op,
1416 struct value *arg1);
1417
1418 /* Base class for unary operations. */
1419 template<enum exp_opcode OP, unary_ftype FUNC>
1420 class unop_operation
1421 : public maybe_constant_operation<operation_up>
1422 {
1423 public:
1424
1425 using maybe_constant_operation::maybe_constant_operation;
1426
1427 value *evaluate (struct type *expect_type,
1428 struct expression *exp,
1429 enum noside noside) override
1430 {
1431 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1432 return FUNC (expect_type, exp, noside, OP, val);
1433 }
1434
1435 enum exp_opcode opcode () const override
1436 { return OP; }
1437 };
1438
1439 /* Unary operations that can also be turned into agent expressions in
1440 the "usual" way. */
1441 template<enum exp_opcode OP, unary_ftype FUNC>
1442 class usual_ax_unop_operation
1443 : public unop_operation<OP, FUNC>
1444 {
1445 using unop_operation<OP, FUNC>::unop_operation;
1446
1447 protected:
1448
1449 void do_generate_ax (struct expression *exp,
1450 struct agent_expr *ax,
1451 struct axs_value *value,
1452 struct type *cast_type)
1453 override
1454 {
1455 gen_expr_unop (exp, OP,
1456 std::get<0> (this->m_storage).get (),
1457 ax, value);
1458 }
1459 };
1460
1461 using unary_plus_operation = usual_ax_unop_operation<UNOP_PLUS, eval_op_plus>;
1462 using unary_neg_operation = usual_ax_unop_operation<UNOP_NEG, eval_op_neg>;
1463 using unary_complement_operation
1464 = usual_ax_unop_operation<UNOP_COMPLEMENT, eval_op_complement>;
1465 using unary_logical_not_operation
1466 = usual_ax_unop_operation<UNOP_LOGICAL_NOT, eval_op_lognot>;
1467
1468 /* Handle pre- and post- increment and -decrement. */
1469 template<enum exp_opcode OP, unary_ftype FUNC>
1470 class unop_incr_operation
1471 : public tuple_holding_operation<operation_up>
1472 {
1473 public:
1474
1475 using tuple_holding_operation::tuple_holding_operation;
1476
1477 value *evaluate (struct type *expect_type,
1478 struct expression *exp,
1479 enum noside noside) override
1480 {
1481 value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1482 return FUNC (expect_type, exp, noside, OP, val);
1483 }
1484
1485 enum exp_opcode opcode () const override
1486 { return OP; }
1487 };
1488
1489 using preinc_operation
1490 = unop_incr_operation<UNOP_PREINCREMENT, eval_op_preinc>;
1491 using predec_operation
1492 = unop_incr_operation<UNOP_PREDECREMENT, eval_op_predec>;
1493 using postinc_operation
1494 = unop_incr_operation<UNOP_POSTINCREMENT, eval_op_postinc>;
1495 using postdec_operation
1496 = unop_incr_operation<UNOP_POSTDECREMENT, eval_op_postdec>;
1497
1498 /* Base class for implementations of UNOP_IND. */
1499 class unop_ind_base_operation
1500 : public tuple_holding_operation<operation_up>
1501 {
1502 public:
1503
1504 using tuple_holding_operation::tuple_holding_operation;
1505
1506 value *evaluate (struct type *expect_type,
1507 struct expression *exp,
1508 enum noside noside) override
1509 {
1510 if (expect_type != nullptr && expect_type->code () == TYPE_CODE_PTR)
1511 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
1512 value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1513 return eval_op_ind (expect_type, exp, noside, val);
1514 }
1515
1516 value *evaluate_for_address (struct expression *exp,
1517 enum noside noside) override;
1518
1519 value *evaluate_for_sizeof (struct expression *exp,
1520 enum noside noside) override;
1521
1522 enum exp_opcode opcode () const override
1523 { return UNOP_IND; }
1524 };
1525
1526 /* Ordinary UNOP_IND implementation. */
1527 class unop_ind_operation
1528 : public unop_ind_base_operation
1529 {
1530 public:
1531
1532 using unop_ind_base_operation::unop_ind_base_operation;
1533
1534 protected:
1535
1536 void do_generate_ax (struct expression *exp,
1537 struct agent_expr *ax,
1538 struct axs_value *value,
1539 struct type *cast_type)
1540 override
1541 {
1542 gen_expr_unop (exp, UNOP_IND,
1543 std::get<0> (this->m_storage).get (),
1544 ax, value);
1545 }
1546 };
1547
1548 /* Implement OP_TYPE. */
1549 class type_operation
1550 : public tuple_holding_operation<struct type *>
1551 {
1552 public:
1553
1554 using tuple_holding_operation::tuple_holding_operation;
1555
1556 value *evaluate (struct type *expect_type,
1557 struct expression *exp,
1558 enum noside noside) override
1559 {
1560 return eval_op_type (expect_type, exp, noside, std::get<0> (m_storage));
1561 }
1562
1563 enum exp_opcode opcode () const override
1564 { return OP_TYPE; }
1565
1566 bool constant_p () const override
1567 { return true; }
1568 };
1569
1570 /* Implement the "typeof" operation. */
1571 class typeof_operation
1572 : public maybe_constant_operation<operation_up>
1573 {
1574 public:
1575
1576 using maybe_constant_operation::maybe_constant_operation;
1577
1578 value *evaluate (struct type *expect_type,
1579 struct expression *exp,
1580 enum noside noside) override
1581 {
1582 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1583 return std::get<0> (m_storage)->evaluate (nullptr, exp,
1584 EVAL_AVOID_SIDE_EFFECTS);
1585 else
1586 error (_("Attempt to use a type as an expression"));
1587 }
1588
1589 enum exp_opcode opcode () const override
1590 { return OP_TYPEOF; }
1591 };
1592
1593 /* Implement 'decltype'. */
1594 class decltype_operation
1595 : public maybe_constant_operation<operation_up>
1596 {
1597 public:
1598
1599 using maybe_constant_operation::maybe_constant_operation;
1600
1601 value *evaluate (struct type *expect_type,
1602 struct expression *exp,
1603 enum noside noside) override
1604 {
1605 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1606 {
1607 value *result
1608 = std::get<0> (m_storage)->evaluate (nullptr, exp,
1609 EVAL_AVOID_SIDE_EFFECTS);
1610 enum exp_opcode sub_op = std::get<0> (m_storage)->opcode ();
1611 if (sub_op == BINOP_SUBSCRIPT
1612 || sub_op == STRUCTOP_MEMBER
1613 || sub_op == STRUCTOP_MPTR
1614 || sub_op == UNOP_IND
1615 || sub_op == STRUCTOP_STRUCT
1616 || sub_op == STRUCTOP_PTR
1617 || sub_op == OP_SCOPE)
1618 {
1619 struct type *type = value_type (result);
1620
1621 if (!TYPE_IS_REFERENCE (type))
1622 {
1623 type = lookup_lvalue_reference_type (type);
1624 result = allocate_value (type);
1625 }
1626 }
1627
1628 return result;
1629 }
1630 else
1631 error (_("Attempt to use a type as an expression"));
1632 }
1633
1634 enum exp_opcode opcode () const override
1635 { return OP_DECLTYPE; }
1636 };
1637
1638 /* Implement 'typeid'. */
1639 class typeid_operation
1640 : public tuple_holding_operation<operation_up>
1641 {
1642 public:
1643
1644 using tuple_holding_operation::tuple_holding_operation;
1645
1646 value *evaluate (struct type *expect_type,
1647 struct expression *exp,
1648 enum noside noside) override
1649 {
1650 enum exp_opcode sub_op = std::get<0> (m_storage)->opcode ();
1651 enum noside sub_noside
1652 = ((sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
1653 ? EVAL_AVOID_SIDE_EFFECTS
1654 : noside);
1655
1656 value *result = std::get<0> (m_storage)->evaluate (nullptr, exp,
1657 sub_noside);
1658 if (noside != EVAL_NORMAL)
1659 return allocate_value (cplus_typeid_type (exp->gdbarch));
1660 return cplus_typeid (result);
1661 }
1662
1663 enum exp_opcode opcode () const override
1664 { return OP_TYPEID; }
1665 };
1666
1667 /* Implement the address-of operation. */
1668 class unop_addr_operation
1669 : public maybe_constant_operation<operation_up>
1670 {
1671 public:
1672
1673 using maybe_constant_operation::maybe_constant_operation;
1674
1675 value *evaluate (struct type *expect_type,
1676 struct expression *exp,
1677 enum noside noside) override
1678 {
1679 /* C++: check for and handle pointer to members. */
1680 return std::get<0> (m_storage)->evaluate_for_address (exp, noside);
1681 }
1682
1683 enum exp_opcode opcode () const override
1684 { return UNOP_ADDR; }
1685
1686 /* Return the subexpression. */
1687 const operation_up &get_expression () const
1688 {
1689 return std::get<0> (m_storage);
1690 }
1691
1692 protected:
1693
1694 void do_generate_ax (struct expression *exp,
1695 struct agent_expr *ax,
1696 struct axs_value *value,
1697 struct type *cast_type)
1698 override
1699 {
1700 gen_expr_unop (exp, UNOP_ADDR,
1701 std::get<0> (this->m_storage).get (),
1702 ax, value);
1703 }
1704 };
1705
1706 /* Implement 'sizeof'. */
1707 class unop_sizeof_operation
1708 : public maybe_constant_operation<operation_up>
1709 {
1710 public:
1711
1712 using maybe_constant_operation::maybe_constant_operation;
1713
1714 value *evaluate (struct type *expect_type,
1715 struct expression *exp,
1716 enum noside noside) override
1717 {
1718 return std::get<0> (m_storage)->evaluate_for_sizeof (exp, noside);
1719 }
1720
1721 enum exp_opcode opcode () const override
1722 { return UNOP_SIZEOF; }
1723
1724 protected:
1725
1726 void do_generate_ax (struct expression *exp,
1727 struct agent_expr *ax,
1728 struct axs_value *value,
1729 struct type *cast_type)
1730 override;
1731 };
1732
1733 /* Implement 'alignof'. */
1734 class unop_alignof_operation
1735 : public maybe_constant_operation<operation_up>
1736 {
1737 public:
1738
1739 using maybe_constant_operation::maybe_constant_operation;
1740
1741 value *evaluate (struct type *expect_type,
1742 struct expression *exp,
1743 enum noside noside) override
1744 {
1745 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
1746 EVAL_AVOID_SIDE_EFFECTS);
1747 return eval_op_alignof (expect_type, exp, noside, val);
1748 }
1749
1750 enum exp_opcode opcode () const override
1751 { return UNOP_ALIGNOF; }
1752 };
1753
1754 /* Implement UNOP_MEMVAL. */
1755 class unop_memval_operation
1756 : public tuple_holding_operation<operation_up, struct type *>
1757 {
1758 public:
1759
1760 using tuple_holding_operation::tuple_holding_operation;
1761
1762 value *evaluate (struct type *expect_type,
1763 struct expression *exp,
1764 enum noside noside) override
1765 {
1766 value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1767 return eval_op_memval (expect_type, exp, noside, val,
1768 std::get<1> (m_storage));
1769 }
1770
1771 value *evaluate_for_sizeof (struct expression *exp,
1772 enum noside noside) override;
1773
1774 value *evaluate_for_address (struct expression *exp,
1775 enum noside noside) override;
1776
1777 enum exp_opcode opcode () const override
1778 { return UNOP_MEMVAL; }
1779
1780 /* Return the type referenced by this object. */
1781 struct type *get_type () const
1782 {
1783 return std::get<1> (m_storage);
1784 }
1785
1786 protected:
1787
1788 void do_generate_ax (struct expression *exp,
1789 struct agent_expr *ax,
1790 struct axs_value *value,
1791 struct type *cast_type)
1792 override;
1793 };
1794
1795 /* Implement UNOP_MEMVAL_TYPE. */
1796 class unop_memval_type_operation
1797 : public tuple_holding_operation<operation_up, operation_up>
1798 {
1799 public:
1800
1801 using tuple_holding_operation::tuple_holding_operation;
1802
1803 value *evaluate (struct type *expect_type,
1804 struct expression *exp,
1805 enum noside noside) override
1806 {
1807 value *typeval
1808 = std::get<0> (m_storage)->evaluate (expect_type, exp,
1809 EVAL_AVOID_SIDE_EFFECTS);
1810 struct type *type = value_type (typeval);
1811 value *val = std::get<1> (m_storage)->evaluate (expect_type, exp, noside);
1812 return eval_op_memval (expect_type, exp, noside, val, type);
1813 }
1814
1815 value *evaluate_for_sizeof (struct expression *exp,
1816 enum noside noside) override;
1817
1818 value *evaluate_for_address (struct expression *exp,
1819 enum noside noside) override;
1820
1821 enum exp_opcode opcode () const override
1822 { return UNOP_MEMVAL_TYPE; }
1823
1824 protected:
1825
1826 void do_generate_ax (struct expression *exp,
1827 struct agent_expr *ax,
1828 struct axs_value *value,
1829 struct type *cast_type)
1830 override;
1831 };
1832
1833 /* Implement the 'this' expression. */
1834 class op_this_operation
1835 : public tuple_holding_operation<>
1836 {
1837 public:
1838
1839 using tuple_holding_operation::tuple_holding_operation;
1840
1841 value *evaluate (struct type *expect_type,
1842 struct expression *exp,
1843 enum noside noside) override
1844 {
1845 return value_of_this (exp->language_defn);
1846 }
1847
1848 enum exp_opcode opcode () const override
1849 { return OP_THIS; }
1850
1851 protected:
1852
1853 void do_generate_ax (struct expression *exp,
1854 struct agent_expr *ax,
1855 struct axs_value *value,
1856 struct type *cast_type)
1857 override;
1858 };
1859
1860 /* Implement the "type instance" operation. */
1861 class type_instance_operation
1862 : public tuple_holding_operation<type_instance_flags, std::vector<type *>,
1863 operation_up>
1864 {
1865 public:
1866
1867 using tuple_holding_operation::tuple_holding_operation;
1868
1869 value *evaluate (struct type *expect_type,
1870 struct expression *exp,
1871 enum noside noside) override;
1872
1873 enum exp_opcode opcode () const override
1874 { return TYPE_INSTANCE; }
1875 };
1876
1877 /* The assignment operator. */
1878 class assign_operation
1879 : public tuple_holding_operation<operation_up, operation_up>
1880 {
1881 public:
1882
1883 using tuple_holding_operation::tuple_holding_operation;
1884
1885 value *evaluate (struct type *expect_type,
1886 struct expression *exp,
1887 enum noside noside) override
1888 {
1889 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1890 /* Special-case assignments where the left-hand-side is a
1891 convenience variable -- in these, don't bother setting an
1892 expected type. This avoids a weird case where re-assigning a
1893 string or array to an internal variable could error with "Too
1894 many array elements". */
1895 struct type *xtype = (VALUE_LVAL (lhs) == lval_internalvar
1896 ? nullptr
1897 : value_type (lhs));
1898 value *rhs = std::get<1> (m_storage)->evaluate (xtype, exp, noside);
1899
1900 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1901 return lhs;
1902 if (binop_user_defined_p (BINOP_ASSIGN, lhs, rhs))
1903 return value_x_binop (lhs, rhs, BINOP_ASSIGN, OP_NULL, noside);
1904 else
1905 return value_assign (lhs, rhs);
1906 }
1907
1908 enum exp_opcode opcode () const override
1909 { return BINOP_ASSIGN; }
1910
1911 /* Return the left-hand-side of the assignment. */
1912 operation *get_lhs () const
1913 {
1914 return std::get<0> (m_storage).get ();
1915 }
1916
1917 protected:
1918
1919 void do_generate_ax (struct expression *exp,
1920 struct agent_expr *ax,
1921 struct axs_value *value,
1922 struct type *cast_type)
1923 override;
1924 };
1925
1926 /* Assignment with modification, like "+=". */
1927 class assign_modify_operation
1928 : public tuple_holding_operation<exp_opcode, operation_up, operation_up>
1929 {
1930 public:
1931
1932 using tuple_holding_operation::tuple_holding_operation;
1933
1934 value *evaluate (struct type *expect_type,
1935 struct expression *exp,
1936 enum noside noside) override
1937 {
1938 value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1939 value *rhs = std::get<2> (m_storage)->evaluate (expect_type, exp, noside);
1940 return eval_binop_assign_modify (expect_type, exp, noside,
1941 std::get<0> (m_storage), lhs, rhs);
1942 }
1943
1944 enum exp_opcode opcode () const override
1945 { return BINOP_ASSIGN_MODIFY; }
1946
1947 protected:
1948
1949 void do_generate_ax (struct expression *exp,
1950 struct agent_expr *ax,
1951 struct axs_value *value,
1952 struct type *cast_type)
1953 override;
1954 };
1955
1956 /* Not a cast! Extract a value of a given type from the contents of a
1957 value. The new value is extracted from the least significant bytes
1958 of the old value. The new value's type must be no bigger than the
1959 old values type. */
1960 class unop_extract_operation
1961 : public maybe_constant_operation<operation_up, struct type *>
1962 {
1963 public:
1964
1965 using maybe_constant_operation::maybe_constant_operation;
1966
1967 value *evaluate (struct type *expect_type, struct expression *exp,
1968 enum noside noside) override;
1969
1970 enum exp_opcode opcode () const override
1971 { return UNOP_EXTRACT; }
1972
1973 /* Return the type referenced by this object. */
1974 struct type *get_type () const
1975 {
1976 return std::get<1> (m_storage);
1977 }
1978
1979 protected:
1980
1981 void do_generate_ax (struct expression *exp,
1982 struct agent_expr *ax,
1983 struct axs_value *value,
1984 struct type *cast_type) override;
1985 };
1986
1987 /* A type cast. */
1988 class unop_cast_operation
1989 : public maybe_constant_operation<operation_up, struct type *>
1990 {
1991 public:
1992
1993 using maybe_constant_operation::maybe_constant_operation;
1994
1995 value *evaluate (struct type *expect_type,
1996 struct expression *exp,
1997 enum noside noside) override
1998 {
1999 return std::get<0> (m_storage)->evaluate_for_cast (std::get<1> (m_storage),
2000 exp, noside);
2001 }
2002
2003 enum exp_opcode opcode () const override
2004 { return UNOP_CAST; }
2005
2006 /* Return the type referenced by this object. */
2007 struct type *get_type () const
2008 {
2009 return std::get<1> (m_storage);
2010 }
2011
2012 protected:
2013
2014 void do_generate_ax (struct expression *exp,
2015 struct agent_expr *ax,
2016 struct axs_value *value,
2017 struct type *cast_type)
2018 override;
2019 };
2020
2021 /* A cast, but the type comes from an expression, not a "struct
2022 type". */
2023 class unop_cast_type_operation
2024 : public maybe_constant_operation<operation_up, operation_up>
2025 {
2026 public:
2027
2028 using maybe_constant_operation::maybe_constant_operation;
2029
2030 value *evaluate (struct type *expect_type,
2031 struct expression *exp,
2032 enum noside noside) override
2033 {
2034 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2035 EVAL_AVOID_SIDE_EFFECTS);
2036 return std::get<1> (m_storage)->evaluate_for_cast (value_type (val),
2037 exp, noside);
2038 }
2039
2040 enum exp_opcode opcode () const override
2041 { return UNOP_CAST_TYPE; }
2042
2043 protected:
2044
2045 void do_generate_ax (struct expression *exp,
2046 struct agent_expr *ax,
2047 struct axs_value *value,
2048 struct type *cast_type)
2049 override;
2050 };
2051
2052 typedef value *cxx_cast_ftype (struct type *, value *);
2053
2054 /* This implements dynamic_cast and reinterpret_cast. static_cast and
2055 const_cast are handled by the ordinary case operations. */
2056 template<exp_opcode OP, cxx_cast_ftype FUNC>
2057 class cxx_cast_operation
2058 : public maybe_constant_operation<operation_up, operation_up>
2059 {
2060 public:
2061
2062 using maybe_constant_operation::maybe_constant_operation;
2063
2064 value *evaluate (struct type *expect_type,
2065 struct expression *exp,
2066 enum noside noside) override
2067 {
2068 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2069 EVAL_AVOID_SIDE_EFFECTS);
2070 struct type *type = value_type (val);
2071 value *rhs = std::get<1> (m_storage)->evaluate (type, exp, noside);
2072 return FUNC (type, rhs);
2073 }
2074
2075 enum exp_opcode opcode () const override
2076 { return OP; }
2077 };
2078
2079 using dynamic_cast_operation = cxx_cast_operation<UNOP_DYNAMIC_CAST,
2080 value_dynamic_cast>;
2081 using reinterpret_cast_operation = cxx_cast_operation<UNOP_REINTERPRET_CAST,
2082 value_reinterpret_cast>;
2083
2084 /* Multi-dimensional subscripting. */
2085 class multi_subscript_operation
2086 : public tuple_holding_operation<operation_up, std::vector<operation_up>>
2087 {
2088 public:
2089
2090 using tuple_holding_operation::tuple_holding_operation;
2091
2092 value *evaluate (struct type *expect_type,
2093 struct expression *exp,
2094 enum noside noside) override;
2095
2096 enum exp_opcode opcode () const override
2097 { return MULTI_SUBSCRIPT; }
2098 };
2099
2100 /* The "&&" operator. */
2101 class logical_and_operation
2102 : public maybe_constant_operation<operation_up, operation_up>
2103 {
2104 public:
2105
2106 using maybe_constant_operation::maybe_constant_operation;
2107
2108 value *evaluate (struct type *expect_type,
2109 struct expression *exp,
2110 enum noside noside) override;
2111
2112 enum exp_opcode opcode () const override
2113 { return BINOP_LOGICAL_AND; }
2114
2115 protected:
2116
2117 void do_generate_ax (struct expression *exp,
2118 struct agent_expr *ax,
2119 struct axs_value *value,
2120 struct type *cast_type)
2121 override;
2122 };
2123
2124 /* The "||" operator. */
2125 class logical_or_operation
2126 : public maybe_constant_operation<operation_up, operation_up>
2127 {
2128 public:
2129
2130 using maybe_constant_operation::maybe_constant_operation;
2131
2132 value *evaluate (struct type *expect_type,
2133 struct expression *exp,
2134 enum noside noside) override;
2135
2136 enum exp_opcode opcode () const override
2137 { return BINOP_LOGICAL_OR; }
2138
2139 protected:
2140
2141 void do_generate_ax (struct expression *exp,
2142 struct agent_expr *ax,
2143 struct axs_value *value,
2144 struct type *cast_type)
2145 override;
2146 };
2147
2148 /* This class implements ADL (aka Koenig) function calls for C++. It
2149 holds the name of the function to call, the block in which the
2150 lookup should be done, and a vector of arguments. */
2151 class adl_func_operation
2152 : public tuple_holding_operation<std::string, const block *,
2153 std::vector<operation_up>>
2154 {
2155 public:
2156
2157 using tuple_holding_operation::tuple_holding_operation;
2158
2159 value *evaluate (struct type *expect_type,
2160 struct expression *exp,
2161 enum noside noside) override;
2162
2163 enum exp_opcode opcode () const override
2164 { return OP_ADL_FUNC; }
2165 };
2166
2167 /* The OP_ARRAY operation. */
2168 class array_operation
2169 : public tuple_holding_operation<int, int, std::vector<operation_up>>
2170 {
2171 public:
2172
2173 using tuple_holding_operation::tuple_holding_operation;
2174
2175 value *evaluate (struct type *expect_type,
2176 struct expression *exp,
2177 enum noside noside) override;
2178
2179 enum exp_opcode opcode () const override
2180 { return OP_ARRAY; }
2181
2182 private:
2183
2184 struct value *evaluate_struct_tuple (struct value *struct_val,
2185 struct expression *exp,
2186 enum noside noside, int nargs);
2187 };
2188
2189 /* A function call. This holds the callee operation and the
2190 arguments. */
2191 class funcall_operation
2192 : public tuple_holding_operation<operation_up, std::vector<operation_up>>
2193 {
2194 public:
2195
2196 using tuple_holding_operation::tuple_holding_operation;
2197
2198 value *evaluate (struct type *expect_type,
2199 struct expression *exp,
2200 enum noside noside) override
2201 {
2202 return std::get<0> (m_storage)->evaluate_funcall (expect_type, exp, noside,
2203 std::get<1> (m_storage));
2204 }
2205
2206 enum exp_opcode opcode () const override
2207 { return OP_FUNCALL; }
2208 };
2209
2210 } /* namespace expr */
2211
2212 #endif /* EXPOP_H */