xo-reader2 stack: expand symbol table to store typedefs

+ typedef utest
+ misc qol policy choices
This commit is contained in:
Roland Conybeare 2026-03-11 07:49:14 -05:00
commit 5a141e09ac
29 changed files with 841 additions and 150 deletions

View file

@ -7,6 +7,7 @@
#include "Binding.hpp"
#include "DVariable.hpp"
#include "DTypename.hpp"
#include <xo/object2/DArray.hpp>
#include <xo/alloc2/dp.hpp>
#include <xo/arena/DArenaHashMap.hpp>
@ -38,16 +39,18 @@ namespace xo {
/** @defgroup scm-globalsymtab-ctors constructors **/
///@{
DGlobalSymtab(dp<repr_type> map, DArray * vars);
DGlobalSymtab(dp<repr_type> var_map, DArray * vars,
dp<repr_type> type_map, DArray * types);
/** create instance.
* Use memory from @p fixed_mm for @ref map_.
* Use memory from @p mm for DGlobalSymtab instance.
* Hashmap configured per @p cfg.
* Hashmap for variables per @p var_cfg; for types per @p type_cfg.
**/
static dp<DGlobalSymtab> make(obj<AAllocator> mm,
obj<AAllocator> fixed_mm,
const ArenaHashMapConfig & cfg);
const ArenaHashMapConfig & var_cfg,
const ArenaHashMapConfig & type_cfg);
/** non-trivial destructor for @ref map_ **/
~DGlobalSymtab() = default;
@ -56,8 +59,8 @@ namespace xo {
/** @defgroup scm-globalsymtab-access-methods access methods **/
///@{
size_type size() const noexcept { return map_->size(); }
size_type capacity() const noexcept { return map_->capacity(); }
size_type n_vars() const noexcept { return var_map_->size(); }
size_type var_capacity() const noexcept { return var_map_->capacity(); }
/** visit symtab-owned memory pools; call visitor(info) for each **/
void visit_pools(const MemorySizeVisitor & visitor) const;
@ -65,6 +68,9 @@ namespace xo {
/** lookup global symbol with name @p sym **/
DVariable * lookup_variable(const DUniqueString * sym) const noexcept;
/** lookup global typename with name @p sym **/
DTypename * lookup_typename(const DUniqueString * sym) const noexcept;
///@}
/** @defgroup scm-globalsymtab-general-methods general methods **/
///@{
@ -76,6 +82,13 @@ namespace xo {
void upsert_variable(obj<AAllocator> mm,
DVariable * var);
/** update this symtab to associate typename @p type with @c type->name().
* If there was a previous type with the same name, replace it with
* @p type.
**/
void upsert_typename(obj<AAllocator> mm,
DTypename * type);
///@}
/** @defgroup scm-globalsymtab-symboltable-facet symboltable facet **/
///@{
@ -104,20 +117,31 @@ namespace xo {
///@}
private:
/** map symbols -> bindings.
* Minor point: storing offsets instead of Variables allows us to omit
* iterating over map elements during GC. Possible savings if map_ slots
* sparsely populated.
/** map variable symbol -> index into @ref vars_.
* Minor point: storing offsets instead of Variables allows us to:
* omit hash-map iteration during GC.
* Savings when map_ slots sparsely populated.
**/
dp<repr_type> map_;
dp<repr_type> var_map_;
/** array of variables.
* When S is a unique-string for a global symbol, then:
* 1. map_[S] is unique global index i(S) for S.
* 1. var_map_[S] is unique global index i(S) for S.
* 2. vars_[i(S)] is variable-expr var(S) for S
* 3. var(S)->name == S
**/
DArray * vars_ = nullptr;
/** map type name -> index values into @ref types_ **/
dp<repr_type> type_map_;
/** array of types.
* When T is a unique-string for a globally-defined type, then:
* 1. type_map_[T] is unique global index i(T) for T.
* 2. types_[i(T)] is type type(T) for T
* 3. type(T)->name == T
**/
DArray * types_ = nullptr;
};
} /*namespace scm*/

View file

@ -62,7 +62,7 @@ namespace xo {
///@{
DLocalSymtab * local_symtab() const noexcept { return local_symtab_; }
size_type n_args() const noexcept { return local_symtab_->size(); }
size_type n_args() const noexcept { return local_symtab_->n_vars(); }
obj<AExpression> body_expr() const noexcept { return body_expr_; }
// get_free_variables()

View file

@ -8,9 +8,7 @@
#include "Binding.hpp"
#include "DVariable.hpp"
#include "DUniqueString.hpp"
//#include "exprtype.hpp"
//#include <xo/reflect/TaggedPtr.hpp>
//#include <xo/gc/GCObject.hpp>
#include <xo/object2/DArray.hpp>
namespace xo {
namespace scm {
@ -19,14 +17,11 @@ namespace xo {
**/
struct DLocalSymtab {
public:
// using TaggedPtr = xo::reflect::TaggedPtr;
// using TypeDescr = xo::reflect::TypeDescr;
// using AGCObject = xo::mm::AGCObject;
// using typeseq = xo::reflect::typeseq;
using DArray = xo::scm::DArray;
using ppindentinfo = xo::print::ppindentinfo;
using ACollector = xo::mm::ACollector;
using AAllocator = xo::mm::AAllocator;
using AGCObject = xo::mm::AGCObject;
/* note: uint16_t would be fine too */
using size_type = std::uint32_t;
@ -46,32 +41,31 @@ namespace xo {
/** @defgroup scm-lambdaexpr-constructors **/
///@{
/** empty instance with parent @p p and capacity for @p n slots.
* Caller must ensure that slots_[0..n) are actually addressable
/** empty instance with parent @p p, using arrays @p vars for variables
* and @p types for type definitions.
**/
DLocalSymtab(DLocalSymtab * p, size_type n);
DLocalSymtab(DLocalSymtab * p, DArray * nv, DArray * nt);
/** scaffold empty symtab instance,
* with capacity for @p n slots, using memory from allocator @p mm
* capacity for @p nv vars and @p nt types,
* using memory from allocator @p mm.
* Symtab chains to parent @p p.
**/
static DLocalSymtab * _make_empty(obj<AAllocator> mm,
DLocalSymtab * p,
size_type n);
size_type nv,
size_type nt);
///@}
/** @defgroup scm-lambdaexpr-methods **/
///@{
DLocalSymtab * parent() const noexcept { return parent_; }
size_type capacity() const noexcept { return capacity_; }
size_type size() const noexcept { return size_; }
//size_type capacity() const noexcept { return capacity_; }
size_type n_vars() const noexcept { return vars_->size(); }
size_type n_types() const noexcept { return types_->size(); }
DVariable * lookup_var(Binding ix) noexcept {
assert(ix.i_link() == 0);
assert(ix.j_slot() < static_cast<int32_t>(size_));
return slots_[ix.j_slot()].var_;
}
DVariable * lookup_var(Binding ix) noexcept;
/** increase slot size (provided below capacity) to append
* binding for one local variable. Local variable will be allocated
@ -81,6 +75,14 @@ namespace xo {
const DUniqueString * name,
TypeRef typeref);
/** increase slot size (provided below capacity) to append
* binding for one local type. Local type will be allocated
* from @p mm, named @p name, with type described by @p type.
**/
void append_type(obj<AAllocator> mm,
const DUniqueString * name,
obj<AType> type);
///@}
/** @defgroup xo-localsymtab-symboltable-facet symboltable facet**/
///@{
@ -110,12 +112,23 @@ namespace xo {
private:
/** parent symbol table from scoping surrounding this one **/
DLocalSymtab * parent_ = nullptr;
/** variables owned by (declared in) this symbol table
* vars_[i] is convertible to obj<AGCObject>
**/
DArray * vars_ = nullptr;
/** types owned by (defined in) this symbol table
* types_[i] is convertible to obj<AType>
**/
DArray * types_ = nullptr;
#ifdef OBSOLETE
/** actual range of slots_[] array. Can use indices in [0,..,n) **/
size_type capacity_ = 0;
/** number of slots in use **/
size_type size_ = 0;
/** memory for names and bindings **/
Slot slots_[];
#endif
};
} /*namespace scm*/
} /*namespace xo*/

View file

@ -29,8 +29,8 @@ namespace xo {
using ppindentinfo = xo::print::ppindentinfo;
public:
DSequenceExpr() = default;
DSequenceExpr(DArray * xv) : expr_v_{xv} {}
DSequenceExpr() ;
DSequenceExpr(DArray * xv);
/** create empty sequence using memory from @p mm **/
static obj<AExpression,DSequenceExpr> make_empty(obj<AAllocator> mm);

View file

@ -0,0 +1,77 @@
/** @file DTypename.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DUniqueString.hpp"
#include <xo/type/Type.hpp>
#include <xo/printable2/Printable.hpp>
#include <xo/indentlog/print/pretty.hpp>
namespace xo {
namespace scm {
/** @class DTypename
* @brief Container for a named type
*
* Represents the result of syntax like
* 1. deftype Foo :: i64;
* 2. deftype FooAlias :: Foo;
**/
class DTypename {
public:
using ppindentinfo = xo::print::ppindentinfo;
using ACollector = xo::mm::ACollector;
using AAllocator = xo::mm::AAllocator;
using AGCObject = xo::mm::AGCObject;
public:
DTypename(const DUniqueString * name,
obj<AType> type);
/** create instance
* @p mm memory allocator
* @p name type name
* @p type type definition
**/
static DTypename * _make(obj<AAllocator> mm,
const DUniqueString * name,
obj<AType> type);
/** create fop for new instance **/
static obj<AGCObject,DTypename> make(obj<AAllocator> mm,
const DUniqueString * name,
obj<AType> type);
const DUniqueString * name() const noexcept { return name_; }
obj<AType> type() const noexcept { return type_; }
void assign_name(const DUniqueString * name) { this->name_ = name; }
/** @defgroup scm-typename-gcobject-facet **/
///@{
size_t shallow_size() const noexcept;
DTypename * shallow_copy(obj<AAllocator> mm) const noexcept;
size_t forward_children(obj<ACollector> gc) noexcept;
///@}
/** @defgroup scm-typename-printable-facet **/
///@{
bool pretty(const ppindentinfo & ppii) const;
///@}
private:
/** symbol name **/
const DUniqueString * name_ = nullptr;
/** type defintion. Everything but the name **/
obj<AType> type_;
};
} /*namespace scm*/
} /*namespace xo*/
/* end DTypename.hpp */

View file

@ -76,7 +76,7 @@ namespace xo {
private:
/** symbol name **/
const DUniqueString * name_;
const DUniqueString * name_ = nullptr;
/** variable value always has type consistent
* with this description
**/

View file

@ -5,7 +5,9 @@
#pragma once
#include <xo/type/Type.hpp>
#include <xo/reflect/TypeDescr.hpp>
#include <xo/alloc2/Collector.hpp>
#include <xo/flatstring/flatstring.hpp>
#include <xo/indentlog/print/pretty.hpp>
@ -23,17 +25,28 @@ namespace xo {
using TypeDescr = xo::reflect::TypeDescr;
using type_var = flatstring<20>;
using prefix_type = flatstring<8>;
using ACollector = xo::mm::ACollector;
using ppindentinfo = xo::print::ppindentinfo;
public:
TypeRef() = default;
TypeRef(const type_var & id, TypeDescr td);
TypeRef(const type_var & id, obj<AType> type);
/** trivial typeref, where already resolved.
* Require: @p td non-null
**/
static TypeRef resolved(TypeDescr td);
/** trivial typeref, where already resolved **/
static TypeRef resolved(obj<AType> type);
/** if @p type is non-null
* -> type already resolved
* else
* -> generate unique typevar name, starting with @p prefix
**/
static TypeRef dwim(prefix_type prefix, obj<AType> type);
/** if @p td is non-null
* -> type is already resolved
*
@ -59,9 +72,19 @@ namespace xo {
/** pretty-printer support **/
bool pretty(const ppindentinfo & ppii) const;
/** gc support **/
void forward_children(obj<ACollector> gc) noexcept;
private:
TypeRef(const type_var & id, TypeDescr td);
private:
/** unique (probably generated) name for type at this location **/
type_var id_;
/** Type, when resolved **/
obj<AType> type_;
/** Description for concrete type, once resolved.
* May be null when this TypeRef created,
* but expected to be immutable once established.

View file

@ -0,0 +1,12 @@
/** @file Typename.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DTypename.hpp"
#include "typename/IGCObject_DTypename.hpp"
#include "typename/IPrintable_DTypename.hpp"
/* end Typename.hpp */

View file

@ -0,0 +1,65 @@
/** @file IGCObject_DTypename.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DTypename.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DTypename.json5]
**/
#pragma once
#include "GCObject.hpp"
#include "DTypename.hpp"
namespace xo { namespace scm { class IGCObject_DTypename; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::mm::AGCObject,
xo::scm::DTypename>
{
using ImplType = xo::mm::IGCObject_Xfer
<xo::scm::DTypename,
xo::scm::IGCObject_DTypename>;
};
}
}
namespace xo {
namespace scm {
/** @class IGCObject_DTypename
**/
class IGCObject_DTypename {
public:
/** @defgroup scm-gcobject-dtypename-type-traits **/
///@{
using size_type = xo::mm::AGCObject::size_type;
using AAllocator = xo::mm::AGCObject::AAllocator;
using ACollector = xo::mm::AGCObject::ACollector;
using Copaque = xo::mm::AGCObject::Copaque;
using Opaque = xo::mm::AGCObject::Opaque;
///@}
/** @defgroup scm-gcobject-dtypename-methods **/
///@{
// const methods
/** memory consumption for this instance **/
static size_type shallow_size(const DTypename & self) noexcept;
/** copy instance using allocator **/
static Opaque shallow_copy(const DTypename & self, obj<AAllocator> mm) noexcept;
// non-const methods
/** during GC: forward immdiate children **/
static size_type forward_children(DTypename & self, obj<ACollector> gc) noexcept;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -0,0 +1,62 @@
/** @file IPrintable_DTypename.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DTypename.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DTypename.json5]
**/
#pragma once
#include "Printable.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
#include "DTypename.hpp"
namespace xo { namespace scm { class IPrintable_DTypename; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::print::APrintable,
xo::scm::DTypename>
{
using ImplType = xo::print::IPrintable_Xfer
<xo::scm::DTypename,
xo::scm::IPrintable_DTypename>;
};
}
}
namespace xo {
namespace scm {
/** @class IPrintable_DTypename
**/
class IPrintable_DTypename {
public:
/** @defgroup scm-printable-dtypename-type-traits **/
///@{
using ppindentinfo = xo::print::APrintable::ppindentinfo;
using Copaque = xo::print::APrintable::Copaque;
using Opaque = xo::print::APrintable::Opaque;
///@}
/** @defgroup scm-printable-dtypename-methods **/
///@{
// const methods
/** Pretty-printing support for this object.
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
static bool pretty(const DTypename & self, const ppindentinfo & ppii);
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */