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