1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 with Atree
; use Atree
;
29 with Einfo
; use Einfo
;
30 with Errout
; use Errout
;
32 with Lib
.Xref
; use Lib
.Xref
;
33 with Nlists
; use Nlists
;
34 with Nmake
; use Nmake
;
36 with Restrict
; use Restrict
;
37 with Rtsfind
; use Rtsfind
;
39 with Sem_Ch5
; use Sem_Ch5
;
40 with Sem_Ch8
; use Sem_Ch8
;
41 with Sem_Res
; use Sem_Res
;
42 with Sem_Util
; use Sem_Util
;
43 with Sinfo
; use Sinfo
;
44 with Stand
; use Stand
;
45 with Uintp
; use Uintp
;
47 package body Sem_Ch11
is
49 -----------------------------------
50 -- Analyze_Exception_Declaration --
51 -----------------------------------
53 procedure Analyze_Exception_Declaration
(N
: Node_Id
) is
54 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
55 PF
: constant Boolean := Is_Pure
(Current_Scope
);
58 Generate_Definition
(Id
);
60 Set_Ekind
(Id
, E_Exception
);
61 Set_Exception_Code
(Id
, Uint_0
);
62 Set_Etype
(Id
, Standard_Exception_Type
);
64 Set_Is_Statically_Allocated
(Id
);
67 end Analyze_Exception_Declaration
;
69 --------------------------------
70 -- Analyze_Exception_Handlers --
71 --------------------------------
73 procedure Analyze_Exception_Handlers
(L
: List_Id
) is
77 H_Scope
: Entity_Id
:= Empty
;
79 procedure Check_Duplication
(Id
: Node_Id
);
80 -- Iterate through the identifiers in each handler to find duplicates
82 -----------------------
83 -- Check_Duplication --
84 -----------------------
86 procedure Check_Duplication
(Id
: Node_Id
) is
91 Handler
:= First_Non_Pragma
(L
);
92 while Present
(Handler
) loop
93 Id1
:= First
(Exception_Choices
(Handler
));
95 while Present
(Id1
) loop
97 -- Only check against the exception choices which precede
98 -- Id in the handler, since the ones that follow Id have not
99 -- been analyzed yet and will be checked in a subsequent call.
104 elsif Nkind
(Id1
) /= N_Others_Choice
105 and then Entity
(Id
) = Entity
(Id1
)
107 if Handler
/= Parent
(Id
) then
108 Error_Msg_Sloc
:= Sloc
(Id1
);
110 ("exception choice duplicates &#", Id
, Id1
);
113 if Ada_83
and then Comes_From_Source
(Id
) then
115 ("(Ada 83): duplicate exception choice&", Id
);
120 Next_Non_Pragma
(Id1
);
125 end Check_Duplication
;
127 -- Start processing for Analyze_Exception_Handlers
130 Handler
:= First
(L
);
131 Check_Restriction
(No_Exceptions
, Handler
);
132 Check_Restriction
(No_Exception_Handlers
, Handler
);
134 -- Loop through handlers (which can include pragmas)
136 while Present
(Handler
) loop
138 -- If pragma just analyze it
140 if Nkind
(Handler
) = N_Pragma
then
143 -- Otherwise we have a real exception handler
146 -- Deal with choice parameter. The exception handler is
147 -- a declarative part for it, so it constitutes a scope
148 -- for visibility purposes. We create an entity to denote
149 -- the whole exception part, and use it as the scope of all
150 -- the choices, which may even have the same name without
151 -- conflict. This scope plays no other role in expansion or
152 -- or code generation.
154 Choice
:= Choice_Parameter
(Handler
);
156 if Present
(Choice
) then
159 H_Scope
:= New_Internal_Entity
160 (E_Block
, Current_Scope
, Sloc
(Choice
), 'E');
164 Set_Etype
(H_Scope
, Standard_Void_Type
);
166 -- Set the Finalization Chain entity to Error means that it
167 -- should not be used at that level but the parent one
168 -- should be used instead.
170 -- ??? this usage needs documenting in Einfo/Exp_Ch7 ???
171 -- ??? using Error for this non-error condition is nasty ???
173 Set_Finalization_Chain_Entity
(H_Scope
, Error
);
176 Set_Ekind
(Choice
, E_Variable
);
177 Set_Etype
(Choice
, RTE
(RE_Exception_Occurrence
));
178 Generate_Definition
(Choice
);
181 Id
:= First
(Exception_Choices
(Handler
));
182 while Present
(Id
) loop
183 if Nkind
(Id
) = N_Others_Choice
then
184 if Present
(Next
(Id
))
185 or else Present
(Next
(Handler
))
186 or else Present
(Prev
(Id
))
188 Error_Msg_N
("OTHERS must appear alone and last", Id
);
194 if not Is_Entity_Name
(Id
)
195 or else Ekind
(Entity
(Id
)) /= E_Exception
197 Error_Msg_N
("exception name expected", Id
);
200 if Present
(Renamed_Entity
(Entity
(Id
))) then
201 Set_Entity
(Id
, Renamed_Entity
(Entity
(Id
)));
204 Check_Duplication
(Id
);
206 -- Check for exception declared within generic formal
207 -- package (which is illegal, see RM 11.2(8))
210 Ent
: Entity_Id
:= Entity
(Id
);
211 Scop
: Entity_Id
:= Scope
(Ent
);
214 while Scop
/= Standard_Standard
215 and then Ekind
(Scop
) = E_Package
217 -- If the exception is declared in an inner
218 -- instance, nothing else to check.
220 if Is_Generic_Instance
(Scop
) then
223 elsif Nkind
(Declaration_Node
(Scop
)) =
224 N_Package_Specification
226 Nkind
(Original_Node
(Parent
227 (Declaration_Node
(Scop
)))) =
228 N_Formal_Package_Declaration
231 ("exception& is declared in " &
232 "generic formal package", Id
, Ent
);
234 ("\and therefore cannot appear in " &
235 "handler ('R'M 11.2(8))", Id
);
239 Scop
:= Scope
(Scop
);
248 Analyze_Statements
(Statements
(Handler
));
250 if Present
(Choice
) then
258 end Analyze_Exception_Handlers
;
260 --------------------------------
261 -- Analyze_Handled_Statements --
262 --------------------------------
264 procedure Analyze_Handled_Statements
(N
: Node_Id
) is
265 Handlers
: constant List_Id
:= Exception_Handlers
(N
);
268 Analyze_Statements
(Statements
(N
));
270 if Present
(Handlers
) then
271 Analyze_Exception_Handlers
(Handlers
);
273 elsif Present
(At_End_Proc
(N
)) then
274 Analyze
(At_End_Proc
(N
));
276 end Analyze_Handled_Statements
;
278 -----------------------------
279 -- Analyze_Raise_Statement --
280 -----------------------------
282 procedure Analyze_Raise_Statement
(N
: Node_Id
) is
283 Exception_Id
: constant Node_Id
:= Name
(N
);
284 Exception_Name
: Entity_Id
:= Empty
;
289 Check_Unreachable_Code
(N
);
291 -- Check exception restrictions on the original source
293 if Comes_From_Source
(N
) then
294 Check_Restriction
(No_Exceptions
, N
);
299 if No
(Exception_Id
) then
302 Nkind_P
:= Nkind
(P
);
304 while Nkind_P
/= N_Exception_Handler
305 and then Nkind_P
/= N_Subprogram_Body
306 and then Nkind_P
/= N_Package_Body
307 and then Nkind_P
/= N_Task_Body
308 and then Nkind_P
/= N_Entry_Body
311 Nkind_P
:= Nkind
(P
);
314 if Nkind
(P
) /= N_Exception_Handler
then
316 ("reraise statement must appear directly in a handler", N
);
319 -- Normal case with exception id present
322 Analyze
(Exception_Id
);
324 if Is_Entity_Name
(Exception_Id
) then
325 Exception_Name
:= Entity
(Exception_Id
);
328 if No
(Exception_Name
)
329 or else Ekind
(Exception_Name
) /= E_Exception
332 ("exception name expected in raise statement", Exception_Id
);
335 end Analyze_Raise_Statement
;
337 -----------------------------
338 -- Analyze_Raise_xxx_Error --
339 -----------------------------
341 -- Normally, the Etype is already set (when this node is used within
342 -- an expression, since it is copied from the node which it rewrites).
343 -- If this node is used in a statement context, then we set the type
344 -- Standard_Void_Type. This is used both by Gigi and by the front end
345 -- to distinguish the statement use and the subexpression use.
347 -- The only other required processing is to take care of the Condition
348 -- field if one is present.
350 procedure Analyze_Raise_xxx_Error
(N
: Node_Id
) is
352 if No
(Etype
(N
)) then
353 Set_Etype
(N
, Standard_Void_Type
);
356 if Present
(Condition
(N
)) then
357 Analyze_And_Resolve
(Condition
(N
), Standard_Boolean
);
360 -- Deal with static cases in obvious manner
362 if Nkind
(Condition
(N
)) = N_Identifier
then
363 if Entity
(Condition
(N
)) = Standard_True
then
364 Set_Condition
(N
, Empty
);
366 elsif Entity
(Condition
(N
)) = Standard_False
then
367 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
371 end Analyze_Raise_xxx_Error
;
373 -----------------------------
374 -- Analyze_Subprogram_Info --
375 -----------------------------
377 procedure Analyze_Subprogram_Info
(N
: Node_Id
) is
379 Set_Etype
(N
, RTE
(RE_Code_Loc
));
380 end Analyze_Subprogram_Info
;