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

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation pthread-emulation qualifiedEnum
Last change on this file since a916aad was cd03b76d, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Andrew MMath: Clean-up pass addressing (or deciding not to address) most of the remaining \todo items.

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