* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / gcc / ada / sem_dim.adb
blob79c1e15037aa44b013a1ef800d06143e031d5bd4
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 return Result;
2281 end Process_Minus;
2283 --------------------
2284 -- Process_Divide --
2285 --------------------
2287 function Process_Divide (N : Node_Id) return Rational is
2288 Left : constant Node_Id := Original_Node (Left_Opnd (N));
2289 Right : constant Node_Id := Original_Node (Right_Opnd (N));
2290 Left_Rat : Rational;
2291 Result : Rational := No_Rational;
2292 Right_Rat : Rational;
2294 begin
2295 -- Both left and right operands are an integer literal
2297 if Nkind (Left) = N_Integer_Literal
2298 and then Nkind (Right) = N_Integer_Literal
2299 then
2300 Left_Rat := Process_Literal (Left);
2301 Right_Rat := Process_Literal (Right);
2302 Result := Left_Rat / Right_Rat;
2303 end if;
2305 return Result;
2306 end Process_Divide;
2308 ---------------------
2309 -- Process_Literal --
2310 ---------------------
2312 function Process_Literal (N : Node_Id) return Rational is
2313 begin
2314 return +Whole (UI_To_Int (Intval (N)));
2315 end Process_Literal;
2317 -- Start of processing for Create_Rational_From
2319 begin
2320 -- Check the expression is either a division of two integers or an
2321 -- integer itself. Note that the check applies to the original node
2322 -- since the node could have already been rewritten.
2324 -- Integer literal case
2326 if Nkind (Or_Node_Of_Expr) = N_Integer_Literal then
2327 Result := Process_Literal (Or_Node_Of_Expr);
2329 -- Divide operator case
2331 elsif Nkind (Or_Node_Of_Expr) = N_Op_Divide then
2332 Result := Process_Divide (Or_Node_Of_Expr);
2334 -- Minus operator case
2336 elsif Nkind (Or_Node_Of_Expr) = N_Op_Minus then
2337 Result := Process_Minus (Or_Node_Of_Expr);
2338 end if;
2340 -- When Expr cannot be interpreted as a rational and Complain is true,
2341 -- generate an error message.
2343 if Complain and then Result = No_Rational then
2344 Error_Msg_N ("rational expected", Expr);
2345 end if;
2347 return Result;
2348 end Create_Rational_From;
2350 -------------------
2351 -- Dimensions_Of --
2352 -------------------
2354 function Dimensions_Of (N : Node_Id) return Dimension_Type is
2355 begin
2356 return Dimension_Table.Get (N);
2357 end Dimensions_Of;
2359 -----------------------
2360 -- Dimensions_Msg_Of --
2361 -----------------------
2363 function Dimensions_Msg_Of
2364 (N : Node_Id;
2365 Description_Needed : Boolean := False) return String
2367 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
2368 Dimensions_Msg : Name_Id;
2369 System : System_Type;
2371 begin
2372 -- Initialization of Name_Buffer
2374 Name_Len := 0;
2376 -- N is not dimensionless
2378 if Exists (Dims_Of_N) then
2379 System := System_Of (Base_Type (Etype (N)));
2381 -- When Description_Needed, add to string "has dimension " before the
2382 -- actual dimension.
2384 if Description_Needed then
2385 Add_Str_To_Name_Buffer ("has dimension ");
2386 end if;
2388 Add_String_To_Name_Buffer
2389 (From_Dim_To_Str_Of_Dim_Symbols (Dims_Of_N, System, True));
2391 -- N is dimensionless
2393 -- When Description_Needed, return "is dimensionless"
2395 elsif Description_Needed then
2396 Add_Str_To_Name_Buffer ("is dimensionless");
2398 -- Otherwise, return "[]"
2400 else
2401 Add_Str_To_Name_Buffer ("[]");
2402 end if;
2404 Dimensions_Msg := Name_Find;
2405 return Get_Name_String (Dimensions_Msg);
2406 end Dimensions_Msg_Of;
2408 --------------------------
2409 -- Dimension_Table_Hash --
2410 --------------------------
2412 function Dimension_Table_Hash
2413 (Key : Node_Id) return Dimension_Table_Range
2415 begin
2416 return Dimension_Table_Range (Key mod 511);
2417 end Dimension_Table_Hash;
2419 -------------------------------------
2420 -- Dim_Warning_For_Numeric_Literal --
2421 -------------------------------------
2423 procedure Dim_Warning_For_Numeric_Literal (N : Node_Id; Typ : Entity_Id) is
2424 begin
2425 -- Initialize name buffer
2427 Name_Len := 0;
2429 Add_String_To_Name_Buffer (String_From_Numeric_Literal (N));
2431 -- Insert a blank between the literal and the symbol
2432 Add_Str_To_Name_Buffer (" ");
2434 Add_String_To_Name_Buffer (Symbol_Of (Typ));
2436 Error_Msg_Name_1 := Name_Find;
2437 Error_Msg_N ("??assumed to be%%", N);
2438 end Dim_Warning_For_Numeric_Literal;
2440 ----------------------------------------
2441 -- Eval_Op_Expon_For_Dimensioned_Type --
2442 ----------------------------------------
2444 -- Evaluate the expon operator for real dimensioned type.
2446 -- Note that if the exponent is an integer (denominator = 1) the node is
2447 -- evaluated by the regular Eval_Op_Expon routine (see Sem_Eval).
2449 procedure Eval_Op_Expon_For_Dimensioned_Type
2450 (N : Node_Id;
2451 Btyp : Entity_Id)
2453 R : constant Node_Id := Right_Opnd (N);
2454 R_Value : Rational := No_Rational;
2456 begin
2457 if Is_Real_Type (Btyp) then
2458 R_Value := Create_Rational_From (R, False);
2459 end if;
2461 -- Check that the exponent is not an integer
2463 if R_Value /= No_Rational and then R_Value.Denominator /= 1 then
2464 Eval_Op_Expon_With_Rational_Exponent (N, R_Value);
2465 else
2466 Eval_Op_Expon (N);
2467 end if;
2468 end Eval_Op_Expon_For_Dimensioned_Type;
2470 ------------------------------------------
2471 -- Eval_Op_Expon_With_Rational_Exponent --
2472 ------------------------------------------
2474 -- For dimensioned operand in exponentiation, exponent is allowed to be a
2475 -- Rational and not only an Integer like for dimensionless operands. For
2476 -- that particular case, the left operand is rewritten as a function call
2477 -- using the function Expon_LLF from s-llflex.ads.
2479 procedure Eval_Op_Expon_With_Rational_Exponent
2480 (N : Node_Id;
2481 Exponent_Value : Rational)
2483 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
2484 L : constant Node_Id := Left_Opnd (N);
2485 Etyp_Of_L : constant Entity_Id := Etype (L);
2486 Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2487 Loc : constant Source_Ptr := Sloc (N);
2488 Actual_1 : Node_Id;
2489 Actual_2 : Node_Id;
2490 Dim_Power : Rational;
2491 List_Of_Dims : List_Id;
2492 New_Aspect : Node_Id;
2493 New_Aspects : List_Id;
2494 New_Id : Entity_Id;
2495 New_N : Node_Id;
2496 New_Subtyp_Decl_For_L : Node_Id;
2497 System : System_Type;
2499 begin
2500 -- Case when the operand is not dimensionless
2502 if Exists (Dims_Of_N) then
2504 -- Get the corresponding System_Type to know the exact number of
2505 -- dimensions in the system.
2507 System := System_Of (Btyp_Of_L);
2509 -- Generation of a new subtype with the proper dimensions
2511 -- In order to rewrite the operator as a type conversion, a new
2512 -- dimensioned subtype with the resulting dimensions of the
2513 -- exponentiation must be created.
2515 -- Generate:
2517 -- Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2518 -- System : constant System_Id :=
2519 -- Get_Dimension_System_Id (Btyp_Of_L);
2520 -- Num_Of_Dims : constant Number_Of_Dimensions :=
2521 -- Dimension_Systems.Table (System).Dimension_Count;
2523 -- subtype T is Btyp_Of_L
2524 -- with
2525 -- Dimension => (
2526 -- Dims_Of_N (1).Numerator / Dims_Of_N (1).Denominator,
2527 -- Dims_Of_N (2).Numerator / Dims_Of_N (2).Denominator,
2528 -- ...
2529 -- Dims_Of_N (Num_Of_Dims).Numerator /
2530 -- Dims_Of_N (Num_Of_Dims).Denominator);
2532 -- Step 1: Generate the new aggregate for the aspect Dimension
2534 New_Aspects := Empty_List;
2535 List_Of_Dims := New_List;
2537 for Position in Dims_Of_N'First .. System.Count loop
2538 Dim_Power := Dims_Of_N (Position);
2539 Append_To (List_Of_Dims,
2540 Make_Op_Divide (Loc,
2541 Left_Opnd =>
2542 Make_Integer_Literal (Loc,
2543 Int (Dim_Power.Numerator)),
2544 Right_Opnd =>
2545 Make_Integer_Literal (Loc,
2546 Int (Dim_Power.Denominator))));
2547 end loop;
2549 -- Step 2: Create the new Aspect Specification for Aspect Dimension
2551 New_Aspect :=
2552 Make_Aspect_Specification (Loc,
2553 Identifier => Make_Identifier (Loc, Name_Dimension),
2554 Expression => Make_Aggregate (Loc, Expressions => List_Of_Dims));
2556 -- Step 3: Make a temporary identifier for the new subtype
2558 New_Id := Make_Temporary (Loc, 'T');
2559 Set_Is_Internal (New_Id);
2561 -- Step 4: Declaration of the new subtype
2563 New_Subtyp_Decl_For_L :=
2564 Make_Subtype_Declaration (Loc,
2565 Defining_Identifier => New_Id,
2566 Subtype_Indication => New_Occurrence_Of (Btyp_Of_L, Loc));
2568 Append (New_Aspect, New_Aspects);
2569 Set_Parent (New_Aspects, New_Subtyp_Decl_For_L);
2570 Set_Aspect_Specifications (New_Subtyp_Decl_For_L, New_Aspects);
2572 Analyze (New_Subtyp_Decl_For_L);
2574 -- Case where the operand is dimensionless
2576 else
2577 New_Id := Btyp_Of_L;
2578 end if;
2580 -- Replacement of N by New_N
2582 -- Generate:
2584 -- Actual_1 := Long_Long_Float (L),
2586 -- Actual_2 := Long_Long_Float (Exponent_Value.Numerator) /
2587 -- Long_Long_Float (Exponent_Value.Denominator);
2589 -- (T (Expon_LLF (Actual_1, Actual_2)));
2591 -- where T is the subtype declared in step 1
2593 -- The node is rewritten as a type conversion
2595 -- Step 1: Creation of the two parameters of Expon_LLF function call
2597 Actual_1 :=
2598 Make_Type_Conversion (Loc,
2599 Subtype_Mark => New_Reference_To (Standard_Long_Long_Float, Loc),
2600 Expression => Relocate_Node (L));
2602 Actual_2 :=
2603 Make_Op_Divide (Loc,
2604 Left_Opnd =>
2605 Make_Real_Literal (Loc,
2606 UR_From_Uint (UI_From_Int (Int (Exponent_Value.Numerator)))),
2607 Right_Opnd =>
2608 Make_Real_Literal (Loc,
2609 UR_From_Uint (UI_From_Int (Int (Exponent_Value.Denominator)))));
2611 -- Step 2: Creation of New_N
2613 New_N :=
2614 Make_Type_Conversion (Loc,
2615 Subtype_Mark => New_Reference_To (New_Id, Loc),
2616 Expression =>
2617 Make_Function_Call (Loc,
2618 Name => New_Reference_To (RTE (RE_Expon_LLF), Loc),
2619 Parameter_Associations => New_List (
2620 Actual_1, Actual_2)));
2622 -- Step 3: Rewrite N with the result
2624 Rewrite (N, New_N);
2625 Set_Etype (N, New_Id);
2626 Analyze_And_Resolve (N, New_Id);
2627 end Eval_Op_Expon_With_Rational_Exponent;
2629 ------------
2630 -- Exists --
2631 ------------
2633 function Exists (Dim : Dimension_Type) return Boolean is
2634 begin
2635 return Dim /= Null_Dimension;
2636 end Exists;
2638 function Exists (Str : String_Id) return Boolean is
2639 begin
2640 return Str /= No_String;
2641 end Exists;
2643 function Exists (Sys : System_Type) return Boolean is
2644 begin
2645 return Sys /= Null_System;
2646 end Exists;
2648 ---------------------------------
2649 -- Expand_Put_Call_With_Symbol --
2650 ---------------------------------
2652 -- For procedure Put (resp. Put_Dim_Of) defined in System.Dim.Float_IO
2653 -- (System.Dim.Integer_IO), the default string parameter must be rewritten
2654 -- to include the unit symbols (resp. dimension symbols) in the output
2655 -- of a dimensioned object. Note that if a value is already supplied for
2656 -- parameter Symbol, this routine doesn't do anything.
2658 -- Case 1. Item is dimensionless
2660 -- * Put : Item appears without a suffix
2662 -- * Put_Dim_Of : the output is []
2664 -- Obj : Mks_Type := 2.6;
2665 -- Put (Obj, 1, 1, 0);
2666 -- Put_Dim_Of (Obj);
2668 -- The corresponding outputs are:
2669 -- $2.6
2670 -- $[]
2672 -- Case 2. Item has a dimension
2674 -- * Put : If the type of Item is a dimensioned subtype whose
2675 -- symbol is not empty, then the symbol appears as a
2676 -- suffix. Otherwise, a new string is created and appears
2677 -- as a suffix of Item. This string results in the
2678 -- successive concatanations between each unit symbol
2679 -- raised by its corresponding dimension power from the
2680 -- dimensions of Item.
2682 -- * Put_Dim_Of : The output is a new string resulting in the successive
2683 -- concatanations between each dimension symbol raised by
2684 -- its corresponding dimension power from the dimensions of
2685 -- Item.
2687 -- subtype Random is Mks_Type
2688 -- with
2689 -- Dimension => (
2690 -- Meter => 3,
2691 -- Candela => -1,
2692 -- others => 0);
2694 -- Obj : Random := 5.0;
2695 -- Put (Obj);
2696 -- Put_Dim_Of (Obj);
2698 -- The corresponding outputs are:
2699 -- $5.0 m**3.cd**(-1)
2700 -- $[l**3.J**(-1)]
2702 procedure Expand_Put_Call_With_Symbol (N : Node_Id) is
2703 Actuals : constant List_Id := Parameter_Associations (N);
2704 Loc : constant Source_Ptr := Sloc (N);
2705 Name_Call : constant Node_Id := Name (N);
2706 New_Actuals : constant List_Id := New_List;
2707 Actual : Node_Id;
2708 Dims_Of_Actual : Dimension_Type;
2709 Etyp : Entity_Id;
2710 New_Str_Lit : Node_Id := Empty;
2711 Symbols : String_Id;
2713 Is_Put_Dim_Of : Boolean := False;
2714 -- This flag is used in order to differentiate routines Put and
2715 -- Put_Dim_Of. Set to True if the procedure is one of the Put_Dim_Of
2716 -- defined in System.Dim.Float_IO or System.Dim.Integer_IO.
2718 function Has_Symbols return Boolean;
2719 -- Return True if the current Put call already has a parameter
2720 -- association for parameter "Symbols" with the correct string of
2721 -- symbols.
2723 function Is_Procedure_Put_Call return Boolean;
2724 -- Return True if the current call is a call of an instantiation of a
2725 -- procedure Put defined in the package System.Dim.Float_IO and
2726 -- System.Dim.Integer_IO.
2728 function Item_Actual return Node_Id;
2729 -- Return the item actual parameter node in the output call
2731 -----------------
2732 -- Has_Symbols --
2733 -----------------
2735 function Has_Symbols return Boolean is
2736 Actual : Node_Id;
2737 Actual_Str : Node_Id;
2739 begin
2740 Actual := First (Actuals);
2742 -- Look for a symbols parameter association in the list of actuals
2744 while Present (Actual) loop
2746 -- Positional parameter association case when the actual is a
2747 -- string literal.
2749 if Nkind (Actual) = N_String_Literal then
2750 Actual_Str := Actual;
2752 -- Named parameter association case when selector name is Symbol
2754 elsif Nkind (Actual) = N_Parameter_Association
2755 and then Chars (Selector_Name (Actual)) = Name_Symbol
2756 then
2757 Actual_Str := Explicit_Actual_Parameter (Actual);
2759 -- Ignore all other cases
2761 else
2762 Actual_Str := Empty;
2763 end if;
2765 if Present (Actual_Str) then
2767 -- Return True if the actual comes from source or if the string
2768 -- of symbols doesn't have the default value (i.e. it is "").
2770 if Comes_From_Source (Actual)
2771 or else String_Length (Strval (Actual_Str)) /= 0
2772 then
2773 -- Complain only if the actual comes from source or if it
2774 -- hasn't been fully analyzed yet.
2776 if Comes_From_Source (Actual)
2777 or else not Analyzed (Actual)
2778 then
2779 Error_Msg_N ("Symbol parameter should not be provided",
2780 Actual);
2781 Error_Msg_N ("\reserved for compiler use only", Actual);
2782 end if;
2784 return True;
2786 else
2787 return False;
2788 end if;
2789 end if;
2791 Next (Actual);
2792 end loop;
2794 -- At this point, the call has no parameter association. Look to the
2795 -- last actual since the symbols parameter is the last one.
2797 return Nkind (Last (Actuals)) = N_String_Literal;
2798 end Has_Symbols;
2800 ---------------------------
2801 -- Is_Procedure_Put_Call --
2802 ---------------------------
2804 function Is_Procedure_Put_Call return Boolean is
2805 Ent : Entity_Id;
2806 Loc : Source_Ptr;
2808 begin
2809 -- There are three different Put (resp. Put_Dim_Of) routines in each
2810 -- generic dim IO package. Verify the current procedure call is one
2811 -- of them.
2813 if Is_Entity_Name (Name_Call) then
2814 Ent := Entity (Name_Call);
2816 -- Get the original subprogram entity following the renaming chain
2818 if Present (Alias (Ent)) then
2819 Ent := Alias (Ent);
2820 end if;
2822 Loc := Sloc (Ent);
2824 -- Check the name of the entity subprogram is Put (resp.
2825 -- Put_Dim_Of) and verify this entity is located in either
2826 -- System.Dim.Float_IO or System.Dim.Integer_IO.
2828 if Loc > No_Location
2829 and then Is_Dim_IO_Package_Entity
2830 (Cunit_Entity (Get_Source_Unit (Loc)))
2831 then
2832 if Chars (Ent) = Name_Put_Dim_Of then
2833 Is_Put_Dim_Of := True;
2834 return True;
2836 elsif Chars (Ent) = Name_Put then
2837 return True;
2838 end if;
2839 end if;
2840 end if;
2842 return False;
2843 end Is_Procedure_Put_Call;
2845 -----------------
2846 -- Item_Actual --
2847 -----------------
2849 function Item_Actual return Node_Id is
2850 Actual : Node_Id;
2852 begin
2853 -- Look for the item actual as a parameter association
2855 Actual := First (Actuals);
2856 while Present (Actual) loop
2857 if Nkind (Actual) = N_Parameter_Association
2858 and then Chars (Selector_Name (Actual)) = Name_Item
2859 then
2860 return Explicit_Actual_Parameter (Actual);
2861 end if;
2863 Next (Actual);
2864 end loop;
2866 -- Case where the item has been defined without an association
2868 Actual := First (Actuals);
2870 -- Depending on the procedure Put, Item actual could be first or
2871 -- second in the list of actuals.
2873 if Has_Dimension_System (Base_Type (Etype (Actual))) then
2874 return Actual;
2875 else
2876 return Next (Actual);
2877 end if;
2878 end Item_Actual;
2880 -- Start of processing for Expand_Put_Call_With_Symbol
2882 begin
2883 if Is_Procedure_Put_Call and then not Has_Symbols then
2884 Actual := Item_Actual;
2885 Dims_Of_Actual := Dimensions_Of (Actual);
2886 Etyp := Etype (Actual);
2888 -- Put_Dim_Of case
2890 if Is_Put_Dim_Of then
2892 -- Check that the item is not dimensionless
2894 -- Create the new String_Literal with the new String_Id generated
2895 -- by the routine From_Dim_To_Str_Of_Dim_Symbols.
2897 if Exists (Dims_Of_Actual) then
2898 New_Str_Lit :=
2899 Make_String_Literal (Loc,
2900 From_Dim_To_Str_Of_Dim_Symbols
2901 (Dims_Of_Actual, System_Of (Base_Type (Etyp))));
2903 -- If dimensionless, the output is []
2905 else
2906 New_Str_Lit :=
2907 Make_String_Literal (Loc, "[]");
2908 end if;
2910 -- Put case
2912 else
2913 -- Add the symbol as a suffix of the value if the subtype has a
2914 -- unit symbol or if the parameter is not dimensionless.
2916 if Exists (Symbol_Of (Etyp)) then
2917 Symbols := Symbol_Of (Etyp);
2918 else
2919 Symbols := From_Dim_To_Str_Of_Unit_Symbols
2920 (Dims_Of_Actual, System_Of (Base_Type (Etyp)));
2921 end if;
2923 -- Check Symbols exists
2925 if Exists (Symbols) then
2926 Start_String;
2928 -- Put a space between the value and the dimension
2930 Store_String_Char (' ');
2931 Store_String_Chars (Symbols);
2932 New_Str_Lit := Make_String_Literal (Loc, End_String);
2933 end if;
2934 end if;
2936 if Present (New_Str_Lit) then
2938 -- Insert all actuals in New_Actuals
2940 Actual := First (Actuals);
2941 while Present (Actual) loop
2943 -- Copy every actuals in New_Actuals except the Symbols
2944 -- parameter association.
2946 if Nkind (Actual) = N_Parameter_Association
2947 and then Chars (Selector_Name (Actual)) /= Name_Symbol
2948 then
2949 Append_To (New_Actuals,
2950 Make_Parameter_Association (Loc,
2951 Selector_Name => New_Copy (Selector_Name (Actual)),
2952 Explicit_Actual_Parameter =>
2953 New_Copy (Explicit_Actual_Parameter (Actual))));
2955 elsif Nkind (Actual) /= N_Parameter_Association then
2956 Append_To (New_Actuals, New_Copy (Actual));
2957 end if;
2959 Next (Actual);
2960 end loop;
2962 -- Create new Symbols param association and append to New_Actuals
2964 Append_To (New_Actuals,
2965 Make_Parameter_Association (Loc,
2966 Selector_Name => Make_Identifier (Loc, Name_Symbol),
2967 Explicit_Actual_Parameter => New_Str_Lit));
2969 -- Rewrite and analyze the procedure call
2971 Rewrite (N,
2972 Make_Procedure_Call_Statement (Loc,
2973 Name => New_Copy (Name_Call),
2974 Parameter_Associations => New_Actuals));
2976 Analyze (N);
2977 end if;
2978 end if;
2979 end Expand_Put_Call_With_Symbol;
2981 ------------------------------------
2982 -- From_Dim_To_Str_Of_Dim_Symbols --
2983 ------------------------------------
2985 -- Given a dimension vector and the corresponding dimension system, create
2986 -- a String_Id to output dimension symbols corresponding to the dimensions
2987 -- Dims. If In_Error_Msg is True, there is a special handling for character
2988 -- asterisk * which is an insertion character in error messages.
2990 function From_Dim_To_Str_Of_Dim_Symbols
2991 (Dims : Dimension_Type;
2992 System : System_Type;
2993 In_Error_Msg : Boolean := False) return String_Id
2995 Dim_Power : Rational;
2996 First_Dim : Boolean := True;
2998 procedure Store_String_Oexpon;
2999 -- Store the expon operator symbol "**" in the string. In error
3000 -- messages, asterisk * is a special character and must be quoted
3001 -- to be placed literally into the message.
3003 -------------------------
3004 -- Store_String_Oexpon --
3005 -------------------------
3007 procedure Store_String_Oexpon is
3008 begin
3009 if In_Error_Msg then
3010 Store_String_Chars ("'*'*");
3011 else
3012 Store_String_Chars ("**");
3013 end if;
3014 end Store_String_Oexpon;
3016 -- Start of processing for From_Dim_To_Str_Of_Dim_Symbols
3018 begin
3019 -- Initialization of the new String_Id
3021 Start_String;
3023 -- Store the dimension symbols inside boxes
3025 Store_String_Char ('[');
3027 for Position in Dimension_Type'Range loop
3028 Dim_Power := Dims (Position);
3029 if Dim_Power /= Zero then
3031 if First_Dim then
3032 First_Dim := False;
3033 else
3034 Store_String_Char ('.');
3035 end if;
3037 Store_String_Chars (System.Dim_Symbols (Position));
3039 -- Positive dimension case
3041 if Dim_Power.Numerator > 0 then
3042 -- Integer case
3044 if Dim_Power.Denominator = 1 then
3045 if Dim_Power.Numerator /= 1 then
3046 Store_String_Oexpon;
3047 Store_String_Int (Int (Dim_Power.Numerator));
3048 end if;
3050 -- Rational case when denominator /= 1
3052 else
3053 Store_String_Oexpon;
3054 Store_String_Char ('(');
3055 Store_String_Int (Int (Dim_Power.Numerator));
3056 Store_String_Char ('/');
3057 Store_String_Int (Int (Dim_Power.Denominator));
3058 Store_String_Char (')');
3059 end if;
3061 -- Negative dimension case
3063 else
3064 Store_String_Oexpon;
3065 Store_String_Char ('(');
3066 Store_String_Char ('-');
3067 Store_String_Int (Int (-Dim_Power.Numerator));
3069 -- Integer case
3071 if Dim_Power.Denominator = 1 then
3072 Store_String_Char (')');
3074 -- Rational case when denominator /= 1
3076 else
3077 Store_String_Char ('/');
3078 Store_String_Int (Int (Dim_Power.Denominator));
3079 Store_String_Char (')');
3080 end if;
3081 end if;
3082 end if;
3083 end loop;
3085 Store_String_Char (']');
3086 return End_String;
3087 end From_Dim_To_Str_Of_Dim_Symbols;
3089 -------------------------------------
3090 -- From_Dim_To_Str_Of_Unit_Symbols --
3091 -------------------------------------
3093 -- Given a dimension vector and the corresponding dimension system,
3094 -- create a String_Id to output the unit symbols corresponding to the
3095 -- dimensions Dims.
3097 function From_Dim_To_Str_Of_Unit_Symbols
3098 (Dims : Dimension_Type;
3099 System : System_Type) return String_Id
3101 Dim_Power : Rational;
3102 First_Dim : Boolean := True;
3104 begin
3105 -- Return No_String if dimensionless
3107 if not Exists (Dims) then
3108 return No_String;
3109 end if;
3111 -- Initialization of the new String_Id
3113 Start_String;
3115 for Position in Dimension_Type'Range loop
3116 Dim_Power := Dims (Position);
3118 if Dim_Power /= Zero then
3120 if First_Dim then
3121 First_Dim := False;
3122 else
3123 Store_String_Char ('.');
3124 end if;
3126 Store_String_Chars (System.Unit_Symbols (Position));
3128 -- Positive dimension case
3130 if Dim_Power.Numerator > 0 then
3132 -- Integer case
3134 if Dim_Power.Denominator = 1 then
3135 if Dim_Power.Numerator /= 1 then
3136 Store_String_Chars ("**");
3137 Store_String_Int (Int (Dim_Power.Numerator));
3138 end if;
3140 -- Rational case when denominator /= 1
3142 else
3143 Store_String_Chars ("**");
3144 Store_String_Char ('(');
3145 Store_String_Int (Int (Dim_Power.Numerator));
3146 Store_String_Char ('/');
3147 Store_String_Int (Int (Dim_Power.Denominator));
3148 Store_String_Char (')');
3149 end if;
3151 -- Negative dimension case
3153 else
3154 Store_String_Chars ("**");
3155 Store_String_Char ('(');
3156 Store_String_Char ('-');
3157 Store_String_Int (Int (-Dim_Power.Numerator));
3159 -- Integer case
3161 if Dim_Power.Denominator = 1 then
3162 Store_String_Char (')');
3164 -- Rational case when denominator /= 1
3166 else
3167 Store_String_Char ('/');
3168 Store_String_Int (Int (Dim_Power.Denominator));
3169 Store_String_Char (')');
3170 end if;
3171 end if;
3172 end if;
3173 end loop;
3175 return End_String;
3176 end From_Dim_To_Str_Of_Unit_Symbols;
3178 ---------
3179 -- GCD --
3180 ---------
3182 function GCD (Left, Right : Whole) return Int is
3183 L : Whole;
3184 R : Whole;
3186 begin
3187 L := Left;
3188 R := Right;
3189 while R /= 0 loop
3190 L := L mod R;
3192 if L = 0 then
3193 return Int (R);
3194 end if;
3196 R := R mod L;
3197 end loop;
3199 return Int (L);
3200 end GCD;
3202 --------------------------
3203 -- Has_Dimension_System --
3204 --------------------------
3206 function Has_Dimension_System (Typ : Entity_Id) return Boolean is
3207 begin
3208 return Exists (System_Of (Typ));
3209 end Has_Dimension_System;
3211 ------------------------------
3212 -- Is_Dim_IO_Package_Entity --
3213 ------------------------------
3215 function Is_Dim_IO_Package_Entity (E : Entity_Id) return Boolean is
3216 begin
3217 -- Check the package entity corresponds to System.Dim.Float_IO or
3218 -- System.Dim.Integer_IO.
3220 return
3221 Is_RTU (E, System_Dim_Float_IO)
3222 or else
3223 Is_RTU (E, System_Dim_Integer_IO);
3224 end Is_Dim_IO_Package_Entity;
3226 -------------------------------------
3227 -- Is_Dim_IO_Package_Instantiation --
3228 -------------------------------------
3230 function Is_Dim_IO_Package_Instantiation (N : Node_Id) return Boolean is
3231 Gen_Id : constant Node_Id := Name (N);
3233 begin
3234 -- Check that the instantiated package is either System.Dim.Float_IO
3235 -- or System.Dim.Integer_IO.
3237 return
3238 Is_Entity_Name (Gen_Id)
3239 and then Is_Dim_IO_Package_Entity (Entity (Gen_Id));
3240 end Is_Dim_IO_Package_Instantiation;
3242 ----------------
3243 -- Is_Invalid --
3244 ----------------
3246 function Is_Invalid (Position : Dimension_Position) return Boolean is
3247 begin
3248 return Position = Invalid_Position;
3249 end Is_Invalid;
3251 ---------------------
3252 -- Move_Dimensions --
3253 ---------------------
3255 procedure Move_Dimensions (From, To : Node_Id) is
3256 begin
3257 if Ada_Version < Ada_2012 then
3258 return;
3259 end if;
3261 -- Copy the dimension of 'From to 'To' and remove dimension of 'From'
3263 Copy_Dimensions (From, To);
3264 Remove_Dimensions (From);
3265 end Move_Dimensions;
3267 ------------
3268 -- Reduce --
3269 ------------
3271 function Reduce (X : Rational) return Rational is
3272 begin
3273 if X.Numerator = 0 then
3274 return Zero;
3275 end if;
3277 declare
3278 G : constant Int := GCD (X.Numerator, X.Denominator);
3279 begin
3280 return Rational'(Numerator => Whole (Int (X.Numerator) / G),
3281 Denominator => Whole (Int (X.Denominator) / G));
3282 end;
3283 end Reduce;
3285 -----------------------
3286 -- Remove_Dimensions --
3287 -----------------------
3289 procedure Remove_Dimensions (N : Node_Id) is
3290 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
3291 begin
3292 if Exists (Dims_Of_N) then
3293 Dimension_Table.Remove (N);
3294 end if;
3295 end Remove_Dimensions;
3297 -----------------------------------
3298 -- Remove_Dimension_In_Statement --
3299 -----------------------------------
3301 -- Removal of dimension in statement as part of the Analyze_Statements
3302 -- routine (see package Sem_Ch5).
3304 procedure Remove_Dimension_In_Statement (Stmt : Node_Id) is
3305 begin
3306 if Ada_Version < Ada_2012 then
3307 return;
3308 end if;
3310 -- Remove dimension in parameter specifications for accept statement
3312 if Nkind (Stmt) = N_Accept_Statement then
3313 declare
3314 Param : Node_Id := First (Parameter_Specifications (Stmt));
3315 begin
3316 while Present (Param) loop
3317 Remove_Dimensions (Param);
3318 Next (Param);
3319 end loop;
3320 end;
3322 -- Remove dimension of name and expression in assignments
3324 elsif Nkind (Stmt) = N_Assignment_Statement then
3325 Remove_Dimensions (Expression (Stmt));
3326 Remove_Dimensions (Name (Stmt));
3327 end if;
3328 end Remove_Dimension_In_Statement;
3330 --------------------
3331 -- Set_Dimensions --
3332 --------------------
3334 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type) is
3335 begin
3336 pragma Assert (OK_For_Dimension (Nkind (N)));
3337 pragma Assert (Exists (Val));
3339 Dimension_Table.Set (N, Val);
3340 end Set_Dimensions;
3342 ----------------
3343 -- Set_Symbol --
3344 ----------------
3346 procedure Set_Symbol (E : Entity_Id; Val : String_Id) is
3347 begin
3348 Symbol_Table.Set (E, Val);
3349 end Set_Symbol;
3351 ---------------------------------
3352 -- String_From_Numeric_Literal --
3353 ---------------------------------
3355 function String_From_Numeric_Literal (N : Node_Id) return String_Id is
3356 Loc : constant Source_Ptr := Sloc (N);
3357 Sbuffer : constant Source_Buffer_Ptr :=
3358 Source_Text (Get_Source_File_Index (Loc));
3359 Src_Ptr : Source_Ptr := Loc;
3360 C : Character := Sbuffer (Src_Ptr);
3361 -- Current source program character
3363 function Belong_To_Numeric_Literal (C : Character) return Boolean;
3364 -- Return True if C belongs to a numeric literal
3366 -------------------------------
3367 -- Belong_To_Numeric_Literal --
3368 -------------------------------
3370 function Belong_To_Numeric_Literal (C : Character) return Boolean is
3371 begin
3372 case C is
3373 when '0' .. '9' |
3374 '_' |
3375 '.' |
3376 'e' |
3377 '#' |
3378 'A' |
3379 'B' |
3380 'C' |
3381 'D' |
3382 'E' |
3383 'F' =>
3384 return True;
3386 -- Make sure '+' or '-' is part of an exponent.
3388 when '+' | '-' =>
3389 declare
3390 Prev_C : constant Character := Sbuffer (Src_Ptr - 1);
3391 begin
3392 return Prev_C = 'e' or else Prev_C = 'E';
3393 end;
3395 -- All other character doesn't belong to a numeric literal
3397 when others =>
3398 return False;
3399 end case;
3400 end Belong_To_Numeric_Literal;
3402 -- Start of processing for String_From_Numeric_Literal
3404 begin
3405 Start_String;
3406 while Belong_To_Numeric_Literal (C) loop
3407 Store_String_Char (C);
3408 Src_Ptr := Src_Ptr + 1;
3409 C := Sbuffer (Src_Ptr);
3410 end loop;
3412 return End_String;
3413 end String_From_Numeric_Literal;
3415 ---------------
3416 -- Symbol_Of --
3417 ---------------
3419 function Symbol_Of (E : Entity_Id) return String_Id is
3420 Subtype_Symbol : constant String_Id := Symbol_Table.Get (E);
3421 begin
3422 if Subtype_Symbol /= No_String then
3423 return Subtype_Symbol;
3424 else
3425 return From_Dim_To_Str_Of_Unit_Symbols
3426 (Dimensions_Of (E), System_Of (Base_Type (E)));
3427 end if;
3428 end Symbol_Of;
3430 -----------------------
3431 -- Symbol_Table_Hash --
3432 -----------------------
3434 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range is
3435 begin
3436 return Symbol_Table_Range (Key mod 511);
3437 end Symbol_Table_Hash;
3439 ---------------
3440 -- System_Of --
3441 ---------------
3443 function System_Of (E : Entity_Id) return System_Type is
3444 Type_Decl : constant Node_Id := Parent (E);
3446 begin
3447 -- Look for Type_Decl in System_Table
3449 for Dim_Sys in 1 .. System_Table.Last loop
3450 if Type_Decl = System_Table.Table (Dim_Sys).Type_Decl then
3451 return System_Table.Table (Dim_Sys);
3452 end if;
3453 end loop;
3455 return Null_System;
3456 end System_Of;
3458 end Sem_Dim;