From: Robert Bradshaw Date: Tue, 15 Dec 2009 01:59:49 +0000 (-0800) Subject: C++ check code X-Git-Tag: 0.13.beta0~353^2~38 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=44adaf7082a99f3b4a8cb5d78b5bf80425520274;p=cython.git C++ check code --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index d3a7f0d5..1ff7504a 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1089,6 +1089,7 @@ class NewExprNode(AtomicExprNode): if entry is None or not entry.is_cpp_class: error(self.pos, "new operator can only be applied to a C++ class") return + self.cpp_check(env) if self.template_parameters is not None: template_types = [v.analyse_as_type(env) for v in self.template_parameters] type = entry.type.specialize_here(self.pos, template_types) diff --git a/Cython/Compiler/Main.py b/Cython/Compiler/Main.py index 7b8b7efd..15774e86 100644 --- a/Cython/Compiler/Main.py +++ b/Cython/Compiler/Main.py @@ -59,7 +59,7 @@ class Context(object): # include_directories [string] # future_directives [object] - def __init__(self, include_directories, pragma_overrides): + def __init__(self, include_directories, pragma_overrides, cpp=False): #self.modules = {"__builtin__" : BuiltinScope()} import Builtin, CythonScope self.modules = {"__builtin__" : Builtin.builtin_scope} @@ -67,6 +67,7 @@ class Context(object): self.include_directories = include_directories self.future_directives = set() self.pragma_overrides = pragma_overrides + self.cpp = cpp self.pxds = {} # full name -> node tree @@ -423,6 +424,7 @@ class Context(object): if not isinstance(source_desc, FileSourceDescriptor): raise RuntimeError("Only file sources for code supported") source_filename = Utils.encode_filename(source_desc.filename) + scope.cpp = self.cpp # Parse the given source file and return a parse tree. try: f = Utils.open_source_file(source_filename, "rU") @@ -521,7 +523,7 @@ def create_default_resultobj(compilation_source, options): def run_pipeline(source, options, full_module_name = None): # Set up context - context = Context(options.include_path, options.pragma_overrides) + context = Context(options.include_path, options.pragma_overrides, options.cplus) # Set up source object cwd = os.getcwd() diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index e9c14eb2..73a9d83b 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -138,6 +138,15 @@ class Node(object): def gil_error(self): error(self.pos, "%s not allowed without gil" % self.gil_message) + + cpp_message = "Operation" + + def cpp_check(self, env): + if not env.is_cpp(): + self.cpp_error() + + def cpp_error(self): + error(self.pos, "%s only allowed in c++" % self.cpp_message) def clone_node(self): """Clone the node. This is defined as a shallow copy, except for member lists @@ -3430,7 +3439,7 @@ class DelStatNode(StatNode): if arg.type.is_pyobject: self.gil_check(env) elif arg.type.is_ptr and arg.type.base_type.is_cpp_class: - pass + self.cpp_check(env) elif arg.type.is_cpp_class: error(arg.pos, "Deletion of non-heap C++ object") else: diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index f5b29ed7..db3eca3f 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -700,6 +700,9 @@ class Scope(object): if name in self.entries: return 1 return 0 + + def is_cpp(self): + return self.outer_scope.is_cpp() class PreImportScope(Scope): @@ -832,6 +835,7 @@ class ModuleScope(Scope): # all_pystring_entries [Entry] Python string consts from all scopes # types_imported {PyrexType : 1} Set of types for which import code generated # has_import_star boolean Module contains import * + # cpp boolean Compiling a C++ file is_module_scope = 1 has_import_star = 0 @@ -1240,6 +1244,9 @@ class ModuleScope(Scope): var_entry.is_cglobal = 1 var_entry.is_readonly = 1 entry.as_variable = var_entry + + def is_cpp(self): + return self.cpp class LocalScope(Scope):