Commit ec7cd84b authored by Robert Bradshaw's avatar Robert Bradshaw

merge

parents be671f42 e6429bcd
......@@ -58,6 +58,8 @@ class Context:
#self.modules = {"__builtin__" : BuiltinScope()}
import Builtin
self.modules = {"__builtin__" : Builtin.builtin_scope}
self.pxds = {}
self.pyxs = {}
self.include_directories = include_directories
self.future_directives = set()
......@@ -285,74 +287,6 @@ class Context:
names.reverse()
return ".".join(names)
def compile(self, source, options = None, full_module_name = None):
raise Exception("Deprecated")
# Compile a Pyrex implementation file in this context
# and return a CompilationResult.
if not options:
options = default_options
result = CompilationResult()
cwd = os.getcwd()
source = os.path.join(cwd, source)
result.main_source_file = source
if options.use_listing_file:
result.listing_file = Utils.replace_suffix(source, ".lis")
Errors.open_listing_file(result.listing_file,
echo_to_stderr = options.errors_to_stderr)
else:
Errors.open_listing_file(None)
if options.output_file:
result.c_file = os.path.join(cwd, options.output_file)
else:
if options.cplus:
c_suffix = ".cpp"
else:
c_suffix = ".c"
result.c_file = Utils.replace_suffix(source, c_suffix)
c_stat = None
if result.c_file:
try:
c_stat = os.stat(result.c_file)
except EnvironmentError:
pass
full_module_name = full_module_name or self.extract_module_name(source, options)
source = FileSourceDescriptor(source)
initial_pos = (source, 1, 0)
scope = self.find_module(full_module_name, pos = initial_pos, need_pxd = 0)
errors_occurred = False
try:
tree = self.parse(source, scope, pxd = 0,
full_module_name = full_module_name)
from ParseTreeTransforms import WithTransform, PostParse
tree = PostParse()(tree)
tree = WithTransform()(tree)
tree.process_implementation(scope, options, result)
except CompileError:
errors_occurred = True
Errors.close_listing_file()
result.num_errors = Errors.num_errors
if result.num_errors > 0:
errors_occurred = True
if errors_occurred and result.c_file:
try:
Utils.castrate_file(result.c_file, os.stat(source.filename))
except EnvironmentError:
pass
result.c_file = None
if result.c_file and not options.c_only and c_compile:
result.object_file = c_compile(result.c_file,
verbose_flag = options.show_version,
cplus = options.cplus)
if not options.obj_only and c_link:
result.extension_file = c_link(result.object_file,
extra_objects = options.objects,
verbose_flag = options.show_version,
cplus = options.cplus)
return result
def setup_errors(self, options):
if options.use_listing_file:
result.listing_file = Utils.replace_suffix(source, ".lis")
......@@ -384,37 +318,56 @@ class Context:
class CompilationSource(object):
def __init__(self, source_desc, full_module_name):
"""
Contains the data necesarry to start up a compilation pipeline for
a single compilation source (= file, usually).
"""
def __init__(self, source_desc, full_module_name, cwd):
self.source_desc = source_desc
self.full_module_name = full_module_name
self.cwd = cwd
def create_parse(context, scope):
def create_parse(context):
def parse(compsrc):
source_desc = compsrc.source_desc
full_module_name = compsrc.full_module_name
initial_pos = (source_desc, 1, 0)
scope = context.find_module(full_module_name, pos = initial_pos, need_pxd = 0)
tree = context.parse(source_desc, scope, pxd = 0, full_module_name = full_module_name)
tree.compilation_source = compsrc
tree.scope = scope
return tree
return parse
def create_generate_code(context, scope, options, result):
def generate_code(tree):
tree.process_implementation(scope, options, result)
def create_generate_code(context, options):
def generate_code(module_node):
scope = module_node.scope
result = create_default_resultobj(module_node.compilation_source, options)
module_node.process_implementation(options, result)
return result
return generate_code
def create_default_pipeline(context, scope, options, result):
def create_default_pipeline(context, options):
from ParseTreeTransforms import WithTransform, PostParse
from ParseTreeTransforms import AnalyseDeclarationsTransform, AnalyseExpressionsTransform
from ModuleNode import check_c_classes
return [
create_parse(context, scope),
create_parse(context),
PostParse(),
WithTransform(),
create_generate_code(context, scope, options, result)
AnalyseDeclarationsTransform(),
check_c_classes,
AnalyseExpressionsTransform(),
create_generate_code(context, options)
]
def create_default_resultobj(source_desc, options, cwd):
def create_default_resultobj(compilation_source, options):
result = CompilationResult()
result.main_source_file = source_desc.filename
result.main_source_file = compilation_source.source_desc.filename
source_desc = compilation_source.source_desc
if options.output_file:
result.c_file = os.path.join(cwd, options.output_file)
result.c_file = os.path.join(compilation_source.cwd, options.output_file)
else:
if options.cplus:
c_suffix = ".cpp"
......@@ -442,24 +395,19 @@ def run_pipeline(source, options = None, full_module_name = None):
cwd = os.getcwd()
source_desc = FileSourceDescriptor(os.path.join(cwd, source))
full_module_name = full_module_name or context.extract_module_name(source, options)
source = CompilationSource(source_desc, full_module_name)
# Set up result object
result = create_default_resultobj(source_desc, options, cwd)
source = CompilationSource(source_desc, full_module_name, cwd)
# Get pipeline
initial_pos = (source_desc, 1, 0)
scope = context.find_module(full_module_name, pos = initial_pos, need_pxd = 0)
pipeline = create_default_pipeline(context, scope, options, result)
pipeline = create_default_pipeline(context, options)
data = source
errors_occurred = False
try:
data = source
for phase in pipeline:
data = phase(data)
except CompileError:
errors_occurred = True
result = data
context.teardown_errors(errors_occurred, options, result, source_desc)
return result
......
......@@ -25,6 +25,10 @@ from PyrexTypes import py_object_type
from Cython.Utils import open_new_file, replace_suffix
def check_c_classes(module_node):
module_node.scope.check_c_classes()
return module_node
class ModuleNode(Nodes.Node, Nodes.BlockNode):
# doc string or None
# body StatListNode
......@@ -32,6 +36,9 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
# referenced_modules [ModuleScope]
# module_temp_cname string
# full_module_name string
#
# scope The module scope.
# compilation_source A CompilationSource (see Main)
child_attrs = ["body"]
......@@ -44,12 +51,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
env.doc = self.doc
self.body.analyse_declarations(env)
def process_implementation(self, env, options, result):
self.scope = env
from ParseTreeTransforms import AnalyseDeclarationsTransform, AnalyseExpressionsTransform
AnalyseDeclarationsTransform(env).visit_ModuleNode(self) # self.analyse_declarations(env)
env.check_c_classes()
AnalyseExpressionsTransform().visit_ModuleNode(self) # self.body.analyse_expressions(env)
def process_implementation(self, options, result):
env = self.scope
env.return_type = PyrexTypes.c_void_type
self.referenced_modules = []
self.find_referenced_modules(env, self.referenced_modules, {})
......
......@@ -145,15 +145,15 @@ class WithTransform(VisitorTransform):
class AnalyseDeclarationsTransform(VisitorTransform):
def __init__(self, env):
VisitorTransform.__init__(self)
self.env_stack = [env]
def __call__(self, root):
self.env_stack = [root.scope]
return super(AnalyseDeclarationsTransform, self).__call__(root)
def visit_ModuleNode(self, node):
node.analyse_declarations(self.env_stack[-1])
self.visitchildren(node)
return node
def visit_FuncDefNode(self, node):
lenv = node.create_local_scope(self.env_stack[-1])
node.body.analyse_control_flow(lenv) # this will be totally refactored
......
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