Commit 230c81c4 authored by Kirill Smelkov's avatar Kirill Smelkov

libgolang: Provide Nil as alias for std::nullptr_t

This continues 60f6db6f (libgolang: Provide nil as alias for nullptr and
NULL): I've tried to compile pygolang with Clang on my Debian 10
workstation and got:

    $ CC=clang CXX=clang++ python setup.py build_dso -i

    In file included from ./golang/fmt.h:32:
    ./golang/libgolang.h:381:11: error: unknown type name 'nullptr_t'; did you mean 'std::nullptr_t'?
    constexpr nullptr_t nil = nullptr;
              ^~~~~~~~~
              std::nullptr_t
    /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8/bits/c++config.h:242:29: note: 'std::nullptr_t' declared here
      typedef decltype(nullptr)     nullptr_t;
                                    ^
    :
    In file included from ./golang/context.h
    In file included from golang/runtime/libgolang.cpp:30:
    ./golang/libgolang.h:381:11: error: unknown type name 'nullptr_t'; did you mean 'std::nullptr_t'?
    constexpr nullptr_t nil = nullptr;
              ^~~~~~~~~
              std::nullptr_t
    /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8/bits/c++config.h:242:29: note: 'std::nullptr_t' declared here
      typedef decltype(nullptr)     nullptr_t;
                                    ^
    :39:
    ./golang/libgolang.h:381:11: error: unknown type In file included from golang/fmt.cpp:25:
    In file included from ./golang/fmt.h:32:
    ./golang/libgolang.h:421:17: error: unknown type name 'nullptr_t'; did you mean 'std::nullptr_t'?
        inline chan(nullptr_t) { _ch = nil; }
                    ^~~~~~~~~
                    std::nullptr_t

    ...

It seems with GCC and Clang under macOS nullptr_t is automatically provided in
builtin namespace, while with older Clang on Linux (clang version 7.0.1-8) only
in std:: namespace - rightfully as nullptr_t is described to be present there:

https://en.cppreference.com/w/cpp/types/nullptr_t

This way we either have to correct all occurrences of nullptr_t to
std::nullptr_t, or do something similar with providing nil under golang:: .

