@ is now part of the name of an attribute or a macro. Macros without '@' must be function-like.

This commit is contained in:
Christoffer Lerno
2022-05-08 15:22:38 +02:00
parent 29a9769651
commit 9691d50a6f
80 changed files with 414 additions and 513 deletions

View File

@@ -9,7 +9,6 @@ import std::mem;
**/
macro alloc($Type, usize elements)
{
assert($Type.max / elements < $Type.sizeof);
$Type* ptr = mem::alloc($Type.sizeof * elements, $alignof($Type));
return ptr[0..(elements - 1)];
}

View File

@@ -14,7 +14,7 @@ fault VarCastResult
*
* @param variable `the variable to store and restore`
**/
macro void scope(&variable; @body) @autoimport
macro void @scope(&variable; @body) @autoimport
{
$typeof(variable) temp = variable;
defer variable = temp;
@@ -51,15 +51,15 @@ fn void panic(char* message, char *file, char *function, uint line) @autoimport
if (stack) stack = stack.prev;
if (stack)
{
libc::fprintf(@libc::stderr(), "\nERROR: '%s'\n", message);
libc::fprintf(libc::stderr(), "\nERROR: '%s'\n", message);
}
else
{
libc::fprintf(@libc::stderr(), "\nERROR: '%s', function %s (%s:%d)\n", message, function, file, line);
libc::fprintf(libc::stderr(), "\nERROR: '%s', function %s (%s:%d)\n", message, function, file, line);
}
while (stack)
{
libc::fprintf(@libc::stderr(), " at function %s (%s:%u)\n", stack.function, stack.file, stack.line);
libc::fprintf(libc::stderr(), " at function %s (%s:%u)\n", stack.function, stack.file, stack.line);
if (stack == stack.prev) break;
stack = stack.prev;
}

View File

@@ -26,7 +26,7 @@ fn void LinkedList.push(LinkedList *list, Type value)
private fn void LinkedList.linkFirst(LinkedList *list, Type value)
{
Node *first = list.first;
Node *new_node = @mem::malloc(Node);
Node *new_node = mem::malloc(Node);
*new_node = { .next = first, .value = value };
list.first = new_node;
if (!first)
@@ -90,7 +90,7 @@ fn Type LinkedList.get(LinkedList* list, usize index)
private fn void LinkedList.linkBefore(LinkedList *list, Node *succ, Type value)
{
Node* pred = succ.prev;
Node* new_node = @mem::malloc(Node);
Node* new_node = mem::malloc(Node);
*new_node = { .prev = pred, .next = succ, .value = value };
succ.prev = new_node;
if (!pred)

View File

@@ -117,12 +117,12 @@ fn void List.free(List *list)
}
macro Type List.item_at(List &list, usize index) @operator(elementat)
macro Type List.@item_at(List &list, usize index) @operator(elementat)
{
return list.entries[index];
}
macro Type* List.item_ref(List &list, usize index) @operator(elementref)
macro Type* List.@item_ref(List &list, usize index) @operator(elementref)
{
return &list.entries[index];
}

View File

@@ -3,18 +3,18 @@
// a copy of which can be found in the LICENSE_STDLIB file.
module std::mem;
macro volatile_load(&x)
macro @volatile_load(&x)
{
return $$volatile_load(&x);
}
macro volatile_store(&x, y)
macro @volatile_store(&x, y)
{
return $$volatile_store(&x, y);
}
/**
* @require @math::is_power_of_2(alignment)
* @require math::is_power_of_2(alignment)
**/
fn usize aligned_offset(usize offset, usize alignment)
{
@@ -23,7 +23,7 @@ fn usize aligned_offset(usize offset, usize alignment)
/**
* @require @math::is_power_of_2(alignment)
* @require math::is_power_of_2(alignment)
**/
fn bool ptr_is_aligned(void* ptr, usize alignment) @inline
{
@@ -32,7 +32,7 @@ fn bool ptr_is_aligned(void* ptr, usize alignment) @inline
fn void copy(char* dst, char* src, usize size) @inline
{
@memcpy(dst, src, size);
memcpy(dst, src, size);
}
macro void memcpy(void* dst, void* src, usize size, bool $is_volatile = false, usize $dst_align = 0, usize $src_align = 0)
@@ -42,7 +42,7 @@ macro void memcpy(void* dst, void* src, usize size, bool $is_volatile = false, u
fn void set(void* dst, char val, usize bytes) @inline
{
@memset(dst, val, bytes);
memset(dst, val, bytes);
}
macro void memset(void* dst, char val, usize bytes, bool $is_volatile = false, usize $dst_align = 0)
@@ -94,7 +94,7 @@ fn char[] alloc_bytes(usize bytes) @inline
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void* alloc(usize size, usize alignment = 0)
{
@@ -102,7 +102,7 @@ fn void* alloc(usize size, usize alignment = 0)
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void*! alloc_checked(usize size, usize alignment = 0)
{
@@ -111,7 +111,7 @@ fn void*! alloc_checked(usize size, usize alignment = 0)
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void* calloc(usize size, usize alignment = 0)
{
@@ -119,7 +119,7 @@ fn void* calloc(usize size, usize alignment = 0)
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void*! calloc_checked(usize size, usize alignment = 0)
{
@@ -127,7 +127,7 @@ fn void*! calloc_checked(usize size, usize alignment = 0)
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void* realloc(void *ptr, usize new_size, usize alignment = 0)
{
@@ -135,7 +135,7 @@ fn void* realloc(void *ptr, usize new_size, usize alignment = 0)
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void*! realloc_checked(void *ptr, usize new_size, usize alignment = 0)
{
@@ -150,7 +150,7 @@ fn void free(void* ptr)
/**
* Run with a specific allocator inside of the macro body.
**/
macro void with_allocator(Allocator allocator; @body())
macro void @with_allocator(Allocator allocator; @body())
{
Allocator old_allocator = thread_allocator;
thread_allocator = allocator;
@@ -172,7 +172,7 @@ struct MemoryArena
/**
* @require alignment > 0 `alignment must be non zero`
* @require @math::is_power_of_2(alignment)
* @require math::is_power_of_2(alignment)
* @require size > 0
* @require alignment <= MAX_MEMORY_ALIGNMENT `alignment too big`
* @require this != null

View File

@@ -13,7 +13,7 @@ const AllocatorFunction NULL_ALLOCATOR = &null_allocator_fn;
const AllocatorFunction SYSTEM_ALLOCATOR = &libc_allocator_fn;
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void*! Allocator.alloc(Allocator *allocator, usize size, usize alignment = 0) @inline
{
@@ -21,7 +21,7 @@ fn void*! Allocator.alloc(Allocator *allocator, usize size, usize alignment = 0)
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void*! Allocator.realloc(Allocator *allocator, void* old_pointer, usize size, usize alignment = 0) @inline
{
@@ -29,7 +29,7 @@ fn void*! Allocator.realloc(Allocator *allocator, void* old_pointer, usize size,
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
*/
fn void*! Allocator.calloc(Allocator *allocator, usize size, usize alignment = 0) @inline
{
@@ -68,7 +68,7 @@ private fn usize alignment_for_allocation(usize alignment) @inline
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
* @require data `unexpectedly missing the allocator`
*/
fn void*! arena_allocator_function(void* data, usize size, usize alignment, void* old_pointer, AllocationKind kind)
@@ -126,7 +126,7 @@ fn void*! arena_allocator_function(void* data, usize size, usize alignment, void
arena.used = 0;
return null;
}
@unreachable();
unreachable();
}
struct DynamicArenaAllocator
@@ -148,7 +148,7 @@ private struct DynamicArenaPage
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
* @require data `unexpectedly missing the allocator`
*/
fn void*! dynamic_arena_allocator_function(void* data, usize size, usize alignment, void* old_pointer, AllocationKind kind)
@@ -184,7 +184,7 @@ fn void*! dynamic_arena_allocator_function(void* data, usize size, usize alignme
allocator.reset();
return null;
}
@unreachable();
unreachable();
}
/**
@@ -287,12 +287,12 @@ private fn void DynamicArenaAllocator.reset(DynamicArenaAllocator* this)
}
/**
* @require @math::is_power_of_2(alignment)
* @require math::is_power_of_2(alignment)
* @require size > 0
*/
private fn void*! DynamicArenaAllocator.alloc_new(DynamicArenaAllocator* this, usize size, usize alignment)
{
usize page_size = @max(this.page_size, size + DEFAULT_SIZE_PREFIX + alignment);
usize page_size = max(this.page_size, size + DEFAULT_SIZE_PREFIX + alignment);
void* mem = this.backing_allocator.alloc(page_size)?;
DynamicArenaPage*! page = this.backing_allocator.alloc(DynamicArenaPage.sizeof);
if (catch err = page)
@@ -313,7 +313,7 @@ private fn void*! DynamicArenaAllocator.alloc_new(DynamicArenaAllocator* this, u
}
/**
* @require !alignment || @math::is_power_of_2(alignment)
* @require !alignment || math::is_power_of_2(alignment)
* @require size > 0
* @require this
*/

View File

@@ -16,7 +16,7 @@ private fn void*! null_allocator_fn(void *data, usize bytes, usize alignment, vo
fn void*! libc_allocator_fn(void *unused, usize bytes, usize alignment, void* old_pointer, AllocationKind kind) @inline
{
if (!alignment) alignment = DEFAULT_MEM_ALIGNMENT;
assert(@math::is_power_of_2(alignment), "Alignment was not a power of 2");
assert(math::is_power_of_2(alignment), "Alignment was not a power of 2");
void* data;
switch (kind)
@@ -60,5 +60,5 @@ fn void*! libc_allocator_fn(void *unused, usize bytes, usize alignment, void* ol
libc::free(old_pointer);
return null;
}
@unreachable();
unreachable();
}