source: doc/theses/andrew_beach_MMath/features.tex @ e3984a68

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since e3984a68 was e3984a68, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Andrew MMath: Added in Peter's feedback to the feature's chapter.

  • Property mode set to 100644
File size: 34.6 KB
Line 
1\chapter{Exception Features}
2\label{c:features}
3
4This chapter covers the design and user interface of the \CFA EHM
5and begins with a general overview of EHMs. It is not a strict
6definition of all EHMs nor an exhaustive list of all possible features.
7However it does cover the most common structure and features found in them.
8
9\section{Overview of EHMs}
10% We should cover what is an exception handling mechanism and what is an
11% exception before this. Probably in the introduction. Some of this could
12% move there.
13\subsection{Raise / Handle}
14An exception operation has two main parts: raise and handle.
15These terms are sometimes known as throw and catch but this work uses
16throw/catch as a particular kind of raise/handle.
17These are the two parts that the user writes and may
18be the only two pieces of the EHM that have any syntax in a language.
19
20\paragraph{Raise}
21The raise is the starting point for exception handling,
22by raising an exception, which passes it to
23the EHM.
24
25Some well known examples include the @throw@ statements of \Cpp and Java and
26the \code{Python}{raise} statement of Python. In real systems, a raise may
27perform some other work (such as memory management) but for the
28purposes of this overview that can be ignored.
29
30\paragraph{Handle}
31The primary purpose of an EHM is to run some user code to handle a raised
32exception. This code is given, along with some other information,
33in a handler.
34
35A handler has three common features: the previously mentioned user code, a
36region of code it guards and an exception label/condition that matches
37against the raised exception.
38Only raises inside the guarded region and raising exceptions that match the
39label can be handled by a given handler.
40If multiple handlers could can handle an exception,
41EHMs define a rule to pick one, such as ``best match" or ``first found".
42
43The @try@ statements of \Cpp, Java and Python are common examples. All three
44also show another common feature of handlers, they are grouped by the guarded
45region.
46
47\subsection{Propagation}
48After an exception is raised comes what is usually the biggest step for the
49EHM: finding and setting up the handler for execution.
50The propagation from raise to
51handler can be broken up into three different tasks: searching for a handler,
52matching against the handler and installing the handler.
53
54\paragraph{Searching}
55The EHM begins by searching for handlers that might be used to handle
56the exception.
57The search will find handlers that have the raise site in their guarded
58region.
59The search includes handlers in the current function, as well as any in
60callers on the stack that have the function call in their guarded region.
61
62\paragraph{Matching}
63Each handler found is with the raised exception. The exception
64label defines a condition that is used with the exception and decides if
65there is a match or not.
66%
67In languages where the first match is used, this step is intertwined with
68searching; a match check is performed immediately after the search finds
69a handler.
70
71\paragraph{Installing}
72After a handler is chosen, it must be made ready to run.
73The implementation can vary widely to fit with the rest of the
74design of the EHM. The installation step might be trivial or it could be
75the most expensive step in handling an exception. The latter tends to be the
76case when stack unwinding is involved.
77
78If a matching handler is not guaranteed to be found, the EHM needs a
79different course of action for this case.
80This situation only occurs with unchecked exceptions as checked exceptions
81(such as in Java) can make the guarantee.
82The unhandled action is usually very general, such as aborting the program.
83
84\paragraph{Hierarchy}
85A common way to organize exceptions is in a hierarchical structure.
86This pattern comes from object-orientated languages where the
87exception hierarchy is a natural extension of the object hierarchy.
88
89Consider the following exception hierarchy:
90\begin{center}
91\input{exception-hierarchy}
92\end{center}
93A handler labeled with any given exception can handle exceptions of that
94type or any child type of that exception. The root of the exception hierarchy
95(here \code{C}{exception}) acts as a catch-all, leaf types catch single types
96and the exceptions in the middle can be used to catch different groups of
97related exceptions.
98
99This system has some notable advantages, such as multiple levels of grouping,
100the ability for libraries to add new exception types and the isolation
101between different sub-hierarchies.
102This design is used in \CFA even though it is not a object-orientated
103language; so different tools are used to create the hierarchy.
104
105% Could I cite the rational for the Python IO exception rework?
106
107\subsection{Completion}
108After the handler has finished, the entire exception operation has to complete
109and continue executing somewhere else. This step is usually simple,
110both logically and in its implementation, as the installation of the handler
111is usually set up to do most of the work.
112
113The EHM can return control to many different places, where
114the most common are after the handler definition (termination)
115and after the raise (resumption).
116
117\subsection{Communication}
118For effective exception handling, additional information is often passed
119from the raise to the handler and back again.
120So far, only communication of the exceptions' identity is covered.
121A common communication method for adding information to an exception
122is putting fields into the exception instance
123and giving the handler access to them.
124% You can either have pointers/references in the exception, or have p/rs to
125% the exception when it doesn't have to be copied.
126Passing references or pointers allows data at the raise location to be
127updated, passing information in both directions.
128
129\section{Virtuals}
130\label{s:virtuals}
131Virtual types and casts are not part of \CFA's EHM nor are they required for
132an EHM.
133However, one of the best ways to support an exception hierarchy
134is via a virtual hierarchy and dispatch system.
135Ideally, the virtual system would have been part of \CFA before the work
136on exception handling began, but unfortunately it was not.
137Hence, only the features and framework needed for the EHM were
138designed and implemented for this thesis.
139Other features were considered to ensure that
140the structure could accommodate other desirable features in the future
141but are not implemented.
142The rest of this section only discusses the implemented subset of the
143virtual system design.
144
145The virtual system supports multiple ``trees" of types. Each tree is
146a simple hierarchy with a single root type. Each type in a tree has exactly
147one parent -- except for the root type which has zero parents -- and any
148number of children.
149Any type that belongs to any of these trees is called a virtual type.
150% A type's ancestors are its parent and its parent's ancestors.
151% The root type has no ancestors.
152% A type's descendants are its children and its children's descendants.
153
154For the purposes of illistration, a proposed -- but unimplemented syntax --
155will be used. Each virtual type is repersented by a trait with an annotation
156that makes it a virtual type. This annotation is empty for a root type, which
157creates a new tree:
158\begin{cfa}
159trait root_type(T) virtual() {}
160\end{cfa}
161The annotation may also refer to any existing virtual type to make this new
162type a child of that type and part of the same tree. The parent may itself
163be a child or a root type and may have any number of existing children.
164\begin{cfa}
165trait child_a(T) virtual(root_type) {}
166trait grandchild(T) virtual(child_a) {}
167trait child_b(T) virtual(root_type) {}
168\end{cfa}
169\todo{Update the diagram in vtable.fig to show the new type tree.}
170
171Every virtual type also has a list of virtual members and a unique id,
172both are stored in a virtual table.
173Every instance of a virtual type also has a pointer to a virtual table stored
174in it, although there is no per-type virtual table as in many other languages.
175
176The list of virtual members is built up down the tree. Every virtual type
177inherits the list of virtual members from its parent and may add more
178virtual members to the end of the list which are passed on to its children.
179Again, using the unimplemented syntax this might look like:
180\begin{cfa}
181trait root_type(T) virtual() {
182        const char * to_string(T const & this);
183        unsigned int size;
184}
185
186trait child_type(T) virtual(root_type) {
187        char * irrelevant_function(int, char);
188}
189\end{cfa}
190% Consider adding a diagram, but we might be good with the explanation.
191
192As @child_type@ is a child of @root_type@ it has the virtual members of
193@root_type@ (@to_string@ and @size@) as well as the one it declared
194(@irrelivant_function@).
195
196It is important to note that these are virtual members, and may contain   
197arbitrary fields, functions or otherwise.
198The names ``size" and ``align" are reserved for the size and alignment of the
199virtual type, and are always automatically initialized as such.
200The other special case are uses of the trait's polymorphic argument
201(@T@ in the example), which are always updated to refer to the current
202virtual type. This allows functions that refer to to polymorphic argument
203to act as traditional virtual methods (@to_string@ in the example), as the
204object can always be passed to a virtual method in its virtual table.
205
206Up until this point the virtual system is similar to ones found in
207object-oriented languages but this is where \CFA diverges.
208Objects encapsulate a single set of methods in each type,
209universally across the entire program,
210and indeed all programs that use that type definition.
211The only way to change any method is to inherit and define a new type with
212its own universal implementation. In this sense,
213these object-oriented types are ``closed" and cannot be altered.
214% Because really they are class oriented.
215
216In \CFA, types do not encapsulate any code.
217Whether or not satisfies any given assertion, and hence any trait, is
218context sensitive. Types can begin to satisfy a trait, stop satisfying it or
219satisfy the same trait at any lexical location in the program.
220In this sense, an type's implementation in the set of functions and variables
221that allow it to satisfy a trait is ``open" and can change
222throughout the program.
223This capability means it is impossible to pick a single set of functions
224that represent a type's implementation across a program.
225
226\CFA side-steps this issue by not having a single virtual table for each
227type. A user can define virtual tables that are filled in at their
228declaration and given a name. Anywhere that name is visible, even if it is
229defined locally inside a function (although in this case the user must ensure
230it outlives any objects that use it), it can be used.
231Specifically, a virtual type is ``bound" to a virtual table that
232sets the virtual members for that object. The virtual members can be accessed
233through the object.
234
235While much of the virtual infrastructure is created, it is currently only used
236internally for exception handling. The only user-level feature is the virtual
237cast, which is the same as the \Cpp \code{C++}{dynamic_cast}.
238\label{p:VirtualCast}
239\begin{cfa}
240(virtual TYPE)EXPRESSION
241\end{cfa}
242Note, the syntax and semantics matches a C-cast, rather than the function-like
243\Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be
244a pointer to a virtual type.
245The cast dynamically checks if the @EXPRESSION@ type is the same or a sub-type
246of @TYPE@, and if true, returns a pointer to the
247@EXPRESSION@ object, otherwise it returns @0p@ (null pointer).
248
249\section{Exception}
250% Leaving until later, hopefully it can talk about actual syntax instead
251% of my many strange macros. Syntax aside I will also have to talk about the
252% features all exceptions support.
253
254Exceptions are defined by the trait system; there are a series of traits, and
255if a type satisfies them, then it can be used as an exception. The following
256is the base trait all exceptions need to match.
257\begin{cfa}
258trait is_exception(exceptT &, virtualT &) {
259        // Numerous imaginary assertions.
260};
261\end{cfa}
262The trait is defined over two types, the exception type and the virtual table
263type. Each exception type should have a single virtual table type.
264There are no actual assertions in this trait because the trait system
265cannot express them yet (adding such assertions would be part of
266completing the virtual system). The imaginary assertions would probably come
267from a trait defined by the virtual system, and state that the exception type
268is a virtual type, is a descendant of @exception_t@ (the base exception type)
269and allow the user to find the virtual table type.
270
271% I did have a note about how it is the programmer's responsibility to make
272% sure the function is implemented correctly. But this is true of every
273% similar system I know of (except Agda's I guess) so I took it out.
274
275There are two more traits for exceptions defined as follows:
276\begin{cfa}
277trait is_termination_exception(
278                exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
279        void defaultTerminationHandler(exceptT &);
280};
281
282trait is_resumption_exception(
283                exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
284        void defaultResumptionHandler(exceptT &);
285};
286\end{cfa}
287Both traits ensure a pair of types are an exception type, its virtual table
288type
289and defines one of the two default handlers. The default handlers are used
290as fallbacks and are discussed in detail in \vref{s:ExceptionHandling}.
291
292However, all three of these traits can be tricky to use directly.
293While there is a bit of repetition required,
294the largest issue is that the virtual table type is mangled and not in a user
295facing way. So these three macros are provided to wrap these traits to
296simplify referring to the names:
297@IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@.
298
299All three take one or two arguments. The first argument is the name of the
300exception type. The macro passes its unmangled and mangled form to the trait.
301The second (optional) argument is a parenthesized list of polymorphic
302arguments. This argument is only used with polymorphic exceptions and the
303list is be passed to both types.
304In the current set-up, the two types always have the same polymorphic
305arguments so these macros can be used without losing flexibility.
306
307For example consider a function that is polymorphic over types that have a
308defined arithmetic exception:
309\begin{cfa}
310forall(Num | IS_EXCEPTION(Arithmetic, (Num)))
311void some_math_function(Num & left, Num & right);
312\end{cfa}
313
314\section{Exception Handling}
315\label{s:ExceptionHandling}
316As stated,
317\CFA provides two kinds of exception handling: termination and resumption.
318These twin operations are the core of \CFA's exception handling mechanism.
319This section covers the general patterns shared by the two operations and
320then goes on to cover the details each individual operation.
321
322Both operations follow the same set of steps.
323First, a user raises an exception.
324Second, the exception propagates up the stack, searching for a handler.
325Third, if a handler is found, the exception is caught and the handler is run.
326After that control continues at a raise-dependent location.
327As an alternate to the third step,
328if a handler is not found, a default handler is run and, if it returns,
329then control
330continues after the raise.
331
332The differences between the two operations include how propagation is
333performed, where excecution after an exception is handler
334and which default handler is run.
335
336\subsection{Termination}
337\label{s:Termination}
338Termination handling is the familiar kind of handling
339and used in most programming
340languages with exception handling.
341It is a dynamic, non-local goto. If the raised exception is matched and
342handled, the stack is unwound and control (usually) continues in the function
343on the call stack that defined the handler.
344Termination is commonly used when an error has occurred and recovery is
345impossible locally.
346
347% (usually) Control can continue in the current function but then a different
348% control flow construct should be used.
349
350A termination raise is started with the @throw@ statement:
351\begin{cfa}
352throw EXPRESSION;
353\end{cfa}
354The expression must return a reference to a termination exception, where the
355termination exception is any type that satisfies the trait
356@is_termination_exception@ at the call site.
357Through \CFA's trait system, the trait functions are implicitly passed into the
358throw code for use by the EHM.
359A new @defaultTerminationHandler@ can be defined in any scope to
360change the throw's behaviour when a handler is not found (see below).
361
362The throw copies the provided exception into managed memory to ensure
363the exception is not destroyed if the stack is unwound.
364It is the user's responsibility to ensure the original exception is cleaned
365up whether the stack is unwound or not. Allocating it on the stack is
366usually sufficient.
367
368% How to say propagation starts, its first sub-step is the search.
369Then propagation starts with the search. \CFA uses a ``first match" rule so
370matching is performed with the copied exception as the search key.
371It starts from the raise site and proceeds towards base of the stack,
372from callee to caller.
373At each stack frame, a check is made for termination handlers defined by the
374@catch@ clauses of a @try@ statement.
375\begin{cfa}
376try {
377        GUARDED_BLOCK
378} catch (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) {
379        HANDLER_BLOCK$\(_1\)$
380} catch (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) {
381        HANDLER_BLOCK$\(_2\)$
382}
383\end{cfa}
384When viewed on its own, a try statement simply executes the statements
385in the \snake{GUARDED_BLOCK} and when those are finished,
386the try statement finishes.
387
388However, while the guarded statements are being executed, including any
389invoked functions, all the handlers in these statements are included in the
390search path.
391Hence, if a termination exception is raised, these handlers may be matched
392against the exception and may handle it.
393
394Exception matching checks the handler in each catch clause in the order
395they appear, top to bottom. If the representation of the raised exception type
396is the same or a descendant of @EXCEPTION_TYPE@$_i$, then @NAME@$_i$
397(if provided) is
398bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$
399are executed. If control reaches the end of the handler, the exception is
400freed and control continues after the try statement.
401
402If no termination handler is found during the search, then the default handler
403(\defaultTerminationHandler) visible at the raise statement is called.
404Through \CFA's trait system the best match at the raise statement is used.
405This function is run and is passed the copied exception.
406If the default handler finishes, control continues after the raise statement.
407
408There is a global @defaultTerminationHandler@ that is polymorphic over all
409termination exception types.
410The global default termination handler performs a cancellation
411(as described in \vref{s:Cancellation})
412on the current stack with the copied exception.
413Since it is so general, a more specific handler can be defined,
414overriding the default behaviour for the specific exception types.
415
416\subsection{Resumption}
417\label{s:Resumption}
418
419Resumption exception handling is less familar form of exception handling,
420but is
421just as old~\cite{Goodenough75} and is simpler in many ways.
422It is a dynamic, non-local function call. If the raised exception is
423matched, a closure is taken from up the stack and executed,
424after which the raising function continues executing.
425The common uses for resumption exceptions include
426potentially repairable errors, where execution can continue in the same
427function once the error is corrected, and
428ignorable events, such as logging where nothing needs to happen and control
429should always continue from the raise site.
430
431Except for the changes to fit into that pattern, resumption exception
432handling is symmetric with termination exception handling, by design
433(see \autoref{s:Termination}).
434
435A resumption raise is started with the @throwResume@ statement:
436\begin{cfa}
437throwResume EXPRESSION;
438\end{cfa}
439\todo{Decide on a final set of keywords and use them everywhere.}
440It works much the same way as the termination raise, except the
441type must satisfy the \snake{is_resumption_exception} that uses the
442default handler: \defaultResumptionHandler.
443This can be specialized for particular exception types.
444
445At run-time, no exception copy is made. Since
446resumption does not unwind the stack nor otherwise remove values from the
447current scope, there is no need to manage memory to keep the exception
448allocated.
449
450Then propagation starts with the search,
451following the same search path as termination,
452from the raise site to the base of stack and top of try statement to bottom.
453However, the handlers on try statements are defined by @catchResume@ clauses.
454\begin{cfa}
455try {
456        GUARDED_BLOCK
457} catchResume (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) {
458        HANDLER_BLOCK$\(_1\)$
459} catchResume (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) {
460        HANDLER_BLOCK$\(_2\)$
461}
462\end{cfa}
463Note that termination handlers and resumption handlers may be used together
464in a single try statement, intermixing @catch@ and @catchResume@ freely.
465Each type of handler only interacts with exceptions from the matching
466kind of raise.
467Like @catch@ clauses, @catchResume@ clauses have no effect if an exception
468is not raised.
469
470The matching rules are exactly the same as well.
471The first major difference here is that after
472@EXCEPTION_TYPE@$_i$ is matched and @NAME@$_i$ is bound to the exception,
473@HANDLER_BLOCK@$_i$ is executed right away without first unwinding the stack.
474After the block has finished running control jumps to the raise site, where
475the just handled exception came from, and continues executing after it,
476not after the try statement.
477
478\subsubsection{Resumption Marking}
479\label{s:ResumptionMarking}
480A key difference between resumption and termination is that resumption does
481not unwind the stack. A side effect is that, when a handler is matched
482and run, its try block (the guarded statements) and every try statement
483searched before it are still on the stack. There presence can lead to
484the recursive resumption problem.
485\todo{Is there a citation for the recursive resumption problem?}
486
487The recursive resumption problem is any situation where a resumption handler
488ends up being called while it is running.
489Consider a trivial case:
490\begin{cfa}
491try {
492        throwResume (E &){};
493} catchResume(E *) {
494        throwResume (E &){};
495}
496\end{cfa}
497When this code is executed, the guarded @throwResume@ starts a
498search and matches the handler in the @catchResume@ clause. This
499call is placed on the stack above the try-block.
500Now the second raise in the handler searches the same try block,
501matches again and then puts another instance of the
502same handler on the stack leading to infinite recursion.
503
504While this situation is trivial and easy to avoid, much more complex cycles
505can form with multiple handlers and different exception types.
506To prevent all of these cases, each try statement is ``marked" from the
507time the exception search reaches it to either when a handler completes
508handling that exception or when the search reaches the base
509of the stack.
510While a try statement is marked, its handlers are never matched, effectively
511skipping over it to the next try statement.
512
513\begin{center}
514\input{stack-marking}
515\end{center}
516
517There are other sets of marking rules that could be used,
518for instance, marking just the handlers that caught the exception,
519would also prevent recursive resumption.
520However, the rules selected mirrors what happens with termination,
521so this reduces the amount of rules and patterns a programmer has to know.
522
523The marked try statements are the ones that would be removed from
524the stack for a termination exception, \ie those on the stack
525between the handler and the raise statement.
526This symmetry applies to the default handler as well, as both kinds of
527default handlers are run at the raise statement, rather than (physically
528or logically) at the bottom of the stack.
529% In early development having the default handler happen after
530% unmarking was just more useful. We assume that will continue.
531
532\section{Conditional Catch}
533Both termination and resumption handler clauses can be given an additional
534condition to further control which exceptions they handle:
535\begin{cfa}
536catch (EXCEPTION_TYPE * [NAME] ; CONDITION)
537\end{cfa}
538First, the same semantics is used to match the exception type. Second, if the
539exception matches, @CONDITION@ is executed. The condition expression may
540reference all names in scope at the beginning of the try block and @NAME@
541introduced in the handler clause. If the condition is true, then the handler
542matches. Otherwise, the exception search continues as if the exception type
543did not match.
544
545The condition matching allows finer matching by checking
546more kinds of information than just the exception type.
547\begin{cfa}
548try {
549        handle1 = open( f1, ... );
550        handle2 = open( f2, ... );
551        handle3 = open( f3, ... );
552        ...
553} catch( IOFailure * f ; fd( f ) == f1 ) {
554        // Only handle IO failure for f1.
555} catch( IOFailure * f ; fd( f ) == f3 ) {
556        // Only handle IO failure for f3.
557}
558// Handle a failure relating to f2 further down the stack.
559\end{cfa}
560In this example the file that experienced the IO error is used to decide
561which handler should be run, if any at all.
562
563\begin{comment}
564% I know I actually haven't got rid of them yet, but I'm going to try
565% to write it as if I had and see if that makes sense:
566\section{Reraising}
567\label{s:Reraising}
568Within the handler block or functions called from the handler block, it is
569possible to reraise the most recently caught exception with @throw@ or
570@throwResume@, respectively.
571\begin{cfa}
572try {
573        ...
574} catch( ... ) {
575        ... throw;
576} catchResume( ... ) {
577        ... throwResume;
578}
579\end{cfa}
580The only difference between a raise and a reraise is that reraise does not
581create a new exception; instead it continues using the current exception, \ie
582no allocation and copy. However the default handler is still set to the one
583visible at the raise point, and hence, for termination could refer to data that
584is part of an unwound stack frame. To prevent this problem, a new default
585handler is generated that does a program-level abort.
586\end{comment}
587
588\subsection{Comparison with Reraising}
589In languages without conditional catch, that is no ability to match an
590exception based on something other than its type, it can be mimicked
591by matching all exceptions of the right type, checking any additional
592conditions inside the handler and re-raising the exception if it does not
593match those.
594
595Here is a minimal example comparing both patterns, using @throw;@
596(no argument) to start a re-raise.
597\begin{center}
598\begin{tabular}{l r}
599\begin{cfa}
600try {
601    do_work_may_throw();
602} catch(exception_t * exc ;
603                can_handle(exc)) {
604    handle(exc);
605}
606
607
608
609\end{cfa}
610&
611\begin{cfa}
612try {
613    do_work_may_throw();
614} catch(exception_t * exc) {
615    if (can_handle(exc)) {
616        handle(exc);
617    } else {
618        throw;
619    }
620}
621\end{cfa}
622\end{tabular}
623\end{center}
624At first glance catch-and-reraise may appear to just be a quality of life
625feature, but there are some significant differences between the two
626stratagies.
627
628A simple difference that is more important for \CFA than many other languages
629is that the raise site changes, with a re-raise but does not with a
630conditional catch.
631This is important in \CFA because control returns to the raise site to run
632the per-site default handler. Because of this only a conditional catch can
633allow the original raise to continue.
634
635The more complex issue comes from the difference in how conditional
636catches and re-raises handle multiple handlers attached to a single try
637statement. A conditional catch will continue checking later handlers while
638a re-raise will skip them.
639If the different handlers could handle some of the same exceptions,
640translating a try statement that uses one to use the other can quickly
641become non-trivial:
642
643\noindent
644Original, with conditional catch:
645\begin{cfa}
646...
647} catch (an_exception * e ; check_a(e)) {
648        handle_a(e);
649} catch (exception_t * e ; check_b(e)) {
650        handle_b(e);
651}
652\end{cfa}
653Translated, with re-raise:
654\begin{cfa}
655...
656} catch (exception_t * e) {
657        an_exception * an_e = (virtual an_exception *)e;
658        if (an_e && check_a(an_e)) {
659                handle_a(an_e);
660        } else if (check_b(e)) {
661                handle_b(e);
662        } else {
663                throw;
664        }
665}
666\end{cfa}
667(There is a simpler solution if @handle_a@ never raises exceptions,
668using nested try statements.)
669
670% } catch (an_exception * e ; check_a(e)) {
671%     handle_a(e);
672% } catch (exception_t * e ; !(virtual an_exception *)e && check_b(e)) {
673%     handle_b(e);
674% }
675%
676% } catch (an_exception * e)
677%   if (check_a(e)) {
678%     handle_a(e);
679%   } else throw;
680% } catch (exception_t * e)
681%   if (check_b(e)) {
682%     handle_b(e);
683%   } else throw;
684% }
685In similar simple examples translating from re-raise to conditional catch
686takes less code but it does not have a general trivial solution either.
687
688So, given that the two patterns do not trivially translate into each other,
689it becomes a matter of which on should be encouraged and made the default.
690From the premise that if a handler that could handle an exception then it
691should, it follows that checking as many handlers as possible is preferred.
692So conditional catch and checking later handlers is a good default.
693
694\section{Finally Clauses}
695\label{s:FinallyClauses}
696Finally clauses are used to preform unconditional clean-up when leaving a
697scope and are placed at the end of a try statement after any handler clauses:
698\begin{cfa}
699try {
700        GUARDED_BLOCK
701} ... // any number or kind of handler clauses
702... finally {
703        FINALLY_BLOCK
704}
705\end{cfa}
706The @FINALLY_BLOCK@ is executed when the try statement is removed from the
707stack, including when the @GUARDED_BLOCK@ finishes, any termination handler
708finishes or during an unwind.
709The only time the block is not executed is if the program is exited before
710the stack is unwound.
711
712Execution of the finally block should always finish, meaning control runs off
713the end of the block. This requirement ensures control always continues as if
714the finally clause is not present, \ie finally is for cleanup not changing
715control flow.
716Because of this requirement, local control flow out of the finally block
717is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or
718@return@ that causes control to leave the finally block. Other ways to leave
719the finally block, such as a long jump or termination are much harder to check,
720and at best requiring additional run-time overhead, and so are only
721discouraged.
722
723Not all languages with unwinding have finally clauses. Notably \Cpp does
724without it as destructors, and the RAII design pattern, serve a similar role.
725Although destructors and finally clauses can be used for the same cases,
726they have their own strengths, similar to top-level function and lambda
727functions with closures.
728Destructors take more work to create, but if there is clean-up code
729that needs to be run every time a type is used, they are much easier
730to set-up for each use. % It's automatic.
731On the other hand finally clauses capture the local context, so is easy to
732use when the clean-up is not dependent on the type of a variable or requires
733information from multiple variables.
734
735\section{Cancellation}
736\label{s:Cancellation}
737Cancellation is a stack-level abort, which can be thought of as as an
738uncatchable termination. It unwinds the entire current stack, and if
739possible forwards the cancellation exception to a different stack.
740
741Cancellation is not an exception operation like termination or resumption.
742There is no special statement for starting a cancellation; instead the standard
743library function @cancel_stack@ is called passing an exception. Unlike a
744raise, this exception is not used in matching only to pass information about
745the cause of the cancellation.
746Finally, as no handler is provided, there is no default handler.
747
748After @cancel_stack@ is called the exception is copied into the EHM's memory
749and the current stack is unwound.
750The behaviour after that depends on the kind of stack being cancelled.
751
752\paragraph{Main Stack}
753The main stack is the one used by the program main at the start of execution,
754and is the only stack in a sequential program.
755After the main stack is unwound there is a program-level abort.
756
757The first reason for this behaviour is for sequential programs where there
758is only one stack, and hence to stack to pass information to.
759Second, even in concurrent programs, the main stack has no dependency
760on another stack and no reliable way to find another living stack.
761Finally, keeping the same behaviour in both sequential and concurrent
762programs is simple and easy to understand.
763
764\paragraph{Thread Stack}
765A thread stack is created for a \CFA @thread@ object or object that satisfies
766the @is_thread@ trait.
767After a thread stack is unwound, the exception is stored until another
768thread attempts to join with it. Then the exception @ThreadCancelled@,
769which stores a reference to the thread and to the exception passed to the
770cancellation, is reported from the join to the joining thread.
771There is one difference between an explicit join (with the @join@ function)
772and an implicit join (from a destructor call). The explicit join takes the
773default handler (@defaultResumptionHandler@) from its calling context while
774the implicit join provides its own; which does a program abort if the
775@ThreadCancelled@ exception cannot be handled.
776
777The communication and synchronization are done here because threads only have
778two structural points (not dependent on user-code) where
779communication/synchronization happens: start and join.
780Since a thread must be running to perform a cancellation (and cannot be
781cancelled from another stack), the cancellation must be after start and
782before the join, so join is used.
783
784% TODO: Find somewhere to discuss unwind collisions.
785The difference between the explicit and implicit join is for safety and
786debugging. It helps prevent unwinding collisions by avoiding throwing from
787a destructor and prevents cascading the error across multiple threads if
788the user is not equipped to deal with it.
789It is always possible to add an explicit join if that is the desired behaviour.
790
791With explicit join and a default handler that triggers a cancellation, it is
792possible to cascade an error across any number of threads,
793alternating between the resumption (possibly termination) and cancellation,
794cleaning up each
795in turn, until the error is handled or the main thread is reached.
796
797\paragraph{Coroutine Stack}
798A coroutine stack is created for a @coroutine@ object or object that
799satisfies the @is_coroutine@ trait.
800After a coroutine stack is unwound, control returns to the @resume@ function
801that most recently resumed it. @resume@ reports a
802@CoroutineCancelled@ exception, which contains a references to the cancelled
803coroutine and the exception used to cancel it.
804The @resume@ function also takes the \defaultResumptionHandler{} from the
805caller's context and passes it to the internal report.
806
807A coroutine only knows of two other coroutines,
808its starter and its last resumer.
809The starter has a much more distant connection, while the last resumer just
810(in terms of coroutine state) called resume on this coroutine, so the message
811is passed to the latter.
812
813With a default handler that triggers a cancellation, it is possible to
814cascade an error across any number of coroutines,
815alternating between the resumption (possibly termination) and cancellation,
816cleaning up each in turn,
817until the error is handled or a thread stack is reached.
Note: See TracBrowser for help on using the repository browser.