Add gobject-introspection.changes file
[profile/ivi/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 def get_default_for_typeval(typeval):
31     default = DEFAULT_C_VALUES.get(typeval)
32     if default:
33         return default
34     return "0"
35
36 def uscore_from_type(typeval):
37     if typeval.target_fundamental:
38         return typeval.target_fundamental.replace(' ', '_')
39     elif typeval.target_giname:
40         return typeval.target_giname.replace('.', '').lower()
41     else:
42         assert False, typeval
43
44 class EverythingCodeGenerator(object):
45
46     def __init__(self, out_h_filename, out_c_filename):
47         self.namespace = ast.Namespace('Everything', '1.0')
48         self.gen = CCodeGenerator(self.namespace, out_h_filename, out_c_filename)
49
50     def write(self):
51         func = ast.Function('nullfunc',
52                             ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
53                             [], False, self.gen.gen_symbol('nullfunc'))
54         self.namespace.append(func)
55         body = "  return;\n"
56         self.gen.set_function_body(func, body)
57
58         # First pass, generate constant returns
59         prefix = 'const return '
60         for typeval in ast.INTROSPECTABLE_BASIC:
61             name = prefix + uscore_from_type(typeval)
62             sym = self.gen.gen_symbol(name)
63             func = ast.Function(name,
64                                 ast.Return(typeval, transfer=ast.PARAM_TRANSFER_NONE),
65                                 [], False, sym)
66             self.namespace.append(func)
67             default = get_default_for_typeval(typeval)
68             body = "  return %s;\n" % (default, )
69             self.gen.set_function_body(func, body)
70
71         # Void return, one parameter
72         prefix = 'oneparam '
73         for typeval in ast.INTROSPECTABLE_BASIC:
74             if typeval is ast.TYPE_NONE:
75                 continue
76             name = prefix + uscore_from_type(typeval)
77             sym = self.gen.gen_symbol(name)
78             func = ast.Function(name,
79                                 ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
80                                 [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
81                                                direction=ast.PARAM_DIRECTION_IN)], False, sym)
82             self.namespace.append(func)
83             self.gen.set_function_body(func, "  return;\n")
84
85         # Void return, one (out) parameter
86         prefix = 'one_outparam '
87         for typeval in ast.INTROSPECTABLE_BASIC:
88             if typeval is ast.TYPE_NONE:
89                 continue
90             name = prefix + uscore_from_type(typeval)
91             sym = self.gen.gen_symbol(name)
92             func = ast.Function(name,
93                                 ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
94                                 [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
95                                                direction=ast.PARAM_DIRECTION_OUT)], False, sym)
96             self.namespace.append(func)
97             body = StringIO('w')
98             default = get_default_for_typeval(func.retval)
99             body.write("  *arg0 = %s;\n" % (default, ))
100             body.write("  return;\n")
101             self.gen.set_function_body(func, body.getvalue())
102
103         # Passthrough one parameter
104         prefix = 'passthrough_one '
105         for typeval in ast.INTROSPECTABLE_BASIC:
106             if typeval is ast.TYPE_NONE:
107                 continue
108             name = prefix + uscore_from_type(typeval)
109             sym = self.gen.gen_symbol(name)
110             func = ast.Function(name, ast.Return(typeval, transfer=ast.PARAM_TRANSFER_NONE),
111                             [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
112                                        direction=ast.PARAM_DIRECTION_IN)], False, sym)
113             self.namespace.append(func)
114             body = StringIO('w')
115             default = get_default_for_typeval(func.retval)
116             body.write("  return arg0;\n")
117             self.gen.set_function_body(func, body.getvalue())
118
119         self.gen.codegen()