I mean I now know I cannot compare the type info in this way, but are there any 
already implemented functions that let you retrieve the vtable and the address 
of the base class vtable from a type that someone could point me to?

________________________________
From: Gcc <gcc-bounces~tdebock=drwuk....@gcc.gnu.org> on behalf of Thomas de 
Bock via Gcc <gcc@gcc.gnu.org>
Sent: 06 July 2025 16:46:16
To: gcc@gcc.gnu.org
Subject: [ext] building generic tree to compare base classes at runtime

Hello, currently working on a tiny experimental compiler optimization in the 
C++ frontend. Trying to compare the base classes of 2 types at runtime, then if 
they are equal, statically cast the second operand's type to the first and the 
tree should return the now-casted ptr of the second operand, otherwise the tree 
should return a null ptr. I read the docs on the website describing the trees, 
but I did not find information on comparing runtime type info. Currently I am 
doing:


//context:

build_dynamic_cast_1 (location_t loc, tree type, tree expr,
              tsubst_flags_t complain)
...

      target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
      static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
      td2 = get_tinfo_decl (target_type);
      td2 = cp_build_addr_expr (td2, complain);
      td3 = get_tinfo_decl (static_type);
      td3 = cp_build_addr_expr (td3, complain);
...
// `expr` is what this code attempts to statically cast to the type of `type`, 
if their base type matches up

      tree cast_expr = cp_convert(type, build_static_cast(loc, type, expr, 
complain), complain);
      tree cond = build2 (NE_EXPR, boolean_type_node, td2, td3);
      tree null_ptr = cp_convert (type, nullptr_node, complain);
      tree result = build3 (COND_EXPR, type, cond, null_ptr, cast_expr);
      result = cp_convert(type, result, complain);
      return build_if_nonnull(expr, result, complain);

the output GIMPLE and assembly don't look anything like what I am trying to 
achieve though. Does anyone know how the base classes should be compared from 
the runtime type info, should I be doing it manually with MEM_REF instead? I 
was unable to find documentation on this, as well as most of the tree building 
functions, sorry if stupid/unclear question.


This e-mail and any attachments may contain information that is confidential 
and proprietary and otherwise protected from disclosure. If you are not the 
intended recipient of this e-mail, do not read, duplicate or redistribute it by 
any means. Please immediately delete it and any attachments and notify the 
sender that you have received it by mistake. Unintended recipients are 
prohibited from taking action on the basis of information in this e-mail or any 
attachments. The DRW Companies make no representations that this e-mail or any 
attachments are free of computer viruses or other defects.

Reply via email to