From 827fff942495312e13570cde9196e6d2b989e143 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Fri, 9 Apr 2010 00:13:49 -0300 Subject: [PATCH] C++ fixes (parsing and scope lookup), working libcpp pxd's, more tests --- Cython/Compiler/Nodes.py | 4 +- Cython/Compiler/Parsing.py | 1 + Cython/Compiler/Symtab.py | 5 ++- Cython/Includes/libcpp/__init__.pxd | 1 + Cython/Includes/libcpp/__init__.pyx | 1 + Cython/Includes/libcpp/deque.pxd | 54 ++++++++++++++----------- Cython/Includes/libcpp/list.pxd | 45 +++++++++++---------- Cython/Includes/libcpp/map.pxd | 62 ++++++++++++++++++++++++++++ Cython/Includes/libcpp/pair.pxd | 4 +- Cython/Includes/libcpp/queue.pxd | 14 +++++-- Cython/Includes/libcpp/set.pxd | 63 ++++++++++++++++------------- Cython/Includes/libcpp/stack.pxd | 10 +++++ Cython/Includes/libcpp/vector.pxd | 53 ++++++++++++------------ tests/compile/libcpp_all.pyx | 55 +++++++++++++++++++++++++ 14 files changed, 263 insertions(+), 109 deletions(-) create mode 100644 Cython/Includes/libcpp/__init__.pxd create mode 100644 Cython/Includes/libcpp/__init__.pyx create mode 100644 Cython/Includes/libcpp/map.pxd create mode 100644 Cython/Includes/libcpp/stack.pxd create mode 100644 tests/compile/libcpp_all.pyx diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index e60614c4..bf4880d8 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -1022,10 +1022,8 @@ class CppClassNode(CStructOrUnionDefNode): def analyse_declarations(self, env): scope = None - if self.attributes: + if self.attributes is not None: scope = CppClassScope(self.name, env) - else: - self.attributes = None base_class_types = [] for base_class_name in self.base_classes: base_class_entry = env.lookup(base_class_name) diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 3ee56367..35276bcd 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -2650,6 +2650,7 @@ def p_cpp_class_definition(s, pos, ctx): s.expect_newline("Expected a newline") s.expect_dedent() else: + attributes = None s.expect_newline("Syntax error in C++ class definition") return Nodes.CppClassNode(pos, name = class_name, diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index 27123a56..d2ffa6a4 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -408,7 +408,7 @@ class Scope(object): error(pos, "C++ classes may only be extern") if cname is None: cname = name - entry = self.lookup(name) + entry = self.lookup_here(name) if not entry: type = PyrexTypes.CppClassType( name, scope, cname, base_classes, templates = templates) @@ -432,7 +432,8 @@ class Scope(object): for base_class in base_classes: declare_inherited_attributes(entry, base_class.base_classes) entry.type.scope.declare_inherited_cpp_attributes(base_class.scope) - declare_inherited_attributes(entry, base_classes) + if entry.type.scope: + declare_inherited_attributes(entry, base_classes) if self.is_cpp_class_scope: entry.type.namespace = self.outer_scope.lookup(self.name).type return entry diff --git a/Cython/Includes/libcpp/__init__.pxd b/Cython/Includes/libcpp/__init__.pxd new file mode 100644 index 00000000..fa81adaf --- /dev/null +++ b/Cython/Includes/libcpp/__init__.pxd @@ -0,0 +1 @@ +# empty file diff --git a/Cython/Includes/libcpp/__init__.pyx b/Cython/Includes/libcpp/__init__.pyx new file mode 100644 index 00000000..fa81adaf --- /dev/null +++ b/Cython/Includes/libcpp/__init__.pyx @@ -0,0 +1 @@ +# empty file diff --git a/Cython/Includes/libcpp/deque.pxd b/Cython/Includes/libcpp/deque.pxd index 45f4ae1b..fc0a4913 100644 --- a/Cython/Includes/libcpp/deque.pxd +++ b/Cython/Includes/libcpp/deque.pxd @@ -3,39 +3,44 @@ from pair cimport pair cdef extern from "" namespace "std": cdef cppclass deque[T]: cppclass iterator: - T operator*() + T& operator*() iterator operator++() + iterator operator--() bint operator==(iterator) bint operator!=(iterator) - cppclass const_iterator(iterator): - pass - cppclass reverse_iterator(iterator): - pass - cppclass const_reverse_iterator(iterator): - pass + cppclass reverse_iterator: + T& operator*() + iterator operator++() + iterator operator--() + bint operator==(iterator) + bint operator!=(iterator) + #cppclass const_iterator(iterator): + # pass + #cppclass const_reverse_iterator(reverse_iterator): + # pass deque() deque(deque&) - deque(size_t, T& val = T()) - #deque(input_iterator, input_iterator) - TYPE& operator[]( size_type index ) - const TYPE& operator[]( size_type index ) const + deque(size_t) + deque(size_t, T&) + #deque[input_iterator](input_iterator, input_iterator) + T& operator[](size_t) #deque& operator=(deque&) - bool operator==(deque&, deque&) - bool operator!=(deque&, deque&) - bool operator<(deque&, deque&) - bool operator>(deque&, deque&) - bool operator<=(deque&, deque&) - bool operator>=(deque&, deque&) - void assign(size_t, TYPE&) + bint operator==(deque&, deque&) + bint operator!=(deque&, deque&) + bint operator<(deque&, deque&) + bint operator>(deque&, deque&) + bint operator<=(deque&, deque&) + bint operator>=(deque&, deque&) + void assign(size_t, T&) void assign(input_iterator, input_iterator) T& at(size_t) T& back() iterator begin() - const_iterator begin() + #const_iterator begin() void clear() - bool empty() + bint empty() iterator end() - const_iterator end() + #const_iterator end() iterator erase(iterator) iterator erase(iterator, iterator) T& front() @@ -48,9 +53,10 @@ cdef extern from "" namespace "std": void push_back(T&) void push_front(T&) reverse_iterator rbegin() - const_reverse_iterator rbegin() + #const_reverse_iterator rbegin() reverse_iterator rend() - const_reverse_iterator rend() - void resize(size_t, T val = T()) + #const_reverse_iterator rend() + void resize(size_t) + void resize(size_t, T&) size_t size() void swap(deque&) diff --git a/Cython/Includes/libcpp/list.pxd b/Cython/Includes/libcpp/list.pxd index 8d5854aa..b395f7d6 100644 --- a/Cython/Includes/libcpp/list.pxd +++ b/Cython/Includes/libcpp/list.pxd @@ -1,35 +1,38 @@ -from pair cimport pair - cdef extern from "" namespace "std": cdef cppclass list[T]: cppclass iterator: - T operator*() + T& operator*() iterator operator++() + iterator operator--() bint operator==(iterator) bint operator!=(iterator) - cppclass const_iterator(iterator): - pass - cppclass reverse_iterator(iterator): - pass - cppclass const_reverse_iterator(iterator): - pass + cppclass reverse_iterator: + T& operator*() + iterator operator++() + iterator operator--() + bint operator==(iterator) + bint operator!=(iterator) + #cppclass const_iterator(iterator): + # pass + #cppclass const_reverse_iterator(reverse_iterator): + # pass list() list(list&) list(size_t, T&) #list operator=(list&) - bool operator==(list&, list&) - bool operator!=(list&, list&) - bool operator<(list&, list&) - bool operator>(list&, list&) - bool operator<=(list&, list&) - bool operator>=(list&, list&) + bint operator==(list&, list&) + bint operator!=(list&, list&) + bint operator<(list&, list&) + bint operator>(list&, list&) + bint operator<=(list&, list&) + bint operator>=(list&, list&) void assign(size_t, T&) T& back() iterator begin() - const_iterator begin() - bool empty() + #const_iterator begin() + bint empty() iterator end() - const_iterator end() + #const_iterator end() iterator erase(iterator) iterator erase(iterator, iterator) T& front() @@ -37,17 +40,17 @@ cdef extern from "" namespace "std": void insert(iterator, size_t, T&) size_t max_size() void merge(list&) - #voide merge(list&, BinPred) + #void merge(list&, BinPred) void pop_back() void pop_front() void push_back(T&) void push_front(T&) reverse_iterator rbegin() - const_reverse_iterator rbegin() + #const_reverse_iterator rbegin() void remove(T&) #void remove_if(UnPred) reverse_iterator rend() - const_reverse_iterator rend() + #const_reverse_iterator rend() void resize(size_t, T&) void reverse() size_t size() diff --git a/Cython/Includes/libcpp/map.pxd b/Cython/Includes/libcpp/map.pxd new file mode 100644 index 00000000..78c8cd99 --- /dev/null +++ b/Cython/Includes/libcpp/map.pxd @@ -0,0 +1,62 @@ +from pair cimport pair + +cdef extern from "" namespace "std": + cdef cppclass map[T, U]: + cppclass iterator: + pair[T,U]& operator*() + iterator operator++() + iterator operator--() + bint operator==(iterator) + bint operator!=(iterator) + cppclass reverse_iterator: + pair[T,U] operator*() + iterator operator++() + iterator operator--() + bint operator==(iterator) + bint operator!=(iterator) + #cppclass const_iterator(iterator): + # pass + #cppclass const_reverse_iterator(reverse_iterator): + # pass + map() + map(map&) + #map(key_compare&) + U& operator[](T&) + #map& operator=(map&) + bint operator==(map&, map&) + bint operator!=(map&, map&) + bint operator<(map&, map&) + bint operator>(map&, map&) + bint operator<=(map&, map&) + bint operator>=(map&, map&) + U& at(T&) + iterator begin() + #const_iterator begin() + void clear() + size_t count(T&) + bint empty() + iterator end() + #const_iterator end() + pair[iterator, iterator] equal_range(T&) + #pair[const_iterator, const_iterator] equal_range(key_type&) + void erase(iterator) + void erase(iterator, iterator) + size_t erase(T&) + iterator find(T&) + #const_iterator find(key_type&) + pair[iterator, bint] insert(pair[T,U]) # XXX pair[T,U]& + iterator insert(iterator, pair[T,U]) # XXX pair[T,U]& + #void insert(input_iterator, input_iterator) + #key_compare key_comp() + iterator lower_bound(T&) + #const_iterator lower_bound(key_type&) + size_t max_size() + reverse_iterator rbegin() + #const_reverse_iterator rbegin() + reverse_iterator rend() + #const_reverse_iterator rend() + size_t size() + void swap(map&) + iterator upper_bound(T&) + #const_iterator upper_bound(key_type&) + #value_compare value_comp() diff --git a/Cython/Includes/libcpp/pair.pxd b/Cython/Includes/libcpp/pair.pxd index ceeeda03..da0eb773 100644 --- a/Cython/Includes/libcpp/pair.pxd +++ b/Cython/Includes/libcpp/pair.pxd @@ -1,7 +1,7 @@ -cdef extern from "pair.h": +cdef extern from "" namespace "std": cdef cppclass pair[T, U]: T first U second pair() + pair(pair&) pair(T&, U&) - diff --git a/Cython/Includes/libcpp/queue.pxd b/Cython/Includes/libcpp/queue.pxd index a92472a8..a680b4e4 100644 --- a/Cython/Includes/libcpp/queue.pxd +++ b/Cython/Includes/libcpp/queue.pxd @@ -1,12 +1,20 @@ -from pair cimport pair - cdef extern from "" namespace "std": cdef cppclass queue[T]: queue() + queue(queue&) #queue(Container&) T& back() - bool empty() + bint empty() T& front() void pop() void push(T&) size_t size() + cdef cppclass priority_queue[T]: + priority_queue() + priority_queue(priority_queue&) + #priority_queue(Container&) + bint empty() + void pop() + void push(T&) + size_t size() + T& top() diff --git a/Cython/Includes/libcpp/set.pxd b/Cython/Includes/libcpp/set.pxd index eb4a9562..6d6607ee 100644 --- a/Cython/Includes/libcpp/set.pxd +++ b/Cython/Includes/libcpp/set.pxd @@ -5,51 +5,56 @@ cdef extern from "" namespace "std": cppclass iterator: T operator*() iterator operator++() + iterator operator--() bint operator==(iterator) bint operator!=(iterator) - cppclass const_iterator(iterator): - pass - cppclass reverse_iterator(iterator): - pass - cppclass const_reverse_iterator(iterator): - pass + cppclass reverse_iterator: + T operator*() + iterator operator++() + iterator operator--() + bint operator==(iterator) + bint operator!=(iterator) + #cppclass const_iterator(iterator): + # pass + #cppclass const_reverse_iterator(reverse_iterator): + # pass set() set(set&) - #set set(key_compare&) + #set(key_compare&) #set& operator=(set&) - bool operator==(set&, set&) - bool operator!=(set&, set&) - bool operator<(set&, set&) - bool operator>(set&, set&) - bool operator<=(set&, set&) - bool operator>=(set&, set&) + bint operator==(set&, set&) + bint operator!=(set&, set&) + bint operator<(set&, set&) + bint operator>(set&, set&) + bint operator<=(set&, set&) + bint operator>=(set&, set&) iterator begin() - const_iterator begin() + #const_iterator begin() void clear() - #size_t count(key_type&) - bool empty() + size_t count(T&) + bint empty() iterator end() - const_iterator end() - #pair[iterator, iterator] equal_range(key_type&) - #pair[const_iterator, const_iterator] equal_range(key_type&) + #const_iterator end() + pair[iterator, iterator] equal_range(T&) + #pair[const_iterator, const_iterator] equal_range(T&) void erase(iterator) void erase(iterator, iterator) - #size_t erase(key_type&) - #iterator find(key_type&) - #const_iterator find(key_type&) - #pair[iterator, bool] insert(T&) + size_t erase(T&) + iterator find(T&) + #const_iterator find(T&) + pair[iterator, bint] insert(T&) iterator insert(iterator, T&) #void insert(input_iterator, input_iterator) #key_compare key_comp() - #iterator lower_bound(key_type&) - #const_iterator lower_bound(key_type&) + iterator lower_bound(T&) + #const_iterator lower_bound(T&) size_t max_size() reverse_iterator rbegin() - const_reverse_iterator rbegin() + #const_reverse_iterator rbegin() reverse_iterator rend() - const_reverse_iterator rend() + #const_reverse_iterator rend() size_t size() void swap(set&) - #iterator upper_bound(key_type&) - #const_iterator upper_bound(key_type&) + iterator upper_bound(T&) + #const_iterator upper_bound(T&) #value_compare value_comp() diff --git a/Cython/Includes/libcpp/stack.pxd b/Cython/Includes/libcpp/stack.pxd new file mode 100644 index 00000000..85746801 --- /dev/null +++ b/Cython/Includes/libcpp/stack.pxd @@ -0,0 +1,10 @@ +cdef extern from "" namespace "std": + cdef cppclass stack[T]: + stack() + stack(stack&) + #stack(Container&) + bint empty() + void pop() + void push(T&) + size_t size() + T& top() diff --git a/Cython/Includes/libcpp/vector.pxd b/Cython/Includes/libcpp/vector.pxd index 6002faba..0a6ba1f0 100644 --- a/Cython/Includes/libcpp/vector.pxd +++ b/Cython/Includes/libcpp/vector.pxd @@ -1,43 +1,45 @@ -from pair cimport pair - cdef extern from "" namespace "std": cdef cppclass vector[T]: cppclass iterator: - T operator*() + T& operator*() iterator operator++() + iterator operator--() bint operator==(iterator) bint operator!=(iterator) - cppclass const_iterator(iterator): - pass - cppclass reverse_iterator(iterator): - pass - cppclass const_reverse_iterator(iterator): - pass - #cppclass input_iterator(iterator): + cppclass reverse_iterator: + T& operator*() + iterator operator++() + iterator operator--() + bint operator==(iterator) + bint operator!=(iterator) + #cppclass const_iterator(iterator): + # pass + #cppclass const_reverse_iterator(reverse_iterator): # pass vector() - #vector(vector&) - #vector(size_t, T&) + vector(vector&) + vector(size_t) + vector(size_t, T&) #vector[input_iterator](input_iterator, input_iterator) T& operator[](size_t) #vector& operator=(vector&) - bool operator==(vector&, vector&) - bool operator!=(vector&, vector&) - bool operator<(vector&, vector&) - bool operator>(vector&, vector&) - bool operator<=(vector&, vector&) - bool operator>=(vector&, vector&) + bint operator==(vector&, vector&) + bint operator!=(vector&, vector&) + bint operator<(vector&, vector&) + bint operator>(vector&, vector&) + bint operator<=(vector&, vector&) + bint operator>=(vector&, vector&) void assign(size_t, T&) - #void assign(input_iterator, input_iterator) + #void assign[input_iterator](input_iterator, input_iterator) T& at(size_t) T& back() iterator begin() - const_iterator begin() + #const_iterator begin() size_t capacity() void clear() - bool empty() + bint empty() iterator end() - const_iterator end() + #const_iterator end() iterator erase(iterator) iterator erase(iterator, iterator) T& front() @@ -48,10 +50,11 @@ cdef extern from "" namespace "std": void pop_back() void push_back(T&) reverse_iterator rbegin() - const_reverse_iterator rbegin() + #const_reverse_iterator rbegin() reverse_iterator rend() - const_reverse_iterator rend() + #const_reverse_iterator rend() void reserve(size_t) - void resize(size_t, T) + void resize(size_t) + void resize(size_t, T&) size_t size() void swap(vector&) diff --git a/tests/compile/libcpp_all.pyx b/tests/compile/libcpp_all.pyx new file mode 100644 index 00000000..8ab5192d --- /dev/null +++ b/tests/compile/libcpp_all.pyx @@ -0,0 +1,55 @@ +cimport libcpp + +cimport libcpp.deque +cimport libcpp.list +cimport libcpp.map +cimport libcpp.pair +cimport libcpp.queue +cimport libcpp.set +cimport libcpp.stack +cimport libcpp.vector + +from libcpp.deque cimport * +from libcpp.list cimport * +from libcpp.map cimport * +from libcpp.pair cimport * +from libcpp.queue cimport * +from libcpp.set cimport * +from libcpp.stack cimport * +from libcpp.vector cimport * + +cdef libcpp.deque.deque[int] d1 = deque[int]() +cdef libcpp.list.list[int] l1 = list[int]() +cdef libcpp.map.map[int,int] m1 = map[int,int]() +cdef libcpp.pair.pair[int,int] p1 = pair[int,int](1,2) +cdef libcpp.queue.queue[int] q1 = queue[int]() +cdef libcpp.set.set[int] s1 = set[int]() +cdef libcpp.stack.stack[int] t1 = stack[int]() +cdef libcpp.vector.vector[int] v1 = vector[int]() + +cdef deque[int].iterator id1 = d1.begin() +cdef deque[int].iterator id2 = d1.end() +cdef deque[int].reverse_iterator rid1 = d1.rbegin() +cdef deque[int].reverse_iterator rid2 = d1.rend() + +cdef list[int].iterator il1 = l1.begin() +cdef list[int].iterator il2 = l1.end() +cdef list[int].reverse_iterator ril1 = l1.rbegin() +cdef list[int].reverse_iterator ril2 = l1.rend() + +cdef map[int,int].iterator im1 = m1.begin() +cdef map[int,int].iterator im2 = m1.end() +cdef map[int,int].reverse_iterator rim1 = m1.rbegin() +cdef map[int,int].reverse_iterator rim2 = m1.rend() +cdef pair[map[int,int].iterator, bint] pimb = m1.insert(p1) + +cdef set[int].iterator is1 = s1.begin() +cdef set[int].iterator is2 = s1.end() +cdef set[int].reverse_iterator ris1 = s1.rbegin() +cdef set[int].reverse_iterator ris2 = s1.rend() +cdef pair[set[int].iterator, bint] pisb = s1.insert(4) + +cdef vector[int].iterator iv1 = v1.begin() +cdef vector[int].iterator iv2 = v1.end() +cdef vector[int].reverse_iterator riv1 = v1.rbegin() +cdef vector[int].reverse_iterator riv2 = v1.rend() -- 2.26.2