1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2002, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
30 with Namet; use Namet;
31 with Nlists; use Nlists;
32 with Nmake; use Nmake;
33 with Restrict; use Restrict;
34 with Sinfo; use Sinfo;
35 with Snames; use Snames;
36 with Stand; use Stand;
37 with Uintp; use Uintp;
39 package body Tbuild is
41 -----------------------
42 -- Local Subprograms --
43 -----------------------
45 procedure Add_Unique_Serial_Number;
46 -- Add a unique serialization to the string in the Name_Buffer. This
47 -- consists of a unit specific serial number, and b/s for body/spec.
49 ------------------------------
50 -- Add_Unique_Serial_Number --
51 ------------------------------
53 procedure Add_Unique_Serial_Number is
54 Unit_Node : constant Node_Id := Unit (Cunit (Current_Sem_Unit));
57 Add_Nat_To_Name_Buffer (Increment_Serial_Number);
59 -- Add either b or s, depending on whether current unit is a spec
60 -- or a body. This is needed because we may generate the same name
61 -- in a spec and a body otherwise.
63 Name_Len := Name_Len + 1;
65 if Nkind (Unit_Node) = N_Package_Declaration
66 or else Nkind (Unit_Node) = N_Subprogram_Declaration
67 or else Nkind (Unit_Node) in N_Generic_Declaration
69 Name_Buffer (Name_Len) := 's';
71 Name_Buffer (Name_Len) := 'b';
73 end Add_Unique_Serial_Number;
79 function Checks_Off (N : Node_Id) return Node_Id is
82 Make_Unchecked_Expression (Sloc (N),
90 function Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
94 if Present (Etype (Expr))
95 and then (Etype (Expr)) = Typ
97 return Relocate_Node (Expr);
100 Make_Type_Conversion (Sloc (Expr),
101 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
102 Expression => Relocate_Node (Expr));
104 Set_Etype (Result, Typ);
109 -------------------------------------------
110 -- Make_Byte_Aligned_Attribute_Reference --
111 -------------------------------------------
113 function Make_Byte_Aligned_Attribute_Reference
116 Attribute_Name : Name_Id)
119 N : constant Node_Id :=
120 Make_Attribute_Reference (Sloc,
122 Attribute_Name => Attribute_Name);
125 pragma Assert (Attribute_Name = Name_Address
127 Attribute_Name = Name_Unrestricted_Access);
128 Set_Must_Be_Byte_Aligned (N, True);
130 end Make_Byte_Aligned_Attribute_Reference;
136 function Make_DT_Access
142 Full_Type : Entity_Id := Typ;
145 if Is_Private_Type (Typ) then
146 Full_Type := Underlying_Type (Typ);
150 Unchecked_Convert_To (
151 New_Occurrence_Of (Etype (Access_Disp_Table (Full_Type)), Loc),
152 Make_Selected_Component (Loc,
153 Prefix => New_Copy (Rec),
155 New_Reference_To (Tag_Component (Full_Type), Loc)));
158 -----------------------
159 -- Make_DT_Component --
160 -----------------------
162 function Make_DT_Component
169 Full_Type : Entity_Id := Typ;
172 if Is_Private_Type (Typ) then
173 Full_Type := Underlying_Type (Typ);
176 X := First_Component (
177 Designated_Type (Etype (Access_Disp_Table (Full_Type))));
180 X := Next_Component (X);
183 return New_Reference_To (X, Loc);
184 end Make_DT_Component;
186 --------------------------------
187 -- Make_Implicit_If_Statement --
188 --------------------------------
190 function Make_Implicit_If_Statement
193 Then_Statements : List_Id;
194 Elsif_Parts : List_Id := No_List;
195 Else_Statements : List_Id := No_List)
199 Check_Restriction (No_Implicit_Conditionals, Node);
200 return Make_If_Statement (Sloc (Node),
205 end Make_Implicit_If_Statement;
207 -------------------------------------
208 -- Make_Implicit_Label_Declaration --
209 -------------------------------------
211 function Make_Implicit_Label_Declaration
213 Defining_Identifier : Node_Id;
214 Label_Construct : Node_Id)
217 N : constant Node_Id :=
218 Make_Implicit_Label_Declaration (Loc, Defining_Identifier);
221 Set_Label_Construct (N, Label_Construct);
223 end Make_Implicit_Label_Declaration;
225 ----------------------------------
226 -- Make_Implicit_Loop_Statement --
227 ----------------------------------
229 function Make_Implicit_Loop_Statement
231 Statements : List_Id;
232 Identifier : Node_Id := Empty;
233 Iteration_Scheme : Node_Id := Empty;
234 Has_Created_Identifier : Boolean := False;
235 End_Label : Node_Id := Empty)
239 Check_Restriction (No_Implicit_Loops, Node);
241 if Present (Iteration_Scheme)
242 and then Present (Condition (Iteration_Scheme))
244 Check_Restriction (No_Implicit_Conditionals, Node);
247 return Make_Loop_Statement (Sloc (Node),
248 Identifier => Identifier,
249 Iteration_Scheme => Iteration_Scheme,
250 Statements => Statements,
251 Has_Created_Identifier => Has_Created_Identifier,
252 End_Label => End_Label);
253 end Make_Implicit_Loop_Statement;
255 --------------------------
256 -- Make_Integer_Literal --
257 ---------------------------
259 function Make_Integer_Literal
265 return Make_Integer_Literal (Loc, UI_From_Int (Intval));
266 end Make_Integer_Literal;
268 ---------------------------------
269 -- Make_Raise_Constraint_Error --
270 ---------------------------------
272 function Make_Raise_Constraint_Error
274 Condition : Node_Id := Empty;
275 Reason : RT_Exception_Code)
279 pragma Assert (Reason in RT_CE_Exceptions);
281 Make_Raise_Constraint_Error (Sloc,
282 Condition => Condition,
284 UI_From_Int (RT_Exception_Code'Pos (Reason)));
285 end Make_Raise_Constraint_Error;
287 ------------------------------
288 -- Make_Raise_Program_Error --
289 ------------------------------
291 function Make_Raise_Program_Error
293 Condition : Node_Id := Empty;
294 Reason : RT_Exception_Code)
298 pragma Assert (Reason in RT_PE_Exceptions);
300 Make_Raise_Program_Error (Sloc,
301 Condition => Condition,
303 UI_From_Int (RT_Exception_Code'Pos (Reason)));
304 end Make_Raise_Program_Error;
306 ------------------------------
307 -- Make_Raise_Storage_Error --
308 ------------------------------
310 function Make_Raise_Storage_Error
312 Condition : Node_Id := Empty;
313 Reason : RT_Exception_Code)
317 pragma Assert (Reason in RT_SE_Exceptions);
319 Make_Raise_Storage_Error (Sloc,
320 Condition => Condition,
322 UI_From_Int (RT_Exception_Code'Pos (Reason)));
323 end Make_Raise_Storage_Error;
325 ---------------------------
326 -- Make_Unsuppress_Block --
327 ---------------------------
329 -- Generates the following expansion:
332 -- pragma Suppress (<check>);
337 function Make_Unsuppress_Block
345 Make_Block_Statement (Loc,
346 Declarations => New_List (
348 Chars => Name_Suppress,
349 Pragma_Argument_Associations => New_List (
350 Make_Pragma_Argument_Association (Loc,
351 Expression => Make_Identifier (Loc, Check))))),
353 Handled_Statement_Sequence =>
354 Make_Handled_Sequence_Of_Statements (Loc,
355 Statements => Stmts));
356 end Make_Unsuppress_Block;
358 --------------------------
359 -- New_Constraint_Error --
360 --------------------------
362 function New_Constraint_Error (Loc : Source_Ptr) return Node_Id is
363 Ident_Node : Node_Id;
364 Raise_Node : Node_Id;
367 Ident_Node := New_Node (N_Identifier, Loc);
368 Set_Chars (Ident_Node, Chars (Standard_Entity (S_Constraint_Error)));
369 Set_Entity (Ident_Node, Standard_Entity (S_Constraint_Error));
370 Raise_Node := New_Node (N_Raise_Statement, Loc);
371 Set_Name (Raise_Node, Ident_Node);
373 end New_Constraint_Error;
375 -----------------------
376 -- New_External_Name --
377 -----------------------
379 function New_External_Name
380 (Related_Id : Name_Id;
381 Suffix : Character := ' ';
382 Suffix_Index : Int := 0;
383 Prefix : Character := ' ')
387 Get_Name_String (Related_Id);
389 if Prefix /= ' ' then
390 pragma Assert (Is_OK_Internal_Letter (Prefix));
392 for J in reverse 1 .. Name_Len loop
393 Name_Buffer (J + 1) := Name_Buffer (J);
396 Name_Len := Name_Len + 1;
397 Name_Buffer (1) := Prefix;
400 if Suffix /= ' ' then
401 pragma Assert (Is_OK_Internal_Letter (Suffix));
402 Name_Len := Name_Len + 1;
403 Name_Buffer (Name_Len) := Suffix;
406 if Suffix_Index /= 0 then
407 if Suffix_Index < 0 then
408 Add_Unique_Serial_Number;
410 Add_Nat_To_Name_Buffer (Suffix_Index);
415 end New_External_Name;
417 function New_External_Name
418 (Related_Id : Name_Id;
420 Suffix_Index : Int := 0;
421 Prefix : Character := ' ')
425 Get_Name_String (Related_Id);
427 if Prefix /= ' ' then
428 pragma Assert (Is_OK_Internal_Letter (Prefix));
430 for J in reverse 1 .. Name_Len loop
431 Name_Buffer (J + 1) := Name_Buffer (J);
434 Name_Len := Name_Len + 1;
435 Name_Buffer (1) := Prefix;
439 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
440 Name_Len := Name_Len + Suffix'Length;
443 if Suffix_Index /= 0 then
444 if Suffix_Index < 0 then
445 Add_Unique_Serial_Number;
447 Add_Nat_To_Name_Buffer (Suffix_Index);
452 end New_External_Name;
454 function New_External_Name
460 Name_Buffer (1) := Suffix;
462 Add_Nat_To_Name_Buffer (Suffix_Index);
464 end New_External_Name;
466 -----------------------
467 -- New_Internal_Name --
468 -----------------------
470 function New_Internal_Name (Id_Char : Character) return Name_Id is
472 pragma Assert (Is_OK_Internal_Letter (Id_Char));
473 Name_Buffer (1) := Id_Char;
475 Add_Unique_Serial_Number;
477 end New_Internal_Name;
479 -----------------------
480 -- New_Occurrence_Of --
481 -----------------------
483 function New_Occurrence_Of
488 Occurrence : Node_Id;
491 Occurrence := New_Node (N_Identifier, Loc);
492 Set_Chars (Occurrence, Chars (Def_Id));
493 Set_Entity (Occurrence, Def_Id);
495 if Is_Type (Def_Id) then
496 Set_Etype (Occurrence, Def_Id);
498 Set_Etype (Occurrence, Etype (Def_Id));
502 end New_Occurrence_Of;
504 ----------------------
505 -- New_Reference_To --
506 ----------------------
508 function New_Reference_To
513 Occurrence : Node_Id;
516 Occurrence := New_Node (N_Identifier, Loc);
517 Set_Chars (Occurrence, Chars (Def_Id));
518 Set_Entity (Occurrence, Def_Id);
520 end New_Reference_To;
522 -----------------------
523 -- New_Suffixed_Name --
524 -----------------------
526 function New_Suffixed_Name
527 (Related_Id : Name_Id;
532 Get_Name_String (Related_Id);
533 Name_Len := Name_Len + 1;
534 Name_Buffer (Name_Len) := '_';
535 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
536 Name_Len := Name_Len + Suffix'Length;
538 end New_Suffixed_Name;
544 function OK_Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
549 Make_Type_Conversion (Sloc (Expr),
550 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
551 Expression => Relocate_Node (Expr));
552 Set_Conversion_OK (Result, True);
553 Set_Etype (Result, Typ);
557 --------------------------
558 -- Unchecked_Convert_To --
559 --------------------------
561 function Unchecked_Convert_To
566 Loc : constant Source_Ptr := Sloc (Expr);
570 -- If the expression is already of the correct type, then nothing
571 -- to do, except for relocating the node in case this is required.
573 if Present (Etype (Expr))
574 and then (Base_Type (Etype (Expr)) = Typ
575 or else Etype (Expr) = Typ)
577 return Relocate_Node (Expr);
579 -- Cases where the inner expression is itself an unchecked conversion
580 -- to the same type, and we can thus eliminate the outer conversion.
582 elsif Nkind (Expr) = N_Unchecked_Type_Conversion
583 and then Entity (Subtype_Mark (Expr)) = Typ
585 Result := Relocate_Node (Expr);
591 Make_Unchecked_Type_Conversion (Loc,
592 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
593 Expression => Relocate_Node (Expr));
596 Set_Etype (Result, Typ);
598 end Unchecked_Convert_To;