Debian Codegraph @

source_2012/11.mss

raw
1 @Part(11, Root="ada.mss")
2
3 @Comment{$Date: 2015/04/03 04:12:42 $}
4 @LabeledSection{Exceptions}
5
6 @Comment{$Source: e:\\cvsroot/ARM/Source/11.mss,v $}
7 @Comment{$Revision: 1.93 $}
8
9 @begin{Intro}
10 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
11 @redundant[This @Chg{Version=[3],New=[clause],Old=[section]} defines
12 the facilities for dealing with errors or other
13 exceptional situations that arise during program execution.]
14 @Defn{exception occurrence}
15 @IndexSeeAlso{Term=[condition],See=(exception)}
16 @IndexSee{Term=[signal (an exception)],See=(raise)}
17 @IndexSee{Term=[throw (an exception)],See=(raise)}
18 @IndexSee{Term=[catch (an exception)],See=(handle)}
19 @ToGlossaryAlso{Term=<Exception>,
20 Text=<An @i(exception) represents a kind of exceptional situation;
21 an occurrence of such a situation (at run time)
22 is called an @i(exception occurrence).
23 @redundant[@PDefn2{Term=[raise], Sec=(an exception)}
24 To @i{raise} an exception is to abandon normal program execution so
25 as to draw attention to the fact that the corresponding situation has
26 arisen.
27 @PDefn2{Term=[handle], Sec=(an exception)}
28 Performing some actions in response to the arising of an exception
29 is called @i{handling} the exception.
30 ]>}
31 @begin{Honest}
32 @PDefn2{Term=[handle], Sec=(an exception occurrence)}
33 ...or handling the exception occurrence.
34 @end{Honest}
35 @begin{Ramification}
36 For example, an exception End_Error might
37 represent error situations in which an attempt is made to read beyond
38 end-of-file. During the execution of a partition, there might be
39 numerous occurrences of this exception.
40 @end{Ramification}
41 @begin{Honest}
42 @Defn{occurrence (of an exception)}
43 When the meaning is clear from the context,
44 we sometimes use @lquotes@;@i{occurrence}@rquotes@; as a
45 short-hand for @lquotes@;exception occurrence.@rquotes@;
46 @end{Honest}
47
48 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0043-1],ARef=[AI05-0258-1]}
49 @redundant[An @nt{exception_declaration} declares a name for an exception.
50 An exception @Chg{Version=[3],New=[can be],Old=[is]} raised
51 @Chg{Version=[3],New=[explicitly (for example,],Old=[initially either]}
52 by a @nt{raise_statement}@Chg{Version=[3],New=[)],Old=[]}
53 or@Chg{Version=[3],New=[ implicitly (for example,],Old=[]}
54 by the failure of a language-defined check@Chg{Version=[3],New=[)],Old=[]}.
55 When an exception arises, control can be transferred to a
56 user-provided @nt{exception_handler} at the end of a
57 @nt{handled_@!sequence_of_@!statements},
58 or it can be propagated to a dynamically enclosing execution.]
59 @end{Intro}
60
61 @begin{DiffWord83}
62 We are more explicit about the difference between an exception and an
63 occurrence of an exception.
64 This is necessary because we now have a type (Exception_Occurrence)
65 that represents exception occurrences,
66 so the program can manipulate them.
67 Furthermore, we say that when an exception is propagated,
68 it is the same occurrence that is being propagated
69 (as opposed to a new occurrence of the same exception).
70 The same issue applies to a re-raise statement.
71 In order to understand these semantics,
72 we have to make this distinction.
73 @end{DiffWord83}
74
75 @begin{DiffWord2005}
76 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0043-1]}
77 @ChgAdded{Version=[3],Text=[@b<Correction:> Generalized the introductory
78 description of how an exception can be raised so that it does not appear
79 to cover all of the cases.]}
80 @end{DiffWord2005}
81
82
83 @LabeledClause{Exception Declarations}
84
85 @begin{Intro}
86 @Defn{exception}
87 An @nt{exception_declaration} declares a name for an
88 exception.
89 @end{Intro}
90
91 @begin{Syntax}
92 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
93 @Syn{lhs=<exception_declaration>,rhs="@Syn2{defining_identifier_list} : @key{exception}@Chg{Version=[3],New=<
94 [@Syn2{aspect_specification}]>,Old=[]};"}
95 @end{Syntax}
96
97 @begin{StaticSem}
98 Each single @nt{exception_declaration} declares a name for a different
99 exception. If a generic unit includes an @nt{exception_declaration},
100 the @nt{exception_declaration}s implicitly generated by
101 different instantiations of the generic unit refer to distinct
102 exceptions (but all have the same @nt{defining_identifier}).
103 The particular exception denoted by an exception name is determined at
104 compilation time and is the same regardless of how many times the
105 @nt{exception_declaration} is elaborated.
106 @begin{Reason}
107 We considered removing this requirement
108 inside generic bodies,
109 because it is an implementation burden for implementations
110 that wish to share code among several instances.
111 In the end, it was decided that it
112 would introduce too much implementation dependence.
113 @end{Reason}
114 @begin{Ramification}
115 Hence, if an @nt{exception_declaration} occurs in a recursive
116 subprogram, the exception name denotes the same exception for all
117 invocations of the recursive subprogram.
118 The reason for this rule is that we allow an exception occurrence to
119 propagate out of its declaration's innermost containing master;
120 if exceptions were created by their declarations like other entities,
121 they would presumably be destroyed upon leaving the master;
122 we would have to do something special to prevent them from
123 propagating to places where they no longer exist.
124 @end{Ramification}
125 @begin{Ramification}
126 Exception identities are unique across all partitions of a
127 program.
128 @end{Ramification}
129
130 @Defn{predefined exception}
131 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
132 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
133 @Defn2{Term=[Storage_Error],Sec=(raised by failure of run-time check)}
134 @Defn2{Term=[Tasking_Error],Sec=(raised by failure of run-time check)}
135 The @i{predefined} exceptions are the ones declared in the
136 declaration of package Standard:
137 Constraint_Error, Program_Error,
138 Storage_Error, and Tasking_Error@Redundant[;
139 one of them is raised when a language-defined check fails.]
140 @begin{Ramification}
141 The exceptions declared in the
142 language-defined package IO_Exceptions, for example,
143 are not predefined.
144 @end{Ramification}
145 @end{StaticSem}
146
147 @begin{RunTime}
148 @PDefn2{Term=[elaboration], Sec=(exception_declaration)}
149 The elaboration of an @nt{exception_declaration} has no effect.
150
151 @IndexCheck{Storage_Check}
152 @Defn2{Term=[Storage_Error],Sec=(raised by failure of run-time check)}
153 The execution of any construct raises Storage_Error if there is
154 insufficient storage for that execution.
155 @PDefn{unspecified}
156 The amount of storage needed for the execution of constructs is
157 unspecified.
158 @begin{Ramification}
159 Note that any execution whatsoever can raise Storage_Error.
160 This allows much implementation freedom in storage management.
161 @end{Ramification}
162 @end{RunTime}
163
164 @begin{Examples}
165 @leading@keepnext@i{Examples of user-defined exception declarations:}
166 @begin{Example}
167 Singular : @key[exception];
168 Error : @key[exception];
169 Overflow, Underflow : @key[exception];
170 @end{Example}
171 @end{Examples}
172
173 @begin{Inconsistent83}
174 @Defn{inconsistencies with Ada 83}
175 The exception Numeric_Error is now defined in the Obsolescent
176 features Annex, as a rename of Constraint_Error.
177 All checks that raise Numeric_Error in Ada 83
178 instead raise Constraint_Error in Ada 95.
179 To increase upward compatibility,
180 we also changed the rules to allow the same exception to be named
181 more than once by a given handler.
182 Thus,
183 @lquotes@;@key[when] Constraint_Error | Numeric_Error =>@rquotes@; will remain
184 legal in Ada 95,
185 even though Constraint_Error and Numeric_Error now denote the same
186 exception. However, it will not be legal to have
187 separate handlers for Constraint_Error and Numeric_Error.
188 This change is inconsistent in the rare case that an
189 existing program explicitly raises Numeric_Error at a point where
190 there is a handler for Constraint_Error;
191 the exception will now be caught by that handler.
192 @end{Inconsistent83}
193
194 @begin{DiffWord83}
195 We explicitly define elaboration for @nt{exception_declaration}s.
196 @end{DiffWord83}
197
198 @begin{Extend2005}
199 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
200 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
201 An optional @nt{aspect_specification} can be used in a @nt{exception_declaration}.
202 This is described in @RefSecNum{Aspect Specifications}.]}
203 @end{Extend2005}
204
205
206 @RMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
207 @LabeledClause{Exception Handlers}
208
209 @begin{Intro}
210 @redundant[The response to one or more exceptions is specified by an
211 @nt{exception_handler}.]
212 @end{Intro}
213
214 @begin{Syntax}
215 @Syn{lhs=<handled_sequence_of_statements>,rhs="
216 @Syn2{sequence_of_statements}
217 [@key{exception}
218 @Syn2{exception_handler}
219 {@Syn2{exception_handler}}]"}
220
221
222 @Syn{lhs=<exception_handler>,rhs="
223 @key{when} [@Syn2{choice_parameter_specification}:] @Syn2{exception_choice} {| @Syn2{exception_choice}} =>
224 @Syn2{sequence_of_statements}"}
225
226 @Syn{lhs=<choice_parameter_specification>,rhs="@Syn2{defining_identifier}"}
227
228 @Syn{lhs=<exception_choice>,rhs="@SynI{exception_}@Syn2{name} | @key{others}"}
229 @begin{Honest}
230 @Defn{handler}
231 @lquotes@;@i{Handler}@rquotes@; is an abbreviation for @lquotes@;@nt{exception_handler}.@rquotes@;
232
233 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
234 @Defn2{term=<choice>, Sec=<of an @nt{exception_handler}>}
235 Within this @Chg{Version=[3],New=[clause],Old=[section]}, we sometimes
236 abbreviate
237 @lquotes@;@nt{exception_choice}@rquotes@; to @lquotes@;@i{choice}.@rquotes@;
238 @end{Honest}
239 @end{Syntax}
240
241 @begin{Legality}
242 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0022-1]}
243 @ChgAdded{Version=[4],Text=[An @SynI{exception_}@nt{name} of an
244 @nt{exception_choice} shall denote an exception.]}
245
246 @Defn2{term=<cover>, Sec=<of a choice and an exception>}
247 A choice with an
248 @SynI{exception_}@nt{name} @i{covers} the named exception.
249 A choice with @key{others} covers all exceptions
250 not named by previous choices of the same
251 @nt{handled_@!sequence_of_@!statements}.
252 Two choices in different @nt<exception_handler>s of the same
253 @nt{handled_@!sequence_of_@!statements} shall not cover the same
254 exception.
255 @begin{Ramification}
256 Two @nt<exception_choice>s of the same @nt<exception_handler> may cover
257 the same exception. For example, given two renaming declarations in
258 separate packages for the same exception, one may nevertheless
259 write, for example,
260 @lquotes@;@key[when] Ada.Text_IO.Data_Error | My_Seq_IO.Data_Error =>@rquotes@;.
261
262 An @key{others} choice even covers exceptions that are not
263 visible at the place of the handler.
264 Since exception raising is a dynamic activity,
265 it is entirely possible for an @key{others} handler to handle an
266 exception that it could not have named.
267 @end{Ramification}
268
269 A choice with @key{others} is allowed only for the
270 last handler of a @nt{handled_sequence_of_statements}
271 and as the only choice of that handler.
272
273 An @SynI{exception_}@nt{name} of a choice shall not denote an
274 exception declared in a generic formal package.
275 @begin{Reason}
276 This is because the compiler doesn't know the identity of such an
277 exception, and thus can't enforce the coverage rules.
278 @end{Reason}
279 @end{Legality}
280
281 @begin{StaticSem}
282 @Defn{choice parameter}
283 A @nt{choice_parameter_specification} declares a
284 @i{choice parameter}, which is a constant object of type
285 Exception_Occurrence (see @RefSecNum{The Package Exceptions}).
286 During the handling of an exception occurrence,
287 the choice parameter, if any, of the handler
288 represents the exception occurrence that is being handled.
289
290 @end{StaticSem}
291
292 @begin{RunTime}
293 @PDefn2{Term=[execution], Sec=(handled_sequence_of_statements)}
294 The execution of a @nt<handled_sequence_of_statements> consists
295 of the execution of the @nt<sequence_of_@!statements>.
296 @Redundant[The optional handlers are used to handle any exceptions
297 that are propagated by the @nt{sequence_of_@!statements}.]
298 @end{RunTime}
299
300 @begin{Examples}
301 @leading@keepnext@i{Example of an exception handler:}
302 @begin{Example}
303 @key[begin]
304 Open(File, In_File, "input.txt"); --@RI[ see @RefSecNum{File Management}]
305 @key[exception]
306 @key[when] E : Name_Error =>
307 Put("Cannot open input file : ");
308 Put_Line(Exception_Message(E)); --@RI[ see @RefSecNum{The Package Exceptions}]
309 @key[raise];
310 @key[end];
311 @end{Example}
312 @end{Examples}
313
314 @begin{Extend83}
315 @Defn{extensions to Ada 83}
316 The syntax rule for @nt{exception_handler} is modified to
317 allow a @nt{choice_parameter_specification}.
318
319 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
320 Different @Chg{Version=[2],New=[@nt{exception_choice}s],Old=[@ntf<choice>s]}
321 of the same @nt<exception_handler> may
322 cover the same exception. This allows for
323 @lquotes@;when Numeric_Error | Constraint_Error =>@rquotes@; even though
324 Numeric_Error is a rename of Constraint_Error.
325 This also allows one to @lquotes@;with@rquotes@; two different I/O packages,
326 and then write, for example,
327 @lquotes@;when Ada.Text_IO.Data_Error | My_Seq_IO.Data_Error =>@rquotes@;
328 even though these might both be renames of the same exception.
329 @end{Extend83}
330
331 @begin{DiffWord83}
332 The syntax rule for @nt{handled_sequence_of_statements} is new.
333 These are now used in all the places where handlers
334 are allowed.
335 This obviates the need to explain
336 (in @Chg{Version=[3],New=[Clauses],Old=[Sections]} 5, 6, 7, and 9)
337 what portions of the program are handled by the handlers.
338 Note that there are more such cases in Ada 95.
339
340 The syntax rule for @nt{choice_parameter_specification} is new.
341 @end{DiffWord83}
342
343 @RMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
344 @LabeledRevisedClause{Version=[4],New=[Raise Statements and Raise Expressions],Old=[Raise Statements]}
345
346 @begin{Intro}
347 @redundant[A @nt{raise_statement} raises an exception.]
348 @end{Intro}
349
350 @begin{Syntax}
351 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00361-01]}
352 @Syn{lhs=<raise_statement>,rhs="@Chg{Version=[2],New=<@key{raise};
353 | @key{raise} @SynI{exception_}@Syn2{name} [@key{with} @SynI{string_}@Syn2{expression}];>,
354 Old=<@key{raise} [@SynI{exception_}@Syn2{name}];>}"}
355
356 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0022-1],ARef=[AI12-0152-1]}
357 @AddedSyn{Version=[4],lhs=<@Chg{Version=[4],New=<raise_expression>,Old=<>}>,rhs="@Chg{Version=[4],New=<@key{raise} @SynI{exception_}@Syn2{name} [@key{with} @SynI{string_}@Syn2{simple_expression}]>,Old=<>}"}
358
359 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0152-1]}
360 @ChgAdded{Version=[4],Type=[Leading],Text=[If a @nt{raise_expression} appears
361 within the @nt{expression} of one of the following
362 contexts, the @nt{raise_expression} shall appear within a pair of parentheses
363 within the @nt{expression}:]}
364 @begin{itemize}
365 @ChgRef{Version=[4],Kind=[Added]}
366 @ChgAdded{Version=[4],Text=[@nt{object_declaration};]}
367
368 @ChgRef{Version=[4],Kind=[Added]}
369 @ChgAdded{Version=[4],Text=[@nt{modular_type_definition};]}
370
371 @ChgRef{Version=[4],Kind=[Added]}
372 @ChgAdded{Version=[4],Text=[@nt{floating_point_definition};]}
373
374 @ChgRef{Version=[4],Kind=[Added]}
375 @ChgAdded{Version=[4],Text=[@nt{ordinary_fixed_point_definition};]}
376
377 @ChgRef{Version=[4],Kind=[Added]}
378 @ChgAdded{Version=[4],Text=[@nt{decimal_fixed_point_definition};]}
379
380 @ChgRef{Version=[4],Kind=[Added]}
381 @ChgAdded{Version=[4],Text=[@nt{default_expression};]}
382
383 @ChgRef{Version=[4],Kind=[Added]}
384 @ChgAdded{Version=[4],Text=[@nt{ancestor_part}.]}
385 @end{itemize}
386
387 @begin{Reason}
388 @ChgRef{Version=[4],Kind=[Added]}
389 @ChgAdded{Version=[4],Text=[Unlike conditional expressions, this doesn't
390 say "immediately surrounded"; the only requirement is that it is somehow
391 within a pair of parentheses that is part of the @nt{expression}. We need
392 this restriction in order that @nt{raise_expression}s cannot be syntactically
393 confused with immediately following constructs (such as
394 @nt{aspect_specification}s).]}
395 @end{Reason}
396
397 @begin{Discussion}
398 @ChgRef{Version=[4],Kind=[Added]}
399 @ChgAdded{Version=[4],Text=[We only need to require that a right parenthesis
400 appear somewhere between the @nt{raise_expression} and the surrounding
401 context; that's all we need to specify in order to eliminate the ambiguities.
402 Moreover, we don't care at all where the left parenthesis is (so long as it
403 is legal, of course).]}
404
405 @ChgRef{Version=[4],Kind=[Added]}
406 @ChgAdded{Version=[4],Type=[Leading],Text=[For instance, the following is
407 illegal by this rule:]}
408 @begin{Example}
409 @ChgRef{Version=[4],Kind=[Added]}
410 @ChgAdded{Version=[4],Text=[Obj : Boolean := Func_Call @key[or else raise] TBD_Error @key[with] Atomic;]}
411 @end{Example}
412 @ChgRef{Version=[4],Kind=[Added]}
413 @ChgAdded{Version=[4],Type=[Leading],Text=[as the "@key[with] Atomic" could
414 be part of the @key[raise_expression] or part of the object declaration.
415 Both of the following are legal:]}
416 @begin{Example}
417 @ChgRef{Version=[4],Kind=[Added]}
418 @ChgAdded{Version=[4],Text=[Obj : Boolean := Func_Call @key[or else] (@key[raise] TBD_Error) @key[with] Atomic;
419 Obj : Boolean := (Func_Call @key[or else raise] TBD_Error) @key[with] Atomic;]}
420 @end{Example}
421 @ChgRef{Version=[4],Kind=[Added]}
422 @ChgAdded{Version=[4],Type=[Leading],Text=[and if the @key[with] belongs to
423 the @nt{raise_expression}, then both of the following are legal:]}
424 @begin{Example}
425 @ChgRef{Version=[4],Kind=[Added]}
426 @ChgAdded{Version=[4],Text=[Obj : Boolean := Func_Call @key[or else] (@key[raise] TBD_Error @key[with] Atomic);
427 Obj : Boolean := (Func_Call @key[or else raise] TBD_Error @key[with] Atomic);]}
428 @end{Example}
429
430 @ChgRef{Version=[4],Kind=[Added]}
431 @ChgAdded{Version=[4],Type=[Leading],Text=[This rule only requires
432 parentheses for @nt{raise_expression}s that are
433 part of the "top-level" of an @nt{expression} in one of the named contexts;
434 the @nt{raise_expression} is either the entire @nt{expression}, or part of a
435 chain of logical operations. In practice, the @nt{raise_expression} will
436 almost always be last in interesting top-level @nt{expression}s; anything
437 that follows it could never be executed, so that should be rare.
438 Other contexts such as conditional
439 expressions, qualified expressions, aggregates, and even function calls,
440 provide the needed parentheses. All of the following are legal, no
441 additional parens are needed:]}
442 @begin{Example}
443 @ChgRef{Version=[4],Kind=[Added]}
444 @ChgAdded{Version=[4],Text=[Pre : Boolean := (@key[if not] Is_Valid(Param) @key[then raise] Not_Valid_Error);
445 A : A_Tagged := (Some_Tagged'(@key[raise] TBD_Error) @key[with] Comp => 'A');
446 B : Some_Array := (1, 2, 3, @key[others] => @key[raise] Not_Valid_Error);
447 C : Natural := Func (Val => @key[raise] TBD_Error);]}
448 @end{Example}
449
450 @ChgRef{Version=[4],Kind=[Added]}
451 @ChgAdded{Version=[4],Type=[Leading],Text=[Parentheses that are part of
452 the context of the @nt{expression} don't count.
453 For instance, the parentheses around the @nt{raise_expression} are required
454 in the following:]}
455 @begin{Example}
456 @ChgRef{Version=[4],Kind=[Added]}
457 @ChgAdded{Version=[4],Text=[D : A_Tagged := ((@key[raise] TBD_Error) @key[with] Comp => 'A');]}
458 @end{Example}
459 @ChgRef{Version=[4],Kind=[Added]}
460 @ChgAdded{Version=[4],Text=[as @nt{ancestor_part} is one of the contexts
461 that triggers the rule.]}
462
463 @ChgRef{Version=[4],Kind=[Added]}
464 @ChgAdded{Version=[4],Text=[This English-language rule could have been
465 implemented instead by adding nonterminals @ntf{initial_expression} and
466 @ntf{initial_relation}, which are the same as @nt{choice_expression} and
467 @nt{choice_relation} except for the inclusion of membership in
468 @ntf{initial_relation}. Then, @ntf{initial_expresion} could be used in
469 place of @nt{expression} in all of the contexts noted. We did not do that
470 because of the large amount of change required, both to the grammar and
471 to language rules that refer to the grammar. A complete grammar is given
472 in @AILink{AI=[AI12-0152-1],Text=[AI12-0152-1]}.]}
473
474 @ChgRef{Version=[4],Kind=[Added]}
475 @ChgAdded{Version=[4],Text=[The use of a @nt{raise_expression} is illegal
476 in each of @nt{modular_type_definition}, @nt{floating_point_definition},
477 @nt{ordinary_fixed_point_definition}, and @nt{decimal_fixed_point_definition}
478 as these uses are required to be static and a @nt{raise_expression} is never
479 static. We include these in this rule so that Ada text has an unambiguous
480 syntax in these cases.]}
481 @end{Discussion}
482
483 @end{Syntax}
484
485 @begin{Legality}
486 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0022-1],ARef=[AI12-0159-1]}
487 The @Chg{Version=[4],New=[@SynI<exception_>@nt{name}],Old=[@nt{name}]}, if
488 any, @Chg{Version=[4],New=[of],Old=[in]} a @nt{raise_statement}
489 @Chg{Version=[4],New=[or @nt{raise_expression} ],Old=[]}shall denote an exception.
490 @Defn{re-raise statement}
491 A @nt{raise_statement} with no @SynI{exception_}@nt{name}
492 (that is, a @i{re-raise statement})
493 shall be within a handler,
494 but not within a body enclosed by that handler.
495 @end{Legality}
496
497 @begin{Resolution}
498 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00361-01]}
499 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0022-1],ARef=[AI12-0152-1]}
500 @ChgAdded{Version=[2],Text=[The
501 @Chg{Version=[4],New=[@SynI{string_}@nt{expression} or
502 @SynI{string_}@nt{simple_expression}],Old=[@nt<expression>]}, if any,
503 @Chg{Version=[4],New=[of],Old=[in]} a
504 @nt<raise_statement>@Chg{Version=[4],New=[ or @nt{raise_expression}],Old=[,]}
505 is expected to be of type String.]}
506
507 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0022-1],ARef=[AI12-0159-1]}
508 @ChgAdded{Version=[4],Text=[The expected type for a @nt{raise_expression}
509 shall be any single type.]}
510 @end{Resolution}
511
512 @begin{RunTime}
513 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00361-01]}
514 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0022-1],ARef=[AI12-0152-1]}
515 @Defn2{Term=[raise], Sec=(an exception)}
516 To @i(raise an exception) is to
517 raise a new occurrence of that exception@Redundant[,
518 as explained in @RefSecNum{Exception Handling}].
519 @PDefn2{Term=[execution], Sec=(raise_statement with an exception_name)}
520 For the execution of a @nt{raise_statement} with an
521 @SynI{exception_}@nt{name}, the named exception is
522 raised.@Chg{Version=[4],New=[ Similarly, for the evaluation of a @nt{raise_expression},
523 the named exception is raised.],Old=[]}
524 @Chg{Version=[2],New=[@redundant{@Chg{Version=[4],New=[In
525 both of these cases, if],Old=[If]} a @SynI<string_>@nt<expression>
526 @Chg{Version=[4],New=[or @SynI<string_>@nt<simple_expression> ],Old=[]}is present,
527 the @Chg{Version=[4],New=[expression],Old=[@nt{expression}]}
528 is evaluated and its value is associated with the exception occurrence.}],Old=[]}
529 @PDefn2{Term=[execution], Sec=(re-raise statement)}
530 For the execution of a re-raise statement,
531 the exception occurrence that caused transfer of control to the
532 innermost enclosing handler is raised @Redundant[again].
533 @begin{TheProof}
534 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00361-01]}
535 @ChgAdded{Version=[2],Text=[The definition of Exceptions.Exception_Message
536 includes a statement that the string is returned (see
537 @RefSecNum{The Package Exceptions}). We describe the use of the string
538 here so that we don't have an unexplained parameter in this subclause.]}
539 @end{TheProof}
540 @begin{ImplNote}
541 For a re-raise statement, the implementation does not create a new
542 Exception_Occurrence, but instead propagates the same
543 Exception_Occurrence value.
544 This allows the original cause of the exception to be determined.
545 @end{ImplNote}
546 @end{RunTime}
547
548 @begin{Notes}
549 @ChgRef{Version=[4],Kind=[Added],Aref=[AI12-0062-1],Aref=[AI12-0152-1],ARef=[AI12-0159-1]}
550 @ChgAdded{Version=[4],Text=[If the evaluation of a
551 @SynI<string_>@nt{expression} or @SynI<string_>@nt{simple_expression} raises
552 an exception, that exception is
553 propagated instead of the one denoted by the @SynI<exception_>@nt{name}
554 of the @nt{raise_statement} or @nt{raise_expression}.]}
555 @end{Notes}
556
557 @begin{Examples}
558 @leading@keepnext@i{Examples of raise statements:}
559 @begin{Example}
560 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00433-01]}
561 @key[raise] Ada.IO_Exceptions.Name_Error; --@RI[ see @RefSecNum{Exceptions In Input-Output}]@Chg{Version=[2],New=[
562 @key[raise] Queue_Error @key[with] "Buffer Full"; --@RI[ see @RefSecNum{Example of Tasking and Synchronization}]],Old=[]}
563
564 @key[raise]; --@RI[ re-raise the current exception]
565 @end{Example}
566 @end{Examples}
567
568 @begin{DiffWord83}
569 The fact that the @nt{name} in a @nt{raise_statement} has to denote
570 an exception is not clear from RM83.
571 Clearly that was the intent,
572 since the italicized part of the syntax rules so indicate,
573 but there was no explicit rule.
574 RM83-1.5(11) doesn't seem to give the italicized parts of the syntax
575 any force.
576 @end{DiffWord83}
577
578 @begin{Extend95}
579 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00361-01]}
580 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}The syntax of a
581 @nt{raise_statement} is extended to include a string message. This is more
582 convenient than calling Exceptions.Exception_Message
583 (@SynI{exception_}@nt{name}'Identity, @SynI{string_}@nt{expression}), and
584 should encourage the use of message strings when raising exceptions.]}
585 @end{Extend95}
586
587 @begin{Extend2012}
588 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0022-1],ARef=[AI12-0152-1],ARef=[AI12-0159-1]}
589 @ChgAdded{Version=[4],Text=[@Defn{extensions to Ada 2012}@b<Corrigendum:>
590 The @nt{raise_expression} is new. This construct is necessary to
591 allow conversion of existing specifications to use preconditions and
592 predicates without changing the exceptions raised. It is considered important
593 enough to be added to Ada 2012 rather than waiting for Ada 202x.]}
594 @end{Extend2012}
595
596
597 @LabeledClause{Exception Handling}
598
599 @begin{Intro}
600 @redundant[When an exception occurrence is raised,
601 normal program execution is abandoned
602 and control is transferred to an applicable @nt{exception_handler},
603 if any.
604 @Defn2{Term=[handle], Sec=(an exception occurrence)}
605 To @i(handle) an exception occurrence is to respond to the
606 exceptional event.
607 @Defn{propagate}
608 To @i(propagate) an exception occurrence is to raise it again in
609 another context; that is,
610 to fail to respond to the exceptional event in the present context.]
611 @begin{Ramification}
612 In other words, if the execution of a given construct raises an exception,
613 but does not handle it,
614 the exception is propagated to an enclosing execution
615 (except in the case of a @nt{task_body}).
616
617 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00023}
618 Propagation involves re-raising the same exception occurrence@Chg{New=[],
619 Old=[(assuming the implementation has not taken advantage of the
620 @ImplPermName of @RefSecNum{Raise Statements and Raise Expressions})]}.
621 For example, calling an entry of an uncallable task raises
622 Tasking_Error; this is not propagation.
623 @end{Ramification}
624 @end{Intro}
625
626 @begin{RunTime}
627 @Defn2{Term=[dynamically enclosing], Sec=(of one execution by another)}
628 @Defn2{Term=[execution], Sec=(dynamically enclosing)}
629 Within a given task, if the
630 execution of construct @i{a} is defined by this International Standard
631 to consist (in part) of the
632 execution of construct @i{b}, then while @i{b} is executing, the
633 execution of @i{a} is said to @i(dynamically enclose) the execution of
634 @i{b}.
635 @Defn{innermost dynamically enclosing}
636 The @i(innermost
637 dynamically enclosing) execution of a given execution is the
638 dynamically enclosing execution that started most recently.
639 @begin{Honest}
640 @Defn2{Term=[included], Sec=(one execution by another)}
641 @Defn2{Term=[execution], Sec=(included by another execution)}
642 If the execution of @i{a} dynamically encloses that of @i{b},
643 then we also say that the execution of @i{b} is
644 @i{included in} the execution of @i{a}.
645 @end{Honest}
646 @begin{Ramification}
647 Examples: The execution of an @nt{if_statement}
648 dynamically encloses the evaluation of the @nt{condition} after the
649 @key{if} (during that evaluation).
650 (Recall that @lquotes@;execution@rquotes@; includes both @lquotes@;elaboration@rquotes@; and
651 @lquotes@;evaluation@rquotes@;, as well as other executions.)
652 The evaluation of a function call dynamically encloses the execution
653 of the @nt{sequence_of_statements} of the function @nt{body}
654 (during that execution). Note that, due to recursion, several
655 simultaneous executions of the same construct can be occurring at once
656 during the execution of a particular task.
657
658 Dynamically enclosing is not defined across task boundaries;
659 a task's execution does not include the execution of any other tasks.
660
661 Dynamically enclosing is only defined for executions that are occurring
662 at a given moment in time; if an @nt{if_statement} is currently
663 executing the @nt{sequence_of_statements} after @key{then}, then
664 the evaluation of the @nt{condition} is no longer dynamically
665 enclosed by the execution of the @nt{if_statement} (or anything else).
666 @end{Ramification}
667
668 @Leading@Defn2{Term=[raise], Sec=(an exception occurrence)}
669 When an exception occurrence is raised by the execution of a given
670 construct, the rest of the execution of that construct is @i{abandoned};
671 that is, any portions of the execution that have not yet taken place
672 are not performed.
673 The construct is first completed, and then left, as explained
674 in @RefSecNum{Completion and Finalization}.
675 Then:
676 @begin{Itemize}
677 If the construct is a @nt<task_body>,
678 the exception does not propagate further;
679 @begin{Ramification}
680 When an exception is raised by the execution of a
681 @nt{task_body}, there is no dynamically enclosing execution,
682 so the exception does not propagate any further.
683 If the exception occurred during
684 the activation of the task, then the activator raises Tasking_Error,
685 as explained in @RefSec{Task Execution - Task Activation},
686 but we don't define that as propagation; it's a special rule.
687 Otherwise (the exception occurred during the execution of the
688 @nt{handled_sequence_of_statements} of the task),
689 the task silently disappears.
690 Thus, abnormal termination of tasks is not always considered to be an
691 error.
692 @end{Ramification}
693
694 If the construct is the @nt{sequence_of_statements} of a
695 @nt{handled_sequence_of_statements} that has a
696 handler with a choice covering the exception,
697 the occurrence is handled by that handler;
698
699 @Defn2{Term=[propagate],
700 Sec=(an exception occurrence by an execution, to a dynamically enclosing execution)}
701 Otherwise, the occurrence is @i{propagated}
702 to the innermost dynamically enclosing execution,
703 which means that the occurrence is raised again
704 in that context.
705 @begin{Honest}
706 @Defn2{Term=[propagate], Sec=(an exception by an execution)}
707 @Defn2{Term=[propagate], Sec=(an exception by a construct)}
708 As shorthands, we refer to the @i{propagation of an exception},
709 and the @i{propagation by a construct},
710 if the execution of the construct propagates an exception occurrence.
711 @end{Honest}
712 @end{Itemize}
713
714 @Defn2{Term=[handle], Sec=(an exception occurrence)}
715 @PDefn2{Term=[execution], Sec=(handler)}
716 @PDefn2{Term=[elaboration], Sec=(choice_parameter_specification)}
717 When an occurrence is @i(handled) by a given handler,
718 the @nt{choice_parameter_specification}, if any, is first elaborated,
719 which creates the choice parameter and initializes it to the occurrence.
720 Then, the @nt{sequence_of_statements} of the
721 handler is executed;
722 this execution replaces the abandoned portion of the execution of
723 the @nt{sequence_of_statements}.
724 @begin{Ramification}
725 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
726 This @lquotes@;replacement@rquotes@; semantics implies that the handler can
727 do pretty much anything the abandoned sequence could do; for example, in a
728 function, the handler can execute a @Chg{Version=[2],New=[return statement],
729 Old=[@nt{return_statement}]} that applies to the function.
730 @end{Ramification}
731 @begin{Ramification}
732 The rules for exceptions raised in library units,
733 main subprograms and partitions follow from the normal rules,
734 plus the semantics of the environment task
735 described in @Chg{Version=[3],New=[Clause],Old=[Section]}
736 @RefSecNum{Program Structure and Compilation Issues} (for example, the
737 environment task of a partition elaborates library units and calls
738 the main subprogram).
739 If an exception is propagated by the main subprogram, it is
740 propagated to the environment task, which then terminates abnormally,
741 causing the partition to terminate abnormally.
742 Although abnormal termination of tasks is not necessarily an error,
743 abnormal termination of a partition due to an exception @i{is} an
744 error.
745 @end{Ramification}
746 @end{RunTime}
747
748 @begin{Notes}
749 Note that exceptions raised in a @nt{declarative_part} of a body
750 are not handled by the handlers of the
751 @nt{handled_@!sequence_of_@!statements} of that body.
752 @end{Notes}
753
754 @LabeledSubClause{The Package Exceptions}
755
756 @begin{StaticSem}
757 @leading@keepnext@;The following language-defined library package exists:
758 @begin{Example}
759 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00362-01],ARef=[AI95-00400-01],ARef=[AI95-00438-01]}
760 @ChildUnit{Parent=[Ada],Child=[Exceptions]}@Chg{Version=[2],New=[@key[with] Ada.Streams;
761 ],Old=[]}@key[package] Ada.Exceptions @key[is]@Chg{Version=[2],New=[
762 @key[pragma] Preelaborate(Exceptions);],Old=[]}
763 @key[type] @AdaTypeDefn{Exception_Id} @key[is] @key[private];@Chg{Version=[2],New=[
764 @key[pragma] Preelaborable_Initialization(Exception_Id);],Old=[]}
765 @AdaObjDefn{Null_Id} : @key[constant] Exception_Id;
766 @key[function] @AdaSubDefn{Exception_Name}(Id : Exception_Id) @key[return] String;@Chg{Version=[2],New=[
767 @key[function] @AdaSubDefn{Wide_Exception_Name}(Id : Exception_Id) @key[return] Wide_String;
768 @key[function] @AdaSubDefn{Wide_Wide_Exception_Name}(Id : Exception_Id)
769 @key[return] Wide_Wide_String;],Old=[]}
770
771 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00362-01]}
772 @key[type] @AdaTypeDefn{Exception_Occurrence} @key[is] @key[limited] @key[private];@Chg{Version=[2],New=[
773 @key[pragma] Preelaborable_Initialization(Exception_Occurrence);],Old=[]}
774 @key[type] @AdaTypeDefn{Exception_Occurrence_Access} @key[is] @key[access] @key[all] Exception_Occurrence;
775 @AdaObjDefn{Null_Occurrence} : @key[constant] Exception_Occurrence;
776
777 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00329-01]}
778 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
779 @key[procedure] @AdaSubDefn{Raise_Exception}(E : @key[in] Exception_Id;
780 Message : @key[in] String := "")@Chg{Version=[3],New=[],Old=[;]}@Chg{Version=[2],New=[
781 @Chg{Version=[3],New=[@key[with]],Old=[@key[pragma]]} No_Return@Chg{Version=[3],New=[],Old=[(Raise_Exception)]};],Old=[]}
782 @key[function] @AdaSubDefn{Exception_Message}(X : Exception_Occurrence) @key[return] String;
783 @key[procedure] @AdaSubDefn{Reraise_Occurrence}(X : @key[in] Exception_Occurrence);
784
785 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00400-01]}
786 @key[function] @AdaSubDefn{Exception_Identity}(X : Exception_Occurrence)
787 @key[return] Exception_Id;
788 @key[function] @AdaSubDefn{Exception_Name}(X : Exception_Occurrence) @key[return] String;
789 --@RI{ Same as Exception_Name(Exception_Identity(X)).}@Chg{Version=[2],New=[
790 @key[function] @AdaSubDefn{Wide_Exception_Name}(X : Exception_Occurrence)
791 @key[return] Wide_String;
792 --@RI{ Same as Wide_Exception_Name(Exception_Identity(X)).}
793 @key[function] @AdaSubDefn{Wide_Wide_Exception_Name}(X : Exception_Occurrence)
794 @key[return] Wide_Wide_String;
795 --@RI{ Same as Wide_Wide_Exception_Name(Exception_Identity(X)).}],Old=[]}
796 @key[function] @AdaSubDefn{Exception_Information}(X : Exception_Occurrence) @key[return] String;
797
798 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00438-01]}
799 @key[procedure] @AdaSubDefn{Save_Occurrence}(Target : @key[out] Exception_Occurrence;
800 Source : @key[in] Exception_Occurrence);
801 @key[function] @AdaSubDefn{Save_Occurrence}(Source : Exception_Occurrence)
802 @key[return] Exception_Occurrence_Access;@Chg{Version=[2],New=[],Old=[
803 @key[private]
804 ... --@RI{ not specified by the language}
805 @key[end] Ada.Exceptions;]}
806
807 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00438-01]}
808 @ChgAdded{Version=[2],Text=[ @key[procedure] Read_Exception_Occurrence
809 (Stream : @key[not null access] Ada.Streams.Root_Stream_Type'Class;
810 Item : @key[out] Exception_Occurrence);
811 @key[procedure] Write_Exception_Occurrence
812 (Stream : @key[not null access] Ada.Streams.Root_Stream_Type'Class;
813 Item : @key[in] Exception_Occurrence);]}
814
815 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00438-01]}
816 @ChgAdded{Version=[2],Text=[ @key[for] Exception_Occurrence'Read @key[use] Read_Exception_Occurrence;
817 @key[for] Exception_Occurrence'Write @key[use] Write_Exception_Occurrence;]}
818
819 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00438-01]}
820 @ChgAdded{Version=[2],Text=[@key[private]
821 ... --@RI{ not specified by the language}
822 @key[end] Ada.Exceptions;]}
823 @end{Example}
824
825 Each distinct exception is represented by a distinct value
826 of type Exception_Id.
827 Null_Id does not represent any exception,
828 and is the default initial value of type Exception_Id.
829 Each occurrence of an exception is represented by a
830 value of type Exception_Occurrence.
831 Null_Occurrence does not represent any exception occurrence,
832 and is the default initial value of type Exception_Occurrence.
833
834 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{To be consistent with 8652/0006}
835 @Leading@;For @ChgPrefixType{Version=[1],Kind=[Revised],Text=[a
836 @Chg{New=[@nt{prefix}],Old=[prefix]} E that denotes an exception]},
837 the following attribute is defined:
838 @begin{Description}
839 @Attribute{Prefix=<E>, AttrName=<Identity>,
840 Text=[E'Identity returns the unique identity of the exception.
841 The type of this attribute is Exception_Id.]}
842 @begin{Ramification}
843 In a distributed program,
844 the identity is unique across an entire program,
845 not just across a single partition.
846 Exception propagation works properly across RPC's.
847 An exception can be propagated from one partition to another,
848 and then back to the first, where its identity is known.
849 @end{Ramification}
850 @end{Description}
851
852 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00361-01]}
853 Raise_Exception raises a new occurrence of the identified
854 exception.@Chg{Version=[2],New=[],Old=[ In this case
855 Exception_Message returns the Message parameter of Raise_Exception.
856 For a @nt{raise_statement} with an @i{exception_}@nt{name},
857 Exception_Message returns implementation-defined information
858 about the exception occurrence.
859 Reraise_Occurrence reraises the specified exception occurrence.]}
860
861 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00361-01],ARef=[AI95-00378-01]}
862 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0043-1],ARef=[AI05-0248-1]}
863 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0022-1],ARef=[AI12-0152-1]}
864 @ChgAdded{Version=[2],Text=[Exception_Message returns the message associated
865 with the given Exception_Occurrence. For an occurrence raised by a call to
866 Raise_Exception, the message is the Message parameter passed to Raise_Exception.
867 For the occurrence raised by a @nt{raise_statement}
868 @Chg{Version=[4],New=[or @nt{raise_expression} ],Old=[]}with an
869 @SynI{exception_}@nt{name} and a @SynI{string_}@nt{expression}@Chg{Version=[4],New=[
870 or @SynI{string_}@nt{simple_expression}],Old=[]}, the message is
871 the @Syni{string_}@nt{expression}@Chg{Version=[4],New=[
872 or @SynI{string_}@nt{simple_expression}],Old=[]}. For the occurrence raised by a
873 @nt{raise_statement} @Chg{Version=[4],New=[or @nt{raise_expression} ],Old=[]}with
874 an @Syni{exception_}@nt{name} but without a @Syni{string_}@nt{expression}@Chg{Version=[4],New=[
875 or @SynI{string_}@nt{simple_expression}],Old=[]},
876 the message is a string giving implementation-defined information about the
877 exception occurrence. @Chg{Version=[3],New=[For an occurrence originally raised
878 in some other manner (including by the failure of a language-defined check),
879 the message is an unspecified
880 string.@PDefn{unspecified} ],Old=[]}In all cases, Exception_Message
881 returns a string with lower bound 1.]}
882 @ImplDef{The information returned by Exception_Message.}
883
884 @begin{Discussion}
885 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0043-1]}
886 @ChgAdded{Version=[3],Text=[There is @ImplAdviceName about the contents
887 of this string for language-defined checks.]}
888 @end{Discussion}
889 @begin{Ramification}
890 @Leading@Keepnext@;Given an exception E, the @nt{raise_statement}:
891 @begin{Example}
892 @key[raise] E;
893 @end{Example}
894
895 @Leading@keepnext@;is equivalent to this call to Raise_Exception:
896 @begin{Example}
897 Raise_Exception(E'Identity, Message => @RI{implementation-defined-string});
898 @end{Example}
899
900 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00361-01]}
901 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[Similarly, the
902 @nt{raise_statement}:]}
903 @begin{Example}
904 @ChgRef{Version=[2],Kind=[Added]}
905 @ChgAdded{Version=[2],Text=[@key[raise] E @key[with] "some information";]}
906 @end{Example}
907
908 @ChgRef{Version=[2],Kind=[Added]}
909 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[is equivalent to
910 this call to Raise_Exception:]}
911 @begin{Example}
912 @ChgRef{Version=[2],Kind=[Added]}
913 @ChgAdded{Version=[2],Text=[Raise_Exception(E'Identity, Message => "some information");]}
914 @end{Example}
915 @end{Ramification}
916
917 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00361-01]}
918 @ChgAdded{Version=[2],Text=[Reraise_Occurrence reraises the specified exception
919 occurrence.]}
920
921 @begin{Ramification}
922 @Leading@keepnext@;The following handler:
923 @begin{Example}
924 @key[when] @key[others] =>
925 Cleanup;
926 @key[raise];
927 @end{Example}
928
929 @begin{WideAbove}
930 @Leading@keepnext@;is equivalent to this one:
931 @end{WideAbove}
932 @begin{Example}
933 @key[when] X : @key[others] =>
934 Cleanup;
935 Reraise_Occurrence(X);
936 @end{Example}
937
938 @end{Ramification}
939
940 Exception_Identity returns the identity of the exception of the
941 occurrence.
942
943 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00400-01]}
944 The @Chg{Version=[2],New=[Wide_@!Wide_@!Exception_Name],Old=[Exception_Name]}
945 functions return
946 the full expanded name of the exception, in upper case, starting with a root
947 library unit. For an exception declared immediately within package Standard,
948 the @nt{defining_@!identifier} is returned.
949 The result is implementation defined if the exception is declared within
950 an unnamed @nt{block_statement}.
951 @begin{Ramification}
952 See the @ImplPermName below.
953 @end{Ramification}
954 @begin{Honest}
955 This name, as well as each @nt{prefix} of it,
956 does not denote a @nt{renaming_declaration}.
957 @end{Honest}
958 @ChgImplDef{Version=[2],Kind=[Revised],InitialVersion=[0],
959 Text=[The result of @Chg{Version=[2],
960 New=[Exceptions.@!Wide_@!Wide_@!Exception_@!Name],Old=[Exceptions.@!Exception_@!Name]}
961 for @Chg{Version=[2],New=[exceptions],Old=[types]} declared within
962 an unnamed @nt{block_statement}.]}
963 @begin{Ramification}
964 Note that we're talking about the name of the exception,
965 not the name of the occurrence.
966 @end{Ramification}
967
968 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00400-01]}
969 @ChgAdded{Version=[2],Text=[The Exception_Name functions (respectively,
970 Wide_Exception_Name) return the same sequence of graphic characters as that
971 defined for Wide_Wide_Exception_Name, if all the graphic characters are defined
972 in Character (respectively, Wide_Character); otherwise, the sequence of
973 characters is implementation defined, but no shorter than that returned by
974 Wide_Wide_Exception_Name for the same value of the argument.]}
975 @ChgImplDef{Version=[2],Kind=[AddedNormal],Text=[@Chg{Version=[2],New=[
976 The sequence of characters of the value returned by Exceptions.Exception_Name
977 (respectively, Exceptions.Wide_Exception_Name) when some of the graphic
978 characters of Exceptions.Wide_Wide_Exception_Name are not defined in Character
979 (respectively, Wide_Character).],Old=[]}]}
980
981 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00378-01],ARef=[AI95-00417-01]}
982 @ChgAdded{Version=[2],Text=[The string returned by the Exception_Name,
983 Wide_Exception_Name, and Wide_Wide_Exception_Name functions has lower bound 1.]}
984
985 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00378-01]}
986 Exception_Information returns implementation-defined information
987 about the exception occurrence.
988 @Chg{Version=[2],New=[The returned string has lower bound 1.],Old=[]}
989 @ImplDef{The information returned by Exception_Information.}
990
991 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00241-01],ARef=[AI95-00446-01]}
992 @Chg{Version=[2],New=[],Old=[Raise_Exception and ]}Reraise_Occurrence
993 @Chg{Version=[2],New=[has],Old=[have]} no effect in the case of
994 @Chg{Version=[2],New=[],Old=[Null_Id or ]}Null_Occurrence.
995 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
996 @Chg{Version=[2],New=[Raise_Exception and Exception_Name raise Constraint_Error
997 for a Null_Id. Exception_Message, Exception_Name, and Exception_Information
998 raise Constraint_Error for a Null_Occurrence. Exception_Identity applied
999 to Null_Occurrence returns Null_Id.],
1000 Old=[Exception_Message, Exception_Identity, Exception_Name, and
1001 Exception_Information
1002 raise Constraint_Error for a Null_Id or Null_Occurrence.]}
1003
1004 @begin{Ramification}
1005 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00241-01]}
1006 @ChgAdded{Version=[2],Text=[Null_Occurrence can be tested for by comparing
1007 Exception_Identity(Occurrence) to Null_Id.]}
1008 @end{Ramification}
1009
1010 @begin{Discussion}
1011 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00446-01]}
1012 @ChgAdded{Version=[2],Text=[Raise_Exception was changed so that it always
1013 raises an exception and thus can be a No_Return procedure. A similar change
1014 was not made for Reraise_Occurrence, as doing so was determined to be a
1015 significant incompatibility. It is not unusual to pass an Exception_Occurrence
1016 to other code to delay raising it. If there was no exception, passing
1017 Null_Occurrence works fine (nothing is raised). Moreover, as there is no test
1018 for Null_Occurrence in Ada 95, this is the only way to write such code without
1019 using additional flags. Breaking this sort of code is unacceptable.]}
1020 @end{Discussion}
1021
1022 The Save_Occurrence procedure copies the Source to the Target.
1023 The Save_Occurrence function uses an @nt{allocator} of type
1024 Exception_Occurrence_Access to create a new object,
1025 copies the Source to this new object,
1026 and returns an access value designating this new object;
1027 @Redundant[the result may be deallocated using an instance of
1028 Unchecked_Deallocation.]
1029 @begin{Ramification}
1030 It's OK to pass Null_Occurrence to the Save_Occurrence
1031 subprograms;
1032 they don't raise an exception,
1033 but simply save the Null_Occurrence.
1034 @end{Ramification}
1035
1036 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00438-01]}
1037 @ChgAdded{Version=[2],Text=[Write_Exception_Occurrence writes a representation
1038 of an exception occurrence to a stream; Read_Exception_Occurrence reconstructs
1039 an exception occurrence from a stream (including one written in a different
1040 partition).]}
1041 @ChgNote{All of these notes (except the first) are moved from below.}
1042 @begin{Ramification}
1043 @ChgRef{Version=[2],Kind=[AddedNormal]}
1044 @ChgAdded{Version=[2],Text=[This routines are used to define the stream
1045 attributes (see @RefSecNum{Stream-Oriented Attributes}) for
1046 Exception_Occurrence.]}
1047
1048 @ChgRef{Version=[2],Kind=[AddedNormal]}
1049 @ChgAdded{Version=[2],Text=[The identity of the exception,
1050 as well as the Exception_Name and Exception_Message,
1051 have to be preserved across partitions.]}
1052
1053 @ChgRef{Version=[2],Kind=[AddedNormal]}
1054 @ChgAdded{Version=[2],Text=[The string returned by Exception_Name
1055 or Exception_Message on the result of calling the Read
1056 attribute on a given stream has to be the same as the value returned
1057 by calling the corresponding function on the exception occurrence
1058 that was written into the stream with the Write attribute.
1059 The string returned by Exception_Information need not be
1060 the same, since it is implementation defined anyway.]}
1061 @end{Ramification}
1062 @begin{Reason}
1063 @ChgRef{Version=[2],Kind=[AddedNormal]}
1064 @ChgAdded{Version=[2],Text=[This is important for supporting writing exception occurrences
1065 to external files for post-mortem analysis, as well as
1066 propagating exceptions across remote subprogram calls
1067 in a distributed system
1068 (see @RefSecNum{Remote Subprogram Calls}).]}
1069 @end{Reason}
1070 @end{StaticSem}
1071
1072 @begin{ImplReq}
1073 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00438-01]}
1074 @ChgDeleted{Version=[2],Text=[The implementation of the Write attribute
1075 (see @RefSecNum{Stream-Oriented Attributes})
1076 of Exception_Occurrence shall support writing
1077 a representation of an exception occurrence
1078 to a stream; the implementation of the Read attribute
1079 of Exception_Occurrence shall support reconstructing
1080 an exception occurrence from a stream (including one written in
1081 a different partition).]}
1082 @begin{Ramification}
1083 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
1084 @ChgDeleted{Version=[2],Text=[The identity of the exception,
1085 as well as the Exception_Name and Exception_Message,
1086 have to be preserved across partitions.]}
1087
1088 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
1089 @ChgDeleted{Version=[2],Text=[The string returned by Exception_Name
1090 or Exception_Message on the result of calling the Read
1091 attribute on a given stream has to be the same as the value returned
1092 by calling the corresponding function on the exception occurrence
1093 that was written into the stream with the Write attribute.
1094 The string returned by Exception_Information need not be
1095 the same, since it is implementation defined anyway.]}
1096 @end{Ramification}
1097 @begin{Reason}
1098 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
1099 @ChgDeleted{Version=[2],Text=[This is important for supporting writing
1100 exception occurrences to external files for post-mortem analysis, as well as
1101 propagating exceptions across remote subprogram calls
1102 in a distributed system
1103 (see @RefSecNum{Remote Subprogram Calls}).]}
1104 @end{Reason}
1105 @end{ImplReq}
1106 @begin{NotIso}
1107 @ChgAdded{Version=[2],Noparanum=[T],Text=[@Shrink{@i<Paragraph 16 was
1108 deleted.>}]}@Comment{This message should be deleted if the paragraphs
1109 are ever renumbered.}
1110 @end{NotIso}
1111
1112 @begin{ImplPerm}
1113 An implementation of Exception_Name in a space-constrained
1114 environment may return the
1115 @nt{defining_@!identifier} instead of the full expanded name.
1116
1117
1118 The string returned by Exception_Message may be truncated (to no less
1119 than 200 characters) by the Save_Occurrence procedure
1120 @Redundant[(not the function)],
1121 the Reraise_Occurrence procedure,
1122 and the re-raise statement.
1123 @begin{Reason}
1124 The reason for allowing truncation is to ease implementations.
1125 The reason for choosing the number 200 is that this is the minimum
1126 source line length that implementations have to support,
1127 and this feature seems vaguely related since it's usually a
1128 @lquotes@;one-liner@rquotes@;.
1129 Note that an implementation is allowed to do this truncation even if it
1130 supports arbitrarily long lines.
1131 @end{Reason}
1132 @end{ImplPerm}
1133
1134 @begin{ImplAdvice}
1135 Exception_Message (by default) and Exception_Information should produce
1136 information useful for debugging.
1137 Exception_Message should be short (about one line),
1138 whereas Exception_Information can be long.
1139 Exception_Message should not include the Exception_Name.
1140 Exception_Information should include both the Exception_Name and the
1141 Exception_Message.
1142
1143
1144 @ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],
1145 Text=[Exception_Information should provide
1146 information useful for debugging, and should include the Exception_Name
1147 and Exception_Message.]}]}
1148 @ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],
1149 Text=[Exception_Message by default should be short, provide
1150 information useful for debugging, and should not include the Exception_Name.]}]}
1151
1152 @begin{Reason}
1153 It may seem strange to define two subprograms whose semantics is
1154 implementation defined.
1155 The idea is that a program can print out debugging/error-logging
1156 information in a portable way.
1157 The program is portable in the sense that it will work in any
1158 implementation; it might print out different information, but the
1159 presumption is that the information printed out is appropriate for
1160 debugging/error analysis on that system.
1161 @end{Reason}
1162 @begin{ImplNote}
1163 As an example, Exception_Information might include
1164 information identifying the location where the exception
1165 occurred, and, for predefined exceptions, the specific kind of
1166 language-defined check that failed.
1167 There is an implementation trade-off here, between how much
1168 information is represented in an Exception_Occurrence, and how much
1169 can be passed through a re-raise.
1170
1171 The string returned should be in a form suitable for printing to an
1172 error log file.
1173 This means that it might need to contain line-termination control
1174 characters with implementation-defined I/O semantics.
1175 The string should neither start nor end with a newline.
1176
1177 If an implementation chooses to provide additional functionality
1178 related to exceptions and their occurrences,
1179 it should do so by providing one or more children of
1180 Ada.Exceptions.
1181
1182 Note that exceptions behave as if declared at library level;
1183 there is no @lquotes@;natural scope@rquotes@; for an exception; an exception always
1184 exists. Hence, there is no harm in saving an exception occurrence in
1185 a data structure, and reraising it later. The reraise has to occur
1186 as part of the same program execution, so saving an exception
1187 occurrence in a file, reading it back in from a different
1188 program execution, and then reraising it is not required to work.
1189 This is similar to I/O of access types.
1190 Note that it is possible to use RPC to propagate exceptions across
1191 partitions.
1192
1193 @Leading@;Here's one way to implement Exception_Occurrence in the private part
1194 of the package. Using this method, an implementation need store only
1195 the actual number of characters in exception messages. If the user
1196 always uses small messages, then exception occurrences can be small.
1197 If the user never uses messages, then exception occurrences can be
1198 smaller still:
1199 @begin{Example}
1200 @key[type] Exception_Occurrence(Message_Length : Natural := 200) @key[is]
1201 @key[limited] @key[record]
1202 Id : Exception_Id;
1203 Message : String(1..Message_Length);
1204 @key[end] @key[record];
1205 @end{Example}
1206
1207 @Leading@;At the point where an exception is raised, an Exception_Occurrence
1208 can be allocated on the stack with exactly the right amount of space
1209 for the message @em none for an empty message. This is just like
1210 declaring a constrained object of the type:
1211 @begin{Example}
1212 Temp : Exception_Occurrence(10); --@RI{ for a 10-character message}
1213 @end{Example}
1214
1215 After finding the appropriate handler, the stack can be cut back,
1216 and the Temp copied to the right place. This is similar to returning
1217 an unknown-sized object from a function. It is not necessary to
1218 allocate the maximum possible size for every Exception_Occurrence.
1219 If, however, the user declares an Exception_Occurrence object,
1220 the discriminant will be permanently set to 200. The Save_Occurrence
1221 procedure would then truncate the Exception_Message. Thus, nothing is
1222 lost until the user tries to save the occurrence. If the user is
1223 willing to pay the cost of heap allocation, the Save_Occurrence
1224 function can be used instead.
1225
1226 Note that any arbitrary-sized implementation-defined Exception_Information can
1227 be handled in a similar way. For example, if the
1228 Exception_Occurrence includes a stack traceback, a discriminant can
1229 control the number of stack frames stored. The traceback would be
1230 truncated or entirely deleted by the Save_Occurrence procedure @em as
1231 the implementation sees fit.
1232
1233 If the internal representation involves pointers to data structures
1234 that might disappear, it would behoove the implementation to
1235 implement it as a controlled type,
1236 so that assignment can either copy the data structures or else null
1237 out the pointers.
1238 Alternatively, if the data structures being pointed at are in a
1239 task control block,
1240 the implementation could keep a unique sequence number
1241 for each task, so it could tell when a task's data structures no longer
1242 exist.
1243
1244 Using the above method, heap space is never allocated unless the user
1245 calls the Save_Occurrence function.
1246
1247 @Leading@;An alternative implementation would be to store the message strings
1248 on the heap when the exception is raised. (It could be the global
1249 heap, or it could be a special heap just for this purpose @em it
1250 doesn't matter.) This representation would be used only for choice
1251 parameters. For normal user-defined exception occurrences, the
1252 Save_Occurrence procedure would copy the message string into the
1253 occurrence itself, truncating as necessary. Thus, in this
1254 implementation, Exception_Occurrence would be implemented as a
1255 variant record:
1256 @begin{Example}
1257 @key[type] Exception_Occurrence_Kind @key[is] (Normal, As_Choice_Param);
1258
1259 @key[type] Exception_Occurrence(Kind : Exception_Occurrence_Kind := Normal) @key[is]
1260 @key[limited] @key[record]
1261 @key[case] Kind @key[is]
1262 @key[when] Normal =>
1263 ... --@RI{ space for 200 characters}
1264 @key[when] As_Choice_Param =>
1265 ... --@RI{ pointer to heap string}
1266 @key[end] @key[case];
1267 @key[end] @key[record];
1268 @end{Example}
1269
1270 Exception_Occurrences created by the run-time system during exception
1271 raising would be As_Choice_Param. User-declared ones would be Normal @em the
1272 user cannot see the discriminant, and so cannot set it to As_Choice_Param.
1273 The strings in the heap would be freed upon completion of the
1274 handler.
1275
1276 This alternative implementation corresponds to a heap-based
1277 implementation of functions returning unknown-sized results.
1278
1279 @Leading@;One possible implementation of Reraise_Occurrence is as follows:
1280 @begin{Example}
1281 @key[procedure] Reraise_Occurrence(X : @key[in] Exception_Occurrence) @key[is]
1282 @key[begin]
1283 Raise_Exception(Identity(X), Exception_Message(X));
1284 @key[end] Reraise_Occurrence;
1285 @end{Example}
1286
1287 However, some implementations may wish to retain more information
1288 across a re-raise @em a stack traceback, for example.
1289 @end{ImplNote}
1290 @begin{Ramification}
1291 Note that Exception_Occurrence is a definite subtype.
1292 Hence, values of type Exception_Occurrence may be written to
1293 an error log for later analysis, or may be passed
1294 to subprograms for immediate error analysis.
1295 @end{Ramification}
1296 @begin{ImplNote}
1297 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00400-01]}
1298 @ChgDeleted{Version=[2],Text=[If an implementation chooses to have a mode in
1299 which it supports non-Latin-1 characters in identifiers, then it needs to
1300 define what the above functions return in the case where the name of an
1301 exception contains such a character.]}
1302 @end{ImplNote}
1303 @end{ImplAdvice}
1304
1305 @begin{Extend83}
1306 @Defn{extensions to Ada 83}
1307 The Identity attribute of exceptions is new, as is the package
1308 Exceptions.
1309 @end{Extend83}
1310
1311 @begin{Inconsistent95}
1312 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00241-01]}
1313 @ChgAdded{Version=[2],Text=[@Defn{inconsistencies with Ada 95}
1314 @B[Amendment Correction:] Exception_Identity of an Exception_Occurrence
1315 now is defined to return Null_Id for Null_Occurrence, rather than raising
1316 Constraint_Error. This provides a simple way to test for Null_Occurrence.
1317 We expect that programs that need Constraint_Error raised will be very rare;
1318 they can be easily fixed by explicitly testing for Null_Id or by using
1319 Exception_Name instead.]}
1320
1321 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00378-01],ARef=[AI95-00417-01]}
1322 @ChgAdded{Version=[2],Text=<@B[Amendment Correction:] We now define the lower
1323 bound of the string returned from [[Wide_]Wide_]Exception_Name,
1324 Exception_Message, and Exception_Information. This makes working with the
1325 returned string easier, and is consistent with many other string-returning
1326 functions in Ada. This is technically an inconsistency; if a program
1327 depended on some other lower bound for the string returned from one of
1328 these functions, it could fail when compiled with Ada 2005. Such code is
1329 not portable even between Ada 95 implementations, so it should be very rare.>}
1330
1331 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00446-01]}
1332 @ChgAdded{Version=[2],Text=[@B[Amendment Correction:]
1333 Raise_Exception now raises Constraint_Error if passed Null_Id. This means
1334 that it always raises an exception, and thus we can apply pragma No_Return to
1335 it. We expect that programs that call Raise_Exception with Null_Id will be
1336 rare, and programs that do that and expect no exception to be raised will be
1337 rarer; such programs can be easily fixed by explicitly testing for Null_Id
1338 before calling Raise_Exception.]}
1339 @end{Inconsistent95}
1340
1341 @begin{Incompatible95}
1342 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00400-01],ARef=[AI95-00438-01]}
1343 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
1344 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}
1345 Functions Wide_Exception_Name and Wide_Wide_Exception_Name, and procedures
1346 Read_Exception_Occurrence and Write_Exception_Occurrence
1347 are @Chg{Version=[3],New=[],Old=[newly ]}added
1348 to Exceptions. If Exceptions is referenced in a
1349 @nt{use_clause}, and an entity @i<E> with the same @nt{defining_identifier}
1350 as a new entity in Exceptions is defined in a
1351 package that is also referenced in a @nt{use_clause}, the entity @i<E> may no
1352 longer be use-visible, resulting in errors. This should be rare and is easily
1353 fixed if it does occur.]}
1354 @end{Incompatible95}
1355
1356 @begin{Extend95}
1357 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00362-01]}
1358 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1359 The package Exceptions is preelaborated, and types Exception_Id and
1360 Exception_Occurrence have preelaborable initialization, allowing this package
1361 to be used in preelaborated units.]}
1362 @end{Extend95}
1363
1364 @begin{DiffWord95}
1365 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00361-01]}
1366 @ChgAdded{Version=[2],Text=[The meaning of Exception_Message is reworded to
1367 reflect that the string can come from a @nt{raise_statement} as well as a
1368 call of Raise_Exception.]}
1369
1370 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00400-01]}
1371 @ChgAdded{Version=[2],Text=[Added Wide_Exception_Name and
1372 Wide_Wide_Exception_Name
1373 because identifiers can now contain characters outside of Latin-1.]}
1374 @end{DiffWord95}
1375
1376 @begin{DiffWord2005}
1377 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0043-1]}
1378 @ChgAdded{Version=[3],Text=[@b<Correction:> Added explicit wording
1379 that the exception message for language-defined checks is unspecified.
1380 The old wording appeared inclusive, but it was not.]}
1381 @end{DiffWord2005}
1382
1383
1384 @LabeledAddedSubClause{Version=[2],Name=[Pragmas Assert and Assertion_Policy]}
1385
1386 @begin{Intro}
1387 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1388 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0274-1]}
1389 @ChgAdded{Version=[2],Text=[Pragma Assert is used to assert the truth of a
1390 @Chg{Version=[3],New=[boolean],Old=[Boolean]} expression at
1391 @Chg{Version=[3],New=[a],Old=[any]} point within a sequence of declarations or
1392 statements.@Chg{Version=[3],New=[],Old=[ Pragma Assertion_Policy is used to
1393 control whether such assertions@Defn{Assertions} are to be ignored by the
1394 implementation, checked at run-time, or handled in some implementation-defined
1395 manner.]}]}
1396
1397 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0274-1]}
1398 @ChgAdded{Version=[3],Text=[Assert pragmas, subtype predicates (see
1399 @RefSecNum{Subtype Predicates}), preconditions and postconditions (see
1400 @RefSecNum{Preconditions and Postconditions}), and type invariants (see
1401 @RefSecNum{Type Invariants}) are collectively referred to as
1402 @i{assertions}; their boolean expressions are referred to as @i{assertion
1403 expressions}.@Defn{assertions}@Defn{assertion expressions}]}
1404
1405 @ChgToGlossary{Version=[3],Kind=[Added],Term=<Predicate>,
1406 Text=<@ChgAdded{Version=[3],Text=[A predicate is an assertion
1407 that is expected to be True for all objects of a given
1408 subtype.]}>}
1409
1410 @ChgToGlossary{Version=[3],Kind=[Added],Term=<Precondition>,
1411 Text=<@ChgAdded{Version=[3],Text=[A precondition is an assertion
1412 that is expected to be True when a given subprogram is
1413 called.]}>}
1414
1415 @ChgToGlossary{Version=[3],Kind=[Added],Term=<Postcondition>,
1416 Text=<@ChgAdded{Version=[3],Text=[A postcondition is an assertion
1417 that is expected to be True when a given subprogram returns
1418 normally.]}>}
1419
1420 @Comment{Really [RevisedAdded],InitialVersion=[3], below, but we don't have that implemented yet.}
1421 @ChgToGlossary{Version=[4],Kind=[Added],Term=<Invariant>,
1422 Text=<@ChgAdded{Version=[3],Text=[@Chg{Version=[4],New=[An],Old=[A]} invariant
1423 is an assertion that is expected
1424 to be True for all objects of a given private type when viewed from outside the
1425 defining package.]}>}
1426
1427 @ChgToGlossary{Version=[4],Kind=[Added],Term=<Type Invariant>,
1428 Text=<@ChgAdded{Version=[4],Text=[See Invariant.]}>}
1429
1430 @ChgToGlossary{Version=[3],Kind=[Added],Term=<Assertion>,
1431 Text=<@ChgAdded{Version=[3],Text=[An assertion is a boolean expression that
1432 appears in any of the following: a @nt{pragma} Assert, a predicate, a
1433 precondition, a postcondition, an invariant, a constraint, or a null exclusion.
1434 An assertion is expected to be True at run time at certain specified places.]}>}
1435
1436 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0274-1]}
1437 @ChgAdded{Version=[3],Text=[Pragma Assertion_Policy is used to control whether
1438 assertions are to be ignored by the implementation, checked at run time, or
1439 handled in some implementation-defined manner.]}
1440 @end{Intro}
1441
1442 @begin{Syntax}
1443 @begin{SyntaxText}
1444 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1445 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[The form of a
1446 @nt{pragma} Assert is as follows:]}
1447 @end{SyntaxText}
1448
1449 @ChgRef{Version=[2],Kind=[AddedNormal]}
1450 @AddedPragmaSyn<Version=[2],@ChgAdded{Version=[2],
1451 Text=`@key{pragma} @prag<Assert>([Check =>] @SynI{boolean_}@Syn2{expression}[, [Message =>] @SynI{string_}@Syn2{expression}]);'}>
1452
1453 @begin{SyntaxText}
1454 @ChgRef{Version=[2],Kind=[AddedNormal]}
1455 @ChgAdded{Version=[2],Text=[A @nt{pragma} Assert is allowed at the place where a
1456 @nt{declarative_item} or a @nt{statement} is allowed.]}
1457
1458 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1459 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[The form of a
1460 @nt{pragma} Assertion_Policy is as follows:]}
1461 @end{SyntaxText}
1462
1463 @ChgRef{Version=[2],Kind=[AddedNormal]}
1464 @AddedPragmaSyn<Version=[2],@ChgAdded[Version=[2],
1465 Text=<@key{pragma} @prag<Assertion_Policy>(@SynI{policy_}@Syn2{identifier});>]>
1466
1467 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0290-1]}
1468 @AddedPragmaSyn<Version=[3],@ChgAdded[Version=[3],
1469 Text=[@key{pragma} @prag<Assertion_Policy>(@*
1470 @ @ @ @ @ @ @ @ @ @SynI(assertion_)@Syn2{aspect_mark} => @SynI{policy_}@Syn2{identifier}@*
1471 @ @ @ @ @ {, @SynI(assertion_)@Syn2{aspect_mark} => @SynI{policy_}@Syn2{identifier}});]]>
1472
1473 @ChgRef{Version=[2],Kind=[AddedNormal]}
1474 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0290-1]}
1475 @ChgAdded{Version=[2],Text=[@PDefn2{Term=[configuration pragma], Sec=(Assertion_Policy)}
1476 @PDefn2{Term=[pragma, configuration], Sec=(Assertion_Policy)}
1477 A @nt{pragma} Assertion_Policy is@Chg{Version=[3],New=[ allowed only immediately
1478 within a @nt{declarative_part}, immediately within a @nt{package_specification},
1479 or as],Old=[]} a configuration pragma.]}
1480
1481 @end{Syntax}
1482
1483 @begin{Resolution}
1484 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1485 @ChgAdded{Version=[2],Text=[The expected type for the
1486 @Syni{boolean_}@nt{expression} of a @nt{pragma} Assert
1487 is any boolean type. The expected type for the @Syni{string_}@nt{expression}
1488 of a @nt{pragma} Assert is type String.]}
1489 @begin{Reason}
1490 @ChgRef{Version=[2],Kind=[AddedNormal]}
1491 @ChgAdded{Version=[2],Text=[We allow any boolean type to be like
1492 @nt{if_statement}s and other conditionals; we only allow String for the
1493 message in order to match @nt{raise_statement}s.]}
1494 @end{Reason}
1495 @end{Resolution}
1496
1497
1498 @begin{Legality}
1499 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1500 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0290-1]}
1501 @ChgAdded{Version=[2],Text=[@Chg{Version=[3],New=[The
1502 @SynI<assertion_>@nt{aspect_mark} of a @nt{pragma} Assertion_Policy shall
1503 be one of Assert, Static_Predicate, Dynamic_Predicate, Pre, Pre'Class, Post,
1504 Post'Class, Type_Invariant, Type_Invariant'Class, or some
1505 implementation defined @nt{aspect_mark}. ],Old=[]}The
1506 @SynI<policy_>@nt<identifier>@Chg{Version=[3],New=[],Old=[ of a @nt{pragma}
1507 Assertion_Policy]} shall be either Check, Ignore, or @Chg{Version=[3],New=[some],Old=[an]}
1508 implementation-defined @nt{identifier}.]}
1509 @ChgImplDef{Version=[3],Kind=[Revised],InitialVersion=[2],
1510 Text=[@Chg{Version=[2],New=[Implementation-defined
1511 @SynI<policy_>@nt{identifier}s @Chg{Version=[3],New=[and
1512 @SynI<assertion_>@nt{aspect_mark}s ],Old=[]}allowed in a @nt{pragma}
1513 Assertion_Policy.],Old=[]}]}
1514 @end{Legality}
1515
1516 @begin{StaticSem}
1517 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1518 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0290-1]}
1519 @ChgAdded{Version=[2],Text=[A @nt{pragma} Assertion_Policy
1520 @Chg{Version=[3],New=[determines for each assertion aspect named in
1521 the @nt{pragma_argument_association}s whether assertions of the given aspect are to
1522 be enforced by a run-time check. The @SynI<policy_>@nt{identifier} Check
1523 requires that assertion expressions of the given aspect be checked that they
1524 evaluate to True at the points specified for the given aspect; the
1525 @SynI<policy_>@nt{identifier} Ignore requires that the assertion expression not
1526 be evaluated at these points, and the run-time checks not be performed.
1527 @Redundant[Note that for subtype predicate aspects (see @RefSecNum{Subtype Predicates}),
1528 even when the applicable Assertion_Policy is Ignore, the predicate will still be
1529 evaluated as part of membership tests and Valid @nt{attribute_reference}s, and
1530 if static, will still have an effect on loop iteration over the subtype, and the
1531 selection of @nt{case_statement_alternative}s and @nt{variant}s.]],Old=[is a
1532 configuration pragma that specifies the assertion policy in effect for the
1533 compilation units to which it applies. Different policies may apply to different
1534 compilation units within the same partition. The default assertion policy is
1535 implementation-defined.]}]}
1536
1537 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0290-1]}
1538 @ChgAdded{Version=[3],Text=[If no @SynI<assertion_>@nt{aspect_mark}s are
1539 specified in the pragma, the specified policy applies to all assertion
1540 aspects.]}
1541
1542 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0290-1]}
1543 @ChgAdded{Version=[3],Text=[A @nt{pragma} Assertion_Policy applies to the named
1544 assertion aspects in a specific region, and applies to all assertion expressions
1545 specified in that region. A @nt{pragma} Assertion_Policy given in a
1546 @nt{declarative_part} or immediately within a @nt{package_specification} applies
1547 from the place of the pragma to the end of the innermost enclosing declarative
1548 region. The region for a @nt{pragma} Assertion_Policy given as a configuration
1549 pragma is the declarative region for the entire compilation unit (or units) to
1550 which it applies.]}
1551
1552 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0290-1]}
1553 @ChgAdded{Version=[3],Text=[If a @nt{pragma} Assertion_Policy applies to
1554 a @nt{generic_instantiation}, then
1555 the @nt{pragma} Assertion_Policy applies to the entire instance.]}
1556
1557 @begin{Ramification}
1558 @ChgRef{Version=[3],Kind=[Added]}
1559 @ChgAdded{Version=[3],Text=[This means that an Assertion_Policy pragma that
1560 occurs in a scope enclosing the declaration of a generic unit but not also
1561 enclosing the declaration of a given instance of that generic unit will not
1562 apply to assertion expressions occurring within the given instance.]}
1563 @end{Ramification}
1564
1565 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0290-1]}
1566 @ChgAdded{Version=[3],Text=[If multiple Assertion_Policy pragmas apply to a
1567 given construct for a given assertion aspect, the assertion policy is determined
1568 by the one in the innermost enclosing region of a @nt{pragma} Assertion_Policy
1569 specifying a policy for the assertion aspect. If no such Assertion_Policy pragma
1570 exists, the policy is implementation defined.]}
1571
1572 @ChgImplDef{Version=[2],Kind=[AddedNormal],Text=[@Chg{Version=[2],New=[The
1573 default assertion policy.],Old=[]}]}
1574
1575 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1576 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[The following
1577 language-defined library package exists:]}
1578
1579 @begin{Example}
1580 @ChgRef{Version=[2],Kind=[AddedNormal]}
1581 @ChgAdded{Version=[2],Text=[@ChildUnit{Parent=[Ada],Child=[Assertions]}@key[package] Ada.Assertions @key[is]
1582 @key[pragma] Pure(Assertions);]}
1583
1584 @ChgRef{Version=[2],Kind=[AddedNormal]}
1585 @ChgAdded{Version=[2],Text=[ @AdaExcDefn{Assertion_Error} : @key<exception>;]}
1586
1587 @ChgRef{Version=[2],Kind=[AddedNormal]}
1588 @ChgAdded{Version=[2],Text=[ @key<procedure> @AdaSubDefn{Assert}(Check : @key<in> Boolean);
1589 @key<procedure> @AdaSubDefn{Assert}(Check : @key<in> Boolean; Message : @key<in> String);]}
1590
1591 @ChgRef{Version=[2],Kind=[AddedNormal]}
1592 @ChgAdded{Version=[2],Text=[@key<end> Ada.Assertions;]}
1593 @end{Example}
1594
1595 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1596 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0290-1]}
1597 @ChgAdded{Version=[2],Text=[A compilation unit containing a
1598 @Chg{Version=[3],New=[check for an assertion (including a ],Old=[]}@nt{pragma}
1599 Assert@Chg{Version=[3],New=[)],Old=[]} has a
1600 semantic dependence on the Assertions library unit.]}
1601
1602 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1603 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0290-1]}
1604 @ChgAdded{Version=[2],Text=[@Chg{Version=[3],New=[],Old=[The assertion policy
1605 that applies to a generic unit also applies to all its instances.]}]}
1606 @end{StaticSem}
1607
1608
1609 @begin{RunTime}
1610 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1611 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0290-1]}
1612 @ChgAdded{Version=[2],Text=[@Chg{Version=[3],New=[],Old=[An assertion policy
1613 @defn{assertion policy}specifies how a @nt{pragma} Assert is interpreted by the
1614 implementation. If the assertion
1615 policy is Ignore at the point of a @nt{pragma} Assert, the pragma is ignored.]}
1616 If @Chg{Version=[3],New=[performing checks is required by ],Old=[]}the
1617 @Chg{Version=[3],New=[Assert ],Old=[]}assertion policy
1618 @Chg{Version=[3],New=[in effect],Old=[is Check]}
1619 at the @Chg{Version=[3],New=[place],Old=[point]} of a @nt{pragma} Assert, the
1620 elaboration of the pragma consists of evaluating the boolean expression, and if
1621 the result is False, evaluating the Message argument, if any, and raising the
1622 exception Assertions.Assertion_Error, with a message if the Message
1623 argument is provided.@Defn2{Term=[assertion policy],Sec=[Assert pragma]}@Defn2{Term=(Assertion_Error),
1624 Sec=(raised by failure of assertion)}]}
1625
1626 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1627 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[Calling the procedure
1628 Assertions.Assert without a Message parameter is equivalent to:]}
1629
1630 @begin{Example}
1631 @ChgRef{Version=[2],Kind=[AddedNormal]}
1632 @ChgAdded{Version=[2],Text=[@key<if> Check = False @key<then>
1633 @key<raise> Ada.Assertions.Assertion_Error;
1634 @key{end} @key{if};]}
1635 @end{Example}
1636
1637 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1638 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[Calling the procedure
1639 Assertions.Assert with a Message parameter is equivalent to:]}
1640
1641 @begin{Example}
1642 @ChgRef{Version=[2],Kind=[AddedNormal]}
1643 @ChgAdded{Version=[2],Text=[@key<if> Check = False @key<then>
1644 @key<raise> Ada.Assertions.Assertion_Error @key<with> Message;
1645 @key{end} @key{if};]}
1646 @end{Example}
1647
1648 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1649 @ChgAdded{Version=[2],Text=[The procedures Assertions.Assert have these
1650 effects independently of the assertion policy in effect.]}
1651
1652 @end{RunTime}
1653
1654 @begin{Bounded}
1655 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0274-1]}
1656 @ChgAdded{Version=[3],Text=[It is a bounded error to invoke a potentially
1657 blocking operation (see @RefSecNum{Protected Subprograms and Protected Actions})
1658 during the evaluation of an assertion expression associated with a call on, or
1659 return from, a protected operation. If the bounded error is
1660 detected, Program_Error is raised. If not detected, execution proceeds normally,
1661 but if it is invoked within a protected action, it might result in deadlock or a
1662 (nested) protected action.]}
1663 @end{Bounded}
1664
1665 @begin{ImplPerm}
1666 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1667 @ChgAdded{Version=[2],Text=[Assertion_Error may be declared by renaming an
1668 implementation-defined exception from another package.]}
1669 @begin{Reason}
1670 @ChgRef{Version=[2],Kind=[AddedNormal]}
1671 @ChgAdded{Version=[2],Text=[This permission is intended to allow implementations
1672 which had an implementation-defined Assert pragma to continue to use their
1673 originally defined exception. Without this permission, such an implementation
1674 would be incorrect, as Exception_Name would return the wrong name.]}
1675 @end{Reason}
1676 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1677 @ChgAdded{Version=[2],Text=[Implementations may define their own assertion policies.]}
1678
1679 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0274-1]}
1680 @ChgAdded{Version=[3],Text=[If the result of a function call in an assertion is
1681 not needed to determine the value of the assertion expression, an implementation
1682 is permitted to omit the function call. @Redundant[This permission applies even
1683 if the function has side effects.]]}
1684
1685 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0274-1]}
1686 @ChgAdded{Version=[3],Text=[An implementation need not allow the specification
1687 of an assertion expression if the evaluation of the expression has a side effect
1688 such that an immediate reevaluation of the expression could produce a different
1689 value. Similarly, an implementation need not allow the specification of an
1690 assertion expression that is checked as part of a call on or return from a
1691 callable entity @i<C>, if the evaluation of the expression has a side effect
1692 such that the evaluation of some other assertion expression associated with the
1693 same call of (or return from) @i<C> could produce a different value than it
1694 would if the first expression had not been evaluated.]}
1695
1696 @begin{Ramification}
1697 @ChgRef{Version=[3],Kind=[AddedNormal]}
1698 @ChgAdded{Version=[3],Text=[This allows an implementation to reject such
1699 assertions. To maximize portability, assertions should not include expressions
1700 that contain these sorts of side effects.]}
1701 @end{Ramification}
1702 @begin{Discussion}
1703 @ChgRef{Version=[3],Kind=[AddedNormal]}
1704 @ChgAdded{Version=[3],Text=[The intended effect of the second part of the
1705 rule (the part starting with @ldquote@;Similarly@rdquote) is that an
1706 evaluation of the involved assertion expressions (subtype predicates, type
1707 invariants, preconditions and postconditions) in any order yields identical
1708 results.]}
1709
1710 @ChgRef{Version=[3],Kind=[AddedNormal]}
1711 @ChgAdded{Version=[3],Text=[The rule is intended to apply to all of the
1712 assertion expressions that are evaluated at the start of call (and similarly
1713 for the assertion expressions that are evaluated during the return from a
1714 call), but not other assertions actually given in the body, nor between the
1715 assertions checked at the start and end of the call. Specifically, a side
1716 effect that alters a variable in a function called from a precondition
1717 expression that changes the result of a postcondition expression of the same
1718 subprogram does @i<not> trigger these rules unless it also changes
1719 the value of a reevaluation of the precondition expression.]}
1720 @end{Discussion}
1721 @begin{Metarules}
1722 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0005-1]}
1723 @ChgAdded{Version=[4],Text=[Our intent is that any assertion expression that
1724 violates this ImplPerm is considered pathological. We definitely want
1725 compilers to be able to assume that if you evaluate an assertion expression
1726 once and it is True, you don't need to evaluate it again if all you are
1727 doing in the mean time is evaluating assertion expressions. We were unable
1728 to find wording that had this effect that didn't throw out important other
1729 cases (logging, memo functions), so we settled for a strong warning that
1730 compilers can reject such pathologies. Perhaps in a future version of
1731 Ada we'll be able to tighten this up.]}
1732 @end{Metarules}
1733 @end{ImplPerm}
1734
1735 @begin{Notes}
1736 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1737 @ChgAdded{Version=[2],Text=[Normally, the boolean expression in a @nt{pragma}
1738 Assert should not call functions that have significant side effects when the
1739 result of the expression is True, so that the particular assertion policy in
1740 effect will not affect normal operation of the program.]}
1741 @end{Notes}
1742
1743 @begin{Extend95}
1744 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00286-01]}
1745 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1746 Pragmas Assert and Assertion_Policy, and package Assertions are new.]}
1747 @end{Extend95}
1748
1749 @begin{Incompatible2005}
1750 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0274-1]}
1751 @ChgAdded{Version=[3],Text=[@Defn{incompatibilities with Ada 2005}
1752 There now is an @ImplPermName to reject an assertion expression
1753 that calls a function that has a side effect such that an immediate
1754 reevalution of the expression could produce a different value.
1755 This means that a @nt{pragma} Assert that works in Ada 2005 might be illegal
1756 in Ada 2012 in the unlikely event that the compiler detected
1757 such an error. This should be unlikely to occur in practice and it
1758 is considered a good thing, as the original expression was tricky and
1759 probably was not portable (as order of evaluation is unspecified within
1760 an expression). Moreover, no compiler is @i<required> to reject such
1761 expressions, so there is no need for any compiler to change behavior.]}
1762 @end{Incompatible2005}
1763
1764 @begin{Extend2005}
1765 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0290-1]}
1766 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
1767 Assertion_Policy pragmas are now allowed in more places and can specify
1768 behavior for individual kinds of assertions.]}
1769 @end{Extend2005}
1770
1771
1772
1773 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
1774 @LabeledSubClause{Example of Exception Handling}
1775
1776 @begin{Examples}
1777 @Leading@;Exception handling may be used to separate the detection of an error
1778 from the response to that error:
1779 @begin{Example}
1780 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00433-01]}
1781 @Chg{Version=[2],New=[],Old=[@key[with] Ada.Exceptions;
1782 @key[use] Ada;
1783 ]}@key[package] File_System @key[is]
1784 @key[type] File_Handle @key[is] @key[limited] @key[private];
1785
1786 File_Not_Found : @key[exception];
1787 @key[procedure] Open(F : @key[in] @key[out] File_Handle; Name : String);
1788 --@RI{ raises File_Not_Found if named file does not exist}
1789
1790 End_Of_File : @key[exception];
1791 @key[procedure] Read(F : @key[in] @key[out] File_Handle; Data : @key[out] Data_Type);
1792 --@RI{ raises End_Of_File if the file is not open}
1793
1794 ...
1795 @key[end] File_System;
1796
1797
1798 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00433-01]}
1799 @key[package] @key[body] File_System @key[is]
1800 @key[procedure] Open(F : @key[in] @key[out] File_Handle; Name : String) @key[is]
1801 @key[begin]
1802 @key[if] File_Exists(Name) @key[then]
1803 ...
1804 @key[else]
1805 @Chg{Version=[2],New=[@key[raise] ],Old=[Exceptions.Raise_Exception(]}File_Not_Found@Chg{Version=[2],New=[ @key[with] ],Old=['Identity,
1806 ]}"File not found: " & Name & "."@Chg{Version=[2],New=[],Old=[)]};
1807 @key[end] @key[if];
1808 @key[end] Open;
1809
1810 @key[procedure] Read(F : @key[in] @key[out] File_Handle; Data : @key[out] Data_Type) @key[is]
1811 @key[begin]
1812 @key[if] F.Current_Position <= F.Last_Position @key[then]
1813 ...
1814 @key[else]
1815 @key[raise] End_Of_File;
1816 @key[end] @key[if];
1817 @key[end] Read;
1818
1819 ...
1820
1821 @key[end] File_System;
1822
1823
1824 @key[with] Ada.Text_IO;
1825 @key[with] Ada.Exceptions;
1826 @key[with] File_System; @key[use] File_System;
1827 @key[use] Ada;
1828 @key[procedure] Main @key[is]
1829 @key[begin]
1830 ... --@RI{ call operations in File_System}
1831 @key[exception]
1832 @key[when] End_Of_File =>
1833 Close(Some_File);
1834 @key[when] Not_Found_Error : File_Not_Found =>
1835 Text_IO.Put_Line(Exceptions.Exception_Message(Not_Found_Error));
1836 @key[when] The_Error : @key[others] =>
1837 Text_IO.Put_Line("Unknown error:");
1838 @key[if] Verbosity_Desired @key[then]
1839 Text_IO.Put_Line(Exceptions.Exception_Information(The_Error));
1840 @key[else]
1841 Text_IO.Put_Line(Exceptions.Exception_Name(The_Error));
1842 Text_IO.Put_Line(Exceptions.Exception_Message(The_Error));
1843 @key[end] @key[if];
1844 @key[raise];
1845 @key[end] Main;
1846 @end{Example}
1847
1848
1849 In the above example, the File_System package contains information about
1850 detecting certain exceptional situations,
1851 but it does not specify how to handle those situations.
1852 Procedure Main specifies how to handle them;
1853 other clients of File_System might have different handlers,
1854 even though the exceptional situations arise from the same basic causes.
1855 @end{Examples}
1856
1857 @begin{DiffWord83}
1858 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
1859 The sections labeled @lquotes@;Exceptions Raised During ...@rquotes@;
1860 are subsumed by this @Chg{Version=[3],New=[subclause],Old=[clause]},
1861 and by parts of @Chg{Version=[3],New=[Clause],Old=[Section]}
1862 @RefSecNum{Tasks and Synchronization}.
1863 @end{DiffWord83}
1864
1865 @LabeledClause{Suppressing Checks}
1866
1867 @begin{Intro}
1868 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
1869 @Chg{Version=[2],New=[@i{Checking pragmas}@Defn{checking pragmas} give
1870 instructions to an implementation on handling language-defined checks.],Old=[]}
1871 A @nt{pragma} Suppress gives permission to an
1872 implementation to omit certain language-defined checks@Chg{Version=[2],
1873 New=[, while a @nt<pragma> Unsuppress revokes the permission to omit checks.],Old=[]}.
1874
1875 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0264-1]}
1876 @Defn{language-defined check}
1877 @Defn2{Term=[check], Sec=(language-defined)}
1878 @IndexSee{Term=[run-time check],See=(language-defined check)}
1879 @Defn{run-time error}
1880 @Defn2{Term=[error], Sec=(run-time)}
1881 A @i{language-defined check} (or simply, a @lquotes@;check@rquotes@;) is
1882 one of the situations defined by this International Standard that requires a check to
1883 be made at run time to determine whether some
1884 condition is true.
1885 @Defn2{Term=[failure],Sec=(of a language-defined check)}
1886 A check @i{fails} when the condition being checked is
1887 @Chg{Version=[3],New=[False],Old=[false]},
1888 causing an exception to be raised.
1889 @begin{Discussion}
1890 All such checks are defined under @lquotes@;@RunTimeTitle@rquotes@;
1891 in clauses and subclauses throughout the standard.
1892 @end{Discussion}
1893 @end{Intro}
1894
1895 @begin{Syntax}
1896 @begin{SyntaxText}
1897 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
1898 @Leading@Keepnext@;The form@Chg{Version=[2],New=[s of checking pragmas are],Old=[ of a @nt{pragma} Suppress is]} as follows:
1899 @end{SyntaxText}
1900
1901 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
1902 @PragmaSyn`@key{pragma} @prag(Suppress)(@Syn2{identifier}@Chg{Version=[2],New=<>,Old=( [, [On =>] @Syn2{name}])});'
1903
1904 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00224-01]}
1905 @AddedPragmaSyn<Version=[2],@ChgAdded{Version=[2],
1906 Text=<@key{pragma} @prag(Unsuppress)(@Syn2{identifier});>}>
1907
1908 @begin{SyntaxText}
1909 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
1910 @PDefn2{Term=[configuration pragma], Sec=(Suppress)}
1911 @PDefn2{Term=[pragma, configuration], Sec=(Suppress)}
1912 @Chg{Version=[2],New=[@PDefn2{Term=[configuration pragma], Sec=(Unsuppress)}
1913 @PDefn2{Term=[pragma, configuration], Sec=(Unsuppress)}],Old=[]}
1914 A @Chg{Version=[2],New=<checking pragma>,Old=<@nt{pragma} Suppress>} is
1915 allowed only immediately within a
1916 @nt{declarative_part}, immediately within a @nt{package_@!specification},
1917 or as a configuration pragma.
1918 @end{SyntaxText}
1919 @end{Syntax}
1920
1921 @begin{Legality}
1922 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
1923 The @nt{identifier} shall be the name of a check.
1924 @Chg{Version=[2],New=<>,Old=<The @nt{name} (if present) shall statically denote
1925 some entity.>}
1926
1927 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00224-01]}
1928 @ChgDeleted{Version=[2],Text=<For a @nt{pragma} Suppress that is immediately
1929 within a @nt{package_specification} and includes a @nt<name>, the @nt<name>
1930 shall denote an entity (or several overloaded subprograms) declared immediately
1931 within the @nt{package_specification}.>}
1932 @end{Legality}
1933
1934 @begin{StaticSem}
1935 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00224-01]}
1936 @ChgAdded{Version=[2],Text=[A checking pragma applies to the named check in a
1937 specific region,
1938 and applies to all entities in that region. A checking pragma given in a
1939 @nt<declarative_part> or immediately within a @nt<package_specification>
1940 applies from the place of the @nt<pragma> to the end of the innermost enclosing
1941 declarative region. The region for a checking pragma given as a configuration
1942 pragma is the declarative region for the entire compilation unit (or units) to
1943 which it applies.]}
1944
1945 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00224-01]}
1946 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0229-1],ARef=[AI05-0290-1]}
1947 @ChgAdded{Version=[2],Text=[If a checking pragma applies to a
1948 @Chg{Version=[3],New=[@nt{generic_instantiation}],Old=[generic instantiation]},
1949 then the checking
1950 pragma also applies to the @Chg{Version=[3],New=[entire ],Old=[]}instance.
1951 @Chg{Version=[3],New=[],Old=[ If a checking pragma applies to a call to
1952 a subprogram that has a @nt<pragma>
1953 Inline applied to it, then the checking
1954 pragma also applies to the inlined subprogram body.]}]}
1955
1956 @begin{Ramification}
1957 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0290-1]}
1958 @ChgAdded{Version=[3],Text=[This means that a Suppress pragma that occurs in a
1959 scope enclosing the declaration of a generic unit but not also enclosing the
1960 declaration of a given instance of that generic unit will not apply to
1961 constructs within the given instance.]}
1962 @end{Ramification}
1963
1964 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
1965 A @nt{pragma} Suppress gives permission to an implementation to omit the
1966 named check @Chg{Version=[2],New=[(or every check in the case of All_Checks) for any entities to which it
1967 applies.],Old=[from the place of the @nt{pragma} to the end of the
1968 innermost enclosing declarative region, or,
1969 if the @nt{pragma} is given in a @nt{package_@!specification}
1970 and includes a @nt<name>,
1971 to the end of the scope of the named entity.
1972 If the @nt{pragma} includes a @nt{name},
1973 the permission applies only to checks performed on the named entity,
1974 or, for a subtype, on objects and values of its type.
1975 Otherwise, the permission applies to all entities.]}
1976 @Defn{suppressed check}
1977 If permission has been given to suppress a given check,
1978 the check is said to be @i{suppressed}.
1979 @begin{Ramification}
1980 A check is suppressed even if the implementation chooses not to
1981 actually generate better code.
1982 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
1983 This allows the implementation to raise Program_Error,
1984 for example, if the erroneousness is detected.
1985 @end{Ramification}
1986
1987 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00224-01]}
1988 @ChgAdded{Version=[2],Text=[A @nt{pragma} Unsuppress revokes the permission
1989 to omit the named check (or every check in the case of All_Checks) given by any
1990 @nt{pragma} Suppress that applies at the point of the @nt{pragma} Unsuppress.
1991 The permission is revoked for the region to which the @nt{pragma} Unsuppress
1992 applies. If there is no such permission at the point of a @nt{pragma}
1993 Unsuppress, then the @nt{pragma} has no effect. A later @nt{pragma} Suppress
1994 can renew the permission.]}
1995
1996 @Leading@Keepnext@;The following are the language-defined checks:
1997 @begin{Itemize}
1998 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
1999 @Leading@Redundant[The following checks correspond to situations in which the
2000 exception Constraint_Error is raised upon failure.]
2001
2002 @begin{Hang2List}
2003 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0036],ARef=[AI95-00176-01]}
2004 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]}
2005 @RootDefn{Access_Check}
2006 Access_Check @\@Redundant[When evaluating a dereference (explicit
2007 or implicit),
2008 check that the value of the @nt{name} is not @key{null}.
2009 @Chg{Version=[2],New=[When converting to a subtype that excludes null,
2010 check that the converted value is not @key{null}.],
2011 Old=[When passing an actual parameter to a formal access parameter,
2012 check that the value of the actual parameter is not @key{null}.
2013 @Chg{Version=[1],New=[When evaluating a @nt{discriminant_association} for an
2014 access discriminant, check that the value of the discriminant is not @key{null}.],
2015 Old=[]}]}]
2016
2017 @RootDefn{Discriminant_Check}
2018 Discriminant_Check @\@Redundant[Check that the discriminants of a
2019 composite value
2020 have the values imposed by a discriminant constraint. Also, when
2021 accessing a record component, check that it exists for the current
2022 discriminant values.]
2023
2024 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00434-01]}
2025 @RootDefn{Division_Check}
2026 Division_Check @\@Redundant[Check that the second operand is not zero
2027 for the
2028 operations /, @Chg{Version=[2],New=[@key[rem]],Old=[rem]} and
2029 @Chg{Version=[2],New=[@key[mod]],Old=[mod]}.]
2030
2031 @RootDefn{Index_Check}
2032 Index_Check @\@Redundant[Check that the bounds of an array value are
2033 equal to the
2034 corresponding bounds of an index constraint. Also, when accessing a
2035 component of an array object, check for each dimension that the given
2036 index value belongs to the range defined by the bounds of the array
2037 object. Also, when accessing a slice of an array object, check that
2038 the given discrete range is compatible with the range defined by the
2039 bounds of the array object.]
2040
2041 @RootDefn{Length_Check}
2042 Length_Check @\@Redundant[Check that two arrays have matching
2043 components,
2044 in the case of array subtype conversions,
2045 and logical operators for arrays of boolean components.]
2046
2047 @RootDefn{Overflow_Check}
2048 Overflow_Check @\@Redundant[Check that a scalar value is within the
2049 base range of
2050 its type, in cases where the implementation chooses to raise an
2051 exception instead of returning the correct mathematical result.]
2052
2053 @RootDefn{Range_Check}
2054 Range_Check @\@Redundant[Check that a scalar value satisfies a range
2055 constraint.
2056 Also, for the elaboration of a @nt<subtype_indication>, check that
2057 the @nt<constraint> (if present) is compatible with the
2058 subtype denoted by the @nt{subtype_mark}.
2059 Also, for an @nt<aggregate>, check that an index or
2060 discriminant value belongs to the corresponding subtype. Also, check
2061 that when the result of an operation yields an array, the value of
2062 each component belongs to the component subtype.]
2063
2064 @RootDefn{Tag_Check}
2065 Tag_Check @\@Redundant[Check that operand tags in a dispatching call
2066 are all equal.
2067 Check for the correct tag on tagged type conversions,
2068 for an @nt{assignment_statement},
2069 and when returning a tagged limited object from a function.]
2070 @end{Hang2List}
2071
2072 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
2073 @Leading@Redundant[The following checks correspond to situations in which the
2074 exception Program_Error is raised upon failure.]
2075 @begin{Hang2List}
2076 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00280]}
2077 @ChgAdded{Version=[2],Text=[@RootDefn{Accessibility_Check}
2078 Accessibility_Check @\@Redundant[Check the accessibility level of an
2079 entity or view.]]}
2080
2081 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00280]}
2082 @ChgAdded{Version=[2],Text=[@RootDefn{Allocation_Check}
2083 Allocation_Check @\@Redundant[For an @nt<allocator>, check that the master of
2084 any tasks to be created by the @nt{allocator} is not yet completed or some
2085 dependents have not yet terminated, and that the finalization of the
2086 collection has not started.]]}
2087
2088 @RootDefn{Elaboration_Check}
2089 Elaboration_Check @\@Redundant[When a subprogram or protected entry is
2090 called, a task activation is accomplished,
2091 or a generic instantiation is elaborated, check that the body
2092 of the corresponding unit has already been elaborated.]
2093
2094 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00280]}
2095 @ChgNote{This item is not in alphabetical order}
2096 @ChgDeleted{Version=[2],Text=[@RootDefn{Accessibility_Check}
2097 Accessibility_Check @\@Redundant[Check the accessibility level of an
2098 entity or view.]]}
2099
2100 @end{Hang2List}
2101
2102 @Leading@Redundant[The following check corresponds to situations in which the
2103 exception Storage_Error is raised upon failure.]
2104 @begin{Hang2List}
2105 @RootDefn{Storage_Check}
2106 @Defn2{Term=[Storage_Error],Sec=(raised by failure of run-time check)}
2107 Storage_Check @\@Redundant[Check that evaluation of an @nt{allocator}
2108 does not require
2109 more space than is available for a storage pool. Check
2110 that the space available for a task or subprogram has
2111 not been exceeded.]
2112 @begin{Reason}
2113 We considered splitting this out into three categories:
2114 Pool_Check (for @nt{allocator}s), Stack_Check (for stack usage),
2115 and Heap_Check (for implicit use of the heap @em use of the heap
2116 other than through an @nt{allocator}).
2117 Storage_Check would then represent the union of these three.
2118 However, there seems to be no compelling reason to do this,
2119 given that it is not feasible to split Storage_Error.
2120 @end{Reason}
2121 @end{Hang2List}
2122
2123 @Leading@Redundant[The following check corresponds to all situations in which
2124 any predefined exception is raised.]
2125 @begin{Hang2List}
2126 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0290-1]}
2127 @RootDefn{All_Checks}
2128 All_Checks @\Represents the union of all checks;
2129 suppressing All_Checks suppresses all checks@Chg{Version=[3],New=[
2130 other than those associated with assertions. In addition,
2131 an implementation is allowed (but not required) to behave as if a
2132 pragma Assertion_Policy(Ignore) applies to any region to which
2133 pragma Suppress(All_Checks) applies],Old=[]}.
2134
2135 @begin{Ramification}
2136 All_Checks includes both language-defined and implementation-defined
2137 checks.
2138 @end{Ramification}
2139 @begin{Honest}
2140 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1]}
2141 @ChgAdded{Version=[3],Text=[There are additional checks defined in various
2142 Specialized Needs Annexes that are not listed here. Nevertheless, they are
2143 included in All_Checks and named in a Suppress pragma on implementations that
2144 support the relevant annex. Look up @ldquote@;check, language-defined@rdquote
2145 in the index to find the complete list.]}
2146 @end{Honest}
2147 @begin{Discussion}
2148 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0290-1]}
2149 @ChgAdded{Version=[3],Text=[We don't want to say that assertions are
2150 suppressed, because we don't want the potential failure of an assertion to
2151 cause erroneous execution (see below). Thus they are excluded from the
2152 suppression part of the above rule and then handled with an implicit Ignore
2153 policy.]}
2154 @end{Discussion}
2155 @end{Hang2List}
2156 @end{Itemize}
2157 @end{StaticSem}
2158
2159 @begin{Erron}
2160 @PDefn2{Term=(erroneous execution),Sec=(cause)}
2161 If a given check has been suppressed,
2162 and the corresponding error situation occurs,
2163 the execution of the program is erroneous.
2164 @end{Erron}
2165
2166 @begin{ImplPerm}
2167 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
2168 An implementation is allowed to place restrictions on
2169 @Chg{Version=[2],New=[checking pragmas, subject only to the requirement that
2170 @nt{pragma} Unsuppress shall allow any
2171 check names supported by @nt{pragma} Suppress],Old=[Suppress @nt<pragma>s]}.
2172 An implementation is allowed to add additional check names,
2173 with implementation-defined semantics.
2174 @PDefn{unspecified}
2175 When Overflow_Check has been suppressed,
2176 an implementation may also suppress an unspecified
2177 subset of the Range_Checks.
2178 @begin{Reason}
2179 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00224-01]}
2180 @ChgDeleted{Version=[2],Text=[The permission to restrict is given
2181 so the implementation can give an error message when the
2182 requested suppression is nonsense, such as suppressing a Range_Check
2183 on a task type.
2184 It would be verbose and pointless to list all the cases of
2185 nonsensical language-defined checks in the standard,
2186 and since the list of checks is open-ended,
2187 we can't list the restrictions for implementation-defined checks
2188 anyway.]}
2189 @end{Reason}
2190 @ImplDef{Implementation-defined check names.}
2191 @begin{Discussion}
2192 For Overflow_Check, the intention is that the
2193 implementation will suppress any Range_Checks that are implemented in
2194 the same manner as Overflow_Checks (unless they are free).
2195 @end{Discussion}
2196
2197 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00224-01]}
2198 @ChgAdded{Version=[2],Text=[An implementation may support an additional
2199 parameter on
2200 @nt{pragma} Unsuppress similar to the one allowed for @nt{pragma} Suppress
2201 (see @RefSecNum{Specific Suppression of Checks}). The meaning of
2202 such a parameter is implementation-defined.]}
2203 @ChgImplDef{Version=[2],Kind=[Added],Text=[@Chg{Version=[2],New=[Existence and
2204 meaning of second parameter of @nt{pragma} Unsuppress.],Old=[]}]}
2205 @end{ImplPerm}
2206
2207 @begin{ImplAdvice}
2208 The implementation should minimize the code executed for checks
2209 that have been suppressed.
2210 @ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],
2211 Text=[Code executed for checks
2212 that have been suppressed should be minimized.]}]}
2213 @begin{ImplNote}
2214 However, if a given check comes for free (for example, the hardware
2215 automatically performs the check in parallel with doing useful work)
2216 or nearly free (for example, the check is a tiny portion of an
2217 expensive run-time system call), the implementation should not bother to
2218 suppress the check. Similarly, if the implementation detects the failure at
2219 compile time and provides a warning message, there is no need to actually
2220 suppress the check.
2221 @end{ImplNote}
2222 @end{ImplAdvice}
2223
2224 @begin{Notes}
2225 @Defn{optimization}
2226 @Defn{efficiency}
2227 There is no guarantee that a suppressed check is actually removed;
2228 hence a @nt{pragma} Suppress should be used only for efficiency reasons.
2229
2230 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00224-01]}
2231 @ChgAdded{Version=[2],Text=[It is possible to give both a @nt{pragma} Suppress
2232 and Unsuppress for the same check immediately within the same
2233 @nt{declarative_part}.
2234 In that case, the last @nt{pragma} given determines whether or not the check is
2235 suppressed. Similarly, it is possible to resuppress a check which has been
2236 unsuppressed by giving a @nt{pragma} Suppress in an inner declarative region.]}
2237 @end{Notes}
2238
2239 @begin{Examples}
2240 @Leading@Keepnext@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
2241 @i{Examples of suppressing @Chg{Version=[2],New=[and unsuppressing ],Old=[]}checks:}
2242 @begin{Example}
2243 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00224-01]}
2244 @key[pragma] Suppress(@Chg{Version=[2],New=[Index_Check);
2245 @key[pragma] Unsuppress(Overflow_Check);],Old=[Range_Check);
2246 @key[pragma] Suppress(Index_Check, On => Table);]}
2247 @end{Example}
2248 @end{Examples}
2249
2250 @begin{Extend83}
2251 @Defn{extensions to Ada 83}
2252 A @nt{pragma} Suppress is allowed as a configuration pragma.
2253 A @nt{pragma} Suppress without a @nt<name> is allowed in a
2254 @nt{package_specification}.
2255
2256 Additional check names are added.
2257 We allow implementations to define their own checks.
2258 @end{Extend83}
2259
2260 @begin{DiffWord83}
2261 We define the checks in a distributed manner.
2262 Therefore, the long list of what checks apply to what
2263 is merely a NOTE.
2264
2265 We have removed the detailed rules about what is allowed
2266 in a @nt{pragma} Suppress,
2267 and allow implementations to invent their own.
2268 The RM83 rules weren't quite right,
2269 and such a change is necessary anyway in the presence
2270 of implementation-defined checks.
2271
2272 We make it clear that the difference between a Range_Check
2273 and an Overflow_Check is fuzzy.
2274 This was true in Ada 83,
2275 given RM83-11.6,
2276 but it was not clear.
2277 We considered removing Overflow_Check from the language
2278 or making it obsolescent, just as we did for Numeric_Error.
2279 However, we kept it for upward compatibility,
2280 and because it may be useful on machines where range checking
2281 costs more than overflow checking,
2282 but overflow checking still costs something.
2283 Different compilers will suppress different checks when asked to
2284 suppress Overflow_Check @em
2285 the nonuniformity in this case is not harmful,
2286 and removing it would have a serious impact on optimizers.
2287
2288 Under Access_Check,
2289 dereferences cover the cases of
2290 @nt{selected_component}, @nt{indexed_component}, @nt{slice},
2291 and attribute that are listed in RM83,
2292 as well as the new @nt{explicit_dereference},
2293 which was included in @nt{selected_component} in RM83.
2294 @end{DiffWord83}
2295
2296 @begin{Extend95}
2297 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00224-01]}
2298 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}Pragma Unsuppress is new.]}
2299
2300 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00280-01]}
2301 @ChgAdded{Version=[2],Text=[Allocation_Check was added to support suppressing
2302 the new check on @nt{allocator}s (see @RefSecNum{Allocators}).]}
2303 @end{Extend95}
2304
2305 @begin{DiffWord95}
2306 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0036],ARef=[AI95-00176-01],ARef=[AI95-00224-01]}
2307 @ChgAdded{Version=[2],Text=[The description of Access_Check was corrected by the
2308 Corrigendum to include the discriminant case. This change was then replaced
2309 by the more general notion of checking conversions to subtypes that exclude
2310 null in Ada 2005.]}
2311
2312 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00224-01]}
2313 @ChgAdded{Version=[2],Text=[The On parameter of pragma Suppress was moved to
2314 Annex J (see @RefSecNum{Specific Suppression of Checks}).
2315 This feature's effect is inherently nonportable, depending on the
2316 implementation's model of computation. Compiler surveys demonstrated this,
2317 showing that implementations vary widely in the interpretation of these
2318 parameters, even on the same target. While this is relatively harmless for
2319 Suppress (which is never required to do anything), it would be a significant
2320 problem for Unsuppress (we want the checks to be made for all implementations).
2321 By moving it, we avoid needing to define the meaning of Unsuppress with an
2322 On parameter.]}
2323
2324 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00280-01]}
2325 @ChgAdded{Version=[2],Text=[The order of the
2326 Program_Error checks was corrected to be alphabetical.]}
2327 @end{DiffWord95}
2328
2329 @begin{DiffWord2005}
2330 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0290-1]}
2331 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0005-1]}
2332 @ChgAdded{Version=[3],Text=[The effect of a checking pragma no longer
2333 applies inside an inlined subprogram body. While this could change the
2334 behavior of a program that depends on a check being suppressed in an
2335 inlined body, such a program is erroneous and thus no behavior can be
2336 depended upon anyway. It's also likely to be very rare. We make this
2337 change so that inlining has no effect on the meaning of the subprogram
2338 body (since inlining is never @Chg{Version=[4],New=[required],Old=[requiring]},
2339 this is necessary in order to be
2340 able to reason about the body), and so that assertion policies and
2341 suppress work the same way for inlining.]}
2342 @end{DiffWord2005}
2343
2344
2345 @RMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
2346 @LabeledClause{Exceptions and Optimization}
2347
2348 @begin{Intro}
2349 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2350 @redundant[@Defn{language-defined check}
2351 @Defn2{Term=[check], Sec=(language-defined)}
2352 @Defn{run-time error}
2353 @Defn2{Term=[error], Sec=(run-time)}
2354 @Defn{optimization}
2355 @Defn{efficiency}
2356 This @Chg{Version=[3],New=[subclause],Old=[clause]} gives permission to
2357 the implementation to perform certain @lquotes@;optimizations@rquotes@; that
2358 do not necessarily preserve the canonical semantics.]
2359 @end{Intro}
2360
2361 @begin{RunTime}
2362 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2363 @Defn{canonical semantics}
2364 The rest of this International Standard (outside this @Chg{Version=[3],New=[subclause],Old=[clause]})
2365 defines the @i{canonical semantics} of the language.
2366 @Redundant[The canonical semantics of a given (legal) program determines a
2367 set of possible external effects that can result from the execution of
2368 the program with given inputs.]
2369 @begin{Ramification}
2370 Note that the canonical semantics is a set of possible behaviors,
2371 since some reordering, parallelism, and nondeterminism is allowed by
2372 the canonical semantics.
2373 @end{Ramification}
2374 @begin{Discussion}
2375 @Leading@ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2376 The following parts of the canonical semantics are of particular
2377 interest to the reader of this @Chg{Version=[3],New=[subclause],Old=[clause]}:
2378 @begin{Itemize}
2379 Behavior in the presence of abnormal objects
2380 and objects with invalid representations
2381 (see @RefSecNum{Data Validity}).
2382
2383 Various actions that are defined to occur in an arbitrary order.
2384
2385 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2386 Behavior in the presence of a misuse of Unchecked_Deallocation,
2387 Unchecked_Access, or imported or exported entity
2388 (see @Chg{Version=[3],New=[Clause],Old=[Section]}
2389 @RefSecNum{Representation Issues}).
2390 @end{Itemize}
2391 @end{Discussion}
2392
2393 @Leading@ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2394 @Redundant[As explained in
2395 @RefSec{Conformity of an Implementation with the Standard},
2396 the external effect of a program is defined in terms of its
2397 interactions with its external environment.
2398 Hence, the implementation can perform any internal
2399 actions whatsoever, in any order or in parallel,
2400 so long as the external effect of the execution of the program is one
2401 that is allowed by the canonical semantics, or by the rules
2402 of this @Chg{Version=[3],New=[subclause],Old=[clause]}.]
2403 @begin{Ramification}
2404 Note that an optimization can change the external effect of the program,
2405 so long as the changed external effect is an external effect that is
2406 allowed by the semantics.
2407 Note that the canonical semantics of an erroneous execution allows any
2408 external effect whatsoever.
2409 Hence, if the implementation can prove that program execution will be
2410 erroneous in certain circumstances,
2411 there need not be any constraints on the machine code executed in those
2412 circumstances.
2413 @end{Ramification}
2414 @end{RunTime}
2415
2416 @begin{ImplPerm}
2417 @Leading@;The following additional permissions are granted to the
2418 implementation:
2419 @begin{Itemize}
2420 @Defn{extra permission to avoid raising exceptions}
2421 @Defn{undefined result}
2422 An implementation need not always
2423 raise an exception when a language-defined check fails.
2424 Instead, the operation that failed the check can simply yield
2425 an @i{undefined result}. The exception need be raised
2426 by the implementation only if, in the absence of raising it,
2427 the value of this undefined result would have some effect on
2428 the external interactions of the program.
2429 In determining this, the implementation shall not presume that
2430 an undefined result has a value that belongs to its subtype,
2431 nor even to the base range of its type, if scalar.
2432 @Redundant[Having removed the raise of the exception, the
2433 canonical semantics will in general allow the implementation
2434 to omit the code for the check, and some or all of the operation itself.]
2435 @begin{Ramification}
2436 Even without this permission, an implementation can always
2437 remove a check if it cannot possibly fail.
2438 @end{Ramification}
2439 @begin{Reason}
2440 We express the permission in terms of removing
2441 the raise, rather than the operation or the check,
2442 as it minimizes the disturbance to the canonical
2443 semantics (thereby simplifying reasoning). By allowing
2444 the implementation to omit the raise, it thereby does
2445 not need to "look" at what happens in the exception handler
2446 to decide whether the optimization is allowed.
2447 @end{Reason}
2448 @begin{Discussion}
2449 The implementation can also omit checks
2450 if they cannot possibly fail,
2451 or if they could only fail in erroneous executions.
2452 This follows from the canonical semantics.
2453 @end{Discussion}
2454 @begin{ImplNote}
2455 @Leading@;This permission is intended to allow normal "dead code removal"
2456 optimizations, even if some of the removed code might have failed
2457 some language-defined check.
2458 However, one may not eliminate the raise of an exception
2459 if subsequent code presumes in some way that the check succeeded.
2460 For example:
2461 @begin{Example}
2462 @key[if] X * Y > Integer'Last @key[then]
2463 Put_Line("X * Y overflowed");
2464 @key[end] @key[if];
2465 @key[exception]
2466 @key[when] @key[others] =>
2467 Put_Line("X * Y overflowed");
2468 @end{Example}
2469 @ChgNote{The following paragraph is missing a number in the original version.
2470 To give it a number in the new version, it is marked as an insertion.}
2471 @ChgRef{Version=[0],Kind=[Added]}
2472 @Chg{New=[],Old=[@Noparanum@;]}If X*Y does overflow, you may not remove the raise of the exception
2473 if the code that does the comparison against Integer'Last presumes
2474 that it is comparing it with an in-range Integer value, and hence
2475 always yields False.
2476
2477 @Leading@;As another example where a raise may not be eliminated:
2478 @begin{Example}
2479 @key[subtype] Str10 @key[is] String(1..10);
2480 @key[type] P10 @key[is] @key[access] Str10;
2481 X : P10 := @key[null];
2482 @key[begin]
2483 @key[if] X.all'Last = 10 @key[then]
2484 Put_Line("Oops");
2485 @key[end] @key[if];
2486 @end{Example}
2487 @ChgNote{The following paragraph is missing a number in the original version.
2488 To give it a number in the new version, it is marked as an insertion.}
2489 @ChgRef{Version=[0],Kind=[Added]}
2490 In the above code, it would be wrong to eliminate the
2491 raise of Constraint_Error on the "X.all" (since X is null),
2492 if the code to evaluate 'Last always yields 10 by presuming
2493 that X.all belongs to the subtype Str10, without even "looking."
2494 @end{ImplNote}
2495
2496 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
2497 @Defn{extra permission to reorder actions}
2498 If an exception is raised due to the failure
2499 of a language-defined check,
2500 then upon
2501 reaching the corresponding @nt<exception_handler> (or the
2502 termination of the task, if none), the external interactions
2503 that have occurred need reflect only that the exception was
2504 raised somewhere within
2505 the execution of the @nt<sequence_of_statements> with the handler
2506 (or the @nt<task_body>), possibly earlier (or later
2507 if the interactions are independent of the result of the checked
2508 operation)
2509 than that defined
2510 by the canonical semantics, but not within the execution of some
2511 abort-deferred operation or @i(independent) subprogram
2512 that does not dynamically enclose the execution of the construct
2513 whose check failed.
2514 @Defn{independent subprogram}
2515 An independent subprogram is one that is
2516 defined outside the library unit containing the construct whose check
2517 failed, and @Chg{Version=[3],New=[for which the],Old=[has no]} Inline
2518 @Chg{Version=[3],New=[aspect is False],Old=[@nt<pragma> applied to it]}.
2519 @Defn{normal state of an object}
2520 @PDefn{abnormal state of an object}
2521 @PDefn{disruption of an assignment}
2522 Any assignment that occurred outside of such abort-deferred
2523 operations or independent subprograms can be disrupted
2524 by the raising of the exception,
2525 causing the object or its parts to become abnormal,
2526 and certain subsequent uses of the object to be erroneous,
2527 as explained in @RefSecNum{Data Validity}.
2528 @begin{Reason}
2529 We allow such variables to become abnormal so that
2530 assignments (other than to atomic variables) can be disrupted
2531 due to @lquotes@;imprecise@rquotes@; exceptions or instruction scheduling,
2532 and so that assignments can be reordered
2533 so long as the correct results are produced in the end if
2534 no language-defined checks fail.
2535 @end{Reason}
2536 @begin{Ramification}
2537 If a check fails, no result dependent on the check may be
2538 incorporated in an external interaction. In other words,
2539 there is no permission to output meaningless results due
2540 to postponing a check.
2541 @end{Ramification}
2542 @end{Itemize}
2543 @begin{Discussion}
2544 We believe it is important to state the
2545 extra permission to reorder actions
2546 in terms of what the programmer can expect at run time,
2547 rather than in terms of what the implementation can assume,
2548 or what transformations the implementation can perform.
2549 Otherwise, how can the programmer write reliable programs?
2550
2551 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2552 This @Chg{Version=[3],New=[subclause],Old=[clause]} has two conflicting goals:
2553 to allow as much optimization as possible,
2554 and to make program execution as predictable as possible
2555 (to ease the writing of reliable programs).
2556 The rules given above represent a compromise.
2557
2558 Consider the two extremes:
2559
2560 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2561 The extreme conservative rule would be
2562 to delete this @Chg{Version=[3],New=[subclause],Old=[clause]} entirely.
2563 The semantics of Ada would be the canonical semantics.
2564 This achieves the best predictability.
2565 It sounds like a disaster from the efficiency point of view,
2566 but in practice, implementations would provide modes
2567 in which less predictability but more efficiency would be achieved.
2568 Such a mode could even be the out-of-the-box mode.
2569 In practice, implementers would provide a compromise based on their
2570 customer's needs.
2571 Therefore, we view this as one viable alternative.
2572
2573 The extreme liberal rule would be @lquotes@;the language does not specify the
2574 execution of a program once a language-defined check has failed;
2575 such execution can be unpredictable.@rquotes@;
2576 This achieves the best efficiency.
2577 It sounds like a disaster from the predictability point of view,
2578 but in practice it might not be so bad.
2579 A user would have to assume that exception handlers for exceptions
2580 raised by language-defined checks are not portable.
2581 They would have to isolate such code (like all nonportable code),
2582 and would have to find out, for each implementation of interest,
2583 what behaviors can be expected.
2584 In practice, implementations would tend to avoid going so far as to
2585 punish their customers too much in terms of predictability.
2586
2587 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2588 The most important thing about this @Chg{Version=[3],New=[subclause],Old=[clause]}
2589 is that users understand what they can expect at run time,
2590 and implementers understand what optimizations are allowed.
2591 Any solution that makes this @Chg{Version=[3],New=[subclause],Old=[clause]}
2592 contain rules that can interpreted in more than one way is unacceptable.
2593
2594 We have chosen a compromise between the extreme conservative and
2595 extreme liberal rules.
2596 The current rule essentially allows arbitrary optimizations within
2597 a library unit and inlined subprograms reachable from it, but
2598 disallow semantics-disrupting optimizations across library units
2599 in the absence of inlined subprograms.
2600 This allows a library unit to be debugged, and then reused with
2601 some confidence that the abstraction it manages cannot be
2602 broken by bugs outside the library unit.
2603 @end{Discussion}
2604 @end{ImplPerm}
2605
2606 @begin{Notes}
2607 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2608 The permissions granted by this @Chg{Version=[3],New=[subclause],Old=[clause]}
2609 can have an effect on the
2610 semantics of a program only if the program fails a language-defined check.
2611 @end{Notes}
2612
2613 @begin{DiffWord83}
2614 @Leading@;RM83-11.6 was unclear.
2615 It has been completely rewritten here; we hope this version is clearer.
2616 Here's what happened to each paragraph of RM83-11.6:
2617 @begin{Itemize}
2618 Paragraphs 1 and 2 contain no semantics;
2619 they are merely pointing out that anything goes if the canonical
2620 semantics is preserved.
2621 We have similar introductory paragraphs,
2622 but we have tried to clarify that these are not granting any @lquotes@;extra@rquotes@;
2623 permission beyond what the rest of the document allows.
2624
2625 Paragraphs 3 and 4 are reflected in the
2626 @lquotes@;extra permission to reorder actions@rquotes@;.
2627 Note that this permission now allows the reordering of assignments in
2628 many cases.
2629
2630 Paragraph 5 is moved to @RefSec{Operators and Expression Evaluation},
2631 where operator association is discussed.
2632 Hence, this is no longer an @lquotes@;extra permission@rquotes@;
2633 but is part of the canonical semantics.
2634
2635 Paragraph 6 now follows from the general permission to store
2636 out-of-range values for unconstrained subtypes.
2637 Note that the parameters and results of all the predefined operators
2638 of a type are of the unconstrained subtype of the type.
2639
2640 Paragraph 7 is reflected in the
2641 @lquotes@;extra permission to avoid raising exceptions@rquotes@;.
2642 @end{Itemize}
2643
2644 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2645 We moved @Chg{Version=[3],New=[subclause],Old=[clause]}
2646 @RefSec{Suppressing Checks} from after 11.6 to
2647 before 11.6, in order to preserve the famous number @lquotes@;11.6@rquotes@;
2648 (given the changes to earlier @Chg{Version=[3],New=[subclauses],Old=[clauses]} in
2649 @Chg{Version=[3],New=[Clause],Old=[Section]} @RefSecNum{Exceptions}).
2650 @end{DiffWord83}