Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Compiler_Ast_Context Class Reference

Arena-backed ownership context for AST nodes. More...

#include <Compiler_AST.H>

Collaboration diagram for Aleph::Compiler_Ast_Context:
[legend]

Classes

struct  Owned_Object
 Internal tracking for objects with destructors. More...
 

Public Member Functions

 Compiler_Ast_Context (const size_t arena_size=AhArenaAllocator::DEFAULT_SIZE)
 Constructs a context with the specified arena capacity.
 
 Compiler_Ast_Context (const Compiler_Ast_Context &)=delete
 
Compiler_Ast_Context & operator= (const Compiler_Ast_Context &)=delete
 
 ~Compiler_Ast_Context () noexcept
 Destroys the context and all managed nodes.
 
template<typename T , typename... Args>
T * make (Args &&...args)
 Allocates and constructs an AST node of type T.
 
void reset () noexcept
 Reclaims all memory and destroys managed nodes.
 
size_t allocated_size () const noexcept
 Returns the total bytes currently used in the arena.
 
size_t capacity () const noexcept
 Returns the total capacity of the arena.
 

Private Attributes

AhArenaAllocator arena_
 Underlying memory arena.
 
DynArray< Owned_Object > owned_
 Tracked objects for destruction.
 

Detailed Description

Arena-backed ownership context for AST nodes.

The context uses an AhArenaAllocator for fast allocations and low fragmentation. It also tracks destructors to ensure that nodes with non-trivial members (like std::string or DynArray) are properly cleaned up.

Note
This class is the primary owner of the AST. Nodes allocated here should not be deleted manually.

Definition at line 328 of file Compiler_AST.H.

Constructor & Destructor Documentation

◆ Compiler_Ast_Context() [1/2]

Aleph::Compiler_Ast_Context::Compiler_Ast_Context ( const size_t  arena_size = AhArenaAllocator::DEFAULT_SIZE)
inlineexplicit

Constructs a context with the specified arena capacity.

Parameters
arena_sizeCapacity in bytes (defaults to arena's default size).
Exceptions
std::runtime_errorIf memory cannot be reserved.
Note
Thread-safety: Not thread-safe.
Complexity: O(1).

Definition at line 348 of file Compiler_AST.H.

◆ Compiler_Ast_Context() [2/2]

Aleph::Compiler_Ast_Context::Compiler_Ast_Context ( const Compiler_Ast_Context &  )
delete

◆ ~Compiler_Ast_Context()

Aleph::Compiler_Ast_Context::~Compiler_Ast_Context ( )
inlinenoexcept

Destroys the context and all managed nodes.

Note
Thread-safety: Not thread-safe.
Complexity: O(N) where N is the number of nodes with destructors.

Definition at line 361 of file Compiler_AST.H.

Member Function Documentation

◆ allocated_size()

size_t Aleph::Compiler_Ast_Context::allocated_size ( ) const
inlinenoexcept

Returns the total bytes currently used in the arena.

Returns
Consumed memory in bytes.
Note
Thread-safety: Safe for concurrent readers.
Complexity: O(1).

Definition at line 419 of file Compiler_AST.H.

References Aleph::AhArenaAllocator::allocated_size().

Referenced by TEST().

◆ capacity()

size_t Aleph::Compiler_Ast_Context::capacity ( ) const
inlinenoexcept

Returns the total capacity of the arena.

Returns
Maximum memory in bytes.
Note
Thread-safety: Safe for concurrent readers.
Complexity: O(1).

Definition at line 430 of file Compiler_AST.H.

References Aleph::AhArenaAllocator::capacity().

◆ make()

template<typename T , typename... Args>
T * Aleph::Compiler_Ast_Context::make ( Args &&...  args)
inline

Allocates and constructs an AST node of type T.

Uses arena allocation for performance. If T has a non-trivial destructor, it is registered for automatic cleanup during context reset.

Template Parameters
TNode type to instantiate.
ArgsConstructor argument types.
Parameters
argsValues passed to the constructor of T.
Returns
Pointer to the new node, stable until reset() is called.
Exceptions
std::runtime_errorIf the arena is out of memory.
Note
Thread-safety: Not thread-safe.
Complexity: O(1) amortized.

Definition at line 380 of file Compiler_AST.H.

References ah_runtime_error_unless, Aleph::DynArray< T >::append(), and Aleph::blossom_maximum_cardinality_matching().

Referenced by Aleph::Compiler_Driver::merge_modules_from_order(), Aleph::Compiler_MVP_Frontend::merge_modules_from_order(), Aleph::Compiler_Parser::parse_block_statement(), Aleph::Compiler_Parser::parse_break_statement(), Aleph::Compiler_Parser::parse_call_suffix(), Aleph::Compiler_Parser::parse_continue_statement(), Aleph::Compiler_Parser::parse_enum_declaration(), Aleph::Compiler_Parser::parse_expression(), Aleph::Compiler_Parser::parse_expression_statement(), Aleph::Compiler_Parser::parse_function(), Aleph::Compiler_Parser::parse_function_type(), Aleph::Compiler_Parser::parse_grouping_expr(), Aleph::Compiler_Parser::parse_if_statement(), Aleph::Compiler_Parser::parse_import_declaration(), Aleph::Compiler_Parser::parse_let_statement(), Aleph::Compiler_Parser::parse_named_type(), Aleph::Compiler_Parser::parse_parenthesized_type(), Aleph::Compiler_Parser::parse_primary(), Aleph::Compiler_Parser::parse_return_statement(), Aleph::Compiler_Parser::parse_struct_declaration(), Aleph::Compiler_Parser::parse_type_alias_declaration(), Aleph::Compiler_Parser::parse_unary(), Aleph::Compiler_Parser::parse_while_statement(), TEST(), and TEST().

◆ operator=()

Compiler_Ast_Context & Aleph::Compiler_Ast_Context::operator= ( const Compiler_Ast_Context &  )
delete

◆ reset()

void Aleph::Compiler_Ast_Context::reset ( )
inlinenoexcept

Reclaims all memory and destroys managed nodes.

Calling this method invalidates all pointers previously returned by make(). Useful for recycling the context between compilation units.

Note
Thread-safety: Not thread-safe.
Complexity: O(N) where N is the number of tracked objects.

Definition at line 401 of file Compiler_AST.H.

References Aleph::DynArray< T >::access(), Aleph::and, Aleph::DynArray< T >::clear(), Aleph::AhArenaAllocator::reset(), and Aleph::DynArray< T >::size().

Referenced by Aleph::Compiler_Driver::clear(), Aleph::Compiler_MVP_Frontend::clear(), and TEST().

Member Data Documentation

◆ arena_

AhArenaAllocator Aleph::Compiler_Ast_Context::arena_
private

Underlying memory arena.

Definition at line 337 of file Compiler_AST.H.

◆ owned_

DynArray<Owned_Object> Aleph::Compiler_Ast_Context::owned_
private

Tracked objects for destruction.

Definition at line 338 of file Compiler_AST.H.


The documentation for this class was generated from the following file: