Implement some unary operations
[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
166 namespace expr
167 {
168
169 /* The check_objfile overloads are used to check whether a particular
170 component of some operation references an objfile. The passed-in
171 objfile will never be a debug objfile. */
172
173 /* See if EXP_OBJFILE matches OBJFILE. */
174 static inline bool
175 check_objfile (struct objfile *exp_objfile, struct objfile *objfile)
176 {
177 if (exp_objfile->separate_debug_objfile_backlink)
178 exp_objfile = exp_objfile->separate_debug_objfile_backlink;
179 return exp_objfile == objfile;
180 }
181
182 static inline bool
183 check_objfile (struct type *type, struct objfile *objfile)
184 {
185 struct objfile *ty_objfile = type->objfile_owner ();
186 if (ty_objfile != nullptr)
187 return check_objfile (ty_objfile, objfile);
188 return false;
189 }
190
191 static inline bool
192 check_objfile (struct symbol *sym, struct objfile *objfile)
193 {
194 return check_objfile (symbol_objfile (sym), objfile);
195 }
196
197 static inline bool
198 check_objfile (const struct block *block, struct objfile *objfile)
199 {
200 return check_objfile (block_objfile (block), objfile);
201 }
202
203 static inline bool
204 check_objfile (minimal_symbol *minsym, struct objfile *objfile)
205 {
206 /* This may seem strange but minsyms are only used with an objfile
207 as well. */
208 return false;
209 }
210
211 static inline bool
212 check_objfile (internalvar *ivar, struct objfile *objfile)
213 {
214 return false;
215 }
216
217 static inline bool
218 check_objfile (const std::string &str, struct objfile *objfile)
219 {
220 return false;
221 }
222
223 static inline bool
224 check_objfile (const operation_up &op, struct objfile *objfile)
225 {
226 return op->uses_objfile (objfile);
227 }
228
229 static inline bool
230 check_objfile (enum exp_opcode val, struct objfile *objfile)
231 {
232 return false;
233 }
234
235 static inline bool
236 check_objfile (ULONGEST val, struct objfile *objfile)
237 {
238 return false;
239 }
240
241 template<typename T>
242 static inline bool
243 check_objfile (enum_flags<T> val, struct objfile *objfile)
244 {
245 return false;
246 }
247
248 template<typename T>
249 static inline bool
250 check_objfile (const std::vector<T> &collection, struct objfile *objfile)
251 {
252 for (const auto &item : collection)
253 {
254 if (check_objfile (item, objfile))
255 return true;
256 }
257 return false;
258 }
259
260 template<typename S, typename T>
261 static inline bool
262 check_objfile (const std::pair<S, T> &item, struct objfile *objfile)
263 {
264 return (check_objfile (item.first, objfile)
265 || check_objfile (item.second, objfile));
266 }
267
268 static inline void
269 dump_for_expression (struct ui_file *stream, int depth,
270 const operation_up &op)
271 {
272 op->dump (stream, depth);
273 }
274
275 extern void dump_for_expression (struct ui_file *stream, int depth,
276 enum exp_opcode op);
277 extern void dump_for_expression (struct ui_file *stream, int depth,
278 const std::string &str);
279 extern void dump_for_expression (struct ui_file *stream, int depth,
280 struct type *type);
281 extern void dump_for_expression (struct ui_file *stream, int depth,
282 CORE_ADDR addr);
283 extern void dump_for_expression (struct ui_file *stream, int depth,
284 internalvar *ivar);
285 extern void dump_for_expression (struct ui_file *stream, int depth,
286 symbol *sym);
287 extern void dump_for_expression (struct ui_file *stream, int depth,
288 minimal_symbol *msym);
289 extern void dump_for_expression (struct ui_file *stream, int depth,
290 const block *bl);
291 extern void dump_for_expression (struct ui_file *stream, int depth,
292 type_instance_flags flags);
293 extern void dump_for_expression (struct ui_file *stream, int depth,
294 enum c_string_type_values flags);
295 extern void dump_for_expression (struct ui_file *stream, int depth,
296 enum range_flag flags);
297 extern void dump_for_expression (struct ui_file *stream, int depth,
298 objfile *objf);
299
300 template<typename T>
301 void
302 dump_for_expression (struct ui_file *stream, int depth,
303 const std::vector<T> &vals)
304 {
305 fprintf_filtered (stream, _("%*sVector:\n"), depth, "");
306 for (auto &item : vals)
307 dump_for_expression (stream, depth + 1, item);
308 }
309
310 template<typename X, typename Y>
311 void
312 dump_for_expression (struct ui_file *stream, int depth,
313 const std::pair<X, Y> &vals)
314 {
315 dump_for_expression (stream, depth, vals.first);
316 dump_for_expression (stream, depth, vals.second);
317 }
318
319 /* Base class for most concrete operations. This class holds data,
320 specified via template parameters, and supplies generic
321 implementations of the 'dump' and 'uses_objfile' methods. */
322 template<typename... Arg>
323 class tuple_holding_operation : public operation
324 {
325 public:
326
327 explicit tuple_holding_operation (Arg... args)
328 : m_storage (std::forward<Arg> (args)...)
329 {
330 }
331
332 DISABLE_COPY_AND_ASSIGN (tuple_holding_operation);
333
334 bool uses_objfile (struct objfile *objfile) const override
335 {
336 return do_check_objfile<0, Arg...> (objfile, m_storage);
337 }
338
339 void dump (struct ui_file *stream, int depth) const override
340 {
341 dump_for_expression (stream, depth, opcode ());
342 do_dump<0, Arg...> (stream, depth + 1, m_storage);
343 }
344
345 protected:
346
347 /* Storage for the data. */
348 std::tuple<Arg...> m_storage;
349
350 private:
351
352 /* do_dump does the work of dumping the data. */
353 template<int I, typename... T>
354 typename std::enable_if<I == sizeof... (T), void>::type
355 do_dump (struct ui_file *stream, int depth, const std::tuple<T...> &value)
356 const
357 {
358 }
359
360 template<int I, typename... T>
361 typename std::enable_if<I < sizeof... (T), void>::type
362 do_dump (struct ui_file *stream, int depth, const std::tuple<T...> &value)
363 const
364 {
365 dump_for_expression (stream, depth, std::get<I> (value));
366 do_dump<I + 1, T...> (stream, depth, value);
367 }
368
369 /* do_check_objfile does the work of checking whether this object
370 refers to OBJFILE. */
371 template<int I, typename... T>
372 typename std::enable_if<I == sizeof... (T), bool>::type
373 do_check_objfile (struct objfile *objfile, const std::tuple<T...> &value)
374 const
375 {
376 return false;
377 }
378
379 template<int I, typename... T>
380 typename std::enable_if<I < sizeof... (T), bool>::type
381 do_check_objfile (struct objfile *objfile, const std::tuple<T...> &value)
382 const
383 {
384 if (check_objfile (std::get<I> (value), objfile))
385 return true;
386 return do_check_objfile<I + 1, T...> (objfile, value);
387 }
388 };
389
390 /* The check_constant overloads are used to decide whether a given
391 concrete operation is a constant. This is done by checking the
392 operands. */
393
394 static inline bool
395 check_constant (const operation_up &item)
396 {
397 return item->constant_p ();
398 }
399
400 static inline bool
401 check_constant (struct minimal_symbol *msym)
402 {
403 return false;
404 }
405
406 static inline bool
407 check_constant (struct type *type)
408 {
409 return true;
410 }
411
412 static inline bool
413 check_constant (const struct block *block)
414 {
415 return true;
416 }
417
418 static inline bool
419 check_constant (const std::string &str)
420 {
421 return true;
422 }
423
424 static inline bool
425 check_constant (struct objfile *objfile)
426 {
427 return true;
428 }
429
430 static inline bool
431 check_constant (ULONGEST cst)
432 {
433 return true;
434 }
435
436 static inline bool
437 check_constant (struct symbol *sym)
438 {
439 enum address_class sc = SYMBOL_CLASS (sym);
440 return (sc == LOC_BLOCK
441 || sc == LOC_CONST
442 || sc == LOC_CONST_BYTES
443 || sc == LOC_LABEL);
444 }
445
446 template<typename T>
447 static inline bool
448 check_constant (const std::vector<T> &collection)
449 {
450 for (const auto &item : collection)
451 if (!check_constant (item))
452 return false;
453 return true;
454 }
455
456 template<typename S, typename T>
457 static inline bool
458 check_constant (const std::pair<S, T> &item)
459 {
460 return check_constant (item.first) && check_constant (item.second);
461 }
462
463 /* Base class for concrete operations. This class supplies an
464 implementation of 'constant_p' that works by checking the
465 operands. */
466 template<typename... Arg>
467 class maybe_constant_operation
468 : public tuple_holding_operation<Arg...>
469 {
470 public:
471
472 using tuple_holding_operation<Arg...>::tuple_holding_operation;
473
474 bool constant_p () const override
475 {
476 return do_check_constant<0, Arg...> (this->m_storage);
477 }
478
479 private:
480
481 template<int I, typename... T>
482 typename std::enable_if<I == sizeof... (T), bool>::type
483 do_check_constant (const std::tuple<T...> &value) const
484 {
485 return true;
486 }
487
488 template<int I, typename... T>
489 typename std::enable_if<I < sizeof... (T), bool>::type
490 do_check_constant (const std::tuple<T...> &value) const
491 {
492 if (!check_constant (std::get<I> (value)))
493 return false;
494 return do_check_constant<I + 1, T...> (value);
495 }
496 };
497
498 /* A floating-point constant. The constant is encoded in the target
499 format. */
500
501 typedef std::array<gdb_byte, 16> float_data;
502
503 /* An operation that holds a floating-point constant of a given
504 type.
505
506 This does not need the facilities provided by
507 tuple_holding_operation, so it does not use it. */
508 class float_const_operation
509 : public operation
510 {
511 public:
512
513 float_const_operation (struct type *type, float_data data)
514 : m_type (type),
515 m_data (data)
516 {
517 }
518
519 value *evaluate (struct type *expect_type,
520 struct expression *exp,
521 enum noside noside) override
522 {
523 return value_from_contents (m_type, m_data.data ());
524 }
525
526 enum exp_opcode opcode () const override
527 { return OP_FLOAT; }
528
529 bool constant_p () const override
530 { return true; }
531
532 void dump (struct ui_file *stream, int depth) const override;
533
534 private:
535
536 struct type *m_type;
537 float_data m_data;
538 };
539
540 class scope_operation
541 : public maybe_constant_operation<struct type *, std::string>
542 {
543 public:
544
545 using maybe_constant_operation::maybe_constant_operation;
546
547 value *evaluate (struct type *expect_type,
548 struct expression *exp,
549 enum noside noside) override
550 {
551 return eval_op_scope (expect_type, exp, noside,
552 std::get<0> (m_storage),
553 std::get<1> (m_storage).c_str ());
554 }
555
556 value *evaluate_for_address (struct expression *exp,
557 enum noside noside) override;
558
559 enum exp_opcode opcode () const override
560 { return OP_SCOPE; }
561
562 protected:
563
564 void do_generate_ax (struct expression *exp,
565 struct agent_expr *ax,
566 struct axs_value *value,
567 struct type *cast_type)
568 override;
569 };
570
571 class long_const_operation
572 : public tuple_holding_operation<struct type *, LONGEST>
573 {
574 public:
575
576 using tuple_holding_operation::tuple_holding_operation;
577
578 value *evaluate (struct type *expect_type,
579 struct expression *exp,
580 enum noside noside) override
581 {
582 return value_from_longest (std::get<0> (m_storage),
583 std::get<1> (m_storage));
584 }
585
586 enum exp_opcode opcode () const override
587 { return OP_LONG; }
588
589 bool constant_p () const override
590 { return true; }
591
592 protected:
593
594 void do_generate_ax (struct expression *exp,
595 struct agent_expr *ax,
596 struct axs_value *value,
597 struct type *cast_type)
598 override;
599 };
600
601 class var_msym_value_operation
602 : public maybe_constant_operation<minimal_symbol *, struct objfile *>
603 {
604 public:
605
606 using maybe_constant_operation::maybe_constant_operation;
607
608 value *evaluate (struct type *expect_type,
609 struct expression *exp,
610 enum noside noside) override
611 {
612 return eval_op_var_msym_value (expect_type, exp, noside, m_outermost,
613 std::get<0> (m_storage),
614 std::get<1> (m_storage));
615 }
616
617 value *evaluate_for_sizeof (struct expression *exp, enum noside noside)
618 override;
619
620 value *evaluate_for_address (struct expression *exp, enum noside noside)
621 override;
622
623 value *evaluate_for_cast (struct type *expect_type,
624 struct expression *exp,
625 enum noside noside) override;
626
627 enum exp_opcode opcode () const override
628 { return OP_VAR_MSYM_VALUE; }
629
630 void set_outermost () override
631 {
632 m_outermost = true;
633 }
634
635 protected:
636
637 /* True if this is the outermost operation in the expression. */
638 bool m_outermost = false;
639
640 void do_generate_ax (struct expression *exp,
641 struct agent_expr *ax,
642 struct axs_value *value,
643 struct type *cast_type)
644 override;
645 };
646
647 class var_entry_value_operation
648 : public tuple_holding_operation<symbol *>
649 {
650 public:
651
652 using tuple_holding_operation::tuple_holding_operation;
653
654 value *evaluate (struct type *expect_type,
655 struct expression *exp,
656 enum noside noside) override
657 {
658 return eval_op_var_entry_value (expect_type, exp, noside,
659 std::get<0> (m_storage));
660 }
661
662 enum exp_opcode opcode () const override
663 { return OP_VAR_ENTRY_VALUE; }
664 };
665
666 class func_static_var_operation
667 : public maybe_constant_operation<operation_up, std::string>
668 {
669 public:
670
671 using maybe_constant_operation::maybe_constant_operation;
672
673 value *evaluate (struct type *expect_type,
674 struct expression *exp,
675 enum noside noside) override
676 {
677 value *func = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
678 return eval_op_func_static_var (expect_type, exp, noside, func,
679 std::get<1> (m_storage).c_str ());
680 }
681
682 enum exp_opcode opcode () const override
683 { return OP_FUNC_STATIC_VAR; }
684 };
685
686 class last_operation
687 : public tuple_holding_operation<int>
688 {
689 public:
690
691 using tuple_holding_operation::tuple_holding_operation;
692
693 value *evaluate (struct type *expect_type,
694 struct expression *exp,
695 enum noside noside) override
696 {
697 return access_value_history (std::get<0> (m_storage));
698 }
699
700 enum exp_opcode opcode () const override
701 { return OP_LAST; }
702 };
703
704 class register_operation
705 : public tuple_holding_operation<std::string>
706 {
707 public:
708
709 using tuple_holding_operation::tuple_holding_operation;
710
711 value *evaluate (struct type *expect_type,
712 struct expression *exp,
713 enum noside noside) override
714 {
715 return eval_op_register (expect_type, exp, noside,
716 std::get<0> (m_storage).c_str ());
717 }
718
719 enum exp_opcode opcode () const override
720 { return OP_REGISTER; }
721
722 protected:
723
724 void do_generate_ax (struct expression *exp,
725 struct agent_expr *ax,
726 struct axs_value *value,
727 struct type *cast_type)
728 override;
729 };
730
731 class bool_operation
732 : public tuple_holding_operation<bool>
733 {
734 public:
735
736 using tuple_holding_operation::tuple_holding_operation;
737
738 value *evaluate (struct type *expect_type,
739 struct expression *exp,
740 enum noside noside) override
741 {
742 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
743 return value_from_longest (type, std::get<0> (m_storage));
744 }
745
746 enum exp_opcode opcode () const override
747 { return OP_BOOL; }
748
749 bool constant_p () const override
750 { return true; }
751 };
752
753 class internalvar_operation
754 : public tuple_holding_operation<internalvar *>
755 {
756 public:
757
758 using tuple_holding_operation::tuple_holding_operation;
759
760 value *evaluate (struct type *expect_type,
761 struct expression *exp,
762 enum noside noside) override
763 {
764 return value_of_internalvar (exp->gdbarch,
765 std::get<0> (m_storage));
766 }
767
768 internalvar *get_internalvar () const
769 {
770 return std::get<0> (m_storage);
771 }
772
773 enum exp_opcode opcode () const override
774 { return OP_INTERNALVAR; }
775
776 protected:
777
778 void do_generate_ax (struct expression *exp,
779 struct agent_expr *ax,
780 struct axs_value *value,
781 struct type *cast_type)
782 override;
783 };
784
785 class string_operation
786 : public tuple_holding_operation<std::string>
787 {
788 public:
789
790 using tuple_holding_operation::tuple_holding_operation;
791
792 value *evaluate (struct type *expect_type,
793 struct expression *exp,
794 enum noside noside) override
795 {
796 const std::string &str = std::get<0> (m_storage);
797 return eval_op_string (expect_type, exp, noside,
798 str.size (), str.c_str ());
799 }
800
801 enum exp_opcode opcode () const override
802 { return OP_STRING; }
803 };
804
805 class ternop_slice_operation
806 : public maybe_constant_operation<operation_up, operation_up, operation_up>
807 {
808 public:
809
810 using maybe_constant_operation::maybe_constant_operation;
811
812 value *evaluate (struct type *expect_type,
813 struct expression *exp,
814 enum noside noside) override
815 {
816 struct value *array
817 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
818 struct value *low
819 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
820 struct value *upper
821 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
822 return eval_op_ternop (expect_type, exp, noside, array, low, upper);
823 }
824
825 enum exp_opcode opcode () const override
826 { return TERNOP_SLICE; }
827 };
828
829 class ternop_cond_operation
830 : public maybe_constant_operation<operation_up, operation_up, operation_up>
831 {
832 public:
833
834 using maybe_constant_operation::maybe_constant_operation;
835
836 value *evaluate (struct type *expect_type,
837 struct expression *exp,
838 enum noside noside) override
839 {
840 struct value *val
841 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
842
843 if (value_logical_not (val))
844 return std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
845 return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
846 }
847
848 enum exp_opcode opcode () const override
849 { return TERNOP_COND; }
850
851 protected:
852
853 void do_generate_ax (struct expression *exp,
854 struct agent_expr *ax,
855 struct axs_value *value,
856 struct type *cast_type)
857 override;
858 };
859
860 class complex_operation
861 : public maybe_constant_operation<operation_up, operation_up, struct type *>
862 {
863 public:
864
865 using maybe_constant_operation::maybe_constant_operation;
866
867 value *evaluate (struct type *expect_type,
868 struct expression *exp,
869 enum noside noside) override
870 {
871 value *real = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
872 value *imag = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
873 return value_literal_complex (real, imag,
874 std::get<2> (m_storage));
875 }
876
877 enum exp_opcode opcode () const override
878 { return OP_COMPLEX; }
879 };
880
881 class structop_base_operation
882 : public tuple_holding_operation<operation_up, std::string>
883 {
884 public:
885
886 /* Used for completion. Return the field name. */
887 const std::string &get_string () const
888 {
889 return std::get<1> (m_storage);
890 }
891
892 /* Used for completion. Evaluate the LHS for type. */
893 value *evaluate_lhs (struct expression *exp)
894 {
895 return std::get<0> (m_storage)->evaluate (nullptr, exp,
896 EVAL_AVOID_SIDE_EFFECTS);
897 }
898
899 protected:
900
901 using tuple_holding_operation::tuple_holding_operation;
902 };
903
904 class structop_operation
905 : public structop_base_operation
906 {
907 public:
908
909 using structop_base_operation::structop_base_operation;
910
911 value *evaluate (struct type *expect_type,
912 struct expression *exp,
913 enum noside noside) override
914 {
915 value *val =std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
916 return eval_op_structop_struct (expect_type, exp, noside, val,
917 std::get<1> (m_storage).c_str ());
918 }
919
920 enum exp_opcode opcode () const override
921 { return STRUCTOP_STRUCT; }
922
923 protected:
924
925 void do_generate_ax (struct expression *exp,
926 struct agent_expr *ax,
927 struct axs_value *value,
928 struct type *cast_type)
929 override
930 {
931 gen_expr_structop (exp, STRUCTOP_STRUCT,
932 std::get<0> (this->m_storage).get (),
933 std::get<1> (this->m_storage).c_str (),
934 ax, value);
935 }
936 };
937
938 class structop_ptr_operation
939 : public structop_base_operation
940 {
941 public:
942
943 using structop_base_operation::structop_base_operation;
944
945 value *evaluate (struct type *expect_type,
946 struct expression *exp,
947 enum noside noside) override
948 {
949 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
950 return eval_op_structop_ptr (expect_type, exp, noside, val,
951 std::get<1> (m_storage).c_str ());
952 }
953
954 enum exp_opcode opcode () const override
955 { return STRUCTOP_PTR; }
956
957 protected:
958
959 void do_generate_ax (struct expression *exp,
960 struct agent_expr *ax,
961 struct axs_value *value,
962 struct type *cast_type)
963 override
964 {
965 gen_expr_structop (exp, STRUCTOP_PTR,
966 std::get<0> (this->m_storage).get (),
967 std::get<1> (this->m_storage).c_str (),
968 ax, value);
969 }
970 };
971
972 class structop_member_operation
973 : public tuple_holding_operation<operation_up, operation_up>
974 {
975 public:
976
977 using tuple_holding_operation::tuple_holding_operation;
978
979 value *evaluate (struct type *expect_type,
980 struct expression *exp,
981 enum noside noside) override
982 {
983 value *lhs
984 = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
985 value *rhs
986 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
987 return eval_op_member (expect_type, exp, noside, lhs, rhs);
988 }
989
990 enum exp_opcode opcode () const override
991 { return STRUCTOP_MEMBER; }
992 };
993
994 class structop_mptr_operation
995 : public tuple_holding_operation<operation_up, operation_up>
996 {
997 public:
998
999 using tuple_holding_operation::tuple_holding_operation;
1000
1001 value *evaluate (struct type *expect_type,
1002 struct expression *exp,
1003 enum noside noside) override
1004 {
1005 value *lhs
1006 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1007 value *rhs
1008 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1009 return eval_op_member (expect_type, exp, noside, lhs, rhs);
1010 }
1011
1012 enum exp_opcode opcode () const override
1013 { return STRUCTOP_MPTR; }
1014 };
1015
1016 class concat_operation
1017 : public maybe_constant_operation<operation_up, operation_up>
1018 {
1019 public:
1020
1021 using maybe_constant_operation::maybe_constant_operation;
1022
1023 value *evaluate (struct type *expect_type,
1024 struct expression *exp,
1025 enum noside noside) override
1026 {
1027 value *lhs
1028 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1029 value *rhs
1030 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1031 return eval_op_concat (expect_type, exp, noside, lhs, rhs);
1032 }
1033
1034 enum exp_opcode opcode () const override
1035 { return BINOP_CONCAT; }
1036 };
1037
1038 class add_operation
1039 : public maybe_constant_operation<operation_up, operation_up>
1040 {
1041 public:
1042
1043 using maybe_constant_operation::maybe_constant_operation;
1044
1045 value *evaluate (struct type *expect_type,
1046 struct expression *exp,
1047 enum noside noside) override
1048 {
1049 value *lhs
1050 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1051 value *rhs
1052 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1053 return eval_op_add (expect_type, exp, noside, lhs, rhs);
1054 }
1055
1056 enum exp_opcode opcode () const override
1057 { return BINOP_ADD; }
1058
1059 protected:
1060
1061 void do_generate_ax (struct expression *exp,
1062 struct agent_expr *ax,
1063 struct axs_value *value,
1064 struct type *cast_type)
1065 override
1066 {
1067 gen_expr_binop (exp, BINOP_ADD,
1068 std::get<0> (this->m_storage).get (),
1069 std::get<1> (this->m_storage).get (),
1070 ax, value);
1071 }
1072 };
1073
1074 class sub_operation
1075 : public maybe_constant_operation<operation_up, operation_up>
1076 {
1077 public:
1078
1079 using maybe_constant_operation::maybe_constant_operation;
1080
1081 value *evaluate (struct type *expect_type,
1082 struct expression *exp,
1083 enum noside noside) override
1084 {
1085 value *lhs
1086 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
1087 value *rhs
1088 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
1089 return eval_op_sub (expect_type, exp, noside, lhs, rhs);
1090 }
1091
1092 enum exp_opcode opcode () const override
1093 { return BINOP_SUB; }
1094
1095 protected:
1096
1097 void do_generate_ax (struct expression *exp,
1098 struct agent_expr *ax,
1099 struct axs_value *value,
1100 struct type *cast_type)
1101 override
1102 {
1103 gen_expr_binop (exp, BINOP_SUB,
1104 std::get<0> (this->m_storage).get (),
1105 std::get<1> (this->m_storage).get (),
1106 ax, value);
1107 }
1108 };
1109
1110 typedef struct value *binary_ftype (struct type *expect_type,
1111 struct expression *exp,
1112 enum noside noside, enum exp_opcode op,
1113 struct value *arg1, struct value *arg2);
1114
1115 template<enum exp_opcode OP, binary_ftype FUNC>
1116 class binop_operation
1117 : public maybe_constant_operation<operation_up, operation_up>
1118 {
1119 public:
1120
1121 using maybe_constant_operation::maybe_constant_operation;
1122
1123 value *evaluate (struct type *expect_type,
1124 struct expression *exp,
1125 enum noside noside) override
1126 {
1127 value *lhs
1128 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1129 value *rhs
1130 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1131 return FUNC (expect_type, exp, noside, OP, lhs, rhs);
1132 }
1133
1134 enum exp_opcode opcode () const override
1135 { return OP; }
1136 };
1137
1138 template<enum exp_opcode OP, binary_ftype FUNC>
1139 class usual_ax_binop_operation
1140 : public binop_operation<OP, FUNC>
1141 {
1142 public:
1143
1144 using binop_operation<OP, FUNC>::binop_operation;
1145
1146 protected:
1147
1148 void do_generate_ax (struct expression *exp,
1149 struct agent_expr *ax,
1150 struct axs_value *value,
1151 struct type *cast_type)
1152 override
1153 {
1154 gen_expr_binop (exp, OP,
1155 std::get<0> (this->m_storage).get (),
1156 std::get<1> (this->m_storage).get (),
1157 ax, value);
1158 }
1159 };
1160
1161 using exp_operation = binop_operation<BINOP_EXP, eval_op_binary>;
1162 using intdiv_operation = binop_operation<BINOP_INTDIV, eval_op_binary>;
1163 using mod_operation = binop_operation<BINOP_MOD, eval_op_binary>;
1164
1165 using mul_operation = usual_ax_binop_operation<BINOP_MUL, eval_op_binary>;
1166 using div_operation = usual_ax_binop_operation<BINOP_DIV, eval_op_binary>;
1167 using rem_operation = usual_ax_binop_operation<BINOP_REM, eval_op_binary>;
1168 using lsh_operation = usual_ax_binop_operation<BINOP_LSH, eval_op_binary>;
1169 using rsh_operation = usual_ax_binop_operation<BINOP_RSH, eval_op_binary>;
1170 using bitwise_and_operation
1171 = usual_ax_binop_operation<BINOP_BITWISE_AND, eval_op_binary>;
1172 using bitwise_ior_operation
1173 = usual_ax_binop_operation<BINOP_BITWISE_IOR, eval_op_binary>;
1174 using bitwise_xor_operation
1175 = usual_ax_binop_operation<BINOP_BITWISE_XOR, eval_op_binary>;
1176
1177 class subscript_operation
1178 : public usual_ax_binop_operation<BINOP_SUBSCRIPT, eval_op_subscript>
1179 {
1180 public:
1181 using usual_ax_binop_operation<BINOP_SUBSCRIPT,
1182 eval_op_subscript>::usual_ax_binop_operation;
1183
1184 value *evaluate_for_sizeof (struct expression *exp,
1185 enum noside noside) override;
1186 };
1187
1188 /* Implementation of comparison operations. */
1189 template<enum exp_opcode OP, binary_ftype FUNC>
1190 class comparison_operation
1191 : public usual_ax_binop_operation<OP, FUNC>
1192 {
1193 public:
1194
1195 using usual_ax_binop_operation<OP, FUNC>::usual_ax_binop_operation;
1196
1197 value *evaluate (struct type *expect_type,
1198 struct expression *exp,
1199 enum noside noside) override
1200 {
1201 value *lhs
1202 = std::get<0> (this->m_storage)->evaluate (nullptr, exp, noside);
1203 value *rhs
1204 = std::get<1> (this->m_storage)->evaluate (value_type (lhs), exp,
1205 noside);
1206 return FUNC (expect_type, exp, noside, OP, lhs, rhs);
1207 }
1208 };
1209
1210 using equal_operation = comparison_operation<BINOP_EQUAL, eval_op_equal>;
1211 using notequal_operation
1212 = comparison_operation<BINOP_NOTEQUAL, eval_op_notequal>;
1213 using less_operation = comparison_operation<BINOP_LESS, eval_op_less>;
1214 using gtr_operation = comparison_operation<BINOP_GTR, eval_op_gtr>;
1215 using geq_operation = comparison_operation<BINOP_GEQ, eval_op_geq>;
1216 using leq_operation = comparison_operation<BINOP_LEQ, eval_op_leq>;
1217
1218 /* Implement the GDB '@' repeat operator. */
1219 class repeat_operation
1220 : public binop_operation<BINOP_REPEAT, eval_op_repeat>
1221 {
1222 using binop_operation<BINOP_REPEAT, eval_op_repeat>::binop_operation;
1223
1224 protected:
1225
1226 void do_generate_ax (struct expression *exp,
1227 struct agent_expr *ax,
1228 struct axs_value *value,
1229 struct type *cast_type)
1230 override;
1231 };
1232
1233 /* C-style comma operator. */
1234 class comma_operation
1235 : public maybe_constant_operation<operation_up, operation_up>
1236 {
1237 public:
1238
1239 using maybe_constant_operation::maybe_constant_operation;
1240
1241 value *evaluate (struct type *expect_type,
1242 struct expression *exp,
1243 enum noside noside) override
1244 {
1245 /* The left-hand-side is only evaluated for side effects, so don't
1246 bother in other modes. */
1247 if (noside == EVAL_NORMAL)
1248 std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1249 return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1250 }
1251
1252 enum exp_opcode opcode () const override
1253 { return BINOP_COMMA; }
1254
1255 protected:
1256
1257 void do_generate_ax (struct expression *exp,
1258 struct agent_expr *ax,
1259 struct axs_value *value,
1260 struct type *cast_type)
1261 override;
1262 };
1263
1264 typedef struct value *unary_ftype (struct type *expect_type,
1265 struct expression *exp,
1266 enum noside noside, enum exp_opcode op,
1267 struct value *arg1);
1268
1269 /* Base class for unary operations. */
1270 template<enum exp_opcode OP, unary_ftype FUNC>
1271 class unop_operation
1272 : public maybe_constant_operation<operation_up>
1273 {
1274 public:
1275
1276 using maybe_constant_operation::maybe_constant_operation;
1277
1278 value *evaluate (struct type *expect_type,
1279 struct expression *exp,
1280 enum noside noside) override
1281 {
1282 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1283 return FUNC (expect_type, exp, noside, OP, val);
1284 }
1285
1286 enum exp_opcode opcode () const override
1287 { return OP; }
1288 };
1289
1290 /* Unary operations that can also be turned into agent expressions in
1291 the "usual" way. */
1292 template<enum exp_opcode OP, unary_ftype FUNC>
1293 class usual_ax_unop_operation
1294 : public unop_operation<OP, FUNC>
1295 {
1296 using unop_operation<OP, FUNC>::unop_operation;
1297
1298 protected:
1299
1300 void do_generate_ax (struct expression *exp,
1301 struct agent_expr *ax,
1302 struct axs_value *value,
1303 struct type *cast_type)
1304 override
1305 {
1306 gen_expr_unop (exp, OP,
1307 std::get<0> (this->m_storage).get (),
1308 ax, value);
1309 }
1310 };
1311
1312 using unary_plus_operation = usual_ax_unop_operation<UNOP_PLUS, eval_op_plus>;
1313 using unary_neg_operation = usual_ax_unop_operation<UNOP_NEG, eval_op_neg>;
1314 using unary_complement_operation
1315 = usual_ax_unop_operation<UNOP_COMPLEMENT, eval_op_complement>;
1316 using unary_logical_not_operation
1317 = usual_ax_unop_operation<UNOP_LOGICAL_NOT, eval_op_lognot>;
1318
1319 } /* namespace expr */
1320
1321 #endif /* EXPOP_H */
This page took 0.055843 seconds and 4 git commands to generate.