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