Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
errors.c File Reference
#include "serene/rt/errors.h"
#include "serene/rt/context.h"
#include "serene/utils.h"
Include dependency graph for errors.c:

Go to the source code of this file.

Functions

srn_error_tsrn_errors_make (const srn_context_t *ctx, srn_error_tag_t tag, const char *msg)
 Build an error in ctx's memory, tagged tag and carrying msg.
void * srn_errors_fail (const srn_context_t *ctx, size_t size, size_t alignment, srn_error_tag_t tag, const char *msg)
 Allocate a size/alignment value in ctx, set its ERROR_HEADER (the first member, srn_error_t *maybe_error) to a fresh error, and return it.

Function Documentation

◆ srn_errors_fail()

void * srn_errors_fail ( const srn_context_t * ctx,
size_t size,
size_t alignment,
srn_error_tag_t tag,
const char * msg )
nodiscard

Allocate a size/alignment value in ctx, set its ERROR_HEADER (the first member, srn_error_t *maybe_error) to a fresh error, and return it.

The result is a valid fallible value whose HAS_ERROR is true. Use the FAIL macro, which fills in size/alignment from the target type.

Definition at line 39 of file errors.c.

41 {
42 // The ERROR_HEADER convention guarantees `srn_error_t *maybe_error` is the
43 // first member of any fallible T, so offset 0 of the value is that pointer.
44 // Allocate the value and set it to a fresh error; the caller checks it with
45 // HAS_ERROR and never reads the other fields of an errored value.
46 void *value = srn_allocate(ctx, size, alignment);
47 PANIC_IF_NULL(value);
48 *(srn_error_t **)value = srn_errors_make(ctx, tag, msg);
49 return value;
50}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:73
srn_error_t * srn_errors_make(const srn_context_t *ctx, srn_error_tag_t tag, const char *msg)
Build an error in ctx's memory, tagged tag and carrying msg.
Definition errors.c:31
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
Here is the call graph for this function:

◆ srn_errors_make()

srn_error_t * srn_errors_make ( const srn_context_t * ctx,
srn_error_tag_t tag,
const char * msg )

Build an error in ctx's memory, tagged tag and carrying msg.

Returns the error to attach to a value's maybe_error (or to inspect directly). The ERR macro below is the shorthand for the common call.

Definition at line 31 of file errors.c.

31 {
32 srn_error_t *err = ALLOC(ctx, srn_error_t);
33 PANIC_IF_NULL(err);
34 err->tag = tag;
35 err->msg = (char *)msg;
36 return err;
37}
#define ALLOC(ctx, T)
Definition context.h:84
char * msg
Definition errors.h:127
srn_error_tag_t tag
Definition errors.h:126
Here is the caller graph for this function: