Imported Upstream version 1.39.3
[platform/upstream/gobject-introspection.git] / giscanner / testcodegen.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2010  Red Hat, Inc.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 #
20
21 from StringIO import StringIO
22 from . import ast
23 from .codegen import CCodeGenerator
24
25 DEFAULT_C_VALUES = {ast.TYPE_ANY: 'NULL',
26                     ast.TYPE_STRING: '""',
27                     ast.TYPE_FILENAME: '""',
28                     ast.TYPE_GTYPE: 'g_object_get_type ()'}
29
30
31 def get_default_for_typeval(typeval):
32     default = DEFAULT_C_VALUES.get(typeval)
33     if default:
34         return default
35     return "0"
36
37
38 def uscore_from_type(typeval):
39     if typeval.target_fundamental:
40         return typeval.target_fundamental.replace(' ', '_')
41     elif typeval.target_giname:
42         return typeval.target_giname.replace('.', '').lower()
43     else:
44         assert False, typeval
45
46
47 class EverythingCodeGenerator(object):
48
49     def __init__(self, out_h_filename, out_c_filename):
50         self.namespace = ast.Namespace('Everything', '1.0')
51         self.gen = CCodeGenerator(self.namespace, out_h_filename, out_c_filename)
52
53     def write(self):
54         func = ast.Function('nullfunc',
55                             ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
56                             [], False, self.gen.gen_symbol('nullfunc'))
57         self.namespace.append(func)
58         body = "  return;\n"
59         self.gen.set_function_body(func, body)
60
61         # First pass, generate constant returns
62         prefix = 'const return '
63         for typeval in ast.INTROSPECTABLE_BASIC:
64             name = prefix + uscore_from_type(typeval)
65             sym = self.gen.gen_symbol(name)
66             func = ast.Function(name,
67                                 ast.Return(typeval, transfer=ast.PARAM_TRANSFER_NONE),
68                                 [], False, sym)
69             self.namespace.append(func)
70             default = get_default_for_typeval(typeval)
71             body = "  return %s;\n" % (default, )
72             self.gen.set_function_body(func, body)
73
74         # Void return, one parameter
75         prefix = 'oneparam '
76         for typeval in ast.INTROSPECTABLE_BASIC:
77             if typeval is ast.TYPE_NONE:
78                 continue
79             name = prefix + uscore_from_type(typeval)
80             sym = self.gen.gen_symbol(name)
81             func = ast.Function(name,
82                                 ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
83                                 [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
84                                                direction=ast.PARAM_DIRECTION_IN)], False, sym)
85             self.namespace.append(func)
86             self.gen.set_function_body(func, "  return;\n")
87
88         # Void return, one (out) parameter
89         prefix = 'one_outparam '
90         for typeval in ast.INTROSPECTABLE_BASIC:
91             if typeval is ast.TYPE_NONE:
92                 continue
93             name = prefix + uscore_from_type(typeval)
94             sym = self.gen.gen_symbol(name)
95             func = ast.Function(name,
96                                 ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
97                                 [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
98                                                direction=ast.PARAM_DIRECTION_OUT)], False, sym)
99             self.namespace.append(func)
100             body = StringIO('w')
101             default = get_default_for_typeval(func.retval)
102             body.write("  *arg0 = %s;\n" % (default, ))
103             body.write("  return;\n")
104             self.gen.set_function_body(func, body.getvalue())
105
106         # Passthrough one parameter
107         prefix = 'passthrough_one '
108         for typeval in ast.INTROSPECTABLE_BASIC:
109             if typeval is ast.TYPE_NONE:
110                 continue
111             name = prefix + uscore_from_type(typeval)
112             sym = self.gen.gen_symbol(name)
113             func = ast.Function(name, ast.Return(typeval, transfer=ast.PARAM_TRANSFER_NONE),
114                             [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
115                                        direction=ast.PARAM_DIRECTION_IN)], False, sym)
116             self.namespace.append(func)
117             body = StringIO('w')
118             default = get_default_for_typeval(func.retval)
119             body.write("  return arg0;\n")
120             self.gen.set_function_body(func, body.getvalue())
121
122         self.gen.codegen()