1 /* Definitions for Ada expressions
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/>. */
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
<OP_ATR_MIN
, ada_binop_minmax
>;
217 using ada_binop_max_operation
= binop_operation
<OP_ATR_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 symbol
*get_symbol () const
355 { return std::get
<0> (m_storage
); }
357 const block
*get_block () const
358 { return std::get
<1> (m_storage
); }
360 bool resolve (struct expression
*exp
,
362 bool parse_completion
,
363 innermost_block_tracker
*tracker
,
364 struct type
*context_type
) override
;
368 using operation::do_generate_ax
;
371 /* Variant of var_msym_value_operation for Ada. */
372 class ada_var_msym_value_operation
373 : public var_msym_value_operation
377 using var_msym_value_operation::var_msym_value_operation
;
379 value
*evaluate_for_cast (struct type
*expect_type
,
380 struct expression
*exp
,
381 enum noside noside
) override
;
385 using operation::do_generate_ax
;
388 /* Implement the Ada 'val attribute. */
389 class ada_atr_val_operation
390 : public tuple_holding_operation
<struct type
*, operation_up
>
394 using tuple_holding_operation::tuple_holding_operation
;
396 value
*evaluate (struct type
*expect_type
,
397 struct expression
*exp
,
398 enum noside noside
) override
;
400 enum exp_opcode
opcode () const override
401 { return OP_ATR_VAL
; }
404 /* The indirection operator for Ada. */
405 class ada_unop_ind_operation
406 : public unop_ind_base_operation
410 using unop_ind_base_operation::unop_ind_base_operation
;
412 value
*evaluate (struct type
*expect_type
,
413 struct expression
*exp
,
414 enum noside noside
) override
;
417 /* Implement STRUCTOP_STRUCT for Ada. */
418 class ada_structop_operation
419 : public structop_base_operation
423 using structop_base_operation::structop_base_operation
;
425 value
*evaluate (struct type
*expect_type
,
426 struct expression
*exp
,
427 enum noside noside
) override
;
429 enum exp_opcode
opcode () const override
430 { return STRUCTOP_STRUCT
; }
433 /* Function calls for Ada. */
434 class ada_funcall_operation
435 : public tuple_holding_operation
<operation_up
, std::vector
<operation_up
>>,
436 public ada_resolvable
440 using tuple_holding_operation::tuple_holding_operation
;
442 value
*evaluate (struct type
*expect_type
,
443 struct expression
*exp
,
444 enum noside noside
) override
;
446 bool resolve (struct expression
*exp
,
448 bool parse_completion
,
449 innermost_block_tracker
*tracker
,
450 struct type
*context_type
) override
;
452 enum exp_opcode
opcode () const override
453 { return OP_FUNCALL
; }
456 /* An Ada assignment operation. */
457 class ada_assign_operation
458 : public assign_operation
462 using assign_operation::assign_operation
;
464 value
*evaluate (struct type
*expect_type
,
465 struct expression
*exp
,
466 enum noside noside
) override
;
468 enum exp_opcode
opcode () const override
469 { return BINOP_ASSIGN
; }
472 /* This abstract class represents a single component in an Ada
473 aggregate assignment. */
478 /* Assign to LHS, which is part of CONTAINER. EXP is the expression
479 being evaluated. INDICES, LOW, and HIGH indicate which
480 sub-components have already been assigned; INDICES should be
481 updated by this call. */
482 virtual void assign (struct value
*container
,
483 struct value
*lhs
, struct expression
*exp
,
484 std::vector
<LONGEST
> &indices
,
485 LONGEST low
, LONGEST high
) = 0;
487 /* Same as operation::uses_objfile. */
488 virtual bool uses_objfile (struct objfile
*objfile
) = 0;
490 /* Same as operation::dump. */
491 virtual void dump (ui_file
*stream
, int depth
) = 0;
493 virtual ~ada_component () = default;
497 ada_component () = default;
498 DISABLE_COPY_AND_ASSIGN (ada_component
);
501 /* Unique pointer specialization for Ada assignment components. */
502 typedef std::unique_ptr
<ada_component
> ada_component_up
;
504 /* An operation that holds a single component. */
505 class ada_aggregate_operation
506 : public tuple_holding_operation
<ada_component_up
>
510 using tuple_holding_operation::tuple_holding_operation
;
512 /* Assuming that LHS represents an lvalue having a record or array
513 type, evaluate an assignment of this aggregate's value to LHS.
514 CONTAINER is an lvalue containing LHS (possibly LHS itself).
515 Does not modify the inferior's memory, nor does it modify the
516 contents of LHS (unless == CONTAINER). */
518 void assign_aggregate (struct value
*container
,
520 struct expression
*exp
);
522 value
*evaluate (struct type
*expect_type
,
523 struct expression
*exp
,
524 enum noside noside
) override
526 error (_("Aggregates only allowed on the right of an assignment"));
529 enum exp_opcode
opcode () const override
530 { return OP_AGGREGATE
; }
533 /* A component holding a vector of other components to assign. */
534 class ada_aggregate_component
: public ada_component
538 explicit ada_aggregate_component (std::vector
<ada_component_up
> &&components
)
539 : m_components (std::move (components
))
543 void assign (struct value
*container
,
544 struct value
*lhs
, struct expression
*exp
,
545 std::vector
<LONGEST
> &indices
,
546 LONGEST low
, LONGEST high
) override
;
548 bool uses_objfile (struct objfile
*objfile
) override
;
550 void dump (ui_file
*stream
, int depth
) override
;
554 std::vector
<ada_component_up
> m_components
;
557 /* A component that assigns according to a provided index (which is
558 relative to the "low" value). */
559 class ada_positional_component
: public ada_component
563 ada_positional_component (int index
, operation_up
&&op
)
565 m_op (std::move (op
))
569 void assign (struct value
*container
,
570 struct value
*lhs
, struct expression
*exp
,
571 std::vector
<LONGEST
> &indices
,
572 LONGEST low
, LONGEST high
) override
;
574 bool uses_objfile (struct objfile
*objfile
) override
;
576 void dump (ui_file
*stream
, int depth
) override
;
584 /* A component which handles an "others" clause. */
585 class ada_others_component
: public ada_component
589 explicit ada_others_component (operation_up
&&op
)
590 : m_op (std::move (op
))
594 void assign (struct value
*container
,
595 struct value
*lhs
, struct expression
*exp
,
596 std::vector
<LONGEST
> &indices
,
597 LONGEST low
, LONGEST high
) override
;
599 bool uses_objfile (struct objfile
*objfile
) override
;
601 void dump (ui_file
*stream
, int depth
) override
;
608 /* An interface that represents an association that is used in
609 aggregate assignment. */
610 class ada_association
614 /* Like ada_component::assign, but takes an operation as a
615 parameter. The operation is evaluated and then assigned into LHS
616 according to the rules of the concrete implementation. */
617 virtual void assign (struct value
*container
,
619 struct expression
*exp
,
620 std::vector
<LONGEST
> &indices
,
621 LONGEST low
, LONGEST high
,
622 operation_up
&op
) = 0;
624 /* Same as operation::uses_objfile. */
625 virtual bool uses_objfile (struct objfile
*objfile
) = 0;
627 /* Same as operation::dump. */
628 virtual void dump (ui_file
*stream
, int depth
) = 0;
630 virtual ~ada_association () = default;
634 ada_association () = default;
635 DISABLE_COPY_AND_ASSIGN (ada_association
);
638 /* Unique pointer specialization for Ada assignment associations. */
639 typedef std::unique_ptr
<ada_association
> ada_association_up
;
641 /* A component that holds a vector of associations and an operation.
642 The operation is re-evaluated for each choice. */
643 class ada_choices_component
: public ada_component
647 explicit ada_choices_component (operation_up
&&op
)
648 : m_op (std::move (op
))
652 /* Set the vector of associations. This is done separately from the
653 constructor because it was simpler for the implementation of the
655 void set_associations (std::vector
<ada_association_up
> &&assoc
)
657 m_assocs
= std::move (assoc
);
660 void assign (struct value
*container
,
661 struct value
*lhs
, struct expression
*exp
,
662 std::vector
<LONGEST
> &indices
,
663 LONGEST low
, LONGEST high
) override
;
665 bool uses_objfile (struct objfile
*objfile
) override
;
667 void dump (ui_file
*stream
, int depth
) override
;
671 std::vector
<ada_association_up
> m_assocs
;
675 /* An association that uses a discrete range. */
676 class ada_discrete_range_association
: public ada_association
680 ada_discrete_range_association (operation_up
&&low
, operation_up
&&high
)
681 : m_low (std::move (low
)),
682 m_high (std::move (high
))
686 void assign (struct value
*container
,
687 struct value
*lhs
, struct expression
*exp
,
688 std::vector
<LONGEST
> &indices
,
689 LONGEST low
, LONGEST high
,
690 operation_up
&op
) override
;
692 bool uses_objfile (struct objfile
*objfile
) override
;
694 void dump (ui_file
*stream
, int depth
) override
;
702 /* An association that uses a name. The name may be an expression
703 that evaluates to an integer (for arrays), or an Ada string or
704 variable value operation. */
705 class ada_name_association
: public ada_association
709 explicit ada_name_association (operation_up val
)
710 : m_val (std::move (val
))
714 void assign (struct value
*container
,
715 struct value
*lhs
, struct expression
*exp
,
716 std::vector
<LONGEST
> &indices
,
717 LONGEST low
, LONGEST high
,
718 operation_up
&op
) override
;
720 bool uses_objfile (struct objfile
*objfile
) override
;
722 void dump (ui_file
*stream
, int depth
) override
;
729 } /* namespace expr */
731 #endif /* ADA_EXP_H */