def get_object_virtual_methods(self): class_instance_ctype = nodes.PrimaryType('Object') class_instance_ctype.push(nodes.PointerType()) class_instance_decl = nodes.Decl('', class_instance_ctype) string_ctype = nodes.PrimaryType('char') string_ctype.push(nodes.PointerType()) string_decl = nodes.Decl('', string_ctype) virtuals = {} virtuals['isKindOf'] = [] virtuals['isInstanceOf'] = [] # int isKindOf(Class *, Class *other) isKindOf_ctype = nodes.FuncType('int', [class_instance_decl, class_instance_decl]) virtuals['isKindOf'].append(nodes.Decl('isKindOf', isKindOf_ctype)) # int isKindOf(Class *, char *name) isKindOfStr_ctype = nodes.FuncType('int', [class_instance_decl, string_decl]) virtuals['isKindOf'].append(nodes.Decl('isKindOfStr', isKindOfStr_ctype)) # int isInstanceOf(Class *, Class *other) isInstanceOf_ctype = nodes.FuncType('int', [class_instance_decl, class_instance_decl]) virtuals['isInstanceOf'].append(nodes.Decl('isInstanceOf', isInstanceOf_ctype)) # int isInstanceOf(Class *, char *name) isInstanceOfStr_ctype = nodes.FuncType('int', [class_instance_decl, string_decl]) virtuals['isInstanceOf'].append(nodes.Decl('isInstanceOfStr', isInstanceOfStr_ctype)) return virtuals
def add_pointer(self, lspec): if not hasattr(lspec, 'ctype'): lspec.ctype = nodes.makeCType('int', lspec.ctype) if not hasattr(lspec.ctype, 'push'): return False lspec.ctype.push(nodes.PointerType()) return True
def build_metadata_struct(self, klass): struct_ctype = nodes.ComposedType('kc_' + klass.name + '_metadata') struct_ctype._specifier = nodes.Specifiers.STRUCT struct_ctype._attr_composed = ['__attribute__((packed))'] struct_ctype.fields = [] # class name cname_ctype = nodes.PrimaryType('char') cname_ctype.push(nodes.PointerType()) struct_ctype.fields.append(nodes.Decl('name', cname_ctype)) # inheritance list inhlist_ctype = nodes.PrimaryType('char') inhlist_ctype.push(nodes.PointerType()) struct_ctype.fields.append(nodes.Decl('inheritance_list', inhlist_ctype)) # vtable vtable_ctype = nodes.PrimaryType('kc_' + klass.name + '_vtable') struct_ctype.fields.append(nodes.Decl('vtable', vtable_ctype)) return nodes.Decl('', struct_ctype)
def build_interface_struct(self, klass): interface_struct_ctype = nodes.ComposedType('kc_' + klass.name + '_interface') interface_struct_ctype._specifier = nodes.Specifiers.STRUCT interface_struct_ctype._attr_composed = ['__attribute__((packed))'] interface_struct_ctype.fields = [] # meta field meta_ctype = nodes.PrimaryType('kc_' + klass.name + '_metadata') meta_ctype.push(nodes.PointerType()) interface_struct_ctype.fields.append(nodes.Decl('meta', meta_ctype)) # instance field instance_ctype = nodes.PrimaryType('kc_' + klass.name + '_instance') interface_struct_ctype.fields.append(nodes.Decl('instance', instance_ctype)) return nodes.Decl('', interface_struct_ctype)
def func_add_param_self(func, class_name): instance_type = nodes.PrimaryType(class_name) instance_type.push(nodes.PointerType()) self_param = nodes.Decl('self', instance_type) func._params.insert(0, self_param)
def kc_new_literal_string(self, ast, val): string_type = nodes.PrimaryType('char') string_type.push(nodes.PointerType()) ast.set(knodes.KcTypedLiteral(self.value(val), string_type)) return True
def first_pointer(self, lspec): if not hasattr(lspec, 'ctype') or lspec.ctype is None: lspec.ctype = nodes.makeCType('int', lspec.ctype) lspec.ctype.push(nodes.PointerType()) return True
def test_01_basicdecl(self): """Test cnorm nodes construction""" d = nodes.Decl('a') self.assertEqual(str(d.to_c()), "int a;\n", "Failed to convert to C") vars(d)["_name"] = 'b' qual = d.ctype qual = qual.link(nodes.PointerType()) qual = qual.link(nodes.QualType(nodes.Qualifiers.VOLATILE)) self.assertEqual(str(d.to_c()), "volatile int *b;\n", "Failed to convert to C") vars(d)["_name"] = 'c' qual = d.ctype qual = qual.link(nodes.QualType(nodes.Qualifiers.CONST)) qual = qual.link(nodes.PointerType()) qual = qual.link(nodes.QualType(nodes.Qualifiers.VOLATILE)) self.assertEqual(str(d.to_c()), "volatile int *const c;\n", "Failed to convert to C") vars(d)["_name"] = 'd' qual = d.ctype qual = qual.link(nodes.PointerType()) qual = qual.link(nodes.ParenType()) qual = qual.link(nodes.QualType(nodes.Qualifiers.CONST)) qual = qual.link(nodes.PointerType()) qual = qual.link(nodes.QualType(nodes.Qualifiers.VOLATILE)) self.assertEqual(str(d.to_c()), "volatile int *const (*d);\n", "Failed to convert to C") vars(d)["_name"] = 'e' qual = d.ctype qual = qual.link(nodes.ArrayType()) qual = qual.link(nodes.ArrayType()) qual = qual.link(nodes.PointerType()) qual = qual.link(nodes.ParenType()) qual = qual.link(nodes.QualType(nodes.Qualifiers.CONST)) qual = qual.link(nodes.PointerType()) qual = qual.link(nodes.QualType(nodes.Qualifiers.VOLATILE)) self.assertEqual(str(d.to_c()), "volatile int *const (*e[][]);\n", "Failed to convert to C") d = nodes.Decl('tf', nodes.PrimaryType('double')) qual = d.ctype qual = qual.link(nodes.ArrayType()) qual = qual.link(nodes.QualType(nodes.Qualifiers.CONST)) self.assertEqual(str(d.to_c()), "const double tf[];\n", "Failed to convert to C") ft = nodes.FuncType('double', [ nodes.Decl('a', nodes.PrimaryType('size_t')), nodes.Decl('b', nodes.PrimaryType('int')) ]) f = nodes.Decl('f', ft) self.assertEqual(str(f.to_c()), "double f(size_t a, int b);\n", "Failed to convert to C") f = nodes.Decl('f', nodes.PrimaryType('double')) qual = f.ctype qual = qual.link(nodes.PointerType()) qual = qual.link( nodes.ParenType([ nodes.Decl('a', nodes.PrimaryType('size_t')), nodes.Decl('b', nodes.PrimaryType('int')) ])) self.assertEqual(str(f.to_c()), "double (*f)(size_t a, int b);\n", "Failed to convert to C") ft2 = nodes.FuncType('double', [nodes.Decl('p', nodes.PrimaryType('ext_func'))]) f2 = nodes.Decl('f2', ft2) qual = f2.ctype qual = qual.link(nodes.PointerType()) qual = qual.link( nodes.ParenType([ nodes.Decl('a', nodes.PrimaryType('size_t')), nodes.Decl('b', nodes.PrimaryType('int')) ])) self.assertEqual(str(f2.to_c()), "double (*f2(ext_func p))(size_t a, int b);\n", "Failed to convert to C") ## test CTYPE construction ctype = nodes.makeCType('int') d = nodes.Decl('ghh', ctype) self.assertEqual(str(d.to_c()), "int ghh;\n", "Failed to convert to C") ctype = nodes.makeCType('const') ctype = nodes.makeCType('double', ctype) d = nodes.Decl('ghh', ctype) self.assertEqual(str(d.to_c()), "const double ghh;\n", "Failed to convert to C") ctype = nodes.makeCType('__int8', ctype) ctype = nodes.makeCType('__unsigned__', ctype) ctype = nodes.makeCType('extern', ctype) d = nodes.Decl('ghh', ctype) self.assertEqual(str(d.to_c()), "extern const unsigned __int8 ghh;\n", "Failed to convert to C") d = nodes.Decl('GG', nodes.PrimaryType('XXX')) qual = d.ctype qual = qual.link(nodes.QualType(nodes.Qualifiers.CONST)) qual = qual.link(nodes.PointerType()) qual = qual.link(nodes.ParenType()) qual = qual.link(nodes.ArrayType(nodes.Literal("12"))) qual = qual.link(nodes.ParenType()) qual = qual.link(nodes.ArrayType(nodes.Literal("66"))) self.assertEqual(str(d.to_c()), "XXX ((*const GG)[12])[66];\n", "Failed to convert to C") ft = nodes.FuncType('HHHHH', [ nodes.Decl('a', nodes.PrimaryType('size_t')), nodes.Decl('b', nodes.PrimaryType('int')) ]) d = nodes.Decl('func', ft) self.assertEqual(str(d.to_c()), "HHHHH func(size_t a, int b);\n", "Failed to convert to C")