1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2011-2016, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Einfo
; use Einfo
;
29 with Errout
; use Errout
;
30 with Exp_Util
; use Exp_Util
;
32 with Namet
; use Namet
;
33 with Nlists
; use Nlists
;
34 with Nmake
; use Nmake
;
36 with Rtsfind
; use Rtsfind
;
38 with Sem_Eval
; use Sem_Eval
;
39 with Sem_Res
; use Sem_Res
;
40 with Sem_Util
; use Sem_Util
;
41 with Sinfo
; use Sinfo
;
42 with Sinput
; use Sinput
;
43 with Snames
; use Snames
;
44 with Stand
; use Stand
;
45 with Stringt
; use Stringt
;
47 with Tbuild
; use Tbuild
;
48 with Uintp
; use Uintp
;
49 with Urealp
; use Urealp
;
53 package body Sem_Dim
is
55 -------------------------
56 -- Rational Arithmetic --
57 -------------------------
59 type Whole
is new Int
;
60 subtype Positive_Whole
is Whole
range 1 .. Whole
'Last;
62 type Rational
is record
64 Denominator
: Positive_Whole
;
67 Zero
: constant Rational
:= Rational
'(Numerator => 0,
70 No_Rational : constant Rational := Rational'(Numerator
=> 0,
72 -- Used to indicate an expression that cannot be interpreted as a rational
73 -- Returned value of the Create_Rational_From routine when parameter Expr
74 -- is not a static representation of a rational.
76 -- Rational constructors
78 function "+" (Right
: Whole
) return Rational
;
79 function GCD
(Left
, Right
: Whole
) return Int
;
80 function Reduce
(X
: Rational
) return Rational
;
82 -- Unary operator for Rational
84 function "-" (Right
: Rational
) return Rational
;
85 function "abs" (Right
: Rational
) return Rational
;
87 -- Rational operations for Rationals
89 function "+" (Left
, Right
: Rational
) return Rational
;
90 function "-" (Left
, Right
: Rational
) return Rational
;
91 function "*" (Left
, Right
: Rational
) return Rational
;
92 function "/" (Left
, Right
: Rational
) return Rational
;
98 Max_Number_Of_Dimensions
: constant := 7;
99 -- Maximum number of dimensions in a dimension system
101 High_Position_Bound
: constant := Max_Number_Of_Dimensions
;
102 Invalid_Position
: constant := 0;
103 Low_Position_Bound
: constant := 1;
105 subtype Dimension_Position
is
106 Nat
range Invalid_Position
.. High_Position_Bound
;
109 array (Dimension_Position
range
110 Low_Position_Bound
.. High_Position_Bound
) of Name_Id
;
111 -- Store the names of all units within a system
113 No_Names
: constant Name_Array
:= (others => No_Name
);
116 array (Dimension_Position
range
117 Low_Position_Bound
.. High_Position_Bound
) of String_Id
;
118 -- Store the symbols of all units within a system
120 No_Symbols
: constant Symbol_Array
:= (others => No_String
);
122 -- The following record should be documented field by field
124 type System_Type
is record
126 Unit_Names
: Name_Array
;
127 Unit_Symbols
: Symbol_Array
;
128 Dim_Symbols
: Symbol_Array
;
129 Count
: Dimension_Position
;
132 Null_System
: constant System_Type
:=
133 (Empty
, No_Names
, No_Symbols
, No_Symbols
, Invalid_Position
);
135 subtype System_Id
is Nat
;
137 -- The following table maps types to systems
139 package System_Table
is new Table
.Table
(
140 Table_Component_Type
=> System_Type
,
141 Table_Index_Type
=> System_Id
,
142 Table_Low_Bound
=> 1,
144 Table_Increment
=> 5,
145 Table_Name
=> "System_Table");
151 type Dimension_Type
is
152 array (Dimension_Position
range
153 Low_Position_Bound
.. High_Position_Bound
) of Rational
;
155 Null_Dimension
: constant Dimension_Type
:= (others => Zero
);
157 type Dimension_Table_Range
is range 0 .. 510;
158 function Dimension_Table_Hash
(Key
: Node_Id
) return Dimension_Table_Range
;
160 -- The following table associates nodes with dimensions
162 package Dimension_Table
is new
163 GNAT
.HTable
.Simple_HTable
164 (Header_Num
=> Dimension_Table_Range
,
165 Element
=> Dimension_Type
,
166 No_Element
=> Null_Dimension
,
168 Hash
=> Dimension_Table_Hash
,
175 type Symbol_Table_Range
is range 0 .. 510;
176 function Symbol_Table_Hash
(Key
: Entity_Id
) return Symbol_Table_Range
;
178 -- Each subtype with a dimension has a symbolic representation of the
179 -- related unit. This table establishes a relation between the subtype
182 package Symbol_Table
is new
183 GNAT
.HTable
.Simple_HTable
184 (Header_Num
=> Symbol_Table_Range
,
185 Element
=> String_Id
,
186 No_Element
=> No_String
,
188 Hash
=> Symbol_Table_Hash
,
191 -- The following array enumerates all contexts which may contain or
192 -- produce a dimension.
194 OK_For_Dimension
: constant array (Node_Kind
) of Boolean :=
195 (N_Attribute_Reference
=> True,
196 N_Expanded_Name
=> True,
197 N_Explicit_Dereference
=> True,
198 N_Defining_Identifier
=> True,
199 N_Function_Call
=> True,
200 N_Identifier
=> True,
201 N_Indexed_Component
=> True,
202 N_Integer_Literal
=> True,
209 N_Op_Multiply
=> True,
212 N_Op_Subtract
=> True,
213 N_Qualified_Expression
=> True,
214 N_Real_Literal
=> True,
215 N_Selected_Component
=> True,
217 N_Type_Conversion
=> True,
218 N_Unchecked_Type_Conversion
=> True,
222 -----------------------
223 -- Local Subprograms --
224 -----------------------
226 procedure Analyze_Dimension_Assignment_Statement
(N
: Node_Id
);
227 -- Subroutine of Analyze_Dimension for assignment statement. Check that the
228 -- dimensions of the left-hand side and the right-hand side of N match.
230 procedure Analyze_Dimension_Binary_Op
(N
: Node_Id
);
231 -- Subroutine of Analyze_Dimension for binary operators. Check the
232 -- dimensions of the right and the left operand permit the operation.
233 -- Then, evaluate the resulting dimensions for each binary operator.
235 procedure Analyze_Dimension_Component_Declaration
(N
: Node_Id
);
236 -- Subroutine of Analyze_Dimension for component declaration. Check that
237 -- the dimensions of the type of N and of the expression match.
239 procedure Analyze_Dimension_Extended_Return_Statement
(N
: Node_Id
);
240 -- Subroutine of Analyze_Dimension for extended return statement. Check
241 -- that the dimensions of the returned type and of the returned object
244 procedure Analyze_Dimension_Has_Etype
(N
: Node_Id
);
245 -- Subroutine of Analyze_Dimension for a subset of N_Has_Etype denoted by
247 -- N_Attribute_Reference
249 -- N_Indexed_Component
250 -- N_Qualified_Expression
251 -- N_Selected_Component
254 -- N_Unchecked_Type_Conversion
256 procedure Analyze_Dimension_Number_Declaration
(N
: Node_Id
);
257 -- Procedure to analyze dimension of expression in a number declaration.
258 -- This allows a named number to have nontrivial dimensions, while by
259 -- default a named number is dimensionless.
261 procedure Analyze_Dimension_Object_Declaration
(N
: Node_Id
);
262 -- Subroutine of Analyze_Dimension for object declaration. Check that
263 -- the dimensions of the object type and the dimensions of the expression
264 -- (if expression is present) match. Note that when the expression is
265 -- a literal, no error is returned. This special case allows object
266 -- declaration such as: m : constant Length := 1.0;
268 procedure Analyze_Dimension_Object_Renaming_Declaration
(N
: Node_Id
);
269 -- Subroutine of Analyze_Dimension for object renaming declaration. Check
270 -- the dimensions of the type and of the renamed object name of N match.
272 procedure Analyze_Dimension_Simple_Return_Statement
(N
: Node_Id
);
273 -- Subroutine of Analyze_Dimension for simple return statement
274 -- Check that the dimensions of the returned type and of the returned
277 procedure Analyze_Dimension_Subtype_Declaration
(N
: Node_Id
);
278 -- Subroutine of Analyze_Dimension for subtype declaration. Propagate the
279 -- dimensions from the parent type to the identifier of N. Note that if
280 -- both the identifier and the parent type of N are not dimensionless,
283 procedure Analyze_Dimension_Unary_Op
(N
: Node_Id
);
284 -- Subroutine of Analyze_Dimension for unary operators. For Plus, Minus and
285 -- Abs operators, propagate the dimensions from the operand to N.
287 function Create_Rational_From
289 Complain
: Boolean) return Rational
;
290 -- Given an arbitrary expression Expr, return a valid rational if Expr can
291 -- be interpreted as a rational. Otherwise return No_Rational and also an
292 -- error message if Complain is set to True.
294 function Dimensions_Of
(N
: Node_Id
) return Dimension_Type
;
295 -- Return the dimension vector of node N
297 function Dimensions_Msg_Of
299 Description_Needed
: Boolean := False) return String;
300 -- Given a node N, return the dimension symbols of N, preceded by "has
301 -- dimension" if Description_Needed. if N is dimensionless, return "'[']",
302 -- or "is dimensionless" if Description_Needed.
304 procedure Dim_Warning_For_Numeric_Literal
(N
: Node_Id
; Typ
: Entity_Id
);
305 -- Issue a warning on the given numeric literal N to indicate that the
306 -- compiler made the assumption that the literal is not dimensionless
307 -- but has the dimension of Typ.
309 procedure Eval_Op_Expon_With_Rational_Exponent
311 Exponent_Value
: Rational
);
312 -- Evaluate the exponent it is a rational and the operand has a dimension
314 function Exists
(Dim
: Dimension_Type
) return Boolean;
315 -- Returns True iff Dim does not denote the null dimension
317 function Exists
(Str
: String_Id
) return Boolean;
318 -- Returns True iff Str does not denote No_String
320 function Exists
(Sys
: System_Type
) return Boolean;
321 -- Returns True iff Sys does not denote the null system
323 function From_Dim_To_Str_Of_Dim_Symbols
324 (Dims
: Dimension_Type
;
325 System
: System_Type
;
326 In_Error_Msg
: Boolean := False) return String_Id
;
327 -- Given a dimension vector and a dimension system, return the proper
328 -- string of dimension symbols. If In_Error_Msg is True (i.e. the String_Id
329 -- will be used to issue an error message) then this routine has a special
330 -- handling for the insertion characters * or [ which must be preceded by
331 -- a quote ' to be placed literally into the message.
333 function From_Dim_To_Str_Of_Unit_Symbols
334 (Dims
: Dimension_Type
;
335 System
: System_Type
) return String_Id
;
336 -- Given a dimension vector and a dimension system, return the proper
337 -- string of unit symbols.
339 function Is_Dim_IO_Package_Entity
(E
: Entity_Id
) return Boolean;
340 -- Return True if E is the package entity of System.Dim.Float_IO or
341 -- System.Dim.Integer_IO.
343 function Is_Invalid
(Position
: Dimension_Position
) return Boolean;
344 -- Return True if Pos denotes the invalid position
346 procedure Move_Dimensions
(From
: Node_Id
; To
: Node_Id
);
347 -- Copy dimension vector of From to To and delete dimension vector of From
349 procedure Remove_Dimensions
(N
: Node_Id
);
350 -- Remove the dimension vector of node N
352 procedure Set_Dimensions
(N
: Node_Id
; Val
: Dimension_Type
);
353 -- Associate a dimension vector with a node
355 procedure Set_Symbol
(E
: Entity_Id
; Val
: String_Id
);
356 -- Associate a symbol representation of a dimension vector with a subtype
358 function String_From_Numeric_Literal
(N
: Node_Id
) return String_Id
;
359 -- Return the string that corresponds to the numeric litteral N as it
360 -- appears in the source.
362 function Symbol_Of
(E
: Entity_Id
) return String_Id
;
363 -- E denotes a subtype with a dimension. Return the symbol representation
364 -- of the dimension vector.
366 function System_Of
(E
: Entity_Id
) return System_Type
;
367 -- E denotes a type, return associated system of the type if it has one
373 function "+" (Right
: Whole
) return Rational
is
375 return Rational
'(Numerator => Right, Denominator => 1);
378 function "+" (Left, Right : Rational) return Rational is
379 R : constant Rational :=
380 Rational'(Numerator
=> Left
.Numerator
* Right
.Denominator
+
381 Left
.Denominator
* Right
.Numerator
,
382 Denominator
=> Left
.Denominator
* Right
.Denominator
);
391 function "-" (Right
: Rational
) return Rational
is
393 return Rational
'(Numerator => -Right.Numerator,
394 Denominator => Right.Denominator);
397 function "-" (Left, Right : Rational) return Rational is
398 R : constant Rational :=
399 Rational'(Numerator
=> Left
.Numerator
* Right
.Denominator
-
400 Left
.Denominator
* Right
.Numerator
,
401 Denominator
=> Left
.Denominator
* Right
.Denominator
);
411 function "*" (Left
, Right
: Rational
) return Rational
is
412 R
: constant Rational
:=
413 Rational
'(Numerator => Left.Numerator * Right.Numerator,
414 Denominator => Left.Denominator * Right.Denominator);
423 function "/" (Left, Right : Rational) return Rational is
424 R : constant Rational := abs Right;
425 L : Rational := Left;
428 if Right.Numerator < 0 then
429 L.Numerator := Whole (-Integer (L.Numerator));
432 return Reduce (Rational'(Numerator
=> L
.Numerator
* R
.Denominator
,
433 Denominator
=> L
.Denominator
* R
.Numerator
));
440 function "abs" (Right
: Rational
) return Rational
is
442 return Rational
'(Numerator => abs Right.Numerator,
443 Denominator => Right.Denominator);
446 ------------------------------
447 -- Analyze_Aspect_Dimension --
448 ------------------------------
451 -- ([Symbol =>] SYMBOL, DIMENSION_VALUE {, DIMENSION_Value})
453 -- SYMBOL ::= STRING_LITERAL | CHARACTER_LITERAL
455 -- DIMENSION_VALUE ::=
457 -- | others => RATIONAL
458 -- | DISCRETE_CHOICE_LIST => RATIONAL
460 -- RATIONAL ::= [-] NUMERIC_LITERAL [/ NUMERIC_LITERAL]
462 -- Note that when the dimensioned type is an integer type, then any
463 -- dimension value must be an integer literal.
465 procedure Analyze_Aspect_Dimension
470 Def_Id : constant Entity_Id := Defining_Identifier (N);
472 Processed : array (Dimension_Type'Range) of Boolean := (others => False);
473 -- This array is used when processing ranges or Others_Choice as part of
474 -- the dimension aggregate.
476 Dimensions : Dimension_Type := Null_Dimension;
478 procedure Extract_Power
480 Position : Dimension_Position);
481 -- Given an expression with denotes a rational number, read the number
482 -- and associate it with Position in Dimensions.
484 function Position_In_System
486 System : System_Type) return Dimension_Position;
487 -- Given an identifier which denotes a dimension, return the position of
488 -- that dimension within System.
494 procedure Extract_Power
496 Position : Dimension_Position)
501 if Is_Integer_Type (Def_Id) then
503 -- Dimension value must be an integer literal
505 if Nkind (Expr) = N_Integer_Literal then
506 Dimensions (Position) := +Whole (UI_To_Int (Intval (Expr)));
508 Error_Msg_N ("integer literal expected", Expr);
514 Dimensions (Position) := Create_Rational_From (Expr, True);
517 Processed (Position) := True;
520 ------------------------
521 -- Position_In_System --
522 ------------------------
524 function Position_In_System
526 System : System_Type) return Dimension_Position
528 Dimension_Name : constant Name_Id := Chars (Id);
531 for Position in System.Unit_Names'Range loop
532 if Dimension_Name = System.Unit_Names (Position) then
537 return Invalid_Position;
538 end Position_In_System;
545 Num_Choices : Nat := 0;
546 Num_Dimensions : Nat := 0;
547 Others_Seen : Boolean := False;
550 Symbol : String_Id := No_String;
551 Symbol_Expr : Node_Id;
552 System : System_Type;
556 -- Errors_Count is a count of errors detected by the compiler so far
557 -- just before the extraction of symbol, names and values in the
558 -- aggregate (Step 2).
560 -- At the end of the analysis, there is a check to verify that this
561 -- count equals to Serious_Errors_Detected i.e. no erros have been
562 -- encountered during the process. Otherwise the Dimension_Table is
565 -- Start of processing for Analyze_Aspect_Dimension
568 -- STEP 1: Legality of aspect
570 if Nkind (N) /= N_Subtype_Declaration then
571 Error_Msg_NE ("aspect& must apply to subtype declaration", N, Id);
575 Sub_Ind := Subtype_Indication (N);
576 Typ := Etype (Sub_Ind);
577 System := System_Of (Typ);
579 if Nkind (Sub_Ind) = N_Subtype_Indication then
581 ("constraint not allowed with aspect&", Constraint (Sub_Ind), Id);
585 -- The dimension declarations are useless if the parent type does not
586 -- declare a valid system.
588 if not Exists (System) then
590 ("parent type of& lacks dimension system", Sub_Ind, Def_Id);
594 if Nkind (Aggr) /= N_Aggregate then
595 Error_Msg_N ("aggregate expected", Aggr);
599 -- STEP 2: Symbol, Names and values extraction
601 -- Get the number of errors detected by the compiler so far
603 Errors_Count := Serious_Errors_Detected;
605 -- STEP 2a: Symbol extraction
607 -- The first entry in the aggregate may be the symbolic representation
610 -- Positional symbol argument
612 Symbol_Expr := First (Expressions (Aggr));
614 -- Named symbol argument
617 or else not Nkind_In (Symbol_Expr, N_Character_Literal,
620 Symbol_Expr := Empty;
622 -- Component associations present
624 if Present (Component_Associations (Aggr)) then
625 Assoc := First (Component_Associations (Aggr));
626 Choice := First (Choices (Assoc));
628 if No (Next (Choice)) and then Nkind (Choice) = N_Identifier then
630 -- Symbol component association is present
632 if Chars (Choice) = Name_Symbol then
633 Num_Choices := Num_Choices + 1;
634 Symbol_Expr := Expression (Assoc);
636 -- Verify symbol expression is a string or a character
638 if not Nkind_In (Symbol_Expr, N_Character_Literal,
641 Symbol_Expr := Empty;
643 ("symbol expression must be character or string",
647 -- Special error if no Symbol choice but expression is string
650 elsif Nkind_In (Expression (Assoc), N_Character_Literal,
653 Num_Choices := Num_Choices + 1;
655 ("optional component Symbol expected, found&", Choice);
661 -- STEP 2b: Names and values extraction
663 -- Positional elements
665 Expr := First (Expressions (Aggr));
667 -- Skip the symbol expression when present
669 if Present (Symbol_Expr) and then Num_Choices = 0 then
673 Position := Low_Position_Bound;
674 while Present (Expr) loop
675 if Position > High_Position_Bound then
677 ("type& has more dimensions than system allows", Def_Id);
681 Extract_Power (Expr, Position);
683 Position := Position + 1;
684 Num_Dimensions := Num_Dimensions + 1;
691 Assoc := First (Component_Associations (Aggr));
693 -- Skip the symbol association when present
695 if Num_Choices = 1 then
699 while Present (Assoc) loop
700 Expr := Expression (Assoc);
702 Choice := First (Choices (Assoc));
703 while Present (Choice) loop
705 -- Identifier case: NAME => EXPRESSION
707 if Nkind (Choice) = N_Identifier then
708 Position := Position_In_System (Choice, System);
710 if Is_Invalid (Position) then
711 Error_Msg_N ("dimension name& not part of system", Choice);
713 Extract_Power (Expr, Position);
716 -- Range case: NAME .. NAME => EXPRESSION
718 elsif Nkind (Choice) = N_Range then
720 Low : constant Node_Id := Low_Bound (Choice);
721 High : constant Node_Id := High_Bound (Choice);
722 Low_Pos : Dimension_Position;
723 High_Pos : Dimension_Position;
726 if Nkind (Low) /= N_Identifier then
727 Error_Msg_N ("bound must denote a dimension name", Low);
729 elsif Nkind (High) /= N_Identifier then
730 Error_Msg_N ("bound must denote a dimension name", High);
733 Low_Pos := Position_In_System (Low, System);
734 High_Pos := Position_In_System (High, System);
736 if Is_Invalid (Low_Pos) then
737 Error_Msg_N ("dimension name& not part of system",
740 elsif Is_Invalid (High_Pos) then
741 Error_Msg_N ("dimension name& not part of system",
744 elsif Low_Pos > High_Pos then
745 Error_Msg_N ("expected low to high range", Choice);
748 for Position in Low_Pos .. High_Pos loop
749 Extract_Power (Expr, Position);
755 -- Others case: OTHERS => EXPRESSION
757 elsif Nkind (Choice) = N_Others_Choice then
758 if Present (Next (Choice)) or else Present (Prev (Choice)) then
760 ("OTHERS must appear alone in a choice list", Choice);
762 elsif Present (Next (Assoc)) then
764 ("OTHERS must appear last in an aggregate", Choice);
766 elsif Others_Seen then
767 Error_Msg_N ("multiple OTHERS not allowed", Choice);
770 -- Fill the non-processed dimensions with the default value
771 -- supplied by others.
773 for Position in Processed'Range loop
774 if not Processed (Position) then
775 Extract_Power (Expr, Position);
782 -- All other cases are illegal declarations of dimension names
785 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
788 Num_Choices := Num_Choices + 1;
792 Num_Dimensions := Num_Dimensions + 1;
796 -- STEP 3: Consistency of system and dimensions
798 if Present (First (Expressions (Aggr)))
799 and then (First (Expressions (Aggr)) /= Symbol_Expr
800 or else Present (Next (Symbol_Expr)))
801 and then (Num_Choices > 1
802 or else (Num_Choices = 1 and then not Others_Seen))
805 ("named associations cannot follow positional associations", Aggr);
808 if Num_Dimensions > System.Count then
809 Error_Msg_N ("type& has more dimensions than system allows", Def_Id);
811 elsif Num_Dimensions < System.Count and then not Others_Seen then
812 Error_Msg_N ("type& has less dimensions than system allows", Def_Id);
815 -- STEP 4: Dimension symbol extraction
817 if Present (Symbol_Expr) then
818 if Nkind (Symbol_Expr) = N_Character_Literal then
820 Store_String_Char (UI_To_CC (Char_Literal_Value (Symbol_Expr)));
821 Symbol := End_String;
824 Symbol := Strval (Symbol_Expr);
827 if String_Length (Symbol) = 0 then
828 Error_Msg_N ("empty string not allowed here", Symbol_Expr);
832 -- STEP 5: Storage of extracted values
834 -- Check that no errors have been detected during the analysis
836 if Errors_Count = Serious_Errors_Detected then
838 -- Check for useless declaration
840 if Symbol = No_String and then not Exists (Dimensions) then
841 Error_Msg_N ("useless dimension declaration", Aggr);
844 if Symbol /= No_String then
845 Set_Symbol (Def_Id, Symbol);
848 if Exists (Dimensions) then
849 Set_Dimensions (Def_Id, Dimensions);
852 end Analyze_Aspect_Dimension;
854 -------------------------------------
855 -- Analyze_Aspect_Dimension_System --
856 -------------------------------------
858 -- with Dimension_System => (DIMENSION {, DIMENSION});
861 -- [Unit_Name =>] IDENTIFIER,
862 -- [Unit_Symbol =>] SYMBOL,
863 -- [Dim_Symbol =>] SYMBOL)
865 procedure Analyze_Aspect_Dimension_System
870 function Is_Derived_Numeric_Type (N : Node_Id) return Boolean;
871 -- Determine whether type declaration N denotes a numeric derived type
873 -------------------------------
874 -- Is_Derived_Numeric_Type --
875 -------------------------------
877 function Is_Derived_Numeric_Type (N : Node_Id) return Boolean is
880 Nkind (N) = N_Full_Type_Declaration
881 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
882 and then Is_Numeric_Type
883 (Entity (Subtype_Indication (Type_Definition (N))));
884 end Is_Derived_Numeric_Type;
891 Dim_Symbol : Node_Id;
892 Dim_Symbols : Symbol_Array := No_Symbols;
893 Dim_System : System_Type := Null_System;
896 Unit_Names : Name_Array := No_Names;
897 Unit_Symbol : Node_Id;
898 Unit_Symbols : Symbol_Array := No_Symbols;
901 -- Errors_Count is a count of errors detected by the compiler so far
902 -- just before the extraction of names and symbols in the aggregate
905 -- At the end of the analysis, there is a check to verify that this
906 -- count equals Serious_Errors_Detected i.e. no errors have been
907 -- encountered during the process. Otherwise the System_Table is
910 -- Start of processing for Analyze_Aspect_Dimension_System
913 -- STEP 1: Legality of aspect
915 if not Is_Derived_Numeric_Type (N) then
917 ("aspect& must apply to numeric derived type declaration", N, Id);
921 if Nkind (Aggr) /= N_Aggregate then
922 Error_Msg_N ("aggregate expected", Aggr);
926 -- STEP 2: Structural verification of the dimension aggregate
928 if Present (Component_Associations (Aggr)) then
929 Error_Msg_N ("expected positional aggregate", Aggr);
933 -- STEP 3: Name and Symbol extraction
935 Dim_Aggr := First (Expressions (Aggr));
936 Errors_Count := Serious_Errors_Detected;
937 while Present (Dim_Aggr) loop
938 Position := Position + 1;
940 if Position > High_Position_Bound then
941 Error_Msg_N ("too many dimensions in system", Aggr);
945 if Nkind (Dim_Aggr) /= N_Aggregate then
946 Error_Msg_N ("aggregate expected", Dim_Aggr);
949 if Present (Component_Associations (Dim_Aggr))
950 and then Present (Expressions (Dim_Aggr))
953 ("mixed positional/named aggregate not allowed here",
956 -- Verify each dimension aggregate has three arguments
958 elsif List_Length (Component_Associations (Dim_Aggr)) /= 3
959 and then List_Length (Expressions (Dim_Aggr)) /= 3
962 ("three components expected in aggregate", Dim_Aggr);
965 -- Named dimension aggregate
967 if Present (Component_Associations (Dim_Aggr)) then
969 -- Check first argument denotes the unit name
971 Assoc := First (Component_Associations (Dim_Aggr));
972 Choice := First (Choices (Assoc));
973 Unit_Name := Expression (Assoc);
975 if Present (Next (Choice))
976 or else Nkind (Choice) /= N_Identifier
978 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
980 elsif Chars (Choice) /= Name_Unit_Name then
981 Error_Msg_N ("expected Unit_Name, found&", Choice);
984 -- Check the second argument denotes the unit symbol
987 Choice := First (Choices (Assoc));
988 Unit_Symbol := Expression (Assoc);
990 if Present (Next (Choice))
991 or else Nkind (Choice) /= N_Identifier
993 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
995 elsif Chars (Choice) /= Name_Unit_Symbol then
996 Error_Msg_N ("expected Unit_Symbol, found&", Choice);
999 -- Check the third argument denotes the dimension symbol
1002 Choice := First (Choices (Assoc));
1003 Dim_Symbol := Expression (Assoc);
1005 if Present (Next (Choice))
1006 or else Nkind (Choice) /= N_Identifier
1008 Error_Msg_NE ("wrong syntax for aspect&", Choice, Id);
1009 elsif Chars (Choice) /= Name_Dim_Symbol then
1010 Error_Msg_N ("expected Dim_Symbol, found&", Choice);
1013 -- Positional dimension aggregate
1016 Unit_Name := First (Expressions (Dim_Aggr));
1017 Unit_Symbol := Next (Unit_Name);
1018 Dim_Symbol := Next (Unit_Symbol);
1021 -- Check the first argument for each dimension aggregate is
1024 if Nkind (Unit_Name) = N_Identifier then
1025 Unit_Names (Position) := Chars (Unit_Name);
1027 Error_Msg_N ("expected unit name", Unit_Name);
1030 -- Check the second argument for each dimension aggregate is
1031 -- a string or a character.
1033 if not Nkind_In (Unit_Symbol, N_String_Literal,
1034 N_Character_Literal)
1037 ("expected unit symbol (string or character)",
1043 if Nkind (Unit_Symbol) = N_String_Literal then
1044 Unit_Symbols (Position) := Strval (Unit_Symbol);
1051 (UI_To_CC (Char_Literal_Value (Unit_Symbol)));
1052 Unit_Symbols (Position) := End_String;
1055 -- Verify that the string is not empty
1057 if String_Length (Unit_Symbols (Position)) = 0 then
1059 ("empty string not allowed here", Unit_Symbol);
1063 -- Check the third argument for each dimension aggregate is
1064 -- a string or a character.
1066 if not Nkind_In (Dim_Symbol, N_String_Literal,
1067 N_Character_Literal)
1070 ("expected dimension symbol (string or character)",
1076 if Nkind (Dim_Symbol) = N_String_Literal then
1077 Dim_Symbols (Position) := Strval (Dim_Symbol);
1084 (UI_To_CC (Char_Literal_Value (Dim_Symbol)));
1085 Dim_Symbols (Position) := End_String;
1088 -- Verify that the string is not empty
1090 if String_Length (Dim_Symbols (Position)) = 0 then
1091 Error_Msg_N ("empty string not allowed here", Dim_Symbol);
1100 -- STEP 4: Storage of extracted values
1102 -- Check that no errors have been detected during the analysis
1104 if Errors_Count = Serious_Errors_Detected then
1105 Dim_System.Type_Decl := N;
1106 Dim_System.Unit_Names := Unit_Names;
1107 Dim_System.Unit_Symbols := Unit_Symbols;
1108 Dim_System.Dim_Symbols := Dim_Symbols;
1109 Dim_System.Count := Position;
1110 System_Table.Append (Dim_System);
1112 end Analyze_Aspect_Dimension_System;
1114 -----------------------
1115 -- Analyze_Dimension --
1116 -----------------------
1118 -- This dispatch routine propagates dimensions for each node
1120 procedure Analyze_Dimension (N : Node_Id) is
1122 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1123 -- dimensions for nodes that don't come from source, except for subtype
1124 -- declarations where the dimensions are inherited from the base type,
1125 -- for explicit dereferences generated when expanding iterators, and
1126 -- for object declarations generated for inlining.
1128 if Ada_Version < Ada_2012 then
1131 elsif not Comes_From_Source (N) then
1132 if Nkind_In (N, N_Explicit_Dereference,
1134 N_Object_Declaration,
1135 N_Subtype_Declaration)
1144 when N_Assignment_Statement =>
1145 Analyze_Dimension_Assignment_Statement (N);
1148 Analyze_Dimension_Binary_Op (N);
1150 when N_Component_Declaration =>
1151 Analyze_Dimension_Component_Declaration (N);
1153 when N_Extended_Return_Statement =>
1154 Analyze_Dimension_Extended_Return_Statement (N);
1156 when N_Attribute_Reference
1158 | N_Explicit_Dereference
1160 | N_Indexed_Component
1161 | N_Qualified_Expression
1162 | N_Selected_Component
1165 | N_Unchecked_Type_Conversion
1167 Analyze_Dimension_Has_Etype (N);
1169 -- In the presence of a repaired syntax error, an identifier
1170 -- may be introduced without a usable type.
1172 when N_Identifier =>
1173 if Present (Etype (N)) then
1174 Analyze_Dimension_Has_Etype (N);
1177 when N_Number_Declaration =>
1178 Analyze_Dimension_Number_Declaration (N);
1180 when N_Object_Declaration =>
1181 Analyze_Dimension_Object_Declaration (N);
1183 when N_Object_Renaming_Declaration =>
1184 Analyze_Dimension_Object_Renaming_Declaration (N);
1186 when N_Simple_Return_Statement =>
1187 if not Comes_From_Extended_Return_Statement (N) then
1188 Analyze_Dimension_Simple_Return_Statement (N);
1191 when N_Subtype_Declaration =>
1192 Analyze_Dimension_Subtype_Declaration (N);
1195 Analyze_Dimension_Unary_Op (N);
1200 end Analyze_Dimension;
1202 ---------------------------------------
1203 -- Analyze_Dimension_Array_Aggregate --
1204 ---------------------------------------
1206 procedure Analyze_Dimension_Array_Aggregate
1208 Comp_Typ : Entity_Id)
1210 Comp_Ass : constant List_Id := Component_Associations (N);
1211 Dims_Of_Comp_Typ : constant Dimension_Type := Dimensions_Of (Comp_Typ);
1212 Exps : constant List_Id := Expressions (N);
1217 Error_Detected : Boolean := False;
1218 -- This flag is used in order to indicate if an error has been detected
1219 -- so far by the compiler in this routine.
1222 -- Aspect is an Ada 2012 feature. Nothing to do here if the component
1223 -- base type is not a dimensioned type.
1225 -- Note that here the original node must come from source since the
1226 -- original array aggregate may not have been entirely decorated.
1228 if Ada_Version < Ada_2012
1229 or else not Comes_From_Source (Original_Node (N))
1230 or else not Has_Dimension_System (Base_Type (Comp_Typ))
1235 -- Check whether there is any positional component association
1237 if Is_Empty_List (Exps) then
1238 Comp := First (Comp_Ass);
1240 Comp := First (Exps);
1243 while Present (Comp) loop
1245 -- Get the expression from the component
1247 if Nkind (Comp) = N_Component_Association then
1248 Expr := Expression (Comp);
1253 -- Issue an error if the dimensions of the component type and the
1254 -- dimensions of the component mismatch.
1256 -- Note that we must ensure the expression has been fully analyzed
1257 -- since it may not be decorated at this point. We also don't want to
1258 -- issue the same error message multiple times on the same expression
1259 -- (may happen when an aggregate is converted into a positional
1260 -- aggregate). We also must verify that this is a scalar component,
1261 -- and not a subaggregate of a multidimensional aggregate.
1263 if Comes_From_Source (Original_Node (Expr))
1264 and then Present (Etype (Expr))
1265 and then Is_Numeric_Type (Etype (Expr))
1266 and then Dimensions_Of (Expr) /= Dims_Of_Comp_Typ
1267 and then Sloc (Comp) /= Sloc (Prev (Comp))
1269 -- Check if an error has already been encountered so far
1271 if not Error_Detected then
1272 Error_Msg_N ("dimensions mismatch in array aggregate", N);
1273 Error_Detected := True;
1277 ("\expected dimension " & Dimensions_Msg_Of (Comp_Typ)
1278 & ", found " & Dimensions_Msg_Of (Expr), Expr);
1281 -- Look at the named components right after the positional components
1283 if not Present (Next (Comp))
1284 and then List_Containing (Comp) = Exps
1286 Comp := First (Comp_Ass);
1291 end Analyze_Dimension_Array_Aggregate;
1293 --------------------------------------------
1294 -- Analyze_Dimension_Assignment_Statement --
1295 --------------------------------------------
1297 procedure Analyze_Dimension_Assignment_Statement (N : Node_Id) is
1298 Lhs : constant Node_Id := Name (N);
1299 Dims_Of_Lhs : constant Dimension_Type := Dimensions_Of (Lhs);
1300 Rhs : constant Node_Id := Expression (N);
1301 Dims_Of_Rhs : constant Dimension_Type := Dimensions_Of (Rhs);
1303 procedure Error_Dim_Msg_For_Assignment_Statement
1307 -- Error using Error_Msg_N at node N. Output the dimensions of left
1308 -- and right hand sides.
1310 --------------------------------------------
1311 -- Error_Dim_Msg_For_Assignment_Statement --
1312 --------------------------------------------
1314 procedure Error_Dim_Msg_For_Assignment_Statement
1320 Error_Msg_N ("dimensions mismatch in assignment", N);
1321 Error_Msg_N ("\left-hand side " & Dimensions_Msg_Of (Lhs, True), N);
1322 Error_Msg_N ("\right-hand side " & Dimensions_Msg_Of (Rhs, True), N);
1323 end Error_Dim_Msg_For_Assignment_Statement;
1325 -- Start of processing for Analyze_Dimension_Assignment
1328 if Dims_Of_Lhs /= Dims_Of_Rhs then
1329 Error_Dim_Msg_For_Assignment_Statement (N, Lhs, Rhs);
1331 end Analyze_Dimension_Assignment_Statement;
1333 ---------------------------------
1334 -- Analyze_Dimension_Binary_Op --
1335 ---------------------------------
1337 -- Check and propagate the dimensions for binary operators
1338 -- Note that when the dimensions mismatch, no dimension is propagated to N.
1340 procedure Analyze_Dimension_Binary_Op (N : Node_Id) is
1341 N_Kind : constant Node_Kind := Nkind (N);
1343 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type;
1344 -- If the operand is a numeric literal that comes from a declared
1345 -- constant, use the dimensions of the constant which were computed
1346 -- from the expression of the constant declaration.
1348 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id);
1349 -- Error using Error_Msg_NE and Error_Msg_N at node N. Output the
1350 -- dimensions of both operands.
1352 ---------------------------
1353 -- Dimensions_Of_Operand --
1354 ---------------------------
1356 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type is
1358 if Nkind (N) = N_Real_Literal
1359 and then Present (Original_Entity (N))
1361 return Dimensions_Of (Original_Entity (N));
1363 return Dimensions_Of (N);
1365 end Dimensions_Of_Operand;
1367 ---------------------------------
1368 -- Error_Dim_Msg_For_Binary_Op --
1369 ---------------------------------
1371 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id) is
1374 ("both operands for operation& must have same dimensions",
1376 Error_Msg_N ("\left operand " & Dimensions_Msg_Of (L, True), N);
1377 Error_Msg_N ("\right operand " & Dimensions_Msg_Of (R, True), N);
1378 end Error_Dim_Msg_For_Binary_Op;
1380 -- Start of processing for Analyze_Dimension_Binary_Op
1383 if Nkind_In (N_Kind, N_Op_Add, N_Op_Expon, N_Op_Subtract)
1384 or else N_Kind in N_Multiplying_Operator
1385 or else N_Kind in N_Op_Compare
1388 L : constant Node_Id := Left_Opnd (N);
1389 Dims_Of_L : constant Dimension_Type :=
1390 Dimensions_Of_Operand (L);
1391 L_Has_Dimensions : constant Boolean := Exists (Dims_Of_L);
1392 R : constant Node_Id := Right_Opnd (N);
1393 Dims_Of_R : constant Dimension_Type :=
1394 Dimensions_Of_Operand (R);
1395 R_Has_Dimensions : constant Boolean := Exists (Dims_Of_R);
1396 Dims_Of_N : Dimension_Type := Null_Dimension;
1399 -- N_Op_Add, N_Op_Mod, N_Op_Rem or N_Op_Subtract case
1401 if Nkind_In (N, N_Op_Add, N_Op_Mod, N_Op_Rem, N_Op_Subtract) then
1403 -- Check both operands have same dimension
1405 if Dims_Of_L /= Dims_Of_R then
1406 Error_Dim_Msg_For_Binary_Op (N, L, R);
1408 -- Check both operands are not dimensionless
1410 if Exists (Dims_Of_L) then
1411 Set_Dimensions (N, Dims_Of_L);
1415 -- N_Op_Multiply or N_Op_Divide case
1417 elsif Nkind_In (N_Kind, N_Op_Multiply, N_Op_Divide) then
1419 -- Check at least one operand is not dimensionless
1421 if L_Has_Dimensions or R_Has_Dimensions then
1423 -- Multiplication case
1425 -- Get both operands dimensions and add them
1427 if N_Kind = N_Op_Multiply then
1428 for Position in Dimension_Type'Range loop
1429 Dims_Of_N (Position) :=
1430 Dims_Of_L (Position) + Dims_Of_R (Position);
1435 -- Get both operands dimensions and subtract them
1438 for Position in Dimension_Type'Range loop
1439 Dims_Of_N (Position) :=
1440 Dims_Of_L (Position) - Dims_Of_R (Position);
1444 if Exists (Dims_Of_N) then
1445 Set_Dimensions (N, Dims_Of_N);
1449 -- Exponentiation case
1451 -- Note: a rational exponent is allowed for dimensioned operand
1453 elsif N_Kind = N_Op_Expon then
1455 -- Check the left operand is not dimensionless. Note that the
1456 -- value of the exponent must be known compile time. Otherwise,
1457 -- the exponentiation evaluation will return an error message.
1459 if L_Has_Dimensions then
1460 if not Compile_Time_Known_Value (R) then
1462 ("exponent of dimensioned operand must be "
1463 & "known at compile time", N);
1467 Exponent_Value : Rational := Zero;
1470 -- Real operand case
1472 if Is_Real_Type (Etype (L)) then
1474 -- Define the exponent as a Rational number
1476 Exponent_Value := Create_Rational_From (R, False);
1478 -- Verify that the exponent cannot be interpreted
1479 -- as a rational, otherwise interpret the exponent
1482 if Exponent_Value = No_Rational then
1484 +Whole (UI_To_Int (Expr_Value (R)));
1487 -- Integer operand case.
1489 -- For integer operand, the exponent cannot be
1490 -- interpreted as a rational.
1493 Exponent_Value := +Whole (UI_To_Int (Expr_Value (R)));
1496 for Position in Dimension_Type'Range loop
1497 Dims_Of_N (Position) :=
1498 Dims_Of_L (Position) * Exponent_Value;
1501 if Exists (Dims_Of_N) then
1502 Set_Dimensions (N, Dims_Of_N);
1509 -- For relational operations, only dimension checking is
1510 -- performed (no propagation). If one operand is the result
1511 -- of constant folding the dimensions may have been lost
1512 -- in a tree copy, so assume that pre-analysis has verified
1513 -- that dimensions are correct.
1515 elsif N_Kind in N_Op_Compare then
1516 if (L_Has_Dimensions or R_Has_Dimensions)
1517 and then Dims_Of_L /= Dims_Of_R
1519 if Nkind (L) = N_Real_Literal
1520 and then not (Comes_From_Source (L))
1521 and then Expander_Active
1525 elsif Nkind (R) = N_Real_Literal
1526 and then not (Comes_From_Source (R))
1527 and then Expander_Active
1532 Error_Dim_Msg_For_Binary_Op (N, L, R);
1537 -- If expander is active, remove dimension information from each
1538 -- operand, as only dimensions of result are relevant.
1540 if Expander_Active then
1541 Remove_Dimensions (L);
1542 Remove_Dimensions (R);
1546 end Analyze_Dimension_Binary_Op;
1548 ----------------------------
1549 -- Analyze_Dimension_Call --
1550 ----------------------------
1552 procedure Analyze_Dimension_Call (N : Node_Id; Nam : Entity_Id) is
1553 Actuals : constant List_Id := Parameter_Associations (N);
1555 Dims_Of_Formal : Dimension_Type;
1557 Formal_Typ : Entity_Id;
1559 Error_Detected : Boolean := False;
1560 -- This flag is used in order to indicate if an error has been detected
1561 -- so far by the compiler in this routine.
1564 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1565 -- dimensions for calls that don't come from source, or those that may
1566 -- have semantic errors.
1568 if Ada_Version < Ada_2012
1569 or else not Comes_From_Source (N)
1570 or else Error_Posted (N)
1575 -- Check the dimensions of the actuals, if any
1577 if not Is_Empty_List (Actuals) then
1579 -- Special processing for elementary functions
1581 -- For Sqrt call, the resulting dimensions equal to half the
1582 -- dimensions of the actual. For all other elementary calls, this
1583 -- routine check that every actual is dimensionless.
1585 if Nkind (N) = N_Function_Call then
1586 Elementary_Function_Calls : declare
1587 Dims_Of_Call : Dimension_Type;
1588 Ent : Entity_Id := Nam;
1590 function Is_Elementary_Function_Entity
1591 (Sub_Id : Entity_Id) return Boolean;
1592 -- Given Sub_Id, the original subprogram entity, return True
1593 -- if call is to an elementary function (see Ada.Numerics.
1594 -- Generic_Elementary_Functions).
1596 -----------------------------------
1597 -- Is_Elementary_Function_Entity --
1598 -----------------------------------
1600 function Is_Elementary_Function_Entity
1601 (Sub_Id : Entity_Id) return Boolean
1603 Loc : constant Source_Ptr := Sloc (Sub_Id);
1606 -- Is entity in Ada.Numerics.Generic_Elementary_Functions?
1612 (Cunit_Entity (Get_Source_Unit (Loc)),
1613 Ada_Numerics_Generic_Elementary_Functions);
1614 end Is_Elementary_Function_Entity;
1616 -- Start of processing for Elementary_Function_Calls
1619 -- Get original subprogram entity following the renaming chain
1621 if Present (Alias (Ent)) then
1625 -- Check the call is an Elementary function call
1627 if Is_Elementary_Function_Entity (Ent) then
1629 -- Sqrt function call case
1631 if Chars (Ent) = Name_Sqrt then
1632 Dims_Of_Call := Dimensions_Of (First_Actual (N));
1634 -- Evaluates the resulting dimensions (i.e. half the
1635 -- dimensions of the actual).
1637 if Exists (Dims_Of_Call) then
1638 for Position in Dims_Of_Call'Range loop
1639 Dims_Of_Call (Position) :=
1640 Dims_Of_Call (Position) *
1641 Rational'(Numerator
=> 1, Denominator
=> 2);
1644 Set_Dimensions
(N
, Dims_Of_Call
);
1647 -- All other elementary functions case. Note that every
1648 -- actual here should be dimensionless.
1651 Actual
:= First_Actual
(N
);
1652 while Present
(Actual
) loop
1653 if Exists
(Dimensions_Of
(Actual
)) then
1655 -- Check if error has already been encountered
1657 if not Error_Detected
then
1659 ("dimensions mismatch in call of&",
1661 Error_Detected
:= True;
1665 ("\expected dimension '['], found "
1666 & Dimensions_Msg_Of
(Actual
), Actual
);
1669 Next_Actual
(Actual
);
1673 -- Nothing more to do for elementary functions
1677 end Elementary_Function_Calls
;
1680 -- General case. Check, for each parameter, the dimensions of the
1681 -- actual and its corresponding formal match. Otherwise, complain.
1683 Actual
:= First_Actual
(N
);
1684 Formal
:= First_Formal
(Nam
);
1685 while Present
(Formal
) loop
1687 -- A missing corresponding actual indicates that the analysis of
1688 -- the call was aborted due to a previous error.
1691 Check_Error_Detected
;
1695 Formal_Typ
:= Etype
(Formal
);
1696 Dims_Of_Formal
:= Dimensions_Of
(Formal_Typ
);
1698 -- If the formal is not dimensionless, check dimensions of formal
1699 -- and actual match. Otherwise, complain.
1701 if Exists
(Dims_Of_Formal
)
1702 and then Dimensions_Of
(Actual
) /= Dims_Of_Formal
1704 -- Check if an error has already been encountered so far
1706 if not Error_Detected
then
1707 Error_Msg_NE
("dimensions mismatch in& call", N
, Name
(N
));
1708 Error_Detected
:= True;
1712 ("\expected dimension " & Dimensions_Msg_Of
(Formal_Typ
)
1713 & ", found " & Dimensions_Msg_Of
(Actual
), Actual
);
1716 Next_Actual
(Actual
);
1717 Next_Formal
(Formal
);
1721 -- For function calls, propagate the dimensions from the returned type
1723 if Nkind
(N
) = N_Function_Call
then
1724 Analyze_Dimension_Has_Etype
(N
);
1726 end Analyze_Dimension_Call
;
1728 ---------------------------------------------
1729 -- Analyze_Dimension_Component_Declaration --
1730 ---------------------------------------------
1732 procedure Analyze_Dimension_Component_Declaration
(N
: Node_Id
) is
1733 Expr
: constant Node_Id
:= Expression
(N
);
1734 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1735 Etyp
: constant Entity_Id
:= Etype
(Id
);
1736 Dims_Of_Etyp
: constant Dimension_Type
:= Dimensions_Of
(Etyp
);
1737 Dims_Of_Expr
: Dimension_Type
;
1739 procedure Error_Dim_Msg_For_Component_Declaration
1743 -- Error using Error_Msg_N at node N. Output the dimensions of the
1744 -- type Etyp and the expression Expr of N.
1746 ---------------------------------------------
1747 -- Error_Dim_Msg_For_Component_Declaration --
1748 ---------------------------------------------
1750 procedure Error_Dim_Msg_For_Component_Declaration
1755 Error_Msg_N
("dimensions mismatch in component declaration", N
);
1757 ("\expected dimension " & Dimensions_Msg_Of
(Etyp
) & ", found "
1758 & Dimensions_Msg_Of
(Expr
), Expr
);
1759 end Error_Dim_Msg_For_Component_Declaration
;
1761 -- Start of processing for Analyze_Dimension_Component_Declaration
1764 -- Expression is present
1766 if Present
(Expr
) then
1767 Dims_Of_Expr
:= Dimensions_Of
(Expr
);
1769 -- Check dimensions match
1771 if Dims_Of_Etyp
/= Dims_Of_Expr
then
1773 -- Numeric literal case. Issue a warning if the object type is not
1774 -- dimensionless to indicate the literal is treated as if its
1775 -- dimension matches the type dimension.
1777 if Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
1780 Dim_Warning_For_Numeric_Literal
(Expr
, Etyp
);
1782 -- Issue a dimension mismatch error for all other cases
1785 Error_Dim_Msg_For_Component_Declaration
(N
, Etyp
, Expr
);
1789 end Analyze_Dimension_Component_Declaration
;
1791 -------------------------------------------------
1792 -- Analyze_Dimension_Extended_Return_Statement --
1793 -------------------------------------------------
1795 procedure Analyze_Dimension_Extended_Return_Statement
(N
: Node_Id
) is
1796 Return_Ent
: constant Entity_Id
:= Return_Statement_Entity
(N
);
1797 Return_Etyp
: constant Entity_Id
:=
1798 Etype
(Return_Applies_To
(Return_Ent
));
1799 Return_Obj_Decls
: constant List_Id
:= Return_Object_Declarations
(N
);
1800 Return_Obj_Decl
: Node_Id
;
1801 Return_Obj_Id
: Entity_Id
;
1802 Return_Obj_Typ
: Entity_Id
;
1804 procedure Error_Dim_Msg_For_Extended_Return_Statement
1806 Return_Etyp
: Entity_Id
;
1807 Return_Obj_Typ
: Entity_Id
);
1808 -- Error using Error_Msg_N at node N. Output dimensions of the returned
1809 -- type Return_Etyp and the returned object type Return_Obj_Typ of N.
1811 -------------------------------------------------
1812 -- Error_Dim_Msg_For_Extended_Return_Statement --
1813 -------------------------------------------------
1815 procedure Error_Dim_Msg_For_Extended_Return_Statement
1817 Return_Etyp
: Entity_Id
;
1818 Return_Obj_Typ
: Entity_Id
)
1821 Error_Msg_N
("dimensions mismatch in extended return statement", N
);
1823 ("\expected dimension " & Dimensions_Msg_Of
(Return_Etyp
)
1824 & ", found " & Dimensions_Msg_Of
(Return_Obj_Typ
), N
);
1825 end Error_Dim_Msg_For_Extended_Return_Statement
;
1827 -- Start of processing for Analyze_Dimension_Extended_Return_Statement
1830 if Present
(Return_Obj_Decls
) then
1831 Return_Obj_Decl
:= First
(Return_Obj_Decls
);
1832 while Present
(Return_Obj_Decl
) loop
1833 if Nkind
(Return_Obj_Decl
) = N_Object_Declaration
then
1834 Return_Obj_Id
:= Defining_Identifier
(Return_Obj_Decl
);
1836 if Is_Return_Object
(Return_Obj_Id
) then
1837 Return_Obj_Typ
:= Etype
(Return_Obj_Id
);
1839 -- Issue an error message if dimensions mismatch
1841 if Dimensions_Of
(Return_Etyp
) /=
1842 Dimensions_Of
(Return_Obj_Typ
)
1844 Error_Dim_Msg_For_Extended_Return_Statement
1845 (N
, Return_Etyp
, Return_Obj_Typ
);
1851 Next
(Return_Obj_Decl
);
1854 end Analyze_Dimension_Extended_Return_Statement
;
1856 -----------------------------------------------------
1857 -- Analyze_Dimension_Extension_Or_Record_Aggregate --
1858 -----------------------------------------------------
1860 procedure Analyze_Dimension_Extension_Or_Record_Aggregate
(N
: Node_Id
) is
1862 Comp_Id
: Entity_Id
;
1863 Comp_Typ
: Entity_Id
;
1866 Error_Detected
: Boolean := False;
1867 -- This flag is used in order to indicate if an error has been detected
1868 -- so far by the compiler in this routine.
1871 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1872 -- dimensions for aggregates that don't come from source, or if we are
1873 -- within an initialization procedure, whose expressions have been
1874 -- checked at the point of record declaration.
1876 if Ada_Version
< Ada_2012
1877 or else not Comes_From_Source
(N
)
1878 or else Inside_Init_Proc
1883 Comp
:= First
(Component_Associations
(N
));
1884 while Present
(Comp
) loop
1885 Comp_Id
:= Entity
(First
(Choices
(Comp
)));
1886 Comp_Typ
:= Etype
(Comp_Id
);
1888 -- Check the component type is either a dimensioned type or a
1889 -- dimensioned subtype.
1891 if Has_Dimension_System
(Base_Type
(Comp_Typ
)) then
1892 Expr
:= Expression
(Comp
);
1894 -- A box-initialized component needs no checking.
1896 if No
(Expr
) and then Box_Present
(Comp
) then
1899 -- Issue an error if the dimensions of the component type and the
1900 -- dimensions of the component mismatch.
1902 elsif Dimensions_Of
(Expr
) /= Dimensions_Of
(Comp_Typ
) then
1904 -- Check if an error has already been encountered so far
1906 if not Error_Detected
then
1908 -- Extension aggregate case
1910 if Nkind
(N
) = N_Extension_Aggregate
then
1912 ("dimensions mismatch in extension aggregate", N
);
1914 -- Record aggregate case
1918 ("dimensions mismatch in record aggregate", N
);
1921 Error_Detected
:= True;
1925 ("\expected dimension " & Dimensions_Msg_Of
(Comp_Typ
)
1926 & ", found " & Dimensions_Msg_Of
(Expr
), Comp
);
1932 end Analyze_Dimension_Extension_Or_Record_Aggregate
;
1934 -------------------------------
1935 -- Analyze_Dimension_Formals --
1936 -------------------------------
1938 procedure Analyze_Dimension_Formals
(N
: Node_Id
; Formals
: List_Id
) is
1939 Dims_Of_Typ
: Dimension_Type
;
1944 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1945 -- dimensions for sub specs that don't come from source.
1947 if Ada_Version
< Ada_2012
or else not Comes_From_Source
(N
) then
1951 Formal
:= First
(Formals
);
1952 while Present
(Formal
) loop
1953 Typ
:= Parameter_Type
(Formal
);
1954 Dims_Of_Typ
:= Dimensions_Of
(Typ
);
1956 if Exists
(Dims_Of_Typ
) then
1958 Expr
: constant Node_Id
:= Expression
(Formal
);
1961 -- Issue a warning if Expr is a numeric literal and if its
1962 -- dimensions differ with the dimensions of the formal type.
1965 and then Dims_Of_Typ
/= Dimensions_Of
(Expr
)
1966 and then Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
1969 Dim_Warning_For_Numeric_Literal
(Expr
, Etype
(Typ
));
1976 end Analyze_Dimension_Formals
;
1978 ---------------------------------
1979 -- Analyze_Dimension_Has_Etype --
1980 ---------------------------------
1982 procedure Analyze_Dimension_Has_Etype
(N
: Node_Id
) is
1983 Etyp
: constant Entity_Id
:= Etype
(N
);
1984 Dims_Of_Etyp
: Dimension_Type
:= Dimensions_Of
(Etyp
);
1987 -- General case. Propagation of the dimensions from the type
1989 if Exists
(Dims_Of_Etyp
) then
1990 Set_Dimensions
(N
, Dims_Of_Etyp
);
1992 -- Identifier case. Propagate the dimensions from the entity for
1993 -- identifier whose entity is a non-dimensionless constant.
1995 elsif Nkind
(N
) = N_Identifier
then
1996 Analyze_Dimension_Identifier
: declare
1997 Id
: constant Entity_Id
:= Entity
(N
);
2000 -- If Id is missing, abnormal tree, assume previous error
2003 Check_Error_Detected
;
2006 elsif Ekind_In
(Id
, E_Constant
, E_Named_Real
)
2007 and then Exists
(Dimensions_Of
(Id
))
2009 Set_Dimensions
(N
, Dimensions_Of
(Id
));
2011 end Analyze_Dimension_Identifier
;
2013 -- Attribute reference case. Propagate the dimensions from the prefix.
2015 elsif Nkind
(N
) = N_Attribute_Reference
2016 and then Has_Dimension_System
(Base_Type
(Etyp
))
2018 Dims_Of_Etyp
:= Dimensions_Of
(Prefix
(N
));
2020 -- Check the prefix is not dimensionless
2022 if Exists
(Dims_Of_Etyp
) then
2023 Set_Dimensions
(N
, Dims_Of_Etyp
);
2027 -- Remove dimensions from inner expressions, to prevent dimensions
2028 -- table from growing uselessly.
2031 when N_Attribute_Reference
2032 | N_Indexed_Component
2035 Exprs
: constant List_Id
:= Expressions
(N
);
2039 if Present
(Exprs
) then
2040 Expr
:= First
(Exprs
);
2041 while Present
(Expr
) loop
2042 Remove_Dimensions
(Expr
);
2048 when N_Qualified_Expression
2050 | N_Unchecked_Type_Conversion
2052 Remove_Dimensions
(Expression
(N
));
2054 when N_Selected_Component
=>
2055 Remove_Dimensions
(Selector_Name
(N
));
2060 end Analyze_Dimension_Has_Etype
;
2062 ------------------------------------------
2063 -- Analyze_Dimension_Number_Declaration --
2064 ------------------------------------------
2066 procedure Analyze_Dimension_Number_Declaration
(N
: Node_Id
) is
2067 Expr
: constant Node_Id
:= Expression
(N
);
2068 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2069 Dim_Of_Expr
: constant Dimension_Type
:= Dimensions_Of
(Expr
);
2072 if Exists
(Dim_Of_Expr
) then
2073 Set_Dimensions
(Id
, Dim_Of_Expr
);
2074 Set_Etype
(Id
, Etype
(Expr
));
2076 end Analyze_Dimension_Number_Declaration
;
2078 ------------------------------------------
2079 -- Analyze_Dimension_Object_Declaration --
2080 ------------------------------------------
2082 procedure Analyze_Dimension_Object_Declaration
(N
: Node_Id
) is
2083 Expr
: constant Node_Id
:= Expression
(N
);
2084 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2085 Etyp
: constant Entity_Id
:= Etype
(Id
);
2086 Dim_Of_Etyp
: constant Dimension_Type
:= Dimensions_Of
(Etyp
);
2087 Dim_Of_Expr
: Dimension_Type
;
2089 procedure Error_Dim_Msg_For_Object_Declaration
2093 -- Error using Error_Msg_N at node N. Output the dimensions of the
2094 -- type Etyp and of the expression Expr.
2096 ------------------------------------------
2097 -- Error_Dim_Msg_For_Object_Declaration --
2098 ------------------------------------------
2100 procedure Error_Dim_Msg_For_Object_Declaration
2105 Error_Msg_N
("dimensions mismatch in object declaration", N
);
2107 ("\expected dimension " & Dimensions_Msg_Of
(Etyp
) & ", found "
2108 & Dimensions_Msg_Of
(Expr
), Expr
);
2109 end Error_Dim_Msg_For_Object_Declaration
;
2111 -- Start of processing for Analyze_Dimension_Object_Declaration
2114 -- Expression is present
2116 if Present
(Expr
) then
2117 Dim_Of_Expr
:= Dimensions_Of
(Expr
);
2119 -- Check dimensions match
2121 if Dim_Of_Expr
/= Dim_Of_Etyp
then
2123 -- Numeric literal case. Issue a warning if the object type is not
2124 -- dimensionless to indicate the literal is treated as if its
2125 -- dimension matches the type dimension.
2127 if Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
2130 Dim_Warning_For_Numeric_Literal
(Expr
, Etyp
);
2132 -- Case of object is a constant whose type is a dimensioned type
2134 elsif Constant_Present
(N
) and then not Exists
(Dim_Of_Etyp
) then
2136 -- Propagate dimension from expression to object entity
2138 Set_Dimensions
(Id
, Dim_Of_Expr
);
2140 -- For all other cases, issue an error message
2143 Error_Dim_Msg_For_Object_Declaration
(N
, Etyp
, Expr
);
2147 -- Remove dimensions in expression after checking consistency
2150 Remove_Dimensions
(Expr
);
2152 end Analyze_Dimension_Object_Declaration
;
2154 ---------------------------------------------------
2155 -- Analyze_Dimension_Object_Renaming_Declaration --
2156 ---------------------------------------------------
2158 procedure Analyze_Dimension_Object_Renaming_Declaration
(N
: Node_Id
) is
2159 Renamed_Name
: constant Node_Id
:= Name
(N
);
2160 Sub_Mark
: constant Node_Id
:= Subtype_Mark
(N
);
2162 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2165 Renamed_Name
: Node_Id
);
2166 -- Error using Error_Msg_N at node N. Output the dimensions of
2167 -- Sub_Mark and of Renamed_Name.
2169 ---------------------------------------------------
2170 -- Error_Dim_Msg_For_Object_Renaming_Declaration --
2171 ---------------------------------------------------
2173 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2176 Renamed_Name
: Node_Id
) is
2178 Error_Msg_N
("dimensions mismatch in object renaming declaration", N
);
2180 ("\expected dimension " & Dimensions_Msg_Of
(Sub_Mark
) & ", found "
2181 & Dimensions_Msg_Of
(Renamed_Name
), Renamed_Name
);
2182 end Error_Dim_Msg_For_Object_Renaming_Declaration
;
2184 -- Start of processing for Analyze_Dimension_Object_Renaming_Declaration
2187 if Dimensions_Of
(Renamed_Name
) /= Dimensions_Of
(Sub_Mark
) then
2188 Error_Dim_Msg_For_Object_Renaming_Declaration
2189 (N
, Sub_Mark
, Renamed_Name
);
2191 end Analyze_Dimension_Object_Renaming_Declaration
;
2193 -----------------------------------------------
2194 -- Analyze_Dimension_Simple_Return_Statement --
2195 -----------------------------------------------
2197 procedure Analyze_Dimension_Simple_Return_Statement
(N
: Node_Id
) is
2198 Expr
: constant Node_Id
:= Expression
(N
);
2199 Return_Ent
: constant Entity_Id
:= Return_Statement_Entity
(N
);
2200 Return_Etyp
: constant Entity_Id
:=
2201 Etype
(Return_Applies_To
(Return_Ent
));
2202 Dims_Of_Return_Etyp
: constant Dimension_Type
:=
2203 Dimensions_Of
(Return_Etyp
);
2205 procedure Error_Dim_Msg_For_Simple_Return_Statement
2207 Return_Etyp
: Entity_Id
;
2209 -- Error using Error_Msg_N at node N. Output the dimensions of the
2210 -- returned type Return_Etyp and the returned expression Expr of N.
2212 -----------------------------------------------
2213 -- Error_Dim_Msg_For_Simple_Return_Statement --
2214 -----------------------------------------------
2216 procedure Error_Dim_Msg_For_Simple_Return_Statement
2218 Return_Etyp
: Entity_Id
;
2222 Error_Msg_N
("dimensions mismatch in return statement", N
);
2224 ("\expected dimension " & Dimensions_Msg_Of
(Return_Etyp
)
2225 & ", found " & Dimensions_Msg_Of
(Expr
), Expr
);
2226 end Error_Dim_Msg_For_Simple_Return_Statement
;
2228 -- Start of processing for Analyze_Dimension_Simple_Return_Statement
2231 if Dims_Of_Return_Etyp
/= Dimensions_Of
(Expr
) then
2232 Error_Dim_Msg_For_Simple_Return_Statement
(N
, Return_Etyp
, Expr
);
2233 Remove_Dimensions
(Expr
);
2235 end Analyze_Dimension_Simple_Return_Statement
;
2237 -------------------------------------------
2238 -- Analyze_Dimension_Subtype_Declaration --
2239 -------------------------------------------
2241 procedure Analyze_Dimension_Subtype_Declaration
(N
: Node_Id
) is
2242 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2243 Dims_Of_Id
: constant Dimension_Type
:= Dimensions_Of
(Id
);
2244 Dims_Of_Etyp
: Dimension_Type
;
2248 -- No constraint case in subtype declaration
2250 if Nkind
(Subtype_Indication
(N
)) /= N_Subtype_Indication
then
2251 Etyp
:= Etype
(Subtype_Indication
(N
));
2252 Dims_Of_Etyp
:= Dimensions_Of
(Etyp
);
2254 if Exists
(Dims_Of_Etyp
) then
2256 -- If subtype already has a dimension (from Aspect_Dimension), it
2257 -- cannot inherit different dimensions from its subtype.
2259 if Exists
(Dims_Of_Id
) and then Dims_Of_Etyp
/= Dims_Of_Id
then
2261 ("subtype& already " & Dimensions_Msg_Of
(Id
, True), N
, Id
);
2263 Set_Dimensions
(Id
, Dims_Of_Etyp
);
2264 Set_Symbol
(Id
, Symbol_Of
(Etyp
));
2268 -- Constraint present in subtype declaration
2271 Etyp
:= Etype
(Subtype_Mark
(Subtype_Indication
(N
)));
2272 Dims_Of_Etyp
:= Dimensions_Of
(Etyp
);
2274 if Exists
(Dims_Of_Etyp
) then
2275 Set_Dimensions
(Id
, Dims_Of_Etyp
);
2276 Set_Symbol
(Id
, Symbol_Of
(Etyp
));
2279 end Analyze_Dimension_Subtype_Declaration
;
2281 --------------------------------
2282 -- Analyze_Dimension_Unary_Op --
2283 --------------------------------
2285 procedure Analyze_Dimension_Unary_Op
(N
: Node_Id
) is
2289 -- Propagate the dimension if the operand is not dimensionless
2296 R
: constant Node_Id
:= Right_Opnd
(N
);
2298 Move_Dimensions
(R
, N
);
2304 end Analyze_Dimension_Unary_Op
;
2306 ---------------------------------
2307 -- Check_Expression_Dimensions --
2308 ---------------------------------
2310 procedure Check_Expression_Dimensions
2315 if Is_Floating_Point_Type
(Etype
(Expr
)) then
2316 Analyze_Dimension
(Expr
);
2318 if Dimensions_Of
(Expr
) /= Dimensions_Of
(Typ
) then
2319 Error_Msg_N
("dimensions mismatch in array aggregate", Expr
);
2321 ("\expected dimension " & Dimensions_Msg_Of
(Typ
)
2322 & ", found " & Dimensions_Msg_Of
(Expr
), Expr
);
2325 end Check_Expression_Dimensions
;
2327 ---------------------
2328 -- Copy_Dimensions --
2329 ---------------------
2331 procedure Copy_Dimensions
(From
, To
: Node_Id
) is
2332 Dims_Of_From
: constant Dimension_Type
:= Dimensions_Of
(From
);
2335 -- Ignore if not Ada 2012 or beyond
2337 if Ada_Version
< Ada_2012
then
2340 -- For Ada 2012, Copy the dimension of 'From to 'To'
2342 elsif Exists
(Dims_Of_From
) then
2343 Set_Dimensions
(To
, Dims_Of_From
);
2345 end Copy_Dimensions
;
2347 --------------------------
2348 -- Create_Rational_From --
2349 --------------------------
2351 -- RATIONAL ::= [-] NUMERAL [/ NUMERAL]
2353 -- A rational number is a number that can be expressed as the quotient or
2354 -- fraction a/b of two integers, where b is non-zero positive.
2356 function Create_Rational_From
2358 Complain
: Boolean) return Rational
2360 Or_Node_Of_Expr
: constant Node_Id
:= Original_Node
(Expr
);
2361 Result
: Rational
:= No_Rational
;
2363 function Process_Minus
(N
: Node_Id
) return Rational
;
2364 -- Create a rational from a N_Op_Minus node
2366 function Process_Divide
(N
: Node_Id
) return Rational
;
2367 -- Create a rational from a N_Op_Divide node
2369 function Process_Literal
(N
: Node_Id
) return Rational
;
2370 -- Create a rational from a N_Integer_Literal node
2376 function Process_Minus
(N
: Node_Id
) return Rational
is
2377 Right
: constant Node_Id
:= Original_Node
(Right_Opnd
(N
));
2381 -- Operand is an integer literal
2383 if Nkind
(Right
) = N_Integer_Literal
then
2384 Result
:= -Process_Literal
(Right
);
2386 -- Operand is a divide operator
2388 elsif Nkind
(Right
) = N_Op_Divide
then
2389 Result
:= -Process_Divide
(Right
);
2392 Result
:= No_Rational
;
2395 -- Provide minimal semantic information on dimension expressions,
2396 -- even though they have no run-time existence. This is for use by
2397 -- ASIS tools, in particular pretty-printing. If generating code
2398 -- standard operator resolution will take place.
2401 Set_Entity
(N
, Standard_Op_Minus
);
2402 Set_Etype
(N
, Standard_Integer
);
2408 --------------------
2409 -- Process_Divide --
2410 --------------------
2412 function Process_Divide
(N
: Node_Id
) return Rational
is
2413 Left
: constant Node_Id
:= Original_Node
(Left_Opnd
(N
));
2414 Right
: constant Node_Id
:= Original_Node
(Right_Opnd
(N
));
2415 Left_Rat
: Rational
;
2416 Result
: Rational
:= No_Rational
;
2417 Right_Rat
: Rational
;
2420 -- Both left and right operands are integer literals
2422 if Nkind
(Left
) = N_Integer_Literal
2424 Nkind
(Right
) = N_Integer_Literal
2426 Left_Rat
:= Process_Literal
(Left
);
2427 Right_Rat
:= Process_Literal
(Right
);
2428 Result
:= Left_Rat
/ Right_Rat
;
2431 -- Provide minimal semantic information on dimension expressions,
2432 -- even though they have no run-time existence. This is for use by
2433 -- ASIS tools, in particular pretty-printing. If generating code
2434 -- standard operator resolution will take place.
2437 Set_Entity
(N
, Standard_Op_Divide
);
2438 Set_Etype
(N
, Standard_Integer
);
2444 ---------------------
2445 -- Process_Literal --
2446 ---------------------
2448 function Process_Literal
(N
: Node_Id
) return Rational
is
2450 return +Whole
(UI_To_Int
(Intval
(N
)));
2451 end Process_Literal
;
2453 -- Start of processing for Create_Rational_From
2456 -- Check the expression is either a division of two integers or an
2457 -- integer itself. Note that the check applies to the original node
2458 -- since the node could have already been rewritten.
2460 -- Integer literal case
2462 if Nkind
(Or_Node_Of_Expr
) = N_Integer_Literal
then
2463 Result
:= Process_Literal
(Or_Node_Of_Expr
);
2465 -- Divide operator case
2467 elsif Nkind
(Or_Node_Of_Expr
) = N_Op_Divide
then
2468 Result
:= Process_Divide
(Or_Node_Of_Expr
);
2470 -- Minus operator case
2472 elsif Nkind
(Or_Node_Of_Expr
) = N_Op_Minus
then
2473 Result
:= Process_Minus
(Or_Node_Of_Expr
);
2476 -- When Expr cannot be interpreted as a rational and Complain is true,
2477 -- generate an error message.
2479 if Complain
and then Result
= No_Rational
then
2480 Error_Msg_N
("rational expected", Expr
);
2484 end Create_Rational_From
;
2490 function Dimensions_Of
(N
: Node_Id
) return Dimension_Type
is
2492 return Dimension_Table
.Get
(N
);
2495 -----------------------
2496 -- Dimensions_Msg_Of --
2497 -----------------------
2499 function Dimensions_Msg_Of
2501 Description_Needed
: Boolean := False) return String
2503 Dims_Of_N
: constant Dimension_Type
:= Dimensions_Of
(N
);
2504 Dimensions_Msg
: Name_Id
;
2505 System
: System_Type
;
2508 -- Initialization of Name_Buffer
2512 -- N is not dimensionless
2514 if Exists
(Dims_Of_N
) then
2515 System
:= System_Of
(Base_Type
(Etype
(N
)));
2517 -- When Description_Needed, add to string "has dimension " before the
2518 -- actual dimension.
2520 if Description_Needed
then
2521 Add_Str_To_Name_Buffer
("has dimension ");
2524 Add_String_To_Name_Buffer
2525 (From_Dim_To_Str_Of_Dim_Symbols
(Dims_Of_N
, System
, True));
2527 -- N is dimensionless
2529 -- When Description_Needed, return "is dimensionless"
2531 elsif Description_Needed
then
2532 Add_Str_To_Name_Buffer
("is dimensionless");
2534 -- Otherwise, return "'[']"
2537 Add_Str_To_Name_Buffer
("'[']");
2540 Dimensions_Msg
:= Name_Find
;
2541 return Get_Name_String
(Dimensions_Msg
);
2542 end Dimensions_Msg_Of
;
2544 --------------------------
2545 -- Dimension_Table_Hash --
2546 --------------------------
2548 function Dimension_Table_Hash
2549 (Key
: Node_Id
) return Dimension_Table_Range
2552 return Dimension_Table_Range
(Key
mod 511);
2553 end Dimension_Table_Hash
;
2555 -------------------------------------
2556 -- Dim_Warning_For_Numeric_Literal --
2557 -------------------------------------
2559 procedure Dim_Warning_For_Numeric_Literal
(N
: Node_Id
; Typ
: Entity_Id
) is
2561 -- Initialize name buffer
2565 Add_String_To_Name_Buffer
(String_From_Numeric_Literal
(N
));
2567 -- Insert a blank between the literal and the symbol
2569 Add_Str_To_Name_Buffer
(" ");
2570 Add_String_To_Name_Buffer
(Symbol_Of
(Typ
));
2572 Error_Msg_Name_1
:= Name_Find
;
2573 Error_Msg_N
("assumed to be%%??", N
);
2574 end Dim_Warning_For_Numeric_Literal
;
2576 ----------------------------------------
2577 -- Eval_Op_Expon_For_Dimensioned_Type --
2578 ----------------------------------------
2580 -- Evaluate the expon operator for real dimensioned type.
2582 -- Note that if the exponent is an integer (denominator = 1) the node is
2583 -- evaluated by the regular Eval_Op_Expon routine (see Sem_Eval).
2585 procedure Eval_Op_Expon_For_Dimensioned_Type
2589 R
: constant Node_Id
:= Right_Opnd
(N
);
2590 R_Value
: Rational
:= No_Rational
;
2593 if Is_Real_Type
(Btyp
) then
2594 R_Value
:= Create_Rational_From
(R
, False);
2597 -- Check that the exponent is not an integer
2599 if R_Value
/= No_Rational
and then R_Value
.Denominator
/= 1 then
2600 Eval_Op_Expon_With_Rational_Exponent
(N
, R_Value
);
2604 end Eval_Op_Expon_For_Dimensioned_Type
;
2606 ------------------------------------------
2607 -- Eval_Op_Expon_With_Rational_Exponent --
2608 ------------------------------------------
2610 -- For dimensioned operand in exponentiation, exponent is allowed to be a
2611 -- Rational and not only an Integer like for dimensionless operands. For
2612 -- that particular case, the left operand is rewritten as a function call
2613 -- using the function Expon_LLF from s-llflex.ads.
2615 procedure Eval_Op_Expon_With_Rational_Exponent
2617 Exponent_Value
: Rational
)
2619 Loc
: constant Source_Ptr
:= Sloc
(N
);
2620 Dims_Of_N
: constant Dimension_Type
:= Dimensions_Of
(N
);
2621 L
: constant Node_Id
:= Left_Opnd
(N
);
2622 Etyp_Of_L
: constant Entity_Id
:= Etype
(L
);
2623 Btyp_Of_L
: constant Entity_Id
:= Base_Type
(Etyp_Of_L
);
2626 Dim_Power
: Rational
;
2627 List_Of_Dims
: List_Id
;
2628 New_Aspect
: Node_Id
;
2629 New_Aspects
: List_Id
;
2632 New_Subtyp_Decl_For_L
: Node_Id
;
2633 System
: System_Type
;
2636 -- Case when the operand is not dimensionless
2638 if Exists
(Dims_Of_N
) then
2640 -- Get the corresponding System_Type to know the exact number of
2641 -- dimensions in the system.
2643 System
:= System_Of
(Btyp_Of_L
);
2645 -- Generation of a new subtype with the proper dimensions
2647 -- In order to rewrite the operator as a type conversion, a new
2648 -- dimensioned subtype with the resulting dimensions of the
2649 -- exponentiation must be created.
2653 -- Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2654 -- System : constant System_Id :=
2655 -- Get_Dimension_System_Id (Btyp_Of_L);
2656 -- Num_Of_Dims : constant Number_Of_Dimensions :=
2657 -- Dimension_Systems.Table (System).Dimension_Count;
2659 -- subtype T is Btyp_Of_L
2662 -- Dims_Of_N (1).Numerator / Dims_Of_N (1).Denominator,
2663 -- Dims_Of_N (2).Numerator / Dims_Of_N (2).Denominator,
2665 -- Dims_Of_N (Num_Of_Dims).Numerator /
2666 -- Dims_Of_N (Num_Of_Dims).Denominator);
2668 -- Step 1: Generate the new aggregate for the aspect Dimension
2670 New_Aspects
:= Empty_List
;
2672 List_Of_Dims
:= New_List
;
2673 for Position
in Dims_Of_N
'First .. System
.Count
loop
2674 Dim_Power
:= Dims_Of_N
(Position
);
2675 Append_To
(List_Of_Dims
,
2676 Make_Op_Divide
(Loc
,
2678 Make_Integer_Literal
(Loc
, Int
(Dim_Power
.Numerator
)),
2680 Make_Integer_Literal
(Loc
, Int
(Dim_Power
.Denominator
))));
2683 -- Step 2: Create the new Aspect Specification for Aspect Dimension
2686 Make_Aspect_Specification
(Loc
,
2687 Identifier
=> Make_Identifier
(Loc
, Name_Dimension
),
2688 Expression
=> Make_Aggregate
(Loc
, Expressions
=> List_Of_Dims
));
2690 -- Step 3: Make a temporary identifier for the new subtype
2692 New_Id
:= Make_Temporary
(Loc
, 'T');
2693 Set_Is_Internal
(New_Id
);
2695 -- Step 4: Declaration of the new subtype
2697 New_Subtyp_Decl_For_L
:=
2698 Make_Subtype_Declaration
(Loc
,
2699 Defining_Identifier
=> New_Id
,
2700 Subtype_Indication
=> New_Occurrence_Of
(Btyp_Of_L
, Loc
));
2702 Append
(New_Aspect
, New_Aspects
);
2703 Set_Parent
(New_Aspects
, New_Subtyp_Decl_For_L
);
2704 Set_Aspect_Specifications
(New_Subtyp_Decl_For_L
, New_Aspects
);
2706 Analyze
(New_Subtyp_Decl_For_L
);
2708 -- Case where the operand is dimensionless
2711 New_Id
:= Btyp_Of_L
;
2714 -- Replacement of N by New_N
2718 -- Actual_1 := Long_Long_Float (L),
2720 -- Actual_2 := Long_Long_Float (Exponent_Value.Numerator) /
2721 -- Long_Long_Float (Exponent_Value.Denominator);
2723 -- (T (Expon_LLF (Actual_1, Actual_2)));
2725 -- where T is the subtype declared in step 1
2727 -- The node is rewritten as a type conversion
2729 -- Step 1: Creation of the two parameters of Expon_LLF function call
2732 Make_Type_Conversion
(Loc
,
2733 Subtype_Mark
=> New_Occurrence_Of
(Standard_Long_Long_Float
, Loc
),
2734 Expression
=> Relocate_Node
(L
));
2737 Make_Op_Divide
(Loc
,
2739 Make_Real_Literal
(Loc
,
2740 UR_From_Uint
(UI_From_Int
(Int
(Exponent_Value
.Numerator
)))),
2742 Make_Real_Literal
(Loc
,
2743 UR_From_Uint
(UI_From_Int
(Int
(Exponent_Value
.Denominator
)))));
2745 -- Step 2: Creation of New_N
2748 Make_Type_Conversion
(Loc
,
2749 Subtype_Mark
=> New_Occurrence_Of
(New_Id
, Loc
),
2751 Make_Function_Call
(Loc
,
2752 Name
=> New_Occurrence_Of
(RTE
(RE_Expon_LLF
), Loc
),
2753 Parameter_Associations
=> New_List
(
2754 Actual_1
, Actual_2
)));
2756 -- Step 3: Rewrite N with the result
2759 Set_Etype
(N
, New_Id
);
2760 Analyze_And_Resolve
(N
, New_Id
);
2761 end Eval_Op_Expon_With_Rational_Exponent
;
2767 function Exists
(Dim
: Dimension_Type
) return Boolean is
2769 return Dim
/= Null_Dimension
;
2772 function Exists
(Str
: String_Id
) return Boolean is
2774 return Str
/= No_String
;
2777 function Exists
(Sys
: System_Type
) return Boolean is
2779 return Sys
/= Null_System
;
2782 ---------------------------------
2783 -- Expand_Put_Call_With_Symbol --
2784 ---------------------------------
2786 -- For procedure Put (resp. Put_Dim_Of) and function Image, defined in
2787 -- System.Dim.Float_IO or System.Dim.Integer_IO, the default string
2788 -- parameter is rewritten to include the unit symbol (or the dimension
2789 -- symbols if not a defined quantity) in the output of a dimensioned
2790 -- object. If a value is already supplied by the user for the parameter
2791 -- Symbol, it is used as is.
2793 -- Case 1. Item is dimensionless
2795 -- * Put : Item appears without a suffix
2797 -- * Put_Dim_Of : the output is []
2799 -- Obj : Mks_Type := 2.6;
2800 -- Put (Obj, 1, 1, 0);
2801 -- Put_Dim_Of (Obj);
2803 -- The corresponding outputs are:
2807 -- Case 2. Item has a dimension
2809 -- * Put : If the type of Item is a dimensioned subtype whose
2810 -- symbol is not empty, then the symbol appears as a
2811 -- suffix. Otherwise, a new string is created and appears
2812 -- as a suffix of Item. This string results in the
2813 -- successive concatanations between each unit symbol
2814 -- raised by its corresponding dimension power from the
2815 -- dimensions of Item.
2817 -- * Put_Dim_Of : The output is a new string resulting in the successive
2818 -- concatanations between each dimension symbol raised by
2819 -- its corresponding dimension power from the dimensions of
2822 -- subtype Random is Mks_Type
2829 -- Obj : Random := 5.0;
2831 -- Put_Dim_Of (Obj);
2833 -- The corresponding outputs are:
2834 -- $5.0 m**3.cd**(-1)
2837 -- The function Image returns the string identical to that produced by
2838 -- a call to Put whose first parameter is a string.
2840 procedure Expand_Put_Call_With_Symbol
(N
: Node_Id
) is
2841 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
2842 Loc
: constant Source_Ptr
:= Sloc
(N
);
2843 Name_Call
: constant Node_Id
:= Name
(N
);
2844 New_Actuals
: constant List_Id
:= New_List
;
2846 Dims_Of_Actual
: Dimension_Type
;
2848 New_Str_Lit
: Node_Id
:= Empty
;
2849 Symbols
: String_Id
;
2851 Is_Put_Dim_Of
: Boolean := False;
2852 -- This flag is used in order to differentiate routines Put and
2853 -- Put_Dim_Of. Set to True if the procedure is one of the Put_Dim_Of
2854 -- defined in System.Dim.Float_IO or System.Dim.Integer_IO.
2856 function Has_Symbols
return Boolean;
2857 -- Return True if the current Put call already has a parameter
2858 -- association for parameter "Symbols" with the correct string of
2861 function Is_Procedure_Put_Call
return Boolean;
2862 -- Return True if the current call is a call of an instantiation of a
2863 -- procedure Put defined in the package System.Dim.Float_IO and
2864 -- System.Dim.Integer_IO.
2866 function Item_Actual
return Node_Id
;
2867 -- Return the item actual parameter node in the output call
2873 function Has_Symbols
return Boolean is
2875 Actual_Str
: Node_Id
;
2878 -- Look for a symbols parameter association in the list of actuals
2880 Actual
:= First
(Actuals
);
2881 while Present
(Actual
) loop
2883 -- Positional parameter association case when the actual is a
2886 if Nkind
(Actual
) = N_String_Literal
then
2887 Actual_Str
:= Actual
;
2889 -- Named parameter association case when selector name is Symbol
2891 elsif Nkind
(Actual
) = N_Parameter_Association
2892 and then Chars
(Selector_Name
(Actual
)) = Name_Symbol
2894 Actual_Str
:= Explicit_Actual_Parameter
(Actual
);
2896 -- Ignore all other cases
2899 Actual_Str
:= Empty
;
2902 if Present
(Actual_Str
) then
2904 -- Return True if the actual comes from source or if the string
2905 -- of symbols doesn't have the default value (i.e. it is ""),
2906 -- in which case it is used as suffix of the generated string.
2908 if Comes_From_Source
(Actual
)
2909 or else String_Length
(Strval
(Actual_Str
)) /= 0
2921 -- At this point, the call has no parameter association. Look to the
2922 -- last actual since the symbols parameter is the last one.
2924 return Nkind
(Last
(Actuals
)) = N_String_Literal
;
2927 ---------------------------
2928 -- Is_Procedure_Put_Call --
2929 ---------------------------
2931 function Is_Procedure_Put_Call
return Boolean is
2936 -- There are three different Put (resp. Put_Dim_Of) routines in each
2937 -- generic dim IO package. Verify the current procedure call is one
2940 if Is_Entity_Name
(Name_Call
) then
2941 Ent
:= Entity
(Name_Call
);
2943 -- Get the original subprogram entity following the renaming chain
2945 if Present
(Alias
(Ent
)) then
2951 -- Check the name of the entity subprogram is Put (resp.
2952 -- Put_Dim_Of) and verify this entity is located in either
2953 -- System.Dim.Float_IO or System.Dim.Integer_IO.
2955 if Loc
> No_Location
2956 and then Is_Dim_IO_Package_Entity
2957 (Cunit_Entity
(Get_Source_Unit
(Loc
)))
2959 if Chars
(Ent
) = Name_Put_Dim_Of
then
2960 Is_Put_Dim_Of
:= True;
2963 elsif Chars
(Ent
) = Name_Put
2964 or else Chars
(Ent
) = Name_Image
2972 end Is_Procedure_Put_Call
;
2978 function Item_Actual
return Node_Id
is
2982 -- Look for the item actual as a parameter association
2984 Actual
:= First
(Actuals
);
2985 while Present
(Actual
) loop
2986 if Nkind
(Actual
) = N_Parameter_Association
2987 and then Chars
(Selector_Name
(Actual
)) = Name_Item
2989 return Explicit_Actual_Parameter
(Actual
);
2995 -- Case where the item has been defined without an association
2997 Actual
:= First
(Actuals
);
2999 -- Depending on the procedure Put, Item actual could be first or
3000 -- second in the list of actuals.
3002 if Has_Dimension_System
(Base_Type
(Etype
(Actual
))) then
3005 return Next
(Actual
);
3009 -- Start of processing for Expand_Put_Call_With_Symbol
3012 if Is_Procedure_Put_Call
and then not Has_Symbols
then
3013 Actual
:= Item_Actual
;
3014 Dims_Of_Actual
:= Dimensions_Of
(Actual
);
3015 Etyp
:= Etype
(Actual
);
3019 if Is_Put_Dim_Of
then
3021 -- Check that the item is not dimensionless
3023 -- Create the new String_Literal with the new String_Id generated
3024 -- by the routine From_Dim_To_Str_Of_Dim_Symbols.
3026 if Exists
(Dims_Of_Actual
) then
3028 Make_String_Literal
(Loc
,
3029 From_Dim_To_Str_Of_Dim_Symbols
3030 (Dims_Of_Actual
, System_Of
(Base_Type
(Etyp
))));
3032 -- If dimensionless, the output is []
3036 Make_String_Literal
(Loc
, "[]");
3042 -- Add the symbol as a suffix of the value if the subtype has a
3043 -- unit symbol or if the parameter is not dimensionless.
3045 if Exists
(Symbol_Of
(Etyp
)) then
3046 Symbols
:= Symbol_Of
(Etyp
);
3048 Symbols
:= From_Dim_To_Str_Of_Unit_Symbols
3049 (Dims_Of_Actual
, System_Of
(Base_Type
(Etyp
)));
3052 -- Check Symbols exists
3054 if Exists
(Symbols
) then
3057 -- Put a space between the value and the dimension
3059 Store_String_Char
(' ');
3060 Store_String_Chars
(Symbols
);
3061 New_Str_Lit
:= Make_String_Literal
(Loc
, End_String
);
3065 if Present
(New_Str_Lit
) then
3067 -- Insert all actuals in New_Actuals
3069 Actual
:= First
(Actuals
);
3070 while Present
(Actual
) loop
3072 -- Copy every actuals in New_Actuals except the Symbols
3073 -- parameter association.
3075 if Nkind
(Actual
) = N_Parameter_Association
3076 and then Chars
(Selector_Name
(Actual
)) /= Name_Symbol
3078 Append_To
(New_Actuals
,
3079 Make_Parameter_Association
(Loc
,
3080 Selector_Name
=> New_Copy
(Selector_Name
(Actual
)),
3081 Explicit_Actual_Parameter
=>
3082 New_Copy
(Explicit_Actual_Parameter
(Actual
))));
3084 elsif Nkind
(Actual
) /= N_Parameter_Association
then
3085 Append_To
(New_Actuals
, New_Copy
(Actual
));
3091 -- Create new Symbols param association and append to New_Actuals
3093 Append_To
(New_Actuals
,
3094 Make_Parameter_Association
(Loc
,
3095 Selector_Name
=> Make_Identifier
(Loc
, Name_Symbol
),
3096 Explicit_Actual_Parameter
=> New_Str_Lit
));
3098 -- Rewrite and analyze the procedure call
3100 if Chars
(Name_Call
) = Name_Image
then
3102 Make_Function_Call
(Loc
,
3103 Name
=> New_Copy
(Name_Call
),
3104 Parameter_Associations
=> New_Actuals
));
3105 Analyze_And_Resolve
(N
);
3108 Make_Procedure_Call_Statement
(Loc
,
3109 Name
=> New_Copy
(Name_Call
),
3110 Parameter_Associations
=> New_Actuals
));
3116 end Expand_Put_Call_With_Symbol
;
3118 ------------------------------------
3119 -- From_Dim_To_Str_Of_Dim_Symbols --
3120 ------------------------------------
3122 -- Given a dimension vector and the corresponding dimension system, create
3123 -- a String_Id to output dimension symbols corresponding to the dimensions
3124 -- Dims. If In_Error_Msg is True, there is a special handling for character
3125 -- asterisk * which is an insertion character in error messages.
3127 function From_Dim_To_Str_Of_Dim_Symbols
3128 (Dims
: Dimension_Type
;
3129 System
: System_Type
;
3130 In_Error_Msg
: Boolean := False) return String_Id
3132 Dim_Power
: Rational
;
3133 First_Dim
: Boolean := True;
3135 procedure Store_String_Oexpon
;
3136 -- Store the expon operator symbol "**" in the string. In error
3137 -- messages, asterisk * is a special character and must be quoted
3138 -- to be placed literally into the message.
3140 -------------------------
3141 -- Store_String_Oexpon --
3142 -------------------------
3144 procedure Store_String_Oexpon
is
3146 if In_Error_Msg
then
3147 Store_String_Chars
("'*'*");
3149 Store_String_Chars
("**");
3151 end Store_String_Oexpon
;
3153 -- Start of processing for From_Dim_To_Str_Of_Dim_Symbols
3156 -- Initialization of the new String_Id
3160 -- Store the dimension symbols inside boxes
3162 if In_Error_Msg
then
3163 Store_String_Chars
("'[");
3165 Store_String_Char
('[');
3168 for Position
in Dimension_Type
'Range loop
3169 Dim_Power
:= Dims
(Position
);
3170 if Dim_Power
/= Zero
then
3175 Store_String_Char
('.');
3178 Store_String_Chars
(System
.Dim_Symbols
(Position
));
3180 -- Positive dimension case
3182 if Dim_Power
.Numerator
> 0 then
3186 if Dim_Power
.Denominator
= 1 then
3187 if Dim_Power
.Numerator
/= 1 then
3188 Store_String_Oexpon
;
3189 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3192 -- Rational case when denominator /= 1
3195 Store_String_Oexpon
;
3196 Store_String_Char
('(');
3197 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3198 Store_String_Char
('/');
3199 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3200 Store_String_Char
(')');
3203 -- Negative dimension case
3206 Store_String_Oexpon
;
3207 Store_String_Char
('(');
3208 Store_String_Char
('-');
3209 Store_String_Int
(Int
(-Dim_Power
.Numerator
));
3213 if Dim_Power
.Denominator
= 1 then
3214 Store_String_Char
(')');
3216 -- Rational case when denominator /= 1
3219 Store_String_Char
('/');
3220 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3221 Store_String_Char
(')');
3227 if In_Error_Msg
then
3228 Store_String_Chars
("']");
3230 Store_String_Char
(']');
3234 end From_Dim_To_Str_Of_Dim_Symbols
;
3236 -------------------------------------
3237 -- From_Dim_To_Str_Of_Unit_Symbols --
3238 -------------------------------------
3240 -- Given a dimension vector and the corresponding dimension system,
3241 -- create a String_Id to output the unit symbols corresponding to the
3244 function From_Dim_To_Str_Of_Unit_Symbols
3245 (Dims
: Dimension_Type
;
3246 System
: System_Type
) return String_Id
3248 Dim_Power
: Rational
;
3249 First_Dim
: Boolean := True;
3252 -- Return No_String if dimensionless
3254 if not Exists
(Dims
) then
3258 -- Initialization of the new String_Id
3262 for Position
in Dimension_Type
'Range loop
3263 Dim_Power
:= Dims
(Position
);
3265 if Dim_Power
/= Zero
then
3269 Store_String_Char
('.');
3272 Store_String_Chars
(System
.Unit_Symbols
(Position
));
3274 -- Positive dimension case
3276 if Dim_Power
.Numerator
> 0 then
3280 if Dim_Power
.Denominator
= 1 then
3281 if Dim_Power
.Numerator
/= 1 then
3282 Store_String_Chars
("**");
3283 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3286 -- Rational case when denominator /= 1
3289 Store_String_Chars
("**");
3290 Store_String_Char
('(');
3291 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3292 Store_String_Char
('/');
3293 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3294 Store_String_Char
(')');
3297 -- Negative dimension case
3300 Store_String_Chars
("**");
3301 Store_String_Char
('(');
3302 Store_String_Char
('-');
3303 Store_String_Int
(Int
(-Dim_Power
.Numerator
));
3307 if Dim_Power
.Denominator
= 1 then
3308 Store_String_Char
(')');
3310 -- Rational case when denominator /= 1
3313 Store_String_Char
('/');
3314 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3315 Store_String_Char
(')');
3322 end From_Dim_To_Str_Of_Unit_Symbols
;
3328 function GCD
(Left
, Right
: Whole
) return Int
is
3348 --------------------------
3349 -- Has_Dimension_System --
3350 --------------------------
3352 function Has_Dimension_System
(Typ
: Entity_Id
) return Boolean is
3354 return Exists
(System_Of
(Typ
));
3355 end Has_Dimension_System
;
3357 ------------------------------
3358 -- Is_Dim_IO_Package_Entity --
3359 ------------------------------
3361 function Is_Dim_IO_Package_Entity
(E
: Entity_Id
) return Boolean is
3363 -- Check the package entity corresponds to System.Dim.Float_IO or
3364 -- System.Dim.Integer_IO.
3367 Is_RTU
(E
, System_Dim_Float_IO
)
3369 Is_RTU
(E
, System_Dim_Integer_IO
);
3370 end Is_Dim_IO_Package_Entity
;
3372 -------------------------------------
3373 -- Is_Dim_IO_Package_Instantiation --
3374 -------------------------------------
3376 function Is_Dim_IO_Package_Instantiation
(N
: Node_Id
) return Boolean is
3377 Gen_Id
: constant Node_Id
:= Name
(N
);
3380 -- Check that the instantiated package is either System.Dim.Float_IO
3381 -- or System.Dim.Integer_IO.
3384 Is_Entity_Name
(Gen_Id
)
3385 and then Is_Dim_IO_Package_Entity
(Entity
(Gen_Id
));
3386 end Is_Dim_IO_Package_Instantiation
;
3392 function Is_Invalid
(Position
: Dimension_Position
) return Boolean is
3394 return Position
= Invalid_Position
;
3397 ---------------------
3398 -- Move_Dimensions --
3399 ---------------------
3401 procedure Move_Dimensions
(From
, To
: Node_Id
) is
3403 if Ada_Version
< Ada_2012
then
3407 -- Copy the dimension of 'From to 'To' and remove dimension of 'From'
3409 Copy_Dimensions
(From
, To
);
3410 Remove_Dimensions
(From
);
3411 end Move_Dimensions
;
3417 function Reduce
(X
: Rational
) return Rational
is
3419 if X
.Numerator
= 0 then
3424 G
: constant Int
:= GCD
(X
.Numerator
, X
.Denominator
);
3426 return Rational
'(Numerator => Whole (Int (X.Numerator) / G),
3427 Denominator => Whole (Int (X.Denominator) / G));
3431 -----------------------
3432 -- Remove_Dimensions --
3433 -----------------------
3435 procedure Remove_Dimensions (N : Node_Id) is
3436 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
3438 if Exists (Dims_Of_N) then
3439 Dimension_Table.Remove (N);
3441 end Remove_Dimensions;
3443 -----------------------------------
3444 -- Remove_Dimension_In_Statement --
3445 -----------------------------------
3447 -- Removal of dimension in statement as part of the Analyze_Statements
3448 -- routine (see package Sem_Ch5).
3450 procedure Remove_Dimension_In_Statement (Stmt : Node_Id) is
3452 if Ada_Version < Ada_2012 then
3456 -- Remove dimension in parameter specifications for accept statement
3458 if Nkind (Stmt) = N_Accept_Statement then
3460 Param : Node_Id := First (Parameter_Specifications (Stmt));
3462 while Present (Param) loop
3463 Remove_Dimensions (Param);
3468 -- Remove dimension of name and expression in assignments
3470 elsif Nkind (Stmt) = N_Assignment_Statement then
3471 Remove_Dimensions (Expression (Stmt));
3472 Remove_Dimensions (Name (Stmt));
3474 end Remove_Dimension_In_Statement;
3476 --------------------
3477 -- Set_Dimensions --
3478 --------------------
3480 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type) is
3482 pragma Assert (OK_For_Dimension (Nkind (N)));
3483 pragma Assert (Exists (Val));
3485 Dimension_Table.Set (N, Val);
3492 procedure Set_Symbol (E : Entity_Id; Val : String_Id) is
3494 Symbol_Table.Set (E, Val);
3497 ---------------------------------
3498 -- String_From_Numeric_Literal --
3499 ---------------------------------
3501 function String_From_Numeric_Literal (N : Node_Id) return String_Id is
3502 Loc : constant Source_Ptr := Sloc (N);
3503 Sbuffer : constant Source_Buffer_Ptr :=
3504 Source_Text (Get_Source_File_Index (Loc));
3505 Src_Ptr : Source_Ptr := Loc;
3507 C : Character := Sbuffer (Src_Ptr);
3508 -- Current source program character
3510 function Belong_To_Numeric_Literal (C : Character) return Boolean;
3511 -- Return True if C belongs to a numeric literal
3513 -------------------------------
3514 -- Belong_To_Numeric_Literal --
3515 -------------------------------
3517 function Belong_To_Numeric_Literal (C : Character) return Boolean is
3521 | '_
' | '.' | 'e
' | '#
' | 'A
' | 'B
' | 'C
' | 'D
' | 'E
' | 'F
'
3525 -- Make sure '+' or '-' is part of an exponent.
3529 Prev_C : constant Character := Sbuffer (Src_Ptr - 1);
3531 return Prev_C = 'e
' or else Prev_C = 'E
';
3534 -- All other character doesn't belong to a numeric literal
3539 end Belong_To_Numeric_Literal;
3541 -- Start of processing for String_From_Numeric_Literal
3545 while Belong_To_Numeric_Literal (C) loop
3546 Store_String_Char (C);
3547 Src_Ptr := Src_Ptr + 1;
3548 C := Sbuffer (Src_Ptr);
3552 end String_From_Numeric_Literal;
3558 function Symbol_Of (E : Entity_Id) return String_Id is
3559 Subtype_Symbol : constant String_Id := Symbol_Table.Get (E);
3561 if Subtype_Symbol /= No_String then
3562 return Subtype_Symbol;
3564 return From_Dim_To_Str_Of_Unit_Symbols
3565 (Dimensions_Of (E), System_Of (Base_Type (E)));
3569 -----------------------
3570 -- Symbol_Table_Hash --
3571 -----------------------
3573 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range is
3575 return Symbol_Table_Range (Key mod 511);
3576 end Symbol_Table_Hash;
3582 function System_Of (E : Entity_Id) return System_Type is
3583 Type_Decl : constant Node_Id := Parent (E);
3586 -- Look for Type_Decl in System_Table
3588 for Dim_Sys in 1 .. System_Table.Last loop
3589 if Type_Decl = System_Table.Table (Dim_Sys).Type_Decl then
3590 return System_Table.Table (Dim_Sys);