]> Git Repo - binutils.git/blob - gdb/expop.h
9a9c6bc2b199e0cf0ac2ee3ad6dd758e34c3b69b
[binutils.git] / gdb / expop.h
1 /* Definitions for expressions in GDB
2
3    Copyright (C) 2020 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifndef EXPOP_H
21 #define EXPOP_H
22
23 #include "block.h"
24 #include "c-lang.h"
25 #include "cp-abi.h"
26 #include "expression.h"
27 #include "objfiles.h"
28 #include "gdbsupport/traits.h"
29 #include "gdbsupport/enum-flags.h"
30
31 struct agent_expr;
32 struct axs_value;
33
34 extern void gen_expr_binop (struct expression *exp,
35                             enum exp_opcode op,
36                             expr::operation *lhs, expr::operation *rhs,
37                             struct agent_expr *ax, struct axs_value *value);
38 extern void gen_expr_structop (struct expression *exp,
39                                enum exp_opcode op,
40                                expr::operation *lhs,
41                                const char *name,
42                                struct agent_expr *ax, struct axs_value *value);
43 extern void gen_expr_unop (struct expression *exp,
44                            enum exp_opcode op,
45                            expr::operation *lhs,
46                            struct agent_expr *ax, struct axs_value *value);
47
48 extern struct value *eval_op_scope (struct type *expect_type,
49                                     struct expression *exp,
50                                     enum noside noside,
51                                     struct type *type, const char *string);
52 extern struct value *eval_op_var_msym_value (struct type *expect_type,
53                                              struct expression *exp,
54                                              enum noside noside,
55                                              bool outermost_p,
56                                              minimal_symbol *msymbol,
57                                              struct objfile *objfile);
58 extern struct value *eval_op_var_entry_value (struct type *expect_type,
59                                               struct expression *exp,
60                                               enum noside noside, symbol *sym);
61 extern struct value *eval_op_func_static_var (struct type *expect_type,
62                                               struct expression *exp,
63                                               enum noside noside,
64                                               value *func, const char *var);
65 extern struct value *eval_op_register (struct type *expect_type,
66                                        struct expression *exp,
67                                        enum noside noside, const char *name);
68 extern struct value *eval_op_string (struct type *expect_type,
69                                      struct expression *exp,
70                                      enum noside noside, int len,
71                                      const char *string);
72 extern struct value *eval_op_ternop (struct type *expect_type,
73                                      struct expression *exp,
74                                      enum noside noside,
75                                      struct value *array, struct value *low,
76                                      struct value *upper);
77 extern struct value *eval_op_structop_struct (struct type *expect_type,
78                                               struct expression *exp,
79                                               enum noside noside,
80                                               struct value *arg1,
81                                               const char *string);
82 extern struct value *eval_op_structop_ptr (struct type *expect_type,
83                                            struct expression *exp,
84                                            enum noside noside,
85                                            struct value *arg1,
86                                            const char *string);
87 extern struct value *eval_op_member (struct type *expect_type,
88                                      struct expression *exp,
89                                      enum noside noside,
90                                      struct value *arg1, struct value *arg2);
91 extern struct value *eval_op_concat (struct type *expect_type,
92                                      struct expression *exp,
93                                      enum noside noside,
94                                      struct value *arg1, struct value *arg2);
95 extern struct value *eval_op_add (struct type *expect_type,
96                                   struct expression *exp,
97                                   enum noside noside,
98                                   struct value *arg1, struct value *arg2);
99 extern struct value *eval_op_sub (struct type *expect_type,
100                                   struct expression *exp,
101                                   enum noside noside,
102                                   struct value *arg1, struct value *arg2);
103 extern struct value *eval_op_binary (struct type *expect_type,
104                                      struct expression *exp,
105                                      enum noside noside, enum exp_opcode op,
106                                      struct value *arg1, struct value *arg2);
107 extern struct value *eval_op_subscript (struct type *expect_type,
108                                         struct expression *exp,
109                                         enum noside noside, enum exp_opcode op,
110                                         struct value *arg1,
111                                         struct value *arg2);
112 extern struct value *eval_op_equal (struct type *expect_type,
113                                     struct expression *exp,
114                                     enum noside noside, enum exp_opcode op,
115                                     struct value *arg1,
116                                     struct value *arg2);
117 extern struct value *eval_op_notequal (struct type *expect_type,
118                                        struct expression *exp,
119                                        enum noside noside, enum exp_opcode op,
120                                        struct value *arg1,
121                                        struct value *arg2);
122 extern struct value *eval_op_less (struct type *expect_type,
123                                    struct expression *exp,
124                                    enum noside noside, enum exp_opcode op,
125                                    struct value *arg1,
126                                    struct value *arg2);
127 extern struct value *eval_op_gtr (struct type *expect_type,
128                                   struct expression *exp,
129                                   enum noside noside, enum exp_opcode op,
130                                   struct value *arg1,
131                                   struct value *arg2);
132 extern struct value *eval_op_geq (struct type *expect_type,
133                                   struct expression *exp,
134                                   enum noside noside, enum exp_opcode op,
135                                   struct value *arg1,
136                                   struct value *arg2);
137 extern struct value *eval_op_leq (struct type *expect_type,
138                                   struct expression *exp,
139                                   enum noside noside, enum exp_opcode op,
140                                   struct value *arg1,
141                                   struct value *arg2);
142 extern struct value *eval_op_repeat (struct type *expect_type,
143                                      struct expression *exp,
144                                      enum noside noside, enum exp_opcode op,
145                                      struct value *arg1,
146                                      struct value *arg2);
147 extern struct value *eval_op_plus (struct type *expect_type,
148                                    struct expression *exp,
149                                    enum noside noside, enum exp_opcode op,
150                                    struct value *arg1);
151 extern struct value *eval_op_neg (struct type *expect_type,
152                                   struct expression *exp,
153                                   enum noside noside, enum exp_opcode op,
154                                   struct value *arg1);
155 extern struct value *eval_op_complement (struct type *expect_type,
156                                          struct expression *exp,
157                                          enum noside noside,
158                                          enum exp_opcode op,
159                                          struct value *arg1);
160 extern struct value *eval_op_lognot (struct type *expect_type,
161                                      struct expression *exp,
162                                      enum noside noside,
163                                      enum exp_opcode op,
164                                      struct value *arg1);
165 extern struct value *eval_op_preinc (struct type *expect_type,
166                                      struct expression *exp,
167                                      enum noside noside,
168                                      enum exp_opcode op,
169                                      struct value *arg1);
170 extern struct value *eval_op_predec (struct type *expect_type,
171                                      struct expression *exp,
172                                      enum noside noside,
173                                      enum exp_opcode op,
174                                      struct value *arg1);
175 extern struct value *eval_op_postinc (struct type *expect_type,
176                                       struct expression *exp,
177                                       enum noside noside,
178                                       enum exp_opcode op,
179                                       struct value *arg1);
180 extern struct value *eval_op_postdec (struct type *expect_type,
181                                       struct expression *exp,
182                                       enum noside noside,
183                                       enum exp_opcode op,
184                                       struct value *arg1);
185 extern struct value *eval_op_ind (struct type *expect_type,
186                                   struct expression *exp,
187                                   enum noside noside,
188                                   struct value *arg1);
189 extern struct value *eval_op_type (struct type *expect_type,
190                                    struct expression *exp,
191                                    enum noside noside, struct type *type);
192 extern struct value *eval_op_alignof (struct type *expect_type,
193                                       struct expression *exp,
194                                       enum noside noside,
195                                       struct value *arg1);
196 extern struct value *eval_op_memval (struct type *expect_type,
197                                      struct expression *exp,
198                                      enum noside noside,
199                                      struct value *arg1, struct type *type);
200 extern struct value *eval_binop_assign_modify (struct type *expect_type,
201                                                struct expression *exp,
202                                                enum noside noside,
203                                                enum exp_opcode op,
204                                                struct value *arg1,
205                                                struct value *arg2);
206
207 namespace expr
208 {
209
210 class ada_component;
211
212 /* The check_objfile overloads are used to check whether a particular
213    component of some operation references an objfile.  The passed-in
214    objfile will never be a debug objfile.  */
215
216 /* See if EXP_OBJFILE matches OBJFILE.  */
217 static inline bool
218 check_objfile (struct objfile *exp_objfile, struct objfile *objfile)
219 {
220   if (exp_objfile->separate_debug_objfile_backlink)
221     exp_objfile = exp_objfile->separate_debug_objfile_backlink;
222   return exp_objfile == objfile;
223 }
224
225 static inline bool 
226 check_objfile (struct type *type, struct objfile *objfile)
227 {
228   struct objfile *ty_objfile = type->objfile_owner ();
229   if (ty_objfile != nullptr)
230     return check_objfile (ty_objfile, objfile);
231   return false;
232 }
233
234 static inline bool 
235 check_objfile (struct symbol *sym, struct objfile *objfile)
236 {
237   return check_objfile (symbol_objfile (sym), objfile);
238 }
239
240 static inline bool 
241 check_objfile (const struct block *block, struct objfile *objfile)
242 {
243   return check_objfile (block_objfile (block), objfile);
244 }
245
246 static inline bool
247 check_objfile (minimal_symbol *minsym, struct objfile *objfile)
248 {
249   /* This may seem strange but minsyms are only used with an objfile
250      as well.  */
251   return false;
252 }
253
254 static inline bool
255 check_objfile (internalvar *ivar, struct objfile *objfile)
256 {
257   return false;
258 }
259
260 static inline bool
261 check_objfile (const std::string &str, struct objfile *objfile)
262 {
263   return false;
264 }
265
266 static inline bool 
267 check_objfile (const operation_up &op, struct objfile *objfile)
268 {
269   return op->uses_objfile (objfile);
270 }
271
272 static inline bool
273 check_objfile (enum exp_opcode val, struct objfile *objfile)
274 {
275   return false;
276 }
277
278 static inline bool
279 check_objfile (ULONGEST val, struct objfile *objfile)
280 {
281   return false;
282 }
283
284 template<typename T>
285 static inline bool
286 check_objfile (enum_flags<T> val, struct objfile *objfile)
287 {
288   return false;
289 }
290
291 template<typename T>
292 static inline bool 
293 check_objfile (const std::vector<T> &collection, struct objfile *objfile)
294 {
295   for (const auto &item : collection)
296     {
297       if (check_objfile (item, objfile))
298         return true;
299     }
300   return false;
301 }
302
303 template<typename S, typename T>
304 static inline bool 
305 check_objfile (const std::pair<S, T> &item, struct objfile *objfile)
306 {
307   return (check_objfile (item.first, objfile)
308           || check_objfile (item.second, objfile));
309 }
310
311 extern bool check_objfile (const std::unique_ptr<ada_component> &comp,
312                            struct objfile *objfile);
313
314 static inline void
315 dump_for_expression (struct ui_file *stream, int depth,
316                      const operation_up &op)
317 {
318   op->dump (stream, depth);
319 }
320
321 extern void dump_for_expression (struct ui_file *stream, int depth,
322                                  enum exp_opcode op);
323 extern void dump_for_expression (struct ui_file *stream, int depth,
324                                  const std::string &str);
325 extern void dump_for_expression (struct ui_file *stream, int depth,
326                                  struct type *type);
327 extern void dump_for_expression (struct ui_file *stream, int depth,
328                                  CORE_ADDR addr);
329 extern void dump_for_expression (struct ui_file *stream, int depth,
330                                  internalvar *ivar);
331 extern void dump_for_expression (struct ui_file *stream, int depth,
332                                  symbol *sym);
333 extern void dump_for_expression (struct ui_file *stream, int depth,
334                                  minimal_symbol *msym);
335 extern void dump_for_expression (struct ui_file *stream, int depth,
336                                  const block *bl);
337 extern void dump_for_expression (struct ui_file *stream, int depth,
338                                  type_instance_flags flags);
339 extern void dump_for_expression (struct ui_file *stream, int depth,
340                                  enum c_string_type_values flags);
341 extern void dump_for_expression (struct ui_file *stream, int depth,
342                                  enum range_flag flags);
343 extern void dump_for_expression (struct ui_file *stream, int depth,
344                                  objfile *objf);
345 extern void dump_for_expression (struct ui_file *stream, int depth,
346                                  const std::unique_ptr<ada_component> &comp);
347
348 template<typename T>
349 void
350 dump_for_expression (struct ui_file *stream, int depth,
351                      const std::vector<T> &vals)
352 {
353   fprintf_filtered (stream, _("%*sVector:\n"), depth, "");
354   for (auto &item : vals)
355     dump_for_expression (stream, depth + 1, item);
356 }
357
358 template<typename X, typename Y>
359 void
360 dump_for_expression (struct ui_file *stream, int depth,
361                      const std::pair<X, Y> &vals)
362 {
363   dump_for_expression (stream, depth, vals.first);
364   dump_for_expression (stream, depth, vals.second);
365 }
366
367 /* Base class for most concrete operations.  This class holds data,
368    specified via template parameters, and supplies generic
369    implementations of the 'dump' and 'uses_objfile' methods.  */
370 template<typename... Arg>
371 class tuple_holding_operation : public operation
372 {
373 public:
374
375   explicit tuple_holding_operation (Arg... args)
376     : m_storage (std::forward<Arg> (args)...)
377   {
378   }
379
380   DISABLE_COPY_AND_ASSIGN (tuple_holding_operation);
381
382   bool uses_objfile (struct objfile *objfile) const override
383   {
384     return do_check_objfile<0, Arg...> (objfile, m_storage);
385   }
386
387   void dump (struct ui_file *stream, int depth) const override
388   {
389     dump_for_expression (stream, depth, opcode ());
390     do_dump<0, Arg...> (stream, depth + 1, m_storage);
391   }
392
393 protected:
394
395   /* Storage for the data.  */
396   std::tuple<Arg...> m_storage;
397
398 private:
399
400   /* do_dump does the work of dumping the data.  */
401   template<int I, typename... T>
402   typename std::enable_if<I == sizeof... (T), void>::type
403   do_dump (struct ui_file *stream, int depth, const std::tuple<T...> &value)
404     const
405   {
406   }
407
408   template<int I, typename... T>
409   typename std::enable_if<I < sizeof... (T), void>::type
410   do_dump (struct ui_file *stream, int depth, const std::tuple<T...> &value)
411     const
412   {
413     dump_for_expression (stream, depth, std::get<I> (value));
414     do_dump<I + 1, T...> (stream, depth, value);
415   }
416
417   /* do_check_objfile does the work of checking whether this object
418      refers to OBJFILE.  */
419   template<int I, typename... T>
420   typename std::enable_if<I == sizeof... (T), bool>::type
421   do_check_objfile (struct objfile *objfile, const std::tuple<T...> &value)
422     const
423   {
424     return false;
425   }
426
427   template<int I, typename... T>
428   typename std::enable_if<I < sizeof... (T), bool>::type
429   do_check_objfile (struct objfile *objfile, const std::tuple<T...> &value)
430     const
431   {
432     if (check_objfile (std::get<I> (value), objfile))
433       return true;
434     return do_check_objfile<I + 1, T...> (objfile, value);
435   }
436 };
437
438 /* The check_constant overloads are used to decide whether a given
439    concrete operation is a constant.  This is done by checking the
440    operands.  */
441
442 static inline bool
443 check_constant (const operation_up &item)
444 {
445   return item->constant_p ();
446 }
447
448 static inline bool
449 check_constant (struct minimal_symbol *msym)
450 {
451   return false;
452 }
453
454 static inline bool
455 check_constant (struct type *type)
456 {
457   return true;
458 }
459
460 static inline bool
461 check_constant (const struct block *block)
462 {
463   return true;
464 }
465
466 static inline bool
467 check_constant (const std::string &str)
468 {
469   return true;
470 }
471
472 static inline bool
473 check_constant (struct objfile *objfile)
474 {
475   return true;
476 }
477
478 static inline bool
479 check_constant (ULONGEST cst)
480 {
481   return true;
482 }
483
484 static inline bool
485 check_constant (struct symbol *sym)
486 {
487   enum address_class sc = SYMBOL_CLASS (sym);
488   return (sc == LOC_BLOCK
489           || sc == LOC_CONST
490           || sc == LOC_CONST_BYTES
491           || sc == LOC_LABEL);
492 }
493
494 template<typename T>
495 static inline bool
496 check_constant (const std::vector<T> &collection)
497 {
498   for (const auto &item : collection)
499     if (!check_constant (item))
500       return false;
501   return true;
502 }
503
504 template<typename S, typename T>
505 static inline bool
506 check_constant (const std::pair<S, T> &item)
507 {
508   return check_constant (item.first) && check_constant (item.second);
509 }
510
511 /* Base class for concrete operations.  This class supplies an
512    implementation of 'constant_p' that works by checking the
513    operands.  */
514 template<typename... Arg>
515 class maybe_constant_operation
516   : public tuple_holding_operation<Arg...>
517 {
518 public:
519
520   using tuple_holding_operation<Arg...>::tuple_holding_operation;
521
522   bool constant_p () const override
523   {
524     return do_check_constant<0, Arg...> (this->m_storage);
525   }
526
527 private:
528
529   template<int I, typename... T>
530   typename std::enable_if<I == sizeof... (T), bool>::type
531   do_check_constant (const std::tuple<T...> &value) const
532   {
533     return true;
534   }
535
536   template<int I, typename... T>
537   typename std::enable_if<I < sizeof... (T), bool>::type
538   do_check_constant (const std::tuple<T...> &value) const
539   {
540     if (!check_constant (std::get<I> (value)))
541       return false;
542     return do_check_constant<I + 1, T...> (value);
543   }
544 };
545
546 /* A floating-point constant.  The constant is encoded in the target
547    format.  */
548
549 typedef std::array<gdb_byte, 16> float_data;
550
551 /* An operation that holds a floating-point constant of a given
552    type.
553
554    This does not need the facilities provided by
555    tuple_holding_operation, so it does not use it.  */
556 class float_const_operation
557   : public operation
558 {
559 public:
560
561   float_const_operation (struct type *type, float_data data)
562     : m_type (type),
563       m_data (data)
564   {
565   }
566
567   value *evaluate (struct type *expect_type,
568                    struct expression *exp,
569                    enum noside noside) override
570   {
571     return value_from_contents (m_type, m_data.data ());
572   }
573
574   enum exp_opcode opcode () const override
575   { return OP_FLOAT; }
576
577   bool constant_p () const override
578   { return true; }
579
580   void dump (struct ui_file *stream, int depth) const override;
581
582 private:
583
584   struct type *m_type;
585   float_data m_data;
586 };
587
588 class scope_operation
589   : public maybe_constant_operation<struct type *, std::string>
590 {
591 public:
592
593   using maybe_constant_operation::maybe_constant_operation;
594
595   value *evaluate (struct type *expect_type,
596                    struct expression *exp,
597                    enum noside noside) override
598   {
599     return eval_op_scope (expect_type, exp, noside,
600                           std::get<0> (m_storage),
601                           std::get<1> (m_storage).c_str ());
602   }
603
604   value *evaluate_for_address (struct expression *exp,
605                                enum noside noside) override;
606
607   value *evaluate_funcall (struct type *expect_type,
608                            struct expression *exp,
609                            enum noside noside,
610                            const std::vector<operation_up> &args) override;
611
612   enum exp_opcode opcode () const override
613   { return OP_SCOPE; }
614
615 protected:
616
617   void do_generate_ax (struct expression *exp,
618                        struct agent_expr *ax,
619                        struct axs_value *value,
620                        struct type *cast_type)
621     override;
622 };
623
624 /* Compute the value of a variable.  */
625 class var_value_operation
626   : public maybe_constant_operation<symbol *, const block *>
627 {
628 public:
629
630   using maybe_constant_operation::maybe_constant_operation;
631
632   value *evaluate (struct type *expect_type,
633                    struct expression *exp,
634                    enum noside noside) override;
635
636   value *evaluate_with_coercion (struct expression *exp,
637                                  enum noside noside) override;
638
639   value *evaluate_for_sizeof (struct expression *exp, enum noside noside)
640     override;
641
642   value *evaluate_for_cast (struct type *expect_type,
643                             struct expression *exp,
644                             enum noside noside) override;
645
646   value *evaluate_for_address (struct expression *exp, enum noside noside)
647     override;
648
649   value *evaluate_funcall (struct type *expect_type,
650                            struct expression *exp,
651                            enum noside noside,
652                            const std::vector<operation_up> &args) override;
653
654   enum exp_opcode opcode () const override
655   { return OP_VAR_VALUE; }
656
657 protected:
658
659   void do_generate_ax (struct expression *exp,
660                        struct agent_expr *ax,
661                        struct axs_value *value,
662                        struct type *cast_type)
663     override;
664 };
665
666 class long_const_operation
667   : public tuple_holding_operation<struct type *, LONGEST>
668 {
669 public:
670
671   using tuple_holding_operation::tuple_holding_operation;
672
673   value *evaluate (struct type *expect_type,
674                    struct expression *exp,
675                    enum noside noside) override
676   {
677     return value_from_longest (std::get<0> (m_storage),
678                                std::get<1> (m_storage));
679   }
680
681   enum exp_opcode opcode () const override
682   { return OP_LONG; }
683
684   bool constant_p () const override
685   { return true; }
686
687 protected:
688
689   void do_generate_ax (struct expression *exp,
690                        struct agent_expr *ax,
691                        struct axs_value *value,
692                        struct type *cast_type)
693     override;
694 };
695
696 class var_msym_value_operation
697   : public maybe_constant_operation<minimal_symbol *, struct objfile *>
698 {
699 public:
700
701   using maybe_constant_operation::maybe_constant_operation;
702
703   value *evaluate (struct type *expect_type,
704                    struct expression *exp,
705                    enum noside noside) override
706   {
707     return eval_op_var_msym_value (expect_type, exp, noside, m_outermost,
708                                    std::get<0> (m_storage),
709                                    std::get<1> (m_storage));
710   }
711
712   value *evaluate_for_sizeof (struct expression *exp, enum noside noside)
713     override;
714
715   value *evaluate_for_address (struct expression *exp, enum noside noside)
716     override;
717
718   value *evaluate_for_cast (struct type *expect_type,
719                             struct expression *exp,
720                             enum noside noside) override;
721
722   value *evaluate_funcall (struct type *expect_type,
723                            struct expression *exp,
724                            enum noside noside,
725                            const std::vector<operation_up> &args) override
726   {
727     const char *name = std::get<0> (m_storage)->print_name ();
728     return operation::evaluate_funcall (expect_type, exp, noside, name, args);
729   }
730
731   enum exp_opcode opcode () const override
732   { return OP_VAR_MSYM_VALUE; }
733
734   void set_outermost () override
735   {
736     m_outermost = true;
737   }
738
739 protected:
740
741   /* True if this is the outermost operation in the expression.  */
742   bool m_outermost = false;
743
744   void do_generate_ax (struct expression *exp,
745                        struct agent_expr *ax,
746                        struct axs_value *value,
747                        struct type *cast_type)
748     override;
749 };
750
751 class var_entry_value_operation
752   : public tuple_holding_operation<symbol *>
753 {
754 public:
755
756   using tuple_holding_operation::tuple_holding_operation;
757
758   value *evaluate (struct type *expect_type,
759                    struct expression *exp,
760                    enum noside noside) override
761   {
762     return eval_op_var_entry_value (expect_type, exp, noside,
763                                     std::get<0> (m_storage));
764   }
765
766   enum exp_opcode opcode () const override
767   { return OP_VAR_ENTRY_VALUE; }
768 };
769
770 class func_static_var_operation
771   : public maybe_constant_operation<operation_up, std::string>
772 {
773 public:
774
775   using maybe_constant_operation::maybe_constant_operation;
776
777   value *evaluate (struct type *expect_type,
778                    struct expression *exp,
779                    enum noside noside) override
780   {
781     value *func = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
782     return eval_op_func_static_var (expect_type, exp, noside, func,
783                                     std::get<1> (m_storage).c_str ());
784   }
785
786   enum exp_opcode opcode () const override
787   { return OP_FUNC_STATIC_VAR; }
788 };
789
790 class last_operation
791   : public tuple_holding_operation<int>
792 {
793 public:
794
795   using tuple_holding_operation::tuple_holding_operation;
796
797   value *evaluate (struct type *expect_type,
798                    struct expression *exp,
799                    enum noside noside) override
800   {
801     return access_value_history (std::get<0> (m_storage));
802   }
803
804   enum exp_opcode opcode () const override
805   { return OP_LAST; }
806 };
807
808 class register_operation
809   : public tuple_holding_operation<std::string>
810 {
811 public:
812
813   using tuple_holding_operation::tuple_holding_operation;
814
815   value *evaluate (struct type *expect_type,
816                    struct expression *exp,
817                    enum noside noside) override
818   {
819     return eval_op_register (expect_type, exp, noside,
820                              std::get<0> (m_storage).c_str ());
821   }
822
823   enum exp_opcode opcode () const override
824   { return OP_REGISTER; }
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 using equal_operation = comparison_operation<BINOP_EQUAL, eval_op_equal>;
1333 using notequal_operation
1334      = comparison_operation<BINOP_NOTEQUAL, eval_op_notequal>;
1335 using less_operation = comparison_operation<BINOP_LESS, eval_op_less>;
1336 using gtr_operation = comparison_operation<BINOP_GTR, eval_op_gtr>;
1337 using geq_operation = comparison_operation<BINOP_GEQ, eval_op_geq>;
1338 using leq_operation = comparison_operation<BINOP_LEQ, eval_op_leq>;
1339
1340 /* Implement the GDB '@' repeat operator.  */
1341 class repeat_operation
1342   : public binop_operation<BINOP_REPEAT, eval_op_repeat>
1343 {
1344   using binop_operation<BINOP_REPEAT, eval_op_repeat>::binop_operation;
1345
1346 protected:
1347
1348   void do_generate_ax (struct expression *exp,
1349                        struct agent_expr *ax,
1350                        struct axs_value *value,
1351                        struct type *cast_type)
1352     override;
1353 };
1354
1355 /* C-style comma operator.  */
1356 class comma_operation
1357   : public maybe_constant_operation<operation_up, operation_up>
1358 {
1359 public:
1360
1361   using maybe_constant_operation::maybe_constant_operation;
1362
1363   value *evaluate (struct type *expect_type,
1364                    struct expression *exp,
1365                    enum noside noside) override
1366   {
1367     /* The left-hand-side is only evaluated for side effects, so don't
1368        bother in other modes.  */
1369     if (noside == EVAL_NORMAL)
1370       std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1371     return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1372   }
1373
1374   enum exp_opcode opcode () const override
1375   { return BINOP_COMMA; }
1376
1377 protected:
1378
1379   void do_generate_ax (struct expression *exp,
1380                        struct agent_expr *ax,
1381                        struct axs_value *value,
1382                        struct type *cast_type)
1383     override;
1384 };
1385
1386 typedef struct value *unary_ftype (struct type *expect_type,
1387                                    struct expression *exp,
1388                                    enum noside noside, enum exp_opcode op,
1389                                    struct value *arg1);
1390
1391 /* Base class for unary operations.  */
1392 template<enum exp_opcode OP, unary_ftype FUNC>
1393 class unop_operation
1394   : public maybe_constant_operation<operation_up>
1395 {
1396 public:
1397
1398   using maybe_constant_operation::maybe_constant_operation;
1399
1400   value *evaluate (struct type *expect_type,
1401                    struct expression *exp,
1402                    enum noside noside) override
1403   {
1404     value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1405     return FUNC (expect_type, exp, noside, OP, val);
1406   }
1407
1408   enum exp_opcode opcode () const override
1409   { return OP; }
1410 };
1411
1412 /* Unary operations that can also be turned into agent expressions in
1413    the "usual" way.  */
1414 template<enum exp_opcode OP, unary_ftype FUNC>
1415 class usual_ax_unop_operation
1416   : public unop_operation<OP, FUNC>
1417 {
1418   using unop_operation<OP, FUNC>::unop_operation;
1419
1420 protected:
1421
1422   void do_generate_ax (struct expression *exp,
1423                        struct agent_expr *ax,
1424                        struct axs_value *value,
1425                        struct type *cast_type)
1426     override
1427   {
1428     gen_expr_unop (exp, OP,
1429                    std::get<0> (this->m_storage).get (),
1430                    ax, value);
1431   }
1432 };
1433
1434 using unary_plus_operation = usual_ax_unop_operation<UNOP_PLUS, eval_op_plus>;
1435 using unary_neg_operation = usual_ax_unop_operation<UNOP_NEG, eval_op_neg>;
1436 using unary_complement_operation
1437      = usual_ax_unop_operation<UNOP_COMPLEMENT, eval_op_complement>;
1438 using unary_logical_not_operation
1439      = usual_ax_unop_operation<UNOP_LOGICAL_NOT, eval_op_lognot>;
1440
1441 /* Handle pre- and post- increment and -decrement.  */
1442 template<enum exp_opcode OP, unary_ftype FUNC>
1443 class unop_incr_operation
1444   : public tuple_holding_operation<operation_up>
1445 {
1446 public:
1447
1448   using tuple_holding_operation::tuple_holding_operation;
1449
1450   value *evaluate (struct type *expect_type,
1451                    struct expression *exp,
1452                    enum noside noside) override
1453   {
1454     value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1455     return FUNC (expect_type, exp, noside, OP, val);
1456   }
1457
1458   enum exp_opcode opcode () const override
1459   { return OP; }
1460 };
1461
1462 using preinc_operation
1463      = unop_incr_operation<UNOP_PREINCREMENT, eval_op_preinc>;
1464 using predec_operation
1465      = unop_incr_operation<UNOP_PREDECREMENT, eval_op_predec>;
1466 using postinc_operation
1467      = unop_incr_operation<UNOP_POSTINCREMENT, eval_op_postinc>;
1468 using postdec_operation
1469      = unop_incr_operation<UNOP_POSTDECREMENT, eval_op_postdec>;
1470
1471 /* Base class for implementations of UNOP_IND.  */
1472 class unop_ind_base_operation
1473   : public tuple_holding_operation<operation_up>
1474 {
1475 public:
1476
1477   using tuple_holding_operation::tuple_holding_operation;
1478
1479   value *evaluate (struct type *expect_type,
1480                    struct expression *exp,
1481                    enum noside noside) override
1482   {
1483     if (expect_type != nullptr && expect_type->code () == TYPE_CODE_PTR)
1484       expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
1485     value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1486     return eval_op_ind (expect_type, exp, noside, val);
1487   }
1488
1489   value *evaluate_for_address (struct expression *exp,
1490                                enum noside noside) override;
1491
1492   value *evaluate_for_sizeof (struct expression *exp,
1493                               enum noside noside) override;
1494
1495   enum exp_opcode opcode () const override
1496   { return UNOP_IND; }
1497 };
1498
1499 /* Ordinary UNOP_IND implementation.  */
1500 class unop_ind_operation
1501   : public unop_ind_base_operation
1502 {
1503 public:
1504
1505   using unop_ind_base_operation::unop_ind_base_operation;
1506
1507 protected:
1508
1509   void do_generate_ax (struct expression *exp,
1510                        struct agent_expr *ax,
1511                        struct axs_value *value,
1512                        struct type *cast_type)
1513     override
1514   {
1515     gen_expr_unop (exp, UNOP_IND,
1516                    std::get<0> (this->m_storage).get (),
1517                    ax, value);
1518   }
1519 };
1520
1521 /* Implement OP_TYPE.  */
1522 class type_operation
1523   : public tuple_holding_operation<struct type *>
1524 {
1525 public:
1526
1527   using tuple_holding_operation::tuple_holding_operation;
1528
1529   value *evaluate (struct type *expect_type,
1530                    struct expression *exp,
1531                    enum noside noside) override
1532   {
1533     return eval_op_type (expect_type, exp, noside, std::get<0> (m_storage));
1534   }
1535
1536   enum exp_opcode opcode () const override
1537   { return OP_TYPE; }
1538
1539   bool constant_p () const override
1540   { return true; }
1541 };
1542
1543 /* Implement the "typeof" operation.  */
1544 class typeof_operation
1545   : public maybe_constant_operation<operation_up>
1546 {
1547 public:
1548
1549   using maybe_constant_operation::maybe_constant_operation;
1550
1551   value *evaluate (struct type *expect_type,
1552                    struct expression *exp,
1553                    enum noside noside) override
1554   {
1555     if (noside == EVAL_SKIP)
1556       return eval_skip_value (exp);
1557     else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1558       return std::get<0> (m_storage)->evaluate (nullptr, exp,
1559                                                 EVAL_AVOID_SIDE_EFFECTS);
1560     else
1561       error (_("Attempt to use a type as an expression"));
1562   }
1563
1564   enum exp_opcode opcode () const override
1565   { return OP_TYPEOF; }
1566 };
1567
1568 /* Implement 'decltype'.  */
1569 class decltype_operation
1570   : public maybe_constant_operation<operation_up>
1571 {
1572 public:
1573
1574   using maybe_constant_operation::maybe_constant_operation;
1575
1576   value *evaluate (struct type *expect_type,
1577                    struct expression *exp,
1578                    enum noside noside) override
1579   {
1580     if (noside == EVAL_SKIP)
1581       return eval_skip_value (exp);
1582     else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1583       {
1584         value *result
1585           = std::get<0> (m_storage)->evaluate (nullptr, exp,
1586                                                EVAL_AVOID_SIDE_EFFECTS);
1587         enum exp_opcode sub_op = std::get<0> (m_storage)->opcode ();
1588         if (sub_op == BINOP_SUBSCRIPT
1589             || sub_op == STRUCTOP_MEMBER
1590             || sub_op == STRUCTOP_MPTR
1591             || sub_op == UNOP_IND
1592             || sub_op == STRUCTOP_STRUCT
1593             || sub_op == STRUCTOP_PTR
1594             || sub_op == OP_SCOPE)
1595           {
1596             struct type *type = value_type (result);
1597
1598             if (!TYPE_IS_REFERENCE (type))
1599               {
1600                 type = lookup_lvalue_reference_type (type);
1601                 result = allocate_value (type);
1602               }
1603           }
1604
1605         return result;
1606       }
1607     else
1608       error (_("Attempt to use a type as an expression"));
1609   }
1610
1611   enum exp_opcode opcode () const override
1612   { return OP_DECLTYPE; }
1613 };
1614
1615 /* Implement 'typeid'.  */
1616 class typeid_operation
1617   : public tuple_holding_operation<operation_up>
1618 {
1619 public:
1620
1621   using tuple_holding_operation::tuple_holding_operation;
1622
1623   value *evaluate (struct type *expect_type,
1624                    struct expression *exp,
1625                    enum noside noside) override
1626   {
1627     enum exp_opcode sub_op = std::get<0> (m_storage)->opcode ();
1628     enum noside sub_noside
1629       = ((sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
1630          ? EVAL_AVOID_SIDE_EFFECTS
1631          : noside);
1632
1633     value *result = std::get<0> (m_storage)->evaluate (nullptr, exp,
1634                                                        sub_noside);
1635     if (noside != EVAL_NORMAL)
1636       return allocate_value (cplus_typeid_type (exp->gdbarch));
1637     return cplus_typeid (result);
1638   }
1639
1640   enum exp_opcode opcode () const override
1641   { return OP_TYPEID; }
1642 };
1643
1644 /* Implement the address-of operation.  */
1645 class unop_addr_operation
1646   : public maybe_constant_operation<operation_up>
1647 {
1648 public:
1649
1650   using maybe_constant_operation::maybe_constant_operation;
1651
1652   value *evaluate (struct type *expect_type,
1653                    struct expression *exp,
1654                    enum noside noside) override
1655   {
1656     /* C++: check for and handle pointer to members.  */
1657     if (noside == EVAL_SKIP)
1658       return eval_skip_value (exp);
1659     else
1660       return std::get<0> (m_storage)->evaluate_for_address (exp, noside);
1661   }
1662
1663   enum exp_opcode opcode () const override
1664   { return UNOP_ADDR; }
1665
1666   /* Return the subexpression.  */
1667   const operation_up &get_expression () const
1668   {
1669     return std::get<0> (m_storage);
1670   }
1671
1672 protected:
1673
1674   void do_generate_ax (struct expression *exp,
1675                        struct agent_expr *ax,
1676                        struct axs_value *value,
1677                        struct type *cast_type)
1678     override
1679   {
1680     gen_expr_unop (exp, UNOP_ADDR,
1681                    std::get<0> (this->m_storage).get (),
1682                    ax, value);
1683   }
1684 };
1685
1686 /* Implement 'sizeof'.  */
1687 class unop_sizeof_operation
1688   : public maybe_constant_operation<operation_up>
1689 {
1690 public:
1691
1692   using maybe_constant_operation::maybe_constant_operation;
1693
1694   value *evaluate (struct type *expect_type,
1695                    struct expression *exp,
1696                    enum noside noside) override
1697   {
1698     if (noside == EVAL_SKIP)
1699       return eval_skip_value (exp);
1700     return std::get<0> (m_storage)->evaluate_for_sizeof (exp, noside);
1701   }
1702
1703   enum exp_opcode opcode () const override
1704   { return UNOP_SIZEOF; }
1705
1706 protected:
1707
1708   void do_generate_ax (struct expression *exp,
1709                        struct agent_expr *ax,
1710                        struct axs_value *value,
1711                        struct type *cast_type)
1712     override;
1713 };
1714
1715 /* Implement 'alignof'.  */
1716 class unop_alignof_operation
1717   : public maybe_constant_operation<operation_up>
1718 {
1719 public:
1720
1721   using maybe_constant_operation::maybe_constant_operation;
1722
1723   value *evaluate (struct type *expect_type,
1724                    struct expression *exp,
1725                    enum noside noside) override
1726   {
1727     value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
1728                                                     EVAL_AVOID_SIDE_EFFECTS);
1729     return eval_op_alignof (expect_type, exp, noside, val);
1730   }
1731
1732   enum exp_opcode opcode () const override
1733   { return UNOP_ALIGNOF; }
1734 };
1735
1736 /* Implement UNOP_MEMVAL.  */
1737 class unop_memval_operation
1738   : public tuple_holding_operation<operation_up, struct type *>
1739 {
1740 public:
1741
1742   using tuple_holding_operation::tuple_holding_operation;
1743
1744   value *evaluate (struct type *expect_type,
1745                    struct expression *exp,
1746                    enum noside noside) override
1747   {
1748     value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
1749     return eval_op_memval (expect_type, exp, noside, val,
1750                            std::get<1> (m_storage));
1751   }
1752
1753   value *evaluate_for_sizeof (struct expression *exp,
1754                               enum noside noside) override;
1755
1756   value *evaluate_for_address (struct expression *exp,
1757                                enum noside noside) override;
1758
1759   enum exp_opcode opcode () const override
1760   { return UNOP_MEMVAL; }
1761
1762 protected:
1763
1764   void do_generate_ax (struct expression *exp,
1765                        struct agent_expr *ax,
1766                        struct axs_value *value,
1767                        struct type *cast_type)
1768     override;
1769 };
1770
1771 /* Implement UNOP_MEMVAL_TYPE.  */
1772 class unop_memval_type_operation
1773   : public tuple_holding_operation<operation_up, operation_up>
1774 {
1775 public:
1776
1777   using tuple_holding_operation::tuple_holding_operation;
1778
1779   value *evaluate (struct type *expect_type,
1780                    struct expression *exp,
1781                    enum noside noside) override
1782   {
1783     value *typeval
1784       = std::get<0> (m_storage)->evaluate (expect_type, exp,
1785                                            EVAL_AVOID_SIDE_EFFECTS);
1786     struct type *type = value_type (typeval);
1787     value *val = std::get<1> (m_storage)->evaluate (expect_type, exp, noside);
1788     return eval_op_memval (expect_type, exp, noside, val, type);
1789   }
1790
1791   value *evaluate_for_sizeof (struct expression *exp,
1792                               enum noside noside) override;
1793
1794   value *evaluate_for_address (struct expression *exp,
1795                                enum noside noside) override;
1796
1797   enum exp_opcode opcode () const override
1798   { return UNOP_MEMVAL_TYPE; }
1799
1800 protected:
1801
1802   void do_generate_ax (struct expression *exp,
1803                        struct agent_expr *ax,
1804                        struct axs_value *value,
1805                        struct type *cast_type)
1806     override;
1807 };
1808
1809 /* Implement the 'this' expression.  */
1810 class op_this_operation
1811   : public tuple_holding_operation<>
1812 {
1813 public:
1814
1815   using tuple_holding_operation::tuple_holding_operation;
1816
1817   value *evaluate (struct type *expect_type,
1818                    struct expression *exp,
1819                    enum noside noside) override
1820   {
1821     return value_of_this (exp->language_defn);
1822   }
1823
1824   enum exp_opcode opcode () const override
1825   { return OP_THIS; }
1826
1827 protected:
1828
1829   void do_generate_ax (struct expression *exp,
1830                        struct agent_expr *ax,
1831                        struct axs_value *value,
1832                        struct type *cast_type)
1833     override;
1834 };
1835
1836 /* Implement the "type instance" operation.  */
1837 class type_instance_operation
1838   : public tuple_holding_operation<type_instance_flags, std::vector<type *>,
1839                                    operation_up>
1840 {
1841 public:
1842
1843   using tuple_holding_operation::tuple_holding_operation;
1844
1845   value *evaluate (struct type *expect_type,
1846                    struct expression *exp,
1847                    enum noside noside) override;
1848
1849   enum exp_opcode opcode () const override
1850   { return TYPE_INSTANCE; }
1851 };
1852
1853 /* The assignment operator.  */
1854 class assign_operation
1855   : public tuple_holding_operation<operation_up, operation_up>
1856 {
1857 public:
1858
1859   using tuple_holding_operation::tuple_holding_operation;
1860
1861   value *evaluate (struct type *expect_type,
1862                    struct expression *exp,
1863                    enum noside noside) override
1864   {
1865     value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1866     /* Special-case assignments where the left-hand-side is a
1867        convenience variable -- in these, don't bother setting an
1868        expected type.  This avoids a weird case where re-assigning a
1869        string or array to an internal variable could error with "Too
1870        many array elements".  */
1871     struct type *xtype = (VALUE_LVAL (lhs) == lval_internalvar
1872                           ? nullptr
1873                           : value_type (lhs));
1874     value *rhs = std::get<1> (m_storage)->evaluate (xtype, exp, noside);
1875
1876     if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1877       return lhs;
1878     if (binop_user_defined_p (BINOP_ASSIGN, lhs, rhs))
1879       return value_x_binop (lhs, rhs, BINOP_ASSIGN, OP_NULL, noside);
1880     else
1881       return value_assign (lhs, rhs);
1882   }
1883
1884   enum exp_opcode opcode () const override
1885   { return BINOP_ASSIGN; }
1886
1887 protected:
1888
1889   void do_generate_ax (struct expression *exp,
1890                        struct agent_expr *ax,
1891                        struct axs_value *value,
1892                        struct type *cast_type)
1893     override;
1894 };
1895
1896 /* Assignment with modification, like "+=".  */
1897 class assign_modify_operation
1898   : public tuple_holding_operation<exp_opcode, operation_up, operation_up>
1899 {
1900 public:
1901
1902   using tuple_holding_operation::tuple_holding_operation;
1903
1904   value *evaluate (struct type *expect_type,
1905                    struct expression *exp,
1906                    enum noside noside) override
1907   {
1908     value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1909     value *rhs = std::get<2> (m_storage)->evaluate (expect_type, exp, noside);
1910     return eval_binop_assign_modify (expect_type, exp, noside,
1911                                      std::get<0> (m_storage), lhs, rhs);
1912   }
1913
1914   enum exp_opcode opcode () const override
1915   { return BINOP_ASSIGN_MODIFY; }
1916
1917 protected:
1918
1919   void do_generate_ax (struct expression *exp,
1920                        struct agent_expr *ax,
1921                        struct axs_value *value,
1922                        struct type *cast_type)
1923     override;
1924 };
1925
1926 /* A type cast.  */
1927 class unop_cast_operation
1928   : public maybe_constant_operation<operation_up, struct type *>
1929 {
1930 public:
1931
1932   using maybe_constant_operation::maybe_constant_operation;
1933
1934   value *evaluate (struct type *expect_type,
1935                    struct expression *exp,
1936                    enum noside noside) override
1937   {
1938     return std::get<0> (m_storage)->evaluate_for_cast (std::get<1> (m_storage),
1939                                                        exp, noside);
1940   }
1941
1942   enum exp_opcode opcode () const override
1943   { return UNOP_CAST; }
1944
1945 protected:
1946
1947   void do_generate_ax (struct expression *exp,
1948                        struct agent_expr *ax,
1949                        struct axs_value *value,
1950                        struct type *cast_type)
1951     override;
1952 };
1953
1954 /* A cast, but the type comes from an expression, not a "struct
1955    type".  */
1956 class unop_cast_type_operation
1957   : public maybe_constant_operation<operation_up, operation_up>
1958 {
1959 public:
1960
1961   using maybe_constant_operation::maybe_constant_operation;
1962
1963   value *evaluate (struct type *expect_type,
1964                    struct expression *exp,
1965                    enum noside noside) override
1966   {
1967     value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
1968                                                     EVAL_AVOID_SIDE_EFFECTS);
1969     return std::get<1> (m_storage)->evaluate_for_cast (value_type (val),
1970                                                        exp, noside);
1971   }
1972
1973   enum exp_opcode opcode () const override
1974   { return UNOP_CAST_TYPE; }
1975
1976 protected:
1977
1978   void do_generate_ax (struct expression *exp,
1979                        struct agent_expr *ax,
1980                        struct axs_value *value,
1981                        struct type *cast_type)
1982     override;
1983 };
1984
1985 typedef value *cxx_cast_ftype (struct type *, value *);
1986
1987 /* This implements dynamic_cast and reinterpret_cast.  static_cast and
1988    const_cast are handled by the ordinary case operations.  */
1989 template<exp_opcode OP, cxx_cast_ftype FUNC>
1990 class cxx_cast_operation
1991   : public maybe_constant_operation<operation_up, operation_up>
1992 {
1993 public:
1994
1995   using maybe_constant_operation::maybe_constant_operation;
1996
1997   value *evaluate (struct type *expect_type,
1998                    struct expression *exp,
1999                    enum noside noside) override
2000   {
2001     value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2002                                                     EVAL_AVOID_SIDE_EFFECTS);
2003     struct type *type = value_type (val);
2004     value *rhs = std::get<1> (m_storage)->evaluate (type, exp, noside);
2005     if (noside == EVAL_SKIP)
2006       return eval_skip_value (exp);
2007     return FUNC (type, rhs);
2008   }
2009
2010   enum exp_opcode opcode () const override
2011   { return OP; }
2012 };
2013
2014 using dynamic_cast_operation = cxx_cast_operation<UNOP_DYNAMIC_CAST,
2015                                                   value_dynamic_cast>;
2016 using reinterpret_cast_operation = cxx_cast_operation<UNOP_REINTERPRET_CAST,
2017                                                       value_reinterpret_cast>;
2018
2019 /* Multi-dimensional subscripting.  */
2020 class multi_subscript_operation
2021   : public tuple_holding_operation<operation_up, std::vector<operation_up>>
2022 {
2023 public:
2024
2025   using tuple_holding_operation::tuple_holding_operation;
2026
2027   value *evaluate (struct type *expect_type,
2028                    struct expression *exp,
2029                    enum noside noside) override;
2030
2031   enum exp_opcode opcode () const override
2032   { return MULTI_SUBSCRIPT; }
2033 };
2034
2035 /* The "&&" operator.  */
2036 class logical_and_operation
2037   : public maybe_constant_operation<operation_up, operation_up>
2038 {
2039 public:
2040
2041   using maybe_constant_operation::maybe_constant_operation;
2042
2043   value *evaluate (struct type *expect_type,
2044                    struct expression *exp,
2045                    enum noside noside) override;
2046
2047   enum exp_opcode opcode () const override
2048   { return BINOP_LOGICAL_AND; }
2049
2050 protected:
2051
2052   void do_generate_ax (struct expression *exp,
2053                        struct agent_expr *ax,
2054                        struct axs_value *value,
2055                        struct type *cast_type)
2056     override;
2057 };
2058
2059 /* The "||" operator.  */
2060 class logical_or_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_OR; }
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 /* This class implements ADL (aka Koenig) function calls for C++.  It
2084    holds the name of the function to call, the block in which the
2085    lookup should be done, and a vector of arguments.  */
2086 class adl_func_operation
2087   : public tuple_holding_operation<std::string, const block *,
2088                                    std::vector<operation_up>>
2089 {
2090 public:
2091
2092   using tuple_holding_operation::tuple_holding_operation;
2093
2094   value *evaluate (struct type *expect_type,
2095                    struct expression *exp,
2096                    enum noside noside) override;
2097
2098   enum exp_opcode opcode () const override
2099   { return OP_ADL_FUNC; }
2100 };
2101
2102 /* The OP_ARRAY operation.  */
2103 class array_operation
2104   : public tuple_holding_operation<int, int, std::vector<operation_up>>
2105 {
2106 public:
2107
2108   using tuple_holding_operation::tuple_holding_operation;
2109
2110   value *evaluate (struct type *expect_type,
2111                    struct expression *exp,
2112                    enum noside noside) override;
2113
2114   enum exp_opcode opcode () const override
2115   { return OP_ARRAY; }
2116
2117 private:
2118
2119   struct value *evaluate_struct_tuple (struct value *struct_val,
2120                                        struct expression *exp,
2121                                        enum noside noside, int nargs);
2122 };
2123
2124 /* A function call.  This holds the callee operation and the
2125    arguments.  */
2126 class funcall_operation
2127   : public tuple_holding_operation<operation_up, std::vector<operation_up>>
2128 {
2129 public:
2130
2131   using tuple_holding_operation::tuple_holding_operation;
2132
2133   value *evaluate (struct type *expect_type,
2134                    struct expression *exp,
2135                    enum noside noside) override
2136   {
2137     return std::get<0> (m_storage)->evaluate_funcall (expect_type, exp, noside,
2138                                                       std::get<1> (m_storage));
2139   }
2140
2141   enum exp_opcode opcode () const override
2142   { return OP_FUNCALL; }
2143 };
2144
2145 } /* namespace expr */
2146
2147 #endif /* EXPOP_H */
This page took 0.132229 seconds and 2 git commands to generate.