Skip to content

Commit

Permalink
Current mainline head, after cl/573041979 (clif_type_casters tests) w…
Browse files Browse the repository at this point in the history
…as submitted.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Oct 13, 2023
1 parent bac1740 commit 33427b3
Show file tree
Hide file tree
Showing 20 changed files with 886 additions and 12 deletions.
4 changes: 4 additions & 0 deletions clif/backend/matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2963,6 +2963,10 @@ const clang::FunctionDecl* ClifMatcher::SpecializeFunctionTemplate(
false, // No partial overloading
#if PYCLIF_LLVM_VERSION_MAJOR > 16
/*AggregateDeductionCandidate=*/false,
#endif
#if PYCLIF_LLVM_VERSION_MAJOR > 18
/*ObjectType=*/QualType(),
/*ObjectClassification=*/clang::Expr::Classification(),
#endif
[&](clang::ArrayRef<QualType> param_types) {
// For template parameters that aren't instantiation
Expand Down
2 changes: 1 addition & 1 deletion clif/pybind11/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def _generate_iterator(
if function_lib.has_bytes_return(func_decl):
template_param = '<py::return_value_policy::_return_as_bytes>'
yield (
f'{class_name}.def("__iter__", [](const {class_decl.name.cpp_name} &s)'
f'{class_name}.def("__iter__", []({class_decl.name.cpp_name} &s)'
f'{{ return py::make_iterator{template_param}(s.begin(), s.end()); }}, '
'py::keep_alive<0, 1>());')

Expand Down
3 changes: 1 addition & 2 deletions clif/pybind11/clif_type_casters.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,10 @@ struct clif_type_caster<
}

template <typename T_>
using cast_op_type = movable_cast_op_type<T_>;
using cast_op_type = ::pybind11::detail::cast_op_type<T_>;

operator Type*() { return value; }
operator Type&() { return *value; }
operator Type&&() && { return std::move(*value); }

private:
Type* value;
Expand Down
9 changes: 7 additions & 2 deletions clif/pybind11/function_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ def __init__(self, param: ast_pb2.ParamDecl, param_name: str,
self.function_argument = f'std::move({self.gen_name})'
elif ptype.cpp_raw_pointer:
if (not ptype.cpp_toptr_conversion and ctype.endswith('*')
and ptype.cpp_has_public_dtor and not ptype.cpp_abstract
and ptype.cpp_has_def_ctor):
and ptype.cpp_abstract and ptype.cpp_touniqptr_conversion):
t = ctype[:-1]
self.cpp_type = f'::std::unique_ptr<{t}>'
self.function_argument = f'{param_name}.get()'
elif (not ptype.cpp_toptr_conversion and ctype.endswith('*')
and ptype.cpp_has_public_dtor and not ptype.cpp_abstract
and ptype.cpp_has_def_ctor):
# Create a copy on stack and pass its address.
# For compatibility with the original C API code generator.
self.cpp_type = ctype[:-1]
Expand Down
9 changes: 4 additions & 5 deletions clif/pybind11/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def generate_header(self,
includes = set()
for decl in ast.decls:
includes.add(decl.cpp_file)
for include in self._ast.usertype_includes:
includes.add(include)
for include in self._pybind11_only_includes:
includes.add(include)
yield '#include "third_party/pybind11/include/pybind11/smart_holder.h"'
yield '#include "clif/python/postconv.h"'
for include in includes:
Expand Down Expand Up @@ -269,11 +273,6 @@ def _generate_headlines(self):
yield '#include "third_party/pybind11/include/pybind11/stl.h"'
yield '#include "third_party/pybind11/include/pybind11/type_caster_pyobject_ptr.h"' # pylint: disable=long-line
yield ''
yield '// See pybind11_protobuf/proto_caster_impl.h'
yield '#if !defined(PYBIND11_PROTOBUF_UNSAFE)'
yield I + '#define PYBIND11_PROTOBUF_UNSAFE 1'
yield '#endif'
yield ''
for include in includes:
yield f'#include "{include}"'
yield f'#include "{self._header_path}"'
Expand Down
5 changes: 3 additions & 2 deletions clif/pybind11/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def _generate_cpp_get(
var_decl: ast_pb2.VarDecl,
class_decl: ast_pb2.ClassDecl,
generate_comma: bool = False,
is_unproperty: bool = False,
) -> Generator[str, None, None]:
"""Generate lambda expressions for getters."""
reference_internal = True
Expand All @@ -72,7 +73,7 @@ def _generate_cpp_get(
ret = f'self.{function_name}()'
else:
ret = f'self.{var_decl.name.cpp_name}'
if _convert_ptr_to_ref(var_decl):
if not is_unproperty and _convert_ptr_to_ref(var_decl):
ret = f'*{ret}'
reference_internal = False
return_value_policy = function_lib.generate_return_value_policy_for_type(
Expand Down Expand Up @@ -228,7 +229,7 @@ def _generate_unproperty(
) -> Generator[str, None, None]:
"""Generates functions to expose attributes instead of exposing directly."""
yield f'{class_name}.def("{var_decl.cpp_get.name.native}",'
yield from _generate_cpp_get(var_decl, class_decl)
yield from _generate_cpp_get(var_decl, class_decl, is_unproperty=True)
if var_decl.HasField('cpp_set'):
yield f'{class_name}.def("{var_decl.cpp_set.name.native}",'
yield from _generate_cpp_set_without_setter(var_decl, class_decl)
6 changes: 6 additions & 0 deletions clif/testing/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ struct NestedAttributes {
}
};

struct SomePointee {};

struct SomePointerOwner {
const SomePointee* some_pointer = nullptr;
};

// Use the duplicated namespace `clif_testing` for testing purposes.
namespace clif_testing {

Expand Down
132 changes: 132 additions & 0 deletions clif/testing/copy_move_types_custom_from_as.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CLIF_TESTING_COPY_MOVE_TYPES_CUSTOM_FROM_AS_H_
#define CLIF_TESTING_COPY_MOVE_TYPES_CUSTOM_FROM_AS_H_

#include <memory>
#include <optional>
#include <string>

#include "clif/testing/copy_move_types_library.h"

namespace clif_testing::copy_move_types_custom_from_as {

using copy_move_types_library::CopyMoveType;

// Need to hide inheritance to get the correct `Clif_PyObjAs()`.
// For clif_type_casters that use `Type` to hold the value:
// http://google3/clif/pybind11/clif_type_casters.h;l=177;rcl=570854707
class FromCRAsPCopyMoveType : private CopyMoveType {
public:
FromCRAsPCopyMoveType() = default;
explicit FromCRAsPCopyMoveType(const CopyMoveType& obj)
: CopyMoveType{obj} {}
using CopyMoveType::GetTrace;
};

inline FromCRAsPCopyMoveType MakeFromCRAsPCopyMoveType(
const CopyMoveType& class_obj) {
return FromCRAsPCopyMoveType{class_obj};
}

inline std::string GetTraceFromCRAsPCopyMoveType(
const FromCRAsPCopyMoveType& cfa_obj) {
return cfa_obj.GetTrace();
}

// For clif_type_casters that use `std::optional<Type>` to hold the value:
// http://google3/clif/pybind11/clif_type_casters.h;l=193;rcl=570854707
class FromCRAsOpCopyMoveType : private CopyMoveType {
public:
FromCRAsOpCopyMoveType() = default;
explicit FromCRAsOpCopyMoveType(const CopyMoveType& obj)
: CopyMoveType{obj} {}
using CopyMoveType::GetTrace;
};

inline FromCRAsOpCopyMoveType MakeFromCRAsOpCopyMoveType(
const CopyMoveType& class_obj) {
return FromCRAsOpCopyMoveType{class_obj};
}

inline std::string GetTraceFromCRAsOpCopyMoveType(
std::optional<FromCRAsOpCopyMoveType> cfa_obj) {
return cfa_obj.value().GetTrace();
}

// For clif_type_casters that use `Type*` to hold the value:
// http://google3/clif/pybind11/clif_type_casters.h;l=219;rcl=570854707
class FromCRAsPPCopyMoveType : private CopyMoveType {
public:
explicit FromCRAsPPCopyMoveType(const CopyMoveType& obj)
: CopyMoveType{obj} {}
using CopyMoveType::GetTrace;
};

inline FromCRAsPPCopyMoveType MakeFromCRAsPPCopyMoveType(
const CopyMoveType& class_obj) {
return FromCRAsPPCopyMoveType{class_obj};
}

inline std::string GetTraceFromCRAsPPCopyMoveType(
const FromCRAsPPCopyMoveType& cfa_obj) {
return cfa_obj.GetTrace();
}

// For clif_type_casters that use `std::unique_ptr<Type>` to hold the value:
// http://google3/clif/pybind11/clif_type_casters.h;l=247;rcl=570854707
class FromCRAsUpCopyMoveType : private CopyMoveType {
public:
FromCRAsUpCopyMoveType() = default;
explicit FromCRAsUpCopyMoveType(const CopyMoveType& obj)
: CopyMoveType{obj} {}
using CopyMoveType::GetTrace;
};

inline FromCRAsUpCopyMoveType MakeFromCRAsUpCopyMoveType(
const CopyMoveType& class_obj) {
return FromCRAsUpCopyMoveType{class_obj};
}

inline std::string GetTraceFromCRAsUpCopyMoveType(
std::unique_ptr<FromCRAsUpCopyMoveType> cfa_obj) {
return cfa_obj->GetTrace();
}

// For clif_type_casters that use `std::shared_ptr<Type>` to hold the value:
// http://google3/clif/pybind11/clif_type_casters.h;l=285;rcl=570854707
class FromCRAsSpCopyMoveType : private CopyMoveType {
public:
FromCRAsSpCopyMoveType() = default;
explicit FromCRAsSpCopyMoveType(const CopyMoveType& obj)
: CopyMoveType{obj} {}
using CopyMoveType::GetTrace;
};

inline FromCRAsSpCopyMoveType MakeFromCRAsSpCopyMoveType(
const CopyMoveType& class_obj) {
return FromCRAsSpCopyMoveType{class_obj};
}

inline std::string GetTraceFromCRAsSpCopyMoveType(
FromCRAsSpCopyMoveType* cfa_obj) {
return cfa_obj->GetTrace();
}

} // namespace clif_testing::copy_move_types_custom_from_as

#endif // CLIF_TESTING_COPY_MOVE_TYPES_CUSTOM_FROM_AS_H_
126 changes: 126 additions & 0 deletions clif/testing/copy_move_types_library.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CLIF_TESTING_COPY_MOVE_TYPES_LIBRARY_H_
#define CLIF_TESTING_COPY_MOVE_TYPES_LIBRARY_H_

#include <string>
#include <string_view>

namespace clif_testing::copy_move_types_library {

// Intentionally not using inheritance, to ensure each type is completely
// self-contained and does not trigger any inheritance-related functionality.

class CopyMoveType {
private:
std::string trace_;

public:
const std::string &GetTrace() const { return trace_; }

explicit CopyMoveType(std::string_view trace = "DefaultCtor")
: trace_(trace) {}

CopyMoveType(const CopyMoveType &other) { trace_ = other.trace_ + "_CpCtor"; }

CopyMoveType &operator=(const CopyMoveType &rhs) {
trace_ = rhs.trace_ + "_CpLhs";
return *this;
}

CopyMoveType(CopyMoveType &&other) {
trace_ = other.trace_ + "_MvCtorTo";
other.trace_ += "_MvCtorFrom";
}

CopyMoveType &operator=(CopyMoveType &&rhs) {
trace_ = rhs.trace_ + "_MvLhs";
rhs.trace_ += "_MvRhs";
return *this;
}
};

class CopyOnlyType {
private:
std::string trace_;

public:
const std::string &GetTrace() const { return trace_; }

explicit CopyOnlyType(std::string_view trace = "DefaultCtor")
: trace_(trace) {}

CopyOnlyType(const CopyOnlyType &other) { trace_ = other.trace_ + "_CpCtor"; }

CopyOnlyType &operator=(const CopyOnlyType &rhs) {
trace_ = rhs.trace_ + "_CpLhs";
return *this;
}

CopyOnlyType(CopyOnlyType &&) = delete;

CopyOnlyType &operator=(CopyOnlyType &&) = delete;
};

class MoveOnlyType {
private:
std::string trace_;

public:
const std::string &GetTrace() const { return trace_; }

explicit MoveOnlyType(std::string_view trace = "DefaultCtor")
: trace_(trace) {}

MoveOnlyType(const MoveOnlyType &) = delete;

MoveOnlyType &operator=(const MoveOnlyType &) = delete;

MoveOnlyType(MoveOnlyType &&other) {
trace_ = other.trace_ + "_MvCtorTo";
other.trace_ += "_MvCtorFrom";
}

MoveOnlyType &operator=(MoveOnlyType &&rhs) {
trace_ = rhs.trace_ + "_MvLhs";
rhs.trace_ += "_MvRhs";
return *this;
}
};

class StayPutType {
private:
std::string trace_;

public:
const std::string &GetTrace() const { return trace_; }

explicit StayPutType(std::string_view trace = "DefaultCtor")
: trace_(trace) {}

StayPutType(const StayPutType &) = delete;

StayPutType &operator=(const StayPutType &) = delete;

StayPutType(StayPutType &&) = delete;

StayPutType &operator=(StayPutType &&) = delete;
};

} // namespace clif_testing::copy_move_types_library

#endif // CLIF_TESTING_COPY_MOVE_TYPES_LIBRARY_H_
Loading

0 comments on commit 33427b3

Please sign in to comment.