Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
Loading...
Searching...
No Matches
Functions
scope.c File Reference

Implementation of scope and symbol table management functions. More...

#include <stdio.h>
#include <string.h>
#include "../c_libs/memory/memory.h"
#include "type.h"
Include dependency graph for scope.c:

Functions

void init_scope (Scope *scope, Scope *parent, const char *name, ArenaAllocator *arena)
 Initialize a scope structure with enhanced metadata.
 
Symbolscope_lookup_with_visibility (Scope *scope, const char *name, Scope *requesting_module_scope)
 
const char * get_current_function_name (Scope *scope)
 
bool scope_add_symbol_with_ownership (Scope *scope, const char *name, AstNode *type, bool is_public, bool is_mutable, bool returns_ownership, bool takes_ownership, ArenaAllocator *arena)
 
bool scope_add_symbol (Scope *scope, const char *name, AstNode *type, bool is_public, bool is_mutable, ArenaAllocator *arena)
 
Scopefind_containing_module (Scope *scope)
 Find the containing module scope for a given scope.
 
Symbolscope_lookup (Scope *scope, const char *name)
 Original scope_lookup - now wraps the visibility version.
 
Symbolscope_lookup_current_only_with_visibility (Scope *scope, const char *name, Scope *requesting_module_scope)
 Look up a symbol only in the current scope with visibility rules.
 
Symbolscope_lookup_current_only (Scope *scope, const char *name)
 Original scope_lookup_current_only - now wraps the visibility version.
 
Scopecreate_child_scope (Scope *parent, const char *name, ArenaAllocator *arena)
 Create a new child scope under the specified parent.
 

Detailed Description

Implementation of scope and symbol table management functions.

This file contains the implementation of the hierarchical scope system for symbol table management. It provides functionality for creating, managing, and searching through nested scopes with proper parent-child relationships and symbol resolution.

The scope system supports:

Function Documentation

◆ create_child_scope()

Scope * create_child_scope ( Scope parent,
const char *  name,
ArenaAllocator arena 
)

Create a new child scope under the specified parent.

Allocates and initializes a new scope as a child of the given parent, automatically establishing the parent-child relationship and adding the new scope to the parent's children list.

Parameters
parentParent scope for the new child (NULL for root scope)
nameDescriptive name for the new scope
arenaArena allocator for memory management
Returns
Pointer to the newly created and initialized child scope

Creation process:

  1. Allocate memory for new Scope structure from arena
  2. Initialize the scope with proper parent linkage
  3. Add the new scope to parent's children array (if parent exists)
  4. Return pointer to fully initialized child scope
Note
The child scope is automatically added to the parent's children list, maintaining the bidirectional parent-child relationship for debugging and traversal purposes
Warning
If memory allocation fails, this function may return NULL or an incompletely initialized scope. The caller should verify successful creation before using the returned scope.
If adding the child to the parent's children array fails due to memory constraints, the child scope will still be created and functional, but won't appear in the parent's children list.

◆ find_containing_module()

Scope * find_containing_module ( Scope scope)

Find the containing module scope for a given scope.

◆ get_current_function_name()

const char * get_current_function_name ( Scope scope)

◆ init_scope()

void init_scope ( Scope scope,
Scope parent,
const char *  name,
ArenaAllocator arena 
)

Initialize a scope structure with enhanced metadata.

Sets up a new scope with proper initialization of all fields, including parent-child relationships, depth calculation, and growable arrays for symbols and children. The scope is fully prepared for symbol addition and child scope creation.

Parameters
scopePointer to the scope structure to initialize (must be pre-allocated)
parentParent scope for hierarchical lookup (NULL for global scope)
nameDescriptive name for debugging and identification (NULL becomes "unnamed")
arenaArena allocator for all memory allocations within this scope

Initialization includes:

  • Parent pointer assignment and depth calculation
  • Name duplication into arena memory for persistence
  • Default flag initialization (not a function scope, no associated AST node)
  • Growable array setup with reasonable initial capacities (16 symbols, 8 children)
Note
The scope name is duplicated using arena allocation, ensuring it remains valid for the lifetime of the scope without external string management
Warning
The scope parameter must point to valid, allocated memory. This function does not allocate the scope structure itself, only initializes it.

◆ scope_add_symbol()

bool scope_add_symbol ( Scope scope,
const char *  name,
AstNode type,
bool  is_public,
bool  is_mutable,
ArenaAllocator arena 
)

◆ scope_add_symbol_with_ownership()

bool scope_add_symbol_with_ownership ( Scope scope,
const char *  name,
AstNode type,
bool  is_public,
bool  is_mutable,
bool  returns_ownership,
bool  takes_ownership,
ArenaAllocator arena 
)

◆ scope_lookup()

Symbol * scope_lookup ( Scope scope,
const char *  name 
)

Original scope_lookup - now wraps the visibility version.

◆ scope_lookup_current_only()

Symbol * scope_lookup_current_only ( Scope scope,
const char *  name 
)

Original scope_lookup_current_only - now wraps the visibility version.

◆ scope_lookup_current_only_with_visibility()

Symbol * scope_lookup_current_only_with_visibility ( Scope scope,
const char *  name,
Scope requesting_module_scope 
)

Look up a symbol only in the current scope with visibility rules.

◆ scope_lookup_with_visibility()

Symbol * scope_lookup_with_visibility ( Scope scope,
const char *  name,
Scope requesting_module_scope 
)