2013-11-13 Jan-Benedict Glaw <jbglaw@lug-owl.de>
[official-gcc.git] / gcc / ada / sem_dim.adb
blob233e30168a23cd53434be3ca830aa04a9c7e8171
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-2013, 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 Lib; use Lib;
31 with Namet; use Namet;
32 with Nlists; use Nlists;
33 with Nmake; use Nmake;
34 with Opt; use Opt;
35 with Rtsfind; use Rtsfind;
36 with Sem; use Sem;
37 with Sem_Eval; use Sem_Eval;
38 with Sem_Res; use Sem_Res;
39 with Sem_Util; use Sem_Util;
40 with Sinfo; use Sinfo;
41 with Sinput; use Sinput;
42 with Snames; use Snames;
43 with Stand; use Stand;
44 with Stringt; use Stringt;
45 with Table;
46 with Tbuild; use Tbuild;
47 with Uintp; use Uintp;
48 with Urealp; use Urealp;
50 with GNAT.HTable;
52 package body Sem_Dim is
54 -------------------------
55 -- Rational Arithmetic --
56 -------------------------
58 type Whole is new Int;
59 subtype Positive_Whole is Whole range 1 .. Whole'Last;
61 type Rational is record
62 Numerator : Whole;
63 Denominator : Positive_Whole;
64 end record;
66 Zero : constant Rational := Rational'(Numerator => 0,
67 Denominator => 1);
69 No_Rational : constant Rational := Rational'(Numerator => 0,
70 Denominator => 2);
71 -- Used to indicate an expression that cannot be interpreted as a rational
72 -- Returned value of the Create_Rational_From routine when parameter Expr
73 -- is not a static representation of a rational.
75 -- Rational constructors
77 function "+" (Right : Whole) return Rational;
78 function GCD (Left, Right : Whole) return Int;
79 function Reduce (X : Rational) return Rational;
81 -- Unary operator for Rational
83 function "-" (Right : Rational) return Rational;
84 function "abs" (Right : Rational) return Rational;
86 -- Rational operations for Rationals
88 function "+" (Left, Right : Rational) return Rational;
89 function "-" (Left, Right : Rational) return Rational;
90 function "*" (Left, Right : Rational) return Rational;
91 function "/" (Left, Right : Rational) return Rational;
93 ------------------
94 -- System Types --
95 ------------------
97 Max_Number_Of_Dimensions : constant := 7;
98 -- Maximum number of dimensions in a dimension system
100 High_Position_Bound : constant := Max_Number_Of_Dimensions;
101 Invalid_Position : constant := 0;
102 Low_Position_Bound : constant := 1;
104 subtype Dimension_Position is
105 Nat range Invalid_Position .. High_Position_Bound;
107 type Name_Array is
108 array (Dimension_Position range
109 Low_Position_Bound .. High_Position_Bound) of Name_Id;
110 -- A data structure used to store the names of all units within a system
112 No_Names : constant Name_Array := (others => No_Name);
114 type Symbol_Array is
115 array (Dimension_Position range
116 Low_Position_Bound .. High_Position_Bound) of String_Id;
117 -- A data structure used to store the symbols of all units within a system
119 No_Symbols : constant Symbol_Array := (others => No_String);
121 -- The following record should be documented field by field
123 type System_Type is record
124 Type_Decl : Node_Id;
125 Unit_Names : Name_Array;
126 Unit_Symbols : Symbol_Array;
127 Dim_Symbols : Symbol_Array;
128 Count : Dimension_Position;
129 end record;
131 Null_System : constant System_Type :=
132 (Empty, No_Names, No_Symbols, No_Symbols, Invalid_Position);
134 subtype System_Id is Nat;
136 -- The following table maps types to systems
138 package System_Table is new Table.Table (
139 Table_Component_Type => System_Type,
140 Table_Index_Type => System_Id,
141 Table_Low_Bound => 1,
142 Table_Initial => 5,
143 Table_Increment => 5,
144 Table_Name => "System_Table");
146 --------------------
147 -- Dimension Type --
148 --------------------
150 type Dimension_Type is
151 array (Dimension_Position range
152 Low_Position_Bound .. High_Position_Bound) of Rational;
154 Null_Dimension : constant Dimension_Type := (others => Zero);
156 type Dimension_Table_Range is range 0 .. 510;
157 function Dimension_Table_Hash (Key : Node_Id) return Dimension_Table_Range;
159 -- The following table associates nodes with dimensions
161 package Dimension_Table is new
162 GNAT.HTable.Simple_HTable
163 (Header_Num => Dimension_Table_Range,
164 Element => Dimension_Type,
165 No_Element => Null_Dimension,
166 Key => Node_Id,
167 Hash => Dimension_Table_Hash,
168 Equal => "=");
170 ------------------
171 -- Symbol Types --
172 ------------------
174 type Symbol_Table_Range is range 0 .. 510;
175 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range;
177 -- Each subtype with a dimension has a symbolic representation of the
178 -- related unit. This table establishes a relation between the subtype
179 -- and the symbol.
181 package Symbol_Table is new
182 GNAT.HTable.Simple_HTable
183 (Header_Num => Symbol_Table_Range,
184 Element => String_Id,
185 No_Element => No_String,
186 Key => Entity_Id,
187 Hash => Symbol_Table_Hash,
188 Equal => "=");
190 -- The following array enumerates all contexts which may contain or
191 -- produce a dimension.
193 OK_For_Dimension : constant array (Node_Kind) of Boolean :=
194 (N_Attribute_Reference => True,
195 N_Expanded_Name => True,
196 N_Defining_Identifier => True,
197 N_Function_Call => True,
198 N_Identifier => True,
199 N_Indexed_Component => True,
200 N_Integer_Literal => True,
201 N_Op_Abs => True,
202 N_Op_Add => True,
203 N_Op_Divide => True,
204 N_Op_Expon => True,
205 N_Op_Minus => True,
206 N_Op_Mod => True,
207 N_Op_Multiply => True,
208 N_Op_Plus => True,
209 N_Op_Rem => True,
210 N_Op_Subtract => True,
211 N_Qualified_Expression => True,
212 N_Real_Literal => True,
213 N_Selected_Component => True,
214 N_Slice => True,
215 N_Type_Conversion => True,
216 N_Unchecked_Type_Conversion => True,
218 others => False);
220 -----------------------
221 -- Local Subprograms --
222 -----------------------
224 procedure Analyze_Dimension_Assignment_Statement (N : Node_Id);
225 -- Subroutine of Analyze_Dimension for assignment statement. Check that the
226 -- dimensions of the left-hand side and the right-hand side of N match.
228 procedure Analyze_Dimension_Binary_Op (N : Node_Id);
229 -- Subroutine of Analyze_Dimension for binary operators. Check the
230 -- dimensions of the right and the left operand permit the operation.
231 -- Then, evaluate the resulting dimensions for each binary operator.
233 procedure Analyze_Dimension_Component_Declaration (N : Node_Id);
234 -- Subroutine of Analyze_Dimension for component declaration. Check that
235 -- the dimensions of the type of N and of the expression match.
237 procedure Analyze_Dimension_Extended_Return_Statement (N : Node_Id);
238 -- Subroutine of Analyze_Dimension for extended return statement. Check
239 -- that the dimensions of the returned type and of the returned object
240 -- match.
242 procedure Analyze_Dimension_Has_Etype (N : Node_Id);
243 -- Subroutine of Analyze_Dimension for a subset of N_Has_Etype denoted by
244 -- the list below:
245 -- N_Attribute_Reference
246 -- N_Identifier
247 -- N_Indexed_Component
248 -- N_Qualified_Expression
249 -- N_Selected_Component
250 -- N_Slice
251 -- N_Type_Conversion
252 -- N_Unchecked_Type_Conversion
254 procedure Analyze_Dimension_Object_Declaration (N : Node_Id);
255 -- Subroutine of Analyze_Dimension for object declaration. Check that
256 -- the dimensions of the object type and the dimensions of the expression
257 -- (if expression is present) match. Note that when the expression is
258 -- a literal, no error is returned. This special case allows object
259 -- declaration such as: m : constant Length := 1.0;
261 procedure Analyze_Dimension_Object_Renaming_Declaration (N : Node_Id);
262 -- Subroutine of Analyze_Dimension for object renaming declaration. Check
263 -- the dimensions of the type and of the renamed object name of N match.
265 procedure Analyze_Dimension_Simple_Return_Statement (N : Node_Id);
266 -- Subroutine of Analyze_Dimension for simple return statement
267 -- Check that the dimensions of the returned type and of the returned
268 -- expression match.
270 procedure Analyze_Dimension_Subtype_Declaration (N : Node_Id);
271 -- Subroutine of Analyze_Dimension for subtype declaration. Propagate the
272 -- dimensions from the parent type to the identifier of N. Note that if
273 -- both the identifier and the parent type of N are not dimensionless,
274 -- return an error.
276 procedure Analyze_Dimension_Unary_Op (N : Node_Id);
277 -- Subroutine of Analyze_Dimension for unary operators. For Plus, Minus and
278 -- Abs operators, propagate the dimensions from the operand to N.
280 function Create_Rational_From
281 (Expr : Node_Id;
282 Complain : Boolean) return Rational;
283 -- Given an arbitrary expression Expr, return a valid rational if Expr can
284 -- be interpreted as a rational. Otherwise return No_Rational and also an
285 -- error message if Complain is set to True.
287 function Dimensions_Of (N : Node_Id) return Dimension_Type;
288 -- Return the dimension vector of node N
290 function Dimensions_Msg_Of
291 (N : Node_Id;
292 Description_Needed : Boolean := False) return String;
293 -- Given a node N, return the dimension symbols of N, preceded by "has
294 -- dimension" if Description_Needed. if N is dimensionless, return "[]", or
295 -- "is dimensionless" if Description_Needed.
297 procedure Dim_Warning_For_Numeric_Literal (N : Node_Id; Typ : Entity_Id);
298 -- Issue a warning on the given numeric literal N to indicate the
299 -- compilateur made the assumption that the literal is not dimensionless
300 -- but has the dimension of Typ.
302 procedure Eval_Op_Expon_With_Rational_Exponent
303 (N : Node_Id;
304 Exponent_Value : Rational);
305 -- Evaluate the exponent it is a rational and the operand has a dimension
307 function Exists (Dim : Dimension_Type) return Boolean;
308 -- Returns True iff Dim does not denote the null dimension
310 function Exists (Str : String_Id) return Boolean;
311 -- Returns True iff Str does not denote No_String
313 function Exists (Sys : System_Type) return Boolean;
314 -- Returns True iff Sys does not denote the null system
316 function From_Dim_To_Str_Of_Dim_Symbols
317 (Dims : Dimension_Type;
318 System : System_Type;
319 In_Error_Msg : Boolean := False) return String_Id;
320 -- Given a dimension vector and a dimension system, return the proper
321 -- string of dimension symbols. If In_Error_Msg is True (i.e. the String_Id
322 -- will be used to issue an error message) then this routine has a special
323 -- handling for the insertion character asterisk * which must be precede by
324 -- a quote ' to to be placed literally into the message.
326 function From_Dim_To_Str_Of_Unit_Symbols
327 (Dims : Dimension_Type;
328 System : System_Type) return String_Id;
329 -- Given a dimension vector and a dimension system, return the proper
330 -- string of unit symbols.
332 function Is_Dim_IO_Package_Entity (E : Entity_Id) return Boolean;
333 -- Return True if E is the package entity of System.Dim.Float_IO or
334 -- System.Dim.Integer_IO.
336 function Is_Invalid (Position : Dimension_Position) return Boolean;
337 -- Return True if Pos denotes the invalid position
339 procedure Move_Dimensions (From : Node_Id; To : Node_Id);
340 -- Copy dimension vector of From to To and delete dimension vector of From
342 procedure Remove_Dimensions (N : Node_Id);
343 -- Remove the dimension vector of node N
345 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type);
346 -- Associate a dimension vector with a node
348 procedure Set_Symbol (E : Entity_Id; Val : String_Id);
349 -- Associate a symbol representation of a dimension vector with a subtype
351 function String_From_Numeric_Literal (N : Node_Id) return String_Id;
352 -- Return the string that corresponds to the numeric litteral N as it
353 -- appears in the source.
355 function Symbol_Of (E : Entity_Id) return String_Id;
356 -- E denotes a subtype with a dimension. Return the symbol representation
357 -- of the dimension vector.
359 function System_Of (E : Entity_Id) return System_Type;
360 -- E denotes a type, return associated system of the type if it has one
362 ---------
363 -- "+" --
364 ---------
366 function "+" (Right : Whole) return Rational is
367 begin
368 return Rational'(Numerator => Right,
369 Denominator => 1);
370 end "+";
372 function "+" (Left, Right : Rational) return Rational is
373 R : constant Rational :=
374 Rational'(Numerator => Left.Numerator * Right.Denominator +
375 Left.Denominator * Right.Numerator,
376 Denominator => Left.Denominator * Right.Denominator);
377 begin
378 return Reduce (R);
379 end "+";
381 ---------
382 -- "-" --
383 ---------
385 function "-" (Right : Rational) return Rational is
386 begin
387 return Rational'(Numerator => -Right.Numerator,
388 Denominator => Right.Denominator);
389 end "-";
391 function "-" (Left, Right : Rational) return Rational is
392 R : constant Rational :=
393 Rational'(Numerator => Left.Numerator * Right.Denominator -
394 Left.Denominator * Right.Numerator,
395 Denominator => Left.Denominator * Right.Denominator);
397 begin
398 return Reduce (R);
399 end "-";
401 ---------
402 -- "*" --
403 ---------
405 function "*" (Left, Right : Rational) return Rational is
406 R : constant Rational :=
407 Rational'(Numerator => Left.Numerator * Right.Numerator,
408 Denominator => Left.Denominator * Right.Denominator);
409 begin
410 return Reduce (R);
411 end "*";
413 ---------
414 -- "/" --
415 ---------
417 function "/" (Left, Right : Rational) return Rational is
418 R : constant Rational := abs Right;
419 L : Rational := Left;
421 begin
422 if Right.Numerator < 0 then
423 L.Numerator := Whole (-Integer (L.Numerator));
424 end if;
426 return Reduce (Rational'(Numerator => L.Numerator * R.Denominator,
427 Denominator => L.Denominator * R.Numerator));
428 end "/";
430 -----------
431 -- "abs" --
432 -----------
434 function "abs" (Right : Rational) return Rational is
435 begin
436 return Rational'(Numerator => abs Right.Numerator,
437 Denominator => Right.Denominator);
438 end "abs";
440 ------------------------------
441 -- Analyze_Aspect_Dimension --
442 ------------------------------
444 -- with Dimension =>
445 -- ([Symbol =>] SYMBOL, DIMENSION_VALUE {, DIMENSION_Value})
447 -- SYMBOL ::= STRING_LITERAL | CHARACTER_LITERAL
449 -- DIMENSION_VALUE ::=
450 -- RATIONAL
451 -- | others => RATIONAL
452 -- | DISCRETE_CHOICE_LIST => RATIONAL
454 -- RATIONAL ::= [-] NUMERIC_LITERAL [/ NUMERIC_LITERAL]
456 -- Note that when the dimensioned type is an integer type, then any
457 -- dimension value must be an integer literal.
459 procedure Analyze_Aspect_Dimension
460 (N : Node_Id;
461 Id : Entity_Id;
462 Aggr : Node_Id)
464 Def_Id : constant Entity_Id := Defining_Identifier (N);
466 Processed : array (Dimension_Type'Range) of Boolean := (others => False);
467 -- This array is used when processing ranges or Others_Choice as part of
468 -- the dimension aggregate.
470 Dimensions : Dimension_Type := Null_Dimension;
472 procedure Extract_Power
473 (Expr : Node_Id;
474 Position : Dimension_Position);
475 -- Given an expression with denotes a rational number, read the number
476 -- and associate it with Position in Dimensions.
478 function Position_In_System
479 (Id : Node_Id;
480 System : System_Type) return Dimension_Position;
481 -- Given an identifier which denotes a dimension, return the position of
482 -- that dimension within System.
484 -------------------
485 -- Extract_Power --
486 -------------------
488 procedure Extract_Power
489 (Expr : Node_Id;
490 Position : Dimension_Position)
492 begin
493 -- Integer case
495 if Is_Integer_Type (Def_Id) then
496 -- Dimension value must be an integer literal
498 if Nkind (Expr) = N_Integer_Literal then
499 Dimensions (Position) := +Whole (UI_To_Int (Intval (Expr)));
500 else
501 Error_Msg_N ("integer literal expected", Expr);
502 end if;
504 -- Float case
506 else
507 Dimensions (Position) := Create_Rational_From (Expr, True);
508 end if;
510 Processed (Position) := True;
511 end Extract_Power;
513 ------------------------
514 -- Position_In_System --
515 ------------------------
517 function Position_In_System
518 (Id : Node_Id;
519 System : System_Type) return Dimension_Position
521 Dimension_Name : constant Name_Id := Chars (Id);
523 begin
524 for Position in System.Unit_Names'Range loop
525 if Dimension_Name = System.Unit_Names (Position) then
526 return Position;
527 end if;
528 end loop;
530 return Invalid_Position;
531 end Position_In_System;
533 -- Local variables
535 Assoc : Node_Id;
536 Choice : Node_Id;
537 Expr : Node_Id;
538 Num_Choices : Nat := 0;
539 Num_Dimensions : Nat := 0;
540 Others_Seen : Boolean := False;
541 Position : Nat := 0;
542 Sub_Ind : Node_Id;
543 Symbol : String_Id := No_String;
544 Symbol_Expr : Node_Id;
545 System : System_Type;
546 Typ : Entity_Id;
548 Errors_Count : Nat;
549 -- Errors_Count is a count of errors detected by the compiler so far
550 -- just before the extraction of symbol, names and values in the
551 -- aggregate (Step 2).
553 -- At the end of the analysis, there is a check to verify that this
554 -- count equals to Serious_Errors_Detected i.e. no erros have been
555 -- encountered during the process. Otherwise the Dimension_Table is
556 -- not filled.
558 -- Start of processing for Analyze_Aspect_Dimension
560 begin
561 -- STEP 1: Legality of aspect
563 if Nkind (N) /= N_Subtype_Declaration then
564 Error_Msg_NE ("aspect& must apply to subtype declaration", N, Id);
565 return;
566 end if;
568 Sub_Ind := Subtype_Indication (N);
569 Typ := Etype (Sub_Ind);
570 System := System_Of (Typ);
572 if Nkind (Sub_Ind) = N_Subtype_Indication then
573 Error_Msg_NE
574 ("constraint not allowed with aspect&", Constraint (Sub_Ind), Id);
575 return;
576 end if;
578 -- The dimension declarations are useless if the parent type does not
579 -- declare a valid system.
581 if not Exists (System) then
582 Error_Msg_NE
583 ("parent type of& lacks dimension system", Sub_Ind, Def_Id);
584 return;
585 end if;
587 if Nkind (Aggr) /= N_Aggregate then
588 Error_Msg_N ("aggregate expected", Aggr);
589 return;
590 end if;
592 -- STEP 2: Symbol, Names and values extraction
594 -- Get the number of errors detected by the compiler so far
596 Errors_Count := Serious_Errors_Detected;
598 -- STEP 2a: Symbol extraction
600 -- The first entry in the aggregate may be the symbolic representation
601 -- of the quantity.
603 -- Positional symbol argument
605 Symbol_Expr := First (Expressions (Aggr));
607 -- Named symbol argument
609 if No (Symbol_Expr)
610 or else not Nkind_In (Symbol_Expr, N_Character_Literal,
611 N_String_Literal)
612 then
613 Symbol_Expr := Empty;
615 -- Component associations present
617 if Present (Component_Associations (Aggr)) then
618 Assoc := First (Component_Associations (Aggr));
619 Choice := First (Choices (Assoc));
621 if No (Next (Choice)) and then Nkind (Choice) = N_Identifier then
623 -- Symbol component association is present
625 if Chars (Choice) = Name_Symbol then
626 Num_Choices := Num_Choices + 1;
627 Symbol_Expr := Expression (Assoc);
629 -- Verify symbol expression is a string or a character
631 if not Nkind_In (Symbol_Expr, N_Character_Literal,
632 N_String_Literal)
633 then
634 Symbol_Expr := Empty;
635 Error_Msg_N
636 ("symbol expression must be character or string",
637 Symbol_Expr);
638 end if;
640 -- Special error if no Symbol choice but expression is string
641 -- or character.
643 elsif Nkind_In (Expression (Assoc), N_Character_Literal,
644 N_String_Literal)
645 then
646 Num_Choices := Num_Choices + 1;
647 Error_Msg_N ("optional component Symbol expected, found&",
648 Choice);
649 end if;
650 end if;
651 end if;
652 end if;
654 -- STEP 2b: Names and values extraction
656 -- Positional elements
658 Expr := First (Expressions (Aggr));
660 -- Skip the symbol expression when present
662 if Present (Symbol_Expr) and then Num_Choices = 0 then
663 Expr := Next (Expr);
664 end if;
666 Position := Low_Position_Bound;
667 while Present (Expr) loop
668 if Position > High_Position_Bound then
669 Error_Msg_N
670 ("type& has more dimensions than system allows", Def_Id);
671 exit;
672 end if;
674 Extract_Power (Expr, Position);
676 Position := Position + 1;
677 Num_Dimensions := Num_Dimensions + 1;
679 Next (Expr);
680 end loop;
682 -- Named elements
684 Assoc := First (Component_Associations (Aggr));
686 -- Skip the symbol association when present
688 if Num_Choices = 1 then
689 Next (Assoc);
690 end if;
692 while Present (Assoc) loop
693 Expr := Expression (Assoc);
695 Choice := First (Choices (Assoc));
696 while Present (Choice) loop
698 -- Identifier case: NAME => EXPRESSION
700 if Nkind (Choice) = N_Identifier then
701 Position := Position_In_System (Choice, System);
703 if Is_Invalid (Position) then
704 Error_Msg_N ("dimension name& not part of system", Choice);
705 else
706 Extract_Power (Expr, Position);
707 end if;
709 -- Range case: NAME .. NAME => EXPRESSION
711 elsif Nkind (Choice) = N_Range then
712 declare
713 Low : constant Node_Id := Low_Bound (Choice);
714 High : constant Node_Id := High_Bound (Choice);
715 Low_Pos : Dimension_Position;
716 High_Pos : Dimension_Position;
718 begin
719 if Nkind (Low) /= N_Identifier then
720 Error_Msg_N ("bound must denote a dimension name", Low);
722 elsif Nkind (High) /= N_Identifier then
723 Error_Msg_N ("bound must denote a dimension name", High);
725 else
726 Low_Pos := Position_In_System (Low, System);
727 High_Pos := Position_In_System (High, System);
729 if Is_Invalid (Low_Pos) then
730 Error_Msg_N ("dimension name& not part of system",
731 Low);
733 elsif Is_Invalid (High_Pos) then
734 Error_Msg_N ("dimension name& not part of system",
735 High);
737 elsif Low_Pos > High_Pos then
738 Error_Msg_N ("expected low to high range", Choice);
740 else
741 for Position in Low_Pos .. High_Pos loop
742 Extract_Power (Expr, Position);
743 end loop;
744 end if;
745 end if;
746 end;
748 -- Others case: OTHERS => EXPRESSION
750 elsif Nkind (Choice) = N_Others_Choice then
751 if Present (Next (Choice)) or else Present (Prev (Choice)) then
752 Error_Msg_N
753 ("OTHERS must appear alone in a choice list", Choice);
755 elsif Present (Next (Assoc)) then
756 Error_Msg_N
757 ("OTHERS must appear last in an aggregate", Choice);
759 elsif Others_Seen then
760 Error_Msg_N ("multiple OTHERS not allowed", Choice);
762 else
763 -- Fill the non-processed dimensions with the default value
764 -- supplied by others.
766 for Position in Processed'Range loop
767 if not Processed (Position) then
768 Extract_Power (Expr, Position);
769 end if;
770 end loop;
771 end if;
773 Others_Seen := True;
775 -- All other cases are erroneous declarations of dimension names
777 else
778 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
779 end if;
781 Num_Choices := Num_Choices + 1;
782 Next (Choice);
783 end loop;
785 Num_Dimensions := Num_Dimensions + 1;
786 Next (Assoc);
787 end loop;
789 -- STEP 3: Consistency of system and dimensions
791 if Present (First (Expressions (Aggr)))
792 and then (First (Expressions (Aggr)) /= Symbol_Expr
793 or else Present (Next (Symbol_Expr)))
794 and then (Num_Choices > 1
795 or else (Num_Choices = 1 and then not Others_Seen))
796 then
797 Error_Msg_N
798 ("named associations cannot follow positional associations", Aggr);
799 end if;
801 if Num_Dimensions > System.Count then
802 Error_Msg_N ("type& has more dimensions than system allows", Def_Id);
804 elsif Num_Dimensions < System.Count and then not Others_Seen then
805 Error_Msg_N ("type& has less dimensions than system allows", Def_Id);
806 end if;
808 -- STEP 4: Dimension symbol extraction
810 if Present (Symbol_Expr) then
811 if Nkind (Symbol_Expr) = N_Character_Literal then
812 Start_String;
813 Store_String_Char (UI_To_CC (Char_Literal_Value (Symbol_Expr)));
814 Symbol := End_String;
816 else
817 Symbol := Strval (Symbol_Expr);
818 end if;
820 if String_Length (Symbol) = 0 then
821 Error_Msg_N ("empty string not allowed here", Symbol_Expr);
822 end if;
823 end if;
825 -- STEP 5: Storage of extracted values
827 -- Check that no errors have been detected during the analysis
829 if Errors_Count = Serious_Errors_Detected then
831 -- Check for useless declaration
833 if Symbol = No_String and then not Exists (Dimensions) then
834 Error_Msg_N ("useless dimension declaration", Aggr);
835 end if;
837 if Symbol /= No_String then
838 Set_Symbol (Def_Id, Symbol);
839 end if;
841 if Exists (Dimensions) then
842 Set_Dimensions (Def_Id, Dimensions);
843 end if;
844 end if;
845 end Analyze_Aspect_Dimension;
847 -------------------------------------
848 -- Analyze_Aspect_Dimension_System --
849 -------------------------------------
851 -- with Dimension_System => (DIMENSION {, DIMENSION});
853 -- DIMENSION ::= (
854 -- [Unit_Name =>] IDENTIFIER,
855 -- [Unit_Symbol =>] SYMBOL,
856 -- [Dim_Symbol =>] SYMBOL)
858 procedure Analyze_Aspect_Dimension_System
859 (N : Node_Id;
860 Id : Entity_Id;
861 Aggr : Node_Id)
863 function Is_Derived_Numeric_Type (N : Node_Id) return Boolean;
864 -- Determine whether type declaration N denotes a numeric derived type
866 -------------------------------
867 -- Is_Derived_Numeric_Type --
868 -------------------------------
870 function Is_Derived_Numeric_Type (N : Node_Id) return Boolean is
871 begin
872 return
873 Nkind (N) = N_Full_Type_Declaration
874 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
875 and then Is_Numeric_Type
876 (Entity (Subtype_Indication (Type_Definition (N))));
877 end Is_Derived_Numeric_Type;
879 -- Local variables
881 Assoc : Node_Id;
882 Choice : Node_Id;
883 Dim_Aggr : Node_Id;
884 Dim_Symbol : Node_Id;
885 Dim_Symbols : Symbol_Array := No_Symbols;
886 Dim_System : System_Type := Null_System;
887 Position : Nat := 0;
888 Unit_Name : Node_Id;
889 Unit_Names : Name_Array := No_Names;
890 Unit_Symbol : Node_Id;
891 Unit_Symbols : Symbol_Array := No_Symbols;
893 Errors_Count : Nat;
894 -- Errors_Count is a count of errors detected by the compiler so far
895 -- just before the extraction of names and symbols in the aggregate
896 -- (Step 3).
898 -- At the end of the analysis, there is a check to verify that this
899 -- count equals Serious_Errors_Detected i.e. no errors have been
900 -- encountered during the process. Otherwise the System_Table is
901 -- not filled.
903 -- Start of processing for Analyze_Aspect_Dimension_System
905 begin
906 -- STEP 1: Legality of aspect
908 if not Is_Derived_Numeric_Type (N) then
909 Error_Msg_NE
910 ("aspect& must apply to numeric derived type declaration", N, Id);
911 return;
912 end if;
914 if Nkind (Aggr) /= N_Aggregate then
915 Error_Msg_N ("aggregate expected", Aggr);
916 return;
917 end if;
919 -- STEP 2: Structural verification of the dimension aggregate
921 if Present (Component_Associations (Aggr)) then
922 Error_Msg_N ("expected positional aggregate", Aggr);
923 return;
924 end if;
926 -- STEP 3: Name and Symbol extraction
928 Dim_Aggr := First (Expressions (Aggr));
929 Errors_Count := Serious_Errors_Detected;
930 while Present (Dim_Aggr) loop
931 Position := Position + 1;
933 if Position > High_Position_Bound then
934 Error_Msg_N
935 ("too many dimensions in system", Aggr);
936 exit;
937 end if;
939 if Nkind (Dim_Aggr) /= N_Aggregate then
940 Error_Msg_N ("aggregate expected", Dim_Aggr);
942 else
943 if Present (Component_Associations (Dim_Aggr))
944 and then Present (Expressions (Dim_Aggr))
945 then
946 Error_Msg_N
947 ("mixed positional/named aggregate not allowed here",
948 Dim_Aggr);
950 -- Verify each dimension aggregate has three arguments
952 elsif List_Length (Component_Associations (Dim_Aggr)) /= 3
953 and then List_Length (Expressions (Dim_Aggr)) /= 3
954 then
955 Error_Msg_N
956 ("three components expected in aggregate", Dim_Aggr);
958 else
959 -- Named dimension aggregate
961 if Present (Component_Associations (Dim_Aggr)) then
963 -- Check first argument denotes the unit name
965 Assoc := First (Component_Associations (Dim_Aggr));
966 Choice := First (Choices (Assoc));
967 Unit_Name := Expression (Assoc);
969 if Present (Next (Choice))
970 or else Nkind (Choice) /= N_Identifier
971 then
972 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
974 elsif Chars (Choice) /= Name_Unit_Name then
975 Error_Msg_N ("expected Unit_Name, found&", Choice);
976 end if;
978 -- Check the second argument denotes the unit symbol
980 Next (Assoc);
981 Choice := First (Choices (Assoc));
982 Unit_Symbol := Expression (Assoc);
984 if Present (Next (Choice))
985 or else Nkind (Choice) /= N_Identifier
986 then
987 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
989 elsif Chars (Choice) /= Name_Unit_Symbol then
990 Error_Msg_N ("expected Unit_Symbol, found&", Choice);
991 end if;
993 -- Check the third argument denotes the dimension symbol
995 Next (Assoc);
996 Choice := First (Choices (Assoc));
997 Dim_Symbol := Expression (Assoc);
999 if Present (Next (Choice))
1000 or else Nkind (Choice) /= N_Identifier
1001 then
1002 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
1004 elsif Chars (Choice) /= Name_Dim_Symbol then
1005 Error_Msg_N ("expected Dim_Symbol, found&", Choice);
1006 end if;
1008 -- Positional dimension aggregate
1010 else
1011 Unit_Name := First (Expressions (Dim_Aggr));
1012 Unit_Symbol := Next (Unit_Name);
1013 Dim_Symbol := Next (Unit_Symbol);
1014 end if;
1016 -- Check the first argument for each dimension aggregate is
1017 -- a name.
1019 if Nkind (Unit_Name) = N_Identifier then
1020 Unit_Names (Position) := Chars (Unit_Name);
1021 else
1022 Error_Msg_N ("expected unit name", Unit_Name);
1023 end if;
1025 -- Check the second argument for each dimension aggregate is
1026 -- a string or a character.
1028 if not Nkind_In (Unit_Symbol, N_String_Literal,
1029 N_Character_Literal)
1030 then
1031 Error_Msg_N
1032 ("expected unit symbol (string or character)",
1033 Unit_Symbol);
1035 else
1036 -- String case
1038 if Nkind (Unit_Symbol) = N_String_Literal then
1039 Unit_Symbols (Position) := Strval (Unit_Symbol);
1041 -- Character case
1043 else
1044 Start_String;
1045 Store_String_Char
1046 (UI_To_CC (Char_Literal_Value (Unit_Symbol)));
1047 Unit_Symbols (Position) := End_String;
1048 end if;
1050 -- Verify that the string is not empty
1052 if String_Length (Unit_Symbols (Position)) = 0 then
1053 Error_Msg_N
1054 ("empty string not allowed here", Unit_Symbol);
1055 end if;
1056 end if;
1058 -- Check the third argument for each dimension aggregate is
1059 -- a string or a character.
1061 if not Nkind_In (Dim_Symbol, N_String_Literal,
1062 N_Character_Literal)
1063 then
1064 Error_Msg_N
1065 ("expected dimension symbol (string or character)",
1066 Dim_Symbol);
1068 else
1069 -- String case
1071 if Nkind (Dim_Symbol) = N_String_Literal then
1072 Dim_Symbols (Position) := Strval (Dim_Symbol);
1074 -- Character case
1076 else
1077 Start_String;
1078 Store_String_Char
1079 (UI_To_CC (Char_Literal_Value (Dim_Symbol)));
1080 Dim_Symbols (Position) := End_String;
1081 end if;
1083 -- Verify that the string is not empty
1085 if String_Length (Dim_Symbols (Position)) = 0 then
1086 Error_Msg_N
1087 ("empty string not allowed here", Dim_Symbol);
1088 end if;
1089 end if;
1090 end if;
1091 end if;
1093 Next (Dim_Aggr);
1094 end loop;
1096 -- STEP 4: Storage of extracted values
1098 -- Check that no errors have been detected during the analysis
1100 if Errors_Count = Serious_Errors_Detected then
1101 Dim_System.Type_Decl := N;
1102 Dim_System.Unit_Names := Unit_Names;
1103 Dim_System.Unit_Symbols := Unit_Symbols;
1104 Dim_System.Dim_Symbols := Dim_Symbols;
1105 Dim_System.Count := Position;
1106 System_Table.Append (Dim_System);
1107 end if;
1108 end Analyze_Aspect_Dimension_System;
1110 -----------------------
1111 -- Analyze_Dimension --
1112 -----------------------
1114 -- This dispatch routine propagates dimensions for each node
1116 procedure Analyze_Dimension (N : Node_Id) is
1117 begin
1118 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1119 -- dimensions for nodes that don't come from source.
1121 if Ada_Version < Ada_2012 or else not Comes_From_Source (N) then
1122 return;
1123 end if;
1125 case Nkind (N) is
1126 when N_Assignment_Statement =>
1127 Analyze_Dimension_Assignment_Statement (N);
1129 when N_Binary_Op =>
1130 Analyze_Dimension_Binary_Op (N);
1132 when N_Component_Declaration =>
1133 Analyze_Dimension_Component_Declaration (N);
1135 when N_Extended_Return_Statement =>
1136 Analyze_Dimension_Extended_Return_Statement (N);
1138 when N_Attribute_Reference |
1139 N_Expanded_Name |
1140 N_Function_Call |
1141 N_Identifier |
1142 N_Indexed_Component |
1143 N_Qualified_Expression |
1144 N_Selected_Component |
1145 N_Slice |
1146 N_Type_Conversion |
1147 N_Unchecked_Type_Conversion =>
1148 Analyze_Dimension_Has_Etype (N);
1150 when N_Object_Declaration =>
1151 Analyze_Dimension_Object_Declaration (N);
1153 when N_Object_Renaming_Declaration =>
1154 Analyze_Dimension_Object_Renaming_Declaration (N);
1156 when N_Simple_Return_Statement =>
1157 if not Comes_From_Extended_Return_Statement (N) then
1158 Analyze_Dimension_Simple_Return_Statement (N);
1159 end if;
1161 when N_Subtype_Declaration =>
1162 Analyze_Dimension_Subtype_Declaration (N);
1164 when N_Unary_Op =>
1165 Analyze_Dimension_Unary_Op (N);
1167 when others => null;
1169 end case;
1170 end Analyze_Dimension;
1172 ---------------------------------------
1173 -- Analyze_Dimension_Array_Aggregate --
1174 ---------------------------------------
1176 procedure Analyze_Dimension_Array_Aggregate
1177 (N : Node_Id;
1178 Comp_Typ : Entity_Id)
1180 Comp_Ass : constant List_Id := Component_Associations (N);
1181 Dims_Of_Comp_Typ : constant Dimension_Type := Dimensions_Of (Comp_Typ);
1182 Exps : constant List_Id := Expressions (N);
1184 Comp : Node_Id;
1185 Expr : Node_Id;
1187 Error_Detected : Boolean := False;
1188 -- This flag is used in order to indicate if an error has been detected
1189 -- so far by the compiler in this routine.
1191 begin
1192 -- Aspect is an Ada 2012 feature. Nothing to do here if the component
1193 -- base type is not a dimensioned type.
1195 -- Note that here the original node must come from source since the
1196 -- original array aggregate may not have been entirely decorated.
1198 if Ada_Version < Ada_2012
1199 or else not Comes_From_Source (Original_Node (N))
1200 or else not Has_Dimension_System (Base_Type (Comp_Typ))
1201 then
1202 return;
1203 end if;
1205 -- Check whether there is any positional component association
1207 if Is_Empty_List (Exps) then
1208 Comp := First (Comp_Ass);
1209 else
1210 Comp := First (Exps);
1211 end if;
1213 while Present (Comp) loop
1215 -- Get the expression from the component
1217 if Nkind (Comp) = N_Component_Association then
1218 Expr := Expression (Comp);
1219 else
1220 Expr := Comp;
1221 end if;
1223 -- Issue an error if the dimensions of the component type and the
1224 -- dimensions of the component mismatch.
1226 -- Note that we must ensure the expression has been fully analyzed
1227 -- since it may not be decorated at this point. We also don't want to
1228 -- issue the same error message multiple times on the same expression
1229 -- (may happen when an aggregate is converted into a positional
1230 -- aggregate).
1232 if Comes_From_Source (Original_Node (Expr))
1233 and then Present (Etype (Expr))
1234 and then Dimensions_Of (Expr) /= Dims_Of_Comp_Typ
1235 and then Sloc (Comp) /= Sloc (Prev (Comp))
1236 then
1237 -- Check if an error has already been encountered so far
1239 if not Error_Detected then
1240 Error_Msg_N ("dimensions mismatch in array aggregate", N);
1241 Error_Detected := True;
1242 end if;
1244 Error_Msg_N
1245 ("\expected dimension "
1246 & Dimensions_Msg_Of (Comp_Typ)
1247 & ", found "
1248 & Dimensions_Msg_Of (Expr),
1249 Expr);
1250 end if;
1252 -- Look at the named components right after the positional components
1254 if not Present (Next (Comp))
1255 and then List_Containing (Comp) = Exps
1256 then
1257 Comp := First (Comp_Ass);
1258 else
1259 Next (Comp);
1260 end if;
1261 end loop;
1262 end Analyze_Dimension_Array_Aggregate;
1264 --------------------------------------------
1265 -- Analyze_Dimension_Assignment_Statement --
1266 --------------------------------------------
1268 procedure Analyze_Dimension_Assignment_Statement (N : Node_Id) is
1269 Lhs : constant Node_Id := Name (N);
1270 Dims_Of_Lhs : constant Dimension_Type := Dimensions_Of (Lhs);
1271 Rhs : constant Node_Id := Expression (N);
1272 Dims_Of_Rhs : constant Dimension_Type := Dimensions_Of (Rhs);
1274 procedure Error_Dim_Msg_For_Assignment_Statement
1275 (N : Node_Id;
1276 Lhs : Node_Id;
1277 Rhs : Node_Id);
1278 -- Error using Error_Msg_N at node N. Output the dimensions of left
1279 -- and right hand sides.
1281 --------------------------------------------
1282 -- Error_Dim_Msg_For_Assignment_Statement --
1283 --------------------------------------------
1285 procedure Error_Dim_Msg_For_Assignment_Statement
1286 (N : Node_Id;
1287 Lhs : Node_Id;
1288 Rhs : Node_Id)
1290 begin
1291 Error_Msg_N ("dimensions mismatch in assignment", N);
1292 Error_Msg_N ("\left-hand side " & Dimensions_Msg_Of (Lhs, True), N);
1293 Error_Msg_N ("\right-hand side " & Dimensions_Msg_Of (Rhs, True), N);
1294 end Error_Dim_Msg_For_Assignment_Statement;
1296 -- Start of processing for Analyze_Dimension_Assignment
1298 begin
1299 if Dims_Of_Lhs /= Dims_Of_Rhs then
1300 Error_Dim_Msg_For_Assignment_Statement (N, Lhs, Rhs);
1301 end if;
1302 end Analyze_Dimension_Assignment_Statement;
1304 ---------------------------------
1305 -- Analyze_Dimension_Binary_Op --
1306 ---------------------------------
1308 -- Check and propagate the dimensions for binary operators
1309 -- Note that when the dimensions mismatch, no dimension is propagated to N.
1311 procedure Analyze_Dimension_Binary_Op (N : Node_Id) is
1312 N_Kind : constant Node_Kind := Nkind (N);
1314 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id);
1315 -- Error using Error_Msg_NE and Error_Msg_N at node N. Output the
1316 -- dimensions of both operands.
1318 ---------------------------------
1319 -- Error_Dim_Msg_For_Binary_Op --
1320 ---------------------------------
1322 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id) is
1323 begin
1324 Error_Msg_NE ("both operands for operation& must have same " &
1325 "dimensions",
1327 Entity (N));
1328 Error_Msg_N ("\left operand " & Dimensions_Msg_Of (L, True), N);
1329 Error_Msg_N ("\right operand " & Dimensions_Msg_Of (R, True), N);
1330 end Error_Dim_Msg_For_Binary_Op;
1332 -- Start of processing for Analyze_Dimension_Binary_Op
1334 begin
1335 if Nkind_In (N_Kind, N_Op_Add, N_Op_Expon, N_Op_Subtract)
1336 or else N_Kind in N_Multiplying_Operator
1337 or else N_Kind in N_Op_Compare
1338 then
1339 declare
1340 L : constant Node_Id := Left_Opnd (N);
1341 Dims_Of_L : constant Dimension_Type := Dimensions_Of (L);
1342 L_Has_Dimensions : constant Boolean := Exists (Dims_Of_L);
1343 R : constant Node_Id := Right_Opnd (N);
1344 Dims_Of_R : constant Dimension_Type := Dimensions_Of (R);
1345 R_Has_Dimensions : constant Boolean := Exists (Dims_Of_R);
1346 Dims_Of_N : Dimension_Type := Null_Dimension;
1348 begin
1349 -- N_Op_Add, N_Op_Mod, N_Op_Rem or N_Op_Subtract case
1351 if Nkind_In (N, N_Op_Add, N_Op_Mod, N_Op_Rem, N_Op_Subtract) then
1353 -- Check both operands have same dimension
1355 if Dims_Of_L /= Dims_Of_R then
1356 Error_Dim_Msg_For_Binary_Op (N, L, R);
1357 else
1358 -- Check both operands are not dimensionless
1360 if Exists (Dims_Of_L) then
1361 Set_Dimensions (N, Dims_Of_L);
1362 end if;
1363 end if;
1365 -- N_Op_Multiply or N_Op_Divide case
1367 elsif Nkind_In (N_Kind, N_Op_Multiply, N_Op_Divide) then
1369 -- Check at least one operand is not dimensionless
1371 if L_Has_Dimensions or R_Has_Dimensions then
1373 -- Multiplication case
1375 -- Get both operands dimensions and add them
1377 if N_Kind = N_Op_Multiply then
1378 for Position in Dimension_Type'Range loop
1379 Dims_Of_N (Position) :=
1380 Dims_Of_L (Position) + Dims_Of_R (Position);
1381 end loop;
1383 -- Division case
1385 -- Get both operands dimensions and subtract them
1387 else
1388 for Position in Dimension_Type'Range loop
1389 Dims_Of_N (Position) :=
1390 Dims_Of_L (Position) - Dims_Of_R (Position);
1391 end loop;
1392 end if;
1394 if Exists (Dims_Of_N) then
1395 Set_Dimensions (N, Dims_Of_N);
1396 end if;
1397 end if;
1399 -- Exponentiation case
1401 -- Note: a rational exponent is allowed for dimensioned operand
1403 elsif N_Kind = N_Op_Expon then
1405 -- Check the left operand is not dimensionless. Note that the
1406 -- value of the exponent must be known compile time. Otherwise,
1407 -- the exponentiation evaluation will return an error message.
1409 if L_Has_Dimensions then
1410 if not Compile_Time_Known_Value (R) then
1411 Error_Msg_N ("exponent of dimensioned operand must be " &
1412 "known at compile time", N);
1413 end if;
1415 declare
1416 Exponent_Value : Rational := Zero;
1418 begin
1419 -- Real operand case
1421 if Is_Real_Type (Etype (L)) then
1423 -- Define the exponent as a Rational number
1425 Exponent_Value := Create_Rational_From (R, False);
1427 -- Verify that the exponent cannot be interpreted
1428 -- as a rational, otherwise interpret the exponent
1429 -- as an integer.
1431 if Exponent_Value = No_Rational then
1432 Exponent_Value :=
1433 +Whole (UI_To_Int (Expr_Value (R)));
1434 end if;
1436 -- Integer operand case.
1438 -- For integer operand, the exponent cannot be
1439 -- interpreted as a rational.
1441 else
1442 Exponent_Value := +Whole (UI_To_Int (Expr_Value (R)));
1443 end if;
1445 for Position in Dimension_Type'Range loop
1446 Dims_Of_N (Position) :=
1447 Dims_Of_L (Position) * Exponent_Value;
1448 end loop;
1450 if Exists (Dims_Of_N) then
1451 Set_Dimensions (N, Dims_Of_N);
1452 end if;
1453 end;
1454 end if;
1456 -- Comparison cases
1458 -- For relational operations, only dimension checking is
1459 -- performed (no propagation).
1461 elsif N_Kind in N_Op_Compare then
1462 if (L_Has_Dimensions or R_Has_Dimensions)
1463 and then Dims_Of_L /= Dims_Of_R
1464 then
1465 Error_Dim_Msg_For_Binary_Op (N, L, R);
1466 end if;
1467 end if;
1469 -- Removal of dimensions for each operands
1471 Remove_Dimensions (L);
1472 Remove_Dimensions (R);
1473 end;
1474 end if;
1475 end Analyze_Dimension_Binary_Op;
1477 ----------------------------
1478 -- Analyze_Dimension_Call --
1479 ----------------------------
1481 procedure Analyze_Dimension_Call (N : Node_Id; Nam : Entity_Id) is
1482 Actuals : constant List_Id := Parameter_Associations (N);
1483 Actual : Node_Id;
1484 Dims_Of_Formal : Dimension_Type;
1485 Formal : Node_Id;
1486 Formal_Typ : Entity_Id;
1488 Error_Detected : Boolean := False;
1489 -- This flag is used in order to indicate if an error has been detected
1490 -- so far by the compiler in this routine.
1492 begin
1493 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1494 -- dimensions for calls that don't come from source, or those that may
1495 -- have semantic errors.
1497 if Ada_Version < Ada_2012
1498 or else not Comes_From_Source (N)
1499 or else Error_Posted (N)
1500 then
1501 return;
1502 end if;
1504 -- Check the dimensions of the actuals, if any
1506 if not Is_Empty_List (Actuals) then
1508 -- Special processing for elementary functions
1510 -- For Sqrt call, the resulting dimensions equal to half the
1511 -- dimensions of the actual. For all other elementary calls, this
1512 -- routine check that every actual is dimensionless.
1514 if Nkind (N) = N_Function_Call then
1515 Elementary_Function_Calls : declare
1516 Dims_Of_Call : Dimension_Type;
1517 Ent : Entity_Id := Nam;
1519 function Is_Elementary_Function_Entity
1520 (Sub_Id : Entity_Id) return Boolean;
1521 -- Given Sub_Id, the original subprogram entity, return True
1522 -- if call is to an elementary function (see Ada.Numerics.
1523 -- Generic_Elementary_Functions).
1525 -----------------------------------
1526 -- Is_Elementary_Function_Entity --
1527 -----------------------------------
1529 function Is_Elementary_Function_Entity
1530 (Sub_Id : Entity_Id) return Boolean
1532 Loc : constant Source_Ptr := Sloc (Sub_Id);
1534 begin
1535 -- Is entity in Ada.Numerics.Generic_Elementary_Functions?
1537 return
1538 Loc > No_Location
1539 and then
1540 Is_RTU
1541 (Cunit_Entity (Get_Source_Unit (Loc)),
1542 Ada_Numerics_Generic_Elementary_Functions);
1543 end Is_Elementary_Function_Entity;
1545 -- Start of processing for Elementary_Function_Calls
1547 begin
1548 -- Get original subprogram entity following the renaming chain
1550 if Present (Alias (Ent)) then
1551 Ent := Alias (Ent);
1552 end if;
1554 -- Check the call is an Elementary function call
1556 if Is_Elementary_Function_Entity (Ent) then
1558 -- Sqrt function call case
1560 if Chars (Ent) = Name_Sqrt then
1561 Dims_Of_Call := Dimensions_Of (First_Actual (N));
1563 -- Evaluates the resulting dimensions (i.e. half the
1564 -- dimensions of the actual).
1566 if Exists (Dims_Of_Call) then
1567 for Position in Dims_Of_Call'Range loop
1568 Dims_Of_Call (Position) :=
1569 Dims_Of_Call (Position) *
1570 Rational'(Numerator => 1, Denominator => 2);
1571 end loop;
1573 Set_Dimensions (N, Dims_Of_Call);
1574 end if;
1576 -- All other elementary functions case. Note that every
1577 -- actual here should be dimensionless.
1579 else
1580 Actual := First_Actual (N);
1581 while Present (Actual) loop
1582 if Exists (Dimensions_Of (Actual)) then
1584 -- Check if error has already been encountered
1586 if not Error_Detected then
1587 Error_Msg_NE ("dimensions mismatch in call of&",
1588 N, Name (N));
1589 Error_Detected := True;
1590 end if;
1592 Error_Msg_N ("\expected dimension [], found " &
1593 Dimensions_Msg_Of (Actual),
1594 Actual);
1595 end if;
1597 Next_Actual (Actual);
1598 end loop;
1599 end if;
1601 -- Nothing more to do for elementary functions
1603 return;
1604 end if;
1605 end Elementary_Function_Calls;
1606 end if;
1608 -- General case. Check, for each parameter, the dimensions of the
1609 -- actual and its corresponding formal match. Otherwise, complain.
1611 Actual := First_Actual (N);
1612 Formal := First_Formal (Nam);
1614 while Present (Formal) loop
1616 -- A missing corresponding actual indicates that the analysis of
1617 -- the call was aborted due to a previous error.
1619 if No (Actual) then
1620 Check_Error_Detected;
1621 return;
1622 end if;
1624 Formal_Typ := Etype (Formal);
1625 Dims_Of_Formal := Dimensions_Of (Formal_Typ);
1627 -- If the formal is not dimensionless, check dimensions of formal
1628 -- and actual match. Otherwise, complain.
1630 if Exists (Dims_Of_Formal)
1631 and then Dimensions_Of (Actual) /= Dims_Of_Formal
1632 then
1633 -- Check if an error has already been encountered so far
1635 if not Error_Detected then
1636 Error_Msg_NE ("dimensions mismatch in& call", N, Name (N));
1637 Error_Detected := True;
1638 end if;
1640 Error_Msg_N
1641 ("\expected dimension " & Dimensions_Msg_Of (Formal_Typ)
1642 & ", found " & Dimensions_Msg_Of (Actual), Actual);
1643 end if;
1645 Next_Actual (Actual);
1646 Next_Formal (Formal);
1647 end loop;
1648 end if;
1650 -- For function calls, propagate the dimensions from the returned type
1652 if Nkind (N) = N_Function_Call then
1653 Analyze_Dimension_Has_Etype (N);
1654 end if;
1655 end Analyze_Dimension_Call;
1657 ---------------------------------------------
1658 -- Analyze_Dimension_Component_Declaration --
1659 ---------------------------------------------
1661 procedure Analyze_Dimension_Component_Declaration (N : Node_Id) is
1662 Expr : constant Node_Id := Expression (N);
1663 Id : constant Entity_Id := Defining_Identifier (N);
1664 Etyp : constant Entity_Id := Etype (Id);
1665 Dims_Of_Etyp : constant Dimension_Type := Dimensions_Of (Etyp);
1666 Dims_Of_Expr : Dimension_Type;
1668 procedure Error_Dim_Msg_For_Component_Declaration
1669 (N : Node_Id;
1670 Etyp : Entity_Id;
1671 Expr : Node_Id);
1672 -- Error using Error_Msg_N at node N. Output the dimensions of the
1673 -- type Etyp and the expression Expr of N.
1675 ---------------------------------------------
1676 -- Error_Dim_Msg_For_Component_Declaration --
1677 ---------------------------------------------
1679 procedure Error_Dim_Msg_For_Component_Declaration
1680 (N : Node_Id;
1681 Etyp : Entity_Id;
1682 Expr : Node_Id) is
1683 begin
1684 Error_Msg_N ("dimensions mismatch in component declaration", N);
1685 Error_Msg_N ("\expected dimension "
1686 & Dimensions_Msg_Of (Etyp)
1687 & ", found "
1688 & Dimensions_Msg_Of (Expr),
1689 Expr);
1690 end Error_Dim_Msg_For_Component_Declaration;
1692 -- Start of processing for Analyze_Dimension_Component_Declaration
1694 begin
1695 -- Expression is present
1697 if Present (Expr) then
1698 Dims_Of_Expr := Dimensions_Of (Expr);
1700 -- Check dimensions match
1702 if Dims_Of_Etyp /= Dims_Of_Expr then
1703 -- Numeric literal case. Issue a warning if the object type is not
1704 -- dimensionless to indicate the literal is treated as if its
1705 -- dimension matches the type dimension.
1707 if Nkind_In (Original_Node (Expr), N_Real_Literal,
1708 N_Integer_Literal)
1709 then
1710 Dim_Warning_For_Numeric_Literal (Expr, Etyp);
1712 -- Issue a dimension mismatch error for all other cases
1714 else
1715 Error_Dim_Msg_For_Component_Declaration (N, Etyp, Expr);
1716 end if;
1717 end if;
1718 end if;
1719 end Analyze_Dimension_Component_Declaration;
1721 -------------------------------------------------
1722 -- Analyze_Dimension_Extended_Return_Statement --
1723 -------------------------------------------------
1725 procedure Analyze_Dimension_Extended_Return_Statement (N : Node_Id) is
1726 Return_Ent : constant Entity_Id := Return_Statement_Entity (N);
1727 Return_Etyp : constant Entity_Id :=
1728 Etype (Return_Applies_To (Return_Ent));
1729 Return_Obj_Decls : constant List_Id := Return_Object_Declarations (N);
1730 Return_Obj_Decl : Node_Id;
1731 Return_Obj_Id : Entity_Id;
1732 Return_Obj_Typ : Entity_Id;
1734 procedure Error_Dim_Msg_For_Extended_Return_Statement
1735 (N : Node_Id;
1736 Return_Etyp : Entity_Id;
1737 Return_Obj_Typ : Entity_Id);
1738 -- Error using Error_Msg_N at node N. Output the dimensions of the
1739 -- returned type Return_Etyp and the returned object type Return_Obj_Typ
1740 -- of N.
1742 -------------------------------------------------
1743 -- Error_Dim_Msg_For_Extended_Return_Statement --
1744 -------------------------------------------------
1746 procedure Error_Dim_Msg_For_Extended_Return_Statement
1747 (N : Node_Id;
1748 Return_Etyp : Entity_Id;
1749 Return_Obj_Typ : Entity_Id)
1751 begin
1752 Error_Msg_N ("dimensions mismatch in extended return statement", N);
1753 Error_Msg_N ("\expected dimension "
1754 & Dimensions_Msg_Of (Return_Etyp)
1755 & ", found "
1756 & Dimensions_Msg_Of (Return_Obj_Typ),
1758 end Error_Dim_Msg_For_Extended_Return_Statement;
1760 -- Start of processing for Analyze_Dimension_Extended_Return_Statement
1762 begin
1763 if Present (Return_Obj_Decls) then
1764 Return_Obj_Decl := First (Return_Obj_Decls);
1765 while Present (Return_Obj_Decl) loop
1766 if Nkind (Return_Obj_Decl) = N_Object_Declaration then
1767 Return_Obj_Id := Defining_Identifier (Return_Obj_Decl);
1769 if Is_Return_Object (Return_Obj_Id) then
1770 Return_Obj_Typ := Etype (Return_Obj_Id);
1772 -- Issue an error message if dimensions mismatch
1774 if Dimensions_Of (Return_Etyp) /=
1775 Dimensions_Of (Return_Obj_Typ)
1776 then
1777 Error_Dim_Msg_For_Extended_Return_Statement
1778 (N, Return_Etyp, Return_Obj_Typ);
1779 return;
1780 end if;
1781 end if;
1782 end if;
1784 Next (Return_Obj_Decl);
1785 end loop;
1786 end if;
1787 end Analyze_Dimension_Extended_Return_Statement;
1789 -----------------------------------------------------
1790 -- Analyze_Dimension_Extension_Or_Record_Aggregate --
1791 -----------------------------------------------------
1793 procedure Analyze_Dimension_Extension_Or_Record_Aggregate (N : Node_Id) is
1794 Comp : Node_Id;
1795 Comp_Id : Entity_Id;
1796 Comp_Typ : Entity_Id;
1797 Expr : Node_Id;
1799 Error_Detected : Boolean := False;
1800 -- This flag is used in order to indicate if an error has been detected
1801 -- so far by the compiler in this routine.
1803 begin
1804 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1805 -- dimensions for aggregates that don't come from source.
1807 if Ada_Version < Ada_2012 or else not Comes_From_Source (N) then
1808 return;
1809 end if;
1811 Comp := First (Component_Associations (N));
1812 while Present (Comp) loop
1813 Comp_Id := Entity (First (Choices (Comp)));
1814 Comp_Typ := Etype (Comp_Id);
1816 -- Check the component type is either a dimensioned type or a
1817 -- dimensioned subtype.
1819 if Has_Dimension_System (Base_Type (Comp_Typ)) then
1820 Expr := Expression (Comp);
1822 -- Issue an error if the dimensions of the component type and the
1823 -- dimensions of the component mismatch.
1825 if Dimensions_Of (Expr) /= Dimensions_Of (Comp_Typ) then
1827 -- Check if an error has already been encountered so far
1829 if not Error_Detected then
1831 -- Extension aggregate case
1833 if Nkind (N) = N_Extension_Aggregate then
1834 Error_Msg_N
1835 ("dimensions mismatch in extension aggregate", N);
1837 -- Record aggregate case
1839 else
1840 Error_Msg_N
1841 ("dimensions mismatch in record aggregate", N);
1842 end if;
1844 Error_Detected := True;
1845 end if;
1847 Error_Msg_N
1848 ("\expected dimension "
1849 & Dimensions_Msg_Of (Comp_Typ)
1850 & ", found "
1851 & Dimensions_Msg_Of (Expr),
1852 Comp);
1853 end if;
1854 end if;
1856 Next (Comp);
1857 end loop;
1858 end Analyze_Dimension_Extension_Or_Record_Aggregate;
1860 -------------------------------
1861 -- Analyze_Dimension_Formals --
1862 -------------------------------
1864 procedure Analyze_Dimension_Formals (N : Node_Id; Formals : List_Id) is
1865 Dims_Of_Typ : Dimension_Type;
1866 Formal : Node_Id;
1867 Typ : Entity_Id;
1869 begin
1870 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1871 -- dimensions for sub specs that don't come from source.
1873 if Ada_Version < Ada_2012 or else not Comes_From_Source (N) then
1874 return;
1875 end if;
1877 Formal := First (Formals);
1878 while Present (Formal) loop
1879 Typ := Parameter_Type (Formal);
1880 Dims_Of_Typ := Dimensions_Of (Typ);
1882 if Exists (Dims_Of_Typ) then
1883 declare
1884 Expr : constant Node_Id := Expression (Formal);
1886 begin
1887 -- Issue a warning if Expr is a numeric literal and if its
1888 -- dimensions differ with the dimensions of the formal type.
1890 if Present (Expr)
1891 and then Dims_Of_Typ /= Dimensions_Of (Expr)
1892 and then Nkind_In (Original_Node (Expr), N_Real_Literal,
1893 N_Integer_Literal)
1894 then
1895 Dim_Warning_For_Numeric_Literal (Expr, Etype (Typ));
1896 end if;
1897 end;
1898 end if;
1900 Next (Formal);
1901 end loop;
1902 end Analyze_Dimension_Formals;
1904 ---------------------------------
1905 -- Analyze_Dimension_Has_Etype --
1906 ---------------------------------
1908 procedure Analyze_Dimension_Has_Etype (N : Node_Id) is
1909 Etyp : constant Entity_Id := Etype (N);
1910 Dims_Of_Etyp : Dimension_Type := Dimensions_Of (Etyp);
1912 begin
1913 -- General case. Propagation of the dimensions from the type
1915 if Exists (Dims_Of_Etyp) then
1916 Set_Dimensions (N, Dims_Of_Etyp);
1918 -- Identifier case. Propagate the dimensions from the entity for
1919 -- identifier whose entity is a non-dimensionless constant.
1921 elsif Nkind (N) = N_Identifier then
1922 Analyze_Dimension_Identifier : declare
1923 Id : constant Entity_Id := Entity (N);
1924 begin
1925 if Ekind (Id) = E_Constant
1926 and then Exists (Dimensions_Of (Id))
1927 then
1928 Set_Dimensions (N, Dimensions_Of (Id));
1929 end if;
1930 end Analyze_Dimension_Identifier;
1932 -- Attribute reference case. Propagate the dimensions from the prefix.
1934 elsif Nkind (N) = N_Attribute_Reference
1935 and then Has_Dimension_System (Base_Type (Etyp))
1936 then
1937 Dims_Of_Etyp := Dimensions_Of (Prefix (N));
1939 -- Check the prefix is not dimensionless
1941 if Exists (Dims_Of_Etyp) then
1942 Set_Dimensions (N, Dims_Of_Etyp);
1943 end if;
1944 end if;
1946 -- Removal of dimensions in expression
1948 case Nkind (N) is
1949 when N_Attribute_Reference |
1950 N_Indexed_Component =>
1951 declare
1952 Expr : Node_Id;
1953 Exprs : constant List_Id := Expressions (N);
1955 begin
1956 if Present (Exprs) then
1957 Expr := First (Exprs);
1958 while Present (Expr) loop
1959 Remove_Dimensions (Expr);
1960 Next (Expr);
1961 end loop;
1962 end if;
1963 end;
1965 when N_Qualified_Expression |
1966 N_Type_Conversion |
1967 N_Unchecked_Type_Conversion =>
1968 Remove_Dimensions (Expression (N));
1970 when N_Selected_Component =>
1971 Remove_Dimensions (Selector_Name (N));
1973 when others => null;
1974 end case;
1975 end Analyze_Dimension_Has_Etype;
1977 ------------------------------------------
1978 -- Analyze_Dimension_Object_Declaration --
1979 ------------------------------------------
1981 procedure Analyze_Dimension_Object_Declaration (N : Node_Id) is
1982 Expr : constant Node_Id := Expression (N);
1983 Id : constant Entity_Id := Defining_Identifier (N);
1984 Etyp : constant Entity_Id := Etype (Id);
1985 Dim_Of_Etyp : constant Dimension_Type := Dimensions_Of (Etyp);
1986 Dim_Of_Expr : Dimension_Type;
1988 procedure Error_Dim_Msg_For_Object_Declaration
1989 (N : Node_Id;
1990 Etyp : Entity_Id;
1991 Expr : Node_Id);
1992 -- Error using Error_Msg_N at node N. Output the dimensions of the
1993 -- type Etyp and of the expression Expr.
1995 ------------------------------------------
1996 -- Error_Dim_Msg_For_Object_Declaration --
1997 ------------------------------------------
1999 procedure Error_Dim_Msg_For_Object_Declaration
2000 (N : Node_Id;
2001 Etyp : Entity_Id;
2002 Expr : Node_Id) is
2003 begin
2004 Error_Msg_N ("dimensions mismatch in object declaration", N);
2005 Error_Msg_N
2006 ("\expected dimension "
2007 & Dimensions_Msg_Of (Etyp)
2008 & ", found "
2009 & Dimensions_Msg_Of (Expr),
2010 Expr);
2011 end Error_Dim_Msg_For_Object_Declaration;
2013 -- Start of processing for Analyze_Dimension_Object_Declaration
2015 begin
2016 -- Expression is present
2018 if Present (Expr) then
2019 Dim_Of_Expr := Dimensions_Of (Expr);
2021 -- Check dimensions match
2023 if Dim_Of_Expr /= Dim_Of_Etyp then
2025 -- Numeric literal case. Issue a warning if the object type is not
2026 -- dimensionless to indicate the literal is treated as if its
2027 -- dimension matches the type dimension.
2029 if Nkind_In (Original_Node (Expr), N_Real_Literal,
2030 N_Integer_Literal)
2031 then
2032 Dim_Warning_For_Numeric_Literal (Expr, Etyp);
2034 -- Case of object is a constant whose type is a dimensioned type
2036 elsif Constant_Present (N) and then not Exists (Dim_Of_Etyp) then
2038 -- Propagate dimension from expression to object entity
2040 Set_Dimensions (Id, Dim_Of_Expr);
2042 -- For all other cases, issue an error message
2044 else
2045 Error_Dim_Msg_For_Object_Declaration (N, Etyp, Expr);
2046 end if;
2047 end if;
2049 -- Removal of dimensions in expression
2051 Remove_Dimensions (Expr);
2052 end if;
2053 end Analyze_Dimension_Object_Declaration;
2055 ---------------------------------------------------
2056 -- Analyze_Dimension_Object_Renaming_Declaration --
2057 ---------------------------------------------------
2059 procedure Analyze_Dimension_Object_Renaming_Declaration (N : Node_Id) is
2060 Renamed_Name : constant Node_Id := Name (N);
2061 Sub_Mark : constant Node_Id := Subtype_Mark (N);
2063 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2064 (N : Node_Id;
2065 Sub_Mark : Node_Id;
2066 Renamed_Name : Node_Id);
2067 -- Error using Error_Msg_N at node N. Output the dimensions of
2068 -- Sub_Mark and of Renamed_Name.
2070 ---------------------------------------------------
2071 -- Error_Dim_Msg_For_Object_Renaming_Declaration --
2072 ---------------------------------------------------
2074 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2075 (N : Node_Id;
2076 Sub_Mark : Node_Id;
2077 Renamed_Name : Node_Id) is
2078 begin
2079 Error_Msg_N ("dimensions mismatch in object renaming declaration", N);
2080 Error_Msg_N
2081 ("\expected dimension "
2082 & Dimensions_Msg_Of (Sub_Mark)
2083 & ", found "
2084 & Dimensions_Msg_Of (Renamed_Name),
2085 Renamed_Name);
2086 end Error_Dim_Msg_For_Object_Renaming_Declaration;
2088 -- Start of processing for Analyze_Dimension_Object_Renaming_Declaration
2090 begin
2091 if Dimensions_Of (Renamed_Name) /= Dimensions_Of (Sub_Mark) then
2092 Error_Dim_Msg_For_Object_Renaming_Declaration
2093 (N, Sub_Mark, Renamed_Name);
2094 end if;
2095 end Analyze_Dimension_Object_Renaming_Declaration;
2097 -----------------------------------------------
2098 -- Analyze_Dimension_Simple_Return_Statement --
2099 -----------------------------------------------
2101 procedure Analyze_Dimension_Simple_Return_Statement (N : Node_Id) is
2102 Expr : constant Node_Id := Expression (N);
2103 Dims_Of_Expr : constant Dimension_Type := Dimensions_Of (Expr);
2104 Return_Ent : constant Entity_Id := Return_Statement_Entity (N);
2105 Return_Etyp : constant Entity_Id :=
2106 Etype (Return_Applies_To (Return_Ent));
2107 Dims_Of_Return_Etyp : constant Dimension_Type :=
2108 Dimensions_Of (Return_Etyp);
2110 procedure Error_Dim_Msg_For_Simple_Return_Statement
2111 (N : Node_Id;
2112 Return_Etyp : Entity_Id;
2113 Expr : Node_Id);
2114 -- Error using Error_Msg_N at node N. Output the dimensions of the
2115 -- returned type Return_Etyp and the returned expression Expr of N.
2117 -----------------------------------------------
2118 -- Error_Dim_Msg_For_Simple_Return_Statement --
2119 -----------------------------------------------
2121 procedure Error_Dim_Msg_For_Simple_Return_Statement
2122 (N : Node_Id;
2123 Return_Etyp : Entity_Id;
2124 Expr : Node_Id)
2126 begin
2127 Error_Msg_N ("dimensions mismatch in return statement", N);
2128 Error_Msg_N
2129 ("\expected dimension "
2130 & Dimensions_Msg_Of (Return_Etyp)
2131 & ", found "
2132 & Dimensions_Msg_Of (Expr),
2133 Expr);
2134 end Error_Dim_Msg_For_Simple_Return_Statement;
2136 -- Start of processing for Analyze_Dimension_Simple_Return_Statement
2138 begin
2139 if Dims_Of_Return_Etyp /= Dims_Of_Expr then
2140 Error_Dim_Msg_For_Simple_Return_Statement (N, Return_Etyp, Expr);
2141 Remove_Dimensions (Expr);
2142 end if;
2143 end Analyze_Dimension_Simple_Return_Statement;
2145 -------------------------------------------
2146 -- Analyze_Dimension_Subtype_Declaration --
2147 -------------------------------------------
2149 procedure Analyze_Dimension_Subtype_Declaration (N : Node_Id) is
2150 Id : constant Entity_Id := Defining_Identifier (N);
2151 Dims_Of_Id : constant Dimension_Type := Dimensions_Of (Id);
2152 Dims_Of_Etyp : Dimension_Type;
2153 Etyp : Node_Id;
2155 begin
2156 -- No constraint case in subtype declaration
2158 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
2159 Etyp := Etype (Subtype_Indication (N));
2160 Dims_Of_Etyp := Dimensions_Of (Etyp);
2162 if Exists (Dims_Of_Etyp) then
2164 -- If subtype already has a dimension (from Aspect_Dimension),
2165 -- it cannot inherit a dimension from its subtype.
2167 if Exists (Dims_Of_Id) then
2168 Error_Msg_N
2169 ("subtype& already" & Dimensions_Msg_Of (Id, True), N);
2171 else
2172 Set_Dimensions (Id, Dims_Of_Etyp);
2173 Set_Symbol (Id, Symbol_Of (Etyp));
2174 end if;
2175 end if;
2177 -- Constraint present in subtype declaration
2179 else
2180 Etyp := Etype (Subtype_Mark (Subtype_Indication (N)));
2181 Dims_Of_Etyp := Dimensions_Of (Etyp);
2183 if Exists (Dims_Of_Etyp) then
2184 Set_Dimensions (Id, Dims_Of_Etyp);
2185 Set_Symbol (Id, Symbol_Of (Etyp));
2186 end if;
2187 end if;
2188 end Analyze_Dimension_Subtype_Declaration;
2190 --------------------------------
2191 -- Analyze_Dimension_Unary_Op --
2192 --------------------------------
2194 procedure Analyze_Dimension_Unary_Op (N : Node_Id) is
2195 begin
2196 case Nkind (N) is
2197 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
2198 declare
2199 R : constant Node_Id := Right_Opnd (N);
2201 begin
2202 -- Propagate the dimension if the operand is not dimensionless
2204 Move_Dimensions (R, N);
2205 end;
2207 when others => null;
2209 end case;
2210 end Analyze_Dimension_Unary_Op;
2212 ---------------------
2213 -- Copy_Dimensions --
2214 ---------------------
2216 procedure Copy_Dimensions (From, To : Node_Id) is
2217 Dims_Of_From : constant Dimension_Type := Dimensions_Of (From);
2219 begin
2220 -- Ignore if not Ada 2012 or beyond
2222 if Ada_Version < Ada_2012 then
2223 return;
2225 -- For Ada 2012, Copy the dimension of 'From to 'To'
2227 elsif Exists (Dims_Of_From) then
2228 Set_Dimensions (To, Dims_Of_From);
2229 end if;
2230 end Copy_Dimensions;
2232 --------------------------
2233 -- Create_Rational_From --
2234 --------------------------
2236 -- RATIONAL ::= [-] NUMERAL [/ NUMERAL]
2238 -- A rational number is a number that can be expressed as the quotient or
2239 -- fraction a/b of two integers, where b is non-zero positive.
2241 function Create_Rational_From
2242 (Expr : Node_Id;
2243 Complain : Boolean) return Rational
2245 Or_Node_Of_Expr : constant Node_Id := Original_Node (Expr);
2246 Result : Rational := No_Rational;
2248 function Process_Minus (N : Node_Id) return Rational;
2249 -- Create a rational from a N_Op_Minus node
2251 function Process_Divide (N : Node_Id) return Rational;
2252 -- Create a rational from a N_Op_Divide node
2254 function Process_Literal (N : Node_Id) return Rational;
2255 -- Create a rational from a N_Integer_Literal node
2257 -------------------
2258 -- Process_Minus --
2259 -------------------
2261 function Process_Minus (N : Node_Id) return Rational is
2262 Right : constant Node_Id := Original_Node (Right_Opnd (N));
2263 Result : Rational;
2265 begin
2266 -- Operand is an integer literal
2268 if Nkind (Right) = N_Integer_Literal then
2269 Result := -Process_Literal (Right);
2271 -- Operand is a divide operator
2273 elsif Nkind (Right) = N_Op_Divide then
2274 Result := -Process_Divide (Right);
2276 else
2277 Result := No_Rational;
2278 end if;
2280 -- Provide minimal semantic information on dimension expressions,
2281 -- even though they have no run-time existence. This is for use by
2282 -- ASIS tools, in particular pretty-printing.
2284 Set_Entity (N, Standard_Op_Minus);
2285 Set_Etype (N, Standard_Integer);
2286 return Result;
2287 end Process_Minus;
2289 --------------------
2290 -- Process_Divide --
2291 --------------------
2293 function Process_Divide (N : Node_Id) return Rational is
2294 Left : constant Node_Id := Original_Node (Left_Opnd (N));
2295 Right : constant Node_Id := Original_Node (Right_Opnd (N));
2296 Left_Rat : Rational;
2297 Result : Rational := No_Rational;
2298 Right_Rat : Rational;
2300 begin
2301 -- Both left and right operands are an integer literal
2303 if Nkind (Left) = N_Integer_Literal
2304 and then Nkind (Right) = N_Integer_Literal
2305 then
2306 Left_Rat := Process_Literal (Left);
2307 Right_Rat := Process_Literal (Right);
2308 Result := Left_Rat / Right_Rat;
2309 end if;
2311 -- Provide minimal semantic information on dimension expressions,
2312 -- even though they have no run-time existence. This is for use by
2313 -- ASIS tools, in particular pretty-printing.
2315 Set_Entity (N, Standard_Op_Divide);
2316 Set_Etype (N, Standard_Integer);
2317 return Result;
2318 end Process_Divide;
2320 ---------------------
2321 -- Process_Literal --
2322 ---------------------
2324 function Process_Literal (N : Node_Id) return Rational is
2325 begin
2326 return +Whole (UI_To_Int (Intval (N)));
2327 end Process_Literal;
2329 -- Start of processing for Create_Rational_From
2331 begin
2332 -- Check the expression is either a division of two integers or an
2333 -- integer itself. Note that the check applies to the original node
2334 -- since the node could have already been rewritten.
2336 -- Integer literal case
2338 if Nkind (Or_Node_Of_Expr) = N_Integer_Literal then
2339 Result := Process_Literal (Or_Node_Of_Expr);
2341 -- Divide operator case
2343 elsif Nkind (Or_Node_Of_Expr) = N_Op_Divide then
2344 Result := Process_Divide (Or_Node_Of_Expr);
2346 -- Minus operator case
2348 elsif Nkind (Or_Node_Of_Expr) = N_Op_Minus then
2349 Result := Process_Minus (Or_Node_Of_Expr);
2350 end if;
2352 -- When Expr cannot be interpreted as a rational and Complain is true,
2353 -- generate an error message.
2355 if Complain and then Result = No_Rational then
2356 Error_Msg_N ("rational expected", Expr);
2357 end if;
2359 return Result;
2360 end Create_Rational_From;
2362 -------------------
2363 -- Dimensions_Of --
2364 -------------------
2366 function Dimensions_Of (N : Node_Id) return Dimension_Type is
2367 begin
2368 return Dimension_Table.Get (N);
2369 end Dimensions_Of;
2371 -----------------------
2372 -- Dimensions_Msg_Of --
2373 -----------------------
2375 function Dimensions_Msg_Of
2376 (N : Node_Id;
2377 Description_Needed : Boolean := False) return String
2379 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
2380 Dimensions_Msg : Name_Id;
2381 System : System_Type;
2383 begin
2384 -- Initialization of Name_Buffer
2386 Name_Len := 0;
2388 -- N is not dimensionless
2390 if Exists (Dims_Of_N) then
2391 System := System_Of (Base_Type (Etype (N)));
2393 -- When Description_Needed, add to string "has dimension " before the
2394 -- actual dimension.
2396 if Description_Needed then
2397 Add_Str_To_Name_Buffer ("has dimension ");
2398 end if;
2400 Add_String_To_Name_Buffer
2401 (From_Dim_To_Str_Of_Dim_Symbols (Dims_Of_N, System, True));
2403 -- N is dimensionless
2405 -- When Description_Needed, return "is dimensionless"
2407 elsif Description_Needed then
2408 Add_Str_To_Name_Buffer ("is dimensionless");
2410 -- Otherwise, return "[]"
2412 else
2413 Add_Str_To_Name_Buffer ("[]");
2414 end if;
2416 Dimensions_Msg := Name_Find;
2417 return Get_Name_String (Dimensions_Msg);
2418 end Dimensions_Msg_Of;
2420 --------------------------
2421 -- Dimension_Table_Hash --
2422 --------------------------
2424 function Dimension_Table_Hash
2425 (Key : Node_Id) return Dimension_Table_Range
2427 begin
2428 return Dimension_Table_Range (Key mod 511);
2429 end Dimension_Table_Hash;
2431 -------------------------------------
2432 -- Dim_Warning_For_Numeric_Literal --
2433 -------------------------------------
2435 procedure Dim_Warning_For_Numeric_Literal (N : Node_Id; Typ : Entity_Id) is
2436 begin
2437 -- Initialize name buffer
2439 Name_Len := 0;
2441 Add_String_To_Name_Buffer (String_From_Numeric_Literal (N));
2443 -- Insert a blank between the literal and the symbol
2444 Add_Str_To_Name_Buffer (" ");
2446 Add_String_To_Name_Buffer (Symbol_Of (Typ));
2448 Error_Msg_Name_1 := Name_Find;
2449 Error_Msg_N ("??assumed to be%%", N);
2450 end Dim_Warning_For_Numeric_Literal;
2452 ----------------------------------------
2453 -- Eval_Op_Expon_For_Dimensioned_Type --
2454 ----------------------------------------
2456 -- Evaluate the expon operator for real dimensioned type.
2458 -- Note that if the exponent is an integer (denominator = 1) the node is
2459 -- evaluated by the regular Eval_Op_Expon routine (see Sem_Eval).
2461 procedure Eval_Op_Expon_For_Dimensioned_Type
2462 (N : Node_Id;
2463 Btyp : Entity_Id)
2465 R : constant Node_Id := Right_Opnd (N);
2466 R_Value : Rational := No_Rational;
2468 begin
2469 if Is_Real_Type (Btyp) then
2470 R_Value := Create_Rational_From (R, False);
2471 end if;
2473 -- Check that the exponent is not an integer
2475 if R_Value /= No_Rational and then R_Value.Denominator /= 1 then
2476 Eval_Op_Expon_With_Rational_Exponent (N, R_Value);
2477 else
2478 Eval_Op_Expon (N);
2479 end if;
2480 end Eval_Op_Expon_For_Dimensioned_Type;
2482 ------------------------------------------
2483 -- Eval_Op_Expon_With_Rational_Exponent --
2484 ------------------------------------------
2486 -- For dimensioned operand in exponentiation, exponent is allowed to be a
2487 -- Rational and not only an Integer like for dimensionless operands. For
2488 -- that particular case, the left operand is rewritten as a function call
2489 -- using the function Expon_LLF from s-llflex.ads.
2491 procedure Eval_Op_Expon_With_Rational_Exponent
2492 (N : Node_Id;
2493 Exponent_Value : Rational)
2495 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
2496 L : constant Node_Id := Left_Opnd (N);
2497 Etyp_Of_L : constant Entity_Id := Etype (L);
2498 Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2499 Loc : constant Source_Ptr := Sloc (N);
2500 Actual_1 : Node_Id;
2501 Actual_2 : Node_Id;
2502 Dim_Power : Rational;
2503 List_Of_Dims : List_Id;
2504 New_Aspect : Node_Id;
2505 New_Aspects : List_Id;
2506 New_Id : Entity_Id;
2507 New_N : Node_Id;
2508 New_Subtyp_Decl_For_L : Node_Id;
2509 System : System_Type;
2511 begin
2512 -- Case when the operand is not dimensionless
2514 if Exists (Dims_Of_N) then
2516 -- Get the corresponding System_Type to know the exact number of
2517 -- dimensions in the system.
2519 System := System_Of (Btyp_Of_L);
2521 -- Generation of a new subtype with the proper dimensions
2523 -- In order to rewrite the operator as a type conversion, a new
2524 -- dimensioned subtype with the resulting dimensions of the
2525 -- exponentiation must be created.
2527 -- Generate:
2529 -- Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2530 -- System : constant System_Id :=
2531 -- Get_Dimension_System_Id (Btyp_Of_L);
2532 -- Num_Of_Dims : constant Number_Of_Dimensions :=
2533 -- Dimension_Systems.Table (System).Dimension_Count;
2535 -- subtype T is Btyp_Of_L
2536 -- with
2537 -- Dimension => (
2538 -- Dims_Of_N (1).Numerator / Dims_Of_N (1).Denominator,
2539 -- Dims_Of_N (2).Numerator / Dims_Of_N (2).Denominator,
2540 -- ...
2541 -- Dims_Of_N (Num_Of_Dims).Numerator /
2542 -- Dims_Of_N (Num_Of_Dims).Denominator);
2544 -- Step 1: Generate the new aggregate for the aspect Dimension
2546 New_Aspects := Empty_List;
2547 List_Of_Dims := New_List;
2549 for Position in Dims_Of_N'First .. System.Count loop
2550 Dim_Power := Dims_Of_N (Position);
2551 Append_To (List_Of_Dims,
2552 Make_Op_Divide (Loc,
2553 Left_Opnd =>
2554 Make_Integer_Literal (Loc,
2555 Int (Dim_Power.Numerator)),
2556 Right_Opnd =>
2557 Make_Integer_Literal (Loc,
2558 Int (Dim_Power.Denominator))));
2559 end loop;
2561 -- Step 2: Create the new Aspect Specification for Aspect Dimension
2563 New_Aspect :=
2564 Make_Aspect_Specification (Loc,
2565 Identifier => Make_Identifier (Loc, Name_Dimension),
2566 Expression => Make_Aggregate (Loc, Expressions => List_Of_Dims));
2568 -- Step 3: Make a temporary identifier for the new subtype
2570 New_Id := Make_Temporary (Loc, 'T');
2571 Set_Is_Internal (New_Id);
2573 -- Step 4: Declaration of the new subtype
2575 New_Subtyp_Decl_For_L :=
2576 Make_Subtype_Declaration (Loc,
2577 Defining_Identifier => New_Id,
2578 Subtype_Indication => New_Occurrence_Of (Btyp_Of_L, Loc));
2580 Append (New_Aspect, New_Aspects);
2581 Set_Parent (New_Aspects, New_Subtyp_Decl_For_L);
2582 Set_Aspect_Specifications (New_Subtyp_Decl_For_L, New_Aspects);
2584 Analyze (New_Subtyp_Decl_For_L);
2586 -- Case where the operand is dimensionless
2588 else
2589 New_Id := Btyp_Of_L;
2590 end if;
2592 -- Replacement of N by New_N
2594 -- Generate:
2596 -- Actual_1 := Long_Long_Float (L),
2598 -- Actual_2 := Long_Long_Float (Exponent_Value.Numerator) /
2599 -- Long_Long_Float (Exponent_Value.Denominator);
2601 -- (T (Expon_LLF (Actual_1, Actual_2)));
2603 -- where T is the subtype declared in step 1
2605 -- The node is rewritten as a type conversion
2607 -- Step 1: Creation of the two parameters of Expon_LLF function call
2609 Actual_1 :=
2610 Make_Type_Conversion (Loc,
2611 Subtype_Mark => New_Reference_To (Standard_Long_Long_Float, Loc),
2612 Expression => Relocate_Node (L));
2614 Actual_2 :=
2615 Make_Op_Divide (Loc,
2616 Left_Opnd =>
2617 Make_Real_Literal (Loc,
2618 UR_From_Uint (UI_From_Int (Int (Exponent_Value.Numerator)))),
2619 Right_Opnd =>
2620 Make_Real_Literal (Loc,
2621 UR_From_Uint (UI_From_Int (Int (Exponent_Value.Denominator)))));
2623 -- Step 2: Creation of New_N
2625 New_N :=
2626 Make_Type_Conversion (Loc,
2627 Subtype_Mark => New_Reference_To (New_Id, Loc),
2628 Expression =>
2629 Make_Function_Call (Loc,
2630 Name => New_Reference_To (RTE (RE_Expon_LLF), Loc),
2631 Parameter_Associations => New_List (
2632 Actual_1, Actual_2)));
2634 -- Step 3: Rewrite N with the result
2636 Rewrite (N, New_N);
2637 Set_Etype (N, New_Id);
2638 Analyze_And_Resolve (N, New_Id);
2639 end Eval_Op_Expon_With_Rational_Exponent;
2641 ------------
2642 -- Exists --
2643 ------------
2645 function Exists (Dim : Dimension_Type) return Boolean is
2646 begin
2647 return Dim /= Null_Dimension;
2648 end Exists;
2650 function Exists (Str : String_Id) return Boolean is
2651 begin
2652 return Str /= No_String;
2653 end Exists;
2655 function Exists (Sys : System_Type) return Boolean is
2656 begin
2657 return Sys /= Null_System;
2658 end Exists;
2660 ---------------------------------
2661 -- Expand_Put_Call_With_Symbol --
2662 ---------------------------------
2664 -- For procedure Put (resp. Put_Dim_Of) defined in System.Dim.Float_IO
2665 -- (System.Dim.Integer_IO), the default string parameter must be rewritten
2666 -- to include the unit symbols (resp. dimension symbols) in the output
2667 -- of a dimensioned object. Note that if a value is already supplied for
2668 -- parameter Symbol, this routine doesn't do anything.
2670 -- Case 1. Item is dimensionless
2672 -- * Put : Item appears without a suffix
2674 -- * Put_Dim_Of : the output is []
2676 -- Obj : Mks_Type := 2.6;
2677 -- Put (Obj, 1, 1, 0);
2678 -- Put_Dim_Of (Obj);
2680 -- The corresponding outputs are:
2681 -- $2.6
2682 -- $[]
2684 -- Case 2. Item has a dimension
2686 -- * Put : If the type of Item is a dimensioned subtype whose
2687 -- symbol is not empty, then the symbol appears as a
2688 -- suffix. Otherwise, a new string is created and appears
2689 -- as a suffix of Item. This string results in the
2690 -- successive concatanations between each unit symbol
2691 -- raised by its corresponding dimension power from the
2692 -- dimensions of Item.
2694 -- * Put_Dim_Of : The output is a new string resulting in the successive
2695 -- concatanations between each dimension symbol raised by
2696 -- its corresponding dimension power from the dimensions of
2697 -- Item.
2699 -- subtype Random is Mks_Type
2700 -- with
2701 -- Dimension => (
2702 -- Meter => 3,
2703 -- Candela => -1,
2704 -- others => 0);
2706 -- Obj : Random := 5.0;
2707 -- Put (Obj);
2708 -- Put_Dim_Of (Obj);
2710 -- The corresponding outputs are:
2711 -- $5.0 m**3.cd**(-1)
2712 -- $[l**3.J**(-1)]
2714 procedure Expand_Put_Call_With_Symbol (N : Node_Id) is
2715 Actuals : constant List_Id := Parameter_Associations (N);
2716 Loc : constant Source_Ptr := Sloc (N);
2717 Name_Call : constant Node_Id := Name (N);
2718 New_Actuals : constant List_Id := New_List;
2719 Actual : Node_Id;
2720 Dims_Of_Actual : Dimension_Type;
2721 Etyp : Entity_Id;
2722 New_Str_Lit : Node_Id := Empty;
2723 Symbols : String_Id;
2725 Is_Put_Dim_Of : Boolean := False;
2726 -- This flag is used in order to differentiate routines Put and
2727 -- Put_Dim_Of. Set to True if the procedure is one of the Put_Dim_Of
2728 -- defined in System.Dim.Float_IO or System.Dim.Integer_IO.
2730 function Has_Symbols return Boolean;
2731 -- Return True if the current Put call already has a parameter
2732 -- association for parameter "Symbols" with the correct string of
2733 -- symbols.
2735 function Is_Procedure_Put_Call return Boolean;
2736 -- Return True if the current call is a call of an instantiation of a
2737 -- procedure Put defined in the package System.Dim.Float_IO and
2738 -- System.Dim.Integer_IO.
2740 function Item_Actual return Node_Id;
2741 -- Return the item actual parameter node in the output call
2743 -----------------
2744 -- Has_Symbols --
2745 -----------------
2747 function Has_Symbols return Boolean is
2748 Actual : Node_Id;
2749 Actual_Str : Node_Id;
2751 begin
2752 Actual := First (Actuals);
2754 -- Look for a symbols parameter association in the list of actuals
2756 while Present (Actual) loop
2758 -- Positional parameter association case when the actual is a
2759 -- string literal.
2761 if Nkind (Actual) = N_String_Literal then
2762 Actual_Str := Actual;
2764 -- Named parameter association case when selector name is Symbol
2766 elsif Nkind (Actual) = N_Parameter_Association
2767 and then Chars (Selector_Name (Actual)) = Name_Symbol
2768 then
2769 Actual_Str := Explicit_Actual_Parameter (Actual);
2771 -- Ignore all other cases
2773 else
2774 Actual_Str := Empty;
2775 end if;
2777 if Present (Actual_Str) then
2779 -- Return True if the actual comes from source or if the string
2780 -- of symbols doesn't have the default value (i.e. it is "").
2782 if Comes_From_Source (Actual)
2783 or else String_Length (Strval (Actual_Str)) /= 0
2784 then
2785 -- Complain only if the actual comes from source or if it
2786 -- hasn't been fully analyzed yet.
2788 if Comes_From_Source (Actual)
2789 or else not Analyzed (Actual)
2790 then
2791 Error_Msg_N ("Symbol parameter should not be provided",
2792 Actual);
2793 Error_Msg_N ("\reserved for compiler use only", Actual);
2794 end if;
2796 return True;
2798 else
2799 return False;
2800 end if;
2801 end if;
2803 Next (Actual);
2804 end loop;
2806 -- At this point, the call has no parameter association. Look to the
2807 -- last actual since the symbols parameter is the last one.
2809 return Nkind (Last (Actuals)) = N_String_Literal;
2810 end Has_Symbols;
2812 ---------------------------
2813 -- Is_Procedure_Put_Call --
2814 ---------------------------
2816 function Is_Procedure_Put_Call return Boolean is
2817 Ent : Entity_Id;
2818 Loc : Source_Ptr;
2820 begin
2821 -- There are three different Put (resp. Put_Dim_Of) routines in each
2822 -- generic dim IO package. Verify the current procedure call is one
2823 -- of them.
2825 if Is_Entity_Name (Name_Call) then
2826 Ent := Entity (Name_Call);
2828 -- Get the original subprogram entity following the renaming chain
2830 if Present (Alias (Ent)) then
2831 Ent := Alias (Ent);
2832 end if;
2834 Loc := Sloc (Ent);
2836 -- Check the name of the entity subprogram is Put (resp.
2837 -- Put_Dim_Of) and verify this entity is located in either
2838 -- System.Dim.Float_IO or System.Dim.Integer_IO.
2840 if Loc > No_Location
2841 and then Is_Dim_IO_Package_Entity
2842 (Cunit_Entity (Get_Source_Unit (Loc)))
2843 then
2844 if Chars (Ent) = Name_Put_Dim_Of then
2845 Is_Put_Dim_Of := True;
2846 return True;
2848 elsif Chars (Ent) = Name_Put then
2849 return True;
2850 end if;
2851 end if;
2852 end if;
2854 return False;
2855 end Is_Procedure_Put_Call;
2857 -----------------
2858 -- Item_Actual --
2859 -----------------
2861 function Item_Actual return Node_Id is
2862 Actual : Node_Id;
2864 begin
2865 -- Look for the item actual as a parameter association
2867 Actual := First (Actuals);
2868 while Present (Actual) loop
2869 if Nkind (Actual) = N_Parameter_Association
2870 and then Chars (Selector_Name (Actual)) = Name_Item
2871 then
2872 return Explicit_Actual_Parameter (Actual);
2873 end if;
2875 Next (Actual);
2876 end loop;
2878 -- Case where the item has been defined without an association
2880 Actual := First (Actuals);
2882 -- Depending on the procedure Put, Item actual could be first or
2883 -- second in the list of actuals.
2885 if Has_Dimension_System (Base_Type (Etype (Actual))) then
2886 return Actual;
2887 else
2888 return Next (Actual);
2889 end if;
2890 end Item_Actual;
2892 -- Start of processing for Expand_Put_Call_With_Symbol
2894 begin
2895 if Is_Procedure_Put_Call and then not Has_Symbols then
2896 Actual := Item_Actual;
2897 Dims_Of_Actual := Dimensions_Of (Actual);
2898 Etyp := Etype (Actual);
2900 -- Put_Dim_Of case
2902 if Is_Put_Dim_Of then
2904 -- Check that the item is not dimensionless
2906 -- Create the new String_Literal with the new String_Id generated
2907 -- by the routine From_Dim_To_Str_Of_Dim_Symbols.
2909 if Exists (Dims_Of_Actual) then
2910 New_Str_Lit :=
2911 Make_String_Literal (Loc,
2912 From_Dim_To_Str_Of_Dim_Symbols
2913 (Dims_Of_Actual, System_Of (Base_Type (Etyp))));
2915 -- If dimensionless, the output is []
2917 else
2918 New_Str_Lit :=
2919 Make_String_Literal (Loc, "[]");
2920 end if;
2922 -- Put case
2924 else
2925 -- Add the symbol as a suffix of the value if the subtype has a
2926 -- unit symbol or if the parameter is not dimensionless.
2928 if Exists (Symbol_Of (Etyp)) then
2929 Symbols := Symbol_Of (Etyp);
2930 else
2931 Symbols := From_Dim_To_Str_Of_Unit_Symbols
2932 (Dims_Of_Actual, System_Of (Base_Type (Etyp)));
2933 end if;
2935 -- Check Symbols exists
2937 if Exists (Symbols) then
2938 Start_String;
2940 -- Put a space between the value and the dimension
2942 Store_String_Char (' ');
2943 Store_String_Chars (Symbols);
2944 New_Str_Lit := Make_String_Literal (Loc, End_String);
2945 end if;
2946 end if;
2948 if Present (New_Str_Lit) then
2950 -- Insert all actuals in New_Actuals
2952 Actual := First (Actuals);
2953 while Present (Actual) loop
2955 -- Copy every actuals in New_Actuals except the Symbols
2956 -- parameter association.
2958 if Nkind (Actual) = N_Parameter_Association
2959 and then Chars (Selector_Name (Actual)) /= Name_Symbol
2960 then
2961 Append_To (New_Actuals,
2962 Make_Parameter_Association (Loc,
2963 Selector_Name => New_Copy (Selector_Name (Actual)),
2964 Explicit_Actual_Parameter =>
2965 New_Copy (Explicit_Actual_Parameter (Actual))));
2967 elsif Nkind (Actual) /= N_Parameter_Association then
2968 Append_To (New_Actuals, New_Copy (Actual));
2969 end if;
2971 Next (Actual);
2972 end loop;
2974 -- Create new Symbols param association and append to New_Actuals
2976 Append_To (New_Actuals,
2977 Make_Parameter_Association (Loc,
2978 Selector_Name => Make_Identifier (Loc, Name_Symbol),
2979 Explicit_Actual_Parameter => New_Str_Lit));
2981 -- Rewrite and analyze the procedure call
2983 Rewrite (N,
2984 Make_Procedure_Call_Statement (Loc,
2985 Name => New_Copy (Name_Call),
2986 Parameter_Associations => New_Actuals));
2988 Analyze (N);
2989 end if;
2990 end if;
2991 end Expand_Put_Call_With_Symbol;
2993 ------------------------------------
2994 -- From_Dim_To_Str_Of_Dim_Symbols --
2995 ------------------------------------
2997 -- Given a dimension vector and the corresponding dimension system, create
2998 -- a String_Id to output dimension symbols corresponding to the dimensions
2999 -- Dims. If In_Error_Msg is True, there is a special handling for character
3000 -- asterisk * which is an insertion character in error messages.
3002 function From_Dim_To_Str_Of_Dim_Symbols
3003 (Dims : Dimension_Type;
3004 System : System_Type;
3005 In_Error_Msg : Boolean := False) return String_Id
3007 Dim_Power : Rational;
3008 First_Dim : Boolean := True;
3010 procedure Store_String_Oexpon;
3011 -- Store the expon operator symbol "**" in the string. In error
3012 -- messages, asterisk * is a special character and must be quoted
3013 -- to be placed literally into the message.
3015 -------------------------
3016 -- Store_String_Oexpon --
3017 -------------------------
3019 procedure Store_String_Oexpon is
3020 begin
3021 if In_Error_Msg then
3022 Store_String_Chars ("'*'*");
3023 else
3024 Store_String_Chars ("**");
3025 end if;
3026 end Store_String_Oexpon;
3028 -- Start of processing for From_Dim_To_Str_Of_Dim_Symbols
3030 begin
3031 -- Initialization of the new String_Id
3033 Start_String;
3035 -- Store the dimension symbols inside boxes
3037 Store_String_Char ('[');
3039 for Position in Dimension_Type'Range loop
3040 Dim_Power := Dims (Position);
3041 if Dim_Power /= Zero then
3043 if First_Dim then
3044 First_Dim := False;
3045 else
3046 Store_String_Char ('.');
3047 end if;
3049 Store_String_Chars (System.Dim_Symbols (Position));
3051 -- Positive dimension case
3053 if Dim_Power.Numerator > 0 then
3054 -- Integer case
3056 if Dim_Power.Denominator = 1 then
3057 if Dim_Power.Numerator /= 1 then
3058 Store_String_Oexpon;
3059 Store_String_Int (Int (Dim_Power.Numerator));
3060 end if;
3062 -- Rational case when denominator /= 1
3064 else
3065 Store_String_Oexpon;
3066 Store_String_Char ('(');
3067 Store_String_Int (Int (Dim_Power.Numerator));
3068 Store_String_Char ('/');
3069 Store_String_Int (Int (Dim_Power.Denominator));
3070 Store_String_Char (')');
3071 end if;
3073 -- Negative dimension case
3075 else
3076 Store_String_Oexpon;
3077 Store_String_Char ('(');
3078 Store_String_Char ('-');
3079 Store_String_Int (Int (-Dim_Power.Numerator));
3081 -- Integer case
3083 if Dim_Power.Denominator = 1 then
3084 Store_String_Char (')');
3086 -- Rational case when denominator /= 1
3088 else
3089 Store_String_Char ('/');
3090 Store_String_Int (Int (Dim_Power.Denominator));
3091 Store_String_Char (')');
3092 end if;
3093 end if;
3094 end if;
3095 end loop;
3097 Store_String_Char (']');
3098 return End_String;
3099 end From_Dim_To_Str_Of_Dim_Symbols;
3101 -------------------------------------
3102 -- From_Dim_To_Str_Of_Unit_Symbols --
3103 -------------------------------------
3105 -- Given a dimension vector and the corresponding dimension system,
3106 -- create a String_Id to output the unit symbols corresponding to the
3107 -- dimensions Dims.
3109 function From_Dim_To_Str_Of_Unit_Symbols
3110 (Dims : Dimension_Type;
3111 System : System_Type) return String_Id
3113 Dim_Power : Rational;
3114 First_Dim : Boolean := True;
3116 begin
3117 -- Return No_String if dimensionless
3119 if not Exists (Dims) then
3120 return No_String;
3121 end if;
3123 -- Initialization of the new String_Id
3125 Start_String;
3127 for Position in Dimension_Type'Range loop
3128 Dim_Power := Dims (Position);
3130 if Dim_Power /= Zero then
3132 if First_Dim then
3133 First_Dim := False;
3134 else
3135 Store_String_Char ('.');
3136 end if;
3138 Store_String_Chars (System.Unit_Symbols (Position));
3140 -- Positive dimension case
3142 if Dim_Power.Numerator > 0 then
3144 -- Integer case
3146 if Dim_Power.Denominator = 1 then
3147 if Dim_Power.Numerator /= 1 then
3148 Store_String_Chars ("**");
3149 Store_String_Int (Int (Dim_Power.Numerator));
3150 end if;
3152 -- Rational case when denominator /= 1
3154 else
3155 Store_String_Chars ("**");
3156 Store_String_Char ('(');
3157 Store_String_Int (Int (Dim_Power.Numerator));
3158 Store_String_Char ('/');
3159 Store_String_Int (Int (Dim_Power.Denominator));
3160 Store_String_Char (')');
3161 end if;
3163 -- Negative dimension case
3165 else
3166 Store_String_Chars ("**");
3167 Store_String_Char ('(');
3168 Store_String_Char ('-');
3169 Store_String_Int (Int (-Dim_Power.Numerator));
3171 -- Integer case
3173 if Dim_Power.Denominator = 1 then
3174 Store_String_Char (')');
3176 -- Rational case when denominator /= 1
3178 else
3179 Store_String_Char ('/');
3180 Store_String_Int (Int (Dim_Power.Denominator));
3181 Store_String_Char (')');
3182 end if;
3183 end if;
3184 end if;
3185 end loop;
3187 return End_String;
3188 end From_Dim_To_Str_Of_Unit_Symbols;
3190 ---------
3191 -- GCD --
3192 ---------
3194 function GCD (Left, Right : Whole) return Int is
3195 L : Whole;
3196 R : Whole;
3198 begin
3199 L := Left;
3200 R := Right;
3201 while R /= 0 loop
3202 L := L mod R;
3204 if L = 0 then
3205 return Int (R);
3206 end if;
3208 R := R mod L;
3209 end loop;
3211 return Int (L);
3212 end GCD;
3214 --------------------------
3215 -- Has_Dimension_System --
3216 --------------------------
3218 function Has_Dimension_System (Typ : Entity_Id) return Boolean is
3219 begin
3220 return Exists (System_Of (Typ));
3221 end Has_Dimension_System;
3223 ------------------------------
3224 -- Is_Dim_IO_Package_Entity --
3225 ------------------------------
3227 function Is_Dim_IO_Package_Entity (E : Entity_Id) return Boolean is
3228 begin
3229 -- Check the package entity corresponds to System.Dim.Float_IO or
3230 -- System.Dim.Integer_IO.
3232 return
3233 Is_RTU (E, System_Dim_Float_IO)
3234 or else
3235 Is_RTU (E, System_Dim_Integer_IO);
3236 end Is_Dim_IO_Package_Entity;
3238 -------------------------------------
3239 -- Is_Dim_IO_Package_Instantiation --
3240 -------------------------------------
3242 function Is_Dim_IO_Package_Instantiation (N : Node_Id) return Boolean is
3243 Gen_Id : constant Node_Id := Name (N);
3245 begin
3246 -- Check that the instantiated package is either System.Dim.Float_IO
3247 -- or System.Dim.Integer_IO.
3249 return
3250 Is_Entity_Name (Gen_Id)
3251 and then Is_Dim_IO_Package_Entity (Entity (Gen_Id));
3252 end Is_Dim_IO_Package_Instantiation;
3254 ----------------
3255 -- Is_Invalid --
3256 ----------------
3258 function Is_Invalid (Position : Dimension_Position) return Boolean is
3259 begin
3260 return Position = Invalid_Position;
3261 end Is_Invalid;
3263 ---------------------
3264 -- Move_Dimensions --
3265 ---------------------
3267 procedure Move_Dimensions (From, To : Node_Id) is
3268 begin
3269 if Ada_Version < Ada_2012 then
3270 return;
3271 end if;
3273 -- Copy the dimension of 'From to 'To' and remove dimension of 'From'
3275 Copy_Dimensions (From, To);
3276 Remove_Dimensions (From);
3277 end Move_Dimensions;
3279 ------------
3280 -- Reduce --
3281 ------------
3283 function Reduce (X : Rational) return Rational is
3284 begin
3285 if X.Numerator = 0 then
3286 return Zero;
3287 end if;
3289 declare
3290 G : constant Int := GCD (X.Numerator, X.Denominator);
3291 begin
3292 return Rational'(Numerator => Whole (Int (X.Numerator) / G),
3293 Denominator => Whole (Int (X.Denominator) / G));
3294 end;
3295 end Reduce;
3297 -----------------------
3298 -- Remove_Dimensions --
3299 -----------------------
3301 procedure Remove_Dimensions (N : Node_Id) is
3302 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
3303 begin
3304 if Exists (Dims_Of_N) then
3305 Dimension_Table.Remove (N);
3306 end if;
3307 end Remove_Dimensions;
3309 -----------------------------------
3310 -- Remove_Dimension_In_Statement --
3311 -----------------------------------
3313 -- Removal of dimension in statement as part of the Analyze_Statements
3314 -- routine (see package Sem_Ch5).
3316 procedure Remove_Dimension_In_Statement (Stmt : Node_Id) is
3317 begin
3318 if Ada_Version < Ada_2012 then
3319 return;
3320 end if;
3322 -- Remove dimension in parameter specifications for accept statement
3324 if Nkind (Stmt) = N_Accept_Statement then
3325 declare
3326 Param : Node_Id := First (Parameter_Specifications (Stmt));
3327 begin
3328 while Present (Param) loop
3329 Remove_Dimensions (Param);
3330 Next (Param);
3331 end loop;
3332 end;
3334 -- Remove dimension of name and expression in assignments
3336 elsif Nkind (Stmt) = N_Assignment_Statement then
3337 Remove_Dimensions (Expression (Stmt));
3338 Remove_Dimensions (Name (Stmt));
3339 end if;
3340 end Remove_Dimension_In_Statement;
3342 --------------------
3343 -- Set_Dimensions --
3344 --------------------
3346 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type) is
3347 begin
3348 pragma Assert (OK_For_Dimension (Nkind (N)));
3349 pragma Assert (Exists (Val));
3351 Dimension_Table.Set (N, Val);
3352 end Set_Dimensions;
3354 ----------------
3355 -- Set_Symbol --
3356 ----------------
3358 procedure Set_Symbol (E : Entity_Id; Val : String_Id) is
3359 begin
3360 Symbol_Table.Set (E, Val);
3361 end Set_Symbol;
3363 ---------------------------------
3364 -- String_From_Numeric_Literal --
3365 ---------------------------------
3367 function String_From_Numeric_Literal (N : Node_Id) return String_Id is
3368 Loc : constant Source_Ptr := Sloc (N);
3369 Sbuffer : constant Source_Buffer_Ptr :=
3370 Source_Text (Get_Source_File_Index (Loc));
3371 Src_Ptr : Source_Ptr := Loc;
3372 C : Character := Sbuffer (Src_Ptr);
3373 -- Current source program character
3375 function Belong_To_Numeric_Literal (C : Character) return Boolean;
3376 -- Return True if C belongs to a numeric literal
3378 -------------------------------
3379 -- Belong_To_Numeric_Literal --
3380 -------------------------------
3382 function Belong_To_Numeric_Literal (C : Character) return Boolean is
3383 begin
3384 case C is
3385 when '0' .. '9' |
3386 '_' |
3387 '.' |
3388 'e' |
3389 '#' |
3390 'A' |
3391 'B' |
3392 'C' |
3393 'D' |
3394 'E' |
3395 'F' =>
3396 return True;
3398 -- Make sure '+' or '-' is part of an exponent.
3400 when '+' | '-' =>
3401 declare
3402 Prev_C : constant Character := Sbuffer (Src_Ptr - 1);
3403 begin
3404 return Prev_C = 'e' or else Prev_C = 'E';
3405 end;
3407 -- All other character doesn't belong to a numeric literal
3409 when others =>
3410 return False;
3411 end case;
3412 end Belong_To_Numeric_Literal;
3414 -- Start of processing for String_From_Numeric_Literal
3416 begin
3417 Start_String;
3418 while Belong_To_Numeric_Literal (C) loop
3419 Store_String_Char (C);
3420 Src_Ptr := Src_Ptr + 1;
3421 C := Sbuffer (Src_Ptr);
3422 end loop;
3424 return End_String;
3425 end String_From_Numeric_Literal;
3427 ---------------
3428 -- Symbol_Of --
3429 ---------------
3431 function Symbol_Of (E : Entity_Id) return String_Id is
3432 Subtype_Symbol : constant String_Id := Symbol_Table.Get (E);
3433 begin
3434 if Subtype_Symbol /= No_String then
3435 return Subtype_Symbol;
3436 else
3437 return From_Dim_To_Str_Of_Unit_Symbols
3438 (Dimensions_Of (E), System_Of (Base_Type (E)));
3439 end if;
3440 end Symbol_Of;
3442 -----------------------
3443 -- Symbol_Table_Hash --
3444 -----------------------
3446 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range is
3447 begin
3448 return Symbol_Table_Range (Key mod 511);
3449 end Symbol_Table_Hash;
3451 ---------------
3452 -- System_Of --
3453 ---------------
3455 function System_Of (E : Entity_Id) return System_Type is
3456 Type_Decl : constant Node_Id := Parent (E);
3458 begin
3459 -- Look for Type_Decl in System_Table
3461 for Dim_Sys in 1 .. System_Table.Last loop
3462 if Type_Decl = System_Table.Table (Dim_Sys).Type_Decl then
3463 return System_Table.Table (Dim_Sys);
3464 end if;
3465 end loop;
3467 return Null_System;
3468 end System_Of;
3470 end Sem_Dim;