Debian Codegraph @

source_2020/11.mss

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