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.
(*) 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 libcpp.utility cimport pair
......@@ -44,7 +44,7 @@ from libcpp.utility cimport pair
cdef extern from "golang/libgolang.h" namespace "golang" nogil:
cppclass cancelFunc "golang::func<void()>":
void operator() ()
void operator= (nullptr_t)
void operator= (Nil)
cdef extern from "golang/context.h" namespace "golang::context" nogil:
cppclass _Context:
......
......@@ -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 libc.stdint cimport uint64_t
cdef extern from *:
......@@ -88,9 +88,9 @@ cdef extern from "golang/libgolang.h" namespace "golang" nogil:
unsigned cap() const
# compare wrt nil; =nil
cbool operator==(nullptr_t) const
cbool operator!=(nullptr_t) const
void operator=(nullptr_t)
cbool operator==(Nil) const
cbool operator!=(Nil) const
void operator=(Nil)
# for tests
_chan *_rawchan() const
......@@ -127,9 +127,9 @@ cdef extern from "golang/libgolang.h" namespace "golang" nogil:
# memory management of C++ nogil classes
cppclass refptr[T]:
# compare wrt nil; =nil
cbool operator== (nullptr_t) const
cbool operator!= (nullptr_t) const
void operator= (nullptr_t) const
cbool operator== (Nil) const
cbool operator!= (Nil) const
void operator= (Nil) const
# compare wrt refptr; =refptr
# XXX workaround for https://github.com/cython/cython/issues/1357:
......
......@@ -366,6 +366,7 @@ LIBGOLANG_API extern void (*_tblockforever)(void);
#ifdef __cplusplus
#include <atomic>
#include <cstddef>
#include <exception>
#include <functional>
#include <initializer_list>
......@@ -377,8 +378,9 @@ LIBGOLANG_API extern void (*_tblockforever)(void);
namespace golang {
// nil is alias for nullptr and NULL.
constexpr nullptr_t nil = nullptr;
// nil is alias for nullptr and NULL; Nil - for std::nullptr_t;
using Nil = std::nullptr_t;
constexpr Nil nil = nullptr;
// string is alias for std::string.
using string = std::string;
......@@ -418,8 +420,8 @@ public:
inline ~chan() { _chanxdecref(_ch); _ch = nil; }
// = nil
inline chan(nullptr_t) { _ch = nil; }
inline chan& operator=(nullptr_t) { _chanxdecref(_ch); _ch = nil; return *this; }
inline chan(Nil) { _ch = nil; }
inline chan& operator=(Nil) { _chanxdecref(_ch); _ch = nil; return *this; }
// copy
inline chan(const chan& from) { _ch = from._ch; _chanxincref(_ch); }
inline chan& operator=(const chan& from) {
......@@ -462,12 +464,12 @@ public:
}
// length/capacity
inline unsigned len() const { return _chanlen(_ch); }
inline unsigned cap() const { return _chancap(_ch); }
inline unsigned len() const { return _chanlen(_ch); }
inline unsigned cap() const { return _chancap(_ch); }
// compare wrt nil
inline bool operator==(nullptr_t) const { return (_ch == nil); }
inline bool operator!=(nullptr_t) const { return (_ch != nil); }
inline bool operator==(Nil) const { return (_ch == nil); }
inline bool operator!=(Nil) const { return (_ch != nil); }
// compare wrt chan
inline bool operator==(const chan<T>& ch2) const { return (_ch == ch2._ch); }
......@@ -587,8 +589,8 @@ public:
}
// = nil
inline refptr(nullptr_t) { _obj = nil; }
inline refptr& operator=(nullptr_t) {
inline refptr(Nil) { _obj = nil; }
inline refptr& operator=(Nil) {
if (_obj != nil)
_obj->decref();
_obj = nil;
......@@ -632,8 +634,8 @@ public:
friend refptr<T> newref<T> (T *_obj);
// compare wrt nil
inline bool operator==(nullptr_t) const { return (_obj == nil); }
inline bool operator!=(nullptr_t) const { return (_obj != nil); }
inline bool operator==(Nil) const { return (_obj == nil); }
inline bool operator!=(Nil) const { return (_obj != nil); }
// compare wrt refptr
inline bool operator==(const refptr& p2) const { return (_obj == p2._obj); }
......@@ -691,8 +693,8 @@ public:
}
// = nil
inline global(nullptr_t) { _obj = nil; }
inline global& operator=(nullptr_t) {
inline global(Nil) { _obj = nil; }
inline global& operator=(Nil) {
if (_obj != nil)
_obj->decref();
_obj = nil;
......@@ -703,8 +705,8 @@ public:
// move - no need due to refptr<T> cast
// compare wrt nil
inline bool operator==(nullptr_t) const { return (_obj == nil); }
inline bool operator!=(nullptr_t) const { return (_obj != nil); }
inline bool operator==(Nil) const { return (_obj == nil); }
inline bool operator!=(Nil) const { return (_obj != nil); }
// compare wrt refptr
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