gcc/
[official-gcc.git] / gcc / ada / tbuild.adb
blob17ca12eac8cb95a0e1e08ed3d22e42f9bd64bddb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- T B U I L D --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Csets; use Csets;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Lib; use Lib;
31 with Nlists; use Nlists;
32 with Nmake; use Nmake;
33 with Opt; use Opt;
34 with Restrict; use Restrict;
35 with Rident; use Rident;
36 with Sem_Aux; use Sem_Aux;
37 with Snames; use Snames;
38 with Stand; use Stand;
39 with Stringt; use Stringt;
40 with Urealp; use Urealp;
42 package body Tbuild is
44 -----------------------
45 -- Local Subprograms --
46 -----------------------
48 procedure Add_Unique_Serial_Number;
49 -- Add a unique serialization to the string in the Name_Buffer. This
50 -- consists of a unit specific serial number, and b/s for body/spec.
52 ------------------------------
53 -- Add_Unique_Serial_Number --
54 ------------------------------
56 Config_Serial_Number : Nat := 0;
57 -- Counter for use in config pragmas, see comment below
59 procedure Add_Unique_Serial_Number is
60 begin
61 -- If we are analyzing configuration pragmas, Cunit (Main_Unit) will
62 -- not be set yet. This happens for example when analyzing static
63 -- string expressions in configuration pragmas. For this case, we
64 -- just maintain a local counter, defined above and we do not need
65 -- to add a b or s indication in this case.
67 if No (Cunit (Current_Sem_Unit)) then
68 Config_Serial_Number := Config_Serial_Number + 1;
69 Add_Nat_To_Name_Buffer (Config_Serial_Number);
70 return;
72 -- Normal case, within a unit
74 else
75 declare
76 Unit_Node : constant Node_Id := Unit (Cunit (Current_Sem_Unit));
78 begin
79 Add_Nat_To_Name_Buffer (Increment_Serial_Number);
81 -- Add either b or s, depending on whether current unit is a spec
82 -- or a body. This is needed because we may generate the same name
83 -- in a spec and a body otherwise.
85 Name_Len := Name_Len + 1;
87 if Nkind (Unit_Node) = N_Package_Declaration
88 or else Nkind (Unit_Node) = N_Subprogram_Declaration
89 or else Nkind (Unit_Node) in N_Generic_Declaration
90 then
91 Name_Buffer (Name_Len) := 's';
92 else
93 Name_Buffer (Name_Len) := 'b';
94 end if;
95 end;
96 end if;
97 end Add_Unique_Serial_Number;
99 ----------------
100 -- Checks_Off --
101 ----------------
103 function Checks_Off (N : Node_Id) return Node_Id is
104 begin
105 return
106 Make_Unchecked_Expression (Sloc (N),
107 Expression => N);
108 end Checks_Off;
110 ----------------
111 -- Convert_To --
112 ----------------
114 function Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
115 Result : Node_Id;
117 begin
118 if Present (Etype (Expr))
119 and then (Etype (Expr)) = Typ
120 then
121 return Relocate_Node (Expr);
122 else
123 Result :=
124 Make_Type_Conversion (Sloc (Expr),
125 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
126 Expression => Relocate_Node (Expr));
128 Set_Etype (Result, Typ);
129 return Result;
130 end if;
131 end Convert_To;
133 ----------------------------
134 -- Convert_To_And_Rewrite --
135 ----------------------------
137 procedure Convert_To_And_Rewrite (Typ : Entity_Id; Expr : Node_Id) is
138 begin
139 Rewrite (Expr, Convert_To (Typ, Expr));
140 end Convert_To_And_Rewrite;
142 ------------------
143 -- Discard_List --
144 ------------------
146 procedure Discard_List (L : List_Id) is
147 pragma Warnings (Off, L);
148 begin
149 null;
150 end Discard_List;
152 ------------------
153 -- Discard_Node --
154 ------------------
156 procedure Discard_Node (N : Node_Or_Entity_Id) is
157 pragma Warnings (Off, N);
158 begin
159 null;
160 end Discard_Node;
162 -------------------------------------------
163 -- Make_Byte_Aligned_Attribute_Reference --
164 -------------------------------------------
166 function Make_Byte_Aligned_Attribute_Reference
167 (Sloc : Source_Ptr;
168 Prefix : Node_Id;
169 Attribute_Name : Name_Id)
170 return Node_Id
172 N : constant Node_Id :=
173 Make_Attribute_Reference (Sloc,
174 Prefix => Prefix,
175 Attribute_Name => Attribute_Name);
177 begin
178 pragma Assert (Nam_In (Attribute_Name, Name_Address,
179 Name_Unrestricted_Access));
180 Set_Must_Be_Byte_Aligned (N, True);
181 return N;
182 end Make_Byte_Aligned_Attribute_Reference;
184 --------------------
185 -- Make_DT_Access --
186 --------------------
188 function Make_DT_Access
189 (Loc : Source_Ptr;
190 Rec : Node_Id;
191 Typ : Entity_Id) return Node_Id
193 Full_Type : Entity_Id := Typ;
195 begin
196 if Is_Private_Type (Typ) then
197 Full_Type := Underlying_Type (Typ);
198 end if;
200 return
201 Unchecked_Convert_To (
202 New_Occurrence_Of
203 (Etype (Node (First_Elmt (Access_Disp_Table (Full_Type)))), Loc),
204 Make_Selected_Component (Loc,
205 Prefix => New_Copy (Rec),
206 Selector_Name =>
207 New_Occurrence_Of (First_Tag_Component (Full_Type), Loc)));
208 end Make_DT_Access;
210 ------------------------
211 -- Make_Float_Literal --
212 ------------------------
214 function Make_Float_Literal
215 (Loc : Source_Ptr;
216 Radix : Uint;
217 Significand : Uint;
218 Exponent : Uint) return Node_Id
220 begin
221 if Radix = 2 and then abs Significand /= 1 then
222 return
223 Make_Float_Literal
224 (Loc, Uint_16,
225 Significand * Radix**(Exponent mod 4),
226 Exponent / 4);
228 else
229 declare
230 N : constant Node_Id := New_Node (N_Real_Literal, Loc);
232 begin
233 Set_Realval (N,
234 UR_From_Components
235 (Num => abs Significand,
236 Den => -Exponent,
237 Rbase => UI_To_Int (Radix),
238 Negative => Significand < 0));
239 return N;
240 end;
241 end if;
242 end Make_Float_Literal;
244 -------------
245 -- Make_Id --
246 -------------
248 function Make_Id (Str : Text_Buffer) return Node_Id is
249 begin
250 Name_Len := 0;
252 for J in Str'Range loop
253 Name_Len := Name_Len + 1;
254 Name_Buffer (Name_Len) := Fold_Lower (Str (J));
255 end loop;
257 return
258 Make_Identifier (System_Location,
259 Chars => Name_Find);
260 end Make_Id;
262 -------------------------------------
263 -- Make_Implicit_Exception_Handler --
264 -------------------------------------
266 function Make_Implicit_Exception_Handler
267 (Sloc : Source_Ptr;
268 Choice_Parameter : Node_Id := Empty;
269 Exception_Choices : List_Id;
270 Statements : List_Id) return Node_Id
272 Handler : Node_Id;
273 Loc : Source_Ptr;
275 begin
276 -- Set the source location only when debugging the expanded code
278 -- When debugging the source code directly, we do not want the compiler
279 -- to associate this implicit exception handler with any specific source
280 -- line, because it can potentially confuse the debugger. The most
281 -- damaging situation would arise when the debugger tries to insert a
282 -- breakpoint at a certain line. If the code of the associated implicit
283 -- exception handler is generated before the code of that line, then the
284 -- debugger will end up inserting the breakpoint inside the exception
285 -- handler, rather than the code the user intended to break on. As a
286 -- result, it is likely that the program will not hit the breakpoint
287 -- as expected.
289 if Debug_Generated_Code then
290 Loc := Sloc;
291 else
292 Loc := No_Location;
293 end if;
295 Handler :=
296 Make_Exception_Handler
297 (Loc, Choice_Parameter, Exception_Choices, Statements);
298 Set_Local_Raise_Statements (Handler, No_Elist);
299 return Handler;
300 end Make_Implicit_Exception_Handler;
302 --------------------------------
303 -- Make_Implicit_If_Statement --
304 --------------------------------
306 function Make_Implicit_If_Statement
307 (Node : Node_Id;
308 Condition : Node_Id;
309 Then_Statements : List_Id;
310 Elsif_Parts : List_Id := No_List;
311 Else_Statements : List_Id := No_List) return Node_Id
313 begin
314 Check_Restriction (No_Implicit_Conditionals, Node);
316 return Make_If_Statement (Sloc (Node),
317 Condition,
318 Then_Statements,
319 Elsif_Parts,
320 Else_Statements);
321 end Make_Implicit_If_Statement;
323 -------------------------------------
324 -- Make_Implicit_Label_Declaration --
325 -------------------------------------
327 function Make_Implicit_Label_Declaration
328 (Loc : Source_Ptr;
329 Defining_Identifier : Node_Id;
330 Label_Construct : Node_Id) return Node_Id
332 N : constant Node_Id :=
333 Make_Implicit_Label_Declaration (Loc, Defining_Identifier);
334 begin
335 Set_Label_Construct (N, Label_Construct);
336 return N;
337 end Make_Implicit_Label_Declaration;
339 ----------------------------------
340 -- Make_Implicit_Loop_Statement --
341 ----------------------------------
343 function Make_Implicit_Loop_Statement
344 (Node : Node_Id;
345 Statements : List_Id;
346 Identifier : Node_Id := Empty;
347 Iteration_Scheme : Node_Id := Empty;
348 Has_Created_Identifier : Boolean := False;
349 End_Label : Node_Id := Empty) return Node_Id
351 begin
352 Check_Restriction (No_Implicit_Loops, Node);
354 if Present (Iteration_Scheme)
355 and then Present (Condition (Iteration_Scheme))
356 then
357 Check_Restriction (No_Implicit_Conditionals, Node);
358 end if;
360 return Make_Loop_Statement (Sloc (Node),
361 Identifier => Identifier,
362 Iteration_Scheme => Iteration_Scheme,
363 Statements => Statements,
364 Has_Created_Identifier => Has_Created_Identifier,
365 End_Label => End_Label);
366 end Make_Implicit_Loop_Statement;
368 --------------------------
369 -- Make_Integer_Literal --
370 ---------------------------
372 function Make_Integer_Literal
373 (Loc : Source_Ptr;
374 Intval : Int) return Node_Id
376 begin
377 return Make_Integer_Literal (Loc, UI_From_Int (Intval));
378 end Make_Integer_Literal;
380 --------------------------------
381 -- Make_Linker_Section_Pragma --
382 --------------------------------
384 function Make_Linker_Section_Pragma
385 (Ent : Entity_Id;
386 Loc : Source_Ptr;
387 Sec : String) return Node_Id
389 LS : Node_Id;
391 begin
392 LS :=
393 Make_Pragma
394 (Loc,
395 Name_Linker_Section,
396 New_List
397 (Make_Pragma_Argument_Association
398 (Sloc => Loc,
399 Expression => New_Occurrence_Of (Ent, Loc)),
400 Make_Pragma_Argument_Association
401 (Sloc => Loc,
402 Expression =>
403 Make_String_Literal
404 (Sloc => Loc,
405 Strval => Sec))));
407 Set_Has_Gigi_Rep_Item (Ent);
408 return LS;
409 end Make_Linker_Section_Pragma;
411 -----------------
412 -- Make_Pragma --
413 -----------------
415 function Make_Pragma
416 (Sloc : Source_Ptr;
417 Chars : Name_Id;
418 Pragma_Argument_Associations : List_Id := No_List) return Node_Id
420 begin
421 return
422 Make_Pragma (Sloc,
423 Pragma_Argument_Associations => Pragma_Argument_Associations,
424 Pragma_Identifier => Make_Identifier (Sloc, Chars));
425 end Make_Pragma;
427 ---------------------------------
428 -- Make_Raise_Constraint_Error --
429 ---------------------------------
431 function Make_Raise_Constraint_Error
432 (Sloc : Source_Ptr;
433 Condition : Node_Id := Empty;
434 Reason : RT_Exception_Code) return Node_Id
436 begin
437 pragma Assert (Reason in RT_CE_Exceptions);
438 return
439 Make_Raise_Constraint_Error (Sloc,
440 Condition => Condition,
441 Reason =>
442 UI_From_Int (RT_Exception_Code'Pos (Reason)));
443 end Make_Raise_Constraint_Error;
445 ------------------------------
446 -- Make_Raise_Program_Error --
447 ------------------------------
449 function Make_Raise_Program_Error
450 (Sloc : Source_Ptr;
451 Condition : Node_Id := Empty;
452 Reason : RT_Exception_Code) return Node_Id
454 begin
455 pragma Assert (Reason in RT_PE_Exceptions);
456 return
457 Make_Raise_Program_Error (Sloc,
458 Condition => Condition,
459 Reason =>
460 UI_From_Int (RT_Exception_Code'Pos (Reason)));
461 end Make_Raise_Program_Error;
463 ------------------------------
464 -- Make_Raise_Storage_Error --
465 ------------------------------
467 function Make_Raise_Storage_Error
468 (Sloc : Source_Ptr;
469 Condition : Node_Id := Empty;
470 Reason : RT_Exception_Code) return Node_Id
472 begin
473 pragma Assert (Reason in RT_SE_Exceptions);
474 return
475 Make_Raise_Storage_Error (Sloc,
476 Condition => Condition,
477 Reason =>
478 UI_From_Int (RT_Exception_Code'Pos (Reason)));
479 end Make_Raise_Storage_Error;
481 -------------
482 -- Make_SC --
483 -------------
485 function Make_SC (Pre, Sel : Node_Id) return Node_Id is
486 begin
487 return
488 Make_Selected_Component (System_Location,
489 Prefix => Pre,
490 Selector_Name => Sel);
491 end Make_SC;
493 -------------------------
494 -- Make_String_Literal --
495 -------------------------
497 function Make_String_Literal
498 (Sloc : Source_Ptr;
499 Strval : String) return Node_Id
501 begin
502 Start_String;
503 Store_String_Chars (Strval);
504 return
505 Make_String_Literal (Sloc,
506 Strval => End_String);
507 end Make_String_Literal;
509 --------------------
510 -- Make_Temporary --
511 --------------------
513 function Make_Temporary
514 (Loc : Source_Ptr;
515 Id : Character;
516 Related_Node : Node_Id := Empty) return Entity_Id
518 Temp : constant Entity_Id :=
519 Make_Defining_Identifier (Loc,
520 Chars => New_Internal_Name (Id));
521 begin
522 Set_Related_Expression (Temp, Related_Node);
523 return Temp;
524 end Make_Temporary;
526 ---------------------------
527 -- Make_Unsuppress_Block --
528 ---------------------------
530 -- Generates the following expansion:
532 -- declare
533 -- pragma Suppress (<check>);
534 -- begin
535 -- <stmts>
536 -- end;
538 function Make_Unsuppress_Block
539 (Loc : Source_Ptr;
540 Check : Name_Id;
541 Stmts : List_Id) return Node_Id
543 begin
544 return
545 Make_Block_Statement (Loc,
546 Declarations => New_List (
547 Make_Pragma (Loc,
548 Chars => Name_Suppress,
549 Pragma_Argument_Associations => New_List (
550 Make_Pragma_Argument_Association (Loc,
551 Expression => Make_Identifier (Loc, Check))))),
553 Handled_Statement_Sequence =>
554 Make_Handled_Sequence_Of_Statements (Loc,
555 Statements => Stmts));
556 end Make_Unsuppress_Block;
558 --------------------------
559 -- New_Constraint_Error --
560 --------------------------
562 function New_Constraint_Error (Loc : Source_Ptr) return Node_Id is
563 Ident_Node : Node_Id;
564 Raise_Node : Node_Id;
566 begin
567 Ident_Node := New_Node (N_Identifier, Loc);
568 Set_Chars (Ident_Node, Chars (Standard_Entity (S_Constraint_Error)));
569 Set_Entity (Ident_Node, Standard_Entity (S_Constraint_Error));
570 Raise_Node := New_Node (N_Raise_Statement, Loc);
571 Set_Name (Raise_Node, Ident_Node);
572 return Raise_Node;
573 end New_Constraint_Error;
575 -----------------------
576 -- New_External_Name --
577 -----------------------
579 function New_External_Name
580 (Related_Id : Name_Id;
581 Suffix : Character := ' ';
582 Suffix_Index : Int := 0;
583 Prefix : Character := ' ') return Name_Id
585 begin
586 Get_Name_String (Related_Id);
588 if Prefix /= ' ' then
589 pragma Assert (Is_OK_Internal_Letter (Prefix) or else Prefix = '_');
591 for J in reverse 1 .. Name_Len loop
592 Name_Buffer (J + 1) := Name_Buffer (J);
593 end loop;
595 Name_Len := Name_Len + 1;
596 Name_Buffer (1) := Prefix;
597 end if;
599 if Suffix /= ' ' then
600 pragma Assert (Is_OK_Internal_Letter (Suffix));
601 Add_Char_To_Name_Buffer (Suffix);
602 end if;
604 if Suffix_Index /= 0 then
605 if Suffix_Index < 0 then
606 Add_Unique_Serial_Number;
607 else
608 Add_Nat_To_Name_Buffer (Suffix_Index);
609 end if;
610 end if;
612 return Name_Find;
613 end New_External_Name;
615 function New_External_Name
616 (Related_Id : Name_Id;
617 Suffix : String;
618 Suffix_Index : Int := 0;
619 Prefix : Character := ' ') return Name_Id
621 begin
622 Get_Name_String (Related_Id);
624 if Prefix /= ' ' then
625 pragma Assert (Is_OK_Internal_Letter (Prefix));
627 for J in reverse 1 .. Name_Len loop
628 Name_Buffer (J + 1) := Name_Buffer (J);
629 end loop;
631 Name_Len := Name_Len + 1;
632 Name_Buffer (1) := Prefix;
633 end if;
635 if Suffix /= "" then
636 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
637 Name_Len := Name_Len + Suffix'Length;
638 end if;
640 if Suffix_Index /= 0 then
641 if Suffix_Index < 0 then
642 Add_Unique_Serial_Number;
643 else
644 Add_Nat_To_Name_Buffer (Suffix_Index);
645 end if;
646 end if;
648 return Name_Find;
649 end New_External_Name;
651 function New_External_Name
652 (Suffix : Character;
653 Suffix_Index : Nat) return Name_Id
655 begin
656 Name_Buffer (1) := Suffix;
657 Name_Len := 1;
658 Add_Nat_To_Name_Buffer (Suffix_Index);
659 return Name_Find;
660 end New_External_Name;
662 -----------------------
663 -- New_Internal_Name --
664 -----------------------
666 function New_Internal_Name (Id_Char : Character) return Name_Id is
667 begin
668 pragma Assert (Is_OK_Internal_Letter (Id_Char));
669 Name_Buffer (1) := Id_Char;
670 Name_Len := 1;
671 Add_Unique_Serial_Number;
672 return Name_Enter;
673 end New_Internal_Name;
675 -----------------------
676 -- New_Occurrence_Of --
677 -----------------------
679 function New_Occurrence_Of
680 (Def_Id : Entity_Id;
681 Loc : Source_Ptr) return Node_Id
683 pragma Assert (Present (Def_Id) and then Nkind (Def_Id) in N_Entity);
684 Occurrence : Node_Id;
686 begin
687 Occurrence := New_Node (N_Identifier, Loc);
688 Set_Chars (Occurrence, Chars (Def_Id));
689 Set_Entity (Occurrence, Def_Id);
691 if Is_Type (Def_Id) then
692 Set_Etype (Occurrence, Def_Id);
693 else
694 Set_Etype (Occurrence, Etype (Def_Id));
695 end if;
697 return Occurrence;
698 end New_Occurrence_Of;
700 -----------------
701 -- New_Op_Node --
702 -----------------
704 function New_Op_Node
705 (New_Node_Kind : Node_Kind;
706 New_Sloc : Source_Ptr) return Node_Id
708 type Name_Of_Type is array (N_Op) of Name_Id;
709 Name_Of : constant Name_Of_Type := Name_Of_Type'(
710 N_Op_And => Name_Op_And,
711 N_Op_Or => Name_Op_Or,
712 N_Op_Xor => Name_Op_Xor,
713 N_Op_Eq => Name_Op_Eq,
714 N_Op_Ne => Name_Op_Ne,
715 N_Op_Lt => Name_Op_Lt,
716 N_Op_Le => Name_Op_Le,
717 N_Op_Gt => Name_Op_Gt,
718 N_Op_Ge => Name_Op_Ge,
719 N_Op_Add => Name_Op_Add,
720 N_Op_Subtract => Name_Op_Subtract,
721 N_Op_Concat => Name_Op_Concat,
722 N_Op_Multiply => Name_Op_Multiply,
723 N_Op_Divide => Name_Op_Divide,
724 N_Op_Mod => Name_Op_Mod,
725 N_Op_Rem => Name_Op_Rem,
726 N_Op_Expon => Name_Op_Expon,
727 N_Op_Plus => Name_Op_Add,
728 N_Op_Minus => Name_Op_Subtract,
729 N_Op_Abs => Name_Op_Abs,
730 N_Op_Not => Name_Op_Not,
732 -- We don't really need these shift operators, since they never
733 -- appear as operators in the source, but the path of least
734 -- resistance is to put them in (the aggregate must be complete).
736 N_Op_Rotate_Left => Name_Rotate_Left,
737 N_Op_Rotate_Right => Name_Rotate_Right,
738 N_Op_Shift_Left => Name_Shift_Left,
739 N_Op_Shift_Right => Name_Shift_Right,
740 N_Op_Shift_Right_Arithmetic => Name_Shift_Right_Arithmetic);
742 Nod : constant Node_Id := New_Node (New_Node_Kind, New_Sloc);
744 begin
745 if New_Node_Kind in Name_Of'Range then
746 Set_Chars (Nod, Name_Of (New_Node_Kind));
747 end if;
749 return Nod;
750 end New_Op_Node;
752 -----------------------
753 -- New_Suffixed_Name --
754 -----------------------
756 function New_Suffixed_Name
757 (Related_Id : Name_Id;
758 Suffix : String) return Name_Id
760 begin
761 Get_Name_String (Related_Id);
762 Add_Char_To_Name_Buffer ('_');
763 Add_Str_To_Name_Buffer (Suffix);
764 return Name_Find;
765 end New_Suffixed_Name;
767 -------------------
768 -- OK_Convert_To --
769 -------------------
771 function OK_Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
772 Result : Node_Id;
773 begin
774 Result :=
775 Make_Type_Conversion (Sloc (Expr),
776 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
777 Expression => Relocate_Node (Expr));
778 Set_Conversion_OK (Result, True);
779 Set_Etype (Result, Typ);
780 return Result;
781 end OK_Convert_To;
783 -------------
784 -- Set_RND --
785 -------------
787 procedure Set_RND (Unit : Node_Id) is
788 begin
789 Set_Restriction_No_Dependence (Unit, Warn => False);
790 end Set_RND;
792 --------------------------
793 -- Unchecked_Convert_To --
794 --------------------------
796 function Unchecked_Convert_To
797 (Typ : Entity_Id;
798 Expr : Node_Id) return Node_Id
800 Loc : constant Source_Ptr := Sloc (Expr);
801 Result : Node_Id;
802 Expr_Parent : Node_Id;
804 begin
805 -- If the expression is already of the correct type, then nothing
806 -- to do, except for relocating the node in case this is required.
808 if Present (Etype (Expr))
809 and then (Base_Type (Etype (Expr)) = Typ
810 or else Etype (Expr) = Typ)
811 then
812 return Relocate_Node (Expr);
814 -- Cases where the inner expression is itself an unchecked conversion
815 -- to the same type, and we can thus eliminate the outer conversion.
817 elsif Nkind (Expr) = N_Unchecked_Type_Conversion
818 and then Entity (Subtype_Mark (Expr)) = Typ
819 then
820 Result := Relocate_Node (Expr);
822 elsif Nkind (Expr) = N_Null
823 and then Is_Access_Type (Typ)
824 then
825 -- No need for a conversion
827 Result := Relocate_Node (Expr);
829 -- All other cases
831 else
832 -- Capture the parent of the expression before relocating it and
833 -- creating the conversion, so the conversion's parent can be set
834 -- to the original parent below.
836 Expr_Parent := Parent (Expr);
838 Result :=
839 Make_Unchecked_Type_Conversion (Loc,
840 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
841 Expression => Relocate_Node (Expr));
843 Set_Parent (Result, Expr_Parent);
844 end if;
846 Set_Etype (Result, Typ);
847 return Result;
848 end Unchecked_Convert_To;
850 end Tbuild;