diff --git a/libs/ast/include/pslang/ast/type.hpp b/libs/ast/include/pslang/ast/type.hpp index 95e9a42..374571f 100644 --- a/libs/ast/include/pslang/ast/type.hpp +++ b/libs/ast/include/pslang/ast/type.hpp @@ -60,5 +60,6 @@ namespace pslang::ast }; std::size_t type_size(types::type const & type); + std::size_t type_alignment(types::type const & type); } diff --git a/libs/ast/source/type.cpp b/libs/ast/source/type.cpp index 6b55fb5..6c5031b 100644 --- a/libs/ast/source/type.cpp +++ b/libs/ast/source/type.cpp @@ -82,6 +82,42 @@ namespace pslang::ast } }; + struct alignment_visitor + : types::const_visitor + { + using const_visitor::apply; + + std::size_t apply(types::unit_type const & type) + { + return 1; + } + + std::size_t apply(types::primitive_type const & type) + { + return types::type_size(type); + } + + std::size_t apply(types::array_type const & type) + { + return apply(*type.element_type); + } + + std::size_t apply(types::function_type const &) + { + return 8; + } + + std::size_t apply(types::pointer_type const &) + { + return 8; + } + + std::size_t apply(types::struct_type const & type) + { + return type.node->layout.alignment; + } + }; + } types::type_ptr get_type(type const & type) @@ -94,4 +130,9 @@ namespace pslang::ast return size_visitor{}.apply(type); } + std::size_t type_alignment(types::type const & type) + { + return alignment_visitor{}.apply(type); + } + }