1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2014, 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 Err_Vars
; use Err_Vars
;
27 with Prj
.Attr
; use Prj
.Attr
;
28 with Prj
.Err
; use Prj
.Err
;
31 with Uintp
; use Uintp
;
33 package body Prj
.Strt
is
35 Buffer
: String_Access
;
36 Buffer_Last
: Natural := 0;
38 type Choice_String
is record
40 Already_Used
: Boolean := False;
42 -- The string of a case label, and an indication that it has already
43 -- been used (to avoid duplicate case labels).
45 Choices_Initial
: constant := 10;
46 Choices_Increment
: constant := 100;
47 -- These should be in alloc.ads
49 Choice_Node_Low_Bound
: constant := 0;
50 Choice_Node_High_Bound
: constant := 099_999_999
;
51 -- In practice, infinite
53 type Choice_Node_Id
is
54 range Choice_Node_Low_Bound
.. Choice_Node_High_Bound
;
56 First_Choice_Node_Id
: constant Choice_Node_Id
:=
57 Choice_Node_Low_Bound
;
61 (Table_Component_Type
=> Choice_String
,
62 Table_Index_Type
=> Choice_Node_Id
'Base,
63 Table_Low_Bound
=> First_Choice_Node_Id
,
64 Table_Initial
=> Choices_Initial
,
65 Table_Increment
=> Choices_Increment
,
66 Table_Name
=> "Prj.Strt.Choices");
67 -- Used to store the case labels and check that there is no duplicate
69 package Choice_Lasts
is
71 (Table_Component_Type
=> Choice_Node_Id
,
72 Table_Index_Type
=> Nat
,
75 Table_Increment
=> 100,
76 Table_Name
=> "Prj.Strt.Choice_Lasts");
77 -- Used to store the indexes of the choices in table Choices, to
78 -- distinguish nested case constructions.
80 Choice_First
: Choice_Node_Id
:= 0;
81 -- Index in table Choices of the first case label of the current
82 -- case construction. Zero means no current case construction.
84 type Name_Location
is record
85 Name
: Name_Id
:= No_Name
;
86 Location
: Source_Ptr
:= No_Location
;
88 -- Store the identifier and the location of a simple name
92 (Table_Component_Type
=> Name_Location
,
93 Table_Index_Type
=> Nat
,
96 Table_Increment
=> 100,
97 Table_Name
=> "Prj.Strt.Names");
98 -- Used to accumulate the single names of a name
100 procedure Add
(This_String
: Name_Id
);
101 -- Add a string to the case label list, indicating that it has not
104 procedure Add_To_Names
(NL
: Name_Location
);
105 -- Add one single names to table Names
107 procedure External_Reference
108 (In_Tree
: Project_Node_Tree_Ref
;
109 Current_Project
: Project_Node_Id
;
110 Current_Package
: Project_Node_Id
;
111 External_Value
: out Project_Node_Id
;
112 Expr_Kind
: in out Variable_Kind
;
113 Flags
: Processing_Flags
);
114 -- Parse an external reference. Current token is "external"
116 procedure Attribute_Reference
117 (In_Tree
: Project_Node_Tree_Ref
;
118 Reference
: out Project_Node_Id
;
119 First_Attribute
: Attribute_Node_Id
;
120 Current_Project
: Project_Node_Id
;
121 Current_Package
: Project_Node_Id
;
122 Flags
: Processing_Flags
);
123 -- Parse an attribute reference. Current token is an apostrophe
126 (In_Tree
: Project_Node_Tree_Ref
;
127 Term
: out Project_Node_Id
;
128 Expr_Kind
: in out Variable_Kind
;
129 Current_Project
: Project_Node_Id
;
130 Current_Package
: Project_Node_Id
;
131 Optional_Index
: Boolean;
132 Flags
: Processing_Flags
);
133 -- Recursive procedure to parse one term or several terms concatenated
140 procedure Add
(This_String
: Name_Id
) is
142 Choices
.Increment_Last
;
143 Choices
.Table
(Choices
.Last
) :=
144 (The_String
=> This_String
,
145 Already_Used
=> False);
152 procedure Add_To_Names
(NL
: Name_Location
) is
154 Names
.Increment_Last
;
155 Names
.Table
(Names
.Last
) := NL
;
158 -------------------------
159 -- Attribute_Reference --
160 -------------------------
162 procedure Attribute_Reference
163 (In_Tree
: Project_Node_Tree_Ref
;
164 Reference
: out Project_Node_Id
;
165 First_Attribute
: Attribute_Node_Id
;
166 Current_Project
: Project_Node_Id
;
167 Current_Package
: Project_Node_Id
;
168 Flags
: Processing_Flags
)
170 Current_Attribute
: Attribute_Node_Id
:= First_Attribute
;
173 -- Declare the node of the attribute reference
177 (Of_Kind
=> N_Attribute_Reference
, In_Tree
=> In_Tree
);
178 Set_Location_Of
(Reference
, In_Tree
, To
=> Token_Ptr
);
179 Scan
(In_Tree
); -- past apostrophe
181 -- Body may be an attribute name
183 if Token
= Tok_Body
then
184 Token
:= Tok_Identifier
;
185 Token_Name
:= Snames
.Name_Body
;
188 Expect
(Tok_Identifier
, "identifier");
190 if Token
= Tok_Identifier
then
191 Set_Name_Of
(Reference
, In_Tree
, To
=> Token_Name
);
193 -- Check if the identifier is one of the attribute identifiers in the
194 -- context (package or project level attributes).
197 Attribute_Node_Id_Of
(Token_Name
, Starting_At
=> First_Attribute
);
199 -- If the identifier is not allowed, report an error
201 if Current_Attribute
= Empty_Attribute
then
202 Error_Msg_Name_1
:= Token_Name
;
203 Error_Msg
(Flags
, "unknown attribute %%", Token_Ptr
);
204 Reference
:= Empty_Node
;
206 -- Scan past the attribute name
211 -- Give its characteristics to this attribute reference
213 Set_Project_Node_Of
(Reference
, In_Tree
, To
=> Current_Project
);
214 Set_Package_Node_Of
(Reference
, In_Tree
, To
=> Current_Package
);
215 Set_Expression_Kind_Of
216 (Reference
, In_Tree
, To
=> Variable_Kind_Of
(Current_Attribute
));
219 To
=> Attribute_Kind_Of
(Current_Attribute
) in
220 All_Case_Insensitive_Associative_Array
);
223 To
=> Attribute_Default_Of
(Current_Attribute
));
225 -- Scan past the attribute name
229 -- If the attribute is an associative array, get the index
231 if Attribute_Kind_Of
(Current_Attribute
) /= Single
then
232 Expect
(Tok_Left_Paren
, "`(`");
234 if Token
= Tok_Left_Paren
then
237 if Others_Allowed_For
(Current_Attribute
)
238 and then Token
= Tok_Others
240 Set_Associative_Array_Index_Of
241 (Reference
, In_Tree
, To
=> All_Other_Names
);
245 if Others_Allowed_For
(Current_Attribute
) then
247 (Tok_String_Literal
, "literal string or others");
249 Expect
(Tok_String_Literal
, "literal string");
252 if Token
= Tok_String_Literal
then
253 Set_Associative_Array_Index_Of
254 (Reference
, In_Tree
, To
=> Token_Name
);
260 Expect
(Tok_Right_Paren
, "`)`");
262 if Token
= Tok_Right_Paren
then
268 -- Change name of obsolete attributes
270 if Present
(Reference
) then
271 case Name_Of
(Reference
, In_Tree
) is
272 when Snames
.Name_Specification
=>
273 Set_Name_Of
(Reference
, In_Tree
, To
=> Snames
.Name_Spec
);
275 when Snames
.Name_Specification_Suffix
=>
277 (Reference
, In_Tree
, To
=> Snames
.Name_Spec_Suffix
);
279 when Snames
.Name_Implementation
=>
280 Set_Name_Of
(Reference
, In_Tree
, To
=> Snames
.Name_Body
);
282 when Snames
.Name_Implementation_Suffix
=>
284 (Reference
, In_Tree
, To
=> Snames
.Name_Body_Suffix
);
291 end Attribute_Reference
;
293 ---------------------------
294 -- End_Case_Construction --
295 ---------------------------
297 procedure End_Case_Construction
298 (Check_All_Labels
: Boolean;
299 Case_Location
: Source_Ptr
;
300 Flags
: Processing_Flags
;
301 String_Type
: Boolean)
303 Non_Used
: Natural := 0;
304 First_Non_Used
: Choice_Node_Id
:= First_Choice_Node_Id
;
307 -- First, if Check_All_Labels is True, check if all values of the string
308 -- type have been used.
310 if Check_All_Labels
then
312 for Choice
in Choice_First
.. Choices
.Last
loop
313 if not Choices
.Table
(Choice
).Already_Used
then
314 Non_Used
:= Non_Used
+ 1;
317 First_Non_Used
:= Choice
;
322 -- If only one is not used, report a single warning for this value
325 Error_Msg_Name_1
:= Choices
.Table
(First_Non_Used
).The_String
;
327 (Flags
, "?value %% is not used as label", Case_Location
);
329 -- If several are not used, report a warning for each one of them
331 elsif Non_Used
> 1 then
333 (Flags
, "?the following values are not used as labels:",
336 for Choice
in First_Non_Used
.. Choices
.Last
loop
337 if not Choices
.Table
(Choice
).Already_Used
then
338 Error_Msg_Name_1
:= Choices
.Table
(Choice
).The_String
;
339 Error_Msg
(Flags
, "\?%%", Case_Location
);
346 "?no when others for this case construction",
351 -- If this is the only case construction, empty the tables
353 if Choice_Lasts
.Last
= 1 then
354 Choice_Lasts
.Set_Last
(0);
355 Choices
.Set_Last
(First_Choice_Node_Id
);
358 -- Second case construction, set the tables to the first
360 elsif Choice_Lasts
.Last
= 2 then
361 Choice_Lasts
.Set_Last
(1);
362 Choices
.Set_Last
(Choice_Lasts
.Table
(1));
365 -- Third or more case construction, set the tables to the previous one
367 Choice_Lasts
.Decrement_Last
;
368 Choices
.Set_Last
(Choice_Lasts
.Table
(Choice_Lasts
.Last
));
369 Choice_First
:= Choice_Lasts
.Table
(Choice_Lasts
.Last
- 1) + 1;
371 end End_Case_Construction
;
373 ------------------------
374 -- External_Reference --
375 ------------------------
377 procedure External_Reference
378 (In_Tree
: Project_Node_Tree_Ref
;
379 Current_Project
: Project_Node_Id
;
380 Current_Package
: Project_Node_Id
;
381 External_Value
: out Project_Node_Id
;
382 Expr_Kind
: in out Variable_Kind
;
383 Flags
: Processing_Flags
)
385 Field_Id
: Project_Node_Id
:= Empty_Node
;
386 Ext_List
: Boolean := False;
391 (Of_Kind
=> N_External_Value
,
393 Set_Location_Of
(External_Value
, In_Tree
, To
=> Token_Ptr
);
395 -- The current token is either external or external_as_list
397 Ext_List
:= Token
= Tok_External_As_List
;
401 Set_Expression_Kind_Of
(External_Value
, In_Tree
, To
=> List
);
403 Set_Expression_Kind_Of
(External_Value
, In_Tree
, To
=> Single
);
406 if Expr_Kind
= Undefined
then
414 Expect
(Tok_Left_Paren
, "`(`");
416 -- Scan past the left parenthesis
418 if Token
= Tok_Left_Paren
then
422 -- Get the name of the external reference
424 Expect
(Tok_String_Literal
, "literal string");
426 if Token
= Tok_String_Literal
then
429 (Of_Kind
=> N_Literal_String
,
431 And_Expr_Kind
=> Single
);
432 Set_String_Value_Of
(Field_Id
, In_Tree
, To
=> Token_Name
);
433 Set_External_Reference_Of
(External_Value
, In_Tree
, To
=> Field_Id
);
435 -- Scan past the first argument
440 when Tok_Right_Paren
=>
442 Error_Msg
(Flags
, "`,` expected", Token_Ptr
);
445 Scan
(In_Tree
); -- scan past right paren
448 Scan
(In_Tree
); -- scan past comma
450 -- Get the string expression for the default
453 Loc
: constant Source_Ptr
:= Token_Ptr
;
458 Expression
=> Field_Id
,
460 Current_Project
=> Current_Project
,
461 Current_Package
=> Current_Package
,
462 Optional_Index
=> False);
464 if Expression_Kind_Of
(Field_Id
, In_Tree
) = List
then
466 (Flags
, "expression must be a single string", Loc
);
468 Set_External_Default_Of
469 (External_Value
, In_Tree
, To
=> Field_Id
);
473 Expect
(Tok_Right_Paren
, "`)`");
475 if Token
= Tok_Right_Paren
then
476 Scan
(In_Tree
); -- scan past right paren
481 Error_Msg
(Flags
, "`,` expected", Token_Ptr
);
483 Error_Msg
(Flags
, "`,` or `)` expected", Token_Ptr
);
487 end External_Reference
;
489 -----------------------
490 -- Parse_Choice_List --
491 -----------------------
493 procedure Parse_Choice_List
494 (In_Tree
: Project_Node_Tree_Ref
;
495 First_Choice
: out Project_Node_Id
;
496 Flags
: Processing_Flags
;
497 String_Type
: Boolean := True)
499 Current_Choice
: Project_Node_Id
:= Empty_Node
;
500 Next_Choice
: Project_Node_Id
:= Empty_Node
;
501 Choice_String
: Name_Id
:= No_Name
;
502 Found
: Boolean := False;
505 -- Declare the node of the first choice
509 (Of_Kind
=> N_Literal_String
,
511 And_Expr_Kind
=> Single
);
513 -- Initially Current_Choice is the same as First_Choice
515 Current_Choice
:= First_Choice
;
518 Expect
(Tok_String_Literal
, "literal string");
519 exit when Token
/= Tok_String_Literal
;
520 Set_Location_Of
(Current_Choice
, In_Tree
, To
=> Token_Ptr
);
521 Choice_String
:= Token_Name
;
523 -- Give the string value to the current choice
525 Set_String_Value_Of
(Current_Choice
, In_Tree
, To
=> Choice_String
);
529 -- Check if the label is part of the string type and if it has not
530 -- been already used.
533 for Choice
in Choice_First
.. Choices
.Last
loop
534 if Choices
.Table
(Choice
).The_String
= Choice_String
then
536 -- This label is part of the string type
540 if Choices
.Table
(Choice
).Already_Used
then
542 -- But it has already appeared in a choice list for this
543 -- case construction so report an error.
545 Error_Msg_Name_1
:= Choice_String
;
546 Error_Msg
(Flags
, "duplicate case label %%", Token_Ptr
);
549 Choices
.Table
(Choice
).Already_Used
:= True;
556 -- If the label is not part of the string list, report an error
559 Error_Msg_Name_1
:= Choice_String
;
560 Error_Msg
(Flags
, "illegal case label %%", Token_Ptr
);
564 -- Scan past the label
568 -- If there is no '|', we are done
570 if Token
= Tok_Vertical_Bar
then
572 -- Otherwise, declare the node of the next choice, link it to
573 -- Current_Choice and set Current_Choice to this new node.
577 (Of_Kind
=> N_Literal_String
,
579 And_Expr_Kind
=> Single
);
580 Set_Next_Literal_String
581 (Current_Choice
, In_Tree
, To
=> Next_Choice
);
582 Current_Choice
:= Next_Choice
;
588 end Parse_Choice_List
;
590 ----------------------
591 -- Parse_Expression --
592 ----------------------
594 procedure Parse_Expression
595 (In_Tree
: Project_Node_Tree_Ref
;
596 Expression
: out Project_Node_Id
;
597 Current_Project
: Project_Node_Id
;
598 Current_Package
: Project_Node_Id
;
599 Optional_Index
: Boolean;
600 Flags
: Processing_Flags
)
602 First_Term
: Project_Node_Id
:= Empty_Node
;
603 Expression_Kind
: Variable_Kind
:= Undefined
;
606 -- Declare the node of the expression
609 Default_Project_Node
(Of_Kind
=> N_Expression
, In_Tree
=> In_Tree
);
610 Set_Location_Of
(Expression
, In_Tree
, To
=> Token_Ptr
);
612 -- Parse the term or terms of the expression
614 Terms
(In_Tree
=> In_Tree
,
616 Expr_Kind
=> Expression_Kind
,
618 Current_Project
=> Current_Project
,
619 Current_Package
=> Current_Package
,
620 Optional_Index
=> Optional_Index
);
622 -- Set the first term and the expression kind
624 Set_First_Term
(Expression
, In_Tree
, To
=> First_Term
);
625 Set_Expression_Kind_Of
(Expression
, In_Tree
, To
=> Expression_Kind
);
626 end Parse_Expression
;
628 ----------------------------
629 -- Parse_String_Type_List --
630 ----------------------------
632 procedure Parse_String_Type_List
633 (In_Tree
: Project_Node_Tree_Ref
;
634 First_String
: out Project_Node_Id
;
635 Flags
: Processing_Flags
)
637 Last_String
: Project_Node_Id
:= Empty_Node
;
638 Next_String
: Project_Node_Id
:= Empty_Node
;
639 String_Value
: Name_Id
:= No_Name
;
642 -- Declare the node of the first string
646 (Of_Kind
=> N_Literal_String
,
648 And_Expr_Kind
=> Single
);
650 -- Initially, Last_String is the same as First_String
652 Last_String
:= First_String
;
655 Expect
(Tok_String_Literal
, "literal string");
656 exit when Token
/= Tok_String_Literal
;
657 String_Value
:= Token_Name
;
659 -- Give its string value to Last_String
661 Set_String_Value_Of
(Last_String
, In_Tree
, To
=> String_Value
);
662 Set_Location_Of
(Last_String
, In_Tree
, To
=> Token_Ptr
);
664 -- Now, check if the string is already part of the string type
667 Current
: Project_Node_Id
:= First_String
;
670 while Current
/= Last_String
loop
671 if String_Value_Of
(Current
, In_Tree
) = String_Value
then
673 -- This is a repetition, report an error
675 Error_Msg_Name_1
:= String_Value
;
676 Error_Msg
(Flags
, "duplicate value %% in type", Token_Ptr
);
680 Current
:= Next_Literal_String
(Current
, In_Tree
);
684 -- Scan past the literal string
688 -- If there is no comma following the literal string, we are done
690 if Token
/= Tok_Comma
then
694 -- Declare the next string, link it to Last_String and set
695 -- Last_String to its node.
699 (Of_Kind
=> N_Literal_String
,
701 And_Expr_Kind
=> Single
);
702 Set_Next_Literal_String
(Last_String
, In_Tree
, To
=> Next_String
);
703 Last_String
:= Next_String
;
707 end Parse_String_Type_List
;
709 ------------------------------
710 -- Parse_Variable_Reference --
711 ------------------------------
713 procedure Parse_Variable_Reference
714 (In_Tree
: Project_Node_Tree_Ref
;
715 Variable
: out Project_Node_Id
;
716 Current_Project
: Project_Node_Id
;
717 Current_Package
: Project_Node_Id
;
718 Flags
: Processing_Flags
)
720 Current_Variable
: Project_Node_Id
:= Empty_Node
;
722 The_Package
: Project_Node_Id
:= Current_Package
;
723 The_Project
: Project_Node_Id
:= Current_Project
;
725 Specified_Project
: Project_Node_Id
:= Empty_Node
;
726 Specified_Package
: Project_Node_Id
:= Empty_Node
;
727 Look_For_Variable
: Boolean := True;
728 First_Attribute
: Attribute_Node_Id
:= Empty_Attribute
;
729 Variable_Name
: Name_Id
;
735 Expect
(Tok_Identifier
, "identifier");
737 if Token
/= Tok_Identifier
then
738 Look_For_Variable
:= False;
742 Add_To_Names
(NL
=> (Name
=> Token_Name
, Location
=> Token_Ptr
));
744 exit when Token
/= Tok_Dot
;
748 if Look_For_Variable
then
750 if Token
= Tok_Apostrophe
then
752 -- Attribute reference
762 -- This may be a project name or a package name.
763 -- Project name have precedence.
765 -- First, look if it can be a package name
769 (Package_Node_Id_Of
(Names
.Table
(1).Name
));
771 -- Now, look if it can be a project name
773 if Names
.Table
(1).Name
=
774 Name_Of
(Current_Project
, In_Tree
)
776 The_Project
:= Current_Project
;
780 Imported_Or_Extended_Project_Of
781 (Current_Project
, In_Tree
, Names
.Table
(1).Name
);
784 if No
(The_Project
) then
786 -- If it is neither a project name nor a package name,
789 if First_Attribute
= Empty_Attribute
then
790 Error_Msg_Name_1
:= Names
.Table
(1).Name
;
791 Error_Msg
(Flags
, "unknown project %",
792 Names
.Table
(1).Location
);
793 First_Attribute
:= Attribute_First
;
796 -- If it is a package name, check if the package has
797 -- already been declared in the current project.
800 First_Package_Of
(Current_Project
, In_Tree
);
802 while Present
(The_Package
)
803 and then Name_Of
(The_Package
, In_Tree
) /=
807 Next_Package_In_Project
(The_Package
, In_Tree
);
810 -- If it has not been already declared, report an
813 if No
(The_Package
) then
814 Error_Msg_Name_1
:= Names
.Table
(1).Name
;
815 Error_Msg
(Flags
, "package % not yet defined",
816 Names
.Table
(1).Location
);
821 -- It is a project name
823 First_Attribute
:= Attribute_First
;
824 The_Package
:= Empty_Node
;
829 -- We have either a project name made of several simple
830 -- names (long project), or a project name (short project)
831 -- followed by a package name. The long project name has
835 Short_Project
: Name_Id
;
836 Long_Project
: Name_Id
;
843 -- Get the name of the short project
845 for Index
in 1 .. Names
.Last
- 1 loop
847 (Get_Name_String
(Names
.Table
(Index
).Name
),
848 Buffer
, Buffer_Last
);
850 if Index
/= Names
.Last
- 1 then
851 Add_To_Buffer
(".", Buffer
, Buffer_Last
);
855 Name_Len
:= Buffer_Last
;
856 Name_Buffer
(1 .. Buffer_Last
) :=
857 Buffer
(1 .. Buffer_Last
);
858 Short_Project
:= Name_Find
;
860 -- Now, add the last simple name to get the name of the
863 Add_To_Buffer
(".", Buffer
, Buffer_Last
);
865 (Get_Name_String
(Names
.Table
(Names
.Last
).Name
),
866 Buffer
, Buffer_Last
);
867 Name_Len
:= Buffer_Last
;
868 Name_Buffer
(1 .. Buffer_Last
) :=
869 Buffer
(1 .. Buffer_Last
);
870 Long_Project
:= Name_Find
;
872 -- Check if the long project is imported or extended
874 if Long_Project
= Name_Of
(Current_Project
, In_Tree
) then
875 The_Project
:= Current_Project
;
879 Imported_Or_Extended_Project_Of
885 -- If the long project exists, then this is the prefix
888 if Present
(The_Project
) then
889 First_Attribute
:= Attribute_First
;
890 The_Package
:= Empty_Node
;
893 -- Otherwise, check if the short project is imported
897 Name_Of
(Current_Project
, In_Tree
)
899 The_Project
:= Current_Project
;
902 The_Project
:= Imported_Or_Extended_Project_Of
903 (Current_Project
, In_Tree
,
907 -- If short project does not exist, report an error
909 if No
(The_Project
) then
910 Error_Msg_Name_1
:= Long_Project
;
911 Error_Msg_Name_2
:= Short_Project
;
912 Error_Msg
(Flags
, "unknown projects % or %",
913 Names
.Table
(1).Location
);
914 The_Package
:= Empty_Node
;
915 First_Attribute
:= Attribute_First
;
918 -- Now, we check if the package has been declared
922 First_Package_Of
(The_Project
, In_Tree
);
923 while Present
(The_Package
)
924 and then Name_Of
(The_Package
, In_Tree
) /=
925 Names
.Table
(Names
.Last
).Name
928 Next_Package_In_Project
(The_Package
, In_Tree
);
931 -- If it has not, then we report an error
933 if No
(The_Package
) then
935 Names
.Table
(Names
.Last
).Name
;
936 Error_Msg_Name_2
:= Short_Project
;
938 "package % not declared in project %",
939 Names
.Table
(Names
.Last
).Location
);
940 First_Attribute
:= Attribute_First
;
943 -- Otherwise, we have the correct project and
948 (Package_Id_Of
(The_Package
, In_Tree
));
959 Current_Project
=> The_Project
,
960 Current_Package
=> The_Package
,
961 First_Attribute
=> First_Attribute
);
968 (Of_Kind
=> N_Variable_Reference
, In_Tree
=> In_Tree
);
970 if Look_For_Variable
then
974 -- Cannot happen (so why null instead of raise PE???)
980 -- Simple variable name
982 Set_Name_Of
(Variable
, In_Tree
, To
=> Names
.Table
(1).Name
);
986 -- Variable name with a simple name prefix that can be
987 -- a project name or a package name. Project names have
988 -- priority over package names.
990 Set_Name_Of
(Variable
, In_Tree
, To
=> Names
.Table
(2).Name
);
992 -- Check if it can be a package name
994 The_Package
:= First_Package_Of
(Current_Project
, In_Tree
);
996 while Present
(The_Package
)
997 and then Name_Of
(The_Package
, In_Tree
) /=
1001 Next_Package_In_Project
(The_Package
, In_Tree
);
1004 -- Now look for a possible project name
1006 The_Project
:= Imported_Or_Extended_Project_Of
1007 (Current_Project
, In_Tree
, Names
.Table
(1).Name
);
1009 if Present
(The_Project
) then
1010 Specified_Project
:= The_Project
;
1012 elsif No
(The_Package
) then
1013 Error_Msg_Name_1
:= Names
.Table
(1).Name
;
1014 Error_Msg
(Flags
, "unknown package or project %",
1015 Names
.Table
(1).Location
);
1016 Look_For_Variable
:= False;
1019 Specified_Package
:= The_Package
;
1024 -- Variable name with a prefix that is either a project name
1025 -- made of several simple names, or a project name followed
1026 -- by a package name.
1029 (Variable
, In_Tree
, To
=> Names
.Table
(Names
.Last
).Name
);
1032 Short_Project
: Name_Id
;
1033 Long_Project
: Name_Id
;
1036 -- First, we get the two possible project names
1042 -- Add all the simple names, except the last two
1044 for Index
in 1 .. Names
.Last
- 2 loop
1046 (Get_Name_String
(Names
.Table
(Index
).Name
),
1047 Buffer
, Buffer_Last
);
1049 if Index
/= Names
.Last
- 2 then
1050 Add_To_Buffer
(".", Buffer
, Buffer_Last
);
1054 Name_Len
:= Buffer_Last
;
1055 Name_Buffer
(1 .. Name_Len
) := Buffer
(1 .. Buffer_Last
);
1056 Short_Project
:= Name_Find
;
1058 -- Add the simple name before the name of the variable
1060 Add_To_Buffer
(".", Buffer
, Buffer_Last
);
1062 (Get_Name_String
(Names
.Table
(Names
.Last
- 1).Name
),
1063 Buffer
, Buffer_Last
);
1064 Name_Len
:= Buffer_Last
;
1065 Name_Buffer
(1 .. Name_Len
) := Buffer
(1 .. Buffer_Last
);
1066 Long_Project
:= Name_Find
;
1068 -- Check if the prefix is the name of an imported or
1069 -- extended project.
1071 The_Project
:= Imported_Or_Extended_Project_Of
1072 (Current_Project
, In_Tree
, Long_Project
);
1074 if Present
(The_Project
) then
1075 Specified_Project
:= The_Project
;
1078 -- Now check if the prefix may be a project name followed
1079 -- by a package name.
1081 -- First check for a possible project name
1084 Imported_Or_Extended_Project_Of
1085 (Current_Project
, In_Tree
, Short_Project
);
1087 if No
(The_Project
) then
1088 -- Unknown prefix, report an error
1090 Error_Msg_Name_1
:= Long_Project
;
1091 Error_Msg_Name_2
:= Short_Project
;
1093 (Flags
, "unknown projects % or %",
1094 Names
.Table
(1).Location
);
1095 Look_For_Variable
:= False;
1098 Specified_Project
:= The_Project
;
1100 -- Now look for the package in this project
1102 The_Package
:= First_Package_Of
(The_Project
, In_Tree
);
1104 while Present
(The_Package
)
1105 and then Name_Of
(The_Package
, In_Tree
) /=
1106 Names
.Table
(Names
.Last
- 1).Name
1109 Next_Package_In_Project
(The_Package
, In_Tree
);
1112 if No
(The_Package
) then
1114 -- The package does not exist, report an error
1116 Error_Msg_Name_1
:= Names
.Table
(2).Name
;
1117 Error_Msg
(Flags
, "unknown package %",
1118 Names
.Table
(Names
.Last
- 1).Location
);
1119 Look_For_Variable
:= False;
1122 Specified_Package
:= The_Package
;
1130 if Look_For_Variable
then
1131 Variable_Name
:= Name_Of
(Variable
, In_Tree
);
1132 Set_Project_Node_Of
(Variable
, In_Tree
, To
=> Specified_Project
);
1133 Set_Package_Node_Of
(Variable
, In_Tree
, To
=> Specified_Package
);
1135 if Present
(Specified_Project
) then
1136 The_Project
:= Specified_Project
;
1138 The_Project
:= Current_Project
;
1141 Current_Variable
:= Empty_Node
;
1143 -- Look for this variable
1145 -- If a package was specified, check if the variable has been
1146 -- declared in this package.
1148 if Present
(Specified_Package
) then
1150 First_Variable_Of
(Specified_Package
, In_Tree
);
1151 while Present
(Current_Variable
)
1153 Name_Of
(Current_Variable
, In_Tree
) /= Variable_Name
1155 Current_Variable
:= Next_Variable
(Current_Variable
, In_Tree
);
1159 -- Otherwise, if no project has been specified and we are in
1160 -- a package, first check if the variable has been declared in
1163 if No
(Specified_Project
)
1164 and then Present
(Current_Package
)
1167 First_Variable_Of
(Current_Package
, In_Tree
);
1168 while Present
(Current_Variable
)
1169 and then Name_Of
(Current_Variable
, In_Tree
) /= Variable_Name
1172 Next_Variable
(Current_Variable
, In_Tree
);
1176 -- If we have not found the variable in the package, check if the
1177 -- variable has been declared in the project, or in any of its
1178 -- ancestors, or in any of the project it extends.
1180 if No
(Current_Variable
) then
1182 Proj
: Project_Node_Id
:= The_Project
;
1186 Current_Variable
:= First_Variable_Of
(Proj
, In_Tree
);
1188 Present
(Current_Variable
)
1190 Name_Of
(Current_Variable
, In_Tree
) /= Variable_Name
1193 Next_Variable
(Current_Variable
, In_Tree
);
1196 exit when Present
(Current_Variable
);
1198 -- If the current project is a child project, check if
1199 -- the variable is declared in its parent. Otherwise, if
1200 -- the current project extends another project, check if
1201 -- the variable is declared in one of the projects the
1202 -- current project extends.
1204 if No
(Parent_Project_Of
(Proj
, In_Tree
)) then
1207 (Project_Declaration_Of
(Proj
, In_Tree
), In_Tree
);
1209 Proj
:= Parent_Project_Of
(Proj
, In_Tree
);
1212 Set_Project_Node_Of
(Variable
, In_Tree
, To
=> Proj
);
1214 exit when No
(Proj
);
1220 -- If the variable was not found, report an error
1222 if No
(Current_Variable
) then
1223 Error_Msg_Name_1
:= Variable_Name
;
1225 (Flags
, "unknown variable %", Names
.Table
(Names
.Last
).Location
);
1229 if Present
(Current_Variable
) then
1230 Set_Expression_Kind_Of
1232 To
=> Expression_Kind_Of
(Current_Variable
, In_Tree
));
1234 if Kind_Of
(Current_Variable
, In_Tree
) =
1235 N_Typed_Variable_Declaration
1239 To
=> String_Type_Of
(Current_Variable
, In_Tree
));
1243 -- If the variable is followed by a left parenthesis, report an error
1244 -- but attempt to scan the index.
1246 if Token
= Tok_Left_Paren
then
1248 (Flags
, "\variables cannot be associative arrays", Token_Ptr
);
1250 Expect
(Tok_String_Literal
, "literal string");
1252 if Token
= Tok_String_Literal
then
1254 Expect
(Tok_Right_Paren
, "`)`");
1256 if Token
= Tok_Right_Paren
then
1261 end Parse_Variable_Reference
;
1263 ---------------------------------
1264 -- Start_New_Case_Construction --
1265 ---------------------------------
1267 procedure Start_New_Case_Construction
1268 (In_Tree
: Project_Node_Tree_Ref
;
1269 String_Type
: Project_Node_Id
)
1271 Current_String
: Project_Node_Id
;
1274 -- Set Choice_First, depending on whether this is the first case
1275 -- construction or not.
1277 if Choice_First
= 0 then
1279 Choices
.Set_Last
(First_Choice_Node_Id
);
1281 Choice_First
:= Choices
.Last
+ 1;
1284 -- Add the literal of the string type to the Choices table
1286 if Present
(String_Type
) then
1287 Current_String
:= First_Literal_String
(String_Type
, In_Tree
);
1288 while Present
(Current_String
) loop
1289 Add
(This_String
=> String_Value_Of
(Current_String
, In_Tree
));
1290 Current_String
:= Next_Literal_String
(Current_String
, In_Tree
);
1294 -- Set the value of the last choice in table Choice_Lasts
1296 Choice_Lasts
.Increment_Last
;
1297 Choice_Lasts
.Table
(Choice_Lasts
.Last
) := Choices
.Last
;
1298 end Start_New_Case_Construction
;
1305 (In_Tree
: Project_Node_Tree_Ref
;
1306 Term
: out Project_Node_Id
;
1307 Expr_Kind
: in out Variable_Kind
;
1308 Current_Project
: Project_Node_Id
;
1309 Current_Package
: Project_Node_Id
;
1310 Optional_Index
: Boolean;
1311 Flags
: Processing_Flags
)
1313 Next_Term
: Project_Node_Id
:= Empty_Node
;
1314 Term_Id
: Project_Node_Id
:= Empty_Node
;
1315 Current_Expression
: Project_Node_Id
:= Empty_Node
;
1316 Next_Expression
: Project_Node_Id
:= Empty_Node
;
1317 Current_Location
: Source_Ptr
:= No_Location
;
1318 Reference
: Project_Node_Id
:= Empty_Node
;
1321 -- Declare a new node for the term
1323 Term
:= Default_Project_Node
(Of_Kind
=> N_Term
, In_Tree
=> In_Tree
);
1324 Set_Location_Of
(Term
, In_Tree
, To
=> Token_Ptr
);
1327 when Tok_Left_Paren
=>
1329 -- If we have a left parenthesis and we don't know the expression
1330 -- kind, then this is a string list.
1341 -- If we already know that this is a single string, report
1342 -- an error, but set the expression kind to string list to
1343 -- avoid several errors.
1347 (Flags
, "literal string list cannot appear in a string",
1351 -- Declare a new node for this literal string list
1353 Term_Id
:= Default_Project_Node
1354 (Of_Kind
=> N_Literal_String_List
,
1356 And_Expr_Kind
=> List
);
1357 Set_Current_Term
(Term
, In_Tree
, To
=> Term_Id
);
1358 Set_Location_Of
(Term
, In_Tree
, To
=> Token_Ptr
);
1360 -- Scan past the left parenthesis
1364 -- If the left parenthesis is immediately followed by a right
1365 -- parenthesis, the literal string list is empty.
1367 if Token
= Tok_Right_Paren
then
1371 -- Otherwise parse the expression(s) in the literal string list
1374 Current_Location
:= Token_Ptr
;
1376 (In_Tree
=> In_Tree
,
1377 Expression
=> Next_Expression
,
1379 Current_Project
=> Current_Project
,
1380 Current_Package
=> Current_Package
,
1381 Optional_Index
=> Optional_Index
);
1383 -- The expression kind is String list, report an error
1385 if Expression_Kind_Of
(Next_Expression
, In_Tree
) = List
then
1386 Error_Msg
(Flags
, "single expression expected",
1390 -- If Current_Expression is empty, it means that the
1391 -- expression is the first in the string list.
1393 if No
(Current_Expression
) then
1394 Set_First_Expression_In_List
1395 (Term_Id
, In_Tree
, To
=> Next_Expression
);
1397 Set_Next_Expression_In_List
1398 (Current_Expression
, In_Tree
, To
=> Next_Expression
);
1401 Current_Expression
:= Next_Expression
;
1403 -- If there is a comma, continue with the next expression
1405 exit when Token
/= Tok_Comma
;
1406 Scan
(In_Tree
); -- past the comma
1409 -- We expect a closing right parenthesis
1411 Expect
(Tok_Right_Paren
, "`)`");
1413 if Token
= Tok_Right_Paren
then
1418 when Tok_String_Literal
=>
1420 -- If we don't know the expression kind (first term), then it is
1423 if Expr_Kind
= Undefined
then
1424 Expr_Kind
:= Single
;
1427 -- Declare a new node for the string literal
1430 Default_Project_Node
1431 (Of_Kind
=> N_Literal_String
, In_Tree
=> In_Tree
);
1432 Set_Current_Term
(Term
, In_Tree
, To
=> Term_Id
);
1433 Set_String_Value_Of
(Term_Id
, In_Tree
, To
=> Token_Name
);
1435 -- Scan past the string literal
1439 -- Check for possible index expression
1441 if Token
= Tok_At
then
1442 if not Optional_Index
then
1443 Error_Msg
(Flags
, "index not allowed here", Token_Ptr
);
1446 if Token
= Tok_Integer_Literal
then
1450 -- Set the index value
1454 Expect
(Tok_Integer_Literal
, "integer literal");
1456 if Token
= Tok_Integer_Literal
then
1458 Index
: constant Int
:= UI_To_Int
(Int_Literal_Value
);
1462 (Flags
, "index cannot be zero", Token_Ptr
);
1465 (Term_Id
, In_Tree
, To
=> Index
);
1474 when Tok_Identifier
=>
1475 Current_Location
:= Token_Ptr
;
1477 -- Get the variable or attribute reference
1479 Parse_Variable_Reference
1480 (In_Tree
=> In_Tree
,
1481 Variable
=> Reference
,
1483 Current_Project
=> Current_Project
,
1484 Current_Package
=> Current_Package
);
1485 Set_Current_Term
(Term
, In_Tree
, To
=> Reference
);
1487 if Present
(Reference
) then
1489 -- If we don't know the expression kind (first term), then it
1490 -- has the kind of the variable or attribute reference.
1492 if Expr_Kind
= Undefined
then
1493 Expr_Kind
:= Expression_Kind_Of
(Reference
, In_Tree
);
1495 elsif Expr_Kind
= Single
1496 and then Expression_Kind_Of
(Reference
, In_Tree
) = List
1498 -- If the expression is a single list, and the reference is
1499 -- a string list, report an error, and set the expression
1500 -- kind to string list to avoid multiple errors.
1505 "list variable cannot appear in single string expression",
1512 -- Project can appear in an expression as the prefix of an
1513 -- attribute reference of the current project.
1515 Current_Location
:= Token_Ptr
;
1517 Expect
(Tok_Apostrophe
, "`'`");
1519 if Token
= Tok_Apostrophe
then
1521 (In_Tree
=> In_Tree
,
1522 Reference
=> Reference
,
1524 First_Attribute
=> Prj
.Attr
.Attribute_First
,
1525 Current_Project
=> Current_Project
,
1526 Current_Package
=> Empty_Node
);
1527 Set_Current_Term
(Term
, In_Tree
, To
=> Reference
);
1530 -- Same checks as above for the expression kind
1532 if Present
(Reference
) then
1533 if Expr_Kind
= Undefined
then
1534 Expr_Kind
:= Expression_Kind_Of
(Reference
, In_Tree
);
1536 elsif Expr_Kind
= Single
1537 and then Expression_Kind_Of
(Reference
, In_Tree
) = List
1540 (Flags
, "lists cannot appear in single string expression",
1545 when Tok_External | Tok_External_As_List
=>
1547 (In_Tree
=> In_Tree
,
1549 Current_Project
=> Current_Project
,
1550 Current_Package
=> Current_Package
,
1551 Expr_Kind
=> Expr_Kind
,
1552 External_Value
=> Reference
);
1553 Set_Current_Term
(Term
, In_Tree
, To
=> Reference
);
1556 Error_Msg
(Flags
, "cannot be part of an expression", Token_Ptr
);
1561 -- If there is an '&', call Terms recursively
1563 if Token
= Tok_Ampersand
then
1564 Scan
(In_Tree
); -- scan past ampersand
1567 (In_Tree
=> In_Tree
,
1569 Expr_Kind
=> Expr_Kind
,
1571 Current_Project
=> Current_Project
,
1572 Current_Package
=> Current_Package
,
1573 Optional_Index
=> Optional_Index
);
1575 -- And link the next term to this term
1577 Set_Next_Term
(Term
, In_Tree
, To
=> Next_Term
);