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