Commit a686fdc4 authored by William Stein's avatar William Stein

Added a "Warning" class, and changed it so redeclaring or re-importing is a...

Added a "Warning" class, and changed it so redeclaring or re-importing is a warning rather than an error.

    Because Pyrex has no #ifndef macro, it is impossibly painful to use
    pxi files for declarations in a large project.  SAGE is a large project.
    Also, in Python it is not an error to import a module twice.  Thus
    more in line with Python's behavior, multiple declarations of the same
    symbol is no longer an error.
parent b33b5821
...@@ -9,6 +9,8 @@ from Pyrex.Utils import open_new_file ...@@ -9,6 +9,8 @@ from Pyrex.Utils import open_new_file
class PyrexError(Exception): class PyrexError(Exception):
pass pass
class PyrexWarning(Exception):
pass
class CompileError(PyrexError): class CompileError(PyrexError):
...@@ -21,6 +23,17 @@ class CompileError(PyrexError): ...@@ -21,6 +23,17 @@ class CompileError(PyrexError):
pos_str = "" pos_str = ""
Exception.__init__(self, pos_str + message) Exception.__init__(self, pos_str + message)
class CompileWarning(PyrexWarning):
def __init__(self, position = None, message = ""):
self.position = position
self.message = message
if position:
pos_str = "%s:%d:%d: " % position
else:
pos_str = ""
Exception.__init__(self, pos_str + message)
class InternalError(Exception): class InternalError(Exception):
# If this is ever raised, there is a bug in the compiler. # If this is ever raised, there is a bug in the compiler.
...@@ -65,3 +78,12 @@ def error(position, message): ...@@ -65,3 +78,12 @@ def error(position, message):
echo_file.write(line) echo_file.write(line)
num_errors = num_errors + 1 num_errors = num_errors + 1
return err return err
def warning(position, message):
warn = CompileWarning(position, message)
line = "%s\n" % warn
if listing_file:
listing_file.write(line)
if echo_file:
echo_file.write(line)
return warn
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# #
import re import re
from Errors import error, InternalError from Errors import error, InternalError, warning
import Options import Options
import Naming import Naming
from PyrexTypes import c_int_type, \ from PyrexTypes import c_int_type, \
...@@ -195,7 +195,7 @@ class Scope: ...@@ -195,7 +195,7 @@ class Scope:
# declared. # declared.
dict = self.entries dict = self.entries
if name and dict.has_key(name): if name and dict.has_key(name):
error(pos, "'%s' redeclared" % name) warning(pos, "'%s' redeclared (ignoring second declaration)" % name)
entry = Entry(name, cname, type, pos = pos) entry = Entry(name, cname, type, pos = pos)
entry.in_cinclude = self.in_cinclude entry.in_cinclude = self.in_cinclude
if name: if name:
...@@ -243,9 +243,9 @@ class Scope: ...@@ -243,9 +243,9 @@ class Scope:
self.sue_entries.append(entry) self.sue_entries.append(entry)
else: else:
if not (entry.is_type and entry.type.is_struct_or_union): if not (entry.is_type and entry.type.is_struct_or_union):
error(pos, "'%s' redeclared" % name) warning(pos, "'%s' redeclared (ignoring second declaration)" % name)
elif scope and entry.type.scope: elif scope and entry.type.scope:
error(pos, "'%s' already defined" % name) warning(pos, "'%s' already defined (ignoring second definition)" % name)
else: else:
self.check_previous_typedef_flag(entry, typedef_flag, pos) self.check_previous_typedef_flag(entry, typedef_flag, pos)
if scope: if scope:
...@@ -556,7 +556,7 @@ class ModuleScope(Scope): ...@@ -556,7 +556,7 @@ class ModuleScope(Scope):
if entry not in self.entries: if entry not in self.entries:
self.entries[name] = entry self.entries[name] = entry
else: else:
error(pos, "'%s' redeclared" % name) warning(pos, "'%s' redeclared (ignoring second declaration)" % name)
def declare_module(self, name, scope, pos): def declare_module(self, name, scope, pos):
# Declare a cimported module. This is represented as a # Declare a cimported module. This is represented as a
...@@ -574,7 +574,7 @@ class ModuleScope(Scope): ...@@ -574,7 +574,7 @@ class ModuleScope(Scope):
# name to appear again, and indeed the generated # name to appear again, and indeed the generated
# code compiles fine. # code compiles fine.
return entry return entry
error(pos, "'%s' redeclared" % name) warning(pos, "'%s' redeclared (ignoring second declaration)" % name)
return None return None
else: else:
entry = self.declare_var(name, py_object_type, pos) entry = self.declare_var(name, py_object_type, pos)
...@@ -822,7 +822,7 @@ class LocalScope(Scope): ...@@ -822,7 +822,7 @@ class LocalScope(Scope):
def declare_global(self, name, pos): def declare_global(self, name, pos):
# Pull entry from global scope into local scope. # Pull entry from global scope into local scope.
if self.lookup_here(name): if self.lookup_here(name):
error(pos, "'%s' redeclared") warning(pos, "'%s' redeclared (ignoring second declaration)")
else: else:
entry = self.global_scope().lookup_target(name) entry = self.global_scope().lookup_target(name)
self.entries[name] = entry self.entries[name] = entry
...@@ -996,7 +996,7 @@ class CClassScope(ClassScope): ...@@ -996,7 +996,7 @@ class CClassScope(ClassScope):
entry = self.lookup_here(name) entry = self.lookup_here(name)
if entry: if entry:
if not entry.is_cfunction: if not entry.is_cfunction:
error(pos, "'%s' redeclared" % name) warning(pos, "'%s' redeclared (ignoring second declaration)" % name)
else: else:
if defining and entry.func_cname: if defining and entry.func_cname:
error(pos, "'%s' already defined" % name) error(pos, "'%s' already defined" % name)
......
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