From 36c9858b3b1df3f636a2478050da3e721cbdf8f9 Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Fri, 10 Jun 2022 21:50:04 -0700 Subject: [PATCH] When traversing the scope chain, account for the size of the earlier scopes. Closes #269 --- src/ast.rs | 30 +++++++++++++++++++++++++++++- tests/tests.rs | 5 +++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ast.rs b/src/ast.rs index c9d68cd..3064aef 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -343,6 +343,9 @@ trait ArgScope<'me, 'ctx>: fmt::Debug { fn get_template_arg(&'me self, index: usize) -> Result<(&'ctx TemplateArg, &'ctx TemplateArgs)>; + /// Get the current scope's count of template arguments. + fn template_arg_count(&'me self) -> usize; + /// Get the current scope's `index`th function argument's type. fn get_function_arg(&'me self, index: usize) -> Result<&'ctx Type>; } @@ -408,7 +411,7 @@ impl<'prev, 'subs> ArgScope<'prev, 'subs> for Option fn get_template_arg( &'prev self, - idx: usize, + mut idx: usize, ) -> Result<(&'subs TemplateArg, &'subs TemplateArgs)> { let mut scope = self.as_ref(); while let Some(s) = scope { @@ -423,11 +426,16 @@ impl<'prev, 'subs> ArgScope<'prev, 'subs> for Option return Ok((arg, args)); } scope = s.prev; + idx = idx.checked_sub(s.item.template_arg_count()).unwrap(); } Err(error::Error::BadTemplateArgReference) } + fn template_arg_count(&self) -> usize { + self.as_ref().map(|s| s.item.template_arg_count()).unwrap_or(0) + } + fn get_function_arg(&'prev self, idx: usize) -> Result<&'subs Type> { let mut scope = self.as_ref(); while let Some(s) = scope { @@ -2624,6 +2632,10 @@ impl<'subs> ArgScope<'subs, 'subs> for SourceName { Err(error::Error::BadTemplateArgReference) } + fn template_arg_count(&self) -> usize { + 0 + } + fn get_function_arg(&'subs self, _: usize) -> Result<&'subs Type> { Err(error::Error::BadFunctionArgReference) } @@ -4569,6 +4581,10 @@ impl<'subs> ArgScope<'subs, 'subs> for UnnamedTypeName { Err(error::Error::BadTemplateArgReference) } + fn template_arg_count(&self) -> usize { + 0 + } + fn get_function_arg(&'subs self, _: usize) -> Result<&'subs Type> { Err(error::Error::BadFunctionArgReference) } @@ -5173,6 +5189,10 @@ impl<'subs> ArgScope<'subs, 'subs> for TemplateArgs { .map(|v| (v, self)) } + fn template_arg_count(&self) -> usize { + self.0.len() + } + fn get_function_arg(&'subs self, _: usize) -> Result<&'subs Type> { Err(error::Error::BadFunctionArgReference) } @@ -7017,6 +7037,10 @@ impl<'subs> ArgScope<'subs, 'subs> for ClosureTypeName { Err(error::Error::BadTemplateArgReference) } + fn template_arg_count(&self) -> usize { + 0 + } + fn get_function_arg(&'subs self, _: usize) -> Result<&'subs Type> { Err(error::Error::BadFunctionArgReference) } @@ -7245,6 +7269,10 @@ impl<'a> ArgScope<'a, 'a> for WellKnownComponent { Err(error::Error::BadTemplateArgReference) } + fn template_arg_count(&self) -> usize { + 0 + } + fn get_function_arg(&'a self, _: usize) -> Result<&'a Type> { Err(error::Error::BadFunctionArgReference) } diff --git a/tests/tests.rs b/tests/tests.rs index 8e908e1..a694cb7 100755 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -600,3 +600,8 @@ demangles!( _ZN4glslL7combineIhLi2EEEDvmlT0_Li4E_T_DvT0__S1_S3_S3_S3_, "unsigned char __vector((2)*(4)) glsl::combine(unsigned char __vector(2), unsigned char __vector(2), unsigned char __vector(2), unsigned char __vector(2))" ); + +demangles!( + __ZZZN3jkj9dragonbox10to_decimalIfNS0_20default_float_traitsIfEEJNS0_6detail11policy_impl4sign6ignoreENS5_13trailing_zero6removeEEEEDaNS0_23signed_significand_bitsIT_T0_EEjDpT1_ENKUlSC_E_clINS5_26decimal_to_binary_rounding15nearest_to_evenEEESA_SC_ENKUlDpT_E_clIJEEESA_SM_, + "jkj::dragonbox::signed_significand_bits jkj::dragonbox::signed_significand_bits auto jkj::dragonbox::to_decimal, jkj::dragonbox::detail::policy_impl::sign::ignore, jkj::dragonbox::detail::policy_impl::trailing_zero::remove>(jkj::dragonbox::signed_significand_bits >, unsigned int, jkj::dragonbox::detail::policy_impl::sign::ignore, jkj::dragonbox::detail::policy_impl::trailing_zero::remove)::{lambda(auto:2)#1}::operator()(jkj::dragonbox::detail::policy_impl::decimal_to_binary_rounding::nearest_to_even) const::{lambda(auto:1)#1}::operator()<>({lambda(auto:1)#1}) const" +);