summaryrefslogtreecommitdiff
path: root/src/bin/eolian_mono/eolian/mono/function_definition.hh
blob: 14c70791f64ced1753a96ca8438662cbf4bf208d (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef EOLIAN_MONO_FUNCTION_DEFINITION_HH
#define EOLIAN_MONO_FUNCTION_DEFINITION_HH

#include <Eina.hh>

#include "grammar/generator.hpp"
#include "grammar/klass_def.hpp"

#include "grammar/indentation.hpp"
#include "grammar/list.hpp"
#include "grammar/alternative.hpp"
#include "grammar/attribute_reorder.hpp"
#include "type.hh"
#include "name_helpers.hh"
#include "helpers.hh"
#include "function_helpers.hh"
#include "marshall_type.hh"
#include "parameter.hh"
#include "keyword.hh"
#include "documentation.hh"
#include "using_decl.hh"
#include "generation_contexts.hh"
#include "blacklist.hh"

namespace eolian_mono {

struct native_function_definition_generator
{
  attributes::klass_def const* klass;
  
  template <typename OutputIterator, typename Context>
  bool generate(OutputIterator sink, attributes::function_def const& f, Context const& context) const
  {
    if(blacklist::is_function_blacklisted(f.c_name) || f.is_static) // Only Concrete classes implement static methods.
      return true;
    else
      {
    if(!as_generator
       ("\n\n" << scope_tab
        << eolian_mono::marshall_native_annotation(true)
        << " private delegate "
        << eolian_mono::marshall_type(true)
        << " "
        << string
        << "_delegate(System.IntPtr obj, System.IntPtr pd"
        << *grammar::attribute_reorder<-1, -1>
        (
         (", " << marshall_native_annotation << " " << marshall_parameter)
        )
        << ");\n")
       .generate(sink, std::make_tuple(f.return_type, f.return_type, f.c_name, f.parameters), context))
      return false;

    if(!as_generator
       (scope_tab << "[System.Runtime.InteropServices.DllImport(" << context_find_tag<library_context>(context).actual_library_name(f.filename) << ")] "
        << eolian_mono::marshall_native_annotation(true)
        << " private static extern "
        << eolian_mono::marshall_type(true)
        << " " << string
        << "(System.IntPtr obj"
        << *grammar::attribute_reorder<-1, -1>
        (
         (", " << marshall_native_annotation << " " << marshall_parameter)
        )
        << ");\n")
       .generate(sink, std::make_tuple(f.return_type, f.return_type, f.c_name, f.parameters), context))
      return false;

    std::string return_type;
    if(!as_generator(eolian_mono::type(true)).generate(std::back_inserter(return_type), f.return_type, context))
      return false;

    if(!as_generator
       (scope_tab
        << " private static "
        << eolian_mono::marshall_type(true) << " "
        << string
        << "(System.IntPtr obj, System.IntPtr pd"
        << *(", " << marshall_parameter)
        << ")\n"
        << scope_tab << "{\n"
        /****/
        << scope_tab << scope_tab << "eina.Log.Debug(\"function " << string << " was called\");\n"
        /****/
        << scope_tab << scope_tab << "efl.eo.IWrapper wrapper = efl.eo.Globals.data_get(pd);\n"
        << scope_tab << scope_tab << "if(wrapper != null) {\n"
        << scope_tab << scope_tab << scope_tab << eolian_mono::native_function_definition_preamble()
        << scope_tab << scope_tab << scope_tab << "try {\n"
        << scope_tab << scope_tab << scope_tab << scope_tab << (return_type != " void" ? "_ret_var = " : "") << "((" << string << "Inherit)wrapper)." << string
        << "(" << (native_argument_invocation % ", ") << ");\n"
        << scope_tab << scope_tab << scope_tab << "} catch (Exception e) {\n"
        << scope_tab << scope_tab << scope_tab << scope_tab << "eina.Log.Warning($\"Callback error: {e.ToString()}\");\n"
        << scope_tab << scope_tab << scope_tab << scope_tab << "eina.Error.Set(eina.Error.EFL_ERROR);\n"
        << scope_tab << scope_tab << scope_tab << "}\n"
        << eolian_mono::native_function_definition_epilogue(*klass)
        << scope_tab << scope_tab << "} else {\n"
        << scope_tab << scope_tab << scope_tab << (return_type != " void" ? "return " : "") << string
        << "(efl.eo.Globals.efl_super(obj, " << string << "Inherit.klass)" << *(", " << argument) << ");\n"
        << scope_tab << scope_tab << "}\n"
        << scope_tab << "}\n"
       )
       .generate(sink, std::make_tuple(f.return_type, escape_keyword(f.name), f.parameters
                                       , /***/f.c_name/***/
                                       , f
                                       , klass->cxx_name, managed_method_name(f.name)
                                       , f.parameters
                                       , f
                                       , f.c_name
                                       , klass->cxx_name, f.parameters
                                      )
                 , context))
      return false;

    if(!as_generator
       (scope_tab << "private static  "
        << string
        << "_delegate "
        << string << "_static_delegate = new " << string << "_delegate(" << string << "NativeInherit." << string << ");\n"
       )
       .generate(sink, std::make_tuple(f.c_name, f.c_name, f.c_name, klass->cxx_name
                                       , escape_keyword(f.name)), context))
      return false;
    return true;
      }
  }
};
  
struct function_definition_generator
{
  function_definition_generator(bool do_super = false)
    : do_super(do_super)
  {}
  
  template <typename OutputIterator, typename Context>
  bool generate(OutputIterator sink, attributes::function_def const& f, Context const& context) const
  {
    if(do_super && f.is_static) // Static methods goes only on Concrete classes.
      return true;
    if(blacklist::is_function_blacklisted(f.c_name))
      return true;

    if(!as_generator
       ("\n\n" << scope_tab << "[System.Runtime.InteropServices.DllImport(" << context_find_tag<library_context>(context).actual_library_name(f.filename) << ")]\n"
        << scope_tab << eolian_mono::marshall_annotation(true)
        << " private static extern "
        << eolian_mono::marshall_type(true)
        << " " << string
        << "(System.IntPtr obj"
        << *grammar::attribute_reorder<-1, -1>
        (
         (", " << marshall_annotation << " " << marshall_parameter)
        )
        << ");\n")
       .generate(sink, std::make_tuple(f.return_type, f.return_type, f.c_name, f.parameters), context))
      return false;

    std::string return_type;
    if(!as_generator(eolian_mono::type(true)).generate(std::back_inserter(return_type), f.return_type, context))
      return false;

    if(!as_generator
       (documentation(1)).generate(sink, f, context))
      return false;

    if(!as_generator
       (scope_tab << (do_super ? "virtual " : "") << "public " << (f.is_static ? "static " : "") << return_type << " " << string << "(" << (parameter % ", ")
        << ") {\n "
        << eolian_mono::function_definition_preamble() << string << "("
        << (do_super ? "efl.eo.Globals.efl_super(" : "")
        << (f.is_static ? name_helpers::klass_get_name(f.klass) + "()": "this.raw_handle")
        << (do_super ? ", this.raw_klass)" : "")
        << *(", " << argument_invocation ) << ");\n"
        << eolian_mono::function_definition_epilogue()
        << " }\n")
       .generate(sink, std::make_tuple(managed_method_name(f.name), f.parameters, f, f.c_name, f.parameters, f), context))
      return false;

    return true;
  }

  bool do_super;
};

struct function_definition_parameterized
{
  function_definition_generator operator()(bool do_super) const
  {
    return {do_super};
  }
} const function_definition;
function_definition_generator as_generator(function_definition_parameterized)
{
  return {};
}
struct native_function_definition_parameterized
{
  native_function_definition_generator operator()(attributes::klass_def const& klass) const
  {
    return {&klass};
  }
} const native_function_definition;

}

namespace efl { namespace eolian { namespace grammar {

template <>
struct is_eager_generator< ::eolian_mono::function_definition_generator> : std::true_type {};
template <>
struct is_eager_generator< ::eolian_mono::native_function_definition_generator> : std::true_type {};
template <>
struct is_generator< ::eolian_mono::function_definition_generator> : std::true_type {};
template <>
struct is_generator< ::eolian_mono::native_function_definition_generator> : std::true_type {};
template <>
struct is_generator< ::eolian_mono::function_definition_parameterized> : std::true_type {};

namespace type_traits {
template <>
struct attributes_needed< ::eolian_mono::function_definition_generator> : std::integral_constant<int, 1> {};

template <>
struct attributes_needed< ::eolian_mono::function_definition_parameterized> : std::integral_constant<int, 1> {};

template <>
struct attributes_needed< ::eolian_mono::native_function_definition_generator> : std::integral_constant<int, 1> {};
}
      
} } }

#endif