@@ -174,6 +174,43 @@ static void sanitize_symbol_name(std::string &name)
174174 }
175175}
176176
177+ static std::string get_parent_name (vpiHandle parent_h)
178+ {
179+ std::string parent_name;
180+ if (auto p = vpi_get_str (vpiFullName, parent_h)) {
181+ parent_name = p;
182+ } else if (auto p = vpi_get_str (vpiName, parent_h)) {
183+ parent_name = p;
184+ } else if (auto p = vpi_get_str (vpiDefName, parent_h)) {
185+ parent_name = p;
186+ }
187+ return parent_name;
188+ }
189+
190+ // Warning: Takes ownership of `parent_h` and releases it.
191+ static void find_ancestor_name (vpiHandle parent_h, std::string &name, std::string &parent_name)
192+ {
193+
194+ while (!parent_name.empty ()) {
195+ parent_name = parent_name + " ." ;
196+ if ((name.rfind (parent_name) != std::string::npos)) {
197+ name = name.substr (name.rfind (parent_name) + parent_name.size ());
198+ break ;
199+ } else {
200+ auto old_parent_h = parent_h;
201+ parent_h = vpi_handle (vpiParent, parent_h);
202+ vpi_release_handle (old_parent_h);
203+
204+ if (parent_h) {
205+ parent_name = get_parent_name (parent_h);
206+ } else {
207+ parent_name.clear ();
208+ }
209+ }
210+ }
211+ vpi_release_handle (parent_h);
212+ }
213+
177214static std::string get_name (vpiHandle obj_h, bool prefer_full_name = false )
178215{
179216 auto first_check = prefer_full_name ? vpiFullName : vpiName;
@@ -186,8 +223,29 @@ static std::string get_name(vpiHandle obj_h, bool prefer_full_name = false)
186223 } else if (auto s = vpi_get_str (last_check, obj_h)) {
187224 name = s;
188225 }
189- if (name.rfind (' .' ) != std::string::npos) {
190- name = name.substr (name.rfind (' .' ) + 1 );
226+ // We are looking for the ancestor name to use it as a delimeter
227+ // when stripping the name of the current node.
228+ // We used to strip the name by searching for "." in it, but this
229+ // approach didn't work for the names whith "." as an escaped
230+ // character.
231+ vpiHandle parent_h = vpi_handle (vpiParent, obj_h);
232+
233+ if (parent_h) {
234+ std::string parent_name;
235+ parent_name = get_parent_name (parent_h);
236+
237+ if (parent_name.empty ()) {
238+ // Nodes of certain types, like param_assign, don't have
239+ // a name, so we need to look further for the ancestor.
240+ auto old_parent_h = parent_h;
241+ parent_h = vpi_handle (vpiParent, parent_h);
242+ vpi_release_handle (old_parent_h);
243+
244+ if (parent_h) {
245+ parent_name = get_parent_name (parent_h);
246+ }
247+ }
248+ find_ancestor_name (parent_h, name, parent_name);
191249 }
192250 sanitize_symbol_name (name);
193251 return name;
0 commit comments