PR rtl-optimization/79386
[official-gcc.git] / gcc / ada / sem_dim.adb
blob2c57bcb5227190ff1cb1c83c3e408e8c847cd3ed
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-2016, 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 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Util; use Exp_Util;
31 with Lib; use Lib;
32 with Namet; use Namet;
33 with Nlists; use Nlists;
34 with Nmake; use Nmake;
35 with Opt; use Opt;
36 with Rtsfind; use Rtsfind;
37 with Sem; use Sem;
38 with Sem_Eval; use Sem_Eval;
39 with Sem_Res; use Sem_Res;
40 with Sem_Util; use Sem_Util;
41 with Sinfo; use Sinfo;
42 with Sinput; use Sinput;
43 with Snames; use Snames;
44 with Stand; use Stand;
45 with Stringt; use Stringt;
46 with Table;
47 with Tbuild; use Tbuild;
48 with Uintp; use Uintp;
49 with Urealp; use Urealp;
51 with GNAT.HTable;
53 package body Sem_Dim is
55 -------------------------
56 -- Rational Arithmetic --
57 -------------------------
59 type Whole is new Int;
60 subtype Positive_Whole is Whole range 1 .. Whole'Last;
62 type Rational is record
63 Numerator : Whole;
64 Denominator : Positive_Whole;
65 end record;
67 Zero : constant Rational := Rational'(Numerator => 0,
68 Denominator => 1);
70 No_Rational : constant Rational := Rational'(Numerator => 0,
71 Denominator => 2);
72 -- Used to indicate an expression that cannot be interpreted as a rational
73 -- Returned value of the Create_Rational_From routine when parameter Expr
74 -- is not a static representation of a rational.
76 -- Rational constructors
78 function "+" (Right : Whole) return Rational;
79 function GCD (Left, Right : Whole) return Int;
80 function Reduce (X : Rational) return Rational;
82 -- Unary operator for Rational
84 function "-" (Right : Rational) return Rational;
85 function "abs" (Right : Rational) return Rational;
87 -- Rational operations for Rationals
89 function "+" (Left, Right : Rational) return Rational;
90 function "-" (Left, Right : Rational) return Rational;
91 function "*" (Left, Right : Rational) return Rational;
92 function "/" (Left, Right : Rational) return Rational;
94 ------------------
95 -- System Types --
96 ------------------
98 Max_Number_Of_Dimensions : constant := 7;
99 -- Maximum number of dimensions in a dimension system
101 High_Position_Bound : constant := Max_Number_Of_Dimensions;
102 Invalid_Position : constant := 0;
103 Low_Position_Bound : constant := 1;
105 subtype Dimension_Position is
106 Nat range Invalid_Position .. High_Position_Bound;
108 type Name_Array is
109 array (Dimension_Position range
110 Low_Position_Bound .. High_Position_Bound) of Name_Id;
111 -- Store the names of all units within a system
113 No_Names : constant Name_Array := (others => No_Name);
115 type Symbol_Array is
116 array (Dimension_Position range
117 Low_Position_Bound .. High_Position_Bound) of String_Id;
118 -- Store the symbols of all units within a system
120 No_Symbols : constant Symbol_Array := (others => No_String);
122 -- The following record should be documented field by field
124 type System_Type is record
125 Type_Decl : Node_Id;
126 Unit_Names : Name_Array;
127 Unit_Symbols : Symbol_Array;
128 Dim_Symbols : Symbol_Array;
129 Count : Dimension_Position;
130 end record;
132 Null_System : constant System_Type :=
133 (Empty, No_Names, No_Symbols, No_Symbols, Invalid_Position);
135 subtype System_Id is Nat;
137 -- The following table maps types to systems
139 package System_Table is new Table.Table (
140 Table_Component_Type => System_Type,
141 Table_Index_Type => System_Id,
142 Table_Low_Bound => 1,
143 Table_Initial => 5,
144 Table_Increment => 5,
145 Table_Name => "System_Table");
147 --------------------
148 -- Dimension Type --
149 --------------------
151 type Dimension_Type is
152 array (Dimension_Position range
153 Low_Position_Bound .. High_Position_Bound) of Rational;
155 Null_Dimension : constant Dimension_Type := (others => Zero);
157 type Dimension_Table_Range is range 0 .. 510;
158 function Dimension_Table_Hash (Key : Node_Id) return Dimension_Table_Range;
160 -- The following table associates nodes with dimensions
162 package Dimension_Table is new
163 GNAT.HTable.Simple_HTable
164 (Header_Num => Dimension_Table_Range,
165 Element => Dimension_Type,
166 No_Element => Null_Dimension,
167 Key => Node_Id,
168 Hash => Dimension_Table_Hash,
169 Equal => "=");
171 ------------------
172 -- Symbol Types --
173 ------------------
175 type Symbol_Table_Range is range 0 .. 510;
176 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range;
178 -- Each subtype with a dimension has a symbolic representation of the
179 -- related unit. This table establishes a relation between the subtype
180 -- and the symbol.
182 package Symbol_Table is new
183 GNAT.HTable.Simple_HTable
184 (Header_Num => Symbol_Table_Range,
185 Element => String_Id,
186 No_Element => No_String,
187 Key => Entity_Id,
188 Hash => Symbol_Table_Hash,
189 Equal => "=");
191 -- The following array enumerates all contexts which may contain or
192 -- produce a dimension.
194 OK_For_Dimension : constant array (Node_Kind) of Boolean :=
195 (N_Attribute_Reference => True,
196 N_Expanded_Name => True,
197 N_Explicit_Dereference => True,
198 N_Defining_Identifier => True,
199 N_Function_Call => True,
200 N_Identifier => True,
201 N_Indexed_Component => True,
202 N_Integer_Literal => True,
203 N_Op_Abs => True,
204 N_Op_Add => True,
205 N_Op_Divide => True,
206 N_Op_Expon => True,
207 N_Op_Minus => True,
208 N_Op_Mod => True,
209 N_Op_Multiply => True,
210 N_Op_Plus => True,
211 N_Op_Rem => True,
212 N_Op_Subtract => True,
213 N_Qualified_Expression => True,
214 N_Real_Literal => True,
215 N_Selected_Component => True,
216 N_Slice => True,
217 N_Type_Conversion => True,
218 N_Unchecked_Type_Conversion => True,
220 others => False);
222 -----------------------
223 -- Local Subprograms --
224 -----------------------
226 procedure Analyze_Dimension_Assignment_Statement (N : Node_Id);
227 -- Subroutine of Analyze_Dimension for assignment statement. Check that the
228 -- dimensions of the left-hand side and the right-hand side of N match.
230 procedure Analyze_Dimension_Binary_Op (N : Node_Id);
231 -- Subroutine of Analyze_Dimension for binary operators. Check the
232 -- dimensions of the right and the left operand permit the operation.
233 -- Then, evaluate the resulting dimensions for each binary operator.
235 procedure Analyze_Dimension_Component_Declaration (N : Node_Id);
236 -- Subroutine of Analyze_Dimension for component declaration. Check that
237 -- the dimensions of the type of N and of the expression match.
239 procedure Analyze_Dimension_Extended_Return_Statement (N : Node_Id);
240 -- Subroutine of Analyze_Dimension for extended return statement. Check
241 -- that the dimensions of the returned type and of the returned object
242 -- match.
244 procedure Analyze_Dimension_Has_Etype (N : Node_Id);
245 -- Subroutine of Analyze_Dimension for a subset of N_Has_Etype denoted by
246 -- the list below:
247 -- N_Attribute_Reference
248 -- N_Identifier
249 -- N_Indexed_Component
250 -- N_Qualified_Expression
251 -- N_Selected_Component
252 -- N_Slice
253 -- N_Type_Conversion
254 -- N_Unchecked_Type_Conversion
256 procedure Analyze_Dimension_Number_Declaration (N : Node_Id);
257 -- Procedure to analyze dimension of expression in a number declaration.
258 -- This allows a named number to have nontrivial dimensions, while by
259 -- default a named number is dimensionless.
261 procedure Analyze_Dimension_Object_Declaration (N : Node_Id);
262 -- Subroutine of Analyze_Dimension for object declaration. Check that
263 -- the dimensions of the object type and the dimensions of the expression
264 -- (if expression is present) match. Note that when the expression is
265 -- a literal, no error is returned. This special case allows object
266 -- declaration such as: m : constant Length := 1.0;
268 procedure Analyze_Dimension_Object_Renaming_Declaration (N : Node_Id);
269 -- Subroutine of Analyze_Dimension for object renaming declaration. Check
270 -- the dimensions of the type and of the renamed object name of N match.
272 procedure Analyze_Dimension_Simple_Return_Statement (N : Node_Id);
273 -- Subroutine of Analyze_Dimension for simple return statement
274 -- Check that the dimensions of the returned type and of the returned
275 -- expression match.
277 procedure Analyze_Dimension_Subtype_Declaration (N : Node_Id);
278 -- Subroutine of Analyze_Dimension for subtype declaration. Propagate the
279 -- dimensions from the parent type to the identifier of N. Note that if
280 -- both the identifier and the parent type of N are not dimensionless,
281 -- return an error.
283 procedure Analyze_Dimension_Unary_Op (N : Node_Id);
284 -- Subroutine of Analyze_Dimension for unary operators. For Plus, Minus and
285 -- Abs operators, propagate the dimensions from the operand to N.
287 function Create_Rational_From
288 (Expr : Node_Id;
289 Complain : Boolean) return Rational;
290 -- Given an arbitrary expression Expr, return a valid rational if Expr can
291 -- be interpreted as a rational. Otherwise return No_Rational and also an
292 -- error message if Complain is set to True.
294 function Dimensions_Of (N : Node_Id) return Dimension_Type;
295 -- Return the dimension vector of node N
297 function Dimensions_Msg_Of
298 (N : Node_Id;
299 Description_Needed : Boolean := False) return String;
300 -- Given a node N, return the dimension symbols of N, preceded by "has
301 -- dimension" if Description_Needed. if N is dimensionless, return "'[']",
302 -- or "is dimensionless" if Description_Needed.
304 procedure Dim_Warning_For_Numeric_Literal (N : Node_Id; Typ : Entity_Id);
305 -- Issue a warning on the given numeric literal N to indicate that the
306 -- compiler made the assumption that the literal is not dimensionless
307 -- but has the dimension of Typ.
309 procedure Eval_Op_Expon_With_Rational_Exponent
310 (N : Node_Id;
311 Exponent_Value : Rational);
312 -- Evaluate the exponent it is a rational and the operand has a dimension
314 function Exists (Dim : Dimension_Type) return Boolean;
315 -- Returns True iff Dim does not denote the null dimension
317 function Exists (Str : String_Id) return Boolean;
318 -- Returns True iff Str does not denote No_String
320 function Exists (Sys : System_Type) return Boolean;
321 -- Returns True iff Sys does not denote the null system
323 function From_Dim_To_Str_Of_Dim_Symbols
324 (Dims : Dimension_Type;
325 System : System_Type;
326 In_Error_Msg : Boolean := False) return String_Id;
327 -- Given a dimension vector and a dimension system, return the proper
328 -- string of dimension symbols. If In_Error_Msg is True (i.e. the String_Id
329 -- will be used to issue an error message) then this routine has a special
330 -- handling for the insertion characters * or [ which must be preceded by
331 -- a quote ' to be placed literally into the message.
333 function From_Dim_To_Str_Of_Unit_Symbols
334 (Dims : Dimension_Type;
335 System : System_Type) return String_Id;
336 -- Given a dimension vector and a dimension system, return the proper
337 -- string of unit symbols.
339 function Is_Dim_IO_Package_Entity (E : Entity_Id) return Boolean;
340 -- Return True if E is the package entity of System.Dim.Float_IO or
341 -- System.Dim.Integer_IO.
343 function Is_Invalid (Position : Dimension_Position) return Boolean;
344 -- Return True if Pos denotes the invalid position
346 procedure Move_Dimensions (From : Node_Id; To : Node_Id);
347 -- Copy dimension vector of From to To and delete dimension vector of From
349 procedure Remove_Dimensions (N : Node_Id);
350 -- Remove the dimension vector of node N
352 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type);
353 -- Associate a dimension vector with a node
355 procedure Set_Symbol (E : Entity_Id; Val : String_Id);
356 -- Associate a symbol representation of a dimension vector with a subtype
358 function String_From_Numeric_Literal (N : Node_Id) return String_Id;
359 -- Return the string that corresponds to the numeric litteral N as it
360 -- appears in the source.
362 function Symbol_Of (E : Entity_Id) return String_Id;
363 -- E denotes a subtype with a dimension. Return the symbol representation
364 -- of the dimension vector.
366 function System_Of (E : Entity_Id) return System_Type;
367 -- E denotes a type, return associated system of the type if it has one
369 ---------
370 -- "+" --
371 ---------
373 function "+" (Right : Whole) return Rational is
374 begin
375 return Rational'(Numerator => Right, Denominator => 1);
376 end "+";
378 function "+" (Left, Right : Rational) return Rational is
379 R : constant Rational :=
380 Rational'(Numerator => Left.Numerator * Right.Denominator +
381 Left.Denominator * Right.Numerator,
382 Denominator => Left.Denominator * Right.Denominator);
383 begin
384 return Reduce (R);
385 end "+";
387 ---------
388 -- "-" --
389 ---------
391 function "-" (Right : Rational) return Rational is
392 begin
393 return Rational'(Numerator => -Right.Numerator,
394 Denominator => Right.Denominator);
395 end "-";
397 function "-" (Left, Right : Rational) return Rational is
398 R : constant Rational :=
399 Rational'(Numerator => Left.Numerator * Right.Denominator -
400 Left.Denominator * Right.Numerator,
401 Denominator => Left.Denominator * Right.Denominator);
403 begin
404 return Reduce (R);
405 end "-";
407 ---------
408 -- "*" --
409 ---------
411 function "*" (Left, Right : Rational) return Rational is
412 R : constant Rational :=
413 Rational'(Numerator => Left.Numerator * Right.Numerator,
414 Denominator => Left.Denominator * Right.Denominator);
415 begin
416 return Reduce (R);
417 end "*";
419 ---------
420 -- "/" --
421 ---------
423 function "/" (Left, Right : Rational) return Rational is
424 R : constant Rational := abs Right;
425 L : Rational := Left;
427 begin
428 if Right.Numerator < 0 then
429 L.Numerator := Whole (-Integer (L.Numerator));
430 end if;
432 return Reduce (Rational'(Numerator => L.Numerator * R.Denominator,
433 Denominator => L.Denominator * R.Numerator));
434 end "/";
436 -----------
437 -- "abs" --
438 -----------
440 function "abs" (Right : Rational) return Rational is
441 begin
442 return Rational'(Numerator => abs Right.Numerator,
443 Denominator => Right.Denominator);
444 end "abs";
446 ------------------------------
447 -- Analyze_Aspect_Dimension --
448 ------------------------------
450 -- with Dimension =>
451 -- ([Symbol =>] SYMBOL, DIMENSION_VALUE {, DIMENSION_Value})
453 -- SYMBOL ::= STRING_LITERAL | CHARACTER_LITERAL
455 -- DIMENSION_VALUE ::=
456 -- RATIONAL
457 -- | others => RATIONAL
458 -- | DISCRETE_CHOICE_LIST => RATIONAL
460 -- RATIONAL ::= [-] NUMERIC_LITERAL [/ NUMERIC_LITERAL]
462 -- Note that when the dimensioned type is an integer type, then any
463 -- dimension value must be an integer literal.
465 procedure Analyze_Aspect_Dimension
466 (N : Node_Id;
467 Id : Entity_Id;
468 Aggr : Node_Id)
470 Def_Id : constant Entity_Id := Defining_Identifier (N);
472 Processed : array (Dimension_Type'Range) of Boolean := (others => False);
473 -- This array is used when processing ranges or Others_Choice as part of
474 -- the dimension aggregate.
476 Dimensions : Dimension_Type := Null_Dimension;
478 procedure Extract_Power
479 (Expr : Node_Id;
480 Position : Dimension_Position);
481 -- Given an expression with denotes a rational number, read the number
482 -- and associate it with Position in Dimensions.
484 function Position_In_System
485 (Id : Node_Id;
486 System : System_Type) return Dimension_Position;
487 -- Given an identifier which denotes a dimension, return the position of
488 -- that dimension within System.
490 -------------------
491 -- Extract_Power --
492 -------------------
494 procedure Extract_Power
495 (Expr : Node_Id;
496 Position : Dimension_Position)
498 begin
499 -- Integer case
501 if Is_Integer_Type (Def_Id) then
503 -- Dimension value must be an integer literal
505 if Nkind (Expr) = N_Integer_Literal then
506 Dimensions (Position) := +Whole (UI_To_Int (Intval (Expr)));
507 else
508 Error_Msg_N ("integer literal expected", Expr);
509 end if;
511 -- Float case
513 else
514 Dimensions (Position) := Create_Rational_From (Expr, True);
515 end if;
517 Processed (Position) := True;
518 end Extract_Power;
520 ------------------------
521 -- Position_In_System --
522 ------------------------
524 function Position_In_System
525 (Id : Node_Id;
526 System : System_Type) return Dimension_Position
528 Dimension_Name : constant Name_Id := Chars (Id);
530 begin
531 for Position in System.Unit_Names'Range loop
532 if Dimension_Name = System.Unit_Names (Position) then
533 return Position;
534 end if;
535 end loop;
537 return Invalid_Position;
538 end Position_In_System;
540 -- Local variables
542 Assoc : Node_Id;
543 Choice : Node_Id;
544 Expr : Node_Id;
545 Num_Choices : Nat := 0;
546 Num_Dimensions : Nat := 0;
547 Others_Seen : Boolean := False;
548 Position : Nat := 0;
549 Sub_Ind : Node_Id;
550 Symbol : String_Id := No_String;
551 Symbol_Expr : Node_Id;
552 System : System_Type;
553 Typ : Entity_Id;
555 Errors_Count : Nat;
556 -- Errors_Count is a count of errors detected by the compiler so far
557 -- just before the extraction of symbol, names and values in the
558 -- aggregate (Step 2).
560 -- At the end of the analysis, there is a check to verify that this
561 -- count equals to Serious_Errors_Detected i.e. no erros have been
562 -- encountered during the process. Otherwise the Dimension_Table is
563 -- not filled.
565 -- Start of processing for Analyze_Aspect_Dimension
567 begin
568 -- STEP 1: Legality of aspect
570 if Nkind (N) /= N_Subtype_Declaration then
571 Error_Msg_NE ("aspect& must apply to subtype declaration", N, Id);
572 return;
573 end if;
575 Sub_Ind := Subtype_Indication (N);
576 Typ := Etype (Sub_Ind);
577 System := System_Of (Typ);
579 if Nkind (Sub_Ind) = N_Subtype_Indication then
580 Error_Msg_NE
581 ("constraint not allowed with aspect&", Constraint (Sub_Ind), Id);
582 return;
583 end if;
585 -- The dimension declarations are useless if the parent type does not
586 -- declare a valid system.
588 if not Exists (System) then
589 Error_Msg_NE
590 ("parent type of& lacks dimension system", Sub_Ind, Def_Id);
591 return;
592 end if;
594 if Nkind (Aggr) /= N_Aggregate then
595 Error_Msg_N ("aggregate expected", Aggr);
596 return;
597 end if;
599 -- STEP 2: Symbol, Names and values extraction
601 -- Get the number of errors detected by the compiler so far
603 Errors_Count := Serious_Errors_Detected;
605 -- STEP 2a: Symbol extraction
607 -- The first entry in the aggregate may be the symbolic representation
608 -- of the quantity.
610 -- Positional symbol argument
612 Symbol_Expr := First (Expressions (Aggr));
614 -- Named symbol argument
616 if No (Symbol_Expr)
617 or else not Nkind_In (Symbol_Expr, N_Character_Literal,
618 N_String_Literal)
619 then
620 Symbol_Expr := Empty;
622 -- Component associations present
624 if Present (Component_Associations (Aggr)) then
625 Assoc := First (Component_Associations (Aggr));
626 Choice := First (Choices (Assoc));
628 if No (Next (Choice)) and then Nkind (Choice) = N_Identifier then
630 -- Symbol component association is present
632 if Chars (Choice) = Name_Symbol then
633 Num_Choices := Num_Choices + 1;
634 Symbol_Expr := Expression (Assoc);
636 -- Verify symbol expression is a string or a character
638 if not Nkind_In (Symbol_Expr, N_Character_Literal,
639 N_String_Literal)
640 then
641 Symbol_Expr := Empty;
642 Error_Msg_N
643 ("symbol expression must be character or string",
644 Symbol_Expr);
645 end if;
647 -- Special error if no Symbol choice but expression is string
648 -- or character.
650 elsif Nkind_In (Expression (Assoc), N_Character_Literal,
651 N_String_Literal)
652 then
653 Num_Choices := Num_Choices + 1;
654 Error_Msg_N
655 ("optional component Symbol expected, found&", Choice);
656 end if;
657 end if;
658 end if;
659 end if;
661 -- STEP 2b: Names and values extraction
663 -- Positional elements
665 Expr := First (Expressions (Aggr));
667 -- Skip the symbol expression when present
669 if Present (Symbol_Expr) and then Num_Choices = 0 then
670 Expr := Next (Expr);
671 end if;
673 Position := Low_Position_Bound;
674 while Present (Expr) loop
675 if Position > High_Position_Bound then
676 Error_Msg_N
677 ("type& has more dimensions than system allows", Def_Id);
678 exit;
679 end if;
681 Extract_Power (Expr, Position);
683 Position := Position + 1;
684 Num_Dimensions := Num_Dimensions + 1;
686 Next (Expr);
687 end loop;
689 -- Named elements
691 Assoc := First (Component_Associations (Aggr));
693 -- Skip the symbol association when present
695 if Num_Choices = 1 then
696 Next (Assoc);
697 end if;
699 while Present (Assoc) loop
700 Expr := Expression (Assoc);
702 Choice := First (Choices (Assoc));
703 while Present (Choice) loop
705 -- Identifier case: NAME => EXPRESSION
707 if Nkind (Choice) = N_Identifier then
708 Position := Position_In_System (Choice, System);
710 if Is_Invalid (Position) then
711 Error_Msg_N ("dimension name& not part of system", Choice);
712 else
713 Extract_Power (Expr, Position);
714 end if;
716 -- Range case: NAME .. NAME => EXPRESSION
718 elsif Nkind (Choice) = N_Range then
719 declare
720 Low : constant Node_Id := Low_Bound (Choice);
721 High : constant Node_Id := High_Bound (Choice);
722 Low_Pos : Dimension_Position;
723 High_Pos : Dimension_Position;
725 begin
726 if Nkind (Low) /= N_Identifier then
727 Error_Msg_N ("bound must denote a dimension name", Low);
729 elsif Nkind (High) /= N_Identifier then
730 Error_Msg_N ("bound must denote a dimension name", High);
732 else
733 Low_Pos := Position_In_System (Low, System);
734 High_Pos := Position_In_System (High, System);
736 if Is_Invalid (Low_Pos) then
737 Error_Msg_N ("dimension name& not part of system",
738 Low);
740 elsif Is_Invalid (High_Pos) then
741 Error_Msg_N ("dimension name& not part of system",
742 High);
744 elsif Low_Pos > High_Pos then
745 Error_Msg_N ("expected low to high range", Choice);
747 else
748 for Position in Low_Pos .. High_Pos loop
749 Extract_Power (Expr, Position);
750 end loop;
751 end if;
752 end if;
753 end;
755 -- Others case: OTHERS => EXPRESSION
757 elsif Nkind (Choice) = N_Others_Choice then
758 if Present (Next (Choice)) or else Present (Prev (Choice)) then
759 Error_Msg_N
760 ("OTHERS must appear alone in a choice list", Choice);
762 elsif Present (Next (Assoc)) then
763 Error_Msg_N
764 ("OTHERS must appear last in an aggregate", Choice);
766 elsif Others_Seen then
767 Error_Msg_N ("multiple OTHERS not allowed", Choice);
769 else
770 -- Fill the non-processed dimensions with the default value
771 -- supplied by others.
773 for Position in Processed'Range loop
774 if not Processed (Position) then
775 Extract_Power (Expr, Position);
776 end if;
777 end loop;
778 end if;
780 Others_Seen := True;
782 -- All other cases are illegal declarations of dimension names
784 else
785 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
786 end if;
788 Num_Choices := Num_Choices + 1;
789 Next (Choice);
790 end loop;
792 Num_Dimensions := Num_Dimensions + 1;
793 Next (Assoc);
794 end loop;
796 -- STEP 3: Consistency of system and dimensions
798 if Present (First (Expressions (Aggr)))
799 and then (First (Expressions (Aggr)) /= Symbol_Expr
800 or else Present (Next (Symbol_Expr)))
801 and then (Num_Choices > 1
802 or else (Num_Choices = 1 and then not Others_Seen))
803 then
804 Error_Msg_N
805 ("named associations cannot follow positional associations", Aggr);
806 end if;
808 if Num_Dimensions > System.Count then
809 Error_Msg_N ("type& has more dimensions than system allows", Def_Id);
811 elsif Num_Dimensions < System.Count and then not Others_Seen then
812 Error_Msg_N ("type& has less dimensions than system allows", Def_Id);
813 end if;
815 -- STEP 4: Dimension symbol extraction
817 if Present (Symbol_Expr) then
818 if Nkind (Symbol_Expr) = N_Character_Literal then
819 Start_String;
820 Store_String_Char (UI_To_CC (Char_Literal_Value (Symbol_Expr)));
821 Symbol := End_String;
823 else
824 Symbol := Strval (Symbol_Expr);
825 end if;
827 if String_Length (Symbol) = 0 then
828 Error_Msg_N ("empty string not allowed here", Symbol_Expr);
829 end if;
830 end if;
832 -- STEP 5: Storage of extracted values
834 -- Check that no errors have been detected during the analysis
836 if Errors_Count = Serious_Errors_Detected then
838 -- Check for useless declaration
840 if Symbol = No_String and then not Exists (Dimensions) then
841 Error_Msg_N ("useless dimension declaration", Aggr);
842 end if;
844 if Symbol /= No_String then
845 Set_Symbol (Def_Id, Symbol);
846 end if;
848 if Exists (Dimensions) then
849 Set_Dimensions (Def_Id, Dimensions);
850 end if;
851 end if;
852 end Analyze_Aspect_Dimension;
854 -------------------------------------
855 -- Analyze_Aspect_Dimension_System --
856 -------------------------------------
858 -- with Dimension_System => (DIMENSION {, DIMENSION});
860 -- DIMENSION ::= (
861 -- [Unit_Name =>] IDENTIFIER,
862 -- [Unit_Symbol =>] SYMBOL,
863 -- [Dim_Symbol =>] SYMBOL)
865 procedure Analyze_Aspect_Dimension_System
866 (N : Node_Id;
867 Id : Entity_Id;
868 Aggr : Node_Id)
870 function Is_Derived_Numeric_Type (N : Node_Id) return Boolean;
871 -- Determine whether type declaration N denotes a numeric derived type
873 -------------------------------
874 -- Is_Derived_Numeric_Type --
875 -------------------------------
877 function Is_Derived_Numeric_Type (N : Node_Id) return Boolean is
878 begin
879 return
880 Nkind (N) = N_Full_Type_Declaration
881 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
882 and then Is_Numeric_Type
883 (Entity (Subtype_Indication (Type_Definition (N))));
884 end Is_Derived_Numeric_Type;
886 -- Local variables
888 Assoc : Node_Id;
889 Choice : Node_Id;
890 Dim_Aggr : Node_Id;
891 Dim_Symbol : Node_Id;
892 Dim_Symbols : Symbol_Array := No_Symbols;
893 Dim_System : System_Type := Null_System;
894 Position : Nat := 0;
895 Unit_Name : Node_Id;
896 Unit_Names : Name_Array := No_Names;
897 Unit_Symbol : Node_Id;
898 Unit_Symbols : Symbol_Array := No_Symbols;
900 Errors_Count : Nat;
901 -- Errors_Count is a count of errors detected by the compiler so far
902 -- just before the extraction of names and symbols in the aggregate
903 -- (Step 3).
905 -- At the end of the analysis, there is a check to verify that this
906 -- count equals Serious_Errors_Detected i.e. no errors have been
907 -- encountered during the process. Otherwise the System_Table is
908 -- not filled.
910 -- Start of processing for Analyze_Aspect_Dimension_System
912 begin
913 -- STEP 1: Legality of aspect
915 if not Is_Derived_Numeric_Type (N) then
916 Error_Msg_NE
917 ("aspect& must apply to numeric derived type declaration", N, Id);
918 return;
919 end if;
921 if Nkind (Aggr) /= N_Aggregate then
922 Error_Msg_N ("aggregate expected", Aggr);
923 return;
924 end if;
926 -- STEP 2: Structural verification of the dimension aggregate
928 if Present (Component_Associations (Aggr)) then
929 Error_Msg_N ("expected positional aggregate", Aggr);
930 return;
931 end if;
933 -- STEP 3: Name and Symbol extraction
935 Dim_Aggr := First (Expressions (Aggr));
936 Errors_Count := Serious_Errors_Detected;
937 while Present (Dim_Aggr) loop
938 Position := Position + 1;
940 if Position > High_Position_Bound then
941 Error_Msg_N ("too many dimensions in system", Aggr);
942 exit;
943 end if;
945 if Nkind (Dim_Aggr) /= N_Aggregate then
946 Error_Msg_N ("aggregate expected", Dim_Aggr);
948 else
949 if Present (Component_Associations (Dim_Aggr))
950 and then Present (Expressions (Dim_Aggr))
951 then
952 Error_Msg_N
953 ("mixed positional/named aggregate not allowed here",
954 Dim_Aggr);
956 -- Verify each dimension aggregate has three arguments
958 elsif List_Length (Component_Associations (Dim_Aggr)) /= 3
959 and then List_Length (Expressions (Dim_Aggr)) /= 3
960 then
961 Error_Msg_N
962 ("three components expected in aggregate", Dim_Aggr);
964 else
965 -- Named dimension aggregate
967 if Present (Component_Associations (Dim_Aggr)) then
969 -- Check first argument denotes the unit name
971 Assoc := First (Component_Associations (Dim_Aggr));
972 Choice := First (Choices (Assoc));
973 Unit_Name := Expression (Assoc);
975 if Present (Next (Choice))
976 or else Nkind (Choice) /= N_Identifier
977 then
978 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
980 elsif Chars (Choice) /= Name_Unit_Name then
981 Error_Msg_N ("expected Unit_Name, found&", Choice);
982 end if;
984 -- Check the second argument denotes the unit symbol
986 Next (Assoc);
987 Choice := First (Choices (Assoc));
988 Unit_Symbol := Expression (Assoc);
990 if Present (Next (Choice))
991 or else Nkind (Choice) /= N_Identifier
992 then
993 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
995 elsif Chars (Choice) /= Name_Unit_Symbol then
996 Error_Msg_N ("expected Unit_Symbol, found&", Choice);
997 end if;
999 -- Check the third argument denotes the dimension symbol
1001 Next (Assoc);
1002 Choice := First (Choices (Assoc));
1003 Dim_Symbol := Expression (Assoc);
1005 if Present (Next (Choice))
1006 or else Nkind (Choice) /= N_Identifier
1007 then
1008 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
1009 elsif Chars (Choice) /= Name_Dim_Symbol then
1010 Error_Msg_N ("expected Dim_Symbol, found&", Choice);
1011 end if;
1013 -- Positional dimension aggregate
1015 else
1016 Unit_Name := First (Expressions (Dim_Aggr));
1017 Unit_Symbol := Next (Unit_Name);
1018 Dim_Symbol := Next (Unit_Symbol);
1019 end if;
1021 -- Check the first argument for each dimension aggregate is
1022 -- a name.
1024 if Nkind (Unit_Name) = N_Identifier then
1025 Unit_Names (Position) := Chars (Unit_Name);
1026 else
1027 Error_Msg_N ("expected unit name", Unit_Name);
1028 end if;
1030 -- Check the second argument for each dimension aggregate is
1031 -- a string or a character.
1033 if not Nkind_In (Unit_Symbol, N_String_Literal,
1034 N_Character_Literal)
1035 then
1036 Error_Msg_N
1037 ("expected unit symbol (string or character)",
1038 Unit_Symbol);
1040 else
1041 -- String case
1043 if Nkind (Unit_Symbol) = N_String_Literal then
1044 Unit_Symbols (Position) := Strval (Unit_Symbol);
1046 -- Character case
1048 else
1049 Start_String;
1050 Store_String_Char
1051 (UI_To_CC (Char_Literal_Value (Unit_Symbol)));
1052 Unit_Symbols (Position) := End_String;
1053 end if;
1055 -- Verify that the string is not empty
1057 if String_Length (Unit_Symbols (Position)) = 0 then
1058 Error_Msg_N
1059 ("empty string not allowed here", Unit_Symbol);
1060 end if;
1061 end if;
1063 -- Check the third argument for each dimension aggregate is
1064 -- a string or a character.
1066 if not Nkind_In (Dim_Symbol, N_String_Literal,
1067 N_Character_Literal)
1068 then
1069 Error_Msg_N
1070 ("expected dimension symbol (string or character)",
1071 Dim_Symbol);
1073 else
1074 -- String case
1076 if Nkind (Dim_Symbol) = N_String_Literal then
1077 Dim_Symbols (Position) := Strval (Dim_Symbol);
1079 -- Character case
1081 else
1082 Start_String;
1083 Store_String_Char
1084 (UI_To_CC (Char_Literal_Value (Dim_Symbol)));
1085 Dim_Symbols (Position) := End_String;
1086 end if;
1088 -- Verify that the string is not empty
1090 if String_Length (Dim_Symbols (Position)) = 0 then
1091 Error_Msg_N ("empty string not allowed here", Dim_Symbol);
1092 end if;
1093 end if;
1094 end if;
1095 end if;
1097 Next (Dim_Aggr);
1098 end loop;
1100 -- STEP 4: Storage of extracted values
1102 -- Check that no errors have been detected during the analysis
1104 if Errors_Count = Serious_Errors_Detected then
1105 Dim_System.Type_Decl := N;
1106 Dim_System.Unit_Names := Unit_Names;
1107 Dim_System.Unit_Symbols := Unit_Symbols;
1108 Dim_System.Dim_Symbols := Dim_Symbols;
1109 Dim_System.Count := Position;
1110 System_Table.Append (Dim_System);
1111 end if;
1112 end Analyze_Aspect_Dimension_System;
1114 -----------------------
1115 -- Analyze_Dimension --
1116 -----------------------
1118 -- This dispatch routine propagates dimensions for each node
1120 procedure Analyze_Dimension (N : Node_Id) is
1121 begin
1122 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1123 -- dimensions for nodes that don't come from source, except for subtype
1124 -- declarations where the dimensions are inherited from the base type,
1125 -- for explicit dereferences generated when expanding iterators, and
1126 -- for object declarations generated for inlining.
1128 if Ada_Version < Ada_2012 then
1129 return;
1131 elsif not Comes_From_Source (N) then
1132 if Nkind_In (N, N_Explicit_Dereference,
1133 N_Identifier,
1134 N_Object_Declaration,
1135 N_Subtype_Declaration)
1136 then
1137 null;
1138 else
1139 return;
1140 end if;
1141 end if;
1143 case Nkind (N) is
1144 when N_Assignment_Statement =>
1145 Analyze_Dimension_Assignment_Statement (N);
1147 when N_Binary_Op =>
1148 Analyze_Dimension_Binary_Op (N);
1150 when N_Component_Declaration =>
1151 Analyze_Dimension_Component_Declaration (N);
1153 when N_Extended_Return_Statement =>
1154 Analyze_Dimension_Extended_Return_Statement (N);
1156 when N_Attribute_Reference
1157 | N_Expanded_Name
1158 | N_Explicit_Dereference
1159 | N_Function_Call
1160 | N_Indexed_Component
1161 | N_Qualified_Expression
1162 | N_Selected_Component
1163 | N_Slice
1164 | N_Type_Conversion
1165 | N_Unchecked_Type_Conversion
1167 Analyze_Dimension_Has_Etype (N);
1169 -- In the presence of a repaired syntax error, an identifier
1170 -- may be introduced without a usable type.
1172 when N_Identifier =>
1173 if Present (Etype (N)) then
1174 Analyze_Dimension_Has_Etype (N);
1175 end if;
1177 when N_Number_Declaration =>
1178 Analyze_Dimension_Number_Declaration (N);
1180 when N_Object_Declaration =>
1181 Analyze_Dimension_Object_Declaration (N);
1183 when N_Object_Renaming_Declaration =>
1184 Analyze_Dimension_Object_Renaming_Declaration (N);
1186 when N_Simple_Return_Statement =>
1187 if not Comes_From_Extended_Return_Statement (N) then
1188 Analyze_Dimension_Simple_Return_Statement (N);
1189 end if;
1191 when N_Subtype_Declaration =>
1192 Analyze_Dimension_Subtype_Declaration (N);
1194 when N_Unary_Op =>
1195 Analyze_Dimension_Unary_Op (N);
1197 when others =>
1198 null;
1199 end case;
1200 end Analyze_Dimension;
1202 ---------------------------------------
1203 -- Analyze_Dimension_Array_Aggregate --
1204 ---------------------------------------
1206 procedure Analyze_Dimension_Array_Aggregate
1207 (N : Node_Id;
1208 Comp_Typ : Entity_Id)
1210 Comp_Ass : constant List_Id := Component_Associations (N);
1211 Dims_Of_Comp_Typ : constant Dimension_Type := Dimensions_Of (Comp_Typ);
1212 Exps : constant List_Id := Expressions (N);
1214 Comp : Node_Id;
1215 Expr : Node_Id;
1217 Error_Detected : Boolean := False;
1218 -- This flag is used in order to indicate if an error has been detected
1219 -- so far by the compiler in this routine.
1221 begin
1222 -- Aspect is an Ada 2012 feature. Nothing to do here if the component
1223 -- base type is not a dimensioned type.
1225 -- Note that here the original node must come from source since the
1226 -- original array aggregate may not have been entirely decorated.
1228 if Ada_Version < Ada_2012
1229 or else not Comes_From_Source (Original_Node (N))
1230 or else not Has_Dimension_System (Base_Type (Comp_Typ))
1231 then
1232 return;
1233 end if;
1235 -- Check whether there is any positional component association
1237 if Is_Empty_List (Exps) then
1238 Comp := First (Comp_Ass);
1239 else
1240 Comp := First (Exps);
1241 end if;
1243 while Present (Comp) loop
1245 -- Get the expression from the component
1247 if Nkind (Comp) = N_Component_Association then
1248 Expr := Expression (Comp);
1249 else
1250 Expr := Comp;
1251 end if;
1253 -- Issue an error if the dimensions of the component type and the
1254 -- dimensions of the component mismatch.
1256 -- Note that we must ensure the expression has been fully analyzed
1257 -- since it may not be decorated at this point. We also don't want to
1258 -- issue the same error message multiple times on the same expression
1259 -- (may happen when an aggregate is converted into a positional
1260 -- aggregate). We also must verify that this is a scalar component,
1261 -- and not a subaggregate of a multidimensional aggregate.
1263 if Comes_From_Source (Original_Node (Expr))
1264 and then Present (Etype (Expr))
1265 and then Is_Numeric_Type (Etype (Expr))
1266 and then Dimensions_Of (Expr) /= Dims_Of_Comp_Typ
1267 and then Sloc (Comp) /= Sloc (Prev (Comp))
1268 then
1269 -- Check if an error has already been encountered so far
1271 if not Error_Detected then
1272 Error_Msg_N ("dimensions mismatch in array aggregate", N);
1273 Error_Detected := True;
1274 end if;
1276 Error_Msg_N
1277 ("\expected dimension " & Dimensions_Msg_Of (Comp_Typ)
1278 & ", found " & Dimensions_Msg_Of (Expr), Expr);
1279 end if;
1281 -- Look at the named components right after the positional components
1283 if not Present (Next (Comp))
1284 and then List_Containing (Comp) = Exps
1285 then
1286 Comp := First (Comp_Ass);
1287 else
1288 Next (Comp);
1289 end if;
1290 end loop;
1291 end Analyze_Dimension_Array_Aggregate;
1293 --------------------------------------------
1294 -- Analyze_Dimension_Assignment_Statement --
1295 --------------------------------------------
1297 procedure Analyze_Dimension_Assignment_Statement (N : Node_Id) is
1298 Lhs : constant Node_Id := Name (N);
1299 Dims_Of_Lhs : constant Dimension_Type := Dimensions_Of (Lhs);
1300 Rhs : constant Node_Id := Expression (N);
1301 Dims_Of_Rhs : constant Dimension_Type := Dimensions_Of (Rhs);
1303 procedure Error_Dim_Msg_For_Assignment_Statement
1304 (N : Node_Id;
1305 Lhs : Node_Id;
1306 Rhs : Node_Id);
1307 -- Error using Error_Msg_N at node N. Output the dimensions of left
1308 -- and right hand sides.
1310 --------------------------------------------
1311 -- Error_Dim_Msg_For_Assignment_Statement --
1312 --------------------------------------------
1314 procedure Error_Dim_Msg_For_Assignment_Statement
1315 (N : Node_Id;
1316 Lhs : Node_Id;
1317 Rhs : Node_Id)
1319 begin
1320 Error_Msg_N ("dimensions mismatch in assignment", N);
1321 Error_Msg_N ("\left-hand side " & Dimensions_Msg_Of (Lhs, True), N);
1322 Error_Msg_N ("\right-hand side " & Dimensions_Msg_Of (Rhs, True), N);
1323 end Error_Dim_Msg_For_Assignment_Statement;
1325 -- Start of processing for Analyze_Dimension_Assignment
1327 begin
1328 if Dims_Of_Lhs /= Dims_Of_Rhs then
1329 Error_Dim_Msg_For_Assignment_Statement (N, Lhs, Rhs);
1330 end if;
1331 end Analyze_Dimension_Assignment_Statement;
1333 ---------------------------------
1334 -- Analyze_Dimension_Binary_Op --
1335 ---------------------------------
1337 -- Check and propagate the dimensions for binary operators
1338 -- Note that when the dimensions mismatch, no dimension is propagated to N.
1340 procedure Analyze_Dimension_Binary_Op (N : Node_Id) is
1341 N_Kind : constant Node_Kind := Nkind (N);
1343 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type;
1344 -- If the operand is a numeric literal that comes from a declared
1345 -- constant, use the dimensions of the constant which were computed
1346 -- from the expression of the constant declaration.
1348 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id);
1349 -- Error using Error_Msg_NE and Error_Msg_N at node N. Output the
1350 -- dimensions of both operands.
1352 ---------------------------
1353 -- Dimensions_Of_Operand --
1354 ---------------------------
1356 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type is
1357 begin
1358 if Nkind (N) = N_Real_Literal
1359 and then Present (Original_Entity (N))
1360 then
1361 return Dimensions_Of (Original_Entity (N));
1362 else
1363 return Dimensions_Of (N);
1364 end if;
1365 end Dimensions_Of_Operand;
1367 ---------------------------------
1368 -- Error_Dim_Msg_For_Binary_Op --
1369 ---------------------------------
1371 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id) is
1372 begin
1373 Error_Msg_NE
1374 ("both operands for operation& must have same dimensions",
1375 N, Entity (N));
1376 Error_Msg_N ("\left operand " & Dimensions_Msg_Of (L, True), N);
1377 Error_Msg_N ("\right operand " & Dimensions_Msg_Of (R, True), N);
1378 end Error_Dim_Msg_For_Binary_Op;
1380 -- Start of processing for Analyze_Dimension_Binary_Op
1382 begin
1383 if Nkind_In (N_Kind, N_Op_Add, N_Op_Expon, N_Op_Subtract)
1384 or else N_Kind in N_Multiplying_Operator
1385 or else N_Kind in N_Op_Compare
1386 then
1387 declare
1388 L : constant Node_Id := Left_Opnd (N);
1389 Dims_Of_L : constant Dimension_Type :=
1390 Dimensions_Of_Operand (L);
1391 L_Has_Dimensions : constant Boolean := Exists (Dims_Of_L);
1392 R : constant Node_Id := Right_Opnd (N);
1393 Dims_Of_R : constant Dimension_Type :=
1394 Dimensions_Of_Operand (R);
1395 R_Has_Dimensions : constant Boolean := Exists (Dims_Of_R);
1396 Dims_Of_N : Dimension_Type := Null_Dimension;
1398 begin
1399 -- N_Op_Add, N_Op_Mod, N_Op_Rem or N_Op_Subtract case
1401 if Nkind_In (N, N_Op_Add, N_Op_Mod, N_Op_Rem, N_Op_Subtract) then
1403 -- Check both operands have same dimension
1405 if Dims_Of_L /= Dims_Of_R then
1406 Error_Dim_Msg_For_Binary_Op (N, L, R);
1407 else
1408 -- Check both operands are not dimensionless
1410 if Exists (Dims_Of_L) then
1411 Set_Dimensions (N, Dims_Of_L);
1412 end if;
1413 end if;
1415 -- N_Op_Multiply or N_Op_Divide case
1417 elsif Nkind_In (N_Kind, N_Op_Multiply, N_Op_Divide) then
1419 -- Check at least one operand is not dimensionless
1421 if L_Has_Dimensions or R_Has_Dimensions then
1423 -- Multiplication case
1425 -- Get both operands dimensions and add them
1427 if N_Kind = N_Op_Multiply then
1428 for Position in Dimension_Type'Range loop
1429 Dims_Of_N (Position) :=
1430 Dims_Of_L (Position) + Dims_Of_R (Position);
1431 end loop;
1433 -- Division case
1435 -- Get both operands dimensions and subtract them
1437 else
1438 for Position in Dimension_Type'Range loop
1439 Dims_Of_N (Position) :=
1440 Dims_Of_L (Position) - Dims_Of_R (Position);
1441 end loop;
1442 end if;
1444 if Exists (Dims_Of_N) then
1445 Set_Dimensions (N, Dims_Of_N);
1446 end if;
1447 end if;
1449 -- Exponentiation case
1451 -- Note: a rational exponent is allowed for dimensioned operand
1453 elsif N_Kind = N_Op_Expon then
1455 -- Check the left operand is not dimensionless. Note that the
1456 -- value of the exponent must be known compile time. Otherwise,
1457 -- the exponentiation evaluation will return an error message.
1459 if L_Has_Dimensions then
1460 if not Compile_Time_Known_Value (R) then
1461 Error_Msg_N
1462 ("exponent of dimensioned operand must be "
1463 & "known at compile time", N);
1464 end if;
1466 declare
1467 Exponent_Value : Rational := Zero;
1469 begin
1470 -- Real operand case
1472 if Is_Real_Type (Etype (L)) then
1474 -- Define the exponent as a Rational number
1476 Exponent_Value := Create_Rational_From (R, False);
1478 -- Verify that the exponent cannot be interpreted
1479 -- as a rational, otherwise interpret the exponent
1480 -- as an integer.
1482 if Exponent_Value = No_Rational then
1483 Exponent_Value :=
1484 +Whole (UI_To_Int (Expr_Value (R)));
1485 end if;
1487 -- Integer operand case.
1489 -- For integer operand, the exponent cannot be
1490 -- interpreted as a rational.
1492 else
1493 Exponent_Value := +Whole (UI_To_Int (Expr_Value (R)));
1494 end if;
1496 for Position in Dimension_Type'Range loop
1497 Dims_Of_N (Position) :=
1498 Dims_Of_L (Position) * Exponent_Value;
1499 end loop;
1501 if Exists (Dims_Of_N) then
1502 Set_Dimensions (N, Dims_Of_N);
1503 end if;
1504 end;
1505 end if;
1507 -- Comparison cases
1509 -- For relational operations, only dimension checking is
1510 -- performed (no propagation). If one operand is the result
1511 -- of constant folding the dimensions may have been lost
1512 -- in a tree copy, so assume that pre-analysis has verified
1513 -- that dimensions are correct.
1515 elsif N_Kind in N_Op_Compare then
1516 if (L_Has_Dimensions or R_Has_Dimensions)
1517 and then Dims_Of_L /= Dims_Of_R
1518 then
1519 if Nkind (L) = N_Real_Literal
1520 and then not (Comes_From_Source (L))
1521 and then Expander_Active
1522 then
1523 null;
1525 elsif Nkind (R) = N_Real_Literal
1526 and then not (Comes_From_Source (R))
1527 and then Expander_Active
1528 then
1529 null;
1531 else
1532 Error_Dim_Msg_For_Binary_Op (N, L, R);
1533 end if;
1534 end if;
1535 end if;
1537 -- If expander is active, remove dimension information from each
1538 -- operand, as only dimensions of result are relevant.
1540 if Expander_Active then
1541 Remove_Dimensions (L);
1542 Remove_Dimensions (R);
1543 end if;
1544 end;
1545 end if;
1546 end Analyze_Dimension_Binary_Op;
1548 ----------------------------
1549 -- Analyze_Dimension_Call --
1550 ----------------------------
1552 procedure Analyze_Dimension_Call (N : Node_Id; Nam : Entity_Id) is
1553 Actuals : constant List_Id := Parameter_Associations (N);
1554 Actual : Node_Id;
1555 Dims_Of_Formal : Dimension_Type;
1556 Formal : Node_Id;
1557 Formal_Typ : Entity_Id;
1559 Error_Detected : Boolean := False;
1560 -- This flag is used in order to indicate if an error has been detected
1561 -- so far by the compiler in this routine.
1563 begin
1564 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1565 -- dimensions for calls that don't come from source, or those that may
1566 -- have semantic errors.
1568 if Ada_Version < Ada_2012
1569 or else not Comes_From_Source (N)
1570 or else Error_Posted (N)
1571 then
1572 return;
1573 end if;
1575 -- Check the dimensions of the actuals, if any
1577 if not Is_Empty_List (Actuals) then
1579 -- Special processing for elementary functions
1581 -- For Sqrt call, the resulting dimensions equal to half the
1582 -- dimensions of the actual. For all other elementary calls, this
1583 -- routine check that every actual is dimensionless.
1585 if Nkind (N) = N_Function_Call then
1586 Elementary_Function_Calls : declare
1587 Dims_Of_Call : Dimension_Type;
1588 Ent : Entity_Id := Nam;
1590 function Is_Elementary_Function_Entity
1591 (Sub_Id : Entity_Id) return Boolean;
1592 -- Given Sub_Id, the original subprogram entity, return True
1593 -- if call is to an elementary function (see Ada.Numerics.
1594 -- Generic_Elementary_Functions).
1596 -----------------------------------
1597 -- Is_Elementary_Function_Entity --
1598 -----------------------------------
1600 function Is_Elementary_Function_Entity
1601 (Sub_Id : Entity_Id) return Boolean
1603 Loc : constant Source_Ptr := Sloc (Sub_Id);
1605 begin
1606 -- Is entity in Ada.Numerics.Generic_Elementary_Functions?
1608 return
1609 Loc > No_Location
1610 and then
1611 Is_RTU
1612 (Cunit_Entity (Get_Source_Unit (Loc)),
1613 Ada_Numerics_Generic_Elementary_Functions);
1614 end Is_Elementary_Function_Entity;
1616 -- Start of processing for Elementary_Function_Calls
1618 begin
1619 -- Get original subprogram entity following the renaming chain
1621 if Present (Alias (Ent)) then
1622 Ent := Alias (Ent);
1623 end if;
1625 -- Check the call is an Elementary function call
1627 if Is_Elementary_Function_Entity (Ent) then
1629 -- Sqrt function call case
1631 if Chars (Ent) = Name_Sqrt then
1632 Dims_Of_Call := Dimensions_Of (First_Actual (N));
1634 -- Evaluates the resulting dimensions (i.e. half the
1635 -- dimensions of the actual).
1637 if Exists (Dims_Of_Call) then
1638 for Position in Dims_Of_Call'Range loop
1639 Dims_Of_Call (Position) :=
1640 Dims_Of_Call (Position) *
1641 Rational'(Numerator => 1, Denominator => 2);
1642 end loop;
1644 Set_Dimensions (N, Dims_Of_Call);
1645 end if;
1647 -- All other elementary functions case. Note that every
1648 -- actual here should be dimensionless.
1650 else
1651 Actual := First_Actual (N);
1652 while Present (Actual) loop
1653 if Exists (Dimensions_Of (Actual)) then
1655 -- Check if error has already been encountered
1657 if not Error_Detected then
1658 Error_Msg_NE
1659 ("dimensions mismatch in call of&",
1660 N, Name (N));
1661 Error_Detected := True;
1662 end if;
1664 Error_Msg_N
1665 ("\expected dimension '['], found "
1666 & Dimensions_Msg_Of (Actual), Actual);
1667 end if;
1669 Next_Actual (Actual);
1670 end loop;
1671 end if;
1673 -- Nothing more to do for elementary functions
1675 return;
1676 end if;
1677 end Elementary_Function_Calls;
1678 end if;
1680 -- General case. Check, for each parameter, the dimensions of the
1681 -- actual and its corresponding formal match. Otherwise, complain.
1683 Actual := First_Actual (N);
1684 Formal := First_Formal (Nam);
1685 while Present (Formal) loop
1687 -- A missing corresponding actual indicates that the analysis of
1688 -- the call was aborted due to a previous error.
1690 if No (Actual) then
1691 Check_Error_Detected;
1692 return;
1693 end if;
1695 Formal_Typ := Etype (Formal);
1696 Dims_Of_Formal := Dimensions_Of (Formal_Typ);
1698 -- If the formal is not dimensionless, check dimensions of formal
1699 -- and actual match. Otherwise, complain.
1701 if Exists (Dims_Of_Formal)
1702 and then Dimensions_Of (Actual) /= Dims_Of_Formal
1703 then
1704 -- Check if an error has already been encountered so far
1706 if not Error_Detected then
1707 Error_Msg_NE ("dimensions mismatch in& call", N, Name (N));
1708 Error_Detected := True;
1709 end if;
1711 Error_Msg_N
1712 ("\expected dimension " & Dimensions_Msg_Of (Formal_Typ)
1713 & ", found " & Dimensions_Msg_Of (Actual), Actual);
1714 end if;
1716 Next_Actual (Actual);
1717 Next_Formal (Formal);
1718 end loop;
1719 end if;
1721 -- For function calls, propagate the dimensions from the returned type
1723 if Nkind (N) = N_Function_Call then
1724 Analyze_Dimension_Has_Etype (N);
1725 end if;
1726 end Analyze_Dimension_Call;
1728 ---------------------------------------------
1729 -- Analyze_Dimension_Component_Declaration --
1730 ---------------------------------------------
1732 procedure Analyze_Dimension_Component_Declaration (N : Node_Id) is
1733 Expr : constant Node_Id := Expression (N);
1734 Id : constant Entity_Id := Defining_Identifier (N);
1735 Etyp : constant Entity_Id := Etype (Id);
1736 Dims_Of_Etyp : constant Dimension_Type := Dimensions_Of (Etyp);
1737 Dims_Of_Expr : Dimension_Type;
1739 procedure Error_Dim_Msg_For_Component_Declaration
1740 (N : Node_Id;
1741 Etyp : Entity_Id;
1742 Expr : Node_Id);
1743 -- Error using Error_Msg_N at node N. Output the dimensions of the
1744 -- type Etyp and the expression Expr of N.
1746 ---------------------------------------------
1747 -- Error_Dim_Msg_For_Component_Declaration --
1748 ---------------------------------------------
1750 procedure Error_Dim_Msg_For_Component_Declaration
1751 (N : Node_Id;
1752 Etyp : Entity_Id;
1753 Expr : Node_Id) is
1754 begin
1755 Error_Msg_N ("dimensions mismatch in component declaration", N);
1756 Error_Msg_N
1757 ("\expected dimension " & Dimensions_Msg_Of (Etyp) & ", found "
1758 & Dimensions_Msg_Of (Expr), Expr);
1759 end Error_Dim_Msg_For_Component_Declaration;
1761 -- Start of processing for Analyze_Dimension_Component_Declaration
1763 begin
1764 -- Expression is present
1766 if Present (Expr) then
1767 Dims_Of_Expr := Dimensions_Of (Expr);
1769 -- Check dimensions match
1771 if Dims_Of_Etyp /= Dims_Of_Expr then
1773 -- Numeric literal case. Issue a warning if the object type is not
1774 -- dimensionless to indicate the literal is treated as if its
1775 -- dimension matches the type dimension.
1777 if Nkind_In (Original_Node (Expr), N_Real_Literal,
1778 N_Integer_Literal)
1779 then
1780 Dim_Warning_For_Numeric_Literal (Expr, Etyp);
1782 -- Issue a dimension mismatch error for all other cases
1784 else
1785 Error_Dim_Msg_For_Component_Declaration (N, Etyp, Expr);
1786 end if;
1787 end if;
1788 end if;
1789 end Analyze_Dimension_Component_Declaration;
1791 -------------------------------------------------
1792 -- Analyze_Dimension_Extended_Return_Statement --
1793 -------------------------------------------------
1795 procedure Analyze_Dimension_Extended_Return_Statement (N : Node_Id) is
1796 Return_Ent : constant Entity_Id := Return_Statement_Entity (N);
1797 Return_Etyp : constant Entity_Id :=
1798 Etype (Return_Applies_To (Return_Ent));
1799 Return_Obj_Decls : constant List_Id := Return_Object_Declarations (N);
1800 Return_Obj_Decl : Node_Id;
1801 Return_Obj_Id : Entity_Id;
1802 Return_Obj_Typ : Entity_Id;
1804 procedure Error_Dim_Msg_For_Extended_Return_Statement
1805 (N : Node_Id;
1806 Return_Etyp : Entity_Id;
1807 Return_Obj_Typ : Entity_Id);
1808 -- Error using Error_Msg_N at node N. Output dimensions of the returned
1809 -- type Return_Etyp and the returned object type Return_Obj_Typ of N.
1811 -------------------------------------------------
1812 -- Error_Dim_Msg_For_Extended_Return_Statement --
1813 -------------------------------------------------
1815 procedure Error_Dim_Msg_For_Extended_Return_Statement
1816 (N : Node_Id;
1817 Return_Etyp : Entity_Id;
1818 Return_Obj_Typ : Entity_Id)
1820 begin
1821 Error_Msg_N ("dimensions mismatch in extended return statement", N);
1822 Error_Msg_N
1823 ("\expected dimension " & Dimensions_Msg_Of (Return_Etyp)
1824 & ", found " & Dimensions_Msg_Of (Return_Obj_Typ), N);
1825 end Error_Dim_Msg_For_Extended_Return_Statement;
1827 -- Start of processing for Analyze_Dimension_Extended_Return_Statement
1829 begin
1830 if Present (Return_Obj_Decls) then
1831 Return_Obj_Decl := First (Return_Obj_Decls);
1832 while Present (Return_Obj_Decl) loop
1833 if Nkind (Return_Obj_Decl) = N_Object_Declaration then
1834 Return_Obj_Id := Defining_Identifier (Return_Obj_Decl);
1836 if Is_Return_Object (Return_Obj_Id) then
1837 Return_Obj_Typ := Etype (Return_Obj_Id);
1839 -- Issue an error message if dimensions mismatch
1841 if Dimensions_Of (Return_Etyp) /=
1842 Dimensions_Of (Return_Obj_Typ)
1843 then
1844 Error_Dim_Msg_For_Extended_Return_Statement
1845 (N, Return_Etyp, Return_Obj_Typ);
1846 return;
1847 end if;
1848 end if;
1849 end if;
1851 Next (Return_Obj_Decl);
1852 end loop;
1853 end if;
1854 end Analyze_Dimension_Extended_Return_Statement;
1856 -----------------------------------------------------
1857 -- Analyze_Dimension_Extension_Or_Record_Aggregate --
1858 -----------------------------------------------------
1860 procedure Analyze_Dimension_Extension_Or_Record_Aggregate (N : Node_Id) is
1861 Comp : Node_Id;
1862 Comp_Id : Entity_Id;
1863 Comp_Typ : Entity_Id;
1864 Expr : Node_Id;
1866 Error_Detected : Boolean := False;
1867 -- This flag is used in order to indicate if an error has been detected
1868 -- so far by the compiler in this routine.
1870 begin
1871 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1872 -- dimensions for aggregates that don't come from source, or if we are
1873 -- within an initialization procedure, whose expressions have been
1874 -- checked at the point of record declaration.
1876 if Ada_Version < Ada_2012
1877 or else not Comes_From_Source (N)
1878 or else Inside_Init_Proc
1879 then
1880 return;
1881 end if;
1883 Comp := First (Component_Associations (N));
1884 while Present (Comp) loop
1885 Comp_Id := Entity (First (Choices (Comp)));
1886 Comp_Typ := Etype (Comp_Id);
1888 -- Check the component type is either a dimensioned type or a
1889 -- dimensioned subtype.
1891 if Has_Dimension_System (Base_Type (Comp_Typ)) then
1892 Expr := Expression (Comp);
1894 -- A box-initialized component needs no checking.
1896 if No (Expr) and then Box_Present (Comp) then
1897 null;
1899 -- Issue an error if the dimensions of the component type and the
1900 -- dimensions of the component mismatch.
1902 elsif Dimensions_Of (Expr) /= Dimensions_Of (Comp_Typ) then
1904 -- Check if an error has already been encountered so far
1906 if not Error_Detected then
1908 -- Extension aggregate case
1910 if Nkind (N) = N_Extension_Aggregate then
1911 Error_Msg_N
1912 ("dimensions mismatch in extension aggregate", N);
1914 -- Record aggregate case
1916 else
1917 Error_Msg_N
1918 ("dimensions mismatch in record aggregate", N);
1919 end if;
1921 Error_Detected := True;
1922 end if;
1924 Error_Msg_N
1925 ("\expected dimension " & Dimensions_Msg_Of (Comp_Typ)
1926 & ", found " & Dimensions_Msg_Of (Expr), Comp);
1927 end if;
1928 end if;
1930 Next (Comp);
1931 end loop;
1932 end Analyze_Dimension_Extension_Or_Record_Aggregate;
1934 -------------------------------
1935 -- Analyze_Dimension_Formals --
1936 -------------------------------
1938 procedure Analyze_Dimension_Formals (N : Node_Id; Formals : List_Id) is
1939 Dims_Of_Typ : Dimension_Type;
1940 Formal : Node_Id;
1941 Typ : Entity_Id;
1943 begin
1944 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1945 -- dimensions for sub specs that don't come from source.
1947 if Ada_Version < Ada_2012 or else not Comes_From_Source (N) then
1948 return;
1949 end if;
1951 Formal := First (Formals);
1952 while Present (Formal) loop
1953 Typ := Parameter_Type (Formal);
1954 Dims_Of_Typ := Dimensions_Of (Typ);
1956 if Exists (Dims_Of_Typ) then
1957 declare
1958 Expr : constant Node_Id := Expression (Formal);
1960 begin
1961 -- Issue a warning if Expr is a numeric literal and if its
1962 -- dimensions differ with the dimensions of the formal type.
1964 if Present (Expr)
1965 and then Dims_Of_Typ /= Dimensions_Of (Expr)
1966 and then Nkind_In (Original_Node (Expr), N_Real_Literal,
1967 N_Integer_Literal)
1968 then
1969 Dim_Warning_For_Numeric_Literal (Expr, Etype (Typ));
1970 end if;
1971 end;
1972 end if;
1974 Next (Formal);
1975 end loop;
1976 end Analyze_Dimension_Formals;
1978 ---------------------------------
1979 -- Analyze_Dimension_Has_Etype --
1980 ---------------------------------
1982 procedure Analyze_Dimension_Has_Etype (N : Node_Id) is
1983 Etyp : constant Entity_Id := Etype (N);
1984 Dims_Of_Etyp : Dimension_Type := Dimensions_Of (Etyp);
1986 begin
1987 -- General case. Propagation of the dimensions from the type
1989 if Exists (Dims_Of_Etyp) then
1990 Set_Dimensions (N, Dims_Of_Etyp);
1992 -- Identifier case. Propagate the dimensions from the entity for
1993 -- identifier whose entity is a non-dimensionless constant.
1995 elsif Nkind (N) = N_Identifier then
1996 Analyze_Dimension_Identifier : declare
1997 Id : constant Entity_Id := Entity (N);
1999 begin
2000 -- If Id is missing, abnormal tree, assume previous error
2002 if No (Id) then
2003 Check_Error_Detected;
2004 return;
2006 elsif Ekind_In (Id, E_Constant, E_Named_Real)
2007 and then Exists (Dimensions_Of (Id))
2008 then
2009 Set_Dimensions (N, Dimensions_Of (Id));
2010 end if;
2011 end Analyze_Dimension_Identifier;
2013 -- Attribute reference case. Propagate the dimensions from the prefix.
2015 elsif Nkind (N) = N_Attribute_Reference
2016 and then Has_Dimension_System (Base_Type (Etyp))
2017 then
2018 Dims_Of_Etyp := Dimensions_Of (Prefix (N));
2020 -- Check the prefix is not dimensionless
2022 if Exists (Dims_Of_Etyp) then
2023 Set_Dimensions (N, Dims_Of_Etyp);
2024 end if;
2025 end if;
2027 -- Remove dimensions from inner expressions, to prevent dimensions
2028 -- table from growing uselessly.
2030 case Nkind (N) is
2031 when N_Attribute_Reference
2032 | N_Indexed_Component
2034 declare
2035 Exprs : constant List_Id := Expressions (N);
2036 Expr : Node_Id;
2038 begin
2039 if Present (Exprs) then
2040 Expr := First (Exprs);
2041 while Present (Expr) loop
2042 Remove_Dimensions (Expr);
2043 Next (Expr);
2044 end loop;
2045 end if;
2046 end;
2048 when N_Qualified_Expression
2049 | N_Type_Conversion
2050 | N_Unchecked_Type_Conversion
2052 Remove_Dimensions (Expression (N));
2054 when N_Selected_Component =>
2055 Remove_Dimensions (Selector_Name (N));
2057 when others =>
2058 null;
2059 end case;
2060 end Analyze_Dimension_Has_Etype;
2062 ------------------------------------------
2063 -- Analyze_Dimension_Number_Declaration --
2064 ------------------------------------------
2066 procedure Analyze_Dimension_Number_Declaration (N : Node_Id) is
2067 Expr : constant Node_Id := Expression (N);
2068 Id : constant Entity_Id := Defining_Identifier (N);
2069 Dim_Of_Expr : constant Dimension_Type := Dimensions_Of (Expr);
2071 begin
2072 if Exists (Dim_Of_Expr) then
2073 Set_Dimensions (Id, Dim_Of_Expr);
2074 Set_Etype (Id, Etype (Expr));
2075 end if;
2076 end Analyze_Dimension_Number_Declaration;
2078 ------------------------------------------
2079 -- Analyze_Dimension_Object_Declaration --
2080 ------------------------------------------
2082 procedure Analyze_Dimension_Object_Declaration (N : Node_Id) is
2083 Expr : constant Node_Id := Expression (N);
2084 Id : constant Entity_Id := Defining_Identifier (N);
2085 Etyp : constant Entity_Id := Etype (Id);
2086 Dim_Of_Etyp : constant Dimension_Type := Dimensions_Of (Etyp);
2087 Dim_Of_Expr : Dimension_Type;
2089 procedure Error_Dim_Msg_For_Object_Declaration
2090 (N : Node_Id;
2091 Etyp : Entity_Id;
2092 Expr : Node_Id);
2093 -- Error using Error_Msg_N at node N. Output the dimensions of the
2094 -- type Etyp and of the expression Expr.
2096 ------------------------------------------
2097 -- Error_Dim_Msg_For_Object_Declaration --
2098 ------------------------------------------
2100 procedure Error_Dim_Msg_For_Object_Declaration
2101 (N : Node_Id;
2102 Etyp : Entity_Id;
2103 Expr : Node_Id) is
2104 begin
2105 Error_Msg_N ("dimensions mismatch in object declaration", N);
2106 Error_Msg_N
2107 ("\expected dimension " & Dimensions_Msg_Of (Etyp) & ", found "
2108 & Dimensions_Msg_Of (Expr), Expr);
2109 end Error_Dim_Msg_For_Object_Declaration;
2111 -- Start of processing for Analyze_Dimension_Object_Declaration
2113 begin
2114 -- Expression is present
2116 if Present (Expr) then
2117 Dim_Of_Expr := Dimensions_Of (Expr);
2119 -- Check dimensions match
2121 if Dim_Of_Expr /= Dim_Of_Etyp then
2123 -- Numeric literal case. Issue a warning if the object type is not
2124 -- dimensionless to indicate the literal is treated as if its
2125 -- dimension matches the type dimension.
2127 if Nkind_In (Original_Node (Expr), N_Real_Literal,
2128 N_Integer_Literal)
2129 then
2130 Dim_Warning_For_Numeric_Literal (Expr, Etyp);
2132 -- Case of object is a constant whose type is a dimensioned type
2134 elsif Constant_Present (N) and then not Exists (Dim_Of_Etyp) then
2136 -- Propagate dimension from expression to object entity
2138 Set_Dimensions (Id, Dim_Of_Expr);
2140 -- For all other cases, issue an error message
2142 else
2143 Error_Dim_Msg_For_Object_Declaration (N, Etyp, Expr);
2144 end if;
2145 end if;
2147 -- Remove dimensions in expression after checking consistency
2148 -- with given type.
2150 Remove_Dimensions (Expr);
2151 end if;
2152 end Analyze_Dimension_Object_Declaration;
2154 ---------------------------------------------------
2155 -- Analyze_Dimension_Object_Renaming_Declaration --
2156 ---------------------------------------------------
2158 procedure Analyze_Dimension_Object_Renaming_Declaration (N : Node_Id) is
2159 Renamed_Name : constant Node_Id := Name (N);
2160 Sub_Mark : constant Node_Id := Subtype_Mark (N);
2162 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2163 (N : Node_Id;
2164 Sub_Mark : Node_Id;
2165 Renamed_Name : Node_Id);
2166 -- Error using Error_Msg_N at node N. Output the dimensions of
2167 -- Sub_Mark and of Renamed_Name.
2169 ---------------------------------------------------
2170 -- Error_Dim_Msg_For_Object_Renaming_Declaration --
2171 ---------------------------------------------------
2173 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2174 (N : Node_Id;
2175 Sub_Mark : Node_Id;
2176 Renamed_Name : Node_Id) is
2177 begin
2178 Error_Msg_N ("dimensions mismatch in object renaming declaration", N);
2179 Error_Msg_N
2180 ("\expected dimension " & Dimensions_Msg_Of (Sub_Mark) & ", found "
2181 & Dimensions_Msg_Of (Renamed_Name), Renamed_Name);
2182 end Error_Dim_Msg_For_Object_Renaming_Declaration;
2184 -- Start of processing for Analyze_Dimension_Object_Renaming_Declaration
2186 begin
2187 if Dimensions_Of (Renamed_Name) /= Dimensions_Of (Sub_Mark) then
2188 Error_Dim_Msg_For_Object_Renaming_Declaration
2189 (N, Sub_Mark, Renamed_Name);
2190 end if;
2191 end Analyze_Dimension_Object_Renaming_Declaration;
2193 -----------------------------------------------
2194 -- Analyze_Dimension_Simple_Return_Statement --
2195 -----------------------------------------------
2197 procedure Analyze_Dimension_Simple_Return_Statement (N : Node_Id) is
2198 Expr : constant Node_Id := Expression (N);
2199 Return_Ent : constant Entity_Id := Return_Statement_Entity (N);
2200 Return_Etyp : constant Entity_Id :=
2201 Etype (Return_Applies_To (Return_Ent));
2202 Dims_Of_Return_Etyp : constant Dimension_Type :=
2203 Dimensions_Of (Return_Etyp);
2205 procedure Error_Dim_Msg_For_Simple_Return_Statement
2206 (N : Node_Id;
2207 Return_Etyp : Entity_Id;
2208 Expr : Node_Id);
2209 -- Error using Error_Msg_N at node N. Output the dimensions of the
2210 -- returned type Return_Etyp and the returned expression Expr of N.
2212 -----------------------------------------------
2213 -- Error_Dim_Msg_For_Simple_Return_Statement --
2214 -----------------------------------------------
2216 procedure Error_Dim_Msg_For_Simple_Return_Statement
2217 (N : Node_Id;
2218 Return_Etyp : Entity_Id;
2219 Expr : Node_Id)
2221 begin
2222 Error_Msg_N ("dimensions mismatch in return statement", N);
2223 Error_Msg_N
2224 ("\expected dimension " & Dimensions_Msg_Of (Return_Etyp)
2225 & ", found " & Dimensions_Msg_Of (Expr), Expr);
2226 end Error_Dim_Msg_For_Simple_Return_Statement;
2228 -- Start of processing for Analyze_Dimension_Simple_Return_Statement
2230 begin
2231 if Dims_Of_Return_Etyp /= Dimensions_Of (Expr) then
2232 Error_Dim_Msg_For_Simple_Return_Statement (N, Return_Etyp, Expr);
2233 Remove_Dimensions (Expr);
2234 end if;
2235 end Analyze_Dimension_Simple_Return_Statement;
2237 -------------------------------------------
2238 -- Analyze_Dimension_Subtype_Declaration --
2239 -------------------------------------------
2241 procedure Analyze_Dimension_Subtype_Declaration (N : Node_Id) is
2242 Id : constant Entity_Id := Defining_Identifier (N);
2243 Dims_Of_Id : constant Dimension_Type := Dimensions_Of (Id);
2244 Dims_Of_Etyp : Dimension_Type;
2245 Etyp : Node_Id;
2247 begin
2248 -- No constraint case in subtype declaration
2250 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
2251 Etyp := Etype (Subtype_Indication (N));
2252 Dims_Of_Etyp := Dimensions_Of (Etyp);
2254 if Exists (Dims_Of_Etyp) then
2256 -- If subtype already has a dimension (from Aspect_Dimension), it
2257 -- cannot inherit different dimensions from its subtype.
2259 if Exists (Dims_Of_Id) and then Dims_Of_Etyp /= Dims_Of_Id then
2260 Error_Msg_NE
2261 ("subtype& already " & Dimensions_Msg_Of (Id, True), N, Id);
2262 else
2263 Set_Dimensions (Id, Dims_Of_Etyp);
2264 Set_Symbol (Id, Symbol_Of (Etyp));
2265 end if;
2266 end if;
2268 -- Constraint present in subtype declaration
2270 else
2271 Etyp := Etype (Subtype_Mark (Subtype_Indication (N)));
2272 Dims_Of_Etyp := Dimensions_Of (Etyp);
2274 if Exists (Dims_Of_Etyp) then
2275 Set_Dimensions (Id, Dims_Of_Etyp);
2276 Set_Symbol (Id, Symbol_Of (Etyp));
2277 end if;
2278 end if;
2279 end Analyze_Dimension_Subtype_Declaration;
2281 --------------------------------
2282 -- Analyze_Dimension_Unary_Op --
2283 --------------------------------
2285 procedure Analyze_Dimension_Unary_Op (N : Node_Id) is
2286 begin
2287 case Nkind (N) is
2289 -- Propagate the dimension if the operand is not dimensionless
2291 when N_Op_Abs
2292 | N_Op_Minus
2293 | N_Op_Plus
2295 declare
2296 R : constant Node_Id := Right_Opnd (N);
2297 begin
2298 Move_Dimensions (R, N);
2299 end;
2301 when others =>
2302 null;
2303 end case;
2304 end Analyze_Dimension_Unary_Op;
2306 ---------------------------------
2307 -- Check_Expression_Dimensions --
2308 ---------------------------------
2310 procedure Check_Expression_Dimensions
2311 (Expr : Node_Id;
2312 Typ : Entity_Id)
2314 begin
2315 if Is_Floating_Point_Type (Etype (Expr)) then
2316 Analyze_Dimension (Expr);
2318 if Dimensions_Of (Expr) /= Dimensions_Of (Typ) then
2319 Error_Msg_N ("dimensions mismatch in array aggregate", Expr);
2320 Error_Msg_N
2321 ("\expected dimension " & Dimensions_Msg_Of (Typ)
2322 & ", found " & Dimensions_Msg_Of (Expr), Expr);
2323 end if;
2324 end if;
2325 end Check_Expression_Dimensions;
2327 ---------------------
2328 -- Copy_Dimensions --
2329 ---------------------
2331 procedure Copy_Dimensions (From, To : Node_Id) is
2332 Dims_Of_From : constant Dimension_Type := Dimensions_Of (From);
2334 begin
2335 -- Ignore if not Ada 2012 or beyond
2337 if Ada_Version < Ada_2012 then
2338 return;
2340 -- For Ada 2012, Copy the dimension of 'From to 'To'
2342 elsif Exists (Dims_Of_From) then
2343 Set_Dimensions (To, Dims_Of_From);
2344 end if;
2345 end Copy_Dimensions;
2347 --------------------------
2348 -- Create_Rational_From --
2349 --------------------------
2351 -- RATIONAL ::= [-] NUMERAL [/ NUMERAL]
2353 -- A rational number is a number that can be expressed as the quotient or
2354 -- fraction a/b of two integers, where b is non-zero positive.
2356 function Create_Rational_From
2357 (Expr : Node_Id;
2358 Complain : Boolean) return Rational
2360 Or_Node_Of_Expr : constant Node_Id := Original_Node (Expr);
2361 Result : Rational := No_Rational;
2363 function Process_Minus (N : Node_Id) return Rational;
2364 -- Create a rational from a N_Op_Minus node
2366 function Process_Divide (N : Node_Id) return Rational;
2367 -- Create a rational from a N_Op_Divide node
2369 function Process_Literal (N : Node_Id) return Rational;
2370 -- Create a rational from a N_Integer_Literal node
2372 -------------------
2373 -- Process_Minus --
2374 -------------------
2376 function Process_Minus (N : Node_Id) return Rational is
2377 Right : constant Node_Id := Original_Node (Right_Opnd (N));
2378 Result : Rational;
2380 begin
2381 -- Operand is an integer literal
2383 if Nkind (Right) = N_Integer_Literal then
2384 Result := -Process_Literal (Right);
2386 -- Operand is a divide operator
2388 elsif Nkind (Right) = N_Op_Divide then
2389 Result := -Process_Divide (Right);
2391 else
2392 Result := No_Rational;
2393 end if;
2395 -- Provide minimal semantic information on dimension expressions,
2396 -- even though they have no run-time existence. This is for use by
2397 -- ASIS tools, in particular pretty-printing. If generating code
2398 -- standard operator resolution will take place.
2400 if ASIS_Mode then
2401 Set_Entity (N, Standard_Op_Minus);
2402 Set_Etype (N, Standard_Integer);
2403 end if;
2405 return Result;
2406 end Process_Minus;
2408 --------------------
2409 -- Process_Divide --
2410 --------------------
2412 function Process_Divide (N : Node_Id) return Rational is
2413 Left : constant Node_Id := Original_Node (Left_Opnd (N));
2414 Right : constant Node_Id := Original_Node (Right_Opnd (N));
2415 Left_Rat : Rational;
2416 Result : Rational := No_Rational;
2417 Right_Rat : Rational;
2419 begin
2420 -- Both left and right operands are integer literals
2422 if Nkind (Left) = N_Integer_Literal
2423 and then
2424 Nkind (Right) = N_Integer_Literal
2425 then
2426 Left_Rat := Process_Literal (Left);
2427 Right_Rat := Process_Literal (Right);
2428 Result := Left_Rat / Right_Rat;
2429 end if;
2431 -- Provide minimal semantic information on dimension expressions,
2432 -- even though they have no run-time existence. This is for use by
2433 -- ASIS tools, in particular pretty-printing. If generating code
2434 -- standard operator resolution will take place.
2436 if ASIS_Mode then
2437 Set_Entity (N, Standard_Op_Divide);
2438 Set_Etype (N, Standard_Integer);
2439 end if;
2441 return Result;
2442 end Process_Divide;
2444 ---------------------
2445 -- Process_Literal --
2446 ---------------------
2448 function Process_Literal (N : Node_Id) return Rational is
2449 begin
2450 return +Whole (UI_To_Int (Intval (N)));
2451 end Process_Literal;
2453 -- Start of processing for Create_Rational_From
2455 begin
2456 -- Check the expression is either a division of two integers or an
2457 -- integer itself. Note that the check applies to the original node
2458 -- since the node could have already been rewritten.
2460 -- Integer literal case
2462 if Nkind (Or_Node_Of_Expr) = N_Integer_Literal then
2463 Result := Process_Literal (Or_Node_Of_Expr);
2465 -- Divide operator case
2467 elsif Nkind (Or_Node_Of_Expr) = N_Op_Divide then
2468 Result := Process_Divide (Or_Node_Of_Expr);
2470 -- Minus operator case
2472 elsif Nkind (Or_Node_Of_Expr) = N_Op_Minus then
2473 Result := Process_Minus (Or_Node_Of_Expr);
2474 end if;
2476 -- When Expr cannot be interpreted as a rational and Complain is true,
2477 -- generate an error message.
2479 if Complain and then Result = No_Rational then
2480 Error_Msg_N ("rational expected", Expr);
2481 end if;
2483 return Result;
2484 end Create_Rational_From;
2486 -------------------
2487 -- Dimensions_Of --
2488 -------------------
2490 function Dimensions_Of (N : Node_Id) return Dimension_Type is
2491 begin
2492 return Dimension_Table.Get (N);
2493 end Dimensions_Of;
2495 -----------------------
2496 -- Dimensions_Msg_Of --
2497 -----------------------
2499 function Dimensions_Msg_Of
2500 (N : Node_Id;
2501 Description_Needed : Boolean := False) return String
2503 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
2504 Dimensions_Msg : Name_Id;
2505 System : System_Type;
2507 begin
2508 -- Initialization of Name_Buffer
2510 Name_Len := 0;
2512 -- N is not dimensionless
2514 if Exists (Dims_Of_N) then
2515 System := System_Of (Base_Type (Etype (N)));
2517 -- When Description_Needed, add to string "has dimension " before the
2518 -- actual dimension.
2520 if Description_Needed then
2521 Add_Str_To_Name_Buffer ("has dimension ");
2522 end if;
2524 Add_String_To_Name_Buffer
2525 (From_Dim_To_Str_Of_Dim_Symbols (Dims_Of_N, System, True));
2527 -- N is dimensionless
2529 -- When Description_Needed, return "is dimensionless"
2531 elsif Description_Needed then
2532 Add_Str_To_Name_Buffer ("is dimensionless");
2534 -- Otherwise, return "'[']"
2536 else
2537 Add_Str_To_Name_Buffer ("'[']");
2538 end if;
2540 Dimensions_Msg := Name_Find;
2541 return Get_Name_String (Dimensions_Msg);
2542 end Dimensions_Msg_Of;
2544 --------------------------
2545 -- Dimension_Table_Hash --
2546 --------------------------
2548 function Dimension_Table_Hash
2549 (Key : Node_Id) return Dimension_Table_Range
2551 begin
2552 return Dimension_Table_Range (Key mod 511);
2553 end Dimension_Table_Hash;
2555 -------------------------------------
2556 -- Dim_Warning_For_Numeric_Literal --
2557 -------------------------------------
2559 procedure Dim_Warning_For_Numeric_Literal (N : Node_Id; Typ : Entity_Id) is
2560 begin
2561 -- Initialize name buffer
2563 Name_Len := 0;
2565 Add_String_To_Name_Buffer (String_From_Numeric_Literal (N));
2567 -- Insert a blank between the literal and the symbol
2569 Add_Str_To_Name_Buffer (" ");
2570 Add_String_To_Name_Buffer (Symbol_Of (Typ));
2572 Error_Msg_Name_1 := Name_Find;
2573 Error_Msg_N ("assumed to be%%??", N);
2574 end Dim_Warning_For_Numeric_Literal;
2576 ----------------------------------------
2577 -- Eval_Op_Expon_For_Dimensioned_Type --
2578 ----------------------------------------
2580 -- Evaluate the expon operator for real dimensioned type.
2582 -- Note that if the exponent is an integer (denominator = 1) the node is
2583 -- evaluated by the regular Eval_Op_Expon routine (see Sem_Eval).
2585 procedure Eval_Op_Expon_For_Dimensioned_Type
2586 (N : Node_Id;
2587 Btyp : Entity_Id)
2589 R : constant Node_Id := Right_Opnd (N);
2590 R_Value : Rational := No_Rational;
2592 begin
2593 if Is_Real_Type (Btyp) then
2594 R_Value := Create_Rational_From (R, False);
2595 end if;
2597 -- Check that the exponent is not an integer
2599 if R_Value /= No_Rational and then R_Value.Denominator /= 1 then
2600 Eval_Op_Expon_With_Rational_Exponent (N, R_Value);
2601 else
2602 Eval_Op_Expon (N);
2603 end if;
2604 end Eval_Op_Expon_For_Dimensioned_Type;
2606 ------------------------------------------
2607 -- Eval_Op_Expon_With_Rational_Exponent --
2608 ------------------------------------------
2610 -- For dimensioned operand in exponentiation, exponent is allowed to be a
2611 -- Rational and not only an Integer like for dimensionless operands. For
2612 -- that particular case, the left operand is rewritten as a function call
2613 -- using the function Expon_LLF from s-llflex.ads.
2615 procedure Eval_Op_Expon_With_Rational_Exponent
2616 (N : Node_Id;
2617 Exponent_Value : Rational)
2619 Loc : constant Source_Ptr := Sloc (N);
2620 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
2621 L : constant Node_Id := Left_Opnd (N);
2622 Etyp_Of_L : constant Entity_Id := Etype (L);
2623 Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2624 Actual_1 : Node_Id;
2625 Actual_2 : Node_Id;
2626 Dim_Power : Rational;
2627 List_Of_Dims : List_Id;
2628 New_Aspect : Node_Id;
2629 New_Aspects : List_Id;
2630 New_Id : Entity_Id;
2631 New_N : Node_Id;
2632 New_Subtyp_Decl_For_L : Node_Id;
2633 System : System_Type;
2635 begin
2636 -- Case when the operand is not dimensionless
2638 if Exists (Dims_Of_N) then
2640 -- Get the corresponding System_Type to know the exact number of
2641 -- dimensions in the system.
2643 System := System_Of (Btyp_Of_L);
2645 -- Generation of a new subtype with the proper dimensions
2647 -- In order to rewrite the operator as a type conversion, a new
2648 -- dimensioned subtype with the resulting dimensions of the
2649 -- exponentiation must be created.
2651 -- Generate:
2653 -- Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2654 -- System : constant System_Id :=
2655 -- Get_Dimension_System_Id (Btyp_Of_L);
2656 -- Num_Of_Dims : constant Number_Of_Dimensions :=
2657 -- Dimension_Systems.Table (System).Dimension_Count;
2659 -- subtype T is Btyp_Of_L
2660 -- with
2661 -- Dimension => (
2662 -- Dims_Of_N (1).Numerator / Dims_Of_N (1).Denominator,
2663 -- Dims_Of_N (2).Numerator / Dims_Of_N (2).Denominator,
2664 -- ...
2665 -- Dims_Of_N (Num_Of_Dims).Numerator /
2666 -- Dims_Of_N (Num_Of_Dims).Denominator);
2668 -- Step 1: Generate the new aggregate for the aspect Dimension
2670 New_Aspects := Empty_List;
2672 List_Of_Dims := New_List;
2673 for Position in Dims_Of_N'First .. System.Count loop
2674 Dim_Power := Dims_Of_N (Position);
2675 Append_To (List_Of_Dims,
2676 Make_Op_Divide (Loc,
2677 Left_Opnd =>
2678 Make_Integer_Literal (Loc, Int (Dim_Power.Numerator)),
2679 Right_Opnd =>
2680 Make_Integer_Literal (Loc, Int (Dim_Power.Denominator))));
2681 end loop;
2683 -- Step 2: Create the new Aspect Specification for Aspect Dimension
2685 New_Aspect :=
2686 Make_Aspect_Specification (Loc,
2687 Identifier => Make_Identifier (Loc, Name_Dimension),
2688 Expression => Make_Aggregate (Loc, Expressions => List_Of_Dims));
2690 -- Step 3: Make a temporary identifier for the new subtype
2692 New_Id := Make_Temporary (Loc, 'T');
2693 Set_Is_Internal (New_Id);
2695 -- Step 4: Declaration of the new subtype
2697 New_Subtyp_Decl_For_L :=
2698 Make_Subtype_Declaration (Loc,
2699 Defining_Identifier => New_Id,
2700 Subtype_Indication => New_Occurrence_Of (Btyp_Of_L, Loc));
2702 Append (New_Aspect, New_Aspects);
2703 Set_Parent (New_Aspects, New_Subtyp_Decl_For_L);
2704 Set_Aspect_Specifications (New_Subtyp_Decl_For_L, New_Aspects);
2706 Analyze (New_Subtyp_Decl_For_L);
2708 -- Case where the operand is dimensionless
2710 else
2711 New_Id := Btyp_Of_L;
2712 end if;
2714 -- Replacement of N by New_N
2716 -- Generate:
2718 -- Actual_1 := Long_Long_Float (L),
2720 -- Actual_2 := Long_Long_Float (Exponent_Value.Numerator) /
2721 -- Long_Long_Float (Exponent_Value.Denominator);
2723 -- (T (Expon_LLF (Actual_1, Actual_2)));
2725 -- where T is the subtype declared in step 1
2727 -- The node is rewritten as a type conversion
2729 -- Step 1: Creation of the two parameters of Expon_LLF function call
2731 Actual_1 :=
2732 Make_Type_Conversion (Loc,
2733 Subtype_Mark => New_Occurrence_Of (Standard_Long_Long_Float, Loc),
2734 Expression => Relocate_Node (L));
2736 Actual_2 :=
2737 Make_Op_Divide (Loc,
2738 Left_Opnd =>
2739 Make_Real_Literal (Loc,
2740 UR_From_Uint (UI_From_Int (Int (Exponent_Value.Numerator)))),
2741 Right_Opnd =>
2742 Make_Real_Literal (Loc,
2743 UR_From_Uint (UI_From_Int (Int (Exponent_Value.Denominator)))));
2745 -- Step 2: Creation of New_N
2747 New_N :=
2748 Make_Type_Conversion (Loc,
2749 Subtype_Mark => New_Occurrence_Of (New_Id, Loc),
2750 Expression =>
2751 Make_Function_Call (Loc,
2752 Name => New_Occurrence_Of (RTE (RE_Expon_LLF), Loc),
2753 Parameter_Associations => New_List (
2754 Actual_1, Actual_2)));
2756 -- Step 3: Rewrite N with the result
2758 Rewrite (N, New_N);
2759 Set_Etype (N, New_Id);
2760 Analyze_And_Resolve (N, New_Id);
2761 end Eval_Op_Expon_With_Rational_Exponent;
2763 ------------
2764 -- Exists --
2765 ------------
2767 function Exists (Dim : Dimension_Type) return Boolean is
2768 begin
2769 return Dim /= Null_Dimension;
2770 end Exists;
2772 function Exists (Str : String_Id) return Boolean is
2773 begin
2774 return Str /= No_String;
2775 end Exists;
2777 function Exists (Sys : System_Type) return Boolean is
2778 begin
2779 return Sys /= Null_System;
2780 end Exists;
2782 ---------------------------------
2783 -- Expand_Put_Call_With_Symbol --
2784 ---------------------------------
2786 -- For procedure Put (resp. Put_Dim_Of) and function Image, defined in
2787 -- System.Dim.Float_IO or System.Dim.Integer_IO, the default string
2788 -- parameter is rewritten to include the unit symbol (or the dimension
2789 -- symbols if not a defined quantity) in the output of a dimensioned
2790 -- object. If a value is already supplied by the user for the parameter
2791 -- Symbol, it is used as is.
2793 -- Case 1. Item is dimensionless
2795 -- * Put : Item appears without a suffix
2797 -- * Put_Dim_Of : the output is []
2799 -- Obj : Mks_Type := 2.6;
2800 -- Put (Obj, 1, 1, 0);
2801 -- Put_Dim_Of (Obj);
2803 -- The corresponding outputs are:
2804 -- $2.6
2805 -- $[]
2807 -- Case 2. Item has a dimension
2809 -- * Put : If the type of Item is a dimensioned subtype whose
2810 -- symbol is not empty, then the symbol appears as a
2811 -- suffix. Otherwise, a new string is created and appears
2812 -- as a suffix of Item. This string results in the
2813 -- successive concatanations between each unit symbol
2814 -- raised by its corresponding dimension power from the
2815 -- dimensions of Item.
2817 -- * Put_Dim_Of : The output is a new string resulting in the successive
2818 -- concatanations between each dimension symbol raised by
2819 -- its corresponding dimension power from the dimensions of
2820 -- Item.
2822 -- subtype Random is Mks_Type
2823 -- with
2824 -- Dimension => (
2825 -- Meter => 3,
2826 -- Candela => -1,
2827 -- others => 0);
2829 -- Obj : Random := 5.0;
2830 -- Put (Obj);
2831 -- Put_Dim_Of (Obj);
2833 -- The corresponding outputs are:
2834 -- $5.0 m**3.cd**(-1)
2835 -- $[l**3.J**(-1)]
2837 -- The function Image returns the string identical to that produced by
2838 -- a call to Put whose first parameter is a string.
2840 procedure Expand_Put_Call_With_Symbol (N : Node_Id) is
2841 Actuals : constant List_Id := Parameter_Associations (N);
2842 Loc : constant Source_Ptr := Sloc (N);
2843 Name_Call : constant Node_Id := Name (N);
2844 New_Actuals : constant List_Id := New_List;
2845 Actual : Node_Id;
2846 Dims_Of_Actual : Dimension_Type;
2847 Etyp : Entity_Id;
2848 New_Str_Lit : Node_Id := Empty;
2849 Symbols : String_Id;
2851 Is_Put_Dim_Of : Boolean := False;
2852 -- This flag is used in order to differentiate routines Put and
2853 -- Put_Dim_Of. Set to True if the procedure is one of the Put_Dim_Of
2854 -- defined in System.Dim.Float_IO or System.Dim.Integer_IO.
2856 function Has_Symbols return Boolean;
2857 -- Return True if the current Put call already has a parameter
2858 -- association for parameter "Symbols" with the correct string of
2859 -- symbols.
2861 function Is_Procedure_Put_Call return Boolean;
2862 -- Return True if the current call is a call of an instantiation of a
2863 -- procedure Put defined in the package System.Dim.Float_IO and
2864 -- System.Dim.Integer_IO.
2866 function Item_Actual return Node_Id;
2867 -- Return the item actual parameter node in the output call
2869 -----------------
2870 -- Has_Symbols --
2871 -----------------
2873 function Has_Symbols return Boolean is
2874 Actual : Node_Id;
2875 Actual_Str : Node_Id;
2877 begin
2878 -- Look for a symbols parameter association in the list of actuals
2880 Actual := First (Actuals);
2881 while Present (Actual) loop
2883 -- Positional parameter association case when the actual is a
2884 -- string literal.
2886 if Nkind (Actual) = N_String_Literal then
2887 Actual_Str := Actual;
2889 -- Named parameter association case when selector name is Symbol
2891 elsif Nkind (Actual) = N_Parameter_Association
2892 and then Chars (Selector_Name (Actual)) = Name_Symbol
2893 then
2894 Actual_Str := Explicit_Actual_Parameter (Actual);
2896 -- Ignore all other cases
2898 else
2899 Actual_Str := Empty;
2900 end if;
2902 if Present (Actual_Str) then
2904 -- Return True if the actual comes from source or if the string
2905 -- of symbols doesn't have the default value (i.e. it is ""),
2906 -- in which case it is used as suffix of the generated string.
2908 if Comes_From_Source (Actual)
2909 or else String_Length (Strval (Actual_Str)) /= 0
2910 then
2911 return True;
2913 else
2914 return False;
2915 end if;
2916 end if;
2918 Next (Actual);
2919 end loop;
2921 -- At this point, the call has no parameter association. Look to the
2922 -- last actual since the symbols parameter is the last one.
2924 return Nkind (Last (Actuals)) = N_String_Literal;
2925 end Has_Symbols;
2927 ---------------------------
2928 -- Is_Procedure_Put_Call --
2929 ---------------------------
2931 function Is_Procedure_Put_Call return Boolean is
2932 Ent : Entity_Id;
2933 Loc : Source_Ptr;
2935 begin
2936 -- There are three different Put (resp. Put_Dim_Of) routines in each
2937 -- generic dim IO package. Verify the current procedure call is one
2938 -- of them.
2940 if Is_Entity_Name (Name_Call) then
2941 Ent := Entity (Name_Call);
2943 -- Get the original subprogram entity following the renaming chain
2945 if Present (Alias (Ent)) then
2946 Ent := Alias (Ent);
2947 end if;
2949 Loc := Sloc (Ent);
2951 -- Check the name of the entity subprogram is Put (resp.
2952 -- Put_Dim_Of) and verify this entity is located in either
2953 -- System.Dim.Float_IO or System.Dim.Integer_IO.
2955 if Loc > No_Location
2956 and then Is_Dim_IO_Package_Entity
2957 (Cunit_Entity (Get_Source_Unit (Loc)))
2958 then
2959 if Chars (Ent) = Name_Put_Dim_Of then
2960 Is_Put_Dim_Of := True;
2961 return True;
2963 elsif Chars (Ent) = Name_Put
2964 or else Chars (Ent) = Name_Image
2965 then
2966 return True;
2967 end if;
2968 end if;
2969 end if;
2971 return False;
2972 end Is_Procedure_Put_Call;
2974 -----------------
2975 -- Item_Actual --
2976 -----------------
2978 function Item_Actual return Node_Id is
2979 Actual : Node_Id;
2981 begin
2982 -- Look for the item actual as a parameter association
2984 Actual := First (Actuals);
2985 while Present (Actual) loop
2986 if Nkind (Actual) = N_Parameter_Association
2987 and then Chars (Selector_Name (Actual)) = Name_Item
2988 then
2989 return Explicit_Actual_Parameter (Actual);
2990 end if;
2992 Next (Actual);
2993 end loop;
2995 -- Case where the item has been defined without an association
2997 Actual := First (Actuals);
2999 -- Depending on the procedure Put, Item actual could be first or
3000 -- second in the list of actuals.
3002 if Has_Dimension_System (Base_Type (Etype (Actual))) then
3003 return Actual;
3004 else
3005 return Next (Actual);
3006 end if;
3007 end Item_Actual;
3009 -- Start of processing for Expand_Put_Call_With_Symbol
3011 begin
3012 if Is_Procedure_Put_Call and then not Has_Symbols then
3013 Actual := Item_Actual;
3014 Dims_Of_Actual := Dimensions_Of (Actual);
3015 Etyp := Etype (Actual);
3017 -- Put_Dim_Of case
3019 if Is_Put_Dim_Of then
3021 -- Check that the item is not dimensionless
3023 -- Create the new String_Literal with the new String_Id generated
3024 -- by the routine From_Dim_To_Str_Of_Dim_Symbols.
3026 if Exists (Dims_Of_Actual) then
3027 New_Str_Lit :=
3028 Make_String_Literal (Loc,
3029 From_Dim_To_Str_Of_Dim_Symbols
3030 (Dims_Of_Actual, System_Of (Base_Type (Etyp))));
3032 -- If dimensionless, the output is []
3034 else
3035 New_Str_Lit :=
3036 Make_String_Literal (Loc, "[]");
3037 end if;
3039 -- Put case
3041 else
3042 -- Add the symbol as a suffix of the value if the subtype has a
3043 -- unit symbol or if the parameter is not dimensionless.
3045 if Exists (Symbol_Of (Etyp)) then
3046 Symbols := Symbol_Of (Etyp);
3047 else
3048 Symbols := From_Dim_To_Str_Of_Unit_Symbols
3049 (Dims_Of_Actual, System_Of (Base_Type (Etyp)));
3050 end if;
3052 -- Check Symbols exists
3054 if Exists (Symbols) then
3055 Start_String;
3057 -- Put a space between the value and the dimension
3059 Store_String_Char (' ');
3060 Store_String_Chars (Symbols);
3061 New_Str_Lit := Make_String_Literal (Loc, End_String);
3062 end if;
3063 end if;
3065 if Present (New_Str_Lit) then
3067 -- Insert all actuals in New_Actuals
3069 Actual := First (Actuals);
3070 while Present (Actual) loop
3072 -- Copy every actuals in New_Actuals except the Symbols
3073 -- parameter association.
3075 if Nkind (Actual) = N_Parameter_Association
3076 and then Chars (Selector_Name (Actual)) /= Name_Symbol
3077 then
3078 Append_To (New_Actuals,
3079 Make_Parameter_Association (Loc,
3080 Selector_Name => New_Copy (Selector_Name (Actual)),
3081 Explicit_Actual_Parameter =>
3082 New_Copy (Explicit_Actual_Parameter (Actual))));
3084 elsif Nkind (Actual) /= N_Parameter_Association then
3085 Append_To (New_Actuals, New_Copy (Actual));
3086 end if;
3088 Next (Actual);
3089 end loop;
3091 -- Create new Symbols param association and append to New_Actuals
3093 Append_To (New_Actuals,
3094 Make_Parameter_Association (Loc,
3095 Selector_Name => Make_Identifier (Loc, Name_Symbol),
3096 Explicit_Actual_Parameter => New_Str_Lit));
3098 -- Rewrite and analyze the procedure call
3100 if Chars (Name_Call) = Name_Image then
3101 Rewrite (N,
3102 Make_Function_Call (Loc,
3103 Name => New_Copy (Name_Call),
3104 Parameter_Associations => New_Actuals));
3105 Analyze_And_Resolve (N);
3106 else
3107 Rewrite (N,
3108 Make_Procedure_Call_Statement (Loc,
3109 Name => New_Copy (Name_Call),
3110 Parameter_Associations => New_Actuals));
3111 Analyze (N);
3112 end if;
3114 end if;
3115 end if;
3116 end Expand_Put_Call_With_Symbol;
3118 ------------------------------------
3119 -- From_Dim_To_Str_Of_Dim_Symbols --
3120 ------------------------------------
3122 -- Given a dimension vector and the corresponding dimension system, create
3123 -- a String_Id to output dimension symbols corresponding to the dimensions
3124 -- Dims. If In_Error_Msg is True, there is a special handling for character
3125 -- asterisk * which is an insertion character in error messages.
3127 function From_Dim_To_Str_Of_Dim_Symbols
3128 (Dims : Dimension_Type;
3129 System : System_Type;
3130 In_Error_Msg : Boolean := False) return String_Id
3132 Dim_Power : Rational;
3133 First_Dim : Boolean := True;
3135 procedure Store_String_Oexpon;
3136 -- Store the expon operator symbol "**" in the string. In error
3137 -- messages, asterisk * is a special character and must be quoted
3138 -- to be placed literally into the message.
3140 -------------------------
3141 -- Store_String_Oexpon --
3142 -------------------------
3144 procedure Store_String_Oexpon is
3145 begin
3146 if In_Error_Msg then
3147 Store_String_Chars ("'*'*");
3148 else
3149 Store_String_Chars ("**");
3150 end if;
3151 end Store_String_Oexpon;
3153 -- Start of processing for From_Dim_To_Str_Of_Dim_Symbols
3155 begin
3156 -- Initialization of the new String_Id
3158 Start_String;
3160 -- Store the dimension symbols inside boxes
3162 if In_Error_Msg then
3163 Store_String_Chars ("'[");
3164 else
3165 Store_String_Char ('[');
3166 end if;
3168 for Position in Dimension_Type'Range loop
3169 Dim_Power := Dims (Position);
3170 if Dim_Power /= Zero then
3172 if First_Dim then
3173 First_Dim := False;
3174 else
3175 Store_String_Char ('.');
3176 end if;
3178 Store_String_Chars (System.Dim_Symbols (Position));
3180 -- Positive dimension case
3182 if Dim_Power.Numerator > 0 then
3184 -- Integer case
3186 if Dim_Power.Denominator = 1 then
3187 if Dim_Power.Numerator /= 1 then
3188 Store_String_Oexpon;
3189 Store_String_Int (Int (Dim_Power.Numerator));
3190 end if;
3192 -- Rational case when denominator /= 1
3194 else
3195 Store_String_Oexpon;
3196 Store_String_Char ('(');
3197 Store_String_Int (Int (Dim_Power.Numerator));
3198 Store_String_Char ('/');
3199 Store_String_Int (Int (Dim_Power.Denominator));
3200 Store_String_Char (')');
3201 end if;
3203 -- Negative dimension case
3205 else
3206 Store_String_Oexpon;
3207 Store_String_Char ('(');
3208 Store_String_Char ('-');
3209 Store_String_Int (Int (-Dim_Power.Numerator));
3211 -- Integer case
3213 if Dim_Power.Denominator = 1 then
3214 Store_String_Char (')');
3216 -- Rational case when denominator /= 1
3218 else
3219 Store_String_Char ('/');
3220 Store_String_Int (Int (Dim_Power.Denominator));
3221 Store_String_Char (')');
3222 end if;
3223 end if;
3224 end if;
3225 end loop;
3227 if In_Error_Msg then
3228 Store_String_Chars ("']");
3229 else
3230 Store_String_Char (']');
3231 end if;
3233 return End_String;
3234 end From_Dim_To_Str_Of_Dim_Symbols;
3236 -------------------------------------
3237 -- From_Dim_To_Str_Of_Unit_Symbols --
3238 -------------------------------------
3240 -- Given a dimension vector and the corresponding dimension system,
3241 -- create a String_Id to output the unit symbols corresponding to the
3242 -- dimensions Dims.
3244 function From_Dim_To_Str_Of_Unit_Symbols
3245 (Dims : Dimension_Type;
3246 System : System_Type) return String_Id
3248 Dim_Power : Rational;
3249 First_Dim : Boolean := True;
3251 begin
3252 -- Return No_String if dimensionless
3254 if not Exists (Dims) then
3255 return No_String;
3256 end if;
3258 -- Initialization of the new String_Id
3260 Start_String;
3262 for Position in Dimension_Type'Range loop
3263 Dim_Power := Dims (Position);
3265 if Dim_Power /= Zero then
3266 if First_Dim then
3267 First_Dim := False;
3268 else
3269 Store_String_Char ('.');
3270 end if;
3272 Store_String_Chars (System.Unit_Symbols (Position));
3274 -- Positive dimension case
3276 if Dim_Power.Numerator > 0 then
3278 -- Integer case
3280 if Dim_Power.Denominator = 1 then
3281 if Dim_Power.Numerator /= 1 then
3282 Store_String_Chars ("**");
3283 Store_String_Int (Int (Dim_Power.Numerator));
3284 end if;
3286 -- Rational case when denominator /= 1
3288 else
3289 Store_String_Chars ("**");
3290 Store_String_Char ('(');
3291 Store_String_Int (Int (Dim_Power.Numerator));
3292 Store_String_Char ('/');
3293 Store_String_Int (Int (Dim_Power.Denominator));
3294 Store_String_Char (')');
3295 end if;
3297 -- Negative dimension case
3299 else
3300 Store_String_Chars ("**");
3301 Store_String_Char ('(');
3302 Store_String_Char ('-');
3303 Store_String_Int (Int (-Dim_Power.Numerator));
3305 -- Integer case
3307 if Dim_Power.Denominator = 1 then
3308 Store_String_Char (')');
3310 -- Rational case when denominator /= 1
3312 else
3313 Store_String_Char ('/');
3314 Store_String_Int (Int (Dim_Power.Denominator));
3315 Store_String_Char (')');
3316 end if;
3317 end if;
3318 end if;
3319 end loop;
3321 return End_String;
3322 end From_Dim_To_Str_Of_Unit_Symbols;
3324 ---------
3325 -- GCD --
3326 ---------
3328 function GCD (Left, Right : Whole) return Int is
3329 L : Whole;
3330 R : Whole;
3332 begin
3333 L := Left;
3334 R := Right;
3335 while R /= 0 loop
3336 L := L mod R;
3338 if L = 0 then
3339 return Int (R);
3340 end if;
3342 R := R mod L;
3343 end loop;
3345 return Int (L);
3346 end GCD;
3348 --------------------------
3349 -- Has_Dimension_System --
3350 --------------------------
3352 function Has_Dimension_System (Typ : Entity_Id) return Boolean is
3353 begin
3354 return Exists (System_Of (Typ));
3355 end Has_Dimension_System;
3357 ------------------------------
3358 -- Is_Dim_IO_Package_Entity --
3359 ------------------------------
3361 function Is_Dim_IO_Package_Entity (E : Entity_Id) return Boolean is
3362 begin
3363 -- Check the package entity corresponds to System.Dim.Float_IO or
3364 -- System.Dim.Integer_IO.
3366 return
3367 Is_RTU (E, System_Dim_Float_IO)
3368 or else
3369 Is_RTU (E, System_Dim_Integer_IO);
3370 end Is_Dim_IO_Package_Entity;
3372 -------------------------------------
3373 -- Is_Dim_IO_Package_Instantiation --
3374 -------------------------------------
3376 function Is_Dim_IO_Package_Instantiation (N : Node_Id) return Boolean is
3377 Gen_Id : constant Node_Id := Name (N);
3379 begin
3380 -- Check that the instantiated package is either System.Dim.Float_IO
3381 -- or System.Dim.Integer_IO.
3383 return
3384 Is_Entity_Name (Gen_Id)
3385 and then Is_Dim_IO_Package_Entity (Entity (Gen_Id));
3386 end Is_Dim_IO_Package_Instantiation;
3388 ----------------
3389 -- Is_Invalid --
3390 ----------------
3392 function Is_Invalid (Position : Dimension_Position) return Boolean is
3393 begin
3394 return Position = Invalid_Position;
3395 end Is_Invalid;
3397 ---------------------
3398 -- Move_Dimensions --
3399 ---------------------
3401 procedure Move_Dimensions (From, To : Node_Id) is
3402 begin
3403 if Ada_Version < Ada_2012 then
3404 return;
3405 end if;
3407 -- Copy the dimension of 'From to 'To' and remove dimension of 'From'
3409 Copy_Dimensions (From, To);
3410 Remove_Dimensions (From);
3411 end Move_Dimensions;
3413 ------------
3414 -- Reduce --
3415 ------------
3417 function Reduce (X : Rational) return Rational is
3418 begin
3419 if X.Numerator = 0 then
3420 return Zero;
3421 end if;
3423 declare
3424 G : constant Int := GCD (X.Numerator, X.Denominator);
3425 begin
3426 return Rational'(Numerator => Whole (Int (X.Numerator) / G),
3427 Denominator => Whole (Int (X.Denominator) / G));
3428 end;
3429 end Reduce;
3431 -----------------------
3432 -- Remove_Dimensions --
3433 -----------------------
3435 procedure Remove_Dimensions (N : Node_Id) is
3436 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
3437 begin
3438 if Exists (Dims_Of_N) then
3439 Dimension_Table.Remove (N);
3440 end if;
3441 end Remove_Dimensions;
3443 -----------------------------------
3444 -- Remove_Dimension_In_Statement --
3445 -----------------------------------
3447 -- Removal of dimension in statement as part of the Analyze_Statements
3448 -- routine (see package Sem_Ch5).
3450 procedure Remove_Dimension_In_Statement (Stmt : Node_Id) is
3451 begin
3452 if Ada_Version < Ada_2012 then
3453 return;
3454 end if;
3456 -- Remove dimension in parameter specifications for accept statement
3458 if Nkind (Stmt) = N_Accept_Statement then
3459 declare
3460 Param : Node_Id := First (Parameter_Specifications (Stmt));
3461 begin
3462 while Present (Param) loop
3463 Remove_Dimensions (Param);
3464 Next (Param);
3465 end loop;
3466 end;
3468 -- Remove dimension of name and expression in assignments
3470 elsif Nkind (Stmt) = N_Assignment_Statement then
3471 Remove_Dimensions (Expression (Stmt));
3472 Remove_Dimensions (Name (Stmt));
3473 end if;
3474 end Remove_Dimension_In_Statement;
3476 --------------------
3477 -- Set_Dimensions --
3478 --------------------
3480 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type) is
3481 begin
3482 pragma Assert (OK_For_Dimension (Nkind (N)));
3483 pragma Assert (Exists (Val));
3485 Dimension_Table.Set (N, Val);
3486 end Set_Dimensions;
3488 ----------------
3489 -- Set_Symbol --
3490 ----------------
3492 procedure Set_Symbol (E : Entity_Id; Val : String_Id) is
3493 begin
3494 Symbol_Table.Set (E, Val);
3495 end Set_Symbol;
3497 ---------------------------------
3498 -- String_From_Numeric_Literal --
3499 ---------------------------------
3501 function String_From_Numeric_Literal (N : Node_Id) return String_Id is
3502 Loc : constant Source_Ptr := Sloc (N);
3503 Sbuffer : constant Source_Buffer_Ptr :=
3504 Source_Text (Get_Source_File_Index (Loc));
3505 Src_Ptr : Source_Ptr := Loc;
3507 C : Character := Sbuffer (Src_Ptr);
3508 -- Current source program character
3510 function Belong_To_Numeric_Literal (C : Character) return Boolean;
3511 -- Return True if C belongs to a numeric literal
3513 -------------------------------
3514 -- Belong_To_Numeric_Literal --
3515 -------------------------------
3517 function Belong_To_Numeric_Literal (C : Character) return Boolean is
3518 begin
3519 case C is
3520 when '0' .. '9'
3521 | '_' | '.' | 'e' | '#' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
3523 return True;
3525 -- Make sure '+' or '-' is part of an exponent.
3527 when '+' | '-' =>
3528 declare
3529 Prev_C : constant Character := Sbuffer (Src_Ptr - 1);
3530 begin
3531 return Prev_C = 'e' or else Prev_C = 'E';
3532 end;
3534 -- All other character doesn't belong to a numeric literal
3536 when others =>
3537 return False;
3538 end case;
3539 end Belong_To_Numeric_Literal;
3541 -- Start of processing for String_From_Numeric_Literal
3543 begin
3544 Start_String;
3545 while Belong_To_Numeric_Literal (C) loop
3546 Store_String_Char (C);
3547 Src_Ptr := Src_Ptr + 1;
3548 C := Sbuffer (Src_Ptr);
3549 end loop;
3551 return End_String;
3552 end String_From_Numeric_Literal;
3554 ---------------
3555 -- Symbol_Of --
3556 ---------------
3558 function Symbol_Of (E : Entity_Id) return String_Id is
3559 Subtype_Symbol : constant String_Id := Symbol_Table.Get (E);
3560 begin
3561 if Subtype_Symbol /= No_String then
3562 return Subtype_Symbol;
3563 else
3564 return From_Dim_To_Str_Of_Unit_Symbols
3565 (Dimensions_Of (E), System_Of (Base_Type (E)));
3566 end if;
3567 end Symbol_Of;
3569 -----------------------
3570 -- Symbol_Table_Hash --
3571 -----------------------
3573 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range is
3574 begin
3575 return Symbol_Table_Range (Key mod 511);
3576 end Symbol_Table_Hash;
3578 ---------------
3579 -- System_Of --
3580 ---------------
3582 function System_Of (E : Entity_Id) return System_Type is
3583 Type_Decl : constant Node_Id := Parent (E);
3585 begin
3586 -- Look for Type_Decl in System_Table
3588 for Dim_Sys in 1 .. System_Table.Last loop
3589 if Type_Decl = System_Table.Table (Dim_Sys).Type_Decl then
3590 return System_Table.Table (Dim_Sys);
3591 end if;
3592 end loop;
3594 return Null_System;
3595 end System_Of;
3597 end Sem_Dim;