Fix C++ const method declarations.
authorRobert Bradshaw <robertwb@gmail.com>
Sat, 28 Dec 2013 00:17:17 +0000 (16:17 -0800)
committerRobert Bradshaw <robertwb@gmail.com>
Sat, 28 Dec 2013 00:17:17 +0000 (16:17 -0800)
Cython/Compiler/Nodes.py
Cython/Compiler/Parsing.py
Cython/Compiler/PyrexTypes.py
tests/run/cpp_classes_def.pyx
tests/run/shapes.h

index 4f0ccad..8c0e708 100644 (file)
@@ -575,11 +575,13 @@ class CFuncDeclaratorNode(CDeclaratorNode):
     # exception_check  boolean    True if PyErr_Occurred check needed
     # nogil            boolean    Can be called without gil
     # with_gil         boolean    Acquire gil around function body
+    # is_const_method  boolean    Whether this is a const method
 
     child_attrs = ["base", "args", "exception_value"]
 
     overridable = 0
     optional_arg_count = 0
+    is_const_method = 0
     templates = None
 
     def analyse_templates(self):
@@ -688,6 +690,7 @@ class CFuncDeclaratorNode(CDeclaratorNode):
             exception_value = exc_val, exception_check = exc_check,
             calling_convention = self.base.calling_convention,
             nogil = self.nogil, with_gil = self.with_gil, is_overridable = self.overridable,
+            is_const_method = self.is_const_method,
             templates = self.templates)
 
         if self.optional_arg_count:
index c84c778..421feb2 100644 (file)
@@ -2135,7 +2135,7 @@ def p_buffer_or_template(s, base_type_node, templates):
         p_positional_and_keyword_args(s, (']',), templates)
     )
     s.expect(']')
-    
+
     if s.sy == '[':
         base_type_node = p_buffer_or_template(s, base_type_node, templates)
 
@@ -2827,6 +2827,8 @@ def p_c_func_or_var_declaration(s, pos, ctx):
     else:
         #if api:
         #    s.error("'api' not allowed with variable declaration")
+        if is_const_method:
+            declarator.is_const_method = is_const_method
         declarators = [declarator]
         while s.sy == ',':
             s.next()
index 1b5556c..d388a90 100644 (file)
@@ -2336,7 +2336,7 @@ class CFuncType(CType):
     def __init__(self, return_type, args, has_varargs = 0,
             exception_value = None, exception_check = 0, calling_convention = "",
             nogil = 0, with_gil = 0, is_overridable = 0, optional_arg_count = 0,
-            templates = None, is_strict_signature = False):
+            is_const_method = False, templates = None, is_strict_signature = False):
         self.return_type = return_type
         self.args = args
         self.has_varargs = has_varargs
@@ -2347,6 +2347,7 @@ class CFuncType(CType):
         self.nogil = nogil
         self.with_gil = with_gil
         self.is_overridable = is_overridable
+        self.is_const_method = is_const_method
         self.templates = templates
         self.is_strict_signature = is_strict_signature
 
@@ -2572,6 +2573,7 @@ class CFuncType(CType):
                            with_gil = self.with_gil,
                            is_overridable = self.is_overridable,
                            optional_arg_count = self.optional_arg_count,
+                           is_const_method = self.is_const_method,
                            templates = self.templates)
 
         result.from_fused = self.is_fused
index 1d873fc..d893ed5 100644 (file)
@@ -7,7 +7,7 @@ from libc.math cimport sin, cos
 
 cdef extern from "shapes.h" namespace "shapes":
     cdef cppclass Shape:
-        float area()
+        float area() const
 
 cdef cppclass RegularPolygon(Shape):
     float radius # major
@@ -15,7 +15,7 @@ cdef cppclass RegularPolygon(Shape):
     __init__(int n, float radius):
         this.n = n
         this.radius = radius
-    float area():
+    float area() const:
         cdef double theta = pi / this.n
         return this.radius * this.radius * sin(theta) * cos(theta) * this.n
 
@@ -81,7 +81,7 @@ def test_templates(long value):
     """
     cdef WithTemplate[long] *base = new WithTemplate[long]()
     del base
-    
+
     cdef ResolveTemplate *resolved = new ResolveTemplate()
     resolved.set_value(value)
     assert resolved.value == resolved.get_value() == value, resolved.value
@@ -89,5 +89,5 @@ def test_templates(long value):
     base = resolved
     base.set_value(2 * value)
     assert base.get_value() == base.value == 2 * value, base.value
-    
+
     del base
index d45b15d..45dc77e 100644 (file)
@@ -9,7 +9,7 @@ namespace shapes {
     class Shape
     {
     public:
-        virtual float area() = 0;
+        virtual float area() const = 0;
         Shape() { constructor_count++; }
         virtual ~Shape() { destructor_count++; }
     };
@@ -24,7 +24,7 @@ namespace shapes {
             this->height = height;
         }
 
-        float area() { return width * height; }
+        float area() const { return width * height; }
         int width;
         int height;
 
@@ -44,13 +44,13 @@ namespace shapes {
     class Circle : public Shape {
     public:
         Circle(int radius) { this->radius = radius; }
-        float area() { return 3.1415926535897931f * radius; }
+        float area() const { return 3.1415926535897931f * radius; }
         int radius;
     };
 
     class Empty : public Shape {
     public:
-        float area() { return 0; }
+        float area() const { return 0; }
     };
 
 }