Debian Codegraph @

gcc/ada/sem_dim.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ D I M --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2011-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Einfo; use Einfo;
28 with Einfo.Entities; use Einfo.Entities;
29 with Einfo.Utils; use Einfo.Utils;
30 with Errout; use Errout;
31 with Exp_Util; use Exp_Util;
32 with Lib; use Lib;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
36 with Opt; use Opt;
37 with Rtsfind; use Rtsfind;
38 with Sem; use Sem;
39 with Sem_Aux; use Sem_Aux;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Sinfo.Nodes; use Sinfo.Nodes;
45 with Sinfo.Utils; use Sinfo.Utils;
46 with Snames; use Snames;
47 with Stand; use Stand;
48 with Stringt; use Stringt;
49 with Table;
50 with Tbuild; use Tbuild;
51 with Uintp; use Uintp;
52 with Urealp; use Urealp;
53
54 with GNAT.HTable;
55
56 package body Sem_Dim is
57
58 -------------------------
59 -- Rational Arithmetic --
60 -------------------------
61
62 type Whole is new Int;
63 subtype Positive_Whole is Whole range 1 .. Whole'Last;
64
65 type Rational is record
66 Numerator : Whole;
67 Denominator : Positive_Whole;
68 end record;
69
70 Zero : constant Rational := Rational'(Numerator => 0,
71 Denominator => 1);
72
73 No_Rational : constant Rational := Rational'(Numerator => 0,
74 Denominator => 2);
75 -- Used to indicate an expression that cannot be interpreted as a rational
76 -- Returned value of the Create_Rational_From routine when parameter Expr
77 -- is not a static representation of a rational.
78
79 -- Rational constructors
80
81 function "+" (Right : Whole) return Rational;
82 function GCD (Left, Right : Whole) return Int;
83 function Reduce (X : Rational) return Rational;
84
85 -- Unary operator for Rational
86
87 function "-" (Right : Rational) return Rational;
88 function "abs" (Right : Rational) return Rational;
89
90 -- Rational operations for Rationals
91
92 function "+" (Left, Right : Rational) return Rational;
93 function "-" (Left, Right : Rational) return Rational;
94 function "*" (Left, Right : Rational) return Rational;
95 function "/" (Left, Right : Rational) return Rational;
96
97 ------------------
98 -- System Types --
99 ------------------
100
101 Max_Number_Of_Dimensions : constant := 7;
102 -- Maximum number of dimensions in a dimension system
103
104 High_Position_Bound : constant := Max_Number_Of_Dimensions;
105 Invalid_Position : constant := 0;
106 Low_Position_Bound : constant := 1;
107
108 subtype Dimension_Position is
109 Nat range Invalid_Position .. High_Position_Bound;
110
111 type Name_Array is
112 array (Dimension_Position range
113 Low_Position_Bound .. High_Position_Bound) of Name_Id;
114 -- Store the names of all units within a system
115
116 No_Names : constant Name_Array := (others => No_Name);
117
118 type Symbol_Array is
119 array (Dimension_Position range
120 Low_Position_Bound .. High_Position_Bound) of String_Id;
121 -- Store the symbols of all units within a system
122
123 No_Symbols : constant Symbol_Array := (others => No_String);
124
125 -- The following record should be documented field by field
126
127 type System_Type is record
128 Type_Decl : Node_Id;
129 Unit_Names : Name_Array;
130 Unit_Symbols : Symbol_Array;
131 Dim_Symbols : Symbol_Array;
132 Count : Dimension_Position;
133 end record;
134
135 Null_System : constant System_Type :=
136 (Empty, No_Names, No_Symbols, No_Symbols, Invalid_Position);
137
138 subtype System_Id is Nat;
139
140 -- The following table maps types to systems
141
142 package System_Table is new Table.Table (
143 Table_Component_Type => System_Type,
144 Table_Index_Type => System_Id,
145 Table_Low_Bound => 1,
146 Table_Initial => 5,
147 Table_Increment => 5,
148 Table_Name => "System_Table");
149
150 --------------------
151 -- Dimension Type --
152 --------------------
153
154 type Dimension_Type is
155 array (Dimension_Position range
156 Low_Position_Bound .. High_Position_Bound) of Rational;
157
158 Null_Dimension : constant Dimension_Type := (others => Zero);
159
160 type Dimension_Table_Range is range 0 .. 510;
161 function Dimension_Table_Hash (Key : Node_Id) return Dimension_Table_Range;
162
163 -- The following table associates nodes with dimensions
164
165 package Dimension_Table is new
166 GNAT.HTable.Simple_HTable
167 (Header_Num => Dimension_Table_Range,
168 Element => Dimension_Type,
169 No_Element => Null_Dimension,
170 Key => Node_Id,
171 Hash => Dimension_Table_Hash,
172 Equal => "=");
173
174 ------------------
175 -- Symbol Types --
176 ------------------
177
178 type Symbol_Table_Range is range 0 .. 510;
179 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range;
180
181 -- Each subtype with a dimension has a symbolic representation of the
182 -- related unit. This table establishes a relation between the subtype
183 -- and the symbol.
184
185 package Symbol_Table is new
186 GNAT.HTable.Simple_HTable
187 (Header_Num => Symbol_Table_Range,
188 Element => String_Id,
189 No_Element => No_String,
190 Key => Entity_Id,
191 Hash => Symbol_Table_Hash,
192 Equal => "=");
193
194 -- The following array enumerates all contexts which may contain or
195 -- produce a dimension.
196
197 OK_For_Dimension : constant array (Node_Kind) of Boolean :=
198 (N_Attribute_Reference => True,
199 N_Case_Expression => True,
200 N_Expanded_Name => True,
201 N_Explicit_Dereference => True,
202 N_Defining_Identifier => True,
203 N_Function_Call => True,
204 N_Identifier => True,
205 N_If_Expression => True,
206 N_Indexed_Component => True,
207 N_Integer_Literal => True,
208 N_Op_Abs => True,
209 N_Op_Add => True,
210 N_Op_Divide => True,
211 N_Op_Expon => True,
212 N_Op_Minus => True,
213 N_Op_Mod => True,
214 N_Op_Multiply => True,
215 N_Op_Plus => True,
216 N_Op_Rem => True,
217 N_Op_Subtract => True,
218 N_Qualified_Expression => True,
219 N_Real_Literal => True,
220 N_Selected_Component => True,
221 N_Slice => True,
222 N_Target_Name => True,
223 N_Type_Conversion => True,
224 N_Unchecked_Type_Conversion => True,
225
226 others => False);
227
228 -----------------------
229 -- Local Subprograms --
230 -----------------------
231
232 procedure Analyze_Dimension_Assignment_Statement (N : Node_Id);
233 -- Subroutine of Analyze_Dimension for assignment statement. Check that the
234 -- dimensions of the left-hand side and the right-hand side of N match.
235
236 procedure Analyze_Dimension_Binary_Op (N : Node_Id);
237 -- Subroutine of Analyze_Dimension for binary operators. Check the
238 -- dimensions of the right and the left operand permit the operation.
239 -- Then, evaluate the resulting dimensions for each binary operator.
240
241 procedure Analyze_Dimension_Component_Declaration (N : Node_Id);
242 -- Subroutine of Analyze_Dimension for component declaration. Check that
243 -- the dimensions of the type of N and of the expression match.
244
245 procedure Analyze_Dimension_Extended_Return_Statement (N : Node_Id);
246 -- Subroutine of Analyze_Dimension for extended return statement. Check
247 -- that the dimensions of the returned type and of the returned object
248 -- match.
249
250 procedure Analyze_Dimension_Has_Etype (N : Node_Id);
251 -- Subroutine of Analyze_Dimension for a subset of N_Has_Etype denoted by
252 -- the list below:
253 -- N_Attribute_Reference
254 -- N_Identifier
255 -- N_Indexed_Component
256 -- N_Qualified_Expression
257 -- N_Selected_Component
258 -- N_Slice
259 -- N_Type_Conversion
260 -- N_Unchecked_Type_Conversion
261
262 procedure Analyze_Dimension_Case_Expression (N : Node_Id);
263 -- Verify that all alternatives have the same dimension
264
265 procedure Analyze_Dimension_If_Expression (N : Node_Id);
266 -- Verify that all alternatives have the same dimension
267
268 procedure Analyze_Dimension_Number_Declaration (N : Node_Id);
269 -- Procedure to analyze dimension of expression in a number declaration.
270 -- This allows a named number to have nontrivial dimensions, while by
271 -- default a named number is dimensionless.
272
273 procedure Analyze_Dimension_Object_Declaration (N : Node_Id);
274 -- Subroutine of Analyze_Dimension for object declaration. Check that
275 -- the dimensions of the object type and the dimensions of the expression
276 -- (if expression is present) match. Note that when the expression is
277 -- a literal, no error is returned. This special case allows object
278 -- declaration such as: m : constant Length := 1.0;
279
280 procedure Analyze_Dimension_Object_Renaming_Declaration (N : Node_Id);
281 -- Subroutine of Analyze_Dimension for object renaming declaration. Check
282 -- the dimensions of the type and of the renamed object name of N match.
283
284 procedure Analyze_Dimension_Simple_Return_Statement (N : Node_Id);
285 -- Subroutine of Analyze_Dimension for simple return statement
286 -- Check that the dimensions of the returned type and of the returned
287 -- expression match.
288
289 procedure Analyze_Dimension_Subtype_Declaration (N : Node_Id);
290 -- Subroutine of Analyze_Dimension for subtype declaration. Propagate the
291 -- dimensions from the parent type to the identifier of N. Note that if
292 -- both the identifier and the parent type of N are not dimensionless,
293 -- return an error.
294
295 procedure Analyze_Dimension_Type_Conversion (N : Node_Id);
296 -- Type conversions handle conversions between literals and dimensioned
297 -- types, from dimensioned types to their base type, and between different
298 -- dimensioned systems. Dimensions of the conversion are obtained either
299 -- from those of the expression, or from the target type, and dimensional
300 -- consistency must be checked when converting between values belonging
301 -- to different dimensioned systems.
302
303 procedure Analyze_Dimension_Unary_Op (N : Node_Id);
304 -- Subroutine of Analyze_Dimension for unary operators. For Plus, Minus and
305 -- Abs operators, propagate the dimensions from the operand to N.
306
307 function Create_Rational_From
308 (Expr : Node_Id;
309 Complain : Boolean) return Rational;
310 -- Given an arbitrary expression Expr, return a valid rational if Expr can
311 -- be interpreted as a rational. Otherwise return No_Rational and also an
312 -- error message if Complain is set to True.
313
314 function Dimensions_Of (N : Node_Id) return Dimension_Type;
315 -- Return the dimension vector of node N
316
317 function Dimensions_Msg_Of
318 (N : Node_Id;
319 Description_Needed : Boolean := False) return String;
320 -- Given a node N, return the dimension symbols of N, preceded by "has
321 -- dimension" if Description_Needed. If N is dimensionless, return "'[']",
322 -- or "is dimensionless" if Description_Needed.
323
324 function Dimension_System_Root (T : Entity_Id) return Entity_Id;
325 -- Given a type that has dimension information, return the type that is the
326 -- root of its dimension system, e.g. Mks_Type. If T is not a dimensioned
327 -- type, i.e. a standard numeric type, return Empty.
328
329 procedure Dim_Warning_For_Numeric_Literal (N : Node_Id; Typ : Entity_Id);
330 -- Issue a warning on the given numeric literal N to indicate that the
331 -- compiler made the assumption that the literal is not dimensionless
332 -- but has the dimension of Typ.
333
334 procedure Eval_Op_Expon_With_Rational_Exponent
335 (N : Node_Id;
336 Exponent_Value : Rational);
337 -- Evaluate the exponent it is a rational and the operand has a dimension
338
339 function Exists (Dim : Dimension_Type) return Boolean;
340 -- Returns True iff Dim does not denote the null dimension
341
342 function Exists (Str : String_Id) return Boolean;
343 -- Returns True iff Str does not denote No_String
344
345 function Exists (Sys : System_Type) return Boolean;
346 -- Returns True iff Sys does not denote the null system
347
348 function From_Dim_To_Str_Of_Dim_Symbols
349 (Dims : Dimension_Type;
350 System : System_Type;
351 In_Error_Msg : Boolean := False) return String_Id;
352 -- Given a dimension vector and a dimension system, return the proper
353 -- string of dimension symbols. If In_Error_Msg is True (i.e. the String_Id
354 -- will be used to issue an error message) then this routine has a special
355 -- handling for the insertion characters * or [ which must be preceded by
356 -- a quote ' to be placed literally into the message.
357
358 function From_Dim_To_Str_Of_Unit_Symbols
359 (Dims : Dimension_Type;
360 System : System_Type) return String_Id;
361 -- Given a dimension vector and a dimension system, return the proper
362 -- string of unit symbols.
363
364 function Is_Dim_IO_Package_Entity (E : Entity_Id) return Boolean;
365 -- Return True if E is the package entity of System.Dim.Float_IO or
366 -- System.Dim.Integer_IO.
367
368 function Is_Invalid (Position : Dimension_Position) return Boolean;
369 -- Return True if Pos denotes the invalid position
370
371 procedure Move_Dimensions (From : Node_Id; To : Node_Id);
372 -- Copy dimension vector of From to To and delete dimension vector of From
373
374 procedure Remove_Dimensions (N : Node_Id);
375 -- Remove the dimension vector of node N
376
377 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type);
378 -- Associate a dimension vector with a node
379
380 procedure Set_Symbol (E : Entity_Id; Val : String_Id);
381 -- Associate a symbol representation of a dimension vector with a subtype
382
383 function Symbol_Of (E : Entity_Id) return String_Id;
384 -- E denotes a subtype with a dimension. Return the symbol representation
385 -- of the dimension vector.
386
387 function System_Of (E : Entity_Id) return System_Type;
388 -- E denotes a type, return associated system of the type if it has one
389
390 ---------
391 -- "+" --
392 ---------
393
394 function "+" (Right : Whole) return Rational is
395 begin
396 return Rational'(Numerator => Right, Denominator => 1);
397 end "+";
398
399 function "+" (Left, Right : Rational) return Rational is
400 R : constant Rational :=
401 Rational'(Numerator => Left.Numerator * Right.Denominator +
402 Left.Denominator * Right.Numerator,
403 Denominator => Left.Denominator * Right.Denominator);
404 begin
405 return Reduce (R);
406 end "+";
407
408 ---------
409 -- "-" --
410 ---------
411
412 function "-" (Right : Rational) return Rational is
413 begin
414 return Rational'(Numerator => -Right.Numerator,
415 Denominator => Right.Denominator);
416 end "-";
417
418 function "-" (Left, Right : Rational) return Rational is
419 R : constant Rational :=
420 Rational'(Numerator => Left.Numerator * Right.Denominator -
421 Left.Denominator * Right.Numerator,
422 Denominator => Left.Denominator * Right.Denominator);
423
424 begin
425 return Reduce (R);
426 end "-";
427
428 ---------
429 -- "*" --
430 ---------
431
432 function "*" (Left, Right : Rational) return Rational is
433 R : constant Rational :=
434 Rational'(Numerator => Left.Numerator * Right.Numerator,
435 Denominator => Left.Denominator * Right.Denominator);
436 begin
437 return Reduce (R);
438 end "*";
439
440 ---------
441 -- "/" --
442 ---------
443
444 function "/" (Left, Right : Rational) return Rational is
445 R : constant Rational := abs Right;
446 L : Rational := Left;
447
448 begin
449 if Right.Numerator < 0 then
450 L.Numerator := Whole (-Integer (L.Numerator));
451 end if;
452
453 return Reduce (Rational'(Numerator => L.Numerator * R.Denominator,
454 Denominator => L.Denominator * R.Numerator));
455 end "/";
456
457 -----------
458 -- "abs" --
459 -----------
460
461 function "abs" (Right : Rational) return Rational is
462 begin
463 return Rational'(Numerator => abs Right.Numerator,
464 Denominator => Right.Denominator);
465 end "abs";
466
467 ------------------------------
468 -- Analyze_Aspect_Dimension --
469 ------------------------------
470
471 -- with Dimension =>
472 -- ([Symbol =>] SYMBOL, DIMENSION_VALUE {, DIMENSION_Value})
473 --
474 -- SYMBOL ::= STRING_LITERAL | CHARACTER_LITERAL
475
476 -- DIMENSION_VALUE ::=
477 -- RATIONAL
478 -- | others => RATIONAL
479 -- | DISCRETE_CHOICE_LIST => RATIONAL
480
481 -- RATIONAL ::= [-] NUMERIC_LITERAL [/ NUMERIC_LITERAL]
482
483 -- Note that when the dimensioned type is an integer type, then any
484 -- dimension value must be an integer literal.
485
486 procedure Analyze_Aspect_Dimension
487 (N : Node_Id;
488 Id : Entity_Id;
489 Aggr : Node_Id)
490 is
491 Def_Id : constant Entity_Id := Defining_Identifier (N);
492
493 Processed : array (Dimension_Type'Range) of Boolean := (others => False);
494 -- This array is used when processing ranges or Others_Choice as part of
495 -- the dimension aggregate.
496
497 Dimensions : Dimension_Type := Null_Dimension;
498
499 procedure Extract_Power
500 (Expr : Node_Id;
501 Position : Dimension_Position);
502 -- Given an expression with denotes a rational number, read the number
503 -- and associate it with Position in Dimensions.
504
505 function Position_In_System
506 (Id : Node_Id;
507 System : System_Type) return Dimension_Position;
508 -- Given an identifier which denotes a dimension, return the position of
509 -- that dimension within System.
510
511 -------------------
512 -- Extract_Power --
513 -------------------
514
515 procedure Extract_Power
516 (Expr : Node_Id;
517 Position : Dimension_Position)
518 is
519 begin
520 Dimensions (Position) := Create_Rational_From (Expr, True);
521 Processed (Position) := True;
522
523 -- If the dimensioned root type is an integer type, it is not
524 -- particularly useful, and fractional dimensions do not make
525 -- much sense for such types, so previously we used to reject
526 -- dimensions of integer types that were not integer literals.
527 -- However, the manipulation of dimensions does not depend on
528 -- the kind of root type, so we can accept this usage for rare
529 -- cases where dimensions are specified for integer values.
530
531 end Extract_Power;
532
533 ------------------------
534 -- Position_In_System --
535 ------------------------
536
537 function Position_In_System
538 (Id : Node_Id;
539 System : System_Type) return Dimension_Position
540 is
541 Dimension_Name : constant Name_Id := Chars (Id);
542
543 begin
544 for Position in System.Unit_Names'Range loop
545 if Dimension_Name = System.Unit_Names (Position) then
546 return Position;
547 end if;
548 end loop;
549
550 return Invalid_Position;
551 end Position_In_System;
552
553 -- Local variables
554
555 Assoc : Node_Id;
556 Choice : Node_Id;
557 Expr : Node_Id;
558 Num_Choices : Nat := 0;
559 Num_Dimensions : Nat := 0;
560 Others_Seen : Boolean := False;
561 Position : Nat := 0;
562 Sub_Ind : Node_Id;
563 Symbol : String_Id := No_String;
564 Symbol_Expr : Node_Id;
565 System : System_Type;
566 Typ : Entity_Id;
567
568 Errors_Count : Nat;
569 -- Errors_Count is a count of errors detected by the compiler so far
570 -- just before the extraction of symbol, names and values in the
571 -- aggregate (Step 2).
572 --
573 -- At the end of the analysis, there is a check to verify that this
574 -- count equals to Serious_Errors_Detected i.e. no erros have been
575 -- encountered during the process. Otherwise the Dimension_Table is
576 -- not filled.
577
578 -- Start of processing for Analyze_Aspect_Dimension
579
580 begin
581 -- STEP 1: Legality of aspect
582
583 if Nkind (N) /= N_Subtype_Declaration then
584 Error_Msg_NE ("aspect& must apply to subtype declaration", N, Id);
585 return;
586 end if;
587
588 Sub_Ind := Subtype_Indication (N);
589 Typ := Etype (Sub_Ind);
590 System := System_Of (Typ);
591
592 if Nkind (Sub_Ind) = N_Subtype_Indication then
593 Error_Msg_NE
594 ("constraint not allowed with aspect&", Constraint (Sub_Ind), Id);
595 return;
596 end if;
597
598 -- The dimension declarations are useless if the parent type does not
599 -- declare a valid system.
600
601 if not Exists (System) then
602 Error_Msg_NE
603 ("parent type of& lacks dimension system", Sub_Ind, Def_Id);
604 return;
605 end if;
606
607 if Nkind (Aggr) /= N_Aggregate then
608 Error_Msg_N ("aggregate expected", Aggr);
609 return;
610 end if;
611
612 -- STEP 2: Symbol, Names and values extraction
613
614 -- Get the number of errors detected by the compiler so far
615
616 Errors_Count := Serious_Errors_Detected;
617
618 -- STEP 2a: Symbol extraction
619
620 -- The first entry in the aggregate may be the symbolic representation
621 -- of the quantity.
622
623 -- Positional symbol argument
624
625 Symbol_Expr := First (Expressions (Aggr));
626
627 -- Named symbol argument
628
629 if No (Symbol_Expr)
630 or else Nkind (Symbol_Expr) not in
631 N_Character_Literal | N_String_Literal
632 then
633 Symbol_Expr := Empty;
634
635 -- Component associations present
636
637 if Present (Component_Associations (Aggr)) then
638 Assoc := First (Component_Associations (Aggr));
639 Choice := First (Choices (Assoc));
640
641 if No (Next (Choice)) and then Nkind (Choice) = N_Identifier then
642
643 -- Symbol component association is present
644
645 if Chars (Choice) = Name_Symbol then
646 Num_Choices := Num_Choices + 1;
647 Symbol_Expr := Expression (Assoc);
648
649 -- Verify symbol expression is a string or a character
650
651 if Nkind (Symbol_Expr) not in
652 N_Character_Literal | N_String_Literal
653 then
654 Symbol_Expr := Empty;
655 Error_Msg_N
656 ("symbol expression must be character or string",
657 Symbol_Expr);
658 end if;
659
660 -- Special error if no Symbol choice but expression is string
661 -- or character.
662
663 elsif Nkind (Expression (Assoc)) in
664 N_Character_Literal | N_String_Literal
665 then
666 Num_Choices := Num_Choices + 1;
667 Error_Msg_N
668 ("optional component Symbol expected, found&", Choice);
669 end if;
670 end if;
671 end if;
672 end if;
673
674 -- STEP 2b: Names and values extraction
675
676 -- Positional elements
677
678 Expr := First (Expressions (Aggr));
679
680 -- Skip the symbol expression when present
681
682 if Present (Symbol_Expr) and then Num_Choices = 0 then
683 Next (Expr);
684 end if;
685
686 Position := Low_Position_Bound;
687 while Present (Expr) loop
688 if Position > High_Position_Bound then
689 Error_Msg_N
690 ("type& has more dimensions than system allows", Def_Id);
691 exit;
692 end if;
693
694 Extract_Power (Expr, Position);
695
696 Position := Position + 1;
697 Num_Dimensions := Num_Dimensions + 1;
698
699 Next (Expr);
700 end loop;
701
702 -- Named elements
703
704 Assoc := First (Component_Associations (Aggr));
705
706 -- Skip the symbol association when present
707
708 if Num_Choices = 1 then
709 Next (Assoc);
710 end if;
711
712 while Present (Assoc) loop
713 Expr := Expression (Assoc);
714
715 Choice := First (Choices (Assoc));
716 while Present (Choice) loop
717
718 -- Identifier case: NAME => EXPRESSION
719
720 if Nkind (Choice) = N_Identifier then
721 Position := Position_In_System (Choice, System);
722
723 if Is_Invalid (Position) then
724 Error_Msg_N ("dimension name& not part of system", Choice);
725 else
726 Extract_Power (Expr, Position);
727 end if;
728
729 -- Range case: NAME .. NAME => EXPRESSION
730
731 elsif Nkind (Choice) = N_Range then
732 declare
733 Low : constant Node_Id := Low_Bound (Choice);
734 High : constant Node_Id := High_Bound (Choice);
735 Low_Pos : Dimension_Position;
736 High_Pos : Dimension_Position;
737
738 begin
739 if Nkind (Low) /= N_Identifier then
740 Error_Msg_N ("bound must denote a dimension name", Low);
741
742 elsif Nkind (High) /= N_Identifier then
743 Error_Msg_N ("bound must denote a dimension name", High);
744
745 else
746 Low_Pos := Position_In_System (Low, System);
747 High_Pos := Position_In_System (High, System);
748
749 if Is_Invalid (Low_Pos) then
750 Error_Msg_N ("dimension name& not part of system",
751 Low);
752
753 elsif Is_Invalid (High_Pos) then
754 Error_Msg_N ("dimension name& not part of system",
755 High);
756
757 elsif Low_Pos > High_Pos then
758 Error_Msg_N ("expected low to high range", Choice);
759
760 else
761 for Position in Low_Pos .. High_Pos loop
762 Extract_Power (Expr, Position);
763 end loop;
764 end if;
765 end if;
766 end;
767
768 -- Others case: OTHERS => EXPRESSION
769
770 elsif Nkind (Choice) = N_Others_Choice then
771 if Present (Next (Choice)) or else Present (Prev (Choice)) then
772 Error_Msg_N
773 ("OTHERS must appear alone in a choice list", Choice);
774
775 elsif Present (Next (Assoc)) then
776 Error_Msg_N
777 ("OTHERS must appear last in an aggregate", Choice);
778
779 elsif Others_Seen then
780 Error_Msg_N ("multiple OTHERS not allowed", Choice);
781
782 else
783 -- Fill the non-processed dimensions with the default value
784 -- supplied by others.
785
786 for Position in Processed'Range loop
787 if not Processed (Position) then
788 Extract_Power (Expr, Position);
789 end if;
790 end loop;
791 end if;
792
793 Others_Seen := True;
794
795 -- All other cases are illegal declarations of dimension names
796
797 else
798 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
799 end if;
800
801 Num_Choices := Num_Choices + 1;
802 Next (Choice);
803 end loop;
804
805 Num_Dimensions := Num_Dimensions + 1;
806 Next (Assoc);
807 end loop;
808
809 -- STEP 3: Consistency of system and dimensions
810
811 if Present (First (Expressions (Aggr)))
812 and then (First (Expressions (Aggr)) /= Symbol_Expr
813 or else Present (Next (Symbol_Expr)))
814 and then (Num_Choices > 1
815 or else (Num_Choices = 1 and then not Others_Seen))
816 then
817 Error_Msg_N
818 ("named associations cannot follow positional associations", Aggr);
819 end if;
820
821 if Num_Dimensions > System.Count then
822 Error_Msg_N ("type& has more dimensions than system allows", Def_Id);
823
824 elsif Num_Dimensions < System.Count and then not Others_Seen then
825 Error_Msg_N ("type& has less dimensions than system allows", Def_Id);
826 end if;
827
828 -- STEP 4: Dimension symbol extraction
829
830 if Present (Symbol_Expr) then
831 if Nkind (Symbol_Expr) = N_Character_Literal then
832 Start_String;
833 Store_String_Char (UI_To_CC (Char_Literal_Value (Symbol_Expr)));
834 Symbol := End_String;
835
836 else
837 Symbol := Strval (Symbol_Expr);
838 end if;
839
840 if String_Length (Symbol) = 0 then
841 Error_Msg_N ("empty string not allowed here", Symbol_Expr);
842 end if;
843 end if;
844
845 -- STEP 5: Storage of extracted values
846
847 -- Check that no errors have been detected during the analysis
848
849 if Errors_Count = Serious_Errors_Detected then
850
851 -- Check for useless declaration
852
853 if Symbol = No_String and then not Exists (Dimensions) then
854 Error_Msg_N ("useless dimension declaration", Aggr);
855 end if;
856
857 if Symbol /= No_String then
858 Set_Symbol (Def_Id, Symbol);
859 end if;
860
861 if Exists (Dimensions) then
862 Set_Dimensions (Def_Id, Dimensions);
863 end if;
864 end if;
865 end Analyze_Aspect_Dimension;
866
867 -------------------------------------
868 -- Analyze_Aspect_Dimension_System --
869 -------------------------------------
870
871 -- with Dimension_System => (DIMENSION {, DIMENSION});
872
873 -- DIMENSION ::= (
874 -- [Unit_Name =>] IDENTIFIER,
875 -- [Unit_Symbol =>] SYMBOL,
876 -- [Dim_Symbol =>] SYMBOL)
877
878 procedure Analyze_Aspect_Dimension_System
879 (N : Node_Id;
880 Id : Entity_Id;
881 Aggr : Node_Id)
882 is
883 function Is_Derived_Numeric_Type (N : Node_Id) return Boolean;
884 -- Determine whether type declaration N denotes a numeric derived type
885
886 -------------------------------
887 -- Is_Derived_Numeric_Type --
888 -------------------------------
889
890 function Is_Derived_Numeric_Type (N : Node_Id) return Boolean is
891 begin
892 return
893 Nkind (N) = N_Full_Type_Declaration
894 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
895 and then Is_Numeric_Type
896 (Entity (Subtype_Indication (Type_Definition (N))));
897 end Is_Derived_Numeric_Type;
898
899 -- Local variables
900
901 Assoc : Node_Id;
902 Choice : Node_Id;
903 Dim_Aggr : Node_Id;
904 Dim_Symbol : Node_Id;
905 Dim_Symbols : Symbol_Array := No_Symbols;
906 Dim_System : System_Type := Null_System;
907 Position : Dimension_Position := Invalid_Position;
908 Unit_Name : Node_Id;
909 Unit_Names : Name_Array := No_Names;
910 Unit_Symbol : Node_Id;
911 Unit_Symbols : Symbol_Array := No_Symbols;
912
913 Errors_Count : Nat;
914 -- Errors_Count is a count of errors detected by the compiler so far
915 -- just before the extraction of names and symbols in the aggregate
916 -- (Step 3).
917 --
918 -- At the end of the analysis, there is a check to verify that this
919 -- count equals Serious_Errors_Detected i.e. no errors have been
920 -- encountered during the process. Otherwise the System_Table is
921 -- not filled.
922
923 -- Start of processing for Analyze_Aspect_Dimension_System
924
925 begin
926 -- STEP 1: Legality of aspect
927
928 if not Is_Derived_Numeric_Type (N) then
929 Error_Msg_NE
930 ("aspect& must apply to numeric derived type declaration", N, Id);
931 return;
932 end if;
933
934 if Nkind (Aggr) /= N_Aggregate then
935 Error_Msg_N ("aggregate expected", Aggr);
936 return;
937 end if;
938
939 -- STEP 2: Structural verification of the dimension aggregate
940
941 if Present (Component_Associations (Aggr)) then
942 Error_Msg_N ("expected positional aggregate", Aggr);
943 return;
944 end if;
945
946 -- STEP 3: Name and Symbol extraction
947
948 Dim_Aggr := First (Expressions (Aggr));
949 Errors_Count := Serious_Errors_Detected;
950 while Present (Dim_Aggr) loop
951 if Position = High_Position_Bound then
952 Error_Msg_N ("too many dimensions in system", Aggr);
953 exit;
954 end if;
955
956 Position := Position + 1;
957
958 if Nkind (Dim_Aggr) /= N_Aggregate then
959 Error_Msg_N ("aggregate expected", Dim_Aggr);
960
961 else
962 if Present (Component_Associations (Dim_Aggr))
963 and then Present (Expressions (Dim_Aggr))
964 then
965 Error_Msg_N
966 ("mixed positional/named aggregate not allowed here",
967 Dim_Aggr);
968
969 -- Verify each dimension aggregate has three arguments
970
971 elsif List_Length (Component_Associations (Dim_Aggr)) /= 3
972 and then List_Length (Expressions (Dim_Aggr)) /= 3
973 then
974 Error_Msg_N
975 ("three components expected in aggregate", Dim_Aggr);
976
977 else
978 -- Named dimension aggregate
979
980 if Present (Component_Associations (Dim_Aggr)) then
981
982 -- Check first argument denotes the unit name
983
984 Assoc := First (Component_Associations (Dim_Aggr));
985 Choice := First (Choices (Assoc));
986 Unit_Name := Expression (Assoc);
987
988 if Present (Next (Choice))
989 or else Nkind (Choice) /= N_Identifier
990 then
991 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
992
993 elsif Chars (Choice) /= Name_Unit_Name then
994 Error_Msg_N ("expected Unit_Name, found&", Choice);
995 end if;
996
997 -- Check the second argument denotes the unit symbol
998
999 Next (Assoc);
1000 Choice := First (Choices (Assoc));
1001 Unit_Symbol := Expression (Assoc);
1002
1003 if Present (Next (Choice))
1004 or else Nkind (Choice) /= N_Identifier
1005 then
1006 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
1007
1008 elsif Chars (Choice) /= Name_Unit_Symbol then
1009 Error_Msg_N ("expected Unit_Symbol, found&", Choice);
1010 end if;
1011
1012 -- Check the third argument denotes the dimension symbol
1013
1014 Next (Assoc);
1015 Choice := First (Choices (Assoc));
1016 Dim_Symbol := Expression (Assoc);
1017
1018 if Present (Next (Choice))
1019 or else Nkind (Choice) /= N_Identifier
1020 then
1021 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
1022 elsif Chars (Choice) /= Name_Dim_Symbol then
1023 Error_Msg_N ("expected Dim_Symbol, found&", Choice);
1024 end if;
1025
1026 -- Positional dimension aggregate
1027
1028 else
1029 Unit_Name := First (Expressions (Dim_Aggr));
1030 Unit_Symbol := Next (Unit_Name);
1031 Dim_Symbol := Next (Unit_Symbol);
1032 end if;
1033
1034 -- Check the first argument for each dimension aggregate is
1035 -- a name.
1036
1037 if Nkind (Unit_Name) = N_Identifier then
1038 Unit_Names (Position) := Chars (Unit_Name);
1039 else
1040 Error_Msg_N ("expected unit name", Unit_Name);
1041 end if;
1042
1043 -- Check the second argument for each dimension aggregate is
1044 -- a string or a character.
1045
1046 if Nkind (Unit_Symbol) not in
1047 N_String_Literal | N_Character_Literal
1048 then
1049 Error_Msg_N
1050 ("expected unit symbol (string or character)",
1051 Unit_Symbol);
1052
1053 else
1054 -- String case
1055
1056 if Nkind (Unit_Symbol) = N_String_Literal then
1057 Unit_Symbols (Position) := Strval (Unit_Symbol);
1058
1059 -- Character case
1060
1061 else
1062 Start_String;
1063 Store_String_Char
1064 (UI_To_CC (Char_Literal_Value (Unit_Symbol)));
1065 Unit_Symbols (Position) := End_String;
1066 end if;
1067
1068 -- Verify that the string is not empty
1069
1070 if String_Length (Unit_Symbols (Position)) = 0 then
1071 Error_Msg_N
1072 ("empty string not allowed here", Unit_Symbol);
1073 end if;
1074 end if;
1075
1076 -- Check the third argument for each dimension aggregate is
1077 -- a string or a character.
1078
1079 if Nkind (Dim_Symbol) not in
1080 N_String_Literal | N_Character_Literal
1081 then
1082 Error_Msg_N
1083 ("expected dimension symbol (string or character)",
1084 Dim_Symbol);
1085
1086 else
1087 -- String case
1088
1089 if Nkind (Dim_Symbol) = N_String_Literal then
1090 Dim_Symbols (Position) := Strval (Dim_Symbol);
1091
1092 -- Character case
1093
1094 else
1095 Start_String;
1096 Store_String_Char
1097 (UI_To_CC (Char_Literal_Value (Dim_Symbol)));
1098 Dim_Symbols (Position) := End_String;
1099 end if;
1100
1101 -- Verify that the string is not empty
1102
1103 if String_Length (Dim_Symbols (Position)) = 0 then
1104 Error_Msg_N ("empty string not allowed here", Dim_Symbol);
1105 end if;
1106 end if;
1107 end if;
1108 end if;
1109
1110 Next (Dim_Aggr);
1111 end loop;
1112
1113 -- STEP 4: Storage of extracted values
1114
1115 -- Check that no errors have been detected during the analysis
1116
1117 if Errors_Count = Serious_Errors_Detected then
1118 Dim_System.Type_Decl := N;
1119 Dim_System.Unit_Names := Unit_Names;
1120 Dim_System.Unit_Symbols := Unit_Symbols;
1121 Dim_System.Dim_Symbols := Dim_Symbols;
1122 Dim_System.Count := Position;
1123 System_Table.Append (Dim_System);
1124 end if;
1125 end Analyze_Aspect_Dimension_System;
1126
1127 -----------------------
1128 -- Analyze_Dimension --
1129 -----------------------
1130
1131 -- This dispatch routine propagates dimensions for each node
1132
1133 procedure Analyze_Dimension (N : Node_Id) is
1134 begin
1135 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1136 -- dimensions for nodes that don't come from source, except for subtype
1137 -- declarations where the dimensions are inherited from the base type,
1138 -- for explicit dereferences generated when expanding iterators, and
1139 -- for object declarations generated for inlining.
1140
1141 if Ada_Version < Ada_2012 then
1142 return;
1143
1144 -- Inlined bodies have already been checked for dimensionality
1145
1146 elsif In_Inlined_Body then
1147 return;
1148
1149 elsif not Comes_From_Source (N) then
1150 if Nkind (N) not in N_Explicit_Dereference
1151 | N_Identifier
1152 | N_Object_Declaration
1153 | N_Subtype_Declaration
1154 then
1155 return;
1156 end if;
1157 end if;
1158
1159 case Nkind (N) is
1160 when N_Assignment_Statement =>
1161 Analyze_Dimension_Assignment_Statement (N);
1162
1163 when N_Binary_Op =>
1164 Analyze_Dimension_Binary_Op (N);
1165
1166 when N_Case_Expression =>
1167 Analyze_Dimension_Case_Expression (N);
1168
1169 when N_Component_Declaration =>
1170 Analyze_Dimension_Component_Declaration (N);
1171
1172 when N_Extended_Return_Statement =>
1173 Analyze_Dimension_Extended_Return_Statement (N);
1174
1175 when N_Attribute_Reference
1176 | N_Expanded_Name
1177 | N_Explicit_Dereference
1178 | N_Function_Call
1179 | N_Indexed_Component
1180 | N_Qualified_Expression
1181 | N_Selected_Component
1182 | N_Slice
1183 | N_Target_Name
1184 | N_Unchecked_Type_Conversion
1185 =>
1186 Analyze_Dimension_Has_Etype (N);
1187
1188 -- In the presence of a repaired syntax error, an identifier may be
1189 -- introduced without a usable type.
1190
1191 when N_Identifier =>
1192 if Present (Etype (N)) then
1193 Analyze_Dimension_Has_Etype (N);
1194 end if;
1195
1196 when N_If_Expression =>
1197 Analyze_Dimension_If_Expression (N);
1198
1199 when N_Number_Declaration =>
1200 Analyze_Dimension_Number_Declaration (N);
1201
1202 when N_Object_Declaration =>
1203 Analyze_Dimension_Object_Declaration (N);
1204
1205 when N_Object_Renaming_Declaration =>
1206 Analyze_Dimension_Object_Renaming_Declaration (N);
1207
1208 when N_Simple_Return_Statement =>
1209 if not Comes_From_Extended_Return_Statement (N) then
1210 Analyze_Dimension_Simple_Return_Statement (N);
1211 end if;
1212
1213 when N_Subtype_Declaration =>
1214 Analyze_Dimension_Subtype_Declaration (N);
1215
1216 when N_Type_Conversion =>
1217 Analyze_Dimension_Type_Conversion (N);
1218
1219 when N_Unary_Op =>
1220 Analyze_Dimension_Unary_Op (N);
1221
1222 when others =>
1223 null;
1224 end case;
1225 end Analyze_Dimension;
1226
1227 ---------------------------------------
1228 -- Analyze_Dimension_Array_Aggregate --
1229 ---------------------------------------
1230
1231 procedure Analyze_Dimension_Array_Aggregate
1232 (N : Node_Id;
1233 Comp_Typ : Entity_Id)
1234 is
1235 Comp_Ass : constant List_Id := Component_Associations (N);
1236 Dims_Of_Comp_Typ : constant Dimension_Type := Dimensions_Of (Comp_Typ);
1237 Exps : constant List_Id := Expressions (N);
1238
1239 Comp : Node_Id;
1240 Dims_Of_Expr : Dimension_Type;
1241 Expr : Node_Id;
1242
1243 Error_Detected : Boolean := False;
1244 -- This flag is used in order to indicate if an error has been detected
1245 -- so far by the compiler in this routine.
1246
1247 begin
1248 -- Aspect is an Ada 2012 feature. Nothing to do here if the component
1249 -- base type is not a dimensioned type.
1250
1251 -- Inlined bodies have already been checked for dimensionality.
1252
1253 -- Note that here the original node must come from source since the
1254 -- original array aggregate may not have been entirely decorated.
1255
1256 if Ada_Version < Ada_2012
1257 or else In_Inlined_Body
1258 or else not Comes_From_Source (Original_Node (N))
1259 or else not Has_Dimension_System (Base_Type (Comp_Typ))
1260 then
1261 return;
1262 end if;
1263
1264 -- Check whether there is any positional component association
1265
1266 if Is_Empty_List (Exps) then
1267 Comp := First (Comp_Ass);
1268 else
1269 Comp := First (Exps);
1270 end if;
1271
1272 while Present (Comp) loop
1273
1274 -- Get the expression from the component
1275
1276 if Nkind (Comp) in N_Component_Association
1277 | N_Iterated_Component_Association
1278 then
1279 Expr := Expression (Comp);
1280 else
1281 Expr := Comp;
1282 end if;
1283
1284 -- Issue an error if the dimensions of the component type and the
1285 -- dimensions of the component mismatch.
1286
1287 -- Note that we must ensure the expression has been fully analyzed
1288 -- since it may not be decorated at this point. We also don't want to
1289 -- issue the same error message multiple times on the same expression
1290 -- (may happen when an aggregate is converted into a positional
1291 -- aggregate). We also must verify that this is a scalar component,
1292 -- and not a subaggregate of a multidimensional aggregate.
1293 -- The expression may be an identifier that has been copied several
1294 -- times during expansion, its dimensions are those of its type.
1295
1296 if Is_Entity_Name (Expr) then
1297 Dims_Of_Expr := Dimensions_Of (Etype (Expr));
1298 else
1299 Dims_Of_Expr := Dimensions_Of (Expr);
1300 end if;
1301
1302 if Comes_From_Source (Original_Node (Expr))
1303 and then Present (Etype (Expr))
1304 and then Is_Numeric_Type (Etype (Expr))
1305 and then Dims_Of_Expr /= Dims_Of_Comp_Typ
1306 and then Sloc (Comp) /= Sloc (Prev (Comp))
1307 then
1308 -- Check if an error has already been encountered so far
1309
1310 if not Error_Detected then
1311 Error_Msg_N ("dimensions mismatch in array aggregate", N);
1312 Error_Detected := True;
1313 end if;
1314
1315 Error_Msg_N
1316 ("\expected dimension " & Dimensions_Msg_Of (Comp_Typ)
1317 & ", found " & Dimensions_Msg_Of (Expr), Expr);
1318 end if;
1319
1320 -- Look at the named components right after the positional components
1321
1322 if No (Next (Comp))
1323 and then List_Containing (Comp) = Exps
1324 then
1325 Comp := First (Comp_Ass);
1326 else
1327 Next (Comp);
1328 end if;
1329 end loop;
1330 end Analyze_Dimension_Array_Aggregate;
1331
1332 --------------------------------------------
1333 -- Analyze_Dimension_Assignment_Statement --
1334 --------------------------------------------
1335
1336 procedure Analyze_Dimension_Assignment_Statement (N : Node_Id) is
1337 Lhs : constant Node_Id := Name (N);
1338 Dims_Of_Lhs : constant Dimension_Type := Dimensions_Of (Lhs);
1339 Rhs : constant Node_Id := Expression (N);
1340 Dims_Of_Rhs : constant Dimension_Type := Dimensions_Of (Rhs);
1341
1342 procedure Error_Dim_Msg_For_Assignment_Statement
1343 (N : Node_Id;
1344 Lhs : Node_Id;
1345 Rhs : Node_Id);
1346 -- Error using Error_Msg_N at node N. Output the dimensions of left
1347 -- and right hand sides.
1348
1349 --------------------------------------------
1350 -- Error_Dim_Msg_For_Assignment_Statement --
1351 --------------------------------------------
1352
1353 procedure Error_Dim_Msg_For_Assignment_Statement
1354 (N : Node_Id;
1355 Lhs : Node_Id;
1356 Rhs : Node_Id)
1357 is
1358 begin
1359 Error_Msg_N ("dimensions mismatch in assignment", N);
1360 Error_Msg_N ("\left-hand side " & Dimensions_Msg_Of (Lhs, True), N);
1361 Error_Msg_N ("\right-hand side " & Dimensions_Msg_Of (Rhs, True), N);
1362 end Error_Dim_Msg_For_Assignment_Statement;
1363
1364 -- Start of processing for Analyze_Dimension_Assignment
1365
1366 begin
1367 if Dims_Of_Lhs /= Dims_Of_Rhs then
1368 Error_Dim_Msg_For_Assignment_Statement (N, Lhs, Rhs);
1369 end if;
1370 end Analyze_Dimension_Assignment_Statement;
1371
1372 ---------------------------------
1373 -- Analyze_Dimension_Binary_Op --
1374 ---------------------------------
1375
1376 -- Check and propagate the dimensions for binary operators
1377 -- Note that when the dimensions mismatch, no dimension is propagated to N.
1378
1379 procedure Analyze_Dimension_Binary_Op (N : Node_Id) is
1380 N_Kind : constant Node_Kind := Nkind (N);
1381
1382 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type;
1383 -- If the operand is a numeric literal that comes from a declared
1384 -- constant, use the dimensions of the constant which were computed
1385 -- from the expression of the constant declaration. Otherwise the
1386 -- dimensions are those of the operand, or the type of the operand.
1387 -- This takes care of node rewritings from validity checks, where the
1388 -- dimensions of the operand itself may not be preserved, while the
1389 -- type comes from context and must have dimension information.
1390
1391 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id);
1392 -- Error using Error_Msg_NE and Error_Msg_N at node N. Output the
1393 -- dimensions of both operands.
1394
1395 ---------------------------
1396 -- Dimensions_Of_Operand --
1397 ---------------------------
1398
1399 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type is
1400 Dims : constant Dimension_Type := Dimensions_Of (N);
1401
1402 begin
1403 if Exists (Dims) then
1404 return Dims;
1405
1406 elsif Is_Entity_Name (N) then
1407 return Dimensions_Of (Etype (Entity (N)));
1408
1409 elsif Nkind (N) = N_Real_Literal then
1410
1411 if Present (Original_Entity (N)) then
1412 return Dimensions_Of (Original_Entity (N));
1413
1414 else
1415 return Dimensions_Of (Etype (N));
1416 end if;
1417
1418 -- Otherwise return the default dimensions
1419
1420 else
1421 return Dims;
1422 end if;
1423 end Dimensions_Of_Operand;
1424
1425 ---------------------------------
1426 -- Error_Dim_Msg_For_Binary_Op --
1427 ---------------------------------
1428
1429 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id) is
1430 begin
1431 Error_Msg_NE
1432 ("both operands for operation& must have same dimensions",
1433 N, Entity (N));
1434 Error_Msg_N ("\left operand " & Dimensions_Msg_Of (L, True), N);
1435 Error_Msg_N ("\right operand " & Dimensions_Msg_Of (R, True), N);
1436 end Error_Dim_Msg_For_Binary_Op;
1437
1438 -- Start of processing for Analyze_Dimension_Binary_Op
1439
1440 begin
1441 -- If the node is already analyzed, do not examine the operands. At the
1442 -- end of the analysis their dimensions have been removed, and the node
1443 -- itself may have been rewritten.
1444
1445 if Analyzed (N) then
1446 return;
1447 end if;
1448
1449 if N_Kind in N_Op_Add | N_Op_Expon | N_Op_Subtract
1450 | N_Multiplying_Operator | N_Op_Compare
1451 then
1452 declare
1453 L : constant Node_Id := Left_Opnd (N);
1454 Dims_Of_L : constant Dimension_Type :=
1455 Dimensions_Of_Operand (L);
1456 L_Has_Dimensions : constant Boolean := Exists (Dims_Of_L);
1457 R : constant Node_Id := Right_Opnd (N);
1458 Dims_Of_R : constant Dimension_Type :=
1459 Dimensions_Of_Operand (R);
1460 R_Has_Dimensions : constant Boolean := Exists (Dims_Of_R);
1461 Dims_Of_N : Dimension_Type := Null_Dimension;
1462
1463 begin
1464 -- N_Op_Add, N_Op_Mod, N_Op_Rem or N_Op_Subtract case
1465
1466 if N_Kind in N_Op_Add | N_Op_Mod | N_Op_Rem | N_Op_Subtract then
1467
1468 -- Check both operands have same dimension
1469
1470 if Dims_Of_L /= Dims_Of_R then
1471 Error_Dim_Msg_For_Binary_Op (N, L, R);
1472 else
1473 -- Check both operands are not dimensionless
1474
1475 if Exists (Dims_Of_L) then
1476 Set_Dimensions (N, Dims_Of_L);
1477 end if;
1478 end if;
1479
1480 -- N_Op_Multiply or N_Op_Divide case
1481
1482 elsif N_Kind in N_Op_Multiply | N_Op_Divide then
1483
1484 -- Check at least one operand is not dimensionless
1485
1486 if L_Has_Dimensions or R_Has_Dimensions then
1487
1488 -- Multiplication case
1489
1490 -- Get both operands dimensions and add them
1491
1492 if N_Kind = N_Op_Multiply then
1493 for Position in Dimension_Type'Range loop
1494 Dims_Of_N (Position) :=
1495 Dims_Of_L (Position) + Dims_Of_R (Position);
1496 end loop;
1497
1498 -- Division case
1499
1500 -- Get both operands dimensions and subtract them
1501
1502 else
1503 for Position in Dimension_Type'Range loop
1504 Dims_Of_N (Position) :=
1505 Dims_Of_L (Position) - Dims_Of_R (Position);
1506 end loop;
1507 end if;
1508
1509 if Exists (Dims_Of_N) then
1510 Set_Dimensions (N, Dims_Of_N);
1511 end if;
1512 end if;
1513
1514 -- Exponentiation case
1515
1516 -- Note: a rational exponent is allowed for dimensioned operand
1517
1518 elsif N_Kind = N_Op_Expon then
1519
1520 -- Check the left operand is not dimensionless. Note that the
1521 -- value of the exponent must be known compile time. Otherwise,
1522 -- the exponentiation evaluation will return an error message.
1523
1524 if L_Has_Dimensions then
1525 if not Compile_Time_Known_Value (R) then
1526 Error_Msg_N
1527 ("exponent of dimensioned operand must be "
1528 & "known at compile time", N);
1529 end if;
1530
1531 declare
1532 Exponent_Value : Rational := Zero;
1533
1534 begin
1535 -- Real operand case
1536
1537 if Is_Real_Type (Etype (L)) then
1538
1539 -- Define the exponent as a Rational number
1540
1541 Exponent_Value := Create_Rational_From (R, False);
1542
1543 -- Verify that the exponent cannot be interpreted
1544 -- as a rational, otherwise interpret the exponent
1545 -- as an integer.
1546
1547 if Exponent_Value = No_Rational then
1548 Exponent_Value :=
1549 +Whole (UI_To_Int (Expr_Value (R)));
1550 end if;
1551
1552 -- Integer operand case.
1553
1554 -- For integer operand, the exponent cannot be
1555 -- interpreted as a rational.
1556
1557 else
1558 Exponent_Value := +Whole (UI_To_Int (Expr_Value (R)));
1559 end if;
1560
1561 for Position in Dimension_Type'Range loop
1562 Dims_Of_N (Position) :=
1563 Dims_Of_L (Position) * Exponent_Value;
1564 end loop;
1565
1566 if Exists (Dims_Of_N) then
1567 Set_Dimensions (N, Dims_Of_N);
1568 end if;
1569 end;
1570 end if;
1571
1572 -- Comparison cases
1573
1574 -- For relational operations, only dimension checking is
1575 -- performed (no propagation). If one operand is the result
1576 -- of constant folding the dimensions may have been lost
1577 -- in a tree copy, so assume that preanalysis has verified
1578 -- that dimensions are correct.
1579
1580 elsif N_Kind in N_Op_Compare then
1581 if (L_Has_Dimensions or R_Has_Dimensions)
1582 and then Dims_Of_L /= Dims_Of_R
1583 then
1584 if Nkind (L) = N_Real_Literal
1585 and then not Comes_From_Source (L)
1586 and then Expander_Active
1587 then
1588 null;
1589
1590 elsif Nkind (R) = N_Real_Literal
1591 and then not Comes_From_Source (R)
1592 and then Expander_Active
1593 then
1594 null;
1595
1596 -- Numeric literal case. Issue a warning to indicate the
1597 -- literal is treated as if its dimension matches the type
1598 -- dimension.
1599
1600 elsif Nkind (Original_Node (L)) in
1601 N_Integer_Literal | N_Real_Literal
1602 then
1603 Dim_Warning_For_Numeric_Literal (L, Etype (R));
1604
1605 elsif Nkind (Original_Node (R)) in
1606 N_Integer_Literal | N_Real_Literal
1607 then
1608 Dim_Warning_For_Numeric_Literal (R, Etype (L));
1609
1610 else
1611 Error_Dim_Msg_For_Binary_Op (N, L, R);
1612 end if;
1613 end if;
1614 end if;
1615
1616 -- If expander is active, remove dimension information from each
1617 -- operand, as only dimensions of result are relevant.
1618
1619 if Expander_Active then
1620 Remove_Dimensions (L);
1621 Remove_Dimensions (R);
1622 end if;
1623 end;
1624 end if;
1625 end Analyze_Dimension_Binary_Op;
1626
1627 ----------------------------
1628 -- Analyze_Dimension_Call --
1629 ----------------------------
1630
1631 procedure Analyze_Dimension_Call (N : Node_Id; Nam : Entity_Id) is
1632 Actuals : constant List_Id := Parameter_Associations (N);
1633 Actual : Node_Id;
1634 Dims_Of_Formal : Dimension_Type;
1635 Formal : Node_Id;
1636 Formal_Typ : Entity_Id;
1637
1638 Error_Detected : Boolean := False;
1639 -- This flag is used in order to indicate if an error has been detected
1640 -- so far by the compiler in this routine.
1641
1642 begin
1643 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1644 -- dimensions for calls in inlined bodies, or calls that don't come
1645 -- from source, or those that may have semantic errors.
1646
1647 if Ada_Version < Ada_2012
1648 or else In_Inlined_Body
1649 or else not Comes_From_Source (N)
1650 or else Error_Posted (N)
1651 then
1652 return;
1653 end if;
1654
1655 -- Check the dimensions of the actuals, if any
1656
1657 if not Is_Empty_List (Actuals) then
1658
1659 -- Special processing for elementary functions
1660
1661 -- For Sqrt call, the resulting dimensions equal to half the
1662 -- dimensions of the actual. For all other elementary calls, this
1663 -- routine check that every actual is dimensionless.
1664
1665 if Nkind (N) = N_Function_Call then
1666 Elementary_Function_Calls : declare
1667 Dims_Of_Call : Dimension_Type;
1668 Ent : Entity_Id := Nam;
1669
1670 function Is_Elementary_Function_Entity
1671 (Sub_Id : Entity_Id) return Boolean;
1672 -- Given Sub_Id, the original subprogram entity, return True
1673 -- if call is to an elementary function (see Ada.Numerics.
1674 -- Generic_Elementary_Functions).
1675
1676 -----------------------------------
1677 -- Is_Elementary_Function_Entity --
1678 -----------------------------------
1679
1680 function Is_Elementary_Function_Entity
1681 (Sub_Id : Entity_Id) return Boolean
1682 is
1683 Loc : constant Source_Ptr := Sloc (Sub_Id);
1684
1685 begin
1686 -- Is entity in Ada.Numerics.Generic_Elementary_Functions?
1687
1688 return
1689 Loc > No_Location
1690 and then
1691 Is_RTU
1692 (Cunit_Entity (Get_Source_Unit (Loc)),
1693 Ada_Numerics_Generic_Elementary_Functions);
1694 end Is_Elementary_Function_Entity;
1695
1696 -- Start of processing for Elementary_Function_Calls
1697
1698 begin
1699 -- Get original subprogram entity following the renaming chain
1700
1701 if Present (Alias (Ent)) then
1702 Ent := Alias (Ent);
1703 end if;
1704
1705 -- Check the call is an Elementary function call
1706
1707 if Is_Elementary_Function_Entity (Ent) then
1708
1709 -- Sqrt function call case
1710
1711 if Chars (Ent) = Name_Sqrt then
1712 Dims_Of_Call := Dimensions_Of (First_Actual (N));
1713
1714 -- Evaluates the resulting dimensions (i.e. half the
1715 -- dimensions of the actual).
1716
1717 if Exists (Dims_Of_Call) then
1718 for Position in Dims_Of_Call'Range loop
1719 Dims_Of_Call (Position) :=
1720 Dims_Of_Call (Position) *
1721 Rational'(Numerator => 1, Denominator => 2);
1722 end loop;
1723
1724 Set_Dimensions (N, Dims_Of_Call);
1725 end if;
1726
1727 -- All other elementary functions case. Note that every
1728 -- actual here should be dimensionless.
1729
1730 else
1731 Actual := First_Actual (N);
1732 while Present (Actual) loop
1733 if Exists (Dimensions_Of (Actual)) then
1734
1735 -- Check if error has already been encountered
1736
1737 if not Error_Detected then
1738 Error_Msg_NE
1739 ("dimensions mismatch in call of&",
1740 N, Name (N));
1741 Error_Detected := True;
1742 end if;
1743
1744 Error_Msg_N
1745 ("\expected dimension '['], found "
1746 & Dimensions_Msg_Of (Actual), Actual);
1747 end if;
1748
1749 Next_Actual (Actual);
1750 end loop;
1751 end if;
1752
1753 -- Nothing more to do for elementary functions
1754
1755 return;
1756 end if;
1757 end Elementary_Function_Calls;
1758 end if;
1759
1760 -- General case. Check, for each parameter, the dimensions of the
1761 -- actual and its corresponding formal match. Otherwise, complain.
1762
1763 Actual := First_Actual (N);
1764 Formal := First_Formal (Nam);
1765 while Present (Formal) loop
1766
1767 -- A missing corresponding actual indicates that the analysis of
1768 -- the call was aborted due to a previous error.
1769
1770 if No (Actual) then
1771 Check_Error_Detected;
1772 return;
1773 end if;
1774
1775 Formal_Typ := Etype (Formal);
1776 Dims_Of_Formal := Dimensions_Of (Formal_Typ);
1777
1778 -- If the formal is not dimensionless, check dimensions of formal
1779 -- and actual match. Otherwise, complain.
1780
1781 if Exists (Dims_Of_Formal)
1782 and then Dimensions_Of (Actual) /= Dims_Of_Formal
1783 then
1784 -- Check if an error has already been encountered so far
1785
1786 if not Error_Detected then
1787 Error_Msg_NE ("dimensions mismatch in& call", N, Name (N));
1788 Error_Detected := True;
1789 end if;
1790
1791 Error_Msg_N
1792 ("\expected dimension " & Dimensions_Msg_Of (Formal_Typ)
1793 & ", found " & Dimensions_Msg_Of (Actual), Actual);
1794 end if;
1795
1796 Next_Actual (Actual);
1797 Next_Formal (Formal);
1798 end loop;
1799 end if;
1800
1801 -- For function calls, propagate the dimensions from the returned type
1802
1803 if Nkind (N) = N_Function_Call then
1804 Analyze_Dimension_Has_Etype (N);
1805 end if;
1806 end Analyze_Dimension_Call;
1807
1808 ---------------------------------------
1809 -- Analyze_Dimension_Case_Expression --
1810 ---------------------------------------
1811
1812 procedure Analyze_Dimension_Case_Expression (N : Node_Id) is
1813 Frst : constant Node_Id := First (Alternatives (N));
1814 Frst_Expr : constant Node_Id := Expression (Frst);
1815 Dims : constant Dimension_Type := Dimensions_Of (Frst_Expr);
1816
1817 Alt : Node_Id;
1818
1819 begin
1820 Alt := Next (Frst);
1821 while Present (Alt) loop
1822 if Dimensions_Of (Expression (Alt)) /= Dims then
1823 Error_Msg_N ("dimension mismatch in case expression", Alt);
1824 exit;
1825 end if;
1826
1827 Next (Alt);
1828 end loop;
1829
1830 Copy_Dimensions (Frst_Expr, N);
1831 end Analyze_Dimension_Case_Expression;
1832
1833 ---------------------------------------------
1834 -- Analyze_Dimension_Component_Declaration --
1835 ---------------------------------------------
1836
1837 procedure Analyze_Dimension_Component_Declaration (N : Node_Id) is
1838 Expr : constant Node_Id := Expression (N);
1839 Id : constant Entity_Id := Defining_Identifier (N);
1840 Etyp : constant Entity_Id := Etype (Id);
1841 Dims_Of_Etyp : constant Dimension_Type := Dimensions_Of (Etyp);
1842 Dims_Of_Expr : Dimension_Type;
1843
1844 procedure Error_Dim_Msg_For_Component_Declaration
1845 (N : Node_Id;
1846 Etyp : Entity_Id;
1847 Expr : Node_Id);
1848 -- Error using Error_Msg_N at node N. Output the dimensions of the
1849 -- type Etyp and the expression Expr of N.
1850
1851 ---------------------------------------------
1852 -- Error_Dim_Msg_For_Component_Declaration --
1853 ---------------------------------------------
1854
1855 procedure Error_Dim_Msg_For_Component_Declaration
1856 (N : Node_Id;
1857 Etyp : Entity_Id;
1858 Expr : Node_Id) is
1859 begin
1860 Error_Msg_N ("dimensions mismatch in component declaration", N);
1861 Error_Msg_N
1862 ("\expected dimension " & Dimensions_Msg_Of (Etyp) & ", found "
1863 & Dimensions_Msg_Of (Expr), Expr);
1864 end Error_Dim_Msg_For_Component_Declaration;
1865
1866 -- Start of processing for Analyze_Dimension_Component_Declaration
1867
1868 begin
1869 -- Expression is present
1870
1871 if Present (Expr) then
1872 Dims_Of_Expr := Dimensions_Of (Expr);
1873
1874 -- Check dimensions match
1875
1876 if Dims_Of_Etyp /= Dims_Of_Expr then
1877
1878 -- Numeric literal case. Issue a warning if the object type is not
1879 -- dimensionless to indicate the literal is treated as if its
1880 -- dimension matches the type dimension.
1881
1882 if Nkind (Original_Node (Expr)) in
1883 N_Real_Literal | N_Integer_Literal
1884 then
1885 Dim_Warning_For_Numeric_Literal (Expr, Etyp);
1886
1887 -- Issue a dimension mismatch error for all other cases
1888
1889 else
1890 Error_Dim_Msg_For_Component_Declaration (N, Etyp, Expr);
1891 end if;
1892 end if;
1893 end if;
1894 end Analyze_Dimension_Component_Declaration;
1895
1896 -------------------------------------------------
1897 -- Analyze_Dimension_Extended_Return_Statement --
1898 -------------------------------------------------
1899
1900 procedure Analyze_Dimension_Extended_Return_Statement (N : Node_Id) is
1901 Return_Ent : constant Entity_Id := Return_Statement_Entity (N);
1902 Return_Etyp : constant Entity_Id :=
1903 Etype (Return_Applies_To (Return_Ent));
1904 Return_Obj_Decls : constant List_Id := Return_Object_Declarations (N);
1905 Return_Obj_Decl : Node_Id;
1906 Return_Obj_Id : Entity_Id;
1907 Return_Obj_Typ : Entity_Id;
1908
1909 procedure Error_Dim_Msg_For_Extended_Return_Statement
1910 (N : Node_Id;
1911 Return_Etyp : Entity_Id;
1912 Return_Obj_Typ : Entity_Id);
1913 -- Error using Error_Msg_N at node N. Output dimensions of the returned
1914 -- type Return_Etyp and the returned object type Return_Obj_Typ of N.
1915
1916 -------------------------------------------------
1917 -- Error_Dim_Msg_For_Extended_Return_Statement --
1918 -------------------------------------------------
1919
1920 procedure Error_Dim_Msg_For_Extended_Return_Statement
1921 (N : Node_Id;
1922 Return_Etyp : Entity_Id;
1923 Return_Obj_Typ : Entity_Id)
1924 is
1925 begin
1926 Error_Msg_N ("dimensions mismatch in extended return statement", N);
1927 Error_Msg_N
1928 ("\expected dimension " & Dimensions_Msg_Of (Return_Etyp)
1929 & ", found " & Dimensions_Msg_Of (Return_Obj_Typ), N);
1930 end Error_Dim_Msg_For_Extended_Return_Statement;
1931
1932 -- Start of processing for Analyze_Dimension_Extended_Return_Statement
1933
1934 begin
1935 if Present (Return_Obj_Decls) then
1936 Return_Obj_Decl := First (Return_Obj_Decls);
1937 while Present (Return_Obj_Decl) loop
1938 if Nkind (Return_Obj_Decl) = N_Object_Declaration then
1939 Return_Obj_Id := Defining_Identifier (Return_Obj_Decl);
1940
1941 if Is_Return_Object (Return_Obj_Id) then
1942 Return_Obj_Typ := Etype (Return_Obj_Id);
1943
1944 -- Issue an error message if dimensions mismatch
1945
1946 if Dimensions_Of (Return_Etyp) /=
1947 Dimensions_Of (Return_Obj_Typ)
1948 then
1949 Error_Dim_Msg_For_Extended_Return_Statement
1950 (N, Return_Etyp, Return_Obj_Typ);
1951 return;
1952 end if;
1953 end if;
1954 end if;
1955
1956 Next (Return_Obj_Decl);
1957 end loop;
1958 end if;
1959 end Analyze_Dimension_Extended_Return_Statement;
1960
1961 -----------------------------------------------------
1962 -- Analyze_Dimension_Extension_Or_Record_Aggregate --
1963 -----------------------------------------------------
1964
1965 procedure Analyze_Dimension_Extension_Or_Record_Aggregate (N : Node_Id) is
1966 Comp : Node_Id;
1967 Comp_Id : Entity_Id;
1968 Comp_Typ : Entity_Id;
1969 Expr : Node_Id;
1970
1971 Error_Detected : Boolean := False;
1972 -- This flag is used in order to indicate if an error has been detected
1973 -- so far by the compiler in this routine.
1974
1975 begin
1976 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1977 -- dimensions in inlined bodies, or for aggregates that don't come
1978 -- from source, or if we are within an initialization procedure, whose
1979 -- expressions have been checked at the point of record declaration.
1980
1981 if Ada_Version < Ada_2012
1982 or else In_Inlined_Body
1983 or else not Comes_From_Source (N)
1984 or else Inside_Init_Proc
1985 then
1986 return;
1987 end if;
1988
1989 Comp := First (Component_Associations (N));
1990 while Present (Comp) loop
1991 Comp_Id := Entity (First (Choices (Comp)));
1992 Comp_Typ := Etype (Comp_Id);
1993
1994 -- Check the component type is either a dimensioned type or a
1995 -- dimensioned subtype.
1996
1997 if Has_Dimension_System (Base_Type (Comp_Typ)) then
1998 Expr := Expression (Comp);
1999
2000 -- A box-initialized component needs no checking.
2001
2002 if No (Expr) and then Box_Present (Comp) then
2003 null;
2004
2005 -- Issue an error if the dimensions of the component type and the
2006 -- dimensions of the component mismatch.
2007
2008 elsif Dimensions_Of (Expr) /= Dimensions_Of (Comp_Typ) then
2009
2010 -- Check if an error has already been encountered so far
2011
2012 if not Error_Detected then
2013
2014 -- Extension aggregate case
2015
2016 if Nkind (N) = N_Extension_Aggregate then
2017 Error_Msg_N
2018 ("dimensions mismatch in extension aggregate", N);
2019
2020 -- Record aggregate case
2021
2022 else
2023 Error_Msg_N
2024 ("dimensions mismatch in record aggregate", N);
2025 end if;
2026
2027 Error_Detected := True;
2028 end if;
2029
2030 Error_Msg_N
2031 ("\expected dimension " & Dimensions_Msg_Of (Comp_Typ)
2032 & ", found " & Dimensions_Msg_Of (Expr), Comp);
2033 end if;
2034 end if;
2035
2036 Next (Comp);
2037 end loop;
2038 end Analyze_Dimension_Extension_Or_Record_Aggregate;
2039
2040 -------------------------------
2041 -- Analyze_Dimension_Formals --
2042 -------------------------------
2043
2044 procedure Analyze_Dimension_Formals (N : Node_Id; Formals : List_Id) is
2045 Dims_Of_Typ : Dimension_Type;
2046 Formal : Node_Id;
2047 Typ : Entity_Id;
2048
2049 begin
2050 -- Aspect is an Ada 2012 feature. Note that there is no need to check
2051 -- dimensions for sub specs that don't come from source.
2052
2053 if Ada_Version < Ada_2012 or else not Comes_From_Source (N) then
2054 return;
2055 end if;
2056
2057 Formal := First (Formals);
2058 while Present (Formal) loop
2059 Typ := Parameter_Type (Formal);
2060 Dims_Of_Typ := Dimensions_Of (Typ);
2061
2062 if Exists (Dims_Of_Typ) then
2063 declare
2064 Expr : constant Node_Id := Expression (Formal);
2065
2066 begin
2067 -- Issue a warning if Expr is a numeric literal and if its
2068 -- dimensions differ with the dimensions of the formal type.
2069
2070 if Present (Expr)
2071 and then Dims_Of_Typ /= Dimensions_Of (Expr)
2072 and then Nkind (Original_Node (Expr)) in
2073 N_Real_Literal | N_Integer_Literal
2074 then
2075 Dim_Warning_For_Numeric_Literal (Expr, Etype (Typ));
2076 end if;
2077 end;
2078 end if;
2079
2080 Next (Formal);
2081 end loop;
2082 end Analyze_Dimension_Formals;
2083
2084 ---------------------------------
2085 -- Analyze_Dimension_Has_Etype --
2086 ---------------------------------
2087
2088 procedure Analyze_Dimension_Has_Etype (N : Node_Id) is
2089 Etyp : constant Entity_Id := Etype (N);
2090 Dims_Of_Etyp : Dimension_Type := Dimensions_Of (Etyp);
2091
2092 begin
2093 -- General case. Propagation of the dimensions from the type
2094
2095 if Exists (Dims_Of_Etyp) then
2096 Set_Dimensions (N, Dims_Of_Etyp);
2097
2098 -- Identifier case. Propagate the dimensions from the entity for
2099 -- identifier whose entity is a non-dimensionless constant.
2100
2101 elsif Nkind (N) = N_Identifier then
2102 Analyze_Dimension_Identifier : declare
2103 Id : constant Entity_Id := Entity (N);
2104
2105 begin
2106 -- If Id is missing, abnormal tree, assume previous error
2107
2108 if No (Id) then
2109 Check_Error_Detected;
2110 return;
2111
2112 elsif Ekind (Id) in E_Constant | E_Named_Real
2113 and then Exists (Dimensions_Of (Id))
2114 then
2115 Set_Dimensions (N, Dimensions_Of (Id));
2116 end if;
2117 end Analyze_Dimension_Identifier;
2118
2119 -- Attribute reference case. Propagate the dimensions from the prefix.
2120
2121 elsif Nkind (N) = N_Attribute_Reference
2122 and then Has_Dimension_System (Base_Type (Etyp))
2123 then
2124 Dims_Of_Etyp := Dimensions_Of (Prefix (N));
2125
2126 -- Check the prefix is not dimensionless
2127
2128 if Exists (Dims_Of_Etyp) then
2129 Set_Dimensions (N, Dims_Of_Etyp);
2130 end if;
2131 end if;
2132
2133 -- Remove dimensions from inner expressions, to prevent dimensions
2134 -- table from growing uselessly.
2135
2136 case Nkind (N) is
2137 when N_Attribute_Reference
2138 | N_Indexed_Component
2139 =>
2140 declare
2141 Exprs : constant List_Id := Expressions (N);
2142 Expr : Node_Id;
2143
2144 begin
2145 if Present (Exprs) then
2146 Expr := First (Exprs);
2147 while Present (Expr) loop
2148 Remove_Dimensions (Expr);
2149 Next (Expr);
2150 end loop;
2151 end if;
2152 end;
2153
2154 when N_Qualified_Expression
2155 | N_Type_Conversion
2156 | N_Unchecked_Type_Conversion
2157 =>
2158 Remove_Dimensions (Expression (N));
2159
2160 when N_Selected_Component =>
2161 Remove_Dimensions (Selector_Name (N));
2162
2163 when others =>
2164 null;
2165 end case;
2166 end Analyze_Dimension_Has_Etype;
2167
2168 -------------------------------------
2169 -- Analyze_Dimension_If_Expression --
2170 -------------------------------------
2171
2172 procedure Analyze_Dimension_If_Expression (N : Node_Id) is
2173 Then_Expr : constant Node_Id := Next (First (Expressions (N)));
2174 Else_Expr : constant Node_Id := Next (Then_Expr);
2175
2176 begin
2177 if Dimensions_Of (Then_Expr) /= Dimensions_Of (Else_Expr) then
2178 Error_Msg_N ("dimensions mismatch in conditional expression", N);
2179 else
2180 Copy_Dimensions (Then_Expr, N);
2181 end if;
2182 end Analyze_Dimension_If_Expression;
2183
2184 ------------------------------------------
2185 -- Analyze_Dimension_Number_Declaration --
2186 ------------------------------------------
2187
2188 procedure Analyze_Dimension_Number_Declaration (N : Node_Id) is
2189 Expr : constant Node_Id := Expression (N);
2190 Id : constant Entity_Id := Defining_Identifier (N);
2191 Dim_Of_Expr : constant Dimension_Type := Dimensions_Of (Expr);
2192
2193 begin
2194 if Exists (Dim_Of_Expr) then
2195 Set_Dimensions (Id, Dim_Of_Expr);
2196 Set_Etype (Id, Etype (Expr));
2197 end if;
2198 end Analyze_Dimension_Number_Declaration;
2199
2200 ------------------------------------------
2201 -- Analyze_Dimension_Object_Declaration --
2202 ------------------------------------------
2203
2204 procedure Analyze_Dimension_Object_Declaration (N : Node_Id) is
2205 Expr : constant Node_Id := Expression (N);
2206 Id : constant Entity_Id := Defining_Identifier (N);
2207 Etyp : constant Entity_Id := Etype (Id);
2208 Dim_Of_Etyp : constant Dimension_Type := Dimensions_Of (Etyp);
2209 Dim_Of_Expr : Dimension_Type;
2210
2211 procedure Error_Dim_Msg_For_Object_Declaration
2212 (N : Node_Id;
2213 Etyp : Entity_Id;
2214 Expr : Node_Id);
2215 -- Error using Error_Msg_N at node N. Output the dimensions of the
2216 -- type Etyp and of the expression Expr.
2217
2218 ------------------------------------------
2219 -- Error_Dim_Msg_For_Object_Declaration --
2220 ------------------------------------------
2221
2222 procedure Error_Dim_Msg_For_Object_Declaration
2223 (N : Node_Id;
2224 Etyp : Entity_Id;
2225 Expr : Node_Id) is
2226 begin
2227 Error_Msg_N ("dimensions mismatch in object declaration", N);
2228 Error_Msg_N
2229 ("\expected dimension " & Dimensions_Msg_Of (Etyp) & ", found "
2230 & Dimensions_Msg_Of (Expr), Expr);
2231 end Error_Dim_Msg_For_Object_Declaration;
2232
2233 -- Start of processing for Analyze_Dimension_Object_Declaration
2234
2235 begin
2236 -- Expression is present
2237
2238 if Present (Expr) then
2239 Dim_Of_Expr := Dimensions_Of (Expr);
2240
2241 -- Check dimensions match
2242
2243 if Dim_Of_Expr /= Dim_Of_Etyp then
2244
2245 -- Numeric literal case. Issue a warning if the object type is
2246 -- not dimensionless to indicate the literal is treated as if
2247 -- its dimension matches the type dimension.
2248
2249 if Nkind (Original_Node (Expr)) in
2250 N_Real_Literal | N_Integer_Literal
2251 then
2252 Dim_Warning_For_Numeric_Literal (Expr, Etyp);
2253
2254 -- Case of object is a constant whose type is a dimensioned type
2255
2256 elsif Constant_Present (N) and then not Exists (Dim_Of_Etyp) then
2257
2258 -- Propagate dimension from expression to object entity
2259
2260 Set_Dimensions (Id, Dim_Of_Expr);
2261
2262 -- Expression may have been constant-folded. If nominal type has
2263 -- dimensions, verify that expression has same type.
2264
2265 elsif Exists (Dim_Of_Etyp) and then Etype (Expr) = Etyp then
2266 null;
2267
2268 -- For all other cases, issue an error message
2269
2270 else
2271 Error_Dim_Msg_For_Object_Declaration (N, Etyp, Expr);
2272 end if;
2273 end if;
2274
2275 -- Remove dimensions in expression after checking consistency with
2276 -- given type.
2277
2278 Remove_Dimensions (Expr);
2279 end if;
2280 end Analyze_Dimension_Object_Declaration;
2281
2282 ---------------------------------------------------
2283 -- Analyze_Dimension_Object_Renaming_Declaration --
2284 ---------------------------------------------------
2285
2286 procedure Analyze_Dimension_Object_Renaming_Declaration (N : Node_Id) is
2287 Renamed_Name : constant Node_Id := Name (N);
2288 Sub_Mark : constant Node_Id := Subtype_Mark (N);
2289
2290 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2291 (N : Node_Id;
2292 Sub_Mark : Node_Id;
2293 Renamed_Name : Node_Id);
2294 -- Error using Error_Msg_N at node N. Output the dimensions of
2295 -- Sub_Mark and of Renamed_Name.
2296
2297 ---------------------------------------------------
2298 -- Error_Dim_Msg_For_Object_Renaming_Declaration --
2299 ---------------------------------------------------
2300
2301 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2302 (N : Node_Id;
2303 Sub_Mark : Node_Id;
2304 Renamed_Name : Node_Id) is
2305 begin
2306 Error_Msg_N ("dimensions mismatch in object renaming declaration", N);
2307 Error_Msg_N
2308 ("\expected dimension " & Dimensions_Msg_Of (Sub_Mark) & ", found "
2309 & Dimensions_Msg_Of (Renamed_Name), Renamed_Name);
2310 end Error_Dim_Msg_For_Object_Renaming_Declaration;
2311
2312 -- Start of processing for Analyze_Dimension_Object_Renaming_Declaration
2313
2314 begin
2315 if Dimensions_Of (Renamed_Name) /= Dimensions_Of (Sub_Mark) then
2316 Error_Dim_Msg_For_Object_Renaming_Declaration
2317 (N, Sub_Mark, Renamed_Name);
2318 end if;
2319 end Analyze_Dimension_Object_Renaming_Declaration;
2320
2321 -----------------------------------------------
2322 -- Analyze_Dimension_Simple_Return_Statement --
2323 -----------------------------------------------
2324
2325 procedure Analyze_Dimension_Simple_Return_Statement (N : Node_Id) is
2326 Expr : constant Node_Id := Expression (N);
2327 Return_Ent : constant Entity_Id := Return_Statement_Entity (N);
2328 Return_Etyp : constant Entity_Id :=
2329 Etype (Return_Applies_To (Return_Ent));
2330 Dims_Of_Return_Etyp : constant Dimension_Type :=
2331 Dimensions_Of (Return_Etyp);
2332
2333 procedure Error_Dim_Msg_For_Simple_Return_Statement
2334 (N : Node_Id;
2335 Return_Etyp : Entity_Id;
2336 Expr : Node_Id);
2337 -- Error using Error_Msg_N at node N. Output the dimensions of the
2338 -- returned type Return_Etyp and the returned expression Expr of N.
2339
2340 -----------------------------------------------
2341 -- Error_Dim_Msg_For_Simple_Return_Statement --
2342 -----------------------------------------------
2343
2344 procedure Error_Dim_Msg_For_Simple_Return_Statement
2345 (N : Node_Id;
2346 Return_Etyp : Entity_Id;
2347 Expr : Node_Id)
2348 is
2349 begin
2350 Error_Msg_N ("dimensions mismatch in return statement", N);
2351 Error_Msg_N
2352 ("\expected dimension " & Dimensions_Msg_Of (Return_Etyp)
2353 & ", found " & Dimensions_Msg_Of (Expr), Expr);
2354 end Error_Dim_Msg_For_Simple_Return_Statement;
2355
2356 -- Start of processing for Analyze_Dimension_Simple_Return_Statement
2357
2358 begin
2359 if Dims_Of_Return_Etyp /= Dimensions_Of (Expr) then
2360 Error_Dim_Msg_For_Simple_Return_Statement (N, Return_Etyp, Expr);
2361 Remove_Dimensions (Expr);
2362 end if;
2363 end Analyze_Dimension_Simple_Return_Statement;
2364
2365 -------------------------------------------
2366 -- Analyze_Dimension_Subtype_Declaration --
2367 -------------------------------------------
2368
2369 procedure Analyze_Dimension_Subtype_Declaration (N : Node_Id) is
2370 Id : constant Entity_Id := Defining_Identifier (N);
2371 Dims_Of_Id : constant Dimension_Type := Dimensions_Of (Id);
2372 Dims_Of_Etyp : Dimension_Type;
2373 Etyp : Node_Id;
2374
2375 begin
2376 -- No constraint case in subtype declaration
2377
2378 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
2379 Etyp := Etype (Subtype_Indication (N));
2380 Dims_Of_Etyp := Dimensions_Of (Etyp);
2381
2382 if Exists (Dims_Of_Etyp) then
2383
2384 -- If subtype already has a dimension (from Aspect_Dimension), it
2385 -- cannot inherit different dimensions from its subtype.
2386
2387 if Exists (Dims_Of_Id) and then Dims_Of_Etyp /= Dims_Of_Id then
2388 Error_Msg_NE
2389 ("subtype& already " & Dimensions_Msg_Of (Id, True), N, Id);
2390 else
2391 Set_Dimensions (Id, Dims_Of_Etyp);
2392 Set_Symbol (Id, Symbol_Of (Etyp));
2393 end if;
2394 end if;
2395
2396 -- Constraint present in subtype declaration
2397
2398 else
2399 Etyp := Etype (Subtype_Mark (Subtype_Indication (N)));
2400 Dims_Of_Etyp := Dimensions_Of (Etyp);
2401
2402 if Exists (Dims_Of_Etyp) then
2403 Set_Dimensions (Id, Dims_Of_Etyp);
2404 Set_Symbol (Id, Symbol_Of (Etyp));
2405 end if;
2406 end if;
2407 end Analyze_Dimension_Subtype_Declaration;
2408
2409 ---------------------------------------
2410 -- Analyze_Dimension_Type_Conversion --
2411 ---------------------------------------
2412
2413 procedure Analyze_Dimension_Type_Conversion (N : Node_Id) is
2414 Expr_Root : constant Entity_Id :=
2415 Dimension_System_Root (Etype (Expression (N)));
2416 Target_Root : constant Entity_Id :=
2417 Dimension_System_Root (Etype (N));
2418
2419 begin
2420 -- If the expression has dimensions and the target type has dimensions,
2421 -- the conversion has the dimensions of the expression. Consistency is
2422 -- checked below. Converting to a non-dimensioned type such as Float
2423 -- ignores the dimensions of the expression.
2424
2425 if Exists (Dimensions_Of (Expression (N)))
2426 and then Present (Target_Root)
2427 then
2428 Set_Dimensions (N, Dimensions_Of (Expression (N)));
2429
2430 -- Otherwise the dimensions are those of the target type.
2431
2432 else
2433 Analyze_Dimension_Has_Etype (N);
2434 end if;
2435
2436 -- A conversion between types in different dimension systems (e.g. MKS
2437 -- and British units) must respect the dimensions of expression and
2438 -- type, It is up to the user to provide proper conversion factors.
2439
2440 -- Upward conversions to root type of a dimensioned system are legal,
2441 -- and correspond to "view conversions", i.e. preserve the dimensions
2442 -- of the expression; otherwise conversion must be between types with
2443 -- then same dimensions. Conversions to a non-dimensioned type such as
2444 -- Float lose the dimensions of the expression.
2445
2446 if Present (Expr_Root)
2447 and then Present (Target_Root)
2448 and then Etype (N) /= Target_Root
2449 and then Dimensions_Of (Expression (N)) /= Dimensions_Of (Etype (N))
2450 then
2451 Error_Msg_N ("dimensions mismatch in conversion", N);
2452 Error_Msg_N
2453 ("\expression " & Dimensions_Msg_Of (Expression (N), True), N);
2454 Error_Msg_N
2455 ("\target type " & Dimensions_Msg_Of (Etype (N), True), N);
2456 end if;
2457 end Analyze_Dimension_Type_Conversion;
2458
2459 --------------------------------
2460 -- Analyze_Dimension_Unary_Op --
2461 --------------------------------
2462
2463 procedure Analyze_Dimension_Unary_Op (N : Node_Id) is
2464 begin
2465 case Nkind (N) is
2466
2467 -- Propagate the dimension if the operand is not dimensionless
2468
2469 when N_Op_Abs
2470 | N_Op_Minus
2471 | N_Op_Plus
2472 =>
2473 declare
2474 R : constant Node_Id := Right_Opnd (N);
2475 begin
2476 Move_Dimensions (R, N);
2477 end;
2478
2479 when others =>
2480 null;
2481 end case;
2482 end Analyze_Dimension_Unary_Op;
2483
2484 ---------------------------------
2485 -- Check_Expression_Dimensions --
2486 ---------------------------------
2487
2488 procedure Check_Expression_Dimensions
2489 (Expr : Node_Id;
2490 Typ : Entity_Id)
2491 is
2492 begin
2493 if Is_Floating_Point_Type (Etype (Expr)) then
2494 Analyze_Dimension (Expr);
2495
2496 if Dimensions_Of (Expr) /= Dimensions_Of (Typ) then
2497 Error_Msg_N ("dimensions mismatch in array aggregate", Expr);
2498 Error_Msg_N
2499 ("\expected dimension " & Dimensions_Msg_Of (Typ)
2500 & ", found " & Dimensions_Msg_Of (Expr), Expr);
2501 end if;
2502 end if;
2503 end Check_Expression_Dimensions;
2504
2505 ---------------------
2506 -- Copy_Dimensions --
2507 ---------------------
2508
2509 procedure Copy_Dimensions (From : Node_Id; To : Node_Id) is
2510 Dims_Of_From : constant Dimension_Type := Dimensions_Of (From);
2511
2512 begin
2513 -- Ignore if not Ada 2012 or beyond
2514
2515 if Ada_Version < Ada_2012 then
2516 return;
2517
2518 -- For Ada 2012, Copy the dimension of 'From to 'To'
2519
2520 elsif Exists (Dims_Of_From) then
2521 Set_Dimensions (To, Dims_Of_From);
2522 end if;
2523 end Copy_Dimensions;
2524
2525 -----------------------------------
2526 -- Copy_Dimensions_Of_Components --
2527 -----------------------------------
2528
2529 procedure Copy_Dimensions_Of_Components (Rec : Entity_Id) is
2530 C : Entity_Id;
2531
2532 begin
2533 C := First_Component (Rec);
2534 while Present (C) loop
2535 if Nkind (Parent (C)) = N_Component_Declaration then
2536 Copy_Dimensions
2537 (Expression (Parent (Corresponding_Record_Component (C))),
2538 Expression (Parent (C)));
2539 end if;
2540 Next_Component (C);
2541 end loop;
2542 end Copy_Dimensions_Of_Components;
2543
2544 --------------------------
2545 -- Create_Rational_From --
2546 --------------------------
2547
2548 -- RATIONAL ::= [-] NUMERAL [/ NUMERAL]
2549
2550 -- A rational number is a number that can be expressed as the quotient or
2551 -- fraction a/b of two integers, where b is non-zero positive.
2552
2553 function Create_Rational_From
2554 (Expr : Node_Id;
2555 Complain : Boolean) return Rational
2556 is
2557 Or_Node_Of_Expr : constant Node_Id := Original_Node (Expr);
2558 Result : Rational := No_Rational;
2559
2560 function Process_Minus (N : Node_Id) return Rational;
2561 -- Create a rational from a N_Op_Minus node
2562
2563 function Process_Divide (N : Node_Id) return Rational;
2564 -- Create a rational from a N_Op_Divide node
2565
2566 function Process_Literal (N : Node_Id) return Rational;
2567 -- Create a rational from a N_Integer_Literal node
2568
2569 -------------------
2570 -- Process_Minus --
2571 -------------------
2572
2573 function Process_Minus (N : Node_Id) return Rational is
2574 Right : constant Node_Id := Original_Node (Right_Opnd (N));
2575 Result : Rational;
2576
2577 begin
2578 -- Operand is an integer literal
2579
2580 if Nkind (Right) = N_Integer_Literal then
2581 Result := -Process_Literal (Right);
2582
2583 -- Operand is a divide operator
2584
2585 elsif Nkind (Right) = N_Op_Divide then
2586 Result := -Process_Divide (Right);
2587
2588 else
2589 Result := No_Rational;
2590 end if;
2591
2592 return Result;
2593 end Process_Minus;
2594
2595 --------------------
2596 -- Process_Divide --
2597 --------------------
2598
2599 function Process_Divide (N : Node_Id) return Rational is
2600 Left : constant Node_Id := Original_Node (Left_Opnd (N));
2601 Right : constant Node_Id := Original_Node (Right_Opnd (N));
2602 Left_Rat : Rational;
2603 Result : Rational := No_Rational;
2604 Right_Rat : Rational;
2605
2606 begin
2607 -- Both left and right operands are integer literals
2608
2609 if Nkind (Left) = N_Integer_Literal
2610 and then
2611 Nkind (Right) = N_Integer_Literal
2612 then
2613 Left_Rat := Process_Literal (Left);
2614 Right_Rat := Process_Literal (Right);
2615 Result := Left_Rat / Right_Rat;
2616 end if;
2617
2618 return Result;
2619 end Process_Divide;
2620
2621 ---------------------
2622 -- Process_Literal --
2623 ---------------------
2624
2625 function Process_Literal (N : Node_Id) return Rational is
2626 begin
2627 return +Whole (UI_To_Int (Intval (N)));
2628 end Process_Literal;
2629
2630 -- Start of processing for Create_Rational_From
2631
2632 begin
2633 -- Check the expression is either a division of two integers or an
2634 -- integer itself. Note that the check applies to the original node
2635 -- since the node could have already been rewritten.
2636
2637 -- Integer literal case
2638
2639 if Nkind (Or_Node_Of_Expr) = N_Integer_Literal then
2640 Result := Process_Literal (Or_Node_Of_Expr);
2641
2642 -- Divide operator case
2643
2644 elsif Nkind (Or_Node_Of_Expr) = N_Op_Divide then
2645 Result := Process_Divide (Or_Node_Of_Expr);
2646
2647 -- Minus operator case
2648
2649 elsif Nkind (Or_Node_Of_Expr) = N_Op_Minus then
2650 Result := Process_Minus (Or_Node_Of_Expr);
2651 end if;
2652
2653 -- When Expr cannot be interpreted as a rational and Complain is true,
2654 -- generate an error message.
2655
2656 if Complain and then Result = No_Rational then
2657 Error_Msg_N ("rational expected", Expr);
2658 end if;
2659
2660 return Result;
2661 end Create_Rational_From;
2662
2663 -------------------
2664 -- Dimensions_Of --
2665 -------------------
2666
2667 function Dimensions_Of (N : Node_Id) return Dimension_Type is
2668 begin
2669 return Dimension_Table.Get (N);
2670 end Dimensions_Of;
2671
2672 -----------------------
2673 -- Dimensions_Msg_Of --
2674 -----------------------
2675
2676 function Dimensions_Msg_Of
2677 (N : Node_Id;
2678 Description_Needed : Boolean := False) return String
2679 is
2680 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
2681 Dimensions_Msg : Name_Id;
2682 System : System_Type;
2683
2684 begin
2685 -- Initialization of Name_Buffer
2686
2687 Name_Len := 0;
2688
2689 -- N is not dimensionless
2690
2691 if Exists (Dims_Of_N) then
2692 System := System_Of (Base_Type (Etype (N)));
2693
2694 -- When Description_Needed, add to string "has dimension " before the
2695 -- actual dimension.
2696
2697 if Description_Needed then
2698 Add_Str_To_Name_Buffer ("has dimension ");
2699 end if;
2700
2701 Append
2702 (Global_Name_Buffer,
2703 From_Dim_To_Str_Of_Dim_Symbols (Dims_Of_N, System, True));
2704
2705 -- N is dimensionless
2706
2707 -- When Description_Needed, return "is dimensionless"
2708
2709 elsif Description_Needed then
2710 Add_Str_To_Name_Buffer ("is dimensionless");
2711
2712 -- Otherwise, return "'[']"
2713
2714 else
2715 Add_Str_To_Name_Buffer ("'[']");
2716 end if;
2717
2718 Dimensions_Msg := Name_Find;
2719 return Get_Name_String (Dimensions_Msg);
2720 end Dimensions_Msg_Of;
2721
2722 --------------------------
2723 -- Dimension_Table_Hash --
2724 --------------------------
2725
2726 function Dimension_Table_Hash
2727 (Key : Node_Id) return Dimension_Table_Range
2728 is
2729 begin
2730 return Dimension_Table_Range (Key mod 511);
2731 end Dimension_Table_Hash;
2732
2733 -------------------------------------
2734 -- Dim_Warning_For_Numeric_Literal --
2735 -------------------------------------
2736
2737 procedure Dim_Warning_For_Numeric_Literal (N : Node_Id; Typ : Entity_Id) is
2738 begin
2739 -- Consider the literal zero (integer 0 or real 0.0) to be of any
2740 -- dimension.
2741
2742 case Nkind (Original_Node (N)) is
2743 when N_Real_Literal =>
2744 if Expr_Value_R (N) = Ureal_0 then
2745 return;
2746 end if;
2747
2748 when N_Integer_Literal =>
2749 if Expr_Value (N) = Uint_0 then
2750 return;
2751 end if;
2752
2753 when others =>
2754 null;
2755 end case;
2756
2757 -- Initialize name buffer
2758
2759 Name_Len := 0;
2760
2761 Append (Global_Name_Buffer, String_From_Numeric_Literal (N));
2762
2763 -- Insert a blank between the literal and the symbol
2764
2765 Add_Char_To_Name_Buffer (' ');
2766 Append (Global_Name_Buffer, Symbol_Of (Typ));
2767
2768 Error_Msg_Name_1 := Name_Find;
2769 Error_Msg_N ("assumed to be%%??", N);
2770 end Dim_Warning_For_Numeric_Literal;
2771
2772 ----------------------
2773 -- Dimensions_Match --
2774 ----------------------
2775
2776 function Dimensions_Match (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
2777 begin
2778 return
2779 not Has_Dimension_System (Base_Type (T1))
2780 or else Dimensions_Of (T1) = Dimensions_Of (T2);
2781 end Dimensions_Match;
2782
2783 ---------------------------
2784 -- Dimension_System_Root --
2785 ---------------------------
2786
2787 function Dimension_System_Root (T : Entity_Id) return Entity_Id is
2788 Root : Entity_Id;
2789
2790 begin
2791 Root := Base_Type (T);
2792
2793 if Has_Dimension_System (Root) then
2794 return First_Subtype (Root); -- for example Dim_Mks
2795
2796 else
2797 return Empty;
2798 end if;
2799 end Dimension_System_Root;
2800
2801 ----------------------------------------
2802 -- Eval_Op_Expon_For_Dimensioned_Type --
2803 ----------------------------------------
2804
2805 -- Evaluate the expon operator for real dimensioned type.
2806
2807 -- Note that if the exponent is an integer (denominator = 1) the node is
2808 -- evaluated by the regular Eval_Op_Expon routine (see Sem_Eval).
2809
2810 procedure Eval_Op_Expon_For_Dimensioned_Type
2811 (N : Node_Id;
2812 Btyp : Entity_Id)
2813 is
2814 R : constant Node_Id := Right_Opnd (N);
2815 R_Value : Rational := No_Rational;
2816
2817 begin
2818 if Is_Real_Type (Btyp) then
2819 R_Value := Create_Rational_From (R, False);
2820 end if;
2821
2822 -- Check that the exponent is not an integer
2823
2824 if R_Value /= No_Rational and then R_Value.Denominator /= 1 then
2825 Eval_Op_Expon_With_Rational_Exponent (N, R_Value);
2826 else
2827 Eval_Op_Expon (N);
2828 end if;
2829 end Eval_Op_Expon_For_Dimensioned_Type;
2830
2831 ------------------------------------------
2832 -- Eval_Op_Expon_With_Rational_Exponent --
2833 ------------------------------------------
2834
2835 -- For dimensioned operand in exponentiation, exponent is allowed to be a
2836 -- Rational and not only an Integer like for dimensionless operands. For
2837 -- that particular case, the left operand is rewritten as a function call
2838 -- using the function Expon_LLF from s-llflex.ads.
2839
2840 procedure Eval_Op_Expon_With_Rational_Exponent
2841 (N : Node_Id;
2842 Exponent_Value : Rational)
2843 is
2844 Loc : constant Source_Ptr := Sloc (N);
2845 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
2846 L : constant Node_Id := Left_Opnd (N);
2847 Etyp_Of_L : constant Entity_Id := Etype (L);
2848 Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2849 Actual_1 : Node_Id;
2850 Actual_2 : Node_Id;
2851 Dim_Power : Rational;
2852 List_Of_Dims : List_Id;
2853 New_Aspect : Node_Id;
2854 New_Aspects : List_Id;
2855 New_Id : Entity_Id;
2856 New_N : Node_Id;
2857 New_Subtyp_Decl_For_L : Node_Id;
2858 System : System_Type;
2859
2860 begin
2861 -- Case when the operand is not dimensionless
2862
2863 if Exists (Dims_Of_N) then
2864
2865 -- Get the corresponding System_Type to know the exact number of
2866 -- dimensions in the system.
2867
2868 System := System_Of (Btyp_Of_L);
2869
2870 -- Generation of a new subtype with the proper dimensions
2871
2872 -- In order to rewrite the operator as a type conversion, a new
2873 -- dimensioned subtype with the resulting dimensions of the
2874 -- exponentiation must be created.
2875
2876 -- Generate:
2877
2878 -- Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2879 -- System : constant System_Id :=
2880 -- Get_Dimension_System_Id (Btyp_Of_L);
2881 -- Num_Of_Dims : constant Number_Of_Dimensions :=
2882 -- Dimension_Systems.Table (System).Dimension_Count;
2883
2884 -- subtype T is Btyp_Of_L
2885 -- with
2886 -- Dimension => (
2887 -- Dims_Of_N (1).Numerator / Dims_Of_N (1).Denominator,
2888 -- Dims_Of_N (2).Numerator / Dims_Of_N (2).Denominator,
2889 -- ...
2890 -- Dims_Of_N (Num_Of_Dims).Numerator /
2891 -- Dims_Of_N (Num_Of_Dims).Denominator);
2892
2893 -- Step 1: Generate the new aggregate for the aspect Dimension
2894
2895 New_Aspects := Empty_List;
2896
2897 List_Of_Dims := New_List;
2898 for Position in Dims_Of_N'First .. System.Count loop
2899 Dim_Power := Dims_Of_N (Position);
2900 Append_To (List_Of_Dims,
2901 Make_Op_Divide (Loc,
2902 Left_Opnd =>
2903 Make_Integer_Literal (Loc, Int (Dim_Power.Numerator)),
2904 Right_Opnd =>
2905 Make_Integer_Literal (Loc, Int (Dim_Power.Denominator))));
2906 end loop;
2907
2908 -- Step 2: Create the new Aspect Specification for Aspect Dimension
2909
2910 New_Aspect :=
2911 Make_Aspect_Specification (Loc,
2912 Identifier => Make_Identifier (Loc, Name_Dimension),
2913 Expression => Make_Aggregate (Loc, Expressions => List_Of_Dims));
2914
2915 -- Step 3: Make a temporary identifier for the new subtype
2916
2917 New_Id := Make_Temporary (Loc, 'T');
2918 Set_Is_Internal (New_Id);
2919
2920 -- Step 4: Declaration of the new subtype
2921
2922 New_Subtyp_Decl_For_L :=
2923 Make_Subtype_Declaration (Loc,
2924 Defining_Identifier => New_Id,
2925 Subtype_Indication => New_Occurrence_Of (Btyp_Of_L, Loc));
2926
2927 Append (New_Aspect, New_Aspects);
2928 Set_Aspect_Specifications (New_Subtyp_Decl_For_L, New_Aspects);
2929
2930 Analyze (New_Subtyp_Decl_For_L);
2931
2932 -- Case where the operand is dimensionless
2933
2934 else
2935 New_Id := Btyp_Of_L;
2936 end if;
2937
2938 -- Replacement of N by New_N
2939
2940 -- Generate:
2941
2942 -- Actual_1 := Long_Long_Float (L),
2943
2944 -- Actual_2 := Long_Long_Float (Exponent_Value.Numerator) /
2945 -- Long_Long_Float (Exponent_Value.Denominator);
2946
2947 -- (T (Expon_LLF (Actual_1, Actual_2)));
2948
2949 -- where T is the subtype declared in step 1
2950
2951 -- The node is rewritten as a type conversion
2952
2953 -- Step 1: Creation of the two parameters of Expon_LLF function call
2954
2955 Actual_1 :=
2956 Make_Type_Conversion (Loc,
2957 Subtype_Mark => New_Occurrence_Of (Standard_Long_Long_Float, Loc),
2958 Expression => Relocate_Node (L));
2959
2960 Actual_2 :=
2961 Make_Op_Divide (Loc,
2962 Left_Opnd =>
2963 Make_Real_Literal (Loc,
2964 UR_From_Uint (UI_From_Int (Int (Exponent_Value.Numerator)))),
2965 Right_Opnd =>
2966 Make_Real_Literal (Loc,
2967 UR_From_Uint (UI_From_Int (Int (Exponent_Value.Denominator)))));
2968
2969 -- Step 2: Creation of New_N
2970
2971 New_N :=
2972 Make_Type_Conversion (Loc,
2973 Subtype_Mark => New_Occurrence_Of (New_Id, Loc),
2974 Expression =>
2975 Make_Function_Call (Loc,
2976 Name => New_Occurrence_Of (RTE (RE_Expon_LLF), Loc),
2977 Parameter_Associations => New_List (
2978 Actual_1, Actual_2)));
2979
2980 -- Step 3: Rewrite N with the result
2981
2982 Rewrite (N, New_N);
2983 Set_Etype (N, New_Id);
2984 Analyze_And_Resolve (N, New_Id);
2985 end Eval_Op_Expon_With_Rational_Exponent;
2986
2987 ------------
2988 -- Exists --
2989 ------------
2990
2991 function Exists (Dim : Dimension_Type) return Boolean is
2992 begin
2993 return Dim /= Null_Dimension;
2994 end Exists;
2995
2996 function Exists (Str : String_Id) return Boolean is
2997 begin
2998 return Str /= No_String;
2999 end Exists;
3000
3001 function Exists (Sys : System_Type) return Boolean is
3002 begin
3003 return Sys /= Null_System;
3004 end Exists;
3005
3006 ---------------------------------
3007 -- Expand_Put_Call_With_Symbol --
3008 ---------------------------------
3009
3010 -- For procedure Put (resp. Put_Dim_Of) and function Image, defined in
3011 -- System.Dim.Float_IO or System.Dim.Integer_IO, the default string
3012 -- parameter is rewritten to include the unit symbol (or the dimension
3013 -- symbols if not a defined quantity) in the output of a dimensioned
3014 -- object. If a value is already supplied by the user for the parameter
3015 -- Symbol, it is used as is.
3016
3017 -- Case 1. Item is dimensionless
3018
3019 -- * Put : Item appears without a suffix
3020
3021 -- * Put_Dim_Of : the output is []
3022
3023 -- Obj : Mks_Type := 2.6;
3024 -- Put (Obj, 1, 1, 0);
3025 -- Put_Dim_Of (Obj);
3026
3027 -- The corresponding outputs are:
3028 -- $2.6
3029 -- $[]
3030
3031 -- Case 2. Item has a dimension
3032
3033 -- * Put : If the type of Item is a dimensioned subtype whose
3034 -- symbol is not empty, then the symbol appears as a
3035 -- suffix. Otherwise, a new string is created and appears
3036 -- as a suffix of Item. This string results in the
3037 -- successive concatenations between each unit symbol
3038 -- raised by its corresponding dimension power from the
3039 -- dimensions of Item.
3040
3041 -- * Put_Dim_Of : The output is a new string resulting in the successive
3042 -- concatenations between each dimension symbol raised by
3043 -- its corresponding dimension power from the dimensions of
3044 -- Item.
3045
3046 -- subtype Random is Mks_Type
3047 -- with
3048 -- Dimension => (
3049 -- Meter => 3,
3050 -- Candela => -1,
3051 -- others => 0);
3052
3053 -- Obj : Random := 5.0;
3054 -- Put (Obj);
3055 -- Put_Dim_Of (Obj);
3056
3057 -- The corresponding outputs are:
3058 -- $5.0 m**3.cd**(-1)
3059 -- $[l**3.J**(-1)]
3060
3061 -- The function Image returns the string identical to that produced by
3062 -- a call to Put whose first parameter is a string.
3063
3064 procedure Expand_Put_Call_With_Symbol (N : Node_Id) is
3065 Actuals : constant List_Id := Parameter_Associations (N);
3066 Loc : constant Source_Ptr := Sloc (N);
3067 Name_Call : constant Node_Id := Name (N);
3068 New_Actuals : constant List_Id := New_List;
3069 Actual : Node_Id;
3070 Dims_Of_Actual : Dimension_Type;
3071 Etyp : Entity_Id;
3072 New_Str_Lit : Node_Id := Empty;
3073 Symbols : String_Id;
3074
3075 Is_Put_Dim_Of : Boolean := False;
3076 -- This flag is used in order to differentiate routines Put and
3077 -- Put_Dim_Of. Set to True if the procedure is one of the Put_Dim_Of
3078 -- defined in System.Dim.Float_IO or System.Dim.Integer_IO.
3079
3080 function Has_Symbols return Boolean;
3081 -- Return True if the current Put call already has a parameter
3082 -- association for parameter "Symbols" with the correct string of
3083 -- symbols.
3084
3085 function Is_Procedure_Put_Call return Boolean;
3086 -- Return True if the current call is a call of an instantiation of a
3087 -- procedure Put defined in the package System.Dim.Float_IO and
3088 -- System.Dim.Integer_IO.
3089
3090 function Item_Actual return Node_Id;
3091 -- Return the item actual parameter node in the output call
3092
3093 -----------------
3094 -- Has_Symbols --
3095 -----------------
3096
3097 function Has_Symbols return Boolean is
3098 Actual : Node_Id;
3099 Actual_Str : Node_Id;
3100
3101 begin
3102 -- Look for a symbols parameter association in the list of actuals
3103
3104 Actual := First (Actuals);
3105 while Present (Actual) loop
3106
3107 -- Positional parameter association case when the actual is a
3108 -- string literal.
3109
3110 if Nkind (Actual) = N_String_Literal then
3111 Actual_Str := Actual;
3112
3113 -- Named parameter association case when selector name is Symbol
3114
3115 elsif Nkind (Actual) = N_Parameter_Association
3116 and then Chars (Selector_Name (Actual)) = Name_Symbol
3117 then
3118 Actual_Str := Explicit_Actual_Parameter (Actual);
3119
3120 -- Ignore all other cases
3121
3122 else
3123 Actual_Str := Empty;
3124 end if;
3125
3126 if Present (Actual_Str) then
3127
3128 -- Return True if the actual comes from source or if the string
3129 -- of symbols doesn't have the default value (i.e. it is ""),
3130 -- in which case it is used as suffix of the generated string.
3131
3132 if Comes_From_Source (Actual)
3133 or else String_Length (Strval (Actual_Str)) /= 0
3134 then
3135 return True;
3136
3137 else
3138 return False;
3139 end if;
3140 end if;
3141
3142 Next (Actual);
3143 end loop;
3144
3145 -- At this point, the call has no parameter association. Look to the
3146 -- last actual since the symbols parameter is the last one.
3147
3148 return Nkind (Last (Actuals)) = N_String_Literal;
3149 end Has_Symbols;
3150
3151 ---------------------------
3152 -- Is_Procedure_Put_Call --
3153 ---------------------------
3154
3155 function Is_Procedure_Put_Call return Boolean is
3156 Ent : Entity_Id;
3157 Loc : Source_Ptr;
3158
3159 begin
3160 -- There are three different Put (resp. Put_Dim_Of) routines in each
3161 -- generic dim IO package. Verify the current procedure call is one
3162 -- of them.
3163
3164 if Is_Entity_Name (Name_Call) then
3165 Ent := Entity (Name_Call);
3166
3167 -- Get the original subprogram entity following the renaming chain
3168
3169 if Present (Alias (Ent)) then
3170 Ent := Alias (Ent);
3171 end if;
3172
3173 Loc := Sloc (Ent);
3174
3175 -- Check the name of the entity subprogram is Put (resp.
3176 -- Put_Dim_Of) and verify this entity is located in either
3177 -- System.Dim.Float_IO or System.Dim.Integer_IO.
3178
3179 if Loc > No_Location
3180 and then Is_Dim_IO_Package_Entity
3181 (Cunit_Entity (Get_Source_Unit (Loc)))
3182 then
3183 if Chars (Ent) = Name_Put_Dim_Of then
3184 Is_Put_Dim_Of := True;
3185 return True;
3186
3187 elsif Chars (Ent) = Name_Put
3188 or else Chars (Ent) = Name_Image
3189 then
3190 return True;
3191 end if;
3192 end if;
3193 end if;
3194
3195 return False;
3196 end Is_Procedure_Put_Call;
3197
3198 -----------------
3199 -- Item_Actual --
3200 -----------------
3201
3202 function Item_Actual return Node_Id is
3203 Actual : Node_Id;
3204
3205 begin
3206 -- Look for the item actual as a parameter association
3207
3208 Actual := First (Actuals);
3209 while Present (Actual) loop
3210 if Nkind (Actual) = N_Parameter_Association
3211 and then Chars (Selector_Name (Actual)) = Name_Item
3212 then
3213 return Explicit_Actual_Parameter (Actual);
3214 end if;
3215
3216 Next (Actual);
3217 end loop;
3218
3219 -- Case where the item has been defined without an association
3220
3221 Actual := First (Actuals);
3222
3223 -- Depending on the procedure Put, Item actual could be first or
3224 -- second in the list of actuals.
3225
3226 if Has_Dimension_System (Base_Type (Etype (Actual))) then
3227 return Actual;
3228 else
3229 return Next (Actual);
3230 end if;
3231 end Item_Actual;
3232
3233 -- Start of processing for Expand_Put_Call_With_Symbol
3234
3235 begin
3236 if Is_Procedure_Put_Call and then not Has_Symbols then
3237 Actual := Item_Actual;
3238 Dims_Of_Actual := Dimensions_Of (Actual);
3239 Etyp := Etype (Actual);
3240
3241 -- Put_Dim_Of case
3242
3243 if Is_Put_Dim_Of then
3244
3245 -- Check that the item is not dimensionless
3246
3247 -- Create the new String_Literal with the new String_Id generated
3248 -- by the routine From_Dim_To_Str_Of_Dim_Symbols.
3249
3250 if Exists (Dims_Of_Actual) then
3251 New_Str_Lit :=
3252 Make_String_Literal (Loc,
3253 From_Dim_To_Str_Of_Dim_Symbols
3254 (Dims_Of_Actual, System_Of (Base_Type (Etyp))));
3255
3256 -- If dimensionless, the output is []
3257
3258 else
3259 New_Str_Lit :=
3260 Make_String_Literal (Loc, "[]");
3261 end if;
3262
3263 -- Put case
3264
3265 else
3266 -- Add the symbol as a suffix of the value if the subtype has a
3267 -- unit symbol or if the parameter is not dimensionless.
3268
3269 if Exists (Symbol_Of (Etyp)) then
3270 Symbols := Symbol_Of (Etyp);
3271 else
3272 Symbols := From_Dim_To_Str_Of_Unit_Symbols
3273 (Dims_Of_Actual, System_Of (Base_Type (Etyp)));
3274 end if;
3275
3276 -- Check Symbols exists
3277
3278 if Exists (Symbols) then
3279 Start_String;
3280
3281 -- Put a space between the value and the dimension
3282
3283 Store_String_Char (' ');
3284 Store_String_Chars (Symbols);
3285 New_Str_Lit := Make_String_Literal (Loc, End_String);
3286 end if;
3287 end if;
3288
3289 if Present (New_Str_Lit) then
3290
3291 -- Insert all actuals in New_Actuals
3292
3293 Actual := First (Actuals);
3294 while Present (Actual) loop
3295
3296 -- Copy every actuals in New_Actuals except the Symbols
3297 -- parameter association.
3298
3299 if Nkind (Actual) = N_Parameter_Association
3300 and then Chars (Selector_Name (Actual)) /= Name_Symbol
3301 then
3302 Append_To (New_Actuals,
3303 Make_Parameter_Association (Loc,
3304 Selector_Name => New_Copy (Selector_Name (Actual)),
3305 Explicit_Actual_Parameter =>
3306 New_Copy (Explicit_Actual_Parameter (Actual))));
3307
3308 elsif Nkind (Actual) /= N_Parameter_Association then
3309 Append_To (New_Actuals, New_Copy (Actual));
3310 end if;
3311
3312 Next (Actual);
3313 end loop;
3314
3315 -- Create new Symbols param association and append to New_Actuals
3316
3317 Append_To (New_Actuals,
3318 Make_Parameter_Association (Loc,
3319 Selector_Name => Make_Identifier (Loc, Name_Symbol),
3320 Explicit_Actual_Parameter => New_Str_Lit));
3321
3322 -- Rewrite and analyze the procedure call
3323
3324 if Chars (Name_Call) = Name_Image then
3325 Rewrite (N,
3326 Make_Function_Call (Loc,
3327 Name => New_Copy (Name_Call),
3328 Parameter_Associations => New_Actuals));
3329 Analyze_And_Resolve (N);
3330 else
3331 Rewrite (N,
3332 Make_Procedure_Call_Statement (Loc,
3333 Name => New_Copy (Name_Call),
3334 Parameter_Associations => New_Actuals));
3335 Analyze (N);
3336 end if;
3337
3338 end if;
3339 end if;
3340 end Expand_Put_Call_With_Symbol;
3341
3342 ------------------------------------
3343 -- From_Dim_To_Str_Of_Dim_Symbols --
3344 ------------------------------------
3345
3346 -- Given a dimension vector and the corresponding dimension system, create
3347 -- a String_Id to output dimension symbols corresponding to the dimensions
3348 -- Dims. If In_Error_Msg is True, there is a special handling for character
3349 -- asterisk * which is an insertion character in error messages.
3350
3351 function From_Dim_To_Str_Of_Dim_Symbols
3352 (Dims : Dimension_Type;
3353 System : System_Type;
3354 In_Error_Msg : Boolean := False) return String_Id
3355 is
3356 Dim_Power : Rational;
3357 First_Dim : Boolean := True;
3358
3359 procedure Store_String_Oexpon;
3360 -- Store the expon operator symbol "**" in the string. In error
3361 -- messages, asterisk * is a special character and must be quoted
3362 -- to be placed literally into the message.
3363
3364 -------------------------
3365 -- Store_String_Oexpon --
3366 -------------------------
3367
3368 procedure Store_String_Oexpon is
3369 begin
3370 if In_Error_Msg then
3371 Store_String_Chars ("'*'*");
3372 else
3373 Store_String_Chars ("**");
3374 end if;
3375 end Store_String_Oexpon;
3376
3377 -- Start of processing for From_Dim_To_Str_Of_Dim_Symbols
3378
3379 begin
3380 -- Initialization of the new String_Id
3381
3382 Start_String;
3383
3384 -- Store the dimension symbols inside boxes
3385
3386 if In_Error_Msg then
3387 Store_String_Chars ("'[");
3388 else
3389 Store_String_Char ('[');
3390 end if;
3391
3392 for Position in Dimension_Type'Range loop
3393 Dim_Power := Dims (Position);
3394 if Dim_Power /= Zero then
3395
3396 if First_Dim then
3397 First_Dim := False;
3398 else
3399 Store_String_Char ('.');
3400 end if;
3401
3402 Store_String_Chars (System.Dim_Symbols (Position));
3403
3404 -- Positive dimension case
3405
3406 if Dim_Power.Numerator > 0 then
3407
3408 -- Integer case
3409
3410 if Dim_Power.Denominator = 1 then
3411 if Dim_Power.Numerator /= 1 then
3412 Store_String_Oexpon;
3413 Store_String_Int (Int (Dim_Power.Numerator));
3414 end if;
3415
3416 -- Rational case when denominator /= 1
3417
3418 else
3419 Store_String_Oexpon;
3420 Store_String_Char ('(');
3421 Store_String_Int (Int (Dim_Power.Numerator));
3422 Store_String_Char ('/');
3423 Store_String_Int (Int (Dim_Power.Denominator));
3424 Store_String_Char (')');
3425 end if;
3426
3427 -- Negative dimension case
3428
3429 else
3430 Store_String_Oexpon;
3431 Store_String_Char ('(');
3432 Store_String_Char ('-');
3433 Store_String_Int (Int (-Dim_Power.Numerator));
3434
3435 -- Integer case
3436
3437 if Dim_Power.Denominator = 1 then
3438 Store_String_Char (')');
3439
3440 -- Rational case when denominator /= 1
3441
3442 else
3443 Store_String_Char ('/');
3444 Store_String_Int (Int (Dim_Power.Denominator));
3445 Store_String_Char (')');
3446 end if;
3447 end if;
3448 end if;
3449 end loop;
3450
3451 if In_Error_Msg then
3452 Store_String_Chars ("']");
3453 else
3454 Store_String_Char (']');
3455 end if;
3456
3457 return End_String;
3458 end From_Dim_To_Str_Of_Dim_Symbols;
3459
3460 -------------------------------------
3461 -- From_Dim_To_Str_Of_Unit_Symbols --
3462 -------------------------------------
3463
3464 -- Given a dimension vector and the corresponding dimension system,
3465 -- create a String_Id to output the unit symbols corresponding to the
3466 -- dimensions Dims.
3467
3468 function From_Dim_To_Str_Of_Unit_Symbols
3469 (Dims : Dimension_Type;
3470 System : System_Type) return String_Id
3471 is
3472 Dim_Power : Rational;
3473 First_Dim : Boolean := True;
3474
3475 begin
3476 -- Return No_String if dimensionless
3477
3478 if not Exists (Dims) then
3479 return No_String;
3480 end if;
3481
3482 -- Initialization of the new String_Id
3483
3484 Start_String;
3485
3486 for Position in Dimension_Type'Range loop
3487 Dim_Power := Dims (Position);
3488
3489 if Dim_Power /= Zero then
3490 if First_Dim then
3491 First_Dim := False;
3492 else
3493 Store_String_Char ('.');
3494 end if;
3495
3496 Store_String_Chars (System.Unit_Symbols (Position));
3497
3498 -- Positive dimension case
3499
3500 if Dim_Power.Numerator > 0 then
3501
3502 -- Integer case
3503
3504 if Dim_Power.Denominator = 1 then
3505 if Dim_Power.Numerator /= 1 then
3506 Store_String_Chars ("**");
3507 Store_String_Int (Int (Dim_Power.Numerator));
3508 end if;
3509
3510 -- Rational case when denominator /= 1
3511
3512 else
3513 Store_String_Chars ("**");
3514 Store_String_Char ('(');
3515 Store_String_Int (Int (Dim_Power.Numerator));
3516 Store_String_Char ('/');
3517 Store_String_Int (Int (Dim_Power.Denominator));
3518 Store_String_Char (')');
3519 end if;
3520
3521 -- Negative dimension case
3522
3523 else
3524 Store_String_Chars ("**");
3525 Store_String_Char ('(');
3526 Store_String_Char ('-');
3527 Store_String_Int (Int (-Dim_Power.Numerator));
3528
3529 -- Integer case
3530
3531 if Dim_Power.Denominator = 1 then
3532 Store_String_Char (')');
3533
3534 -- Rational case when denominator /= 1
3535
3536 else
3537 Store_String_Char ('/');
3538 Store_String_Int (Int (Dim_Power.Denominator));
3539 Store_String_Char (')');
3540 end if;
3541 end if;
3542 end if;
3543 end loop;
3544
3545 return End_String;
3546 end From_Dim_To_Str_Of_Unit_Symbols;
3547
3548 ---------
3549 -- GCD --
3550 ---------
3551
3552 function GCD (Left, Right : Whole) return Int is
3553 L : Whole;
3554 R : Whole;
3555
3556 begin
3557 L := Left;
3558 R := Right;
3559 while R /= 0 loop
3560 L := L mod R;
3561
3562 if L = 0 then
3563 return Int (R);
3564 end if;
3565
3566 R := R mod L;
3567 end loop;
3568
3569 return Int (L);
3570 end GCD;
3571
3572 --------------------------
3573 -- Has_Dimension_System --
3574 --------------------------
3575
3576 function Has_Dimension_System (Typ : Entity_Id) return Boolean is
3577 begin
3578 return Exists (System_Of (Typ));
3579 end Has_Dimension_System;
3580
3581 ------------------------------
3582 -- Is_Dim_IO_Package_Entity --
3583 ------------------------------
3584
3585 function Is_Dim_IO_Package_Entity (E : Entity_Id) return Boolean is
3586 begin
3587 -- Check the package entity corresponds to System.Dim.Float_IO or
3588 -- System.Dim.Integer_IO.
3589
3590 return
3591 Is_RTU (E, System_Dim_Float_IO)
3592 or else
3593 Is_RTU (E, System_Dim_Integer_IO);
3594 end Is_Dim_IO_Package_Entity;
3595
3596 -------------------------------------
3597 -- Is_Dim_IO_Package_Instantiation --
3598 -------------------------------------
3599
3600 function Is_Dim_IO_Package_Instantiation (N : Node_Id) return Boolean is
3601 Gen_Id : constant Node_Id := Name (N);
3602
3603 begin
3604 -- Check that the instantiated package is either System.Dim.Float_IO
3605 -- or System.Dim.Integer_IO.
3606
3607 return
3608 Is_Entity_Name (Gen_Id)
3609 and then Is_Dim_IO_Package_Entity (Entity (Gen_Id));
3610 end Is_Dim_IO_Package_Instantiation;
3611
3612 ----------------
3613 -- Is_Invalid --
3614 ----------------
3615
3616 function Is_Invalid (Position : Dimension_Position) return Boolean is
3617 begin
3618 return Position = Invalid_Position;
3619 end Is_Invalid;
3620
3621 ---------------------
3622 -- Move_Dimensions --
3623 ---------------------
3624
3625 procedure Move_Dimensions (From, To : Node_Id) is
3626 begin
3627 if Ada_Version < Ada_2012 then
3628 return;
3629 end if;
3630
3631 -- Copy the dimension of 'From to 'To' and remove dimension of 'From'
3632
3633 Copy_Dimensions (From, To);
3634 Remove_Dimensions (From);
3635 end Move_Dimensions;
3636
3637 ---------------------------------------
3638 -- New_Copy_Tree_And_Copy_Dimensions --
3639 ---------------------------------------
3640
3641 function New_Copy_Tree_And_Copy_Dimensions
3642 (Source : Node_Id;
3643 Map : Elist_Id := No_Elist;
3644 New_Sloc : Source_Ptr := No_Location;
3645 New_Scope : Entity_Id := Empty) return Node_Id
3646 is
3647 New_Copy : constant Node_Id :=
3648 New_Copy_Tree (Source, Map, New_Sloc, New_Scope);
3649
3650 begin
3651 -- Move the dimensions of Source to New_Copy
3652
3653 Copy_Dimensions (Source, New_Copy);
3654 return New_Copy;
3655 end New_Copy_Tree_And_Copy_Dimensions;
3656
3657 ------------
3658 -- Reduce --
3659 ------------
3660
3661 function Reduce (X : Rational) return Rational is
3662 begin
3663 if X.Numerator = 0 then
3664 return Zero;
3665 end if;
3666
3667 declare
3668 G : constant Int := GCD (X.Numerator, X.Denominator);
3669 begin
3670 return Rational'(Numerator => Whole (Int (X.Numerator) / G),
3671 Denominator => Whole (Int (X.Denominator) / G));
3672 end;
3673 end Reduce;
3674
3675 -----------------------
3676 -- Remove_Dimensions --
3677 -----------------------
3678
3679 procedure Remove_Dimensions (N : Node_Id) is
3680 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
3681 begin
3682 if Exists (Dims_Of_N) then
3683 Dimension_Table.Remove (N);
3684 end if;
3685 end Remove_Dimensions;
3686
3687 -----------------------------------
3688 -- Remove_Dimension_In_Statement --
3689 -----------------------------------
3690
3691 -- Removal of dimension in statement as part of the Analyze_Statements
3692 -- routine (see package Sem_Ch5).
3693
3694 procedure Remove_Dimension_In_Statement (Stmt : Node_Id) is
3695 begin
3696 if Ada_Version < Ada_2012 then
3697 return;
3698 end if;
3699
3700 -- Remove dimension in parameter specifications for accept statement
3701
3702 if Nkind (Stmt) = N_Accept_Statement then
3703 declare
3704 Param : Node_Id := First (Parameter_Specifications (Stmt));
3705 begin
3706 while Present (Param) loop
3707 Remove_Dimensions (Param);
3708 Next (Param);
3709 end loop;
3710 end;
3711
3712 -- Remove dimension of name and expression in assignments
3713
3714 elsif Nkind (Stmt) = N_Assignment_Statement then
3715 Remove_Dimensions (Expression (Stmt));
3716 Remove_Dimensions (Name (Stmt));
3717 end if;
3718 end Remove_Dimension_In_Statement;
3719
3720 --------------------
3721 -- Set_Dimensions --
3722 --------------------
3723
3724 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type) is
3725 begin
3726 pragma Assert (OK_For_Dimension (Nkind (N)));
3727 pragma Assert (Exists (Val));
3728
3729 Dimension_Table.Set (N, Val);
3730 end Set_Dimensions;
3731
3732 ----------------
3733 -- Set_Symbol --
3734 ----------------
3735
3736 procedure Set_Symbol (E : Entity_Id; Val : String_Id) is
3737 begin
3738 Symbol_Table.Set (E, Val);
3739 end Set_Symbol;
3740
3741 ---------------
3742 -- Symbol_Of --
3743 ---------------
3744
3745 function Symbol_Of (E : Entity_Id) return String_Id is
3746 Subtype_Symbol : constant String_Id := Symbol_Table.Get (E);
3747 begin
3748 if Subtype_Symbol /= No_String then
3749 return Subtype_Symbol;
3750 else
3751 return From_Dim_To_Str_Of_Unit_Symbols
3752 (Dimensions_Of (E), System_Of (Base_Type (E)));
3753 end if;
3754 end Symbol_Of;
3755
3756 -----------------------
3757 -- Symbol_Table_Hash --
3758 -----------------------
3759
3760 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range is
3761 begin
3762 return Symbol_Table_Range (Key mod 511);
3763 end Symbol_Table_Hash;
3764
3765 ---------------
3766 -- System_Of --
3767 ---------------
3768
3769 function System_Of (E : Entity_Id) return System_Type is
3770 begin
3771 if Present (E) then
3772 declare
3773 Type_Decl : constant Node_Id := Parent (E);
3774 begin
3775 -- Look for Type_Decl in System_Table
3776
3777 for Dim_Sys in 1 .. System_Table.Last loop
3778 if Type_Decl = System_Table.Table (Dim_Sys).Type_Decl then
3779 return System_Table.Table (Dim_Sys);
3780 end if;
3781 end loop;
3782 end;
3783 end if;
3784
3785 return Null_System;
3786 end System_Of;
3787
3788 end Sem_Dim;