2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2010 Red Hat, Inc.
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.
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.
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.
21 from StringIO import StringIO
23 from .codegen import CCodeGenerator
25 DEFAULT_C_VALUES = {ast.TYPE_ANY: 'NULL',
26 ast.TYPE_STRING: '""',
27 ast.TYPE_FILENAME: '""',
28 ast.TYPE_GTYPE: 'g_object_get_type ()'}
30 def get_default_for_typeval(typeval):
31 default = DEFAULT_C_VALUES.get(typeval)
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()
44 class EverythingCodeGenerator(object):
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)
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)
56 self.gen.set_function_body(func, body)
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),
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)
71 # Void return, one parameter
73 for typeval in ast.INTROSPECTABLE_BASIC:
74 if typeval is ast.TYPE_NONE:
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")
85 # Void return, one (out) parameter
86 prefix = 'one_outparam '
87 for typeval in ast.INTROSPECTABLE_BASIC:
88 if typeval is ast.TYPE_NONE:
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)
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())
103 # Passthrough one parameter
104 prefix = 'passthrough_one '
105 for typeval in ast.INTROSPECTABLE_BASIC:
106 if typeval is ast.TYPE_NONE:
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)
115 default = get_default_for_typeval(func.retval)
116 body.write(" return arg0;\n")
117 self.gen.set_function_body(func, body.getvalue())