Skip to content

Commit 6c46dd0

Browse files
committed
Implement a proof-of-concept @fcall for calling overloaded functions
1 parent 6428e8c commit 6c46dd0

32 files changed

+1678
-0
lines changed

.JuliaFormatter.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
style = "yas"
2+
3+
margin=120
4+
always_for_in=false
5+
whitespace_in_kwargs=false
6+
whitespace_typedefs=false
7+
always_use_return=false
8+
pipe_to_function_call=false
9+
10+
ignore = ["lib"]

.github/dependabot.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
2+
version: 2
3+
updates:
4+
- package-ecosystem: "github-actions"
5+
directory: "/" # Location of package manifests
6+
schedule:
7+
interval: "weekly"

.github/workflows/CI.yml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
tags: ['*']
7+
pull_request:
8+
workflow_dispatch:
9+
concurrency:
10+
# Skip intermediate builds: always.
11+
# Cancel intermediate builds: only if it is a pull request build.
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}
14+
jobs:
15+
test:
16+
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
17+
runs-on: ${{ matrix.os }}
18+
timeout-minutes: 60
19+
permissions: # needed to allow julia-actions/cache to proactively delete old caches that it has created
20+
actions: write
21+
contents: read
22+
strategy:
23+
fail-fast: false
24+
matrix:
25+
version:
26+
- '1.12'
27+
- 'nightly'
28+
os:
29+
- ubuntu-latest
30+
arch:
31+
- x64
32+
steps:
33+
- uses: actions/checkout@v4
34+
- uses: julia-actions/setup-julia@v1
35+
with:
36+
version: ${{ matrix.version }}
37+
arch: ${{ matrix.arch }}
38+
- uses: julia-actions/cache@v1
39+
- uses: julia-actions/julia-buildpkg@v1
40+
- uses: julia-actions/julia-runtest@v1
41+
- uses: julia-actions/julia-processcoverage@v1
42+
- uses: codecov/codecov-action@v4
43+
with:
44+
files: lcov.info
45+
token: ${{ secrets.CODECOV_TOKEN }}
46+
fail_ci_if_error: false
47+
docs:
48+
name: Documentation
49+
runs-on: ubuntu-latest
50+
permissions:
51+
actions: write # needed to allow julia-actions/cache to proactively delete old caches that it has created
52+
contents: write
53+
statuses: write
54+
steps:
55+
- uses: actions/checkout@v4
56+
- uses: julia-actions/setup-julia@v1
57+
with:
58+
version: '1'
59+
- uses: julia-actions/cache@v1
60+
- name: Configure doc environment
61+
shell: julia --project=docs --color=yes {0}
62+
run: |
63+
using Pkg
64+
Pkg.develop(PackageSpec(path=pwd()))
65+
Pkg.instantiate()
66+
- uses: julia-actions/julia-buildpkg@v1
67+
- uses: julia-actions/julia-docdeploy@v1
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
71+
- name: Run doctests
72+
shell: julia --project=docs --color=yes {0}
73+
run: |
74+
using Documenter: DocMeta, doctest
75+
using CppCall
76+
DocMeta.setdocmeta!(CppCall, :DocTestSetup, :(using CppCall); recursive=true)
77+
doctest(CppCall)

.github/workflows/CompatHelper.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: CompatHelper
2+
on:
3+
schedule:
4+
- cron: 0 0 * * *
5+
workflow_dispatch:
6+
jobs:
7+
CompatHelper:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Pkg.add("CompatHelper")
11+
run: julia -e 'using Pkg; Pkg.add("CompatHelper")'
12+
- name: CompatHelper.main()
13+
env:
14+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
15+
COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }}
16+
run: julia -e 'using CompatHelper; CompatHelper.main()'

.github/workflows/TagBot.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: TagBot
2+
on:
3+
issue_comment:
4+
types:
5+
- created
6+
workflow_dispatch:
7+
inputs:
8+
lookback:
9+
default: "3"
10+
permissions:
11+
actions: read
12+
checks: read
13+
contents: write
14+
deployments: read
15+
issues: read
16+
discussions: read
17+
packages: read
18+
pages: read
19+
pull-requests: read
20+
repository-projects: read
21+
security-events: read
22+
statuses: read
23+
jobs:
24+
TagBot:
25+
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: JuliaRegistries/TagBot@v1
29+
with:
30+
token: ${{ secrets.GITHUB_TOKEN }}
31+
ssh: ${{ secrets.DOCUMENTER_KEY }}

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.jl.*.cov
2+
*.jl.cov
3+
*.jl.mem
4+
/Manifest.toml
5+
/docs/Manifest.toml
6+
/docs/build/
7+
LocalPreferences.toml

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Yupei Qi <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Project.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name = "CppCall"
2+
uuid = "f8cf9361-dadf-4655-ba37-7d4cfa6bff53"
3+
version = "0.1.0"
4+
authors = ["Yupei Qi <[email protected]>"]
5+
6+
[deps]
7+
ClangCompiler = "06fc9500-c033-43bc-8ca2-e20da63309d9"
8+
CppInterOp = "13f4c181-cae7-41d1-84d4-2ea5f5fafb15"
9+
10+
[compat]
11+
ClangCompiler = "~0.1"
12+
CppInterOp = "~0.1"
13+
julia = "1.12"
14+
15+
[extras]
16+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
17+
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
18+
19+
[targets]
20+
test = ["Test", "Logging"]

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# CppCall
2+
3+
[![Build Status](https://github.com/Gnimuc/CppCall.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/Gnimuc/CppCall.jl/actions/workflows/CI.yml?query=branch%3Amain)
4+
[![Coverage](https://codecov.io/gh/Gnimuc/CppCall.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/Gnimuc/CppCall.jl)
5+
[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://Gnimuc.github.io/CppCall.jl/stable/)
6+
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://Gnimuc.github.io/CppCall.jl/dev/)

docs/Project.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[deps]
2+
CppCall = "f8cf9361-dadf-4655-ba37-7d4cfa6bff53"
3+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"

docs/make.jl

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using CppCall
2+
using Documenter
3+
4+
DocMeta.setdocmeta!(CppCall, :DocTestSetup, :(using CppCall); recursive=true)
5+
6+
makedocs(;
7+
modules=[CppCall],
8+
authors="Yupei Qi <[email protected]> and contributors",
9+
sitename="CppCall.jl",
10+
format=Documenter.HTML(;
11+
canonical="https://Gnimuc.github.io/CppCall.jl",
12+
edit_link="main",
13+
assets=String[],
14+
),
15+
pages=[
16+
"Home" => "index.md",
17+
],
18+
)
19+
20+
deploydocs(;
21+
repo="github.com/Gnimuc/CppCall.jl",
22+
devbranch="main",
23+
)

docs/src/index.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
```@meta
2+
CurrentModule = CppCall
3+
```
4+
5+
# CppCall
6+
7+
Documentation for [CppCall](https://github.com/Gnimuc/CppCall.jl).
8+
9+
```@index
10+
```
11+
12+
```@autodocs
13+
Modules = [CppCall]
14+
```

examples/overloading.jl

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using CppCall
2+
3+
@include "../test/include"
4+
5+
declare"""#include "overloading.h" """
6+
7+
x = @cppobj cppty"int"
8+
9+
@time @fcall increment(x,x)
10+
11+
@time @fcall increment(x,x)
12+
13+
y = @cppobj cppty"double"
14+
15+
@time @fcall increment(y)
16+
17+
@time @fcall increment(y)
18+
19+
convert(Cdouble, y)
20+
21+
t = cppty"float"
22+
23+
obj = @cppobj t
24+
obj[] = 1
25+
obj[]
26+
27+
28+
z = CppObject{Cint}(1)
29+
30+
zz = Ref(z)
31+
32+
pointer_from_objref(z) == Base.unsafe_convert(Ptr{Cvoid}, zz)
33+
34+
zzz = @cppobj Ptr{cppty"int"}
35+
36+
@fcall increment(zz)
37+
38+
convert(Cint, z)
39+
40+
qualty"unsigned int"cv |> CppCall.CC.dump
41+
42+
@fcall increment(x)
43+
44+
convert(Cuint, y)
45+
46+
qualty"unsigned int" |> CppCall.to_jl
47+
48+
cppty"unsigned int" == CppCall.to_jl(qualty"unsigned int")
49+
50+
obj = CppObject{Cppint,sizeof(Cint)}()
51+
52+
reinterpret(Cint, obj.data)
53+
54+
ntuple(0x00, 0x00, 0x00, 0x00)

src/CppCall.jl

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module CppCall
2+
3+
using ClangCompiler
4+
using ClangCompiler: AbstractClangType, AbstractType, AbstractBuiltinType, AbstractRecordType
5+
using ClangCompiler: QualType, RecordType, EnumType, ElaboratedType, TypedefType
6+
using ClangCompiler: PointerType, LValueReferenceType, RValueReferenceType
7+
using ClangCompiler: FunctionNoProtoType, FunctionProtoType
8+
using ClangCompiler: TypeDecl, NamedDecl
9+
using ClangCompiler: get_type_ptr, get_qual_type, get_decl_type, get_pointee_type, desugar
10+
using ClangCompiler: get_return_type, get_params, get_param_num, get_param_type, get_integer_type
11+
using ClangCompiler: get_pointer_type
12+
using ClangCompiler: add_const, add_volatile, is_const, is_volatile
13+
using ClangCompiler: clty_to_jlty, jlty_to_clty, is_builtin_type
14+
using ClangCompiler: size_of
15+
using ClangCompiler: DeclFinder, get_decl, get_decls
16+
17+
import ClangCompiler as CC
18+
19+
using CppInterOp
20+
using CppInterOp: JLLEnvs
21+
using CppInterOp: create_interpreter, dispose
22+
using CppInterOp: addIncludePath, getptr
23+
using CppInterOp: CXScope
24+
using CppInterOp: invoke
25+
using CppInterOp: getFunctionSignature
26+
27+
include("interpreter.jl")
28+
export initialize, is_valid, terminate
29+
export cppinclude, declare
30+
31+
include("utils.jl")
32+
33+
include("lookup.jl")
34+
export LookupKind, TypeLookup, FuncLookup, FuncOverloadingLookup, lookup
35+
36+
include("types.jl")
37+
export CppType, CppFunc, CppPtr, CppRef, CppObject
38+
39+
include("typemaps.jl")
40+
export cpptypemap
41+
42+
include("convert.jl")
43+
44+
include("macros.jl")
45+
export @declare_str, @include, @cppty_str, @cppobj, @qualty_str
46+
47+
include("registry.jl")
48+
export register, get_instance, get_instance_id
49+
50+
function __init__()
51+
try
52+
register(CppCall, initialize())
53+
catch e
54+
@warn "failed to initialize the default interpreter: $e"
55+
end
56+
57+
atexit() do
58+
for (_, I) in CPPCALL_INSTANCES
59+
terminate(I)
60+
end
61+
end
62+
end
63+
64+
include("call.jl")
65+
export @fcall
66+
67+
end # module

0 commit comments

Comments
 (0)