sim: Include object header files in SWIG interfaces

When casting objects in the generated SWIG interfaces, SWIG uses
classical C-style casts ( (Foo *)bar; ). In some cases, this can
degenerate into the equivalent of a reinterpret_cast (mainly if only a
forward declaration of the type is available). This usually works for
most compilers, but it is known to break if multiple inheritance is
used anywhere in the object hierarchy.

This patch introduces the cxx_header attribute to Python SimObject
definitions, which should be used to specify a header to include in
the SWIG interface. The header should include the declaration of the
wrapped object. We currently don't enforce header the use of the
header attribute, but a warning will be generated for objects that do
not use it.
This commit is contained in:
Andreas Sandberg
2012-11-02 11:32:01 -05:00
parent 044a652587
commit c0ab52799c
122 changed files with 232 additions and 22 deletions

View File

@@ -105,6 +105,9 @@ allClasses = {}
# dict to look up SimObjects based on path
instanceDict = {}
# Did any of the SimObjects lack a header file?
noCxxHeader = False
def public_value(key, value):
return key.startswith('_') or \
isinstance(value, (FunctionType, MethodType, ModuleType,
@@ -119,6 +122,7 @@ class MetaSimObject(type):
init_keywords = { 'abstract' : bool,
'cxx_class' : str,
'cxx_type' : str,
'cxx_header' : str,
'type' : str }
# Attributes that can be set any time
keywords = { 'check' : FunctionType }
@@ -203,6 +207,12 @@ class MetaSimObject(type):
cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class']
if 'cxx_header' not in cls._value_dict:
global noCxxHeader
noCxxHeader = True
print >> sys.stderr, \
"warning: No header file specified for SimObject: %s" % name
# Export methods are automatically inherited via C++, so we
# don't want the method declarations to get inherited on the
# python side (and thus end up getting repeated in the wrapped
@@ -407,6 +417,7 @@ class MetaSimObject(type):
code('#include "params/$cls.hh"')
for param in params:
param.cxx_predecls(code)
code('#include "${{cls.cxx_header}}"')
cls.export_method_cxx_predecls(code)
code('''\
/**
@@ -568,15 +579,7 @@ class SimObject(object):
__metaclass__ = MetaSimObject
type = 'SimObject'
abstract = True
@classmethod
def export_method_cxx_predecls(cls, code):
code('''
#include <Python.h>
#include "sim/serialize.hh"
#include "sim/sim_object.hh"
''')
cxx_header = "sim/sim_object.hh"
@classmethod
def export_method_swig_predecls(cls, code):
@@ -1099,10 +1102,11 @@ baseClasses = allClasses.copy()
baseInstances = instanceDict.copy()
def clear():
global allClasses, instanceDict
global allClasses, instanceDict, noCxxHeader
allClasses = baseClasses.copy()
instanceDict = baseInstances.copy()
noCxxHeader = False
# __all__ defines the list of symbols that get exported when
# 'from config import *' is invoked. Try to keep this reasonably