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 add_ary(self, lspec, expr): if not hasattr(lspec, 'ctype') or lspec.ctype is None: lspec.ctype = nodes.makeCType('int') if not hasattr(lspec, 'cur_paren'): lspec.cur_paren = [] lspec.cur_paren.append(ref(lspec.ctype)) lspec.cur_paren[-1]().push(nodes.ArrayType(expr)) return True
def open_params(self, lspec): if lspec.ctype is None: lspec.ctype = nodes.makeCType('int') if not hasattr(lspec, 'cur_paren'): lspec.cur_paren = [] lspec.cur_paren.append(ref(lspec.ctype)) if not hasattr(lspec.cur_paren[-1](), '_params'): lspec.cur_paren[-1]()._params = [] return True
def add_paren(self, lspec): if not hasattr(lspec, 'ctype') or lspec.ctype is None: lspec.ctype = nodes.makeCType('int') paren = nodes.ParenType() if not hasattr(lspec, 'cur_paren'): lspec.cur_paren = [] lspec.cur_paren.append(ref(lspec.ctype)) last = lspec.cur_paren.pop() lspec.cur_paren.append(ref(paren)) lspec.cur_paren.append(last) lspec.ctype.push(paren) return True
def new_decl_spec(self, lspec, i, current_block): idsetval = "" i_value = self.value(i) if i_value in Idset: idsetval = Idset[i_value] # don't f**k with reserved keywords if idsetval == "reserved": return False # not for asm or attribute if idsetval != "" and idsetval[0] != 'a': lspec.ctype = nodes.makeCType(i_value, lspec.ctype) return True if ((hasattr(current_block.ref, 'types') and i_value in current_block.ref.types)): if lspec.ctype is None: lspec.ctype = nodes.PrimaryType(i_value) else: lspec.ctype._identifier = i_value lspec.ctype._identifier = i_value return True return False
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")
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 add_attr_specifier(self, lspec, attrspec): if lspec.ctype is None: lspec.ctype = nodes.makeCType('int', lspec.ctype) lspec.ctype.push(nodes.AttrType(self.value(attrspec))) 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")