1 /* Definitions for Ada expressions
3 Copyright (C) 2020, 2021 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/>. */
25 extern struct value
*ada_unop_neg (struct type
*expect_type
,
26 struct expression
*exp
,
27 enum noside noside
, enum exp_opcode op
,
29 extern struct value
*ada_atr_tag (struct type
*expect_type
,
30 struct expression
*exp
,
31 enum noside noside
, enum exp_opcode op
,
33 extern struct value
*ada_atr_size (struct type
*expect_type
,
34 struct expression
*exp
,
35 enum noside noside
, enum exp_opcode op
,
37 extern struct value
*ada_abs (struct type
*expect_type
,
38 struct expression
*exp
,
39 enum noside noside
, enum exp_opcode op
,
41 extern struct value
*ada_unop_in_range (struct type
*expect_type
,
42 struct expression
*exp
,
43 enum noside noside
, enum exp_opcode op
,
44 struct value
*arg1
, struct type
*type
);
45 extern struct value
*ada_mult_binop (struct type
*expect_type
,
46 struct expression
*exp
,
47 enum noside noside
, enum exp_opcode op
,
48 struct value
*arg1
, struct value
*arg2
);
49 extern struct value
*ada_equal_binop (struct type
*expect_type
,
50 struct expression
*exp
,
51 enum noside noside
, enum exp_opcode op
,
52 struct value
*arg1
, struct value
*arg2
);
53 extern struct value
*ada_ternop_slice (struct expression
*exp
,
56 struct value
*low_bound_val
,
57 struct value
*high_bound_val
);
58 extern struct value
*ada_binop_in_bounds (struct expression
*exp
,
63 extern struct value
*ada_binop_minmax (struct type
*expect_type
,
64 struct expression
*exp
,
65 enum noside noside
, enum exp_opcode op
,
68 extern struct value
*ada_pos_atr (struct type
*expect_type
,
69 struct expression
*exp
,
70 enum noside noside
, enum exp_opcode op
,
72 extern struct value
*ada_val_atr (enum noside noside
, struct type
*type
,
74 extern struct value
*ada_binop_exp (struct type
*expect_type
,
75 struct expression
*exp
,
76 enum noside noside
, enum exp_opcode op
,
77 struct value
*arg1
, struct value
*arg2
);
82 /* The base class for Ada type resolution. Ada operations that want
83 to participate in resolution implement this interface. */
86 /* Resolve this object. EXP is the expression being resolved.
87 DEPROCEDURE_P is true if a symbol that refers to a zero-argument
88 function may be turned into a function call. PARSE_COMPLETION
89 and TRACKER are passed in from the parser context. CONTEXT_TYPE
90 is the expected type of the expression, or nullptr if none is
91 known. This method should return true if the operation should be
92 replaced by a function call with this object as the callee. */
93 virtual bool resolve (struct expression
*exp
,
95 bool parse_completion
,
96 innermost_block_tracker
*tracker
,
97 struct type
*context_type
) = 0;
100 /* In Ada, some generic operations must be wrapped with a handler that
101 handles some Ada-specific type conversions. */
102 class ada_wrapped_operation
103 : public tuple_holding_operation
<operation_up
>
107 using tuple_holding_operation::tuple_holding_operation
;
109 value
*evaluate (struct type
*expect_type
,
110 struct expression
*exp
,
111 enum noside noside
) override
;
113 enum exp_opcode
opcode () const override
114 { return std::get
<0> (m_storage
)->opcode (); }
117 /* An Ada string constant. */
118 class ada_string_operation
119 : public string_operation
123 using string_operation::string_operation
;
125 /* Return the underlying string. */
126 const char *get_name () const
128 return std::get
<0> (m_storage
).c_str ();
131 value
*evaluate (struct type
*expect_type
,
132 struct expression
*exp
,
133 enum noside noside
) override
;
136 /* The Ada TYPE'(EXP) construct. */
137 class ada_qual_operation
138 : public tuple_holding_operation
<operation_up
, struct type
*>
142 using tuple_holding_operation::tuple_holding_operation
;
144 value
*evaluate (struct type
*expect_type
,
145 struct expression
*exp
,
146 enum noside noside
) override
;
148 enum exp_opcode
opcode () const override
149 { return UNOP_QUAL
; }
152 /* Ternary in-range operator. */
153 class ada_ternop_range_operation
154 : public tuple_holding_operation
<operation_up
, operation_up
, operation_up
>
158 using tuple_holding_operation::tuple_holding_operation
;
160 value
*evaluate (struct type
*expect_type
,
161 struct expression
*exp
,
162 enum noside noside
) override
;
164 enum exp_opcode
opcode () const override
165 { return TERNOP_IN_RANGE
; }
168 using ada_neg_operation
= unop_operation
<UNOP_NEG
, ada_unop_neg
>;
169 using ada_atr_tag_operation
= unop_operation
<OP_ATR_TAG
, ada_atr_tag
>;
170 using ada_atr_size_operation
= unop_operation
<OP_ATR_SIZE
, ada_atr_size
>;
171 using ada_abs_operation
= unop_operation
<UNOP_ABS
, ada_abs
>;
172 using ada_pos_operation
= unop_operation
<OP_ATR_POS
, ada_pos_atr
>;
174 /* The in-range operation, given a type. */
175 class ada_unop_range_operation
176 : public tuple_holding_operation
<operation_up
, struct type
*>
180 using tuple_holding_operation::tuple_holding_operation
;
182 value
*evaluate (struct type
*expect_type
,
183 struct expression
*exp
,
184 enum noside noside
) override
186 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
187 return ada_unop_in_range (expect_type
, exp
, noside
, UNOP_IN_RANGE
,
188 val
, std::get
<1> (m_storage
));
191 enum exp_opcode
opcode () const override
192 { return UNOP_IN_RANGE
; }
195 /* The Ada + and - operators. */
196 class ada_binop_addsub_operation
197 : public tuple_holding_operation
<enum exp_opcode
, operation_up
, operation_up
>
201 using tuple_holding_operation::tuple_holding_operation
;
203 value
*evaluate (struct type
*expect_type
,
204 struct expression
*exp
,
205 enum noside noside
) override
;
207 enum exp_opcode
opcode () const override
208 { return std::get
<0> (m_storage
); }
211 using ada_binop_mul_operation
= binop_operation
<BINOP_MUL
, ada_mult_binop
>;
212 using ada_binop_div_operation
= binop_operation
<BINOP_DIV
, ada_mult_binop
>;
213 using ada_binop_rem_operation
= binop_operation
<BINOP_REM
, ada_mult_binop
>;
214 using ada_binop_mod_operation
= binop_operation
<BINOP_MOD
, ada_mult_binop
>;
216 using ada_binop_min_operation
= binop_operation
<BINOP_MIN
, ada_binop_minmax
>;
217 using ada_binop_max_operation
= binop_operation
<BINOP_MAX
, ada_binop_minmax
>;
219 using ada_binop_exp_operation
= binop_operation
<BINOP_EXP
, ada_binop_exp
>;
221 /* Implement the equal and not-equal operations for Ada. */
222 class ada_binop_equal_operation
223 : public tuple_holding_operation
<enum exp_opcode
, operation_up
, operation_up
>
227 using tuple_holding_operation::tuple_holding_operation
;
229 value
*evaluate (struct type
*expect_type
,
230 struct expression
*exp
,
231 enum noside noside
) override
233 value
*arg1
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
234 value
*arg2
= std::get
<2> (m_storage
)->evaluate (value_type (arg1
),
236 return ada_equal_binop (expect_type
, exp
, noside
, std::get
<0> (m_storage
),
240 enum exp_opcode
opcode () const override
241 { return std::get
<0> (m_storage
); }
244 /* Bitwise operators for Ada. */
245 template<enum exp_opcode OP
>
246 class ada_bitwise_operation
247 : public maybe_constant_operation
<operation_up
, operation_up
>
251 using maybe_constant_operation::maybe_constant_operation
;
253 value
*evaluate (struct type
*expect_type
,
254 struct expression
*exp
,
255 enum noside noside
) override
257 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
258 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
259 value
*result
= eval_op_binary (expect_type
, exp
, noside
, OP
, lhs
, rhs
);
260 return value_cast (value_type (lhs
), result
);
263 enum exp_opcode
opcode () const override
267 using ada_bitwise_and_operation
= ada_bitwise_operation
<BINOP_BITWISE_AND
>;
268 using ada_bitwise_ior_operation
= ada_bitwise_operation
<BINOP_BITWISE_IOR
>;
269 using ada_bitwise_xor_operation
= ada_bitwise_operation
<BINOP_BITWISE_XOR
>;
271 /* Ada array- or string-slice operation. */
272 class ada_ternop_slice_operation
273 : public maybe_constant_operation
<operation_up
, operation_up
, operation_up
>,
274 public ada_resolvable
278 using maybe_constant_operation::maybe_constant_operation
;
280 value
*evaluate (struct type
*expect_type
,
281 struct expression
*exp
,
282 enum noside noside
) override
284 value
*array
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
285 value
*low
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
286 value
*high
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
287 return ada_ternop_slice (exp
, noside
, array
, low
, high
);
290 enum exp_opcode
opcode () const override
291 { return TERNOP_SLICE
; }
293 bool resolve (struct expression
*exp
,
295 bool parse_completion
,
296 innermost_block_tracker
*tracker
,
297 struct type
*context_type
) override
;
300 /* Implement BINOP_IN_BOUNDS for Ada. */
301 class ada_binop_in_bounds_operation
302 : public maybe_constant_operation
<operation_up
, operation_up
, int>
306 using maybe_constant_operation::maybe_constant_operation
;
308 value
*evaluate (struct type
*expect_type
,
309 struct expression
*exp
,
310 enum noside noside
) override
312 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
313 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
314 return ada_binop_in_bounds (exp
, noside
, arg1
, arg2
,
315 std::get
<2> (m_storage
));
318 enum exp_opcode
opcode () const override
319 { return BINOP_IN_BOUNDS
; }
322 /* Implement several unary Ada OP_ATR_* operations. */
323 class ada_unop_atr_operation
324 : public maybe_constant_operation
<operation_up
, enum exp_opcode
, int>
328 using maybe_constant_operation::maybe_constant_operation
;
330 value
*evaluate (struct type
*expect_type
,
331 struct expression
*exp
,
332 enum noside noside
) override
;
334 enum exp_opcode
opcode () const override
335 { return std::get
<1> (m_storage
); }
338 /* Variant of var_value_operation for Ada. */
339 class ada_var_value_operation
340 : public var_value_operation
, public ada_resolvable
344 using var_value_operation::var_value_operation
;
346 value
*evaluate (struct type
*expect_type
,
347 struct expression
*exp
,
348 enum noside noside
) override
;
350 value
*evaluate_for_cast (struct type
*expect_type
,
351 struct expression
*exp
,
352 enum noside noside
) override
;
354 const block
*get_block () const
355 { return std::get
<0> (m_storage
).block
; }
357 bool resolve (struct expression
*exp
,
359 bool parse_completion
,
360 innermost_block_tracker
*tracker
,
361 struct type
*context_type
) override
;
365 using operation::do_generate_ax
;
368 /* Variant of var_msym_value_operation for Ada. */
369 class ada_var_msym_value_operation
370 : public var_msym_value_operation
374 using var_msym_value_operation::var_msym_value_operation
;
376 value
*evaluate_for_cast (struct type
*expect_type
,
377 struct expression
*exp
,
378 enum noside noside
) override
;
382 using operation::do_generate_ax
;
385 /* Implement the Ada 'val attribute. */
386 class ada_atr_val_operation
387 : public tuple_holding_operation
<struct type
*, operation_up
>
391 using tuple_holding_operation::tuple_holding_operation
;
393 value
*evaluate (struct type
*expect_type
,
394 struct expression
*exp
,
395 enum noside noside
) override
;
397 enum exp_opcode
opcode () const override
398 { return OP_ATR_VAL
; }
401 /* The indirection operator for Ada. */
402 class ada_unop_ind_operation
403 : public unop_ind_base_operation
407 using unop_ind_base_operation::unop_ind_base_operation
;
409 value
*evaluate (struct type
*expect_type
,
410 struct expression
*exp
,
411 enum noside noside
) override
;
414 /* Implement STRUCTOP_STRUCT for Ada. */
415 class ada_structop_operation
416 : public structop_base_operation
420 using structop_base_operation::structop_base_operation
;
422 value
*evaluate (struct type
*expect_type
,
423 struct expression
*exp
,
424 enum noside noside
) override
;
426 enum exp_opcode
opcode () const override
427 { return STRUCTOP_STRUCT
; }
430 /* Function calls for Ada. */
431 class ada_funcall_operation
432 : public tuple_holding_operation
<operation_up
, std::vector
<operation_up
>>,
433 public ada_resolvable
437 using tuple_holding_operation::tuple_holding_operation
;
439 value
*evaluate (struct type
*expect_type
,
440 struct expression
*exp
,
441 enum noside noside
) override
;
443 bool resolve (struct expression
*exp
,
445 bool parse_completion
,
446 innermost_block_tracker
*tracker
,
447 struct type
*context_type
) override
;
449 enum exp_opcode
opcode () const override
450 { return OP_FUNCALL
; }
453 /* An Ada assignment operation. */
454 class ada_assign_operation
455 : public assign_operation
459 using assign_operation::assign_operation
;
461 value
*evaluate (struct type
*expect_type
,
462 struct expression
*exp
,
463 enum noside noside
) override
;
465 enum exp_opcode
opcode () const override
466 { return BINOP_ASSIGN
; }
469 /* This abstract class represents a single component in an Ada
470 aggregate assignment. */
475 /* Assign to LHS, which is part of CONTAINER. EXP is the expression
476 being evaluated. INDICES, LOW, and HIGH indicate which
477 sub-components have already been assigned; INDICES should be
478 updated by this call. */
479 virtual void assign (struct value
*container
,
480 struct value
*lhs
, struct expression
*exp
,
481 std::vector
<LONGEST
> &indices
,
482 LONGEST low
, LONGEST high
) = 0;
484 /* Same as operation::uses_objfile. */
485 virtual bool uses_objfile (struct objfile
*objfile
) = 0;
487 /* Same as operation::dump. */
488 virtual void dump (ui_file
*stream
, int depth
) = 0;
490 virtual ~ada_component () = default;
494 ada_component () = default;
495 DISABLE_COPY_AND_ASSIGN (ada_component
);
498 /* Unique pointer specialization for Ada assignment components. */
499 typedef std::unique_ptr
<ada_component
> ada_component_up
;
501 /* An operation that holds a single component. */
502 class ada_aggregate_operation
503 : public tuple_holding_operation
<ada_component_up
>
507 using tuple_holding_operation::tuple_holding_operation
;
509 /* Assuming that LHS represents an lvalue having a record or array
510 type, evaluate an assignment of this aggregate's value to LHS.
511 CONTAINER is an lvalue containing LHS (possibly LHS itself).
512 Does not modify the inferior's memory, nor does it modify the
513 contents of LHS (unless == CONTAINER). Returns the modified
516 value
*assign_aggregate (struct value
*container
,
518 struct expression
*exp
);
520 value
*evaluate (struct type
*expect_type
,
521 struct expression
*exp
,
522 enum noside noside
) override
524 error (_("Aggregates only allowed on the right of an assignment"));
527 enum exp_opcode
opcode () const override
528 { return OP_AGGREGATE
; }
531 /* A component holding a vector of other components to assign. */
532 class ada_aggregate_component
: public ada_component
536 explicit ada_aggregate_component (std::vector
<ada_component_up
> &&components
)
537 : m_components (std::move (components
))
541 void assign (struct value
*container
,
542 struct value
*lhs
, struct expression
*exp
,
543 std::vector
<LONGEST
> &indices
,
544 LONGEST low
, LONGEST high
) override
;
546 bool uses_objfile (struct objfile
*objfile
) override
;
548 void dump (ui_file
*stream
, int depth
) override
;
552 std::vector
<ada_component_up
> m_components
;
555 /* A component that assigns according to a provided index (which is
556 relative to the "low" value). */
557 class ada_positional_component
: public ada_component
561 ada_positional_component (int index
, operation_up
&&op
)
563 m_op (std::move (op
))
567 void assign (struct value
*container
,
568 struct value
*lhs
, struct expression
*exp
,
569 std::vector
<LONGEST
> &indices
,
570 LONGEST low
, LONGEST high
) override
;
572 bool uses_objfile (struct objfile
*objfile
) override
;
574 void dump (ui_file
*stream
, int depth
) override
;
582 /* A component which handles an "others" clause. */
583 class ada_others_component
: public ada_component
587 explicit ada_others_component (operation_up
&&op
)
588 : m_op (std::move (op
))
592 void assign (struct value
*container
,
593 struct value
*lhs
, struct expression
*exp
,
594 std::vector
<LONGEST
> &indices
,
595 LONGEST low
, LONGEST high
) override
;
597 bool uses_objfile (struct objfile
*objfile
) override
;
599 void dump (ui_file
*stream
, int depth
) override
;
606 /* An interface that represents an association that is used in
607 aggregate assignment. */
608 class ada_association
612 /* Like ada_component::assign, but takes an operation as a
613 parameter. The operation is evaluated and then assigned into LHS
614 according to the rules of the concrete implementation. */
615 virtual void assign (struct value
*container
,
617 struct expression
*exp
,
618 std::vector
<LONGEST
> &indices
,
619 LONGEST low
, LONGEST high
,
620 operation_up
&op
) = 0;
622 /* Same as operation::uses_objfile. */
623 virtual bool uses_objfile (struct objfile
*objfile
) = 0;
625 /* Same as operation::dump. */
626 virtual void dump (ui_file
*stream
, int depth
) = 0;
628 virtual ~ada_association () = default;
632 ada_association () = default;
633 DISABLE_COPY_AND_ASSIGN (ada_association
);
636 /* Unique pointer specialization for Ada assignment associations. */
637 typedef std::unique_ptr
<ada_association
> ada_association_up
;
639 /* A component that holds a vector of associations and an operation.
640 The operation is re-evaluated for each choice. */
641 class ada_choices_component
: public ada_component
645 explicit ada_choices_component (operation_up
&&op
)
646 : m_op (std::move (op
))
650 /* Set the vector of associations. This is done separately from the
651 constructor because it was simpler for the implementation of the
653 void set_associations (std::vector
<ada_association_up
> &&assoc
)
655 m_assocs
= std::move (assoc
);
658 void assign (struct value
*container
,
659 struct value
*lhs
, struct expression
*exp
,
660 std::vector
<LONGEST
> &indices
,
661 LONGEST low
, LONGEST high
) override
;
663 bool uses_objfile (struct objfile
*objfile
) override
;
665 void dump (ui_file
*stream
, int depth
) override
;
669 std::vector
<ada_association_up
> m_assocs
;
673 /* An association that uses a discrete range. */
674 class ada_discrete_range_association
: public ada_association
678 ada_discrete_range_association (operation_up
&&low
, operation_up
&&high
)
679 : m_low (std::move (low
)),
680 m_high (std::move (high
))
684 void assign (struct value
*container
,
685 struct value
*lhs
, struct expression
*exp
,
686 std::vector
<LONGEST
> &indices
,
687 LONGEST low
, LONGEST high
,
688 operation_up
&op
) override
;
690 bool uses_objfile (struct objfile
*objfile
) override
;
692 void dump (ui_file
*stream
, int depth
) override
;
700 /* An association that uses a name. The name may be an expression
701 that evaluates to an integer (for arrays), or an Ada string or
702 variable value operation. */
703 class ada_name_association
: public ada_association
707 explicit ada_name_association (operation_up val
)
708 : m_val (std::move (val
))
712 void assign (struct value
*container
,
713 struct value
*lhs
, struct expression
*exp
,
714 std::vector
<LONGEST
> &indices
,
715 LONGEST low
, LONGEST high
,
716 operation_up
&op
) override
;
718 bool uses_objfile (struct objfile
*objfile
) override
;
720 void dump (ui_file
*stream
, int depth
) override
;
727 } /* namespace expr */
729 #endif /* ADA_EXP_H */