Skip to content

Return pointer for static members.#1

Open
jianlingzhong wants to merge 1 commit intojank-lang:mainfrom
jianlingzhong:main
Open

Return pointer for static members.#1
jianlingzhong wants to merge 1 commit intojank-lang:mainfrom
jianlingzhong:main

Conversation

@jianlingzhong
Copy link
Copy Markdown

@jianlingzhong jianlingzhong commented Mar 1, 2026

Code should be able handle regular static members and inline const static int member correctly:

Test cpp code

auto* I = Cpp::CreateInterpreter();
std::vector<Decl*> Decls, SubDecls;

std::string code = R"(
  struct MyStruct {
    static const int my_inline_static_var{10086}; 
    static int my_outline_static_var; 
  };

  MyStruct::my_outline_static_var = 1964;
)";

TestUtils::GetAllTopLevelDecls(code, Decls);
full code
#include "clang/Interpreter/CppInterOp.h"
#include "unittests/CppInterOp/Utils.h"
#include "clang/AST/Decl.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include <iostream>

using namespace clang;

int main() {
  auto* I = Cpp::CreateInterpreter();
  std::vector<Decl*> Decls, SubDecls;
  
  std::string code = R"(
    struct MyStruct {
      static const int my_inline_static_var{10086}; 
      static int my_outline_static_var; 
    };

    MyStruct::my_outline_static_var = 1964;
  )";
  
  TestUtils::GetAllTopLevelDecls(code, Decls);
  if(Decls.size() > 0) {
      TestUtils::GetAllSubDecls(Decls[0], SubDecls);
      for(auto* decl : SubDecls) {
          if (decl && decl->getKind() == Decl::Var) {
              auto scope = (Cpp::TCppScope_t)decl;
              auto* VD = cast<VarDecl>(decl);
              std::string var_name = VD->getNameAsString();
              std::string wrapper_name = "wrapper_" + var_name;
              
              auto aot_call = Cpp::MakeAotCallable(scope, wrapper_name);
              if (aot_call.isValid()) {
                 std::cout << "\n========================================\n";
                 std::cout << "IR Dump for: " << var_name << "\n";
                 std::cout << "========================================\n";
                 llvm::Module* M = (llvm::Module*)aot_call.getModule();
                 M->print(llvm::outs(), nullptr);
              } else {
                 std::cout << "Failed to generate AOT wrapper for " << var_name << "\n";
              }
          }
      }
  }
}

output:

inline const static int

; Function Attrs: alwaysinline mustprogress nounwind ssp uwtable(sync)
define linkonce_odr void @wrapper_my_inline_static_var(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef nonnull %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8
  store i32 %1, ptr %6, align 4
  store ptr %2, ptr %7, align 8
  store ptr %3, ptr %8, align 8
  %9 = load ptr, ptr %8, align 8
  store i32 10086, ptr %9, align 4  ;; <= wrote 10086 directly and didn't take the address of the variable, 
  ret void
}

out of line const int

define linkonce_odr void @wrapper_my_outline_static_var(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef nonnull %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8
  store i32 %1, ptr %6, align 4
  store ptr %2, ptr %7, align 8
  store ptr %3, ptr %8, align 8
  %9 = load ptr, ptr %8, align 8
  store ptr @_ZN8MyStruct21my_outline_static_varE, ptr %9, align 8 ;; <= access ptr
  ret void
}
full ir
========================================
IR Dump for: my_inline_static_var
========================================
; ModuleID = 'incr_module_4'
source_filename = "incr_module_4"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx15.0.0"

@.str = private unnamed_addr constant [23 x i8] c"__cling__ptrcheck(off)\00", section "llvm.metadata"
@.str.1 = private unnamed_addr constant [13 x i8] c"input_line_2\00", section "llvm.metadata"
@llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @wrapper_my_inline_static_var, ptr @.str, ptr @.str.1, i32 3, ptr null }], section "llvm.metadata"
@llvm.used = appending global [1 x ptr] [ptr @wrapper_my_inline_static_var], section "llvm.metadata"

; Function Attrs: alwaysinline mustprogress nounwind ssp uwtable(sync)
define linkonce_odr void @wrapper_my_inline_static_var(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef nonnull %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8
  store i32 %1, ptr %6, align 4
  store ptr %2, ptr %7, align 8
  store ptr %3, ptr %8, align 8
  %9 = load ptr, ptr %8, align 8
  store i32 10086, ptr %9, align 4
  ret void
}

attributes #0 = { alwaysinline mustprogress nounwind ssp uwtable(sync) "frame-pointer"="non-leaf-no-reserve" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+altnzcv,+ccdp,+ccidx,+ccpp,+complxnum,+crc,+dit,+dotprod,+flagm,+fp-armv8,+fp16fml,+fptoint,+fullfp16,+jsconv,+lse,+neon,+pauth,+perfmon,+predres,+ras,+rcpc,+rdm,+sb,+sha2,+sha3,+specrestrict,+ssbs,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8a" }

!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}

!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 26, i32 2]}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 8, !"PIC Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 1}
!4 = !{i32 7, !"frame-pointer", i32 4}
!5 = !{!"clang version 22.1.0-rc1 (https://github.com/jank-lang/llvm-project.git 8164f1a0c17b192e133817436bdb07598b7402a3)"}
VarDecl code generation for my_outline_static_var: Static=1 MissingDef=1 ConstUsable=0 MissingConstDef=0

========================================
IR Dump for: my_outline_static_var
========================================
; ModuleID = 'incr_module_5'
source_filename = "incr_module_5"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx15.0.0"

@_ZN8MyStruct21my_outline_static_varE = external global i32, align 4
@.str = private unnamed_addr constant [23 x i8] c"__cling__ptrcheck(off)\00", section "llvm.metadata"
@.str.1 = private unnamed_addr constant [13 x i8] c"input_line_3\00", section "llvm.metadata"
@llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @wrapper_my_outline_static_var, ptr @.str, ptr @.str.1, i32 3, ptr null }], section "llvm.metadata"
@llvm.used = appending global [1 x ptr] [ptr @wrapper_my_outline_static_var], section "llvm.metadata"

; Function Attrs: alwaysinline mustprogress nounwind ssp uwtable(sync)
define linkonce_odr void @wrapper_my_outline_static_var(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef nonnull %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8
  store i32 %1, ptr %6, align 4
  store ptr %2, ptr %7, align 8
  store ptr %3, ptr %8, align 8
  %9 = load ptr, ptr %8, align 8
  store ptr @_ZN8MyStruct21my_outline_static_varE, ptr %9, align 8
  ret void
}

attributes #0 = { alwaysinline mustprogress nounwind ssp uwtable(sync) "frame-pointer"="non-leaf-no-reserve" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+altnzcv,+ccdp,+ccidx,+ccpp,+complxnum,+crc,+dit,+dotprod,+flagm,+fp-armv8,+fp16fml,+fptoint,+fullfp16,+jsconv,+lse,+neon,+pauth,+perfmon,+predres,+ras,+rcpc,+rdm,+sb,+sha2,+sha3,+specrestrict,+ssbs,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8a" }

!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}

!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 26, i32 2]}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 8, !"PIC Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 1}
!4 = !{i32 7, !"frame-pointer", i32 4}
!5 = !{!"clang version 22.1.0-rc1 (https://github.com/jank-lang/llvm-project.git 8164f1a0c17b192e133817436bdb07598b7402a3)"}

Except for the case where there is no pointer address available.
jeaye pushed a commit that referenced this pull request Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant