CppInterOp
C++ Language Interoperability Layer
Loading...
Searching...
No Matches
Unwrap.h
Go to the documentation of this file.
1//===--- Unwrap.h - wrap/unwrap for opaque handle types ---------*- C++ -*-===//
2//
3// Part of the compiler-research project, under the Apache License v2.0 with
4// LLVM Exceptions.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Internal helpers for converting between CppInterOp's typed opaque handles
10// (DeclRef, TypeRef, FuncRef, ObjectRef, InterpRef and their const variants)
11// and raw pointers to the underlying Clang AST nodes. Not part of the public
12// API — included only from lib/CppInterOp/*.cpp and unit-test TUs that need
13// to materialise typed pointers for testing the primitives themselves.
14//
15// Downstream consumers (cppyy, xeus-cpp) call the public C++ / C APIs and
16// never need wrap/unwrap; this header is deliberately not reachable from
17// include/CppInterOp/CppInterOp.h.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef CPPINTEROP_LIB_UNWRAP_H
22#define CPPINTEROP_LIB_UNWRAP_H
23
25
26namespace Cpp {
27
28// wrap<Handle>(ptr) materialises a handle from a raw pointer.
29// unwrap<T>(handle) extracts the raw pointer, statically cast to T*.
30// Const handles yield const T* via their const void* data member.
31
32template <typename Handle> Handle wrap(void* P) { return Handle{P}; }
33
34// Const handles from const pointers — no const_cast needed.
35template <typename Handle> Handle wrap(const void* P) { return Handle{P}; }
36
37// Mutable handles — unwrap to non-const pointer.
38template <typename T> T* unwrap(DeclRef H) { return static_cast<T*>(H.data); }
39template <typename T> T* unwrap(TypeRef H) { return static_cast<T*>(H.data); }
40template <typename T> T* unwrap(FuncRef H) { return static_cast<T*>(H.data); }
41template <typename T> T* unwrap(ObjectRef H) { return static_cast<T*>(H.data); }
42template <typename T> T* unwrap(InterpRef H) { return static_cast<T*>(H.data); }
43
44// Const handles — unwrap to const pointer.
45template <typename T> const T* unwrap(ConstDeclRef H) {
46 return static_cast<const T*>(H.data);
47}
48template <typename T> const T* unwrap(ConstTypeRef H) {
49 return static_cast<const T*>(H.data);
50}
51template <typename T> const T* unwrap(ConstFuncRef H) {
52 return static_cast<const T*>(H.data);
53}
54
55} // namespace Cpp
56
57#endif // CPPINTEROP_LIB_UNWRAP_H
Definition Box.h:70
T * unwrap(DeclRef H)
Definition Unwrap.h:38
Handle wrap(void *P)
Definition Unwrap.h:32