You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by va...@apache.org on 2009/03/09 18:27:41 UTC
svn commit: r751772 - in /lucene/pylucene/trunk/jcc: CHANGES jcc/cpp.py
jcc/python.py
Author: vajda
Date: Mon Mar 9 17:27:41 2009
New Revision: 751772
URL: http://svn.apache.org/viewvc?rev=751772&view=rev
Log:
- added 'bool' and 'operator' to list of reserved words
- added support for packages and classes named with C++ reserved words
- static methods shadowed by non-static methods of same name now '_' suffixed
Modified:
lucene/pylucene/trunk/jcc/CHANGES
lucene/pylucene/trunk/jcc/jcc/cpp.py
lucene/pylucene/trunk/jcc/jcc/python.py
Modified: lucene/pylucene/trunk/jcc/CHANGES
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/CHANGES?rev=751772&r1=751771&r2=751772&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/CHANGES (original)
+++ lucene/pylucene/trunk/jcc/CHANGES Mon Mar 9 17:27:41 2009
@@ -11,6 +11,9 @@
- added JDK variable to setup.py to better parameterize build configuration
- added support for proxying mapping and sequence protocols on FinalizerProxy
- changed Type suffix to $$Type to avoid clashes with like-named Java classes
+ - added 'bool' and 'operator' to list of reserved words
+ - added support for packages and classes named with C++ reserved words
+ - static methods shadowed by non-static methods of same name now '_' suffixed
-
Version 2.0 -> 2.1
Modified: lucene/pylucene/trunk/jcc/jcc/cpp.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/cpp.py?rev=751772&r1=751771&r2=751772&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/cpp.py (original)
+++ lucene/pylucene/trunk/jcc/jcc/cpp.py Mon Mar 9 17:27:41 2009
@@ -70,7 +70,19 @@
'void': 'V' }
RESERVED = set(['delete', 'and', 'or', 'not', 'xor', 'union', 'NULL',
- 'register', 'const'])
+ 'register', 'const', 'bool', 'operator'])
+
+def cppname(name):
+
+ if name in RESERVED:
+ return name + '$'
+
+ return name
+
+
+def cppnames(names):
+
+ return [cppname(name) for name in names]
def typename(cls, current, const):
@@ -89,10 +101,10 @@
const = False
elif cls == current:
- name = cls.getName().split('.')[-1]
+ name = cppname(cls.getName().split('.')[-1])
else:
- name = cls.getName().replace('.', '::')
+ name = '::'.join([cppname(name) for name in cls.getName().split('.')])
if const:
return "const %s&" %(name)
@@ -209,9 +221,9 @@
for name, entries in namespace.iteritems():
if entries is True:
- line(out, indent, 'class %s;', name)
+ line(out, indent, 'class %s;', cppname(name))
else:
- line(out, indent, 'namespace %s {', name)
+ line(out, indent, 'namespace %s {', cppname(name))
forward(out, entries, indent + 1)
line(out, indent, '}')
@@ -378,6 +390,7 @@
typeset.add(findClass('java/lang/Double'))
typeset.add(findClass('java/util/Iterator'))
typeset.add(findClass('java/util/Enumeration'))
+ packages.add('java.lang')
if moduleName:
cppdir = os.path.join(output, '_%s' %(moduleName))
@@ -582,15 +595,15 @@
indent = 0;
line(out)
for name in names[:-1]:
- line(out, indent, 'namespace %s {', name)
+ line(out, indent, 'namespace %s {', cppname(name))
indent += 1
line(out)
if superClsName == 'JObject':
- line(out, indent, 'class %s : public JObject {', names[-1])
+ line(out, indent, 'class %s : public JObject {', cppname(names[-1]))
else:
line(out, indent, 'class %s : public %s {',
- names[-1], '::'.join(superNames))
+ cppname(names[-1]), '::'.join(cppnames(superNames)))
line(out, indent, 'public:')
indent += 1
@@ -632,19 +645,18 @@
line(out)
line(out, indent, 'explicit %s(jobject obj) : %s(obj) {',
- names[-1], '::'.join(superNames))
+ cppname(names[-1]), '::'.join(cppnames(superNames)))
line(out, indent + 1, 'initializeClass();')
line(out, indent, '}')
line(out, indent, '%s(const %s& obj) : %s(obj) {}',
- names[-1], names[-1], '::'.join(superNames))
+ cppname(names[-1]), cppname(names[-1]),
+ '::'.join(cppnames(superNames)))
if fields:
line(out)
for field in fields:
fieldType = field.getType()
- fieldName = field.getName()
- if fieldName in RESERVED:
- fieldName += '$'
+ fieldName = cppname(field.getName())
if fieldType.isPrimitive():
line(out, indent, 'static %s %s;',
typename(fieldType, cls, False), fieldName)
@@ -669,7 +681,7 @@
for constructor in constructors:
params = [typename(param, cls, True)
for param in constructor.getParameterTypes()]
- line(out, indent, '%s(%s);', names[-1], ', '.join(params))
+ line(out, indent, '%s(%s);', cppname(names[-1]), ', '.join(params))
if methods:
line(out)
@@ -683,9 +695,7 @@
const = ' const'
params = [typename(param, cls, True)
for param in method.getParameterTypes()]
- methodName = method.getName()
- if methodName in RESERVED:
- methodName += '$'
+ methodName = cppname(method.getName())
line(out, indent, '%s%s %s(%s)%s;',
prefix, typename(method.getReturnType(), cls, False),
methodName, ', '.join(params), const)
@@ -724,30 +734,29 @@
indent = 0
line(out)
for name in names[:-1]:
- line(out, indent, 'namespace %s {', name)
+ line(out, indent, 'namespace %s {', cppname(name))
indent += 1
line(out)
- line(out, indent, 'java::lang::Class *%s::class$ = NULL;', names[-1])
- line(out, indent, 'jmethodID *%s::mids$ = NULL;', names[-1])
+ line(out, indent, 'java::lang::Class *%s::class$ = NULL;',
+ cppname(names[-1]))
+ line(out, indent, 'jmethodID *%s::mids$ = NULL;', cppname(names[-1]))
if instanceFields:
- line(out, indent, 'jfieldID *%s::fids$ = NULL;', names[-1])
+ line(out, indent, 'jfieldID *%s::fids$ = NULL;', cppname(names[-1]))
for field in fields:
fieldType = field.getType()
- fieldName = field.getName()
- if fieldName in RESERVED:
- fieldName += '$'
+ fieldName = cppname(field.getName())
typeName = typename(fieldType, cls, False)
if fieldType.isPrimitive():
line(out, indent, '%s %s::%s = (%s) 0;',
- typeName, names[-1], fieldName, typeName)
+ typeName, cppname(names[-1]), fieldName, typeName)
else:
line(out, indent, '%s *%s::%s = NULL;',
- typeName, names[-1], fieldName)
+ typeName, cppname(names[-1]), fieldName)
line(out)
- line(out, indent, 'jclass %s::initializeClass()', names[-1])
+ line(out, indent, 'jclass %s::initializeClass()', cppname(names[-1]))
line(out, indent, '{')
line(out, indent + 1, 'if (!class$)')
line(out, indent + 1, '{')
@@ -803,15 +812,14 @@
fieldName = field.getName()
if fieldType.isPrimitive():
line(out, indent + 2,
- '%s%s = env->getStatic%sField(cls, "%s");',
- fieldName, fieldName in RESERVED and '$' or '',
- fieldType.getName().capitalize(), fieldName)
+ '%s = env->getStatic%sField(cls, "%s");',
+ cppname(fieldName), fieldType.getName().capitalize(),
+ fieldName)
else:
line(out, indent + 2,
- '%s%s = new %s(env->getStaticObjectField(cls, "%s", "%s"));',
- fieldName, fieldName in RESERVED and '$' or '',
- typename(fieldType, cls, False), fieldName,
- signature(field))
+ '%s = new %s(env->getStaticObjectField(cls, "%s", "%s"));',
+ cppname(fieldName), typename(fieldType, cls, False),
+ fieldName, signature(field))
line(out, indent + 1, '}')
line(out, indent + 1, 'return (jclass) class$->this$;')
@@ -823,7 +831,8 @@
decls, args = argnames(constructor.getParameterTypes(), cls)
line(out, indent, "%s::%s(%s) : %s(env->newObject(initializeClass, &mids$, mid_init$_%s%s)) {}",
- names[-1], names[-1], decls, '::'.join(superNames),
+ cppname(names[-1]), cppname(names[-1]), decls,
+ '::'.join(cppnames(superNames)),
env.strhash(sig), args)
for method in methods:
@@ -849,9 +858,9 @@
isStatic = False
if superMethod is not None:
qualifier = 'Nonvirtual'
- this = 'this$, (jclass) %s::class$->this$' %('::'.join(superNames))
+ this = 'this$, (jclass) %s::class$->this$' %('::'.join(cppnames(superNames)))
declaringClass = superMethod.getDeclaringClass()
- midns = '%s::' %(declaringClass.getName().replace('.', '::'))
+ midns = '%s::' %(typename(declaringClass, cls, False))
else:
qualifier = ''
this = 'this$'
@@ -862,9 +871,9 @@
decls, args = argnames(params, cls)
line(out)
- line(out, indent, '%s %s::%s%s(%s)%s',
- typename(returnType, cls, False), names[-1], methodName,
- methodName in RESERVED and '$' or '', decls, const)
+ line(out, indent, '%s %s::%s(%s)%s',
+ typename(returnType, cls, False), cppname(names[-1]),
+ cppname(methodName), decls, const)
line(out, indent, '{')
if isStatic:
line(out, indent + 1, 'jclass cls = initializeClass();');
@@ -887,7 +896,7 @@
fieldName = field.getName()
line(out)
line(out, indent, '%s %s::_get_%s() const',
- typename(fieldType, cls, False), names[-1], fieldName)
+ typename(fieldType, cls, False), cppname(names[-1]), fieldName)
line(out, indent, '{')
if fieldType.isPrimitive():
line(out, indent + 1,
@@ -902,7 +911,8 @@
if not Modifier.isFinal(field.getModifiers()):
line(out)
line(out, indent, 'void %s::_set_%s(%s a0) const',
- names[-1], fieldName, typename(fieldType, cls, True))
+ cppname(names[-1]), fieldName,
+ typename(fieldType, cls, True))
line(out, indent, '{')
if fieldType.isPrimitive():
line(out, indent + 1,
Modified: lucene/pylucene/trunk/jcc/jcc/python.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/python.py?rev=751772&r1=751771&r2=751772&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/python.py (original)
+++ lucene/pylucene/trunk/jcc/jcc/python.py Mon Mar 9 17:27:41 2009
@@ -14,8 +14,9 @@
import os, sys, platform, shutil, _jcc, py_compile
-from cpp import PRIMITIVES, RESERVED, INDENT, HALF_INDENT
-from cpp import typename, line, signature, find_method, split_pkg, sort
+from cpp import PRIMITIVES, INDENT, HALF_INDENT
+from cpp import cppname, cppnames, typename
+from cpp import line, signature, find_method, split_pkg, sort
from cpp import Modifier
from _jcc import findClass
from config import INCLUDES, CFLAGS, DEBUG_CFLAGS, LFLAGS, SHARED
@@ -91,7 +92,7 @@
line(out, indent, '%s a%d%s;',
typename(params[i], cls, False), i,
not params[i].isPrimitive() and '((jobject) NULL)' or '')
- line(out, indent, '%s object((jobject) NULL);', names[-1])
+ line(out, indent, '%s object((jobject) NULL);', cppname(names[-1]))
line(out)
if count:
@@ -101,7 +102,7 @@
indent += 1
line(out, indent, 'INT_CALL(object = %s(%s));',
- names[-1], ', '.join(['a%d' %(i) for i in xrange(count)]))
+ cppname(names[-1]), ', '.join(['a%d' %(i) for i in xrange(count)]))
line(out, indent, 'self->object = object;')
if inCase:
line(out, indent, 'break;')
@@ -217,11 +218,10 @@
line(out, indent, '{')
indent += 1
- if name in RESERVED:
- name += '$'
+ name = cppname(name)
if Modifier.isStatic(modifiers):
line(out, indent, 'OBJ_CALL(%s%s::%s(%s));',
- result, '::'.join(names), name,
+ result, '::'.join(cppnames(names)), name,
', '.join(['a%d' %(i) for i in xrange(count)]))
else:
line(out, indent, 'OBJ_CALL(%sself->object.%s(%s));',
@@ -292,7 +292,7 @@
def extension(env, out, indent, cls, names, name, count, method):
line(out, indent, 'jlong ptr = jenv->CallLongMethod(jobj, %s::mids$[%s::mid_pythonExtension_%s]);',
- names[-1], names[-1], env.strhash('()J'))
+ cppname(names[-1]), cppname(names[-1]), env.strhash('()J'))
line(out, indent, 'PyObject *obj = (PyObject *) (Py_intptr_t) ptr;')
if name == 'pythonDecRef':
@@ -300,7 +300,7 @@
line(out, indent, 'if (obj != NULL)')
line(out, indent, '{')
line(out, indent + 1, 'jenv->CallVoidMethod(jobj, %s::mids$[%s::mid_pythonExtension_%s], (jlong) 0);',
- names[-1], names[-1], env.strhash('(J)V'))
+ cppname(names[-1]), cppname(names[-1]), env.strhash('(J)V'))
line(out, indent + 1, 'env->finalizeObject(jenv, obj);')
line(out, indent, '}')
return
@@ -398,7 +398,7 @@
indent = 0
for name in names[:-1]:
- line(out_h, indent, 'namespace %s {', name)
+ line(out_h, indent, 'namespace %s {', cppname(name))
indent += 1
line(out_h, indent, 'extern PyTypeObject %s$$Type;', names[-1])
@@ -406,9 +406,9 @@
line(out_h, indent, 'class t_%s {', names[-1])
line(out_h, indent, 'public:')
line(out_h, indent + 1, 'PyObject_HEAD')
- line(out_h, indent + 1, '%s object;', names[-1])
+ line(out_h, indent + 1, '%s object;', cppname(names[-1]))
line(out_h, indent + 1, 'static PyObject *wrap_Object(const %s&);',
- names[-1])
+ cppname(names[-1]))
line(out_h, indent + 1, 'static PyObject *wrap_jobject(const jobject&);')
line(out_h, indent + 1, 'static void install(PyObject *module);')
line(out_h, indent + 1, 'static void initialize(PyObject *module);')
@@ -442,7 +442,7 @@
line(out)
indent = 0
for name in names[:-1]:
- line(out, indent, 'namespace %s {', name)
+ line(out, indent, 'namespace %s {', cppname(name))
indent += 1
if not isExtension:
@@ -481,7 +481,27 @@
extMethods.setdefault(name, []).append(method)
if superMethod or not (isExtension and isNative and not isStatic):
- allMethods.setdefault(name, []).append(method)
+ if isStatic:
+ if name in allMethods:
+ if Modifier.isStatic(allMethods[name][0].getModifiers()):
+ allMethods[name].append(method)
+ elif name + '_' in allMethods:
+ allMethods[name + '_'].append(method)
+ else:
+ print >>sys.stderr, "Renaming static method %s on class %s to %s_ since it is shadowed by non-static method of same name." %(name, '.'.join(names), name)
+ allMethods[name + '_'] = [method]
+ else:
+ allMethods[name] = [method]
+ else:
+ if name in allMethods:
+ if Modifier.isStatic(allMethods[name][0].getModifiers()):
+ print >>sys.stderr, "Renaming static method %s on class %s to %s_ since it is shadowed by non-static method of same name." %(name, '.'.join(names), name)
+ allMethods[name + '_'] = allMethods[name]
+ allMethods[name] = [method]
+ else:
+ allMethods[name].append(method)
+ else:
+ allMethods[name] = [method]
if not (isExtension and isNative):
nameLen = len(name)
@@ -522,7 +542,7 @@
if sequence:
sequenceLenName, sequenceLenSig = sequence[0].split(':')
sequenceGetName, sequenceGetSig = sequence[1].split(':')
-
+
for name, methods in allMethods:
args, x, cardinality = methodargs(methods, superMethods)
sort(methods, key=lambda x: len(x.getParameterTypes()))
@@ -775,11 +795,14 @@
else:
tp_as_sequence = '0'
+ if len(superNames) > 1:
+ base = '::'.join(('::'.join(cppnames(superNames[:-1])), superNames[-1]))
+ else:
+ base = superNames[-1]
line(out)
line(out, indent, 'DECLARE_TYPE(%s, t_%s, %s, %s, %s, %s, %s, %s, %s, %s);',
- names[-1], names[-1], '::'.join(superNames), names[-1],
- constructorName, tp_iter, tp_iternext, tp_getset, tp_as_mapping,
- tp_as_sequence)
+ names[-1], names[-1], base, cppname(names[-1]), constructorName,
+ tp_iter, tp_iternext, tp_getset, tp_as_mapping, tp_as_sequence)
line(out)
line(out, indent, 'void t_%s::install(PyObject *module)', names[-1])
@@ -800,14 +823,15 @@
line(out, indent, 'void t_%s::initialize(PyObject *module)', names[-1])
line(out, indent, '{')
line(out, indent + 1, 'PyDict_SetItemString(%s$$Type.tp_dict, "class_", make_descriptor(%s::initializeClass));',
- names[-1], names[-1])
+ names[-1], cppname(names[-1]))
line(out, indent + 1, 'PyDict_SetItemString(%s$$Type.tp_dict, "wrapfn_", make_descriptor(t_%s::wrap_jobject));',
names[-1], names[-1])
if isExtension:
- line(out, indent + 1, 'jclass cls = %s::initializeClass();', names[-1]);
+ line(out, indent + 1, 'jclass cls = %s::initializeClass();',
+ cppname(names[-1]))
elif fields:
- line(out, indent + 1, '%s::initializeClass();', names[-1]);
+ line(out, indent + 1, '%s::initializeClass();', cppname(names[-1]))
if isExtension:
count = 0
@@ -824,9 +848,7 @@
for field in fields:
fieldType = field.getType()
fieldName = field.getName()
- value = '%s::%s' %(names[-1], fieldName)
- if fieldName in RESERVED:
- value += '$'
+ value = '%s::%s' %(cppname(names[-1]), cppname(fieldName))
value = fieldValue(cls, value, fieldType)
line(out, indent + 1, 'PyDict_SetItemString(%s$$Type.tp_dict, "%s", make_descriptor(%s));',
names[-1], fieldName, value)
@@ -836,14 +858,14 @@
line(out)
line(out, indent, 'static PyObject *t_%s_cast_(PyTypeObject *type, PyObject *arg)', names[-1])
line(out, indent, '{')
- line(out, indent + 1, 'if (!(arg = castCheck(arg, %s::initializeClass(), 1)))', names[-1])
+ line(out, indent + 1, 'if (!(arg = castCheck(arg, %s::initializeClass(), 1)))', cppname(names[-1]))
line(out, indent + 2, 'return NULL;')
- line(out, indent + 1, 'return t_%s::wrap_Object(%s(((t_%s *) arg)->object.this$));', names[-1], names[-1], names[-1])
+ line(out, indent + 1, 'return t_%s::wrap_Object(%s(((t_%s *) arg)->object.this$));', names[-1], cppname(names[-1]), names[-1])
line(out, indent, '}')
line(out, indent, 'static PyObject *t_%s_instance_(PyTypeObject *type, PyObject *arg)', names[-1])
line(out, indent, '{')
- line(out, indent + 1, 'if (!castCheck(arg, %s::initializeClass(), 0))', names[-1])
+ line(out, indent + 1, 'if (!castCheck(arg, %s::initializeClass(), 0))', cppname(names[-1]))
line(out, indent + 2, 'Py_RETURN_FALSE;')
line(out, indent + 1, 'Py_RETURN_TRUE;')
line(out, indent, '}')
@@ -892,10 +914,12 @@
for name, methods in allMethods:
line(out)
modifiers = methods[0].getModifiers()
+
if isExtension and name == 'clone' and Modifier.isNative(modifiers):
declargs, args, cardinality = ', PyObject *arg', ', arg', 1
else:
declargs, args, cardinality = methodargs(methods, superMethods)
+
static = Modifier.isStatic(modifiers)
if static:
line(out, indent, 'static PyObject *t_%s_%s(PyTypeObject *type%s)',
@@ -1143,12 +1167,12 @@
if names:
line(out)
for name in names:
- line(out, indent, 'namespace %s {', name)
+ line(out, indent, 'namespace %s {', cppname(name))
indent += 1
line(out);
for name, entries in packages:
- line(out, indent, 'namespace %s {', name)
+ line(out, indent, 'namespace %s {', cppname(name))
line(out, indent + 1, 'void __install__(PyObject *module);')
line(out, indent + 1, 'void __initialize__(PyObject *module);')
line(out, indent, '}')
@@ -1159,7 +1183,7 @@
for name in types:
line(out, indent + 1, 't_%s::install(module);', name)
for name, entries in packages:
- line(out, indent + 1, '%s::__install__(module);', name)
+ line(out, indent + 1, '%s::__install__(module);', cppname(name))
line(out, indent, '}')
line(out)
@@ -1177,7 +1201,7 @@
for name in types:
line(out, indent + 1, 't_%s::initialize(module);', name)
for name, entries in packages:
- line(out, indent + 1, '%s::__initialize__(module);', name)
+ line(out, indent + 1, '%s::__initialize__(module);', cppname(name))
if not names:
line(out)
line(out, indent + 1, 'return env;')