To reduce noise I prefer the later and let it be named as Nil.
parent b5a2f9dc
...@@ -34,7 +34,7 @@ See also https://blog.golang.org/context for overview. ...@@ -34,7 +34,7 @@ See also https://blog.golang.org/context for overview.
(*) not provided in Go version. (*) not provided in Go version.
""" """
from golang cimport chan, structZ, error, refptr, interface from golang cimport chan, structZ, error, refptr, interface, Nil
from golang cimport cxx from golang cimport cxx
from libcpp.utility cimport pair from libcpp.utility cimport pair
...@@ -44,7 +44,7 @@ from libcpp.utility cimport pair ...@@ -44,7 +44,7 @@ from libcpp.utility cimport pair
cdef extern from "golang/libgolang.h" namespace "golang" nogil: cdef extern from "golang/libgolang.h" namespace "golang" nogil:
cppclass cancelFunc "golang::func<void()>": cppclass cancelFunc "golang::func<void()>":
void operator() () void operator() ()
void operator= (nullptr_t) void operator= (Nil)
cdef extern from "golang/context.h" namespace "golang::context" nogil: cdef extern from "golang/context.h" namespace "golang::context" nogil:
cppclass _Context: cppclass _Context:
......
...@@ -44,7 +44,7 @@ In addition to Cython/nogil API, golang.pyx provides runtime for golang.py: ...@@ -44,7 +44,7 @@ In addition to Cython/nogil API, golang.pyx provides runtime for golang.py:
""" """
from libcpp cimport nullptr_t, nullptr as nil # golang::nil = nullptr from libcpp cimport nullptr_t as Nil, nullptr as nil # golang::nil = nullptr
from libcpp.utility cimport pair from libcpp.utility cimport pair
from libc.stdint cimport uint64_t from libc.stdint cimport uint64_t
cdef extern from *: cdef extern from *:
...@@ -88,9 +88,9 @@ cdef extern from "golang/libgolang.h" namespace "golang" nogil: ...@@ -88,9 +88,9 @@ cdef extern from "golang/libgolang.h" namespace "golang" nogil:
unsigned cap() const unsigned cap() const
# compare wrt nil; =nil # compare wrt nil; =nil
cbool operator==(nullptr_t) const cbool operator==(Nil) const
cbool operator!=(nullptr_t) const cbool operator!=(Nil) const
void operator=(nullptr_t) void operator=(Nil)
# for tests # for tests
_chan *_rawchan() const _chan *_rawchan() const
...@@ -127,9 +127,9 @@ cdef extern from "golang/libgolang.h" namespace "golang" nogil: ...@@ -127,9 +127,9 @@ cdef extern from "golang/libgolang.h" namespace "golang" nogil:
# memory management of C++ nogil classes # memory management of C++ nogil classes
cppclass refptr[T]: cppclass refptr[T]:
# compare wrt nil; =nil # compare wrt nil; =nil
cbool operator== (nullptr_t) const cbool operator== (Nil) const
cbool operator!= (nullptr_t) const cbool operator!= (Nil) const
void operator= (nullptr_t) const void operator= (Nil) const
# compare wrt refptr; =refptr # compare wrt refptr; =refptr
# XXX workaround for https://github.com/cython/cython/issues/1357: # XXX workaround for https://github.com/cython/cython/issues/1357:
......
...@@ -366,6 +366,7 @@ LIBGOLANG_API extern void (*_tblockforever)(void); ...@@ -366,6 +366,7 @@ LIBGOLANG_API extern void (*_tblockforever)(void);
#ifdef __cplusplus #ifdef __cplusplus
#include <atomic> #include <atomic>
#include <cstddef>
#include <exception> #include <exception>
#include <functional> #include <functional>
#include <initializer_list> #include <initializer_list>
...@@ -377,8 +378,9 @@ LIBGOLANG_API extern void (*_tblockforever)(void); ...@@ -377,8 +378,9 @@ LIBGOLANG_API extern void (*_tblockforever)(void);
namespace golang { namespace golang {
// nil is alias for nullptr and NULL. // nil is alias for nullptr and NULL; Nil - for std::nullptr_t;
constexpr nullptr_t nil = nullptr; using Nil = std::nullptr_t;
constexpr Nil nil = nullptr;
// string is alias for std::string. // string is alias for std::string.
using string = std::string; using string = std::string;
...@@ -418,8 +420,8 @@ public: ...@@ -418,8 +420,8 @@ public:
inline ~chan() { _chanxdecref(_ch); _ch = nil; } inline ~chan() { _chanxdecref(_ch); _ch = nil; }
// = nil // = nil
inline chan(nullptr_t) { _ch = nil; } inline chan(Nil) { _ch = nil; }
inline chan& operator=(nullptr_t) { _chanxdecref(_ch); _ch = nil; return *this; } inline chan& operator=(Nil) { _chanxdecref(_ch); _ch = nil; return *this; }
// copy // copy
inline chan(const chan& from) { _ch = from._ch; _chanxincref(_ch); } inline chan(const chan& from) { _ch = from._ch; _chanxincref(_ch); }
inline chan& operator=(const chan& from) { inline chan& operator=(const chan& from) {
...@@ -462,12 +464,12 @@ public: ...@@ -462,12 +464,12 @@ public:
} }
// length/capacity // length/capacity
inline unsigned len() const { return _chanlen(_ch); } inline unsigned len() const { return _chanlen(_ch); }
inline unsigned cap() const { return _chancap(_ch); } inline unsigned cap() const { return _chancap(_ch); }
// compare wrt nil // compare wrt nil
inline bool operator==(nullptr_t) const { return (_ch == nil); } inline bool operator==(Nil) const { return (_ch == nil); }
inline bool operator!=(nullptr_t) const { return (_ch != nil); } inline bool operator!=(Nil) const { return (_ch != nil); }
// compare wrt chan // compare wrt chan
inline bool operator==(const chan<T>& ch2) const { return (_ch == ch2._ch); } inline bool operator==(const chan<T>& ch2) const { return (_ch == ch2._ch); }
...@@ -587,8 +589,8 @@ public: ...@@ -587,8 +589,8 @@ public:
} }
// = nil // = nil
inline refptr(nullptr_t) { _obj = nil; } inline refptr(Nil) { _obj = nil; }
inline refptr& operator=(nullptr_t) { inline refptr& operator=(Nil) {
if (_obj != nil) if (_obj != nil)
_obj->decref(); _obj->decref();
_obj = nil; _obj = nil;
...@@ -632,8 +634,8 @@ public: ...@@ -632,8 +634,8 @@ public:
friend refptr<T> newref<T> (T *_obj); friend refptr<T> newref<T> (T *_obj);
// compare wrt nil // compare wrt nil
inline bool operator==(nullptr_t) const { return (_obj == nil); } inline bool operator==(Nil) const { return (_obj == nil); }
inline bool operator!=(nullptr_t) const { return (_obj != nil); } inline bool operator!=(Nil) const { return (_obj != nil); }
// compare wrt refptr // compare wrt refptr
inline bool operator==(const refptr& p2) const { return (_obj == p2._obj); } inline bool operator==(const refptr& p2) const { return (_obj == p2._obj); }
...@@ -691,8 +693,8 @@ public: ...@@ -691,8 +693,8 @@ public:
} }
// = nil // = nil
inline global(nullptr_t) { _obj = nil; } inline global(Nil) { _obj = nil; }
inline global& operator=(nullptr_t) { inline global& operator=(Nil) {
if (_obj != nil) if (_obj != nil)
_obj->decref(); _obj->decref();
_obj = nil; _obj = nil;
...@@ -703,8 +705,8 @@ public: ...@@ -703,8 +705,8 @@ public:
// move - no need due to refptr<T> cast // move - no need due to refptr<T> cast
// compare wrt nil // compare wrt nil
inline bool operator==(nullptr_t) const { return (_obj == nil); } inline bool operator==(Nil) const { return (_obj == nil); }
inline bool operator!=(nullptr_t) const { return (_obj != nil); } inline bool operator!=(Nil) const { return (_obj != nil); }
// compare wrt refptr // compare wrt refptr
inline bool operator==(const refptr<T>& p2) const { return (_obj == p2._obj); } inline bool operator==(const refptr<T>& p2) const { return (_obj == p2._obj); }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment