From 0d06ebcefb36a19710d87832fc1ea027e21281af Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Thu, 18 Jul 2024 13:11:19 -0400 Subject: Initial implementation for decl-tree reflection API (#4666) * Initial implementation for decl-tree reflection API This patch adds Slang API methods for walking all the declarations in the AST. We expose this functionality through an abstract `DeclReflection` class that can be a type, function or a variable declaration. We also provide ways to cast the decl to a `FunctionReflection`, `TypeReflection` or `VariableReflection` and traverse through the child nodes (for instance, a struct type will have component variable declarations) This patch also adds `ISlangInternal` as an internal COM interface to allow us to cast IGlobalSession to the internal Session pointer while bypassing any wrappers (such as the capture interface) * Update slang.h * Remove `ISlangInternal` (its causing a diamond pattern w.r.t `ISlangUnknown`) and use `ComPtr` for proper ref management. * Update unit-test-decl-tree-reflection.cpp * Change `FunctionDeclBase` to use `DeclRef` instead of directly using the decl. * Update slang-reflection-api.cpp --- source/slang/slang.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'source/slang/slang.cpp') diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index f28d3cdeb..f440ce3cb 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -546,11 +546,23 @@ SlangResult Session::_readBuiltinModule(ISlangFileSystem* fileSystem, Scope* sco return SLANG_OK; } -ISlangUnknown* Session::getInterface(const Guid& guid) +SLANG_NO_THROW SlangResult SLANG_MCALL Session::queryInterface(SlangUUID const& uuid, void** outObject) { - if(guid == ISlangUnknown::getTypeGuid() || guid == IGlobalSession::getTypeGuid()) - return asExternal(this); - return nullptr; + if (uuid == Session::getTypeGuid()) + { + addReference(); + *outObject = static_cast(this); + return SLANG_OK; + } + + if (uuid == ISlangUnknown::getTypeGuid() && uuid == IGlobalSession::getTypeGuid()) + { + addReference(); + *outObject = static_cast(this); + return SLANG_OK; + } + + return SLANG_E_NO_INTERFACE; } static size_t _getStructureSize(const uint8_t* src) @@ -4070,6 +4082,11 @@ void Module::buildHash(DigestBuilder& builder) builder.append(computeDigest()); } +slang::DeclReflection* Module::getModuleReflection() +{ + return (slang::DeclReflection*)m_moduleDecl; +} + SHA1::Digest Module::computeDigest() { if (m_digest == SHA1::Digest()) -- cgit v1.2.3