1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2011-2017, 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. Otherwise the
1347 -- dimensions are those of the operand, or the type of the operand.
1348 -- This takes care of node rewritings from validity checks, where the
1349 -- dimensions of the operand itself may not be preserved, while the
1350 -- type comes from context and must have dimension information.
1352 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id);
1353 -- Error using Error_Msg_NE and Error_Msg_N at node N. Output the
1354 -- dimensions of both operands.
1356 ---------------------------
1357 -- Dimensions_Of_Operand --
1358 ---------------------------
1360 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type is
1361 Dims : constant Dimension_Type := Dimensions_Of (N);
1364 if Exists (Dims) then
1367 elsif Is_Entity_Name (N) then
1368 return Dimensions_Of (Etype (Entity (N)));
1370 elsif Nkind (N) = N_Real_Literal then
1372 if Present (Original_Entity (N)) then
1373 return Dimensions_Of (Original_Entity (N));
1376 return Dimensions_Of (Etype (N));
1379 -- A type conversion may have been inserted to rewrite other
1380 -- expressions, e.g. function returns. Dimensions are those of
1383 elsif Nkind (N) = N_Type_Conversion then
1384 return Dimensions_Of (Etype (N));
1386 -- Otherwise return the default dimensions
1391 end Dimensions_Of_Operand;
1393 ---------------------------------
1394 -- Error_Dim_Msg_For_Binary_Op --
1395 ---------------------------------
1397 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id) is
1400 ("both operands for operation& must have same dimensions",
1402 Error_Msg_N ("\left operand " & Dimensions_Msg_Of (L, True), N);
1403 Error_Msg_N ("\right operand " & Dimensions_Msg_Of (R, True), N);
1404 end Error_Dim_Msg_For_Binary_Op;
1406 -- Start of processing for Analyze_Dimension_Binary_Op
1409 -- If the node is already analyzed, do not examine the operands. At the
1410 -- end of the analysis their dimensions have been removed, and the node
1411 -- itself may have been rewritten.
1413 if Analyzed (N) then
1417 if Nkind_In (N_Kind, N_Op_Add, N_Op_Expon, N_Op_Subtract)
1418 or else N_Kind in N_Multiplying_Operator
1419 or else N_Kind in N_Op_Compare
1422 L : constant Node_Id := Left_Opnd (N);
1423 Dims_Of_L : constant Dimension_Type :=
1424 Dimensions_Of_Operand (L);
1425 L_Has_Dimensions : constant Boolean := Exists (Dims_Of_L);
1426 R : constant Node_Id := Right_Opnd (N);
1427 Dims_Of_R : constant Dimension_Type :=
1428 Dimensions_Of_Operand (R);
1429 R_Has_Dimensions : constant Boolean := Exists (Dims_Of_R);
1430 Dims_Of_N : Dimension_Type := Null_Dimension;
1433 -- N_Op_Add, N_Op_Mod, N_Op_Rem or N_Op_Subtract case
1435 if Nkind_In (N, N_Op_Add, N_Op_Mod, N_Op_Rem, N_Op_Subtract) then
1437 -- Check both operands have same dimension
1439 if Dims_Of_L /= Dims_Of_R then
1440 Error_Dim_Msg_For_Binary_Op (N, L, R);
1442 -- Check both operands are not dimensionless
1444 if Exists (Dims_Of_L) then
1445 Set_Dimensions (N, Dims_Of_L);
1449 -- N_Op_Multiply or N_Op_Divide case
1451 elsif Nkind_In (N_Kind, N_Op_Multiply, N_Op_Divide) then
1453 -- Check at least one operand is not dimensionless
1455 if L_Has_Dimensions or R_Has_Dimensions then
1457 -- Multiplication case
1459 -- Get both operands dimensions and add them
1461 if N_Kind = N_Op_Multiply then
1462 for Position in Dimension_Type'Range loop
1463 Dims_Of_N (Position) :=
1464 Dims_Of_L (Position) + Dims_Of_R (Position);
1469 -- Get both operands dimensions and subtract them
1472 for Position in Dimension_Type'Range loop
1473 Dims_Of_N (Position) :=
1474 Dims_Of_L (Position) - Dims_Of_R (Position);
1478 if Exists (Dims_Of_N) then
1479 Set_Dimensions (N, Dims_Of_N);
1483 -- Exponentiation case
1485 -- Note: a rational exponent is allowed for dimensioned operand
1487 elsif N_Kind = N_Op_Expon then
1489 -- Check the left operand is not dimensionless. Note that the
1490 -- value of the exponent must be known compile time. Otherwise,
1491 -- the exponentiation evaluation will return an error message.
1493 if L_Has_Dimensions then
1494 if not Compile_Time_Known_Value (R) then
1496 ("exponent of dimensioned operand must be "
1497 & "known at compile time", N);
1501 Exponent_Value : Rational := Zero;
1504 -- Real operand case
1506 if Is_Real_Type (Etype (L)) then
1508 -- Define the exponent as a Rational number
1510 Exponent_Value := Create_Rational_From (R, False);
1512 -- Verify that the exponent cannot be interpreted
1513 -- as a rational, otherwise interpret the exponent
1516 if Exponent_Value = No_Rational then
1518 +Whole (UI_To_Int (Expr_Value (R)));
1521 -- Integer operand case.
1523 -- For integer operand, the exponent cannot be
1524 -- interpreted as a rational.
1527 Exponent_Value := +Whole (UI_To_Int (Expr_Value (R)));
1530 for Position in Dimension_Type'Range loop
1531 Dims_Of_N (Position) :=
1532 Dims_Of_L (Position) * Exponent_Value;
1535 if Exists (Dims_Of_N) then
1536 Set_Dimensions (N, Dims_Of_N);
1543 -- For relational operations, only dimension checking is
1544 -- performed (no propagation). If one operand is the result
1545 -- of constant folding the dimensions may have been lost
1546 -- in a tree copy, so assume that pre-analysis has verified
1547 -- that dimensions are correct.
1549 elsif N_Kind in N_Op_Compare then
1550 if (L_Has_Dimensions or R_Has_Dimensions)
1551 and then Dims_Of_L /= Dims_Of_R
1553 if Nkind (L) = N_Real_Literal
1554 and then not (Comes_From_Source (L))
1555 and then Expander_Active
1559 elsif Nkind (R) = N_Real_Literal
1560 and then not (Comes_From_Source (R))
1561 and then Expander_Active
1566 Error_Dim_Msg_For_Binary_Op (N, L, R);
1571 -- If expander is active, remove dimension information from each
1572 -- operand, as only dimensions of result are relevant.
1574 if Expander_Active then
1575 Remove_Dimensions (L);
1576 Remove_Dimensions (R);
1580 end Analyze_Dimension_Binary_Op;
1582 ----------------------------
1583 -- Analyze_Dimension_Call --
1584 ----------------------------
1586 procedure Analyze_Dimension_Call (N : Node_Id; Nam : Entity_Id) is
1587 Actuals : constant List_Id := Parameter_Associations (N);
1589 Dims_Of_Formal : Dimension_Type;
1591 Formal_Typ : Entity_Id;
1593 Error_Detected : Boolean := False;
1594 -- This flag is used in order to indicate if an error has been detected
1595 -- so far by the compiler in this routine.
1598 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1599 -- dimensions for calls that don't come from source, or those that may
1600 -- have semantic errors.
1602 if Ada_Version < Ada_2012
1603 or else not Comes_From_Source (N)
1604 or else Error_Posted (N)
1609 -- Check the dimensions of the actuals, if any
1611 if not Is_Empty_List (Actuals) then
1613 -- Special processing for elementary functions
1615 -- For Sqrt call, the resulting dimensions equal to half the
1616 -- dimensions of the actual. For all other elementary calls, this
1617 -- routine check that every actual is dimensionless.
1619 if Nkind (N) = N_Function_Call then
1620 Elementary_Function_Calls : declare
1621 Dims_Of_Call : Dimension_Type;
1622 Ent : Entity_Id := Nam;
1624 function Is_Elementary_Function_Entity
1625 (Sub_Id : Entity_Id) return Boolean;
1626 -- Given Sub_Id, the original subprogram entity, return True
1627 -- if call is to an elementary function (see Ada.Numerics.
1628 -- Generic_Elementary_Functions).
1630 -----------------------------------
1631 -- Is_Elementary_Function_Entity --
1632 -----------------------------------
1634 function Is_Elementary_Function_Entity
1635 (Sub_Id : Entity_Id) return Boolean
1637 Loc : constant Source_Ptr := Sloc (Sub_Id);
1640 -- Is entity in Ada.Numerics.Generic_Elementary_Functions?
1646 (Cunit_Entity (Get_Source_Unit (Loc)),
1647 Ada_Numerics_Generic_Elementary_Functions);
1648 end Is_Elementary_Function_Entity;
1650 -- Start of processing for Elementary_Function_Calls
1653 -- Get original subprogram entity following the renaming chain
1655 if Present (Alias (Ent)) then
1659 -- Check the call is an Elementary function call
1661 if Is_Elementary_Function_Entity (Ent) then
1663 -- Sqrt function call case
1665 if Chars (Ent) = Name_Sqrt then
1666 Dims_Of_Call := Dimensions_Of (First_Actual (N));
1668 -- Evaluates the resulting dimensions (i.e. half the
1669 -- dimensions of the actual).
1671 if Exists (Dims_Of_Call) then
1672 for Position in Dims_Of_Call'Range loop
1673 Dims_Of_Call (Position) :=
1674 Dims_Of_Call (Position) *
1675 Rational'(Numerator
=> 1, Denominator
=> 2);
1678 Set_Dimensions
(N
, Dims_Of_Call
);
1681 -- All other elementary functions case. Note that every
1682 -- actual here should be dimensionless.
1685 Actual
:= First_Actual
(N
);
1686 while Present
(Actual
) loop
1687 if Exists
(Dimensions_Of
(Actual
)) then
1689 -- Check if error has already been encountered
1691 if not Error_Detected
then
1693 ("dimensions mismatch in call of&",
1695 Error_Detected
:= True;
1699 ("\expected dimension '['], found "
1700 & Dimensions_Msg_Of
(Actual
), Actual
);
1703 Next_Actual
(Actual
);
1707 -- Nothing more to do for elementary functions
1711 end Elementary_Function_Calls
;
1714 -- General case. Check, for each parameter, the dimensions of the
1715 -- actual and its corresponding formal match. Otherwise, complain.
1717 Actual
:= First_Actual
(N
);
1718 Formal
:= First_Formal
(Nam
);
1719 while Present
(Formal
) loop
1721 -- A missing corresponding actual indicates that the analysis of
1722 -- the call was aborted due to a previous error.
1725 Check_Error_Detected
;
1729 Formal_Typ
:= Etype
(Formal
);
1730 Dims_Of_Formal
:= Dimensions_Of
(Formal_Typ
);
1732 -- If the formal is not dimensionless, check dimensions of formal
1733 -- and actual match. Otherwise, complain.
1735 if Exists
(Dims_Of_Formal
)
1736 and then Dimensions_Of
(Actual
) /= Dims_Of_Formal
1738 -- Check if an error has already been encountered so far
1740 if not Error_Detected
then
1741 Error_Msg_NE
("dimensions mismatch in& call", N
, Name
(N
));
1742 Error_Detected
:= True;
1746 ("\expected dimension " & Dimensions_Msg_Of
(Formal_Typ
)
1747 & ", found " & Dimensions_Msg_Of
(Actual
), Actual
);
1750 Next_Actual
(Actual
);
1751 Next_Formal
(Formal
);
1755 -- For function calls, propagate the dimensions from the returned type
1757 if Nkind
(N
) = N_Function_Call
then
1758 Analyze_Dimension_Has_Etype
(N
);
1760 end Analyze_Dimension_Call
;
1762 ---------------------------------------------
1763 -- Analyze_Dimension_Component_Declaration --
1764 ---------------------------------------------
1766 procedure Analyze_Dimension_Component_Declaration
(N
: Node_Id
) is
1767 Expr
: constant Node_Id
:= Expression
(N
);
1768 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1769 Etyp
: constant Entity_Id
:= Etype
(Id
);
1770 Dims_Of_Etyp
: constant Dimension_Type
:= Dimensions_Of
(Etyp
);
1771 Dims_Of_Expr
: Dimension_Type
;
1773 procedure Error_Dim_Msg_For_Component_Declaration
1777 -- Error using Error_Msg_N at node N. Output the dimensions of the
1778 -- type Etyp and the expression Expr of N.
1780 ---------------------------------------------
1781 -- Error_Dim_Msg_For_Component_Declaration --
1782 ---------------------------------------------
1784 procedure Error_Dim_Msg_For_Component_Declaration
1789 Error_Msg_N
("dimensions mismatch in component declaration", N
);
1791 ("\expected dimension " & Dimensions_Msg_Of
(Etyp
) & ", found "
1792 & Dimensions_Msg_Of
(Expr
), Expr
);
1793 end Error_Dim_Msg_For_Component_Declaration
;
1795 -- Start of processing for Analyze_Dimension_Component_Declaration
1798 -- Expression is present
1800 if Present
(Expr
) then
1801 Dims_Of_Expr
:= Dimensions_Of
(Expr
);
1803 -- Check dimensions match
1805 if Dims_Of_Etyp
/= Dims_Of_Expr
then
1807 -- Numeric literal case. Issue a warning if the object type is not
1808 -- dimensionless to indicate the literal is treated as if its
1809 -- dimension matches the type dimension.
1811 if Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
1814 Dim_Warning_For_Numeric_Literal
(Expr
, Etyp
);
1816 -- Issue a dimension mismatch error for all other cases
1819 Error_Dim_Msg_For_Component_Declaration
(N
, Etyp
, Expr
);
1823 end Analyze_Dimension_Component_Declaration
;
1825 -------------------------------------------------
1826 -- Analyze_Dimension_Extended_Return_Statement --
1827 -------------------------------------------------
1829 procedure Analyze_Dimension_Extended_Return_Statement
(N
: Node_Id
) is
1830 Return_Ent
: constant Entity_Id
:= Return_Statement_Entity
(N
);
1831 Return_Etyp
: constant Entity_Id
:=
1832 Etype
(Return_Applies_To
(Return_Ent
));
1833 Return_Obj_Decls
: constant List_Id
:= Return_Object_Declarations
(N
);
1834 Return_Obj_Decl
: Node_Id
;
1835 Return_Obj_Id
: Entity_Id
;
1836 Return_Obj_Typ
: Entity_Id
;
1838 procedure Error_Dim_Msg_For_Extended_Return_Statement
1840 Return_Etyp
: Entity_Id
;
1841 Return_Obj_Typ
: Entity_Id
);
1842 -- Error using Error_Msg_N at node N. Output dimensions of the returned
1843 -- type Return_Etyp and the returned object type Return_Obj_Typ of N.
1845 -------------------------------------------------
1846 -- Error_Dim_Msg_For_Extended_Return_Statement --
1847 -------------------------------------------------
1849 procedure Error_Dim_Msg_For_Extended_Return_Statement
1851 Return_Etyp
: Entity_Id
;
1852 Return_Obj_Typ
: Entity_Id
)
1855 Error_Msg_N
("dimensions mismatch in extended return statement", N
);
1857 ("\expected dimension " & Dimensions_Msg_Of
(Return_Etyp
)
1858 & ", found " & Dimensions_Msg_Of
(Return_Obj_Typ
), N
);
1859 end Error_Dim_Msg_For_Extended_Return_Statement
;
1861 -- Start of processing for Analyze_Dimension_Extended_Return_Statement
1864 if Present
(Return_Obj_Decls
) then
1865 Return_Obj_Decl
:= First
(Return_Obj_Decls
);
1866 while Present
(Return_Obj_Decl
) loop
1867 if Nkind
(Return_Obj_Decl
) = N_Object_Declaration
then
1868 Return_Obj_Id
:= Defining_Identifier
(Return_Obj_Decl
);
1870 if Is_Return_Object
(Return_Obj_Id
) then
1871 Return_Obj_Typ
:= Etype
(Return_Obj_Id
);
1873 -- Issue an error message if dimensions mismatch
1875 if Dimensions_Of
(Return_Etyp
) /=
1876 Dimensions_Of
(Return_Obj_Typ
)
1878 Error_Dim_Msg_For_Extended_Return_Statement
1879 (N
, Return_Etyp
, Return_Obj_Typ
);
1885 Next
(Return_Obj_Decl
);
1888 end Analyze_Dimension_Extended_Return_Statement
;
1890 -----------------------------------------------------
1891 -- Analyze_Dimension_Extension_Or_Record_Aggregate --
1892 -----------------------------------------------------
1894 procedure Analyze_Dimension_Extension_Or_Record_Aggregate
(N
: Node_Id
) is
1896 Comp_Id
: Entity_Id
;
1897 Comp_Typ
: Entity_Id
;
1900 Error_Detected
: Boolean := False;
1901 -- This flag is used in order to indicate if an error has been detected
1902 -- so far by the compiler in this routine.
1905 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1906 -- dimensions for aggregates that don't come from source, or if we are
1907 -- within an initialization procedure, whose expressions have been
1908 -- checked at the point of record declaration.
1910 if Ada_Version
< Ada_2012
1911 or else not Comes_From_Source
(N
)
1912 or else Inside_Init_Proc
1917 Comp
:= First
(Component_Associations
(N
));
1918 while Present
(Comp
) loop
1919 Comp_Id
:= Entity
(First
(Choices
(Comp
)));
1920 Comp_Typ
:= Etype
(Comp_Id
);
1922 -- Check the component type is either a dimensioned type or a
1923 -- dimensioned subtype.
1925 if Has_Dimension_System
(Base_Type
(Comp_Typ
)) then
1926 Expr
:= Expression
(Comp
);
1928 -- A box-initialized component needs no checking.
1930 if No
(Expr
) and then Box_Present
(Comp
) then
1933 -- Issue an error if the dimensions of the component type and the
1934 -- dimensions of the component mismatch.
1936 elsif Dimensions_Of
(Expr
) /= Dimensions_Of
(Comp_Typ
) then
1938 -- Check if an error has already been encountered so far
1940 if not Error_Detected
then
1942 -- Extension aggregate case
1944 if Nkind
(N
) = N_Extension_Aggregate
then
1946 ("dimensions mismatch in extension aggregate", N
);
1948 -- Record aggregate case
1952 ("dimensions mismatch in record aggregate", N
);
1955 Error_Detected
:= True;
1959 ("\expected dimension " & Dimensions_Msg_Of
(Comp_Typ
)
1960 & ", found " & Dimensions_Msg_Of
(Expr
), Comp
);
1966 end Analyze_Dimension_Extension_Or_Record_Aggregate
;
1968 -------------------------------
1969 -- Analyze_Dimension_Formals --
1970 -------------------------------
1972 procedure Analyze_Dimension_Formals
(N
: Node_Id
; Formals
: List_Id
) is
1973 Dims_Of_Typ
: Dimension_Type
;
1978 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1979 -- dimensions for sub specs that don't come from source.
1981 if Ada_Version
< Ada_2012
or else not Comes_From_Source
(N
) then
1985 Formal
:= First
(Formals
);
1986 while Present
(Formal
) loop
1987 Typ
:= Parameter_Type
(Formal
);
1988 Dims_Of_Typ
:= Dimensions_Of
(Typ
);
1990 if Exists
(Dims_Of_Typ
) then
1992 Expr
: constant Node_Id
:= Expression
(Formal
);
1995 -- Issue a warning if Expr is a numeric literal and if its
1996 -- dimensions differ with the dimensions of the formal type.
1999 and then Dims_Of_Typ
/= Dimensions_Of
(Expr
)
2000 and then Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
2003 Dim_Warning_For_Numeric_Literal
(Expr
, Etype
(Typ
));
2010 end Analyze_Dimension_Formals
;
2012 ---------------------------------
2013 -- Analyze_Dimension_Has_Etype --
2014 ---------------------------------
2016 procedure Analyze_Dimension_Has_Etype
(N
: Node_Id
) is
2017 Etyp
: constant Entity_Id
:= Etype
(N
);
2018 Dims_Of_Etyp
: Dimension_Type
:= Dimensions_Of
(Etyp
);
2021 -- General case. Propagation of the dimensions from the type
2023 if Exists
(Dims_Of_Etyp
) then
2024 Set_Dimensions
(N
, Dims_Of_Etyp
);
2026 -- Identifier case. Propagate the dimensions from the entity for
2027 -- identifier whose entity is a non-dimensionless constant.
2029 elsif Nkind
(N
) = N_Identifier
then
2030 Analyze_Dimension_Identifier
: declare
2031 Id
: constant Entity_Id
:= Entity
(N
);
2034 -- If Id is missing, abnormal tree, assume previous error
2037 Check_Error_Detected
;
2040 elsif Ekind_In
(Id
, E_Constant
, E_Named_Real
)
2041 and then Exists
(Dimensions_Of
(Id
))
2043 Set_Dimensions
(N
, Dimensions_Of
(Id
));
2045 end Analyze_Dimension_Identifier
;
2047 -- Attribute reference case. Propagate the dimensions from the prefix.
2049 elsif Nkind
(N
) = N_Attribute_Reference
2050 and then Has_Dimension_System
(Base_Type
(Etyp
))
2052 Dims_Of_Etyp
:= Dimensions_Of
(Prefix
(N
));
2054 -- Check the prefix is not dimensionless
2056 if Exists
(Dims_Of_Etyp
) then
2057 Set_Dimensions
(N
, Dims_Of_Etyp
);
2061 -- Remove dimensions from inner expressions, to prevent dimensions
2062 -- table from growing uselessly.
2065 when N_Attribute_Reference
2066 | N_Indexed_Component
2069 Exprs
: constant List_Id
:= Expressions
(N
);
2073 if Present
(Exprs
) then
2074 Expr
:= First
(Exprs
);
2075 while Present
(Expr
) loop
2076 Remove_Dimensions
(Expr
);
2082 when N_Qualified_Expression
2084 | N_Unchecked_Type_Conversion
2086 Remove_Dimensions
(Expression
(N
));
2088 when N_Selected_Component
=>
2089 Remove_Dimensions
(Selector_Name
(N
));
2094 end Analyze_Dimension_Has_Etype
;
2096 ------------------------------------------
2097 -- Analyze_Dimension_Number_Declaration --
2098 ------------------------------------------
2100 procedure Analyze_Dimension_Number_Declaration
(N
: Node_Id
) is
2101 Expr
: constant Node_Id
:= Expression
(N
);
2102 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2103 Dim_Of_Expr
: constant Dimension_Type
:= Dimensions_Of
(Expr
);
2106 if Exists
(Dim_Of_Expr
) then
2107 Set_Dimensions
(Id
, Dim_Of_Expr
);
2108 Set_Etype
(Id
, Etype
(Expr
));
2110 end Analyze_Dimension_Number_Declaration
;
2112 ------------------------------------------
2113 -- Analyze_Dimension_Object_Declaration --
2114 ------------------------------------------
2116 procedure Analyze_Dimension_Object_Declaration
(N
: Node_Id
) is
2117 Expr
: constant Node_Id
:= Expression
(N
);
2118 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2119 Etyp
: constant Entity_Id
:= Etype
(Id
);
2120 Dim_Of_Etyp
: constant Dimension_Type
:= Dimensions_Of
(Etyp
);
2121 Dim_Of_Expr
: Dimension_Type
;
2123 procedure Error_Dim_Msg_For_Object_Declaration
2127 -- Error using Error_Msg_N at node N. Output the dimensions of the
2128 -- type Etyp and of the expression Expr.
2130 ------------------------------------------
2131 -- Error_Dim_Msg_For_Object_Declaration --
2132 ------------------------------------------
2134 procedure Error_Dim_Msg_For_Object_Declaration
2139 Error_Msg_N
("dimensions mismatch in object declaration", N
);
2141 ("\expected dimension " & Dimensions_Msg_Of
(Etyp
) & ", found "
2142 & Dimensions_Msg_Of
(Expr
), Expr
);
2143 end Error_Dim_Msg_For_Object_Declaration
;
2145 -- Start of processing for Analyze_Dimension_Object_Declaration
2148 -- Expression is present
2150 if Present
(Expr
) then
2151 Dim_Of_Expr
:= Dimensions_Of
(Expr
);
2153 -- Check dimensions match
2155 if Dim_Of_Expr
/= Dim_Of_Etyp
then
2157 -- Numeric literal case. Issue a warning if the object type is
2158 -- not dimensionless to indicate the literal is treated as if
2159 -- its dimension matches the type dimension.
2161 if Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
2164 Dim_Warning_For_Numeric_Literal
(Expr
, Etyp
);
2166 -- Case of object is a constant whose type is a dimensioned type
2168 elsif Constant_Present
(N
) and then not Exists
(Dim_Of_Etyp
) then
2170 -- Propagate dimension from expression to object entity
2172 Set_Dimensions
(Id
, Dim_Of_Expr
);
2174 -- Expression may have been constant-folded. If nominal type has
2175 -- dimensions, verify that expression has same type.
2177 elsif Exists
(Dim_Of_Etyp
) and then Etype
(Expr
) = Etyp
then
2180 -- For all other cases, issue an error message
2183 Error_Dim_Msg_For_Object_Declaration
(N
, Etyp
, Expr
);
2187 -- Remove dimensions in expression after checking consistency with
2190 Remove_Dimensions
(Expr
);
2192 end Analyze_Dimension_Object_Declaration
;
2194 ---------------------------------------------------
2195 -- Analyze_Dimension_Object_Renaming_Declaration --
2196 ---------------------------------------------------
2198 procedure Analyze_Dimension_Object_Renaming_Declaration
(N
: Node_Id
) is
2199 Renamed_Name
: constant Node_Id
:= Name
(N
);
2200 Sub_Mark
: constant Node_Id
:= Subtype_Mark
(N
);
2202 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2205 Renamed_Name
: Node_Id
);
2206 -- Error using Error_Msg_N at node N. Output the dimensions of
2207 -- Sub_Mark and of Renamed_Name.
2209 ---------------------------------------------------
2210 -- Error_Dim_Msg_For_Object_Renaming_Declaration --
2211 ---------------------------------------------------
2213 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2216 Renamed_Name
: Node_Id
) is
2218 Error_Msg_N
("dimensions mismatch in object renaming declaration", N
);
2220 ("\expected dimension " & Dimensions_Msg_Of
(Sub_Mark
) & ", found "
2221 & Dimensions_Msg_Of
(Renamed_Name
), Renamed_Name
);
2222 end Error_Dim_Msg_For_Object_Renaming_Declaration
;
2224 -- Start of processing for Analyze_Dimension_Object_Renaming_Declaration
2227 if Dimensions_Of
(Renamed_Name
) /= Dimensions_Of
(Sub_Mark
) then
2228 Error_Dim_Msg_For_Object_Renaming_Declaration
2229 (N
, Sub_Mark
, Renamed_Name
);
2231 end Analyze_Dimension_Object_Renaming_Declaration
;
2233 -----------------------------------------------
2234 -- Analyze_Dimension_Simple_Return_Statement --
2235 -----------------------------------------------
2237 procedure Analyze_Dimension_Simple_Return_Statement
(N
: Node_Id
) is
2238 Expr
: constant Node_Id
:= Expression
(N
);
2239 Return_Ent
: constant Entity_Id
:= Return_Statement_Entity
(N
);
2240 Return_Etyp
: constant Entity_Id
:=
2241 Etype
(Return_Applies_To
(Return_Ent
));
2242 Dims_Of_Return_Etyp
: constant Dimension_Type
:=
2243 Dimensions_Of
(Return_Etyp
);
2245 procedure Error_Dim_Msg_For_Simple_Return_Statement
2247 Return_Etyp
: Entity_Id
;
2249 -- Error using Error_Msg_N at node N. Output the dimensions of the
2250 -- returned type Return_Etyp and the returned expression Expr of N.
2252 -----------------------------------------------
2253 -- Error_Dim_Msg_For_Simple_Return_Statement --
2254 -----------------------------------------------
2256 procedure Error_Dim_Msg_For_Simple_Return_Statement
2258 Return_Etyp
: Entity_Id
;
2262 Error_Msg_N
("dimensions mismatch in return statement", N
);
2264 ("\expected dimension " & Dimensions_Msg_Of
(Return_Etyp
)
2265 & ", found " & Dimensions_Msg_Of
(Expr
), Expr
);
2266 end Error_Dim_Msg_For_Simple_Return_Statement
;
2268 -- Start of processing for Analyze_Dimension_Simple_Return_Statement
2271 if Dims_Of_Return_Etyp
/= Dimensions_Of
(Expr
) then
2272 Error_Dim_Msg_For_Simple_Return_Statement
(N
, Return_Etyp
, Expr
);
2273 Remove_Dimensions
(Expr
);
2275 end Analyze_Dimension_Simple_Return_Statement
;
2277 -------------------------------------------
2278 -- Analyze_Dimension_Subtype_Declaration --
2279 -------------------------------------------
2281 procedure Analyze_Dimension_Subtype_Declaration
(N
: Node_Id
) is
2282 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2283 Dims_Of_Id
: constant Dimension_Type
:= Dimensions_Of
(Id
);
2284 Dims_Of_Etyp
: Dimension_Type
;
2288 -- No constraint case in subtype declaration
2290 if Nkind
(Subtype_Indication
(N
)) /= N_Subtype_Indication
then
2291 Etyp
:= Etype
(Subtype_Indication
(N
));
2292 Dims_Of_Etyp
:= Dimensions_Of
(Etyp
);
2294 if Exists
(Dims_Of_Etyp
) then
2296 -- If subtype already has a dimension (from Aspect_Dimension), it
2297 -- cannot inherit different dimensions from its subtype.
2299 if Exists
(Dims_Of_Id
) and then Dims_Of_Etyp
/= Dims_Of_Id
then
2301 ("subtype& already " & Dimensions_Msg_Of
(Id
, True), N
, Id
);
2303 Set_Dimensions
(Id
, Dims_Of_Etyp
);
2304 Set_Symbol
(Id
, Symbol_Of
(Etyp
));
2308 -- Constraint present in subtype declaration
2311 Etyp
:= Etype
(Subtype_Mark
(Subtype_Indication
(N
)));
2312 Dims_Of_Etyp
:= Dimensions_Of
(Etyp
);
2314 if Exists
(Dims_Of_Etyp
) then
2315 Set_Dimensions
(Id
, Dims_Of_Etyp
);
2316 Set_Symbol
(Id
, Symbol_Of
(Etyp
));
2319 end Analyze_Dimension_Subtype_Declaration
;
2321 --------------------------------
2322 -- Analyze_Dimension_Unary_Op --
2323 --------------------------------
2325 procedure Analyze_Dimension_Unary_Op
(N
: Node_Id
) is
2329 -- Propagate the dimension if the operand is not dimensionless
2336 R
: constant Node_Id
:= Right_Opnd
(N
);
2338 Move_Dimensions
(R
, N
);
2344 end Analyze_Dimension_Unary_Op
;
2346 ---------------------------------
2347 -- Check_Expression_Dimensions --
2348 ---------------------------------
2350 procedure Check_Expression_Dimensions
2355 if Is_Floating_Point_Type
(Etype
(Expr
)) then
2356 Analyze_Dimension
(Expr
);
2358 if Dimensions_Of
(Expr
) /= Dimensions_Of
(Typ
) then
2359 Error_Msg_N
("dimensions mismatch in array aggregate", Expr
);
2361 ("\expected dimension " & Dimensions_Msg_Of
(Typ
)
2362 & ", found " & Dimensions_Msg_Of
(Expr
), Expr
);
2365 end Check_Expression_Dimensions
;
2367 ---------------------
2368 -- Copy_Dimensions --
2369 ---------------------
2371 procedure Copy_Dimensions
(From
: Node_Id
; To
: Node_Id
) is
2372 Dims_Of_From
: constant Dimension_Type
:= Dimensions_Of
(From
);
2375 -- Ignore if not Ada 2012 or beyond
2377 if Ada_Version
< Ada_2012
then
2380 -- For Ada 2012, Copy the dimension of 'From to 'To'
2382 elsif Exists
(Dims_Of_From
) then
2383 Set_Dimensions
(To
, Dims_Of_From
);
2385 end Copy_Dimensions
;
2387 --------------------------
2388 -- Create_Rational_From --
2389 --------------------------
2391 -- RATIONAL ::= [-] NUMERAL [/ NUMERAL]
2393 -- A rational number is a number that can be expressed as the quotient or
2394 -- fraction a/b of two integers, where b is non-zero positive.
2396 function Create_Rational_From
2398 Complain
: Boolean) return Rational
2400 Or_Node_Of_Expr
: constant Node_Id
:= Original_Node
(Expr
);
2401 Result
: Rational
:= No_Rational
;
2403 function Process_Minus
(N
: Node_Id
) return Rational
;
2404 -- Create a rational from a N_Op_Minus node
2406 function Process_Divide
(N
: Node_Id
) return Rational
;
2407 -- Create a rational from a N_Op_Divide node
2409 function Process_Literal
(N
: Node_Id
) return Rational
;
2410 -- Create a rational from a N_Integer_Literal node
2416 function Process_Minus
(N
: Node_Id
) return Rational
is
2417 Right
: constant Node_Id
:= Original_Node
(Right_Opnd
(N
));
2421 -- Operand is an integer literal
2423 if Nkind
(Right
) = N_Integer_Literal
then
2424 Result
:= -Process_Literal
(Right
);
2426 -- Operand is a divide operator
2428 elsif Nkind
(Right
) = N_Op_Divide
then
2429 Result
:= -Process_Divide
(Right
);
2432 Result
:= No_Rational
;
2435 -- Provide minimal semantic information on dimension expressions,
2436 -- even though they have no run-time existence. This is for use by
2437 -- ASIS tools, in particular pretty-printing. If generating code
2438 -- standard operator resolution will take place.
2441 Set_Entity
(N
, Standard_Op_Minus
);
2442 Set_Etype
(N
, Standard_Integer
);
2448 --------------------
2449 -- Process_Divide --
2450 --------------------
2452 function Process_Divide
(N
: Node_Id
) return Rational
is
2453 Left
: constant Node_Id
:= Original_Node
(Left_Opnd
(N
));
2454 Right
: constant Node_Id
:= Original_Node
(Right_Opnd
(N
));
2455 Left_Rat
: Rational
;
2456 Result
: Rational
:= No_Rational
;
2457 Right_Rat
: Rational
;
2460 -- Both left and right operands are integer literals
2462 if Nkind
(Left
) = N_Integer_Literal
2464 Nkind
(Right
) = N_Integer_Literal
2466 Left_Rat
:= Process_Literal
(Left
);
2467 Right_Rat
:= Process_Literal
(Right
);
2468 Result
:= Left_Rat
/ Right_Rat
;
2471 -- Provide minimal semantic information on dimension expressions,
2472 -- even though they have no run-time existence. This is for use by
2473 -- ASIS tools, in particular pretty-printing. If generating code
2474 -- standard operator resolution will take place.
2477 Set_Entity
(N
, Standard_Op_Divide
);
2478 Set_Etype
(N
, Standard_Integer
);
2484 ---------------------
2485 -- Process_Literal --
2486 ---------------------
2488 function Process_Literal
(N
: Node_Id
) return Rational
is
2490 return +Whole
(UI_To_Int
(Intval
(N
)));
2491 end Process_Literal
;
2493 -- Start of processing for Create_Rational_From
2496 -- Check the expression is either a division of two integers or an
2497 -- integer itself. Note that the check applies to the original node
2498 -- since the node could have already been rewritten.
2500 -- Integer literal case
2502 if Nkind
(Or_Node_Of_Expr
) = N_Integer_Literal
then
2503 Result
:= Process_Literal
(Or_Node_Of_Expr
);
2505 -- Divide operator case
2507 elsif Nkind
(Or_Node_Of_Expr
) = N_Op_Divide
then
2508 Result
:= Process_Divide
(Or_Node_Of_Expr
);
2510 -- Minus operator case
2512 elsif Nkind
(Or_Node_Of_Expr
) = N_Op_Minus
then
2513 Result
:= Process_Minus
(Or_Node_Of_Expr
);
2516 -- When Expr cannot be interpreted as a rational and Complain is true,
2517 -- generate an error message.
2519 if Complain
and then Result
= No_Rational
then
2520 Error_Msg_N
("rational expected", Expr
);
2524 end Create_Rational_From
;
2530 function Dimensions_Of
(N
: Node_Id
) return Dimension_Type
is
2532 return Dimension_Table
.Get
(N
);
2535 -----------------------
2536 -- Dimensions_Msg_Of --
2537 -----------------------
2539 function Dimensions_Msg_Of
2541 Description_Needed
: Boolean := False) return String
2543 Dims_Of_N
: constant Dimension_Type
:= Dimensions_Of
(N
);
2544 Dimensions_Msg
: Name_Id
;
2545 System
: System_Type
;
2548 -- Initialization of Name_Buffer
2552 -- N is not dimensionless
2554 if Exists
(Dims_Of_N
) then
2555 System
:= System_Of
(Base_Type
(Etype
(N
)));
2557 -- When Description_Needed, add to string "has dimension " before the
2558 -- actual dimension.
2560 if Description_Needed
then
2561 Add_Str_To_Name_Buffer
("has dimension ");
2565 (Global_Name_Buffer
,
2566 From_Dim_To_Str_Of_Dim_Symbols
(Dims_Of_N
, System
, True));
2568 -- N is dimensionless
2570 -- When Description_Needed, return "is dimensionless"
2572 elsif Description_Needed
then
2573 Add_Str_To_Name_Buffer
("is dimensionless");
2575 -- Otherwise, return "'[']"
2578 Add_Str_To_Name_Buffer
("'[']");
2581 Dimensions_Msg
:= Name_Find
;
2582 return Get_Name_String
(Dimensions_Msg
);
2583 end Dimensions_Msg_Of
;
2585 --------------------------
2586 -- Dimension_Table_Hash --
2587 --------------------------
2589 function Dimension_Table_Hash
2590 (Key
: Node_Id
) return Dimension_Table_Range
2593 return Dimension_Table_Range
(Key
mod 511);
2594 end Dimension_Table_Hash
;
2596 -------------------------------------
2597 -- Dim_Warning_For_Numeric_Literal --
2598 -------------------------------------
2600 procedure Dim_Warning_For_Numeric_Literal
(N
: Node_Id
; Typ
: Entity_Id
) is
2602 -- Initialize name buffer
2606 Append
(Global_Name_Buffer
, String_From_Numeric_Literal
(N
));
2608 -- Insert a blank between the literal and the symbol
2610 Add_Str_To_Name_Buffer
(" ");
2611 Append
(Global_Name_Buffer
, Symbol_Of
(Typ
));
2613 Error_Msg_Name_1
:= Name_Find
;
2614 Error_Msg_N
("assumed to be%%??", N
);
2615 end Dim_Warning_For_Numeric_Literal
;
2617 ----------------------
2618 -- Dimensions_Match --
2619 ----------------------
2621 function Dimensions_Match
(T1
: Entity_Id
; T2
: Entity_Id
) return Boolean is
2624 not Has_Dimension_System
(Base_Type
(T1
))
2625 or else Dimensions_Of
(T1
) = Dimensions_Of
(T2
);
2626 end Dimensions_Match
;
2628 ----------------------------------------
2629 -- Eval_Op_Expon_For_Dimensioned_Type --
2630 ----------------------------------------
2632 -- Evaluate the expon operator for real dimensioned type.
2634 -- Note that if the exponent is an integer (denominator = 1) the node is
2635 -- evaluated by the regular Eval_Op_Expon routine (see Sem_Eval).
2637 procedure Eval_Op_Expon_For_Dimensioned_Type
2641 R
: constant Node_Id
:= Right_Opnd
(N
);
2642 R_Value
: Rational
:= No_Rational
;
2645 if Is_Real_Type
(Btyp
) then
2646 R_Value
:= Create_Rational_From
(R
, False);
2649 -- Check that the exponent is not an integer
2651 if R_Value
/= No_Rational
and then R_Value
.Denominator
/= 1 then
2652 Eval_Op_Expon_With_Rational_Exponent
(N
, R_Value
);
2656 end Eval_Op_Expon_For_Dimensioned_Type
;
2658 ------------------------------------------
2659 -- Eval_Op_Expon_With_Rational_Exponent --
2660 ------------------------------------------
2662 -- For dimensioned operand in exponentiation, exponent is allowed to be a
2663 -- Rational and not only an Integer like for dimensionless operands. For
2664 -- that particular case, the left operand is rewritten as a function call
2665 -- using the function Expon_LLF from s-llflex.ads.
2667 procedure Eval_Op_Expon_With_Rational_Exponent
2669 Exponent_Value
: Rational
)
2671 Loc
: constant Source_Ptr
:= Sloc
(N
);
2672 Dims_Of_N
: constant Dimension_Type
:= Dimensions_Of
(N
);
2673 L
: constant Node_Id
:= Left_Opnd
(N
);
2674 Etyp_Of_L
: constant Entity_Id
:= Etype
(L
);
2675 Btyp_Of_L
: constant Entity_Id
:= Base_Type
(Etyp_Of_L
);
2678 Dim_Power
: Rational
;
2679 List_Of_Dims
: List_Id
;
2680 New_Aspect
: Node_Id
;
2681 New_Aspects
: List_Id
;
2684 New_Subtyp_Decl_For_L
: Node_Id
;
2685 System
: System_Type
;
2688 -- Case when the operand is not dimensionless
2690 if Exists
(Dims_Of_N
) then
2692 -- Get the corresponding System_Type to know the exact number of
2693 -- dimensions in the system.
2695 System
:= System_Of
(Btyp_Of_L
);
2697 -- Generation of a new subtype with the proper dimensions
2699 -- In order to rewrite the operator as a type conversion, a new
2700 -- dimensioned subtype with the resulting dimensions of the
2701 -- exponentiation must be created.
2705 -- Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2706 -- System : constant System_Id :=
2707 -- Get_Dimension_System_Id (Btyp_Of_L);
2708 -- Num_Of_Dims : constant Number_Of_Dimensions :=
2709 -- Dimension_Systems.Table (System).Dimension_Count;
2711 -- subtype T is Btyp_Of_L
2714 -- Dims_Of_N (1).Numerator / Dims_Of_N (1).Denominator,
2715 -- Dims_Of_N (2).Numerator / Dims_Of_N (2).Denominator,
2717 -- Dims_Of_N (Num_Of_Dims).Numerator /
2718 -- Dims_Of_N (Num_Of_Dims).Denominator);
2720 -- Step 1: Generate the new aggregate for the aspect Dimension
2722 New_Aspects
:= Empty_List
;
2724 List_Of_Dims
:= New_List
;
2725 for Position
in Dims_Of_N
'First .. System
.Count
loop
2726 Dim_Power
:= Dims_Of_N
(Position
);
2727 Append_To
(List_Of_Dims
,
2728 Make_Op_Divide
(Loc
,
2730 Make_Integer_Literal
(Loc
, Int
(Dim_Power
.Numerator
)),
2732 Make_Integer_Literal
(Loc
, Int
(Dim_Power
.Denominator
))));
2735 -- Step 2: Create the new Aspect Specification for Aspect Dimension
2738 Make_Aspect_Specification
(Loc
,
2739 Identifier
=> Make_Identifier
(Loc
, Name_Dimension
),
2740 Expression
=> Make_Aggregate
(Loc
, Expressions
=> List_Of_Dims
));
2742 -- Step 3: Make a temporary identifier for the new subtype
2744 New_Id
:= Make_Temporary
(Loc
, 'T');
2745 Set_Is_Internal
(New_Id
);
2747 -- Step 4: Declaration of the new subtype
2749 New_Subtyp_Decl_For_L
:=
2750 Make_Subtype_Declaration
(Loc
,
2751 Defining_Identifier
=> New_Id
,
2752 Subtype_Indication
=> New_Occurrence_Of
(Btyp_Of_L
, Loc
));
2754 Append
(New_Aspect
, New_Aspects
);
2755 Set_Parent
(New_Aspects
, New_Subtyp_Decl_For_L
);
2756 Set_Aspect_Specifications
(New_Subtyp_Decl_For_L
, New_Aspects
);
2758 Analyze
(New_Subtyp_Decl_For_L
);
2760 -- Case where the operand is dimensionless
2763 New_Id
:= Btyp_Of_L
;
2766 -- Replacement of N by New_N
2770 -- Actual_1 := Long_Long_Float (L),
2772 -- Actual_2 := Long_Long_Float (Exponent_Value.Numerator) /
2773 -- Long_Long_Float (Exponent_Value.Denominator);
2775 -- (T (Expon_LLF (Actual_1, Actual_2)));
2777 -- where T is the subtype declared in step 1
2779 -- The node is rewritten as a type conversion
2781 -- Step 1: Creation of the two parameters of Expon_LLF function call
2784 Make_Type_Conversion
(Loc
,
2785 Subtype_Mark
=> New_Occurrence_Of
(Standard_Long_Long_Float
, Loc
),
2786 Expression
=> Relocate_Node
(L
));
2789 Make_Op_Divide
(Loc
,
2791 Make_Real_Literal
(Loc
,
2792 UR_From_Uint
(UI_From_Int
(Int
(Exponent_Value
.Numerator
)))),
2794 Make_Real_Literal
(Loc
,
2795 UR_From_Uint
(UI_From_Int
(Int
(Exponent_Value
.Denominator
)))));
2797 -- Step 2: Creation of New_N
2800 Make_Type_Conversion
(Loc
,
2801 Subtype_Mark
=> New_Occurrence_Of
(New_Id
, Loc
),
2803 Make_Function_Call
(Loc
,
2804 Name
=> New_Occurrence_Of
(RTE
(RE_Expon_LLF
), Loc
),
2805 Parameter_Associations
=> New_List
(
2806 Actual_1
, Actual_2
)));
2808 -- Step 3: Rewrite N with the result
2811 Set_Etype
(N
, New_Id
);
2812 Analyze_And_Resolve
(N
, New_Id
);
2813 end Eval_Op_Expon_With_Rational_Exponent
;
2819 function Exists
(Dim
: Dimension_Type
) return Boolean is
2821 return Dim
/= Null_Dimension
;
2824 function Exists
(Str
: String_Id
) return Boolean is
2826 return Str
/= No_String
;
2829 function Exists
(Sys
: System_Type
) return Boolean is
2831 return Sys
/= Null_System
;
2834 ---------------------------------
2835 -- Expand_Put_Call_With_Symbol --
2836 ---------------------------------
2838 -- For procedure Put (resp. Put_Dim_Of) and function Image, defined in
2839 -- System.Dim.Float_IO or System.Dim.Integer_IO, the default string
2840 -- parameter is rewritten to include the unit symbol (or the dimension
2841 -- symbols if not a defined quantity) in the output of a dimensioned
2842 -- object. If a value is already supplied by the user for the parameter
2843 -- Symbol, it is used as is.
2845 -- Case 1. Item is dimensionless
2847 -- * Put : Item appears without a suffix
2849 -- * Put_Dim_Of : the output is []
2851 -- Obj : Mks_Type := 2.6;
2852 -- Put (Obj, 1, 1, 0);
2853 -- Put_Dim_Of (Obj);
2855 -- The corresponding outputs are:
2859 -- Case 2. Item has a dimension
2861 -- * Put : If the type of Item is a dimensioned subtype whose
2862 -- symbol is not empty, then the symbol appears as a
2863 -- suffix. Otherwise, a new string is created and appears
2864 -- as a suffix of Item. This string results in the
2865 -- successive concatanations between each unit symbol
2866 -- raised by its corresponding dimension power from the
2867 -- dimensions of Item.
2869 -- * Put_Dim_Of : The output is a new string resulting in the successive
2870 -- concatanations between each dimension symbol raised by
2871 -- its corresponding dimension power from the dimensions of
2874 -- subtype Random is Mks_Type
2881 -- Obj : Random := 5.0;
2883 -- Put_Dim_Of (Obj);
2885 -- The corresponding outputs are:
2886 -- $5.0 m**3.cd**(-1)
2889 -- The function Image returns the string identical to that produced by
2890 -- a call to Put whose first parameter is a string.
2892 procedure Expand_Put_Call_With_Symbol
(N
: Node_Id
) is
2893 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
2894 Loc
: constant Source_Ptr
:= Sloc
(N
);
2895 Name_Call
: constant Node_Id
:= Name
(N
);
2896 New_Actuals
: constant List_Id
:= New_List
;
2898 Dims_Of_Actual
: Dimension_Type
;
2900 New_Str_Lit
: Node_Id
:= Empty
;
2901 Symbols
: String_Id
;
2903 Is_Put_Dim_Of
: Boolean := False;
2904 -- This flag is used in order to differentiate routines Put and
2905 -- Put_Dim_Of. Set to True if the procedure is one of the Put_Dim_Of
2906 -- defined in System.Dim.Float_IO or System.Dim.Integer_IO.
2908 function Has_Symbols
return Boolean;
2909 -- Return True if the current Put call already has a parameter
2910 -- association for parameter "Symbols" with the correct string of
2913 function Is_Procedure_Put_Call
return Boolean;
2914 -- Return True if the current call is a call of an instantiation of a
2915 -- procedure Put defined in the package System.Dim.Float_IO and
2916 -- System.Dim.Integer_IO.
2918 function Item_Actual
return Node_Id
;
2919 -- Return the item actual parameter node in the output call
2925 function Has_Symbols
return Boolean is
2927 Actual_Str
: Node_Id
;
2930 -- Look for a symbols parameter association in the list of actuals
2932 Actual
:= First
(Actuals
);
2933 while Present
(Actual
) loop
2935 -- Positional parameter association case when the actual is a
2938 if Nkind
(Actual
) = N_String_Literal
then
2939 Actual_Str
:= Actual
;
2941 -- Named parameter association case when selector name is Symbol
2943 elsif Nkind
(Actual
) = N_Parameter_Association
2944 and then Chars
(Selector_Name
(Actual
)) = Name_Symbol
2946 Actual_Str
:= Explicit_Actual_Parameter
(Actual
);
2948 -- Ignore all other cases
2951 Actual_Str
:= Empty
;
2954 if Present
(Actual_Str
) then
2956 -- Return True if the actual comes from source or if the string
2957 -- of symbols doesn't have the default value (i.e. it is ""),
2958 -- in which case it is used as suffix of the generated string.
2960 if Comes_From_Source
(Actual
)
2961 or else String_Length
(Strval
(Actual_Str
)) /= 0
2973 -- At this point, the call has no parameter association. Look to the
2974 -- last actual since the symbols parameter is the last one.
2976 return Nkind
(Last
(Actuals
)) = N_String_Literal
;
2979 ---------------------------
2980 -- Is_Procedure_Put_Call --
2981 ---------------------------
2983 function Is_Procedure_Put_Call
return Boolean is
2988 -- There are three different Put (resp. Put_Dim_Of) routines in each
2989 -- generic dim IO package. Verify the current procedure call is one
2992 if Is_Entity_Name
(Name_Call
) then
2993 Ent
:= Entity
(Name_Call
);
2995 -- Get the original subprogram entity following the renaming chain
2997 if Present
(Alias
(Ent
)) then
3003 -- Check the name of the entity subprogram is Put (resp.
3004 -- Put_Dim_Of) and verify this entity is located in either
3005 -- System.Dim.Float_IO or System.Dim.Integer_IO.
3007 if Loc
> No_Location
3008 and then Is_Dim_IO_Package_Entity
3009 (Cunit_Entity
(Get_Source_Unit
(Loc
)))
3011 if Chars
(Ent
) = Name_Put_Dim_Of
then
3012 Is_Put_Dim_Of
:= True;
3015 elsif Chars
(Ent
) = Name_Put
3016 or else Chars
(Ent
) = Name_Image
3024 end Is_Procedure_Put_Call
;
3030 function Item_Actual
return Node_Id
is
3034 -- Look for the item actual as a parameter association
3036 Actual
:= First
(Actuals
);
3037 while Present
(Actual
) loop
3038 if Nkind
(Actual
) = N_Parameter_Association
3039 and then Chars
(Selector_Name
(Actual
)) = Name_Item
3041 return Explicit_Actual_Parameter
(Actual
);
3047 -- Case where the item has been defined without an association
3049 Actual
:= First
(Actuals
);
3051 -- Depending on the procedure Put, Item actual could be first or
3052 -- second in the list of actuals.
3054 if Has_Dimension_System
(Base_Type
(Etype
(Actual
))) then
3057 return Next
(Actual
);
3061 -- Start of processing for Expand_Put_Call_With_Symbol
3064 if Is_Procedure_Put_Call
and then not Has_Symbols
then
3065 Actual
:= Item_Actual
;
3066 Dims_Of_Actual
:= Dimensions_Of
(Actual
);
3067 Etyp
:= Etype
(Actual
);
3071 if Is_Put_Dim_Of
then
3073 -- Check that the item is not dimensionless
3075 -- Create the new String_Literal with the new String_Id generated
3076 -- by the routine From_Dim_To_Str_Of_Dim_Symbols.
3078 if Exists
(Dims_Of_Actual
) then
3080 Make_String_Literal
(Loc
,
3081 From_Dim_To_Str_Of_Dim_Symbols
3082 (Dims_Of_Actual
, System_Of
(Base_Type
(Etyp
))));
3084 -- If dimensionless, the output is []
3088 Make_String_Literal
(Loc
, "[]");
3094 -- Add the symbol as a suffix of the value if the subtype has a
3095 -- unit symbol or if the parameter is not dimensionless.
3097 if Exists
(Symbol_Of
(Etyp
)) then
3098 Symbols
:= Symbol_Of
(Etyp
);
3100 Symbols
:= From_Dim_To_Str_Of_Unit_Symbols
3101 (Dims_Of_Actual
, System_Of
(Base_Type
(Etyp
)));
3104 -- Check Symbols exists
3106 if Exists
(Symbols
) then
3109 -- Put a space between the value and the dimension
3111 Store_String_Char
(' ');
3112 Store_String_Chars
(Symbols
);
3113 New_Str_Lit
:= Make_String_Literal
(Loc
, End_String
);
3117 if Present
(New_Str_Lit
) then
3119 -- Insert all actuals in New_Actuals
3121 Actual
:= First
(Actuals
);
3122 while Present
(Actual
) loop
3124 -- Copy every actuals in New_Actuals except the Symbols
3125 -- parameter association.
3127 if Nkind
(Actual
) = N_Parameter_Association
3128 and then Chars
(Selector_Name
(Actual
)) /= Name_Symbol
3130 Append_To
(New_Actuals
,
3131 Make_Parameter_Association
(Loc
,
3132 Selector_Name
=> New_Copy
(Selector_Name
(Actual
)),
3133 Explicit_Actual_Parameter
=>
3134 New_Copy
(Explicit_Actual_Parameter
(Actual
))));
3136 elsif Nkind
(Actual
) /= N_Parameter_Association
then
3137 Append_To
(New_Actuals
, New_Copy
(Actual
));
3143 -- Create new Symbols param association and append to New_Actuals
3145 Append_To
(New_Actuals
,
3146 Make_Parameter_Association
(Loc
,
3147 Selector_Name
=> Make_Identifier
(Loc
, Name_Symbol
),
3148 Explicit_Actual_Parameter
=> New_Str_Lit
));
3150 -- Rewrite and analyze the procedure call
3152 if Chars
(Name_Call
) = Name_Image
then
3154 Make_Function_Call
(Loc
,
3155 Name
=> New_Copy
(Name_Call
),
3156 Parameter_Associations
=> New_Actuals
));
3157 Analyze_And_Resolve
(N
);
3160 Make_Procedure_Call_Statement
(Loc
,
3161 Name
=> New_Copy
(Name_Call
),
3162 Parameter_Associations
=> New_Actuals
));
3168 end Expand_Put_Call_With_Symbol
;
3170 ------------------------------------
3171 -- From_Dim_To_Str_Of_Dim_Symbols --
3172 ------------------------------------
3174 -- Given a dimension vector and the corresponding dimension system, create
3175 -- a String_Id to output dimension symbols corresponding to the dimensions
3176 -- Dims. If In_Error_Msg is True, there is a special handling for character
3177 -- asterisk * which is an insertion character in error messages.
3179 function From_Dim_To_Str_Of_Dim_Symbols
3180 (Dims
: Dimension_Type
;
3181 System
: System_Type
;
3182 In_Error_Msg
: Boolean := False) return String_Id
3184 Dim_Power
: Rational
;
3185 First_Dim
: Boolean := True;
3187 procedure Store_String_Oexpon
;
3188 -- Store the expon operator symbol "**" in the string. In error
3189 -- messages, asterisk * is a special character and must be quoted
3190 -- to be placed literally into the message.
3192 -------------------------
3193 -- Store_String_Oexpon --
3194 -------------------------
3196 procedure Store_String_Oexpon
is
3198 if In_Error_Msg
then
3199 Store_String_Chars
("'*'*");
3201 Store_String_Chars
("**");
3203 end Store_String_Oexpon
;
3205 -- Start of processing for From_Dim_To_Str_Of_Dim_Symbols
3208 -- Initialization of the new String_Id
3212 -- Store the dimension symbols inside boxes
3214 if In_Error_Msg
then
3215 Store_String_Chars
("'[");
3217 Store_String_Char
('[');
3220 for Position
in Dimension_Type
'Range loop
3221 Dim_Power
:= Dims
(Position
);
3222 if Dim_Power
/= Zero
then
3227 Store_String_Char
('.');
3230 Store_String_Chars
(System
.Dim_Symbols
(Position
));
3232 -- Positive dimension case
3234 if Dim_Power
.Numerator
> 0 then
3238 if Dim_Power
.Denominator
= 1 then
3239 if Dim_Power
.Numerator
/= 1 then
3240 Store_String_Oexpon
;
3241 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3244 -- Rational case when denominator /= 1
3247 Store_String_Oexpon
;
3248 Store_String_Char
('(');
3249 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3250 Store_String_Char
('/');
3251 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3252 Store_String_Char
(')');
3255 -- Negative dimension case
3258 Store_String_Oexpon
;
3259 Store_String_Char
('(');
3260 Store_String_Char
('-');
3261 Store_String_Int
(Int
(-Dim_Power
.Numerator
));
3265 if Dim_Power
.Denominator
= 1 then
3266 Store_String_Char
(')');
3268 -- Rational case when denominator /= 1
3271 Store_String_Char
('/');
3272 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3273 Store_String_Char
(')');
3279 if In_Error_Msg
then
3280 Store_String_Chars
("']");
3282 Store_String_Char
(']');
3286 end From_Dim_To_Str_Of_Dim_Symbols
;
3288 -------------------------------------
3289 -- From_Dim_To_Str_Of_Unit_Symbols --
3290 -------------------------------------
3292 -- Given a dimension vector and the corresponding dimension system,
3293 -- create a String_Id to output the unit symbols corresponding to the
3296 function From_Dim_To_Str_Of_Unit_Symbols
3297 (Dims
: Dimension_Type
;
3298 System
: System_Type
) return String_Id
3300 Dim_Power
: Rational
;
3301 First_Dim
: Boolean := True;
3304 -- Return No_String if dimensionless
3306 if not Exists
(Dims
) then
3310 -- Initialization of the new String_Id
3314 for Position
in Dimension_Type
'Range loop
3315 Dim_Power
:= Dims
(Position
);
3317 if Dim_Power
/= Zero
then
3321 Store_String_Char
('.');
3324 Store_String_Chars
(System
.Unit_Symbols
(Position
));
3326 -- Positive dimension case
3328 if Dim_Power
.Numerator
> 0 then
3332 if Dim_Power
.Denominator
= 1 then
3333 if Dim_Power
.Numerator
/= 1 then
3334 Store_String_Chars
("**");
3335 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3338 -- Rational case when denominator /= 1
3341 Store_String_Chars
("**");
3342 Store_String_Char
('(');
3343 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3344 Store_String_Char
('/');
3345 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3346 Store_String_Char
(')');
3349 -- Negative dimension case
3352 Store_String_Chars
("**");
3353 Store_String_Char
('(');
3354 Store_String_Char
('-');
3355 Store_String_Int
(Int
(-Dim_Power
.Numerator
));
3359 if Dim_Power
.Denominator
= 1 then
3360 Store_String_Char
(')');
3362 -- Rational case when denominator /= 1
3365 Store_String_Char
('/');
3366 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3367 Store_String_Char
(')');
3374 end From_Dim_To_Str_Of_Unit_Symbols
;
3380 function GCD
(Left
, Right
: Whole
) return Int
is
3400 --------------------------
3401 -- Has_Dimension_System --
3402 --------------------------
3404 function Has_Dimension_System
(Typ
: Entity_Id
) return Boolean is
3406 return Exists
(System_Of
(Typ
));
3407 end Has_Dimension_System
;
3409 ------------------------------
3410 -- Is_Dim_IO_Package_Entity --
3411 ------------------------------
3413 function Is_Dim_IO_Package_Entity
(E
: Entity_Id
) return Boolean is
3415 -- Check the package entity corresponds to System.Dim.Float_IO or
3416 -- System.Dim.Integer_IO.
3419 Is_RTU
(E
, System_Dim_Float_IO
)
3421 Is_RTU
(E
, System_Dim_Integer_IO
);
3422 end Is_Dim_IO_Package_Entity
;
3424 -------------------------------------
3425 -- Is_Dim_IO_Package_Instantiation --
3426 -------------------------------------
3428 function Is_Dim_IO_Package_Instantiation
(N
: Node_Id
) return Boolean is
3429 Gen_Id
: constant Node_Id
:= Name
(N
);
3432 -- Check that the instantiated package is either System.Dim.Float_IO
3433 -- or System.Dim.Integer_IO.
3436 Is_Entity_Name
(Gen_Id
)
3437 and then Is_Dim_IO_Package_Entity
(Entity
(Gen_Id
));
3438 end Is_Dim_IO_Package_Instantiation
;
3444 function Is_Invalid
(Position
: Dimension_Position
) return Boolean is
3446 return Position
= Invalid_Position
;
3449 ---------------------
3450 -- Move_Dimensions --
3451 ---------------------
3453 procedure Move_Dimensions
(From
, To
: Node_Id
) is
3455 if Ada_Version
< Ada_2012
then
3459 -- Copy the dimension of 'From to 'To' and remove dimension of 'From'
3461 Copy_Dimensions
(From
, To
);
3462 Remove_Dimensions
(From
);
3463 end Move_Dimensions
;
3469 function Reduce
(X
: Rational
) return Rational
is
3471 if X
.Numerator
= 0 then
3476 G
: constant Int
:= GCD
(X
.Numerator
, X
.Denominator
);
3478 return Rational
'(Numerator => Whole (Int (X.Numerator) / G),
3479 Denominator => Whole (Int (X.Denominator) / G));
3483 -----------------------
3484 -- Remove_Dimensions --
3485 -----------------------
3487 procedure Remove_Dimensions (N : Node_Id) is
3488 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
3490 if Exists (Dims_Of_N) then
3491 Dimension_Table.Remove (N);
3493 end Remove_Dimensions;
3495 -----------------------------------
3496 -- Remove_Dimension_In_Statement --
3497 -----------------------------------
3499 -- Removal of dimension in statement as part of the Analyze_Statements
3500 -- routine (see package Sem_Ch5).
3502 procedure Remove_Dimension_In_Statement (Stmt : Node_Id) is
3504 if Ada_Version < Ada_2012 then
3508 -- Remove dimension in parameter specifications for accept statement
3510 if Nkind (Stmt) = N_Accept_Statement then
3512 Param : Node_Id := First (Parameter_Specifications (Stmt));
3514 while Present (Param) loop
3515 Remove_Dimensions (Param);
3520 -- Remove dimension of name and expression in assignments
3522 elsif Nkind (Stmt) = N_Assignment_Statement then
3523 Remove_Dimensions (Expression (Stmt));
3524 Remove_Dimensions (Name (Stmt));
3526 end Remove_Dimension_In_Statement;
3528 --------------------
3529 -- Set_Dimensions --
3530 --------------------
3532 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type) is
3534 pragma Assert (OK_For_Dimension (Nkind (N)));
3535 pragma Assert (Exists (Val));
3537 Dimension_Table.Set (N, Val);
3544 procedure Set_Symbol (E : Entity_Id; Val : String_Id) is
3546 Symbol_Table.Set (E, Val);
3549 ---------------------------------
3550 -- String_From_Numeric_Literal --
3551 ---------------------------------
3553 function String_From_Numeric_Literal (N : Node_Id) return String_Id is
3554 Loc : constant Source_Ptr := Sloc (N);
3555 Sbuffer : constant Source_Buffer_Ptr :=
3556 Source_Text (Get_Source_File_Index (Loc));
3557 Src_Ptr : Source_Ptr := Loc;
3559 C : Character := Sbuffer (Src_Ptr);
3560 -- Current source program character
3562 function Belong_To_Numeric_Literal (C : Character) return Boolean;
3563 -- Return True if C belongs to a numeric literal
3565 -------------------------------
3566 -- Belong_To_Numeric_Literal --
3567 -------------------------------
3569 function Belong_To_Numeric_Literal (C : Character) return Boolean is
3573 | '_
' | '.' | 'e
' | '#
' | 'A
' | 'B
' | 'C
' | 'D
' | 'E
' | 'F
'
3577 -- Make sure '+' or '-' is part of an exponent.
3581 Prev_C : constant Character := Sbuffer (Src_Ptr - 1);
3583 return Prev_C = 'e
' or else Prev_C = 'E
';
3586 -- All other character doesn't belong to a numeric literal
3591 end Belong_To_Numeric_Literal;
3593 -- Start of processing for String_From_Numeric_Literal
3597 while Belong_To_Numeric_Literal (C) loop
3598 Store_String_Char (C);
3599 Src_Ptr := Src_Ptr + 1;
3600 C := Sbuffer (Src_Ptr);
3604 end String_From_Numeric_Literal;
3610 function Symbol_Of (E : Entity_Id) return String_Id is
3611 Subtype_Symbol : constant String_Id := Symbol_Table.Get (E);
3613 if Subtype_Symbol /= No_String then
3614 return Subtype_Symbol;
3616 return From_Dim_To_Str_Of_Unit_Symbols
3617 (Dimensions_Of (E), System_Of (Base_Type (E)));
3621 -----------------------
3622 -- Symbol_Table_Hash --
3623 -----------------------
3625 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range is
3627 return Symbol_Table_Range (Key mod 511);
3628 end Symbol_Table_Hash;
3634 function System_Of (E : Entity_Id) return System_Type is
3635 Type_Decl : constant Node_Id := Parent (E);
3638 -- Look for Type_Decl in System_Table
3640 for Dim_Sys in 1 .. System_Table.Last loop
3641 if Type_Decl = System_Table.Table (Dim_Sys).Type_Decl then
3642 return System_Table.Table (Dim_Sys);