1 /* Definitions for expressions in GDB
3 Copyright (C) 2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
26 #include "expression.h"
28 #include "gdbsupport/traits.h"
29 #include "gdbsupport/enum-flags.h"
34 extern void gen_expr_binop (struct expression
*exp
,
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
,
42 struct agent_expr
*ax
, struct axs_value
*value
);
44 extern struct value
*eval_op_scope (struct type
*expect_type
,
45 struct expression
*exp
,
47 struct type
*type
, const char *string
);
48 extern struct value
*eval_op_var_msym_value (struct type
*expect_type
,
49 struct expression
*exp
,
52 minimal_symbol
*msymbol
,
53 struct objfile
*objfile
);
54 extern struct value
*eval_op_var_entry_value (struct type
*expect_type
,
55 struct expression
*exp
,
56 enum noside noside
, symbol
*sym
);
57 extern struct value
*eval_op_func_static_var (struct type
*expect_type
,
58 struct expression
*exp
,
60 value
*func
, const char *var
);
61 extern struct value
*eval_op_register (struct type
*expect_type
,
62 struct expression
*exp
,
63 enum noside noside
, const char *name
);
68 /* The check_objfile overloads are used to check whether a particular
69 component of some operation references an objfile. The passed-in
70 objfile will never be a debug objfile. */
72 /* See if EXP_OBJFILE matches OBJFILE. */
74 check_objfile (struct objfile
*exp_objfile
, struct objfile
*objfile
)
76 if (exp_objfile
->separate_debug_objfile_backlink
)
77 exp_objfile
= exp_objfile
->separate_debug_objfile_backlink
;
78 return exp_objfile
== objfile
;
82 check_objfile (struct type
*type
, struct objfile
*objfile
)
84 struct objfile
*ty_objfile
= type
->objfile_owner ();
85 if (ty_objfile
!= nullptr)
86 return check_objfile (ty_objfile
, objfile
);
91 check_objfile (struct symbol
*sym
, struct objfile
*objfile
)
93 return check_objfile (symbol_objfile (sym
), objfile
);
97 check_objfile (const struct block
*block
, struct objfile
*objfile
)
99 return check_objfile (block_objfile (block
), objfile
);
103 check_objfile (minimal_symbol
*minsym
, struct objfile
*objfile
)
105 /* This may seem strange but minsyms are only used with an objfile
111 check_objfile (internalvar
*ivar
, struct objfile
*objfile
)
117 check_objfile (const std::string
&str
, struct objfile
*objfile
)
123 check_objfile (const operation_up
&op
, struct objfile
*objfile
)
125 return op
->uses_objfile (objfile
);
129 check_objfile (enum exp_opcode val
, struct objfile
*objfile
)
135 check_objfile (ULONGEST val
, struct objfile
*objfile
)
142 check_objfile (enum_flags
<T
> val
, struct objfile
*objfile
)
149 check_objfile (const std::vector
<T
> &collection
, struct objfile
*objfile
)
151 for (const auto &item
: collection
)
153 if (check_objfile (item
, objfile
))
159 template<typename S
, typename T
>
161 check_objfile (const std::pair
<S
, T
> &item
, struct objfile
*objfile
)
163 return (check_objfile (item
.first
, objfile
)
164 || check_objfile (item
.second
, objfile
));
168 dump_for_expression (struct ui_file
*stream
, int depth
,
169 const operation_up
&op
)
171 op
->dump (stream
, depth
);
174 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
176 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
177 const std::string
&str
);
178 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
180 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
182 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
184 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
186 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
187 minimal_symbol
*msym
);
188 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
190 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
191 type_instance_flags flags
);
192 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
193 enum c_string_type_values flags
);
194 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
195 enum range_flag flags
);
196 extern void dump_for_expression (struct ui_file
*stream
, int depth
,
201 dump_for_expression (struct ui_file
*stream
, int depth
,
202 const std::vector
<T
> &vals
)
204 fprintf_filtered (stream
, _("%*sVector:\n"), depth
, "");
205 for (auto &item
: vals
)
206 dump_for_expression (stream
, depth
+ 1, item
);
209 template<typename X
, typename Y
>
211 dump_for_expression (struct ui_file
*stream
, int depth
,
212 const std::pair
<X
, Y
> &vals
)
214 dump_for_expression (stream
, depth
, vals
.first
);
215 dump_for_expression (stream
, depth
, vals
.second
);
218 /* Base class for most concrete operations. This class holds data,
219 specified via template parameters, and supplies generic
220 implementations of the 'dump' and 'uses_objfile' methods. */
221 template<typename
... Arg
>
222 class tuple_holding_operation
: public operation
226 explicit tuple_holding_operation (Arg
... args
)
227 : m_storage (std::forward
<Arg
> (args
)...)
231 DISABLE_COPY_AND_ASSIGN (tuple_holding_operation
);
233 bool uses_objfile (struct objfile
*objfile
) const override
235 return do_check_objfile
<0, Arg
...> (objfile
, m_storage
);
238 void dump (struct ui_file
*stream
, int depth
) const override
240 dump_for_expression (stream
, depth
, opcode ());
241 do_dump
<0, Arg
...> (stream
, depth
+ 1, m_storage
);
246 /* Storage for the data. */
247 std::tuple
<Arg
...> m_storage
;
251 /* do_dump does the work of dumping the data. */
252 template<int I
, typename
... T
>
253 typename
std::enable_if
<I
== sizeof... (T
), void>::type
254 do_dump (struct ui_file
*stream
, int depth
, const std::tuple
<T
...> &value
)
259 template<int I
, typename
... T
>
260 typename
std::enable_if
<I
< sizeof... (T
), void>::type
261 do_dump (struct ui_file
*stream
, int depth
, const std::tuple
<T
...> &value
)
264 dump_for_expression (stream
, depth
, std::get
<I
> (value
));
265 do_dump
<I
+ 1, T
...> (stream
, depth
, value
);
268 /* do_check_objfile does the work of checking whether this object
269 refers to OBJFILE. */
270 template<int I
, typename
... T
>
271 typename
std::enable_if
<I
== sizeof... (T
), bool>::type
272 do_check_objfile (struct objfile
*objfile
, const std::tuple
<T
...> &value
)
278 template<int I
, typename
... T
>
279 typename
std::enable_if
<I
< sizeof... (T
), bool>::type
280 do_check_objfile (struct objfile
*objfile
, const std::tuple
<T
...> &value
)
283 if (check_objfile (std::get
<I
> (value
), objfile
))
285 return do_check_objfile
<I
+ 1, T
...> (objfile
, value
);
289 /* The check_constant overloads are used to decide whether a given
290 concrete operation is a constant. This is done by checking the
294 check_constant (const operation_up
&item
)
296 return item
->constant_p ();
300 check_constant (struct minimal_symbol
*msym
)
306 check_constant (struct type
*type
)
312 check_constant (const struct block
*block
)
318 check_constant (const std::string
&str
)
324 check_constant (struct objfile
*objfile
)
330 check_constant (ULONGEST cst
)
336 check_constant (struct symbol
*sym
)
338 enum address_class sc
= SYMBOL_CLASS (sym
);
339 return (sc
== LOC_BLOCK
341 || sc
== LOC_CONST_BYTES
347 check_constant (const std::vector
<T
> &collection
)
349 for (const auto &item
: collection
)
350 if (!check_constant (item
))
355 template<typename S
, typename T
>
357 check_constant (const std::pair
<S
, T
> &item
)
359 return check_constant (item
.first
) && check_constant (item
.second
);
362 /* Base class for concrete operations. This class supplies an
363 implementation of 'constant_p' that works by checking the
365 template<typename
... Arg
>
366 class maybe_constant_operation
367 : public tuple_holding_operation
<Arg
...>
371 using tuple_holding_operation
<Arg
...>::tuple_holding_operation
;
373 bool constant_p () const override
375 return do_check_constant
<0, Arg
...> (this->m_storage
);
380 template<int I
, typename
... T
>
381 typename
std::enable_if
<I
== sizeof... (T
), bool>::type
382 do_check_constant (const std::tuple
<T
...> &value
) const
387 template<int I
, typename
... T
>
388 typename
std::enable_if
<I
< sizeof... (T
), bool>::type
389 do_check_constant (const std::tuple
<T
...> &value
) const
391 if (!check_constant (std::get
<I
> (value
)))
393 return do_check_constant
<I
+ 1, T
...> (value
);
397 /* A floating-point constant. The constant is encoded in the target
400 typedef std::array
<gdb_byte
, 16> float_data
;
402 /* An operation that holds a floating-point constant of a given
405 This does not need the facilities provided by
406 tuple_holding_operation, so it does not use it. */
407 class float_const_operation
412 float_const_operation (struct type
*type
, float_data data
)
418 value
*evaluate (struct type
*expect_type
,
419 struct expression
*exp
,
420 enum noside noside
) override
422 return value_from_contents (m_type
, m_data
.data ());
425 enum exp_opcode
opcode () const override
428 bool constant_p () const override
431 void dump (struct ui_file
*stream
, int depth
) const override
;
439 class scope_operation
440 : public maybe_constant_operation
<struct type
*, std::string
>
444 using maybe_constant_operation::maybe_constant_operation
;
446 value
*evaluate (struct type
*expect_type
,
447 struct expression
*exp
,
448 enum noside noside
) override
450 return eval_op_scope (expect_type
, exp
, noside
,
451 std::get
<0> (m_storage
),
452 std::get
<1> (m_storage
).c_str ());
455 value
*evaluate_for_address (struct expression
*exp
,
456 enum noside noside
) override
;
458 enum exp_opcode
opcode () const override
463 void do_generate_ax (struct expression
*exp
,
464 struct agent_expr
*ax
,
465 struct axs_value
*value
,
466 struct type
*cast_type
)
470 class long_const_operation
471 : public tuple_holding_operation
<struct type
*, LONGEST
>
475 using tuple_holding_operation::tuple_holding_operation
;
477 value
*evaluate (struct type
*expect_type
,
478 struct expression
*exp
,
479 enum noside noside
) override
481 return value_from_longest (std::get
<0> (m_storage
),
482 std::get
<1> (m_storage
));
485 enum exp_opcode
opcode () const override
488 bool constant_p () const override
493 void do_generate_ax (struct expression
*exp
,
494 struct agent_expr
*ax
,
495 struct axs_value
*value
,
496 struct type
*cast_type
)
500 class var_msym_value_operation
501 : public maybe_constant_operation
<minimal_symbol
*, struct objfile
*>
505 using maybe_constant_operation::maybe_constant_operation
;
507 value
*evaluate (struct type
*expect_type
,
508 struct expression
*exp
,
509 enum noside noside
) override
511 return eval_op_var_msym_value (expect_type
, exp
, noside
, m_outermost
,
512 std::get
<0> (m_storage
),
513 std::get
<1> (m_storage
));
516 value
*evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
519 value
*evaluate_for_address (struct expression
*exp
, enum noside noside
)
522 value
*evaluate_for_cast (struct type
*expect_type
,
523 struct expression
*exp
,
524 enum noside noside
) override
;
526 enum exp_opcode
opcode () const override
527 { return OP_VAR_MSYM_VALUE
; }
529 void set_outermost () override
536 /* True if this is the outermost operation in the expression. */
537 bool m_outermost
= false;
539 void do_generate_ax (struct expression
*exp
,
540 struct agent_expr
*ax
,
541 struct axs_value
*value
,
542 struct type
*cast_type
)
546 class var_entry_value_operation
547 : public tuple_holding_operation
<symbol
*>
551 using tuple_holding_operation::tuple_holding_operation
;
553 value
*evaluate (struct type
*expect_type
,
554 struct expression
*exp
,
555 enum noside noside
) override
557 return eval_op_var_entry_value (expect_type
, exp
, noside
,
558 std::get
<0> (m_storage
));
561 enum exp_opcode
opcode () const override
562 { return OP_VAR_ENTRY_VALUE
; }
565 class func_static_var_operation
566 : public maybe_constant_operation
<operation_up
, std::string
>
570 using maybe_constant_operation::maybe_constant_operation
;
572 value
*evaluate (struct type
*expect_type
,
573 struct expression
*exp
,
574 enum noside noside
) override
576 value
*func
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
577 return eval_op_func_static_var (expect_type
, exp
, noside
, func
,
578 std::get
<1> (m_storage
).c_str ());
581 enum exp_opcode
opcode () const override
582 { return OP_FUNC_STATIC_VAR
; }
586 : public tuple_holding_operation
<int>
590 using tuple_holding_operation::tuple_holding_operation
;
592 value
*evaluate (struct type
*expect_type
,
593 struct expression
*exp
,
594 enum noside noside
) override
596 return access_value_history (std::get
<0> (m_storage
));
599 enum exp_opcode
opcode () const override
603 class register_operation
604 : public tuple_holding_operation
<std::string
>
608 using tuple_holding_operation::tuple_holding_operation
;
610 value
*evaluate (struct type
*expect_type
,
611 struct expression
*exp
,
612 enum noside noside
) override
614 return eval_op_register (expect_type
, exp
, noside
,
615 std::get
<0> (m_storage
).c_str ());
618 enum exp_opcode
opcode () const override
619 { return OP_REGISTER
; }
623 void do_generate_ax (struct expression
*exp
,
624 struct agent_expr
*ax
,
625 struct axs_value
*value
,
626 struct type
*cast_type
)
631 : public tuple_holding_operation
<bool>
635 using tuple_holding_operation::tuple_holding_operation
;
637 value
*evaluate (struct type
*expect_type
,
638 struct expression
*exp
,
639 enum noside noside
) override
641 struct type
*type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
642 return value_from_longest (type
, std::get
<0> (m_storage
));
645 enum exp_opcode
opcode () const override
648 bool constant_p () const override
652 } /* namespace expr */