summaryrefslogtreecommitdiff
path: root/giscanner/testcodegen.py
blob: 32139e3b5f73b6b93633e0a875a4bc76a4246bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- Mode: Python -*-
# GObject-Introspection - a framework for introspecting GObject libraries
# Copyright (C) 2010  Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

from StringIO import StringIO
from . import ast
from .codegen import CCodeGenerator

DEFAULT_C_VALUES = {ast.TYPE_ANY: 'NULL',
                    ast.TYPE_STRING: '""',
                    ast.TYPE_FILENAME: '""',
                    ast.TYPE_GTYPE: 'g_object_get_type ()'}


def get_default_for_typeval(typeval):
    default = DEFAULT_C_VALUES.get(typeval)
    if default:
        return default
    return "0"


def uscore_from_type(typeval):
    if typeval.target_fundamental:
        return typeval.target_fundamental.replace(' ', '_')
    elif typeval.target_giname:
        return typeval.target_giname.replace('.', '').lower()
    else:
        assert False, typeval


class EverythingCodeGenerator(object):

    def __init__(self,
                 out_h_filename,
                 out_c_filename,
                 function_decoration,
                 include_first_header,
                 include_last_header,
                 include_first_src,
                 include_last_src):
        self.namespace = ast.Namespace('Everything', '1.0')
        self.gen = CCodeGenerator(self.namespace,
                                  out_h_filename,
                                  out_c_filename,
                                  function_decoration,
                                  include_first_header,
                                  include_last_header,
                                  include_first_src,
                                  include_last_src)

    def write(self):
        func = ast.Function('nullfunc',
                            ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
                            [], False, self.gen.gen_symbol('nullfunc'))
        self.namespace.append(func)
        body = "  return;\n"
        self.gen.set_function_body(func, body)

        # First pass, generate constant returns
        prefix = 'const return '
        for typeval in ast.INTROSPECTABLE_BASIC:
            name = prefix + uscore_from_type(typeval)
            sym = self.gen.gen_symbol(name)
            func = ast.Function(name,
                                ast.Return(typeval, transfer=ast.PARAM_TRANSFER_NONE),
                                [], False, sym)
            self.namespace.append(func)
            default = get_default_for_typeval(typeval)
            body = "  return %s;\n" % (default, )
            self.gen.set_function_body(func, body)

        # Void return, one parameter
        prefix = 'oneparam '
        for typeval in ast.INTROSPECTABLE_BASIC:
            if typeval is ast.TYPE_NONE:
                continue
            name = prefix + uscore_from_type(typeval)
            sym = self.gen.gen_symbol(name)
            func = ast.Function(name,
                                ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
                                [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
                                               direction=ast.PARAM_DIRECTION_IN)], False, sym)
            self.namespace.append(func)
            self.gen.set_function_body(func, "  return;\n")

        # Void return, one (out) parameter
        prefix = 'one_outparam '
        for typeval in ast.INTROSPECTABLE_BASIC:
            if typeval is ast.TYPE_NONE:
                continue
            name = prefix + uscore_from_type(typeval)
            sym = self.gen.gen_symbol(name)
            func = ast.Function(name,
                                ast.Return(ast.TYPE_NONE, transfer=ast.PARAM_TRANSFER_NONE),
                                [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
                                               direction=ast.PARAM_DIRECTION_OUT)], False, sym)
            self.namespace.append(func)
            body = StringIO('w')
            default = get_default_for_typeval(func.retval)
            body.write("  *arg0 = %s;\n" % (default, ))
            body.write("  return;\n")
            self.gen.set_function_body(func, body.getvalue())

        # Passthrough one parameter
        prefix = 'passthrough_one '
        for typeval in ast.INTROSPECTABLE_BASIC:
            if typeval is ast.TYPE_NONE:
                continue
            name = prefix + uscore_from_type(typeval)
            sym = self.gen.gen_symbol(name)
            func = ast.Function(name, ast.Return(typeval, transfer=ast.PARAM_TRANSFER_NONE),
                            [ast.Parameter('arg0', typeval, transfer=ast.PARAM_TRANSFER_NONE,
                                       direction=ast.PARAM_DIRECTION_IN)], False, sym)
            self.namespace.append(func)
            body = StringIO('w')
            default = get_default_for_typeval(func.retval)
            body.write("  return arg0;\n")
            self.gen.set_function_body(func, body.getvalue())

        self.gen.codegen()