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