/* =====================================
 * Generated by Mod-c 0.26.5
 * HEADER symtab MODULE
 * ===================================== */

#ifndef MODC_MODULE_symtab_H
#define MODC_MODULE_symtab_H

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

#define nil ((void *)0)
#define NIL nil
typedef uint8_t byte;
typedef const char* string;
typedef int integer;
typedef unsigned int cardinal;
typedef float real;

/** symtab.mc
 * Symbol Table Definition
 *
 * Scoped symbol tables for semantic analysis.
 * Each table is one scope; parent links form a chain.
 */

/* === IMPORT === */
#include "arena.h"

/* === IMPORT === */
#include "symbol.h"

/* === IMPORT === */
#include "symkind.h"

/* === IMPORT === */
#include "stddef.h"

/* === IMPORT === */
#include "stdio.h"

/* === IMPORT === */
#include "stdlib.h"

/* === IMPORT === */
#include "string.h"

/* === IMPORT === */
#include "node.h"

/** One lexical scope */

/* === TYPE EXPORT === */
typedef struct TSymbolTable TSymbolTable;
typedef struct TSymbolTable {
    TSymbolTable* parent;
    ppTSymbol symbols;
    size_t count;
    size_t capacity;
} TSymbolTable;

/* === TYPE EXPORT === */
typedef TSymbolTable* pTSymbolTable;

/**
 * Report a fatal symbol-table error and exit.
 * Location-aware errors come later (tsemantic.mc + Node.token).
 */

/* === PROC/FUNC EXPORT === */
void symtab_fatal(TSymbol* symbol, const char* msg);

/**
 * Create a new scope. parent may be nil (program/module scope).
 */

/* === PROC/FUNC EXPORT === */
pTSymbolTable symtab_create(Arena* arena, pTSymbolTable parent);

/**
 * Compare identifier spelling (length + bytes). No case folding.
 */

/**
 * Look up a name in the current scope only.
 * @return symbol, or nil if not found.
 */

/* === PROC/FUNC EXPORT === */
pTSymbol symtab_lookup_current(pTSymbolTable table, const char* name, size_t name_len);

/**
 * Look up a name in the current scope, then parent scopes.
 * @return symbol, or nil if not found.
 */

/* === PROC/FUNC EXPORT === */
pTSymbol symtab_lookup(pTSymbolTable table, const char* name, size_t name_len);

/**
 * Insert a symbol into the current scope.
 * Fatal on duplicate name in this scope.
 */

/* === PROC/FUNC EXPORT === */
void symtab_define(Arena* arena, pTSymbolTable table, pTSymbol symbol);

/**
 * Insert or rebind a LET symbol in the current scope.
 * Rebinds when an existing LET has the same name; fatal on any other kind.
 */

/* === PROC/FUNC EXPORT === */
void symtab_define_let(Arena* arena, pTSymbolTable table, pTSymbol symbol);

/**
 * Replace an existing symbol in the current scope (IMPORT refinement).
 */

/* === PROC/FUNC EXPORT === */
void symtab_refine_symbol(Arena* arena, pTSymbolTable table, pTSymbol existing, pTSymbol replacement);

void symtab(void);

#endif /* MODC_MODULE_symtab_H */
