1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2011-2015, 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.
1125 if Ada_Version < Ada_2012 or else not Comes_From_Source (N) then
1130 when N_Assignment_Statement =>
1131 Analyze_Dimension_Assignment_Statement (N);
1134 Analyze_Dimension_Binary_Op (N);
1136 when N_Component_Declaration =>
1137 Analyze_Dimension_Component_Declaration (N);
1139 when N_Extended_Return_Statement =>
1140 Analyze_Dimension_Extended_Return_Statement (N);
1142 when N_Attribute_Reference |
1144 N_Explicit_Dereference |
1147 N_Indexed_Component |
1148 N_Qualified_Expression |
1149 N_Selected_Component |
1152 N_Unchecked_Type_Conversion =>
1153 Analyze_Dimension_Has_Etype (N);
1155 when N_Number_Declaration =>
1156 Analyze_Dimension_Number_Declaration (N);
1158 when N_Object_Declaration =>
1159 Analyze_Dimension_Object_Declaration (N);
1161 when N_Object_Renaming_Declaration =>
1162 Analyze_Dimension_Object_Renaming_Declaration (N);
1164 when N_Simple_Return_Statement =>
1165 if not Comes_From_Extended_Return_Statement (N) then
1166 Analyze_Dimension_Simple_Return_Statement (N);
1169 when N_Subtype_Declaration =>
1170 Analyze_Dimension_Subtype_Declaration (N);
1173 Analyze_Dimension_Unary_Op (N);
1175 when others => null;
1178 end Analyze_Dimension;
1180 ---------------------------------------
1181 -- Analyze_Dimension_Array_Aggregate --
1182 ---------------------------------------
1184 procedure Analyze_Dimension_Array_Aggregate
1186 Comp_Typ : Entity_Id)
1188 Comp_Ass : constant List_Id := Component_Associations (N);
1189 Dims_Of_Comp_Typ : constant Dimension_Type := Dimensions_Of (Comp_Typ);
1190 Exps : constant List_Id := Expressions (N);
1195 Error_Detected : Boolean := False;
1196 -- This flag is used in order to indicate if an error has been detected
1197 -- so far by the compiler in this routine.
1200 -- Aspect is an Ada 2012 feature. Nothing to do here if the component
1201 -- base type is not a dimensioned type.
1203 -- Note that here the original node must come from source since the
1204 -- original array aggregate may not have been entirely decorated.
1206 if Ada_Version < Ada_2012
1207 or else not Comes_From_Source (Original_Node (N))
1208 or else not Has_Dimension_System (Base_Type (Comp_Typ))
1213 -- Check whether there is any positional component association
1215 if Is_Empty_List (Exps) then
1216 Comp := First (Comp_Ass);
1218 Comp := First (Exps);
1221 while Present (Comp) loop
1223 -- Get the expression from the component
1225 if Nkind (Comp) = N_Component_Association then
1226 Expr := Expression (Comp);
1231 -- Issue an error if the dimensions of the component type and the
1232 -- dimensions of the component mismatch.
1234 -- Note that we must ensure the expression has been fully analyzed
1235 -- since it may not be decorated at this point. We also don't want to
1236 -- issue the same error message multiple times on the same expression
1237 -- (may happen when an aggregate is converted into a positional
1240 if Comes_From_Source (Original_Node (Expr))
1241 and then Present (Etype (Expr))
1242 and then Dimensions_Of (Expr) /= Dims_Of_Comp_Typ
1243 and then Sloc (Comp) /= Sloc (Prev (Comp))
1245 -- Check if an error has already been encountered so far
1247 if not Error_Detected then
1248 Error_Msg_N ("dimensions mismatch in array aggregate", N);
1249 Error_Detected := True;
1253 ("\expected dimension " & Dimensions_Msg_Of (Comp_Typ)
1254 & ", found " & Dimensions_Msg_Of (Expr), Expr);
1257 -- Look at the named components right after the positional components
1259 if not Present (Next (Comp))
1260 and then List_Containing (Comp) = Exps
1262 Comp := First (Comp_Ass);
1267 end Analyze_Dimension_Array_Aggregate;
1269 --------------------------------------------
1270 -- Analyze_Dimension_Assignment_Statement --
1271 --------------------------------------------
1273 procedure Analyze_Dimension_Assignment_Statement (N : Node_Id) is
1274 Lhs : constant Node_Id := Name (N);
1275 Dims_Of_Lhs : constant Dimension_Type := Dimensions_Of (Lhs);
1276 Rhs : constant Node_Id := Expression (N);
1277 Dims_Of_Rhs : constant Dimension_Type := Dimensions_Of (Rhs);
1279 procedure Error_Dim_Msg_For_Assignment_Statement
1283 -- Error using Error_Msg_N at node N. Output the dimensions of left
1284 -- and right hand sides.
1286 --------------------------------------------
1287 -- Error_Dim_Msg_For_Assignment_Statement --
1288 --------------------------------------------
1290 procedure Error_Dim_Msg_For_Assignment_Statement
1296 Error_Msg_N ("dimensions mismatch in assignment", N);
1297 Error_Msg_N ("\left-hand side " & Dimensions_Msg_Of (Lhs, True), N);
1298 Error_Msg_N ("\right-hand side " & Dimensions_Msg_Of (Rhs, True), N);
1299 end Error_Dim_Msg_For_Assignment_Statement;
1301 -- Start of processing for Analyze_Dimension_Assignment
1304 if Dims_Of_Lhs /= Dims_Of_Rhs then
1305 Error_Dim_Msg_For_Assignment_Statement (N, Lhs, Rhs);
1307 end Analyze_Dimension_Assignment_Statement;
1309 ---------------------------------
1310 -- Analyze_Dimension_Binary_Op --
1311 ---------------------------------
1313 -- Check and propagate the dimensions for binary operators
1314 -- Note that when the dimensions mismatch, no dimension is propagated to N.
1316 procedure Analyze_Dimension_Binary_Op (N : Node_Id) is
1317 N_Kind : constant Node_Kind := Nkind (N);
1319 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type;
1320 -- If the operand is a numeric literal that comes from a declared
1321 -- constant, use the dimensions of the constant which were computed
1322 -- from the expression of the constant declaration.
1324 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id);
1325 -- Error using Error_Msg_NE and Error_Msg_N at node N. Output the
1326 -- dimensions of both operands.
1328 ---------------------------
1329 -- Dimensions_Of_Operand --
1330 ---------------------------
1332 function Dimensions_Of_Operand (N : Node_Id) return Dimension_Type is
1334 if Nkind (N) = N_Real_Literal
1335 and then Present (Original_Entity (N))
1337 return Dimensions_Of (Original_Entity (N));
1339 return Dimensions_Of (N);
1341 end Dimensions_Of_Operand;
1343 ---------------------------------
1344 -- Error_Dim_Msg_For_Binary_Op --
1345 ---------------------------------
1347 procedure Error_Dim_Msg_For_Binary_Op (N, L, R : Node_Id) is
1350 ("both operands for operation& must have same dimensions",
1352 Error_Msg_N ("\left operand " & Dimensions_Msg_Of (L, True), N);
1353 Error_Msg_N ("\right operand " & Dimensions_Msg_Of (R, True), N);
1354 end Error_Dim_Msg_For_Binary_Op;
1356 -- Start of processing for Analyze_Dimension_Binary_Op
1359 if Nkind_In (N_Kind, N_Op_Add, N_Op_Expon, N_Op_Subtract)
1360 or else N_Kind in N_Multiplying_Operator
1361 or else N_Kind in N_Op_Compare
1364 L : constant Node_Id := Left_Opnd (N);
1365 Dims_Of_L : constant Dimension_Type :=
1366 Dimensions_Of_Operand (L);
1367 L_Has_Dimensions : constant Boolean := Exists (Dims_Of_L);
1368 R : constant Node_Id := Right_Opnd (N);
1369 Dims_Of_R : constant Dimension_Type :=
1370 Dimensions_Of_Operand (R);
1371 R_Has_Dimensions : constant Boolean := Exists (Dims_Of_R);
1372 Dims_Of_N : Dimension_Type := Null_Dimension;
1375 -- N_Op_Add, N_Op_Mod, N_Op_Rem or N_Op_Subtract case
1377 if Nkind_In (N, N_Op_Add, N_Op_Mod, N_Op_Rem, N_Op_Subtract) then
1379 -- Check both operands have same dimension
1381 if Dims_Of_L /= Dims_Of_R then
1382 Error_Dim_Msg_For_Binary_Op (N, L, R);
1384 -- Check both operands are not dimensionless
1386 if Exists (Dims_Of_L) then
1387 Set_Dimensions (N, Dims_Of_L);
1391 -- N_Op_Multiply or N_Op_Divide case
1393 elsif Nkind_In (N_Kind, N_Op_Multiply, N_Op_Divide) then
1395 -- Check at least one operand is not dimensionless
1397 if L_Has_Dimensions or R_Has_Dimensions then
1399 -- Multiplication case
1401 -- Get both operands dimensions and add them
1403 if N_Kind = N_Op_Multiply then
1404 for Position in Dimension_Type'Range loop
1405 Dims_Of_N (Position) :=
1406 Dims_Of_L (Position) + Dims_Of_R (Position);
1411 -- Get both operands dimensions and subtract them
1414 for Position in Dimension_Type'Range loop
1415 Dims_Of_N (Position) :=
1416 Dims_Of_L (Position) - Dims_Of_R (Position);
1420 if Exists (Dims_Of_N) then
1421 Set_Dimensions (N, Dims_Of_N);
1425 -- Exponentiation case
1427 -- Note: a rational exponent is allowed for dimensioned operand
1429 elsif N_Kind = N_Op_Expon then
1431 -- Check the left operand is not dimensionless. Note that the
1432 -- value of the exponent must be known compile time. Otherwise,
1433 -- the exponentiation evaluation will return an error message.
1435 if L_Has_Dimensions then
1436 if not Compile_Time_Known_Value (R) then
1438 ("exponent of dimensioned operand must be "
1439 & "known at compile time", N);
1443 Exponent_Value : Rational := Zero;
1446 -- Real operand case
1448 if Is_Real_Type (Etype (L)) then
1450 -- Define the exponent as a Rational number
1452 Exponent_Value := Create_Rational_From (R, False);
1454 -- Verify that the exponent cannot be interpreted
1455 -- as a rational, otherwise interpret the exponent
1458 if Exponent_Value = No_Rational then
1460 +Whole (UI_To_Int (Expr_Value (R)));
1463 -- Integer operand case.
1465 -- For integer operand, the exponent cannot be
1466 -- interpreted as a rational.
1469 Exponent_Value := +Whole (UI_To_Int (Expr_Value (R)));
1472 for Position in Dimension_Type'Range loop
1473 Dims_Of_N (Position) :=
1474 Dims_Of_L (Position) * Exponent_Value;
1477 if Exists (Dims_Of_N) then
1478 Set_Dimensions (N, Dims_Of_N);
1485 -- For relational operations, only dimension checking is
1486 -- performed (no propagation). If one operand is the result
1487 -- of constant folding the dimensions may have been lost
1488 -- in a tree copy, so assume that pre-analysis has verified
1489 -- that dimensions are correct.
1491 elsif N_Kind in N_Op_Compare then
1492 if (L_Has_Dimensions or R_Has_Dimensions)
1493 and then Dims_Of_L /= Dims_Of_R
1495 if Nkind (L) = N_Real_Literal
1496 and then not (Comes_From_Source (L))
1497 and then Expander_Active
1501 elsif Nkind (R) = N_Real_Literal
1502 and then not (Comes_From_Source (R))
1503 and then Expander_Active
1508 Error_Dim_Msg_For_Binary_Op (N, L, R);
1513 -- If expander is active, remove dimension information from each
1514 -- operand, as only dimensions of result are relevant.
1516 if Expander_Active then
1517 Remove_Dimensions (L);
1518 Remove_Dimensions (R);
1522 end Analyze_Dimension_Binary_Op;
1524 ----------------------------
1525 -- Analyze_Dimension_Call --
1526 ----------------------------
1528 procedure Analyze_Dimension_Call (N : Node_Id; Nam : Entity_Id) is
1529 Actuals : constant List_Id := Parameter_Associations (N);
1531 Dims_Of_Formal : Dimension_Type;
1533 Formal_Typ : Entity_Id;
1535 Error_Detected : Boolean := False;
1536 -- This flag is used in order to indicate if an error has been detected
1537 -- so far by the compiler in this routine.
1540 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1541 -- dimensions for calls that don't come from source, or those that may
1542 -- have semantic errors.
1544 if Ada_Version < Ada_2012
1545 or else not Comes_From_Source (N)
1546 or else Error_Posted (N)
1551 -- Check the dimensions of the actuals, if any
1553 if not Is_Empty_List (Actuals) then
1555 -- Special processing for elementary functions
1557 -- For Sqrt call, the resulting dimensions equal to half the
1558 -- dimensions of the actual. For all other elementary calls, this
1559 -- routine check that every actual is dimensionless.
1561 if Nkind (N) = N_Function_Call then
1562 Elementary_Function_Calls : declare
1563 Dims_Of_Call : Dimension_Type;
1564 Ent : Entity_Id := Nam;
1566 function Is_Elementary_Function_Entity
1567 (Sub_Id : Entity_Id) return Boolean;
1568 -- Given Sub_Id, the original subprogram entity, return True
1569 -- if call is to an elementary function (see Ada.Numerics.
1570 -- Generic_Elementary_Functions).
1572 -----------------------------------
1573 -- Is_Elementary_Function_Entity --
1574 -----------------------------------
1576 function Is_Elementary_Function_Entity
1577 (Sub_Id : Entity_Id) return Boolean
1579 Loc : constant Source_Ptr := Sloc (Sub_Id);
1582 -- Is entity in Ada.Numerics.Generic_Elementary_Functions?
1588 (Cunit_Entity (Get_Source_Unit (Loc)),
1589 Ada_Numerics_Generic_Elementary_Functions);
1590 end Is_Elementary_Function_Entity;
1592 -- Start of processing for Elementary_Function_Calls
1595 -- Get original subprogram entity following the renaming chain
1597 if Present (Alias (Ent)) then
1601 -- Check the call is an Elementary function call
1603 if Is_Elementary_Function_Entity (Ent) then
1605 -- Sqrt function call case
1607 if Chars (Ent) = Name_Sqrt then
1608 Dims_Of_Call := Dimensions_Of (First_Actual (N));
1610 -- Evaluates the resulting dimensions (i.e. half the
1611 -- dimensions of the actual).
1613 if Exists (Dims_Of_Call) then
1614 for Position in Dims_Of_Call'Range loop
1615 Dims_Of_Call (Position) :=
1616 Dims_Of_Call (Position) *
1617 Rational'(Numerator
=> 1, Denominator
=> 2);
1620 Set_Dimensions
(N
, Dims_Of_Call
);
1623 -- All other elementary functions case. Note that every
1624 -- actual here should be dimensionless.
1627 Actual
:= First_Actual
(N
);
1628 while Present
(Actual
) loop
1629 if Exists
(Dimensions_Of
(Actual
)) then
1631 -- Check if error has already been encountered
1633 if not Error_Detected
then
1635 ("dimensions mismatch in call of&",
1637 Error_Detected
:= True;
1641 ("\expected dimension '['], found "
1642 & Dimensions_Msg_Of
(Actual
), Actual
);
1645 Next_Actual
(Actual
);
1649 -- Nothing more to do for elementary functions
1653 end Elementary_Function_Calls
;
1656 -- General case. Check, for each parameter, the dimensions of the
1657 -- actual and its corresponding formal match. Otherwise, complain.
1659 Actual
:= First_Actual
(N
);
1660 Formal
:= First_Formal
(Nam
);
1661 while Present
(Formal
) loop
1663 -- A missing corresponding actual indicates that the analysis of
1664 -- the call was aborted due to a previous error.
1667 Check_Error_Detected
;
1671 Formal_Typ
:= Etype
(Formal
);
1672 Dims_Of_Formal
:= Dimensions_Of
(Formal_Typ
);
1674 -- If the formal is not dimensionless, check dimensions of formal
1675 -- and actual match. Otherwise, complain.
1677 if Exists
(Dims_Of_Formal
)
1678 and then Dimensions_Of
(Actual
) /= Dims_Of_Formal
1680 -- Check if an error has already been encountered so far
1682 if not Error_Detected
then
1683 Error_Msg_NE
("dimensions mismatch in& call", N
, Name
(N
));
1684 Error_Detected
:= True;
1688 ("\expected dimension " & Dimensions_Msg_Of
(Formal_Typ
)
1689 & ", found " & Dimensions_Msg_Of
(Actual
), Actual
);
1692 Next_Actual
(Actual
);
1693 Next_Formal
(Formal
);
1697 -- For function calls, propagate the dimensions from the returned type
1699 if Nkind
(N
) = N_Function_Call
then
1700 Analyze_Dimension_Has_Etype
(N
);
1702 end Analyze_Dimension_Call
;
1704 ---------------------------------------------
1705 -- Analyze_Dimension_Component_Declaration --
1706 ---------------------------------------------
1708 procedure Analyze_Dimension_Component_Declaration
(N
: Node_Id
) is
1709 Expr
: constant Node_Id
:= Expression
(N
);
1710 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1711 Etyp
: constant Entity_Id
:= Etype
(Id
);
1712 Dims_Of_Etyp
: constant Dimension_Type
:= Dimensions_Of
(Etyp
);
1713 Dims_Of_Expr
: Dimension_Type
;
1715 procedure Error_Dim_Msg_For_Component_Declaration
1719 -- Error using Error_Msg_N at node N. Output the dimensions of the
1720 -- type Etyp and the expression Expr of N.
1722 ---------------------------------------------
1723 -- Error_Dim_Msg_For_Component_Declaration --
1724 ---------------------------------------------
1726 procedure Error_Dim_Msg_For_Component_Declaration
1731 Error_Msg_N
("dimensions mismatch in component declaration", N
);
1733 ("\expected dimension " & Dimensions_Msg_Of
(Etyp
) & ", found "
1734 & Dimensions_Msg_Of
(Expr
), Expr
);
1735 end Error_Dim_Msg_For_Component_Declaration
;
1737 -- Start of processing for Analyze_Dimension_Component_Declaration
1740 -- Expression is present
1742 if Present
(Expr
) then
1743 Dims_Of_Expr
:= Dimensions_Of
(Expr
);
1745 -- Check dimensions match
1747 if Dims_Of_Etyp
/= Dims_Of_Expr
then
1749 -- Numeric literal case. Issue a warning if the object type is not
1750 -- dimensionless to indicate the literal is treated as if its
1751 -- dimension matches the type dimension.
1753 if Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
1756 Dim_Warning_For_Numeric_Literal
(Expr
, Etyp
);
1758 -- Issue a dimension mismatch error for all other cases
1761 Error_Dim_Msg_For_Component_Declaration
(N
, Etyp
, Expr
);
1765 end Analyze_Dimension_Component_Declaration
;
1767 -------------------------------------------------
1768 -- Analyze_Dimension_Extended_Return_Statement --
1769 -------------------------------------------------
1771 procedure Analyze_Dimension_Extended_Return_Statement
(N
: Node_Id
) is
1772 Return_Ent
: constant Entity_Id
:= Return_Statement_Entity
(N
);
1773 Return_Etyp
: constant Entity_Id
:=
1774 Etype
(Return_Applies_To
(Return_Ent
));
1775 Return_Obj_Decls
: constant List_Id
:= Return_Object_Declarations
(N
);
1776 Return_Obj_Decl
: Node_Id
;
1777 Return_Obj_Id
: Entity_Id
;
1778 Return_Obj_Typ
: Entity_Id
;
1780 procedure Error_Dim_Msg_For_Extended_Return_Statement
1782 Return_Etyp
: Entity_Id
;
1783 Return_Obj_Typ
: Entity_Id
);
1784 -- Error using Error_Msg_N at node N. Output dimensions of the returned
1785 -- type Return_Etyp and the returned object type Return_Obj_Typ of N.
1787 -------------------------------------------------
1788 -- Error_Dim_Msg_For_Extended_Return_Statement --
1789 -------------------------------------------------
1791 procedure Error_Dim_Msg_For_Extended_Return_Statement
1793 Return_Etyp
: Entity_Id
;
1794 Return_Obj_Typ
: Entity_Id
)
1797 Error_Msg_N
("dimensions mismatch in extended return statement", N
);
1799 ("\expected dimension " & Dimensions_Msg_Of
(Return_Etyp
)
1800 & ", found " & Dimensions_Msg_Of
(Return_Obj_Typ
), N
);
1801 end Error_Dim_Msg_For_Extended_Return_Statement
;
1803 -- Start of processing for Analyze_Dimension_Extended_Return_Statement
1806 if Present
(Return_Obj_Decls
) then
1807 Return_Obj_Decl
:= First
(Return_Obj_Decls
);
1808 while Present
(Return_Obj_Decl
) loop
1809 if Nkind
(Return_Obj_Decl
) = N_Object_Declaration
then
1810 Return_Obj_Id
:= Defining_Identifier
(Return_Obj_Decl
);
1812 if Is_Return_Object
(Return_Obj_Id
) then
1813 Return_Obj_Typ
:= Etype
(Return_Obj_Id
);
1815 -- Issue an error message if dimensions mismatch
1817 if Dimensions_Of
(Return_Etyp
) /=
1818 Dimensions_Of
(Return_Obj_Typ
)
1820 Error_Dim_Msg_For_Extended_Return_Statement
1821 (N
, Return_Etyp
, Return_Obj_Typ
);
1827 Next
(Return_Obj_Decl
);
1830 end Analyze_Dimension_Extended_Return_Statement
;
1832 -----------------------------------------------------
1833 -- Analyze_Dimension_Extension_Or_Record_Aggregate --
1834 -----------------------------------------------------
1836 procedure Analyze_Dimension_Extension_Or_Record_Aggregate
(N
: Node_Id
) is
1838 Comp_Id
: Entity_Id
;
1839 Comp_Typ
: Entity_Id
;
1842 Error_Detected
: Boolean := False;
1843 -- This flag is used in order to indicate if an error has been detected
1844 -- so far by the compiler in this routine.
1847 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1848 -- dimensions for aggregates that don't come from source, or if we are
1849 -- within an initialization procedure, whose expressions have been
1850 -- checked at the point of record declaration.
1852 if Ada_Version
< Ada_2012
1853 or else not Comes_From_Source
(N
)
1854 or else Inside_Init_Proc
1859 Comp
:= First
(Component_Associations
(N
));
1860 while Present
(Comp
) loop
1861 Comp_Id
:= Entity
(First
(Choices
(Comp
)));
1862 Comp_Typ
:= Etype
(Comp_Id
);
1864 -- Check the component type is either a dimensioned type or a
1865 -- dimensioned subtype.
1867 if Has_Dimension_System
(Base_Type
(Comp_Typ
)) then
1868 Expr
:= Expression
(Comp
);
1870 -- A box-initialized component needs no checking.
1872 if No
(Expr
) and then Box_Present
(Comp
) then
1875 -- Issue an error if the dimensions of the component type and the
1876 -- dimensions of the component mismatch.
1878 elsif Dimensions_Of
(Expr
) /= Dimensions_Of
(Comp_Typ
) then
1880 -- Check if an error has already been encountered so far
1882 if not Error_Detected
then
1884 -- Extension aggregate case
1886 if Nkind
(N
) = N_Extension_Aggregate
then
1888 ("dimensions mismatch in extension aggregate", N
);
1890 -- Record aggregate case
1894 ("dimensions mismatch in record aggregate", N
);
1897 Error_Detected
:= True;
1901 ("\expected dimension " & Dimensions_Msg_Of
(Comp_Typ
)
1902 & ", found " & Dimensions_Msg_Of
(Expr
), Comp
);
1908 end Analyze_Dimension_Extension_Or_Record_Aggregate
;
1910 -------------------------------
1911 -- Analyze_Dimension_Formals --
1912 -------------------------------
1914 procedure Analyze_Dimension_Formals
(N
: Node_Id
; Formals
: List_Id
) is
1915 Dims_Of_Typ
: Dimension_Type
;
1920 -- Aspect is an Ada 2012 feature. Note that there is no need to check
1921 -- dimensions for sub specs that don't come from source.
1923 if Ada_Version
< Ada_2012
or else not Comes_From_Source
(N
) then
1927 Formal
:= First
(Formals
);
1928 while Present
(Formal
) loop
1929 Typ
:= Parameter_Type
(Formal
);
1930 Dims_Of_Typ
:= Dimensions_Of
(Typ
);
1932 if Exists
(Dims_Of_Typ
) then
1934 Expr
: constant Node_Id
:= Expression
(Formal
);
1937 -- Issue a warning if Expr is a numeric literal and if its
1938 -- dimensions differ with the dimensions of the formal type.
1941 and then Dims_Of_Typ
/= Dimensions_Of
(Expr
)
1942 and then Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
1945 Dim_Warning_For_Numeric_Literal
(Expr
, Etype
(Typ
));
1952 end Analyze_Dimension_Formals
;
1954 ---------------------------------
1955 -- Analyze_Dimension_Has_Etype --
1956 ---------------------------------
1958 procedure Analyze_Dimension_Has_Etype
(N
: Node_Id
) is
1959 Etyp
: constant Entity_Id
:= Etype
(N
);
1960 Dims_Of_Etyp
: Dimension_Type
:= Dimensions_Of
(Etyp
);
1963 -- General case. Propagation of the dimensions from the type
1965 if Exists
(Dims_Of_Etyp
) then
1966 Set_Dimensions
(N
, Dims_Of_Etyp
);
1968 -- Identifier case. Propagate the dimensions from the entity for
1969 -- identifier whose entity is a non-dimensionless constant.
1971 elsif Nkind
(N
) = N_Identifier
then
1972 Analyze_Dimension_Identifier
: declare
1973 Id
: constant Entity_Id
:= Entity
(N
);
1976 -- If Id is missing, abnormal tree, assume previous error
1979 Check_Error_Detected
;
1982 elsif Ekind_In
(Id
, E_Constant
, E_Named_Real
)
1983 and then Exists
(Dimensions_Of
(Id
))
1985 Set_Dimensions
(N
, Dimensions_Of
(Id
));
1987 end Analyze_Dimension_Identifier
;
1989 -- Attribute reference case. Propagate the dimensions from the prefix.
1991 elsif Nkind
(N
) = N_Attribute_Reference
1992 and then Has_Dimension_System
(Base_Type
(Etyp
))
1994 Dims_Of_Etyp
:= Dimensions_Of
(Prefix
(N
));
1996 -- Check the prefix is not dimensionless
1998 if Exists
(Dims_Of_Etyp
) then
1999 Set_Dimensions
(N
, Dims_Of_Etyp
);
2003 -- Removal of dimensions in expression
2006 when N_Attribute_Reference |
2007 N_Indexed_Component
=>
2010 Exprs
: constant List_Id
:= Expressions
(N
);
2012 if Present
(Exprs
) then
2013 Expr
:= First
(Exprs
);
2014 while Present
(Expr
) loop
2015 Remove_Dimensions
(Expr
);
2021 when N_Qualified_Expression |
2023 N_Unchecked_Type_Conversion
=>
2024 Remove_Dimensions
(Expression
(N
));
2026 when N_Selected_Component
=>
2027 Remove_Dimensions
(Selector_Name
(N
));
2029 when others => null;
2031 end Analyze_Dimension_Has_Etype
;
2033 ------------------------------------------
2034 -- Analyze_Dimension_Number_Declaration --
2035 ------------------------------------------
2037 procedure Analyze_Dimension_Number_Declaration
(N
: Node_Id
) is
2038 Expr
: constant Node_Id
:= Expression
(N
);
2039 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2040 Dim_Of_Expr
: constant Dimension_Type
:= Dimensions_Of
(Expr
);
2043 if Exists
(Dim_Of_Expr
) then
2044 Set_Dimensions
(Id
, Dim_Of_Expr
);
2045 Set_Etype
(Id
, Etype
(Expr
));
2047 end Analyze_Dimension_Number_Declaration
;
2049 ------------------------------------------
2050 -- Analyze_Dimension_Object_Declaration --
2051 ------------------------------------------
2053 procedure Analyze_Dimension_Object_Declaration
(N
: Node_Id
) is
2054 Expr
: constant Node_Id
:= Expression
(N
);
2055 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2056 Etyp
: constant Entity_Id
:= Etype
(Id
);
2057 Dim_Of_Etyp
: constant Dimension_Type
:= Dimensions_Of
(Etyp
);
2058 Dim_Of_Expr
: Dimension_Type
;
2060 procedure Error_Dim_Msg_For_Object_Declaration
2064 -- Error using Error_Msg_N at node N. Output the dimensions of the
2065 -- type Etyp and of the expression Expr.
2067 ------------------------------------------
2068 -- Error_Dim_Msg_For_Object_Declaration --
2069 ------------------------------------------
2071 procedure Error_Dim_Msg_For_Object_Declaration
2076 Error_Msg_N
("dimensions mismatch in object declaration", N
);
2078 ("\expected dimension " & Dimensions_Msg_Of
(Etyp
) & ", found "
2079 & Dimensions_Msg_Of
(Expr
), Expr
);
2080 end Error_Dim_Msg_For_Object_Declaration
;
2082 -- Start of processing for Analyze_Dimension_Object_Declaration
2085 -- Expression is present
2087 if Present
(Expr
) then
2088 Dim_Of_Expr
:= Dimensions_Of
(Expr
);
2090 -- Check dimensions match
2092 if Dim_Of_Expr
/= Dim_Of_Etyp
then
2094 -- Numeric literal case. Issue a warning if the object type is not
2095 -- dimensionless to indicate the literal is treated as if its
2096 -- dimension matches the type dimension.
2098 if Nkind_In
(Original_Node
(Expr
), N_Real_Literal
,
2101 Dim_Warning_For_Numeric_Literal
(Expr
, Etyp
);
2103 -- Case of object is a constant whose type is a dimensioned type
2105 elsif Constant_Present
(N
) and then not Exists
(Dim_Of_Etyp
) then
2107 -- Propagate dimension from expression to object entity
2109 Set_Dimensions
(Id
, Dim_Of_Expr
);
2111 -- For all other cases, issue an error message
2114 Error_Dim_Msg_For_Object_Declaration
(N
, Etyp
, Expr
);
2118 -- Removal of dimensions in expression
2120 Remove_Dimensions
(Expr
);
2122 end Analyze_Dimension_Object_Declaration
;
2124 ---------------------------------------------------
2125 -- Analyze_Dimension_Object_Renaming_Declaration --
2126 ---------------------------------------------------
2128 procedure Analyze_Dimension_Object_Renaming_Declaration
(N
: Node_Id
) is
2129 Renamed_Name
: constant Node_Id
:= Name
(N
);
2130 Sub_Mark
: constant Node_Id
:= Subtype_Mark
(N
);
2132 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2135 Renamed_Name
: Node_Id
);
2136 -- Error using Error_Msg_N at node N. Output the dimensions of
2137 -- Sub_Mark and of Renamed_Name.
2139 ---------------------------------------------------
2140 -- Error_Dim_Msg_For_Object_Renaming_Declaration --
2141 ---------------------------------------------------
2143 procedure Error_Dim_Msg_For_Object_Renaming_Declaration
2146 Renamed_Name
: Node_Id
) is
2148 Error_Msg_N
("dimensions mismatch in object renaming declaration", N
);
2150 ("\expected dimension " & Dimensions_Msg_Of
(Sub_Mark
) & ", found "
2151 & Dimensions_Msg_Of
(Renamed_Name
), Renamed_Name
);
2152 end Error_Dim_Msg_For_Object_Renaming_Declaration
;
2154 -- Start of processing for Analyze_Dimension_Object_Renaming_Declaration
2157 if Dimensions_Of
(Renamed_Name
) /= Dimensions_Of
(Sub_Mark
) then
2158 Error_Dim_Msg_For_Object_Renaming_Declaration
2159 (N
, Sub_Mark
, Renamed_Name
);
2161 end Analyze_Dimension_Object_Renaming_Declaration
;
2163 -----------------------------------------------
2164 -- Analyze_Dimension_Simple_Return_Statement --
2165 -----------------------------------------------
2167 procedure Analyze_Dimension_Simple_Return_Statement
(N
: Node_Id
) is
2168 Expr
: constant Node_Id
:= Expression
(N
);
2169 Return_Ent
: constant Entity_Id
:= Return_Statement_Entity
(N
);
2170 Return_Etyp
: constant Entity_Id
:=
2171 Etype
(Return_Applies_To
(Return_Ent
));
2172 Dims_Of_Return_Etyp
: constant Dimension_Type
:=
2173 Dimensions_Of
(Return_Etyp
);
2175 procedure Error_Dim_Msg_For_Simple_Return_Statement
2177 Return_Etyp
: Entity_Id
;
2179 -- Error using Error_Msg_N at node N. Output the dimensions of the
2180 -- returned type Return_Etyp and the returned expression Expr of N.
2182 -----------------------------------------------
2183 -- Error_Dim_Msg_For_Simple_Return_Statement --
2184 -----------------------------------------------
2186 procedure Error_Dim_Msg_For_Simple_Return_Statement
2188 Return_Etyp
: Entity_Id
;
2192 Error_Msg_N
("dimensions mismatch in return statement", N
);
2194 ("\expected dimension " & Dimensions_Msg_Of
(Return_Etyp
)
2195 & ", found " & Dimensions_Msg_Of
(Expr
), Expr
);
2196 end Error_Dim_Msg_For_Simple_Return_Statement
;
2198 -- Start of processing for Analyze_Dimension_Simple_Return_Statement
2201 if Dims_Of_Return_Etyp
/= Dimensions_Of
(Expr
) then
2202 Error_Dim_Msg_For_Simple_Return_Statement
(N
, Return_Etyp
, Expr
);
2203 Remove_Dimensions
(Expr
);
2205 end Analyze_Dimension_Simple_Return_Statement
;
2207 -------------------------------------------
2208 -- Analyze_Dimension_Subtype_Declaration --
2209 -------------------------------------------
2211 procedure Analyze_Dimension_Subtype_Declaration
(N
: Node_Id
) is
2212 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2213 Dims_Of_Id
: constant Dimension_Type
:= Dimensions_Of
(Id
);
2214 Dims_Of_Etyp
: Dimension_Type
;
2218 -- No constraint case in subtype declaration
2220 if Nkind
(Subtype_Indication
(N
)) /= N_Subtype_Indication
then
2221 Etyp
:= Etype
(Subtype_Indication
(N
));
2222 Dims_Of_Etyp
:= Dimensions_Of
(Etyp
);
2224 if Exists
(Dims_Of_Etyp
) then
2226 -- If subtype already has a dimension (from Aspect_Dimension),
2227 -- it cannot inherit a dimension from its subtype.
2229 if Exists
(Dims_Of_Id
) then
2231 ("subtype& already " & Dimensions_Msg_Of
(Id
, True), N
, Id
);
2233 Set_Dimensions
(Id
, Dims_Of_Etyp
);
2234 Set_Symbol
(Id
, Symbol_Of
(Etyp
));
2238 -- Constraint present in subtype declaration
2241 Etyp
:= Etype
(Subtype_Mark
(Subtype_Indication
(N
)));
2242 Dims_Of_Etyp
:= Dimensions_Of
(Etyp
);
2244 if Exists
(Dims_Of_Etyp
) then
2245 Set_Dimensions
(Id
, Dims_Of_Etyp
);
2246 Set_Symbol
(Id
, Symbol_Of
(Etyp
));
2249 end Analyze_Dimension_Subtype_Declaration
;
2251 --------------------------------
2252 -- Analyze_Dimension_Unary_Op --
2253 --------------------------------
2255 procedure Analyze_Dimension_Unary_Op
(N
: Node_Id
) is
2258 when N_Op_Plus | N_Op_Minus | N_Op_Abs
=>
2260 -- Propagate the dimension if the operand is not dimensionless
2263 R
: constant Node_Id
:= Right_Opnd
(N
);
2265 Move_Dimensions
(R
, N
);
2268 when others => null;
2271 end Analyze_Dimension_Unary_Op
;
2273 ---------------------
2274 -- Copy_Dimensions --
2275 ---------------------
2277 procedure Copy_Dimensions
(From
, To
: Node_Id
) is
2278 Dims_Of_From
: constant Dimension_Type
:= Dimensions_Of
(From
);
2281 -- Ignore if not Ada 2012 or beyond
2283 if Ada_Version
< Ada_2012
then
2286 -- For Ada 2012, Copy the dimension of 'From to 'To'
2288 elsif Exists
(Dims_Of_From
) then
2289 Set_Dimensions
(To
, Dims_Of_From
);
2291 end Copy_Dimensions
;
2293 --------------------------
2294 -- Create_Rational_From --
2295 --------------------------
2297 -- RATIONAL ::= [-] NUMERAL [/ NUMERAL]
2299 -- A rational number is a number that can be expressed as the quotient or
2300 -- fraction a/b of two integers, where b is non-zero positive.
2302 function Create_Rational_From
2304 Complain
: Boolean) return Rational
2306 Or_Node_Of_Expr
: constant Node_Id
:= Original_Node
(Expr
);
2307 Result
: Rational
:= No_Rational
;
2309 function Process_Minus
(N
: Node_Id
) return Rational
;
2310 -- Create a rational from a N_Op_Minus node
2312 function Process_Divide
(N
: Node_Id
) return Rational
;
2313 -- Create a rational from a N_Op_Divide node
2315 function Process_Literal
(N
: Node_Id
) return Rational
;
2316 -- Create a rational from a N_Integer_Literal node
2322 function Process_Minus
(N
: Node_Id
) return Rational
is
2323 Right
: constant Node_Id
:= Original_Node
(Right_Opnd
(N
));
2327 -- Operand is an integer literal
2329 if Nkind
(Right
) = N_Integer_Literal
then
2330 Result
:= -Process_Literal
(Right
);
2332 -- Operand is a divide operator
2334 elsif Nkind
(Right
) = N_Op_Divide
then
2335 Result
:= -Process_Divide
(Right
);
2338 Result
:= No_Rational
;
2341 -- Provide minimal semantic information on dimension expressions,
2342 -- even though they have no run-time existence. This is for use by
2343 -- ASIS tools, in particular pretty-printing. If generating code
2344 -- standard operator resolution will take place.
2347 Set_Entity
(N
, Standard_Op_Minus
);
2348 Set_Etype
(N
, Standard_Integer
);
2354 --------------------
2355 -- Process_Divide --
2356 --------------------
2358 function Process_Divide
(N
: Node_Id
) return Rational
is
2359 Left
: constant Node_Id
:= Original_Node
(Left_Opnd
(N
));
2360 Right
: constant Node_Id
:= Original_Node
(Right_Opnd
(N
));
2361 Left_Rat
: Rational
;
2362 Result
: Rational
:= No_Rational
;
2363 Right_Rat
: Rational
;
2366 -- Both left and right operands are integer literals
2368 if Nkind
(Left
) = N_Integer_Literal
2370 Nkind
(Right
) = N_Integer_Literal
2372 Left_Rat
:= Process_Literal
(Left
);
2373 Right_Rat
:= Process_Literal
(Right
);
2374 Result
:= Left_Rat
/ Right_Rat
;
2377 -- Provide minimal semantic information on dimension expressions,
2378 -- even though they have no run-time existence. This is for use by
2379 -- ASIS tools, in particular pretty-printing. If generating code
2380 -- standard operator resolution will take place.
2383 Set_Entity
(N
, Standard_Op_Divide
);
2384 Set_Etype
(N
, Standard_Integer
);
2390 ---------------------
2391 -- Process_Literal --
2392 ---------------------
2394 function Process_Literal
(N
: Node_Id
) return Rational
is
2396 return +Whole
(UI_To_Int
(Intval
(N
)));
2397 end Process_Literal
;
2399 -- Start of processing for Create_Rational_From
2402 -- Check the expression is either a division of two integers or an
2403 -- integer itself. Note that the check applies to the original node
2404 -- since the node could have already been rewritten.
2406 -- Integer literal case
2408 if Nkind
(Or_Node_Of_Expr
) = N_Integer_Literal
then
2409 Result
:= Process_Literal
(Or_Node_Of_Expr
);
2411 -- Divide operator case
2413 elsif Nkind
(Or_Node_Of_Expr
) = N_Op_Divide
then
2414 Result
:= Process_Divide
(Or_Node_Of_Expr
);
2416 -- Minus operator case
2418 elsif Nkind
(Or_Node_Of_Expr
) = N_Op_Minus
then
2419 Result
:= Process_Minus
(Or_Node_Of_Expr
);
2422 -- When Expr cannot be interpreted as a rational and Complain is true,
2423 -- generate an error message.
2425 if Complain
and then Result
= No_Rational
then
2426 Error_Msg_N
("rational expected", Expr
);
2430 end Create_Rational_From
;
2436 function Dimensions_Of
(N
: Node_Id
) return Dimension_Type
is
2438 return Dimension_Table
.Get
(N
);
2441 -----------------------
2442 -- Dimensions_Msg_Of --
2443 -----------------------
2445 function Dimensions_Msg_Of
2447 Description_Needed
: Boolean := False) return String
2449 Dims_Of_N
: constant Dimension_Type
:= Dimensions_Of
(N
);
2450 Dimensions_Msg
: Name_Id
;
2451 System
: System_Type
;
2454 -- Initialization of Name_Buffer
2458 -- N is not dimensionless
2460 if Exists
(Dims_Of_N
) then
2461 System
:= System_Of
(Base_Type
(Etype
(N
)));
2463 -- When Description_Needed, add to string "has dimension " before the
2464 -- actual dimension.
2466 if Description_Needed
then
2467 Add_Str_To_Name_Buffer
("has dimension ");
2470 Add_String_To_Name_Buffer
2471 (From_Dim_To_Str_Of_Dim_Symbols
(Dims_Of_N
, System
, True));
2473 -- N is dimensionless
2475 -- When Description_Needed, return "is dimensionless"
2477 elsif Description_Needed
then
2478 Add_Str_To_Name_Buffer
("is dimensionless");
2480 -- Otherwise, return "'[']"
2483 Add_Str_To_Name_Buffer
("'[']");
2486 Dimensions_Msg
:= Name_Find
;
2487 return Get_Name_String
(Dimensions_Msg
);
2488 end Dimensions_Msg_Of
;
2490 --------------------------
2491 -- Dimension_Table_Hash --
2492 --------------------------
2494 function Dimension_Table_Hash
2495 (Key
: Node_Id
) return Dimension_Table_Range
2498 return Dimension_Table_Range
(Key
mod 511);
2499 end Dimension_Table_Hash
;
2501 -------------------------------------
2502 -- Dim_Warning_For_Numeric_Literal --
2503 -------------------------------------
2505 procedure Dim_Warning_For_Numeric_Literal
(N
: Node_Id
; Typ
: Entity_Id
) is
2507 -- Initialize name buffer
2511 Add_String_To_Name_Buffer
(String_From_Numeric_Literal
(N
));
2513 -- Insert a blank between the literal and the symbol
2515 Add_Str_To_Name_Buffer
(" ");
2516 Add_String_To_Name_Buffer
(Symbol_Of
(Typ
));
2518 Error_Msg_Name_1
:= Name_Find
;
2519 Error_Msg_N
("assumed to be%%??", N
);
2520 end Dim_Warning_For_Numeric_Literal
;
2522 ----------------------------------------
2523 -- Eval_Op_Expon_For_Dimensioned_Type --
2524 ----------------------------------------
2526 -- Evaluate the expon operator for real dimensioned type.
2528 -- Note that if the exponent is an integer (denominator = 1) the node is
2529 -- evaluated by the regular Eval_Op_Expon routine (see Sem_Eval).
2531 procedure Eval_Op_Expon_For_Dimensioned_Type
2535 R
: constant Node_Id
:= Right_Opnd
(N
);
2536 R_Value
: Rational
:= No_Rational
;
2539 if Is_Real_Type
(Btyp
) then
2540 R_Value
:= Create_Rational_From
(R
, False);
2543 -- Check that the exponent is not an integer
2545 if R_Value
/= No_Rational
and then R_Value
.Denominator
/= 1 then
2546 Eval_Op_Expon_With_Rational_Exponent
(N
, R_Value
);
2550 end Eval_Op_Expon_For_Dimensioned_Type
;
2552 ------------------------------------------
2553 -- Eval_Op_Expon_With_Rational_Exponent --
2554 ------------------------------------------
2556 -- For dimensioned operand in exponentiation, exponent is allowed to be a
2557 -- Rational and not only an Integer like for dimensionless operands. For
2558 -- that particular case, the left operand is rewritten as a function call
2559 -- using the function Expon_LLF from s-llflex.ads.
2561 procedure Eval_Op_Expon_With_Rational_Exponent
2563 Exponent_Value
: Rational
)
2565 Loc
: constant Source_Ptr
:= Sloc
(N
);
2566 Dims_Of_N
: constant Dimension_Type
:= Dimensions_Of
(N
);
2567 L
: constant Node_Id
:= Left_Opnd
(N
);
2568 Etyp_Of_L
: constant Entity_Id
:= Etype
(L
);
2569 Btyp_Of_L
: constant Entity_Id
:= Base_Type
(Etyp_Of_L
);
2572 Dim_Power
: Rational
;
2573 List_Of_Dims
: List_Id
;
2574 New_Aspect
: Node_Id
;
2575 New_Aspects
: List_Id
;
2578 New_Subtyp_Decl_For_L
: Node_Id
;
2579 System
: System_Type
;
2582 -- Case when the operand is not dimensionless
2584 if Exists
(Dims_Of_N
) then
2586 -- Get the corresponding System_Type to know the exact number of
2587 -- dimensions in the system.
2589 System
:= System_Of
(Btyp_Of_L
);
2591 -- Generation of a new subtype with the proper dimensions
2593 -- In order to rewrite the operator as a type conversion, a new
2594 -- dimensioned subtype with the resulting dimensions of the
2595 -- exponentiation must be created.
2599 -- Btyp_Of_L : constant Entity_Id := Base_Type (Etyp_Of_L);
2600 -- System : constant System_Id :=
2601 -- Get_Dimension_System_Id (Btyp_Of_L);
2602 -- Num_Of_Dims : constant Number_Of_Dimensions :=
2603 -- Dimension_Systems.Table (System).Dimension_Count;
2605 -- subtype T is Btyp_Of_L
2608 -- Dims_Of_N (1).Numerator / Dims_Of_N (1).Denominator,
2609 -- Dims_Of_N (2).Numerator / Dims_Of_N (2).Denominator,
2611 -- Dims_Of_N (Num_Of_Dims).Numerator /
2612 -- Dims_Of_N (Num_Of_Dims).Denominator);
2614 -- Step 1: Generate the new aggregate for the aspect Dimension
2616 New_Aspects
:= Empty_List
;
2618 List_Of_Dims
:= New_List
;
2619 for Position
in Dims_Of_N
'First .. System
.Count
loop
2620 Dim_Power
:= Dims_Of_N
(Position
);
2621 Append_To
(List_Of_Dims
,
2622 Make_Op_Divide
(Loc
,
2624 Make_Integer_Literal
(Loc
, Int
(Dim_Power
.Numerator
)),
2626 Make_Integer_Literal
(Loc
, Int
(Dim_Power
.Denominator
))));
2629 -- Step 2: Create the new Aspect Specification for Aspect Dimension
2632 Make_Aspect_Specification
(Loc
,
2633 Identifier
=> Make_Identifier
(Loc
, Name_Dimension
),
2634 Expression
=> Make_Aggregate
(Loc
, Expressions
=> List_Of_Dims
));
2636 -- Step 3: Make a temporary identifier for the new subtype
2638 New_Id
:= Make_Temporary
(Loc
, 'T');
2639 Set_Is_Internal
(New_Id
);
2641 -- Step 4: Declaration of the new subtype
2643 New_Subtyp_Decl_For_L
:=
2644 Make_Subtype_Declaration
(Loc
,
2645 Defining_Identifier
=> New_Id
,
2646 Subtype_Indication
=> New_Occurrence_Of
(Btyp_Of_L
, Loc
));
2648 Append
(New_Aspect
, New_Aspects
);
2649 Set_Parent
(New_Aspects
, New_Subtyp_Decl_For_L
);
2650 Set_Aspect_Specifications
(New_Subtyp_Decl_For_L
, New_Aspects
);
2652 Analyze
(New_Subtyp_Decl_For_L
);
2654 -- Case where the operand is dimensionless
2657 New_Id
:= Btyp_Of_L
;
2660 -- Replacement of N by New_N
2664 -- Actual_1 := Long_Long_Float (L),
2666 -- Actual_2 := Long_Long_Float (Exponent_Value.Numerator) /
2667 -- Long_Long_Float (Exponent_Value.Denominator);
2669 -- (T (Expon_LLF (Actual_1, Actual_2)));
2671 -- where T is the subtype declared in step 1
2673 -- The node is rewritten as a type conversion
2675 -- Step 1: Creation of the two parameters of Expon_LLF function call
2678 Make_Type_Conversion
(Loc
,
2679 Subtype_Mark
=> New_Occurrence_Of
(Standard_Long_Long_Float
, Loc
),
2680 Expression
=> Relocate_Node
(L
));
2683 Make_Op_Divide
(Loc
,
2685 Make_Real_Literal
(Loc
,
2686 UR_From_Uint
(UI_From_Int
(Int
(Exponent_Value
.Numerator
)))),
2688 Make_Real_Literal
(Loc
,
2689 UR_From_Uint
(UI_From_Int
(Int
(Exponent_Value
.Denominator
)))));
2691 -- Step 2: Creation of New_N
2694 Make_Type_Conversion
(Loc
,
2695 Subtype_Mark
=> New_Occurrence_Of
(New_Id
, Loc
),
2697 Make_Function_Call
(Loc
,
2698 Name
=> New_Occurrence_Of
(RTE
(RE_Expon_LLF
), Loc
),
2699 Parameter_Associations
=> New_List
(
2700 Actual_1
, Actual_2
)));
2702 -- Step 3: Rewrite N with the result
2705 Set_Etype
(N
, New_Id
);
2706 Analyze_And_Resolve
(N
, New_Id
);
2707 end Eval_Op_Expon_With_Rational_Exponent
;
2713 function Exists
(Dim
: Dimension_Type
) return Boolean is
2715 return Dim
/= Null_Dimension
;
2718 function Exists
(Str
: String_Id
) return Boolean is
2720 return Str
/= No_String
;
2723 function Exists
(Sys
: System_Type
) return Boolean is
2725 return Sys
/= Null_System
;
2728 ---------------------------------
2729 -- Expand_Put_Call_With_Symbol --
2730 ---------------------------------
2732 -- For procedure Put (resp. Put_Dim_Of) and function Image, defined in
2733 -- System.Dim.Float_IO or System.Dim.Integer_IO, the default string
2734 -- parameter is rewritten to include the unit symbol (or the dimension
2735 -- symbols if not a defined quantity) in the output of a dimensioned
2736 -- object. If a value is already supplied by the user for the parameter
2737 -- Symbol, it is used as is.
2739 -- Case 1. Item is dimensionless
2741 -- * Put : Item appears without a suffix
2743 -- * Put_Dim_Of : the output is []
2745 -- Obj : Mks_Type := 2.6;
2746 -- Put (Obj, 1, 1, 0);
2747 -- Put_Dim_Of (Obj);
2749 -- The corresponding outputs are:
2753 -- Case 2. Item has a dimension
2755 -- * Put : If the type of Item is a dimensioned subtype whose
2756 -- symbol is not empty, then the symbol appears as a
2757 -- suffix. Otherwise, a new string is created and appears
2758 -- as a suffix of Item. This string results in the
2759 -- successive concatanations between each unit symbol
2760 -- raised by its corresponding dimension power from the
2761 -- dimensions of Item.
2763 -- * Put_Dim_Of : The output is a new string resulting in the successive
2764 -- concatanations between each dimension symbol raised by
2765 -- its corresponding dimension power from the dimensions of
2768 -- subtype Random is Mks_Type
2775 -- Obj : Random := 5.0;
2777 -- Put_Dim_Of (Obj);
2779 -- The corresponding outputs are:
2780 -- $5.0 m**3.cd**(-1)
2783 -- The function Image returns the string identical to that produced by
2784 -- a call to Put whose first parameter is a string.
2786 procedure Expand_Put_Call_With_Symbol
(N
: Node_Id
) is
2787 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
2788 Loc
: constant Source_Ptr
:= Sloc
(N
);
2789 Name_Call
: constant Node_Id
:= Name
(N
);
2790 New_Actuals
: constant List_Id
:= New_List
;
2792 Dims_Of_Actual
: Dimension_Type
;
2794 New_Str_Lit
: Node_Id
:= Empty
;
2795 Symbols
: String_Id
;
2797 Is_Put_Dim_Of
: Boolean := False;
2798 -- This flag is used in order to differentiate routines Put and
2799 -- Put_Dim_Of. Set to True if the procedure is one of the Put_Dim_Of
2800 -- defined in System.Dim.Float_IO or System.Dim.Integer_IO.
2802 function Has_Symbols
return Boolean;
2803 -- Return True if the current Put call already has a parameter
2804 -- association for parameter "Symbols" with the correct string of
2807 function Is_Procedure_Put_Call
return Boolean;
2808 -- Return True if the current call is a call of an instantiation of a
2809 -- procedure Put defined in the package System.Dim.Float_IO and
2810 -- System.Dim.Integer_IO.
2812 function Item_Actual
return Node_Id
;
2813 -- Return the item actual parameter node in the output call
2819 function Has_Symbols
return Boolean is
2821 Actual_Str
: Node_Id
;
2824 -- Look for a symbols parameter association in the list of actuals
2826 Actual
:= First
(Actuals
);
2827 while Present
(Actual
) loop
2829 -- Positional parameter association case when the actual is a
2832 if Nkind
(Actual
) = N_String_Literal
then
2833 Actual_Str
:= Actual
;
2835 -- Named parameter association case when selector name is Symbol
2837 elsif Nkind
(Actual
) = N_Parameter_Association
2838 and then Chars
(Selector_Name
(Actual
)) = Name_Symbol
2840 Actual_Str
:= Explicit_Actual_Parameter
(Actual
);
2842 -- Ignore all other cases
2845 Actual_Str
:= Empty
;
2848 if Present
(Actual_Str
) then
2850 -- Return True if the actual comes from source or if the string
2851 -- of symbols doesn't have the default value (i.e. it is ""),
2852 -- in which case it is used as suffix of the generated string.
2854 if Comes_From_Source
(Actual
)
2855 or else String_Length
(Strval
(Actual_Str
)) /= 0
2867 -- At this point, the call has no parameter association. Look to the
2868 -- last actual since the symbols parameter is the last one.
2870 return Nkind
(Last
(Actuals
)) = N_String_Literal
;
2873 ---------------------------
2874 -- Is_Procedure_Put_Call --
2875 ---------------------------
2877 function Is_Procedure_Put_Call
return Boolean is
2882 -- There are three different Put (resp. Put_Dim_Of) routines in each
2883 -- generic dim IO package. Verify the current procedure call is one
2886 if Is_Entity_Name
(Name_Call
) then
2887 Ent
:= Entity
(Name_Call
);
2889 -- Get the original subprogram entity following the renaming chain
2891 if Present
(Alias
(Ent
)) then
2897 -- Check the name of the entity subprogram is Put (resp.
2898 -- Put_Dim_Of) and verify this entity is located in either
2899 -- System.Dim.Float_IO or System.Dim.Integer_IO.
2901 if Loc
> No_Location
2902 and then Is_Dim_IO_Package_Entity
2903 (Cunit_Entity
(Get_Source_Unit
(Loc
)))
2905 if Chars
(Ent
) = Name_Put_Dim_Of
then
2906 Is_Put_Dim_Of
:= True;
2909 elsif Chars
(Ent
) = Name_Put
2910 or else Chars
(Ent
) = Name_Image
2918 end Is_Procedure_Put_Call
;
2924 function Item_Actual
return Node_Id
is
2928 -- Look for the item actual as a parameter association
2930 Actual
:= First
(Actuals
);
2931 while Present
(Actual
) loop
2932 if Nkind
(Actual
) = N_Parameter_Association
2933 and then Chars
(Selector_Name
(Actual
)) = Name_Item
2935 return Explicit_Actual_Parameter
(Actual
);
2941 -- Case where the item has been defined without an association
2943 Actual
:= First
(Actuals
);
2945 -- Depending on the procedure Put, Item actual could be first or
2946 -- second in the list of actuals.
2948 if Has_Dimension_System
(Base_Type
(Etype
(Actual
))) then
2951 return Next
(Actual
);
2955 -- Start of processing for Expand_Put_Call_With_Symbol
2958 if Is_Procedure_Put_Call
and then not Has_Symbols
then
2959 Actual
:= Item_Actual
;
2960 Dims_Of_Actual
:= Dimensions_Of
(Actual
);
2961 Etyp
:= Etype
(Actual
);
2965 if Is_Put_Dim_Of
then
2967 -- Check that the item is not dimensionless
2969 -- Create the new String_Literal with the new String_Id generated
2970 -- by the routine From_Dim_To_Str_Of_Dim_Symbols.
2972 if Exists
(Dims_Of_Actual
) then
2974 Make_String_Literal
(Loc
,
2975 From_Dim_To_Str_Of_Dim_Symbols
2976 (Dims_Of_Actual
, System_Of
(Base_Type
(Etyp
))));
2978 -- If dimensionless, the output is []
2982 Make_String_Literal
(Loc
, "[]");
2988 -- Add the symbol as a suffix of the value if the subtype has a
2989 -- unit symbol or if the parameter is not dimensionless.
2991 if Exists
(Symbol_Of
(Etyp
)) then
2992 Symbols
:= Symbol_Of
(Etyp
);
2994 Symbols
:= From_Dim_To_Str_Of_Unit_Symbols
2995 (Dims_Of_Actual
, System_Of
(Base_Type
(Etyp
)));
2998 -- Check Symbols exists
3000 if Exists
(Symbols
) then
3003 -- Put a space between the value and the dimension
3005 Store_String_Char
(' ');
3006 Store_String_Chars
(Symbols
);
3007 New_Str_Lit
:= Make_String_Literal
(Loc
, End_String
);
3011 if Present
(New_Str_Lit
) then
3013 -- Insert all actuals in New_Actuals
3015 Actual
:= First
(Actuals
);
3016 while Present
(Actual
) loop
3018 -- Copy every actuals in New_Actuals except the Symbols
3019 -- parameter association.
3021 if Nkind
(Actual
) = N_Parameter_Association
3022 and then Chars
(Selector_Name
(Actual
)) /= Name_Symbol
3024 Append_To
(New_Actuals
,
3025 Make_Parameter_Association
(Loc
,
3026 Selector_Name
=> New_Copy
(Selector_Name
(Actual
)),
3027 Explicit_Actual_Parameter
=>
3028 New_Copy
(Explicit_Actual_Parameter
(Actual
))));
3030 elsif Nkind
(Actual
) /= N_Parameter_Association
then
3031 Append_To
(New_Actuals
, New_Copy
(Actual
));
3037 -- Create new Symbols param association and append to New_Actuals
3039 Append_To
(New_Actuals
,
3040 Make_Parameter_Association
(Loc
,
3041 Selector_Name
=> Make_Identifier
(Loc
, Name_Symbol
),
3042 Explicit_Actual_Parameter
=> New_Str_Lit
));
3044 -- Rewrite and analyze the procedure call
3046 if Chars
(Name_Call
) = Name_Image
then
3048 Make_Function_Call
(Loc
,
3049 Name
=> New_Copy
(Name_Call
),
3050 Parameter_Associations
=> New_Actuals
));
3051 Analyze_And_Resolve
(N
);
3054 Make_Procedure_Call_Statement
(Loc
,
3055 Name
=> New_Copy
(Name_Call
),
3056 Parameter_Associations
=> New_Actuals
));
3062 end Expand_Put_Call_With_Symbol
;
3064 ------------------------------------
3065 -- From_Dim_To_Str_Of_Dim_Symbols --
3066 ------------------------------------
3068 -- Given a dimension vector and the corresponding dimension system, create
3069 -- a String_Id to output dimension symbols corresponding to the dimensions
3070 -- Dims. If In_Error_Msg is True, there is a special handling for character
3071 -- asterisk * which is an insertion character in error messages.
3073 function From_Dim_To_Str_Of_Dim_Symbols
3074 (Dims
: Dimension_Type
;
3075 System
: System_Type
;
3076 In_Error_Msg
: Boolean := False) return String_Id
3078 Dim_Power
: Rational
;
3079 First_Dim
: Boolean := True;
3081 procedure Store_String_Oexpon
;
3082 -- Store the expon operator symbol "**" in the string. In error
3083 -- messages, asterisk * is a special character and must be quoted
3084 -- to be placed literally into the message.
3086 -------------------------
3087 -- Store_String_Oexpon --
3088 -------------------------
3090 procedure Store_String_Oexpon
is
3092 if In_Error_Msg
then
3093 Store_String_Chars
("'*'*");
3095 Store_String_Chars
("**");
3097 end Store_String_Oexpon
;
3099 -- Start of processing for From_Dim_To_Str_Of_Dim_Symbols
3102 -- Initialization of the new String_Id
3106 -- Store the dimension symbols inside boxes
3108 if In_Error_Msg
then
3109 Store_String_Chars
("'[");
3111 Store_String_Char
('[');
3114 for Position
in Dimension_Type
'Range loop
3115 Dim_Power
:= Dims
(Position
);
3116 if Dim_Power
/= Zero
then
3121 Store_String_Char
('.');
3124 Store_String_Chars
(System
.Dim_Symbols
(Position
));
3126 -- Positive dimension case
3128 if Dim_Power
.Numerator
> 0 then
3132 if Dim_Power
.Denominator
= 1 then
3133 if Dim_Power
.Numerator
/= 1 then
3134 Store_String_Oexpon
;
3135 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3138 -- Rational case when denominator /= 1
3141 Store_String_Oexpon
;
3142 Store_String_Char
('(');
3143 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3144 Store_String_Char
('/');
3145 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3146 Store_String_Char
(')');
3149 -- Negative dimension case
3152 Store_String_Oexpon
;
3153 Store_String_Char
('(');
3154 Store_String_Char
('-');
3155 Store_String_Int
(Int
(-Dim_Power
.Numerator
));
3159 if Dim_Power
.Denominator
= 1 then
3160 Store_String_Char
(')');
3162 -- Rational case when denominator /= 1
3165 Store_String_Char
('/');
3166 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3167 Store_String_Char
(')');
3173 if In_Error_Msg
then
3174 Store_String_Chars
("']");
3176 Store_String_Char
(']');
3180 end From_Dim_To_Str_Of_Dim_Symbols
;
3182 -------------------------------------
3183 -- From_Dim_To_Str_Of_Unit_Symbols --
3184 -------------------------------------
3186 -- Given a dimension vector and the corresponding dimension system,
3187 -- create a String_Id to output the unit symbols corresponding to the
3190 function From_Dim_To_Str_Of_Unit_Symbols
3191 (Dims
: Dimension_Type
;
3192 System
: System_Type
) return String_Id
3194 Dim_Power
: Rational
;
3195 First_Dim
: Boolean := True;
3198 -- Return No_String if dimensionless
3200 if not Exists
(Dims
) then
3204 -- Initialization of the new String_Id
3208 for Position
in Dimension_Type
'Range loop
3209 Dim_Power
:= Dims
(Position
);
3211 if Dim_Power
/= Zero
then
3215 Store_String_Char
('.');
3218 Store_String_Chars
(System
.Unit_Symbols
(Position
));
3220 -- Positive dimension case
3222 if Dim_Power
.Numerator
> 0 then
3226 if Dim_Power
.Denominator
= 1 then
3227 if Dim_Power
.Numerator
/= 1 then
3228 Store_String_Chars
("**");
3229 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3232 -- Rational case when denominator /= 1
3235 Store_String_Chars
("**");
3236 Store_String_Char
('(');
3237 Store_String_Int
(Int
(Dim_Power
.Numerator
));
3238 Store_String_Char
('/');
3239 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3240 Store_String_Char
(')');
3243 -- Negative dimension case
3246 Store_String_Chars
("**");
3247 Store_String_Char
('(');
3248 Store_String_Char
('-');
3249 Store_String_Int
(Int
(-Dim_Power
.Numerator
));
3253 if Dim_Power
.Denominator
= 1 then
3254 Store_String_Char
(')');
3256 -- Rational case when denominator /= 1
3259 Store_String_Char
('/');
3260 Store_String_Int
(Int
(Dim_Power
.Denominator
));
3261 Store_String_Char
(')');
3268 end From_Dim_To_Str_Of_Unit_Symbols
;
3274 function GCD
(Left
, Right
: Whole
) return Int
is
3294 --------------------------
3295 -- Has_Dimension_System --
3296 --------------------------
3298 function Has_Dimension_System
(Typ
: Entity_Id
) return Boolean is
3300 return Exists
(System_Of
(Typ
));
3301 end Has_Dimension_System
;
3303 ------------------------------
3304 -- Is_Dim_IO_Package_Entity --
3305 ------------------------------
3307 function Is_Dim_IO_Package_Entity
(E
: Entity_Id
) return Boolean is
3309 -- Check the package entity corresponds to System.Dim.Float_IO or
3310 -- System.Dim.Integer_IO.
3313 Is_RTU
(E
, System_Dim_Float_IO
)
3315 Is_RTU
(E
, System_Dim_Integer_IO
);
3316 end Is_Dim_IO_Package_Entity
;
3318 -------------------------------------
3319 -- Is_Dim_IO_Package_Instantiation --
3320 -------------------------------------
3322 function Is_Dim_IO_Package_Instantiation
(N
: Node_Id
) return Boolean is
3323 Gen_Id
: constant Node_Id
:= Name
(N
);
3326 -- Check that the instantiated package is either System.Dim.Float_IO
3327 -- or System.Dim.Integer_IO.
3330 Is_Entity_Name
(Gen_Id
)
3331 and then Is_Dim_IO_Package_Entity
(Entity
(Gen_Id
));
3332 end Is_Dim_IO_Package_Instantiation
;
3338 function Is_Invalid
(Position
: Dimension_Position
) return Boolean is
3340 return Position
= Invalid_Position
;
3343 ---------------------
3344 -- Move_Dimensions --
3345 ---------------------
3347 procedure Move_Dimensions
(From
, To
: Node_Id
) is
3349 if Ada_Version
< Ada_2012
then
3353 -- Copy the dimension of 'From to 'To' and remove dimension of 'From'
3355 Copy_Dimensions
(From
, To
);
3356 Remove_Dimensions
(From
);
3357 end Move_Dimensions
;
3363 function Reduce
(X
: Rational
) return Rational
is
3365 if X
.Numerator
= 0 then
3370 G
: constant Int
:= GCD
(X
.Numerator
, X
.Denominator
);
3372 return Rational
'(Numerator => Whole (Int (X.Numerator) / G),
3373 Denominator => Whole (Int (X.Denominator) / G));
3377 -----------------------
3378 -- Remove_Dimensions --
3379 -----------------------
3381 procedure Remove_Dimensions (N : Node_Id) is
3382 Dims_Of_N : constant Dimension_Type := Dimensions_Of (N);
3384 if Exists (Dims_Of_N) then
3385 Dimension_Table.Remove (N);
3387 end Remove_Dimensions;
3389 -----------------------------------
3390 -- Remove_Dimension_In_Statement --
3391 -----------------------------------
3393 -- Removal of dimension in statement as part of the Analyze_Statements
3394 -- routine (see package Sem_Ch5).
3396 procedure Remove_Dimension_In_Statement (Stmt : Node_Id) is
3398 if Ada_Version < Ada_2012 then
3402 -- Remove dimension in parameter specifications for accept statement
3404 if Nkind (Stmt) = N_Accept_Statement then
3406 Param : Node_Id := First (Parameter_Specifications (Stmt));
3408 while Present (Param) loop
3409 Remove_Dimensions (Param);
3414 -- Remove dimension of name and expression in assignments
3416 elsif Nkind (Stmt) = N_Assignment_Statement then
3417 Remove_Dimensions (Expression (Stmt));
3418 Remove_Dimensions (Name (Stmt));
3420 end Remove_Dimension_In_Statement;
3422 --------------------
3423 -- Set_Dimensions --
3424 --------------------
3426 procedure Set_Dimensions (N : Node_Id; Val : Dimension_Type) is
3428 pragma Assert (OK_For_Dimension (Nkind (N)));
3429 pragma Assert (Exists (Val));
3431 Dimension_Table.Set (N, Val);
3438 procedure Set_Symbol (E : Entity_Id; Val : String_Id) is
3440 Symbol_Table.Set (E, Val);
3443 ---------------------------------
3444 -- String_From_Numeric_Literal --
3445 ---------------------------------
3447 function String_From_Numeric_Literal (N : Node_Id) return String_Id is
3448 Loc : constant Source_Ptr := Sloc (N);
3449 Sbuffer : constant Source_Buffer_Ptr :=
3450 Source_Text (Get_Source_File_Index (Loc));
3451 Src_Ptr : Source_Ptr := Loc;
3453 C : Character := Sbuffer (Src_Ptr);
3454 -- Current source program character
3456 function Belong_To_Numeric_Literal (C : Character) return Boolean;
3457 -- Return True if C belongs to a numeric literal
3459 -------------------------------
3460 -- Belong_To_Numeric_Literal --
3461 -------------------------------
3463 function Belong_To_Numeric_Literal (C : Character) return Boolean is
3479 -- Make sure '+' or '-' is part of an exponent.
3483 Prev_C : constant Character := Sbuffer (Src_Ptr - 1);
3485 return Prev_C = 'e
' or else Prev_C = 'E
';
3488 -- All other character doesn't belong to a numeric literal
3493 end Belong_To_Numeric_Literal;
3495 -- Start of processing for String_From_Numeric_Literal
3499 while Belong_To_Numeric_Literal (C) loop
3500 Store_String_Char (C);
3501 Src_Ptr := Src_Ptr + 1;
3502 C := Sbuffer (Src_Ptr);
3506 end String_From_Numeric_Literal;
3512 function Symbol_Of (E : Entity_Id) return String_Id is
3513 Subtype_Symbol : constant String_Id := Symbol_Table.Get (E);
3515 if Subtype_Symbol /= No_String then
3516 return Subtype_Symbol;
3518 return From_Dim_To_Str_Of_Unit_Symbols
3519 (Dimensions_Of (E), System_Of (Base_Type (E)));
3523 -----------------------
3524 -- Symbol_Table_Hash --
3525 -----------------------
3527 function Symbol_Table_Hash (Key : Entity_Id) return Symbol_Table_Range is
3529 return Symbol_Table_Range (Key mod 511);
3530 end Symbol_Table_Hash;
3536 function System_Of (E : Entity_Id) return System_Type is
3537 Type_Decl : constant Node_Id := Parent (E);
3540 -- Look for Type_Decl in System_Table
3542 for Dim_Sys in 1 .. System_Table.Last loop
3543 if Type_Decl = System_Table.Table (Dim_Sys).Type_Decl then
3544 return System_Table.Table (Dim_Sys);