def resolveInterfacePath(self, decl_or_type): name = builtin_plugin.getJavaBuiltinType(self.target_info, decl_or_type.pointeeType) cname = 'Const' + name[0].upper() + name[1:] vname = 'C' + name[0].upper() + name[1:] cpath = jpath.JPath(("primitives", cname, 'Ptr')) vpath = jpath.JPath(("primitives", vname, 'Ptr')) if decl_or_type.pointeeType.isConstQualified: name = cname klass = jclass.Interface self.creator.setNamespace(jclass.Class, vpath) else: name = vname klass = jclass.Class self.creator.setNamespace(jclass.Interface, jpath.JPath(( "primitives", cname, ))) self.creator.setNamespace(jclass.Interface, cpath) if decl_or_type.pointeeType.spelling != "void": self.creator.getClass( bc.LValueReferenceType( bc.BuiltinType(decl_or_type.pointeeType.spelling))) self.creator.resolveInterfacePath( bc.PointerType(bc.BuiltinType("void"))) self.creator.getClass(bc.PointerType(bc.BuiltinType("void"))) self.creator.setNamespace(jclass.Interface, jpath.JPath(( "primitives", name, ))) pjp = jpath.JPath(("primitives", name, 'Ptr')) self.classes[decl_or_type.pointeeType.spelling] = cpath, vpath return klass, pjp
def listConstructors(self, decl): l = [] # if decl.hasDefaultConstructor: # dd = filter(lambda x: x.isDefaultConstructor, decl.ctors) # if not dd: # l.append(bc.CXXConstructorDecl( # decl.path, None, bc.BuiltinType( # "void"), [], decl)) if decl.hasDefaultConstructor and not decl.hasUserDeclaredConstructor: l.append( bc.CXXConstructorDecl(decl.cname(), None, bc.BuiltinType("void"), [], decl)) return l # + decl.ctors
def setter(self, class_decl, decl, const): if const: return [] name = decl.path n = name.split('::') name = 'set_' + n[-1] if len(n) > 1: name = '::'.join(n[:-1] + [name]) arg_type = decl.type if isinstance(arg_type, bc.SubstTemplateTypeParmType): arg_type = arg_type.sugar arg_type = arg_type.shallowCopy() arg_type.isConstQualified = True l = [] l.append( CXXSetterDecl(name, None, bc.BuiltinType("void"), [bc.ParmVarDecl("var", arg_type)], class_decl)) if not isinstance(arg_type, bc.LValueReferenceType): arg_type = bc.LValueReferenceType(arg_type) l.append( CXXSetterDecl(name, None, bc.BuiltinType("void"), [bc.ParmVarDecl("var", arg_type)], class_decl)) return l
def linkEnd(self): if not self.__flag: return klass = self.creator.getClass(STRING_REF_CLASS_PATH) f = StringRefGetDecl("StringRef::get", None, STRING_TYPE, [], bc.CXXRecordDecl("int")) conv = self.creator.getFunctionConverter(f) klass.addFunction(conv) f = StringRefSetDecl("StringRef::set", None, bc.BuiltinType("void"), [ bc.ParmVarDecl("arg", STRING_TYPE)], bc.CXXRecordDecl("int")) conv = self.creator.getFunctionConverter(f) klass.addFunction(conv)
def linkEnd(self): for i in self.classes: decl = self.classes[i].pointeeType.decl isConstQualified = self.classes[i].pointeeType.isConstQualified c = self.creator.getClass(self.classes[i]) intf = self.creator.getInterface(self.classes[i].pointeeType) intf2 = self.creator.getClass(self.classes[i].pointeeType) # print ">>", i, self.classes[i] fn = 'at' if isConstQualified: fn = 'cAt' f = PointerAtDecl( decl.name + "::" + fn, None, bc.LValueReferenceType( bc.RecordType(decl, isConstQualified=isConstQualified)), [bc.ParmVarDecl( "index", bc.BuiltinType("int"), )], decl, isConstQualified=isConstQualified) conv = self.creator.getFunctionConverter(f) c.addFunction(conv) fn = 'ref' if isConstQualified: fn = 'cref' f = getPointerFunctionDecl( decl.name + "::" + fn, None, bc.PointerType( bc.RecordType(decl, isConstQualified=isConstQualified)), [], decl) conv = self.creator.getFunctionConverter(f) # print intf, intf2, conv intf.addFunction(conv) intf2.addFunction(conv) if not isConstQualified: d = self.creator.getClass( bc.PointerType(bc.RecordType(decl, isConstQualified=True))) c.setBaseClass(d)
def declare(self, decl): if not isinstance(decl, bc.RecordDecl): return False if decl.access != "public" and decl.access != "none": return False # print decl, "!" * 10 # decl.show() if decl.describedClassTemplate is not None: return False self.classes.add(decl) for i in self.listBases(decl): self.classes.add(i) cc = bc.RecordType(decl, isConstQualified=True) nc = bc.RecordType(decl, isConstQualified=False) cls = [ self.creator.getInterface(cc), self.creator.getInterface(nc), self.creator.getClass(nc), ] cls[2].is_valid = not decl.isAbstract cls[1].addBase(cls[0]) cls[2].addBase(cls[1]) # for i in self.listMethods(decl, const=True): for i in self.listAllMethods(decl, const=True): conv = self.creator.getFunctionConverter(i, decl) if not conv.isStatic(): cls[0].addFunction(conv) # cls[2].addFunction(conv) # for i in self.listMethods(decl, const=False): for i in self.listAllMethods(decl, const=False): conv = self.creator.getFunctionConverter(i, decl) if not conv.isStatic(): cls[1].addFunction(conv) # cls[2].addFunction(conv) if not decl.isAbstract: for i in self.listAllMethods(decl): conv = self.creator.getFunctionConverter(i, decl) cls[2].addFunction(conv) for i in self.listConstructors(decl): conv = self.creator.getFunctionConverter(i, decl) cls[2].addFunction(conv) if self.hasCopyConstructor(decl): f = CXXCopyDecl(decl.cname() + "::__copy__", None, bc.RecordType(decl), [], decl) conv = self.creator.getFunctionConverter(f, decl) cls[0].addFunction(conv) if not decl.isAbstract: cls[2].addFunction(conv) f = CXXAssignOperatorDecl( decl.cname() + "::__assign__", None, bc.BuiltinType("void"), [ bc.ParmVarDecl( "arg", bc.LValueReferenceType( bc.RecordType(decl, isConstQualified=True)), ), ], decl) conv = self.creator.getFunctionConverter(f, decl) cls[1].addFunction(conv) if not decl.isAbstract: cls[2].addFunction(conv) return True
def listAllMethods(self, decl, const=False): bases = self.listBases(decl) declared = set() overriden = set() signatures = set() dup_methods = {} def calc_signature(d): fname = d.path.split('::')[-1] # return fname, tuple([self.creator.resolveClass(x.type)[1] for x in d.params]) return fname, tuple([ x.name if x.name else self.creator.resolveClass(x.type)[1] for x in d.params ]) if not const: if (decl.hasDefaultConstructor and (not decl.hasUserDeclaredConstructor) and decl.needsImplicitDefaultConstructor): flag = True for c in decl.ctors: if len(c.params) == 0: flag = False if flag and (not decl.isAbstract): d = bc.CXXConstructorDecl(decl.path, None, bc.BuiltinType("void"), [], decl) dl = dup_methods.get('init', []) dl.append(d) dup_methods['init'] = dl declared.add(d) # declared.update(decl.ctors) for d in [decl] + bases: for i in d.methods: if d is not decl and isinstance(i, bc.CXXConstructorDecl): continue if const and isinstance(i, bc.CXXConstructorDecl): continue if decl.isAbstract and isinstance(i, bc.CXXConstructorDecl): continue if i.access != "public": continue if ((const is not None) and (i.isConst ^ const)): continue if i.name.startswith('operator'): continue if i.isDeleted: continue # if 'StaticOutputStream' in decl.path and isinstance(i, bc.CXXConstructorDecl): # print("=" * 80) # print(i) sign = calc_signature(i) if sign in signatures: continue signatures.add(calc_signature(i)) declared.add(i) map(overriden.add, i.overridden_methods) fname = i.path.split('::')[-1] if isinstance(i, bc.CXXConstructorDecl): fname = 'init' dl = dup_methods.get(fname, []) dl.append(i) dup_methods[fname] = dl for fname, flist in dup_methods.items(): # if 'StaticOutputStream' in decl.path: # print("=" * 80) # print(decl) # print(flist) # decl.show() if len(flist) <= 1: continue arg_infos = [calc_signature(x)[1] for x in flist] rename = {} for d, arg in zip(flist, arg_infos): if not 'x'.join(arg): continue l = rename.get(len(arg), []) l.append((d, arg)) rename[len(arg)] = l for len_arg, rename_list in rename.items(): for d, arg in rename_list: d._objc_name = str(len_arg) + '_' + '_'.join( x.replace('_', '') for x in arg) declared.difference_update(overriden) m = list(declared) return sorted(m, key=lambda x: x.path)
def declare(self, decl): # print("!" * 80) # print("field !!") # print(decl) # print(isinstance(decl, bc.RecordDecl)) if not isinstance(decl, bc.RecordDecl): return False # print("decl.describedClassTemplate: ", decl.describedClassTemplate) # TODO: bug fix,,, # if decl.describedClassTemplate is not None: # return False # print("access = ", decl.access) if decl.access != "public" and decl.access != "none": return False cc = bc.RecordType(decl, isConstQualified=True) nc = bc.RecordType(decl, isConstQualified=False) cc = self.creator.getClass(cc) nc = self.creator.getClass(nc) cc.setImpl(decl) nc.addBase(cc) nc.priority = 10 for i in self.listAllMethods(decl, const=True): conv = self.creator.getFunctionConverter(i) cc.addFunction(conv) for i in self.listAllMethods(decl, const=False): conv = self.creator.getFunctionConverter(i) nc.addFunction(conv) # print("=" * 80) # print("field !!") # print(decl) for field in self.listAllFields(decl): # print(field) ft = field.type if isinstance(ft, bc.SubstTemplateTypeParmType): ft = ft.sugar if (isinstance(base.eraseTypedef(ft), bc.BuiltinType) or isinstance(base.eraseTypedef(ft), bc.EnumType)): rt = ft else: rt = bc.LValueReferenceType(ft, isConstQualified=True) f = CXXGetterDecl("%s::get_%s" % (decl.path, field.name), None, rt, [], decl) f.field_name = field.name gconv = CXXGetterConverter(self.creator, f) cc.addFunction(gconv) if not (isinstance(base.eraseTypedef(ft), bc.BuiltinType) or isinstance(base.eraseTypedef(ft), bc.EnumType)): rt = bc.LValueReferenceType(ft, isConstQualified=False) f = CXXGetterDecl("%s::get_%s" % (decl.path, field.name), None, rt, [], decl) f.field_name = field.name gconv = CXXGetterConverter(self.creator, f) nc.addFunction(gconv) f = CXXSetterDecl("%s::set_%s" % (decl.path, field.name), None, bc.BuiltinType("void"), [ bc.ParmVarDecl("arg", ft), ], decl) f.field_name = field.name sconv = CXXSetterConverter(self.creator, f) nc.addFunction(sconv) t = gconv.return_converter.getObjCType() if not gconv.return_converter.isValid(): continue if gconv.isValid(): cc.addProperty( objc_class.Property(t, field.name, getter=gconv.getName(), other_attribs='assign, nonatomic')) gname = gconv.getName() else: gname = None if sconv.isValid(): # t = sconv.arg_converters[0].getObjCType() t = gconv.return_converter.getObjCType() nc.addProperty( objc_class.Property(t, field.name, getter=gname, setter=sconv.getName(), other_attribs='assign, nonatomic')) if False and self.hasCopyConstructor(decl): f = CXXCopyDecl(decl.path + "::__copy__", None, bc.RecordType(decl), [], decl) conv = self.creator.getFunctionConverter(f) cc.addFunction(conv) f = CXXAssignOperatorDecl( decl.path + "::__assign__", None, bc.BuiltinType("void"), [ bc.ParmVarDecl( "arg", bc.LValueReferenceType( bc.RecordType(decl, isConstQualified=True)), ), ], decl) conv = self.creator.getFunctionConverter(f) nc.addFunction(conv) return True
def linkEnd(self): for spelling, (cpath, vpath) in self.classes.items(): if spelling == 'void': void_ptr = cpath, vpath for spelling, (cpath, vpath) in self.classes.items(): # print spelling, cpath, vpath cklass = self.creator.getInterface(cpath) klass = self.creator.getClass(vpath) klass.addBase(cklass) if spelling == 'void': t = builtin_reference_plugin.builtin_class_wrapper[spelling] f = WrapVoidPtrDecl( "wrap", None, bc.PointerType(bc.BuiltinType("void")), [WrapVoidPtrArg( "ptr", bc.BuiltinType("void"), )], t, isStatic=True) conv = self.creator.getFunctionConverter(f) klass.addFunction(conv) else: klass.setBaseClass(self.creator.getClass(void_ptr[1])) t = builtin_reference_plugin.builtin_class_wrapper[spelling] f = PointerAtDecl( "at", None, bc.LValueReferenceType(bc.BuiltinType(spelling)), [bc.ParmVarDecl( "index", bc.BuiltinType("int"), )], t) conv = self.creator.getFunctionConverter(f) klass.addFunction(conv) f = PointerCAtDecl( "cAt", None, bc.LValueReferenceType( bc.BuiltinType(spelling, isConstQualified=True)), [bc.ParmVarDecl("index", bc.BuiltinType("int"))], t, isConstQualified=True) conv = self.creator.getFunctionConverter(f) cklass.addFunction(conv) klass.addFunction(conv) f = WrapArrayDecl( "wrap", None, bc.PointerType(bc.BuiltinType(spelling)), # f = WrapArrayDecl(t, bc.BuiltinType("void"), [ WrapArrayArg( "arg", bc.PointerType(bc.BuiltinType(spelling)), ) ], t, isStatic=True) conv = self.creator.getFunctionConverter(f) klass.addFunction(conv)
def resolveClassPath(self, decl_or_type): if decl_or_type.pointeeType.isConstQualified: decl_or_type = bc.PointerType( bc.BuiltinType(decl_or_type.pointeeType.spelling)) return self.resolveInterfacePath(decl_or_type)
def __init__(self, creator, decl, func_conv): arg = bc.ParmVarDecl("_this", bc.PointerType(bc.BuiltinType(decl.name))) super(InstanceArgConverter, self).__init__(creator, arg, func_conv)
def linkEnd(self): base = self.creator.getBaseClass() f = bc.FunctionDecl("", None, bc.BuiltinType("void"), []) f = FinalizeFunctionConverter(self.creator, f) base.addFunction(f)
# coding: utf-8 from blueboss import common as bc import plugin import string_plugin import jpath import jclass import builtin_reference_plugin STRING_REF_CLASS_PATH = jpath.JPath(("primitives", "StringRef")) STRING_TYPE = bc.TemplateSpecializationType() STRING_TYPE.sugar = bc.RecordType(bc.RecordDecl('std::basic_string')) STRING_TYPE.args = [ bc.TemplateArgument() ] STRING_TYPE.args[0].type = bc.BuiltinType("char") class StringRefGetDecl(bc.CXXMethodDecl): pass class StringRefSetDecl(bc.CXXMethodDecl): pass class InstanceArgConverter(builtin_reference_plugin.InstanceArgConverter): def getCTypeName(self): return "std::string"